|
7 | 7 | #' @param land raster; a mask to be applied to the output usually landmasses |
8 | 8 | #' @param mask raster; an additional mask to apply to the output, if needed |
9 | 9 | #' @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 |
12 | 12 | #' |
13 | 13 | #' @return a raster map with the desired prediction |
14 | 14 | #' @export |
15 | 15 | #' @importFrom terra values |
16 | 16 | #' @importFrom terra setValues |
17 | 17 | #' |
18 | 18 | #' @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 | + } |
30 | 31 |
|
31 | 32 | # Identify the type and make the main prediction |
32 | | - type=tolower(type) |
| 33 | + type <- tolower(type) |
33 | 34 | # 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") { |
35 | 36 | # 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) |
37 | 38 |
|
38 | 39 | # 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) |
41 | 44 |
|
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. |
48 | 51 | } |
49 | 52 | # 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) |
52 | 55 |
|
53 | 56 | # 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) |
56 | 61 |
|
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) |
62 | 67 | } |
63 | 68 | # 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) |
66 | 71 | } |
67 | 72 |
|
68 | 73 | # For some reason, the crs info isn't always carrying over |
69 | 74 | 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 | + } |
71 | 78 |
|
72 | 79 | # 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 | + ) |
76 | 85 | } |
77 | 86 | # 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 | + ) |
81 | 92 | } |
82 | 93 | return(habitat.prediction) |
83 | 94 | } |
84 | | - |
|
0 commit comments