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
2 Answers
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)
-
$\begingroup$ Thanks for the quick answer! Unfortunately I don't know the actual pdf so I have to go with the first method. is there any numeric criterion to measure difference between the density and histogram rather than just look at the figure. $\endgroup$ Commented Apr 19, 2020 at 4:15
A Kolmogorov-Smirnov Goodness of Fit test (ks.test()
in R) will let you examine the largest difference between your estimated and actual CDF. That is definitely the simplest way to test the differences (in addition to looking at it graphically).
-
$\begingroup$ thanks for the quick answer. however I don't know the actual pdf so I can't use this method $\endgroup$ Commented Apr 19, 2020 at 4:16