3

tabulateStata でコマンドを使用して、時系列の周波数を作成しようとしています。tabulate各日付を実行した後の出力を結合しようとすると、問題が発生します。tabulate問題の変数の値に対する観測が存在しない場合、エントリとして 0 は含まれません。たとえば、あるクラスの 10 歳、11 歳、12 歳の人数を 3 年間にわたって数えたい場合、Stata は (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;
4

3 に答える 3

2

この問題は、によって対処されtabcountます。2003年の論文を参照

http://www.stata-journal.com/article.html?article=pr0011

でリンクを取得した後、プログラム コードとヘルプ ファイルをダウンロードしますsearch tabcount

于 2013-01-31T14:42:12.803 に答える
2

あなたが望むのは、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

それがあなたが求めていることだと思います(ただし、生成しようとしている行列をどのように使用するかはわかりません)。

PS私は次のinlistようなものに関数を使用することを好みます:

replace LT_num = 2 if inlist(splticrm,"AA","AA+","AA-")
于 2011-02-15T17:28:32.447 に答える
0

これは私が使用したソリューションです。おそらく Keith の方が優れていると思います。

行ラベルを (matrow を使用して) ベクトルに保存し、ゼロに初期化された正しい次元の行列のインデックスとして使用しました。そうすれば、各周波数をマトリックスの正しい場所に配置し、すべてのゼロを保持できます。解決策は、「local finish=r(max)」の後に上記のコードに従います。[この変数に対して空の最初の観測を排除するためにカウンターを含めることに注意してください。]

local counter=0;
forv x = `first'/`last' {;
tab LT_num if yr_mn == `x', matrow(index_`x') matcell(freq_`x');
local rows = r(r); /*r(r) is number of rows for tabulate*/;

if `rows'!=0{;
    matrix define A_`x'=J(10,1,0);
    forv r=1/`rows'{;
        local a=index_`x'[`r',1];
        matrix define A_`x'[`a',1]=freq_`x'[`r',1];
    };
};
else {;
    local counter=`counter'+1;
};
};   


local start=`first'+`counter'+1;
matrix define FREQ = freq_`start';

forv i = `start'/`last' {;
    matrix FREQ = (FREQ,A_`i');
};
于 2011-02-16T18:34:42.543 に答える