library(car)
## Loading required package: carData
library(CCA)
## Loading required package: fda
## Loading required package: splines
## Loading required package: fds
## Loading required package: rainbow
## Loading required package: MASS
## Loading required package: pcaPP
## Loading required package: RCurl
## Loading required package: deSolve
## 
## Attaching package: 'fda'
## The following object is masked from 'package:graphics':
## 
##     matplot
## Loading required package: fields
## Loading required package: spam
## 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)
## Loading required package: heplots
## Loading required package: broom
## 
## Attaching package: 'candisc'
## The following object is masked from 'package:stats':
## 
##     cancor
library(readxl)
dataset <- read_excel("dataset.xlsx")
dataCCA <- dataset
dataCCA
## # A tibble: 34 × 6
##       X1    X2    X3    Y1    Y2    Y3
##    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
##  1 15.3  14.6  14.4   9.38  9.45  9.55
##  2  9.01  8.42  8.15  9.59  9.72  9.82
##  3  6.63  5.92  5.95  9.07  9.18  9.28
##  4  7.12  6.78  6.68  9.21  9.25  9.34
##  5  8.09  7.62  7.58  8.59  8.68  8.81
##  6 12.8  11.9  11.8   8.30  8.39  8.5 
##  7 15.2  14.6  14.0   8.89  8.92  9.02
##  8 12.6  11.6  11.1   8.07  8.17  8.28
##  9  4.9   4.45  4.52  8.12  8.15  8.28
## 10  6.12  6.24  5.69 10.2  10.4  10.4 
## # ℹ 24 more rows
CCA <- as.matrix(dataCCA) #mengubah menjadi matriks
rata2<- colMeans(CCA) #rata rata masing masing varibael
rata2
##        X1        X2        X3        Y1        Y2        Y3 
## 10.762353 10.243235 10.089118  8.761618  8.882206  8.972059
n <- nrow(dataCCA) #menghitung jumlah baris pada data
n
## [1] 34
p <- ncol (dataCCA)
p
## [1] 6
kovarian <- cov(CCA) #covarian dari data CCA yaitu gabungan dari cov X dan Y

d<- mahalanobis(CCA,rata2,kovarian) #Sebelum melakukan KK kita perlu menghitung jarak mahalanobis untuk melihat normalitas data  
d
##  [1]  3.178702  1.859418  1.632642  3.223461  2.679754  1.818222  6.360051
##  [8]  6.980799  6.829145  6.866996 14.695965  3.995753  3.892642  7.187205
## [15]  3.461068  2.553188 11.936217  8.585601  4.845631  4.173037  5.134871
## [22]  2.834425  3.151160  4.286571  3.026196  4.626417  6.149263  5.035178
## [29]  2.748290  8.815909 17.158253  3.964074  9.889131 14.424764
qchisq(ppoints(n), df=p)  #q chisquare
##  [1]  1.008342  1.553205  1.924941  2.233120  2.507324  2.760624  3.000186
##  [8]  3.230487  3.454599  3.674787  3.892832  4.110211  4.328215  4.548029
## [15]  4.770788  4.997626  5.229717  5.468318  5.714817  5.970786  6.238051
## [22]  6.518782  6.815625  7.131875  7.471751  7.840804  8.246593  8.699829
## [29]  9.216475  9.821954 10.560668 11.521732 12.932673 15.828291
qqplot (qchisq(ppoints(n), df=p),d,xlim=c(0,30),pch=20,col="black",ylim=c(0,30),main="QQ Plot Data",
        ylab="Jarak Mahalanobis") #Asumsi normalitasnya terpenuhi berdasarkan plot karena berada dalam garis berarti berdistribusi normal
abline(a=0,b=1,col="red")

