intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Bài giảng Hiển thị dữ liệu (Data visualization)

Chia sẻ: Menh Menh | Ngày: | Loại File: PDF | Số trang:50

44
lượt xem
5
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Nội dung của bài giảng trình bày biểu đồ phân bố geom histogram; điểm thi mô Toán; tuổi thọ của vài lãnh đạo; box plot geom boxplot; 5 yếu tố trong biểu đồ hộp; không nên dùng barplot để mô tả biến liên tục; biểu đồ tán xạ; scatter plot dữ liệu theo thời gian...

Chủ đề:
Lưu

Nội dung Text: Bài giảng Hiển thị dữ liệu (Data visualization)

  1. Tuan V. Nguyen Senior Principal Research Fellow, Garvan Institute of Medical Research Professor, UNSW School of Public Health and Community Medicine Professor of Predictive Medicine, University of Technology Sydney Adj. Professor of Epidemiology and Biostatistics, School of Medicine Sydney, University of Notre Dame Australia Phân tích dữ liệu và ứng dụng | Đại học Dược Hà Nội | 12/6 to 17/6/2019 © Tuan V. Nguyen
  2. Biểu đồ • Phân bố: histogram • Tần số: barplot • So sánh: boxplot • Liên quan: scatterplot
  3. Obesity data (Vietnam) • Cross-sectional study of obesity in Vietnam • Aim: to predict percent body fat (pcfat) by using gender, age, bmi. dat = read.csv("~/Dropbox/_Conferences and Workshops/TDTU 2018/Datasets/obesity data.csv") dim(dat) [1] 1217 11 head(dat) id gender height weight bmi age WBBMC wbbmd fat lean pcfat 1 1 F 150 49 21.8 53 1312 0.88 17802 28600 37.3 2 2 M 165 52 19.1 65 1309 0.84 8381 40229 16.8 3 3 F 157 57 23.1 64 1230 0.84 19221 36057 34.0 4 4 F 156 53 21.8 56 1171 0.80 17472 33094 33.8 5 5 M 160 51 19.9 54 1681 0.98 7336 40621 14.8 6 6 F 153 47 20.1 52 1358 0.91 14904 30068 32.2
  4. Histogram
  5. Biểu đồ phân bố: geom_histogram() • Mục tiêu: mô tả phân bố của dữ liệu • Có thể so sánh phân bố giữa 2 hay nhiều hơn 2 nhóm • Ví dụ: Phân bố của pcfat (tỉ trọng mỡ) dat = read.csv("~/Dropbox/_Conferences and Workshops/TDTU 2018/Datasets/obesity data.csv") # Biểu đồ đơn giản library(ggplot2); library(gridExtra) p = ggplot(data=dat, aes(x=pcfat)) p1 = p + geom_histogram(color="white", fill="blue") p = p + geom_histogram(aes(y=..density..), color="white", fill="blue") p2 = p + geom_density(col="red") grid.arrange(p1, p2, ncol=2)
  6. # Biểu đồ đơn giản 100 0.06 library(ggplot2); library(gridExtra) p = ggplot(data=dat, aes(x=pcfat)) 75 p1 = p + 0.04 geom_histogram(color="white", fill="blue") density count 50 p = p + geom_histogram(aes(y=..density..), color="white", fill="blue") 0.02 p2 = p + geom_density(col="red") 25 grid.arrange(p1, p2, ncol=2) 0 0.00 10 20 30 40 50 10 20 30 40 50 pcfat pcfat
  7. 100 Phân tích theo giới tính 75 gender count 50 F p = ggplot(data=dat, aes(x=pcfat, M fill=gender)) 25 p1 = p + geom_histogram(position="dodge") 0 10 20 30 40 50 pcfat 0.08 p2 = ggplot(data=dat, aes(x=pcfat, fill=gender, color=gender)) + 0.06 geom_density(alpha = 0.1) gender density 0.04 F M grid.arrange(p1, p2, nrow=2) 0.02 0.00 10 20 30 40 50 pcfat
  8. Điểm thi môn toán (2018) dat = read.csv("~/Dropbox/_Conferences and Workshops/TDTU 2018/Datasets/THPT 2018 All Provinces.csv") > head(dat) ID Province Math Viet English Physics Chemistry Biology History Geography 1 1 VN 3.0 3.75 3.0 NA NA NA 3.0 6.50 2 2 VN 8.8 7.50 9.0 NA NA NA 6.0 9.00 3 3 VN 6.0 5.50 4.0 5.75 5.5 5.00 NA NA 4 4 VN 3.4 5.75 2.6 NA NA NA 3.5 4.75 5 5 VN 3.8 6.75 3.0 NA NA NA 3.5 6.25 6 6 VN 5.0 6.50 2.2 2.00 3.5 4.25 NA NA p = ggplot(data=dat, aes(x=Math, fill=Province, color=Province)) p = p + geom_density(alpha = 0.1)
  9. 0.4 0.3 Province Bac Lieu Can Tho density 0.2 Ha Giang Hoa Binh Son La VN 0.1 0.0 0.0 2.5 5.0 7.5 10.0 Math p = ggplot(data=dat, aes(x=Math, fill=Province, color=Province)) p = p + geom_density(alpha = 0.1)
  10. Bar plot
  11. Biểu đồ thanh: geom_bar() • Rất hạn chế về giá trị khoa học (gần như không khuyến khích) • Tuy nhiên, có thể dùng để mô tả dữ liệu có sẵn hay "summary data" • Ví dụ: tuổi thọ của một số lãnh đạo, có thể hiện thị qua biểu đồ name dd db HCMinh 2/9/1969 19/5/1890 LDuẩn 10/7/1986 7/4/1907 TChinh 30/9/1998 9/2/1907 NVLinh 27/4/1998 1/7/1915 PVĐồng 29/4/2000 1/3/1906 VVKiet 11/6/2008 23/11/1922 VNGiap 4/10/2013 25/8/1911 ĐMuoi 1/10/2018 2/2/1917 PVKhai 17/3/2018 25/12/1933 LĐAnh 22/4/2019 1/12/1920
  12. Tuổi thọ của vài lãnh đạo name = c("HCMinh", "LDuẩn", "TChinh", "NVLinh", "PVĐồng", "VVKiet", "VNGiap", "ĐMuoi", "PVKhai", "LĐAnh") life.exp = c(55.7, 64.5, 67.2, 67.2, 67.2, 69.3, 70.5, 71.5, 71.5, 71.5) dd = c("2/9/1969", "10/7/1986", "30/9/1998", "27/4/1998", "29/4/2000", "11/6/2008", "4/10/2013", "1/10/2018", "17/3/2018", "22/4/2019") db = c("19/5/1890", "7/4/1907", "9/2/1907", "1/7/1915", "1/3/1906", "23/11/1922", "25/8/1911", "2/2/1917", "25/12/1933", "1/12/1920") dob = as.Date(db, "%d/%m/%Y") dod = as.Date(dd, "%d/%m/%Y") age = as.numeric((dod-dob)/365) extra = age-life.exp dat = data.frame(name, dob, dod, age, life.exp, extra) dat
  13. Tuổi thọ của vài lãnh đạo > dat name dob dod age life.exp extra 1 HCMinh 1890-05-19 1969-09-02 79.34247 55.7 23.64247 2 LDuẩn 1907-04-07 1986-07-10 79.31233 64.5 14.81233 3 TChinh 1907-02-09 1998-09-30 91.70137 67.2 24.50137 4 NVLinh 1915-07-01 1998-04-27 82.87945 67.2 15.67945 5 PVĐồng 1906-03-01 2000-04-29 94.22740 67.2 27.02740 6 VVKiet 1922-11-23 2008-06-11 85.60822 69.3 16.30822 7 VNGiap 1911-08-25 2013-10-04 102.18082 70.5 31.68082 8 ĐMuoi 1917-02-02 2018-10-01 101.72877 71.5 30.22877 9 PVKhai 1933-12-25 2018-03-17 84.28219 71.5 12.78219 10 LĐAnh 1920-12-01 2019-04-22 98.45479 71.5 26.95479
  14. Tuổi thọ của vài lãnh đạo library(ggplot2) p = ggplot(data=dat, aes(x=reorder(name, -age), y=age, fill=name, col=name)) p + geom_bar(stat="identity") + geom_errorbar(aes(y=life.exp, ymax=life.exp, ymin=life.exp), col="black", linetype="dashed") + geom_text(label=round(extra, 1), vjust=-0.2) + xlab("Tên") + ylab("Tuổi") + theme(legend.position="none")
  15. 31.7 30.2 100 27 27 24.5 16.3 12.8 15.7 23.6 14.8 75 Tuổi 50 25 0 VNGiap ĐMuoi LĐAnh PVĐồng TChinh VVKiet PVKhai NVLinh HCMinh LDuẩn Tên
  16. Box plot: geom_boxplot()
  17. 5 yếu tố trong biểu đồ hộp • median = trung vị • 2 hinges = 25% và 75% bách phân vị 1.4 • fences = 1.5 x interquartile range 1.2 • whiskers = nối hai hinges 1.0 75% (Q3) median (50%) • Outliers (có thể giá trị ngoại vi) 0.8 25% (Q1) 0.6 0.4 interquartile range = Q3 – Q1
  18. Không nên dùng barplot để mô tả biến số liên tục! https://www.nature.com/articles/nmeth.2811/figures/1
  19. Biểu đồ hộp: nghiên cứu bệnh tiểu đường > db = read.csv("~/Dropbox/_Conferences and Workshops/Dai hoc Duoc 6- 2019/Datasets/Diabetes data.csv") > head(db) id age gender height weight waist hip sysbp diabp active hypertension 1 1 76 Female 163 53 90 93 160 90 0 1 2 1 40 Female 149 51 74 94 100 60 0 0 3 1 51 Female 151 55 91 100 120 80 0 0 4 1 43 Female 158 62 78 96 120 80 1 0 5 2 72 Female 148 47 91 95 130 60 1 0 6 2 44 Male 155 48 69 86 120 80 0 0 bmi whr diabetes 1 19.95 0.97 IFG 2 22.97 0.79 Normal 3 24.12 0.91 Normal 4 24.84 0.81 Normal 5 21.46 0.96 IFG 6 19.98 0.80 Normal
  20. WHR và tiểu đường • Mục tiêu: so sánh tỉ số vòng eo-mông giữa 3 nhóm cá nhân: bình thường, IFG, và tiểu đường • Biến phân tích: whr • Biến phân nhóm: diabetes
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2