Binomial Test on Coin Flips and Clinical Data
Simulating Coin Flips
Set the seed for reproducibility and simulate 1000 coin flips using a Bernoulli distribution.
/* Set the seed for reproducibility */
%let seed = 19;
data coin_flips;
call streaminit(&seed);
do i = 1 to 1000;
/* Simulate coin flips: 1 for Heads (H), 0 for Tails (T) */
flip = rand("Bernoulli", 0.5);
/* flip = rand("BINOMIAL", 0.5,1); */
if flip = 1 then result = "H";
else result = "T";
output;
end;
run;Counting Heads and Tails
Use SQL to count how many heads and tails were observed in the simulation.
proc sql;
select
sum(result = "H") as heads_count,
sum(result = "T") as tails_count,
count(*) as total_flips
into :heads_count, :tails_count, :total_flips
from coin_flips;
quit;Display the Results
Print the counts using %put statements.
%put Heads Count: &heads_count;
%put Tails Count: &tails_count;
%put Total Flips: &total_flips;Perform four binomial tests (Exact test, Wald test, Mid-p adjusted test and Wilson score test).
Use proc freq to check if the observed results differ significantly from the expected probability of 0.5.
\(H_0 : p = 0.5\)
1. Exact Binomial Test
proc freq data=coin_flips;
tables result / binomial(p=0.5 );
exact binomial;
run;2. Wald Binomial Test(Asymptotic)
proc freq data=coin_flips;
tables result / binomial(p=0.5);
run;3. Mid-p adjusted Test
proc freq data=coin_flips;
tables result / binomial(p=0.5 level='H' cl=midp );
exact binomial / midp;
run;4. Wilson Score Test
proc freq data=coin_flips;
tables result / binomial(level='H' p=0.5 cl=score);
run;Example: Binomial Test in Clinical Trial Data
We load a clinical dataset and test if the observed death proportion is significantly different from a hypothesized value (e.g., 19%).
Import Dataset
proc import datafile='/home/u63532805/CAMIS/lung_cancer.csv'
out=lung_cancer
dbms=csv
replace;
getnames=yes;
guessingrows=max;
run;Create Binary Flag for Deaths
data lung_cancer;
set lung_cancer;
death_flag = (status = 1);
run;Perform four binomial tests (Exact test, Wald test, Mid-p adjusted test and Wilson score test).
1. Exact Binomial Test
proc freq data=lung_cancer;
tables death_flag / binomial(p=0.19 level='1');
exact binomial;
title "Exact Binomial Test for Death Proportion";
run;2. Wald Binomial Test (Asymptotic)
proc freq data=lung_cancer;
tables death_flag / binomial(p=0.19 level='1');
title "Asymptotic Binomial Test for Death Proportion";
run;3. Mid-p adjusted Exact Binomial Test
proc freq data=lung_cancer;
tables death_flag / binomial(p=0.19 level='1');
exact binomial / midp;
title "Exact mid-P Binomial Test for Death Proportion";
run;4. Wilson Score Test.
proc freq data=lung_cancer;
tables death_flag / binomial(level='1' p=0.19 cl=score);
title "Wilson Score Test for Death Proportion";
run;SAS Output
Coin Flip Summary
| heads_count | tails_count | total_flips |
|---|---|---|
| 520 | 480 | 1000 |
Binomial Test on Coin Flips
The FREQ Procedure
| result | Frequency | Percent | Cumulative Frequency | Cumulative Percent |
|---|---|---|---|---|
| H | 520 | 52.00 | 520 | 52.00 |
| T | 480 | 48.00 | 1000 | 100.00 |
Exact Binomial Test result= H
- Proportion: 0.5200
- 95% Lower Conf Limit: 0.4885
- 95% Upper Conf Limit: 0.5514
- Two sided p_value : 0.2174
Wald(Asymptotic) Binomial Test for result = H
- Proportion: 0.5200
- ASE: 0.0158
- 95% Lower Conf Limit: 0.4890
- 95% Upper Conf Limit: 0.5510
- Two sided p_value : 0.2059
Mid-p adjusted Binomial Test for result = H
- Proportion: 0.5200
- 95% Lower Conf Limit: 0.4890
- 95% Upper Conf Limit: 0.5509
- Right sided p_value : 0.1031
Wilson Score Test for result = H
- Proportion: 0.5200
- ASE: 0.0158
- 95% Lower Conf Limit: 0.4890
- 95% Upper Conf Limit: 0.5508
- Two sided p_value : 0.2059
Binomial Test for Death Proportion
The FREQ Procedure
| death_flag | Frequency | Percent | Cumulative Frequency | Cumulative Percent |
|---|---|---|---|---|
| 0 | 165 | 72.37 | 165 | 72.37 |
| 1 | 63 | 27.63 | 228 | 100.00 |
Exact Binomial Test result= H
- Proportion: 0.2763
- 95% Lower Conf Limit: 0.2193
- 95% Upper Conf Limit: 0.3392
- Two sided p_value : 0.0019
Wald(Asymptotic) Binomial Test for result = H
- Proportion: 0.2763
- ASE: 0.0296
- 95% Lower Conf Limit: 0.2183
- 95% Upper Conf Limit: 0.3344
- Two sided p_value : 0.0009
Mid-p adjusted Binomial Test for result = H
- Proportion: 0.2763
- 95% Lower Conf Limit: 0.2212
- 95% Upper Conf Limit: 0.3371
- Right sided p_value : 0.0008
Wilson Score Test for result = H
- Proportion: 0.2763
- ASE: 0.0296
- 95% Lower Conf Limit: 0.2223
- 95% Upper Conf Limit: 0.3377
- Two sided p_value : 0.0009
More detailed information around CIs for proportions can be found here