igraph-help
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [igraph] clustering coefficient in bipartite network


From: Gábor Csárdi
Subject: Re: [igraph] clustering coefficient in bipartite network
Date: Sat, 27 Nov 2010 15:05:56 +0100

On Sat, Nov 27, 2010 at 11:55 AM, Simone Gabbriellini
<address@hidden> wrote:
> Hi Gabor,
>
> just installed this latest, but got same error...

Maybe you have isolates in your graph, and then it did not work
because of a strange R bug. Here is a corrected version, this one
works for me, even if there are isolates:

ccBip <- function(g) {
 if (! "name" %in% list.vertex.attributes(g)) {
   V(g)$name <- seq_len(vcount(g))
 }
 neib <- get.adjlist(g)
 names(neib) <- V(g)$name
 proj <- bipartite.projection(g)
 lapply(proj, function(x) {
   el <- get.edgelist(x)
   sapply(V(x)$name, function(v) {
     subs <- el[,1]==v | el[,2]==v
     f <- function(un, vn) length(union(un, vn))
     vals <- E(x)[subs]$weight /
       unlist(mapply(f, neib[el[subs,1]], neib[el[subs,2]]))
     mean(vals)
   })
 })
}

Let me know if it works for your graph. If not, then please send me a
small example for which it does not work, or send me your graph in
private.

Best,
G.

