Announcement

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

  • Interacting variables using command reg2hdfe

    Hello,

    I have the following database:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int year long gid int country_code float dep_var str34 country float prec_gpcc double Grid_Fix
    1998  87538 426 1 "Lesotho"                              200.55                      0
    2000 103362 516 1 "Namibia"                             164.495     .05380343156866729
    2000 103363 516 1 "Namibia"                            186.0725       .122654800536111
    2001 103364 516 1 "Namibia"                            142.0425    .062420822883723304
    2000 104077 516 1 "Namibia"                            116.0225     .21106905583701518
    2000 104078 516 1 "Namibia"                              107.92      .1652349072915058
    2001 104078  24 1 "Namibia"                            141.2475      .1652349072915058
    2000 104079 516 1 "Angola"                              104.095      .0145694757812862
    2001 104079 516 1 "Angola"                             140.1975      .0145694757812862
    1999 104080 516 1 "Angola"                             139.3075    .014651511770580328
    2000 104080 516 1 "Angola"                              124.715    .014651511770580328
    2001 104080 516 1 "Angola"                             155.2675    .014651511770580328
    2000 104081 516 1 "Angola"                             136.2725    .007587175757066689
    2000 104083  24 1 "Angola"                             181.5175    .011220968579038981
    2000 104792 516 1 "Angola"                             149.2125     .05242094327695668
    2000 104797 516 1 "Angola"                              118.545     .01234819730836989
    1998 107673  24 1 "Angola"                              163.725    .013085424508915366
    1998 107674  24 1 "Angola"                             161.1975    .021263801726718157
    2002 107685 894 1 "Zambia"                             170.7375    .014658009429695085
    1999 107697 894 1 "Zambia"                                196.5     .02923699520761147
    1997 108388  24 1 "Angola"                             232.3975    .018561954074016285
    2000 108392  24 1 "Angola"                              190.675    .013132140469394926
    1999 108393  24 1 "Angola"                             202.9175     .01313219772202956
    2000 109109  24 1 "Angola"                             250.1525    .018562012112852244
    1997 109110  24 1 "Angola"                               271.35     .01856201348016384
    1998 109110  24 1 "Angola"                             214.4575     .01856201348016384
    1999 109113  24 1 "Angola"                             198.2925    .013132198242658433
    1998 109123  24 1 "Angola"                              225.075                      0
    1998 109828  24 1 "Angola"                             228.4525    .013154803685381466
    2001 109828  24 1 "Angola"                             315.7675    .013154803685381466
    2000 109829  24 1 "Angola"                              260.125    .012839902408245507
    1997 109831  24 1 "Angola"                             296.1275    .008421999419226722
    2001 109833  24 1 "Angola"                             199.0425    .011025086708968956
    1998 109834  24 1 "Angola"                             176.0175     .01655012812932455
    1999 109834  24 1 "Angola"                             202.1925     .01655012812932455
    1997 109835  24 1 "Angola"                              285.545    .020990719991459628
    2001 110547  24 1 "Angola"                             245.5325    .010242043200548778
    2000 110548  24 1 "Angola"                             232.8175    .003888186707520447
    2002 110548  24 1 "Angola"                              228.355    .003888186707520447
    1997 110549  24 1 "Angola"                              285.825                      0
    1998 110550  24 1 "Angola"                             245.4575                      0
    2000 110550  24 1 "Angola"                             236.0225                      0
    1998 110551  24 1 "Angola"                              212.715                      0
    1998 110552  24 1 "Angola"                               199.49                      0
    2001 110554  24 1 "Angola"                             187.1325    .018246799613052644
    2000 110566 894 1 "Zambia"                             345.1475   .0008129414303539306
    2001 111266  24 1 "Angola"                             129.0375     .01661371345801399
    1999 111267  24 1 "Angola"                              82.3025    .001424574752582508
    2000 111267  24 1 "Angola"                              79.6325    .001424574752582508
    2001 111267  24 1 "Angola"                             155.0725    .001424574752582508
    1999 111269  24 1 "Angola"                               233.18 5.9604641222676946e-08
    2001 111269  24 1 "Angola"                               279.16 5.9604641222676946e-08
    1998 111270  24 1 "Angola"                             256.3375                      0
    1999 111270  24 1 "Angola"                               275.49                      0
    2000 111270  24 1 "Angola"                             242.4675                      0
    2000 111271  24 1 "Angola"                             210.6025                      0
    2001 111271  24 1 "Angola"                                247.3                      0
    2002 111271  24 1 "Angola"                               229.07                      0
    1997 111272  24 1 "Angola"                               328.63                      0
    1998 111272  24 1 "Angola"                             225.6275                      0
    1999 111272  24 1 "Angola"                              250.075                      0
    2000 111272  24 1 "Angola"                             198.0175                      0
    1997 111273  24 1 "Angola"                             335.6975                      0
    1998 111273  24 1 "Angola"                             233.6925                      0
    1999 111273  24 1 "Angola"                                257.9                      0
    2000 111273  24 1 "Angola"                             203.0175                      0
    2000 111276  24 1 "Angola"                               249.02    .021412115738485227
    2001 111276  24 1 "Angola"                             271.8125    .021412115738485227
    2001 111286  24 1 "Angola"                              273.635                      0
    2007 111299 180 1 "Congo (Democratic Republic of the)" 338.2825     .19334044121205807
    1999 111988  24 1 "Angola"                              98.7425                      0
    1998 111990  24 1 "Angola"                              251.035                      0
    1998 111992  24 1 "Angola"                             224.0875                      0
    2001 111992  24 1 "Angola"                             228.4075                      0
    2011 111992  24 1 "Angola"                             361.5275                      0
    2001 111993  24 1 "Angola"                              248.915                      0
    1997 111994  24 1 "Angola"                               293.21    .018505860297523213
    1998 111994  24 1 "Angola"                               229.16    .018505860297523213
    1999 111994  24 1 "Angola"                               246.74    .018505860297523213
    2000 111994  24 1 "Angola"                                194.7    .018505860297523213
    2001 111994  24 1 "Angola"                             227.8375    .018505860297523213
    1998 111995  24 1 "Angola"                              239.685    .021937269366844703
    1999 111995  24 1 "Angola"                              252.105    .021937269366844703
    2000 111995  24 1 "Angola"                               209.51    .021937269366844703
    2000 111996  24 1 "Angola"                             256.0475    .021369504562926522
    1997 112006  24 1 "Angola"                               306.02                      0
    1997 112016 180 1 "Congo (Democratic Republic of the)" 341.5425      .1657853932119906
    1999 112709  24 1 "Angola"                             219.6875                      0
    1999 112711  24 1 "Angola"                             260.3175  5.960463766996327e-08
    2000 112711  24 1 "Angola"                             207.0275  5.960463766996327e-08
    1999 112712  24 1 "Angola"                              251.065                      0
    1999 112714  24 1 "Angola"                             262.5775     .01246941094120757
    2000 112715  24 1 "Angola"                             213.3225    .024924696709604177
    1999 112720  24 1 "Angola"                             301.0825                      0
    2001 112720  24 1 "Angola"                               327.61                      0
    1998 112722  24 1 "Angola"                              354.765   .0002822570781074174
    1998 112726  24 1 "Angola"                              318.865                      0
    1999 112729 894 1 "Zambia"                             305.5575    .011939670861465856
    1997 112735 180 1 "Zambia"                              333.945     .02719444310059771
    1998 112735 180 1 "Zambia"                              365.865     .02719444310059771
    end

    I would like to run the regression where my dependent variable is "dep_var" which change by gid (it is a code from a grid cell 50x50km) and year. I want to regress dep_var on the interaction term between prec_gpcc (which change by gid and year) and Grid_fix (which change only by gid)

    I think I have to control by year fixed effect and grid cell fixed effect. Hence, I am using command reg2hdfe with the following specification:

    Code:
    reg2hdfe dep_var c.prec_gpcc##c.Grid_Fix, id1(year) id2(gid)
    However, it gives me the following error: "factor variables and time-series operators not allowed" I am not sure what I can do and If I am doing well. If you can help me, I appreciate it.

    Best,

    Diego.
    Last edited by Diego Malo; 22 Jul 2021, 15:29.

  • #2
    Consider reghdfe from SSC instead.

    Code:
    ssc install reghdfe, replace
    ssc install ftools, replace
    help reghdfe

    Comment


    • #3
      Thank you Andrew Musau . What is exactly the difference between reghdfe and reg2hdfe? I am not see it clear. When I use reghdfe it works but not when I use reg2hdfe and I do not understand why this happens.

      Am I doing well adding year and gid cell fixed effect in this way or should I add some country fixed effect?

      Should I consider the interaction like this c.prec_gpcc##c.Grid_Fix ?

      Last edited by Diego Malo; 22 Jul 2021, 15:35.

      Comment


      • #4
        reg2hdfe (Stata Journal) was written in 2012, and it appears that it does not support factor variables. reghdfe is more modern and versatile. You include the fixed effects within -absorb()-, see examples in

        Code:
        help reghdfe
        Otherwise, without factor variables:

        reg2hdfe dep_var c.prec_gpcc##c.Grid_Fix, id1(year) id2(gid)
        is

        Code:
        gen interaction =prec_gpcc#c.Grid_Fix
        reg2hdfe dep_var prec_gpcc Grid_Fix interaction, id1(year) id2(gid)

        If you have more than 30 gids, cluster your standard errors using -cluster(gid)-
        Last edited by Andrew Musau; 22 Jul 2021, 15:53.

        Comment


        • #5
          Thank you so much Andrew Musau . It helps me a lot!

          I have two more doubts. To generate the interaction, shouldn't I put c. in the variable prec_gpcc as well? I am a bit confuse when to put c. or not. I thought that I have to use c. to interact a continuos variable with a factor variable, prefixing the continuos variable with c. However, I think that in my case, both, prec_gpcc and grid_fix are continuos variables. With grid_fix, I am not sure in fact.

          When I add cluster(gid), Am I taking into account both the cross-sectional spatial correlation and the location-specific serial correlation? I am thinking in using reg2hdfespatial, developed by Fetzer in 2014. Something like that:


          Code:
          reg2hdfespatial dep_var prec_gpcc Grid_Fix interaction , timevar(year) panelvar(gid) lat(latitude) lon(longitude) distcutoff(500) lagcutoff(100000)

          Thank you again,

          Best,

          Diego.

          Comment


          • #6
            I have two more doubts. To generate the interaction, shouldn't I put c. in the variable prec_gpcc as well?
            Yes, I accidentally deleted the c. prefix. Alternatively, multiply the variables directly

            Code:
            gen interaction =prec_gpcc*Grid_Fix

            When I add cluster(gid), Am I taking into account both the cross-sectional spatial correlation and the location-specific serial correlation? I am thinking in using reg2hdfespatial, developed by Fetzer in 2014.
            With \(N>>T\), cluster robust standard errors are robust to the presence of heteroscedasticity and arbitrary forms of serial correlation. Whether spatial dependence is a problem in your dataset and whether you should address it depends on what is done in the literature. You may, of course, compare the results of both estimators and see if the estimates differ.

            Comment


            • #7
              Thank you Andrew Musau ! It helps me a lot!

              Comment

              Working...
              X