2016-07-04 11 views
-1
私は次のコマンドを使用して二つの行列を作成している

別で1つの行列を分割する方法:のStata

tab year region if x==1, matcell(cases) 
tab year region, matcell(total) 

両行列は9行10列です。 は、私は今
に等しいパーセントで新しいマトリックスを作成します:P =(例/合計)* 100

をしかし、私はStataの中でそうするコマンドを見つけることができませんか?簡単な方法はありますか?

+0

再現可能な例を投稿してください。 http://stackoverflow.com/help/mcveについて説明します。 –

答えて

3

私たちは実験のためにあなた自身のデータを持っていませんので、このアナログを考慮してください。

最初のテーブルを2番目のテーブルのパーセントとして使用します。私たちが最初に示したいものを計算するだけで、そこに直接行くことは簡単です。

. webuse nlsw88, clear 
(NLSW, 1988 extract) 

. tab race collgrad if married == 1 

      | college graduate 
     race | not colle college g |  Total 
-----------+----------------------+---------- 
    white |  862  288 |  1,150 
    black |  224   50 |  274 
    other |  12   6 |  18 
-----------+----------------------+---------- 
    Total |  1,098  344 |  1,442 


. tab race collgrad 

      | college graduate 
     race | not colle college g |  Total 
-----------+----------------------+---------- 
    white |  1,217  420 |  1,637 
    black |  480  103 |  583 
    other |  17   9 |  26 
-----------+----------------------+---------- 
    Total |  1,714  532 |  2,246 


. egen toshow = mean(100 * (married == 1)), by(race collgrad) 

. tabdisp race collgrad, c(toshow) format(%2.1f) 

---------------------------------------------- 
      |   college graduate   
    race | not college grad  college grad 
----------+----------------------------------- 
    white |    70.8    68.6 
    black |    46.7    48.5 
    other |    70.6    66.7 
---------------------------------------------- 

しかし、行列の操作方法を見てみましょう。あなたが求めているのはではなく、 が要素単位であるため、通常は線形代数で理解されるように、の行列除算であることに注意してください。これを行う最も簡単な方法は、今すぐMataを呼び出すことです。

quietly tab race collgrad if married == 1, matcell(num) 
quietly tab race collgrad, matcell(den) 
mata : st_matrix("wanted", 100 * st_matrix("num") :/ st_matrix("den")) 
mat li wanted 

wanted[3,2] 
      c1   c2 
r1 70.82991 68.571429 
r2 46.666667 48.543689 
r3 70.588235 66.666667 

、それだけではStataの中で行うことがより厄介だと注意が、一度インストールmatewdと完全に可能。しかしStata 9はMataが1行の解決策を許して以来、Stataは「歴史的」なようなプログラムをかなり公表している。 matewdは、簡略要約には記載されていませんが、dm69に含まれています。

. search dm69, entry historical 


STB-50 dm69 . . . . . . . . . . . . . . . . . . Further new matrix commands 
     (help matdelrc, matewm, matmad, matpow if installed) . . . N. J. Cox 
     7/99 pp.5--9; STB Reprints Vol 9, pp.29--34 
     collection of new matrix commands providing additional matrix 
     checking, management, element-wise operators, maximum absolute 
     difference, and power 
+2

それで、あなた自身のデータセットに翻訳してください。あなたの質問は 'x'と書いてあることに注意してください。今あなたは「セックス」と言う。原則は同じです。 –