[R] Sampling in R

2006-03-23 Thread Noam Friedman
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@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


Re: [R] Sampling in R

2006-03-23 Thread Thomas Petzoldt
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@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

__
R-help@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


Re: [R] Sampling in R

2006-03-23 Thread Sean Davis



On 3/23/06 5:52 AM, Noam Friedman [EMAIL PROTECTED] 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

__
R-help@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


Re: [R] Sampling in R

2006-03-23 Thread pedro caƱadilla
I prefer the function resample as it is given in the sample help, in this
way:

resample - function(x, size, ...)
{
 if length(x = 1) {if (!missing(size)  size == 0) x[FALSE] else x}
 else sample(x, size, ...)
}

Anyway, if an observation can not be twice in the sample, you have to use
replace=FALSE, this is, not replacement.

The solution of Thomas is very good, although you have to check if your data
is going to be in the matrix as rows vectors or columns vector. If the
former, user byrow=TRUE in the matrix function, if the later, thomas
solution is perfect.

Instead of using a for loop, I prefer:
t(replicate(100, resample(x, 9))) if you want a matrix where the vectors are
rows
or replicate(100, resample(x, 9)) if you want a matrix where the vectors are
columns.

So the program can be:

hist(rowMeans(replicate(100, resample(x, 9.

I hope this is useful for you!!

Thanks  Regards,
Pedro Canadilla
Oracle DBA


On 3/23/06, Thomas Petzoldt [EMAIL PROTECTED] wrote:

 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@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

 __
 R-help@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


[[alternative HTML version deleted]]

__
R-help@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


Re: [R] Sampling in R

2006-03-23 Thread Liaw, Andy
From: Sean Davis
 
 On 3/23/06 5:52 AM, Noam Friedman [EMAIL PROTECTED] 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@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
 


__
R-help@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


[R] Sampling With R

2003-09-18 Thread Nathan Cooper
Hello,

I'm going to use R to analyze some point count data and I need to use the 
sample function. Let's say that I have a data set like below with each row 
being a visit to a site and the numbers in the rows individual distances. 
The real data set will of course have many more rows.

a
[[1]]
[1]  1 10  2 32 43 54 65 NA
[[2]]
[1]  2  1 32  6  5 44  3  2
[[3]]
[1]  3  3 22 56  7 NA NA NA
[[4]]
[1]  4 23  4 33 NA NA NA NA
[[5]]
[1]  5 22 12  2  2  2 32 NA
[[6]]
[1]  6 22 32 43 23  5 NA NA
I'm going to analyze how density changes with number of visits. So I will 
sample 1 visit (row) x number of times and export the distances to the 
program DISTANCE. I know that I can do the sampling part using:

sample(a,x,replace=TRUE,prob=NULL) This works fine. But I also need to 
sample 2 visits (rows) out of visits 1-3 and then 2 visits out of visits 
4-6, and so on x number of times. Any ideas?

Thanks much,

Nathan Cooper
Utah Division of Wildlife Resources
[EMAIL PROTECTED]
_
Use custom emotions -- try MSN Messenger 6.0! 
http://www.msnmessenger-download.com/tracking/reach_emoticon

__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help