Regression Coefficients from LMM (More Efficient Matrix Code)

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Regression Coefficients from LMM (More Efficient Matrix Code)

Ryan
Dear SPSS-L,
 
A few years ago I provided an example of how to estimate regression coefficients from a linear mixed model:
 
 
Although the MATRIX code provided in the link above was intended to be instructive (I hope), clearly it could have been much more efficient. You will see BELOW that each matrix invoked is now only one line of code. There is nothing groundbreaking here. I simply wanted to provide more efficient code. Of course, thoughts on making it more efficient or general thoughts, comments, and/or questions about this LMM are more than welcome. With that in mind, I should also point out that in a subsequent post, I showed how to use the coefficient matrix L to calculate test statistics here (a couple of the notes reversed the rows and columns, but the code is correct):
 
Best,
 
Ryan
 
--
***** Create data set *****.
DATA LIST /observation 1-2 ingot 4 metal 6 pressure 8-11(1).
BEGIN DATA
01 1 3 67.0
02 1 2 71.9
03 1 1 72.2
04 2 3 67.5
05 2 2 68.8
06 2 1 66.4
07 3 3 76.0
08 3 2 82.6
09 3 1 74.5
10 4 3 72.7
11 4 2 78.1
12 4 1 67.3
13 5 3 73.1
14 5 2 74.2
15 5 1 73.2
16 6 3 65.8
17 6 2 70.8
18 6 1 68.7
19 7 3 75.6
20 7 2 84.9
21 7 1 69.0
END DATA.
***** random intercept model *****.
MIXED pressure BY metal
  /FIXED=metal | SSTYPE(3)
  /METHOD=REML
  /PRINT=CORB COVB DESCRIPTIVES G  LMATRIX R SOLUTION TESTCOV
  /RANDOM=INTERCEPT | SUBJECT(ingot) COVTYPE(VC).
***** repeated measures model with CS var-cov residual matrix *****.
MIXED pressure BY metal
  /FIXED=metal | SSTYPE(3)
  /METHOD=REML
  /PRINT=CORB COVB DESCRIPTIVES  LMATRIX R SOLUTION TESTCOV
  /REPEATED=metal | SUBJECT(ingot) COVTYPE(CS).
MATRIX.
COMPUTE Y = {67.0;71.9;72.2;67.5;68.8;66.4;76.0;82.6;74.5;72.7;78.1;67.3;73.1;74.2;73.2;65.8;70.8;68.7;75.6;84.9;69.0}.
COMPUTE X = {MAKE (21, 1, 1),{IDENT(3);IDENT(3);IDENT(3);IDENT(3);IDENT(3);IDENT(3);IDENT(3)}}.
COMPUTE Z = {IDENT(7);IDENT(7);IDENT(7)}.
COMPUTE G= IDENT(7)*11.447777777778326.
COMPUTE R= IDENT(21)*10.371587301585574.
COMPUTE V = Z*G*TRANSPOS(Z)+R.
COMPUTE B = GINV(TRANSPOS(X)*INV(V)*X)*TRANSPOS(X)*INV(V)*Y.
print Y /title "Y".
print X /title "X".
print Z /title "Z".
print G /title "G".
print R /title "R".
print V /title "V".
print B /title "B".
end matrix.
COMPUTE b0 = 54.29642857 + 16.80357143.
COMPUTE b1 = (54.29642857 + 15.88928571) - (54.29642857 + 16.80357143).
COMPUTE b2 = (54.29642857 + 21.60357143) - (54.29642857 + 16.80357143).
EXECUTE.
Reply | Threaded
Open this post in threaded view
|

Re: Regression Coefficients from LMM (More Efficient Matrix Code)

Ryan
I think the first link might be broken. Here is the corrected first link:
 
 
Ryan


On Fri, Apr 26, 2013 at 11:22 PM, Ryan Black <[hidden email]> wrote:
Dear SPSS-L,
 
A few years ago I provided an example of how to estimate regression coefficients from a linear mixed model:
 
 
Although the MATRIX code provided in the link above was intended to be instructive (I hope), clearly it could have been much more efficient. You will see BELOW that each matrix invoked is now only one line of code. There is nothing groundbreaking here. I simply wanted to provide more efficient code. Of course, thoughts on making it more efficient or general thoughts, comments, and/or questions about this LMM are more than welcome. With that in mind, I should also point out that in a subsequent post, I showed how to use the coefficient matrix L to calculate test statistics here (a couple of the notes reversed the rows and columns, but the code is correct):
 
Best,
 
Ryan
 
--
***** Create data set *****.
DATA LIST /observation 1-2 ingot 4 metal 6 pressure 8-11(1).
BEGIN DATA
01 1 3 67.0
02 1 2 71.9
03 1 1 72.2
04 2 3 67.5
05 2 2 68.8
06 2 1 66.4
07 3 3 76.0
08 3 2 82.6
09 3 1 74.5
10 4 3 72.7
11 4 2 78.1
12 4 1 67.3
13 5 3 73.1
14 5 2 74.2
15 5 1 73.2
16 6 3 65.8
17 6 2 70.8
18 6 1 68.7
19 7 3 75.6
20 7 2 84.9
21 7 1 69.0
END DATA.
***** random intercept model *****.
MIXED pressure BY metal
  /FIXED=metal | SSTYPE(3)
  /METHOD=REML
  /PRINT=CORB COVB DESCRIPTIVES G  LMATRIX R SOLUTION TESTCOV
  /RANDOM=INTERCEPT | SUBJECT(ingot) COVTYPE(VC).
***** repeated measures model with CS var-cov residual matrix *****.
MIXED pressure BY metal
  /FIXED=metal | SSTYPE(3)
  /METHOD=REML
  /PRINT=CORB COVB DESCRIPTIVES  LMATRIX R SOLUTION TESTCOV
  /REPEATED=metal | SUBJECT(ingot) COVTYPE(CS).
MATRIX.
COMPUTE Y = {67.0;71.9;72.2;67.5;68.8;66.4;76.0;82.6;74.5;72.7;78.1;67.3;73.1;74.2;73.2;65.8;70.8;68.7;75.6;84.9;69.0}.
COMPUTE X = {MAKE (21, 1, 1),{IDENT(3);IDENT(3);IDENT(3);IDENT(3);IDENT(3);IDENT(3);IDENT(3)}}.
COMPUTE Z = {IDENT(7);IDENT(7);IDENT(7)}.
COMPUTE G= IDENT(7)*11.447777777778326.
COMPUTE R= IDENT(21)*10.371587301585574.
COMPUTE V = Z*G*TRANSPOS(Z)+R.
COMPUTE B = GINV(TRANSPOS(X)*INV(V)*X)*TRANSPOS(X)*INV(V)*Y.
print Y /title "Y".
print X /title "X".
print Z /title "Z".
print G /title "G".
print R /title "R".
print V /title "V".
print B /title "B".
end matrix.
COMPUTE b0 = 54.29642857 + 16.80357143.
COMPUTE b1 = (54.29642857 + 15.88928571) - (54.29642857 + 16.80357143).
COMPUTE b2 = (54.29642857 + 21.60357143) - (54.29642857 + 16.80357143).
EXECUTE.