Bài giảng Phân tích dữ liệu và ứng dụng - Bài 2: Biên tập dữ liệu

Dữ liệu thực hành "kidiq.txt" • File name: kidiq.txt • 434 trẻ với các biến số sau đây: kid.score: điểm 'thông minh' của trẻ mom.hs: mẹ tốt nghiệp trung học (0, 1) mom.iq: IQ của mẹ mom.age: tuổi của mẹ mom.work: (1=mẹ không đi làm trong 3 năm đầu của trẻ; 2=mẹ làm trong năm 2 và 3; 3=mẹ làm bán thời gian năm đầu; 4=mẹ làm toàn thời gian năm đầu)

pdf37 trang | Chia sẻ: thanhle95 | Lượt xem: 435 | Lượt tải: 1download
Bạn đang xem trước 20 trang tài liệu Bài giảng Phân tích dữ liệu và ứng dụng - Bài 2: Biên tập dữ liệu, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Tuan V. Nguyen 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 Biên tập dữ liệu • Dùng hàm cơ bản trong R – mã hóa, hoán chuyển, đổi tên biến, v.v. • Dùng package tidyverse – select, filter, mutate, arrange, summarize Biên tập dữ liệu với hàm cơ bản trong R Dấu "$" • Rất quan trọng! • $ nối kết dataset và biến số (dataframe và variable) dat$var1 • có nghĩa là biến "var1" thuộc dataset "dat" Dấu "$" bw = read.csv("~/Dropbox/_Conferences and Workshops/TDTU 2018/Datasets/birthwt.csv") head(bw, 3) id low age lwt race smoke ptl ht ui ftv bwt 1 85 0 19 182 2 0 0 0 1 0 2523 2 86 0 33 155 3 0 0 0 0 3 2551 3 87 0 20 105 1 1 0 0 0 1 2557 > weight = lwt*0.453592 Error: object 'lwt' not found > bw$weight = bw$lwt*0.453592 > head(bw, 3) id low age lwt race smoke ptl ht ui ftv bwt weight 1 85 0 19 182 2 0 0 0 1 0 2523 82.55374 2 86 0 33 155 3 0 0 0 0 3 2551 70.30676 3 87 0 20 105 1 1 0 0 0 1 2557 47.62716 Mã hoá (coding) # Chúng ta muốn tạo ra một biến mới "lowbw" mã hóa từ biến low. Nếu low=1 thì lowbw="Yes"; nếu low=0 thì lowbw="No" bw$lowbw[low=1] <- "Yes" bw$lowbw[low=0] <- "No" > head(bw, 3) id low age lwt race smoke ptl ht ui ftv bwt weight lowbw 1 85 0 19 182 2 0 0 0 1 0 2523 82.55374 Yes 2 86 0 33 155 3 0 0 0 0 3 2551 70.30676 Yes 3 87 0 20 105 1 1 0 0 0 1 2557 47.62716 Yes ifelse # Chúng ta muốn tạo ra một biến mới "smoker". Nếu smoke=1 thì smoker=Yes, tất cả các giá trị khác thì smoker=No bw$smoker = ifelse(bw$smoke==1, 1, 0) as.factor và as.numeric • Biến character: dùng cho phân nhóm • Biến numeric: dùng cho tính toán > head(bw, 3) id low age lwt race smoke ptl ht ui ftv bwt weight lowbw smoker 1 85 0 19 182 2 0 0 0 1 0 2523 82.55374 Yes 0 2 86 0 33 155 3 0 0 0 0 3 2551 70.30676 Yes 0 3 87 0 20 105 1 1 0 0 0 1 2557 47.62716 Yes 1 > bw$temp = as.factor(bw$race) > mean(bw$temp) [1] NA Warning message: In mean.default(bw$temp) : argument is not numeric or logical: returning NA > mean(bw$race) [1] 1.846561 sort() > bw$bwt [1] 2523 2551 2557 2594 2600 2622 2637 2637 2663 2665 2722 2733 2751 2750 [15] 2769 2769 2778 2782 2807 2821 2835 2835 2836 2863 2877 2877 2906 2920 [29] 2920 2920 2920 2948 2948 2977 2977 2977 2977 2922 3005 3033 3042 3062 [43] 3062 3062 3062 3062 3080 3090 3090 3090 3100 3104 3132 3147 3175 3175 > sort(bw$bwt) [1] 709 1021 1135 1330 1474 1588 1588 1701 1729 1790 1818 1885 1893 1899 [15] 1928 1928 1928 1936 1970 2055 2055 2082 2084 2084 2100 2125 2126 2187 [29] 2187 2211 2225 2240 2240 2282 2296 2296 2301 2325 2353 2353 2367 2381 [43] 2381 2381 2410 2410 2410 2414 2424 2438 2442 2450 2466 2466 2466 2495 [57] 2495 2495 2495 2523 2551 2557 2594 2600 2622 2637 2637 2663 2665 2722 Thay đổi tên biến (rename) Chúng ta muốn đổi tên biến "low" thành "low.bw", và "lwt" thành "mother.weight" > bw = read.csv("~/Dropbox/_Conferences and Workshops/TDTU 2018/Datasets/birthwt.csv") > head(bw, 3) id low age lwt race smoke ptl ht ui ftv bwt 1 85 0 19 182 2 0 0 0 1 0 2523 2 86 0 33 155 3 0 0 0 0 3 2551 3 87 0 20 105 1 1 0 0 0 1 2557 names(bw)[names(bw)=="low"] <- "low.bw" names(bw)[names(bw)=="lwt"] <- "mother.weight" > head(bw, 3) id low.bw age mother.weight race smoke ptl ht ui ftv bwt 1 85 0 19 182 2 0 0 0 1 0 2523 2 86 0 33 155 3 0 0 0 0 3 2551 3 87 0 20 105 1 1 0 0 0 1 2557 Hợp nhất dữ liệu: merge() id = c(1,2,3,4) sex=c("M","F","M","F") dat1=data.frame(id,sex) id = c(1,2,3,4,5) age=c(21,34,45,32,18) dat2=data.frame(id,age) dat = merge(dat1, dat2, by="id") dat = merge(dat1, dat2, by="id", all.x=T, all.y=T) Tóm lược biên tập dữ liệu • Biên tập dữ liệu bao gồm – mã hóa (ifelse) – sắp xếp thứ tự – thay đổi tên biến số (names) – hợp nhất dữ liệu (merge) Biên tập dữ liệu với "tidyverse" Package tidyverse • Hadley Wickham phát triển • Tổng hợp từ dplyr, ggplot2 • Dùng cho khoa học dữ liệu • Rất tiện cho quản lí dữ liệu phức tạp • Chuẩn bị dữ liệu cho phân tích https://www.tidyverse.org Những hàm chính trong tidyverse/dplyr 5 'động từ' chính: • select() chọn những cột/field liên quan • filter() chọn những dòng quan tâm • mutate() thêm cột/field • arrange() thứ tự hóa dòng dữ liệu • summarize()tóm tắt dữ liệu theo dòng %>% (pipe) operator "Văn phạm" chính của tidyverse select(dataframe, conditions) filter(dataframe, conditions) mutate(dataframe, conditions) arrange(dataframe, conditions) summarize(dataframe, conditions) hoặc dataframe %>% select(conditions) Dữ liệu thực hành: cân nặng trẻ sơ sinh bw = read.csv("birthwt.csv") head(bw) id low age lwt race smoke ptl ht ui ftv bwt 1 85 0 19 182 2 0 0 0 1 0 2523 2 86 0 33 155 3 0 0 0 0 3 2551 3 87 0 20 105 1 1 0 0 0 1 2557 4 88 0 21 108 1 1 0 0 1 2 2594 5 89 0 18 107 1 1 0 0 1 0 2600 6 91 0 21 124 3 0 0 0 0 0 2622 select() – chọn những cột liên quan full data (bw) 189 dòng, 11 cột) id low age lwt race smoke ptl ht ui ftv bwt 1 85 0 19 182 2 0 0 0 1 0 2523 2 86 0 33 155 3 0 0 0 0 3 2551 3 87 0 20 105 1 1 0 0 0 1 2557 4 88 0 21 108 1 1 0 0 1 2 2594 5 89 0 18 107 1 1 0 0 1 0 2600 6 91 0 21 124 3 0 0 0 0 0 2622 temp = select(bw, c("low", "bwt", "lwt", "age")) temp = bw %>% select(c("low", "bwt", "lwt", "age")) head(temp) > head(temp) low bwt lwt age 1 0 2523 182 19 2 0 2551 155 33 3 0 2557 105 20 4 0 2594 108 21 5 0 2600 107 18 6 0 2622 124 21 filter() – chọn những dòng liên quan temp = bw %>% filter(race==1, bwt<2500) temp = filter(bw, race==1, bwt<2500) head(temp) > temp id low age lwt race smoke ptl ht ui ftv bwt 1 10 1 29 130 1 0 0 0 1 2 1021 2 20 1 21 165 1 1 0 1 0 1 1790 3 22 1 32 105 1 1 0 0 0 0 1818 4 23 1 19 91 1 1 2 0 1 0 1885 5 26 1 25 92 1 1 0 0 0 0 1928 6 27 1 20 150 1 1 0 0 0 2 1928 7 29 1 24 155 1 1 1 0 0 0 1936 8 33 1 19 102 1 0 0 0 0 2 2082 9 34 1 19 112 1 1 0 0 1 0 2084 10 35 1 26 117 1 1 1 0 0 0 2084 11 36 1 24 138 1 0 0 0 0 0 2100 12 42 1 22 130 1 1 1 0 1 1 2187 13 45 1 17 110 1 1 0 0 0 0 2225 14 51 1 20 121 1 1 1 0 1 0 2296 15 56 1 31 102 1 1 1 0 0 1 2353 16 57 1 15 110 1 0 0 0 0 0 2353 17 65 1 30 142 1 1 1 0 0 0 2410 18 67 1 22 130 1 1 0 0 0 1 2410 19 68 1 17 120 1 1 0 0 0 3 2414 20 69 1 23 110 1 1 1 0 0 0 2424 21 77 1 26 190 1 1 0 0 0 0 2466 22 79 1 28 95 1 1 0 0 0 2 2466 23 84 1 21 130 1 1 0 1 0 3 2495 mutate - tạo ra biến số mới temp = bw %>% mutate(mother.wt=lwt*0.453592, weight=bwt/1000) temp = mutate(bw, mother.wt=lwt*0.453592, weight=bwt/1000) head(temp) id low age lwt race smoke ptl ht ui ftv bwt mother.wt weight 1 85 0 19 182 2 0 0 0 1 0 2523 82.55374 2.523 2 86 0 33 155 3 0 0 0 0 3 2551 70.30676 2.551 3 87 0 20 105 1 1 0 0 0 1 2557 47.62716 2.557 4 88 0 21 108 1 1 0 0 1 2 2594 48.98794 2.594 5 89 0 18 107 1 1 0 0 1 0 2600 48.53434 2.600 6 91 0 21 124 3 0 0 0 0 0 2622 56.24541 2.622 arrange – sort dữ liệu theo dòng arrange(temp, mother.wt, weight) id low age lwt race smoke ptl ht ui ftv bwt mother.wt weight 1 44 1 20 80 3 1 0 0 1 0 2211 36.28736 2.211 2 15 1 25 85 3 0 0 0 1 0 1474 38.55532 1.474 3 137 0 22 85 3 1 0 0 0 0 3090 38.55532 3.090 4 32 1 25 89 3 0 2 0 0 1 2055 40.36969 2.055 5 118 0 24 90 1 1 1 0 0 1 2948 40.82328 2.948 6 132 0 18 90 1 1 0 0 1 0 3062 40.82328 3.062 .... summarize, group_by – tóm tắt dữ liệu theo nhóm # tính giá trị trung bình của biến age, lwt và bwt theo smoke và race # tạo ra những biến mới có giá trị trung bình với tên là mean.age, mean.lwt, và mean.bw # Định nghĩa nhóm bygroup = group_by(bw, race, smoke) # Tạo ra dataset mới temp temp = summarize(bygroup, count = n(), mean.age = mean(age, na.rm=T), mean.lwt = mean(lwt, na.rm=T), mean.bw = mean(bwt, na.rm=T)) temp summarize, group_by – tóm tắt dữ liệu theo nhóm > temp # A tibble: 6 x 6 # Groups: race [?] race smoke count mean.age mean.lwt mean.bw 1 1 0 44 26.0 139. 3429. 2 1 1 52 22.8 126. 2827. 3 2 0 16 19.9 149. 2854. 4 2 1 10 24.1 143. 2504. 5 3 0 55 22.4 119. 2816. 6 3 1 12 22.5 124. 2757. Hàm "count" đếm tần số bw %>% count(race) > bw %>% count(race) race n 1 1 96 2 2 26 3 3 67 bw %>% count(race, smoke) race smoke n 1 1 0 44 2 1 1 52 3 2 0 16 4 2 1 10 5 3 0 55 6 3 1 12 Hàm "sample" có thể dùng để chọn mẫu ngẫu nhiên d5 = sample_n(bw, 10) # Chọn ngẫu nhiên 10 đối tượng từ bw d5 id low age lwt race smoke ptl ht ui ftv bwt 57 144 0 21 110 3 1 0 0 1 0 3203 159 43 1 27 130 2 0 0 0 1 0 2187 124 220 0 22 129 1 0 0 0 0 0 4111 93 187 0 19 235 1 1 0 1 0 0 3629 48 135 0 19 132 3 0 0 0 0 0 3090 73 164 0 23 115 3 1 0 0 0 1 3331 17 102 0 15 98 2 0 0 0 0 0 2778 154 35 1 26 117 1 1 1 0 0 0 2084 30 116 0 17 113 2 0 0 0 0 1 2920 79 172 0 20 121 2 1 0 0 0 0 3444 Hàm "sample_frac()" có thể dùng để chọn mẫu ngẫu nhiên d6 = sample_frac(bw, 0.05) # Chọn ngẫu nhiên 5% đối tượng từ bw d6 id low age lwt race smoke ptl ht ui ftv bwt 127 223 0 35 170 1 0 1 0 0 1 4174 125 221 0 25 130 1 0 0 0 0 2 4153 141 22 1 32 105 1 1 0 0 0 0 1818 146 27 1 20 150 1 1 0 0 0 2 1928 31 117 0 17 113 2 0 0 0 0 1 2920 48 135 0 19 132 3 0 0 0 0 0 3090 128 224 0 19 120 1 1 0 0 0 0 4238 116 212 0 28 134 3 0 0 0 0 1 3941 97 191 0 29 154 1 0 0 0 0 1 3651 Tóm tắt tidyverse • select() chọn những cột/field liên quan • filter() chọn những dòng quan tâm • mutate() thêm cột/field • arrange() thứ tự hóa dòng dữ liệu • summarize()tóm tắt dữ liệu theo dòng • sample() lấy mẫu ngẫu nhiên Đến phiên bạn ... Thực hành với R Dữ liệu thực hành "kidiq.txt" • File name: kidiq.txt • 434 trẻ với các biến số sau đây: kid.score: điểm 'thông minh' của trẻ mom.hs: mẹ tốt nghiệp trung học (0, 1) mom.iq: IQ của mẹ mom.age: tuổi của mẹ mom.work: (1=mẹ không đi làm trong 3 năm đầu của trẻ; 2=mẹ làm trong năm 2 và 3; 3=mẹ làm bán thời gian năm đầu; 4=mẹ làm toàn thời gian năm đầu) Đọc file vào máy tính Set working directory # đọc file kidiq.csv # iq = read.csv("kidiq.csv") iq = read.csv(file.choose()) iq head(iq) names(iq) dim(iq) "Cảm nhận" và tóm lược dữ liệu mean(id$kid.score) median(id$kid.score) sd(id$kid.score) var(id$kid.score) summary(iq$kid.score) summary(iq$mom.score) summary(iq) hist(iq$kid.score) hist(iq$kid.score, col="blue") hist(iq$kid.score, col="blue", border="white") qqnorm(iq$kid.score) qqline(iq$kid.score) Tabulation freq = table(iq$mom.work)) barplot(freq) barplot(table(iq$mom.work)) table(iq$mom.work) table(iq$mom.work, iq$mom.hs) # cần cài đặt DescTools install.packages("DescTools") library(DescTools) iq$mom.work = as.factor(iq$mom.work) Desc(iq$mom.work ~ iq$mom.hs) Biểu đồ phân bố theo nhóm ks1 = iq$kid.score[iq$mom.hs == 1] ks0 = iq$kid.score[iq$mom.hs == 0] h = hist(iq$kid.score, plot=TRUE) h1 = hist(ks1, breaks = h$breaks, plot=FALSE) h0 = hist(ks0, breaks = h$breaks, plot=FALSE) plot(h1, main="", col="red") plot(h0, add=T, col="grey") Biểu đồ hộp (box plot) boxplot(iq$kid.score ~ iq$mom.hs) boxplot(iq$kid.score ~ iq$mom.hs, names=c("No HS", "HS")) boxplot(iq$kid.score ~ iq$mom.hs, col=c("blue", "red"), names=c("No HS", "HS")) Biểu đồ tương quan plot(iq$mom.iq, iq$kid.score) plot(iq$kid.score ~ iq$mom.iq, pch=16) rownames(iq) = paste("kid", 1:nrow(iq), sep=" ") plot(iq$mom.iq, iq$kid.score, pch=16) identify(iq$mom.iq, iq$kid.score, labels=rownames(iq)) index = iq$mom.hs==1 plot(iq$mom.iq[index], iq$kid.score[index], pch=16) points(iq$mom.iq[!index], iq$kid.score[!index], col="red", pch=16) Tương quan đa chiều iq = read.csv("kidiq.csv") cor(iq) install.packages("psych") library(psych) pairs.panels(iq)