主要内容

matchResponse

Predictor values at specified response value for linear regression model

Since R2026a

Description

vals = matchResponse(mdl,responseVal) returns a table of predictor values that yield the response value responseVal for the linear regression model mdl.

Note

This function requires Optimization Toolbox™. Global Optimization Toolbox is also required if mdl has categorical predictors whose values are not fixed using CategoricalValues.

example

vals = matchResponse(mdl,responseVal,Name=Value) specifies additional options using one or more name-value arguments. For example, you can search over a specified range of continuous predictor values.

example

Examples

collapse all

Fit a linear regression model and find a set of predictor values that yields a specific response value according to the model.

Load the carbig data set, which contains measurements of cars made in the 1970s and early 1980s. Create a table that contains the acceleration, weight, and displacement predictors, and the miles per gallon response variable.

rng(0,"twister")  % For reproducibility
load carbig
tbl = table(Acceleration,Weight,Displacement,MPG);

Fit a linear regression model by using fitlm.

mdl = fitlm(tbl,"MPG");

Find the set of predictor values that corresponds to a predicted MPG value of 30.

vals = matchResponse(mdl,30)
vals=1×4 table
    Acceleration    Weight    Displacement    MPG
    ____________    ______    ____________    ___

       13.692       1994.9       122.36       30 

The model predicts that a car with an Acceleration value of 13.7, Weight value of 1995, and Displacement value of 122.4 has an MPG value of 30.

Fit a linear regression model and find the predictor values in a localized region that yield a specified response value according to the model.

Load the hald data set. Create a table containing the response variable, which is the heat evolved during hardening of a cement mixture, and the first two predictors, which are the amount of tricalcium aluminate and tricalcium silicate in the mixture.

rng(0,"twister")  % For reproducibility
load hald
X = ingredients(:,1:2);
tbl = array2table([heat X],VariableNames=["heat" "x1" "x2"]);

Fit a linear regression model by using fitlm.

mdl = fitlm(tbl,"heat");

Find a set of predictor values in the localized region [5x110, 30x240] that corresponds to a predicted heat value of 80.

vals = matchResponse(mdl,80,LowerBounds=[5 30],UpperBounds=[10 40])
vals=1×3 table
      x1        x2      heat
    ______    ______    ____

    5.0016    30.319     80 

The model predicts a heat value of 80 for x1 = 5 and x2 = 30.3.

Fit a linear regression model and find a set of categorical and continuous predictor values that yields a specific response value according to the model.

Load the patients data set, which contains simulated data for 100 hospital patients.

load patients

Convert the Gender and Smoker predictors to categorical.

Gender = categorical(Gender);
Smoker = categorical(Smoker);

Create a table containing the Age, Gender, Smoker, Weight, and Diastolic predictors.

tbl = table(Age,Gender,Smoker,Weight,Diastolic);

Fit a linear regression model to the Diastolic predictor using fitlm.

mdl = fitlm(tbl);

Find a set of predictor values that corresponds to a Diastolic value of 80, subject to the constraints that the patient is a male between the ages of 40 and 50, who weighs between 150–200 pounds. In this case, Global Optimization Toolbox is required because the Smoker predictor is categorical and its value is not fixed. If you have Global Optimization Toolbox, you can uncomment the lines below.

% vals = matchResponse(mdl,80,LowerBounds=[40,150], ...
%    UpperBounds=[50,200],CategoricalValues={"Male" []})

If you do not have Global Optimization toolbox, you can instead perform separate searches for a set of predictor values when Smoker=false and Smoker=true.

vals = matchResponse(mdl,80,LowerBounds=[40,150], ...
UpperBounds=[50,200],CategoricalValues={"Male" categorical(false)})
vals=1×5 table
    40.3028    Male    false    180.7239    80.0000

The model predicts that a 40-year old, nonsmoker male weighing 180 pounds has the target response value of 80.

Find the predictor values when Smoker=true.

vals = matchResponse(mdl,80,LowerBounds=[40,150], ...
UpperBounds=[50,200],CategoricalValues={"Male" categorical(true)})
Warning: Exact target value was not matched. Predictor bounds or values may need to be adjusted, or the target may be infeasible for the model.
vals=1×5 table
    40.0000    Male    true    150.0000    89.8196

The function issues a warning because it cannot find a predicted response that matches the target value when Smoker=true.

Input Arguments

collapse all

Linear regression model object, specified as a LinearModel object created by using fitlm or stepwiselm, or a CompactLinearModel object created by using compact.

Response value, specified as a numeric scalar.

Example: 10.5

Data Types: single | double

Name-Value Arguments

collapse all

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is 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.

Example: vals = matchResponse(mdl,responseVal,Options=myOptions) uses optimization options in the options object myOptions.

Fixed values for the categorical predictors, specified as a numeric vector, logical vector, string vector, categorical vector, cell array of character vectors, cell array, or table. The elements of CategoricalValues must have the same order as the categorical predictors in mdl.VariableNames. If an element of CategoricalValues is empty ([]), the function searches over all the unique values of the corresponding predictor in mdl.Variables.

Example: CategoricalValues={[] "male"}

Data Types: single | double | logical | char | string | cell | categorical | table

Optimization options, specified as an options object. If mdl has categorical predictors whose values are not fixed with CategoricalValues, specify options for the ga solver. Otherwise, specify options for the fmincon solver. For more information about optimization options objects, see optimoptions (Optimization Toolbox).

Example: Options=myOptions

Lower bounds for the continuous predictors, specified as a numeric vector with length equal to the number of continuous predictors in mdl. Use this option to specify the lower bounds of the search range. The values in LowerBounds must follow the order of the predictors in mdl.VariableInfo. If you do not specify LowerBounds, the function searches down to the minimum value of each continuous predictor in mdl.Variables.

Example: LowerBounds=[1 -Inf 0]

Data Types: single | double

Upper bounds for the continuous predictors, specified as a numeric vector with length equal to the number of continuous predictors in mdl. Use this option to specify the upper bounds of the search range. The values in UpperBounds must follow the order of the predictors in mdl.VariableInfo. If you do not specify UpperBounds, the function searches up to the maximum value of each continuous predictor in mdl.Variables.

Example: UpperBounds=[1 Inf 0]

Data Types: single | double

Output Arguments

collapse all

Predictor values at the specified response value, returned as a table. If more than one combination of predictor values yields responseVal, the function returns the combination that has the smallest response variance. The response variance is sum(X*Covar .* X), where X is the design matrix (see x2fx) and Covar is the coefficient covariance matrix of mdl.

Version History

Introduced in R2026a