Plot fitted line within certain range R

前端 未结 5 2165
执笔经年
执笔经年 2020-12-31 05:26

Using R, I would like to plot a linear relationship between two variables, but I would like the fitted line to be present only within the range of the data.

For exam

5条回答
  •  孤城傲影
    2020-12-31 06:03

    Instead of using abline(), (a) save the fitted model, (b) use predict.lm() to find the fitted y-values corresponding to x=1 and x=10, and then (c) use lines() to add a line between the two points:

    f <- lm(y~x)
    X <- c(1, 10)
    Y <- predict(f, newdata=data.frame(x=X))
    
    plot(x,y)
    lines(x=X, y=Y)
    

提交回复
热议问题