Regression Commands - Maple Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : Statistics : Statistics Package : Regression : Statistics/Regression

Regression Commands

  

The Statistics package provides various commands for fitting linear and nonlinear models to data points and performing regression analysis.   The fitting algorithms are based on least-squares methods, which minimize the sum of the residuals squared.

Available Commands

ExponentialFit

fit an exponential function to data

Fit

fit a model function to data

LinearFit

fit a linear model function to data

LogarithmicFit

fit a logarithmic function to data

Lowess

produce lowess smoothed functions

NonlinearFit

fit a nonlinear model function to data

OneWayANOVA

generate a one-way ANOVA table

PolynomialFit

fit a polynomial to data

PowerFit

fit a power function to data

PredictiveLeastSquares

fit a predictive linear model function to data

RepeatedMedianEstimator

robust linear regression

Linear Fitting

• 

A number of commands are available for fitting a model function that is linear in the model parameters to given data.  For example, the model function bt2+at is linear in the parameters a and b, though it is nonlinear in the independent variable t.

• 

The LinearFit command is available for multiple general linear regression.  For certain classes of model functions involving only one independent variable, the PolynomialFit, LogarithmicFit, PowerFit, and ExponentialFit commands are available. The PowerFit and ExponentialFit commands use a transformed model function that is linear in the parameters.

Nonlinear Fitting

• 

The NonlinearFit command is available for nonlinear fitting.  An example model function is ax+ⅇby where a and b are the parameters, and x and y are the independent variables.

• 

This command relies on local nonlinear optimization solvers available in the Optimization package.  The LSSolve and NLPSolve commands in that package can also be used directly for least-squares and general nonlinear minimization.

Other Commands

• 

The general Fit command allows you to provide either a linear or nonlinear model function.  It then determines the appropriate regression solver to use.

• 

The OneWayANOVA command generates the standard ANOVA table for one-way classification, given two or more groups of observations.

Using the Regression Commands

• 

Various options can be provided to the regression commands. For example, the weights option allows you to specify weights for the data points and the output option allows you to control the format of the results.  The options available for each command are described briefly in the command's help page and in greater detail in the Statistics/Regression/Options help page.

• 

The format of the solutions returned by the regression commands is described in the Statistics/Regression/Solution help page.

• 

Most of the regression commands use methods implemented in a built-in library provided by the Numerical Algorithms Group (NAG).  The underlying computation is done in floating-point.  Either hardware or software (arbitrary precision) floating-point computation can be specified.

• 

The model function and data sets may be provided in different ways.  Full details are available in the Statistics/Regression/InputForms help page. The regression routines work primarily with Vectors and Matrices.  In most cases, lists (both flat and nested) and Arrays are also accepted and automatically converted to Vectors or Matrices.  Consequently, all output, including error messages, uses these data types.

Examples

withStatistics:

Define Vectors X and Y, containing values of an independent variable x and a dependent variable y.

X:=Vector1.2,2.1,3.1,4.0,5.7,6.6,7.2,7.9,9.1,10.3:

Y:=Vector4.6,7.7,11.5,15.4,22.2,33.1,48.1,70.6,109.0,168.4:

Find the values of a and b that minimize the least-squares error when the model function at+bⅇx is used.

Fitax+bⅇx,X,Y,x

6.02861839709607x+0.00380375570567371ⅇx

(1)

Fit a polynomial of degree 3 through this data.

PolynomialFit3,X,Y,x

3.37372868459017+9.90059487215674x2.79612412098216x2+0.336249676048196x3

(2)

Use the output option to see the residual sum of squares and the standard errors.

PolynomialFit3,X,Y,x,output=residualsumofsquares,standarderrors

47.8471318673565,6.325965104747094.473060232720560.8617838332836650.0487355015438641

(3)

Fit the model function ax+ⅇbx, which is nonlinear in the parameters.

NonlinearFitax+ⅇbx,X,Y,x

2.12883148575966x+ⅇ0.486510105685615x

(4)

Consider now an experiment where quantities x, y, and z are quantities influencing a quantity w according to an approximate relationship

w=xa+bx2y+cyz

with unknown parameters a, b, and c. Six data points are given by the following matrix, with respective columns for x, y, z, and w.

ExperimentalData:=1,1,1,2,2,2|1,2,3,1,2,3|1,2,3,4,5,6|0.531,0.341,0.163,0.641,0.713,0.040

ExperimentalData:=1110.5311220.3411330.1632140.6412250.7132360.040

(5)

We take an initial guess that the first term will be approximately quadratic in x, that b will be approximately 1, and for c we don't even know whether it's going to be positive or negative, so we guess c=0. We compute both the model function and the residuals. Also, we select more verbose operation by setting infolevel.

