# How to implement weighted Linear Regression

86 visualizzazioni (ultimi 30 giorni)
Shagun Khare il 12 Nov 2016
Commentato: Paul Safier il 6 Feb 2024
I am trying to reproduce the results of a paper. It is mentioned that they used weighted linear regression with three different weights.
Is Weighted least square regression is same as weighted linear regression?
How can i implement weighted linear regression in matlab?
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposte (2)

John D'Errico il 12 Nov 2016
"Is Weighted least square regression is same as weighted linear regression?" Yes.
Given the problem
A*x = y
where x is the vector of unknowns, and a weight vector w. w must have the same number of elements as y. I'll assume that w and y are column vectors.
W = diag(W);
x = (W*A)\(w.*y);
If there are many data points, then creating W as a diagonal matrix (that is not sparse) and multiplying by W will be less efficient that you may want. If you are using R2016b (or later) then you need not create W at all.
x = (w.*A)\(w.*y);
Again, this presumes that w and y are column vectors, and that you have R2016b.
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Paul Safier il 6 Feb 2024
Also, @John D'Errico, do you know why the formula for beta above differs from the one here (taken from documentation online as well as the WLS wiki page)?
A = [1;2;3;4]; Y = [1.1;2.3;2.9;10]; W = [1;1;1;0.4];
A1 = [ones(size(A)) A];
Beta1 = (W.*A1)\(W.*Y) % The one you show above.
Beta2 = (A1'*diag(W)*A1)\(A1'*diag(W)*Y) % Eqn 7 from: https://www.stat.cmu.edu/~cshalizi/mreg/15/lectures/24/lecture-24--25.pdf

Accedi per commentare.

Paul Safier il 6 Feb 2024
@John D'Errico do you know how to compute the R^2 value in this weighted case?
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Linear and Nonlinear Regression in Help Center e File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by