#Canonical Correlation Analysis (CCA)
CCA_raw <- read.csv("C:/Users/Tingyen/OneDrive - 國立陽明交通大學/桌面/多變量/L3_CCA/HW3_data.csv")

library(plyr)
CCA_raw$PC_Ability<-mapvalues(CCA_raw$PC_Ability,from=c("bad","good","excellent"),to=c(1,2,3))
CCA_raw$PC_Ability<-as.numeric(CCA_raw$PC_Ability)
str(CCA_raw)
## 'data.frame':    15 obs. of  10 variables:
##  $ Name         : chr  "A" "B" "C" "D" ...
##  $ Sex          : chr  "girl" "boy" "boy" "boy" ...
##  $ Residence    : chr  "middle" "north" "middle" "south" ...
##  $ Height       : int  152 164 173 168 175 168 165 169 156 166 ...
##  $ Weight       : int  43 60 83 69 66 55 47 72 58 56 ...
##  $ PC_Ability   : num  2 3 2 3 1 2 1 2 1 3 ...
##  $ PC_Hour_Week : int  12 22 15 30 10 12 16 8 11 26 ...
##  $ Pre_Score    : int  64 80 70 90 50 70 56 72 42 78 ...
##  $ Midterm_Score: int  70 86 66 86 62 78 70 78 62 82 ...
##  $ Final_Score  : int  76 92 82 96 66 62 74 64 60 68 ...
# 執行典型相關分析
ability_set <- as.matrix(CCA_raw$PC_Ability)
score_set <- as.matrix(CCA_raw[c(8,9,10)])

library(CCA)
matcor(ability_set, score_set)
## $Xcor
##      [,1]
## [1,]    1
## 
## $Ycor
##               Pre_Score Midterm_Score Final_Score
## Pre_Score     1.0000000     0.9052961   0.6859947
## Midterm_Score 0.9052961     1.0000000   0.5805411
## Final_Score   0.6859947     0.5805411   1.0000000
## 
## $XYcor
##                         Pre_Score Midterm_Score Final_Score
##               1.0000000 0.8961251     0.8041104   0.5093932
## Pre_Score     0.8961251 1.0000000     0.9052961   0.6859947
## Midterm_Score 0.8041104 0.9052961     1.0000000   0.5805411
## Final_Score   0.5093932 0.6859947     0.5805411   1.0000000
CCA_op <- cc(ability_set,score_set)
CCA_op
## $cor
## [1] 0.9084632
## 
## $names
## $names$Xnames
## NULL
## 
## $names$Ynames
## [1] "Pre_Score"     "Midterm_Score" "Final_Score"  
## 
## $names$ind.names
## NULL
## 
## 
## $xcoef
##          [,1]
## [1,] 1.251864
## 
## $ycoef
##                      [,1]
## Pre_Score      0.08361635
## Midterm_Score -0.01020029
## Final_Score   -0.01933319
## 
## $scores
## $scores$xscores
##              [,1]
##  [1,] -0.08345762
##  [2,]  1.16840666
##  [3,] -0.08345762
##  [4,]  1.16840666
##  [5,] -1.33532190
##  [6,] -0.08345762
##  [7,] -1.33532190
##  [8,] -0.08345762
##  [9,] -1.33532190
## [10,]  1.16840666
## [11,] -0.08345762
## [12,] -1.33532190
## [13,]  1.16840666
## [14,] -0.08345762
## [15,]  1.16840666
## 
## $scores$yscores
##              [,1]
##  [1,] -0.48460050
##  [2,]  0.38072553
##  [3,] -0.05810039
##  [4,]  1.13955628
##  [5,] -1.38029525
##  [6,]  0.20615994
##  [7,] -1.11486493
##  [8,]  0.33472626
##  [9,] -1.93322692
## [10,]  0.71829047
## [11,]  0.54795823
## [12,] -0.79036767
## [13,]  1.11228712
## [14,] -0.26826899
## [15,]  1.59002081
## 
## $scores$corr.X.xscores
##      [,1]
## [1,]    1
## 
## $scores$corr.Y.xscores
##                    [,1]
## Pre_Score     0.8961251
## Midterm_Score 0.8041104
## Final_Score   0.5093932
## 
## $scores$corr.X.yscores
##           [,1]
## [1,] 0.9084632
## 
## $scores$corr.Y.yscores
##                    [,1]
## Pre_Score     0.9864187
## Midterm_Score 0.8851326
## Final_Score   0.5607197
library(CCP)
p_values <- p.asym(CCA_op$cor, 15, 1, 3, tstat = "Wilks")
## Wilks' Lambda, using F-approximation (Rao's F):
##               stat   approx df1 df2      p.value
## 1 to 1:  0.1746947 17.32233   3  11 0.0001771469
p_values
## $id
## [1] "Wilks"
## 
## $stat
## [1] 0.1746947
## 
## $approx
## [1] 17.32233
## 
## $df1
## [1] 3
## 
## $df2
## [1] 11
## 
## $p.value
## [1] 0.0001771469
p_values1 <-p.asym(CCA_op$cor, 15, 1, 3, tstat = "Hotelling")
##  Hotelling-Lawley Trace, using F-approximation:
##              stat   approx df1 df2      p.value
## 1 to 1:  4.724272 17.32233   3  11 0.0001771469
p_values2 <-p.asym(CCA_op$cor, 15, 1, 3, tstat = "Pillai")
##  Pillai-Bartlett Trace, using F-approximation:
##               stat   approx df1 df2      p.value
## 1 to 1:  0.8253053 17.32233   3  11 0.0001771469
p_values3 <-p.asym(CCA_op$cor, 15, 1, 3, tstat = "Roy")
##  Roy's Largest Root, using F-approximation:
##               stat   approx df1 df2      p.value
## 1 to 1:  0.8253053 17.32233   3  11 0.0001771469
## 
##  F statistic for Roy's Greatest Root is an upper bound.
# 计算典型相關係數的解释百分比
total_cor <- sum(CCA_op$cor^2)
total_cor
## [1] 0.8253053
# 每个典型相關係數的解释百分比
explained_variance <- (CCA_op$cor^2) / total_cor  
explained_variance
## [1] 1

