Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Saving lincom output.

    Dear All,

    I have run a regression of the form:

    Code:
     eststo: reghdfe aopioid_ras int2006 int2007 int2008 int2010 int2011 int2012 int
    > 2013 int2014 int2015 int2016 unemprate east ///
    >         int2006_east int2007_east int2008_east int2010_east int2011_east int2012_east i
    > nt2013_east int2014_east int2015_east int2016_east ///
    >  CensusRuralShare2010 perc_white   pop_ratio_65plus ///
    >  [weight=pop_total], absorb(i.year i.countyFIPS) vce(c
    > luster countyFIPS)
    (analytic weights assumed)
    (converged in 5 iterations)
    note: east omitted because of collinearity
    
    HDFE Linear regression                            Number of obs   =     33,433
    Absorbing 2 HDFE groups                           F(  24,   3039) =       4.79
    Statistics robust to heteroskedasticity           Prob > F        =     0.0000
                                                      R-squared       =     0.6089
                                                      Adj R-squared   =     0.5693
                                                      Within R-sq.    =     0.0444
    Number of clusters (countyFIPS) =      3,040      Root MSE        =     4.1842
    
                                     (Std. Err. adjusted for 3,040 clusters in countyFIPS)
    --------------------------------------------------------------------------------------
                         |               Robust
             aopioid_ras |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    ---------------------+----------------------------------------------------------------
                 int2006 |   9.926164   5.182502     1.92   0.056    -.2354009    20.08773
                 int2007 |    6.32521   3.637105     1.74   0.082    -.8062248    13.45664
                 int2008 |   7.171485   3.040367     2.36   0.018     1.210101    13.13287
                 int2010 |   13.89054   4.888414     2.84   0.005      4.30561    23.47548
                 int2011 |   13.56041   5.912389     2.29   0.022     1.967729     25.1531
                 int2012 |    6.19747   4.079553     1.52   0.129    -1.801494    14.19643
                 int2013 |   3.785004      4.824     0.78   0.433    -5.673629    13.24364
                 int2014 |   13.26759    5.48916     2.42   0.016     2.504743    24.03043
                 int2015 |    22.9769   10.32162     2.23   0.026     2.738838    43.21497
                 int2016 |   37.67179   13.32013     2.83   0.005     11.55442    63.78917
               unemprate |   .1409835   .0809161     1.74   0.082    -.0176723    .2996393
                    east |          0  (omitted)
            int2006_east |   1.045147   7.263032     0.14   0.886    -13.19581     15.2861
            int2007_east |   12.27889   5.568436     2.21   0.028     1.360607    23.19717
            int2008_east |   4.186853    4.51996     0.93   0.354    -4.675635    13.04934
            int2010_east |   13.74968   5.823592     2.36   0.018       2.3311    25.16826
            int2011_east |   11.54176   7.137946     1.62   0.106    -2.453932    25.53745
            int2012_east |   3.910216   7.168708     0.55   0.585    -10.14579    17.96622
            int2013_east |   4.060913   7.892771     0.51   0.607     -11.4148    19.53662
            int2014_east |   12.60207   9.846917     1.28   0.201    -6.705222    31.90936
            int2015_east |    31.3365    14.2346     2.20   0.028     3.426073    59.24693
            int2016_east |   97.16012   20.14295     4.82   0.000     57.66492    136.6553
    CensusRuralShare2010 |   1.53e+08   1.13e+08     1.35   0.177    -6.92e+07    3.76e+08
              perc_white |   .1384629   .1548491     0.89   0.371    -.1651567    .4420825
        pop_ratio_65plus |  -25.48138    15.5909    -1.63   0.102    -56.05115     5.08839
    --------------------------------------------------------------------------------------
    I am interested in plotting the estimates with the 95% CI of the combined effect of (
    int2006+int2006_east), (
    int2007+int2007_east),
    (
    int2008+int2008_east) etc

    I can calculate the combined estimates as follows:


    Code:
    lincom   int2006+int2006_east
    
     ( 1)  int2006 + int2006_east = 0
    
    ------------------------------------------------------------------------------
     aopioid_ras |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
             (1) |   10.97131   8.158032     1.34   0.179     -5.02451    26.96713
    ------------------------------------------------------------------------------
    How can I store the coefficient and 95 CI for all the estimates
    (
    int2006+int2006_east), (
    int2007+int2007_east),
    (
    int2008+int2008_east) etc.? Once I have it stored how can I retrieve that information to plot using a twoway command?

    I saw an earlier post but the specific commands were not clear and hence I will be very grateful for some specific help.

    Sincerely,
    Sumedha.

  • #2
    The following uses esttab from Stata Journal, authored by Ben Jann.

    Code:
    forval year= 2006/2016{
    qui reghdfe aopioid_ras int2006 ...
    margins, expression(_b[int`year']+_b[int`year'_east]) post
    qui esttab, ci
    mat ci`year'= r(coefs)
    }
    mat ci = ci2006\ci2007\...\ci2016
    mat list ci

    Comment


    • #3
      Thank you so much. But, I ran into the following error:

      Code:
      . local years 2006 2007 2008 2010 2011 2012 2013 2014 2015 2016
      
      .
      . foreach year of local years{
        2. qui eststo: reghdfe aopioid_ras int2006 int2007 int2008 int2010 int2011 int2012 int2
      > 013 int2014 int2015 int2016 unemprate east ///
      >         int2006_east int2007_east int2008_east int2010_east int2011_east int2012_east i
      > nt2013_east int2014_east int2015_east int2016_east ///
      > CensusRuralShare2010 perc_white pop_ratio_65plus [weight=pop_total], absorb(i.year i.co
      > untyFIPS) vce(cluster countyFIPS)
        3. margins, expression(_b[int`year']+_b[int`year'_east]) post
        4. qui esttab, ci
        5. mat ci`year'= r(coefs)
        6. }
      Warning: cannot perform check for estimable functions.
      Warning: expression() does not contain predict() or xb().
      Warning: prediction constant over observations.
      
      Predictive margins                              Number of obs     =     33,433
      Model VCE    : Robust
      
      Expression   : _b[int2006]+_b[int2006_east]
      
      ------------------------------------------------------------------------------
                   |            Delta-method
                   |     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
      -------------+----------------------------------------------------------------
             _cons |   10.97131   8.158032     1.34   0.179    -5.018139    26.96076
      ------------------------------------------------------------------------------
      Warning: cannot perform check for estimable functions.
      Warning: expression() does not contain predict() or xb().
      Warning: prediction constant over observations.
      
      Predictive margins                              Number of obs     =     33,433
      Model VCE    : Robust
      
      Expression   : _b[int2007]+_b[int2007_east]
      
      ------------------------------------------------------------------------------
                   |            Delta-method
                   |     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
      -------------+----------------------------------------------------------------
             _cons |    18.6041   5.651185     3.29   0.001      7.52798    29.68022
      ------------------------------------------------------------------------------
      Warning: cannot perform check for estimable functions.
      Warning: expression() does not contain predict() or xb().
      Warning: prediction constant over observations.
      
      Predictive margins                              Number of obs     =     33,433
      Model VCE    : Robust
      
      Expression   : _b[int2008]+_b[int2008_east]
      
      ------------------------------------------------------------------------------
                   |            Delta-method
                   |     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
      -------------+----------------------------------------------------------------
             _cons |   11.35834   4.409717     2.58   0.010     2.715451    20.00122
      ------------------------------------------------------------------------------
      Warning: cannot perform check for estimable functions.
      Warning: expression() does not contain predict() or xb().
      Warning: prediction constant over observations.
      
      Predictive margins                              Number of obs     =     33,433
      Model VCE    : Robust
      
      Expression   : _b[int2010]+_b[int2010_east]
      
      ------------------------------------------------------------------------------
                   |            Delta-method
                   |     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
      -------------+----------------------------------------------------------------
             _cons |   27.64022   5.262979     5.25   0.000     17.32497    37.95547
      ------------------------------------------------------------------------------
      Warning: cannot perform check for estimable functions.
      Warning: expression() does not contain predict() or xb().
      Warning: prediction constant over observations.
      
      Predictive margins                              Number of obs     =     33,433
      Model VCE    : Robust
      
      Expression   : _b[int2011]+_b[int2011_east]
      
      ------------------------------------------------------------------------------
                   |            Delta-method
                   |     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
      -------------+----------------------------------------------------------------
             _cons |   25.10217    6.69164     3.75   0.000      11.9868    38.21755
      ------------------------------------------------------------------------------
      Warning: cannot perform check for estimable functions.
      Warning: expression() does not contain predict() or xb().
      Warning: prediction constant over observations.
      
      Predictive margins                              Number of obs     =     33,433
      Model VCE    : Robust
      
      Expression   : _b[int2012]+_b[int2012_east]
      
      ------------------------------------------------------------------------------
                   |            Delta-method
                   |     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
      -------------+----------------------------------------------------------------
             _cons |   10.10769   7.023474     1.44   0.150     -3.65807    23.87344
      ------------------------------------------------------------------------------
      Warning: cannot perform check for estimable functions.
      Warning: expression() does not contain predict() or xb().
      Warning: prediction constant over observations.
      
      Predictive margins                              Number of obs     =     33,433
      Model VCE    : Robust
      
      Expression   : _b[int2013]+_b[int2013_east]
      
      ------------------------------------------------------------------------------
                   |            Delta-method
                   |     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
      -------------+----------------------------------------------------------------
             _cons |   7.845918   7.733221     1.01   0.310    -7.310917    23.00275
      ------------------------------------------------------------------------------
      Warning: cannot perform check for estimable functions.
      Warning: expression() does not contain predict() or xb().
      Warning: prediction constant over observations.
      
      Predictive margins                              Number of obs     =     33,433
      Model VCE    : Robust
      
      Expression   : _b[int2014]+_b[int2014_east]
      
      ------------------------------------------------------------------------------
                   |            Delta-method
                   |     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
      -------------+----------------------------------------------------------------
             _cons |   25.86966   9.305137     2.78   0.005     7.631922    44.10739
      ------------------------------------------------------------------------------
      Warning: cannot perform check for estimable functions.
      Warning: expression() does not contain predict() or xb().
      Warning: prediction constant over observations.
      
      Predictive margins                              Number of obs     =     33,433
      Model VCE    : Robust
      
      Expression   : _b[int2015]+_b[int2015_east]
      
      ------------------------------------------------------------------------------
                   |            Delta-method
                   |     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
      -------------+----------------------------------------------------------------
             _cons |    54.3134   12.18004     4.46   0.000     30.44095    78.18585
      ------------------------------------------------------------------------------
      Warning: cannot perform check for estimable functions.
      Warning: expression() does not contain predict() or xb().
      Warning: prediction constant over observations.
      
      Predictive margins                              Number of obs     =     33,433
      Model VCE    : Robust
      
      Expression   : _b[int2016]+_b[int2016_east]
      
      ------------------------------------------------------------------------------
                   |            Delta-method
                   |     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
      -------------+----------------------------------------------------------------
             _cons |   134.8319   18.02137     7.48   0.000     99.51067    170.1531
      ------------------------------------------------------------------------------
      
      . mat ci = ci2006\ci2007\ci2008\ci2010\ci2011\ci2012\ci2013\ci2014\ci2015\ci2016
      conformability error
      r(503);
      
      end of do-file
      
      r(503);
      2009 is left out as the reference period. Any thoughts ?

      Sincerely,
      S.

      Comment


      • #4
        It is not apparently clear to me why you get a conformability error when the matrices have the same dimensions. Can you post what you get after running

        Code:
        foreach year of local years{
        mat list ci`year'
        }
        mat ci12= ci2006\ci2007

        Comment


        • #5
          Hi,

          Thank you for trying to help me resolve this. I get the same error again:

          Code:
          . foreach year of local years{
            2. mat list ci`year'
            3. }
          
          . mat ci12= ci2006\ci2007
          conformability error
          r(503);
          Hmm...

          Comment


          • #6
            I want to see these matrices. Copy and paste what appears after you run

            Code:
            foreach year of local years{
            mat list ci`year'
            }

            Comment


            • #7
              I am unable to copy and paste due to the number of characters. So please find attached the log file.

              Thank you so much.
              Attached Files

              Comment


              • #8
                You did not implement the code in #2 as I suggested. I see

                ci2006[25,200]
                whereas these matrices (from the output of margins) will be of dimension \(1\times 4\).

                First, you have to install esttab from Stata Journal.

                Code:
                findit esttab
                Before you run the loop, try with one year and make sure you are getting a matrix with the right dimensions. Also, remove "eststo:" in front of the reghdfe command.

                Code:
                reghdfe aopioid_ras ...
                margins, expression(_b[int2006]+_b[int2006_east]) post
                esttab, ci
                mat ci2006= r(coefs)
                mat list ci2006
                The code above should result in

                ci2006[1,4]

                Comment


                • #9
                  Reproducible example:

                  Code:
                  . sysuse auto, clear
                  . reghdfe mpg weight headroom price trunk, a(rep78)
                  (MWFE estimator converged in 1 iterations)
                  
                  HDFE Linear regression                            Number of obs   =         69
                  Absorbing 1 HDFE group                            F(   4,     60) =      20.72
                                                                    Prob > F        =     0.0000
                                                                    R-squared       =     0.6787
                                                                    Adj R-squared   =     0.6358
                                                                    Within R-sq.    =     0.5801
                                                                    Root MSE        =     3.5402
                  
                  ------------------------------------------------------------------------------
                           mpg |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                  -------------+----------------------------------------------------------------
                        weight |  -.0047189   .0009656    -4.89   0.000    -.0066503   -.0027874
                      headroom |   .1558354   .7066684     0.22   0.826    -1.257712    1.569383
                         price |  -.0001431   .0001903    -0.75   0.455    -.0005238    .0002376
                         trunk |  -.1385207   .1602502    -0.86   0.391    -.4590689    .1820275
                         _cons |   37.93883   2.112565    17.96   0.000     33.71307    42.16458
                  ------------------------------------------------------------------------------
                  
                  Absorbed degrees of freedom:
                  -----------------------------------------------------+
                   Absorbed FE | Categories  - Redundant  = Num. Coefs |
                  -------------+---------------------------------------|
                         rep78 |         5           0           5     |
                  -----------------------------------------------------+
                  
                  . margins, expression(_b[weight]+_b[trunk]) post
                  Warning: expression() does not contain predict() or xb().
                  Warning: prediction constant over observations.
                  
                  Predictive margins                              Number of obs     =         69
                  Model VCE    : OLS
                  
                  Expression   : _b[weight]+_b[trunk]
                  
                  ------------------------------------------------------------------------------
                               |            Delta-method
                               |     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
                  -------------+----------------------------------------------------------------
                         _cons |  -.1432396   .1598192    -0.90   0.370    -.4564795    .1700004
                  ------------------------------------------------------------------------------
                  
                  . esttab, ci
                  
                  --------------------------------------
                                                  (1)  
                                                        
                  --------------------------------------
                  _cons                        -0.143  
                                       [-0.456,0.170]  
                  --------------------------------------
                  N                                69  
                  --------------------------------------
                  95% confidence intervals in brackets
                  * p<0.05, ** p<0.01, *** p<0.001
                  
                  . mat ci1= r(coefs)
                  
                  . mat list ci1
                  
                  ci1[1,4]
                             active:     active:     active:     active:
                                  b        ci_l        ci_u           p
                  _cons  -.14323956  -.45647947   .17000036   .37011397

                  Comment


                  • #10
                    OMG! Thank you so much! It worked:

                    Code:
                     mat ci = ci2006\ci2007\ci2008\ci2010\ci2011\ci2012\ci2013\ci2014\ci2015\ci2016
                    
                    . mat list ci
                    
                    ci[10,4]
                               active:     active:     active:     active:
                                    b        ci_l        ci_u           p
                    _cons   10.971311   -5.018139    26.96076   .17867444
                    _cons   18.604099   7.5279798   29.680218   .00099453
                    _cons   11.358338   2.7154512   20.001225   .01000223
                    _cons   27.640223   17.324974   37.955471   1.506e-07
                    _cons   25.102173     11.9868   38.217547   .00017594
                    _cons   10.107686  -3.6580705   23.873442   .15011395
                    _cons   7.8459178  -7.3109171   23.002753   .31030939
                    _cons   25.869656   7.6319216    44.10739   .00543342
                    _cons   54.313401   30.440952   78.185851   8.226e-06
                    _cons   134.83191   99.510675   170.15315   7.327e-14
                    A quick follow up question, if I want to plot the coefficients with the CI's using something like :

                    twoway
                    (scatter
                    r(coeffs)
                    year
                    , msym(s) mcol(black)) || (rcap lci uci
                    year
                    , lcol(black))

                    How should I 'refer' to the matrix location for the betas, lci and uci to plot? Something like ci[.,1] for beta and ci[
                    .,2] for lci and ci[.,3] for uci? Do I need to give them names first?

                    Comment


                    • #11
                      You can use coefplot, another wonderful program written by Ben Jann. Download from the Stata Journal.

                      Code:
                      findit coefplot
                      Command:

                      Code:
                      matrix rownames ci= "my label 2006" "my label 2007" ... "my label 2016"
                      mat cip= ci'
                      coefplot matrix(cip), ci((cip[2] cip[3])) scheme(s1color)
                      where you replace "my label" with some appropriate name.

                      Comment


                      • #12
                        Thank you Andrew. It worked perfectly. Its been a few days but I am trying to extend your code to add a couple of more interactions. Specifically, I want to calculate total effects for quartiles (q1, q2, q3 and q4) using lincom. Q3 serves as the reference group and is thus omitted.:
                        Int2006+int2006_q1
                        int2007+int2007q1
                        .
                        .
                        int2016+int2016_q4
                        But am unable to figure out the code:

                        Code:
                        . local years 2006 2007 2008 2010 2011 2012 2013 2014 2015 2016
                        
                        . local qs 1 2 4
                        
                        .
                        . foreach year of local years{
                          2. qui reghdfe aopioid_ras int2006 int2007 int2008 int2010 int2011 int2012 int2013 int2
                        > 014 int2015 int2016 unemprate q1 q2 q4 ///
                        > int2006_q1 int2007_q1 int2008_q1 int2010_q1 int2011_q1 int2012_q1 int2013_q1 int2014_q1
                        >  int2015_q1 int2016_q1 ///
                        > int2006_q2 int2007_q2 int2008_q2 int2010_q2 int2011_q2 int2012_q2 int2013_q2 int2014_q2
                        >  int2015_q2 int2016_q2 ///
                        > int2006_q4 int2007_q4 int2008_q4 int2010_q4 int2011_q4 int2012_q4 int2013_q4 int2014_q4
                        >  int2015_q4 int2016_q4 ///
                        > CensusRuralShare2010 perc_white pop_ratio_65plus [weight=pop_total], absorb(i.year i.co
                        > untyFIPS) vce(cluster countyFIPS)
                          3. foreach q of local qs{
                          4.         margins, expression(_b[int`year']+_b[int`year'_q`q']) post
                          5.         qui esttab, ci`year'
                          6.         mat ci`year'`q'= r(coefs)
                          7.         }
                          8.         mat ci`year'=ci`year'1\ci`year'2\ci`year'4
                          9. }
                        Warning: cannot perform check for estimable functions.
                        Warning: expression() does not contain predict() or xb().
                        Warning: prediction constant over observations.
                        
                        Predictive margins                              Number of obs     =     33,433
                        Model VCE    : Robust
                        
                        Expression   : _b[int2006]+_b[int2006_q1]
                        
                        ------------------------------------------------------------------------------
                                     |            Delta-method
                                     |     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
                        -------------+----------------------------------------------------------------
                               _cons |   17.67973    9.70216     1.82   0.068    -1.336158    36.69561
                        ------------------------------------------------------------------------------
                        option ci2006 not allowed
                        r(198);
                        
                        end of do-file
                        
                        r(198);
                        Not sure why is ci2006 entering as an option. Any thoughts? Will be grateful for your help.
                        Sincerely,
                        S.

                        Comment


                        • #13
                          You need to include a reproducible example like I did in #9. I cannot follow what you want to do,

                          Comment


                          • #14
                            Sorry. I am trying to estimate the effect of a policy change in 2010 by variation in county-level 'pre-rate'. The data looks as follows:

                            Code:
                            * Example generated by -dataex-. To install: ssc install dataex
                            clear
                            input float year double aopioid_ras float(Post pre_rate) byte(q1 q2 q3 q4) long countyFIPS double pop_total
                            2006 3.4311306476593018 0 .014084662 0 0 1 0 1001  51328
                            2007  7.033212065696716 0 .014084662 0 0 1 0 1001  52405
                            2008  3.915321111679077 0 .014084662 0 0 1 0 1001  53277
                            2009  4.997965931892395 0 .014084662 0 0 1 0 1001  54135
                            2010  3.215880811214447 0 .014084662 0 0 1 0 1001  54742
                            2011  8.087055802345276 0 .014084662 0 0 1 0 1001  55255
                            2012 1.6561747789382935 0 .014084662 0 0 1 0 1001  55027
                            2013  4.553722620010376 0 .014084662 0 0 1 0 1001  54792
                            2014  3.625663936138153 1 .014084662 0 0 1 0 1001  54977
                            2015 2.6239168643951416 1 .014084662 0 0 1 0 1001  55035
                            2016  5.292389154434204 1 .014084662 0 0 1 0 1001  55416
                            2006  9.748047590255737 0  .04371211 0 0 0 1 1003 168121
                            2007 11.819020807743073 0  .04371211 0 0 0 1 1003 172404
                            2008 10.980432540178299 0  .04371211 0 0 0 1 1003 175827
                            2009 14.819782257080078 0  .04371211 0 0 0 1 1003 179406
                            2010 10.578196406364441 0  .04371211 0 0 0 1 1003 183199
                            2011 10.057409226894379 0  .04371211 0 0 0 1 1003 186653
                            2012 10.666273325681686 0  .04371211 0 0 0 1 1003 190403
                            2013  6.558670341968536 0  .04371211 0 0 0 1 1003 195147
                            2014 12.903846681118011 1  .04371211 0 0 0 1 1003 199745
                            2015 14.208545863628387 1  .04371211 0 0 0 1 1003 203690
                            2016  9.546390891075134 1  .04371211 0 0 0 1 1003 208563
                            2006 2.2586216926574707 0 .008630031 1 0 0 0 1005  27861
                            2007                  0 0 .008630031 1 0 0 0 1005  27757
                            2008                  0 0 .008630031 1 0 0 0 1005  27808
                            2009 2.4353842735290527 0 .008630031 1 0 0 0 1005  27657
                            2010                  0 0 .008630031 1 0 0 0 1005  27348
                            2011                  0 0 .008630031 1 0 0 0 1005  27326
                            2012                  0 0 .008630031 1 0 0 0 1005  27132
                            2013  3.905710458755493 0 .008630031 1 0 0 0 1005  26938
                            2014 1.9059680700302124 1 .008630031 1 0 0 0 1005  26763
                            2015                  0 1 .008630031 1 0 0 0 1005  26270
                            2016  4.390766382217407 1 .008630031 1 0 0 0 1005  25965
                            2006 14.376561641693115 0  .05500945 0 1 0 0 1007  22099
                            2007 14.726265907287598 0  .05500945 0 1 0 0 1007  22438
                            2008  21.81593894958496 0  .05500945 0 1 0 0 1007  22705
                            2009 21.019699096679687 0  .05500945 0 1 0 0 1007  22941
                            2010  2.764141798019409 0  .05500945 0 1 0 0 1007  22861
                            2011  2.613255500793457 0  .05500945 0 1 0 0 1007  22736
                            2012  6.436553478240967 0  .05500945 0 1 0 0 1007  22645
                            2013   5.03369665145874 0  .05500945 0 1 0 0 1007  22501
                            2014 13.076036214828491 1  .05500945 0 1 0 0 1007  22511
                            2015  5.778356671333313 1  .05500945 0 1 0 0 1007  22561
                            2016 21.761616945266724 1  .05500945 0 1 0 0 1007  22643
                            2006 3.9149229526519775 0  .02577892 0 1 0 0 1009  55485
                            2007  7.211379587650299 0  .02577892 0 1 0 0 1009  56240
                            2008 18.713393449783325 0  .02577892 0 1 0 0 1009  57055
                            2009 16.429306626319885 0  .02577892 0 1 0 0 1009  57341
                            2010  7.632001519203186 0  .02577892 0 1 0 0 1009  57376
                            2011  7.030265212059021 0  .02577892 0 1 0 0 1009  57707
                            2012  17.91651350259781 0  .02577892 0 1 0 0 1009  57772
                            2013 1.8803969621658325 0  .02577892 0 1 0 0 1009  57746
                            2014 10.289381057024002 1  .02577892 0 1 0 0 1009  57621
                            2015  5.483194470405579 1  .02577892 0 1 0 0 1009  57676
                            2016  9.233121633529663 1  .02577892 0 1 0 0 1009  57704
                            2006                  0 0          0 1 0 0 0 1011  10776
                            2007                  0 0          0 1 0 0 0 1011  11011
                            2008                  0 0          0 1 0 0 0 1011  10953
                            2009                  0 0          0 1 0 0 0 1011  10987
                            2010                  0 0          0 1 0 0 0 1011  10892
                            2011  7.423938751220703 0          0 1 0 0 0 1011  10722
                            2012                  0 0          0 1 0 0 0 1011  10654
                            2013                  0 0          0 1 0 0 0 1011  10576
                            2014  3.668813467025757 1          0 1 0 0 0 1011  10712
                            2015                  0 1          0 1 0 0 0 1011  10455
                            2016  14.79034423828125 1          0 1 0 0 0 1011  10362
                            2006  4.421046257019043 0 .007248664 1 0 0 0 1013  20815
                            2007                  0 0 .007248664 1 0 0 0 1013  20894
                            2008   6.43389368057251 0 .007248664 1 0 0 0 1013  20949
                            2009  8.174416303634644 0 .007248664 1 0 0 0 1013  20867
                            2010 11.537550687789917 0 .007248664 1 0 0 0 1013  20938
                            2011 7.0957558155059814 0 .007248664 1 0 0 0 1013  20848
                            2012 3.2639384269714355 0 .007248664 1 0 0 0 1013  20665
                            2013                  0 0 .007248664 1 0 0 0 1013  20330
                            2014  2.165806293487549 1 .007248664 1 0 0 0 1013  20283
                            2015  4.935991525650024 1 .007248664 1 0 0 0 1013  20126
                            2016  10.40921139717102 1 .007248664 1 0 0 0 1013  19998
                            2006 2.9637354612350464 0  .04249525 0 1 0 0 1015 115388
                            2007 7.1379284262657166 0  .04249525 0 1 0 0 1015 116211
                            2008  11.30965781211853 0  .04249525 0 1 0 0 1015 117274
                            2009 10.021137237548828 0  .04249525 0 1 0 0 1015 118363
                            2010 13.387976169586182 0  .04249525 0 1 0 0 1015 118468
                            2011   8.50603574514389 0  .04249525 0 1 0 0 1015 117736
                            2012 3.2640677392482758 0  .04249525 0 1 0 0 1015 117208
                            2013 2.7501730918884277 0  .04249525 0 1 0 0 1015 116475
                            2014  7.401610255241394 1  .04249525 0 1 0 0 1015 115837
                            2015  8.810407876968384 1  .04249525 0 1 0 0 1015 115285
                            2016  5.867506802082062 1  .04249525 0 1 0 0 1015 114611
                            2006 3.0412070751190186 0  .04897014 1 0 0 0 1017  34945
                            2007 3.0202934741973877 0  .04897014 1 0 0 0 1017  34847
                            2008 12.489797711372375 0  .04897014 1 0 0 0 1017  34563
                            2009   9.05411148071289 0  .04897014 1 0 0 0 1017  34384
                            2010 10.873043775558472 0  .04897014 1 0 0 0 1017  34101
                            2011  7.589829206466675 0  .04897014 1 0 0 0 1017  34006
                            2012  6.057841062545776 0  .04897014 1 0 0 0 1017  34084
                            2013  8.101014375686646 0  .04897014 1 0 0 0 1017  34123
                            2014                  0 1  .04897014 1 0 0 0 1017  33996
                            2015  3.153040885925293 1  .04897014 1 0 0 0 1017  34043
                            2016  5.365427136421204 1  .04897014 1 0 0 0 1017  33843
                            2006 12.556070804595947 0  .02212559 0 1 0 0 1019  25466
                            end

                            q1-q4 are the county-level income quartile. I am tying to estimate a model with interactions of county-level income quartiles.

                            Code:
                            qui reghdfe aopioid_ras int2006 int2007 int2008 int2010 int2011 int2012 int2013 int2014 int2015 int2016 unemprate q1 q2 q4 ///
                            int2006_q1 int2007_q1 int2008_q1 int2010_q1 int2011_q1 int2012_q1 int2013_q1 int2014_q1 int2015_q1 int2016_q1 ///
                            int2006_q2 int2007_q2 int2008_q2 int2010_q2 int2011_q2 int2012_q2 int2013_q2 int2014_q2 int2015_q2 int2016_q2 ///
                            int2006_q4 int2007_q4 int2008_q4 int2010_q4 int2011_q4 int2012_q4 int2013_q4 int2014_q4 int2015_q4 int2016_q4 ///
                             [weight=pop_total], absorb(i.year i.countyFIPS) vce(cluster countyFIPS)
                            Here int2006 ….int2016 are the interactions between pre_rate and year variables.
                            int2006_q1,.....,int2016_q1 are the interactions between int2006 and county being in income quartile 1
                            .
                            .
                            int2006_q4...…., int2016_q4 are the interactions between int2006 and county being in income quartile 4

                            q3 is considered as the reference category and is thus omitted.

                            The above code will estimate all the individual effects but I want to calculate the total effect for a county in a given q1, q2, q4. Last time you suggested something like:

                            Code:
                            foreach year of local years{
                            
                            qui reghdfe aopioid_ras int2006 int2007 int2008 int2010 int2011 int2012 int2013 int2014 int2015 int2016 unemprate q1 q2 q4 ///
                            int2006_q1 int2007_q1 int2008_q1 int2010_q1 int2011_q1 int2012_q1 int2013_q1 int2014_q1 int2015_q1 int2016_q1 ///
                            int2006_q2 int2007_q2 int2008_q2 int2010_q2 int2011_q2 int2012_q2 int2013_q2 int2014_q2 int2015_q2 int2016_q2 ///
                            int2006_q4 int2007_q4 int2008_q4 int2010_q4 int2011_q4 int2012_q4 int2013_q4 int2014_q4 int2015_q4 int2016_q4 ///
                            [weight=pop_total], absorb(i.year i.countyFIPS) vce(cluster countyFIPS)
                            margins, expression(_b[int`year']+_b[int`year'_q1]) post qui esttab, ci mat ci`year'= r(coefs) } mat ci = ci2006\ci2007\ci2008\ci2010\ci2011\ci2012\ci2013\ci2014\ci2015\ci2016 mat list ci mat colnames ci ="tot_q1" "tot_q1_uci" "tot_q1_lci" "tot_q1_p" matrix rownames ci= "2006" "2007" "2008" "2010" "2011" "2012" "2013" "2014" "2015" "2016" mat cip= ci' coefplot matrix(cip), vertical ci((cip[2] cip[3])) yline(0) xline(4) title("(8) Cocaine death rate (Ruhm)") subtitle("Q1") /// ytitle("Cocaine death rate", margin(vlarge)) xtitle("") coeflabels(2006="2006" 2007="2007" 2008="2008" 2010="2010" /// 2011="2011" 2012="2012" 2013="2013" 2014="2014" 2015="2015" 2016="2016", angle(45)) name (i) scheme(s2gcolor)

                            Now I want to add to the loop above total effects for county q2 and q4 as well:
                            margins, expression(_b[int`year']+_b[int`year'_q2]) post
                            margins, expression(_b[int`year']+_b[int`year'_q4) post

                            and then plot those as well using coefplot. How can I do that?

                            I tried the following and it didn't work:


                            Code:
                            . local years 2006 2007 2008 2010 2011 2012 2013 2014 2015 2016 . local qs 1 2 4 . . foreach year of local years{ 2. qui reghdfe aopioid_ras int2006 int2007 int2008 int2010 int2011 int2012 int2013 int2 > 014 int2015 int2016 unemprate q1 q2 q4 /// > int2006_q1 int2007_q1 int2008_q1 int2010_q1 int2011_q1 int2012_q1 int2013_q1 int2014_q1 > int2015_q1 int2016_q1 /// > int2006_q2 int2007_q2 int2008_q2 int2010_q2 int2011_q2 int2012_q2 int2013_q2 int2014_q2 > int2015_q2 int2016_q2 /// > int2006_q4 int2007_q4 int2008_q4 int2010_q4 int2011_q4 int2012_q4 int2013_q4 int2014_q4 > int2015_q4 int2016_q4 /// > CensusRuralShare2010 perc_white pop_ratio_65plus [weight=pop_total], absorb(i.year i.co > untyFIPS) vce(cluster countyFIPS) 3. foreach q of local qs{ 4. margins, expression(_b[int`year']+_b[int`year'_q`q']) post 5. qui esttab, ci`year' 6. mat ci`year'`q'= r(coefs) 7. } 8. mat ci`year'=ci`year'1\ci`year'2\ci`year'4 9. } Warning: cannot perform check for estimable functions. Warning: expression() does not contain predict() or xb(). Warning: prediction constant over observations. Predictive margins Number of obs = 33,433 Model VCE : Robust Expression : _b[int2006]+_b[int2006_q1] ------------------------------------------------------------------------------ | Delta-method | Margin Std. Err. z P>|z| [95% Conf. Interval] -------------+---------------------------------------------------------------- _cons | 17.67973 9.70216 1.82 0.068 -1.336158 36.69561 ------------------------------------------------------------------------------ option ci2006 not allowed r(198); end of do-file r(198);
                            Thank you so much for your help.
                            Sincerely,
                            S.

                            Comment


                            • #15
                              I still cannot reproduce your results as your data does not include the "int" and "int_q" variables. Is there any reason that you are creating these variables by hand instead of using Stata's factor variable notation?

                              Here int2006 ….int2016 are the interactions between pre_rate and year variables.
                              c.pre_rate##i.year
                              .
                              int2006_q4...…., int2016_q4 are the interactions between int2006 and county being in income quartile 4

                              q3 is considered as the reference category and is thus omitted.
                              Code:
                              2006.year##ib3.q
                              You also appear to be including interactions without having the main variable being interacted with present. This most likely will lead your model to be misspecified. In any case, before trying to amend code that includes a loop, make sure that you try out one instance and it works. A loop is simply repetition of some lines of code.

                              Comment

                              Working...
                              X