Skip to contents

This function identifies and labels the base segment of a melatonin profile, where melatonin concentrations are below a specified threshold and the slope of the profile is non-positive (indicating stable or decreasing melatonin levels).

Usage

define_base_segment(profile_tibble, threshold = 2.3)

Arguments

profile_tibble

A tibble containing datetime, melatonin, and time columns.

  • datetime: A POSIXct column representing timestamps for each melatonin measurement.

  • melatonin: A numeric column representing melatonin concentrations at each time point.

  • time: A numeric column representing the time in hh:mm:ss format.

threshold

Numeric. The melatonin concentration threshold to define the base segment (default = 2.3 pg/mL).

Value

A tibble with the base segment labeled (1 for base, 0 for non-base), including:

  • datetime: The original timestamps.

  • melatonin: The input melatonin concentrations.

  • time: The hh:mm:ss time format.

  • slope: The rate of change in melatonin concentrations between consecutive time points.

  • base: A binary indicator (1 for points in the base segment, 0 otherwise).

Examples

# Example data
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
library(lubridate)
#> 
#> Attaching package: ‘lubridate’
#> The following objects are masked from ‘package:base’:
#> 
#>     date, intersect, setdiff, union

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),
  time = decimal_date(seq(ymd_hms("2023-01-01 20:00:00"), by = "15 min", length.out = 12))
)

# Define the base segment using a threshold of 2.3
base_segment <- define_base_segment(profile_tibble = profile_data, threshold = 2.3)

# View the results
print(base_segment)
#> # A tibble: 12 × 5
#>    datetime            melatonin  time  slope  base
#>    <dttm>                  <dbl> <dbl>  <dbl> <dbl>
#>  1 2023-01-01 20:00:00       1.2 2023. NA         0
#>  2 2023-01-01 20:15:00       1.4 2023.  0.8       0
#>  3 2023-01-01 20:30:00       1.5 2023.  0.400     0
#>  4 2023-01-01 20:45:00       1.7 2023.  0.8       0
#>  5 2023-01-01 21:00:00       2   2023.  1.2       0
#>  6 2023-01-01 21:15:00       2.3 2023.  1.2       0
#>  7 2023-01-01 21:30:00       2.8 2023.  2         0
#>  8 2023-01-01 21:45:00       3.5 2023.  2.8       0
#>  9 2023-01-01 22:00:00       4.2 2023.  2.8       0
#> 10 2023-01-01 22:15:00       4.7 2023.  2         0
#> 11 2023-01-01 22:30:00       5   2023.  1.2       0
#> 12 2023-01-01 22:45:00       5.2 2023.  0.800     0