Skip to contents

Detect points of sudden increase in Rate-of-Change values from the output of estimate_roc().

Usage

detect_peak_points(
  data_source,
  sel_method = c("trend_linear", "trend_non_linear", "threshold", "GAM_deriv", "SNI"),
  sd_threshold = 2,
  silent = FALSE
)

Arguments

data_source

tibble. Output of estimate_roc().

sel_method

character. Method to use for peak-point detection.

  • "threshold" - Each point is compared to the median of all RoC values (ROC). A point is significant if its ROC_dw, the 2.5 % quantile of RoC across randomisation runs (the lower bound of its 95 % interval), lies above that median, that is, if its whole 95 % interval lies above the median.

  • "trend_linear" - A linear model is fitted between RoC values and their ages. A peak is significant if it is sd_threshold SD above the fitted value.

  • "trend_non_linear" - A conservative GAM (RoC ~ s(age, k = 3)) is fitted. A peak is significant if it is sd_threshold SD above the fitted value.

  • "GAM_deriv" - A smooth GAM (RoC ~ s(age)) is fitted and the first derivative evaluated using the gratia package (Simpson, 2018). A peak is significant if the confidence interval of the first derivative excludes zero.

  • "SNI" - Signal-to-noise index adapted from Kelly et al. (2011). A peak is significant if SNI > 3.

sd_threshold

numeric. Number of standard deviations above the trend required for a point to be classified as a peak (default = 2). Used by "trend_linear" and "trend_non_linear".

silent

logical. If TRUE, do not warn about RoC values of zero or NA (see Details). Errors still occur (default = FALSE).

Value

The input tibble with all its columns and an additional logical column Peak that is TRUE for samples identified as significant peak points and NA for samples whose RoC is NA. With "SNI", Peak is also NA where the SNI cannot be computed and the RoC lies above the trend. The output can therefore be passed to check_rand().

Details

The trends of "trend_linear", "trend_non_linear", and "SNI", and the GAM of "GAM_deriv", assume a Gamma distribution of RoC (Tweedie with power 2) with a log link, which is defined only for values above zero. RoC is zero between two Working Units with identical composition, which can happen after smoothing. For fitting the trend only, such values are replaced by half of the smallest RoC value above zero, with a warning unless silent = TRUE. The returned ROC column and the residuals of "trend_linear", "trend_non_linear", and "SNI" use the original values.

Rows whose RoC is NA are skipped, with a warning unless silent = TRUE: all five methods detect peaks from the remaining rows only (the median of "threshold", the fitted trends, and the window of "SNI"), and the skipped rows get Peak = NA. The function stops with an error when no RoC value other than NA is left.

References

Kelly, R.F., Higuera, P.E., Barrett, C.M., Feng Sheng, H., 2011. A signal-to-noise index to quantify the potential for peak detection in sediment-charcoal records. Quat. Res. 75, 11-17.

Simpson, G.L., 2018. Modelling palaeoecological time series using generalised additive models. Front. Ecol. Evol. 6, 1-21.

Wood, S.N., 2011. Fast stable restricted maximum likelihood and marginal likelihood estimation of semiparametric generalized linear models. J. R. Stat. Soc. Ser. B Stat. Methodol. 73, 3-36.

Examples

# \donttest{
data("example_data", package = "RRatepol")

# RoC of the first sequence with age uncertainty. 100 runs are only a
#    pilot; see `check_rand()` for the number of runs of a real analysis.
data_roc <-
  estimate_roc(
    data_source_community = purrr::chuck(example_data, "pollen_data", 1),
    data_source_age = purrr::chuck(example_data, "sample_age", 1),
    age_uncertainty = purrr::chuck(example_data, "age_uncertainty", 1),
    smooth_method = "shep",
    dissimilarity_coefficient = "chisq",
    rand = 100,
    seed = 900723,
    silent = TRUE
  )

# Peaks 2 standard deviations above a non-linear (GAM) trend. The result
#    is the input with an added logical column `Peak`.
data_peaks <-
  detect_peak_points(
    data_source = data_roc,
    sel_method = "trend_non_linear",
    sd_threshold = 2
  )

data_peaks
#> # A tibble: 62 × 10
#>    Working_Unit    Age   ROC ROC_up ROC_dw n_runs ROC_mc_error ROC_up_mc_error
#>    <chr>         <dbl> <dbl>  <dbl>  <dbl>  <int>        <dbl>           <dbl>
#>  1 392671-392672  -20  1.35    6.96  0.288    100       0.0978           1.22 
#>  2 392672-392673  121  0.720   3.65  0.256    100       0.112            0.284
#>  3 392673-392674  244. 0.557   3.21  0.165    100       0.0710           0.478
#>  4 392674-392675  374  0.656   3.31  0.200    100       0.0549           1.80 
#>  5 392675-392676  526  0.747   6.23  0.222    100       0.0552           1.85 
#>  6 392676-392677  628. 0.955   5.58  0.304    100       0.0958           2.26 
#>  7 392677-392678  760. 1.18    6.14  0.244    100       0.159            2.27 
#>  8 392678-392679  870  0.896   4.87  0.277    100       0.0932           1.80 
#>  9 392679-392680  971  0.678   4.12  0.211    100       0.0616           2.19 
#> 10 392680-392681 1121  1.03    4.64  0.270    100       0.124            1.12 
#> # ℹ 52 more rows
#> # ℹ 2 more variables: ROC_dw_mc_error <dbl>, Peak <lgl>

plot_roc(
  data_source = data_peaks,
  peaks = TRUE,
  trend = "trend_non_linear"
)


# A less strict criterion: 1.5 standard deviations above a linear trend
data_peaks_linear <-
  detect_peak_points(
    data_source = data_roc,
    sel_method = "trend_linear",
    sd_threshold = 1.5
  )

# All methods, and the number of peak points each of them finds
list_peaks <-
  c("threshold", "trend_linear", "trend_non_linear", "GAM_deriv", "SNI") %>%
  purrr::set_names() %>%
  purrr::map(
    .f = ~ detect_peak_points(
      data_source = data_roc,
      sel_method = .x
    )
  )

purrr::map_int(
  .x = list_peaks,
  .f = ~ sum(
    dplyr::pull(.data = .x, var = "Peak"),
    na.rm = TRUE
  )
)
#>        threshold     trend_linear trend_non_linear        GAM_deriv 
#>                3                3                3               16 
#>              SNI 
#>                2 
# }