Skip to content

Commit 2791350

Browse files
udpate fn and add tests for MakeMaxEntAbundance()
1 parent 0926159 commit 2791350

File tree

3 files changed

+95
-45
lines changed

3 files changed

+95
-45
lines changed

R/MakeMaxEntAbundance.R

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,78 +7,88 @@
77
#' @param land raster; a mask to be applied to the output usually landmasses
88
#' @param mask raster; an additional mask to apply to the output, if needed
99
#' @param type character; use "maxnet" for prob suitable habitat or "cloglog" for approximate abundance
10-
#' @param clamp Logical; just leave this as F; shoudl the covariates be restricted to the range observe in fitting the model?
11-
#' @param filename a filename to save results, "" writes to active memory
10+
#' @param clamp Logical; just leave this as F; should the covariates be restricted to the range observe in fitting the model?
11+
#' @param filename a filename to save results; "" writes to active memory
1212
#'
1313
#' @return a raster map with the desired prediction
1414
#' @export
1515
#' @importFrom terra values
1616
#' @importFrom terra setValues
1717
#'
1818
#' @examples
19-
MakeMaxEntAbundance<-function(model,
20-
maxent.stack,
21-
scale.fac=1,
22-
land=NULL,
23-
mask=NULL,
24-
type="cloglog",
25-
clamp=F,
26-
filename=""){
27-
28-
#correct a common mistake
29-
if(is.null(filename)||is.na(filename)){filename<-""}
19+
MakeMaxEntAbundance <- function(model,
20+
maxent.stack,
21+
scale.fac = 1,
22+
land = NULL,
23+
mask = NULL,
24+
type = "cloglog",
25+
clamp = F,
26+
filename = "") {
27+
# correct a common mistake
28+
if (is.null(filename) || is.na(filename)) {
29+
filename <- ""
30+
}
3031

3132
# Identify the type and make the main prediction
32-
type=tolower(type)
33+
type <- tolower(type)
3334
# Somewhat counterintuitive, but this is the type if using the cloglog link to make an abundance estimate
34-
if(type=="cloglog"){
35+
if (type == "cloglog") {
3536
# since ENMeval 2.0, they got rid of the useful function and I need to make the predictions the long way
36-
dat<-terra::values(maxent.stack)
37+
dat <- terra::values(maxent.stack)
3738

3839
# using predict with maxnet will quietly remove the NAs, so need to track them manually
39-
na.spots<-which(apply(X = dat,MARGIN = 1,FUN = function(x){return(any(is.na(x)))}))
40-
dat.spots<-which(seq(1:nrow(dat))%in%na.spots==F)
40+
na.spots <- which(apply(X = dat, MARGIN = 1, FUN = function(x) {
41+
return(any(is.na(x)))
42+
}))
43+
dat.spots <- which(seq(1:nrow(dat)) %in% na.spots == F)
4144

42-
preds<-stats::predict(model,dat[dat.spots,],type="link")
43-
preds2<-exp(preds+model$ent)*scale.fac
44-
new.vals<-vector(length=nrow(dat))
45-
new.vals[na.spots]<-NA
46-
new.vals[dat.spots]<-preds2
47-
habitat.prediction<-terra::setValues(x = terra::rast(maxent.stack[[1]]),values = new.vals) #terra needs to call just one raster in the stack
45+
preds <- stats::predict(model, dat[dat.spots, ], type = "link")
46+
preds2 <- exp(preds + model$ent) * scale.fac
47+
new.vals <- vector(length = nrow(dat))
48+
new.vals[na.spots] <- NA
49+
new.vals[dat.spots] <- preds2
50+
habitat.prediction <- terra::setValues(x = terra::rast(maxent.stack[[1]]), values = new.vals) # terra needs to call just one raster in the stack bc it's just setting dimensions.
4851
}
4952
# this makes a habitat suitability map from a maxnet model
50-
if(type=="maxnet"){
51-
dat<-terra::values(maxent.stack)
53+
if (type == "maxnet") {
54+
dat <- terra::values(maxent.stack)
5255

5356
# using predict with maxnet will quietly remove the NAs, so need to track them manually
54-
na.spots<-which(apply(X = dat,MARGIN = 1,FUN = function(x){return(any(is.na(x)))}))
55-
dat.spots<-which(seq(1:nrow(dat))%in%na.spots==F)
57+
na.spots <- which(apply(X = dat, MARGIN = 1, FUN = function(x) {
58+
return(any(is.na(x)))
59+
}))
60+
dat.spots <- which(seq(1:nrow(dat)) %in% na.spots == F)
5661

57-
preds<-stats::predict(model,dat[dat.spots,],type="cloglog")
58-
new.vals<-vector(length=nrow(dat))
59-
new.vals[na.spots]<-NA
60-
new.vals[dat.spots]<-preds
61-
habitat.prediction<-terra::setValues(x = terra::rast(maxent.stack),values = new.vals)
62+
preds <- stats::predict(model, dat[dat.spots, ], type = "cloglog")
63+
new.vals <- vector(length = nrow(dat))
64+
new.vals[na.spots] <- NA
65+
new.vals[dat.spots] <- preds
66+
habitat.prediction <- terra::setValues(x = terra::rast(maxent.stack[[1]]), values = new.vals)
6267
}
6368
# need to add a check to see about the strange problems with the EBS
64-
if(is.null(land)==F){
65-
habitat.prediction<-raster::extend(x=habitat.prediction,y=land)
69+
if (is.null(land) == F) {
70+
habitat.prediction <- raster::extend(x = habitat.prediction, y = land)
6671
}
6772

6873
# For some reason, the crs info isn't always carrying over
6974
terra::crs(habitat.prediction) <- terra::crs(maxent.stack)
70-
if(filename!=""){terra::writeRaster(x = habitat.prediction,filename = filename, overwrite = TRUE)}
75+
if (filename != "") {
76+
terra::writeRaster(x = habitat.prediction, filename = filename, overwrite = TRUE)
77+
}
7178

7279
# Apply additional masks if necessary
73-
if(is.null(land)==F){
74-
habitat.prediction<-terra::mask(habitat.prediction, land, inverse = T, overwrite = TRUE,
75-
filename = filename)
80+
if (is.null(land) == F) {
81+
habitat.prediction <- terra::mask(habitat.prediction, land,
82+
inverse = T, overwrite = TRUE,
83+
filename = filename
84+
)
7685
}
7786
# Apply additional masks if necessary
78-
if(is.null(mask)==F){
79-
habitat.prediction<-terra::mask(habitat.prediction, mask, overwrite = TRUE,
80-
filename = filename)
87+
if (is.null(mask) == F) {
88+
habitat.prediction <- terra::mask(habitat.prediction, mask,
89+
overwrite = TRUE,
90+
filename = filename
91+
)
8192
}
8293
return(habitat.prediction)
8394
}
84-

man/MakeMaxEntAbundance.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
library(terra)
2+
library(maxnet)
3+
4+
# testing data -------------------------------------------------------------
5+
# Create some presence/absence data to fit a model
6+
species.data <- readRDS(system.file("test_files", "goa_data_logarea_folds.rds", package = "EFHSDM"))
7+
data(raster_stack) # not sure if this will work in context. This is GOA data only.
8+
raster.stack <- terra::rast(raster_stack)
9+
names(raster.stack) <- c("lon", "lat", "bdepth", "btemp", "slope", "sponge")
10+
11+
maxnet.covars <- c("bdepth", "btemp", "slope")
12+
cofactors <- c("sponge")
13+
r.mult <- 0.5
14+
maxnet.model0 <- FitMaxnet(
15+
data = species.data,
16+
species = "dogfish",
17+
vars = maxnet.covars,
18+
facs = cofactors,
19+
regmult = r.mult,
20+
reduce = TRUE
21+
)
22+
23+
# tests -------------------------------------------------------------------
24+
test_that("cloglog prediction works and returns a SpatRaster", {
25+
result <- MakeMaxEntAbundance(model = maxnet.model0, maxent.stack = raster.stack,
26+
scale.fac = 1.619, type = "cloglog")
27+
expect_s4_class(result, "SpatRaster")
28+
expect_equal(ncell(result), ncell(raster.stack[[1]]))
29+
})
30+
31+
test_that("maxnet prediction type works", {
32+
result <- MakeMaxEntAbundance(model = maxnet.model0, maxent.stack = raster.stack,
33+
scale.fac = 1.619, type = "maxnet")
34+
expect_s4_class(result, "SpatRaster")
35+
})
36+
37+
test_that("does not error with NULL or NA filename", {
38+
expect_no_error(MakeMaxEntAbundance(maxnet.model0, raster.stack, filename = NULL))
39+
expect_no_error(MakeMaxEntAbundance(maxnet.model0, raster.stack, filename = NA))
40+
})

0 commit comments

Comments
 (0)