Accidentally posted this in stackoverflow, so reposting here. I have a repeated-measures design experiment. Each subject took part in some experimental conditions, each one associated with several events. Normally I would analyse this with an ANOVA by grouping across all events, but I want to include those individual events in the model, and also account for subject effects. Example data
import numpy as np
import pandas as pd
condition = ['A']*3 + ['B']*3 + ['C']*3
events = list(range(1, len(condition)+1))
subject = [1]*len(events)
data = {
'subject':subject+[i+1 for i in subject]+[i+2 for i in subject],
'events':events*3,
'condition':condition*3
}
df = pd.DataFrame(data)
np.random.seed(1)
df['y'] = [np.random.randint(0,2) for i in range(df.shape[0])]
print(df)
print(df.groupby(['subject', 'condition'], as_index=False)['y'].mean())
So the full data:
subject events condition y
1 1 A 1
1 2 A 1
1 3 A 0
1 4 B 0
1 5 B 1
1 6 B 1
1 7 C 1
1 8 C 1
1 9 C 1
2 1 A 0
2 2 A 0
2 3 A 1
2 4 B 0
2 5 B 1
2 6 B 1
2 7 C 0
2 8 C 0
2 9 C 1
3 1 A 0
3 2 A 0
3 3 A 0
3 4 B 1
3 5 B 0
3 6 B 0
3 7 C 1
3 8 C 0
3 9 C 0
And the grouped data:
subject condition y
1 A 0.666666667
1 B 0.666666667
1 C 1
2 A 0.333333333
2 B 0.666666667
2 C 0.333333333
3 A 0
3 B 0.333333333
3 C 0.333333333
I would like to know how to do a multi-level (logistic regression?) model to account for events and subjects as well as the condition as factors. It's a bit strange to me because when we group together and analyse the means we can use a standard ANOVA, but when we retain the full dataset and analyse it the outcome measure is of course binary, meaning we should use some sort of logistic regression. Is this correct? And if so, is this possible given the within-subjects design? If so, how do we achieve this in python (or R)?