Post-hoc contrast on ANOVA in statsmodels

88 views
Skip to first unread message

Ma Gugge

unread,
May 30, 2016, 10:15:14 AM5/30/16
to pystatsmodels
Hi,

has statsmodels a designated way to compute statistical tests on self-specified post-hoc contrasts in an ANOVA?

So if I have

from statsmodels.formula.api import ols
model = ols('y ~ C(factor1) + C(factor2) + C(factor1):C(factor2)', data).fit()

Then in addition to the ANOVA itself 

anova_lm(model, typ=2)

I want to compute e.g. a linear contrast on factor1 (with N levels):

c = np.arange(N) - np.arange(N).mean()

which computes a t-value and a p-value for the specified contrast for factor1.

If there is no shortcut to my issue I'd of course also be grateful for a more verbose solution.

Thanks,
Matthias

josef...@gmail.com

unread,
May 30, 2016, 10:20:25 AM5/30/16
to pystatsmodels
you can use the t_test method to specify arbitrary linear or affine constraints on the parameters. It's vectorized, so it can calculate it for several constraints at the same time.
For joint tests there is f_test and wald_test

see also the recent thread with title "post-hoc test of contrasts after MixedLM" 

For OLS and the other models without extra parameters it will work directly either with string constraints or contrast matrix.

Josef
 

Thanks,
Matthias

Ma Gugge

unread,
Jun 6, 2016, 8:24:20 AM6/6/16
to pystatsmodels
you can use the t_test method to specify arbitrary linear or affine constraints on the parameters. It's vectorized, so it can calculate it for several constraints at the same time.
For joint tests there is f_test and wald_test

see also the recent thread with title "post-hoc test of contrasts after MixedLM"

Thanks! I understand how I could specify a constraint across parameters using t_test. However, I don't understand how to specify a constraint across the different *levels* of a single parameter (what I called factor). In my example above, factor1 may be a categorical variable with N levels, and I would want to specify a linear test across these levels.

Cheers,
Matthias

josef...@gmail.com

unread,
Jun 6, 2016, 8:31:06 AM6/6/16
to pystatsmodels
If you use the formula interface and strings to define the contrasts, then you need to use the full names in the contrast specification as it is for example shown in summary().

That is, you need to use the long name that patsy created for the column which will include the level as part of the name

Josef

 

Cheers,
Matthias

Reply all
Reply to author
Forward
0 new messages