[R] sampling with weights

r.ghezzo heberto.ghezzo at mcgill.ca
Wed May 5 14:36:20 CEST 2004


Hello, I am trying to produce an example of weighted sampling vs uniform 
sampling, the subject is estimation of the size distribution by sampling 
from a micrograph with a graticule of points. The sampling obtained is 
thus area weighted.
I created a distribution by
pop <- rexp(1000)+rexp(1000)+rexp(1000)
Now an uniform random sample is
index <- floor(1000*runif(100))+1
samp1 <- pop[index]
and the histograms of pop and samp1 are very similar as expected, Now to 
simulate a sample obtained by point counting method I did
cupop <- cumsum(pop)
popmax <- cupop[1000]
index <- popmax * runif(100)
samp2 <- rep(0,100)
now for each value in index I have to find the last point in cupop that 
is less than , then the next one is the chosen
for( i in 1:100) {
   xi <- which(cupop < index[i])
   samp2[i] <- pop[xi[length(xi)]+1]
}
Q: is there a more elegant and general way to do this sampling?
As it is it works but look clumsy
Thanks
Heberto Ghezzo Ph.D.
Meakins-Christie Labs
McGill University
Montreal - Canada




More information about the R-help mailing list