Parametric nonlinear models represent the relationship between a continuous response variable and one or more continuous predictor variables in the form
y = f(X,β) + ε,
where
y is an n-by-1 vector of observations of the response variable.
f is any function of X and β that evaluates each row of X along with the vector β to compute the prediction for the corresponding row of y.
X is an n-by-p matrix of predictors, with one row for each observation, and one column for each predictor.
β is a p-by-1 vector of unknown parameters to be estimated.
ε is an n-by-1 vector of independent, identically distributed random disturbances.
In contrast, nonparametric models do not attempt to characterize the relationship between predictors and response with model parameters. Descriptions are often graphical, as in the case of Decision Trees.
fitnlm
attempts to find values of the parameters β that minimize the mean squared differences between the observed responses y and the predictions of the model f(X,β). To do so, it needs a starting value beta0
before iteratively modifying the vector β to a vector with minimal mean squared error.
To begin fitting a regression, put your data into a form that fitting functions expect. All regression techniques begin with input data in an array X
and response data in a separate vector y
, or input data in a table or dataset array tbl
and response data as a column in tbl
. Each row of the input data represents one observation. Each column represents one predictor (variable).
For a table or dataset array tbl
, indicate the response variable with the 'ResponseVar'
name-value pair:
mdl = fitlm(tbl,'ResponseVar','BloodPressure');
The response variable is the last column by default.
You cannot use categorical predictors for nonlinear regression. A categorical predictor is one that takes values from a fixed set of possibilities.
Represent missing data as NaN
for both input data and response data.
For example, to create a dataset array from an Excel® spreadsheet:
ds = dataset('XLSFile','hospital.xls',... 'ReadObsNames',true);
To create a dataset array from workspace variables:
load carsmall ds = dataset(Weight,Model_Year,MPG);
To create a table from an Excel spreadsheet:
tbl = readtable('hospital.xls',... 'ReadRowNames',true);
To create a table from workspace variables:
load carsmall tbl = table(Weight,Model_Year,MPG);
For example, to create numeric arrays from workspace variables:
load carsmall X = [Weight Horsepower Cylinders Model_Year]; y = MPG;
To create numeric arrays from an Excel spreadsheet:
[X, Xnames] = xlsread('hospital.xls'); y = X(:,4); % response y is systolic pressure X(:,4) = []; % remove y from the X matrix
Notice that the nonnumeric entries, such as sex
, do not appear in X
.
There are several ways to represent a nonlinear model. Use whichever is most convenient.
The nonlinear model is a required input to fitnlm
, in the modelfun
input.
fitnlm
assumes that the response function f(X,β) is smooth in the parameters β. If your function is not smooth, fitnlm
can fail to provide optimal parameter estimates.
Function Handle to Anonymous Function or Function File
Text Representation of Formula
The function handle @modelfun
(b,x)
accepts a vector b
and matrix, table, or dataset array x
. The function handle should return a vector f
with the same number of rows as x
. For example, the function file hougen.m
computes
hougen(b,x)=b(1)x(2)−x(3)/b(5)1+b(2)x(1)+b(3)x(2)+b(4)x(3).
Examine the function by entering type hougen
at the MATLAB® command line.
function yhat = hougen(beta,x) %HOUGEN Hougen-Watson model for reaction kinetics. % YHAT = HOUGEN(BETA,X) gives the predicted values of the % reaction rate, YHAT, as a function of the vector of % parameters, BETA, and the matrix of data, X. % BETA must have 5 elements and X must have three % columns. % % The model form is: % y = (b1*x2 - x3/b5)./(1+b2*x1+b3*x2+b4*x3) % % Reference: % [1] Bates, Douglas, and Watts, Donald, "Nonlinear % Regression Analysis and Its Applications", Wiley % 1988 p. 271-272. % Copyright 1993-2004 The MathWorks, Inc. % B.A. Jones 1-06-95. b1 = beta(1); b2 = beta(2); b3 = beta(3); b4 = beta(4); b5 = beta(5); x1 = x(:,1); x2 = x(:,2); x3 = x(:,3); yhat = (b1*x2 - x3/b5)./(1+b2*x1+b3*x2+b4*x3);
You can write an anonymous function that performs the same calculation as hougen.m
.
modelfun = @(b,x)(b(1)*x(:,2) - x(:,3)/b(5))./... (1 + b(2)*x(:,1) + b(3)*x(:,2) + b(4)*x(:,3));
For data in a matrix X
and response in a vector y
:
Represent the formula using 'x1'
as the first predictor (column) in X
, 'x2'
as the second predictor, etc.
Represent the vector of parameters to optimize as 'b1'
, 'b2'
, etc.
Write the formula as 'y ~ (mathematical expressions)'
.
For example, to represent the response to the reaction data:
modelfun = 'y ~ (b1*x2 - x3/b5)/(1 + b2*x1 + b3*x2 + b4*x3)';
For data in a table or dataset array, you can use formulas represented as the variable names from the table or dataset array. Put the response variable name at the left of the formula, followed by a ~
, followed by a character vector representing the response formula.
This example shows how to create a character vector to represent the response to the reaction
data that is in a dataset array.
Load the reaction
data.
load reaction
Put the data into a dataset array, where each variable has a name given in xn
or yn
.
ds = dataset({reactants,xn(1,:),xn(2,:),xn(3,:)},... {rate,yn});
Examine the first row of the dataset array.
ds(1,:) ans = Hydrogen n_Pentane Isopentane ReactionRate 470 300 10 8.55
Write the hougen
formula using names in the dataset array.
modelfun = ['ReactionRate ~ (b1*n_Pentane - Isopentane/b5) /'... ' (1 + Hydrogen*b2 + n_Pentane*b3 + Isopentane*b4)'] modelfun = ReactionRate ~ (b1*n_Pentane - Isopentane/b5) / ... (1 + Hydrogen*b2 + n_Pentane*b3 + Isopentane*b4)
The initial vector for the fitting iterations, beta0
, can greatly influence the quality of the resulting fitted model. beta0
gives the dimensionality of the problem, meaning it needs the correct length. A good choice of beta0
leads to a quick, reliable model, while a poor choice can lead to a long computation, or to an inadequate model.
It is difficult to give advice on choosing a good beta0
. If you believe certain components of the vector should be positive or negative, set your beta0
to have those characteristics. If you know the approximate value of other components, include them in beta0
. However, if you don’t know good values, try a random vector, such as
beta0 = randn(nVars,1); % or beta0 = 10*rand(nVars,1);
The syntax for fitting a nonlinear regression model using a table or dataset array tbl
is
mdl = fitnlm(tbl,modelfun,beta0)
The syntax for fitting a nonlinear regression model using a numeric array X
and numeric response vector y
is
mdl = fitnlm(X,y,modelfun,beta0)
For information on representing the input parameters, see Prepare Data, Represent the Nonlinear Model, and Choose Initial Vector beta0.
fitnlm
assumes that the response variable in a table or dataset array tbl
is the last column. To change this, use the ResponseVar
name-value pair to name the response column.
Matlabsolutions.com provides guaranteed satisfaction with a
commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain
experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support
to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been
empanelled after extensive research and quality check.
Matlabsolutions.com provides undivided attention to each Matlab
assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services
include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work
done at the best price in industry.