hoguejm
2012-Oct-23 18:43 UTC
[R] Error in contrasts message when using logistic regression code.
I have a rather large data set (about 30 predictor variables) I need to preform a logistic regression on this data. My response variable is binary. My code looks like this: mylogit <- glm(Enrolled ~ A + B + C + ... + EE, data = data, family binomial(link="logit")) with A,B,C, ... as my predictor variables. Some categorical, some continuous, some binary. I run the code and get this error: Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more levels Any ideas on how to fix this? I am fairly new to R so Im guessing there is something missing/wrong in my logit code. Thanks! -- View this message in context: http://r.789695.n4.nabble.com/Error-in-contrasts-message-when-using-logistic-regression-code-tp4647198.html Sent from the R help mailing list archive at Nabble.com.
Marc Schwartz
2012-Oct-23 19:40 UTC
[R] Error in contrasts message when using logistic regression code.
On Oct 23, 2012, at 1:43 PM, hoguejm <hoguejm at gmail.com> wrote:> I have a rather large data set (about 30 predictor variables) > > I need to preform a logistic regression on this data. My response variable > is binary. > > My code looks like this: > > mylogit <- glm(Enrolled ~ A + B + C + ... + EE, data = data, family > binomial(link="logit")) > > with A,B,C, ... as my predictor variables. Some categorical, some > continuous, some binary. > > I run the code and get this error: > > Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : > contrasts can be applied only to factors with 2 or more levels > > Any ideas on how to fix this? I am fairly new to R so Im guessing there is > something missing/wrong in my logit code. > > Thanks!More than likely, one or more of your categorical variables are not set up as factors, or if they are, then one or more have only a single factor level present in the dataset that is used for the model, thus are constant. You might want to run: # see ?str str(data) and: # see ?summary summary(data) to get a sense of the structure of your data set (to make sure that you have factors and not character vectors) and to get a sense for the distribution of your IVs. Regards, Marc Schwartz
hoguejm
2012-Oct-23 20:57 UTC
[R] Error in contrasts message when using logistic regression code.
How to change categorical vars to factors. I am very new at R Thanks -- View this message in context: http://r.789695.n4.nabble.com/Error-in-contrasts-message-when-using-logistic-regression-code-tp4647198p4647226.html Sent from the R help mailing list archive at Nabble.com.
David Winsemius
2012-Oct-24 00:26 UTC
[R] Error in contrasts message when using logistic regression code.
On Oct 23, 2012, at 1:57 PM, hoguejm wrote:> How to change categorical vars to factors.There is nio such thing as a "categorical var" in R, although the closest thing to one is a factor class vector. If you have either numeric or character vectors and you want them to become factors: facvar <- factor(oldvar)> I am very new at RYes. Consider abandoning Nabble. And do read the Posting Guide.> View this message in context: http://r.789695.n4.nabble.com/Error-in-contrasts-message-when-using-logistic-regression-code-tp4647198p4647226.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius, MD Alameda, CA, USA