Statalist The Stata Listserver


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

Re: st: FW: argmax for -summaraize-


From   Joseph Coveney <jcoveney@bigplanet.com>
To   Statalist <statalist@hsphsun2.harvard.edu>
Subject   Re: st: FW: argmax for -summaraize-
Date   Tue, 25 Jul 2006 16:04:36 +0900

Alan H. Feiveson wrote:

Hello - Does anyone know an efficient way to identify the observation at
which a particular variable is minimum or maximum (subject to `if'
and/or `in') ?

Apparently -summarize- does not return this value. I see nothing in
-egen- nor  does "findit argmax" produce anything. I can program this
myself by looping through the observations but that is not efficient. In
particular one cannot gurantee that anything like

summ x
local xmax=r(max)
if x = `xmax' {
...

will work because of rounding. I also wish to avoid -preserve-,
-collapse-, etc

--------------------------------------------------------------------------------

Couldn't you just -generate- a 0/1 indicator variable?  Then just use the
indicator in your Boolean expression:  -if indicator_variable . . .-

Generating such a variable (1) allows for both -if- and -in-, (2) won't be
affected by missing values in the target variable, and (3) doesn't appear to
be liable to rounding errors regardless of whether the target variable is
single- or double-precision:  one (and only one) maximum observation is 
identified in each of 1000 200-observation datasets.

Joseph Coveney

clear
set more off
set seed `=date("2006-07-25", "ymd")'
set matsize 10000
tempname A
tempvar a max
set obs 200
generate double `a' = . // double-precision
generate byte `max' = 0
forvalues i = 1/1000 {
    quietly replace `a' = uniform()
    summarize `a', meanonly
    quietly replace `max' = (`a' == r(max)) if (1==1) in 1/200
    summarize `max', meanonly
    matrix define `A' = (nullmat(`A') \ r(sum))
}
drop _all
svmat byte `A', names(col)
assert c1 == 1
*
clear
set obs 200
generate float `a' = . // single-precision
generate byte `max' = 0
forvalues i = 1/1000 {
    quietly replace `a' = uniform()
    summarize `a', meanonly
    quietly replace `max' = (`a' == r(max)) if (1==1) in 1/200
    summarize `max', meanonly
    matrix define `A' = (nullmat(`A') \ r(sum))
}
drop _all
svmat byte `A', names(col)
assert c1 == 1
exit


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