Skip to content

Nested ANOVA #8336

Open
Open

Description

@kseniawa

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_table

Снимок экрана от 2022-07-01 15-13-30

The 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))

The results:
Снимок экрана от 2022-07-01 15-22-44

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

  1. josef-pkt commented on Sep 26, 2022

    @josef-pkt
    Member

    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

  2. kseniawa commented on Nov 10, 2022

    @kseniawa
    Author

    I found out how to get right results using statsmodels in the example. We should:

    1. 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).

    2. note that age factor as a categorical one, so use C(age) in the formula: C(age)/C(dose) or even C(age)/dose

    mod = ols('expr~C(age) + C(age):C(dose)', data=data_new).fit()
    
    anova_table = sm.stats.anova_lm(mod, typ=1); anova_table
    

    Снимок экрана от 2022-11-10 13-15-13

    Only both conditions applied can give the right results (that are totally the same R gives)

  3. josef-pkt commented on Nov 10, 2022

    @josef-pkt
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions