You are on page 1of 2

clear

set seed 2121


set obs 10
gen double y= rnormal(3,1)
gen double x1= rnormal(2,2)

/* First, we examine a model with only a constant term (i.e., B0). */


reg y
predict double u_hat, resid

/* Note that the sum of the residuals is equal to zero per the OLS first
order conditions when a constant is included in the model. I use that property
here to solve for one of the residuals based on the sum of the other
(in this case) 9. */

egen double resid_sum= total(u_hat) if [_n] >1

/* u1 + resid_sum = 0, so solve for u1


u1 + -.80960111 = 0
u1= .80960111
*/

list u_hat in 1/1

/*
Note that this agrees to the residual, u_hat, for the first observation.
This is why we have nine degrees of freedom. Once we
know the other nine residuals, the last one is "fixed" in the
sample based on the sum of the residuals equaling zero
when a constant is included in the regression.

Second, we examine a model with a constant term and one regressor. There are
two parameter estimates, B0 and B1, so two degrees of freedom are lost. */

drop resid_sum u_hat

reg y x1
predict double u_hat, resid

egen double resid_sum= total(u_hat) if [_n] >2

/*
First equation from the OLS first order conditions to solve for the
first two residuals:
u1 + u2 + resid_sum = 0, so solve for u1
u1 + u2 + -1.5522751 = 0
u1 + u2 = 1.5522751
u1 = 1.5522751 - u2

Second equation from the OLS first order conditions needed to solve for the
first two residuals: sum(xi*u_hati) = 0 */

gen double x1_u_hat= x1*u_hat


egen double sum_x1_u_hat= total(x1_u_hat) if [_n] > 2

/* u1*x1 + u2*x2 + sum of other products = 0


u1(3.1292971) + u2(2.4768956) + -4.1841163 = 0

Substitute in the previous equation for u1, where u1 = 1.5522751 - u2


(1.5522751 - u2)(3.1292971) + u2(2.4768956) + -4.1841163 = 0
distribute the 3.1292971
4.8575300 - 3.1292971(u2) + u2(2.4768956) + -4.1841163 = 0
-3.1292971(u2) + u2(2.4768956)= -.6734137
factor out u2
u2(-3.1292971 + 2.4768956)= -.6734137
u2(-0.6524015)= -.6734137
u2= 1.0322075
*/

list u_hat in 2

/* Note that this agrees to the second residual to the sixth decimal place

Next, substitute u2 above into the original equation for u1 from


the first constraint
u1 = 1.5522751 - u2
u1= 1.5522751 - 1.0322075
u1= .5200676
*/

list u_hat in 1

/* Note that this agress to the first residual to the sixth decimal place

This shows how a degree of freedom is lost for each paramter estimated.
Each time a new regressor is added, the sum of each regressor times the residual
has to equal zero per OLS conditions. That means that you have one more
equation constraint added to the system per regressor. Here, there are eight
degrees of freedom because once those eight are given, the other
two may be solved for based on the two equations that restrict the system. */

You might also like