0
$\begingroup$

I want to use NonlinearModelFit with the following data. I tried it firstly without inserting any error bar.

 bb={{0., 10.5}, {5., 10.5}, {10., 10.3}, {15., 10.1}, {20., 10.}, {25.,  9.7}, {30., 9.5}, {35., 9.2}, {40., 8.9}, {45., 8.6}, {50.,8.2}, {55., 7.6}, {60., 7.3}, {65., 6.7}, {70., 5.9}, {75.,5.4}, {80., 4.8}, {85., 4.}, {90., 3.5}, {95., 2.9}, {100.,2.}, {105., 1.4}, {110.,0.5}, {115., -0.1}, {120., -0.8}, {140., -3.8}, {160., -6.8},{180., -9.4}, {200., -11.7}, {220., -13.5}, {240., -14.8}, {260.,-15.5}, {280., -15.5}, {300., -14.7}, {320., -13.5}, {340., -11.9},{360., -10.}, {380., -7.8}, {400., -5.4}, {420., -3.}, {440., -0.8}, {460., 1.2}, {480., 2.7}, {500., 3.8}, {520., 4.4}, {540.,4.6}, {560., 4.1}, {580., 3.4}, {600., 2.3}, {620.,0.9}, {640., -0.8}, {660., -2.6}, {680., -4.6}, {700., -6.3},{720., -7.9}, {740., -9.1}, {760., -10.2}, {780., -10.7}, {800.,  -11.}, {820., -10.8}, {840., -10.4}, {860., -9.4}, {880., -8.5},{900., -7.1}, {920., -5.8}, {940., -4.4}, {960., -2.9}, {980., -1.5},{1000., -0.5}, {1020., 0.4}, {1040., 1.}, {1060., 1.3}, {1080.,1.4}, {1100., 1.}, {1120.,0.4}, {1140., -0.4}, {1160., -1.3}, {1180., -2.4}, {1200., -3.6},{1220., -4.6}, {1240., -5.6}, {1260., -6.5}, {1280., -7.2}, {1300.,-7.8}, {1320., -8.2}, {1340., -8.1}, {1360., -8.}, {1380., -7.6},{1400., -7.1}, {1420., -6.3}, {1440., -5.6}, {1460., -4.8}, {1480.,-3.8}, {1500., -2.9}, {1520., -2.2}, {1540., -1.7}, {1560., -1.1},  {1580., -0.8}, {1600., -0.7}, {1620., -0.8}, {1640., -1.1}, {1660.,-1.5},{1680., -1.9}, {1700., -2.7}, {1720., -3.3}, {1740., -4.},{1760., -4.6}, {1780., -5.2}, {1800., -5.7}, {1820., -6.1}, {1840.,-6.4}, {1860., -6.6}, {1880., -6.4}, {1900., -6.3}, {1920., -6.},{1940., -5.6}, {1960., -5.2}, {1980., -4.8}, {2000., -4.2}, {2020.,-3.7}, {2040., -3.2}, {2060., -2.7}, {2080., -2.4}, {2100., -2.1},    {2120., -1.9}, {2140., -1.9}, {2160., -2.}, {2180., -2.3}, {2200.,-2.5}, {2220., -2.9}, {2240., -3.1}, {2260., -3.6}, {2280., -4.},{2300., -4.4}, {2320., -4.8}, {2340., -5.1}, {2360., -5.3}, {2380.,-5.5}, {2400., -5.6}, {2420., -5.5}, {2440., -5.3}, {2460., -5.2},{2480., -4.9}, {2500., -4.6}, {2520., -4.3}, {2540., -3.9}, {2560.,-3.7}, {2580., -3.3}, {2600., -2.9}, {2620., -2.8}, {2640., -2.7},{2660., -2.6}, {2680., -2.7}, {2700., -2.7}, {2720., -2.8}, {2740.,-3.}, {2760., -3.3}, {2780., -3.5}, {2800., -3.7}, {2820., -4.},     {2840., -4.3}, {2860., -4.5}, {2880., -4.8}, {2900., -4.9}, {2920.,-5.},{2940., -5.}, {2960., -4.9}, {2980., -4.8}, {3000., -4.7},{3020., -4.5}, {3040., -4.3}, {3060., -4.}, {3080., -3.9}, {3100.,-3.6}, {3120., -3.3}, {3140., -3.2}, {3160., -3.}, {3180., -2.9},{3200., -2.8}, {3220., -2.9}, {3240., -3.}, {3260., -3.1}, {3280.,-3.4}, {3300., -3.5}, {3320., -3.6}, {3340., -3.7}, {3360., -4.},{3380., -4.1}, {3400., -4.3}, {3420., -4.5}, {3440., -4.6}, {3460.,-4.6}, {3480., -4.6}, {3500., -4.6}, {3520., -4.5}, {3540., -4.5},{3560., -4.3}, {3580., -4.2}, {3600., -4.}, {3620., -3.9}, {3640.,-3.7}, {3660., -3.5}, {3680., -3.4}, {3700., -3.3}, {3720., -3.3}}

