forked from rvalavi/blockCV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
512 lines (512 loc) · 19.1 KB
/
.Rhistory
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# create an empty vector to store the AUC of each fold
AUCs <- vector(mode = "numeric")
for(k in seq_len(length(folds))){
trainSet <- which(k != folds) # extract the training set indices
testSet <- which(k == folds) # extract the testing set indices
# fitting a maxent model using linear, quadratic and hinge features
mx <- maxnet(p = pb[trainSet],
data = mydata[trainSet, ],
maxnet.formula(p = pb[trainSet],
data = mydata[trainSet, ],
classes = "default"))
testTable <- pb_data[testSet, ] # a table for testing predictions and reference data
testTable$pred <- predict(mx, mydata[testSet, ], type="cloglog") # predict the test set
# calculate AUC using calc_auc function in plotROC package
auc <- calc_auc(ggplot(testTable, aes(m=pred, d=Species)) + geom_roc(n.cuts = 0))[3]
AUCs[k] <- as.numeric(auc)
}
# loading the libraries
library(maxnet)
library(plotROC)
# extract the raster values for the species points as a dataframe
mydata <- raster::extract(awt, pa_data)
mydata <- as.data.frame(mydata)
# create a vector of 1 (for presence) and 0 (for background samples)
pb <- pb_data$Species
# extract the folds in spatialBlock object created in the previous section (with presence-background data)
folds <- sb2$folds
# create an empty vector to store the AUC of each fold
AUCs <- vector(mode = "numeric")
for(k in seq_len(unique(folds))){
trainSet <- which(k != folds) # extract the training set indices
testSet <- which(k == folds) # extract the testing set indices
# fitting a maxent model using linear, quadratic and hinge features
mx <- maxnet(p = pb[trainSet],
data = mydata[trainSet, ],
maxnet.formula(p = pb[trainSet],
data = mydata[trainSet, ],
classes = "default"))
testTable <- pb_data[testSet, ] # a table for testing predictions and reference data
testTable$pred <- predict(mx, mydata[testSet, ], type="cloglog") # predict the test set
# calculate AUC using calc_auc function in plotROC package
auc <- calc_auc(ggplot(testTable, aes(m=pred, d=Species)) + geom_roc(n.cuts = 0))[3]
AUCs[k] <- as.numeric(auc)
}
seq_len(unique(folds))
seq_len(unique(folds))
# extract the folds in spatialBlock object created in the previous section (with presence-background data)
folds <- sb2$folds
seq_len(unique(folds))
folds
seq_len(unique(folds))
# extract the folds in spatialBlock object created in the previous section (with presence-background data)
folds <- sb2$foldID
folds
seq_len(unique(folds))
unique(folds)
seq_len(max(folds))
seq_len(5)
# loading the libraries
library(maxnet)
library(plotROC)
# extract the raster values for the species points as a dataframe
mydata <- raster::extract(awt, pa_data)
mydata <- as.data.frame(mydata)
# create a vector of 1 (for presence) and 0 (for background samples)
pb <- pb_data$Species
# extract the folds in spatialBlock object created in the previous section (with presence-background data)
folds <- sb2$foldID
# create an empty vector to store the AUC of each fold
AUCs <- vector(mode = "numeric")
for(k in seq_len(5)){
trainSet <- which(k != folds) # extract the training set indices
testSet <- which(k == folds) # extract the testing set indices
# fitting a maxent model using linear, quadratic and hinge features
mx <- maxnet(p = pb[trainSet],
data = mydata[trainSet, ],
maxnet.formula(p = pb[trainSet],
data = mydata[trainSet, ],
classes = "default"))
testTable <- pb_data[testSet, ] # a table for testing predictions and reference data
testTable$pred <- predict(mx, mydata[testSet, ], type="cloglog") # predict the test set
# calculate AUC using calc_auc function in plotROC package
auc <- calc_auc(ggplot(testTable, aes(m=pred, d=Species)) + geom_roc(n.cuts = 0))[3]
AUCs[k] <- as.numeric(auc)
}
folds
seq_len(5)
k
trainSet <- which(k != folds)
trainSet
testSet <- which(k == folds)
testSet
which(folds != k)
which(folds == k)
trainSet <- which(folds != k) # extract the training set indices
testSet <- which(folds == k) # extract the testing set indices
# fitting a maxent model using linear, quadratic and hinge features
mx <- maxnet(p = pb[trainSet],
data = mydata[trainSet, ],
maxnet.formula(p = pb[trainSet],
data = mydata[trainSet, ],
classes = "default"))
# loading the libraries
library(maxnet)
library(plotROC)
# extract the raster values for the species points as a dataframe
mydata <- raster::extract(awt, pb_data)
mydata <- as.data.frame(mydata)
# create a vector of 1 (for presence) and 0 (for background samples)
pb <- pb_data$Species
# extract the folds in spatialBlock object created in the previous section (with presence-background data)
folds <- sb2$foldID
# create an empty vector to store the AUC of each fold
AUCs <- vector(mode = "numeric")
for(k in seq_len(5)){
trainSet <- which(folds != k) # extract the training set indices
testSet <- which(folds == k) # extract the testing set indices
# fitting a maxent model using linear, quadratic and hinge features
mx <- maxnet(p = pb[trainSet],
data = mydata[trainSet, ],
maxnet.formula(p = pb[trainSet],
data = mydata[trainSet, ],
classes = "default"))
testTable <- pb_data[testSet, ] # a table for testing predictions and reference data
testTable$pred <- predict(mx, mydata[testSet, ], type="cloglog") # predict the test set
# calculate AUC using calc_auc function in plotROC package
auc <- calc_auc(ggplot(testTable, aes(m=pred, d=Species)) + geom_roc(n.cuts = 0))[3]
AUCs[k] <- as.numeric(auc)
}
# print the mean and standard deviation of AUCs
print(mean(AUCs))
precrec_obj
auc(precrec_obj)
auc(precrec_obj)[1,3]
auc(precrec_obj)[1,]
auc(precrec_obj)[1,4]
# loading the libraries
library(maxnet)
library(ROCR)
# extract the raster values for the species points as a dataframe
mydata <- raster::extract(awt, pb_data)
mydata <- as.data.frame(mydata)
# create a vector of 1 (for presence) and 0 (for background samples)
pb <- pb_data$Species
# extract the folds in spatialBlock object created in the previous section (with presence-background data)
folds <- sb2$foldID
# create an empty vector to store the AUC of each fold
AUCs <- vector(mode = "numeric")
for(k in seq_len(5)){
trainSet <- which(folds != k) # extract the training set indices
testSet <- which(folds == k) # extract the testing set indices
# fitting a maxent model using linear, quadratic and hinge features
mx <- maxnet(p = pb[trainSet],
data = mydata[trainSet, ],
maxnet.formula(p = pb[trainSet],
data = mydata[trainSet, ],
classes = "default"))
testTable <- pb_data[testSet, ] # a table for testing predictions and reference data
testTable$pred <- predict(mx, mydata[testSet, ], type = "cloglog") # predict the test set
# calculate AUC using calc_auc function in plotROC package
precrec_obj <- evalmod(scores = testTable$pred, labels = testTable$Species)
AUCs[k] <- auc(precrec_obj)[1,4] # extract AUC-ROC
}
# print the mean and standard deviation of AUCs
print(mean(AUCs))
# loading the libraries
library(maxnet)
library(precrec)
# extract the raster values for the species points as a dataframe
mydata <- raster::extract(awt, pb_data)
mydata <- as.data.frame(mydata)
# create a vector of 1 (for presence) and 0 (for background samples)
pb <- pb_data$Species
# extract the folds in spatialBlock object created in the previous section (with presence-background data)
folds <- sb2$foldID
# create an empty vector to store the AUC of each fold
AUCs <- vector(mode = "numeric")
for(k in seq_len(5)){
trainSet <- which(folds != k) # extract the training set indices
testSet <- which(folds == k) # extract the testing set indices
# fitting a maxent model using linear, quadratic and hinge features
mx <- maxnet(p = pb[trainSet],
data = mydata[trainSet, ],
maxnet.formula(p = pb[trainSet],
data = mydata[trainSet, ],
classes = "default"))
testTable <- pb_data[testSet, ] # a table for testing predictions and reference data
testTable$pred <- predict(mx, mydata[testSet, ], type = "cloglog") # predict the test set
# calculate AUC using calc_auc function in plotROC package
precrec_obj <- evalmod(scores = testTable$pred, labels = testTable$Species)
AUCs[k] <- auc(precrec_obj)[1,4] # extract AUC-ROC
}
# print the mean and standard deviation of AUCs
print(mean(AUCs))
# loading the libraries
library(randomForest)
library(precrec)
# library(ggplot2)
# extract the raster values for the species points as a dataframe
mydata <- raster::extract(awt, pa_data, df=TRUE)
# adding species column to the dataframe
mydata$Species <- as.factor(pa_data$Species)
# remove extra column (ID)
mydata <- mydata[,-1]
# extract the foldIDs in SpatialBlock object created in the previous section
folds <- bf1$folds
# create a data.frame to store the prediction of each fold (record)
testTable <- pa_data
testTable$pred <- NA
for(k in seq_len(length(folds))){
trainSet <- unlist(folds[[k]][1]) # extract the training set indices
testSet <- unlist(folds[[k]][2]) # extract the testing set indices
rf <- randomForest(Species~., mydata[trainSet, ], ntree = 250) # model fitting on training set
testTable$pred[testSet] <- predict(rf, mydata[testSet, ], type = "prob")[,2] # predict the test set
}
# calculate Area Under the ROC and PR curves and plot the result
precrec_obj <- evalmod(scores = testTable$pred, labels = testTable$Species)
autoplot(precrec_obj)
precrec_obj
# loading the libraries
library(randomForest)
library(precrec)
# extract the raster values for the species points as a dataframe
mydata <- raster::extract(awt, pa_data, df=TRUE)
# adding species column to the dataframe
mydata$Species <- as.factor(pa_data$Species)
# remove extra column (ID)
mydata <- mydata[,-1]
# extract the foldIDs in SpatialBlock object created in the previous section
folds <- bf1$folds
# create a data.frame to store the prediction of each fold (record)
testTable <- pa_data
testTable$pred <- NA
for(k in seq_len(length(folds))){
trainSet <- unlist(folds[[k]][1]) # extract the training set indices
testSet <- unlist(folds[[k]][2]) # extract the testing set indices
rf <- randomForest(Species~., mydata[trainSet, ], ntree = 250) # model fitting on training set
testTable$pred[testSet] <- predict(rf, mydata[testSet, ], type = "prob")[,2] # predict the test set
}
# calculate Area Under the ROC and PR curves and plot the result
precrec_obj <- evalmod(scores = testTable$pred, labels = testTable$Species)
autoplot(precrec_obj)
precrec_obj
autoplot(precrec_obj)
library(blockCV)
library(blockCV)
library(blockCV)
usethis::use_appveyor()
library(blockCV)
devtools::release_checks()
browseVignettes("blockCV")
library(blockCV)
browseVignettes("blockCV")
devtools::build_vignettes()
browseVignettes("blockCV")
devtools::release_checks()
library(blockCV)
devtools::release_checks()
browseVignettes("blockCV")
devtools::build()
browseVignettes("blockCV")
browseVignettes("blockCV")
devtools::build()
library(blockCV)
browseVignettes("blockCV")
devtools::build()
browseVignettes("blockCV")
vignettes("blockCV")
vignette("blockCV")
devtools::build()
library(blockCV)
browseVignettes("blockCV")
library(blockCV)
library(blockCV)
citation("blockCV")
?bibentry
library(blockCV)
library(blockCV)
library(blockCV)
devtools::release()
devtools::release(pkg = "/Users/rvalavi/Dropbox/My PhD thesis/My package/blockCV")
devtools::release(pkg = "blockCV")
devtools::release(pkg = "/Users/rvalavi/Dropbox/My PhD thesis/My package/blockCV_2.0.0.tar.gz")
devtools::release(pkg = "/Users/rvalavi/Dropbox/My PhD thesis/My package/blockCV_2.0.0.tgz")
devtools::release(pkg = "/Users/rvalavi/Dropbox/My PhD thesis/My package/blockCV")
update.packages("devtools")
library(blockCV)
pkg <- devtools::as.package(create = TRUE)
pkg <- devtools::as.package(x = "/Users/rvalavi/Dropbox/My PhD thesis/My package/blockCV", create = TRUE)
devtools::release(pkg = pkg)
devtools::build_win()
library(devtools)
pkg <- as.package(x = "/Users/rvalavi/Dropbox/My PhD thesis/My package/blockCV_2.0.0.tar.gz")
pkg <- as.package(x = "/Users/rvalavi/Dropbox/My PhD thesis/My package/blockCV_2.0.0.tar.gz", create = TRUE)
pkg <- as.package(x = "/Users/rvalavi/Dropbox/My PhD thesis/My package/blockCV_2.0.0.tar", create = TRUE)
pkg <- as.package(x = "/Users/rvalavi/Dropbox/My PhD thesis/My package", create = TRUE)
pkg <- as.package(x = "/Users/rvalavi/Dropbox/My PhD thesis/My package/blockCV", create = TRUE)
build_win(pkg = ".")
build_win(pkg = pkg)
citation("biomod2")
citation("maxney")
citation("maxnet")
citation("glmnet")
library(blockCV)
install.packages(c("backports", "bigreadr", "callr", "classInt", "covr", "curl", "data.table", "devtools", "digest", "DT", "effects", "feather", "fields", "gdtools", "ggimage", "gstat", "htmlTable", "htmltools", "htmlwidgets", "huxtable", "jpeg", "keras", "KernSmooth", "knitr", "later", "lintr", "maptools", "mgcv", "microbenchmark", "miscTools", "ncdf4", "ncmeta", "pkgbuild", "plotmo", "pls", "polspline", "pool", "promises", "purrr", "qvcalc", "R.rsp", "RcppArmadillo", "rgdal", "rgeos", "rlang", "rmarkdown", "RNetCDF", "rvcheck", "shiny", "slam", "spam", "sparklyr", "spatstat", "spData", "spdep", "stars", "stringdist", "strucchange", "tensorflow", "tinytex", "tmap", "units", "vtreat", "wrapr", "xfun"))
install.packages(c("mgcv", "slam", "spam", "stringdist"))
library(roxygen2)
devtools::install_github("r-lib/roxygen2")
detach("package:roxygen2", unload = TRUE)
devtools::document()
source('~/Dropbox/My PhD thesis/My package/blockCV/R/explorer.R')
library(blockCV)
library(blockCV)
library(blockCV)
library(‘pandoc)
library(blockCV)
library(blockCV)
library(blockCV)
library(blockCV)
library(blockCV)
install.packages("spatialreg")
?spatialreg::predict.sarlm
.libPaths()
library(blockCV)
install.packages("~/Dropbox/My PhD thesis/My package/blockCV_2.0.0.tar.gz", repos = NULL, type = "source")
browseVignettes(package = "blockCV")
?spatialBlock
library(blockCV)
library(covr)
tmp <- Sys.time()
cov1 <- covr::package_coverage(function_exclusions = c("foldExplorer", "rangeExplorer"))
Sys.time() - tmp
rm(tmp)
library(blockCV)
install.packages("rhub")
rhub::check(platform = "MacOS")
rhub::check(platform = "macOS")
rhub::platforms()
rhub::check(platform = "debian-gcc-devel")
library(covr)
tmp <- Sys.time()
cov1 <- covr::package_coverage(function_exclusions = c("foldExplorer", "rangeExplorer"))
Sys.time() - tmp
rm(tmp)
# print the coverage
cov1
# visualise the code coverage
report(cov1)
rm(cov1)
awt <- raster::brick(system.file("extdata", "awt.grd", package = "blockCV"))
# import presence-absence species data
PA <- read.csv(system.file("extdata", "PA.csv", package = "blockCV"))
# make a sf object from data.frame
pa_data <- sf::st_as_sf(PA, coords = c("x", "y"), crs = raster::crs(awt))
sb4 <- spatialBlock(speciesData = pa_data,
species = "response", # wrong response
rasterLayer = awt,
maskBySpecies = FALSE,
theRange = 70000,
k = 5)
tmp <- Sys.time()
cov1 <- covr::package_coverage(function_exclusions = c("foldExplorer", "rangeExplorer"))
Sys.time() - tmp
rm(tmp)
# print the coverage
cov1
tmp <- Sys.time()
cov1 <- covr::package_coverage(function_exclusions = c("foldExplorer", "rangeExplorer"))
Sys.time() - tmp
rm(tmp)
# print the coverage
cov1
rhub::check_for_cran()
install.packages("/Users/rvalavi/Dropbox/My PhD thesis/My package/blockCV_2.0.1.tar.gz", repos = NULL, type = "source")
library(blockCV)
?blockCV
library(blockCV)
install.packages("/Users/rvalavi/Dropbox/My PhD thesis/My package/blockCV_2.0.1.tar.gz", repos = NULL, type = "source")
library(blockCV)
?blockCV
browseVignettes('blockCV')
rhub::check_for_cran()
tmp <- Sys.time()
cov1 <- covr::package_coverage(function_exclusions = c("foldExplorer", "rangeExplorer"))
Sys.time() - tmp
rm(tmp)
tmp <- Sys.time()
cov1 <- covr::package_coverage(function_exclusions = c("foldExplorer", "rangeExplorer"))
Sys.time() - tmp
rm(tmp)
# print the coverage
cov1
rhub::platforms()
library(rhub)
rhub::check(platform = "windows-x86_64-release")
rhub::check(platform = "debian-gcc-release")
library(blockCV)
library(blockCV)
library(blockCV)
?blockCV::spatialAutoRange
awt <- raster::brick(system.file("extdata", "awt.grd", package = "blockCV"))
range1 <- spatialAutoRange(rasterLayer = awt,
sampleNumber = 5000, # number of cells to be used
doParallel = TRUE,
nCores = 2, # if NULL, it uses half of the CPU cores
plotVariograms = FALSE,
showPlots = TRUE)
range1
range1
plot(range1$variograms[[1]])
library(automap)
plot(range1$variograms[[1]])
library(blockCV)
stats::complete.cases()
utils::install.packages()
library(blockCV)
library(blockCV)
library(blockCV)
library(blockCV)
library(blockCV)
library(blockCV)
requireNamespace("foo", quietly = TRUE)
!requireNamespace("foo", quietly = TRUE)
library(blockCV)
library(blockCV)
sp::CRS("+init=epsg:4326")
sf::st_crs(4326)
library(blockCV)
library(blockCV)
library(rhub)
platforms()
rhub::check(platform = "debian-gcc-release")
library(blockCV)
?blockCV
library(blockCV)
?blockCV
rhub::platforms()
rhub::check(platform = "ubuntu-gcc-release")
rhub::check(platform = "debian-gcc-release")
library(blockCV)
rhub::check(platform = "debian-gcc-release")
raster::projectRaster
isLonLat
raster::isLonLat()
raster::isLonLat(r)
raster::isLonLat(raster())
raster::isLonLat(raster::raster())
library(blockCV)
library(blockCV)
library(blockCV)
?spatialAutoRange
awt <- raster::brick(system.file("extdata", "awt.grd", package = "blockCV"))
# import presence-absence species data
PA <- read.csv(system.file("extdata", "PA.csv", package = "blockCV"))
# make a sf object from data.frame
pa_data <- sf::st_as_sf(PA, coords = c("x", "y"), crs = raster::crs(awt))
raster::isLonLat(pa_data)
pa_data
raster::isLonLat(awt)
awt
library(blockCV)
library(blockCV)
awt <- raster::brick(system.file("extdata", "awt.grd", package = "blockCV"))
awt_wgs <- raster::projectRaster(awt, crs = "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0")
awt_wgs
plot(awt_wgs[[1]])
raster::plot(awt_wgs[[1]])
awt <- raster::brick(system.file("extdata", "awt.grd", package = "blockCV"))
expect_warning(awt_wgs <- raster::projectRaster(awt, crs = "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))
awt_wgs <- raster::projectRaster(awt, crs = "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0")
raster::projection(awt_wgs) <- NA
awt_wgs
net <- blockCV:::rasterNet(x = awt_wgs, resolution = 70000, mask = TRUE)
plot(net)
library(blockCV)
rhub::check(platform = "debian-gcc-release")
rhub::check(platform = "debian-gcc-release")
library(blockCV)
context("external helper functions fully")
awt <- raster::brick(system.file("extdata", "awt.grd", package = "blockCV"))
PA <- read.csv(system.file("extdata", "PA.csv", package = "blockCV"))
pa_data <- sf::st_as_sf(PA, coords = c("x", "y"), crs = crs(awt))
pa_data <- sf::st_as_sf(PA, coords = c("x", "y"), crs = raster::projection(awt))
suppressWarnings(awt_wgs <- raster::projectRaster(from = awt, crs = "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))
raster::projection(awt_wgs) <- NA
expect_warning(
net <- blockCV:::rasterNet(x = awt_wgs, resolution = 70000, mask = TRUE)
)
net <- blockCV:::rasterNet(x = awt_wgs, resolution = 70000, mask = TRUE)
net
net <- blockCV:::rasterNet(x = pa_data, resolution = 70000, mask = TRUE)
net
blockCV:::rasterNet(x = awt, mask = TRUE)
blockCV:::rasterNet(x = pa_data, xOffset = 3)
blockCV:::rasterNet(x = pa_data, yOffset = 3)
library(blockCV)
rhub::check(platform = "debian-gcc-release")
library(blockCV)
rhub::check(platform = "debian-gcc-release")
rhub::check(platform = "debian-gcc-release")
rhub::check(platform = "debian-gcc-release")
rhub::platforms()
rhub::check(platform = "windows-x86_64-release")
context
library(blockCV)
interactive()
install.packages(c("cowplot", "ggplot2"))
install.packages(c("cowplot", "ggplot2"))
library(blockCV)
library(blockCV)
library(blockCV)
browseVignettes("blockCV")