Title: | Efficient Bayesian Inference for Time-Varying Parameter Models with Shrinkage |
---|---|
Description: | Efficient Markov chain Monte Carlo (MCMC) algorithms for fully Bayesian estimation of time-varying parameter models with shrinkage priors, both dynamic and static. Details on the algorithms used are provided in Bitto and Frühwirth-Schnatter (2019) <doi:10.1016/j.jeconom.2018.11.006> and Cadonna et al. (2020) <doi:10.3390/econometrics8020020> and Knaus and Frühwirth-Schnatter (2023) <doi:10.48550/arXiv.2312.10487>. For details on the package, please see Knaus et al. (2021) <doi:10.18637/jss.v100.i13>. |
Authors: | Peter Knaus [aut, cre] , Angela Bitto-Nemling [aut], Annalisa Cadonna [aut] , Sylvia Frühwirth-Schnatter [aut] , Daniel Winkler [ctb], Kemal Dingic [ctb] |
Maintainer: | Peter Knaus <[email protected]> |
License: | GPL (>= 2) |
Version: | 3.0.1 |
Built: | 2024-11-15 06:56:01 UTC |
Source: | CRAN |
eval_pred_dens
evaluates the one-step ahead predictive density of a fitted TVP model resulting from a call to
shrinkTVP at the points supplied in x
. For details on the approximation of the one-step ahead predictive density used,
see the vignette.
eval_pred_dens(x, mod, data_test, log = FALSE)
eval_pred_dens(x, mod, data_test, log = FALSE)
x |
a real number or a vector of real numbers, taken to be the points at which the predictive density will be evaluated. |
mod |
an object of class |
data_test |
a data frame with one row, containing the one-step ahead covariates. The names of the covariates have to match the
names of the covariates used during model estimation in the call to |
log |
a single logical value determining whether the density should be evaluated on the log scale or not. |
The value returned is a vector of length length(x)
, containing the values of the predictive density evaluated
at the points supplied in x
.
Peter Knaus [email protected]
Other prediction functions:
LPDS()
,
fitted.shrinkTVP()
,
forecast_shrinkTVP()
,
predict.shrinkTVP()
,
residuals.shrinkTVP()
# Simulate data set.seed(123) sim <- simTVP(theta = c(0.2, 0, 0), beta_mean = c(1.5, -0.3, 0)) data <- sim$data # Estimate model res <- shrinkTVP(y ~ x1 + x2, data = data[1:199, ]) # Create sequence of x values where the density is to be evaluated x_vals <- seq(0, 12, by = 0.1) # Evaluate density and plot dens <- eval_pred_dens(x_vals, res, data[200, ]) plot(x_vals, dens, type = "l") # Add vertical line where true value of the one-step ahead y lies abline(v = data$y[200])
# Simulate data set.seed(123) sim <- simTVP(theta = c(0.2, 0, 0), beta_mean = c(1.5, -0.3, 0)) data <- sim$data # Estimate model res <- shrinkTVP(y ~ x1 + x2, data = data[1:199, ]) # Create sequence of x values where the density is to be evaluated x_vals <- seq(0, 12, by = 0.1) # Evaluate density and plot dens <- eval_pred_dens(x_vals, res, data[200, ]) plot(x_vals, dens, type = "l") # Add vertical line where true value of the one-step ahead y lies abline(v = data$y[200])
Calculates the fitted values for an estimated TVP model, i.e. .
Note that in contrast to
predict.shrinkTVP
this does not include the error term.
## S3 method for class 'shrinkTVP' fitted(object, ...)
## S3 method for class 'shrinkTVP' fitted(object, ...)
object |
A |
... |
Currently ignored. |
An object of class shrinkTVP_fitted
Peter Knaus [email protected]
Other prediction functions:
LPDS()
,
eval_pred_dens()
,
forecast_shrinkTVP()
,
predict.shrinkTVP()
,
residuals.shrinkTVP()
# Generate synthetic data sim <- simTVP() # Estimate a model res <- shrinkTVP(y ~ x1 + x2, sim$data) # Calculate fitted values fitted <- fitted(res) # Visualize plot(fitted) lines(sim$data$y, col = "forestgreen")
# Generate synthetic data sim <- simTVP() # Estimate a model res <- shrinkTVP(y ~ x1 + x2, sim$data) # Calculate fitted values fitted <- fitted(res) # Visualize plot(fitted) lines(sim$data$y, col = "forestgreen")
forecast_shrinkTVP
draws from the posterior predictive distribution of a fitted TVP model resulting from a call to
shrinkTVP
.
forecast_shrinkTVP(mod, newdata, n.ahead)
forecast_shrinkTVP(mod, newdata, n.ahead)
mod |
an object of class |
newdata |
a data frame containing the future covariates. The names of the covariates
have to match the names used during model estimation in the call to |
n.ahead |
a single, positive integer indicating the forecasting horizon, i.e. how many time-points into the future
the posterior predictive distribution should be sampled from. Can not be larger than the number of rows in |
The value returned is a list object of class shrinkTVP_forc
containing the samples from the
posterior predictive density.
Peter Knaus [email protected]
Other prediction functions:
LPDS()
,
eval_pred_dens()
,
fitted.shrinkTVP()
,
predict.shrinkTVP()
,
residuals.shrinkTVP()
# Simulate data set.seed(123) sim <- simTVP(theta = c(0.2, 0, 0), beta_mean = c(1.5, -0.3, 0)) data <- sim$data # Estimate model res <- shrinkTVP(y ~ x1 + x2, data = data[1:190, ]) # Forecast forc <- forecast_shrinkTVP(res, data[191:200, ]) # Plot plot(forc)
# Simulate data set.seed(123) sim <- simTVP(theta = c(0.2, 0, 0), beta_mean = c(1.5, -0.3, 0)) data <- sim$data # Estimate model res <- shrinkTVP(y ~ x1 + x2, data = data[1:190, ]) # Forecast forc <- forecast_shrinkTVP(res, data[191:200, ]) # Plot plot(forc)
LPDS
calculates the one-step ahead Log Predictive Density Score (LPDS) of a fitted TVP model resulting from a call to
shrinkTVP
For details on the approximation of the one-step ahead predictive density used, see the vignette.
LPDS(mod, data_test)
LPDS(mod, data_test)
mod |
an object of class |
data_test |
a data frame with one row, containing the one-step ahead covariates and response. The names of the covariates
and the response have to match the names used during model estimation in the call to |
A real number equaling the calculated LPDS.
Peter Knaus [email protected]
Other prediction functions:
eval_pred_dens()
,
fitted.shrinkTVP()
,
forecast_shrinkTVP()
,
predict.shrinkTVP()
,
residuals.shrinkTVP()
# Simulate data set.seed(123) sim <- simTVP(theta = c(0.2, 0, 0), beta_mean = c(1.5, -0.3, 0)) data <- sim$data # Estimate model res <- shrinkTVP(y ~ x1 + x2, data = data[1:199, ]) # Calculate LPDS LPDS(res, data[200,])
# Simulate data set.seed(123) sim <- simTVP(theta = c(0.2, 0, 0), beta_mean = c(1.5, -0.3, 0)) data <- sim$data # Estimate model res <- shrinkTVP(y ~ x1 + x2, data = data[1:199, ]) # Calculate LPDS LPDS(res, data[200,])
plot.mcmc.tvp
plots empirical posterior quantiles for a time-varying parameter.
## S3 method for class 'mcmc.tvp' plot( x, probs = c(0.025, 0.25, 0.75, 0.975), shaded = TRUE, quantlines = FALSE, shadecol = "skyblue", shadealpha = 0.5, quantlty = 2, quantcol = "black", quantlwd = 0.5, drawzero = TRUE, zerolty = 2, zerolwd = 1, zerocol = "grey", ... )
## S3 method for class 'mcmc.tvp' plot( x, probs = c(0.025, 0.25, 0.75, 0.975), shaded = TRUE, quantlines = FALSE, shadecol = "skyblue", shadealpha = 0.5, quantlty = 2, quantcol = "black", quantlwd = 0.5, drawzero = TRUE, zerolty = 2, zerolwd = 1, zerocol = "grey", ... )
x |
|
probs |
vector of boundaries for credible intervals to plot for each point in time, with values in [0,1].
The largest and smallest value form the outermost credible interval, the second smallest and second largest the second outermost and so forth.
The default value is |
shaded |
single logical value or a vector of logical values, indicating whether or not to shade the area between the pointwise
credible intervals. If a vector is given, the first value given is used to determine if the area between the outermost credible interval
is shaded, the second for the second outermost and so forth. Recycled in the usual fashion if the vector is shorter than the
number of quantile pairs. The default value is |
quantlines |
single logical value or a vector of logical values, indicating whether or not to draw borders along the pointwise
credible intervals. If a vector is given, the first value given is used to determine whether the outermost credible interval
is marked by lines, the second for the second outermost and so forth. Recycled in the usual fashion if the vector is shorter than the
number of credible intervals. The default value is |
shadecol |
single character string or a vector of character strings. Determines the color of the shaded areas that represent
the credible intervals. If a vector is given, the first color given is used for the outermost area,
the second for the second outermost and so forth. Recycled in the usual fashion if the vector is shorter than the number
of shaded areas. Has no effect if |
shadealpha |
real number between 0 and 1 or a vector of real numbers between 0 and 1.
Determines the level of transparency of the shaded areas that represent
the credible intervals. If a vector is given, the first value
given is used for the outermost area, the second for the second outermost and so forth. Recycled in the usual fashion
if the vector is shorter than the number of shaded areas. Has no effect if |
quantlty |
either a single integer in [0,6] or one of the character strings |
quantcol |
single character string or a vector of character strings. Determines the color of the borders drawn around the shaded
areas that represent the credible intervals. If a vector is given, the first color given is used for borders of the outermost area,
the second for the second outermost and so forth. Recycled in the usual fashion if the vector is shorter than the number
of shaded areas. Has no effect if |
quantlwd |
single real, positive number or a vector of real, positive numbers. Determines the line width of the borders
drawn around the shaded areas that represent the credible intervals. If a vector is given, the first number given is used for
the borders of the outermost area, the second for the second outermost and so forth. Recycled in the usual fashion if the vector
is shorter than the number of shaded areas. Has no effect if |
drawzero |
single logical value determining whether to draw a horizontal line at zero or not. The default value is |
zerolty |
single integer in [0,6] or one of the character strings |
zerolwd |
single real, positive number. Determines the line width of the horizontal line at zero. Has no effect
if |
zerocol |
single character string. Determines the color of the horizontal line at zero. Has no effect if |
... |
further arguments to be passed to |
Called for its side effects and returns invisibly.
Peter Knaus [email protected]
Other plotting functions:
plot.shrinkTVP_forc()
,
plot.shrinkTVP()
set.seed(123) sim <- simTVP(theta = c(0.2, 0, 0), beta_mean = c(1.5, -0.3, 0)) data <- sim$data res <- shrinkTVP(y ~ x1 + x2, data) plot(res$beta$beta_x1)
set.seed(123) sim <- simTVP(theta = c(0.2, 0, 0), beta_mean = c(1.5, -0.3, 0)) data <- sim$data res <- shrinkTVP(y ~ x1 + x2, data) plot(res$beta$beta_x1)
plot.shrinkTVP
generates plots visualizing the posterior distribution.
## S3 method for class 'shrinkTVP' plot( x, pars = c("beta"), nplot = 3, h_borders = c(0.05, 0.05), w_borders = c(0.02, 0.02), ... )
## S3 method for class 'shrinkTVP' plot( x, pars = c("beta"), nplot = 3, h_borders = c(0.05, 0.05), w_borders = c(0.02, 0.02), ... )
x |
a |
pars |
a character vector containing the names of the parameters to be visualized.
The names have to coincide with the names of the list elements of the |
nplot |
positive integer that indicates the number of tvp plots to display on a single page before a new page is generated. The default value is 3. |
h_borders |
single real, positive number smaller than 0.5 or a vector containing two such numbers. Determines
the relative amount of space (the total amount summing up to 1) left blank on the left and right of the plot, in that order.
The default is |
w_borders |
single real, positive number smaller than 0.5 or a vector containing two such numbers. Determines
the relative amount of space (the total amount summing up to 1) left blank at the top and bottom of the plot, in that order.
The default is |
... |
further arguments to be passed to the respective plotting functions. |
Called for its side effects and returns invisibly.
Peter Knaus [email protected]
Other plotting functions:
plot.mcmc.tvp()
,
plot.shrinkTVP_forc()
set.seed(123) sim <- simTVP(theta = c(0.2, 0, 0), beta_mean = c(1.5, -0.3, 0)) data <- sim$data output <- shrinkTVP(y ~ x1 + x2, data) plot(output) ## Will produce an error because 'hello' is not a parameter in the model ## Not run: plot(output, pars = c("beta", "hello")) ## End(Not run)
set.seed(123) sim <- simTVP(theta = c(0.2, 0, 0), beta_mean = c(1.5, -0.3, 0)) data <- sim$data output <- shrinkTVP(y ~ x1 + x2, data) plot(output) ## Will produce an error because 'hello' is not a parameter in the model ## Not run: plot(output, pars = c("beta", "hello")) ## End(Not run)
plot.shrinkTVP_forc
generates plots visualizing the posterior predictive density generated by forecast_shrinkTVP
.
## S3 method for class 'shrinkTVP_forc' plot(x, showgap = FALSE, ...)
## S3 method for class 'shrinkTVP_forc' plot(x, showgap = FALSE, ...)
x |
a |
showgap |
if |
... |
further arguments to be passed to |
Called for its side effects and returns invisibly.
Peter Knaus [email protected]
Other plotting functions:
plot.mcmc.tvp()
,
plot.shrinkTVP()
set.seed(123) sim <- simTVP() train <- sim$data[1:190, ] test <- sim$data[191:200, ] res <- shrinkTVP(y ~ x1 + x2, train) forecast <- forecast_shrinkTVP(res, test) plot(forecast) lines(sim$data$y, col = "forestgreen")
set.seed(123) sim <- simTVP() train <- sim$data[1:190, ] test <- sim$data[191:200, ] res <- shrinkTVP(y ~ x1 + x2, train) forecast <- forecast_shrinkTVP(res, test) plot(forecast) lines(sim$data$y, col = "forestgreen")
Calculates the predicted past values for an estimated TVP model, i.e. .
Note that in contrast to
fitted.shrinkTVP
this includes the error term.
## S3 method for class 'shrinkTVP' predict(object, ...)
## S3 method for class 'shrinkTVP' predict(object, ...)
object |
a |
... |
Currently ignored. |
An object of class shrinkTVP_pred
.
Peter Knaus [email protected]
Other prediction functions:
LPDS()
,
eval_pred_dens()
,
fitted.shrinkTVP()
,
forecast_shrinkTVP()
,
residuals.shrinkTVP()
# Generate synthetic data sim <- simTVP(N = 300) # Estimate a model res <- shrinkTVP(y ~ x1 + x2, sim$data) # Calculate predicted values pred <- predict(res) # Visualize plot(pred) lines(sim$data$y, col = "forestgreen")
# Generate synthetic data sim <- simTVP(N = 300) # Estimate a model res <- shrinkTVP(y ~ x1 + x2, sim$data) # Calculate predicted values pred <- predict(res) # Visualize plot(pred) lines(sim$data$y, col = "forestgreen")
Nicer printing of shrinkTVP objects
## S3 method for class 'shrinkTVP' print(x, ...)
## S3 method for class 'shrinkTVP' print(x, ...)
x |
a |
... |
Currently ignored. |
Called for its side effects and returns invisibly.
Peter Knaus [email protected]
Calculates the residuals for an estimated TVP model, i.e. .
## S3 method for class 'shrinkTVP' residuals(object, ...)
## S3 method for class 'shrinkTVP' residuals(object, ...)
object |
a |
... |
Currently ignored. |
An object of class shrinkTVP_resid
Peter Knaus [email protected]
Other prediction functions:
LPDS()
,
eval_pred_dens()
,
fitted.shrinkTVP()
,
forecast_shrinkTVP()
,
predict.shrinkTVP()
# Generate synthetic data sim <- simTVP(N = 300) # Estimate a model res <- shrinkTVP(y ~ x1 + x2, sim$data) # Calculate residuals resids <- residuals(res) # Visualize plot(resids)
# Generate synthetic data sim <- simTVP(N = 300) # Estimate a model res <- shrinkTVP(y ~ x1 + x2, sim$data) # Calculate residuals resids <- residuals(res) # Visualize plot(resids)
shrinkTVP
samples from the joint posterior distribution of the parameters of a time-varying
parameter model with dynamic triple gamma shrinkage, potentially including stochastic volatility (SV),
and returns the MCMC draws.
shrinkDTVP( formula, data, mod_type = "double", niter = 10000, nburn = round(niter/2), nthin = 1, learn_a_xi = TRUE, learn_a_tau = TRUE, a_xi = 0.1, a_tau = 0.1, learn_c_xi = TRUE, learn_c_tau = TRUE, c_xi = 0.1, c_tau = 0.1, a_eq_c_xi = FALSE, a_eq_c_tau = FALSE, learn_kappa2_B = TRUE, learn_lambda2_B = TRUE, kappa2_B = 20, lambda2_B = 20, a_psi = 0.5, c_psi = 0.5, iid = FALSE, shrink_inter = TRUE, hyperprior_param, display_progress = TRUE, sv = FALSE, sv_param, MH_tuning, starting_vals )
shrinkDTVP( formula, data, mod_type = "double", niter = 10000, nburn = round(niter/2), nthin = 1, learn_a_xi = TRUE, learn_a_tau = TRUE, a_xi = 0.1, a_tau = 0.1, learn_c_xi = TRUE, learn_c_tau = TRUE, c_xi = 0.1, c_tau = 0.1, a_eq_c_xi = FALSE, a_eq_c_tau = FALSE, learn_kappa2_B = TRUE, learn_lambda2_B = TRUE, kappa2_B = 20, lambda2_B = 20, a_psi = 0.5, c_psi = 0.5, iid = FALSE, shrink_inter = TRUE, hyperprior_param, display_progress = TRUE, sv = FALSE, sv_param, MH_tuning, starting_vals )
formula |
object of class "formula": a symbolic representation of the model, as in the
function |
data |
optional data frame containing the response variable and the covariates. If not found in |
mod_type |
character string that reads either |
niter |
positive integer, indicating the number of MCMC iterations
to perform, including the burn-in. Has to be larger than or equal to |
nburn |
non-negative integer, indicating the number of iterations discarded
as burn-in. Has to be smaller than or equal to |
nthin |
positive integer, indicating the degree of thinning to be performed. Every |
learn_a_xi |
logical value indicating whether to learn a_xi, the spike parameter of the state variances.
Ignored if |
learn_a_tau |
logical value indicating whether to learn a_tau, the spike parameter of the mean of the
initial values of the states. Ignored if |
a_xi |
positive, real number, indicating the (fixed) value for a_xi. Ignored if
|
a_tau |
positive, real number, indicating the (fixed) value for a_tau. Ignored if
|
learn_c_xi |
logical value indicating whether to learn c_xi, the tail parameter of the state variances.
Ignored if |
learn_c_tau |
logical value indicating whether to learn c_tau, the tail parameter of the mean of the
initial values of the states. Ignored if |
c_xi |
positive, real number, indicating the (fixed) value for c_xi. Ignored if
|
c_tau |
positive, real number, indicating the (fixed) value for c_tau. Ignored if
|
a_eq_c_xi |
logical value indicating whether to force |
a_eq_c_tau |
logical value indicating whether to force |
learn_kappa2_B |
logical value indicating whether to learn kappa2_B, the global level of shrinkage for
the state variances. The default value is |
learn_lambda2_B |
logical value indicating whether to learn the lambda2_B parameter,
the global level of shrinkage for the mean of the initial values of the states. The default value is |
kappa2_B |
positive, real number, indicating the (fixed) value for kappa2_B. Ignored if
|
lambda2_B |
positive, real number, indicating the (fixed) value for lambda2_B. Ignored if
|
a_psi |
positive, real number, or a vector of length equal to the number of covariates containing positive, real numbers. Indicates the value for a_psi, which is the pole parameter of the dynamic triple gamma. The default value is 0.5. |
c_psi |
positive, real number, or a vector of length equal to the number of covariates containing positive, real numbers. Indicates the value for c_psi, which is the tail parameter of the dynamic triple gamma. The default value is 0.5. |
iid |
logical value indicating whether the innovations are assumed to be independent and identically distributed.
If set to |
shrink_inter |
logical value indicating whether to dynamically shrink the intercept. Note that shrinkage is still applied
to the |
hyperprior_param |
optional named list containing hyperparameter values. Not all have to be supplied, with those missing being replaced by the default values. Any list elements that are misnamed will be ignored and a warning will be thrown. All hyperparameter values have to be positive, real numbers. The following hyperparameters can be supplied:
|
display_progress |
logical value indicating whether the progress bar and other informative output should be
displayed. The default value is |
sv |
logical value indicating whether to use stochastic volatility for the error of the observation
equation. For details please see |
sv_param |
optional named list containing hyperparameter values for the stochastic volatility
parameters. Not all have to be supplied, with those missing being replaced by the default values.
Any list elements that are misnamed will be ignored and a warning will be thrown. Ignored if
|
MH_tuning |
optional named list containing values used to tune the MH steps for
|
starting_vals |
optional named list containing the values at which the MCMC algorithm will be initialized. In the
following
|
For details concerning the algorithms please refer to the papers by Bitto and Frühwirth-Schnatter (2019), Cadonna et al. (2020) and Knaus and Frühwirth-Schnatter (2023). For more details on the package and the usage of the functions, see Knaus et al. (2021).
The value returned is a list object of class shrinkTVP
containing
beta |
|
beta_mean |
|
theta_sr |
|
tau2 |
|
xi2 |
|
psi |
|
lambda_p |
|
kappa_p |
(optional) |
lambda2 |
(optional) |
kappa2 |
(optional) |
a_xi |
(optional) |
a_tau |
(optional) |
c_xi |
(optional) |
c_tau |
(optional) |
lambda2_B |
(optional) |
kappa2_B |
(optional) |
rho |
(optional) |
sigma2 |
|
C0 |
(optional) |
sv_mu |
(optional) |
sv_phi |
(optional) |
sv_sigma2 |
(optional) |
MH_diag |
(optional) named list containing statistics for assessing MH performance. Not returned if no MH steps are required or none of them are specified to be adaptive. |
internals |
|
priorvals |
|
model |
|
summaries |
|
To display the output, use plot
and summary
. The summary
method displays the specified prior values stored in
priorvals
and the posterior summaries stored in summaries
, while the plot
method calls coda
's plot.mcmc
or the plot.mcmc.tvp
method. Furthermore, all functions that can be applied to coda::mcmc
objects
(e.g. coda::acfplot
) can be applied to all output elements that are coda
compatible.
Peter Knaus [email protected]
Bitto, A., & Frühwirth-Schnatter, S. (2019). "Achieving shrinkage in a time-varying parameter model framework." Journal of Econometrics, 210(1), 75-97. <doi:10.1016/j.jeconom.2018.11.006>
Cadonna, A., Frühwirth-Schnatter, S., & Knaus, P. (2020). "Triple the Gamma—A Unifying Shrinkage Prior for Variance and Variable Selection in Sparse State Space and TVP Models." Econometrics, 8(2), 20. <doi:10.3390/econometrics8020020>
Knaus, P., Bitto-Nemling, A., Cadonna, A., & Frühwirth-Schnatter, S. (2021) "Shrinkage in the Time-Varying Parameter Model Framework Using the R
Package shrinkTVP
."
Journal of Statistical Software 100(13), 1–32. <doi:10.18637/jss.v100.i13>
Knaus, P., & Frühwirth-Schnatter, S. (2023). "The Dynamic Triple Gamma Prior as a Shrinkage Process Prior for Time-Varying Parameter Models." arXiv preprint arXiv:2312.10487. <doi:10.48550/arXiv.2312.10487>
set.seed(123) sim <- simTVP(DTG = TRUE, theta = c(0, 1, 0), beta_mean = c(1, 1, 0), rho = 0.95, c_psi = 2) data <- sim$data ## Example 1, match the true underlying process res <- shrinkDTVP(y ~ x1 + x2, data = data, c_psi = 2) # summarize output summary(res) ## Example 2, dynamic horseshoe res <- shrinkDTVP(y ~ x1 + x2, data = data) ## Example 3, modify hyperparameters res <- shrinkDTVP(y ~ x1 + x2, data = data, hyperprior_param = list(a_rho = 1, alpha_rho = 0.5, beta_rho = 0.5))
set.seed(123) sim <- simTVP(DTG = TRUE, theta = c(0, 1, 0), beta_mean = c(1, 1, 0), rho = 0.95, c_psi = 2) data <- sim$data ## Example 1, match the true underlying process res <- shrinkDTVP(y ~ x1 + x2, data = data, c_psi = 2) # summarize output summary(res) ## Example 2, dynamic horseshoe res <- shrinkDTVP(y ~ x1 + x2, data = data) ## Example 3, modify hyperparameters res <- shrinkDTVP(y ~ x1 + x2, data = data, hyperprior_param = list(a_rho = 1, alpha_rho = 0.5, beta_rho = 0.5))
shrinkTVP
samples from the joint posterior distribution of the parameters of a time-varying
parameter model with shrinkage, potentially including stochastic volatility (SV), and returns the MCMC draws.
shrinkTVP( formula, data, mod_type = "double", niter = 10000, nburn = round(niter/2), nthin = 1, learn_a_xi = TRUE, learn_a_tau = TRUE, a_xi = 0.1, a_tau = 0.1, learn_c_xi = TRUE, learn_c_tau = TRUE, c_xi = 0.1, c_tau = 0.1, a_eq_c_xi = FALSE, a_eq_c_tau = FALSE, learn_kappa2_B = TRUE, learn_lambda2_B = TRUE, kappa2_B = 20, lambda2_B = 20, hyperprior_param, display_progress = TRUE, sv = FALSE, sv_param, MH_tuning, starting_vals )
shrinkTVP( formula, data, mod_type = "double", niter = 10000, nburn = round(niter/2), nthin = 1, learn_a_xi = TRUE, learn_a_tau = TRUE, a_xi = 0.1, a_tau = 0.1, learn_c_xi = TRUE, learn_c_tau = TRUE, c_xi = 0.1, c_tau = 0.1, a_eq_c_xi = FALSE, a_eq_c_tau = FALSE, learn_kappa2_B = TRUE, learn_lambda2_B = TRUE, kappa2_B = 20, lambda2_B = 20, hyperprior_param, display_progress = TRUE, sv = FALSE, sv_param, MH_tuning, starting_vals )
formula |
object of class "formula": a symbolic representation of the model, as in the
function |
data |
optional data frame containing the response variable and the covariates. If not found in |
mod_type |
character string that reads either |
niter |
positive integer, indicating the number of MCMC iterations
to perform, including the burn-in. Has to be larger than or equal to |
nburn |
non-negative integer, indicating the number of iterations discarded
as burn-in. Has to be smaller than or equal to |
nthin |
positive integer, indicating the degree of thinning to be performed. Every |
learn_a_xi |
logical value indicating whether to learn a_xi, the spike parameter of the state variances.
Ignored if |
learn_a_tau |
logical value indicating whether to learn a_tau, the spike parameter of the mean of the
initial values of the states. Ignored if |
a_xi |
positive, real number, indicating the (fixed) value for a_xi. Ignored if
|
a_tau |
positive, real number, indicating the (fixed) value for a_tau. Ignored if
|
learn_c_xi |
logical value indicating whether to learn c_xi, the tail parameter of the state variances.
Ignored if |
learn_c_tau |
logical value indicating whether to learn c_tau, the tail parameter of the mean of the
initial values of the states. Ignored if |
c_xi |
positive, real number, indicating the (fixed) value for c_xi. Ignored if
|
c_tau |
positive, real number, indicating the (fixed) value for c_tau. Ignored if
|
a_eq_c_xi |
logical value indicating whether to force |
a_eq_c_tau |
logical value indicating whether to force |
learn_kappa2_B |
logical value indicating whether to learn kappa2_B, the global level of shrinkage for
the state variances. The default value is |
learn_lambda2_B |
logical value indicating whether to learn the lambda2_B parameter,
the global level of shrinkage for the mean of the initial values of the states. The default value is |
kappa2_B |
positive, real number, indicating the (fixed) value for kappa2_B. Ignored if
|
lambda2_B |
positive, real number, indicating the (fixed) value for lambda2_B. Ignored if
|
hyperprior_param |
optional named list containing hyperparameter values. Not all have to be supplied, with those missing being replaced by the default values. Any list elements that are misnamed will be ignored and a warning will be thrown. All hyperparameter values have to be positive, real numbers. The following hyperparameters can be supplied:
|
display_progress |
logical value indicating whether the progress bar and other informative output should be
displayed. The default value is |
sv |
logical value indicating whether to use stochastic volatility for the error of the observation
equation. For details please see |
sv_param |
optional named list containing hyperparameter values for the stochastic volatility
parameters. Not all have to be supplied, with those missing being replaced by the default values.
Any list elements that are misnamed will be ignored and a warning will be thrown. Ignored if
|
MH_tuning |
optional named list containing values used to tune the MH steps for
|
starting_vals |
optional named list containing the values at which the MCMC algorithm will be initialized. In the
following
|
For details concerning the algorithms please refer to the papers by Bitto and Frühwirth-Schnatter (2019) and Cadonna et al. (2020). For more details on the package and the usage of the functions, see Knaus et al. (2021).
The value returned is a list object of class shrinkTVP
containing
beta |
|
beta_mean |
|
theta_sr |
|
tau2 |
|
xi2 |
|
lambda2 |
(optional) |
kappa2 |
(optional) |
a_xi |
(optional) |
a_tau |
(optional) |
c_xi |
(optional) |
c_tau |
(optional) |
lambda2_B |
(optional) |
kappa2_B |
(optional) |
sigma2 |
|
C0 |
(optional) |
sv_mu |
(optional) |
sv_phi |
(optional) |
sv_sigma2 |
(optional) |
MH_diag |
(optional) named list containing statistics for assessing MH performance. Not returned if no MH steps are required or none of them are specified to be adaptive. |
internals |
|
priorvals |
|
model |
|
summaries |
|
To display the output, use plot
and summary
. The summary
method displays the specified prior values stored in
priorvals
and the posterior summaries stored in summaries
, while the plot
method calls coda
's plot.mcmc
or the plot.mcmc.tvp
method. Furthermore, all functions that can be applied to coda::mcmc
objects
(e.g. coda::acfplot
) can be applied to all output elements that are coda
compatible.
Peter Knaus [email protected]
Bitto, A., & Frühwirth-Schnatter, S. (2019). "Achieving shrinkage in a time-varying parameter model framework." Journal of Econometrics, 210(1), 75-97. <doi:10.1016/j.jeconom.2018.11.006>
Cadonna, A., Frühwirth-Schnatter, S., & Knaus, P. (2020). "Triple the Gamma—A Unifying Shrinkage Prior for Variance and Variable Selection in Sparse State Space and TVP Models." Econometrics, 8(2), 20. <doi:10.3390/econometrics8020020>
Knaus, P., Bitto-Nemling, A., Cadonna, A., & Frühwirth-Schnatter, S. (2021) "Shrinkage in the Time-Varying Parameter Model Framework Using the R
Package shrinkTVP
."
Journal of Statistical Software 100(13), 1–32. <doi:10.18637/jss.v100.i13>
## Example 1, learn everything set.seed(123) sim <- simTVP(theta = c(0.2, 0, 0), beta_mean = c(1.5, -0.3, 0)) data <- sim$data res <- shrinkTVP(y ~ x1 + x2, data = data) # summarize output summary(res) ## Example 2, hierarchical Bayesian Lasso res <- shrinkTVP(y ~ x1 + x2, data = data, learn_a_xi = FALSE, learn_a_tau = FALSE, a_xi = 1, a_tau = 1) ## Example 3, non-hierarchical Bayesian Lasso res <- shrinkTVP(y ~ x1 + x2, data = data, learn_a_xi = FALSE, learn_a_tau = FALSE, a_xi = 1, a_tau = 1, learn_kappa2 = FALSE, learn_lambda2 = FALSE) ## Example 4, adding stochastic volatility res <- shrinkTVP(y ~ x1 + x2, data = data, sv = TRUE) ## Example 4, changing some of the default hyperparameters res <- shrinkTVP(y ~ x1 + x2, data = data, hyperprior_param = list(beta_a_xi = 5, alpha_a_xi = 10)) ## Example 5, using the triple gamma prior res <- shrinkTVP(y ~ x1 + x2, data = data, mod_type = "triple")
## Example 1, learn everything set.seed(123) sim <- simTVP(theta = c(0.2, 0, 0), beta_mean = c(1.5, -0.3, 0)) data <- sim$data res <- shrinkTVP(y ~ x1 + x2, data = data) # summarize output summary(res) ## Example 2, hierarchical Bayesian Lasso res <- shrinkTVP(y ~ x1 + x2, data = data, learn_a_xi = FALSE, learn_a_tau = FALSE, a_xi = 1, a_tau = 1) ## Example 3, non-hierarchical Bayesian Lasso res <- shrinkTVP(y ~ x1 + x2, data = data, learn_a_xi = FALSE, learn_a_tau = FALSE, a_xi = 1, a_tau = 1, learn_kappa2 = FALSE, learn_lambda2 = FALSE) ## Example 4, adding stochastic volatility res <- shrinkTVP(y ~ x1 + x2, data = data, sv = TRUE) ## Example 4, changing some of the default hyperparameters res <- shrinkTVP(y ~ x1 + x2, data = data, hyperprior_param = list(beta_a_xi = 5, alpha_a_xi = 10)) ## Example 5, using the triple gamma prior res <- shrinkTVP(y ~ x1 + x2, data = data, mod_type = "triple")
simTVP
generates synthetic data from a time-varying parameter model. The covariates are always
generated i.i.d. from a Normal(0,1) distribution.
simTVP( N = 200, d = 3, sv = FALSE, DTG = FALSE, sigma2 = 1, theta, beta_mean, sv_mu = 0, sv_phi = 0.98, sv_sigma2 = 0.2, a_psi = 0.5, c_psi = 2, rho = 0.9 )
simTVP( N = 200, d = 3, sv = FALSE, DTG = FALSE, sigma2 = 1, theta, beta_mean, sv_mu = 0, sv_phi = 0.98, sv_sigma2 = 0.2, a_psi = 0.5, c_psi = 2, rho = 0.9 )
N |
integer > 2. Indicates the length of the time series to be generated. The default value is 200. |
d |
positive integer. Indicates the number of covariates to simulate. The default value is 3. |
sv |
logical value. If set to |
DTG |
logical value. If set to |
sigma2 |
positive real number. Determines the variance of the errors of the observation
equation. Ignored if sv is |
theta |
(optional) vector containing positive real numbers. If supplied, these determine the variances of the
innovations of the state equation. Otherwise, the elements of |
beta_mean |
(optional) vector containing real numbers. If supplied, these determine the mean of the
initial value of the state equation. Otherwise, the elements of |
sv_mu |
real number. Determines the mean of the logarithm of the volatility. Ignored if |
sv_phi |
real number between -1 and 1. Determines the persistence of the SV process. Ignored if |
sv_sigma2 |
positive, real number. Determines the variance of the innovations of the logarithm of the volatility.
Ignored if |
a_psi |
positive, real number. Determines the pole parameter of the dynamic triple gamma process.
Ignored if |
c_psi |
positive, real number. Determines the tail parameter of the dynamic triple gamma process.
Ignored if |
rho |
real number between 0 and 1. Determines the persistence of the dynamic triple gamma process.
Ignored if |
The value returned is a list object containing:
data |
data frame that holds the simulated data. |
true_vals |
list object containing:
|
Peter Knaus [email protected]
# Generate a time series of length 300 res <- simTVP(N = 300) # Extract the generated data data <- res$data # Now with stochastic volatility res_sv <- simTVP(N = 300, sv = TRUE) # Now with dynamic triple gamma process res_DTG <- simTVP(N = 300, DTG = TRUE, c_psi = 1)
# Generate a time series of length 300 res <- simTVP(N = 300) # Extract the generated data data <- res$data # Now with stochastic volatility res_sv <- simTVP(N = 300, sv = TRUE) # Now with dynamic triple gamma process res_DTG <- simTVP(N = 300, DTG = TRUE, c_psi = 1)
shrinkTVP
with minimal overheadupdateTVP
draws a single sample from the joint posterior distribution of the parameters of a time-varying
parameter model with shrinkage potentially including stochastic volatility (SV). It performs no input checks and must
therefore be used with caution. It is designed to be used in a modular fashion within other samplers, where speed is important.
As such, no draws are saved and must be stored manually if the user wants to analyze them further.
updateTVP( y, x, curr_draws, mod_type = "double", learn_a_xi = TRUE, learn_a_tau = TRUE, a_xi = 0.1, a_tau = 0.1, learn_c_xi = TRUE, learn_c_tau = TRUE, c_xi = 0.1, c_tau = 0.1, a_eq_c_xi = FALSE, a_eq_c_tau = FALSE, learn_kappa2_B = TRUE, learn_lambda2_B = TRUE, kappa2_B = 20, lambda2_B = 20, hyperprior_param, sv = FALSE, sv_param, MH_tuning )
updateTVP( y, x, curr_draws, mod_type = "double", learn_a_xi = TRUE, learn_a_tau = TRUE, a_xi = 0.1, a_tau = 0.1, learn_c_xi = TRUE, learn_c_tau = TRUE, c_xi = 0.1, c_tau = 0.1, a_eq_c_xi = FALSE, a_eq_c_tau = FALSE, learn_kappa2_B = TRUE, learn_lambda2_B = TRUE, kappa2_B = 20, lambda2_B = 20, hyperprior_param, sv = FALSE, sv_param, MH_tuning )
y |
vector of length N containing the response variable. |
x |
matrix of dimension Nxd containing the covariates. |
curr_draws |
named list containing all the current draws from the joint posterior of the parameters. Not all values are required for all model setups. The following elements can be supplied:
|
mod_type |
character string that reads either |
learn_a_xi |
logical value indicating whether to learn a_xi, the spike parameter of the state variances.
Ignored if |
learn_a_tau |
logical value indicating whether to learn a_tau, the spike parameter of the mean of the
initial values of the states. Ignored if |
a_xi |
positive, real number, indicating the (fixed) value for a_xi. Ignored if
|
a_tau |
positive, real number, indicating the (fixed) value for a_tau. Ignored if
|
learn_c_xi |
logical value indicating whether to learn c_xi, the tail parameter of the state variances.
Ignored if |
learn_c_tau |
logical value indicating whether to learn c_tau, the tail parameter of the mean of the
initial values of the states. Ignored if |
c_xi |
positive, real number, indicating the (fixed) value for c_xi. Ignored if
|
c_tau |
positive, real number, indicating the (fixed) value for c_tau. Ignored if
|
a_eq_c_xi |
logical value indicating whether to force |
a_eq_c_tau |
logical value indicating whether to force |
learn_kappa2_B |
logical value indicating whether to learn kappa2_B, the global level of shrinkage for
the state variances. The default value is |
learn_lambda2_B |
logical value indicating whether to learn the lambda2_B parameter,
the global level of shrinkage for the mean of the initial values of the states. The default value is |
kappa2_B |
positive, real number, indicating the (fixed) value for kappa2_B. Ignored if
|
lambda2_B |
positive, real number, indicating the (fixed) value for lambda2_B. Ignored if
|
hyperprior_param |
optional named list containing hyperparameter values. Not all have to be supplied, with those missing being replaced by the default values. Any list elements that are misnamed will be ignored and a warning will be thrown. All hyperparameter values have to be positive, real numbers. The following hyperparameters can be supplied:
|
sv |
logical value indicating whether to use stochastic volatility for the error of the observation
equation. For details please see |
sv_param |
optional named list containing hyperparameter values for the stochastic volatility
parameters. Not all have to be supplied, with those missing being replaced by the default values.
Any list elements that are misnamed will be ignored and a warning will be thrown. Ignored if
|
MH_tuning |
optional named list containing values used to tune the MH steps for
|
The value returned is a named list object which can be immediately used as the curr_draws
argument
for another draw from the posterior with updateTVP
. Note that, depending on the model setup, some elements may be matrices of dimension zero.
It contains the following elements:
beta_st |
dx(N + 1) matrix containing the current draw from the posterior distribution of beta. |
beta_mean_st |
dx1 matrix containing the current draws from the posterior distribution of beta_mean. |
theta_sr_st |
dx1 matrix containing the current draws from the posterior distribution of the square root of theta. |
tau2_st |
dx1 matrix containing the current draws from the posterior distribution of tau2. |
xi2_st |
dx1 matrix containing the current draws from the posterior distribution of xi2. |
lambda2_st |
dx1 matrix containing the current draws from the posterior distribution of lambda2. |
kappa2_st |
dx1 matrix containing the current draws from the posterior distribution of kappa2. |
a_xi_st |
number representing the current draw from the posterior distribution of a_xi. |
a_tau_st |
number representing the current draw from the posterior distribution of a_tau. |
c_xi_st |
number representing the current draw from the posterior distribution of c_xi. |
c_tau_st |
number representing the current draw from the posterior distribution of c_tau. |
lambda2_B_st |
number representing the current draw from the posterior distribution of lambda2_B. |
kappa2_B_st |
|
sigma2_st |
number if |
C0_st |
number representing the current draw from the posterior distribution of C0. |
sv_mu_st |
number representing the current draw from the posterior distribution of the mu parameter for the stochastic volatility model on the errors. |
sv_phi_st |
number representing the current draw from the posterior distribution of the phi parameter for the stochastic volatility model on the errors. |
sv_sigma2_st |
number representing the current draw from the posterior distribution of the sigma2 parameter for the stochastic volatility model on the errors. |
h0_st |
number representing the current draw from the posterior distribution of the h0 parameter for the stochastic volatility model on the errors. |
internals |
|
Peter Knaus [email protected]
Bitto, A., & Frühwirth-Schnatter, S. (2019). "Achieving shrinkage in a time-varying parameter model framework." Journal of Econometrics, 210(1), 75-97. <doi:10.1016/j.jeconom.2018.11.006>
Cadonna, A., Frühwirth-Schnatter, S., & Knaus, P. (2020). "Triple the Gamma—A Unifying Shrinkage Prior for Variance and Variable Selection in Sparse State Space and TVP Models." Econometrics, 8(2), 20. <doi:10.3390/econometrics8020020>
Knaus, P., Bitto-Nemling, A., Cadonna, A., & Frühwirth-Schnatter, S. (2021) "Shrinkage in the Time-Varying Parameter Model Framework Using the R
Package shrinkTVP
."
Journal of Statistical Software 100(13), 1–32.<doi:10.18637/jss.v100.i13>
# Simulate data sim <- simTVP() y <- sim$data$y x <- as.matrix(sim$data[,2:4]) # Create starting values d <- ncol(x) curr_draws <- list(beta_mean_st = rep(0, d), theta_sr_st = rep(1, d), tau2_st = rep(1, d), xi2_st = rep(1, d), lambda2_st = rep(1, d), kappa2_B_st = 20, lambda2_B_st = 20, a_xi_st = 0.1, a_tau_st = 0.1, c_tau_st = 0.1, sv_mu_st = -10, sv_phi_st = 0.5, sv_sigma2_st = 1, C0_st = 1, sigma2_st = 1, h0_st = 0) # Run the algorithm for 1000 iterations # Note that curr_draws is always re-written and immediately re-used for (i in 1:1000){ curr_draws <- updateTVP(y, x, curr_draws) }
# Simulate data sim <- simTVP() y <- sim$data$y x <- as.matrix(sim$data[,2:4]) # Create starting values d <- ncol(x) curr_draws <- list(beta_mean_st = rep(0, d), theta_sr_st = rep(1, d), tau2_st = rep(1, d), xi2_st = rep(1, d), lambda2_st = rep(1, d), kappa2_B_st = 20, lambda2_B_st = 20, a_xi_st = 0.1, a_tau_st = 0.1, c_tau_st = 0.1, sv_mu_st = -10, sv_phi_st = 0.5, sv_sigma2_st = 1, C0_st = 1, sigma2_st = 1, h0_st = 0) # Run the algorithm for 1000 iterations # Note that curr_draws is always re-written and immediately re-used for (i in 1:1000){ curr_draws <- updateTVP(y, x, curr_draws) }