#Đưa dữ liệu vào R
library(haven)
endo <- read_sav("D:/Deskop/Endobronchial_biopsy/Xử lý số liệu/RStudio/dulieuchuan.sav")
head(endo)
## # A tibble: 6 × 108
## stt hoten namsinh gioi ldvv V6 hutthuocla goinam tha suytim dtd
## <dbl> <chr> <dbl> <dbl> <dbl> <chr> <dbl+lbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 NGUYEN V… 1948 1 5 "" 1 [Còn hú… 30 0 0 0
## 2 2 LY VAN T… 1956 1 5 "" 1 [Còn hú… 20 1 0 0
## 3 3 DIEN CHE 1954 1 5 "" 1 [Còn hú… 40 1 0 0
## 4 4 NGUYEN V… 1976 1 2 "" 0 [Không … NA 0 0 0
## 5 5 LUU VAN … 1956 1 1 "" 1 [Còn hú… 20 1 0 0
## 6 6 NGUYEN M… 1960 1 1 "" 0 [Không … NA 0 0 0
## # ℹ 97 more variables: ckd <dbl>, xogan <dbl>, doiquy <dbl>, ungthu <dbl>,
## # loaiut <chr>, copd <dbl>, hen <dbl>, lao <dbl>, laomp <dbl>, cc <dbl>,
## # cn <dbl>, sot <dbl>, met <dbl>, sutcan <dbl>, chanan <dbl>, hokhan <dbl>,
## # hodam <dbl>, homau <dbl>, daunguc <dbl>, khotho <dbl>, khangiong <dbl>,
## # nuotnghen <dbl>, sohach <dbl>, hc3giam <dbl>, tn <dbl>, tt <dbl>, nt <dbl>,
## # khonggn <dbl>, kinhmo <dbl>, hang <dbl>, notmo <dbl>, dam <dbl>,
## # dongdac <dbl>, xep <dbl>, tdmp <dbl>, trenp <dbl>, giuap <dbl>, …
str(endo)
## tibble [112 × 108] (S3: tbl_df/tbl/data.frame)
## $ stt : num [1:112] 1 2 3 4 5 6 7 8 9 10 ...
## ..- attr(*, "format.spss")= chr "F3.0"
## $ hoten : chr [1:112] "NGUYEN VAN HOI" "LY VAN THAN" "DIEN CHE" "NGUYEN VAN EM" ...
## ..- attr(*, "format.spss")= chr "A20"
## ..- attr(*, "display_width")= int 20
## $ namsinh : num [1:112] 1948 1956 1954 1976 1956 ...
## ..- attr(*, "format.spss")= chr "F4.0"
## $ gioi : num [1:112] 1 1 1 1 1 1 1 1 1 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ ldvv : num [1:112] 5 5 5 2 1 1 4 4 5 3 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ V6 : chr [1:112] "" "" "" "" ...
## ..- attr(*, "format.spss")= chr "A15"
## ..- attr(*, "display_width")= int 15
## $ hutthuocla: dbl+lbl [1:112] 1, 1, 1, 0, 1, 0, 1, 0, 2, 0, 2, 1, 2, 1, 0, 1, 1, 1, ...
## ..@ format.spss: chr "F1.0"
## ..@ labels : Named num [1:3] 0 1 2
## .. ..- attr(*, "names")= chr [1:3] "Không hút" "Còn hút" "Ngưng"
## $ goinam : num [1:112] 30 20 40 NA 20 NA 30 NA 28 NA ...
## ..- attr(*, "format.spss")= chr "F2.0"
## $ tha : num [1:112] 0 1 1 0 1 0 1 1 1 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ suytim : num [1:112] 0 0 0 0 0 0 0 0 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ dtd : num [1:112] 0 0 0 0 0 0 0 0 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ ckd : num [1:112] 0 0 0 0 0 0 0 0 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ xogan : num [1:112] 0 0 0 0 0 0 0 0 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ doiquy : num [1:112] 0 0 0 0 0 0 0 0 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ ungthu : num [1:112] 0 0 0 0 0 1 0 0 0 1 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ loaiut : chr [1:112] "" "" "" "" ...
## ..- attr(*, "format.spss")= chr "A14"
## ..- attr(*, "display_width")= int 14
## $ copd : num [1:112] 0 0 0 0 0 0 1 0 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ hen : num [1:112] 0 0 0 0 0 0 0 0 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ lao : num [1:112] 0 0 0 0 0 0 1 0 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ laomp : num [1:112] 1 0 0 0 0 0 0 0 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ cc : num [1:112] 1.65 1.62 1.6 1.62 1.65 1.7 1.64 1.62 1.65 1.6 ...
## ..- attr(*, "format.spss")= chr "F4.2"
## $ cn : num [1:112] 43 69 49 65 55 60 50 58 62 52 ...
## ..- attr(*, "format.spss")= chr "F2.0"
## $ sot : num [1:112] 0 0 0 1 0 0 1 0 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ met : num [1:112] 0 0 0 0 0 0 0 0 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ sutcan : num [1:112] 1 0 1 0 0 0 0 0 1 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ chanan : num [1:112] 1 0 1 0 0 0 0 0 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ hokhan : num [1:112] 0 0 1 0 0 0 0 0 1 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ hodam : num [1:112] 0 0 0 1 1 0 1 0 0 1 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ homau : num [1:112] 0 0 0 0 0 0 1 1 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ daunguc : num [1:112] 1 1 1 1 0 1 0 0 1 1 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ khotho : num [1:112] 1 0 0 0 1 1 0 0 1 1 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ khangiong : num [1:112] 0 0 1 0 0 0 0 0 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ nuotnghen : num [1:112] 0 0 1 0 0 0 0 0 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ sohach : num [1:112] 0 0 0 0 0 0 0 0 0 1 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ hc3giam : num [1:112] 1 0 1 0 1 0 0 0 1 1 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ tn : num [1:112] 0 1 1 0 0 1 0 0 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ tt : num [1:112] 0 0 0 0 0 0 0 0 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ nt : num [1:112] 0 0 0 1 1 0 1 0 0 1 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ khonggn : num [1:112] 0 0 0 0 0 0 0 1 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ kinhmo : num [1:112] 1 1 0 1 1 0 0 0 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ hang : num [1:112] 0 0 0 0 1 0 0 0 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ notmo : num [1:112] 0 0 0 0 1 0 0 0 0 1 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ dam : num [1:112] 1 1 1 1 0 1 1 1 1 1 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ dongdac : num [1:112] 0 0 0 0 1 0 0 1 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ xep : num [1:112] 0 0 0 0 0 0 0 0 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ tdmp : num [1:112] 1 0 1 1 1 0 1 0 1 1 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ trenp : num [1:112] 1 1 1 1 0 0 1 1 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ giuap : num [1:112] 1 0 1 0 1 0 1 1 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ duoip : num [1:112] 1 0 1 0 0 0 0 0 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ trent : num [1:112] 0 0 0 0 0 1 0 0 1 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ duoit : num [1:112] 0 0 0 0 0 1 0 0 0 1 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ nho3 : num [1:112] 0 0 0 0 1 0 0 0 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ tu35 : num [1:112] 0 0 0 0 0 0 0 1 0 1 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ tu57 : num [1:112] 1 0 0 0 0 0 0 0 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ lon7 : num [1:112] 0 1 1 1 0 1 1 0 1 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ bo : num [1:112] 3 2 2 2 2 1 3 3 2 1 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ hach : num [1:112] 1 0 1 0 1 1 1 1 0 1 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ khongxl : num [1:112] 0 0 0 1 0 1 0 1 0 1 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ xlmp : num [1:112] 1 0 1 0 1 0 1 0 1 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ xltn : num [1:112] 1 1 0 0 0 0 0 0 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ xltt : num [1:112] 0 0 1 0 0 0 0 0 1 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ noilong : num [1:112] 0 1 0 0 0 1 1 1 1 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ kq : num [1:112] 0 0 0 0 0 0 0 0 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ carina : num [1:112] 0 0 0 0 0 0 0 0 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ gocp : num [1:112] 0 0 1 0 0 0 0 0 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ goct : num [1:112] 0 0 0 0 0 0 0 0 1 1 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ pqtp : num [1:112] 1 1 1 1 1 0 1 1 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ pqgp : num [1:112] 0 0 0 0 1 0 0 0 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ pqdp : num [1:112] 1 0 0 0 0 0 0 0 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ pqtg : num [1:112] 0 0 0 0 0 0 0 0 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ pqtt : num [1:112] 0 0 0 0 0 1 0 0 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ pqdt : num [1:112] 0 0 0 0 0 1 0 0 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ xhuyet : num [1:112] 0 0 0 0 1 0 0 0 0 0 ...
## ..- attr(*, "label")= chr "Xung huyết"
## ..- attr(*, "format.spss")= chr "F1.0"
## $ thamnhiem : num [1:112] 1 1 0 1 1 0 0 0 0 1 ...
## ..- attr(*, "label")= chr "Thâm nhiễm"
## ..- attr(*, "format.spss")= chr "F1.0"
## $ usui : num [1:112] 0 1 0 0 0 1 1 1 1 0 ...
## ..- attr(*, "label")= chr "U sùi"
## ..- attr(*, "format.spss")= chr "F1.0"
## $ chenep : num [1:112] 0 0 1 0 0 0 0 0 0 0 ...
## ..- attr(*, "label")= chr "Chèn ép"
## ..- attr(*, "format.spss")= chr "F1.0"
## $ cttb : num [1:112] 0 0 0 0 0 0 0 0 0 0 ...
## ..- attr(*, "label")= chr "Công thức TB"
## ..- attr(*, "format.spss")= chr "F1.0"
## $ soluong : num [1:112] 2 2 2 4 3 3 3 1 3 2 ...
## ..- attr(*, "label")= chr "Số lượng"
## ..- attr(*, "format.spss")= chr "F1.0"
## $ vtri : dbl+lbl [1:112] 2, 1, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 1, 2, 1, 1, 2, ...
## ..@ label : chr "Vị trí sinh thiết"
## ..@ format.spss: chr "F1.0"
## ..@ labels : Named num [1:2] 1 2
## .. ..- attr(*, "names")= chr [1:2] "\"Khối u nhìn thấy\"" "\"Niêm mạc/dưới niêm mạc\""
## $ kocm : num [1:112] 0 0 0 0 1 0 1 1 0 1 ...
## ..- attr(*, "label")= chr "Không cầm máu"
## ..- attr(*, "format.spss")= chr "F1.0"
## $ adre : num [1:112] 1 1 1 1 0 1 0 0 1 0 ...
## ..- attr(*, "label")= chr "Adrenalin"
## ..- attr(*, "format.spss")= chr "F1.0"
## $ muoilanh : num [1:112] 0 0 0 0 0 0 0 0 0 0 ...
## ..- attr(*, "label")= chr "Muối lạnh"
## ..- attr(*, "format.spss")= chr "F1.0"
## $ gpb : dbl+lbl [1:112] 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, ...
## ..@ label : chr "GPB"
## ..@ format.spss: chr "F1.0"
## ..@ labels : Named num [1:2] 0 1
## .. ..- attr(*, "names")= chr [1:2] "\"Không ung thư\"" "Ung thư"
## $ koK : num [1:112] NA 1 NA 1 1 NA 1 1 NA 1 ...
## ..- attr(*, "label")= chr "Không UT"
## ..- attr(*, "format.spss")= chr "F1.0"
## $ loaiK : num [1:112] 5 NA 1 NA NA 5 NA NA 5 NA ...
## ..- attr(*, "label")= chr "Loại UT"
## ..- attr(*, "format.spss")= chr "F1.0"
## $ hb : num [1:112] 12.8 12.3 11.5 10.8 12.4 11.9 6.7 12.5 7.4 12 ...
## ..- attr(*, "format.spss")= chr "F4.1"
## $ hct : num [1:112] 39.2 35.3 35.2 34.1 39.4 36.2 21 39.2 24.2 35.8 ...
## ..- attr(*, "format.spss")= chr "F4.1"
## $ plt : num [1:112] 432 426 489 331 225 358 659 480 449 81 ...
## ..- attr(*, "format.spss")= chr "F3.0"
## $ wbc : num [1:112] 11.8 18.4 7.8 37.5 10.3 ...
## ..- attr(*, "format.spss")= chr "F5.2"
## $ neu : num [1:112] 78.4 63.1 60.4 83 72.5 ...
## ..- attr(*, "format.spss")= chr "F5.2"
## $ pt : num [1:112] 80 83 106 71 70 109 75 109 97 68 ...
## ..- attr(*, "format.spss")= chr "F3.0"
## $ aptt : num [1:112] 28.9 26.3 29 28.1 26.7 26.9 33.4 30.5 30.1 28.6 ...
## ..- attr(*, "format.spss")= chr "F4.1"
## $ inr : num [1:112] 1.16 1.14 0.96 1.28 1.29 0.94 1.23 0.95 1.02 1.32 ...
## ..- attr(*, "format.spss")= chr "F4.2"
## $ urea : num [1:112] 4.9 4 4 5.5 8.6 4.2 4.7 5.5 3.8 2.7 ...
## ..- attr(*, "format.spss")= chr "F4.1"
## $ crea : num [1:112] 65 76 84 78 101 87 77 93 56 55 ...
## ..- attr(*, "format.spss")= chr "F3.0"
## $ gfr : num [1:112] 110.1 94.3 83.3 97.9 67.7 ...
## ..- attr(*, "format.spss")= chr "F6.2"
## $ ast : num [1:112] 41 8 26 24 26 21 22 16 26 659 ...
## ..- attr(*, "format.spss")= chr "F3.0"
## $ alt : num [1:112] 30 9 12 17 15 7 5 7 27 519 ...
## ..- attr(*, "format.spss")= chr "F3.0"
## $ natri : num [1:112] 132 142 122 134 137 133 134 138 134 135 ...
## ..- attr(*, "format.spss")= chr "F3.0"
## [list output truncated]
#Chạy nhóm tuổi
## install package
#install.packages("gmodels")
library(gmodels)
## Warning: package 'gmodels' was built under R version 4.4.3
CrossTable(endo$nhomtuoi, endo$gpb,
prop.r = FALSE, # phần trăm theo hàng
prop.c = TRUE, # phần trăm theo cột
prop.t = FALSE, # phần trăm theo tổng
prop.chisq = FALSE, # ẩn phần trăm đóng góp vào Chi-squared
chisq = TRUE) # thực hiện kiểm định Chi-squared
## Warning in chisq.test(t, correct = FALSE, ...): Chi-squared approximation may
## be incorrect
##
##
## Cell Contents
## |-------------------------|
## | N |
## | N / Col Total |
## |-------------------------|
##
##
## Total Observations in Table: 112
##
##
## | endo$gpb
## endo$nhomtuoi | 0 | 1 | Row Total |
## --------------|-----------|-----------|-----------|
## 1 | 7 | 2 | 9 |
## | 0.108 | 0.043 | |
## --------------|-----------|-----------|-----------|
## 2 | 38 | 21 | 59 |
## | 0.585 | 0.447 | |
## --------------|-----------|-----------|-----------|
## 3 | 20 | 24 | 44 |
## | 0.308 | 0.511 | |
## --------------|-----------|-----------|-----------|
## Column Total | 65 | 47 | 112 |
## | 0.580 | 0.420 | |
## --------------|-----------|-----------|-----------|
##
##
## Statistics for All Table Factors
##
##
## Pearson's Chi-squared test
## ------------------------------------------------------------
## Chi^2 = 5.283326 d.f. = 2 p = 0.07124271
##
##
##
#Tính tuổi trung bình
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
endo %>%
group_by(gpb) %>%
summarise(
mean_tuoi = mean(tuoi, na.rm = TRUE),
sd_tuoi = sd(tuoi, na.rm = TRUE),
n = n()
)
## # A tibble: 2 × 4
## gpb mean_tuoi sd_tuoi n
## <dbl+lbl> <dbl> <dbl> <int>
## 1 0 ["Không ung thư"] 64.1 10.6 65
## 2 1 [Ung thư] 68.3 11.3 47
#Tỷ lệ giới tính
library(janitor)
## Warning: package 'janitor' was built under R version 4.4.3
##
## Attaching package: 'janitor'
## The following objects are masked from 'package:stats':
##
## chisq.test, fisher.test
endo %>%
tabyl(gioi) %>%
adorn_pct_formatting(digits = 2) %>%
adorn_totals()
## gioi n percent
## 0 25 22.32%
## 1 87 77.68%
## Total 112 -
#tạo bảng hút thuốc lá
endo$hutthuocla <- factor(endo$hutthuocla,
levels = c(0, 1, 2),
labels = c("Không", "Đang hút", "Ngưng"))
endo$gpb <- factor(endo$gpb,
levels = c(1, 0), # chú ý: 1 là UTP, 0 là không UTP
labels = c("UTP", "Không UTP"))
library(gmodels)
CrossTable(endo$hutthuocla, endo$gpb,
prop.r = TRUE, # không tính theo hàng
prop.c = TRUE, # tính tỷ lệ theo cột (giống SPSS)
prop.t = FALSE,
prop.chisq = FALSE,
chisq = TRUE) # kiểm định chi-squared
##
##
## Cell Contents
## |-------------------------|
## | N |
## | N / Row Total |
## | N / Col Total |
## |-------------------------|
##
##
## Total Observations in Table: 112
##
##
## | endo$gpb
## endo$hutthuocla | UTP | Không UTP | Row Total |
## ----------------|-----------|-----------|-----------|
## Không | 15 | 35 | 50 |
## | 0.300 | 0.700 | 0.446 |
## | 0.319 | 0.538 | |
## ----------------|-----------|-----------|-----------|
## Đang hút | 25 | 23 | 48 |
## | 0.521 | 0.479 | 0.429 |
## | 0.532 | 0.354 | |
## ----------------|-----------|-----------|-----------|
## Ngưng | 7 | 7 | 14 |
## | 0.500 | 0.500 | 0.125 |
## | 0.149 | 0.108 | |
## ----------------|-----------|-----------|-----------|
## Column Total | 47 | 65 | 112 |
## | 0.420 | 0.580 | |
## ----------------|-----------|-----------|-----------|
##
##
## Statistics for All Table Factors
##
##
## Pearson's Chi-squared test
## ------------------------------------------------------------
## Chi^2 = 5.328096 d.f. = 2 p = 0.06966564
##
##
##
##trung bình gói-năm
library(dplyr)
endo %>%
group_by(gpb) %>%
summarise(
mean_goinam = round(mean(goinam, na.rm = TRUE), 2),
sd_goinam = round(sd(goinam, na.rm = TRUE), 2),
n = n()
)
## # A tibble: 2 × 4
## gpb mean_goinam sd_goinam n
## <fct> <dbl> <dbl> <int>
## 1 UTP 32.6 9.98 47
## 2 Không UTP 30.4 12.5 65
t.test(goinam ~ gpb, data = endo)
##
## Welch Two Sample t-test
##
## data: goinam by gpb
## t = 0.76228, df = 55.407, p-value = 0.4491
## alternative hypothesis: true difference in means between group UTP and group Không UTP is not equal to 0
## 95 percent confidence interval:
## -3.603869 8.029676
## sample estimates:
## mean in group UTP mean in group Không UTP
## 32.6129 30.4000
#Bệnh lý nền
library(dplyr)
benhnen_vars <- c("tha", "suytim", "dtd", "doiquy", "ungthu")
ketqua <- lapply(benhnen_vars, function(var) {
# Kiểm tra biến tồn tại
if (!(var %in% names(endo))) return(NULL)
# Tạo bảng chéo
tbl <- table(endo[[var]], endo$gpb)
# Kiểm định p-value
if (all(dim(tbl) == c(2, 2))) {
if (any(tbl < 5)) {
p_val <- fisher.test(tbl)$p.value
} else {
p_val <- chisq.test(tbl)$p.value
}
} else {
p_val <- NA
}
# Lấy số liệu
n_utp <- tbl["1", "UTP"]
n_k_utp <- tbl["1", "Không UTP"]
n_total <- n_utp + n_k_utp
p_utp <- 100 * n_utp / sum(tbl[, "UTP"])
p_k_utp <- 100 * n_k_utp / sum(tbl[, "Không UTP"])
p_total <- 100 * n_total / sum(tbl)
data.frame(
BenhNen = var,
N_UTP = n_utp,
P_UTP = round(p_utp, 2),
N_K_UTP = n_k_utp,
P_K_UTP = round(p_k_utp, 2),
N_Tong = n_total,
P_Tong = round(p_total, 2),
P_Value = round(p_val, 3)
)
}) # <- ĐÂY là dấu đóng bị thiếu
# Gộp kết quả lại
benhnen_bang <- bind_rows(ketqua)
# Hiển thị bảng
print(benhnen_bang)
## BenhNen N_UTP P_UTP N_K_UTP P_K_UTP N_Tong P_Tong P_Value
## 1 tha 25 53.19 29 44.62 54 48.21 0.481
## 2 suytim 1 2.13 1 1.54 2 1.79 1.000
## 3 dtd 6 12.77 11 16.92 17 15.18 0.735
## 4 doiquy 0 0.00 3 4.62 3 2.68 0.263
## 5 ungthu 6 12.77 9 13.85 15 13.39 1.000
#bảng bệnh nền hô hấp
benhhh_vars <- c("copd", "lao", "laomp")
library(dplyr)
ketqua_hh <- lapply(benhhh_vars, function(var) {
# Kiểm tra biến có tồn tại
if (!(var %in% names(endo))) return(NULL)
# Tạo bảng chéo
tbl <- table(endo[[var]], endo$gpb)
# Nếu thiếu mức hoặc không phải 2x2, xử lý riêng
if (!all(dim(tbl) == c(2, 2))) {
n_utp <- ifelse("1" %in% rownames(tbl) & "UTP" %in% colnames(tbl), tbl["1", "UTP"], 0)
n_k_utp <- ifelse("1" %in% rownames(tbl) & "Không UTP" %in% colnames(tbl), tbl["1", "Không UTP"], 0)
p_val <- NA
} else {
n_utp <- tbl["1", "UTP"]
n_k_utp <- tbl["1", "Không UTP"]
if (any(tbl < 5)) {
p_val <- fisher.test(tbl)$p.value
} else {
p_val <- chisq.test(tbl)$p.value
}
}
# Tính tỷ lệ
n_total <- n_utp + n_k_utp
p_utp <- 100 * n_utp / sum(tbl[, "UTP"])
p_k_utp <- 100 * n_k_utp / sum(tbl[, "Không UTP"])
p_total <- 100 * n_total / sum(tbl)
data.frame(
BenhLyHoHap = var,
N_UTP = n_utp,
P_UTP = round(p_utp, 2),
N_K_UTP = n_k_utp,
P_K_UTP = round(p_k_utp, 2),
N_Tong = n_total,
P_Tong = round(p_total, 2),
P_Value = round(p_val, 3)
)
})
# Gộp kết quả thành bảng
benhhh_bang <- bind_rows(ketqua_hh)
# In kết quả ra console
print(benhhh_bang)
## BenhLyHoHap N_UTP P_UTP N_K_UTP P_K_UTP N_Tong P_Tong P_Value
## 1 copd 4 8.51 6 9.23 10 8.93 1.000
## 2 lao 11 23.40 8 12.31 19 16.96 0.197
## 3 laomp 1 2.13 1 1.54 2 1.79 1.000
#bảng lí do vào viện
table(endo$ldvv)
##
## 1 2 3 4 5 6
## 31 24 7 20 19 11
endo$ldvv <- factor(endo$ldvv,
levels = c(1, 2, 3, 4, 5, 6), # nếu đang là số
labels = c("Khó thở", "Ho khạc đàm", "Ho kéo dài", "Ho ra máu", "Đau ngực", "Khác"))
library(dplyr)
ldvv_tbl <- endo %>%
group_by(ldvv, gpb) %>%
summarise(n = n(), .groups = "drop") %>%
tidyr::pivot_wider(names_from = gpb, values_from = n, values_fill = 0) %>%
mutate(
Total = UTP + `Không UTP`,
P_UTP = round(100 * UTP / sum(UTP), 2),
P_KUTP = round(100 * `Không UTP` / sum(`Không UTP`), 2),
P_Tong = round(100 * Total / sum(Total), 2)
)
ldvv_result <- ldvv_tbl %>%
select(ldvv, UTP, P_UTP, `Không UTP`, P_KUTP, Total, P_Tong) %>%
rename(
LyDoVaoVien = ldvv,
N_UTP = UTP,
TiLe_UTP = P_UTP,
N_K_UTP = `Không UTP`,
TiLe_K_UTP = P_KUTP,
N_Tong = Total,
TiLe_Tong = P_Tong
)
print(ldvv_result)
## # A tibble: 6 × 7
## LyDoVaoVien N_UTP TiLe_UTP N_K_UTP TiLe_K_UTP N_Tong TiLe_Tong
## <fct> <int> <dbl> <int> <dbl> <int> <dbl>
## 1 Khó thở 12 25.5 19 29.2 31 27.7
## 2 Ho khạc đàm 5 10.6 19 29.2 24 21.4
## 3 Ho kéo dài 4 8.51 3 4.62 7 6.25
## 4 Ho ra máu 10 21.3 10 15.4 20 17.9
## 5 Đau ngực 13 27.7 6 9.23 19 17.0
## 6 Khác 3 6.38 8 12.3 11 9.82
fisher.test(table(endo$ldvv, endo$gpb), simulate.p.value = TRUE)
##
## Fisher's Exact Test for Count Data with simulated p-value (based on
## 2000 replicates)
##
## data: table(endo$ldvv, endo$gpb)
## p-value = 0.02899
## alternative hypothesis: two.sided
#Phân tích triệu chứng toàn thân
trieuchung_vars <- c("sot", "met", "sutcan", "chanan")
library(dplyr)
trieuchung_kq <- lapply(trieuchung_vars, function(var) {
if (!(var %in% names(endo))) return(NULL)
tbl <- table(endo[[var]], endo$gpb)
# Nếu không phải bảng 2x2, xử lý thiếu dữ liệu
if (!all(dim(tbl) == c(2, 2))) {
n_utp <- ifelse("1" %in% rownames(tbl) & "UTP" %in% colnames(tbl), tbl["1", "UTP"], 0)
n_k_utp <- ifelse("1" %in% rownames(tbl) & "Không UTP" %in% colnames(tbl), tbl["1", "Không UTP"], 0)
p_val <- NA
} else {
n_utp <- tbl["1", "UTP"]
n_k_utp <- tbl["1", "Không UTP"]
p_val <- if (any(tbl < 5)) fisher.test(tbl)$p.value else chisq.test(tbl)$p.value
}
n_total <- n_utp + n_k_utp
p_utp <- 100 * n_utp / sum(tbl[, "UTP"])
p_k_utp <- 100 * n_k_utp / sum(tbl[, "Không UTP"])
p_total <- 100 * n_total / sum(tbl)
data.frame(
TrieuChung = var,
N_UTP = n_utp,
P_UTP = round(p_utp, 2),
N_K_UTP = n_k_utp,
P_K_UTP = round(p_k_utp, 2),
N_Tong = n_total,
P_Tong = round(p_total, 2),
P_Value = round(p_val, 3)
)
})
trieuchung_bang <- bind_rows(trieuchung_kq)
trieuchung_bang$TrieuChung <- recode(trieuchung_bang$TrieuChung,
"sot" = "Sốt",
"met" = "Mệt mỏi",
"sutcan" = "Sụt cân",
"chanan" = "Chán ăn")
print(trieuchung_bang)
## TrieuChung N_UTP P_UTP N_K_UTP P_K_UTP N_Tong P_Tong P_Value
## 1 Sốt 10 21.28 25 38.46 35 31.25 0.084
## 2 Mệt mỏi 1 2.13 4 6.15 5 4.46 0.397
## 3 Sụt cân 25 53.19 29 44.62 54 48.21 0.481
## 4 Chán ăn 13 27.66 7 10.77 20 17.86 0.040
#Bang triệu chứng cơ năng
trieuchung_cn_vars <- c("hokhan", "hodam", "homau", "daunguc", "khotho", "khangiong", "nuotnghen", "sohach")
library(dplyr)
kq_cn <- lapply(trieuchung_cn_vars, function(var) {
if (!(var %in% names(endo))) return(NULL)
tbl <- table(endo[[var]], endo$gpb)
if (!all(dim(tbl) == c(2, 2))) {
n_utp <- ifelse("1" %in% rownames(tbl) & "UTP" %in% colnames(tbl), tbl["1", "UTP"], 0)
n_k_utp <- ifelse("1" %in% rownames(tbl) & "Không UTP" %in% colnames(tbl), tbl["1", "Không UTP"], 0)
p_val <- NA
} else {
n_utp <- tbl["1", "UTP"]
n_k_utp <- tbl["1", "Không UTP"]
p_val <- if (any(tbl < 5)) fisher.test(tbl)$p.value else chisq.test(tbl)$p.value
}
n_total <- n_utp + n_k_utp
p_utp <- 100 * n_utp / sum(tbl[, "UTP"])
p_k_utp <- 100 * n_k_utp / sum(tbl[, "Không UTP"])
p_total <- 100 * n_total / sum(tbl)
data.frame(
TrieuChung = var,
N_UTP = n_utp,
P_UTP = round(p_utp, 2),
N_K_UTP = n_k_utp,
P_K_UTP = round(p_k_utp, 2),
N_Tong = n_total,
P_Tong = round(p_total, 2),
P_Value = round(p_val, 3)
)
})
bang_cn <- bind_rows(kq_cn)
bang_cn$TrieuChung <- recode(bang_cn$TrieuChung,
"hokhan" = "Ho khan",
"hodam" = "Ho khạc đàm",
"homau" = "Ho máu",
"daunguc" = "Đau ngực",
"khotho" = "Khó thở",
"khangiong" = "Khàn giọng",
"nuotnghen" = "Nuốt nghẹn",
"sohach" = "Sờ thấy hạch")
print(bang_cn)
## TrieuChung N_UTP P_UTP N_K_UTP P_K_UTP N_Tong P_Tong P_Value
## 1 Ho khan 8 17.02 10 15.38 18 16.07 1.000
## 2 Ho khạc đàm 27 57.45 40 61.54 67 59.82 0.810
## 3 Ho máu 11 23.40 10 15.38 21 18.75 0.408
## 4 Đau ngực 27 57.45 25 38.46 52 46.43 0.072
## 5 Khó thở 33 70.21 35 53.85 68 60.71 0.120
## 6 Khàn giọng 5 10.64 1 1.54 6 5.36 0.081
## 7 Nuốt nghẹn 4 8.51 0 0.00 4 3.57 0.029
## 8 Sờ thấy hạch 6 12.77 3 4.62 9 8.04 0.162
#Triệu chứng thực thể
trieuchung_tt_vars <- c("hc3giam", "tn", "tt", "nt", "khonggn")
# Đặt nhãn để đổi tên cột cho dễ đọc
ten_viet <- c(
hc3giam = "Hội chứng 3 giảm",
tn = "Hội chứng tắc nghẽn đường hô hấp dưới",
tt = "Hội chứng trung thất",
nt = "Hội chứng nhiễm trùng hô hấp dưới",
khonggn = "Không ghi nhận"
)
library(dplyr)
kq_tt <- lapply(trieuchung_tt_vars, function(var) {
if (!(var %in% names(endo))) return(NULL)
tbl <- table(endo[[var]], endo$gpb)
if (!all(dim(tbl) == c(2, 2))) {
n_utp <- ifelse("1" %in% rownames(tbl) & "UTP" %in% colnames(tbl), tbl["1", "UTP"], 0)
n_k_utp <- ifelse("1" %in% rownames(tbl) & "Không UTP" %in% colnames(tbl), tbl["1", "Không UTP"], 0)
p_val <- NA
} else {
n_utp <- tbl["1", "UTP"]
n_k_utp <- tbl["1", "Không UTP"]
p_val <- if (any(tbl < 5)) fisher.test(tbl)$p.value else chisq.test(tbl)$p.value
}
n_total <- n_utp + n_k_utp
p_utp <- 100 * n_utp / sum(tbl[, "UTP"])
p_k_utp <- 100 * n_k_utp / sum(tbl[, "Không UTP"])
p_total <- 100 * n_total / sum(tbl)
data.frame(
TrieuChung = var,
N_UTP = n_utp,
P_UTP = round(p_utp, 2),
N_K_UTP = n_k_utp,
P_K_UTP = round(p_k_utp, 2),
N_Tong = n_total,
P_Tong = round(p_total, 2),
P_Value = round(p_val, 3)
)
})
bang_tt <- bind_rows(kq_tt)
bang_tt$TrieuChung <- recode(bang_tt$TrieuChung, !!!ten_viet)
print(bang_tt)
## TrieuChung N_UTP P_UTP N_K_UTP P_K_UTP N_Tong
## 1 Hội chứng 3 giảm 10 21.28 14 21.54 24
## 2 Hội chứng tắc nghẽn đường hô hấp dưới 21 44.68 11 16.92 32
## 3 Hội chứng trung thất 3 6.38 2 3.08 5
## 4 Hội chứng nhiễm trùng hô hấp dưới 10 21.28 27 41.54 37
## 5 Không ghi nhận 15 31.91 15 23.08 30
## P_Tong P_Value
## 1 21.43 1.000
## 2 28.57 0.003
## 3 4.46 0.648
## 4 33.04 0.041
## 5 26.79 0.409
#Bảng hình ảnh học
hinh_anh_vars <- c("kinhmo", "hang", "notmo", "dam", "dongdac", "xep", "tdmp")
ten_hinh_anh <- c(
kinhmo = "Kính mờ",
hang = "Dạng hang",
notmo = "Nốt",
dam = "Đám mờ/khói mờ",
dongdac = "Đông đặc",
xep = "Xẹp phổi",
tdmp = "Tràn dịch màng phổi"
)
library(dplyr)
kq_ha <- lapply(hinh_anh_vars, function(var) {
if (!(var %in% names(endo))) return(NULL)
tbl <- table(endo[[var]], endo$gpb)
if (!all(dim(tbl) == c(2, 2))) {
n_utp <- ifelse("1" %in% rownames(tbl) & "UTP" %in% colnames(tbl), tbl["1", "UTP"], 0)
n_k_utp <- ifelse("1" %in% rownames(tbl) & "Không UTP" %in% colnames(tbl), tbl["1", "Không UTP"], 0)
p_val <- NA
} else {
n_utp <- tbl["1", "UTP"]
n_k_utp <- tbl["1", "Không UTP"]
p_val <- if (any(tbl < 5)) fisher.test(tbl)$p.value else chisq.test(tbl)$p.value
}
n_total <- n_utp + n_k_utp
p_utp <- 100 * n_utp / sum(tbl[, "UTP"])
p_k_utp <- 100 * n_k_utp / sum(tbl[, "Không UTP"])
p_total <- 100 * n_total / sum(tbl)
data.frame(
HinhAnh = var,
N_UTP = n_utp,
P_UTP = round(p_utp, 2),
N_K_UTP = n_k_utp,
P_K_UTP = round(p_k_utp, 2),
N_Tong = n_total,
P_Tong = round(p_total, 2),
P_Value = round(p_val, 3)
)
})
bang_hinh_anh <- bind_rows(kq_ha)
bang_hinh_anh$HinhAnh <- recode(bang_hinh_anh$HinhAnh, !!!ten_hinh_anh)
print(bang_hinh_anh)
## HinhAnh N_UTP P_UTP N_K_UTP P_K_UTP N_Tong P_Tong P_Value
## 1 Kính mờ 11 23.40 11 16.92 22 19.64 0.541
## 2 Dạng hang 1 2.13 10 15.38 11 9.82 0.024
## 3 Nốt 4 8.51 21 32.31 25 22.32 0.003
## 4 Đám mờ/khói mờ 47 100.00 52 80.00 99 88.39 0.001
## 5 Đông đặc 14 29.79 23 35.38 37 33.04 0.676
## 6 Xẹp phổi 8 17.02 9 13.85 17 15.18 0.845
## 7 Tràn dịch màng phổi 17 36.17 24 36.92 41 36.61 1.000
vitri_vars <- c("trenp", "giuap", "duoip", "trent", "duoit")
ten_vitri <- c(
trenp = "Thùy trên (P)",
giuap = "Thùy giữa (P)",
duoip = "Thùy dưới (P)",
trent = "Thùy trên (T)",
duoit = "Thùy dưới (T)"
)
library(dplyr)
kq_vitri <- lapply(vitri_vars, function(var) {
if (!(var %in% names(endo))) return(NULL)
tbl <- table(endo[[var]], endo$gpb)
if (!all(dim(tbl) == c(2, 2))) {
n_utp <- ifelse("1" %in% rownames(tbl) & "UTP" %in% colnames(tbl), tbl["1", "UTP"], 0)
n_k_utp <- ifelse("1" %in% rownames(tbl) & "Không UTP" %in% colnames(tbl), tbl["1", "Không UTP"], 0)
p_val <- NA
} else {
n_utp <- tbl["1", "UTP"]
n_k_utp <- tbl["1", "Không UTP"]
p_val <- if (any(tbl < 5)) fisher.test(tbl)$p.value else chisq.test(tbl)$p.value
}
n_total <- n_utp + n_k_utp
p_utp <- 100 * n_utp / sum(tbl[, "UTP"])
p_k_utp <- 100 * n_k_utp / sum(tbl[, "Không UTP"])
p_total <- 100 * n_total / sum(tbl)
data.frame(
ViTri = var,
N_UTP = n_utp,
P_UTP = round(p_utp, 2),
N_K_UTP = n_k_utp,
P_K_UTP = round(p_k_utp, 2),
N_Tong = n_total,
P_Tong = round(p_total, 2),
P_Value = round(p_val, 3)
)
})
bang_vitri <- bind_rows(kq_vitri)
bang_vitri$ViTri <- recode(bang_vitri$ViTri, !!!ten_vitri)
print(bang_vitri)
## ViTri N_UTP P_UTP N_K_UTP P_K_UTP N_Tong P_Tong P_Value
## 1 Thùy trên (P) 16 34.04 34 52.31 50 44.64 0.084
## 2 Thùy giữa (P) 10 21.28 17 26.15 27 24.11 0.710
## 3 Thùy dưới (P) 7 14.89 9 13.85 16 14.29 1.000
## 4 Thùy trên (T) 20 42.55 15 23.08 35 31.25 0.047
## 5 Thùy dưới (T) 8 17.02 14 21.54 22 19.64 0.724
# Biến Bờ tổn thương
endo1 <- read_sav("D:/Deskop/Endobronchial_biopsy/Xử lý số liệu/RStudio/dulieuchuan.sav")
endo$bo <- endo1$bo
endo$bo1 <- endo$bo
# Nếu chưa là factor
endo$bo1 <- factor(endo$bo1,
levels = c(1, 2, 3),
labels = c("Bờ tròn đều", "Bờ đa cung", "Bờ tua gai"))
library(dplyr)
library(tidyr)
bo1_tbl <- endo %>%
group_by(bo1, gpb) %>%
summarise(n = n(), .groups = "drop") %>%
pivot_wider(names_from = gpb, values_from = n, values_fill = 0) %>%
mutate(
N_Tong = UTP + `Không UTP`,
P_UTP = round(100 * UTP / sum(UTP), 2),
P_KUTP = round(100 * `Không UTP` / sum(`Không UTP`), 2),
P_Tong = round(100 * N_Tong / sum(N_Tong), 2)
) %>%
rename(
Bo1 = bo1,
N_UTP = UTP,
N_K_UTP = `Không UTP`,
TiLe_UTP = P_UTP,
TiLe_K_UTP = P_KUTP,
TiLe_Tong = P_Tong
)
bo1_p <- chisq.test(table(endo$bo1, endo$gpb))$p.value
## Warning in stats::chisq.test(x, y, ...): Chi-squared approximation may be
## incorrect
bo1_tbl$P_Value <- round(bo1_p, 3)
print(bo1_tbl)
## # A tibble: 3 × 8
## Bo1 N_UTP N_K_UTP N_Tong TiLe_UTP TiLe_K_UTP TiLe_Tong P_Value
## <fct> <int> <int> <int> <dbl> <dbl> <dbl> <dbl>
## 1 Bờ tròn đều 1 8 9 2.13 12.3 8.04 0
## 2 Bờ đa cung 17 40 57 36.2 61.5 50.9 0
## 3 Bờ tua gai 29 17 46 61.7 26.2 41.1 0
tbl_bo1 <- table(endo$bo1, endo$gpb)
fisher_p <- fisher.test(tbl_bo1, simulate.p.value = TRUE, B = 10000)$p.value
bo1_tbl$P_Value <- round(fisher_p, 3)
print(bo1_tbl)
## # A tibble: 3 × 8
## Bo1 N_UTP N_K_UTP N_Tong TiLe_UTP TiLe_K_UTP TiLe_Tong P_Value
## <fct> <int> <int> <int> <dbl> <dbl> <dbl> <dbl>
## 1 Bờ tròn đều 1 8 9 2.13 12.3 8.04 0
## 2 Bờ đa cung 17 40 57 36.2 61.5 50.9 0
## 3 Bờ tua gai 29 17 46 61.7 26.2 41.1 0
#Tạo biến hạch
endo$hach1 <- factor(endo$hach, levels = c(0, 1), labels = c("Không", "Có"))
# Tạo bảng tần số và tỷ lệ
library(dplyr)
library(tidyr)
hach1_tbl <- endo %>%
group_by(hach1, gpb) %>%
summarise(n = n(), .groups = "drop") %>%
pivot_wider(names_from = gpb, values_from = n, values_fill = 0) %>%
mutate(
N_Tong = UTP + `Không UTP`,
P_UTP = round(100 * UTP / sum(UTP), 2),
P_KUTP = round(100 * `Không UTP` / sum(`Không UTP`), 2),
P_Tong = round(100 * N_Tong / sum(N_Tong), 2)
) %>%
rename(
Hach = hach1,
N_UTP = UTP,
N_K_UTP = `Không UTP`,
TiLe_UTP = P_UTP,
TiLe_K_UTP = P_KUTP,
TiLe_Tong = P_Tong
)
p_hach1 <- fisher.test(table(endo$hach1, endo$gpb))$p.value
hach1_tbl$P_Value <- round(p_hach1, 3)
print(hach1_tbl)
## # A tibble: 2 × 8
## Hach N_UTP N_K_UTP N_Tong TiLe_UTP TiLe_K_UTP TiLe_Tong P_Value
## <fct> <int> <int> <int> <dbl> <dbl> <dbl> <dbl>
## 1 Không 21 41 62 44.7 63.1 55.4 0.058
## 2 Có 26 24 50 55.3 36.9 44.6 0.058
library(haven)
endo2 <- read_sav("D:/Deskop/Endobronchial_biopsy/Xử lý số liệu/RStudio/dulieuchuan - Copy.sav")
table(endo2$khongxl, endo2$gpb)
##
## 0 1
## 0 26 21
## 1 39 26
table(endo2$xlmp, endo2$gpb)
##
## 0 1
## 0 55 35
## 1 10 12
table(endo2$xltn, endo2$gpb)
##
## 0 1
## 0 58 42
## 1 7 5
table(endo2$xltt, endo2$gpb)
##
## 0 1
## 0 53 31
## 1 12 16
xamlan_vars <- c("khongxl", "xlmp", "xltn", "xltt")
# Tính p-value cho từng biến
p_values <- sapply(xamlan_vars, function(var) {
tbl <- table(endo2[[var]], endo2$gpb)
# Kiểm tra bảng đúng 2x2
if (!all(dim(tbl) == c(2, 2))) return(NA)
# Dùng Fisher khi có ô < 5, còn lại dùng Chi-squared
if (any(tbl < 5)) {
fisher.test(tbl)$p.value
} else {
chisq.test(tbl)$p.value
}
})
# Làm tròn giá trị p
p_values <- round(p_values, 3)
# Hiển thị kết quả
data.frame(
Bien = xamlan_vars,
P_value = p_values
)
## Bien P_value
## khongxl khongxl 0.763
## xlmp xlmp 0.274
## xltn xltn 1.000
## xltt xltt 0.097
# Kiểm tra phân phối của từng biến xâm lấn theo nhóm `gpb`
table(endo2$khongxl, endo$gpb)
##
## UTP Không UTP
## 0 21 26
## 1 26 39
table(endo2$xlmp, endo$gpb)
##
## UTP Không UTP
## 0 35 55
## 1 12 10
table(endo2$xltn, endo$gpb)
##
## UTP Không UTP
## 0 42 58
## 1 5 7
table(endo2$xltt, endo$gpb)
##
## UTP Không UTP
## 0 31 53
## 1 16 12
# Các biến xâm lấn
xamlan_vars <- c("khongxl", "xlmp", "xltn", "xltt")
# Tính p-value cho từng biến xâm lấn
p_values <- sapply(xamlan_vars, function(var) {
tbl <- table(endo2[[var]], endo$gpb) # Sử dụng gpb từ endo
# Kiểm tra nếu bảng có đủ 2x2
if (!all(dim(tbl) == c(2, 2))) return(NA)
# Dùng Fisher khi có ô < 5, còn lại dùng Chi-squared
if (any(tbl < 5)) {
fisher.test(tbl)$p.value
} else {
chisq.test(tbl)$p.value
}
})
# Làm tròn giá trị p
p_values <- round(p_values, 3)
# Hiển thị kết quả
data.frame(
Bien = xamlan_vars,
P_value = p_values
)
## Bien P_value
## khongxl khongxl 0.763
## xlmp xlmp 0.274
## xltn xltn 1.000
## xltt xltt 0.097
library(dplyr)
# Danh sách các biến xâm lấn nằm trong endo2
xamlan_vars <- c("khongxl", "xlmp", "xltn", "xltt")
# Nhãn tiếng Việt
xamlan_labels <- c(
khongxl = "Không có xâm lấn",
xlmp = "Xâm lấn màng phổi",
xltn = "Xâm lấn thành ngực",
xltt = "Xâm lấn trung thất"
)
# Tạo bảng mô tả cho từng biến
bang_xamlan <- lapply(xamlan_vars, function(var) {
tbl <- table(endo2[[var]], endo$gpb) # Lưu ý: biến trong endo2, gpb trong endo
if (!all(dim(tbl) == c(2, 2))) {
return(NULL)
}
n_utp <- tbl["1", "UTP"]
n_k_utp <- tbl["1", "Không UTP"]
n_total <- n_utp + n_k_utp
p_utp <- 100 * n_utp / sum(tbl[, "UTP"])
p_k_utp <- 100 * n_k_utp / sum(tbl[, "Không UTP"])
p_total <- 100 * n_total / sum(tbl)
p_val <- if (any(tbl < 5)) fisher.test(tbl)$p.value else chisq.test(tbl)$p.value
data.frame(
DacDiem = var,
N_UTP = n_utp,
TiLe_UTP = round(p_utp, 2),
N_K_UTP = n_k_utp,
TiLe_K_UTP = round(p_k_utp, 2),
N_Tong = n_total,
TiLe_Tong = round(p_total, 2),
P_Value = round(p_val, 3)
)
}) %>% bind_rows()
# Gán nhãn tiếng Việt
bang_xamlan$DacDiem <- recode(bang_xamlan$DacDiem, !!!xamlan_labels)
# Hiển thị kết quả
print(bang_xamlan)
## DacDiem N_UTP TiLe_UTP N_K_UTP TiLe_K_UTP N_Tong TiLe_Tong P_Value
## 1 Không có xâm lấn 26 55.32 39 60.00 65 58.04 0.763
## 2 Xâm lấn màng phổi 12 25.53 10 15.38 22 19.64 0.274
## 3 Xâm lấn thành ngực 5 10.64 7 10.77 12 10.71 1.000
## 4 Xâm lấn trung thất 16 34.04 12 18.46 28 25.00 0.097
#Bang mo ta dac diem noi soi phe quan
# Tên biến trong endo2
vitri_vars <- c("carina", "gocp", "goct", "pqtp", "pqgp", "pqdp", "pqtg", "pqtt", "pqdt")
# Nhãn mô tả tiếng Việt
vitri_labels <- c(
carina = "Carina",
gocp = "Phế quản góc P",
goct = "Phế quản góc T",
pqtp = "Phế quản thùy trên P",
pqgp = "Phế quản thùy giữa P",
pqdp = "Phế quản thùy dưới P",
pqtg = "Phế quản trung gian",
pqtt = "Phế quản thùy trên T",
pqdt = "Phế quản thùy dưới T"
)
library(dplyr)
bang_vitri <- lapply(vitri_vars, function(var) {
tbl <- table(endo2[[var]], endo$gpb) # biến trong endo2, gpb trong endo
# Chỉ thực hiện khi bảng có đủ 2x2
if (!all(dim(tbl) == c(2, 2))) return(NULL)
n_utp <- tbl["1", "UTP"]
n_k_utp <- tbl["1", "Không UTP"]
n_total <- n_utp + n_k_utp
p_utp <- 100 * n_utp / sum(tbl[, "UTP"])
p_k_utp <- 100 * n_k_utp / sum(tbl[, "Không UTP"])
p_total <- 100 * n_total / sum(tbl)
p_val <- if (any(tbl < 5)) fisher.test(tbl)$p.value else chisq.test(tbl)$p.value
data.frame(
ViTriTonThuong = var,
N_UTP = n_utp,
TiLe_UTP = round(p_utp, 2),
N_K_UTP = n_k_utp,
TiLe_K_UTP = round(p_k_utp, 2),
N_Tong = n_total,
TiLe_Tong = round(p_total, 2),
P_Value = round(p_val, 3)
)
}) %>% bind_rows()
# Gán nhãn tiếng Việt
bang_vitri$ViTriTonThuong <- recode(bang_vitri$ViTriTonThuong, !!!vitri_labels)
library(dplyr)
bang_vitri <- lapply(vitri_vars, function(var) {
tbl <- table(endo2[[var]], endo$gpb) # biến trong endo2, gpb trong endo
# Chỉ thực hiện khi bảng có đủ 2x2
if (!all(dim(tbl) == c(2, 2))) return(NULL)
n_utp <- tbl["1", "UTP"]
n_k_utp <- tbl["1", "Không UTP"]
n_total <- n_utp + n_k_utp
p_utp <- 100 * n_utp / sum(tbl[, "UTP"])
p_k_utp <- 100 * n_k_utp / sum(tbl[, "Không UTP"])
p_total <- 100 * n_total / sum(tbl)
p_val <- if (any(tbl < 5)) fisher.test(tbl)$p.value else chisq.test(tbl)$p.value
data.frame(
ViTriTonThuong = var,
N_UTP = n_utp,
TiLe_UTP = round(p_utp, 2),
N_K_UTP = n_k_utp,
TiLe_K_UTP = round(p_k_utp, 2),
N_Tong = n_total,
TiLe_Tong = round(p_total, 2),
P_Value = round(p_val, 3)
)
}) %>% bind_rows()
# Gán nhãn tiếng Việt
bang_vitri$ViTriTonThuong <- recode(bang_vitri$ViTriTonThuong, !!!vitri_labels)
print(bang_vitri)
## ViTriTonThuong N_UTP TiLe_UTP N_K_UTP TiLe_K_UTP N_Tong TiLe_Tong
## 1 Carina 2 4.26 3 4.62 5 4.46
## 2 Phế quản góc P 4 8.51 7 10.77 11 9.82
## 3 Phế quản góc T 8 17.02 5 7.69 13 11.61
## 4 Phế quản thùy trên P 19 40.43 31 47.69 50 44.64
## 5 Phế quản thùy giữa P 1 2.13 4 6.15 5 4.46
## 6 Phế quản thùy dưới P 6 12.77 9 13.85 15 13.39
## 7 Phế quản trung gian 7 14.89 13 20.00 20 17.86
## 8 Phế quản thùy trên T 16 34.04 5 7.69 21 18.75
## 9 Phế quản thùy dưới T 6 12.77 16 24.62 22 19.64
## P_Value
## 1 1.000
## 2 0.759
## 3 0.222
## 4 0.568
## 5 0.397
## 6 1.000
## 7 0.655
## 8 0.001
## 9 0.188
# Danh sách biến trong endo
tonthuong_vars <- c("xhuyet", "thamnhiem", "usui", "chenep")
# Nhãn tiếng Việt tương ứng
tonthuong_labels <- c(
xhuyet = "Xung huyết niêm mạc",
thamnhiem = "Thâm nhiễm",
usui = "U sùi",
chenep = "Chèn ép từ ngoài vào"
)
library(dplyr)
bang_tonthuong <- lapply(tonthuong_vars, function(var) {
tbl <- table(endo[[var]], endo$gpb)
# Chỉ thực hiện nếu đủ 2x2
if (!all(dim(tbl) == c(2, 2))) return(NULL)
n_utp <- tbl["1", "UTP"]
n_k_utp <- tbl["1", "Không UTP"]
n_total <- n_utp + n_k_utp
p_utp <- 100 * n_utp / sum(tbl[, "UTP"])
p_k_utp <- 100 * n_k_utp / sum(tbl[, "Không UTP"])
p_total <- 100 * n_total / sum(tbl)
p_val <- if (any(tbl < 5)) fisher.test(tbl)$p.value else chisq.test(tbl)$p.value
data.frame(
DacDiemTonThuong = var,
N_UTP = n_utp,
TiLe_UTP = round(p_utp, 2),
N_K_UTP = n_k_utp,
TiLe_K_UTP = round(p_k_utp, 2),
N_Tong = n_total,
TiLe_Tong = round(p_total, 2),
P_Value = round(p_val, 3)
)
}) %>% bind_rows()
# Gán lại nhãn tiếng Việt
bang_tonthuong$DacDiemTonThuong <- recode(bang_tonthuong$DacDiemTonThuong, !!!tonthuong_labels)
print(bang_tonthuong)
## DacDiemTonThuong N_UTP TiLe_UTP N_K_UTP TiLe_K_UTP N_Tong TiLe_Tong
## 1 Xung huyết niêm mạc 12 25.53 18 27.69 30 26.79
## 2 Thâm nhiễm 31 65.96 53 81.54 84 75.00
## 3 U sùi 30 63.83 22 33.85 52 46.43
## 4 Chèn ép từ ngoài vào 3 6.38 9 13.85 12 10.71
## P_Value
## 1 0.969
## 2 0.097
## 3 0.003
## 4 0.236
table (endo$soluong)
##
## 1 2 3 4 5
## 4 44 53 10 1
library(dplyr)
library(tidyr)
# Tạo bảng đếm
soluong_tbl <- endo %>%
group_by(soluong, gpb) %>%
summarise(n = n(), .groups = "drop") %>%
pivot_wider(names_from = gpb, values_from = n, values_fill = 0) %>%
mutate(
Tong = UTP + `Không UTP`,
TiLe_UTP = round(100 * UTP / sum(UTP), 2),
TiLe_KUTP = round(100 * `Không UTP` / sum(`Không UTP`), 2),
TiLe_Tong = round(100 * Tong / sum(Tong), 2)
) %>%
rename(
SoLuong = soluong,
N_UTP = UTP,
N_K_UTP = `Không UTP`,
N_Tong = Tong
)
# Tạo bảng chéo
soluong_chisq_tbl <- table(endo$soluong, endo$gpb)
# Tính p-value
p_val_soluong <- if (any(soluong_chisq_tbl < 5)) {
fisher.test(soluong_chisq_tbl, simulate.p.value = TRUE)$p.value
} else {
chisq.test(soluong_chisq_tbl)$p.value
}
# Thêm p-value vào bảng
soluong_tbl$P_value <- round(p_val_soluong, 3)
print(soluong_tbl)
## # A tibble: 5 × 8
## SoLuong N_UTP N_K_UTP N_Tong TiLe_UTP TiLe_KUTP TiLe_Tong P_value
## <dbl> <int> <int> <int> <dbl> <dbl> <dbl> <dbl>
## 1 1 1 3 4 2.13 4.62 3.57 0.134
## 2 2 23 21 44 48.9 32.3 39.3 0.134
## 3 3 17 36 53 36.2 55.4 47.3 0.134
## 4 4 6 4 10 12.8 6.15 8.93 0.134
## 5 5 0 1 1 0 1.54 0.89 0.134
library(haven)
# Chuyển về numeric trước, sau đó gán nhãn thủ công
endo$vtri1 <- as.numeric(endo$vtri)
# Gán nhãn
endo$vtri1 <- factor(endo$vtri1,
levels = c(1, 2),
labels = c("Khối u nhìn thấy", "Niêm mạc / dưới niêm mạc"))
library(dplyr)
library(tidyr)
# Tạo bảng tần số và tỷ lệ theo nhóm gpb
vtri1_tbl <- endo %>%
group_by(vtri1, gpb) %>%
summarise(n = n(), .groups = "drop") %>%
pivot_wider(names_from = gpb, values_from = n, values_fill = 0) %>%
mutate(
N_Tong = UTP + `Không UTP`,
TiLe_UTP = round(100 * UTP / sum(UTP), 2),
TiLe_KUTP = round(100 * `Không UTP` / sum(`Không UTP`), 2),
TiLe_Tong = round(100 * N_Tong / sum(N_Tong), 2)
) %>%
rename(
ViTriSinhThiet = vtri1,
N_UTP = UTP,
N_K_UTP = `Không UTP`
)
# Chuyển vtri từ 1/2 → 1/0
endo$vtri_bin <- ifelse(endo$vtri == 1, 1, 0)
# Kiểm tra lại
table(endo$vtri, endo$vtri_bin)
##
## 0 1
## 1 0 37
## 2 75 0
endo$vtri_bin <- factor(endo$vtri_bin,
levels = c(0, 1),
labels = c("Niêm mạc / dưới niêm mạc", "Khối u nhìn thấy"))
tbl <- table(endo$vtri_bin, endo$gpb)
# Tính p-value
p_vtri_bin <- if (any(tbl < 5)) fisher.test(tbl)$p.value else chisq.test(tbl)$p.value
# In kết quả kiểm định
print(tbl)
##
## UTP Không UTP
## Niêm mạc / dưới niêm mạc 20 55
## Khối u nhìn thấy 27 10
print(p_vtri_bin)
## [1] 7.929424e-06
#Cấm máu qua nội soi
# Các biến cần mô tả
cammau_vars <- c("kocm", "adre", "muoilanh")
# Nhãn tiếng Việt tương ứng
cammau_labels <- c(
kocm = "Không cầm máu",
adre = "Adrenalin",
muoilanh = "Muối lanh"
)
library(dplyr)
library(tidyr)
# Tính tần số và tỷ lệ cho từng biến
bang_cammau <- lapply(cammau_vars, function(var) {
tbl <- table(endo[[var]], endo$gpb) # Lấy dữ liệu từ `endo`, `gpb` trong `endo`
# Kiểm tra bảng có đủ 2x2
if (!all(dim(tbl) == c(2, 2))) return(NULL)
n_utp <- tbl["1", "UTP"]
n_k_utp <- tbl["1", "Không UTP"]
n_total <- n_utp + n_k_utp
p_utp <- 100 * n_utp / sum(tbl[, "UTP"])
p_k_utp <- 100 * n_k_utp / sum(tbl[, "Không UTP"])
p_total <- 100 * n_total / sum(tbl)
p_val <- if (any(tbl < 5)) fisher.test(tbl)$p.value else chisq.test(tbl)$p.value
data.frame(
PhuongPhapCammau = var,
N_UTP = n_utp,
TiLe_UTP = round(p_utp, 2),
N_K_UTP = n_k_utp,
TiLe_K_UTP = round(p_k_utp, 2),
N_Tong = n_total,
TiLe_Tong = round(p_total, 2),
P_Value = round(p_val, 3)
)
}) %>% bind_rows()
# Gán lại nhãn tiếng Việt cho biến
bang_cammau$PhuongPhapCammau <- recode(bang_cammau$PhuongPhapCammau, !!!cammau_labels)
print(bang_cammau)
## PhuongPhapCammau N_UTP TiLe_UTP N_K_UTP TiLe_K_UTP N_Tong TiLe_Tong P_Value
## 1 Không cầm máu 13 27.66 21 32.31 34 30.36 0.749
## 2 Adrenalin 29 61.70 40 61.54 69 61.61 1.000
## 3 Muối lanh 6 12.77 4 6.15 10 8.93 0.316