X <- data.frame( Sistolik = c(120, 109, 130, 121, 135, 140), Diastolik = c(76, 80, 82, 78, 85, 87) ) Y <- data.frame( Tinggi = c(165, 180, 170, 185, 180, 187), Berat = c(60, 80, 70, 85, 90, 87) )

Compute Canonical Correlations

cc <- cancor(X,Y) str(cc) print(cc)

Interpretation The Result Of A CCA

print(cc$cor)

Get the canonical covariate pairs

CC1_X <- as.matrix(X) %*% cc\(xcoef[, 1] CC1_Y <- as.matrix(Y) %*% cc\)ycoef[, 1]

CC2_X <- as.matrix(X) %*% cc\(xcoef[, 2] CC2_Y <- as.matrix(Y) %*% cc\)ycoef[, 2]

Create a dataframe Canonincal Covariates

cca_df <- data.frame( Sistolik = X\(Sistolik, Diastolik = X\)Diastolik, Tinggi = Y\(Tinggi, Berat = Y\)Berat, CC1_X = CC1_X, CC1_Y = CC1_Y, CC2_X = CC2_X, CC2_Y = CC2_Y )

library(dplyr) # Memuat package dplyr # Melihat struktur data glimpse(cca_df)

library(ggplot2)

Scatter plot between the first pair of canonical covariate

#| fig.width: 5.5 #| fig.height: 4 cca_df %>% ggplot(aes(x=CC1_X,y=CC1_Y, color=Tinggi))+ geom_point()

#| fig.width: 8 #| fig.height: 5

First Canonical Variate of X vs Latent Variable

p1<-cca_df %>% ggplot(aes(x=Tinggi,y=CC1_X, color=Tinggi))+ geom_boxplot(width=0.5)+ geom_jitter(width=0.15)+ theme(legend.position=“none”)+ ggtitle(“First Canonical Variate of X vs Tinggi”)

First Canonical Variate of Y vs Latent Variable

p2<-cca_df %>% ggplot(aes(x=Tinggi,y=CC1_Y, color=Tinggi))+ geom_boxplot(width=0.5)+ geom_jitter(width=0.15)+ theme(legend.position=“none”)+ ggtitle(“First Canonical Variate of Y vs Tinggi”)

install.packages(“patchwork”) # Install the patchwork package library(patchwork) # Load the patchwork package p1+p2 # Combine the plots