statnet / ergm

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

Enabling the constraint combination: blockdiag("gid")+edges #538

Closed benrosche closed 7 months ago

benrosche commented 1 year ago

~ Feature request ~

Dear Pavel / ergm developers,

As discussed at Sunbelt 2023, I would like to request the following constraint combination:

ergm(..., constraints=~blockdiag("gid")+edges) or ergm(..., constraints=~blockdiag("gid")+(o)degrees )

to model multiple networks while keeping the overall number of edges constant.

When using ergms to simulate networks, keeping the number of edges constant is often necessary to draw valid comparisons between network states. When modeling multiple networks, this is currently only possible with the following hack:

ergm(g~... + offset(F(~edges, ~!nodematch("gid"))), offset.coef=-Inf, constraints=~edges)

ergm.multi does not solve this problem because

ergm(Networks(gs) ~... , constraints=~edges)

is not possible either.

Thank you very much and best wishes,

Ben Rosche

krivit commented 1 year ago

Thanks for the request. I am currently testing a proposal that will do that, but I want to check something: do you want to constrain the number of edges overall or within each block? The former should be in master tonight, but the latter is a taller order.

benrosche commented 1 year ago

Hi Pavel, I'd be happy with either. Within blocks would be great to have at some point but it's not urgent. Thank you so much! I'll update the package tomorrow then! Ben

Benjamin Rosche PhD candidate in Sociology at Cornell University benrosche.com

Sent from my phone. Terse, odd spellings, apologies.

On Tue, Jul 4, 2023, 6:20 PM Pavel N. Krivitsky @.***> wrote:

Thanks for the request. I am currently testing a proposal that will do that, but I want to check something: do you want to constrain the number of edges overall or within each block? The former should be in master tonight, but the latter is a taller order.

— Reply to this email directly, view it on GitHub https://github.com/statnet/ergm/issues/538#issuecomment-1620793759, or unsubscribe https://github.com/notifications/unsubscribe-auth/AG3M3NX6KNGVEQIPXC4UIP3XOSJJDANCNFSM6AAAAAAZ6DUSYM . You are receiving this because you authored the thread.Message ID: @.***>

krivit commented 1 year ago

Merged in in add1887606fb3f2dd78465f59b0a8de554804e58 (for full-network edge constraint).

benrosche commented 12 months ago

Dear Pavel / ergm developers, I tried implementing the new constraint combination ergm(...,constraints=~blockdiag("gid")+edges) now with actual (larger) data. Unfortunately, the RStudio crashes when model parameters need to be fitted as soon as I add +edges (both with CD and MLE algorithms). Simulating networks with ergm(...,constraints=~blockdiag("gid")+edges) works, however. Fitting a model with the constraint combination produces the following error message. Alas, I cannot provide a reproducible example because the data I'm using (Add Health) is on a secured server. I wonder whether you have come across this error message before and whether this could be due to the ergm package or more likely due to the server I'm working on.

krivit commented 12 months ago

That's unfortunate. Can you try finding a minimal example, say stripping network attributes off?

benrosche commented 11 months ago

It took a long time but I was finally authorized to create fake data that resembles the structure of the Add Health friendship networks. I am able to replicate the error outside the secure server. This model crashes on my Dell XPS 9370:

library(dplyr)
library(igraph)
library(intergraph)
library(ergm)

nd <- read.csv(file="nd.txt") %>% relocate(id)
ed <- read.csv(file="ed.txt") %>% relocate(id_from, id_to)

g <- 
  graph_from_data_frame(
    d=ed, 
    vertices=nd, 
    directed = T
  ) %>% 
  asNetwork(., multiple=F) 

g.ergm <- 
  ergm(
    g ~
      edges+mutual,
    estimate="MLE", 
    constraints=~blockdiag("gid")+edges,
    control=control.ergm(
      seed=1,
      MCMLE.maxit = 20
    ))

The data files can be downloaded here.

benrosche commented 9 months ago

Dear Pavel,

I wanted to follow up one more time regarding the new constraint combination ergm(...,constraints=~blockdiag("gid")+edges).

Unfortunately, R crashes when both blockdiag() and edges are specified. This happens regardless of whether an ergm model is fitted using the ergm function or networks are simulated using the simulate function.

This code reproduces the error:

library(dplyr)
library(igraph)
library(intergraph)
library(ergm)

nd <- read.csv(file="nd.txt") %>% relocate(id)
ed <- read.csv(file="ed.txt") %>% relocate(id_from, id_to)

g <- 
  graph_from_data_frame(
    d=ed, 
    vertices=nd, 
    directed = T
  ) %>% 
  asNetwork(., multiple=F) 

g.ergm <- 
  ergm(
    g ~
      edges+mutual,
    estimate="MLE", 
    constraints=~blockdiag("gid")+edges,
    control=control.ergm(
      seed=1,
      MCMLE.maxit = 20
    ))

The data files can be downloaded here.

I'd appreciate it very much if you could have quick look.

benrosche commented 7 months ago

I appreciate it.

Any chance you could enable this feature for ergm.multi? When using the edges (or (o)degrees) constraint with ergm.multi, it responds that blockdiag + edges is not supported.

Thank you,

Benjamin Rosche PhD candidate in Sociology at Cornell University benrosche.com

Sent from my phone. Terse, odd spellings, apologies.

On Sat, Dec 9, 2023, 3:05 AM Pavel N. Krivitsky @.***> wrote:

Closed #538 https://github.com/statnet/ergm/issues/538 as completed via 1ef5543 https://github.com/statnet/ergm/commit/1ef5543f5bc93c622d16bdc2b4d00fd3022b2129 .

— Reply to this email directly, view it on GitHub https://github.com/statnet/ergm/issues/538#event-11199945080, or unsubscribe https://github.com/notifications/unsubscribe-auth/AG3M3NTUXRMTY45YHZVP4CTYIQLWFAVCNFSM6AAAAAAZ6DUSYOVHI2DSMVQWIX3LMV45UABCJFZXG5LFIV3GK3TUJZXXI2LGNFRWC5DJN5XDWMJRGE4TSOJUGUYDQMA . You are receiving this because you authored the thread.Message ID: @.***>

krivit commented 7 months ago

I appreciate it. Any chance you could enable this feature for ergm.multi? When using the edges (or (o)degrees) constraint with ergm.multi, it responds that blockdiag + edges is not supported.

Odd. Can I trouble you to please open a new ticket with test code?

benrosche commented 6 months ago

I tried to replicate the error just now and ergm.multi with constraint=~edges works. My bad!