git-svn-id: file:///home/git/hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/multtest@68484 bc3139a8-67e5-0310-9ffc-ced21a209358
... | ... |
@@ -62,10 +62,10 @@ corr.null <- function(X,W=NULL,Y=NULL,Z=NULL,test="t.twosamp.unequalvar",alterna |
62 | 62 |
# Regression ICs written to automatically incorporate weights. |
63 | 63 |
# If W=NULL, then give equal weights. |
64 | 64 |
if(test=="lm.XvsZ"){ |
65 |
- if(is.null(Z)) Z <- matrix(1,nr=n,nc=1) |
|
65 |
+ if(is.null(Z)) Z <- matrix(1,nrow=n,ncol=1) |
|
66 | 66 |
else Z <- cbind(Z,1) |
67 |
- if(is.null(W)) W <- matrix(1/n,nr=p,nc=n) |
|
68 |
- IC.i <- matrix(0,nr=m,nc=n) |
|
67 |
+ if(is.null(W)) W <- matrix(1/n,nrow=p,ncol=n) |
|
68 |
+ IC.i <- matrix(0,nrow=m,ncol=n) |
|
69 | 69 |
for(i in 1:m){ |
70 | 70 |
drop <- is.na(X[i,]) | is.na(rowSums(Z)) | is.na(W[i,]) |
71 | 71 |
x <- as.numeric(X[i,!drop]) |
... | ... |
@@ -89,8 +89,8 @@ corr.null <- function(X,W=NULL,Y=NULL,Z=NULL,test="t.twosamp.unequalvar",alterna |
89 | 89 |
if(length(Y)!=n) stop(paste("Dimension of outcome Y=",length(Y),", not equal dimension of data=",n,sep="")) |
90 | 90 |
if(is.null(Z)) Z <- matrix(1,n,1) |
91 | 91 |
else Z <- cbind(Z,1) |
92 |
- if(is.null(W)) W <- matrix(1,nr=p,nc=n) |
|
93 |
- IC.i <- matrix(0,nr=m,nc=n) |
|
92 |
+ if(is.null(W)) W <- matrix(1,nrow=p,ncol=n) |
|
93 |
+ IC.i <- matrix(0,nrow=m,ncol=n) |
|
94 | 94 |
for(i in 1:m){ |
95 | 95 |
drop <- is.na(X[i,]) | is.na(rowSums(Z)) | is.na(W[i,]) |
96 | 96 |
x <- as.numeric(X[i,!drop]) |
... | ... |
@@ -139,13 +139,13 @@ corr.null <- function(X,W=NULL,Y=NULL,Z=NULL,test="t.twosamp.unequalvar",alterna |
139 | 139 |
EX1X2.v <- rowMeans(X.vec12,na.rm=TRUE) |
140 | 140 |
|
141 | 141 |
cons <- 1/sqrt(Var1.v*Var2.v) |
142 |
- gradient <- matrix(1,nr=M,nc=5) |
|
142 |
+ gradient <- matrix(1,nrow=M,ncol=5) |
|
143 | 143 |
gradient[,1] <- EX1.v*Cov.v/Var1.v - EX2.v |
144 | 144 |
gradient[,2] <- EX2.v*Cov.v/Var2.v - EX1.v |
145 | 145 |
gradient[,3] <- -0.5*Cov.v/Var1.v |
146 | 146 |
gradient[,4] <- -0.5*Cov.v/Var2.v |
147 | 147 |
|
148 |
- IC.i <- matrix(0, nr=M, nc=N) |
|
148 |
+ IC.i <- matrix(0, nrow=M, ncol=N) |
|
149 | 149 |
for(i in 1:N){ |
150 | 150 |
diffs.i <- diffs.1.N(X[ind[,1],i], X[ind[,2],i], EX1.v, EX2.v, E2X1.v, E2X2.v, EX1X2.v) |
151 | 151 |
IC.M <- rep(0,M) |
... | ... |
@@ -164,17 +164,17 @@ corr.null <- function(X,W=NULL,Y=NULL,Z=NULL,test="t.twosamp.unequalvar",alterna |
164 | 164 |
if(MVN.method=="mvrnorm") nulldist <- t(mvrnorm(n=B,mu=rep(0,dim(IC.Cor)[1]),Sigma=IC.Cor)) |
165 | 165 |
if(MVN.method=="Cholesky"){ |
166 | 166 |
IC.chol <- t(chol(IC.Cor+penalty*diag(dim(IC.Cor)[1]))) |
167 |
- norms <- matrix(rnorm(B*dim(IC.Cor)[1]),nr=dim(IC.Cor)[1],nc=B) |
|
167 |
+ norms <- matrix(rnorm(B*dim(IC.Cor)[1]),nrow=dim(IC.Cor)[1],ncol=B) |
|
168 | 168 |
nulldist <- IC.chol%*%norms |
169 | 169 |
} |
170 | 170 |
if(ic.quant.trans==TRUE){ |
171 | 171 |
cat("applying quantile transform...", "\n\n") |
172 | 172 |
if(is.null(marg.null)){ |
173 | 173 |
marg.null <- "t" |
174 |
- if(test=="t.cor" | test=="z.cor" | test=="t.twosamp.equalvar") marg.par <- matrix(rep(dim(X)[2]-2,dim(IC.Cor)[1]),nr=dim(IC.Cor)[1],nc=1) |
|
175 |
- if(test=="lm.XvsZ") marg.par <- matrix(rep(dim(X)[2]-dim(Z)[2],dim(IC.Cor)[1]),nr=dim(IC.Cor)[1],nc=1) |
|
176 |
- if(test=="lm.YvsXZ") marg.par <- matrix(rep(dim(X)[2]-dim(Z)[2]-1,dim(IC.Cor)[1]),nr=dim(IC.Cor)[1],nc=1) |
|
177 |
- else marg.par <- matrix(rep(dim(X)[2]-1,dim(IC.Cor)[1]),nr=dim(IC.Cor)[1],nc=1) |
|
174 |
+ if(test=="t.cor" | test=="z.cor" | test=="t.twosamp.equalvar") marg.par <- matrix(rep(dim(X)[2]-2,dim(IC.Cor)[1]),nrow=dim(IC.Cor)[1],ncol=1) |
|
175 |
+ if(test=="lm.XvsZ") marg.par <- matrix(rep(dim(X)[2]-dim(Z)[2],dim(IC.Cor)[1]),nrow=dim(IC.Cor)[1],ncol=1) |
|
176 |
+ if(test=="lm.YvsXZ") marg.par <- matrix(rep(dim(X)[2]-dim(Z)[2]-1,dim(IC.Cor)[1]),nrow=dim(IC.Cor)[1],ncol=1) |
|
177 |
+ else marg.par <- matrix(rep(dim(X)[2]-1,dim(IC.Cor)[1]),nrow=dim(IC.Cor)[1],ncol=1) |
|
178 | 178 |
} |
179 | 179 |
if(test=="z.cor" & marg.null=="t") warning("IC nulldist for z.cor already MVN. Transforming to N-2 df t marginal distribution not advised.") |
180 | 180 |
if(marg.null!="t" & marg.null!="perm") stop("IC nulldists can only be quantile transformed to a marginal t-distribution or user-supplied marginal permutation distribution") |
... | ... |
@@ -193,11 +193,11 @@ IC.Cor.NA <- function(IC,W,N,M,output){ |
193 | 193 |
n <- dim(IC)[2] |
194 | 194 |
m <- dim(IC)[1] |
195 | 195 |
if(is.null(W)){ |
196 |
- W <- matrix(1,nr=dim(IC)[1],nc=dim(IC)[2]) |
|
196 |
+ W <- matrix(1,nrow=dim(IC)[1],ncol=dim(IC)[2]) |
|
197 | 197 |
Wnew <- W/rowSums(W,na.rm=TRUE) # Equal weight, NA handling. |
198 | 198 |
} |
199 | 199 |
else Wnew <- W/rowSums(W,na.rm=TRUE) |
200 |
- IC.VC <- matrix(0,nr=m,nc=m) |
|
200 |
+ IC.VC <- matrix(0,nrow=m,ncol=m) |
|
201 | 201 |
for(i in 1:n){ |
202 | 202 |
temp <- crossprod(t(sqrt(Wnew[,i])*IC[,i])) |
203 | 203 |
temp[is.na(temp)] <- 0 |
... | ... |
@@ -222,7 +222,7 @@ IC.CorXW.NA <- function(X,W,N,M,output){ |
222 | 222 |
EXW <- rowSums(XW)/rowSums(W) |
223 | 223 |
ICW.i <- X-EXW |
224 | 224 |
Wnew <- W/rowSums(W,na.rm=T) |
225 |
- IC.VC <- matrix(0,nr=m,nc=m) |
|
225 |
+ IC.VC <- matrix(0,nrow=m,ncol=m) |
|
226 | 226 |
for(i in 1:n){ |
227 | 227 |
temp <- crossprod(t(sqrt(Wnew[,i])*X[,i])) |
228 | 228 |
temp[is.na(temp)] <- 0 |
... | ... |
@@ -246,7 +246,7 @@ insert.NA <- function(orig.NA, res.vec){ |
246 | 246 |
# This is the difference between estimates for |
247 | 247 |
# a sample size of one and a sample of size n. |
248 | 248 |
diffs.1.N <- function(vec1, vec2, e1, e2, e21, e22, e12){ |
249 |
- diff.mat.1.N <- matrix(0,nr=5,nc=length(vec1)) |
|
249 |
+ diff.mat.1.N <- matrix(0,nrow=5,ncol=length(vec1)) |
|
250 | 250 |
diff.mat.1.N[1,] <- vec1 - e1 |
251 | 251 |
diff.mat.1.N[2,] <- vec2 - e2 |
252 | 252 |
diff.mat.1.N[3,] <- vec1*vec1 - e21 |
git-svn-id: file:///home/git/hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/multtest@38657 bc3139a8-67e5-0310-9ffc-ced21a209358
... | ... |
@@ -178,8 +178,8 @@ corr.null <- function(X,W=NULL,Y=NULL,Z=NULL,test="t.twosamp.unequalvar",alterna |
178 | 178 |
} |
179 | 179 |
if(test=="z.cor" & marg.null=="t") warning("IC nulldist for z.cor already MVN. Transforming to N-2 df t marginal distribution not advised.") |
180 | 180 |
if(marg.null!="t" & marg.null!="perm") stop("IC nulldists can only be quantile transformed to a marginal t-distribution or user-supplied marginal permutation distribution") |
181 |
- if(marg.null=="t") nulldist <- t.quant.trans(nulldist,marg.null="t",marg.par,ncp=0,perm.mat=NULL) |
|
182 |
- if(marg.null=="perm") nulldist <- t.quant.trans(nulldist,marg.null="perm",marg.par=NULL,ncp=NULL,perm.mat=perm.mat) |
|
181 |
+ if(marg.null=="t") nulldist <- tQuantTrans(nulldist,marg.null="t",marg.par,ncp=0,perm.mat=NULL) |
|
182 |
+ if(marg.null=="perm") nulldist <- tQuantTrans(nulldist,marg.null="perm",marg.par=NULL,ncp=NULL,perm.mat=perm.mat) |
|
183 | 183 |
} |
184 | 184 |
if(alternative=="greater") nulldist <- nulldist |
185 | 185 |
else if(alternative=="less") nulldist <- -nulldist |
... | ... |
@@ -267,7 +267,7 @@ out |
267 | 267 |
} |
268 | 268 |
|
269 | 269 |
### Quantile transform streamlined for IC nulldists. |
270 |
-t.quant.trans <- function(rawboot, marg.null, marg.par, ncp, perm.mat=NULL){ |
|
270 |
+tQuantTrans <- function(rawboot, marg.null, marg.par, ncp, perm.mat=NULL){ |
|
271 | 271 |
m <- dim(rawboot)[1] |
272 | 272 |
B <- dim(rawboot)[2] |
273 | 273 |
ranks <- t(apply(rawboot,1,rank,ties.method="random")) |
git-svn-id: file:///home/git/hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/multtest@38574 bc3139a8-67e5-0310-9ffc-ced21a209358
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,321 @@ |
1 |
+ |
|
2 |
+# No robust correlation test statistics. |
|
3 |
+# Want to return a 3 by M matrix of observations. |
|
4 |
+corr.Tn <- function(X,test,alternative,use="pairwise"){ |
|
5 |
+ P <- dim(X)[1] |
|
6 |
+ M <- P*(P-1)/2 |
|
7 |
+ N <- dim(X)[2] |
|
8 |
+ VCM <- cov(t(X),use=use) |
|
9 |
+ Cor <- cov2cor(VCM) |
|
10 |
+ Cov.v <- VCM[lower.tri(VCM)] # vectorize. |
|
11 |
+ Cor.v <- Cor[lower.tri(Cor)] # vectorize. |
|
12 |
+ if(test=="t.cor") num <- sqrt(N-2)*Cor.v/sqrt(1-Cor.v^2) |
|
13 |
+ if(test=="z.cor") num <- sqrt(N-3)*0.5*log((1+Cor.v)/(1-Cor.v)) |
|
14 |
+ denom <- 1 |
|
15 |
+ if(alternative=="two.sided"){ |
|
16 |
+ snum<-sign(num) |
|
17 |
+ num<-abs(num) |
|
18 |
+ } |
|
19 |
+ else { |
|
20 |
+ if(alternative=="less"){ |
|
21 |
+ snum<-(-1) |
|
22 |
+ num<-(-num) |
|
23 |
+ } |
|
24 |
+ else snum<-1 |
|
25 |
+ } |
|
26 |
+ rbind(num,denom,snum) |
|
27 |
+} |
|
28 |
+ |
|
29 |
+ic.tests <- c("t.onesamp","t.pair","t.twosamp.equalvar","t.twosamp.unequalvar","lm.XvsZ","lm.YvsXZ","t.cor","z.cor") |
|
30 |
+ |
|
31 |
+corr.null <- function(X,W=NULL,Y=NULL,Z=NULL,test="t.twosamp.unequalvar",alternative="two-sided",use="pairwise",B=1000,MVN.method="mvrnorm",penalty=1e-6,ic.quant.trans=FALSE,marg.null=NULL,marg.par=NULL,perm.mat=NULL){ |
|
32 |
+ # Most sanity checks conducted already... |
|
33 |
+ p <- dim(X)[1] |
|
34 |
+ m <- dim(X)[1] |
|
35 |
+ n <- dim(X)[2] |
|
36 |
+ cat("calculating vector influence curve...", "\n") |
|
37 |
+ |
|
38 |
+ if(test=="t.onesamp" | test=="t.pair"){ |
|
39 |
+ #t.pair sanity checks and formatting done in stat.closure section |
|
40 |
+ #in test.R |
|
41 |
+ if(is.null(W)) IC.Cor <- cor(t(X),use=use) |
|
42 |
+ else IC.Cor <- IC.CorXW.NA(X,W,N=n,M=p,output="cor") |
|
43 |
+ } |
|
44 |
+ |
|
45 |
+ if(test=="t.twosamp.equalvar" | test=="t.twosamp.unequalvar"){ |
|
46 |
+ uY<-sort(unique(Y)) |
|
47 |
+ if(length(uY)!=2) stop("Must have two class labels for this test") |
|
48 |
+ n1 <- sum(Y==uY[1]) |
|
49 |
+ n2 <- sum(Y==uY[2]) |
|
50 |
+ if(is.null(W)){ |
|
51 |
+ cov1 <- cov(t(X[,Y==uY[1]]),use=use) |
|
52 |
+ cov2 <- cov(t(X[,Y==uY[2]]),use=use) |
|
53 |
+ } |
|
54 |
+ else{ |
|
55 |
+ cov1 <- IC.CorXW.NA(X[,Y==uY[1]],W[,Y==uY[1]],N=n1,M=p,output="cov") |
|
56 |
+ cov2 <- IC.CorXW.NA(X[,Y==uY[2]],W[,Y==uY[2]],N=n2,M=p,output="cov") |
|
57 |
+ } |
|
58 |
+ newcov <- cov1/n1 + cov2/n2 |
|
59 |
+ IC.Cor <- cov2cor(newcov) |
|
60 |
+ } |
|
61 |
+ |
|
62 |
+ # Regression ICs written to automatically incorporate weights. |
|
63 |
+ # If W=NULL, then give equal weights. |
|
64 |
+ if(test=="lm.XvsZ"){ |
|
65 |
+ if(is.null(Z)) Z <- matrix(1,nr=n,nc=1) |
|
66 |
+ else Z <- cbind(Z,1) |
|
67 |
+ if(is.null(W)) W <- matrix(1/n,nr=p,nc=n) |
|
68 |
+ IC.i <- matrix(0,nr=m,nc=n) |
|
69 |
+ for(i in 1:m){ |
|
70 |
+ drop <- is.na(X[i,]) | is.na(rowSums(Z)) | is.na(W[i,]) |
|
71 |
+ x <- as.numeric(X[i,!drop]) |
|
72 |
+ z <- Z[!drop,] |
|
73 |
+ w <- W[i,!drop] |
|
74 |
+ nn <- n-sum(drop) |
|
75 |
+ EXtWXinv <- solve(t(z)%*%(w*diag(nn))%*%z)*sum(w) |
|
76 |
+ res.m <- lm.wfit(z,x,w)$res |
|
77 |
+ if(sum(drop)>0) res.m <- insert.NA(which(drop==TRUE),res.m) |
|
78 |
+ EXtWXinvXt <- rep(0,n) |
|
79 |
+ for(j in 1:n){ |
|
80 |
+ EXtWXinvXt[j] <- (EXtWXinv%*%(t(Z)[,j]))[1] |
|
81 |
+ } |
|
82 |
+ IC.i[i,] <- res.m * EXtWXinvXt |
|
83 |
+ } |
|
84 |
+ IC.Cor <- IC.Cor.NA(IC.i,W,N=n,M=p,output="cor") |
|
85 |
+ } |
|
86 |
+ |
|
87 |
+ if(test=="lm.YvsXZ"){ |
|
88 |
+ if(is.null(Y)) stop("An outcome variable is needed for this test") |
|
89 |
+ if(length(Y)!=n) stop(paste("Dimension of outcome Y=",length(Y),", not equal dimension of data=",n,sep="")) |
|
90 |
+ if(is.null(Z)) Z <- matrix(1,n,1) |
|
91 |
+ else Z <- cbind(Z,1) |
|
92 |
+ if(is.null(W)) W <- matrix(1,nr=p,nc=n) |
|
93 |
+ IC.i <- matrix(0,nr=m,nc=n) |
|
94 |
+ for(i in 1:m){ |
|
95 |
+ drop <- is.na(X[i,]) | is.na(rowSums(Z)) | is.na(W[i,]) |
|
96 |
+ x <- as.numeric(X[i,!drop]) |
|
97 |
+ z <- Z[!drop,] |
|
98 |
+ w <- W[i,!drop] |
|
99 |
+ y <- Y[!drop] |
|
100 |
+ nn <- n-sum(drop) |
|
101 |
+ xz <- cbind(x,z) |
|
102 |
+ XZ <- cbind(X[i,],Z) |
|
103 |
+ EXtWXinv <- solve(t(xz)%*%(w*diag(nn))%*%xz)*sum(w) |
|
104 |
+ res.m <- lm.wfit(xz,y,w)$res |
|
105 |
+ if(sum(drop)>0) res.m <- insert.NA(which(drop==TRUE),res.m) |
|
106 |
+ EXtWXinvXt <- rep(0,n) |
|
107 |
+ for(j in 1:n){ |
|
108 |
+ EXtWXinvXt[j] <- (EXtWXinv%*%(t(XZ)[,j]))[1] |
|
109 |
+ } |
|
110 |
+ IC.i[i,] <- res.m * EXtWXinvXt |
|
111 |
+ } |
|
112 |
+ IC.Cor <- IC.Cor.NA(IC.i,W,N=n,M=p,output="cor") |
|
113 |
+ } |
|
114 |
+ |
|
115 |
+ if(test=="t.cor" | test=="z.cor"){ |
|
116 |
+ if(!is.null(W)) warning("Weights not currently implemented for tests of correlation parameters. Proceeding with unweighted version") |
|
117 |
+ # Change of dimension |
|
118 |
+ P <- dim(X)[1] -> p # Number of variables. |
|
119 |
+ M <- P*(P-1)/2 -> m # Actual number of pairwise hypotheses. |
|
120 |
+ N <- dim(X)[2] -> m |
|
121 |
+ ind <- t(combn(P,2)) |
|
122 |
+ VCM <- cov(t(X),use="pairwise") |
|
123 |
+ Cor <- cov2cor(VCM) |
|
124 |
+ Vars <- diag(VCM) |
|
125 |
+ Cov.v <- VCM[lower.tri(VCM)] # vectorize. |
|
126 |
+ Cor.v <- Cor[lower.tri(Cor)] # vectorize. |
|
127 |
+ X2 <- X*X |
|
128 |
+ EX <- rowMeans(X,na.rm=TRUE) |
|
129 |
+ E2X <- rowMeans(X2,na.rm=TRUE) |
|
130 |
+ Var1.v <- Vars[ind[,1]] |
|
131 |
+ Var2.v <- Vars[ind[,2]] |
|
132 |
+ EX1.v <- EX[ind[,1]] |
|
133 |
+ EX2.v <- EX[ind[,2]] |
|
134 |
+ E2X1.v <- E2X[ind[,1]] |
|
135 |
+ E2X2.v <- E2X[ind[,2]] |
|
136 |
+ X.vec1 <- X[ind[,1],] |
|
137 |
+ X.vec2 <- X[ind[,2],] |
|
138 |
+ X.vec12 <- X.vec1*X.vec2 |
|
139 |
+ EX1X2.v <- rowMeans(X.vec12,na.rm=TRUE) |
|
140 |
+ |
|
141 |
+ cons <- 1/sqrt(Var1.v*Var2.v) |
|
142 |
+ gradient <- matrix(1,nr=M,nc=5) |
|
143 |
+ gradient[,1] <- EX1.v*Cov.v/Var1.v - EX2.v |
|
144 |
+ gradient[,2] <- EX2.v*Cov.v/Var2.v - EX1.v |
|
145 |
+ gradient[,3] <- -0.5*Cov.v/Var1.v |
|
146 |
+ gradient[,4] <- -0.5*Cov.v/Var2.v |
|
147 |
+ |
|
148 |
+ IC.i <- matrix(0, nr=M, nc=N) |
|
149 |
+ for(i in 1:N){ |
|
150 |
+ diffs.i <- diffs.1.N(X[ind[,1],i], X[ind[,2],i], EX1.v, EX2.v, E2X1.v, E2X2.v, EX1X2.v) |
|
151 |
+ IC.M <- rep(0,M) |
|
152 |
+ for(j in 1:M){ |
|
153 |
+ IC.M[j] <- gradient[j,]%*%diffs.i[,j] |
|
154 |
+ } |
|
155 |
+ IC.i[,i] <- IC.M |
|
156 |
+ } |
|
157 |
+ IC.i <- cons * IC.i |
|
158 |
+ IC.Cor <- IC.Cor.NA(IC.i,W=NULL,N=n,M=M,output="cor") |
|
159 |
+ } |
|
160 |
+ |
|
161 |
+ if(ic.quant.trans==FALSE) cat("sampling null test statistics...", "\n\n") |
|
162 |
+ else cat("sampling null test statistics...", "\n") |
|
163 |
+ |
|
164 |
+ if(MVN.method=="mvrnorm") nulldist <- t(mvrnorm(n=B,mu=rep(0,dim(IC.Cor)[1]),Sigma=IC.Cor)) |
|
165 |
+ if(MVN.method=="Cholesky"){ |
|
166 |
+ IC.chol <- t(chol(IC.Cor+penalty*diag(dim(IC.Cor)[1]))) |
|
167 |
+ norms <- matrix(rnorm(B*dim(IC.Cor)[1]),nr=dim(IC.Cor)[1],nc=B) |
|
168 |
+ nulldist <- IC.chol%*%norms |
|
169 |
+ } |
|
170 |
+ if(ic.quant.trans==TRUE){ |
|
171 |
+ cat("applying quantile transform...", "\n\n") |
|
172 |
+ if(is.null(marg.null)){ |
|
173 |
+ marg.null <- "t" |
|
174 |
+ if(test=="t.cor" | test=="z.cor" | test=="t.twosamp.equalvar") marg.par <- matrix(rep(dim(X)[2]-2,dim(IC.Cor)[1]),nr=dim(IC.Cor)[1],nc=1) |
|
175 |
+ if(test=="lm.XvsZ") marg.par <- matrix(rep(dim(X)[2]-dim(Z)[2],dim(IC.Cor)[1]),nr=dim(IC.Cor)[1],nc=1) |
|
176 |
+ if(test=="lm.YvsXZ") marg.par <- matrix(rep(dim(X)[2]-dim(Z)[2]-1,dim(IC.Cor)[1]),nr=dim(IC.Cor)[1],nc=1) |
|
177 |
+ else marg.par <- matrix(rep(dim(X)[2]-1,dim(IC.Cor)[1]),nr=dim(IC.Cor)[1],nc=1) |
|
178 |
+ } |
|
179 |
+ if(test=="z.cor" & marg.null=="t") warning("IC nulldist for z.cor already MVN. Transforming to N-2 df t marginal distribution not advised.") |
|
180 |
+ if(marg.null!="t" & marg.null!="perm") stop("IC nulldists can only be quantile transformed to a marginal t-distribution or user-supplied marginal permutation distribution") |
|
181 |
+ if(marg.null=="t") nulldist <- t.quant.trans(nulldist,marg.null="t",marg.par,ncp=0,perm.mat=NULL) |
|
182 |
+ if(marg.null=="perm") nulldist <- t.quant.trans(nulldist,marg.null="perm",marg.par=NULL,ncp=NULL,perm.mat=perm.mat) |
|
183 |
+ } |
|
184 |
+ if(alternative=="greater") nulldist <- nulldist |
|
185 |
+ else if(alternative=="less") nulldist <- -nulldist |
|
186 |
+ else nulldist <- abs(nulldist) |
|
187 |
+ nulldist |
|
188 |
+} |
|
189 |
+ |
|
190 |
+# Function, given ICs for each individual, returns variance covariance |
|
191 |
+# matrix or corresponding correlation matrix. |
|
192 |
+IC.Cor.NA <- function(IC,W,N,M,output){ |
|
193 |
+ n <- dim(IC)[2] |
|
194 |
+ m <- dim(IC)[1] |
|
195 |
+ if(is.null(W)){ |
|
196 |
+ W <- matrix(1,nr=dim(IC)[1],nc=dim(IC)[2]) |
|
197 |
+ Wnew <- W/rowSums(W,na.rm=TRUE) # Equal weight, NA handling. |
|
198 |
+ } |
|
199 |
+ else Wnew <- W/rowSums(W,na.rm=TRUE) |
|
200 |
+ IC.VC <- matrix(0,nr=m,nc=m) |
|
201 |
+ for(i in 1:n){ |
|
202 |
+ temp <- crossprod(t(sqrt(Wnew[,i])*IC[,i])) |
|
203 |
+ temp[is.na(temp)] <- 0 |
|
204 |
+ IC.VC <- IC.VC + temp |
|
205 |
+ } |
|
206 |
+ if(output=="cov") out <- IC.VC |
|
207 |
+ if(output=="cor") out <- cov2cor(IC.VC) |
|
208 |
+ out |
|
209 |
+} |
|
210 |
+ |
|
211 |
+# Weighted correlation. Generalizes cov.wt() to account for a matrix |
|
212 |
+# of weights. Uses IC formulation instead of sweep() and crossprod(). |
|
213 |
+# May be slower/clunkier, but pretty transparent, and allows for NA |
|
214 |
+# handling much like cor(...,use="pairwise") would. That is, each |
|
215 |
+# element of the correlation matrix returned uses the maximum amount |
|
216 |
+# of information possible in obtaining individual elements of that |
|
217 |
+# matrix. |
|
218 |
+IC.CorXW.NA <- function(X,W,N,M,output){ |
|
219 |
+ n <- dim(X)[2] |
|
220 |
+ m <- dim(X)[1] |
|
221 |
+ XW <- X*W |
|
222 |
+ EXW <- rowSums(XW)/rowSums(W) |
|
223 |
+ ICW.i <- X-EXW |
|
224 |
+ Wnew <- W/rowSums(W,na.rm=T) |
|
225 |
+ IC.VC <- matrix(0,nr=m,nc=m) |
|
226 |
+ for(i in 1:n){ |
|
227 |
+ temp <- crossprod(t(sqrt(Wnew[,i])*X[,i])) |
|
228 |
+ temp[is.na(temp)] <- 0 |
|
229 |
+ IC.VC <- IC.VC + temp |
|
230 |
+ } |
|
231 |
+ if(output=="cov") out <- IC.VC |
|
232 |
+ if(output=="cor") out <- cov2cor(IC.VC) |
|
233 |
+ out |
|
234 |
+} |
|
235 |
+ |
|
236 |
+# For regression ICs, a function to insert NAs into appropriate locations |
|
237 |
+# of a vector of returned residuals. |
|
238 |
+insert.NA <- function(orig.NA, res.vec){ |
|
239 |
+ for(i in 1:length(orig.NA)){ |
|
240 |
+ res.vec <- append(res.vec, NA, after=orig.NA[i]-1) |
|
241 |
+ } |
|
242 |
+ res.vec |
|
243 |
+} |
|
244 |
+ |
|
245 |
+# For correlation ICS, a function to get diff vectors for all M. |
|
246 |
+# This is the difference between estimates for |
|
247 |
+# a sample size of one and a sample of size n. |
|
248 |
+diffs.1.N <- function(vec1, vec2, e1, e2, e21, e22, e12){ |
|
249 |
+ diff.mat.1.N <- matrix(0,nr=5,nc=length(vec1)) |
|
250 |
+ diff.mat.1.N[1,] <- vec1 - e1 |
|
251 |
+ diff.mat.1.N[2,] <- vec2 - e2 |
|
252 |
+ diff.mat.1.N[3,] <- vec1*vec1 - e21 |
|
253 |
+ diff.mat.1.N[4,] <- vec2*vec2 - e22 |
|
254 |
+ diff.mat.1.N[5,] <- vec1*vec2 - e12 |
|
255 |
+ diff.mat.1.N |
|
256 |
+} |
|
257 |
+ |
|
258 |
+### For quantile transform, take a sample from the marginal null distribution. |
|
259 |
+marg.samp <- function(marg.null,marg.par,m,B,ncp){ |
|
260 |
+out <- matrix(0,m,B) |
|
261 |
+for(i in 1:m){ |
|
262 |
+ if(marg.null=="normal") out[i,] <- rnorm(B,mean=marg.par[i,1],sd=marg.par[i,2]) |
|
263 |
+ if(marg.null=="t") out[i,] <- rt(B,df=marg.par[i,1],ncp) |
|
264 |
+ if(marg.null=="f") out[i,] <- rf(B,df1=marg.par[i,1],df2=marg.par[i,2],ncp) |
|
265 |
+} |
|
266 |
+out |
|
267 |
+} |
|
268 |
+ |
|
269 |
+### Quantile transform streamlined for IC nulldists. |
|
270 |
+t.quant.trans <- function(rawboot, marg.null, marg.par, ncp, perm.mat=NULL){ |
|
271 |
+ m <- dim(rawboot)[1] |
|
272 |
+ B <- dim(rawboot)[2] |
|
273 |
+ ranks <- t(apply(rawboot,1,rank,ties.method="random")) |
|
274 |
+ if(marg.null=="t") Z.quant <- marg.samp(marg.null="t",marg.par,m,B,ncp) |
|
275 |
+ if(marg.null=="perm") Z.quant <- perm.mat |
|
276 |
+ Z.quant <- t(apply(Z.quant,1,sort)) |
|
277 |
+ if(marg.null!="perm"){ |
|
278 |
+ for(i in 1:m){ |
|
279 |
+ Z.quant[i,] <- Z.quant[i,][ranks[i,]] |
|
280 |
+ } |
|
281 |
+ } |
|
282 |
+ else{ |
|
283 |
+ Z.quant <- t(apply(Z.quant,1,quantile,probs=seq(0,1,length.out=B),na.rm=TRUE)) |
|
284 |
+ for(i in 1:m){ |
|
285 |
+ Z.quant[i,] <- Z.quant[i,][ranks[i,]] |
|
286 |
+ } |
|
287 |
+ } |
|
288 |
+ Z.quant |
|
289 |
+} |
|
290 |
+ |
|
291 |
+### Effective df for two sample test of means, unequal var. |
|
292 |
+t.effective.df <- function(X,Y){ |
|
293 |
+ uY<-sort(unique(Y)) |
|
294 |
+ X1 <- X[Y==uY[1]] |
|
295 |
+ X2 <- X[Y==uY[2]] |
|
296 |
+ mu <- var(X2)/var(X1) |
|
297 |
+ n1 <- length(Y[Y==uY[1]]) |
|
298 |
+ n2 <- length(Y[Y==uY[2]]) |
|
299 |
+ df <- (((1/n1)+(mu/n2))^2)/(1/((n1^2)*(n1-1)) + (mu^2)/((n2^2)*(n2-1))) |
|
300 |
+ df |
|
301 |
+} |
|
302 |
+ |
|
303 |
+ |
|
304 |
+ |
|
305 |
+ |
|
306 |
+ |
|
307 |
+ |
|
308 |
+ |
|
309 |
+ |
|
310 |
+ |
|
311 |
+ |
|
312 |
+ |
|
313 |
+ |
|
314 |
+ |
|
315 |
+ |
|
316 |
+ |
|
317 |
+ |
|
318 |
+ |
|
319 |
+ |
|
320 |
+ |
|
321 |
+ |