Statalist


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

st: re: matrix manipulation


From   Kit Baum <[email protected]>
To   [email protected]
Subject   st: re: matrix manipulation
Date   Fri, 12 Dec 2008 10:54:14 -0500

< >
As Austin notes, this can be done as a function. He points out the way to avoid a loop to zap the diagonal. I would rather do it as a Mata function than an inline call. Both work, but the inline call IMHO leads to write-only code. The function approach is attractive in that you can readily clone the function and tweak it when you have something a bit different (and perhaps more complicated) to achieve.

In any case, whether done as Mata function or in-line call, using Mata for this purpose beats the old Stata matrix language seven ways from Sunday.

-------------
mata: mata clear
mata:
void underbar(string scalar mat, real scalar thresh)
{
	real scalar i
	i = rows(st_matrix(mat))
st_matrix("B", (st_matrix(mat) :< thresh) :/ st_matrix(mat) :* (J(i,i, 1) - I(i)) )
}
end

sysuse auto,clear
qui reg price trunk weight length turn
mat list e(V)
mata: underbar("e(V)", 10)
mat list B
---------------


Kit Baum, Boston College Economics and DIW Berlin
http://ideas.repec.org/e/pba1.html
An Introduction to Modern Econometrics Using Stata:
http://www.stata-press.com/books/imeus.html


*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index