首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >smooth.spline超调

smooth.spline超调
EN

Stack Overflow用户
提问于 2021-08-15 20:02:26
回答 1查看 36关注 0票数 2

我已经尝试了几种不同的开箱即用选项来平滑我的数据中的一步,但还没有找到我真正想要的东西。下面粘贴一个小的可重现的例子。正如下面的屏幕截图中突出显示的那样,是否有一个R函数可以平滑步骤,类似于smooth.spline(),但没有超调(如红色箭头所指)?

代码语言:javascript
复制
# 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)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-15 21:09:06

您可以使用内核回归,例如,由stats::ksmooth()实现的Nadaraya-Watson estimator。下面的循环将bandwidth1到10的拟合添加到您的曲线图中:

代码语言:javascript
复制
for(bw in 1:10) {
  lines(
   ksmooth(x, y, kernel = "normal", bandwidth = bw, n.points = 200),
   col = "blue", lty = 1, lwd = 1)
}

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68794979

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档