*Foundations of Econometrics, Fall 2015, Prepared by Dr. I-Ming Chiu
*Accompany 367_Handout06.pdf

insheet using “study.csv”, clear
gen const = 1 /*generate a constant; a column of ones*/
describe

/*You have to type your STATA code from here on using MATA*/

/*Beginning of Mata*/
mata

st_view(y = .,., “score”) /
st_view(x = .,., (“const”, “time”))
xbar = mean(x[,2])
ybar = mean(y)
beta = luinv(x’x)*(x’y)
beta1 = beta[2,1] beta0 = beta[1,1] yhat = x*beta
resid = y – yhat
varcovbeta = sigmasq*luinv(x’*x)
varbeta1 = varcovbeta[2,2] varbeta0 = varcovbeta[1,1]

testbeta1 = (beta1 – 0)/sqrt(varbeta1)
critval = invt(8, 0.025) /*Method 1: finding cutoff value using 5% rejection*/
pbeta1 = 2*(1-t(8, testbeta1)) /*Method 2: p-value*/
/*Do the test for beta0 by yourself*/
tss = y’*y – length(y)*ybar^2
ess = tss – rss /*alternatively, ess = beta1^2*(x’*x)*/
Rsquare = ess/tss /*coefficient of determination*/
end
/*End of Mata*/

/*Check your regression outcome using command regress*/
regress score time
/*Create a scatter diagram w/ the regression line*/
graph twoway (lfit score time) (scatter score time)