Skip to content

Evaluation without using the parent frame

2 messages · Terry Therneau, Duncan Murdoch

#
I've been tracking down a survival problem from R-help today.  A short 
version of the primary issue is reconstructed by the following simple 
example:

library(survival)
attach(lung)
fit <- coxph(Surv(time, status) ~ log(age))
predict(fit, newdata=data.frame(abe=45))

Note the typo in the last line of "abe" instead of "age".  Instead of an 
error message, this returns predictions for all the subjects since 
model.frame matches "age" by searching more widely.   I'd prefer the error.
I suspect this is hard -- I'd like it to not see the attached lung data 
set, but still be able to find the log function.
Is there a not-horribly-complex solution?

I also tried to change the primary function to lm instead of coxph.  It 
has the same problem, but does print a warning that the newdata and 
results have different lengths (which I will incorporate).

Terry T.
#
On 12-05-16 4:59 PM, Terry Therneau wrote:
The best solution is to not use attach(), use data=lung in the fit.

I think if you want to use attach but limit the search, you need 
something like

predict(fit, newdata=list2env(data.frame(abe=45), parent=baseenv()))

but I don't think that meets your "not horribly complex" criterion.

Duncan Murdoch