R-CMD-check Codecov test coverage

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  cache = TRUE
)

SummarizedActigraphy Package:

The goal of SummarizedActigraphy is to provide functions for reading Actigraphy data and turn it into SummarizedExperiments.

Installation

You can install SummarizedActigraphy from GitHub with:

# install.packages("remotes")
remotes::install_github("muschellij2/SummarizedActigraphy")

Reading in some data

library(SummarizedActigraphy)
url = paste0("https://github.com/THLfi/read.gt3x/files/",
             "3522749/GT3X%2B.01.day.gt3x.zip")
destfile = tempfile(fileext = ".zip")
dl = utils::download.file(url, destfile = destfile)
gt3x_file = utils::unzip(destfile, exdir = tempdir())
gt3x_file = gt3x_file[!grepl("__MACOSX", gt3x_file)]
path = gt3x_file
result = summarize_actigraphy(path)
result

Reading in GT3X files

In GT3X files, you must be aware of Idle Sleep Mode, which saves power on the device, but the device essentially stops recording. In most cases, you still want a "full" time series without missing elements. To read in gt3x files, we use read.gt3x::read.gt3x. By default read.gt3x::read.gt3x, that data isn’t in the output data set. If you use the imputeZeros = TRUE argument, then they are there, but all 0s, which doesn’t make sense (no gravity even!?).

In Actigraph's ActiLife, the raw output data repeats the same value before the device went into sleep mode. To mimic this behavior, the SummarizedActigraphy::fix_zeros function sets the values in the rows with all 0 to be NA (just for X/Y/Z, not time), and then uses zoo::na.locf for last observation carried forward (LOCF). Aside: you could also use tidyr::fill(direction = "down") for the tidyverse folks.

In SummarizedActigraphy::read_actigraphy, we use read.gt3x::read.gt3x(asDataFrame = TRUE, imputeZeroes = TRUE) as the default.

See below as an example.

data = SummarizedActigraphy::read_actigraphy(path)
df = data$data
df = tibble::as_tibble(df)
all_zero = df$X == 0 & df$Y == 0 & df$Z == 0
df[all_zero, ] %>% 
  dplyr::mutate(time = lubridate::floor_date(time, "1 sec")) %>% 
  dplyr::distinct()
df = SummarizedActigraphy::fix_zeros(df)
df[all_zero, ] %>% 
  dplyr::mutate(time = lubridate::floor_date(time, "1 sec")) %>% 
  dplyr::distinct()

Note well, the fix_zeros does not do anything with data that is all zero in the beginning of a time series (as there is no observation to carry forward). This behavior should mimic ActiLife to our knowledge and that data is likely to be discarded regardless.

Converting to wide 1440 format

hms_times = structure(seq(0, 86340, by = 60), class = c("hms", "difftime"),
                      units = "secs")
hms_times = tibble::tibble(time = hms_times)
measure = "AI_mean"
tmeasure = c("time", measure)
x = result[, tmeasure, drop = FALSE]
x = tibble::as_tibble(x)
x = dplyr::left_join(hms_times, x)
head(x)
x1440 = tidyr::spread(x, time, value = measure)
x1440


muschellij2/SummarizedActigraphy documentation built on April 9, 2024, 8:32 a.m.