[R] Constrain density to 0 at 0?

Ravi Varadhan rvaradhan at jhmi.edu
Wed Jul 21 18:33:12 CEST 2010


This is the "boundary" problem in density estimation.  One simple trick is to use the idea of "reflection".  If your data is (y1, y2, ...,yn), you create a reflected data by appending `n' negative values to your data, call this y*.  Estimate the kernel density for this as fhat(y*).  Redefine this estimate so that it is zero for all y < 0, and is equal to 2 * fhat(y*).

Here is an example:

y <- rexp(100, rate=10)

yref <- c(-y, y)

fref <- density(yref)

sel <- fref$x >= 0

fref$y[sel] <- 2 * fref$y[sel]

par(mfrow=c(2,1))

plot(density(y))

plot(fref$x[sel], fref$y[sel], type="l")

This approach assumes that the density has 0 gradient at the boundary (gradient defined from the right hand side).

There are other (better) approaches as well.  Look at a book on density estimation.

Ravi.  
____________________________________________________________________

Ravi Varadhan, Ph.D.
Assistant Professor,
Division of Geriatric Medicine and Gerontology
School of Medicine
Johns Hopkins University

Ph. (410) 502-2619
email: rvaradhan at jhmi.edu


----- Original Message -----
From: "Farley, Robert" <FarleyR at metro.net>
Date: Tuesday, July 20, 2010 2:47 pm
Subject: Re: [R] Constrain density to 0 at 0?
To: "r-help at r-project.org" <r-help at r-project.org>


> "Read the help page for density more carefully. Especially the bw and  
> 
>  from arguments."
>  
>  
>  Yes,  it was my inability to make sense of the help page that 
> motivated my email.
>  
>  My distances range from 0.4 to 7.6 but the density plot ranges from 
> -2 to 10.  
>  
>  "from=0" seems to hide the negative portion of the density without 
> setting it to 0.
>  
>  
>  I've tried adjust, but it requires a value of 0.2, which results in a 
> very, very spiky plot.
>  
>  
>  I was hoping for a process that could "forbid" impossible (negative) 
> values, yet allow the density plot to blend the individual 
> measurements. Is there a "variable bw" that could be set small at the 
> extrema, and larger in the range of the data?
>  
>  
>  
>  
>   
>  Robert Farley
>  Metro
>  www.Metro.net 
>   
>  -----Original Message-----
>  From: David Winsemius [ 
>  Sent: Monday, July 19, 2010 19:31
>  To: Farley, Robert
>  Cc: r-help at r-project.org
>  Subject: Re: [R] Constrain density to 0 at 0?
>  
>  
>  On Jul 19, 2010, at 9:57 PM, Farley, Robert wrote:
>  
>  > I'm plotting some trip length frequencies using the following code:
>  >
>  > plot( density(zTestData$Distance, weights=zTestData$Actual),
>  >    xlim=c(0,10),
>  >    main="Test TLFD",
>  >    xlab="Distance",
>  >   col=6  )
>  > lines(density(zTestData$Distance, weights=zTestData$FlatWeight),  
>  > col=2)
>  > lines(density(zTestData$Distance, weights=zTestData$BrdWeight ),  
>  > col=3)
>  >
>  > which works fine except the distances are all positive, but the  
>  > densities don't drop to 0 until around -2 or -3.
>  >
>  > Is there a way for me to "force" the density plot to 0 at 0?
>  
>  Yes. (Assuming it can be zero, given the data.)
>  
>  Read the help page for density more carefully. Especially the bw and  
> 
>  from arguments.
>  
>  -- 
>  David.
>  
>  ______________________________________________
>  R-help at r-project.org mailing list
>  
>  PLEASE do read the posting guide 
>  and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list