fitted
Fitted responses from a linear mixed-effects model
Description
returns
the fitted conditional
response from the linear mixed-effects model yfit
= fitted(lme
)lme
.
specifies whether to calculate the conditional or marginal response.yfit
= fitted(lme
,Conditional=conditional
)
Examples
Load the sample data and use the dataset2table
function to convert it into table format.
load flu
flu = dataset2table(flu)
flu=52×11 table
Date NE MidAtl ENCentral WNCentral SAtl ESCentral WSCentral Mtn Pac WtdILI
______________ _____ ______ _________ _________ _____ _________ _________ _____ _____ ______
{'10/9/2005' } 0.97 1.025 1.232 1.286 1.082 1.457 1.1 0.981 0.971 1.182
{'10/16/2005'} 1.136 1.06 1.228 1.286 1.146 1.644 1.123 0.976 0.917 1.22
{'10/23/2005'} 1.135 1.172 1.278 1.536 1.274 1.556 1.236 1.102 0.895 1.31
{'10/30/2005'} 1.52 1.489 1.576 1.794 1.59 2.252 1.612 1.321 1.082 1.343
{'11/6/2005' } 1.365 1.394 1.53 1.825 1.62 2.059 1.471 1.453 1.118 1.586
{'11/13/2005'} 1.39 1.477 1.506 1.9 1.683 1.813 1.464 1.388 1.204 1.47
{'11/20/2005'} 1.212 1.231 1.295 1.495 1.347 1.794 1.303 1.371 1.137 1.611
{'11/27/2005'} 1.477 1.546 1.557 1.855 1.678 2.159 1.739 1.628 1.443 1.827
{'12/4/2005' } 1.285 1.43 1.482 1.635 1.577 1.903 1.53 1.701 1.516 1.776
{'12/11/2005'} 1.354 1.45 1.46 1.794 1.583 1.894 1.831 2.364 2.094 1.941
{'12/18/2005'} 1.502 1.622 1.638 1.988 1.947 2.22 2.577 3.89 2.66 2.34
{'12/25/2005'} 1.86 1.915 1.955 2.38 2.343 3.027 3.219 4.862 2.595 3.086
{'1/1/2006' } 2.114 2.174 2.065 2.557 2.275 2.498 2.644 3.352 2.181 3.26
{'1/8/2006' } 1.815 1.932 1.822 2.046 1.969 1.805 2.189 2.132 1.717 2.613
{'1/15/2006' } 1.541 1.695 1.581 2.008 1.718 1.662 2.156 1.694 1.351 2.247
{'1/22/2006' } 1.632 1.758 1.711 2.217 1.866 2.194 2.268 1.826 1.384 2.352
⋮
The flu
table has a Date
variable, 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 table. 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 table, flu2
, must have the response variable, FluRate
, the nominal variable, Region
, that shows which region each estimate is from, and the grouping variable Date
.
flu2 = stack(flu,2:10,NewDataVariableName="FluRate",IndexVariableName="Region")
flu2=468×4 table
Date WtdILI Region FluRate
______________ ______ _________ _______
{'10/9/2005' } 1.182 NE 0.97
{'10/9/2005' } 1.182 MidAtl 1.025
{'10/9/2005' } 1.182 ENCentral 1.232
{'10/9/2005' } 1.182 WNCentral 1.286
{'10/9/2005' } 1.182 SAtl 1.082
{'10/9/2005' } 1.182 ESCentral 1.457
{'10/9/2005' } 1.182 WSCentral 1.1
{'10/9/2005' } 1.182 Mtn 0.981
{'10/9/2005' } 1.182 Pac 0.971
{'10/16/2005'} 1.22 NE 1.136
{'10/16/2005'} 1.22 MidAtl 1.06
{'10/16/2005'} 1.22 ENCentral 1.228
{'10/16/2005'} 1.22 WNCentral 1.286
{'10/16/2005'} 1.22 SAtl 1.146
{'10/16/2005'} 1.22 ESCentral 1.644
{'10/16/2005'} 1.22 WSCentral 1.123
⋮
flu2.Date = nominal(flu2.Date);
Fit a linear mixed-effects model with fixed effects for region and a random intercept that varies by Date
.
Region is a categorical variable. You can specify the contrasts for categorical variables using the DummyVarCoding
name-value pair argument when fitting the model. When you do not specify the contrasts, fitlme
uses the 'reference'
contrast by default. Because the model has an intercept, fitlme
takes the first region, NE
, as the reference and creates eight dummy variables representing the other eight regions. For example, is the dummy variable representing the region MidAtl
. For details, see Dummy Variables.
The corresponding model is
where is the observation for level of grouping variable Date
, , = 0, 1, ..., 8, are the fixed-effects coefficients, with being the coefficient for region NE
. is the random effect for level of the grouping variable Date
, and is the observation error for observation . The random effect has the prior distribution, and the error term has the distribution, .
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 pValue Lower Upper {'(Intercept)' } 1.2233 0.096678 12.654 459 1.085e-31 1.0334 1.4133 {'Region_MidAtl' } 0.010192 0.052221 0.19518 459 0.84534 -0.092429 0.11281 {'Region_ENCentral'} 0.051923 0.052221 0.9943 459 0.3206 -0.050698 0.15454 {'Region_WNCentral'} 0.23687 0.052221 4.5359 459 7.3324e-06 0.13424 0.33949 {'Region_SAtl' } 0.075481 0.052221 1.4454 459 0.14902 -0.02714 0.1781 {'Region_ESCentral'} 0.33917 0.052221 6.495 459 2.1623e-10 0.23655 0.44179 {'Region_WSCentral'} 0.069 0.052221 1.3213 459 0.18705 -0.033621 0.17162 {'Region_Mtn' } 0.046673 0.052221 0.89377 459 0.37191 -0.055948 0.14929 {'Region_Pac' } -0.16013 0.052221 -3.0665 459 0.0022936 -0.26276 -0.057514 Random effects covariance parameters (95% CIs): Group: Date (52 Levels) Name1 Name2 Type Estimate Lower Upper {'(Intercept)'} {'(Intercept)'} {'std'} 0.6443 0.5297 0.78368 Group: Error Name Estimate Lower Upper {'Res Std'} 0.26627 0.24878 0.285
The -values 7.3324e-06 and 2.1623e-10 respectively show that the fixed effects of the flu rates in regions WNCentral
and ESCentral
are significantly different relative to the flu rates in region NE
.
The confidence limits for the standard deviation of the random-effects term, , 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 the compare
method.
The conditional fitted response from the model at a given observation includes contributions from fixed and random effects. For example, the estimated best linear unbiased predictor (BLUP) of the flu rate for region WNCentral
in week 10/9/2005 is
This is the fitted conditional response, since it includes contributions 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
In the previous calculation, beta(1)
corresponds to the estimate for and beta(4)
corresponds to the estimate for . You can simply display the fitted value using the fitted
method.
F = fitted(lme); F(flu2.Date == '10/9/2005' & flu2.Region == 'WNCentral')
ans = 1.2884
The estimated marginal response for region WNCentral
in week 10/9/2005 is
Compute the fitted marginal response.
F = fitted(lme,'Conditional',false); F(flu2.Date == '10/9/2005' & flu2.Region == 'WNCentral')
ans = 1.4602
Load the sample data.
load('weight.mat');
weight
contains data from a longitudinal study, where 20 subjects are randomly assigned to 4 exercise programs, and their weight loss is recorded over six 2-week time periods. This is simulated data.
Store the data in a table. Define Subject
and Program
as categorical variables.
tbl = table(InitialWeight,Program,Subject,Week,y); tbl.Subject = nominal(tbl.Subject); tbl.Program = nominal(tbl.Program);
Fit a linear mixed-effects model where the initial weight, type of program, week, and the interaction between the week and type of program are the fixed effects. The intercept and week vary by subject.
lme = fitlme(tbl,'y ~ InitialWeight + Program*Week + (Week|Subject)');
Compute the fitted values and raw residuals.
F = fitted(lme); R = residuals(lme);
Plot the residuals versus the fitted values.
plot(F,R,'bx') xlabel('Fitted Values') ylabel('Residuals')
Now, plot the residuals versus the fitted values, grouped by program.
figure() gscatter(F,R,Program)
Input Arguments
Linear mixed-effects model, specified as a LinearMixedModel
object constructed using fitlme
or fitlmematrix
.
Indicator for conditional response, specified as one of the following.
true | Contribution from both fixed effects and random effects (conditional) |
false | Contribution from only fixed effects (marginal) |
Example: Conditional=false
Data Types: logical
Output Arguments
Fitted response values, returned as an n-by-1 vector, where n is the number of observations.
More About
A conditional response includes contributions from both fixed and random effects, whereas a marginal response includes contribution from only fixed effects.
Suppose the linear mixed-effects model, lme
,
has an n-by-p fixed-effects
design matrix X and an n-by-q random-effects
design matrix Z. Also, suppose the p-by-1
estimated fixed-effects vector is , and the q-by-1
estimated best linear unbiased predictor (BLUP) vector of random effects
is . The fitted conditional response
is
and the fitted marginal response is
Version History
Introduced in R2013b
See Also
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)