You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在R中生成州界内动物运动相关随机路径的问题求助

动物相关随机路径模拟问题:性能优化与边界约束解决方案

我正在基于实际动物追踪数据,在州域范围内生成模拟真实运动的相关随机路径(CRW),用于提取环境协变量开展建模工作。已尝试sf、move、adehabitat系列包,参考了Fletcher和Fortin(2018)《资源选择》章节及包示例,但遇到两个核心问题:

  • 州域范围大、数据量多(单只动物约1600个点位,总数据64000个),模拟时程序频繁崩溃或运行耗时极久;
  • 模拟路径超出预设的州界范围。

使用的R包

library(dplyr); 
library(raster); 
library(sf); 
library(sp); 
library(mapview); 
library(lubridate); 
library(tidyverse); 
library(adehabitatLT); 
library(adehabitatHR) 

数据样例

animal.id   timestamp              lat            long
1          2019-09-22 16:03       43.44296      -105.8370
1          2019-09-29 16:23       43.47755      -105.8217
2          2019-08-31 09:18       41.44881      -109.8222

尝试的adehabitat代码片段

data(animal_data)

# 加载高程TIFF并转为空间像素数据框
par <- raster("D:/R/ELEV_30.tif")
par <- as(par, "SpatialPixelsDataFrame")

# 定义处理与约束函数(未实现具体逻辑)
myfunc <- function(animal_data, par)
consfun <- function(animal_data, par)
par(mar = c(0,0,0,0))

# 绘制边界并处理数据坐标
image(par)
map <- par
lines(animal_data[,1], animal_data[,2], lwd=2)
rxy <- apply(coordinates(par),2,range)
coordinates(animal_data) <- animal_data[,1:2]

# 格式化时间并创建ltraj对象
animal_data$timestamp <- as.POSIXct(animal_data$timestamp, format = "%Y-%m-%d %H:%M")
animal.final <- animal_data %>%   
  mutate(timestamp = force_tz(timestamp, "UTC"))

animal.traj <- as.ltraj(xy = animal_data[,c('long', 'lat')], date = animal_data[,'timestamp'], id = animal_data[,'animal.id'],
                        typeII = TRUE,
                        infolocs = animal_data[,c(1,2)])

# 生成相关随机路径(10次迭代)
animal.CRW <- NMs.randomCRW(animal.traj, rangles=TRUE, rdist=TRUE, fixedStart = TRUE,
                            x0 = NULL, rx = NULL, ry = NULL,
                            treatment.func = myfunc,
                            treatment.par = map, constraint.func =consfun,
                            constraint.par = map, nrep=10)

# 绘图与输出结果
plot.ltraj(animal.traj)
plot.ltraj(animal.CRW)
par(mfrow = c(3,3))
tmp <- testNM(animal.CRW)

write.csv(animal.CRW, file = "random path.csv", row.names = FALSE)

补充问题(hab包尝试)

尝试使用hab包时触发内存错误:

Error in h(simpleError(msg, call)) :
error in evaluating the argument 'obj' in selecting a method for function 'coordinates': cannot allocate vector of size 16.0 Gb

单只动物数据运行即出现该错误或程序崩溃,想知道这是仅需增加电脑内存即可解决,还是存在代码/方法层面的问题?

需求

  • 如何通过逻辑限制(如if语句)确保模拟路径不超出州界范围;
  • 解决程序崩溃、运行耗时过久的优化方案。

内容的提问来源于stack exchange,提问作者sparverius23

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 14:32:36