bayes_dsge: Estimate a DSGE Model by Bayesian Methods

View source: R/bayes-estimate.R

bayes_dsgeR Documentation

Estimate a DSGE Model by Bayesian Methods

Description

Estimates the parameters of a DSGE model using Random-Walk Metropolis-Hastings (RWMH) with adaptive proposal covariance. Supports both linear models (dsge_model) and nonlinear models (dsgenl_model). For nonlinear models, the steady state is re-solved and the model re-linearized at each candidate parameter vector.

Usage

bayes_dsge(
  model,
  data,
  priors,
  chains = 2L,
  iter = 5000L,
  warmup = floor(iter/2),
  thin = 1L,
  proposal_scale = 0.1,
  demean = TRUE,
  seed = NULL,
  n_cores = 1L,
  endogenous_prior = NULL,
  presample = 0L,
  shock_start = NULL
)

Arguments

model

A dsge_model or dsgenl_model object, or a Dynare model imported with read_dynare().

data

A data frame, matrix, or ts object containing the observed variables.

priors

Named list of dsge_prior objects (one per free parameter). For a model imported with read_dynare(), defaults to the priors translated from its estimated_params block. Shock standard deviations get default inv_gamma(0.01, 0.01) priors unless overridden (names: "sd_e.shock_name").

chains

Integer. Number of MCMC chains. Default is 2.

iter

Integer. Total iterations per chain (warmup + sampling). Default is 5000.

warmup

Integer. Number of warmup iterations. Default is floor(iter / 2).

thin

Integer. Thinning interval. Default is 1.

proposal_scale

Numeric. Initial proposal standard deviation scale. Default is 0.1.

demean

Logical. If TRUE (default), observed variables are demeaned before estimation. For nonlinear models, demeaning is not applied because the Kalman filter operates around the parameter-specific steady state.

seed

Integer. Random seed for reproducibility. If NULL, no seed is set.

n_cores

Integer. Number of CPU cores for parallel chain execution. Set to 1 (default) for sequential execution. Values greater than chains are silently clamped to chains. On Windows a PSOCK cluster is used; on POSIX systems (mclapply is used instead. Parallel execution requires the dsge package to be installed (not just loaded via devtools::load_all()).

endogenous_prior

Optional dsge_endog_prior object returned by endogenous_prior(). If supplied, its log density is added to the parameter log-prior at every MH evaluation. Default NULL (no endogenous prior, identical to the original behaviour).

presample

Integer. Number of initial observations used only to initialise the Kalman filter and excluded from the likelihood (as Dynare's presample option). Default 0; for a model imported with read_dynare(), the value in the file's estimation command.

shock_start

Optional named numeric vector of starting values for the shock standard deviations (names as the shocks). Defaults to the standard deviation of the data; for a model imported with read_dynare(), the shock standard deviations from the file.

Details

The sampler operates in unconstrained space with appropriate transformations (log for positive parameters, logit for unit-interval parameters) and Jacobian corrections. The proposal covariance is adapted during warmup to target approximately 25\

Chains are initialized by drawing from the prior. If any draw yields a non-finite log-posterior, the starting values are jittered until a valid point is found.

For nonlinear models (dsgenl_model), each posterior evaluation involves: (1) solving the deterministic steady state at the candidate parameters, (2) computing a first-order linearization, (3) solving the resulting linear system, and (4) evaluating the Kalman filter likelihood. If any stage fails (e.g., steady-state non-convergence, Blanchard-Kahn violation), the proposal is safely rejected. This is computationally more expensive than linear Bayesian estimation.

Parallel chains

When n_cores > 1 each chain runs in its own R worker process, so wall-clock time scales roughly as chains / min(n_cores, chains). Results are numerically identical to sequential execution given the same per-chain seeds. The parallel package (part of base R) is used; no additional installation is required.

Value

An object of class "dsge_bayes" containing posterior draws and diagnostics. For nonlinear models, the result also includes solve_failures, the number of parameter draws where steady-state, linearization, or solution failed.

Examples


m <- dsge_model(
  obs(y ~ z),
  state(z ~ rho * z),
  start = list(rho = 0.5)
)

set.seed(42)
z <- numeric(200); for (i in 2:200) z[i] <- 0.8 * z[i-1] + rnorm(1)
dat <- data.frame(y = z)

fit <- bayes_dsge(m, data = dat,
                  priors = list(rho = prior("beta", shape1 = 2, shape2 = 2)),
                  chains = 2, iter = 2000, seed = 1)
summary(fit)



dsge documentation built on Sept. 25, 2026, 5:08 p.m.