6 3 2 3 4 9

advertisement
CROP 590 Experimental Design in Agriculture
Lab exercise – Week 10
Special Topic: Matrix Algebra
ANOVA and Regression in Matrix Notation
Refer to the power point presentation for background information for this exercise.
6
4
A
8
2

3
9

2
7 
2
1
B
6
8

3
7

5
4 
1) What is b3,2? What are the dimensions of B?
b3,2 = 5
B is a 4 x 2 matrix
2) Calculate C = A – B
3) Calculate E = A + B
4 0
3
2

C
 2 3 
 6 3 


8 6
 5 16 

E
14 7 
10 11


4) What is the transpose of A?
5) Calculate A’B
 6 4 8 2
A  

3 9 2 7 
2

 6 4 8 2  1
AB  

3 9 2 7 6
8

1
3
7   80

5   83
4 
94 
110 
6) Calculate the determinant of H and the inverse of H.
 7 2
H
|H| = ab-cd = 7*3-2*5 = 21 – 10 = 11

5 3 
3
2
 11
  0.2727 0.1818
1
H   5 117   

 11 11   0.4545 0.6364 
7) What is the trace of H? tr(H) = 7 + 3 = 10
8) You wish to fit a quadratic curve to the following data.
X
2
4
6
8
Y
6
13
17
16
Yi = b0 + b1Xi + b2Xi2
In Matrix notation , Y = XB + e
Y =
6 
13 
 
17 
16 
  
X
1
1
1
1
B
+ e
4
 e1 
b


0
16     e 2 
 b1   
36     e3 
b2   
64   e 4 
2
4
6
8
The solution to the normal equations is B = X’X-1X’Y
9) Use matrix functions in Excel to solve for B (MMULT, TRANSPOSE, and MINVERSE)
10) Solve for B using PROC IML in SAS
PROC IML;
X={1 2 4, 1 4 16, 1 6 36, 1 8 64};
Y={6, 13, 17, 16};
XPX=X`*X; print XPX;
XPXinv=inv(XPX); print XPXinv;
XPY=X`*Y; print XPY;
B=XPXinv*XPY; print B;
quit ;
Yi = -5.5 + 6.7Xi -0.5Xi
2
2
11) Now let’s analyze a simple CRD experiment with matrix notation:
Y =
X
3   1
 4  1
  
 1  1
 4   1
  
8   1
  
 6  1
1
1
0
0
0
0
B
+ e
0
 e11 
e 
0
 b0   12 
1    e 21 
b1   
1   e 22 
 b2 
 e31 
0
 

0
 e32 
proc iml;
X={1 1 0,
1 1 0,
1 0 1,
1 0 1,
1 0 0,
1 0 0}
;
Y={3, 4, 1, 4, 8, 6};
CF=sum(Y)*sum(Y)/countn(Y);
XPX=X`*X;
XPXinv=inv(XPX);
XPY=X`*Y;
B=XPXinv*XPY;
SSTotal=(Y`*Y)-CF;
SSTrt=B`*XPY-CF;
SSE=SSTotal-SSTrt;
YHAT=X*B;
Resid=Y-YHAT;
print SSTotal SSTrt SSE B YHAT Resid;
QUIT;
Refer to the power point presentation for more details….
3
Download