R/reg_de.R

Defines functions scoring_reg

Documented in scoring_reg

#' @title {Scoring the Skala zur Selbstregulation (REG) (engl. Self-regulation scale)}
#' @description {
#' The REG is a 10-item Likert-type measure of the ability of self-regulation.
#' Items are rated on a scale of 1 (completely disagree) to 4 (completely agree).
#' A total sum score ranging from 10 to 40 may be calculated.
#' }
#' @details
#' \itemize{
#' \item \code{Number of items:} {10}
#' \item \code{Item range:} {1 to 4}
#' \item \code{Reverse items:} {none}
#' \item \code{Score range:} {10 to 40}
#' \item \code{Cut-off-values:} {none}
#' \item \code{Minimal clinically important difference:} {none}
#' \item \code{Treatment of missing values:}
#' {The summary score is calculated as long as at least 8 questions have been answered.}
#' }
#' @references
#' \itemize{
#'  \item Link to Questionnaire (\url{https://www.usucbs.com/uploads/5/1/3/4/51340265/wssq__weight_self_stigma_.docx})
#'  \item Schwarzer, Jerusalem 1999 (\url{http://www.psyc.de/skalendoku.pdf})
#' }
#' @return The function returns 2 variables:
#' \itemize{
#'  \item \code{nvalid.reg:} Number of valid values (MAX=10)
#'  \item \code{score.reg:} Summary Score
#' }
#' @examples
#' \dontrun{
#' library(dplyr)
#' items.reg <- paste0("REG_", seq(1, 10, 1))
#' scoring_reg(mydata, items = items.reg)
#' }
#' @param data a \code{\link{data.frame}} containing the REG items
#' orderd from 1 to 10
#' @param items A character vector with the REG item names ordered from 1 to 10,
#' or a numeric vector indicating the column numbers of the REG 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 nvalid A numeric value indicating the number of non-missing items required for score
#' calculations. The default is 8.
#' @param digits Integer of length one: value to round to. No rounding by default.
#' @export
scoring_reg <- function(data, items = 1:10, keep = TRUE, nvalid = 8, digits = NULL) {
  library(dplyr, warn.conflicts = FALSE)
  if (min(data[, items], na.rm = T) < 1) {
    stop("Minimum possible value for REG items is 1")
  } else if (max(data[, items], na.rm = T) > 4) {
    stop("Maximum possible value for REG items is 4")
  }
  # check for number of specified items
  if (length(items) != 10) {
    stop("Number of items must be 10!")
  }
  items <- items
  data <- data %>%
    mutate(
      nvalid.reg = rowSums(!is.na(select(., items))),
      mean.temp = round(rowSums(select(., items), na.rm = TRUE) / nvalid.reg)
    ) %>%
    mutate_at(
      vars(items), list(~ ifelse(is.na(.), mean.temp, .))
    ) %>%
    mutate(
      score.temp = rowSums(select(., items), na.rm = TRUE),
      score.reg = ifelse(nvalid.reg >= nvalid, score.temp, NA)
    ) %>%
    select(-mean.temp, -score.temp)
  # Keep single items and nvalid variables
  if (keep == FALSE) {
    data <- data %>% select(-items, -nvalid.reg)
  } else {
    data <- data
  }
  # Rounding
  if (is.numeric(digits) == TRUE) {
    data <- data %>% mutate_at(vars(score.reg), list(~ round(., digits)))
  } else {
    data <- data
  }
  data
}
NULL
nrkoehler/qscorer documentation built on April 5, 2020, 3:09 a.m.