-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomForest.R
More file actions
181 lines (139 loc) · 6.84 KB
/
randomForest.R
File metadata and controls
181 lines (139 loc) · 6.84 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
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
library(randomForest)
library(dplyr)
asv_abundance <- read.table("asv_realtive_abundance.txt",header=T,sep="\t")
## 过滤ASV
count <- function(abun){
num <- sum(abun>0.1)
return(num)
}
filter_count <- apply(asv_abundance,1,count)
index <- which(filter_count>2)
filter_abundance <- asv_abundance[index,]
## 读入metadata数据并合并数据
metadata <- read.table("healthy_metadata.txt",header=T,sep="\t",row.names=1)
otu <- data.frame(t(filter_abundance))
otu$age_day <- metadata[rownames(otu),]$age_day
# 训练集和测试集
train_sample <- otu[rownames(metadata)[which(metadata$Type=="Training")],]
valid_sample <- otu[rownames(metadata)[which(metadata$Type=="Validation")],]
set.seed(123)
otu_train.forest <- randomForest(age_day~., data = train_sample, importance = TRUE)
otu_train.forest
# Call:
# randomForest(formula = age_day ~ ., data = train_sample, importance = TRUE)
# Type of random forest: regression
# Number of trees: 500
# No. of variables tried at each split: 201
# Mean of squared residuals: 16052.08
# % Var explained: 64.26
#查看该模型的预测性能,可以看到具有较高的精度。
importance_otu <- data.frame(otu_train.forest$importance)
varImpPlot(otu_train.forest, n.var = min(30, nrow(otu_train.forest$importance)),
main = 'Top 30 - variable importance')
#“%IncMSE”即increase in mean squared error,通过对每一个预测变量随机赋值,如果该预测变量更为重要,那么其值被随机替换后模型预测的误差会增大。因此,该值越大表示该变量的重要性越大;
#“IncNodePurity”即increase in node purity,通过残差平方和来度量,代表了每个变量对分类树每个节点上观测值的异质性的影响,从而比较变量的重要性。该值越大表示该变量的重要性越大。
importance_otu <- importance_otu[order(importance_otu$IncNodePurity, decreasing = TRUE), ]
write.table(importance_otu, 'importance_otu.txt', sep = '\t', col.names = NA, quote = FALSE)
# 十折交叉验证
otu_train <- train_sample
set.seed(123)
otu_train.cv <- replicate(10, rfcv(otu_train[-ncol(otu_train)], otu_train$age_day, cv.fold = 10, step = 1.5), simplify = FALSE)
otu_train.cv
otu_train.cv <- data.frame(sapply(otu_train.cv, '[[', 'error.cv'))
otu_train.cv$otus <- rownames(otu_train.cv)
otu_train.cv <- reshape2::melt(otu_train.cv, id = 'otus')
otu_train.cv$otus <- as.numeric(as.character(otu_train.cv$otus))
otu_train.cv.mean <- aggregate(otu_train.cv$value, by = list(otu_train.cv$otus), FUN = mean)
head(otu_train.cv.mean, 10)
#拟合线图
library(ggplot2)
ggplot(otu_train.cv.mean, aes(Group.1, x)) +
geom_line() +
theme(panel.grid = element_blank(), panel.background = element_rect(color = 'black', fill = 'transparent')) +
labs(title = '',x = 'Number of OTUs', y = 'Cross-validation error')
## 还是24个变量
# Group.1 x
# 1 1 31429.30
# 2 2 26456.25
# 3 3 25148.69
# 4 5 23241.56
# 5 7 21539.77
# 6 10 19436.76
# 7 16 17416.52
# 8 24 16545.88
# 9 35 16572.65
# 10 53 16623.18
importance_otu.select <- importance_otu[1:24, ]
otu_id.select <- rownames(importance_otu.select)
write.table(importance_otu.select, 'importance_otu.select.txt', sep = '\t', col.names = NA, quote = FALSE)
##只包含 24 个重要预测变量的简约回归
otu_train.select <- train_sample[,c(otu_id.select,"age_day")]
otu_test.select <- valid_sample[,c(otu_id.select,"age_day")]
#随机森林计算(默认生成 500 棵决策树),详情 ?randomForest
set.seed(123)
otu_train.select.forest <- randomForest(age_day~., data = otu_train.select, importance = TRUE)
otu_train.select.forest
# Call:
# randomForest(formula = age_day ~ ., data = otu_train.select, importance = TRUE)
# Type of random forest: regression
# Number of trees: 500
# No. of variables tried at each split: 8
# Mean of squared residuals: 15775.65
# % Var explained: 64.88
#使用训练集,查看预测精度
# age_predict <- predict(otu_train.select.forest, otu_train.select)
# plot(otu_train.select$age_day, age_predict, main = '训练集',
# xlab = 'age (days)', ylab = 'Predict')
# abline(1, 1)
#使用测试集,评估预测性能
# 将测试集分为signle和多胞胎
names <- rownames(otu_test.select)
single_test <- otu_test.select[which(grepl(names,pattern="Bgs")==TRUE),]
twin_test <- otu_test.select[which(grepl(names,pattern="Bgt")==TRUE),]
age_predict_single <- predict(otu_train.select.forest, single_test)
plot(single_test$age_day, age_predict_single, main = 'Single_Birth_Cohort_Test',
xlab = 'age (days)', ylab = 'Predict')
abline(1, 1)
Single_data <- data.frame(single_test$age_day, age_predict_single)
Single_Fit <- lm(age_predict_single~single_test.age_day,data=Single_data)
summary(Single_Fit)
# lm(formula = age_predict_single ~ single_test.age_day, data = Single_data)
# Residuals:
# Min 1Q Median 3Q Max
# -340.61 -66.55 -1.79 64.17 308.49
# Coefficients:
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) 115.47437 11.21453 10.30 <2e-16 ***
# single_test.age_day 0.71214 0.02805 25.39 <2e-16 ***
# ---
# Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# Residual standard error: 97.4 on 274 degrees of freedom
# Multiple R-squared: 0.7017, Adjusted R-squared: 0.7006
# F-statistic: 644.5 on 1 and 274 DF, p-value: < 2.2e-16
age_predict_twin <- predict(otu_train.select.forest, twin_test)
plot(twin_test$age_day, age_predict_twin, main = 'Twin_Birth_Cohort_Test',
xlab = 'age (days)', ylab = 'Predict')
abline(1, 1)
Twin_data <- data.frame(twin_test$age_day, age_predict_twin)
Twin_Fit <- lm(age_predict_twin~twin_test.age_day,data=Twin_data)
summary(Twin_Fit)
# Call:
# lm(formula = age_predict_twin ~ twin_test.age_day, data = Twin_data)
# Residuals:
# Min 1Q Median 3Q Max
# -327.22 -69.77 -8.07 63.99 276.87
# Coefficients:
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) 138.39419 8.50596 16.27 <2e-16 ***
# twin_test.age_day 0.75011 0.02635 28.47 <2e-16 ***
# ---
# Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# Residual standard error: 100.3 on 445 degrees of freedom
# Multiple R-squared: 0.6455, Adjusted R-squared: 0.6447
# F-statistic: 810.4 on 1 and 445 DF, p-value: < 2.2e-16
## 获取ASV信息
taxa <- read.table("taxonomy.txt",sep="\t",header=T)
ASV <- read.table("Selected_OTUs.txt",header=T,sep="\t")
ASV_infomation <- inner_join(taxa,ASV,by="ASV")
show_info <- ASV_infomation[,c(1,2)]
show_info