0

I want to pick up 50 samples from (TRUNCATED) Normal Distribution (Gaussian) in a range 15-85 with mean=35, and sd=30. For reproducibility:

num = 50 # number of samples
rng = c(15, 85) # the range to pick the samples from
mu = 35 # mean
std = 30 # standard deviation

The following code gives 50 samples:

rnorm(n = num, mean = mu, sd = std)

However, I want these numbers to be strictly between the range 15-85. How can I achieve this?

UPDATE: Some people made great points in the comment section that this problem can not be solved as this will no longer be Gaussian Distribution. I added the word TRUNCATED to the original post so it makes more sense (Truncated Normal Distribution).

bird
  • 2,938
  • 1
  • 6
  • 27
  • 1
    If you restrict the bounds of data sampled from a Normal distribution, no matter how wide the bounds are relative to the mean and sd, it's no longer Normal. Your question, as posed, has no solution. – Limey Mar 25 '21 at 13:14
  • 1
    `library('MCMCglmm')` `rtnorm(n = 50, mean = mu, sd = std, lower = 15, upper = 85)` can help you ? – Basti Mar 25 '21 at 13:16
  • 1
    See: [setting upper and lower limits in rnorm](https://stackoverflow.com/q/19343133/10488504) – GKi Mar 25 '21 at 13:22

1 Answers1

3

As Limey said in the comments, by imposing a bounded region the distribution is no longer normal. There are several ways to achieve this.

library("MCMCglmm")
rtnorm(n = 50, mean = mu, sd = std, lower = 15, upper = 85)

is one method. If you want a more manual approach you could simulate using uniform distribution within the range and apply the normal distribution function

bounds <- c(pnorm(15, mu, std), pnorm(50, mu, std))
samples <- qnorm(runif(50, bounds[1], bounds[2]), mu, std)

The idea is very basic: Simulate the quantiles of the outcome, and then estimate the value of the specific quantive given the distribution. The value of this approach rather than the approach linked by GKi is that it ensures a "normal-ish" distribution, where simulating and bounding the resulting vector will cause the bounds to have additional mass compared to the normal distribution.

Note the outcome is not normal, as it is bounded.

Oliver
  • 8,169
  • 3
  • 15
  • 37