## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7,
  fig.height = 4.5
)

## ----setup--------------------------------------------------------------------
library(ProfileGLMM)

## ----preprocess---------------------------------------------------------------
data("exposure_data")
exp_data <- exposure_data$df
theta0 <- exposure_data$theta0

covList <- list(
  FE = c("X"),
  RE = c("t"),
  REunit = c("indiv"),
  Lat = c("X"),
  Assign = list(Cont = c("Exp1", "Exp2"), Cat = NULL),
  Y = c("Y")
)

dataProfile <- profileGLMM_preprocess(
  regType = "linear",
  covList = covList,
  dataframe = exp_data,
  nC = 30,
  intercept = list(FE = TRUE, RE = FALSE, Lat = TRUE)
)

## ----run-chains---------------------------------------------------------------
set.seed(1)
nIt <- 400
nBurnIn <- 100

# Chain 1
dataProfile$theta$betaFE
MCMC_Obj <- profileGLMM_Gibbs(
  model = dataProfile,
  nIt = nIt,
  nBurnIn = nBurnIn
)

# Chain 2 (fresh initialization from prior)
dataProfile$theta <- theta_init(dataProfile$prior, dataProfile$params)
dataProfile$theta$betaFE
MCMC_Obj2 <- profileGLMM_Gibbs(
  model = dataProfile,
  nIt = nIt,
  nBurnIn = nBurnIn
)

# Chain 3 (fresh initialization from prior)
dataProfile$theta <- theta_init(dataProfile$prior, dataProfile$params)
dataProfile$theta$betaFE
MCMC_Obj3 <- profileGLMM_Gibbs(
  model = dataProfile,
  nIt = nIt,
  nBurnIn = nBurnIn
)

## ----trace-plot-beta----------------------------------------------------------
y_range <- range(
  MCMC_Obj$beta[1, ],
  MCMC_Obj2$beta[1, ],
  MCMC_Obj3$beta[1, ]
)

plot(
  MCMC_Obj$beta[1, ],
  type = "l",
  ylim = y_range,
  xlab = "Post-burn-in iteration",
  ylab = "FE intercept",
  main = "Line trace plot across three chains"
)
lines(MCMC_Obj2$beta[1, ], col = "red")
lines(MCMC_Obj3$beta[1, ], col = "blue")
legend(
  "topright",
  legend = c("Chain 1", "Chain 2", "Chain 3"),
  col = c("black", "red", "blue"),
  lty = 1,
  bty = "n"
)

## ----trace-plot-zeta----------------------------------------------------------
y_range <- range(
  MCMC_Obj$zeta,
  MCMC_Obj2$zeta,
  MCMC_Obj3$zeta
)

plot(
  MCMC_Obj$zeta,
  type = "l",
  ylim = y_range,
  xlab = "Post-burn-in iteration",
  ylab = "zeta",
  main = "Line trace plot across three chains"
)
plot(
  MCMC_Obj$zeta,
  type = "l",
  ylim = y_range,
  xlab = "Post-burn-in iteration",
  ylab = "zeta",
  main = "Line trace plot across three chains"
)
lines(MCMC_Obj2$zeta, col = "red")
lines(MCMC_Obj3$zeta, col = "blue")
legend(
  "topright",
  legend = c("Chain 1", "Chain 2", "Chain 3"),
  col = c("black", "red", "blue"),
  lty = 1,
  bty = "n"
)

## ----diagnostics, eval = requireNamespace("posterior", quietly = TRUE)--------
library(posterior)

# preparing the draws in posterior format (3D array draws x chains x params )
n_draws = nIt-nBurnIn # nb of draws per chain
n_chains = 3 # nb of chains
param_names = list('FE_int', 'FE_slope','Zeta') # parameter we want to check the convergence

draws_arr <- array(
  0,
  dim = c(n_draws, 
          n_chains, # number of chains
          length(param_names)  # Fe intercept and slope plus zeta 
          ),
  dimnames = list(
    iteration = seq_len(n_draws),
    chain = paste0("chain_", seq_len(n_chains)),
    variable = param_names
  )
)
draws_arr[, 1, ] = cbind(t(MCMC_Obj$beta), MCMC_Obj$zeta)
draws_arr[, 2, ] = cbind(t(MCMC_Obj2$beta), MCMC_Obj2$zeta)
draws_arr[, 3, ] = cbind(t(MCMC_Obj3$beta), MCMC_Obj3$zeta)

beta_draws <- as_draws_array(draws_arr)
summarise_draws(beta_draws, "mean", "rhat", "ess_bulk", "ess_tail")

## ----combine-chains-----------------------------------------------------------
combined_mcmc <- combine_chains(list(MCMC_Obj, MCMC_Obj2, MCMC_Obj3),3)

#post_single <- profileGLMM_postProcess(MCMC_Obj)
#post_combined <- profileGLMM_postProcess(combined_mcmc)

#summary(post_single)
#summary(post_combined)

