tf_smooth | R Documentation |
tf
objectsApply running means or medians, lowess
or Savitzky-Golay
filtering to smooth functional data. This does nothing for tfb
-objects,
which should be smoothed by using a smaller basis / stronger penalty.
tf_smooth(x, ...)
## S3 method for class 'tfb'
tf_smooth(x, verbose = TRUE, ...)
## S3 method for class 'tfd'
tf_smooth(
x,
method = c("lowess", "rollmean", "rollmedian", "savgol"),
verbose = TRUE,
...
)
x |
a |
... |
arguments for the respective |
verbose |
give lots of diagnostic messages? Defaults to TRUE |
method |
one of "lowess" (see |
tf_smooth.tfd
overrides/automatically sets some defaults of the
used methods:
lowess
uses a span parameter of f
= 0.15 (instead of 0.75)
by default.
rollmean
/median
use a window size of k
= $<$number of
grid points$>$/20 (i.e., the nearest odd integer to that) and sets fill= "extend"
(i.e., constant extrapolation to replace missing values at the
extremes of the domain) by default. Use fill= NA
for zoo
's default
behavior of shortening the smoothed series.
savgol
uses a window size of k
= $<$number of
grid points$>$/10 (i.e., the nearest odd integer to that).
a smoothed version of the input. For some methods/options, the smoothed functions may be shorter than the original ones (at both ends).
library(zoo)
library(pracma)
f <- tf_sparsify(tf_jiggle(tf_rgp(4, 201, nugget = 0.05)))
f_lowess <- tf_smooth(f, "lowess")
# these methods ignore the distances between arg-values:
f_mean <- tf_smooth(f, "rollmean")
f_median <- tf_smooth(f, "rollmean", k = 31)
f_sg <- tf_smooth(f, "savgol", fl = 31)
layout(t(1:4))
plot(f, points = FALSE, main = "original")
plot(f_lowess,
points = FALSE, col = "blue", main = "lowess (default,\n span 0.9 in red)"
)
lines(tf_smooth(f, "lowess", f = 0.9), col = "red", alpha = 0.2)
plot(f_mean,
points = FALSE, col = "blue", main = "rolling means &\n medians (red)"
)
lines(f_median, col = "red", alpha = 0.2) # note constant extrapolation at both ends!
plot(f, points = FALSE, main = "orginal and\n savgol (red)")
lines(f_sg, col = "red")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.