- by Jimmy Fisher
- Oct 19, 2024
# ANOVA Example import pandas as pd import statsmodels.api as sm from statsmodels.formula.api import ols # Sample data for ANOVA data = {'Group': ['A', 'A', 'B', 'B', 'C', 'C'], 'Score': [23, 26, 27, 22, 24, 28]} df = pd.DataFrame(data) # Fit the model for ANOVA model = ols('Score ~ C(Group)', data=df).fit() anova_result = sm.stats.anova_lm(model, typ=2) print(anova_result) # MANOVA Example from statsmodels.multivariate.manova import MANOVA # Sample data for MANOVA data = {'Group': ['A', 'A', 'B', 'B', 'C', 'C'], 'Metric1': [23, 26, 27, 22, 24, 28], 'Metric2': [15, 18, 19, 14, 16, 20]} df = pd.DataFrame(data) # Fit the MANOVA model manova = MANOVA.from_formula('Metric1 + Metric2 ~ C(Group)', data=df) manova_result = manova.mv_test() print(manova_result)
In the provided code, ANOVA is used to compare one dependent variable ('Score') across different groups. In contrast, MANOVA evaluates two dependent variables ('Metric1' and 'Metric2') to ascertain the impact of group differences while considering correlations between the dependent variables. These statistical techniques are widely used in fields like psychology, biology, and social sciences, offering invaluable insights into how various independent variables influence outcomes.
# Load necessary libraries library(dplyr) # For data manipulation library(car) # For Anova (Type II) library(stats) # For aov library(MASS) # For MANOVA # ANOVA Example # Sample data for ANOVA data_anova <- data.frame(Group = factor(c('A', 'A', 'B', 'B', 'C', 'C')), Score = c(23, 26, 27, 22, 24, 28)) # Fit the model for ANOVA anova_model <- aov(Score ~ Group, data = data_anova) anova_result <- Anova(anova_model, type = "II") # Type II ANOVA print(summary(anova_result)) # MANOVA Example # Sample data for MANOVA data_manova <- data.frame(Group = factor(c('A', 'A', 'B', 'B', 'C', 'C')), Metric1 = c(23, 26, 27, 22, 24, 28), Metric2 = c(15, 18, 19, 14, 16, 20)) # Fit the MANOVA model manova_model <- manova(cbind(Metric1, Metric2) ~ Group, data = data_manova) manova_result <- summary(manova_model, test = "Pillai") # Pillai's trace test print(manova_result)
MANOVA and ANOVA compare variance between groups and are important statistical methods to understand. For more detail on how this approach works, check out the following: