R/ipaq.R

Defines functions scoring_ipaqsf

Documented in scoring_ipaqsf

#' @title {Scoring the International Physical Activity Questionnaire (IPAQ) short form}
#' @description {\emph{The purpose of the IPAQ is to provide a set of well-developed instruments
#' that can be used internationally to obtain comparable estimates of physical activity.
#' There are two versions of the questionnaire. The short version is suitable for use in
#' national and regional surveillance systems and the long version provide more detailed
#' information often required in research work or for evaluation purposes.} (The IPAQ Group)}
#' @details
#' \itemize{
#' \item \code{Number of items:} {11}
#' \item \code{Item range:} {none}
#' \item \code{Reverse items:} {none}
#' \item \code{Score range:} {none}
#' \item \code{Cut-off-values:} {none}
#' \item \code{Minimal clinically important difference:} {none}
#' \item \code{Treatment of missing values:}
#' {not reported}
#' }
#' @references
#' Craig et al. 2003 (\url{https://doi.org/10.1249/01.MSS.0000078924.61453.FB})
#'
#' The IPAQ Group (2005). Guidelines for Data Processing and Analysis of the International Physical Activity Questionnaire. Retrieved from (\url{http://www.ipaq.ki.se})
#' @return The function returns 20 variables:
#' \itemize{
#'  \item \code{vminday:} Number of minutes in vigorous physical activity per day
#'  \item \code{mminday:} Number of minutes in moderate physical activity per day
#'  \item \code{wminday:} Number of minutes walking per day
#'  \item \code{sminday:} Number of minutes sitting per day
#'  \item \code{totalhourday:} Number of hours in total (vigorous, moderate, walking, sitting) per day
#'  \item \code{vminwk:} Number of minutes in vigorous physical activity per week
#'  \item \code{mminwk:} Number of minutes in moderate physical activity per week
#'  \item \code{wminwk:} Number of minutes walking per week
#'  \item \code{sminwk:} Number of minutes sitting per week
#'  \item \code{sumpa:} Total number of minutes of vigorous pa, moderate pa, and walking per week
#'  \item \code{sumday:} Total number of days of vigorous pa, moderate pa, and walking per week
#'  \item \code{excMin:} Flag if > 960 minutes per day
#'  \item \code{excDay:} Flag if sum of days of vigorous pa, moderate pa, and walking per week > 21
#'  \item \code{exc16h:} Flag if hours of daily activity and sitting exceeds 16h
#'  \item \code{vminwkMET:} MET–minutes in vigorous physical activity per week
#'  \item \code{mminwkMET:} MET–minutes in moderate physical activity per week
#'  \item \code{wminwkMET:} MET–minutes walking per week
#'  \item \code{MET:} MET–minutes in total per week
#'  \item \code{kilocalories:} Kilocalories per week
#'  \item \code{pacat:} Levels of physical activity (Low, Moderate, High)
#' }
#' @examples
#' \dontrun{
#' library(dplyr)
#' scoring_ipaqsf(mydata, items = items.ipaq, weight = WEIGHT)
#' }
#' @param data a \code{\link{data.frame}} containing the IPAQ items
#' orderd from 1 to 11:
#' \enumerate{
#'  \item \code{VigDays:} Number of days doing vigorous physical activity per week
#'  \item \code{VigHours:} Number of hours in vigorous physical activity per day
#'  \item \code{VigMin:} Number of minutes in vigorous physical activity per day
#'  \item \code{ModDays:} Number of days doing moderate physical activity per week
#'  \item \code{ModHours:} Number of hours in moderate physical activity per day
#'  \item \code{ModMin:} Number of minutes in moderate physical activity per day
#'  \item \code{WalkDays:} Number of days walking per week
#'  \item \code{WalkHours:} Number of hours walking per day
#'  \item \code{WalkMin:} Number of minutes in walking per day
#'  \item \code{SitHours:} Number of hours sitting per day
#'  \item \code{SitMin:} Number of minutes sitting per day
#' }
#' @param items A character vector with the IPAQ  item names ordered from 1 to 11,
#' or a numeric vector indicating the column numbers of the IPAQ  items in \code{data}.
#' @param keep Logical, whether to keep the single items and  whether to return variables containing
#' the number of non-missing items on each scale for each respondent. The default is TRUE.
#' @param weight Character vector with name of the body weight variable
#' @export
scoring_ipaqsf <- function(data, items = 1:11, keep = TRUE, weight) {
  library(dplyr, warn.conflicts = FALSE)
  items <- items
  newvars <- c(
    "VigDays", "VigHours", "VigMin", "ModDays",
    "ModHours", "ModMin", "WalkDays", "WalkHours",
    "WalkMin", "SitHours", "SitMin", "Weight"
  )
  # check for number of specified items
  if (length(items) != 11) {
    stop("Number of items must be 11!")
  }

  data <- data %>%
    mutate(
      VigDays = rowSums(select(., items[1])),
      VigHours = rowSums(select(., items[2])),
      VigMin = rowSums(select(., items[3])),
      ModDays = rowSums(select(., items[4])),
      ModHours = rowSums(select(., items[5])),
      ModMin = rowSums(select(., items[6])),
      WalkDays = rowSums(select(., items[7])),
      WalkHours = rowSums(select(., items[8])),
      WalkMin = rowSums(select(., items[9])),
      SitHours = rowSums(select(., items[10])),
      SitMin = rowSums(select(., items[11])),
      Weight = rowSums(select(., weight))
    ) %>%
    # If number of days == 0, set hours and minutes to 0
    # If number of days is missing, set hours and minutes to missing
    mutate_at(vars(VigHours, VigMin), list(~case_when(
      VigDays == 0 ~ 0,
      is.na(VigDays) ~ as.numeric(NA),
      TRUE ~ .
    ))) %>%
    mutate_at(vars(ModHours, ModMin), list(~case_when(
      ModDays == 0 ~ 0,
      is.na(ModDays) ~ as.numeric(NA),
      TRUE ~ .
    ))) %>%
    mutate_at(vars(WalkHours, WalkMin), list(~case_when(
      WalkDays == 0 ~ 0,
      is.na(WalkDays) ~ as.numeric(NA),
      TRUE ~ .
    ))) %>%
    mutate(
      # If number of hours is missing, but number of minutes is not missing, set hours = 0
      # If number of minutes is missing, but number of hours is not missing, set minutes = 0
      VigHours = ifelse(!is.na(VigDays) & !is.na(VigMin) & is.na(VigHours), 0, VigHours),
      VigMin = ifelse(!is.na(VigDays) & !is.na(VigHours) & is.na(VigMin), 0, VigMin),
      ModHours = ifelse(!is.na(ModDays) & !is.na(ModMin) & is.na(ModHours), 0, ModHours),
      ModMin = ifelse(!is.na(ModDays) & !is.na(ModHours) & is.na(ModMin), 0, ModMin),
      WalkHours = ifelse(!is.na(WalkDays) & !is.na(WalkMin) & is.na(WalkHours), 0, WalkHours),
      WalkMin = ifelse(!is.na(WalkDays) & !is.na(WalkHours) & is.na(WalkMin), 0, WalkMin),
      SitHours = ifelse(!is.na(SitMin) & is.na(SitHours), 0, SitHours),
      SitMin = ifelse(!is.na(SitHours) & is.na(SitMin), 0, SitMin),
      # Calculate Variables
      vminday = VigHours * 60 + VigMin,
      mminday = ModHours * 60 + ModMin,
      wminday = WalkHours * 60 + WalkMin,
      sminday = SitHours * 60 + SitMin
    ) %>%
    mutate(
      totalhourday = ifelse(is.na(vminday) + is.na(mminday) + is.na(wminday) + is.na(sminday) > 2,
                            NA, round(rowSums(select(., ends_with("minday")), na.rm = TRUE) / 60, 1)
      ),
      vminwk = VigDays * vminday,
      mminwk = ModDays * mminday,
      wminwk = WalkDays * wminday,
      sminwk = sminday * 7,
      sumpa = ifelse(is.na(vminday) + is.na(mminday) + is.na(wminday) > 2,
                     NA, rowSums(select(., matches("[vmw]minday")), na.rm = TRUE)
      ),
      sumday = ifelse(is.na(VigDays) + is.na(ModDays) + is.na(WalkDays) > 2,
                      NA, rowSums(select(., ends_with("Days")), na.rm = TRUE)
      ),
      excMin = ifelse(sumpa > 960, TRUE, FALSE),
      excDay = ifelse(sumday > 21, TRUE, FALSE),
      exc16h = ifelse(totalhourday > 16, TRUE, FALSE),
      vminday = ifelse(vminday < 10, 0, vminday),
      mminday = ifelse(mminday < 10, 0, mminday),
      wminday = ifelse(wminday < 10, 0, wminday),
      vminwkMET = 8 * vminwk,
      mminwkMET = 4 * mminwk,
      wminwkMET = 3.3 * wminwk,
      MET = vminwkMET + mminwkMET + wminwkMET,
      kilocalories = MET * (Weight / 60),
      pacat = case_when(
        VigDays >= 3 & MET >= 1500 ~ "High",
        sumday >= 7 & MET >= 3000 ~ "High",
        VigDays >= 3 & vminday >= 20 ~ "Moderate",
        ModDays + WalkDays >= 5 & mminday + wminday >= 30 ~ "Moderate",
        sumday >= 5 & MET >= 600 ~ "Moderate",
        is.na(VigDays) | is.na(vminday) | is.na(mminday) |
          is.na(wminday) | is.na(sumday) ~ as.character(NA),
        TRUE ~ "Low"
      ),
      pacat = factor(pacat, levels = c("High", "Moderate", "Low"))
    ) %>%
    select(-newvars) %>%
    mutate_at(vars(vminwkMET:kilocalories), list(~ifelse(excMin == TRUE | excDay == TRUE, NA, .))) %>%
    rename_at(vars(vminday:pacat), list(~paste0("ipaq.", .)))
  # Keep single items and nvalid variables
  if (keep == FALSE) {
    data <- data %>% select(-items)
  } else {
    data <- data
  }
  data
}
NULL
nrkoehler/qscorer documentation built on April 5, 2020, 3:09 a.m.