gmacro CI ### Simulation to construct (k1) 95% confidence intervals from random ### samples, each of size (k2) from a normal distribution of mean (k3) ### and standard deviation (k4). The proportion of intervals that ### capture the true value of the population mean is reported. ### Macro created 2015 02 17 and last modified 2021 02 18 by G.H. George mtitle "Confidence Interval Simulation" #keep all output in one pane erase c1-c10 # needed to ensure clear space in the worksheet ### Constants: ### k1 = number of random samples ### k2 = sample size for each sample let k3 = 100 # population mean let k4 = 5 # population standard deviation (must be positive) ### k5 = loop counter; index number of current random sample ### k6 = half-width of confidence interval ### k7 = proportion of 95% CI that capture the true mean ### k8 = tail area = 2.5% for 95% two-tailed CI ### k9 = critical t-value at 95% confidence (2-tailed) for (k2-1) deg. freedom ### k10 = number of degrees of freedom = k2 - 1 ### Columns: name c1 'sample' # all values in the current random sample name c2 'mean' # sample means for all (k1) random samples name c3 's.e.' # standard errors for all (k1) random samples name c4 'low' # lower boundary of each confidence interval name c5 'high' # upper boundary of each confidence interval name c6 'capture' # = 1 if true mean inside confidence interval name c7 'sample size' # temporary column for input of k2 name c8 'alpha/2' # tail area = 2.5% for 95% two-tailed CI name c9 't_crit' # critical t-value name c10 'sampleNo' # current sample number ### Note that keyboard data entry must be into columns, not constants: note Enter sample size (must be an integer > 2): set 'sample size'; file "terminal"; nobs 1. copy 'sample size' k2 note Enter number of samples (must be a positive integer): set 'sampleNo'; file "terminal"; nobs 1. copy 'sampleNo' k1 let 'sampleNo'(2) = k1 ### Calculate and store the critical t-value (k9) let k10 = k2 - 1 let 'alpha/2'(1) = 0.025 InvCDF 'alpha/2' 't_crit'; T k10. copy 't_crit' k9 ### Draw the (k1) random samples, each of size (k2) in column c1: do k5=1:k1 # conduct the (k1) simulations let 'sampleNo'(1) = k5 # display current sample number random k2 'sample'; Normal k3 k4. # X ~ N(k3, (k4)^2) in column c1 let 'mean'(k5) = mean('sample') let 's.e.'(k5) = stdev('sample')/sqrt(k2) let k6 = abs(k9*'s.e.'(k5)) let 'low'(k5) = 'mean'(k5) - k6 let 'high'(k5) = 'mean'(k5) + k6 let 'capture'(k5) = 0 if (k3 > 'low'(k5)) and (k3 < 'high'(k5)) # mu inside CI? let 'capture'(k5) = 1 endif enddo ### Calculate and report the proportion of 95% CI that capture mu. let k7 = sum('capture') / k1 note The percentage of 95% confidence intervals that capture the true value of the population mean is print k7; format (%7.2). ### Plot all the confidence interval boundaries in ascending order ### of sample mean Sort 'mean'-'capture' 'mean'-'capture'; By 'mean'. Tsplot high low; Overlay; Index; Legend; Section 1; Reference 2 k3; Symbol; Grid 2; Footnote; FPanel; Title "Plot of 95% confidence interval boundaries for mu"; NoDTitle. endmtitle endmacro