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]
Re: Re: st: pick maximum,minimum, mode and median among scalars
From
Tirthankar Chakravarty <[email protected]>
To
[email protected]
Subject
Re: Re: st: pick maximum,minimum, mode and median among scalars
Date
Tue, 9 Aug 2011 17:27:22 -0700
Su,
You can keep track of regressors by naming the columns of the matrices
produced - you can choose to omit rows after the calculations in the
final vector produced. See the code below.
Also, since Mata lacks a function to find the column indices of the
row maxima/minima of a matrix, I have written you one in Mata and show
you how to use it to find the constraint number that produces the
maximum and minimum (the constraint number is simply the column index
of the maximum).
/***************************************************/
clear*
sysuse auto, clear
constraint 1 price = weight
constraint 2 displacement = weight
constraint 3 gear_ratio = -foreign
constraint 4 _cons = 0
forv i=1/4 {
cnsreg mpg price weight displacement gear_ratio foreign length, c(`i')
mat mBeta = (nullmat(mBeta) , e(b)')
}
matlist mBeta
local matrownames: rownames mBeta
// max and min
mata: st_matrix("vBetaMax", rowmax(st_matrix("mBeta")))
mata: st_matrix("vBetaMin", rowmin(st_matrix("mBeta")))
mat rownames vBetaMax =`matrownames'
mat rownames vBetaMin =`matrownames'
// median (needs Ben Jann's -moremata-, SSC)
mata mata mlib index
mata: st_matrix("vBetaMed", mm_quantile(st_matrix("mBeta")', 1, .5 )')
mat rownames vBetaMed = `matrownames'
mat mMatStat = (vBetaMax, vBetaMin, vBetaMed)
mat colnames mMatStat = Max Min Median
matlist mMatStat
// identify the index of row max and min
capture mata mata drop fnMatMaxIndex()
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)
}
// test the function
mA = runiform(10, 5)
vI = fnMatMaxIndex(mA)
vI
// find the column index of the row maximum for each regressor
mBeta = st_matrix("mBeta")
vMaxIndex = fnMatMaxIndex(mBeta)
st_matrix("vBetaMaxIndex", vMaxIndex)
vMinIndex = fnMatMaxIndex(-mBeta)
st_matrix("vBetaMinIndex", vMinIndex)
end
mat rownames vBetaMaxIndex = `matrownames'
mat rownames vBetaMinIndex = `matrownames'
mat mBetaMinMaxIndex = (vBetaMaxIndex, vBetaMinIndex)
mat colnames mBetaMinMaxIndex = MaxIndex MinIndex
matlist mBetaMinMaxIndex
/*******************************************************/
T
On Tue, Aug 9, 2011 at 4:31 PM, Zhi Su <[email protected]> wrote:
> Dear Tirthankar,
> Thanks for the quick answer. I still have a few question.
> You suggest to use
> forv i=1/4 {
> cnsreg mpg price weight displacement gear_ratio foreign length, c(`i')
> mat mBeta = (nullmat(mBeta) , e(b)')
> }
> What if I only want to record some coefficients not all the
> coefficients in the matrix mBeta? For example, I only want to record
> coefficients of "price" and "displacement" in this case.
>
> And in this case, I get the maximum value for coefficient of "price"
> is .00009169. How can I identify under which constraint the regresion
> produce the maximum value of the coefficient for "price"?
>
> Thank you!
>
> Su
> --------------------------------------------------------------------------------
> From Tirthankar Chakravarty <[email protected]>
> To [email protected]
> Subject Re: st: pick maximum,minimum, mode and median among scalars
> Date Tue, 9 Aug 2011 15:16:08 -0700
>
> --------------------------------------------------------------------------------
>
> A scalable way of doing this without the need to save individual
> coefficients uses Mata (and Ben Jann's -moremata- package, SSC).
>
> /******************************************/
> clear*
> sysuse auto, clear
> constraint 1 price = weight
> constraint 2 displacement = weight
> constraint 3 gear_ratio = -foreign
> constraint 4 _cons = 0
>
> forv i=1/4 {
> cnsreg mpg price weight displacement gear_ratio foreign length, c(`i')
> mat mBeta = (nullmat(mBeta) , e(b)')
> }
> mat list mBeta
>
> // max and min
> mata: st_matrix("vBetaMax", rowmax(st_matrix("mBeta")))
> mata: st_matrix("vBetaMin", rowmin(st_matrix("mBeta")))
> mat list vBetaMax
> mat list vBetaMin
>
> // median (needs Ben Jann's -moremata-, SSC)
> mata mata mlib index
> mata: st_matrix("vBetaMed", mm_quantile(st_matrix("mBeta")', 1, .5 )')
> mat list vBetaMed
> /******************************************/
>
>
> T
>
>
> On Tue, Aug 9, 2011 at 2:52 PM, Zhi Su <[email protected]> wrote:
>> Dear Statalists,
>>
>> I wan to get some statistics of coefficients of the same independent
>> in regressions subjected to different constraints.
>> For example, I run the following regression four times. Each time
>> the regression is subjected to a constraint. Here is the code for
>> regression in constraint 1.
>>
>> cnsreg y x1 x2 x3 x4,constraints(1)
>> scalar b11=_b[x1]
>> scalar b12=_b[x2]
>> scalar b13=_b[x3]
>> scalar b14=_b[x4]
>> then I save the coefficients for x1, x2, x3, x4 in scalars
>>
>> Then I get a group of scalars:
>> b11 b12 b13 b14 for the regression with constraint1
>> b21 b22 b23 b24 for the regression with constraint2
>> b31 b32 b33 b34 for the regression with constraint3
>> b41 b42 b43 b44 for the regression with constraint4
>>
>> Now b11 b21 b31 b41 are all coefficients for x1. I want to determine
>> which one among them is maximum,minimum, mode and median of this group
>> of scalars.
>> What if the number of constraints is over 100, how can I pick the
>> maximum,minimum, mode and median among a group of scalars of interest?
>>
>> Thank you!
>
>
> --
> Zhi Su
> 348 Holmes Hall
> Northeastern University
> 360 Huntington Avenue
> Boston, MA 02115
> Office:1-617-373-2316
> email:[email protected]
> *
> * 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
[email protected]
[email protected]
*
* 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/