Main Content

随机的

Class:线性混合模型

Generate random responses from fitted linear mixed-effects model

Description

例子

ys一世m=随机(lmereturns a vector of simulated responsesys一世m来自拟合的线性混合效应模型lmeat the original fixed- and random-effects design points, used to fitlme

随机的s一世mulates new random-effects vector and new observation errors. So, the simulated response is

y s 一世 m = X β ^ + z b ^ + ε ,,,,

在哪里 β ^ 一世sthe estimated fixed-effects coefficients, b ^ 一世sthe new random effects, andε是新的观察错误。

随机的also accounts for the effect of observation weights, if you use any when fitting the model.

例子

ys一世m=随机(lme,,,,tblnewreturns a vector of simulated responsesys一世m来自拟合的线性混合效应模型lme在新表或数据集数组中的值tblnew。Use a table or dataset array for随机的一世f you use a table or dataset array for fitting the modellme

ys一世m=随机(lme,,,,XNew,,,,znewreturns a vector of simulated responsesys一世m来自拟合的线性混合效应模型lmeat the values in the new fixed- and random-effects design matrices,XNewandznew, 分别。znewcan also be a cell array of matrices. Use the matrix format for随机的如果您使用设计矩阵拟合模型lme

例子

ys一世m=随机(lme,,,,XNew,,,,znew,,,,Gnewreturns a vector of simulated responsesys一世m来自拟合的线性混合效应模型lmeat the values in the new fixed- and random-effects design matrices,XNewandznew分别和分组变量Gnew

znewandGnewcan also be cell arrays of matrices and grouping variables, respectively.

Input Arguments

expand all

Linear mixed-effects model, specified as a线性混合模型目的constructed usingfitlmeorfitlmematrix

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

新的固定效应设计矩阵,指定为n-by-p矩阵,,,,在哪里n一世sthe number of observations andp是固定预测变量的数量。每一行X对应于一个观察结果,每列的X对应于一个变量。

数据类型:单身的|double

新的随机效应设计,指定为n-by-矩阵或单元格r设计矩阵z{r},,,,在哪里r=1, 2, ...,r。如果znew是一个单元阵列,然后eachz{r}一世sann-by-((r)矩阵,,,,在哪里n是观察的数量,并且((r)一世sthe number of random predictor variables.

数据类型:单身的|double|cell

Newgrouping variable or variables,,,,specified as a vector or a cell array, of lengthr,用于拟合线性混合效应模型的分组变量,lme

随机的将每个分组变量的所有级别视为新级别。它为每个分组变量的每个级别绘制一个独立的随机效应向量。

数据类型:单身的|double|categorical|逻辑|char|细绳|cell

输出参数

expand all

模拟响应值,返回n-b-1矢量,,,,在哪里n一世sthe number of observations.

Examples

expand all

Load the sample data.

加载('肥料);

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.

将数据存储在数据集数组中DS,,,,for practical purposes, and defineTomato,,,,土壤,,,,andFertilizeras categorical variables.

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

适合线性混合效应模型,其中FertilizerandTomatoare 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)');

Generate random response values at the original design points. Display the first five values.

RNG(123,'twister'% For reproducibilityysim =随机(LME);ys一世m((1:5)
ans =5×1114.8785 134.2018 154.2818 169.7554 84.6089

Load the sample data.

加载carsmall

Fit a linear mixed-effects model, with a fixed-effects for重量,,,,and a random intercept grouped byModel_Year。首先,将数据存储在表中。

tbl = table(MPG,Weight,Model_Year); lme = fitlme(tbl,'mpg〜重量 +(1 | model_year)');

使用原始数据随机生成响应。

RNG(123,'twister'% For reproducibilityys一世m=随机(lme,tbl);

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

fige()gscatter(重量,mpg,model_year)保持ongScatter(重量,ysim,model_year,[],,'o+x') 传奇('70-data',,,,'76 -Data',,,,'82-data',,,,'70-sim',,,,'76-sim',,,,'82 -sim')holdoff

图包含一个轴对象。轴对象包含6个类型行的对象。这些对象代表70-DATA,76-DATA,82-DATA,70-SIM,76-SIM,82-SIM。

请注意,第82年的模拟随机响应值低于当年的原始数据。这可能是由于第82年的模拟随机效应低于原始数据中估计的随机效应。

Load the sample data.

加载('肥料);

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.

将数据存储在数据集数组中DS,,,,for practical purposes, and defineTomato,,,,土壤,,,,andFertilizeras categorical variables.

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

适合线性混合效应模型,其中FertilizerandTomatoare 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';“粉质”;“粉质”});DSnew.Tomato = nominal({'Cherry';'藤蔓';'李子'});DSnew.Fertilizer = nominal([2;2;4]);

Generate random responses at the new points.

RNG(123,'twister'% For reproducibilityys一世m=随机(lme,dsnew)
ys一世m=3×199.6006 101.9911 161.4026

Load the sample data.

加载卡比格

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

首先,准备设计矩阵以拟合线性混合效应模型。

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',,,,“加速”,,,,'马力'},'randomeffectpredictors',,,,...{{'Intercept',,,,“加速”}},'randomeffectgroups',,,,{'Model_Year'});

创建包含可以预测响应值的数据的设计矩阵。XNew必须有三列X。The first column must be a column of 1s. And the values in the last two columns must correspond toAccelerationandHorsepower, 分别。The first column ofznew必须是1列,第二列必须包含相同的Accelerationvalues as inXNew。The original grouping variable inG一世sthe 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];gnew =名义([[73 77 82]);

Generate random responses for the data in the new design matrices.

RNG(123,'twister'% For reproducibilityysim = Random(LME,XNew,Znew,Gnew)
ys一世m=3×115.7416 10.6085 6.8796

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

z = {一个(406,1),加速};g = {model_year,model_year};lme = fitlmematrix(x,mpg,z,g,'FixedEffectPredictors',,,,...{'Intercept',,,,“加速”,,,,'马力'},'randomeffectpredictors',,,,...{{'Intercept'},{“加速”}},'randomeffectgroups',,,,{'Model_Year',,,,'Model_Year'});

Now, recreate the new random effects design,znew,以及分组变量设计,Gnew,使用该预测响应值。

znew = {[1;1;1],[13.5;17;21.2]}; MY = nominal([73 77 82]); Gnew = {MY,MY};

使用新设计矩阵生成随机响应。

RNG(123,'twister'% For reproducibilityysim = Random(LME,XNew,Znew,Gnew)
ys一世m=3×116.8280 10.4375 4.1027