Main Content

random

Simulate responses with random noise for censored linear regression model

Since R2025a

    Description

    ysim = random(mdl,Xnew) simulates responses to the predictor data in Xnew using the censored linear regression model mdl, with added random noise.

    example

    Examples

    collapse all

    Load the readmissiontimes sample data.

    load readmissiontimes

    The variables Age and ReadmissionTime contain data for patient age and time of readmission. The Censored variable contains censoring information for ReadmissionTime.

    Save Age and ReadmissionTime in a table, and fit a censored linear regression model to the data.

    tbl = table(Age,ReadmissionTime);
    mdl = fitlmcens(tbl,Censoring=Censored);

    mdl is a CensoredLinearModel object that contains the results of fitting a linear model to the censored data.

    Create new predictor data.

    Age_new = linspace(min(Age),max(Age),100)';

    Calculate the fitted responses, and simulate responses with random noise at the new predictor data.

    t = feval(mdl,Age_new);
    tsim = random(mdl,Age_new);

    Plot the simulated values and the input data together with the predicted response curve.

    plot(Age,ReadmissionTime,"rx",Age_new,tsim,"bo",Age_new,t,"g")
    legend("Data","Simulated response with noise","Predicted response", ...
        Location="best")

    Figure contains an axes object. The axes object contains 3 objects of type line. One or more of the lines displays its values using only markers These objects represent Data, Simulated response with noise, Predicted response.

    The plot shows that the simulated responses have the same distribution around the fitted curve as the input data.

    Input Arguments

    collapse all

    Censored linear regression model, specified as a CensoredLinearModel object created using fitlmcens, or a CompactCensoredLinearModel object created using fitlmcens and compact.

    New predictor input values, specified as a table or matrix. Each row of Xnew corresponds to one observation, and each column corresponds to one variable.

    • If Xnew is a table, it must contain predictors that have the same names as predictors in the PredictorNames property of mdl.

    • If Xnew is a matrix, it must have the same number of variables (columns) in the same order as the predictor input used to create mdl. All variables used to create mdl must be numeric. To treat numerical predictors as categorical, specify the predictors using the CategoricalVars name-value argument when you create mdl.

    Note that Xnew must also contain any predictor variables not used as predictors in the fitted model.

    Data Types: single | double | table

    Output Arguments

    collapse all

    Simulated response values, returned as a numeric vector. The simulated values are the predicted response values at Xnew perturbed by random noise. The noise is independent and normally distributed, with mean equal to zero and variance equal to mdl.Sigma2.

    Alternative Functionality

    For predictions without random noise, use predict or feval. These two functions give the same predictions.

    • predict accepts a single input argument containing all predictor variables, and gives confidence intervals on its predictions.

    • feval accepts multiple input arguments, with one input for each predictor variable.

    Version History

    Introduced in R2025a