ANOVA

ANOVA

Analysis of Variance models

Examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
In [1]: import statsmodels.api as sm
 
In [2]: from statsmodels.formula.api import ols
 
In [3]: moore = sm.datasets.get_rdataset("Moore", "car",
   ...:                                  cache=True) # load data
   ...:
 
In [4]: data = moore.data
 
In [5]: data = data.rename(columns={"partner.status" :
   ...:                             "partner_status"}) # make name pythonic
   ...:
 
In [6]: moore_lm = ols('conformity ~ C(fcategory, Sum)*C(partner_status, Sum)',
   ...:                 data=data).fit()
   ...:
 
In [7]: table = sm.stats.anova_lm(moore_lm, typ=2) # Type 2 ANOVA DataFrame
 
In [8]: print table
                                              sum_sq  df          F    PR(>F)
C(fcategory, Sum)                          11.614700   2   0.276958  0.759564
C(partner_status, Sum)                    212.213778   1  10.120692  0.002874
C(fcategory, Sum):C(partner_status, Sum175.488928   2   4.184623  0.022572
Residual                                  817.763961  39        NaN       NaN

A more detailed example can be found here:

Module Reference

anova_lm(*args, **kwargs) ANOVA table for one or more fitted linear models.
doc_statsmodels
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.