Table of Contents
The lm() function in R is used for linear regression analysis and provides various statistical outputs, including the standard errors of the regression coefficients. Standard errors are a measure of the variability or uncertainty in the estimated coefficients. In order to extract these standard errors from the lm() function, the “summary()” command can be used, which will generate a summary of the regression results, including the standard errors. The standard errors can also be accessed directly by using the “coef()” function and specifying the “se” option. This will return a vector of the standard errors for each coefficient. Overall, the lm() function in R provides multiple ways to extract the standard errors, allowing for further analysis and interpretation of the linear regression results.
Extract Standard Errors from lm() Function in R
You can use the following methods to extract the residual standard error along with the standard error of the individual regression coefficients from the function in R:
Method 1: Extract Residual Standard Error
#extract residual standard error of regression model
summary(model)$sigmaMethod 2: Extract Standard Error of Individual Regression Coefficients
#extract standard error of individual regression coefficients
sqrt(diag(vcov(model)))The following example shows how to use each method in practice.
Example: Extract Standard Errors from lm() in R
Suppose we fit the following multiple linear regression model in R:
#create data frame df <- data.frame(rating=c(67, 75, 79, 85, 90, 96, 97), points=c(8, 12, 16, 15, 22, 28, 24), assists=c(4, 6, 6, 5, 3, 8, 7), rebounds=c(1, 4, 3, 3, 2, 6, 7)) #fit multiple linear regression model model <- lm(rating ~ points + assists + rebounds, data=df)
We can use the summary() function to view the entire summary of the regression model:
#view model summary
summary(model)
Call:
lm(formula = rating ~ points + assists + rebounds, data = df)
Residuals:
1 2 3 4 5 6 7
-1.5902 -1.7181 0.2413 4.8597 -1.0201 -0.6082 -0.1644
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 66.4355 6.6932 9.926 0.00218 **
points 1.2152 0.2788 4.359 0.02232 *
assists -2.5968 1.6263 -1.597 0.20860
rebounds 2.8202 1.6118 1.750 0.17847
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 3.193 on 3 degrees of freedom
Multiple R-squared: 0.9589, Adjusted R-squared: 0.9179
F-statistic: 23.35 on 3 and 3 DF, p-value: 0.01396
The residual standard error of the model is 3.193 and each of the standard errors for the individual regression coefficients can be seen in the Std. Error column of the output.
To only extract the residual standard error for the model, we can use the following syntax:
#extract residual standard error of regression model
summary(model)$sigma
[1] 3.19339And to only extract the standard errors for each of the individual regression coefficients, we can use the following syntax:
#extract standard error of individual regression coefficients
sqrt(diag(vcov(model)))
(Intercept) points assists rebounds
6.6931808 0.2787838 1.6262899 1.6117911
Notice that these values match the values that we saw earlier in the entire regression output summary.
Cite this article
stats writer (2024). How can I extract standard errors from the lm() function in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-extract-standard-errors-from-the-lm-function-in-r/
stats writer. "How can I extract standard errors from the lm() function in R?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-extract-standard-errors-from-the-lm-function-in-r/.
stats writer. "How can I extract standard errors from the lm() function in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-extract-standard-errors-from-the-lm-function-in-r/.
stats writer (2024) 'How can I extract standard errors from the lm() function in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-extract-standard-errors-from-the-lm-function-in-r/.
[1] stats writer, "How can I extract standard errors from the lm() function in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I extract standard errors from the lm() function in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