nlregcos = 
 NonlinearModelFit[bb, 
  Offf + A Exp[-ζ Subscript[ω, n] t] Cos[
     Sqrt[1 - ζ^2] Subscript[ω, n]
        t + ϕ], {{Offf, -3.9}, {A, 12}, {ζ, 
    0.08`}, {Subscript[ω, n], 0.011`}, {ϕ, 0}}, t, 
  MaxIterations -> 2000]


nlregcos[{"BestFit", "ParameterTable"}]

And that's what I got

enter image description here


I tried in the following way to insert errors (of 0.1 cm) on the $y$ axis.

err = Table[0.1, {i, 1, Length[bb]}]

    nlregcos = 
 NonlinearModelFit[bb, 
  Offf + A Exp[-ζ Subscript[ω, n] t] Cos[
     Sqrt[1 - ζ^2] Subscript[ω, n]
        t + ϕ], {{Offf, -3.9}, {A, 12}, {ζ, 
    0.08`}, {Subscript[ω, n], 0.011`}, {ϕ, 0}}, t, 
  MaxIterations -> 2000, Weights -> 1/err^2]

And I get

enter image description here

How is that possible to find exactly the same errors on coefficients? How did Mathematica found standard errors on coefficients in the first fit (where I did not insert any error)? And moreover, how come that, once that I use errors that I put in to wheight data I get exactly the same results?

Is this the correct way to insert errors in the fit? Do I have to do something different rather than just add Weights -> 1/err^2 ?

I understood the fact that, inserting errors equal for all data is like non inserting them because actually all data weight the same if they have the same error. Nevertheless, shouldn't the standard errors of parameters change if I increase the error (even if that error is the same for all data)?

So that's makes me think that this is not the proper way to insert errors in this case, what is the right procedure to do so?


EDIT Simpler example to show the doubt (I'll use NonlinearModelFit for a linear fit, which is quite nonsense but it is just to have a simple situation).

data={{0, 3}, {1, 4.5}, {2, 6.7}, {3, 8.4}, {4, 9.1}}
err=Table[0.1,{i,1,Lenght[data]}]

Case 1: no error bars

fit =NonlinearModelFit[data, a + b*t, {a, b}, t, MaxIterations -> 2000] 

fit[{"BestFit", "ParameterTable"}]

Case 2: error bars (same error for all data on $y$ axis)

 fit =NonlinearModelFit[data, a + b*t, {a, b}, t, MaxIterations -> 2000,Weights->1/err^2] 

 fit[{"BestFit", "ParameterTable"}]

In both cases I get the same result, especially the same standard errors on coefficients. enter image description here

This means that standard errors on coefficient are not influenced by error bars? That is not possible. Moreover a fit cannot be done at all if some errors are not considered.

So I would like to know here, how to insert the errors I want in the NonlinearModelFit in this very simple case?

$\endgroup$
  • $\begingroup$ I don't know where you get the term "error bars" but you should use the term "weights". But as you've discovered when the weights are the same, it is just like not using the weight option and each observation is associated with the same error variance. What weights do you want? Are the error variances proportional to a function of one of the predictor variables? Stating the form of the error variance will tell you what the weights should be. $\endgroup$ – JimB Jul 26 '16 at 19:25
2
$\begingroup$

This is more of an extended comment. The setting of the weights depends on what structure you want for the error term. For example, suppose you have the linear model

$$y=a+b x + x \epsilon$$

where $a$ and $b$ are the respective intercept and slope, $x$ is the predictor variable and $\epsilon \sim N(0,\sigma^2)$.

The weights you want are $1/x^2$. This has observations with larger variances ($x^2\sigma^2$) having smaller weights.

a = 0; (* Intercept *)
b = 1; (* Slope *)
(* Predictor variable *)
x = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
(* Random errors *)
e = RandomVariate[NormalDistribution[0, 0.3], Length[x]];
(* Generate response *)
y = a + b x + x e

(* Fit *)
data = Transpose[{x, y}];
lm = LinearModelFit[data, z, z, Weights -> (1/#^2 &)]
lm["BestFitParameters"]
lm["EstimatedVariance"]^0.5

(* Plot results *)
Show[ListPlot[data], Plot[lm[z], {z, 1, 10}],
 Plot[lm["MeanPredictionBands"], {z, 1, 10}, PlotStyle -> Dotted]]

Linear model fit with 95 pct confidence bands

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.