-
Notifications
You must be signed in to change notification settings - Fork 0
/
Becciu-et-al-2023_Mov-Ecol_first_deriv_GAMM.R
159 lines (126 loc) · 6.1 KB
/
Becciu-et-al-2023_Mov-Ecol_first_deriv_GAMM.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
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
# Example script to fit a GAMM and calculate its first derivative and simultaneous intervals ####
packages<-function(x){
x<-as.character(match.call()[[2]])
if (!require(x,character.only=TRUE)){
install.packages(pkgs=x,repos="http://cran.r-project.org")
require(x,character.only=TRUE)
}
}
packages(circular)
packages(tidyverse)
packages(tidylog)
packages(lubridate)
packages(mgcv)
packages(sjPlot)
packages(performance)
packages(gratia)
# theme for graphs ####
THEME2 <- theme_bw() +
theme(axis.text.x = element_text(size = 15, colour = "black"),
axis.text.y = element_text(size = 15, colour = "black"),
axis.title.x = element_text(size = 18, vjust = -0.35),
axis.title.y = element_text(size = 18, vjust = 1.2),
legend.title = element_text(size = 15, colour = "black"),
legend.text = element_text(size = 15, colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank())
# load data ####
data <- read.csv("/Users/pbecciu/Desktop/latrun_radar_data_ms_analysis.csv") # load data shared in the online Suppl Materials of Becciu et al. 2023 Movement Ecology
# GAMM of Crosswind speed ~ hours to sunset ####
dataCW <- data %>%
dplyr::select(h.from.sunset, h.sset, year, n.days, CW) %>%
mutate(yearf = factor(year),
n.daysf = factor(n.days),
h.sset = as.numeric(h.sset),
h.from.sunset = as.numeric(h.from.sunset),
h.ssetf = factor(h.sset)
)
ctrl <- list(niterEM = 0, msVerbose = TRUE, optimMethod="L-BFGS-B") # non-linear optimization method for parameter estimation
modCW <- gamm(CW ~ s(h.from.sunset, bs = "cr", k = 12),
correlation = corARMA(form = ~ 1|n.days, p = 2),
random = list(yearf = ~1),
data = dataCW,
control = ctrl)
summary(modCW$gam)
plot(modCW$gam)
## diagnostics
plot(resid(modCW$lme, type = "normalized"))
gam.check(modCW$gam)
appraise(modCW$gam, method = "simulate", n_simulate = 10000)
## first derivative - using gratia package ####
# parameters for testing
N <- 10000 # number of posterior draws
n <- 1000 # number of newdata values
EPS <- 1e-07 # finite difference
# Generating new data grid
newd <- expand.grid(h.from.sunset = seq(min(data$h.from.sunset), max(data$h.from.sunset), length.out = n),
n.days = mean(dataCW$n.days),
year = mean(dataCW$year))
# Computing first derivative using central difference method and simultaneous intervals
FDmodCW <- gratia::derivatives(modCW$gam,
term = "s(h.from.sunset)",
type = "central",
eps = EPS,
newdata = newd,
n = n,
n_sim = N,
interval = "simultaneous",
unconditional = F,
frequentist = F)
draw(FDmodCW)
# Adding additional columns to highlight the periods of change, and renaming columns for clarity
FDmodCW <- FDmodCW %>%
mutate(increasing = as.numeric(ifelse(derivative > 0 & upper > 0 & lower > 0, derivative, NA)),
decreasing = as.numeric(ifelse(derivative < 0 & upper < 0 & lower < 0, derivative, NA))) %>%
rename(FDlower = lower,
FDupper = upper,
h.from.sunset = data) %>%
as.data.frame()
# Simulating posterior predictive draws
sims <- simulate(modCW, nsim = N, newdata = newd)
ci <- apply(sims, 1L, quantile, probs = c(0.025, 0.975))
newd1 <- transform(newd,
fitted = predict(modCW$gam, newdata = newd),
lower = ci[1, ],
upper = ci[2, ])
# Combining the results of the derivative and the posterior predictive draws
all_FD_simCI <- cbind(newd1[,-1:-3], FDmodCW)
all_FD_simCI <- all_FD_simCI %>%
mutate(incr = as.character(ifelse(increasing > 0, "increasing", NA)),
decr = as.character(ifelse(decreasing < 0, "decreasing", NA)),
fit.incr = as.numeric(ifelse(incr == "increasing", fitted, NA)),
fit.decr = as.numeric(ifelse(decr == "decreasing", fitted, NA)))
# graph of the first derivative plus 95% simultaneous confidence intervals
## this highlights the significant increasing (red) and decreasing (blue) changes over the time window used (here hours)
FDplotCW <- ggplot(all_FD_simCI, aes(x = h.from.sunset, y = derivative)) +
geom_ribbon(aes(ymax = FDupper, ymin = FDlower), alpha = 0.3, fill = "grey") +
geom_line() +
geom_line(aes(y = increasing), size = 1.5, colour = "red") +
geom_line(aes(y = decreasing), size = 1.5, colour = "blue") +
labs(y = expression(italic(hat(f) * "'") * ("x")),
x = "Hours to sunset") +
geom_hline(yintercept = 0, linetype = "dashed") +
THEME2
FDplotCW
# graph of the GAMM plus 95% simultaneous confidence intervals and 95% confidence intervals (darker grey)
modelplotCW_data <- plot_model(modCW, type = "pred", terms = c("h.from.sunset"), show.data = F, alpha = 0.4) +
geom_point(data = dataCW, aes(y = CW, x = h.from.sunset), colour = "black", alpha = 0.1) +
geom_ribbon(data = all_FD_simCI, aes(x = h.from.sunset, y = fitted, ymax = upper, ymin = lower), alpha = 0.3, fill = "grey") +
geom_line(data = all_FD_simCI, aes(x = h.from.sunset, y = fit.incr), size = 1.5, colour = "red") +
geom_line(data = all_FD_simCI, aes(x = h.from.sunset, y = fit.decr), size = 1.5, colour = "blue") +
labs(title = "",
y = "Crosswinds [m/s]",
x = "Hours to sunset") +
THEME2
modelplotCW_data
# graph of the GAMM plus 95% confidence intervals
modelplotCW <- plot_model(modCW, type = "pred", terms = c("h.from.sunset"), show.data = F) +
# geom_point(data = hb.aspeed, aes(y = CW, x = h.from.sunset), colour = "black", alpha = 0.1) +
geom_line(data = all_FD_simCI, aes(x = h.from.sunset, y = fit.incr), size = 1.5, colour = "red") +
geom_line(data = all_FD_simCI, aes(x = h.from.sunset, y = fit.decr), size = 1.5, colour = "blue") +
labs(title = "",
y = "Crosswinds [m/s]",
x = "Hours to sunset") +
THEME2
modelplotCW