statnet / ergm

Fit, Simulate and Diagnose Exponential-Family Models for Networks
Other
94 stars 36 forks source link

Weird error in simulate.formula.ergm only when EpiModel loaded #477

Closed smjenness closed 1 year ago

smjenness commented 1 year ago

With the latest master of ergm and statnet.common, simulating from a formula when loading ergm is fine:

library(ergm)
nw <- network.initialize(1000, FALSE)
fit <- ergm(nw ~ edges, target.stats = 500)
sim <- simulate(fit$formula, coef = fit$coefficients, basis = fit$newnetwork)

Session Info

> sessioninfo::session_info()
─ Session info ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.2.0 (2022-04-22)
 os       macOS Monterey 12.5
 system   aarch64, darwin20
 ui       RStudio
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       America/New_York
 date     2022-07-27
 rstudio  2022.07.1+554 Spotted Wakerobin (desktop)
 pandoc   NA

─ Packages ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 package        * version      date (UTC) lib source
 cachem           1.0.6        2021-08-19 [1] CRAN (R 4.2.0)
 cli              3.3.0        2022-04-25 [1] CRAN (R 4.2.0)
 coda             0.19-4       2020-09-30 [1] CRAN (R 4.2.0)
 DEoptimR         1.0-11       2022-04-03 [1] CRAN (R 4.2.0)
 ergm           * 4.2-6962     2022-07-28 [1] Github (statnet/ergm@554b40d)
 fansi            1.0.3        2022-03-24 [1] CRAN (R 4.2.0)
 fastmap          1.1.0        2021-01-25 [1] CRAN (R 4.2.0)
 glue             1.6.2        2022-02-24 [1] CRAN (R 4.2.0)
 lattice          0.20-45      2021-09-22 [2] CRAN (R 4.2.0)
 lifecycle        1.0.1        2021-09-24 [1] CRAN (R 4.2.0)
 lpSolveAPI       5.5.2.0-17.7 2020-06-14 [1] CRAN (R 4.2.0)
 magrittr         2.0.3        2022-03-30 [1] CRAN (R 4.2.0)
 MASS             7.3-58       2022-07-14 [1] CRAN (R 4.2.0)
 Matrix           1.4-1        2022-03-23 [2] CRAN (R 4.2.0)
 memoise          2.0.1        2021-11-26 [1] CRAN (R 4.2.0)
 network        * 1.17.2       2022-05-21 [1] CRAN (R 4.2.0)
 pillar           1.8.0        2022-07-18 [1] CRAN (R 4.2.0)
 pkgconfig        2.0.3        2019-09-22 [1] CRAN (R 4.2.0)
 purrr            0.3.4        2020-04-17 [1] CRAN (R 4.2.0)
 rlang            1.0.4        2022-07-12 [1] CRAN (R 4.2.0)
 rle              0.9.2        2020-09-25 [1] CRAN (R 4.2.0)
 robustbase       0.95-0       2022-04-02 [1] CRAN (R 4.2.0)
 rstudioapi       0.13         2020-11-12 [1] CRAN (R 4.2.0)
 sessioninfo      1.2.2        2021-12-06 [1] CRAN (R 4.2.0)
 statnet.common   4.7.0-403    2022-07-28 [1] Github (statnet/statnet.common@8a7a61b)
 tibble           3.1.8        2022-07-22 [1] CRAN (R 4.2.0)
 trust            0.1-8        2020-01-10 [1] CRAN (R 4.2.0)
 utf8             1.2.2        2021-07-24 [1] CRAN (R 4.2.0)
 vctrs            0.4.1        2022-04-13 [1] CRAN (R 4.2.0)

But then loading EpiModel, and running the same code generates an error:

library(EpiModel)
nw <- network.initialize(1000, FALSE)
fit <- ergm(nw ~ edges, target.stats = 500)
sim <- simulate(fit$formula, coef = fit$coefficients, basis = fit$newnetwork)

