Digunakan dataset chemicaldata (Box and Youle 1955; Rencher2002) yang terdapat pada package ACSWR. Dataset chemicaldata berisi hasil eksperimen reaksi kimia, dimana dalam eksperimen ini variabel yang digunakan adalah : X1 = temperatur X2 = konsentrasi zat X3 = waktu Y1= persentase material yang tidak berubah Y2 = persentase material yang berubah sesuai yang diharapkan Y3 = persentase material yang berubah tidak sesuai yang diharapkan Misal kita ingin mengetahui besarnya korelasi antara kelompok variabel input (X) dengan kelompok variabel output (Y), maka dengan R pengerjaannya sebagai berikut:
library(CCA)
## Warning: package 'CCA' was built under R version 4.4.2
## Loading required package: fda
## Warning: package 'fda' was built under R version 4.4.2
## Loading required package: splines
## Loading required package: fds
## Warning: package 'fds' was built under R version 4.4.2
## Loading required package: rainbow
## Warning: package 'rainbow' was built under R version 4.4.2
## Loading required package: MASS
## Loading required package: pcaPP
## Warning: package 'pcaPP' was built under R version 4.4.2
## Loading required package: RCurl
## Warning: package 'RCurl' was built under R version 4.4.2
## Loading required package: deSolve
## Warning: package 'deSolve' was built under R version 4.4.2
##
## Attaching package: 'fda'
## The following object is masked from 'package:graphics':
##
## matplot
## Loading required package: fields
## Warning: package 'fields' was built under R version 4.4.2
## Loading required package: spam
## Warning: package 'spam' was built under R version 4.4.2
## Spam version 2.11-0 (2024-10-03) is loaded.
## Type 'help( Spam)' or 'demo( spam)' for a short introduction
## and overview of this package.
## Help for individual functions is also obtained by adding the
## suffix '.spam' to the function name, e.g. 'help( chol.spam)'.
##
## Attaching package: 'spam'
## The following objects are masked from 'package:base':
##
## backsolve, forwardsolve
## Loading required package: viridisLite
##
## Try help(fields) to get started.
library(candisc)
## Warning: package 'candisc' was built under R version 4.4.2
## Loading required package: heplots
## Warning: package 'heplots' was built under R version 4.4.2
## Loading required package: broom
##
## Attaching package: 'candisc'
## The following object is masked from 'package:stats':
##
## cancor
library(ACSWR)
## Warning: package 'ACSWR' was built under R version 4.4.2
Tahap 1. Input data
#Load dataset
data(chemicaldata)
chem = chemicaldata
chem
#Membagi sesuai gugus peubah X dan Y
X <- chem[,4:6]
Y <- chem[,1:3]
# Persebaran Data
library(GGally)
## Warning: package 'GGally' was built under R version 4.4.2
## Loading required package: ggplot2
## Registered S3 method overwritten by 'GGally':
## method from
## +.gg ggplot2
#visualisasi untuk gugus peubah X
ggpairs(X)
#visualisasi untuk gugus peubah Y
ggpairs(Y)
Tahap 2. Korelasi kanonik
#Keterkaitan Linier
library(CCA)
correl<-matcor(X,Y)
correl
## $Xcor
## x1 x2 x3
## x1 1.00000000 -0.4620014 -0.02188275
## x2 -0.46200145 1.0000000 0.17665667
## x3 -0.02188275 0.1766567 1.00000000
##
## $Ycor
## y1 y2 y3
## y1 1.0000000 -0.6078234 -0.8804014
## y2 -0.6078234 1.0000000 0.1822497
## y3 -0.8804014 0.1822497 1.0000000
##
## $XYcor
## x1 x2 x3 y1 y2 y3
## x1 1.00000000 -0.46200145 -0.02188275 -0.6769387 0.40395099 0.5768977
## x2 -0.46200145 1.00000000 0.17665667 -0.2247259 0.07998377 0.2255903
## x3 -0.02188275 0.17665667 1.00000000 -0.4525396 0.39273121 0.3615240
## y1 -0.67693865 -0.22472586 -0.45253956 1.0000000 -0.60782343 -0.8804014
## y2 0.40395099 0.07998377 0.39273121 -0.6078234 1.00000000 0.1822497
## y3 0.57689773 0.22559034 0.36152405 -0.8804014 0.18224967 1.0000000
img.matcor(correl,type=2)
Interpretasi :
Nilai korelasi pearson antar peubah dependen, antar peubah independen, dan korelasi silang antara peubah dependen dengan peubah independen
#Analisis Korelasi Kanonik
chem_cc2 <- candisc::cancor(X, Y)
summary(chem_cc2)
##
## Canonical correlation analysis of:
## 3 X variables: x1, x2, x3
## with 3 Y variables: y1, y2, y3
##
## CanR CanRSQ Eigen percent cum scree
## 1 0.98153 0.963395 26.318349 99.60771 99.61 ******************************
## 2 0.30199 0.091200 0.100353 0.37981 99.99
## 3 0.05733 0.003287 0.003298 0.01248 100.00
##
## Test of H0: The canonical correlations in the
## current row and all that follow are zero
##
## CanR LR test stat approx F numDF denDF Pr(> F)
## 1 0.98153 0.03316 10.7870 9 31.789 1.884e-07 ***
## 2 0.30199 0.90581 0.3549 4 28.000 0.8384
## 3 0.05733 0.99671 0.0495 1 15.000 0.8270
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Raw canonical coefficients
##
## X variables:
## Xcan1 Xcan2 Xcan3
## x1 -0.16062 -0.06944 -0.049094
## x2 -0.14861 -0.12159 0.191179
## x3 -0.21568 0.58392 0.037688
##
## Y variables:
## Ycan1 Ycan2 Ycan3
## y1 0.170794 0.62594 0.37732
## y2 0.069097 0.72999 0.21730
## y3 0.085825 0.71274 0.53900
Interpretasi :
Uji hipotesis menunjukkan bahwa korelasi kanonik pertama berbeda nyata. Artinya korelasi kanonik yang dapat digunakan untuk menjelaskan hubungan antar gugus peubah X dan Y adalah satu korelasi kanonik
Dugaan koefisien gugus X dan Y, yang akan membentuk variat kanonik gugus X dan Y (U dan V)
Kontribusi korelasi kanonik terbesar ditunjukkan pada fungsi kanonik pertama. Artinya untuk menerangkan keragaman total cukup mengambil fungsi kanonik pertama saja
res.cc <- cc(X, Y)
#Korelasi X dan U
res.cc$scores$corr.X.xscores
## [,1] [,2] [,3]
## x1 -0.7002371 -0.2147010 -0.6808608
## x2 -0.2303823 -0.1471813 0.9619052
## x3 -0.4415774 0.8719836 0.2113147
#Korelasi Y dan V
res.cc$scores$corr.Y.yscores
## [,1] [,2] [,3]
## y1 0.9954170 -0.07757607 -0.05591924
## y2 -0.6033066 0.52598224 -0.59946959
## y3 -0.8615072 -0.06822615 0.50314074
#Korelasi X dan V
res.cc$scores$corr.X.yscores
## [,1] [,2] [,3]
## x1 -0.6873014 -0.06483844 -0.03903680
## x2 -0.2261263 -0.04444788 0.05515033
## x3 -0.4334200 0.26333395 0.01211562
#Korelasi Y dan U
res.cc$scores$corr.Y.xscores
## [,1] [,2] [,3]
## y1 0.9770283 -0.02342752 -0.00320610
## y2 -0.5921615 0.15884355 -0.03437028
## y3 -0.8455922 -0.02060390 0.02884731
Interpretasi :
Nilai korelasi antar gugus peubah X dan Y terhadap fungsi kanonik pertama menunjukkan terdapat hubungan positif dan negatif yang erat dan memberikan kontribusi yang cukup besar.