4.5 C
London
Thursday, December 19, 2024
HomeStatistics TutorialRHow to Calculate AIC in R (Including Examples)

How to Calculate AIC in R (Including Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

The Akaike information criterion (AIC) is a metric that is used to compare the fit of several regression models.

It is calculated as:

AIC = 2K – 2ln(L)

where:

  • K: The number of model parameters. The default value of K is 2, so a model with just one predictor variable will have a K value of 2+1 = 3.
  • ln(L): The log-likelihood of the model. Most statistical software can automatically calculate this value for you.

The AIC is designed to find the model that explains the most variation in the data, while penalizing for models that use an excessive number of parameters.

Once you’ve fit several regression models, you can compare the AIC value of each model. The lower the AIC, the better the model fit.

To calculate the AIC of several regression models in R, we can use the aictab() function from the AICcmodavg package.

The following example shows how to use this function to calculate and interpret the AIC for various regression models in R.

Example: Calculate & Interpret AIC in R

Suppose we would like to fit three different multiple linear regression models using variables from the mtcars dataset.

Here are the predictor variables we’ll use in each model:

  • Predictor variables in Model 1: disp, hp, wt, qsec
  • Predictor variables in Model 2: disp, qsec
  • Predictor variables in Model 3: disp, wt

The following code shows how to fit each of these regression models:

#fit three models
model1 

Next, we’ll put the models into a list and use the aictab() function to calculate the AIC of each model:

library(AICcmodavg)

#define list of models
models #specify model names
mod.names #calculate AIC of each model
aictab(cand.set = models, modnames = mod.names)

Model selection based on AICc:

                K   AICc Delta_AICc AICcWt Cum.Wt     LL
disp.hp.wt.qsec 6 162.43       0.00   0.83   0.83 -73.53
disp.wt         4 165.65       3.22   0.17   1.00 -78.08
disp.qsec       4 173.32      10.89   0.00   1.00 -81.92

Here’s how to interpret the output:

  • K: The number of parameters in the model.
  • AICc: The AIC value of the model. The lowercase ‘c’ indicates that the AIC has been calculated from the AIC corrected for small sample sizes.
  • Delta_AICc: The difference between the AIC of the best model compared to the current model being compared.
  • AICcWt: The proportion of the total predictive power that can be found in the model.
  • Cum.Wt: The cumulative sum of the AIC weights.
  • LL: The log-likelihood of the model. This tells us how likely the model is, given the data we used.

The model with the lowest AIC value is always listed first. From the output we can see that the following model has the lowest AIC value and is thus the best fitting model:

mpg = β0 + β1(disp) + β2(hp) + β3(wt) + β4(qsec)

Once we’ve identified this model as the best, we can proceed to fit the model and analyze the results including the R-squared value and the beta coefficients to determine the exact relationship between the set of predictor variables and the response variable.

Additional Resources

How to Perform Simple Linear Regression in R
How to Perform Multiple Linear Regression in R
How to Calculate Adjusted R-Squared in R
How to Calculate Mallows’ Cp in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories