[Rd] Evaluation without using the parent frame

Duncan Murdoch murdoch.duncan at gmail.com
Wed May 16 23:59:55 CEST 2012


On 12-05-16 4:59 PM, Terry Therneau wrote:
> 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?

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

>
> 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.
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list