#install.packages(c("tidyverse", "ggplot2", "readxl", "table1", "compareGroups"))
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.6 v dplyr 1.0.8
## v tidyr 1.2.0 v stringr 1.4.0
## v readr 2.1.2 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(ggplot2)
library(readxl)
library(table1)
##
## Attaching package: 'table1'
## The following objects are masked from 'package:base':
##
## units, units<-
library(compareGroups)
arr = read.csv("C:\\UTS\\Arrest dataset.csv", header= TRUE)
dim(arr)
## [1] 432 12
head(arr)
tail(arr)
arr$arrest1[arr$arrest == 1] = "Yes"
arr$arrest1[arr$arrest == 0] = "No"
arr$fin[arr$finance == "yes"] = 1
arr$fin[arr$finance == "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 Length:432
## Class :character Class :character
## Mode :character Mode :character
##
##
##
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] |
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] |
# Cách 1:
t = compareGroups(finance ~ age + race + prior + parole, data=arr)
createTable(t)
##
## --------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
## parole: 0.843
## no 81 (37.5%) 84 (38.9%)
## yes 135 (62.5%) 132 (61.1%)
## ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
# Cách 2:
createTable(compareGroups(finance ~ age + race + prior + parole, 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
## parole: 0.843
## no 81 (37.5%) 84 (38.9%)
## yes 135 (62.5%) 132 (61.1%)
## ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
hist(arr$week)
hist(arr$week, col="blue", border="white")
hist(arr$week, col="blue", border="white", main="Distribution of Time to arrest (week)")
hist(arr$week, col="blue", border="white", main="Distribution of time to arrest (week)", xlab="Week", ylab="Number of participants")
ggplot(data=arr, aes(x=week)) + geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(data=arr, aes(x=week)) + geom_histogram(fill="blue", col="white")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(data=arr, aes(x=week)) + geom_histogram(fill="blue", col="white") + labs(title="Distribution of Time to arrest", x="Number of participants", y="Number of participants")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(data=arr, aes(x=age)) + geom_histogram(fill="blue", col="white")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(data=arr, aes(x=age)) + geom_histogram(aes(y=..density..), fill="blue", col="white")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(data=arr, aes(x=age)) + geom_histogram(aes(y=..density..), fill="blue", col="white") + geom_density(col="red")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(data=arr, aes(x=educ)) + geom_bar(col="blue")
ggplot(data=arr, aes(x=educ)) + geom_bar(fill="blue")
ggplot(data=arr, aes(x=educ, fill=arrest)) + geom_bar()
ggplot(data=arr, aes(x=educ, fill=arrest1)) + geom_bar()