lmmresample

R-CMD-check Lifecycle: experimental

Resampling inference for mixed models of densely sampled physiological time series.

lmmresample provides permutation and bootstrap inference for models fitted to repeated measurements organised in blocks (trials, epochs or participants). Resampling always operates on whole blocks and refits a single model on the full data, so dependence within each block, such as the strong autocorrelation of samples within a trial, is carried into the reference distribution without having to be modelled. Where hundreds of autocorrelated samples per trial make standard Wald or Satterthwaite inference unreliable, this gives valid tests and intervals.

It applies to any block-structured signal, including pupillometry, eye movements, EEG, fNIRS, heart rate and HRV, electrodermal activity, EMG and kinematics, and at any level of aggregation: the full time series, features extracted per trial, or one value per participant. Supported models are lm() and lme4::lmer().

Status: under active development. The interface is not yet stable.

Features

Installation

Development version from GitHub:

# install.packages("pak")
pak::pak("ricardo-martinez-flores/lmmresample")

Example

A permutation test of a condition × time interaction in a mixed model of full time series, and bootstrap intervals for its coefficients:

library(lmmresample)
library(lme4)
library(splines)

d <- sim_blocks("within", level = "timeseries", n_participants = 20,
                n_trials = 20, n_time = 50, sampling_rate = 25,
                effect_condition = 0.6, effect_onset = 0.5, seed = 2)
m <- lmer(y ~ condition * ns(time, 4) + (1 | participant), data = d)

# Whole trials are permuted; samples within a trial are matched by time
perm_test(m, "condition:ns(time, 4)", unit = "trial",
          exchange = exch_signflip("participant"), time = "time")

# Whole participants are resampled
boot_ci(m, cluster = "participant",
        terms = grep("^conditionB:", names(fixef(m)), value = TRUE))

# Does the test hold its error rate for this design?
ac <- diag_acf(m, series = "trial", time = "time")
perm_calibrate(m, "condition:ns(time, 4)", unit = "trial",
               exchange = exch_signflip("participant"), ar1 = ac,
               series = "trial", time = "time")

Computation runs in parallel with future::plan("multisession").

Learning more

Authors

License

MIT © lmmresample authors