View License

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

» Watch video

Highlights from
Two-way repeated measures ANOVA

4.1 | 16 ratings Rate this file 55 Downloads (last 30 days) File Size: 2.72 KB File ID: #6874 Version: 1.4

Two-way repeated measures ANOVA


Aaron Schurger (view profile)


07 Feb 2005 (Updated )

Two-way repeated measures ANOVA, for designs with two within-subjects variables.

| Watch this File

File Information

% function stats = rm_anova2(Y,S,F1,F2,FACTNAMES)
% Two-factor, within-subject repeated measures ANOVA.
% For designs with two within-subject factors.
% Parameters:
% Y dependent variable (numeric) in a column vector
% S grouping variable for SUBJECT
% F1 grouping variable for factor #1
% F2 grouping variable for factor #2
% FACTNAMES a cell array w/ two char arrays: {'factor1', 'factor2'}
% Y should be a 1-d column vector with all of your data (numeric).
% The grouping variables should also be 1-d numeric, each with same
% length as Y. Each entry in each of the grouping vectors indicates the
% level # (or subject #) of the corresponding entry in Y.
% Returns:
% stats is a cell array with the usual ANOVA table:
% Source / ss / df / ms / F / p
% Notes:
% Program does not do any input validation, so it is up to you to make
% sure that you have passed in the parameters in the correct form:
% Y, S, F1, and F2 must be numeric vectors all of the same length.
% There must be at least one value in Y for each possible combination
% of S, F1, and F2 (i.e. there must be at least one measurement per
% subject per condition).
% If there is more than one measurement per subject X condition, then
% the program will take the mean of those measurements.
% Aaron Schurger (2005.02.04)
% Derived from Keppel & Wickens (2004) "Design and Analysis" ch. 18

Required Products Statistics and Machine Learning Toolbox
MATLAB release MATLAB 6.5.1 (R13SP1)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (32)
02 Feb 2017 Dish

Dish (view profile)

Aaron, how do we estimate the effect size here. Usually eta2 is used for effect size = SSeffect / SStotal.
In case of an example that I ran with repeated measures, 2 factors each with 2 levels and 1 response variable, I got the 'source' in the table as:
factor 1, factor 2, factor 1 x factor 2, factor 1 x subjects , factor 2 x subjects, factor 1 x factor 2 x subjects. I am not sure if the SStotal the sum of all of these..? Can you please confirm.

26 Jan 2017 Echo M

Echo M (view profile)

Thank you for the script, Aaron. will this script work for unbalanced samples (i.e. different number of subjects
per group) ?

09 Sep 2016 K Jay

K Jay (view profile)

Can we include between-subject factors here?

Comment only
24 Feb 2016 Antonietta Gabriella Liuzzi

Hi all,

It's not clear to me how input data (Y, S, F1,F2, FACTNAMES) have to be arranged. Can someone gives an example? Thanks!

Comment only
08 Nov 2015 Timothy Myers

I am not sure what to put for the FACTNAMES. I have entered "1"s and "2"s for the conditions in F1 & F2 is this correct?

Comment only
19 Jun 2014 Lisa

Lisa (view profile)

Solved it!

Comment only
23 Apr 2014 Chloe

Chloe (view profile)

Is the result of p-value from first line indicating the result of within subject?

Comment only
21 Nov 2013 Aaron Schurger

Aaron Schurger (view profile)

Hi, Marc,
This procedure is strictly for situations where you have exactly two within-subjects factors. To my knowledge there exists no n-factor repeated measures ANOVA algorithm. Just going from two-factor to three-factor you have a huge jump in complexity of the algorithm, and when you have different numbers of between-subjects and within-subjects factors things get really scary really fast. But there are functions on the File Exchange for more than two factors.

Comment only
25 May 2013 Marc

Marc (view profile)

Will this file work for a multi-factor (more than two factors) ANOVA?

Will it also work on a One-Way ANOVA?

Comment only
10 Dec 2011 Jordan

Jordan (view profile)

This seems very promising, but I can't figure out what kind of input is needed. For instance, what is d in the function definitions, and are Y and grouping vectors meant to comprise one column or multiple columns? It would be very helpful if the author could include with his download a small .mat file with sample inputs for Y, F1, F2 etc.

Comment only
11 Oct 2011 Marieke

the script works now - I appear to have had an old version of the script.

Comment only
06 Jul 2011 Erin

Erin (view profile)

Good submission. I found this script gives the same results as anovan. To do repeated measures with anovan, its necessary to make another factor for subjects, and specify that factor as 'random'.

06 May 2011 Marieke Rohde

for the data that made me spot the bug the script displays the following first three lines of the ANOVA tables.

If you take the average from multiple cells by hand:

'Source'SS df MS F p
'cond' 1.0413 1 1.0413 1.8483 0.1971
'hand' 0.2325 1 0.2325 1.7135 0.2132
'cxh' 0.1184 1 0.1184 2.7210 0.1230

if you enter several values for multiple cells, according to documentation, it should do the same thing automatically, but doesn't:

