Ngày 1: 16/4/2022

Test thử R markdown Load library

library(tidyverse)

Load data:

file.choose()

chiều 16/4/2022

Data editting with tidyverse, magrittr

data set Thử thống kê ko được: Trường phái Bayesian.

#Bài tập ngày 1:

Việc 3: Đọc dữ liệu “Arrest dataset.csv” vào R dùng hàm read.csv và gọi R. object là arr.

arr = read.csv("/Users/marcelkhoaphuoc/Documents/Data R/Dataset for TDTU workshop 4-2022/Arrest dataset.csv")

Việc 4: Thông tin về dữ liệu arr (a) tìm hiểu số cột và dòng

dim(arr)
## [1] 432  12
  1. liệt kê 6 dòng đầu của dữ liệu
head(arr)
##   id age finance week arrest  race work.exp     married parole prior educ
## 1  1  27      no   20      1 black       no not married    yes     3    3
## 2  2  18      no   17      1 black       no not married    yes     8    4
## 3  3  19      no   25      1 other      yes not married    yes    13    3
## 4  4  23     yes   52      0 black      yes     married    yes     1    5
## 5  5  19      no   52      0 other      yes not married    yes     3    3
## 6  6  24      no   52      0 black      yes not married     no     2    4
##   employ1
## 1      no
## 2      no
## 3      no
## 4      no
## 5      no
## 6      no
  1. liệt kê 6 dòng cuối của dữ liệu
tail(arr)
##      id age finance week arrest  race work.exp     married parole prior educ
## 427 427  22     yes   12      1 black      yes     married    yes     2    4
## 428 428  31     yes   52      0 other      yes not married    yes     3    3
## 429 429  20      no   52      0 black       no not married    yes     1    4
## 430 430  20     yes   52      0 black      yes     married    yes     1    3
## 431 431  29      no   52      0 black      yes not married    yes     3    4
## 432 432  24     yes   52      0 black      yes not married    yes     1    4
##     employ1
## 427      no
## 428      no
## 429      no
## 430      no
## 431     yes
## 432     yes

Việc 5: Tạo ra một biến mới và gọi là arrest1 là một biến character từ biến arrest (đang là biến numeric) như sau:

arr$arrest1[arr$arrest == 1] = "YES" 
arr$arrest1[arr$arrest == 0] = "NO"
head(arr,8)
##   id age finance week arrest  race work.exp     married parole prior educ
## 1  1  27      no   20      1 black       no not married    yes     3    3
## 2  2  18      no   17      1 black       no not married    yes     8    4
## 3  3  19      no   25      1 other      yes not married    yes    13    3
## 4  4  23     yes   52      0 black      yes     married    yes     1    5
## 5  5  19      no   52      0 other      yes not married    yes     3    3
## 6  6  24      no   52      0 black      yes not married     no     2    4
## 7  7  25      no   23      1 black      yes     married    yes     0    4
## 8  8  21     yes   52      0 black      yes not married    yes     4    3
##   employ1 arrest1
## 1      no     YES
## 2      no     YES
## 3      no     YES
## 4      no      NO
## 5      no      NO
## 6      no      NO
## 7     yes     YES
## 8      no      NO

Tạo ra một biến mới và gọi là fin là một biến numeric từ biến finance (đang là biến character) như sau: Nếu finance là “yes” thì fin là 1
Nếu finance là “no” thì fin là 0

arr$fin[arr$finance == "yes"] = 1
arr$fin[arr$finance == "no"] = 0
head(arr,9)
##   id age finance week arrest  race work.exp     married parole prior educ
## 1  1  27      no   20      1 black       no not married    yes     3    3
## 2  2  18      no   17      1 black       no not married    yes     8    4
## 3  3  19      no   25      1 other      yes not married    yes    13    3
## 4  4  23     yes   52      0 black      yes     married    yes     1    5
## 5  5  19      no   52      0 other      yes not married    yes     3    3
## 6  6  24      no   52      0 black      yes not married     no     2    4
## 7  7  25      no   23      1 black      yes     married    yes     0    4
## 8  8  21     yes   52      0 black      yes not married    yes     4    3
## 9  9  22      no   52      0 black       no not married     no     6    3
##   employ1 arrest1 fin
## 1      no     YES   0
## 2      no     YES   0
## 3      no     YES   0
## 4      no      NO   1
## 5      no      NO   0
## 6      no      NO   0
## 7     yes     YES   0
## 8      no      NO   1
## 9      no      NO   0

Việc 6: Tóm tắt dữ liệu arr bằng hàm summary.

summary(arr)
##        id             age         finance               week      
##  Min.   :  1.0   Min.   :17.0   Length:432         Min.   : 1.00  
##  1st Qu.:108.8   1st Qu.:20.0   Class :character   1st Qu.:50.00  
##  Median :216.5   Median :23.0   Mode  :character   Median :52.00  
##  Mean   :216.5   Mean   :24.6                      Mean   :45.85  
##  3rd Qu.:324.2   3rd Qu.:27.0                      3rd Qu.:52.00  
##  Max.   :432.0   Max.   :44.0                      Max.   :52.00  
##      arrest           race             work.exp           married         
##  Min.   :0.0000   Length:432         Length:432         Length:432        
##  1st Qu.:0.0000   Class :character   Class :character   Class :character  
##  Median :0.0000   Mode  :character   Mode  :character   Mode  :character  
##  Mean   :0.2639                                                           
##  3rd Qu.:1.0000                                                           
##  Max.   :1.0000                                                           
##     parole              prior             educ         employ1         
##  Length:432         Min.   : 0.000   Min.   :2.000   Length:432        
##  Class :character   1st Qu.: 1.000   1st Qu.:3.000   Class :character  
##  Mode  :character   Median : 2.000   Median :3.000   Mode  :character  
##                     Mean   : 2.984   Mean   :3.477                     
##                     3rd Qu.: 4.000   3rd Qu.:4.000                     
##                     Max.   :18.000   Max.   :6.000                     
##    arrest1              fin           
##  Length:432         Length:432        
##  Class :character   Class :character  
##  Mode  :character   Mode  :character  
##                                       
##                                       
## 

