[Rd] model frames and update()
Therneau, Terry M., Ph.D.
therneau at mayo.edu
Thu Apr 23 18:58:14 CEST 2015
This issue has arisen within my anova.coxph routine, but is as easily illustrated with glm.
testdata <- data.frame(y= 1:5,
sex = c(0,1,0,1,1),
age = c(30,20,35,25,40))
fit <- glm(cbind(y,n) ~ age + sex, binomial, data=testdata, model=TRUE)
saveit <- fit$model
update(fit, .~. - age, data=saveit)
Error in cbind(y, n) : object 'y' not found
One would hope that a saved model frame is precisely the thing that would work best. The
issue of course is that "cbind(y, n)" is the name of the first variable in saveit, and it
is not being properly quoted somewhere down the line. The same issue can occur on the
right hand side. "Save the model frame in case you need to refit something next month" is
does not appear to be a safe approach to reproducable research.
fit2 <- glm(y ~ sex + log(age), poisson, testdata)
save2 <- fit2$model
update(fit2, . ~ . - sex, data=save2) # fails
glm(y ~ log(age), poisson, save2) # fails
I can work around this in my anova, but I wanted to not rebuild the frame if it is already
present. It looks like model.matrix plus attr(x, 'assign') time -- a bit harder to read,
but that looks like what anova.glm is doing. Is there a way to make update work?
The current code, BTW, starts by building its own frame using results of terms.inner,
which solves the above issue nicely and update() works as expected. But it isn't robust
to scoping issues. (As pointed out yesterday by a user: lapply of a function that
contained coxph followed by anova gives a variable not found error.) It also ignores
saved model frames; thus the rewrite.
More information about the R-devel