I am using a loop to run multiple mixed effect linear regression models, to see the effect of the same two independent variables on multiple dependent variables. Here is a sample of my data (it only includes a subset of the variables that I am using as dependent variables in the model):
Here is the loop I've written (I've used capture because some variables that start with acad have no observations for the subset of data specified by form==1):
I would like to save the output of each regression in a dta file, with data looking something like this:
I've tried -statsby- (see below) but I don't think it can accomplish what I want, unless I'm not fully understanding what it's capable of. First, I don't actually need to run a regression for each value of the variable 'form' (I only need to run it for form==1) but statsby requires a grouping variable. Second, it ignores capture so I have to specify each variable so that the loop doesn't error out - ok, fine, I have to do some extra typing. Third, the save option only saves the output of each individual regression (although that's really more how the loop is written).
What I need is some way to append the results of each regression (ultimately, I will be running dozens of regressions) within the loop, so that only one dta file is created. I have done lots of searching and reading past posts, and I'm sure there is a solution but I haven't found one, at least one I could understand and translate to my situation - I am still relatively new to Stata.
I am using Stata 17.
Code:
* Example generated by -dataex-. For more info, type help dataex clear input float(form title1 rrate1) long(acad1 acad2 stat2 stat3) 1 0 100 4 3 4 3 1 0 100 4 1 1 1 1 0 100 4 3 4 3 1 0 100 4 3 4 . 1 0 100 3 2 2 2 1 0 100 4 4 4 4 1 0 100 4 2 3 4 1 0 100 4 4 4 4 1 0 100 4 4 4 4 1 0 100 3 4 2 2 end
Code:
foreach i of varlist acad* stat* { display "`i'" capture noisily mixed `i' i.title1 rrate1 if form==1 || school: }
DV name | IV name | Coeff | SE | z | p |
acad1 | title1 yes | xxx | xxx | xx | xx |
acad1 | rrate1 | xxx | xxx | xxx | xx |
acad2 | title1 yes | xxx | xxx | xxx | xxx |
acad2 | rrate1 | xxx | xxx | xxx | xxx |
I've tried -statsby- (see below) but I don't think it can accomplish what I want, unless I'm not fully understanding what it's capable of. First, I don't actually need to run a regression for each value of the variable 'form' (I only need to run it for form==1) but statsby requires a grouping variable. Second, it ignores capture so I have to specify each variable so that the loop doesn't error out - ok, fine, I have to do some extra typing. Third, the save option only saves the output of each individual regression (although that's really more how the loop is written).
Code:
foreach i of varlist acad1 acad2 acad3 acad6 acad7 acad8 acad9 acad10 stat2 stat3 { display "`i'" statsby, by(form) saving(".do Files/Reporting/Insights Report/Working Files/myest.dta"): mixed `i' i.title1 rrate1 || school: }
I am using Stata 17.
Comment