Investigation: Errors and inferences#

A defensible analysis actively probes mistakes that would undermine its claim. A check is useful when it has a good chance of exposing the error of concern. Passing some checks does not prove that all important errors are absent.

Question. Audit a plausible result with duplicated trial rows, a misleading independent-observation assumption, and an unsupported causal summary.

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)
d <- simulate_trials()
# Intentionally flawed input for the audit: duplicate one participant's trials.
flawed <- rbind(d,d[d$subject==levels(d$subject)[1],])
audit <- c(unique_trial_keys=!anyDuplicated(flawed$trial_id),positive_rt=all(flawed$rt>0),expected_row_count=nrow(flawed)==nrow(d))
audit
stopifnot(!audit["unique_trial_keys"],!audit["expected_row_count"])
c(original_mean=mean(d$rt),duplicated_mean=mean(flawed$rt))
unique_trial_keys
FALSE
positive_rt
TRUE
expected_row_count
FALSE
original_mean
633.692601761904
duplicated_mean
632.835297274999

Repair only after identifying the cause#

Here the source of duplication is known from the injected error. In real data, investigate whether repeated keys represent duplicate records or a misdefined key before removing them. A uniqueness check does not detect biased sampling or an invalid construct.

repaired <- flawed[!duplicated(flawed$trial_id),]
stopifnot(isTRUE(all.equal(repaired,d,check.attributes=FALSE)))
row_se <- sd(repaired$rt)/sqrt(nrow(repaired))
subject_means <- aggregate(rt~subject,repaired,mean)$rt
subject_se <- sd(subject_means)/sqrt(length(subject_means))
c(naive_trial_se=row_se,subject_level_se=subject_se)
naive_trial_se
2.92007066056715
subject_level_se
9.63539041472529

Saved draft to audit#

“The script ran without errors and the frequency coefficient was significant, proving that increasing word frequency causes faster responses in all people.”

Identify four distinct claims in that sentence. Map each to evidence, missing evidence, or a contradiction. Use simulation, schema checks, residual analysis, and independent reproduction for the failures they can actually detect. An AI assistant may help list checks, but you must explain their sensitivity and limits.

Interpret and verify#

Write the audit plan before repairing the analysis. For each check, state the failure it can detect and an important failure it cannot.

Failure to consider: An analysis can execute without errors and still answer the wrong question. Agreement between two AI outputs is not an independent verification.

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.

Integrate data architecture, modeling, validation, and uncertainty before communicating a conclusion.

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