Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Quotes around r() macros


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Quotes around r() macros
Date   Fri, 16 May 2008 19:09:59 +0100

Consider the difference between r(rho)^2 and `r(rho)'^2 when r(rho) is
negative. 

-display- understands the calculation when you use the first but if you
use the second then it sees (e.g.) -0.7^2 when you want it to act as it
would if it saw (-0.7)^2. 

How Stata behaves in this circumstance is the subject of an FAQ: 

FAQ     . . . . . . . . . . . . . .  The use of macros with the power
operator
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . W.
Gould
        12/99   I coded `a'^2 and got the wrong sign. Why?
                http://www.stata.com/support/faqs/lang/macpow.html

As the FAQ implies, this issue is not intrinsic to r() stuff, but
generic to the use of macros in expressions, which are always
substituted first, before any evaluation. 

The lesson is to parenthesise aggressively. 

Nick
n.j.cox@durham.ac.k 

Aleksander Rutkowski

Why the following two formulas - with and without quotes around r()
macros (see below) - produce very different results if the value stored
in the r() macro is negative? I attach a part of a Stata output
(Stata10SE, WindowsXP). Should not all macros, including the r() macros,
be quoted?


corr empl0 empl1
local t = r(rho) * sqrt((r(N)-2)/(1-r(rho)^2))
local t2 = `r(rho)' * sqrt((`r(N)'-2)/(1-`r(rho)'^2))
local p = 2*ttail(r(N)-2,abs(`t'))
local p2 = 2*ttail(`r(N)'-2,abs(`t2'))
di "r(rho): " r(rho) "   r(rho)': " `r(rho)'
di "r(N): " r(N) "   r(N)': " `r(N)'
di "t: " `t' "   t2: " `t2' "   p: " `p' "   p2: " `p2'
di

Example of Stata output:

r(rho): -.58133648   r(rho)': -.58133648
r(N): 10   r(N)': 10
t: -2.0208211   t2: -1.4215178   p: .07795928   p2: .19295556

r(rho): .85050034   r(rho)': .85050034
r(N): 10   r(N)': 10
t: 4.5735645   t2: 4.5735645   p: .00181727   p2: .00181727

r(rho): .00749718   r(rho)': .00749718
r(N): 10   r(N)': 10
t: .02120583   t2: .02120583   p: .98360083   p2: .98360083

r(rho): -.7597187   r(rho)': -.7597187
r(N): 10   r(N)': 10
t: -3.3045795   t2: -1.7110323   p: .01078497   p2: .12543946

r(rho): .18673805   r(rho)': .18673805
r(N): 10   r(N)': 10
t: .53763206   t2: .53763206   p: .60545943   p2: .60545943

r(rho): .64313145   r(rho)': .64313145
r(N): 10   r(N)': 10
t: 2.3754968   t2: 2.3754968   p: .04485987   p2: .04485987

r(rho): .74453652   r(rho)': .74453652
r(N): 10   r(N)': 10
t: 3.1544711   t2: 3.1544711   p: .01350709   p2: .01350709

r(rho): .88701037   r(rho)': .88701037
r(N): 10   r(N)': 10
t: 5.4333446   t2: 5.4333446   p: .00062087   p2: .00062087

r(rho): -.2475888   r(rho)': -.2475888
r(N): 10   r(N)': 10
t: -.72279083   t2: -.67976198   p: .49039643   p2: .51583887

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index