This repository provides a simulation of Ordinary Least-Squares (OLS) in Mata.
I assume that the reader has some basic knowledge using STATA and has an introductory understanding of matrix algebra and the matrix linear regression model.
Mata is a matrix programming language that can be used by those who want to perform matrix calculations interactively and by those who want to add new features to Stata.
In this case, this simulation can be useful as a replication of the beta coefficient in a regression analysis through OLS in Mata.
In matrix form, the sample regression function is:
and the least-square estimator can be expressed as:
The user must first prepare the data for analysis. Load the exercise data, stored in a file called auto2.dta, which is included in STATA.
clear
sysuse auto2.dta
Now enter the Mata prompt to finally view the data in Stata’s memory as a matrix, and then clear Mata’s memory:
mata
mata clear
Thus, generate the vectors of dependent and independent variables.
X = st_data(.,"weight") // covariate var
X = X, J(rows(X),1,1) //constant term
Y = st_data(.,"mpg") // dependent var
Finally, estimate the beta coefficient by using the same matrix expression above and exit the Mata session in order to return to STATA
beta = luinv(X'*X)*X'*Y
beta
end
In conclusion, compare the results using the Matrix Linear Regression Model in Mata to the results using the Scalar Linear Regression Model in STATA.
reg mpg weight //verify