Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
34b6e5d
commit 97d0de3
Showing
14 changed files
with
564 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
"angles" <- function (x, id = levels(x$id), burst = levels(x$burst), | ||
date = NULL, slsp = c("remove", "missing")) | ||
{ | ||
## The function is deprecated | ||
.Deprecated("as.ltraj") | ||
|
||
## Verifications | ||
if (!inherits(x, "traj")) | ||
stop("x should be of class \"traj\"") | ||
slsp <- match.arg(slsp) | ||
|
||
|
||
## prepangles is used to remove successive relocations | ||
## located at the same place | ||
prepangles <- function(x) | ||
{ | ||
## Verifications | ||
if (!inherits(x, "traj")) | ||
stop("x should be of class \"traj\"") | ||
|
||
## split per burst | ||
li <- split(x, x$burst) | ||
|
||
## keeps only the successive relocations at different places | ||
foo <- function(y) { | ||
oo <- unlist(lapply(2:nrow(y), | ||
function(i) | ||
(!all(y[i,c("x","y")]==y[i-1,c("x","y")])))) | ||
oo <- c(TRUE,oo) | ||
y <- y[oo,] | ||
} | ||
|
||
## output | ||
res <- do.call("rbind", lapply(li, foo)) | ||
return(res) | ||
} | ||
|
||
## gets the selected bursts | ||
x <- getburst(x, burst = burst, id = id, date = date) | ||
|
||
## if the angles are to be removed when successive relocations are | ||
## on the same place | ||
if (slsp=="remove") | ||
x <- prepangles(x) | ||
|
||
## split per burst | ||
li <- split(x, x$burst) | ||
|
||
## To compute the angles | ||
foo <- function(x) { | ||
|
||
## gets the coordinates | ||
xy<-as.matrix(x[,c("x","y")]) | ||
ang<-1:(nrow(xy)-2) | ||
|
||
|
||
for (i in 2:(nrow(xy)-1)) { | ||
|
||
## current relocation, with the previous one | ||
na <- 0 | ||
ref1<-xy[i-1,] | ||
|
||
## the origin of the space is placed on the previous reloc | ||
xyb1<-t(t(xy)-ref1) | ||
ang1<--atan2(xyb1[i,2],xyb1[i,1]) | ||
|
||
## Position of rotated x2 and x3 | ||
x2<-c(sqrt(sum(xyb1[i,]^2)), 0) | ||
if (sum(abs(x2)) < 1e-7) | ||
na<-1 | ||
x3b<-x3<-xyb1[i+1,] | ||
x3b[1]= cos(ang1)*x3[1] - sin(ang1)*x3[2] | ||
x3b[2]= sin(ang1)*x3[1] + cos(ang1)*x3[2] | ||
x3<-x3b | ||
|
||
## Computation of the angles | ||
x3<-x3-x2 | ||
if (sum(abs(x3)) < 1e-7) | ||
na<-1 | ||
ang[i-1]<-atan2(x3[2],x3[1]) | ||
if (na > 0.5) | ||
if (slsp == "missing") | ||
ang[i - 1] <- NA | ||
} | ||
|
||
## output | ||
so<-data.frame(id=x$id[-c(1,nrow(xy))], | ||
x=xy[-c(1,nrow(xy)),1], | ||
y=xy[-c(1,nrow(xy)),2], | ||
date=x$date[-c(1,nrow(xy))], | ||
burst=x$burst[-c(1,nrow(xy))], | ||
angles=ang) | ||
} | ||
|
||
## output | ||
lo <- do.call("rbind", lapply(li, foo)) | ||
row.names(lo) <- 1:nrow(lo) | ||
return(lo) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
kerneloverlap <- function(xy, id = NULL, | ||
method = c("HR", "PHR", "VI", "BA", "UDOI", "HD"), | ||
lev=95, conditional=FALSE, ...) | ||
{ | ||
## Verifications | ||
method <- match.arg(method) | ||
|
||
## UD estimation | ||
x <- kernelUD(xy, id, same4all=TRUE, ...) | ||
vol <- getvolumeUD(x) | ||
|
||
## Matrix of results | ||
res <- matrix(0, ncol=length(x), nrow=length(x)) | ||
|
||
## loop for each animal | ||
for (i in 1:length(x)) { | ||
for (j in 1:i) { | ||
|
||
if (method=="HR") { | ||
vi <- vol[[i]]$UD | ||
vj <- vol[[j]]$UD | ||
vi[vi<=lev] <- 1 | ||
vi[vi>lev] <- 0 | ||
vj[vj<=lev] <- 1 | ||
vj[vj>lev] <- 0 | ||
vk <- vi*vj | ||
res[i,j] <- sum(vk)/sum(vi) | ||
res[j,i] <- sum(vk)/sum(vj) | ||
} | ||
|
||
if (method=="PHR") { | ||
vi <- x[[i]]$UD | ||
vj <- x[[j]]$UD | ||
ai <- vol[[i]]$UD | ||
aj <- vol[[j]]$UD | ||
ai[ai<=lev] <- 1 | ||
ai[ai>lev] <- 0 | ||
aj[aj<=lev] <- 1 | ||
aj[aj>lev] <- 0 | ||
if (conditional) { | ||
vi <- vi*ai | ||
vj <- vj*aj | ||
res[j,i] <- sum(vi*aj)*(attr(vi,"cellsize")^2) | ||
res[i,j] <- sum(vj*ai)*(attr(vi,"cellsize")^2) | ||
} else { | ||
res[j,i] <- sum(vi*aj)*(attr(vi,"cellsize")^2) | ||
res[i,j] <- sum(vj*ai)*(attr(vi,"cellsize")^2) | ||
} | ||
} | ||
|
||
|
||
|
||
if (method=="VI") { | ||
vi <- c(x[[i]]$UD) | ||
vj <- c(x[[j]]$UD) | ||
ai <- vol[[i]]$UD | ||
aj <- vol[[j]]$UD | ||
ai[ai<=lev] <- 1 | ||
ai[ai>lev] <- 0 | ||
aj[aj<=lev] <- 1 | ||
aj[aj>lev] <- 0 | ||
if (conditional) { | ||
vi <- vi*ai | ||
vj <- vj*aj | ||
res[i,j] <- res[j,i] <- sum(pmin(vi, vj))*(attr(x[[i]]$UD,"cellsize")^2) | ||
} else { | ||
res[i,j] <- res[j,i] <- sum(pmin(vi, vj))*(attr(x[[i]]$UD,"cellsize")^2) | ||
} | ||
} | ||
|
||
if (method=="BA") { | ||
vi <- x[[i]]$UD | ||
vj <- x[[j]]$UD | ||
ai <- vol[[i]]$UD | ||
aj <- vol[[j]]$UD | ||
ai[ai<=lev] <- 1 | ||
ai[ai>lev] <- 0 | ||
aj[aj<=lev] <- 1 | ||
aj[aj>lev] <- 0 | ||
if (conditional) { | ||
vi <- vi*ai | ||
vj <- vj*aj | ||
res[j,i] <- res[i,j] <- sum(sqrt(vi)*sqrt(vj))*(attr(vi,"cellsize")^2) | ||
} else { | ||
res[j,i] <- res[i,j] <- sum(sqrt(vi)*sqrt(vj))*(attr(vi,"cellsize")^2) | ||
} | ||
} | ||
|
||
if (method=="UDOI") { | ||
vi <- x[[i]]$UD | ||
vj <- x[[j]]$UD | ||
ai <- vol[[i]]$UD | ||
aj <- vol[[j]]$UD | ||
ai[ai<=lev] <- 1 | ||
ai[ai>lev] <- 0 | ||
aj[aj<=lev] <- 1 | ||
aj[aj>lev] <- 0 | ||
if (conditional) { | ||
vi <- vi*ai | ||
vj <- vj*aj | ||
ak <- sum(ai*aj)*(attr(vi,"cellsize")^2) | ||
res[j,i] <- res[i,j] <- ak * sum(vi*vj)*(attr(vi,"cellsize")^2) | ||
} else { | ||
ak <- sum(ai*aj)*(attr(vi,"cellsize")^2) | ||
res[j,i] <- res[i,j] <- ak * sum(vi*vj)*(attr(vi,"cellsize")^2) | ||
} | ||
} | ||
|
||
if (method=="HD") { | ||
vi <- x[[i]]$UD | ||
vj <- x[[j]]$UD | ||
ai <- vol[[i]]$UD | ||
aj <- vol[[j]]$UD | ||
ai[ai<=lev] <- 1 | ||
ai[ai>lev] <- 0 | ||
aj[aj<=lev] <- 1 | ||
aj[aj>lev] <- 0 | ||
if (conditional) { | ||
vi <- vi*ai | ||
vj <- vj*aj | ||
res[j,i] <- res[i,j] <- sqrt(sum((sqrt(vi) - sqrt(vj))^2*(attr(vi,"cellsize")^2))) | ||
} else { | ||
res[j,i] <- res[i,j] <- sqrt(sum((sqrt(vi) - sqrt(vj))^2*(attr(vi,"cellsize")^2))) | ||
} | ||
} | ||
} | ||
} | ||
rownames(res) <- names(x) | ||
colnames(res) <- names(x) | ||
return(res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
"speed" <- function(x, id=levels(x$id), burst=levels(x$burst), | ||
date=NULL, units=c("seconds", "hours","days")) | ||
{ | ||
## Verifications | ||
.Deprecated("as.ltraj") | ||
if (!inherits(x, "traj")) | ||
stop("should be an object of class traj") | ||
units<-match.arg(units) | ||
|
||
## Selection of dates | ||
x<-getburst(x, burst=burst, id=id, date=date) | ||
|
||
## distances between successives relocations | ||
li<-split(x, x$burst) | ||
foo<-function(x) { | ||
x1<-x[-1,] | ||
x2<-x[-nrow(x),] | ||
dist<-sqrt( (x1$x-x2$x)^2 + (x1$y-x2$y)^2) | ||
hour<-(unclass(x1$date)-unclass(x2$date)) | ||
if (units=="hours") | ||
hour<-(unclass(x1$date)-unclass(x2$date))/3600 | ||
if (units=="days") | ||
hour<-(unclass(x1$date)-unclass(x2$date))/(3600*24) | ||
disx<-(x1$x-x2$x) | ||
disy<-(x1$y-x2$y) | ||
so<-cbind.data.frame(id=x2$id,x=x2$x, y=x2$y, date=x2$date, | ||
burst=x2$burst, | ||
sp.x=disx/hour, sp.y=disy/hour, | ||
speed=dist/hour, dt=hour) | ||
return(so) | ||
} | ||
|
||
## Output | ||
lo<-do.call("rbind", lapply(li, foo)) | ||
row.names(lo)<-1:nrow(lo) | ||
return(lo) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Oops, something went wrong.