Lấy số liệu

ob=read.csv("D:\\Download\\Obesity data.csv")
head(ob)
##   id gender height weight  bmi age WBBMC wbbmd   fat  lean pcfat hypertension
## 1  1      F    150     49 21.8  53  1312  0.88 17802 28600  37.3            0
## 2  2      M    165     52 19.1  65  1309  0.84  8381 40229  16.8            1
## 3  3      F    157     57 23.1  64  1230  0.84 19221 36057  34.0            1
## 4  4      F    156     53 21.8  56  1171  0.80 17472 33094  33.8            1
## 5  5      M    160     51 19.9  54  1681  0.98  7336 40621  14.8            0
## 6  6      F    153     47 20.1  52  1358  0.91 14904 30068  32.2            1
##   diabetes
## 1        1
## 2        0
## 3        0
## 4        0
## 5        0
## 6        0

Bài 1 ngày 2

Tạo bảng table1

library(table1)
## 
## Attaching package: 'table1'
## The following objects are masked from 'package:base':
## 
##     units, units<-
table1(~age+ gender + height + weight +pcfat + hypertension +diabetes, data=ob)
Overall
(N=1217)
age
Mean (SD) 47.2 (17.3)
Median [Min, Max] 48.0 [13.0, 88.0]
gender
F 862 (70.8%)
M 355 (29.2%)
height
Mean (SD) 157 (7.98)
Median [Min, Max] 155 [136, 185]
weight
Mean (SD) 55.1 (9.40)
Median [Min, Max] 54.0 [34.0, 95.0]
pcfat
Mean (SD) 31.6 (7.18)
Median [Min, Max] 32.4 [9.20, 48.4]
hypertension
Mean (SD) 0.507 (0.500)
Median [Min, Max] 1.00 [0, 1.00]
diabetes
Mean (SD) 0.111 (0.314)
Median [Min, Max] 0 [0, 1.00]

Xử lý biến số định tính hypertension và diabetes

ob$hyper = as.factor(ob$hypertension)
ob$diab = as.factor(ob$diabetes)
table1(~age+ gender + height + weight +pcfat + hypertension + hyper +diabetes + diab, data=ob)
Overall
(N=1217)
age
Mean (SD) 47.2 (17.3)
Median [Min, Max] 48.0 [13.0, 88.0]
gender
F 862 (70.8%)
M 355 (29.2%)
height
Mean (SD) 157 (7.98)
Median [Min, Max] 155 [136, 185]
weight
Mean (SD) 55.1 (9.40)
Median [Min, Max] 54.0 [34.0, 95.0]
pcfat
Mean (SD) 31.6 (7.18)
Median [Min, Max] 32.4 [9.20, 48.4]
hypertension
Mean (SD) 0.507 (0.500)
Median [Min, Max] 1.00 [0, 1.00]
hyper
0 600 (49.3%)
1 617 (50.7%)
diabetes
Mean (SD) 0.111 (0.314)
Median [Min, Max] 0 [0, 1.00]
diab
0 1082 (88.9%)
1 135 (11.1%)

So sánh khác biệt giữa Nam và Nữ

table1(~age + height + weight +pcfat + hypertension + hyper +diabetes + diab|gender, data=ob)
F
(N=862)
M
(N=355)
Overall
(N=1217)
age
Mean (SD) 48.6 (16.4) 43.7 (18.8) 47.2 (17.3)
Median [Min, Max] 49.0 [14.0, 85.0] 44.0 [13.0, 88.0] 48.0 [13.0, 88.0]
height
Mean (SD) 153 (5.55) 165 (6.73) 157 (7.98)
Median [Min, Max] 153 [136, 170] 165 [146, 185] 155 [136, 185]
weight
Mean (SD) 52.3 (7.72) 62.0 (9.59) 55.1 (9.40)
Median [Min, Max] 51.0 [34.0, 95.0] 62.0 [38.0, 95.0] 54.0 [34.0, 95.0]
pcfat
Mean (SD) 34.7 (5.19) 24.2 (5.76) 31.6 (7.18)
Median [Min, Max] 34.7 [14.6, 48.4] 24.6 [9.20, 39.0] 32.4 [9.20, 48.4]
hypertension
Mean (SD) 0.501 (0.500) 0.521 (0.500) 0.507 (0.500)
Median [Min, Max] 1.00 [0, 1.00] 1.00 [0, 1.00] 1.00 [0, 1.00]
hyper
0 430 (49.9%) 170 (47.9%) 600 (49.3%)
1 432 (50.1%) 185 (52.1%) 617 (50.7%)
diabetes
Mean (SD) 0.118 (0.323) 0.0930 (0.291) 0.111 (0.314)
Median [Min, Max] 0 [0, 1.00] 0 [0, 1.00] 0 [0, 1.00]
diab
0 760 (88.2%) 322 (90.7%) 1082 (88.9%)
1 102 (11.8%) 33 (9.3%) 135 (11.1%)

