scalar do not allow -if-

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

scalar do not allow -if-

Tomas Lind-2
Dear Statalisters,

In a small simulation I generate data and analyze them with 3 regression methods. In the bottom of the loop I save (among other things)

scalar b2   = exp(_b[x]) // beta  

and then put the -scalar- values into a variable in a dataset where I collect the results from the 1000 simulated datasets. For example

quietly replace b2=scalar(b1) in `i'


That works fine. Now I want to calculate "hig" (in the bottom of the loop) and save it in the dataset with aggregated data (where w1, w2, w3 are -scalars-).

scalar hig = 1  if (w1 > w2) & (w1 > w3)  
scalar hig = 2  if (w2 > w1) & (w2 > w3)  
scalar hig = 3  if (w3 > w1) & (w3 > w1)

However, -if- is not allowed in combination with  -scalar-.

Anyone know a better solution for this.

/Tomas  

*
*   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/
Reply | Threaded
Open this post in threaded view
|

RE: scalar do not allow -if-

Nick Cox
A command

... if <condition>

applies observation by observation and can not be what you want.

I guess that you want

if (w1 > w2) & (w1 > w3) {
        scalar hig = 1
}
else if (w2 > w1) & (w2 > w3) {
        scalar hig = 2
}
else if (w3 > w1) & (w3 > w2) {
        scalar hig = 3
}

These sound like "w1 biggest", "w2 biggest", "w3 biggest". If there are any ties w1 == w2 | w2 == w3 your scalar will be undefined. (I've corrected what looks like a typo in your code, but not this problem with ties, which is perhaps unlikely.)  

Nick
[hidden email]

Tomas Lind

In a small simulation I generate data and analyze them with 3 regression methods. In the bottom of the loop I save (among other things)

scalar b2   = exp(_b[x]) // beta  

and then put the -scalar- values into a variable in a dataset where I collect the results from the 1000 simulated datasets. For example

quietly replace b2=scalar(b1) in `i'


That works fine. Now I want to calculate "hig" (in the bottom of the loop) and save it in the dataset with aggregated data (where w1, w2, w3 are -scalars-).

scalar hig = 1  if (w1 > w2) & (w1 > w3)  
scalar hig = 2  if (w2 > w1) & (w2 > w3)  
scalar hig = 3  if (w3 > w1) & (w3 > w1)

However, -if- is not allowed in combination with  -scalar-.


*
*   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/
Reply | Threaded
Open this post in threaded view
|

Re: scalar do not allow -if-

Scott Merryman
In reply to this post by Tomas Lind-2
Something like this?


