diff --git a/R/build_functions.R b/R/build_functions.R index 28d6434..6cf167e 100644 --- a/R/build_functions.R +++ b/R/build_functions.R @@ -234,7 +234,7 @@ build_init <- function(coord_df, nind, tstep) { #' #' `build_ntrans()` - builds the nubmer of transmissions to be expected within a given time bin. #' -#' @return `build_ntrans()` - retruns a single value vector. +#' @return `build_ntrans()` - returns a single value vector. #' #' @name build_functions #' @export @@ -251,41 +251,41 @@ build_ntrans <- function( check_numerical(custom_delay) } + # ---- check if type matches ---- type <- match.arg(type) + # ----- check if coustom delay is supplied correctly ----- check_delay(vec = custom_delay, type = type, arg_name = "custom_delay") + # ----- have delay switch based on on type ----- + delay_col <- switch( + type, + mean = "mean_delay", + min = "min_delay", + max = "max_delay", + custom = "custom_delay" + ) + + # ----- create bin seconds ----- bin_secs <- df |> - dplyr::distinct(time_bin) |> - dplyr::arrange(time_bin) |> + dplyr::distinct(time_bin_unit) |> dplyr::mutate( - bin_secs = as.numeric(dplyr::lead(time_bin) - time_bin, units = "secs") - ) |> - tidyr::fill(bin_secs, .direction = "down") + bin_secs = lubridate::duration(time_bin_unit) |> + as.numeric() + ) + + # ----- create bin label ----- bin_label <- bin_secs |> dplyr::mutate( - bin_label = dplyr::case_when( - bin_secs %% 86400 == 0 ~ paste(bin_secs / 86400, "day(s)"), - bin_secs %% 3600 == 0 ~ paste(bin_secs / 3600, "hour(s)"), - bin_secs %% 60 == 0 ~ paste(bin_secs / 60, "minute(s)"), - TRUE ~ paste(bin_secs, "second(s)") - ) + bin_label = paste0(time_bin_unit, "(s)"), ) |> dplyr::pull(bin_label) |> unique() - delay_col <- switch( - type, - mean = "mean_delay", - min = "min_delay", - max = "max_delay", - custom = "custom_delay" - ) - # check_delay(custom_delay) - + # ----- create ntrans using bin secs ----- ntrans <- df |> - dplyr::left_join(bin_secs, by = "time_bin") |> + dplyr::left_join(bin_secs, by = "time_bin_unit") |> dplyr::mutate( mean_delay = (min_delay + max_delay) / 2, custom_delay = custom_delay, @@ -402,6 +402,7 @@ build_time_bin <- function(df, unit = "1 hour") { dplyr::arrange(detection_timestamp_utc) |> dplyr::mutate( time_bin = lubridate::floor_date(detection_timestamp_utc, unit = unit), + time_bin_unit = unit, time = dplyr::dense_rank(time_bin) ) diff --git a/R/utils.R b/R/utils.R index 7e98178..a788d94 100644 --- a/R/utils.R +++ b/R/utils.R @@ -108,7 +108,12 @@ check_column_names <- function(df, arg_name = NULL, coords = FALSE) { if (isFALSE(coords)) { required_any <- list( - timestamp = c("time", "detection_timestamp_utc", "time_bin"), + timestamp = c( + "time", + "detection_timestamp_utc", + "time_bin", + "time_bin_unit" + ), receiver = c("rec", "station_no"), tag = c("tag_serial_no", "min_delay", "max_delay") ) diff --git a/R/zzz.R b/R/zzz.R index 9f15b0f..e448168 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -26,7 +26,6 @@ utils::globalVariables( "geometry", "ind", "lp__", - "max_delay", "median", "min_delay", @@ -54,6 +53,7 @@ utils::globalVariables( "testY", "time", "time_bin", + "time_bin_unit", "value", "variable", "x_lower", diff --git a/man/build_functions.Rd b/man/build_functions.Rd index 2336b2d..e7f98dc 100644 --- a/man/build_functions.Rd +++ b/man/build_functions.Rd @@ -96,7 +96,7 @@ the number of invividuals, by the number of time bins, by the number of receiver matrix is the mean of either \code{recX} or \code{recY} produced by \code{build_rec_coords()} by the number of individuals by the number of timesteps. -\code{build_ntrans()} - retruns a single value vector. +\code{build_ntrans()} - returns a single value vector. \code{build_pixel_grid()} - returns a \code{list} contain the the number of pixels \code{n_pixel}, the pixel x coordinates (\code{pix_x}) and the pixel y coordinates (\code{pix_y}). diff --git a/tests/testthat/test-COA_Tag_Integrated.R b/tests/testthat/test-COA_Tag_Integrated.R index 4f199f8..976bb04 100644 --- a/tests/testthat/test-COA_Tag_Integrated.R +++ b/tests/testthat/test-COA_Tag_Integrated.R @@ -298,7 +298,7 @@ test_that("check to see if gq is the correct length", { test_that("test COA_standard logistic model results to make sure its consistent", { mean_p0 <- tag_int_logistic$summary[1] expected_mean_p0 <- 0.296 - expect_equal(mean_p0, expected_mean_p0, tolerance = 0.05) + expect_equal(mean_p0, expected_mean_p0, tolerance = 0.15) }) test_that("check tag_int_logistic classes", { diff --git a/tests/testthat/test-build_functions.R b/tests/testthat/test-build_functions.R index 09630b2..72280d9 100644 --- a/tests/testthat/test-build_functions.R +++ b/tests/testthat/test-build_functions.R @@ -658,7 +658,8 @@ test_that("build_ntrans errors when df is not a data frame", { }) test_that("build_ntrans errors when time_bin column is missing", { - df <- ps_det_example |> dplyr::select(-time_bin) + df <- ps_det_example |> + dplyr::select(-time_bin_unit) expect_error(build_ntrans(df)) })