Written and maintained by CASRAI Editorial Board
Published · Last updated
On this page
The Akaike Information Criterion (AIC) in R is calculated using the built-in AIC() function from the standard stats package, which extracts the log-likelihood of a fitted parametric model and applies a penalty for the number of estimated parameters according to the formula -2log(L) + 2k. Researchers use AIC in R to rank and select among competing statistical models fitted to the same dataset, identifying the model that minimizes estimated information loss while preventing overfitting.
In empirical research, statistical modeling rarely involves evaluating a single hypothesis in isolation. Instead, scientists frequently specify multiple plausible parameterizations—varying the set of explanatory covariates, functional forms, or interaction terms—and require an objective criterion to identify the model that provides the most parsimonious representation of the underlying data-generating process. Base R provides native tools for extracting AIC across linear models, generalized linear models, and non-linear regression objects, while specialized packages such as MASS, MuMIn, and AICcmodavg extend this framework to stepwise regression, small-sample bias corrections (AICc), and multimodel inference.
What it is used for in research
The primary use of AIC in research is model selection based on information theory. Formulated by Hirotugu Akaike in 1974, AIC establishes a formal connection between the maximized empirical log-likelihood of a model and the Kullback-Leibler (K-L) divergence between the true, unknown data-generating distribution and the approximating model. Because the true distribution is unknown in empirical research, absolute K-L divergence cannot be computed directly. However, Akaike proved that the maximized log-likelihood of a model is a positively biased estimator of the relative expected K-L divergence, and that the asymptotic bias is approximately equal to the number of freely estimated parameters, denoted as k. Multiplying by -2 yields the classic Akaike Information Criterion formula:
AIC = -2log(L) + 2k
where L represents the maximized likelihood of the model given the observed data, and k represents the total count of estimated parameters (including regression coefficients, intercepts, and scale or variance parameters). Because lower K-L divergence indicates less information lost when approximating reality, the decision rule is straightforward: among a set of candidate models fitted to identical data, the model with the minimum AIC value is judged to have the best empirical support.
Researchers across diverse fields rely on AIC in R for specific analytical advantages:
- Non-nested model comparison: Unlike traditional likelihood ratio tests or F-tests—which strictly require nested models where one model is a constrained subset of another—AIC evaluates non-nested model architectures. For instance, an ecologist can directly compare an exponential growth model against a logistic specification without requiring a shared hierarchical framework.
- Balancing parsimony and goodness of fit: Unpenalized measures such as the raw coefficient of determination (R-squared) or raw log-likelihood monotonically improve as additional predictors are added to a model, inevitably encouraging severe overfitting. The 2k penalty in AIC directly counterbalances fit improvement against model complexity.
- Multimodel inference and model averaging: Rather than forcing an all-or-nothing selection of a single “best” model, information-theoretic frameworks allow researchers to compute Akaike weights (model probabilities) across an entire candidate set. Parameter estimates can then be averaged across models in proportion to their relative empirical support.
- Methodological compatibility: Researchers evaluating statistical criteria often compare AIC with the Bayesian Information Criterion; for a deeper exploration of asymptotic efficiency versus dimensional consistency, see the AIC vs. BIC for model selection comparison. Similarly, in longitudinal studies, AIC guides choices between fixed and random effects, as explored in our guide to panel data analysis.
How to install or access it
The primary functions for computing AIC in R—namely AIC(), extractAIC(), BIC(), and logLik()—are integral components of the standard stats package. The stats package is bundled with the base distribution of R maintained by the R Core Team and is automatically loaded whenever an R interactive session or script initiates. Consequently, calculating basic AIC requires zero external package installation or network access.
To verify that your R environment is functional and to inspect the underlying base distribution version, run:
getRversion()
sessionInfo()
While base R handles single-model extraction and stepwise regression, specialized empirical research often demands extended capabilities, including small-sample bias corrections (AICc), automated dredging, and multi-model averaging tables. These workflows utilize three standard, peer-reviewed CRAN packages:
MASS: ProvidesstepAIC()for bidirectional, forward, and backward stepwise model selection.MASSis a recommended package bundled with standard R binary distributions.MuMIn: Multi-Model Inference package authored by Kamil Barton. ProvidesAICc(),dredge(),model.sel(), andmodel.avg().AICcmodavg: Developed by Marc J. Mazerolle, specializing in model selection tables, Akaike weights, and goodness-of-fit assessments for generalized linear and mixed models.
To install and load these CRAN packages in an R session, execute the following commands:
install.packages(c("MASS", "MuMIn", "AICcmodavg"))
library(MASS)
library(MuMIn)
library(AICcmodavg)
Researchers managing data pipelines before model fitting frequently organize predictors and transform variables using the dplyr package in R to guarantee clean, complete datasets prior to likelihood estimation.
Step-by-step task
The following step-by-step walkthrough demonstrates how to fit competing regression models, extract AIC, calculate small-sample corrections, execute stepwise selection, and generate multimodel inference tables in R.
Step 1: Prepare data and enforce strict observation consistency
Before fitting any models for AIC comparison, you must guarantee that every candidate model is evaluated on the exact same set of observations. When datasets contain missing values (NA), R’s modeling functions automatically drop rows with missing values in the variables specific to that model. If Model A contains variables X1 and X2 (with no missing values), while Model B contains X1, X2, and X3 (where X3 has missing values), Model B will be fitted to fewer observations. Comparing AIC values across different sample sizes is mathematically invalid because the likelihood depends directly on the sample size n.
To prevent this defect, subset the dataset to complete cases across all candidate variables prior to model estimation:
data(mtcars)
vars_to_use <- c("mpg", "wt", "hp", "qsec", "drat")
clean_data <- na.omit(mtcars[, vars_to_use])
nrow(clean_data)
Step 2: Fit candidate models
Fit several competing regression models representing alternative scientific hypotheses regarding fuel efficiency (mpg):
m1 <- lm(mpg ~ wt, data = clean_data)
m2 <- lm(mpg ~ wt + hp, data = clean_data)
m3 <- lm(mpg ~ wt + hp + qsec, data = clean_data)
m4 <- lm(mpg ~ wt * hp + qsec, data = clean_data)
Step 3: Extract AIC for individual models
Use the base AIC() function to compute the criterion for a single model object:
AIC(m1)
Under the hood, AIC() invokes the logLik() method for class lm. For Gaussian linear regression, logLik.lm estimates both the regression coefficients and the residual variance (sigma-squared). Thus, for Model 1 (intercept, slope for wt, and residual variance), the degrees of freedom parameter k equals 3.
Step 4: Compare multiple candidate models simultaneously
Pass multiple model objects as comma-separated arguments directly into AIC():
model_comp <- AIC(m1, m2, m3, m4)
print(model_comp)
The function returns a clean data.frame with two columns: df (degrees of freedom, representing total estimated parameters) and AIC. You can easily sort the table by ascending AIC to identify the top-ranked model:
model_comp[order(model_comp$AIC), ]
Step 5: Apply small-sample correction (AICc)
When the ratio of sample size n to the number of parameters k is small (typically n / k < 40), standard AIC underestimates the penalty term and tends to select overly complex models. The second-order bias-corrected criterion, AICc, was developed by Hurvich and Tsai (1989) and popularized by Burnham and Anderson (2002):
AICc = AIC + (2k(k + 1)) / (n - k - 1)
In our mtcars example, n = 32. For Model 4, k = 6 total parameters (intercept, wt, hp, qsec, wt:hp interaction, plus residual variance). Here n / k = 32 / 6 = 5.33, which is well below 40. Computing AICc is essential:
MuMIn::AICc(m1, m2, m3, m4)
Step 6: Stepwise model selection with MASS::stepAIC
When searching across a large feature space, researchers often use stepwise search algorithms to locate the parameter combination that minimizes AIC. The stepAIC() function in the MASS package automates backward elimination, forward selection, or bidirectional search:
full_fit <- lm(mpg ~ wt + hp + qsec + drat, data = clean_data)
step_fit <- stepAIC(full_fit, direction = "both", trace = 0)
summary(step_fit)
Setting trace = 0 suppresses intermediate step logging, while trace = 1 outputs the parameter dropped or added at each iteration along with the resulting criterion value.
Step 7: Multi-model inference and Akaike weights
To quantify empirical uncertainty across candidate models, calculate Delta AIC (the difference between each model’s AIC and that of the top-ranked model) and Akaike weights (wi):
sel_table <- MuMIn::model.sel(m1, m2, m3, m4)
print(sel_table[, c("df", "logLik", "AICc", "delta", "weight")])
The Akaike weight wi represents the normalized relative likelihood of model i, interpreted as the probability that model i is the best approximating model in the candidate set, given the data.
Output and how to read it
Interpreting R’s model selection output requires understanding the exact components of the returned objects, the nuances of parameter counting, and the statistical significance of relative differences.
Deconstructing the AIC() output table
When multiple models are supplied to AIC(m1, m2, m3), R displays an output structured as follows:
df AIC
m1 3 166.0294
m2 4 156.6523
m3 5 154.1160
m4 6 151.7892
Key elements of this output include:
- Row names (m1, m2, …): Correspond to the names of the fitted model objects passed into the function.
- Degrees of freedom (df): Represents the total number of freely estimated parameters k. For a standard linear regression model fitted via
lm()with p regression coefficients (including the intercept beta_0), R setsdf = p + 1. The additional parameter is the residual variance estimate sigma-squared. In contrast, for Poisson or binomial GLMs with fixed dispersion,dfequals exactly the number of regression coefficients p. - AIC value: The calculated criterion score. Absolute values depend on arbitrary scaling factors, measurement units of the response variable, and sample size. Therefore, absolute AIC values have no standalone interpretation; only differences (Delta AIC) between models fitted to identical data are meaningful.
The difference between AIC() and extractAIC()
A frequent source of confusion among empirical researchers is that stats::AIC(fit) and stats::extractAIC(fit) produce different numbers for the exact same linear regression model. For example, evaluating m1 <- lm(mpg ~ wt, data = mtcars) yields an AIC() value of approximately 166.03, whereas extractAIC(m1) yields an AIC value of approximately 73.21.
This difference arises from how constants and dispersion parameters are handled:
stats::AIC(): Evaluates the full maximized log-likelihood fromlogLik(), retaining all constant terms (such as -n/2 * log(2*pi) and -n/2) and explicitly counting the estimated residual variance as a parameter (df = p + 1).stats::extractAIC(): Designed specifically for computational efficiency inside stepwise routines likestep()andMASS::stepAIC(). Forlmmodels, it drops additive constants that do not vary across models and computes n * log(RSS / n) + 2p, treating the scale parameter as fixed rather than estimated in its parameter count (edf = p).
Because the omitted constants and the variance penalty offset are strictly identical across models fitted to the same dataset with the same sample size, the relative difference Delta AIC between any pair of models is identical under both functions. Nonetheless, researchers reporting criterion values in publications must explicitly state which function was employed.
Benchmarking Delta AIC and evidence ratios
To assess the strength of empirical support for competing models, researchers compute Delta AIC:
Delta_i = AIC_i - min(AIC)
Burnham and Anderson (2002) provide widely cited, peer-reviewed guidelines for interpreting Delta AIC values:
| Delta AIC (Delta_i) | Level of Empirical Support | Operational Interpretation in Research |
|---|---|---|
| 0 to 2 | Substantial support | Model has strong empirical support; essentially indistinguishable from the top model. Consider model averaging. |
| 4 to 7 | Considerably less support | Model has weak empirical backing; plausible but unlikely to be the primary approximating structure. |
| Greater than 10 | Essentially no support | Model can be safely ruled out from practical consideration; fails to capture key data patterns. |
Furthermore, the relative plausibility between any two models can be expressed as an evidence ratio: ER = w_1 / w_2, where w_1 and w_2 are their respective Akaike weights. An evidence ratio of 8.5 indicates that the top-ranked model is 8.5 times more likely to be the best approximating model than the second-ranked alternative.
Troubleshooting
Applying AIC in empirical research involves common methodological pitfalls. Below are diagnostics and verified solutions for frequent errors encountered in R.
1. Error: “models were not all fitted to the same size of dataset”
Symptom: Calling AIC(m1, m2) throws an explicit error or warning stating that models differ in number of observations, or silently produces misleading criterion rankings.
Root cause: One of your candidate predictors contains missing values (NA). When R evaluates lm() or glm(), its default setting na.action = na.omit drops rows containing missing values for the terms in that formula. Consequently, models with more covariates are evaluated on a smaller sample size n, artificially deflating the log-likelihood and invalidating AIC comparisons.
Solution: Pre-screen candidate datasets and restrict all models to complete cases across all variables considered in the candidate set using na.omit() before model fitting. In MuMIn, setting options(na.action = "na.fail") forces R to throw an immediate error if a model is fitted to incomplete data, preventing silent sample size discrepancies.
2. Comparing models with transformed response variables
Symptom: A researcher attempts to compare a linear model lm(y ~ x) against a log-transformed model lm(log(y) ~ x) using AIC.
Root cause: The likelihood of a model depends on the scale and probability density of the dependent variable. When the response variable is transformed via f(y) = log(y), the likelihood of the transformed model is computed on the scale of log(y), not y. Comparing raw AIC values across different response scales is mathematically invalid.
Solution: To compare a model of y against a model of log(y), adjust the log-likelihood of the log-transformed model by adding the log of the Jacobian of the transformation: sum(log(1 / y_i)) = -sum(log(y_i)). Alternatively, fit a Generalized Linear Model with a log link function (e.g., glm(y ~ x, family = gaussian(link = "log"))), which preserves the response variable on its original scale y.
3. Comparing random-effects models fitted with REML
Symptom: Calling AIC() on mixed-effects models fitted via lme4::lmer() with differing fixed-effect structures produces invalid comparisons.
Root cause: By default, linear mixed models in R use Restricted Maximum Likelihood (REML) estimation. REML estimates variance components by maximizing the likelihood of linear contrasts of the data that are invariant to the fixed effects. Consequently, REML likelihoods depend on the fixed-effects design matrix X. Models with different fixed effects possess different REML likelihood baselines and cannot be compared using AIC.
Solution: When using AIC to select among competing fixed-effects structures in mixed models, refit the models using standard Maximum Likelihood by setting REML = FALSE in lmer(). Once the optimal fixed structure is chosen, refit the final model with REML = TRUE to obtain unbiased variance component estimates.
4. Overdispersion in count regression models
Symptom: In Poisson GLMs fitted with glm(y ~ x, family = poisson), the residual deviance greatly exceeds the residual degrees of freedom, yet standard AIC continues to favor complex models.
Root cause: Count data frequently exhibit overdispersion (variance greater than mean). Unmodeled overdispersion inflates test statistics and biases standard AIC. In such settings, standard AIC is inappropriate; researchers should evaluate negative binomial models or quasi-likelihood frameworks as described in our comparison of quasi-Poisson vs. negative binomial regression.
Solution: Calculate Quasi-AIC (QAIC) or QAICc using the estimated variance inflation factor c-hat (c-hat = residual deviance / residual df). When c-hat exceeds 1.0, use AICcmodavg::aictab() with the c.hat parameter specified, which scales the log-likelihood by c-hat and adds an extra parameter for overdispersion.
Free alternatives
While Akaike’s information criterion is the most widely adopted metric for model selection in R, several open-source alternatives and complementary paradigms exist across computational research.
| Method / Tool | Underlying Principle | Penalty or Mechanism | Primary Advantage | Key Limitation |
|---|---|---|---|---|
| Bayesian Information Criterion (BIC / SBC) | Approximates integrated marginal likelihood | k * log(n) penalty per parameter | Dimension consistent; asymptotically identifies true model if in set | Often underfits in finite samples; penalizes complex models harshly |
| Second-order AIC (AICc) | Corrects small-sample finite bias in AIC | AIC + 2k(k+1) / (n – k – 1) | Prevents severe overfitting when n / k < 40; converges to AIC as n grows | Requires explicit sample size n; complex to define in hierarchical random effects |
| K-Fold Cross-Validation (boot::cv.glm) | Out-of-sample empirical predictive accuracy | Data partitioning (e.g. 5-fold, 10-fold) | Free from distributional assumptions; directly evaluates predictive generalization | Computationally demanding on large datasets; variance across folds |
| LASSO / Elastic Net (glmnet) | L1 / L2 penalized maximum likelihood | Lambda * sum(|beta|) coefficient shrinkage | Simultaneously handles estimation and variable selection; works when p > n | Does not provide classical p-values or standard errors without post-selection inference |
| Watanabe-Akaike Information Criterion (WAIC) | Fully Bayesian predictive information criterion | Computed across posterior MCMC draws in loo package | Averages over parameter uncertainty; valid for singular and hierarchical models | Requires full Bayesian posterior sampling via Stan or brms |
The choice between AIC and BIC often hinges on research objectives: AIC seeks the model that minimizes prediction error (asymptotic efficiency), whereas BIC seeks the true data-generating model under the assumption that the true model is contained within the candidate set. Cross-validation serves as an empirical verification step that does not rely on asymptotic likelihood theory.
The data table below compares the technical specifications, degrees of freedom handling, and software interfaces across primary R model selection functions.
Frequently asked questions
Can you compare AIC between models fitted to different datasets?
No. AIC evaluates the relative log-likelihood of models given the exact same observed data. If datasets differ in sample size, observation filtering, or variable transformations, their likelihood scales are non-comparable, rendering AIC comparisons invalid.
Why does AIC() give a different number than extractAIC() in R?
The standard AIC() function computes full maximized log-likelihood including all additive constants and counts residual variance as an estimated parameter (df = p + 1 for linear models). In contrast, extractAIC() drops constant terms and sets df = p for computational speed in stepwise routines. Both yield identical relative differences (Delta AIC) across models fitted to identical data.
When should AICc be used instead of standard AIC?
Second-order AIC (AICc) should be used whenever the ratio of sample size n to the number of estimated parameters k is less than 40 (n / k < 40). Because AICc asymptotically converges to AIC as sample size increases, many methodologists recommend using AICc by default across all empirical studies.
What is a good or acceptable AIC value in R?
There is no universal threshold or “good” absolute AIC value. Absolute AIC scores are arbitrary and depend on sample size, variance, and measurement units. In model selection, only relative differences (Delta AIC) between competing models fitted to identical data matter, where lower values indicate superior empirical support.
Can AIC be used to compare linear regression with log-transformed regression?
Not without adjustment. Transforming the dependent variable (e.g., from y to log(y)) alters the scale of the probability density function. To compare models on different response scales, you must add the log of the Jacobian of the transformation to the log-likelihood of the transformed model, or fit generalized linear models with link functions.
How does AIC differ from BIC for model selection in R?
AIC applies a fixed penalty of 2k per parameter, whereas BIC applies a sample-size-dependent penalty of k * log(n). When sample size n exceeds 7 (log(7) is approximately 1.95), BIC penalizes additional parameters more severely than AIC, favoring simpler, more parsimonious models.
Sources
- Akaike, H. (1974). A new look at the statistical model identification. IEEE Transactions on Automatic Control, 19(6), 716-723. https://doi.org/10.1109/TAC.1974.1100705 (Accessed 2026-11-14)
- Barton, K. (2023). MuMIn: Multi-Model Inference. Comprehensive R Archive Network (CRAN). https://cran.r-project.org/web/packages/MuMIn/MuMIn.pdf (Accessed 2026-11-14)
- Burnham, K. P., and Anderson, D. R. (2002). Model Selection and Multimodel Inference: A Practical Information-Theoretic Approach (2nd ed.). Springer-Verlag. https://doi.org/10.1007/b97636 (Accessed 2026-11-14)
- Hurvich, C. M., and Tsai, C.-L. (1989). Regression and time series model selection in small samples. Biometrika, 76(2), 297-307. https://doi.org/10.1093/biomet/76.2.297 (Accessed 2026-11-14)
- Mazerolle, M. J. (2023). AICcmodavg: Model selection and multimodel inference based on (Q)AIC(c). Comprehensive R Archive Network (CRAN). https://cran.r-project.org/web/packages/AICcmodavg/AICcmodavg.pdf (Accessed 2026-11-14)
- R Core Team (2026). R: A Language and Environment for Statistical Computing. R Foundation for Statistical Computing, Vienna, Austria. Documentation for stats::AIC. https://stat.ethz.ch/R-manual/R-devel/library/stats/html/AIC.html (Accessed 2026-11-14)
- R Core Team (2026). Documentation for stats::extractAIC. https://stat.ethz.ch/R-manual/R-devel/library/stats/html/extractAIC.html (Accessed 2026-11-14)
- Venables, W. N., and Ripley, B. D. (2002). Modern Applied Statistics with S (4th ed.). Springer, New York. Package MASS documentation on CRAN. https://cran.r-project.org/web/packages/MASS/MASS.pdf (Accessed 2026-11-14)
Data: Model Selection and Information Criterion Functions in R
Comparative specifications of base R and CRAN model selection functions, reporting criterion formulas, degrees of freedom handling, variance penalty inclusion, and supported model classes.
| Function | Target Criterion | Formula Structure | Residual Variance in df | Small Sample Correction | Automated Search Mode |
|---|---|---|---|---|---|
| stats::AIC | AIC | -2*logLik + 2*k | Yes (p + 1) | No (requires MuMIn or manual) | Manual comparison across candidate models |
| stats::extractAIC | Generalized AIC | n*log(RSS/n) + 2*p | No (p only) | No | Stepwise via step(), add1(), drop1() |
| stats::BIC | BIC / SBC | -2*logLik + log(n)*k | Yes (p + 1) | No | Manual comparison across candidate models |
| MASS::stepAIC | AIC / BIC | extractAIC-based -2*logL + k*edf | No (p only) | No | Stepwise (both, backward, forward) |
| MuMIn::AICc | AICc | AIC + 2*k*(k+1)/(n-k-1) | Yes (p + 1) | Yes (native second-order) | Manual or exhaustive dredging via dredge() |
| AICcmodavg::aictab | AICc / QAICc | -2*logL + 2*K + correction | Yes (K includes variance) | Yes (native second-order and overdispersion) | Candidate set ranking and Akaike weights |
Sources
- R Core Team (2026) stats::AIC Reference Manual (accessed 2026-11-14)
- R Core Team (2026) stats::extractAIC Reference Manual (accessed 2026-11-14)
- Venables and Ripley (2002) MASS Package CRAN Manual (accessed 2026-11-14)
- Barton (2023) MuMIn Package CRAN Manual (accessed 2026-11-14)
- Mazerolle (2023) AICcmodavg Package CRAN Manual (accessed 2026-11-14)
Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.







