私はによって作成されたデータがあるとし、次のは、マトリックス中のタブの結果をソートする方法
clear all
set obs 150
set seed 1234
foreach i in 1 2 {
gen year`i' = round(runiform()*4)
tostring year`i', replace
replace year`i' = "Super Low" if year`i'=="0"
replace year`i' = "Kinda Low" if year`i'=="1"
replace year`i' = "Average to Mediocre" if year`i'=="2"
replace year`i' = "Pretty High" if year`i'=="3"
replace year`i' = "Incredibly High" if year`i'=="4"
}
私は最終的にの割合で周波数が、パーセンテージ、および相違点を提示し、ラテックス中のテーブルを作成したいですこれらの2つの変数。重要なことは、私はこれらの線に沿って1
それは難しい私が予想より行うことを見つける、私は次のコードを思いついた(感謝https://www.statalist.org/forums/forum/general-stata-discussion/general/1124796-any-way-to-save-row-percentages-output-as-a-matrix) :
label define order 1 "Pretty High" 2 "Average to Mediocre" 3 "Kinda Low" 4 "Incredibly High" 5 "Super Low"
foreach i in 1 2 {
encode year`i', gen(y`i'_freq) label(order)
tab y`i'_freq, matcell(y`i'_freq)
mata: st_matrix("y`i'_pct", (st_matrix("y`i'_freq") :/ colsum(st_matrix("y`i'_freq"))))
}
matrix combined = y1_freq, y1_pct
foreach i in 2 {
matrix combined = combined, y`i'_freq, y`i'_pct
}
mata: st_matrix("c", (st_matrix("combined"), st_matrix("combined")[.,2] - st_matrix("combined")[.,4]))
matrix rownames c = "Pretty High" "Average to Mediocre" "Kinda Low" "Incredibly High" "Super Low"
matrix colnames c = "No. 1 Freq" "No. 1 Pct" "No. 2 Freq" "No. 2 Pct" "Difference"
esttab matrix(c), nomtitles
上記の問題は、変数のソートをハードコードしていることです。自動的に行われるようにこれを一般化するにはどうすればよいですか?
私のコードを改善するための他のヒントもありがとうございます。
'matrix combined = y1_freq、y1_pct、y2_freq、y2_pct'は3行を節約します。 –