Reputation: 469
I've tried a couple different out-of-the-box options for smoothing a step in my data, but haven't hit upon exactly what I'm after. Pasting a small reproducible example below. As highlighted in the screen grab below, is there an R function that would smooth the step, similar to smooth.spline()
, but without the overshoot (as pointed to by the red arrow)?
# 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)
Upvotes: 2
Views: 232
Reputation: 9678
You could use kernel regression, e.g., the Nadaraya-Watson estimator as implemented by stats::ksmooth()
. The loop below adds fits for bandwidth
s 1 to 10 to your plot:
for(bw in 1:10) {
lines(
ksmooth(x, y, kernel = "normal", bandwidth = bw, n.points = 200),
col = "blue", lty = 1, lwd = 1)
}
Upvotes: 3