Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions .Rproj.user/shared/notebooks/paths
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
C:/Subarashi/R/enmSdmX/vignettes/00_getting_started.Rmd="CFE35708"
C:/Subarashi/R/fasterRaster/README.md="1CAE1F75"
C:/Subarashi/R/fasterRaster/_pkgdown.yml="8F005A55"
C:/Subarashi/R/pkgdown_workflow.r="4D123D79"
C:/Ecology/R/enmSdmX/vignettes/00_getting_started.Rmd="0E8AD837"
C:/Ecology/R/enmSdmX/vignettes/07_selecting_predictors.Rmd="F3FCEA9A"
C:/Ecology/R/enmSdmX/vignettes/18_troubleshooting.Rmd="1BFB8FF5"
7 changes: 3 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: enmSdmX
Type: Package
Title: Species Distribution Modeling and Ecological Niche Modeling
Version: 1.2.11
Date: 2025-01-22
Version: 1.2.13
Date: 2025-04-10
Authors@R:
c(
person(
Expand Down Expand Up @@ -57,14 +57,13 @@ Imports:
terra,
utils
Suggests:
brglm,
brglm2,
cluster,
gecko,
geodata,
knitr,
rgbif,
rmarkdown
VignetteBuilder: knitr
LazyData: true
LazyLoad: yes
URL: https://github.com/adamlilith/enmSdmX
Expand Down
10 changes: 10 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# enmSdmX 1.2.XX 2025-XX-XX
- Added WKT string "Equal Earth Greenwich" to data object `crss` (available through `getCRS()`).
- Minor updates to help.

# enmSdmX 1.2.13 2025-04-10
- Fixed bug in `responseCurves()` that disallowed plotting with >2 predictors (thank you, JC!).

# enmSdmX 1.2.12 2025-03-13
- Fixed bug that sometimes appeared in `coordImprecision()` because output included units :(.

# enmSdmX 1.2.11 2025-02-19
- Added `responseCurves()` for easy plotting of model response curves.
- Bug fix: `predictEnmSdm()` no longer fails for GLMs when `newdata` has more columns than necessary.
Expand Down
15 changes: 7 additions & 8 deletions R/coordImprecision.r
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#' @return Numeric values (by default in units of meters).
#'
#' @details
#' For coordinates originally reported in decimal notation, coordinate imprecision is \emph{half} the distance between the two opposing corners on a bounding box whose size is based on the number of significant digits in the coordinates. This box is defined by 1) finding the maximum number of significant digits after the decimal in the longitude/latitude pair; 2) adding/subtracting 5 to the decimal place that falls just after this; and 3) calculating the distance between these points then dividing by 2. For example, if longitude is 82.37 and latitude 45.8 then the number of significant digits after the decimal place is 2 and 1, respectively so 2 is used on the assumption that latitude is measured to the nearest 100th degree. The precision is then the distance between the point pairs (82.37 - 0.05 = 82.365, 45.8 - 0.05 = 45.795) and (82.37 + 0.05 = 82.375, 45.8 + 0.05 = 45.805). \cr \cr
#' For coordinates originally reported in decimal notation, coordinate imprecision is \emph{half} the distance between the two opposing corners on a bounding box whose size is based on the number of significant digits in the coordinates. This box is defined by 1) finding the maximum number of significant digits after the decimal in the longitude/latitude pair; 2) adding/subtracting 5 to the decimal place that falls just after this; and 3) calculating the distance between these points then dividing by 2. For example, if longitude is 82.37 and latitude 45.8 then the number of significant digits after the decimal place is 2 and 1, respectively so 2 is used on the assumption that latitude is measured to the nearest 100th of a degree. The precision is then the distance between the point pairs (82.37 - 0.005 = 82.365, 45.8 - 0.005 = 45.795) and (82.37 + 0.005 = 82.375, 45.8 + 0.005 = 45.805). \cr \cr
#'
#' For coordinates originally reported in degree-minus-second (DMS) format, the bounding box is defined by adding/subtracting 0.5 units (degrees, minutes, or seconds, depending on the smallest non-zero unit reported) from the coordinate. For example, if longitude is 90deg 00min 00sec and latitude is 37deg 37min 37sec, then the bounding box will be defined by adding/subtracting 0.5 arcsec to the coordinates.
#'
Expand Down Expand Up @@ -66,13 +66,11 @@
#' # is to use maximum:
#' pmax(decImp, dmsImp)
#'
#' if (FALSE) {
#' # known error when longitude is negative and latitude is -90
#' long <- -45
#' lat <- -90
#' ll <- cbind(long, lat)
#' coordImprecision(ll)
#' }
#' # when longitude is negative and latitude is -90
#' long <- -45
#' lat <- -90
#' ll <- cbind(long, lat)
#' coordImprecision(ll)
#'
#' @export
coordImprecision <- function(
Expand Down Expand Up @@ -275,6 +273,7 @@ coordImprecision <- function(
minusMinus <- sf::st_as_sf(as.data.frame(minusMinus), coords=1:2, crs=crs)

out <- 0.5 * sf::st_distance(plusPlus, minusMinus, by_element = TRUE)
out <- as.numeric(out) # strips units from output but obviates error: no applicable method for 'units' applied to an object of class "c('matrix', 'array', 'double', 'numeric')" on some CRAN checks
out

}
6 changes: 3 additions & 3 deletions R/predictEnmSdm.r
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#'
#' @param newdata Data frame or matrix, or \code{SpatRaster} with data to which to predict.
#'
#' @param maxentFun This argument is only used if the \code{model} object is a MaxEnt model; otherwise, it is ignored. It takes a value of either \code{'terra'}, in which case a MaxEnt model is predicted using the default \code{predict} function from the \pkg{terra} package, or \code{'enmSdmX'} in which case the function \code{\link[enmSdmX]{predictMaxEnt}} function from the \pkg{enmSdmX} package (this package) is used.
#' @param maxentFun This argument is only used if the \code{model} object is a MaxEnt model; otherwise, it is ignored. It takes a value of either \code{'terra'}, in which case a MaxEnt model is predicted using the default \code{predict} function from the \pkg{terra} package, or \code{'enmSdmX'} in which case the function \code{\link[enmSdmX]{predictMaxEnt}} function from the \pkg{enmSdmX} package (this package) is used. If the former is used, then predictions should be clamped, unless you use the \code{...} to supply arguments to \code{terra::predict()} and thus modify how MaxEnt is predicted. If you use \code{'enmSdmX'}, the predictions are not clamped.
#'
#' @param scale Logical. If the model is a GLM trained with \code{\link{trainGLM}} or \code{\link{trainNS}}, you can use the \code{scale} argument in that function to center and scale the predictors. In the \code{predictEnmSdm} function, you can set \code{scale} to \code{TRUE} to scale the rasters or data frame to which you are training using the centers (means) and scales (standard deviations) used in the mode. Otherwise, it is up to you to ensure variables are properly centered and scaled. This argument only has effect if the model is a GLM trained using \code{\link{trainGLM}} or \code{\link{trainNS}}.
#'
Expand Down Expand Up @@ -218,9 +218,9 @@ predictEnmSdm <- function(
# Maxent
} else if (inherits(model, c('MaxEnt', 'MaxEnt_model'))) {

out <- if (maxentFun == 'terra') {
out <- if (tolower(maxentFun) == 'terra') {
terra::predict(model, newdata, ...)
} else if (maxentFun == 'enmSdmX') {
} else if (tolower(maxentFun) == 'enmsdmx') {
predictMaxEnt(model, newdata, ...)
}

Expand Down
6 changes: 4 additions & 2 deletions R/responseCurves.r
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ responseCurves <- function(
constants <- apply(ref[ , vars, drop = FALSE], 2, constantFx, na.rm = TRUE)

# models
if (!is.list(models)) models <- list(models)
if (!inherits(models, 'list')) models <- list(models)

# create data frame with increasing values for each variable
minsEnv <- sapply(env[ , vars], min, na.rm = TRUE)
Expand Down Expand Up @@ -89,7 +89,9 @@ responseCurves <- function(
# create data frame with focal variable varying but all others constant
constantGrad <- gradients
varsSansFocal <- vars[vars != variable]
constantGrad[ , varsSansFocal] <- constants[[varsSansFocal]]
for (varSansFocal in varsSansFocal) {
constantGrad[ , varSansFocal] <- constants[[varSansFocal]]
}

# predict for each model
df <- data.frame()
Expand Down
Binary file modified data/crss.rda
Binary file not shown.
14 changes: 6 additions & 8 deletions man/coordImprecision.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 0 additions & 59 deletions man/examples/trainXYZ_examples.r
Original file line number Diff line number Diff line change
Expand Up @@ -160,65 +160,6 @@ names(maps) <- c('MaxEnt', 'MaxNet', 'GLM', 'GAM', 'NSs', 'BRTs', 'RFs')
fun <- function() plot(occs, col='black', pch=3, add=TRUE)
plot(maps, fun = fun, nc = 4)

### compare model responses to BIO12 (mean annual precipitation)
################################################################

# make a data frame holding all other variables at mean across occurrences,
# varying only BIO12
occEnvMeans <- colMeans(occEnv, na.rm=TRUE)
occEnvMeans <- rbind(occEnvMeans)
occEnvMeans <- as.data.frame(occEnvMeans)
climFrame <- occEnvMeans[rep(1, 100), ]
rownames(climFrame) <- NULL

minBio12 <- min(env$bio12)
maxBio12 <- max(env$bio12)
climFrame$bio12 <- seq(minBio12, maxBio12, length.out=100)

predMx <- predictEnmSdm(mx, climFrame)
predMn <- predictEnmSdm(mn, climFrame)
predGl <- predictEnmSdm(gl, climFrame)
predGa <- predictEnmSdm(ga, climFrame)
predNat <- predictEnmSdm(ns, climFrame)
predBrt <- predictEnmSdm(brt, climFrame)
predRf <- predictEnmSdm(rf, climFrame)


plot(climFrame$bio12, predMx,
xlab='BIO12', ylab='Prediction', type='l', ylim=c(0, 1))

lines(climFrame$bio12, predMn, lty='solid', col='red')
lines(climFrame$bio12, predGl, lty='dotted', col='blue')
lines(climFrame$bio12, predGa, lty='dashed', col='green')
lines(climFrame$bio12, predNat, lty=4, col='purple')
lines(climFrame$bio12, predBrt, lty=5, col='orange')
lines(climFrame$bio12, predRf, lty=6, col='cyan')

legend(
'topleft',
inset = 0.01,
legend = c(
'MaxEnt',
'MaxNet',
'GLM',
'GAM',
'NS',
'BRT',
'RF'
),
lty = c(1, 1:6),
col = c(
'black',
'red',
'blue',
'green',
'purple',
'orange',
'cyan'
),
bg = 'white'
)

### compare response curves ###
###############################

Expand Down
Binary file removed man/figures/sdm_enm.png
Binary file not shown.
59 changes: 0 additions & 59 deletions man/predictEnmSdm.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 0 additions & 59 deletions man/predictMaxEnt.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading