Bricolage.Rmd 8.85 KB
 Eric Coissac committed Jan 24, 2019 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 --- 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)  Eric Coissac committed Apr 16, 2019 87 88 r2s = seq(from=0.0, to = 0.9, by = 0.1) n.sim=1000  Eric Coissac committed Jan 24, 2019 89 r2.obs = matrix(0,nrow = n.sim,ncol = length(r2s))  Eric Coissac committed Apr 16, 2019 90 d1 = simulate_matrix(10,2)  Eric Coissac committed Jan 24, 2019 91 for (j in seq_along(r2s)) {  Eric Coissac committed Apr 16, 2019 92  #print(c("r2",as.character(r2s[j])))  Eric Coissac committed Jan 24, 2019 93  for (i in seq_len(n.sim)) {  Eric Coissac committed Apr 16, 2019 94 95 96 97 98 99 100  # print(c("round",as.character(i))) d2 = simulate_correlation(d1,1,r2s[j]) #r2.obs[i,j] = cor5(d1,d2)^2 r2.obs[i,j] = ((max(0,.covls(d1,d2)-.rcovls(d1,d2)))/(sqrt(.covls(d1,d1)-.rcovls(d1,d1))*sqrt(.covls(d2,d2)-.rcovls(d2,d2))))^2 # v = varls(d1,d2,permutations = 10) # s = sqrt(diag(v)) # r2.obs[i,j] = (v / outer(s,s))[1,2]^2  Eric Coissac committed Jan 24, 2019 101 102 103 104 105 106 107 108  } } boxplot(r2.obs) cbind(R2_theo=r2s, R2_obs=colMeans(r2.obs), R2_sd=apply(r2.obs,MARGIN = 2,FUN = sd))   Eric Coissac committed Apr 16, 2019 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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 {r} a= simulate_matrix(40,1) b= simulate_matrix(40,2) x = varls(a,b,permutations = NULL) y = corls(a,b,permutations = 100) y^2 m = x - y #(sqrt(outer(diag(x),diag(x)))/m)^2 #sqrtm(outer(diag(x),diag(x)))  5 -> 6 10 -> 12 6 20 -> 28 14 30 -> 44 22 40 -> 60 30 20 {r} i=c(5,10,20,30,40) j=c(6,12,28,44,60) lm(j~i) plot(i,j,xlim=c(0,50),ylim = c(-10,70)) abline(lm(j~i)) plot(sqrt(i),sqrt(j)) lm(sqrt(j)~sqrt(i)) plot(i^2,j^2) lm(i^2~j^2)  {r} a = simulate_matrix(20,1) b = vector(mode = "list",9) for (i in seq_along(b)) { b[[i]] = simulate_correlation(a,1,i/10) } b = c(list(a),b) var(do.call(cbind,b)) var2(do.call(cbind,b)) v.perm=varls(as.procmod.frame(b),permutations = 100,rcovls = TRUE) v.perm m = apply(attributes(v.perm)$rcov,MARGIN = c(1,2),mean,na.rm=TRUE)^2 mean(m[upper.tri(m,diag = TRUE)],na.rm=TRUE)  {r} library(ProcMod) ps = c(seq(1,10,by=1),seq(15,100,by=5),1000) qs = ps ns = seq(5,20, by=5) n.sim=1000 r2.obs = array(0,dim = c(length(ns),length(ps),length(qs),n.sim)) for (ni in seq_along(ns)) { N=ns[ni] for (pi in seq_along(ps)) { P = ps[pi] for (qi in seq_along(qs)) { Q = qs[qi] for (si in 1:n.sim) { not_ok = TRUE while(not_ok) { d1 = simulate_matrix(N,P,equal.var = TRUE) d2 = simulate_matrix(N,Q,equal.var = TRUE) r2=tryCatch({v = varls(d1,d2,permutations = 0) s = sqrt(diag(v)) (v / outer(s,s))[1,2]^2 }, error=function(cond) {#message("Redo simulation") NA} ) r2.obs[ni,pi,qi,si] = r2 not_ok=is.na(r2) } } } } }  {r} R2_obs=apply(r2.obs,MARGIN = c(1,2,3),FUN = mean) R2_sd=apply(r2.obs,MARGIN = c(1,2,3),FUN = sd) dimnames(R2_obs) = list(ns,ps,qs) N = rep(ns,length(ps)*length(qs)) P = rep(rep(ps,rep(length(ns),length(ps))),length(qs)) Q = rep(qs,rep(length(ns)*length(ps),length(qs))) R2_obs.tab = tibble(N=N, P=P, Q=Q, Pmin = mapply(min,P,Q), Qmax = mapply(max,P,Q), mean=as.numeric(R2_obs), sd=as.numeric(R2_sd)) R2_obs.tab$NPmin = mapply(min,R2_obs.tab$Pmin,N) R2_obs.tab$NPmax = mapply(max,R2_obs.tab\$Pmin,N) write_csv2(R2_obs.tab,"R2_H0.csv") #p = rep(ps,rep(length(qs),length(ps))) #q = rep(qs,length(ps)) #R2_obs.tn = data.frame(p,q,N=N,mean=as.numeric(R2_obs),sd=as.numeric(R2_sd))  {r} library(expm)  # Modle 1 : l'axe suivant se prend le reste {r} R2.alea.predict.single = function(N,nP,nQ) { # N : nombre de points # nP : Nb de var dans petit tableau # nQ : Nb de var dans grand tableau # Axes principaux : P = (N-2)/(N-1) ; Q = 1/(N-1) Axes.main = min(nP,(N-1)) i = 0:(Axes.main-1) Q = 1/(N-1) P = (N-2)/(N-1) Pis = P**i R2.main = sum(Pis)*Q # Axes secondaires : Q = Axes.main/(N-1)^2 ; P = 1 - Q ; Axes.comp = max(nP,nQ) - Axes.main if (Axes.comp >0) { i = 0:(Axes.comp-1) Q = 1/(N-1)^2 P = 1 - Q Pis = (P**i) R2.main = P**Axes.comp*R2.main+Q*sum(Pis) } return(R2.main) }  # Modle 2 : l'axe suivant se prend le reste {r} R2.alea.predict.single = function(N,nP,nQ) { # N : nombre de points # nP : Nb de var dans petit tableau # nQ : Nb de var dans grand tableau # Axes principaux : P = (N-2)/(N-1) ; Q = 1/(N-1) Axes.main = min(nP,(N-1)) Rres = numeric(Axes.main) R2.main=0 for (i in 1:Axes.main) { Rres[i]=1-R2.main if (i==1) k = 0 else k = sum(Rres[1:(i-1)]^2) # print(k / (N-1)^2 * Rres[i]) R2.main = R2.main + Rres[i] * ( 1/(N-1) - k/(N-1)^2) } # R2.main = R2.main - 2*Axes.main / (N-1)^2 # print(2*Axes.main / (N-1)^2) # Axes secondaires : Q = Axes.main/(N-1)^2 ; P = 1 - Q ; k=sum(Rres^2) + (1 - R2.main)^2 Axes.comp = max(nP,nQ) - Axes.main if (Axes.comp >0) { for (i in 1:Axes.comp) { #print(R2.main) R2.main = R2.main + (1-R2.main) * k/(N-1)^2 } } return(R2.main) } R2.alea.predict =Vectorize(R2.alea.predict.single, vectorize.args = c("N","nP","nQ"))  # Modle 3 : l'axe suivant se prend le reste {r} R2.alea.predict.single = function(N,nP,nQ) { # N : nombre de points # nP : Nb de var dans petit tableau # nQ : Nb de var dans grand tableau # Axes principaux : P = (N-2)/(N-1) ; Q = 1/(N-1) Axes.main = min(nP,(N-1)) Rres = numeric(Axes.main) R2.main=0 for (i in 1:Axes.main) { Rres[i]=1-R2.main R2.main = R2.main + Rres[i] * ( 1/(N-1) - (Axes.main - i )/(N-1)^2) } # R2.main = R2.main - 2*Axes.main / (N-1)^2 # print(2*Axes.main / (N-1)^2) # Axes secondaires : Q = Axes.main/(N-1)^2 ; P = 1 - Q ; k=sum(Rres) + (1 - R2.main) Axes.comp = max(nP,nQ) - Axes.main if (Axes.comp >0) { for (i in 1:Axes.comp) { #print(R2.main) R2.main = R2.main + (1-R2.main) * k/(N-1)^2 } } return(R2.main) } R2.alea.predict =Vectorize(R2.alea.predict.single, vectorize.args = c("N","nP","nQ"))  {r} library(tidyverse) R2_obs.tab = read_csv2("R2_H0.csv")  {r} R2_obs.tab.PQ = R2_obs.tab %>% filter(Qmax==Pmin)  {r} with(data = R2_obs.tab.PQ, plot(mean, R2.alea.predict(N,Pmin,Qmax), col=rainbow(4)[N/5], pch=(1+(Qmax==Pmin)), cex=0.5, log="") ) abline(b=1,a=0) abline(h=1,lty=2) abline(v=1,lty=2) abline(lm(R2.alea.predict(N,Pmin,Qmax)~mean,data = R2_obs.tab.PQ)) #summary(lm(mean~R2.alea.predict(N,Pmin,Qmax),data = R2_obs.tab.PQ)) legend("bottomright",legend = (1:4)*5,fill=rainbow(4),cex = 0.5)  {r} with(data = R2_obs.tab.PQ, plot(mean, R2.alea.predict(N,Pmin,Qmax)-mean, col=rainbow(4)[N/5], pch=(1+(Qmax==Pmin)), cex=0.5, log="") ) abline(h=1) abline(v=2) abline(v=c(5,10,15,20)*2.5,lty=2) #abline(lm(log10(R2.alea.predict(N,Pmin,Qmax)/mean)[-1]~log10(Pmin)[-1],data=R2_obs.tab.PQ)) legend("topright",legend = (1:4)*5,fill=rainbow(4),cex = 0.5) #summary(lm(log10(R2.alea.predict(N,Pmin,Qmax)/mean)[-1]~log10(Pmin)[-1],data=R2_obs.tab.PQ))  {r} with(data = R2_obs.tab.PQ, plot(Pmin, R2.alea.predict(N,Pmin,Qmax)-mean, col=rainbow(4)[N/5], pch=(1+(Qmax==Pmin)), cex=0.5, log="x") ) abline(h=1) abline(v=2) abline(v=c(5,10,15,20)*4,lty=2,col=rainbow(4)) abline(v=c(5,10,15,20),lty=3,col=rainbow(4)) #abline(lm(log10(R2.alea.predict(N,Pmin,Qmax)/mean)[-1]~log10(Pmin)[-1],data=R2_obs.tab.PQ)) legend("topright",legend = (1:4)*5,fill=rainbow(4),cex = 0.5) #summary(lm(log10(R2.alea.predict(N,Pmin,Qmax)/mean)[-1]~log10(Pmin)[-1],data=R2_obs.tab.PQ))  {r} with(data = R2_obs.tab.PQ, plot(Pmin, mean, col=rainbow(4)[N/5], pch=(1+(Qmax==Pmin)), cex=0.5, log="x") ) abline(h=1) legend("topright",legend = (1:4)*5,fill=rainbow(4),cex = 0.5)   Eric Coissac committed Jan 24, 2019 412