Tuesday, July 2, 2019

Using matrices in STATA to handle results more efficiently

In the social sciences, it is a common practice to explore the association between two variables in a series of models that include various amounts of 'control' or 'adjustment' variables. Another common task for analysts is to estimate the association between a number of 'independent' variables of interest with one common 'dependent' variable. This process usually involves time consuming copying and pasting into Excel or Word. In this post, I will show you how to automate this process by using loops and matrices in STATA, using a do-file you can download here.

To get started, let's use the auto.dta dataset. Let's say that we are interested in miles per gallon (MPG) as a predictor of a car's price. First we estimate an 'unadjusted' regression with MPG as the independent variable and price as the dependent variable. Let's store these results in memory as m1:

regress price mpg
estimates store m1

Then, sequentially, we control for headroom, trunk space and weight of the car:

regress price mpg headroom
estimates store m2

regress price mpg headroom trunk
estimates store m3
regress price mpg headroom trunk weight
estimates store m4 

If we are only interested in the estimates of MPG, then there is a faster method compared to scrolling through the output of each model and copying the one line of results. Using the method outlined by Maarten Buis here to calculate p-values and confidence intervals, and combining this with loops and matrices.

First, we create a series of empty matrices:

matrix coef = (.)
matrix se = (.)
matrix p = (.)
matrix lb = (.)
matrix ub = (.)



Then, as we have four models, we will loop over the four stored models, extracting the beta, se, p and confidence intervals for the MPG estimates:



Now, we combine the estimates into one combined matrix called 'comb', and give this matrix row and column names:

* Now combine the results into one matrix *
matrix comb = (coef, se, p, lb, ub)
* Give the matrix column and row names *
matrix colnames comb = b se p LB UB
matrix rownames comb = . model1 model2 model3 model4


To see the matrix of results, use the matrix list command:

matrix list comb

You should see the following table:













This same principle can be used to estimate unadjusted linear regressions for the four independent variables:




























Now the disadvantage of this approach is that it takes longer to write in this format, but I really think this is quicker once you have a list of, say, 10 variables or so. Also, if you decide to change the model structure later on, you can quickly retrieve new results.

To add additional models, you will have to store the model results as sequential numbers and update the loop number in line 27 (e.g. if you had 10 models, it would be 1/10). To add additional variables, include these in the foreach loop in line 57. Remember to update the rownames as well.

No comments:

Post a Comment