# This example can be pasted into a script or copied into R to run. It
# takes a few minutes, but illustrates how the code can be used
data(IsraelPalestineConflict)
# Find the mode of an msbvar model
# Initial guess is based on random draw, so set seed.
set.seed(123)
xm <- msbvar(y=IsraelPalestineConflict, p=1, h=2,
lambda0=0.8, lambda1=0.15,
lambda3=2, lambda4=1, lambda5=0, mu5=0,
mu6=0, qm=12,
alpha.prior=matrix(c(5,2,2,10), 2, 2))
# Plot out the initial mode
plot(ts(xm$fp))
print(xm$Q)
# Now sample the posterior
N1 <- 100
N2 <- 500
# First, so this with random permutation sampling
x1 <- gibbs.msbvar(xm, N1=N1, N2=N2, permute=TRUE)
# Since the sample was permuted, we need to cluster the posterior
# to see what identifies the h! posterior modes
Q.clus <- kmeans(x1$Q.sample, centers=2)
# Look at the modes
print(Q.clus$centers)
# We need to translate these into identification restrictions on the
# intercepts or the variances. Here's how we can extract these from the
# posterior:
m <- ncol(IsraelPalestineConflict)
h <- x1$h
p <- 1
intercept.indices <- seq(m*p + 1, by = m+1, length=m*h)
# Extract the intercept and variance coefficients from the posterior
# sample
intercepts <- x1$Beta.sample[,intercept.indices]
intercepts <- rbind(intercepts[,1:2], intercepts[,3:4])
colnames(intercepts) <- colnames(IsraelPalestineConflict)
# Extract out the variance elements
tmp <- (rep(c(1,m:2),h))
variance.indices <- tmp
for(i in 2:(m*h)) variance.indices[i] <- variance.indices[i-1] + tmp[i]
Sigma <- x1$Sigma.sample[,variance.indices]
Sigma <- rbind(Sigma[,1:2], Sigma[,3:4])
colnames(Sigma) <- colnames(IsraelPalestineConflict)
# Make a vector of the indicators for the colors
indicator <- rep(Q.clus$cluster, h)
# Here's how to plot those based on the posterior clustering above.
pairs(intercepts, pch=".", col=indicator)
pairs(Sigma, pch=".", col=indicator)
# Now sample, clustering on the intercepts of the first equation. To see
# what the index is for this, look at the output of the mode:
print(xm$hreg$Bk)
x2 <- gibbs.msbvar(xm, N1=N1, N2=N2, permute=FALSE, Beta.idx=c(3,1))
# Plot the regime probabilities
plot.SS(x2)
# Nicer plot with some labeling
plot(ts(mean.SS(x2), start=c(1979,15), freq=52))
# Look at the clustering of the intercepts for the identified model
intercepts2 <- x2$Beta.sample[,intercept.indices]
# Identified posterior modes
summary(intercepts2)
# So the first regime is high conflict (negative values) and the second
# regime is low conflict (closer to positive values):
pairs(rbind(intercepts2[,1:2], intercepts2[,3:4]),
col=c(rep(1,N2), rep(2, N2)))Run the code above in your browser using DataLab