Investigation: Data as objects and architectures

Investigation: Data as objects and architectures#

Data structure encodes the unit of observation. Tidy tables put variables in columns and observations in rows, but the observational unit must be defined first. Long and wide are shapes, not automatic guarantees of tidiness or correctness.

Question. Separate trial, participant, and item tables; join item attributes to trials and detect a duplicate key that multiplies rows.

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()
trials <- d[c("trial_id","subject","item","rt","correct")]
items <- unique(d[c("item","length","frequency")])
stopifnot(!anyDuplicated(items$item), !anyDuplicated(trials$trial_id))
joined <- merge(trials,items,by="item",all.x=TRUE,sort=FALSE)
stopifnot(nrow(joined)==nrow(trials), !anyNA(joined$frequency))
head(joined)
A data.frame: 6 × 7
itemtrial_idsubjectrtcorrectlengthfrequency
<fct><int><fct><dbl><int><int><dbl>
1111 646.9188181.624019
21224606.2979181.624019
31317717.1454181.624019
41435591.9779181.624019
51523734.8015181.624019
61616610.3224081.624019

A join that runs but changes the sample#

The item table must have one row per item. Duplicating one key changes the number of trials after a join. Always state expected cardinality before merging.

bad_items <- rbind(items,items[1,])
bad_join <- merge(trials,bad_items,by="item",all.x=TRUE)
c(original=nrow(trials), valid_join=nrow(joined), duplicated_join=nrow(bad_join))
stopifnot(anyDuplicated(bad_items$item)>0, nrow(bad_join)>nrow(trials))
schema <- data.frame(variable=c("trial_id","subject","item","rt","correct"),meaning=c("unique trial key","participant key","word key","reaction time in ms","1=correct, 0=incorrect"))
schema
original
1080
valid_join
1080
duplicated_join
1116
A data.frame: 5 × 2
variablemeaning
<chr><chr>
trial_idunique trial key
subject participant key
item word key
rt reaction time in ms
correct 1=correct, 0=incorrect

Transfer to real data#

The bundled English Lexicon Project files under book/exercises/lexDat can replace the simulation after their data dictionary, licensing, keys, and missing-value encodings are checked. BIDS-style organization is useful for neuroimaging: separate raw and derived data and describe transformations. A file layout does not validate its contents.

Interpret and verify#

Document keys, units, missing-value codes, and table relationships. Demonstrate a key check and a row-count check.

Failure to consider: A join that silently duplicates trials changes the weighting of observations and can create false precision.

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.

The hypothesis determines the observational unit; data-quality checks protect that unit during transformation.

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.