Canonical Correlation Analysis using R

 





How to perform canonical correlation in R?

Follow this step to find canonical correlation in R-
  •     open R program
  •     open a new script
  •     import your data set
  •     define the independent and dependent variable
  •     combing the data into matrix
  •     convert the data into standard normal
  •     block the correlation matrix
  •     find the eigen value
  •     calculate canonical correlation

R code for finding canonical correlation

###calculating canonical correlation

#import data set

library(foreign)
df <- read.spss("data set location/data set name", use.value.label=TRUE, to.data.frame=TRUE)
df     #df=data set

#define independent variable 

##Fathers education(X1)
##Mothers education(X2)
##income(X3)

X1<-df[,2]
X1
X2<-df[,3]
X2
X3<-df[,4]
X3

#define dependent variable 

##JSC result(Y1)
##SSC result(Y2) 
##HSC result(Y3)
##Academic awards(Y4)

Y1<-df[,6]
Y1
Y2<-df[,7]
Y2
Y3<-df[,8]
Y3
Y4<-df[,9]
Y4

#combinig independent variable
##EC=economic condition

EC<-cbind(X1,X2,X3)
dim(EC)

#combining dependent variable
#AA=academic performance

AA<-cbind(Y1,Y2,Y3,Y4)
dim(AA)

#standerized dependent and independent variable

x.std<-sweep(EC,2,sqrt(apply(EC,2,var)),FUN="/")
x.std

y.std<-sweep(AA,2,sqrt(apply(AA,2,var)),FUN="/")
y.std


*******************
#block of correlation matrix


R11<-cor(x.std)
R11
R22<-cor(y.std)
R22
R12<-cor(x.std,y.std)
R12
R21<-t(R12)
R21


#finding E1 and E2 matrix

E1<-solve(R11)%*%(R12)%*%solve(R22)%*%(R21)
E1
E2<-solve(R22)%*%(R21)%*%solve(R11)%*%(R12)
E2

#finding eigen vales

eigen(E1)
eigen(E2)

#cononical correlation

cannon.corr<-sqrt(eigen(E1)$values)
cannon.corr



You can watch the full video for step by step procedur-





Post a Comment

0 Comments