connectscape/Makurhini

can't install

Closed this issue · 4 comments

cperk commented

hi!

I tried to install your package but am getting an error:

Error: .onLoad failed in loadNamespace() for 'geojsonlint', details:
call: make_context(private$console)
error: function 'Rcpp_precious_remove' not provided by package 'Rcpp'
Execution halted
ERROR: lazy loading failed for package ‘Makurhini’

  • removing ‘/Library/Frameworks/R.framework/Versions/4.0/Resources/library/Makurhini’
    Error: Failed to install 'Makurhini' from GitHub:
    (converted from warning) installation of package ‘/var/folders/sn/hw405d1n4694ky_zvwhjlbkc0000gn/T//Rtmphp0PLy/file4c71e4c5c99/Makurhini_2.0.4.tar.gz’ had non-zero exit status

Do you know what I should do to fix it?

cperk commented

Nevermind, i went back to your closed issues and found the solution (I updated terra, Rcpp and sf)!

Is it ok that I get the message "replacing previous import ‘raster::buffer’ by ‘terra::buffer’ when loading ‘Makurhini’ "? A similar message appeared when installing.

cperk commented

Hi again,

I am really excited to use your package and have been trying to figure out a way to convert my dataframe of pairwise distances between nodes to be the distance matrix for MK_dPCIIC(). However, i am having trouble figuring out the correct format. Here are the first few rows of the dataframe. Each node is represented in both the "to" and "from" direction, so for example the minimum distance from node 1 to node 10 is also represented the opposite way, from node 10 to node 1.

Do you know what I would have to do to convert this dataframe?

Thank you!

-Carrie
Screen Shot 2021-11-28 at 10 08 44 PM

cperk commented

(to clarify, these are cost distances that I already calculated using gDistance)

I want to use the argument type="least-cost" in your package but am just a little turned around on exactly how to do this. Was I not supposed to already calculate cost distances?

Hi Carrie! Thanks for using Makurhini!
Glad to hear you solved the installation problem.

  1. Is it ok that I get the message "replacing previous import ‘raster::buffer’ by ‘terra::buffer’ when loading ‘Makurhini’ "?...
    No problem with that message, many raster and terra functions have the same name, we use :: to call the correct package function.

  2. You can use as input a data.frame with the values of the nodes, i.e. an id field and an attribute field. You can also provide a distance matrix instead of a paired data.frame with the distances.

EXAMPLES:
library(Makurhini)

1. Using a data.frame and a distance matrix

Open spatial polygons:
data("vegetation_patches", package = "Makurhini")

Note. You could open your files using sf package:
e.g., vegetation_patches <- read_sf("directory/shapefile.shp")

#Generate id
vegetation_patches$id <- 1:nrow(vegetation_patches)

Distance matrix

I will use a human footprint of Mexico as resistance

HFP_Mexico <- raster(system.file("extdata", "HFP_Mexico.tif", package = "Makurhini", mustWork = TRUE))

Crop raster to the extent of my patches
mask_1 <- as(extent(vegetation_patches), 'SpatialPolygons')
crs(mask_1) <- crs(vegetation_patches)
mask_1 <- buffer(mask_1, 20000)
HFP_Mexico <- crop(HFP_Mexico, mask_1)

Distance matrix:
distance_vegetation <- distancefile(vegetation_patches, id = "id", type = "least-cost", resistance = HFP_Mexico, pairwise = FALSE)
distance_vegetation # It's a distance matrix

NOTE. You need a matrix distance that you could obtain with igraph or using the distancefile() function of Makurhini that use as input an spatial object (shapefile or raster)

NOTE. You can try to convert your dataframe to a distance matrix, for example:
Carrie_distance_matrix <- as.dist(xtabs(Carrie.dataframe[, "minimum_dist"] ~ Carrie.dataframe[, "toNode"] + Carrie.dataframe[, "toNode"]))

We continue the example: Nodes data.frame

Estimate the attribute, in this case we will use the area of the patches:

vegetation_patches$attribute <- st_area(vegetation_patches) %>% as.numeric()
vegetation_patches.df <- st_drop_geometry(vegetation_patches) %>% as.data.frame()
head(vegetation_patches.df) #With id and attribute fields

##Apply the MK_dPCIIC()
IIC <- MK_dPCIIC(nodes = vegetation_patches.df, attribute = "attribute", area_unit = "m2", distance = distance_vegetation, metric = "IIC", distance_thresholds = c(2000, 4000, 6000, 8000, 10000)) #1:10 km
IIC

2. Using spatial format

Open spatial polygons
data("vegetation_patches", package = "Makurhini")

Again, I will open a human footprint of Mexico:

HFP_Mexico <- raster(system.file("extdata", "HFP_Mexico.tif", package = "Makurhini", mustWork = TRUE))

Crop raster to the extent of my patches
mask_1 <- as(extent(vegetation_patches), 'SpatialPolygons')
crs(mask_1) <- crs(vegetation_patches)
mask_1 <- buffer(mask_1, 20000)
HFP_Mexico <- crop(HFP_Mexico, mask_1)

Apply MK_dPCIIC()

IIC <- MK_dPCIIC(nodes = vegetation_patches, attribute = NULL, area_unit = "m2", distance = list(type = "least-cost", resistance = HFP_Mexico), metric = "IIC", distance_thresholds = c(2000, 4000, 6000, 8000, 10000)) #1:10 km
IIC

PC <- MK_dPCIIC(nodes = vegetation_patches, attribute = NULL, area_unit = "m2", distance = list(type = "least-cost", resistance = HFP_Mexico), metric = "PC", probability = 0.5, distance_thresholds = c(2000, 4000, 6000, 8000, 10000)) #1:10 km
PC

Best,
Oscar