Wow -- and thanks! This makes me like the two or three line Cholesky
work-around that much better, at least for a vector!
Professor of Sociology and
Director, Population Studies Center
230 McNeil Building
3718 Locust Walk CR
University of Pennsylvania
Philadelphia, PA 19104-6298
[email protected]
215.898.7768 (office)
215.898.2124 (fax)
On Fri, 5 Jan 2007, Nick Cox wrote:
> -matmap- from SSC offers users of Stata 6 .. 8
> a way to get element-wise square roots (and more
> besides).
>
> Here is the program, and pretty horrible it is
> too. I didn't do anything further with it because
> I became aware that Bill Gould had a much better program
> for elementwise stuff, but he never got round to
> releasing it, given the alternative he was developing
> called Mata.
>
> For Stata 8, -matmap- can be rewritten, as below.
>
> -------------------------- matmap from SSC
> *! 1.0.0 NJC 23 August 2000
> program define matmap
> version 6.0
> gettoken A 0 : 0
> gettoken B 0 : 0, parse(" ,")
> syntax , Map(str) [ Symbol(str) ]
>
> if "`symbol'" == "" { local symbol "@" }
>
> if !index("`map'","`symbol'") {
> di in r "map( ) does not contain `symbol'"
> exit 198
> }
>
> local nr = rowsof(matrix(`A'))
> local nc = colsof(matrix(`A'))
>
> tempname C val
> mat `C' = `A'
>
> local i 1
> while `i' <= `nr' {
> local j 1
> while `j' <= `nc' {
> local exp : /*
> */ subinstr local map "`symbol'" "`A'[`i',`j']", all
> scalar `val' = `exp'
> if `val' == . {
> di in r "matrix would have missing values"
> exit 504
> }
> mat `C'[`i',`j'] = `val'
> local j = `j' + 1
> }
> local i = `i' + 1
> }
>
> mat `B' = `C' /* allows overwriting of either `A' or `B' */
> end
> ----------------------------------------
>
> --------------------------- matmap8
> *! 1.1.0 NJC 5 January 2007
> program matmap8
> version 8
> gettoken A 0 : 0
> gettoken B 0 : 0, parse(" ,")
> syntax , Map(str) [ Symbol(str) ]
>
> if "`symbol'" == "" local symbol "@"
>
> if !index("`map'","`symbol'") {
> di as err "map() does not contain `symbol'"
> exit 198
> }
>
> tempname C val
> mat `C' = `A'
>
> forval i = 1/`= rowsof(matrix(`A'))' {
> forval j = 1/`= colsof(matrix(`A'))' {
> local exp : ///
> subinstr local map "`symbol'" "`A'[`i',`j']", all
> mat `C'[`i',`j'] = `exp'
> }
> }
>
> mat `B' = `C' /* allows overwriting of either `A' or `B' */
> end
> ---------------------------------
>
> But the best way to do e.w. square roots is like this:
>
> mat A = J(5,5,3)
> forval i = 1/5 {
> forval j = 1/5 {
> mat A[`i',`j'] = sqrt(A[`i',`j'])
> }
> }
>
> Nick
> [email protected]
>
> Herb Smith
> >
> > Mis-step 2: Okay, what about using the old -matrix-
> > commands? When I had
> > a vector of coefficient variances, I could locate within
> > -mata- a simple
> > command for taking element-by-element square roots, but not
> > under the old
> > -matrix- commands. I ended up turning this vector into a
> > diagonal matrix
> > and getting square roots via Cholesky; but this is inelegant
> > and hardly
> > the sort of thing that gets social science graduate students
> > into using a
> > set of commands to do things....
>
> *
> * 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/
>
*
* 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/