-1

I am running a logit regression in R with a large number of input variables.

newlogit <- glm(install. ~ SIZES + GROSSCONSUMPTION.... + 
                NETTCONSUMPTION..... + NETTGENERATION....... + 
                GROSSGENERATION.... + Variable. + Fixed + 
                Cost.of.gross.cons + Cost.of.net.cons + Cons.savings + 
                generation.gains + Total.savings + Cost.of.system + 
                Payback + Self.consumption + Total.consumption.as.solar + 
                Owner.occupied + postcode + Suburb + Market.penetration + 
                X..green.vote, data = newdata, family = "binomial")

I am getting this error:

Error in contrasts<-(tmp, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more levels

Would you please be able to advise why this error is occurring and if there is a way to run this regression?

smci
  • 32,567
  • 20
  • 113
  • 146
Cara Teoh
  • 3
  • 2
  • 1
    It's impossible to tell why that's happening without a [minimum reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). My guess is that one or more of the variables in your large number of input variables has only a single unique value. – Alex A. May 01 '15 at 01:44

2 Answers2

2

I've seen this problem happen when one of the variables on the right hand side of the model is a factor with a single level.

If that is the case, all you need to do is to remove that factor from the model.

W7GVR
  • 1,990
  • 1
  • 18
  • 24
1

It objects to one of your variables (as gvrocha said); you might have a factor with only one level, or a string.

A tip to quickly track down the offending variable(s) is to do interval bisection and increase/decrease the col indices till you trigger the error.

Best to use the numerical (column-index) interface to glm (glm(data[,'install.'] ~ data[,2:40]), see [1])

rather than the formula interface glm(install. ~ var1 + var2 + ...:

[1] Dynamic formula creation in R?

Community
  • 1
  • 1
smci
  • 32,567
  • 20
  • 113
  • 146