Tables thường được yêu cầu trình bày khi phân tích mối quan hệ giữa 2 biến số rời rạc (categorical variables).
Data về bệnh diabetes type 2 được sử dụng trong bài minh hoạ này.
library(stats)
library(tidyverse)
library("mlbench")
data("PimaIndiansDiabetes2")
Trước hết chúng ta tạo ra các biến số categorical từ các biến continuous.
[1] "pregnant" "glucose" "pressure" "triceps" "insulin" "mass"
[7] "pedigree" "age" "diabetes"
Lập các bảng như sau.
table(df$agegr, df$outcome)
0 1
old 173 178
young 327 90
table(df$preg, df$outcome)
0 1
non-standard 189 155
standard 311 113
# thêm cột sum
addmargins(table(df$agegr, df$outcome))
0 1 Sum
old 173 178 351
young 327 90 417
Sum 500 268 768
addmargins(table(df$preg, df$outcome))
0 1 Sum
non-standard 189 155 344
standard 311 113 424
Sum 500 268 768
# tính phần trăm các ô
prop.table(table(df$agegr, df$outcome))
0 1
old 0.2252604 0.2317708
young 0.4257812 0.1171875
prop.table(table(df$preg, df$outcome))
0 1
non-standard 0.2460938 0.2018229
standard 0.4049479 0.1471354
Trong thực hành phân tích dữ liệu, nhiều trường hợp chúng ta phân tích mối quan hệ giữa 2 categorical variables nhưng có sự nghi ngờ bị ảnh hưởng bởi một biến số categorical khác. Hiện tượng này gọi là confounding hoặc interaction.
Để làm rõ sự ảnh hưởng của biến số thứ ba, chúng ta phải lập bảng có sự tham gia của 3 biến số đó. Sau đó phân tích và so sánh những RRs, ORs để xem chúng thay đổi, tác động đến 2 biến số ban đầu như thế nào.
Gọi là hiệu chỉnh về biến số thứ ba (adjusted for the third variables)
tb1 <- xtabs(~ agegr + outcome + preg, data = df)
ftable(tb1)
preg non-standard standard
agegr outcome
old 0 132 41
1 135 43
young 0 57 270
1 20 70
prop.table(ftable(tb1))
preg non-standard standard
agegr outcome
old 0 0.17187500 0.05338542
1 0.17578125 0.05598958
young 0 0.07421875 0.35156250
1 0.02604167 0.09114583
Để tính RRs và ORs chúng ta cần thêm cột sum cho thuận lợi tính toán hơn, sử dụng hàm addmargins()
tb2 <- addmargins(tb1)
tb2
, , preg = non-standard
outcome
agegr 0 1 Sum
old 132 135 267
young 57 20 77
Sum 189 155 344
, , preg = standard
outcome
agegr 0 1 Sum
old 41 43 84
young 270 70 340
Sum 311 113 424
, , preg = Sum
outcome
agegr 0 1 Sum
old 173 178 351
young 327 90 417
Sum 500 268 768
prop.table(tb2, 1)
, , preg = non-standard
outcome
agegr 0 1 Sum
old 0.09401709 0.09615385 0.19017094
young 0.03417266 0.01199041 0.04616307
Sum 0.06152344 0.05045573 0.11197917
, , preg = standard
outcome
agegr 0 1 Sum
old 0.02920228 0.03062678 0.05982906
young 0.16187050 0.04196643 0.20383693
Sum 0.10123698 0.03678385 0.13802083
, , preg = Sum
outcome
agegr 0 1 Sum
old 0.12321937 0.12678063 0.25000000
young 0.19604317 0.05395683 0.25000000
Sum 0.16276042 0.08723958 0.25000000
Với kết quả ở trên chúng ta tính được RRs, ORs trong sự hiện diện hoặc không hiện diện của biến số thứ 3, trong ví dụ này là preg.
Nếu (RRcrude - RRadjusted)/RRadjusted >= 10 % thì chúng ta nói biến số thứ 3 là một confounder.
mantelhaen.test(tb1)
Mantel-Haenszel chi-squared test with continuity correction
data: tb1
Mantel-Haenszel X-squared = 43.214, df = 1, p-value = 4.906e-11
alternative hypothesis: true common odds ratio is not equal to 1
95 percent confidence interval:
0.1988202 0.4239000
sample estimates:
common odds ratio
0.29031
p < 0.0001 cho thấy preg là một confounder