Source SS df MS F p
'cond' 1.0413 1 1.0413 1.8483 0.1971
'hand' 0.2325 1 0.2325 1.7135 0.2132
cdxhd 0.1184 1 0.1184 1.4417e-05 0.9970

which is jsut plainly wrong (note that first 3 columns are identical). i would be surprised if Statistica did the same. i did the calculations by hand at the time to figure out what exactly went wrong but cannot remember - if you're interested in the data please be in touch.

Comment only
27 Apr 2011 titou

titou (view profile)

Thanks a lot.

I precise I just needed to change :




and it works great ! (same results as Statistica)

29 Apr 2010 Jamie

Jamie (view profile)

I agree this is a very well written script. However, I am somewhat confused about the Y input variable. In the comments above it says that 'Y' should be a column vector (I assume this means (:, 1)). My confusion comes from the following lines:

for i=1:a % F1
for j=1:b % F2
for k=1:n % Subjs
INDS{i,j,k} = ...
CELLS{i,j,k} = Y(INDS{i,j,k});
MEANS(i,j,k) = mean(CELLS{i,j,k});

The '...' was placed instead of the code to create the indices. The line CELLS{i,j,k} = Y(INDS{i,j,k}); generates a 3D cell from Y, which makes sense to have the data in three dimensions, however Y is a 1D column and creates a large number of NaNs.

Also, Matlab produces the following error:

??? Error using ==> horzcat
CAT arguments dimensions are not consistent.

Error in ==> rm_anova2 at 138
stats = {'Source','SS','df','MS','F','p';...

Long story short, what am I not getting about this?

Comment only
22 Apr 2010 Marieke Rohde

There's a mistake in this script: if several measurements are provided for one cell, it gives wrong values for the interaction. help says it would take the average in that case, which it does for the most part, but at some point it gets confused with the number of measurements taken and F and p are then really wrong (only concerns interaction with several values per cell).

By the way, RMAOV2 --> same functionality, even though a bit bulky, takes the variance across repeated measurements for the same cell into consideration.

Comment only
23 Feb 2010 Alberich Qi

I think it should be written as :

FACTNAMES = {F1name, F2name};
F1name = {'abc' , ..., 'xyz'};
F2name = {'xx', ..., 'yy'};

It can work well in my script.

31 Dec 2009 Robert

Robert (view profile)

I would also appreciate a one line example of how to enter FACTNAMES. I'm getting an error. Thanks!

Comment only
19 Nov 2009 Negar

Negar (view profile)


Can someone give me a one line example of how to enter FACTNAMES? I'd appreciate it.

Comment only
17 Jul 2009 Aaron Schurger

Hi, Zachary,
Good question. No, it was not designed to deal with a different number of subjects on each factor. Sorry about that. It might not be too complicated to code, but I imagine that there are some statistical issues to be mindful of, and I would want to look into that before changing the code. In the meantime, you might be able to insert a dummy value, which is just the mean of the other values, just to make the program work. One thing that I can do is to make the program generate an error instead of just giving back nan's. It would help to see the data, just as you passed them to the program. -Aaron

Comment only
17 Jun 2009 Zachary Danziger

The stats output is a cell array of NaNs. Is this software capable of dealing with unequally distributed data? I.e., 7 subjects are evaluated on factor 1, but only 6 are evaluated on factor 2.

Comment only
24 Jul 2008 piyush singhal

07 Apr 2008 terence hill

23 Mar 2008 keidit chankhachon

16 Mar 2008 Perspicacity Slim

It would make my day if you could add Greenhouse-Geiser correction for violating the sphericity assumption of repeated measures ANVOA.

18 Feb 2008 Ashwin Sundar

28 Jun 2007 malia mason

This worked well for me.

13 Dec 2006 adam mcnamara

Examples with data are always very helpful.. this is what this lacks

27 Feb 2006 Teck Por Lim

The author had corrected the code after Ted Zanto's comment in 2005.

02 Feb 2006 dojoon yi

It is great. Thank you!!!

22 Jul 2005 Ted Zanto

Excellent program...does exactly what it's designed for. Easy to implement and coded in a very straightforward manner. Results were confirmed in SPSS...right on the mark.

One tiny error on line 133: FACTNAMES{1} should be FACTNAMES{2}...but this merely a labeling issue.

Nonetheless, it's a great program, thanks Aaron!

29 Apr 2005 Aaron Schurger

A very well-written program (of course, I wrote it myself). One or two additional features are of interest, so I would not yet call it excellent.

02 May 2005

An error on line 70 of the code produced errors if you ran the ANOVA on your raw data, without having previously averaged the measurements within each cell. This feature now works correctly.

31 May 2005

There was a minor problem in the code (line 70) which would produce inaccurate results if you did not average your data within each condition prior to calling the function. This is now fixed.

11 Dec 2009 1.3

Fixed a minor error in the code. Uploaded new version. I had fixed this problem quite a while back, and uploaded the file. It may not have uploaded properly the first time. Thanks to Johan Carlin for bringing the problem to my attention.

24 Jul 2011 1.4

Fixed one minor error in the labeling of the output, and also modified the comments.

Contact us