*! version 1.0 30 Mar 2021

Title

[D] egen -- User written egen function wmean(), mnemonic for weighted mean:
calculates (optionally) byable, (optionally) weighted, Arithmetic, Geometric or Harmonic mean.
Requires Stata 11. Written by Gueorgui I. Kolev in March 2021.

Syntax

egen
newvarname
= wmean(expression)
[if]
[in]
[,
options]

Optional
by(varlist)
The user can optionally specify by(varlist), and the function will calculate the weighted mean for each of the groups defined by(varlist). If by(varlist) is not specified, the function calculates the overall weighted mean.

weights(varname)
The user can optionally specify weights(varname), where this option can be abbreviated down to w(varname). If the option is specified, the function calculates the weighted mean.
If the option is not specified, the function calculates the unweighted mean, which is equivalent to a weighted mean where all the weights are equal to 1.

arithmetic
Requires the calculation of the Arithmetic mean and is the default. The option can be abbreviated down to a, or omitted altogether.
If none of the options arithmetic, geometric and harmonic is specified, the function calculates arithmetic mean, the default.
The formula is Arithmetic Mean = Sum(Wi*Xi)/Sum(Wi), where Wi are the weights, Xi is the expression being averaged (can be a variable or can be a general expression as well).
The formula is applied for each group defined by(varlist) if the latter is specified.

geometric
Requires the calculation of the Geometric mean, and the option can be abbreviated down to g. The formula is Geometric Mean = exp.
The Geometric mean is defined only for all positive Xi. If some of the Xi are negative or 0, the function will calculate Geometric mean on the basis of the positive Xi only.

harmonic
Requires the calculation of the Harmonic mean, and the option can be abbreviated down to h. The formula is Harmonic Mean = 1/[Sum(Wi/Xi)/Sum(Wi)]. The Harmonic mean is defined only for all positive Xi.
If some of the Xi are negative or 0, the function will calculate Harmonic mean on the basis of the positive Xi only.

label
The option can be abbreviated down to l, and if specified will result in the new generated variable being labelled, e.g., "Arithmetic Mean of Xi".

Description

Optionally byable, optionally weighted, Arithmetic, Geometric, or Harmonic mean.

Options

Main

by(varlist)

weights(varname)

arithmetic

geometric

harmonic

label

Examples

sysuse auto, clear

keep foreign price weight

* Introduce some missing, and negative values

sort foreign

replace price = . in 1/4

replace weight = . in 4/7

replace price = -price in -3/l

egen arimean = wmean(price), by(foreign) weights(weight) label // the default is Arithmetic mean,
//Weights can be abbreviated to w. Option Label can be abbreviated to l, and labels the new generated variable.

egen geomean = wmean(price), by(foreign) w(weight) geometric
// Geometric mean option can be abbreviated to g.

egen harmean = wmean(price), by(foreign) w(weight) harmonic label // Harmonic mean option can be
// abbreviated to h.

* The native Stata's -ameans- calculates on this data the same Arithmetic,
* Geometric, and Harmonic means as our -egen, wmean- function above.

by foreign: ameans price [aw=weight]

tabstat arimean geomean harmean, by(foreign) stat(mean count) notot

* And an example where the argument of the function is a general expression, and with If and In.

egen arimeanexpre = wmean(log(price)*price) if weight>3000 in 10/l, by(foreign) weights(weight) label

Author

Gueorgui I. Kolev

Version 1: 5 March 2021.