Skip to content

Commit 62816ba

Browse files
authored
Add files via upload
change from gradually remove the Orthogonal variation to remove them together
1 parent fc6dc4f commit 62816ba

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

R/o2pls.R

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
#' \item{CXosc}{Orthogonal \eqn{Y} weights}
2121
#' \item{BU}{Regression coefficient in \code{Tt} ~ \code{U}}
2222
#' \item{BT}{Regression coefficient in \code{U} ~ \code{Tt}}
23-
#' \item{R2Xhat}{Prediction of \eqn{X} with \eqn{Y}}
24-
#' \item{R2Yhat}{Prediction of \eqn{Y} with \eqn{X}}
23+
#' \item{Xhat}{Prediction of \eqn{X} with \eqn{Y}}
24+
#' \item{Yhat}{Prediction of \eqn{Y} with \eqn{X}}
25+
#' \item{R2Xhat}{Variation of the predicted \eqn{X} as proportion of variation in \eqn{X}}
26+
#' \item{R2Yhat}{Variation of the predicted \eqn{Y} as proportion of variation in \eqn{Y}}
2527
#' \item{R2X}{Variation of the modeled part in \eqn{X} (defined by Joint + Orthogonal variation) as proportion of total variation in \eqn{X}}
2628
#' \item{R2Y}{Variation of the modeled part in \eqn{Y} (defined by Joint + Orthogonal variation) as proportion of total variation in \eqn{Y}}
2729
#' \item{R2Xcorr}{Variation of the joint part in \eqn{X} }
@@ -94,7 +96,6 @@ o2pls <- function(X,Y,nc,nx,ny,scale=FALSE,center=FALSE){
9496
CXosc <- matrix(0, ncy, nny)
9597

9698
# Calculate the PCA components of Y'T
97-
## nn<-nc+max(nx+ny)
9899
###borrow idea from OmicsPLS
99100
ns <- nc + max(nx, ny)
100101
CDW <- eigenmult(t(Y),X)
@@ -106,50 +107,43 @@ o2pls <- function(X,Y,nc,nx,ny,scale=FALSE,center=FALSE){
106107
# Yscore <- Y %*% Yloading
107108
Yscore <- eigenmult(Y,Yloading)
108109
if(nx>0){
109-
for(i in 1:nx){
110+
#####Notice the change #### Now remove the orthogonal by doing once
111+
#### Still thinking to do it one by one #######
112+
#### In previous version I did this one by one #######
110113
# Exy <- X - Xscore%*%t(Xloading)
111114
Exy <- X - eigenmult(Xscore, t(Xloading))
112115
# wosc<- svd(t(Exy)%*%Xscore,nu=1,nv=0)$u
113-
wosc<- svd(eigenmult(t(Exy),Xscore),nu=1,nv=0)$u
116+
wosc<- svd(eigenmult(t(Exy),Xscore),nu=nx,nv=0)$u
114117
# t_yo <- X%*%wosc
115118
t_yo <- eigenmult(X,wosc)
116119
# p_yo <- t(solve(t(t_yo)%*%t_yo)%*%t(t_yo)%*%X)
117120
p_yo <- t(eigenthree(solve(AtA(t_yo)),t(t_yo),X))
118121
# X <- X - t_yo%*%t(p_yo)
119122
X <- X - eigenmult(t_yo,t(p_yo))
120-
TYosc[,i] <- t_yo
121-
PYosc[,i] <- p_yo
122-
WYosc[,i] <- wosc
123-
#Xscore <- X%*%Xloading
124-
CDW <- eigenmult(t(Y), X)
125-
cdw <- svd(CDW, nu = nc, nv = nc)
126-
Xloading <- cdw$v
123+
TYosc <- t_yo
124+
PYosc <- p_yo
125+
WYosc <- wosc
127126
Xscore <- eigenmult(X, Xloading)
128-
}
127+
129128
}
130129
if(ny>0){
131-
for(i in 1:ny){
132130
# Fxy <- Y - Yscore %*% t(Yloading)
133131
Fxy <- Y - eigenmult(Yscore, t(Yloading))
134132
# cosc <- svd(t(Fxy)%*%Yscore,nu=1,nv=0)$u
135-
cosc <- svd(eigenmult(t(Fxy), Yscore),nu=1,nv=0)$u
133+
cosc <- svd(eigenmult(t(Fxy), Yscore),nu=ny,nv=0)$u
136134
# u_xo <- Y%*%cosc
137135
u_xo <- eigenmult(Y, cosc)
138136
# p_xo <- t(solve(t(u_xo)%*%u_xo)%*%t(u_xo)%*%Y)
139137
p_xo <- t(eigenthree(solve(AtA(u_xo)),t(u_xo),Y))
140138
# Y <- Y - u_xo%*%t(p_xo)
141139
Y <- Y - eigenmult(u_xo, t(p_xo))
142-
UXosc[,i] <- u_xo
143-
PXosc[,i] <- p_xo
144-
CXosc[,i] <- cosc
140+
UXosc <- u_xo
141+
PXosc <- p_xo
142+
CXosc <- cosc
145143
# Yscore <- Y%*%Yloading
146-
CDW <- eigenmult(t(Y), X)
147-
cdw <- svd(CDW, nu = nc, nv = nc)
148-
Yloading <- cdw$u
149144
Yscore <- eigenmult(Y, Yloading)
150-
}
151145
}
152-
##calculate again
146+
##calculate the score and loading again
153147
CDW <- eigenmult(t(Y), X)
154148
cdw <- svd(CDW, nu = nc, nv = nc)
155149
Xloading <- cdw$v
@@ -168,8 +162,13 @@ o2pls <- function(X,Y,nc,nx,ny,scale=FALSE,center=FALSE){
168162
R2Y <- 1- s2(Fxy)/SSY
169163
Xh <- eigenthree(Yscore, BU, t(Xloading))
170164
Yh <- eigenthree(Xscore, BT, t(Yloading))
171-
R2Xhat <- 1 - s2(Xh-Xt)/SSX
172-
R2Yhat <- 1 - s2(Yh-Yt)/SSY
165+
#####
166+
# R2Xhat <- 1 - s2(Xh-Xt)/SSX
167+
# R2Yhat <- 1 - s2(Yh-Yt)/SSY
168+
#### #########
169+
R2Xhat <- (s2(Yscore %*% BU)/s2(Xt))
170+
R2Yhat <- (s2(Xscore %*% BT)/s2(Yt))
171+
####
173172
R2Xp <- (s2(Xh)/SSX)/R2Xcorr
174173
R2Yp <- (s2(Yh)/SSY)/R2Ycorr
175174
Qx <- Q(Xt,Xh)
@@ -178,10 +177,10 @@ o2pls <- function(X,Y,nc,nx,ny,scale=FALSE,center=FALSE){
178177
varYj = apply(Yscore,2,function(x)sum(x^2))/SSY
179178
varXorth = apply(PYosc,2,function(x)sum(x^2))*apply(TYosc,2,function(x)sum(x^2))/SSX
180179
varYorth = apply(PXosc,2,function(x)sum(x^2))*apply(UXosc,2,function(x)sum(x^2))/SSY
181-
rownames(Xscore) <- rownames(TYosc) <- rownames(Exy) <- rownames(Xt)
182-
rownames(Yscore) <- rownames(UXosc) <- rownames(Fxy) <- rownames(Yt)
183-
rownames(Xloading) <- rownames(PYosc) <- rownames(WYosc) <- colnames(Exy) <- colnames(Xt)
184-
rownames(Yloading) <- rownames(PXosc) <- rownames(CXosc) <- colnames(Fxy) <- colnames(Yt)
180+
rownames(Xscore) <- rownames(TYosc) <- rownames(Exy) <- rownames(Xh) <- rownames(Xt)
181+
rownames(Yscore) <- rownames(UXosc) <- rownames(Fxy) <- rownames(Yh) <- rownames(Yt)
182+
rownames(Xloading) <- rownames(PYosc) <- rownames(WYosc) <- colnames(Exy) <- colnames(Xh)<- colnames(Xt)
183+
rownames(Yloading) <- rownames(PXosc) <- rownames(CXosc) <- colnames(Fxy) <- colnames(Yh)<-colnames(Yt)
185184
colnames(Xscore) <- colnames(Yscore) <- colnames(Xloading) <- colnames(Yloading) <- paste0("LV",1:nc)
186185
colnames(TYosc) <- colnames(PYosc) <- colnames(WYosc) <- paste0("LV", 1:nnx)
187186
colnames(UXosc) <- colnames(PXosc) <- colnames(CXosc) <- paste0("LV", 1:nny)
@@ -194,6 +193,7 @@ o2pls <- function(X,Y,nc,nx,ny,scale=FALSE,center=FALSE){
194193
PXosc = PXosc, PYosc = PYosc,
195194
WYosc = WYosc, CXosc = CXosc,
196195
R2Xhat = R2Xhat, R2Yhat = R2Yhat,
196+
Xhat = Xh, Yhat= Yh,
197197
Qx = Qx, Qy = Qy,
198198
varXj = varXj, varYj = varYj,
199199
varXorth = varXorth, varYorth =varYorth,

0 commit comments

Comments
 (0)