Documentation

LinearMixedModel class

Linear mixed-effects model class

Description

ALinearMixedModel对象代表一个响应变量的模型with fixed and random effects. It comprises data, a model description, fitted coefficients, covariance parameters, design matrices, residuals, residual plots, and other diagnostic information for a linear mixed-effects model. You can predict model responses with thepredictfunction and generate random data at new design points using therandomfunction.

Construction

You can fit a linear mixed-effects model usingfitlme(tbl,formula)if your data is in a table or dataset array. Alternatively, if your model is not easily described using a formula, you can create matrices to define the fixed and random effects, and fit the model usingfitlmematrix(X,y,Z,G).

Input Arguments

expand all

Input data, which includes the response variable, predictor variables, and grouping variables, specified as a table ordatasetarray. The predictor variables can be continuous or grouping variables (seeGrouping Variables). You must specify the model for the variables usingformula.

Data Types:table

Formula for model specification, specified as a character vector or string scalar of the form'y ~ fixed + (random1|grouping1) + ... + (randomR|groupingR)'. For a full description, seeFormula.

Example:'y ~ treatment +(1|block)'

Fixed-effects design matrix, specified as ann-by-pmatrix, wherenis the number of observations, andpis the number of fixed-effects predictor variables. Each row ofXcorresponds to one observation, and each column ofXcorresponds to one variable.

Data Types:single|double

Response values, specified as ann1的向量, wherenis the number of observations.

Data Types:single|double

Random-effects design, specified as either of the following.

  • 如果there is one random-effects term in the model, thenZmust be ann-by-qmatrix, wherenis the number of observations andqis the number of variables in the random-effects term.

  • 如果there areRrandom-effects terms, thenZmust be a cell array of lengthR. Each cell ofZcontains ann-by-q(r) design matrixZ{r},r= 1, 2, ...,R, corresponding to each random-effects term. Here,q(r) is the number of random effects term in therth random effects design matrix,Z{r}.

Data Types:single|double|cell

Grouping variable or variables, specified as either of the following.

  • 如果there is one random-effects term, thenGmust be ann1的向量corresponding to a single grouping variable withMlevels or groups.

    Gcan be a categorical vector, logical vector, numeric vector, character array, string array, or cell array of character vectors.

  • 如果there are multiple random-effects terms, thenGmust be a cell array of lengthR. Each cell ofGcontains a grouping variableG{r},r= 1, 2, ...,R, withM(r) levels.

    G{r}can be a categorical vector, logical vector, numeric vector, character array, string array, or cell array of character vectors.

Data Types:categorical|logical|single|double|char|string|cell

Properties

expand all

Fixed-effects coefficient estimates and related statistics, stored as a dataset array containing the following fields.

Name Name of the term.
Estimate Estimated value of the coefficient.
SE Standard error of the coefficient.
tStat t-statistics for testing the null hypothesis that the coefficient is equal to zero.
DF Degrees of freedom for thet-test. Method to computeDFis specified by the'DFMethod'name-value pair argument.Coefficientsalways uses the'Residual'method for'DFMethod'.
pValue p-value for thet-test.
Lower Lower limit of the confidence interval for coefficient.Coefficientsalways uses the 95% confidence level, i.e.'alpha'is 0.05.
Upper Upper limit of confidence interval for coefficient.Coefficientsalways uses the 95% confidence level, i.e.'alpha'is 0.05.

You can change'DFMethod'and'alpha'while computing confidence intervals for or testing hypotheses involving fixed- and random-effects, using thecoefCIandcoefTestmethods.

Covariance of the estimated fixed-effects coefficients of the linear mixed-effects model, stored as ap-by-pmatrix, wherepis the number of fixed-effects coefficients.

You can display the covariance parameters associated with the random effects using thecovarianceParametersmethod.

Data Types:double

Names of the fixed-effects coefficients of a linear mixed-effects model, stored as a 1-by-pcell array of character vectors.

Data Types:cell

Residual degrees of freedom, stored as a positive integer value.教育部=np, wherenis the number of observations, andpis the number of fixed-effects coefficients.

This corresponds to the'Residual'method of calculating degrees of freedom in thefixedEffectsandrandomEffectsmethods.

