[R] sampling with weights

About this list Date view Thread view Subject view Author view Attachment view

From: r.ghezzo (heberto.ghezzo@mcgill.ca)
Date: Wed 05 May 2004 - 22:36:20 EST


Message-id: <4098DFC4.4000409@mcgill.ca>

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

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


About this list Date view Thread view Subject view Author view Attachment view

This archive was generated by hypermail 2.1.3 : Mon 31 May 2004 - 23:05:07 EST