Search code examples
rsocial-networkingsnastatnet

Importing a weighted adjacency matrix using the Statnet package in R


I am trying to analyze a weighted 1 mode projection of a 2 mode network in R using bipartite and the statnet suite (consisting of network, sna, and several other packages) on a Unix server. The projection works fine using a mix of bipartite and matrix algebra, but when I try to import the valued matrix as a weighted network object, using the code below, I seem to loose the values that are in my original matrix.

MNDocnet<-as.network(MNDocmatrix,matrix.type="adjacency",directed=FALSE, hyper=FALSE, loops=TRUE, multiple=FALSE, bipartite = FALSE, ignore.eval=FALSE, names.eval="patients")

Thanks for any help you can provide.


Solution

  • It is hard to know exactly without your data structures, but that syntax looks correct to me. Here is an example

    make sample input data

    > adjmat<-matrix(c(0,1,2,3,0,4,5,6,0),ncol=3)
    > adjmat
         [,1] [,2] [,3]
    [1,]    0    3    5
    [2,]    1    0    6
    [3,]    2    4    0
    

    Convert matrix into network object

    > test<-as.network(adjmat,matrix.type='adjacency',ignore.eval=FALSE,names.eval='sample')
    

    print edge values for attribute named 'sample'

    > test%e%'sample'
    [1] 1 2 3 4 5 6
    

    Notice that if you want to convert it back to a valued matrix, you need to give it the name of the attribute providing the values:

    > as.matrix(test)
      1 2 3
    1 0 1 1
    2 1 0 1
    3 1 1 0
    

    vs.

    > as.matrix(test,attrname='sample')
      1 2 3
    1 0 3 5
    2 1 0 6
    3 2 4 0