phit

Mixed-model functions

Gaussian and binomial mixed models. See Mixed models for the supported structures and Model contracts for result keys and algorithms.

Formula models

fit_mixed_gaussian()

Fits a Gaussian mixed model from an lme4 formula, like lme4::lmer().

  • Fixed terms are as in fit_ols(). Random terms can be (x | g), (x || g), (1 | g1) + (1 | g2), (1 | g1:g2), and (1 | g1/g2).
  • Fits by REML unless reml is false.
  • Groups and effects follow first appearance after missing rows are dropped, not lme4’s sorted levels; terms keep formula order.
  • Each random term’s effects_cond_var holds one k × k conditional covariance per group, as ranef(condVar = TRUE).
  • Rows with null or NAN in a variable the formula uses are dropped with one warning; missing: 'raise' rejects them instead. retained_rows and dropped_rows give the original row positions.
Phit\fit_mixed_gaussian(    string $formula,    array|object $data,    bool $reml = true,    string $missing = 'drop',    array|string|null $weights = NULL,    ?array $start = NULL,    int $maxEvaluations = 20000,    float $tolerance = 1.0E-7,    string $optimizer = 'bobyqa',    float $objectiveTolerance = 1.0E-12,): MixedGaussianFit
Parameter Type Default Description
$formula string required R formula, such as 'y ~ log(x) + f * z + offset(log(t))'
$data array\|object required Named columns or factor descriptors; objects use toModelData(), else toColumns()
$reml bool true true for REML, false for ML
$missing string 'drop' 'drop' removes rows with null or NAN in any variable the model uses and warns once; 'raise' rejects them
$weights array\|string\|null NULL Positive prior weights aligned with the original rows, or a column name
$start ?array NULL Starting relative covariance factors, in the units of the random columns; null starts diagonal factors at one and off-diagonals at zero
$maxEvaluations int 20000 Largest number of optimizer evaluations
$tolerance float 1.0E-7 BOBYQA final trust-region radius, in (0, 0.1]
$optimizer string 'bobyqa' 'bobyqa' or 'nelder_mead'
$objectiveTolerance float 1.0E-12 Nelder–Mead stopping tolerance: the standard deviation of the simplex objective values

Returns

MixedGaussianFit A fit object, read like this array:array{  model: 'linear'|'generalized_linear'|'mixed_gaussian'|'mixed_logistic',  coefficients: list<float>,  covariance: list<list<float>>,  std_errors: list<float>,  p_values: list<float>,  conf_int: list<array{float,float}>,  fitted: list<float>,  residuals: list<float>,  log_likelihood: float,  df_residual: int,  diagnostics: array{converged:bool,solver:string,warnings:list<string>,iterations?:int,evaluations?:int,gradient_max?:float,boundary?:bool,singular?:bool},  n_input: int,  n_observations: int,  n_dropped: int,  retained_rows: list<int>,  dropped_rows: list<int>,  z_statistics: list<float>,  fixed_fitted: list<float>,  marginal_residuals: list<float>,  aic: float,  residual_variance: float,  sigma: float,  method: string,  converged: bool,  n_parameters: int,  bic: float,  theta: list<float>,  singular: bool,  optimizer: string,  optimizer_implementation: string,  evaluations: int,  random: list<array{covariance:list<list<float>>,effects:list<list<float>>,effects_cond_var:list<list<list<float>>>,group_labels:list<int|string>,group_sizes:list<int>,label:string,group:string,columns:list<string>,correlated:bool}>,  formula: string,  response: string,  columns: list<string>,  terms: list<string>,  assign: list<int>,  intercept: bool,  named: array<string,float>,  predvars: array{levels:array<string,list<string>>,poly:array<string,array{alpha:list<float>,norm2:list<float>}>},}

Errors Exception: a code from 1001 to 1008 for an input or statistical error; 1007 for an unsupported function, family, or random term.

See also Details · Example

fit_mixed_logistic()

