[Rd] Negatives density values (PR#8876)

Duncan Murdoch murdoch at stats.uwo.ca
Sat May 20 17:03:28 CEST 2006


On 5/18/2006 10:51 AM, ecu at info.fundp.ac.be wrote:
> Full_Name: Cuvelier Etienne
> Version: R version 2.2.1
> OS: Windows XP
> Submission from: (NULL) (81.240.71.204)
> 
> 
> If we use the "density" function "far" from the data, some values are
> negatives.
> I know that these value of the density are "near equal" to zero, but the change
> of sign can involve some "false" decision.

I wouldn't consider this a bug, rather a consequence of using the 
Fourier transform to do the density estimate, as documented.

If slightly negative values of the density cause trouble for you, then 
use pmax(density, 0) to force them to be non-negative, or use a 
different algorithm for the estimates.

Duncan Murdoch
> 
> Here is an example of this fact:
> 
> 
>> x = c(0.006, 0.002, 0.024, 0.02, 0.034, 0.09, 0.074, 0.072, 0.122,
> + 0.048, 0.044, 0.168)
>> min(x)
> [1] 0.002
>> max(x)
> [1] 0.168
>> result = density(x,n=20, from = -1 , to = 1)
>>
>> result$y
>  [1]  2.433854e-17  1.726562e-17  1.285227e-16  2.359248e-16
>  [5]  4.628709e-17 -1.535879e-16  1.438105e-16  2.023210e-16
>  [9]  2.649354e-11  1.438340e-01  7.679909e+00  1.767860e+00
> [13]  2.056151e-04 -1.407219e-16 -3.575895e-18 -1.979607e-16
> [17] -1.574544e-17 -2.257917e-17 -2.305234e-16  4.052727e-18
>> #A plot of the "fluctuations" near zero
>> plot(result$x,result$y,type="l",xlim=c(-1,1),ylim=c(-10^-14,10^-14))
>> abline(h=0)
>>
>> #All neagtives value of density are "near equal" to zero 
>> all(apply(as.matrix(result$y[result$y<0]),1,all.equal, target=0)==TRUE)
> [1] TRUE
> 
> 
>>  sessionInfo()
> R version 2.2.1, 2005-12-20, i386-pc-mingw32
> 
> attached base packages:
> [1] "methods"   "stats"     "graphics"  "grDevices" "utils"
> [6] "datasets"  "base"
> 
>> Sys.info()
>                       sysname                       release 
>                     "Windows"                      "NT 5.1" 
>                       version                      nodename 
> "(build 2600) Service Pack 2"                       "WINXP" 
>                       machine                         login 
>                         "x86"                     "Etienne" 
>                          user 
>                     "Etienne" 
> 
>> R.Version()
> $platform
> [1] "i386-pc-mingw32"
> 
> $arch
> [1] "i386"
> 
> $os
> [1] "mingw32"
> 
> $system
> [1] "i386, mingw32"
> 
> $status
> [1] ""
> 
> $major
> [1] "2"
> 
> $minor
> [1] "2.1"
> 
> $year
> [1] "2005"
> 
> $month
> [1] "12"
> 
> $day
> [1] "20"
> 
> $"svn rev"
> [1] "36812"
> 
> $language
> [1] "R"
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list