1

I am trying to plot the output from predict function in stars package. But it is throwing error

library(stars)

tif = system.file("tif/L7_ETMs.tif", package = "stars")
i = read_stars(tif, proxy = TRUE) %>%
  split()
nclus = 5

sam = st_sample(i, 1000)
k = kmeans(na.omit(as.data.frame(sam)[, -c(1:2)]), nclus)
out = predict(i, k)
plot(out, col = sf.colors(nclus, categorical=TRUE))

prediction on array(s) X1,X2,X3,X4,X5,X6' failed; will try to split() dimension y' over attributes Error in split.stars(object) : length(x) == 1 is not TRUE

sessionInfo()
R version 4.2.1 (2022-06-23 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale:
[1] LC_COLLATE=English_India.utf8  LC_CTYPE=English_India.utf8   
[3] LC_MONETARY=English_India.utf8 LC_NUMERIC=C                  
[5] LC_TIME=English_India.utf8    

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

other attached packages:
[1] caret_6.0-92    lattice_0.20-45 ggplot2_3.3.6   MASS_7.3-57    
[5] openxlsx_4.2.5  stars_0.5-5     sf_1.0-7        abind_1.4-5    

loaded via a namespace (and not attached):
 [1] nlme_3.1-157         bitops_1.0-7         bit64_4.0.5         
 [4] lubridate_1.8.0      doParallel_1.0.17    tools_4.2.1         
 [7] utf8_1.2.2           R6_2.5.1             rpart_4.1.16        
[10] KernSmooth_2.23-20   DBI_1.1.3            colorspace_2.0-3    
[13] nnet_7.3-17          withr_2.5.0          gbm_2.1.8           
[16] tidyselect_1.1.2     gridExtra_2.3        bit_4.0.4           
[19] compiler_4.2.1       cli_3.3.0            scales_1.2.0        
[22] classInt_0.4-7       randomForest_4.7-1.1 proxy_0.4-27        
[25] plotmo_3.6.2         stringr_1.4.0        digest_0.6.29       
[28] rmarkdown_2.14       pkgconfig_2.0.3      htmltools_0.5.2     
[31] parallelly_1.32.0    plotrix_3.8-2        fastmap_1.1.0       
[34] rlang_1.0.3          rstudioapi_0.13      generics_0.1.3      
[37] jsonlite_1.8.0       dplyr_1.0.9          ModelMetrics_1.2.2.2
[40] zip_2.2.0            RCurl_1.98-1.7       magrittr_2.0.3      
[43] Formula_1.2-4        Matrix_1.4-1         Rcpp_1.0.8.3        
[46] munsell_0.5.0        fansi_1.0.3          lifecycle_1.0.1     
[49] terra_1.6-1          stringi_1.7.6        pROC_1.18.0         
[52] yaml_2.3.5           plyr_1.8.7           recipes_0.2.0       
[55] grid_4.2.1           earth_5.3.1          parallel_4.2.1      
[58] listenv_0.8.0        crayon_1.5.1         splines_4.2.1       
[61] knitr_1.39           pillar_1.7.0         ranger_0.13.1       
[64] xgboost_1.6.0.1      future.apply_1.9.0   reshape2_1.4.4      
[67] codetools_0.2-18     stats4_4.2.1         glue_1.6.2          
[70] evaluate_0.15        data.table_1.14.2    BiocManager_1.30.18 
[73] vctrs_0.4.1          foreach_1.5.2        gtable_0.3.0        
[76] purrr_0.3.4          kernlab_0.9-31       future_1.26.1       
[79] assertthat_0.2.1     TeachingDemos_2.12   xfun_0.31           
[82] gower_1.0.0          prodlim_2019.11.13   h2o_3.36.1.2        
[85] lwgeom_0.2-8         e1071_1.7-11         vip_0.3.2           
[88] class_7.3-20         survival_3.3-1       timeDate_3043.102   
[91] tibble_3.1.7         iterators_1.0.14     fastAdaboost_1.0.0  
[94] hardhat_1.2.0        units_0.8-0          lava_1.6.10         
[97] globals_0.15.1       ellipsis_0.3.2       ipred_0.9-12   
UseR10085
  • 7,120
  • 3
  • 24
  • 54
  • I can produce same error with your code above, trying `%>% split(., 'band')` and will report. And don't understand your `sam)[, -c(1:2)])` in the context of a numeric vector... – Chris Jul 16 '22 at 15:33
  • I think you want this sequence `i = read_stars(tif) %>% split("band") %>% as.data.frame() %>% head()`, and them move on the sampling and predict [stars 7](https://r-spatial.github.io/stars/articles/stars7.html), from what I read. – Chris Jul 16 '22 at 16:05
  • I have put that issue on GitHub and the author replied that I have to provide a predict method for kmeans objects like `library(clue) predict.kmeans = function(object, newdata, ...) { unclass(clue::cl_predict(object, newdata[, -c(1:2)], ...)) }`. Actually, I was trying to implement Support Vector Machines with Radial Basis Function Kernel with `caret` R package and was getting the same error. Is it like that for every model we have to create a custom predict function? Is the predict function of `stars` package not compatible with `caret` object? – UseR10085 Jul 16 '22 at 16:31

0 Answers0