[R] density(): obtaining p-values

Frank E Harrell Jr fharrell at virginia.edu
Mon Mar 3 12:53:03 CET 2003

On Mon, 3 Mar 2003 12:40:37 +0100
"Pfaff, Bernhard" <Bernhard.Pfaff at drkw.com> wrote:

> Dear R-List-Member,
> is there a more elegant way to obtain p-values of a vector x, whose
> empirical density has been estimated with density(), than summing up the
> rectangles as an approximation of the area beneath the empirical
> distribution function and interpolating the values of x by using approx()?
> pval.emp <- function(x)
>   {
>    df <- density(x,from=min(x),to=max(x),kernel="gaussian")
>    width <- df$x[2]-df$x[1]
>    rect <- df$y*width
>    cdf.emp <- cumsum(rect)
>    approx(df$x,cdf.emp,x)$y
>   }
> Many thks in advance,
> Bernhard

You may want to refer to this just as "probability" and not "p-values".  It may not be fruitful to fit a density if what you want is cumulative probabilities.   Just compute the empirical cumulative distribution function of the original x:


Frank E Harrell Jr              Prof. of Biostatistics & Statistics
Div. of Biostatistics & Epidem. Dept. of Health Evaluation Sciences
U. Virginia School of Medicine  http://hesweb1.med.virginia.edu/biostat

More information about the R-help mailing list