Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

run_mcmc_g

Bayesian Inference of Linear-Gaussian State Space Models


Description

Bayesian Inference of Linear-Gaussian State Space Models

Usage

## S3 method for class 'gaussian'
run_mcmc(
  model,
  iter,
  output_type = "full",
  burnin = floor(iter/2),
  thin = 1,
  gamma = 2/3,
  target_acceptance = 0.234,
  S,
  end_adaptive_phase = FALSE,
  threads = 1,
  seed = sample(.Machine$integer.max, size = 1),
  ...
)

Arguments

model

Model model.

iter

Number of MCMC iterations.

output_type

Type of output. Default is "full", which returns samples from the posterior p(α, θ). Option "summary" does not simulate states directly but computes the posterior means and variances of states using fast Kalman smoothing. This is slightly faster, more memory efficient and more accurate than calculations based on simulation smoother. Using option "theta" will only return samples from the marginal posterior of the hyperparameters θ.

burnin

Length of the burn-in period which is disregarded from the results. Defaults to iter / 2. Note that all MCMC algorithms of bssm used adaptive MCMC during the burn-in period in order to find good proposal.

thin

Thinning rate. All MCMC algorithms in bssm use the jump chain representation, and the thinning is applied to these blocks. Defaults to 1.

gamma

Tuning parameter for the adaptation of RAM algorithm. Must be between 0 and 1 (not checked).

target_acceptance

Target acceptance rate for MCMC. Defaults to 0.234.

S

Initial value for the lower triangular matrix of RAM algorithm, so that the covariance matrix of the Gaussian proposal distribution is SS'. Note that for some parameters (currently the standard deviation and dispersion parameters of bsm_lg models) the sampling is done for transformed parameters with internal_theta = log(theta).

end_adaptive_phase

If TRUE, S is held fixed after the burnin period. Default is FALSE.

threads

Number of threads for state simulation. The default is 1.

seed

Seed for the random number generator.

...

Ignored.

References

Vihola, M, Helske, J, Franks, J. Importance sampling type estimators based on approximate marginal Markov chain Monte Carlo. Scand J Statist. 2020; 1– 38. https://doi.org/10.1111/sjos.12492

Examples

model <- ar1_lg(LakeHuron, rho = uniform(0.5,-1,1), 
  sigma = halfnormal(1, 10), mu = normal(500, 500, 500), 
  sd_y = halfnormal(1, 10))

mcmc_results <- run_mcmc(model, iter = 2e4)
summary(mcmc_results, return_se = TRUE)

require("dplyr")
sumr <- as.data.frame(mcmc_results, variable = "states") %>%
  group_by(time) %>%
  summarise(mean = mean(value), 
    lwr = quantile(value, 0.025), 
    upr = quantile(value, 0.975))
require("ggplot2")
sumr %>% ggplot(aes(time, mean)) + 
  geom_ribbon(aes(ymin = lwr, ymax = upr),alpha=0.25) + 
  geom_line() + theme_bw() +
  geom_point(data = data.frame(mean = LakeHuron, time = time(LakeHuron)),
    col = 2)

bssm

Bayesian Inference of Non-Linear and Non-Gaussian State Space Models

v1.1.4
GPL (>= 2)
Authors
Jouni Helske [aut, cre] (<https://orcid.org/0000-0001-7130-793X>), Matti Vihola [aut] (<https://orcid.org/0000-0002-8041-7222>)
Initial release
2021-04-13

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.