将中位数 "best fit line" 添加到 r 中的散点图

Add a median "best fit line" to a scatter plot in r

我想向基于 x 范围的中值 y 的散点图添加一条最适合的经验线,例如,在 x=0.105 处将是 x 的中值 y 的 y 线范围 0.100 到 0.110,在 x=0.115 将是 x 在 0.110 到 0.120 范围内的 y 中值的 y 线,等等。这些 y 线点将被连接起来。这些 x 只是为了说明,我想在他们的选择上保持灵活。 (我不需要参数拟合,谢谢)

这模拟了一些数据:

x<-0.1+runif(n=1000,min=0,max=1)
y<-1/x+runif(n=1000,min=0,max=1)*(1-x)*5
plot(x,y)

这是橙色中线的模型。

plot() 函数之后,尝试以下操作:

lines( x = lowess( x = x, y = y ), col = "darkgoldenrod2", lwd = 4 )

来自?lowess

lowess returns a list containing components x and y which give the coordinates of the smooth. The smooth can be added to a plot of the original points with the function lines().

我会研究低平滑作为选项。查看 f 选项,了解您希望它成为 'smooth' 的样子。我在下面复制了一些文档。

option f the smoother span. This gives the proportion of points in the plot which influence the smooth at each value. Larger values give more smoothness.

x<-0.1+runif(n=1000,min=0,max=1)
    y<-1/x+runif(n=1000,min=0,max=1)*(1-x)*5    
    plot(x, y)
    lines(lowess(x, y), col=2)
    lines(lowess(x, y, f=.2), col=3)