The notes for the topics on this page can be found in the lecture 8 folder on Canvas.

R

To conduct a chi-squared test or Fisher’s exact test in R you can use the CrossTable() function from the descr package. CrossTable() isn’t set up to directly account for weighted cell counts. If you only have summary data you can first use xtabs() to build the contigency table and then use the %>% operator to ‘pipe’ the table into CrossTable(); the below examples illustrate this. For more help, type ?CrossTable in the R console. For further help, please watch the video example.

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))

Chi-squared

To conduct a chi-squared test, set the chisq argument to TRUE.

library(descr)

xtabs(count ~ service + sleep, data = vietnam) %>% 
  CrossTable(expected = TRUE, chisq = TRUE)
##    Cell Contents 
## |-------------------------|
## |                       N | 
## |              Expected N | 
## | Chi-square contribution | 
## |           N / Row Total | 
## |           N / Col Total | 
## |         N / Table Total | 
## |-------------------------|
## 
## ================================
##            sleep
## service      yes      no   Total
## --------------------------------
## yes          173     160     333
##            144.2   188.8        
##            5.760   4.398        
##            0.520   0.480   0.187
##            0.224   0.158        
##            0.097   0.090        
## --------------------------------
## no           599     851    1450
##            627.8   822.2        
##            1.323   1.010        
##            0.413   0.587   0.813
##            0.776   0.842        
##            0.336   0.477        
## --------------------------------
## Total        772    1011    1783
##            0.433   0.567        
## ================================
## 
## Statistics for All Table Factors
## 
## Pearson's Chi-squared test 
## ------------------------------------------------------------
## Chi^2 = 12.49136      d.f. = 1      p = 0.000409 
## 
## Pearson's Chi-squared test with Yates' continuity correction 
## ------------------------------------------------------------
## Chi^2 = 12.06166      d.f. = 1      p = 0.000515

Fisher’s exact

To conduct a Fisher’s exact test, set the fisher argument to TRUE

xtabs(count ~ service + sleep, data = vietnam) %>% 
  CrossTable(fisher = TRUE)
##    Cell Contents 
## |-------------------------|
## |                       N | 
## | Chi-square contribution | 
## |           N / Row Total | 
## |           N / Col Total | 
## |         N / Table Total | 
## |-------------------------|
## 
## ================================
##            sleep
## service      yes      no   Total
## --------------------------------
## yes          173     160     333
##            5.760   4.398        
##            0.520   0.480   0.187
##            0.224   0.158        
##            0.097   0.090        
## --------------------------------
## no           599     851    1450
##            1.323   1.010        
##            0.413   0.587   0.813
##            0.776   0.842        
##            0.336   0.477        
## --------------------------------
## Total        772    1011    1783
##            0.433   0.567        
## ================================
## 
##  
## Fisher's Exact Test for Count Data
## ------------------------------------------------------------
## Sample estimate odds ratio: 1.535716 
## 
## Alternative hypothesis: true odds ratio is not equal to 1 
## p = 0.000466 
## 95% confidence interval: 1.200679 1.965386 
## 
## Alternative hypothesis: true odds ratio is less than 1 
## p = 1 
## 95%s confidence interval: % 0 1.890994 
## 
## Alternative hypothesis: true odds ratio is greater than 1 
## p = 0.000269 
## 95%s confidence interval: % 1.247644 Inf

R demonstration

SAS

To conduct a chi-squared test or Fisher’s exact test in SAS use the frequency procedure. The following code will produce return results for both a chi-squared test and a Fisher’s exact test. The syntax is:

proc freq data = <dataset>; 
  table <row variable>*<column variable> / chisq expected; 
  weight <weighting variable if summary data>; 
run; 

Chi-squared & Fisher’s exact

data vietnam; 
  input service $ sleep $ count; 
  cards; 
  yes yes 173
  yes no 160
  no yes 599
  no no 851
  ; 
run; 
  
title 'Chi-squared test example'; 
proc freq data = vietnam order = data; 
  table service*sleep / relrisk chisq expected; 
  weight count; 
run; 
##                                         Chi-squared test example
## 
##                                            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