Announcement

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

  • Storing results to calculate mean

    Hi!

    I'm running a thousand simulations for two different regressions and the code goes as:

    forvalues i = 1/1000 {

    teffects ra (y edad_centrada experiencia_centrada) (tratamiento), atet
    local att1 = _b[tratamiento]
    replace att_est_1 = `att1' in `i'

    teffects ra (y edad_centrada edad_sq experiencia_centrada experiencia_sq edad_x_exp) (tratamiento), atet
    local att2 = _b[tratamiento]
    replace att_est_2 = `att2' in `i'

    }

    Tratamiento is my treatment value but every time that I try to run the code, I get the error "Tratamiento not found" and I can't wrap my head around it. Any help?

    Thanks!

  • #2
    Is this the exact code and the exact error message? I suspect not, because your commands refer to a variable tratamiento, but the error message refers to a different variable, Tratamiento. Stata is case sensitive. Stata would not capitalize the variable in the error message in response to a command that used the variable name in all lower case.

    So since I don't think you've actually said what happened, it isn't possible to troubleshoot. Given that in writing your post you didn't take care with the case distinction, I'm guessing that perhaps your actual problem arose for the same reason: inconsistent capitalization. If that's not the case, then please post back showing the exact code you ran and the exact error message you got. The way to assure that you do that is to use your computer's copy/paste facilities to transfer the information directly from the Results window or your log file here into the Forum editor.

    Comment


    • #3
      Hi, Clyde!

      First of all, thanks for the answer. Now, what I have posted is the exact code and it goes again as:

      forvalues i = 1/1000 {

      teffects ra (y edad_centrada experiencia_centrada) (tratamiento), atet
      local att1 = _b[tratamiento]
      replace att_est_1 = `att1' in `i'

      teffects ra (y edad_centrada edad_sq experiencia_centrada experiencia_sq edad_x_exp) (tratamiento), atet
      local att2 = _b[tratamiento]
      replace att_est_2 = `att2' in `i'

      }

      The variable name is 'tratamiento' without capitalization. And the error is:

      [tratamiento] not found.

      The strange thing is that I can run a normal regression in simulations like this:

      forvalues i = 1/1000 {

      reg y tratamiento edad_centrada experiencia_centrada if tratamiento == 1, robust
      replace att_est_1 = _b[tratamiento] in `i'

      reg y tratamiento edad_centrada edad_sq experiencia_centrada experiencia_sq edad_x_exp if tratamiento == 1, robust
      replace att_est_2 = _b[tratamiento] in `i'

      }

      But whenever I try to use teffects or nnmatch, it does not work and it gives the exact error I just shared. Thank you!

      Comment


      • #4
        OK. So the problem is that you are using the wrong name to access the ATE associated with tratamiento. It is not _b[tratamiento] It is _b[ATE:r1vs0.tratamiento]. (That's assuming that tratamiento takes on two values, coded 0 and 1.) How do I know this? Well, in general, with any Stata regression command, after you run the regression command, you can run it a second time and add the option -coeflegend-. Then Stata will replay the regression results showing the coefficients, but instead of the standard errors and test statistics and confidence interval, it will show you the names you can use to access those coefficients. Here's an example:
        Code:
        . use https://www.stata-press.com/data/r18/cattaneo2
        (Excerpt from Cattaneo (2010) Journal of Econometrics 155: 138–154)
        
        . teffects ra (bweight prenatal1 mmarried mage fbaby) (mbsmoke)
        
        Iteration 0:  EE criterion =  7.734e-24  
        Iteration 1:  EE criterion =  1.196e-25  
        
        Treatment-effects estimation                    Number of obs     =      4,642
        Estimator      : regression adjustment
        Outcome model  : linear
        Treatment model: none
        ----------------------------------------------------------------------------------------
                               |               Robust
                       bweight | Coefficient  std. err.      z    P>|z|     [95% conf. interval]
        -----------------------+----------------------------------------------------------------
        ATE                    |
                       mbsmoke |
        (Smoker vs Nonsmoker)  |  -239.6392   23.82402   -10.06   0.000    -286.3334    -192.945
        -----------------------+----------------------------------------------------------------
        POmean                 |
                       mbsmoke |
                    Nonsmoker  |   3403.242   9.525207   357.29   0.000     3384.573    3421.911
        ----------------------------------------------------------------------------------------
        
        . teffects ra, coeflegend
        
        Treatment-effects estimation                    Number of obs     =      4,642
        Estimator      : regression adjustment
        Outcome model  : linear
        Treatment model: none
        ----------------------------------------------------------------------------------------
                       bweight | Coefficient  Legend
        -----------------------+----------------------------------------------------------------
        ATE                    |
                       mbsmoke |
        (Smoker vs Nonsmoker)  |  -239.6392  _b[ATE:r1vs0.mbsmoke]
        -----------------------+----------------------------------------------------------------
        POmean                 |
                       mbsmoke |
                    Nonsmoker  |   3403.242  _b[POmean:0.mbsmoke]
        ----------------------------------------------------------------------------------------

        Comment


        • #5
          Clyde, I just want to say that you are an absolute genius! I've been battling against this error for two nights already.

          That worked like a charm and I have learnt something new thanks to your explanation. Indeed, the treatment value takes on two values, coded 0 and 1.

          Thank you so much for the help, and have a great weekend!

          Cheers!

          Comment

          Working...
          X