Hi ALL: Suppose you have a population of N <- 5 observations, x <- c(43, 28, 7, 61, 39). From that you can draw a maximum of 10 samples without replacement of size n <- 3. (Command choose(N,n) yields 10). For instance the samples I seek are 43, 61, 7 39, 7, 28 ...etc How can I get R to do that for me, to get an exhaustive list of samples of size n drawn without replacement from a population of size N? The command, sample(x, 3, replace=FALSE), works well for one draw. Is there a package that will handle multiple draws? Thanks and best wishes, ANDREW -------------- next part -------------- An HTML attachment was scrubbed... URL: https://stat.ethz.ch/pipermail/r-help/attachments/20011120/40da9b3e/attachment.html
On Tue, 20 Nov 2001, Andrew Criswell wrote:> Hi ALL: > > Suppose you have a population of N <- 5 observations, x <- c(43, 28, 7, 61, 39). From that you can draw a maximum of 10 samples without replacement of size n <- 3. (Command choose(N,n) yields 10). For instance the samples I seek are > > 43, 61, 7 > 39, 7, 28 ...etc > > How can I get R to do that for me, to get an exhaustive list of samples of size n drawn without replacement from a population of size N? The command, sample(x, 3, replace=FALSE), works well for one draw. Is there a package that will handle multiple draws?This is a question about drawing subsets, as covered in the Programmer's Niche articles in Rnews. You want all subsets (not subvectors, as order does not matter) of size n=3 from N=5 items. There is some C code to so it in package lqs, where it is used for exhaustive searching, but that doesn't currently have an R interface (it did once). -- 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 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Suppose you have a population of N <- 5 observations, x <- c(43, 28, 7, 61, 39). From that you can draw a maximum of 10 samples without replacement of size n <- 3. (Command choose(N,n) yields 10). For instance the samples I seek are 43, 61, 7 39, 7, 28 ...etc How can I get R to do that for me, to get an exhaustive list of samples of size n drawn without replacement from a population of size N? The command, sample(x, 3, replace=FALSE), works well for one draw. Is there a package that will handle multiple draws? # First solution combix<-function(x,r) { n<-length(x) total<-choose(n,r) a<-matrix(0,total,r) ind<-c(1:r) for (i in 1:total){ for (j in 1:r){a[i,j]<-x[ind[j]]} for (j in r:1){ miro<-j ind[j]<-ind[j]+1 if (ind[j]<n-r+j+1) break() } while(miro<r) { ind[miro+1]<-ind[miro]+1 miro<-miro+1 } } return(a) } print(combix(x=1:5,r=3)) x<-c(8,3,1,11,4,7) print(mean(x)) medias<- apply(combix(x,2),1,mean) print(mean(medias)) #With-Replacement combixr<-function(x,r) { n<-length(x) total<-n**r a<-matrix(0,total,r) ind<-rep(1,r) for (i in 1:total){ for (j in 1:r){a[i,j]<-x[ind[j]]} for (j in r:1){ miro<-j ind[j]<-ind[j]+1 if (ind[j]<n+1) break() } while(miro<r) { ind[miro+1]<-1 miro<-miro+1 } } return(a) } print(combixr(x=1:5,r=3)) x<-c(8,3,1,11,4,7) print(mean(x)) medias<- apply(combixr(x,2),1,mean) mean(medias) #Other form darcombi<- function(N,n,caso){ vecpeq<-rep(0,n) ante<-0 ante1<-0 for(i in 1:n){ N1<- N-(ante1+1) n1<- n-i v1<-cumsum(choose(N1:n1,n1)) ante<-min(which(caso<=v1)) vecpeq[i]<-ante+ante1 resta<-0 if (ante>1) resta<-v1[ante-1] caso<-caso-resta ante1<-vecpeq[i] } return(vecpeq) } N<-5 n<-3 for (i in 1:choose(N,n)){ print(darcombi(N=N,n=n,i)) } x <- c(43, 28, 7, 61, 39) N<-length(x) n<-3 for (i in 1:choose(N,n)){ print(x[darcombi(N=N,n=n,i)]) } -------------- next part -------------- An HTML attachment was scrubbed... URL: https://stat.ethz.ch/pipermail/r-help/attachments/20011120/5dd9b6ff/attachment.html
There is a recursive function called "subsets" in the S-Programming book by V&R, that lets you generate all unique combinations of n objects taken k at a time. Tim Hesterberg also has a non-recursive function called "combinations", which may also be used. Note that the recursive function uses a lot more memory. You can use any of these functions to generate unique, exhaustive samples. Hope this helps, Ravi. Here is the non-recursive function (courtesy: Tim Hesterberg): combinations _ function(n, k){ # Compute all n choose k combinations of size k from 1:n # Return matrix with k rows and choose(n,k) columns. # Avoids recursion. if(!is.numeric(n) || length(n) != 1 || n%%1) stop("'n' must be an integer") if(!is.numeric(k) || length(k) != 1 || k%%1) stop("'k' must be an integer") if(k > n || k <= 0) return(numeric(0)) rowMatrix _ function(n) structure(1:n, dim=c(1,n)) colMatrix _ function(n) structure(1:n, dim=c(n,1)) if(k == n) return(colMatrix(n)) if(k == 1) return(rowMatrix(n)) L _ vector("list", k) # L[[j]] will contain combinations(N, j) for N = 2:n L[[1]] _ rowMatrix(2) L[[2]] _ colMatrix(2) Diff _ n-k for(N in seq(3, n, by=1)){ # loop over j in reverse order, to avoid overwriting for(j in seq(min(k, N-1), max(2, N-Diff), by= -1)) L[[j]] _ cbind(L[[j]], rbind(L[[j-1]], N, deparse.level=0)) if(N <= Diff+1) L[[1]] _ rowMatrix(N) else L[[N-(Diff+1)]] _ numeric(0) if(N <= k) L[[N]] _ colMatrix(N) } L[[k]] } -----Original Message----- From: Andrew Criswell <arc at arcriswell.com> To: r-help at stat.math.ethz.ch <r-help at stat.math.ethz.ch> Date: Tuesday, November 20, 2001 12:52 AM Subject: [R] Sampling from a population Hi ALL: Suppose you have a population of N <- 5 observations, x <- c(43, 28, 7, 61, 39). From that you can draw a maximum of 10 samples without replacement of size n <- 3. (Command choose(N,n) yields 10). For instance the samples I seek are 43, 61, 7 39, 7, 28 ...etc How can I get R to do that for me, to get an exhaustive list of samples of size n drawn without replacement from a population of size N? The command, sample(x, 3, replace=FALSE), works well for one draw. Is there a package that will handle multiple draws? Thanks and best wishes, ANDREW -------------- next part -------------- An HTML attachment was scrubbed... URL: https://stat.ethz.ch/pipermail/r-help/attachments/20011120/f1e9cc8a/attachment.html