Extract Beta Coefficients from Linear Regression Model in R (Example)
In this tutorial, I’ll illustrate how to get standardized regression coefficients (also called beta coefficients or beta weights) from a linear model in R.
The article contains this:
Let’s get started…
Introduction of Example Data
Consider the following example data:
set.seed(2344637) # Create example data x1 <- rnorm(100) x2 <- rnorm(100) + 0.25 * x1 x3 <- rnorm(100) + 0.5 * x1 - 0.3 * x2 y <- rnorm(100) + 0.15 * x1 + 0.4 * x2 - 0.1 * x3 data <- data.frame(x1, x2, x3, y) head(data) # Print head of example data |
set.seed(2344637) # Create example data x1 <- rnorm(100) x2 <- rnorm(100) + 0.25 * x1 x3 <- rnorm(100) + 0.5 * x1 - 0.3 * x2 y <- rnorm(100) + 0.15 * x1 + 0.4 * x2 - 0.1 * x3 data <- data.frame(x1, x2, x3, y) head(data) # Print head of example data
Table 1 shows that our example data contains four columns. The variables x1-x3 will be used as predictors and the variable y as target variable.
Next, we can estimate a linear regression model based on our data using the lm function:
my_mod <- lm(y ~ ., data) # Estimate linear regression model summary(my_mod) # Print summary statistics # Call: # lm(formula = y ~ ., data = data) # # Residuals: # Min 1Q Median 3Q Max # -2.52046 -0.72756 0.04412 0.73519 2.82633 # # Coefficients: # Estimate Std. Error t value Pr(>|t|) # (Intercept) 0.27485 0.10599 2.593 0.0110 * # x1 0.13312 0.12173 1.094 0.2769 # x2 0.22596 0.10180 2.220 0.0288 * # x3 -0.08231 0.11531 -0.714 0.4771 # --- # Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 # # Residual standard error: 1.049 on 96 degrees of freedom # Multiple R-squared: 0.06315, Adjusted R-squared: 0.03388 # F-statistic: 2.157 on 3 and 96 DF, p-value: 0.09811 |
my_mod <- lm(y ~ ., data) # Estimate linear regression model summary(my_mod) # Print summary statistics # Call: # lm(formula = y ~ ., data = data) # # Residuals: # Min 1Q Median 3Q Max # -2.52046 -0.72756 0.04412 0.73519 2.82633 # # Coefficients: # Estimate Std. Error t value Pr(>|t|) # (Intercept) 0.27485 0.10599 2.593 0.0110 * # x1 0.13312 0.12173 1.094 0.2769 # x2 0.22596 0.10180 2.220 0.0288 * # x3 -0.08231 0.11531 -0.714 0.4771 # --- # Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 # # Residual standard error: 1.049 on 96 degrees of freedom # Multiple R-squared: 0.06315, Adjusted R-squared: 0.03388 # F-statistic: 2.157 on 3 and 96 DF, p-value: 0.09811
The previous output shows the summary statistics for our regression model.
However, this output does not show the beta coefficients. This is what we are going to compute next!
Example 1: Extract Standardized Coefficients from Linear Regression Model Using Base R
In this example, I’ll explain how to calculate beta weights based on a linear regression model using the basic installation of the R programming language.
More precisely, we are using the lm, data.frame, and scale functions.
Consider the following R code and its output below:
lm(data.frame(scale(my_mod$model))) # Get standardized regression coefficients # Call: # lm(formula = data.frame(scale(my_mod$model))) # # Coefficients: # (Intercept) x1 x2 x3 # 5.070e-17 1.441e-01 2.200e-01 -9.387e-02 |
lm(data.frame(scale(my_mod$model))) # Get standardized regression coefficients # Call: # lm(formula = data.frame(scale(my_mod$model))) # # Coefficients: # (Intercept) x1 x2 x3 # 5.070e-17 1.441e-01 2.200e-01 -9.387e-02
As you can see, we have returned the beta coefficients corresponding to our linear regression model.
Note that the code of this example was provided by Dr. R. H. Red Owl – thanks a lot to him! Please have a look at his comments below this tutorial to get more info on this code.
Example 2: Extract Standardized Coefficients from Linear Regression Model Using lm.beta Package
Alternatively to the functions of Base R (as explained in Example 1), we can also use the lm.beta package to get the beta coefficients.
In order to use the functions of the lm.beta package, we first have to install and load lm.beta to R:
install.packages("lm.beta") # Install lm.beta package library("lm.beta") # Load lm.beta package |
install.packages("lm.beta") # Install lm.beta package library("lm.beta") # Load lm.beta package
In the next step, we can apply the lm.beta function to the lm model object that we have created before:
lm.beta(my_mod) # Get standardized regression coefficients # Call: # lm(formula = y ~ ., data = data) # # Standardized Coefficients:: # (Intercept) x1 x2 x3 # 0.0000000 0.1441121 0.2199585 -0.0938736 |
lm.beta(my_mod) # Get standardized regression coefficients # Call: # lm(formula = y ~ ., data = data) # # Standardized Coefficients:: # (Intercept) x1 x2 x3 # 0.0000000 0.1441121 0.2199585 -0.0938736
The previous output shows our standardized regression coefficients.
Video, Further Resources & Summary
Do you need further info on the R syntax of this article? Then I recommend watching the following video instruction on my YouTube channel. In the video, I’m explaining the R syntax of this article in R:
The YouTube video will be added soon.
Additionally, you could have a look at some of the other articles on my website:
- Extract Fitted Values from Regression Model in R
- Exclude Specific Predictors from Linear Regression Model
- Extract Standard Error, t-Value & p-Value from Linear Regression Model
- Extract Residuals & Sigma from Linear Regression Model
- How to Extract the Intercept from a Linear Regression Model
- R Programming Overview
Summary: You have learned in this article how to calculate beta coefficients from a linear model in the R programming language. In case you have additional questions, please let me know in the comments section.
Statistics Globe Newsletter
4 Comments. Leave new
Thanks for all your very helpful posts and Youtubes.
As you probably already know, R stores a pointer to the raw data in the regression model. That allows us to get the beta coefficients with a single line of Base R code after running a linear model.
Hey,
Thanks a lot for this very nice alternative code! I have added another example (see Example 1), which uses your code to produce the beta coefficients.
Regards,
Joachim
Thanks again for the additional syntax, this is great! 🙂
Regards,
Joachim