Skip to content

Pearson Chi2 Test Statistic in a GLM #3101

Open

Description

@thequackdaddy

I'm trying to compare 2 models using a Pearson Chi-2 type 3 statistical test. I've done this before in some expensive software, so was trying to figure out how to do it with python.

That procedure followed (I'm fairly sure) what exists in this paper. In essence, you calculate 2 values for a pair of nested models (nested meaning that one model has every parameter in the other model, and also has at least one additional parameter the other model doesn't.)

In python/statsmodels, I was thinking you could calculate it this way...

df_diff = res1.df_resid - res2.df_resid
chi2_diff = res1.pearson_chi2 - res1.pearson_chi2

from scipy.stats import chi2
1 - chi2.cdf(chi2_diff, df_diff)

I'm getting extremely large values that I don't believe are accurate.

Looking through the literature, I found this paper by Smyth. It uses this formula when scale == 1:

image

However, more generally, it uses (see Section 6, "Unknown Dispersion Parameter"):

image

Should we be dividing by the scale parameter as well? If I understand the current code, we do not:

https://github.com/statsmodels/statsmodels/blob/master/statsmodels/genmod/generalized_linear_model.py#L1325

Thoughts?

Activity

  1. thequackdaddy commented on Jul 9, 2016

    @thequackdaddy
    MemberAuthor

    I just realized that the above question is dumb, dividing by scale will give you the df_resid. Still, it makes me wonder what this software package called the "chi-sq" statistic. Perhaps it was really the F-test?

  2. josef-pkt commented on Jul 9, 2016

    @josef-pkt
    Member

    Does the extra parameter refer to an additional explanatory variable in exog or to some other parameter?

    In Binomial and Poisson the scale is 1 under the assumption of correct specification. In gaussian and the others including Tweedie we have an additional scale parameter not fixed at one. In dispersed Poisson or Binomial we also have an additional scale.

    With fixed scale=1, we can just take the difference of pearson chi2 or of the deviance as the test statistic and chisquare distribution for the p-values
    If scale is estimated, then we need to divide the difference of pearson chi2 or of deviance by the scale of the unrestricted/larger model. I haven't actually tried that yet with scale different from 1, but it's largely in analogy to F-test in the linear model.

    To your last comment, pearson_chi2 is in itself a test statistic if the scale is fixed, but as you said it's just equal to df_resid by definition. However we can still compare two nested models

  3. thequackdaddy commented on Jul 9, 2016

    @thequackdaddy
    MemberAuthor

    Ahhh, this makes sense to me then....

    chi2_diff = (res2.pearson_chi2 - res1.pearson_chi2) / model1.scale, where model1.df_model > model2.df_model (model1 is nested in model2).

    When I add a parameter I know is meaningless (from the p-value), I get a bad chi2 score.

    I think this is what I need. Thanks.

  4. thequackdaddy commented on Jul 9, 2016

    @thequackdaddy
    MemberAuthor

    And yes, the additional parameter is an extra column in exog. Generally, its like I'm testing whether adding a categorical variable for a discount or type of risk (class1 vs. class2).

  5. josef-pkt commented on Jul 9, 2016

    @josef-pkt
    Member

    Note, that you can also use Wald and score tests for the same hypothesis. Asymptotically they are the same.

    One problems with hypothesis testing at a usual significance level is that in large samples like you have, even small/tiny effects will be statistically significant because the power to reject even small deviations from the null hypothesis is large.

  6. thequackdaddy commented on Jul 9, 2016

    @thequackdaddy
    MemberAuthor

    First, you answered a question before I even was about to ask it. Yes, you are right, on my 4.8 million record dataset I indeed am finding that adding a exog parameter which I know is insignificant (like a discount that only 1% of the records get, and p-value is 0.7) still gives a 0.0% Chi2. That's not helpful.

    However I swear that the software package that I used to deal with would calculate values for this. And the Chi-Squared scores would generally jive with p-scores. I mainly used that when I had a categorical with 3 levels, and I wanted to know if I should have separate parameters for each level or if there was some grouping (setting levels 2 and 3 equal to each other).

    It makes me wonder what the number really meant. There were a few things that the software sort of hid from the user (to simplify the modeling process) and so I imagine there is some trick it was doing...

  7. josef-pkt commented on Jul 10, 2016

    @josef-pkt
    Member

    "0.0% Chi2" Does that mean 0 pearson_chi2 or the diff in pearson_chi2 is 0?

    p-value=0.7 sounds like the test statistic is reasonable for an insignificant effect. 1% of records is still a large number in your case, so I would have expected that it would be significant even in this case if it affects the 1% of observations.

    Can you post/add your code here or somewhere?
    We need to add this to GLM. We have several issues open, but I only read about the dispersion case a while ago and don't have any verified code yet.
    We have an open PR for adding compare_lrtest to other models than the linear models, but I wasn't sure about the general pattern that it needs to have. Given that tweedie doesn't have loglike, we need to add the deviance and pearson_chi2 versions to GLM.

    Aside: IIUC, If we use deviance or pearson_chi2, then this ignores the estimation of the tweedie power, it might be good to check the test with the same power coefficient in both models versus separate power coefficients. (My guess is that only the former is strictly correct.)

    (I'm travelling today, but I should have time to look at it over the next few days.)

  8. thequackdaddy commented on Jul 10, 2016

    @thequackdaddy
    MemberAuthor

    Sorry what I meant is that when I ran...

    from spicy.stats import chi2
    
    1 - chi2.cdf(pearson_chi2_diff / scale_of_larger, df_diff)

    I got 0.0%. Reading through some notes and documentation, I was thinking this was what the software did, in some fashion.

    After reading more, I realized something else. From this document I noticed the following on page:

    screen shot 2016-07-10 at 10 10 32 am

    It is saying compare the deviances, not the pearson_chi2 values. Maybe that's my mistake?

    When you ask me to "post/add your code", are you referring to just the lines above or do you want the entire model specification? There's actually multiple models at this point for different lines of business. I"m happy to send to you personally, Josef. But I'm hesitant to have them floating out on the internet as what I'm doing is still quite rough and unimplemented. Not that anything in the model would be a real shock to anyone. Its probably more shyness than anything else.

  9. added this to the 0.10 milestone on Oct 8, 2017
  10. modified the milestones: 0.10, 0.14 on Sep 9, 2021
  11. added a commit that references this issue on Oct 10, 2022
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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions