[R] update.lm question
Duncan Murdoch
murdoch at stats.uwo.ca
Sun Nov 15 18:14:13 CET 2009
On 15/11/2009 11:28 AM, Duncan Murdoch wrote:
> On 15/11/2009 9:23 AM, Karsten Weinert wrote:
>> Hello,
>> at the Rgui command line I can easily remove a term from a fitted lm
>> object, like
>>
>> fit <- lm(y~x1+x2+x3, data=myData)
>> update(fit, .~.-x1)
>>
>> However, I would like to do this in a function with term given as string, like
>>
>> removeTerm <- function(linModel, termName) { ??? }
>> removeTerm(fit, "x1")
>>
>> but I can not fill the ???. I already tried
>>
>> removeTerm <- function(linModel, termName) { update(linModel, .~. - termName },
>> removeTerm <- function(linModel, termName) { update(linModel, .~. -
>> as.name(termName) },
>> removeTerm <- function(linModel, termName) { update(linModel, .~. -
>> eval(termName) },
>> removeTerm <- function(linModel, termName) { update(linModel, .~. -
>> eval.parent(termName) },
>> removeTerm <- function(linModel, termName) { update(linModel, .~. -
>> get(termName) },
>>
>> but these attempts produce error messages.
>>
>> Can you advise me here?
>
> There are two problems:
>
> 1. ".~." is different from ". ~ .".
Oops, wrong. Those are the same. Sorry...
Duncan Murdoch
>
> 2. You need to construct the formula ". ~ . - x1", and none of your
> expressions do that. You need to use substitute() or bquote() to edit a
> formula. For example, I think both of these should work:
>
> removeTerm <- function(linModel, termName)
> update(linModel, bquote(. ~ . - .(as.name(termName))))
>
>
> removeTerm <- function(linModel, termName)
> update(linModel, substitute(. ~ . - x, list(x=as.name(termName))))
>
> Duncan Murdoch
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list