[Rd] Levels attribute in integer columns created by model.frame()

John Maindonald john.maindonald at anu.edu.au
Tue May 1 06:26:04 CEST 2007


The following is evidence of what is surely an undesirable feature.
The issue is the handling, in calls to model.frame(), of  an
explanatory variable that has been derived as an unclassed
factor. (Ross Darnell drew this to my attention.)

## Data are slightly modified from p.191 of MASS
 > worms <- data.frame(sex=gl(2,6), Dose=factor(rep(2^(0:5),2)),
+                     deaths=c(1,4,9,13,18,20,0,2,6,10,12,16))
 > worms$doselin <- unclass(worms$Dose)
 > class(worms$doselin)
[1] "integer"
 > attributes(worms$doselin)
$levels
[1] "1"  "2"  "4"  "8"  "16" "32"

 > worms.glm <- glm(cbind(deaths, (20-deaths)) ~ sex+ doselin,
+                  data=worms, family=binomial)
 > predict(worms.glm, new=data.frame(sex="1", doselin=6))
Error: variable 'doselin' was fitted with class "other" but class  
"numeric" was supplied
In addition: Warning message:
variable 'doselin' is not a factor in: model.frame.default(Terms,  
newdata, na.action = na.action, xlev = object$xlevels)


The error is reported in the call to model.frame() from predict.lm()
which is called by predict.glm(). It is not clear to me why this call to
model.frame identifies the class that should be expected as "other".

The problem might be fixed by stripping the levels attribute from
any column created by model.frame() that is integer or numeric.

 > ###################################################
 >
 > ## Note the following
 > mframe <- model.frame(cbind(deaths, (20-deaths)) ~ sex+ doselin,
+                       data=worms)
 > class(mframe$doselin)
[1] "integer"
 > attributes(mframe$doselin)
$levels
[1] "1"  "2"  "4"  "8"  "16" "32"


John Maindonald             email: john.maindonald at anu.edu.au
phone : +61 2 (6125)3473    fax  : +61 2(6125)5549
Centre for Mathematics & Its Applications, Room 1194,
John Dedman Mathematical Sciences Building (Building 27)
Australian National University, Canberra ACT 0200.



More information about the R-devel mailing list