gm_mean | R Documentation |
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.
gm_mean(x, offset = 0, zero.propagate = FALSE, conf.level = 0.95, na.rm = TRUE)
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 |
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
).
a vector of length 1 or, if conf.level is not missing, length 3
gm_mean(c(1,4,2,3))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.