Announcement

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

  • Comparison CPI between EU and UK before and after Brexit (Diff-in-Diff estimation)

    I want to run a diff-in-diff regression for the UK and the EU (I took the Euro-area countries) for comparing the behaviour of the Consumer Price Indices (CPI) between those two groups. I generated a binary variable for the treatment group (UK) and the control group (Euro area). Now I'm struggling to create a binary variable which separate the observations into two groups (before and after the treatment). I chose the 30jun2016 as treatment date. I have observations for each month from 31jan2010 until 31jul2023. STATA didn't accept my code:

    Code:
    gen pre_brexit = (date < "30jun2016")
    gen post_brexit = (date >= "30jun2016")
    Have somebody a hint, how I can solve this problem?

    Besides the variable for CPI, I have some other variables in my data set like the interest rate determined by the central banks for each month. For taking care of the effects of theCovid-19 pandemic I have variables like the stringency index, economic support for firms and the total numbers of vaccinations. How would a diff-in-diff equation looks like in STATA, if control for these variables as well?

  • #2
    Because you did not use -dataex- and show example data, nobody can give you a specific answer here. The fix depends on details of your data and metadata that you did not disclose. Nor do you say whether the code you show produced error messages (and what they are, if so) or ran without error messages but produced incorrect results.

    In general terms, you are trying to compare a variable named date to a string constant "30jun2016". There are two general cases with separate reasons why they will go wrong.

    The first case is that date is also a string variable. In that case, the code will produce no error messages, but the results will not be what you want because the alphabetic order comparison does not correspond to chronological order. For example "1jul2016" (or "01jul2016") alphabetically precedes "30jun2016". The second case is that date is actually a Stata internal format numeric date variable. In that case, the comparison to a string is a syntax error, specifically "type mismatch".

    Ultimately, to work with dates, you need them to be Stata internal format date variables. If your variable date is a string, you will need to convert it, using the -daily()- function. (See -help datetime functions- for details.) And then to compare it to the constant date 30jun2016, the correct code will be:
    Code:
    gen pre_brexit = date < td(30jun2016)
    gen post_brexit = date >= td(30jun2016)
    Note the use of the -td()- function here: direct comparison to a string constant is not allowed, but -td()- converts strings in this format to the correct corresponding numeric value for Stata dates.

    Let me also point out that unless you are using some user-written command for your DID estimation that specifically requires separate variables for the pre- and post-Brexit periods, you don't actually need both of these variables. With Stata's built-in commands (as well as those user-written ones for DID estimation that I'm aware of) you will need only a single pre vs post indicator:
    Code:
    gen byte pre_post = date >= td(30jun2016)
    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Added: I did not respond to the questions raised in the final paragraph of #1 because I am not familiar with these variables or what they represent, so I don't know how one would use them. Perhaps somebody else following the thread is familiar with them and can suggest an appropriate model.

    If nobody does that within, say, 48 hours, I would recommend reposting the final paragraph as a new thread, but expanding it to explain what those variables are, what they represent, where they come from, what type of data they are, and, unless it is blatantly obvious at that point, what their relationship to CPI is, or might plausibly be.
    Last edited by Clyde Schechter; 10 Sep 2023, 10:40.

    Comment


    • #3
      Hi

      Thanks for your answer. Sorry, I was not aware of -dataex-.

      Below, I exported a sample of my data set:

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input str4 Country int Date double CPI float(ln_CPI Year) double interest long vacs double(stringency econ_sup)
      "CH" 18293   99.64195 4.6015835 2010 .375 0 0 0
      "CH" 18321  99.787004  4.603038 2010 .375 0 0 0
      "CH" 18352  99.923791  4.604408 2010 .375 0 0 0
      "CH" 18382 100.777777  4.612918 2010 .375 0 0 0
      "CH" 18413 100.673661  4.611884 2010 .375 0 0 0
      "CH" 18443  100.23545  4.607522 2010 .375 0 0 0
      "CH" 18474  99.502998  4.600188 2010 .375 0 0 0
      "CH" 18505  99.489221 4.6000495 2010 .375 0 0 0
      "CH" 18535  99.466193 4.5998178 2010 .375 0 0 0
      "CH" 18566  99.995432 4.6051245 2010 .375 0 0 0
      "CH" 18596  100.23545  4.607522 2010 .375 0 0 0
      "CH" 18627 100.271073 4.6078773 2010 .375 0 0 0
      "CH" 18658  99.919461 4.6043644 2011 .375 0 0 0
      "CH" 18686 100.296069 4.6081266 2011 .375 0 0 0
      "CH" 18717 100.927259    4.6144 2011 .375 0 0 0
      "CH" 18747 101.038657 4.6155033 2011 .375 0 0 0
      "CH" 18778 101.045348 4.6155696 2011 .375 0 0 0
      "CH" 18808 100.801099  4.613149 2011 .375 0 0 0
      "CH" 18839  99.972798  4.604898 2011 .375 0 0 0
      "CH" 18870  99.680526   4.60197 2011 .125 0 0 0
      "CH" 18900  99.949279  4.604663 2011 .125 0 0 0
      "CH" 18931  99.882952  4.603999 2011 .125 0 0 0
      "CH" 18961  99.708573 4.6022515 2011 .125 0 0 0
      "CH" 18992  99.554465 4.6007047 2011 .125 0 0 0
      "CH" 19023  99.130622 4.5964384 2012 .125 0 0 0
      "CH" 19052  99.409904 4.5992517 2012 .125 0 0 0
      "CH" 19083   99.96404 4.6048107 2012 .125 0 0 0
      "CH" 19113 100.038929 4.6055593 2012 .125 0 0 0
      "CH" 19144 100.021018 4.6053805 2012 .125 0 0 0
      "CH" 19174  99.738882 4.6025558 2012 .125 0 0 0
      "CH" 19205  99.258946  4.597732 2012 .125 0 0 0
      "CH" 19236  99.228833  4.597429 2012 .125 0 0 0
      "CH" 19266   99.53813 4.6005406 2012 .125 0 0 0
      "CH" 19297   99.66114 4.6017756 2012 .125 0 0 0
      "CH" 19327  99.334228   4.59849 2012 .125 0 0 0
      "CH" 19358   99.12206  4.596352 2012 .125 0 0 0
      "CH" 19389  98.863247 4.5937376 2013 .125 0 0 0
      "CH" 19417  99.155421 4.5966887 2013 .125 0 0 0
      "CH" 19448  99.370836  4.598859 2013 .125 0 0 0
      "CH" 19478  99.404196 4.5991945 2013 .125 0 0 0
      "CH" 19509  99.513528 4.6002936 2013 .125 0 0 0
      "CH" 19539  99.611345  4.601276 2013 .125 0 0 0
      "CH" 19570  99.256191 4.5977044 2013 .125 0 0 0
      "CH" 19601  99.184648 4.5969834 2013 .125 0 0 0
      "CH" 19631  99.483021  4.599987 2013 .125 0 0 0
      "CH" 19662  99.406657  4.599219 2013 .125 0 0 0
      "CH" 19692  99.413545 4.5992885 2013 .125 0 0 0
      "CH" 19723  99.188486  4.597022 2013 .125 0 0 0
      "CH" 19754  98.917076 4.5942817 2014 .125 0 0 0
      "CH" 19782  99.015386 4.5952754 2014 .125 0 0 0
      "CH" 19813  99.373591 4.5988865 2014 .125 0 0 0
      "CH" 19843  99.441985 4.5995746 2014 .125 0 0 0
      "CH" 19874  99.733273 4.6024995 2014 .125 0 0 0
      "CH" 19904  99.660845  4.601773 2014 .125 0 0 0
      "CH" 19935   99.28778 4.5980225 2014 .125 0 0 0
      "CH" 19966  99.255797    4.5977 2014 .125 0 0 0
      "CH" 19996  99.395832   4.59911 2014 .125 0 0 0
      "CH" 20027   99.39406 4.5990925 2014 .125 0 0 0
      "CH" 20057  99.355287  4.598702 2014 .125 0 0 0
      "CH" 20088  98.862656 4.5937314 2014 -.25 0 0 0
      "CH" 20119  98.437041  4.589417 2015 -.75 0 0 0
      "CH" 20147  98.184133 4.5868444 2015 -.75 0 0 0
      "CH" 20178  98.504353  4.590101 2015 -.75 0 0 0
      "CH" 20208  98.325643  4.588285 2015 -.75 0 0 0
      "CH" 20239  98.554541   4.59061 2015 -.75 0 0 0
      "CH" 20269  98.626969  4.591345 2015 -.75 0 0 0
      "CH" 20300  98.020874 4.5851803 2015 -.75 0 0 0
      "CH" 20331  97.863716 4.5835757 2015 -.75 0 0 0
      "CH" 20361  97.960845  4.584568 2015 -.75 0 0 0
      "CH" 20392  98.042523  4.585401 2015 -.75 0 0 0
      "CH" 20422  97.971768  4.584679 2015 -.75 0 0 0
      "CH" 20453   97.56918 4.5805616 2015 -.75 0 0 0
      "CH" 20484  97.190112  4.576669 2016 -.75 0 0 0
      "CH" 20513  97.370593  4.578524 2016 -.75 0 0 0
      "CH" 20544  97.656468 4.5814557 2016 -.75 0 0 0
      "CH" 20574  97.979542 4.5847588 2016 -.75 0 0 0
      "CH" 20605  98.120955 4.5862007 2016 -.75 0 0 0
      "CH" 20635  98.258037  4.587597 2016 -.75 0 0 0
      "CH" 20666  97.821991 4.5831494 2016 -.75 0 0 0
      "CH" 20697  97.723681  4.582144 2016 -.75 0 0 0
      "CH" 20727   97.78312 4.5827518 2016 -.75 0 0 0
      "CH" 20758    97.8338   4.58327 2016 -.75 0 0 0
      "CH" 20788  97.640329 4.5812907 2016 -.75 0 0 0
      "CH" 20819  97.562685  4.580495 2016 -.75 0 0 0
      "CH" 20850  97.529325  4.580153 2017 -.75 0 0 0
      "CH" 20878  97.999617  4.584964 2017 -.75 0 0 0
      "CH" 20909  98.210998  4.587118 2017 -.75 0 0 0
      "CH" 20939  98.408503  4.589127 2017 -.75 0 0 0
      "CH" 20970  98.591345 4.5909834 2017 -.75 0 0 0
      "CH" 21000  98.451803  4.589567 2017 -.75 0 0 0
      "CH" 21031   98.14408 4.5864367 2017 -.75 0 0 0
      "CH" 21062  98.175866 4.5867605 2017 -.75 0 0 0
      "CH" 21092  98.420706  4.589251 2017 -.75 0 0 0
      "CH" 21123  98.480439  4.589858 2017 -.75 0 0 0
      "CH" 21153  98.408897 4.5891314 2017 -.75 0 0 0
      "CH" 21184   98.38085  4.588846 2017 -.75 0 0 0
      "CH" 21215  98.251247 4.5875278 2018 -.75 0 0 0
      "CH" 21243  98.615062  4.591224 2018 -.75 0 0 0
      "CH" 21274  98.992162  4.595041 2018 -.75 0 0 0
      "CH" 21304  99.198917  4.597127 2018 -.75 0 0 0
      end
      format %td Date
      As you suggested the daily function, I made some research and found the -date- function.

      My code:

      Code:
      // Formate variable Date
      gen date = date(Date, "DMY")
      But STATA gave me the error message: type mismatch

      Have you any hint for this problem?
      Last edited by Severin Voll; 11 Sep 2023, 09:40.

      Comment


      • #4
        Code:
        . list Date in 1/5
        
             +-----------+
             |      Date |
             |-----------|
          1. | 31jan2010 |
          2. | 28feb2010 |
          3. | 31mar2010 |
          4. | 30apr2010 |
          5. | 31may2010 |
             +-----------+
        
        . des Date
        
        Variable      Storage   Display    Value
            name         type    format    label      Variable label
        ----------------------------------------------------------------------------------------------------------------------------------------------
        Date            int     %td
        Date is already a Stata internal format date variable. It may look like a string in listings, because that is what format %td does. But it is actually numeric. If you look at the -dataex- output you posted, you will also see that in the Date column of that listing you see numbers like 21304, not "30apr2018". In Stata, by design, what you see is not necessarily what you have. Dates are complicated: to do calculations with dates, they need to be numeric variables that count of the time elapsed (number of days for a daily date variable) since some reference point (which, for Stata, is 1 Jan 1960). But human brains can't wrap their minds around those. So, while maintaining the variables as numeric internally, Stata can display them in human readable format in -list-ings and other output, as well as in the data editor. To know whether you have a string or a numeric date variable, you can't just look at outputs containing its values. You need to run -des- and look at the storage type and display format.

        Ok, with that background in mind, to generate your indicator variables, given that Date is already a Stata numeric date variable:
        Code:
        gen pre_brexit = Date < td(30jun2016)
        gen post_brexit = Date >= td(30jun2016)
        Or, as I previously suggested, you probably don't need both:
        Code:
        gen byte pre_post = Date >= td(30jun2016)
        Finally, I'm a little surprised that you were able to find -date()- but not -daily()-. The two are synonyms, so fine. But -help daily()- would show you the -daily()- function. Or if you just scrolled through -help datetime functions-, -daily()-, being alphabetically earlier than -date()- would have crossed your screen first. I'm wondering if you are using an older version of Stata that predates the introduction of -daily()-. For the purposes of the present thread, it makes no difference. But, in general, when you are not using the current version of Stata (18 at this point), it is wise to say what version you are using, so that you will not be given solutions that you cannot use with your version.

        Comment

        Working...
        X