mcov.R 5.75 KB
 1 #' @include procmod.frame.R  Eric Coissac committed Jul 09, 2018 2 #' @include multivariate.R  3 #'  Eric Coissac committed Jan 24, 2019 4 5 6 7 #' @import expm #' #' @author Christelle Gonindard-Melodelima #' @author Eric Coissac  8 9 NULL  Eric Coissac committed Jan 24, 2019 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #' Compute the trace of a square matrix. #' #' #' @param X a square matrix #' @return the trace of X #' #' @examples #' m = matrix(1:16,nrow=4) #' ProcMod:::.Trace(m) #' #' @note Internal function do not use. #' #' @rdname internal.getPermuteMatrix #' @author Eric Coissac #' @author Christelle Gonindard-Melodelima #' .Trace = function(X) sum(diag(X))  28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 #' Compute the variance, covariance matrix of K coordinate matrices. #' #' Covariance between two matrices is defined as the sum of the #' sigular values of the X'Y matrix. All the matrices must have #' the same number of rows. #' #' @param ... the set of matrices #' #' @examples #' # Build Three matrices of 3 rows. #' A <- matrix(1:9,nrow=3) #' B <- matrix(10:15,nrow=3) #' C <- matrix(20:31,nrow=3) #' # compute the variance covariance matrix #' mvar(A,B,C) #' mvar(A=A,B=B,C=C) #'  Eric Coissac committed Jan 24, 2019 45 46 #' @author Eric Coissac #' @author Christelle Gonindard-Melodelima  47 #' @export  Eric Coissac committed Jan 24, 2019 48 varls = function(...,permutations = how(nperm = 999)) {  49 50  Xs <- list(...)  Eric Coissac committed Jun 19, 2018 51  if (length(Xs)==1) {  Eric Coissac committed Jun 22, 2018 52 53 54 55 56  x = Xs[[1]] if (is.procmod.frame(x)) Xs=x else if (is.pm(x)) return(x$cov)  57  else  Eric Coissac committed Jun 22, 2018 58 59  Xs=procmod.frame(x) }  Eric Coissac committed Jun 19, 2018 60  else  61 62  Xs=as.procmod.frame(Xs)  Eric Coissac committed Jan 24, 2019 63  Xnames=names(Xs)  64   Eric Coissac committed Jan 24, 2019 65  Xs <- ortho(Xs)  66   Eric Coissac committed Jan 24, 2019 67 68  XXs = as.procmod.frame(mapply(tcrossprod, Xs, SIMPLIFY = FALSE))  69   Eric Coissac committed Jan 24, 2019 70 71  nX = length(Xs) N = nrow(Xs)-1  72   Eric Coissac committed Jan 24, 2019 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105  if (! is.null(permutations)) { pmatrix = .getPermuteMatrix(perm=permutations,N=nrow(Xs)) rCovXXs = matrix(0, nrow = nX * 2, ncol = nX * 2) pval = matrix(0, nrow = nX * 2, ncol = nX * 2) for (i in 1:(2*nX)) for (j in 1:(2*nX)) { if (i %% 2 && j %% 2) { rCovXXs[i,j]=.Trace(sqrtm(XXs[[ceiling(i / 2)]] %*% XXs[[ceiling(j/2)]])) / N pval[i,j]=-1 } else if (i ==j) { vv = numeric(nrow(pmatrix)) for (k in seq_len(nrow(pmatrix))) { d = Xs[[ceiling(i / 2)]][pmatrix[k,],] dd = tcrossprod(d) vv[k] = Re(.Trace(sqrtm(dd %*% dd))) } rCovXXs[i,j]=mean(vv) / N pval[i,j]=shapiro.test(vv)$p.value } else if (i <=j) { vv = numeric(nrow(pmatrix)) for (k in seq_len(nrow(pmatrix))) { d = Xs[[ceiling(i / 2)]][pmatrix[k,],] dd = tcrossprod(d) vv[k] = Re(.Trace(sqrtm(dd %*% XXs[[ceiling(j/2)]]))) } rCovXXs[i,j]=mean(vv) / N rCovXXs[j,i]=rCovXXs[i,j] pval[i,j]=shapiro.test(vv)$p.value pval[j,i]=shapiro.test(vv)$p.value } }  106   Eric Coissac committed Jan 24, 2019 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124  CovXXs=rCovXXs Xnames = rep(Xnames,rep(2,length(Xnames))) Xsuff = rep("",length(Xnames)) Xsuff[seq(from=2,to=length(Xnames),by=2)]="r_" Xnames = mapply(paste0, Xsuff,Xnames,collapse="") colnames(CovXXs)=Xnames rownames(CovXXs)=Xnames colnames(pval)=Xnames rownames(pval)=Xnames print(pval) } else { Xx <- rep(1:nX,nX) Xy <- rep(1:nX,rep(nX,nX)) CovXXs <- mapply(function(x,y) .Trace(sqrtm(XXs[[x]] %*% XXs[[y]])), Xx,Xy, SIMPLIFY = TRUE) / N  125 126 127 128  dim(CovXXs)=c(nX,nX) colnames(CovXXs)=Xnames rownames(CovXXs)=Xnames  Eric Coissac committed Jan 24, 2019 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  } return(Re(CovXXs)) } #' Compute the variance, covariance matrix of K coordinate matrices. #' #' Covariance between two matrices is defined as the sum of the #' sigular values of the X'Y matrix. All the matrices must have #' the same number of rows. #' #' @param ... the set of matrices #' #' @examples #' # Build Three matrices of 3 rows. #' A <- matrix(1:9,nrow=3) #' B <- matrix(10:15,nrow=3) #' C <- matrix(20:31,nrow=3) #' # compute the variance covariance matrix #' varls2(A,B,C) #' varls2(A=A,B=B,C=C) #' #' @author Eric Coissac #' @author Christelle Gonindard-Melodelima #' @export varls2 = function(...,permutations = how(nperm = 999)) {  156   Eric Coissac committed Jan 24, 2019 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  Xs <- list(...) if (length(Xs)==1) { x = Xs[[1]] if (is.procmod.frame(x)) Xs=x else if (is.pm(x)) return(x\$cov) else Xs=procmod.frame(x) } else Xs=as.procmod.frame(Xs) Xnames=names(Xs) Xs <- ortho(Xs) XXs = as.procmod.frame(mapply(tcrossprod, Xs, SIMPLIFY = FALSE)) nX = length(Xs) N = nrow(Xs)-1 CovXXs = matrix(0, nrow = nX, ncol = nX) for (i in seq_len(nX)) for (j in i:nX) { CovXXs[i,j] = .Trace(sqrtm(XXs[[i]] %*% XXs[[j]])) } if (! is.null(permutations)) { pmatrix = .getPermuteMatrix(perm=permutations,N=nrow(Xs)) nP = nrow(pmatrix) rCovXXs = array(0,dim=c(nX,nX,nP)) for (k in seq_len(nP)) { Xp = Xs[pmatrix[k,],] for (i in seq_len(nX)) { dd = tcrossprod(Xp[[i]]) for (j in i:nX) rCovXXs[i,j,k] = Re(.Trace(sqrtm(dd %*% XXs[[j]]))) } } for (i in seq_len(nX)) for (j in i:nX) CovXXs[i,j] = CovXXs[i,j] - mean(rCovXXs[i,j,]) } for (i in seq_len(nX)) for (j in i:nX) CovXXs[j,i] = CovXXs[i,j] CovXXs = CovXXs / N colnames(CovXXs)=Xnames rownames(CovXXs)=Xnames return(Re(CovXXs))  217 218 }  Eric Coissac committed Jan 24, 2019 219   220 221 222 223 224 #' Compute the person correlation matrix of K coordinate matrices #' #' @author Eric Coissac #' @author Christelle Gonindard-Melodelima #' @export  Eric Coissac committed Jan 24, 2019 225 226 corls = function(...,permutations = how(nperm = 999)) { cov = varls(...,permutations = permutations)  227 228 229 230 231 232 233 234 235 236  s = sqrt(diag(cov)) vv= outer(s,s) return(cov/vv) } #' Compute the person partial correlation matrix of K coordinate matrices #' #' @author Eric Coissac #' @author Christelle Gonindard-Melodelima #' @export  Eric Coissac committed Jan 24, 2019 237 238 corls.partial = function(...,permutations = how(nperm = 999)) { C = solve(corls(...,permutations = permutations))  239 240 241 242 243  D = sqrt(diag(C) %o% diag(C)) return(C/D) }