Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using 'anovan' with repeated measures

3,760 views
Skip to first unread message

Joe

unread,
Dec 23, 2003, 4:57:38 PM12/23/03
to
Hi,

I am trying to run an N-way ANOVA with Matlab function anovan(X,
group). In the documentation for 'anovan', an example of 'group' is
given as the following with a single measure for each observation:

group = {[1 2 1 2 1 2 1 2];
['hi';'hi';'lo';'lo';'hi';'hi';'lo';'lo']; {'may' 'may' 'may' 'may'
'june' 'june' 'june' 'june'}}

However it is not clear to me how to create such a cell array for
repeated measures. If there are two repeated measures, what kind of
cell array would be? Something like the following?

group2 = {[1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2];
['hi';'hi';'hi';'hi';'lo';'lo';'lo';'lo';'hi';
'hi';'hi';'hi';'lo';'lo';'lo';'lo']; {'may' 'may' 'may' 'may' 'may'
'may' 'may' 'may' 'june' 'june' 'june' 'june' 'june' 'june' 'june'
'june'}}

Your clarification and help would be highly appreciated.

Thanks,
Joe

us

unread,
Dec 23, 2003, 5:38:49 PM12/23/03
to
Joe:
<SNIP wants to do a repeated measure anova using
<anovan>>

doesn't work:

<http://groups.google.com/groups?hl=en&lr=lang_en&ie=UTF-8&safe=off&threadm=b1e8gv%242e%241%40ginger.mathworks.com&rnum=2&prev=/groups%3Fq%3Drepeated%2Bmeasure%2Banova%2Bgroup>:comp.soft-sys.matlab%26hl%3Den%26lr%3Dlang_en%26ie%3DUTF-8%26group%3Dcomp.soft-sys.matlab%26safe%3Doff%26sa%3DG%26scoring%3Dd

us

Daniel

unread,
Oct 13, 2009, 2:42:04 PM10/13/09
to
I realize this thread is years old, nevertheless...

To use anovan for repeated measures, you have to enter the subject id's as a new factor and specify that factor as a random variable in anovan. If you have different subjects in different groups, you must also specify that the subject index is nested in the group variable.

In the example above, if the first cell is the between conditions factor and the remaining two cells are within subjects factors then the correct way to specify the factor indexes is as follows (with a bit of rearranging for clarity):

subjects = [1 2 3 4 5 6 7 8]; % Assuming that subjects 1-4 were in group 1 and 5-8 were in group 2

group = {[1 1 1 1 2 2 2 2];
['hi';'lo';'hi';'lo';'hi';'lo';'hi';'lo']; {'may' 'may' 'june' 'june' 'may' 'may'
'june' 'june'}; subjects}

Then call anovan:

anovan(data, group, 'random', 4, 'nested', [0 0 0 0; 0 0 0 0; 0 0 0 0; 1 0 0 0]);

This will produce the appropriate ANOVA output.

Dan

Jean-Jacques

unread,
Nov 25, 2009, 2:34:17 PM11/25/09
to
Hi Daniel,

That does not work!!!! It gives NaNs for some factors:
(data from Matlab three-way ANOVA example)

Source Sum Sq. d.f. Mean Sq. F Prob>F
-----------------------------------------------------
X1 0.061 1 0.06125 0.01 0.914
# X2 0 0 0 0 NaN
# X3 0 0 0 0 NaN
X4(X1) 18.535 4 4.63375 Inf NaN
Error 0 0 0
Total 221.379 7

Basically, with your technique, the X2 and X3 are not of full rank...

How can I fix this?

Daniel

unread,
Feb 1, 2010, 1:54:09 PM2/1/10
to
In my message above, the subject numbers should have been fully crossed with the different conditions. In the example below, there are two subjects in each condition (numbers 11, 12, 21, and 22) who underwent all levels of treatments A and B. The columns are Condition, Treatment A, Treatment B, and Subject:

variables = [1 1 1 11
1 1 1 12
1 1 2 11
1 1 2 12
1 2 1 11
1 2 1 12
1 2 2 11
1 2 2 12
2 1 1 21
2 1 1 22
2 1 2 21
2 1 2 22
2 2 1 21
2 2 1 22
2 2 2 21
2 2 2 22]

If you now run the following, you'll get the appropriate ANOVA table:
anovan(y, [group subjects], 'random', 4,...
'nested', [0 0 0 0; 0 0 0 0; 0 0 0 0; 1 0 0 0],...
'varnames', {'Condition', 'A', 'B', 'Subject'}, 'model' ,'full')

