1

いくつかの国にまたがるデータのパネル (企業年数) があります。logit国ごとに、最初の 5 年間を使用してモデルを推定し、このモデルを使用してpredict、その後の数年間の確率を計算します。私foreachは国をforvaluesループし、その後の年をループします。

最初の数カ国は (推定と予測の両方で) うまく機能しますが、5 番目の国の最初の標本外予測は次のように失敗します。

Country: United Kingdom
Year: 1994
too many variables specified
r(103);

モデルは適合し、1994 年には確率を予測するのに十分なデータがあります。私のpredict電話は:

predict temp_`c'`y' ///
    if (country == "`c'") ///
        & (fyear == `y'), ///
    pr

このエラーの原因は何ですか? 同じループの他の場所で作業しているためlogit、私は混乱しています。predictありがとう!

FWIW、これが .do ファイルです。

* generate table 5 from Denis and Osobov (2008 JFE)
preserve

* loop to estimate model by country
levelsof country, local(countries)
foreach c of local countries {
    display "Country: `c'"
    summarize fyear if (country == "`c'"), meanonly
    local est_low = `r(min)'
    local est_high = `=`r(min)' + 4'
    local pred_low = `=`r(min)' + 5'
    local pred_high = `r(max)'
    logit payer size v_a_tr e_a_tr re_be_tr ///
        if (country == "`c'") ///
            & inrange(fyear, `est_low', `est_high')
    forvalues y = `pred_low'/`pred_high' {
        display "Country: `c'"
        display "Year: `y'"
        predict temp_`c'`y' ///
            if (country == "`c'") ///
                & (fyear == `y'), ///
            pr
    }
}

* combine fitted values and generate delta
egen payer_expected = rowfirst(temp_*)
drop temp_*
generate delta = payer - payer_expected

* table
table country fyear, ///
    contents(count payer mean payer mean payer_expected)

*
restore    

更新: Idrop (country == "United Kingdom")の場合、同じ問題が米国 (パネルの次と最後の国) に移行します。Idrop inlist(country, "United Kingdom", "United States")の場合、問題はなくなり、.do ファイルが実行されます。

4

2 に答える 2

2

作成する新しい変数名の一部として国名を使用していますpredict。しかし、あなたが「イギリス」に着くとき、あなたのライン

predict temp_`c'`y'

のようなものを意味します

predict temp_United Kingdom1812 

しかし、Stataは、これを1つだけが許可される2つの変数名と見なしています。

別の言い方をすれば、あなたは単純なルールに噛まれています:Stataは変数名内にスペースを許可しません。

明らかに同じ問題が「米国」にかみつくでしょう。

最も簡単なファッジは、スペースがアンダースコア「_」になるように値を変更することです。アンダースコアを含む変数名でStataはOKです。それは可能性があります

gen country2 = subinstr(country, " ", "_", .) 

ループオーバーが続きますcountry2

歴史的な詳細に触れていないすべての人に注意してください。1812年は、イギリス軍がホワイトハウスを全焼させた年です。「1776」またはその他の選択した日付に置き換えてください。

(ちなみに、非常に明確な質問の功績です!)

于 2013-02-12T12:58:53.547 に答える
1

これがあなたの問題に対する別のアプローチです。予測値を保持するように変数を初期化します。次に、可能性をループするとreplace、予測の各セットでチャンクごとにチャンクします。これにより、長期的に保持したくないさまざまな名前の変数の束を生成するというビジネス全体が回避されます。

* generate table 5 from Denis and Osobov (2008 JFE)

preserve
gen payer_expected = . 

* loop to estimate model by country
levelsof country, local(countries)
foreach c of local countries {
    display "Country: `c'"
    summarize fyear if (country == "`c'"), meanonly
    local est_low = `r(min)'
    local est_high = `=`r(min)' + 4'
    local pred_low = `=`r(min)' + 5'
    local pred_high = `r(max)'
    logit payer size v_a_tr e_a_tr re_be_tr ///
       if (country == "`c'") ///
       & inrange(fyear, `est_low', `est_high')
    forvalues y = `pred_low'/`pred_high' {
        display "Country: `c'"
        display "Year: `y'"
        predict temp ///
            if (country == "`c'") ///
            & (fyear == `y'), pr
        quietly replace payer_expected = temp if temp < . 
        drop temp 
   }
}

generate delta = payer - payer_expected

* table
table country fyear, ///
     contents(count payer mean payer mean payer_expected)

*
restore    
于 2013-02-12T14:08:02.173 に答える