Investigation: Data quality and cleaning#
Cleaning is a sequence of accountable decisions about measurement. Formatting errors, missingness, implausible values, and exclusions need different responses. Preserve raw values and explain how each transformation affects the population or estimand.
Question. Introduce commas, missing values, and a negative reaction time into simulated trials. Parse values, retain an exclusion ledger, and compare two defensible inclusion rules.
Before running: write the result you expect and what would challenge it. Data are simulated unless explicitly identified otherwise.
# Open this notebook from book/notebooks (or book/exercises).
course_path <- if (file.exists("../R/course.R")) "../R/course.R" else "book/R/course.R"
stopifnot(file.exists(course_path))
source(course_path)
set.seed(432)
raw <- simulate_trials()
raw$rt_raw <- format(round(raw$rt),big.mark=",",trim=TRUE)
raw$rt_raw[c(2,7)] <- NA
raw$rt_raw[12] <- "-250"
clean <- raw
clean$rt_ms <- as.numeric(gsub(",","",clean$rt_raw))
clean$reason <- ifelse(is.na(clean$rt_ms),"missing",ifelse(clean$rt_ms<=0,"nonpositive","retained"))
ledger <- clean[c("trial_id","subject","rt_raw","rt_ms","reason")]
table(ledger$reason)
head(ledger,12)
missing nonpositive retained
2 1 1077
| trial_id | subject | rt_raw | rt_ms | reason | |
|---|---|---|---|---|---|
| <int> | <fct> | <chr> | <dbl> | <chr> | |
| 1 | 1 | 1 | 647 | 647 | retained |
| 2 | 2 | 24 | NA | NA | missing |
| 3 | 3 | 17 | 717 | 717 | retained |
| 4 | 4 | 35 | 592 | 592 | retained |
| 5 | 5 | 23 | 735 | 735 | retained |
| 6 | 6 | 16 | 610 | 610 | retained |
| 7 | 7 | 27 | NA | NA | missing |
| 8 | 8 | 34 | 575 | 575 | retained |
| 9 | 9 | 9 | 721 | 721 | retained |
| 10 | 10 | 4 | 778 | 778 | retained |
| 11 | 11 | 2 | 782 | 782 | retained |
| 12 | 12 | 8 | -250 | -250 | nonpositive |
Make exclusions visible#
For this simulated example, missing and nonpositive values cannot support the measurement. In real data, investigate the encoding before excluding or correcting. Do not silently replace a negative value with its absolute value.
primary <- subset(clean,reason=="retained")
sensitivity <- subset(primary,rt_ms>=300 & rt_ms<=1500)
results <- data.frame(rule=c("positive observed RT","300–1500 ms"), n=c(nrow(primary),nrow(sensitivity)), slope=c(coef(lm(rt_ms~frequency+length,primary))[2],coef(lm(rt_ms~frequency+length,sensitivity))[2]))
results
stopifnot(nrow(primary)+sum(clean$reason!="retained")==nrow(raw))
| rule | n | slope |
|---|---|---|
| <chr> | <int> | <dbl> |
| positive observed RT | 1077 | -27.68604 |
| 300–1500 ms | 1077 | -27.68604 |
Missingness and learned transformations#
Complete-case analysis changes the analyzed population unless its assumptions justify the target. Describe why values are missing; consider sensitivity analysis or an appropriate imputation model. In prediction, fit imputers and scalers inside training folds. Useful modern R verbs include mutate, summarise, filter, pivot_longer, pivot_wider, and joins; the base |> pipe and tidyverse %>% pipe are both available, with different semantics.
Interpret and verify#
Write a cleaning specification before coding; report retained and excluded counts by reason and compare the main result under two rules.
Failure to consider: Taking an absolute value makes a negative reaction time positive without establishing why it was negative. Dropping missing rows can also change the target population.
Report the target, the numerical result with its units, the relevant uncertainty, and one limitation. Distinguish what is established by the simulation from what would require real data.
AI audit: critique the saved faulty claim in the companion exercise before using any live assistant. If you use AI, record the relevant suggestion, your independent check, and the final decision.
Apply the schema and key checks from data architecture; carry the cleaned data and ledger into visualization.
sessionInfo()
R version 4.2.1 (2022-06-23)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS 15.7.7
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib
locale:
[1] C.UTF-8/C.UTF-8/C.UTF-8/C/C.UTF-8/C.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] fansi_1.0.3 crayon_1.5.1 digest_0.6.29
[4] utf8_1.2.2 IRdisplay_1.1 repr_1.1.4
[7] lifecycle_1.0.1 jsonlite_1.8.0 evaluate_0.15
[10] pillar_1.8.0 rlang_1.0.6 cli_3.3.0
[13] uuid_1.1-0 vctrs_0.4.1 IRkernel_1.3.0.9000
[16] tools_4.2.1 glue_1.6.2 fastmap_1.1.0
[19] compiler_4.2.1 base64enc_0.1-3 pbdZMQ_0.3-7
[22] htmltools_0.5.3
Attribution#
Notebook authored by Ven Popov and edited by Krista Bond, Charles Wu, Patience Stevens, Amy Sentis, and Fiona Horner.
Revised investigation: 2026 course revision. Original authorship is retained; examples and explanatory text have been updated.