Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""One-way repeated measures ANOVA
"""
import pandas as pd
from numpy import repeat
from pingouin import anova, rm_anova, mixed_anova, print_table
# Load dataset
df = pd.read_csv('sleep_dataset.csv')
# ONE-WAY ANOVA
aov = anova(dv='DV', between='Group', data=df, detailed=False)
print_table(aov, floatfmt=".3f")
# ONE-WAY REPEATED MEASURES ANOVA
aov = rm_anova(dv='DV', within='Time', data=df, correction='auto',
remove_na=True, detailed=True)
print_table(aov)
# TWO-WAY MIXED MODEL ANOVA (Within + Between factors)
aov = mixed_anova(dv='DV', within='Time', between='Group', data=df,
correction='auto', export_filename='mixed_anova.csv')
print_table(aov)
data = data.reset_index().melt(id_vars=targets, value_name=ratings)
# Check that ratings is a numeric variable
assert data[ratings].dtype.kind in 'bfi', 'Ratings must be numeric.'
# Check that data are fully balanced
# This behavior is ensured by the long-to-wide-to-long transformation
# Unbalanced data will result in rows with missing values.
# assert data.groupby(raters)[ratings].count().nunique() == 1
# Extract sizes
k = data[raters].nunique()
n = data[targets].nunique()
# Two-way ANOVA
with np.errstate(invalid='ignore'):
aov = anova(dv=ratings, between=[targets, raters], data=data,
ss_type=2)
# Extract mean squares
msb = aov.at[0, 'MS']
msw = (aov.at[1, 'SS'] + aov.at[2, 'SS']) / (aov.at[1, 'DF'] +
aov.at[2, 'DF'])
msj = aov.at[1, 'MS']
mse = aov.at[2, 'MS']
# Calculate ICCs
icc1 = (msb - msw) / (msb + (k - 1) * msw)
icc2 = (msb - mse) / (msb + (k - 1) * mse + k * (msj - mse) / n)
icc3 = (msb - mse) / (msb + (k - 1) * mse)
icc1k = (msb - msw) / msb
icc2k = (msb - mse) / (msb + (msj - mse) / n)
icc3k = (msb - mse) / msb