如何将平滑线拟合到某些点但保持单调性

How to fit a smooth line to some points but preserve monotonicity

我有以下数据点

example<-structure(list(y = c(1, 0.961538461538462, 0.923076923076923, 
0.884615384615385, 0.846153846153846, 0.807692307692308, 0.769230769230769, 0.730769230769231, 0.730769230769231, 0.730769230769231, 0.687782805429864, 0.687782805429864, 0.641930618401207, 0.596078431372549, 0.596078431372549, 0.54640522875817, 0.496732026143791, 0.496732026143791, 0.496732026143791, 
0.496732026143791, 0.496732026143791, 0.496732026143791, 0.496732026143791, 0.496732026143791, 0.496732026143791, 0.496732026143791, 0.496732026143791
), x = c(0, 59, 115, 156, 268, 329, 353, 365, 377, 421, 431, 
448, 464, 475, 477, 563, 638, 744, 769, 770, 803, 855, 1040, 
1106, 1129, 1206, 1227)), .Names = c("y", "x"), row.names = c(NA, 
-27L), class = "data.frame")

我想拟合一条平滑的线。在 R 中有几种方法可以做到这一点,使用 loessksmoothlocpoly

有没有办法确保强制得到的平滑线将单调(在本例中单调递减?)

您可以使用 scam 包中的 scam() 函数来进行带约束的单变量或多变量平滑。帮助文件 ?scam:::shape.constrained.smooth.terms 显示了所有可用选项。例如,用于平滑的 B 样条基可以被惩罚以产生单调递减的系数 scam(y~s(x,bs="mpd")).

require(scam)   

attach(example)

yhat <- predict(scam(y~s(x,bs="mpd")),se=TRUE)

plot(x,y)
lines(x,y=yhat$fit)
lines(x,y=yhat$fit+1.96*yhat$se.fit,lty=2)
lines(x,y=yhat$fit-1.96*yhat$se.fit,lty=2)