# detect_all_events function : cgmguru vs iglu

## Datasets

We use two CGM example datasets shipped with `iglu`:

- `example_data_5_subject`: 5 subjects
- `example_data_hall`: 19 subjects

    data(example_data_5_subject, package = "iglu")
    data(example_data_hall, package = "iglu")

    # Base-R summaries (no external dependencies)
    summary_5 <- data.frame(
      rows = nrow(example_data_5_subject),
      subjects = length(unique(example_data_5_subject$id)),
      time_min = min(example_data_5_subject$time),
      time_max = max(example_data_5_subject$time),
      gl_min = min(example_data_5_subject$gl, na.rm = TRUE),
      gl_max = max(example_data_5_subject$gl, na.rm = TRUE)
    )

    summary_5
    #>    rows subjects            time_min            time_max gl_min gl_max
    #> 1 13866        5 2015-02-24 17:31:29 2015-06-19 08:59:36     50    400

    cat("Note: The 'iglu' package is not available; vignette examples are skipped.\n")

## iglu: episode_calculation

`iglu::episode_calculation()` identifies hypo/hyperglycemia episodes.

    iglu_episodes_5 <- iglu::episode_calculation(
      data = example_data_5_subject
    )
    print(iglu_episodes_5)
    #> # A tibble: 35 × 7
    #>    id        type  level avg_ep_per_day avg_ep_duration avg_ep_gl total_episodes
    #>    <fct>     <chr> <chr>          <dbl>           <dbl>     <dbl>          <dbl>
    #>  1 Subject 1 hypo  lv1           0.0899            35        68.6              1
    #>  2 Subject 1 hypo  lv2           0                  0        NA                0
    #>  3 Subject 1 hypo  exte…         0                  0        NA                0
    #>  4 Subject 1 hyper lv1           1.44              80.3     200.              16
    #>  5 Subject 1 hyper lv2           0.180             30       264.               2
    #>  6 Subject 1 hypo  lv1_…         0.0899            35        68.6              1
    #>  7 Subject 1 hyper lv1_…         1.26              79.6     195.              14
    #>  8 Subject 2 hypo  lv1           0                  0        NA                0
    #>  9 Subject 2 hypo  lv2           0                  0        NA                0
    #> 10 Subject 2 hypo  exte…         0                  0        NA                0
    #> # ℹ 25 more rows

    iglu_episodes_hall <- iglu::episode_calculation(
      data = example_data_hall
    )
    print(iglu_episodes_hall)
    #> # A tibble: 133 × 7
    #>    id        type  level avg_ep_per_day avg_ep_duration avg_ep_gl total_episodes
    #>    <chr>     <chr> <chr>          <dbl>           <dbl>     <dbl>          <dbl>
    #>  1 1636-69-… hypo  lv1            0.468            15        68.1              3
    #>  2 1636-69-… hypo  lv2            0                 0        NA                0
    #>  3 1636-69-… hypo  exte…          0                 0        NA                0
    #>  4 1636-69-… hyper lv1            0.623            57.5     201.               4
    #>  5 1636-69-… hyper lv2            0                 0        NA                0
    #>  6 1636-69-… hypo  lv1_…          0.468            15        68.1              3
    #>  7 1636-69-… hyper lv1_…          0.623            57.5     201.               4
    #>  8 1636-69-… hypo  lv1            0                 0        NA                0
    #>  9 1636-69-… hypo  lv2            0                 0        NA                0
    #> 10 1636-69-… hypo  exte…          0                 0        NA                0
    #> # ℹ 123 more rows

## cgmguru: detect_all_events

    all_events_5 <- detect_all_events(example_data_5_subject, reading_minutes = 5)
    print(all_events_5)
    #> # A tibble: 40 × 6
    #>    id        type  level    total_episodes avg_ep_per_day avg_episode_duration…¹
    #>    <chr>     <chr> <chr>             <int>          <dbl>                  <dbl>
    #>  1 Subject 1 hypo  lv1                   1           0.08                      0
    #>  2 Subject 1 hypo  lv2                   0           0                         0
    #>  3 Subject 1 hypo  extended              0           0                         0
    #>  4 Subject 1 hypo  lv1_excl              1           0.08                      0
    #>  5 Subject 1 hyper lv1                  14           1.1                       0
    #>  6 Subject 1 hyper lv2                   2           0.16                      0
    #>  7 Subject 1 hyper extended              0           0                         0
    #>  8 Subject 1 hyper lv1_excl             12           0.95                      0
    #>  9 Subject 2 hypo  lv1                   0           0                         0
    #> 10 Subject 2 hypo  lv2                   0           0                         0
    #> # ℹ 30 more rows
    #> # ℹ abbreviated name: ¹​avg_episode_duration_below_54

    all_events_hall <- detect_all_events(example_data_hall, reading_minutes = 5)
    print(all_events_hall)
    #> # A tibble: 152 × 6
    #>    id          type  level  total_episodes avg_ep_per_day avg_episode_duration…¹
    #>    <chr>       <chr> <chr>           <int>          <dbl>                  <dbl>
    #>  1 1636-69-001 hypo  lv1                 2           0                         0
    #>  2 1636-69-001 hypo  lv2                 0           0                         0
    #>  3 1636-69-001 hypo  exten…              0           0                         0
    #>  4 1636-69-001 hypo  lv1_e…              2           0                         0
    #>  5 1636-69-001 hyper lv1                 4           0.01                      0
    #>  6 1636-69-001 hyper lv2                 0           0                         0
    #>  7 1636-69-001 hyper exten…              0           0                         0
    #>  8 1636-69-001 hyper lv1_e…              4           0.01                      0
    #>  9 1636-69-026 hypo  lv1                 0           0                         0
    #> 10 1636-69-026 hypo  lv2                 0           0                         0
    #> # ℹ 142 more rows
    #> # ℹ abbreviated name: ¹​avg_episode_duration_below_54

## Speed comparison

We compare performance using `microbenchmark` on both datasets. Each benchmark contrasts `iglu::episode_calculation()` with `cgmguru::detect_all_events()`.

    library(microbenchmark)
    library(iglu)

    # example_data_5_subject
    bench_5 <- microbenchmark(
      episode_calculation = iglu::episode_calculation(example_data_5_subject),
      detect_all_events   = cgmguru::detect_all_events(example_data_5_subject, reading_minutes = 5),
      times = 10,
      unit = "ms"
    )
    print(bench_5)
    #> Unit: milliseconds
    #>                 expr        min         lq       mean     median         uq
    #>  episode_calculation 384.799473 390.001061 394.910766 390.266372 394.868458
    #>    detect_all_events   1.706994   1.729134   1.774878   1.775218   1.800474
    #>        max neval cld
    #>  428.10617    10  a
    #>    1.84951    10   b

    # example_data_hall (all subjects)
    bench_hall <- microbenchmark(
      episode_calculation = iglu::episode_calculation(example_data_hall),
      detect_all_events   = cgmguru::detect_all_events(example_data_hall, reading_minutes = 5),
      times = 10,
      unit = "ms"
    )
    print(bench_hall)
    #> Unit: milliseconds
    #>                 expr         min          lq        mean     median          uq
    #>  episode_calculation 1092.125528 1102.494756 1133.194166 1122.38427 1142.780085
    #>    detect_all_events    3.903241    4.007217    4.025917    4.03237    4.049775
    #>          max neval cld
    #>  1216.378201    10  a
    #>     4.106027    10   b

    cat("Note: Installed 'iglu' version has a different 'episode_calculation' API; iglu examples are skipped.\n")