--- title: "R Notebook" output: html_document: df_print: paged --- ```{r} Xs = test2 nperm=100 ``` ```{r} ix = c(4,3,6,5,8,7) iy = 1 r = corls(Xs,permutations = how(nperm = nperm)) rx = r[ix,ix] ry = r[iy,ix] rxi = r[ix[(ix %% 2)>0],ix[(ix %% 2)>0]] ryi = r[iy,ix[(ix %% 2)>0]] ``` ```{r} r2 = (solve(rx) %*% ry) * ry r2 ``` ```{r} sum(r2) ``` ```{r} r2 = (solve(rxi) %*% ryi) * ryi r2 ``` ```{r} sum(r2) ``` ```{r} v = varls(Xs,permutations = how(nperm = nperm)) vt = v[c(iy,ix[(ix %% 2)>0]),c(iy,ix[(ix %% 2)>0])] * (nrow(Xs)-1) vr = v[c(iy+1,ix[(ix %% 2)==0]),c(iy,ix[(ix %% 2)>0])] * (nrow(Xs)-1) vt vr ``` ```{r} #diag(vr)=0 vp = vt - vr vp ``` ```{r} d = sqrt(diag(vp)) n = d %o% d r = vp / n r ``` ```{r} rx = r[ix[(ix %% 2)==0]/2,ix[(ix %% 2)==0]/2] ry = r[iy,ix[(ix %% 2)==0]/2] r2 = (solve(rx) %*% ry) * ry r2 ``` ```{r} sum(r2) ``` ```{r} rp = solve(r) rp = rp / sqrt(diag(rp) %o% diag(rp)) rp rp^2 ``` ```{r} library(ProcMod) r2s = seq(from=0.1, to = 0.9, by = 0.1) n.sim=20 r2.obs = matrix(0,nrow = n.sim,ncol = length(r2s)) d1 = simulate_matrix(10,2000) for (j in seq_along(r2s)) { print(c("r2",as.character(r2s[j]))) for (i in seq_len(n.sim)) { print(c("round",as.character(i))) d2 = simulate_correlation(10,3000,d1,r2s[j]) v = varls2(d1,d2,permutations = 10) s = sqrt(diag(v)) r2.obs[i,j] = (v / outer(s,s))[1,2]^2 } } boxplot(r2.obs) cbind(R2_theo=r2s, R2_obs=colMeans(r2.obs), R2_sd=apply(r2.obs,MARGIN = 2,FUN = sd)) ```