# NOT RUN {
set.seed(3128) # for replicating the result
n=100 # sample size
q=20 # base size
S=100*matrix(rnorm(100),n,q) # base matrix
u=c(rep(0,5),rep(1,3),rep(0,2) ) # True u
v=c(rep(1,5),rep(0,5),rep(1,5) ) # True v
p1=length(u) # length of true u
p2=length(v) #length of true v
U=matrix(rep(u, q),p1,q) # coefficients of base matrix for constructing X
V=matrix(rep(v, q),p2,q) # coefficients of base matrix for constructing Z
x=S%*%t(U) # constructing U
x=x+matrix(rnorm(dim(x)[1]*dim(x)[2]),dim(x)[1],dim(x)[2]); # adding noise
z=S%*%t(V) # constructing V
z=z+matrix(rnorm(dim(z)[1]*dim(z)[2]),dim(z)[1],dim(z)[2]); # adding noise
library(PMA2)
#for beter estimations try with more permutations,
# such as nperms=1000
perm.out <- CCA.permute(x,z,typex="standard",typez="standard",
nperms=10, SD=TRUE, upos = TRUE, vpos = TRUE)
# by SD=TRUE we estimate SD of U and V components too.
# by upos and vpos we restrict the estimations to only
# positive values but this isn't necessary generally
print(perm.out)
out <- CCA(x,z,typex="standard",typez="standard",K=1,
penaltyx=perm.out$bestpenaltyx,penaltyz=perm.out$bestpenaltyz,
v=perm.out$v.init, UVperms = perm.out$UVperms,
allpenaltyxs = perm.out$penaltyxs , upos = TRUE, vpos = TRUE)
print(out)
# results of projection for u
# respectively:True U, Estimated U, Standard deviations, Zscores, nonparametric-Pvalues
utable=base::cbind(u,out$u, out$SDu, out$standardu, out$pvalsu)
colnames(utable)=c("True U", "Estimated U", "SDs", "Zscores", "nonpar-Pvals")
utable
# results of projection for v
# respectively:True V, Estimated V, Standard deviations, Zscores, nonparametric-Pvalues
vtable=base::cbind(v,out$v, out$SDv, out$standardv, out$pvalsv)
colnames(vtable)=c("True V", "Estimated V", "SDs", "Zscores", "nonpar-Pvals")
vtable
# }
Run the code above in your browser using DataLab