#' @include Algorithm.SDM.R
#' @import methods
#' @importFrom raster raster stack extract predict reclassify layerStats calc
NULL
setGeneric("project", function(obj, Env, ...) {
return(standardGeneric("project"))
})
setMethod("project", "Algorithm.SDM", function(obj, Env, ...) {
model = get_model(obj, ...)
factors <- sapply(seq_len(length(Env@layers)), function(i)
if(Env[[i]]@data@isfactor) Env[[i]]@data@attributes[[1]]$ID)
factors[sapply(factors, is.null)] <- NULL
names(factors) <- unlist(sapply(seq_len(length(Env@layers)), function(i)
if(Env[[i]]@data@isfactor) names(Env[[i]])))
proj = suppressWarnings(raster::predict(Env, model, factors = factors))
# proj = suppressWarnings(raster::predict(Env, model, fun = function(model,
# x) {
# x = as.data.frame(x)
# for (i in seq_len(length(Env@layers))) {
# if (Env[[i]]@data@isfactor) {
# x[, i] = as.factor(x[, i])
# x[, i] = droplevels(x[, i])
# levels(x[, i]) = Env[[i]]@data@attributes[[1]]$ID
# }
# }
# return(predict(model, x))
# }))
# Rescaling projection
proj = reclassify(proj, c(-Inf, 0, 0))
if(all(obj@data$Presence %in% c(0,1))) # MEMs should not be rescaled
if(proj@data@max > 0) proj = proj / proj@data@max
names(proj) = "Projection"
obj@projection = proj
if(all(obj@data$Presence %in% c(0,1))) # MEMs can't produce binary
obj@binary <- reclassify(proj, c(-Inf,obj@evaluation$threshold,0,
obj@evaluation$threshold,Inf,1))
return(obj)
})
setMethod("project", "MAXENT.SDM", function(obj, Env, ...) {
model = get_model(obj, Env, ...)
proj = raster::predict(Env, model, fun = function(model, x) {
x = as.data.frame(x)
for (i in seq_len(length(Env@layers))) {
if (Env[[i]]@data@isfactor) {
x[, i] = as.factor(x[, i])
x[, i] = droplevels(x[, i])
levels(x[, i]) = Env[[i]]@data@attributes[[1]]$ID
}
}
return(predict(model, x))
})
# Rescaling projection
proj = reclassify(proj, c(-Inf, 0, 0))
if(!all(obj@data$Presence %in% c(0,1))) # MEMs should not be rescaled
if(proj@data@max > 0) proj = proj / proj@data@max
names(proj) = "Projection"
obj@projection = proj
if(all(obj@data$Presence %in% c(0,1))) # MEMs can't produce binary
obj@binary <- reclassify(proj, c(-Inf,obj@evaluation$threshold,0,
obj@evaluation$threshold,Inf,1))
return(obj)
})
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.