0

I'm preparing some network data to run ERGMs in R using the statnet library. I want to assign an attribute to the edges that I will use when I run the ERGM. The matrix include numbers between 0 and 1 for each tie in the network. I'm getting an error when I use set.edge.attribute that says "inappropriate value given in set.edge.attribute."

I first thought that there might be an issue with the values in the matrix that contains the attribute I'd like to add. To check this, I created a matrix with random numbers in it and ran the set.edge.attribute code again, but still got the error.

I imported the network and the edge attribute as CSV files, converted the network file into a network object, and converted the edge attribute to a matrix. The edge attribute has the same number of edges in it as the network.

library(statnet)
NetworkGraph = network(NetworkData,type="adjacency", directed=FALSE)
EdgeInfo = as.matrix(EdgeInfo) 
NetworkGraph<-set.edge.attribute(NetworkGraph,"edge_attribute", EdgeInfo)

To generate a matrix of attributes to test this with, I used runif to make a new matrix, but I still got the same error):

Test = matrix(runif(23*23), nrow=23, ncol=23)
NetworkGraph<-set.edge.attribute(NetworkGraph,"edge_attribute", Test)

What could make this work?

JLou
  • 41
  • 5

1 Answers1

0

one way to do this would be to directly create the network from the matrix of values:

> library(network)

# create a small test matrix
> gvalues<-matrix(runif(5*5),nrow=5,ncol=5)
> gvalues
          [,1]       [,2]      [,3]       [,4]       [,5]
[1,] 0.6456140 0.88881086 0.2855281 0.79027269 0.01526509
[2,] 0.4825466 0.64184675 0.4456986 0.44277690 0.27018424
[3,] 0.5276330 0.04742485 0.7675878 0.05453299 0.36940432
[4,] 0.3188620 0.52574674 0.1642077 0.07034616 0.60229633
[5,] 0.3741370 0.22432400 0.8093938 0.24704229 0.29042967

# convert to network object, telling it to *not* ignore edge values
# and also name the edge values 'testValue'
> g <- as.network(gvalues,ignore.eval = FALSE, loops=TRUE, names.eval='testValue')

# check that the edge value was added. since it was a full matrix, it should be a 'complete' network with 25 edges connecting all nodes
> g
 Network attributes:
  vertices = 5 
  directed = TRUE 
  hyper = FALSE 
  loops = TRUE 
  multiple = FALSE 
  bipartite = FALSE 
  total edges= 25 
    missing edges= 0 
    non-missing edges= 25 

 Vertex attribute names: 
    vertex.names 

 Edge attribute names: 
    testValue 

# convert network back to a matrix to check if it worked
> as.matrix(g,attrname = 'testValue')
          1          2         3          4          5
1 0.6456140 0.88881086 0.2855281 0.79027269 0.01526509
2 0.4825466 0.64184675 0.4456986 0.44277690 0.27018424
3 0.5276330 0.04742485 0.7675878 0.05453299 0.36940432
4 0.3188620 0.52574674 0.1642077 0.07034616 0.60229633
5 0.3741370 0.22432400 0.8093938 0.24704229 0.29042967

I included the loops=TRUE argument because the input matrix has values on the diagonal, otherwise don't need this. If you are adding edge attributes to an existing network from a matrix you need to use set.edge.value() instead of set.edge.attribute(). This seems poorly documented.

skyebend
  • 1,079
  • 6
  • 19