Documentation

predict

Class:NonLinearModel

Predict response of nonlinear regression model

Syntax

ypred = predict(mdl,Xnew)
[ypred,开办]= predict(mdl,Xnew)
[ypred,开办]= predict(mdl,Xnew,Name,Value)

Description

ypred= predict(mdl,Xnew)returns the predicted response of themdlnonlinear regression model to the points inXnew.

[ypred,yci] = predict(mdl,Xnew)returns confidence intervals for the true mean responses.

[ypred,yci] = predict(mdl,Xnew,Name,Value)predicts responses with additional options specified by one or moreName,Valuepair arguments.

Input Arguments

mdl

Nonlinear regression model, constructed byfitnlm.

Xnew

Points at whichmdlpredicts responses.

  • IfXnewis a table or dataset array, it must contain the predictor names inmdl.

  • IfXnewis a numeric matrix, it must have the same number of variables (columns) as was used to createmdl. Furthermore, all variables used in creatingmdlmust be numeric.

Name-Value Pair Arguments

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

'Alpha'

Positive scalar from0to1. Confidence level ofyciis 100(1 –alpha)%.

Default:0.05, meaning a 95% confidence interval.

'Prediction'

Type of prediction:

  • 'curve'predictpredicts confidence bounds for the fitted mean values.

  • 'observation'predictpredicts confidence bounds for the new observations. This results in wider bounds because the error in a new observation is equal to the error in the estimated mean value, plus the variability in the observation from the true mean.

For details, seepolyconf.

Default:'curve'

'Simultaneous'

Logical value specifying whether the confidence bounds are for all predictor values simultaneously (true), or hold for each individual predictor value (false). Simultaneous bounds are wider than separate bounds, because it is more stringent to require that the entire curve be within the bounds than to require that the curve at a single predictor value be within the bounds.

For details, seepolyconf.

Default:false

“重量”

Vector of real, positive value weights or a function handle.

  • If you specify a vector, then it must have the same number of elements as the number of observations (or rows) inXnew.

  • If you specify a function handle, then the function must accept a vector of predicted response values as input, and return a vector of real positive weights as output.

Given weights,W,predictestimates the error variance at observationibyMSE*(1/W(i)), where MSE is the mean squared error.

Default:No weights

Output Arguments

ypred

Predicted mean values atXnew.ypredis the same size as each component ofXnew.

yci

Confidence intervals, a two-column matrix with each row providing one interval. The meaning of the confidence interval depends on the settings of the name-value pairs.

Examples

expand all

Create a nonlinear model of car mileage as a function of weight, and predict the response.

Create an exponential model of car mileage as a function of weight from thecarsmalldata. Scale the weight by a factor of 1000 so all the variables are roughly equal in size.

loadcarsmallX = Weight; y = MPG; modelfun ='y ~ b1 + b2*exp(-b3*x/1000)'; beta0 = [1 1 1]; mdl = fitnlm(X,y,modelfun,beta0);

Create predicted responses to the data.

Xnew = X; ypred = predict(mdl,Xnew);

Plot the original responses and the predicted responses to see how they differ.

plot(X,y,'o',X,ypred,'x') legend('Data','Predicted')

Create a nonlinear model of car mileage as a function of weight, and examine confidence intervals of some responses.

Create an exponential model of car mileage as a function of weight from thecarsmalldata. Scale the weight by a factor of 1000 so all the variables are roughly equal in size.

loadcarsmallX = Weight; y = MPG; modelfun ='y ~ b1 + b2*exp(-b3*x/1000)'; beta0 = [1 1 1]; mdl = fitnlm(X,y,modelfun,beta0);

Create predicted responses to the smallest, mean, and largest data points.

Xnew = [min(X);mean(X);max(X)]; [ypred,yci] = predict(mdl,Xnew)
ypred = 34.9469 22.6868 10.0617 yci = 32.5212 37.3726 21.4061 23.9674 7.0148 13.1086

Generate sample data from the nonlinear regression model

y = b 1 + b 2 exp { b 3 x } + ε ,

whereb1,b2, andb3are coefficients, and the error term is normally distributed with mean 0 and standard deviation 0.5.

modelfun = @(b,x)(b(1)+b(2)*exp(-b(3)*x)); rng('default')% for reproducibilityb = [1;3;2]; x = exprnd(2,100,1); y = modelfun(b,x) + normrnd(0,0.5,100,1);

Fit the nonlinear model using robust fitting options.

opts = statset('nlinfit'); opts.RobustWgtFun ='bisquare'; b0 = [2;2;2]; mdl = fitnlm(x,y,modelfun,b0,'Options',opts);

Plot the fitted regression model and simultaneous 95% confidence bounds.

xrange = [min (x): . 01:马克斯(x)) ';[ypred,开办]= predict(mdl,xrange,'Simultaneous',true); figure() plot(x,y,'ko')% observed dataholdonplot(xrange,ypred,'k','LineWidth',2) plot(xrange,yci','r--','LineWidth',1.5)

Load sample data.

S = load('reaction'); X = S.reactants; y = S.rate; beta0 = S.beta;

Specify a function handle for observation weights, then fit the Hougen-Watson model to the rate data using the specified observation weights function.

a = 1; b = 1; weights = @(yhat) 1./((a + b*abs(yhat)).^2); mdl = fitnlm(X,y,@hougen,beta0,“重量”,weights);

Compute the 95% prediction interval for a new observation with reactant levels[100,100,100]using the observation weight function.

[ypred,开办]= predict(mdl,[100,100,100],'Prediction','observation',...“重量”,weights)
ypred = 1.8149 yci = 1.5264 2.1033

Tips

  • For predictions with added noise, userandom.

  • For a syntax that can be easier to use with models created from tables or dataset arrays, tryfeval.

References

[1] Lane, T. P. and W. H. DuMouchel. "Simultaneous Confidence Intervals in Multiple Regression."The American Statistician. Vol. 48, No. 4, 1994, pp. 315–321.

[2] Seber, G. A. F., and C. J. Wild.Nonlinear Regression. Hoboken, NJ: Wiley-Interscience, 2003.

Was this topic helpful?