Announcement

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

  • Margins Help

    Dear Statalisters ,


    I have been struggling trying to generate margins for a panel and was looking for your assistant on the following:


    My data are for a panel for a large period of years and with the solid variables, dummies and some custom indicators , which you can see in the sample provided. In my sample each id bellows to a group (A, B and C), and time can also be divided in decades and in two large subperiods(in the provided data 1990-2000 and 2001-2014) . For obvious reason, I can provide only a small sample of my data and reduce the period length

    I am able to generate margins figures at full sample level. For the gdp and the indicator
    I used the following code for a quadratic function

    qui xtregar D.gdp cpi u Output dummy1 dummy2 c.indicator1##c.indicator1, fe

    // get the x where the minimum occurs
    local x = -_b[indicator1]/(2*_b[indicator1#indicator1])


    // get the y for that minimim
    qui margins, at(indicator1 = `x' cpi=0 u=2 Output= 7.54e+16 dummy1=0 dummy2=0 indicator1=-4)
    // the predictions are stored in the matrix r(b)
    // el(r(b),1,1) extracts the cell 1,1 from the matrix r(b)
    local y = el(r(b),1,1)


    // to display those values we don't want all the decimal places
    // : display allows you to control how a number is displayed
    // it adds some spaces before the number, which strtrim() removes
    local ylab : display %9.3fc `y'
    local ylab = strtrim("`ylab'")
    local xlab : display %9.3fc `x'
    local xlab = strtrim("`xlab'")


    // prepare for our plot
    nlcom -_b[indicator1]/(2*_b[indicator1#indicator1])
    qui margins, at(indicator1=(-16(4)29) cpi=0 u=2 Output= 7.54e+16 dummy1=0 dummy2=0 indicator1=-4)

    // with scatteri we can add a point and a label to our graph
    marginsplot, noci plotopts(msymbol(i)) legend(off) ///
    addplot(scatteri `y' `x' (12) "(`xlab'; `ylab')") ///
    ylab(,format(%9.3fc) angle(0)) ytitle("predicted gdp ") title("Predicted Marings pair")

    What I would need now are the followings:
    1. I need to store the values found in the figure in my real data. That will allow me then to create, two new variables, as the difference between my margin value and their actual weighed sum , for each one of the variables, In the example my variables are the indicator1 and gdp.
    2. I then must repeat the procures estimating those margins values, store them again in my real data for each subperiod and each decade as a whole, and then by each group separately, Respectively, I need to generate the difference values for the each subperiod and for each decade as a whole, and then by each group separately.
    3. Same procedure as above needs to be done for the other variables, in the sample. I guess a loop here is needed.
    4. Finally, once that is done, I need to collapse everything by the sum of the weighted average.
    @Maarten Buis helped a lot in a previous related post, I would be grateful for any help you can provide.

    Best regards,

    Giorgio Di Stefano

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(id ts) str1 group str97 country float dummy1 byte dummy2 float(indicator1 indicator2) long gdp float(cpi u) double Output
    1 1990 "A" "Australia"      1 1  10.999176  10.999176   571564  7.333022  6.93 1.28877e+16
    1 1991 "A" "Australia"      0 1       12.2       12.2   565844  3.176675  9.58  1.3296e+15
    1 1992 "A" "Australia"      0 1       12.2       12.2   580278 1.0122311 10.73 1.22544e+16
    1 1993 "A" "Australia"      1 1  17.553352  17.553352   602944 1.7536534 10.87 1.18076e+16
    1 1994 "A" "Australia"      0 1      19.11      19.11   632477 1.9696348  9.72 1.34421e+16
    1 2000 "A" "Australia"      0 1     28.634     28.634   798334  4.457435  6.28 1.39202e+16
    1 2001 "A" "Australia"      1 1   26.41307   26.54496   819144 4.4071355  6.74 1.30234e+16
    1 2002 "A" "Australia"      0 1   5.536341      6.908   853048 2.9815745  6.37 1.61436e+16
    1 2003 "A" "Australia"      0 1   5.536341      6.908   876715  2.732596  5.93 2.02622e+16
    1 2004 "A" "Australia"      1 1   7.350179   8.569935   912927 2.3432553  5.39 2.35621e+16
    1 2010 "A" "Australia"      1 1   6.241802   6.241802  1080050   2.91834  5.21 3.47466e+16
    1 2011 "A" "Australia"      0 0      5.068      5.068  1110726   3.30385  5.08  4.0027e+15
    1 2012 "A" "Australia"      0 0      5.068      5.068  1152963 1.7627802  5.22 4.13369e+16
    1 2013 "A" "Australia"      1 1   7.929047   8.248285  1177554  2.449889  5.66 3.74135e+16
    1 2014 "A" "Australia"      0 .  15.081665     16.199  1207855  2.487923  6.08 3.47753e+16
    2 1990 "A" "France"         0 0        1.6        1.6  2081911 3.1942835  9.36 5.03813e+16
    2 1991 "A" "France"         0 0        1.6        1.6  2103733  3.213407  9.13 4.93537e+16
    2 1992 "A" "France"         0 1        1.6        1.6  2137379 2.3637605 10.21  5.2038e+15
    2 1993 "A" "France"         1 1   6.996447  -.8161401  2123942 2.1044629 11.32 5.80229e+16
    2 1994 "A" "France"         0 0    8.69136     -1.575  2174032 1.6555153 12.59   5.071e+14
    2 2000 "A" "France"         0 0   2.364474      2.797  2564959   1.67596 10.22 7.85938e+16
    2 2001 "A" "France"         0 0   2.364474      2.797  2615840 1.6347808  8.61 7.85391e+16
    2 2002 "A" "France"         1 1  -.7668521  -.7921649  2645544 1.9234123   8.7 8.75676e+16
    2 2003 "A" "France"         0 0 -2.9592714     -3.286  2667321  2.098472  8.31 1.03639e+17
    2 2004 "A" "France"         0 0  -2.950783     -3.286  2742800 2.1420896  8.91  1.0654e+16
    2 2010 "A" "France"         0 0      3.891      3.891  2904699 1.5311227  8.87 9.63052e+16
    2 2011 "A" "France"         0 0      3.891      3.891  2968390  2.111598  8.81 1.08307e+17
    2 2012 "A" "France"         1 1 -2.5410414 -2.1623123  2977685 1.9541953   9.4 9.84001e+16
    2 2013 "A" "France"         0 0  -6.333579     -5.608  2994846  .8637155  9.92 9.87864e+16
    2 2014 "A" "France"         0 0  -6.108374     -5.608  3023483  .5077588 10.29 1.04153e+17
    3 1990 "B" "Germany"        0 1   4.255927      4.029  3090684 2.6964715  4.89           .
    3 1991 "B" "Germany"        0 1   .5641079 -1.6926868  3245558 4.0470366  5.32 1.21946e+17
    3 1992 "B" "Germany"        0 1   .3835026     -1.973  3307973  5.056979  6.32 1.29671e+17
    3 1993 "B" "Germany"        0 1   .3835026     -1.973  3275659  4.474575  7.68 1.14872e+17
    3 1994 "B" "Germany"        1 1   .8169867 -1.2526814  3354009  2.693057  8.73 1.21188e+17
    3 2000 "B" "Germany"        0 1  1.9049623      2.806  3738235  1.440268  7.92 1.17777e+17
    3 2001 "B" "Germany"        0 1  1.9049622      2.806  3801092  1.983857  7.77 1.17577e+17
    3 2002 "B" "Germany"        1 1   1.283737  2.0394616  3793567 1.4208056  8.48 1.22359e+17
    3 2003 "B" "Germany"        0 1 -1.3254085      -1.18  3767008 1.0342277  9.78 1.50381e+17
    3 2004 "B" "Germany"        0 1 -1.3254085      -1.18  3811273 1.6657335 10.73 1.75373e+17
    3 2010 "B" "Germany"        0 1  1.1606808       -.05  4071113 1.1038091  6.97  2.0819e+16
    3 2011 "B" "Germany"        0 1  1.1606808       -.05  4230912 2.0751746  5.82 2.44939e+17
    3 2012 "B" "Germany"        0 1  1.1606808       -.05  4248618  2.008491  5.38 2.25506e+17
    3 2013 "B" "Germany"        1 1  1.1165322      .0625  4267210  1.504721  5.23 2.32201e+17
    3 2014 "B" "Germany"        0 .  .01281774      2.875  4361496  .9067979  4.98 2.37452e+17
    4 1990 "B" "Italy"          0 0   3.829138       3.66  2199474  6.456609  9.79 4.78032e+16
    4 1991 "B" "Italy"          0 1   3.777037       3.66  2233312      6.25  10.1 4.77836e+16
    4 1992 "B" "Italy"          1 1   2.615249  2.9572604  2251944   5.27059  9.33 5.49646e+16
    4 1993 "B" "Italy"          0 1  1.4437795   .7736538  2232739 4.6267347 10.24 4.39227e+16
    4 1994 "B" "Italy"          1 1  4.0116615  13.063806  2280766  4.051842 11.09 5.03757e+16
    4 2000 "B" "Italy"          0 1    5.97681   1.812674  2598506 2.5376854 10.84 7.22295e+16
    4 2001 "B" "Italy"          1 1   7.402936  4.7593465  2649212  2.785165   9.6 7.13962e+16
    4 2002 "B" "Italy"          0 0      8.534      8.534  2655940  2.465323  9.21 7.52609e+16
    4 2003 "B" "Italy"          0 0      8.534      8.534  2659622 2.6725554  8.87 8.79973e+16
    4 2004 "B" "Italy"          0 0      8.534      8.534  2697484 2.2067366  7.87 1.03212e+17
    4 2010 "B" "Italy"          0 0     -16.25          0  2680599  1.525516  8.36  1.1136e+16
    4 2011 "B" "Italy"          0 0 -14.330358          0  2699559  2.780633  8.36 1.24328e+17
    4 2012 "B" "Italy"          0 1          0          0  2619088  3.041363 10.65 1.11628e+17
    4 2013 "B" "Italy"          1 1   .6806767 -1.9956785  2570869 1.2199935 12.15 1.13354e+17
    4 2014 "B" "Italy"          0 0 -1.9673892     -2.941  2570752 .24104743 12.68 1.12803e+17
    5 1990 "C" "United Kingdom" 0 1     16.809     16.809  1846210  8.063461  6.97 5.76584e+16
    5 1991 "C" "United Kingdom" 0 1     16.809     16.809  1825844  7.461783  8.55 5.51486e+16
    5 1992 "C" "United Kingdom" 1 1  13.403038  13.403038  1833167 4.5915494  9.78 5.94441e+16
    5 1993 "C" "United Kingdom" 0 1       12.1       12.1  1878822  2.558578 10.35 5.35544e+16
    5 1994 "C" "United Kingdom" 0 1       12.1       12.1  1951082 2.2190125  9.65 5.90669e+16
    5 2000 "C" "United Kingdom" 0 1      1.806      1.806  2386524 1.1829562  5.56 6.64745e+16
    5 2001 "C" "United Kingdom" 1 1   1.958802   1.958802  2451683 1.5323496   4.7 6.12017e+16
    5 2002 "C" "United Kingdom" 0 1      2.076      2.076  2505100 1.5204024  5.04 6.22673e+16
    5 2003 "C" "United Kingdom" 0 1      2.076      2.076  2588317 1.3765004  4.81 6.70562e+16
    5 2004 "C" "United Kingdom" 0 1      2.076      2.076  2647493 1.3903975  4.59 7.82735e+16
    5 2010 "C" "United Kingdom" 1 1   3.564247   3.697813  2796536  2.492655  7.79 6.99631e+16
    5 2011 "C" "United Kingdom" 0 .   5.454876   5.670001  2832212 3.8561125  8.04 7.67414e+16
    5 2012 "C" "United Kingdom" 0 .   5.454876       5.67  2872722  2.573235  7.88 7.59697e+16
    5 2013 "C" "United Kingdom" 0 .   5.454876   5.670001  2935525 2.2916667  7.52 7.64401e+16
    5 2014 "C" "United Kingdom" 0 .   5.454876   5.670001  3019561   1.45112  6.11 7.94942e+16
    6 1990 "C" "United States"  1 1       14.9       14.9 10650444  5.397956   5.6           .
    6 1991 "C" "United States"  0 1       14.9       14.9 10638913  4.234964   6.8           .
    6 1992 "C" "United States"  1 1       14.9       14.9 11013662 3.0288196   7.5           .
    6 1993 "C" "United States"  0 1   .0375137   .0375137 11316734  2.951657   6.9           .
    6 1994 "C" "United States"  1 1      -.781      -.781 11772662  2.607442  6.12           .
    6 2000 "C" "United States"  1 1      1.463      1.463 14931055  3.376857  3.99           .
    6 2001 "C" "United States"  0 1   5.542341   5.542341 15073548  2.826171  4.73           .
    6 2002 "C" "United States"  1 1      5.767      5.767 15329187 1.5860317  5.78           .
    6 2003 "C" "United States"  0 1      5.767      5.767 15757822 2.2700949  5.99           .
    6 2004 "C" "United States"  1 1      5.767      5.767 16364901  2.677237  5.53           .
    6 2010 "C" "United States"  1 1       -.36       -.36 17784695 1.6400435  9.63           .
    6 2011 "C" "United States"  0 1       -.36       -.36 18060339 3.1568415  8.95           .
    6 2012 "C" "United States"  1 1       -.36       -.36 18472238 2.0693374  8.07           .
    6 2013 "C" "United States"  0 1   1.298654   1.298654 18812474 1.4648327  7.37           .
    6 2014 "C" "United States"  0 1       1.39       1.39 19242861  1.622223  6.17           .
    end
    Last edited by Giorgio Di Stefano; 08 Mar 2022, 16:05.
Working...
X