statnet / networkDynamic

Dynamic Extensions for Network Objects
10 stars 1 forks source link

Issue using dynamicNetwork. Code seems to take max numeric value among vertex.ids instead of vertext length to check for validity. #17

Open roelicaal opened 2 years ago

roelicaal commented 2 years ago

Hi! I am having issues loading a dynamic network. I am on Windows 11, run R 4.2.1 and the latest versions of the network- and networkDynamic packages installed using devtools::install_github and auto-compiled using rtools4.2.

Loading a static network works I am able to load a static network:

`

network(edgelist_bare,vertices=nodelist_bare)

##  Network attributes:
##   vertices = 124 
##   directed = TRUE 
##   hyper = FALSE 
##   loops = FALSE 
##   multiple = FALSE 
##   bipartite = FALSE 
##   total edges= 321 
##     missing edges= 0 
##     non-missing edges= 321 
## 
##  Vertex attribute names: 
##     Betweenness Community Full_name Label PageRank vertex.names 
## 
## No edge attributes`

My data look like this:

`

head(nodelist_bare)

## # A tibble: 6 × 6
##      Id Label           Full_name               PageRank Betweenness Community
##   <int> <chr>           <chr>                      <dbl>       <dbl>     <int>
## 1    80 jijbentscherper JIJ BENT SCHERPER          2.87       2043.        148
## 2    81 shanks_down     #ShanksDown                3.48        656.        148
## 3    82 urbanskillsz    Stichting Urban Skillsz    5.28       2764.        148
## 4    83 wapensdewijkuit Wapens de Wijk Uit         3.92       1708.        148
## 5    84 jbrr.jongeren   JBRR | Jongeren            4.72       1342.        148
## 6    85 mierojw         Miero jongerenwerk         0.543        23.8        77

head(edgelist_bare)

## # A tibble: 6 × 2
##    tail  head
##   <int> <int>
## 1    80   349
## 2    80  1350
## 3    80  2507
## 4    80    82
## 5    80   391
## 6    80  2949`

Network characteristics are as follows:

`

network.size(net_bare)

## [1] 124

network.edgecount(net_bare)

## [1] 321`

I can't load a dynamic network I can't seem to load a dynamic network by using the static networks as a base.

`

networkDynamic(base.net = net_bare, edge.spells=edge_spells, vertex.spells=vertex_spells)

## Error in networkDynamic(base.net = net_bare, edge.spells = edge_spells,  : 
##   base.net network size is smaller than size implied by vertex.ids in vertex or edge argument`

When I try to load the network only using the spell data frames, then it seem to load but incorrectly, imputing the number of nodes from the highest numeric value in my vertex.ids (which is 4036).

`

networkDynamic(edge.spells=edge_spells, vertex.spells=vertex_spells)

## Initializing base.net of size 4036 imputed from maximum vertex id in edge records
## Created net.obs.period to describe network
##  Network observation period info:
##   Number of observation spells: 1 
##   Maximal time range observed: 1 until 40 
##   Temporal mode: continuous 
##   Time unit: unknown 
##   Suggested time increment: NA

## NetworkDynamic properties:
##   distinct change times: 8 
##   maximal time range: 1 until  40 
## 
## Includes optional net.obs.period attribute:
##  Network observation period info:
##   Number of observation spells: 1 
##   Maximal time range observed: 1 until 40 
##   Temporal mode: continuous 
##   Time unit: unknown 
##   Suggested time increment: NA 
## 
##  Network attributes:
##   vertices = 4036 
##   directed = TRUE 
##   hyper = FALSE 
##   loops = FALSE 
##   multiple = FALSE 
##   bipartite = FALSE 
##   net.obs.period: (not shown)
##   total edges= 321 
##     missing edges= 0 
##     non-missing edges= 321 
## 
##  Vertex attribute names: 
##     active vertex.names 
## 
##  Edge attribute names: 
##     active

`

I double checked my data and that seems to be fine:

`

network.size(net_bare)

## [1] 124

network.edgecount(net_bare)

## [1] 321

vertex_spells$vertex.id %>% length

## [1] 124

vertex_spells$vertex.id %>% unique %>% length

## [1] 124

c(edge_spells$head,edge_spells$tail) %>% unique %>% length

## [1] 124

edge_spells %>% nrow

## [1] 321

`

When looking at the code of the networkDynamic package, it seems to go wrong at lines 470-490 where it does not take the maximum number of vertexes to check data validity, but the maximum value among the vertexes.

Which makes me wonder: is this a bug or am I doing something wrong?

`

vertex_spells$vertex.id %>% sort
## [1]   80   81   82   83   84   85   86  254  256  257  258  262  263  265  271  274  277  279  281  282  284  285  288  301  304  305  306  308  309  310
## [31]  313  314  315  322  327  328  329  336  337  338  341  347  348  349  350  351  355  358  363  364  365  366  369  372  374  382  383  385  391  393
## [61]  395  405  409  432  464  483  490  501  580  619  746  766  804  816  835  850  862  903  912  950  959  970  975 1076 1081 1085 1091 1146 1174 1201
## [91] 1209 1216 1228 1252 1331 1334 1341 1350 1360 1399 1401 1492 1589 1616 1791 1822 1982 2147 2333 2507 2614 2663 2674 2690 2848 2855 2868 2949 3050 3092
## [121] 3136 3187 3522 4036

`

skyebend commented 2 years ago

My hunch is that it is confused that you are using integer vertex ids, but they don't correspond the 'index' id of vertex ids. i.e. the first vertex has id 80, and it created a network with 4036 vertices in order to include the largest id. I think the network constructors can do some conversion from labels to ids, so could try converting the vertex.id column to character before constructing the network? otherwise, I think you need to replace each value in your edgelist with the index of the node that has that label.

roelicaal commented 2 years ago

Thank you!

Converting the vertex.id column does not work (the networkDynamic function does not accept values other than numeric) but I solved it by replacing the ids with consecutive values.

karl-an commented 4 months ago

Thank you!

Converting the vertex.id column does not work (the networkDynamic function does not accept values other than numeric) but I solved it by replacing the ids with consecutive values.

Thank you very much, that was very useful! I think it should be possible to have node.ids that are non-consecutive