Examples for 'stats::ecdf'


Empirical Cumulative Distribution Function

Aliases: ecdf plot.ecdf print.ecdf summary.ecdf quantile.ecdf

Keywords: dplot hplot

### ** Examples

##-- Simple didactical  ecdf  example :
x <- rnorm(12)
Fn <- ecdf(x)
Fn     # a *function*
Empirical CDF 
Call: ecdf(x)
 x[1:12] = -2.5308, -2.335, -1.7375,  ..., 0.18591, 0.86469
Fn(x)  # returns the percentiles for x
 [1] 0.66666667 1.00000000 0.25000000 0.08333333 0.16666667 0.50000000
 [7] 0.91666667 0.41666667 0.58333333 0.33333333 0.75000000 0.83333333
tt <- seq(-2, 2, by = 0.1)
12 * Fn(tt) # Fn is a 'simple' function {with values k/12}
 [1]  2  2  2  3  3  3  3  3  4  4  4  4  5  5  6  6  7  7  8  8  8  8 11 11 11
[26] 11 11 11 11 12 12 12 12 12 12 12 12 12 12 12 12
summary(Fn)
Empirical CDF:	  12 unique values with summary
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
-2.5308 -1.3762 -0.5176 -0.7229  0.1197  0.8647 
##--> see below for graphics
knots(Fn)  # the unique data values {12 of them if there were no ties}
 [1] -2.5308337 -2.3349949 -1.7374782 -1.2558061 -0.8751188 -0.6080846
 [7] -0.4270782 -0.2132016  0.1105915  0.1470544  0.1859052  0.8646945
y <- round(rnorm(12), 1); y[3] <- y[1]
Fn12 <- ecdf(y)
Fn12
Empirical CDF 
Call: ecdf(y)
 x[1:10] =   -1.1,     -1,   -0.8,  ...,    0.6,    0.7
knots(Fn12) # unique values (always less than 12!)
 [1] -1.1 -1.0 -0.8 -0.3  0.0  0.3  0.4  0.5  0.6  0.7
summary(Fn12)
Empirical CDF:	  10 unique values with summary
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 -1.100  -0.675   0.150  -0.070   0.475   0.700 
summary.stepfun(Fn12)
Step function with continuity 'f'= 0 ,  10 knots with summary
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 -1.100  -0.675   0.150  -0.070   0.475   0.700 

and	11 plateau levels (y) with summary
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.0000  0.2083  0.5000  0.4924  0.7500  1.0000 
## Advanced: What's inside the function closure?
ls(environment(Fn12))
[1] "f"      "method" "na.rm"  "nobs"   "x"      "y"      "yleft"  "yright"
## "f"     "method" "na.rm"  "nobs"   "x"     "y"    "yleft"  "yright"
utils::ls.str(environment(Fn12))
f :  num 0
method :  int 2
na.rm :  logi TRUE
nobs :  int 12
x :  num [1:10] -1.1 -1 -0.8 -0.3 0 0.3 0.4 0.5 0.6 0.7
y :  num [1:10] 0.0833 0.1667 0.25 0.4167 0.5 ...
yleft :  num 0
yright :  num 1
stopifnot(all.equal(quantile(Fn12), quantile(y)))

###----------------- Plotting --------------------------
require(graphics)

op <- par(mfrow = c(3, 1), mgp = c(1.5, 0.8, 0), mar =  .1+c(3,3,2,1))

F10 <- ecdf(rnorm(10))
summary(F10)
Empirical CDF:	  10 unique values with summary
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
-1.64660 -0.41171 -0.06476  0.26119  0.89590  2.68543 
plot(F10)
plot(F10, verticals = TRUE, do.points = FALSE)

plot(Fn12 , lwd = 2) ; mtext("lwd = 2", adj = 1)
xx <- unique(sort(c(seq(-3, 2, length.out = 201), knots(Fn12))))
lines(xx, Fn12(xx), col = "blue")
abline(v = knots(Fn12), lty = 2, col = "gray70")
plot of chunk example-stats-ecdf-1
plot(xx, Fn12(xx), type = "o", cex = .1)  #- plot.default {ugly}
plot(Fn12, col.hor = "red", add =  TRUE)  #- plot method
abline(v = knots(Fn12), lty = 2, col = "gray70")
## luxury plot
plot(Fn12, verticals = TRUE, col.points = "blue",
     col.hor = "red", col.vert = "bisque")

##-- this works too (automatic call to  ecdf(.)):
plot.ecdf(rnorm(24))
title("via  simple  plot.ecdf(x)", adj = 1)
plot of chunk example-stats-ecdf-1
par(op)

[Package stats version 4.2.3 Index]