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: RE: Stata Program Command


From   Nick Cox <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   RE: st: RE: Stata Program Command
Date   Fri, 15 Jun 2012 19:20:57 +0100

. help nullmat()

e.g. 

program define grabest
        version 8
        syntax [, MATrix(string) ]

        if "`e(cmd)'" != "regress" {
                di as err "no regress results in memory"
                exit 498
        }

        if "`matrix'" == "" local matrix "X"
        tempname betas beta var se
        matrix `betas' = e(b)
		scalar `beta' = `betas'[1,1]
        matrix `var' = e(V)
        scalar `se' = sqrt(`var'[1,1])
        matrix `matrix' =  nullmat(`matrix') \ `beta', `e(N)', `se'
        matrix colnames `matrix' = beta N se 
end

You'll want to add more, but I'm signing myself off on this now. 

Nick 
[email protected] 


-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Hassan Enayati
Sent: 15 June 2012 19:13
To: [email protected]
Subject: Re: st: RE: Stata Program Command

Nick,

Thank you for the advice. I still need the program to populate a
matrix with the results of more than one regression. Your example
program only stores the most recent regression results. Do you have
any advice on how to write a program that stores the results of
several regressions. For example:

reg price weight
grabest
reg price turn
grabest

Then the matrix created by grabest would contain the results of the
first regression on row one and the results of the second regression
on row two.

Thanks.

On Fri, Jun 15, 2012 at 10:53 AM, Nick Cox <[email protected]> wrote:
> That's what you ask for. You create a matrix with one row; then if all goes well, you add the same row as a second row.
>
> Cutting that out, I advise
>
> 1. Using temporary names to avoid conflicts.
>
> 2. Using -syntax- to indicate an option.
>
> 3. Using -sqrt()- not powering for square roots.
>
> 4. More checking and labelling.
>
> Something like
>
> program define grabest
>        version 8
>        syntax [, MATrix(string) ]
>
>        if "`e(cmd)'" != "regress" {
>                di as err "no regress results in memory"
>                exit 498
>        }
>
>        if "`matrix'" == "" local matrix "X"
>        tempname betas beta var se
>        matrix `betas' = e(b)
>    scalar `beta' = `betas'[1,1]
>        matrix `var' = e(V)
>        scalar `se' = sqrt(`var'[1,1])
>        matrix `matrix' = `beta', `e(N)', `se'
>        matrix colnames `matrix' = beta N se
> end
>
> But comparable to -eststo- this is not!
>
> Nick
> [email protected]
>
> Hassan Enayati
>
> I am trying to write my first Stata program and am having some
> difficulties. At the end of the day, I would like a program (like
> eststo but not really the same b/c of formatting issues) that grabs
> certain results from a series of regressions (always a simple
> regressions) and stores these results in a matrix. Below is the
> program. The interesting problem is that the matrix contains duplicate
> rows of the last regression and always only two rows.
>
> clear all
> sysuse auto
> capture matrix drop X
> capture program drop grabest
> program define grabest
>     matrix betas = e(b)
>     matrix beta = betas[1,1]
>     matrix num = e(N)
>     matrix var1 = e(V)
>     scalar se1 = ( var1[1,1])^(0.5)
>     matrix X = beta, num, se1
>     capture confirm matrix X
>     if _rc==111 {
>         matrix X = beta,num,se1
>         }
>     else {
>         matrix X = X \ beta,num,se1
>         }
>     matrix drop betas beta num var1
>     scalar drop se1
> end
>
> reg price weight
> grabest
> reg length turn
> grabest
> reg turn weight
> grabest
> matrix list X
>
> An additional question I have deals more directly with the
> construction of the program. How do I create the program so that it
> generates a matrix with a name I specify. Hence, I would like to be
> able to write:
> grabest, matrix(autos1)
> where autos1 = X in the above example.
>
>

*
*   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