Penyelidikan ilmiah merupakan proses belajar berulang yang bertujuan untuk memperoleh penjelasan atas suatu fenomena melalui eksperimen atau observasi. Selama proses pembelajaran berulang ini, variabel sering ditambahkan atau dihapus dari pembelajaran.
Dengan demikian, kompleksitas sebagian besar fenomena memerlukan peneliti untuk mengumpulkan pengamatan pada banyak variabel berbeda, sehingga untuk itulah kita butuh analisis peubah ganda.
Dalam melakukan analisis peubah ganda kita harus memahami aspek-aspek di dalam analisis peubah ganda terlebih dahulu.
Tujuan pembelajaran pada modul 1 ini adalah memberikan pengetahuan tentang aspek aspek dalam analisis peubah ganda dan mampu melakukan analisis deksriptif dan visualisasi pada data multivariat.
Pada kegiatan modul 1 diperlukan beberapa material berupa software, yaitu:
1) Software R (direkomendasikan versi terbaru).
2) Software R Studio.
Cobalah Sintaksis R berikut:
Notes : “Gunakan tombol kecil di pojok kanan atas jendela kode untuk menyalin semua sintaks.”
#DATA ORGANIZATION
attach(mtcars)
#save as matrice
dat1 <- as.matrix(mtcars)
nrow(dat1)
ncol(dat1)
dim(dat1)
#save as dataframe
dat2 <- as.data.frame(mtcars)
str(dat2)
colfac <- c("vs","am","gear","carb")
dat2[colfac] <- lapply(dat2[colfac], factor)
str(dat2)
#STATISTIK DESKRIPTIF
summary(dat2)
colnum <- c("mpg","cyl","disp","hp","drat","wt","qsec")
dat3 <- dat2[colnum]
apply(dat3,2,mean)
apply(dat3,2,var)
apply(dat3,2,sd)
apply(dat3,2,min)
apply(dat3,2,max)
matkov <- cov(dat3)
matkov
##VISUALISASI DATA MULTIVARIAT
#Scatterplot
head(mtcars)
plot(wt,mpg)
#matrix scatterplot
plot(mtcars[,c(1,3,4,6)])
#Scatterplot using ggplot
plot1 <- ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() +
theme(legend.position="none")
plot1
#Matrix scatterplot using ggplot
library(GGally)
plot2 <- ggpairs(mtcars[,c(1,3,4,6)])
plot2
#Scatterplot dengan marginal
# library
library(ggplot2)
library(ggExtra)
# The mtcars dataset is proposed in R
head(mtcars)
# 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
#install.packages("scatterplot3d")
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)
#install.packages("corrplot")
library(corrplot)
# We use Motor Trend Data #
# Correlation Plot #
M <- cor(mtcars)
head(M)
corrplot(M, method = "circle")
corrplot(M, method = "ellipse")
corrplot(M, type = "upper")
#Scatterplot 3d interactive
library(shiny)
library(crosstalk)
library(rgl)
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
#install.packages("aplpack")
library(aplpack)
faces()
faces(face.type=1)
faces(rbind(1:3,5:3,3:5,5:7))
data(longley)
faces(longley[1:9,],face.type=0)
faces(longley[1:9,],face.type=1)
faces(mtcars[1:20,1:7])
paket <- c("ggplot2","GGally","ggExtra","scatterplot3d","corrplot",
"shiny","crosstalk","rgl","plot3D","plot3Drgl","aplpack","gridExtra")
install.packages(paket) # Install packages
paket <- c("ggplot2","GGally","ggExtra","scatterplot3d","corrplot",
"shiny","crosstalk","rgl","plot3D","plot3Drgl","aplpack","gridExtra")
lapply(paket,require,character.only=TRUE) # Load library
## The following object is masked from package:ggplot2:
##
## mpg
## [1] 32
## [1] 11
## [1] 32 11
## '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 ...
## '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 ...
## 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
## mpg cyl disp hp drat wt qsec
## 20.090625 6.187500 230.721875 146.687500 3.596563 3.217250 17.848750
## 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
## mpg cyl disp hp drat wt
## 6.0269481 1.7859216 123.9386938 68.5628685 0.5346787 0.9784574
## qsec
## 1.7869432
## mpg cyl disp hp drat wt qsec
## 10.400 4.000 71.100 52.000 2.760 1.513 14.500
## mpg cyl disp hp drat wt qsec
## 33.900 8.000 472.000 335.000 4.930 5.424 22.900
## 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
## 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
#Scatterplot using ggplot
plot1 <- ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() +
theme(legend.position="none")
plot1
#Scatterplot dengan marginal
# library
library(ggplot2)
library(ggExtra)
# 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") + ggtitle("Classic Plot")
pclassic <- p
# with marginal histogram
p <- p + ggtitle("with marginal histogram")
p1 <- ggMarginal(p, type="histogram")
# marginal density
p <- p + ggtitle("with marginal density")
p2 <- ggMarginal(p, type="density")
# marginal boxplot
p <- p + ggtitle("with marginal boxplot")
p3 <- ggMarginal(p, type="boxplot")
grid.arrange(pclassic,p1,p2,p3,ncol=2)
#Trivariate scatterplot
library(scatterplot3d)
scatterplot3d(wt,disp,mpg, main="3D Scatterplot", color = "red")
# ll.packages("corrplot")
library(corrplot)
# 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
#Scatterplot 3d interactive
library(shiny)
library(crosstalk)
library(rgl)
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))
## 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"
## 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"
## 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"
## 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"
## 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"
## 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"