You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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...
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?
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
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).
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.
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...
"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.)
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:
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.
Activity
thequackdaddy commentedon Jul 9, 2016
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?
josef-pkt commentedon Jul 9, 2016
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
thequackdaddy commentedon Jul 9, 2016
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.
thequackdaddy commentedon Jul 9, 2016
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).
josef-pkt commentedon Jul 9, 2016
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.
thequackdaddy commentedon Jul 9, 2016
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...
josef-pkt commentedon Jul 10, 2016
"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_lrtestto 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.)
thequackdaddy commentedon Jul 10, 2016
Sorry what I meant is that when I ran...
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:
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.
Add files via upload