我有严格增加的数据,并且smooth.spline()由于易于使用此功能,因此如果可能的话,还希望适应单调增加的平滑样条函数.
例如,我可以使用以下示例有效地复制我的数据:
testx <- 1:100
testy <- abs(rnorm(length(testx)))^3
testy <- cumsum(testy)
plot(testx,testy)
sspl <- smooth.spline(testx,testy)
lines(sspl,col="blue")
Run Code Online (Sandbox Code Playgroud)
这不一定随处可见.有什么建议?
Nic*_*ich 10
这不会使用,smooth.spline()但是splinefun(..., method="hyman")它将适合单调增加的样条并且也易于使用.例如:
testx <- 1:100
testy <- abs(rnorm(length(testx)))^3
testy <- cumsum(testy)
plot(testx,testy)
sspl <- smooth.spline(testx,testy)
lines(sspl,col="blue")
tmp <- splinefun(x=testx, y=cumsum(testy), method="hyman")
lines(testx[-1], diff(tmp(testx)), col="red")
Run Code Online (Sandbox Code Playgroud)
从帮助文件splinefun:"方法"hyman"计算单调三次样条使用Hyman过滤方法="fmm"适合严格单调输入.(在R 2.15.2中添加.)"
您可以为此使用形状约束样条,例如使用scam包:
require(scam)
fit = scam(testy~s(testx, k=100, bs="mpi", m=5),
family=gaussian(link="identity"))
plot(testx,testy)
lines(testx,predict(fit),col="red")
Run Code Online (Sandbox Code Playgroud)
或者,如果您想使用 L1 损失而不是 L2 损失,后者对异常值不太敏感,您也可以使用该cobs包...
与上述解决方案相比,此方法的优点在于,如果由于存在噪声,原始数据可能不是 100% 单调,它也可以工作......
| 归档时间: |
|
| 查看次数: |
5182 次 |
| 最近记录: |