Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions R/build_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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)
)

Expand Down
7 changes: 6 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
Expand Down
2 changes: 1 addition & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ utils::globalVariables(
"geometry",
"ind",
"lp__",

"max_delay",
"median",
"min_delay",
Expand Down Expand Up @@ -54,6 +53,7 @@ utils::globalVariables(
"testY",
"time",
"time_bin",
"time_bin_unit",
"value",
"variable",
"x_lower",
Expand Down
2 changes: 1 addition & 1 deletion man/build_functions.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/testthat/test-COA_Tag_Integrated.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/test-build_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})

Expand Down
Loading