stats[fit, leastsquare]
Fit a curve to data using the least square method
Calling Sequence
Parameters
Description
Examples
stats[fit, leastsquare[vars, eqn, parms]]( data)
fit[leastsquare[vars, eqn, parms]]( data)
data
-
list of statistical lists
vars
list of variables, corresponding, in order, to the lists in data
eqn
The equation to fit (optional, default = a linear equation with the last variable in vars as dependent variable and with a constant term).
parms
Set of parameters that will be replaced (optional, default = indets(eqn) minus op(vars)).
Important: The stats package has been deprecated. Use the superseding package Statistics instead.
The function leastsquare of the subpackage stats[fit, ...] fits a curve to the given data using the method of least squares.
The equation to fit must be linear in the unknown parameters. The equation itself need not be linear. For example, y=a⁢x2+b⁢x+c with the parameters a, b, c is accepted. Note that some equations which have their parameters appearing nonlinearly can be transformed to linear ones. y=a⁢ⅇb⁢x can be transformed into ln⁡y=A+b⁢x, where A=log⁡a. The leastsquares command does not apply this transformation automatically, but the Statistics[ExponentialFit] command can be used instead.
Missing data and ranges cannot be handled.
Weighted data are handled in the following fashion. The weight associated with the dependent variable is the weight given to the corresponding point. The weight specifications corresponding to the independent variables are ignored.
Data fitting routines are also available in the Statistics package. For more information, see the Statistics[Regression] help page.
The command with(stats[fit],leastsquare) allows the use of the abbreviated form of this command.
with⁡stats:
fitleastsquarex,y,z⁡1,2,3,5,2,4,6,8,3,5,7,10
z=x+y2+1
Here's an example using Weight
fitleastsquarex,y,z⁡1,2,3,5,5,5,2,4,6,8,8,8,3,5,7,10,15,15
z=13⁢x3−7⁢y6+1
fitleastsquarex,y,z⁡1,2,3,5,5,2,4,6,8,8,3,5,7,10,Weight⁡15,2
A fit to a quadratic:
Xvalues≔1,2,3,4
Yvalues≔0,6,14,24
eq_fit≔fitleastsquarex,y,y=a⁢x2+b⁢x+c,a,b,c⁡Xvalues,Yvalues
eq_fit≔y=x2+3⁢x−4
The {a,b,c} parameter is optional in this case, since there are no extra Maple variables in the equation (compare with y=a*x^2+b*x+c+Pi*x^3, where Pi is definitely not a parameter.)
eq_fit≔fitleastsquarex,y,y=a⁢x2+b⁢x+c⁡Xvalues,Yvalues
Transform this into a procedure
eq_function≔unapply⁡rhs⁡eq_fit,x
eq_function≔x↦x2+3⋅x−4
Then give the predicted values (we could have used map() in this case, since the data does not involve classes or weights)
Yvalues_predicted≔transformapplyeq_function⁡Xvalues
Yvalues_predicted≔0,6,14,24
Find the residuals:
Residuals≔transformmultiapplyx,y↦x−y⁡Yvalues,Yvalues_predicted
Residuals≔0,0,0,0
The residuals are all zero since all the points fall on the quadratic.
See Also
describe(deprecated)[linearcorrelation]
Statistics
Statistics[Regression]
Download Help Document