3.2 Movement trajectories

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.

2. Set working directory to the extracted folder in R under File - Change dir...
3. First we need to load the packages needed for the exercise

library(chron)
library(spatstat)#for "duplicate" function
4. 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
str(muleys)
5. 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),]
6. 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"))
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)
7. 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)
#Creates a spatial data frame of dt
dtsp<-data.frame(muleys)
#Creates a spatial data frame of Burst
busp<-data.frame(muleys)
#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)
8. Now create an object of class "ltraj" by animal using the ID field and display by each individual (i.e., ltraj).

ltraj <- as.ltraj(coordinates(merge),merge\$da,id=merge\$id)
plot(ltraj)
plot(ltraj)
head(ltraj)#Describes the trajectory for the first deer

*********** 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:
# "pkey"

plot(ltraj)
plot(ltraj)
plot(ltraj)
plot(ltraj
plot(ltraj)
plot(ltraj)
9. 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, "dt", freq = TRUE)
windows()#opens a new window to show both figures
hist(ltraj, "dist", freq = TRUE)
windows()

hist(ltraj, "dt", freq = TRUE)
windows()
hist(ltraj, "dist", freq = TRUE)
windows()

hist(ltraj, "dt", freq = TRUE)
windows()
hist(ltraj, "dist", freq = TRUE)
windows()

hist(ltraj, "dt", freq = TRUE)
windows()
hist(ltraj, "dist", freq = TRUE)
windows()

hist(ltraj, "dt", freq = TRUE)
windows()
hist(ltraj, "dist", freq = TRUE)
windows()

hist(ltraj, "dt", freq = TRUE)
windows()
hist(ltraj, "dist", freq = TRUE)
windows()

hist(ltraj, "dt", freq = TRUE)
windows()
hist(ltraj, "dist", freq = TRUE)