Repository navigation
Expand file tree
/
Copy pathspearman.R
More file actions
76 lines (67 loc) · 2.73 KB
/
Copy pathspearman.R
File metadata and controls
76 lines (67 loc) · 2.73 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#
# Step 6: Calculate pariwise spearman correlations
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# 'normalized_order_counts.csv' is generated by 'normalize.R'
normalizedData <- read.csv('data/normalized_order_counts.csv', row.names=1, stringsAsFactors=F)
colCounts <- dim(normalizedData)[2] + 1
# for each of the treatment combinations that was found significant, and one that wasn't (for contrast)
trtGroups <- c("Plants.Water", "Plants", "Season")
for(i in 1:length(trtGroups)) {
curTrtGroup <- strsplit(trtGroups[i], ".", fixed=T)
curTrtGroup <- curTrtGroup[[1]]
trtGroupData <- normalizedData
if (length(curTrtGroup) > 1) {
rep <- apply(trtGroupData[,curTrtGroup], MARGIN=1, FUN=paste, collapse=" ")
fileName <- paste(curTrtGroup, collapse="_")
}
else {
rep <- trtGroupData[,curTrtGroup]
fileName <- curTrtGroup
}
trtGroupData$rep <- rep
trtGroupData <- trtGroupData[,c(colCounts, 1:(colCounts - 1))]
trts<-unique(trtGroupData$rep)
# Each treatment combination will have a 'results' matrix
# 'Treatment' is the treatment combination, i.e. exotic not irrigated
# 'Species1' and 'Species2' will make up every possible unique pair of orders
# 'Coeff' is the spearman correlation coefficient describing the strength of co-occurrence between the two taxa
# 'pValue' is the statistical significant of 'Coeff'
# 'fdr' is the false discovery rate, a p-value adjustment required for multiple test correction
results<-matrix(nrow=0,ncol=8)
colnames(results) <- c("Treatment", "Species1", "Species2", "Coeff", "pValue", "Species1Count", "Species2Count", "fdr")
for(a in 1:length(trts)){
temp<-subset(trtGroupData, rep==trts[a])
tempResults<-matrix(nrow=0,ncol=8)
colnames(tempResults) <- c("Treatment", "Species1", "Species2", "Coeff", "pValue", "Species1Count", "Species2Count", "fdr")
for(b in 8:(dim(temp)[2]-1)){
for(c in (b+1):(dim(temp)[2])){
# this is a place holder
fdr <- 1
# 'ab' = abudnance
species1.ab<-sum(temp[,b])
species2.ab<-sum(temp[,c])
if(species1.ab >1 & species2.ab >1){
test<-cor.test(temp[,b],temp[,c],method="spearman",na.action=na.rm, exact=F)
rho<-test$estimate
p.value<-test$p.value
}
else {
rho<-0
p.value<-1
}
new.row<-c(trts[a],names(temp)[b],names(temp)[c],rho,p.value,species1.ab,species2.ab,fdr)
tempResults<-rbind(tempResults,new.row)
}
}
tempResults[,"fdr"] <- p.adjust(tempResults[,"pValue"], method="fdr")
results <- rbind(results, tempResults)
}
rownames(results) <- c(1:dim(results)[1])
results <- data.frame(results)
# results are for one treatment combination
# save results to disk
fileName <- paste("data/", fileName, "spearman.csv", sep="_")
write.csv(results, file=fileName)
}
q(save="no")