The notes for the topics on this page can be found in the lectures 9 folders on Canvas.

R

To conduct a likelihood ratio test on a 2 x 2 table in R you can use GTest() from the DescTools package. To conduct a 2-sample test of proportions you can use prop_test() from the catfun package. For both functions you can provide a frequency table as the main argument as seen in the examples below.

vietnam <- tibble(
  service = c(rep("yes", 2), rep("no", 2)), 
  sleep = c(rep(c("yes", "no"), 2)), 
  count = c(173, 160, 599, 851)
)

vietnam <- vietnam %>% 
  mutate(service = as_factor(service), 
         sleep = as_factor(sleep))

Likelihood ratio test

library(DescTools)

xtabs(count ~ service + sleep, data = vietnam) %>% 
  GTest()
## 
##  Log likelihood ratio (G-test) test of independence without
##  correction
## 
## data:  .
## G = 12.394, X-squared df = 1, p-value = 0.0004308

2-sample test of proportions

library(catfun)

xtabs(count ~ service + sleep, data = vietnam) %>% 
  prop_test()
## Observed proportions: 
## * 0.5195
## * 0.4131
## ---------------------------------------- 
## Confidence interval method: wald 
## Confidence intervals: 
##   Lower bound Upper bound
## 1   0.4658578   0.5731813
## 2   0.3877595   0.4384474
## ---------------------------------------- 
## 2 sample test for equality of proportions 
## Chi-squared: 12.49136 
## p-value: 0.00041

SAS

To conduct a likelihood ratio test for a frequency table and a two-sample test of proportions in SAS you use the frequency procedure. To indicate that you want SAS to return a likelihood ratio test you specify the chisq argument in the options of the table statement. To indicate you want SAS to return a two-sample test of proportions you can specify riskdiff(equal var = null) in the options of the table statement.

Likelihood ratio test

data vietnam; 
  input service $ sleep $ count; 
  cards; 
  yes yes 173
  yes no 160
  no yes 599
  no no 851
  ; 
run; 

proc freq data = vietnam order = data; 
  table service*sleep / relrisk chisq expected; 
  weight count; 
run; 
##                                            The FREQ Procedure
## 
##                                        Table of service by sleep
## 
##                                   service     sleep
## 
##                                   Frequency|
##                                   Expected |
##                                   Percent  |
##                                   Row Pct  |
##                                   Col Pct  |yes     |no      |  Total
##                                   ---------+--------+--------+
##                                   yes      |    173 |    160 |    333
##                                            | 144.18 | 188.82 |
##                                            |   9.70 |   8.97 |  18.68
##                                            |  51.95 |  48.05 |
##                                            |  22.41 |  15.83 |
##                                   ---------+--------+--------+
##                                   no       |    599 |    851 |   1450
##                                            | 627.82 | 822.18 |
##                                            |  33.60 |  47.73 |  81.32
##                                            |  41.31 |  58.69 |
##                                            |  77.59 |  84.17 |
##                                   ---------+--------+--------+
##                                   Total         772     1011     1783
##                                               43.30    56.70   100.00
## 
## 
##                                 Statistics for Table of service by sleep
## 
##                          Statistic                     DF       Value      Prob
##                          ------------------------------------------------------
##                          Chi-Square                     1     12.4914    0.0004
##                          Likelihood Ratio Chi-Square    1     12.3936    0.0004
##                          Continuity Adj. Chi-Square     1     12.0617    0.0005
##                          Mantel-Haenszel Chi-Square     1     12.4843    0.0004
##                          Phi Coefficient                       0.0837          
##                          Contingency Coefficient               0.0834          
##                          Cramer's V                            0.0837          
## 
## 
##                                           Fisher's Exact Test
##                                    ----------------------------------
##                                    Cell (1,1) Frequency (F)       173
##                                    Left-sided Pr <= F          0.9998
##                                    Right-sided Pr >= F         0.0003
##                                                                      
##                                    Table Probability (P)       <.0001
##                                    Two-sided Pr <= P           0.0005
## 
## 
##                                      Odds Ratio and Relative Risks
##  
##                    Statistic                        Value       95% Confidence Limits
##                    ------------------------------------------------------------------
##                    Odds Ratio                      1.5361        1.2095        1.9509
##                    Relative Risk (Column 1)        1.2576        1.1152        1.4181
##                    Relative Risk (Column 2)        0.8187        0.7263        0.9228
## 
##                                            Sample Size = 1783

2-sample test of proportions

data vietnam; 
  input service $ sleep $ count; 
  cards; 
  yes yes 173
  yes no 160
  no yes 599
  no no 851
  ; 
run; 
  
proc freq data = vietnam order = data; 
  table service*sleep / expected riskdiff(equal var = null); 
  weight count; 
run; 
##                                            The FREQ Procedure
## 
##                                        Table of service by sleep
## 
##                                   service     sleep
## 
##                                   Frequency|
##                                   Expected |
##                                   Percent  |
##                                   Row Pct  |
##                                   Col Pct  |yes     |no      |  Total
##                                   ---------+--------+--------+
##                                   yes      |    173 |    160 |    333
##                                            | 144.18 | 188.82 |
##                                            |   9.70 |   8.97 |  18.68
##                                            |  51.95 |  48.05 |
##                                            |  22.41 |  15.83 |
##                                   ---------+--------+--------+
##                                   no       |    599 |    851 |   1450
##                                            | 627.82 | 822.18 |
##                                            |  33.60 |  47.73 |  81.32
##                                            |  41.31 |  58.69 |
##                                            |  77.59 |  84.17 |
##                                   ---------+--------+--------+
##                                   Total         772     1011     1783
##                                               43.30    56.70   100.00
## 
## 
##                                 Statistics for Table of service by sleep
## 
##                                         Column 1 Risk Estimates
##  
##                                                    (Asymptotic) 95%         (Exact) 95%
##                                Risk        ASE     Confidence Limits     Confidence Limits
##              -----------------------------------------------------------------------------
##              Row 1           0.5195     0.0274     0.4659     0.5732     0.4644     0.5743
##              Row 2           0.4131     0.0129     0.3878     0.4384     0.3876     0.4389
##              Total           0.4330     0.0117     0.4100     0.4560     0.4098     0.4564
## 
##              Difference      0.1064     0.0303     0.0471     0.1658                      
## 
##                                      Difference is (Row 1 - Row 2)
## 
## 
##                                    Proportion (Risk) Difference Test
##                                     H0: P1 - P2 = 0    Wald Method
## 
##                                    Proportion Difference      0.1064
##                                    ASE (H0)                   0.0301
##                                    Z                          3.5343
##                                    One-sided Pr >  Z          0.0002
##                                    Two-sided Pr > |Z|         0.0004
## 
##                                         Column 1 (sleep = yes)
## 
## 
##                                         Column 2 Risk Estimates
##  
##                                                    (Asymptotic) 95%         (Exact) 95%
##                                Risk        ASE     Confidence Limits     Confidence Limits
##              -----------------------------------------------------------------------------
##              Row 1           0.4805     0.0274     0.4268     0.5341     0.4257     0.5356
##              Row 2           0.5869     0.0129     0.5616     0.6122     0.5611     0.6124
##              Total           0.5670     0.0117     0.5440     0.5900     0.5436     0.5902
## 
##              Difference     -0.1064     0.0303    -0.1658    -0.0471                      
## 
##                                      Difference is (Row 1 - Row 2)
## 
##                                            Sample Size = 1783