if (FALSE) {
set.seed(1973)
x1 <- rnorm(300, 0, 1)
true.beta <- c(-.5, .2, 1)
true.alpha <- c(.1, -1., .2)
X <- cbind(1, x1)
## set two true breaks at 100 and 200
true.phi1 <- pnorm(true.alpha[1] + x1[1:100]*true.beta[1])
true.phi2 <- pnorm(true.alpha[2] + x1[101:200]*true.beta[2])
true.phi3 <- pnorm(true.alpha[3] + x1[201:300]*true.beta[3])
## generate y
y1 <- rbinom(100, 1, true.phi1)
y2 <- rbinom(100, 1, true.phi2)
y3 <- rbinom(100, 1, true.phi3)
Y <- as.ts(c(y1, y2, y3))
## fit multiple models with a varying number of breaks
out0 <- MCMCprobitChange(formula=Y~X-1, data=parent.frame(), m=0,
mcmc=1000, burnin=1000, thin=1, verbose=1000,
b0 = 0, B0 = 0.1, a = 1, b = 1, marginal.likelihood = c("Chib95"))
out1 <- MCMCprobitChange(formula=Y~X-1, data=parent.frame(), m=1,
mcmc=1000, burnin=1000, thin=1, verbose=1000,
b0 = 0, B0 = 0.1, a = 1, b = 1, marginal.likelihood = c("Chib95"))
out2 <- MCMCprobitChange(formula=Y~X-1, data=parent.frame(), m=2,
mcmc=1000, burnin=1000, thin=1, verbose=1000,
b0 = 0, B0 = 0.1, a = 1, b = 1, marginal.likelihood = c("Chib95"))
out3 <- MCMCprobitChange(formula=Y~X-1, data=parent.frame(), m=3,
mcmc=1000, burnin=1000, thin=1, verbose=1000,
b0 = 0, B0 = 0.1, a = 1, b = 1, marginal.likelihood = c("Chib95"))
## find the most reasonable one
BayesFactor(out0, out1, out2, out3)
## draw plots using the "right" model
plotState(out2)
plotChangepoint(out2)
}
Run the code above in your browser using DataLab