Data Types:double

Method used to fit the linear mixed-effects model, stored as either of the following.

  • ML, if the fitting method is maximum likelihood

  • REML, if the fitting method is restricted maximum likelihood

Data Types:char

Specification of the fixed-effects terms, random-effects terms, and grouping variables that define the linear mixed-effects model, stored as an object.

For more information on how to specify the model to fit using a formula, seeFormula.

Maximized log likelihood or maximized restricted log likelihood of the fitted linear mixed-effects model depending on the fitting method you choose, stored as a scalar value.

Data Types:double

Model criterion to compare fitted linear mixed-effects models, stored as a dataset array with the following columns.

AIC Akaike Information Criterion
BIC Bayesian Information Criterion
Loglikelihood Log likelihood value of the model
Deviance –2 times the log likelihood of the model

如果nis the number of observations used in fitting the model, andpis the number of fixed-effects coefficients, then for calculating AIC and BIC,

  • The total number of parameters isnc+p+ 1, wherencis the total number of parameters in the random-effects covariance excluding the residual variance

  • The effective number of observations is

    • n, when the fitting method is maximum likelihood (ML)

    • np, when the fitting method is restricted maximum likelihood (REML)

ML or REML estimate, based on the fitting method used for estimating σ2, stored as a positive scalar value. σ2is the residual variance or variance of the observation error term of the linear mixed-effects model.

Data Types:double

Number of fixed-effects coefficients in the fitted linear mixed-effects model, stored as a positive integer value.

Data Types:double

Number of estimated fixed-effects coefficients in the fitted linear mixed-effects model, stored as a positive integer value.

Data Types:double

Number of observations used in the fit, stored as a positive integer value. This is the number of rows in the table or dataset array, or the design matrices minus the excluded rows or rows withNaNvalues.

Data Types:double

Number of variables used as predictors in the linear mixed-effects model, stored as a positive integer value.

Data Types:double

Total number of variables including the response and predictors, stored as a positive integer value.

  • 如果the sample data is in a table or dataset arraytbl,NumVariablesis the total number of variables intblincluding the response variable.

  • 如果the fit is based on matrix input,NumVariablesis the total number of columns in the predictor matrix or matrices, and response vector.

NumVariablesincludes variables, if there are any, that are not used as predictors or as the response.

Data Types:double

Information about the observations used in the fit, stored as a table.

ObservationInfohas one row for each observation and the following four columns.

Weights The value of the weighted variable for that observation. Default value is 1.
Excluded true, if the observation was excluded from the fit using the'Exclude'name-value pair argument,false, otherwise. 1 stands fortrueand 0 stands forfalse.
Missing

true, if the observation was excluded from the fit because any response or predictor value is missing,false, otherwise.

Missing values includeNaNfor numeric variables, empty cells for cell arrays, blank rows for character arrays, and thevalue for categorical arrays.

Subset true,如果观察用于健康,false, if it was not used because it is missing or excluded.

Data Types:table

Names of observations used in the fit, stored as a cell array of character vectors.

  • 如果the data is in a table or dataset array,tbl, containing observation names,ObservationNameshas those names.

  • 如果the data is provided in matrices, or a table or dataset array without observation names, thenObservationNamesis an empty cell array.

Data Types:cell

Names of the variables that you use as predictors in the fit, stored as a cell array of character vectors that has the same length asNumPredictors.

Data Types:cell

Name of the variable used as the response variable in the fit, stored as a character vector.

Data Types:char

Proportion of variability in the response explained by the fitted model, stored as a structure. It is the multiple correlation coefficient or R-squared.Rsquaredhas two fields.

Ordinary R-squared value, stored as a scalar value in a structure.Rsquared.Ordinary = 1 – SSE./SST
Adjusted

R-squared value adjusted for the number of fixed-effects coefficients, stored as a scalar value in a structure.

Rsquared.Adjusted = 1 – (SSE./SST)*(DFT./DFE),

where教育部= n – p,DFT = n – 1, andnis the total number of observations,pis the number of fixed-effects coefficients.

Data Types:struct

Error sum of squares, that is, sum of the squared conditional residuals, stored as a positive scalar value.

