Dear All,
The purpose for the following program is to work with inverting a 3X3
matrix .
I am wondering if anyone has done such work and one knows of any prettier
way
of writing such a program .I am also interested in the technique of handling
matrices bigger than 3x3 :
use auto,clear
rename price a
rename weight b
rename mpg c
gen d = 1
keep a b c d
capture program drop nvrs
program define nvrs
gen `1'`2' = `3' * `4'
sum `1'`2'
scalar `5' = r(sum)
end
nvrs a a a a v1
nvrs a b a b v2
nvrs a c a c v3
nvrs a d a d v4
nvrs b b b b v5
nvrs b c b c v6
nvrs b d b d v7
nvrs c c c c v8
nvrs c d c d v9
nvrs d d d d v10
matrix A = (v1,v2,v3,v4\v2,v5,v6,v7\v3,v6,v8,v9\v4,v7,v9,v10)
matrix list A
capture program drop nvrsn
program define nvrsn
scalar `1' = [`2' * `3' - `4' * `5']*[ `6' ]
end
nvrsn s1 v8 v10 v9 v9 1
nvrsn s2 v6 v10 v7 v9 -1
nvrsn s3 v6 v9 v7 v8 1
nvrsn s4 v6 v10 v9 v7 -1
nvrsn s5 v5 v10 v7 v7 1
nvrsn s6 v5 v9 v7 v6 -1
nvrsn s7 v6 v9 v8 v7 1
nvrsn s8 v5 v9 v6 v7 -1
nvrsn s9 v5 v8 v6 v6 1
scalar s10 = v5*[v8*v10-v9*v9] - v6*[v6*v10-v7*v9] + v7*[v6*v9-v7*v8]
matrix B = (s1/s10,s2/s10,s3/s10\s4/s10,s5/s10,s6/s10\s7/s10,s8/s10,s9/s10)
matrix list B
matrix Xy = A[1,2...]
matrix list Xy
matrix b = Xy*B
matrix list b
use auto,clear
reg price weight mpg
use auto,clear
(1978 Automobile Data)
. rename price a
. rename weight b
. rename mpg c
. gen d = 1
. keep a b c d
. capture program drop nvrs
. program define nvrs
1. gen `1'`2' = `3' * `4'
2. sum `1'`2'
3. scalar `5' = r(sum)
4. end
. nvrs a a a a v1
. matrix A = (v1,v2,v3,v4\v2,v5,v6,v7\v3,v6,v8,v9\v4,v7,v9,v10)
. matrix list A
symmetric A[4,4]
c1 c2 c3 c4
r1 3.448e+09
r2 1.468e+09 7.188e+08
r3 9132716 4493720 36008
r4 456229 223440 1576 74
. capture program drop nvrsn
. program define nvrsn
1. scalar `1' = [`2' * `3' - `4' * `5']*[ `6' ]
2. end
. nvrsn s1 v8 v10 v9 v9 1
. nvrsn s2 v6 v10 v7 v9 -1
. nvrsn s3 v6 v9 v7 v8 1
. nvrsn s4 v6 v10 v9 v7 -1
. nvrsn s5 v5 v10 v7 v7 1
. nvrsn s6 v5 v9 v7 v6 -1
. nvrsn s7 v6 v9 v8 v7 1
. nvrsn s8 v5 v9 v6 v7 -1
. nvrsn s9 v5 v8 v6 v6 1
. scalar s10 = v5*[v8*v10-v9*v9] - v6*[v6*v10-v7*v9] + v7*[v6*v9-v7*v8]
. matrix B =
(s1/s10,s2/s10,s3/s10\s4/s10,s5/s10,s6/s10\s7/s10,s8/s10,s9/s10)
. matrix list B
symmetric B[3,3]
c1 c2 c3
r1 6.508e-08
r2 7.057e-06 .00117444
r3 -.0003468 -.04632027 2.047163
. matrix Xy = A[1,2...]
. matrix list Xy
Xy[1,3]
c2 c3 c4
r1 1.468e+09 9132716 456229
. matrix b = Xy*B
. matrix list b
b[1,3]
c1 c2 c3
r1 1.7465588 -49.512263 1946.0707
. use auto,clear
(1978 Automobile Data)
. reg price weight mpg
Source | SS df MS Number of obs =
74
---------+------------------------------ F( 2, 71) =
14.74
Model | 186321280 2 93160639.9 Prob > F =
0.0000
Residual | 448744116 71 6320339.67 R-squared =
0.2934
---------+------------------------------ Adj R-squared =
0.2735
Total | 635065396 73 8699525.97 Root MSE =
2514.0
----------------------------------------------------------------------------
--
price | Coef. Std. Err. t P>|t| [95% Conf.
Interval]
---------+------------------------------------------------------------------
--
weight | 1.746559 .6413538 2.723 0.008 .4677361
3.025382
mpg | -49.51222 86.15604 -0.575 0.567 -221.3025
122.278
_cons | 1946.069 3597.05 0.541 0.590 -5226.244
9118.382
----------------------------------------------------------------------------
--
.
end of do-file
.
*
* 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/