Guest User

Untitled

a guest
Apr 25th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. P1 <- data.frame(split(rnorm(5*16, 1, 1), 1:16))
  2. M1 <- matrix(unlist(P1[1,]), nrow = 4)
  3. M1[upper.tri(M1)] <- t(M1)[upper.tri(M1)]
  4.  
  5. P2 <- data.frame(split(rnorm(5*16, 1, 1), 1:16))
  6. M2 <- matrix(unlist(P2[1,]), nrow = 4)
  7. M2[upper.tri(M2)] <- t(M2)[upper.tri(M2)]
  8.  
  9. B <- rnorm(4, 0, 1)
  10.  
  11. R1 <- M1 %*% B
  12. R2 <- M2 %*% B
  13. cor(R1, R2)
  14.  
  15. com_rsk_p2 <- function(m1, m2, n = 2){
  16. nitt <- length(m1[,1])
  17. k <- sqrt(length(m1))
  18. B <- split(rnorm(n*k, 0, 1), 1:n)
  19.  
  20. rv_cor <- split(rep(NA, times = n*nitt), 1:nitt)
  21.  
  22. for(i in 1:nitt){
  23. R1 <- sapply(B, function(x) x %*% matrix(unlist(m1[i,]), ncol = k))
  24. R2 <- sapply(B, function(x) x %*% matrix(unlist(m2[i,]), ncol = k))
  25.  
  26. rv_cor[[i]] <- diag(matrix(mapply(cor, list(R1), list(R2)), ncol = n))
  27. }
  28.  
  29. return(t(data.frame(rv_cor)))
  30. }
  31.  
  32. > out <- com_rsk_p2(P1, P2)
  33. > out
  34. [,1] [,2]
  35. X1 0.7622732 0.8156658
  36. X2 0.4414054 0.4266134
  37. X3 0.4388098 -0.1248999
  38. X4 0.5438046 0.7723585
  39. X5 -0.5833943 -0.5294521
Add Comment
Please, Sign In to add comment