我正在尝试使用tabulate
Stata 中的命令创建频率时间序列。当我尝试合并输出时,问题就出现了tabulate
运行完每个日期后。tabulate
当相关变量的值不存在观察值时,不会将 0 作为条目包含在内。例如,如果我想在三年内统计一个班级中 10、11 和 12 岁的学生,如果仅其中一组有代表,那么 Stata 可能会输出 (8),因此我们不知道哪一组是 8 名学生学生属于:可以是 (0,8,0) 或 (0,0,8)。
如果时间序列很短,这不是问题,因为“结果”窗口显示了哪些类别被代表或没有被代表。我的数据有更长的时间序列。有谁知道强制 Stata 在这些表格中包含零的解决方案/方法?我的代码的相关部分如下:
# delimit;
set more off;
clear;
matrix drop _all;
set mem 1200m;
cd ;
global InputFile "/Users/.../1973-2010.dta";
global OutputFile "/Users/.../results.txt";
use $InputFile;
compress;
log using "/Users/.../log.txt", append;
gen yr_mn = ym(year(datadate), month(datadate));
la var yr_mn "Year-Month Date"
xtset, clear;
xtset id datadate, monthly;
/*Converting the Ratings Scale to Numeric*/;
gen LT_num = .;
replace LT_num = 1 if splticrm=="AAA";
replace LT_num = 2 if (splticrm=="AA"||splticrm=="AA+"||splticrm=="AA-");
replace LT_num = 3 if (splticrm=="A"||splticrm=="A+"||splticrm=="A-");
replace LT_num = 4 if (splticrm=="BBB"||splticrm=="BBB+"||splticrm=="BBB-");
replace LT_num = 5 if (splticrm=="BB"||splticrm=="BB+"||splticrm=="BB-");
replace LT_num = 6 if (splticrm=="B"||splticrm=="B+"||splticrm=="B-");
replace LT_num = 7 if (splticrm=="CCC"||splticrm=="CCC+"||splticrm=="CCC-");
replace LT_num = 8 if (splticrm=="CC");
replace LT_num = 9 if (splticrm=="SD");
replace LT_num = 10 if (splticrm=="D");
summarize(yr_mn);
local start = r(min);
local finish = r(max);
forv x = `start'/`finish' {;
qui tab LT_num if yr_mn == `x', matcell(freq_`x');
};
log close;
你想要的不是一个选项tab
命令。如果您想将结果显示到屏幕上,您可以使用table ..., missing
成功地。
除了循环之外,您还可以尝试以下方法,我认为这将适合您的目的:
preserve
gen n = 1 // (n could be a variable that indicates if you want to include the row or not; or just something that never ==.)
collapse (count) n , by(LT_num yr_mn)
reshape wide n, i(yr_mn) j(LT_num)
mkmat _all , matrix(mymatname)
restore
mat list mymatname
我认为这就是你想要的(但无法告诉你如何使用你试图生成的矩阵)。
附:我更喜欢使用inlist
函数用于类似的事情:
replace LT_num = 2 if inlist(splticrm,"AA","AA+","AA-")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)