x <- rnorm(150)
q1 <- function(data,n,p,i,f) {
np <- n*p;
i <<- floor(np)
f <<- np - i
qvalue <- (1-f)*data[i] + f*data[i+1]
}
q2 <- function(data,n,p,i,f) {
np <- (n+1)*p
i <<- floor(np)
f <<- np - i
qvalue <- (1-f)*data[i] + f*data[i+1]
}
q3 <- function(data,n,p,i,f) {
np <- n*p
i <<- floor(np)
f <<- np - i
if (f==0) {
qvalue <- data[i]
} else {
qvalue <- data[i+1]
}
}
q4 <- function(data,n,p,i,f) {
np <- n*p
i <<- floor(np)
f <<- np - i
if (f==0) {
qvalue <- (data[i]+data[i+1])/2
} else {
qvalue <- data[i+1]
}
}
q5 <- function(data,n,p,i,f) {
np <- (n-1)*p
i <<- floor(np)
f <<- np - i
if (f==0) {
qvalue <- data[i+1]
} else {
qvalue <- data[i+1] + f*(data[i+2]-data[i+1])
}
}
q6 <- function(data,n,p,i,f) {
np <- n*p+0.5
i <<- floor(np)
f <<- np - i
qvalue <- data[i]
}
q7 <- function(data,n,p,i,f) {
np <- (n+1)*p
i <<- floor(np)
f <<- np - i
if (f==0) {
qvalue <- data[i]
} else {
qvalue <- (1-f)*data[i] + f*data[i+1]
}
}
q8 <- function(data,n,p,i,f) {
np <- (n+1)*p
i <<- floor(np)
f <<- np - i
if (f==0) {
qvalue <- data[i]
} else {
if (f == 0.5) {
qvalue <- (data[i]+data[i+1])/2
} else {
if (f < 0.5) {
qvalue <- data[i]
} else {
qvalue <- data[i+1]
}
}
}
}
iqd <- function(x,def) {
x <-sort(x[!is.na(x)])
n<-length(x)
if (def==1) {
qvalue1 <- q1(x,n,0.25,i,f)
qvalue3 <- q1(x,n,0.75,i,f)
}
if (def==2) {
qvalue1 <- q2(x,n,0.25,i,f)
qvalue3 <- q2(x,n,0.75,i,f)
}
if (def==3) {
qvalue1 <- q3(x,n,0.25,i,f)
qvalue3 <- q3(x,n,0.75,i,f)
}
if (def==4) {
qvalue1 <- q4(x,n,0.25,i,f)
qvalue3 <- q4(x,n,0.75,i,f)
}
if (def==5) {
qvalue1 <- q5(x,n,0.25,i,f)
qvalue3 <- q5(x,n,0.75,i,f)
}
if (def==6) {
qvalue1 <- q6(x,n,0.25,i,f)
qvalue3 <- q6(x,n,0.75,i,f)
}
if (def==7) {
qvalue1 <- q7(x,n,0.25,i,f)
qvalue3 <- q7(x,n,0.75,i,f)
}
if (def==8) {
qvalue1 <- q8(x,n,0.25,i,f)
qvalue3 <- q8(x,n,0.75,i,f)
}
iqdiff <- qvalue3 - qvalue1
return(c(iqdiff,iqdiff/2,iqdiff/(qvalue3 + qvalue1)))
}
num <- 50
res <- array(NA,dim=c(num,2))
range <- max(x) - min(x)
lx <- length(x)
biasf <- (lx-1)/lx
varx <- var(x)
bvarx <- varx*biasf
sdx <- sqrt(varx)
mx <- mean(x)
bsdx <- sqrt(bvarx)
x2 <- x*x
mse0 <- sum(x2)/lx
xmm <- x-mx
xmm2 <- xmm*xmm
msem <- sum(xmm2)/lx
axmm <- abs(x - mx)
medx <- median(x)
axmmed <- abs(x - medx)
xmmed <- x - medx
xmmed2 <- xmmed*xmmed
msemed <- sum(xmmed2)/lx
qarr <- array(NA,dim=c(8,3))
for (j in 1:8) {
qarr[j,] <- iqd(x,j)
}
sdpo <- 0
adpo <- 0
for (i in 1:(lx-1)) {
for (j in (i+1):lx) {
ldi <- x[i]-x[j]
aldi <- abs(ldi)
sdpo = sdpo + ldi * ldi
adpo = adpo + aldi
}
}
denom <- (lx*(lx-1)/2)
sdpo = sdpo / denom
adpo = adpo / denom
gmd <- 0
for (i in 1:lx) {
for (j in 1:lx) {
ldi <- abs(x[i]-x[j])
gmd = gmd + ldi
}
}
gmd <- gmd / (lx*(lx-1))
cat_counts <- c(18, 24, 15, 27, 21, 19, 12, 14)
kcat <- length(cat_counts)
sumx <- sum(cat_counts)
pk <- cat_counts / sumx
ck <- cumsum(pk)
dk <- array(NA,dim=kcat)
for (i in 1:kcat) {
if (ck[i] <= 0.5) dk[i] <- ck[i] else dk[i] <- 1 - ck[i]
}
bigd <- sum(dk) * 2 / (kcat-1)
iod <- 1 - sum(pk*pk)
df = data.frame(Statistic = c("Absolute range ",
"Relative range (n-1 denominator) ",
"Relative range (biased) ",
"Variance (unbiased) ",
"Variance (biased) ",
"Standard Deviation (n-1 denominator) ",
"Standard Deviation (biased) ",
"Coefficient of Variation (n-1 denominator) ",
"Coefficient of Variation (biased) ",
"Mean Squared Error (MSE versus 0) ",
"Mean Squared Error (MSE versus Mean) ",
"Mean Absolute Deviation from Mean (MAD Mean) ",
"Mean Absolute Deviation from Median (MAD Median) ",
"Median Absolute Deviation from Mean ",
"Median Absolute Deviation from Median ",
"Mean Squared Deviation from Mean ",
"Mean Squared Deviation from Median ",
"Interquartile Difference (Weighted Average at Xnp) ",
" (Weighted Average at X(n+1)p) ",
" (Empirical Distribution Function) ",
" (Empirical Distribution Function - Averaging) ",
" (Empirical Distribution Function - Interpolation) ",
" (Closest Observation) ",
" (True Basic - Statistics Graphics Toolkit) ",
" (MS Excel (old versions)) ",
"Semi Interquartile Difference (Weighted Average at Xnp) ",
" (Weighted Average at X(n+1)p) ",
" (Empirical Distribution Function) ",
" (Empirical Distribution Function - Averaging) ",
" (Empirical Distribution Function - Interpolation) ",
" (Closest Observation) ",
" (True Basic - Statistics Graphics Toolkit) ",
" (MS Excel (old versions)) ",
"Coefficient of Quartile Variation (Weighted Average at Xnp)",
" (Weighted Average at X(n+1)p)",
" (Empirical Distribution Function)",
" (Empirical Distribution Function - Averaging)",
" (Empirical Distribution Function - Interpolation)",
" (Closest Observation)",
" (True Basic - Statistics Graphics Toolkit)",
" (MS Excel (old versions))",
"Number of all Pairs of Observations ",
"Squared Differences between all Pairs of Observations ",
"Mean Absolute Differences between all Pairs of Observations",
"Gini Mean Difference ",
"Leik Measure of Dispersion ",
"Index of Diversity ",
"Index of Qualitative Variation ",
"Coefficient of Dispersion ",
"Observations "),
Value = c(range,
range/sd(x),
range/sqrt(varx*biasf),
varx,
bvarx,
sdx,
bsdx,
sdx/mx,
bsdx/mx,
mse0,
msem,
sum(axmm)/lx,
sum(axmmed)/lx,
median(axmm),
median(axmmed),
msem,
msemed,
qarr[1,1],
qarr[2,1],
qarr[3,1],
qarr[4,1],
qarr[5,1],
qarr[6,1],
qarr[7,1],
qarr[8,1],
qarr[1,2],
qarr[2,2],
qarr[3,2],
qarr[4,2],
qarr[5,2],
qarr[6,2],
qarr[7,2],
qarr[8,2],
qarr[1,3],
qarr[2,3],
qarr[3,3],
qarr[4,3],
qarr[5,3],
qarr[6,3],
qarr[7,3],
qarr[8,3],
lx*(lx-1)/2,
sdpo,
adpo,
gmd,
bigd,
iod,
iod*kcat/(kcat-1),
sum(axmmed)/lx/medx,
lx))
print(df) Statistic Value
1 Absolute range 5.8745635
2 Relative range (n-1 denominator) 6.0916876
3 Relative range (biased) 6.1120954
4 Variance (unbiased) 0.9299850
5 Variance (biased) 0.9237851
6 Standard Deviation (n-1 denominator) 0.9643573
7 Standard Deviation (biased) 0.9611374
8 Coefficient of Variation (n-1 denominator) -12.5465629
9 Coefficient of Variation (biased) -12.5046711
10 Mean Squared Error (MSE versus 0) 0.9296929
11 Mean Squared Error (MSE versus Mean) 0.9237851
12 Mean Absolute Deviation from Mean (MAD Mean) 0.7324641
13 Mean Absolute Deviation from Median (MAD Median) 0.7324526
14 Median Absolute Deviation from Mean 0.5886378
15 Median Absolute Deviation from Median 0.5800523
16 Mean Squared Deviation from Mean 0.9237851
17 Mean Squared Deviation from Median 0.9238588
18 Interquartile Difference (Weighted Average at Xnp) 1.1412143
19 (Weighted Average at X(n+1)p) 1.1479336
20 (Empirical Distribution Function) 1.1435906
21 (Empirical Distribution Function - Averaging) 1.1435906
22 (Empirical Distribution Function - Interpolation) 1.1377373
23 (Closest Observation) 1.1435906
24 (True Basic - Statistics Graphics Toolkit) 1.1479336
25 (MS Excel (old versions)) 1.1435906
26 Semi Interquartile Difference (Weighted Average at Xnp) 0.5706071
27 (Weighted Average at X(n+1)p) 0.5739668
28 (Empirical Distribution Function) 0.5717953
29 (Empirical Distribution Function - Averaging) 0.5717953
30 (Empirical Distribution Function - Interpolation) 0.5688686
31 (Closest Observation) 0.5717953
32 (True Basic - Statistics Graphics Toolkit) 0.5739668
33 (MS Excel (old versions)) 0.5717953
34 Coefficient of Quartile Variation (Weighted Average at Xnp) -10.6117404
35 (Weighted Average at X(n+1)p) -11.5405659
36 (Empirical Distribution Function) -11.1615030
37 (Empirical Distribution Function - Averaging) -11.1615030
38 (Empirical Distribution Function - Interpolation) -11.3393434
39 (Closest Observation) -11.1615030
40 (True Basic - Statistics Graphics Toolkit) -11.5405659
41 (MS Excel (old versions)) -11.1615030
42 Number of all Pairs of Observations 11175.0000000
43 Squared Differences between all Pairs of Observations 1.8599701
44 Mean Absolute Differences between all Pairs of Observations 1.0720356
45 Gini Mean Difference 1.0720356
46 Leik Measure of Dispersion 0.5104762
47 Index of Diversity 0.8668444
48 Index of Qualitative Variation 0.9906794
49 Coefficient of Dispersion -10.7276914
50 Observations 150.0000000