我已经尝试了几种不同的开箱即用选项来平滑我的数据中的一步,但还没有找到我真正想要的东西。下面粘贴一个小的可重现的例子。正如下面的屏幕截图中突出显示的那样,是否有一个R函数可以平滑步骤,类似于smooth.spline(),但没有超调(如红色箭头所指)?
# data
x <- seq(1,100)
y <- c(rep(4,times=50), rep(10,times=50))
# 1st attempt used loess(), but was not what I wanted
lo_t <- loess(y~x)
plot(x, y, ylim=c(0,12), las=1)
lines(predict(lo_t), col='red', lty=2)
# 2nd attempt used smooth.spline() and is much closer to what I'm after.
# Would like to eliminate the overshoot, Ok with trying out a different function
smoothingSpline <- smooth.spline(x, y, spar=0.5)
plot(x[seq(2,100,by=2)], y[seq(2,100,by=2)], ylim=c(3,11), las=1, pch=16, col='grey70', xlab='X', ylab='Y')
lines(smoothingSpline, col='red', lty=2, lwd=2)

发布于 2021-08-15 21:09:06
您可以使用内核回归,例如,由stats::ksmooth()实现的Nadaraya-Watson estimator。下面的循环将bandwidth1到10的拟合添加到您的曲线图中:
for(bw in 1:10) {
lines(
ksmooth(x, y, kernel = "normal", bandwidth = bw, n.points = 200),
col = "blue", lty = 1, lwd = 1)
}

https://stackoverflow.com/questions/68794979
复制相似问题