The problem with the first example was that each subject only completed one level of each treatment so it wasn't a repeated measures design. To make that work you have to increase the size of the design matrix and repeat the subject factor at each combination of treatments, e.g., 'hi' + 'may', 'lo' + 'may', 'hi' + 'june', and 'lo' + 'june'.

Dan

StatLearner

unread,
Feb 18, 2010, 4:26:56 PM2/18/10
to

Hi Dan,

You are of great help. Thank you so much. But, this is still a bit
complicated for me to understand how you did this (specially matrix
manipulation)

Yes, I am exactly looking for what you are explaining here, but it is
kind of difficult for me to understand it. I am trying to do repeated
measure anova using "anovan". Here is what my data looks like.

I have 2 groups

Group 1 (control group)
There are 15 subjects.
There are 5 recording for each subject over time.

Group 2 (treatment group)
There are 13 subjects (different subjects)
There are 5 recording for each subject over time.

So, I want to find the effect of treatment(if there is any), effect of
time(if there is any) and combined effect of treatment over
time(interaction, if there is any).

Can you please explain me the matrix arrangement for my querry. I will
highly appreciate it. I have a solved example from a textbook and I
want to try the same problem using "code" to make sure if the code
will really work.

Also, can you suggest me some good books on Advanced statistical
analysis and methods, which explains everything in detail. I will
really appreciate any help .

Thanks.

Eric

unread,
Apr 19, 2011, 12:27:48 AM4/19/11
to
I realize that this thread is even older now, but it still ranks high on a search using Google.

I was wondering this too, but the example Joe posted does work. You simply repeat your indices, pass that to ANOVA, and it will work.

Let's say you have the two following factors:
. Two-Level Factor A (A1, A2)
. Two-Level Factor B (B1, B2)
with the following data mapped to their factor combinations, with two repeated measures:
(A1, B1) [4 0]
(A1, B2) [3 11]
(A2, B1) [13 7]
(A2, B2) [20 14]

To plug this into MATLAB: (data grouped in [] for aesthetics)
% ((A1, B1), (A1, B2)) ((A2, B1) (A2, B2))
data = [[[4 0] [3 11]] [[13 7] [20 14]]];
% A1, A1, A2, A2
aLevels = [[[1 1] [1 1]], [[2 2] [2 2]]];
% B1, B2, B1, B2
bLevels = [[[1 1] [2 2]], [[1 1] [2 2]]];
anovan(data, {aLevels, bLevels}, 'model', 'interaction');

Output:
Analysis of Variance


Source Sum Sq. d.f. Mean Sq. F Prob>F

A 162 1 162 8.5263 0.043244
B 72 1 72 3.7895 0.12343
A*B 2 1 2 0.10526 0.76186
Error 76 4 19
Total 312 7
Constrained (Type III) sums of squares.

This is a simple example, but it still took me a while to understand. Just thought it might help others.

Tim

unread,
Sep 19, 2013, 4:22:08 AM9/19/13
to
Hi Eric,
thanks for the example. However, I had some trouble adapting that to my design (one between factor with 3 levels and one within factor with 5 levels; see example below).

My main problem is that I don't fully understand which factor - in your example - is the within and which is the between factor. If you could re-write your example to include a within factor with three levels, I think I'd have a better chance of understanding it :-)

Thanks for your help!
Cheers,
Tim

Here my dummy data:
% lines represent subjects; cols represent observations per subject)
data = [250 278 442 368 456
65 207 341 382 298
251 261 384 421 342
241 314 423 415 468
154 167 257 275 332
103 286 401 291 367
230 306 432 386 423
54 172 307 261 360
20 116 425 398 268
41 168 378 317 470
200 157 283 259 273
34 86 351 280 320
29 81 193 240 233
3 54 285 216 245
118 124 365 311 331
83 266 382 369 295
38 207 289 385 373
71 211 356 380 305
123 331 407 461 445
71 285 471 407 433
108 247 317 307 324];
betweenSubjectsFactor(1:7) = 1;
betweenSubjectsFactor(8:14) = 3;
betweenSubjectsFactor(15:21) = 2;

predictor = repmat(betweenSubjectsFactor,[n,1]);
anovan(data(:), {predictor(:), BETWEENFACTOR?}, 'model', 'interaction');



Eric <eacou...@gmail.com> wrote in message <e38ecfcf-6c48-4789...@glegroupsg2000goo.googlegroups.com>...

Duijnhouwer

unread,
Jul 4, 2017, 7:45:16 AM7/4/17
to
0 new messages