-4

i have 4 classes of size (72,22,22) and i want to do anova1 for each pair of the (22,22) in those classes . for example; i wanna grab, let's say, pair (2,3) in each class and do the anova for each pair in the 4 arrays, so the output would be an array of (22,22)representing P-values of each pair across 4 classes. hopefullay my code explains what i am trying to say :) i have tried this piece of code but im not sure cuz the results are kinda odd; i guess the issue is in 2 for loops. here is the code here is my code :



x = load("plv_8_12.mat");

x = x.plv;

size(x)

ans =
        2592          22          22

S1C1 =x(1:72,:,:);

S1C2 = x(649:720,:,:);

S1C3 = x(1297:1368,:,:);

S1C4 = x(1945:2016,:,:);

p_all = [];


for x =1:22

    for y=1:22
        
        tc1 = S1C1(:,x,y);
        tc2 = S1C2(:,x,y);
        tc3 = S1C3(:,x,y);
        tc4 = S1C4(:,x,y);
        temp = [tc1 tc2 tc3 tc4];
        
        %p_all(x,y) = anova1(temp);
        [p,tbl,stats]=anova1(temp);
        
        close all
    end
end
Abdul
  • 1
  • 3
  • Please, explain what is "classes"? What is (72,22,22) - a 3-dimentional array with such a shape? If yes, why the dimension with the size of 72 disappear? – Alex Sep 12 '22 at 01:08
  • 1
    [ask] and [mre] – Julien Sep 12 '22 at 01:09
  • classes are kinda groups ( 4 groups) (72,22,22) is the dimension for each group – Abdul Sep 12 '22 at 01:11
  • It's confusing: does "pair (2,3)" mean one taken from "class" 2 and one from "class 3"? – Alex Sep 12 '22 at 01:15
  • Or 2 and 3 are taken along the 0-axis (which has the length of 72) in each "class"? – Alex Sep 12 '22 at 01:15
  • 1
    yes, along the 0-axis (which has the length of 72) – Abdul Sep 12 '22 at 01:22
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 12 '22 at 04:35

1 Answers1

0

Since the observations are structured in an array, there is a strong suspicion that they are related between each element (z, x, y) in each "class", i.e. paired. If it is the case, the regular ANOVA is a non-valid test, and one may need the repeated measures ANOVA (much slower in the code below). For the regular ANOVA, all observations need to be independent from the other group. I don't have any mat files, so I can't verify the first part of the code. If it doesn't work, please let me know.

import numpy as np
import pandas as pd
import scipy.io as sio
from scipy.stats import f_oneway
from statsmodels.stats.anova import AnovaRM

mat_contents = sio.loadmat("plv_8_12.mat")
data = sorted(mat_contents.keys())[-1]
data = mat_contents[data]
print(data.shape)

#data = np.random.normal(loc=0, scale=1, size=(2016, 22, 22))

S1C1 = data[0:72,:,:]
S1C2 = data[648:720,:,:]
S1C3 = data[1296:1368,:,:]
S1C4 = data[1944:2016,:,:]

p_all_paired = np.zeros((22,22))
p_all_indipendent = np.zeros((22,22))

for x in range(data.shape[1]):
    for y in range(data.shape[1]):
        df = pd.DataFrame({'ID': np.arange(72), 'S1C1': S1C1[:,x,y], 'S1C2': S1C2[:,x,y], 'S1C3': S1C3[:,x,y], 'S1C4': S1C4[:,x,y]}).melt(id_vars='ID', var_name='Class', value_name='Measurement')
        p_all_indipendent[x,y] = f_oneway(S1C1[:,x,y], S1C2[:,x,y], S1C3[:,x,y], S1C4[:,x,y])[1]
        p_all_paired[x,y] = AnovaRM(df, 'Measurement', 'ID', ['Class']).fit().anova_table.loc['Class','Pr > F']
        
print("Unpaired p-values")
print(p_all_indipendent)
print("\n\nPaired p-values")
print(p_all_paired)
Alex
  • 707
  • 1
  • 4
  • 9
  • thank you so much, Alex. the code is working perfectly. but the p_values are significantly different, in fact, all values are way below 0.05 " ANOVA is a non-valid test" so what do you suggest – Abdul Sep 12 '22 at 02:18
  • The repeated measures ANOVA. I added it to the code above. Moreover, it may be worth checking the sample for normality. If that assumption is violated, ANOVA is really bad. – Alex Sep 12 '22 at 02:55
  • all p-values can be below 0.05 if at least of "class" is very very different. – Alex Sep 12 '22 at 02:57