library(readr) ob <- read_csv(“C:/Users/ADMIN/Desktop/Khóa học NCKH 07.09.2024/obesity data.csv”) View(ob) ## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
table1(~ age + bmi + WBBMC + Wbmd + fat + lean + as.factor(hypertension)
+ as.factor(diabetes)| ) table1(~ age + gender + bmi + WBBMC + wbbmd +
fat + lean + hypertension + diabetes, data = ob) ## Chiều 9/9/2024
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%) bmi Mean (SD) 22.4 (3.06) Median [Min, Max]
22.2 [14.5, 37.1] WBBMC
Mean (SD) 1720 (363) Median [Min, Max] 1710 [695, 3040] wbbmd
Mean (SD) 1.01 (0.113) Median [Min, Max] 1.01 [0.650, 1.35] fat Mean
(SD) 17300 (5210) Median [Min, Max] 17000 [4280, 40800] lean
Mean (SD) 35500 (7030) Median [Min, Max] 33600 [19100, 63100]
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] ###table1(~ age +
bmi + WBBMC + wbbmd + fat + lean + as.factor(hypertension) +
as.factor(diabetes) | 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] bmi
Mean (SD) 22.3 (3.05) 22.7 (3.04) 22.4 (3.06) Median [Min, Max] 22.1
[15.2, 37.1] 22.5 [14.5, 34.7] 22.2 [14.5, 37.1] WBBMC
Mean (SD) 1600 (293) 2030 (336) 1720 (363) Median [Min, Max] 1610 [695,
2660] 2030 [1190, 3040] 1710 [695, 3040] wbbmd
Mean (SD) 0.988 (0.111) 1.06 (0.101) 1.01 (0.113) Median [Min, Max]
0.990 [0.650, 1.35] 1.06 [0.780, 1.34] 1.01 [0.650, 1.35] fat
Mean (SD) 18200 (4950) 15000 (5110) 17300 (5210) Median [Min, Max] 17700
[6220, 40800] 15100 [4280, 29900] 17000 [4280, 40800] lean
Mean (SD) 32000 (3970) 43800 (5820) 35500 (7030) Median [Min, Max] 31500
[19100, 53400] 43400 [28600, 63100] 33600 [19100, 63100]
as.factor(hypertension)
0 430 (49.9%) 170 (47.9%) 600 (49.3%) 1 432 (50.1%) 185 (52.1%) 617
(50.7%) as.factor(diabetes)
0 760 (88.2%) 322 (90.7%) 1082 (88.9%) ## Vẽ biểu đồ library(ggplot2)
> library(gridExtra) > > p = ggplot(data = ob, aes(x = pcfat))
> p1 = p + geom_histogram() > p2 = p + geom_histogram(fill =
“blue”, col = “white”) + labs(x = “Tỉ trọng mỡ (%)”, y = “Số người”,
title = “Phân bố tỉ trọng mỡ”) > > grid.arrange(p1, p2, ncol = 2)
Đánh giá phân bố một giá trị y số người, hoành: tỉ trọng mỡ p =
ggplot(data = ob, aes(x = pcfat, fill = gender)) p1 = p +
geom_histogram(col=“white”) + labs(x = “Tỉ trọng mỡ”, y = “Số người”,
title = “Phân bố tỉ trọng mỡ”) p2 = p + geom_density(alpha = 0.5) +
labs(x = “Tỉ trọng mỡ”, y = “Số người”, title = “Phân bố tỉ trọng
mỡ”)
grid.arrange(p1, p2, ncol = 2)
Vẽ biểu đồ - Phân bố theo giới tính Biểu đồ hình chuông=> Phân bố chuẩn Biểu đồ Histogram ## Việc 5: Biểu đồ thanh ob\(OB[ob\)bmi< 18.5] = “Underweight” ob\(OB[ob\)bmi>= 18.5 & ob\(bmi< 25] = "Normal" ob\)OB[ob\(bmi>= 25 & ob\)bmi< 30] = “Overweight” ob\(OB[ob\)bmi>= 30] = “Obese” p = ggplot(data = ob, aes(x = OB, fill = OB, col = OB)) p + geom_bar(position = “dodge”) - Sắp xêp không theo trình tự mong đợi Phần mềm theo thứ tự ABC Mình muốn theo thứ tự phải bắt phần mềm làm theo ý mình ## Việc 6. Biểu đồ hộp ###6.1 so sánh p = ggplot(data = women, aes(x = OB, y = pcfat, fill = OB, col = OB)) p1 = p + geom_boxplot(col = “black”) + geom_jitter(alpha = 0.05) + labs(x = “Tình trạng béo phì”, y = “Tỉ trọng mỡ (%)”) + ggtitle(“Tỉ trọng mỡ theo tình trạng béo phì ở nữ”) p1 Hiểu biểu đồ này ntn - Vết chấm là những giá trị outliner women\(OB.n = factor(women\)OB, levels = c(“Underweight”, “Normal”, “Overweight”, “Obese”)) Trình bày theo thứ tự p = ggplot(data = women, aes(x = OB.n, y = pcfat, fill = OB.n, col = OB.n)) p2 = p + geom_boxplot(col = “black”) + geom_jitter(alpha = 0.05) + labs(x = “Tình trạng béo phì”, y = “Tỉ trọng mỡ (%)”) + ggtitle(“Tỉ trọng mỡ theo tình trạng béo phì ở nữ”) p2 Trình bày cả nam và nữ trong 1 biểu đồ - bất kì nhóm nào tỉ trọng mỡ của nữ cao hơn nam - trong normalnormal dao động rất lớn so với các nhóm khác ## Việc 7: Vẽ biểu đồ tán xạ