# st: Re: reg and matrix munipulation for OLS in Stata

 From "Scott Merryman" To Subject st: Re: reg and matrix munipulation for OLS in Stata Date Mon, 5 Apr 2004 19:41:10 -0500

```Don't you need to add a column of 1s to the X matrix for the intercept?

For example

. sysuse auto
(1978 Automobile Data)

. gen con = 1

. mkmat con mpg weight, matrix(x)

. mkmat price

. matrix B = inv(x'*x)*(x'*price)

. matrix list B

B[3,1]
price
con   1946.0687
mpg  -49.512221
weight   1.7465592

. reg price mpg weight

Source |       SS       df       MS              Number of obs =
74
-------------+------------------------------           F(  2,    71) =
14.74
Model |   186321280     2  93160639.9           Prob > F      =
0.0000
Residual |   448744116    71  6320339.67           R-squared     =
0.2934
0.2735
Total |   635065396    73  8699525.97           Root MSE      =
2514

----------------------------------------------------------------------------
--
price |      Coef.   Std. Err.      t    P>|t|     [95% Conf.
Interval]
-------------+--------------------------------------------------------------
--
mpg |  -49.51222   86.15604    -0.57   0.567    -221.3025
122.278
weight |   1.746559   .6413538     2.72   0.008      .467736
3.025382
_cons |   1946.069    3597.05     0.54   0.590    -5226.244
9118.382
----------------------------------------------------------------------------
--

.

Hope this helps,
Scott

----- Original Message -----
From: <Nomorenook@netscape.net>
To: <statalist@hsphsun2.harvard.edu>
Sent: Monday, April 05, 2004 6:59 PM
Subject: st: reg and matrix munipulation for OLS in Stata

> Hi all,
>
> I encountered a weird problem. As you know, the formal form of obtaining
the coefficient estimates in OLS is: B = (X'X)^(-1)X'Y. In a dataset that
contains X1, X2, X3, X4, X5 and Y, I did the following:
>
> reg Y X1 X2 X3 X4 X5
>
> mkmat X1 X2 X3 X4 X5, matrix(X_ALL)
> matrix B = inv(X_ALL'*X_ALL)*X_ALL'Y
>
> Theoretically, I am supposed to get the same results for the coefficient
estimates contained in B matrix as well as in the results from reg command.
However, my regression results are slightly different in these two
approaches. Can anyone tell why I have different results?
>
> Thank you very much!
>

*
*   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/
```