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
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
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
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?
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
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.
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.