Fits a binomial logit model with one random intercept from a formula, like lme4::glmer(family = binomial).

  • The formula has exactly one (1 | g) term. The model is binomial with the logit link only, so the function takes no family or link.
  • Uses the Laplace approximation by default, and adaptive quadrature when quadraturePoints is above 1.
  • Responses are as in fit_generalized_linear().
  • random_effects_cond_var holds the variance of each conditional mode, as ranef(condVar = TRUE).
  • Rows with null or NAN in a variable the formula uses are dropped with one warning; missing: 'raise' rejects them instead. retained_rows and dropped_rows give the original row positions.
Phit\fit_mixed_logistic(    string $formula,    array|object $data,    string $missing = 'drop',    array|string|null $weights = NULL,    int $quadraturePoints = 1,    int $maxEvaluations = 20000,    float $tolerance = 1.0E-7,): MixedLogisticFit
Parameter Type Default Description
$formula string required R formula, such as 'y ~ log(x) + f * z + offset(log(t))'
$data array\|object required Named columns or factor descriptors; objects use toModelData(), else toColumns()
$missing string 'drop' 'drop' removes rows with null or NAN in any variable the model uses and warns once; 'raise' rejects them
$weights array\|string\|null NULL Positive prior weights aligned with the original rows, or a column name
$quadraturePoints int 1 1 for the Laplace approximation, or 2 to 61 for adaptive Gauss–Hermite quadrature
$maxEvaluations int 20000 Largest number of optimizer evaluations
$tolerance float 1.0E-7 BOBYQA final trust-region radius, in (0, 0.1]

Returns

MixedLogisticFit A fit object, read like this array:array{  model: 'linear'|'generalized_linear'|'mixed_gaussian'|'mixed_logistic',  coefficients: list<float>,  covariance: list<list<float>>,  std_errors: list<float>,  p_values: list<float>,  conf_int: list<array{float,float}>,  fitted: list<float>,  residuals: list<float>,  log_likelihood: float,  df_residual: int,  diagnostics: array{converged:bool,solver:string,warnings:list<string>,iterations?:int,evaluations?:int,gradient_max?:float,boundary?:bool,singular?:bool},  n_input: int,  n_observations: int,  n_dropped: int,  retained_rows: list<int>,  dropped_rows: list<int>,  z_statistics: list<float>,  fixed_fitted: list<float>,  linear_predictor: list<float>,  aic: float,  bic: float,  n_parameters: int,  group_labels: list<int|string>,  group_sizes: list<int>,  random_effects: list<float>,  random_effects_cond_var: list<float>,  n_groups: int,  random_intercept_sd: float,  random_intercept_variance: float,  neg2_log_likelihood: float,  quadrature_points: int,  integration: string,  method: string,  family: string,  link: string,  optimizer: string,  converged: bool,  evaluations: int,  gradient_max: float,  boundary: bool,  singular: bool,  random: list<array{label:string,group:string,columns:list<string>}>,  formula: string,  response: string,  columns: list<string>,  terms: list<string>,  assign: list<int>,  intercept: bool,  named: array<string,float>,  predvars: array{levels:array<string,list<string>>,poly:array<string,array{alpha:list<float>,norm2:list<float>}>},}

Errors Exception: a code from 1001 to 1008 for an input or statistical error; 1007 for an unsupported function, family, or random term.

See also Details

Matrix models

fit_mixed_gaussian_matrix()

Fits a Gaussian mixed model on a numeric design with explicit random terms, like lme4::lmer().

  • Fits by ML unless reml is true.
  • An intercept column is prepended unless intercept is false, in which case x is used exactly.
  • Rows with null or NAN in x, y, or another aligned input are dropped with one warning; missing: 'raise' rejects them instead. retained_rows and dropped_rows give the original row positions.
