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

summary(egodata~...) does not work when ergm.ego is loaded but not attached. #52

Closed martinamorris closed 3 years ago

martinamorris commented 3 years ago

not sure if this is related to the ergm::ergm issue. this is with ergm.ego 0.6

ergm.ego::summary(c.egodata ~ meandeg, scaleto = fit_casl$ppopsize)
Error: 'summary' is not an exported object from 'namespace:ergm.ego'
krivit commented 3 years ago

summary(c.egodata ~ meandeg, scaleto = fit_casl$ppopsize) doesn't work?

mbojan commented 3 years ago

I don't think it is related. This does not work because there is no summary in ergm.ego. The actual S3 dispatch is base::summary() -> ergm:::summary.formula() -> ergm::summary_formula() -> ergm.ego:::summary_formula.egodata(). I'm not sure if it is possible without attaching ergm.ego.

mbojan commented 3 years ago
data(faux.mesa.high, package="ergm")
fmh <- ergm.ego::as.egodata(faux.mesa.high)
summary(fmh ~ edges)
# Error in EgoStat.edges(egodata = list(egos = list(vertex.names = 1:205,  :
#  could not find function "EgoStat.edges"

The compiled code not loading @krivit ?

martinamorris commented 3 years ago

What @mbojan said.

But even after attaching, it doesn't work:

d> summary(c.egodata ~ meandeg, scaleto = fit_casl$ppopsize)
Error in EgoStat.meandeg(egodata = list(egos = list(if_PART5 = c(NA, 1,  : 
  could not find function "EgoStat.meandeg"

ergm 3.11, ergm.ego 0.6

krivit commented 3 years ago

Per the error message, that's because there is no meandeg EgoStat. Mean degree is not an egocentric statistic: its ego's contribution depends on the network size, which an ego cannot observe.

mbojan commented 3 years ago

Still, the edges does not work with package not attached.

I (untested and speculative) think that in:

https://github.com/statnet/ergm.ego/blob/0eefec9e1e73adcaf775c49c6829c369c32d1387/R/summary.statistics.egodata.R#L85

the terms are evaluated in the environment of the formula. If the formula is created with the package not attached, R can't find these functions in none of the enclosing environments of the formula environment.

krivit commented 3 years ago

The meandeg case is working as intended. The problem arises when the package is loaded but not attached.

mbojan commented 3 years ago

Yep, exactly.

krivit commented 3 years ago

We need to use the term lookup mechanism from ergm, which can handle this situation. (The relevant utilities are exported.)

martinamorris commented 3 years ago

Per the error message, that's because there is no meandeg EgoStat. Mean degree is not an egocentric statistic: its ego's contribution depends on the network size, which an ego cannot observe.

Right. But if our assumption is (as it has been) that meandeg is the size invariant parameterization, can you remind me why we can't estimate it from egocentric data?

I'm trying to track down the source of the 25% difference in the observed and simulated meandegs for the casl network (as we've been discussing, report online here), and they track back to the way the network is scaled up in ergm.ego -- it doesn't preserve the mean degree for either the fit$network, or the fit$newnetwork. Both of those have the same mean degree as sims from the model, so there's consistency after scaleup -- the problem emerges in the scaleup itself.

krivit commented 3 years ago

We can calculate it, but it doesn't scale like an egocentric statistic. We may be able to add the term (and tag it appropriately to keep it from confusing the inferential machinery). In the meantime, summary(egodata~edges, scaleto=2) should do the trick (since m = 2 * e / n).

martinamorris commented 3 years ago

Yep, but the post-scale up edges for all nets are consistent with the scaled up target stats from the summary function. So the divergence between the weighted egodata and the scaled up network is happening somewhere in that scaleup.

It's not consistent (the other nets don't show this large discrepancy), and I don't think it's a weighting problem (the other nets are also weighted, and the weights are generally moderate (with a handful of outliers).

d> summary(c.egodata$egoWt)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.6273  0.8056  0.9476  1.0000  1.1050 11.7128 

So I'm at a dead end here.

krivit commented 3 years ago

OK, it looks like the term locator is not actually exported from ergm. That complicates things.

krivit commented 3 years ago

OK, that fixes the problem of not being able to find the EgoStat.* when package not attached. The downside is that we need to release another update to statnet.common before pushing this.

I'll start a separate ticket to discuss how the meandeg egostat should be evaluated, if at all.

krivit commented 3 years ago

Using the mean.age statistic as a model, it turned out that EgoStat.meandeg was easy enough that it's not worth opening another ticket.