Jim Robinsons at.or.below() was precise in that it does not claimed
symmetry.
Song Qian's suggestion of substracting 0.5
beside missing () had a logical error:
-0.5 aims to symmetry, and going for symmetry requires
considering ties, which was not done and was not necessary
in at.or.below().
Bill Venables vectorized edf() is also asymetric, as can be seen in
> x
[1] 1 2 2 3
> edf(x,3:1)
3 2 1
1 0.75 0.25
wheras quantile() is defined symetric
> quantile(x)
0% 25% 50% 75% 100%
1 1.75 2 2.25 3
given the current definition of quantile() it *cannot* have an exact
inverse, since different p can be projected onto the same q as in
> quantile(x,seq(0,1,0.1))
0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100%
1 1.3 1.6 1.9 2 2 2 2.1 2.4 2.7 3
What can have an exact inverse, is e.g. q.rankit()
which is based on the symetric RankIt transform,
that is calculating mean rank for ties
and using -0.5, cf. also ppoints()
With q.rankit the quantiles are symetric and unique like:
> q.rankit(x, seq(0,1,0.1))
p=0 p=0.1 p=0.2 p=0.3 p=0.4 p=0.5 p=0.6 p=0.7 p=0.8 p=0.9
p=1
NA NA 1.2 1.466667 1.733333 2 2.266667 2.533333 2.8 NA
NA
where quantiles are not defined for extreme p. Percentiles are also
symetric
> p.rankit(x, 1:3)
q=1 q=2 q=3
0.125 0.5 0.875
Here are the inverses
> p.rankit(x, na.omit(q.rankit(x, seq(0,1,0.1))))
q=1.2 q=1.46667 q=1.73333 q=2 q=2.26667 q=2.53333 q=2.8
0.2 0.3 0.4 0.5 0.6 0.7 0.8
> q.rankit(x, p.rankit(x, 1:3))
p=0.125 p=0.5 p=0.875
1 2 3
Best regards
Jens Oehlschlaegel-Akiyoshi
at.or.below <- function(x, q)
{
.l <- length(x)
length((1:.l)[x <= q])/.l
}
edf <- function(x, q = seq(min(x), max(x), len = 5),
na.rm = F) {
if(any(c1 <- is.na(x))) {
if(na.rm) x <- x[!c1] else
stop("NAs in x not allowed with na.rm=F")
}
x <- sort(x)
c1 <- q < x[1]
c2 <- q > x[n <- length(x)]
ok <- !(c1 | c2)
y <- (1:n)/n
p <- numeric(length(q))
p[ok] <- approx(x, y, q[ok])$y
p[c1] <- 0
p[c2] <- 1
structure(p, names = format(q))
}
p.rankit <- function(x,q=q.rankit(x)){
# (c) JOA 1997
# corresponds to RankIt: (r-1/2)/W by Chambers et.al. 1983
x <- sort(x[!is.na(x)])
pr<- (rank(x)-0.5)/length(x)
p <- approx(x,pr,q)$y
names(p) <- paste("q=", q, sep="")
p
}
q.rankit <- function(x,p=c(0.25,0.5,0.75)){
# (c) JOA 1997
# corresponds to RankIt: (r-1/2)/W by Chambers et.al. 1983
x <- sort(x[!is.na(x)])
pr<- (rank(x)-0.5)/length(x)
q <- approx(pr,x,p)$y
names(q) <- paste("p=", p, sep="")
q
}
--
Jens Oehlschlaegel-Akiyoshi
Psychologist/Statistician
Project TR-EAT + COST Action B6
F.rankfurt
oehl@psyres-stuttgart.de A.ttention
+49 711 6781-408 (phone) I.nventory
+49 711 6876902 (fax) R .-----.
/ ----- \
Center for Psychotherapy Research | | 0 0 | |
Christian-Belser-Strasse 79a | | ? | |
D-70597 Stuttgart Germany \ ----- /
-------------------------------------------------- '-----' -
(general disclaimer) it's better