Suppose that I have a variable like X
with unknown distribution. In Mathematica, by using SmoothKernelDensity
function we can have an estimated density function.This estimated density function can be used alongside with PDF
function to calculate probability density function of a value like X
in the form of PDF[density,X]
assuming that "density" is the result of SmoothKernelDensity
. It would be good if there is such feature in R.This is how it works in Mathematica
http://reference.wolfram.com/mathematica/ref/SmoothKernelDistribution.html
As an example (based on Mathematica functions):
data = RandomVariate[NormalDistribution[], 100]; #generates 100 values from N(0,1)
density= SmoothKernelDistribution[data]; #estimated density
PDF[density, 2.345] returns 0.0588784
Here you can find more information about PDF:
http://reference.wolfram.com/mathematica/ref/PDF.html
I know that I can plot its density function using density(X)
in R and by using ecdf(X)
I can obtain its empirical cumulative distribution function.Is it possible to do the same thing in R based on what I described about Mathematica?
Any help and idea is appreciated.
density(x)
gives an estimate of the pdf, as you already noted, but its suitability depends on the purpose for which you want to have the density. Note, for example, that the variance is biased up (in performing convolution, you add the variance of the kernel to the variance of the data, itself an unbiased estimate) - such bias-variance tradeoffs are ubiquitous. There are other alternatives, such as log-spline density estimation, for example -- but again, its suitability partly depends on what you want to do with it. $\endgroup$ – Glen_b -Reinstate Monica Dec 5 '13 at 23:08ecdf(X)
gives me the equivalent percentile of 7.5 but it's not what I'm looking for. $\endgroup$ – Amin Dec 5 '13 at 23:59ecdf(b)-ecdf(a)
would estimate $P(\text{a}<X\leq \text{b})$. Of course with continuous variables the distinction between $<$ and $\leq$ is unimportant. If $X$ is discrete, then you can estimate $P(X=7.5)$ by computing the sample proportion of values that are 0.75. $\endgroup$ – Glen_b -Reinstate Monica Dec 6 '13 at 7:03