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

From: John Maindonald <john.maindonald_at_anu.edu.au>
Date: Tue, 01 May 2007 14:26:04 +1000


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.



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Tue 01 May 2007 - 12:55:10 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 01 May 2007 - 13:33:16 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.