> no need to rewrite for 0.5!
>
> best,
> Simone
>
> Il giorno 27/nov/2010, alle ore 11.37, Gábor Csárdi ha scritto:
>
>> Hi Simone,
>>
>> the latest is from last week,
>> http://code.google.com/p/igraph/downloads/detail?name=igraph_nightly_0.6-2221-20101122.tar.gz
>>
>> But be careful, because this one has the one-based indexing already.
>> Alternatively I can rewrite the code to work with 0.5.
>>
>> Gabor
>>
>> On Sat, Nov 27, 2010 at 11:31 AM, Simone Gabbriellini
>> <address@hidden> wrote:
>>> Hi Gabor,
>>>
>>> thanks again for this piece of code...
>>>
>>> I have to admit I am still trying to figure out what this function does in 
>>> the details, because of my lack of R expertise... so many things are 
>>> unclear but I am going to figure them out... ;)
>>>
>>> the only thing I can say now is that I have this error:
>>>
>>>> ccBip(g)
>>> Errore in E(x)[subs]$weight/mapply(f, neib[el[subs, 1]], neib[el[subs,  :
>>>        non-numeric argument transformed in binary operator
>>>
>>> my version of igraph is, I guess, the last nightly source, 
>>> igraph_nightly_0.6-2030-20100726.tar.gz
>>>
>>> best,
>>> Simone
>>>
>>>
>>> Il giorno 27/nov/2010, alle ore 00.30, Gábor Csárdi ha scritto:
>>>
>>>> Hi Simone,
>>>>
>>>> On Fri, Nov 26, 2010 at 12:10 PM, Simone Gabbriellini
>>>> <address@hidden> wrote:
>>>>> HI Gabor,
>>>>>
>>>>> thanks very much, yes that is the right direction for me!
>>>>>
>>>>> what I have to reproduce is, according to Latapy:
>>>>>
>>>>> for each bottom node v:
>>>>>        for each bottom node u, 2-dist-neighbor of v:
>>>>>                 find the number of (shared top nodes of u and v) / (total 
>>>>> top nodes neighbors of u AND v)
>>>>
>>>> ccBip <- function(g) {
>>>>  if (! "name" %in% list.vertex.attributes(g)) {
>>>>    V(g)$name <- seq_len(vcount(g))
>>>>  }
>>>>  neib <- get.adjlist(g)
>>>>  names(neib) <- V(g)$name
>>>>  proj <- bipartite.projection(g)
>>>>  lapply(proj, function(x) {
>>>>    el <- get.edgelist(x)
>>>>    sapply(V(x)$name, function(v) {
>>>>      subs <- el[,1]==v | el[,2]==v
>>>>      f <- function(un, vn) length(union(un, vn))
>>>>      vals <- E(x)[subs]$weight /
>>>>        mapply(f, neib[el[subs,1]], neib[el[subs,2]])
>>>>      mean(vals)
>>>>    })
>>>>  })
>>>> }
>>>>
>>>> I think this does exactly what you want, assuming I understood the
>>>> definition correctly. I have only tested it with igraph 0.6, for which
>>>> the nightly builds are available again at
>>>> http://code.google.com/p/igraph/downloads/list
>>>>
>>>> Please tell me if something is not clear.
>>>>
>>>> Best,
>>>> Gabor
>>>>
>>>>> then to find the local clustering of v, I have to average the list of 
>>>>> values obtained.
>>>>>
>>>>> and conversely for top nodes... it's a bit tricky for me with R...
>>>>>
>>>>> thanks a lot,
>>>>> simone
>>>>>
>>>>> Il giorno 26/nov/2010, alle ore 11.34, Gábor Csárdi ha scritto:
>>>>>
>>>>>> Hi Simone,
>>>>>>
>>>>>> a couple of comments.
>>>>>>
>>>>>> On Thu, Nov 25, 2010 at 5:22 PM, Simone Gabbriellini
>>>>>> <address@hidden> wrote:
>>>>>>> Hello List,
>>>>>>>
>>>>>>> I am trying to reproduce the clustering measures detailed in Latapy et 
>>>>>>> al. Social Networks, 30 (2008).
>>>>>>>
>>>>>>> I attempted successfully to reproduce the ccN(G) clustering, which is 
>>>>>>> basically an extension for bipartite networks of the global 
>>>>>>> transitivity measure.
>>>>>>>
>>>>>>> I am stuck with the cc. measure of clustering coefficient, an extension 
>>>>>>> of local transitivity for bipartite network - a reprise of what 
>>>>>>> Borgatti and Everett have already suggested in 1997.
>>>>>>>
>>>>>>> I have to find, for each distance-2 neighbors of a node (which are 
>>>>>>> still nodes of the same set), how many nodes of the other set they have 
>>>>>>> in common.
>>>>>>>
>>>>>>> This is not all of what is needed to implement this measure, but it 
>>>>>>> would be a great step for me...
>>>>>>>
>>>>>>> In order to find distance-2 neighbors for each node, I can use a 
>>>>>>> partition, as Tamas suggested in a previous thread.
>>>>>>>
>>>>>>> V(g)[type==FALSE]$neibi<-neighborhood(bipartite.projection(g)[[1]], 1)
>>>>>>>
>>>>>>> V(g)[type==TRUE]$neibi<-neighborhood(bipartite.projection(g)[[2]], 1)
>>>>>>
>>>>>> it is actually better to use vertex names to be sure that you assign
>>>>>> the second neighbors to the right vertices. While your solution works
>>>>>> if the order of the vertices is kept in the projections, this is not
>>>>>> documented for bipartite.projection, so you cannot take it for
>>>>>> granted.
>>>>>>
>>>>>> Anyway, I think an easier way to get the second neighbors is to simply
>>>>>> subtract the 1-neighborhood from the 1-2-neighborhood, this works for
>>>>>> bipartite graphs.
>>>>>>
>>>>>> nei12 <- neighborhood(g, 2)
>>>>>> nei1 <- neighborhood(g, 1)
>>>>>> nei2 <- mapply(setdiff, nei12, nei1)
>>>>>>
>>>>>>> While in order to find neighbors in the bipartite, I can simply use:
>>>>>>>
>>>>>>> V(g)$nei<-neighborhood(g, 1)
>>>>>>>
>>>>>>> now, how can I confront a node with every nodes listed in its neibi 
>>>>>>> attribute in order to find if there are duplicates in each nei 
>>>>>>> attributes? this is the hardest part I cannot solve.
>>>>>>
>>>>>> If you have two numeric or character vectors, 'v1' and 'v2', then
>>>>>> 'intersection(v1, v2)' treats them as sets and gives a vector that is
>>>>>> their intersection. Is this what you need?
>>>>>>
>>>>>> G.
>>>>>>
>>>>>>> any help more than welcome!
>>>>>>>
>>>>>>> thanks in advance,
>>>>>>> Simone
>>>>>>> _______________________________________________
>>>>>>> igraph-help mailing list
>>>>>>> address@hidden
>>>>>>> http://lists.nongnu.org/mailman/listinfo/igraph-help
>>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> --
>>>>>> Gabor Csardi <address@hidden>     UNIL DGM
>>>>>>
>>>>>> _______________________________________________
>>>>>> igraph-help mailing list
>>>>>> address@hidden
>>>>>> http://lists.nongnu.org/mailman/listinfo/igraph-help
>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> igraph-help mailing list
>>>>> address@hidden
>>>>> http://lists.nongnu.org/mailman/listinfo/igraph-help
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Gabor Csardi <address@hidden>     UNIL DGM
>>>>
>>>> _______________________________________________
>>>> igraph-help mailing list
>>>> address@hidden
>>>> http://lists.nongnu.org/mailman/listinfo/igraph-help
>>>
>>>
>>> _______________________________________________
>>> igraph-help mailing list
>>> address@hidden
>>> http://lists.nongnu.org/mailman/listinfo/igraph-help
>>>
>>
>>
>>
>> --
>> Gabor Csardi <address@hidden>     UNIL DGM
>>
>> _______________________________________________
>> igraph-help mailing list
>> address@hidden
>> http://lists.nongnu.org/mailman/listinfo/igraph-help
>
>
> _______________________________________________
> igraph-help mailing list
> address@hidden
> http://lists.nongnu.org/mailman/listinfo/igraph-help
>



-- 
Gabor Csardi <address@hidden>     UNIL DGM



reply via email to

[Prev in Thread] Current Thread [Next in Thread]