R/control.R

Defines functions control

Documented in control

#' Calculate raw score for control-required games
#'
#' This function calculates raw scores for two types of test games. The first
#' type has two types of stimuli: \emph{Congruent} and \emph{Incongruent},
#' whereas the second type has three types of stimuli: \emph{Filler},
#' \emph{Repeat}, \emph{Switch}.
#'
#' @param data Raw data of class \code{data.frame}.
#' @param ... Other input argument for future expansion.
#' @return The raw score calculated of \code{double} type.
#' @importFrom magrittr %>%
#' @importFrom rlang .data
#' @export
control <- function(data, ...) {
  key_types <- c("Switch", "Incongruent")
  if (!utils::hasName(data, "Type") || !any(key_types %in% data$Type)) {
    return(NA_real_)
  }
  if (!utils::hasName(data, "ACC")) {
    warning("Accuracy (ACC) variable is required.")
    return(NA_real_)
  }
  if (mean(data$ACC == -1) >= 0.2) {
    warning("Response rate is too low.")
    return(NA_real_)
  }
  # correctness score
  Sc <- mean(data$ACC == 1)
  # speediness score
  Ss <- 0
  if (Sc > 0.8) {
    data_speed <- data %>%
      dplyr::filter(.data$Type %in% key_types, .data$ACC == 1)
    if (nrow(data_speed) > 0) {
      Ss <- data_speed %>%
        dplyr::mutate(
          RT_mod = ifelse(
            .data$RT < 100 |
              .data$RT > mean(.data$RT) + 3 * stats::sd(.data$RT),
            NA_integer_, .data$RT
          )
        ) %>%
        dplyr::summarise(
          Med = as.numeric(stats::median(.data$RT_mod, na.rm = TRUE)),
          Med = dplyr::case_when(
            .data$Med > 3000 ~ 3000,
            .data$Med < 500 ~ 500,
            TRUE ~ .data$Med
          )
        ) %>%
        dplyr::mutate(
          Ss = (log10(3000) - log10(.data$Med)) / (log10(3000) - log10(500))
        ) %>%
        dplyr::pull("Ss")
    }
  }
  Sc + Ss
}
psychelzh/dataprocr documentation built on Oct. 12, 2019, 1:50 a.m.