[R] Forcing coefficients in lm object

J.J.P.Lebrec at lumc.nl J.J.P.Lebrec at lumc.nl
Tue Aug 28 15:33:11 CEST 2007


Dear all,

I would like to use predict.lm() with an existing lm object but with new arbitrary coefficients. I modify 'fit$coef' (see example below) "by hand" but the actual model in 'fit' used for prediction does not seem to be altered (although fit$coef is!).

Can anyone please help me do this properly?

Thanks in advance,

Jérémie



> dat <- data.frame(y=c(0,25,32,15), x=as.factor(c(1,1,2,2)))
> fit <- lm(y ~ x, data=dat)
> fit

Call:
lm(formula = y ~ x, data = dat)

Coefficients:
(Intercept)           x2  
       12.5         11.0  

> fit$coef[[2]] <- 100
> dat.new <- data.frame(x=as.factor(c(1,2,1,2)))
> predict.lm(fit, dat.new)
   1    2    3    4 
12.5 23.5 12.5 23.5 
> fit

Call:
lm(formula = y ~ x, data = dat)

Coefficients:
(Intercept)           x2  
       12.5         11.0  

> fit$coef
(Intercept)          x2 
       12.5       100.0 
>



Jérémie Lebrec
Dept. of Medical Statistics and Bioinformatics
Leiden University Medical Center
Postzone S-05-P
P.O. Box 9600
2300 RC Leiden
The Netherlands
j.j.p.lebrec at lumc.nl



More information about the R-help mailing list