Việc 7: Tóm tắt thống kê dữ liệu qua hàm table1 trong package table1

library(table1)
## 
## Attaching package: 'table1'
## The following objects are masked from 'package:base':
## 
##     units, units<-
table1(~age + finance + fin + arrest + arrest1 + race + parole + educ, data=arr)
Overall
(N=432)
age
Mean (SD) 24.6 (6.11)
Median [Min, Max] 23.0 [17.0, 44.0]
finance
no 216 (50.0%)
yes 216 (50.0%)
fin
0 216 (50.0%)
1 216 (50.0%)
arrest
Mean (SD) 0.264 (0.441)
Median [Min, Max] 0 [0, 1.00]
arrest1
NO 318 (73.6%)
YES 114 (26.4%)
race
black 379 (87.7%)
other 53 (12.3%)
parole
no 165 (38.2%)
yes 267 (61.8%)
educ
Mean (SD) 3.48 (0.834)
Median [Min, Max] 3.00 [2.00, 6.00]

Tóm tắt thống kê theo tình trạng hỗ trợ tài chánh (finance):

table1(~age+finance+fin+arrest+arrest1+race+parole+educ | finance, data=arr)
no
(N=216)
yes
(N=216)
Overall
(N=432)
age
Mean (SD) 24.2 (5.73) 25.0 (6.47) 24.6 (6.11)
Median [Min, Max] 23.0 [17.0, 44.0] 23.0 [17.0, 44.0] 23.0 [17.0, 44.0]
finance
no 216 (100%) 0 (0%) 216 (50.0%)
yes 0 (0%) 216 (100%) 216 (50.0%)
fin
0 216 (100%) 0 (0%) 216 (50.0%)
1 0 (0%) 216 (100%) 216 (50.0%)
arrest
Mean (SD) 0.306 (0.462) 0.222 (0.417) 0.264 (0.441)
Median [Min, Max] 0 [0, 1.00] 0 [0, 1.00] 0 [0, 1.00]
arrest1
NO 150 (69.4%) 168 (77.8%) 318 (73.6%)
YES 66 (30.6%) 48 (22.2%) 114 (26.4%)
race
black 185 (85.6%) 194 (89.8%) 379 (87.7%)
other 31 (14.4%) 22 (10.2%) 53 (12.3%)
parole
no 81 (37.5%) 84 (38.9%) 165 (38.2%)
yes 135 (62.5%) 132 (61.1%) 267 (61.8%)
educ
Mean (SD) 3.44 (0.844) 3.52 (0.824) 3.48 (0.834)
Median [Min, Max] 3.00 [2.00, 6.00] 3.00 [2.00, 6.00] 3.00 [2.00, 6.00]

Việc 8: So sánh hai nhóm qua hàm createTable và compareGroups trong package compareGroups:

library(compareGroups)
createTable(compareGroups(finance~age+race+parole+prior, data = arr))
## 
## --------Summary descriptives table by 'finance'---------
## 
## ___________________________________________ 
##               no          yes     p.overall 
##              N=216       N=216              
## ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ 
## age       24.2 (5.73) 25.0 (6.47)   0.203   
## race:                               0.241   
##     black 185 (85.6%) 194 (89.8%)           
##     other 31 (14.4%)  22 (10.2%)            
## parole:                             0.843   
##     no    81 (37.5%)  84 (38.9%)            
##     yes   135 (62.5%) 132 (61.1%)           
## prior     2.99 (2.92) 2.98 (2.88)   0.987   
## ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
compareGroups(~age+race+parole+prior, data = arr)
## 
## 
## -------- Summary of results ---------
## 
## 
##   var    N   method            selection
## 1 age    432 continuous normal ALL      
## 2 race   432 categorical       ALL      
## 3 parole 432 categorical       ALL      
## 4 prior  432 continuous normal ALL

Việc 9: Vẽ biểu đồ phân bố đơn giản cho thời gian bị bắt lại (week) với hàm hist:

hist(arr$week)

Việc 10: Vẽ biểu đồ phân bố của thời gian bị bắt lại (week) với hàm ggplot trong package ggplot2:

library(ggplot2)
ggplot(data=arr, aes(x=week)) + geom_histogram(fill="pink", col="white") + labs(title="Phân bố tuần bị bắt", x="Số đối tượng", y="Số đối tượng")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Việc 11: Vẽ biểu đồ phân bố tuổi khi bị bắt (age) và đường ước tính phân bố (probability density)

p = ggplot(data=arr, aes(x=age)) + geom_histogram(fill="pink", col="white")
p2 = p + geom_density(row="red")
## Warning: Ignoring unknown parameters: row