[R] Question about quantile.default

Gavin Simpson gavin.simpson at ucl.ac.uk
Sat Oct 4 11:14:16 CEST 2008


On Fri, 2008-10-03 at 15:27 -0400, Lo, Ken wrote:
> Hi all,
> 
> I am running into a snag using quantile function in stats.  Basically, I
> don't understand why the loop below throws the error that it does.
> 
> test.data <- rnorm(1000, 0, 1)
> 
> for (i in seq(0.00001, 0.001, 0.00001)){
> 	test <- quantile(test.data, probs=seq(0,1,i));
> 	print(i);
> }
> 
> It runs fine from 1e-05 to 0.00024, but then throws the error
> 
> Error in quantile.default(test.data, probs=seq(0,1,i)):
> 'probs' outside [0,1]

Perhaps this helps:
Run your loop
> tmp <- seq(0, 1, i)
> tmp[length(tmp)]
[1] 1
> tmp[1] == 0
[1] TRUE
> tmp[length(tmp)] == 1
[1] FALSE
> tmp[length(tmp)] > 1
[1] TRUE

So in this sequence, the "1" isn't represented exactly as 1 in the
computer, it is a little bit bigger than 1, and hence the error.

The reason the below examples work is that when the loop stops, i is not
exactly equal to 0.00025

> i == 0.00025
[1] FALSE

And when using 0.00025 explicitly, the probs are all within 0,1.

These issues are covered in the R FAQ, 7.31:

http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f

If this is just a toy example for a proper application, you have to
generate the sequence outside of the call to quantile and insure that
any values outside 0,1 are set back to 0,1. For example, this loop now
completes the task you tried to achieve, modified a bit to do
pre-allocation of storage and save all sets of quantiles:

test.data <- rnorm(1000, 0, 1)

seqs <- seq(0.00001, 0.001, 0.00001)
test <- vector(mode = "list", length = length(seqs))
for (i in seq_along(seqs)){
	tmp <- seq(0, 1, seqs[i])
	tmp[tmp < 0] <- 0
	tmp[tmp > 1] <- 1
        test[[i]] <- quantile(test.data, probs=tmp)
}

G

Ps: This is R, not C. The ";" are not required

> 
> I tested it some more by using
> 
> test <- quantile(test.data, probs=seq(0,1,0.00024));
> test <- quantile(test.data, probs=seq(0,1,0.00025));
> 
> both ran fine.  So, I'm baffled as to what the error actually is.
> 
> My sessionInfo() is:
> 
> R version 2.7.1 (2008-06-23)
> i386-pc-mingw32
> 
> Locale:
> LC_COLLATE=English_United States.1252.......
> 
> Attached base packages:
> [1] stats graphics grDevice utils datasets methods base
> 
> 
> Any assistance would be much appreciated.
> 
> Best,
> 
> Ken Lo
> 
> ______________________________________________
> 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.
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20081004/57abbf40/attachment.bin>


More information about the R-help mailing list