[R] update.lm question
David Winsemius
dwinsemius at comcast.net
Sun Nov 15 17:57:26 CET 2009
On Nov 15, 2009, at 11:15 AM, Karsten Weinert wrote:
> Hello David,
>
> of course I read those help pages and searched the r-help archive.
But did you look at the as.formula page?
>
> I agree that the way should be clear, but I am stuck and looking for
> help. Here is reproducible code
And here is what seemed like the obvious extension of your example but
using as.formula:
myData <- data.frame(x1=rnorm(10), x2=rnorm(10), x3=rnorm(10),
y=rnorm(10))
fit <- lm(y~x1+x2+x3, data=myData)
remvterm <- function(ft, termname) update(ft, as.formula(paste(".~.-",
termname, sep="")))
remvterm(fit, "x3")
Call:
lm(formula = y ~ x1 + x2, data = myData)
Coefficients:
(Intercept) x1 x2
-0.2598 -0.0290 -0.2645
--
David
> removeTerm1 <- function(linModel, termName) { update(linModel, .~. -
> termName) }
> removeTerm2 <- function(linModel, termName) { update(linModel, .~. -
> as.name(termName)) }
> removeTerm3 <- function(linModel, termName) { update(linModel, .~. -
> eval(termName)) }
> removeTerm4 <- function(linModel, termName) { update(linModel, .~. -
> eval.parent(termName)) }
> removeTerm5 <- function(linModel, termName) { update(linModel, .~. -
> get(termName)) }
>
> myData <- data.frame(x1=rnorm(10), x2=rnorm(10), x3=rnorm(10),
> y=rnorm(10))
> fit <- lm(y~x1+x2+x3, data=myData)
>
> # all this does not work, as I am expecting the function to return a
> lm object with formula y~x2+x3
> removeTerm1(fit, "x1")
> removeTerm2(fit, "x1")
> removeTerm3(fit, "x1")
> removeTerm4(fit, "x1")
> removeTerm5(fit, "x1")
>
>
> Any help appreciated,
> kind regards,
> Karsten Weinert
>
>
>
> 2009/11/15 David Winsemius <dwinsemius at comcast.net>:
>>
>> You need to review:
>>
>> ?update
>> ?update.formula
>> ?as.formula
>>
>> The way should be clear at that point. If not, then include a
>> reproducible
>> data example to work with.
>>
>> --
>> David
>
> ______________________________________________
> 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.
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
More information about the R-help
mailing list