GeneralizedLinearMixedModel<T>

Models & Types · AiDotNet.Regression.MixedEffects

Generalized Linear Mixed-Effects Model (GLMM) for non-Gaussian hierarchical data.

Inheritance: RegressionBase<T> → GeneralizedLinearMixedModel<T>

Implements: IGeneratedParameterRegistrar<T>

For Beginners

GLMMs are like mixed models but for outcomes that aren’t continuous/normal:

Common use cases:

  • Binary outcomes (yes/no): Use logistic GLMM with logit link
  • Count data: Use Poisson GLMM with log link
  • Overdispersed counts: Use Negative Binomial GLMM
  • Proportions: Use Binomial GLMM

Example: Student pass/fail across schools

  • Fixed effect: Effect of study time on probability of passing
  • Random intercept: Schools have different baseline pass rates
  • Random slope: Effect of study time might differ by school

The model estimates effects on the log-odds (or log-rate) scale, which can then be converted to probabilities or rates.

How It Works

GLMMs extend linear mixed models to handle non-Gaussian responses (binary, count, etc.) by incorporating a link function and response distribution from the exponential family.

The model has the form: g(E[y|u]) = Xbeta + Zu where:

  • g() is the link function (logit, log, identity, etc.)
  • X*beta: Fixed effects on the linear predictor scale
  • Z*u: Random effects on the linear predictor scale
  • u ~ N(0, D) where D is the variance-covariance matrix

Example

using AiDotNet;
using AiDotNet.Data.Loaders;
using AiDotNet.Regression.MixedEffects;
using AiDotNet.Tensors.LinearAlgebra;

double[][] features =
{
    new[] { 1.0, 2.0 }, new[] { 2.0, 3.0 }, new[] { 3.0, 4.0 },
    new[] { 4.0, 5.0 }, new[] { 5.0, 6.0 }, new[] { 6.0, 7.0 }
};
double[] targets = { 3.0, 5.0, 7.0, 9.0, 11.0, 13.0 };

var result = await new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigureModel(new GeneralizedLinearMixedModel<double>())
    .ConfigureDataLoader(DataLoaders.FromArrays(features, targets))
    .BuildAsync();

Console.WriteLine("Trained GeneralizedLinearMixedModel.");

Constructors

Constructor Summary
GeneralizedLinearMixedModel(GLMMOptions<T> options, IRegularization<T, Matrix<T>, Vector<T>> regularization) Initializes a new Generalized Linear Mixed-Effects Model.

Properties

Property Summary
GLMMFamily Family { get; } Gets the response distribution family.
GLMMLinkFunction LinkFunction { get; } Gets the link function.
IReadOnlyList<RandomEffect<T>> RandomEffects { get; } Gets the random effects specifications with estimated values.
T AIC { get; } Gets the AIC (Akaike Information Criterion).
T BIC { get; } Gets the BIC (Bayesian Information Criterion).
T Dispersion { get; } Gets the dispersion parameter.
T LogLikelihood { get; } Gets the log-likelihood of the fitted model.
VarianceDecomposition<T> VarianceComponents { get; } Gets the variance decomposition.
Vector<T> FixedEffects { get; } Gets the fixed effects coefficients.
bool SupportsParameterInitialization { get; } Trains the GLMM using Penalized Quasi-Likelihood (PQL) or Laplace approximation.

Methods

Method Summary
GeneralizedLinearMixedModel<T> AddRandomIntercept(string name, int groupColumnIndex) Adds a random intercept effect to the model.
GeneralizedLinearMixedModel<T> AddRandomSlope(string name, int groupColumnIndex, int[] slopeColumns, bool includeIntercept) Adds a random slope effect to the model.
Vector<T> Predict(Matrix<T> input) Makes predictions for new data on the response scale.
Vector<T> PredictLinearPredictor(Matrix<T> input) Gets predictions on the linear predictor scale (before applying inverse link).
Vector<T> PredictProbability(Matrix<T> input) Gets predicted probabilities for binary classification (logistic GLMM).
void Train(Matrix<T> x, Vector<T> y)