Re: Automate datafitting to a series of parameterized function
- To: mathgroup at smc.vnet.net
- Subject: [mg70297] Re: [mg70277] Automate datafitting to a series of parameterized function
- From: Darren Glosemeyer <darreng at wolfram.com>
- Date: Thu, 12 Oct 2006 05:37:05 -0400 (EDT)
- References: <200610110553.BAA19424@smc.vnet.net>
The model is a linear model in the basis functions 1, x, x^2,..., so you
can use Regress to easily get this information.
This will give the fitted function and residuals for the model with n terms.
<< Statistics`LinearRegression`
fittedAndResiduals[n_] := {BestFit, FitResiduals} /. Regress[data,
x^Range[0, n], x,
RegressionReport -> {BestFit, FitResiduals} ];
The mentioned results for a constant model can then be computed directly
as follows, and for higher order models by choosing the appropriate
value of n.
res0 = fittedAndResiduals[0];
error0 = res0[[2]]^2;
avererror0 = Total[error0]/Length[data]
maxerror = Max[error0]
If there is a maximum power for the terms, say 5, that you wish to
allow, results for all those models could be obtained as follows.
Table[Block[{res = fittedAndResiduals[n], error},
error = res[[2]]^2;
{res[[1]], Total[error]/Length[data], Max[error]}], {n, 0, 5}]
Other diagnostics could also be obtained from Regress or computed from
results obtained from Regress if desired.
Darren Glosemeyer
Wolfram Research
Peng Yu wrote:
> Suppose I have some data x_i, y_i. Let us generated them by
>
> coef = Table[Random[Real, {-1, 1}], {i, 0, 4}]
> poly = Table[x^n, {n, 0, 4}]
> data = Table[{x, coef.poly}, {x, 0, 1, .01}]
>
> Let us forget how we generated the data. Now, we want to find an
> function to fit the data. The simplest way to do is using Tayler
> series. But the problem is I don't know how many terms I should keep.
> One way I can do is to try for different number of terms. I start by
> only 1 term (0th order).
>
> f[x_, a0_] := a0
> f[x_] := Evaluate@NonlinearFit[data, f[x, a0], {x}, {a0}]
> error = Apply[(f[#1] - #2)^2 &, data, {1}];
> avererror = Apply[Plus, error]/Length[data]
> maxerror = Apply[Max, error]
>
> If the above one doesn't work, I'll try 2 terms (0th and 1st order).
> f[x_, a0_, a1_] := a0 + a1 x
> f[x_] := Evaluate@NonlinearFit[data, f[x, a0, a1], {x}, {a0, a1}];
> error = Apply[(f[#1] - #2)^2 &, data, {1}];
> avererror = Apply[Plus, error]/Length[data]
> maxerror = Apply[Max, error]
>
> I can try even higher orders. But the problem is that ever time I have
> to define the function f[x_,a0_...] and supply the right function and
> parameter list to NonlinearFit. I feel it is especially difficult to
> parameterize the number of parameters to a function.
>
> Can some give some idea on how to do it if it is possible in
> Mathematica? Of cause I could write a python program to generate the
> mathematica code, and call math.exe in the command line mode. But that
> is just to much, I just want to stick with mathematica.
>
> Thanks,
> Peng
>
- References:
- Automate datafitting to a series of parameterized function
- From: "Peng Yu" <pengyu.ut@gmail.com>
- Automate datafitting to a series of parameterized function