#DATA ORGANIZATION
attach(mtcars)
head(mtcars,15)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
## Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
## Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
## Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
## Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
## Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
#save as matrice
dat1 <- as.matrix(mtcars)
#jumlah baris
nrow(dat1)
## [1] 32
#jumlah kolom
ncol(dat1)
## [1] 11
#dimensi matrik
dim(dat1)
## [1] 32 11
summary(dat1)
## mpg cyl disp hp
## Min. :10.40 Min. :4.000 Min. : 71.1 Min. : 52.0
## 1st Qu.:15.43 1st Qu.:4.000 1st Qu.:120.8 1st Qu.: 96.5
## Median :19.20 Median :6.000 Median :196.3 Median :123.0
## Mean :20.09 Mean :6.188 Mean :230.7 Mean :146.7
## 3rd Qu.:22.80 3rd Qu.:8.000 3rd Qu.:326.0 3rd Qu.:180.0
## Max. :33.90 Max. :8.000 Max. :472.0 Max. :335.0
## drat wt qsec vs
## Min. :2.760 Min. :1.513 Min. :14.50 Min. :0.0000
## 1st Qu.:3.080 1st Qu.:2.581 1st Qu.:16.89 1st Qu.:0.0000
## Median :3.695 Median :3.325 Median :17.71 Median :0.0000
## Mean :3.597 Mean :3.217 Mean :17.85 Mean :0.4375
## 3rd Qu.:3.920 3rd Qu.:3.610 3rd Qu.:18.90 3rd Qu.:1.0000
## Max. :4.930 Max. :5.424 Max. :22.90 Max. :1.0000
## am gear carb
## Min. :0.0000 Min. :3.000 Min. :1.000
## 1st Qu.:0.0000 1st Qu.:3.000 1st Qu.:2.000
## Median :0.0000 Median :4.000 Median :2.000
## Mean :0.4062 Mean :3.688 Mean :2.812
## 3rd Qu.:1.0000 3rd Qu.:4.000 3rd Qu.:4.000
## Max. :1.0000 Max. :5.000 Max. :8.000
#save as dataframe
dat2 <- as.data.frame(mtcars)
str(dat2)
## 'data.frame': 32 obs. of 11 variables:
## $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
## $ cyl : num 6 6 4 6 8 6 8 4 4 6 ...
## $ disp: num 160 160 108 258 360 ...
## $ hp : num 110 110 93 110 175 105 245 62 95 123 ...
## $ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
## $ wt : num 2.62 2.88 2.32 3.21 3.44 ...
## $ qsec: num 16.5 17 18.6 19.4 17 ...
## $ vs : num 0 0 1 1 0 1 0 1 1 1 ...
## $ am : num 1 1 1 0 0 0 0 0 0 0 ...
## $ gear: num 4 4 4 3 3 3 3 4 4 4 ...
## $ carb: num 4 4 1 1 2 1 4 2 2 4 ...
colfac <- c("vs","am","gear","carb")
dat2[colfac] <- lapply(dat2[colfac], factor)
str(dat2)
## 'data.frame': 32 obs. of 11 variables:
## $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
## $ cyl : num 6 6 4 6 8 6 8 4 4 6 ...
## $ disp: num 160 160 108 258 360 ...
## $ hp : num 110 110 93 110 175 105 245 62 95 123 ...
## $ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
## $ wt : num 2.62 2.88 2.32 3.21 3.44 ...
## $ qsec: num 16.5 17 18.6 19.4 17 ...
## $ vs : Factor w/ 2 levels "0","1": 1 1 2 2 1 2 1 2 2 2 ...
## $ am : Factor w/ 2 levels "0","1": 2 2 2 1 1 1 1 1 1 1 ...
## $ gear: Factor w/ 3 levels "3","4","5": 2 2 2 1 1 1 1 2 2 2 ...
## $ carb: Factor w/ 6 levels "1","2","3","4",..: 4 4 1 1 2 1 4 2 2 4 ...
#STATISTIK DESKRIPTIF
summary(dat2)
## mpg cyl disp hp
## Min. :10.40 Min. :4.000 Min. : 71.1 Min. : 52.0
## 1st Qu.:15.43 1st Qu.:4.000 1st Qu.:120.8 1st Qu.: 96.5
## Median :19.20 Median :6.000 Median :196.3 Median :123.0
## Mean :20.09 Mean :6.188 Mean :230.7 Mean :146.7
## 3rd Qu.:22.80 3rd Qu.:8.000 3rd Qu.:326.0 3rd Qu.:180.0
## Max. :33.90 Max. :8.000 Max. :472.0 Max. :335.0
## drat wt qsec vs am gear carb
## Min. :2.760 Min. :1.513 Min. :14.50 0:18 0:19 3:15 1: 7
## 1st Qu.:3.080 1st Qu.:2.581 1st Qu.:16.89 1:14 1:13 4:12 2:10
## Median :3.695 Median :3.325 Median :17.71 5: 5 3: 3
## Mean :3.597 Mean :3.217 Mean :17.85 4:10
## 3rd Qu.:3.920 3rd Qu.:3.610 3rd Qu.:18.90 6: 1
## Max. :4.930 Max. :5.424 Max. :22.90 8: 1
colnum <- c("mpg","cyl","disp","hp","drat","wt","qsec")
dat3 <- dat2[colnum]
apply(dat3,2,mean)
## mpg cyl disp hp drat wt qsec
## 20.090625 6.187500 230.721875 146.687500 3.596563 3.217250 17.848750
apply(dat3,2,var)
## mpg cyl disp hp drat wt
## 3.632410e+01 3.189516e+00 1.536080e+04 4.700867e+03 2.858814e-01 9.573790e-01
## qsec
## 3.193166e+00
apply(dat3,2,sd)
## mpg cyl disp hp drat wt
## 6.0269481 1.7859216 123.9386938 68.5628685 0.5346787 0.9784574
## qsec
## 1.7869432
apply(dat3,2,min)
## mpg cyl disp hp drat wt qsec
## 10.400 4.000 71.100 52.000 2.760 1.513 14.500
apply(dat3,2,max)
## mpg cyl disp hp drat wt qsec
## 33.900 8.000 472.000 335.000 4.930 5.424 22.900
matkov <- cov(dat3)
matkov
## mpg cyl disp hp drat wt
## mpg 36.324103 -9.1723790 -633.09721 -320.73206 2.19506351 -5.1166847
## cyl -9.172379 3.1895161 199.66028 101.93145 -0.66836694 1.3673710
## disp -633.097208 199.6602823 15360.79983 6721.15867 -47.06401915 107.6842040
## hp -320.732056 101.9314516 6721.15867 4700.86694 -16.45110887 44.1926613
## drat 2.195064 -0.6683669 -47.06402 -16.45111 0.28588135 -0.3727207
## wt -5.116685 1.3673710 107.68420 44.19266 -0.37272073 0.9573790
## qsec 4.509149 -1.8868548 -96.05168 -86.77008 0.08714073 -0.3054816
## qsec
## mpg 4.50914919
## cyl -1.88685484
## disp -96.05168145
## hp -86.77008065
## drat 0.08714073
## wt -0.30548161
## qsec 3.19316613
##VISUALISASI DATA MULTIVARIAT
#Scatterplot
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
plot(wt,mpg)

