*! NJC 20nov2016/15jun2020/29jun2021/11jan2024/19jun2024
help entropyetc

Title

Entropy and related measures for categories

Syntax

entropyetc
varname
[if] [in]
[weight]
[
,
list
generate(newvar_spec)
by(byvarlist)
list_options
]

aweights and fweights are allowed; see weight.

Description

entropyetc treats varname, which may be numeric or string, as
a categorical variable, so that distinct values define distinct
categories, and calculates

(1) the number of distinct categories observed

(2) Shannon entropy H

(3) exp H

(4) Simpson's sum of squared probabilities R

(5) 1/R.

Optionally, results may be listed. Optionally,
new variables may be generated containing results.
Hence the syntax supports those who want a listing, but not
new variables, and conversely.

Hence you should ask for at least one of those options.
Otherwise, there is nothing to do.

Remarks

Given S categories of varname, calculate their relative
frequencies, applying any weights specified, as proportions p with
sum 1. In ecology at least, S is conventional for "number of
species". Then

Shannon entropy is SUM p ln 1/p =: H, say. It is often
written more concisely but more cryptically as - SUM p ln p.
If one value of p is 1, this index takes on a minimum value of 0.
If all p are equal at 1/S, it takes on a maximum value of ln
S. This behaviour motivates looking at exp H as a "numbers
equivalent".

Those preferring to use logarithms to base 2 or 10 should divide results
by ln 2 or ln 10 respectively.

Simpson's index is SUM p^2 =: R, say. If one value of p is
1, and the others thus all 0, this index takes on a maximum value of 1.
If all p are equal at 1/S, it takes on a minimum value of
1/S. This behaviour motivates looking at 1/R as a "numbers
equivalent".

Simpson's index has been discovered or invented many times, sometimes in
the form of its complement or its reciprocal. It is also named repeat
rate and match probability. In particular circumstances it measures
homozygosity or purity of classifications, so its complement measures
heterozygosity or impurity. It is also associated directly or indirectly
with (this is far from a complete list) Gini, W.F. Friedman, Turing,
Hirschman, Herfindahl and Blau.

Options

list calls for listing of results.

generate() specifies the creation of between one and five new
variables from results. The syntax
is exemplified by generate(2=H 4=R). The elements of the
specification are #=newvar, where # is an integer
between 1 and 5 and newvar is a legal new variable name. Elements
must not contain spaces and are separated by spaces. In this example,
values corresponding to the second result are saved
as H and values corresponding to the fourth result as R. Values of
new variables are copied to all observations used in their calculation.

by() specifies that calculations should be performed separately
for groups defined by byvarlist.

list_options are options of list controlling the
display of results.
The default display format is %1.0f for the number of distinct
categories and %4.3f otherwise. The latter may be changed
using the format() option.

Examples

. sysuse auto, clear

. entropyetc rep78, list

. entropyetc rep78, list by(foreign)

. webuse nlsw88

. entropyetc occupation, list by(industry) gen(3=numeq)

. egen tag = tag(industry)

. graph dot (asis) numeq if tag, over(industry, sort(1) descending) ysc(alt) linetype(line) lines(lc(gs8) lw(vthin))

. webuse grunfeld, clear

. entropyetc company [w=invest], list by(year)

Author

Nicholas J. Cox, Durham University

n.j.cox@durham.ac.uk

Acknowledgments

Paris Rira reported a problem if 5 variables were named in generate().

Also see

divcat (SSC; if installed);

distinct (Stata Journal; if installed)