Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Linear Regression with Weights 7

Status
Not open for further replies.

DRWeig

Electrical
Apr 8, 2002
3,004
Group,

Does one of you perhaps have a VBA routine for linear regression with weighting, without using any built-in Excel functions or plug-ins (boss request).

I've attached a very simple example of data. My program is much more complex, but I've never dealt with weighted regressions before without using a "black box" plug-in for Excel.

If you have something, or can do it without much trouble, I'd be most appreciative.

Best to you,

Goober Dave

Haven't see the forum policies? Do so now: Forum Policies
 
 http://files.engineering.com/getfile.aspx?folder=ff54b63e-9fda-4685-9f88-e89e5cb1cf3f&file=Simple_Weighted_Linear_Regression.xlsx
Replies continue below

Recommended for you

Doug,

I might not be understanding you
weighted_variance2_kharfz.png


The last part results in W*(y-X*B){sup]2[/sup], so the square of deltas are weighted, not the deltas.

Since the squared deltas are more heavily moved by large deltas, it makes sense to beat them down directly, and to boost squares of small deltas directly

TTFN (ta ta for now)
I can do absolutely anything. I'm an expert! faq731-376 forum1529 Entire Forum list
 
IDS- sorry I missed your point. Errors are squared and its just an issue of whether you square the weighting factor or not. I understand now.

After two beers, it seems like a better approach for regression analysis would be to work with the absolute value of the error and apply a weighting factor or a weighting factor-squared - even though this probably goes against most statistical recommendations!
 
I think that's the way it should be. The regression is based on what's called "Least Squared Error" because the squared errors all wind up positive, and the larger errors get beat down before the smaller errors, which is the way things should work.

TTFN (ta ta for now)
I can do absolutely anything. I'm an expert! faq731-376 forum1529 Entire Forum list
 
Doug/IDS and others.
On the side topic of how to apply the weighting, ie to the error or the squared error.
What follows is "straight off the top of my head" (or maybe straight out of a more southerly orifice).

It is going to depend upon what a person means by the word "weighting".[ ] I doubt there is a widely agreed definition among statisticians.[ ] To my way of thinking, if sample point A has a weighting of twice that of sample point B, then A is worth twice as much as B.[ ] This would mean that in an unweighted analysis you would enter point A twice and point B only once.[ ] In a weighted regression based on minimising the sum of the squared errors the equivalent of this is to apply the weighting to the error's square.[ ] In a weighted regression based on minimising the sum of the magnitudes of the errors the equivalent would be to apply the weighting to the error's magnitude.

It there a statistical philosopher or a philosophical statistician in the house?
 
Denial - having worked through some examples, I'd say:

The weighting is applied to the square of the errors in all the code discussed in this thread, but the Alglib code also squares the weights (see Note 4 at:
So Alglib minimises Sum((w * error)^2)
whereas the VBA and Python functions minimise Sum(w * error^2), which is also what Wikipedia says should be done. Dave also said his "real statistics" package gave the same results as the VBA. So it seems that the Alglib code is the odd one out.

Doug Jenkins
Interactive Design Services
 
I have now written this up as a blog post, including downloads of the VBA, Alglib and Python functions:


I have tidied up the VBA code in the WeightLSq.xlsb spreadsheet, so it should run much quicker now:

Doug Jenkins
Interactive Design Services
 
You're a champion, Doug!

Best to you,

Goober Dave

Haven't see the forum policies? Do so now: Forum Policies
 
Doug.[ ] Sorry for the delay in replying:[ ] too many things coming to the boil at once.[ ] I agree with everything you say in your 05Feb17@04:46 post.

However a test of my philosophical musing would be to find a (trusted) web site that discusses the application of weightings to data fitting that minimises total absolute error rather than total squared error.[ ] I have not been able to find one.
 
But, linear regression does, in fact, use total squared error. Given that, the actual weighting seems a bit arbitrary, since it's now a subjective calculation, in that the user has "decided" that some points are more important to fit than others. I've been down that route before, and there's really no objective way to set the weights, as there are now essentially two additional degrees of freedom, the points to be weighted, and the amounts.

TTFN (ta ta for now)
I can do absolutely anything. I'm an expert! faq731-376 forum1529 Entire Forum list
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor