-
Notifications
You must be signed in to change notification settings - Fork 0
/
Supplemental7.R
236 lines (225 loc) · 7.44 KB
/
Supplemental7.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
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
# --------------------------------------------------------------------------------- #
# Manuscript Supplemental Figure 7
# --------------------------------------------------------------------------------- #
#
# Created by: Ian Buller, Ph.D., M.A. (GitHub: @idblr)
# Created on: 2022-05-20
#
# Most recently modified by: @idblr
# Most recently modified on: 2024-08-06
#
# Notes:
# A) See pre-steps to prepare for model run
# --------------------------------------------------------------------------------- #
# ----------- #
# PREPARATION #
# ----------- #
# Step 1: You must download the elevation BIL file at 4-km resolution from the
# PRISM data portal https://www.prism.oregonstate.edu/normals/
# Step 2: Save the BIL file to the data directory in this repository
# Step 3: Set your own file paths to the data in the 'Paths.R' file
# Use the code found in 'Preparation.R' and 'Paths.R' files
## Loads sixteen objects
### A) 'ca' a large 'SpatVector' of California counties
### B) 'ca_buffer' a small 'SpatVector' of California state boundary geographically buffered a little larger
### C) 'ca_buffer_proj' a small 'SpatVector' of California state boundary geographically buffered a little larger and projected to UTM10N
### D) 'CA_proj' a small 'SpatVector' of California state boundary projected to UTM 10N
### E) 'cdph_coyote_sp' a large 'sf' of CDPH coyote plague data
### F) 'mask_pc1' a 'SpatRaster' of principal component 1 in California
### G) 'mask_pc2' a 'SpatRaster' of principal component 2 in California
### H) 'crs_us' a 'string' of PROJ4 coordinate reference system for WG84
### I) 'lrr_raster' a 'SpatRaster ' of log RR_[coyote+] in 'covariate space'
### J) 'Narrow2' a 'sf' of a North Arrow for figures at UTM10N
### K) 'nfld' a 'numeric' value of k=25 folds of cross-validation
### L) 'obs_dat' a 'data.frame' of observed coyote data with coordinates for 'geographic space' and 'covariate space' and a flag for seropositivity
### M) 'out' a 'list' of the output from an envi::lrren model
### N) 'out_univar' a 'data.frame' of log RR_[coyote+], significance levels, climate data, and elevation in 'covariate space,' includes a flag for outside of inner polygon or areas with 'sparse data'
### O) 'predict_risk_reclass' a 'SpatRaster' of log RR_[coyote+] in 'geographic space' at UTM10N
### P) 'reclass_tol' a 'SpatRaster' of log RR_[coyote+] significant levels at two-tailed alpha levels in 'geographic space' at UTM10N
source(file.path('code', 'Preparation.R'))
# -------------- #
# POSTPROCESSING #
# -------------- #
# Restrict inference to within the inner polygon or 'extent of coyote data' (i.e., more than 'sparse data')
conserved_univar <- out_univar[out_univar$outside == FALSE, ]
# --------------------- #
# SUPPLEMENTAL Figure 7 #
# --------------------- #
f <- 2.5 # Graphical expansion factor
png(
file = file.path('figures', 'SupplementalFigure7.png'),
width = 6*f,
height = 7*f,
units = 'in',
res = 200*f
)
layout(
matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 9), ncol = 2, byrow = TRUE),
heights = c(0.23, 0.23, 0.23, 0.23, 0.08)
)
par(
pty = 'm', oma = c(0, 0, 0, 0), mar = c(5.1, 6.1, 1.1, 2.1), family = 'LM Roman 10'
)
# Precipitation
plot.gam(
gam(rr ~ s(ppt), data = conserved_univar),
residuals = FALSE,
all.terms = FALSE,
shade = TRUE,
col = 'black',
shade.col = 'grey80',
xlab = 'precipitation (millimeters)',
ylab = expression('log'~hat('RR')['coyote+']),
select = 1,
shift = coef(gam(rr ~ s(ppt), data = conserved_univar))[1],
ylim = c(-8, max(conserved_univar$rr) * 1.5),
cex = 1*f,
cex.axis = 0.8*f,
cex.lab = 1*f
)
abline(h = 0, col = 'black', lwd = 1*f, lty = 2)
# Maximum Temperature
plot.gam(
gam(rr ~ s(tmax), data = conserved_univar),
residuals = FALSE,
all.terms = TRUE,
shade = TRUE,
col = 'black',
shade.col = 'grey80',
xlab = 'maximum temperature (degrees Celsius)',
ylab = expression('log'~hat('RR')['coyote+']),
select = 1,
shift = coef(gam(rr ~ s(tmax), data = conserved_univar))[1],
ylim = c(-8, max(conserved_univar$rr)*1.5),
cex = 1*f,
cex.axis = 0.8*f,
cex.lab = 1*f
)
abline(h = 0, col = 'black', lwd = 1*f, lty = 2)
# Mean Temperature
plot.gam(
gam(rr ~ s(tmean), data = conserved_univar),
residuals = FALSE,
all.terms = TRUE,
shade = TRUE,
col = 'black',
shade.col = 'grey80',
xlab = 'mean temperature (degrees Celsius)',
ylab = expression('log'~hat('RR')['coyote+']),
select = 1,
shift = coef(gam(rr ~ s(tmean), data = conserved_univar))[1],
ylim = c(-8, max(conserved_univar$rr)*1.5),
cex = 1*f,
cex.axis = 0.8*f,
cex.lab = 1*f
)
abline(h = 0, col = 'black', lwd = 1*f, lty = 2)
# Minimum Temperature
plot.gam(
gam(rr ~ s(tmin), data = conserved_univar),
residuals = FALSE,
all.terms = TRUE,
shade = TRUE,
col = 'black',
shade.col = 'grey80',
xlab = 'minimum temperature (degrees Celsius)',
ylab = expression('log'~hat('RR')['coyote+']),
select = 1,
shift = coef(gam(rr ~ s(tmin),data = conserved_univar))[1],
ylim = c(-8, max(conserved_univar$rr)*1.5),
cex = 1*f,
cex.axis = 0.8*f,
cex.lab = 1*f
)
abline(h = 0, col = 'black', lwd = 1*f, lty = 2)
# Dew Point Temperature
plot.gam(
gam(rr ~ s(tdmean), data = conserved_univar),
residuals = FALSE,
all.terms = TRUE,
shade = TRUE,
col = 'black',
shade.col = 'grey80',
xlab = 'dew point temperature (degrees Celsius)',
ylab = expression('log'~hat('RR')['coyote+']),
select = 1,
shift = coef(gam(rr ~ s(tdmean), data = conserved_univar))[1],
ylim = c(-8, max(conserved_univar$rr)*1.5),
cex = 1*f,
cex.axis = 0.8*f,
cex.lab = 1*f
)
abline(h = 0, col = 'black', lwd = 1*f, lty = 2)
# Maximum Vapor Pressure Deficit
plot.gam(
gam(rr ~ s(vpdmax), data = conserved_univar),
residuals = FALSE,
all.terms = TRUE,
shade = TRUE,
col = 'black',
shade.col = 'grey80',
xlab = 'maximum vapor pressure deficit (hectopascal)',
ylab = expression('log'~hat('RR')['coyote+']),
select = 1,
shift = coef(gam(rr ~ s(vpdmax), data = conserved_univar))[1],
ylim = c(-8, max(conserved_univar$rr)*1.5),
cex = 1*f,
cex.axis = 0.8*f,
cex.lab = 1*f
)
abline(h = 0, col = 'black', lwd = 1*f, lty = 2)
# Maximum Vapor Pressure Deficit
plot.gam(
gam(rr ~ s(vpdmin), data = conserved_univar),
residuals = FALSE,
all.terms = TRUE,
shade = TRUE,
col = 'black',
shade.col = 'grey80',
xlab = 'minimum vapor pressure deficit (hectopascal)',
ylab = expression('log'~hat('RR')['coyote+']),
select = 1,
shift = coef(gam(rr ~ s(vpdmin), data = conserved_univar))[1],
ylim = c(-8, max(conserved_univar$rr)*1.5),
cex = 1*f,
cex.axis = 0.8*f,
cex.lab = 1*f
)
abline(h = 0, col = 'black', lwd = 1*f, lty = 2)
# Elevation
plot.gam(
gam(rr ~ s(elev), data = conserved_univar),
residuals = FALSE,
all.terms = TRUE,
shade = TRUE,
col = 'black',
shade.col = 'grey80',
xlab = 'elevation (meters)',
ylab = expression('log'~hat('RR')['coyote+']),
select = 1,
shift = coef(gam(rr ~ s(elev), data = conserved_univar))[1],
ylim = c(-8, max(conserved_univar$rr)*1.5),
cex = 1*f,
cex.axis = 0.8*f,
cex.lab = 1*f
)
abline(h = 0, col = 'black', lwd = 1*f, lty = 2)
plot.new()
legend(
x = 'top',
horiz = TRUE,
inset = 0,
legend = c(
expression('null log'~hat('RR')['coyote+']~'(reference)'),
'univariate generalized additive model',
'95% confidence interval'
),
lty = c(2, 1, NA),
pch = c(NA, NA, 15),
col = c('black', 'black', 'grey80'),
lwd = 1*f,
cex = 0.8*f,
bty = 'n'
)
dev.off()
# ---------------------------------- END OF CODE ---------------------------------- #