Phit\fit_mixed_gaussian_matrix(    array $x,    array $y,    array $random,    string $missing = 'drop',    bool $reml = false,    ?array $weights = NULL,    ?array $offset = NULL,    ?array $start = NULL,    int $maxEvaluations = 20000,    float $tolerance = 1.0E-7,    string $optimizer = 'bobyqa',    float $objectiveTolerance = 1.0E-12,    bool $intercept = true,): MixedGaussianFit
Parameter Type Default Description
$x array required Predictor matrix: one row per observation and one numeric column per predictor; null or NAN is missing
$y array required Numeric response, one value per row; null or NAN is missing
$random array required Random terms, each an array with 'z' (n × k design), 'groups' (one label per row), and optional 'correlated' (default true; false is (x || g))
$missing string 'drop' 'drop' removes rows with null or NAN in any variable the model uses and warns once; 'raise' rejects them
$reml bool false true for REML, false for ML
$weights ?array NULL Positive prior weights aligned with the rows
$offset ?array NULL Known part of the linear predictor, aligned with the rows; null is zero
$start ?array NULL Starting relative covariance factors, in the units of the random columns; null starts diagonal factors at one and off-diagonals at zero
$maxEvaluations int 20000 Largest number of optimizer evaluations
$tolerance float 1.0E-7 BOBYQA final trust-region radius, in (0, 0.1]
$optimizer string 'bobyqa' 'bobyqa' or 'nelder_mead'
$objectiveTolerance float 1.0E-12 Nelder–Mead stopping tolerance: the standard deviation of the simplex objective values
$intercept bool true False uses x exactly (including any caller-supplied ones).

Returns

MixedGaussianFit A fit object, read like this array:array{  model: 'linear'|'generalized_linear'|'mixed_gaussian'|'mixed_logistic',  coefficients: list<float>,  covariance: list<list<float>>,  std_errors: list<float>,  p_values: list<float>,  conf_int: list<array{float,float}>,  fitted: list<float>,  residuals: list<float>,  log_likelihood: float,  df_residual: int,  diagnostics: array{converged:bool,solver:string,warnings:list<string>,iterations?:int,evaluations?:int,gradient_max?:float,boundary?:bool,singular?:bool},  n_input: int,  n_observations: int,  n_dropped: int,  retained_rows: list<int>,  dropped_rows: list<int>,  z_statistics: list<float>,  fixed_fitted: list<float>,  marginal_residuals: list<float>,  aic: float,  residual_variance: float,  sigma: float,  method: string,  converged: bool,  n_parameters: int,  bic: float,  theta: list<float>,  singular: bool,  optimizer: string,  optimizer_implementation: string,  evaluations: int,  random: list<array{covariance:list<list<float>>,effects:list<list<float>>,effects_cond_var:list<list<list<float>>>,group_labels:list<int|string>,group_sizes:list<int>}>,}

Errors Exception: a code from 1001 to 1008 for an input or statistical error.

See also Details

fit_mixed_gaussian_intercept_matrix()

Fits a Gaussian model with one random intercept by ML on a numeric design.

  • Has no random slopes, additional grouping terms, offsets, weights, or REML; use fit_mixed_gaussian_matrix() for those.
  • An intercept column is prepended unless intercept is false, in which case x is used exactly.
  • Rows with null or NAN in x, y, or another aligned input are dropped with one warning; missing: 'raise' rejects them instead. retained_rows and dropped_rows give the original row positions.
Phit\fit_mixed_gaussian_intercept_matrix(    array $x,    array $y,    array $groups,    string $missing = 'drop',    bool $intercept = true,): MixedGaussianFit
Parameter Type Default Description
$x array required Predictor matrix: one row per observation and one numeric column per predictor; null or NAN is missing
$y array required Numeric response, one value per row; null or NAN is missing
$groups array required Group label per row, an integer or a string; integer 1 and string “1” are different groups
$missing string 'drop' 'drop' removes rows with null or NAN in any variable the model uses and warns once; 'raise' rejects them
$intercept bool true False uses x exactly (including any caller-supplied ones).

Returns

