BlasBenito / spatialRF

R package to fit spatial models with Random Forest
https://blasbenito.github.io/spatialRF/
109 stars 16 forks source link

Issues with distance matrix #20

Closed jenifferteles closed 5 months ago

jenifferteles commented 5 months ago

Hello, I hope everything is fine? Could you please help me? I'm trying to run the function spatialRF::rf, but it's not recognizing my distance matrix as numeric, I believe, because when I remove the line it runs normally. How can I fix this issue, please? Thank you in advance.

Here's the script and the error below: dependent.variable.name <- "SR_log" predictor.variable.names <- c("fishing_log" , "calcite" , "bathym" , "chlomean", "curvel", "tempSS", "sal" )

library(raster) dist_data <- pointDistance(coords[, c("x", "y")], lonlat = FALSE) distance.matrix <- dist_data head(distance.matrix)

distance thresholds (same units as distance_matrix)

distance.thresholds <- c(0, 6, 12, 19)

random seed for reproducibility

random.seed <- 1

names(dados_OK)[names(dados_OK) == "Longitude"] <- "x" names(dados_OK)[names(dados_OK) == "Latitude"] <- "y"

xy <- dados_OK[, c("x", "y")]

library(spatialRF) library(ggplot2)

Finding promising variable interactions

interactions <- spatialRF::the_feature_engineer( data = dados_OK, dependent.variable.name = dependent.variable.name, predictor.variable.names = predictor.variable.names, xy = xy, importance.threshold = 0.50, #uses 50% best predictors cor.threshold = 0.60, #max corr between interactions and predictors seed = random.seed, repetitions = 100, verbose = TRUE)

kableExtra::kbl( head(interactions$screening, 10), format = "html" ) %>% kableExtra::kable_paper("hover", full_width = F)

adding interaction column to the training data

dados_OK <- interactions$data

adding interaction name to predictor.variable.names

predictor.variable.names <- interactions$predictor.variable.names

model.non.spatial <- spatialRF::rf( data = dados_OK, dependent.variable.name = dependent.variable.name, predictor.variable.names = predictor.variable.names, distance.matrix = distance.matrix, distance.thresholds = distance.thresholds, xy = xy, #not needed by rf, but other functions read it from the model seed = random.seed, verbose = FALSE)

ISSUE AFTER THIS PART: Error in moran_multithreshold(x = m$residuals$values, distance.matrix = distance.matrix, : Argument 'x' must be a numeric vector.