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 6.065044e+00
2 Relative range (n-1 denominator) 5.665120e+00
3 Relative range (biased) 5.684099e+00
4 Variance (unbiased) 1.146172e+00
5 Variance (biased) 1.138530e+00
6 Standard Deviation (n-1 denominator) 1.070594e+00
7 Standard Deviation (biased) 1.067019e+00
8 Coefficient of Variation (n-1 denominator) 1.097662e+01
9 Coefficient of Variation (biased) 1.093997e+01
10 Mean Squared Error (MSE versus 0) 1.148043e+00
11 Mean Squared Error (MSE versus Mean) 1.138530e+00
12 Mean Absolute Deviation from Mean (MAD Mean) 8.629014e-01
13 Mean Absolute Deviation from Median (MAD Median) 8.606067e-01
14 Median Absolute Deviation from Mean 7.706503e-01
15 Median Absolute Deviation from Median 7.319228e-01
16 Mean Squared Deviation from Mean 1.138530e+00
17 Mean Squared Deviation from Median 1.145465e+00
18 Interquartile Difference (Weighted Average at Xnp) 1.589987e+00
19 (Weighted Average at X(n+1)p) 1.606575e+00
20 (Empirical Distribution Function) 1.584776e+00
21 (Empirical Distribution Function - Averaging) 1.584776e+00
22 (Empirical Distribution Function - Interpolation) 1.573038e+00
23 (Closest Observation) 1.584776e+00
24 (True Basic - Statistics Graphics Toolkit) 1.606575e+00
25 (MS Excel (old versions)) 1.584776e+00
26 Semi Interquartile Difference (Weighted Average at Xnp) 7.949936e-01
27 (Weighted Average at X(n+1)p) 8.032877e-01
28 (Empirical Distribution Function) 7.923878e-01
29 (Empirical Distribution Function - Averaging) 7.923878e-01
30 (Empirical Distribution Function - Interpolation) 7.865189e-01
31 (Closest Observation) 7.923878e-01
32 (True Basic - Statistics Graphics Toolkit) 8.032877e-01
33 (MS Excel (old versions)) 7.923878e-01
34 Coefficient of Quartile Variation (Weighted Average at Xnp) 4.683934e+00
35 (Weighted Average at X(n+1)p) 4.446692e+00
36 (Empirical Distribution Function) 4.596883e+00
37 (Empirical Distribution Function - Averaging) 4.596883e+00
38 (Empirical Distribution Function - Interpolation) 4.413111e+00
39 (Closest Observation) 4.596883e+00
40 (True Basic - Statistics Graphics Toolkit) 4.446692e+00
41 (MS Excel (old versions)) 4.596883e+00
42 Number of all Pairs of Observations 1.117500e+04
43 Squared Differences between all Pairs of Observations 2.292343e+00
44 Mean Absolute Differences between all Pairs of Observations 1.209083e+00
45 Gini Mean Difference 1.209083e+00
46 Leik Measure of Dispersion 5.104762e-01
47 Index of Diversity 8.668444e-01
48 Index of Qualitative Variation 9.906794e-01
49 Coefficient of Dispersion 6.034353e+01
50 Observations 1.500000e+02