######################################################################### # # Correlacao canonica # ######################################################################### # # A researcher has collected data on three psychological variables, # four academic variables (standardized test scores) and gender for # 600 college freshman. She is interested in how the set of # psychological variables relates to the academic variables and # gender. In particular, the researcher is interested in how many # dimensions (canonical variables) are necessary to understand # the association between the two sets of variables. # The psychological variables are: # locus_of_control # self_concept # motivation # The academic variables are standardized tests in # reading (read) # writing (write) # math (math) # science (science) # Additionally, the variable female is a zero-one indicator variable # with the one indicating a female student. # # Source: http://www.ats.ucla.edu/stat/r/dae/canonical.htm # ######################################################################### install.packages("GGally") install.packages("CCA") library(GGally) library(CCA) mm = read.csv("http://www.ats.ucla.edu/stat/data/mmreg.csv") colnames(mm) = c("Control","Concept","Motivation","Read","Write","Math","Science","Sex") X = as.matrix(mm[, 1:2]) Y = as.matrix(mm[, 4:7]) # scatterplot Y ggpairs(Y) # scatterplot X ggpairs(X) # correlations matcor(X,Y) # Ajustando modelo de correlacoes canonicas fit = cc(X,Y) # correlacoes canonicas fit$cor # pesos canonicos fit$ycoef fit$xcoef # Variaveis canonicas A = X%*%fit$xcoef B = Y%*%fit$ycoef par(mfrow=c(1,2)) plot(A[,1],B[,1],xlab="1a variavel canonica (X)",ylab="1a variavel canonica (Y)") title(paste("1a correlacao canonica=",round(fit$cor[1],4),sep="")) plot(A[,2],B[,2],xlab="2a variavel canonica (X)",ylab="2a variavel canonica (Y)") title(paste("2a correlacao canonica=",round(fit$cor[2],4),sep="")) # Cargas canonicas CCXA=cor(X,A) CCYB=cor(Y,B) round(CCXA,3) round(CCYB,3) # Determinação da % da variância explicada pelas componentes CCYB^2 CCXA^2 # Determinação das cargas cruzadas cor(Y,A) cor(X,B)