random
Description
Examples
Load the fisheriris sample data set.
load fisheririsThe column vector species contains three iris flower species: setosa, versicolor, and virginica. The matrix meas contains four types of measurements for the flowers: the length and width of sepals and petals in centimeters.
Fit a multinomial regression model using the measurements as the predictor data and the iris species as the response data.
mdl = fitmnr(meas,species);
mdl is a multinomial regression model object that contains the results of fitting a nominal multinomial regression model to the data.
Use the rand function to generate new predictor data by adding uniformly distributed random noise to the measurements data.
rng("default") % Set the random seed for reproducibility sz = size(meas); measNew = meas + rand(sz);
Each row of measNew corresponds to predictor data for a unique data point.
Generate random response values sampled from the multinomial distributions generated by passing measNew to the fitted model.
speciesPred = random(mdl,measNew)
speciesPred = 150×1 cell
{'versicolor'}
{'setosa' }
{'setosa' }
{'setosa' }
{'setosa' }
{'versicolor'}
{'setosa' }
{'versicolor'}
{'setosa' }
{'setosa' }
{'setosa' }
{'setosa' }
{'setosa' }
{'setosa' }
{'setosa' }
{'versicolor'}
{'setosa' }
{'versicolor'}
{'setosa' }
{'versicolor'}
{'setosa' }
{'versicolor'}
{'setosa' }
{'setosa' }
{'setosa' }
{'setosa' }
{'versicolor'}
{'setosa' }
{'setosa' }
{'versicolor'}
⋮
Display the original response data.
species
species = 150×1 cell
{'setosa'}
{'setosa'}
{'setosa'}
{'setosa'}
{'setosa'}
{'setosa'}
{'setosa'}
{'setosa'}
{'setosa'}
{'setosa'}
{'setosa'}
{'setosa'}
{'setosa'}
{'setosa'}
{'setosa'}
{'setosa'}
{'setosa'}
{'setosa'}
{'setosa'}
{'setosa'}
{'setosa'}
{'setosa'}
{'setosa'}
{'setosa'}
{'setosa'}
{'setosa'}
{'setosa'}
{'setosa'}
{'setosa'}
{'setosa'}
⋮
The outputs for speciesPred and species show that the majority of the randomly generated responses in speciesPred match the original response labels in species. The difference between measNew and meas contributes to the difference between speciesPred and species, as does the random nature of sampling the speciesPred values from the multinomial distributions associated with the predictor data.
Load the fisheriris sample data set.
load fisheririsThe column vector species contains three iris flower species: setosa, versicolor, and virginica. The matrix meas contains four types of measurements for the flowers: the length and width of sepals and petals in centimeters.
Fit a multinomial regression model using the measurements as the predictor data and the iris species as the response data.
mdl = fitmnr(meas,species);
mdl is a multinomial regression model object that contains the results of fitting a nominal multinomial regression model to the data.
Use the rand function to generate new predictor data by adding uniformly distributed random noise to the measurements data.
rng("default") % Set the random seed for reproducibility sz = size(meas); measNew = meas + 5*rand(sz);
Each row of measNew corresponds to predictor data for a unique data point.
Generate random response values sampled from the multinomial distributions generated by passing measNew to the fitted model. For each data point in measNew, generate 100 samples from the corresponding multinomial distribution, and return the results in a table of counts.
speciesPred = random(mdl,measNew,100,"counts")speciesPred=150×3 table
setosa versicolor virginica
______ __________ _________
0 31 69
100 0 0
0 100 0
0 100 0
0 100 0
0 78 22
0 100 0
0 0 100
0 98 2
100 0 0
0 100 0
0 0 100
0 0 100
0 100 0
0 100 0
0 0 100
⋮
Each row of the speciesPred table corresponds to a data point in measNew, and each table variable corresponds to a response category. The elements of speciesPred indicate how many times each species is sampled.
Input Arguments
Multinomial regression model object, specified as a MultinomialRegression model object created with the fitmnr
function.
New predictor input values, specified as a table or an
n-by-p matrix, where n is the
number of new observations, and p is the number of predictor
variables used to fit mdl.
If
XNewis a table, it must contain all the names of the predictors used to fitmdl. You can find the predictor names in themdl.PredictorNamesproperty.If
XNewis a matrix, it must have the same number of columns as the number of estimated coefficients. You can find the number of estimated coefficients in themdl.NumPredictorsproperty. You can specifyXNewas a matrix only when all names inmdl.PredictorNamesrefer to numeric predictors.
Example: random(mdl,[5.8 2.7; 5.7 2.5]) generates a simulated
response from each multinomial distribution given by evaluating the two-predictor model
mdl at the query points xq1 = [5.8 2.7] and
xq2 = [5.7 2.5].
Data Types: single | double | table
Number of trials to simulate for each data point in XNew,
specified as a positive integer scalar.
Data Types: single | double
Type for the returned simulated responses, specified as "samples"
or "counts". To return the simulated response values as a cell array
of class labels, specify type as "samples". To
return the simulated response values as a table of counts for each response category,
specify type as "counts".
Data Types: char | string
Output Arguments
Simulated responses, returned as a cell array of category labels or a table of counts.
If you specify the input argument
typeas"samples",Ysimis an r-by-m cell array of category labels. r is the number of data points inXNew, and m is the number of trials specified inNumTrials.If you specify the input argument
typeas"counts",Ysimis an r-by-N table, where N is the number of response variable categories in the data used to fitmdl.
Algorithms
The random function uses the fitted multinomial regression model
object mdl to calculate response category probabilities for each data
point in XNew. Each set of probabilities defines a multinomial
distribution from which the function randomly samples response values for the corresponding
predictor data.
Version History
Introduced in R2023a
See Also
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Sélectionner un site web
Choisissez un site web pour accéder au contenu traduit dans votre langue (lorsqu'il est disponible) et voir les événements et les offres locales. D’après votre position, nous vous recommandons de sélectionner la région suivante : .
Vous pouvez également sélectionner un site web dans la liste suivante :
Comment optimiser les performances du site
Pour optimiser les performances du site, sélectionnez la région Chine (en chinois ou en anglais). Les sites de MathWorks pour les autres pays ne sont pas optimisés pour les visites provenant de votre région.
Amériques
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)