Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: Looping and saving regression outputs

 From Steve Samuels To statalist@hsphsun2.harvard.edu Subject Re: st: Looping and saving regression outputs Date Thu, 17 May 2012 14:49:04 -0400

```Correction of a small typo:

*********************************
matrix beta = nullmat(beta) \ e(b)
*********************************

Steve
sjsamuels@gmail.com

On May 17, 2012, at 2:34 PM, Fernando Rios Avila wrote:

oops
You are totally right. The code should be like this:
> foreach i in 1 2 3 4 5 6 7 8 9 10 {
> reg y x if sizef==`i'
> matrix betas=nullmat(beta) \ e(b)
> matrix r2=nullmat(r2), e(r2)
> matrix s2=nullmat(s2), e(V)
> }

And the Size of firm...I guess too much work on my own data.
Fernando
On Thu, May 17, 2012 at 2:27 PM, Nick Cox <n.j.cox@durham.ac.uk> wrote:
> This sounds more likely to be what Sebastian wants, but note that this loop repeats exactly the same code each time round the loop. Something like an -if- condition is presumably needed inside.
>
> (How you do know that -sizef- is "size of firm"? Immaterial, but it's nowhere stated.)
>
> Nick
> n.j.cox@durham.ac.uk
>
>
> -----Original Message-----
> From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Fernando Rios Avila
> Sent: 17 May 2012 19:20
> To: statalist@hsphsun2.harvard.edu
> Subject: Re: st: Looping and saving regression outputs
>
> Ok Sebastian,
> So i believe what you want do is use different samples identified by
> the size of the firm which is given in the variable sizef.
> My answer, then, is more "manual". The first thing you need to have is
> a list of all the possible groups you can obtain by sizef. A simple -
> tab sizef -, or a recode of sizef should work here.
> Lets say now that you have 10 groups. Your loop then should look something like:
>
> foreach i in 1 2 3 4 5 6 7 8 9 10 {
> reg y x
> matrix betas=nullmat(beta) \ e(b)
> matrix r2=nullmat(r2), e(r2)
> matrix s2=nullmat(s2), e(V)
> }
>
> that should save your betas, the R2 and the variance-covariance
> matrix. You might need to modify it specifically for your purposes in
> the case of the var_covar matrix, since you only want the standard
> errors.
>
> Hope this helps
> F
>> Sent: 17 May 2012 19:01
>> To: statalist@hsphsun2.harvard.edu
>> Subject: Re: st: Looping and saving regression outputs
>>
>> Nick et al,
>>
>> Sorry for the lack of clarity. I will try to be more clear now and in future posts.
>>
>> Here goes:
>>
>> I have a panel dataset with several variables, including (sizef) with around 50,000 observations.
>> The code that I am running does not contain the cycling/loop because I am unsure how to write it.
>>
>> So the first question/problem is that:
>> I would like to cycle through each observation of sizef and run each of three regressions.
>>
>> The second question/problem regards:
>> Recovering the coefficient vector, r2 and standard errors in a matrix.
>>
>> After recovering these values for each observation, I want to create a histogram by regression for each of these values (coefficients, r2 and standard errors) to get a visual representation of how they are distributed.
>>
>> I hope this helps clarify my issue.
>>
>> On May 17, 2012, at 10:37 AM, Nick Cox wrote:
>>
>>> I think it's the other way round. What you are doing precisely is unclear and more detailed advice is difficult for that reason.
>>>
>>> For example:
>>>
>>> You refer to cycling over variables -sizef- but it is not clear where -sizef- appears in your code.
>>>
>>> You say you have 51,000 variables, but no Stata allows more than 32,767 variables. Do you mean observations?
>>>
>>>
>>>
>>> Nick
>>> n.j.cox@durham.ac.uk
>>>
>>> Sebastian Galarza
>>>
>>>
>>> Thank you for your response. Can you be a little bit more explicit in the coding of this? I am still far from being a STATA power user and some examples might be helpful. Im looking into the svmat command and its a good starting point. Thanks for your input,
>>>
>>> On May 17, 2012, at 8:15 AM, Ada Ma wrote:
>>>
>>>> First you can create one matrix for each regression by stacking the r2
>>>> on to the coefficients.  So say you have Q explanatory variable your
>>>> row matrix would be 1 x (Q+1+1) (one for the constant one for the r2).
>>>> You might also consider adding the SEs to your matrix too.  You might
>>>> want to add an extra item at the top end of your matrix to label your
>>>> regressions too to save your effort.
>>>>
>>>> Then you can stack all the regression results into one big matrix.
>>>>
>>>> Then after you have done all the loopings use -svmat- to turn your big
>>>> matrix into variables.
>>>>
>>>> Then finally you -outsheet- those variables into a CSV or save the new
>>>> variables as a separate Stata dataset.
>>>
>>>
>>> On Wed, May 16, 2012 at 6:22 PM, Sebastian Galarza
>>>
>>>>> I want to run the following code and save the beta values and r2 for three different regressions in a matrix such as this:
>>>>>
>>>>> Variable B0a B1a B2a R2a B1b B2b B3b R2b B1c B2c B3c R2c
>>>>> 1
>>>>> 2
>>>>> 3
>>>>>
>>>>> I have a total of 51000 variables (sizef) so that the code I would like to run is:
>>>>>
>>>>>
>>>>> *for each variable sizef
>>>>>
>>>>> nl (P_WOR = {b0=0.1}*(1 * exp({b1=0.05}* (ageyear))))
>>>>> predict a
>>>>> matrix list e(b)
>>>>> display e(r2)
>>>>>
>>>>> * save beta values and r2
>>>>>
>>>>> nl log3: P_WOR ageyear
>>>>> predict b
>>>>> matrix list e(b)
>>>>> display e(r2)
>>>>>
>>>>> * save beta values and r2
>>>>>
>>>>> nl gom3: P_WOR ageyear
>>>>> predict c
>>>>> matrix list e(b)
>>>>> display e(r2)
>>>>>
>>>>> * save beta values and r2
>>>>>
>>>>> Any help would be greatly appreciated.
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/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/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/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```