Search code examples
rnetwork-programmingstatnet

Generate network using adjacency matrix


I have a problem with generating network object for further ergm estimation in R. Namely, when I feed the network function with an adjacency matrix, it generates an error that my network contains loops and multiple edges, which is simply not true. I ran the same code with the same matrix about two years ago and everything was alright. Now I tried to use different versions of R, but all of them lead to the same problem. As an example, I also used the following simple matrix:

    1 2 3 4 5 6 7 8 9 10
1   0 1 0 0 0 0 0 0 0 0
2   1 0 0 0 0 0 0 0 0 0
3   0 0 0 1 1 0 0 0 0 0
4   0 0 1 0 1 1 0 0 0 0
5   0 0 1 1 0 0 1 0 1 1
6   0 0 0 1 0 0 1 1 1 0
7   0 0 0 0 1 1 0 0 1 1
8   0 0 0 0 0 1 0 0 0 0 
9   0 0 0 0 1 1 1 0 0 1 
10  0 0 0 0 1 0 1 0 1 0

and the following code:

net <- network(data, directed=F, matrix.type='adjacency') 

with data being the matrix above. The obtained error is:

Error: `loops` is `FALSE`, but `x` contains loops.
The following values are affected:
    - `x[3, 1:2]`
    - `x[4, 1:2]`
    - `x[5, 1:2]`
    - `x[6, 1:2]`
    - `x[7, 1:2]`
    - `x[8, 1:2]`

I would be very pleased if you could help me to find out what is the problem and what could be a possible remedy. Or recommend me some another ergm package to use, probably a python one


Solution

  • I am not sure what could be the problem in your case; maybe another function from a loaded package that interferes, but it seems to work for me (see reproducible example below):

    data <- data.matrix(read.table(text="    1 2 3 4 5 6 7 8 9 10
    1   0 1 0 0 0 0 0 0 0 0
    2   1 0 0 0 0 0 0 0 0 0
    3   0 0 0 1 1 0 0 0 0 0
    4   0 0 1 0 1 1 0 0 0 0
    5   0 0 1 1 0 0 1 0 1 1
    6   0 0 0 1 0 0 1 1 1 0
    7   0 0 0 0 1 1 0 0 1 1
    8   0 0 0 0 0 1 0 0 0 0 
    9   0 0 0 0 1 1 1 0 0 1 
    10  0 0 0 0 1 0 1 0 1 0
    ", check.names = FALSE))
    library(ergm)
    #> Loading required package: network
    #> 
    #> 'network' 1.18.0 (2022-10-05), part of the Statnet Project
    #> * 'news(package="network")' for changes since last version
    #> * 'citation("network")' for citation information
    #> * 'https://statnet.org' for help, support, and other information
    #> 
    #> 'ergm' 4.3.1 (2022-11-07), part of the Statnet Project
    #> * 'news(package="ergm")' for changes since last version
    #> * 'citation("ergm")' for citation information
    #> * 'https://statnet.org' for help, support, and other information
    #> 'ergm' 4 is a major update that introduces some backwards-incompatible
    #> changes. Please type 'news(package="ergm")' for a list of major
    #> changes.
    library(network)
    net <- network(data, directed=F, matrix.type='adjacency') 
    summary(net)
    #> Network attributes:
    #>   vertices = 10
    #>   directed = FALSE
    #>   hyper = FALSE
    #>   loops = FALSE
    #>   multiple = FALSE
    #>   bipartite = FALSE
    #>  total edges = 14 
    #>    missing edges = 0 
    #>    non-missing edges = 14 
    #>  density = 0.3111111 
    #> 
    #> Vertex attributes:
    #>   vertex.names:
    #>    character valued attribute
    #>    10 valid vertex names
    #> 
    #> No edge attributes
    #> 
    #> Network adjacency matrix:
    #>    1 2 3 4 5 6 7 8 9 10
    #> 1  0 1 0 0 0 0 0 0 0  0
    #> 2  1 0 0 0 0 0 0 0 0  0
    #> 3  0 0 0 1 1 0 0 0 0  0
    #> 4  0 0 1 0 1 1 0 0 0  0
    #> 5  0 0 1 1 0 0 1 0 1  1
    #> 6  0 0 0 1 0 0 1 1 1  0
    #> 7  0 0 0 0 1 1 0 0 1  1
    #> 8  0 0 0 0 0 1 0 0 0  0
    #> 9  0 0 0 0 1 1 1 0 0  1
    #> 10 0 0 0 0 1 0 1 0 1  0
    

    Created on 2022-11-23 with reprex v2.0.2