-
Notifications
You must be signed in to change notification settings - Fork 0
/
exploring_interpolation_for_bioticVelocity.r
231 lines (151 loc) · 7.39 KB
/
exploring_interpolation_for_bioticVelocity.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
# source('C:/Ecology/Drive/Research/ABC vs Biogeography/NSF_ABI_2018_2021/data_and_analyses/green_ash/enms/code/exploring_interpolation_for_bioticVelocity.r')
rm(list=ls())
memory.limit(memory.limit() * 2^30)
library(raster)
library(omnibus)
library(statisfactory)
library(enmSdm)
exts <- c(80, 160, 320) # in km
gcms <- c('ccsm', 'ecbilt')
# algos <- c('brt', 'glm', 'maxent', 'ns')
algos <- c('glm')
setwd('C:/Ecology/Drive/Research/ABC vs Biogeography/NSF_ABI_2018_2021/data_and_analyses/green_ash/enms')
if (file.exists('./figures_and_tables/predictors.rda')) load('./figures_and_tables/predictors.rda')
lorenzPath <- 'D:/Ecology/Climate/Lorenz et al 2016 North America 21Kybp to 2100 CE/'
studyRegionRastsFileName <- 'C:/Ecology/Drive/Research/ABC vs Biogeography/NSF_ABI_2018_2021/data_and_analyses/green_ash/study_region/!study_region_raster_masks/study_region_daltonIceMask_lakesMasked_linearIceSheetInterpolation.tif'
getClimRasts <- function(gcm, year, variables, rescale = TRUE, fillCoasts = FALSE) {
# gcm 'ccsm' or 'ecbilt'
# year year BP (from 0 to 21000 for ccsm or 22000 for ecbilt)
# variables names of variables
# rescale TRUE ==> rescale rasters to [0, 1] using present-day values for min/max
# fillCoasts FALSE ==> use rasters as-is; TRUE ==> extrapolate to NA cells immediately adjacent to non-NA cells (typically coastal cells)
gcmFolder <- if (gcm == 'ccsm') {
'ccsm3_22-0k_all_tifs'
} else if (gcm == 'ecbilt') {
'ecbilt_21-0k_all_tifs'
}
# get current version of each variable
for (variable in variables) {
rast <- stack(paste0(lorenzPath, '/V2/', gcmFolder, '/', year, 'BP/', variable, '.tif'))
names(rast) <- variable
rasts <- if (exists('rasts', inherits=FALSE)) {
stack(rasts, rast)
} else {
rast
}
}
# rescale
if (rescale) {
# not present... rescale by present-day values
if (year != 0 | file.exists ('./environmental_rasters/lorenz_et_al_2016/variable_statistics_0bp_across_north_america.csv')) {
variableStats <- read.csv('./environmental_rasters/lorenz_et_al_2016/variable_statistics_0bp_across_north_america.csv')
for (variable in variables) {
minVal <- variableStats$min[variableStats$gcm == gcm & variableStats$variable == variable]
maxVal <- variableStats$max[variableStats$gcm == gcm & variableStats$variable == variable]
rasts[[variable]] <- (rasts[[variable]] - minVal) / (maxVal - minVal)
}
# present-day... just rescale to [0, 1]
} else {
for (i in 1:nlayers(rasts)) rasts[[i]] <- stretch(rasts[[i]], 0, 1)
}
}
if (fillCoasts) {
name <- names(rasts)
# fill NA cells near coasts to account for fact that some records may not fall in a cell near a coast
for (i in 1:nlayers(rasts)) {
rasts[[i]] <- focal(rasts[[i]], w=matrix(1, nrow=3, ncol=3), fun=mean, na.rm=TRUE, NAonly=TRUE)
}
names(rasts) <- name
}
names(rasts) <- paste0(gcm, '_', variables)
rasts
}
###################################
###################################
###################################
# get study region rasters for present and 21 Kybp, rescale so fully-available land cells are 1 and fully-covered glacial cells are NA
studyRegionRasts <- brick(studyRegionRastsFileName)
names(studyRegionRasts) <- paste0('year', seq(21000, 0, by=-30), 'ybp')
# time periods represented by rasters
climYears <- seq(21000, 0, by=-500)
for (gcm in gcms) {
for (ext in exts) {
for (algo in algos) {
say(paste(gcm, ext, algo))
load(paste0('./models/final_model_for_', tolower(ext), 'km_extent_with_', algo, '_', gcm, '_gcm.rda'))
if (exists('preds')) rm(preds)
for (climYear in climYears) {
# get climate data
clim <- getClimRasts(gcm=gcm, year=climYear, variables=predictors, rescale=TRUE, fillCoasts=FALSE)
thisPred <- raster::predict(clim, model, fun=enmSdm::predictEnmSdm)
preds <- if (exists('preds')) {
stack(preds, thisPred)
} else {
thisPred
}
} # next year
interpFrom <- -1 * climYears
interpTo <- seq(-21000, 0, by=30)
predsLogit <- logitAdj(preds, epsilon=0)
source('C:/Ecology/Drive/R/enmSdm/R/interpolateRasters.r')
say('linear')
predsLinear <- interpolateRasters(preds, interpFrom=interpFrom, interpTo=interpTo, type='linear')
say('gam')
predsGamLogit <- interpolateRasters(predsLogit, interpFrom=interpFrom, interpTo=interpTo, type='gam', family='gaussian', onFail='linear')
say('poly')
predsPolyLogit <- interpolateRasters(predsLogit, interpFrom=interpFrom, interpTo=interpTo, type='poly', family='gaussian', onFail='linear')
say('bs')
predsBsLogit <- interpolateRasters(predsLogit, interpFrom=interpFrom, interpTo=interpTo, type='bs', family='gaussian', onFail='linear')
say('smooth')
predsSmoothLogit <- interpolateRasters(predsLogit, interpFrom=interpFrom, interpTo=interpTo, type='smooth.spline', onFail='linear')
predsGam <- probitAdj(predsGamLogit, epsilon=0)
predsPoly <- probitAdj(predsPolyLogit, epsilon=0)
predsBs <- probitAdj(predsBsLogit, epsilon=0)
predsSmooth <- probitAdj(predsSmoothLogit, epsilon=0)
xy <- rbind(c(-87.10108, 32.27589))
linear <- c(extract(predsLinear, xy))
gam <- c(extract(predsGam, xy))
poly <- c(extract(predsPoly, xy))
bs <- c(extract(predsBs, xy))
ss <- c(extract(predsSmooth, xy))
plot(interpTo, linear, type='l', ylim=c(0, 1), lwd=2)
lines(interpTo, gam, col='red', lwd=2)
lines(interpTo, poly, col='blue')
lines(interpTo, bs, col='orange')
lines(interpTo, ss, col='cyan')
legend('topleft', inset=0.01, legend=c('linear', 'gam', 'poly', 'bs', 'ss'), col=c('black', 'red', 'blue', 'orange', 'cyan'), lwd=1)
if (exists('bv')) rm(bv)
bv <- data.frame()
# biotic velocity for each type of interpolation
for (this in c('predsLinear', 'predsGam', 'predsPoly', 'predsBs', 'predsSmooth')) {
preds <- get(this)
# project
preds <- projectRaster(preds, studyRegionRasts)
preds <- calc(preds, fun=function(x) ifelse(x < 0, 0, x))
preds <- calc(preds, fun=function(x) ifelse(x > 1, 1, x))
# mask by study region and force values to be within [0, 1] (can get pushed outside this during re-projection)
for (i in 1:nlayers(preds)) {
landMask <- (1 - studyRegionRasts[[i]])
preds[[i]] <- preds[[i]] * landMask
}
names(preds) <- paste0('ybp', seq(21000, 0, by=-30))
source('C:/Ecology/Drive/R/enmSdm/R/bioticVelocity.r')
thisBv <- bioticVelocity(preds, time=interpTo, onlyInSharedCells=TRUE)
thisBv$interp <- this
bv <- rbind(bv, thisBv)
}
print(NON)
} # next algorithm
} # next extent
} # next GCM
# maxs <- max(bv$centroidVelocity)
maxs <- max(1000)
plot(1, xlim=c(-21000, 0), ylim=c(0, maxs), col='white', main='Different methods for interpolating suitability layers', ylab='Centroid velocity (m / y)', xlab='Year')
for (i in seq(-21000, 0, by=500)) lines(c(i, i), c(0, maxs), col='gray70')
interps <- c('predsLinear', 'predsGam', 'predsPoly', 'predsBs', 'predsSmooth')
for (this in seq_along(interps)) {
interp <- interps[this]
these <- bv[bv$interp == interp, ]
lines(these$timeTo, these$centroidVelocity, col=this, lwd=2)
}
legend('topright', legend=interps, lwd=1, col=seq_along(interps), bg='white')