glm

How to do a GLM when “contrasts can be applied only to factors with 2 or more levels”?

心已入冬 提交于 2019-11-27 08:22:23
问题 I want to do a regression in R using glm , but is there a way to do it since I get the contrasts error. mydf <- data.frame(Group=c(1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12), WL=rep(c(1,0),12), New.Runner=c("N","N","N","N","N","N","Y","N","N","N","N","N","N","Y","N","N","N","Y","N","N","N","N","N","Y"), Last.Run=c(1,5,2,6,5,4,NA,3,7,2,4,9,8,NA,3,5,1,NA,6,10,7,9,2,NA)) mod <- glm(formula = WL~New.Runner+Last.Run, family = binomial, data = mydf) #Error in `contrasts<-`(`*tmp*`,

how do i exclude specific variables from a glm in R?

夙愿已清 提交于 2019-11-27 06:36:54
问题 I have 50 variables. This is how I use them all in my glm. var = glm(Stuff ~ ., data=mydata, family=binomial) But I want to exclude 2 of them. So how do I exclude 2 in specific? I was hoping there would be something like this: var = glm(Stuff ~ . # notthisstuff, data=mydata, family=binomial) thoughts? 回答1: In addition to using the - like in the comments glm(Stuff ~ . - var1 - var2, data= mydata, family=binomial) you can also subset the data frame passed in glm(Stuff ~ ., data=mydata[ , !

R : Pass argument to glm inside an R function

倾然丶 夕夏残阳落幕 提交于 2019-11-27 05:36:20
I am trying to get used to scoping issues in R. I'd like to call the function glm() inside a function but it does not work, apparently for scoping reasons I did not manage to fix with the functions assign() or eval() . Here is a simplified version: ao <- function (y, x, phi = seq (0,1,0.1), dataset, weights) { logLikvector <- rep(0,length(phi)) # vector of zeros to be replaced thereafter for (i in 1:length(phi)) { # loop to use glm() fit <- glm (y ~ x, data = dataset, family = binomial, weights = weights) logLikvector[i] <- logLik(fit) # get log likelihood } logLikvector } Now I want to use

Extract standard errors from glm

為{幸葍}努か 提交于 2019-11-27 04:33:59
问题 I did a glm and I just want to extract the standard errors of each coefficient. I saw on the internet the function se.coef() but it doesn't work, it returns "Error: could not find function "se.coef"" . 回答1: The information you're after is stored in the coefficients object returned by summary() . You can extract it thusly: summary(glm.D93)$coefficients[, 2] #Example from ?glm counts <- c(18,17,15,20,10,20,25,13,12) outcome <- gl(3,1,9) treatment <- gl(3,3) print(d.AD <- data.frame(treatment,

R error which says “Models were not all fitted to the same size of dataset”

无人久伴 提交于 2019-11-26 14:39:40
问题 I have created two generalised linear models as follows: glm1 <-glm(Y ~ X1 + X2 + X3, family=binomial(link=logit)) glm2 <-glm(Y ~ X1 + X2, family=binomial(link=logit)) I then use the anova function: anova(glm2,glm1) but get an error message: "Error in anova.glmlist(c(list(object),dotargs), dispersion = dispersion, : models were not all fitted to the same size of dataset" What does this mean and how can I fix this? I have attach ed the dataset at the start of my code so both models are working

Specifying formula in R with glm without explicit declaration of each covariate

蓝咒 提交于 2019-11-26 12:01:43
问题 I would like to force specific variables into glm regressions without fully specifying each one. My real data set has ~200 variables. I haven\'t been able to find samples of this in my online searching thus far. For example (with just 3 variables): n=200 set.seed(39) samp = data.frame(W1 = runif(n, min = 0, max = 1), W2=runif(n, min = 0, max = 5)) samp = transform(samp, # add A A = rbinom(n, 1, 1/(1+exp(-(W1^2-4*W1+1))))) samp = transform(samp, # add Y Y = rbinom(n, 1,1/(1+exp(-(A-sin(W1^2)

How to succinctly write a formula with many variables from a data frame?

可紊 提交于 2019-11-25 23:52:39
问题 Suppose I have a response variable and a data containing three covariates (as a toy example): y = c(1,4,6) d = data.frame(x1 = c(4,-1,3), x2 = c(3,9,8), x3 = c(4,-4,-2)) I want to fit a linear regression to the data: fit = lm(y ~ d$x1 + d$x2 + d$y2) Is there a way to write the formula, so that I don\'t have to write out each individual covariate? For example, something like fit = lm(y ~ d) (I want each variable in the data frame to be a covariate.) I\'m asking because I actually have 50

How to debug “contrasts can be applied only to factors with 2 or more levels” error?

喜夏-厌秋 提交于 2019-11-25 21:57:11
问题 Here are all the variables I\'m working with: str(ad.train) $ Date : Factor w/ 427 levels \"2012-03-24\",\"2012-03-29\",..: 4 7 12 14 19 21 24 29 31 34 ... $ Team : Factor w/ 18 levels \"Adelaide\",\"Brisbane Lions\",..: 1 1 1 1 1 1 1 1 1 1 ... $ Season : int 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 ... $ Round : Factor w/ 28 levels \"EF\",\"GF\",\"PF\",..: 5 16 21 22 23 24 25 26 27 6 ... $ Score : int 137 82 84 96 110 99 122 124 49 111 ... $ Margin : int 69 18 -56 46 19 5 50 69 -26