Announcement

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

  • Calculating annual % changes in panel data

    I am running a Diff-in-Diff fixed effects model, where I am evaluating a policy's impact at the neighborhood-level, and I am collapsing the outcome variable at the month-level.

    However, I am trying to add controls for annual percentage changes for oil prices, which I suspect influences both my treatment and control observations. However, I have oil prices by year and at the country-level, rather than neighborhood-level.

    I tried to create a variable capturing yearly percentage changes for oil prices as follows:
    ```
    foreach v of varlist oil_avg {
    gen change_`v' = D.`v'
    }
    replace change_oil_avg=0 if change_oil_avg==.
    ```

    Here is a data example:
    ```
    . dataex numeric_neighborhood oil_avg change_oil_avg hijri_yr month

    ----------------------- copy starting from the next line -----------------------
    [CODE]
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long numeric_neighborhood float(oil_avg change_oil_avg hijri_yr month)
    1 54.13 0 1438 76
    2 79.61 0 1431 1
    2 111.26 0 1432 4
    2 111.63 0 1433 18
    2 108.56 0 1434 34
    3 64.3 0 1440 110
    3 41.96 -22.340004 1441 111
    3 41.96 0 1441 112
    3 41.96 0 1441 115
    3 41.96 0 1441 116
    4 41.96 0 1441 113
    4 41.96 0 1441 114
    4 41.96 0 1441 117
    5 111.63 0 1433 25
    6 71.34 0 1439 95
    7 108.56 0 1434 30
    7 52.32 0 1436 53
    7 52.32 0 1436 57
    7 52.32 0 1436 62
    7 43.64 0 1437 65
    7 54.13 0 1438 79
    ```

    I then added the "change_oil_avg" variable to the DiD model as follows:
    ```
    xtreg tobacco_sales i.DiD_implementation i.change_oil_avg i.gender i.education i.month, fe cluster(numeric_neighborhood)
    ```

    However, I keep receiving the following error message
    ```
    change_oil_avg: factor variables may not contain noninteger values
    ```

    Here is a data description of the "change_oil_avg" variable:
    ```
    desc change_oil_avg

    Variable Storage Display Value
    name type format label Variable label
    ---------------------------------------------------------------------------------------------------------------------------
    change_oil_avg float %9.0g
    ```

  • #2
    This?

    Comment


    • #3
      Originally posted by Jared Greathouse View Post
      Thanks, I actually saw and tried that as:
      ```
      sort numeric_neighborhood hijri_yr
      bysort numeric_neighborhood:gen pchange=100*(oil_avg[_n]-oil_avg[_n-1])/oil_avg[_n-1]
      ```
      The code works, but I keep receiving the same error message.

      Comment


      • #4
        In your xtreg command from post #1
        Code:
        xtreg tobacco_sales i.DiD_implementation i.change_oil_avg i.gender i.education i.month, fe cluster(numeric_neighborhood)
        change_oil_avg is not a categorical variable so it should not have the i. prefix; that is what the Stata error message was telling you.

        Similarly, pchange in post #3 is not a categorical so if you included i.pchange in your model that was the source of the error.

        Comment


        • #5
          Originally posted by William Lisowski View Post
          In your xtreg command from post #1
          Code:
          xtreg tobacco_sales i.DiD_implementation i.change_oil_avg i.gender i.education i.month, fe cluster(numeric_neighborhood)
          change_oil_avg is not a categorical variable so it should not have the i. prefix; that is what the Stata error message was telling you.

          Similarly, pchange in post #3 is not a categorical so if you included i.pchange in your model that was the source of the error.
          Thanks, I saw one relevant solution to recode the variable here: https://stats.oarc.ucla.edu/stata/fa...s-into-groups/

          However, if my goal is to capture the annual variation in oil prices and their potential impact on my outcome variable, does it make sense to recode the variable into a categorical one?

          Comment


          • #6
            It is unclear whether you understood my advice in post #4.

            Where your xtreg command now is, for example,
            Code:
            xtreg tobacco_sales i.DiD_implementation i.change_oil_avg i.gender i.education i.month, fe cluster(numeric_neighborhood)
            you could remove the i. prefix on change_oil_avg so your model becomes
            Code:
            xtreg tobacco_sales i.DiD_implementation change_oil_avg i.gender i.education i.month, fe cluster(numeric_neighborhood)
            and thereby avoid the error message.

            Why would you want to convert change_oil_avg to a categorical variable if there is no need to do so?

            Comment


            • #7
              Originally posted by William Lisowski View Post
              It is unclear whether you understood my advice in post #4.

              Where your xtreg command now is, for example,
              Code:
              xtreg tobacco_sales i.DiD_implementation i.change_oil_avg i.gender i.education i.month, fe cluster(numeric_neighborhood)
              you could remove the i. prefix on change_oil_avg so your model becomes
              Code:
              xtreg tobacco_sales i.DiD_implementation change_oil_avg i.gender i.education i.month, fe cluster(numeric_neighborhood)
              and thereby avoid the error message.

              Why would you want to convert change_oil_avg to a categorical variable if there is no need to do so?
              Thanks, indeed I missed that and your solution solved the problem!

              Comment

              Working...
              X