Update to C+=C$g  # this is not working
It was not working since '+=' needs the right matrix structure in before.
Now I have the code to get the 'variance' within the the groups:

<hansl>
loop for i=2..rows(G) -q
    if G[i]<>G[i-1]
        Ns[i]=i
    endif
endloop
eval Ns
matrix Ns =uniq(Ns)
matrix C=zeros(cols(Y),cols(Y))
loop for g=1..rows(Ns)
    if g<rows(Ns)
        C$g=mcov(Y[Ns[g]:Ns[g+1]-1,])*(rows(Y[Ns[g]:Ns[g+1]-1,])-1)
    else
        C$g=mcov(Y[Ns[g]:rows(G),])*(rows(Y[Ns[g]:rows(G),])-1)
    endif
    C+=C$g
endloop
eval C
<hansl>


Am 25.10.2012 12:50, schrieb Pindar:
Hi there,

I'm trying to do multivariate ANOVA and have a bit trouble with the following code:

<hansl>
nulldata 8
matrix Y={9,4;10,2;11,0;2,3;4,4;6,5;1,5;5,9}
matrix G={1;1;1;2;2;2;3;3}
matrix T=mcov(Y)*(rows(Y)-1)
eval T
matrix Ns =zeros(rows(uniq(G)),1)
#How to get to the Sum of squared residuals?
matrix Y_g1=Y[1:3,]
matrix Y_g2=Y[4:6,]
matrix Y_g3=Y[7:8,]
matrix C={}
matrix C1=mcov(Y_g1)*(rows(Y_g1)-1)
matrix C2=mcov(Y_g2)*(rows(Y_g2)-1)
matrix C3=mcov(Y_g3)*(rows(Y_g3)-1)
matrix C=C1+C2+C3
eval C
matrix C={}
loop for g=1..rows(Ns)
    #C+=mcov(Y_g$g)*(rows(Y_g$g)-1)
    C+=C$g  # this is not working
endloop
eval C
<hansl>

Up until now I did not find a way to get to know the number of obs per group or the indices for the the matrices matrix Y_g*.
Should work with some loop construction. However, what do you thing of giving the function cdemean a second argument that demeans the columns
according to the structure of the groups as in G: C=cdemean(Y,G)'cdemean(Y,G)'

Cheers
Leon Unger