[R] How to get the intercept from lm?

Bernd Weiss bernd.weiss at uni-koeln.de
Fri Mar 10 11:29:25 CET 2006


Am 10 Mar 2006 um 12:17 hat Rainer M Krug geschrieben:

> Hi
> 
> I am using R 2.2.0 under SuSE 10
> 
> I want to use lm() to get the slope and intercept for several
> daatasets and store them in a database. So far so good - but how do I
> extract the slope and the intercept from the result from lm()?
> 
> my code looks like this:
> 
> lmNNDens <- lm(log(DensNN$MeanNN) ~ log(DensNN$MeanDensity))
> anovaLM <- anova(lmNNDens)
> 
> Results$slope[No] <- ???lmNNDens???
> Results$intercept[No] <- ???lmNNDens???
> 
> any help welcome,
> 



> x<-rnorm(100)
> y<-rnorm(100)
> summary(lmResult <- lm(y~x))

Call:
lm(formula = y ~ x)

Residuals:
     Min       1Q   Median       3Q      Max 
-2.53308 -0.66265  0.02258  0.64282  3.38839 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)
(Intercept)  0.13784    0.11211   1.230    0.222
x           -0.02748    0.12184  -0.226    0.822

Residual standard error: 1.114 on 98 degrees of freedom
Multiple R-Squared: 0.0005188,  Adjusted R-squared: -0.00968 
F-statistic: 0.05087 on 1 and 98 DF,  p-value: 0.822 

> coef(lmResult)
(Intercept)           x 
 0.13783934 -0.02748145 
> coef(lmResult)["(Intercept)"]
(Intercept) 
  0.1378393 
> coef(lmResult)["x"]
          x 
-0.02748145 



That's the way I would do it.

HTH,

Bernd




More information about the R-help mailing list