Code covered by the BSD License  

Highlights from
Two-way repeated measures ANOVA

4.0

4.0 | 14 ratings Rate this file 92 Downloads (last 30 days) File Size: 2.72 KB File ID: #6874

Two-way repeated measures ANOVA

by

 

07 Feb 2005 (Updated )

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

| Watch this File

File Information
Description

%
% 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 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 (26)
23 Apr 2014 Chloe

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

21 Nov 2013 Aaron Schurger

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.

25 May 2013 Marc

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

Will it also work on a One-Way ANOVA?

10 Dec 2011 Jordan

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.

11 Oct 2011 Marieke

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

06 Jul 2011 Erin

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.

27 Apr 2011 titou

Thanks a lot.

I precise I just needed to change :

F1==F1_lvls(i)

in

strcmp(F1,F1_lvls(i))==1

and it works great ! (same results as Statistica)

29 Apr 2010 Jamie

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});
end
end
end

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?

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.

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

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

19 Nov 2009 Negar

Hi,

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

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

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.

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.

Updates
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

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

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

Contact us