R/AG_Nonwear.R

Defines functions AG_nonwear

Documented in AG_nonwear

#' Determine non-wear for ActiGraph data using the Choi algorithm
#'
#' @param AG data frame containing ActiGraph data read in using
#'   \code{AGread::read_AG_counts}
#'
#' @keywords internal
AG_nonwear <- function(AG) {

  ag60 <- AGread::reintegrate(
    AG, 60, "Timestamp", "forwards"
  )

  invisible(utils::capture.output(
    nonwear <-
      within(ag60, {Timestamp = as.character(Timestamp)}) %>%
      PhysicalActivity::wearingMarking(
        perMinuteCts = 1, TS = "Timestamp", cts = "Axis1"
      ) %>%
      .$wearing %>%
      as.character(.) %>%
      PAutilities::index_runs(.) %>%
      {.[.$values == "nw", ]} %>%
      within({
        start_index = ag60$Timestamp[start_index]
        end_index = ag60$Timestamp[end_index] + 59
      }) %>%
      {lubridate::interval(.$start_index, .$end_index)}
  ))

  AG$Choi_isNonWear <-
    AG$Timestamp %>%
    sapply(function(x) {
      lubridate::`%within%`(x, nonwear) %>%
      any(.)
    })

  AG

}
PAHPLabResearch/FLASH documentation built on May 15, 2020, 7:08 p.m.