Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when using environmental RasterLayer and not RasterStack #20

Closed
Rekyt opened this issue Mar 26, 2018 · 2 comments
Closed

Error when using environmental RasterLayer and not RasterStack #20

Rekyt opened this issue Mar 26, 2018 · 2 comments

Comments

@Rekyt
Copy link
Contributor

Rekyt commented Mar 26, 2018

Hi @sylvainschmitt ,
Thank you for building SSDM!
Because I'm working on simple simulations with a single variable I discovered a simple bug: when using a RasterLayer as the Env variable in the function modelling() I got an error.

Error in modelling("GLM", occ, env_raster, Xcol = "x", Ycol = "y", Pcol = "presence") : 
  Environment data set is not a raster or a raster stack

Here is a minimal reprex:

Minimal Reproductible Example
library("SSDM")
#> Welcome to the SSDM package, you can launch the graphical user interface by typing gui() in the console.
library("raster")
#> Le chargement a nécessité le package : sp

env_raster = raster(matrix(1:10))  # Create simple environmental gradient

occ = data.frame(presence = c(rep(1, 9), 0), cell = 1:10)  # Dummy occurences

# Extract cell coordinates
cell_xy = xyFromCell(env_raster, occ$cell)
occ$x = cell_xy[, "x"]
occ$y = cell_xy[, "y"]

# Not working model ###
raster_layer = modelling("GLM", occ, env_raster, Xcol = "x", Ycol = "y",
                         Pcol = "presence")
#> Data check ...
#> Error in modelling("GLM", occ, env_raster, Xcol = "x", Ycol = "y", Pcol = "presence"): Environment data set is not a raster or a raster stack

# Working model ####
raster_stack = modelling("GLM", occ, stack(env_raster), Xcol = "x", Ycol = "y",
                         Pcol = "presence")
#> Data check ... 
#>    done. 
#> 
#> Model evaluation...
#>    done. 
#> 
#> Model projection...
#>    done. 
#> 
#> Model axes contribution evaluation...
#>    done.

My workaround is currently to wrap my RasterLayer into a call of stack().
But it would great if it wouldn't be necessary when you only need a single environmental variable.

Possible bug origin: I think the problem comes from

if (!inherits(Env, "RasterStack")) {

It could be replaced by if (!inherits(Env, "Raster")) I'm going to check if it doesn't break anything before making a PR ;)

@Rekyt
Copy link
Contributor Author

Rekyt commented Mar 26, 2018

If you want to have a look, I did something at https://github.com/Rekyt/SSDM/tree/fix_raster_layer

Sidenote: I added context() to all tests to fix r-lib/testthat#700

I'm having issues currently with

values <- data.frame(extract(Env, cbind(obj@data$X, obj@data$Y)))

Because it creates weird variable names for RasterLayer such as extract.Env..cbind.obj.data.X..obj.data.Y.. and seems to make modelling() fail.

@sylvainschmitt
Copy link
Owner

There was supposed to be a security for this case that does not seems to work:

SSDM/R/modelling.R

Lines 295 to 297 in 939b53e

if (is.raster(Env)) {
Env <- stack(Env)
}

I will fix it but anyway I would not much use an SDM with only one predictor.

sylvainschmitt pushed a commit that referenced this issue Mar 30, 2018
fixed issue #20 from @Rekyt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants