pacman::p_load(pacman,dplyr,GGally,ggplot2,ggthemes,ggvis,httr,lubridate,plotly,rio,rmarkdown,shiny,stringr,tidyr,tidyverse,lattice,caret,pls,MASS,yarrr,psych,ggcorrplot,GGally,CCA,CCP)
library(tidyverse)
theme_set(theme_bw(16))
library(CCA)
library(stats)
Y=read.csv("sensorialCSV.csv",sep=",")
Y<-Y[,c('Aroma', 'Flavor', 'Aftertaste', 'Acidity', 'Body', 'Balance')]
X=read.csv("apolarCSV.csv",sep=",")
X <- X[, c('A25', 'A58', 'A1', 'A15', 'A40', 'A72', 'A34', 'A14', 'A69', 'A13', 'A46')]
X
##          A25       A58        A1       A15       A40        A72       A34
## 1  19.370000 0.4533333 16.543333  8.736667 0.6000000 0.03666667 0.0000000
## 2  21.316667 0.9800000  0.000000 12.213333 0.8233333 0.18000000 0.1366667
## 3  11.443333 0.9733333  9.526667 18.383333 0.6600000 0.11000000 0.1833333
## 4  14.000000 0.9266667 20.610000  9.060000 0.5700000 0.12666667 0.1600000
## 5  17.210000 0.9566667  6.876667 19.186667 1.3966667 0.06333333 0.0000000
## 6  15.210000 0.7233333 16.190000 10.170000 0.8133333 0.13666667 0.1466667
## 7  12.236667 1.0566667 15.453333 15.730000 0.7833333 0.12666667 0.1133333
## 8  16.996667 0.8966667 13.613333 10.906667 0.9233333 0.08666667 0.1133333
## 9  11.596667 1.1066667 20.816667 13.970000 0.7333333 0.07000000 0.1666667
## 10 11.936667 1.2533333 16.423333 14.733333 0.8433333 0.06000000 0.3533333
## 11 13.473333 1.2900000  8.676667 14.356667 0.9666667 0.13666667 0.3033333
## 12 17.526667 0.7233333 22.580000  8.370000 0.6866667 0.08666667 0.1633333
## 13 16.943333 1.0633333 15.406667 17.133333 0.8366667 0.09000000 0.0000000
## 14 14.723333 0.9666667 21.490000 14.553333 0.8400000 0.00000000 0.0000000
## 15 11.193333 0.6433333 16.930000 11.800000 2.2233333 0.13666667 0.1633333
## 16  9.086667 0.6533333 21.066667 17.583333 1.9866667 0.10333333 0.1433333
##         A14        A69      A13       A46
## 1  20.27000 0.00000000 2.110000 0.5333333
## 2  16.81000 0.03666667 2.516667 0.4600000
## 3  17.37000 0.03000000 1.780000 0.4600000
## 4  15.24667 0.00000000 1.863333 0.3233333
## 5  15.71000 0.02000000 2.386667 0.6000000
## 6  16.07667 0.02666667 1.550000 0.4766667
## 7  14.86667 0.03333333 2.003333 0.3833333
## 8  16.26000 0.03000000 2.736667 0.5100000
## 9  15.46333 0.02000000 2.223333 0.2900000
## 10 14.00333 0.01333333 1.866667 0.2833333
## 11 16.26667 0.00000000 2.243333 0.3500000
## 12 14.89000 0.01333333 2.003333 0.3700000
## 13 14.94000 0.02333333 1.883333 0.4900000
## 14 11.72667 0.02000000 1.983333 0.4700000
## 15 12.80333 0.01666667 3.350000 0.2666667
## 16 13.28000 0.02000000 2.830000 0.2333333
Y
##    Aroma Flavor Aftertaste Acidity Body Balance
## 1   8.50   8.42       8.47    8.42 8.36    8.75
## 2   8.39   8.36       8.31    8.44 8.39    8.58
## 3   8.31   8.44       8.33    8.42 8.36    8.64
## 4   8.36   8.25       8.17    8.28 8.31    8.39
## 5   8.17   8.08       8.14    8.33 8.33    8.44
## 6   8.39   8.36       8.25    8.33 8.33    8.58
## 7   8.31   8.39       8.31    8.42 8.36    8.67
## 8   8.50   8.44       8.33    8.61 8.56    8.58
## 9   8.44   8.39       8.31    8.47 8.39    8.67
## 10  8.42   8.39       8.28    8.33 8.47    8.50
## 11  8.47   8.14       8.31    8.42 8.19    8.50
## 12  8.53   8.31       8.25    8.47 8.28    8.61
## 13  8.42   8.47       8.39    8.53 8.42    8.64
## 14  8.56   8.47       8.42    8.53 8.33    8.64
## 15  8.16   8.13       8.03    8.16 8.22    8.22
## 16  8.07   7.89       7.89    8.11 8.00    8.25
#Correlatsion matrices
mat_cor  <-matcor(X,Y)
mat_cor
## $Xcor
##             A25         A58          A1         A15          A40         A72
## A25  1.00000000 -0.18135639 -0.43085120 -0.40275513 -0.419975534 -0.01825580
## A58 -0.18135639  1.00000000 -0.30172536  0.44880878 -0.310536587  0.06820285
## A1  -0.43085120 -0.30172536  1.00000000 -0.29210867  0.031280286 -0.46898725
## A15 -0.40275513  0.44880878 -0.29210867  1.00000000  0.294046156 -0.10936872
## A40 -0.41997553 -0.31053659  0.03128029  0.29404616  1.000000000  0.16200851
## A72 -0.01825580  0.06820285 -0.46898725 -0.10936872  0.162008508  1.00000000
## A34 -0.46732026  0.44853553 -0.04290717 -0.07364446  0.008578171  0.39303208
## A14  0.51064973 -0.19008092 -0.44103729 -0.25507348 -0.492925389  0.12640453
## A69  0.05861037  0.10287365 -0.35893143  0.36259933  0.050740697  0.26426427
## A13 -0.12449413 -0.29310138 -0.12170794  0.03028465  0.809255779  0.19689497
## A46  0.72436738 -0.11557843 -0.46283931  0.05901247 -0.364399100 -0.24739540
##              A34        A14         A69         A13         A46
## A25 -0.467320263  0.5106497  0.05861037 -0.12449413  0.72436738
## A58  0.448535526 -0.1900809  0.10287365 -0.29310138 -0.11557843
## A1  -0.042907173 -0.4410373 -0.35893143 -0.12170794 -0.46283931
## A15 -0.073644460 -0.2550735  0.36259933  0.03028465  0.05901247
## A40  0.008578171 -0.4929254  0.05074070  0.80925578 -0.36439910
## A72  0.393032081  0.1264045  0.26426427  0.19689497 -0.24739540
## A34  1.000000000 -0.1021803 -0.18656346 -0.02347316 -0.66409496
## A14 -0.102180264  1.0000000 -0.12548042 -0.24400168  0.49992626
## A69 -0.186563462 -0.1254804  1.00000000  0.07430970  0.24983090
## A13 -0.023473162 -0.2440017  0.07430970  1.00000000 -0.27349364
## A46 -0.664094959  0.4999263  0.24983090 -0.27349364  1.00000000
## 
## $Ycor
##                Aroma    Flavor Aftertaste   Acidity      Body   Balance
## Aroma      1.0000000 0.7460524  0.8403546 0.8118285 0.5585906 0.7369988
## Flavor     0.7460524 1.0000000  0.8804859 0.7854296 0.8221883 0.8334571
## Aftertaste 0.8403546 0.8804859  1.0000000 0.8529577 0.6921323 0.9119873
## Acidity    0.8118285 0.7854296  0.8529577 1.0000000 0.7165935 0.8201281
## Body       0.5585906 0.8221883  0.6921323 0.7165935 1.0000000 0.6099369
## Balance    0.7369988 0.8334571  0.9119873 0.8201281 0.6099369 1.0000000
## 
## $XYcor
##                    A25         A58           A1         A15          A40
## A25         1.00000000 -0.18135639 -0.430851202 -0.40275513 -0.419975534
## A58        -0.18135639  1.00000000 -0.301725356  0.44880878 -0.310536587
## A1         -0.43085120 -0.30172536  1.000000000 -0.29210867  0.031280286
## A15        -0.40275513  0.44880878 -0.292108670  1.00000000  0.294046156
## A40        -0.41997553 -0.31053659  0.031280286  0.29404616  1.000000000
## A72        -0.01825580  0.06820285 -0.468987253 -0.10936872  0.162008508
## A34        -0.46732026  0.44853553 -0.042907173 -0.07364446  0.008578171
## A14         0.51064973 -0.19008092 -0.441037291 -0.25507348 -0.492925389
## A69         0.05861037  0.10287365 -0.358931425  0.36259933  0.050740697
## A13        -0.12449413 -0.29310138 -0.121707941  0.03028465  0.809255779
## A46         0.72436738 -0.11557843 -0.462839308  0.05901247 -0.364399100
## Aroma       0.47987188  0.21756819  0.129362597 -0.49806772 -0.771264360
## Flavor      0.36894284  0.20904091  0.001415696 -0.22626186 -0.763870859
## Aftertaste  0.50997520  0.27310672 -0.155076544 -0.17934135 -0.807842942
## Acidity     0.52628653  0.34199917 -0.142644893 -0.10585283 -0.696231144
## Body        0.44478432  0.32190921 -0.220988604 -0.15348048 -0.601675397
## Balance     0.46002302  0.15847236 -0.072210914 -0.12179748 -0.829185297
##                    A72          A34        A14         A69         A13
## A25        -0.01825580 -0.467320263  0.5106497  0.05861037 -0.12449413
## A58         0.06820285  0.448535526 -0.1900809  0.10287365 -0.29310138
## A1         -0.46898725 -0.042907173 -0.4410373 -0.35893143 -0.12170794
## A15        -0.10936872 -0.073644460 -0.2550735  0.36259933  0.03028465
## A40         0.16200851  0.008578171 -0.4929254  0.05074070  0.80925578
## A72         1.00000000  0.393032081  0.1264045  0.26426427  0.19689497
## A34         0.39303208  1.000000000 -0.1021803 -0.18656346 -0.02347316
## A14         0.12640453 -0.102180264  1.0000000 -0.12548042 -0.24400168
## A69         0.26426427 -0.186563462 -0.1254804  1.00000000  0.07430970
## A13         0.19689497 -0.023473162 -0.2440017  0.07430970  1.00000000
## A46        -0.24739540 -0.664094959  0.4999263  0.24983090 -0.27349364
## Aroma      -0.35177414 -0.005735030  0.2486802 -0.18417950 -0.48798410
## Flavor     -0.26931620 -0.170462632  0.2714448  0.27109350 -0.55396964
## Aftertaste -0.34440454 -0.216665962  0.4588940  0.02369976 -0.55937954
## Acidity    -0.30817677 -0.263953482  0.2786702  0.23971656 -0.36226707
## Body       -0.20771172 -0.100360715  0.3043094  0.30963048 -0.31966161
## Balance    -0.32986242 -0.281594235  0.5104018  0.18778275 -0.62320807
##                    A46       Aroma       Flavor  Aftertaste    Acidity
## A25         0.72436738  0.47987188  0.368942837  0.50997520  0.5262865
## A58        -0.11557843  0.21756819  0.209040910  0.27310672  0.3419992
## A1         -0.46283931  0.12936260  0.001415696 -0.15507654 -0.1426449
## A15         0.05901247 -0.49806772 -0.226261857 -0.17934135 -0.1058528
## A40        -0.36439910 -0.77126436 -0.763870859 -0.80784294 -0.6962311
## A72        -0.24739540 -0.35177414 -0.269316197 -0.34440454 -0.3081768
## A34        -0.66409496 -0.00573503 -0.170462632 -0.21666596 -0.2639535
## A14         0.49992626  0.24868015  0.271444773  0.45889397  0.2786702
## A69         0.24983090 -0.18417950  0.271093504  0.02369976  0.2397166
## A13        -0.27349364 -0.48798410 -0.553969642 -0.55937954 -0.3622671
## A46         1.00000000  0.28189707  0.410396473  0.54487713  0.5515343
## Aroma       0.28189707  1.00000000  0.746052422  0.84035457  0.8118285
## Flavor      0.41039647  0.74605242  1.000000000  0.88048587  0.7854296
## Aftertaste  0.54487713  0.84035457  0.880485874  1.00000000  0.8529577
## Acidity     0.55153429  0.81182848  0.785429604  0.85295767  1.0000000
## Body        0.48735985  0.55859059  0.822188317  0.69213230  0.7165935
## Balance     0.53277374  0.73699879  0.833457108  0.91198732  0.8201281
##                  Body     Balance
## A25         0.4447843  0.46002302
## A58         0.3219092  0.15847236
## A1         -0.2209886 -0.07221091
## A15        -0.1534805 -0.12179748
## A40        -0.6016754 -0.82918530
## A72        -0.2077117 -0.32986242
## A34        -0.1003607 -0.28159424
## A14         0.3043094  0.51040177
## A69         0.3096305  0.18778275
## A13        -0.3196616 -0.62320807
## A46         0.4873598  0.53277374
## Aroma       0.5585906  0.73699879
## Flavor      0.8221883  0.83345711
## Aftertaste  0.6921323  0.91198732
## Acidity     0.7165935  0.82012809
## Body        1.0000000  0.60993686
## Balance     0.6099369  1.00000000
cc1 <- cc(X, Y)

# display the canonical correlations
cc1$cor
## [1] 1.0000000 1.0000000 0.9807809 0.9459939 0.6022575 0.5244332
# canonical vectors
cc1[3:4]
## $xcoef
##             [,1]         [,2]          [,3]        [,4]        [,5]
## A25  -0.12122232   0.08741659  -0.058323720   0.1926928  -0.5063744
## A58  -3.45338335   1.98220520   1.887393915  -2.2602260  -3.3661593
## A1   -0.06756581   0.17751323  -0.168415509   0.2544482  -0.3538928
## A15   0.21557891   0.27861429  -0.211460693   0.2766940  -0.1911074
## A40  -0.64723073  -1.70292936   1.832342233  -2.9295898  -4.2835399
## A72  12.10401693  14.83618510  -9.290449412  22.1816098  -2.8298197
## A34  -2.82643700   4.35542228  -2.203682375  13.9260880 -10.8251705
## A14  -0.25156186  -0.03710752  -0.008916911  -0.6200673  -0.8858299
## A69 -36.10718354 -19.48785279 -68.671750582 -35.5423510 -55.8575382
## A13   0.28488329   3.21575180  -0.903230736   3.4776590   0.6546994
## A46  -1.15607403  13.59315413  -1.428673052  14.1454821  -0.8955144
##             [,6]
## A25    0.3727698
## A58   -6.4688128
## A1     0.2440669
## A15    0.7634864
## A40   -5.3802716
## A72   26.0556494
## A34    8.6533718
## A14   -0.3855186
## A69 -100.1378574
## A13    3.2822327
## A46    4.8103434
## 
## $ycoef
##                 [,1]      [,2]       [,3]       [,4]       [,5]      [,6]
## Aroma      -8.601644 -8.147004   3.676741   6.488945  -4.837793  2.297379
## Flavor      3.575096 -5.506885 -10.289474   4.553118  11.062029 -3.117002
## Aftertaste  3.463244  6.928188  15.671556 -10.516516  11.565301 -1.141995
## Acidity     3.744878 13.017124  -6.495353   7.456519   4.384930  5.104057
## Body       -6.942830  2.210205   4.603664  -2.476606 -11.009861 -8.349326
## Balance    -4.232448 -5.103435  -7.039717  -6.756198 -13.216140  4.625235
# Standarized coefficients of X
std_coef_x<-cc1$xcoef*apply(X,2,sd)
std_coef_x
##           [,1]        [,2]        [,3]       [,4]        [,5]       [,6]
## A25 -0.4082558  0.29440394 -0.19642419  0.6489560 -1.70538143  1.2554242
## A58 -0.7850883  0.45063230  0.42907800 -0.5138372 -0.76525886 -1.4706126
## A1  -0.4180194  1.09824725 -1.04196104  1.5742321 -2.18948098  1.5100045
## A15  0.7584143  0.98017505 -0.74392629  0.9734195 -0.67232283  2.6859725
## A40 -0.3103048 -0.81644336  0.87848837 -1.4045469 -2.05367749 -2.5794887
## A72  0.5424034  0.66483689 -0.41632222  0.9939990 -0.12680945  1.1676018
## A34 -0.2878816  0.44361369 -0.22445210  1.4184166 -1.10257824  0.8813736
## A14 -0.4992707 -0.07364669 -0.01769725 -1.2306374 -1.75809210 -0.7651324
## A69 -0.4162779 -0.22467446 -0.79171312 -0.4097660 -0.64397871 -1.1544843
## A13  0.1315806  1.48527650 -0.41718002  1.6062450  0.30238953  1.5159824
## A46 -0.1252809  1.47305635 -0.15482175  1.5329108 -0.09704467  0.5212850
# Standarized coefficients of Y
std_coef_y<-cc1$ycoef*apply(Y,2,sd)
std_coef_y
##                  [,1]       [,2]       [,3]       [,4]       [,5]       [,6]
## Aroma      -1.2107660 -1.1467710  0.5175374  0.9133829 -0.6809669  0.3233788
## Flavor      0.5949742 -0.9164662 -1.7123938  0.7577386  1.8409640 -0.5187373
## Aftertaste  0.5052411  1.0107302  2.2862709 -1.5342193  1.6872231 -0.1666018
## Acidity     0.4936460  1.7159044 -0.8562110  0.9829109  0.5780171  0.6728117
## Body       -0.8671129  0.2760398  0.5749668 -0.3093114 -1.3750577 -1.0427748
## Balance    -0.6370033 -0.7680910 -1.0595105 -1.0168396 -1.9890913  0.6961197
# Calculate structure correlations
struc_cor_x <- cor(X, cc1$scores$xscores)
struc_cor_y <- cor(Y, cc1$scores$yscores)
# Compute redundancy indices
can_cor <- cc1$cor
redundancy_x <- colMeans(struc_cor_x^2) * can_cor^2
redundancy_y <- colMeans(struc_cor_y^2) * can_cor^2
print(redundancy_x)
## [1] 0.16316268 0.12240024 0.07163330 0.09863615 0.01182328 0.01584773
print(redundancy_y)
## [1] 0.57526829 0.06957837 0.06960760 0.07370398 0.04340058 0.02221592
# Total redundancy
total_redundancy_x <- sum(redundancy_x)
total_redundancy_y <- sum(redundancy_y)


# Print results
print(paste("Total redundancy for X:", total_redundancy_x))
## [1] "Total redundancy for X: 0.483503383507049"
print(paste("Total redundancy for Y:", total_redundancy_y))
## [1] "Total redundancy for Y: 0.853774748337202"
# compute canonical loadings
cc2 <- comput(X, Y, cc1)

# display canonical loadings
cc2[3:6]
## $corr.X.xscores
##            [,1]        [,2]        [,3]        [,4]         [,5]        [,6]
## A25 -0.52275581  0.29951651  0.10220669 -0.16131032 -0.015863223  0.17036707
## A58 -0.21231799  0.38893380  0.10542190  0.01433344  0.227965027 -0.15033843
## A1  -0.06693200 -0.55668935 -0.22272047  0.37920513  0.029756157  0.28818831
## A15  0.53622935  0.46681675 -0.15182832 -0.30015735  0.020198894 -0.01863829
## A40  0.77740430  0.04415157  0.19438559  0.31824151 -0.281996221 -0.26086620
## A72  0.32977538 -0.03065069 -0.01463453  0.10546667 -0.122425262 -0.26132148
## A34 -0.06784469 -0.32052330  0.26532105  0.27055655 -0.215730367 -0.27895449
## A14 -0.35917252  0.10000667  0.11075679 -0.64528405 -0.278898871  0.16898996
## A69  0.12649833  0.33928451 -0.74586794 -0.05313587  0.006249432 -0.44811420
## A13  0.47395200  0.27074424  0.20784327  0.39031097 -0.268258760 -0.23163145
## A46 -0.31155370  0.52302995 -0.06894990 -0.44174722  0.119225648  0.04050190
## 
## $corr.Y.xscores
##                  [,1]          [,2]         [,3]        [,4]       [,5]
## Aroma      -0.8953783 -9.958044e-16  0.006371812  0.06165458 0.20811181
## Flavor     -0.7195839  5.242400e-02 -0.388461399 -0.22844380 0.29156255
## Aftertaste -0.7484019  1.942610e-01 -0.083534593 -0.38078488 0.27895869
## Acidity    -0.7348205  4.950887e-01 -0.282316535 -0.04223105 0.17682230
## Body       -0.7393491  3.426343e-01 -0.216987643 -0.14553521 0.07645323
## Balance    -0.6967086  1.203062e-01 -0.364607290 -0.46719904 0.13097724
##                   [,6]
## Aroma       0.14323943
## Flavor     -0.09067209
## Aftertaste  0.07101052
## Acidity     0.10985072
## Body       -0.26076240
## Balance     0.13946276
## 
## $corr.X.yscores
##            [,1]        [,2]        [,3]        [,4]         [,5]         [,6]
## A25 -0.52275581  0.29951651  0.10024237 -0.15259858 -0.009553745  0.089346148
## A58 -0.21231799  0.38893380  0.10339579  0.01355935  0.137293641 -0.078842460
## A1  -0.06693200 -0.55668935 -0.21843999  0.35872574  0.017920868  0.151135513
## A15  0.53622935  0.46681675 -0.14891031 -0.28394702  0.012164935 -0.009774538
## A40  0.77740430  0.04415157  0.19064967  0.30105453 -0.169834332 -0.136806895
## A72  0.32977538 -0.03065069 -0.01435327  0.09977082 -0.073731529 -0.137045657
## A34 -0.06784469 -0.32052330  0.26022182  0.25594485 -0.129925226 -0.146292993
## A14 -0.35917252  0.10000667  0.10862815 -0.61043477 -0.167968930  0.088623945
## A69  0.12649833  0.33928451 -0.73153304 -0.05026621  0.003763767 -0.235005958
## A13  0.47395200  0.27074424  0.20384871  0.36923180 -0.161560843 -0.121475222
## A46 -0.31155370  0.52302995 -0.06762474 -0.41789018  0.071804538  0.021240542
## 
## $corr.Y.yscores
##                  [,1]          [,2]         [,3]        [,4]      [,5]
## Aroma      -0.8953783 -9.404684e-16  0.006496673  0.06517440 0.3455529
## Flavor     -0.7195839  5.242400e-02 -0.396073573 -0.24148549 0.4841161
## Aftertaste -0.7484019  1.942610e-01 -0.085171512 -0.40252361 0.4631884
## Acidity    -0.7348205  4.950887e-01 -0.287848726 -0.04464199 0.2935992
## Body       -0.7393491  3.426343e-01 -0.221239668 -0.15384371 0.1269444
## Balance    -0.6967086  1.203062e-01 -0.371752026 -0.49387109 0.2174771
##                  [,6]
## Aroma       0.2731319
## Flavor     -0.1728954
## Aftertaste  0.1354043
## Acidity     0.2094656
## Body       -0.4972271
## Balance     0.2659305
# tests of canonical dimensions
rho <- cc1$cor
## Define number of observations, number of variables in first set, and number of variables in the second set.
n <- dim(X)[1]
p <- length(X)
q <- length(Y)

## Calculate p-values using the F-approximations of different test statistics:
p.asym(rho, n, p, q, tstat = "Wilks")
## Wilks' Lambda, using F-approximation (Rao's F):
##                  stat      approx df1       df2      p.value
## 1 to 6:  0.000000e+00         Inf  66 0.1050906 0.000000e+00
## 2 to 6:  8.208768e-19 621.4740283  50 3.3642102 3.216798e-05
## 3 to 6:  1.848450e-03   0.6646059  36 5.4847775 7.912900e-01
## 4 to 6:  4.855551e-02   0.4902057  24 6.4018255 9.040948e-01
## 5 to 6:  4.620131e-01   0.2019445  14 6.0000000 9.935366e-01
## 6 to 6:  7.249698e-01   0.2529118   6 4.0000000 9.339501e-01
p.asym(rho, n, p, q, tstat = "Hotelling")
## Warning in pf(approx[rhostart], df1[rhostart], df2[rhostart]): Se han producido
## NaNs
## Warning in pf(approx[rhostart], df1[rhostart], df2[rhostart]): Se han producido
## NaNs
##  Hotelling-Lawley Trace, using F-approximation:
##                  stat        approx df1 df2   p.value
## 1 to 6:           Inf          -Inf  66 -16       NaN
## 2 to 6:  2.251800e+15 -3.002400e+13  50  -4       NaN
## 3 to 6:  3.473190e+01  1.286367e+00  36   8 0.3749573
## 4 to 6:  9.463675e+00  1.314399e+00  24  20 0.2693943
## 5 to 6:  9.485221e-01  3.613418e-01  14  32 0.9768808
## 6 to 6:  3.793677e-01  4.636717e-01   6  44 0.8313388
# Canonical variates (linear combinations of X and Y)
X_canon <- as.matrix(X) %*% cc1$xcoef
Y_canon <- as.matrix(Y) %*% cc1$ycoef

# Plot the first canonical variates
plot(X_canon[,1], Y_canon[,1], 
     xlab = "Canonical Variate 1 (X)", 
     ylab = "Canonical Variate 1 (Y)", 
     main = "Scatter plot of first pair of canonical variates")

plot(X_canon[,2], Y_canon[,2], 
     xlab = "Canonical Variate 2 (X)", 
     ylab = "Canonical Variate 2 (Y)", 
     main = "Scatter plot of the second pair of canonical variates")

plot(X_canon[,3], Y_canon[,3], 
     xlab = "Canonical Variate 3 (X)", 
     ylab = "Canonical Variate  (Y)", 
     main = "Scatter plot of the third pair of canonical variates")

# Create a data frame of canonical correlations
canon_corr_df <- data.frame(Variable = 1:length(cc1$cor), 
                            Correlation = cc1$cor)

# Bar plot of canonical correlations
ggplot(canon_corr_df, aes(x = as.factor(Variable), y = Correlation)) +
  geom_bar(stat = "identity") +
  xlab("Canonical Pair") +
  ylab("Canonical Correlation") +
  ggtitle("Canonical Correlations for Each Canonical Pair")

plt.cc(cc1, var.label = TRUE, d1 = 1, d2 = 2, type = "b")

plt.cc(cc1,d1=1,d2=2, type = "b")