-
Notifications
You must be signed in to change notification settings - Fork 0
/
04_ODs.R
64 lines (54 loc) · 1.4 KB
/
04_ODs.R
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
# Goodness-of-fit
# Load packages ----
library(amt)
library(circular)
library(raster)
library(dplyr)
library(lubridate)
# Source functions ----
source("99_fun.R")
# Load data ----
# Locations
locs <- read.csv("dat/simulated_data.csv") %>%
mutate(t = ymd_hms(t))
# Models
dat <- readRDS("out/issf_fits.rds")
# Habitat
hab <- stack("dat/habitat_scaled.tif")
names(hab) <- c("forage", "pred", "cover", "dist_to_cent", "wrong")
# Create observed OD ----
loc_ods <- locs %>%
split(~sim) %>%
lapply(function(x) {
cat(unique(x$sim), "\n")
x %>%
make_track(x, y, t, crs = 32612) %>%
od(trast = hab[[1]])
})
# # SAVE
# saveRDS(loc_ods, "out/loc_ods.rds")
# Create simulated ODs ----
# Takes about 10h with n_sim = 25
system.time({
sim_ods <- lapply(1:nrow(dat), function(r) {
cat(r, "of", nrow(dat), "\n")
return(
sim_od(n_sim = 25, object = dat$issf[[r]], hs_form = dat$f[[r]], hab = hab,
start_loc = c(locs$x[1], locs$y[1]), start_t = locs$t[1],
n_avail = 500, T = 5000, delta_t = "1 hour", param_uncert = FALSE)
)
})
})
# # SAVE
# saveRDS(sim_ods, "out/sim_ods.rds")
# LOAD
sim_ods <- readRDS("out/sim_ods.rds")
# Attach ODs to dat ----
dat$obs_od <- lapply(1:nrow(dat), function(r) {
return(loc_ods[[dat$beta[r]]])
})
dat$sim_od <- lapply(1:nrow(dat), function(r) {
return(sim_ods[[r]])
})
# Save ----
saveRDS(dat, "out/issf_fits_ods.rds")