//One-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 > dataOneWayComparisons <- read.csv("dataset_ANOVA_OneWayComparisons.csv") > #display the data > dataOneWayComparisons Treatment StressReduction 1 mental 2 2 mental 2 3 mental 3 4 mental 4 5 mental 4 6 mental 5 7 mental 3 8 mental 4 9 mental 4 10 mental 4 11 physical 4 12 physical 4 13 physical 3 14 physical 5 15 physical 4 16 physical 1 17 physical 1 18 physical 2 19 physical 3 20 physical 3 21 medical 1 22 medical 2 23 medical 2 24 medical 2 25 medical 3 26 medical 2 27 medical 3 28 medical 1 29 medical 3 30 medical 1 > #use anova(object) to test the omnibus hypothesis > #Is there a significant difference amongst the treatment means? > anova(lm(StressReduction ~ Treatment, dataOneWayComparisons)) Analysis of Variance Table Response: StressReduction Df Sum Sq Mean Sq F value Pr(>F) Treatment 2 11.667 5.8333 5.1639 0.01262 * Residuals 27 30.500 1.1296 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > #omnibus test significant; 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? > #note that no Type I error adjustment is used in this example and an alpha level of .05 is assumed > pairwise.t.test(dataOneWayComparisons$StressReduction, dataOneWayComparisons$Treatment, p.adj = "none") Pairwise comparisons using t tests with pooled SD data: dataOneWayComparisons$StressReduction and dataOneWayComparisons$Treatment medical mental mental 0.0039 - physical 0.0448 0.3022 P value adjustment method: none > #significant differences exist between the mental-medical and physical-medical, but not the mental-physical treatments > #additional information > #treatment group means derived using subset(data, condition) > mean(subset(dataOneWayComparisons$StressReduction, dataOneWayComparisons$Treatment == "mental")) [1] 3.5 > mean(subset(dataOneWayComparisons$StressReduction, dataOneWayComparisons$Treatment == "physical")) [1] 3 > mean(subset(dataOneWayComparisons$StressReduction, dataOneWayComparisons$Treatment == "medical")) [1] 2