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
| rm(list = ls())
library(rpart) library(maptree) library(TH.data) library(tidyverse)
df = data.table::fread('df.csv', header = T, encoding = 'UTF-8')
colnames(df)
df_1 = df[,-c(2,3)] formula_1 = Taxa_S ~. fit_1 = rpart(formula_1, method='anova', data = df_1) pfit_1=prune(fit_1,cp= fit_1$cptable[which.min(fit_1$cptable [,"xerror"]),"CP"]) pdf(file = 'Taxa_S.回归树.pdf', width = 8, height = 5,family="GB1") draw.tree(fit_1,digits = 2) dev.off()
printcp(fit_1) %>% as.data.frame() %>% write.csv(file = 'Taxa_S结果.csv', row.names = FALSE)
df_2 = df[,-c(1,3)] formula_2 = Individuals ~. fit_2 = rpart(formula_2, method='anova', data = df_2) pfit_2=prune(fit_2,cp= fit_2$cptable[which.min (fit_2$cptable [,"xerror"]),"CP"])
pdf(file = 'Individuals.回归树.pdf', width = 8, height = 5,family="GB1") draw.tree(fit_2,digits = 2) dev.off()
printcp(fit_2) %>% as.data.frame() %>% write.csv(file = 'Individuals结果.csv', row.names = FALSE)
df_3 = df[,-c(1,2)] formula_3 = Chao_1 ~. fit_3 = rpart(formula_3, method='anova', data = df_3) pfit_3=prune(fit_3,cp= fit_3$cptable[which.min(fit_3$cptable [,"xerror"]),"CP"])
pdf(file = 'Chao_1.回归树.pdf', width = 8, height = 5,family="GB1") draw.tree(fit_3,digits = 2) dev.off()
printcp(fit_3) %>% as.data.frame() %>% write.csv(file = 'Chao_1结果.csv', row.names = FALSE)
|