Bookmark and Share

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: Append new row to existing matrix


From   Jorge Eduardo Pérez Pérez <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: Append new row to existing matrix
Date   Sat, 24 Mar 2012 20:04:25 -0400

Regarding your question about the generated variables:

qui gen x`i'=uniform()

quietly generates a random uniform variable named x`i' (i goes from 0 to 100)

I generated random variables to get a example dataset for the example
code. In your case, as you already noticed in your adapted code, you
do not need to generate random variables.

Looking at your adapted code, -statsby. is a more convenient
alternative to your problem. It will
generate a new dataset with the event number and the coefficients of
the regression

statsby, by(event): reg rprf rmrf smb hml

If you still want to use the matrix approach, you can use -capture- to
skip the regressions that fail because of lack of observations.

forv i=1(1)917 {
     capture quietly reg rprf rmrf smb hml if event==`i'
     if _rc matrix b=[.,.,.]
     else matrix b=e(b)
     if `i'==1 matrix betas=b
     else matrix betas=(betas \ b)
     }
matrix list betas

Notice that you could generate a matrix of the coefficients anyway
with -statsby- and -mkmat-

statsby, by(event): reg rprf rmrf smb hml
mkmat *, matrix(betas)

If you do not want the event number in the matrix, try

statsby, by(event): reg rprf rmrf smb hml
mkmat _b*, matrix(betas)
_______________________
Jorge Eduardo Pérez Pérez



On Sat, Mar 24, 2012 at 7:40 PM, Sandy Y.  Zhu <[email protected]> wrote:
> Hi everyone:
>
> Sorry to email again, but I just adjusted to my own dataset, and it
> won't work. My dataset has 917 events, each with 6 observations. I
> would like to regress rprf on rmrf, smb, and hml for each event,
> that's why i added reg rprf rmrf smb hml if event==`i'. I'm not sure
> if this is correct, but it's coming back as no observation. I know
> that some of the events in my dataset have no observation--is there
> any chance I can program it in the loop to tell stata to output an
> empty space for those and carry on?
>
> Thanks!
>
> Current adpated code:
>
> forv i=1(1)917 {
>       qui reg rprf rmrf smb hml if event==`i'
>       matrix b=e(b)
>       if `i'==1 matrix betas=b
>       else matrix betas=(betas \ b)
>       }
> matrix list betas
>
> On Sat, Mar 24, 2012 at 7:31 PM, Sandy Y.  Zhu <[email protected]> wrote:
>> I guess I just want to know what I should do with this portion of the
>> code if my variables are y (dependent), X1, X2, and X3 (independent)
>>
>> gen y=uniform()
>> forv i=1(1)100 {
>>  qui gen x`i'=uniform()
>>  qui reg y x`i'
>>
>>
>> Thanks!
>>
>> On Sat, Mar 24, 2012 at 7:28 PM, Sandy Y.  Zhu <[email protected]> wrote:
>>> Thank you!
>>>
>>> Would some one mind telling me what this portion of the code means?
>>>
>>>>        qui gen x`i'=uniform()
>>>>        qui reg y x`i'
>>>
>>> I'm trying to run a multivariate regression, so should I do multiple
>>> qui gen x`i'?
>>>
>>> Thanks!
>>>
>>> 2012/3/24 Jorge Eduardo Pérez Pérez <[email protected]>:
>>>> clear
>>>> set obs 100
>>>> gen y=uniform()
>>>> forv i=1(1)100 {
>>>>        qui gen x`i'=uniform()
>>>>        qui reg y x`i'
>>>>        matrix b=e(b)
>>>>        if `i'==1 matrix betas=b
>>>>        else matrix betas=(betas \ b)
>>>>        drop x`i'
>>>> }
>>>> matrix list betas
>>>>
>>>> Also, take a look at -statsby-
>>>> _______________________
>>>> Jorge Eduardo Pérez Pérez
>>>>
>>>>
>>>>
>>>> On Sat, Mar 24, 2012 at 7:11 PM, Sandy Y.  Zhu <[email protected]> wrote:
>>>>> Hi guys:
>>>>>
>>>>> How can I append a new row to an existing matrix? Basically, I'm
>>>>> trying to update the matrix in a loop,
>>>>>
>>>>> I have:
>>>>>
>>>>> for i=1 to 900
>>>>> {reg y x1 x2 x3
>>>>> mat beta=e(b)
>>>>>
>>>>> append e(b) to an existing matrix}
>>>>>
>>>>> Basically, I  have 900 regressions to run, and I  need to save the
>>>>> beta values from each regression. I would like to save them all in a
>>>>> matrix, so the first row in the matrix saves the betas from the 1st
>>>>> regression, the 2nd row would save the coefficients from the 2nd
>>>>> regression, etc....
>>>>>
>>>>>
>>>>> Thank you!
>>>>>
>>>>> --
>>>>> Yours sincerely,
>>>>>
>>>>> Sandy Y. Zhu
>>>>> *
>>>>> *   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/
>>>
>>>
>>>
>>> --
>>> Yours sincerely,
>>>
>>> Sandy Y. Zhu
>>
>>
>>
>> --
>> Yours sincerely,
>>
>> Sandy Y. Zhu
>
>
>
> --
> Yours sincerely,
>
> Sandy Y. Zhu
>
> *
> *   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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index