1

When I run this code, it works for about 100 iterations of the for loop then throws this error:Error in seq.default(start.at, NROW(data), by = by) : wrong sign in 'by' argument

Here is the data that I used, and here is my code...

library(igraph)
library(zoo)

#import network data as edgelist
fake.raw.data <- read.csv("fakedata.csv") 
fake.raw.data <- fake.raw.data[,2:3]
as.matrix(fake.raw.data)
#create igraph object from edglist data
fgraph <- graph_from_data_frame(fake.raw.data, directed = TRUE)

#finding the shortest paths  that go through "special chain"
POI <- list()
df.vertices <- get.data.frame(fgraph, what = "vertices")
list.vertices <- as.list(df.vertices[,1])
AverageEBForPath <- function(graph = fgraph, from, to, mode = "out", chain){
  browser()
  asp <- all_shortest_paths(graph, from = from, to = to, mode)$res



  for(i in seq_along(asp)){

    if(sum(rollapply(names(asp[[i]]), length(chain), identical, chain)) == 1){
      print(names(asp[[i]]))
    }

  }
}
AverageEBForPath(from = 32, to = V(fgraph), chain = c(32, 15, 9))

If anybody could help that would be extremely appreciated. I have been working on this for days, and I am really stuck.

mousadafousa
  • 77
  • 4
  • 14
  • 1
    I haven't worked through your code, but the error message suggests that the error is being caused by a situation analogous to the following: `seq(10,1,by=1)` which results in `Error in seq.default(10, 1, by = 1) : wrong sign in 'by' argument`. Is there a way your code could result in `from` > `to` with a positive `by` or `from` < `to` with a negative `by`? – eipi10 Jan 25 '16 at 22:52
  • That's the thing, I really don't know how that could occur with my values. However, in a version of the code that wasn't anonomized, I noticed that the error was thrown when it got to the iteration that had a shortest path of one element, and was the same value as the from value. – mousadafousa Jan 25 '16 at 23:04

1 Answers1

4

Looking through the code of rollapply, there's a bit where it works out where in the array to start the rolling. The code it uses is:

start.at <- if (partial < 0) 
    max(-min(width[[1]]), 0) + 1
else 1

Note that in the function itself, width is a list generated from the window width that you're trying to use and the alignment you want... Given that you're passing a window width of 3 and a default alignment of "centre", the width list the function has generated for the code above is a list of three integers: [-1, 0, 1]

Which means that, using the code above, it has decided that given you're after a centre aligned window of width 3, the place to start is the second value in the data (because max(-min(width[[1]]),0) + 1 in the above code evaluates to 2).

All very reasonable, but whilst all of the rest of the instances of asp[[i]] have either 2 or 3 vertices, asp[[100]] has only one vertex (as you rightly pointed out) - so it throws a bit of fit trying to find the second one in order to start rolling through it!

I'm not entirely sure what your function is eventually going to do, so the ball's a bit in your court to work out how best to handle this, I think you've got two options given what you're seeing:

Option 1

Use the partial = TRUE setting on your rollapply, which will just always start at the first vertex no matter what (see the code snippet above!)

Option 2

Use align="left" in your rollapply. In this case, the width list we saw in the rollapply function itself would be [0, 1, 2] for a window width of 3 and start.at would evaluate to 1.

Hope that rambling and convoluted attempt at an answer helps!

Tim Hirst
  • 461
  • 3
  • 15
  • Thank you so much! Using align = "left" has totally fixed this error!! I realize that not being able to fix this bug was largely due to my ignorance about this function. If you could take a look at this [question](http://stackoverflow.com/questions/35022781/how-does-the-rollapply-function-work) I would be very grateful. I am confused about the notion of of a rolling window", and how this function differs from the normal apply function. – mousadafousa Jan 31 '16 at 15:26