We will start with simply creating trajectories between successive locations. As stated above, there are 2 types of trajectories but their are also 2 forms of Type II trajectories if we have time recorded. Depending on the duration between locations we can have uniform time lag between successive relocations termed regular trajectories and non-uniform time lag that results in irregular trajectories. We will begin this section with simply creating irregular trajectories from relocation data because, even though we set up a time schedule to collection locations at uniform times, climate, habitat, and satellites do not always permit such schedules of data collection.
- Exercise 3.2 - Download and extract zip folder into your preferred location
- Set working directory to the extracted folder in R under File - Change dir...
- First we need to load the packages needed for the exercise
library(adehabitatLT)
library(chron)
library(spatstat)#for "duplicate" function - Now open the script "MovementsScript.R" and run code directly from the script
#We are again going to be using more of the mule deer dataset than from the earlier exercises
muleys <-read.csv("DCmuleysedited.csv", header=T)
str(muleys) - Check for duplicate locations in dataset. The reason for this is very important and will be apparent shortly.
summary(duplicated(muleys))
#Sort data to address error in code if needed
#muleys <- muleys[order(muleys$id),] - For trajectories of type II (time recorded), the conversion of the date to the format POSIX needs to be done to get proper digits of date into R.
da <- as.POSIXct(strptime(muleys$GPSFixTime,format="%Y.%m.%d %H:%M:%S"))
head(da)
muleys$da <- da #attach "da" to muleys dataset
str(muleys)
#Create time lag between successive locations to censor data if needed.
timediff <- diff(muleys$da)
muleys <-muleys[-1,]
muleys$timediff <-as.numeric(abs(timediff))
str(muleys)#check to see timediff column was added to muleys
#Look at number of locations by animal ID
summary(muleys$id)
#Remove outlier locations or known outliers collected too far apart in time
newmuleys <-subset(muleys, muleys$X > 599000 & muleys$X < 705000
& muleys$Y > 4167000 & muleys$timediff < 14401)
muleys <- newmuleys
str(muleys) - Let's create a Spatial Points Data Frame in UTM zone 12 adding ID, time diff, burst to xy coordinates
data.xy = muleys[c("X","Y")]
#Creates class Spatial Points for all locations
xysp <- SpatialPoints(data.xy)
#proj4string(xysp) <- CRS("+proj=utm +zone=12 +ellps=WGS84")
#Creates a Spatial Data Frame from
sppt<-data.frame(xysp)
#Creates a spatial data frame of ID
idsp<-data.frame(muleys[2])
#Creates a spatial data frame of dt
dtsp<-data.frame(muleys[24])
#Creates a spatial data frame of Burst
busp<-data.frame(muleys[23])
#Merges ID and Date into the same spatial data frame
merge<-data.frame(idsp,dtsp,busp)
#Adds ID and Date data frame with locations data frame
coordinates(merge)<-sppt
plot(merge)#visualize data
str(merge) - Now create an object of class "ltraj" by animal using the ID field and display by each individual (i.e., ltraj[1]).
ltraj <- as.ltraj(coordinates(merge),merge$da,id=merge$id)
plot(ltraj)
plot(ltraj[1])
head(ltraj[1])#Describes the trajectory for the first deer
#> head(ltraj[1])#Describes the trajectory
*********** List of class ltraj ***********
#Type of the traject: Type II (time recorded)
#Irregular traject. Variable time lag between two locs
#
#Characteristics of the bursts:
# id burst nb.reloc NAs date.begin date.end
#1 D12 D12 100 0 2011-10-12 06:00:52 2011-10-24 21:00:48
#
#infolocs provided. The following variables are available:
#[1] "pkey"
plot(ltraj[2])
plot(ltraj[3])
plot(ltraj[4])
plot(ltraj[5]
plot(ltraj[6])
plot(ltraj[7]) - Let's create a histogram of time lag (i.e., interval) and distance between successive locations for each deer. This is a nice way to inspect the time lag between locations as you don't want to include a location if too much time has passed since the previous and it also shows why a trajectory is irregular.
hist(ltraj[1], "dt", freq = TRUE)
windows()#opens a new window to show both figures
hist(ltraj[1], "dist", freq = TRUE)
windows()
hist(ltraj[2], "dt", freq = TRUE)
windows()
hist(ltraj[2], "dist", freq = TRUE)
windows()
hist(ltraj[3], "dt", freq = TRUE)
windows()
hist(ltraj[3], "dist", freq = TRUE)
windows()
hist(ltraj[4], "dt", freq = TRUE)
windows()
hist(ltraj[4], "dist", freq = TRUE)
windows()
hist(ltraj[5], "dt", freq = TRUE)
windows()
hist(ltraj[5], "dist", freq = TRUE)
windows()
hist(ltraj[6], "dt", freq = TRUE)
windows()
hist(ltraj[6], "dist", freq = TRUE)
windows()
hist(ltraj[7], "dt", freq = TRUE)
windows()
hist(ltraj[7], "dist", freq = TRUE)