Hi all! I was wondering if you can help me with a little sampling issue I'm having in R. I have a database with 100 observations. I need to sample n=9 sample size, 200 times (i.e. get 200 samples of size 9). N (pop. size) =100 Each sample can't contain the same observation more than one time (i.e. the program needs to check if the obs. has already been sampled into the sample - it it has, it should put it back and sample again another obs.) obviously I need to do this with replacement. Then, I need to draw (I think the best is with a histogram) the distribution of the mean of each os the 200 samples. I guess I need to do a loop for the 'sample' function in R. I could really use your help. Regards, Noam Friedman.
Does the following what you want:
x <- 1:100
s <- matrix(0, nrow=200, ncol=9)
for (i in 1:200) {
  s[i, ] <- sample(x, 9)
}
m <- rowMeans(s)
hist(m)
The default behavior of sample is without replacement.
Thomas
Noam Friedman wrote:> > Hi all!
> >
> >
> > I was wondering if you can help me with a little sampling issue
I'm
> > having in R.
> >
> > I have a database with 100 observations.
> > I need to sample n=9 sample size, 200 times (i.e. get 200 samples of
> > size 9).
> > N (pop. size) =100
> >
> > Each sample can't contain the same observation more than one time
> > (i.e. the program needs to check if the obs. has already been sampled
> > into the sample - it it has, it should put it back and sample again
> > another obs.)
> >
> > obviously  I need to do this with replacement.
> >
> > Then, I need to draw (I think the best is with a histogram) the
> > distribution of the mean of each os the 200 samples.
> >
> > I guess I need to do a loop for the 'sample' function in R.
> >
> > I could really use your help.
> >
> >
> > Regards,
> >
> >
> > Noam Friedman.
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
On 3/23/06 5:52 AM, "Noam Friedman" <noamfrie at qos.net.il> wrote:> Hi all! > > > I was wondering if you can help me with a little sampling issue I'm > having in R. > > I have a database with 100 observations. > I need to sample n=9 sample size, 200 times (i.e. get 200 samples of > size 9). > N (pop. size) =100 > > Each sample can't contain the same observation more than one time > (i.e. the program needs to check if the obs. has already been sampled > into the sample - it it has, it should put it back and sample again > another obs.) > > obviously I need to do this with replacement. > > Then, I need to draw (I think the best is with a histogram) the > distribution of the mean of each os the 200 samples. > > I guess I need to do a loop for the 'sample' function in R.That sounds correct.> I could really use your help.x <- rnorm(100) meanvec <- vector() for (j in 1:200) { y <- sample(x,9) meanvec[j] <- mean(y) } hist(meanvec) If this example code doesn't get you started, then you will probably need to be more specific about where you are getting stuck. Sean
From: Sean Davis> > On 3/23/06 5:52 AM, "Noam Friedman" <noamfrie at qos.net.il> wrote: > > > Hi all! > > > > > > I was wondering if you can help me with a little sampling issue I'm > > having in R. > > > > I have a database with 100 observations. > > I need to sample n=9 sample size, 200 times (i.e. get 200 > samples of > > size 9). N (pop. size) =100 > > > > Each sample can't contain the same observation more than one time > > (i.e. the program needs to check if the obs. has already > been sampled > > into the sample - it it has, it should put it back and sample again > > another obs.) > > > > obviously I need to do this with replacement. > > > > Then, I need to draw (I think the best is with a histogram) the > > distribution of the mean of each os the 200 samples. > > > > I guess I need to do a loop for the 'sample' function in R. > > That sounds correct. > > > I could really use your help. > > x <- rnorm(100) > meanvec <- vector() > for (j in 1:200) { > y <- sample(x,9) > meanvec[j] <- mean(y) > } > hist(meanvec)If the samples are not needed beyond the calculations Noam mentioned, then this might save some memory: samp.means <- replicate(200, mean(sample(x, 9))) hist(samp.means) Andy> If this example code doesn't get you started, then you will > probably need to be more specific about where you are getting stuck. > > Sean > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >