use "H:\oldTS1desktop\ECNS 561\Data Sets\Mega HW #2\KS_Crime_Data_Set_for_Mega_HW_2.dta", clear
keep year county_fips county dry wet violent_rate property_rate unemployment democrat_to_gop pop_density
/*Generating a constant that we will use in our regression*/
gen cons = 1
/*Mata does not automatically drop missing values when performing matrix
calculations, so we need to do this on our own. We are going to focus simply
on the violent crime rate and wet laws.*/
drop if (violent_rate == . | wet == .)
/*For illustration, it is going to be easier to focus on one year of data and
just look at crime and wet laws in the cross section*/
keep if year == 2011
/*Calling Mata*/
mata
/*The command we want to use for creating our matrices is st_view(V=., ., .)
The first argument in st_view simply names our matrix. The second declares the observations
we are interested...if we leave this set at ".", then this tells the st_view that
we are interested in all of the observations. The third argument tells st_view the
variables we want included in our matrix. If we leave this set at ".", then it will include
all variables
*/
st_view(Y=., ., "violent_rate")
st_view(X=., ., ("cons", "unemployment", "pop_density", "democrat_to_gop"))
/*We can view our matrices by simply typing the following*/
Y
X
/*Our OLS estimates can be computed with the following matrix algebra command...where invsym performs
symmetric real matrix inversion*/
b = invsym(X'X)*X'Y
b
/*Now, we can find the variance-covariance matrix and compute our standard errors*/
/*First, we calculate the residuals*/
e = Y - X*b
/*next we declare the sample size and the number of parameters to be estimated*/
n = rows(X)
k = cols(X)
/*Now, we calculate our variance estimate based on the sum of the squared residuals*/
s2 = (e'e)/(n-k)
/*Finally, we are ready to calculate our variance-covariance matrix*/
V = s2*invsym(X'X)
V
/*To compute the standard errors of our OLS esimtators we just take the square root of the diagonal of V*/
se = sqrt(diagonal(V))
/*Let's compute the t stats and p-values*/
t = b:/se
p = 2*normal(-abs(t))
/*Here are our results!*/
(b, se, t, p)
/*Ending our Mata session*/
end
/*Do we get the same results simply using the canned reg command in STATA?*/
reg violent_rate unemployment pop_density democrat_to_gop