 Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: Re: Re: st: pick maximum,minimum, mode and median among scalars

 From Tirthankar Chakravarty To statalist@hsphsun2.harvard.edu Subject Re: Re: Re: st: pick maximum,minimum, mode and median among scalars Date Wed, 10 Aug 2011 14:05:37 -0700

```Nick has answered the first part of the question.

The extension you seek is an application of the code I sent you
yesterday. I leave finding the numbers corresponding to the median and
the minimum for you. If you run into difficulties, let me know.

/***********************************************/
clear*
sysuse auto, clear
set matsize 1200

foreach n1 in 0 5 10 {
foreach n2 in 0 5 10 {
foreach n3 in 0 5 10 {
foreach n4 in 0 5 10 {

generate su_`n1'_`n2'_`n3'_`n4'= ///
(`n1'*mpg)/10+(`n2'*weight)/10+ ///
generate price_`n1'_`n2'_`n3'_`n4'= ///
price- su_`n1'_`n2'_`n3'_`n4'
regress price_`n1'_`n2'_`n3'_`n4'   ///
turn displacement gear_ratio foreign, robust
mat mBeta= (nullmat(mBeta) \  e(b))
mat mN = (nullmat(mN)\ (`n1', `n2', `n3', `n4'))

}
}
}
}
local matcolnames: colnames mBeta
mat colnames mN = n1 n2 n3 n4
local matcolnames2: colnames mN
mat mA = (mBeta, mN)
matlist mA

mata

mata set matastrict off
version 11.2
// function to return the max index of rows of a matrix
real colvector function fnMatMaxIndex(real matrix mA)
{
real vector vMaxIndex
vMaxIndex=J(rows(mA), 1, .)
w=.
v=.
for(i=1; i<= rows(mA); i++)
{
maxindex(mA[i, .]', 1, v, w)
vMaxIndex[i, 1]=v
}
return(vMaxIndex)
}

mBeta = st_matrix("mBeta")
mN = st_matrix("mN")

// find the column maxima and minima
vMaxIndex = fnMatMaxIndex(mBeta')'
mMaxN = mN[vMaxIndex, .]

st_matrix("mMaxN", mMaxN)
end

matrix rownames mMaxN = `matcolnames'
matrix colnames mMaxN = `matcolnames2'
matlist mMaxN

/*********************************************/

T

On Wed, Aug 10, 2011 at 9:55 AM, Nick Cox <njcoxstata@gmail.com> wrote:
> Negating values interchanges minimum and maximum.
>
> Consider 1,2,3,7,42 and (after negating and sorting) -42, -7, -3, -2, -1.
>
> Nick
>
> On Wed, Aug 10, 2011 at 4:59 PM, Zhi Su <su.zh@husky.neu.edu> wrote:
>>  Why entering parameter (-mBeta) in
>> the mata function can produce index for minimum?
>
> *
> *   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/
>

--
Tirthankar Chakravarty
tchakravarty@ucsd.edu
tirthankar.chakravarty@gmail.com

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