Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[no subject]



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

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




-- 
Zhi Su
348 Holmes Hall
Northeastern University
360 Huntington Avenue
Boston, MA 02115
Office:1-617-373-2316
email:su.zh@husky.neu.edu
*
*   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/


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