Demo_Manuscript/1_Merge_Minute-by-minute.R

# Setup -------------------------------------------------------------------

  rm(list = ls()) # Clear memory
  library(magrittr) # Attach magrittr package (makes code more readable)
  devtools::load_all() # Load functions etc. from the FLASH package
  source("Demo_Manuscript/zz_merge_helpers.R") # Load some code/variables

# Read and format the data tracker ----------------------------------------

  tracker <- readRDS("data-raw/Data_Tracker.rds")

  ## Restrict to complete cases

    tracker %<>%
      {c("act24_file", "Hip_file_AG",  "SWA_File")} %>%
      tracker[ ,.] %>%
      complete.cases(.) %>%
      tracker[., ]

  ## Add demographic variables

    tracker <-
      readRDS("data-raw/Demographic/rds/demographics.rds") %>%
      {.[.$id %in% tracker$id, ]} %T>%
      {stopifnot(identical(.$id, tracker$id))} %>%
      .[ ,demo_vars] %>%
      merge(tracker, "id") %T>%
      {message("\n", nrow(.), " participants have complete data")}

    tracker %<>%
      {.[.$id != "F3B123", ]} %T>%
      {message(
        "\nOne participant (F3B123) removed due to",
        " an error when running Sojourn"
      )}

# Set up directories ------------------------------------------------------

    c(
      "Demo_Manuscript/_merged_data",
      "Demo_Manuscript/_merged_data/60s"
    ) %>%
    lapply(function(x) if (!dir.exists(x)) dir.create(x)) %>%
    invisible()
    
# Loop for each participant -----------------------------------------------

  for (i in seq(nrow(tracker))) {

    timer <- PAutilities::manage_procedure(
      "Start", "\nProcessing", i, "of", nrow(tracker),
      "--", tracker$id[i]
    )

    out_file <-
      tracker$id[i] %>%
      paste0("_merged_60s.rds") %>%
      file.path("Demo_Manuscript/_merged_data/60s", .)

    if (file.exists(out_file)) {
      message(
        "\n", basename(out_file),
        " already exists -- skipping"
      )
      next
    }

    cat("\n...Processing ActiGraph data")

      AG <-
        tracker$Hip_file_AG[i] %>%
        readRDS(.) %>%
        {.[ ,-1]} %>%
        data.frame(
          tracker[i, demo_vars], .,
          stringsAsFactors = FALSE,
          row.names = NULL
        )

      AG %<>%
        {.} %$%
        Sojourn::soj_3x_original(
          Axis1, Axis2, Axis3, Vector.Magnitude
        ) %>%
        {data.frame(
          AG, .[ ,-c(1:4)], stringsAsFactors = FALSE
        )} %>%
        AG_collapse(.) %>%
        renamer("AG_")

    cat("\n...Merging with ACT24 Data")

      AG <-
        tracker$act24_file[i] %>%
        readRDS(.) %>%
        within({is_nonwear = is.na(METs)}) %>%
        renamer("act24_") %>%
        merge(AG, .) %>%
        within({act24_kcal = act24_METs*weightkg/60}) %T>%
        {stopifnot(nrow(.) == 1440)}

    cat("\n...Merging with SWA Data")

      AG <-
        tracker$SWA_File[i] %>%
        readRDS(.) %>%
        {.[ ,swa_vars]} %>%
        {data.frame(
          .,
          swa_is_nonwear = apply(
            .[ ,setdiff(names(.), c("id", "SWA_Time"))],
            1, function(x) all(is.na(x))
          ),
          stringsAsFactors = FALSE,
          row.names = NULL
        )} %>%
        stats::setNames(
          ., tolower(names(.))
        ) %>%
        stats::setNames(
          ., gsub("^swa_time$", "Timestamp", names(.))
        ) %>%
        merge(AG, .) %>%
        within({swa_kcal = swa_mets * weightkg / 60}) %T>%
        {stopifnot(nrow(.) == 1440)}

    saveRDS(AG, out_file)
    PAutilities::manage_procedure("End", timer = timer)

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