Cumulative distribution function in R

The probability that x will take a value less than or equal to x is the cumulative distribution function (CDF) of a random variable assessed at x.

The ecdf() function in R is used to calculate the cumulative distribution function. The ecdf() function in R is used to compute and plot the value of a numeric vector’s Empirical Cumulative Distribution Function.

The CDF data is returned by the ecdf() function, which takes the data vector as an input.

To calculate and plot a cumulative distribution function (CDF) in R, use the following basic syntax.

compute the data’s empirical CDF

p <-ecdf(data)

Simply we can create a CDF plot


The examples below demonstrate how to utilize this syntax in practice.

Example 1: Calculate and plot the CDF of raw data

In R, you can calculate and plot a CDF of a random dataset using the following code:

Let’s create a some information


Now we can calculate the empirical CDF of the data


In R, plot the cumulative distribution function.

In order to plot a CDF function in R, we must first compute the CDF using the ecdf() function. The CDF plot is then plotted in the R language using the plot() function.

The result of the ecdf() method is passed to the plot function, which plots the CDF plot.

Let’s plot a CDF

plot(p, xlab='x', ylab='CDF', main='CDF Plot')

The x-axis shows the raw data values and the y-axis shows the corresponding CDF values.

Example 2: Calculate and plot a known distribution’s CDF

The following code demonstrates how to compute and plot a conventional normal distribution CDF.

3D Plot in R Programming-Quick Guide »

curve(pnorm, from = -4, to = 4)

Alternatively, you can use ggplot2 to make the same plot:

ggplot(data.frame(x = c(-4, 4)), aes(x = x)) +
  stat_function(fun = pnorm)

You may also like...

Leave a Reply

Your email address will not be published.


Subscribe Now