Main Content

random

Class:GeneralizedLinearMixedModel

Generate random responses from fitted generalized linear mixed-effects model

Description

example

ysim= random(glme)returns simulated responses,ysim, from the fitted generalized linear mixed-effects modelglme, at the original design points.

ysim= random(glme,tblnew)returns simulated responses using new input values specified in the table or dataset array,tblnew.

ysim= random(___,Name,Value)returns simulated responses using additional options specified by one or moreName,Valuepair arguments, using any of the previous syntaxes. For example, you can specify observation weights, binomial sizes, or offsets for the model.

Input Arguments

expand all

Generalized linear mixed-effects model, specified as aGeneralizedLinearMixedModelobject. For properties and methods of this object, seeGeneralizedLinearMixedModel.

New input data, which includes the response variable, predictor variables, andgrouping variables, specified as a table or dataset array. The predictor variables can be continuous or grouping variables.tblnewmust contain the same variables as the original table or dataset array,tbl, used to fit the generalized linear mixed-effects modelglme.

Name-Value Arguments

Specify optional comma-separated pairs ofName,Valuearguments.Nameis the argument name andValueis the corresponding value.Namemust appear inside quotes. You can specify several name and value pair arguments in any order asName1,Value1,...,NameN,ValueN.

Number of trials for binomial distribution, specified as the comma-separated pair consisting of'BinomialSize'and anm1的向量of positive integer values, wheremis the number of rows intblnew. The'BinomialSize'name-value pair applies only to the binomial distribution. The value specifies the number of binomial trials when generating the random response values.

Data Types:single|double

Model offset, specified as a vector of scalar values of lengthm, wheremis the number of rows intblnew. The offset is used as an additional predictor and has a coefficient value fixed at1.

观察权重, specified as the comma-separated pair consisting of“重量”and anm1的向量of nonnegative scalar values, wheremis the number of rows intblnew. If the response distribution is binomial or Poisson, then“重量”must be a vector of positive integers.

Data Types:single|double

Output Arguments

expand all

Simulated response values, returned as anm1的向量, wheremis the number of rows intblnew.randomcreatesysimby first generating the random-effects vector based on its fitted prior distribution.randomthen generatesysimfrom its fitted conditional distribution given the random effects.random考虑观察体重的影响ts specified when fitting the model usingfitglme, if any.

Examples

expand all

Load the sample data.

loadmfr

This simulated data is from a manufacturing company that operates 50 factories across the world, with each factory running a batch process to create a finished product. The company wants to decrease the number of defects in each batch, so it developed a new manufacturing process. To test the effectiveness of the new process, the company selected 20 of its factories at random to participate in an experiment: Ten factories implemented the new process, while the other ten continued to run the old process. In each of the 20 factories, the company ran five batches (for a total of 100 batches) and recorded the following data:

  • Flag to indicate whether the batch used the new process (newprocess)

  • Processing time for each batch, in hours (time)

  • Temperature of the batch, in degrees Celsius (temp)

  • Categorical variable indicating the supplier (A,B, orC) of the chemical used in the batch (supplier)

  • Number of defects in the batch (defects)

The data also includestime_devandtemp_dev, which represent the absolute deviation of time and temperature, respectively, from the process standard of 3 hours at 20 degrees Celsius.

Fit a generalized linear mixed-effects model usingnewprocess,time_dev,temp_dev, andsupplieras fixed-effects predictors. Include a random-effects term for intercept grouped byfactory, to account for quality differences that might exist due to factory-specific variations. The response variabledefectshas a Poisson distribution, and the appropriate link function for this model is log. Use the Laplace fit method to estimate the coefficients. Specify the dummy variable encoding as'effects', so the dummy variable coefficients sum to 0.

The number of defects can be modeled using a Poisson distribution

defects i j Poisson ( μ i j )

This corresponds to the generalized linear mixed-effects model

log ( μ i j ) = β 0 + β 1 newprocess i j + β 2 time _ dev i j + β 3 temp _ dev i j + β 4 supplier _ C i j + β 5 supplier _ B i j + b i ,

where

  • defects i j is the number of defects observed in the batch produced by factory i during batch j .

  • μ i j is the mean number of defects corresponding to factory i (where i = 1 , 2 , . . . , 2 0 ) during batch j (where j = 1 , 2 , . . . , 5 ).

  • newprocess i j , time _ dev i j , and temp _ dev i j are the measurements for each variable that correspond to factory i during batch j . For example, newprocess i j indicates whether the batch produced by factory i during batch j used the new process.

  • supplier _ C i j and supplier _ B i j are dummy variables that use effects (sum-to-zero) coding to indicate whether companyCorB, respectively, supplied the process chemicals for the batch produced by factory i during batch j .

  • b i N ( 0 , σ b 2 ) is a random-effects intercept for each factory i that accounts for factory-specific variation in quality.

glme = fitglme(mfr,'defects ~ 1 + newprocess + time_dev + temp_dev + supplier + (1|factory)','Distribution','Poisson','Link','log','FitMethod','Laplace','DummyVarCoding','effects');

Userandomto simulate a new response vector from the fitted model.

rng(0,'twister');% For reproducibilityynew = random(glme);

Display the first 10 rows of the simulated response vector.

ynew(1:10)
ans =10×13 3 1 7 5 8 7 9 5 9

Simulate a new response vector using new input values. Create a new table by copying the first 10 rows ofmfrintotblnew.

tblnew = mfr(1:10,:);

The first 10 rows ofmfrinclude data collected from trials 1 through 5 for factories 1 and 2. Both factories used the old process for all of their trials during the experiment, sonewprocess = 0for all 10 observations.

Change the value ofnewprocessto1for the observations intblnew.

tblnew.newprocess = ones(height(tblnew),1);

Simulate new responses using the new input values intblnew.

ynew2 = random(glme,tblnew)
ynew2 =10×12 3 5 4 2 2 2 1 2 0

More About

expand all