Hello Listers, I am trying to sample a vector to create a new one of sample length, witha sum equal to the sum of the initial vector: initial = 10, 30, 10 (sum=50) sample example = 5, 35, 10 (sum=50) or 25, 15, 10 (sum=50), etc ... My problem is to control the sum, so it stays constant. Any suggestions would be very helpful ! Thank you in advance, Eric
Eric Pante a ??crit :> Hello Listers, > I am trying to sample a vector to create a new one of sample length, > witha sum equal to the sum of the initial vector: > initial = 10, 30, 10 (sum=50) > sample example = 5, 35, 10 (sum=50) or 25, 15, 10 (sum=50), etc ... > My problem is to control the sum, so it stays constant.f0 = function() { s1 = 50; for (i in 0:s1) { s2 = s1 - i; for (j in 0:s2) { s3 = s2 - j; print (c(i,j,s3)); } } } (If I have well understood the question) ? hih Vincent
Eric, If you want samples of size 3 from 0:50, with sum==50, this seems to do the job (with apologies to those who really know how to program in R): tot <- 50 ii <- 0 aa <- list() for(i in 0:tot){ for(j in 0:(tot-i)){ k <- tot-i-j ii <- ii+1 aa[[ii]] <- list(i=i,j=j,k=k) } } aa[sample(ii, 4)] # for a sample of 4. If you want a sample of size n from x[1],...x[N], such that the sample sum is T, then that is much trickier! Ted. On 04/10/05 07:31, Eric Pante wrote,:> Hello Listers, > > I am trying to sample a vector to create a new one of sample length, > witha sum equal to the sum of the initial vector: > > initial = 10, 30, 10 (sum=50) > sample example = 5, 35, 10 (sum=50) or 25, 15, 10 (sum=50), etc ... > > My problem is to control the sum, so it stays constant. > Any suggestions would be very helpful ! > > Thank you in advance, > Eric > > ______________________________________________ > 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-- Dr E.A. Catchpole Visiting Fellow Univ of New South Wales at ADFA, Canberra, Australia and University of Kent, Canterbury, England - www.ma.adfa.edu.au/~eac - fax: +61 2 6268 8786 - ph: +61 2 6268 8895
Your problem is nothing but that of drawing a random sample from a multinomial distribution. Check out the "Multinomial" function. ?Multinomial Best, Ravi. ________________________________ From: r-help-bounces@stat.math.ethz.ch on behalf of vincent@7d4.com Sent: Tue 10/4/2005 1:59 AM To: r-help@stat.math.ethz.ch Subject: Re: [R] sampling vectors Eric Pante a écrit :> Hello Listers, > I am trying to sample a vector to create a new one of sample length, > witha sum equal to the sum of the initial vector: > initial = 10, 30, 10 (sum=50) > sample example = 5, 35, 10 (sum=50) or 25, 15, 10 (sum=50), etc ... > My problem is to control the sum, so it stays constant.f0 = function() { s1 = 50; for (i in 0:s1) { s2 = s1 - i; for (j in 0:s2) { s3 = s2 - j; print (c(i,j,s3)); } } } (If I have well understood the question) ? hih Vincent ______________________________________________ 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]]
I mean to say: check out "rmultinom" function ? rmultinom For example, rmultinom(10,50,prob=rep(1,5)) will give you 10 vectors (arranged as columns of a matrix), each of length 5, sampled from 1:50 and adding up to 50. You can adjust the weights "prob" to draw according to your needs. Hope this is helpful, Ravi. ________________________________ From: r-help-bounces@stat.math.ethz.ch on behalf of Ravi Varadhan Sent: Tue 10/4/2005 7:03 AM To: vincent@7d4.com; r-help@stat.math.ethz.ch Subject: Re: [R] sampling vectors Your problem is nothing but that of drawing a random sample from a multinomial distribution. Check out the "Multinomial" function. ?Multinomial Best, Ravi. ________________________________ From: r-help-bounces@stat.math.ethz.ch on behalf of vincent@7d4.com Sent: Tue 10/4/2005 1:59 AM To: r-help@stat.math.ethz.ch Subject: Re: [R] sampling vectors Eric Pante a écrit :> Hello Listers, > I am trying to sample a vector to create a new one of sample length, > witha sum equal to the sum of the initial vector: > initial = 10, 30, 10 (sum=50) > sample example = 5, 35, 10 (sum=50) or 25, 15, 10 (sum=50), etc ... > My problem is to control the sum, so it stays constant.f0 = function() { s1 = 50; for (i in 0:s1) { s2 = s1 - i; for (j in 0:s2) { s3 = s2 - j; print (c(i,j,s3)); } } } (If I have well understood the question) ? hih Vincent ______________________________________________ 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]] [[alternative HTML version deleted]]
> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] > On Behalf Of ecatchpole > Sent: Monday, October 03, 2005 11:57 PM > To: Eric Pante > Cc: r-help at stat.math.ethz.ch > Subject: Re: [R] sampling vectors > > Eric, > > If you want samples of size 3 from 0:50, with sum==50, this seems to do > the job (with apologies to those who really know how to program in R): > > tot <- 50 > ii <- 0 > aa <- list() > for(i in 0:tot){ > for(j in 0:(tot-i)){ > k <- tot-i-j > ii <- ii+1 > aa[[ii]] <- list(i=i,j=j,k=k) > } > } > > aa[sample(ii, 4)] # for a sample of 4. > > If you want a sample of size n from x[1],...x[N], such that the sample > sum is T, then that is much trickier! > > Ted. > > On 04/10/05 07:31, Eric Pante wrote,: > > Hello Listers, > > > > I am trying to sample a vector to create a new one of sample length, > > witha sum equal to the sum of the initial vector: > > > > initial = 10, 30, 10 (sum=50) > > sample example = 5, 35, 10 (sum=50) or 25, 15, 10 (sum=50), etc ... > > > > My problem is to control the sum, so it stays constant. > > Any suggestions would be very helpful ! > > > > Thank you in advance, > > Eric > >Eric, Here is another solution, which allows vectors of different lengths and sums. vectorSample <- function(vec) { tot<-sum(vec) Len<-length(vec) v < -rep(0,Len) for(i in Len:2) { UL <- tot - sum(v) - i + 1 v[i]<-sample(1:UL,1) } v[1] <- tot - sum(v) v } vectorSample( c(10,30,10) ) Hope this helps, Dan Nordlund Bothell, WA
Eric,> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch [mailto:r-help- > bounces at stat.math.ethz.ch] On Behalf Of Eric Pante > Sent: Tuesday, October 04, 2005 8:47 AM > To: Daniel Nordlund > Cc: r-help at stat.math.ethz.ch > Subject: Re: [R] sampling vectors > > Hi Dan, > > I just tried your code with long vectors, and the sampling stops to be > random. Do you see any reason why this is ? > > examples: > > ex = c(30,13,9,8,7,7,7,6,6,5,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1) > > > vectorSample(ex) > [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3 1 7 > 6 > [24] 4 2 75 > > vectorSample(ex) > [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 2 > 11 > [24] 7 8 67 > >Eric, Probably because I failed to provide a complete solution. I had a couple questions while I was writing this that I didn't get clarified. One, the code needs to be slightly modified if you want to allow vector elements to be zero. I required each element to be >= 1. Two, I did not reorder the elements in the vector. Larger counts are more likely early in the process than late in the process (since the partial sum is approaching the original total). I just placed the counts in the vector result in reverse order of when they were obtained. The fix to the "randomness" is simple. Just return sample(v) instead of v as the function result (see below). If you want to allow zero elements I think you can just change the lower limit on the sampling to be 0 rather than 1 (not thoroughly tested) vectorSample <- function(vec) { tot<-sum(vec) Len<-length(vec) v <-rep(0,Len) for(i in Len:2) { UL <- tot - sum(v) - i + 1 v[i]<-sample(1:UL,1) #change preceding statement to # v[i]<-sample(0:UL,1) #if you want to allow zeros } v[1] <- tot - sum(v) sample(v) #return vector in random order } Dan Nordlund Bothell, WA