SSE = sum((y – F).^2), whereyis the response vector, andFis the fitted conditional response of the linear mixed-effects model. The conditional model has contributions from both fixed and random effects.

Data Types:double

Regression sum of squares, that is, the sum of squares explained by the linear mixed-effects regression, stored as a positive scalar value. It is the sum of squared deviations of the conditional fitted values from their mean.

SSR = sum((F – mean(F)).^2), whereFis the fitted conditional response of the linear mixed-effects model. The conditional model has contributions from both fixed and random effects.

Data Types:double

Total sum of squares, that is, the sum of the squared deviations of the observed response values from their mean, stored as a positive scalar value.

SST = sum((y – mean(y)).^2) = SSR + SSE, whereyis the response vector.

Data Types:double

Variables, stored as a table.

  • 如果the fit is based on a table or dataset arraytbl, thenVariablesis identical totbl.

  • 如果the fit is based on matrix input, thenVariablesis a table containing all the variables in the predictor matrix or matrices, and response variable.

Data Types:table

Information about the variables used in the fit, stored as a table.

VariableInfohas one row for each variable and contains the following four columns.

Class Class of the variable ('double','cell','nominal', and so on).
Range

Value range of the variable.

  • For a numerical variable, it is a two-element vector of the form[min,max].

  • For a cell or categorical variable, it is a cell or categorical array containing all unique values of the variable.

InModel

true, if the variable is a predictor in the fitted model.

false, if the variable is not in the fitted model.

IsCategorical

true, if the variable has a type that is treated as a categorical predictor, such as cell, logical, or categorical, or if it is specified as categorical by the'Categorical'name-value pair argument of thefitmethod.

false, if it is a continuous predictor.

Data Types:table

Names of the variables used in the fit, stored as a cell array of character vectors.

  • 如果sample data is in a table or dataset arraytbl,VariableNamescontains the names of the variables intbl.

  • 如果sample data is in matrix format, thenVariableInfoincludes variable names you supply while fitting the model. If you do not supply the variable names, thenVariableInfocontains the default names.

Data Types:cell

Methods

anova Analysis of variance for linear mixed-effects model
coefCI 系数的置信区间线性mixed-effects model
coefTest Hypothesis test on fixed and random effects of linear mixed-effects model
compare Compare linear mixed-effects models
covarianceParameters Extract covariance parameters of linear mixed-effects model
designMatrix Fixed- and random-effects design matrices
disp Display linear mixed-effects model
fit (不推荐)符合线性mixed-effects模型using tables
fitmatrix (不推荐)符合线性mixed-effects模型using design matrices
fitted Fitted responses from a linear mixed-effects model
fixedEffects Estimates of fixed effects and related statistics
plotResiduals Plot residuals of linear mixed-effects model
predict Predict response of linear mixed-effects model
random Generate random responses from fitted linear mixed-effects model
randomEffects Estimates of random effects and related statistics
residuals 拟合线性mixed-effects模型的残差
response Response vector of the linear mixed-effects model

Copy Semantics

Value. To learn how value classes affect copy operations, seeCopying Objects(MATLAB).

Examples

collapse all

Load the sample data.

loadflu

Thefludataset array has aDatevariable, and 10 variables containing estimated influenza rates (in 9 different regions, estimated from Google® searches, plus a nationwide estimate from the Center for Disease Control and Prevention, CDC).

To fit a linear-mixed effects model, your data must be in a properly formatted dataset array. To fit a linear mixed-effects model with the influenza rates as the responses and region as the predictor variable, combine the nine columns corresponding to the regions into an array. The new dataset array,flu2, must have the response variable,FluRate, the nominal variable,Region, that shows which region each estimate is from, and the grouping variableDate.

flu2 = stack(flu,2:10,'NewDataVarName','FluRate',...'IndVarName','Region'); flu2.Date = nominal(flu2.Date);

Fit a linear mixed-effects model with fixed effects for region and a random intercept that varies byDate.

Because region is a nominal variable,fitlmetakes the first region,NE, as the reference and creates eight dummy variables representing the other eight regions. For example, I [ M i d A t l ] is the dummy variable representing the regionMidAtl. For details, seeDummy Variables.

The corresponding model is

