I've been stuck with this for a while, so I decided to write a question.
Problem: How to generate a random sample (of lenght n) with a lower/upper bound and arround a specific mean.
Observation: distribution doesn't need to be specific (it could be normal, beta, etc).
Aproaches considered:
- One aproach is to use the
rtnorm
function (package msm) which generates a random numbers with a normal distribution within specified bounds but it doesn't hold your wanted mean value. -
A second aproach I've tried is this function which I found in a question I can't find anymore
rBootstrap<-function(n,mean,sd,lowerBound,upperBound){ range <- upperBound - lowerBound m <- (mean-lowerBound) / range #mapping mean to 0-1 range s <- sd / range #mapping sd to 0-1 range a <- (m^2 - m^3 - m*s^2)/s^2 #calculating alpha for rbeta b <- (m-2*m^2+m^3-s^2+m*s^2)/s^2 #calculating beta for rbeta data <- rbeta(n,a,b) #generating data data <- lowerBound + data * range #remaping to given bounds return(data) }
this function actually gives great results unless: upperBound > lowerBound + (2* mean - lowerBound) (upper bound exceeds two times the distance from the lowerBound to the mean).
Particularly, I would like to generate a random sample of lenght 1,800, with values between 50,000 and 250,000 with mean value = 70,000.
Aucun commentaire:
Enregistrer un commentaire