How to calculate cumulative distribution in R?

  • I need to calculate the cumulative distribution function of a data sample.

    Is there something similar to hist() in R that measure the cumulative density function?

    I have tries ecdf() but i can't understand the logic.

  • The ecdf function applied to a data sample returns a function representing the empirical cumulative distribution function. For example:

    > X = rnorm(100) # X is a sample of 100 normally distributed random variables
    > P = ecdf(X)    # P is a function giving the empirical CDF of X
    > P(0.0)         # This returns the empirical CDF at zero (should be close to 0.5)
    [1] 0.52
    > plot(P)        # Draws a plot of the empirical CDF (see below)
    

    enter image description here

    If you want to have an object representing the empirical CDF evaluated at specific values (rather than as a function object) then you can do

    > z = seq(-3, 3, by=0.01) # The values at which we want to evaluate the empirical CDF
    > p = P(z)                # p now stores the empirical CDF evaluated at the values in z
    

    Note that p contains at most the same amount of information as P (and possibly it contains less) which in turn contains the same amount of information as X.

    Yes i know, but how is it possible to access the values of ecdf? this is a mystery for me.

    If you want its value at `x` you simply write `P(x)`. Note that `x` can be a vector (see the last couple of sentences of my answer.)

    @ChrisTaylor The correct terminology is empirical cumulative distribution function not density function.

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM