http://personality-project.org/r/r.anova.html (Repeated measures ANOVA) One way ANOVA - within subjects Example 3. One-Way Within-Subjects ANOVA Five subjects are asked to memorize a list of words. The words on this list are of three types: positive words, negative words and neutral words. Their recall data by word type is displayed in Appendix III. Note that there is a single factor (Valence ) with three levels (negative, neutral and positive). In addition, there is also a random factor Subject . Create a data file ex3 that contains this data. Again it is important that each observation appears on an individual line! Note that this is not the standard way of thinking about data. Example 6 will show how to transform data from the standard data table into this form. #Run the analysis: datafilename=http://personality-project.org/r/datasets/R.appendix3.data data.ex3=read.table(datafilename,header=T) #read the data into a table data.ex3 #show the data aov.ex3 = aov(Recall~Valence+Error(Subject/Valence),data.ex3) summary(aov.ex3) print(model.tables(aov.ex3,means),digits=3) #report the means and the number of subjects/cell boxplot(Recall~Valence,data=data.ex3) #graphical output Because Valence is crossed with the random factor Subject (i.e., every subject sees all three types of words), you must specify the error term for Valence , which in this case is Subject by Valence . Do this by adding the termError(Subject/Valence) to the factor Valence , as shown above. The output will look like: datafilename=http://personality-project.org/r/datasets/R.appendix3.data data.ex3=read.table(datafilename,header=T) #read the data into a table data.ex3 #show the data Observation Subject Valence Recall 1 1 Jim Neg 32 2 2 Jim Neu 15 3 3 Jim Pos 45 4 4 Victor Neg 30 5 5 Victor Neu 13 6 6 Victor Pos 40 7 7 Faye Neg 26 8 8 Faye Neu 12 9 9 Faye Pos 42 10 10 Ron Neg 22 11 11 Ron Neu 10 12 12 Ron Pos 38 13 13 Jason Neg 29 14 14 Jason Neu 8 15 15 Jason Pos 35 aov.ex3 = aov(Recall~Valence+Error(Subject/Valence),data.ex3) summary(aov.ex3) Error: Subject Df Sum Sq Mean Sq F value Pr(F) Residuals 4 105.067 26.267 Error: Subject:Valence Df Sum Sq Mean Sq F value Pr(F) Valence 2 2029.73 1014.87 189.11 1.841e-07 *** Residuals 8 42.93 5.37 --- Signif. codes: 0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1 print(model.tables(aov.ex3,means),digits=3) #report the means and the number of subjects/cell Tables of means Grand mean 26.46667 Valence Valence Neg Neu Pos 27.8 11.6 40.0 The analysis of between-subjects factors will appear first (there are none in this case), followed by the within-subjects factors. Note that the p value for Valence is displayed in exponential notation; this occurs when the p value is extremely low, as it is in this case (approximately .00000018). Two-way Within Subjects ANOVA Example 4. Two-Way Within-Subjects ANOVA Appendix IV contains the data from an experiment where five subjects were tested on their recall of words of differing valences. There were two different memory tasks: free or cued recall. Thus, there were 2 independent factors: Valence (3 levels) and Task (2 levels). Again, Subject serves as a random factor. Enter the data into a file entitled ex4 and run the following analysis: In this example, Subject is crossed with both Task and Valence , so you must specify three different error terms: one forTask , one for Valence and one for the interaction between the two. Fortunately, R is smart enough to divide up the within-subjects error term properly as long as you specify it in your command. The commands are: datafilename=http://personality-project.org/r/datasets/R.appendix4.data data.ex4=read.table(datafilename,header=T) #read the data into a table data.ex4 #show the data aov.ex4=aov(Recall~(Task*Valence)+Error(Subject/(Task*Valence)),data.ex4 ) summary(aov.ex4) print(model.tables(aov.ex4,means),digits=3) #report the means and the number of subjects/cell boxplot(Recall~Task*Valence,data=data.ex4) #graphical summary of means of the 6 cells results in the following output datafilename=http://personality-project.org/r/datasets/R.appendix4.data data.example4=read.table(datafilename,header=T) #read the data into a table data.example4 #show the data Observation Subject Task Valence Recall 1 1 Jim Free Neg 8 2 2 Jim Free Neu 9 3 3 Jim Free Pos 5 4 4 Jim Cued Neg 7 5 5 Jim Cued Neu 9 6 6 Jim Cued Pos 10 7 7 Victor Free Neg 12 8 8 Victor Free Neu 13 9 9 Victor Free Pos 14 10 10 Victor Cued Neg 16 11 11 Victor Cued Neu 13 12 12 Victor Cued Pos 14 13 13 Faye Free Neg 13 14 14 Faye Free Neu 13 15 15 Faye Free Pos 12 16 16 Faye Cued Neg 15 17 17 Faye Cued Neu 16 18 18 Faye Cued Pos 14 19 19 Ron Free Neg 12 20 20 Ron Free Neu 14 21 21 Ron Free Pos 15 22 22 Ron Cued Neg 17 23 23 Ron Cued Neu 18 24 24 Ron Cued Pos 20 25 25 Jason Free Neg 6 26 26 Jason Free Neu 7 27 27 Jason Free Pos 9 28 28 Jason Cued Neg 4 29 29 Jason Cued Neu 9 30 30 Jason Cued Pos 10 aov.ex4=aov(Recall~(Task*Valence)+Error(Subject/(Task*Valence)),data.example4 ) summary(aov.ex4) Error: Subject Df Sum Sq Mean Sq F value Pr(F) Residuals 4 349.13 87.28 Error: Subject:Task Df Sum Sq Mean Sq F value Pr(F) Task 1 30.0000 30.0000 7.3469 0.05351 . Residuals 4 16.3333 4.0833 --- Signif. codes: 0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1 Error: Subject:Valence Df Sum Sq Mean Sq F value Pr(F) Valence 2 9.8000 4.9000 1.4591 0.2883 Residuals 8 26.8667 3.3583 Error: Subject:Task:Valence Df Sum Sq Mean Sq F value Pr(F) Task:Valence 2 1.4000 0.7000 0.2907 0.7553 Residuals 8 19.2667 2.4083 print(model.tables(aov.ex4,means),digits=3) #report the means and the number of subjects/cell Tables of means Grand mean 11.8 Task Cued Free 12.8 10.8 rep 15.0 15.0 Valence Neg Neu Pos 11 12.1 12.3 rep 10 10.0 10.0 Task:Valence Valence Task Neg Neu Pos Cued 11.8 13.0 13.6 rep 5.0 5.0 5.0 Free 10.2 11.2 11.0 rep 5.0 5.0 5.0 ------------------------------------------------------------- # Note 这里用的模型是: aov.ex4=aov(Recall~(Task*Valence)+Error(Subject/(Task*Valence)),data.example4 ) 而上篇博文中用的是: aov.out = aov(SSS ~ diet * test + Error(subject/test), data=hill) http://blog.sciencenet.cn/home.php?mod=spaceuid=285393do=blogid=672361 区别在与第一个模型中,两个变量都基于重复测量。即每个个体都经历过各种组合的处理。 而第二个模型中,一部分个体经过diet中的 chicken ,而另一部分个体经过diet中的 chicken 的 pasta , 所以diet不是within变量。
http://www.r-statistics.com/2010/04/repeated-measures-anova-with-r-tutorials/ Repeated measures ANOVA is a common task for the data analyst. There are (at least) two ways of performing “repeated measures ANOVA” using R but none is really trivial, and each way has it’s own complication/pitfalls (explanation/solution to which I was usually able to find through searching in the R-help mailing list). So for future reference, I am starting this page to document links I find to tutorials, explanations (and troubleshooting) of “repeated measure ANOVA” done with R Functions and packages (I suggest using the tutorials supplied bellow for how to use these functions) aov {stats} – offers SS type I repeated measures anova, by a call to lm for each stratum. A short example is given in the ?aov help file Anova { car } – Calculates type-II or type-III analysis-of-variance tables for model objects produced by lm, and for various other object. The ?Anova help file offers an example for how to use this for repeated measures ezANOVA { ez } – This function provides easy analysis of data from factorial experiments, including purely within-Ss designs (a.k.a. “repeated measures”), purely between-Ss designs, and mixed within-and-between-Ss designs, yielding ANOVA results and assumption checks. It is a wrapper of the Anova {car} function, and is easier to use. The ez package also offers the functions ezPlot and ezStats to give plot and statistics of the ANOVA analysis. The ?ezANOVA help file gives a good demonstration for the functions use (My thanks goes to Matthew Finkbe for letting me know about this cool package) friedman.test {stats} – Performs a Friedman rank sum test with unreplicated blocked data. That is, a non-parametric one-way repeated measures anova. I also wrote a wrapper function to perform and plot a post-hoc analysis on the friedman test results Non parametric multi way repeated measures anova – I believe such a function could be developed based on the Proportional Odds Model, maybe using the {repolr} or the {ordinal} packages. But I still didn’t come across any function that implements these models (if you do – please let me know in the comments). Repeated measures, non-parametric, multivariate analysis of variance – as far as I know, such a method is not currently available in R. There is, however, the Analysis of similarities (ANOSIM) analysis which provides a way to test statistically whether there is a significantdifference between two or more groups of sampling units. Is is available in the { vegan } package through the “anosim” function. There is also a tutorial and a relevant published paper . Good Tutorials A basic tutorial about ANOVA with R (only the last bit holds some example of repeated measures) on personality-project A thorough tutorial on motor control lab A thorough tutorial on UCLA seminar page Another good tutorial by Jonathan Baron and Yuelin Li on “Notes on the use of R for psychology experiments and questionnaires” Troubelshooting Unbalanced design Unbalanced design doesn’t work when doing repeated measures ANOVA with aov, it just doesn’t. This situation occurs if there are missing values in the data or that the data is not from a fully balanced design. The way this will show up in your output is that you will see the between subject section showing withing subject variables. A solution for this might be to use the Anova function from library car with parameter type=”III”. But before doing that, first make sure you understand the difference between SS type I, II and III. Here is a good tutorial for helping you out with that. By the way, these links are also useful in case you want to do a simple two way ANOVA for unbalanced design I will “later” add R-help mailing list discussions that I found helpful on the subject. If you come across good resources, please let me know about them in the comments.