Cochrane armitage test for trend

Hi

I'm a doctor writing a thesis (about multidrug-resistant bacteria) - meaning I'm a beginner in rstudio :slight_smile: I'd like to create a cochrane armitage test in rstudio. the data is relatively simple:

year = 2018, 2019, 2020, 2021, 2022
0 = 481, 459, 602, 689, 542
1 = 23, 38, 25, 37, 35

Can anybody help me?

Thank you!
Christian

There is a function to perform a Cochrane Amitrage Test in the {DescTools} package.

Perform a Cochran Armitage test for trend in binomial proportions across the levels of a single variable. This test is appropriate only when one variable has two levels and the other variable is ordinal. The two-level variable represents the response, and the other represents an explanatory variable with ordered levels. The null hypothesis is the hypothesis of no trend, which means that the binomial proportion is the same for all levels of the explanatory variable.

The CochranArmitageTest() function takes as its argument a contingency table or a matrix. The example given is

library(DescTools)
d <- data.frame(
  Year = c(2018, 2019, 2020, 2021, 2022),
  Absent = c(481, 459, 602, 689, 542),
  Present = c(23, 38, 25, 37, 35))

dose <- matrix(c(10,9,10,7, 0,1,0,3), byrow=TRUE, nrow=2, dimnames=list(resp=0:1, dose=0:3))
dose
#>     dose
#> resp  0 1  2 3
#>    0 10 9 10 7
#>    1  0 1  0 3
Desc(dose)
#> ------------------------------------------------------------------------------ 
#> dose (matrix, array)
#> 
#> Summary: 
#> n: 40, rows: 2, columns: 4
#> 
#> Pearson's Chi-squared test:
#>   X-squared = 6.6667, df = 3, p-value = 0.08332
#> Log likelihood ratio (G-test) test of independence:
#>   G = 7.2877, X-squared df = 3, p-value = 0.06327
#> Mantel-Haenszel Chi-squared:
#>   X-squared = 3.4667, df = 1, p-value = 0.06262
#> 
#> Warning message:
#>   Exp. counts < 5: Chi-squared approx. may be incorrect!!
#> 
#> 
#> Contingency Coeff.     0.378
#> Cramer's V             0.408
#> Kendall Tau-b          0.272
#> 
#>                                                
#>        dose       0      1      2      3    Sum
#> resp                                           
#>                                                
#> 0      freq      10      9     10      7     36
#>        perc   25.0%  22.5%  25.0%  17.5%  90.0%
#>        p.row  27.8%  25.0%  27.8%  19.4%      .
#>        p.col 100.0%  90.0% 100.0%  70.0%      .
#>                                                
#> 1      freq       0      1      0      3      4
#>        perc    0.0%   2.5%   0.0%   7.5%  10.0%
#>        p.row   0.0%  25.0%   0.0%  75.0%      .
#>        p.col   0.0%  10.0%   0.0%  30.0%      .
#>                                                
#> Sum    freq      10     10     10     10     40
#>        perc   25.0%  25.0%  25.0%  25.0% 100.0%
#>        p.row      .      .      .      .      .
#>        p.col      .      .      .      .      .
#> 


CochranArmitageTest(dose)
#> 
#>  Cochran-Armitage test for trend
#> 
#> data:  dose
#> Z = -1.8856, dim = 4, p-value = 0.05935
#> alternative hypothesis: two.sided
CochranArmitageTest(dose, alternative="one.sided")
#> 
#>  Cochran-Armitage test for trend
#> 
#> data:  dose
#> Z = -1.8856, dim = 4, p-value = 0.02967
#> alternative hypothesis: one.sided

Created on 2023-12-15 with reprex v2.0.2

Your data's variable year is the only candidate as the treatment, or explanatory, variable, meaning that the proportions of binary outcomes 0 and 1 are some function of the calendar year in which they are observed. The test provides evidence on whether the outcomes differ from random at some level of confidence.

There's an implicit assumption in selecting year that all observations within a calendar year are treated the same, such that 2021-01-01 and 2021-12-31 are the same and 2021-12-31 and 2023-01-01 are different even though many days elapse in the former and ony a single day in the later.

In working with R it is rewarding to begin at a higher level of generality by defining the question to be put to the data, imagining the form of the answer and only then looking for a specific function.

A homely example is the hapless woodworker who walks into a hardware store to ask "what chainsaw should I use on my mahogeny?" The clerk asks "how big is your log?" The customer answers "I have two planks." Clerk: "And what is it you want to do with the planks?" Customer: "Join them." Clerk: "Like to make a tabletop?" Customer: "Yeah, I want to stick them together now that I have them cut to the same size and planed." Clerk: "I suggest a biscuit cutter."

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.