knitr::opts_chunk$set(echo = TRUE)
library(mcmsupply) set.seed(1209)
cleaned_data <- get_data(national=FALSE, local=TRUE, mycountry="Nepal")
pkg_data <- get_modelinputs(startyear=1990, endyear=2025.5, nsegments=12, raw_data = cleaned_data)
For speed and illustration purposes, we will use 10 iterations, with no burn in period and taking every third sample. This leaves only 9 samples. We DO NOT recommend this setting. The recommended settings are 40000 iterations, with 10000 burn in period and taking every 15th sample. This is commented out and listed underneath the below R code.
mod <- run_jags_model(jagsdata = pkg_data, jagsparams = NULL, n_iter = 10, n_burnin = 1, n_thin = 3) # n_iter = 40000, n_burnin = 10000, n_thin = 15)
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.
sample_draws <- tidybayes::tidy_draws(mod$JAGS$BUGSoutput$sims.matrix) var <- sample_draws %>% dplyr::select(.chain, .iteration, .draw,`P[1,2,1,1]`) %>% dplyr::mutate(chain = 1, # rep(1:mod$JAGS$BUGSoutput$n.chains, each=mod$JAGS$BUGSoutput$n.sims)), iteration = 3) # rep(1:mod$JAGS$BUGSoutput$n.sims, mod$JAGS$BUGSoutput$n.chains)) ggplot2::ggplot(data=var) + ggplot2::geom_line(ggplot2::aes(x=iteration, y=`P[1,2,1,1]`, color=as.factor(chain)))
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.
head(mod$JAGS$BUGSoutput$summary)
plots <- plot_estimates(jagsdata = pkg_data, model_output = mod) plots[[1]]
estimates_2018 <- pull_estimates(model_output = mod, country = 'Nepal', year=2018) head(estimates_2018)
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.