R Get Standard Error, t- & p-Value from Regression (4 Examples)

In this article you’ll learn how to return linear regression stats such as standard errors or p-values in the R programming language.

Example Data & Fitting Linear Model

data(iris)                                        # Loading iris flower data set
head(iris)                                        # Head of iris flower data set
#   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1          5.1         3.5          1.4         0.2  setosa
# 2          4.9         3.0          1.4         0.2  setosa
# 3          4.7         3.2          1.3         0.2  setosa
# 4          4.6         3.1          1.5         0.2  setosa
# 5          5.0         3.6          1.4         0.2  setosa
# 6          5.4         3.9          1.7         0.4  setosa
iris_smry <- summary(lm(Sepal.Length ~ ., iris))  # Summary of linear model
iris_smry                                         # Print summary to RStudio console
# Call:
# lm(formula = Sepal.Length ~ ., data = iris)
# 
# Residuals:
#      Min       1Q   Median       3Q      Max 
# -0.79424 -0.21874  0.00899  0.20255  0.73103 
# 
# Coefficients:
# Estimate Std. Error t value Pr(>|t|)    
# (Intercept)        2.17127    0.27979   7.760 1.43e-12 ***
# Sepal.Width        0.49589    0.08607   5.761 4.87e-08 ***
# Petal.Length       0.82924    0.06853  12.101  < 2e-16 ***
# Petal.Width       -0.31516    0.15120  -2.084  0.03889 *  
# Speciesversicolor -0.72356    0.24017  -3.013  0.00306 ** 
# Speciesvirginica  -1.02350    0.33373  -3.067  0.00258 ** 
#   ---
#   Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# 
# Residual standard error: 0.3068 on 144 degrees of freedom
# Multiple R-squared:  0.8673,	Adjusted R-squared:  0.8627 
# F-statistic: 188.3 on 5 and 144 DF,  p-value: < 2.2e-16

Example 1: Get Standard Errors of Linear Model

iris_smry$coefficients[ , 2]                      # Extract standard error in R
# (Intercept)       Sepal.Width      Petal.Length       Petal.Width Speciesversicolor  Speciesvirginica 
#  0.27979415        0.08606992        0.06852765        0.15119575        0.24016894        0.33372630

Example 2: Get t-Values of Linear Model

iris_smry$coefficients[ , 3]                      # Extract t-value in R
# (Intercept)       Sepal.Width      Petal.Length       Petal.Width Speciesversicolor  Speciesvirginica 
#    7.760227          5.761466         12.100867         -2.084418         -3.012721         -3.066878

Example 3: Get p-Values of Predictors of Linear Model

iris_smry$coefficients[ , 4]                      # Extract p-value in R
#  (Intercept)       Sepal.Width      Petal.Length       Petal.Width Speciesversicolor  Speciesvirginica 
# 1.429502e-12      4.867516e-08      1.073592e-23      3.888826e-02      3.059634e-03      2.584344e-03

Example 4: Get F-statistic p-Value of Linear Model

pf(iris_smry$fstatistic[1],                       # Extract F-statistic p-value in R
   iris_smry$fstatistic[2],
   iris_smry$fstatistic[3],
   lower.tail = FALSE)
# 2.666942e-61

Further Resources & Related Articles

Here, you may find some further resources on topics such as extracting data, coding errors, matrices, and regression models:

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top