[R] unexpected behaviour with sparse.model.matrix

Jarrod Hadfield j.hadfield at ed.ac.uk
Sun Aug 15 19:24:22 CEST 2010


Hi,

I'm trying to get sparse.model.matrix to retain unused levels. I can't  
seem to get this working through the most obvious routes such as  
specifying drop.unused.levels = FALSE in the model.frame or trying to  
pass all levels in xlev,which is an argument to sparse.model.matrix  
(see code below).

Any help would be gratefully received.

Cheers,

Jarrod


fac<-factor(rep(1:10,10), levels=1:100)
mf<-model.frame(~fac, drop.unused.levels = TRUE)
mf2<-model.frame(~fac, drop.unused.levels = FALSE)

dim(model.matrix(~fac, mf))
dim(model.matrix(~fac, mf2))
dim(model.matrix(~fac, xlev=list(fac=1:10)))
dim(model.matrix(~fac, xlev=list(fac=1:100)))

dim(sparse.model.matrix(~fac, mf))
dim(sparse.model.matrix(~fac, mf2))
dim(sparse.model.matrix(~fac, xlev=list(fac=1:10)))
dim(sparse.model.matrix(~fac, xlev=list(fac=1:100)))

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20100815/6064ed25/attachment.pl>


More information about the R-help mailing list