statnet / ergm

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

Unloading a package containing proposals should remove its entries from the proposal table #497

Closed krivit closed 1 year ago

krivit commented 1 year ago

We currently have no way of removing entries from ergm_proposal_table(), which should happen whenever an extension package is unloaded. This leads to the following:

suppressPackageStartupMessages(library(ergm))
data(florentine)
flomarriage %e% "w" <- 0

dummy <- simulate(flomarriage~sum, response="w", reference=~DiscUnif(0,5), verbose=TRUE,
                  control=snctrl(MCMC.burnin=0, MCMC.interval=1))
#> Initializing unconstrained Metropolis-Hastings proposal: Best valid proposal 'DiscUnif' cannot take into account hint(s) 'sparse'.
#> 'ergm:MH_DiscUnif'.
#> Initializing model...
#> Model initialized.
#> Warning in simulate.ergm_model(m, nsim = nsim, seed = seed, coef = coef, : No
#> parameter values given, using Bernouli network.
#> Starting MCMC iterations to generate 1 network

suppressPackageStartupMessages(library(ergm.count))
dummy <- simulate(flomarriage~sum, response="w", reference=~DiscUnif(0,5), verbose=TRUE,
                  control=snctrl(MCMC.burnin=0, MCMC.interval=1))
#> Initializing unconstrained Metropolis-Hastings proposal: 'ergm.count:MH_DiscTNT'.
#> Initializing model...
#> Model initialized.
#> Warning in simulate.ergm_model(m, nsim = nsim, seed = seed, coef = coef, : No
#> parameter values given, using Bernouli network.
#> Starting MCMC iterations to generate 1 network

unloadNamespace("ergm.count")
dummy <- simulate(flomarriage~sum, response="w", reference=~DiscUnif(0,5), verbose=TRUE,
                  control=snctrl(MCMC.burnin=0, MCMC.interval=1))
#> Initializing unconstrained Metropolis-Hastings proposal:
#> Error: Metropolis-Hastings proposal 'DiscTNT' function 'InitWtErgmProposal.DiscTNT' not found.

Created on 2022-11-21 with reprex v2.0.2

It may be possible to autodetect which package is calling ergm_proposal_table(), so that we could watch for that package unloading, but I wonder if a cleaner API is also possible.

krivit commented 1 year ago

A shorter test case:

library(ergm.count)
expect_true("DiscTNT" %in% ergm_proposal_table()$Proposal)
unloadNamespace("ergm.count")
expect_false("DiscTNT" %in% ergm_proposal_table()$Proposal)