connectscape/Makurhini

Can't successfully calculate node deltas

Opened this issue · 3 comments

Hi Oscar, thanks for this package, which I'm just getting to know this week.
I've got many aspects working, but I would like to be able to calculate per-patch deltas via MK_ProtConnMulti(). I'm getting an error message "Error in table construction". At line 197 and 198 of MK_ProtConnMulti() the code tries to access the third and fourth elements of a protconn_result, when that result only ever has two elements (the viewer panel and the geo objects). This then causes a 'subscript out of bounds' error, but that's masked by the trycatch() wrapper.
Am I missing something, that my protconn_results only have these two elements? Or, should the code not be trying to access elements 3 and 4?
Hoping you can help and I can continue getting to know Makurhini! Thanks very much,
Eric Goodwin

This works OK with delta=F, but seems to fail with delta = T

Hi EricOGoodwin, could you successfully run dECA? I'm having some issues, if you were able to run it successfully, could you share your experience?

The iusse is this "Error in .subset2(x, i, exact = exact) : subscript out of bounds"

I directly read the shapefile (.shp) and then calculate the dECA.
my R code is as follows:

nodes <- readShapeSpatial("xx.shp")
dECA_test <- MK_dECA(
nodes= nodes,
attribute = nodes@data$SS,
area_unit = "km2",
distance = list(type= "edge"),
metric = "PC",
probability = 0.05,
distance_thresholds = 5000,
write = "xx.csv"
)
dECA_test

shapefile data like this:

str(nodes)
Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
..@ data :'data.frame': 13 obs. of 2 variables:
.. ..$ ID_11: Factor w/ 13 levels "3328650","3329004",..: 1 2 3 4 5 6 7 8 9 10 ...
.. ..$ SS : int [1:13] 0 0 1 3 1 3 1 1 1 1 ...
.. ..- attr(*, "data_types")= chr [1:2] "C" "N"
..@ polygons :List of 13

Thanks for getting back to me.
I dont have access to the deprecated maptools::readShapeSpatial(), so have been using sf::read_sf().
I then have to cast the object with sf::as_Spatial(), to pass it in to MK_dECA().
Within MK_dECA(), I get past the line that causes the .subset2() error if I pass it the sf version of the object:

listT <- purrr::compact(sf::st_as_sf(nodes))

However, then stepping further through the code I get an error at

listT <- lapply(listT, function(x) {
if (class(x)[1] != "sf") {
x <- st_as_sf(x)
x$IdTemp <- 1:nrow(x)
}
else {
x$IdTemp <- 1:nrow(x)
}
return(x)
})

Error in UseMethod("st_as_sf") :
no applicable method for 'st_as_sf' applied to an object of class "character"

Using the apply() function on the listT (compacted version of nodes?) it accesses the list elements of that data object, the first of which is the values of the first attribute, which in my case is character type. I wonder, are you expecting compact() to return a list? It returns just a shapefile, but if I change that line to

listT <- purrr::compact(sf::st_as_sf(list(nodes)))

Then I get past that lapply() chunk too. (Is it because I'm operating with just one time point?)

Until I get to the line
DECA <- cbind(time, LA, DECA)

Where there's no current object "LA", so that element's left null, and it then trips up trying to assign the third name, etc etc.

That MK_dECA() is meant for comparing change over time, right? Is ProtConnMulti() also mostly intended to compare through time? I'd like to be able to run a 'panel' as opposed to longitudinal study, just at one point in time, but comparing different areas. I wonder if that's the source of some of my trouble?

Thanks again,

Eric