fitted.dynamitefit | R Documentation |
Fitted values for a dynamitefit
object, i.e.,
E(y_t | newdata, \theta)
where \theta
contains all the
model parameters. See also predict.dynamitefit()
for multi-step
predictions.
## S3 method for class 'dynamitefit'
fitted(
object,
newdata = NULL,
n_draws = NULL,
thin = 1,
expand = TRUE,
df = TRUE,
...
)
object |
[ |
newdata |
[ |
n_draws |
[ |
thin |
[ |
expand |
[ |
df |
[ |
... |
Ignored. |
A data.frame
containing the fitted values.
Obtaining predictions
predict.dynamitefit()
data.table::setDTthreads(1) # For CRAN
fitted(gaussian_example_fit, n_draws = 2L)
set.seed(1)
# Please update your rstan and StanHeaders installation before running
# on Windows
if (!identical(.Platform$OS.type, "windows")) {
fit <- dynamite(
dformula = obs(LakeHuron ~ 1, "gaussian") + lags(),
data = data.frame(LakeHuron, time = seq_len(length(LakeHuron)), id = 1),
time = "time",
group = "id",
chains = 1,
refresh = 0
)
if (requireNamespace("dplyr") && requireNamespace("tidyr")) {
# One-step ahead samples (fitted values) from the posterior
# (first time point is fixed due to lag in the model):
f <- dplyr::filter(fitted(fit), time > 2)
ggplot2::ggplot(f, ggplot2::aes(time, LakeHuron_fitted, group = .draw)) +
ggplot2::geom_line(alpha = 0.5) +
# observed values
ggplot2::geom_line(ggplot2::aes(y = LakeHuron), colour = "tomato") +
ggplot2::theme_bw()
# Posterior predictive distribution given the first time point:
p <- dplyr::filter(predict(fit, type = "mean"), time > 2)
ggplot2::ggplot(p, ggplot2::aes(time, LakeHuron_mean, group = .draw)) +
ggplot2::geom_line(alpha = 0.5) +
# observed values
ggplot2::geom_line(ggplot2::aes(y = LakeHuron), colour = "tomato") +
ggplot2::theme_bw()
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.