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

bsm_ng

Non-Gaussian Basic Structural (Time Series) Model


Description

Constructs a non-Gaussian basic structural model with local level or local trend component, a seasonal component, and regression component (or subset of these components).

Usage

bsm_ng(
  y,
  sd_level,
  sd_slope,
  sd_seasonal,
  sd_noise,
  distribution,
  phi,
  u = 1,
  beta,
  xreg = NULL,
  period = frequency(y),
  a1,
  P1,
  C
)

Arguments

y

Vector or a ts object of observations.

sd_level

A fixed value or a prior for the standard error of the noise in level equation. See priors for details.

sd_slope

A fixed value or a prior for the standard error of the noise in slope equation. See priors for details. If missing, the slope term is omitted from the model.

sd_seasonal

A fixed value or a prior for the standard error of the noise in seasonal equation. See priors for details. If missing, the seasonal component is omitted from the model.

sd_noise

Prior for the standard error of the additional noise term. See priors for details. If missing, no additional noise term is used.

distribution

Distribution of the observed time series. Possible choices are "poisson", "binomial", "gamma", and "negative binomial".

phi

Additional parameter relating to the non-Gaussian distribution. For negative binomial distribution this is the dispersion term, for gamma distribution this is the shape parameter, and for other distributions this is ignored.

u

Constant parameter vector for non-Gaussian models. For Poisson, gamma, and negative binomial distribution, this corresponds to the offset term. For binomial, this is the number of trials.

beta

Prior for the regression coefficients.

xreg

Matrix containing covariates.

period

Length of the seasonal component i.e. the number of observations per season. Default is frequency(y).

a1

Prior means for the initial states (level, slope, seasonals). Defaults to vector of zeros.

P1

Prior covariance for the initial states (level, slope, seasonals). Default is diagonal matrix with 1e5 on the diagonal.

C

Intercept terms for state equation, given as a m times n matrix.

Value

Object of class bsm_ng.

Examples

model <- bsm_ng(Seatbelts[, "VanKilled"], distribution = "poisson",
  sd_level = halfnormal(0.01, 1),
  sd_seasonal = halfnormal(0.01, 1),
  beta = normal(0, 0, 10),
  xreg = Seatbelts[, "law"])
## Not run: 
set.seed(123)
mcmc_out <- run_mcmc(model, iter = 5000, particles = 10)
mcmc_out$acceptance_rate
theta <- expand_sample(mcmc_out, "theta")
plot(theta)
summary(theta)

library("ggplot2")
ggplot(as.data.frame(theta[,1:2]), aes(x = sd_level, y = sd_seasonal)) +
  geom_point() + stat_density2d(aes(fill = ..level.., alpha = ..level..),
  geom = "polygon") + scale_fill_continuous(low = "green", high = "blue") +
  guides(alpha = "none")
  
# Traceplot using as.data.frame method for MCMC output:
library("dplyr")
as.data.frame(mcmc_out) %>% 
  filter(variable == "sd_level") %>% 
  ggplot(aes(y = value, x = iter)) + geom_line()

## End(Not run)

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.