Question: hypergraph package - kcores error
0
gravatar for john.horne
28 days ago by
john.horne0
john.horne0 wrote:

Hi,

I am using the hypergraph package specifically because I want to try hypergraph partitioning on some pairwise data I have.

I've populated my hypergraph by creating hyperedges for each of my sample names that meet a pairwise threshold criteria with other sample names. For example: is sampleA shares a pairwise value of between 0.4 and 0.6 with sampleB and sample_C, then all three are described by a hyperedge.

But when I use the kCoresHypergraph() function to partition my hypergraph into cores, I get this error:

> kCoresHypergraph(STX_hypergraph)
**Error in im[r_chosen, c_chosen] : subscript out of bounds.**

After digging a bit deeper this appears to be a bug.

> options(error=recover)

> STX_cores <- kCoresHypergraph(STX_hypergraph)
**Error in im[r_chosen, c_chosen] : subscript out of bounds**

>Enter a frame number, or 0 to exit   
>1: kCoresHypergraph(STX_hypergraph)
>2: matrix(im[r_chosen, c_chosen], nrow = length(r_chosen), ncol = length(c_cho

>Selection: 2

>Called from: top level 

>Browse[1]> ls()
[1] "byrow"    "data"     "dimnames" "ncol"     "nrow"   

>Browse[1]> data
**Error during wrapup: promise already under evaluation: recursive default argument reference or earlier problems?**

> Browse[1]> dim(im)
[1] 170  21

> Browse[1]> r_chosen
91633 185314 
    84    167 

> Browse[1]> c_chosen
[1] 35 36

It is also worth mentioning that my constructed hypergraph does not throw an error with the vCoverHypergraph() function. Therefore, there must be something awry with the kCoresHypergraph() function.

If somebody could help me with this I would greatly appreciate it.

hypergraph software error • 142 views
ADD COMMENTlink modified 23 days ago by James W. MacDonald52k • written 28 days ago by john.horne0

As the FAQ asks, please provide a self-contained example that shows the problem. Unless other people can reproduce and try to diagnose, all we know is you did something and it didn't work.

ADD REPLYlink written 23 days ago by James W. MacDonald52k

Thanks, yes, OK,

Some data:

> test1 <- c(rep('A',5),rep('B',4),rep('C',3),rep('D',2),rep('E',1))
> test2 <- c("B","C","D","E","F","C","D","E","F","D","E","F","E","F","F")
> test3 <- c(-0.020,0.101,0.100,0.036,-0.152,-0.059,-0.062,-0.050,0.060,0.093,0.195,-0.015,0.066,-0.125,-0.135)
> test <- data.frame(test1, test2, test3, stringsAsFactors=FALSE)
> names(test) <- c("id1","id2","values")

> test
   id1 id2 values
    A   B -0.020
    A   C  0.101
    A   D  0.100
    A   E  0.036
    A   F -0.152
    B   C -0.059
    B   D -0.062
    B   E -0.050
    B   F  0.060
    C   D  0.093
    C   E  0.195
    C   F -0.015
    D   E  0.066
    D   F -0.125
    E   F -0.135

A loop to make the hyper edges. Here, a hyper edge encompasses a sample id node and all other sample id that share a positive pairwise value with it.

> library(hypergraph)

> h_edges=NULL
for(i in 1:length(test$id1)) {
   k <- test$id2[which(test$id1==test$id1[i] & test$values > 0)]
   nodes <- c(test$id1[i], k)
   if(length(nodes) > 1) {
      hyper <- Hyperedge(nodes, label = test$id1[i])
      h_edges <- c(h_edges, hyper)
   }      
}

make the hypergraph

> node_names <- c(unique(test$id1),"F")

> STX_hypergraph <- Hypergraph(node_names, h_edges)

Cause Error when trying to find the hyper graph cores

> STX_cores <- kCoresHypergraph(STX_hypergraph)
**Error in im[r_chosen, c_chosen] : subscript out of bounds**
ADD REPLYlink modified 23 days ago • written 23 days ago by john.horne0

Also, will you either highlight your code and click the box with 101010 (or hit Ctrl-K or bracket with triple back-ticks) so the code is readable? I have fixed both of your posts, but it's pretty tedious to have to do that.

ADD REPLYlink written 23 days ago by James W. MacDonald52k

Also, your example isn't self-contained (where by 'self contained' we normally mean 'I don't have to, like, type in a bunch of data to recreate your test data.frame). Normally people use dput to make that easier on people who might want to help, although I do hear that the tidyverse has a package called reprex that is supposed to be useful for that sort of thing.

ADD REPLYlink written 23 days ago by James W. MacDonald52k

Thanks for that. I appreciate it.

ADD REPLYlink written 23 days ago by john.horne0

Note that

> dput(test)
structure(list(id1 = c("A", "A", "A", "A", "A", "B", "B", "B", 
"B", "C", "C", "C", "D", "D", "E"), id2 = c("B", "C", "D", "E", 
"F", "C", "D", "E", "F", "D", "E", "F", "E", "F", "F"), values = c(-0.02, 
0.101, 0.1, 0.036, -0.152, -0.059, -0.062, -0.05, 0.06, 0.093, 
0.195, -0.015, 0.066, -0.125, -0.135)), class = "data.frame", row.names = c(NA, 
-15L))

Allows one to do a simple copy'n'paste

> test <-  structure(list(id1 = c("A", "A", "A", "A", "A", "B", "B", "B", 
"B", "C", "C", "C", "D", "D", "E"), id2 = c("B", "C", "D", "E", 
"F", "C", "D", "E", "F", "D", "E", "F", "E", "F", "F"), values = c(-0.02, 
0.101, 0.1, 0.036, -0.152, -0.059, -0.062, -0.05, 0.06, 0.093, 
0.195, -0.015, 0.066, -0.125, -0.135)), class = "data.frame", row.names = c(NA, 
-15L))
test <-  structure(list(id1 = c("A", "A", "A", "A", "A", "B", "B", "B", 
+ "B", "C", "C", "C", "D", "D", "E"), id2 = c("B", "C", "D", "E", 
+ "F", "C", "D", "E", "F", "D", "E", "F", "E", "F", "F"), values = c(-0.02, 
+ 0.101, 0.1, 0.036, -0.152, -0.059, -0.062, -0.05, 0.06, 0.093, 
+ 0.195, -0.015, 0.066, -0.125, -0.135)), class = "data.frame", row.names = c(NA, 
+ -15L))
> test
   id1 id2 values
1    A   B -0.020
2    A   C  0.101
3    A   D  0.100
4    A   E  0.036
5    A   F -0.152
6    B   C -0.059
7    B   D -0.062
8    B   E -0.050
9    B   F  0.060
10   C   D  0.093
11   C   E  0.195
12   C   F -0.015
13   D   E  0.066
14   D   F -0.125
15   E   F -0.135

Which is easier than copying and pasting five lines of code that have > prepended.

ADD REPLYlink written 23 days ago by James W. MacDonald52k
Answer: hypergraph package - kcores error
1
gravatar for James W. MacDonald
23 days ago by
United States
James W. MacDonald52k wrote:

Your nodes seem incorrect to me:

> for(i in 1:14) cat(nodes(h_edges[[i]]), "\n")
A C D E 
A C D E 
A C D E 
A C D E 
A C D E 
B F 
B F 
B F 
B F 
C D E 
C D E 
C D E 
D E 
D E 

Is there a particular reason to have all those duplicates? That seems like a pathological example, but maybe I'm wrong? If so, one could argue that there should be some error checking. Anyway, this seems more reasonable:

> goodtest <- test[test$values > 0,]
> he <- lapply(mapply(c, goodtest[,1], goodtest[,2], SIMPLIFY = FALSE), "Hyperedge")
> he2 <- Hypergraph(LETTERS[1:6], he)
> kCoresHypergraph(he2)
A B C D E F 
3 1 3 3 3 1 
ADD COMMENTlink written 23 days ago by James W. MacDonald52k

This works on my larger data set too. Thanks.

Do you have any good ideas for how to represent hypergraphs graphically in R?

ADD REPLYlink written 23 days ago by john.horne0

There's the hyperdraw package, but you need DirectedHypergraph objects, not Hypergraph.

## after generating goodtest data.frame, and loading the hyperdraw package
> he <- lapply(seq_len(nrow(goodtest)), function(x) DirectedHyperedge(goodtest[x,1], goodtest[x,2], paste0("R", x)))
> he2 <- Hypergraph(LETTERS[1:6], he)
> plot(graphBPH(he2))
ADD REPLYlink written 22 days ago by James W. MacDonald52k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 16.09
Traffic: 140 users visited in the last hour