主要内容

predict

Class:LinearMixedModel

Predict response of linear mixed-effects model

Description

例子

ypred=预测(lmereturns a vector of有条件预测的响应ypred一个t the original predictors used to fit the linear mixed-effects modellme

例子

ypred=预测(lme,,,,tblnew返回条件预测响应的向量ypred来自拟合的线性混合效应模型lme一个t the values in the new table or dataset arraytblnew。Use a table or dataset array forpredictif you use a table or dataset array for fitting the modellme

If a particular grouping variable intblnewhas levels that are not in the original data, then the random effects for that grouping variable do not contribute to the“有条件”prediction at observations where the grouping variable has new levels.

ypred=预测(lme,,,,Xnew,,,,znew返回条件预测响应的向量ypred来自拟合的线性混合效应模型lme一个t the values in the new fixed- and random-effects design matrices,Xnew一个ndznew, 分别。znew也可以是矩阵的单元格数组。在这种情况下,分组变量Gis一个(n,1),,,,wherenis the number of observations used in the fit.

Use the matrix format forpredictif using design matrices for fitting the modellme

例子

ypred=预测(lme,,,,Xnew,,,,znew,,,,Gnew返回条件预测响应的向量ypred来自拟合的线性混合效应模型lme一个t the values in the new fixed- and random-effects design matrices,Xnew一个ndznew分别和分组变量Gnew

znew一个ndGnew也可以分别是矩阵和分组变量的细胞阵列。

例子

ypred=预测(___,,,,Name,Value返回预测响应的向量ypred来自拟合的线性混合效应模型lmewith additional options specified by one or moreName,Value配对参数。

For example, you can specify the confidence level, simultaneous confidence bounds, or contributions from only fixed effects.

例子

[[ypred,,,,ypredCI] =预测(___一个lso returns confidence intervalsypredCIfor the predictionsypredfor any of the input arguments in the previous syntaxes.

例子

[[ypred,,,,ypredCI,,,,DF] =预测(___还返回自由度DFused in computing the confidence intervals for any of the input arguments in the previous syntaxes.

Input Arguments

expand all

线性混合效应模型,指定为LinearMixedModel目的constructed usingfitlmeorfitlmematrix

New input data, which includes the response variable, predictor variables, and分组变量,指定为表或数据集数组。预测变量可以是连续的或分组变量。tblnewmust have the same variables as in the original table or dataset array used to fit the linear mixed-effects modellme

新的固定效应设计矩阵,指定为n-经过-p矩阵,哪里nis the number of observations andpis the number of fixed predictor variables. Each row ofXcorresponds to one observation and each column ofX对应于一个变量。

Data Types:single|double

新的随机效应设计,指定为n-经过-矩阵or一个cell array ofr设计矩阵z{r},,,,wherer=1, 2, ...,r。Ifznew是一个单元格数,然后z{r}is ann-经过-((r)矩阵,哪里nis the number of observations, and((r)是随机预测变量的数量。

Data Types:single|double|cell

Newgrouping variable or variables,,,,specified as a vector or a cell array, of lengthr,,,,of grouping variables with the same levels or groups as the original grouping variables used to fit the linear mixed-effects modellme

Data Types:single|double|分类|逻辑|char|string|cell

名称值参数

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN,,,,whereName是参数名称和Value是相应的值。名称值参数必须在其他参数之后出现,但是对的顺序并不重要。

Before R2021a, use commas to separate each name and value, and encloseNamein quotes.

显着性水平,指定为逗号分隔对,由'Alpha'范围0到1中的标量值,对于一个值α,置信度水平为100*(1 -α)%。

例如,对于99%的置信区间,您可以按以下方式指定置信度。

Example:'Alpha',0.01

Data Types:single|double

Indicator forconditional predictions,指定为逗号分隔对,由“有条件”以及以下内容之一。

true 固定效应和随机效应(条件)的贡献
错误的 Contribution from only fixed effects (marginal)

Example:'有条件,假

计算大约在置信区间计算中使用自由度的方法,该计算指定为逗号分隔对'DFMethod'以及以下内容之一。

'residual' Default. The degrees of freedom are assumed to be constant and equal ton-p,,,,wherenis the number of observations andp是固定效果的数量。
'satterthwaite' Satterthwaite近似。
'没有任何' 所有自由度都设定为无限。

例如,您可以按以下方式指定Satterthwaite近似。

Example:“ dfmethod','satterthwaite'

置信界的类型,指定为逗号分隔对'同时'以及以下内容之一。

错误的 Default. Nonsimultaneous bounds.
true 同时界限。

Example:“同时”,真实

预测类型,,,,specified as the comma-separated pair consisting of'预言'以及以下内容之一。

'curve' Default. Confidence bounds for the predictions based on the fitted function.
'observation' Variability due to observation error for the new observations is also included in the confidence bound calculations and this results in wider bounds.

Example:'预言',,,,'observation'

Output Arguments

expand all

Predicted responses, returned as a vector.ypredcan contain the conditional or marginal responses, depending on the value choice of the“有条件”名称值对参数。条件预测包括固定和随机效应的贡献。

预测值的点置信区间,作为两列矩阵返回。第一列yCI包含下限,第二列包含上限。默认,yCIcontains the 95% confidence intervals for the predictions. You can change the confidence level using theAlpha名称值对参数,使它们同时使用同时名称值对参数,也使它们进行新的观察,而不是使用曲线预言名称值对参数。

计算置信区间的自由度,作为向量或标量值返回。

  • If the'同时'n一个me-value pair argument is错误的, 然后DFis a vector.

  • If the'同时'n一个me-value pair argument istrue, 然后DFis a scalar value.

Examples

expand all

Load the sample data.

加载(('fertilizer.mat');

The dataset array includes data from a split-plot experiment, where soil is divided into three blocks based on the soil type: sandy, silty, and loamy. Each block is divided into five plots, where five different types of tomato plants (cherry, heirloom, grape, vine, and plum) are randomly assigned to these plots. The tomato plants in the plots are then divided into subplots, where each subplot is treated by one of four fertilizers. This is simulated data.

Store the data in a dataset array calledDS,,,,for practical purposes, and defineTomato,,,,土壤,,,,一个ndFertilizer一个s categorical variables.

DS =肥料;ds.tomato =名义(ds.tomato);ds.soil =名义(ds.soil);ds.fertierizer =名义(ds.fertilizer);

适合线性混合效应模型,其中Fertilizer一个ndTomato一个re the fixed-effects variables, and the mean yield varies by the block (soil type), and the plots within blocks (tomato types within soil types) independently.

lme = fitlme(ds,'Yield ~ Fertilizer * Tomato + (1|Soil) + (1|Soil:Tomato)');

预测原始设计值的响应值。用观察到的响应值显示前五个预测。

yhat =预测(LME);[yhat(1:5)Ds.yield(1:5)]
一个ns =5×2115.4788 104.0000 135.1455 136.0000 152.8121 158.0000 160.4788 174.0000 58.0839 57.0000

Load the sample data.

加载carsmall

Fit a linear mixed-effects model, with a fixed effect for重量,,,,一个nd一个随机的intercept grouped bymodel_year。首先,将数据存储在表中。

tbl = table(MPG,Weight,Model_Year); lme = fitlme(tbl,'MPG ~ Weight + (1|Model_Year)');

Create predicted responses to the data.

yhat =predict(lme,tbl);

绘制原始响应和预测的响应,以了解它们的不同。按模型年对它们进行分组。

figure() gscatter(Weight,MPG,Model_Year) holdongScatter(重量,yhat,model_year,[],,'o+x')legend('70 -Data',,,,'76 -Data',,,,'82-data',,,,'70-pred',,,,'76-pred',,,,'82-pred')抓住off

图包含一个坐标轴对象。坐标轴对象有限公司ntains 6 objects of type line. These objects represent 70-data, 76-data, 82-data, 70-pred, 76-pred, 82-pred.

Load the sample data.

加载(('fertilizer.mat');

The dataset array includes data from a split-plot experiment, where soil is divided into three blocks based on the soil type: sandy, silty, and loamy. Each block is divided into five plots, where five different types of tomato plants (cherry, heirloom, grape, vine, and plum) are randomly assigned to these plots. The tomato plants in the plots are then divided into subplots, where each subplot is treated by one of four fertilizers. This is simulated data.

Store the data in a dataset array calledDS,,,,for practical purposes, and defineTomato,,,,土壤,,,,一个ndFertilizer一个s categorical variables.

DS =肥料;ds.tomato =名义(ds.tomato);ds.soil =名义(ds.soil);ds.fertierizer =名义(ds.fertilizer);

适合线性混合效应模型,其中Fertilizer一个ndTomato一个re the fixed-effects variables, and the mean yield varies by the block (soil type), and the plots within blocks (tomato types within soil types) independently.

lme = fitlme(ds,'Yield ~ Fertilizer * Tomato + (1|Soil) + (1|Soil:Tomato)');

Create a new dataset array with design values. The new dataset array must have the same variables as the original dataset array you use for fitting the modellme

DSnew = dataset(); dsnew.Soil = nominal({'Sandy';'Silty'}); dsnew.Tomato = nominal({'Cherry';'Vine'}); dsnew. Fertilizer = nominal([2;2]);

预测原始设计点处的条件和边际响应。

yhatC = predict(lme,dsnew); yhatM = predict(lme,dsnew,“有条件”,,,,错误的);[[yhatC yhatM]
一个ns =2×292.7505 111.6667 87.5891 82.6667

Load the sample data.

加载carbig

拟合每加仑英里(MPG)的线性混合效应模型,具有固定的效果,可加速,马力和气缸,以及模型年度分组的截距和加速度的潜在相关随机效应。

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.

lme = fitlmematrix(X,MPG,Z,G,'FixedEffectPredictors',,,,。。。{'Intercept',,,,“加速”,,,,'Horsepower'},'randomeffectpredictors',,,,。。。{{'Intercept',,,,“加速”}},'RandomEffectGroups',,,,{'model_year'});

创建包含可以预测响应值的数据的设计矩阵。Xnewmust have three columns as inX。The first column must be a column of 1s. And the values in the last two columns must correspond toAcceleration一个ndHorsepower, 分别。第一列znew必须是1列,第二列必须包含相同的Accelerationvalues as inXnew。The original grouping variable inGis the model year. So,Gnew必须包含模型年度的值。注意Gnewmust contain nominal values.

Xnew = [1,13.5,185;1,17,205;1,21.2,193];Znew = [1,13.5;1,17;1,21.2];% alternatively Znew = Xnew(:,1:2);Gnew=nominal([73 77 82]);

Predict the responses for the data in the new design matrices.

yhat =预测(LME,XNew,Znew,Gnew)
yhat =3×18.7063 5.4423 12.5384

现在,对于具有无关的随机效应项以进行截距和加速度的线性混合效应模型,重复此操作。首先,更改原始的随机效果设计和随机效应分组变量。然后,重新安装模型。

z={ones(406,1),Acceleration}; G = {Model_Year,Model_Year}; lme = fitlmematrix(X,MPG,Z,G,'FixedEffectPredictors',,,,。。。{'Intercept',,,,“加速”,,,,'Horsepower'},'randomeffectpredictors',,,,。。。{{'Intercept'},{“加速”}},'RandomEffectGroups',,,,{'model_year',,,,'model_year'});

Now, recreate the new random effects design,znew,以及分组变量设计,Gnew,,,,using which to predict the response values.

Znew = {[1; 1; 1],[13.5; 17; 21.2]};my =名义([[73 77 82]);gnew = {my,my};

使用新设计矩阵预测响应。

yhat =预测(LME,XNew,Znew,Gnew)
yhat =3×18.6365 5.9199 12.1247

Load the sample data.

加载carbig

拟合每加仑英里(MPG)的线性混合效应模型,具有固定的效果,可加速,马力和气缸,以及模型年度分组的截距和加速度的潜在相关随机效应。First, store the variables in a table.

tbl = table(MPG,Acceleration,Horsepower,Model_Year);

Now, fit the model usingfitlmewith the defined design matrices and grouping variables.

lme = fitlme(tbl,'mpg〜加速度 +马力 +(加速度| model_year)');

Create the new data and store it in a new table.

tblNew = table();tblNew.Acceleration = linspace(8,25)';tblnew.horsepower = linspace(最小值(马力),最大功能(马力))';tblnew.model_year = repmat(70,100,1);

linspacecreates 100 equally distanced values between the lower and the upper input limits.model_yearis fixed at 70. You can repeat this for any model year.

计算和绘制预测的值和95%的置信度限制(非同步)。

[ypred,yci,df] =预测(lme,tblNew);数字();h1 = line(tblNew.Acceleration,ypred);抓住on;h2 = plot(tblnew.Acceleration,yCI,'G-。');

图包含一个坐标轴对象。坐标轴对象有限公司ntains 3 objects of type line.

显示自由度。

DF(1)
ANS = 389

Compute and plot the simultaneous confidence bounds.

[ypred,yci,df] =预测(lme,tblNew,'同时',,,,true); h3 = plot(tblnew.Acceleration,yCI,'r--');

图包含一个坐标轴对象。坐标轴对象有限公司ntains 5 objects of type line.

显示自由度。

DF
DF=389

Compute the simultaneous confidence bounds using the Satterthwaite method to compute the degrees of freedom.

[ypred,yci,df] =预测(lme,tblNew,'同时',,,,true,'DFMethod',,,,'satterthwaite');h4 =图(tblnew.acceleration,yci,'k:');抓住offXlabel(“加速”)ylabel('Response')ylim([-50,60]) xlim([8,25]) legend([h1,h2(1),h3(1),h4(1)],“预测回应”,,,,'95%',,,,'95%sim',,,,。。。'95% Sim-Satt',,,,'Location',,,,'最好的'

图包含一个坐标轴对象。轴对象包含7个类型行的对象。这些对象表示预测的响应,95%,95%SIM,95%SIM-Satt。

显示自由度。

DF
DF=3.6001

更多关于

expand all