Comparing Two Linear Models with anova() in R
Last Updated :
13 Sep, 2024
Comparing two linear models is a fundamental task in statistical analysis, especially when determining if a more complex model provides a significantly better fit to the data than a simpler one. In R, the anova() the function allows you to perform an Analysis of Variance (ANOVA) to compare nested models.
What is a Linear Model?
A linear model describes the relationship between a response variable (dependent variable) and one or more explanatory variables (independent variables) using a linear equation.
ANOVA for Comparing Models
The Analysis of Variance (ANOVA) technique compares two nested models to determine if the more complex model provides a significantly better fit to the data. The anova() function in R performs this comparison by calculating an F-statistic and a p-value. The null hypothesis is that the simpler model is adequate, and the alternative hypothesis is that the more complex model is better. If the p-value is small (typically less than 0.05), we reject the null hypothesis and conclude that the complex model provides a significantly better fit.
anova(model1, model2)
- model1: The simpler model.
- model2: The more complex model.
Letâs explain how to compare two linear models using the mtcars dataset in R Programming Language. The mtcars dataset contains data about fuel consumption and other aspects of automobile design and performance for 32 cars.
Step 1: Loading the Data
We will use the mtcars dataset, which is preloaded in R. First, inspect the data:
R
# Load the dataset
data(mtcars)
# Display the first few rows of the dataset
head(mtcars)
Output:
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
Step 2: Building the Models
We will build two models:
- Model 1 (Simpler): This model predicts the miles per gallon (
mpg) using only the weight (wt) of the car. - Model 2 (Complex): This model predicts
mpg using both weight (wt) and horsepower (hp).
R
# Build Model 1: mpg as a function of weight (wt)
model1 <- lm(mpg ~ wt, data = mtcars)
# Build Model 2: mpg as a function of weight (wt) and horsepower (hp)
model2 <- lm(mpg ~ wt + hp, data = mtcars)
Step 3: Comparing the Models with anova()
Now, we use the anova() function to compare the two models.
R
# Compare the two models using ANOVA
anova_result <- anova(model1, model2)
print(anova_result)
Output:
Analysis of Variance Table
Model 1: mpg ~ wt
Model 2: mpg ~ wt + hp
Res.Df RSS Df Sum of Sq F Pr(>F)
1 30 278.32
2 29 195.05 1 83.274 12.381 0.001451 **
---
Signif. codes: 0 â***â 0.001 â**â 0.01 â*â 0.05 â.â 0.1 â â 1
- Model 1 (Simpler) has 30 residual degrees of freedom (Res.Df) and a residual sum of squares (RSS) of 278.32.
- Model 2 (Complex) has 29 residual degrees of freedom and an RSS of 180.29.
- The difference in degrees of freedom (Df) between the two models is 1, and the sum of squares of the difference is 98.025.
- The F-statistic is 15.775, and the p-value is 0.0004344.
Since the p-value is much less than 0.05, we reject the null hypothesis and conclude that Model 2 (which includes both wt and hp) provides a significantly better fit than Model 1 (which only includes wt).
Conclusion
In this article, we have explored how to compare two linear models using the anova() function in R. The ANOVA test provides a formal way to determine whether a more complex model provides a significantly better fit than a simpler model. This technique is particularly useful in model selection, stepwise regression, and hypothesis testing.
Similar Reads
Comparing Two Linear Models with anova() in R
Comparing two linear models is a fundamental task in statistical analysis, especially when determining if a more complex model provides a significantly better fit to the data than a simpler one. In R, the anova() the function allows you to perform an Analysis of Variance (ANOVA) to compare nested mo
4 min read
How to Obtain ANOVA Table with Statsmodels
Analysis of Variance (ANOVA) is a statistical method used to analyze the differences among group means in a sample. It is particularly useful for comparing three or more groups for statistical significance. In Python, the statsmodels library provides robust tools for performing ANOVA. This article w
4 min read
How to Use lm() Function in R to Fit Linear Models?
In this article, we will learn how to use the lm() function to fit linear models in the R Programming Language. A linear model is used to predict the value of an unknown variable based on independent variables. It is mostly used for finding out the relationship between variables and forecasting. The
4 min read
What are Comparison Operators in R?
Comparison operators are fundamental tools in any programming language, allowing you to compare values and make decisions based on the results. In R Programming Language comparison operators are used to compare numeric, string, and other types of data. They return a logical value (TRUE or FALSE) bas
3 min read
Comparing two data sets in R
There may be a situation where we have to compare datasets to do data analysis, with having the same structure but differences in the data. So to identify what is changed in the dataset and to get a summary to which extent it is changed. We can use the compare package in R. We can easily use this pa
2 min read
How Linear Mixed Model Works in R
Linear mixed models (LMMs) are statistical models that are used to analyze data with both fixed and random effects. They are particularly useful when analyzing data with hierarchical or nested structures, such as longitudinal or clustered data. In R Programming Language, the lme4 package provides a
6 min read
Adding Legend to Multiple Line Plots with ggplot in R
In this article, we are going to see how can we add a legend to multiple line plots with ggplot in the R programming language. For a plot that contains more than one line plot, a legend is created by default if the col attribute is used. All the changes made in the appearance of the line plots will
2 min read
Visualization a Linear Model on a Scatterplot with ggvis
It is a statistical model used to describe the relationship between a dependent variable and one or more independent variables. The linear model is used in data analysis. We can say that a linear model assumes a linear relationship between the dependent variable and each independent variable. Linear
3 min read
How to Fix in R: invalid model formula in ExtractVars
In this article, we will discuss how we can fix the "invalid model formula in ExtractVars" error in the R programming language. The error that one may face in R is:Error in terms.formula(formula, data = data) : invalid model formula in ExtractVars The R compiler produces such an error we try to fit
2 min read
How to Get Residuals from Repeated Measures ANOVA Model in R
In R Language when performing a Repeated Measures ANOVA, residuals can be an important diagnostic tool to assess the model's goodness-of-fit. Residuals represent the difference between observed and predicted values in the model, and their analysis can help in detecting violations of model assumption
4 min read