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

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

```{r, setup}
library(card)
library(magrittr)
library(ggplot2)
library(dplyr)
```

# Zeitgeibers

This vignette demonstrates the use of the circadian rhythm analysis functions. It demonstrates a robust use of `circ_sun()` and `circ_center()` to create a new dataset for analysis that is rotated around the _zeitgeiber_ of sunrise.

```{r}
# Data set to be used is included
data("twins")
head(twins)
df <- 
  twins %>%
  subset(patid %in% c(1:30))
```

The research problem is that participants came from around the country for a study. Continuous, 24-hour measures were obtained. However, the patients came in from different time zones and on different dates over the course of 10 years. Findings could reflect perhaps jetlag instead of physiological disturbances. We could use sunrise and sunset times as natural _zeitgeibers_ to standardize the time series data.

The first step is inclusion of geographical locations, along with study date, to calculate sunrise times. This dataset was recorded in Atlanta, GA, but the individuals flew from across the country. The zipcodes are available in the dataset. This can then be converted to latitude and longitude using the {zipcode} package (which is archived on CRAN) - the zipcode data was bundled in this package for simplicity.

```{r}
# Zipcodes, contained as characters (because of leading 0)
data("zipcode")
head(zipcode)

# Get the zipcodes merged into to get latitude and longitude
df <- left_join(df, zipcode[c("zip", "latitude", "longitude")], by = "zip")

# Sunrise is dependent on location and date
df$sunrise <- circ_sun(date = df$date, lat = df$latitude, lon = df$longitude)
```

Another limitation is the issues in R with POSIX. The time zone of the sunrise is demarcated by "UTC", but is actually corrected for the timezone by location. Next, we have continuous measures of autonomic physiology measured by ECG, called _Dyx_. These are recorded in a variable called `dyxtime`. The recordings start in the afternoon, and continue to the next day. The sunrise in between is likely the best marker to center around. We need to use the circadian centering function for each patient to be able to compare them fairly.

```{r}
## Time series data
length(df$dyxtime)

# Number of participants
length(unique(df$patid))

# Unique sunrise times per patient
zeit <- 
  df %>%
  group_by(patid) %>%
  arrange(dyxtime) %>%
  select(patid, sunrise) %>%
  unique() %>%
  group_by(patid) %>%
  slice(n()) # Sunrise time during study
names(zeit)[2] <- "zeit"
  

# Add surnsie zeitgeiber back in
df %<>% left_join(., zeit, by = "patid")

x <- df %>%
  group_by(patid) %>%
  tidyr::nest()

# Slow and steady method for going through all the potential vectors
# Will look to "tidy" this in the future (TODO)
for(i in seq(x$patid)) {
  z <- unique(x[[i,2]][[1]]$zeit)
  t <- x[[i,2]][[1]]$dyxtime
  x[[i,2]][[1]]$zvec <- circ_center(x[[i,2]][[1]]$dyxtime, z)
}

# Visualize data trend
df <- tidyr::unnest(x)
summary(df$zvec)

# Pseudo-rose plot
ggplot(df, aes(x = hour, y = rDYX, group = hour, fill = hour)) + 
  geom_boxplot() +   
  coord_polar(theta = "x", start = 0, direction = 1) + 
  scale_x_continuous(expand = c(0,0), breaks = seq(0, 24, 1)) + 
  scale_fill_viridis_c() + 
  theme_minimal()
```



