Truncate Ascending Segment of Melatonin Profile
Source:R/truncate_ascending_segment.R
truncate_ascending_segment.RdThis function truncates the ascending segment of a melatonin profile based on a set of
rules to ensure valid slopes and segment consistency. It iteratively modifies the
ascending column of the input data to satisfy the rules and uses the
parallelogram_fit function as a diagnostic measure to validate the final segment. The
parallelogram_fit function evaluates the geometric structure of the ascending
segment and ensures the segment satisfies predefined geometric constraints, including
slope ratios of lateral and diagonal shape segments. This ensures that only the steadily
increasing part of the melatonin rise is taken into account when determining the DLMO point
and that slower rises, or drops in melatonin levels are not.
Arguments
- profile_data
A tibble containing the melatonin profile with the following columns:
datetime: A POSIXct column with timestamps.melatonin: Numeric column representing melatonin concentrations.slope: Numeric column with the slope between consecutive melatonin points.ascending: Binary column (1 for ascending segment, 0 otherwise).
Value
A list containing:
profile: The updated profile_data tibble with truncated ascending segments.plll: The result of the parallelogram fit diagnostic (fromparallelogram_fit).
This function ensures the following rules are satisfied:
The rightmost slope in the ascending segment cannot be less than half the steepest slope in the segment.
The ascending segment must adhere to additional conditions validated by the
parallelogram_fitfunction.
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, 1.7, 2.0, 2.3, 2.8, 3.5, 4.2, 4.7, 5.0, 5.2),
slope = c(NA, diff(c(1.2, 1.4, 1.5, 1.7, 2.0, 2.3, 2.8, 3.5, 4.2, 4.7, 5.0, 5.2))),
ascending = c(0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1)
)
# Truncate the ascending segment
result <- truncate_ascending_segment(profile_data)
print(result$profile)
#> # A tibble: 12 × 4
#> datetime melatonin slope ascending
#> <dttm> <dbl> <dbl> <dbl>
#> 1 2023-01-01 20:00:00 1.2 NA 0
#> 2 2023-01-01 20:15:00 1.4 0.2 0
#> 3 2023-01-01 20:30:00 1.5 0.100 0
#> 4 2023-01-01 20:45:00 1.7 0.2 0
#> 5 2023-01-01 21:00:00 2 0.3 1
#> 6 2023-01-01 21:15:00 2.3 0.300 1
#> 7 2023-01-01 21:30:00 2.8 0.5 1
#> 8 2023-01-01 21:45:00 3.5 0.7 1
#> 9 2023-01-01 22:00:00 4.2 0.7 1
#> 10 2023-01-01 22:15:00 4.7 0.5 1
#> 11 2023-01-01 22:30:00 5 0.300 1
#> 12 2023-01-01 22:45:00 5.2 0.200 1
print(result$plll)
#> $pll_datetime_0
#> [1] "2023-01-01 20:42:00 UTC"
#>
#> $pll_datetime_1
#> [1] "2023-01-01 20:56:41 UTC"
#>
#> $pll_slope
#> [1] 1.93477
#>
#> $corners
#> $corners$ll
#> [1] 20.7001 1.7000
#>
#> $corners$lr
#> [1] 20.94485 1.70000
#>
#> $corners$ur
#> [1] 22.75385 5.20000
#>
#> $corners$ul
#> [1] 22.5091 5.2000
#>
#>
#> $datetime_ref
#> [1] "2023-01-01 20:30:00 UTC"
#>
#> $flag
#> [1] FALSE
#>