Announcement

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

  • Error to get the marginal effect of a variable to do an event study

    I want to run an event study where the effect of my variable AGOA_shock (continous) varies by the value of another continuous variable (LD_Desmet). I want to plot separate lines for the estimates effect of AGOA_shock at the percentil r10 and r90 of variable LD_Desmet

    I am trying to use lincom command to compute those marginal effect for each event time with the goal to create an event study with two lines, ones for my r10 and another for my r90.

    This is the command I am doing;

    (agoa_year indicates the year my units are treated)

    Code:
    * Create Event_time variable
    gen event_time = year - agoa_year if AGOA_shock > 0.01
    replace event_time = . if AGOA_shock == 0
    
    * Trim the extremes and make ir categorical for plotting
    gen event_time_cat = event_time
    replace event_time_cat = -5 if event_time_cat < -5
    replace event_time_cat = 5 if event_time_cat > 5
    
    label define etime -5 "-5 or earlier" -4 "-4" -3 "-3" -2 "-2" -1 "-1" 0 "0 (treat year)" ///
                      1 "1" 2 "2" 3 "3" 4 "4" 5 "5 or later"
    label values event_time_cat etime
    
    * Get percentiles of iLD_Desmet
    sum LD_Desmet, detail
    local p10 = r(p10)
    local p90 = r(p90)
    
    * Regression
    reghdfe outcome i.event_time_cat##c.AGOA_shock##c.LD_Desmet, absorb(i.ccode#i.year) cluster(UID)
    
    * Get marginal effects at p10 and p90 for each event year
    
    // Create storage variables
    foreach p in p10 p90 {
        gen BETA_`p' = .
        gen LB_`p' = .
        gen UB_`p' = .
    }
    
    gen _year = .
    local i = 1
    
    foreach k in -5 -4 -3 -2 -1 0 1 2 3 4 5 {
        replace _year = `k' in `i'
        local i = `i' + 1
    
        foreach p in p10 p90 {
            local val = ``p''
    
            lincom _b[`k'.event_time_cat#c.AGOA_shock] + _b[`k'.event_time_cat#c.AGOA_shock#c.LD_Desmet]*`val'
    
            replace BETA_`p' = r(estimate) if _year == `k'
            replace LB_`p'   = r(lb) if _year == `k'
            replace UB_`p'   = r(ub) if _year == `k'
        }
    }
    
    
    colorpalette viridis, n(3) nograph reverse
    local style1 lcolor("`r(p2)'") mc("`r(p2)'") ms(Oh) lwidth(thick) msize(medlarge)
    local style2 lcolor("`r(p3)'") mc("`r(p3)'") ms(Sh) lwidth(thick) msize(medlarge)
    
    twoway ///
        rarea  LB_p10 UB_p10 _year, color(gs11%50) ///
        ||  connected BETA_p10 _year, `style1' ///
        ||     rarea  LB_p90 UB_p90 _year, color(gs12%50) ///
        ||  connected BETA_p90 _year, `style2' lstyle(solid) lc("`r(p3)'") ///
        ytitle("Effect of AGOA_Shock on Outcome") ///
        xline(0, lcolor(red) lpattern(dash)) ///
        yline(0, lcolor(black) lpattern(solid)) ///
        xlabel(-5(1)5) xtitle("Years since treatment") ylabel(, format(%03.1f)) ///
        legend(order(2 "10{superscript:th} percentile" 4 "90{superscript:th} percentile" 1 "95% CI") pos(6) rows(1))
    However, in my in my loop with lincom it gives an error saying 'invalid sintax', which I do not understand. Any help?


    Here an example of my database:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float ccode double(UID year) float lfatalities0_UCDP double LD_Desmet int agoa_year float AGOA_shock
    6 11054 1995         0  .03122459246126068 2004        0
    6 10781 2006         0 .046738434006659994 2004 .8965186
    6 10656 2010         0 .054640854143657575 2004 .8443321
    6 10765 2015         0  .03621029833960686 2004 .9557872
    6 10957 1996         0 .054640652864372516 2004        0
    6 10765 2017         0  .03621029833960686 2004 .9557872
    6 10766 1991         0  .03122459246126069 2004        0
    6 10766 1992         0  .03122459246126069 2004        0
    6 10870 2005         0  .03535951558410838 2004  .898278
    6 10766 2002         0  .03122459246126069 2004        0
    6 10766 2004         0  .03122459246126069 2004 .9269122
    6 10766 2007         0  .03122459246126069 2004 .9269122
    6 10648 1996         0 .039586657895826154 2004        0
    6 11029 1999         0  .03589644691322339 2004        0
    6 10841 2016         0 .030510913346212486 2004        0
    6 11063 2008         0 .029688981056183946 2004 .9681068
    6 10767 1993         0  .03535951558410838 2004        0
    6 10767 1996         0  .03535951558410838 2004        0
    6 11057 2006         0  .04552176496748251 2004        0
    6 11025 2017         0  .05189877093765067 2004 .9278287
    6 10689 1991         0  .04910504807254094 2004        0
    6 10767 2005         0  .03535951558410838 2004 .8837473
    6 10767 2010         0  .03535951558410838 2004 .8837473
    6 10919 2015         0  .02968898917584467 2004 .9522026
    6 10767 2012         0  .03535951558410838 2004 .8837473
    6 11039 2005         0  .03114714433891032 2004 .9539912
    6 10752 2017         0  .03884992747547268 2004 .9007437
    6 10768 1994         0 .039943438577893134 2004        0
    6 10768 1995         0 .039943438577893134 2004        0
    6 10768 2000         0 .039943438577893134 2004        0
    6 11046 2000         0  .03550619714534043 2004        0
    6 10978 1991         0 .054640528277877305 2004        0
    6 10952 1997         0  .12436312964624884 2004        0
    6 10768 2011         0 .039943438577893134 2004 .8781926
    6 10769 1989         0 .036050519315794595 2004        0
    6 10722 2010         0  .03016660029687864 2004 .9620417
    6 10656 1989         0 .054640854143657575 2004        0
    6 10919 2003         0  .02968898917584467 2004        0
    6 10769 2004         0 .036050519315794595 2004        0
    6 10769 2007         0 .036050519315794595 2004        0
    6 10769 2011         0 .036050519315794595 2004        0
    6 11002 1990         0 .024926625510840333 2004        0
    6 10653 1992         0 .036426614925238625 2004        0
    6 10770 1989         0 .031224592461260695 2004        0
    6 10907 2007         0  .04552174682757335 2004 .9175014
    6 10770 1990         0 .031224592461260695 2004        0
    6 10770 1991         0 .031224592461260695 2004        0
    6 10848 1996         0 .029904704668571678 2004        0
    6 10770 1997         0 .031224592461260695 2004        0
    6 10770 1998         0 .031224592461260695 2004        0
    6 10770 2003         0 .031224592461260695 2004        0
    6 10836 1993         0   .0546287245726871 2004        0
    6 10659 1995         0 .040777561009889725 2004        0
    6 10839 2008         0  .04235204486331569 2004  .801679
    6 10770 2010         0 .031224592461260695 2004  .970064
    6 10981 2013         0 .024926625510840322 2004 .9603535
    6 10821 1998 1.0986123 .024926625510840326 2004        0
    6 10710 1999         0  .03958678795331469 2004        0
    6 10771 1995         0  .04855532902527665 2004        0
    6 10771 2005         0  .04855532902527665 2004        0
    6 10771 2007         0  .04855532902527665 2004        0
    6 10661 2016         0   .0312245924612607 2004 .9688161
    6 10687 2017         0  .05189872516444452 2004 .8533979
    6 10914 1990         0  .04911224387717576 2004        0
    6 10771 2011         0  .04855532902527665 2004        0
    6 10684 2003         0  .05075844521415258 2004        0
    6 10772 1993         0  .05189880762660367 2004        0
    6 10772 1996         0  .05189880762660367 2004        0
    6 10997 1997         0  .05230786529905664 2004        0
    6 10772 1990         0  .05189880762660367 2004        0
    6 10772 2005         0  .05189880762660367 2004 .8449366
    6 10772 2011         0  .05189880762660367 2004 .8449366
    6 10772 2012         0  .05189880762660367 2004 .8449366
    6 11064 2007         0  .05189873934130504 2004 .8650948
    6 10772 2016         0  .05189880762660367 2004 .8449366
    6 10774 1989         0   .0516996194532162 2004        0
    6 11009 1993         0  .03826129738640382 2004        0
    6 11091 2003         0 .051093136867124675 2004        0
    6 10765 1998  3.135494  .03621029833960686 2004        0
    6 10774 1997         0   .0516996194532162 2004        0
    6 10774 2000         0   .0516996194532162 2004        0
    6 10774 2001         0   .0516996194532162 2004        0
    6 10893 2014         0 .054640528277877305 2004        0
    6 10774 2002         0   .0516996194532162 2004        0
    6 10777 1991         0  .03642661492523862 2004        0
    6 10755 1994         0 .038849937657266735 2004        0
    6 10961 1992         0 .040777522012711474 2004        0
    6 10774 2010         0   .0516996194532162 2004        0
    6 10774 2012         0   .0516996194532162 2004        0
    6 10977 2016         0 .024926625510840333 2004 .9678628
    6 10875 1998         0  .03313812485550841 2004        0
    6 11090 2010         0   .0388339011493831 2004 .8892661
    6 10637 1999         0  .05189878716190642 2004        0
    6 10775 1994         0   .0357153003620774 2004        0
    6 10651 1997         0  .04546213423805111 2004        0
    6 10619 2007         0 .031224592461260685 2004        0
    6 10664 2001         0 .035581931142228566 2004        0
    6 10775 2014         0   .0357153003620774 2004 .8700608
    6 10776 1995         0   .0518443562937012 2004        0
    6 10990 2011         0 .031224592461260692 2004 .9562348
    end

  • #2
    Originally posted by Diego Malo View Post

    foreach k in -5 -4 -3 -2 -1 0 1 2 3 4 5 {
    replace _year = `k' in `i'
    local i = `i' + 1

    foreach p in p10 p90 {
    local val = ``p''

    lincom _b[`k'.event_time_cat#c.AGOA_shock] + _b[`k'.event_time_cat#c.AGOA_shock#c.LD_Desmet]*`val'
    I haven't run your code, but one thing that stands out is that you're referencing negative numbers for factor variables. Stata's factor variables cannot contain negative values. You should add:

    Code:
    set trace on
    to see where the code fails, in case this doesn't resolve your issue.
    Last edited by Andrew Musau; 24 Apr 2025, 04:46.

    Comment

    Working...
    X