Running a national level single-country model

Load your library


Load the data

cleaned_natdata <- get_data(national=TRUE, local=TRUE, mycountry="Nepal")

Get the JAGS model inputs and the cleaned data

pkg_data <- get_modelinputs(startyear=1990, endyear=2025.5, nsegments=12, 
                            raw_data = cleaned_natdata)


Run JAGS model and get posterior point estimates with uncertainty

mod <- run_jags_model(jagsdata = pkg_data, jagsparams = NULL, 
                      n_iter = 50, n_burnin = 1, n_thin = 2)
                      # n_iter = 50000, n_burnin = 10000, n_thin = 20)

Check the model diagnostics

We use this to evaluate the convergence of the model parameters. We should expect to see R-hat values of approximately 1.05. The plot function will give you a visual summary for each parameter monitored.



Using the ggplot2 and tidybayes R packages, we will check the trace plots to assess the convergence of individual parameters. We expect to see a ‘caterpillar’ like appearance of the chains over the iterations.

sample_draws <- tidybayes::tidy_draws(mod$JAGS$BUGSoutput$sims.matrix)

var <- sample_draws %>% dplyr::select(.chain, .iteration, .draw,`P[1,2,1]`) %>%
  dplyr::mutate(chain = rep(1:2, each=mod$JAGS$BUGSoutput$n.keep)) %>%
  dplyr::mutate(iteration = rep(1:mod$JAGS$BUGSoutput$n.keep, 2))

ggplot2::ggplot(data=var) +
  ggplot2::geom_line(ggplot2::aes(x=iteration, y=`P[1,2,1]`, color=as.factor(chain)))

Plot posterior point estimates with uncertainty

plots <- plot_estimates(jagsdata = pkg_data, model_output = mod) 


Pull out estimates that you are particularly interested in

estimates_2018 <- pull_estimates(model_output = mod, country = cleaned_natdata$args$mycountry, year=2018)


Review the complete posterior sample of estimated method-supply shares

This function will allow you to pull out the posterior sample of estimated method supply shares. The posterior sample will be of size ‘nposterior’. Note that ‘nposterior’ should not be larger than your total iterations (given in ‘run_jags_model’) In this example, we supply the JAGS model object and the JAGS input data to the function, we set ‘nposterior=4’ to pull out 4 posterior samples.

post_samps <- get_posterior_P_samps(jagsdata = pkg_data, model_output = mod, nposterior=4)