[R-sig-eco] issues with getvolumeUD adehabitat

Plumb, Reid@Wildlife Reid.Plumb at wildlife.ca.gov
Mon Feb 1 21:31:08 CET 2016


Hi,

I am having trouble with the getvolumeUD function in adehabitat.  I have a GPS dataset with many individuals that have thousands of locations for each individual.  The main problem I am having is that when I iterate based on the individual unique ID, the getvolumeUD function throws an error saying that the kud.out object is not a "estUD or estUDm".  This is false as I have used class() of the object and environment window in R-studio to make sure that it is in the proper format.  I had a similar problem with the kernelUD function not recognizing that the kud.tmp1 object (SpatialPointsDataFrame) had more than 5 locations even though it had 50 in the object.  I got around this by changing the SPDF to an object of SpatialPoints.  It then seemed to recognize the locations.  I don't think I have a way around this problem and have hit a road block.  Do some packages not function properly as newer version of R are produced?  My code is below and sample dataset attached.  Any help/suggestions/ideas and much appreciated.

library(adehabitatHR)
library(KernSmooth)
library(maptools)
library(raster)
library(rgdal)
library(maptools)
library(shapefiles)

setwd("G:/MA_research/Analysis/RUF/RUF_results/Output_data/2013/NW/BS_2013_p5/BBMM_UD/BBMM_ascii")
kuddata = read.csv(file.choose(),header=T,sep=",") #setting data souce and using associated headers.
coordinates(kuddata) = c("X","Y") #tells R that the X,Y colums are the coordinates.  In order for the next proj4string line to work the BBMMdata needs to be transformed into a formal SpatialPointsDataFrame like this code does.
proj4string(kuddata) = CRS("+init=epsg:32614") #32614 is the espg code for WGS84 UTM14N.  You have to tell R what projection the point data is in.
kud_h = matrix(NA,length(unique(kuddata$Unique_ID)),2) #creating a list that holds the variances given by each UD calculation
for(i in 1:length(unique(kuddata$Unique_ID))){
  kud.tmp=kuddata[which(kuddata$Unique_ID==unique(kuddata$Unique_ID)[i]),] #subsetting data by each Unique_ID and for each year
  kud.tmp1=kud.tmp[sample(1:nrow(kud.tmp), 50, replace = FALSE),] # Taking a random sample of 50 relocations of the subset data
  writePointsShape(kud.tmp1, paste(kud.tmp1$Unique_ID[1],"_", "pts",sep="")) # writing the pt shape file for the random sample
  dpikdata = cbind(kud.tmp1$X, kud.tmp1$Y) #creating a list with X, Y data from the random sample
  dpiklocs = unlist(dpikdata) #unlisting the data so that it is a matrix
  dpik_h = dpik(dpiklocs, gridsize = 30) # estimating the data driven direct Plug-In Bandwidth
  ext = extent(min(kud.tmp1$X)-3000, max(kud.tmp1$X)+3000, min(kud.tmp1$Y)-3000, max(kud.tmp1$Y)+3000) # making a raster extent
  ras=raster(ext, resolution = 30) # creating the raster
  SPDF = as(ras, "SpatialPixelsDataFrame") # turns the raster to a spatial pixel dataframe for kud
  kud_h[i,1]=as.character(kud.tmp1$Unique_ID[1])
  kud_h[i,2]=dpik_h[1]
  kud.tmp2 = SpatialPoints(kud.tmp1)
  kud.out <- kernelUD(kud.tmp2, h = dpik_h, grid = SPDF) # building the kernel
  chickenud<-getvolumeUD(kud.out[[1]])
  chickenras <- raster(as(chickenud, "SpatialPixelsDataFrame"))
  contour = rasterToContour(chickenras, levels = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99))
  contour2 = SpatialLines2PolySet(contour) # making a Polyset object from SpatialLines format
  iso_shape = PolySet2SpatialPolygons(contour2, close_polys=TRUE) # making polygons at each UD level
  isoIDs <- sapply(slot(iso_shape, "polygons"), function(x) slot(x, "ID")) #extracting polygon IDs
  isodf <- data.frame(rep(0, length(isoIDs)), row.names=isoIDs) #create dataframe with correct rownames
  iso_shape2 <- SpatialPolygonsDataFrame(iso_shape, isodf) #coerse dataframe to SPDF
  writePolyShape(iso_shape2, paste(kud.tmp$Unique_ID[1],"_",bbmm.tmp$Year[1],"_iso", sep="")) # write iso polygons
  chicken1<-as.data.frame(chickenud)
  names(chicken1)<-c("UD","X","Y")
  chicken1$include1[chicken1$UD < 99.00001] <- 1
  IND1<-na.omit(chicken1)
  IND1$UD2<-100-IND1$UD
  coordinates(IND1) = c("X","Y")
  proj4string(IND1) = CRS("+init=epsg:32614")
  writePointsShape(IND1, paste(kud.tmp$Unique_ID[1],"_", bbmm.tmp$Year[1], "_rufpts",sep=""))
}
h = as.data.frame(kud_h)
write.csv(h ,file = "kernel_h.csv") # write a csv of all bandwidths

Thanks,
Reid



More information about the R-sig-ecology mailing list