AleksandarSekulic / RFSI

Random Forest Spatial Interpolation
45 stars 12 forks source link

problem running example #3

Closed fipoucat closed 1 year ago

fipoucat commented 1 year ago

Hi,

I found your interesting package and installed it. While trying to run the example I am having an error: [1] "Preparing data ..." Error in data.prepare(data = data, data.staid.x.y.z = data.staid.x.y.z, : The argument data/newdata must be of sf, sftime, SpatVector, SpatRaster (as newdata only), data.frame class! What's the cause and what to do?

AleksandarSekulic commented 1 year ago

Hi,

Can you send me a code example? As it says, the data must be of sf, sftime, SpatVector, SpatRaster (as newdata only), data.frame class! I suppose that the data or newdata is not in one of these formats. Check an example from R meteo package.

fipoucat commented 1 year ago

This code:

library(meteo) library(sp) library(spacetime) library(gstat) library(plyr) library(xts) library(snowfall) library(doParallel) library(CAST) library(ranger) library(sf)

preparing data

demo(meuse, echo=FALSE) meuse <- meuse[complete.cases(meuse@data),]

#################### rfsi ####################

fm.RFSI <- as.formula("zinc ~ dist + soil + ffreq")

rfsi_model <- rfsi(formula = fm.RFSI, data = meuse, zero.tol = 0, n.obs = 5, # number of nearest observations s.crs = NA, # or meuse@proj4string # nedded only if in lon/lat (WGS84) t.crs = NA, # or meuse@proj4string # nedded only if in lon/lat (WGS84) cpus = detectCores()-1, progress = TRUE,

ranger parameters

               importance = "impurity",
               seed = 42,
               num.trees = 250,
               mtry = 5,
               splitrule = "variance",
               min.node.size = 5,
               sample.fraction = 0.95,
               quantreg = FALSE)

rfsi_model

Note that OOB error statistics are biased and should not be considered as accuracy metrics

(they do not show spatial accuracy)!

The proper way to assess accuaracy of the RFSI model is by using the nested k-fold

cross-validation (cv.rfsi function)

sort(rfsi_model$variable.importance)

#################### pred.rfsi ####################

rfsi_prediction <- pred.rfsi(model = rfsi_model, data = meuse, # meuse.df (use data.staid.x.y.time) zcol = "zinc", newdata = meuse.grid, # meuse.grid.df (use newdata.staid.x.y.time) output.format = "SpatialPixelsDataFrame", zero.tol = 0, s.crs = meuse@proj4string, # NA newdata.s.crs = meuse@proj4string, # NA t.crs = meuse@proj4string, # NA cpus = detectCores()-1, progress = TRUE )

spplot(rfsi_prediction['pred'])

AleksandarSekulic commented 1 year ago

The code haven't been updated to new version of R meteo package. It's updated now. You can find all examples here. Thank you for noticing this.