R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

library(table1)
## 
## Attaching package: 'table1'
## The following objects are masked from 'package:base':
## 
##     units, units<-
library(magrittr)
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 tidyr::extract()   masks magrittr::extract()
## x dplyr::filter()    masks stats::filter()
## x dplyr::lag()       masks stats::lag()
## x purrr::set_names() masks magrittr::set_names()
library(compareGroups)
t<-"C:\\Users\\hntn\\OneDrive - Sun Hydraulics\\Hoa\\Ftu\\DATA ANALYSIS\\Dataset for TDTU workshop 4-2022\\Arrest dataset.csv"
arr<-read.csv(t)
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
dim(arr)
## [1] 432  12
arr$arrest1[arr$arrest==1]="Yes"
arr$arrest1[arr$arrest==0]="No"

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 arrest1
## 1      no     Yes
## 2      no     Yes
## 3      no     Yes
## 4      no      No
## 5      no      No
## 6      no      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 arrest1
## 427      no     Yes
## 428      no      No
## 429      no      No
## 430      no      No
## 431     yes      No
## 432     yes      No
arr$fin[arr$finance=="yes"]=1
arr$fin[arr$finance=="no"]=0
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 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
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  
##                                       
##                                       
## 

#TOM TAT THONG KE DU LIEU QUA HAM TABLE 1 TRONG PACKAGE TABLE1

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]

#TOM TAT THONG KE THEO TINH TRANG HO TRO TAI CHINH (FINANCE)

table1(~age+finance+fin+arrest+arrest1+race+parole+educ|fin, data=arr)
0
(N=216)
1
(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]
table1(~age+finance+fin+arrest+arrest1+race+parole+as.factor(educ)|fin, data=arr)
0
(N=216)
1
(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%)
as.factor(educ)
2 17 (7.9%) 7 (3.2%) 24 (5.6%)
3 117 (54.2%) 122 (56.5%) 239 (55.3%)
4 57 (26.4%) 62 (28.7%) 119 (27.5%)
5 21 (9.7%) 18 (8.3%) 39 (9.0%)
6 4 (1.9%) 7 (3.2%) 11 (2.5%)

SO SANH HAI NHOM QUA HAM CREATETABLE VA COMPAREGROUPS TRONG PACKAGE COMPAREGROUPS

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%)           
## ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

Ve bieu do phan bo don gian cho thoi gian bi bat lai (week) voi ham hist:

hist(arr$week)

Ve bieu do phan bo don gian cho thoi gian bi bat lai (week) voi ham GGPLOT

ggplot(data=arr, aes(x=week))+geom_histogram(fill="violet", col="black")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.