gm_mean: Geometric mean

View source: R/tables.R

gm_meanR Documentation

Geometric mean

Description

Calculates the geometric mean with confidence interval. If conf.level is given, a vector will be output containing the geometric mean, the lower bound confidence value, and the upper bound confidence value. Output can be used directly in the function format_values to create summary tables. Loosely based on this stackoverflow post.

Usage

gm_mean(x, offset = 0, zero.propagate = FALSE, conf.level = 0.95, na.rm = TRUE)

Arguments

x

numeric vector as input

offset

Optional variable: adds a value to the data (1 is often used), so that no more zeros occur in the data. This is an acceptable way of handling zeros when, for example, zeros represent undetectable low concentrations in a biomedical laboratory assay.

zero.propagate

if TRUE, the output will be zero if any value is zero. Overrides the offset parameter is true.

conf.level

If not missing, the confidence interval will be given together with the geometric mean

na.rm

logical, indicating whether NA values should be excluded

Details

Standard zeros are omitted from the data and mean values are calculated as follows: exp(sum(log(x[x > 0]), na.rm=na.rm) / length(x)). Side effect can be that, when there are many zeros in the data, the geometric mean could fall outside the confidence interval, since zeros are not omitted from length(x). To prevent this, you can use a different way to handle zeros (for example, offset = 1).

Value

a vector of length 1 or, if conf.level is not missing, length 3

Examples

gm_mean(c(1,4,2,3))

LDSamson/associationstudies documentation built on April 14, 2025, 11:52 a.m.