MixedGaussianFit A fit object, read like this array:array{  model: 'linear'|'generalized_linear'|'mixed_gaussian'|'mixed_logistic',  coefficients: list<float>,  covariance: list<list<float>>,  std_errors: list<float>,  p_values: list<float>,  conf_int: list<array{float,float}>,  fitted: list<float>,  residuals: list<float>,  log_likelihood: float,  df_residual: int,  diagnostics: array{converged:bool,solver:string,warnings:list<string>,iterations?:int,evaluations?:int,gradient_max?:float,boundary?:bool,singular?:bool},  n_input: int,  n_observations: int,  n_dropped: int,  retained_rows: list<int>,  dropped_rows: list<int>,  z_statistics: list<float>,  fixed_fitted: list<float>,  marginal_residuals: list<float>,  aic: float,  residual_variance: float,  sigma: float,  method: string,  converged: bool,  group_labels: list<int|string>,  group_sizes: list<int>,  random_effects: list<float>,  random_effects_cond_var: list<float>,  evaluations: int,  n_groups: int,  random_intercept_variance: float,  icc: float,  boundary: bool,}

Errors Exception: a code from 1001 to 1008 for an input or statistical error.

See also Details

fit_mixed_logistic_matrix()

Fits a binomial logit model with one random intercept on a numeric design, like lme4::glmer(family = binomial).

  • Uses the Laplace approximation by default, and adaptive quadrature when quadraturePoints is above 1.
  • An intercept column is prepended unless intercept is false, in which case x is used exactly.
  • Rows with null or NAN in x, y, or another aligned input are dropped with one warning; missing: 'raise' rejects them instead. retained_rows and dropped_rows give the original row positions.
Phit\fit_mixed_logistic_matrix(    array $x,    array $y,    array $groups,    string $missing = 'drop',    ?array $weights = NULL,    ?array $offset = NULL,    int $quadraturePoints = 1,    int $maxEvaluations = 20000,    float $tolerance = 1.0E-7,    bool $intercept = true,): MixedLogisticFit
Parameter Type Default Description
$x array required Predictor matrix: one row per observation and one numeric column per predictor; null or NAN is missing
$y array required 0/1 outcomes, or proportions with integer trial weights; null or NAN is missing
$groups array required Group label per row, an integer or a string; integer 1 and string “1” are different groups
$missing string 'drop' 'drop' removes rows with null or NAN in any variable the model uses and warns once; 'raise' rejects them
$weights ?array NULL Positive integer trial counts, at most 1,000,000 per row; null is one trial per row
$offset ?array NULL Known part of the linear predictor, aligned with the rows; null is zero
$quadraturePoints int 1 1 for the Laplace approximation, or 2 to 61 for adaptive Gauss–Hermite quadrature
$maxEvaluations int 20000 Largest number of optimizer evaluations
$tolerance float 1.0E-7 BOBYQA final trust-region radius, in (0, 0.1]
$intercept bool true False uses x exactly (including any caller-supplied ones).

Returns

MixedLogisticFit A fit object, read like this array:array{  model: 'linear'|'generalized_linear'|'mixed_gaussian'|'mixed_logistic',  coefficients: list<float>,  covariance: list<list<float>>,  std_errors: list<float>,  p_values: list<float>,  conf_int: list<array{float,float}>,  fitted: list<float>,  residuals: list<float>,  log_likelihood: float,  df_residual: int,  diagnostics: array{converged:bool,solver:string,warnings:list<string>,iterations?:int,evaluations?:int,gradient_max?:float,boundary?:bool,singular?:bool},  n_input: int,  n_observations: int,  n_dropped: int,  retained_rows: list<int>,  dropped_rows: list<int>,  z_statistics: list<float>,  fixed_fitted: list<float>,  linear_predictor: list<float>,  aic: float,  bic: float,  n_parameters: int,  group_labels: list<int|string>,  group_sizes: list<int>,  random_effects: list<float>,  random_effects_cond_var: list<float>,  n_groups: int,  random_intercept_sd: float,  random_intercept_variance: float,  neg2_log_likelihood: float,  quadrature_points: int,  integration: string,  method: string,  family: string,  link: string,  optimizer: string,  converged: bool,  evaluations: int,  gradient_max: float,  boundary: bool,  singular: bool,}

Errors Exception: a code from 1001 to 1008 for an input or statistical error.

See also Details