MI data

mi <- matrix(c(189, 104, 10845, 10933), ncol = 2)
mi
##      [,1]  [,2]
## [1,]  189 10845
## [2,]  104 10933
dimnames(mi) <- list(treat = c("placebo", "aspirin"), "mypcarial infraction" = c("yes", "no"))
mi
##          mypcarial infraction
## treat     yes    no
##   placebo 189 10845
##   aspirin 104 10933
margin.table(mi, 1)
## treat
## placebo aspirin 
##   11034   11037
prop.table(mi, 1)
##          mypcarial infraction
## treat            yes        no
##   placebo 0.01712887 0.9828711
##   aspirin 0.00942285 0.9905771

seat-belt data

ex1 <- matrix(c(56, 2, 8, 16), nrow = 2)
dimnames(ex1) <- list(parent = c("buckled", "unbuckled"), child = c("buckled", "unbuckled"))
ex1
##            child
## parent      buckled unbuckled
##   buckled        56         8
##   unbuckled       2        16
prop.table(ex1)         
##            child
## parent         buckled  unbuckled
##   buckled   0.68292683 0.09756098
##   unbuckled 0.02439024 0.19512195

chi-square tests

chisq.test(mi)
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  mi
## X-squared = 24.429, df = 1, p-value = 7.71e-07
res <- chisq.test(mi)
res$p.value
## [1] 7.709708e-07
chisq.test(ex1)
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  ex1
## X-squared = 35.995, df = 1, p-value = 1.978e-09
res <- chisq.test(ex1)
res$p.value
## [1] 1.977918e-09

Fisher’s exact test

ex2 <- matrix(c(3, 1, 1, 3), nrow = 2)
dimnames(ex2) <- list(Guess = c("Milk", "Tea"), Truth = c("Milk", "Tea"))
ex2
##       Truth
## Guess  Milk Tea
##   Milk    3   1
##   Tea     1   3
fisher.test(ex2)
## 
##  Fisher's Exact Test for Count Data
## 
## data:  ex2
## p-value = 0.4857
## alternative hypothesis: true odds ratio is not equal to 1
## 95 percent confidence interval:
##    0.2117329 621.9337505
## sample estimates:
## odds ratio 
##   6.408309
fisher.test(ex2, alt = "greater")
## 
##  Fisher's Exact Test for Count Data
## 
## data:  ex2
## p-value = 0.2429
## alternative hypothesis: true odds ratio is greater than 1
## 95 percent confidence interval:
##  0.3135693       Inf
## sample estimates:
## odds ratio 
##   6.408309

Hypergeometric distribution

  • 연령집단과 질병 유무
x <- 0:36
p <- choose(214, x) * choose(112, 36 - x) / choose(326, 36)
plot(x, p, col = "red", pch = 16)

observed.p <- p[x == 30]
observed.p
## [1] 0.008079815
exact.P <- sum(p[p <= observed.p])
exact.P
## [1] 0.02423025
d <- matrix(c(30, 6, 184, 106), ncol = 2)
fisher.test(d)
## 
##  Fisher's Exact Test for Count Data
## 
## data:  d
## p-value = 0.02423
## alternative hypothesis: true odds ratio is not equal to 1
## 95 percent confidence interval:
##  1.127473 8.719466
## sample estimates:
## odds ratio 
##    2.87246

Correlation analysis

  • 맥주 섭취량과 혈중알콜수준(blood alcohol level)
beers <- c(5,2,9,8,3,7,3,5,3,5)
BAL <- c(0.10,0.03,0.19,0.12,0.04,0.095,0.07,0.06,0.02,0.05)
cor.test(beers,BAL)
## 
##  Pearson's product-moment correlation
## 
## data:  beers and BAL
## t = 5.4687, df = 8, p-value = 0.0005953
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.5867467 0.9734515
## sample estimates:
##       cor 
## 0.8882323