forked from Jiangtang/Programming-SAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCI_Single_Proportion.sas
391 lines (312 loc) · 12.7 KB
/
CI_Single_Proportion.sas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
*******************************************************************************************************************;
*Program Name : CI_Single_Proportion.sas *;
*Programmer Name : Jiangtang Hu *;
* Jiangtanghu@gmail.com *;
* Jiangtanghu.com/blog *;
* *;
*Purpose : Compute two-sided confidence intervals for single proportion with 11 methods: *;
* 1. Simple asymptotic, Without CC | Wald *;
* 2. Simple asymptotic, With CC *;
* 3. Score method, Without CC | Wilson *;
* 4. Score method, With CC *;
* 5. Binomial-based, 'Exact' | Clopper-Pearson *;
* 6. Binomial-based, Mid-p *;
* 7. Likelihood-based *;
* 8. Jeffreys *;
* 9. Agresti-Coull, pseudo frequency, z^2/2 successes| psi = z^2/2 *;
* 10. Agresti-Coull, pseudo frequency, 2 successes and 2 fail| psi = 2 *;
* 11. Agresti-Coull, pseudo frequency, psi = 1 *;
* 12. Agresti-Coull, pseudo frequency, psi = 3 *;
* 13. Logit *;
* 14. Blaker *;
* *;
*Input : r - the number of interested responses *;
* n - total observations, 0 =< r <= n *;
* alpha - 0.05 by default *;
*Output : confidence intervals using 14 methods *;
*Usage : %CI_Single_Proportion(r=81,n=263) *;
* *;
*References : Newcombe R.G., Two-sided confidence intervals for the single proportion: *;
* comparison of seven methods, Statistics in Medicine, (1998) 17, 857-872 *;
* *;
* http://www2.jura.uni-hamburg.de/instkrim/kriminologie/Mitarbeiter/Enzmann/Software/prop.CI.r *;
* *;
*License : public domain, ABSOLUTELY NO WARRANTY *;
*Platform : tested in SAS/Base 9.4 (TS1M2) *;
*Version : V1.0 *;
*Date : 21May2015 *;
*******************************************************************************************************************;
%macro CI_Single_Proportion(r=,n=,alpha=0.05);
proc fcmp outlib=work.func.CI;
function acceptbin(r, n, p) label = "computes the Blaker acceptability of p when x is observed and X is bin(n, p)";
p1 = 1 - CDF('BINOMIAL', r - 1,p,n);
p2 = CDF('BINOMIAL', r,p,n);
a1 = p1 + CDF('BINOMIAL', quantile('BINOM', p1, p, n)-1, p, n) ;
a2 = p2 + 1 - CDF('BINOMIAL', quantile('BINOM', 1-p2, p, n), p, n) ;
return (min(a1,a2));
endsub;
run;
options cmplib=work.func;
data param;
do i=1 to 14;
r = &r;
n = &n;
alpha = α
p = r/n;
q = 1 - p;
z = probit (1-alpha/2);
output;
end;
run;
/*method 1-5,8-14;*/
data CI5;
length method $75.;
set param(where=(i not in (6 7)));
if i=1 then do;
Method = "1. Simple asymptotic, Without CC | Wald";
se = (sqrt(&n*p*(1-p)))/n; *standard error;
p_CI_low = p - z * se;
p_CI_up = p + z * se;
end;
if i=2 then do;
Method = "2. Simple asymptotic, With CC";
se = (sqrt(&n*p*(1-p)))/n; *standard error;
cc = 1/(2*&n); *continuity correction;
p_CI_low = p - (z * se + cc);
p_CI_up = p + (z * se + cc);
/* if r=0 then p_CI_low=0;*/
/* if r=n then p_CI_up =1;*/
end;
if i=3 then do;
Method = "3. Score method, Without CC | Wilson";
*n1=2*r+z**2;
*n2=z*sqrt(z**2+4*r*q);
*d=2*(n+z**2);
*p_CI_low = (n1 - n2)/d;
*p_CI_up = (n1 + n2)/d;
p_CI_low = ( 2*r+z**2 - (z*sqrt(z**2+4*r*q)) ) / (2*(n+z**2));
p_CI_up = ( 2*r+z**2 + (z*sqrt(z**2+4*r*q)) ) / (2*(n+z**2));
end;
if i=4 then do;
Method = "4. Score method, With CC";
*n1=2*r+z**2;
*n12=z*sqrt(z**2 - 2- 1/n + 4*p*(n*q+1));
*n22=z*sqrt(z**2 + 2- 1/n + 4*p*(n*q-1));
*d=2*(n+z**2);
*p_CI_low = ( n1 -1 - n12) / d;
*p_CI_up = ( n1 +1 + n22) / d;
p_CI_low = ( 2*r+z**2 -1 - z*sqrt(z**2 - 2- 1/n + 4*p*(n*q+1))) / (2*(n+z**2));
p_CI_up = ( 2*r+z**2 +1 + z*sqrt(z**2 + 2- 1/n + 4*p*(n*q-1))) / (2*(n+z**2));
/* if r=0 then p_CI_low=0;*/
/* if r=n then p_CI_up =1; */
end;
if i=5 then do;
Method = "5. Binomial-based, 'Exact' | Clopper-Pearson";
p_CI_low =1 - betainv(1 - alpha/2,n-r+1,r);
p_CI_up = betainv(1 - alpha/2,r+1 ,n-r);
/* if r=0 then p_CI_low=0;*/
/* if r=n then p_CI_up =1;*/
end;
if i=8 then do;
Method = "8. Jeffreys";
p_CI_low = betainv( alpha/2, r+0.5,n-r+0.5);
p_CI_up = betainv(1-alpha/2, r+0.5,n-r+0.5);
end;
if i=9 then do;
Method = "9. Agresti-Coull, pseudo frequency, z^2/2 successes| psi = z^2/2";
psi = z**2/2;
p2=(r+psi)/(n+2*psi);
p_CI_low =p2 - z*(sqrt(p2*(1-p2)/(n+2*psi)));
p_CI_up =p2 + z*(sqrt(p2*(1-p2)/(n+2*psi)));
if p_CI_low<0 then p_CI_low=0;
if p_CI_up>1 then p_CI_up =1;
end;
if i=10 then do;
Method = "10. Agresti-Coull, pseudo frequency, 2 successes and 2 failures| psi = 2";
psi = 2;
p2=(r+psi)/(n+2*psi);
p_CI_low =p2 - z*(sqrt(p2*(1-p2)/(n+2*psi)));
p_CI_up =p2 + z*(sqrt(p2*(1-p2)/(n+2*psi)));
if p_CI_low<0 then p_CI_low=0;
if p_CI_up>1 then p_CI_up =1;
end;
if i=11 then do;
Method = "11. Agresti-Coull, pseudo frequency, psi = 1";
psi = 1;
p2=(r+psi)/(n+2*psi);
p_CI_low =p2 - z*(sqrt(p2*(1-p2)/(n+2*psi)));
p_CI_up =p2 + z*(sqrt(p2*(1-p2)/(n+2*psi)));
if p_CI_low<0 then p_CI_low=0;
if p_CI_up>1 then p_CI_up =1;
end;
if i=12 then do;
Method = "12. Agresti-Coull, pseudo frequency, psi = 3";
psi = 3;
p2=(r+psi)/(n+2*psi);
p_CI_low =p2 - z*(sqrt(p2*(1-p2)/(n+2*psi)));
p_CI_up =p2 + z*(sqrt(p2*(1-p2)/(n+2*psi)));
if p_CI_low<0 then p_CI_low=0;
if p_CI_up>1 then p_CI_up =1;
end;
if i=13 then do;
Method = "13. Logit";
p_CI_low=exp(log(p/(1-p)) - z*sqrt(n/(r*(n-r))))/(1+exp(log(p/(1-p)) - z*sqrt(n/(r*(n-r)))));
p_CI_up =exp(log(p/(1-p)) + z*sqrt(n/(r*(n-r))))/(1+exp(log(p/(1-p)) + z*sqrt(n/(r*(n-r)))));
end;
if i=14 then do;
Method = "14. Blaker";
tolerance=1e-05;
lower = 0;
upper = 1;
if r ^= 0 then do;
lower = quantile('BETA',alpha/2, r, n-r+1);
do while (acceptbin(r, n, lower + tolerance) < (alpha));
lower = lower + tolerance;
end;
end;
if r ^= n then do;
upper = quantile('BETA',1 - alpha/2, r+1, n-r);
do while (acceptbin(r, n, upper - tolerance) < (alpha));
upper = upper - tolerance;
end;
end;
p_CI_low=lower;
p_CI_up =upper;
end;
run;
/*method 6;*/
data param6;
set param(where=(i=6));
max_idx=alpha/2;
min_idx=1-alpha/2;
do j=0.000001 to 0.999999 by 0.00001;
if (r>0 and r<n) then a2=0.5*probbnml(j,n,r-1) + 0.5*probbnml(j,n,r);
output;
end;
run;
proc sql;
create table max as
select max(j) as p_CI_up
from param6
where a2 > max_idx and r>0 and r<n
;
create table min as
select min(j) as p_CI_low
from param6
where a2 <= min_idx and r>0 and r<n
;
create table param6_2 as
select *
from param
where i=6
;
quit;
data CI6;
merge param6_2 min max;
Method = "6. Binomial-based, Mid-p";
if r=0 then do;
p_CI_low=0;
p_CI_up = 1-alpha**(1/n);
end;
if r=n then do;
p_CI_low=alpha**(1/n);
p_CI_up = 1;
end;
run;
/*method 7;*/
data param7;
set param(where=(i=7));
k=-cinv(1-alpha,1)/2;
do j=0.000001 to 0.999999 by 0.00001;
lik=pdf('Binomial',r,j,n);
output;
end;
run;
proc sql;
create table max as
select i,max(lik) as max
from param7
;
quit;
data test1;
merge param7 max;
by i;
if lik ^= 0 then
logLR = log(lik/max);
run;
proc sql;
create table max2 as
select min(j) as p_CI_low,max(j) as p_CI_up
from test1
where logLR>k
;
create table param7_2 as
select distinct *
from param
where i=7
;
quit;
data CI7;
merge param7_2 max2;
Method = "7. Likelihood-based";
if r=0 then p_CI_low=0;
if r=n then p_CI_up =1;
run;
/*put together,1-12;*/
data CI_SP;
set CI5 CI6 CI7;
p_CI=compress(catx("","[",put(round(p_CI_low,0.0001),6.4),",",put(round(p_CI_up,0.0001),6.4),"]"));
run;
proc sort; by i;run;
proc print data=CI_SP;
var r n p method p_ci;
run;
%mend CI_Single_Proportion;
/*test;
filename CI url 'https://raw.github.com/Jiangtang/Programming-SAS/master/CI_Single_Proportion.sas';
%include CI;
%CI_Single_Proportion(r=81,n=263);
%CI_Single_Proportion(r=15,n=148);
%CI_Single_Proportion(r=0, n=20 );
%CI_Single_Proportion(r=1, n=29 );
%CI_Single_Proportion(r=29,n=29 );
check with SAS
data test;
input grp outcome $ count;
datalines;
1 f 81
1 u 182
;
proc freq data=test;
tables outcome / binomial;
weight Count;
run;
ods select BinomialCLs;
proc freq data=test;
tables outcome / binomial (CL=ALL);
weight Count;
run;
ods select BinomialCLs;
proc freq data=test;
tables outcome / binomial (CL=
WALD
WILSON
CLOPPERPEARSON
MIDP
LIKELIHOODRATIO
JEFFREYS
AGRESTICOULL
LOGIT
BLAKER
);
weight Count;
run;
ods select BinomialCLs;
proc freq data=test;
tables outcome / binomial (CL =
WILSON(CORRECT)
WALD(CORRECT)
);
weight Count;
run;
*/