Repository navigation
Description
The issue
When trying to compute two-way nested ANOVA, the results do not equal the appropriate results from R (formulas and data are the same).
Sample
We use "atherosclerosis" dataset from here: https://stepik.org/media/attachments/lesson/9250/atherosclerosis.csv.
To get nested data we replace dose values for age == 2:
df['dose'] = np.where((df['age']==2) & (df['dose']=='D1'),'D3', df.dose)
df['dose'] = np.where((df['age']==2) & (df['dose']=='D2'),'D4', df.dose)So we have dose factor nested into age: values D1 and D2 are in first age and values D3 and D4 are only in the 2nd age.
After getting ANOVA table we have the results below:
mod = ols('expr~age/C(dose)', data=df).fit()
anova_table = sm.stats.anova_lm(mod, typ=1); anova_tableThe total sum of the 'sum_sg' = 1590.257424 + 47.039636 + 197.452754 = 1834.7498139999998 that is NOT equal the right total sum (computed below) = 1805.5494956433238
grand_mean = df['expr'].mean()
ssq_t = sum((df.expr - grand_mean)**2)Expected Output
Let's try to get ANOVA table in R:
df <- read.csv(file = "/mnt/storage/users/kseniya/platform-adpkd-mrwda-aim-imaging/mrwda_training/data_samples/athero_new.csv")
nest <- aov(df$expr ~ df$age / factor(df$dose))
print(summary(nest))Why they are not equal? The formulas are the same. Are there any mistakes in computing ANOVA through statsmodels?
The results from R seem to be right, because the total sum 197.5 + 17.8 + 1590.3 = 1805.6 is equal to the total sum computed manually.


Activity
josef-pkt commentedon Sep 26, 2022
I never looked at nested effects in details. So I don't know what's going on.
strange is that df =3 in statsmodels, and df=2 in R aov.
My main guess is that patsy encodes the nested effect differently than R. (A check would be to compare OLS with R lm.)
The second possibility is that statsmodels anova_lm does not recover type 1 anova correctly in this case.
(anova_lm backs out the terms for different types from the full model estimate. Using type 1 sequential models in anova_lm could be used to verify type 1 anova. That's possible in this case, the full model will not have SS decomposition because of the correlation of regressors, I guess.)
maybe bug, maybe different specification
kseniawa commentedon Nov 10, 2022
I found out how to get right results using statsmodels in the example. We should:
use same values of the nested factor within every level of the main factor (in my case only D1 and D2 values in the dose field).
note that age factor as a categorical one, so use C(age) in the formula: C(age)/C(dose) or even C(age)/dose
Only both conditions applied can give the right results (that are totally the same R gives)
josef-pkt commentedon Nov 10, 2022
very good, thanks for the solution
This was my guess in #8506
However, I guess the will still be zero columns if not all nested categories have the same number of levels.
For example if number of dose levels differs by age.