【问题标题】:Regression by year and companyID to save coefficients按年份和 companyID 回归以保存系数
【发布时间】:2017-04-29 10:16:08
【问题描述】:

我正在尝试按 companyID 和年份运行回归,并将每个公司-年份模型的系数作为新变量保存在其他列旁边的新列中。还有一个额外的皱纹‹我有 1990-2010 年的面板数据,并且希望仅使用 t 到 t-4 运行每个回归(即,对于 2001 年,仅使用 1998-2001 年的数据,即对于 1990 年,那么只有1990 年等)。我是使用 foreach 循环的新手,我在网上找到了一些先前的编码。我试图让它适应我的情况,但有两个问题:任何.....

  1. 输出保持空白

  2. 我还没弄清楚如何使用滚动的四年数据周期。

这是我尝试过的代码。任何建议将不胜感激。

use paneldata.dta // the dataset I am working in
generate coeff . //empty variable for coefficient
foreach x of local levels {
forval z = 1990/2010
{
capture reg excess_returns excess_market
replace coeff = _b[fyear] & _b[CompanyID] if e(sample) }
}

所以下面是数据的简短快照;

CompanyID Re_Rf Rm-Rf Year
10 2 2 1990 
10 3 2 1991 
15 3 2 1991 
15 4 2 1992
15 5 2 1993 
21 4 2 1990 
21 4 2 1991 
34 3 1 1990 
34 3 1 1991
34 4 1 1992
34 2 1 1993  
34 3 1 1994
34 4 1 1995
34 2 1 1996   

 

Re_Rf = excess_returns 

Rm_Rf = excess_market 

我想运行以下回归:​​​​​​​​

reg excess_returns excess_market

【问题讨论】:

标签: regression stata finance


【解决方案1】:

Statalist 上有一个很好的讨论,但我认为这个答案可能有助于您了解循环以及 Stata 语法的工作原理。

我将使用的代码如下:

generate coeff = . //empty variable for coefficient

// put the values of gvkey into a local macro called levels
qui levelsof CompanyID, local(levels)

foreach co of local levels {
    forval yr = 1994/2010 {

        // run the regression with the condition that year is between yr 
        // and yr-3 (which is what you write in your example)
        // and the CompanyID is the same as in the regression
        qui reg Re_Rf Rm_Rf if fyear <= `yr' & fyear >= `yr'-3 & CompanyID== `co'

        // now replace coeff equal to the coefficient on Rm_Rf with the same 
        // condiditions as above, but only for year yr
        replace coeff = _b[Rm_Rf] if fyear == `yr' & CompanyID == `co'
    }
}

如果您没有平衡面板,这是一件有潜在危险的事情。如果您对此感到担心,可以使用capture 或更改 fyear 循环以包含以下内容来处理它:

levelsof fyear if CompanyID == `co', local(yr_level)
foreach yr of `yr_level' { ...

【讨论】:

  • 这很好地展示了一种特定方法的逻辑,但很难看出 Robert Picard 在Statalist 上发布的rangestat 解决方案有任何优势,即一行代码!
  • 我完全同意,我认为这可能会有所帮助,因为 OP 说它们是 foreach 循环的新手
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-15
  • 2021-11-22
  • 2017-12-21
  • 1970-01-01
  • 2012-07-31
  • 2016-05-21
  • 2019-09-06
相关资源
最近更新 更多