diff --git a/Comp/r-sas_binomial_test.qmd b/Comp/r-sas_binomial_test.qmd new file mode 100644 index 000000000..85a5fdb88 --- /dev/null +++ b/Comp/r-sas_binomial_test.qmd @@ -0,0 +1,142 @@ +--- +title: "r-sas_binomial" +--- + +```{r} +#| label: setup +#| include: false +knitr::opts_chunk$set(echo = TRUE) +library(procs) +``` + +# Binomial Test Comparison. + +## One Sample Proportion + +The following table shows the types of Binomial test analysis, the capabilities of each language, and whether or not the results from each language match. + ++-----------------------------------------------------+----------------------+--------------------+-----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| **Analysis** | **Supported in SAS** | **Supported in R** | **Match** | **Notes** | ++-----------------------------------------------------+----------------------+--------------------+-----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| Exact Binomial Test | Yes | Yes | Yes | Performed in Base R, using `binom.test()` function which executes the exact test of a single proportion based on exact binomial distribution. SAS uses `PROC FREQ` with binomial option and level to define category of success variable. | ++-----------------------------------------------------+----------------------+--------------------+-----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| Asymptotic Binomial Test (Wald test for proportion) | Yes | Yes | Yes | There is no base R function (library(help="stats")) for one‑sample binomial proportion. The Wald test can be done manually based on z statistic formula; `BinomCI()` gives the confidence interval but does not perform the formal hypothesis test. In SAS, it is implemented by default. To implement continuity correction, apply correct syntax and use `level` to define category of success variable. | ++-----------------------------------------------------+----------------------+--------------------+-----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| Mid-P adjusted Exact Binomial Test | Yes | Yes | Yes | Not implemented by base R. Apply R package `exactci` for one sample proportion. SAS applies `PROC FREQ` with `EXACT BINOMIAL / MIDP` option. Mid-P binomial test is less conservative than the exact binomial test. | ++-----------------------------------------------------+----------------------+--------------------+-----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| Wilson score test | Yes | Yes | Yes | Implemented in base R by applying the inbuilt function `prop.test()` which can perform both one and two samples z-tests of proportions. In SAS, it is implemented using `PROC FREQ` with a binomial option and `CL=SCORE` for confidence interval. | ++-----------------------------------------------------+----------------------+--------------------+-----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ + +# Comparison Results + +Here is a table of comparison values between `binom.test()` and SAS `PROC FREQ` with binomial option: + +1. **Binomial Test on coin flips.** + + $H_0 : p = 0.5$ + ++---------------------------------------+------------------------+----------------------+--------------------------------+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| Test | Statistic | Binom.test( ) | PROC FREQ with Binomial option | Match | Notes | ++=======================================+========================+======================+================================+=======+==========================================================================================================================================================================================================================================================================================================================================+ +| Exact Binomial Test (Clopper-Pearson) | Probability of success | 0.52 | 0.52 | Yes | Binomial distributiion | ++---------------------------------------+------------------------+----------------------+--------------------------------+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| | Confidence Interval | Lower: 0.4885 | Lower: 0.4885 | Yes | Constructed by inverting exact binomial test, with interval bounds obtained from beta distribution quantile. | +| | | | | | | +| | | Upper: 0.5513 | Upper: 0.5514 | | | ++---------------------------------------+------------------------+----------------------+--------------------------------+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| | p- value | 0.2174 | 0.2174 | Yes | Two tailed p_value. R generates two tailed test by default. SAS generates both two sided and right sided p-value. | ++---------------------------------------+------------------------+----------------------+--------------------------------+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| Asymptotic Binomial Test( Wald test) | Probability of success | 0.52 | 0.52 | Yes | | ++---------------------------------------+------------------------+----------------------+--------------------------------+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| | Confidence interval | Lower: 0.4890 | Lower: 0.4890 | Yes | Use normal approximation to the binomial distribution for confidence intervals and hypothesis tests. | +| | | | | | | +| | | Upper: 0.5510 | Upper: 0.5510 | | | ++---------------------------------------+------------------------+----------------------+--------------------------------+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| | p- value | 0.2059 | 0.2059 | Yes | Two sided p_value. Since the Wald test is done manually in R based on z statistic formula, p_value can be calculated for either tail tests. SAS generates both two sided and right sided p-value. | ++---------------------------------------+------------------------+----------------------+--------------------------------+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| Mid-P adjusted Exact Binomial Test | Probability of success | 0.52 | 0.52 | Yes | Binomial distribution | ++---------------------------------------+------------------------+----------------------+--------------------------------+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| | Confidence interval | Lower: 0.4890 | Lower: 0.4890 | Yes | | +| | | | | | | +| | | Upper: 0.5509 | Upper: 0.5509 | | | ++---------------------------------------+------------------------+----------------------+--------------------------------+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| | p-value | Right sided : 0.1031 | Exact one sided: 0.1031 | Yes | SAS default is right sided p-value. R default is two sided p-value. A Request of a right sided test p-value in R matches that of SAS. This because both software apply same midpoint definition. | +| | | | | | | +| | | Two sided: 0.2061 | | | | +| | | | | | | +| | | Left sided: 0.8969 | | | | ++---------------------------------------+------------------------+----------------------+--------------------------------+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| Wilson Score Test | Probability of success | 0.52 | 0.52 | Yes | | ++---------------------------------------+------------------------+----------------------+--------------------------------+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| | Confidence interval | Lower: 0.4890 | Lower: 0.4890 | Yes | Obtained by inverting the score (Pearson chi-square) test for a single binomial proportion. It's formed by a set of values that are not rejected by Pearson's chi-square score test. It is applicable for extreme data, for instance, when the number of successes (x)=0 which is unlikely for Wald interval since it collapses \[0,0\]. | +| | | | | | | +| | | Upper: 0.5508 | Upper: 0.5508 | | | ++---------------------------------------+------------------------+----------------------+--------------------------------+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| | p-value | 0.2059 | 0.2059 | Yes | Two sided p-value. R generates two tailed test by default. SAS generates both two sided and right sided p-value. | ++---------------------------------------+------------------------+----------------------+--------------------------------+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ + +2. **Binomial Test with Clinical Trial Data.** + +$H_0 : p = 0.19$ + ++----------------------------------------+------------------------+--------------------------+--------------------------------+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| Test | Statistic | Binom.test( ) | PROC FREQ with binomial option | Match | Notes | ++========================================+========================+==========================+================================+=======+==========================================================================================================================================================================================================================================================================================================================================+ +| Exact Binomial Test ( Clopper-Pearson) | Probability of success | 0.2763 | 0.2763 | Yes | Binomial distribution | ++----------------------------------------+------------------------+--------------------------+--------------------------------+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| | Confidence interval | Lower:0.2193 | Lower:0.2193 | Yes | Constructed by inverting exact binomial test, with interval bounds obtained from beta distribution quantile. | +| | | | | | | +| | | Upper:0.3392 | Upper:0.3392 | | | ++----------------------------------------+------------------------+--------------------------+--------------------------------+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| | p-value | 0.0017 | 0.0019 | Yes | Two tailed p-value. R generates two tailed test by default. SAS generates both two sided and right sided p-value. | ++----------------------------------------+------------------------+--------------------------+--------------------------------+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| Asymptotic Binomial Test (Wald Test) | Probability of success | 0.2763 | 0.2763 | Yes | | ++----------------------------------------+------------------------+--------------------------+--------------------------------+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| | Confidence interval | Lower: 0.2183 | Lower: 0.2183 | Yes | Use normal approximation to the binomial distribution for confidence intervals and hypothesis tests. | +| | | | | | | +| | | Upper: 0.3344 | Upper: 0.3344 | | | ++----------------------------------------+------------------------+--------------------------+--------------------------------+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| | p-value | 0.0009 | 0.0009 | Yes | Two sided p_value. Since the Wald test is done manually in R based on z statistic formula, p_value can be calculated for either tail tests. SAS generates both two sided and right sided p-value. | ++----------------------------------------+------------------------+--------------------------+--------------------------------+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| Mid-P adjusted Exact Binomial Test | Probability of success | 0.2763 | 0.2763 | Yes | Binomial distribution | ++----------------------------------------+------------------------+--------------------------+--------------------------------+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| | Confidence interval | Lower: 0.2212 | Lower: 0.2212 | Yes | | +| | | | | | | +| | | Upper: 0.3371 | Upper: 0.3371 | | | ++----------------------------------------+------------------------+--------------------------+--------------------------------+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| | p-value | one-tailed Upper: 0.0008 | exact one sided: 0.0008 | Yes | SAS default is right sided p-value. R default is two sided p-value. A Request of a right sided test p-value in R matches that of SAS. This because both software apply same midpoint definition. | +| | | | | | | +| | | Two tailed: 0.0015 | | | | +| | | | | | | +| | | one-tailed Lower: 0.9992 | | | | ++----------------------------------------+------------------------+--------------------------+--------------------------------+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| Wilson Score Test | Probability of success | 0.2763 | 0.2763 | Yes | | ++----------------------------------------+------------------------+--------------------------+--------------------------------+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| | Confidence interval | Lower: 0.2223 | Lower: 0.2223 | Yes | Obtained by inverting the score (Pearson chi-square) test for a single binomial proportion. It's formed by a set of values that are not rejected by Pearson's chi-square score test. It is applicable for extreme data, for instance, when the number of successes (x)=0 which is unlikely for Wald interval since it collapses \[0,0\]. | +| | | | | | | +| | | Upper: 0.3377 | Upper: 0.3377 | | | ++----------------------------------------+------------------------+--------------------------+--------------------------------+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| | p-value | 0.0009 | 0.0009 | Yes | Two sided p-value. R generates two tailed test by default. SAS generates both two sided and right sided p-value. | ++----------------------------------------+------------------------+--------------------------+--------------------------------+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ + +# Summary and Recommendation + +Test results for the two example data are identical in both R and SAS for every instance. For the Exact Binomial test in both software, `binom.test()` and the `PROC FREQ` procedure with Binomial option offer Clopper-Pearson confidence intervals. Default is two sided alternative. Exact test is based on binomial distribution. + +Asymptotic Binomial Test uses normal approximation to the binomial distribution for confidence intervals and hypothesis tests, which is suitable for large samples. Default tests are typically two-sided in both R and SAS. SAS use normal approximation for binomial proportion test in `PROC FREQ`. Since asymptotic method assumes large samples, it is therefore not reliable for small samples or for proportions closer to 0 or 1. The confidence interval can also go beyond \[0,1\]. + +For one sample case in R , Mid-P adjusted Exact Binomial Test implements `tsmethod = ' central'` by default which gives Garwood(1936) exact central intervals. They are obtained by inverting the mid-p value function. `PROC FREQ` provides exact mid-p-values if you specify `MIDP` option in the `EXACT` statement `CL=MIDP` for confidence interval. + +`prop.test()` by default applies Yates continuity correction (for this case, it was not applied: `correction=FALSE`). Wilson interval corresponds to Pearson's chi-square test. If Yates' continuity correction is implemented to the chi-square test, the resultant confidence interval is that of continuity corrected Wilson interval. It works well for small number of trials(n) and probability of success(p) as well as offering better coverage. + +# References + +`binom.test()` documentation: + +`Package 'exactci'` documentation: + +`PROC FREQ with binomial option` documentation: + +`PROC FREQ with EXACT statement` documentation: + +`Binomial proportion confidence interval` documentation: diff --git a/R/binomial_test.qmd b/R/binomial_test.qmd index a4edcc4a7..e3da858f8 100644 --- a/R/binomial_test.qmd +++ b/R/binomial_test.qmd @@ -4,48 +4,83 @@ title: "Binomial Test" The statistical test used to determine whether the proportion in a binary outcome experiment is equal to a specific value. It is appropriate when we have a small sample size and want to test the success probability $p$ against a hypothesized value $p_0$. -## Creating a sample dataset +# Coin flips dataset. -- We will generate a dataset where we record the outcomes of 1000 coin flips. +- We will use coin flips dataset generated from SAS simulation to carry out four binomial tests (Exact test, Wald test, Mid-p adjusted test and Wilson score test). This is to ensure that the proportion value obtained from coin flips dataset is similar for both software rather than simulating in both instances, which leads to different proportion value. -- We will use the `binom.test` function to test if the proportion of heads is significantly different from 0.5. +- We will use the various functions for each test to investigate if the proportion of heads is significantly different from 0.5. Therefore: -```{r} -set.seed(19) -coin_flips <- sample(c("H", "T"), size = 1000, replace = T, prob = c(0.5, 0.5)) -``` - -Now, we will count the heads and tails and summarize the data. +$H_0 : p = 0.5$ ```{r} # heads -heads_count <- sum(coin_flips == "H") +heads_count <- 520 heads_count # tails -tails_count <- sum(coin_flips == "T") +tails_count <- 480 tails_count # total -total_flips <- length(coin_flips) +total_flips <- 1000 total_flips ``` -## Conducting Binomial Test +## 1. Exact Binomial Test. ```{r} -binom_test_result <- stats::binom.test(heads_count, total_flips, p = 0.5) -binom_test_result +binom.test(heads_count, total_flips, p = 0.5, conf.level = 0.95) ``` ### Results: -The output has a p-value `r binom_test_result$p.value` $> 0.05$ (chosen level of significance). Hence, we fail to reject the null hypothesis and conclude that the **coin is fair**. +The output has a p-value $> 0.05$ (chosen level of significance). Hence, we fail to reject the null hypothesis and conclude that the **coin is fair**. + +## 2. Wald(Asymptotic) Test. + +```{r} +library(DescTools) +p=0.5 +phat<-heads_count/total_flips +phat +z <- (phat - p) / sqrt(p * (1 - p) / total_flips) +2 * (1 - pnorm(abs(z))) +BinomCI(heads_count, total_flips, method = "wald") +``` -# Example of Clinical Trial Data +## Results: + +The output has a p-value $> 0.05$ (chosen level of significance). Hence, we fail to reject the null hypothesis and conclude that the **coin is fair**. + +## 3. Mid-P adjusted Exact Binomial Test. + +```{r} +library(exactci) +binom.exact(heads_count,total_flips, p = 0.5,alternative = "greater", midp = TRUE,tsmethod = "central") +binom.exact(heads_count,total_flips, p = 0.5,alternative = "less", midp = TRUE,tsmethod = "central") +binom.exact(heads_count,total_flips, p = 0.5, midp = TRUE,tsmethod = "central") +``` + +## Results: + +The output has a p-value $> 0.05$ (chosen level of significance). Hence, we fail to reject the null hypothesis and conclude that the **coin is fair**. + +## 3. Wilson Score Test. + +```{r} +prop.test(heads_count, total_flips, p = 0.5, correct = FALSE) +``` + +## Results: + +The output has a p-value $> 0.05$ (chosen level of significance). Hence, we fail to reject the null hypothesis and conclude that the **coin is fair**. + +# Example of Clinical Trial Data. We load the `lung` dataset from `survival` package. We want to test if the proportion of patients with survival status 1 (dead) is significantly different from a hypothesized proportion (e.g. 50%) +$H_0 : p = 0.19$ + We will calculate number of deaths and total number of patients. ```{r} @@ -61,15 +96,67 @@ total_pat <- nrow(lung) total_pat ``` -## Conduct the Binomial Test +## 1. Exact Binomial Test. -We will conduct the Binomial test and hypothesize that the proportion of death should be 19%. +```{r} +# Exact (Clopper–Pearson) +binom.test(num_deaths, total_pat, p = 0.19, conf.level = 0.95) +``` + +## Results: + +The output has a p-value $< 0.05$ (chosen level of significance). Hence, we reject the null hypothesis and conclude that **the proportion of death is significantly different from 19%**. + +## 2. Wald(Asymptotic) Test + +```{r} +library(DescTools) +p=0.19 +phat<-num_deaths/total_pat +z <- (phat - p) / sqrt(p * (1 - p) / total_pat) + 2 * (1 - pnorm(abs(z))) +BinomCI(num_deaths, total_pat, method = "wald") +``` + +## Results: + +The output has a p-value $< 0.05$ (chosen level of significance). Hence, we reject the null hypothesis and conclude that **the proportion of death is significantly different from 19%**. + +## 3. Mid-P adjusted Exact Binomial Test. + +```{r} +library(exactci) +binom.exact(num_deaths, total_pat, p = 0.19, midp = TRUE,tsmethod = "central") +binom.exact(num_deaths, total_pat, p = 0.19,alternative = "less", midp = TRUE,tsmethod = "central") +binom.exact(num_deaths, total_pat, p = 0.19,alternative = "greater", midp = TRUE,tsmethod = "central") +``` + +## Results: + +The output for right sided and two sided test has a p-value $< 0.05$ (chosen level of significance). Hence, we reject the null hypothesis and conclude that **the proportion of death is significantly different from 19%**. + +## 4. Wilson Score Test ```{r} -binom_test <- stats::binom.test(num_deaths, total_pat, p = 0.19) -binom_test +prop.test(num_deaths, total_pat, p = 0.19, correct = FALSE) ``` ## Results: -The output has a p-value `r binom_test$p.value` $< 0.05$ (chosen level of significance). Hence, we reject the null hypothesis and conclude that **the proportion of death is significantly different from 19%**. +The output has a p-value $< 0.05$ (chosen level of significance). Hence, we reject the null hypothesis and conclude that **the proportion of death is significantly different from 19%**. + +## Summary: + +| Data | Test | P_Value | +|----------------|---------------------------|---------| +| Coin Flips | Exact Test | 0.2174 | +| | Wald Test | 0.2059 | +| | Mid-p adjusted Exact Test | 0.2061 | +| | Wilson score Test | 0.2059 | +| Clinical Trial | Exact Test | 0.0017 | +| | Wald Test | 0.0009 | +| | Mid-p adjusted Exact Test | 0.0015 | +| | Wilson Score Test | 0.0009 | + +For the two datasets, the results for Wald and Wilson Score test match. This implies that the sample data are adequate because Wald and Wilson differ mainly when sample size is small or probability of success is close to \[0,1\]. In that case Wilson score test will have better coverage. + diff --git a/SAS/binomial_test.qmd b/SAS/binomial_test.qmd index 5c4dae584..8f9a78d4d 100644 --- a/SAS/binomial_test.qmd +++ b/SAS/binomial_test.qmd @@ -1,6 +1,6 @@ --- title: "Binomial Test on Coin Flips and Clinical Data" -execute: +execute: eval: false --- @@ -50,16 +50,46 @@ Print the counts using `%put` statements. %put Total Flips: &total_flips; ``` -## Perform Asymptotic Binomial Test on Coin Flip Results +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 + +```{sas} +proc freq data=coin_flips; + tables result / binomial(p=0.5 ); + exact binomial; +run; +``` + +## 2. Wald Binomial Test(Asymptotic) + ```{sas} proc freq data=coin_flips; tables result / binomial(p=0.5); run; ``` +## 3. Mid-p adjusted Test + +```{sas} +proc freq data=coin_flips; + tables result / binomial(p=0.5 level='H' cl=midp ); + exact binomial / midp; +run; +``` + +## 4. Wilson Score Test + +```{sas} +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%). @@ -85,30 +115,30 @@ data lung_cancer; run; ``` -### Perform Asymptotic Binomial Test +Perform four binomial tests (Exact test, Wald test, Mid-p adjusted test and Wilson score test). + +## 1. Exact Binomial Test ```{sas} -#| eval: false proc freq data=lung_cancer; tables death_flag / binomial(p=0.19 level='1'); - title "Asymptotic Binomial Test for Death Proportion"; + exact binomial; + title "Exact Binomial Test for Death Proportion"; run; ``` -### Perform Exact Binomial Test +## 2. Wald Binomial Test (Asymptotic) ```{sas} proc freq data=lung_cancer; tables death_flag / binomial(p=0.19 level='1'); - exact binomial; - title "Exact Binomial Test for Death Proportion"; + title "Asymptotic Binomial Test for Death Proportion"; run; ``` -### Perform mid-P adjusted Exact Binomial Test +## 3. Mid-p adjusted Exact Binomial Test ```{sas} -#| eval: false proc freq data=lung_cancer; tables death_flag / binomial(p=0.19 level='1'); exact binomial / midp; @@ -116,6 +146,15 @@ proc freq data=lung_cancer; run; ``` +## 4. Wilson Score Test. + +```{sas} +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 @@ -133,27 +172,37 @@ run; | H | 520 | 52.00 | 520 | 52.00 | | T | 480 | 48.00 | 1000 | 100.00 | -**Binomial Proportion for result = H** +**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 -**Exact Confidence Limits** +**Mid-p adjusted Binomial Test for result = H**\ -- 95% Lower Conf Limit: 0.4885\ -- 95% Upper Conf Limit: 0.5514 +- Proportion: 0.5200\ +- 95% Lower Conf Limit: 0.4890\ +- 95% Upper Conf Limit: 0.5509\ +- Right sided p_value : 0.1031\ -**Test of H0: Proportion = 0.5** +**Wilson Score Test for result = H**\ -- ASE under H0: 0.0158\ -- Z: 1.2649\ -- One-sided Pr \> Z: 0.1030\ -- Two-sided Pr \> \|Z\|: 0.2059\ -- Sample Size: 1000 +- Proportion: 0.5200\ +- ASE: 0.0158\ +- 95% Lower Conf Limit: 0.4890\ +- 95% Upper Conf Limit: 0.5508\ +- Two sided p_value : 0.2059 -### Asymptotic Binomial Test for Death Proportion +### Binomial Test for Death Proportion **The FREQ Procedure** @@ -162,22 +211,32 @@ run; | 0 | 165 | 72.37 | 165 | 72.37 | | 1 | 63 | 27.63 | 228 | 100.00 | -**Binomial Proportion for death_flag = 1** +**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 +- 95% Upper Conf Limit: 0.3344\ +- Two sided p_value : 0.0009\ -**Exact Confidence Limits** +**Mid-p adjusted Binomial Test for result = H**\ -- 95% Lower Conf Limit: 0.2193\ -- 95% Upper Conf Limit: 0.3392 +- Proportion: 0.2763\ +- 95% Lower Conf Limit: 0.2212\ +- 95% Upper Conf Limit: 0.3371\ +- Right sided p_value : 0.0008\ -**Test of H0: Proportion = 0.19** +**Wilson Score Test for result = H**\ -- ASE under H0: 0.0260\ -- Z: 3.3223\ -- One-sided Pr \> Z: 0.0004\ -- Two-sided Pr \> \|Z\|: 0.0009\ -- Sample Size: 228 \ No newline at end of file +- Proportion: 0.2763\ +- ASE: 0.0296\ +- 95% Lower Conf Limit: 0.2223\ +- 95% Upper Conf Limit: 0.3377\ +- Two sided p_value : 0.0009