Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: -foreach- loop to fit regression at each level of a variable
From
Milena Przheska <[email protected]>
To
[email protected]
Subject
Re: st: -foreach- loop to fit regression at each level of a variable
Date
Tue, 19 Nov 2013 22:23:29 +0100
It appears my messages bounced, so I have to send this again.
Dear Sergiy, Klaus,
Thank you very much for your help.
Sergiy definitely hit a bull's eye! The code worked like a charm.
The reason why I did not want to use interactions was because I was
afraid it might interfere with the estimates of the other predictors
or cause a problem of multicollinerarity, while my only intention was
to check out the robustness of the model.
So bottom line, I used Sergiy's code along with the command -eststo-
from the userwritten package -estout- by Ben Jann (estout from
http://fmwww.bc.edu/RePEc/bocode/e), and I was able to achieve exactly
what I wanted.
levelsof sample, local(levs)
foreach n in `levs' {
display ""
display "================ Regression for level: `n'
============================='"
logistic outcome var1 var2 var3 if sample==`n', vce(cluster id)
eststo
estat ic
}
tabout
Once again, thank you so much for your help.
Best,
Milena
On Tue, Nov 19, 2013 at 8:49 PM, Sergiy Radyakin <[email protected]> wrote:
> There are many ways to get to the same result. one convenience with
> the loop written explicitly is that it is easier for a beginner to add
> new things into the loop's body, for example, to save results to
> matrices, or dump to a file, and many other tasks. Many Stata commands
> support -by- prefix/suffixes, which can be used to avoid explicit
> looping, at the expense of more constraints at the command.
> Best,Sergiy Radyakin
>
> On Tue, Nov 19, 2013 at 2:40 PM, Nick Winter <[email protected]> wrote:
>> isn't this what the -by:- prefix does:
>>
>> by sample_level, sort: logistic ...
>>
>>
>> - Nick Winter
>>
>>
>>
>> On 11/19/2013 11:11 AM, Sergiy Radyakin wrote:
>>>
>>> Milena, is this something you are after?
>>>
>>> set seed 123
>>> clear
>>> set obs 740
>>> generate sample_level=floor(runiform()*4)
>>> generate outcome=runiform()*4200>2000
>>> generate var1=runiform()
>>> generate var2=runiform()
>>> generate var3=runiform()
>>> generate id=floor(runiform()*2)
>>>
>>> levelsof sample_level, local(levs)
>>>
>>> foreach n in `levs' {
>>> display ""
>>> display "======== Regression for level: `n'
>>> ===================================="
>>> logistic outcome var1 var2 var3 if sample_level==`n', vce(cluster id)
>>> }
>>>
>>> Best, Sergiy
>>>
>>> On Tue, Nov 19, 2013 at 7:34 AM, Milena Przheska
>>> <[email protected]> wrote:
>>>>
>>>> Dear Statalist members,
>>>>
>>>> I am trying to fit the same logistic regression model using different
>>>> samples from a data set and I am wondering if it is even possible to
>>>> use a -foreach- loop to estimate regression models for each sample
>>>> (indicated by a variable).
>>>>
>>>> I use the variable 'sample' to indicate the sample, but instead of
>>>> writing the command over and over for each sample, I tried to do this
>>>> with a -foreach- loop, something like this:
>>>>
>>>> egen local sample_level = levelsof(sample)
>>>>
>>>> foreach n of sample_level {
>>>> logistic outcome var1 var2 var3 if sample_level==`n', vce(cluster id)
>>>> }
>>>>
>>>> As always, I would appreciate your help.
>>>>
>>>> Thanks,
>>>> Milena
>>>> *
>>>> * For searches and help try:
>>>> * http://www.stata.com/help.cgi?search
>>>> * http://www.stata.com/support/faqs/resources/statalist-faq/
>>>> * http://www.ats.ucla.edu/stat/stata/
>>>
>>> *
>>> * For searches and help try:
>>> * http://www.stata.com/help.cgi?search
>>> * http://www.stata.com/support/faqs/resources/statalist-faq/
>>> * http://www.ats.ucla.edu/stat/stata/
>>
>>
>> *
>> * For searches and help try:
>> * http://www.stata.com/help.cgi?search
>> * http://www.stata.com/support/faqs/resources/statalist-faq/
>> * http://www.ats.ucla.edu/stat/stata/
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/faqs/resources/statalist-faq/
> * http://www.ats.ucla.edu/stat/stata/
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/