Optimize Spline Models for Time Series Resampling
Source:R/utils_optimize_spline.R
utils_optimize_spline.Rd
Internal function used in zoo_resample()
. It finds optimal df
parameter of a smoothing spline model y ~ x
fitted with stats::smooth.spline()
that minimizes the root mean squared error (rmse) between observations and predictions, and returns a model fitted with such df
.
Arguments
- x
(required, numeric vector) predictor, a time vector coerced to numeric. Default: NULL
- y
(required, numeric vector) response, a column of a zoo object. Default: NULL
- max_complexity
(required, logical). If TRUE, RMSE optimization is ignored, and the model of maximum complexity is returned. Default: FALSE
See also
Other tsl_processing_internal:
utils_drop_geometry()
,
utils_global_scaling_params()
,
utils_optimize_loess()
,
utils_rescale_vector()
Examples
#zoo time series
xy <- zoo_simulate(
cols = 1,
rows = 30
)
#optimize splines model
m <- utils_optimize_spline(
x = as.numeric(zoo::index(xy)), #predictor
y = xy[, 1] #response
)
print(m)
#> Call:
#> stats::smooth.spline(x = model_df$x, y = model_df$y, df = complexity_value,
#> all.knots = TRUE)
#>
#> Smoothing Parameter spar= -0.9835838 lambda= 8.196849e-16 (26 iterations)
#> Equivalent Degrees of Freedom (Df): 30
#> Penalized Criterion (RSS): 3.436391e-19
#> GCV: 0.04191323
#plot observation
plot(
x = zoo::index(xy),
y = xy[, 1],
col = "forestgreen",
type = "l",
lwd = 2
)
#plot prediction
points(
x = zoo::index(xy),
y = stats::predict(
object = m,
x = as.numeric(zoo::index(xy))
)$y,
col = "red"
)