Announcement

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

  • How to do a Difference in Difference with Fixed Effect regression?

    Hi everyone.

    I am currently doing a research paper studying how China's Loan Prime Rates (LPR) will affect the profitability of state owned corporations vs non state owned corporations.

    1. We have decided to use DID for our regression and have created dummy variables for year 2016-2019 (dummy = 0) and 2019-2023 (dummy = 1) as well as non state owned corporations (dummy = 0) and state owned (dummy = 1). and we use gen Diff = dummy_cty * dummy_yr. Can I check if we have created this correctly?

    2. However, the LPR is constant from 2016-2019 hence there is a collinearity in the variables. Hence, we decided to bring in fixed effects into this. However, after all the research, we are still unsure how to carry out the regression and what code to write in stata. Could anyone advice how to proceed?

    We are intending to use net income, profit margin, and a few other variables to measure profitability.

  • #2
    Yu:
    welcome to this forum.
    Why not using -didregress- or -xtdidregress-?
    Kind regards,
    Carlo
    (StataNow 18.5)

    Comment


    • #3
      Hey Yu,

      copy what Carlo said. There are also other ways to code a standard Two-Way-Fixed-Effects Diff-in-Diff. I like to code it using the user-written command reghdfe. Also, you can code it using the basic reg command. Both provide the same result as didregress.

      Code:
      ssc install reghdfe
      
      gen Diff = dummy_cty*dummy_yr
      
      * User-written command reghdfe:
      
      reghdfe outcome Diff, absorb(year-FE corporate-FE)
      
      * Build-in command reg:
      
      reg outcome Diff i.year-FE i.corporate-FE
      The ,absorb() part applies the fixed effects. In contrast to reg, reghdfe automatically excludes the fixed effects from display in the result window (which I find handy).

      Good luck with your research!
      Sebastian
      Last edited by Sebastian Schirner; 16 Feb 2024, 08:41.

      Comment


      • #4
        Yu:
        as an aside to Sebastian's helpful reply, you may want to take a look to this (pretty old but still interesting) slide-kit on DID in Stata (PowerPoint Presentation (princeton.edu)).
        Kind regards,
        Carlo
        (StataNow 18.5)

        Comment


        • #5
          Hi Sebastian and Carlo,

          thank you for the reply and the help.

          may I know how do i create year-FE and corporate-FE? Do I use xtset to do it? The way my data is formatted is also slightly different (please see picture attached) where I have duplicates of the same ticker. Does this affect my FE?

          also, can I clarify what is supposed to be i.year-FE? How to I gen this variable?

          thank you again!
          Attached Files

          Comment


          • #6
            Hey Yu,

            to understand your data better please provide example-data using dataex from ssc.

            Regarding your first and second question: The Stata-notation "i." automatically creates the fixed effects (instead of you yourself e.g. creating all dummy variables "manually"). Also, regdhfe , absorb() creates the fixed effects automatically. The fixed effects are created from (typically already existing) variables that indicate the time-dimension (e.g. the year/month/day) and the unit dimension (e.g. an ID for each corporation).

            You need xtset if you use commands that start with xt (such as xtdidregress suggested by Carlo).

            The duplicates in tickers (are these your corporations?) are probably fine if they are in different time periods. Thats exactly what you would want: Multiple corporations in multiple time periods. But we need to see your data to fully assess this.

            Best regards,
            Sebastian

            Comment


            • #7
              Yu:
              as Sebastian wisely advised you about, please use -dataex- and, as per FAQ, do not post screenshots, as they are difficult to read and impossible to elaborate on. Thanks.
              Kind regards,
              Carlo
              (StataNow 18.5)

              Comment


              • #8
                Hi Sebastian and Carlo,

                Apologies for that. Please find my example data sets below. I have also experienced error using -reghdfe outcome Diff, absorb(year-Fe corporate-FE) on Stata 14.0. The error i got was invalid syntax, error occurred while loading reghdfe.ado. So i tried -reg NET_INCOME Diff i.date i.corp_id- and -reg NET_INCOME Diff i.DUMMY_DATE i.corp_id-. Should i be using date or dummy date? I have attached the results i got in this thread. Thank you for the help again.

                Code:
                * Example generated by -dataex-. To install: ssc install dataex
                clear
                input str16 ticker int date double(lproneyr NET_INCOME) byte DUMMY_DATE float(dummy_china corp_id)
                "1 HK Equity"    20635  4.3     14921 0 0 1
                "1 HK Equity"    20819  4.3     18087 0 0 1
                "1 HK Equity"    21000  4.3     15919 0 0 1
                "1 HK Equity"    21184  4.3     19181 0 0 1
                "1 HK Equity"    21365  4.3     18020 0 0 1
                "1 HK Equity"    21549  4.3     20980 0 0 1
                "1 HK Equity"    21730  4.3     18324 1 0 1
                "1 HK Equity"    21914 4.15     21506 1 0 1
                "1 HK Equity"    22096 3.85     13000 1 0 1
                "1 HK Equity"    22280 3.85     16143 1 0 1
                "1 HK Equity"    22461 3.85     18300 1 0 1
                "1 HK Equity"    22645  3.8     15184 1 0 1
                "1 HK Equity"    22826  3.7     19088 1 0 1
                "1 HK Equity"    23010 3.65     17592 1 0 1
                "1 HK Equity"    23191 3.55     11208 1 0 1
                "101 HK Equity"  20635  4.3      2935 0 0 2
                "101 HK Equity"  20819  4.3      3260 0 0 2
                "101 HK Equity"  21000  4.3      3830 0 0 2
                "101 HK Equity"  21184  4.3      4294 0 0 2
                "101 HK Equity"  21365  4.3      4689 0 0 2
                "101 HK Equity"  21549  4.3      3389 0 0 2
                "101 HK Equity"  21730  4.3      3516 1 0 2
                "101 HK Equity"  21914 4.15      2656 1 0 2
                "101 HK Equity"  22096 3.85     -2537 1 0 2
                "101 HK Equity"  22280 3.85       -34 1 0 2
                "101 HK Equity"  22461 3.85      2235 1 0 2
                "101 HK Equity"  22645  3.8      1633 1 0 2
                "101 HK Equity"  22826  3.7      1948 1 0 2
                "101 HK Equity"  23010 3.65      1888 1 0 2
                "101 HK Equity"  23191 3.55      2394 1 0 2
                "1038 HK Equity" 20635  4.3      5819 0 0 3
                "1038 HK Equity" 20819  4.3      4401 0 0 3
                "1038 HK Equity" 21000  4.3      5932 0 0 3
                "1038 HK Equity" 21184  4.3      4950 0 0 3
                "1038 HK Equity" 21365  4.3      6340 0 0 3
                "1038 HK Equity" 21549  4.3      4899 0 0 3
                "1038 HK Equity" 21730  4.3      6341 1 0 3
                "1038 HK Equity" 21914 4.15      4961 1 0 3
                "1038 HK Equity" 22096 3.85      3258 1 0 3
                "1038 HK Equity" 22280 3.85      4858 1 0 3
                "1038 HK Equity" 22461 3.85      3234 1 0 3
                "1038 HK Equity" 22645  3.8      4715 1 0 3
                "1038 HK Equity" 22826  3.7      4628 1 0 3
                "1038 HK Equity" 23010 3.65      3558 1 0 3
                "1038 HK Equity" 23191 3.55      4458 1 0 3
                "1044 HK Equity" 20635  4.3   1635.04 0 0 4
                "1044 HK Equity" 20819  4.3  1961.781 0 0 4
                "1044 HK Equity" 21000  4.3  1853.935 0 0 4
                "1044 HK Equity" 21184  4.3  1940.106 0 0 4
                "1044 HK Equity" 21365  4.3  1946.907 0 0 4
                "1044 HK Equity" 21549  4.3  1852.898 0 0 4
                "1044 HK Equity" 21730  4.3  1877.782 1 0 4
                "1044 HK Equity" 21914 4.15  2029.941 1 0 4
                "1044 HK Equity" 22096 3.85  2259.528 1 0 4
                "1044 HK Equity" 22280 3.85  2335.287 1 0 4
                "1044 HK Equity" 22461 3.85  1860.292 1 0 4
                "1044 HK Equity" 22645  3.8  1413.309 1 0 4
                "1044 HK Equity" 22826  3.7  1276.191 1 0 4
                "1044 HK Equity" 23010 3.65   649.058 1 0 4
                "1044 HK Equity" 23191 3.55  1225.768 1 0 4
                "1088 HK Equity" 20635  4.3      6086 0 0 5
                "1088 HK Equity" 20819  4.3      6262 0 0 5
                "1088 HK Equity" 21000  4.3     12080 0 0 5
                "1088 HK Equity" 21184  4.3      9388 0 0 5
                "1088 HK Equity" 21365  4.3     12317 0 0 5
                "1088 HK Equity" 21549  4.3      6930 0 0 5
                "1088 HK Equity" 21730  4.3     11375 1 0 5
                "1088 HK Equity" 21914 4.15      5457 1 0 5
                "1088 HK Equity" 22096 3.85     10851 1 0 5
                "1088 HK Equity" 22280 3.85      5608 1 0 5
                "1088 HK Equity" 22461 3.85     14636 1 0 5
                "1088 HK Equity" 22645  3.8      9314 1 0 5
                "1088 HK Equity" 22826  3.7     22180 1 0 5
                "1088 HK Equity" 23010 3.65     10130 1 0 5
                "1088 HK Equity" 23191 3.55     16184 1 0 5
                "1093 HK Equity" 20635  4.3  869.0602 0 0 6
                "1093 HK Equity" 20819  4.3  929.3191 0 0 6
                "1093 HK Equity" 21000  4.3  1160.144 0 0 6
                "1093 HK Equity" 21184  4.3 1238.2518 0 0 6
                "1093 HK Equity" 21365  4.3   1504.74 0 0 6
                "1093 HK Equity" 21549  4.3  1576.062 0 0 6
                "1093 HK Equity" 21730  4.3  1878.284 1 0 6
                "1093 HK Equity" 21914 4.15  1835.822 1 0 6
                "1093 HK Equity" 22096 3.85  2313.996 1 0 6
                "1093 HK Equity" 22280 3.85  2845.659 1 0 6
                "1093 HK Equity" 22461 3.85  3062.569 1 0 6
                "1093 HK Equity" 22645  3.8  2542.616 1 0 6
                "1093 HK Equity" 22826  3.7  2966.205 1 0 6
                "1093 HK Equity" 23010 3.65  3125.185 1 0 6
                "1093 HK Equity" 23191 3.55  2966.987 1 0 6
                "11 HK Equity"   20635  4.3      8005 0 0 7
                "11 HK Equity"   20819  4.3      8207 0 0 7
                "11 HK Equity"   21000  4.3      9838 0 0 7
                "11 HK Equity"   21184  4.3     10180 0 0 7
                "11 HK Equity"   21365  4.3     12647 0 0 7
                "11 HK Equity"   21549  4.3     11564 0 0 7
                "11 HK Equity"   21730  4.3     13656 1 0 7
                "11 HK Equity"   21914 4.15     11184 1 0 7
                "11 HK Equity"   22096 3.85      9143 1 0 7
                "11 HK Equity"   22280 3.85      7544 1 0 7
                end
                format %tdnn/dd/CCYY date
                Attached Files

                Comment


                • #9
                  Hey Yu,

                  as far as I can see you should be using date for the fixed effects. In principle, this code that you provided should do the job:

                  Code:
                  reg NET_INCOME Diff i.date i.corp_id
                  
                  * Alternatively:
                  reghdfe NET_INCOME Diff, absorb(date corp_id)
                  Why reghdfe is not working in your case is strange. There should be no additional loading of the ado file. ssc install takes care of that.

                  An additional thing: You probably would want to include robust and/or clustered standard errors. In Diff-in-Diff designs, standard errors are typically clustered at the level of the treatment (in your case at the firm level). But make sure to read more about that and follow the convention in the literature that you are speaking to.

                  Best,
                  Sebastian

                  Comment


                  • #10
                    Yu:
                    I do not understand why you skipper -xtreg,fe- altogether and prefer a pooled OLS to deal with a panel data regression.
                    In addition, the -reghdfe- code you sketched is equivalent to a two-way fixed effect that you can perform via -xtreg,fe-.
                    I do share Sebastian's wise recommendation to use cluster-robust standard errors (provided that you panels are at lesst 30).
                    Kind regards,
                    Carlo
                    (StataNow 18.5)

                    Comment

                    Working...
                    X