library(readxl)
df <- read_excel("C:/dl/data/R-Rstudio/dac diem chung dan so.xlsx", 
    col_types = c("text", "numeric", "numeric", 
        "text", "text", "numeric", "numeric", 
        "numeric", "text", "text"))
View(df)
#KIỂM ĐỊNH PHÂN PHỐI CHUẨN
shapiro.test(df$age)
## 
##  Shapiro-Wilk normality test
## 
## data:  df$age
## W = 0.99485, p-value = 0.8563
shapiro.test(df$weight)
## 
##  Shapiro-Wilk normality test
## 
## data:  df$weight
## W = 0.9613, p-value = 0.0002143
shapiro.test(df$height)
## 
##  Shapiro-Wilk normality test
## 
## data:  df$height
## W = 0.97742, p-value = 0.01079
shapiro.test(df$bmi)
## 
##  Shapiro-Wilk normality test
## 
## data:  df$bmi
## W = 0.93298, p-value = 9.205e-07
#Kết luân: age (Tuổi) có phân phối chuẩn, còn lại là phân phối không chuẩn. Trình bày age dưới dạng số trung bình và độ lệch chuẩn, còn các biến khác trình bày số trung vị và IQR
#Đặc điểm chung của dân số nghiên cứu
library(gtsummary)
summary2 <- df %>%
select(age, sex, weight, height, bmi, classbmi, TOCA) %>%
   tbl_summary(by = TOCA,
    type = all_continuous() ~ "continuous2",
    statistic = list(
      all_continuous() ~ c("{mean} ({sd})", "{median} ({p25}, {p75})", "{min}, {max}"),
      all_categorical() ~ "{n} / {N} ({p}%)"
    ),
    digits = all_continuous() ~ 2,
    label = list(
      age ~ "Tuổi",
      sex ~ "Giới tính",
      weight ~ "Cân nặng (Kg)",
      height ~ "Chiều cao (m)",
      bmi ~ "Chỉ số khối cơ thể BMI (Kg/m2)",
      classbmi ~ "Béo phì"
    ),
    missing_text = "(Missing)"
   ) %>%
  add_p(test = list(
    age ~ "t.test", 
    weight ~ "wilcox.test",
    height ~ "wilcox.test",
    bmi ~ "wilcox.test",
    sex ~ "chisq.test",
    classbmi ~ "chisq.test"),
    pvalue_fun = ~ style_pvalue(.x, digits = 2)) %>%
  add_overall() %>%
  add_n() %>%
  modify_caption("*Đặc điểm chung của dân số nghiên cứu *")
summary2
*Đặc điểm chung của dân số nghiên cứu *
Characteristic N Overall
N = 158
1
0
N = 120
1
1
N = 38
1
p-value2
Tuổi 158


0.094
    Mean (SD)
63.24 (12.15) 64.17 (12.03) 60.32 (12.21)
    Median (Q1, Q3)
63.50 (55.00, 72.00) 63.50 (56.00, 72.50) 62.00 (51.00, 69.00)
    Min, Max
33.00, 95.00 35.00, 95.00 33.00, 80.00
Giới tính 158


0.55
    1
100 / 158 (63%) 78 / 120 (65%) 22 / 38 (58%)
    2
58 / 158 (37%) 42 / 120 (35%) 16 / 38 (42%)
Cân nặng (Kg) 158


0.49
    Mean (SD)
60.42 (10.01) 59.86 (8.95) 62.18 (12.80)
    Median (Q1, Q3)
60.00 (53.00, 65.00) 60.00 (53.00, 65.00) 60.50 (52.00, 67.00)
    Min, Max
40.00, 100.00 40.00, 93.00 43.00, 100.00
Chiều cao (m) 158


0.51
    Mean (SD)
1.60 (0.07) 1.60 (0.07) 1.61 (0.08)
    Median (Q1, Q3)
1.60 (1.55, 1.65) 1.60 (1.55, 1.65) 1.60 (1.55, 1.67)
    Min, Max
1.45, 1.80 1.45, 1.80 1.48, 1.76
Chỉ số khối cơ thể BMI (Kg/m2) 158


0.84
    Mean (SD)
23.49 (2.96) 23.38 (2.79) 23.84 (3.47)
    Median (Q1, Q3)
23.24 (21.40, 24.74) 23.44 (21.44, 24.63) 22.99 (21.34, 25.71)
    Min, Max
17.69, 33.73 17.69, 32.43 19.56, 33.73
Béo phì 158


0.17
    0
123 / 158 (78%) 97 / 120 (81%) 26 / 38 (68%)
    1
35 / 158 (22%) 23 / 120 (19%) 12 / 38 (32%)
1 n / N (%)
2 Welch Two Sample t-test; Pearson’s Chi-squared test; Wilcoxon rank sum test
library(ggplot2)
#Biểu đồ phân phối giới tính theo từng nhóm
ggplot(df, aes(x= TOCA, fill = sex)) +
  geom_bar( position = "fill") +
  labs(x = "Tình trạng tắc ĐMV thủ phạm", y = "Tỷ lệ (%)") +
  scale_y_continuous(labels = scales::percent_format()) +
  scale_x_discrete(labels = c("1"="Tắc hoàn toàn", "0" = " Không tắc hoàn toàn")) +   scale_fill_manual(values = c("black","pink"), labels = c("1" = "Nam", "2" = "Nữ")) +
  theme_minimal()