Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Random' sample with underlying pattern

Tags:

r

I want to create a vector of random integers with values between 1 and 10, with a gradual increase in the likelihood of higher numbers appearing as I progress through the vector. But, I still want the values to be mixed together, i.e., I'm trying to avoid 1,1,1,1,2,2,2,3... etc

I have so far managed the following, which works, but surely there is a more elegant way of achieving this?

# Generate 3 vectors of 1000 random integers, each with slightly different distributions
LOW <- rpois(1000, 2)
MID <- rpois(1000, 4)
HIGH <- rpois(1000, 7)

# Remove any values outside the desired range of 1:10
LOW <- LOW[LOW>0 & LOW<11]
MID <- MID[MID>0 & MID<11]
HIGH <- HIGH[HIGH>0 & HIGH<11]

# Sample 200 values from each vector
LOW <- sample(LOW, 200)
MID <- sample(MID, 200)
HIGH <- sample(HIGH, 200)

# Combine into a single vector
COMBINED <- c(LOW, MID, HIGH)

# Check distribution
X <- 1:600
plot(COMBINED)
lines(predict(lm(COMBINED~X)), col='red', lwd = 6)

enter image description here

like image 656
EcologyTom Avatar asked Oct 22 '25 11:10

EcologyTom


1 Answers

You don't say anything about the distribution you actually want, but if a binomial distribution is acceptable:

plot(1+rbinom(n=600, size=9, prob = seq(0.1,0.9,l=600)))

enter image description here

like image 109
George Savva Avatar answered Oct 25 '25 01:10

George Savva