LinearMixedModel<T>

Models & Types ยท AiDotNet.Regression.MixedEffects

Linear Mixed-Effects Model for hierarchical and clustered data.

Inheritance: RegressionBase<T> โ†’ LinearMixedModel<T>

Implements: IGeneratedParameterRegistrar<T>

For Beginners

Mixed models are essential when your data has natural grouping:

Example: Student test scores across 50 schools

  • Fixed effect: Effect of study time on scores (same for all schools)
  • Random intercept: Each school may have a different baseline score level
  • Random slope: Effect of study time might differ by school

Benefits over simple regression:

  1. Correct standard errors (not underestimated)
  2. Borrowing strength across groups (shrinkage)
  3. Quantify between-group variation
  4. Handle unbalanced data naturally

When to use mixed models:

  • Repeated measures on individuals
  • Students in classrooms in schools
  • Patients in hospitals
  • Longitudinal/panel data

How It Works

Linear Mixed-Effects (LME) models extend linear regression to handle grouped or nested data by including both fixed effects (population-level parameters) and random effects (group-level deviations from the population).

The model has the form: y = Xbeta + Zu + epsilon where:

  • X*beta: Fixed effects (same for all observations)
  • Z*u: Random effects (vary by group)
  • epsilon: Residual error

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 LinearMixedModel<double>())
    .ConfigureDataLoader(DataLoaders.FromArrays(features, targets))
    .BuildAsync();

Console.WriteLine("Trained LinearMixedModel.");

Constructors

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

Properties

Property Summary
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 ConditionalRSquared { get; set; } Conditional R-squared (fixed + random effects).
T LogLikelihood { get; } Gets the log-likelihood of the fitted model.
T MarginalRSquared { get; set; } Marginal R-squared (fixed effects only).
VarianceDecomposition<T> VarianceComponents { get; } Gets the variance decomposition.
Vector<T> FixedEffects { get; } Gets the fixed effects coefficients.
bool SupportsParameterInitialization { get; } Trains the Linear Mixed-Effects Model using the EM algorithm.

Methods

Method Summary
LinearMixedModel<T> AddRandomIntercept(string name, int groupColumnIndex) Adds a random intercept effect to the model.
LinearMixedModel<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.
void Train(Matrix<T> x, Vector<T> y)