Search code examples
statatrendline

How to get Y value at a given X value of a trendline


I have created a trend-line using the lpoly command (local polynomial smoothed trendline).

I want to find what the y value of that trend-line is at any given x value.

How can I do this?


Solution

  • One can do this using the generate() option of the lpoly command:

    webuse motorcycle, clear
    lpoly accel time, generate(x y)
    

    enter image description here

    The values are stored in the y and x variables (here showing the first 10 observations):

    list y x in 1/10
    
         +------------------------+
         |          y           x |
         |------------------------|
      1. | -1.6245329   2.4000001 |
      2. |  -1.775922   3.5265307 |
      3. | -1.9832878   4.6530613 |
      4. | -2.2217888   5.7795918 |
      5. | -2.3814197   6.9061224 |
         |------------------------|
      6. | -2.5199665    8.032653 |
      7. | -3.3919962   9.1591836 |
      8. | -8.8572222   10.285714 |
      9. | -16.957709   11.412245 |
     10. | -26.693355   12.538775 |
         +------------------------+
    

    If these two variables are then plotted, it can be seen that this is indeed the case:

    twoway line y x
    

    enter image description here