#********************************************************************** # # FILENAME: Example1.R # PURPOSE: Illustrate the calculation of the HR indices # # AUTHORS: John Fieberg # #**********************************************************************; # load kernsmooth library library(KernSmooth) # bkde2D, dpik (plug-in estimate) library(adehabitat) # use data from this library to illustrate functions # Change working directory (needs to point to directory holding Indices.R and calcHR.R) # Uncomment the line below, and insert the directory name # setwd("") # Load necessary functions source("Indices.txt") source("CalcHR.txt") # Data from adehabitat library (saved as a .csv file) locs<-read.csv("puechabon_locs.csv") animal1<-locs[locs$Name=="Brock",c("X", "Y")] animal2<-locs[locs$Name=="Calou",c("X", "Y")] # First, find reasonable axis limits # Use 2 times the bandwidth to set min and max ranges.... htemp<-apply(rbind(animal1,animal2),2,dpik) min.x<-min(min(animal1$X), min(animal2$X))-2*htemp[1] max.x<-max(max(animal1$X), max(animal2$X))+2*htemp[1] min.y<-min(min(animal1$Y), min(animal2$Y))-2*htemp[2] max.y<-max(max(animal1$Y), max(animal2$Y))+2*htemp[2] # Calculate Home ranges with plug in method (separate bandwidths in x and y directions) UD1<-bkde2D(animal1,apply(animal1,2,dpik), gridsize=c(101, 101), range.x=list(c(min.x,max.x), c(min.y, max.y))) UD2<-bkde2D(animal2,apply(animal2,2,dpik), gridsize=c(101, 101), range.x=list(c(min.x,max.x), c(min.y, max.y))) # Now, get 50% and 95% contour info using function calcHR # Returns: # 1. HR = HR size (for each contour level; default= 50%, 95%) # 2. ps = probability level associated with the pth probability contour # 3. totp = total probability level (should be close to 1 if grid size is adequate) animal1hr<-calcHR(UD1) animal2hr<-calcHR(UD2) # Get grid cell size dxdy<-(UD1$x1[2]-UD1$x1[1])*(UD1$x2[2]-UD1$x2[1]) # Get 50% and 95% conditional distributions UD1.50<-UD1$fhat UD1.50[UD1.50