Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Re: RE: Re: manipulating matrix elements


From   "Michael Blasnik" <[email protected]>
To   <[email protected]>
Subject   st: Re: RE: Re: manipulating matrix elements
Date   Tue, 28 Mar 2006 17:35:18 -0500

One more twist/caveat I discovered is that you can get unexpected results from the code I posted because:

scalar x=x+p*ln(p)

will refer to any variable beginning with the letter p before it goes to look at the fact that scalar p exists. I tried my code on the auto dataset and found that it used price in the calculation, not the scalar I intended. Instead, the code should read:

scalar x=x+scalar(p)*ln(scalar(p))

and Nick's corrected code should also use the scalar() notation. In an ado file, I would have used tempname to name the scalar and this problem wouldn't occur.

Michael Blasnik

----- Original Message ----- "Nick Cox" <[email protected]> wrote:

A twist to this problem is that it is customary
in entropy calculations to define 0 ln 0 as 0.

However, Stata doesn't know that and will
return missing instead. So, it is necessary
to trap 0 within some expression as

scalar x = x  + cond(p = 0, 0, p * ln(p))

Nick
[email protected]

Michael Blasnik

-findit matmap- will lead you to the matmap package which can perform
arbitrary elementwise operations on matrices, but since you
want the sum of
the calculation across all elements, I think you should just
do the looping
yourself.  Here's one approach (untested):

scalar x=0
tab var1 var2, matcell(Cell)
matrix P = Cell/r(N)
local rows=rowsof(P)
local cols=colsof(P)
forvalues i=1(1)`rows' {
    forvalues j=1(1)`cols' {
        scalar p=P[`i',`j']
        scalar x=x+p*ln(p)
  }
}
scalar x=x+ln(`rows'*`cols')
di x

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



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index