##標準化

#標準化
CCA_raw1<-CCA_raw[c(6,8,9,10)]

CCA_Z <- as.data.frame(scale(CCA_raw1))
str(CCA_Z)
## 'data.frame':    15 obs. of  4 variables:
##  $ PC_Ability   : num  -0.0835 1.1684 -0.0835 1.1684 -1.3353 ...
##  $ Pre_Score    : num  -0.4725 0.6178 -0.0636 1.2993 -1.4265 ...
##  $ Midterm_Score: num  -0.735 0.994 -1.167 0.994 -1.6 ...
##  $ Final_Score  : num  -0.114 1.253 0.399 1.595 -0.968 ...
ability_Z <- as.matrix(CCA_Z$PC_Ability)
score_Z <- as.matrix(CCA_Z[c(2,3,4)])

library(CCA)
matcor(ability_Z, score_Z)
## $Xcor
##      [,1]
## [1,]    1
## 
## $Ycor
##               Pre_Score Midterm_Score Final_Score
## Pre_Score     1.0000000     0.9052961   0.6859947
## Midterm_Score 0.9052961     1.0000000   0.5805411
## Final_Score   0.6859947     0.5805411   1.0000000
## 
## $XYcor
##                         Pre_Score Midterm_Score Final_Score
##               1.0000000 0.8961251     0.8041104   0.5093932
## Pre_Score     0.8961251 1.0000000     0.9052961   0.6859947
## Midterm_Score 0.8041104 0.9052961     1.0000000   0.5805411
## Final_Score   0.5093932 0.6859947     0.5805411   1.0000000
CCA_opZ <- cc(ability_Z,score_Z)
CCA_opZ
## $cor
## [1] 0.9084632
## 
## $names
## $names$Xnames
## NULL
## 
## $names$Ynames
## [1] "Pre_Score"     "Midterm_Score" "Final_Score"  
## 
## $names$ind.names
## NULL
## 
## 
## $xcoef
##      [,1]
## [1,]    1
## 
## $ycoef
##                      [,1]
## Pre_Score      1.22706069
## Midterm_Score -0.09437331
## Final_Score   -0.22624983
## 
## $scores
## $scores$xscores
##              [,1]
##  [1,] -0.08345762
##  [2,]  1.16840666
##  [3,] -0.08345762
##  [4,]  1.16840666
##  [5,] -1.33532190
##  [6,] -0.08345762
##  [7,] -1.33532190
##  [8,] -0.08345762
##  [9,] -1.33532190
## [10,]  1.16840666
## [11,] -0.08345762
## [12,] -1.33532190
## [13,]  1.16840666
## [14,] -0.08345762
## [15,]  1.16840666
## 
## $scores$yscores
##              [,1]
##  [1,] -0.48460050
##  [2,]  0.38072553
##  [3,] -0.05810039
##  [4,]  1.13955628
##  [5,] -1.38029525
##  [6,]  0.20615994
##  [7,] -1.11486493
##  [8,]  0.33472626
##  [9,] -1.93322692
## [10,]  0.71829047
## [11,]  0.54795823
## [12,] -0.79036767
## [13,]  1.11228712
## [14,] -0.26826899
## [15,]  1.59002081
## 
## $scores$corr.X.xscores
##      [,1]
## [1,]    1
## 
## $scores$corr.Y.xscores
##                    [,1]
## Pre_Score     0.8961251
## Midterm_Score 0.8041104
## Final_Score   0.5093932
## 
## $scores$corr.X.yscores
##           [,1]
## [1,] 0.9084632
## 
## $scores$corr.Y.yscores
##                    [,1]
## Pre_Score     0.9864187
## Midterm_Score 0.8851326
## Final_Score   0.5607197
library(CCP)
p_valuesZ <- p.asym(CCA_opZ$cor, 15, 1, 3, tstat = "Wilks")
## Wilks' Lambda, using F-approximation (Rao's F):
##               stat   approx df1 df2      p.value
## 1 to 1:  0.1746947 17.32233   3  11 0.0001771469
p_valuesZ
## $id
## [1] "Wilks"
## 
## $stat
## [1] 0.1746947
## 
## $approx
## [1] 17.32233
## 
## $df1
## [1] 3
## 
## $df2
## [1] 11
## 
## $p.value
## [1] 0.0001771469
p_values1Z <-p.asym(CCA_opZ$cor, 15, 1, 3, tstat = "Hotelling")
##  Hotelling-Lawley Trace, using F-approximation:
##              stat   approx df1 df2      p.value
## 1 to 1:  4.724272 17.32233   3  11 0.0001771469
p_values2Z <-p.asym(CCA_opZ$cor, 15, 1, 3, tstat = "Pillai")
##  Pillai-Bartlett Trace, using F-approximation:
##               stat   approx df1 df2      p.value
## 1 to 1:  0.8253053 17.32233   3  11 0.0001771469
p_values3Z <-p.asym(CCA_opZ$cor, 15, 1, 3, tstat = "Roy")
##  Roy's Largest Root, using F-approximation:
##               stat   approx df1 df2      p.value
## 1 to 1:  0.8253053 17.32233   3  11 0.0001771469
## 
##  F statistic for Roy's Greatest Root is an upper bound.