| MCMCintervention {MCMCpack} | R Documentation |
This function generates a sample from the posterior distribution of a linear Gaussian model with multiple changepoints. The function uses the Markov chain Monte Carlo method of Chib (1998). The user supplies data and priors, and a sample from the posterior distribution is returned as an mcmc object, which can be subsequently analyzed with functions provided in the coda package.
MCMCintervention(y, data=parent.frame(), m = 1, intervention,
prediction.type=c("trend","ar"), change.type=c("fixed", "random", "all"),
b0 = 0, B0 = 0, c0 = 0.001, d0 = 0.001, sigma.mu = NA, sigma.var = NA,
a = NULL, b = NULL, mcmc = 1000, burnin = 1000, thin = 1, verbose = 0,
seed = NA, beta.start = NA, P.start = NA,
marginal.likelihood = c("none", "Chib95"), ...)
y |
data. |
data |
Data frame. |
m |
The number of changepoints. |
intervention |
The timing of intervention measured by its place in the response vector. It should be larger than 0 and smaller than the length of the response vector. No default value is provided. |
prediction.type |
The type of local process. "trend" denotes the linear trend model and "ar" denotes AR(1) process. By default, MCMCintervention uses the linear trend model. |
change.type |
The tyep of parameteric breaks. "all" denotes that all parameters have breaks, "fixed" denotes that only the intercept and the slope have breaks, and "random" denotes that only the variance has breaks. By default, MCMCintervetnion assumes that all parameters have breaks. |
b0 |
The prior mean of beta. This can either be a scalar or a column vector with dimension equal to the number of betas. If this takes a scalar value, then that value will serve as the prior mean for all of the betas. |
B0 |
The prior precision of beta. This can either be a scalar or a square matrix with dimensions equal to the number of betas. If this takes a scalar value, then that value times an identity matrix serves as the prior precision of beta. Default value of 0 is equivalent to an improper uniform prior for beta. |
c0 |
c0/2 is the shape parameter for the inverse Gamma prior on sigma^2 (the variance of the disturbances). The amount of information in the inverse Gamma prior is something like that from c0 pseudo-observations. |
d0 |
d0/2 is the scale parameter for the inverse Gamma prior on sigma^2 (the variance of the disturbances). In constructing the inverse Gamma prior, d0 acts like the sum of squared errors from the c0 pseudo-observations. |
sigma.mu |
The mean of the inverse Gamma prior on sigma^2. sigma.mu and sigma.var allow users to choose the inverse Gamma prior by choosing its mean and variance. |
sigma.var |
The variacne of the inverse Gamma prior on sigma^2. sigma.mu and sigma.var allow users to choose the inverse Gamma prior by choosing its mean and variance. |
a |
a is the shape1 beta prior for transition probabilities. By default, the expected duration is computed and corresponding a and b values are assigned. The expected duration is the sample period divided by the number of states. |
b |
b is the shape2 beta prior for transition probabilities. By default, the expected duration is computed and corresponding a and b values are assigned. The expected duration is the sample period divided by the number of states. |
burnin |
The number of burn-in iterations for the sampler. |
mcmc |
The number of MCMC iterations after burnin. |
thin |
The thinning interval used in the simulation. The number of MCMC iterations must be divisible by this value. |
verbose |
A switch which determines whether or not the progress of
the sampler is printed to the screen. If |
seed |
The seed for the random number generator. If NA, the Mersenne
Twister generator is used with default seed 12345; if an integer is
passed it is used to seed the Mersenne twister. The user can also
pass a list of length two to use the L'Ecuyer random number generator,
which is suitable for parallel computation. The first element of the
list is the L'Ecuyer seed, which is a vector of length six or NA (if NA
a default seed of |
beta.start |
The starting values for the beta vector. This can either be a scalar or a column vector with dimension equal to the number of betas. The default value of of NA will use the MLE estimate of beta as the starting value. If this is a scalar, that value will serve as the starting value mean for all of the betas. |
P.start |
The starting values for the transition matrix.
A user should provide a square matrix with dimension equal to the number of states.
By default, draws from the |
marginal.likelihood |
How should the marginal likelihood be
calculated? Options are: |
... |
further arguments to be passed |
MCMCintervention simulates from the posterior distribution of
a binary model with multiple changepoints.
The model takes the following form:
y_t = x_t'beta_i + I(s_t = i)epsilon_t, i = 1,...,k.
Where k is the number of states and I(s_t = i) is an indicator function that becomes 1 when a state at t is i and otherwise 0.
The errors are assumed to be Gaussian in each regime:
I(s_t = i)epsilon_t ~ N(0, sigma^2_i)
We assume standard, semi-conjugate priors:
beta_i ~ N(b0,B0^(-1)), i = 1,...,k.
And:
sigma^(-2)_i ~ Gamma(c0/2, d0/2), i = 1,...,k.
Where beta_i and sigma^(-2)_i are assumed a priori independent.
The simulation proper is done in compiled C++ code to maximize efficiency.
An mcmc object that contains the posterior sample. This
object can be summarized by functions provided by the coda package.
The object contains an attribute prob.state storage matrix that contains the probability of state_i for each period, the
log-likelihood of the model (loglike), and
the log-marginal likelihood of the model (logmarglike).
Jong Hee Park. 2012. "A Change-point Approach to Intervention Analysis Using Bayesian Inference" Presented at the 2012 Annual Meeting of Korean Statistical Society.
Siddhartha Chib. 1998. "Estimation and comparison of multiple change-point models." Journal of Econometrics. 86: 221-241.
## Not run:
Nile.std <- (Nile - mean(Nile))/sd(Nile)
set.seed(1973)
b0 <- matrix(c(0, 0), 2, 1);
B0 <- diag(rep(0.25, 2))
c0 = 2; d0 = 1
## Model Comparison
ar0 <- MCMCintervention(Nile.std, m=0, prediction.type="ar", change.type = "all",
b0=b0, B0=B0, c0=c0, d0=d0, mcmc = 1000, burnin = 1000, verbose = 500,
intervention = 29, marginal.likelihood = "Chib95")
ar1all <- MCMCintervention(Nile.std, m=1, prediction.type="ar", change.type = "all",
b0=b0, B0=B0, c0=c0, d0=d0, mcmc = 1000, burnin = 1000, verbose = 500,
intervention = 29, marginal.likelihood = "Chib95")
ar1fixed <- MCMCintervention(Nile.std, m=1, prediction.type="ar", change.type = "fixed",
b0=b0, B0=B0, c0=c0, d0=d0, mcmc = 1000, burnin = 1000, verbose = 500,
intervention = 29, marginal.likelihood = "Chib95")
ar1random <- MCMCintervention(Nile.std, m=1, prediction.type="ar", change.type = "random",
b0=b0, B0=B0, c0=c0, d0=d0, mcmc = 1000, burnin = 1000, verbose = 500,
intervention = 29, marginal.likelihood = "Chib95")
tr0 <- MCMCintervention(Nile.std, m=0, prediction.type="trend", change.type = "all",
b0=b0, B0=B0, c0=c0, d0=d0, mcmc = 1000, burnin = 1000, verbose = 500,
intervention = 29, marginal.likelihood = "Chib95")
tr1all <- MCMCintervention(Nile.std, m=1, prediction.type="trend", change.type = "all",
b0=b0, B0=B0, c0=c0, d0=d0, mcmc = 1000, burnin = 1000, verbose = 500,
intervention = 29, marginal.likelihood = "Chib95")
tr1fixed <- MCMCintervention(Nile.std, m=1, prediction.type="trend", change.type = "fixed",
b0=b0, B0=B0, c0=c0, d0=d0, mcmc = 1000, burnin = 1000, verbose = 500,
intervention = 29, marginal.likelihood = "Chib95")
tr1random <- MCMCintervention(Nile.std, m=1, prediction.type="trend", change.type = "random",
b0=b0, B0=B0, c0=c0, d0=d0, mcmc = 1000, burnin = 1000, verbose = 500,
intervention = 29, marginal.likelihood = "Chib95")
BayesFactor(ar0, ar1all, ar1fixed, ar1random, tr0, tr1all, tr1fixed, tr1random)
par(mfrow=c(1,3))
plotState(ar1fixed, start=1871, main="Hidden Regime Change")
plotIntervention(ar1fixed, start=1871, main="Forward Analysis", alpha= 0.5, ylab="Nile River flow", xlab="Year")
plotIntervention(ar1fixed, forward=FALSE, start=1871, main="Backward Analysis", alpha= 0.5, ylab="Nile River flow", xlab="Year")
## End(Not run)