LinearModelFit doesn't seem to support WeightedData objects. Is there anything I can do smarter than repeating the data proportionally to the weights?

MWE: The following code,

data = WeightedData[{{1, 1}, {3, 2}, {2, 3}}, {1, 2, 3}];
LinearModelFit[data, x, x]

gives the following error message:

error message

| improve this question | | | | |
  • 2
    $\begingroup$ You're right in that it's strange that WeightedData doesn't work here, but LinearModelFit has the Weights options that you can use to specify the weights. See: reference.wolfram.com/language/ref/Weights.html $\endgroup$ – Sjoerd Smit Oct 10 '18 at 20:58
  • 2
    $\begingroup$ "I couldn't imagine..." - that's why it's good to read the docs. ;) $\endgroup$ – J. M.'s technical difficulties Oct 10 '18 at 21:07
  • 2
    $\begingroup$ Is there a particular reason for the weights? Specifically, are you considering a model where the variance increases with x such as $y=a+b x+x \epsilon$ with $\epsilon \sim N(0,\sigma^2)$? Or are the weights related to the measured precision of an observation? I ask because you might consider different options depending on what defines the weights. $\endgroup$ – JimB Oct 10 '18 at 21:28
  • 1
    $\begingroup$ "expansion factors to account for the representativity of each individual in the sample" seems kinda vague and/or arbitrary. Why can't that be included in an explicit model rather than showing up only as an option in a software function? (I'm not suggesting that weights don't exist. I'm just looking for a concrete definition.) $\endgroup$ – JimB Oct 10 '18 at 22:53
  • 2
    $\begingroup$ OK, I see. Survey weights is what you have in mind which are well-defined in finite population sample survey methods. Usually those are built into the estimators of means, proportions, and totals. The following article (stat.columbia.edu/~gelman/research/published/STS226.pdf) by Andrew Gelman might be of interest. $\endgroup$ – JimB Oct 11 '18 at 3:01

You're right in that it's strange that WeightedData doesn't work here, but LinearModelFit has the Weights options that you can use to specify the weights. This example is from the documentation of LinearModelFit (specifically the Weights section under Options):

LinearModelFit[Range[10]^2, x, x] // Normal
LinearModelFit[Range[10]^2, x, x, Weights -> 1/Range[10]] // Normal

Out[1]= -22. + 11. x

Out[2]= -13.5039 + 9.45526 x

As a general tip for finding these kinds of options, it's always worthwhile to browse the Options section in the documentation of a function if you think some logical functionality of a function is missing. Many option symbols have their own documentation page as well. Furthermore, some functions have hidden options that aren't shown in the documentation. You can see all of them by simply typing:


{ConfidenceLevel -> 19/20, IncludeConstantBasis -> True, LinearOffsetFunction -> None, NominalVariables -> None, Tolerance -> Automatic, VarianceEstimatorFunction -> Automatic, Weights -> Automatic, WorkingPrecision -> Automatic}

| improve this answer | | | | |
  • 1
    $\begingroup$ If one already has the WeightedData[] object, one can just use the "InputData" and "Weights" properties to extract what needs to be given to LinearModelFit[]. $\endgroup$ – J. M.'s technical difficulties Oct 11 '18 at 8:30
  • 1
    $\begingroup$ @J.M.issomewhatokay True, of course. That's why it's strange that it doesn't just work out-of-the-box, though. $\endgroup$ – Sjoerd Smit Oct 11 '18 at 8:47
  • $\begingroup$ @SjoerdSmit, and that's why I just thought it wasn't implemented yet in the version I work with (not the latest one, I even think I remember reading a while ago that weights weren't universally implemented), and gave up early. Thanks, that's a welcoming way of saying "read the docs". I realize I could have tried harder before asking, I was just misguided and lost hope too early this time. Sorry. $\endgroup$ – Rafael Oct 11 '18 at 10:16

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.