library(readr)
CCA <- read_csv("HW3_data.csv")
## Rows: 15 Columns: 10
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (4): Name, Sex, Residence, PC_Ability
## dbl (6): Height, Weight, PC_Hour_Week, Pre_Score, Midterm_Score, Final_Score
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
str(CCA)
## spc_tbl_ [15 × 10] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ Name         : chr [1:15] "A" "B" "C" "D" ...
##  $ Sex          : chr [1:15] "girl" "boy" "boy" "boy" ...
##  $ Residence    : chr [1:15] "middle" "north" "middle" "south" ...
##  $ Height       : num [1:15] 152 164 173 168 175 168 165 169 156 166 ...
##  $ Weight       : num [1:15] 43 60 83 69 66 55 47 72 58 56 ...
##  $ PC_Ability   : chr [1:15] "good" "excellent" "good" "excellent" ...
##  $ PC_Hour_Week : num [1:15] 12 22 15 30 10 12 16 8 11 26 ...
##  $ Pre_Score    : num [1:15] 64 80 70 90 50 70 56 72 42 78 ...
##  $ Midterm_Score: num [1:15] 70 86 66 86 62 78 70 78 62 82 ...
##  $ Final_Score  : num [1:15] 76 92 82 96 66 62 74 64 60 68 ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   Name = col_character(),
##   ..   Sex = col_character(),
##   ..   Residence = col_character(),
##   ..   Height = col_double(),
##   ..   Weight = col_double(),
##   ..   PC_Ability = col_character(),
##   ..   PC_Hour_Week = col_double(),
##   ..   Pre_Score = col_double(),
##   ..   Midterm_Score = col_double(),
##   ..   Final_Score = col_double()
##   .. )
##  - attr(*, "problems")=<externalptr>
score<-as.matrix(CCA[,c(8,9,10)])
score<-as.data.frame(scale(score))
library(plyr)
CCA$PC_Ability<-mapvalues(CCA$PC_Ability, from=c("bad", "good", "excellent"), to=c(1,2,3))
CCA$PC_Ability<-as.numeric(CCA$PC_Ability)
pc<-as.matrix(CCA[,c(6)])
pc<-as.data.frame(scale(pc))

#correlations matrices
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.10-0 (2023-10-23) 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.
matcor(pc, score)
## $Xcor
##            PC_Ability
## PC_Ability          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
##               PC_Ability Pre_Score Midterm_Score Final_Score
## PC_Ability     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
#canonical correlation analysis
CCA_op<-cc(pc, score)
CCA_op
## $cor
## [1] 0.9084632
## 
## $names
## $names$Xnames
## [1] "PC_Ability"
## 
## $names$Ynames
## [1] "Pre_Score"     "Midterm_Score" "Final_Score"  
## 
## $names$ind.names
##  [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12" "13" "14" "15"
## 
## 
## $xcoef
##            [,1]
## PC_Ability    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]
## PC_Ability    1
## 
## $scores$corr.Y.xscores
##                    [,1]
## Pre_Score     0.8961251
## Midterm_Score 0.8041104
## Final_Score   0.5093932
## 
## $scores$corr.X.yscores
##                 [,1]
## PC_Ability 0.9084632
## 
## $scores$corr.Y.yscores
##                    [,1]
## Pre_Score     0.9864187
## Midterm_Score 0.8851326
## Final_Score   0.5607197
#significance
library(CCP)
cca_sig<-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
#redundancy analysis
yacca::cca(pc,score)
## 
## Canonical Correlation Analysis
## 
## Canonical Correlations:
##      CV 1 
## 0.9084632 
## 
## X Coefficients:
##            CV 1
## PC_Ability    1
## 
## Y Coefficients:
##                      CV 1
## Pre_Score      1.22706069
## Midterm_Score -0.09437331
## Final_Score   -0.22624983
## 
## Structural Correlations (Loadings) - X Vars:
##            CV 1
## PC_Ability    1
## 
## Structural Correlations (Loadings) - Y Vars:
##                    CV 1
## Pre_Score     0.9864187
## Midterm_Score 0.8851326
## Final_Score   0.5607197
## 
## Aggregate Redundancy Coefficients (Total Variance Explained):
##  X | Y: 0.8253053 
##  Y | X: 0.569705