-
Notifications
You must be signed in to change notification settings - Fork 0
/
NewRobust.R
156 lines (141 loc) · 4.53 KB
/
NewRobust.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
##### CCA Calculation with outlier ##############
#================================================
source(MeanVar.r)
library(MASS)
Data=rbind(matrix(rnorm(90,2,1),ncol=6),matrix(rnorm(30,10,1),ncol=6))
colnames(Data)=c("x1","x2","x3","x4","x5","x6")
plot(Data) #cor(Data)
x=Data[,c(1,2,3)]
y=Data[,c(4,5,6)]
Rxx=cor(x)
Ryy=cor(y)
Rxy=cor(x,y)
Ryx=cor(y,x)
CCA=function(Rxx,Ryy,Rxy,Ryx)
{
Px<-solve(Rxx)%*%Rxy%*%solve(Ryy)%*%t(Rxy);Px
Py<-solve(Ryy)%*%t(Rxy)%*%solve(Rxx)%*%Rxy;Py
Egn1=eigen(Px);Egn2=eigen(Py)
l=eigen(Px)$values
#eigen(Px)
a<-eigen(Px)$vectors#[,1]
b<-eigen(Py)$vectors#[,1]
ccc=sqrt(l)
p<-length(Y);p
q<-length(X);q
n<-nrow(X);n
CHI<- -((n-1)-.5*(p+q+1))*log(1-l)
Rxxi=Rxx%*%a ## Caninical Loading :Measure of simple correlation of original variable
Ryyi=Ryy%*%b ##with corresponding canonical variable.
rxy=Ryyi%*%sqrt(l) #Cross loading
ryx=Rxxi%*%sqrt(l)
#Rsqiy=
Rsqygx=(l/p)%*%t(Ryyi)%*%Ryyi # Proportion of explained variance
Rsqxgy=(l/q)%*%t(Rxxi)%*%Rxx
return(list(Canonical=ccc,Chisquare=CHI,LoadingX=Rxxi,
LoadingY=Ryyi,CrossloadXY=rxy,CrosssloadYX=ryx))
}
Result=CCA(Rxx,Ryy,Rxy,Ryx)
RR=cancor(x,y)
###========================================================
##===ROBUST CCA CALCULATION WITH OUTLIER ===############################
DataR=cov2cor(MeanVar(Data,Beta=0.1)$V)
Rxxr=DataR[1:3,1:3]
Ryyr=DataR[4:6,4:6]
Rxyr=DataR[1:3,4:6]
Ryxr=DataR[4:6,1:3]
CCA=function(Rxxr,Ryyr,Rxyr,Ryxr)
{
Pxr<-solve(Rxxr)%*%Rxyr%*%solve(Ryyr)%*%t(Rxyr);Pxr
Pyr<-solve(Ryyr)%*%t(Rxyr)%*%solve(Rxxr)%*%Rxyr;Pyr
Egn1=eigen(Pxr);Egn2=eigen(Pyr)
lr=eigen(Pxr)$values
#eigen(Pxr)
ar<-eigen(Pxr)$vectors#[,1]
br<-eigen(Pyr)$vectors#[,1]
rccc=sqrt(l)
p<-length(Y);p
q<-length(X);q
n<-nrow(Data);n
CHIr<- -((n-1)-.5*(p+q+1))*log(1-l)
Rxxir=Rxxr%*%ar ## Caninical Loading :Measure of simple correlation of original variable
Ryyir=Ryyr%*%br ##with corresponding canonical variable.
rxyr=Ryyir%*%sqrt(lr) #Cross loading
ryxr=Rxxir%*%sqrt(lr)
#Rsqiy=
Rsqygx=(l/p)%*%t(Ryyir)%*%Ryyir # Proportion of explained variance
Rsqxgy=(l/q)%*%t(Rxxir)%*%Rxxr
return(list(Canonical=rccc,Chisquare=CHIr,LoadingXr=Rxxir,
LoadingYr=Ryyir,CrossloadXYr=rxyr,CrosssloadYXr=ryxr))
}
Resultr=CCA(Rxxr,Ryyr,Rxyr,Ryxr)
###======================================================================
###Without outlier CCA========================================
DD=matrix(rnorm(120,2,1),ncol=6)
colnames(DD)=c("x1","x2","x3","x4","x5","x6")
plot(DD) #cor(Data)
x=DD[,c(1,2,3)]
y=DD[,c(4,5,6)]
Rxx=cor(x)
Ryy=cor(y)
Rxy=cor(x,y)
Ryx=cor(y,x)
CCA=function(Rxx,Ryy,Rxy,Ryx)
{
Px<-solve(Rxx)%*%Rxy%*%solve(Ryy)%*%t(Rxy);Px
Py<-solve(Ryy)%*%t(Rxy)%*%solve(Rxx)%*%Rxy;Py
Egn1=eigen(Px);Egn2=eigen(Py)
l=eigen(Px)$values
#eigen(Px)
a<-eigen(Px)$vectors#[,1]
b<-eigen(Py)$vectors#[,1]
ccc=sqrt(l)
p<-length(Y);p
q<-length(X);q
n<-nrow(X);n
CHI<- -((n-1)-.5*(p+q+1))*log(1-l)
Rxxi=Rxx%*%a ## Caninical Loading :Measure of simple correlation of original variable
Ryyi=Ryy%*%b ##with corresponding canonical variable.
rxy=Ryyi%*%sqrt(l) #Cross loading
ryx=Rxxi%*%sqrt(l)
#Rsqiy=
Rsqygx=(l/p)%*%t(Ryyi)%*%Ryyi # Proportion of explained variance
Rsqxgy=(l/q)%*%t(Rxxi)%*%Rxx
return(list(Canonical=ccc,Chisquare=CHI,LoadingX=Rxxi,
LoadingY=Ryyi,CrossloadXY=rxy,CrosssloadYX=ryx))
}
Result=CCA(Rxx,Ryy,Rxy,Ryx)
Resultp=cancor(x,y)
###========================================================
##===ROBUST CCA CALCULATION ===############################
DataR=cov2cor(MeanVar(DD,Beta=0.1)$V)
Rxxr=DataR[1:3,1:3]
Ryyr=DataR[4:6,4:6]
Rxyr=DataR[1:3,4:6]
Ryxr=DataR[4:6,1:3]
CCA=function(Rxxr,Ryyr,Rxyr,Ryxr)
{
Pxr<-solve(Rxxr)%*%Rxyr%*%solve(Ryyr)%*%t(Rxyr);Pxr
Pyr<-solve(Ryyr)%*%t(Rxyr)%*%solve(Rxxr)%*%Rxyr;Pyr
Egn1=eigen(Pxr);Egn2=eigen(Pyr)
lr=eigen(Pxr)$values
#eigen(Pxr)
ar<-eigen(Pxr)$vectors#[,1]
br<-eigen(Pyr)$vectors#[,1]
rccc=sqrt(l)
p<-length(Y);p
q<-length(X);q
n<-nrow(Data);n
CHIr<- -((n-1)-.5*(p+q+1))*log(1-l)
Rxxir=Rxxr%*%ar ## Caninical Loading :Measure of simple correlation of original variable
Ryyir=Ryyr%*%br ##with corresponding canonical variable.
rxyr=Ryyir%*%sqrt(lr) #Cross loading
ryxr=Rxxir%*%sqrt(lr)
#Rsqiy=
Rsqygx=(l/p)%*%t(Ryyir)%*%Ryyir # Proportion of explained variance
Rsqxgy=(l/q)%*%t(Rxxir)%*%Rxxr
return(list(Canonical=rccc,Chisquare=CHIr,LoadingXr=Rxxir,
LoadingYr=Ryyir,CrossloadXYr=rxyr,CrosssloadYXr=ryxr))
}
Resultr=CCA(Rxxr,Ryyr,Rxyr,Ryxr)
###======================================================================