qchisq(ppoints(n),df=p)
##  [1]  1.008342  1.553205  1.924941  2.233120  2.507324  2.760624  3.000186
##  [8]  3.230487  3.454599  3.674787  3.892832  4.110211  4.328215  4.548029
## [15]  4.770788  4.997626  5.229717  5.468318  5.714817  5.970786  6.238051
## [22]  6.518782  6.815625  7.131875  7.471751  7.840804  8.246593  8.699829
## [29]  9.216475  9.821954 10.560668 11.521732 12.932673 15.828291
#Untuk melihat asumsi multikolinearitas
dataCCA
## # A tibble: 34 × 6
##       X1    X2    X3    Y1    Y2    Y3
##    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
##  1 15.3  14.6  14.4   9.38  9.45  9.55
##  2  9.01  8.42  8.15  9.59  9.72  9.82
##  3  6.63  5.92  5.95  9.07  9.18  9.28
##  4  7.12  6.78  6.68  9.21  9.25  9.34
##  5  8.09  7.62  7.58  8.59  8.68  8.81
##  6 12.8  11.9  11.8   8.30  8.39  8.5 
##  7 15.2  14.6  14.0   8.89  8.92  9.02
##  8 12.6  11.6  11.1   8.07  8.17  8.28
##  9  4.9   4.45  4.52  8.12  8.15  8.28
## 10  6.12  6.24  5.69 10.2  10.4  10.4 
## # ℹ 24 more rows
X <- dataCCA[, c("X1", "X2", "X3")]
X
## # A tibble: 34 × 3
##       X1    X2    X3
##    <dbl> <dbl> <dbl>
##  1 15.3  14.6  14.4 
##  2  9.01  8.42  8.15
##  3  6.63  5.92  5.95
##  4  7.12  6.78  6.68
##  5  8.09  7.62  7.58
##  6 12.8  11.9  11.8 
##  7 15.2  14.6  14.0 
##  8 12.6  11.6  11.1 
##  9  4.9   4.45  4.52
## 10  6.12  6.24  5.69
## # ℹ 24 more rows
Y <- dataCCA[, c("Y1", "Y2", "Y3")]
Y
## # A tibble: 34 × 3
##       Y1    Y2    Y3
##    <dbl> <dbl> <dbl>
##  1  9.38  9.45  9.55
##  2  9.59  9.72  9.82
##  3  9.07  9.18  9.28
##  4  9.21  9.25  9.34
##  5  8.59  8.68  8.81
##  6  8.30  8.39  8.5 
##  7  8.89  8.92  9.02
##  8  8.07  8.17  8.28
##  9  8.12  8.15  8.28
## 10 10.2  10.4  10.4 
## # ℹ 24 more rows
korelasi <- matcor(X,Y)
korelasi #mencari apakah ada yang diatas 0.8 hasil cor nya
## $Xcor
##           X1        X2        X3
## X1 1.0000000 0.9968177 0.9972616
## X2 0.9968177 1.0000000 0.9987520
## X3 0.9972616 0.9987520 1.0000000
## 
## $Ycor
##           Y1        Y2       Y3
## Y1 1.0000000 0.9983514 0.998037
## Y2 0.9983514 1.0000000 0.999411
## Y3 0.9980370 0.9994110 1.000000
## 
## $XYcor
##            X1         X2         X3         Y1         Y2         Y3
## X1  1.0000000  0.9968177  0.9972616 -0.3951843 -0.3969859 -0.3986361
## X2  0.9968177  1.0000000  0.9987520 -0.4101743 -0.4113221 -0.4125987
## X3  0.9972616  0.9987520  1.0000000 -0.4142962 -0.4157257 -0.4171227
## Y1 -0.3951843 -0.4101743 -0.4142962  1.0000000  0.9983514  0.9980370
## Y2 -0.3969859 -0.4113221 -0.4157257  0.9983514  1.0000000  0.9994110
## Y3 -0.3986361 -0.4125987 -0.4171227  0.9980370  0.9994110  1.0000000
img.matcor(korelasi, type=2)

analisis <- candisc::cancor(X,Y)
summary (analisis)
## 
## 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.48088 0.2312493 0.3008119 85.1922  85.19 ******************************
## 2 0.22200 0.0492836 0.0518384 14.6810  99.87 *****                         
## 3 0.02116 0.0004476 0.0004478  0.1268 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.48088      0.73054  1.04493     9 68.295  0.4144
## 2 0.22200      0.95029  0.37441     4 58.000  0.8260
## 3 0.02116      0.99955  0.01343     1 30.000  0.9085
## 
## Raw canonical coefficients
## 
##    X  variables: 
##       Xcan1   Xcan2   Xcan3
## X1  1.26447  1.8909 -1.1826
## X2  0.12974 -2.8832 -2.6350
## X3 -1.61104  1.0128  3.8170
## 
##    Y  variables: 
##       Ycan1    Ycan2   Ycan3
## Y1  1.39602  14.6894  12.289
## Y2  0.39315   9.0547 -33.870
## Y3 -0.70440 -23.9981  21.736
hasil <- cc(X,Y)
hasil
## $cor
## [1] 0.48088390 0.22199921 0.02115657
## 
## $names
## $names$Xnames
## [1] "X1" "X2" "X3"
## 
## $names$Ynames
## [1] "Y1" "Y2" "Y3"
## 
## $names$ind.names
##  [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12" "13" "14" "15"
## [16] "16" "17" "18" "19" "20" "21" "22" "23" "24" "25" "26" "27" "28" "29" "30"
## [31] "31" "32" "33" "34"
## 
## 
## $xcoef
##          [,1]      [,2]      [,3]
## X1  1.2644667  1.890923 -1.182560
## X2  0.1297381 -2.883217 -2.634965
## X3 -1.6110390  1.012772  3.816965
## 
## $ycoef
##          [,1]       [,2]      [,3]
## Y1  1.3960219  14.689415  12.28876
## Y2  0.3931482   9.054677 -33.87048
## Y3 -0.7044044 -23.998129  21.73638
## 
## $scores
## $scores$xscores
##             [,1]        [,2]        [,3]
##  [1,] -0.6794862  0.37682535 -0.34150444
##  [2,]  0.6716592 -0.02066714 -0.52511976
##  [3,]  0.8821690  0.45887824  0.47946349
##  [4,]  0.4372740 -0.35481185  0.42032348
##  [5,]  0.3228516 -0.03102330  0.49513775
##  [6,]  0.1179836  0.86433812 -0.36842147
##  [7,] -0.1606463 -0.18874855 -1.72367907
##  [8,]  0.8763834  0.72123899 -1.79608622
##  [9,]  0.8077125 -0.02235471  0.94043162
## [10,]  0.6976774 -1.69144257 -0.75303025
## [11,]  0.7401287 -1.13571460  0.21554378
## [12,]  0.7074795 -0.67294157 -0.87816190
## [13,]  0.2915970  0.65268891 -0.42595161
## [14,]  1.1869204  1.65385084 -1.67009275
## [15,]  0.4037354  1.07563363 -0.11864763
## [16,]  0.5968116  0.04644390  0.65132739
## [17,]  0.7904089 -1.34143074  0.03120273
## [18,] -1.3421267  0.28685147  1.30512982
## [19,] -1.6975466  1.06162386 -0.25847995
## [20,]  0.4203899 -0.12354016  0.63110384
## [21,]  0.2936434 -1.32624872  0.69795439
## [22,]  1.0949283 -0.50298564  0.03991036
## [23,]  0.5611987 -0.67371567  0.16897204
## [24,]  1.1099876 -0.10509585 -0.71504330
## [25,]  0.1963191  0.14162050  1.00605216
## [26,] -0.6388690  0.56514933  0.71402185
## [27,] -0.4779946 -0.50403003  1.29285721
## [28,] -0.9049323  0.38332679  1.61458649
## [29,] -1.3519671 -0.63368441 -0.05601851
## [30,] -1.3941992 -1.92781393  0.75287220
## [31,] -0.4689360  3.34025330  0.66970951
## [32,]  0.4295193  0.57323503  1.30181075
## [33,] -1.3105358 -0.48483942 -2.61341990
## [34,] -3.2095389 -0.46086942 -1.18475411
## 
## $scores$yscores
##             [,1]        [,2]         [,3]
##  [1,]  0.6863783  0.42880739  0.991485926
##  [2,]  0.8780215 -0.78803232  0.281713341
##  [3,]  0.3271486 -0.28361687  0.505415506
##  [4,]  0.5148918  1.20682214  0.941726948
##  [5,] -0.2068897 -0.50803660  1.386622270
##  [6,] -0.5123980  0.01742308  0.676246890
##  [7,]  0.1567913  0.95758597  1.448304161
##  [8,] -0.7545684 -0.04667938  0.750903476
##  [9,] -0.6960884  0.55324138  1.872625082
## [10,]  1.5255043 -1.08640381 -0.776377906
## [11,]  2.5726569 -2.19714999  1.566431347
## [12,] -0.1607226  0.13780976 -1.381366462
## [13,] -1.1089838 -0.56091041 -0.812725032
## [14,]  0.9684989  0.17026497  0.050935772
## [15,] -0.9461587  0.09307942 -0.645186846
## [16,]  0.1603198 -0.52506548 -1.189387361
## [17,]  0.2849290 -2.13096333 -2.441041498
## [18,] -1.5255099 -1.55412835  1.140054329
## [19,] -1.1304586  0.82836114 -0.527140045
## [20,] -1.4526684 -0.16713511  0.009863574
## [21,] -0.1165020  1.31237565 -0.008170449
## [22,] -0.4553111  0.15764108 -0.224225784
## [23,]  1.1724386  0.24674407 -1.058680582
## [24,]  0.6755048  1.45999694 -0.452883431
## [25,]  0.9393964  0.49036949  0.918375945
## [26,]  0.1757576  1.80227601 -0.342789182
## [27,] -0.3694155 -1.88356570  0.844722127
## [28,]  0.4258345  0.65979587 -0.353986861
## [29,] -0.9373584  0.23775841  0.506474049
## [30,] -0.8483556  0.92884569 -0.765735896
## [31,]  1.4003034  0.35993048 -1.305009463
## [32,]  0.3779029  0.77887401 -1.099799905
## [33,]  0.2381839  0.22047924  0.055995099
## [34,] -2.2590735 -1.31679483 -0.563389139
## 
## $scores$corr.X.xscores
##          [,1]      [,2]       [,3]
## X1 -0.8177336 0.3678225 -0.4427395
## X2 -0.8497526 0.2949835 -0.4369272
## X3 -0.8579913 0.3249537 -0.3978140
## 
## $scores$corr.Y.xscores
##         [,1]         [,2]          [,3]
## Y1 0.4807295 -0.005516707  0.0001049456
## Y2 0.4795781 -0.014058273 -0.0007954096
## Y3 0.4791014 -0.018933146 -0.0002376941
## 
## $scores$corr.X.yscores
##          [,1]       [,2]         [,3]
## X1 -0.3932349 0.08165630 -0.009366848
## X2 -0.4086323 0.06548609 -0.009243878
## X3 -0.4125942 0.07213945 -0.008416378
## 
## $scores$corr.Y.yscores
##         [,1]        [,2]         [,3]
## Y1 0.9996789 -0.02485012  0.004960429
## Y2 0.9972845 -0.06332578 -0.037596350
## Y3 0.9962933 -0.08528475 -0.011235004
plot(hasil$cor,type="b")

