CIFA Urban Ipixuna MCMC chains: Traceplots and diagnostics
Visualise MCMC chains and assess convergence for CIFA model:
- Traceplots of randomly selected items
- Convergence measure
Load required libraries and data
rm(list = ls())
library(day2day)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(mirt)
#> Loading required package: stats4
#> Loading required package: lattice
library(spifa)
library(tidyr)
library(sf)
#> Linking to GEOS 3.10.2, GDAL 3.4.1, PROJ 8.2.1; sf_use_s2() is TRUE
library(purrr)
library(ggplot2)
path_main <- git_path()
path_data <- file.path(path_main, "data")
path_raw <- file.path(path_data, "raw")
path_processed <- file.path(path_data, "processed")
path_modelled <- file.path(path_data, "modelled")
path_fig <- file.path(path_data, "figures")
path_src <- file.path(path_main, "src")
source(file.path(path_src, "ggtheme-publication.R"))
source(file.path(path_src, "mcmc-traceplot-random.R"))
source(file.path(path_src, "mcmc-diagnostics.R"))
fidata <- file.path(path_processed, "fi-items-ipixuna-urban.gpkg") |>
st_read(as_tibble = TRUE)
#> Reading layer `fi-items-ipixuna-urban' from data source
#> `/home/rstudio/documents/projects/food-insecurity-mapping/data/processed/fi-items-ipixuna-urban.gpkg'
#> using driver `GPKG'
#> Simple feature collection with 200 features and 36 fields
#> Geometry type: POINT
#> Dimension: XY
#> Bounding box: xmin: -71.70038 ymin: -7.06058 xmax: -71.68109 ymax: -7.03724
#> Geodetic CRS: WGS 84
samples <- readRDS(file.path(path_modelled, "cifa-ipixuna-urban.rds"))
iter <- nrow(samples[["theta"]])
MCMC traceplots for full model
ggthemr::ggthemr_reset()
pal_aux <- ggthemr:::load_palette.character("fresh")
ggthemr::ggthemr(pal_aux, layout = "clear")
set.seed(2)
plot_mcmc(samples, "c", nshow = 3)
#> Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
#> ℹ Please use the `linewidth` argument instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.
plot_mcmc(samples, "a", nshow = 5, mat = TRUE)
set.seed(1)
plot_mcmc(samples, "theta", nshow = 5, mat = TRUE)
plot_mcmc(samples, "corr", "R", mat = TRUE)
MCMC convergence for full model, dry and wet
diag_labels <- c("rhat" = "hat(R)", "ess_bulk" = "ESS~Bulk", "ess_tail" = "ESS~Tail")
out_full <- spifa_diagnostics(samples, burnin = iter / 5, thin = 1) |>
pivot_longer(rhat:ess_tail) |>
mutate(name = factor(name, levels = names(diag_labels), labels = diag_labels))
out_full |>
ggplot() +
geom_histogram(aes(value), color = 1, linewidth = rel(0.1), bins = 50) +
facet_grid(~ name, scales = "free", labeller = label_parsed) +
theme_bw() +
theme(axis.text.x = element_text(size = rel(0.9))) +
labs(x = "Value", y = "Frequency")
#> Warning: Removed 126 rows containing non-finite outside the scale range (`stat_bin()`).
ggsave(file.path(path_fig, "mcmc-cifa-diagnostics-full.pdf"), width = 7, height = 2.7)
#> Warning: Removed 126 rows containing non-finite outside the scale range (`stat_bin()`).
Time to execute the task
Only useful when executed with Rscript
.
proc.time()
#> user system elapsed
#> 27.684 0.573 27.980