# State-space smoothing with Kalman Filter (KF)
# Load package and example data
library(daltoolbox)
data(tsd)
tsd$y[9] <- 2 * tsd$y[9] # inject an outlier
# Fit KF (H = obs noise, Q = process noise) and transform
filter <- ts_fil_kalman()
filter <- fit(filter, tsd$y)
y <- transform(filter, tsd$y)
# Plot original vs KF-smoothed series
plot_ts_pred(y = tsd$y, yadj = y)
Run the code above in your browser using DataLab