Hello, I am trying to perform partial Canonical Correlation (CCA) in R. I have calculated the CCA between two vectors X and Y and now I want to "control" for Z. I know I have to modify the cc and rcc functions to include Z but what I am doing does not seem to be working... Below I have the original code and my modified code Original cc: function (X, Y) { Xnames = dimnames(X)[[2]] Ynames = dimnames(Y)[[2]] ind.names = dimnames(X)[[1]] res = rcc(X, Y, 0, 0) return(res) } I changed this to: function (X, Y, Z) { Xnames = dimnames(X)[[2]] Ynames = dimnames(Y)[[2]] Znames = dimnames(Z) [[2]] ind.names = dimnames(X)[[1]] res = rcc2(X, Y, Z, 0, 0, 0) return(res) } and then for rcc Original rcc: function (X, Y, lambda1, lambda2) { Xnames <- dimnames(X)[[2]] Ynames <- dimnames(Y)[[2]] ind.names <- dimnames(X)[[1]] Cxx <- var(X, na.rm = TRUE, use = "pairwise") + diag(lambda1, ncol(X)) Cyy <- var(Y, na.rm = TRUE, use = "pairwise") + diag(lambda2, ncol(Y)) Cxy <- cov(X, Y, use = "pairwise") res <- geigen(Cxy, Cxx, Cyy) names(res) <- c("cor", "xcoef", "ycoef") scores <- comput(X, Y, res) return(list(cor = res$cor, names = list(Xnames = Xnames, Ynames = Ynames, ind.names = ind.names), xcoef = res$xcoef, ycoef = res$ycoef, scores = scores)) } I know I have to calculate Czz Czx and Czy but do I have to calculate Cxy.z (partial covariance)? and I am not sure what to add in the res<- geigen etc when I add Czz and/or Czx Czy I get an error - I have also left scores and return ans original since I am not sure what to include modified rcc2 function (X, Y, Z, lambda1, lambda2, lambda3) { Xnames <- dimnames(X)[[2]] Ynames <- dimnames(Y)[[2]] Znames <- dimnames(Z)[[2]] ind.names <- dimnames(X)[[1]] Cxx <- var(X, na.rm = TRUE, use = "pairwise") + diag(lambda1, ncol(X)) Cyy <- var(Y, na.rm = TRUE, use = "pairwise") + diag(lambda2, ncol(Y)) Czz <- var(Z, na.rm = TRUE, use = "pairwise") + diag(lambda3, ncol(Z)) Cxy <- cov(X, Y, use = "pairwise") Czx <- cov(X, Z, use = "pairwise") Czy <- cov(Y, Z, use = "pairwise") # Cxy.z? res <- geigen(Cxy, Cxx, Cyy, Czz, Czx, Czy) # doesnt work names(res) <- c("cor", "xcoef", "ycoef", "zcoef") scores <- comput(X, Y, Z, res) return(list(cor = res$cor, names = list(Xnames = Xnames, Ynames = Ynames, Znames=Znames, ind.names = ind.names), xcoef = res$xcoef, ycoef = res$ycoef, zcoef = res$zcoef, scores = scores)) } Any help would be really appreciated..I am completely new to this and lost. many thanks Joe