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

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

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:

env <- rast("data/rasters/predictors.tif")
cv_data <- SDM_list_new[[2477804]]$simple_glm
pr_cur_prob <- terra::predict(env, cv_data, cores = 1, type = "response")
#Error in .runModel(model, fun, d, nl, const, na.rm, index, ...) :
#    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:

env <- rast("data/rasters/predictors.tif")
cv_data <- SDM_list_new[[2477804]]$simple_glm
pr_cur_prob <- terra::predict(env, cv_data, cores = 1, type = "response")
#Error in .runModel(model, fun, d, nl, const, na.rm, index, ...) :
#    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)对我来说可以正常运行:

library(terra)
# terra 1.7.29
logo <- rast(system.file("ex/logo.tif", package="terra"))
names(logo) <- c("red", "green", "blue")
p <- matrix(c(48, 48, 48, 53, 50, 46, 54, 70, 84, 85, 74, 84, 95, 85, 
   66, 42, 26, 4, 19, 17, 7, 14, 26, 29, 39, 45, 51, 56, 46, 38, 31, 
   22, 34, 60, 70, 73, 63, 46, 43, 28), ncol=2)

a <- matrix(c(22, 33, 64, 85, 92, 94, 59, 27, 30, 64, 60, 33, 31, 9,
   99, 67, 15, 5, 4, 30, 8, 37, 42, 27, 19, 69, 60, 73, 3, 5, 21,
   37, 52, 70, 74, 9, 13, 4, 17, 47), ncol=2)
xy <- rbind(cbind(1, p), cbind(0, a))
e <- extract(logo, xy[,2:3])
v <- data.frame(cbind(pa=xy[,1], e))
model <- glm(formula=pa~., data=v)

r0 <- predict(logo, model)
r1 <- predict(logo, model, cores=1)
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.

library(terra)
#terra 1.7.29
logo &lt;- rast(system.file(&quot;ex/logo.tif&quot;, package=&quot;terra&quot;))   
names(logo) &lt;- c(&quot;red&quot;, &quot;green&quot;, &quot;blue&quot;)
p &lt;- matrix(c(48, 48, 48, 53, 50, 46, 54, 70, 84, 85, 74, 84, 95, 85, 
   66, 42, 26, 4, 19, 17, 7, 14, 26, 29, 39, 45, 51, 56, 46, 38, 31, 
   22, 34, 60, 70, 73, 63, 46, 43, 28), ncol=2)

a &lt;- matrix(c(22, 33, 64, 85, 92, 94, 59, 27, 30, 64, 60, 33, 31, 9,
   99, 67, 15, 5, 4, 30, 8, 37, 42, 27, 19, 69, 60, 73, 3, 5, 21,
   37, 52, 70, 74, 9, 13, 4, 17, 47), ncol=2)
xy &lt;- rbind(cbind(1, p), cbind(0, a))
e &lt;- extract(logo, xy[,2:3])
v &lt;- data.frame(cbind(pa=xy[,1], e))
model &lt;- glm(formula=pa~., data=v)

r0 &lt;- predict(logo, model)
r1 &lt;- predict(logo, model, cores=1)
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:

确定