2012-01-09 10 views
1

tabulate twowayの5行5列のグリッドを次の表のように作成します。各サブテーブルを生成Stataの `tabulate twoway`頻度カウントテーブルの5行5列マトリックス

enter image description here

ネストさforeachループと簡単であるが、長いリストの出力は、5×5グリッドより解釈がより困難である(冗長エントリを有している - それは両方の半分を提供します対称行列の)。

Stataでこのようなテーブルを作成することはできますか?明確にするために、後でLaTeXを理解することができます。私は、明確かつ簡潔なコンソール出力を得ることに興味があります。

ありがとうございます! autoのデータを使用して基本を行うコードですが、マトリックスの代わりにリストを生成します。 xtileは、私はあなたのループにいくつかの行列演算を追加しましたegenmoreパッケージ

sysuse auto, clear 
global vars price mpg headroom trunk weight 
foreach x of global vars { 
    egen d_`x' = xtile(`x'), nquantiles(2) 
} 

* can make diagonal entries 
tabulate d_price d_price 

* can make off-diagonal entries 
tabulate d_price d_mpg 

* crude solution that generates list output with redundant entries 
foreach x of global vars { 
    foreach y of global vars { 
     tabulate d_`x' d_`y' 
    } 
} 

答えて

3

からです。

tempname col all tabout 
foreach x of global vars { 
    foreach y of global vars { 
     qui tabulate d_`x' d_`y', matcell(`tabout') 
     mat colnames `tabout' = `x' `x' 
     mat rownames `tabout' = `y' `y' 
     mat `col' = (nullmat(`col') \ `tabout') 
    } 
    mat `all'= (nullmat(`all') , `col') 
    mat drop `col' 
} 
mat list `all' 

それはそれをしないhttp://code.google.com/p/kk-adofiles/

+0

で、meantabという名前の私のプログラムに少し似ています!私はスタータ変換(RとMatlabから)です。Stataの行列言語は、誰かが適切な視点を示すまで、非常に困難です。ありがとう! –

+0

MatlabからStataに来る場合は、Stataの "mata"機能をチェックしてください。それはほぼ同じです。 – Keith

関連する問題