我有微观数据,我正在对行业假人的工资进行回归分析。
然后,我的回归输出包括每个行业的系数,我想将其保存为一个名为wd (工资差异)的新变量。
下面的代码举例说明了我想要做的事情,但实际上我有数百个行业和近30年的历史。
我怎么才能做一个高效的循环呢?
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发布于 2021-03-09 22:15:55
最有效的循环是根本没有循环。
考虑这个例子。对于只有一个因子变量的回归,predict几乎可以直接完成您想要的所有功能。
. 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
. |
-------------------------有多种方法可以继续进行,一种是直接减去截距,另一种是对截距进行一些变化。
. egen reference = mean(cond(rep78 == 1, foo, .))
. replace foo = foo - reference
(69 real changes made)
. tabdisp rep78, c(foo)循环多年确实意味着循环多年,但请参阅Statalist帖子中的许多方法来直接做到这一点。
https://stackoverflow.com/questions/66544392
复制相似问题