Define Intermediate Segment of Melatonin Profile
Source:R/define_intermediate_segment.R
define_intermediate_segment.RdThis function identifies and labels the intermediate segment of a melatonin profile. The intermediate segment lies between the base segment and the ascending segment, where melatonin concentrations begin to rise but have not crossed the threshold value.
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).
- threshold
Numeric. The melatonin concentration threshold for defining profile segments (default = 2.3 pg/mL).
Value
A tibble with an additional intermediate column:
intermediate: Binary column (1 for intermediate segment, 0 otherwise).
If no intermediate segment is identified, the intermediate column will not be created.
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)
)
# Define the intermediate segment
updated_profile <- define_intermediate_segment(profile_data, threshold = 2.3)
#> Warning: Unknown or uninitialised column: `slope`.
#> Error in if (is.na(last_base_row) & is.na(profile_data$slope[1])) { profile_data$base[1] <- 1 last_base_row <- 1}: argument is of length zero
print(updated_profile)
#> Error: object 'updated_profile' not found