I made up a data set with n=314, mean =14.27854, standard deviation =2.16547 using p <-rnorm(314,14.27854, 2.16547). Now, I want to compare theoretical cumulative distribution with the empirical cumulative distribution by drawing both in the same graph in R. I thought that I can find the empirical cumulative distribution using ecdf(p), but I could not find a way for theoretical cumulative distribution. Moreover, I am a beginner in R, and I do not know how to show these two graphs in one graph.

Firstly, is ecdf(p) code correct?

Secondly, how can I find theoretical cumulative distribution?

Thirdly, how can I show them together in one graph in R?


2 Answers 2


In order of your questions:

  1. Yes, that is a correct way, but there may be a better way for your particular problem
  2. Assuming your mean and SD are the true population statistics, you can generate the proper values using pnorm.
  3. It depends on what you want. Do you want to show the two CDFs overlayed? They will probably be very close and overwrite each other as 314 observations should make for a decent Gaussian sample. Or do you want to plot one against the other like a qqplot?

Here is some code based on your questions which I believe should help:

# Mean
m <- 14.27854

# SD
s <- 2.16547

# Obs
n <- 314

# Set seed for repeatibility

# Generate observations
A <- rnorm(n, m, s)

# Manually create CDF table by sorting the empirical observations and using the
# convention that the points are plotted at the END (so first observation starts
# at 1 / 314, etc.)
empCDF <- data.frame(x = sort(A), p = seq_len(n) / n)

# True CDF applied to observations. empCDF$x is the sorted A's
trueCDF <- pnorm(empCDF$x, m, s)

# Overplot CDFs and against each other
par(mfrow = c(1L, 2L))
plot(empCDF$x, empCDF$p, type = 'l')
lines(empCDF$x, trueCDF, type = 'l', col = 'blue')
plot(trueCDF, empCDF$p, type = 'l')
par(mfrow = c(1L, 1L))

CDF and QQPlot

Running more simulations would provide a better fit. Here is the result of the exact same code using $n = 10,000$.

Second set of plots


To show how using 10,000 observations makes the results very close, I will redo the plots with two line types and thicker lines to show they are both there. I will also change the empirical to red for contrast. The blue will remain the true CDF.

# Overplot CDFs and against each other
# Split screen into two windows next to each other: 1 row and 2 columns
par(mfrow = c(1L, 2L))
# Plot the empirical first in red
plot(empCDF$x, empCDF$p, type = 'l', col = 'red')
# Add (lines adds to existing plot) the true value in thick dashed blue
lines(empCDF$x, trueCDF, type = 'l', col = 'blue', lwd = 3L, lty = 3L)
# Now in the second window, plot the empirical against the true
plot(trueCDF, empCDF$p, type = 'l')
# Going forward, make R use one window per plot as usual
par(mfrow = c(1L, 1L))

enter image description here

  • $\begingroup$ thank you , by the way how can i make different color the graphs $\endgroup$ Dec 21, 2021 at 18:37
  • $\begingroup$ Which graph? For simplicity, I used base R, and you can pass colors using the col option. see the line col = 'blue' in the CDF graph? In the second, the empirical and true are so close they just overwrite each other. $\endgroup$
    – Avraham
    Dec 21, 2021 at 18:39
  • $\begingroup$ "they just overwrite each other." , to show the overwrite , i wan to make these two line different color $\endgroup$ Dec 21, 2021 at 18:41
  • $\begingroup$ They ARE a different color :) I used the same code, but the blue overwrites the black. What I will do is use a different line TYPE and show this. Let me update the answer. $\endgroup$
    – Avraham
    Dec 21, 2021 at 18:41
  • 1
    $\begingroup$ thanks a lot +1 $\endgroup$ Dec 21, 2021 at 18:52

Firstly, is ecdf(p) code correct?

  1. Yes, your code is correct for plotting the empirical CDF for 314 randomly generated data points from a known distribution.

Secondly, how can I find theoretical cumulative distribution?

  1. You know the parameters for the distribution (normal with the mean and standard deviation that you supplied), you just need to plot them. You also know how to plot an empirical distribution already. The empirical distribution approaches the theoretical distribution as the sample size approaches infinity. You can get very close to the theoretical distribution within the ecdf function by using a very large sample size (say n = 20,000).

Thirdly, how can I show them together in one graph in R?

  1. The related answer here shows just that. Below, I tuned the answer to your question below. ecdf1 represents your empirical function, you can change ecdf2 to approximate the theoretical distribution by using a very large sample size.
    ecdf1 <- ecdf(rnorm(314,14.27854, 2.16547))
    ecdf2 <- ecdf(rnorm(20000,14.27854, 2.16547))
    plot(ecdf1, verticals=TRUE, do.points=FALSE)
    plot(ecdf2, verticals=TRUE, do.points=FALSE, add=TRUE, col='brown')


  • 1
    $\begingroup$ thanks for answer , you can give exact answer , actually exact answer is better , because i am here to learn form you like a book. By the way , why your graph and @avrahams graphs are different ? $\endgroup$ Dec 21, 2021 at 18:58
  • 1
    $\begingroup$ @halo no problem, I modified the answer to show that. The graphs are different mainly because of the width of the graph (my x axis appears wider). $\endgroup$
    – Underminer
    Dec 21, 2021 at 19:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.