<p>If you check Stata's help file on <a href="http://www.stata.com/manuals13/rregress.pdf" rel="nofollow">regress</a> you should understand how to do it. Particularly pp. 16-7 have specific examples of how to apply weights.</p>

<p>I will edit in order to be more detailed.</p>

<pre><code>gen lnyl1y=ln(y)-l1.ln(y)
xi: reg lnyl1y i.country [w=y]
</code></pre>

<p>Notice that if the weighted regression is done by dividing all values for observation $i$ by $\sqrt{w_i}$, then 
$$\tilde{x} = 
\begin{pmatrix} 
\frac{x_{1,1}}{\sqrt{w_1}} &amp; \cdots &amp; \frac{x_{1,k}}{\sqrt{w_1}}\\
\vdots &amp;\ddots &amp;\vdots\\
\frac{x_{n,1}}{\sqrt{w_n}} &amp; \cdots &amp; \frac{x_{n,k}}{\sqrt{w_n}} 
\end{pmatrix} 
$$
Call the diagonal matrix composed of $\sqrt{w_i}$ in the $(i,i)$ element $\sqrt{w}$. Then, the WLS estimator is given by
$$
\hat{\beta}_{WLS}=\left(\tilde{x}'\tilde{x}\right)^{-1}\tilde{x}'\tilde{y}=
\left[(\sqrt{w}x)'\sqrt{w}x\right]^{-1}(\sqrt{w}x)'(\sqrt{w}y)=(x'Wx)^{-1}(xWy)
$$
as $\sqrt{w}'\sqrt{w}=W$ and $\tilde{x}=\sqrt{w}x$ which is exactly the same as multiplying each row of $X$ and $y$ by the sqrt of your weight (analytical weight, here $\sqrt{w}$).</p>

<p>The thing here is to understand how your matrix relates to applying the weights and understanding the connection between your formula and the one which is in the help file. For other ways to calculate weighted regressions in relation with Stata, see e.g. <a href="http://www.parisschoolofeconomics.eu/docs/dupraz-yannick/using-weights-in-stata(1).pdf" rel="nofollow">this document</a></p>

CC BY-SA 3.0
2016-05-18T01:47:40.220Z
2016-05-19T18:41:10.523Z
2016-05-19T18:41:10.523Z
user_newbie10
<p>I have a question regarding weighing observations by importance. </p>

<p>Suppose I am running the following regression:$$log(y_{it}/y_{it-1})=\alpha+\sum_{i=1}^{N}\gamma_{i}Country_{i}+u_{i}$$ where basically my LHS is GDP growth of country $i$ at time $t$ that I regress on a full set of country dummies (-1).<br>
I want to weigh the observations by the GDP of the country. 
If I am doing this on stata, what weights are appropriate? Frequency weights or probability weights? My intuition is that I have to basically run a weighted least squares regression of the form: $$ \hat{\beta}=(x'Wx)^{-1}(x'Wy)$$</p>

<p>where $W$ is a diagonal matrix with the weights (GDP) on the diagonal. I am at a loss regarding how to implement this on Stata. Thanks!</p>

CC BY-SA 3.0
2015-11-25T01:39:01.293Z
You should consider looking at the Generalized Least Squares (GLS) method, it's basically a 2 step OLS where you try to find the optimal weighting matrix at the 2nd step.
Louis. B
CC BY-SA 3.0
2015-11-25T05:40:35.033Z
Not to take away from the economic forum, but if you ask the question about Stata programming on Stack Overflow, they will help you with the code much quicker. I must share that I don't use Stata, I prefer Matlab/Excel combo so my opinion is not very credible.
Arthur Tarasov
<regression><stata>
Regression with weights
ChinG
# Regression with weights

(Asked by: username_0 on 2015-11-24)

I have a question regarding weighing observations by importance.

Suppose I am running the following regression:$$log(y\_{it}/y\_{it-1})=\alpha+\sum\_{i=1}^{N}\gamma\_{i}Country\_{i}+u\_{i}$$ where basically my LHS is GDP growth of country $i$ at time $t$ that I regress on a full set of country dummies (-1).  
I want to weigh the observations by the GDP of the country.
If I am doing this on stata, what weights are appropriate? Frequency weights or probability weights? My intuition is that I have to basically run a weighted least squares regression of the form: $$ \hat{\beta}=(x'Wx)^{-1}(x'Wy)$$

where $W$ is a diagonal matrix with the weights (GDP) on the diagonal. I am at a loss regarding how to implement this on Stata. Thanks!

**Tags:** regression, stata
> **Comments:**
> **username_1**: You should consider looking at the Generalized Least Squares (GLS) method, it's basically a 2 step OLS where you try to find the optimal weighting matrix at the 2nd step.  
> **username_2**: Not to take away from the economic forum, but if you ask the question about Stata programming on Stack Overflow, they will help you with the code much quicker. I must share that I don't use Stata, I prefer Matlab/Excel combo so my opinion is not very credible.  

---

## 1 answers

### Answer by username_3 on 2016-05-18. Score: 4 | Accepted answer

If you check Stata's help file on regress you should understand how to do it. Particularly pp. 16-7 have specific examples of how to apply weights.
I will edit in order to be more detailed.
gen lnyl1y=ln(y)-l1.ln(y)
xi: reg lnyl1y i.country [w=y]

Notice that if the weighted regression is done by dividing all values for observation $i$ by $\sqrt{w_i}$, then 
$$\tilde{x} = 
\begin{pmatrix} 
\frac{x_{1,1}}{\sqrt{w_1}} & \cdots & \frac{x_{1,k}}{\sqrt{w_1}}\\
\vdots &\ddots &\vdots\\
\frac{x_{n,1}}{\sqrt{w_n}} & \cdots & \frac{x_{n,k}}{\sqrt{w_n}} 
\end{pmatrix} 
$$
Call the diagonal matrix composed of $\sqrt{w_i}$ in the $(i,i)$ element $\sqrt{w}$. Then, the WLS estimator is given by
$$
\hat{\beta}_{WLS}=\left(\tilde{x}'\tilde{x}\right)^{-1}\tilde{x}'\tilde{y}=
\left[(\sqrt{w}x)'\sqrt{w}x\right]^{-1}(\sqrt{w}x)'(\sqrt{w}y)=(x'Wx)^{-1}(xWy)
$$
as $\sqrt{w}'\sqrt{w}=W$ and $\tilde{x}=\sqrt{w}x$ which is exactly the same as multiplying each row of $X$ and $y$ by the sqrt of your weight (analytical weight, here $\sqrt{w}$).
The thing here is to understand how your matrix relates to applying the weights and understanding the connection between your formula and the one which is in the help file. For other ways to calculate weighted regressions in relation with Stata, see e.g. this document


---