#matrix scatterplot
plot(mtcars[,c(1,3,4,6)])
#Scatterplot using ggplot
library(ggplot2)
##
## Attaching package: 'ggplot2'
## The following object is masked from 'mtcars':
##
## mpg

plot1 <- ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point() +
theme(legend.position="none")
plot1

#Matrix scatterplot using ggplot
library(GGally)
## Registered S3 method overwritten by 'GGally':
## method from
## +.gg ggplot2
plot2 <- ggpairs(mtcars[,c(1,3,4,6)])
plot2

#Scatterplot dengan marginal
# library
library(ggplot2)
library(ggExtra)
## Warning: package 'ggExtra' was built under R version 3.6.3
# The mtcars dataset is proposed in R
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
# classic plot :
p <- ggplot(mtcars, aes(x=wt, y=mpg, color=cyl)) +
geom_point() +
theme(legend.position="none")
p
# with marginal histogram
p1 <- ggMarginal(p, type="histogram")
p1
# marginal density
p2 <- ggMarginal(p, type="density")
p2
# marginal boxplot
p3 <- ggMarginal(p, type="boxplot")
p3

#Trivariate scatterplot
library(scatterplot3d)
#scatterplot3d(wt,disp,mpg, main="3D Scatterplot",
#color = "red")
#scatterplot3d(wt,disp,mpg, main="3D Scatterplot",
#highlight.3d = TRUE)
#dat <- data.frame(wt,disp,mpg)
#plot(dat)
my.mat <- matrix(runif(25), nrow=5)
dimnames(my.mat) <- list(LETTERS[1:5], letters[11:15])
my.mat
## k l m n o
## A 0.3992742 0.4032561 0.95096151 0.3756202 0.4172937
## B 0.6299352 0.7803125 0.42550212 0.2932776 0.6039669
## C 0.8614901 0.2208201 0.08491196 0.8582320 0.3389465
## D 0.5062308 0.3824951 0.76117637 0.7170406 0.8062484
## E 0.5394654 0.6689070 0.35133562 0.6634317 0.4376115
s3d.dat <- data.frame(cols=as.vector(col(my.mat)),
rows=as.vector(row(my.mat)),
value=as.vector(my.mat))
s3d.dat
## cols rows value
## 1 1 1 0.39927420
## 2 1 2 0.62993524
## 3 1 3 0.86149013
## 4 1 4 0.50623076
## 5 1 5 0.53946538
## 6 2 1 0.40325607
## 7 2 2 0.78031253
## 8 2 3 0.22082007
## 9 2 4 0.38249513
## 10 2 5 0.66890697
## 11 3 1 0.95096151
## 12 3 2 0.42550212
## 13 3 3 0.08491196
## 14 3 4 0.76117637
## 15 3 5 0.35133562
## 16 4 1 0.37562018
## 17 4 2 0.29327757
## 18 4 3 0.85823205
## 19 4 4 0.71704060
## 20 4 5 0.66343170
## 21 5 1 0.41729371
## 22 5 2 0.60396688
## 23 5 3 0.33894654
## 24 5 4 0.80624840
## 25 5 5 0.43761149
scatterplot3d(s3d.dat, type="h", lwd=5, pch=" ",
x.ticklabs=colnames(my.mat), y.ticklabs=rownames(my.mat),
color=grey(25:1/40), main="scatterplot3d - 4")

