//Two-Way ANOVA with Pairwise Comparisons Example //Created by John M. Quick //http://www.johnmquick.com //January 16, 2011 > #read the dataset into an R variable using the read.csv(file) function > dataTwoWayComparisons <- read.csv("dataset_ANOVA_TwoWayComparisons.csv") > #display the data > dataTwoWayComparisons Treatment Age StressReduction 1 mental young 10 2 mental young 9 3 mental young 8 4 mental mid 7 5 mental mid 6 6 mental mid 5 7 mental old 4 8 mental old 3 9 mental old 2 10 physical young 9 11 physical young 8 12 physical young 7 13 physical mid 6 14 physical mid 5 15 physical mid 4 16 physical old 3 17 physical old 2 18 physical old 1 19 medical young 8 20 medical young 7 21 medical young 6 22 medical mid 5 23 medical mid 4 24 medical mid 3 25 medical old 2 26 medical old 1 27 medical old 0 > #use anova(object) to test the omnibus hypothesis > #Are main effects or interaction effects present in the independent variables? > anova(lm(StressReduction ~ Treatment * Age, dataTwoWayComparisons)) Analysis of Variance Table Response: StressReduction Df Sum Sq Mean Sq F value Pr(>F) Treatment 2 18 9 9 0.001953 ** Age 2 162 81 81 1e-09 *** Treatment:Age 4 0 0 2.250e-30 1.000000 Residuals 18 18 1 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 > #omnibus test significant with no interaction effect; continue to pairwise comparisons > #use pairwise.t.test(x, g, p.adj) to test the pairwise comparisons between the treatment group means > #What significant differences are present amongst the treatment means? > pairwise.t.test(dataTwoWayComparisons$StressReduction, dataTwoWayComparisons$Treatment, p.adj = "none") Pairwise comparisons using t tests with pooled SD data: dataTwoWayComparisons$StressReduction and dataTwoWayComparisons$Treatment medical mental mental 0.13 - physical 0.45 0.45 P value adjustment method: none > #use pairwise.t.test(x, g, p.adj) to test the pairwise comparisons between the age group means > #What significant differences are present amongst the age group means? > pairwise.t.test(dataTwoWayComparisons$StressReduction, dataTwoWayComparisons$Age, p.adj = "none") Pairwise comparisons using t tests with pooled SD data: dataTwoWayComparisons$StressReduction and dataTwoWayComparisons$Age mid old old 2.5e-05 - young 2.5e-05 2.3e-10 P value adjustment method: none > #no statistically significant differences exist between the treatment group means > #all of the age group means are significantly different from one another > #additional information > #treatment group means derived using subset(data, condition) > mean(subset(dataTwoWayComparisons$StressReduction, dataTwoWayComparisons $Treatment == "mental")) 6 > mean(subset(dataTwoWayComparisons $StressReduction, dataTwoWayComparisons $Treatment == "physical")) 5 > mean(subset(dataTwoWayComparisons $StressReduction, dataTwoWayComparisons $Treatment == "medical")) 4 > #age group means derived using subset(data, condition) > mean(subset(dataTwoWayComparisons $StressReduction, dataTwoWayComparisons $Age == "young")) 8 > mean(subset(dataTwoWayComparisons $StressReduction, dataTwoWayComparisons $Age == "mid")) 5 > mean(subset(dataTwoWayComparisons $StressReduction, dataTwoWayComparisons $Age == "old")) 2