【问题标题】:How to Sort Results of Tab in Matrix如何对矩阵中选项卡的结果进行排序
【发布时间】:2017-08-28 18:10:11
【问题描述】:

假设我有以下创建的数据:

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"
}

我最终想在 LaTeX 中创建一个表格,显示这两个变量的频率、百分比和百分比差异。重要的是,我想按第 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 将为您节省三行代码。

标签: matrix stata


【解决方案1】:

这是@Andrey Ampilogov 先前发布的答案的变体。

* sandbox code from OP 
clear all
set obs 150
set seed 1234
foreach i in 1 2 {
    gen year`i' = round(runiform()*4)
}

preserve 

stack year1 year2, into(year) clear 
contract year _stack, f(freq) p(percent) 
reshape wide freq percent, i(year) j(_stack) 

* define labels once when needed 
label define year 0 "Super Low"      ///
    1 "Kinda Low"  2 "Average to Mediocre"   ///
    3 "Pretty High"  4 "Incredibly High" 
label val year year 

gsort -freq1
list 

     +-----------------------------------------------------------+
     |                year   freq1   percent1   freq2   percent2 |
     |-----------------------------------------------------------|
  1. |           Kinda Low      39      13.00      27       9.00 |
  2. |         Pretty High      37      12.33      33      11.00 |
  3. | Average to Mediocre      29       9.67      44      14.67 |
  4. |     Incredibly High      24       8.00      23       7.67 |
  5. |           Super Low      21       7.00      23       7.67 |
     +-----------------------------------------------------------+

  restore 

我要强调的技术要点是

  1. 当您可以保留整数并在方便时附加值标签时,将整数值转换为字符串似乎不是一个更好的主意。您必须查看原始定义才能恢复订单信息。

  2. merge m:m 得到 Stata 的支持,但即使在它工作的时候也是矫枉过正。不需要详细的文件编排。

  3. 对我来说,在此类问题中,百分比以 0 和 100 为界。但是使用正确的数据结构,按比例缩放和计算差异很容易。

【讨论】:

  • 谢谢你的更多澄清,尼克。这是了解-stack- 命令的好方法
【解决方案2】:

我建议使用涉及两年的contractmerge 的更简单的解决方案。在您的初始代码运行后:

foreach i in 1 2 {
    preserve
    contract year`i', f(freq`i') p(pct`i')
    tempfile year`i'
    save `year`i''
    restore
}

use `year1', clear
ren year1 year2
merge m:m year2 using `year2', nogen
ren year2 type
gsort -freq1
replace pct1 = pct1/100
replace pct2 = pct2/100
gen diff = pct1 - pct2
list, clean

这会给你一个结果:

                      type   freq1   pct1   freq2   pct2        diff  
  1.             Kinda Low      39   0.26      27   0.18         .08  
  2.           Pretty High      37   0.25      33   0.22    .0266667  
  3.   Average to Mediocre      29   0.19      44   0.29         -.1  
  4.       Incredibly High      24   0.16      23   0.15    .0066667  
  5.             Super Low      21   0.14      23   0.15   -.0133333  

备注:

contract 清除当前数据集并创建具有频率和百分比的数据集year'i'。数据集被保存到临时文件中,以保持文件系统清洁,而不必担心删除文件。

然后第一个数据集与第二个数据集合并。仅保留来自第二个数据集的频率和百分比。

降序排序是通过gsort -freq1 命令完成的。要按升序排序,请运行gsort freq1

【讨论】:

  • 谢谢——这比我的简单多了!
  • 此时,将当前数据集导出到LaTeX最简单的方法是什么?
  • 很高兴它成功了!有一个包-dataout- 应该可以完成这项工作。运行这些以将数据集导出到 LaTeX:ssc install dataoutdataout, save(myfile) tex replace
  • 不错的答案,但创建数据的完整代码会使其变得更好。
猜你喜欢
  • 1970-01-01
  • 2020-11-04
  • 2013-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多