MCMCpoissonChange  R Documentation 
This function generates a sample from the posterior distribution of a Poisson regression 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.
MCMCpoissonChange(
formula,
data = parent.frame(),
m = 1,
b0 = 0,
B0 = 1,
a = NULL,
b = NULL,
c0 = NA,
d0 = NA,
lambda.mu = NA,
lambda.var = NA,
burnin = 1000,
mcmc = 1000,
thin = 1,
verbose = 0,
seed = NA,
beta.start = NA,
P.start = NA,
marginal.likelihood = c("none", "Chib95"),
...
)
formula 
Model formula. 
data 
Data frame. 
m 
The number of changepoints. 
b0 
The prior mean of 
B0 
The prior precision of 
a 

b 

c0 

d0 

lambda.mu 
The mean of the Gamma prior on 
lambda.var 
The variacne of the Gamma prior on 
burnin 
The number of burnin 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, current R system seed is used. 
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 NA will use draws from the Uniform distribution with the same boundary with the data as the starting value. If this is a scalar, that value will serve as the starting value mean for all of the betas. When there is no covariate, the log value of means should be used. 
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 
MCMCpoissonChange
simulates from the posterior distribution of a
Poisson regression model with multiple changepoints using the methods of
Chib (1998) and FruhwirthSchnatter and Wagner (2006). The details of the
model are discussed in Park (2010).
The model takes the following form:
y_t \sim \mathcal{P}oisson(\mu_t)
\mu_t = x_t ' \beta_m,\;\; m = 1, \ldots, M
Where
M
is the number of states and \beta_m
is paramters
when a state is m
at t
.
We assume Gaussian distribution for prior of \beta
:
\beta_m \sim \mathcal{N}(b_0,B_0^{1}),\;\; m = 1, \ldots, M
And:
p_{mm} \sim \mathcal{B}eta(a, b),\;\; m = 1, \ldots, M
Where M
is the number of states.
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, and the logmarginal
likelihood of the model (logmarglike
).
Jong Hee Park. 2010. “Structural Change in the U.S. Presidents' Use of Force Abroad.” American Journal of Political Science 54: 766782. <doi:10.1111/j.15405907.2010.00459.x>
Sylvia FruhwirthSchnatter and Helga Wagner 2006. “Auxiliary Mixture Sampling for Parameterdriven Models of Time Series of Counts with Applications to State Space Modelling.” Biometrika. 93:827–841.
Siddhartha Chib. 1998. “Estimation and comparison of multiple changepoint models.” Journal of Econometrics. 86: 221241. <doi: 10.1016/S03044076(97)001152>
Andrew D. Martin, Kevin M. Quinn, and Jong Hee Park. 2011. “MCMCpack: Markov Chain Monte Carlo in R.”, Journal of Statistical Software. 42(9): 121. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.18637/jss.v042.i09")}.
Siddhartha Chib. 1995. “Marginal Likelihood from the Gibbs Output.” Journal of the American Statistical Association. 90: 13131321. <doi: 10.1080/01621459.1995.10476635>
MCMCbinaryChange
, plotState
,
plotChangepoint
## Not run:
set.seed(11119)
n < 150
x1 < runif(n, 0, 0.5)
true.beta1 < c(1, 1)
true.beta2 < c(1, 2)
true.beta3 < c(1, 2)
## set true two breaks at (50, 100)
true.s < rep(1:3, each=n/3)
mu1 < exp(1 + x1[true.s==1]*1)
mu2 < exp(1 + x1[true.s==2]*2)
mu3 < exp(1 + x1[true.s==3]*2)
y < as.ts(c(rpois(n/3, mu1), rpois(n/3, mu2), rpois(n/3, mu3)))
formula = y ~ x1
## fit multiple models with a varying number of breaks
model0 < MCMCpoissonChange(formula, m=0,
mcmc = 1000, burnin = 1000, verbose = 500,
b0 = rep(0, 2), B0 = 1/5*diag(2), marginal.likelihood = "Chib95")
model1 < MCMCpoissonChange(formula, m=1,
mcmc = 1000, burnin = 1000, verbose = 500,
b0 = rep(0, 2), B0 = 1/5*diag(2), marginal.likelihood = "Chib95")
model2 < MCMCpoissonChange(formula, m=2,
mcmc = 1000, burnin = 1000, verbose = 500,
b0 = rep(0, 2), B0 = 1/5*diag(2), marginal.likelihood = "Chib95")
model3 < MCMCpoissonChange(formula, m=3,
mcmc = 1000, burnin = 1000, verbose = 500,
b0 = rep(0, 2), B0 = 1/5*diag(2), marginal.likelihood = "Chib95")
model4 < MCMCpoissonChange(formula, m=4,
mcmc = 1000, burnin = 1000, verbose = 500,
b0 = rep(0, 2), B0 = 1/5*diag(2), marginal.likelihood = "Chib95")
model5 < MCMCpoissonChange(formula, m=5,
mcmc = 1000, burnin = 1000, verbose = 500,
b0 = rep(0, 2), B0 = 1/5*diag(2), marginal.likelihood = "Chib95")
## find the most reasonable one
print(BayesFactor(model0, model1, model2, model3, model4, model5))
## draw plots using the "right" model
par(mfrow=c(attr(model2, "m") + 1, 1), mai=c(0.4, 0.6, 0.3, 0.05))
plotState(model2, legend.control = c(1, 0.6))
plotChangepoint(model2, verbose = TRUE, ylab="Density", start=1, overlay=TRUE)
## No covariate case
model2.1 < MCMCpoissonChange(y ~ 1, m = 2, c0 = 2, d0 = 1,
mcmc = 1000, burnin = 1000, verbose = 500,
marginal.likelihood = "Chib95")
print(BayesFactor(model2, model2.1))
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.