[R] Constrain density to 0 at 0?

David Winsemius dwinsemius at comcast.net
Tue Jul 20 21:18:18 CEST 2010


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:dwinsemius at comcast.net]
> 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
> 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



More information about the R-help mailing list