0

Whenever I try to run the following script,

lm.fit <- glm(res~., data=train) #res is class of my dataset

I get the following error

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

my dataset details:

15 variable, 10 of them are integer and 5 of them are factor.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • 2
    That means you have a categorical variables in your data frame (factor or character) that has only a single value. – joran Nov 04 '18 at 00:55
  • 1
    try `sapply(train, function(x) if (is.factor(x)) length(levels(x)) else NA)` to see where the problem is ... – Ben Bolker Nov 04 '18 at 01:36
  • Question has nothing to do with `neural-network` - kindly do not spam the tag (removed). – desertnaut Nov 04 '18 at 12:21

1 Answers1

0

As commented, you should check which one of your factors has less than 2 levels.

sapply(train, function(x) if (is.factor(x)) length(levels(x)) else NA)

Would do the job as said by Ben Bolker

gaut
  • 5,771
  • 1
  • 14
  • 45