load library

library(table1)
## 
## Attaching package: 'table1'
## The following objects are masked from 'package:base':
## 
##     units, units<-
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.3
library(GGally)
## Warning: package 'GGally' was built under R version 4.3.3
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
library(explore)
## 
## Attaching package: 'explore'
## The following object is masked from 'package:GGally':
## 
##     rescale01
library(gridExtra)

load data

dữ liệu này về ô tô mpg: số dặm trên galon cyl: số xilanh disp: khoảng cách trục hp: mã lực

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
temp=mtcars[,c("mpg","cyl","disp","wt","gear")]
head(temp)
##                    mpg cyl disp    wt gear
## Mazda RX4         21.0   6  160 2.620    4
## Mazda RX4 Wag     21.0   6  160 2.875    4
## Datsun 710        22.8   4  108 2.320    4
## Hornet 4 Drive    21.4   6  258 3.215    3
## Hornet Sportabout 18.7   8  360 3.440    3
## Valiant           18.1   6  225 3.460    3

phân tích mô tả

table1(~mpg+factor(cyl)+disp+hp+wt|gear,data=mtcars)
## Warning in table1.formula(~mpg + factor(cyl) + disp + hp + 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]
factor(cyl)
4 1 (6.7%) 8 (66.7%) 2 (40.0%) 11 (34.4%)
6 2 (13.3%) 4 (33.3%) 1 (20.0%) 7 (21.9%)
8 12 (80.0%) 0 (0%) 2 (40.0%) 14 (43.8%)
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]
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]

phân tích khám phá

explore_all(mtcars,target=cyl)

# more phân tích khám phá

explore_all(temp,target=cyl)

# phân tích tương quan với GGally

ggpairs(mtcars)

# phân tích biểu đồ vơi ggplot2

p=ggplot(data=mtcars,aes(x-disp,y=mpg))
p1=p+geom_point()
p2=p+geom_point()+geom_smooth()
p3=ggplot(data = mtcars, aes(x = mpg)) +     geom_histogram(aes(y = ..density..), color = "white", fill = "blue", bins = 20) +     geom_density(color = "red")