The mean and other location measures

TODO

  • link to npWilcoxon for wilcox.test()

Install required packages

DescTools, modeest, robustbase

wants <- c("DescTools", "modeest", "robustbase")
has   <- wants %in% rownames(installed.packages())
if(any(!has)) install.packages(wants[!has])

Mean, weighted mean, geometric mean, harmonic mean, and mode

Mean

age <- c(17, 30, 30, 25, 23, 21)
mean(age)
[1] 24.33333

Weighted mean

weights <- c(0.6, 0.6, 0.3, 0.2, 0.4, 0.6)
weighted.mean(age, weights)
[1] 23.7037

Geometric mean

library(DescTools)
Gmean(age)
[1] 23.86509

Harmonic mean

library(DescTools)
Hmean(age)
[1] 23.38384

Mode

vec <- c(11, 22, 22, 33, 33, 33, 33)
library(modeest)
mfv(vec)
[1] 33
mlv(vec, method="mfv")
Mode (most likely value): 33 
Bickel's modal skewness: -0.4285714 
Call: mlv.default(x = vec, method = "mfv") 

Robust location measures

Median

median(age)
[1] 24

Trimmed mean

mean(age, trim=0.2)
[1] 24.75

Winsorized mean

library(DescTools)
(ageWins <- Winsorize(age, probs=c(0.2, 0.8)))
[1] 21 30 30 25 23 21
mean(ageWins)
[1] 25

Huber-\(M\) estimator

library(robustbase)
hM <- huberM(age)
hM$mu
[1] 24.33333

Hodges-Lehmann estimator (pseudo-median)

library(DescTools)
HodgesLehmann(age, conf.level=0.95)
     est   lwr.ci   upr.ci 
23.99998 20.99997 27.50005 

Hodges-Lehmann estimator of difference between two location parameters

N <- 8
X <- rnorm(N, 100, 15)
Y <- rnorm(N, 110, 15)
wilcox.test(X, Y, conf.int=TRUE)$estimate
difference in location 
             -13.78561 

Detach (automatically) loaded packages (if possible)

try(detach(package:modeest))
try(detach(package:DescTools))
try(detach(package:robustbase))

Get the article source from GitHub

R markdown - markdown - R code - all posts