[R] SOS! error in GLM logistic regression...

Gavin Simpson gavin.simpson at ucl.ac.uk
Wed Jul 15 09:11:56 CEST 2009


On Tue, 2009-07-14 at 15:57 -0700, Michael wrote:
> Hi all,
> 
> Could anybody tell me what happened to my logistic regression in R?
> mylog=glm(mytraindata$V1 ~ ., data=mytraindata, family=binomial("logit"))
> 
> It generated the following error message:
> 
> Error in model.frame.default(Terms, newdata, na.action = na.action,
> xlev = object$xlevels) :
>   factor 'state1' has new level(s) AP

Hi Michael,

I am 99.9% certain that what you claim above is completely false. That
error looks to arise from a call in predict.lm which is called when you
use predict.glm

The big give away for me was 'newdata' in the call that produced the
error. That suggested to me you were using a predict method. I then
tracked down a potential source of the error by looking at what happens
when you call predict on a "glm" object. Members of the list shouldn't
have to do this to help answer posts to the list. The posting guide asks
you to provide, minimal, self-contained, reproducible examples. And
failing that, the R code you actually used.

After the chastisement ;-) some help:

1) I'm guessing, but in the dataset that you predict for, does the
state1 variable have more/different levels to the training data? Try
levels(mytraindata) and levels(mytestdata) where mytestdata is the data
you supplied as 'newdata' in your call to predict.

2) Your glm call contains some redundancy. It can be simplified to:

mylog <- glm(V1 ~ ., data = mytraindata, family = binomial)

Realising that the logit link is the default in the binomial family, and
if you specify data you don't need to refer to the data object in the
formula.

HTH

G

-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%




More information about the R-help mailing list