This function defines the Region of Interest (ROI) in a melatonin profile. The ROI is determined based on the base, intermediate, and ascending segments, as well as the melatonin threshold. The function calculates the horizontal (time) and vertical (melatonin level) bounds of the ROI.
Arguments
- profile_data
A tibble containing the melatonin profile with the following columns:
datetime: A POSIXct column representing timestamps for each measurement.melatonin: Numeric column representing melatonin concentrations.base: Binary column (1 for base segment, 0 otherwise).ascending: Binary column (1 for ascending segment, 0 otherwise).intermediate(optional): Binary column (1 for intermediate segment, 0 otherwise).
- threshold
Numeric. The melatonin concentration threshold used to define the vertical upper bound of the ROI (default = 2.3).
Value
A list with the following components:
x_start: POSIXct. The start time of the ROI.x_end: POSIXct. The end time of the ROI.y_min: Numeric. The lower bound of the ROI (minimum melatonin concentration in the profile).y_max: Numeric. The upper bound of the ROI (set to the threshold).
Examples
library(dplyr)
library(lubridate)
# Example data
profile_data <- tibble(
datetime = seq(ymd_hms("2023-01-01 20:00:00"), by = "15 min", length.out = 12),
melatonin = c(1.2, 1.4, 1.5, 2.0, 2.5, 2.3, 2.8, 3.5, 4.2, 4.7, 5.0, 5.2),
base = c(1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0),
ascending = c(0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1),
intermediate = c(0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0)
)
# Define the ROI
roi <- define_roi(profile_data, threshold = 2.3)
print(roi)
#> $x_start
#> [1] "2023-01-01 20:46:30 UTC"
#>
#> $x_end
#> [1] "2023-01-01 21:58:30 UTC"
#>
#> $y_min
#> [1] 1.2
#>
#> $y_max
#> [1] 2.3
#>