statnet / ergm

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

Dyadic attributes and graph manipulation #567

Open krivit opened 5 days ago

krivit commented 5 days ago

The problem:

library(ergm)
data(florentine)
## Business as dyad covariate for marriage
flomarriage %n% "business" <- as.matrix(flobusiness)
summary(flomarriage~edgecov("business"))
#> edgecov.business 
#>                8
## Consider a subgraph excluding nodes 1 and 2.  Because the node IDs
## have shifted, the covariate matrix is now shifted relative to the
## sociomatrix:
summary(flomarriage~S(~edgecov("business"), ~3:16))
#> S(3:16)~edgecov.business 
#>                        1
## Correct value
sum((as.matrix(flomarriage) & as.matrix(flobusiness))[3:16,3:16])
#> [1] 16

Approaches

network API

Introduce the concept of a dyad attribute (%d%?) that functions such as add.nodes(), permute.vertexIDs(), and get.inducedSubgraph() understand and modify accordingly. This is probably the most seamless and elegant way. @CarterButts , what do you think?

%ergmlhs% API

It's up to the user to provide ergm() with a list of network attributes subject to adjustment. Then, something along the lines of

flomarriage %n% "business" <- as.matrix(flobusiness)
flomarriage %ergmlhs% "dyadattr" <- c("business")

could inform the S() operator that a subgraph should be taken. This could be automated by providing a %d%<-.network() method that sets the network attribute and updates the %ergmlhs% metadata.

Vertex name matching

edgecov() and dyadcov() could be more clever. Firstly, if the dyadic covariate dimension does not match the current network size, they should at the very least detect that. They could then see if, e.g., the dyadic covariate matrix has row and column names, which could then be used to map onto the current vertex names.

mbojan commented 2 days ago

Thumbs up for a network feature.