Skip to content

Commit

Permalink
tergm_MCMC_sample() now provides informative error messages on variou…
Browse files Browse the repository at this point in the history
…s MCMC failures; tests have been added. (Thanks to Rodrigo Anderle and Sandra Flores Alvarado for flagging the matter.)

fixes #122
  • Loading branch information
krivit committed Aug 13, 2024
1 parent 721782b commit 1916d17
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
10 changes: 9 additions & 1 deletion R/tergm.getMCMCsample.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ tergm_MCMC_sample <- function(nw, model, model.mon = NULL,

z <- tergm_MCMC_slave(state, eta.comb, control, verbose)

if(z$status)
stop(switch(z$status,
paste0("Number of edges in a simulated network exceeds the maximum set by the ", sQuote("MCMC.maxedges"), " control parameter."), # 1: MCMCDyn_TOO_MANY_EDGES
"A Metropolis-Hastings proposal has failed.", # 2: MCMCDyn_MH_FAILED
paste0("Logging of changes in the network has been requested, and the storage capacity specified by ", sQuote("MCMC.maxchanges"), " has been exceeded.") # 3: MCMCDyn_TOO_MANY_CHANGES
)
)

state <- z$state

diffedgelist<-if(control$changes) {
Expand Down Expand Up @@ -137,7 +145,7 @@ tergm_MCMC_slave <- function(state, eta, control, verbose){
as.integer(verbose),
PACKAGE="tergm")

if(z$status != 0) stop("MCMCDyn failed with error code ", z$status)
if(z$status) return(z) # If there is an error.

z$state <- update(z$state)

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/helper-CMLE.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
logit<-function(p) log(p/(1-p))
ilogit<-function(x) 1/(1+exp(-x))

o <- options(tergm.eval.loglik=FALSE)
options(tergm.eval.loglik = FALSE, useFancyQuotes = FALSE)

CMLE.tools <- new.env()

Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-simulate.R
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,16 @@ test_that("simulate.networkDynamic behaves reasonably", {
expect_equal(unname(summary(network.collapse(new_nwD_constr2, at = 20) ~ concurrent)), 0)
})

test_that("Dynamic simulation error messages are correct", {
nw <- network.initialize(100, directed = FALSE)

expect_error(simulate(nw ~ Form(~edges) + Persist(~edges), coef = c(5, 5), dynamic = TRUE, control = control.simulate.formula.tergm(MCMC.maxedges=2)),
"^Number of edges in a simulated network exceeds the maximum set by the 'MCMC.maxedges' control parameter.$")

expect_error(simulate(nw ~ Form(~edges) + Persist(~edges), coef = c(5, 5), dynamic = TRUE, control = control.simulate.formula.tergm(MCMC.maxchanges=2)),
"^Logging of changes in the network has been requested, and the storage capacity specified by 'MCMC.maxchanges' has been exceeded.$")
})

statnet.common::opttest({
test_that("simulate.tergm behaves reasonably", {
nw <- network.initialize(100, directed = FALSE)
Expand Down

0 comments on commit 1916d17

Please sign in to comment.