|
| 1 | +#' Distribution interval derived (DID) cutline method |
| 2 | +#' |
| 3 | +#' @description Implementation of the method described by Richar & Foy (2022) (DOI: |
| 4 | +#' 10.1139/facets-2021-0061). |
| 5 | +#' |
| 6 | +#' @param dat data frame or matrix containing the data |
| 7 | +#' @param xvar Name of column (or integer or double vector) containing |
| 8 | +#' measurements for the x-axis variable (e.g., carapace width). |
| 9 | +#' @param yvar Name of column (or integer or double vector) containing |
| 10 | +#' measurements for the y-axis variable (e.g., claw height). |
| 11 | +#' @param log Boolean; should both variables be log-transformed before |
| 12 | +#' performing the regression? Defaults to FALSE. |
| 13 | +#' @param upper Integer or double; the upper bound for possible SM50 values. |
| 14 | +#' Must be on the same scale as the data. Defaults to the 80th percentile of |
| 15 | +#' the x-variable. |
| 16 | +#' @param lower Integer or double; the lower bound for possible SM50 values. |
| 17 | +#' Must be on the same scale of the data. Defaults to the 20th percentile of |
| 18 | +#' the x-variable. |
| 19 | +#' @param int_num Integer; how many intervals between the lower and upper bound |
| 20 | +#' should be used? Defaults to 25. With fewer intervals, each interval will |
| 21 | +#' contain more points, increasing the accuracy of the estimated density |
| 22 | +#' minimum for a given interval. However, the linear regression of the minima |
| 23 | +#' distributions (the divisions between immature and mature individuals within |
| 24 | +#' an interval) against the midpoints of those intervals may be more reliable |
| 25 | +#' with more intervals. |
| 26 | +#' @param plot Boolean; should a plot of the data with the calculated minima and |
| 27 | +#' discriminating line be displayed? |
| 28 | +#' @param adjust the bandwidth used for the kernel density estimate is actually |
| 29 | +#' adjust*bw. This makes it easy to specify values like ‘half the default’ |
| 30 | +#' bandwidth. |
| 31 | +#' |
| 32 | +#' @returns Something |
| 33 | +#' |
| 34 | +#' @examples |
| 35 | +#' set.seed(12) |
| 36 | +#' fc <- fake_crustaceans(n = 1000, L50 = 100, allo_params = c(1, 0.2, 1.1, 0.2)) |
| 37 | +#' density_int(dat = fc, xvar = "x", yvar = "y", upper = 120) |
| 38 | +#' density_int(dat = fc, xvar = "x", yvar = "y", upper = log(120), log = TRUE) |
| 39 | +density_int <- density_int <- function(dat, |
| 40 | + xvar, |
| 41 | + yvar, |
| 42 | + lower = NULL, |
| 43 | + upper = NULL, |
| 44 | + int_num = 25, |
| 45 | + log = FALSE, |
| 46 | + plot = FALSE, |
| 47 | + adjust = 1) { |
| 48 | + if (isTRUE(log)) { |
| 49 | + dat$xvar <- log(dat[[xvar]]) |
| 50 | + dat$yvar <- log(dat[[yvar]]) |
| 51 | + } |
| 52 | + else { |
| 53 | + dat$xvar <- dat[[xvar]] |
| 54 | + dat$yvar <- dat[[yvar]] |
| 55 | + } |
| 56 | + |
| 57 | + if (is.null(lower)) { |
| 58 | + lower <- stats::quantile(dat$xvar, 0.2, names = FALSE) |
| 59 | + } |
| 60 | + |
| 61 | + if (is.null(upper)) { |
| 62 | + upper <- stats::quantile(dat$xvar, 0.8, names = FALSE) |
| 63 | + } |
| 64 | + |
| 65 | + int_width <- (upper - lower) / int_num |
| 66 | + |
| 67 | + i <- 1 |
| 68 | + int_bottom <- lower |
| 69 | + df_ints <- data.frame( |
| 70 | + int_bottom = rep(NA, int_num), |
| 71 | + int_top = rep(NA, int_num), |
| 72 | + min = rep(NA, int_num), |
| 73 | + n_obs = rep(NA, int_num) |
| 74 | + ) |
| 75 | + |
| 76 | + ##### BEGIN LOOP |
| 77 | + while (i < int_num + 1) { |
| 78 | + int_top_temp <- int_bottom + int_width |
| 79 | + temp_df <- dat %>% filter(xvar >= int_bottom, xvar <= int_top_temp) |
| 80 | + n_obs <- nrow(temp_df) |
| 81 | + |
| 82 | + if (n_obs < 5) { |
| 83 | + abort( |
| 84 | + paste( |
| 85 | + "Each interval must contain at least 5 data points. The interval from", |
| 86 | + round(int_bottom, 3), |
| 87 | + "to", |
| 88 | + round(int_top_temp, 3), |
| 89 | + "only contains", |
| 90 | + n_obs, |
| 91 | + "points.", |
| 92 | + sep = " " |
| 93 | + ) |
| 94 | + ) |
| 95 | + } |
| 96 | + |
| 97 | + df_ints$int_bottom[i] <- int_bottom |
| 98 | + df_ints$int_top[i] <- int_top_temp |
| 99 | + df_ints$n_obs[i] <- n_obs |
| 100 | + |
| 101 | + # compute a kernel density estimate |
| 102 | + density_test <- stats::density(temp_df$yvar, adjust = adjust) |
| 103 | + |
| 104 | + # convert into a data frame |
| 105 | + density_test <- data.frame(x = density_test$x, density = density_test$y) |
| 106 | + |
| 107 | + span <- 5 |
| 108 | + |
| 109 | + # find the local maxima - should be two modes |
| 110 | + density_test$is_max <- splus2R::peaks(x = density_test$density, |
| 111 | + span = span, |
| 112 | + strict = TRUE) |
| 113 | + modes <- density_test %>% |
| 114 | + dplyr::filter(.data$is_max == TRUE) %>% |
| 115 | + dplyr::pull(x) |
| 116 | + |
| 117 | + |
| 118 | + while(length(modes) > 2) { |
| 119 | + span <- span + 2 |
| 120 | + |
| 121 | + density_test$is_max <- splus2R::peaks(x = density_test$density, |
| 122 | + span = span, |
| 123 | + strict = TRUE) |
| 124 | + modes <- density_test %>% |
| 125 | + dplyr::filter(.data$is_max == TRUE) %>% |
| 126 | + dplyr::pull(x) |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | + if(length(modes) < 2) { |
| 131 | + # int_num <- int_num - 1 |
| 132 | + # int_width <- (upper - lower) / int_num |
| 133 | + # i <- 1 |
| 134 | + # int_bottom <- lower |
| 135 | + # df_ints <- data.frame( |
| 136 | + # int_bottom = rep(NA, int_num), |
| 137 | + # int_top = rep(NA, int_num), |
| 138 | + # min = rep(NA, int_num), |
| 139 | + # n_obs = rep(NA, int_num) |
| 140 | + # ) |
| 141 | + abort( |
| 142 | + paste( |
| 143 | + "Each interval should contain two peaks in the density of points along the y-axis. The interval from", |
| 144 | + round(int_bottom, 3), |
| 145 | + "to", |
| 146 | + round(int_top_temp, 3), |
| 147 | + "only contains", |
| 148 | + length(modes), |
| 149 | + "peaks. Try decreasing the int_num argument or changing the adjust argument, which is a multiplier for the smoothing bandwidth.", |
| 150 | + sep = " " |
| 151 | + ) |
| 152 | + ) |
| 153 | + } |
| 154 | + # else { |
| 155 | + between_modes <- density_test %>% filter(x > modes[1], x < modes[2]) |
| 156 | + |
| 157 | + interval_min <- between_modes[which.min(between_modes$density), "x"] |
| 158 | + |
| 159 | + df_ints$min[i] <- interval_min |
| 160 | + |
| 161 | + int_bottom <- int_top_temp |
| 162 | + i <- i + 1 |
| 163 | + # } |
| 164 | + } # end loop |
| 165 | + |
| 166 | + df_ints$midpt <- (df_ints$int_bottom + df_ints$int_top) / 2 |
| 167 | + |
| 168 | + # optionally visualize the data with the discriminant line |
| 169 | + if (plot == TRUE) { |
| 170 | + if (log == TRUE) { |
| 171 | + xlab <- paste0("ln(", xvar, ")") |
| 172 | + ylab <- paste0("ln(", yvar, ")") |
| 173 | + } |
| 174 | + else { |
| 175 | + xlab <- xvar |
| 176 | + ylab <- yvar |
| 177 | + } |
| 178 | + |
| 179 | + lm_density <- stats::lm(min ~ midpt, data = df_ints) |
| 180 | + pred_line <- data.frame(x = dat$xvar, |
| 181 | + y = stats::predict(lm_density, data.frame(midpt = dat$xvar))) |
| 182 | + print( |
| 183 | + ggplot2::ggplot() + |
| 184 | + ggplot2::geom_point(data = dat, aes(x = xvar, y = yvar)) + |
| 185 | + ggplot2::geom_point( |
| 186 | + data = na.omit(df_ints), |
| 187 | + aes(x = midpt, y = min), |
| 188 | + color = "red" |
| 189 | + ) + |
| 190 | + ggplot2::geom_line(data = pred_line, aes(x, y)) + |
| 191 | + ggplot2::labs(x = xlab, y = ylab) + |
| 192 | + ggplot2::theme_light() |
| 193 | + ) |
| 194 | + } |
| 195 | + |
| 196 | + return(df_ints) |
| 197 | +} |
0 commit comments