I am trying to write a Stata program to help me run regressions.
I have two dependent variables, y1
and y2
. On the right-hand side I have x1 x2 x3 x4 x5
. I have defined my global xlist
as below and wrote a program help me reg y1
on each specification.
I was wondering how to add another argument so I can run another set of regressions by using y2
as my left-hand variable.
global xlist1 "x1"
global xlist2 "x2"
global xlist3 "x1 x2"
global xlist4 "x1 x3"
global xlist5 "x1 x4"
global xlist6 "x1 x5"
capture program drop reg_and_save
program define reg_and_save
args j
reg y1 ${spec`j'}
est save results`j'.ster, replace
end
forvalues j=1(1)6{
reg_and_save `j'
}
CodePudding user response:
There are numerous possible answers here depending on
how literally we take your question
whether this code is going to grow in some direction, and even if it is, in what direction.
My rather personal answer is that I see no reason for abstraction here. You are attempting to wire in various specific details into a program, but in most cases the point of a program is to provide generality, not specificity.
Similarly there is little or no point in defining globals here. Globals don't really play much part in most Stata programming.
The most obvious weakness of your code that would stop it working is that you define global xlist1
to xlist6
but use quite a different set of global names within the program.
I can't see a strong case for this to be anything but part of a .do file.
local xlist1 "x1"
local xlist2 "x2"
local xlist3 "x1 x2"
local xlist4 "x1 x3"
local xlist5 "x1 x4"
local xlist6 "x1 x5"
forval k = 1/2 {
forval j = 1/6 {
reg y`k' `xlist`j''
est save results`k'_`j'.ster, replace
}
}
There's more caprice in your code. With 5 predictors, there are in principle 31 different regressions, so long as you have enough data. Trying all those regressions would be unusual but not out of the question. In specifying only 6 of those possible regressions, you may have been just inventing an example, or have subject-matter reasons for being interested in those regressions alone, but the point remains: your details don't encourage or suggest a more general command.