Announcement

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

  • How to add local linear regression line to rdplot graph?

    Hi,

    For my assignment, I am trying to replicate the graph in the paper: What Does Debt Relief Do for Development? Evidence from India's Bailout for Rural Households by Martin Kanz

    I was able to run the regressions but I am having a problem replicating the graph. The commands that I have used:

    Code:
    xtreg  lnagrevpa treated h_1 h_1x  i._IFE* i._MFE* if included & farming==1, robust fe i(bankdistrictid)
    
    predict xi
    
    rdplot lnagrevpa hfc if included & farming==1, c(0) p(2) genvars(rdplot_hat_lnagrevpa)
    The graph that I aim to get:

    Click image for larger version

Name:	Example.png
Views:	1
Size:	85.6 KB
ID:	1653458


    The graph that I am getting:




    Last edited by Jose Williams; 08 Mar 2022, 05:14.

  • #2
    It does not appear that -rdplot- saves the data for the polynomial fit ( part of the rdrobust package by Calonico, S., M. D. Cattaneo, M. H. Farrell, and R. Titiunik.) One can install the latest version:
    Code:
     net install rdrobust, from(https://raw.githubusercontent.com/rdpackages/rdrobust/master/stata)
    It is possible to extract the data from a graph using sersets. This example uses Roger Newson's -xframeappend-

    Code:
    clear*
    webuse grunfeld
    keep if company == 1
    qui reg invest kstock if kstock <500
    local b0_l = e(b)[1,2]
    local b1_l = e(b)[1,1]
    
    qui reg invest kstock if kstock >=500
    local b0_r = e(b)[1,2]
    local b1_r = e(b)[1,1]
    
    rdplot invest kstock if company == 1, c(500) p(2) graph_options(legend(pos(6)))
    graph describe
    
    serset dir
    serset set 0
    serset use ,clear
    gen id = _n
    frame put rd* id,into(rdplot)
    
    serset set 1
    serset use,clear
    gen id = _n
    rename (y_plot_l x_plot_l) (y x)
    frame put x y id, into(left)
    
    serset set 2
    serset use,clear
    gen id = _n
    rename (y_plot_r x_plot_r) (y x)
    frame put y x id, into(right)
    frame change left
    xframeappend right , gen(c)
    
    frlink m:1 id  , frame(rdplot id )
    frget *, from(rdplot) gen lfit_left = `b0_l' + `b1_l'*x if c == 0
    gen lfit_right = `b0_r' + `b1_r'* x if c == 1
    
    line y x if c == 1, lc(black) lw(thin)  /// 
        || line y x if c == 0, lc(black) lw(thin) /// 
        || scatter rdplot_mean_y rdplot_mean_bin, sort msize(small) mcolor(gs10) ///
        || line lfit_left x, lw(thin) lc(black) lp(dash) /// 
        || line lfit_right x , lw(thin) lc(black) lp(dash) /// 
        ||, name(gr1,replace) xline(500, lw(medthin) lc(black) lp(dash)) /// 
        legend(pos(6) row(1) order(3 "Sample average within bin" 1 "Polynomial fit of order 2" /// 
        5 "Local Linear Regression"))
    Click image for larger version

Name:	gr1.png
Views:	1
Size:	37.9 KB
ID:	1653517

    Comment


    • #3
      Scott Merryman Thank you for your guidance. As I am trying to replicate your code, I am getting stuck at the following command:

      Code:
      frget *, from(rdplot) gen lfit_left = `b0_l' + `b1_l'*x if c == 0
      gen lfit_right = `b0_r' + `b1_r'* x if c == 1
      It tells me that the option gen is not allowed.

      Comment


      • #4
        Jose Williams The lines should be:

        Code:
        frget *, from(rdplot)
        gen lfit_left = `b0_l' + `b1_l'*x if c == 0
        gen lfit_right = `b0_r' + `b1_r'* x if c == 1

        Comment

        Working...
        X