Generate random responses from fitted generalized linear mixed-effects model
tblnew — New input data
table | dataset array
New input data, which includes the response variable, predictor
variables, and grouping
variables, specified as a table or dataset array. The predictor
variables can be continuous or grouping variables.
contain the same variables as the original table or dataset array,
used to fit the generalized linear mixed-effects model
Specify optional pairs of arguments as
the argument name and
Value is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose
Name in quotes.
BinomialSize — Number of trials for binomial distribution
ones(m,1) (default) | m-by-1 vector of positive integer values
Number of trials for binomial distribution, specified as the
comma-separated pair consisting of
an m-by-1 vector of positive integer values, where m is
the number of rows in
pair applies only to the binomial distribution. The value specifies
the number of binomial trials when generating the random response
Offset — Model offset
zeros(m,1) (default) | vector of scalar values
Model offset, specified as a vector of scalar values of length m,
where m is the number of rows in
The offset is used as an additional predictor and has a coefficient
value fixed at
Weights — Observation weights
m-by-1 vector of nonnegative scalar values
Observation weights, specified as the comma-separated pair consisting
'Weights' and an m-by-1
vector of nonnegative scalar values, where m is
the number of rows in
tblnew. If the response
distribution is binomial or Poisson, then
be a vector of positive integers.
ysim — Simulated response values
Simulated response values, returned as an m-by-1
vector, where m is the number of rows in
first generating the random-effects vector based on its fitted prior
random then generates
its fitted conditional distribution given the random effects.
into account the effect of observation weights specified when fitting
the model using
fitglme, if any.
Simulate Random Responses from a GLME Model
Load the sample data.
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 (
Processing time for each batch, in hours (
Temperature of the batch, in degrees Celsius (
Categorical variable indicating the supplier (
C) of the chemical used in the batch (
Number of defects in the batch (
The data also includes
temp_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 using
supplier as fixed-effects predictors. Include a random-effects term for intercept grouped by
factory, to account for quality differences that might exist due to factory-specific variations. The response variable
defects has 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
This corresponds to the generalized linear mixed-effects model
is the number of defects observed in the batch produced by factory during batch .
is the mean number of defects corresponding to factory (where ) during batch (where ).
, , and are the measurements for each variable that correspond to factory during batch . For example, indicates whether the batch produced by factory during batch used the new process.
and are dummy variables that use effects (sum-to-zero) coding to indicate whether company
B, respectively, supplied the process chemicals for the batch produced by factory during batch .
is a random-effects intercept for each factory 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');
random to simulate a new response vector from the fitted model.
rng(0,'twister'); % For reproducibility ynew = random(glme);
Display the first 10 rows of the simulated response vector.
ans = 10×1 3 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 of
tblnew = mfr(1:10,:);
The first 10 rows of
mfr include 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, so
newprocess = 0 for all 10 observations.
Change the value of
1 for the observations in
tblnew.newprocess = ones(height(tblnew),1);
Simulate new responses using the new input values in
ynew2 = random(glme,tblnew)
ynew2 = 10×1 2 3 5 4 2 2 2 1 2 0
Conditional Distribution Method
random generates random data from the fitted
generalized linear mixed-effects model as follows:
Sample , where is the estimated prior distribution of random effects, and is a vector of estimated covariance parameters, and is the estimated dispersion parameter.
Given bsim, for i = 1 to m, sample , where is the conditional distribution of the ith new response ynew_i given bsim and the model parameters.