//One-Way Repeated Measures ANOVA Example //Created by John M. Quick //http://www.johnmquick.com //January 20, 2011 > #read the dataset into an R variable using the read.csv(file) function > dataOneWayRepeatedMeasures <- read.csv("dataset_ANOVA_OneWayRepeatedMeasures.csv") > #display the data > #notice the atypical column arrangement for repeated measures data > dataOneWayRepeatedMeasures Subject Interest10 Interest15 Interest20 1 1 1 1 3 2 2 1 1 3 3 3 1 1 3 4 4 1 1 3 5 5 1 1 3 6 6 1 2 4 7 7 1 2 5 8 8 1 2 4 9 9 1 2 5 10 10 1 2 4 11 11 2 3 5 12 12 2 3 4 13 13 2 3 5 14 14 3 4 4 15 15 3 4 5 16 16 1 1 3 17 17 1 1 2 18 18 1 1 3 19 19 1 1 2 20 20 1 1 3 21 21 1 2 4 22 22 1 2 3 23 23 1 2 4 24 24 1 2 3 25 25 1 2 4 26 26 2 3 5 27 27 2 3 4 28 28 2 3 5 29 29 2 3 4 30 30 2 3 5 > #prepare the repeated measures factor > #step 1: define the levels > #use c() to create a vector containing the number of levels within the repeated measures factor > #create a vector numbering the levels for our three voting interest age groups > ageLevels <- c(1, 2, 3) > #step 2: define the factor > #use as.factor() to create a factor using the levels from step 1 > #convert the age levels into a factor > ageFactor <- as.factor(ageLevels) > #step 3: define the frame > #use data.frame() to create a data frame using the factor from step 2 > #convert the age factor into a data frame > ageFrame <- data.frame(ageFactor) > #step 4: bind the columns > #use cbind() to bind the levels of the factor from the original dataset > #bind the age columns > ageBind <- cbind(dataOneWayRepeatedMeasures$Interest10, dataOneWayRepeatedMeasures$Interest15, dataOneWayRepeatedMeasures$Interest20) > #step 5: define the model > #use lm() to generate a linear model using the bound factor levels from step 4 > #generate a linear model using the bound age levels > ageModel <- lm(ageBind ~ 1) > #load the car package (install first, if necessary) > library(car) > #compose the Anova(mod, idata, idesign) function > analysis <- Anova(ageModel, idata = ageFrame, idesign = ~ageFactor) > #use summary(object) to visualize the results of the repeated measures ANOVA > summary(analysis) Type III Repeated Measures MANOVA Tests: ------------------------------------------ Term: (Intercept) Response transformation matrix: (Intercept) [1,] 1 [2,] 1 [3,] 1 Sum of squares and products for the hypothesis: (Intercept) (Intercept) 1584.133 Sum of squares and products for error: (Intercept) (Intercept) 149.8667 Multivariate Tests: (Intercept) Df test stat approx F num Df den Df Pr(>F) Pillai 1 0.913572 306.5383 1 29 < 2.22e-16 *** Wilks 1 0.086428 306.5383 1 29 < 2.22e-16 *** Hotelling-Lawley 1 10.570285 306.5383 1 29 < 2.22e-16 *** Roy 1 10.570285 306.5383 1 29 < 2.22e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ------------------------------------------ Term: ageFactor Response transformation matrix: ageFactor1 ageFactor2 [1,] 1 0 [2,] 0 1 [3,] -1 -1 Sum of squares and products for the hypothesis: ageFactor1 ageFactor2 ageFactor1 172.8 124.80000 ageFactor2 124.8 90.13333 Sum of squares and products for error: ageFactor1 ageFactor2 ageFactor1 17.2 11.20000 ageFactor2 11.2 11.86667 Multivariate Tests: ageFactor Df test stat approx F num Df den Df Pr(>F) Pillai 1 0.911011 143.3220 2 28 1.9532e-15 *** Wilks 1 0.088989 143.3220 2 28 1.9532e-15 *** Hotelling-Lawley 1 10.237288 143.3220 2 28 1.9532e-15 *** Roy 1 10.237288 143.3220 2 28 1.9532e-15 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Univariate Type III Repeated-Measures ANOVA Assuming Sphericity SS num Df Error SS den Df F Pr(>F) (Intercept) 528.04 1 49.956 29 306.54 < 2.2e-16 *** ageFactor 92.09 2 11.911 58 224.21 < 2.2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Mauchly Tests for Sphericity Test statistic p-value ageFactor 0.73931 0.014573 Greenhouse-Geisser and Huynh-Feldt Corrections for Departure from Sphericity GG eps Pr(>F[GG]) ageFactor 0.79321 < 2.2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 HF eps Pr(>F[HF]) ageFactor 0.83157 < 2.2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1