| cp.test {corrperm} | R Documentation |
Three permutation tests of correlation useful when there are repeated measurements
cp.test(xx, yy, nrep = 1000, alt = 1)
xx |
data for variable xx in a matrix in which each row corresponds to a population and each column corresponds to an experiment |
yy |
data for variable yy arranged as for xx |
nrep |
number of Monte Carlo replicates for the permuatation test |
alt |
alt specifies the sign of the correlation for the alternative hypothesis: 1 if >0 and -1 if <0 |
The function cp.test tests the null hypothesis of no correlation of two variables (x and y) that are measured in repeated independent experiments. Each experiment measures either x or y on the same fixed number of populations. Inference is based on permutation tests. Three different statistics are used in the tests, which are one-sided, and are computed at the same time.
In the absence of ties, the Spearman correlation, r, equals 1 - 6 sum d^2/(n^3 - n) where d is the difference in ranks for a x,y pair, n is the number of populations (x,y pairs) and the summation is over all d. Under the null hypothesis the variance of r is 1/(n-1). The tests are based on the combining the Spearman correlations or p-values for all possible combinations of x and y experiments.
The inverse-variance weighted average Spearman correlation is: S = sum (njk - 1) rjk/(m sum (njk - 1)), where m is the total number of combinations of x and y experiments, njk is the number of x,y pairs and rjk is the correlation for the jth x and the kth y experiment, and the summation is over all possible pairs.
The unweighted Fisher's combining function is: F = -2 sum ln(pjk) , where pjk is the p-value of the Spearman test for the jth x and the kth y experiment.
The weighted Fisher's combining function is: Fw = -2 sum (njk - 1)log(pjk)/sum (njk - 1).
Each experiment must sample the same populations, but the number of experiments measuring x and y can differ. Missing values are allowed.
cp.test calls spearnoties.
Alternative |
direction of alternative hypothesis: positive or negative correlation |
F.p.value |
one-sided p-value based on Fisher's combining function |
Fw.p.value |
one-sided p-value based on a weighted Fisher's combining function |
S.p.value |
one-sided p-value based on a weighted average Spearman correlation |
Correlation |
weighted average Spearman correlation |
Douglas M. Potter
Potter, D.M. and Acilan C., Permutation Tests of Correlation for Data with Repeated Measurements, available from D. Potter via email.
##9 different populations on which 7 experiments
##measure x and 5 measure y
npop<-9
nxexpt<-7
nyexpt<-5
##population effects
popeff<-rnorm(npop,0,.5)
x<-rnorm(nxexpt*npop)
y<-rnorm(nyexpt*npop)
x<-matrix(x,nrow=npop)
y<-matrix(y,nrow=npop)
for(i in 1:npop){
for(j in 1:nxexpt){
x[i,j]<-x[i,j]+popeff[i]
}
for(j in 1:nyexpt){
y[i,j]<-y[i,j]+popeff[i]
}
}
cp.test(x,y,100)
## The function is currently defined as
function(xx,yy,nrep=1000,alt=1){
#assumes data is matrix, rows=populations, columns=replicate experiments
if(alt==1)alte<-"One-sided p-values for positive correlation"
else if(alt==-1)alte<-"One-sided p-values for negative correlation"
else stop("alt must be 1 or -1")
p0<-1
p0w<-0
ntot0<-0
stat0<-0
npop<-length(xx[,1])
nxexpt<-length(xx[1,])
nyexpt<-length(yy[1,])
for(i in 1:nxexpt){
for(j in 1:nyexpt){
out<-spearnoties(xx[,i],yy[,j],alt)
if(out[3]>2){
ntot0<-ntot0+(out[3]-1)
p0<-p0*out[1]
p0w<-p0w+log(out[1])*(out[3]-1)
stat0<-stat0+out[2]*(out[3]-1)
}
}
}
stat0<-stat0/ntot0
p0w<-p0w/ntot0
#print(ntot0)
ptest<-rep(1,nrep)
ptestw<-rep(0,nrep)
stat<-rep(0,nrep)
ntot<-rep(0,nrep)
for(irep in 1:nrep){
sam<-sample(1:npop)
for(i in 1:nxexpt){
for(j in 1:nyexpt){
out<-spearnoties(x[,i],y[sam,j],alt)
if(out[3]>2){
ntot[irep]<-ntot[irep]+out[3]-1
ptest[irep]<-ptest[irep]*out[1]
ptestw[irep]<-ptestw[irep]+log(out[1])*(out[3]-1)
stat[irep]<-stat[irep]+out[2]*(out[3]-1)
}
}
}
}
stat<-stat/ntot#weighted by number of pairs that contribute
ptestw<-ptestw/ntot
#print(c(p0,ptest,ptestw))
#print(c(stat0,stat,length(stat[stat<0])))
#print(ntot)
ret.val<-list(Alternative=alte,F.p.value=length(ptest[ptest<=p0])/nrep,
Fw.p.value=length(ptestw[ptestw<=p0w])/nrep,
S.p.value=length((alt*stat)[alt*stat>=alt*stat0])/nrep)
names(ret.val$Alternative)<-" "
names(ret.val$F.p.value)<-"Fisher, unweighted"
names(ret.val$Fw.p.value)<-"Fisher, weighted"
names(ret.val$S.p.value)<-"Spearman"
return(ret.val)
}