program define stars version 4.0 global D_sm11 " This lab illustrates how one can use the Chi-square goodness of fit test to test whether a random sample comes from a particular distribuiton. You pick a sample size (n) and a number of bins (bins) and the lab generates n points that fall in the square of side 1 whose lower left corner is the origin." wdctl static D_sm11 5 5 290 32 global D_sm12 " The lab tests whether the points are randomly placed in the square by placing a (bins by bins) grid on the square, counting how many points fall in each little square, and using the Chi-square statistic to compare the observed counts to the expected counts. The lab displays the value of the statistic and its p-value." wdctl static D_sm12 5 40 290 40 * global D_sm13 "An Example: Let X be the number of times we flip a coin until we get the 5th head. This would be Negative Binomial with n=5 and pi=.5." * wdctl static D_sm13 5 85 290 16 global D_sm14 "Notes: 1) The expected number of points in each square is n/(bins^2). For the Chi-square statistic to be valid, this number must be at least 5. If you choose an n and bins for which this is not true, you will get an error message. 2) The degrees of freedom of the Chi-square statistic is the number of squares, that is, bins^2. 3) You can use the Tests of Significance Lab to verify that the p-value is correct." wdctl static D_sm14 75 85 200 60 global D_sm6 "n" global D_sm7 "10 50 100 200 300 400 500" wdctl static D_sm6 5 85 10 10 global D_var1 "500" wdctl ssimple D_var1 D_sm7 5 95 25 70 global D_sm8 "bins" global D_sm9 "2 3 4 5 6 7 8 9 10" wdctl static D_sm8 40 85 20 10 global D_var2 "5" wdctl ssimple D_var2 D_sm9 40 95 25 85 wdctl button "Run" 115 160 30 14 D_b1 wdctl button "Close" 150 160 30 14 D_b2 wdctl button "Help" 185 160 30 14 D_b3 global D_b1 "starsdr" global D_b2 "exit 1234" global D_b3 "whelp stars" cap noi wdlg "Chi-square Goodness of Fit Test" $D_dlgx $D_dlgy 300 240 end program define starsdr version 4.0 preserve clear local n=$D_var1 local nb=$D_var2 local nb2=`nb'*`nb' if `n'/`nb2' < 5 { sstopbox stop "n/(bins^2) must be at least 5" exit} gph open gph pen 1 set obs `n' gen x=uniform() gen y=uniform() graph y x, gap(1) s(i) xlab(0,.25,.50,.75,1) ylab(0,.25,.50,.75,1) bbox(3000,000,19000,16000,850,400,0) l1(" ") l2(" ") b1(" ") b2(" ") gphconv replace x=uniform() replace y=uniform() points2 x y 50 1 local nbp1=`nb'+1 gen x1=0 in 1/`nbp1' gen x2=1 in 1/`nbp1' gen x3=(_n-1)/`nb' segments x3 x1 x3 x2 segments x1 x3 x2 x3 graph y x, gap(1) s(i) xlab(0,.25,.50,.75,1) ylab(0,.25,.50,.75,1) bbox(3000,15000,19000,31000,850,400,0) l1(" ") l2(" ") b1(" ") b2(" ") gphconv segments x3 x1 x3 x2 segments x1 x3 x2 x3 gen bin=int(`nb'*y)*`nb'+int(`nb'*x)+1 sort bin by bin: gen int cx=_N if _n==1 sort cx * replace cx=0 if cx==. replace x1=. replace x2=. replace x1=(mod(bin-1,`nb')/`nb')+(1/(2*`nb')) replace x2=(int((bin-1)/`nb')/`nb')+(1/(2*`nb')) gen str3 z=string(int(cx)) if cx!=. texts x1 x2 z local expn=`n'/(`nb'*`nb') replace cx=(cx-`expn')^2/`expn' summarize cx local chi2: display %5.2f _result(1)*_result(3) local df=`nb'*`nb' local pv: display %5.2f chiprob(`df',`chi2') gph text 1000 16000 0 0 Chi-square=`chi2', df=`df', p-value=`pv' gph text 2000 16000 0 0 n=`n', Expected in each cell=`expn' gph close restore end exit