R/datasim.R

Defines functions simulate_items

Documented in simulate_items

#' @title {Simulate questionnaire items}
#' @description {
#' The function simulates a \code{\link{data.frame}} including an id variable and the items of any questionnaire.
#' }
#' @return The function returns the following variables:
#' \itemize{
#'  \item \code{id:} {Variable with identification numbers}
#'  \item \code{item_1 to item_n} {Questionnaire items}
#' }
#' @examples
#' \dontrun{
#' # Simulate Hospital Anxiety and Depression Scale
#' library(dplyr)
#' library(wakefield)
#' df <- simulate_items(num_cols = 14, num_rows = 100, item_name = "hads", item_range = 0:3, prop_mis = 0.05)
#' }
#' @param num_cols Number of items (default = 10)
#' @param num_rows Number of rows (default = 100)
#' @param item_name Abbreviated name of questionnaire (default = 'item')
#' @param item_range Item Range (is the same for each item; default = 0 to 3)
#' @param prop_mis Proportion of missing values to be put into each item (default = 0.05)
#' @export
simulate_items <- function(num_cols = 10, num_rows = 100,
                           item_name = "item", item_range = 0:3,
                           prop_mis = 0.05) {
  library(wakefield)
  library(dplyr)
  set.seed(123)
  data <- data.frame(matrix(NA,
                            nrow = num_rows, ncol = num_cols,
                            dimnames = list(NULL, paste(item_name, 1:num_cols, sep = "_"))
  ))
  data <- data %>%
    mutate_all(list(~ sample(x = item_range, size = num_rows, replace = TRUE))) %>%
    r_na(., cols = 1:num_cols, prob = prop_mis) %>%
    mutate(
      id = wakefield::id(n = num_rows, name = "id")
    ) %>%
    select(id, everything())
}
NULL
nrkoehler/qscorer documentation built on April 5, 2020, 3:09 a.m.