program define pval version 4.0 preserve clear quietly { set obs 501 gen x1=. gen y1=. gen zer=0 gen x= . gen y = .} global D_sm11 "If H_0 is true, Z and t should be close to 0, Chi-square close to n-1, and F close to 1." wdctl static D_sm11 5 5 290 8 global D_sm15 "The p-value of a test is a probability (between 0 and 1) that measures how close an observed test statistic is to what it should be if H_0 is true. If it is small, we reject H_0." wdctl static D_sm15 5 18 290 16 global D_sm12 "p-value = P(the observed value or any value less likely to occur if H_0 is true)." wdctl static D_sm12 5 40 290 8 global D_sm13 " Right-Sided (Left-Sided): p = Area to right of (left of) observed value of statistic." wdctl static D_sm13 5 50 290 8 global D_sm14 " Two-Sided: If statistic to left (right) of middle, p = 2 * area to left (right) of statistic." wdctl static D_sm14 5 60 290 8 global D_sm16 "You pick a situation (Z, one or two sample t, chi-square, or F), as well as right, left, or two sided test, and specify sample size (or sizes if two samples), and then lab generates a sample (or samples) from a N(0,1) population and calculates the test statistic. It draws the theoretical curve for this test, draws a circle where statistic falls, and shades in region under curve that is less likely than observed value. The p-value is the area of the shaded region. IN THIS LAB, H_0 IS TRUE." wdctl static D_sm16 5 70 290 48 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_sm20 "stat" global HJN_V3=1 wdctl static D_sm20 75 130 12 10 wdctl edit HJN_V3 90 130 16 10 global D_sm4 "Test" global D_sm5 "Right-Sided Left-Sided Two-Sided" wdctl static D_sm4 110 120 40 16 global D_var3 "Right-Sided" wdctl ssimple D_var3 D_sm5 110 130 50 40 global D_sm8 "Statistic" global D_sm9 "Z One-sample-t Two-sample-t Chi-square F" wdctl static D_sm8 170 120 45 10 global D_var2 "Z" wdctl ssimple D_var2 D_sm9 170 130 55 60 global D_sm6 "Choice" global D_sm7 "Lab-Value User-Value" wdctl static D_sm6 230 120 45 10 global D_var4 "Lab-Value" wdctl ssimple D_var4 D_sm7 230 130 55 36 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 "pvaldr" global D_b2 "exit 1234" global D_b3 "whelp size" cap noi wdlg "Tests of Significance" $D_dlgx $D_dlgy 300 240 restore end program define pvaldr version 4.0 local n1=$HJN_V1 local n2=$HJN_V2 local stat1=$HJN_V3 global D_var1 "Normal(0,1)" 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 x=. if "$D_var1"=="Uniform(0,1)" { replace x = uniform() in 1/`n1'} if "$D_var1"=="Exponential(1)" { replace x = -log(uniform()) in 1/`n1'} if "$D_var1"=="Normal(0,1)" { replace x = invnorm(uniform()) in 1/`n1'} summarize x local xbar1=_result(3) local s21=_result(4) if `nsamps'==2 { if "$D_var1"=="Uniform(0,1)" { replace x = uniform() in 1/`n2'} if "$D_var1"=="Exponential(1)" { replace x = -log(uniform()) in 1/`n2'} if "$D_var1"=="Normal(0,1)" { replace x = invnorm(uniform()) in 1/`n2'} summarize x local xbar2=_result(3) local s22=_result(4) } local df1=`n1'-1 local df2=`n2'-1 if "$D_var2"=="Z" {local stat = (`xbar1'-`mu')/sqrt(`sig2'/`n1')} if "$D_var2"=="One-sample-t" {local stat = (`xbar1'-`mu')/sqrt(`s21'/`n1')} if "$D_var2"=="Chi-square" {local stat = ((`n1'-1)*`s21')/`sig2'} if "$D_var2"=="Two-sample-t" { local s21=((`n1'-1)*`s21'+(`n2'-1)*`s22')/(`n1'+`n2'-2) local df1=`n1'+`n2'-2 local stat = (`xbar1'-`xbar2')/sqrt(`s21'*((1/`n1')+(1/`n2')))} if "$D_var2"=="F" {local stat = `s21'/`s22'} if "$D_var4"=="User-Value" { local stat=`stat1' if "$D_var2"=="Chi-square" | "$D_var2"=="F" { if `stat' <= 0 { sstopbox stop "Chi-square or F must be positive" exit} } } if "$D_var2"=="Z" | "$D_var2"=="One-sample-t" | "$D_var2"=="Two-sample-t" { local c1=-abs(`stat') local c2=abs(`stat')} if "$D_var2"=="Z" { if "$D_var3"=="Right-Sided" { local pval=1-normprob(`stat')} if "$D_var3"=="Left-Sided" { local pval=normprob(`stat')} if "$D_var3"=="Two-Sided" { local pval=2*(1-normprob(abs(`stat')))} local xmin=invnorm(.001) local xmax=invnorm(.999) replace x=`xmin' + (`xmax'-`xmin')*(_n-1)/500 replace y=exp(-(x^2)/2.)/sqrt(2.*_pi)} local side=1 if "$D_var3"=="Left-Sided" { local side = -1} if "$D_var3"=="Two-Sided" { local side=0} local test =1 if "$D_var2"=="One-sample-t" | "$D_var2"=="Two-sample-t" {local test=2} if "$D_var2"=="Chi-square" { local test=3} if "$D_var2"=="F" {local test=4} if `test'==2 { if `side'==1 & `stat' < 0 {local pval=1-tprob(`df1',`stat')/2} if `side'==1 & `stat' >=0 {local pval=tprob(`df1',`stat')/2} if `side'==(-1) & `stat' < 0 {local pval=tprob(`df1',`stat')/2} if `side'==(-1) & `stat' >=0 {local pval=1-tprob(`df1',`stat')/2} if `side'==0 { local pval=tprob(`df1',`stat')} local xmin=-invt(`df1',.998) local xmax=-`xmin' replace x=`xmin' + (`xmax'-`xmin')*(_n-1)/500 pdf_t x y `df1'} if "$D_var2"=="Chi-square" { if "$D_var3"=="Right-Sided" { local pval=chiprob(`df1',`stat')} if "$D_var3"=="Left-Sided" { local pval=1-chiprob(`df1',`stat')} if "$D_var3"=="Two-Sided" { local pv=1-chiprob(`df1',`stat') if `pv' < .5 { local c1=`stat' local c2=invnchi(`df1',0,1-`pv') local pval=2*`pv'} else { local c1=invnchi(`df1',0,1-`pv') local c2=`stat' local pval=2*(1-`pv') } } local xmin=invnchi(`df1',0,.001) local xmax=invnchi(`df1',0,.999) replace x=`xmin' + (`xmax'-`xmin')*(_n-1)/500 pdf_chi2 x y `df1'} if "$D_var2"=="F" { if "$D_var3"=="Right-Sided" { local pval=fprob(`df1',`df2',`stat')} if "$D_var3"=="Left-Sided" { local pval=1-fprob(`df1',`df2',`stat')} if "$D_var3"=="Two-Sided" { local pv=1-fprob(`df1',`df2',`stat') if `pv' < .5 { local c1=`stat' local c2=invfprob(`df1',`df2',`pv') local pval=2*`pv'} else { local c1=invfprob(`df1',`df2',`pv') local c2=`stat' local pval=2*(1-`pv') } } local xmin=invfprob(`df1',`df2',.001) local xmin=0. local xmax=invfprob(`df1',`df2',.001) replace x=`xmin' + (`xmax'-`xmin')*(_n-1)/500 pdf_f x y `df1' `df2'} graph y x, c(l) s(.) xlab ylab pen(3) bbox(3000,0,23000,32000,850,400,0) l1(" ") l2(" ") b1(" ") b2(" ") gphconv gph pen 2 local mpv: display %4.3f `pval' local mstat: display %5.3f `stat' gph text 1000 16000 0 0 p-value = shaded area = `mpv' for $D_var3 $D_var2 test if `nsamps'==1 { gph text 2000 16000 0 0 n = `n1', $D_var2 = `mstat'} else { gph text 2000 16000 0 0 n_1 = `n1', n_2 = `n2', $D_var2 = `mstat'} drpoint `stat' 0 200 1 summarize y local ymax1=-(_result(6)/5) local ymax2=-(_result(6)/7) if `side' == 0 { local mc1:display %5.3f `c1' local mc2:display %5.3f `c2' drtext `c1' `ymax1' 0 `mc1' drtext `c2' `ymax2' 0 `mc2'} else { drtext `stat' `ymax2' 0 `mstat'} gph pen 3 if "$D_var3"=="Right-Sided" { replace x1=. replace y1=. replace y1=y if x > `stat' replace x1=x if x > `stat' segments x1 zer x1 y1} if "$D_var3"=="Left-Sided" { replace x1=. replace y1=. replace y1=y if x < `stat' replace x1=x if x < `stat' segments x1 zer x1 y1} if "$D_var3"=="Two-Sided" { replace x1=. replace y1=. replace y1=y if x < `c1' | x > `c2' replace x1=x if x < `c1' | x > `c2' segments x1 zer x1 y1} * gph text 3000 16000 0 0 p-value=shaded area=`mpv' end exit