View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Repeated Measures ANOVA

5.0 | 11 ratings Rate this file 63 Downloads (last 30 days) File Size: 2.77 KB File ID: #22088 Version: 1.0

Repeated Measures ANOVA



13 Nov 2008 (Updated )

Single factor repeated measures ANOVA for one or more samples.

| Watch this File

File Information

******** Warning *********
This program was originally released when MATLAB had no support for repeated measures ANOVA. However, since a few releases ago, MATLAB statistics toolbox has added this functionality (see the fitrm function). Thus this program is now deprecated and is not recommended anymore. The issue is that it only support a very small subclass of the problems that fitrm can solve. Also, it might not have been tested as extensively as fitrm so it is possible that it does not produce correct results in all cases.
I keep the program as it is here but it will not be maintained any more.

[p, table] = anova_rm(X, displayopt) performs a repeated measures ANOVA for comparing the means of two or more columns (time) in one or more samples(groups). Unbalanced samples (i.e. different number of subjects per group) is supported though the number of columns (followups) should be the same.

Required Products Statistics and Machine Learning Toolbox
MATLAB release MATLAB 7.4 (R2007a)
MATLAB Search Path
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (27)
16 Aug 2016 Rodrigo Martín

13 May 2016 Andy Kay

03 Aug 2015 CécileI


I am not able to obtain the F-value, i obtain "NaN" although my matrix do not contain any NaN. How can I fix this problem ?

Comment only
30 Apr 2015 Shazux Gharasoo

18 Mar 2015 Félix Chénier

Very useful ! Thanks a lot.

28 Dec 2014 Nickless


Comment only
28 Dec 2014 Nickless

07 Nov 2014 David Saxby

@ Pegah Hosseini,

Tidy, very tidy.



Comment only
11 Jun 2014 Pegah Hosseini

Well, I couldn't find that specific website but here is another one. Please let me know if you found any cite-able references for this purpose.

Comment only
11 Jun 2014 Pegah Hosseini

As I had this problem and took me a while to figure it out, I thought it may be useful to someone else.
For anova_rm to be compatible with multcompare function of MATLAB, we need a stat output. I read somewhere (I printed out the webpage about a year ago but I can't find it anymore) that the post-hoc for repeated measures anova is the same as independent measures anova. You just need to replace MS(within) with MS(error) and df(within) with df(error) and then use Tuckey's equation to find the critical value. Thus, as I work with anova_rm as a one-way measure, I added the following lines at the end of anova_rm and added "stat" variable to the output of the function. I can now feed this stat to multcompare and the function sees stat as the output of anova1 and estimates the critical value using Tuckey (if that is your selected ctype) in the same way it does for anova1. As I don't use it for two-way measures, I haven't checked how to define stat for that purpose but I am sure it shouldn't be hard now.


Comment only
22 Apr 2014 Chloe

Chloe (view profile)

I wonder that how can I interpret the subject(matching) ?

Comment only
26 Nov 2013 Arash Salarian

@Dan I cannot help you much with that in MATLAB. These days I mostly use R for anything beyond basic statistics. For your reference, here is a Q/A about something what I believe is exactly similar to your situation:

Comment only
26 Nov 2013 Dan

Dan (view profile)

That's very good but I'd like to run a post-hoc analysis after the rm-anova. I'm currently running a two-way rm anova and I'd like to know the differences between all combinations. Is their a way to do so with multcompare?

21 Nov 2013 Arash Salarian

@Patrizia your problem with NaN and in general, missing data is beyond the scope of this code. Generally this is handled separately through what we call imputation methods. They range from very basic (and unsatisfactory) approaches like removing incomplete cases to more advanced approaches like multiple imputations. However, the point is that there is no single, universally best approach for the missing data problem.

Comment only
21 Nov 2013 Arash Salarian

@J The main difference between repeated measures and the n-way ANOVA is that the former can model within subject variations while the latter can only model between subject variations. Essentially repeated measures ANOVA is a small subset of linear mixed models. In general you cannot model mixed models with simple, n-way ANOVA. In SPSS as well as the newest version of MATLAB linear mixed modeling is supported.

Comment only
20 Nov 2013 J

J (view profile)

I'm trying to understand something related to a one-way repeated measures ANOVA.

I have one group and two scores over time. In SPSS (and this code) you input every subject's score on one row with each column being the next measurement in time. If I use anovan (built in RM analysis within Matlab) I can do the same thing but have to instead put all data sequentially in a line (row or column does not matter as long as every subject's score follows each other) and create a file the same length as my data file with the condition order.

This doesn't actually change anything a part from the df (which are doubled in anovan since I spread the data in 1 line rather than 2 equal columns) however it also gives different results in the p value (nothing drastic but still different).

Any ideas why anovan and this code producing different results while this code and SPSS produce the same results? THANKS!

04 Nov 2013 Patrizia

Hi, thank you for your script. I have problems in to manage the NaN cells. Ideas? Thanks a lot

Comment only
27 Sep 2013 Amber Tyler

I keep getting the follow error:
Undefined function or variable 'displayopt'.

I have have 3x10 array.

Comment only
22 May 2013 Marc

Marc (view profile)


I keep getting an errors when I run your file.

??? patients =
Error: Expression or statement is incomplete or incorrect.

Any suggestions on troubleshooting?

Comment only
03 Dec 2012 ted p teng

ted p teng (view profile)

30 Sep 2012 Andrew

Andrew (view profile)

Does anyone know what the subjects matching output is? I'm assuming it shows the probably that subjects are the same, but just want to make sure

Comment only
04 Sep 2012 Arash Salarian

@Justin: I'm struggling with the comment editor. A previous comment was magically deleted and the other one does not make much sense now. In short, I verified the code and I think it is fine. Also, I run the same data through R and got identical results. You can see the R output here:

Comment only
04 Sep 2012 Arash Salarian

Ah, I messed up the R output in my comment. Here is a link to a picture of the R output:

Comment only
16 Mar 2012 Justin

Justin (view profile)

One problem that I noticed is that in your example, you show that time is the within subjects factor, but in your reality you seem to treat the group as the within subjects factor.

the f-value that you get for time seems to come from dividing MStime by MSerror. This should really be MStime / MSsubjects if this is the variable with repeated measures. You also get the f value for group by MSgroup / MSsubjects. The group variable should not have repeated measures, so basically, the two should be reversed. I'm not sure, but I think that the error is consistent with itself int he rest of your code, so In order to get the correct results from this function you need to switch the organize your two factors opposite of the way that the you did it in the example.

statizticz is 3vil

Comment only
11 Jul 2011 Ali Ali

Thanks a lot

16 May 2011 Chris Allen

Nice clear function, be even better if it could handel more factors (like anovan)

20 Jan 2011 Sophie

Sophie (view profile)

Dear Arash, First thank you for this really useful function. Please, would you mind telling us, what does this "Subject matching" correspond to exactly (in the resulting table)?
Thanks a lot.

15 Nov 2016 1.0

The program is now deprecated! Please use MATLAB's fitrm instead.

Contact us