# Illustrate that fast_spike_slab_beta is a faster way to compute the same results as
# general_sequence_model on the beta-binomial prior
# Generate data
n <- 500 # sample size
n_signal <- 25 # number of non-zero theta
A <- 5 # signal strength
theta <- c(rep(A,n_signal), rep(0,n-n_signal))
x <- theta + rnorm(n, sd=1)
# Choose slab
slab <- "Cauchy"
Cauchy_gamma <- 1
cat("Running fast_spike_slab_beta (fast for very large n)...\n")
res_fss <- fast_spike_slab_beta(x, sigma=1, slab=slab, Cauchy_gamma=Cauchy_gamma)
cat("Running general_sequence_model (slower for very large n)...\n")
res_gsm <- general_sequence_model(x, sigma=1, slab=slab,
log_prior="beta-binomial", Cauchy_gamma=Cauchy_gamma)
cat("Maximum difference in marginal posterior slab probabilities:",
max(abs(res_gsm$postprobs - res_fss$postprobs)))
cat("\nMaximum difference in posterior means:",
max(abs(res_gsm$postmean - res_fss$postmean)), "\n")
# Plot means
M=max(abs(x))+1
plot(1:n, x, pch=20, ylim=c(-M,M), col='green', xlab="", ylab="",
main="Posterior Means (Same for Both Methods)")
points(1:n, theta, pch=20, col='blue')
points(1:n, res_gsm$postmean, pch=20, col='black', cex=0.6)
points(1:n, res_fss$postmean, pch=20, col='magenta', cex=0.6)
legend("topright", legend=c("general_sequence_model", "fast_spike_slab_beta",
"data", "truth"),
col=c("black", "magenta", "green", "blue"), pch=20, cex=0.7)
Run the code above in your browser using DataLab