Stata 矩陣操作


一、生成矩陣

matrix 矩陣名 = (1,2,3  4,5,6)
變量轉矩陣 mkmat
矩陣轉變量 svmat

 1 matrix list 矩陣名 [, noblank nohalf noheader nonames format(%fmt) title(string) nodotz]               // 列示矩陣
 2 * 更為細致地列示矩陣
 3 #delimit ;
 4 matrix Htest = ( 12.30,  2,  .00044642   
 5                   2.17,  1,  .35332874                      
 6                   8.81,  3,  .04022625   
 7                  20.05,  6,  .00106763  ) ;
 8 #delimit cr
 9 matrix rownames Htest = trunk length weight overall  // 定義行名
10 matrix colnames Htest = chi2 df p                    // 定義列名
11 matlist Htest                                        // 添加邊框
12 matrix rename 原矩陣名 新矩陣名    // 矩陣更名  
13 * 矩陣的行數和列數
14 matrix 矩陣名 = (1,2,3  4,5,6)
15 scalar ra = rowsof(矩陣名)
16 scalar ca = colsof(矩陣名)
17 dis in g "矩陣 矩陣名 的行數是: " in y ra 
18 dis in g "矩陣 矩陣名 的列數是: " in y ca  
19 matrix dir                   // 查找矩陣
20 matrix drop 矩陣名           // 刪除矩陣       
21 display matmissing(矩陣名)   // 顯示矩陣缺漏值個數
22 * 矩陣的行名和列名
23 matrix rownames 矩陣名 = 行名(空格分隔)       // 行名
24 matrix colnames 矩陣名 = 列名(空格分隔)       // 列名
25 * 矩陣的選擇
26 matrix 新矩陣名 = 矩陣名[行索引,列索引]        // 中間省略:.. 全部省略:.... 尾部省略:...
27 matrix 矩陣中想要修改的元素 = 修改值           // 如果想要修改區域,只需在等號左側填入修改區域左上角的元素位置即可
28 * 矩陣的合並
29 matrix aa  = [a1, a2]   // 橫向合並兩個矩陣 
30 matrix aaa = [a1  a2]  // 縱向追加兩個矩陣
31 * 常用矩陣的定義
32 matrix I = I(n)         // 單位矩陣
33 matrix 矩陣名 = J(行數,列數,常數)
34 matrix r3 = matuniform(10,4)  // 生成一個10*4的隨機數矩陣,隨機數區間為(0,1)
35 * 將一維矩陣轉換成對角矩陣
36 mat u = J(5,1,-0.5)
37 mat du = diag(u)  // 取出對角元素
38 * 將變量轉換為矩陣
39 mkmat varlist [if] [in] [, matrix(matname) nomissing   // 單變量:矩陣名默認為變量名,選項nomissing表示僅包含非缺漏值
40 * 將矩陣轉化為變量
41 xsvmat 矩陣名, list(,)                                 // 以變量方式列示矩陣的內容
42 * 用矩陣存儲統計結果
43  makematrix [matrix_name], from(results_list) [production_options] [list_options]:["]command["] [varlist] ... [, options ]
44  * 矩陣運算
45  mgen exprlist , in(matname) out(matname) [ common(term) ]   // in-進行操作的矩陣 out-新矩陣 exprlist:數學表達式
46  * 保存矩陣
47  matsave matrix [, replace saving dropall path(path) type(type) ]  // 保存到dta中
48  mat2txt , matrix(matrixname) saving(filename) [ title(text) note(text) format(formatlist) replace append ] // 保存為txt格式
49  dataout <using filename> [, options]   // word:轉成rtf格式的word文檔 excel:轉成xml格式的excel文檔

 1 * 基本運算
 2 matrix A = (1,23,4)
 3 matrix B = (5,79,2)
 4 matrix C = A+B             // 加法
 5 matrix B = A-B             // 減法
 6 matrix X = (1,12,58,04,5)
 7 matrix C = 3*X*A'*B        // 乘法
 8 matrix D = (X'*X - A'*A)/4
 9 matrix D = A#D               // 直乘
10 matrix E = hadamard(A,B)     // Hadamard乘法
11 * 矩陣元素的數學變換
12 math B = function(A)         // 可供調用的function: help math functions
13 mgen exprlist , in(matname) out(matname) [ common(term) ]  // 分列變換:"v1=ln(c1)" 不可以寫為 "v1 = ln(c1)"
14 * 矩陣與單值的運算
15 scalar c = 5                 // 單值c
16 mat D = J(4,4,1)             // 矩陣D
17 mat Dc = D*c                 // mat cD = c*D   矩陣與單值相乘
18 mat D_c = D/c                // 矩陣與單值相除
19 * 矩陣的轉置: 行列互換
20 matrix A = (-1, 2  3, 4 )
21 mat At = A'                  // 轉置運算優先於乘法運算
22 * 矩陣的逆矩陣
23 scalar detA = det(A)         // 矩陣的行列式
24 dis issym(A)                 // 判斷一個矩陣是否為對稱矩陣
25 mat invA = inv(A)            // 求矩陣的逆矩陣
26 * 矩陣的向量化
27 mat vA = vec(A)
28 mat dA = vecdiag(A)          // 向量化方陣的對角元素
29 * 矩陣的對角值(trace)
30 matrix Atr = trace(A)        // 方陣的對角元素之和

matrix accum A = varlist [if] [in] [weight] [, noconstant deviations means(M) absorb(varname)]  // matrix accum 語法
matrix vecaccum a = varlist [if] [in] [weight] [, noconstant]                                   // matrix vecaccum語法
*- 幾個重要選項:
*  (1) noconstant 不在 X 矩陣中自動附加常數項;
*  (2) deviation  采用離差的形式

*-eg1- 線性模型的 OLS 估計 
*-目的:求取 b = inv(X'X)*X'y
* 其中,y = price, 
*       X =(weight,mpg,Cons)
sysuse auto, clear
* 方法1:僅使用 matrix accum 命令
* 思路: 若 A = (y, X), 則
*
*                                       [ y'y  y'X ]
*   mat accum (A) = S = (y, X)'(y, X) = [          ]
*                                       [ X'y  X'X ]
matrix accum S = price weight mpg  // y=price, X=[weight mpg]
mat list S
matrix XX = S[2..., 2...] 
matrix Xy = S[2..., 1]
mat b = inv(XX)*Xy
mat list b
reg price weight mpg,nohead       // 檢驗上述結果
* 方法2:結合使用 matrix accum 和 matrix vecaccum
mat accum XX = weight mpg
mat vecaccum yX = price weight mpg
mat Xy = yX'
mat b = inv(XX)*Xy
mat list b
reg price weight mpg, noheader   // 檢驗上述結果
* -eg2- 獲取變量的相關系數矩陣
sysuse auto, clear
corr price weight mpg length
* 加權交乘矩陣   -mat glsaccum-
matrix glsaccum A = varlist [if] [in] [weight], group(groupvar) glsmat(W|stringvar) row(rowvar) [noconstant]   // 基本語法
*-mat glsaccum 的定義:mat glsaccum(X) = S = X'BX
* 其中,B 為權重矩陣,定義如下:
*       [ W_1   0   ...   0  ]
*       |  0   W_2  ...   0  |
*   B = |  .    .    .    .  |    W_k(k=1,2,...,K) 表示第 k 組觀察值的權重矩陣,是一個方陣
*       |  .    .     .   .  |
*       [  0    0   ...  W_k ]
*  若 X 也根據組別定義,則可表示為:
*        [ X_1 ]
*        | X_2 | 
*    X = |  .  |
*        |  .  | 
*        [ X_k ]
*  由此可以更為細致的了解到 glsaccum 的定義方式:X'BX = X1'W1X1 + X2'W2X2 + ... + X_k'*W_k*X_k 
*- 應用舉例:White(1980) 異方差穩健性標准誤的計算
*  
*  Var(b) = inv(X'X)*(X'WX)*inv(X'X)  // White(1980)穩健性方差-協方差矩陣 
*
*  其中,
*  
*       [ e1^2   0    ...    0  ]
*       |  0    e2^2  ...    0  |
*   W = |  .     .     .     .  |
*       |  .     .     .     .  |
*       [  0     0    ...  eN^2 ]  NXN 矩陣
*
*  ei 表示第 i 個觀察值對應的殘差
*
*  問題的關鍵:求得 (X'WX) 矩陣即可,可采用 -mat glsaccum- 命令  
sysuse auto, clear
*-1 獲得OLS估計值
mat accum XX = weight mpg
mat vecaccum yX = price weight mpg
mat Xy = yX'
mat b = inv(XX)*Xy
mat list b
*-2 求取殘差之平方向量:e2
mkmat price, mat(y)
gen cons = 1
mkmat wei len mpg cons, mat(X) // 注意附加常數項
mat e = y - X*b                // 殘差向量

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM