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

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

<style>
    h2 {
        font-size: 1.7rem;
    }
</style>

The activAnalyzer package was primarily built for working through a Shiny app. The procedure for using the app is explained in the related [user's guide](https://github.com/pydemull/activAnalyzer/blob/master/inst/guide/user_guide_en.pdf). The functions used in this app can also be used to analyze data outside the app, as shown below.

## Loading packages 
```{r setup, warning=FALSE, message=FALSE}
library(activAnalyzer)
library(magrittr)
library(ggplot2)
library(patchwork)
library(dplyr)
```

## Getting file 
```{r}
file <- system.file("extdata", "acc.agd", package = "activAnalyzer")
```

## Preparing dataset 
```{r, warning=FALSE, message=FALSE}
mydata <- prepare_dataset(data = file)
```

## Getting nonwear/wear time marks 
```{r, message = FALSE}
mydata_with_wear_marks <- 
  mydata %>%
  mark_wear_time(
    to_epoch = 60,
    cts = "vm",
    frame = 90, 
    allowanceFrame = 2, 
    streamFrame = 30
    )
```

```{r, include=FALSE}
height_factor <- nlevels(as.factor(mydata_with_wear_marks$date)) * 0.80
```

## Viewing data with nonwear/wear time marks 
```{r, fig.height = height_factor, fig.width=7, fig.align="center"}
#| fig.alt: >
#|   Plot with nonwear/wear time marks
plot_data(data = mydata_with_wear_marks, metric = "vm")
```

## Viewing data with nonwear/wear time marks and with a zoom in on the figure 
```{r, fig.height = height_factor, fig.width=7, fig.align="center"}
#| fig.alt: >
#|   Plot with nonwear/wear time marks and with a zoom in on the figure
plot_data(
  data = mydata_with_wear_marks, 
  metric = "vm",
  zoom_from = "16:00:00",
  zoom_to = "18:00:00"
  )
```

## Getting activity intensity marks 
```{r, warning = FALSE, message = FALSE}
mydata_with_intensity_marks <- 
  mark_intensity(
     data = mydata_with_wear_marks, 
     col_axis = "vm", 
     equation = "Sasaki et al. (2011) [Adults]",
     sed_cutpoint = 200, 
     mpa_cutpoint = 2690, 
     vpa_cutpoint = 6167, 
     age = 32,
     weight = 67,
     sex = "male"
    )
```

## Viewing data with activity intensity marks 
```{r, fig.height = height_factor, fig.width=7, fig.align="center"}
#| fig.alt: >
#|   Plot with activity intensity marks
plot_data_with_intensity(
  mydata_with_intensity_marks, 
  metric = "vm",
  valid_wear_time_start = "00:00:00",
  valid_wear_time_end = "23:59:59"
  )
```

## Viewing data with activity intensity marks and grey bands to highlight the daily period considered for analysis 
```{r, fig.height = height_factor, fig.width=7, fig.align="center"}
#| fig.alt: >
#|   Plot with activity intensity marks and grey bands to highlight the daily period considered for analysis
plot_data_with_intensity(
  mydata_with_intensity_marks, 
  metric = "vm",
  valid_wear_time_start = "07:00:00",
  valid_wear_time_end = "22:00:00"
  )
```

## Viewing data with activity intensity marks and a zoom in on the figure 
```{r, fig.height = height_factor, fig.width=7, fig.align="center"}
#| fig.alt: >
#|   Plot with activity intensity marks and a zoom in on the figure
plot_data_with_intensity(
  mydata_with_intensity_marks, 
  metric = "vm",
  zoom_from = "13:00:00",
  zoom_to = "16:30:00"
  )
```

## Getting activity metrics by day
```{r, message = FALSE}
results_by_day <-
  mydata_with_intensity_marks %>%
  recap_by_day(
    age = 32, 
    weight = 67, 
    sex = "male",
    valid_wear_time_start = "07:00:00",
    valid_wear_time_end = "22:00:00",
    start_first_bin = 0,
    start_last_bin = 10000,
    bin_width = 500
    )
```

## Computing means of the metrics using the valid days 
```{r, eval}
mean_results <-
  results_by_day$df_all_metrics  %>%
  average_results(minimum_wear_time = 10, fun = "mean")
```

## Computing medians of the metrics using the valid days 
```{r}
median_results <-
  results_by_day$df_all_metrics  %>%
  average_results(minimum_wear_time = 10, fun = "median")
```

## Showing activity volume metrics
### Results by day: Tabular view
```{r}
results_by_day$df_all_metrics %>% 
  dplyr::select(date:total_steps) %>%
  reactable::reactable(striped = TRUE, defaultColDef = reactable::colDef(align = "center", minWidth = 180))
```

### Results by day: Graphical view
```{r, fig.height=10, fig.width = 17, warning=FALSE, message=FALSE}
#| fig.alt: >
#|   Graphical view of the results by day for the activity volume metrics
create_fig_res_by_day(
  results_by_day$df_all_metrics, 
  minimum_wear_time_for_analysis = 10, 
  start_day_analysis = "00:00:00", 
  end_day_analysis = "23:59:00", 
  metrics = "volume",
  epoch_label = "60s"
  ) + theme(plot.margin = margin(1, 1, 1, 1, "cm"))
```

### Means | Medians computed using valid days
```{r}
create_flextable_summary(
  results_summary_means = mean_results, 
  results_summary_medians = median_results, 
  metrics = "volume",
  epoch_label = "60s"
  )
```
<br>

### Comparisons with norms and recommendations
```{r, fig.height=9, fig.width = 17, warning=FALSE, message=FALSE, fig.align="center"}
#| fig.alt: >
#|   Comparisons with norms and recommendations
# PAL
g_pal <- create_fig_pal(score = mean_results[["pal"]], "en") + theme(plot.margin = margin(2, 1, 0.5, 1, "cm"))
  
# Steps
g_steps <- create_fig_steps(score = mean_results[["total_steps"]], "en") + theme(plot.margin = margin(0, 1, 0.5, 1, "cm"))

# MVPA
g_mvpa <- create_fig_mvpa(score = mean_results[["minutes_MVPA"]], "en") + theme(plot.margin = margin(0, 1, 0, 1, "cm"))

# SED
g_sed <- create_fig_sed(score = mean_results[["minutes_SED"]], "en") + theme(plot.margin = margin(0, 1, 0, 1, "cm"))

# MVPA/SED ratio
g_ratio <- create_fig_ratio_mvpa_sed(score = mean_results[["ratio_mvpa_sed"]], "en") + theme(plot.margin = margin(0, 1, 1, 1, "cm"))

# Whole figure
(g_pal + theme(legend.position = "top")) / g_steps / (g_mvpa | g_sed | g_ratio) + 
    plot_layout(heights = c(0.8, 0.7, 1.5)) & theme(legend.justification = "center")
```

## Showing step accumulation metrics

### Results by day: Tabular view
```{r}
results_by_day$df_all_metrics %>% 
  dplyr::select(date, max_steps_60min:peak_steps_1min) %>%
  reactable::reactable(striped = TRUE, defaultColDef = reactable::colDef(align = "center", minWidth = 180))
```

### Results by day: Graphical view
```{r, fig.height=7, fig.width = 17, warning=FALSE, message=FALSE}
#| fig.alt: >
#|   Graphical view of the results by day for the step accumulation metrics
create_fig_res_by_day(
  results_by_day$df_all_metrics, 
  minimum_wear_time_for_analysis = 10, 
  start_day_analysis = "00:00:00", 
  end_day_analysis = "23:59:00", 
  metrics = "step_acc",
  epoch_label = "60s"
  ) + theme(plot.margin = margin(1, 1, 1, 1, "cm"))
```

### Means | Medians computed using valid days
```{r}
create_flextable_summary(
  results_summary_means = mean_results, 
  results_summary_medians = median_results,
  metrics = "step_acc",
  epoch_label = "60s"
  )
```
<br>

## Showing intensity gradient and MX metrics

### Distribution of time spent in intensity bins
```{r, fig.width = 15, fig.height = 10}
#| fig.alt: >
#|   Distribution of time spent in intensity bins | Bar plot
  results_by_day$p_band
```

```{r, fig.width = 15, fig.height = 10, message = FALSE}
#| fig.alt: >
#|   Distribution of time spent in intensity bins | Log-log plot
  results_by_day$p_log
```

### Results by day: Tabular view
```{r}
results_by_day$df_all_metrics %>% 
  dplyr::select(date, ig:M5) %>%
  reactable::reactable(striped = TRUE, defaultColDef = reactable::colDef(align = "center", minWidth = 180))
```

### Results by day: Graphical view
```{r, fig.height=7, fig.width = 17, warning=FALSE, message=FALSE}
#| fig.alt: >
#|   Graphical view of the results by day for the intensity gradient and MX metrics
create_fig_res_by_day(
  results_by_day$df_all_metrics, 
  minimum_wear_time_for_analysis = 10, 
  start_day_analysis = "00:00:00", 
  end_day_analysis = "23:59:00", 
  metrics = "int_distri",
  epoch_label = "60s"
  ) + theme(plot.margin = margin(1, 1, 1, 1, "cm"))
```

### Means | Medians computed using valid days
```{r}
create_flextable_summary(
  results_summary_means = mean_results, 
  results_summary_medians = median_results,
  metrics = "int_distri",
  epoch_label = "60s"
  )
```
<br>

### Radar view based on the means of valid days
```{r, fig.align = "center", out.width='50%', out.height='50%', fig.width=8, fig.height=8}
#| fig.alt: >
#|   Radar plot for the MX metrics
create_fig_mx_summary(
data = mean_results,
labels = NULL,
mpa_cutpoint = 2690, 
vpa_cutpoint = 6167
)
```

## Showing sedentary behaviour accumulation metrics based on valid days

### Getting results
```{r}
accum_metrics_sed <- 
  compute_accumulation_metrics(
    data = mydata_with_intensity_marks, 
    behaviour = "sed",
    dates = c("2021-04-07", "2021-04-08", "2021-04-09", "2021-04-10", "2021-04-11")
    )
```

### Temporal distribution of sedentary bouts
```{r, fig.height = height_factor/0.75*0.85, fig.width=7, fig.align="center"}
#| fig.alt: >
#|    Temporal distribution of sedentary bouts
accum_metrics_sed$p_breaks
```

### Alpha coefficient, median bout duration (MBD), usual bout duration (UBD), and Gini index
```{r, out.width='100%', out.height='60%', fig.height=11, fig.width=11, fig.align="center"}
#| fig.alt: >
#|    Alpha coefficient, median bout duration (MBD), usual bout duration (UBD), and Gini index related to sedentary behaviour
p1 <- accum_metrics_sed$p_alpha  + guides(color = "none", fill = "none")
p2 <- accum_metrics_sed$p_MBD    + guides(color = "none", fill = "none")
p3 <- accum_metrics_sed$p_UBD
p4 <- accum_metrics_sed$p_gini

(p1 | p2) / (p3 | p4) + plot_layout(guides = "collect") & theme(legend.position = 'bottom') 
```


## Showing physical activity accumulation metrics based on valid days

### Getting results
```{r}
accum_metrics_pa <- 
  compute_accumulation_metrics(
    mydata_with_intensity_marks, 
    behaviour = "pa",
    dates = c("2021-04-07", "2021-04-08", "2021-04-09", "2021-04-10", "2021-04-11")
    )
```

### Temporal distribution of physical activity bouts
```{r, fig.height = height_factor/0.75*0.85, fig.width=7, fig.align="center"}
#| fig.alt: >
#|   Temporal distribution of physical activity bouts
accum_metrics_pa$p_breaks
```

### Alpha coefficient, median bout duration (MBD), usual bout duration (UBD), and Gini index
```{r, out.width='100%', out.height='60%', fig.height=11, fig.width=11, fig.align="center"}
#| fig.alt: >
#|   Alpha coefficient, median bout duration (MBD), usual bout duration (UBD), and Gini index related to physical activity
p1 <- accum_metrics_pa$p_alpha  + guides(color = "none", fill = "none")
p2 <- accum_metrics_pa$p_MBD    + guides(color = "none", fill = "none")
p3 <- accum_metrics_pa$p_UBD
p4 <- accum_metrics_pa$p_gini

(p1 | p2) / (p3 | p4) + plot_layout(guides = "collect") & theme(legend.position = 'bottom') 
```