/
README.Rmd
117 lines (86 loc) · 5.55 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
---
output:
md_document:
variant: gfm
bibliography: actigraph.bib
---
```{r, echo = FALSE}
version <- as.vector(read.dcf("DESCRIPTION")[, "Version"])
version <- gsub("-", ".", version)
dep <- as.vector(read.dcf("DESCRIPTION")[, "Depends"])
m <- regexpr("R *\\(>= \\d+.\\d+.\\d+\\)", dep)
rm <- regmatches(dep, m)
rvers <- gsub(".*(\\d+.\\d+.\\d+).*", "\\1", rm)
```
<!-- badges: start -->
[![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
[![R-CMD-check](https://github.com/dipetkov/actigraph.sleepr/workflows/R-CMD-check/badge.svg)](https://github.com/dipetkov/actigraph.sleepr/actions)
[![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/dipetkov/actigraph.sleepr?branch=master&svg=true)](https://ci.appveyor.com/project/dipetkov/actigraph.sleepr)
[![codecov](https://codecov.io/gh/dipetkov/actigraph.sleepr/branch/master/graph/badge.svg)](https://codecov.io/gh/dipetkov/actigraph.sleepr)
<!-- badges: end -->
<!-- README.md is generated from README.Rmd. Please edit that file -->
### `actigraph.sleepr`: Sleep and non-wear detection from ActiGraph data
```{r , echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```
The `actigraph.sleepr` package implements three sleep scoring/detection algorithms: Sadeh [@Sadeh:1994aa], Cole-Kripke [@Cole:1992aa] and Tudor-Locke [@Tudor-Locke:2014aa] as well as two non-wear detection algorithms: Troiano [@Troiano:2008aa] and Choi [@Choi:2011aa].
### Installation
```{r , eval = FALSE}
# install.packages("remotes")
remotes::install_github("dipetkov/actigraph.sleepr")
```
### Read AGD file(s)
An AGD file is an SQLite database file exported by an ActiGraph device. See the [ActiLife 6 User manual](https://www.actigraphcorp.com/support/manuals/actilife-6-manual/). For illustration let's use one day of sample data recorded by a GT3X+ monitor.
```{r }
library("actigraph.sleepr")
file_10s <- system.file("extdata", "GT3XPlus-RawData-Day01.agd",
package = "actigraph.sleepr"
)
agdb_10s <- read_agd(file_10s)
```
The `read_agd` function loads the raw activity measurements into a convenient format: a tibble (data frame) of timestamped activity counts, whose attributes are the device settings. However, further manipulations of the tibble using the `dplyr` verbs (e.g., `mutate`, `inner_join`) might drop these non-standard attributes, i.e., the attributes are not always inherited.
```{r }
attributes(agdb_10s)[10:12]
```
Since the data is stored in a tibble, we can use the dplyr verbs (mutate, select, filter, summarise, group_by, arrange) to manipulate the data. For example, let's compute the vector magnitude of the three-axis counts (axis1 - vertical, axis2 - horizontal, axis3 - lateral).
```{r }
suppressMessages(library("dplyr"))
agdb_10s <- agdb_10s %>% select(timestamp, starts_with("axis"))
agdb_10s %>%
mutate(magnitude = sqrt(axis1^2 + axis2^2 + axis3^2))
```
### Reintegrate from 10s to 60s epochs
The Sadeh and Cole-Kripke algorithms for converting activity measurements into asleep/awake indicators were developed for 60s epochs. If the data is in smaller epochs, we need to collapse or aggregate the epochs. The example data is in 10s epochs. So we aggregate the epochs from 10s to 60s by adding the counts for the six consecutive 10s epochs that fall into the same 60s epoch.
```{r }
# Collapse epochs from 10 sec to 60 sec by summing
agdb_60s <- agdb_10s %>% collapse_epochs(60)
agdb_60s
```
### Sleep scoring with the Sadeh algorithm
The Sadeh algorithm is primarily used for younger adolescents as the supporting research was performed on children and young adults. It takes 60s epochs and uses an 11-minute window that includes the five previous and five future epochs. The `apply_sadeh` function implements the algorithm as described in the ActiGraph user manual.
```{r }
agdb_60s %>% apply_sadeh()
```
### Sleep scoring with the Cole-Kripke algorithm
The Cole-Kripke algorithm is primarily used for adult populations as the supporting research was performed on subjects ranging from 35 to 65 years of age. Like the Sadeh algorithm, it takes 60s epochs and uses a 7-minute window that includes the four previous and two future epochs. The `apply_cole` function implements the algorithm as described in the ActiGraph user manual.
```{r }
agdb_60s %>% apply_cole_kripke()
```
### Sleep period detection with the Tudor-Locke algorithm
Once each one-minute epoch is labeled as asleep (S) or awake (W), we can use the Tudor-Locke algorithm to detect periods of time in bed and, for each period, to compute sleep quality metrics such as total minutes in bed, total sleep time, number and average length of awakenings, movement and fragmentation index.
```{r }
agdb_60s %>%
apply_sadeh() %>%
apply_tudor_locke()
```
### Non-wear period detection with the Troiano and Choi algorithms
Long stretches that consist almost entirely of zero counts (zero epochs) suggest that the device wasn't worn at all and therefore should be excluded from downstream analysis. The Troiano algorithm for detecting periods of non-wear formalizes a technique used to analyze the 2003-2004 NHANES data, which allows a non-wear period to contain a few nonzero epochs of artifactual movement (spikes). The Choi algorithm extends the Troiano algorithm by requiring that short spikes of artifactual movement during a non-wear period are preceded and followed by a fixed number of consecutive zero epochs.
```{r }
agdb_60s %>% apply_troiano()
agdb_60s %>% apply_choi()
```
### References