9

I am currently trying to run a function that "calculates the expected random-walk commute time between nodes in a graph. It is defined as the effective distance (resistance distance) between the selected nodes multiplied by the volume of the graph, which is the sum of the conductance weights of all the edges in the graph (Chandra et al. 1997). The result represents the average number of steps that is needed to commute between the nodes during a random walk"

To run commuteDistance from the gDistance package, I need to create a transition object.

Step 1: I loaded my rasterlayer

> jul <- EVIrec[[6]]
> jul
class       : RasterLayer 
dimensions  : 642, 382, 245244  (nrow, ncol, ncell)
resolution  : 1000, 1000  (x, y)
extent      : 461951, 843951, 892583.3, 1534583  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=43 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
data source : in memory
names       : Jul 
values      : 1, 3  (min, max)

Step 2: I created a 'transition object' as mentioned here

> tr <- transition(jul, mean, directions = 8)
> tr
 class       : TransitionLayer 
 dimensions  : 642, 382, 245244  (nrow, ncol, ncell)
 resolution  : 1000, 1000  (x, y)
 extent      : 461951, 843951, 892583.3, 1534583  (xmin, xmax, ymin, ymax)
 coord. ref. : +proj=utm +zone=43 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
 values      : conductance 
 matrix class: dsCMatrix 

Step 3: I used a geographic correction approach on the transition layer. Details here

> julcorr <- geoCorrection(tr, type = "r", multpl=F)
> julcorr
 class       : TransitionLayer 
 dimensions  : 642, 382, 245244  (nrow, ncol, ncell)
 resolution  : 1000, 1000  (x, y)
 extent      : 461951, 843951, 892583.3, 1534583  (xmin, xmax, ymin, ymax)
 coord. ref. : +proj=utm +zone=43 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
values      : conductance 
matrix class: dsCMatrix

I then used the commuteDistance function as mentioned here and I get this error as shown below.

> jul_comm <- commuteDistance(julcorr, centroids_jul)
Error in LU.dgC(a) : cs_lu(A) failed: near-singular A (or out of memory)
In addition: Warning message:
In .rD(x, coords) :
  46 out of 47 locations were found in the fully connected transition matrix. NAs introduced.

Ultimately, I would like to run a function that does an operation similar to CIRCUITSCAPE, but within R and then use these results for further analyses.

8
  • I have the same exact issue, although it came through the use of another package before gdistance. It appears that the creation of a negative number instead of a positive one at some point in the algorithm creates the issue. Have you found a solution?! Commented Mar 22, 2018 at 20:22
  • @Ouistiti Still waiting on a solution actually. Did you make sure that they were all in the same projection system? Commented Mar 23, 2018 at 19:09
  • Yes I made sure. I also tried the following: 1) replaced all the NA 2) ran on a single core (to see if there was an issue with one of the parallelizing packages). I do not have any other ideas. Commented Mar 25, 2018 at 4:37
  • Could you give us the traceback? Commented Mar 27, 2018 at 5:22
  • Did you find a solution?Because actually I have the same problem
    – Aridai
    Commented Aug 28, 2018 at 19:01

2 Answers 2

0

I've tested the package and it gives that error when you try to compute commuteDistance with points laying in the NA region of a raster. Here is an example. From a DEM I created a grid of 4x4 to pick the centroids. You can see in the image that there are 3 points in the NA region of the raster.

Here the image of the selected points and raster:

enter image description here

When you compute all, you will see exactly that error, that 13 out of 16 are fully connected, or that 3 are not connected, exactly the poisitions without values but in the extent of the raster.

Warning message:
In .rD(x, coords) :
  13 out of 16 locations were found in the fully connected transition matrix. NAs introduced.

Here is the code i've used:

library(raster)
library(gdistance)
library(mapview)

r <- raster("./data/dtm.tif")

# raster to spatial points - this doesnt include NA values
jul <- r

# Does r contain NA values ?
NA %in% values(r)

# Create a perfect grid over the extent of the raster
grid <- raster::raster(extent(r), nrow = 4, ncol = 4)

# Convert the grid to SpatialPoints
centroids_jul_sel <- as(grid, "SpatialPoints")
crs(centroids_jul_sel) <- crs(r)

# Show centroids (some out, some in)
mapview(r)+centroids_jul_sel

# check if any NA value
terra::extract(r, centroids_jul_sel)

# transition layer
tr <- transition(jul, mean, directions = 8)

# correction
julcorr <- geoCorrection(tr, type = "r", multpl=F)

# commuteDistance
jul_comm <- commuteDistance(julcorr, centroids_jul_sel)

The possible solutions:

  1. Remove or replace the points in NA raster values (you could check that with extract function.
  2. Interpolate or asign a value to the NA raster cell so it makes sense for the computation.
  3. Don't worry about it. Just remember that your matrix will have NA values.

enter image description here

Here is the code and data for this example.

-1

I've had this error pop up before and it turned out that one of my sample locations did not overlap with the raster I provided. Go back and double check that all points fall within the raster cells. Even if the points are within the raster extent, they may be falling on NA cells.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.