[R] glm - prediction of a factor with several levels

Marc Schwartz marc_schwartz at me.com
Mon Jul 26 15:22:55 CEST 2010

On Jul 26, 2010, at 4:18 AM, blackscorpio wrote:

> Thanks a lot for your anwers.
> To Ben Bolker  : I am trying to perform an ordinal logistic regression to
> predict an Y 3-class variable, having observed 3 continous predictors V1,
> V2, V3.
> With random data my code would be something like :  
> # simulate 10 observations of 3 independant N(0,1) predictors
> X=rmvnorm(10,rep(0,3))
> # variable to predict
> Y=c(1,1,1,2,2,2,3,3,3,3)
> # create data frame
> A=as.data.frame(cbind(X,Y))
> # turn Y into class variable
> A$Y=as.factor(A$Y)
> # perform logisitic regression
> glm(Y~V1+V2+V3,A,family="binomial")
> As only one intercept is returned, it seems indeed that a 2-class model has
> been performed instead, as said by zachmor.
> Although I solved my problem by using polr instead of glm, I'd like to
> understand what glm does in such a case since it gave me better
> well-classification rates with the predict function.
> Thanks a lot again !

>From the Details section of ?glm:

 "For binomial and quasibinomial families the response can also be specified as a factor (****when the first level denotes failure and all others success****) ..."

So "1" is a failure and c("2", "3") denote success, thus a two level response.

BTW, if you are going to use a function (eg. rmvnorm()) from an external package, be sure to include the relevant library() call in your example code so folks don't need to guess which CRAN package(s) may be required to run it.


Marc Schwartz

More information about the R-help mailing list