valued bipartite projection using R igraph

依然范特西╮ 提交于 2021-01-28 07:40:36

问题


I am trying to create valued bipartite projection from my data which looks like the following:

                 Name   rid
                Emile    17560
                Lou     11800
                Luther   11800
                Tot     11800
                Phineas   11800
                Phineas   13580
                Calvin    13580
                Calvin    11800
                  Les      11800
                 Jeff      11800
                 Sonny     13580
                  Leon      13580
                  Red       13580

I imported the above data and call it data1

Then I do the following:

##create graph object from data1
graph1 <- graph.data.frame(data1)

#check if it is bipartite
is.bipartite(graph1)
FALSE

##convert to bipartite graph
V(graph1)$type <- V(graph1)$name %in% data1[,1]

##check again if it is bipartite
is.bipartite(graph1)
TRUE

##create biparite projection
 ##the multiplicity argument is suppose to count the number of 
 ##edges
 proj<-bipartite.projection(graph1, V(graph1)$type,multiplicity = TRUE)

##get adjacency matrix from second projection
get.adjacency(proj$proj2)

I get the following ouput

 11 x 11 sparse Matrix of class "dgCMatrix"
  [[ suppressing 11 column names ‘Emile ’, ‘Lou ’, ‘Luther ’ ... ]]

   Emile    . . . . . . . . . . .
   Lou      . . 1 1 1 . 1 1 1 . .
   Luther   . 1 . 1 1 . 1 1 1 . .
   Tot      . 1 1 . 1 . 1 1 1 . .
   Phineas  . 1 1 1 . . 1 1 1 . .
   Phineas  . . . . . . 1 . . 1 1
   Calvin   . 1 1 1 1 1 . 1 1 1 1
   Les      . 1 1 1 1 . 1 . 1 . .
   Jeff     . 1 1 1 1 . 1 1 . . .
   Sonny    . . . . . 1 1 . . . 1
   Leon     . . . . . 1 1 . . 1 .

and

     3 x 3 sparse Matrix of class "dgCMatrix"
                17560 11800 13580
        17560     .     .     .
        11800     .     .     1
       13580     .     1     .

Instead of Phineas and Calvin having 2 edges through ***rid***s 13580 and 11800, Phineas appears twice. And the plot of the "rid" projection shows only 1 edge between 13580 and 11800. The plot for the "Name" shows Phineas twice.

I appreciate any suggestions to modify this code to get valued projection and adjacency matrix. thank you!

EDIT #1: Phineas was appearing twice because of some type of formatting issue where two "Phinease-s" were being recognized as separate names. BUT, fixing this still does not solve the main problem. The output still gives only 1 edge between Phineas and Calvin and between 13580 and 11800.

Edit #2: Session Info results

R version 3.1.0 (2014-04-10)
Platform: i386-w64-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United        States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

 other attached packages:
  [1] igraph_1.0.1

  loaded via a namespace (and not attached):
  [1] grid_3.1.0      lattice_0.20-29 magrittr_1.5    Matrix_1.1-3        tools_3.1.0    

\

Edit #3

Desired output
  3 x 3 sparse Matrix of class "dgCMatrix"
                17560 11800 13580
        17560     .     .     .
        11800     .     .     2
       13580     .     2     .


 11 x 11 sparse Matrix of class "dgCMatrix"
  [[ suppressing 11 column names ‘Emile ’, ‘Lou ’, ‘Luther ’ ... ]]

   Emile    . . . . . . . . . . .
   Lou      . . 1 1 1 . 1 1 1 . .
   Luther   . 1 . 1 1 . 1 1 1 . .
   Tot      . 1 1 . 1 . 1 1 1 . .
   Phineas  . 1 1 1 . 2 1 1 1 1
   Calvin   . 1 1 1 2 1 . 1 1 1 1
   Les      . 1 1 1 1 . 1 . 1 . .
   Jeff     . 1 1 1 1 . 1 1 . . .
   Sonny    . . . . . 1 1 . . . 1
   Leon     . . . . . 1 1 . . 1 .

回答1:


I do not know what you have done. Probably some more information is necessary: sessionInfo() to check what system were you using and what version of the packages.

This is what I've found in my PC (see below the result of sessionInfo()

the data

data1 <- read.table(text="Name   rid
           Emile    17560
             Lou     11800
             Luther   11800
             Tot     11800
             Phineas   11800
             Phineas   13580
             Calvin    13580
             Calvin    11800
               Les      11800
              Jeff      11800
              Sonny     13580
               Leon      13580
               Red       13580", header=TRUE)

Check the data

head(data1)
dim(data1)
str(data1)
plot(data1)

Call the library

library(igraph)

Create graph object from data1

graph1 <- graph.data.frame(data1)

Check if it is bipartite

is.bipartite(graph1)        ### FALSE

Convert to bipartite graph

V(graph1)$type <- V(graph1)$name %in% data1[,1]

Check again if it is bipartite

is.bipartite(graph1)        ### TRUE

Create biparite projection The multiplicity argument is suppose to count the number of edges

proj<-bipartite.projection(graph1, V(graph1)$type,multiplicity = TRUE)

Get adjacency matrix from the first projection

t1 <- get.adjacency(proj$proj1)
t1

Loading required package: Matrix

11 x 11 sparse Matrix of class "dgCMatrix"

[[ suppressing 11 column names ‘Emile’, ‘Lou’, ‘Luther’ ... ]]

Emile . . . . . . . . . . .

Lou . . 1 1 1 1 1 1 . . .

Luther . 1 . 1 1 1 1 1 . . .

Tot . 1 1 . 1 1 1 1 . . .

Phineas . 1 1 1 . 1 1 1 1 1 1

Calvin . 1 1 1 1 . 1 1 1 1 1

Les . 1 1 1 1 1 . 1 . . .

Jeff . 1 1 1 1 1 1 . . . .

Sonny . . . . 1 1 . . . 1 1

Leon . . . . 1 1 . . 1 . 1

Red . . . . 1 1 . . 1 1 .

Get adjacency matrix from second projection

t2 <- get.adjacency(proj$proj2)
t2

3 x 3 sparse Matrix of class "dgCMatrix"

  17560 11800 13580

17560 . . .

11800 . . 1

13580 . 1 .

Plot your matrix

g1 <- graph.adjacency(t1)

plot(g1)

Result of sessionInfo() R version 3.0.2 (2013-09-25) Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=Italian_Italy.1252  LC_CTYPE=Italian_Italy.1252   
[3] LC_MONETARY=Italian_Italy.1252 LC_NUMERIC=C                  
[5] LC_TIME=Italian_Italy.1252    

attached base packages:[1] stats     graphics  grDevices utils        datasets  methods   base     
other attached packages: [1] Matrix_1.1-0 igraph_0.6.6
loaded via a namespace (and not attached): [1] grid_3.0.2      lattice_0.20-24


来源:https://stackoverflow.com/questions/32300263/valued-bipartite-projection-using-r-igraph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!