33

We got a lm object from and want to extract the standard error

lm_aaa <- lm(aaa ~ x + y + z)

I know the function summary, names and coefficients.

However, summary seems to be the only way to manually access the standard error.

Have you any idea how I can just output se?

zx8754
  • 52,746
  • 12
  • 114
  • 209
Fabian Stolz
  • 1,935
  • 7
  • 27
  • 30

5 Answers5

27

The output of from the summary function is just an R list. So you can use all the standard list operations. For example:

#some data (taken from Roland's example)
x = c(1,2,3,4)
y = c(2.1,3.9,6.3,7.8)

#fitting a linear model
fit = lm(y~x)
m = summary(fit)

The m object or list has a number of attributes. You can access them using the bracket or named approach:

m$sigma
m[[6]]

A handy function to know about is, str. This function provides a summary of the objects attributes, i.e.

str(m)
csgillespie
  • 59,189
  • 14
  • 150
  • 185
  • 4
    However, what @csgillespie refers to is the **residual** standard deviation of the model, not the standard deviation of the individual coefficients. The function `m$sigma` corresponds to `sigma(fit)`, see [here](https://stat.ethz.ch/R-manual/R-devel/library/stats/html/sigma.html). I believe the question was really about the standard deviation of the **individual** coefficients. – Annerose N Dec 21 '16 at 09:33
12

To get a list of the standard errors for all the parameters, you can use

summary(lm_aaa)$coefficients[, 2]

As others have pointed out, str(lm_aaa) will tell you pretty much all the information that can be extracted from your model.

smillig
  • 5,073
  • 6
  • 36
  • 46
10
#some data
x<-c(1,2,3,4)
y<-c(2.1,3.9,6.3,7.8)

#fitting a linear model
fit<-lm(y~x)

#look at the statistics summary
summary(fit)

#get the standard error of the slope
se_slope<-summary(fit)$coef[[4]] 
#the index depends on the model and which se you want to extract

#get the residual standard error
rse<-summary(fit)$sigma
Roland
  • 127,288
  • 10
  • 191
  • 288
7

If you don't want to get the standard error/deviation of the model, but instead the standard error/deviation of the individual coefficients, use

# some data (taken from Roland's example)
x = c(1, 2, 3, 4)
y = c(2.1, 3.9, 6.3, 7.8)

# fitting a linear model
fit = lm(y ~ x)

# get vector of all standard errors of the coefficients
coef(summary(fit))[, "Std. Error"] 

For more information on the standard error/deviation of the model, see here. For more information on the standard error/deviation of the coefficients, see here.

Annerose N
  • 477
  • 6
  • 14
5

I think that the following lines can also provide you with a quick answer:

lm_aaa<- lm(aaa~x+y+z)
se <- sqrt(diag(vcov(lm_aaa)))
anothernode
  • 5,100
  • 13
  • 43
  • 62
Whitebeard13
  • 411
  • 1
  • 7
  • 17