Error when running terra::predict(). Caught exception 'std::bad_alloc' in function 'MakeADFunObject'

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

Error when running terra::predict(). Caught exception 'std::bad_alloc' in function 'MakeADFunObject'

问题

I tried to use the predict() function in the terra package to make a prediction out of a glmmTMB result. I have 4 spatraster files in R environment and I stack them together as raster stack for the prediction. 1 of the 4 spatraster is categorical. I would post my code below.

The error message I have received is:

> Error in MakeADFunObject(data, parameters, reportenv, ADreport = ADreport,  : 
> Caught exception 'std::bad_alloc' in function 'MakeADFunObject'

I actually have successfully run the prediction once when I am still trying things out. But then when I run the exact same code, this error occurred.

From what I learned online, this could mean I run out of memory or storage, which is super weird.
I have tried to clean the R memory and environment with gc() and rm(). I also tried to enlarge the memory size for R (which I do not know if I have succeeded in because I still have the same error after it). I have also updated the terra package to the newest one. Nothing helps.

The code I run:

> envi <- c(dist_building, dist_road, dist_coast, habitat_type)
> 
> names(envi) <- c('dist_building','dist_road','dist_coast','habitat_type')
> #those are my rasters, and habitat_type is a categorical raster, which I define by terra::as.factor()
> 
> prediction_oland_tuned <- terra::predict(object = envi,
>                                          model = model_oland_tuned, 
>                                          type = "response", #I have also tried not to include this, did NOT help
>                                          const = data.frame(ID=NA),
>                                          re.form = NA)

Please help.... Thanks in advance.

英文:

I tried to use the predict() function in the terra package to make a prediction out of a glmmTMB result. I have 4 spatraster files in R environment and I stack them together as raster stack for the prediction. 1 of the 4 spatraster is categorical. I would post my code below.

The error message I have received is:

> Error in MakeADFunObject(data, parameters, reportenv, ADreport = ADreport,  : 
> Caught exception 'std::bad_alloc' in function 'MakeADFunObject'

I actually have successfully run the prediction once when I am still trying things out. But then when I run the exact same code, this error occurred.

From what I learned online, this could mean I run out of memory or storage, which is super weird.
I have tried to clean the R memory and environment with gc() and rm(). I also tried to enlarge the memory size for R (which I do not know if I have succeeded in because I still have the same error after it). I have also updated the terra package to the newest one. Nothing helps.

The code I run:

> envi <- c(dist_building, dist_road, dist_coast, habitat_type)
> 
> names(envi) <- c('dist_building','dist_road','dist_coast','habitat_type')
> #those are my rasters, and habitat_type is a categorical raster, which I define by terra::as.factor()
> 
> prediction_oland_tuned <- terra::predict(object = envi,
>                                          model = model_oland_tuned, 
>                                          type = "response", #I have also tried not to include this, did NOT help
>                                          const = data.frame(ID=NA),
>                                          re.form = NA)

Please help.... Thanks in advance.

答案1

得分: 1

The apparent problem is that

MakeADFunObject(data, parameters, reportenv, ADreport = ADreport,  )

tries to allocate a vector (in C++) that is too large (there is no contiguous area in RAM where it can be created).

As you do not provide a reproducible example, it is hard to say more. What class does model_oland_tuned have (what package is it from)?

You may be able to solve this indirectly, by making terra::predict use smaller chunks by lowering the amount of memory it is allowed to use (see terraOptions). You can also use additional argument steps to a largish number (predict will then process the data in at least as many chunks).

p <- terra::predict(object = envi, model_oland_tuned, type="response", wopt=list(steps=10))
英文:

The apparent problem is that

MakeADFunObject(data, parameters, reportenv, ADreport = ADreport,  )

tries to allocate a vector (in C++) that is too large (there is no contiguous area in RAM where it can be created).

As you do not provide a reproducible example, it is hard to say more. What class does model_oland_tuned have (what package is it from)?

You may be able to solve this indirectly, by making terra::predict use smaller chunks by lowering the amount of memory it is allowed to use (see terraOptions). You can also use additional argument steps to a largish number (predict will then process the data in at least as many chunks).

p <- terra::predict(object = envi, model_oland_tuned, type="response", wopt=list(steps=10))

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

发表评论

匿名网友

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

确定