Dear All,
I am running a regression and want to add to the results table the mean of the dependent variable using the estadd command. My data is as follows:
So I want to run the following:
But, I get an error:
. local out "aotopioidr"
. qui sum `out' [weight=pop] if post==0
. local base=`r(mean)'
. eststo res1: reghdfe aotopioidr post DD [weight=pop], absorb(i.year i.countyFIPS) vce(
> cluster countyFIPS)
(analytic weights assumed)
(converged in 4 iterations)
note: post omitted because of collinearity
HDFE Linear regression Number of obs = 36,480
Absorbing 2 HDFE groups F( 1, 3039) = 14.08
Statistics robust to heteroskedasticity Prob > F = 0.0002
R-squared = 0.5533
Adj R-squared = 0.5125
Within R-sq. = 0.0149
Number of clusters (countyFIPS) = 3,040 Root MSE = 4.7271
(Std. Err. adjusted for 3,040 clusters in countyFIPS)
------------------------------------------------------------------------------
| Robust
aotopioidr | Coef. Std. Err. t P>|t| [95% Conf. Interval]
-------------+----------------------------------------------------------------
post | 0 (omitted)
DD | 39.58188 10.54944 3.75 0.000 18.89711 60.26664
------------------------------------------------------------------------------
Absorbed degrees of freedom:
---------------------------------------------------------------+
Absorbed FE | Num. Coefs. = Categories - Redundant |
-------------+-------------------------------------------------|
year | 12 12 0 |
countyFIPS | 0 3040 3040 * |
---------------------------------------------------------------+
* = fixed effect nested within cluster; treated as redundant for DoF computation
. estadd scalar Bsl `base'
added scalar:
e(Bsl) = 4.8985249
.
. qui sum `out' [weight=pop] if post==0 & DD2==1
. local base1=`r(mean)'
invalid syntax
r(198);
end of do-file
r(198);
Not sure why it runs the first time but gives invalid syntax the second time?
Thanks for your help.
Sincerely,
Sumedha.
I am running a regression and want to add to the results table the mean of the dependent variable using the estadd command. My data is as follows:
Code:
Code:* Example generated by -dataex-. To install: ssc install dataex clear input float(aotopioidr post DD DD1 DD2 DD3 DD4 pre_rate pop year) long(countyFIPS countyFIPS) 2.1078045 0 0 0 0 0 0 .014084662 51328 2006 1001 1001 3.556655 0 0 0 0 0 0 .014084662 52405 2007 1001 1001 1.7534662 0 0 0 0 0 0 .014084662 53277 2008 1001 1001 3.691462 0 0 0 0 0 0 .014084662 54135 2009 1001 1001 2.0714817 0 0 0 0 0 0 .014084662 54750 2010 1001 1001 5.795664 1 .014084662 0 0 1 0 .014084662 55199 2011 1001 1001 1.0835801 1 .014084662 0 0 1 0 .014084662 54927 2012 1001 1001 3.6981726 1 .014084662 0 0 1 0 .014084662 54695 2013 1001 1001 2.5682335 1 .014084662 0 0 1 0 .014084662 54864 2014 1001 1001 2.464126 1 .014084662 0 0 1 0 .014084662 54838 2015 1001 1001 4.845098 1 .014084662 0 0 1 0 .014084662 55278 2016 1001 1001 0 1 .014084662 0 0 1 0 .014084662 55504 2017 1001 1001 2.955111 0 0 0 0 0 0 .04371211 168121 2006 1003 1003 6.94116 0 0 0 0 0 0 .04371211 172404 2007 1003 1003 4.4962626 0 0 0 0 0 0 .04371211 175827 2008 1003 1003 7.301706 0 0 0 0 0 0 .04371211 179406 2009 1003 1003 8.162058 0 0 0 0 0 0 .04371211 183110 2010 1003 1003 6.958169 1 .04371211 0 0 0 1 .04371211 186534 2011 1003 1003 7.300091 1 .04371211 0 0 0 1 .04371211 190048 2012 1003 1003 3.1255424 1 .04371211 0 0 0 1 .04371211 194736 2013 1003 1003 9.019099 1 .04371211 0 0 0 1 .04371211 199064 2014 1003 1003 11.985537 1 .04371211 0 0 0 1 .04371211 202863 2015 1003 1003 7.903885 1 .04371211 0 0 0 1 .04371211 207509 2016 1003 1003 7.354195 1 .04371211 0 0 0 1 .04371211 212628 2017 1003 1003 1.3861238 0 0 0 0 0 0 .008630031 27861 2006 1005 1005 0 0 0 0 0 0 0 .008630031 27757 2007 1005 1005 0 0 0 0 0 0 0 .008630031 27808 2008 1005 1005 1.6909406 0 0 0 0 0 0 .008630031 27657 2009 1005 1005 0 0 0 0 0 0 0 .008630031 27332 2010 1005 1005 0 1 .008630031 0 1 0 0 .008630031 27351 2011 1005 1005 0 1 .008630031 0 1 0 0 .008630031 27175 2012 1005 1005 2.759785 1 .008630031 0 1 0 0 .008630031 26947 2013 1005 1005 1.5779904 1 .008630031 0 1 0 0 .008630031 26749 2014 1005 1005 0 1 .008630031 0 1 0 0 .008630031 26264 2015 1005 1005 3.92432 1 .008630031 0 1 0 0 .008630031 25774 2016 1005 1005 3.9572616 1 .008630031 0 1 0 0 .008630031 25270 2017 1005 1005 3.449423 0 0 0 0 0 0 .05500945 22099 2006 1007 1007 6.819527 0 0 0 0 0 0 .05500945 22438 2007 1007 1007 13.217328 0 0 0 0 0 0 .05500945 22705 2008 1007 1007 11.785565 0 0 0 0 0 0 .05500945 22941 2009 1007 1007 1.8566246 0 0 0 0 0 0 .05500945 22872 2010 1007 1007 1.6748624 1 .05500945 0 0 0 1 .05500945 22745 2011 1007 1007 3.925781 1 .05500945 0 0 0 1 .05500945 22658 2012 1007 1007 3.8865304 1 .05500945 0 0 0 1 .05500945 22503 2013 1007 1007 11.63334 1 .05500945 0 0 0 1 .05500945 22533 2014 1007 1007 5.207737 1 .05500945 0 0 0 1 .05500945 22561 2015 1007 1007 16.490114 1 .05500945 0 0 0 1 .05500945 22633 2016 1007 1007 9.930326 1 .05500945 0 0 0 1 .05500945 22668 2017 1007 1007 1.9686617 0 0 0 0 0 0 .02577892 55485 2006 1009 1009 4.039482 0 0 0 0 0 0 .02577892 56240 2007 1009 1009 8.328896 0 0 0 0 0 0 .02577892 57055 2008 1009 1009 6.383059 0 0 0 0 0 0 .02577892 57341 2009 1009 1009 3.290385 0 0 0 0 0 0 .02577892 57381 2010 1009 1009 0 1 .02577892 0 0 1 0 .02577892 57562 2011 1009 1009 9.612587 1 .02577892 0 0 1 0 .02577892 57595 2012 1009 1009 1.6251583 1 .02577892 0 0 1 0 .02577892 57623 2013 1009 1009 7.842396 1 .02577892 0 0 1 0 .02577892 57546 2014 1009 1009 5.209238 1 .02577892 0 0 1 0 .02577892 57590 2015 1009 1009 8.721302 1 .02577892 0 0 1 0 .02577892 57562 2016 1009 1009 13.07919 1 .02577892 0 0 1 0 .02577892 58013 2017 1009 1009 0 0 0 0 0 0 0 0 10776 2006 1011 1011 0 0 0 0 0 0 0 0 11011 2007 1011 1011 0 0 0 0 0 0 0 0 10953 2008 1011 1011 0 0 0 0 0 0 0 0 10987 2009 1011 1011 0 0 0 0 0 0 0 0 10880 2010 1011 1011 5.336122 1 0 1 0 0 0 0 10675 2011 1011 1011 0 1 0 1 0 0 0 0 10612 2012 1011 1011 0 1 0 1 0 0 0 0 10549 2013 1011 1011 3.019276 1 0 1 0 0 0 0 10673 2014 1011 1011 0 1 0 1 0 0 0 0 10419 2015 1011 1011 14.739713 1 0 1 0 0 0 0 10441 2016 1011 1011 0 1 0 1 0 0 0 0 10309 2017 1011 1011 4.804228 0 0 0 0 0 0 .007248664 20815 2006 1013 1013 0 0 0 0 0 0 0 .007248664 20894 2007 1013 1013 2.986412 0 0 0 0 0 0 .007248664 20949 2008 1013 1013 4.475471 0 0 0 0 0 0 .007248664 20867 2009 1013 1013 3.203039 0 0 0 0 0 0 .007248664 20944 2010 1013 1013 4.427414 1 .007248664 0 1 0 0 .007248664 20880 2011 1013 1013 2.0005171 1 .007248664 0 1 0 0 .007248664 20688 2012 1013 1013 0 1 .007248664 0 1 0 0 .007248664 20372 2013 1013 1013 2.1197255 1 .007248664 0 1 0 0 .007248664 20327 2014 1013 1013 3.5380616 1 .007248664 0 1 0 0 .007248664 20141 2015 1013 1013 4.2959642 1 .007248664 0 1 0 0 .007248664 19965 2016 1013 1013 0 1 .007248664 0 1 0 0 .007248664 19825 2017 1013 1013 1.4868804 0 0 0 0 0 0 .04249525 115388 2006 1015 1015 3.521547 0 0 0 0 0 0 .04249525 116211 2007 1015 1015 5.355203 0 0 0 0 0 0 .04249525 117274 2008 1015 1015 4.788292 0 0 0 0 0 0 .04249525 118363 2009 1015 1015 8.605871 0 0 0 0 0 0 .04249525 118466 2010 1015 1015 4.7684 1 .04249525 0 0 0 1 .04249525 117785 2011 1015 1015 1.9615124 1 .04249525 0 0 0 1 .04249525 117219 2012 1015 1015 1.975795 1 .04249525 0 0 0 1 .04249525 116482 2013 1015 1015 5.354645 1 .04249525 0 0 0 1 .04249525 115941 2014 1015 1015 7.208512 1 .04249525 0 0 0 1 .04249525 115505 2015 1015 1015 5.111183 1 .04249525 0 0 0 1 .04249525 114980 2016 1015 1015 13.381508 1 .04249525 0 0 0 1 .04249525 114728 2017 1015 1015 0 0 0 0 0 0 0 .04897014 34945 2006 1017 1017 1.4669044 0 0 0 0 0 0 .04897014 34847 2007 1017 1017 3.94596 0 0 0 0 0 0 .04897014 34563 2008 1017 1017 5.346541 0 0 0 0 0 0 .04897014 34384 2009 1017 1017 end
Code:
eststo clear local out "aotopioidr" qui sum `out' [weight=pop] if post==0 local base=`r(mean)' eststo res1: reghdfe aotopioidr post DD [weight=pop], absorb(i.year i.countyFIPS) vce(cluster countyFIPS) estadd scalar Bsl `base' qui sum `out' [weight=pop] if post==0 & DD2==1 local base1=`r(mean)' qui sum `out' [weight=pop] if post==0 & DD3==1 local base2=`r(mean)' qui sum `out' [weight=pop] if post==0 & DD4==1 local base3=`r(mean)' eststo res2: reghdfe aotopioidr post DD1 DD2 DD3 DD4 pre_rate [weight=pop], absorb(i.year i.countyFIPS) vce(cluster countyFIPS) estadd scalar Bs2 `base1' estadd scalar Bs3 `base2' estadd scalar Bs4 `base3' esttab res1 res2 using "TableDD_1.csv", keep(DD DD2 DD3 DD4) b(a4) se(4) nogaps nolabel star(* 0.10 ** 0.05 *** 0.01) /// label title(Difference-in-difference estimates of impact of PDMP must-access laws on prescription opioid related poisonings, BY OUTCOME (NN).) /// mtitles( "Total" "By quartile" ) stats(N) coeflabels(DD "Average impact OC reformulation" DD2 "Impact OC reformulation Q2" /// DD3 "Impact OC reformulation Q3" DD4 "Impact OC reformulation Q4") replace
. local out "aotopioidr"
. qui sum `out' [weight=pop] if post==0
. local base=`r(mean)'
. eststo res1: reghdfe aotopioidr post DD [weight=pop], absorb(i.year i.countyFIPS) vce(
> cluster countyFIPS)
(analytic weights assumed)
(converged in 4 iterations)
note: post omitted because of collinearity
HDFE Linear regression Number of obs = 36,480
Absorbing 2 HDFE groups F( 1, 3039) = 14.08
Statistics robust to heteroskedasticity Prob > F = 0.0002
R-squared = 0.5533
Adj R-squared = 0.5125
Within R-sq. = 0.0149
Number of clusters (countyFIPS) = 3,040 Root MSE = 4.7271
(Std. Err. adjusted for 3,040 clusters in countyFIPS)
------------------------------------------------------------------------------
| Robust
aotopioidr | Coef. Std. Err. t P>|t| [95% Conf. Interval]
-------------+----------------------------------------------------------------
post | 0 (omitted)
DD | 39.58188 10.54944 3.75 0.000 18.89711 60.26664
------------------------------------------------------------------------------
Absorbed degrees of freedom:
---------------------------------------------------------------+
Absorbed FE | Num. Coefs. = Categories - Redundant |
-------------+-------------------------------------------------|
year | 12 12 0 |
countyFIPS | 0 3040 3040 * |
---------------------------------------------------------------+
* = fixed effect nested within cluster; treated as redundant for DoF computation
. estadd scalar Bsl `base'
added scalar:
e(Bsl) = 4.8985249
.
. qui sum `out' [weight=pop] if post==0 & DD2==1
. local base1=`r(mean)'
invalid syntax
r(198);
end of do-file
r(198);
Not sure why it runs the first time but gives invalid syntax the second time?
Thanks for your help.
Sincerely,
Sumedha.