This post was taken from video of Professor Nguyen Van Tuan, you can refer to his video on youtube via link below
my post is to use for study purpose
library(ggplot2)
data(diamonds)
head(diamonds)
## # A tibble: 6 x 10
## carat cut color clarity depth table price x y z
## <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
## 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
## 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
## 4 0.290 Premium I VS2 62.4 58 334 4.2 4.23 2.63
## 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
## 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
##load packages of table
Tai 2 packages “table1” va “compareGroup” tu chuc nang cua Rstudio hoac R
#install.packages(table1)
#install.packages(compareGroups)
library(table1)
##
## Attaching package: 'table1'
## The following objects are masked from 'package:base':
##
## units, units<-
library(compareGroups)
## Loading required package: SNPassoc
## Loading required package: haplo.stats
## Loading required package: survival
## Loading required package: mvtnorm
## Loading required package: parallel
## Registered S3 method overwritten by 'SNPassoc':
## method from
## summary.haplo.glm haplo.stats
table1(~carat+table+price+x+y+z+cut+color, data=diamonds)
| Overall (N=53940) |
|
|---|---|
| carat | |
| Mean (SD) | 0.798 (0.474) |
| Median [Min, Max] | 0.700 [0.200, 5.01] |
| table | |
| Mean (SD) | 57.5 (2.23) |
| Median [Min, Max] | 57.0 [43.0, 95.0] |
| price | |
| Mean (SD) | 3930 (3990) |
| Median [Min, Max] | 2400 [326, 18800] |
| x | |
| Mean (SD) | 5.73 (1.12) |
| Median [Min, Max] | 5.70 [0, 10.7] |
| y | |
| Mean (SD) | 5.73 (1.14) |
| Median [Min, Max] | 5.71 [0, 58.9] |
| z | |
| Mean (SD) | 3.54 (0.706) |
| Median [Min, Max] | 3.53 [0, 31.8] |
| cut | |
| Fair | 1610 (3.0%) |
| Good | 4906 (9.1%) |
| Very Good | 12082 (22.4%) |
| Premium | 13791 (25.6%) |
| Ideal | 21551 (40.0%) |
| color | |
| D | 6775 (12.6%) |
| E | 9797 (18.2%) |
| F | 9542 (17.7%) |
| G | 11292 (20.9%) |
| H | 8304 (15.4%) |
| I | 5422 (10.1%) |
| J | 2808 (5.2%) |
table1(~carat+table+price+x+y+z+cut|color, data=diamonds)
| D (N=6775) |
E (N=9797) |
F (N=9542) |
G (N=11292) |
H (N=8304) |
I (N=5422) |
J (N=2808) |
Overall (N=53940) |
|
|---|---|---|---|---|---|---|---|---|
| carat | ||||||||
| Mean (SD) | 0.658 (0.360) | 0.658 (0.369) | 0.737 (0.398) | 0.771 (0.441) | 0.912 (0.521) | 1.03 (0.579) | 1.16 (0.596) | 0.798 (0.474) |
| Median [Min, Max] | 0.530 [0.200, 3.40] | 0.530 [0.200, 3.05] | 0.700 [0.200, 3.01] | 0.700 [0.230, 3.01] | 0.900 [0.230, 4.13] | 1.00 [0.230, 4.01] | 1.11 [0.230, 5.01] | 0.700 [0.200, 5.01] |
| table | ||||||||
| Mean (SD) | 57.4 (2.21) | 57.5 (2.24) | 57.4 (2.26) | 57.3 (2.15) | 57.5 (2.24) | 57.6 (2.30) | 57.8 (2.31) | 57.5 (2.23) |
| Median [Min, Max] | 57.0 [52.0, 73.0] | 57.0 [44.0, 73.0] | 57.0 [50.0, 95.0] | 57.0 [52.0, 76.0] | 57.0 [50.0, 73.0] | 57.0 [43.0, 70.0] | 58.0 [51.6, 68.0] | 57.0 [43.0, 95.0] |
| price | ||||||||
| Mean (SD) | 3170 (3360) | 3080 (3340) | 3720 (3780) | 4000 (4050) | 4490 (4220) | 5090 (4720) | 5320 (4440) | 3930 (3990) |
| Median [Min, Max] | 1840 [357, 18700] | 1740 [326, 18700] | 2340 [342, 18800] | 2240 [354, 18800] | 3460 [337, 18800] | 3730 [334, 18800] | 4230 [335, 18700] | 2400 [326, 18800] |
| x | ||||||||
| Mean (SD) | 5.42 (0.939) | 5.41 (0.961) | 5.61 (1.01) | 5.68 (1.08) | 5.98 (1.20) | 6.22 (1.25) | 6.52 (1.20) | 5.73 (1.12) |
| Median [Min, Max] | 5.23 [0, 9.42] | 5.23 [3.74, 9.26] | 5.65 [0, 9.24] | 5.64 [0, 9.44] | 6.14 [0, 10.0] | 6.35 [3.94, 10.1] | 6.64 [3.93, 10.7] | 5.70 [0, 10.7] |
| y | ||||||||
| Mean (SD) | 5.42 (0.936) | 5.42 (0.993) | 5.62 (0.999) | 5.68 (1.08) | 5.98 (1.32) | 6.22 (1.24) | 6.52 (1.20) | 5.73 (1.14) |
| Median [Min, Max] | 5.24 [0, 9.34] | 5.24 [3.71, 31.8] | 5.65 [0, 9.13] | 5.63 [0, 9.37] | 6.14 [0, 58.9] | 6.35 [3.90, 10.1] | 6.63 [3.90, 10.5] | 5.71 [0, 58.9] |
| z | ||||||||
| Mean (SD) | 3.34 (0.579) | 3.34 (0.659) | 3.46 (0.626) | 3.51 (0.674) | 3.70 (0.744) | 3.85 (0.770) | 4.03 (0.741) | 3.54 (0.706) |
| Median [Min, Max] | 3.22 [0, 6.27] | 3.22 [2.06, 31.8] | 3.48 [0, 5.73] | 3.48 [0, 6.16] | 3.82 [0, 8.06] | 3.93 [0, 6.31] | 4.11 [2.46, 6.98] | 3.53 [0, 31.8] |
| cut | ||||||||
| Fair | 163 (2.4%) | 224 (2.3%) | 312 (3.3%) | 314 (2.8%) | 303 (3.6%) | 175 (3.2%) | 119 (4.2%) | 1610 (3.0%) |
| Good | 662 (9.8%) | 933 (9.5%) | 909 (9.5%) | 871 (7.7%) | 702 (8.5%) | 522 (9.6%) | 307 (10.9%) | 4906 (9.1%) |
| Very Good | 1513 (22.3%) | 2400 (24.5%) | 2164 (22.7%) | 2299 (20.4%) | 1824 (22.0%) | 1204 (22.2%) | 678 (24.1%) | 12082 (22.4%) |
| Premium | 1603 (23.7%) | 2337 (23.9%) | 2331 (24.4%) | 2924 (25.9%) | 2360 (28.4%) | 1428 (26.3%) | 808 (28.8%) | 13791 (25.6%) |
| Ideal | 2834 (41.8%) | 3903 (39.8%) | 3826 (40.1%) | 4884 (43.3%) | 3115 (37.5%) | 2093 (38.6%) | 896 (31.9%) | 21551 (40.0%) |
t1=compareGroups(~carat+table+price+x+y+z+cut+color, data=diamonds)
t1
##
##
## -------- Summary of results ---------
##
##
## var N method selection
## 1 carat 53940 continuous normal ALL
## 2 table 53940 continuous normal ALL
## 3 price 53940 continuous normal ALL
## 4 x 53940 continuous normal ALL
## 5 y 53940 continuous normal ALL
## 6 z 53940 continuous normal ALL
## 7 cut 53940 categorical ALL
## 8 color 53940 categorical ALL
createTable(t1)
##
## --------Summary descriptives table ---------
##
## _________________________________
## [ALL] N
## N=53940
## ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
## carat 0.80 (0.47) 53940
## table 57.5 (2.23) 53940
## price 3933 (3989) 53940
## x 5.73 (1.12) 53940
## y 5.73 (1.14) 53940
## z 3.54 (0.71) 53940
## cut: 53940
## Fair 1610 (2.98%)
## Good 4906 (9.10%)
## Very Good 12082 (22.4%)
## Premium 13791 (25.6%)
## Ideal 21551 (40.0%)
## color: 53940
## D 6775 (12.6%)
## E 9797 (18.2%)
## F 9542 (17.7%)
## G 11292 (20.9%)
## H 8304 (15.4%)
## I 5422 (10.1%)
## J 2808 (5.21%)
## ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
createTable(compareGroups(cut~carat+table+price+x+y+z, method=c(price=2, carat=2), data=diamonds))
## Warning in cor.test.default(x, as.integer(y), method = "spearman"): Cannot
## compute exact p-value with ties
## Warning in cor.test.default(x, as.integer(y), method = "spearman"): Cannot
## compute exact p-value with ties
##
## --------Summary descriptives table by 'cut'---------
##
## ____________________________________________________________________________________________________________
## Fair Good Very Good Premium Ideal p.overall p.trend
## N=1610 N=4906 N=12082 N=13791 N=21551
## ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
## carat 1.00 [0.70;1.20] 0.82 [0.50;1.01] 0.71 [0.41;1.02] 0.86 [0.41;1.20] 0.54 [0.35;1.01] 0.000 <0.001
## table 59.1 (3.95) 58.7 (2.85) 58.0 (2.12) 58.7 (1.48) 56.0 (1.25) 0.000 0.000
## price 3282 [2050;5206] 3050 [1145;5028] 2648 [912;5373] 3185 [1046;6296] 1810 [878;4678] <0.001 <0.001
## x 6.25 (0.96) 5.84 (1.06) 5.74 (1.10) 5.97 (1.19) 5.51 (1.06) 0.000 <0.001
## y 6.18 (0.96) 5.85 (1.05) 5.77 (1.10) 5.94 (1.26) 5.52 (1.07) 0.000 <0.001
## z 3.98 (0.65) 3.64 (0.65) 3.56 (0.73) 3.65 (0.73) 3.40 (0.66) 0.000 <0.001
## ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