[R] sampling vectors
ecatchpole
E.Catchpole at adfa.edu.au
Wed Oct 5 04:13:15 CEST 2005
Eric,
Following on from Ravi's suggestion, try
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)
tot <- sum(ex)
N <- length(ex)
rmultinom(n=3, size=tot, prob=rep(1,N))
Ted.
On 05/10/05 06:07, Nordlund, Dan wrote,:
> 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
>
> ______________________________________________
> 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
More information about the R-help
mailing list