Search code examples
rplotergm

ERGM MCMC diagnostics joint plot


How to combine MCMC diagnostic plots from two (or more) ERGM estimation in one figure as in the example below?

library(ergm)
library(latticeExtra)

data(florentine)

# Fit two ERGM models
model1 <- ergm(flomarriage ~ edges + kstar(2))
model2 <- ergm(flomarriage ~ edges + kstar(2) + triangle)

mcmc.diagnostic(model1)
mcmc.diagnostic(model2)

enter image description here


Solution

  • Obviously you can "glue" together the plots in some image-editting program. To do it in R you'll need to use coda (used by ergm internally anyway) and some lattice gymnastics, for example:

    p1 <- model1$sample |>
      coda::as.mcmc.list() |>
      ergm_plot.mcmc.list(main = "Model 1")
    
    p2 <- model2$sample |>
      coda::as.mcmc.list() |>
      ergm_plot.mcmc.list(main = "Model 2")
    
    plot(p1, split=c(1,1,2,1) )
    plot(p2, split=c(2,1,2,1), newpage = FALSE)
    

    enter image description here