forked from Tomwangjie/Global-bacterial-diversity-in-WWTPs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom forest.R
256 lines (202 loc) · 8.57 KB
/
random forest.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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
156
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
env.file="meta_plant level.csv"
com.file="OTU table_plant level.csv"
# internal use. not published yet. please at least acknowledge if you used.
#install.packages("ieggr",repos = c("ftp://gr:[email protected]","http://cran.us.r-project.org"))
# In some cases, you need to install it in R rather than RStudio.
library(ieggr)
library(parallel)
n<-detectCores()
comm=t(lazyopen(com.file))
env.sample<-lazyopen(env.file)
head(env.sample)
group<-env.sample$Continent
samp.all=rownames(env.sample)
check.samp=match.name(name.check = samp.all,rn.list = list(comm=comm))
comm<-check.samp$comm;sum(row.names(comm)==row.names(env.sample))
##random forest all data##
test1<-colSums(comm>0)
Group<-factor(group,levels=unique(group))
test2<-sapply(split(as.data.frame(comm),Group),colSums)
Test2<-rowSums(test2>0) ##detected in how many continents
otuid<-which(test1>=55 & Test2>=6) ##detected in 20% samples, and in every continent##
commused<-comm[,otuid]
env.sample$Influent.BOD..mg.L.[env.sample$Influent.BOD..mg.L.>800]<-NA ##remove outliers in response variable
yenv<-env.sample$Influent.BOD..mg.L.
sampleid<-which(!is.na(yenv))
yenvused<-yenv[sampleid]
xcommused<-commused[sampleid,]
library(randomForest)
randomf<-randomForest(x=xcommused,y=yenvused,ntree=500,proximity=T,importance = T)
print(randomf)
a<-randomf$predicted
crosspredict<-data.frame(observedBOD=yenvused,crosspredictedBOD=a)
plot(yenvused,a)
save.file(crosspredict,filename = "cross predict_BOD.otu",folder = save.wd)
b<-randomf$importance
save.file(b,filename = "importance_BOD.otu",folder = save.wd)
c<-randomf$proximity
save.file(c,filename = "proximity_BOD.otu",folder = save.wd)
##psedo r2, to indicate prediction strength#
ymean<-mean(crosspredict$observedBOD);
sum((crosspredict$observedBOD-crosspredict$crosspredictedBOD)^2)/sum((crosspredict$observedBOD-ymean)^2)-1
##sludge age
env.sample$Sludge.Age..days.[env.sample$Sludge.Age..days.>35]<-NA
yenv<-env.sample$Sludge.Age..days.
sampleid<-which(!is.na(yenv))
yenvused<-yenv[sampleid]
xcommused<-commused[sampleid,]
randomf<-randomForest(x=xcommused,y=yenvused,ntree=500,proximity=T,importance = T)
print(randomf)
a<-randomf$predicted
crosspredict<-data.frame(observedSage=yenvused,crosspredictedSage=a)
plot(yenvused,a)
save.file(crosspredict,filename = "cross predict_Sludge age.otu",folder = save.wd)
b<-randomf$importance
save.file(b,filename = "importance_Sludge age.otu",folder = save.wd)
c<-randomf$proximity
save.file(c,filename = "proximity_Sludge age.otu",folder = save.wd)
##psedo r2#
ymean<-mean(crosspredict$observedSage);
sum((crosspredict$observedSage-crosspredict$crosspredictedSage)^2)/sum((crosspredict$observedSage-ymean)^2)-1
##Temperature
yenv<-env.sample$Air.temperature.Mean.annual
sampleid<-which(!is.na(yenv))
yenvused<-yenv[sampleid]
xcommused<-commused[sampleid,]
trainid<-which(env.sample$Continent %in% c("North America","South America"))
testid<-which(env.sample$Continent=="Asia")
xcommused<-commused[trainid,];yenvused<-yenv[trainid]
randomf<-randomForest(x=xcommused,y=yenvused,ntree=500,proximity=T,importance = T)
print(randomf)
a<-randomf$predicted
crosspredict<-data.frame(observedTemp=yenvused,crosspredictedTemp=a)
plot(yenvused,a)
save.file(crosspredict,filename = "cross predict_Temp.otu",folder = save.wd)
b<-randomf$importance
save.file(b,filename = "importance_Temp.otu",folder = save.wd)
c<-randomf$proximity
save.file(c,filename = "proximity_Temp.otu",folder = save.wd)
trainid<-which(env.sample$Continent %in% c("North America","South America"))
testid<-which(env.sample$Continent=="Asia")
xcommused<-commused[trainid,];yenvused<-yenv[trainid]
randomf<-randomForest(x=xcommused,y=yenvused,ntree=500,proximity=T,importance = T)
testcommused<-commused[testid,]
testTemp.used<-yenv[testid]
d<-predict(randomf,testcommused)
testpredict<-data.frame(observedTemp=testTemp.used,testpredicted.temp=d)
save.file(testpredict,filename = "test predict_Temp",folder = save.wd)
cor(d,testTemp.used)
plot(testTemp.used,d)
##plot
library(ggplot2)
dat2<-data.frame(x=c(2,29),y=c(2,29))
testpredict$testpredicted.temp
ggplot(testpredict,aes(x=observedTemp,y=testpredicted.temp))+
geom_point(alpha=0.6,colour="blue")+
geom_segment(aes(x = 5, y = 5, xend = 28, yend = 28),colour="red")+
xlab("Observed temperature") +
ylab("Predicted temperature") +
xlim(4,28)+
ylim(4,28)+
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
##psedo r2#
ymean<-mean(testpredict$observedTemp);
sum((testpredict$observedTemp-testpredict$testpredicted.temp)^2)/sum((testpredict$observedTemp-ymean)^2)-1
##temp
crosspredict$observedTemp; crosspredict$crosspredictedTemp
ggplot(crosspredict,aes(x=observedTemp,y=crosspredictedTemp))+
geom_point(alpha=0.6,colour="blue")+
geom_segment(aes(x = 4, y = 4, xend = 28, yend = 28),colour="red")+
xlab("Observed temperature") +
ylab("Predicted temperature") +
xlim(2,30)+
ylim(2,30)+
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
##psedo r2#
ymean<-mean(crosspredict$observedTemp);
sum((crosspredict$observedTemp-crosspredict$crosspredictedTemp)^2)/sum((crosspredict$observedTemp-ymean)^2)-1
##psedo r2# y is observed, x is predicted
ymean<-mean(y)
sum((y-x)^2)/sum((y-ymean)^2)-1
###########exclude geographical neighbors###############################
source("distLLE.r")
geodis=distLLE(latitude = env.sample$Latitude.Google,longitude = env.sample$Longitude.Google,site.name = rownames(env.sample),short = FALSE)
geoexclude<-function(xcomm,yenv,geodis,distance) {
out<-t(parSapply(c1,1:nrow(xcomm), function (i) {
remain<-which(geodis[i,]>=distance)
names(remain)<-NULL
remain2<-sample(c(1:nrow(xcomm))[-i],length(remain))
randomf<-randomForest(x=xcomm[remain,],y=yenv[remain],ntree=500)
excpredicted<-predict(randomf,newdata=xcomm[i,])
randomf2<-randomForest(x=xcomm[remain2,],y=yenv[remain2],ntree=500)
randompredicted<-predict(randomf2,newdata=xcomm[i,])
observed<-yenv[i]
c(observed,excpredicted,randompredicted)
}))
row.names(out)<-row.names(xcomm)
colnames(out)<-c("observed","exclude.predict","rdexclu.predict")
out
}
##sludge age genus##
env.sample$Sludge.Age..days.[env.sample$Sludge.Age..days.>35]<-NA
yenv<-env.sample$Sludge.Age..days.
sampleid<-which(!is.na(yenv))
yenvused<-yenv[sampleid]
xcommused<-commused[sampleid,]
geodisused<-geodis[sampleid,sampleid]
threshold<-c(10,100,500,1000,10000,50000,100000,1000000,5000000)
no_cores=16
c1<-makeCluster(no_cores)
clusterExport(c1,varlist=c("geodisused","threshold","xcommused","yenvused"))
clusterEvalQ(c1, library(randomForest))
diss<-sapply(1:length(threshold),function(i){
message("Now i=",i," in ",length(threshold),". ",date())
dis<-threshold[i]
geoexclude(xcomm=xcommused,yenv=yenvused,geodis=geodisused,distance=dis)
})
colnames(diss)<-c("10m","100m","500m","1000m","10000m","50000m","100000m","1000000m","5000000m")
save.file(diss,filename = "Plant level_Sludge genus level_exclude",folder = save.wd)
stopCluster(c1)
##BOD genus level##
env.sample$Influent.BOD..mg.L.[env.sample$Influent.BOD..mg.L.>800]<-NA
yenv<-env.sample$Influent.BOD..mg.L.
sampleid<-which(!is.na(yenv))
yenvused<-yenv[sampleid]
xcommused<-commused[sampleid,]
geodisused<-geodis[sampleid,sampleid]
threshold<-c(10,100,500,1000,10000,50000,100000,1000000,5000000)
no_cores=16
c1<-makeCluster(no_cores)
clusterExport(c1,varlist=c("geodisused","threshold","xcommused","yenvused"))
clusterEvalQ(c1, library(randomForest))
diss<-sapply(1:length(threshold),function(i){
message("Now i=",i," in ",length(threshold),". ",date())
dis<-threshold[i]
geoexclude(xcomm=xcommused,yenv=yenvused,geodis=geodisused,distance=dis)
})
colnames(diss)<-c("10m","100m","500m","1000m","10000m","50000m","100000m","1000000m","5000000m")
save.file(diss,filename = "plant level_bod genus level_exclude",folder = save.wd)
stopCluster(c1)
## temperature##
yenv<-env.sample$Air.temperature.Mean.annual
sampleid<-which(!is.na(yenv))
yenvused<-yenv[sampleid]
xcommused<-commused[sampleid,]
geodisused<-geodis[sampleid,sampleid]
threshold<-c(10,100,500,1000,10000,50000,100000,1000000,5000000)
no_cores=16
c1<-makeCluster(no_cores)
clusterExport(c1,varlist=c("geodisused","threshold","xcommused","yenvused"))
clusterEvalQ(c1, library(randomForest))
diss<-sapply(1:length(threshold),function(i){
message("Now i=",i," in ",length(threshold),". ",date())
dis<-threshold[i]
geoexclude(xcomm=xcommused,yenv=yenvused,geodis=geodisused,distance=dis)
})
colnames(diss)<-c("10m","100m","500m","1000m","10000m","50000m","100000m","1000000m","5000000m")
save.file(diss,filename = "plant level_temp genus level_exclude",folder = save.wd)
stopCluster(c1)