* version 1.0.1 12jul2000 MER version 6 (STB-58: sxd3) capture program drop sskdlg program define sskdlg version 6.0 * ========================= DIALOG BOX ================================= * ====================================================================== global D_run "1" clear set more off * enter kappa, p1, p2, d and significance level global D_sm1 "Expected kappa" global D_sm2 "Prop. pos. by rater 1" global D_sm3 "Prop. pos. by rater 2" global D_sm4 "Absolute precision" global D_sm5 "Confidence level" window control static D_sm1 15 7 60 10 window control static D_sm2 15 18 60 10 window control static D_sm3 15 29 60 10 window control static D_sm4 15 40 60 10 window control static D_sm5 15 51 60 10 window control edit 90 7 15 8 D_kappa window control edit 90 18 15 8 D_p1 window control edit 90 29 15 8 D_p2 window control edit 90 40 15 8 D_d window control edit 90 51 9 8 D_level * activating q and qmax window control check "Show value of Q" 15 64 140 8 D_cb1 right window control check "Show maximum sample size" 15 75 140 8 D_cb2 right * Graphs Q, S, P and D window control static D_sm6 5 92 107 91 blackframe window control static D_sm7 27 89 62 10 center global D_sm7 "Graph Options" window control button "Graph Q" 12 100 35 9 D_bgrq global D_bgrq "noi drgrq" global D_sm12 "X: kappa" window control static D_sm12 69 99 37 7 left window control edit 63 107 17 7 D_frk window control edit 86 107 17 7 D_tok window control check "k_max" 68 116 37 7 D_cb4 window control button "Graph S" 12 117 35 9 D_bgrs global D_bgrs "noi drgrs" window control button "Graph P" 12 135 35 9 D_bgrp global D_bgrp "noi drgrp" global D_sm13 "X: prop." window control static D_sm13 69 129 37 7 left window control edit 63 137 17 7 D_frp window control edit 86 137 17 7 D_top window control button "Graph D" 12 154 35 9 D_bgrd global D_bgrd "noi drgrd" global D_sm14 "X: ssize" window control static D_sm14 69 148 37 7 left window control edit 60 156 20 7 D_frd window control edit 86 156 20 7 D_tod * Keep variables window control check "Keep variables on exit" 15 165 80 15 D_cb3 right * Sample size for sample(#) global D_sm6 "Sample size for d" window control check "Sample size for d" 15 189 70 8 D_cb8 window control edit 89 189 20 7 D_ssize * Defaults values global D_kappa 0 global D_p1 0.1 global D_p2 0.1 global D_d 0.1 global D_frk 0 global D_tok 1.0 global D_frp 0 global D_top 1.0 global D_frd 0 global D_tod 500 global D_ssize 1000 global D_level 95 * Buttons window control button "OK" 8 203 30 11 D_bok window control button "Exit" 44 203 30 11 D_bex window control button "Help" 80 203 30 11 D_bhl help global D_bhl "whelp sskdlg" global D_bok "noi sskok" global D_bex "noi exitok" noi capture window dialog "Sample size for kappa" . . 120 230 global D_run end * ======================= MAIN PROGRAM ================================= * ====================================================================== program define sskok version 6.0 global z = invnorm(1-(1-$D_level/100)/2) * Exit situations + Warning messages if $D_kappa < -1 { di _n in re " " _n di in re " The specified value for the minimum kappa worth" _n di in re " finding is less than -1, which is meaningless." _n di in re " Sample size will not be calculated." _n di _n in re " " _n capture clear exit } if $D_kappa > 1 { di _n in re " " _n di in re " The specified value for the minimum kappa" _n di in re " worth finding is above 1, which is meaningless." _n di in re " Sample size will not be calculated." _n di _n in re " " _n capture clear exit } if $D_p1 <= 0 { di _n in re " " _n di in re " The specified value for p1 is less or equal to 0," _n di in re " which is meaningless. Sample size will not be" _n di in re " calculated." _n di _n in re " " _n capture clear exit } if $D_p1 > 1 { di _n in re " " _n di in re " The specified value for p1 is above 1," _n di in re " which is meaningless. Sample size will not be" _n di in re " calculated." _n di _n in re " " _n capture clear exit } if $D_p2 <= 0 { di _n in re " " _n di in re " The specified value for p1 is less or equal to 0," _n di in re " which is meaningless. Sample size will not be" _n di in re " calculated." _n di _n in re " " _n capture clear exit } if $D_p2 > 1 { di _n in re " " _n di in re " The specified value for p1 is above 1," _n di in re " which is meaningless. Sample size will not be" _n di in re " calculated." _n di _n in re " " _n capture clear exit } * Evaluating maximum kappa given a set o specified marginals if "$D_p2" == "" {$D_p1 == p} if $D_p1 >= $D_p2 { global kmax = (($D_p2+(1-$D_p1))-(($D_p1*$D_p2)+ /* */ ((1-$D_p1)*(1-$D_p2))))/(1-(($D_p1*$D_p2)+ /* */ ((1-$D_p1)*(1-$D_p2)))) } if $D_p1 < $D_p2 { global kmax = (($D_p1+(1 -$D_p2))-(($D_p1*$D_p2)+ /* */ ((1-$D_p1)*(1-$D_p2))))/(1-(($D_p1*$D_p2)+ /* */ ((1-$D_p1)*(1-$D_p2)))) } * Evaluating minimum kappa given a set o specified marginals if $D_p1 >= $D_p2 { global kmin = (((1-$D_p2)-$D_p1)-(($D_p1*$D_p2)+ /* */ ((1-$D_p1)*(1-$D_p2))))/(1-(($D_p1*$D_p2)+ /* */ ((1-$D_p1)*(1-$D_p2)))) } if $D_p1 < $D_p2 { global kmin = (((1-$D_p1)-$D_p2)-(($D_p1*$D_p2)+ /* */ ((1-$D_p1)*(1-$D_p2))))/(1-(($D_p1*$D_p2)+ /* */ ((1-$D_p1)*(1-$D_p2)))) } * More global macros global rkmax = round($kmax, .01) global kplusd = ($D_kappa+$D_d) global kmind = ($D_kappa-$D_d) global rkmin = round($kmin, .01) * Calculating Q, conditional on a permissible maximum value of kappa * (given specified marginals) if $kmax < $D_kappa { di _n in re " " _n di in re " Marginals p1 = $D_p1 and p2 = $D_p2 are incompatible" _n di in re " with the specified kappa ($D_kappa), since the " _n di in re " maximum possible value is $rkmax." _n di in re " Sample size will not be calculated." _n di _n in re " " _n capture clear exit } if $D_kappa < $kmin { di _n in re " " _n di in re " Marginals p1 = $D_p1 and p2 = $D_p2 are incompatible" _n di in re " with the specified kappa ($D_kappa), since the " _n di in re " minimum possible value is $rkmin. " _n di in re " Sample size will not be calculated." _n di _n in re " " _n capture clear exit } if $kmax < $kplusd & $kmin > $kmind { di _n in re " " _n di in re " The specified value of d ($D_d) is incompatible" _n di in re " with the kappa you selected ($D_kappa), since" _n di in re " both confidence limits ($kmind and $kplusd)" _n di in re " exceed the possible boundary values for kappa " _n di in re " ($rkmin and $rkmax, respectively), given the " _n di in re " specified marginals (p1 = $D_p1 and p2 = $D_p2). " _n di in re " Sample size will not be calculated." _n di _n in re " " _n capture clear exit } global Q = ( ( ( (-1 + $D_kappa) * ( (-2*$D_kappa*$D_p1) + /* */ (($D_kappa^2)*$D_p1) + (4*$D_kappa*($D_p1^2)) - /* */ (2*($D_kappa^2)*($D_p1^2)) - (2*$D_kappa*$D_p2) + /* */ (($D_kappa^2)*$D_p2) - (4*$D_p1*$D_p2) + /* */ (8*$D_kappa*$D_p1*$D_p2) - (6*($D_kappa^2)*$D_p1*$D_p2) + /* */ (4*($D_p1^2)*$D_p2) - (12*$D_kappa*($D_p1^2)*$D_p2) + /* */ (8*($D_kappa^2)*($D_p1^2)*$D_p2) + (4*$D_kappa*($D_p2^2)) - /* */ (2*($D_kappa^2)*($D_p2^2)) + (4*$D_p1*($D_p2^2)) - /* */ (12*$D_kappa*$D_p1*($D_p2^2)) + /* */ (8*($D_kappa^2)*$D_p1*($D_p2^2)) - /* */ (4*($D_p1^2)*($D_p2^2)) + /* */ (12*$D_kappa*($D_p1^2)*($D_p2^2)) - /* */ (8*($D_kappa^2)*($D_p1^2)*($D_p2^2))) /* */ ) / ( ($D_p1 + $D_p2 - (2*$D_p1*$D_p2) )^2 ) ) ) global rQ = round($Q,.001) * Calculating sample size global S = ($Q*($z/$D_d)^2) global rS = round($S,1) * Calculating dout from sample(S) if "$D_cb8" == "1" { global dout = sqrt(($Q * $z) / $D_ssize) global rdout = round($dout, .001) } if $kmax < $kplusd { di _n in bl " Note: the specified value of d ($D_d) is " di in bl " partially incompatible with the kappa you" di in bl " selected ($D_kappa), since the upper confidence " di in bl " limit ($kplusd) would be beyond the maximum " di in bl " possible value for kappa ($rkmax) for the " di in bl " specified marginals (p1 = $D_p1 and p2 = $D_p2). " di in bl " Yet, sample size is calculated, since d is " di in bl " compatible with kappa regarding the lower" di in bl " limit." _n } if $kmin > $kmind { di _n in bl " Note: the specified value of d ($D_d) is " di in bl " partially incompatible with the kappa you" di in bl " selected ($D_kappa), since the lower confidence " di in bl " limit ($kmind) would be below the minimum " di in bl " possible value for kappa ($rkmin) for the " di in bl " specified marginals (p1 = $D_p1 and p2 = $D_p2). " di in bl " Yet, sample size is calculated, since d is " di in bl " compatible with kappa regarding the upper" di in bl " limit." _n } if $D_kappa < 0 & $D_kappa > -1 { di _n in bl " Atention! The specified values for the" di in bl " minimum kappa worth finding is less than 0." di in bl " Although negative values down to -1 are" di in bl " possible, it is very unusual to specify a" di in bl " value less than 0." _n } * =========== Stuff for qmax, graph(q) and graph(s) ============ capture clear qui set obs 1000 global k (((_n/1000)/.5)-1) gen k = $k * Putting globals into vars gen p1 = $D_p1 gen p2 = $D_p2 gen d = $D_d gen z = $z gen kmax = $kmax gen kmin = $kmin gen kplusd = $kplusd gen kmind = $kmind * Generating Q. When incompatible situations occur, Q is set to missing * and plots for those will be absent. If all are missing, there will * be no plot at all (only an empty frame) gen Q = ( ( ( (-1 + k) * ( (-2*k*p1) + /* */ ((k^2)*p1) + (4*k*(p1^2)) - /* */ (2*(k^2)*(p1^2)) - (2*k*p2) + /* */ ((k^2)*p2) - (4*p1*p2) + /* */ (8*k*p1*p2) - (6*(k^2)*p1*p2) + /* */ (4*(p1^2)*p2) - (12*k*(p1^2)*p2) + /* */ (8*(k^2)*(p1^2)*p2) + (4*k*(p2^2)) - /* */ (2*(k^2)*(p2^2)) + (4*p1*(p2^2)) - /* */ (12*k*p1*(p2^2)) + /* */ (8*(k^2)*p1*(p2^2)) - /* */ (4*(p1^2)*(p2^2)) + /* */ (12*k*(p1^2)*(p2^2)) - /* */ (8*(k^2)*(p1^2)*(p2^2))) /* */ ) / ( (p1 + p2 - (2*p1*p2) )^2 ) ) ) qui replace Q=. if kmax < k qui replace Q=. if k < kmin qui replace Q=. if kmax < kplusd & kmin > kmind qui egen qmax = max(Q) qui gen rqmax = round(qmax, .001) global rqmax rqmax qui gen Smax = (rqmax*(z/d)^2) qui gen rSmax = round(Smax,1) global rSmax rSmax qui gen S = (Q*(z/d)^2) qui gen rS = round(S,1) qui gen kqmax=. qui replace kqmax=k if Q==qmax & Q~=. qui gen rkqmax=round(kqmax,.001) global rkqmax rkqmax qui sort kqmax qui egen Qbar=mean(Q) * ============ OUTPUTS (sample size, Q, Qmax and dout) ====================== set more 1 di _n in gr " " _n di _n in gr " --- Begin -----------------------------------------------" _n di _n in wh " Results for kappa=$D_kappa, p1=$D_p1, p2=$D_p2," di _n in wh " d=$D_d [$D_level% Conf. Interval]:" _n di _n in gr " * Sample size = " in ye $rS _n if "$D_cb1" == "1" { di _n in gr " * Value of Q = " in ye $rQ _n } if "$D_cb2" == "1" { if Qbar~=. { di _n in wh " " _n di _n in wh " Given the values specified above, the" di _n in wh " maximum sample size is " in ye $rSmax in wh " for a" di _n in wh " kappa of " in ye $rkqmax _n } if Qbar==. { di _n in re " The specified values preclude calculating any Q." di in re " " _n di _n in re " Sample size cannot be calculated." _n exit } } if "$D_cb8" == "1" { di _n in gr " " _n di _n in wh " If the sample size is fixed at " in gr $D_ssize in wh ", " di _n in wh " given the kappa, p1, p2 and CI stated " di _n in wh " above," in gr " d = " in ye $rdout in wh "." _n } di _n in gr " --- End -------------------------------------------------" _n di _n in gr " " _n sort k * =========== Stuff for graph(p) ============================= global newp1 _n/1000 qui gen newp1 = $newp1 qui gen newp2=newp1 qui gen kap=$D_kappa * Evaluating maximum kappa given the marginals for w qui gen newkmax = ((newp2+(1-newp1))-((newp1*newp2)+ /* */ ((1-newp1)*(1-newp2))))/(1-((newp1*newp2)+ /* */ ((1-newp1)*(1-newp2)))) qui replace newkmax=. if newkmax > -1 * Evaluating minimum kappa given a set o specified marginals qui gen newkmin = (((1-newp2)-newp1)-((newp1*newp2)+ /* */ ((1-newp1)*(1-newp2))))/(1-((newp1*newp2)+ /* */ ((1-newp1)*(1-newp2)))) qui replace newkmin=. if newkmin < -1 qui gen nkplusd = kap+d qui gen nkmind = kap-d * Generating Q for w. When incompatible situations occur, Q is set to missing * and plots for those will be absent. If all are missing, there will * be no plot at all (only an empty frame) qui gen newQ = ( ( ( (-1 + kap) * ( (-2*kap*newp1) + /* */ ((kap^2)*newp1) + (4*kap*(newp1^2)) - /* */ (2*(kap^2)*(newp1^2)) - (2*kap*newp2) + /* */ ((kap^2)*newp2) - (4*newp1*newp2) + /* */ (8*kap*newp1*newp2) - (6*(kap^2)*newp1*newp2) + /* */ (4*(newp1^2)*newp2) - (12*kap*(newp1^2)*newp2) + /* */ (8*(kap^2)*(newp1^2)*newp2) + (4*kap*(newp2^2)) - /* */ (2*(kap^2)*(newp2^2)) + (4*newp1*(newp2^2)) - /* */ (12*kap*newp1*(newp2^2)) + /* */ (8*(kap^2)*newp1*(newp2^2)) - /* */ (4*(newp1^2)*(newp2^2)) + /* */ (12*kap*(newp1^2)*(newp2^2)) - /* */ (8*(kap^2)*(newp1^2)*(newp2^2))) /* */ ) / ( (newp1 + newp2 - (2*newp1*newp2) )^2 ) ) ) qui replace newQ=. if newkmax < kap qui replace newQ=. if kap < newkmin qui replace newQ=. if newkmax < nkplusd qui replace newQ=. if newkmin > nkmind qui egen nqmax = max(newQ) qui gen rnqmax = round(nqmax, .001) qui gen nSmax = (rnqmax*(z/d)^2) qui gen rnSmax = round(nSmax,1) qui gen nS = (newQ*(z/d)^2) qui gen rnS = round(nS,1) qui gen nkqmax=. qui replace nkqmax=kap if newQ==nqmax & newQ~=. qui gen rnkqmax=round(nkqmax,.001) qui sort nkqmax qui egen nQbar=mean(newQ) sort newp1 * =========== Stuff for graph(d) ============================= global mult = (($D_tod)/1000) qui gen srange = $mult * _n qui gen Qspec = $Q qui gen drange = sqrt((Qspec * z) / srange) qui gen kplusdr = kap+drange qui gen kmindr = kap-drange qui replace drange=. if kmax < kplusdr & kmin > kmindr * =========== double save globals ============================ qui sum qmax global qmax `r(mean)' qui sum Smax global Smax `r(mean)' qui sum kqmax global kqmax `r(mean)' qui sum Qbar global qbar `r(mean)' global S_1 = $S global S_2 = $Smax global S_3 = $Q global S_4 = $qmax global S_5 = $D_kappa global S_6 = $kmax global S_7 = $kmin global S_8 = $D_p1 global S_9 = $D_p2 global S_10 = $D_d global S_11 = $z end * ======================= GRAPH PROGRAMS =============================== * ====================================================================== * Graph Q program define drgrq version 6.0 capture sskok if "$D_cb4" == "1" { qui sum kqmax local opt "xline(`r(mean)')" } capture label var Q "Value of Q" capture label var k "kappa" gr Q k if (k > $D_frk) & (k < $D_tok), xlab ylab /* */ s(.) c(l) `opt' /* */ t1("Q values according to kappa values (p1=$D_p1, p2=$D_p2)") gap(5) end * Graph S program define drgrs version 6.0 capture sskok if "$D_cb4" == "1" { qui sum kqmax local opt "xline(`r(mean)')" } capture label var S "Sample size" capture label var k "kappa " gr S k if (k > $D_frk) & (k < $D_tok), xlab ylab /* */ s(.) c(l) `opt' /* */ t1("Sample size according to kappa values (p1=$D_p1, p2=$D_p2)") gap(6) end * Graph P program define drgrp version 6.0 capture sskok capture label var rnS "Sample size (log scale)" capture label var newp1 "p1 = p2" gr rnS newp1 if (nS <= 2000) & (newp1 > $D_frp) & (newp1 < $D_top), /* */ ylog xlab ylab s(.) c(l) /* */ t2("Sample size according to p1 (or p2) (kappa set at $D_kappa)") gap(6) end * Graph D program define drgrd version 6.0 capture sskok capture label var srange "Sample size" capture label var drange "Absolute precision" gr drange srange if (srange > $D_frd) & (srange < $D_tod), /* */ xlab ylab s(.) c(l) /* */ t2("Values of d according to sample size (kappa=$D_kappa, p1=$D_p1 and p2=$D_p2)") gap(6) end * ================================ EXIT ================================ * ====================================================================== program define exitok version 6.0 * Dropping or keeping vars for further use and/or saving if "$D_cb3" == "1" { capture rename k k_q_s capture label var k_q_s "Kappas used for Graph Q and S" capture rename Q Q_q_s capture label var Q_q_s "Values of Q used for Graph Q" capture rename rS S_q_s capture label var S_q_s "Sample size values used for Graph S" capture rename newp1 p_p capture label var p_p "Values of positives used for Graph P" capture rename newQ Q_p capture label var Q_p "Values of Q underlying Graph P" capture rename rnS S_p capture label var S_p "Sample size values used for Graph P" capture rename drange d_d capture label var d_d "Values of d used for Graph D" capture rename srange S_d capture label var S_d "Sample size values used for Graph D" order k_q_s Q_q_s S_q_s p_p Q_p S_p d_d S_d capture keep k_q_s Q_q_s S_q_s p_p Q_p S_p d_d S_d } if "$D_cb3" ~= "1" { clear } exit 3000 end