require(MASS) # load the data set
head(epil)
fit = sample_bpr( y ~ lbase*trt + lage + V4, data = epil,
iter = 1000)
summary(fit) # summary of posterior inference
mcmc_diagnostics(fit) # summary of MCMC convergence diagnostics
plot(fit)
## Examples with different options
# Select prior parameters and set tuning parameter
fit2 = sample_bpr( y ~ lbase*trt + lage + V4, data = epil,
iter = 1000,
prior = list( type = "gaussian", b = rep(0, 6),
B = diag(6) * 3 ),
pars = list( max_dist = 10 ))
# Simulate multiple chains and merge outputs after checking convergence
fit3 = sample_bpr( y ~ lbase*trt + lage + V4, data = epil,
iter = 1000,
nchains = 4, thin = 2)
# fit3 now contains additional elements ($sim2, $sim3, $sim4)
mcmc_diagnostics(fit3)
# the Gelman-Rubin diagnostics confirms convergence of the 4
# independent chains to the same stationary distribution
fit3b = merge_sim(fit3)
str(fit3b$sim)
# fit 3b contains only one MCMC chain of length 1500
# (after thinning and burn-in)
# \donttest{
## introduce more variables and use regularization
epil2 <- epil[epil$period == 1, ]
epil2["period"] <- rep(0, 59); epil2["y"] <- epil2["base"]
epil["time"] <- 1; epil2["time"] <- 4
epil2 <- rbind(epil, epil2)
epil2$pred <- unclass(epil2$trt) * (epil2$period > 0)
epil2$subject <- factor(epil2$subject)
epil3 <- aggregate(epil2, list(epil2$subject, epil2$period > 0),
function(x) if(is.numeric(x)) sum(x) else x[1])
epil3$pred <- factor(epil3$pred,
labels = c("base", "placebo", "drug"))
contrasts(epil3$pred) <- structure(contr.sdif(3),
dimnames = list(NULL, c("placebo-base", "drug-placebo")))
fit4 = sample_bpr(y ~ pred + factor(subject), data = epil3,
pars = list(max_dist = 0.3),
prior = list(type = "horseshoe", tau = 2),
iter = 3000, burnin = 1000)
summary(fit4)
mcmc_diagnostics(fit4)
plot(posterior_predictive(fit4), stats = c("mean", "sd", "max"))
# }
Run the code above in your browser using DataLab