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")
[1] 33

Robust location measures

Median

median(age)
[1] 24

Quantiles

Default: Quartiles

(quant <- quantile(age))
   0%   25%   50%   75%  100% 
17.00 21.50 24.00 28.75 30.00 
quant[c("25%", "50%")]
 25%  50% 
21.5 24.0 

Set probabilities with option probs

quantile(age, probs=c(1/3, 2/3))
33.33333% 66.66667% 
 22.33333  26.66667 

Trimmed mean

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

Winsorized mean

library(DescTools)
border   <- quantile(age, probs=c(0.2, 0.8))
(ageWins <- Winsorize(age, val=border))
[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 
    24     NA     NA 

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 
             -2.536386 

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