I've estimated a Gaussian kernel density of a univariate variable with density(), but after I would like to find out the CDF value and graph.

How can I do it?


To estimate the CDF you can use the empirical distribution function. For this you don't even need a bandwidth.

data(geyser, package = "MASS")
duration <- geyser$duration
duration_grid <- seq(min(x), max(x), length.out = 100)
cdf_grid <- sapply(duration_grid, function(x) mean(duration <= x))
plot(duration_grid, cdf_grid, type = "l")

If you are sure that the probability function that you are estimating is smooth you might want to see this reflected in your estimate. The package kerdiest implements such smoothing methods with optimal bandwidth choices.

h_AL <- kerdiest::ALbw(vec_data=duration)
F_AL <- kerdiest::kde(vec_data=duration, bw=h_AL)
plot(F_AL$grid,F_AL$Estimated_values,type = "l")

I do not recommend that you try to derive an estimate of the distribution function from your density estimate.


Kennel density estimator is

$$ g(x) = \sum_{i=1}^n w_i \; K_h(x - x_i) $$

where $w_i$ are mixing weights (usually uniform for kernel densities) and $K_h$ is a kernel parameterized by bandwidth $h$. Since kernel is a probability density function, this is just a mixture of distributions $f = K_h$. Notice that cumulative distribution function of a mixture is

$$ G(x) = \sum_{i=1}^n w_i \; F(x - x_i) $$

where $F$ is the cumulative distribution function of the kernel. So if you estimated the kernel density with Gaussian kernels, just replace the Gaussian probability density functions with normal cumulative distribution functions and you have your cdf.


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