Announcement

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

  • R-squared and -didregress-

    Good evening all,

    Is there a way to have -didregress- show a value for r-squared- as you would see if you did a difference-in-difference regression using the old methods (just using -reg-)?

    Thanks!

  • #2
    You can quietly run the regression using regress and obtain the statistic. This code is valid for a single variable in -group()-.

    Code:
    use https://www.stata-press.com/data/r17/hospdd.dta, clear
    didregress (satis)(procedure), group(hospital) time(month)
    qui regress `e(depvar)' `e(treatment)' i.`e(clustvar)' i.`e(timevar)' if e(sample), cluster(`e(clustvar)')
    display e(r2)
    Res.:

    Code:
    . didregress (satis)(procedure), group(hospital) time(month)
    
    Number of groups and treatment time
    
    Time variable: month
    Control:       procedure = 0
    Treatment:     procedure = 1
    -----------------------------------
                 |   Control  Treatment
    -------------+---------------------
    Group        |
        hospital |        28         18
    -------------+---------------------
    Time         |
         Minimum |         1          4
         Maximum |         1          4
    -----------------------------------
    
    Difference-in-differences regression                     Number of obs = 7,368
    Data type: Repeated cross-sectional
    
                                   (Std. err. adjusted for 46 clusters in hospital)
    -------------------------------------------------------------------------------
                  |               Robust
            satis | Coefficient  std. err.      t    P>|t|     [95% conf. interval]
    --------------+----------------------------------------------------------------
    ATET          |
        procedure |
    (New vs Old)  |   .8479879   .0321121    26.41   0.000     .7833108     .912665
    -------------------------------------------------------------------------------
    Note: ATET estimate adjusted for group effects and time effects.
    
    . 
    . qui regress `e(depvar)' `e(treatment)' i.`e(clustvar)' i.`e(timevar)' if e(sample), cluster(`e(clustvar)')
    
    . 
    . display e(r2)
    .53325215
    
    .

    Comment


    • #3
      Thanks for the help Andrew! I do have a slight issue when running this command though. This works when the cluster variable has a small amount of values. However, my group variable is made up of 52,317 clusters. As such, when I run the command, I get the r(950) insufficient memory error. I have 44.6 million observations and Stats uses 5.7GB of memory. I have allowed Stats to use all available memory which is ~64GB total. I have also allowed Stata to use the maximum of 120,000 variables for State MP. I'm not sure if I'll be able to get around this one...

      Thanks,

      Comment


      • #4
        Install reghdfe from SSC.

        Code:
        ssc install reghdfe, replace
        net install ftools, from("https://github.com/sergiocorreia/ftools/raw/master/src/")
        Code:
        use https://www.stata-press.com/data/r17/hospdd.dta, clear
        didregress (satis)(procedure), group(hospital) time(month)
        reghdfe `e(depvar)' `e(treatment)', absorb(`e(clustvar)' `e(timevar)') cluster(`e(clustvar)')
        di e(r2)
        Res.:

        Code:
        . didregress (satis)(procedure), group(hospital) time(month)
        
        Number of groups and treatment time
        
        Time variable: month
        Control:       procedure = 0
        Treatment:     procedure = 1
        -----------------------------------
                     |   Control  Treatment
        -------------+---------------------
        Group        |
            hospital |        28         18
        -------------+---------------------
        Time         |
             Minimum |         1          4
             Maximum |         1          4
        -----------------------------------
        
        Difference-in-differences regression                     Number of obs = 7,368
        Data type: Repeated cross-sectional
        
                                       (Std. err. adjusted for 46 clusters in hospital)
        -------------------------------------------------------------------------------
                      |               Robust
                satis | Coefficient  std. err.      t    P>|t|     [95% conf. interval]
        --------------+----------------------------------------------------------------
        ATET          |
            procedure |
        (New vs Old)  |   .8479879   .0321121    26.41   0.000     .7833108     .912665
        -------------------------------------------------------------------------------
        Note: ATET estimate adjusted for group effects and time effects.
        
        . 
        . reghdfe `e(depvar)' `e(treatment)', absorb(`e(clustvar)' `e(timevar)') cluster(`e(clustvar)')
        (MWFE estimator converged in 2 iterations)
        
        HDFE Linear regression                            Number of obs   =      7,368
        Absorbing 2 HDFE groups                           F(   1,     45) =     701.63
        Statistics robust to heteroskedasticity           Prob > F        =     0.0000
                                                          R-squared       =     0.5333
                                                          Adj R-squared   =     0.5299
                                                          Within R-sq.    =     0.0774
        Number of clusters (hospital) =         46        Root MSE        =     0.7239
        
                                      (Std. err. adjusted for 46 clusters in hospital)
        ------------------------------------------------------------------------------
                     |               Robust
               satis | Coefficient  std. err.      t    P>|t|     [95% conf. interval]
        -------------+----------------------------------------------------------------
           procedure |   .8479879   .0320138    26.49   0.000     .7835088    .9124669
               _cons |   3.442755   .0066565   517.20   0.000     3.429348    3.456162
        ------------------------------------------------------------------------------
        
        Absorbed degrees of freedom:
        -----------------------------------------------------+
         Absorbed FE | Categories  - Redundant  = Num. Coefs |
        -------------+---------------------------------------|
            hospital |        46          46           0    *|
               month |         7           0           7     |
        -----------------------------------------------------+
        * = FE nested within cluster; treated as redundant for DoF computation
        
        . di e(r2)
        .53325215
        
        .

        Comment


        • #5
          Andrew,

          This works great. Thank you! As always, you've been a big help.

          Comment

          Working...
          X