[R] adding predictor to linear model without changing existing coefficients
Viechtbauer Wolfgang (SP)
wolfgang.viechtbauer at maastrichtuniversity.nl
Wed May 17 09:36:13 CEST 2017
You could use an offset term. An example:
n <- 100
x1 <- rnorm(n)
x2 <- rnorm(n)
x3 <- rnorm(n)
y <- 0 + .2 * x1 - .5 * x2 + .3 * x3 + rnorm(n)
res1 <- lm(y ~ x1 + x2)
res2 <- lm(y ~ 1 + offset(coef(res1) * x1 + coef(res1) * x2))
summary(res2) ### identical intercept as in res1
res3 <- lm(y ~ offset(coef(res1) * x1 + coef(res1) * x2) + x3)
You may need to consider whether you want to fix up the dfs, since the coefficients in the offset term are obviously not counted. Also, you did not say whether you want to reestimate the intercept; in res3 the intercept is reestimated.
Wolfgang Viechtbauer, Ph.D., Statistician | Department of Psychiatry and
Neuropsychology | Maastricht University | P.O. Box 616 (VIJV1) | 6200 MD
Maastricht, The Netherlands | +31 (43) 388-4170 | http://www.wvbauer.com
>From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Urs
>Sent: Wednesday, May 17, 2017 09:12
>To: r-help at r-project.org
>Subject: [R] adding predictor to linear model without changing existing
>Dear list members,
>I want to add a predictor to a linear model without changing the
>coefficients of the existing model. How is that done with R?
>So if I have a response y and predictors x1, x2, x3 I want to make a model
>lm1 = lm(y~x1+x2)
>After this model is computed I want to add x3 like
>lm2 = lm(y~x1+x2+x3)
>However, unlike it is done by the notation above or by update or add1
>(as far as I understand) I don't want a new model with all predictors
>estimated anew but I want a model lm2 where the coefficients for x1 and
>x2 stay exactly as in lm1 and the coefficent for x3 is estimated
>additionally. The reasons for this are theoretical. I guess what I want
>is similar to calculating a new regression on the residuals of lm1.
>lm2 = lm(residuals(lm1)~x3)
>however, I would prefer to to that in the common framework of the lm
>command in order to calculate statistics, perform anova on the models
>and so on.
>thanks for your help!
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>PLEASE do read the posting guide http://www.R-project.org/posting-
>and provide commented, minimal, self-contained, reproducible code.
More information about the R-help