Weighted OLS

Hi, I have an nx1 vector of positive integers frequency weights (total population), which I want to use to estimate a Weighted OLS using the formula b = inv(x'x)*x'y but with the total population weights.

How can I estimate it using GAUSS? Thanks!

1 Answer



0



The Weighted Least Squares (WLS) coefficients can be found by

$$B_{wls} = (X'WX)^{-1}*(X'WY)$$

where

$$ W =\begin{bmatrix} w_1 & 0 & 0 & \dots & 0 \\ 0 & w_2 & 0 & \dots & 0 \\ 0 & 0 & w_3 & \dots & 0 \\ \vdots & \vdots & \vdots & \ddots & \vdots\\ 0 & 0 & 0 & \dots & w_k \end{bmatrix} $$

This weight matrix, $W$, can be formed in GAUSS using the diagrv function. The diagrv procedure inserts a vector into the diagonal of an existing matrix. It requires two inputs:


x
N x N matrix,  The matrix to have the diagonal inserted into.
v
N x 1 vector,  The vector to be inserted into the diagonal.

To compose the weight matrix for WLS, the x input should always be a square matrix of zeros with dimensions equal to the number of observations in your model.

Supposing your weights are stored in a column vector called weights, the following code will build the appropriate weight matrix:

N = rows(weights);
w_mat = diagrv(zeros(N, N), weights);

You can then compute and print the WLS coefficients using:

// Calculate wls coefficients
b_wls = inv(x'w_mat*x)*(x'w_mat*y);
print "WLS coefficients:"; b_wls;

The code below will run an example WLS computation from start to finish using hypothetical data :

// Define data 
Parent = { 0.21, 0.2, 0.19, 0.18, 0.17, 0.16, 0.15 };
Progeny = { 0.1726, 0.1707, 0.1637, 0.164, 0.1613, 0.1617, 0.1598 };
SD = {0.01988, 0.01938, 0.01896, 0.02037, 0.01654, 0.01594, 0.01763 };

// Add a constant 
x = ones(rows(parent),1)~parent;
y = progeny;

// Calculate weights
weights = 1./SD.^2;
w_mat = diagrv(zeros(rows(weights),rows(weights)), weights); 

// Calculate ols coefficients
b_ols = inv(x'x)*(x'y);
print "OLS coefficients:"; b_ols;

// Calculate wls coefficients
b_wls = inv(x'w_mat*x)*(x'w_mat*y);
print "WLS coefficients:"; b_wls;



Eric

105

Your Answer

1 Answer

0

The Weighted Least Squares (WLS) coefficients can be found by

$$B_{wls} = (X'WX)^{-1}*(X'WY)$$

where

$$ W =\begin{bmatrix} w_1 & 0 & 0 & \dots & 0 \\ 0 & w_2 & 0 & \dots & 0 \\ 0 & 0 & w_3 & \dots & 0 \\ \vdots & \vdots & \vdots & \ddots & \vdots\\ 0 & 0 & 0 & \dots & w_k \end{bmatrix} $$

This weight matrix, $W$, can be formed in GAUSS using the diagrv function. The diagrv procedure inserts a vector into the diagonal of an existing matrix. It requires two inputs:


x
N x N matrix,  The matrix to have the diagonal inserted into.
v
N x 1 vector,  The vector to be inserted into the diagonal.

To compose the weight matrix for WLS, the x input should always be a square matrix of zeros with dimensions equal to the number of observations in your model.

Supposing your weights are stored in a column vector called weights, the following code will build the appropriate weight matrix:

N = rows(weights);
w_mat = diagrv(zeros(N, N), weights);

You can then compute and print the WLS coefficients using:

// Calculate wls coefficients
b_wls = inv(x'w_mat*x)*(x'w_mat*y);
print "WLS coefficients:"; b_wls;

The code below will run an example WLS computation from start to finish using hypothetical data :

// Define data 
Parent = { 0.21, 0.2, 0.19, 0.18, 0.17, 0.16, 0.15 };
Progeny = { 0.1726, 0.1707, 0.1637, 0.164, 0.1613, 0.1617, 0.1598 };
SD = {0.01988, 0.01938, 0.01896, 0.02037, 0.01654, 0.01594, 0.01763 };

// Add a constant 
x = ones(rows(parent),1)~parent;
y = progeny;

// Calculate weights
weights = 1./SD.^2;
w_mat = diagrv(zeros(rows(weights),rows(weights)), weights); 

// Calculate ols coefficients
b_ols = inv(x'x)*(x'y);
print "OLS coefficients:"; b_ols;

// Calculate wls coefficients
b_wls = inv(x'w_mat*x)*(x'w_mat*y);
print "WLS coefficients:"; b_wls;


You must login to post answers.

Have a Specific Question?

Get a real answer from a real person

Need Support?

Get help from our friendly experts.