-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy path.Rhistory
More file actions
512 lines (512 loc) · 21.3 KB
/
.Rhistory
File metadata and controls
512 lines (512 loc) · 21.3 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# histograms
hist(distdata$distance,main="",xlab="Distance (m)")
hist(distdata$size,main="",xlab="Cluster size")
# plots of distance vs. cluster size
plot(distdata$distance, distdata$size, main="", xlab="Distance (m)",
ylab="Group size", pch=19, cex=0.5, col=gray(0.7))
# lm fit
l.dat <- data.frame(distance=seq(0,8000,len=1000))
lo <- lm(size~distance, data=distdata)
lines(l.dat$distance, as.vector(predict(lo,l.dat)))
plot(distdata$distance,distdata$beaufort, main="", xlab="Distance (m)",
ylab="Beaufort sea state", pch=19, cex=0.5, col=gray(0.7))
# Chunk 17: loadDistance
library(Distance)
# Chunk 18: hrmodel
detfc.hr.null <- ds(distdata, max(distdata$distance), key="hr", adjustment=NULL)
# Chunk 19: hrmodelsummary
summary(detfc.hr.null)
# Chunk 20: hr-detfct
layout(matrix(c(1, 2), 1, 2), width=c(1.5, 1))
plot(detfc.hr.null, showpoints=FALSE, pl.den=0, lwd=2)
gof_ds(detfc.hr.null)
par(mfrow=c(1,1))
# Chunk 21: hrcovdf
detfc.hr.beau<-ds(distdata, max(distdata$distance), formula=~as.factor(beaufort),
key="hr", adjustment=NULL)
# Chunk 22: hrcovdfsummary
summary(detfc.hr.beau)
# Chunk 23: dsm-xy
dsm.xy <- dsm(count~s(x,y), detfc.hr.null, segdata, obsdata, method="REML")
# Chunk 24: dsm-xy-summary
summary(dsm.xy)
# Chunk 25: visgam1
vis.gam(dsm.xy, plot.type="contour", view=c("x","y"), asp=1, type="response", contour.col="black", n.grid=100)
# Chunk 26: depthmodel
dsm.xy.depth <- dsm(count~s(x,y,k=10) + s(depth,k=20), detfc.hr.null, segdata, obsdata, method="REML")
summary(dsm.xy.depth)
# Chunk 27: dsm-xy-depth-depth
plot(dsm.xy.depth, select=2)
# Chunk 28: dsm-est-xy
dsm.est.xy <- dsm(abundance.est~s(x,y), detfc.hr.beau, segdata, obsdata, method="REML")
# Chunk 29: dsm-est-xy-summary
summary(dsm.est.xy)
# Chunk 30: visgam5
vis.gam(dsm.est.xy, plot.type="contour", view=c("x","y"), asp=1, type="response", zlim=c(0, 300), contour.col="black", n.grid=100)
# Chunk 31: tweedie-fit
dsm.xy.tweedie <- dsm(count~s(x,y), detfc.hr.null, segdata, obsdata, family=tw(), method="REML")
summary(dsm.xy.tweedie)
# Chunk 32: soap-knots
soap.knots <- make.soapgrid(survey.area,c(15,10))
# Chunk 33: soap-setup
x <- segdata$x; y<-segdata$y
onoff <- inSide(x=x,y=y, bnd=as.list(survey.area))
rm(x,y)
segdata.soap <- segdata[onoff,]
# Chunk 34: soap-fit
dsm.xy.tweedie.soap<-dsm(count~s(x, y, bs="so", k=15, xt=list(bnd=list(survey.area))) +
s(depth),
family=tw(), method="REML",
detfc.hr.null, segdata.soap, obsdata, knots=soap.knots)
summary(dsm.xy.tweedie.soap)
# Chunk 35: dsm.xy-check
gam.check(dsm.xy)
# Chunk 36: dsm.xy.tweedie-check
gam.check(dsm.xy.tweedie)
# Chunk 37: dsm.xy.tweedie-rqcheck
rqgam_check(dsm.xy.tweedie)
# Chunk 38: modelcomp
# make a data.frame to print out
mod_results <- data.frame("Model name" = c("`dsm.xy`", "`dsm.xy.depth`", "`dsm.xy.tweedie`", "`dsm.xy.tweedie.soap`",
"`dsm.est.xy`"),
"Description" = c("Bivariate smooth of location, quasipoisson",
"Bivariate smooth of location, smooth of depth, quasipoisson",
"Bivariate smooth of location, smooth of depth, Tweedie",
"Soap film smooth of location, smooth of depth, Tweedie",
"Bivariate smooth of location, smooth of depth, Tweedie, Beaufort covariate in detection function"),
"Deviance explained" = c(unlist(lapply(list(dsm.xy,
dsm.xy.depth,
dsm.xy.tweedie,
dsm.xy.tweedie.soap),
function(x){paste0(round(summary(x)$dev.expl*100,2),"%")})),NA))
# Chunk 39: results-table
kable(mod_results, col.names=c("Model name", "Description", "Deviance explained"))
# Chunk 40: dsm-xy-pred
dsm.xy.pred <- predict(dsm.xy, preddata, preddata$area)
# Chunk 41: dsm.xy-preds
# p <- ggplot() + grid_plot_obj(dsm.xy.pred, "Abundance", pred.polys) + coord_equal() +gg.opts
# p <- p + geom_path(aes(x=x, y=y),data=survey.area)
# p <- p + labs(fill="Abundance")
# print(p)
prediction_grid <- st_make_grid(area.sf.proj, cellsize = c(9000,9000))
prediction_grid_sf <- st_sf(geometry = prediction_grid)
cropped_grid <- st_join(prediction_grid_sf, preddata_sf, join = st_nearest_feature)
cropped_grid <- st_intersection(cropped_grid, area.sf.proj)
preddata_sf$Prediction_xy <- dsm.xy.pred
pred <- ggplot() +
geom_sf(data = cropped_grid, aes(fill = Prediction_xy), color = NA) +
geom_sf(data=segdata_sf, fill=NA, color="white", linewidth=.001) +
labs(title="Spotted dolphins, Gulf of Mexico, abundance estimates",
subtitle = "Bivariate smooth of location, quasipoisson") +
scale_fill_viridis_c(option = "viridis", direction = 1)
pred
# p <- ggplot() + grid_plot_obj(dsm.xy.pred, "Abundance", pred.polys) + coord_equal() +gg.opts
# p <- p + geom_path(aes(x=x, y=y),data=survey.area)
# p <- p + labs(fill="Abundance")
# print(p)
prediction_grid <- st_make_grid(area.sf.proj, cellsize = c(9000,9000))
prediction_grid_sf <- st_sf(geometry = prediction_grid)
preddata_sf$Prediction_xy <- dsm.xy.pred
cropped_grid <- st_join(prediction_grid_sf, preddata_sf, join = st_nearest_feature)
cropped_grid <- st_intersection(cropped_grid, area.sf.proj)
pred <- ggplot() +
geom_sf(data = cropped_grid, aes(fill = Prediction_xy), color = NA) +
geom_sf(data=segdata_sf, fill=NA, color="white", linewidth=.001) +
labs(title="Spotted dolphins, Gulf of Mexico, abundance estimates",
subtitle = "Bivariate smooth of location, quasipoisson") +
scale_fill_viridis_c(option = "viridis", direction = 1)
pred
dsm.xy.depth.pred <- predict(dsm.xy.depth, preddata, preddata$area)
preddata_sf$Prediction_xy_depth <- dsm.xy.depth.pred
cropped_grid <- st_join(prediction_grid_sf, preddata_sf, join = st_nearest_feature)
cropped_grid <- st_intersection(cropped_grid, area.sf.proj)
pred <- ggplot() +
geom_sf(data = cropped_grid, aes(fill = Prediction_xy_depth), color = NA) +
geom_sf(data=segdata_sf, fill=NA, color="white", linewidth=.001) +
labs(title="Spotted dolphins, Gulf of Mexico, abundance estimates",
subtitle = "Bivariate smooth of location, smooth of depth, quasipoisson") +
scale_fill_viridis_c(option = "viridis", direction = 1)
pred
# p <- ggplot() + grid_plot_obj(dsm.xy.depth.pred, "Abundance", pred.polys) + coord_equal() +gg.opts
# p <- p + geom_path(aes(x=x, y=y), data=survey.area)
# p <- p + labs(fill="Abundance")
# print(p)
preddata.var <- split(preddata, 1:nrow(preddata))
dsm.xy.var <- dsm_var_gam(dsm.xy, pred.data=preddata.var,
off.set=preddata$area)
summary(dsm.xy.var)
# p <- ggplot() + grid_plot_obj(sqrt(dsm.xy.var$pred.var)/unlist(dsm.xy.var$pred),
# "CV", pred.polys) + coord_equal() +gg.opts
# p <- p + geom_path(aes(x=x, y=y), data=survey.area)
# p <- p + geom_line(aes(x, y, group=Transect.Label), data=segdata)
#print(p)
preddata_sf$CV <- sqrt(dsm.xy.var$pred.var)/preddata_sf$Prediction
# p <- ggplot() + grid_plot_obj(sqrt(dsm.xy.var$pred.var)/unlist(dsm.xy.var$pred),
# "CV", pred.polys) + coord_equal() +gg.opts
# p <- p + geom_path(aes(x=x, y=y), data=survey.area)
# p <- p + geom_line(aes(x, y, group=Transect.Label), data=segdata)
#print(p)
preddata_sf$CV <- sqrt(dsm.xy.var$pred.var)/preddata_sf$Prediction_xy
cropped_grid <- st_join(prediction_grid_sf, preddata_sf, join = st_nearest_feature)
cropped_grid <- st_intersection(cropped_grid, area.sf.proj)
CV <- ggplot() +
geom_sf(data = cropped_grid, aes(fill = CV), color = NA) +
geom_sf(data=segdata_sf, fill=NA, color="white", linewidth=.001) +
labs(title="Spotted dolphins, Gulf of Mexico, uncertainty (CV)",
subtitle = "Bivariate smooth of location, quasipoisson") +
scale_fill_viridis_c(option = "viridis", direction = 1)
CV
?fig.dim
data(mexdolphins)
load("mexdolphins-extra.rda")
predsf <- st_as_sf(pred.polys)
# plot as projected
plot(pred.polys, xlab="Northing", ylab="Easting")
axis(1); axis(2); box()
predsf <- st_as_sf(pred.polys)
# plot as projected
plot(st_geometry(pred.polys), axes=TRUE)
predsf <- st_as_sf(pred.polys)
# plot as projected
plot(st_geometry(predsf), axes=TRUE)
axis(1); axis(2); box()
library(sf)
library(plyr)
# tell R that the survey.area object is currently in lat/long
sp::proj4string(survey.area) <- sp::CRS("+proj=longlat +datum=WGS84")
predsf <- st_as_sf(pred.polys)
area.sf <- st_as_sf(survey.area)
st_crs(area.sf) <- "WGS84"
area.sf.proj <- st_transform(area.sf, crs = st_crs(predsf))
# Convert preddata to a spatial object
preddata_sf <- st_as_sf(preddata, coords=c("x", "y"))
st_crs(preddata_sf) <- st_crs(area.sf.proj)
# Perform the intersection
preddata_sf <- st_intersection(preddata_sf, area.sf.proj)
coords_preddata <- data.frame(st_coordinates(preddata_sf))
preddata_sf$x <- coords_preddata$X
preddata_sf$y <- coords_preddata$Y
depth <- ggplot() +
geom_sf(data=cropped_grid, aes(fill=depth), color=NA) +
labs(title = "Gulf of Mexico study area",
subtitle = "Depth in meters") +
scale_fill_viridis_c(option = "viridis", direction = 1)
cropped_grid <- st_join(prediction_grid_sf, preddata_sf, join = st_nearest_feature)
prediction_grid <- st_make_grid(area.sf.proj, cellsize = c(9000,9000))
prediction_grid_sf <- st_sf(geometry = prediction_grid)
cropped_grid <- st_join(prediction_grid_sf, preddata_sf, join = st_nearest_feature)
cropped_grid <- st_intersection(cropped_grid, area.sf.proj)
depth <- ggplot() +
geom_sf(data=cropped_grid, aes(fill=depth), color=NA) +
labs(title = "Gulf of Mexico study area",
subtitle = "Depth in meters") +
scale_fill_viridis_c(option = "viridis", direction = 1)
depth
depth <- ggplot() +
geom_sf(data=cropped_grid, aes(fill=depth), color=NA) +
labs(title = "Gulf of Mexico study area",
subtitle = "Depth in meters") +
geom_point(aes(x, y, size=size), data=distdata) +
scale_fill_viridis_c(option = "viridis", direction = 1)
depth
View(distdata)
depth <- ggplot() +
geom_sf(data=cropped_grid, aes(fill=depth), color=NA) +
labs(title = "Spotted dolphins, Gulf of Mexico",
subtitle = "Depth in meters, size of detected dolphin groups") +
geom_point(aes(x, y, size=size), data=distdata, colour="red",alpha=I(0.7)) +
scale_fill_viridis_c(option = "viridis", direction = 1)
depth
depth <- ggplot() +
geom_sf(data=cropped_grid, aes(fill=depth), color=NA) +
labs(title = "Spotted dolphins, Gulf of Mexico",
subtitle = "Depth in meters, size of detected dolphin groups") +
geom_point(aes(x, y, size=size), data=distdata, colour="red",alpha=I(0.5)) +
scale_fill_viridis_c(option = "viridis", direction = 1)
depth
depth <- ggplot() +
geom_sf(data=cropped_grid, aes(fill=depth), color=NA) +
labs(title = "Spotted dolphins, Gulf of Mexico",
subtitle = "Depth in meters, size of detected dolphin groups",
xlab("Longitude"), ylab=("Latitude")) +
geom_point(aes(x, y, size=size), data=distdata, colour="red",alpha=I(0.5)) +
scale_fill_viridis_c(option = "viridis", direction = 1)
depth
depth <- ggplot() +
geom_sf(data=cropped_grid, aes(fill=depth), color=NA) +
labs(title = "Spotted dolphins, Gulf of Mexico",
subtitle = "Depth in meters, size of detected dolphin groups") +
xlab("Longitude") + ylab=("Latitude") +
geom_point(aes(x, y, size=size), data=distdata, colour="red",alpha=I(0.5)) +
scale_fill_viridis_c(option = "viridis", direction = 1)
depth <- ggplot() +
geom_sf(data=cropped_grid, aes(fill=depth), color=NA) +
labs(title = "Spotted dolphins, Gulf of Mexico",
subtitle = "Depth in meters, size of detected dolphin groups") +
xlab("Longitude") + ylab=("Latitude") +
geom_point(aes(x, y, size=size), data=distdata, colour="red",alpha=I(0.5)) +
scale_fill_viridis_c(option = "viridis", direction = 1)
depth <- ggplot() +
geom_sf(data=cropped_grid, aes(fill=depth), color=NA) +
labs(title = "Spotted dolphins, Gulf of Mexico",
subtitle = "Depth in meters, size of detected dolphin groups") +
xlab("Longitude") + ylab("Latitude") +
geom_point(aes(x, y, size=size), data=distdata, colour="red",alpha=I(0.5)) +
scale_fill_viridis_c(option = "viridis", direction = 1)
depth
setwd("~/GitHub/dsm/vignettes/points_france")
shape <- st_read("Contour_Rouillacais.shp")
plot(shape)
# provide the correct projection for the data
newproj <- "+proj=lcc +nadgrids=ntf_r93.gsb,null +a=6378249.2000 +rf=293.4660210000000 +pm=2.337229167 +lat_0=46.800000000 +lon_0=0.000000000 +k_0=0.99987742 +lat_1=46.800000000 +x_0=600000.000 +y_0=200000.000 +units=m +no_defs"
bob <- st_transform(shape, newproj)
bob <- st_transform(shape, crs=newproj)
CRS(shape) <- 4326
bob <- st_transform(x=shape, src=4326, dst=newproj)
shape <- st_read("Contour_Rouillacais.shp")
sp::CRS(shape) <- 4326
sp:::CRS(shape) <- 4326
?CRS
sp::crs(shape) <- 4326
st_crs(shape)
st_transform(shape, crs=4326)
shape <- st_read("Contour_Rouillacais.shp", crs=st_crs(4326))
shape <- st_read("Contour_Rouillacais.shp", crs=st_crs(27562))
EPP <- readShapeSpatial("Rouillacais_points.shp", crs=st_crs(27562))
# make the object simpler
survey.area <- data.frame(shape@polygons[[1]]@Polygons[[1]]@coords)
library(sfheaders)
install.packages("sfheaders")
library(sfheaders)
survey.area <- sf_to_df(shape)
study <- ggplot() +
geom_sf(data=shape, fill="lightblue") +
geom_sf(data=EPP, color="black") +
theme_minimal()
EPP <- st_read("Rouillacais_points.shp", crs=st_crs(27562))
study <- ggplot() +
geom_sf(data=shape, fill="lightblue") +
geom_sf(data=EPP, color="black") +
theme_minimal()
print(study)
# load raw data
data <- read.table("Hare_data.csv", header = TRUE, sep = ";", stringsAsFactors=FALSE)
str(data)
DSdata <- data.frame(Sample.Label = sub("Rouillacais_2016", "", data$point_ID),
Point = data$point_ID,
Xcoord = as.integer(data$Xcoord),
Ycoord = as.integer(data$Ycoord),
Area = 1)
DSdata$distance <- as.numeric(data$distance)/1000
DSdata$Effort <- 3
shape$AREA/(1000^2)
# construct segment (point) data (x, y, Effort, Sample.Label)
segdata <- as.data.frame(matrix(NA, ncol = 5, nrow=100))
segdata <- DSdata[, c("Sample.Label", "Effort", "Point", "Xcoord", "Ycoord")]
segdata <- segdata[!duplicated(segdata), ]
colnames(segdata) <- c("Sample.Label", "Effort", "Segment.Label", "X", "Y")
obsdata <- DSdata
obsdata$size <- 1
obsdata$object <- 1:nrow(obsdata)
str(obsdata)
prediction_grid <- st_make_grid(shape, cellsize = c(500,500))
prediction_grid_sf <- st_sf(geometry = prediction_grid)
hmm <- st_intersection(prediction_grid_sf, shape)
plot(hmm)
plot(prediction_grid_sf)
library("Distance")
# define distance bins
cut <- c(0, 0.180, 0.220, 0.280, 0.300)
df_ht <- ds(DSdata, truncation=0.3, transect="point",
formula=~1, key="hn", adjustment=NULL, cutpoints=cut)
mod_tw <- dsm(count~s(X, Y), ddf.obj=df_ht, segment.data=segdata,
observation.data=obsdata, family=tw(), transect="point")
mod_tw_pred <- predict(mod_tw, preddata, preddata$area)
str(hmm)
preddata <- as.data.frame(matrix(NA, ncol=3, nrow=length(hmm$geometry)))
colnames(preddata) <- c("X", "Y", "area")
head(hmm$geometry)
head(hmm$geometry)[[1]]
701*24
hmm$geometry[[1]][[1:4]]
hmm$geometry[[1]]
tom <- hmm$geometry[[1]]
str(tom)
tom[[1]][1:4, 1:2]
mean(tom[[1]][1:4, 1])
mean(tom[[1]][1:4, 2])
for (i in 1:length(hmm$geometry)){
preddata[i, c("X")] <- mean(hmm$geometry[[i]][1:4, 1])
preddata[i, c("Y")] <- mean(hmm$geometry[[i]][1:4, 2])
preddata[i, c("area")] <- pred.grid$AREA[[i]]/(1000^2)
}
hmm$geometry[[22]]
hmm$geometry[[22]][1:4, 1]
hmm$geometry[[22]][[1]][1:4, 1]
prediction_grid <- st_make_grid(shape, cellsize = c(500,500))
prediction_grid_sf <- st_sf(geometry = prediction_grid)
hmm <- st_intersection(prediction_grid_sf, shape)
preddata <- as.data.frame(matrix(NA, ncol=3, nrow=length(hmm$geometry)))
colnames(preddata) <- c("X", "Y", "area")
for (i in 1:length(hmm$geometry)){
preddata[i, c("X")] <- mean(hmm$geometry[[i]][[1]][1:4, 1])
preddata[i, c("Y")] <- mean(hmm$geometry[[i]][[1]][1:4, 2])
preddata[i, c("area")] <- pred.grid$AREA[[i]]/(1000^2)
}
prediction_grid <- st_make_grid(shape, cellsize = c(500,500))
prediction_grid_sf <- st_sf(geometry = prediction_grid)
hmm <- st_intersection(prediction_grid_sf, shape)
preddata <- as.data.frame(matrix(NA, ncol=3, nrow=length(hmm$geometry)))
colnames(preddata) <- c("X", "Y", "area")
for (i in 1:length(hmm$geometry)){
preddata[i, c("X")] <- mean(hmm$geometry[[i]][[1]][1:4, 1])
preddata[i, c("Y")] <- mean(hmm$geometry[[i]][[1]][1:4, 2])
preddata[i, c("area")] <- hmm$AREA[[i]]/(1000^2)
}
hmm$geometry[[23]][[1]][1:4,1]
hmm$geometry[[23]][[1]][1:4,2]
hmm$geometry[[700]][[1]][1:4,2]
hmm$geometry[[500]][[1]][1:4,2]
hmm$geometry[[600]][[1]][1:4,2]
hmm$geometry[[700]][[1]][1:4,2]
hmm$geometry[[650]][[1]][1:4,2]
hmm$geometry[[680]][[1]][1:4,2]
hmm$geometry[[690]][[1]][1:4,2]
hmm$geometry[[699]][[1]][1:4,2]
hmm$AREA[600]
hmm$AREA[700]
hmm$AREA[701]
length(hmm$geometry)
# create a prediction grid
# method from http://rfunctions.blogspot.co.uk/2014/12/how-to-create-grid-and-intersect-it.html
library("raster")
library("rgeos")
library("dismo")
# Create an empty raster
grid <- raster(extent(shape))
# Choose its resolution. 500 m in both X and Y (truncation distance)
res(grid) <- 500
# Make the grid have the same coordinate reference system (CRS) as the shapefile.
proj4string(grid) <- proj4string(shape)
area.sf <- st_as_sf(survey.area)
# provide the correct projection for the data
newproj <- "+proj=lcc +nadgrids=ntf_r93.gsb,null +a=6378249.2000 +rf=293.4660210000000 +pm=2.337229167 +lat_0=46.800000000 +lon_0=0.000000000 +k_0=0.99987742 +lat_1=46.800000000 +x_0=600000.000 +y_0=200000.000 +units=m +no_defs"
# import shapefile for the survey area
shape <- readShapeSpatial("Contour_Rouillacais.shp", proj4string = CRS(newproj),
repair=TRUE, force_ring=T, verbose=TRUE)
library(sp)
# provide the correct projection for the data
newproj <- "+proj=lcc +nadgrids=ntf_r93.gsb,null +a=6378249.2000 +rf=293.4660210000000 +pm=2.337229167 +lat_0=46.800000000 +lon_0=0.000000000 +k_0=0.99987742 +lat_1=46.800000000 +x_0=600000.000 +y_0=200000.000 +units=m +no_defs"
# import shapefile for the survey area
shape <- readShapeSpatial("Contour_Rouillacais.shp", proj4string = CRS(newproj),
repair=TRUE, force_ring=T, verbose=TRUE)
# import shapefile for the points
EPP <- readShapeSpatial("Rouillacais_points.shp", proj4string = CRS(newproj),
repair=TRUE, force_ring=T, verbose=TRUE)
?readShapeSpatial
??readShapeSpatial
prediction_grid <- st_make_grid(shape, cellsize = c(500,500))
prediction_grid_sf <- st_sf(geometry = prediction_grid)
hmm <- st_intersection(prediction_grid_sf, shape)
preddata <- as.data.frame(matrix(NA, ncol=3, nrow=length(hmm$geometry)))
colnames(preddata) <- c("X", "Y", "area")
for (i in 1:length(hmm$geometry)){
preddata[i, c("X")] <- mean(hmm$geometry[[i]][[1]][1:4, 1])
preddata[i, c("Y")] <- mean(hmm$geometry[[i]][[1]][1:4, 2])
preddata[i, c("area")] <- hmm$AREA[[i]]/(1000^2)
}
# create a prediction grid
# method from http://rfunctions.blogspot.co.uk/2014/12/how-to-create-grid-and-intersect-it.html
library("raster")
library("rgeos")
library("dismo")
# Create an empty raster
grid <- raster(extent(shape))
# Choose its resolution. 500 m in both X and Y (truncation distance)
res(grid) <- 500
# Make the grid have the same coordinate reference system (CRS) as the shapefile.
proj4string(grid) <- proj4string(shape)
# import shapefile for the survey area
shape <- readShapeSpatial("Contour_Rouillacais.shp", proj4string = CRS(newproj),
repair=TRUE, force_ring=T, verbose=TRUE)
library(maptools)
pkgdown::build_site()
pkgdown::build_news()
getwd()
setwd("~/GitHub/dsm")
pkgdown::build_news()
pkgdown::build_site()
pkgdown::build_site()
pkgdown::build_site()
pkgdown::build_site()
pkgdown::build_site()
pkgdown::build_site()
pkgdown::build_site()
pkgdown::build_site()
pkgdown::build_site()
pkgdown::build_site()
library(dsm)
library(ggplot2)
# plotting options
gg.opts <- theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.background=element_blank())
load("mexdolphins-extra.rda")
data(mexdolphins)
data(mexdolphins)
setwd("~/GitHub/dsm/vignettes/lines_gomex")
load("mexdolphins-extra.rda")
library(sf)
library(plyr)
# tell R that the survey.area object is currently in lat/long
sp::proj4string(survey.area) <- sp::CRS("+proj=longlat +datum=WGS84")
predsf <- st_as_sf(pred.polys)
area.sf <- st_as_sf(survey.area)
st_crs(area.sf) <- "WGS84"
area.sf.proj <- st_transform(area.sf, crs = st_crs(predsf))
# Convert preddata to a spatial object
preddata_sf <- st_as_sf(preddata, coords=c("x", "y"))
st_crs(preddata_sf) <- st_crs(area.sf.proj)
# Perform the intersection
preddata_sf <- st_intersection(preddata_sf, area.sf.proj)
coords_preddata <- data.frame(st_coordinates(preddata_sf))
preddata_sf$x <- coords_preddata$X
preddata_sf$y <- coords_preddata$Y
# proj 4 string
# using http://spatialreference.org/ref/esri/north-america-lambert-conformal-conic/
lcc_proj4 <- sp::CRS("+proj=lcc +lat_1=20 +lat_2=60 +lat_0=40 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs ")
# project using LCC
survey.area <- sp::spTransform(survey.area, CRSobj=lcc_proj4)
# simplify the object
survey.area <- data.frame(survey.area@polygons[[1]]@Polygons[[1]]@coords)
names(survey.area) <- c("x", "y")
prediction_grid <- st_make_grid(area.sf.proj, cellsize = c(9000,9000))
prediction_grid_sf <- st_sf(geometry = prediction_grid)
cropped_grid <- st_join(prediction_grid_sf, preddata_sf, join = st_nearest_feature)
cropped_grid <- st_intersection(cropped_grid, area.sf.proj)
depth <- ggplot() +
geom_sf(data=cropped_grid, aes(fill=depth), color=NA) +
labs(title = "Spotted dolphins, Gulf of Mexico",
subtitle = "Depth in meters, size of detected dolphin groups") +
xlab("Longitude") + ylab("Latitude") +
geom_point(aes(x, y, size=size), data=distdata, colour="red",alpha=I(0.5)) +
scale_fill_viridis_c(option = "viridis", direction = 1)
depth
library(Distance)
detfc.hr.null <- ds(distdata, max(distdata$distance), key="hr", adjustment=NULL)
plot(detfc.hr.null, showpoints=FALSE, pl.den=0, lwd=2)
gof_ds(detfc.hr.null)
dsm.xy <- dsm(count~s(x,y), detfc.hr.null, segdata, obsdata, method="REML")
summary(dsm.xy)
unique(detfc.hr.null$ddf$fitted)
library(pkgdown)
build_site()
library(pkgdown)
build_site()
build_site()
build_site()
build_site()
library(pkgdown)
build_site()
build_home_index()
build_site()