y i m = β 0 + β 1 I [ M i d A t l ] i + β 2 I [ E N C e n t r a l ] i + β 3 I [ W N C e n t r a l ] i + β 4 I [ S A t l ] i + β 5 I [ E S C e n t r a l ] i + β 6 I [ W S C e n t r a l ] i + β 7 I [ M t n ] i + β 8 I [ P a c ] i + b 0 m + ε i m , m = 1 , 2 , . . . , 5 2 ,

where y i m is the observation i for level m of grouping variableDate, β j , j = 0, 1, ..., 8, are the fixed-effects coefficients, b 0 m is the random effect for level m of the grouping variableDate, and ε i m is the observation error for observation i . The random effect has the prior distribution, b 0 m N ( 0 , σ b 2 ) and the error term has the distribution, ε i m N ( 0 , σ 2 ) .

lme = fitlme(flu2,'FluRate ~ 1 + Region + (1|Date)')
lme = Linear mixed-effects model fit by ML Model information: Number of observations 468 Fixed effects coefficients 9 Random effects coefficients 52 Covariance parameters 2 Formula: FluRate ~ 1 + Region + (1 | Date) Model fit statistics: AIC BIC LogLikelihood Deviance 318.71 364.35 -148.36 296.71 Fixed effects coefficients (95% CIs): Name Estimate SE tStat DF {'(Intercept)' } 1.2233 0.096678 12.654 459 {'Region_MidAtl' } 0.010192 0.052221 0.19518 459 {'Region_ENCentral'} 0.051923 0.052221 0.9943 459 {'Region_WNCentral'} 0.23687 0.052221 4.5359 459 {'Region_SAtl' } 0.075481 0.052221 1.4454 459 {'Region_ESCentral'} 0.33917 0.052221 6.495 459 {'Region_WSCentral'} 0.069 0.052221 1.3213 459 {'Region_Mtn' } 0.046673 0.052221 0.89377 459 {'Region_Pac' } -0.16013 0.052221 -3.0665 459 pValue Lower Upper 1.085e-31 1.0334 1.4133 0.84534 -0.092429 0.11281 0.3206 -0.050698 0.15454 7.3324e-06 0.13424 0.33949 0.14902 -0.02714 0.1781 2.1623e-10 0.23655 0.44179 0.18705 -0.033621 0.17162 0.37191 -0.055948 0.14929 0.0022936 -0.26276 -0.057514 Random effects covariance parameters (95% CIs): Group: Date (52 Levels) Name1 Name2 Type Estimate {'(Intercept)'} {'(Intercept)'} {'std'} 0.6443 Lower Upper 0.5297 0.78368 Group: Error Name Estimate Lower Upper {'Res Std'} 0.26627 0.24878 0.285

The p -values 7.3324e-06 and 2.1623e-10 respectively show that the fixed effects of the flu rates in regionsWNCentralandESCentralare significantly different relative to the flu rates in regionNE.

The confidence limits for the standard deviation of the random-effects term, σ b , do not include 0 (0.5297, 0.78368), which indicates that the random-effects term is significant. You can also test the significance of the random-effects terms using thecomparemethod.

The estimated value of an observation is the sum of the fixed effects and the random-effect value at the grouping variable level corresponding to that observation. For example, the estimated best linear unbiased predictor (BLUP) of the flu rate for regionWNCentralin week 10/9/2005 is

y ˆ W N C e n t r a l , 1 0 / 9 / 2 0 0 5 = β ˆ 0 + β ˆ 3 I [ W N C e n t r a l ] + b ˆ 1 0 / 9 / 2 0 0 5 = 1 . 2 2 3 3 + 0 . 2 3 6 8 7 - 0 . 1 7 1 8 = 1 . 2 8 8 3 7 .

This is the fitted conditional response, since it includes contribution to the estimate from both the fixed and random effects. You can compute this value as follows.

beta = fixedEffects(lme); [~,~,STATS] = randomEffects(lme);% Compute the random-effects statistics (STATS)STATS.Level = nominal(STATS.Level); y_hat = beta(1) + beta(4) + STATS.Estimate(STATS.Level=='10/9/2005')
y_hat = 1.2884

You can simply display the fitted value using thefittedmethod.

