Skip to content
Prev 308815 / 398503 Next

Data type in a data frame

This is somewhat tangential, but if you plan on using
  predict(fit,newdata=nd)
after fitting a model like
  fit <- lm(y~x, data=d)
be sure you have converted character columns in nd and d into factors.
Otherwise you are likely to get errors from predict().   You will get
a warning when fitting the model if you use character columns, but
the results are ok until you use predict() on the result.

E.g.,
Warning message:
In model.matrix.default(mt, mf, contrasts) :
  variable 'cGroup' converted to a factor
cGroupA cGroupB cGroupC 
    2.0     5.5     8.5
fGroupA fGroupB fGroupC 
    2.0     5.5     8.5
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
  contrasts can be applied only to factors with 2 or more levels
In addition: Warning message:
In model.matrix.default(Terms, m, contrasts.arg = object$contrasts) :
  variable 'cGroup' converted to a factor
10 
8.5
Error in predict.lm(fitChar, newdata = d[c(1, 10), ]) : 
  subscript out of bounds
In addition: Warning message:
In model.matrix.default(Terms, m, contrasts.arg = object$contrasts) :
  variable 'cGroup' converted to a factor
1  10 
2.0 8.5


Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com