Skip to content

AIC value inconsistent with R #6577

Closed

Description

@JoonsukPark

Describe the bug

The definition of AIC in linear regression (sm.api.OLS()) does not match that of R. This is due to the fact that the number of parameters is defined as (df_model + 1), which does not take into account the variance term that is also estimated from the data. (This is why this bug happens in OLS but not in case of GLM.) I think this should have been (df_model + 2). This bug actually does not change rankings of models because the +1 term vanishes when comparing models, but the definition is conceptually wrong and would potentially cause incompatibilities with some of R functions.

Code Sample, a copy-pastable example if possible

import statsmodels.api as sm
from sklearn.datasets import load_iris
from numpy import c_, ones, log

data = load_iris()

X = c_[ones(data['data'].shape[0]), data['data'][:, 1]]
y = data['data'][:, 0]
n = data['data'].shape[0]

model = sm.OLS(y, X)
fit = model.fit()

print(fit.aic) # gives 369.9916713254629
print(fit.bic) # gives 376.0129419136554

print(-2fit.llf + 32) # R AIC: 371.9916713254629
print(-2fit.llf + 3log(n)) # R BIC: 381.02357720775166

# Your code here that produces the bug
# This example should be self-contained, and so not rely on external data.
# It should run in a fresh ipython session, and so include all relevant imports.
Details

Note: As you can see, there are many issues on our GitHub tracker, so it is very possible that your issue has been posted before. Please check first before submitting so that we do not have to handle and close duplicates.

Note: Please be sure you are using the latest released version of statsmodels, or a recent build of master. If your problem has been fixed in an unreleased version, you might be able to use master until a new release occurs.

Note: If you are using a released version, have you verified that the bug exists in the master branch of this repository? It helps the limited resources if we know problems exist in the current master so that they do not need to check whether the code sample produces a bug in the next release.

If the issue has not been resolved, please file it in the issue tracker.

Expected Output

A clear and concise description of what you expected to happen.

Output of import statsmodels.api as sm; sm.show_versions()

Details

[paste the output of import statsmodels.api as sm; sm.show_versions() here below this line]

Activity

  1. josef-pkt commented on Mar 10, 2020

    @josef-pkt
    Member

    same issue for BIC #1802

    no decision to change it yet

  2. bashtage commented on May 13, 2020

    @bashtage
    Member

    Label this as a FAQ. I think we are past the point where this can change.

  3. comatrion commented on Nov 29, 2020

    @comatrion
    Contributor

    Still showing in 0.12
    If it's wrong, could it be corrected?
    It's not just a diagnostic: if you're using the AIC/BIC to compare models, it can have a material effect.

  4. bashtage commented on Nov 29, 2020

    @bashtage
    Member

    It does not affect the relative rank of models with the same IC which is how IC are used in model selection.

  5. josef-pkt commented on Nov 29, 2020

    @josef-pkt
    Member

    The plan is to add an infocrit method to results classes that has options. GLM also does not count an estimated scale or dispersion parameter in the ic.

    In OLS (and GLM), the parameter estimates and fit in terms of sum of squares, or residual measures is independent of a scale estimate. So there is not necessarily a reason to include the scale estimate when comparing models with different exog.

    But we do want consistent definitions across models, so we can us the ic for choosing among those, e.g. count models with different distributional assumptions.

  6. ChadFulton commented on Nov 29, 2020

    @ChadFulton
    Member

    Just a note that state space models have an info_criteria method rather than an infocrit method.

  7. josef-pkt commented on Nov 29, 2020

    @josef-pkt
    Member

    @ChadFulton Thanks, then we can coordinate on that.
    I haven't looked at tsa in some time.)

  8. josef-pkt commented on Nov 29, 2020

    @josef-pkt
    Member

    @ChadFulton
    Is scale included in the count of parameters for ic in tsa and statespace?

  9. ChadFulton commented on Nov 30, 2020

    @ChadFulton
    Member

    Is scale included in the count of parameters for ic in tsa and statespace?

    TL;DR: SARIMAX, ARIMA, and AutoReg all include the scale in the definition of AIC, while VAR does not.


    Scale is included in df_model and in the information criteria computations for state space models (e.g. SARIMAX). This is true both when it is estimated as part of the overall parameter vector and when we use the version where we first concentrate it out of the likelihood.

    I took a look at the old ARIMA model, and there it is also included in the information criteria. This model always estimates the scale by concentrating it out, however, df_model does not include report it (so the information criteria computations use the private _ic_df_model.

    The IC for both AutoReg and VAR are based on Lutkepohl's definition, and they (a) do not include the intercept terms, (b) drops the constant term, and (c) divide through by nobs (in fact, Stata's varsoc divides even its standard AIC through by nobs). However, AutoReg does include the scale term as a parameter (different from Lutkepohl's formulation) while VAR does not include the free terms in the error covariance matrix (the same as Lutkepohl's formulation).


    One other thing that is interesting / annoying: the way that Lutkepohl forms the AIC by concentrating out the error covariance matrix implies that the constant term that is dropped is of the form k_endog * (np.log(2 * np.pi) + 1), while in other cases (such as R's forecast package, if I remember correctly), the constant term that is dropped is of the form k_endog * np.log(2 * np.pi). (Where, again, either of these cases may or may not also be multipled by nobs, depending on the definition used).

  10. josef-pkt commented on Nov 30, 2020

    @josef-pkt
    Member

    @ChadFulton Thanks for the overview

    sounds also pretty messy outside full MLE

    One problem is that in the least squares and quasi-likelihood version we don't need a fully specified likelihood and variance/scale estimate are in most of those cases orthogonal to the mean parameter estimates.

    The traditional sum of squares, Sigma definition of aic divides by nobs
    https://www.statsmodels.org/devel/generated/statsmodels.tools.eval_measures.aic_sigma.html

    I'm only part way to understanding more of the IC for not fully specified or misspecified likelihood cases.
    e.g. TIC and GIC #6516
    I still need to see how this works out in various models with specified or un-/misspecified likelihood function.

    Adding options for various definitions of IC pushes the decision which to use to the user :)

    As Kevin pointed out, any constant term is irrelevant for specification decisions within a model. But if we have different models or different distributional assumptions for the same underlying modelling task, then we need consistent definitions, whatever they are.

    Another issue: What is relevant in the IC might also depend on the target of the estimation.
    In OLS and similar we are interested in the mean function and we might not care about the exact distribution of the residuals. So scale and higher order properties of OLS or Poisson model are not of interest.
    If we are interested in a correctly specified distribution, then we don't want to ignore those extra features of the distribution.
    #7142

    (I guess a tsa example would be an ARMA-GARCH that is separable in mean and variance model. For the specification search for the mean model, we don't really need to include all parameters from the GARCH model. If we want to check the overall fit, then we need the full model.
    My similar examples these days are count models and overdispersion)

  11. added this to the 0.13 milestone on Sep 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions