Home‎ > ‎Schedule‎ > ‎

### R commands for April 23

 # ONE FACTOR INDEPENDENT MEASURES ANOVA (See Hinton Chapter 11)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) 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 = 30numberofconditions = 3numberofdatapointspercondition = 10# We need to calculate the total sum of squares of the dataSS_total = sum((hinton.anagram - (sum(hinton.anagram)/numberofdatapoints))^2)# We need to calculate the between conditions sum of squaresSS_between = sum((mean(hinton.anagram) - (sum(hinton.anagram)/numberofdatapoints))^2*numberofdatapointspercondition)SS_err = SS_total - SS_betweendf_total = 30 - 1df_between = 3 - 1df_err = df_total - df_betweenMS_between= SS_between/df_betweenMS_error = SS_err/df_errF = MS_between/MS_error1 - pf(F,df_between,df_err)anagram.lm = lm(time ~ condition,data=hinton.anagram.altformat)anova(anagram.lm)# Does this look familiar?anagram_SSres = sum(residuals(anagram.lm)^2)SStot = sum((hinton.anagram.altformat\$variable - mean(hinton.anagram.altformat\$variable))^2)F = (SStot - anagram_SSres / 1) / (anagram_SSres/8)1-pf(F,1,8)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.(See Hinton Chapter 12)pairwise.t.test(hinton.anagram.altformat\$time,hinton.anagram.altformat\$condition,p.adj="bonferroni")TukeyHSD(anagram.aov)# ONE FACTOR REPEATED MEASURES ANOVA (See Hinton Chapter 13)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)hinton.keyboard = data.frame(kb1=c(5,1,0,2),kb2=c(6,2,4,4),kb3=c(10,3,5,6))numberofdatapoints = 12numberofconditions = 3numberofparticipants = 4df_total = numberofdatapoints - 1df_between = numberofconditions - 1df_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)*numberofconditionsSS_error = SS_within_conditions - SS_between_subjsMS_between_conditions= SS_between_conditions/df_betweenMS_error = SS_error/df_errF = MS_between_conditions/MS_error1 - pf(F,df_between,df_err)kb.aov = aov(errors ~ kb + Error(part/kb),data=hinton.keyboard.altformat)#And to relate this back to regression: The following command gives us the same thing!anova(lm(errors ~ part,data=hinton.keyboard.altformat),lm(errors ~ part + kb,data=hinton.keyboard.altformat))pairwise.t.test(hinton.keyboard.altformat\$errors,hinton.keyboard.altformat\$kb,paired=T,p.adj="bonferroni")