Re: [R] Constrain density to 0 at 0?

2010-07-24 Thread Greg Snow
Look at the logspline package.  This is a different approach to density 
estimation from the kernel densities used by 'density', but does allow you to 
set fixed boundaries.

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.s...@imail.org
801.408.8111


 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-
 project.org] On Behalf Of Farley, Robert
 Sent: Monday, July 19, 2010 7:57 PM
 To: r-help@r-project.org
 Subject: [R] Constrain density to 0 at 0?
 
 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?
 
 
 
 Thanks
 
 
 
 Robert Farley
 Metro
 1 Gateway Plaza
 Mail Stop 99-23-7
 Los Angeles, CA 90012-2952
 Voice: (213)922-2532
 Fax:(213)922-2868
 www.Metro.net
 
 
 
   [[alternative HTML version deleted]]
 
 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-
 guide.html
 and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Constrain density to 0 at 0?

2010-07-21 Thread Ravi Varadhan
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: rvarad...@jhmi.edu


- Original Message -
From: Farley, Robert farl...@metro.net
Date: Tuesday, July 20, 2010 2:47 pm
Subject: Re: [R] Constrain density to 0 at 0?
To: r-help@r-project.org r-help@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@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@r-project.org mailing list
  
  PLEASE do read the posting guide 
  and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Constrain density to 0 at 0?

2010-07-20 Thread Farley, Robert
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 [mailto:dwinsem...@comcast.net] 
Sent: Monday, July 19, 2010 19:31
To: Farley, Robert
Cc: r-help@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@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Constrain density to 0 at 0?

2010-07-20 Thread David Winsemius


On Jul 20, 2010, at 2:45 PM, Farley, Robert wrote:


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?


The best answer may depend on what your (unstated) goals are. Have you  
considered fitting a log-Normal or Gamma distribution to the data? You  
could plot the histogram and then overlay a smooth fitted distribution  
curve.


?hist
require(MASS)
?fitdistr

--
David.


-Original Message-
From: David Winsemius [mailto:dwinsem...@comcast.net]
Sent: Monday, July 19, 2010 19:31
To: Farley, Robert
Cc: r-help@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@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


David Winsemius, MD
West Hartford, CT

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Constrain density to 0 at 0?

2010-07-19 Thread Farley, Robert
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?



Thanks



Robert Farley
Metro
1 Gateway Plaza
Mail Stop 99-23-7
Los Angeles, CA 90012-2952
Voice: (213)922-2532
Fax:(213)922-2868
www.Metro.net



[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Constrain density to 0 at 0?

2010-07-19 Thread David Winsemius


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@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.