[Rd] model.frame and predvars

Terry Therneau therneau at mayo.edu
Tue Jun 5 17:17:58 CEST 2012


I was looking at how the model.frame method for lm works and comparing 
it to my own for coxph.
The big difference is that I try to retain xlevels and predvars 
information for a new model frame, and lm does not.
I use a call to model.frame in predict.coxph, which is why I went that 
route, but never noted the difference till now (preparing for my course 
in Nashville).

Could someone shed light on the rationale for non-preservation?

Terry T.


Simple example

 > library(survival)

 > lfit <- lm(time ~ factor(ph.ecog) + ns(age, 3), data=lung)
 > ltemp <- model.frame(lfit, data=lung[1:2,])
 > ltemp
   time factor(ph.ecog) ns(age, 3).1 ns(age, 3).2 ns(age, 3).3
1  306               1   -0.1428571    0.4285714    0.7142857
2  455               0    0.0000000    0.0000000    0.0000000

 > lfit$model[1:2,]
   time factor(ph.ecog) ns(age, 3).1 ns(age, 3).2 ns(age, 3).3
1  306               1    0.4443546    0.3286161    0.1900511
2  455               0    0.5697239    0.3618440   -0.1297479

 > levels(ltemp[[2]])
[1] "0" "1"

 > levels(lfit$model[[2]])
[1] "0" "1" "2" "3"

 > cfit <- coxph(Surv(time, status) ~ factor(ph.ecog) + ns(age,3), lung)
 > model.frame(cfit, data= lung[1:2,])
   Surv(time, status) factor(ph.ecog) ns(age, 3).1 ns(age, 3).2 ns(age, 3).3
1               306                1    0.4443546    0.3286161    0.1900511
2               455                0    0.5697239    0.3618440   -0.1297479



More information about the R-devel mailing list