statnet / ergm.ego

Fit, Simulate and Diagnose Exponential-Family Random Graph Models to Egocentrically Sampled Network Data https://statnet.org
Other
14 stars 4 forks source link

possible bug in ergm.ego #34

Closed chad-klumb closed 5 years ago

chad-klumb commented 5 years ago

While working through the tutorial I ran into a problem in an ergm.ego call. The problematic term (in this case) turned out to be nodefactor, and the issue arises when the alters have a category that the egos don't. Here's a self-contained example of the same phenomenon.

library(ergm.ego)

# set up egodata
n <- 100
e <- 150
ds <- c(10,15,5,20)
y <- network.initialize(n, directed=FALSE)
y %v% "cat1" <- sample(letters[1:4],n,replace=TRUE)
y <- san(y~edges+degree(0:3), target.stats=c(e,ds))
y.e <- as.egodata(y)

# no problem with this one
ergm.ego(y.e ~ nodefactor("cat1"))

# now add a new category to alters only
w <- which(y.e$alters$cat1 == "a")
w <- w[w %% 2 == 0]
y.e$alters$cat1[w] <- "e"

# error
ergm.ego(y.e ~ nodefactor("cat1"))

The error for me is:

Error in vector.namesmatch(target.stats, names(nw.stats)) : 
  Name missmatch in "target.stats". Specify by position.

The error appears to happen in function ergm. Debugging showed that target.stats includes the e category while nw.stats doesn't.

Browse[2]> nw.stats
nodefactor.cat1.b nodefactor.cat1.c nodefactor.cat1.d 
                0                 0                 0 
Browse[2]> target.stats
nodefactor.cat1.b nodefactor.cat1.c nodefactor.cat1.d nodefactor.cat1.e 
               84                97                51                16 

The offending line of code is

 target.stats <- vector.namesmatch(target.stats, names(nw.stats))

Would appreciate someone confirming the above error.

krivit commented 5 years ago

@chad-klumb , can you add a short test case in tests/testthat that would expect_error in the right place and of the right kind?