-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMRRtutorial.R
executable file
·56 lines (51 loc) · 1.62 KB
/
MMRRtutorial.R
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
# MMRR performs Multiple Matrix Regression with Randomization analysis
# Y is a dependent distance matrix
# X is a list of independent distance matrices (with optional names)
MMRR<-function(Y,X,nperm=999){
#compute regression coefficients and test statistics
nrowsY<-nrow(Y)
y<-unfold(Y)
if(is.null(names(X)))names(X)<-paste("X",1:length(X),sep="")
Xmats<-sapply(X,unfold)
fit<-lm(y~Xmats)
coeffs<-fit$coefficients
summ<-summary(fit)
r.squared<-summ$r.squared
tstat<-summ$coefficients[,"t value"]
Fstat<-summ$fstatistic[1]
tprob<-rep(1,length(tstat))
Fprob<-1
#perform permutations
for(i in 1:nperm){
rand<-sample(1:nrowsY)
Yperm<-Y[rand,rand]
yperm<-unfold(Yperm)
fit<-lm(yperm~Xmats)
summ<-summary(fit)
Fprob<-Fprob+as.numeric(summ$fstatistic[1]>=Fstat)
tprob<-tprob+as.numeric(abs(summ$coefficients[,"t value"])>=abs(tstat))
}
#return values
tp<-tprob/(nperm+1)
Fp<-Fprob/(nperm+1)
names(r.squared)<-"r.squared"
names(coeffs)<-c("Intercept",names(X))
names(tstat)<-paste(c("Intercept",names(X)),"(t)",sep="")
names(tp)<-paste(c("Intercept",names(X)),"(p)",sep="")
names(Fstat)<-"F-statistic"
names(Fp)<-"F p-value"
return(list(r.squared=r.squared,
coefficients=coeffs,
tstatistic=tstat,
tpvalue=tp,
Fstatistic=Fstat,
Fpvalue=Fp))
}
# unfold converts the lower diagonal elements of a matrix into a vector
# unfold is called by MMRR
unfold<-function(X){
x<-vector()
for(i in 2:nrow(X)) x<-c(x,X[i,1:i-1])
x<-scale(x, center=TRUE, scale=TRUE) # Comment this line out if you wish to perform the analysis without standardizing the distance matrices!
return(x)
}