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

R

To conduct a Cochran-Armitage Trend test in R you can use CochranArmitageTest() from the DescTools package. CochranArmitageTest() requires a frequency table as itโ€™s main argument. For more information and examples, run ?CochranArmitageTest.

library(tidyverse)
library(DescTools)

trend <- tibble(
  insomnia = c(rep("yes", 6), rep("no", 6)), 
  age_class = rep(seq(1:6), 2), 
  count = c(150, 250, 264, 302, 238, 176, 384, 496, 520, 403, 205, 123)
)

trend <- trend %>% 
  mutate(insomnia = fct_relevel(insomnia, c("yes", "no")))

trend_table <- xtabs(count ~ insomnia + age_class, data = trend)

CochranArmitageTest(trend_table)
## 
##  Cochran-Armitage test for trend
## 
## data:  trend_table
## Z = 11.297, dim = 6, p-value < 2.2e-16
## alternative hypothesis: two.sided

SAS

To conduct a Cochran-Armitage Trend test in SAS you use the frequency procedure with the trend argument specified in the options of the table statement.

proc freq data = <dataset>; 
  table <row variable>*<column variable> / chisq trend; 
  weight <weighting variable if summary data>; 
run; 
data trend; 
  input insomnia $ ageclass count @@; 
  cards; 
  yes 1 150 yes 2 250 yes 3 264 yes 4 302 yes 5 238 yes 6 176
  no 1 384 no 2 496 no 3 520 no 4 403 no 5 205 no 6 123
  ; 
run; 
  
proc freq data = trend order = data; 
  table insomnia*ageclass / chisq trend; 
  weight count; 
run;
##                                            The FREQ Procedure
## 
##                                      Table of insomnia by ageclass
## 
##                 insomnia     ageclass
## 
##                 Frequency|
##                 Percent  |
##                 Row Pct  |
##                 Col Pct  |       1|       2|       3|       4|       5|       6|  Total
##                 ---------+--------+--------+--------+--------+--------+--------+
##                 yes      |    150 |    250 |    264 |    302 |    238 |    176 |   1380
##                          |   4.27 |   7.12 |   7.52 |   8.60 |   6.78 |   5.01 |  39.31
##                          |  10.87 |  18.12 |  19.13 |  21.88 |  17.25 |  12.75 |
##                          |  28.09 |  33.51 |  33.67 |  42.84 |  53.72 |  58.86 |
##                 ---------+--------+--------+--------+--------+--------+--------+
##                 no       |    384 |    496 |    520 |    403 |    205 |    123 |   2131
##                          |  10.94 |  14.13 |  14.81 |  11.48 |   5.84 |   3.50 |  60.69
##                          |  18.02 |  23.28 |  24.40 |  18.91 |   9.62 |   5.77 |
##                          |  71.91 |  66.49 |  66.33 |  57.16 |  46.28 |  41.14 |
##                 ---------+--------+--------+--------+--------+--------+--------+
##                 Total         534      746      784      705      443      299     3511
##                             15.21    21.25    22.33    20.08    12.62     8.52   100.00
## 
## 
##                               Statistics for Table of insomnia by ageclass
## 
##                          Statistic                     DF       Value      Prob
##                          ------------------------------------------------------
##                          Chi-Square                     5    139.3097    <.0001
##                          Likelihood Ratio Chi-Square    5    138.4440    <.0001
##                          Mantel-Haenszel Chi-Square     1    127.5934    <.0001
##                          Phi Coefficient                       0.1992          
##                          Contingency Coefficient               0.1954          
##                          Cramer's V                            0.1992          
## 
## 
##                                       Cochran-Armitage Trend Test
##                                       ---------------------------
##                                       Statistic (Z)       11.2973
##                                       One-sided Pr >  Z    <.0001
##                                       Two-sided Pr > |Z|   <.0001
## 
##                                            Sample Size = 3511