# NOT RUN {
# Neal (2000) model and data
nealData <- c(-1.48, -1.40, -1.16, -1.08, -1.02, 0.14, 0.51, 0.53, 0.78)
mkLogPosteriorPredictiveDensity <- function(data = nealData,
sigma2 = 0.1^2,
mu0 = 0,
sigma02 = 1) {
function(i, subset) {
posteriorVariance <- 1 / ( 1/sigma02 + length(subset)/sigma2 )
posteriorMean <- posteriorVariance * ( mu0/sigma02 + sum(data[subset])/sigma2 )
posteriorPredictiveSD <- sqrt(posteriorVariance + sigma2)
dnorm(data[i], posteriorMean, posteriorPredictiveSD, log = TRUE)
}
}
logPostPredict <- mkLogPosteriorPredictiveDensity()
nSamples <- 1000L
partitions <- matrix(0, nrow = nSamples, ncol = length(nealData))
accept <- 0
for ( i in 2:nSamples ) {
ms <- restrictedGibbsMergeSplit(partitions[i-1,],
logPostPredict,
t = 1,
mass = 1.0,
nUpdates = 2)
partitions[i,] <- ms$partition
accept <- accept + ms$accept
}
accept / nSamples # M-H acceptance rate
# convergence and mixing diagnostics
nSubsets <- apply(partitions, 1, function(x) length(unique(x)))
mean(nSubsets)
sum(acf(nSubsets)$acf)-1 # Autocorrelation time
entropy <- apply(partitions, 1, partitionEntropy)
plot.ts(entropy)
# }
Run the code above in your browser using DataLab