## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  # dplyr is a Suggests: the vignette reads without it and runs with it
  eval = requireNamespace("dplyr", quietly = TRUE)
)

## ----setup, message = FALSE---------------------------------------------------
library(rtprep)
library(dplyr)

## ----data---------------------------------------------------------------------
head(rt_example)

rt_example |>
  count(id, contam_rate, contaminant) |>
  filter(contaminant)

## ----screen-------------------------------------------------------------------
screened <- rt_example |>
  mutate(rt_screen(rt, rule_sd(2.5)), .by = c(id, condition))

head(screened)

## ----rules--------------------------------------------------------------------
rule_cutoff(0.18, 3)
rule_mad(2.5)
rule_recursive("modified")
rule_mixture("lognormal")

## ----keep---------------------------------------------------------------------
clean <- rt_example |>
  filter(rt_keep(rt, rule_sd(2.5), .by = list(id, condition)))

nrow(clean)

## ----fits---------------------------------------------------------------------
rt_example |>
  reframe(screen_fits(rt, rule_sd(2.5)), .by = c(id, condition)) |>
  select(id, condition, n_trials, n_dropped, prop_dropped, lower, upper)

## ----score--------------------------------------------------------------------
screened |>
  summarise(
    sensitivity = mean(!.keep[contaminant]),
    specificity = mean(.keep[!contaminant]),
    .by = id
  )

## ----score-process------------------------------------------------------------
screened |>
  filter(contaminant) |>
  summarise(found = mean(!.keep), n = n(), .by = process)

## ----estimate-----------------------------------------------------------------
estimates <- clean |>
  reframe(rt_summary(rt, response), .by = c(id, condition)) |>
  mutate(ez_ddm(mean_rt, var_rt, n_upper / n_trials, n_trials))

estimates |>
  select(id, condition, n_trials, drift, bound, ndt)

## ----error--------------------------------------------------------------------
truth <- rt_example |>
  distinct(id, condition, true_drift, contam_rate)

no_screen <- rt_example |>
  reframe(rt_summary(rt, response), .by = c(id, condition)) |>
  mutate(ez_ddm(mean_rt, var_rt, n_upper / n_trials, n_trials)) |>
  select(id, condition, drift_none = drift)

estimates |>
  select(id, condition, drift_sd = drift) |>
  left_join(no_screen, by = c("id", "condition")) |>
  left_join(truth, by = c("id", "condition")) |>
  mutate(
    error_none = drift_none - true_drift,
    error_sd = drift_sd - true_drift
  ) |>
  select(id, condition, contam_rate, true_drift, error_none, error_sd) |>
  arrange(contam_rate, condition)

## ----compare------------------------------------------------------------------
cmp <- screen_compare(
  rt_example$rt,
  list(
    cutoff = rule_cutoff(0.18, 3),
    sd = rule_sd(2.5),
    mad = rule_mad(2.5),
    recursive = rule_recursive("modified"),
    mixture = rule_mixture("lognormal")
  ),
  .by = list(rt_example$id, rt_example$condition)
)
cmp
cmp$agreement

## ----guessing-----------------------------------------------------------------
rt_example |>
  mutate(rt_screen(rt, rule_cutoff(0.35, 3)), .by = c(id, condition)) |>
  reframe(check_guessing(.keep, rt, response), .by = id) |>
  select(id, n_tested, prop_upper, bf_01, bf_evidence)

## ----reporting-fits-----------------------------------------------------------
rule <- rule_sd(2.5)
rule

fits <- rt_example |>
  reframe(screen_fits(rt, rule), .by = c(id, condition))

fits |>
  summarise(
    cells = n(),
    trials = sum(n_trials),
    dropped = sum(n_dropped),
    prop = sum(n_dropped) / sum(n_trials),
    lowest_cell = min(prop_dropped),
    highest_cell = max(prop_dropped)
  )

## ----reporting-reasons--------------------------------------------------------
rt_example |>
  mutate(rt_screen(rt, rule), .by = c(id, condition)) |>
  count(.rule, .reason)

## ----reporting-paragraph------------------------------------------------------
scr <- rt_screen(
  rt_example$rt, rule,
  .by = list(participant = rt_example$id, condition = rt_example$condition)
)

report_screening(scr)

## ----reporting-numbers--------------------------------------------------------
rep <- report_screening(scr)
c(excluded = rep$n_excluded, screened = rep$n_screened, missing = rep$n_missing)
rep$cells

