首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Stata:生成一个循环,将回归输出保存为新变量

Stata:生成一个循环,将回归输出保存为新变量
EN

Stack Overflow用户
提问于 2021-03-09 17:41:52
回答 1查看 89关注 0票数 0

我有微观数据,我正在对行业假人的工资进行回归分析。

然后,我的回归输出包括每个行业的系数,我想将其保存为一个名为wd (工资差异)的新变量。

下面的代码举例说明了我想要做的事情,但实际上我有数百个行业和近30年的历史。

我怎么才能做一个高效的循环呢?

代码语言:javascript
复制
reg lnwage i.industry if year == 2002

gen wd = 0

replace wd = 0 if industry==1 & year==2002
replace wd = _b[2.industry] if industry==2 & year==2002
replace wd = _b[3.industry] if industry==3 & year==2002
replace wd = _b[4.industry] if industry==4 & year==2002
replace wd = _b[5.industry] if industry==5 & year==2002
replace wd = _b[6.industry] if industry==6 & year==2002
replace wd = _b[7.industry] if industry==7 & year==2002
replace wd = _b[8.industry] if industry==8 & year==2002
replace wd = _b[9.industry] if industry==9 & year==2002
replace wd = _b[10.industry] if industry==10 & year==2002
replace wd = _b[11.industry] if industry==11 & year==2002
replace wd = _b[12.industry] if industry==12 & year==2002
replace wd = _b[13.industry] if industry==13 & year==2002
replace wd = _b[14.industry] if industry==14 & year==2002
replace wd = _b[15.industry] if industry==15 & year==2002
EN

回答 1

Stack Overflow用户

发布于 2021-03-09 22:15:55

最有效的循环是根本没有循环。

考虑这个例子。对于只有一个因子变量的回归,predict几乎可以直接完成您想要的所有功能。

代码语言:javascript
复制
. sysuse auto, clear
(1978 Automobile Data)

. regress price i.rep78

      Source |       SS           df       MS      Number of obs   =        69
-------------+----------------------------------   F(4, 64)        =      0.24
       Model |  8360542.63         4  2090135.66   Prob > F        =    0.9174
    Residual |   568436416        64     8881819   R-squared       =    0.0145
-------------+----------------------------------   Adj R-squared   =   -0.0471
       Total |   576796959        68  8482308.22   Root MSE        =    2980.2

------------------------------------------------------------------------------
       price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
       rep78 |
          2  |   1403.125   2356.085     0.60   0.554    -3303.696    6109.946
          3  |   1864.733   2176.458     0.86   0.395    -2483.242    6212.708
          4  |       1507   2221.338     0.68   0.500    -2930.633    5944.633
          5  |     1348.5   2290.927     0.59   0.558    -3228.153    5925.153
             |
       _cons |     4564.5   2107.347     2.17   0.034     354.5913    8774.409
------------------------------------------------------------------------------

. predict foo
(option xb assumed; fitted values)
(5 missing values generated)

. tabdisp rep78, c(foo)

-------------------------
Repair    |
Record    |
1978      | Fitted values
----------+--------------
        1 |        4564.5
        2 |      5967.625
        3 |      6429.233
        4 |        6071.5
        5 |          5913
        . |              
-------------------------

有多种方法可以继续进行,一种是直接减去截距,另一种是对截距进行一些变化。

代码语言:javascript
复制
. egen reference = mean(cond(rep78 == 1, foo, .))

. replace foo  = foo  - reference
(69 real changes made)

. tabdisp rep78, c(foo)

循环多年确实意味着循环多年,但请参阅Statalist帖子中的许多方法来直接做到这一点。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66544392

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档