-
Notifications
You must be signed in to change notification settings - Fork 13
/
zoonDemo.R
306 lines (159 loc) · 7.22 KB
/
zoonDemo.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
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
# Simplest example
library(zoon)
work1 <- workflow(occurrence = UKAnophelesPlumbeus,
covariate = UKBioclim,
process = OneHundredBackground,
model = LogisticRegression,
output = PrintMap)
# Help
GetModuleList()
ModuleHelp(UKBioclim)
# Modules with arguments
ModuleHelp(SpOcc)
work2 <- workflow(occurrence = SpOcc(species = 'Loxia scotica',
extent=c(-10, 10, 45, 65)),
covariate = UKBioclim,
process = OneHundredBackground,
model = LogisticRegression,
output = PrintMap)
# Combining modules
# Do you want 1 analysis?
?Chain
work3 <- workflow(occurrence = Chain(SpOcc(species = 'Eresus kollari',
extent=c(-10, 10, 45, 65)),
SpOcc(species = 'Eresus sandaliatus',
extent=c(-10, 10, 45, 65))),
covariate = UKBioclim,
process = OneHundredBackground,
model = LogisticRegression,
output = PrintMap)
# Or many separate analyses
work4 <- workflow(occurrence = list(SpOcc(species = 'Eresus kollari',
extent=c(-10, 10, 45, 65)),
SpOcc(species = 'Eresus sandaliatus',
extent=c(-10, 10, 45, 65))),
covariate = UKBioclim,
process = OneHundredBackground,
model = LogisticRegression,
output = PrintMap)
# This should be in a module, but isnt.
par(mfrow=c(1,2))
plot(work4$report[[1]], main = 'Eresus kollari')
plot(work4$report[[2]], main = 'Eresus sandaliatus')
# Crossvalidation
ModuleHelp(BackgroundAndCrossvalid)
work5 <- workflow(occurrence = UKAnophelesPlumbeus,
covariate = UKBioclim,
process = BackgroundAndCrossvalid,
model = LogisticRegression,
output = PerformanceMeasures)
work5$report
# A larger example
# Note: only one module type can be in a list
# Many module types can be chained
# Model modules can be in a list, not in a chain
work6 <- workflow(occurrence = Chain(SpOcc(species = 'Eresus kollari',
extent=c(-10, 10, 45, 65)),
SpOcc(species = 'Eresus sandaliatus',
extent=c(-10, 10, 45, 65))),
covariate = UKBioclim,
process = BackgroundAndCrossvalid(k=2),
model = list(LogisticRegression, RandomForest),
output = Chain(PrintMap, PerformanceMeasures)
)
str(work6, 1)
par(mfrow=c(1,2))
plot(work6$report[[1]][[1]],
main=paste('Logistic Regression: AUC = ',
round(work6$report[[1]][[2]]$auc, 2)))
plot(work6$report[[2]][[1]],
main=paste('Random forest: AUC = ',
round(work6$report[[2]][[2]]$auc, 2)))
# Building modules
# A model module
# Input is a dataframe with columns value, type, fold, longitude, latitude then 6:ncol(df) covar columns
# Can have other arguments
NewModule <- function(df){
zoon:::GetPackage("gam")
covs <- as.data.frame(df[, 6:ncol(df)])
names(covs) <- names(df)[6:ncol(df)]
m <- gam::gam(formula = df$value ~ .,
data = covs,
family = binomial)
# Output a model object. The object class must have a predict method available.
# If it doesnt, define one here (see BiomodModel at link below for example)
# https://github.com/zoonproject/modules/blob/master/R/BiomodModel.R
return (m)
}
BuildModule(NewModule, type = "Model", dir = ".",
description = "My cool new module")
rm(NewModule)
LoadModule('NewModule.R')
work1 <- workflow(occurrence = UKAnophelesPlumbeus,
covariate = UKBioclim,
process = OneHundredBackground,
model = NewModule,
output = PrintMap)
################################################################################################################
# What follows is a simple example of each module type.
# If you wish to build a module you can use these as an outline
# The names and structure of input arguments are important
# And must match these, even if some arguments arent used
# Class, strucuter and column names of return value is also constrained.
# Structure of an occurrence module
# Input can be anything
SpOcc <- function(species, extent, databases = gbif){
zoon:::GetPackage(spocc)
raw <- occ2df(occ(query = species, geometry = extent, from = databases, limit=10e5))
occurrence <- raw[,c(longitude, latitude)]
occurrence$value <- 1
occurrence$type <- presence
occurrence$fold <- 1
# Must return a dataframe with columns longitude, latitude, value, type and fold
# Value can be 1/0 for presence absence or numbers for abundance etc.
# type is mostly presence, presence/absence or abundance string. Not settled yet.
# Fold is just a column of 1s. It is currently needed (though I imagine it might be moved out of modules).
# No other columns. Exact names please.
return(occurrence)
}
# Then run BuildModule to create the module file properly.
BuildModule(SpOcc, type = "Occurrence", dir = ".",
# Be a good citizen! Describe your module well including parameters
# These are autobuilt into documentation.
# Note: required arguments such as df in the model module above should not be documented.
description = "My cool new module species occurrence module",
paras = list(species = The species name,
extent = latitudinal, longitudinal extent as numeric vector,
databases = Character vector of databases to use from gbif, inat, ebird. Defaults to just gbif.))
# Structure of a covariate module
# Any input
LocalRaster <- function(filenames){
if(is.character(filenames)){
raster <- raster(filenames)
} else if(is.list(filenames)) {
rasterList <- lapply(filenames, raster)
raster <- stack(rasterList)
}
# Must return a raster object. Layer, stack or brick.
return(raster)
}
# Structure of process module
# Input and output are the same
# Must accept/return a list. Input list must be called data.
# First element is a df w/ value, type, fold, longitude, latitude + covs
# Second element is a raster layer, stack or brick.
# Other input arguments is ok
NoProcess <- function (data) {
occurrence <- data$df
ras <- data$ras
noccurrence <- nrow(occurrence)
df <- occurrence
names(df)[6:ncol(df)] <- names(ras)
return(list(df=df, ras=ras))
}
# Structure of an output module
# Input args are a model object called model and a raster object called ras
# + Any other arguments
# I imagine this will change so it expects the occurrence data as well
SameTimePlaceMap <- function (model, ras) {
# Output can be anything. Go nuts.