[R] code for index of canor analysis

ronggui 0034058 at fudan.edu.cn
Fri Apr 15 07:58:01 CEST 2005


i have search the web and not find code to cal these index.so i write one.if there exists code for such index,i hope you can let me know.

this is my first R code.i send the the list and hope uesRs give me some advise for improve it or check if i make any mistake . i appriciate your suggestion .
i have check the result roughly to the SPSS's. though i do not know excatly how SPSS calculate the redundancy index,but my result is  similar to the SPSS's.

thank you.


--------------------------------------
cancor.index<-function(object,x,y,center=T,scale=F){
x<-scale(x,center=center,scale=scale)
y<-scale(y,center=center,scale=scale)
ncor<-length(object$cor)
#number of canonical variables
nx<-dim(object$xco)[1]
#number of X
ny<-dim(object$yco)[1]
#number of Y
xscore<-x%*%(object$xcoe[,1:ncor])
colnames(xscore)<-paste("con",1:ncor,"x",sep=".")
yscore<-y%*%(object$ycoe[,1:ncor])
colnames(yscore)<-paste("con",1:ncor,"y",sep=".")
#canonical score
eigenvalue<-object$cor^2/(1-object$cor^2)
#eigenvalue/lambda
x.xscore<-cor(x,xscore)
y.yscore<-cor(y,yscore)
#canonical loadings
y.xscore<-cor(y,xscore)
x.yscore<-cor(x,yscore)
#structure loadings/cross loadings
prop.y<-diag(crossprod(y.yscore)/ny)
prop.x<-diag(crossprod(x.xscore)/nx)
#proportion of varoiance accounted for  by its own cv
cr.sqare<-as.vector(object$cor^2)
#canonical R-sqare
RD.yy<-cr.sqare*prop.y
RD.xx<-cr.sqare*prop.x
#proportion variance accounting for my the opposite Can. Var
index<-list(
"xscore"=xscore,"yscore"=yscore,
"eigenvalue"=eigenvalue,
"cr.sqare"=cr.sqare,
"can.loadings.x"=x.xscore,"can.loadings.y"=y.yscore,
"cros.loadings.y"=y.xscore,"cros.loadings.x"=x.yscore,
"prop.var.of.Y.by.CV.Y"=prop.y,
"prop.var.of.X.by.CV.X"=prop.x,
"prop.var.of.y.by.CV-X"=RD.yy,
"prop.var.of.X.by.CV-Y"=RD.xx
)
class(index)<-"cancor.index"
return(index)
}




More information about the R-help mailing list