[Rd] Evaluation without the parent frame
Terry Therneau
therneau at mayo.edu
Thu May 17 14:27:55 CEST 2012
Duncan,
I agree completely with "don't use attach"; if I could get all the
users of the survival package to agree as well the problem in question
would go away :-) I'm thinking about ways to add more effective error
surveillance.
Your suggestion was not horribly complex and I'll look into it
further. My first foray failed because what I want (I think) is the
environment that they had when the first ">" came up. I tried baseenv
in a spot, but then my code couldn't find the model.frame function.
Terry T.
On 05/17/2012 05:00 AM, Duncan wrote:
> 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.
>> >
>> >
More information about the R-devel
mailing list