fit a model function to data - Maple Help

Online Help

All Products    Maple    MapleSim


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

Statistics[Fit] - fit a model function to data

Calling Sequence

Fit(f, X, Y, v, options)

Fit(f, XY, v, options)

Parameters

f

-

algebraic; model function

X

-

Vector or Matrix; values of independent variable(s)

Y

-

Vector; values of dependent variable

XY

-

Matrix; values of independent and dependent variables

v

-

name or list(names); name(s) of independent variables in the component functions

options

-

(optional) equation(s) of the form option=value where option is one of output, weights, or initialvalues; specify options for the Fit command

Description

• 

The Fit command fits a model function to data by minimizing the least-squares error. Consider the model y=fx1,x2,...,xn; a1,a2,...,am, where y is the dependent variable and f is the model function of n independent variables x1,x2,...,xn, and m model parameters a1,a2,...,am. Given k data points, where each data point is an (n+1)-tuple of numerical values for x1,x2,...,xn,y, the Fit command finds values of the model parameters such that the sum of the k residuals squared is minimized.  The ith residual is the value of yfx1,x2,...,xn; a1,a1,...,am evaluated at the ith data point.

• 

The first parameter f is an algebraic expression in the independent variables x1,x2,...,xn and the model parameters a1,a2,...,am.

• 

In the first calling sequence, the second parameter X is a Matrix containing the values of the independent variables.  Row i in the Matrix contains the n values for the ith data point while column j contains all values of the single variable xj. If there is only one independent variable, X can be either a Vector or a k-by-1 Matrix.  The third parameter Y is a Vector containing the k values of the dependent variable y.

• 

In the second calling sequence, the second parameter XY is a Matrix containing the values of both the independent and the dependent variables, consisting of k rows and n + 1 columns. The first n columns correspond to X, the final column to Y. That is, the ith row of XY contains first the n values for the ith data point of variables x1 through xn, then the value of the dependent variable y.

• 

The parameters X, Y, and XY can also be specified as lists or Arrays; for details, see the Input Forms help page.

• 

The parameter v is a list of the independent variable names used in f.  If there is only one independent variable, then v can be a single name.  The order of the names in the list must match exactly the order in which the independent variable values are placed in the columns of X.

• 

The Fit command returns the model function, with the final parameter values, in terms of the independent variables. Additional results or a solution module that allows you to query for various settings and results can be obtained with the output option.  For more information, see the Statistics/Regression/Solution help page.

• 

The Fit command determines if the model function is linear or nonlinear in the model parameters.  (Note that a model function can be nonlinear in the independent variables but linear in the model parameters.)  It then calls either Statistics[LinearFit] or Statistics[NonlinearFit]. The most commonly used options are described below.  Additional options accepted by LinearFit or NonlinearFit are passed directly to those commands. Note in particular the (recommended) use of the initialvalues option.

• 

The Fit command accepts the model function only as an algebraic expression.  Different input forms, allowing for greater flexibility and efficiency, are offered by the LinearFit and NonlinearFit commands.  For more information, see the Input Forms help page.

Options

  

The options argument can contain one or more of the options shown below.  These options are described in more detail on the Statistics/Regression/Options help page.

• 

output = name or string -- Specify the form of the solution.  The output option can take as a value the name solutionmodule, or one of the following names (or a list of these names): AtkinsonTstatistic, confidenceintervals, CookDstatistic, degreesoffreedom, externallystandardizedresiduals, internallystandardizedresiduals, leastsquaresfunction, leverages, parametervalues, parametervector, residuals, residualmeansquare, residualstandarddeviation, residualsumofsquares, standarderrors, variancecovariancematrix. For more information, see the Statistics/Regression/Solution help page.

• 

svdtolerance = realcons(nonnegative) -- Set the tolerance that determines whether a singular-value decomposition is performed.

• 

weights = Vector -- Provide weights for the data points.

Notes

• 

The Fit command uses various methods implemented in a built-in library provided by the Numerical Algorithms Group (NAG). The underlying computation is done in floating-point; therefore, all data points must have type realcons and all returned solutions are floating-point, even if the problem is specified with exact values.  For more information about numeric computation in the Statistics package, see the Statistics/Computation help page.

• 

See infolevel[Statistics] to 1 or higher to see messages about the progress of the solvers.  In particular, these userinfo messages indicate whether the LinearFit command or the NonlinearFit command is being used.

• 

For fitting a data sample to a distribution, see MaximumLikelihoodEstimate.

Examples

withStatistics:

X:=Vector1,2,3,4,5,6,datatype=float:

Y:=Vector2,3,4.8,10.2,15.6,30.9,datatype=float:

Fit a model that is linear in the parameters.

Fita+bt+ct2,X,Y,t

6.629999999999995.37464285714286t+1.53392857142857t2

(1)

Fit a model that is nonlinear in the parameters.

Fita+bⅇct,X,Y,t

0.887576142919275+0.606352318207693ⅇ0.649251558313311t

(2)

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

(3)

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

(4)

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

(5)

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.

See Also

CurveFitting, Statistics, Statistics/Computation, Statistics/MaximumLikelihoodEstimate, Statistics/Regression, Statistics/Regression/InputForms, Statistics/Regression/Options, Statistics/Regression/Solution, Statistics[LinearFit], Statistics[NonlinearFit]


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