leifeld / btergm

Temporal Exponential Random Graph Models by Bootstrapped Pseudolikelihood
16 stars 10 forks source link

simulate.formula conflict between ergm and lme4 #13

Closed leifeld closed 4 years ago

leifeld commented 5 years ago

Message by Meng-Hao Li on the SOCNET mailing list on 18 June 2019:

Dear SOCNETters,

I was following Leifeld , Cranmer and Desmarais' (2018) example to replicate a btergm model. When I ran gof, I kept receiving an error message below. I wonder if anyone knows how to solve the problem.

Leifeld, Philip, Skyler J. Cranmer, and Bruce A. Desmarais. "Temporal exponential random graph models with btergm: Estimation and bootstrap confidence intervals." Journal of Statistical Software 83.6 (2018).

library("statnet") library("texreg") library("xergm")

data("alliances", package = "xergm.common")

model.1a <- btergm(allyNet ~ edges + gwesp(0, fixed = TRUE) + edgecov(LSP) + edgecov(warNet) + nodecov("polity") + nodecov("cinc") + absdiff("polity") + absdiff("cinc") + edgecov(contigMat) , R = 50, parallel = "snow", ncpus = 2)

gof.1a <- gof(model.1a, nsim = 50, statistics = c(esp, geodesic, deg))

20 networks from which simulations are drawn were provided. Error in sim[[i]] : subscript out of bounds In addition: There were 20 warnings (use warnings() to see them)

My working environment:

platform x86_64-w64-mingw32
arch x86_64
os mingw32
system x86_64, mingw32
status
major 3
minor 6.0
year 2019
month 04
day 26
svn rev 76424
language R
version.string R version 3.6.0 (2019-04-26) nickname Planting of a Tree

Thanks, Meng-Hao

-- Meng-Hao Li Graduate Research Assistant Center for Health Policy Research and Ethics George Mason University

It seems that the problem is related to the lme4 package. I posted the following reply:

One of the following two steps should fix this:

  1. Use library("btergm") instead of library("xergm")

Or

  1. Remove the lme4 package before you use btergm's gof function.

The reason is a bit complex, but I'll try to explain it briefly:

xergm is a meta-package that loads btergm, tnam, GERGM, rem, and xergm.common -- several network analysis packages my colleagues and I wrote. You are using the btergm package, but are loading all these other packages. One of those packages, tnam, depends on lme4, a package for mixed-effects modeling. lme4 contains a method for the simulate function in the methods package for "formula" objects. The ergm package, on which btergm depends, also has a simulate method for formula objects. The gof function in btergm relies on this formula method of the simulate function in the ergm package, but not in lme4. If both packages, ergm and lme4, are loaded, there are situations in which the wrong formula method of the simulate function is picked when it is used. This causes the error message you are seeing. Therefore you can fix the problem by either loading the btergm package instead of the xergm package (thereby avoiding loading tnam and hence lme4, which causes the conflict) or by removing the lme4 package so it's no longer loaded when xergm is loaded. This was not a problem until very recently, when the statnet team (the developers of the ergm package) changed the way the formula method of the simulate function is supplied in the ergm package. We reacted to these changes in the ergm package in the most recent version (in coordination with the statnet folks) by changing the code in btergm from calling simulate.formula to calling simulate to make btergm compatible with the most recent ergm version. Unfortunately, this apparently also introduced the conflict between ergm and lme4. I will look into better ways to resolve this namespace conflict.

This should probably be resolved through more targeted namespace imports.

leifeld commented 4 years ago

Opened a StackOverflow question on the topic.

Further discussion also at https://github.com/lme4/lme4/issues/566 (though the discussion seems to have gone stale).

leifeld commented 4 years ago

Looks like this has been fixed in lme4 and ergm/statnet. See https://github.com/lme4/lme4/issues/566.