-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFPTscript.Rmd
executable file
·186 lines (137 loc) · 5.98 KB
/
FPTscript.Rmd
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
## First Passage Time (FPT)
The first passage time (FPT) is a parameter often used to describe the scale at which patterns occur in a trajectory. For a given scale r, it is defined as the time required by the animals to pass through a circle of radius r. The mean first passage time scales proportionately to the square of the radius of the circle for an uncorrelated random walk (Johnson et al. 1992). Johnson et al. (1992) used this property to differentiate facilitated diffusion and impeded diffusion, according to the value of the coefficient of the linear regression log(FPT) = a \* log(radius) + b. Under the hypothesis of a random walk, a should be equal to 2 (higher for impeded diffusion, and lower for facilitated diffusion). Note however, that the value of a converges to 2 only for large values of radius. Another use of the FPT was proposed that, instead of computing the mean of FPT, use the variance of the log(FPT). This variance should be high for scales at which patterns occur in the trajectory (e.g. area restricted search; Fauchald and Tverra 2003). This method is often used to determine the scale at which an animal searches for food.
The value fpt computes the FPT for each relocation and each radius, and for each animal. This function returns an object of class "fipati" (i.e., a list with one component per animal). Each component is a data frame with each column corresponding to a value of radii and each row corresponding to a relocation. An object of class fipati has an attribute named "radii" corresponding to the argument radii of the function fpt. meanfpt and varlogfpt return a data frame giving respectively the mean FPT and the variance of the log(FPT) for each animal (rows) and rach radius (column). These objects also have an attribute "radii".
1\. Open the script "FPTscript.Rmd" and run code directly from the script
2\. First we need to load the packages needed for the exercise
```{r warning=FALSE, message=FALSE}
library(adehabitatLT)
library(chron)
library(sf)
```
3\. Now let's have a separate section of code to include projection information we will use throughout the exercise. In previous versions, these lines of code were within each block of code
```{r warning=FALSE, message=FALSE}
ll.crs <- st_crs(4269)
utm.crs <- st_crs(9001)
albers.crs <- st_crs(5070)
```
4\. Load in our mule deer dataset from previous exercises
```{r}
muleys <-read.csv("data/DCmuleysedited.csv", header=T)
#Code to look at number of relocations per animal
table(muleys$id)
#Remove outlier locations
newmuleys <-subset(muleys, muleys$Long > -110.50 & muleys$Lat > 37.3 & muleys$Long < -107)
muleys <- newmuleys
#Conversion of the date to the format POSIX as in previous exercise
da <- as.character(muleys$GPSFixTime)
da <- as.POSIXct(strptime(muleys$GPSFixTime,format="%Y.%m.%d %H:%M:%S"))
muleys$da <- da
```
5\. 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. Then create an sf class of locations.
```{r}
da <- as.POSIXct(strptime(muleys$GPSFixTime,format="%Y.%m.%d %H:%M:%S"))
muleys$da <- da
timediff <- diff(muleys$da)*60
muleys <-muleys[-1,]
muleys$timediff <-as.numeric(abs(timediff))
#Remove outlier locations
coords <- st_as_sf(muleys, coords = c("Long", "Lat"), crs = ll.crs)
plot(st_geometry(coords),axes=T)
deer.spdf <- st_crop(coords, xmin=-107.0,xmax=-110.5,ymin=37.8,ymax=39.0)#Visually identified based on previous plot
plot(st_geometry(deer.spdf),axes=T)
#Project deer.spdf to Albers as in previous exercise
deer.albers <-st_transform(deer.spdf, crs=albers.crs)
plot(st_geometry(deer.albers),axes=T)
```
6\. Create an object of class "ltraj" (i.e., trajectory) for all animals
```{r fig.height=4, fig.width=4}
ltraj <- as.ltraj(st_coordinates(deer.albers),deer.albers$da,id=deer.albers$id)
plot(ltraj)
```
7\. Code below actually creates First Passage Time and mean and variance of fpt
```{r eval=FALSE}
plot(ltraj[1])
i1 <- fpt(ltraj[1], seq(300,1000, length=30))
plot(i1, scale = 200, warn = FALSE)
plot(ltraj[2])
i2 <- fpt(ltraj[2], seq(300,1000, length=30))
plot(i2, scale = 500, warn = FALSE)
toto2 <- meanfpt(i2)
toto2
attr(toto2, "radii")
toto2 <- varlogfpt(i2)
toto2
attr(toto2, "radii")
plot(ltraj[3])
i3 <- fpt(ltraj[3], seq(300,1000, length=30))
plot(i3, scale = 500, warn = FALSE)
toto3 <- meanfpt(i3)
toto3
attr(toto3, "radii")
toto3 <- varlogfpt(i3)
toto3
attr(toto3, "radii")
plot(ltraj[4])
i4 <- fpt(ltraj[4], seq(300,1000, length=30))
plot(i4, scale = 500, warn = FALSE)
toto4 <- meanfpt(i4)
toto4
attr(toto4, "radii")
toto4 <- varlogfpt(i4)
toto4
attr(toto4, "radii")
plot(ltraj[5])
i5 <- fpt(ltraj[5], seq(300,1000, length=30))
plot(i5, scale = 500, warn = FALSE)
toto5 <- meanfpt(i5)
toto5
attr(toto5, "radii")
toto5 <- varlogfpt(i5)
toto5
attr(toto5, "radii")
plot(ltraj[6])
i6 <- fpt(ltraj[6], seq(300,1000, length=30))
plot(i6, scale = 500, warn = FALSE)
plot(ltraj[7])
i7 <- fpt(ltraj[7], seq(300,1000, length=30))
plot(i7, scale = 500, warn = FALSE)
toto7 <- meanfpt(i7)
toto7
attr(toto7, "radii")
toto7 <- varlogfpt(i7)
toto7
attr(toto7, "radii")
```
8\. Code to export each trajectory as a shapefile if needed
```{r eval=FALSE, warning=FALSE,message=FALSE}
toto1 <-ltraj2sldf(ltraj[1])
plot(toto1)
#st_write(toto1,"D12.sp")
summary(toto1)
#Write lines and points as a shapefile
toto2lines <-ltraj2sldf(ltraj[2],byid=TRUE)
toto2pts <- ltraj2spdf(ltraj[2])
#If we want to define projection before making a shapefile
proj4string <- CRS("+proj=utm +zone=13N +ellps=WGS84")
toto2lines@proj4string <- proj4string
toto2pts@proj4string <- proj4string
plot(toto2pts)
plot(toto2lines, add=T)
st_write(toto2pts,"D15pts.shp")
st_write(toto2lines, paste("traj_line_",sep=""))
toto3 <-ltraj2sldf(ltraj[3])
plot(toto3)
st_write(toto3,"D16.shp")
toto4 <-ltraj2sldf(ltraj[4])
plot(toto4)
st_write(toto4,"D19.shp")
toto5 <-ltraj2sldf(ltraj[5])
plot(toto5)
st_write(toto5,"D4.shp")
toto6 <-ltraj2sldf(ltraj[6])
plot(toto6)
st_write(toto6,"D6.shp")
toto7 <-ltraj2sldf(ltraj[7])
plot(toto7)
st_write(toto7,"D8.shp")
```