Learn R Programming

qwraps2 (version 0.5.2)

geometric_mean_var_sd: Geometric Mean, Variance, and Standard Deviation

Description

Return the geometric mean, variance, and standard deviation,

Usage

gmean(x, na_rm = FALSE)

gvar(x, na_rm = FALSE)

gsd(x, na_rm = FALSE)

Arguments

x

a numeric vector

na_rm

a logical value indicating whether NA values should be stripped before the computation proceeds.

Value

a numeric value

See Also

gmean_sd for easy formatting of the geometric mean and standard deviation. vignette("summary-statistics", package = "qwraps2").

Examples

Run this code
# NOT RUN {
set.seed(42)
x <- runif(14, min = 4, max = 70)

# geometric mean - four equivalent ways to get the same result
prod(x) ^ (1 / length(x))
exp(mean(log(x)))
1.2 ^ mean(log(x, base = 1.2))
gmean(x)

# geometric variance
gvar(x)

# geometric sd
exp(sd(log(x)))                                     ## This is wrong (incorrect sample size)
exp(sqrt((length(x) - 1) / length(x)) * sd(log(x))) ## Correct calculation
gsd(x)

# Missing data will result in and NA being returned
x[c(2, 4, 7)] <- NA
gmean(x)
gmean(x, na_rm = TRUE)
gvar(x, na_rm = TRUE)
gsd(x, na_rm = TRUE)

# }

Run the code above in your browser using DataLab