0

我对卫星标记的海龟有 608 次观察。我想用包括海面温度、当前速度、风速等在内的环境数据对这些进行建模。当然,标记和环境数据都会在空间和时间上发生变化。我使用从这里改编的下面的代码生成了伪缺席数据。但是,我现在意识到我生成的数据点只是空间样本。有什么方法可以编辑此代码以进行临时采样,以便生成的 csv 具有每个点的日期/时间,以便我可以将其与我的环境数据相匹配?或者,我可以尝试一个不同的包来允许我这样做吗?

dir.create(path = "data")

library("sp")
library("raster")
library("maptools")
library("rgdal")
library("dismo")

bioclim.data <- getData(name = "worldclim",
                        var = "bio",
                        res = 2.5,
                        path = "data/")

# Read in observations
obs.data <- read.csv(file = "data/Presence.csv")


# Determine geographic extent of data
max.lat <- ceiling(max(obs.data$Latitude))
min.lat <- floor(min(obs.data$Latitude))
max.lon <- ceiling(max(obs.data$Longitude))
min.lon <- floor(min(obs.data$Longitude))
geographic.extent <- extent(x = c(min.lon, max.lon, min.lat, max.lat))


# Use the bioclim data files for sampling resolution
bil.files <- list.files(path = "data/wc2-5", 
                        pattern = "*.bil$", 
                        full.names = TRUE)

# only need one file, so use the first one in the list of .bil files
mask <- raster(bil.files[1])

# Randomly sample points (same number as our observed points)
background <- randomPoints(mask = mask,     # Provides resolution of sampling points
                           n = nrow(obs.data),      # Number of random points
                           ext = geographic.extent, # Spatially restricts sampling
                           extf = 1.25)             # Expands sampling a little bit


write.csv(background, "pseudo-absence.csv")
4

1 回答 1

0

我通过简单地使用下面的代码生成随机时间并将结果和上面的 .csv 合并来解决了这个问题。

#ADD TIMES 

time.start <- as.POSIXct('2014-12-01T01:00:00z', format = "%Y-%m-%dT%H:%M:%S")
time.end <- as.POSIXct('2015-04-30T01:00:00z', format = "%Y-%m-%dT%H:%M:%S")

seconds <- difftime(time.end, time.start, units = "secs")
# Option with runif()
v <- round(runif(6000, 0, seconds))

# Option with sample()
v <- sample(1:seconds, 6000, replace = T)

time.uniform <- time.start + v   

write.csv(time.uniform, "time.csv")

tag<-read.csv("pseudo-absence.csv")

time<- read.csv("time.csv")

myfulldata = merge(tag,time)

write.csv(myfulldata, "pseudo-absence_with_time.csv")
于 2019-03-03T18:35:44.497 回答