Load libraries and datasets

library(explore)
library(GGally)
## Loading required package: ggplot2
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
## 
## Attaching package: 'GGally'
## The following object is masked from 'package:explore':
## 
##     rescale01
library(ggplot2)
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
## 
## Attaching package: 'compareGroups'
## The following object is masked from 'package:explore':
## 
##     report
# load data

data(mtcars)
head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
data("ToothGrowth")
head(ToothGrowth)
##    len supp dose
## 1  4.2   VC  0.5
## 2 11.5   VC  0.5
## 3  7.3   VC  0.5
## 4  5.8   VC  0.5
## 5  6.4   VC  0.5
## 6 10.0   VC  0.5

Descriptive Analysis with table1

table1(~mpg + cyl + disp + hp + drat + wt, data=mtcars)
Overall
(n=32)
mpg
Mean (SD) 20.1 (6.03)
Median [Min, Max] 19.2 [10.4, 33.9]
cyl
Mean (SD) 6.19 (1.79)
Median [Min, Max] 6.00 [4.00, 8.00]
disp
Mean (SD) 231 (124)
Median [Min, Max] 196 [71.1, 472]
hp
Mean (SD) 147 (68.6)
Median [Min, Max] 123 [52.0, 335]
drat
Mean (SD) 3.60 (0.535)
Median [Min, Max] 3.70 [2.76, 4.93]
wt
Mean (SD) 3.22 (0.978)
Median [Min, Max] 3.33 [1.51, 5.42]
table1(~mpg + cyl + disp + hp + drat + wt | gear, data=mtcars)
## Warning in table1.formula(~mpg + cyl + disp + hp + drat + wt | gear, data =
## mtcars): Terms to the right of '|' in formula 'x' define table columns and
## are expected to be factors with meaningful labels.
3
(n=15)
4
(n=12)
5
(n=5)
Overall
(n=32)
mpg
Mean (SD) 16.1 (3.37) 24.5 (5.28) 21.4 (6.66) 20.1 (6.03)
Median [Min, Max] 15.5 [10.4, 21.5] 22.8 [17.8, 33.9] 19.7 [15.0, 30.4] 19.2 [10.4, 33.9]
cyl
Mean (SD) 7.47 (1.19) 4.67 (0.985) 6.00 (2.00) 6.19 (1.79)
Median [Min, Max] 8.00 [4.00, 8.00] 4.00 [4.00, 6.00] 6.00 [4.00, 8.00] 6.00 [4.00, 8.00]
disp
Mean (SD) 326 (94.9) 123 (38.9) 202 (115) 231 (124)
Median [Min, Max] 318 [120, 472] 131 [71.1, 168] 145 [95.1, 351] 196 [71.1, 472]
hp
Mean (SD) 176 (47.7) 89.5 (25.9) 196 (103) 147 (68.6)
Median [Min, Max] 180 [97.0, 245] 94.0 [52.0, 123] 175 [91.0, 335] 123 [52.0, 335]
drat
Mean (SD) 3.13 (0.274) 4.04 (0.312) 3.92 (0.390) 3.60 (0.535)
Median [Min, Max] 3.08 [2.76, 3.73] 3.92 [3.69, 4.93] 3.77 [3.54, 4.43] 3.70 [2.76, 4.93]
wt
Mean (SD) 3.89 (0.833) 2.62 (0.633) 2.63 (0.819) 3.22 (0.978)
Median [Min, Max] 3.73 [2.47, 5.42] 2.70 [1.62, 3.44] 2.77 [1.51, 3.57] 3.33 [1.51, 5.42]
table1(~len + dose | supp, data=ToothGrowth)
OJ
(n=30)
VC
(n=30)
Overall
(n=60)
len
Mean (SD) 20.7 (6.61) 17.0 (8.27) 18.8 (7.65)
Median [Min, Max] 22.7 [8.20, 30.9] 16.5 [4.20, 33.9] 19.3 [4.20, 33.9]
dose
Mean (SD) 1.17 (0.634) 1.17 (0.634) 1.17 (0.629)
Median [Min, Max] 1.00 [0.500, 2.00] 1.00 [0.500, 2.00] 1.00 [0.500, 2.00]

Explore analysis with package ‘explore’

explore_all(mtcars)

explore_all(mtcars, target=cyl)

temp = mtcars[, c("gear", "mpg", "cyl", "wt")]
head(temp)
##                   gear  mpg cyl    wt
## Mazda RX4            4 21.0   6 2.620
## Mazda RX4 Wag        4 21.0   6 2.875
## Datsun 710           4 22.8   4 2.320
## Hornet 4 Drive       3 21.4   6 3.215
## Hornet Sportabout    3 18.7   8 3.440
## Valiant              3 18.1   6 3.460
explore_all(temp, target = gear)

Graphical analysis using ggplot2

ggpairs(mtcars)

p = ggplot(data=mtcars, aes(x=wt, y=mpg))
p1 = p + geom_point() 
p1

p = ggplot(data=mtcars, aes(x=wt, y=mpg))
p2 = p + geom_point() + geom_smooth()
p2
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

p = ggplot(data=mtcars, aes(x=wt, y=mpg))
p3 = p + geom_point() + geom_smooth(method="lm", formula=y ~ x + I(x^2))
p3

ggplot(data=ToothGrowth, aes(x=dose, y=len, col=supp, fill=supp)) + geom_boxplot(col="black")

ggplot(data=ToothGrowth, aes(x=factor(dose), y=len, col=supp, fill=supp)) + geom_boxplot(col="black")

ggplot(data=ToothGrowth, aes(x=factor(dose), y=len, col=supp, fill=supp)) + geom_boxplot(col="black") + geom_jitter(alpha=0.7)