# Linear regression with weighted data?

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:

• 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 – Sjoerd Smit Oct 10 at 20:58
• "I couldn't imagine..." - that's why it's good to read the docs. ;) – J. M. is computer-less Oct 10 at 21:07
• 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. – JimB Oct 10 at 21:28
• "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.) – JimB Oct 10 at 22:53
• 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. – JimB Oct 11 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:

Options[LinearModelFit]

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

• 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[]. – J. M. is computer-less Oct 11 at 8:30
• @J.M.issomewhatokay True, of course. That's why it's strange that it doesn't just work out-of-the-box, though. – Sjoerd Smit Oct 11 at 8:47
• @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. – Rafael Oct 11 at 10:16