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]
st: Re: st: From: Rodrigo Briceño <[email protected]>
From
Nick Cox <[email protected]>
To
[email protected]
Subject
st: Re: st: From: Rodrigo Briceño <[email protected]>
Date
Tue, 25 Sep 2012 18:06:34 +0100
In principle the way to start that off is an outer loop
foreach j in 8 225 29 220 18 251 222 221 244 24 276 {
... if isin == `j'
}
but you may need to make other changes.
I have various other comments on your code.
0. You use -estout- and -estadd-, which are user-written programs, so
you are asked to explain where they come from.
1. There is unnecessary creation of locals to act as what I call "middle macros"
local hettest = r(nr2)
local hetprob = r(nr2p)
estadd scalar hettest = `hettest'
estadd scalar hetp = `hetprob'
can be condensed to
estadd scalar hettest = r(nr2)
estadd scalar hetp = r(nr2p)
2. The use of matrices to hold constants is also not needed, sp
matrix bgchi2s = r(chi2)
matrix bgchi2p = r(p)
estadd scalar bgchi2s = bgchi2s[1,1]
estadd scalar bgchi2p = bgchi2p[1,1]
reduces to two lines similarly.
3. The Jarque-Bera test consists of using asymptotic results for
skewness and kurtosis of Gaussians for small samples. It's a widely
kept secret that it has terrible properties. A few simulations is
enough to convince that convergence is appallingly slow.
4. That said,
tabstat ehat, stats(skewness kurtosis) column(variable) save, if isin==8
matrix stats=r(StatTotal)
local SK=stats[1,1]
local KU=stats[2,1]
gen jb = (e(N)/6)*((`SK')^2+(`KU'-3)^2/4) if isin==8
egen jb1=max(jb)
estadd scalar jb=jb1
is also roundabout. The variable -jb- just is holding a constant, so
the -egen- call is redundant.
su ehat if isin == 8, detail
estadd scalar jb = (r(N)/6)*((r(skewness)^2+(r(kurtosis) - 3)^2/4)
is, I believe, equivalent.
5. The two -forvalues- loops could be collapsed to one. In fact, it is
not necessary to -generate- lagged variables because -regress- will
create them on the fly.
Although I haven't tested it I thus suggest that your code (apart from
the -estout- call) simplifies to
local X "sfid liqmk dv pprom"
forvalues i=1(1)24 {
regress liq `X' l`i'.liq if isin==8
predict ehat if e(sample), res
ivhettest, ivsq
estadd scalar hettest = r(nr2)
estadd scalar hetp = r(nr2p)
estat bgodfrey, lags(1)
estadd scalar bgchi2s = r(chi2)
estadd scalar bgchi2p = r(p)
su ehat, detail
estadd scalar jb = (r(N)/6)*((r(skewness)^2+(r(kurtosis) - 3)^2/4)
scalar work = (r(N)/6)*((r(skewness)^2+(r(kurtosis) - 3)^2/4)
estadd scalar jbp = chiprob(2,work)
drop ehat
estimates store m`i', title(m`i')
}
The line -scalar work ...- may be redundant but I don't ever use
-estout- or -estadd-, so I can't be sure.
Simplifying the code in this way may help you generalise it by adding
an outer loop. I suspect that the line
estimates store m`i', title(m`i')
would need to become something more like
estimates store m`i'_`j', title(m`i'_`j')
Nick
On Tue, Sep 25, 2012 at 5:03 PM, <[email protected]> wrote:
> Hello Statalisters. I have a do file that use a macro in order to run
> several regression models, using different lags each time. What I'm
> following pasting is the syntax corresponding to ISIN=8. I have eleven
> different ISIN (let's call them IDs). How can I introduce the issue of
> different values there. I think this a matter of nesting, but I never
> have used that. Even the text file with the results is associated to
> the number (ISIN) selected. My full list includes 8, 225, 29, 220, 18,
> 251, 222 ,221, 244, 24, 276
>
> Thanks for your contribution
>
> -------------------------------------------------------------------------
>
> forvalues i=1(1)24 {
> gen l`i'liq=l`i'.liq
> }
>
> local X = "sfid liqmk dv pprom"
>
> forvalues i=1(1)24 {
> regress liq `X' l`i'liq if isin==8
> predict ehat if e(sample), res
> ivhettest, ivsq
> local hettest = r(nr2)
> local hetprob = r(nr2p)
> estadd scalar hettest = `hettest'
> estadd scalar hetp = `hetprob'
> estat bgodfrey, lags(1)
> matrix bgchi2s = r(chi2)
> matrix bgchi2p = r(p)
> estadd scalar bgchi2s = bgchi2s[1,1]
> estadd scalar bgchi2p = bgchi2p[1,1]
> *jarque bera
> tabstat ehat, stats(skewness kurtosis) column(variable) save, if isin==8
> matrix stats=r(StatTotal)
> local SK=stats[1,1]
> local KU=stats[2,1]
> gen jb = (e(N)/6)*((`SK')^2+(`KU'-3)^2/4) if isin==8
> egen jb1=max(jb)
> estadd scalar jb=jb1
> estadd scalar jbp=chiprob(2,jb1)
> drop ehat jb jb1
> *end of jarque bera
> estimates store m`i', title(m`i')
> }
>
> estout * using modisin8.txt, cells(b(star fmt(%9.3f)) p(fmt(%9.3f)))
> starlevels(* .10 ** .05 *** .01) stats(r2 N hettest hetp bgchi2s
> bgchi2p jb jbp, fmt(%9.3f %9.0g %9.3f %9.3f %9.3f %9.3f %9.3f %9.3f)
> labels(R-squared)) legend label collabels(none) varlabels(_cons
> Constant)
> drop _est*
> drop l*liq
> save, replace
*
* 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/