Often you may want to fit multiple regression models to a dataset in R and compare the resulting coefficients from each model.

One of the best ways to do so is by using the **mtable()** function from the **memisc** package in R, which can be used to perform this exact task.

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 output**summary.stats:**Whether to include summary statistics for each model in the output**coef.style**: A character string that specifies the style to use for coefficient values

Note that you can supply as many model names as you’d like to compare the coefficient values from as many models as you’d like.

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.

The following example shows how to use the **mtable()** function in practice in R.

**Example: How to Use the mtable() Function in R**

For this particular example we will fit several regression models to the built-in mtcars dataset in R.

We can use the **head()** function to view the first few rows from this dataset:

**#view head of mtcars dataset
head(mtcars)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1**

The dataset contains various measurements for different cars.

Suppose that we would like to 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 dataset.

We can use the following syntax to fit the two regression models:

**#fit first regression model
model1 <- lm(mpg ~ disp + carb + hp + cyl, data = mtcars)
#fit second regression model
model2 <- lm(mpg ~ disp + carb, data = mtcars)**

Now suppose that we would like to compare the coefficient values from both of these resulting regression models.

We can use the following syntax with the **mtable()** function do so:

**library(memisc)
#create table to compare coefficient values from both regression models
mtable("Model 1"=model1,"Model 2"=model2)
Calls:
Model 1: lm(formula = mpg ~ disp + carb + hp + cyl, data = mtcars)
Model 2: lm(formula = mpg ~ disp + carb, data = mtcars)
=====================================
Model 1 Model 2
-------------------------------------
(Intercept) 34.022*** 31.153***
(2.523) (1.264)
disp -0.027* -0.036***
(0.011) (0.005)
carb -0.927 -0.956*
(0.579) (0.359)
hp 0.009
(0.021)
cyl -1.049
(0.784)
-------------------------------------
R-squared 0.788 0.774
N 32 32
=====================================
Significance: *** = p < 0.001;
** = p < 0.01;
* = p < 0.05
**

The resulting table displays the coefficient values for each of the models along with some summary statistics of each model.

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 also 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.

Lastly, below these values are the significance level codes that help us see whether each coefficient is statistically significant at various levels.

**Additional Resources**

The following tutorials explain how to perform other common tasks in R:

How to Sort a Table in R

How to Plot a Table in R

How to Create a Three-Way Table in R