I am trying to create correlated random walk models that are bounded within polygons (100% MCP home ranges), using the NMs.randomCRW function from the adehabitatLT package. I noticed a similar topic "How to Bound Correlated Random Walk Models within a Polygon using R", but our issues appear to be different.
My NMs.randomCRW models are created no problem. When I use the TestNM function to test the NMs models, it only works effectively when no functions are in my model (quickly each time). When I test my null models with a constraint function (constrained to a polygon), it only appears to 'work' <2% of the time (and so far, for only one animal). Most of the time, the TestNM model runs for several hours before I stop the function (figuring it wasn't working properly)... the <2% of the time it works, I get exactly what I want within 2 minutes (or less), making me suspect that my code is correct. (Image here shows randomCRW constrained to polygon (left), actual trajectory in polygon (right): Constrained randomCRW
Does anyone know what my issue may be? Is this a bug with the TestNM function? How does the testNM work? Does it keep running until it 'finds a model' that fits my Null Model?
My code is as follows:
library(adehabitatLT)
library(dplyr)
library(sp)
## Load data and create PFTraj
fc<-read.csv("C:/Users/Brennan/Documents/DataManagement/AnimalswithENV.csv")
da <- as.character(fc$TIMESTAMP_)
dapf <- as.POSIXct(da)
da$dapf <- dapf
PFTraj <- adehabitatLT::as.ltraj(xy = fc[, c("Long_", "Lat")], date=dapf, id=fc$tag_ident)
## Create MCP home Range, 100%
PFs <- structure(list(longitude = fc$Long_, latitude = fc$Lat, BirdID = fc$tag_ident, DateTime = fc$TIMESTAMP_), .Names = c('longitude', 'latitude', 'BirdID', 'DateTime'), class="data.frame", row.names= c(NA, -93382L))
xy <- PFs[,c(1,2)]
PFs2 <- PFs[,c(3,4)]
PFsp <- SpatialPointsDataFrame(coords=xy, data= PFs2, proj4string = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))
cp2 <- adehabitatHR::mcp(PFsp[,1], percent = 100)
#Use a consistent projection
proj4string = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
## My Constraint Function
confun <- function(x, par) {
coordinates(x) <- x[,1:2]
proj4string(x) <- proj4string(par)
ov <- over(x, par)
return(all(!is.na(ov)))
}
#Extract 1 bird from PFTraj and SpatialPolygonsDataFrame (~2000 relocations)
eA1 <- PFTraj[1]
Bird1 <- cp2[cp2@data$id=="3117",]
Bird1 <- as(Bird1, "SpatialPolygons")
## Create Null model and test it
xo1 <- coordinates(Bird1)
nmo1B <- NMs.randomCRW(na.omit(eA1), rangles = TRUE, rdist = TRUE, fixedStart = TRUE,
rx = range(xo1[,1]), ry = range(xo1[,2]), treatment.func = NULL,
treatment.par = NULL, constraint.func = confun, constraint.par = Bird1,
nrep = 1)
resu <- testNM(nmo1B, count = FALSE)
## Plot results
resul <- data.frame(resu)
spresu<- SpatialPointsDataFrame(coords=resul[,c(1,2)], data=resul, proj4string = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))
plot(Bird1)
plot(spresu, add=TRUE)
Again, this code HAS produced the randomCRW I want, just not very often, and only for one animal so far. HOWEVER, when I replicate this code for my 2nd animal, the TestNM function doesn't complete, even after hours of the test running...
Any help would be appreciated,
Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire