Announcement

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

  • Quick question, two fixed effects

    Hello!

    I would appreciate an answer to this question.
    If I use xtreg, and I order the panel data with xtset, how do I do so that I have fixed effects for two variables?

    In my case:

    xtset country_id year
    xtreg (Y-variable) (X-variable) (X-variable), fe

    In this case, I get fixed effects for the variable 'country_id'. But how do so that I have fixed effects for year, and then year+countries?

    I got an error message when I typed 'xtset historical_date (my other time variable) year', saying that "repeated time variables aren't allowed in the panel".


    Thank you.


  • #2
    Originally posted by Joh Sko View Post

    xtset country_id year
    xtreg (Y-variable) (X-variable) (X-variable), fe

    In this case, I get fixed effects for the variable 'country_id'. But how do so that I have fixed effects for year, and then year+countries?
    In xtreg, you include a second set of fixed effects using dummies.

    Code:
    xtset depvar indepvars i.year, fe
    If you got no errors from your xtset command, it means that you have panel data. Therefore, the combination of country and year defines a single observation and it is not possible to have country\(\times\) year fixed effects. Install reghdfe from SSC which will allow you to absorb multiple levels of fixed effects.

    Code:
    ssc install reghdfe
    help reghdfe

    Comment


    • #3
      Originally posted by Andrew Musau View Post

      In xtreg, you include a second set of fixed effects using dummies.

      Code:
      xtset depvar indepvars i.year, fe
      If you got no errors from your xtset command, it means that you have panel data. Therefore, the combination of country and year defines a single observation and it is not possible to have country\(\times\) year fixed effects. Install reghdfe from SSC which will allow you to absorb multiple levels of fixed effects.

      Code:
      ssc install reghdfe
      help reghdfe

      Thanks, but..

      'xtset depvar indepvars i.year, fe' did not work for me. I just get an error saying 'factor-variable and time-series operators not allowed'


      Using reghdfe, what would be the syntax for running xtreg with both year and country fixed effects?

      Comment


      • #4
        You do

        Code:
        xtset id
        
        xtreg y x i.year, fe
        For the -reghdfe- read the help file, but the syntax will be something like

        Code:
        reghdfe y x, absorb(id year)

        Comment


        • #5
          Joh:
          try:
          Code:
          xtset panelid year
          xtreg depvar indepvars i.year, fe
          The following toy-example includes both -xtreg,fe. and -reghdfe-:
          Code:
          . use "https://www.stata-press.com/data/r17/nlswork.dta"
          (National Longitudinal Survey of Young Women, 14-24 years old in 1968)
          
          . xtreg ln_wage i.year c.age##c.age, fe vce(cluster idcode)
          
          Fixed-effects (within) regression               Number of obs     =     28,510
          Group variable: idcode                          Number of groups  =      4,710
          
          R-squared:                                      Obs per group:
               Within  = 0.1162                                         min =          1
               Between = 0.1078                                         avg =        6.1
               Overall = 0.0932                                         max =         15
          
                                                          F(16,4709)        =      79.11
          corr(u_i, Xb) = 0.0613                          Prob > F          =     0.0000
          
                                       (Std. err. adjusted for 4,710 clusters in idcode)
          ------------------------------------------------------------------------------
                       |               Robust
               ln_wage | Coefficient  std. err.      t    P>|t|     [95% conf. interval]
          -------------+----------------------------------------------------------------
                  year |
                   69  |   .0647054   .0155249     4.17   0.000     .0342693    .0951415
                   70  |   .0284423   .0264639     1.07   0.283    -.0234395     .080324
                   71  |   .0579959   .0384111     1.51   0.131    -.0173078    .1332996
                   72  |   .0510671   .0502675     1.02   0.310    -.0474808     .149615
                   73  |   .0424104   .0624924     0.68   0.497    -.0801038    .1649247
                   75  |   .0151376    .086228     0.18   0.861    -.1539096    .1841848
                   77  |   .0340933   .1106841     0.31   0.758    -.1828994     .251086
                   78  |   .0537334   .1232232     0.44   0.663    -.1878417    .2953084
                   80  |   .0369475   .1473725     0.25   0.802    -.2519716    .3258667
                   82  |   .0391687   .1715621     0.23   0.819    -.2971733    .3755108
                   83  |    .058766   .1836086     0.32   0.749    -.3011928    .4187249
                   85  |   .1042758   .2080199     0.50   0.616    -.3035406    .5120922
                   87  |   .1242272   .2327328     0.53   0.594    -.3320379    .5804922
                   88  |   .1904977   .2486083     0.77   0.444    -.2968909    .6778863
                       |
                   age |   .0728746    .013687     5.32   0.000     .0460416    .0997075
                       |
           c.age#c.age |  -.0010113   .0001076    -9.40   0.000    -.0012224   -.0008003
                       |
                 _cons |   .3937532   .2469015     1.59   0.111    -.0902893    .8777957
          -------------+----------------------------------------------------------------
               sigma_u |  .40275174
               sigma_e |  .30127563
                   rho |  .64120306   (fraction of variance due to u_i)
          ------------------------------------------------------------------------------
          
          . reghdfe ln_wage c.age##c.age, abs(idcode year) vce(cluster idcode)
          (dropped 551 singleton observations)
          (MWFE estimator converged in 8 iterations)
          
          HDFE Linear regression                            Number of obs   =     27,959
          Absorbing 2 HDFE groups                           F(   2,   4158) =      44.91
          Statistics robust to heteroskedasticity           Prob > F        =     0.0000
                                                            R-squared       =     0.6593
                                                            Adj R-squared   =     0.5995
                                                            Within R-sq.    =     0.0115
          Number of clusters (idcode)  =      4,159         Root MSE        =     0.3013
          
                                       (Std. err. adjusted for 4,159 clusters in idcode)
          ------------------------------------------------------------------------------
                       |               Robust
               ln_wage | Coefficient  std. err.      t    P>|t|     [95% conf. interval]
          -------------+----------------------------------------------------------------
                   age |   .0728746   .0136873     5.32   0.000     .0460402    .0997089
                       |
           c.age#c.age |  -.0010113   .0001076    -9.39   0.000    -.0012224   -.0008003
                       |
                 _cons |   .4586164   .3651743     1.26   0.209    -.2573205    1.174553
          ------------------------------------------------------------------------------
          
          Absorbed degrees of freedom:
          -----------------------------------------------------+
           Absorbed FE | Categories  - Redundant  = Num. Coefs |
          -------------+---------------------------------------|
                idcode |      4159        4159           0    *|
                  year |        15           0          15     |
          -----------------------------------------------------+
          * = FE nested within cluster; treated as redundant for DoF computation
          
          .
          Kind regards,
          Carlo
          (Stata 19.0)

          Comment


          • #6
            Originally posted by Joh Sko View Post

            xtset depvar indepvars i.year, fe did not work for me. I just get an error saying 'factor-variable and time-series operators not allowed'
            That's a typo. It should be xtreg as Joro Kolev and Carlo Lazzaro show above.

            Comment

            Working...
            X