library(tidyverse); library(ggplot2); library(readxl); library(table1); library(compareGroups)
## Warning: package 'tidyverse' was built under R version 4.0.3
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.3 v purrr 0.3.4
## v tibble 3.1.2 v dplyr 1.0.6
## v tidyr 1.1.1 v stringr 1.4.0
## v readr 1.3.1 v forcats 0.5.0
## Warning: package 'ggplot2' was built under R version 4.0.5
## Warning: package 'tibble' was built under R version 4.0.5
## Warning: package 'dplyr' was built under R version 4.0.5
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
## Warning: package 'table1' was built under R version 4.0.3
##
## Attaching package: 'table1'
## The following objects are masked from 'package:base':
##
## units, units<-
## Loading required package: SNPassoc
## Loading required package: haplo.stats
## Loading required package: arsenal
## Loading required package: survival
## Warning: package 'survival' was built under R version 4.0.5
## Loading required package: mvtnorm
## Registered S3 method overwritten by 'SNPassoc':
## method from
## summary.haplo.glm haplo.stats
arr = read.csv("D:\\Downloads\\tailieu\\R course\\Seminar TDT 2022\\Tai lieu\\Data set\\Arrest dataset.csv")
dim(arr)
## [1] 432 12
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
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
arr = arr %>% mutate(arrest1 = recode(arrest, "1"="Yes", "0"="No"),
fin = recode(finance, "yes" = 1, "no" = 0))
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 Min. :0.0
## Class :character 1st Qu.:0.0
## Mode :character Median :0.5
## Mean :0.5
## 3rd Qu.:1.0
## Max. :1.0
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 | |
| Mean (SD) | 0.500 (0.501) |
| Median [Min, Max] | 0.500 [0, 1.00] |
| 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] |
# Biến fin, arrest (gồm 2 giá trị 0, 1) được báo cáo bằng trung bình và độ lệch chuẩn do R đang hiểu 2 biến này là biến liên tục
# Tóm tắt thống kê theo tình trạng finance
table1(~ age + 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] |
| 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] |
library(compareGroups)
createTable(compareGroups(finance ~ age + race + 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%)
## prior 2.99 (2.92) 2.98 (2.88) 0.987
## ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
hist(arr$week)
hist(arr$week, col ="blue", border ="white", xlab = "Tuần", ylab = "Số đối tượng", main = "Phân bố tuần bị bắt")
p1 = ggplot(arr, aes(x=week))
p1 + geom_histogram(aes(y=..count..), col = "white", fill = "blue") + xlab("Tuần") + ylab("Số đối tượng") + ggtitle("Phân bố tuần bị bắt")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
p2 = ggplot(arr, aes(x=age))
p2 + geom_histogram(aes(y=..density..), col="white", fill="blue") + geom_density(col="red")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
p3 = ggplot(arr, aes(x=educ))
p3 + geom_bar(fill="blue") + labs(x = "Trình độ học vấn", y = "Số đối tượng")
p3 + geom_bar()
p4 = ggplot(arr, aes(x=educ, fill = arrest1))
p4 + geom_bar()
# Biểu đồ 2 có phân loại theo tình trạng bị bắt lại