20

Simple question really! I am running lots of linear regressions of y~x and want to obtain the variance for each regression without computing it from hand from the Standard Error output given in the summary.lm command. Just to save a bit of time :-). Any ideas of the command to do this? Or will I have to write a function to do it myself?

m<-lm(Alopecurus.geniculatus~Year)
> summary(m)

Call:
lm(formula = Alopecurus.geniculatus ~ Year)

Residuals:
    Min      1Q  Median      3Q     Max 
-19.374  -8.667  -2.094   9.601  21.832 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)  
(Intercept) 700.3921   302.2936   2.317   0.0275 *
Year         -0.2757     0.1530  -1.802   0.0817 .
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 11.45 on 30 degrees of freedom
  (15 observations deleted due to missingness)
Multiple R-squared: 0.09762,    Adjusted R-squared: 0.06754 
F-statistic: 3.246 on 1 and 30 DF,  p-value: 0.08168 

So I get a Standard Error output and I was hoping to get a Variance output without calculating it by hand...

Sarah
  • 789
  • 3
  • 12
  • 29
  • Could you please provide an example? – Sven Hohenstein Feb 19 '13 at 14:09
  • Just added in the example - hope it helps... :) – Sarah Feb 19 '13 at 14:49
  • Sorry it was off topic - haven't used this site before :). I mean the actual variance statistic that is in turn used to calculate the SE and so on. It's easy to calculate, I just wondered if there was a simple call for it. I'll do it by hand though, no matter. Cheers :) – Sarah Feb 19 '13 at 14:54
  • 2
    @Sarah welcome to CV - off topic doesn't mean you won't get an answer! Far from it - watch this space and this well get migrated to SO once enough mods have voted on it. There are an enormous number of R experts over there that will probably know exactly what call to make. CV would have been the place to ask the opposite question: "How do I calculate this by hand". – Corvus Feb 19 '13 at 15:01

3 Answers3

28

I'm not sure what you want the variance of.

If you want the residual variance, it's: (summary(m)$sigma)**2.

If you want the variance of your slope, it's: (summary(m)$coefficients[2,2])**2, or vcov(m)[2,2].

gung - Reinstate Monica
  • 11,583
  • 7
  • 60
  • 79
  • Thanks everyone. It was the variance of the slope I wanted, so that's really helpful gung, thank you. – Sarah Feb 20 '13 at 13:03
13

vcov(m)

gives the covariance matrix of the coefficients – variances on the diagonal.

Qaswed
  • 3,649
  • 7
  • 27
  • 47
user20650
  • 131
  • 2
11

if you're referring to the standard errors for the coefficient estimates, the answer is

summary(m)$coef[,2] 

and if you're referring to the estimated residual variance, it's

summary(m)$sigma

Type names( summary(m) ) and names(m) for other information you can access.

Macro
  • 1,450
  • 1
  • 10
  • 18