### dis_data comes loaded with the package
### We set B = 100 to obtain 100 posterior samples, you probably want to run it
### longer for say, B = 100000, but B = 100 runs fast for illustrative purposes
### and passing CRAN checks
B <- 100
tp <- seq(10, 80, 10) # Time points
burnin <- B * 0.1 # A 10% burn-in
thin <- 10 # Keep every 10th sample, i.e., thinning
post <- hgp(dis_data, locs = tp, B = B, n_interp = 100)
### Example: Removing burn-in and then thinning the posterior samples for the covariance parameters
### and then plotting the chains
phi <- post$mcmc_chains$cov_pars$phi[-c(1:burnin)]
phi <- phi[seq(1, (B-burnin), thin)]
psi <- post$mcmc_chains$cov_pars$psi[-c(1:burnin)]
psi <- psi[seq(1, (B-burnin), thin)]
sigma_R <- post$mcmc_chains$cov_pars$sigma_R[-c(1:burnin)]
sigma_R <- sigma_R[seq(1, (B-burnin), thin)]
sigma_T <- post$mcmc_chains$cov_pars$sigma_T[-c(1:burnin)]
sigma_T <- sigma_T[seq(1, (B-burnin), thin)]
tau <- post$mcmc_chains$cov_pars$tau[-c(1:burnin)]
tau <- tau[seq(1, (B-burnin), thin)]
chains <- data.frame( # Data frame holding posterior samples
samples = rep(1:((B-burnin)/thin), times = 5),
parameter = rep(c("phi", "psi", "sigma_R", "sigma_T", "tau"),
each = (B-burnin)/thin),
values = c(phi, psi, sigma_R, sigma_T, tau))
chains$parameter <- factor(chains$parameter,
labels = c(expression(phi),
expression(psi),
expression(sigma[R]),
expression(sigma[T]),
expression(tau)))
ggplot2::ggplot(chains, ggplot2::aes(samples, values)) +
ggplot2::geom_line() +
ggplot2::labs(x = "Iterations", y = "Posterior Sample Values") +
ggplot2::facet_wrap(~parameter, scales = "free",
labeller = ggplot2::label_parsed) +
ggplot2::theme(text = ggplot2::element_text(size = 22))
ggplot2::ggplot(chains, ggplot2::aes(values)) +
ggplot2::geom_density() +
ggplot2::labs(x = "Values", y = "Posterior Density") +
ggplot2::facet_wrap(~parameter, scales = "free",
labeller = ggplot2::label_parsed) +
ggplot2::theme(text = ggplot2::element_text(size = 22))
### Plotting the predicted dissolution profiles
dissplot(dis_data, tp)
grid <- sort(c(tp, seq(min(tp), max(tp), length = 100)))
grid1 <- (1:B)[-(1:burnin)][seq(1, (B-burnin), thin)]
grid2 <- ((B+1):(2*B))[-(1:burnin)][seq(1, (B-burnin), thin)]
lines(grid, apply(post$mcmc_chains$mu_pars[,grid1], 1, mean),
col = "gray65", lwd = 2)
lines(grid, apply(post$mcmc_chains$mu_pars[,grid2], 1, mean),
col = "black", lwd = 2)
lower <- apply(post$mcmc_chains$mu_pars[,grid1], 1,
quantile, prob = 0.025)
upper <- apply(post$mcmc_chains$mu_pars[,grid1], 1,
quantile, prob = 0.975)
polygon(c(grid, rev(grid)), c(lower, rev(upper)),
col = scales::alpha("gray65",.2), border = NA)
lower <- apply(post$mcmc_chains$mu_pars[,grid2], 1,
quantile, prob = 0.025)
upper <- apply(post$mcmc_chains$mu_pars[,grid2], 1,
quantile, prob = 0.975)
polygon(c(grid, rev(grid)), c(lower, rev(upper)),
col = scales::alpha("black",.2), border = NA)
### If we want to calculate the Pr(f2 > 50 & delta < 15)
prob <- sum(post$mcmc_chains$f2[grid1] > 50 &
post$mcmc_chains$delta[grid1] < 15) / ((B - burnin)/thin)
Run the code above in your browser using DataLab