library(stringr)
Summarise <- function(t,title) {
n1 <- t[1] + t[2]
n2 <- t[3] + t[4]
m1 <- t[1] + t[3]
m2 <- t[2] + t[4]
padchar <- ' '
print('',quote=FALSE)
print(title,quote=FALSE)
print (' D | not D | total',quote = FALSE)
print(paste(
'E ',
str_pad(t[1], 6, pad = padchar),'|',
str_pad(t[2], 6, pad = padchar),'|',
str_pad(n1, 6, pad = padchar)
),quote = FALSE)
print(paste(
'not E',
str_pad(t[3], 6, pad = padchar),'|',
str_pad(t[4], 6, pad = padchar),'|',
str_pad(n2, 6, pad = padchar)
),quote = FALSE)
print('----------------------------- ',quote = FALSE)
print(paste(
'total',
str_pad(m1, 6, pad = padchar),'|',
str_pad(m2, 6, pad = padchar),'|',
str_pad(sum(t), 6, pad = padchar)
),quote = FALSE)
}
Analyse <- function(t,option=1) {
a <- t[1]
b <- t[2]
c <- t[3]
d <- t[4]
if ( option == 1 ) {
RelativeRisk <- (a/(a+b)) / (c/(c+d))
OddsRatio <- (a*d) / (c*b)
} else {
RelativeRisk <- (a/(a+c)) / (b/(b+d))
OddsRatio <- (a*d) / (c*b)
}
print('',quote=FALSE)
if ( option == 1 ) {
print('P( D | E )',quote=FALSE)
} else {
print('P( E | D )',quote=FALSE)
}
print('Relative Risk (RR) =',quote=FALSE)
print(RelativeRisk,quote=FALSE)
print('Odds Ratio (OR) = ',quote=FALSE)
print(OddsRatio,quote=FALSE)
}
This shows examples of the effect of low disease rates and low exposure rates on RR and OR.
Note thet RR depends on whether we are calculating P ( D | E ) or P ( E | D ), and has the corresponding interpretation; OR does not and is the same in either case.
Table <- as.matrix(c(35,34,153,637),rows=2)
Summarise(Table,'M249 Book 1, example 2.3 Cannabis use and mental health')
## [1]
## [1] M249 Book 1, example 2.3 Cannabis use and mental health
## [1] D | not D | total
## [1] E 35 | 34 | 69
## [1] not E 153 | 637 | 790
## [1] -----------------------------
## [1] total 188 | 671 | 859
Analyse(Table,1)
## [1]
## [1] P( D | E )
## [1] Relative Risk (RR) =
## [1] 2.619115
## [1] Odds Ratio (OR) =
## [1] 4.285852
Analyse(Table,2)
## [1]
## [1] P( E | D )
## [1] Relative Risk (RR) =
## [1] 3.674124
## [1] Odds Ratio (OR) =
## [1] 4.285852
Table.2 <- as.matrix(c(3,200,1,240),rows=2)
Summarise(Table.2,'Low disease rate example')
## [1]
## [1] Low disease rate example
## [1] D | not D | total
## [1] E 3 | 200 | 203
## [1] not E 1 | 240 | 241
## [1] -----------------------------
## [1] total 4 | 440 | 444
Analyse(Table.2,1)
## [1]
## [1] P( D | E )
## [1] Relative Risk (RR) =
## [1] 3.561576
## [1] Odds Ratio (OR) =
## [1] 3.6
Analyse(Table.2,2)
## [1]
## [1] P( E | D )
## [1] Relative Risk (RR) =
## [1] 1.65
## [1] Odds Ratio (OR) =
## [1] 3.6
Table.3 <- as.matrix(c(5,2,250,240),rows=2)
Summarise(Table.3,'Low exposure rate example')
## [1]
## [1] Low exposure rate example
## [1] D | not D | total
## [1] E 5 | 2 | 7
## [1] not E 250 | 240 | 490
## [1] -----------------------------
## [1] total 255 | 242 | 497
Analyse(Table.3,1)
## [1]
## [1] P( D | E )
## [1] Relative Risk (RR) =
## [1] 1.4
## [1] Odds Ratio (OR) =
## [1] 2.4
Analyse(Table.3,2)
## [1]
## [1] P( E | D )
## [1] Relative Risk (RR) =
## [1] 2.372549
## [1] Odds Ratio (OR) =
## [1] 2.4