Đánh giá sự khác biệt giữa hai nhóm định tính Nam và Nữ

library(compareGroups)
createTable(compareGroups(gender ~ age + height + weight +pcfat + hyper + diab,data=ob))
## 
## --------Summary descriptives table by 'gender'---------
## 
## ________________________________________ 
##             F           M      p.overall 
##           N=862       N=355              
## ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ 
## age    48.6 (16.4) 43.7 (18.8)  <0.001   
## height 153 (5.55)  165 (6.73)   <0.001   
## weight 52.3 (7.72) 62.0 (9.59)  <0.001   
## pcfat  34.7 (5.19) 24.2 (5.76)  <0.001   
## hyper:                           0.569   
##     0  430 (49.9%) 170 (47.9%)           
##     1  432 (50.1%) 185 (52.1%)           
## diab:                            0.238   
##     0  760 (88.2%) 322 (90.7%)           
##     1  102 (11.8%) 33 (9.30%)            
## ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

Phân tích:

Tuổi, chiều cao,cân nặng, tỷ lệ nhiễm mỡ (kiểm định T.tes)t cho thấy Có sự khác biệt giữa Nam và Nữ

Cao huyết áp và tiểu đường (kiểm định Chi-square): Không có sự khác biệt

Bài 2 ngày 2

Tạo biến A, B. Đánh giá có phải phân phối chuẩn?

A=c(14, 4, 10, 6, 3, 11, 12)
B=c(16, 17, 13, 12, 7, 16, 11, 8, 7)
shapiro.test(A)
## 
##  Shapiro-Wilk normality test
## 
## data:  A
## W = 0.92541, p-value = 0.5126
shapiro.test(B)
## 
##  Shapiro-Wilk normality test
## 
## data:  B
## W = 0.89641, p-value = 0.2319

Kết luận: A và B là phân phối chuẩn

Kiểm tra T.test

t.test(A,B)
## 
##  Welch Two Sample t-test
## 
## data:  A and B
## t = -1.6, df = 12.554, p-value = 0.1345
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -7.813114  1.178194
## sample estimates:
## mean of x mean of y 
##  8.571429 11.888889

Kết luận: Kiểm định T.test: p-value=0.1345 cho thấy A và B giống nhu

Sử dụng simpleboot và boot

Đánh giá sự khác biệt theo trung bình - mean

library(simpleboot); library(boot)
## Simple Bootstrap Routines (1.1-8)
b= two.boot(A,B,mean,R=500)
boot.ci(b)
## Warning in boot.ci(b): bootstrap variances needed for studentized intervals
## BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
## Based on 500 bootstrap replicates
## 
## CALL : 
## boot.ci(boot.out = b)
## 
## Intervals : 
## Level      Normal              Basic         
## 95%   (-7.182,  0.317 )   (-6.977,  0.349 )  
## 
## Level     Percentile            BCa          
## 95%   (-6.984,  0.342 )   (-7.382,  0.039 )  
## Calculations and Intervals on Original Scale
## Some BCa intervals may be unstable

Kết luận: với độ tin cậy 95%, sự khác biệt không rõ ràng. A giống B

Đánh giá sự khác biệt theo trung vị - median

b= two.boot(A,B,median,R=500)
boot.ci(b)
## Warning in boot.ci(b): bootstrap variances needed for studentized intervals
## BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
## Based on 500 bootstrap replicates
## 
## CALL : 
## boot.ci(boot.out = b)
## 
## Intervals : 
## Level      Normal              Basic         
## 95%   (-7.894,  6.206 )   (-8.000,  6.000 )  
## 
## Level     Percentile            BCa          
## 95%   (-10,   4 )   (-10,   3 )  
## Calculations and Intervals on Original Scale
## Some BCa intervals may be unstable

Kết luận: với độ tin cậy 95%, sự khác biệt không rõ ràng. A giống B

Đánh giá sự khác biệt theo trung vị sử dụng wilcox.test

wilcox.test(A, B, alternative = "two.sided")
## Warning in wilcox.test.default(A, B, alternative = "two.sided"): cannot compute
## exact p-value with ties
## 
##  Wilcoxon rank sum test with continuity correction
## 
## data:  A and B
## W = 17, p-value = 0.1372
## alternative hypothesis: true location shift is not equal to 0

Kết luận: p=0.1372>0.05: A giống B

boxplot(A, B,
        names = c("Nhóm A", "Nhóm B"),
        col = c("lightblue", "lightgreen"),
        main = "So sánh trung vị giữa 2 nhóm (Wilcoxon test)")