fitgrid.models module

User modeling functions and developer utilities for populating Time x Channel grids with OLS and LMER model fits. The API exposes the primary user interface functions for general use with Epochs objects as fitgrid.lm and fitgrid.lmer.

fitgrid.models.lm(epochs, LHS=None, RHS=None, parallel=False, n_cores=4, quiet=False, eval_env=4)[source]

Run ordinary least squares linear regression on the epochs.

Parameters
  • epochs (Epochs) – epochs object on which regression is to be run

  • LHS (list of str, optional, defaults to all channels) – list of channels for the left hand side of the regression formula

  • RHS (str) – right hand side of the regression formula

  • parallel (bool, defaults to False) – change to True to run in parallel

  • n_cores (int, defaults to 4) – number of processes to use for computation

  • quiet (bool, defaults to False) – set to True to disable fitting progress bar

  • eval_env (int or patsy.EvalEnvironment, defaults to 4) – environment to use for evaluating patsy formulas, see patsy docs

Returns

grid – LMFitGrid object containing the results of the regression

Return type

LMFitGrid

fitgrid.models.lm_single(data, channel, RHS, eval_env)[source]
fitgrid.models.lmer(epochs, LHS=None, RHS=None, family='gaussian', conf_int='Wald', factors=None, permute=None, ordered=False, REML=True, parallel=False, n_cores=4, quiet=False)[source]

Fit lme4 linear mixed model by interfacing with R.

Parameters
  • epochs (Epochs) – epochs object on which lmer is to be run

  • LHS (list of str, optional, defaults to all channels) – list of channels for the left hand side of the lmer formula

  • RHS (str) – right hand side of the lmer formula

  • family (str, defaults to ‘gaussian’) – distribution link function to use

  • conf_int (str, defaults to ‘Wald’)

  • factors (dict, optional) – Keys should be column names in data to treat as factors. Values should either be a list containing unique variable levels if dummy-coding or polynomial coding is desired. Otherwise values should themselves be dictionaries with unique variable levels as keys and desired contrast values (as specified in R!) as keys.

  • permute (int, defaults to None) – if non-zero, computes parameter significance tests by permuting test stastics rather than parametrically. Permutation is done by shuffling observations within clusters to respect random effects structure of data.

  • ordered (bool, defaults to False) – whether factors should be treated as ordered polynomial contrasts; this will parameterize a model with K-1 orthogonal polynomial regressors beginning with a linear contrast based on the factor order provided

  • REML (bool, defaults to True) – change to False to use ML estimation

  • parallel (bool, defaults to False) – change to True to run in parallel

  • n_cores (int, defaults to 4) – number of processes to use for computation

  • quiet (bool, defaults to False) – set to True to disable fitting progress bar

Returns

grid – LMERFitGrid object containing the results of lmer fitting

Return type

LMERFitGrid

fitgrid.models.lmer_single(data, channel, RHS, family, conf_int, factors, permute, ordered, REML)[source]
fitgrid.models.process_key_and_group(key_and_group, function, channels)[source]
fitgrid.models.run_model(epochs, function, channels=None, parallel=False, n_cores=4, quiet=False)[source]

Run an arbitrary model on the epochs.

Parameters
  • epochs (Epochs) – the epochs object on which the model is to be run

  • function (Python function) – function that runs a model, see Notes below for details

  • channels (list of str) – list of channels to serve as dependent variables

  • parallel (bool, defaults to False) – set to True in order to run in parallel

  • n_cores (int, defaults to 4) – number of processes to run in parallel

  • quiet (bool, defaults to False) – set to True to disable progress bar display

Returns

grid – a FitGrid object containing the results

Return type

FitGrid

Notes

The function should take two parameters, data and channel, run some model on the data, and return an object containing the results. data will be a snapshot across epochs at a single timepoint, containing all channels of interest. channel is the name of the target variable that the function runs the model against (uses it as the dependent variable).

Examples

Here’s an example of a function that can be passed to run_model:

def regression(data, channel):
    formula = channel + ' ~ continuous + categorical'
    return ols(formula, data).fit()
fitgrid.models.validate_LHS(epochs, LHS)[source]
fitgrid.models.validate_RHS(RHS)[source]