I recently posted about my first adventures with R. I have an update for anyone trying to do an ANOVA in R.
R’s aov() function defaults to using Type I Sum of Squares (read more about types of sum of squares here). My conditions have 44 and 45 participants, and although these are close, this is technically unbalanced – Type I Sum of Squares is for balanced designs. I found an oft-recommended-on-the-internet package called ‘ez’ that might be useful for you if you’re conducting ANOVAs. Follow along!
You can read about the ‘ez’ package here on CRAN: http://cran.r-project.org/web/packages/ez/index.html
Here’s what to do! I’ve shared my data if you’re interested in just testing this out. If you haven’t read it yet, see the linked post above to find out what’s in my data file and how you should be formatting your tab-delimited text file if you’re working on your own.
# If you don't have the ez package, install it like this: install.packages("ez") # Load the package into your library library("ez") # Read in the data. R is awesome, because you can read data in with a URL. # h=TRUE is the same as header=TRUE, and lets R know that there are headers. data5to1 <- read.table('http://dl.dropbox.com/u/31803545/differenceScoresForR5to1.txt', h=TRUE) # Next, factorize your stuff. I like to label, but you don't necessarily have to. # Some notes: data5to1 is a dataframe. $condition lets R know I want that # variable from the dataframe. c() combines things into a vector - these # things can be numbers or strings, not unlike [item1 item2] in matlab. data5to1$condition = factor(data5to1$condition, labels = c('equal', 'unequal')) data5to1$block = factor(data5to1$block, labels = c('b1', 'b2', 'b3', 'b4')) data5to1$subject = factor(data5to1$subject) # Next, we use ezANOVA to conduct an ANOVA. Type help(ezANOVA) first to take a look # at the function. (If it can't be found, you haven't loaded library("ez").) # I'll show you what happens when I try to replicate the aov() Type I: ez5to1 <- ezANOVA(data=data5to1, dv= .(score), wid = .(subject), within = .(block), between = .(condition), type=1, detailed = TRUE) # You can read this in help(ezANOVA), but if you're not in R right now, here's what it means: # data : the dataframe I'm using # dv : name of the variable in the dataframe I'm measuring # wid : name of the factor in the dataframe that identifies subjects # within : name of the within-subjects factor (in this case, blocks) # between : name of the between-subjects factor (in this case, condition) # type : Type of SS calculation # detailed : if TRUE, shows you more information about your output # ezANOVA gives me two warnings when I do this. The first warns me that my n per group # is unbalanced. This happens if you call ezANOVA with unbalanced groups. # The second warning happens because I've called type=1, and you should not use this # for unbalanced groups. ezANOVA changes to type=2 automatically. # Take a look at the output: ez5to1 # Effect DFn DFd SSn SSd F p p<.05 ges # 1 (Intercept) 1 87 13.694885 93.64892 12.722570 5.901379e-04 * 0.105695380 # 2 condition 1 87 4.202376 93.64892 3.904014 5.133986e-02 0.034997384 # 3 block 3 261 8.286415 22.22557 32.436428 7.476979e-18 * 0.066739321 # 4 condition:block 3 261 1.034951 22.22557 4.051223 7.727161e-03 * 0.008852588 # # $`Mauchly's Test for Sphericity` # Effect W p p<.05 # 3 block 0.4842223 4.363694e-12 * # 4 condition:block 0.4842223 4.363694e-12 * # # $`Sphericity Corrections` # Effect GGe p[GG] p[GG]<.05 HFe p[HF] p[HF]<.05 # 3 block 0.7152694 1.880476e-13 * 0.73393 9.667764e-14 * # 4 condition:block 0.7152694 1.669995e-02 * 0.73393 1.587206e-02 *
Neat! For my particular dataset, calling type = 2 yields the same results as type = 3, likely because my groups are only off by 1. I hope you found this demonstration useful!