8
$\begingroup$

Suppose I have the following data:

data = {{{4.05, -5.06579}, ErrorBar[0.180393]},
        {{ 4.1, -4.93148}, ErrorBar[0.191881]},
        {{4.15, -5.14983}, ErrorBar[0.201101]}}

and I fit it to a constant with weights that are 1/the error bar as follows:

fit = LinearModelFit[ data[[All,1]], 1, t, Weights -> 1/( Sequence@@@data[[All,2]] )^2 ]

Then I know I can recover the actual data points via fit["Data"]. Is it possible to recover the weights used in the fit? I have searched through the documentation extensively and none of the options of FittedModel seem to produce what I'm looking for (and the weights don't seem to be stored in the same way when I look at FullForm@fit).

I'd like to accomplish this because then my analysis can be independent of the data set -- I could just pass around the FittedModel output objects themselves.

$\endgroup$
3
  • $\begingroup$ I'm confused. Since you seem to be providing the fitting function with explicit values for the weights that you calculate from data, can't you just recalculate the weights in the same way? $\endgroup$
    – MarcoB
    Sep 2, 2015 at 0:48
  • 1
    $\begingroup$ This is an extremely simplified example. In reality, I have many many data sets, and life would be tremendously simplified if I didn't have to keep track of which fit came from which set. That is, I'd love to generate the fits and then be able to (in principle) throw away data. $\endgroup$
    – evanb
    Sep 2, 2015 at 0:52
  • $\begingroup$ I see. Well, interesting question, and we learned something from Belisarius' answer! If you haven't seen it already, you might also be interested in the following tutorial on using Weights together with VarianceEstimatorFunction : How to | Fit Models with Measurement Errors. $\endgroup$
    – MarcoB
    Sep 2, 2015 at 14:26

1 Answer 1

5
$\begingroup$

The weights are there, but not documented.
Please beware that the following may not work in future releases. I tested it on v9.

data = {{{4.05, -5.06579}, ErrorBar[0.180393]}, {{4.1, -4.93148}, ErrorBar[0.191881]},
        {{4.15, -5.14983}, ErrorBar[0.201101]}};
w    =  1/data[[All, 2, 1]]^2;
fit  = LinearModelFit[data[[All, 1]], 1, t, Weights -> w];
fit[[2, 1]] == w

(* True *)

Note that NonlinearModelFit has a different internal structure:

nlFit = NonlinearModelFit[data[[All, 1]], 1, {t}, Weights -> w];
nlFit[[4]] 
(*
 Weights -> {30.7299, 27.1604, 24.727}
*)
$\endgroup$
5
  • 3
    $\begingroup$ (+1) w = 1/(Sequence @@@ data[[All, 2]])^2 can be simplified to w = 1/data[[All, 2, 1]]^2. $\endgroup$ Sep 2, 2015 at 2:11
  • $\begingroup$ Works in v10, too! Thanks! $\endgroup$
    – evanb
    Sep 2, 2015 at 2:24
  • $\begingroup$ @AlexeyPopkov Thanks. Editing :) $\endgroup$ Sep 2, 2015 at 2:41
  • $\begingroup$ @belisarius There may be differences between your version and v. 10.2 that I am using. In my version, the NonlinearModelFit expression you use doesn't work. Something like NonlinearModelFit[data[[All, 1]], a, a, t, Weights -> w] will work instead. Also, in my version nlFit[[4]] returns something abstruse; the Weights can be retrieved with nlFit[[2,1]] just like in the LinearModelFit result. $\endgroup$
    – MarcoB
    Sep 2, 2015 at 14:16
  • $\begingroup$ @MarcoB Thanks! I don't have a Mma v10 installed to test it :( A pity $\endgroup$ Sep 2, 2015 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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