I derived a density estimation(not using any R packages) from the observed data. Now I want to measure if my estimation did a good job. so I am wondering if there are any function in R can help me or I have to write my own code
Histogram and KDE. One method is to overlay the density estimator on a histogram
of the sample in order to judge whether the fit is satisfactory. The match will be better for larger sample sizes.
(I use the default kernel density estimator
density in R.)
set.seed(2020) par(mfrow = c(1,2)) x = rgamma(100, 5, 1/2) hist(x, prob=T, col="skyblue2"); rug(x) lines(density(x), type="l", col="red") y = rgamma(5000, 5, 1/2) hist(y, prob=T, col="skyblue2") lines(density(y), type="l", col="red") par(mfrow = c(1,1))
Kolmogorov-Smirnov test: ECDF vs CDF. If you know the CDF of the population, then you can use a Kolmogorov-Smirnov test to judge how well the sample ECDF matches the known population CDF. The test criterion is based on the maximum vertical discrepancy between the ECDF stairstep and the CDF curve.
ks.test(x, pgamma, 5, 1/2) One-sample Kolmogorov-Smirnov test data: x D = 0.051657, p-value = 0.9523 alternative hypothesis: two-sided plot(ecdf(x), col="blue") curve(pgamma(x,5, 1/2), add=T, col="red", lwd=2)