diff --git a/DESCRIPTION b/DESCRIPTION index df0eff0..80a3dde 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: shar Title: Species-Habitat Associations -Version: 2.2.1 +Version: 2.3 Authors@R: c(person("Maximilian H.K.", "Hesselbarth", email = "mhk.hesselbarth@gmail.com", role = c("aut", "cre"), comment = c(ORCID = "0000-0003-1125-9918")), person("Marco", "Sciaini", email = "marco.sciaini@posteo.net", @@ -45,7 +45,7 @@ Suggests: rmarkdown, spatstat (>= 2.0-0), testthat (>= 3.0.0) -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.0 VignetteBuilder: knitr Encoding: UTF-8 LazyData: true diff --git a/NEWS.md b/NEWS.md index 1457305..d29af70 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,10 @@ +# shar 2.3 +* Improvements + * Option to return fitted parameters during `fit_point_process` + * Using `NA` instead of "NA" +* Various + * Iterations and stopping criterion are vectors instead of list during `reconstruct_algorithm` + # shar 2.2.1 * Improvements * Simplify roxygen2 docs diff --git a/R/fit_point_process.R b/R/fit_point_process.R index b05d72f..a75a131 100644 --- a/R/fit_point_process.R +++ b/R/fit_point_process.R @@ -6,6 +6,7 @@ #' @param n_random Integer with number of randomizations. #' @param process Character specifying which point process model to use. #' Either \code{"poisson"} or \code{"cluster"}. +#' @param return_para Logical if fitted parameters should be returned. #' @param return_input Logical if the original input data is returned. #' @param simplify Logical if only pattern will be returned if \code{n_random = 1} #' and \code{return_input = FALSE}. @@ -32,11 +33,8 @@ #' ecology. Chapman and Hall/CRC Press, Boca Raton. ISBN 978-1-4200-8254-8 #' #' @export -fit_point_process <- function(pattern, - n_random = 1, process = "poisson", - return_input = TRUE, - simplify = FALSE, - verbose = TRUE){ +fit_point_process <- function(pattern, n_random = 1, process = "poisson", return_para = FALSE, + return_input = TRUE, simplify = FALSE, verbose = TRUE){ # check if n_random is >= 1 if (!n_random >= 1) { @@ -45,8 +43,6 @@ fit_point_process <- function(pattern, } - iterations_list <- as.list(rep(NA, times = n_random)) - # unmark pattern if (spatstat.geom::is.marked(pattern)) { @@ -72,6 +68,16 @@ fit_point_process <- function(pattern, }) + # calc parameters + if (return_para) { + + number_points <- pattern$n + lambda <- spatstat.geom::intensity(pattern) + + param_vec <- c(number_points = number_points,lambda = lambda) + + } + } else if (process == "cluster") { # fit cluster process @@ -81,6 +87,18 @@ fit_point_process <- function(pattern, correction = "best"), method = "mincon", improve.type = "none") + # calc parameters + if (return_para) { + + number_parents <- fitted_process$clustpar[["kappa"]] * spatstat.geom::area(pattern$window) + number_points <- fitted_process$mu + cluster_area <- fitted_process$clustpar[["scale"]] ^ 2 * pi + + param_vec <- c(number_parents = number_parents, number_points = number_points, + cluster_area = cluster_area) + + } + result <- lapply(seq_len(n_random), function(x) { # simulate clustered pattern @@ -130,13 +148,20 @@ fit_point_process <- function(pattern, } + # set param to NA + if (!return_para) param_vec <- NA + # set names names(result) <- paste0("randomized_", seq_len(n_random)) # combine to one list - result <- list(randomized = result, observed = pattern, - method = "fit_point_process()", energy_df = "NA", - stop_criterion = "NA", iterations = iterations_list) + result <- list(randomized = result, observed = pattern, method = "fit_point_process()", + energy_df = NA, stop_criterion = NA, iterations = NA, param = param_vec) + + # add param + if (return_para) { + result$param <- param_vec + } # set class of result class(result) <- "rd_pat" @@ -145,7 +170,7 @@ fit_point_process <- function(pattern, if (!return_input) { # set observed to NA - result$observed <- "NA" + result$observed <- NA # check if output should be simplified if (simplify) { diff --git a/R/list_to_randomized.R b/R/list_to_randomized.R index a056577..b4f0c5a 100644 --- a/R/list_to_randomized.R +++ b/R/list_to_randomized.R @@ -64,20 +64,15 @@ list_to_randomized <- function(list, observed = NULL) { # return observed if present or NA if not if (is.null(observed)) { - observed <- "NA" + observed <- NA } # set names names(list) <- paste0("randomized_", seq_along(list)) - # create empty iterations list - iterations_list <- as.list(rep(NA, times = length(list))) - # combine to one list - result <- list(randomized = list, observed = observed, - method = "list_to_randomized()", energy_df = "NA", - stop_criterion = "NA", iterations = iterations_list) + result <- list(randomized = list, observed = observed, method = "list_to_randomized()") class(result) <- result_class diff --git a/R/plot_energy.R b/R/plot_energy.R index 7da0aec..4906ec9 100644 --- a/R/plot_energy.R +++ b/R/plot_energy.R @@ -39,7 +39,7 @@ plot_energy <- function(pattern, } - if (all(pattern$energy_df == "NA")) { + if (!is.list(pattern$energy_df)) { stop("There is no 'energy_df' slot. Please use pattern reconstruction for valid input data.", call. = FALSE) diff --git a/R/print.rd_mar.R b/R/print.rd_mar.R index dec49fc..5712c1b 100644 --- a/R/print.rd_mar.R +++ b/R/print.rd_mar.R @@ -33,7 +33,7 @@ print.rd_mar <- function(x, digits = 4, ...) { number_patterns_obs <- 0 - includes_observed <- "NA" + includes_observed <- NA # observed pattern is present } else { @@ -53,7 +53,7 @@ print.rd_mar <- function(x, digits = 4, ...) { number_patterns <- length(x$randomized) + number_patterns_obs # calculate mean iterations - mean_iterations <- round(mean(unlist(x$iterations)), digits = digits) + mean_iterations <- round(mean(x$iterations), digits = digits) # count stop criterions stop_criterion <- tryCatch(expr = table(do.call(c, x$stop_criterion), useNA = "ifany"), @@ -75,7 +75,7 @@ print.rd_mar <- function(x, digits = 4, ...) { } else { - mean_energy <- "NA" + mean_energy <- NA } diff --git a/R/print.rd_pat.R b/R/print.rd_pat.R index 1e7c17f..fc3f58c 100644 --- a/R/print.rd_pat.R +++ b/R/print.rd_pat.R @@ -36,7 +36,7 @@ print.rd_pat <- function(x, number_patterns_obs <- 0 - includes_observed <- "NA" + includes_observed <- NA # observed pattern is present } else { @@ -55,7 +55,7 @@ print.rd_pat <- function(x, number_patterns <- length(x$randomized) + number_patterns_obs # calculate mean iterations - mean_iterations <- round(mean(unlist(x$iterations)), digits = digits) + mean_iterations <- round(mean(x$iterations), digits = digits) # count stop criterions stop_criterion <- tryCatch(expr = table(do.call(c, x$stop_criterion), useNA = "ifany"), @@ -77,7 +77,7 @@ print.rd_pat <- function(x, } else { - mean_energy <- "NA" + mean_energy <- NA } diff --git a/R/print.rd_ras.R b/R/print.rd_ras.R index b32938e..42d0866 100644 --- a/R/print.rd_ras.R +++ b/R/print.rd_ras.R @@ -37,7 +37,7 @@ print.rd_ras <- function(x, ...) { number_raster_obs <- 0 - includes_observed <- "NA" + includes_observed <- NA # observed pattern is present } else { diff --git a/R/randomize_raster.R b/R/randomize_raster.R index 78ed912..11ef54c 100644 --- a/R/randomize_raster.R +++ b/R/randomize_raster.R @@ -193,7 +193,7 @@ randomize_raster <- function(raster, if (!return_input) { # set observed to NA - randomization$observed <- "NA" + randomization$observed <- NA # check if output should be simplified if (simplify) { diff --git a/R/reconstruct_algorithm.R b/R/reconstruct_algorithm.R index 269886e..310467e 100644 --- a/R/reconstruct_algorithm.R +++ b/R/reconstruct_algorithm.R @@ -24,19 +24,9 @@ #' @return list #' #' @keywords internal -reconstruct_algorithm <- function(pattern, - method, - n_random, - e_threshold, - max_runs, - no_change, - annealing, - weights, - r_length, - r_max, - stoyan, - verbose, - plot){ +reconstruct_algorithm <- function(pattern, method, n_random, e_threshold, max_runs, + no_change, annealing, weights, r_length, r_max, + stoyan, verbose, plot){ # check if n_random is >= 1 if (n_random < 1) { @@ -90,14 +80,14 @@ reconstruct_algorithm <- function(pattern, # create empty lists for results energy_list <- vector("list", length = n_random) - iterations_list <- vector("list", length = n_random) - stop_criterion_list <- as.list(rep("max_runs", times = n_random)) + iterations_vec <- vector(mode = "numeric", length = n_random) + stop_criterion_vec <- rep("max_runs", times = n_random) result_list <- vector("list", length = n_random) # set names names(energy_list) <- names_randomization - names(iterations_list) <- names_randomization - names(stop_criterion_list) <- names_randomization + names(iterations_vec) <- names_randomization + names(stop_criterion_vec) <- names_randomization names(result_list) <- names_randomization # calculate summary functions @@ -286,8 +276,8 @@ reconstruct_algorithm <- function(pattern, if (energy <= e_threshold || energy_counter > no_change) { # set stop criterion due to energy - stop_criterion_list[[i]] <- ifelse(test = energy <= e_threshold, - yes = "e_threshold", no = "no_change") + stop_criterion_vec[i] <- ifelse(test = energy <= e_threshold, + yes = "e_threshold", no = "no_change") break @@ -302,7 +292,7 @@ reconstruct_algorithm <- function(pattern, } # remove NAs if stopped due to energy - if (stop_criterion_list[[i]] %in% c("e_threshold", "no_change")) { + if (stop_criterion_vec[i] %in% c("e_threshold", "no_change")) { energy_df <- energy_df[1:j, ] @@ -310,7 +300,7 @@ reconstruct_algorithm <- function(pattern, # save results in lists energy_list[[i]] <- energy_df - iterations_list[[i]] <- j + iterations_vec[i] <- j result_list[[i]] <- simulated } @@ -321,8 +311,8 @@ reconstruct_algorithm <- function(pattern, # combine to one list reconstruction <- list(randomized = result_list, observed = pattern, method = method, energy_df = energy_list, - stop_criterion = stop_criterion_list, - iterations = iterations_list) + stop_criterion = stop_criterion_vec, + iterations = iterations_vec, param = NA) return(reconstruction) } diff --git a/R/reconstruct_pattern.R b/R/reconstruct_pattern.R index 07bc738..a0802e2 100644 --- a/R/reconstruct_pattern.R +++ b/R/reconstruct_pattern.R @@ -81,20 +81,11 @@ #' ecology. Chapman and Hall/CRC Press, Boca Raton. ISBN 978-1-4200-8254-8 #' #' @export -reconstruct_pattern <- function(pattern, method = "homo", - n_random = 1, - e_threshold = 0.01, - max_runs, - no_change = Inf, - annealing = 0.01, - weights = c(1, 1), - r_length = 255, - r_max = NULL, - stoyan = 0.15, - return_input = TRUE, - simplify = FALSE, - verbose = TRUE, - plot = FALSE) { +reconstruct_pattern <- function(pattern, method = "homo", n_random = 1, e_threshold = 0.01, + max_runs = 10000, no_change = Inf, annealing = 0.01, + weights = c(1, 1), r_length = 255, r_max = NULL, + stoyan = 0.15, return_input = TRUE, simplify = FALSE, + verbose = TRUE, plot = FALSE) { # check if correct method is selected if (!method %in% c("homo", "hetero", "cluster")) stop("Method must be one of the following: 'homo', 'hetero', or 'cluster'.", @@ -113,7 +104,7 @@ reconstruct_pattern <- function(pattern, method = "homo", if (!return_input) { # set observed to NA - reconstruction$observed <- "NA" + reconstruction$observed <- NA # check if output should be simplified if (simplify) { diff --git a/R/reconstruct_pattern_marks.R b/R/reconstruct_pattern_marks.R index badaa4c..bc7e144 100644 --- a/R/reconstruct_pattern_marks.R +++ b/R/reconstruct_pattern_marks.R @@ -112,14 +112,14 @@ reconstruct_pattern_marks <- function(pattern, # create empty lists for results energy_list <- vector("list", length = n_random) - iterations_list <- vector("list", length = n_random) - stop_criterion_list <- as.list(rep("max_runs", times = n_random)) + iterations_vec <- vector("numeric", length = n_random) + stop_criterion_vec <- rep("max_runs", times = n_random) result_list <- vector("list", length = n_random) # set names names(energy_list) <- names_randomization - names(iterations_list) <- names_randomization - names(stop_criterion_list) <- names_randomization + names(iterations_vec) <- names_randomization + names(stop_criterion_vec) <- names_randomization names(result_list) <- names_randomization # calculate summary functions @@ -238,8 +238,8 @@ reconstruct_pattern_marks <- function(pattern, if (energy <= e_threshold || energy_counter > no_change) { # set stop criterion due to energy - stop_criterion_list[[i]] <- ifelse(test = energy <= e_threshold, - yes = "e_threshold", no = "no_change") + stop_criterion_vec[i] <- ifelse(test = energy <= e_threshold, + yes = "e_threshold", no = "no_change") break @@ -253,7 +253,7 @@ reconstruct_pattern_marks <- function(pattern, } # remove NAs if stopped due to energy - if (stop_criterion_list[[i]] %in% c("e_threshold", "no_change")) { + if (stop_criterion_vec[i] %in% c("e_threshold", "no_change")) { energy_df <- energy_df[1:j, ] @@ -261,7 +261,7 @@ reconstruct_pattern_marks <- function(pattern, # save results in lists energy_list[[i]] <- energy_df - iterations_list[[i]] <- j + iterations_vec[i] <- j result_list[[i]] <- simulated } @@ -275,9 +275,8 @@ reconstruct_pattern_marks <- function(pattern, # combine to one list reconstruction <- list(randomized = result_list, observed = marked_pattern, - method = "marks", - energy_df = energy_list, stop_criterion = stop_criterion_list, - iterations = iterations_list) + method = "marks", energy_df = energy_list, + stop_criterion = stop_criterion_vec, iterations = iterations_vec) # set class of returning object class(reconstruction) <- "rd_mar" @@ -286,7 +285,7 @@ reconstruct_pattern_marks <- function(pattern, if (!return_input) { # set observed to NA - reconstruction$observed <- "NA" + reconstruction$observed <- NA # check if output should be simplified if (simplify) { diff --git a/R/translate_raster.R b/R/translate_raster.R index 04e1f93..4bc7b19 100644 --- a/R/translate_raster.R +++ b/R/translate_raster.R @@ -159,7 +159,7 @@ translate_raster <- function(raster, steps_x = NULL, steps_y = NULL, if (!return_input) { # set observed to NA - randomization$observed <- "NA" + randomization$observed <- NA # check if output should be simplified if (simplify) { diff --git a/cran-comments.md b/cran-comments.md index f485724..1097709 100644 --- a/cran-comments.md +++ b/cran-comments.md @@ -1,5 +1,8 @@ For details changes, please see NEWS.md +# shar 2.3 +Adding new arguments + # shar 2.2.1 Minor improvements diff --git a/man/fit_point_process.Rd b/man/fit_point_process.Rd index a58e893..c96a435 100644 --- a/man/fit_point_process.Rd +++ b/man/fit_point_process.Rd @@ -8,6 +8,7 @@ fit_point_process( pattern, n_random = 1, process = "poisson", + return_para = FALSE, return_input = TRUE, simplify = FALSE, verbose = TRUE @@ -21,6 +22,8 @@ fit_point_process( \item{process}{Character specifying which point process model to use. Either \code{"poisson"} or \code{"cluster"}.} +\item{return_para}{Logical if fitted parameters should be returned.} + \item{return_input}{Logical if the original input data is returned.} \item{simplify}{Logical if only pattern will be returned if \code{n_random = 1} diff --git a/man/reconstruct_pattern.Rd b/man/reconstruct_pattern.Rd index 8237f86..0882402 100644 --- a/man/reconstruct_pattern.Rd +++ b/man/reconstruct_pattern.Rd @@ -9,7 +9,7 @@ reconstruct_pattern( method = "homo", n_random = 1, e_threshold = 0.01, - max_runs, + max_runs = 10000, no_change = Inf, annealing = 0.01, weights = c(1, 1), diff --git a/man/shar.Rd b/man/shar.Rd index 668db0d..a04117f 100644 --- a/man/shar.Rd +++ b/man/shar.Rd @@ -2,8 +2,8 @@ % Please edit documentation in R/shar-package.R \docType{package} \name{shar} -\alias{shar} \alias{shar-package} +\alias{shar} \title{Package description} \description{ Analyse species-habitat associations in R. Therefore, information about the