Announcement

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

  • Unit and year variables

    Hello

    I have a bunch of unit dummys (u1-u350) and year variable for the years 2000-2020, and I want to create a variable that takes the values of 2000-2020 for the first unit (u1) and 0 for the rest, a variable that takes the values of 2000-2020 for the second unit (u2) and 0 for the rest and so on. Do you guys know an efficient way to go about this? I have 350 units so

    Code:
    gen u1year = u1*year
    gen u2year = u2*year
    gen u3year = u3*year
    gen u4year = u4*year
    gen u5year = u5*year
    gen u6year = u6*year
    ...
    is a bit inconvenient
    Last edited by Robert Garcia; 08 Apr 2022, 07:46.

  • #2
    It's simple enough to do:
    Code:
    forvalues i = 1/350 {
        gen u`i'year = u`i'*year
    }
    But why are you doing this? Unless you are a using an antique version of Stata there is probably no need for this at all. In fact you probably don't need the unit variables either. If the purpose of these "dummy" variables is to represent units and unitXyear interactions in a regression, you should use factor variable notation instead. Instead of the 350 unit variables, create a single variable taking on values from 1 through 350 and call it unit. Then:

    Code:
    regression_command outcome_variable explanatory_variables i.unit##c.year
    and you're done. No cluttering up your data set with a lot of junky dummy variables, and easy coding of the regression.

    Read -help fvvarlist- for an explanation of factor-variable notation.

    Comment


    • #3
      Thanks man! That's very helpful. I am trying to perform a Goodman-Bacon decomposition on a difference-in-differences regression using the command bacondecomp, but when I include
      Code:
      i.unit##c.year
      I get the error message
      factor-variable and time-series operators not allowed
      r(101);
      so I thought I would try to create the interaction term manually

      Are you familiar with bacondecomp? Do you, by any chance, happen to have a better solution?

      My DiD model is
      Code:
      xtreg outcome treatment i.year i.unit##c.year, fe cluster(unit)
      and my (failed) attempt at Goodman-Bacon decomposition looks like this
      Code:
      bacondecomp outcome treatment i.unit##c.year, cluster(unit)
      It works fine without the
      Code:
      i.unit##c.year
      I am using Stata 15.1

      Comment


      • #4
        Sorry, I don't know anything about bacondecomp. Evidently it is a user-written command and it does not support factor-variable notation. So, I'm afraid you will have to either create those dummies and interaction terms on your own after all, or figure out a way to get equivalent results from a series of official and user-written Stata commands that do support factor-variable notation. And I'm afraid I can't help you with the latter approach because I don't much about Bacon decomposition itself.

        Comment


        • #5
          Bacon's decomposition is intended to basically say, to put it politely, "Forget what you learned in graduate school, 2 way fixed-effect estimators are terrible (under certain, specific conditions)".

          It doesn't make sense to include unit specific linear trends in the DD model when the point of the decomposition is to show how TWFE estimators come up with weird weights in the presence of heterogeneous treatment effects and imbalances in calendar and event time with staggered implementation.

          Furthermore, it's meant for post-estimation. The help file says the command accepts "outcome treatment_indicator [controls] " as the variables of interest, not counting your unit and time variables because it handles those under the hood.

          Comment


          • #6
            Clyde, thanks for the advice

            Jared, I see what you’re saying, but would it be possible to include them manually do you think? Just for fun. The DiD estimate changes quite drastically when I include unit specific linear time trends (which I guess is not a good thing), and I want to see how/if the weights change in the Bacon decomposition.

            Comment


            • #7
              Honestly, the best person to ask about this is Andrew. He'd know much more about this than I would.

              Comment


              • #8
                Hi everyone,
                I'm running into the exact same issue. Not sure (i) if conceptually it is required for a bacondecomp command to include all interactions of the "original" regression; and (ii), if that is the case, how to include them. Maybe the best way would be to manually create a bunch of interactions between year*dummy, and include each of them in the model? But this would mean including a bunch of variables, risking elevating the errors of the model. Any clues?

                Comment

                Working...
                X