hasil$cor
## [1] 0.48088390 0.22199921 0.02115657
hasil$xcoef
##          [,1]      [,2]      [,3]
## X1  1.2644667  1.890923 -1.182560
## X2  0.1297381 -2.883217 -2.634965
## X3 -1.6110390  1.012772  3.816965
hasil$ycoef
##          [,1]       [,2]      [,3]
## Y1  1.3960219  14.689415  12.28876
## Y2  0.3931482   9.054677 -33.87048
## Y3 -0.7044044 -23.998129  21.73638
hasil$scores
## $xscores
##             [,1]        [,2]        [,3]
##  [1,] -0.6794862  0.37682535 -0.34150444
##  [2,]  0.6716592 -0.02066714 -0.52511976
##  [3,]  0.8821690  0.45887824  0.47946349
##  [4,]  0.4372740 -0.35481185  0.42032348
##  [5,]  0.3228516 -0.03102330  0.49513775
##  [6,]  0.1179836  0.86433812 -0.36842147
##  [7,] -0.1606463 -0.18874855 -1.72367907
##  [8,]  0.8763834  0.72123899 -1.79608622
##  [9,]  0.8077125 -0.02235471  0.94043162
## [10,]  0.6976774 -1.69144257 -0.75303025
## [11,]  0.7401287 -1.13571460  0.21554378
## [12,]  0.7074795 -0.67294157 -0.87816190
## [13,]  0.2915970  0.65268891 -0.42595161
## [14,]  1.1869204  1.65385084 -1.67009275
## [15,]  0.4037354  1.07563363 -0.11864763
## [16,]  0.5968116  0.04644390  0.65132739
## [17,]  0.7904089 -1.34143074  0.03120273
## [18,] -1.3421267  0.28685147  1.30512982
## [19,] -1.6975466  1.06162386 -0.25847995
## [20,]  0.4203899 -0.12354016  0.63110384
## [21,]  0.2936434 -1.32624872  0.69795439
## [22,]  1.0949283 -0.50298564  0.03991036
## [23,]  0.5611987 -0.67371567  0.16897204
## [24,]  1.1099876 -0.10509585 -0.71504330
## [25,]  0.1963191  0.14162050  1.00605216
## [26,] -0.6388690  0.56514933  0.71402185
## [27,] -0.4779946 -0.50403003  1.29285721
## [28,] -0.9049323  0.38332679  1.61458649
## [29,] -1.3519671 -0.63368441 -0.05601851
## [30,] -1.3941992 -1.92781393  0.75287220
## [31,] -0.4689360  3.34025330  0.66970951
## [32,]  0.4295193  0.57323503  1.30181075
## [33,] -1.3105358 -0.48483942 -2.61341990
## [34,] -3.2095389 -0.46086942 -1.18475411
## 
## $yscores
##             [,1]        [,2]         [,3]
##  [1,]  0.6863783  0.42880739  0.991485926
##  [2,]  0.8780215 -0.78803232  0.281713341
##  [3,]  0.3271486 -0.28361687  0.505415506
##  [4,]  0.5148918  1.20682214  0.941726948
##  [5,] -0.2068897 -0.50803660  1.386622270
##  [6,] -0.5123980  0.01742308  0.676246890
##  [7,]  0.1567913  0.95758597  1.448304161
##  [8,] -0.7545684 -0.04667938  0.750903476
##  [9,] -0.6960884  0.55324138  1.872625082
## [10,]  1.5255043 -1.08640381 -0.776377906
## [11,]  2.5726569 -2.19714999  1.566431347
## [12,] -0.1607226  0.13780976 -1.381366462
## [13,] -1.1089838 -0.56091041 -0.812725032
## [14,]  0.9684989  0.17026497  0.050935772
## [15,] -0.9461587  0.09307942 -0.645186846
## [16,]  0.1603198 -0.52506548 -1.189387361
## [17,]  0.2849290 -2.13096333 -2.441041498
## [18,] -1.5255099 -1.55412835  1.140054329
## [19,] -1.1304586  0.82836114 -0.527140045
## [20,] -1.4526684 -0.16713511  0.009863574
## [21,] -0.1165020  1.31237565 -0.008170449
## [22,] -0.4553111  0.15764108 -0.224225784
## [23,]  1.1724386  0.24674407 -1.058680582
## [24,]  0.6755048  1.45999694 -0.452883431
## [25,]  0.9393964  0.49036949  0.918375945
## [26,]  0.1757576  1.80227601 -0.342789182
## [27,] -0.3694155 -1.88356570  0.844722127
## [28,]  0.4258345  0.65979587 -0.353986861
## [29,] -0.9373584  0.23775841  0.506474049
## [30,] -0.8483556  0.92884569 -0.765735896
## [31,]  1.4003034  0.35993048 -1.305009463
## [32,]  0.3779029  0.77887401 -1.099799905
## [33,]  0.2381839  0.22047924  0.055995099
## [34,] -2.2590735 -1.31679483 -0.563389139
## 
## $corr.X.xscores
##          [,1]      [,2]       [,3]
## X1 -0.8177336 0.3678225 -0.4427395
## X2 -0.8497526 0.2949835 -0.4369272
## X3 -0.8579913 0.3249537 -0.3978140
## 
## $corr.Y.xscores
##         [,1]         [,2]          [,3]
## Y1 0.4807295 -0.005516707  0.0001049456
## Y2 0.4795781 -0.014058273 -0.0007954096
## Y3 0.4791014 -0.018933146 -0.0002376941
## 
## $corr.X.yscores
##          [,1]       [,2]         [,3]
## X1 -0.3932349 0.08165630 -0.009366848
## X2 -0.4086323 0.06548609 -0.009243878
## X3 -0.4125942 0.07213945 -0.008416378
## 
## $corr.Y.yscores
##         [,1]        [,2]         [,3]
## Y1 0.9996789 -0.02485012  0.004960429
## Y2 0.9972845 -0.06332578 -0.037596350
## Y3 0.9962933 -0.08528475 -0.011235004
X_canonical <- as.matrix(X) %*% hasil$xcoef  # Variabel kanonik untuk X
Y_canonical <- as.matrix(Y) %*% hasil$ycoef

