Multiple regression model in R
Multiple regression model in R, it is often necessary to fit multiple regression models to a dataset and compare the resulting coefficients from each model.
One of the best ways to do this is by using the mtable()
function from the memisc
package in R.
Multiple regression model in R
The mtable()
function uses the following syntax:
mtable(…, summary.stats=TRUE, coef.style, … )
Where:
…
: A named list of models whose coefficients should be shown in the outputsummary.stats
: Whether to include summary statistics for each model in the outputcoef.style
: A character string that specifies the style to use for coefficient values
This function will produce a neatly formatted table that allows you to compare the coefficient values from different models along with other summary statistics such as overall R-squared, etc.
Free Data Science Books » EBooks » finnstats
Example: How to Use the mtable() Function in R
In this example, we will fit two different regression models using two different sets of predictor variables to predict the value of mpg (miles per gallon) of each car in the built-in mtcars dataset in R.
First, we will view the first few rows from the dataset using the head()
function:
head(mtcars)
Then, we will fit the two regression models using the lm()
function:
#fit first regression model
model1 <- lm(mpg ~ disp + carb + hp + cyl, data = mtcars)
#fit second regression model
model2 <- lm(mpg ~ disp + carb, data = mtcars)
Next, we will use the mtable()
function to compare the coefficient values from both of these resulting regression models:
library(memisc)
#create table to compare coefficient values from both regression models
mtable("Model 1"=model1,"Model 2"=model2)
This will produce a table that displays the coefficient values for each of the models along with some summary statistics of each model. The table also includes information on whether each coefficient is statistically significant at various levels.
Interpreting the Output
Here is how to interpret the output:
- The intercept value of the first model is 34.022 with a standard error of 2.523.
- The intercept value of the second model is 31.153 with a standard error of 1.264.
- We can similarly compare the other coefficient values between each of the two models.
- At the bottom of the output, we can see the following values for each model:
- R-Squared: The percentage of variation in the response variable that can be explained by the predictor variables in the model.
- N: The total number of observations used to fit each model.
- Significance: The significance level codes that help us see whether each coefficient is statistically significant at various levels.
Overall, the mtable()
function is a powerful tool that allows you to easily compare and analyze multiple regression models in R.