program define size version 4.0 preserve clear quietly { set obs 501 gen x= . gen xbar1=. gen xbar2=. gen s21=. gen s22=. gen stat = . gen y = .} global D_sm12 " The level of significance of a test (usually denoted by alpha) is the probability of rejecting the null hypothesis when it is true. In this lab you select 1) a test statistic, 2) whether you want a left, right, or two sided test, 3) a value of alpha, 4) the distribution of the population to be sampled from, and 5) a sample size (or sizes if doing F or two-sample-t)." wdctl static D_sm12 5 5 300 32 global D_sm13 " Then the lab will generate 500 samples (or pairs of samples) from the selected population and calculate the value of the test statistic (using the true parameter value). It draws a histogram of the 500 Z, t, chi-square, or F's and superimposes the theoretical curve. Finally, it shades in the critical region (or regions if a two-sided test) under the curve, displays the critical values and the proportion of the 500 statistics that would lead to rejection of H_0." wdctl static D_sm13 5 40 300 40 global D_sm14 "Things to Note: 1) This should emphasize what alpha represents (the proportion of times H_0 would be rejected if we did millions of samples), 2) In some of the situations, alpha is invalid if we are not sampling from a Normal population. In other situations the normality assumption is not required." wdctl static D_sm14 5 85 300 32 global D_sm1 "n_1" global HJN_V1=10 wdctl static D_sm1 5 130 12 10 wdctl edit HJN_V1 20 130 16 10 global D_sm2 "n_2" global HJN_V2=10 wdctl static D_sm2 40 130 12 10 wdctl edit HJN_V2 55 130 16 10 global D_sm31 "alpha" global D_sm32 ".01 .025 .05 .10" wdctl static D_sm31 102 120 20 10 global D_var4 ".05" wdctl ssimple D_var4 D_sm32 102 130 30 50 global D_sm4 "Test" global D_sm5 "Right-Sided Left-Sided Two-Sided" wdctl static D_sm4 135 120 40 16 global D_var3 "Right-Sided" wdctl ssimple D_var3 D_sm5 135 130 50 40 global D_sm8 "Statistic" global D_sm9 "Z One-sample-t Two-sample-t Chi-square F" wdctl static D_sm8 188 120 45 10 global D_var2 "Z" wdctl ssimple D_var2 D_sm9 188 130 55 60 global D_sm6 "Sample from" global D_sm7 "Normal(0,1) Uniform(0,1) Exponential(1)" wdctl static D_sm6 245 120 45 10 global D_var1 "Normal(0,1)" wdctl ssimple D_var1 D_sm7 245 130 55 40 wdctl button "Run" 5 165 30 14 D_b1 wdctl button "Close" 40 165 30 14 D_b2 wdctl button "Help" 75 165 30 14 D_b3 help global D_b1 "sizedr" global D_b2 "exit 1234" global D_b3 "whelp size" cap noi wdlg "Level of Significance of a Test" 5 5 310 200 restore end program define sizedr version 4.0 local n1=$HJN_V1 local n2=$HJN_V2 local alpha=$D_var4 if `n1'< 2 | `n1' > 100 { sstopbox stop "n1 must be between 2 and 100" exit} if `n2'< 2 | `n2' > 100 { sstopbox stop "n_2 must be between 2 and 100" exit} local nsamps=1 if "$D_var2"=="F" | "$D_var2"=="Two-sample-t" {local nsamps=2} gph open gph pen 2 if "$D_var1"=="Normal(0,1)" { local sig2 = 1.0 local mu = 0.0} if "$D_var1"=="Uniform(0,1)" { local sig2 = 1./12 local mu = 0.5} if "$D_var1"=="Exponential(1)" { local sig2 = 1.0 local mu = 1.0} local sig = sqrt(`sig2') replace xbar1=0 replace s21=0 if `nsamps'==1 { gph text 1000 16000 0 0 Generating 500 Samples From a $D_var1 Population gph text 2000 16000 0 0 and Calculating $D_var2 for Each Sample} else { gph text 1000 16000 0 0 Generating 500 Pairs of Samples From a $D_var1 Population gph text 2000 16000 0 0 and Calculating $D_var2 for Each Pair of Samples} gph box 6000 10000 8000 20000 4 local i = 1 while `i' <= `n1'{ local bx2=int(10000 + 10000*(`i'/`n1')) gph box 6000 10000 8000 `bx2' 1 if "$D_var1"=="Uniform(0,1)" { replace x = uniform()} if "$D_var1"=="Exponential(1)" { replace x = -log(uniform())} if "$D_var1"=="Normal(0,1)" { replace x = invnorm(uniform())} replace xbar1=xbar1+x replace s21=s21+x^2 local i=`i'+1 } replace s21=(s21-(xbar1^2/`n1'))/(`n1'-1) replace xbar1=xbar1/`n1' if `nsamps'==2 { replace xbar2=0 replace s22=0 gph box 6000 10000 8000 20000 4 local i = 1 while `i' <= `n2'{ local bx2=int(10000 + 10000*(`i'/`n2')) gph box 6000 10000 8000 `bx2' 1 if "$D_var1"=="Uniform(0,1)" { replace x = uniform()} if "$D_var1"=="Exponential(1)" { replace x = -log(uniform())} if "$D_var1"=="Normal(0,1)" { replace x = invnorm(uniform())} replace xbar2=xbar2+x replace s22=s22+x^2 local i=`i'+1 } replace s22=(s22-(xbar2^2/`n2'))/(`n2'-1) replace xbar2=xbar2/`n2' } if "$D_var2"=="Z" {replace stat = (xbar1-`mu')/sqrt(`sig2'/`n1')} if "$D_var2"=="One-sample-t" {replace stat = (xbar1-`mu')/sqrt(s21/`n1')} if "$D_var2"=="Chi-square" {replace stat = ((`n1'-1)*s21)/`sig2'} if "$D_var2"=="Two-sample-t" { replace s21=((`n1'-1)*s21+(`n2'-1)*s22)/(`n1'+`n2'-2) replace stat = (xbar1-xbar2)/sqrt(s21*((1/`n1')+(1/`n2')))} if "$D_var2"=="F" {replace stat = s21/s22} set graphics off graph stat, bin(20) xlabel ylabel l1(" ") l2(" ") b1(" ") b2(" ") bbox(3000,0,23000,32000,850,400,0) set graphics on local ymin = _result(1) local ymax = _result(2) local xmin = _result(3) local xmax = _result(4) replace x = `xmin' + ((_n-1)/500)*(`xmax'-`xmin') local ymax=`ymax'/((`xmax'-`xmin')/20) replace y = `ymax'*uniform() gph close gph open gph pen 1 graph y x, s(i) xlab ylab l1(" ") l2(" ") b1(" ") b2(" ") bbox(3000,0,23000,32000,850,400,0) gphconv local alp = `alpha' if "$D_var3"=="Two-Sided" {local alp = `alpha'/2} if "$D_var2"=="Z" { replace y = exp(-(x^2)/2)/sqrt(2*_pi) local critr=invnorm(1.-`alp') local critl=-`critr'} local ioptt=0 if "$D_var2"=="One-sample-t" | "$D_var2"=="Two-sample-t" {local ioptt=1} if `ioptt'==1 { local df=`n1'-1 if "$D_var2"=="Two-sample-t" {local df=`n1'+`n2'-2} pdf_t x y `df' local critr=invt(`df',1.-2*`alp') local critl=-`critr'} if "$D_var2"=="Chi-square" { local df=`n1'-1 pdf_chi2 x y `df' local critr=invnchi(`df',0,1.-`alp') local critl=invnchi(`df',0,`alp')} if "$D_var2"=="F" { local df1=`n1'-1 local df2=`n2'-1 pdf_f x y `df1' `df2' local critr=invfprob(`df1',`df2',`alp') local critl=invfprob(`df1',`df2',1.-`alp')} * replace y = y*(`xmax'-`xmin')/20 * summarize y * local yy1 = _result(6) * if(`yy1' > `ymax') {local ymax=`yy1'} * graph stat, bin(20) xlabel ylabel xscale(`xmin',`xmax') bbox(3000,0,23000,32000,850,400,0)/* * */ yscale(`ymin',`ymax') l1(" ") l2(" ") b1(" ") b2(" ") shading(4) * graph y x, c(l) s(i) xlabel ylabel xscale(`xmin',`xmax') bbox(3000,0,23000,32000,850,400,0) /* * */ yscale(`ymin',`ymax') l1(" ") l2(" ") b1(" ") b2(" ") pen(3) * gphconv replace xbar1=. replace xbar2=. if "$D_var3"=="Right-Sided" { replace xbar1=x if x>`critr' replace xbar2=y if x>`critr'} if "$D_var3"=="Left-Sided" { replace xbar1=x if x<`critl' replace xbar2=y if x<`critl'} if "$D_var3"=="Two-Sided" { replace xbar1=x if x<`critl' | x > `critr' replace xbar2=y if x<`critl' | x > `critr'} gph pen 3 lines x y replace s21=0 segments xbar1 s21 xbar1 xbar2 gph pen 2 histgm stat `xmin' `xmax' 20 * local mcritr=critr * local mcritl=critl gph pen 3 local ycr = -`ymax'/6 if "$D_var3"=="Two-Sided" { local mcritr: display %6.3f `critr' local mcritl: display %6.3f `critl' drtext `critr' `ycr' 1 `mcritr' drtext `critl' `ycr' -1 `mcritl'} if "$D_var3"=="Right-Sided" { local mcrit: display %6.3f `critr' drtext `critr' `ycr' 1 `mcrit'} if "$D_var3"=="Left-Sided" { local mcrit: display %6.3f `critl' drtext `critl' `ycr' -1 `mcrit'} * drpoint `crit' 0 1000 6 * drpoint `mcritl' 0 1000 1 * drpoint `mcritr' 0 1000 1 if "$D_var3"=="Right-Sided" { replace stat=stat>`critr' replace stat=sum(stat)} if "$D_var3"=="Left-Sided" { replace stat=stat<`critl' replace stat=sum(stat)} if "$D_var3"=="Two-Sided" { replace stat=stat>`critr' | stat < `critl' replace stat=sum(stat)} local ng: display %5.3f stat[501]/501. local ng1: display %6.3f `crit' gph pen 2 if `nsamps'==2 { gph text 1000 16000 0 0 Values of $D_var2 for 500 Pairs of Samples (n_1=`n1',n_2=`n2') From a $D_var1 Population} else { gph text 1000 16000 0 0 Values of $D_var2 for 500 Samples (n=`n1') From a $D_var1 Population} gph text 2000 16000 0 0 Proportion of Times $D_var3 Test With alpha=$D_var4 is Rejected is `ng' * if `nsamps'==2 { * gph text 3000 16000 0 0 n_1=`n1', n_2=`n2'} * else { * gph text 3000 16000 0 0 n=`n1'} gph close end exit