# R Code for canonical correlation analysis

Jun 29th, 2015
Studypool Tutor
Texas Woman's University
Course: general
Price: \$25 USD

Tutor description

R11 <- cor(sepal.meas) R22 <- cor(petal.meas) R12 <- c(cor(sepal.meas[,1], petal.meas[,1]), cor(sepal.meas[,1], etal.meas[,2]), cor(sepal.meas[,2], petal.meas[,1]), cor(sepal.meas[,2].

Word Count: 2501
Showing Page: 1/10
############################################### R Code for canonical correlation analysis ################################################ We will use the built-in iris data set.# We will consider the entire data set (all three species)attach(iris)# We will standardize the variables first# by dividing by each column's standard deviation:# (we will remove column 5, the species labels)iris.std <- sweep(iris[,-5], 2, sqrt(apply(iris[,-5],2,var)), FUN="/")sepal.meas <- iris.std[,1:2]petal.meas <- iris.std[,3:4]### Doing the CCA the long way:# Finding blocks of the correlation matrix:R11 <- cor(sepal.meas)R22 <- cor(petal.meas)R12 <- c(cor(sepal.meas[,1], petal.meas[,1]), cor(sepal.meas[,1], petal.meas[,2]), cor(sepal.meas[,2], petal.meas[,1]), cor(sepal.meas[,2], petal.meas[,2]))R12 <- matrix(R12, ncol=ncol(R22), byrow=T) # R12 has q2 columns, same as number of petal measurementsR21 <- t(R12) # R21=transpose of R12# Finding the E1 and E2 matrices:E1 <- solve(R11) %*% R12 %*% solve(R22) %*% R21E2 <- solve(R22) %*% R21 %*% solve(R11) %*% R12# print(E1)# print(E2)eigen(E1)eigen(E2)# The canonical correlations are:canon.corr <- sqrt(eigen(E1)\$values)canon.corr# The canonical variates are based on the eigenvectors of E1 and E2:# a1 = (0.922, -0.388)# b1 = (0.943, -0.333)# a2 = (0.457, 0.890)# b2 = (-0.679, 0.734)# Only the first canonical correlation is really substantial:# u1 = 0.92*Sepal.Length - 0.39*Sepal.Width# v1 =

## Review from student

Studypool Student
" Goes above and beyond expectations ! "

1821 tutors are online

Brown University

1271 Tutors

California Institute of Technology

2131 Tutors

Carnegie Mellon University

982 Tutors

Columbia University

1256 Tutors

Dartmouth University

2113 Tutors

Emory University

2279 Tutors

Harvard University

599 Tutors

Massachusetts Institute of Technology

2319 Tutors

New York University

1645 Tutors

Notre Dam University

1911 Tutors

Oklahoma University

2122 Tutors

Pennsylvania State University

932 Tutors

Princeton University

1211 Tutors

Stanford University

983 Tutors

University of California

1282 Tutors

Oxford University

123 Tutors

Yale University

2325 Tutors