I am trying to return the p value for a stat from the ECDF.  That is the
index of the first occurrence,
on an ordered vector, of a value either greater than or equal to a given
value.
Ideally I would not have to order the vector beforehand.
Currently I use:
	PValue<-function(stat, ECDF){
		###Get the length of the ECFD
     		L<-length(ECDF)
		###Loop through the ECDF until the p value is found
		for(i in 1:L){
			if(ECDF[i]>=stat){
				break
			}
		}
		###Return the 3 values that bracket the p value
		c((i-1)/L, i/L, (i+1)/L)
	}
Is there a way of doing this that avoids the explicit loop?
platform i386-pc-mingw32
arch     i386
os       mingw32
system   i386, mingw32
status
major    1
minor    9.1
year     2004
month    06
day      21
language R
Phineas Campbell
On Tue, 26 Oct 2004, Phineas Campbell wrote:> I am trying to return the p value for a stat from the ECDF. That is the > index of the first occurrence, > on an ordered vector, of a value either greater than or equal to a given > value.That's called a *quantile*, probably type=1 in the Hyndman-Fan classification, if the `ordered vector' is the ECDF x-values.> Ideally I would not have to order the vector beforehand.You could use sort(partial=something), though, as quantile does. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
On 26-Oct-04 Phineas Campbell wrote:> I am trying to return the p value for a stat from the ECDF. > That is the index of the first occurrence, on an ordered vector, > of a value either greater than or equal to a given value. > > Ideally I would not have to order the vector beforehand. > > Currently I use: > > PValue<-function(stat, ECDF){ > ###Get the length of the ECFD > L<-length(ECDF) > ###Loop through the ECDF until the p value is found > for(i in 1:L){ > if(ECDF[i]>=stat){ > break > } > } > ###Return the 3 values that bracket the p value > c((i-1)/L, i/L, (i+1)/L) > } > > Is there a way of doing this that avoids the explicit loop?PValue<-function(stat, ECDF){ ###Get the length of the ECFD L<-length(ECDF) i<-min(which(ECDF>=stat)) ###Return the 3 values that bracket the p value c((i-1)/L, i/L, (i+1)/L) } will do what your function does, without the loop. Best wishes, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 [NB: New number!] Date: 26-Oct-04 Time: 20:13:33 ------------------------------ XFMail ------------------------------