*! NJC 20nov2016/15jun2020/29jun2021/10jan2024
help entropyetc2

Title

Entropy and related measures for categories (version 2)

Syntax

entropyetc2
varname
[if] [in]
[weight]
[
,
by(byvarlist)
generate(newvar_spec)
tabdisp_options
]

aweights and fweights are allowed; see weight.

Description

entropyetc2 treats varname, which may be numeric or string, as
a categorical variable, so that distinct values define distinct
categories, and calculates Shannon entropy H, exp H, Simpson's
sum of squared probabilities R, 1/R, and the dissimilarity
index D. Results are displayed and saved as a matrix. Optionally,
new variables may be generated containing results.

Note January 2024

This is version 2 of entropyetc, renamed. For most purposes,
it is considered to be superseded by the latest version, still named
entropyetc. The main difficulty with version 2 was that it made use of
various features or commands that pose limits on the number of categories that
can be handled in one or more versions of Stata, namely Stata matrices,
tabulate, and tabdisp. In any case, even if researchers were using
a version of Stata that did not pose problems, listing of results by default
for hundreds or thousands of categories is of little or no interest when the
goal of using the command is just to calculate new variables.

This 2024 version makes some small fixes noticed in 2020 and 2021 but
somehow not posted to SSC.

Researchers concerned with reproducibility will need to rename
this version entropyetc, or alternatively to edit their do files to use
the command under this name.

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 with (this is far from
a complete list) Gini, Turing, Hirschman and Herfindahl.

Dissimilarity index is (1 / 2) SUM | p - 1/S | =: D,
say. If one value of p is 1, this index takes on a maximum value
of (S - 1) / S. If all p are equal at 1/S, it
takes on a minimum value of 0.

Note that the dissimilarity index, unlike the others, is affected by
categories with probabilities of zero. entropyetc2 always compares
groups according to the number of categories present in all the data
presented, thus allowing some zeros to be included in the calculation
for individual groups.

Options

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

generate() specifies the creation of between one and five new
variables from results in different columns of the display. The syntax
is exemplified by generate(1=H 3=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 first displayed column, column 1, are saved
as H and values corresponding to column 3 as R. Values of
new variables are copied to all observations used in their calculation.

tabdisp_options are options of tabdisp controlling the
tabulation of results. The default display format is %4.3f.

Examples

. sysuse auto

. entropyetc2 rep78

. entropyetc2 rep78, by(foreign)

. webuse nlsw88

. entropyetc2 occupation, by(industry) gen(2=numeq)

. egen tag = tag(industry)

. graph dot (asis) numeq if tag, over(industry, sort(1) descending) linetype(line)

. webuse grunfeld, clear

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

Author

Nicholas J. Cox, Durham University

n.j.cox@durham.ac.uk

Acknowledgments

Mike Lacy pointed out that the columns of the saved matrix were incorrectly labelled.

Stored results

entropyetc2 stores the following in r():

Scalars

r(categories) number of categories

Matrices

r(entropyetc2) matrix of results

Also see

divcat (SSC; if installed)