Main Content

Fit Mixed-Effects Spline Regression

This example shows how to fit a mixed-effects linear spline model.

Load the sample data.


This is simulated data.

Plot y versus sorted x.

[x_sorted,I] = sort(x,'ascend');

Figure contains an axes object. The axes contains a line object which displays its values using only markers.

Fit the following mixed-effects linear spline regression model


where kj is the j th knot, and K is the total number of knots. Assume that bjN(0,σb2) and ϵN(0,σ2).

Define the knots.

k = linspace(0.05,0.95,100);

Define the design matrices.

X = [ones(1000,1),x];
Z = zeros(length(x),length(k));
for j = 1:length(k)
      Z(:,j) = max(X(:,2) - k(j),0);

Fit the model with an isotropic covariance structure for the random effects.

lme = fitlmematrix(X,y,Z,[],'CovariancePattern','Isotropic');

Fit a fixed-effects only model.

X = [X Z];
lme_fixed = fitlmematrix(X,y,[],[]);

Compare lme_fixed and lme via a simulated likelihood ratio test.

ans = 
    Simulated Likelihood Ratio Test: Nsim = 500, Alpha = 0.05

    Model        DF     AIC       BIC       LogLik     LRStat    pValue     Lower      Upper  
    lme            4    170.62    190.25    -81.309                                           
    lme_fixed    103    113.38    618.88     46.309    255.24    0.68064    0.63784    0.72129

The p-value indicates that the fixed-effects only model is not a better fit than the mixed-effects spline regression model.

Plot the fitted values from both models on top of the original response data.

R = response(lme);
plot(x_sorted,R(I),'o', 'MarkerFaceColor',[0.8,0.8,0.8],...
hold on
F = fitted(lme);
F_fixed = fitted(lme_fixed);
legend('data','mixed effects','fixed effects','Location','NorthWest')
xlabel('sorted x values');
hold off

Figure contains an axes object. The axes object with xlabel sorted x values, ylabel y contains 3 objects of type line. One or more of the lines displays its values using only markers These objects represent data, mixed effects, fixed effects.

You can also see from the figure that the mixed-effects model provides a better fit to data than the fixed-effects only model.