USCCANA / netdiffuseR

netdiffuseR: Analysis of Diffusion and Contagion Processes on Networks
https://USCCANA.github.io/netdiffuseR
Other
85 stars 21 forks source link

Simulation doesn't run, error unclear #34

Closed PCK1992 closed 1 year ago

PCK1992 commented 1 year ago

image

A quick description of my graph: I have a provider network built from co-prescription ties with around 8,000 nodes and 27,000 ties (low density). The network is connected. My ultimate goal is to simulate the diffusion of interventions on the network taking different structural factors into account (selecting high centrality nodes, using geographic sampling, etc.) but for now I'd like to start simple and simulate simple diffusion. Unfortunately, it doesn't work, and the error message is not really helping me figuring out how to deal with it. The intention of my code is to randomly sample from a set of seed nodes seed_nodes <- as.numeric(sample(V(net_all)$name, 500)) and model the diffusion in the network.

Anyone any idea what I'm doing wrong?

gvegayon commented 1 year ago

What is the structure of net_adj? Is it a list of sparse matrices, or a diffnet object? Make sure the network is the proper type.

PCK1992 commented 1 year ago

net_adj is created by taking my igraph network object and extracting a matrix using the as_adj() command.

net_adj <-as_adj(igraph_object , type = c("upper"), attr = "weight",
                                edges = FALSE, names = TRUE, sparse = igraph_opt("sparsematrices"))
gvegayon commented 1 year ago

Not sure what the problem could be. What is the seed_nodes object you are passing to the function? Here is a full reproducible example:

# Loading the packages
library(netdiffuseR)
#> 
#> Attaching package: 'netdiffuseR'
#> The following object is masked from 'package:base':
#> 
#>     %*%
library(igraph)
#> 
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#> 
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#> 
#>     union

# The UK faculty network
data(UKfaculty, package = "igraphdata")

# Getting tj
net_adj <-as_adj(
  UKfaculty , 
  type   = c("upper"),
  attr   = "weight",
  edges  = FALSE,  # There's a warning from igraph saying this arg is deprecated
  names  = TRUE,
  sparse = igraph_opt("sparsematrices")
  )
#> Warning in as_adj(UKfaculty, type = c("upper"), attr = "weight", edges = FALSE,
#> : The `edges` argument of `as_adjacency_matrix` is deprecated; it will be
#> removed in igraph 1.4.0

# It is a simulation, so we need to make sure it is reproducible
set.seed(12313)
seed_nodes <- sample.int(nrow(net_adj), 10)

# Running OK...
testdiff <- rdiffnet(
  seed.graph = net_adj,
  t          = 10,
  rewire     = FALSE,
  seed.nodes = seed_nodes,
  stop.no.diff = TRUE
)

# How it looks like
testdiff
#> Dynamic network of class -diffnet-
#>  Name               : A diffusion network
#>  Behavior           : Random contagion
#>  # of nodes         : 81 (1, 2, 3, 4, 5, 6, 7, 8, ...)
#>  # of time periods  : 10 (1 - 10)
#>  Type               : directed
#>  Final prevalence   : 0.73
#>  Static attributes  : real_threshold (1)
#>  Dynamic attributes : -
plot_diffnet(testdiff)


sessionInfo()
#> R version 4.3.0 (2023-04-21)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 22.04.2 LTS
#> 
#> Matrix products: default
#> BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0 
#> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
#> 
#> locale:
#>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
#>  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
#>  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
#> 
#> time zone: America/Denver
#> tzcode source: system (glibc)
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] igraph_1.4.2       netdiffuseR_1.22.5
#> 
#> loaded via a namespace (and not attached):
#>  [1] Matrix_1.5-4          dplyr_1.1.2           compiler_4.3.0       
#>  [4] tidyselect_1.2.0      reprex_2.0.2          Rcpp_1.0.10          
#>  [7] networkLite_1.0.5     boot_1.3-28.1         yaml_2.3.7           
#> [10] fastmap_1.1.1         lattice_0.21-8        coda_0.19-4          
#> [13] R6_2.5.1              generics_0.1.3        MatchIt_4.5.3        
#> [16] knitr_1.42            MASS_7.3-59           backports_1.4.1      
#> [19] tibble_3.2.1          statnet.common_4.8.0  R.cache_0.16.0       
#> [22] pillar_1.9.0          R.utils_2.12.2        rlang_1.1.1          
#> [25] utf8_1.2.3            xfun_0.39             fs_1.6.2             
#> [28] viridisLite_0.4.1     cli_3.6.1             withr_2.5.0          
#> [31] magrittr_2.0.3        network_1.18.1        digest_0.6.31        
#> [34] grid_4.3.0            rstudioapi_0.14       lifecycle_1.0.3      
#> [37] R.methodsS3_1.8.2     R.oo_1.25.0           vctrs_0.6.2          
#> [40] SparseM_1.81          sna_2.7-1             evaluate_0.20        
#> [43] glue_1.6.2            styler_1.9.1          fansi_1.0.4          
#> [46] rmarkdown_2.21        purrr_1.0.1           networkDynamic_0.11.3
#> [49] tools_4.3.0           pkgconfig_2.0.3       htmltools_0.5.5

Created on 2023-05-03 with reprex v2.0.2

PCK1992 commented 1 year ago

That snippet helped me figure out what my issue was! I was sampling the node IDs and not their position on the matrix. Since I have very large number as node names (e.g. 192846283648) the error message now makes sense. Thanks for the help, I will close this issue!