Announcement

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

  • generation of a variable, y, that interacts between another variable x and year indictor i.year

    Dear all,
    I am trying to generate variable y that interacts with variable x and year indicator in panel data, where x = housingsupply in year 1 that varies across counties, and I want to generate a variable, y, that is based on housingsupply in year1 and evolves over time according to the year-fixed effect after year 1. So y should vary across year and county. The idea is similar to a Bartik-style instrument, where housingsupply in year 1 serves as a historic share.
    So, I did
    sort wave
    gen y = x * i.year
    I got the error message,
    i: operator invalid
    r(198);
    It appears that Stata does not allow for generation of a variable that interacts between another variable and i.year?


    Thanks,
    Kangoh

  • #2
    No, you can't do that because i.year is not, itself, a variable: it is an abbreviation for a series of variables, one for each year except for a reference year. -gen- can only work with variables.

    The question is why you even want to do this in the first place. If your purpose is to use this interaction in a regression of some kind, you don't need to create the variables. You can have Stata do it "on the fly" in the regression command:

    Code:
    regression_command depvar ... c.x#i.year...
    Note: I used c.x here on the assumption that the x variable is continuous.

    If you have some other reason for wanting to create these variables, perhaps you can explain what you plan to do with them, and then advice on how to accomplish that might be available.

    Comment


    • #3
      Thanks, Clyde.
      Yes, I know that it works in regressions.
      However, before I use the interaction variable in regressions, I want to understand the properties of the variable such as the mean and s.d.
      It does not appear to be possible to know what c.x#i.year looks like, and I believe that it does not vary across year.

      I did:
      bys county: fillmissing x
      tabstat x, by(year)
      by wave: summ c.x#i.year

      Then, I got the same mean for every year, so the interaction variable does not vary across year, while I want the interaction variable to very across year.

      Thanks,
      Kangoh
      Last edited by kangoh lee; 16 Oct 2025, 21:50.

      Comment


      • #4
        Originally posted by kangoh lee View Post
        . . . x = housingsupply in year 1 that varies across counties . . .

        Originally posted by kangoh lee View Post
        Then, I got the same mean for every year, so the interaction variable does not vary across year, while I want the interaction variable to very across year.
        But x doesn't vary across years. Why are you surprised that you get the same mean for x across years?

        Comment


        • #5
          I am surprised that x#i.year, not x, does not vary across years.

          Comment


          • #6
            Originally posted by kangoh lee View Post
            I am surprised that x#i.year, not x, does not vary across years.
            I think that you're confusing predictors and predictions.

            If you're tabulating x#i.year, then those are predictors, and if x is a baseline value (i.e., is time-invariant), then there is no expectation that a cross-tabulation of it and i.year will vary across years.

            But predictions of a regression model where as Clyde said c.x##i.year are predictors and where a nonadditive effect of their combination (an interaction of x and year) is present, then, yes, the model's predictions will vary across years.

            Comment


            • #7
              Thanks, and you are correct.
              However, I repeat myself, and I am interested in understanding the properties of the predictor, c.x#i.year.
              For example, what is the mean and s,d, of the predictor, how does the predictor change over years,...
              We typically study the nature and properties of the independent variable and other covariates (controls), not just the regression results.

              Comment


              • #8
                Originally posted by kangoh lee View Post
                [...] I am interested in understanding the properties of the predictor, c.x#i.year.
                It's not the (as in one) predictor. There are two predictors: x and year. Their interaction is not another predictor. It's a way of relaxing a purely additive model.

                Anyway, the term c.x#i.year specifies as many variables as there are years (typically minus one for the reference year, depending on model parametrization). So there's more than one mean or standard deviation. If you really want to see the implied variables, you can type
                Code:
                xi i.year*x
                You can then type
                Code:
                summarize _I*
                and look at summary statistics. However, you're probably better off looking at x and year separately because, as said, the interaction terms are not to be interpreted as independent predictors.
                Last edited by daniel klein; 17 Oct 2025, 02:20.

                Comment


                • #9
                  Thanks, Daniel.
                  I got summary statistics. The mean of the interaction variable is zero for all years, except for year ==1, which does not seem to make sense.
                  It is frustrating, as I would like to see how the mean of the interaction variable evolves over years, but Stata does not seem to allow for it.

                  Comment


                  • #10
                    From looking through what you have been posting, I am fairly confident that this is not a Stata limitation (you being unable to do something because "Stata does not seem to allow for it"), but some degree of confusion on your own part about what you are trying to do.

                    For starters, could you provide us a data extract (using dataex -- see FAQ #12)? I'd like to see a small example of how x varies by year, county, and wave.

                    Comment


                    • #11
                      Thanks.
                      x does not vary with year, as it is housing supply in year 1 but varies with county.
                      Yes, it is my limitation, and I do not know how to display the mean of x#i.year by year.
                      I think it is a generic question, as x#i.year is a vector, and the question is how to display the mean of the vector by the indictor variable.

                      Comment


                      • #12
                        You still don't provide a data extract, but I shall try and help with whatever limited thoughts I have in the absence of that.

                        You are conceptualizing it incorrectly and/or using Stata notation incorrectly.

                        First: The factor variable interaction term # helps generate multiple internal variables corresponding to all the relevant products. Specifically, x#i.year corresponds to k products, one of each of the k values of year. So no, it is not a vector, it is a matrix with n rows (where n is the number of observations) and k columns. Of course, I am just repeating what Daniel has already tried to explain to you in #8. He has also explained to you how to show the means of all of these k variables. You seem to have got unexpected results by doing that. I cannot help you there without a data extract. But see my second point below.

                        Second: If, as you say, x does not vary by year, then why is the product of x with year, by year, even interesting? The answer should be absolutely predictable: since for each year, x would take on the same average value -- call it xbar, the products with year will be just scaled versions of xbar. E.g. if the years are 0, 1, 2, 3, 4, then the respective means by year will be 0, xbar, 2xbar, 3xbar and 4xbar. If you are not expecting this, then you have some conceptual confusion.

                        Comment


                        • #13
                          Thanks, and yes, you are correct, and I apologize for my mistake.
                          As in my original question, I wanted x to evolve according to year-fixed effect, and I thought x#i.wave would do it, which is incorrect.
                          If there is any way to create a variable (or interaction or anything else) that enables x to evolve according to year-fixed effects, let me know.

                          Comment

                          Working...
                          X