anova_lm for GLM

250 views
Skip to first unread message

Thomas Haslwanter

unread,
May 10, 2013, 1:25:32 PM5/10/13
to pystat...@googlegroups.com
Background:
I am currently trying to put together Python/stats-models solutions for the problems in the textbook

Dobson AJ & Barnett AG: "An Introduction to Generalized Linear Models"
3rd ed
CRC Press(2008)


The book currently includes solution code for STATA and for R, and I believe that adding a solution set for Python/statsmodels could really help to advertise statsmodels.

Question:
When showing how to run ANOVAs in statsmodels, I first tried to use anova_lm it with

glm_model = sm.GLM.from_formula('weight~group', family=sm.families.Gaussian(), data=df).fit()

(I did this in order to stay as close as possible to the R-formulations).
When this did not work (because the GLM-class does not include an attribute "ssr"), I implemented it with

model = sm.OLS.from_formula('weight~group', data=df).fit()

which worked.

The documentation for anova_lm says

"ANOVA table for one or more fitted linear models."

which I thought should also include GLM.

Would it be sufficient to add an attribute "ssr" to .fit()'s from GLM models, to make it work with ANOVA?
And do you think that might be worth-while?
Otherwise, it should at least be mentioned in the docu of anova_lm that it only works with OLS-output.

josef...@gmail.com

unread,
May 10, 2013, 1:46:34 PM5/10/13
to pystat...@googlegroups.com
On Fri, May 10, 2013 at 1:25 PM, Thomas Haslwanter
<thomas.h...@gmail.com> wrote:
> Background:
> I am currently trying to put together Python/stats-models solutions for the
> problems in the textbook
>
> Dobson AJ & Barnett AG: "An Introduction to Generalized Linear Models"
> 3rd ed
> CRC Press(2008)
>
> The book currently includes solution code for STATA and for R, and I believe
> that adding a solution set for Python/statsmodels could really help to
> advertise statsmodels.

That's "super".

>
> Question:
> When showing how to run ANOVAs in statsmodels, I first tried to use anova_lm
> it with
>
> glm_model = sm.GLM.from_formula('weight~group',
> family=sm.families.Gaussian(), data=df).fit()
>
> (I did this in order to stay as close as possible to the R-formulations).
> When this did not work (because the GLM-class does not include an attribute
> "ssr"), I implemented it with
>
> model = sm.OLS.from_formula('weight~group', data=df).fit()
>
> which worked.
>
> The documentation for anova_lm says
>
> "ANOVA table for one or more fitted linear models."
>
> which I thought should also include GLM.
>
> Would it be sufficient to add an attribute "ssr" to .fit()'s from GLM
> models, to make it work with ANOVA?
> And do you think that might be worth-while?
> Otherwise, it should at least be mentioned in the docu of anova_lm that it
> only works with OLS-output.

I don't see a reason why we shouldn't just use OLS in this case. Using
GLM is an inefficient way to calculate this.

Is there a general definition of `ssr` that would apply to GLM outside
of the gaussian family, and outside of gaussian with the linear link
function?

Is there an ANOVA equivalent for non-gaussian non-linear GLM? (maybe
just likelihood ratio and Wald tests?)

Are there results in GLM (residuals, ...) that we don't have available for OLS?

I would prefer to keep distribution and link specific
results/attributes mostly out of GLM, especially if there is already
an alternative.

Josef

Skipper Seabold

unread,
May 10, 2013, 2:22:20 PM5/10/13
to pystat...@googlegroups.com
On Fri, May 10, 2013 at 1:25 PM, Thomas Haslwanter
<thomas.h...@gmail.com> wrote:
I think it's a simple fix to make ANOVA also work on GLMs using the
deviance instead of the SSR. I need to check my references though to
be sure.

If you file a ticket I'll look into it at some point soon-ish.

Skipper
Reply all
Reply to author
Forward
0 new messages