Error using predict function terra package in R 在R中使用terra包的predict函数时发生错误

huangapple go评论89阅读模式
英文:

Error using predict function terra package in R

问题

I get an error when trying to perform a spatial prediction using a previously fitted GLM model and a SpatRaster containing the environmental predictors. Does anyone know what could be causing this error?

this is my code:

  1. env <- rast("data/rasters/predictors.tif")
  2. cv_data <- SDM_list_new[[2477804]]$simple_glm
  3. pr_cur_prob <- terra::predict(env, cv_data, cores = 1, type = "response")
  4. #Error in .runModel(model, fun, d, nl, const, na.rm, index, ...) :
  5. # argument "cores" is missing, with no default

I have updated the terra package and also tried running the code with changing the number of the "cores" argument or omitting the cores argument, which did not help.

英文:

I get an error when trying to perform a spatial prediction using a previously fitted GLM model and a SpatRaster containing the environmental predictors. Does anyone know what could be causing this error?

this is my code:

  1. env <- rast("data/rasters/predictors.tif")
  2. cv_data <- SDM_list_new[[2477804]]$simple_glm
  3. pr_cur_prob <- terra::predict(env, cv_data, cores = 1, type = "response")
  4. #Error in .runModel(model, fun, d, nl, const, na.rm, index, ...) :
  5. # argument "cores" is missing, with no default

I have updated the terra package and also tried running the code with changing the number of the "cores" argument or omitting the cores argument, which did not help.

答案1

得分: 1

以下是您要翻译的部分:

无法回答您的问题,因为您没有提供一个最小的自包含可重现示例。我们也不知道您正在使用哪些其他包以及它们的版本是什么。我猜测这可能是由于其他包的干扰导致的。因此,我建议尝试在不加载不必要的包的情况下运行此代码(然后可能再次添加它们以查明问题出在哪里)。

以下代码(来自?terra::predict)对我来说可以正常运行:

  1. library(terra)
  2. # terra 1.7.29
  3. logo <- rast(system.file("ex/logo.tif", package="terra"))
  4. names(logo) <- c("red", "green", "blue")
  5. p <- matrix(c(48, 48, 48, 53, 50, 46, 54, 70, 84, 85, 74, 84, 95, 85,
  6. 66, 42, 26, 4, 19, 17, 7, 14, 26, 29, 39, 45, 51, 56, 46, 38, 31,
  7. 22, 34, 60, 70, 73, 63, 46, 43, 28), ncol=2)
  8. a <- matrix(c(22, 33, 64, 85, 92, 94, 59, 27, 30, 64, 60, 33, 31, 9,
  9. 99, 67, 15, 5, 4, 30, 8, 37, 42, 27, 19, 69, 60, 73, 3, 5, 21,
  10. 37, 52, 70, 74, 9, 13, 4, 17, 47), ncol=2)
  11. xy <- rbind(cbind(1, p), cbind(0, a))
  12. e <- extract(logo, xy[,2:3])
  13. v <- data.frame(cbind(pa=xy[,1], e))
  14. model <- glm(formula=pa~., data=v)
  15. r0 <- predict(logo, model)
  16. r1 <- predict(logo, model, cores=1)
  17. r2 <- predict(logo, model, cores=2)

希望这对您有所帮助。

英文:

It is not possible to answer your question as you do not include a minimal self-contained reproducible example. We also do not know what other packages you are using, and what their versions are. I am guessing that this happens because of some interference from another package. So I would suggest trying this without loading packages that are not strictly needed (and then perhaps adding them again to see where things go wrong).

The below (from ?terra::predict) works fine for me.

  1. library(terra)
  2. #terra 1.7.29
  3. logo &lt;- rast(system.file(&quot;ex/logo.tif&quot;, package=&quot;terra&quot;))
  4. names(logo) &lt;- c(&quot;red&quot;, &quot;green&quot;, &quot;blue&quot;)
  5. p &lt;- matrix(c(48, 48, 48, 53, 50, 46, 54, 70, 84, 85, 74, 84, 95, 85,
  6. 66, 42, 26, 4, 19, 17, 7, 14, 26, 29, 39, 45, 51, 56, 46, 38, 31,
  7. 22, 34, 60, 70, 73, 63, 46, 43, 28), ncol=2)
  8. a &lt;- matrix(c(22, 33, 64, 85, 92, 94, 59, 27, 30, 64, 60, 33, 31, 9,
  9. 99, 67, 15, 5, 4, 30, 8, 37, 42, 27, 19, 69, 60, 73, 3, 5, 21,
  10. 37, 52, 70, 74, 9, 13, 4, 17, 47), ncol=2)
  11. xy &lt;- rbind(cbind(1, p), cbind(0, a))
  12. e &lt;- extract(logo, xy[,2:3])
  13. v &lt;- data.frame(cbind(pa=xy[,1], e))
  14. model &lt;- glm(formula=pa~., data=v)
  15. r0 &lt;- predict(logo, model)
  16. r1 &lt;- predict(logo, model, cores=1)
  17. r2 &lt;- predict(logo, model, cores=2)

huangapple
  • 本文由 发表于 2023年5月21日 18:18:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76299380.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定