Learn R Programming

bssm (version 0.1.11)

ng_bsm: 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

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

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 observation. Possible choices are "poisson" and "binomial".

phi

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

u

Constant parameter for non-Gaussian models. For Poisson 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.

state_intercept

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

Value

Object of class ng_bsm.

Examples

Run this code
# NOT RUN {
model <- ng_bsm(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, n_iter = 5000, nsim = 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")

# }

Run the code above in your browser using DataLab