I need help in customizing the smoothing to my time series data. The code below smooths the data using sm.regression
and approx
functions, but the degree of smoothing is not user controlled, i.e. By changing function parameters I want the ability to control whether the smoothed curve follows the underlying data more closely or less closely.
find.extrema <- function(x)
{
if(is.xts(x)) {
y = as.vector( Cl(x) )
} else {
y = x
}
n = len(y)
t = 1:n
h = h.select(t, y, method = 'cv')
temp = sm.regression(t, y, h=h, display = 'none')
mhat = approx(temp$eval.points, temp$estimate, t, method='linear')$y
#mhat = y #to exactly match underlying data
return (mhat)
}
Any help would be appreciated.
Thanks.
There are not many questions regarding the
sm
package. Perhaps it is not widely used nowadays, but I still remember playing with it a lot when doing my MRes degree.You can't control smoothness because you are using cross-validation for auto-selection of smoothing parameter. Just get rid of the
h.select
line and passh
as an argument of your function.The whole point of
sm
package on kernel smoothing and / or kernel density estimation, is the cross-validation part. If you don't utilize it, you can just useksmooth
from R base for Nadaraya-Watson kernel estimator. You may read more about it from Scatter plot kernel smoothing: ksmooth() does not smooth my data at all. I did made a comparison withsm.regression
there.