data(trees)
trees
## Girth Height Volume
## 1 8.3 70 10.3
## 2 8.6 65 10.3
## 3 8.8 63 10.2
## 4 10.5 72 16.4
## 5 10.7 81 18.8
## 6 10.8 83 19.7
## 7 11.0 66 15.6
## 8 11.0 75 18.2
## 9 11.1 80 22.6
## 10 11.2 75 19.9
## 11 11.3 79 24.2
## 12 11.4 76 21.0
## 13 11.4 76 21.4
## 14 11.7 69 21.3
## 15 12.0 75 19.1
## 16 12.9 74 22.2
## 17 12.9 85 33.8
## 18 13.3 86 27.4
## 19 13.7 71 25.7
## 20 13.8 64 24.9
## 21 14.0 78 34.5
## 22 14.2 80 31.7
## 23 14.5 74 36.3
## 24 16.0 72 38.3
## 25 16.3 77 42.6
## 26 17.3 81 55.4
## 27 17.5 82 55.7
## 28 17.9 80 58.3
## 29 18.0 80 51.5
## 30 18.0 80 51.0
## 31 20.6 87 77.0
s3d <- scatterplot3d(trees, type="h", highlight.3d=TRUE,
angle=55, scale.y=0.7, pch=16, main="scatterplot3d - 5")
# Now adding some points to the "scatterplot3d"
s3d$points3d(seq(10,20,2), seq(85,60,-5), seq(60,10,-10),
col="blue", type="h", pch=16)
# Now adding a regression plane to the "scatterplot3d"
attach(trees)
my.lm <- lm(Volume ~ Girth + Height)
s3d$plane3d(my.lm, lty.box = "solid")

library(corrplot)
## corrplot 0.92 loaded
# We use Motor Trend Data #
# Correlation Plot #
M <- cor(mtcars)
head(M)
## mpg cyl disp hp drat wt
## mpg 1.0000000 -0.8521620 -0.8475514 -0.7761684 0.6811719 -0.8676594
## cyl -0.8521620 1.0000000 0.9020329 0.8324475 -0.6999381 0.7824958
## disp -0.8475514 0.9020329 1.0000000 0.7909486 -0.7102139 0.8879799
## hp -0.7761684 0.8324475 0.7909486 1.0000000 -0.4487591 0.6587479
## drat 0.6811719 -0.6999381 -0.7102139 -0.4487591 1.0000000 -0.7124406
## wt -0.8676594 0.7824958 0.8879799 0.6587479 -0.7124406 1.0000000
## qsec vs am gear carb
## mpg 0.41868403 0.6640389 0.5998324 0.4802848 -0.5509251
## cyl -0.59124207 -0.8108118 -0.5226070 -0.4926866 0.5269883
## disp -0.43369788 -0.7104159 -0.5912270 -0.5555692 0.3949769
## hp -0.70822339 -0.7230967 -0.2432043 -0.1257043 0.7498125
## drat 0.09120476 0.4402785 0.7127111 0.6996101 -0.0907898
## wt -0.17471588 -0.5549157 -0.6924953 -0.5832870 0.4276059
corrplot(M, method = "circle")

