From a7d957ab71c3b0aa1cc06c94c38fede6f1e21199 Mon Sep 17 00:00:00 2001 From: sjevelazco Date: Tue, 25 Feb 2025 12:46:24 -0800 Subject: [PATCH] kmean fixed --- R/auxiliary_functions.R | 4 ++-- R/sample_pseudoabs.R | 6 +++--- man/sample_pseudoabs.Rd | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/R/auxiliary_functions.R b/R/auxiliary_functions.R index 26ed2a5e..500ea167 100644 --- a/R/auxiliary_functions.R +++ b/R/auxiliary_functions.R @@ -444,7 +444,7 @@ mah_dist <- function(x, y, cov) { #' @noRd #' kf <- function(df, n){ - suppressWarnings(km <- stats::kmeans(env_changed %>% dplyr::select(-c(cell:y)), + suppressWarnings(km <- stats::kmeans(df %>% dplyr::select(-c(cell:y)), centers = n)) result <- data.frame(cluster = km$cluster, cell = names(km$cluster)) %>% dplyr::as_tibble() %>% @@ -454,7 +454,7 @@ kf <- function(df, n){ dplyr::group_by() %>% dplyr::pull(cell) %>% as.numeric() - result <- env_changed %>% + result <- df %>% dplyr::filter(cell %in% result) %>% dplyr::select(x, y) %>% dplyr::as_tibble() diff --git a/R/sample_pseudoabs.R b/R/sample_pseudoabs.R index 585af0c4..441909d5 100644 --- a/R/sample_pseudoabs.R +++ b/R/sample_pseudoabs.R @@ -12,7 +12,7 @@ #' provide a vector for this argument. The methods implemented are: #' \itemize{ #' \item random: Random allocation of pseudo-absences throughout the area used for model fitting. Usage method='random'. -#' \item kmeans: Pseudo-absences are sampled randomly within environmental clusters defined by k-means +#' \item kmeans: Pseudo-absences are sampled randomly within environmental clusters defined by K-means #' cluster analysis (the number of clusters will equal than number of pseudo-absences defined in 'n'). #' For this method, it is necessary to provide a raster object with environmental #' variables. Usage method = c('kmeans', env = somevar). @@ -22,7 +22,7 @@ #' \item geo_env_const: Pseudo-absences are constrained environmentally (based on Bioclim model) and distributed geographically far from occurrences based on a geographical buffer. For this method, a raster with environmental variables stored as SpatRaster object should be provided. A value of the buffer width in m must be provided if raster (used in rlayer) has a longitude/latitude CRS, or in map units in other cases. Usage method=c('geo_env_const', width='50000', env = somevar). #' \item geo_env_km_const: Pseudo-absences are constrained using a three-level procedure; it is similar to #' the geo_env_const with an additional step which distributes the pseudo-absences in environmental space -#' using k-means cluster analysis. For this method, it is necessary to provide a raster object with +#' using K-means cluster analysis. For this method, it is necessary to provide a raster object with #' environmental variables and a value of the buffer width in m if raster (used in rlayer) has a #' longitude/latitude CRS, or map units in other cases. Usage method=c('geo_env_km_const', #' width='50000', env = somevar). @@ -105,7 +105,7 @@ #' points(single_spp[-1], col = "blue", cex = 0.7, pch = 19) #' points(ps1, col = "red", cex = 0.7, pch = 19) #' -#' # Pseudo-absences sampled with K-mean approach +#' # Pseudo-absences sampled with K-means approach #' set.seed(123) #' ps1 <- #' sample_pseudoabs( diff --git a/man/sample_pseudoabs.Rd b/man/sample_pseudoabs.Rd index 2599ed06..bcfaa1ff 100644 --- a/man/sample_pseudoabs.Rd +++ b/man/sample_pseudoabs.Rd @@ -30,7 +30,7 @@ sample_pseudoabs( provide a vector for this argument. The methods implemented are: \itemize{ \item random: Random allocation of pseudo-absences throughout the area used for model fitting. Usage method='random'. -\item kmeans: Pseudo-absences are sampled randomly within environmental clusters defined by k-means +\item kmeans: Pseudo-absences are sampled randomly within environmental clusters defined by K-means cluster analysis (the number of clusters will equal than number of pseudo-absences defined in 'n'). For this method, it is necessary to provide a raster object with environmental variables. Usage method = c('kmeans', env = somevar). @@ -40,7 +40,7 @@ throughout the area used for model fitting. Usage method='random'. \item geo_env_const: Pseudo-absences are constrained environmentally (based on Bioclim model) and distributed geographically far from occurrences based on a geographical buffer. For this method, a raster with environmental variables stored as SpatRaster object should be provided. A value of the buffer width in m must be provided if raster (used in rlayer) has a longitude/latitude CRS, or in map units in other cases. Usage method=c('geo_env_const', width='50000', env = somevar). \item geo_env_km_const: Pseudo-absences are constrained using a three-level procedure; it is similar to the geo_env_const with an additional step which distributes the pseudo-absences in environmental space -using k-means cluster analysis. For this method, it is necessary to provide a raster object with +using K-means cluster analysis. For this method, it is necessary to provide a raster object with environmental variables and a value of the buffer width in m if raster (used in rlayer) has a longitude/latitude CRS, or map units in other cases. Usage method=c('geo_env_km_const', width='50000', env = somevar). @@ -123,7 +123,7 @@ plot(regions, col = gray.colors(9)) points(single_spp[-1], col = "blue", cex = 0.7, pch = 19) points(ps1, col = "red", cex = 0.7, pch = 19) -# Pseudo-absences sampled with K-mean approach +# Pseudo-absences sampled with K-means approach set.seed(123) ps1 <- sample_pseudoabs(