---
title: "AGP and Episodes"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{AGP and Episodes}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(iglu)
```
The iglu package includes two single page reports - an ambulatory glucose profile (AGP), and an episode calculation report.

# Ambulatory Glucose Profile (AGP)

The iglu package allows one to generate an Ambulatory Glucose Profile (AGP) report - see [Johnson (2019) "Utilizing the ambulatory glucose profile to standardize and implement continuous glucose monitoring in clinical practice."](https://doi.org/10.1089/dia.2019.0034). Below is an example report for Subject 1, which includes information on data collection period, time spent in standardized glycemic ranges (cutoffs of 54, 70, 180 and 250 mg/dL) displayed as a stacked bar chart, glucose variability as measured by %CV, and visualization of quantiles of the glucose profile across days together with daily glucose views. 

```{r, fig.width=10, fig.height=10, out.width="600px", out.height ="500px"}
agp(example_data_1_subject)
```


## Episode Calculation

The episode_Calculation function measures the number of hypoglycemia and hyperglycemia episodes or events.

```{r}
episode_calculation(example_data_1_subject,lv2_hypo = 70, lv1_hypo = 120, lv2_hyper = 180, dur_length = 15)
```

In this example, example_data_1_subject contains multiple days, and episode_calculation function calculate the episodes across all days.

### Parameters

#### data 
DataFrame object with column names "id", "time", and "gl". 

#### lv1_hypo, lv2_hypo, hy1_hyper, lv2_hpyer
Users can set certain thresholds for the hypo and hyperglycemia by passing parameters, lv1_hypo, lv2_hypo, hy1_hyper, lv2_hpyer. Level 2 indicates more severe states than level 1 so the threshold value for the lv2_hypo value should be lower than hy1_hypo value, and the threshold for the lv2_hyper value should higher than lv1_hyper value.

#### dur_length
By setting a duration length to 15 minutes (the last parameter), the function will count the number of episodes that glucose values go below or above the thresholds more than 15 minutes. 

### Return value
The function returns a dataframe for average number of episodes, average episode duration, and average glucose in episodes. Optionally, the function can return the input data with interpolation and episode labeling added.

## Epicalc_profile function

Visualization of the metrics produced by the `episode_calculation()` function is done with the function `epicalc_profile()`. This function takes the `episode_calculation()` output and displays it as a tables of the episode metrics as well as plots that visualizes the subject's episodes. This function is designed to work with one subject data at a time, and the structure of the function output is shown below.

```{r, fig.width=8, fig.height=4}
epicalc_profile(example_data_1_subject)
```