Announcement

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

  • Help with Program that Demonstrates Central Limit Theorem Nested Loop

    Dear all,

    I am using Stata 16, on mac. The following program I included selects a particular number of observations from a Chi-Square distribution.

    cap program drop randdraw

    program define randdraw

    clear

    args N distribution

    set obs `N'

    gen x = `distribution'

    sum

    end

    simulate mean_x =r(mean), reps(1000): randdraw 50 "rchi2(2)"


    If I I wanted to write a nested loop that will simulate drawing a sample mean from a uniform distribution, poisson distribution, and beta distribution from with sample sizes of 3, 40, 500, and 1,000, replicate each 500 times and plot a histogram of the resulting distribution of the sample means. So that the first histogram will have 500 observations of sample means taken from 3 observations drawn according to the underlying distribution, the second will have 500 observations of sample means taken from 50 observations, etc. Am I able to use the program that I listed previously?


    Thank you in advance for your help


    Jason Browen

  • #2
    Well, the code you show will generate 1000 means of 50 draws from a chi2 distribution with 2df. It won't plot a histogram, and it won't do any other sample sizes. So it seems you want to embellish the code a bit and then wrap it in a loop over sample size.

    Code:
    clear*
    
    cap program drop randdraw
    program define randdraw
        clear
        args N distribution
        set obs `N'
        gen x = `distribution'
        summ
    end
    
    set seed 1234
    
    foreach ss of numlist 3 40 500 1000 {
        simulate mean_x =r(mean), reps(1000) : randdraw `ss' "rchi2(2)"
        histogram mean_x, name(sample_size_`ss', replace)
    }

    Comment


    • #3
      Thank for Clyde for your help!

      Comment

      Working...
      X