diff --git a/R/tergm.getMCMCsample.R b/R/tergm.getMCMCsample.R index 0181d9fe..4421824b 100644 --- a/R/tergm.getMCMCsample.R +++ b/R/tergm.getMCMCsample.R @@ -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) { @@ -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) diff --git a/tests/testthat/helper-CMLE.R b/tests/testthat/helper-CMLE.R index 0260a443..54a48761 100644 --- a/tests/testthat/helper-CMLE.R +++ b/tests/testthat/helper-CMLE.R @@ -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() diff --git a/tests/testthat/test-simulate.R b/tests/testthat/test-simulate.R index 02e39e7a..6ba427bb 100644 --- a/tests/testthat/test-simulate.R +++ b/tests/testthat/test-simulate.R @@ -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)