forv i = 1/3 {
        scalar w`i' = ceil(10*runiform())
}


if scalar(w1) > scalar(w2) & scalar(w1) > scalar(w3) {
        scalar hig = 1
}
else if scalar(w2) > scalar(w1) & scalar(w2) > scalar(w3) {
        scalar hig = 2
}
else if scalar(w3) > scalar(w1) & scalar(w3) > scalar(w2) {
                scalar hig = 3
}
else {
        scalar hig = 0
}
scalar list _all


Scott


On Wed, Nov 17, 2010 at 9:27 AM, Tomas Lind <[hidden email]> wrote:

> Dear Statalisters,
>
> In a small simulation I generate data and analyze them with 3 regression methods. In the bottom of the loop I save (among other things)
>
> scalar b2   = exp(_b[x])        // beta
>
> and then put the -scalar- values into a variable in a dataset where I collect the results from the 1000 simulated datasets. For example
>
> quietly replace b2=scalar(b1) in `i'
>
>
> That works fine. Now I want to calculate "hig" (in the bottom of the loop) and save it in the dataset with aggregated data (where w1, w2, w3 are -scalars-).
>
> scalar hig = 1  if (w1 > w2) & (w1 > w3)
> scalar hig = 2  if (w2 > w1) & (w2 > w3)
> scalar hig = 3  if (w3 > w1) & (w3 > w1)
>
> However, -if- is not allowed in combination with  -scalar-.
>
> Anyone know a better solution for this.
>
> /Tomas
>
> *
> *   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/
Reply | Threaded
Open this post in threaded view
|

Re: scalar do not allow -if-

Maarten buis
In reply to this post by Tomas Lind-2
--- On Wed, 17/11/10, Tomas Lind wrote:

> Now I want to calculate "hig" (in the
> bottom of the loop) and save it in the dataset with
> aggregated data (where w1, w2, w3 are -scalars-).
>
> scalar hig = 1  if (w1 > w2) & (w1 > w3)         
> scalar hig = 2  if (w2 > w1) & (w2 > w3)         
> scalar hig = 3  if (w3 > w1) & (w3 > w1)   
>
> However, -if- is not allowed in combination with 
> -scalar-.


-if- after a command is supposed to work on the dataset,
i.e. the result is a vector of trues and falses, one for
each observation in the dataset. This is why -scalar-
cannot handle it: it needs to be fed one number.

-if- on its own or before a command is supposed to work
on its own and will result in a scalar outcome either
true or false.

So you could get your scalar -hig- by:

if (w1 > w2) & (w1 > w3)  scalar hig = 1          
if (w2 > w1) & (w2 > w3)  scalar hig = 2 
if (w3 > w1) & (w3 > w1)  scalar hig = 3 
 
or:

if (w1 > w2) & (w1 > w3) {
    scalar hig = 1          
}
if (w2 > w1) & (w2 > w3) {
    scalar hig = 2 
}
if (w3 > w1) & (w3 > w1) {
    scalar hig = 3 
}

or:

scalar hig = cond( (w1 > w2) & (w1 > w3), 1,  ///
             cond( (w2 > w1) & (w2 > w3), 2,  ///
             cond( (w3 > w1) & (w3 > w1), 3, .)))

Hope this helps,
Maarten

--------------------------
Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
Germany

http://www.maartenbuis.nl
--------------------------


     

*
*   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/
Reply | Threaded
Open this post in threaded view
|

RE: RE: scalar do not allow -if-

Nick Cox
In reply to this post by Nick Cox
As Scott Merryman's similar solution underlines,
the code here optimistically assumes no clash between scalar and variable names.

Wrapping with -scalar()- means being always safe and never sorry.

Nick
[hidden email]


-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of Nick Cox
Sent: 17 November 2010 16:12
To: '[hidden email]'
Subject: st: RE: scalar do not allow -if-

A command

... if <condition>

applies observation by observation and can not be what you want.

I guess that you want

if (w1 > w2) & (w1 > w3) {
        scalar hig = 1
}
else if (w2 > w1) & (w2 > w3) {
        scalar hig = 2
}
else if (w3 > w1) & (w3 > w2) {
        scalar hig = 3
}

These sound like "w1 biggest", "w2 biggest", "w3 biggest". If there are any ties w1 == w2 | w2 == w3 your scalar will be undefined. (I've corrected what looks like a typo in your code, but not this problem with ties, which is perhaps unlikely.)  

Nick
[hidden email]

Tomas Lind

In a small simulation I generate data and analyze them with 3 regression methods. In the bottom of the loop I save (among other things)

scalar b2   = exp(_b[x]) // beta  

and then put the -scalar- values into a variable in a dataset where I collect the results from the 1000 simulated datasets. For example

quietly replace b2=scalar(b1) in `i'


That works fine. Now I want to calculate "hig" (in the bottom of the loop) and save it in the dataset with aggregated data (where w1, w2, w3 are -scalars-).

scalar hig = 1  if (w1 > w2) & (w1 > w3)  
scalar hig = 2  if (w2 > w1) & (w2 > w3)  
scalar hig = 3  if (w3 > w1) & (w3 > w1)

However, -if- is not allowed in combination with  -scalar-.


*
*   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/
Reply | Threaded
Open this post in threaded view
|

SV: st: scalar do not allow -if-

Tomas Lind-2
In reply to this post by Maarten buis
Thanks Nick, Scott and Maarten,

Many thanks for helpful code and helpful comments and explanations.

/Tomas



-----Ursprungligt meddelande-----
Från: [hidden email] [mailto:[hidden email]] För Maarten buis
Skickat: den 17 november 2010 17:17
Till: [hidden email]
Ämne: Re: st: scalar do not allow -if-

--- On Wed, 17/11/10, Tomas Lind wrote:
> Now I want to calculate "hig" (in the
> bottom of the loop) and save it in the dataset with aggregated data
> (where w1, w2, w3 are -scalars-).
>
> scalar hig = 1  if (w1 > w2) & (w1 > w3) scalar hig = 2  if (w2 > w1)
> & (w2 > w3) scalar hig = 3  if (w3 > w1) & (w3 > w1)
>
> However, -if- is not allowed in combination with -scalar-.


-if- after a command is supposed to work on the dataset, i.e. the result is a vector of trues and falses, one for each observation in the dataset. This is why -scalar- cannot handle it: it needs to be fed one number.

-if- on its own or before a command is supposed to work on its own and will result in a scalar outcome either true or false.

So you could get your scalar -hig- by:

if (w1 > w2) & (w1 > w3)  scalar hig = 1 if (w2 > w1) & (w2 > w3)  scalar hig = 2 if (w3 > w1) & (w3 > w1)  scalar hig = 3 
 
or:

if (w1 > w2) & (w1 > w3) {
    scalar hig = 1
}
if (w2 > w1) & (w2 > w3) {
    scalar hig = 2
}
if (w3 > w1) & (w3 > w1) {
    scalar hig = 3
}

or:

scalar hig = cond( (w1 > w2) & (w1 > w3), 1,  ///
             cond( (w2 > w1) & (w2 > w3), 2,  ///
             cond( (w3 > w1) & (w3 > w1), 3, .)))

Hope this helps,
Maarten

--------------------------
Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
Germany

http://www.maartenbuis.nl
--------------------------


     

*
*   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/
Reply | Threaded
Open this post in threaded view
|

RE: RE: scalar do not allow -if-

Maarten buis
In reply to this post by Nick Cox
--- Nick wrote:
> the code here optimistically assumes no clash between scalar
> and variable names.
>
> Wrapping with -scalar()- means being always safe and never
> sorry.

An alternative solution that I often prefer (mostly because
I am used to it) is:

tempname w1 w2 w3
scalar `w1' = ...
scalar `w2' = ...
scalar `w3' = ...

Hope this helps,
Maarten

--------------------------
Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
Germany

http://www.maartenbuis.nl
--------------------------


     

*
*   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/
Reply | Threaded
Open this post in threaded view
|

RE: RE: scalar do not allow -if-

Nick Cox
I agree in terms of my own programming.

The original question was focused on saving scalars as results, in which case temporary names that disappear when a program has finished are of no use.

There is a middle way. Within a -program- (declared as such) use temporary names for scalars as here, and then when all is done use -return- or -ereturn- to define scalars as r() or e() class results.

A broader discussion is accessible at

SJ-6-2  dm0021  . Stata tip 31: Scalar or variable? Problem of ambiguous names
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  G. I. Kolev
        Q2/06   SJ 6(2):279--280                                 (no commands)
        tips for avoiding abbreviation conflicts with variables
        when naming scalars

Nick
[hidden email]

Maarten buis

--- Nick wrote:
> the code here optimistically assumes no clash between scalar
> and variable names.
>
> Wrapping with -scalar()- means being always safe and never
> sorry.

An alternative solution that I often prefer (mostly because
I am used to it) is:

tempname w1 w2 w3
scalar `w1' = ...
scalar `w2' = ...
scalar `w3' = ...


*
*   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/
Reply | Threaded
Open this post in threaded view
|

RE: scalar do not allow -if-

Sergiy Radyakin
In reply to this post by Nick Cox
On Wed, Nov 17, 2010 at 11:11 AM, Nick Cox <[hidden email]> wrote:

> A command
>
> ... if <condition>
>
> applies observation by observation and can not be what you want.
>
> I guess that you want
>
> if (w1 > w2) & (w1 > w3) {
>        scalar hig = 1
> }
> else if (w2 > w1) & (w2 > w3) {
>        scalar hig = 2
> }
> else if (w3 > w1) & (w3 > w2) {
>        scalar hig = 3
> }
>
> These sound like "w1 biggest", "w2 biggest", "w3 biggest". If there are any ties w1 == w2 | w2 == w3 your scalar will be undefined. (I've corrected what looks like a typo in your code, but not this problem with ties, which is perhaps unlikely.)
>
> Nick
> [hidden email]
>
> Tomas Lind
>
> In a small simulation I generate data and analyze them with 3 regression methods. In the bottom of the loop I save (among other things)
>
> scalar b2   = exp(_b[x])        // beta
>
> and then put the -scalar- values into a variable in a dataset where I collect the results from the 1000 simulated datasets. For example
>
> quietly replace b2=scalar(b1) in `i'
>
>
> That works fine. Now I want to calculate "hig" (in the bottom of the loop) and save it in the dataset with aggregated data (where w1, w2, w3 are -scalars-).
>
> scalar hig = 1  if (w1 > w2) & (w1 > w3)
> scalar hig = 2  if (w2 > w1) & (w2 > w3)
> scalar hig = 3  if (w3 > w1) & (w3 > w1)
>
> However, -if- is not allowed in combination with  -scalar-.



the scalar version of -if- is cond():
scalar hig = cond(w2>w1,cond(w3>w2,3,2),cond(w3>w1,3,1))
although if in your example you had w1,w2,...w100, you wouldn't want
to write anything like the above.
you would then program one -forvalues- loop and one -if- branch that
updates the index of the max element.

Best, Sergiy Radyakin

>
>
> *
> *   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/
Reply | Threaded
Open this post in threaded view
|

RE: scalar do not allow -if-

Nick Cox
Yes and no; the code is good, but the commentary could be misleading.

You can certainly use -cond()- to define these scalars, as others have already pointed out in this thread.

But its role is not analogous to that of the -if- qualifier. -if- qualifiers apply conditionally to a subset of observations (which may be empty or the whole set), but no such notion applies to scalars.

Nick
[hidden email]

Sergiy Radyakin

the scalar version of -if- is cond():
scalar hig = cond(w2>w1,cond(w3>w2,3,2),cond(w3>w1,3,1))
although if in your example you had w1,w2,...w100, you wouldn't want
to write anything like the above.
you would then program one -forvalues- loop and one -if- branch that
updates the index of the max element.

On Wed, Nov 17, 2010 at 11:11 AM, Nick Cox <[hidden email]> wrote:

> A command
>
> ... if <condition>
>
> applies observation by observation and can not be what you want.
>
> I guess that you want
>
> if (w1 > w2) & (w1 > w3) {
>        scalar hig = 1
> }
> else if (w2 > w1) & (w2 > w3) {
>        scalar hig = 2
> }
> else if (w3 > w1) & (w3 > w2) {
>        scalar hig = 3
> }
>
> These sound like "w1 biggest", "w2 biggest", "w3 biggest". If there are any ties w1 == w2 | w2 == w3 your scalar will be undefined. (I've corrected what looks like a typo in your code, but not this problem with ties, which is perhaps unlikely.)

Tomas Lind

> In a small simulation I generate data and analyze them with 3 regression methods. In the bottom of the loop I save (among other things)
>
> scalar b2   = exp(_b[x])        // beta
>
> and then put the -scalar- values into a variable in a dataset where I collect the results from the 1000 simulated datasets. For example
>
> quietly replace b2=scalar(b1) in `i'
>
>
> That works fine. Now I want to calculate "hig" (in the bottom of the loop) and save it in the dataset with aggregated data (where w1, w2, w3 are -scalars-).
>
> scalar hig = 1  if (w1 > w2) & (w1 > w3)
> scalar hig = 2  if (w2 > w1) & (w2 > w3)
> scalar hig = 3  if (w3 > w1) & (w3 > w1)
>
> However, -if- is not allowed in combination with  -scalar-.



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