# st: Re: weights from regressor matrix

 From Christopher Baum To statalist@hsphsun2.harvard.edu Subject st: Re: weights from regressor matrix Date Tue, 8 Apr 2003 07:20:10 -0400

```On Tuesday, April 8, 2003, at 02:33 AM, Jesper wrote:

```
I am trying to use some of STATA's matrix commands, but do not really succeed, and hope some of you can give me a hint.

For my dataset I need a weighting factor (for every observation) calculated as
w=xi'(X'X)^-1 *xi, where X is a n*k matrix of all variables and all observations, and xi is a k*1 vector with the variable-values for a single observation. The middle part, (X'X)^-1, I can build using "matrix accum", but I can't get STATA to build a n*1 vector for every observation to pre- and postmultiply it with. Hope some of you might a suggestion.
I think if you write out the w vector, you will find that it may be computed by the
following:

webuse auto,clear
mkmat price mpg headroom trunk, mat(x)
mat wfull = x*syminv(x'*x)*x'
mat iota = J(rowsof(wfull),1,1)
mat wt = wfull*iota
svmat wt
summ wt
list wt

This is ugly, in that it generates a n x n matrix and uses only the diagonal, but that is much cleaner than extracting each row of x in a loop.

Kit

*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/