#simulate irregular time series
x <- zoo_simulate(
cols = 2,
rows = 50,
time_range = c("2010-01-01", "2020-01-01"),
irregular = TRUE
)
#plot time series
if(interactive()){
zoo_plot(x)
}
#intervals between samples
x_intervals <- diff(zoo::index(x))
x_intervals
#create regular time from the minimum of the observed intervals
new_time <- seq.Date(
from = min(zoo::index(x)),
to = max(zoo::index(x)),
by = floor(min(x_intervals))
)
new_time
diff(new_time)
#resample using piecewise linear regression
x_linear <- zoo_resample(
x = x,
new_time = new_time,
method = "linear"
)
#resample using max complexity splines
x_spline <- zoo_resample(
x = x,
new_time = new_time,
method = "spline",
max_complexity = TRUE
)
#resample using max complexity loess
x_loess <- zoo_resample(
x = x,
new_time = new_time,
method = "loess",
max_complexity = TRUE
)
#intervals between new samples
diff(zoo::index(x_linear))
diff(zoo::index(x_spline))
diff(zoo::index(x_loess))
#plotting results
if(interactive()){
par(mfrow = c(4, 1), mar = c(3,3,2,2))
zoo_plot(
x,
guide = FALSE,
title = "Original"
)
zoo_plot(
x_linear,
guide = FALSE,
title = "Method: linear"
)
zoo_plot(
x_spline,
guide = FALSE,
title = "Method: spline"
)
zoo_plot(
x_loess,
guide = FALSE,
title = "Method: loess"
)
}
Run the code above in your browser using DataLab