Kashif Ali
Kashif Ali

Reputation: 197

How to Generate Normal Random Samples within Mean±3Sigma

I want to draw normal random numbers in an array of order ((100*8)*5000) with a specific Mean (M) and Standard Deviation (S) but I want them to be only within the range M±3S, so that I don't have any outliers in my array exceeding those limits.

Any Suggestion? I want to write a program in R based on this array for some simulation studies. I am using following R Code to generate my Data Set:

for(i in 1:5000){
for(j in 1:8){
            Dat[,j,i]=rnorm(100,mean=muu[j],sd=sigma[j])
         }
         }

Now, We want to get rid of those values which are higher than muu±3sigma in the above data. Definitely, We have to replace discarded values with fresh values so that the dimension of the Dat array keep intact.

Upvotes: 0

Views: 3806

Answers (1)

SabDeM
SabDeM

Reputation: 7200

First Solution

Here is a start but I bet there is a more elegant solution. First generate a sample next step is to subset it to your desired values. Of course you have to adjust values to your desire.

set.seed(123)
rs <- rnorm(10000, mean = 10, sd = 3)
rs1 <- rs[ rs >= -19 & rs <= 19  ]

Second (better) solution

I think my first solutions didn't work so well. I have just written some code that might be perfect for your purposes. Here are the steps.

  • create an array of NAs with the required dimensions
  • fill it with random numbers
  • create a logical vector where TRUEs are for the desired conditions
  • subset the data based on that vector and replace the values where TRUE is TRUE (pardon my words game) with the mean used to generate samples

    data <- array(NA, dim = c(100, 8, 5000))

    for(i in 1:5000){
        data[ , , i] <- rnorm(800, 3, 1)
    }
    
    bound <- 3 + c(-1, 1)*3*1
    pr <- data <= bound[1] | data >= bound[2]
    data[pr] <- 3
    

Upvotes: 2

Related Questions