library(zoo)
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(lmtest)
## 
## Attaching package: 'lmtest'
## The following object is masked from 'package:RCurl':
## 
##     reset
# Konversi ke data frame agar mudah diakses
X_canonical <- as.data.frame(X_canonical)
Y_canonical <- as.data.frame(Y_canonical)

# Memberi nama kolom untuk identifikasi yang mudah
colnames(X_canonical) <- paste0("XCan", 1:ncol(X_canonical))
colnames(Y_canonical) <- paste0("YCan", 1:ncol(Y_canonical))

# Model regresi linier antara variabel kanonik pertama dari X dan Y
model <- lm(Y_canonical$YCan1 ~ X_canonical$XCan1)
model2 <- lm(Y_canonical$YCan2 ~ X_canonical$XCan2)

# Uji RESET (Regression Equation Specification Error Test) untuk memeriksa adanya bentuk non-linear
# Uji RESET untuk memeriksa non-linearitas (Linear jika > 0.05)
reset_result <- resettest(model, power = 2:3)
print(reset_result)
## 
##  RESET test
## 
## data:  model
## RESET = 0.7677, df1 = 2, df2 = 30, p-value = 0.473
reset_result2 <- resettest(model2, power = 2:3)
print(reset_result2)
## 
##  RESET test
## 
## data:  model2
## RESET = 0.28427, df1 = 2, df2 = 30, p-value = 0.7546