SPIFA Urban Ipixuna MCMC chains: Diagnostics


Visualise MCMC chains and assess convergence:

  • Traceplots of randomly selected items
    • Full model with 3GP
  • Convergence measure
    • Full model with 3GP
    • Dry model with 3GP
    • Wet model with 3GP

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_1 <- readRDS(file.path(path_modelled, "spifa-ipixuna-urban-1gp.rds"))
samples_2 <- readRDS(file.path(path_modelled, "spifa-ipixuna-urban-2gp.rds"))
samples_3 <- readRDS(file.path(path_modelled, "spifa-ipixuna-urban-3gp.rds"))

iter <- nrow(samples_1[["theta"]])

MCMC convergence for spifa models

diag_labels <- c("rhat" = "hat(R)", "ess_bulk" = "ESS~Bulk", "ess_tail" = "ESS~Tail")

out_1 <- spifa_diagnostics(samples_1, burnin = iter / 5, thin = 1) |>
    pivot_longer(rhat:ess_tail) |>
    mutate(name = factor(name, levels = names(diag_labels), labels = diag_labels))

out_2 <- spifa_diagnostics(samples_3, burnin = iter / 5, thin = 1) |>
    pivot_longer(rhat:ess_tail) |>
    mutate(name = factor(name, levels = names(diag_labels), labels = diag_labels))

out_3 <- spifa_diagnostics(samples_2, burnin = iter / 5, thin = 1) |>
    pivot_longer(rhat:ess_tail) |>
    mutate(name = factor(name, levels = names(diag_labels), labels = diag_labels))

ggthemr::ggthemr_reset()
pal_aux <- ggthemr:::load_palette.character("fresh")
ggthemr::ggthemr(pal_aux, layout = "clear")

bind_rows(out_1, out_2, out_3, .id = "model") |>
    mutate(model = factor(model, levels = 1:3, labels = c("SPIFA~I", "SPIFA~II", "SPIFA~III"))) |>
    ggplot() +
    geom_histogram(aes(value), color = 1, linewidth = rel(0.1), bins = 50) +
    facet_grid(model ~ name, scales = "free", labeller = label_parsed) +
    theme_bw() +
    labs(x = "Value", y = "Frequency")
#> Warning: Removed 324 rows containing non-finite outside the scale range (`stat_bin()`).

ggsave(file.path(path_fig, "mcmc-diagnostics-full-spifas.pdf"), width = 7, height = 6)
#> Warning: Removed 324 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 
#>  61.536   1.303  61.962