infolevel[Statistics]:=1:

NonlinearFitxa+bx2y+cyz,ExperimentalData,x,y,z,initialvalues=a=2,b=1,c=0,output=leastsquaresfunction,residuals

In NonlinearFit (algebraic form)

x1.147019739969680.298041864889394x2y0.0982511893429762yz,0.07270694576763000.1169743101833980.1466079923832510.01161274700576860.07703615328483880.0886489085642805

(6)

We note that Maple selected the nonlinear fitting method. Furthermore, the exponent on x is only about 1.14, and the other guesses were not very good either. However, this problem is conditioned well enough that Maple finds a good fit anyway.

Now suppose that the relationship that is used to model the data is altered as follows:

w=ax+bx2y+cyz

We adapt the calling sequence very slightly:

Fitax+bx2y+cyz,ExperimentalData,x,y,z,initialvalues=a=2,b=1,c=0,output=leastsquaresfunction,residuals

In Fit
In LinearFit (container form)

0.823072918385878x0.167910114211606x2y0.0758022678386438yz,0.04836053633562850.09490878992549990.07811753022685410.03029630857075830.1606970700378930.0978248634499976

(7)

infolevel[Statistics]:=0:

This time, Maple could select the linear fitting method, because the expression is linear in the parameters. The initial values for the parameters are not used.

Finally, consider a situation where an ordinary differential equation leads to results that need to be fitted. The system is given by

x0=a,ⅆⅆtxt=zxtb+1

where a and b are parameters that we want to find, z is a variable that we can vary between experiments, and xt is a quantity that we can measure at t=1. We perform 10 experiments at z=0.1,0.2,...,1.0, and the results are as follows.

Input:=seq0.1..1,0.1

Input:=0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0

(8)

Output:=1.932,2.092,2.090,2.416,2.544,2.638,2.894,3.188,3.533,3.822

Output:=1.932,2.092,2.090,2.416,2.544,2.638,2.894,3.188,3.533,3.822

(9)

We now need to set up a procedure that NonlinearFit can call to obtain the value for a given input value z and a given pair of parameters a and b. We do this using dsolve/numeric.

ODE:=x0=a,ⅆⅆtxt=zxtb+1

ODE:=x0=a,ⅆⅆtxt=zxtb+1

(10)

ODE_Solution:=dsolveODE,numeric,parameters=a,b,z

ODE_Solution:=procx_rkf45...end proc

(11)

We now have a procedure ODE_Solution that can compute the correct value, but we need to write a wrapper that has the form that NonlinearFit expects. We first need to call ODE_Solution once to set the parameters, then another time to obtain the value of xt at t=1, and then return this value (for more information about how this works, see dsolve/numeric). By hand, we can do this as follows:

ODE_Solutionparameters=a=1,b=0.5,z=1

a=1.,b=0.5,z=1.

(12)

ODE_Solution1

t=1.,xt=3.44630585135012

(13)

ODE_Solutionparameters=a=1,b=1,z=1

a=1.,b=1.,z=1.

(14)

ODE_Solution1

Error, (in ODE_Solution) cannot evaluate the solution past the initial point, problem may be complex, initially singular or improperly set up

Note that for some settings of the parameters, we cannot obtain a solution. We need to take care of this in the procedure we create (which we call f), by returning a value that is very far from all output points, leading to a very bad fit for these erroneous parameter values.

f := proc(zValue, aValue, bValue) global ODE_Solution, a, b, z, x, t; ODE_Solution('parameters' = [a = aValue, b = bValue, z = zValue]); try return eval(x(t), ODE_Solution(1)); catch: return 100; end try; end proc;

f:=proczValue,aValue,bValueglobalODE_Solution,a,b,z,x,t;ODE_Solution'parameters'=a=aValue,b=bValue,z=zValue;tryreturnevalxt,ODE_Solution1catch:return100end tryend proc

(15)

f1,1,0.5

3.44630585135012

(16)

We need to provide an initial estimate for the parameter values, because the fitting procedure is only performed in a local sense. We go with the values that provided a solution above: a=1,b=0.5.

NonlinearFitf,Input,Output,output=parametervector,initialvalues=1,0.5

0.7394069215465611.03081992749769

(17)

See Also

CurveFitting, Statistics, Statistics/Computation, Statistics/MaximumLikelihoodEstimate, Statistics/Regression/Options, Statistics/Regression/Solution, TimeSeriesAnalysis

References

  

Draper, Norman R., and Smith, Harry. Applied Regression Analysis. 3rd ed. New York: Wiley, 1998.


Download Help Document

Was this information helpful?



Please add your Comment (Optional)
E-mail Address (Optional)
What is ? This question helps us to combat spam