Announcement

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

  • Plotting certain coefficients for a fixed-effects regression

    I am trying to plot coefficients for a regression where I have year, region and crop fixed-effects. The regression command looks like this:

    regress yields i.region i.year i.crop c.t1#i.crop c.t2#i.crop c.t8#i.crop c.t9#i.crop c.t10#i.crop c.t11#i.crop c.t12#i.crop c.t13#i.crop c.t14#i.crop c.t15#i.crop c.t17#i.crop c.t18#i.crop c.t19#i.crop c.t20#i.crop c.t21#i.crop

    I end up with a lot of different coefficients but the ones I care about are the temperature interacted with crop dummies. This means that for each temperature bin I have 8 coefficients (for each of the 8 crops I am looking at)

    Here is an example of how part of the regression table looks like (for temperature bin 17)

    crop#c.t17 |
    1 | .2153015 .6866107 0.31 0.754 -1.131614 1.562217
    2 | -.2161207 .7129302 -0.30 0.762 -1.614667 1.182425
    3 | .1795203 .6893462 0.26 0.795 -1.172761 1.531802
    4 | .9881432 .6951312 1.42 0.155 -.3754867 2.351773
    5 | .3005558 .6953362 0.43 0.666 -1.063476 1.664588
    6 | -1.20966 .7663634 -1.58 0.115 -2.713025 .2937053
    7 | .9395245 .6866107 1.37 0.171 -.4073908 2.28644
    8 | .1810834 .7911555 0.23 0.819 -1.370916 1.733083


    Since I have region and year FE's, I also end up with a lot of coefficients that I do not want to plot.
    I tried using coefplot and marginsplot for this but I just do not understand how to separate the coefficients and only have the ones I am interesting in on this graph.

    Thank you so much for your help!

  • #2
    Given that you have to do a lot of picking and choosing among the margins you plot, I think you are best off creating a data set of the coefficients and then working with -graph- directly. The -margins, dydx()- command can recapture the coefficients somewhat selectively for you, and also has an undocumented -saving()- option. So run margins and save the results in a file. Open that file and familiarize yourself with the names of the variables. You may want to give them labels, etc. so that the graphs come out nicely. And then just -graph- whatever subsets of them you like in whatever way you like, just as you would any other data. So something like this:

    Code:
    margins crop, dydx(t??) saving(my_coefficient_file, replace)
    
    use my_coefficient_file, clear
    // GRAPHING COMMANDS HERE
    Last edited by Clyde Schechter; 09 Jul 2016, 09:03.

    Comment


    • #3
      Thank you so much. My only issue is that I am trying to plot for each crop from temperature bin 1 to 21, not for each temperature bin from crop 1 to 8. Is there any way to choose the crop instead of the temperature bin when I graph this?

      I tried this code:

      margins crop(1), dydx(*)
      // GRAPHING COMMANDS

      But it is not working, I am not sure how to choose the crop and not the temperature bin.

      Thanks so much for all of your help!

      Comment


      • #4
        You have 21 distinct temperature bin variables, each of which is continuous, according to what you wrote in #1. So I can't even imagine what the graph you're asking for might look like. If the horizontal axis goes from 1 to 21, what does it represent? The numbers 1 to 21 are not values of any variable in your data: they are indices distinguishing 21 different temperature bin variables, those variables themselves being continuous valued.

        Can you explain more clearly, or show a handworked graph that would resemble what you are seeking, and then explain how you constructed it?

        Or do you perhaps want 21 graphs, one for each temperature bin, each graph showing 8 curves (one for each crop) showing the marginal effect of that crop at selected values of the corresponding temperature bin variable on the horizontal axis? And if so, how would you identify what those values on the horizontal axis would be?


        .
        Last edited by Clyde Schechter; 18 Jul 2016, 17:39.

        Comment


        • #5
          So the idea is that I am looking at the effects of temperature on agricultural yields for 8 different crops. The way my temperature variable is measured is T1 = "the number of days in temperature bin 1". So each coefficient I get represents the effect of one more day in bin "c" for crop "a" compared to the temperature bin I drop. What is interesting is to see how the effects of temperature change depending on which bin we are in for each crop. So if the first crop is corn for example, the graph I am trying to create would show the marginal effects for one more day in each temperature bin for corn only. It is interesting because I find that low temperatures have a small positive effect, then that effect increases with warmer temperatures but becomes largely negative with very hot temperatures.

          Let me know if you need more information, it is tricky because of the way I measure temperature I think.

          Comment


          • #6
            I am still not 100% sure I understand this, but perhaps this is on the right track.

            Code:
            // GENERATE A DEMONSTRATION DATA SET
            set more off
            clear*
            set seed 1234
            set obs 8
            gen byte crop = _n
            expand 100    // ASSUME 100 SPECIMENS PER CROP
            forvalues i = 1/17 {
                gen t`i' = rpoisson(5)
            }
            
            // NOW BUILD AN OUTCOME VARIABLE THAT HAS
            // A MODEL BASED ON BIN # CROP INTERACTION
            matrix mod = J(1, 8, .)
            forvalues i = 1/8 {
                matrix mod[1, `i'] = 0.4*`i'
            }
            matrix coef = J(1, 17, .)
            forvalues i = 1/17 {
                matrix coef[1, `i'] = (`i'*(18-`i'))/50
            }
            gen y = 2
            forvalues i = 1/17 {
                replace y = y + (coef[1,`i'] + mod[1,crop])*t`i'
                replace y = y + rnormal(0, 0.5)
            }
            
            
            //    FIT THE MODEL
            regress y i.crop##c.(t*)
            
            //    CALCULATE MARGINAL EFFECTS AND SAVE RESULTS
            tempfile for_graphs
            margins crop, dydx(t*) saving(`for_graphs')
            
            //    USE THE RESULTS AND GRAPH THEM
            use `for_graphs', clear
            rename _m1 crop
            label var crop "Crop"
            rename _deriv bin
            label var bin "Temperature Bin"
            rename _margin marginal_effect
            label var marginal_effect "Marginal Effect"
            
            graph twoway line marginal_effect bin, sort by(crop)

            Comment


            • #7
              Hi Statalist,
              I think I have the same problem as Celine.
              I am performing a regression using -xtreg, fe-, and would like to plot the coefficient of interaction terms. However, when using the code of Clyde Schechter , I have errors from -margins- since one of my variable is continuous.
              Variables dum_before* design numbers of years before event date
              Variables dum_after* design numbers of years after event date. dum_after0 is the event date.
              And I would like to plot the coefficients of DIV following the time. Can someone help me to clarify this problem?
              Thanks very much!

              Here is my code.
              Code:
              xtreg z div c.div#c.(dum_before*) c.div#(c.dum_after*) ,fe
              I tried to upload DTA file but impossible. Here is the URL for the DTA file.

              Comment


              • #8
                Actually, I think you have an awkward data layout and a misconception about factor-variable notation. Looking at your data set, all of the variables dum_before* and dum_after* are zero-one variables, and they come in a series. So they are not continuous variables. They are discrete. Next, it is always the case that exactly one of them is 1 and the rest are 0. So these are just indicator ("dummy") variables for time periods ranging between 15 periods before and 16 periods after some event. So these variables shouldn't even be in your data set in the first place: you should have a single variable denoting the time from event and then rely on factor variable notation. Unfortunately, the natural time metric in your case runs from 15 to +16, but factor-variables only allow non-negative integers. So we'll work around that by shifting everything up 15 and, to make the output easier to understand, label the values in the natural metric.

                Finally, your regression model is mis-specified because it does not include the un-interacted dum_* variables. So cleaning this all up you get:

                Code:
                clear*
                use statalist_coef
                
                gen time = 0 if dum_after0
                forvalues i = 1/16 {
                    replace time = `i' if dum_after`i'
                }
                forvalues i = 1/15 {
                    replace time = -`i' if dum_before`i'
                }
                drop dum_*
                
                //    NOW SHIFT UP TO GET A NON-NEGATIVE
                //    VARIABLE WE CAN USE FOR FACTOR VARIABLE NOTATION
                
                gen time_shifted = time + 15
                capture label drop time_shifted
                forvalues i = 0/31 {
                    if `i' < 15 {
                        label define time_shifted `i' "`=15-`i'' before", add
                    }
                    else if `i' == 15 {
                        label define time_shifted `i' "event", add
                    }
                    else {
                        label define time_shifted `i' "`=`i'-15' after", add
                    }
                }
                label values time_shifted time_shifted
                
                xtset id quarter
                xtreg z c.div##(i.time_shifted), fe
                Now you are in a position more or less like celine boul was in in #1. There is one difference: she had a discrete variable crop where your corresponding variable is div. So you have to make a choice. You either have to pick some interesting value(s) of div to calculate your time-period effects at, or you have to go for the average marginal effects of time. If you pick interesting values of div, then your values of div correspond to the variable crop, and your values of time_shifted correspond to the variable bin and you can just adapt the code from #6. If you go with the average marginal effects, then just omit everything involving crop in that code.

                Added: Here's the code that would get you all the way to the plot using the average marginal effects.

                Code:
                tempfile marginal_effects
                margins, dydx(i.time_shifted) saving(`marginal_effects') nose
                
                use `marginal_effects', clear
                
                rename _margin marginal_effect
                gen time = _deriv - 15
                
                graph twoway line marginal_effect time, sort
                Last edited by Clyde Schechter; 17 Oct 2018, 15:49.

                Comment


                • #9
                  Thanks Clyde Schechter for your help.
                  I tried to complicate my stuff by creating dum_after* and dum_before*. Your time_shifted variable is very nice.
                  Just to make sure that I understand your code. Here is the code to plot coefficients for my problem.
                  Code:
                  tempfile for_graphs
                  margins, dydx(time_shifted) at((p50) div) saving(`for_graphs')
                  marginsplot, xline(15) // 15 is the event date
                  I have one more problem:
                  1 - How can I drop the value of 15 in the x-axis? I meant, marginsplot for every x-axis value, exept for 15 (the event date).

                  Comment


                  • #10
                    I'm not sure what you mean. When I ran your code, 15 doesn't appear on the x-axis, only 0, 10, 20, and 30. Perhaps you mean you want to exclude the point with time_shifted == 15 from graph? If so, you can't do it in -marginsplot- as far as I know. You have to -use `for_graphs'- and then you can make your graph using -graph twoway- however you like. -marginsplot- allows a lot of customization through twoway options, but it does not allow -if- conditions.

                    Comment


                    • #11
                      Yes. x=15 is the point with time_shifted==15 to indicate the event time. That's why I would like to drop this point from the graph.
                      I will try with -graph twoway-.
                      Many thanks for your help

                      Comment

                      Working...
                      X