Announcement

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

  • How to Fit Non-Linear Regressions with Scatter Plot

    Dear Statalists,

    I really need help now. Thanks in advance.

    In the below code chunk, I would like to fit a non-linear regression that has 2 variables on a scatter plot. That is to say, I would like to put a scatter plot and a regression plot into the same graph. But, my question is that twoway lfit can only be used with one independent variable when fitting on a scatter plot. But I actually has two independent variables, length and log_length. I also want to add a pweight option in the regression.

    The x axis of the graph in my mind would be length and the y axis would be price, I just do not how to achieve this. The error I got is always "too many variables specified: price length log_length".

    Thank you so much!

    Code:
    sysuse auto.dta, clear
    
    reg price length
    
    gen log_length = log(length)
    
    local idpt_list length log_length
    
    twoway scatter price length || lfit price `idpt_list', pweight=headroom

  • #2
    Code:
    sysuse auto.dta, clear
    gen log_length = log(length)
    reg price length log_length
    predict yhat
    scatter price length || line yhat length, sort
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment

    Working...
    X