gpt4 book ai didi

r - 使用 smooth.spline 过冲

转载 作者:行者123 更新时间:2023-12-04 07:15:35 26 4
gpt4 key购买 nike

我尝试了几种不同的开箱即用选项来平滑我的数据中的一个步骤,但还没有完全找到我所追求的。在下面粘贴一个小的可复制示例。正如下面的屏幕截图中突出显示的,是否有一个 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)
How to remove overshoot pointed to by red arrows?

最佳答案

您可以使用内核回归,例如 Nadaraya-Watson estimatorstats::ksmooth() 实现.下面的循环为 bandwidth 添加了拟合s 1 到 10 到你的情节:

for(bw in 1:10) {
lines(
ksmooth(x, y, kernel = "normal", bandwidth = bw, n.points = 200),
col = "blue", lty = 1, lwd = 1)
}
enter image description here

关于r - 使用 smooth.spline 过冲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68794979/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com