Home‎ > ‎Schedule‎ > ‎

### R Commands for April 24

 `# ONE FACTOR INDEPENDENT MEASURES ANOVA``hinton.anagram.altformat = data.frame(time=c(15,20,14,13,18,16,13,12,18,11,21,25,29,18,26,22,26,24,28,21,28,30,32,``28,26,30,25,36,20,25),condition=c("first","first","first","first","first","first",``"first","first","first","first","last","last","last","last","last","last","last",``"last","last","last","none","none","none","none","none","none","none","none","none",``"none"))``bwplot(time ~ condition,data=hinton.anagram.altformat) ``# To work through this we'll use a slightly different format....``hinton.anagram = data.frame(``first.letter=c(15,20,14,13,18,16,13,12,18,11),``last.letter=c(21,25,29,18,26,22,26,24,28,21),``no.letter= c(28,30,32,28,26,30,25,36,20,25))``numberofdatapoints = 30``numberofconditions = 3``numberofdatapointspercondition = 10``# We need to calculate the total sum of squares of the data``SS_total = sum((hinton.anagram - (sum(hinton.anagram)/numberofdatapoints))^2)``# We need to calculate the between conditions sum of squares``SS_between = sum((mean(hinton.anagram) - (sum(hinton.anagram)/numberofdatapoints))^2*numberofdatapointspercondition)``# We can then use this to calculate to SS of the error``SS_err = SS_total - SS_between``# We need to consider the degrees of freedom. We divide by the df to give the "mean``# squares" in each case.``df_total = 30 - 1``df_between = 3 - 1``df_err = df_total - df_between``MS_between= SS_between/df_between``MS_error = SS_err/df_err``F = MS_between/MS_error``1 - pf(F,df_between,df_err)``# We can do this in R without all the steps``anagram.lm = lm(time ~ condition,data=hinton.anagram.altformat)``anova(anagram.lm)``# Wait! This look familiar! Yes it is exactly what we were doing for model comparison.``# There we simply calculated the SS of the error directly from the model residuals.``anagram_SSres = sum(residuals(anagram.lm)^2)``SStot = sum((hinton.anagram.altformat\$time - mean(hinton.anagram.altformat\$time))^2)``F = (SStot - anagram_SSres / 1) / (anagram_SSres/8)``1-pf(F,1,8)``# An alternative syntax``anagram.aov = aov(time ~ condition,data=hinton.anagram.altformat)``summary(anagram.aov)``# Anova tells you that the factor explains more variance that expected by chance, but``# `it doesn't tell you where the differences lie. For that we need other tests.`pairwise.t.test(hinton.anagram.altformat\$time,hinton.anagram.altformat\$condition,``p.adj="bonferroni")``TukeyHSD(anagram.aov)``###``# ONE FACTOR REPEATED MEASURES ANOVA``####``hinton.keyboard.altformat = data.frame(errors=c(5,1,0,2,6,2,4,4,10,3,5,6),kb=c("one","one","one","one","two","two",``"two","two","three","three","three","three"),part=c("a","b","c","d","a","b","c","d",``"a","b","c","d"))``bwplot(errors ~ as.factor(kb),data=hinton.keyboard.altformat)``bwplot(errors ~ as.factor(kb)|part,data=hinton.keyboard.altformat)``#Again we'll use a slightly different format to work through this``hinton.keyboard = data.frame(``kb1=c(5,1,0,2),kb2=c(6,2,4,4),kb3=c(10,3,5,6)``)``numberofdatapoints = 12``numberofconditions = 3``numberofparticipants = 4``df_total = numberofdatapoints - 1``df_between = numberofconditions - 1``df_err = (numberofparticipants-1)*(numberofconditions-1)``SS_total = sum((hinton.keyboard - (sum(hinton.keyboard)/numberofdatapoints))^2)``SS_between_conditions = sum((mean(hinton.keyboard) - (sum(hinton.keyboard)/numberofdatapoints))^2*numberofparticipants)``SS_within_conditions = SS_total - SS_between_conditions ``SS_between_subjs = sum((rowMeans(hinton.keyboard) - mean(mean(hinton.keyboard)))^2)*numberofconditions``SS_error = SS_within_conditions - SS_between_subjs``MS_between_conditions= SS_between_conditions/df_between``MS_error = SS_error/df_err``F = MS_between_conditions/MS_error``1 - pf(F,df_between,df_err)``# We can do this without all of the steps...``kb.aov = aov(errors ~ kb + Error(part/kb),data=hinton.keyboard.altformat)``#And to relate this back to regression again....The following command gives us the ``# same thing!``anova(lm(errors ~ part,data=hinton.keyboard.altformat),lm(errors ~ part + kb,data=hinton.keyboard.altformat))``#And again we need additional tests to locate the differences..``pairwise.t.test(hinton.keyboard.altformat\$errors,hinton.keyboard.altformat\$kb,paired=T,``p.adj="bonferroni")``# You can add factors and interactions between factors to the ANOVA in the same way you``# added them to the regression models``# For example, if you had an additional factor font color in the anagram task, you ``# could do the following:``hinton.anagram.altformat2 = data.frame(time=c(15,20,14,13,18,16,13,12,18,11,21,25,29,18,26,22,26,24,28,21,28,30,32,``28,26,30,25,36,20,25),condition=c("first","first","first","first","first","first",``"first","first","first","first","last","last","last","last","last","last","last",``"last","last","last","none","none","none","none","none","none","none","none","none",``"none"),color=c("blue","blue","blue","blue","blue","red","red","red","red","red",``"blue","blue","blue","blue","blue","red","red","red","red","red","blue","blue","blue",``"blue","blue","red","red","red","red","red"))``#For a simple two factor analysis:``anagram.2factor.aov = aov(time ~ condition + color,data=hinton.anagram.altformat2)``# For a two factor analysis with interaction``anagram.2factorwithinteraction.aov = aov(time ~ condition * color,data=hinton.anagram.altformat2)``# For repeated measures multi-factor, you just do the same and then add the multiple``# factors to the error partitioning, so if you were to have had an additional factor``# time_of_day in the keyboard data you could have written ``# aov(errors ~ kb + time_of_day + ``# `Error(part/(kb+time_of_day)),data=hinton.keyboard.altformat)