Error in ergm_proposal.NULL(constraints, arguments = if (observational) control$obs.MCMC.prop.args else control$MCMC.prop.args,  : 
  NULL passed to ergm_proposal. This may be due to passing an ergm object from an earlier version. If this is the case, please refit it with the latest version, and try again. If this is not the case, this may be a bug, so please file a bug report.

Session Info

─ Session info ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.2.0 (2022-04-22)
 os       macOS Monterey 12.5
 system   aarch64, darwin20
 ui       RStudio
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       America/New_York
 date     2022-07-27
 rstudio  2022.07.1+554 Spotted Wakerobin (desktop)
 pandoc   NA

─ Packages ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 package        * version      date (UTC) lib source
 ape              5.6-2        2022-03-02 [1] CRAN (R 4.2.0)
 cachem           1.0.6        2021-08-19 [1] CRAN (R 4.2.0)
 cli              3.3.0        2022-04-25 [1] CRAN (R 4.2.0)
 coda             0.19-4       2020-09-30 [1] CRAN (R 4.2.0)
 codetools        0.2-18       2020-11-04 [2] CRAN (R 4.2.0)
 colorspace       2.0-3        2022-02-21 [1] CRAN (R 4.2.0)
 DBI              1.1.3        2022-06-18 [1] CRAN (R 4.2.0)
 DEoptimR         1.0-11       2022-04-03 [1] CRAN (R 4.2.0)
 deSolve        * 1.33         2022-07-16 [1] CRAN (R 4.2.0)
 doParallel       1.0.17       2022-02-07 [1] CRAN (R 4.2.0)
 dplyr            1.0.9        2022-04-28 [1] CRAN (R 4.2.0)
 EpiModel       * 2.3.0        2022-07-19 [1] CRAN (R 4.2.0)
 ergm           * 4.2-6962     2022-07-28 [1] Github (statnet/ergm@554b40d)
 fansi            1.0.3        2022-03-24 [1] CRAN (R 4.2.0)
 fastmap          1.1.0        2021-01-25 [1] CRAN (R 4.2.0)
 foreach          1.5.2        2022-02-02 [1] CRAN (R 4.2.0)
 generics         0.1.3        2022-07-05 [1] CRAN (R 4.2.0)
 ggplot2          3.3.6        2022-05-03 [1] CRAN (R 4.2.0)
 glue             1.6.2        2022-02-24 [1] CRAN (R 4.2.0)
 gtable           0.3.0        2019-03-25 [1] CRAN (R 4.2.0)
 iterators        1.0.14       2022-02-05 [1] CRAN (R 4.2.0)
 lattice          0.20-45      2021-09-22 [2] CRAN (R 4.2.0)
 lazyeval         0.2.2        2019-03-15 [1] CRAN (R 4.2.0)
 lifecycle        1.0.1        2021-09-24 [1] CRAN (R 4.2.0)
 lpSolveAPI       5.5.2.0-17.7 2020-06-14 [1] CRAN (R 4.2.0)
 magrittr         2.0.3        2022-03-30 [1] CRAN (R 4.2.0)
 MASS             7.3-58       2022-07-14 [1] CRAN (R 4.2.0)
 Matrix           1.4-1        2022-03-23 [2] CRAN (R 4.2.0)
 memoise          2.0.1        2021-11-26 [1] CRAN (R 4.2.0)
 munsell          0.5.0        2018-06-12 [1] CRAN (R 4.2.0)
 network        * 1.17.2       2022-05-21 [1] CRAN (R 4.2.0)
 networkDynamic * 0.11.2       2022-05-05 [1] CRAN (R 4.2.0)
 nlme             3.1-158      2022-06-15 [1] CRAN (R 4.2.0)
 pillar           1.8.0        2022-07-18 [1] CRAN (R 4.2.0)
 pkgconfig        2.0.3        2019-09-22 [1] CRAN (R 4.2.0)
 purrr            0.3.4        2020-04-17 [1] CRAN (R 4.2.0)
 R6               2.5.1        2021-08-19 [1] CRAN (R 4.2.0)
 RColorBrewer     1.1-3        2022-04-03 [1] CRAN (R 4.2.0)
 Rcpp             1.0.9        2022-07-08 [1] CRAN (R 4.2.0)
 rlang            1.0.4        2022-07-12 [1] CRAN (R 4.2.0)
 rle              0.9.2        2020-09-25 [1] CRAN (R 4.2.0)
 robustbase       0.95-0       2022-04-02 [1] CRAN (R 4.2.0)
 rstudioapi       0.13         2020-11-12 [1] CRAN (R 4.2.0)
 scales           1.2.0        2022-04-13 [1] CRAN (R 4.2.0)
 sessioninfo      1.2.2        2021-12-06 [1] CRAN (R 4.2.0)
 statnet.common * 4.7.0-403    2022-07-28 [1] Github (statnet/statnet.common@8a7a61b)
 tergm          * 4.1.0        2022-06-22 [1] CRAN (R 4.2.0)
 tibble           3.1.8        2022-07-22 [1] CRAN (R 4.2.0)
 tidyselect       1.1.2        2022-02-21 [1] CRAN (R 4.2.0)
 trust            0.1-8        2020-01-10 [1] CRAN (R 4.2.0)
 utf8             1.2.2        2021-07-24 [1] CRAN (R 4.2.0)
 vctrs            0.4.1        2022-04-13 [1] CRAN (R 4.2.0)
smjenness commented 1 year ago

cc: @adrienleguillou

krivit commented 1 year ago

Can you try reinstalling tergm from source as well? If that fixes it, I'll explain.

smjenness commented 1 year ago

Yes, that solved it. Updated with the CRAN version of tergm with:

install.packages("tergm", type = "source")

Interested to know why...

smjenness commented 1 year ago

However, now, a new error message pops up. Here is an example with EpiModel code, I will try to isolate with a MRE with ergm only.

nw <- network_initialize(n = 100)
nw <- set_vertex_attribute(nw, "group", rep(1:2, each = 50))
formation <- ~edges
target.stats <- 50
coef.diss <- dissolution_coefs(dissolution = ~offset(edges), duration = 20)
est1 <- netest(nw, formation, target.stats, coef.diss, verbose = FALSE)

# Epidemic model
param <- param.net(inf.prob = 0.3, inf.prob.g2 = 0.15)
init <- init.net(i.num = 10, i.num.g2 = 10)
control <- control.net(type = "SI", nsteps = 100, nsims = 5, verbose.int = 0)
mod1 <- netsim(est1, param, init, control)

Error:

Starting Network Simulation...Error in select_ergm_proposal(object, class = class, ref = reference,  : 
  The combination of class (t), model constraints and hints (‘.’, ‘.attributes’, ‘discord’, and ‘sparse’), reference measure ("Bernoulli"), proposal weighting (default), and conjunctions and disjunctions is not implemented. Check your arguments for typos. 
smjenness commented 1 year ago

Here is an MRE with ergm/tergm code only:

nw <- network.initialize(n = 100, FALSE)

x <- ergm(nw ~ edges, target.stats = 50)
x$coefficients[1] <- x$coefficients - 2.94 # edges dissolution approx

nw <- simulate(x$formula,
               coef = x$coefficients,
               basis = x$newnetwork,
               dynamic = FALSE)

sim <- simulate(nw ~ Form(x$formula) + Persist(~offset(edges)),
                coef = c(x$coefficients, 2.94),
                time.slices = 1,
                time.start = 0,
                time.offset = 1,
                nsim = 1,
                dynamic = TRUE)

Same error as above.

smjenness commented 1 year ago

Note that I tested this against both CRAN and Github master versions of tergm, same error.

krivit commented 1 year ago

Yes, that solved it. Updated with the CRAN version of tergm with: Interested to know why...

Long story short, for technical reasons, tergm grabs a copy of ergm's simulate_formula.network() method at build time, before overriding it with its own. Unfortunately, it means that if ergm's copy of simulate_formula.network() gets updated without rebuilding tergm, it may be calling the old version, causing an error.

I'll look into the new issue tomorrow.

krivit commented 1 year ago

Small correction to the MWE:

library(tergm)
nw <- network.initialize(n = 100, FALSE)

x <- ergm(nw ~ edges, target.stats = 50)
x$coefficients[1] <- x$coefficients - 2.94 # edges dissolution approx

nw <- simulate(x$formula,
               coef = x$coefficients,
               basis = x$newnetwork,
               dynamic = FALSE)

sim <- simulate(nw ~ Form(x$formula) + Persist(~offset(edges)),
                coef = c(x$coefficients, 2.94),
                time.slices = 1,
                time.start = 0,
                time.offset = 1,
                nsim = 1,
                dynamic = TRUE)
smjenness commented 1 year ago

I can confirm this resolved the issue. Thanks @krivit!