F = fitted(lme); F(flu2.Date =='10/9/2005'& flu2.Region =='WNCentral')
ans = 1.2884

Compute the fitted marginal response for regionWNCentralin week 10/9/2005.

F = fitted(lme,'Conditional',false); F(flu2.Date =='10/9/2005'& flu2.Region =='WNCentral')
ans = 1.4602

Load the sample data.

loadcarbig

Fit a linear mixed-effects model for miles per gallon (MPG), with fixed effects for acceleration, horsepower and the cylinders, and uncorrelated random-effect for intercept and acceleration grouped by the model year. This model corresponds to

M P G i m = β 0 + β 1 A c c i + β 2 H P + b 0 m + b 1 m A c c i m + ε i m , m = 1 , 2 , 3 ,

with the random-effects terms having the following prior distributions:

b m = ( b 0 m b 1 m ) N ( 0 , ( σ 0 2 σ 0 , 1 σ 0 , 1 σ 1 2 ) ) ,

where m represents the model year.

First, prepare the design matrices for fitting the linear mixed-effects model.

X = [ones(406,1) Acceleration Horsepower]; Z = [ones(406,1) Acceleration]; Model_Year = nominal(Model_Year); G = Model_Year;

Now, fit the model usingfitlmematrixwith the defined design matrices and grouping variables. Use the'fminunc'optimization algorithm.

lme = fitlmematrix(X,MPG,Z,G,'FixedEffectPredictors',....{'Intercept','Acceleration','Horsepower'},'RandomEffectPredictors',...{{'Intercept','Acceleration'}},'RandomEffectGroups',{“莫del_Year'},...'FitMethod','REML')
lme = Linear mixed-effects model fit by REML Model information: Number of observations 392 Fixed effects coefficients 3 Random effects coefficients 26 Covariance parameters 4 Formula: Linear Mixed Formula with 4 predictors. Model fit statistics: AIC BIC LogLikelihood Deviance 2202.9 2230.7 -1094.5 2188.9 Fixed effects coefficients (95% CIs): Name Estimate SE tStat DF {'Intercept' } 50.064 2.3176 21.602 389 {'Acceleration'} -0.57897 0.13843 -4.1825 389 {'Horsepower' } -0.16958 0.0073242 -23.153 389 pValue Lower Upper 1.4185e-68 45.507 54.62 3.5654e-05 -0.85112 -0.30681 3.5289e-75 -0.18398 -0.15518 Random effects covariance parameters (95% CIs): Group: Model_Year (13 Levels) Name1 Name2 Type Estimate {'Intercept' } {'Intercept' } {'std' } 3.72 {'Acceleration'} {'Intercept' } {'corr'} -0.8769 {'Acceleration'} {'Acceleration'} {'std' } 0.3593 Lower Upper 1.5215 9.0954 -0.98274 -0.33845 0.19418 0.66483 Group: Error Name Estimate Lower Upper {'Res Std'} 3.6913 3.4331 3.9688

The fixed effects coefficients display includes the estimate, standard errors (SE), and the 95% confidence interval limits (LowerandUpper). The p -values for (pValue) indicate that all three fixed-effects coefficients are significant.

The confidence intervals for the standard deviations and the correlation between the random effects for intercept and acceleration do not include zeros, hence they seem significant. Use thecomparemethod to test for the random effects.

Display the covariance matrix of the estimated fixed-effects coefficients.

lme.CoefficientCovariance
ans =3×35.3711 -0.2809 -0.0126 -0.2809 0.0192 0.0005 -0.0126 0.0005 0.0001

The diagonal elements show the variances of the fixed-effects coefficient estimates. For example, the variance of the estimate of the intercept is 5.3711. Note that the standard errors of the estimates are the square roots of the variances. For example, the standard error of the intercept is 2.3176, which issqrt(5.3711).

The off-diagonal elements show the correlation between the fixed-effects coefficient estimates. For example, the correlation between the intercept and acceleration is –0.2809 and the correlation between acceleration and horsepower is 0.0005.

Display the coefficient of determination for the model.

lme.Rsquared
ans =struct with fields:Ordinary: 0.7826 Adjusted: 0.7815

The adjusted value is the R-squared value adjusted for the number of predictors in the model.

More About

expand all