Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Generate random numbers with specific mean and standard deviation within a range

    Hello, I am using STATA 14 and would like to generate some random numbers that have a specific mean and standard deviation but fall with a range.

    I used

    gen x = rnormal (m, s) and the mean and SD were spot on but the range contains negative numbers. In this case I need the numbers to range from a minimum of 5.98 to a maximum of 9. Just to test, I used runiform( ) as it allows a range but the SD was off. Any help is welcomed.

    Thanks in advance!

  • #2
    If you wish the mean, SD and range all to be respected, your only obvious option is to sample from the data. The request isn't consistent with any named distribution.

    Comment


    • #3
      Ted.
      perhaps what follows can be useful (I do not think that you can set the range of your in addition to -rnormal- parameters):
      Code:
      . set obs 100
      number of observations (_N) was 100, now 100
      
      . range x 5.98 9 100
      
      . sum
      
          Variable |        Obs        Mean    Std. Dev.       Min        Max
      -------------+---------------------------------------------------------
                 x |        100        7.49     .884997       5.98          9
      
      . gen X=rnormal(r(mean), r(sd)
      Kind regards,
      Carlo
      (Stata 19.0)

      Comment


      • #4
        My approach would be this. I'll first set up an Excel spreadsheet with two input cells and two output cells. The first input is parameter mu and the second input is parameter sigma. The first output is the mean formula for a truncated normal distribution and second output is the square root of the variance formula for a truncated normal distribution, where truncation occurs at alpha = 5.98 and beta = 9. The Greek letters refer to the Wikipedia page on truncated normal:

        https://en.wikipedia.org/wiki/Trunca...l_distribution

        Then I will play around with mu and sigma until the mean and the square root of the variance (i.e., SD) come close to what I'm looking for. There may be a more efficient way to solve for mu and sigma (e.g., https://doi.org/10.1016/j.spl.2015.05.006), but I will not consider it unless the simple trial and error method turns out to be hopelessly slow.

        Once I have identified a combination of mu and sigma that gives the mean and SD that I'm looking for, I will return to Stata and draw from the corresponding truncated normal distribution.
        Last edited by Hong Il Yoo; 05 Nov 2020, 08:23.

        Comment


        • #5
          Have you looked at the beta distribution?
          ---------------------------------
          Maarten L. Buis
          University of Konstanz
          Department of history and sociology
          box 40
          78457 Konstanz
          Germany
          http://www.maartenbuis.nl
          ---------------------------------

          Comment


          • #6
            If your distribution is to be taken seriously as even roughly normal then a range from about 6 to about 9 implies a mean around 7.5. The range is about 3, with an SD therefore of around 0.75 if the range is about 4 SD and an SD of around 0.5 if the range is about 6 SD. So, on this reasoning any negative values simulated are at least 10 and possibly 15 SDs below the mean and thus utterly freakish in a normal distribution.

            I stick with the suggestion in #2 but independently of that I don't find the evidence in #1 to be very plausible. Otherwise put, what is the error in my reasoning?

            Comment


            • #7
              Thanks for the suggestions, I will keep on searching!

              Comment

              Working...
              X