cannot add attribute to bipartite network of class sna

257 Views Asked by At

I am trying to format data to run an ERGM model. However, I have problems combining the two data sets that I would like to use for the ergm model. First, my actual data set is a bipartite network with the first type of nodes (politicians) as rows and the second type of nodes (each votes of the politicians on one topic) as columns. However, when trying to add attributes to the politicians, I have run into the error that my sna object is not a graph object. In order to find out what the problem is, I used an example from the website (link bellow) and changed it slightly so that it is a bipartite network, too. I ran into the same problem that I have with my larger original data set.

http://www.mjdenny.com/Preparing_Network_Data_In_R.html

here the reproducible example

rm(list = ls())
# install.packages("statnet", dependencies = TRUE) 
library(statnet)

num_nodes <- 10 # in my real data set, these would be the politicians
num_events <- 30 # in my real data set, these would be the votes

my_sociomatrix <- matrix(round(runif(num_nodes*num_events)), # edge values
                         nrow = num_events, #nrow must be same as ncol
                         ncol = num_nodes)
diag(my_sociomatrix) <- 0
class(my_sociomatrix)
net <- as.network(x = my_sociomatrix, # the network object
                  directed = TRUE, # specify whether the network is directed
                  bipartite = TRUE,
                  loops = FALSE, # do we allow self ties (should not allow them)
                  matrix.type = "adjacency" # the type of input
)

Adding names seems to work:

network.vertex.names(net) <- LETTERS[1:10]
network.vertex.names(net) <- c("Susan","Rachel","Angela","Carly","Stephanie","Tom","Mike","Tony","Matt","Steven")
gender <- c(rep("Female",num_nodes/2),rep("Male",num_nodes/2))
print(gender)

Here is the part that does not work:

set.vertex.attribute(net, # the name of the network object
                     "Gender", # the name we want to reference the variable by in that object
                     gender # the value we are giving that variable
) 

This is the error that needs to be resolved:

Error in i_set_vertex_attr(graph = graph, name = name, index = index,  : 
  Not a graph object

does anyone know how to add attributes to bipartite sna object (sna objects should allow me to run an ERGM)? The code does work if I follow the example on the website (link above) but not for the bipartite network.

0

There are 0 best solutions below