# st: Matrix question - square roots of matrix elements

 From "Amy Dunbar" <[email protected]> To <[email protected]> Subject st: Matrix question - square roots of matrix elements Date Wed, 24 Jan 2007 19:34:05 -0500

```How can I get the square roots of the diagonal elements in the covhat
matrix in the do file below? I am teaching a seminar that includes a
section on stata, and I am trying to replicate an example from a
textbook using stata.  I am also struggling with the language to compute
R2.  I apologize for these elementary questions; I did search for an
example, but if there was one, I didn't find it.

/* This example is from Griffiths, W.E., R. Carter Hill, and George G.
Judge, Learning and Practicing Econometrics, John Wiley and Sons, 1993.
Ch9 example. 52 weeks of observations */

clear
input y x1 x2 x3
123.1	1	1.92	12.4
124.3	1	2.15	9.9
89.3	1	1.67	2.4
141.3	1	1.68	13.8
112.8	1	1.75	3.5
108.1	1	1.55	1.8
143.9	1	1.54	17.8
124.2	1	2.1	9.8
110.1	1	2.44	8.3
111.7	1	2.47	9.8
123.8	1	1.86	12.6
123.5	1	1.93	11.5
110.2	1	2.47	7.4
100.9	1	2.11	6.1
123.3	1	2.1	9.5
115.7	1	1.73	8.8
116.6	1	1.86	4.9
153.5	1	2.19	18.8
149.2	1	1.9	18.9
89	1	1.67	2.3
132.6	1	2.43	14.1
97.5	1	2.13	2.9
106.1	1	2.33	5.9
115.3	1	1.75	7.6
98.5	1	2.05	5.3
135.1	1	2.35	16.8
124.2	1	2.12	8.8
98.4	1	2.13	3.2
114.8	1	1.89	5.4
142.5	1	1.5	17.3
122.6	1	1.93	11.2
127.7	1	2.27	11.2
113	1	1.66	7.9
144.2	1	1.73	17
109.2	1	1.59	3.3
106.8	1	2.29	7.1
145	1	1.86	15.3
124	1	1.91	12.7
106.7	1	2.34	6.1
153.2	1	2.13	19.6
120.1	1	2.05	6.3
119.3	1	1.89	9
150.6	1	2.12	18.7
92.2	1	1.87	2.2
130.5	1	2.09	16
112.5	1	1.76	4.5
111.8	1	1.77	4.3
120.1	1	1.94	9.3
107.4	1	2.37	8.3
128.6	1	2.1	15.4
124.6	1	2.29	9.2
127.2	1	2.36	10.2
end

* create y "matrix" (actually a vector but Stata doesn't create vectors)
mkmat y
matrix list y

* create X matrix (notice that I made it a cap X)
mkmat x1 x2 x3, matrix (X)
matrix list X

* create X'X matrix - xprimex
matrix xprimex= X'*X
matrix list xprimex

* create X'y matrix - xprimey
matrix xprimey=X'*y
matrix list xprimey

* create (X'X)-1 matrix  - inverseX
matrix inverseX=inv(xprimex)
matrix list inverseX

* create least squares estimates - b
mat b=inverseX*xprimey
matrix list b

* create least squares residual  -  ehat
matrix ehat=y-X*b
matrix list ehat

* create variance estimate - sigma2hat
matrix sigma2hat=(ehat'*ehat)/(52-3)
matrix list sigma2hat

*__________________________________________________
*NEED HELP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

* create estimated covariance matrix - covhat
matrix covhat=sigma2hat*inverseX
mat list covhat

display sqrt(42.025641)
display sqrt(10.183711)
display sqrt(.02786767)

* coefficient of determination R2 =
mat ehat2=ehat'*ehat
mat list ehat2
mat accum ydev = y, dev noconstant
mat list ydev

* R2 = 1 - ehat2/ydev
display 1- 1805.1682/13581.352

* Confirm results
reg y x2 x3

Amy Dunbar
University of Connecticut
Department of Accounting
Storrs, CT 06269

[email protected]
cell: 860-208-2737

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```