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

predict function issue #22

Open
pablohs27 opened this issue Jan 25, 2024 · 8 comments
Open

predict function issue #22

pablohs27 opened this issue Jan 25, 2024 · 8 comments

Comments

@pablohs27
Copy link

Hi, I am trying to run the predict function following the same steps as in the exercise but I get the following error:

predicted <- predict(sdm.model,
envs.tars %>% sf::st_crop(bb),
clamp = clamp, type = type)

Error in [.stars(newdata, , v) :
selecting using invalid value label(s)?

Everything else I managed to replicate without problem. But I have not been able to solve this error.

@btupper
Copy link
Collaborator

btupper commented Jan 25, 2024

Hi,

Hmmm, it's hard to know with the information provided. When you say "the exercise", which one do you mean? Could you provide a link to the line in the tutorial?

I do have a first guess though. Is it possible that the names of each variable (also known as attribute) in your env.tars stars object are not the same as the ones used to build the model sdm.model?

@pablohs27
Copy link
Author

I am in the "Predicting" section of the Marine Species Distribution Model (SDM) Tutorial (https://oceanhackweek.org/tutorials_marine_sdm/SDM/Turtle_maxnet.html)
Here the structure of my variables and model:

image
image

@pablohs27
Copy link
Author

And these are the names of the variables with which I built the model. They are the same as in the env.stars object

image
But it keeps giving me the same error

image

@btupper
Copy link
Collaborator

btupper commented Jan 26, 2024

Those definitely look like a match to me. Next to test is the crop of env.stars - debugging is always just a series of little steps ruling out possibilities.

Does this throw an error when you run it?

cropped = env.stars |> sf::st_crop(bb)
cropped

@btupper
Copy link
Collaborator

btupper commented Jan 26, 2024

I have stepped through this markdown and I can't replicate the error you see. That makes me wonder if there is a difference in the versions in the stars packages we are using. Here is the output of my sessionInfo(), can you share the same from your computer with the rest of us?

I seem to be running stars version 0.6-4

> sessionInfo()
R version 4.3.1 (2023-06-16)
Platform: x86_64-apple-darwin20 (64-bit)
Running under: macOS Sonoma 14.2.1

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: America/New_York
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
 [1] rnaturalearthdata_0.1.0 DT_0.31                
 [3] cmocean_0.3-1           geodata_0.5-9          
 [5] terra_1.7-55            stars_0.6-4            
 [7] abind_1.4-5             maxnet_0.1.4           
 [9] dismo_1.3-14            sdmpredictors_0.2.15   
[11] here_1.0.1              raster_3.6-20          
[13] sp_1.6-1                mapview_2.11.0         
[15] robis_2.11.3            ggspatial_1.1.9        
[17] janitor_2.2.0           lubridate_1.9.2        
[19] forcats_1.0.0           stringr_1.5.1          
[21] dplyr_1.1.4             purrr_1.0.2            
[23] readr_2.1.4             tidyr_1.3.0            
[25] tibble_3.2.1            ggplot2_3.4.4          
[27] tidyverse_2.0.0         rnaturalearth_0.3.3    
[29] sf_1.0-14              

loaded via a namespace (and not attached):
 [1] RColorBrewer_1.1-3      rstudioapi_0.14        
 [3] jsonlite_1.8.7          wk_0.9.1               
 [5] shape_1.4.6             magrittr_2.0.3         
 [7] farver_2.1.1            rmarkdown_2.21         
 [9] vctrs_0.6.5             base64enc_0.1-3        
[11] webshot_0.5.5           htmltools_0.5.5        
[13] curl_5.1.0              s2_1.1.6               
[15] KernSmooth_2.23-21      htmlwidgets_1.6.2      
[17] uuid_1.1-0              mime_0.12              
[19] lifecycle_1.0.4         iterators_1.0.14       
[21] pkgconfig_2.0.3         Matrix_1.5-4.1         
[23] R6_2.5.1                fastmap_1.1.1          
[25] shiny_1.7.4             snakecase_0.11.1       
[27] digest_0.6.33           colorspace_2.1-0       
[29] rprojroot_2.0.3         leafem_0.2.0           
[31] crosstalk_1.2.0         labeling_0.4.3         
[33] fansi_1.0.6             timechange_0.2.0       
[35] httr_1.4.7              compiler_4.3.1         
[37] proxy_0.4-27            bit64_4.0.5            
[39] withr_2.5.2             brew_1.0-8             
[41] DBI_1.1.3               leaflet_2.2.0          
[43] classInt_0.4-10         tools_4.3.1            
[45] units_0.8-5             mapedit_0.6.0          
[47] httpuv_1.6.9            glue_1.6.2             
[49] satellite_1.0.4         promises_1.2.0.1       
[51] grid_4.3.1              generics_0.1.3         
[53] gtable_0.3.4            leaflet.providers_1.9.0
[55] tzdb_0.4.0              class_7.3-22           
[57] data.table_1.14.8       hms_1.1.3              
[59] xml2_1.3.5              utf8_1.2.4             
[61] foreach_1.5.2           pillar_1.9.0           
[63] vroom_1.6.5             later_1.3.0            
[65] splines_4.3.1           lattice_0.21-8         
[67] survival_3.5-5          bit_4.0.5              
[69] tidyselect_1.2.0        knitr_1.42             
[71] svglite_2.1.1           stats4_4.3.1           
[73] xfun_0.40               leafpop_0.1.0          
[75] stringi_1.8.1           yaml_2.3.7             
[77] evaluate_0.20           codetools_0.2-19       
[79] cli_3.6.2               xtable_1.8-4           
[81] systemfonts_1.0.4       munsell_0.5.0          
[83] jquerylib_0.1.4         Rcpp_1.0.11            
[85] png_0.1-8               parallel_4.3.1         
[87] ellipsis_0.3.2          glmnet_4.1-8           
[89] scales_1.2.1            e1071_1.7-14           
[91] crayon_1.5.2            rlang_1.1.2   

@pablohs27
Copy link
Author

I have run
cropped = env.stars |> sf::st_crop(bb)
cropped

and works.

I reviewed the "predict" function of the raster package in detail and came across other examples of models where the attributes within it are ordered differently. First the environmental variables (env.stars) and then the model (sdm.model). I ran the following code and it worked:

cropped = env.stars |> sf::st_crop(bb)
predicted <- predict(cropped, sdm.model, clamp = TRUE, type = "cloglog")

image

Is that correct?

Finally, here is the output of my sessionInfo() :

image

@btupper
Copy link
Collaborator

btupper commented Jan 26, 2024

Well, it seems we are using the same version of stars, so that helps. And glad to know that cropping works.

I am not aware that the [dismo] package supports maxnet class models. But even if it did, it would need to have the model come first. The tutorial you are following produces a maxnet (not maxent) class model.

The predict() function actually is a wrapper function for another function specifically written for your model type that must exist somewhere in your current session of R. It will look for a function named predict.classname_goes_here() The classname looked for is the first one listed in the class hierarchy when you run the class() function.

class(sdm.model)
## [1] "maxnet" "lognet" "glmnet"

So in this case R will look for predict.maxnet() That function can be found here…
https://github.com/BigelowLab/maxnet/blob/master/R/predict.maxnet.R I am not sure what predict function you are using that correctly works on a maxent class model without the model being the first argument. So, what you found puzzles me - especially since it seems to work! @eeholmes do you know what might be happening here?

P.S. Other packages may provide predict functions for other classes of models. During any session of R you can list the available predict functions using...

methods(predict)
 ##  [1] predict,ANY-method                predict,Bioclim-method           
 ##  [3] predict,CircleHull-method         predict,CirclesRange-method      
 ##  [5] predict,ConvexHull-method         predict,Domain-method            
 ##  [7] predict,EcoLim-method             predict,GeographicDistance-method
 ##  [9] predict,InvDistWeightModel-method predict,Mahalanobis-method       
 ## [11] predict,MaxEnt-method             predict,MaxEntReplicates-method  
 ## [13] predict,Raster-method             predict,RectangularHull-method   
 ## [15] predict,SpatRaster-method         predict,VoronoiHull-method       
 ## [17] predict.ar*                       predict.Arima*                   
 ## [19] predict.arima0*                   predict.bs*                      
 ## [21] predict.bSpline*                  predict.coxnet*                  
 ## [23] predict.coxph*                    predict.coxph.penal*             
 ## [25] predict.coxphms*                  predict.cv.glmnet*               
 ## [27] predict.cv.relaxed*               predict.elnet*                   
 ## [29] predict.fishnet*                  predict.gknn*                    
 ## [31] predict.glm                       predict.glmnet*                  
 ## [33] predict.glmnetfit*                predict.HoltWinters*             
 ## [35] predict.lca*                      predict.lm                       
 ## [37] predict.loess*                    predict.lognet*                  
 ## [39] predict.maxnet*                   predict.mlm*                     
 ## [41] predict.mrelnet*                  predict.multnet*                 
 ## [43] predict.naiveBayes*               predict.nbSpline*                
 ## [45] predict.nls*                      predict.npolySpline*             
 ## [47] predict.ns*                       predict.pbSpline*                
 ## [49] predict.poly*                     predict.polySpline*              
 ## [51] predict.ppolySpline*              predict.ppr*                     
 ## [53] predict.prcomp*                   predict.princomp*                
 ## [55] predict.pspline*                  predict.relaxed*                 
 ## [57] predict.smooth.spline*            predict.smooth.spline.fit*       
 ## [59] predict.stars*                    predict.stars_proxy*             
 ## [61] predict.StructTS*                 predict.survreg*                 
 ## [63] predict.survreg.penal*            predict.svm*                     
 ## see '?methods' for accessing help and source code

@btupper
Copy link
Collaborator

btupper commented Jan 26, 2024

Ooooh, here's what might be happening...

There is a predict.stars() function that accepts arguments in the order you listed them: stars object followed by model object. This function is also a wrapper, and deep inside it calls the generic predict() with the arguments reversed. That means that eventually the arguments land in the predict.maxnet() function. Aha! That's how you got this to work...

predicted <- predict(cropped, sdm.model, clamp = TRUE, type = "cloglog")

But we still don't know why your original attempt, like that shown below, fails.

predicted <- predict(sdm.model, cropped, clamp = clamp, type = type)

I hate to ask, but have you tried restarting your session of R and knitting the tutorial? If you do that does it still throw an error?

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