www.gusucode.com > stats 源码程序 matlab案例代码 > stats/PredictOrSimulateResponsesUsingANonlinearModelExample.m

    %% Predict or Simulate Responses Using a Nonlinear Model
% This example shows how to use the methods |predict| , |feval| , and
% |random| to predict and simulate responses to new data.
%%
% Randomly generate a sample from a  Cauchy distribution.

% Copyright 2015 The MathWorks, Inc.

rng('default')
X = rand(100,1);
X = tan(pi*X - pi/2);
%%
% Generate the response according to the model |y = b1*(pi /2 + atan((x -
% b2) / b3))| and add noise to the response.
modelfun = @(b,x) b(1) * ...
    (pi/2 + atan((x - b(2))/b(3)));
y = modelfun([12 5 10],X) + randn(100,1);
%%
% Fit a model starting from the arbitrary parameters _b_ = [1,1,1].
beta0 = [1 1 1]; % An arbitrary guess
mdl = fitnlm(X,y,modelfun,beta0)
%%
% The fitted values are within a few percent of the parameters [12,5,10].
%%
% Examine the fit.
plotSlice(mdl)
%% predict
% The |predict| method predicts the mean responses and, if requested, gives
% confidence bounds. Find the predicted response values and predicted
% confidence intervals about the response at _X_ values [-15;5;12].
Xnew = [-15;5;12];
[ynew,ynewci] = predict(mdl,Xnew)
%%
% The confidence intervals are reflected in the slice plot.
%% feval
% The |feval| method predicts the mean responses. |feval| is often more
% convenient to use than predict when you construct a model from a dataset
% array.
%%
% Create the nonlinear model from a dataset array.
ds = dataset({X,'X'},{y,'y'});
mdl2 = fitnlm(ds,modelfun,beta0);
%%
% Find the predicted model responses (CDF) at _X_ values [-15;5;12].
Xnew = [-15;5;12];
ynew = feval(mdl2,Xnew)
%% random
% The |random| method simulates new random response values, equal to the
% mean prediction plus a random disturbance with the same variance as the
% training data.
Xnew = [-15;5;12];
ysim = random(mdl,Xnew)
%%
% Rerun the random method. The results change.
ysim = random(mdl,Xnew)