下面我在 Stata 中的代码汇总了各个事件研究回归的估计值,以计算所有治疗的平均效果。平均效应在概念上等同于堆叠回归的输出,但具有能够使用自定义权重来计算更有效的平均值的优点。然而,代码只给出了每个结果的系数和标准误差。我还想找到 p 值来确定统计显着性。由于我不想大幅修改我的代码,我该怎么做?
或者,如何在给定系数和标准误差的情况下手动计算 p 值?
////////////////////////////////////////////////
//// ////
//// PART 1. Define regression program ////
//// ////
////////////////////////////////////////////////
capture program drop runEventStudies
program define runEventStudies
args data yList name
use "${outdir}\\`data'.dta", clear
* group indicator. So it's 1 both before and after the event - Luca mail 09/06
bysort state: egen treated = max(stepone)
egen eventBlock = group(Treated_State adopt_week)
qui gen post = weeknum > adopt_week //should turn on for the controls based on the state
*for which the reg is run - aka eventblock
*qui gen event = weeknum == adopt_week
*qui gen treated_event = treated * event
qui gen treated_post = treated * post
qui gen treated_trend = weeknum * treated
qui sum eventBlock
local N = r(max)
disp "TOTAL: `N'"
* Run regressions - include test rates
foreach y of local yList {
disp "`y'"
forvalues i = 1/`N' {
disp "`i'"
* Balanced T, with FEs to control for week
areg `y' post treated_post tested i.weeknum if eventBlock == `i' ///
,absorb(state) //mine is not a balanced panel - restrict analysis to make it balanced
local b_`y'_`i'_b = _b[treated_post]
local se_`y'_`i'_b = _se[treated_post]
* Adding linear, group-specific trend
qui areg `y' weeknum treated_trend post treated_post tested i.weeknum ///
if eventBlock == `i',absorb(state)
local b_`y'_`i'_b_trend = _b[treated_post]
local se_`y'_`i'_b_trend = _se[treated_post]
}
}
*** Step 2: Construct a dataset saving all coefficients and SEs
*keep Treated_State adopt_week eventBlock cem_varlist
qui duplicates drop
foreach suff in b b_trend {
foreach y of local yList {
qui gen b_`y'_`suff' = ""
qui gen se_`y'_`suff' = ""
forvalues i = 1/`N' {
qui replace b_`y'_`suff' = "`b_`y'_`i'_`suff''" if eventBlock == `i'
qui replace se_`y'_`suff' = "`se_`y'_`i'_`suff''" if eventBlock == `i'
}
qui destring b_`y'_`suff', replace
qui destring se_`y'_`suff', replace
}
}
drop eventBlock
* Save the data
foreach x in Treated_State {
encode `x', gen(temp)
drop `x'
rename temp `x'
}
* save
order state weeknum cem_varlist
sort state weeknum
save "${coeffdir}\coefficients_`name'.dta", replace
end
////////////////////////////////////////////////////////////////////////////////
//////////// ////////////
//////////// PART 2. Run the event studies ////////////
//////////// ////////////
////////////////////////////////////////////////////////////////////////////////
* Acquired
runEventStudies "1matched_cohort" /// Data
`"confirmed_1 recovered_1 deceased_1"' /// yList
"one" /// name
////////////////////////////////////////////////////////////////////////////////
//////////// ////////////
//////////// PART 3. Calculate averages ////////////
//////////// ////////////
////////////////////////////////////////////////////////////////////////////////
* Uses Inverse variance average: these weights give the smallest SE of the sum of the vars
local oneVars "confirmed_1 recovered_1 deceased_1"
local twoVars "confirmed_2 recovered_2 deceased_2"
local threeVars "confirmed_3 recovered_3 deceased_3"
local fourVars "confirmed_4 recovered_4 deceased_4"
local sets `" "" "_trend" "'
foreach set of local sets {
* open file, write header
file open EScoeff using "${paperdir}\Tables\EScoeff`set'.csv", write replace
file write EScoeff "Variable,All states,Early adopters,Late adopters" _n
foreach sample in one two three four {
use "${coeffdir}\coefficients_`sample'.dta", replace
* Average effect of all states
foreach var of local `sample'Vars {
* ALL
qui egen wavg_`var' = wtmean(b_`var'_b`set') ///
if treated == 1, weight(1/(se_`var'_b`set'^2))
qui egen wvar_`var' = mean(1/(se_`var'_b`set'^2)) /// SE command
if treated == 1
qui replace wvar_`var' = sqrt(1/wvar_`var') ///follows from 206
* record values
sum wavg_`var'
local avgAllStates : display %4.2f r(mean)
drop wavg_`var'
sum wvar_`var'
local sdAllStates : display %4.2f r(mean)
drop wvar_`var'
/*
file write testtable "`var',`avgAllStates'" _n
file write testtable `",="(`sdAllStates')""' _n
*/
file write EScoeff "`var',`avgAllStates',`avgEarlyStates',`avgLateStates'" _n
file write EScoeff `",="(`sdAllStates')",="(`sdEarlyStates')",="(`sdLateStates')""' _n
}
}
file close EScoeff
}
数据样本
input str50 state byte stepone str13 Treated_State byte(weeknum adopt_week) float(tested confirmed_1 recovered_1 deceased_1)
"Bihar" 1 "Bihar" 45 22 16.280634 .21531174 .21075015 .001205656
"Bihar" 1 "Bihar" 32 22 7.18386 .1659875 .1561607 .00080393
"Bihar" 1 "Bihar" 41 22 13.814168 .20439556 .19904032 .0011118283
"Bihar" 1 "Bihar" 44 22 15.731228 .21312033 .2083853 .001183663
"Bihar" 0 "Bihar" 17 22 .1847392 .008343254 .006337254 .0000580895
"Bihar" 1 "Bihar" 38 22 11.61945 .19399765 .18851824 .0010309094
"Bihar" 1 "Bihar" 29 22 5.068347 .14344008 .13148248 .0007292264
"Bihar" 0 "Bihar" 16 22 .1419289 .006759179 .005025698 .00004458309
"Bihar" 1 "Bihar" 43 22 15.157097 .21073844 .20558017 .0011626267
"Bihar" 1 "Bihar" 26 22 2.7848125 .11582927 .1013988 .0005929671
"Bihar" 1 "Bihar" 39 22 12.378263 .1976383 .19192722 .0010621055
"Bihar" 1 "Bihar" 23 22 1.0034382 .07272435 .0476965 .0003849923
"Bihar" 0 "Bihar" 11 22 .04245099 .001358888 .000448341 7.53012e-06
"Bihar" 1 "Bihar" 31 22 6.606687 .159154 .14880905 .0007728533
"Bihar" 1 "Bihar" 47 22 17.28098 .2176378 .21473382 .001243187
"Bihar" 1 "Bihar" 34 22 8.671193 .17866656 .16996786 .00088963
"Bihar" 0 "Bihar" 4 22 0 .000017450755 7.171544e-07 7.171544e-07
"Bihar" 0 "Bihar" 13 22 .06862139 .003441026 .0015243116 .00002067795
"Bihar" 0 "Bihar" 21 22 .4104603 .036436457 .02434966 .00022243737
"Bihar" 1 "Bihar" 22 22 .58980757 .05235824 .033925343 .0002959457
"Bihar" 0 "Bihar" 20 22 .3337463 .024219856 .015814569 .00016518455
"Bihar" 1 "Bihar" 48 22 5.016259 .06231139 .06166022 .0003585772
"Bihar" 1 "Bihar" 28 22 4.260896 .13483803 .12253084 .0007003012
"Bihar" 0 "Bihar" 10 22 .030944014 .000708907 .0003206875 5.259132e-06
"Bihar" 0 "Bihar" 9 22 .023821475 .00044607 .00013972557 3.5857715e-06
"Bihar" 0 "Bihar" 5 22 .0021611445 .000034781988 8.964429e-06 8.3668e-07
"Bihar" 1 "Bihar" 37 22 10.968039 .190893 .1850546 .0010001912
"Bihar" 0 "Bihar" 3 22 0 3.34672e-06 0 0
"Bihar" 0 "Bihar" 15 22 .11010375 .005678189 .0037403186 .0000334672
"Bihar" 0 "Bihar" 8 22 .01659794 .0003028782 .00005581851 1.7928858e-06
"Bihar" 0 "Bihar" 6 22 .006518694 .00005976286 .00002402467 8.3668e-07
"Bihar" 1 "Bihar" 46 22 16.790081 .21676815 .2128226 .0012233458
"Bihar" 1 "Bihar" 25 22 2.1707711 .10481963 .08708943 .0005343995
"Bihar" 1 "Bihar" 40 22 13.10717 .20105493 .19557564 .0010884013
"Bihar" 0 "Bihar" 18 22 .2263394 .010706636 .007791762 .00008283133
"Bihar" 0 "Bihar" 12 22 .04013124 .0024207544 .0007249235 .000011235418
"Bihar" 1 "Bihar" 42 22 14.507592 .20766734 .2023336 .0011390802
"Bihar" 0 "Bihar" 19 22 .27508557 .015854968 .01087182 .00012155766
"Bihar" 1 "Bihar" 30 22 5.962443 .15187284 .14039527 .0007506215
"Bihar" 1 "Bihar" 33 22 7.866549 .1729872 .1631078 .0008461226
"Bihar" 1 "Bihar" 35 22 9.480743 .18312895 .1762636 .0009274
"Bihar" 1 "Bihar" 24 22 1.5934727 .09181153 .06732514 .00046399885
"Bihar" 1 "Bihar" 36 22 10.247828 .18750395 .1810866 .000966963
"Bihar" 0 "Bihar" 7 22 .01026487 .0001143861 .000035618665 1.67336e-06
"Bihar" 1 "Bihar" 27 22 3.595409 .12605326 .112047 .000643885
"Bihar" 0 "Bihar" 14 22 .08867207 .0045832135 .0023129422 .000027012813
"Tamil Nadu" 0 "Bihar" 31 22 10.399572 .8328966 .7599111 .01310164
"Kerala" 0 "Bihar" 32 22 10.481126 .8627852 .5889338 .0029799694
"DNHnDD" 0 "Bihar" 31 22 7.098346 .3239088 .3102786 .00020855057
"DNHnDD" 0 "Bihar" 45 22 0 .3512737 .3472516 .00020855057
"Goa" 0 "Bihar" 5 22 .006474954 .0004545455 9.276439e-06 0
"Andhra Pradesh" 0 "Bihar" 29 22 10.027867 1.2246617 1.0736066 .01045064
"Telangana" 0 "Bihar" 19 22 .5291103 .10167153 .06675328 .0010090581
"Andhra Pradesh" 0 "Bihar" 19 22 2.2918196 .06419585 .0333213 .000790049
"Tripura" 0 "Bihar" 28 22 8.414711 .49370885 .3015925 .00518895
"Himachal Pradesh" 0 "Bihar" 2 22 0 3.9138945e-06 0 0
"Uttar Pradesh" 0 "Bihar" 44 22 10.940186 .2620493 .25282785 .003746318
"Kerala" 0 "Bihar" 29 22 7.141237 .4114534 .28997296 .0016374175
"Andhra Pradesh" 0 "Bihar" 14 22 .922762 .00960916 .005391099 .0001471767
"DNHnDD" 0 "Bihar" 28 22 6.201341 .29074928 .26459107 .00020855057
"DNHnDD" 0 "Bihar" 19 22 3.864889 .05648741 .03478326 .0001489647
"Gujarat" 0 "Bihar" 8 22 .08439687 .005654902 .0006937201 .0002678992
"DNHnDD" 0 "Bihar" 26 22 5.691241 .25045434 .21994637 .00020855057
"Tamil Nadu" 0 "Bihar" 17 22 1.5489976 .11930643 .06695649 .0015883292
"Himachal Pradesh" 0 "Bihar" 12 22 .4074325 .003340509 .0009080234 .0000665362
"Tripura" 0 "Bihar" 13 22 .7343115 .01232107 .004330089 0
您可以组合来自单独回归模型的估计,如下所示:
// Load the 'auto' dataset into memory, clearing any existing data in memory
sysuse auto, clear
// Create a new variable 'terc_mpg' that divides 'mpg' (miles per gallon) into tertiles (3 equal groups)
xtile terc_mpg = mpg, nq(3)
// Run a regression analysis of 'price' on the tertile groups of 'mpg' and 'weight' for domestic cars (foreign == 0)
reg price i.terc_mpg c.weight if foreign == 0
// Store the estimates from the previous regression for later comparison, labeled as 'domestic'
estimates store domestic
// Repeat the regression analysis for foreign cars (foreign == 1)
reg price i.terc_mpg c.weight if foreign == 1
// Store these estimates as well, labeled as 'foreign'
estimates store foreign
// Perform a seemingly unrelated estimation (suest) to compare the 'domestic' and 'foreign' models
suest domestic foreign, coefl
// Use the 'lincom' command to compute a linear combination of coefficients,
// specifically a weighted average of the 'weight' variable's coefficients from the two models
lincom (_b[domestic_mean:weight]*0.75 + _b[foreign_mean:weight]*0.25)
不幸的是,这不适用于
areg
,因此您必须使用 reg
或 xtreg
作为您的 DID。