Extract F-Statistic, Number of Predictor Variables/Categories & Degrees of Freedom from Linear Regression Model in R
In this article you’ll learn how to pull out the F-Statistic, the number of predictor variables and categories, as well as the degrees of freedom from a linear regression model in R.
The post will contain the following content blocks:
Let’s dive into it…
Introduction of Example Data
The following data is used as basement for this R tutorial:
set.seed(439846) # Creating random example data x1 <- rnorm(500) x2 <- rnorm(500) + 0.3 * x1 x3 <- rnorm(500) - 0.2 * x1 + 0.4 * x2 x4 <- rnorm(500) + 0.3 * x1 - 0.1 * x3 x5 <- rnorm(500) - 0.01 * x2 - 0.3 * x4 y <- rnorm(500) + 0.1 * x1 - 0.3 * x2 + 0.5 * x3 - 0.1 * x4 - 0.25 * x5 data <- data.frame(y, x1, x2, x3, x4, x5) head(data) # Returning head of data # y x1 x2 x3 x4 x5 # 1 1.1358024 0.2307740 1.3217573 0.6355502 2.11914482 -0.5558387 # 2 -0.1978492 1.5587003 -0.2060491 -0.4888357 0.93288548 2.2203048 # 3 0.2915948 0.0421542 -1.5101331 -0.2775710 -1.56980914 -0.8382218 # 4 0.8573821 0.6745012 -1.0606676 0.5397349 -0.34250782 -0.4814685 # 5 -0.5523900 2.0011617 1.0309716 -0.6566911 -0.89026835 0.4691092 # 6 1.1484029 -0.6232096 -0.3605382 -0.3616607 0.05405318 -0.6393136
The previous output of the RStudio console shows the structure of our example data: It consists of one outcome variable (i.e. y) and five predictor variables (i.e. x1-x5). All variables are numeric.
Now, we can estimate a linear regression model using the summary and lm functions in R:
mod_summary <- summary(lm(y ~ ., data)) # Creating linear regression output mod_summary # Showing linear regression output # Call: # lm(formula = y ~ ., data = data) # # Residuals: # Min 1Q Median 3Q Max # -2.8920 -0.6481 -0.0029 0.6649 3.2213 # # Coefficients: # Estimate Std. Error t value Pr(>|t|) # (Intercept) 0.08433 0.04361 1.934 0.0537 . # x1 0.06954 0.04606 1.510 0.1318 # x2 -0.31924 0.04794 -6.660 7.33e-11 *** # x3 0.51738 0.04409 11.734 < 2e-16 *** # x4 -0.07131 0.04464 -1.597 0.1108 # x5 -0.27661 0.04478 -6.178 1.36e-09 *** # --- # Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 # # Residual standard error: 0.9715 on 494 degrees of freedom # Multiple R-squared: 0.2721, Adjusted R-squared: 0.2647 # F-statistic: 36.93 on 5 and 494 DF, p-value: < 2.2e-16
The output is relatively complex. For that reason, it might be useful to pull out certain values of the output. The following examples show how to extract F-statistic, number of predictors, and degrees of freedom from our regression summary.
Example 1: Extracting F-statistic from Linear Regression Model
The following R code shows how to extract the F-statistic of our linear regression analysis.
mod_summary$fstatistic[1] # Return F-statistic # value # 36.92899
The F-statistic is 36.92899.
Example 2: Extracting Number of Predictor Variables from Linear Regression Model
The following syntax explains how to pull out the number of independent variables and categories (i.e. numdf) from our lm() output.
mod_summary$fstatistic[2] # Return number of variables # numdf # 5
We have used five predictor columns in our analysis.
Example 3: Extracting Degrees of Freedom from Linear Regression Model
This Example shows how to identify the degrees of freedom (DF or dendf) of our linear regression model.
mod_summary$fstatistic[3] # Return degrees of freedom # dendf # 494
Our linear regression model has 494 degrees of freedom.
Video, Further Resources & Summary
In case you need further info on the R programming syntax of this article, you might want to have a look at the following video of my YouTube channel. In the video, I’m explaining the R programming codes of this article.
The YouTube video will be added soon.
In addition, I can recommend to read the related articles of https://statisticsglobe.com/:
In summary: In this tutorial, I illustrated how to extract F-statistic and degrees of freedom from a model in the R programming language. If you have any additional questions and/or comments, don’t hesitate to let me know in the comments.
Statistics Globe Newsletter