Evaluate Event Independence
mm_independence.Rd
This function calculates the difference between times and evaluates whether events are independent based on a given threshold. It is useful for checking if certain events in a dataset are independent of each other based on time intervals.
Usage
mm_independence(
data = NULL,
species_column = NULL,
datetime,
format,
threshold = 30 * 60,
only = FALSE
)
Arguments
- data
A
data.frame
,tbl_df
, ortbl
containing the event data. This should include a column with datetime values. IfNULL
, the function will use thedeltatime
argument instead of thedata
argument.- species_column
An optional column name specifying the species grouping. If provided, independence will be assessed separately within each species group.
- datetime
A
character
string specifying the name of the column indata
that contains the datetime values. This argument is required ifdata
is provided.- format
A
character
string defining the format used to parse the datetime values in thedatetime
column.- threshold
A
numeric
value representing the time difference threshold (in seconds) to determine whether events are independent. Events are considered independent if the time difference between them is greater than or equal to this threshold. The default is 30 minutes (1800 seconds).- only
A
logical
value indicating whether to return only the rows ofdata
that are identified as independent events. IfTRUE
, only independent events are returned. IfFALSE
, the entire data frame is returned with an additional column indicating the independence status. The default isTRUE
.
Value
If
data
is provided andonly
isTRUE
, a tibble of events identified as independent.If
data
is provided andonly
isFALSE
, a tibble of the original data with additional columns indicating theindependent
status anddeltatime
differences.If
data
is not provided, a tibble of thedeltatime
values withindependent
status.
Examples
library(dplyr)
#>
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#>
#> filter, lag
#> The following objects are masked from ‘package:base’:
#>
#> intersect, setdiff, setequal, union
# Load example dataset
cam_data <- read.csv(system.file("penessoulou_season1.csv", package = "maimer"))
# Independence without considering species occurrence
indep1 <- cam_data %>%
mm_independence(data = ., datetime = datetimes, format = "%Y-%m-%d %H:%M:%S",
only = TRUE)
sprintf("Independent observations: %s", nrow(indep1))
#> [1] "Independent observations: 177"
# Independence considering species occurrence
indep2 <- cam_data %>%
mm_independence(data = ., datetime = datetimes, format = "%Y-%m-%d %H:%M:%S",
only = TRUE, species_column = "species")
sprintf("Independent observations: %s", nrow(indep2))
#> [1] "Independent observations: 183"
# Use a standalone vector of datetime values
dtime <- cam_data$datetimes
mm_independence(datetime = dtime, format = "%Y-%m-%d %H:%M:%S", only = TRUE)
#> # A tibble: 177 × 1
#> datetime
#> <dttm>
#> 1 2019-01-20 15:24:41
#> 2 2019-01-21 15:23:38
#> 3 2019-01-23 15:35:20
#> 4 2019-01-24 06:01:07
#> 5 2023-09-20 16:15:28
#> 6 2023-09-21 13:32:45
#> 7 2024-03-02 22:32:10
#> 8 2024-03-10 10:21:56
#> 9 2024-03-10 15:04:38
#> 10 2024-03-10 17:28:11
#> # ℹ 167 more rows