Skip to content

Commit 2d6c85b

Browse files
committed
Built site for ModelArray@0.1.5: bc8e8a5
1 parent 3d07ba6 commit 2d6c85b

18 files changed

Lines changed: 519 additions & 147 deletions

articles/modelling.html

Lines changed: 74 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

articles/modelling.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,80 @@ head(result)
288288
writeResults(h5_path, df.output = result, analysis_name = "site_analysis")
289289
```
290290

291+
### Case study: harmonize with `covfam` and stream to a new scalar
292+
293+
You can also use
294+
[`ModelArray.wrap()`](https://pennlinc.github.io/ModelArray/reference/ModelArray.wrap.md)
295+
as a transformation engine rather than a test runner. The example below
296+
applies a harmonization step per element, streams the harmonized values
297+
directly into `/scalars`, and then loads that harmonized scalar in a new
298+
`ModelArray`.
299+
300+
``` r
301+
library(covfam)
302+
303+
# Assume phenotypes has harmonization variables, e.g.:
304+
# source_file, site, age, sex
305+
# and that "thickness" is already present in the h5 as an input scalar.
306+
307+
h5_in <- "thickness_raw.h5"
308+
h5_harmonized <- "thickness_harmonized.h5"
309+
310+
ma_raw <- ModelArray(h5_in, scalar_types = "thickness")
311+
phenotypes <- read.csv("phenotypes.csv")
312+
313+
# Copy metadata/layout into a new file so we can append new scalars there.
314+
file.copy(h5_in, h5_harmonized, overwrite = TRUE)
315+
316+
covfam_harmonize_element <- function(data) {
317+
# Harmonize one element across subjects.
318+
# Adjust arguments here to match your covfam configuration.
319+
out <- covfam::covfam(
320+
y = data$thickness,
321+
batch = data$site,
322+
mod = data.frame(age = data$age, sex = data$sex)
323+
)
324+
325+
# Return named subject-level vector so wrap columns map to source_file order.
326+
vals <- out$y_harmonized
327+
names(vals) <- data$source_file
328+
vals
329+
}
330+
331+
# Stream harmonized subject-level values into scalars/thickness_covfam/values.
332+
# Set return_output = FALSE to avoid keeping the full output table in memory.
333+
ModelArray.wrap(
334+
FUN = covfam_harmonize_element,
335+
data = ma_raw,
336+
phenotypes = phenotypes,
337+
scalar = "thickness",
338+
n_cores = 8,
339+
write_scalar_name = "thickness_covfam",
340+
write_scalar_file = h5_harmonized,
341+
write_scalar_flush_every = 2000L,
342+
return_output = FALSE
343+
)
344+
345+
# Load the harmonized scalar as a new modelling input
346+
ma_harmonized <- ModelArray(h5_harmonized, scalar_types = c("thickness_covfam"))
347+
348+
# Use harmonized data in downstream models
349+
fit_harmonized <- ModelArray.lm(
350+
thickness_covfam ~ age + sex,
351+
data = ma_harmonized,
352+
phenotypes = phenotypes,
353+
scalar = "thickness_covfam",
354+
n_cores = 8
355+
)
356+
```
357+
358+
If you want to stream model statistics too (not only transformed
359+
scalars), use `write_results_name` and `write_results_file` in
360+
[`ModelArray.lm()`](https://pennlinc.github.io/ModelArray/reference/ModelArray.lm.md),
361+
[`ModelArray.gam()`](https://pennlinc.github.io/ModelArray/reference/ModelArray.gam.md),
362+
or
363+
[`ModelArray.wrap()`](https://pennlinc.github.io/ModelArray/reference/ModelArray.wrap.md).
364+
291365
## Modelling across multiple h5 files with `mergeModelArrays()`
292366

293367
When scalars live in separate h5 files — for example, cortical thickness

llms.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ If you use ModelArray, please cite:
124124
## All functions
125125

126126
- [`ModelArray()`](https://pennlinc.github.io/ModelArray/reference/ModelArray.md)
127-
: Construct a ModelArray object
127+
: An S4 class to represent element-wise scalar data and statistics.
128128
- [`ModelArray.gam()`](https://pennlinc.github.io/ModelArray/reference/ModelArray.gam.md)
129129
: Run GAM for element-wise data
130130
- [`ModelArray.lm()`](https://pennlinc.github.io/ModelArray/reference/ModelArray.lm.md)

pkgdown.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ articles:
1212
installations: installations.html
1313
modelling: modelling.html
1414
walkthrough: walkthrough.html
15-
last_built: 2026-03-27T14:04Z
15+
last_built: 2026-03-27T17:03Z
1616
urls:
1717
reference: https://pennlinc.github.io/ModelArray/reference
1818
article: https://pennlinc.github.io/ModelArray/articles

0 commit comments

Comments
 (0)