corrplot(M, method = "ellipse")

corrplot(M, type = "upper")

#Scatterplot 3d interactive
library(shiny)
## Warning: package 'shiny' was built under R version 3.6.3
##
## Attaching package: 'shiny'
## The following object is masked from 'package:ggExtra':
##
## runExample
library(crosstalk)
## Warning: package 'crosstalk' was built under R version 3.6.3
##
## Attaching package: 'crosstalk'
## The following object is masked from 'package:shiny':
##
## getDefaultReactiveDomain
library(rgl)
## Warning: package 'rgl' was built under R version 3.6.3
library(plot3D)
library(plot3Drgl)
#scatter3Drgl(wt,disp,mpg)
#stars
## 'Spider' or 'Radar' plot:
stars(mtcars[1:6, 1:7], locations = c(0, 0), radius = FALSE,
key.loc = c(0, 0), main = "Motor Trend Cars", lty = 1,
col.lines = rainbow(6))

#chernoff faces
library(aplpack)
faces()
## effect of variables:
## modified item Var
## "height of face " "Var1"
## "width of face " "Var2"
## "structure of face" "Var3"
## "height of mouth " "Var1"
## "width of mouth " "Var2"
## "smiling " "Var3"
## "height of eyes " "Var1"
## "width of eyes " "Var2"
## "height of hair " "Var3"
## "width of hair " "Var1"
## "style of hair " "Var2"
## "height of nose " "Var3"
## "width of nose " "Var1"
## "width of ear " "Var2"
## "height of ear " "Var3"
faces(face.type=1)
## effect of variables:
## modified item Var
## "height of face " "Var1"
## "width of face " "Var2"
## "structure of face" "Var3"
## "height of mouth " "Var1"
## "width of mouth " "Var2"
## "smiling " "Var3"
## "height of eyes " "Var1"
## "width of eyes " "Var2"
## "height of hair " "Var3"
## "width of hair " "Var1"
## "style of hair " "Var2"
## "height of nose " "Var3"
## "width of nose " "Var1"
## "width of ear " "Var2"
## "height of ear " "Var3"

faces(rbind(1:3,5:3,3:5,5:7))

## effect of variables:
## modified item Var
## "height of face " "Var1"
## "width of face " "Var2"
## "structure of face" "Var3"
## "height of mouth " "Var1"
## "width of mouth " "Var2"
## "smiling " "Var3"
## "height of eyes " "Var1"
## "width of eyes " "Var2"
## "height of hair " "Var3"
## "width of hair " "Var1"
## "style of hair " "Var2"
## "height of nose " "Var3"
## "width of nose " "Var1"
## "width of ear " "Var2"
## "height of ear " "Var3"
data(longley)
faces(longley[1:9,],face.type=0)

## effect of variables:
## modified item Var
## "height of face " "GNP.deflator"
## "width of face " "GNP"
## "structure of face" "Unemployed"
## "height of mouth " "Armed.Forces"
## "width of mouth " "Population"
## "smiling " "Year"
## "height of eyes " "Employed"
## "width of eyes " "GNP.deflator"
## "height of hair " "GNP"
## "width of hair " "Unemployed"
## "style of hair " "Armed.Forces"
## "height of nose " "Population"
## "width of nose " "Year"
## "width of ear " "Employed"
## "height of ear " "GNP.deflator"
faces(longley[1:9,],face.type=1)

## effect of variables:
## modified item Var
## "height of face " "GNP.deflator"
## "width of face " "GNP"
## "structure of face" "Unemployed"
## "height of mouth " "Armed.Forces"
## "width of mouth " "Population"
## "smiling " "Year"
## "height of eyes " "Employed"
## "width of eyes " "GNP.deflator"
## "height of hair " "GNP"
## "width of hair " "Unemployed"
## "style of hair " "Armed.Forces"
## "height of nose " "Population"
## "width of nose " "Year"
## "width of ear " "Employed"
## "height of ear " "GNP.deflator"
faces(mtcars[1:20,1:7])

## effect of variables:
## modified item Var
## "height of face " "mpg"
## "width of face " "cyl"
## "structure of face" "disp"
## "height of mouth " "hp"
## "width of mouth " "drat"
## "smiling " "wt"
## "height of eyes " "qsec"
## "width of eyes " "mpg"
## "height of hair " "cyl"
## "width of hair " "disp"
## "style of hair " "hp"
## "height of nose " "drat"
## "width of nose " "wt"
## "width of ear " "qsec"
## "height of ear " "mpg"