Announcement

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

  • Calculate intersection of non-parametric smoothing functions with line graph

    Dear all,

    I have plotted a scatterplot and run two non-parametric smoothing functions over the respective data. The graphs work just fine for me, the question is the following: How can I calculate the intersection of the smoothed functions with the 45-degree-line? Basically, this reduces to the question, where the x- and y-value of the function are exactly the same. You can find my code and the resulting graph below.

    Thank you very much in advance

    * Generate first graph
    twoway scatter income_abi_ppp income_abi_ppp_t_2 , msize(tiny) mcolor(blue) ///
    ||scatteri 25 25 -5 -5, connect(l) msymbol(i) lpattern(dash) lcolor(black) ///
    ||lpoly income_abi_ppp income_abi_ppp_t_2, lpattern(solid) lwidth(medthick) lcolor(black) ///
    legend(off) ///
    xscale(range(-5 25)) xlabel(-5[5]25) yscale(range(-5 25)) ylabel(-5[5]25) ///
    ytitle("Daily p.c. ABI in 2005PPP US$, 2016") ///
    title("Kernel, Entire Sample") ///
    saving(Figure_4_poly_all)

    * Generate secondgraph
    twoway scatter income_abi_ppp income_abi_ppp_t_2 , msize(tiny) mcolor(blue) ///
    || scatteri 25 25 -5 -5, connect(l) msymbol(i) lpattern(dash) lcolor(black) ///
    || lowess income_abi_ppp income_abi_ppp_t_2, lpattern(solid) lwidth(medthick) lcolor(black) ///
    legend(off) ///
    xscale(range(-5 25)) xlabel(-5[5]25) yscale(range(-5 25)) ylabel(-5[5]25) ///
    ytitle("Daily p.c. ABI in 2005PPP US$, 2016") ///
    xtitle("Daily p.c. ABI in 2005PPP US$, 2010") ///
    title("Lowess, Entire Sample") ///
    saving(Figure_4_lowess_all)

    * Combine the two graphs
    graph combine "Figure_4_poly_all" ///
    "Figure_4_lowess_all", ///
    rows(2) cols(3) ///
    title("Mean Asset Paths") ///
    ycommon xcommon iscale(0.5) ///
    saving(Statalist_Kernel_Lowess_All)
    Click image for larger version

Name:	Statalist_Kernel_Lowess_All.png
Views:	1
Size:	152.4 KB
ID:	1743782



  • #2
    You can use lpoly , generate() to get the smoothed values.

    Comment


    • #3
      Statalist_Fits.png Hello George,

      thank you for the rapid reply. I thoroughly worked through the command you suggest, and found a similar one for lowess. I now use the following code (with renamed variables to make it easier to comprehend,, hopefully):


      lpoly assets_today assets_yesterday if year==2016, gen(grid fitted_poly)

      lowess assets_today assets_yesterday if year==2016, gen(fitted_lowess)


      I can plot these smoothed values in order to visualize what Stata has calculated. Both results are presented graphically below, and for obvious reasons look exactly like the initital plots. My problem now is that there are many smoothed points, but I still do not know how to find the intersection with a 45-degree diagonal line. So what I need is some form of command or calculation that tells me at which point the line running through the smoothed values has exactly the same x- and y-value. Is there any possibility or maybe workaround?

      I am sorry if this seems to be a basic question. I saw basically every researcher in my research topic report the intersection, but of course, noone mentions the basic calculation behind it...

      Kind regards

      Attached Files
      Last edited by Oliver Schulte; 23 Feb 2024, 07:26.

      Comment


      • #4
        I'm sure there's a more clever way to do this, but it works.

        Code:
        clear all
        
        set obs 1000
        
        g x = (rnormal(5,2))
        g y = -20 + 0.5*x^2 + rnormal(3,1)
        g line45 = 0 + 1*x
        
        lowess y x, generate(yfit)
        twoway lowess yfit x || lfit line45 x
        
        g diff = yfit - line45
        egen diffrank = rank(abs(diff))
        summ x y if diffrank==1

        Comment

        Working...
        X