I have a data set of which contains the name of buyers. I want to plot a CDF that shows what percentage of buyers purchased up to what percentage of products. Something like bellow: enter image description here, but I don't know how to do it with categorical data in R? Can some one please give me a clue?


1 Answer 1


The definition of a CDF requires that a random variable take values in a totally ordered set. However, the defining property of a categorical random variable is that its values are non-orderable. Therefore, CDF's do not exist for categorical RV's.

That said, if your primary interest is to provide a graphical representation of your data, you might want to look to the probability mass function instead. In order to present the PMF, you'll want to organize your data so that the probabilities are arranged in decreasing order. This approach is commonly used in fields like text mining. The R script below illustrates how to do this:

K <- 10  # Total number of possible categories
pop <- letters[1:K]  # Categorical sample space
n <- 50  # Sample size
f <- 1:K / sum(1:K)  # True PMF
X <- sample(pop, n, replace = T, prob = f)  # Sample data
f_hat <- table(X) / n  # Estimated PMF
f_hat_ord <- sort(f_hat, decreasing = T)  # Ordered estimated PMF

enter image description hereIf you really wanted, you could generate something akin to a CDF by taking the cumulative sum of the ordered estimated PMF. However, I would imagine that you'd lose some of the visual appeal by doing that.

  • $\begingroup$ And what is the set is ordered? Say a random variable can take on $n$ discrete values on reals $\mathbb{R}$? How do we express CDF in this case? And what is the name for such distribution if not categorical? Thank you $\endgroup$
    – Confounded
    Commented Oct 14, 2021 at 12:00

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