/*A nice beginner's guide to using Mata is available at:
http://jasoneichorst.com/wp-content/uploads/2012/01/BeginMatrix.pdf */
/*We will use our KS crime data set for this example*/
clear
set more off
use "H:\oldTS1desktop\ECNS 561\Data Sets\KS_Crime_Data.dta", clear
/*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", "wet"))
/*We can view our matrices by simply typing the following*/
Y
X
/*Here is an example of restricting our matrices to include only a subset of the observations
st_view(Y=., 1::5\7::9, .)
st_view(X=., 1::5\7::9, ("cons", "wet"))
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
/*Ending our Mata session*/
end