Explore 读取数据 请根据您的数据文件路径进行调整 data <- read.csv("C14.csv") 定义自变量和因变量 根据您的数据框的列名设置列选择… Save Share Download ▾ ReportJump to turn (29) 1 读取数据 请根据您的数据文件路径进行调整 data = 0.05] = 0.01] = 0.001] <- "" dfpre$sig[dfpre$sig == 2 4.401159e-52 =多少 3 那怎么把计算出的R方 和p值放在图中 4 读取数据 请根据您的数据文件路径进行调整 data = 0.05] = 0.01] = 0.001] <- "" dfpre$sig[dfpre$sig == 5 library(randomForest) library(ggplot2) library(extrafont) 创建函数 createrfplot maxi 6 p <- p + annotate("text", x = nrow(featureImportance) 0.8, y = max(featureImport 7 label = paste("P =", format(pvalue, scientific = TRUE)), size = 6, hjust = 0) hj 8 annotate("text", x = nrow(featureImportance) 0.8, y = max(featureImportance$Impo 9 p <- p + annotate("text", x = nrow(featureImportance) 0.8, y = max(featureImport 10 创建函数 createrfplot maximportance 0.9] maximportance 0.7 & featureImportance$Signi 11 library(randomForest) library(ggplot2) library(extrafont) 创建函数 createrfplot maxi 12 rfoutput % mutate(PValue = pvalues): 没有"%>%"这个函数 13 用 else if 结构设置显著性标记 featureImportance$Significance[featureImportance$Importance 14 那么随机森林分析在标注显著性星号的时候是根据重要性 还是p-值呢 15 library(randomForest) library(ggplot2) library(extrafont) 创建函数 createrfplot maxi 16 library(randomForest) library(ggplot2) library(extrafont) 创建函数 createrfplot maxi 17 Mean Decrease in MSE 和%IncMSE是一样的吗 18 library(randomForest) library(ggplot2) library(extrafont) 创建函数 createrfplot maxi 19 library(randomForest) library(ggplot2) library(extrafont) 创建函数 createrfplot maxi 20 可是你给的代码 R方和R方的p值不显示了 21 library(randomForest) library(ggplot2) library(extrafont) 创建函数 createrfplot maxi 22 #随机森林分析DOC同位素------------------------------------------------------------------分 23 特征重要性(Mean Decrease in MSE) 有没有单位 24 p <- ggplot(featureImportance, aes(x = reorder(Feature, Importance), y = Importa 25 5.69669e-61 =多少 26 p < 0.001 在AI软件中 我只想让p斜体, 而< 0.001不是斜体,怎么设置 27 添加R方和P值到图中 if (!is.null(rsquared) && !is.null(pvalue)) { p <- p + annotate("text 28 print(rfoutput$plot) 警告信息: In is.na(x) : is.na()不适用于类别为'language'的非列表或非向量 29 if (!is.null(rsquared) && !is.null(pvalue)) { p <- p + annotate("text", x = nrow 读取数据
请根据您的数据文件路径进行调整
data <- read.csv("C14.csv")
定义自变量和因变量
根据您的数据框的列名设置列选择
response_variable <- "POC"
predictor_variables <- c("precipitation", "rainfallPOC",
"velocity", "WT", "DO", "streampH",
"TClitter_stream", "TClitter_shore",
"soilTN", "soilTP", "leachatepH",
"leachate_amount", "leachateTN",
"leachateDOC", "leachatePOC",
"sedimentTC", "sedimentTN")
Show full message · 62 lines Copy first prompt Copy first response
Details
Provider OpenAI
Model provenance As recorded by source dataset
Turns 29 (58 messages)
Language en
Conversation date Dec 4, 2024
Added to archive Nov 16, 2026
ID 01m7h42ajhnncz4ekpfe369cjwSource & license Imported from WildChat-4.8M (AI2) (source ). Imported records earn no Research Points.
Upstream record: 3180fa0e53960a9c672fe661ba5fac4f
Zhao et al., WildChat: 1M ChatGPT Interaction Logs in the Wild (ICLR 2024). Contains information from WildChat-4.8M, made available under the ODC Attribution License.
License: ODC-BY-1.0
创建包含目标变量和特征的子数据框 df <- data[, c(response_variable, predictor_variables)]
设置随机种子
随机森林分析 rf_results <- rfPermute(df[[response_variable]] ~ ., data = df, importance = TRUE, ntree = 1000, num.cores = 15)
提取变量重要性 predictor_var <- data.frame(importance(rf_results, scale = TRUE), check.names = FALSE)
predictor_sig <- as.data.frame((rf_results$pval)[,,2])
colnames(predictor_sig) <- c("sig", "other")
对结果进行排序并合并显著性与解释率 predictor_var <- predictor_var[order(as.factor(row.names(predictor_var))), ]
predictor_sig <- predictor_sig[order(as.factor(row.names(predictor_sig))), ]
合并显著因子和解释率表,标记显著性 df_pre <- cbind(predictor_var, predictor_sig)
df_pres i g [ d f p r e sig[df_pre s i g [ d f p r e sig >= 0.05] <- 0
df_pres i g [ d f p r e sig[df_pre s i g [ d f p r e sig >= 0.01] <- "*"
df_pres i g [ d f p r e sig[df_pre s i g [ d f p r e sig >= 0.001] <- "**"
df_pres i g [ d f p r e sig[df_pre s i g [ d f p r e sig == 0] <- ""
进行排序 df_pre <- df_pre[order(df_pre$%IncMSE, decreasing = TRUE), ]
添加特征颜色 df_pre$IncNodePurity <- c("#3FBDA7", "#3162A0", "#3FBDA7", "#F08080", rep("#7AA1D2", 5)) # 根据特征替换颜色
仅保留前几个特征进行展示 df_pre <- df_pre[1:nrow(df_pre), ]
绘制柱状图 ID <- as.data.frame(c(1:length(rownames(df_pre))))
df_pre <- cbind(df_pre, ID)
添加图例 ggplot(data = df_pre, aes(x = reorder(rownames(df_pre), df_pre‘ `%IncMSE`), y = df_pre ‘ %IncMSE)) +
geom_bar(stat = 'identity', width = 0.8, linewidth = 1.0, color = "gray2", fill = df_preI n c N o d e P u r i t y ) + t h e m e b w ( b a s e l i n e s i z e = 1.05 , b a s e r e c t s i z e = 1.05 ) + l a b s ( y = ′ I n c r e a s e i n M S E ( g e o m t e x t ( a e s ( l a b e l = d f p r e IncNodePurity) +
theme_bw(base_line_size = 1.05, base_rect_size = 1.05) +
labs(y = 'Increase in MSE (%)', x = '') +
geom_text(aes(label = df_pre I n c N o d e P u r i t y ) + t h e m e b w ( ba s e l in e sig, y = df_pre$%IncMSE + 0.17, x = rownames(df_pre)), vjust = 0.55, size = 7, face = "plain", family = "Times New Roman") +
theme(axis.text = element_text(color = "black", size = 12, face = "plain", family = "Times New Roman")) +
coord_flip()
我想计算这个模型的R方以及R方的P值,怎么修改代码
Assistant2024-12-04 07:45 UTC Copy response
要计算模型的 ( R^2 ) 值和对应的 P 值,可以采用 caret 包来计算模型的性能指标。以下是修改后的代码,添加了 ( R^2 ) 和 P 值的计算。
请确保你已经安装并加载了所需的 R 包,例如 randomForest 和 caret。下面是相关代码的示例,包含计算 ( R^2 ) 和 P 值的部分:
R Copy code
# 载入所需的库
library(randomForest)
library(caret) # 用于计算R方
# 读取数据
data <- read.csv("C14.csv")
# 定义自变量和因变量
response_variable <- "POC"
predictor_variables <- c("precipitation", "rainfallPOC",
"velocity", "WT", "DO", "streampH",
"TClitter_stream", "TClitter_shore",
"soilTN", "soilTP", "leachatepH",
"leachate_amount", "leachateTN",
"leachateDOC", "leachatePOC",
"sedimentTC", "sedimentTN")
# 创建包含目标变量和特征的子数据框
df <- data[, c(response_variable, predictor_variables)]
# 设置随机种子
set.seed(1234)
# 随机森林分析
rf_model <- randomForest(df[[response_variable]] ~ ., data = df, importance = TRUE, ntree = 1000)
# 预测值
predictions <- predict(rf_model, df)
# 计算R方
r_squared <- cor(df[[response_variable]], predictions)^2
# 使用线性回归模型来计算P值
lm_model <- lm(df[[response_variable]] ~ predictions)
summary_lm <- summary(lm_model)
p_value <- summary_lm$coefficients[2,4] # 获取自变量(predictions)的P值
# 输出R方和P值
cat("R方值: ", r_squared, "\n")
cat("R方的P值: ", p_value, "\n")
# 随机森林结果的变量重要性
rf_results <- rfPermute(df[[response_variable]] ~ ., data = df, importance = TRUE, ntree = 1000, num.cores = 15)
# 提取变量重要性
predictor_var <- data.frame(importance(rf_results, scale = TRUE), check.names = FALSE)
predictor_sig <- as.data.frame((rf_results$pval)[,,2])
colnames(predictor_sig) <- c("sig", "other")
# 对结果进行排序并合并显著性与解释率
predictor_var <- predictor_var[order(as.factor(row.names(predictor_var))), ]
predictor_sig <- predictor_sig[order(as.factor(row.names(predictor_sig))), ]
# 合并显著因子和解释率表,标记显著性
df_pre <- cbind(predictor_var, predictor_sig)
df_pre$sig[df_pre$sig >= 0.05] <- 0
df_pre$sig[df_pre$sig >= 0.01] <- "*"
df_pre$sig[df_pre$sig >= 0.001] <- "**"
df_pre$sig[df_pre$sig == 0] <- ""
# 进行排序
df_pre <- df_pre[order(df_pre$`%IncMSE`, decreasing = TRUE), ]
# 添加特征颜色
df_pre$IncNodePurity <- c("#3FBDA7", "#3162A0", "#3FBDA7", "#F08080", rep("#7AA1D2", 5))
# 绘制柱状图
ID <- as.data.frame(c(1:length(rownames(df_pre))))
df_pre <- cbind(df_pre, ID)
# 添加图例
ggplot(data = df_pre, aes(x = reorder(rownames(df_pre), df_pre$`%IncMSE`), y = df_pre$`%IncMSE`)) +
geom_bar(stat = 'identity', width = 0.8, linewidth = 1.0, color = "gray2", fill = df_pre$IncNodePurity) +
theme_bw(base_line_size = 1.05, base_rect_size = 1.05) +
labs(y = 'Increase in MSE (%)', x = '') +
geom_text(aes(label = df_pre$sig, y = df_pre$`%IncMSE` + 0.17, x = rownames(df_pre)),
vjust = 0.55, size = 7, face = "plain", family = "Times New Roman") +
theme(axis.text = element_text(color = "black", size = 12, face = "plain", family = "Times New Roman")) +
coord_flip()
Assistant2024-12-04 07:47 UTC Copy response
4.401159e-52 是科学记数法表示的数值,表示 ( 4.401159 \times 10^{-52} )。
这个数值转换为十进制形式是 0.000000000000000000000000000000000000000000000000004401159,即一个非常接近于零的数,可以用来表示极小的概率或与某个变量的显著性相关的很小的 P 值。
如果您需要转换为更易理解的格式,可以通过计算它的数值,或是判断它在具体应用场合下的意义。在统计分析中,例如,这通常表明所观察到的结果在随机情况下的发生概率是极其微小的,通常会被认为是具有统计显著性的。
Assistant2024-12-04 07:49 UTC Copy response
要将计算得到的 ( R^2 ) 值和 P 值添加到 ggplot 图中,可以使用 annotate 函数。在 ggplot2 中,您可以用 geom_text 或 annotate 来实现。下面是如何在柱状图上添加 ( R^2 ) 和 P 值的示例。
在您的代码中,找到绘制图形的部分,并在适当的位置添加 ( R^2 ) 和 P 值的注释。以下是修改过的部分代码示例:
r Copy code
# 计算R方和P值(之前的代码保持不变)
# ...
# 计算R方
r_squared <- cor(df[[response_variable]], predictions)^2
# 计算P值
lm_model <- lm(df[[response_variable]] ~ predictions)
summary_lm <- summary(lm_model)
p_value <- summary_lm$coefficients[2, 4] # 获取自变量(predictions)的P值
# 输出R方和P值
cat("R方值: ", r_squared, "\n")
cat("R方的P值: ", p_value, "\n")
# 绘制柱状图
ID <- as.data.frame(c(1:length(rownames(df_pre))))
df_pre <- cbind(df_pre, ID)
# 添加图例
plot <- ggplot(data = df_pre, aes(x = reorder(rownames(df_pre), df_pre$`%IncMSE`), y = df_pre$`%IncMSE`)) +
geom_bar(stat = 'identity', width = 0.8, linewidth = 1.0, color = "gray2", fill = df_pre$IncNodePurity) +
theme_bw(base_line_size = 1.05, base_rect_size = 1.05) +
labs(y = 'Increase in MSE (%)', x = '') +
geom_text(aes(label = df_pre$sig, y = df_pre$`%IncMSE` + 0.17, x = rownames(df_pre)),
vjust = 0.55, size = 7, face = "plain", family = "Times New Roman") +
theme(axis.text = element_text(color = "black", size = 12, face = "plain", family = "Times New Roman")) +
coord_flip()
# 在图中添加R方和P值
plot +
annotate("text", x = nrow(df_pre) * 0.8, y = max(df_pre$`%IncMSE`) * 0.9,
label = paste("R² =", round(r_squared, 3)), size = 5, hjust = 0) +
annotate("text", x = nrow(df_pre) * 0.8, y = max(df_pre$`%IncMSE`) * 0.85,
label = paste("P =", format(p_value, scientific = TRUE)), size = 5, hjust = 0)
在这个代码中:
annotate 用于在图中添加文本。x 和 y 参数控制文本的位置,您可以根据需要进行调整。
label 中的内容使用 paste 函数将字符串拼接在一起,以便显示 ( R^2 ) 和 P 值。
round(r_squared, 3) 用于将 ( R^2 ) 的值格式化为小数点后三位。
format(p_value, scientific = TRUE) 用于将 P 值以科学记数法格式显示。
您可以调整 x 和 y 的值,以适应您的图形布局。运行完整代码后,您应该能在图中看到 ( R^2 ) 和 P 值的文本。
读取数据
请根据您的数据文件路径进行调整
data <- read.csv("C14.csv")
定义自变量和因变量
根据您的数据框的列名设置列选择
response_variable <- "POC"
predictor_variables <- c("precipitation", "rainfallPOC",
"velocity", "WT", "DO", "streampH",
"TClitter_stream", "TClitter_shore",
"soilTN", "soilTP", "leachatepH",
"leachate_amount", "leachateTN",
"leachateDOC", "leachatePOC",
"sedimentTC", "sedimentTN")
创建包含目标变量和特征的子数据框
df <- data[, c(response_variable, predictor_variables)]
设置随机种子
set.seed(1234)
随机森林分析
rf_results <- rfPermute(df[[response_variable]] ~ ., data = df, importance = TRUE, ntree = 1000, num.cores = 15)
提取变量重要性
predictor_var <- data.frame(importance(rf_results, scale = TRUE), check.names = FALSE)
predictor_sig <- as.data.frame((rf_results$pval)[,,2])
colnames(predictor_sig) <- c("sig", "other")
对结果进行排序并合并显著性与解释率
predictor_var <- predictor_var[order(as.factor(row.names(predictor_var))), ]
predictor_sig <- predictor_sig[order(as.factor(row.names(predictor_sig))), ]
合并显著因子和解释率表,标记显著性
df_pre <- cbind(predictor_var, predictor_sig)
df_pres i g [ d f p r e sig[df_pre s i g [ d f p r e sig >= 0.05] <- 0
df_pres i g [ d f p r e sig[df_pre s i g [ d f p sig >= 0.01] <- "*"
df_pre sig >= 0.001] <- "**"
df_pre sig == 0] <- ""
进行排序
df_pre <- df_pre[order(df_pre$%IncMSE, decreasing = TRUE), ]
添加特征颜色
df_pre$IncNodePurity <- c("#3FBDA7", "#3162A0", "#3FBDA7", "#F08080", rep("#7AA1D2", 5)) # 根据特征替换颜色
仅保留前几个特征进行展示
df_pre <- df_pre[1:nrow(df_pre), ]
绘制柱状图
ID <- as.data.frame(c(1:length(rownames(df_pre))))
df_pre <- cbind(df_pre, ID)
添加图例
ggplot(data = df_pre, aes(x = reorder(rownames(df_pre), df_pre%IncMSE), y = df_pre %IncMSE)) +
geom_bar(stat = 'identity', width = 0.8, linewidth = 1.0, color = "gray2", fill = df_preI n c N o d e P u r i t y ) + t h e m e b w ( b a s e l i n e s i z e = 1.05 , b a s e r e c t s i z e = 1.05 ) + l a b s ( y = ′ I n c r e a s e i n M S E ( g e o m t e x t ( a e s ( l a b e l = d f p r e IncNodePurity) +
theme_bw(base_line_size = 1.05, base_rect_size = 1.05) +
labs(y = 'Increase in MSE (%)', x = '') +
geom_text(aes(label = df_pre sig, y = df_pre$%IncMSE + 0.17, x = rownames(df_pre)), vjust = 0.55, size = 7, face = "plain", family = "Times New Roman") +
theme(axis.text = element_text(color = "black", size = 12, face = "plain", family = "Times New Roman")) +
coord_flip()
我想计算这个模型的R方以及R方的P值,并且在图中显示R方以及R方的P值,怎么修改代码
Show full message · 62 lines Assistant2024-12-04 07:50 UTC Copy response
您可以在随机森林模型中计算 ( R^2 ) 和 P 值,然后将这些信息添加到绘制的柱状图中。下面是完整代码的修改版本,包括计算 ( R^2 ) 和 P 值的步骤,并将这些值以文本形式添加到图中。
修改后的代码如下所示:
r Copy code
# 载入所需的库
library(randomForest)
library(caret) # 用于计算R方
library(ggplot2)
# 读取数据
# 请根据您的数据文件路径进行调整
data <- read.csv("C14.csv")
# 定义自变量和因变量
response_variable <- "POC"
predictor_variables <- c("precipitation", "rainfallPOC",
"velocity", "WT", "DO", "streampH",
"TClitter_stream", "TClitter_shore",
"soilTN", "soilTP", "leachatepH",
"leachate_amount", "leachateTN",
"leachateDOC", "leachatePOC",
"sedimentTC", "sedimentTN")
# 创建包含目标变量和特征的子数据框
df <- data[, c(response_variable, predictor_variables)]
# 设置随机种子
set.seed(1234)
# 随机森林分析
rf_results <- randomForest(df[[response_variable]] ~ ., data = df, importance = TRUE, ntree = 1000)
# 预测值
predictions <- predict(rf_results, df)
# 计算R方
r_squared <- cor(df[[response_variable]], predictions)^2
# 使用线性回归模型来计算P值
lm_model <- lm(df[[response_variable]] ~ predictions)
summary_lm <- summary(lm_model)
p_value <- summary_lm$coefficients[2, 4] # 获取自变量(predictions)的P值
# 输出R方和P值
cat("R方值: ", r_squared, "\n")
cat("R方的P值: ", p_value, "\n")
# 提取变量重要性
predictor_var <- data.frame(importance(rf_results, scale = TRUE), check.names = FALSE)
predictor_sig <- as.data.frame((rf_results$importance[,1])[, drop = FALSE])
colnames(predictor_sig) <- c("sig")
# 对结果进行排序并合并显著性与解释率
predictor_var <- predictor_var[order(as.factor(row.names(predictor_var))), ]
predictor_sig <- predictor_sig[order(as.factor(row.names(predictor_sig))), ]
# 合并显著因子和解释率表,标记显著性
df_pre <- cbind(predictor_var, predictor_sig)
df_pre$sig[df_pre$sig >= 0.05] <- 0
df_pre$sig[df_pre$sig >= 0.01] <- "*"
df_pre$sig[df_pre$sig >= 0.001] <- "**"
df_pre$sig[df_pre$sig == 0] <- ""
# 进行排序
df_pre <- df_pre[order(df_pre$`%IncMSE`, decreasing = TRUE), ]
# 添加特征颜色
df_pre$IncNodePurity <- c("#3FBDA7", "#3162A0", "#3FBDA7", "#F08080", rep("#7AA1D2", 5))
# 绘制柱状图
ID <- as.data.frame(c(1:length(rownames(df_pre))))
df_pre <- cbind(df_pre, ID)
# 添加图例
plot <- ggplot(data = df_pre, aes(x = reorder(rownames(df_pre), df_pre$`%IncMSE`), y = df_pre$`%IncMSE`)) +
geom_bar(stat = 'identity', width = 0.8, linewidth = 1.0, color = "gray2", fill = df_pre$IncNodePurity) +
theme_bw(base_line_size = 1.05, base_rect_size = 1.05) +
labs(y = 'Increase in MSE (%)', x = '') +
geom_text(aes(label = df_pre$sig, y = df_pre$`%IncMSE` + 0.17, x = rownames(df_pre)),
vjust = 0.55, size = 7, face = "plain", family = "Times New Roman") +
theme(axis.text = element_text(color = "black", size = 12, face = "plain", family = "Times New Roman")) +
coord_flip()
# 在图中添加R方和P值
plot +
annotate("text", x = nrow(df_pre) * 0.8, y = max(df_pre$`%IncMSE`) * 0.9,
label = paste("R² =", round(r_squared, 3)), size = 5, hjust = 0) +
annotate("text", x = nrow(df_pre) * 0.8, y = max(df_pre$`%IncMSE`) * 0.85,
label = paste("P =", format(p_value, scientific = TRUE)), size = 5, hjust = 0)
library(randomForest)
library(ggplot2)
library(extrafont)
创建函数
create_rfplot <- function(rf, type, significance_threshold = 0.05) {
计算特征重要性
imp <- importance(rf, type = type, scale = FALSE)
featureImportance <- data.frame(Feature = row.names(imp), Importance = imp[, 1])
添加显著性标记
max_importance <- max(featureImportance$Importance)
featureImportance$Significance <- ""
用 else if 结构设置显著性标记
featureImportanceS i g n i f i c a n c e [ f e a t u r e I m p o r t a n c e Significance[featureImportance S i g ni f i c an ce [ f e a t u r e I m p or t an ce Importance > max_importance * 0.9] <- "**" # 三颗星
featureImportance Importance > max_importance * 0.7 &
featureImportance Significance[featureImportance Significance == ""] <- " " # 一颗星
Show full message · 98 lines Assistant2024-12-04 07:55 UTC Copy response
要计算随机森林模型的 ( R^2 ) 和 P 值,并把这些值添加到柱状图中,您可以在现有的代码基础上进行一些修改。具体步骤如下:
计算 ( R^2 ) 值。
使用线性回归模型计算 P 值。
使用 annotate 函数将 ( R^2 ) 和 P 值添加到生成的图中。
以下是修改后的完整代码:
p <- p +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportanceI m p o r t a n c e ) ∗ 0.9 , l a b e l = p a s t e ( " R 2 = " , r o u n d ( r s q u a r e d , 3 ) ) , s i z e = 6 , h j u s t = 0 ) + a n n o t a t e ( " t e x t " , x = n r o w ( f e a t u r e I m p o r t a n c e ) ∗ 0.8 , y = m a x ( f e a t u r e I m p o r t a n c e Importance) * 0.9,
label = paste("R² =", round(r_squared, 3)), size = 6, hjust = 0) +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportance I m p or t an ce ) ∗ 0.9 , l ab e l = p a s t e ( " R 2 = " , r o u n d ( r Importance) * 0.85,
label = paste("P =", format(p_value, scientific = TRUE)), size = 6, hjust = 0)
} R方以及R方的p值分开一点怎么设置
Assistant2024-12-04 07:58 UTC Copy response
要调整 ( R^2 ) 和 P 值之间的垂直间距,您可以通过调整 y 值来实现。下面是修改后的代码示例,增加了 ( R^2 ) 和 P 值之间的分离距离。
您可以在 annotate 函数中增加 y 值的大小差异,使两者之间的距离更大。例如,将 y 的值增加以调整间距。
r Copy code
# 添加R方和P值到图中,并调整间距
if (!is.null(r_squared) && !is.null(p_value)) {
p <- p +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportance$Importance) * 0.9,
label = paste("R² =", round(r_squared, 3)), size = 6, hjust = 0) +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportance$Importance) * 0.83,
label = paste("P =", format(p_value, scientific = TRUE)), size = 6, hjust = 0)
}
关键点:
调整 y 值 :更新第二个 annotate 函数中 y 的值,使其稍低于第一个 ( R^2 ) 值,这样可以在视觉上增加它们之间的间距(在上述代码中,我将第二个 y 值从 max(featureImportance$Importance) * 0.85 更新为 max(featureImportance$Importance) * 0.83)。
可以根据需要微调 :您可以根据显示效果进一步调整这两个 y 值,以适应您图表的具体需求。
完整示例代码中更新的部分:
如果将所有修改汇总到完整代码中,您只需替换 create_rfplot 函数中的这部分即可。
r Copy code
# 添加R方和P值到图中,并调整间距
if (!is.null(r_squared) && !is.null(p_value)) {
p <- p +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportance$Importance) * 0.9,
label = paste("R² =", round(r_squared, 3)), size = 6, hjust = 0) +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportance$Importance) * 0.83,
label = paste("P =", format(p_value, scientific = TRUE)), size = 6, hjust = 0)
}
调整这些值后再运行代码,您将看到 ( R^2 ) 和 P 值之间的距离有所增加。
label = paste("P =", format(p_value, scientific = TRUE)), size = 6, hjust = 0) hjust是什么
Assistant2024-12-04 08:00 UTC Copy response
在 ggplot2 中,hjust 参数用于控制文本水平对齐的方式。它的值可以是 0 到 1 之间的任意数字,具体含义如下:
hjust = 0 :文本将左对齐(也就是文本的左边缘与指定坐标对齐)。
hjust = 0.5 :文本将居中对齐(文本的中心与指定坐标对齐)。
hjust = 1 :文本将右对齐(文本的右边缘与指定坐标对齐)。
举例来说,如果您在指定的坐标位置 (x, y) 上放置文本,并使用 hjust = 0,那么文本将以其左边缘与 x 位置对齐;使用 hjust = 1 时,文本的右边缘将与 x 位置对齐;使用 hjust = 0.5 时,文本的中心将与 x 位置对齐。
示例:
假设您要在图中的 (3, 5) 处放置文本 "Hello"。
如果 hjust = 0,那么 "Hello" 的左端将与 x = 3 对齐。
如果 hjust = 0.5,那么 "Hello" 的中心将与 x = 3 对齐。
如果 hjust = 1,那么 "Hello" 的右端将与 x = 3 对齐。
选择合适的 hjust 值可以帮助您将文本放置得更加美观和易于阅读。在您的代码中,您可以根据文本的实际情况选择合适的对齐方式。例如,您可能希望 P 值文本左对齐,以便与之前的 ( R^2 ) 值保持一定的间距和一致的格式。
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportance$Importance) * 0.85,
label = paste("P =", format(p_value, scientific = TRUE)), size = 6, hjust = 0) 解读
Assistant2024-12-04 08:02 UTC Copy response
在 R 中,annotate 函数用于在 ggplot 图形中添加文本或者其他标注。下面是您提供的这行代码的逐部分解读:
r Copy code
annotate("text",
x = nrow(featureImportance) * 0.8,
y = max(featureImportance$Importance) * 0.85,
label = paste("P =", format(p_value, scientific = TRUE)),
size = 6,
hjust = 0)
各部分详细解读:
annotate("text", ...) :
这部分指定了要添加的注释类型为文本("text")。
x = nrow(featureImportance) * 0.8 :
nrow(featureImportance) 返回 featureImportance 数据框的行数。
乘以 0.8 是为了设置文本的 x 坐标,使文本位于图形宽度的 80% 位置。这通常用于确保文本在图的适当位置,以便清晰可读。
y = max(featureImportance$Importance) * 0.85 :
max(featureImportance$Importance) 获取特征重要性(Importance)列中的最大值。
乘以 0.85 设置文本的 y 坐标,使文本位置在最大的特征重要性值的 85% 位置。这使得文本稍微低于最高的柱子,从而避免与柱子重叠,提升可读性。
label = paste("P =", format(p_value, scientific = TRUE)) :
paste("P =", format(p_value, scientific = TRUE)) 创建了要显示的文本标签。
format(p_value, scientific = TRUE) 将 P 值格式化为科学记数法。例如,如果 P 值是 (0.000123),使用 scientific = TRUE 会将其格式化为 (1.23 \times 10^{-4})。
paste 函数将 "P =" 与格式化的 P 值连接起来,构成最终显示的标签,例如 "P = 1.23e-04"。
size = 6 :
hjust = 0 :
指定了文本的水平对齐方式为左对齐(文本的左边缘与指定的 x 位置对齐)。这在这种情况下通常有助于使文本沿着其他元素(例如绘图中的 ( R^2 ) 标记)对齐,提升整体的可读性和美观性。
综合理解:
这行代码的作用是在绘制的 ggplot 图形上,以指定的 x 和 y 坐标位置添加文本,内容为 P 值,并进行适当的格式化和对齐,使其在图形上清晰可见且易于理解。通过这些设置,确保 P 值未与柱状图重叠并且位置适当,以便观众易于解读结果。
p <- p +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportanceI m p o r t a n c e ) ∗ 0.9 , l a b e l = p a s t e ( " R 2 = " , r o u n d ( r s q u a r e d , 3 ) ) , s i z e = 6 , h j u s t = 0 ) + a n n o t a t e ( " t e x t " , x = n r o w ( f e a t u r e I m p o r t a n c e ) ∗ 0.8 , y = m a x ( f e a t u r e I m p o r t a n c e Importance) * 0.9,
label = paste("R² =", round(r_squared, 3)), size = 6, hjust = 0) +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportance I m p or t an ce ) ∗ 0.9 , l ab e l = p a s t e ( " R 2 = " , r o u n d ( r Importance) * 0.83,
p <- p +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportanceI m p o r t a n c e ) ∗ 0.9 , l a b e l = p a s t e ( " R 2 = " , r o u n d ( r s q u a r e d , 3 ) ) , s i z e = 6 , h j u s t = 0 ) + a n n o t a t e ( " t e x t " , x = n r o w ( f e a t u r e I m p o r t a n c e ) ∗ 0.8 , y = m a x ( f e a t u Importance) * 0.85,
label = paste("P =", format(p_value, scientific = TRUE)), size = 6, hjust = 0)
}
} 这代码R方 和R方对应的p值 黏在一起了 ,怎么上下分开 在图中
Assistant2024-12-04 08:03 UTC Copy response
要确保图中的 ( R^2 ) 和 P 值上下分开,并且不会彼此重叠,您可以通过调整 Y 坐标的位置来实现。以下是一个示例,更新了您的代码并添加了一些注释,帮助您理解每一部分的作用。这将确保两个值之间的距离足够大,以避免重叠。
r Copy code
# 添加 R² 和 P 值到图中,并调整它们之间的垂直间距
if (!is.null(r_squared) && !is.null(p_value)) {
p <- p +
annotate("text",
x = nrow(featureImportance) * 0.8,
y = max(featureImportance$Importance) * 0.9, # R² 的 y 值
label = paste("R² =", round(r_squared, 3)),
size = 6,
hjust = 0) +
annotate("text",
x = nrow(featureImportance) * 0.8,
y = max(featureImportance$Importance) * 0.82, # P 值的 y 值,向下调整
label = paste("P =", format(p_value, scientific = TRUE)),
size = 6,
hjust = 0)
}
关键修改:
y = max(featureImportance$Importance) * 0.9 用于确定 ( R^2 ) 的位置。
y = max(featureImportance$Importance) * 0.82 适当地将 P 值位置向下调整,使得 ( R^2 ) 和 P 值之间有明确空隙。您可以根据需要微调这两个值,以确保在您的图形上它们的显示美观。
如何进一步调整:
如果 ( R^2 ) 和 P 值仍然看起来太接近,您可以进一步减小 P 值的 y 值。例如,可以尝试将 P 值的 y 值改为 max(featureImportance$Importance) * 0.80,依此类推,直到满足视觉上的需求。
请留意您使用的相对坐标,这样在不同的数据集上也能够保持更好的适应性。
将这些修改后的代码放入您完整的绘图代码中,运行之后,您应该能够看到 ( R^2 ) 和 P 值在图中清晰分开的显示效果。
创建函数
create_rfplot <- function(rf, type, significance_threshold = 0.05, r_squared = NULL, p_value = NULL) {
计算特征重要性
imp <- importance(rf, type = type, scale = FALSE)
featureImportance <- data.frame(Feature = row.names(imp), Importance = imp[, 1])
添加显著性标记
max_importance <- max(featureImportance$Importance)
featureImportance$Significance <- ""
用 else if 结构设置显著性标记
featureImportanceS i g n i f i c a n c e [ f e a t u r e I m p o r t a n c e Significance[featureImportance S i g ni f i c an ce [ f e a t u r e I m p or t an ce Importance > max_importance * 0.9] <- "**" # 三颗星
featureImportance Importance > max_importance * 0.7 &
featureImportance Significance[featureImportance Significance == ""] <- " " # 一颗星
Show full message · 114 lines Assistant2024-12-04 08:08 UTC Copy response
要将您提到的显著性标记逻辑应用于 create_rfplot 函数中,您需要将相应的显著性标记(sig)从 df_pre 数据框合并到 featureImportance 数据框中。以下是修改后的代码示例,展示了如何将新的显著性标记逻辑应用于您的随机森林图形中。
修改后的完整代码示例
library(randomForest)
library(ggplot2)
library(extrafont)
创建函数
create_rfplot <- function(rf, type, significance_threshold = 0.05, r_squared = NULL, p_value = NULL) {
计算特征重要性
imp <- importance(rf, type = type, scale = FALSE)
featureImportance <- data.frame(Feature = row.names(imp), Importance = imp[, 1])
添加显著性标记
max_importance <- max(featureImportance$Importance)
featureImportance$Significance <- ""
用 else if 结构设置显著性标记
featureImportanceS i g n i f i c a n c e [ f e a t u r e I m p o r t a n c e Significance[featureImportance S i g ni f i c an ce [ f e a t u r e I m p or t an ce Importance > max_importance * 0.9] <- "**" # 三颗星
featureImportance Importance > max_importance * 0.7 &
featureImportance Significance[featureImportance Significance == ""] <- " " # 一颗星
Show full message · 113 lines Assistant2024-12-04 08:14 UTC Copy response
要根据显著性的 P 值设置符号(*、**、***等),您需要在随机森林分析中提取和计算 P 值,然后将这些 P 值与特征的重要性一同使用用于确定显著性符号。以下是如何修改代码,使其能够根据 P 值分配显著性的标记。
修改步骤:
在 importance 函数计算完后,提取每个特征的 P 值。
根据这些 P 值设置显著性标记。
在绘制图形时,将这些显著性标记显示出来。
以下是更新后的完整代码示例:
rf_output <- create_rfplot(rf1, type = 2, p_values = p_values, r_squared = r_squared, p_value = p_value)
错误于featureImportance %>% mutate(P_Value = p_values):
没有"%>%"这个函数
Assistant2024-12-04 08:15 UTC Copy response
您遇到的错误提示说明您缺少 dplyr 包或者没有加载它,导致 %>% 操作符无法识别。%>% 是管道操作符,属于 dplyr 包,用于将左侧的对象传递给右侧的函数。
解决方法:
确保安装并加载 dplyr 包。
使用 install.packages("dplyr") 安装 dplyr 包(如果尚未安装)。可以通过以下命令加载它:
修改后的完整代码示例
以下是包含所有修改(确保所有所需库已加载)的完整代码示例:
用 else if 结构设置显著性标记
featureImportanceS i g n i f i c a n c e [ f e a t u r e I m p o r t a n c e Significance[featureImportance S i g ni f i c an ce [ f e a t u r e I m p or t an ce Importance > max_importance * 0.9] <- "**" # 三颗星
featureImportanceS i g n i f i c a n c e [ f e a t u r e I m p o r t a n c e Significance[featureImportance S i g ni f i c an ce [ f e a t u r e I m p or t an ce Importance > max_importance * 0.7 &
featureImportanceSignificance == ""] <- "**" # 两颗星
featureImportance Significance[featureImportanceImportance > max_importance * 0.5 &
featureImportance Significance == ""] <- " " # 一颗星
print(featureImportance) 这样设置显著性星号是对的吗
Assistant2024-12-04 08:18 UTC Copy response
您所提供的代码使用 else if 逻辑来为特征设置显著性星号是合理的,但存在一个潜在问题,特别是在检测 P 值时。下面是您代码片段的解读和建议。
当前的显著性标记逻辑
r Copy code
featureImportance$Significance[featureImportance$Importance > max_importance * 0.9] <- "***" # 三颗星
featureImportance$Significance[featureImportance$Importance > max_importance * 0.7 &
featureImportance$Significance == ""] <- "**" # 两颗星
featureImportance$Significance[featureImportance$Importance > max_importance * 0.5 &
featureImportance$Significance == ""] <- "*" # 一颗星
逻辑分析
最高重要性阈值 :
三颗星 : 如果某个特征的重要性超过最大重要性的 90%,则分配三颗星。
两颗星 : 如果重要性超过最大重要性的 70% 但未分配星号,则分配两颗星。
一颗星 : 如果重要性超过最大重要性的 50% 但未分配星号,则分配一颗星。
潜在问题
基于重要性而非 P 值 : 这样的显著性逻辑是基于特征的重要性,而不是基于特征的统计显著性水平(P 值)。如果您的目标是基于 P 值来定义显著性标记,您需要首先计算 P 值,然后使用 P 值进行标记,而不是重要性值。
应确保不重复 : 在标记时,应确保在一次操作中设置标记,并且不能在其后被再次设置为其他标记。您已经使用了 Significance == "" 作为条件来避免覆盖,但应确保该逻辑的一致性。
修改建议
如果您想使用基于 P 值的显著性标记,可以按以下方式修改:
确保每个特征都有对应的 P 值。
下面是基于 P 值设置显著性标记的示例代码:
r Copy code
# 假设已经计算了 p_values
featureImportance$Significance <- ifelse(featureImportance$P_Value < 0.001, "***",
ifelse(featureImportance$P_Value < 0.01, "**",
ifelse(featureImportance$P_Value < 0.05, "*", "")))
使用基于 P 值设置的示例代码
r Copy code
# 计算 P 值的示例(请根据实际情况替换)
p_values <- runif(nrow(featureImportance), 0, 0.1) # 示例随机 P 值
# 将 P 值合并到特征重要性数据框中
featureImportance <- featureImportance %>%
mutate(P_Value = p_values)
# 设置显著性标记
featureImportance$Significance <- ifelse(featureImportance$P_Value < 0.001, "***",
ifelse(featureImportance$P_Value < 0.01, "**",
ifelse(featureImportance$P_Value < 0.05, "*", "")))
print(featureImportance)
结论
如果使用重要性来标记显著性,您当前的代码是合理的,但确保阈值定义和上下文相符。
如果您想用 P 值而非重要性进行显著性标记,则应按照上述建议修改代码。
那么随机森林分析在标注显著性星号的时候是根据重要性 还是p-值呢
Assistant2024-12-04 08:21 UTC Copy response
在随机森林分析中,标注显著性星号的方式可以根据您的具体需要而定,其实并没有固定的标准。然而,通常有两种方法来进行显著性标注:
基于特征重要性 :这是您当前使用的方法。特征重要性是通过随机森林模型计算得出的指标,反映了某一特征在预测任务中的贡献程度。您可以使用特征的重要性得分进行显著性标注,比如设置阈值得分来决定标记星号,示例代码如下:
r Copy code
# 根据特征重要性设置星号标记
featureImportance$Significance <- ""
featureImportance$Significance[featureImportance$Importance > max_importance * 0.9] <- "***" # 三颗星
featureImportance$Significance[featureImportance$Importance > max_importance * 0.7 &
featureImportance$Significance == ""] <- "**" # 两颗星
featureImportance$Significance[featureImportance$Importance > max_importance * 0.5 &
featureImportance$Significance == ""] <- "*" # 一颗星
基于 P 值 :这是另一种常见的方法,通常在传统统计分析中使用。在这种方法中,您需要计算每个特征的 P 值(例如使用线性回归或其他统计检验),然后根据这些 P 值来决定星号的标注。例如,您可以将 P 值与 0.05, 0.01, 和 0.001 等显著性水平进行比较,以决定显著性星号:
r Copy code
# 假设 p_values 是每个特征的 P 值
featureImportance$Significance <- ifelse(featureImportance$P_Value < 0.001, "***",
ifelse(featureImportance$P_Value < 0.01, "**",
ifelse(featureImportance$P_Value < 0.05, "*", "")))
比较:
特征重要性标注 :
反映了特征在模型中的贡献度。
适用于模型的解释和特征选择。
P 值标注 :
反映了特征对响应变量的统计显著性。
更适用于对假设检验的检验和解释。
结论:
您可以根据您的最终目标选择使用哪种方法标注显著性星号。
如果您关注的是特征如何在模型中驱动预测,使用特征重要性是合适的。
如果您希望评估特征对结果变量的统计显著性,并以此进行决策,则使用 P 值更为恰当。在实践中,某些研究可能会同时报告特征重要性和 P 值,以提供更全面的视角。
library(randomForest)
library(ggplot2)
library(extrafont)
创建函数
create_rfplot <- function(rf, type, significance_threshold = 0.05) {
计算特征重要性
imp <- importance(rf, type = type, scale = FALSE)
featureImportance <- data.frame(Feature = row.names(imp), Importance = imp[,1])
添加显著性标记
max_importance <- max(featureImportance$Importance)
featureImportance$Significance <- ""
用 else if 结构设置显著性标记
featureImportanceS i g n i f i c a n c e [ f e a t u r e I m p o r t a n c e Significance[featureImportance S i g ni f i c an ce [ f e a t u r e I m p or t an ce Importance > max_importance * 0.9] <- "**" # 三颗星
featureImportance Importance > max_importance * 0.7 &
featureImportance Significance[featureImportance Significance == ""] <- " " # 一颗星
Show full message · 99 lines Assistant2024-12-04 08:28 UTC Copy response
为了在您现有的随机森林绘图代码中计算和标注 ( R^2 ) 值和相应的 P 值,您需要在代码中添加几个步骤。这包括:
从预测结果中计算 ( R^2 ) 值。
使用线性回归模型计算 P 值。
将这些值通过 annotate 函数添加到生成的图形中。
以下是修改后的完整代码示例:
library(randomForest)
library(ggplot2)
library(extrafont)
创建函数
create_rfplot <- function(rf, type, significance_threshold = 0.05, r_squared = NULL, p_value = NULL) {
计算特征重要性
imp <- importance(rf, type = type, scale = FALSE)
featureImportance <- data.frame(Feature = row.names(imp), Importance = imp[, 1])
添加显著性标记
max_importance <- max(featureImportance$Importance)
featureImportance$Significance <- ""
用 else if 结构设置显著性标记
featureImportanceS i g n i f i c a n c e [ f e a t u r e I m p o r t a n c e Significance[featureImportance S i g ni f i c an ce [ f e a t u r e I m p or t an ce Importance > max_importance * 0.9] <- "**" # 三颗星
featureImportance Importance > max_importance * 0.7 &
featureImportance Significance[featureImportance Significance == ""] <- " " # 一颗星
Show full message · 113 lines Assistant2024-12-04 08:43 UTC Copy response
在您的代码中,X 轴表示的是特征的重要性(Importance),它是通过 randomForest 的 importance 函数计算得出的。重要性可以通过不同的度量标准来计算,包括 %IncMSE(即均方误差增加百分比)和 IncNodePurity(节点纯度增加)。在您提供的代码片段中,使用了默认 type 为 1 的设置,即Mean Decrease in MSE(均方误差下降的平均值)。
关键点:
X轴的含义 :
在绘图中,importance 默认是与特征的预测能力相关的标准,通常在使用随机森林时,这个标准是 Mean Decrease in MSE,即均方误差的下降。
如何更改类型 :
如果您希望将图形中的重要性标准更换为 IncNodePurity,您需要将随机森林模型中的 type 参数更改为 1。但是要注意,如果您使用 type = 2,那么这将直接反映为 Mean Decrease in MSE。
如何查看不同的重要性度量 :
如果您希望查看 IncNodePurity,可以通过设置 type = 1,然后在绘图时确保读取正确的列。在使用 importance 函数时,可以通过 imp[, 2] 来选择 IncNodePurity。
示例代码
以下是如何在保持现有代码结构的同时获取不同重要性的计算:
r Copy code
# 在创建函数时传入 type = 1 或 2,依据定义从 importance 得到的相应列
create_rfplot <- function(rf, type, r_squared = NULL, p_value = NULL) {
# 计算特征重要性
imp <- importance(rf, type = type, scale = FALSE)
featureImportance <- data.frame(Feature = row.names(imp), Importance = imp[, 1])
# 选择IncNodePurity:使用 imp[, 2]作为X轴值
if (type == 1) {
featureImportance$Importance <- imp[, 2] # 获取IncNodePurity
}
# 其余代码...
}
# 调用绘图函数时
rf_output <- create_rfplot(rf1, type = 1, r_squared = r_squared, p_value = p_value) # 使用type = 1计算IncNodePurity
在上述示例代码中,您只需根据需要调整 type 的值,如果希望获取 Mean Decrease in MSE 则使用 type = 2,如果要获取 IncNodePurity 则将 type 设置为 1。
总结
X 轴表示的内容 : 默认情况下,importance 函数计算 H的特征重要性,通常是均方误差的变化(Mean Decrease in MSE)。
切换重要性度量 : 通过设置正确的 type 参数并在图中选择适当的列,确保所需要的度量能够正确反映在图形上。
Mean Decrease in MSE 和%IncMSE是一样的吗
Assistant2024-12-04 08:45 UTC Copy response
Mean Decrease in MSE 和 %IncMSE 是两个相关但不同的概念,在随机森林分析中,均用于评估特征的重要性。
1. Mean Decrease in MSE (均方误差降低的平均值)
2. %IncMSE (均方误差增加的百分比)
定义 : %IncMSE 是在随机森林中计算特征重要性的另一种方法。它表示在使用该特征时,模型的均方误差相较于仅用其他特征的预测能力提升的百分比。即在预测中,将某特征的值随机化(打乱),然后查看模型预测性能下降的比例。
计算方式 : 在计算 %IncMSE 时,首先计算没有该特征时模型的均方误差,然后用随机打乱该特征后的均方误差进行比较,得出损失的均方误差占原均方误差的百分比。这个值通常被认为更具解释性,因为它直接反映了在不使用某特征情况下模型性能的下降量。
总结:
相同点 :两者都是用来衡量特征在随机森林模型中的重要性。
不同点 :
Mean Decrease in MSE 是绝对值,表示特征的重要性具体数值,强调每个特征对降低预测错误的贡献。
%IncMSE 是相对值,考虑特征变动对预测性能的影响,强调特征在模型性能中的重要性占比。
实际使用:
在一般情况下,您在随机森林模型中使用的特征重要性度量,应该根据具体需求选择. Mean Decrease in MSE更常用于模型的特征选择决策,而%IncMSE` 则用于回答特征对总预测准确性影响的相对问题。
library(randomForest)
library(ggplot2)
library(extrafont)
创建函数
create_rfplot <- function(rf, type, significance_threshold = 0.05, r_squared = NULL, p_value = NULL) {
计算特征重要性
imp <- importance(rf, type = type, scale = FALSE)
featureImportance <- data.frame(Feature = row.names(imp), Importance = imp[, 1])
添加显著性标记
max_importance <- max(featureImportance$Importance)
featureImportance$Significance <- ""
用 else if 结构设置显著性标记
featureImportanceS i g n i f i c a n c e [ f e a t u r e I m p o r t a n c e Significance[featureImportance S i g ni f i c an ce [ f e a t u r e I m p or t an ce Importance > max_importance * 0.9] <- "**" # 三颗星
featureImportance Importance > max_importance * 0.7 &
featureImportance Significance[featureImportance Significance == ""] <- " " # 一颗星
Show full message · 113 lines Assistant2024-12-04 08:46 UTC Copy response
在大多数情况下,与随机森林相关的特征重要性是由 R 中的 importance 函数计算得出的。而在您的代码中,X 轴所表示的特征的重要性取决于您选择的 type 参数。
关于 importance 函数
默认情况下,importance(rf, type = 1) 计算的是 Mean Decrease in MSE (均方误差的减少量),即通过特征分裂所带来的均方误差的总体减少。
如果将 type 设置为 2,则结果会显示 Mean Decrease in Accuracy (即模型准确率的减少),通常是通过随机打乱特征的值所产生的平均增加误差。
X轴是否表示 %IncMSE
Mean Decrease in MSE 与 %IncMSE 的区别:
Mean Decrease in MSE 是绝对值,表示特征对模型改善影响的量化。
%IncMSE 则是相对量,通常表达为特征改变后,模型性能下降的百分比。这可以通过将均方误差或准确性做对比得出。
您的代码分析
在您的代码中,X 轴表示的是 Importance 列,该列是通过计算 importance(rf, type = 2) 获得的,因此它表示 Mean Decrease in Accuracy (模型准确性的减少),而不是 %IncMSE。至于是否将数值转换为百分比,这通常需要您自己实现一些额外的计算。
具体代码示例
如果您希望在 X 轴上显示特征的重要性为 %IncMSE,您需要将 type 参数设置为 2(或相应地对结果进行处理),确保您在 importance 函数中计算:
r Copy code
# 调用绘图函数并获取特征重要性
rf_output <- create_rfplot(rf1, type = 1, r_squared = r_squared, p_value = p_value) # type = 1表示Mean Decrease in MSE
总结
在您的代码中,X 轴的 Importance 表示的是 Mean Decrease in Accuracy (由 type = 2 计算的结果)。
如果您想要将结果显示为 %IncMSE ,请务必调整计算方式或使用合适的 type 参数。
library(randomForest)
library(ggplot2)
library(extrafont)
创建函数
create_rfplot <- function(rf, type, significance_threshold = 0.05, r_squared = NULL, p_value = NULL) {
计算特征重要性
imp <- importance(rf, type = type, scale = FALSE)
featureImportance <- data.frame(Feature = row.names(imp), Importance = imp[, 1])
添加显著性标记
max_importance <- max(featureImportance$Importance)
featureImportance$Significance <- ""
用 else if 结构设置显著性标记
featureImportanceS i g n i f i c a n c e [ f e a t u r e I m p o r t a n c e Significance[featureImportance S i g ni f i c an ce [ f e a t u r e I m p or t an ce Importance > max_importance * 0.9] <- "**" # 三颗星
featureImportance Importance > max_importance * 0.7 &
featureImportance Significance[featureImportance Significance == ""] <- " " # 一颗星
Show full message · 113 lines Assistant2024-12-04 08:48 UTC Copy response
要使 X 轴表示 %IncMSE(均方误差增加的百分比),您需要在调用 importance() 函数时确保它返回的特征重要性符合这一标准。
步骤概述:
选择正确的 type 参数 : 在 importance() 函数中使用 type = 1 以获得均方误差减少值。
计算 %IncMSE : 将均方误差的减少量转换为百分比。
调整图形绘制部分 : 根据新的值更新图标。
以下是修改后的示例代码,将 X 轴设为 %IncMSE:
Assistant2024-12-04 08:50 UTC Copy response
要确保在图中显示 ( R^2 ) 值和 P 值,并同时将 X 轴设置为 %IncMSE,需要确保以下几点:
在绘图函数中,确保您传递了 ( R^2 ) 和 P 值。
在注释部分将这些值添加到图中。
以下是完整的代码示例,经过调整以确保 ( R^2 ) 和 P 值在图中显示,同时将 X 轴设置为 %IncMSE:
library(randomForest)
library(ggplot2)
library(extrafont)
创建函数
create_rfplot <- function(rf, type, significance_threshold = 0.05, r_squared = NULL, p_value = NULL) {
计算特征重要性
imp <- importance(rf, type = type, scale = FALSE)
featureImportance <- data.frame(Feature = row.names(imp), Importance = imp[, 1])
添加显著性标记
max_importance <- max(featureImportance$Importance)
featureImportance$Significance <- ""
用 else if 结构设置显著性标记
featureImportanceS i g n i f i c a n c e [ f e a t u r e I m p o r t a n c e Significance[featureImportance S i g ni f i c an ce [ f e a t u r e I m p or t an ce Importance > max_importance * 0.9] <- "**" # 三颗星
featureImportance Importance > max_importance * 0.7 &
featureImportance Significance[featureImportance Significance == ""] <- " " # 一颗星
Show full message · 113 lines Assistant2024-12-04 08:51 UTC Copy response
要使 X 轴显示 IncNodePurity (节点纯度增加),您需要调整几个部分,以便从 importance 函数提取正确的值。IncNodePurity 是随机森林模型计算的另一种特征重要性指标,通常与特征分裂后所带来的模型纯度提升相关。
修改步骤:
调整 importance 函数的 type 参数 :将其设置为 1(表示均方误差数量减少),这样可以从中提取 IncNodePurity 的值。
在可视化时使用 IncNodePurity 值 。
下面是修改后的代码示例:
#随机森林分析DOC同位素------------------------------------------------------------------分类
library(randomForest)
library(ggplot2)
library(extrafont)
创建函数
create_rfplot <- function(rf, type, significance_threshold = 0.05, r_squared = NULL, p_value = NULL) {
计算特征重要性
imp <- importance(rf, type = type, scale = FALSE)
featureImportance <- data.frame(Feature = row.names(imp), Importance = imp[, 1])
添加显著性标记
max_importance <- max(featureImportance$Importance)
featureImportance$Significance <- ""
用 else if 结构设置显著性标记
featureImportanceS i g n i f i c a n c e [ f e a t u r e I m p o r t a n c e Significance[featureImportance S i g ni f i c an ce [ f e a t u r e I m p or t an ce Importance > max_importance * 0.9] <- "** " # 一颗星
Show full message · 115 lines Assistant2024-12-04 08:54 UTC Copy response
在您提供的代码中,X 轴表示 特征的重要性 (Importance),具体取决于您在调用 importance() 函数时选择的 type 参数。
具体分析:
您在 create_rfplot 函数中设置了 type = 2,这意味着您获取的是 均方误差的减少量 (Mean Decrease in MSE),也可以称为 特征对模型准确性的贡献 。换句话说,这个值越高,表示该特征在提高模型预测能力方面的贡献越大。
中文表示:
因此,您可以将 X 轴的标签翻译为:
特征重要性(均方误差减少量)
或者更具体一些,您可以写成 特征重要性(Mean Decrease in MSE) 。
代码中的修改:
如果您想在最终的图形中更改 X 轴的标签,可以在 create_rfplot 函数的 ggplot 部分添加以下代码:
r Copy code
# 创建图形
p <- ggplot(featureImportance, aes(x = reorder(Feature, Importance), y = Importance, fill = Category)) +
geom_bar(stat = "identity", width = 0.6, color = "black") +
scale_fill_manual(values = colors) + # 手动指定填充颜色
geom_text(aes(label = Significance), hjust = -0.5, size = 8) +
coord_flip() +
labs(x = "特征重要性(均方误差减少量)") + # 添加X轴标签
theme_light(base_size = 20) +
theme(
axis.title.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.title.y = element_blank(),
axis.text.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.text.y = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = c(0.8, 0.5), # 图例
legend.title = element_text(size = 12, face = "plain", family = "Times New Roman"), # 设置图例标题的样式
legend.text = element_text(size = 10, face = "plain", family = "Times New Roman") # 设置图例文本的样式
)
综上所述:
X 轴表示的内容可以总结为特征重要性(均方误差减少量) ,并可根据需要在图中添加相应的标签。希望这一解释对您有所帮助!
特征重要性(Mean Decrease in MSE) 有没有单位
Assistant2024-12-04 09:06 UTC Copy response
在随机森林分析中,特征重要性(Mean Decrease in MSE)通常没有单位。其主要原因如下:
理由:
计算方式 :
Mean Decrease in MSE 是通过计算模型预测的均方误差来得出的。在训练模型时,每个特征的使用导致的均方误差减少量会被记录下来。
由于均方误差本身是通过平方误差(即预测值与实际值之差的平方)来计算的,因此其值是表示误差的量度,通常单位是“平方单位”(例如, 如果因变量是 (DOC),则误差的单位是 (DOC^2))。
相对影响 :
特征重要性得分通常用于相对比较,而不是绝对数值。当比较不同特征对模型的贡献时,它们之间的相对大小(即哪个特征更重要)是更重要的,而不是其绝对数值。
单位的影响 :
由于特征重要性是通过比较模型性能变化得出的,具体的数值受输入数据的影响,因此如果输入数据的单位发生变化,随着单位变化特征重要性数值也将受到影响。
结论:
总的来说,特征重要性(Mean Decrease in MSE)没有默认为某种单位,而是一个相对度量,主要用于比较特征对模型的影响程度。如果您需要在报告中解释或描述这些值,可以提供上下文说明,但通常不需要附带单位。
如果需要在分析中进行具体的度量,重要的是关注结果的解释和比较,而不是其绝对数值或单位。
p <- ggplot(featureImportance, aes(x = reorder(Feature, Importance), y = Importance, fill = Category)) +
geom_bar(stat = "identity", width = 0.6, color = "black") +
scale_fill_manual(values = colors) + # 手动指定填充颜色
geom_text(aes(label = Significance), hjust = -0.5, size = 8) +
coord_flip() +
theme_light(base_size = 20) +
theme(
axis.title.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.title.y = element_blank(),
axis.text.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.text.y = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = c(0.8, 0.2), # 图例
legend.title = element_text(size = 12, face = "plain", family = "Times New Roman"), # 设置图例标题的样式
legend.text = element_text(size = 10, face = "plain", family = "Times New Roman") # 设置图例文本的样式
) 图例大小和图例文本大小怎么调整
Assistant2024-12-04 09:12 UTC Copy response
在 ggplot2 中,您可以通过修改图例相关的主题元素来调整图例的大小和文本大小。这些设置允许您自定义图例的外观,使其符合您的需求。
调整图例大小和文本大小的步骤
调整图例文本大小 :使用 legend.text 属性设置。
调整图例标题大小 :使用 legend.title 属性设置。
调整图例的填充和边距 :通过 legend.key.size 控制图例符号的大小。
调整图例位置 :可以通过 theme 函数的 legend.position 控制位置,但通常不影响大小。
示例代码
下面是您原始代码的修改版本,展示了如何进行这些调整:
r Copy code
p <- ggplot(featureImportance, aes(x = reorder(Feature, Importance), y = Importance, fill = Category)) +
geom_bar(stat = "identity", width = 0.6, color = "black") +
scale_fill_manual(values = colors) + # 手动指定填充颜色
geom_text(aes(label = Significance), hjust = -0.5, size = 8) +
coord_flip() +
theme_light(base_size = 20) +
theme(
axis.title.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.title.y = element_blank(),
axis.text.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.text.y = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = c(0.8, 0.2), # 图例位置
legend.title = element_text(size = 12, face = "plain", family = "Times New Roman"), # 设置图例标题的样式
legend.text = element_text(size = 10, face = "plain", family = "Times New Roman"), # 设置图例文本的样式
legend.key.size = unit(1, "cm") # 设置图例符号的大小
)
解释:
legend.key.size = unit(1, "cm") :此行代码设置图例符号的大小为 1 cm。您可以根据需要调整大小(更大的值会使符号更大)。
legend.title 和 legend.text :通过这些设置,您可以控制图例标题和文本的字形和规模。
调整示例:
图例符号的大小 : 如果希望图例符号更大,您可以将 unit(1, "cm") 改为更大的值,例如unit(1.5, "cm") 或 unit(2, "cm")。
图例文本的大小 : 根据需要当您调整字形时,也可以增大或减小 size 参数。
结尾:
通过在图形的 theme 函数中使用这些设置,您可以非常灵活地控制图例的外观。根据需要进行相应的调整,确保图形与报告或发表的要求一致。
Assistant2024-12-04 09:24 UTC Copy response
5.69669e-61 是科学记数法表示的数值,表示 ( 5.69669 \times 10^{-61} )。
转换为十进制
要转换这个数值为十进制形式,可以将 5.69669 乘以 ( 10^{-61} ),这意味着它将向左移动小数点 61 位。具体步骤如下:
5.69669e-61 等于 0.00000000000000000000000000000000000000000000000000000000000000000000569669
这个值的十进制形式非常接近于零。
在科学和统计中的意义
这样的 P 值通常指示您观察到结果的概率非常小,意味着结果在统计上是极为显著的。通常在假设检验中,这样的小 P 值可以被认为是具有高度的统计显著性。
如果您需要对这个值的具体含义进行解释或分析,通常是在上下文中,比如统计测试结果。
p < 0.001 在AI软件中 我只想让p斜体, 而< 0.001不是斜体,怎么设置
Assistant2024-12-04 09:31 UTC Copy response
在 R 中,如果您希望在图形中使用 ggplot2 显示部分文本为斜体,您可以使用 parse 函数来解析表达式,并调整标签的格式。具体来说,您可以通过以下方式来实现,使得 P 值部分以斜体格式显示,而小于号和数字部分保持为正常格式。
示例代码
以下是如何实现这个要求的示例:
r Copy code
# 假设您想在 annotate 中添加 P 值文本
p_value <- 0.001 # P 值示例
# ggplot 的绘图部分
p + annotate("text", x = x_position, y = y_position,
label = bquote('P <' ~ italic(0.001)), size = 6, hjust = 0)
关键点:
bquote 函数 : 该函数用于生成包含 R 计算结果和表达式的列。在这个例子中,'P <' 表示文本的一部分,而 italic(0.001) 是将 0.001 转换为斜体。
~ 运算符 : 在 bquote 中,~ 用于在文本的不同部分之间创建空间。
完整示例:
如果放入到您的绘图代码片段中,可能看起来是这样的:
r Copy code
p <- ggplot(...) + # 假设这里是您的 ggplot 代码
# 其他图形设置
# 假设您在图中添加了 P 值的注释
p + annotate("text", x = 1, y = 0.8, # 示例的 X 和 Y 位置
label = bquote('P <' ~ italic(0.001)), size = 6, hjust = 0)
解释:
x = 1 和 y = 0.8 是您可以根据需要调整的文本位置。
size 控制文本的大小,hjust 控制文本的水平对齐。
使用这种方式,您可以轻松地在图形中显示 P 值的一部分为斜体,而其他部分保持正常格式。希望这对您有帮助!
添加R方和P值到图中
if (!is.null(r_squared) && !is.null(p_value)) {
p <- p +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportanceI m p o r t a n c e ) ∗ 0.9 , l a b e l = p a s t e ( " R 2 = " , r o u n d ( r s q u a r e d , 3 ) ) , s i z e = 6 , h j u s t = 0 , f a c e = " p l a i n " , f a m i l y = " T i m e s N e w R o m a n " ) + a n n o t a t e ( " t e x t " , x = n r o w ( f e a t u r e I m p o r t a n c e ) ∗ 0.7 , y = m a x ( f e a t u r e I m p o r t a n c e Importance) * 0.9,
label = paste("R² =", round(r_squared, 3)), size = 6, hjust = 0, face = "plain", family = "Times New Roman") +
annotate("text", x = nrow(featureImportance) * 0.7, y = max(featureImportance I m p or t an ce ) ∗ 0.9 , l ab e l = p a s t e ( " R 2 = " , r o u n d ( r Importance) * 0.85,
label = paste("P =", format(p_value, scientific = TRUE)), size = 6, hjust = 0, face = "plain", family = "Times New Roman")
} 让p值斜体怎么修改代码
Assistant2024-12-04 09:34 UTC Copy response
要让 P 值以斜体形式显示,同时保持 "P =" 不变,您可以使用 bquote 函数和 italic 函数将 P 值部分处理为斜体。这种方法允许您对文本进行更细粒度的控制。
以下是您的代码修改后的示例,确保 P 值以斜体形式呈现:
r Copy code
# 添加R方和P值到图中
if (!is.null(r_squared) && !is.null(p_value)) {
p <- p +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportance$Importance) * 0.9,
label = paste("R² =", round(r_squared, 3)), size = 6, hjust = 0,
face = "plain", family = "Times New Roman") +
annotate("text", x = nrow(featureImportance) * 0.7, y = max(featureImportance$Importance) * 0.85,
label = bquote('P =' ~ italic(.(format(p_value, scientific = TRUE)))), size = 6,
hjust = 0, family = "Times New Roman")
}
代码中的关键点:
bquote : 允许您使用 R 对象(例如计算的 P 值)与表达式结合。在 bquote 中,'P =' 是普通文本,italic(.(format(p_value, scientific = TRUE))) 将 P 值格式化为科学记数法并设置为斜体。
.(...) : 这个语法用于在 bquote 内部插入 R 的对象,确保格式化的 P 值可以直接放入文本中。
完整示例:
假定下面是整个绘图代码片段,显示了如何设置图形和添加 R² 及 P 值的注释。
print(rf_output$plot)
警告信息:
In is.na(x) : is.na()不适用于类别为'language'的非列表或非向量
Assistant2024-12-04 09:35 UTC Copy response
您遇到的警告信息 "In is.na(x) : is.na()不适用于类别为'language'的非列表或非向量" 通常是由于在使用 bquote 和 italic 函数时某个部分的格式不正确,导致无法正确解析。
常见原因
这可能是因为 bquote 函数中的对象(例如 P 值)被转换为另一个不适当的类型,或者因为输入错误的值格式。
解决方案
我们可以采取以下步骤来解决问题:
确保p_value 是有效的数值 :在调用 bquote 时,确保 p_value 是合适的数值类型(可以使用 as.numeric())。
避免使用 .(...) :直接将 P 值传入 bquote,而无需使用 .(...),因为它会从原始上下文中解析值。
修改后的代码示例
请尝试使用下面的修改后的代码:
r Copy code
# 添加R方和P值到图中
if (!is.null(r_squared) && !is.null(p_value)) {
p <- p +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportance$Importance) * 0.9,
label = paste("R² =", round(r_squared, 3)), size = 6, hjust = 0,
face = "plain", family = "Times New Roman") +
annotate("text", x = nrow(featureImportance) * 0.7, y = max(featureImportance$Importance) * 0.85,
label = bquote(P == italic(.(format(p_value, scientific = TRUE)))), size = 6,
hjust = 0, family = "Times New Roman")
}
解释:
在 bquote(P == italic(.(format(p_value, scientific = TRUE)))) 中,P 将保持为普通文本,italic(.(format(p_value, scientific = TRUE))) 将把 P 值部分设为斜体。
这样更改可以帮助确保在图形中没有类型冲突,从而避免 is.na() 警告。
运行代码:
请再次尝试运行所有更新后的代码,保持 p_value 为有效数值。如果您使用的是随机森林模型的预测中的合适 P 值,无需担心直接传递格式。在上下文中,确保相关数据和输入是有效的,试运行后您应该可以看到所需的图形输出。
if (!is.null(r_squared) && !is.null(p_value)) {
p <- p +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportanceI m p o r t a n c e ) ∗ 0.9 , l a b e l = p a s t e ( " R 2 = " , r o u n d ( r s q u a r e d , 3 ) ) , s i z e = 6 , h j u s t = 0 , f a c e = " p l a i n " , f a m i l y = " T i m e s N e w R o m a n " ) + a n n o t a t e ( " t e x t " , x = n r o w ( f e a t u r e I m p o r t a n c e ) ∗ 0.7 , y = m a x ( f e a t u r e I m p o r t a n c e Importance) * 0.9,
label = paste("R² =", round(r_squared, 3)), size = 6, hjust = 0, face = "plain", family = "Times New Roman") +
annotate("text", x = nrow(featureImportance) * 0.7, y = max(featureImportance I m p or t an ce ) ∗ 0.9 , l ab e l = p a s t e ( " R 2 = " , r o u n d ( r Importance) * 0.85,
label = paste("p =", format(p_value, scientific = TRUE)), size = 6, hjust = 0, face = "plain", family = "Times New Roman")
}
我想让代码中的p这个字母斜体 而数值不斜体
Assistant2024-12-04 09:37 UTC Copy response
要在 ggplot2 中实现只将 "p" 字母格式化为斜体,而保持随后的数值部分为正常格式,您可以使用 bquote 函数和 italic 组合。以下是如何调整代码以符合这一要求的示例:
r Copy code
# 添加R方和P值到图中
if (!is.null(r_squared) && !is.null(p_value)) {
p <- p +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportance$Importance) * 0.9,
label = paste("R² =", round(r_squared, 3)), size = 6, hjust = 0, face = "plain", family = "Times New Roman") +
annotate("text", x = nrow(featureImportance) * 0.7, y = max(featureImportance$Importance) * 0.85,
label = bquote(italic(p) == .(format(p_value, scientific = TRUE))), size = 6, hjust = 0,
face = "plain", family = "Times New Roman")
}
解释:
bquote :
italic(p) 将字母 "p" 格式化为斜体。
.(format(p_value, scientific = TRUE)) 将 P 值正常呈现,不使用斜体。
== 在这里用于连接 "p" 和数值部分,使整个文本看起来连贯。
输出效果:
运行上述代码后,您将在图中看到 "p" 字母以斜体形式显示,而后面的 P 值保持为正常字体样式。这样,您就可以在表达统计信息时更具视觉效果。
Not yet included in a published release.
Turns (29) 1 读取数据 请根据您的数据文件路径进行调整 data = 0.05] = 0.01] = 0.001] <- "" dfpre$sig[dfpre$sig == 2 4.401159e-52 =多少 3 那怎么把计算出的R方 和p值放在图中 4 读取数据 请根据您的数据文件路径进行调整 data = 0.05] = 0.01] = 0.001] <- "" dfpre$sig[dfpre$sig == 5 library(randomForest) library(ggplot2) library(extrafont) 创建函数 createrfplot maxi 6 p <- p + annotate("text", x = nrow(featureImportance) 0.8, y = max(featureImport 7 label = paste("P =", format(pvalue, scientific = TRUE)), size = 6, hjust = 0) hj 8 annotate("text", x = nrow(featureImportance) 0.8, y = max(featureImportance$Impo 9 p <- p + annotate("text", x = nrow(featureImportance) 0.8, y = max(featureImport 10 创建函数 createrfplot maximportance 0.9] maximportance 0.7 & featureImportance$Signi 11 library(randomForest) library(ggplot2) library(extrafont) 创建函数 createrfplot maxi 12 rfoutput % mutate(PValue = pvalues): 没有"%>%"这个函数 13 用 else if 结构设置显著性标记 featureImportance$Significance[featureImportance$Importance 14 那么随机森林分析在标注显著性星号的时候是根据重要性 还是p-值呢 15 library(randomForest) library(ggplot2) library(extrafont) 创建函数 createrfplot maxi 16 library(randomForest) library(ggplot2) library(extrafont) 创建函数 createrfplot maxi 17 Mean Decrease in MSE 和%IncMSE是一样的吗 18 library(randomForest) library(ggplot2) library(extrafont) 创建函数 createrfplot maxi 19 library(randomForest) library(ggplot2) library(extrafont) 创建函数 createrfplot maxi 20 可是你给的代码 R方和R方的p值不显示了 21 library(randomForest) library(ggplot2) library(extrafont) 创建函数 createrfplot maxi 22 #随机森林分析DOC同位素------------------------------------------------------------------分 23 特征重要性(Mean Decrease in MSE) 有没有单位 24 p <- ggplot(featureImportance, aes(x = reorder(Feature, Importance), y = Importa 25 5.69669e-61 =多少 26 p < 0.001 在AI软件中 我只想让p斜体, 而< 0.001不是斜体,怎么设置 27 添加R方和P值到图中 if (!is.null(rsquared) && !is.null(pvalue)) { p <- p + annotate("text 28 print(rfoutput$plot) 警告信息: In is.na(x) : is.na()不适用于类别为'language'的非列表或非向量 29 if (!is.null(rsquared) && !is.null(pvalue)) { p <- p + annotate("text", x = nrow s
i
z
e
=
1.05 , ba s e r ec t s i z e =
1.05 ) +
l ab s ( y = ′
I n cr e a se in M S E ( g eo m t e x t ( a es ( l ab e l =
d f p r e
在这个代码中,我们首先使用随机森林模型得到预测值。然后,我们计算 ( R^2 ) 值和基于线性回归模型的 P 值。最后将 ( R^2 ) 值和 P 值输出到控制台中。这样,您就可以按照您的要求计算并查看相关指标了。
r
e
s i g [ d f p r e sig[df_pre s i g [ d f p r e s i g [ d f p r e sig[df_pre s i g [ d f p r e I n c N o d e P u r i t y ) + t h e m e b w ( ba s e l in e s i z e = 1.05 , ba s e r ec t s i z e = 1.05 ) + l ab s ( y = ′ I n cr e a se in M S E ( g eo m t e x t ( a es ( l ab e l = d f p r e
我们首先计算 ( R^2 ) 和 P 值。
使用 annotate 函数将 ( R^2 ) 和 P 值添加到柱状图中。您可以根据需要调整文本的位置,以确保它在图中清晰可见。
最后,运行修改后的代码,您应该看到柱状图,并在图中标注出 ( R^2 ) 和相应的 P 值。
S i g n i f i c a n c e [ f e a t u r e I m p o r t a n c e Significance[featureImportance S i g ni f i c an ce [ f e a t u r e I m p or t an ce
Significance == ""] <- "**" # 两颗星
featureImportance
Importance > max_importance * 0.5 &
featureImportance
添加特征类别 featureImportanceC a t e g o r y < − " " f e a t u r e I m p o r t a n c e Category <- ""
featureImportance C a t e g or y < − "" f e a t u r e I m p or t an ce Category[featureImportanceF e a t u r e f e a t u r e I m p o r t a n c e Feature %in% c("precipitation", "rainfallPOC")] <- "Rainfall Features"
featureImportance F e a t u r e f e a t u r e I m p or t an ce Category[featureImportanceF e a t u r e f e a t u r e I m p o r t a n c e Feature %in% c("velocity", "WT", "DO", "streampH")] <- "Stream Hydrochemical Features"
featureImportance F e a t u r e f e a t u r e I m p or t an ce Category[featureImportanceF e a t u r e f e a t u r e I m p o r t a n c e Feature %in% c("TClitter_stream", "TClitter_shore")] <- "Litter Features"
featureImportance F e a t u r e f e a t u r e I m p or t an ce Category[featureImportanceF e a t u r e " l e a c h a t e T N " , " l e a c h a t e D O C " , " l e a c h a t e P O C " ) ] < − " S o i l F e a t u r e s " f e a t u r e I m p o r t a n c e Feature %in% c("soilTN", "soilTP", "leachatepH", "leachate_amount",
"leachateTN", "leachateDOC", "leachatePOC")] <- "Soil Features"
featureImportance F e a t u r e " l e a c Category[featureImportance$Feature %in% c("sedimentTC", "sedimentTN")] <- "Sediment Features"
定义颜色 colors <- c(
"Rainfall Features" = "#519D78",
"Stream Hydrochemical Features" = "#8BCF8B",
"Litter Features" = "#AADCA9",
"Soil Features" = "#C4E9CA",
"Sediment Features" = "#DDF3DE"
)
确保 Feature 是因子,并设置因子的顺序 featureImportanceF e a t u r e < − f a c t o r ( f e a t u r e I m p o r t a n c e Feature <- factor(featureImportance F e a t u r e < − f a c t or ( f e a t u r e I m p or t an ce Feature, levels = featureImportanceF e a t u r e ) f e a t u r e I m p o r t a n c e Feature)
featureImportance F e a t u r e ) f e a t u r e I m p or t an ce Category <- factor(featureImportance$Category) # 确保类别是因子
创建图形 p <- ggplot(featureImportance, aes(x = reorder(Feature, Importance), y = Importance, fill = Category)) +
geom_bar(stat = "identity", width = 0.6, color = "black") +
scale_fill_manual(values = colors) + # 手动指定填充颜色
geom_text(aes(label = Significance), hjust = -0.5, size = 8) +
coord_flip() +
theme_light(base_size = 20) +
theme(
axis.title.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.title.y = element_blank(),
axis.text.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.text.y = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = c(0.8, 0.5), # 图例
legend.title = element_text(size = 12, face = "plain", family = "Times New Roman"), # 设置图例标题的样式
legend.text = element_text(size = 10, face = "plain", family = "Times New Roman") # 设置图例文本的样式
)
return(list(plot = p, featureImportance = featureImportance)) # 返回图形和特征重要性数据框
}
设置随机种子
读取数据(此处需要您本地存在的 CSV 文件) mydata <- read.csv("C14.csv")
进行随机森林分析 rf1 <- randomForest(
POC ~ precipitation + rainfallPOC + velocity + WT + DO +
streampH + TClitter_stream + TClitter_shore +
soilTN + soilTP + leachatepH + leachate_amount +
leachateTN + leachateDOC + leachatePOC +
sedimentTC + sedimentTN,
ntree = 400,
data = mydata,
nodesize = 1,
replace = FALSE,
importance = TRUE
)
调用绘图函数并获取特征重要性 rf_output <- create_rfplot(rf1, type = 2)
提取并查看特征重要性 featureImportance <- rf_output$featureImportance
print(featureImportance)
绘图 print(rf_output$plot) 我想计算这个模型的R方 以及R方的p值,并且把R方 以及R方的p值标注在图中,怎么修改代码
library(randomForest)
library(ggplot2)
library(extrafont)
# 创建函数
create_rfplot <- function(rf, type, significance_threshold = 0.05, r_squared = NULL, p_value = NULL) {
# 计算特征重要性
imp <- importance(rf, type = type, scale = FALSE)
featureImportance <- data.frame(Feature = row.names(imp), Importance = imp[, 1])
# 添加显著性标记
max_importance <- max(featureImportance$Importance)
featureImportance$Significance <- ""
# 用 else if 结构设置显著性标记
featureImportance$Significance[featureImportance$Importance > max_importance * 0.9] <- "***" # 三颗星
featureImportance$Significance[featureImportance$Importance > max_importance * 0.7 &
featureImportance$Significance == ""] <- "**" # 两颗星
featureImportance$Significance[featureImportance$Importance > max_importance * 0.5 &
featureImportance$Significance == ""] <- "*" # 一颗星
print(featureImportance)
# 添加特征类别
featureImportance$Category <- ""
featureImportance$Category[featureImportance$Feature %in% c("precipitation", "rainfallPOC")] <- "Rainfall Features"
featureImportance$Category[featureImportance$Feature %in% c("velocity", "WT", "DO", "streampH")] <- "Stream Hydrochemical Features"
featureImportance$Category[featureImportance$Feature %in% c("TClitter_stream", "TClitter_shore")] <- "Litter Features"
featureImportance$Category[featureImportance$Feature %in% c("soilTN", "soilTP", "leachatepH", "leachate_amount",
"leachateTN", "leachateDOC", "leachatePOC")] <- "Soil Features"
featureImportance$Category[featureImportance$Feature %in% c("sedimentTC", "sedimentTN")] <- "Sediment Features"
# 定义颜色
colors <- c(
"Rainfall Features" = "#519D78",
"Stream Hydrochemical Features" = "#8BCF8B",
"Litter Features" = "#AADCA9",
"Soil Features" = "#C4E9CA",
"Sediment Features" = "#DDF3DE"
)
# 确保 Feature 是因子,并设置因子的顺序
featureImportance$Feature <- factor(featureImportance$Feature, levels = featureImportance$Feature)
featureImportance$Category <- factor(featureImportance$Category) # 确保类别是因子
# 创建图形
p <- ggplot(featureImportance, aes(x = reorder(Feature, Importance), y = Importance, fill = Category)) +
geom_bar(stat = "identity", width = 0.6, color = "black") +
scale_fill_manual(values = colors) + # 手动指定填充颜色
geom_text(aes(label = Significance), hjust = -0.5, size = 8) +
coord_flip() +
theme_light(base_size = 20) +
theme(
axis.title.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.title.y = element_blank(),
axis.text.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.text.y = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = c(0.8, 0.5), # 图例
legend.title = element_text(size = 12, face = "plain", family = "Times New Roman"), # 设置图例标题的样式
legend.text = element_text(size = 10, face = "plain", family = "Times New Roman") # 设置图例文本的样式
)
# 添加R方和P值到图中
if (!is.null(r_squared) && !is.null(p_value)) {
p <- p +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportance$Importance) * 0.9,
label = paste("R² =", round(r_squared, 3)), size = 6, hjust = 0) +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportance$Importance) * 0.85,
label = paste("P =", format(p_value, scientific = TRUE)), size = 6, hjust = 0)
}
return(list(plot = p, featureImportance = featureImportance)) # 返回图形和特征重要性数据框
}
# 设置随机种子
set.seed(123)
# 读取数据(此处需要您本地存在的 CSV 文件)
mydata <- read.csv("C14.csv")
# 进行随机森林分析
rf1 <- randomForest(
POC ~ precipitation + rainfallPOC + velocity + WT + DO +
streampH + TClitter_stream + TClitter_shore +
soilTN + soilTP + leachatepH + leachate_amount +
leachateTN + leachateDOC + leachatePOC +
sedimentTC + sedimentTN,
ntree = 400,
data = mydata,
nodesize = 1,
replace = FALSE,
importance = TRUE
)
# 计算R方和P值
predictions <- predict(rf1, mydata)
r_squared <- cor(mydata$POC, predictions)^2
lm_model <- lm(mydata$POC ~ predictions)
summary_lm <- summary(lm_model)
p_value <- summary_lm$coefficients[2, 4] # 获取自变量(predictions)的P值
# 调用绘图函数并获取特征重要性
rf_output <- create_rfplot(rf1, type = 2, r_squared = r_squared, p_value = p_value)
# 提取并查看特征重要性
featureImportance <- rf_output$featureImportance
print(featureImportance)
# 绘图
print(rf_output$plot)
修改的关键点:
在 create_rfplot 函数中添加 r_squared 和 p_value 参数。
在图中使用 annotate 函数显示 ( R^2 ) 和 P 值。
在主程序中计算 ( R^2 ) 和 P 值,并将结果传递给 create_rfplot 函数。
运行这个完整的代码后,您应该能够看到绘制的柱状图,并且图中标注了 ( R^2 ) 和 P 值。
s
q
u
a
r
e
d
,
3
))
,
s
i
z
e
=
6 , hj u s t =
0 ) +
ann o t a t e ( " t e x t " , x =
n r o w ( f e a t u r e I m p or t an ce ) ∗
0.8 , y =
ma x ( f e a t u r e I m p or t an ce
s
q
u
a
r
e
d
,
3
))
,
s
i
z
e
=
6 , hj u s t =
0 ) +
ann o t a t e ( " t e x t " , x =
n r o w ( f e a t u r e I m p or t an ce ) ∗
0.8 , y =
ma x ( f e a t u r e I m p or t an ce
r e I m p o r t a n c e Importance) * 0.9,
label = paste("R² =", round(r_squared, 3)), size = 6, hjust = 0) +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportance I m p or t an ce ) ∗ 0.9 , l ab e l = p a s t e ( " R 2 = " , r o u n d ( r s q u a r e d , 3 )) , s i z e = 6 , hj u s t = 0 ) + ann o t a t e ( " t e x t " , x = n r o w ( f e a t u r e I m p or t an ce ) ∗ 0.8 , y = ma x ( f e a t u r e I m p or t an ce
S i g n i f i c a n c e [ f e a t u r e I m p o r t a n c e Significance[featureImportance S i g ni f i c an ce [ f e a t u r e I m p or t an ce
Significance == ""] <- "**" # 两颗星
featureImportance
Importance > max_importance * 0.5 &
featureImportance
添加特征类别 featureImportanceC a t e g o r y < − " " f e a t u r e I m p o r t a n c e Category <- ""
featureImportance C a t e g or y < − "" f e a t u r e I m p or t an ce Category[featureImportanceF e a t u r e f e a t u r e I m p o r t a n c e Feature %in% c("precipitation", "rainfallPOC")] <- "Rainfall Features"
featureImportance F e a t u r e f e a t u r e I m p or t an ce Category[featureImportanceF e a t u r e f e a t u r e I m p o r t a n c e Feature %in% c("velocity", "WT", "DO", "streampH")] <- "Stream Hydrochemical Features"
featureImportance F e a t u r e f e a t u r e I m p or t an ce Category[featureImportanceF e a t u r e f e a t u r e I m p o r t a n c e Feature %in% c("TClitter_stream", "TClitter_shore")] <- "Litter Features"
featureImportance F e a t u r e f e a t u r e I m p or t an ce Category[featureImportanceF e a t u r e " l e a c h a t e T N " , " l e a c h a t e D O C " , " l e a c h a t e P O C " ) ] < − " S o i l F e a t u r e s " f e a t u r e I m p o r t a n c e Feature %in% c("soilTN", "soilTP", "leachatepH", "leachate_amount",
"leachateTN", "leachateDOC", "leachatePOC")] <- "Soil Features"
featureImportance F e a t u r e " l e a c Category[featureImportance$Feature %in% c("sedimentTC", "sedimentTN")] <- "Sediment Features"
定义颜色 colors <- c(
"Rainfall Features" = "#519D78",
"Stream Hydrochemical Features" = "#8BCF8B",
"Litter Features" = "#AADCA9",
"Soil Features" = "#C4E9CA",
"Sediment Features" = "#DDF3DE"
)
确保 Feature 是因子,并设置因子的顺序 featureImportanceF e a t u r e < − f a c t o r ( f e a t u r e I m p o r t a n c e Feature <- factor(featureImportance F e a t u r e < − f a c t or ( f e a t u r e I m p or t an ce Feature, levels = featureImportanceF e a t u r e ) f e a t u r e I m p o r t a n c e Feature)
featureImportance F e a t u r e ) f e a t u r e I m p or t an ce Category <- factor(featureImportance$Category) # 确保类别是因子
创建图形 p <- ggplot(featureImportance, aes(x = reorder(Feature, Importance), y = Importance, fill = Category)) +
geom_bar(stat = "identity", width = 0.6, color = "black") +
scale_fill_manual(values = colors) + # 手动指定填充颜色
geom_text(aes(label = Significance), hjust = -0.5, size = 8) +
coord_flip() +
theme_light(base_size = 20) +
theme(
axis.title.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.title.y = element_blank(),
axis.text.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.text.y = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = c(0.8, 0.5), # 图例
legend.title = element_text(size = 12, face = "plain", family = "Times New Roman"), # 设置图例标题的样式
legend.text = element_text(size = 10, face = "plain", family = "Times New Roman") # 设置图例文本的样式
)
添加R方和P值到图中 if (!is.null(r_squared) && !is.null(p_value)) {
p <- p +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportanceI m p o r t a n c e ) ∗ 0.9 , l a b e l = p a s t e ( " R 2 = " , r o u n d ( r s q u a r e d , 3 ) ) , s i z e = 6 , h j u s t = 0 ) + a n n o t a t e ( " t e x t " , x = n r o w ( f e a t u r e I m p o r t a n c e ) ∗ 0.5 , y = m a x ( f e a t u r e I m p o r t a n c e Importance) * 0.9,
label = paste("R² =", round(r_squared, 3)), size = 6, hjust = 0) +
annotate("text", x = nrow(featureImportance) * 0.5, y = max(featureImportance I m p or t an ce ) ∗ 0.9 , l ab e l = p a s t e ( " R 2 = " , r o u n d ( r Importance) * 0.85,
label = paste("P =", format(p_value, scientific = TRUE)), size = 6, hjust = 0)
}
return(list(plot = p, featureImportance = featureImportance)) # 返回图形和特征重要性数据框
}
设置随机种子
读取数据(此处需要您本地存在的 CSV 文件) mydata <- read.csv("C14.csv")
进行随机森林分析 rf1 <- randomForest(
POC ~ precipitation + rainfallPOC + velocity + WT + DO +
streampH + TClitter_stream + TClitter_shore +
soilTN + soilTP + leachatepH + leachate_amount +
leachateTN + leachateDOC + leachatePOC +
sedimentTC + sedimentTN,
ntree = 400,
data = mydata,
nodesize = 1,
replace = FALSE,
importance = TRUE
)
计算R方和P值 predictions <- predict(rf1, mydata)
r_squared <- cor(mydataP O C , p r e d i c t i o n s ) 2 l m m o d e l < − l m ( m y d a t a POC, predictions)^2
lm_model <- lm(mydata P O C , p r e d i c t i o n s ) 2 l m m o d e l < − l m ( m y d a t a POC ~ predictions)
summary_lm <- summary(lm_model)
p_value <- summary_lm$coefficients[2, 4] # 获取自变量(predictions)的P值
调用绘图函数并获取特征重要性 rf_output <- create_rfplot(rf1, type = 2, r_squared = r_squared, p_value = p_value)
提取并查看特征重要性 featureImportance <- rf_output$featureImportance
print(featureImportance)
绘图 print(rf_outputplot) 这串代码的显著性我想要让他根据这个来显示:# 合并显著因子和解释率表,标记显著性
df_pre <- cbind(predictor_var, predictor_sig)
df_pre sig[df_pres i g > = 0.05 ] < − 0 d f p r e sig >= 0.05] <- 0
df_pre s i g >= 0.05 ] < − 0 d f p r e sig[df_pres i g > = 0.01 ] < − " ∗ " d f p r e sig >= 0.01] <- "*"
df_pre s i g >= 0.01 ] < − " ∗ " d f p r e sig[df_pres i g > = 0.001 ] < − " ∗ ∗ " d f p r e sig >= 0.001] <- "**"
df_pre s i g >= 0.001 ] < − " ∗ ∗ " d f p r e sig[df_pre$sig == 0] <- ""
library(randomForest)
library(ggplot2)
# 创建函数
create_rfplot <- function(rf, type, significance_threshold = 0.05, r_squared = NULL, p_value = NULL) {
# 计算特征重要性
imp <- importance(rf, type = type, scale = FALSE)
featureImportance <- data.frame(Feature = row.names(imp), Importance = imp[, 1])
# 计算显著性
max_importance <- max(featureImportance$Importance)
p_values <- c(NA, runif(nrow(featureImportance)-1, 0, 0.1)) # 示例随机p值,实际应由具体模型计算
predictor_sig <- ifelse(p_values >= 0.05, 0,
ifelse(p_values >= 0.01, "*",
ifelse(p_values >= 0.001, "**", "")))
# 将显著性合并到特征重要性数据框中
featureImportance$Significance <- predictor_sig
print(featureImportance)
# 添加特征类别
featureImportance$Category <- ""
featureImportance$Category[featureImportance$Feature %in% c("precipitation", "rainfallPOC")] <- "Rainfall Features"
featureImportance$Category[featureImportance$Feature %in% c("velocity", "WT", "DO", "streampH")] <- "Stream Hydrochemical Features"
featureImportance$Category[featureImportance$Feature %in% c("TClitter_stream", "TClitter_shore")] <- "Litter Features"
featureImportance$Category[featureImportance$Feature %in% c("soilTN", "soilTP", "leachatepH", "leachate_amount",
"leachateTN", "leachateDOC", "leachatePOC")] <- "Soil Features"
featureImportance$Category[featureImportance$Feature %in% c("sedimentTC", "sedimentTN")] <- "Sediment Features"
# 定义颜色
colors <- c(
"Rainfall Features" = "#519D78",
"Stream Hydrochemical Features" = "#8BCF8B",
"Litter Features" = "#AADCA9",
"Soil Features" = "#C4E9CA",
"Sediment Features" = "#DDF3DE"
)
# 确保 Feature 是因子,并设置因子的顺序
featureImportance$Feature <- factor(featureImportance$Feature, levels = featureImportance$Feature)
featureImportance$Category <- factor(featureImportance$Category) # 确保类别是因子
# 创建图形
p <- ggplot(featureImportance, aes(x = reorder(Feature, Importance), y = Importance, fill = Category)) +
geom_bar(stat = "identity", width = 0.6, color = "black") +
scale_fill_manual(values = colors) + # 手动指定填充颜色
geom_text(aes(label = Significance), hjust = -0.5, size = 8) +
coord_flip() +
theme_light(base_size = 20) +
theme(
axis.title.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.title.y = element_blank(),
axis.text.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.text.y = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = c(0.8, 0.5),
legend.title = element_text(size = 12, face = "plain", family = "Times New Roman"),
legend.text = element_text(size = 10, face = "plain", family = "Times New Roman")
)
# 添加R方和P值到图中
if (!is.null(r_squared) && !is.null(p_value)) {
p <- p +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportance$Importance) * 0.9,
label = paste("R² =", round(r_squared, 3)), size = 6, hjust = 0) +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportance$Importance) * 0.83,
label = paste("P =", format(p_value, scientific = TRUE)), size = 6, hjust = 0)
}
return(list(plot = p, featureImportance = featureImportance)) # 返回图形和特征重要性数据框
}
# 设置随机种子
set.seed(123)
# 读取数据(此处需要您本地存在的 CSV 文件)
mydata <- read.csv("C14.csv")
# 进行随机森林分析
rf1 <- randomForest(
POC ~ precipitation + rainfallPOC + velocity + WT + DO +
streampH + TClitter_stream + TClitter_shore +
soilTN + soilTP + leachatepH + leachate_amount +
leachateTN + leachateDOC + leachatePOC +
sedimentTC + sedimentTN,
ntree = 400,
data = mydata,
nodesize = 1,
replace = FALSE,
importance = TRUE
)
# 计算R方和P值
predictions <- predict(rf1, mydata)
r_squared <- cor(mydata$POC, predictions)^2
lm_model <- lm(mydata$POC ~ predictions)
summary_lm <- summary(lm_model)
p_value <- summary_lm$coefficients[2, 4] # 获取自变量(predictions)的P值
# 调用绘图函数并获取特征重要性
rf_output <- create_rfplot(rf1, type = 2, r_squared = r_squared, p_value = p_value)
# 提取并查看特征重要性
featureImportance <- rf_output$featureImportance
print(featureImportance)
# 绘图
print(rf_output$plot)
关键改动
显著性标记的计算 :通过示例生成随机 p 值来标记显著性。这部分代码中的 p_values 生成逻辑应替换为实际计算的 p 值,然后通过 ifelse 语句分配显著性标记(Significance 列)。在实际情况下,您应该根据您的分析计算这些 p 值,可能来自于其他模型或统计分析结果。
将 Significance 合并到 featureImportance 数据框 :将显著性标记添加到特征重要性数据框,使得在绘图时显示出来。
请根据您的数据集和分析需求适当调整 p 值生成的部分,确保 predictor_sig 变量包含了真实的显著性计算结果。运行上述代码,您应该能够看到更新后的图形,且显著性根据您的要求得到了标记。
S i g n i f i c a n c e [ f e a t u r e I m p o r t a n c e Significance[featureImportance S i g ni f i c an ce [ f e a t u r e I m p or t an ce Significance == ""] <- "**" # 两颗星
featureImportance
Importance > max_importance * 0.5 &
featureImportance
添加特征类别 featureImportanceC a t e g o r y < − " " f e a t u r e I m p o r t a n c e Category <- ""
featureImportance C a t e g or y < − "" f e a t u r e I m p or t an ce Category[featureImportanceF e a t u r e f e a t u r e I m p o r t a n c e Feature %in% c("precipitation", "rainfallPOC")] <- "Rainfall Features"
featureImportance F e a t u r e f e a t u r e I m p or t an ce Category[featureImportanceF e a t u r e f e a t u r e I m p o r t a n c e Feature %in% c("velocity", "WT", "DO", "streampH")] <- "Stream Hydrochemical Features"
featureImportance F e a t u r e f e a t u r e I m p or t an ce Category[featureImportanceF e a t u r e f e a t u r e I m p o r t a n c e Feature %in% c("TClitter_stream", "TClitter_shore")] <- "Litter Features"
featureImportance F e a t u r e f e a t u r e I m p or t an ce Category[featureImportanceF e a t u r e " l e a c h a t e T N " , " l e a c h a t e D O C " , " l e a c h a t e P O C " ) ] < − " S o i l F e a t u r e s " f e a t u r e I m p o r t a n c e Feature %in% c("soilTN", "soilTP", "leachatepH", "leachate_amount",
"leachateTN", "leachateDOC", "leachatePOC")] <- "Soil Features"
featureImportance F e a t u r e " l e a c Category[featureImportance$Feature %in% c("sedimentTC", "sedimentTN")] <- "Sediment Features"
定义颜色 colors <- c(
"Rainfall Features" = "#519D78",
"Stream Hydrochemical Features" = "#8BCF8B",
"Litter Features" = "#AADCA9",
"Soil Features" = "#C4E9CA",
"Sediment Features" = "#DDF3DE"
)
确保 Feature 是因子,并设置因子的顺序 featureImportanceF e a t u r e < − f a c t o r ( f e a t u r e I m p o r t a n c e Feature <- factor(featureImportance F e a t u r e < − f a c t or ( f e a t u r e I m p or t an ce Feature, levels = featureImportanceF e a t u r e ) f e a t u r e I m p o r t a n c e Feature)
featureImportance F e a t u r e ) f e a t u r e I m p or t an ce Category <- factor(featureImportance$Category) # 确保类别是因子
创建图形 p <- ggplot(featureImportance, aes(x = reorder(Feature, Importance), y = Importance, fill = Category)) +
geom_bar(stat = "identity", width = 0.6, color = "black") +
scale_fill_manual(values = colors) + # 手动指定填充颜色
geom_text(aes(label = Significance), hjust = -0.5, size = 8) +
coord_flip() +
theme_light(base_size = 20) +
theme(
axis.title.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.title.y = element_blank(),
axis.text.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.text.y = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = c(0.8, 0.5), # 图例
legend.title = element_text(size = 12, face = "plain", family = "Times New Roman"), # 设置图例标题的样式
legend.text = element_text(size = 10, face = "plain", family = "Times New Roman") # 设置图例文本的样式
)
添加R方和P值到图中 if (!is.null(r_squared) && !is.null(p_value)) {
p <- p +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportanceI m p o r t a n c e ) ∗ 0.9 , l a b e l = p a s t e ( " R 2 = " , r o u n d ( r s q u a r e d , 3 ) ) , s i z e = 6 , h j u s t = 0 ) + a n n o t a t e ( " t e x t " , x = n r o w ( f e a t u r e I m p o r t a n c e ) ∗ 0.5 , y = m a x ( f e a t u r e I m p o r t a n c e Importance) * 0.9,
label = paste("R² =", round(r_squared, 3)), size = 6, hjust = 0) +
annotate("text", x = nrow(featureImportance) * 0.5, y = max(featureImportance I m p or t an ce ) ∗ 0.9 , l ab e l = p a s t e ( " R 2 = " , r o u n d ( r Importance) * 0.85,
label = paste("P =", format(p_value, scientific = TRUE)), size = 6, hjust = 0)
}
return(list(plot = p, featureImportance = featureImportance)) # 返回图形和特征重要性数据框
}
设置随机种子
读取数据(此处需要您本地存在的 CSV 文件) mydata <- read.csv("C14.csv")
进行随机森林分析 rf1 <- randomForest(
POC ~ precipitation + rainfallPOC + velocity + WT + DO +
streampH + TClitter_stream + TClitter_shore +
soilTN + soilTP + leachatepH + leachate_amount +
leachateTN + leachateDOC + leachatePOC +
sedimentTC + sedimentTN,
ntree = 400,
data = mydata,
nodesize = 1,
replace = FALSE,
importance = TRUE
)
计算R方和P值 predictions <- predict(rf1, mydata)
r_squared <- cor(mydataP O C , p r e d i c t i o n s ) 2 l m m o d e l < − l m ( m y d a t a POC, predictions)^2
lm_model <- lm(mydata P O C , p r e d i c t i o n s ) 2 l m m o d e l < − l m ( m y d a t a POC ~ predictions)
summary_lm <- summary(lm_model)
p_value <- summary_lm$coefficients[2, 4] # 获取自变量(predictions)的P值
调用绘图函数并获取特征重要性 rf_output <- create_rfplot(rf1, type = 2, r_squared = r_squared, p_value = p_value)
提取并查看特征重要性 featureImportance <- rf_output$featureImportance
print(featureImportance)
绘图 print(rf_output$plot) 这代码 我想要根据p值设置显著性符号
library(randomForest)
library(ggplot2)
library(extrafont)
library(dplyr)
# 创建函数
create_rfplot <- function(rf, type, p_values, r_squared = NULL, p_value = NULL) {
# 计算特征重要性
imp <- importance(rf, type = type, scale = FALSE)
featureImportance <- data.frame(Feature = row.names(imp), Importance = imp[, 1])
# 将 P 值合并到特征重要性数据框中
featureImportance <- featureImportance %>%
mutate(P_Value = p_values)
# 添加显著性标记
featureImportance$Significance <- ""
featureImportance$Significance[featureImportance$P_Value < 0.001] <- "***" # 三颗星
featureImportance$Significance[featureImportance$P_Value < 0.01] <- "**" # 两颗星
featureImportance$Significance[featureImportance$P_Value < 0.05] <- "*" # 一颗星
print(featureImportance)
# 添加特征类别
featureImportance$Category <- ""
featureImportance$Category[featureImportance$Feature %in% c("precipitation", "rainfallPOC")] <- "Rainfall Features"
featureImportance$Category[featureImportance$Feature %in% c("velocity", "WT", "DO", "streampH")] <- "Stream Hydrochemical Features"
featureImportance$Category[featureImportance$Feature %in% c("TClitter_stream", "TClitter_shore")] <- "Litter Features"
featureImportance$Category[featureImportance$Feature %in% c("soilTN", "soilTP", "leachatepH", "leachate_amount",
"leachateTN", "leachateDOC", "leachatePOC")] <- "Soil Features"
featureImportance$Category[featureImportance$Feature %in% c("sedimentTC", "sedimentTN")] <- "Sediment Features"
# 定义颜色
colors <- c(
"Rainfall Features" = "#519D78",
"Stream Hydrochemical Features" = "#8BCF8B",
"Litter Features" = "#AADCA9",
"Soil Features" = "#C4E9CA",
"Sediment Features" = "#DDF3DE"
)
# 确保 Feature 是因子,并设置因子的顺序
featureImportance$Feature <- factor(featureImportance$Feature, levels = featureImportance$Feature)
featureImportance$Category <- factor(featureImportance$Category) # 确保类别是因子
# 创建图形
p <- ggplot(featureImportance, aes(x = reorder(Feature, Importance), y = Importance, fill = Category)) +
geom_bar(stat = "identity", width = 0.6, color = "black") +
scale_fill_manual(values = colors) + # 手动指定填充颜色
geom_text(aes(label = Significance), hjust = -0.5, size = 6) +
coord_flip() +
theme_light(base_size = 20) +
theme(
axis.title.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.title.y = element_blank(),
axis.text.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.text.y = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = c(0.8, 0.5),
legend.title = element_text(size = 12, face = "plain", family = "Times New Roman"),
legend.text = element_text(size = 10, face = "plain", family = "Times New Roman")
)
# 添加R方和P值到图中
if (!is.null(r_squared) && !is.null(p_value)) {
p <- p +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportance$Importance) * 0.9,
label = paste("R² =", round(r_squared, 3)), size = 6, hjust = 0) +
annotate("text", x = nrow(featureImportance) * 0.5, y = max(featureImportance$Importance) * 0.85,
label = paste("P =", format(p_value, scientific = TRUE)), size = 6, hjust = 0)
}
return(list(plot = p, featureImportance = featureImportance)) # 返回图形和特征重要性数据框
}
# 设置随机种子
set.seed(123)
# 读取数据(此处需要您本地存在的 CSV 文件)
mydata <- read.csv("C14.csv")
# 进行随机森林分析
rf1 <- randomForest(
POC ~ precipitation + rainfallPOC + velocity + WT + DO +
streampH + TClitter_stream + TClitter_shore +
soilTN + soilTP + leachatepH + leachate_amount +
leachateTN + leachateDOC + leachatePOC +
sedimentTC + sedimentTN,
ntree = 400,
data = mydata,
nodesize = 1,
replace = FALSE,
importance = TRUE
)
# 计算R方和P值
predictions <- predict(rf1, mydata)
r_squared <- cor(mydata$POC, predictions)^2
lm_model <- lm(mydata$POC ~ predictions)
summary_lm <- summary(lm_model)
p_value <- summary_lm$coefficients[2, 4] # 获取自变量(predictions)的P值
# 计算P值的示例(实际需要根据您的需求替换)。
p_values <- runif(nrow(rf1$importance), 0, 0.1) # 示例随机p值,应替换为具体的P值计算
# 调用绘图函数并获取特征重要性
rf_output <- create_rfplot(rf1, type = 2, p_values = p_values, r_squared = r_squared, p_value = p_value)
# 提取并查看特征重要性
featureImportance <- rf_output$featureImportance
print(featureImportance)
# 绘图
print(rf_output$plot)
关键步骤说明:
P 值生成 :
使用 runif 函数生成一个随机 P 值例子,您需要将此部分替换为从您的模型中提取的实际 P 值。这通常取决于要计算的具体统计分析。
显著性标记 :
利用 P 值直接创建显著性标记。featureImportance$Significance 将基于各个特征的 P 值显示 、 * 或 *** 的标记。
图形绘制 :
在条形图中使用 geom_text 显示每个特征的显著性符号。
注意: 请确保在实际应用中,p_values 变量应该反映每个特征的真实显著性水平,而不是随机生成的值。您可能需要根据您的分析具体方法计算或提取这些真实的 P 值。
# 确保加载所需的库
library(randomForest)
library(ggplot2)
library(extrafont)
library(dplyr) # 添加dplyr库
# 创建函数
create_rfplot <- function(rf, type, p_values, r_squared = NULL, p_value = NULL) {
# 计算特征重要性
imp <- importance(rf, type = type, scale = FALSE)
featureImportance <- data.frame(Feature = row.names(imp), Importance = imp[, 1])
# 将 P 值合并到特征重要性数据框中
featureImportance <- featureImportance %>%
mutate(P_Value = p_values) # 使用dplyr的mutate直接添加P值
# 添加显著性标记
featureImportance$Significance <- ""
featureImportance$Significance[featureImportance$P_Value < 0.001] <- "***" # 三颗星
featureImportance$Significance[featureImportance$P_Value < 0.01] <- "**" # 两颗星
featureImportance$Significance[featureImportance$P_Value < 0.05] <- "*" # 一颗星
print(featureImportance)
# 添加特征类别
featureImportance$Category <- ""
featureImportance$Category[featureImportance$Feature %in% c("precipitation", "rainfallPOC")] <- "Rainfall Features"
featureImportance$Category[featureImportance$Feature %in% c("velocity", "WT", "DO", "streampH")] <- "Stream Hydrochemical Features"
featureImportance$Category[featureImportance$Feature %in% c("TClitter_stream", "TClitter_shore")] <- "Litter Features"
featureImportance$Category[featureImportance$Feature %in% c("soilTN", "soilTP", "leachatepH", "leachate_amount",
"leachateTN", "leachateDOC", "leachatePOC")] <- "Soil Features"
featureImportance$Category[featureImportance$Feature %in% c("sedimentTC", "sedimentTN")] <- "Sediment Features"
# 定义颜色
colors <- c(
"Rainfall Features" = "#519D78",
"Stream Hydrochemical Features" = "#8BCF8B",
"Litter Features" = "#AADCA9",
"Soil Features" = "#C4E9CA",
"Sediment Features" = "#DDF3DE"
)
# 确保 Feature 是因子,并设置因子的顺序
featureImportance$Feature <- factor(featureImportance$Feature, levels = featureImportance$Feature)
featureImportance$Category <- factor(featureImportance$Category) # 确保类别是因子
# 创建图形
p <- ggplot(featureImportance, aes(x = reorder(Feature, Importance), y = Importance, fill = Category)) +
geom_bar(stat = "identity", width = 0.6, color = "black") +
scale_fill_manual(values = colors) + # 手动指定填充颜色
geom_text(aes(label = Significance), hjust = -0.5, size = 6) +
coord_flip() +
theme_light(base_size = 20) +
theme(
axis.title.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.title.y = element_blank(),
axis.text.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.text.y = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = c(0.8, 0.5),
legend.title = element_text(size = 12, face = "plain", family = "Times New Roman"),
legend.text = element_text(size = 10, face = "plain", family = "Times New Roman")
)
# 添加R方和P值到图中
if (!is.null(r_squared) && !is.null(p_value)) {
p <- p +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportance$Importance) * 0.9,
label = paste("R² =", round(r_squared, 3)), size = 6, hjust = 0) +
annotate("text", x = nrow(featureImportance) * 0.5, y = max(featureImportance$Importance) * 0.85,
label = paste("P =", format(p_value, scientific = TRUE)), size = 6, hjust = 0)
}
return(list(plot = p, featureImportance = featureImportance)) # 返回图形和特征重要性数据框
}
# 设置随机种子
set.seed(123)
# 读取数据(此处需要您本地存在的 CSV 文件)
mydata <- read.csv("C14.csv")
# 进行随机森林分析
rf1 <- randomForest(
POC ~ precipitation + rainfallPOC + velocity + WT + DO +
streampH + TClitter_stream + TClitter_shore +
soilTN + soilTP + leachatepH + leachate_amount +
leachateTN + leachateDOC + leachatePOC +
sedimentTC + sedimentTN,
ntree = 400,
data = mydata,
nodesize = 1,
replace = FALSE,
importance = TRUE
)
# 计算R方和P值
predictions <- predict(rf1, mydata)
r_squared <- cor(mydata$POC, predictions)^2
lm_model <- lm(mydata$POC ~ predictions)
summary_lm <- summary(lm_model)
p_value <- summary_lm$coefficients[2, 4] # 获取自变量(predictions)的P值
# 计算P值的示例(实际需要根据您的需求替换)。
p_values <- runif(nrow(rf1$importance), 0, 0.1) # 示例随机p值,应替换为具体的P值计算
# 调用绘图函数并获取特征重要性
rf_output <- create_rfplot(rf1, type = 2, p_values = p_values, r_squared = r_squared, p_value = p_value)
# 提取并查看特征重要性
featureImportance <- rf_output$featureImportance
print(featureImportance)
# 绘图
print(rf_output$plot)
关键点
通过添加 library(dplyr) 确保您能够使用 %>% 管道操作符。
mutate 用于创建 P_Value 列并将 P 值添加到 featureImportance 数据框。
使用 runif 生成示例随机 P 值,需要您替换实际的 P 值计算。
执行这段代码之前,请确保您已经设置好数据文件路径,并安装并加载了所需的 R 包。运行代码后,您将能看到特征的重要性以及显著性符号(、、 )在图中显示。
S i g n i f i c a n c e [ f e a t u r e I m p o r t a n c e Significance[featureImportance S i g ni f i c an ce [ f e a t u r e I m p or t an ce
Significance == ""] <- "**" # 两颗星
featureImportance
Importance > max_importance * 0.5 &
featureImportance
定义颜色 colors <- c(
"precipitation" = "#2e8b57",
"rainfallPOC" = "#3cb371",
"velocity" = "#66cdaa",
"WT" = "#98fb98",
"DO" = "#add8e6",
"streampH" = "#b2e3b2",
"TClitter_stream" = "#4caf50",
"TClitter_shore" = "#a7d6a0",
"soilTN" = "#66db67",
"soilTP" = "#7fffd4",
"leachatepH" = "#90ee90",
"leachate_amount" = "#2f8e4b",
"leachateTN" = "#3fbf94",
"leachateDOC" = "#70db70",
"leachatePOC" = "#2dd8a1",
"sedimentTC" = "#c5e3c5",
"sedimentTN" = "#81c3c0"
)
确保 Feature 是因子,并设置因子的顺序 featureImportanceF e a t u r e < − f a c t o r ( f e a t u r e I m p o r t a n c e Feature <- factor(featureImportance F e a t u r e < − f a c t or ( f e a t u r e I m p or t an ce Feature, levels = featureImportance$Feature)
创建图形 p <- ggplot(featureImportance, aes(x = reorder(Feature, Importance), y = Importance, fill = Feature)) +
geom_bar(stat = "identity", width = 0.6, color = "black") +
scale_fill_manual(values = colors) + # 手动指定填充颜色
geom_text(aes(label = Significance), hjust = -0.5, size = 8) +
coord_flip() +
theme_light(base_size = 20) +
theme(
axis.title.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.title.y = element_blank(),
axis.text.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.text.y = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = c(0.8, 0.5), # 图例
legend.title = element_text(size = 12, face = "plain", family = "Times New Roman"), # 设置图例标题的样式
legend.text = element_text(size = 10, face = "plain", family = "Times New Roman") # 设置图例文本的样式
)
return(list(plot = p, featureImportance = featureImportance)) # 返回图形和特征重要性数据框
}
设置随机种子
读取数据 mydata <- read.csv("C14.csv")
进行随机森林分析 rf1 <- randomForest(
POC ~ precipitation + rainfallPOC + velocity + WT + DO +
streampH + TClitter_stream + TClitter_shore +
soilTN + soilTP + leachatepH + leachate_amount +
leachateTN + leachateDOC + leachatePOC +
sedimentTC + sedimentTN,
ntree = 400,
data = mydata,
nodesize = 1,
replace = FALSE,
importance = TRUE
)
调用绘图函数并获取特征重要性 rf_output <- create_rfplot(rf1, type = 2)
提取并查看特征重要性 featureImportance <- rf_output$featureImportance
print(featureImportance)
绘图 print(rf_output$plot) 计算R方和R方的p值,并标注在图中,修改代码
library(randomForest)
library(ggplot2)
library(extrafont)
# 创建函数
create_rfplot <- function(rf, type, r_squared = NULL, p_value = NULL) {
# 计算特征重要性
imp <- importance(rf, type = type, scale = FALSE)
featureImportance <- data.frame(Feature = row.names(imp), Importance = imp[,1])
# 添加显著性标记
max_importance <- max(featureImportance$Importance)
featureImportance$Significance <- ""
# 用 else if 结构设置显著性标记
featureImportance$Significance[featureImportance$Importance > max_importance * 0.9] <- "***" # 三颗星
featureImportance$Significance[featureImportance$Importance > max_importance * 0.7 &
featureImportance$Significance == ""] <- "**" # 两颗星
featureImportance$Significance[featureImportance$Importance > max_importance * 0.5 &
featureImportance$Significance == ""] <- "*" # 一颗星
print(featureImportance)
# 定义颜色
colors <- c(
"precipitation" = "#2e8b57",
"rainfallPOC" = "#3cb371",
"velocity" = "#66cdaa",
"WT" = "#98fb98",
"DO" = "#add8e6",
"streampH" = "#b2e3b2",
"TClitter_stream" = "#4caf50",
"TClitter_shore" = "#a7d6a0",
"soilTN" = "#66db67",
"soilTP" = "#7fffd4",
"leachatepH" = "#90ee90",
"leachate_amount" = "#2f8e4b",
"leachateTN" = "#3fbf94",
"leachateDOC" = "#70db70",
"leachatePOC" = "#2dd8a1",
"sedimentTC" = "#c5e3c5",
"sedimentTN" = "#81c3c0"
)
# 确保 Feature 是因子,并设置因子的顺序
featureImportance$Feature <- factor(featureImportance$Feature, levels = featureImportance$Feature)
# 创建图形
p <- ggplot(featureImportance, aes(x = reorder(Feature, Importance), y = Importance, fill = Feature)) +
geom_bar(stat = "identity", width = 0.6, color = "black") +
scale_fill_manual(values = colors) + # 手动指定填充颜色
geom_text(aes(label = Significance), hjust = -0.5, size = 8) +
coord_flip() +
theme_light(base_size = 20) +
theme(
axis.title.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.title.y = element_blank(),
axis.text.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.text.y = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = c(0.8, 0.5), # 图例
legend.title = element_text(size = 12, face = "plain", family = "Times New Roman"), # 设置图例标题的样式
legend.text = element_text(size = 10, face = "plain", family = "Times New Roman") # 设置图例文本的样式
)
# 添加R方和P值到图中
if (!is.null(r_squared) && !is.null(p_value)) {
p <- p +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportance$Importance) * 0.9,
label = paste("R² =", round(r_squared, 3)), size = 6, hjust = 0) +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportance$Importance) * 0.85,
label = paste("P =", format(p_value, scientific = TRUE)), size = 6, hjust = 0)
}
return(list(plot = p, featureImportance = featureImportance)) # 返回图形和特征重要性数据框
}
# 设置随机种子
set.seed(123)
# 读取数据
mydata <- read.csv("C14.csv")
# 进行随机森林分析
rf1 <- randomForest(
POC ~ precipitation + rainfallPOC + velocity + WT + DO +
streampH + TClitter_stream + TClitter_shore +
soilTN + soilTP + leachatepH + leachate_amount +
leachateTN + leachateDOC + leachatePOC +
sedimentTC + sedimentTN,
ntree = 400,
data = mydata,
nodesize = 1,
replace = FALSE,
importance = TRUE
)
# 计算R方和P值
predictions <- predict(rf1, mydata)
r_squared <- cor(mydata$POC, predictions)^2
lm_model <- lm(mydata$POC ~ predictions)
summary_lm <- summary(lm_model)
p_value <- summary_lm$coefficients[2, 4] # 获取自变量(predictions)的P值
# 调用绘图函数并获取特征重要性
rf_output <- create_rfplot(rf1, type = 2, r_squared = r_squared, p_value = p_value)
# 提取并查看特征重要性
featureImportance <- rf_output$featureImportance
print(featureImportance)
# 绘图
print(rf_output$plot)
主要修改部分:
计算 ( R^2 ) :在预测后,用相关性计算 ( R^2 )。
计算 P 值 :使用线性回归模型来计算 P 值。
添加注释 :在图形中添加 ( R^2 ) 和 P 值的注释。
在确保数据清洗和准备完好的前提下,运行这段代码即可得到含有 ( R^2 ) 和 P 值标注的随机森林特征重要性图。
S i g n i f i c a n c e [ f e a t u r e I m p o r t a n c e Significance[featureImportance S i g ni f i c an ce [ f e a t u r e I m p or t an ce Significance == ""] <- "**" # 两颗星
featureImportance
Importance > max_importance * 0.5 &
featureImportance
添加特征类别 featureImportanceC a t e g o r y < − " " f e a t u r e I m p o r t a n c e Category <- ""
featureImportance C a t e g or y < − "" f e a t u r e I m p or t an ce Category[featureImportanceF e a t u r e f e a t u r e I m p o r t a n c e Feature %in% c("precipitation", "rainfallPOC")] <- "Rainfall Features"
featureImportance F e a t u r e f e a t u r e I m p or t an ce Category[featureImportanceF e a t u r e f e a t u r e I m p o r t a n c e Feature %in% c("velocity", "WT", "DO", "streampH")] <- "Stream Hydrochemical Features"
featureImportance F e a t u r e f e a t u r e I m p or t an ce Category[featureImportanceF e a t u r e f e a t u r e I m p o r t a n c e Feature %in% c("TClitter_stream", "TClitter_shore")] <- "Litter Features"
featureImportance F e a t u r e f e a t u r e I m p or t an ce Category[featureImportanceF e a t u r e " l e a c h a t e T N " , " l e a c h a t e D O C " , " l e a c h a t e P O C " ) ] < − " S o i l F e a t u r e s " f e a t u r e I m p o r t a n c e Feature %in% c("soilTN", "soilTP", "leachatepH", "leachate_amount",
"leachateTN", "leachateDOC", "leachatePOC")] <- "Soil Features"
featureImportance F e a t u r e " l e a c Category[featureImportance$Feature %in% c("sedimentTC", "sedimentTN")] <- "Sediment Features"
定义颜色 colors <- c(
"Rainfall Features" = "#519D78",
"Stream Hydrochemical Features" = "#8BCF8B",
"Litter Features" = "#AADCA9",
"Soil Features" = "#C4E9CA",
"Sediment Features" = "#DDF3DE"
)
确保 Feature 是因子,并设置因子的顺序 featureImportanceF e a t u r e < − f a c t o r ( f e a t u r e I m p o r t a n c e Feature <- factor(featureImportance F e a t u r e < − f a c t or ( f e a t u r e I m p or t an ce Feature, levels = featureImportanceF e a t u r e ) f e a t u r e I m p o r t a n c e Feature)
featureImportance F e a t u r e ) f e a t u r e I m p or t an ce Category <- factor(featureImportance$Category) # 确保类别是因子
创建图形 p <- ggplot(featureImportance, aes(x = reorder(Feature, Importance), y = Importance, fill = Category)) +
geom_bar(stat = "identity", width = 0.6, color = "black") +
scale_fill_manual(values = colors) + # 手动指定填充颜色
geom_text(aes(label = Significance), hjust = -0.5, size = 8) +
coord_flip() +
theme_light(base_size = 20) +
theme(
axis.title.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.title.y = element_blank(),
axis.text.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.text.y = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = c(0.8, 0.5), # 图例
legend.title = element_text(size = 12, face = "plain", family = "Times New Roman"), # 设置图例标题的样式
legend.text = element_text(size = 10, face = "plain", family = "Times New Roman") # 设置图例文本的样式
)
添加R方和P值到图中 if (!is.null(r_squared) && !is.null(p_value)) {
p <- p +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportanceI m p o r t a n c e ) ∗ 0.9 , l a b e l = p a s t e ( " R 2 = " , r o u n d ( r s q u a r e d , 3 ) ) , s i z e = 6 , h j u s t = 0 ) + a n n o t a t e ( " t e x t " , x = n r o w ( f e a t u r e I m p o r t a n c e ) ∗ 0.7 , y = m a x ( f e a t u r e I m p o r t a n c e Importance) * 0.9,
label = paste("R² =", round(r_squared, 3)), size = 6, hjust = 0) +
annotate("text", x = nrow(featureImportance) * 0.7, y = max(featureImportance I m p or t an ce ) ∗ 0.9 , l ab e l = p a s t e ( " R 2 = " , r o u n d ( r Importance) * 0.85,
label = paste("P =", format(p_value, scientific = TRUE)), size = 6, hjust = 0)
}
return(list(plot = p, featureImportance = featureImportance)) # 返回图形和特征重要性数据框
}
设置随机种子
读取数据(此处需要您本地存在的 CSV 文件) mydata <- read.csv("C13.csv")
进行随机森林分析 rf1 <- randomForest(
DOC ~ precipitation + rainfallPOC + velocity + WT + DO +
streampH + TClitter_stream + TClitter_shore +
soilTN + soilTP + leachatepH + leachate_amount +
leachateTN + leachateDOC + leachatePOC +
sedimentTC + sedimentTN,
ntree = 400,
data = mydata,
nodesize = 1,
replace = FALSE,
importance = TRUE
)
计算R方和P值 predictions <- predict(rf1, mydata)
r_squared <- cor(mydataD O C , p r e d i c t i o n s ) 2 l m m o d e l < − l m ( m y d a t a DOC, predictions)^2
lm_model <- lm(mydata D O C , p r e d i c t i o n s ) 2 l m m o d e l < − l m ( m y d a t a DOC ~ predictions)
summary_lm <- summary(lm_model)
p_value <- summary_lm$coefficients[2, 4] # 获取自变量(predictions)的P值
调用绘图函数并获取特征重要性 rf_output <- create_rfplot(rf1, type = 2, r_squared = r_squared, p_value = p_value)
提取并查看特征重要性 featureImportance <- rf_output$featureImportance
print(featureImportance)
绘图 print(rf_output$plot) 这串代码X轴是表示%IncMSE 还是IncNodePurity结果
S i g n i f i c a n c e [ f e a t u r e I m p o r t a n c e Significance[featureImportance S i g ni f i c an ce [ f e a t u r e I m p or t an ce Significance == ""] <- "**" # 两颗星
featureImportance
Importance > max_importance * 0.5 &
featureImportance
添加特征类别 featureImportanceC a t e g o r y < − " " f e a t u r e I m p o r t a n c e Category <- ""
featureImportance C a t e g or y < − "" f e a t u r e I m p or t an ce Category[featureImportanceF e a t u r e f e a t u r e I m p o r t a n c e Feature %in% c("precipitation", "rainfallPOC")] <- "Rainfall Features"
featureImportance F e a t u r e f e a t u r e I m p or t an ce Category[featureImportanceF e a t u r e f e a t u r e I m p o r t a n c e Feature %in% c("velocity", "WT", "DO", "streampH")] <- "Stream Hydrochemical Features"
featureImportance F e a t u r e f e a t u r e I m p or t an ce Category[featureImportanceF e a t u r e f e a t u r e I m p o r t a n c e Feature %in% c("TClitter_stream", "TClitter_shore")] <- "Litter Features"
featureImportance F e a t u r e f e a t u r e I m p or t an ce Category[featureImportanceF e a t u r e " l e a c h a t e T N " , " l e a c h a t e D O C " , " l e a c h a t e P O C " ) ] < − " S o i l F e a t u r e s " f e a t u r e I m p o r t a n c e Feature %in% c("soilTN", "soilTP", "leachatepH", "leachate_amount",
"leachateTN", "leachateDOC", "leachatePOC")] <- "Soil Features"
featureImportance F e a t u r e " l e a c Category[featureImportance$Feature %in% c("sedimentTC", "sedimentTN")] <- "Sediment Features"
定义颜色 colors <- c(
"Rainfall Features" = "#519D78",
"Stream Hydrochemical Features" = "#8BCF8B",
"Litter Features" = "#AADCA9",
"Soil Features" = "#C4E9CA",
"Sediment Features" = "#DDF3DE"
)
确保 Feature 是因子,并设置因子的顺序 featureImportanceF e a t u r e < − f a c t o r ( f e a t u r e I m p o r t a n c e Feature <- factor(featureImportance F e a t u r e < − f a c t or ( f e a t u r e I m p or t an ce Feature, levels = featureImportanceF e a t u r e ) f e a t u r e I m p o r t a n c e Feature)
featureImportance F e a t u r e ) f e a t u r e I m p or t an ce Category <- factor(featureImportance$Category) # 确保类别是因子
创建图形 p <- ggplot(featureImportance, aes(x = reorder(Feature, Importance), y = Importance, fill = Category)) +
geom_bar(stat = "identity", width = 0.6, color = "black") +
scale_fill_manual(values = colors) + # 手动指定填充颜色
geom_text(aes(label = Significance), hjust = -0.5, size = 8) +
coord_flip() +
theme_light(base_size = 20) +
theme(
axis.title.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.title.y = element_blank(),
axis.text.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.text.y = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = c(0.8, 0.5), # 图例
legend.title = element_text(size = 12, face = "plain", family = "Times New Roman"), # 设置图例标题的样式
legend.text = element_text(size = 10, face = "plain", family = "Times New Roman") # 设置图例文本的样式
)
添加R方和P值到图中 if (!is.null(r_squared) && !is.null(p_value)) {
p <- p +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportanceI m p o r t a n c e ) ∗ 0.9 , l a b e l = p a s t e ( " R 2 = " , r o u n d ( r s q u a r e d , 3 ) ) , s i z e = 6 , h j u s t = 0 ) + a n n o t a t e ( " t e x t " , x = n r o w ( f e a t u r e I m p o r t a n c e ) ∗ 0.7 , y = m a x ( f e a t u r e I m p o r t a n c e Importance) * 0.9,
label = paste("R² =", round(r_squared, 3)), size = 6, hjust = 0) +
annotate("text", x = nrow(featureImportance) * 0.7, y = max(featureImportance I m p or t an ce ) ∗ 0.9 , l ab e l = p a s t e ( " R 2 = " , r o u n d ( r Importance) * 0.85,
label = paste("P =", format(p_value, scientific = TRUE)), size = 6, hjust = 0)
}
return(list(plot = p, featureImportance = featureImportance)) # 返回图形和特征重要性数据框
}
设置随机种子
读取数据(此处需要您本地存在的 CSV 文件) mydata <- read.csv("C13.csv")
进行随机森林分析 rf1 <- randomForest(
DOC ~ precipitation + rainfallPOC + velocity + WT + DO +
streampH + TClitter_stream + TClitter_shore +
soilTN + soilTP + leachatepH + leachate_amount +
leachateTN + leachateDOC + leachatePOC +
sedimentTC + sedimentTN,
ntree = 400,
data = mydata,
nodesize = 1,
replace = FALSE,
importance = TRUE
)
计算R方和P值 predictions <- predict(rf1, mydata)
r_squared <- cor(mydataD O C , p r e d i c t i o n s ) 2 l m m o d e l < − l m ( m y d a t a DOC, predictions)^2
lm_model <- lm(mydata D O C , p r e d i c t i o n s ) 2 l m m o d e l < − l m ( m y d a t a DOC ~ predictions)
summary_lm <- summary(lm_model)
p_value <- summary_lm$coefficients[2, 4] # 获取自变量(predictions)的P值
调用绘图函数并获取特征重要性 rf_output <- create_rfplot(rf1, type = 2, r_squared = r_squared, p_value = p_value)
提取并查看特征重要性 featureImportance <- rf_output$featureImportance
print(featureImportance)
绘图 print(rf_output$plot) 所以我这串代码的X轴到底表示什么,是%IncMSE嘛
S i g n i f i c a n c e [ f e a t u r e I m p o r t a n c e Significance[featureImportance S i g ni f i c an ce [ f e a t u r e I m p or t an ce Significance == ""] <- "**" # 两颗星
featureImportance
Importance > max_importance * 0.5 &
featureImportance
添加特征类别 featureImportanceC a t e g o r y < − " " f e a t u r e I m p o r t a n c e Category <- ""
featureImportance C a t e g or y < − "" f e a t u r e I m p or t an ce Category[featureImportanceF e a t u r e f e a t u r e I m p o r t a n c e Feature %in% c("precipitation", "rainfallPOC")] <- "Rainfall Features"
featureImportance F e a t u r e f e a t u r e I m p or t an ce Category[featureImportanceF e a t u r e f e a t u r e I m p o r t a n c e Feature %in% c("velocity", "WT", "DO", "streampH")] <- "Stream Hydrochemical Features"
featureImportance F e a t u r e f e a t u r e I m p or t an ce Category[featureImportanceF e a t u r e f e a t u r e I m p o r t a n c e Feature %in% c("TClitter_stream", "TClitter_shore")] <- "Litter Features"
featureImportance F e a t u r e f e a t u r e I m p or t an ce Category[featureImportanceF e a t u r e " l e a c h a t e T N " , " l e a c h a t e D O C " , " l e a c h a t e P O C " ) ] < − " S o i l F e a t u r e s " f e a t u r e I m p o r t a n c e Feature %in% c("soilTN", "soilTP", "leachatepH", "leachate_amount",
"leachateTN", "leachateDOC", "leachatePOC")] <- "Soil Features"
featureImportance F e a t u r e " l e a c Category[featureImportance$Feature %in% c("sedimentTC", "sedimentTN")] <- "Sediment Features"
定义颜色 colors <- c(
"Rainfall Features" = "#519D78",
"Stream Hydrochemical Features" = "#8BCF8B",
"Litter Features" = "#AADCA9",
"Soil Features" = "#C4E9CA",
"Sediment Features" = "#DDF3DE"
)
确保 Feature 是因子,并设置因子的顺序 featureImportanceF e a t u r e < − f a c t o r ( f e a t u r e I m p o r t a n c e Feature <- factor(featureImportance F e a t u r e < − f a c t or ( f e a t u r e I m p or t an ce Feature, levels = featureImportanceF e a t u r e ) f e a t u r e I m p o r t a n c e Feature)
featureImportance F e a t u r e ) f e a t u r e I m p or t an ce Category <- factor(featureImportance$Category) # 确保类别是因子
创建图形 p <- ggplot(featureImportance, aes(x = reorder(Feature, Importance), y = Importance, fill = Category)) +
geom_bar(stat = "identity", width = 0.6, color = "black") +
scale_fill_manual(values = colors) + # 手动指定填充颜色
geom_text(aes(label = Significance), hjust = -0.5, size = 8) +
coord_flip() +
theme_light(base_size = 20) +
theme(
axis.title.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.title.y = element_blank(),
axis.text.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.text.y = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = c(0.8, 0.5), # 图例
legend.title = element_text(size = 12, face = "plain", family = "Times New Roman"), # 设置图例标题的样式
legend.text = element_text(size = 10, face = "plain", family = "Times New Roman") # 设置图例文本的样式
)
添加R方和P值到图中 if (!is.null(r_squared) && !is.null(p_value)) {
p <- p +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportanceI m p o r t a n c e ) ∗ 0.9 , l a b e l = p a s t e ( " R 2 = " , r o u n d ( r s q u a r e d , 3 ) ) , s i z e = 6 , h j u s t = 0 ) + a n n o t a t e ( " t e x t " , x = n r o w ( f e a t u r e I m p o r t a n c e ) ∗ 0.7 , y = m a x ( f e a t u r e I m p o r t a n c e Importance) * 0.9,
label = paste("R² =", round(r_squared, 3)), size = 6, hjust = 0) +
annotate("text", x = nrow(featureImportance) * 0.7, y = max(featureImportance I m p or t an ce ) ∗ 0.9 , l ab e l = p a s t e ( " R 2 = " , r o u n d ( r Importance) * 0.85,
label = paste("P =", format(p_value, scientific = TRUE)), size = 6, hjust = 0)
}
return(list(plot = p, featureImportance = featureImportance)) # 返回图形和特征重要性数据框
}
设置随机种子
读取数据(此处需要您本地存在的 CSV 文件) mydata <- read.csv("C13.csv")
进行随机森林分析 rf1 <- randomForest(
DOC ~ precipitation + rainfallPOC + velocity + WT + DO +
streampH + TClitter_stream + TClitter_shore +
soilTN + soilTP + leachatepH + leachate_amount +
leachateTN + leachateDOC + leachatePOC +
sedimentTC + sedimentTN,
ntree = 400,
data = mydata,
nodesize = 1,
replace = FALSE,
importance = TRUE
)
计算R方和P值 predictions <- predict(rf1, mydata)
r_squared <- cor(mydataD O C , p r e d i c t i o n s ) 2 l m m o d e l < − l m ( m y d a t a DOC, predictions)^2
lm_model <- lm(mydata D O C , p r e d i c t i o n s ) 2 l m m o d e l < − l m ( m y d a t a DOC ~ predictions)
summary_lm <- summary(lm_model)
p_value <- summary_lm$coefficients[2, 4] # 获取自变量(predictions)的P值
调用绘图函数并获取特征重要性 rf_output <- create_rfplot(rf1, type = 2, r_squared = r_squared, p_value = p_value)
提取并查看特征重要性 featureImportance <- rf_output$featureImportance
print(featureImportance)
绘图 print(rf_output$plot) 如果我想要X轴表示%IncMSE ,我应该怎么修改代码
library(randomForest)
library(ggplot2)
library(extrafont)
# 创建函数
create_rfplot <- function(rf, type, significance_threshold = 0.05, r_squared = NULL, p_value = NULL) {
# 计算特征重要性
imp <- importance(rf, type = type, scale = FALSE)
featureImportance <- data.frame(Feature = row.names(imp), Importance = imp[, 1])
# 计算%IncMSE
total_importance <- sum(featureImportance$Importance)
featureImportance$PercentageIncMSE <- (featureImportance$Importance / total_importance) * 100
# 添加显著性标记
max_importance <- max(featureImportance$PercentageIncMSE)
featureImportance$Significance <- ""
featureImportance$Significance[featureImportance$PercentageIncMSE > max_importance * 0.9] <- "***" # 三颗星
featureImportance$Significance[featureImportance$PercentageIncMSE > max_importance * 0.7 &
featureImportance$Significance == ""] <- "**" # 两颗星
featureImportance$Significance[featureImportance$PercentageIncMSE > max_importance * 0.5 &
featureImportance$Significance == ""] <- "*" # 一颗星
print(featureImportance)
# 添加特征类别
featureImportance$Category <- ""
featureImportance$Category[featureImportance$Feature %in% c("precipitation", "rainfallPOC")] <- "Rainfall Features"
featureImportance$Category[featureImportance$Feature %in% c("velocity", "WT", "DO", "streampH")] <- "Stream Hydrochemical Features"
featureImportance$Category[featureImportance$Feature %in% c("TClitter_stream", "TClitter_shore")] <- "Litter Features"
featureImportance$Category[featureImportance$Feature %in% c("soilTN", "soilTP", "leachatepH", "leachate_amount",
"leachateTN", "leachateDOC", "leachatePOC")] <- "Soil Features"
featureImportance$Category[featureImportance$Feature %in% c("sedimentTC", "sedimentTN")] <- "Sediment Features"
# 定义颜色
colors <- c(
"Rainfall Features" = "#519D78",
"Stream Hydrochemical Features" = "#8BCF8B",
"Litter Features" = "#AADCA9",
"Soil Features" = "#C4E9CA",
"Sediment Features" = "#DDF3DE"
)
# 确保 Feature 是因子,并设置因子的顺序
featureImportance$Feature <- factor(featureImportance$Feature, levels = featureImportance$Feature)
featureImportance$Category <- factor(featureImportance$Category) # 确保类别是因子
# 创建图形
p <- ggplot(featureImportance, aes(x = reorder(Feature, PercentageIncMSE), y = PercentageIncMSE, fill = Category)) +
geom_bar(stat = "identity", width = 0.6, color = "black") +
scale_fill_manual(values = colors) + # 手动指定填充颜色
geom_text(aes(label = Significance), hjust = -0.5, size = 8) +
coord_flip() +
theme_light(base_size = 20) +
theme(
axis.title.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.title.y = element_blank(),
axis.text.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.text.y = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = c(0.8, 0.5), # 图例
legend.title = element_text(size = 12, face = "plain", family = "Times New Roman"), # 设置图例标题的样式
legend.text = element_text(size = 10, face = "plain", family = "Times New Roman") # 设置图例文本的样式
)
# 添加R方和P值到图中
if (!is.null(r_squared) && !is.null(p_value)) {
p <- p +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportance$PercentageIncMSE) * 0.9,
label = paste("R² =", round(r_squared, 3)), size = 6, hjust = 0) +
annotate("text", x = nrow(featureImportance) * 0.7, y = max(featureImportance$PercentageIncMSE) * 0.85,
label = paste("P =", format(p_value, scientific = TRUE)), size = 6, hjust = 0)
}
return(list(plot = p, featureImportance = featureImportance)) # 返回图形和特征重要性数据框
}
# 设置随机种子
set.seed(123)
# 读取数据(此处需要您本地存在的 CSV 文件)
mydata <- read.csv("C13.csv")
# 进行随机森林分析
rf1 <- randomForest(
DOC ~ precipitation + rainfallPOC + velocity + WT + DO +
streampH + TClitter_stream + TClitter_shore +
soilTN + soilTP + leachatepH + leachate_amount +
leachateTN + leachateDOC + leachatePOC +
sedimentTC + sedimentTN,
ntree = 400,
data = mydata,
nodesize = 1,
replace = FALSE,
importance = TRUE
)
# 调用绘图函数并获取特征重要性
rf_output <- create_rfplot(rf1, type = 1) # 此处使用type = 1以获得Mean Decrease in MSE
# 提取并查看特征重要性
featureImportance <- rf_output$featureImportance
print(featureImportance)
# 绘图
print(rf_output$plot)
关键修改:
在 importance 计算后,通过计算每个特征的重要性与总重要性的百分比来生成 %IncMSE.
使用新的列 PercentageIncMSE 替换 X 轴绘制。
确保在调用 create_rfplot() 时用 type = 1 以获取适当的格式。
执行这些修改后,您应该能够在官方生成的图形中看到 X 轴表示 %IncMSE。
library(randomForest)
library(ggplot2)
library(extrafont)
# 创建函数
create_rfplot <- function(rf, type, significance_threshold = 0.05, r_squared = NULL, p_value = NULL) {
# 计算特征重要性
imp <- importance(rf, type = type, scale = FALSE)
featureImportance <- data.frame(Feature = row.names(imp), Importance = imp[, 1])
# 计算各特征的 %IncMSE
total_importance <- sum(featureImportance$Importance)
featureImportance$PercentageIncMSE <- (featureImportance$Importance / total_importance) * 100
# 添加显著性标记
max_importance <- max(featureImportance$PercentageIncMSE)
featureImportance$Significance <- ""
# 用 else if 结构设置显著性标记
featureImportance$Significance[featureImportance$PercentageIncMSE > max_importance * 0.9] <- "***" # 三颗星
featureImportance$Significance[featureImportance$PercentageIncMSE > max_importance * 0.7 &
featureImportance$Significance == ""] <- "**" # 两颗星
featureImportance$Significance[featureImportance$PercentageIncMSE > max_importance * 0.5 &
featureImportance$Significance == ""] <- "*" # 一颗星
print(featureImportance)
# 添加特征类别
featureImportance$Category <- ""
featureImportance$Category[featureImportance$Feature %in% c("precipitation", "rainfallPOC")] <- "Rainfall Features"
featureImportance$Category[featureImportance$Feature %in% c("velocity", "WT", "DO", "streampH")] <- "Stream Hydrochemical Features"
featureImportance$Category[featureImportance$Feature %in% c("TClitter_stream", "TClitter_shore")] <- "Litter Features"
featureImportance$Category[featureImportance$Feature %in% c("soilTN", "soilTP", "leachatepH", "leachate_amount",
"leachateTN", "leachateDOC", "leachatePOC")] <- "Soil Features"
featureImportance$Category[featureImportance$Feature %in% c("sedimentTC", "sedimentTN")] <- "Sediment Features"
# 定义颜色
colors <- c(
"Rainfall Features" = "#519D78",
"Stream Hydrochemical Features" = "#8BCF8B",
"Litter Features" = "#AADCA9",
"Soil Features" = "#C4E9CA",
"Sediment Features" = "#DDF3DE"
)
# 确保 Feature 是因子,并设置因子的顺序
featureImportance$Feature <- factor(featureImportance$Feature, levels = featureImportance$Feature)
featureImportance$Category <- factor(featureImportance$Category) # 确保类别是因子
# 创建图形
p <- ggplot(featureImportance, aes(x = reorder(Feature, PercentageIncMSE), y = PercentageIncMSE, fill = Category)) +
geom_bar(stat = "identity", width = 0.6, color = "black") +
scale_fill_manual(values = colors) + # 手动指定填充颜色
geom_text(aes(label = Significance), hjust = -0.5, size = 8) +
coord_flip() +
theme_light(base_size = 20) +
theme(
axis.title.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.title.y = element_blank(),
axis.text.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.text.y = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = c(0.8, 0.5), # 图例
legend.title = element_text(size = 12, face = "plain", family = "Times New Roman"), # 设置图例标题的样式
legend.text = element_text(size = 10, face = "plain", family = "Times New Roman") # 设置图例文本的样式
)
# 添加R方和P值到图中
if (!is.null(r_squared) && !is.null(p_value)) {
p <- p +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportance$PercentageIncMSE) * 0.9,
label = paste("R² =", round(r_squared, 3)), size = 6, hjust = 0) +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportance$PercentageIncMSE) * 0.85,
label = paste("P =", format(p_value, scientific = TRUE)), size = 6, hjust = 0)
}
return(list(plot = p, featureImportance = featureImportance)) # 返回图形和特征重要性数据框
}
# 设置随机种子
set.seed(123)
# 读取数据(此处需要您本地存在的 CSV 文件)
mydata <- read.csv("C13.csv")
# 进行随机森林分析
rf1 <- randomForest(
DOC ~ precipitation + rainfallPOC + velocity + WT + DO +
streampH + TClitter_stream + TClitter_shore +
soilTN + soilTP + leachatepH + leachate_amount +
leachateTN + leachateDOC + leachatePOC +
sedimentTC + sedimentTN,
ntree = 400,
data = mydata,
nodesize = 1,
replace = FALSE,
importance = TRUE
)
# 计算R方和P值
predictions <- predict(rf1, mydata)
r_squared <- cor(mydata$DOC, predictions)^2
lm_model <- lm(mydata$DOC ~ predictions)
summary_lm <- summary(lm_model)
p_value <- summary_lm$coefficients[2, 4] # 获取自变量(predictions)的P值
# 调用绘图函数并获取特征重要性
rf_output <- create_rfplot(rf1, type = 1, r_squared = r_squared, p_value = p_value) # 使用type = 1以获取Mean Decrease in MSE
# 提取并查看特征重要性
featureImportance <- rf_output$featureImportance
print(featureImportance)
# 绘图
print(rf_output$plot)
重点修改:
使用 PercentageIncMSE 来代替 Importance 作为 Y 轴的值。
X 轴设置为特征的 %IncMSE 代表特征在预测中的相对贡献。
确保在绘图时正确调用 type = 1 来得到所需的结果。
执行这些调整后,您应该能够看到包含关系到特征的重要性的条形图,并且图中会显示 ( R^2 ) 和 P 值的标注。
S i g n i f i c a n c e [ f e a t u r e I m p o r t a n c e Significance[featureImportance S i g ni f i c an ce [ f e a t u r e I m p or t an ce Significance == ""] <- "**" # 两颗星
featureImportance
Importance > max_importance * 0.5 &
featureImportance
添加特征类别 featureImportanceC a t e g o r y < − " " f e a t u r e I m p o r t a n c e Category <- ""
featureImportance C a t e g or y < − "" f e a t u r e I m p or t an ce Category[featureImportanceF e a t u r e f e a t u r e I m p o r t a n c e Feature %in% c("precipitation", "rainfallPOC")] <- "Rainfall Features"
featureImportance F e a t u r e f e a t u r e I m p or t an ce Category[featureImportanceF e a t u r e f e a t u r e I m p o r t a n c e Feature %in% c("velocity", "WT", "DO", "streampH")] <- "Stream Hydrochemical Features"
featureImportance F e a t u r e f e a t u r e I m p or t an ce Category[featureImportanceF e a t u r e f e a t u r e I m p o r t a n c e Feature %in% c("TClitter_stream", "TClitter_shore")] <- "Litter Features"
featureImportance F e a t u r e f e a t u r e I m p or t an ce Category[featureImportanceF e a t u r e " l e a c h a t e T N " , " l e a c h a t e D O C " , " l e a c h a t e P O C " ) ] < − " S o i l F e a t u r e s " f e a t u r e I m p o r t a n c e Feature %in% c("soilTN", "soilTP", "leachatepH", "leachate_amount",
"leachateTN", "leachateDOC", "leachatePOC")] <- "Soil Features"
featureImportance F e a t u r e " l e a c Category[featureImportance$Feature %in% c("sedimentTC", "sedimentTN")] <- "Sediment Features"
定义颜色 colors <- c(
"Rainfall Features" = "#519D78",
"Stream Hydrochemical Features" = "#8BCF8B",
"Litter Features" = "#AADCA9",
"Soil Features" = "#C4E9CA",
"Sediment Features" = "#DDF3DE"
)
确保 Feature 是因子,并设置因子的顺序 featureImportanceF e a t u r e < − f a c t o r ( f e a t u r e I m p o r t a n c e Feature <- factor(featureImportance F e a t u r e < − f a c t or ( f e a t u r e I m p or t an ce Feature, levels = featureImportanceF e a t u r e ) f e a t u r e I m p o r t a n c e Feature)
featureImportance F e a t u r e ) f e a t u r e I m p or t an ce Category <- factor(featureImportance$Category) # 确保类别是因子
创建图形 p <- ggplot(featureImportance, aes(x = reorder(Feature, Importance), y = Importance, fill = Category)) +
geom_bar(stat = "identity", width = 0.6, color = "black") +
scale_fill_manual(values = colors) + # 手动指定填充颜色
geom_text(aes(label = Significance), hjust = -0.5, size = 8) +
coord_flip() +
theme_light(base_size = 20) +
theme(
axis.title.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.title.y = element_blank(),
axis.text.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.text.y = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = c(0.8, 0.5), # 图例
legend.title = element_text(size = 12, face = "plain", family = "Times New Roman"), # 设置图例标题的样式
legend.text = element_text(size = 10, face = "plain", family = "Times New Roman") # 设置图例文本的样式
)
添加R方和P值到图中 if (!is.null(r_squared) && !is.null(p_value)) {
p <- p +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportanceI m p o r t a n c e ) ∗ 0.9 , l a b e l = p a s t e ( " R 2 = " , r o u n d ( r s q u a r e d , 3 ) ) , s i z e = 6 , h j u s t = 0 ) + a n n o t a t e ( " t e x t " , x = n r o w ( f e a t u r e I m p o r t a n c e ) ∗ 0.7 , y = m a x ( f e a t u r e I m p o r t a n c e Importance) * 0.9,
label = paste("R² =", round(r_squared, 3)), size = 6, hjust = 0) +
annotate("text", x = nrow(featureImportance) * 0.7, y = max(featureImportance I m p or t an ce ) ∗ 0.9 , l ab e l = p a s t e ( " R 2 = " , r o u n d ( r Importance) * 0.85,
label = paste("P =", format(p_value, scientific = TRUE)), size = 6, hjust = 0)
}
return(list(plot = p, featureImportance = featureImportance)) # 返回图形和特征重要性数据框
}
设置随机种子
读取数据(此处需要您本地存在的 CSV 文件) mydata <- read.csv("C13.csv")
进行随机森林分析 rf1 <- randomForest(
DOC ~ precipitation + rainfallPOC + velocity + WT + DO +
streampH + TClitter_stream + TClitter_shore +
soilTN + soilTP + leachatepH + leachate_amount +
leachateTN + leachateDOC + leachatePOC +
sedimentTC + sedimentTN,
ntree = 400,
data = mydata,
nodesize = 1,
replace = FALSE,
importance = TRUE
)
计算R方和P值 predictions <- predict(rf1, mydata)
r_squared <- cor(mydataD O C , p r e d i c t i o n s ) 2 l m m o d e l < − l m ( m y d a t a DOC, predictions)^2
lm_model <- lm(mydata D O C , p r e d i c t i o n s ) 2 l m m o d e l < − l m ( m y d a t a DOC ~ predictions)
summary_lm <- summary(lm_model)
p_value <- summary_lm$coefficients[2, 4] # 获取自变量(predictions)的P值
调用绘图函数并获取特征重要性 rf_output <- create_rfplot(rf1, type = 2, r_squared = r_squared, p_value = p_value)
提取并查看特征重要性 featureImportance <- rf_output$featureImportance
print(featureImportance)
绘图 print(rf_output$plot) 这代码我想要X轴显示IncNodePurity,怎么修改代码
library(randomForest)
library(ggplot2)
library(extrafont)
# 创建函数
create_rfplot <- function(rf, type, significance_threshold = 0.05, r_squared = NULL, p_value = NULL) {
# 计算特征重要性
imp <- importance(rf, type = type, scale = FALSE)
featureImportance <- data.frame(Feature = row.names(imp),
IncNodePurity = imp[, 2]) # 取第二列即 IncNodePurity
# 添加显著性标记
max_importance <- max(featureImportance$IncNodePurity)
featureImportance$Significance <- ""
# 用 ifelse 结构设置显著性标记
featureImportance$Significance[featureImportance$IncNodePurity > max_importance * 0.9] <- "***" # 三颗星
featureImportance$Significance[featureImportance$IncNodePurity > max_importance * 0.7 &
featureImportance$Significance == ""] <- "**" # 两颗星
featureImportance$Significance[featureImportance$IncNodePurity > max_importance * 0.5 &
featureImportance$Significance == ""] <- "*" # 一颗星
print(featureImportance)
# 添加特征类别
featureImportance$Category <- ""
featureImportance$Category[featureImportance$Feature %in% c("precipitation", "rainfallPOC")] <- "Rainfall Features"
featureImportance$Category[featureImportance$Feature %in% c("velocity", "WT", "DO", "streampH")] <- "Stream Hydrochemical Features"
featureImportance$Category[featureImportance$Feature %in% c("TClitter_stream", "TClitter_shore")] <- "Litter Features"
featureImportance$Category[featureImportance$Feature %in% c("soilTN", "soilTP", "leachatepH", "leachate_amount",
"leachateTN", "leachateDOC", "leachatePOC")] <- "Soil Features"
featureImportance$Category[featureImportance$Feature %in% c("sedimentTC", "sedimentTN")] <- "Sediment Features"
# 定义颜色
colors <- c(
"Rainfall Features" = "#519D78",
"Stream Hydrochemical Features" = "#8BCF8B",
"Litter Features" = "#AADCA9",
"Soil Features" = "#C4E9CA",
"Sediment Features" = "#DDF3DE"
)
# 确保 Feature 是因子,并设置因子的顺序
featureImportance$Feature <- factor(featureImportance$Feature, levels = featureImportance$Feature)
featureImportance$Category <- factor(featureImportance$Category) # 确保类别是因子
# 创建图形
p <- ggplot(featureImportance, aes(x = reorder(Feature, IncNodePurity), y = IncNodePurity, fill = Category)) +
geom_bar(stat = "identity", width = 0.6, color = "black") +
scale_fill_manual(values = colors) + # 手动指定填充颜色
geom_text(aes(label = Significance), hjust = -0.5, size = 8) +
coord_flip() +
theme_light(base_size = 20) +
theme(
axis.title.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.title.y = element_blank(),
axis.text.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.text.y = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = c(0.8, 0.5), # 图例
legend.title = element_text(size = 12, face = "plain", family = "Times New Roman"), # 设置图例标题的样式
legend.text = element_text(size = 10, face = "plain", family = "Times New Roman") # 设置图例文本的样式
)
# 添加R方和P值到图中
if (!is.null(r_squared) && !is.null(p_value)) {
p <- p +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportance$IncNodePurity) * 0.9,
label = paste("R² =", round(r_squared, 3)), size = 6, hjust = 0) +
annotate("text", x = nrow(featureImportance) * 0.7, y = max(featureImportance$IncNodePurity) * 0.85,
label = paste("P =", format(p_value, scientific = TRUE)), size = 6, hjust = 0)
}
return(list(plot = p, featureImportance = featureImportance)) # 返回图形和特征重要性数据框
}
# 设置随机种子
set.seed(123)
# 读取数据(此处需要您本地存在的 CSV 文件)
mydata <- read.csv("C13.csv")
# 进行随机森林分析
rf1 <- randomForest(
DOC ~ precipitation + rainfallPOC + velocity + WT + DO +
streampH + TClitter_stream + TClitter_shore +
soilTN + soilTP + leachatepH + leachate_amount +
leachateTN + leachateDOC + leachatePOC +
sedimentTC + sedimentTN,
ntree = 400,
data = mydata,
nodesize = 1,
replace = FALSE,
importance = TRUE
)
# 调用绘图函数并获取特征重要性
rf_output <- create_rfplot(rf1, type = 1) # 使用 type = 1 获取 IncNodePurity
# 提取并查看特征重要性
featureImportance <- rf_output$featureImportance
print(featureImportance)
# 绘图
print(rf_output$plot)
关键修改:
获取 IncNodePurity : 在 importance 函数中,获取第二列(即IncNodePurity)并保存于 featureImportance 数据框中。
设置 Y 轴 : 将 Y 轴的值设置为 IncNodePurity。
调用绘图函数时使用 type = 1 : 这样可以确保获取特征重要性的相关度量。
使用这些修改后,您的图形将显示 IncNodePurity,同时 ( R^2 ) 和 P 值也会被正确地添加到图中。
" # 三颗星
featureImportanceS i g n i f i c a n c e [ f e a t u r e I m p o r t a n c e Significance[featureImportance S i g ni f i c an ce [ f e a t u r e I m p or t an ce Importance > max_importance * 0.7 &
featureImportanceSignificance == ""] <- "**" # 两颗星
featureImportance Significance[featureImportanceImportance > max_importance * 0.5 &
featureImportance Significance == ""] <- "
添加特征类别 featureImportanceC a t e g o r y < − " " f e a t u r e I m p o r t a n c e Category <- ""
featureImportance C a t e g or y < − "" f e a t u r e I m p or t an ce Category[featureImportanceF e a t u r e f e a t u r e I m p o r t a n c e Feature %in% c("precipitation", "rainfallPOC")] <- "Rainfall Features"
featureImportance F e a t u r e f e a t u r e I m p or t an ce Category[featureImportanceF e a t u r e f e a t u r e I m p o r t a n c e Feature %in% c("velocity", "WT", "DO", "streampH")] <- "Stream Hydrochemical Features"
featureImportance F e a t u r e f e a t u r e I m p or t an ce Category[featureImportanceF e a t u r e f e a t u r e I m p o r t a n c e Feature %in% c("TClitter_stream", "TClitter_shore")] <- "Litter Features"
featureImportance F e a t u r e f e a t u r e I m p or t an ce Category[featureImportanceF e a t u r e " l e a c h a t e T N " , " l e a c h a t e D O C " , " l e a c h a t e P O C " ) ] < − " S o i l F e a t u r e s " f e a t u r e I m p o r t a n c e Feature %in% c("soilTN", "soilTP", "leachatepH", "leachate_amount",
"leachateTN", "leachateDOC", "leachatePOC")] <- "Soil Features"
featureImportance F e a t u r e " l e a c Category[featureImportance$Feature %in% c("sedimentTC", "sedimentTN")] <- "Sediment Features"
定义颜色 colors <- c(
"Rainfall Features" = "#519D78",
"Stream Hydrochemical Features" = "#8BCF8B",
"Litter Features" = "#AADCA9",
"Soil Features" = "#C4E9CA",
"Sediment Features" = "#DDF3DE"
)
确保 Feature 是因子,并设置因子的顺序 featureImportanceF e a t u r e < − f a c t o r ( f e a t u r e I m p o r t a n c e Feature <- factor(featureImportance F e a t u r e < − f a c t or ( f e a t u r e I m p or t an ce Feature, levels = featureImportanceF e a t u r e ) f e a t u r e I m p o r t a n c e Feature)
featureImportance F e a t u r e ) f e a t u r e I m p or t an ce Category <- factor(featureImportance$Category) # 确保类别是因子
创建图形 p <- ggplot(featureImportance, aes(x = reorder(Feature, Importance), y = Importance, fill = Category)) +
geom_bar(stat = "identity", width = 0.6, color = "black") +
scale_fill_manual(values = colors) + # 手动指定填充颜色
geom_text(aes(label = Significance), hjust = -0.5, size = 8) +
coord_flip() +
theme_light(base_size = 20) +
theme(
axis.title.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.title.y = element_blank(),
axis.text.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.text.y = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = c(0.8, 0.5), # 图例
legend.title = element_text(size = 12, face = "plain", family = "Times New Roman"), # 设置图例标题的样式
legend.text = element_text(size = 10, face = "plain", family = "Times New Roman") # 设置图例文本的样式
)
添加R方和P值到图中 if (!is.null(r_squared) && !is.null(p_value)) {
p <- p +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportanceI m p o r t a n c e ) ∗ 0.9 , l a b e l = p a s t e ( " R 2 = " , r o u n d ( r s q u a r e d , 3 ) ) , s i z e = 6 , h j u s t = 0 ) + a n n o t a t e ( " t e x t " , x = n r o w ( f e a t u r e I m p o r t a n c e ) ∗ 0.7 , y = m a x ( f e a t u r e I m p o r t a n c e Importance) * 0.9,
label = paste("R² =", round(r_squared, 3)), size = 6, hjust = 0) +
annotate("text", x = nrow(featureImportance) * 0.7, y = max(featureImportance I m p or t an ce ) ∗ 0.9 , l ab e l = p a s t e ( " R 2 = " , r o u n d ( r Importance) * 0.85,
label = paste("P =", format(p_value, scientific = TRUE)), size = 6, hjust = 0)
}
return(list(plot = p, featureImportance = featureImportance)) # 返回图形和特征重要性数据框
}
设置随机种子
读取数据(此处需要您本地存在的 CSV 文件) mydata <- read.csv("C13.csv")
进行随机森林分析 rf1 <- randomForest(
DOC ~ precipitation + rainfallPOC + velocity + WT + DO +
streampH + TClitter_stream + TClitter_shore +
soilTN + soilTP + leachatepH + leachate_amount +
leachateTN + leachateDOC + leachatePOC +
sedimentTC + sedimentTN,
ntree = 400,
data = mydata,
nodesize = 1,
replace = FALSE,
importance = TRUE
)
计算R方和P值 predictions <- predict(rf1, mydata)
r_squared <- cor(mydataD O C , p r e d i c t i o n s ) 2 l m m o d e l < − l m ( m y d a t a DOC, predictions)^2
lm_model <- lm(mydata D O C , p r e d i c t i o n s ) 2 l m m o d e l < − l m ( m y d a t a DOC ~ predictions)
summary_lm <- summary(lm_model)
p_value <- summary_lm$coefficients[2, 4] # 获取自变量(predictions)的P值
调用绘图函数并获取特征重要性 rf_output <- create_rfplot(rf1, type = 2, r_squared = r_squared, p_value = p_value)
提取并查看特征重要性 featureImportance <- rf_output$featureImportance
print(featureImportance)
绘图 print(rf_output$plot)
这串代码X轴表示的是什么 中文怎么表示
s
q
u
a
r
e
d
,
3
))
,
s
i
z
e
=
6 , hj u s t =
0 , f a ce =
" pl ain " , f ami l y =
" T im es N e w R o man " ) +
ann o t a t e ( " t e x t " , x =
n r o w ( f e a t u r e I m p or t an ce ) ∗
0.7 , y =
ma x ( f e a t u r e I m p or t an ce
# 创建图形
p <- ggplot(featureImportance, aes(x = reorder(Feature, Importance), y = Importance, fill = Category)) +
geom_bar(stat = "identity", width = 0.6, color = "black") +
scale_fill_manual(values = colors) + # 手动指定填充颜色
geom_text(aes(label = Significance), hjust = -0.5, size = 8) +
coord_flip() +
theme_light(base_size = 20) +
theme(
axis.title.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.title.y = element_blank(),
axis.text.x = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
axis.text.y = element_text(size = 15, color = "black", face = "plain", family = "Times New Roman"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = c(0.8, 0.5),
legend.title = element_text(size = 12, face = "plain", family = "Times New Roman"),
legend.text = element_text(size = 10, face = "plain", family = "Times New Roman")
)
# 添加R方和P值到图中
if (!is.null(r_squared) && !is.null(p_value)) {
p <- p +
annotate("text", x = nrow(featureImportance) * 0.8, y = max(featureImportance$Importance) * 0.9,
label = paste("R² =", round(r_squared, 3)), size = 6, hjust = 0,
face = "plain", family = "Times New Roman") +
annotate("text", x = nrow(featureImportance) * 0.7, y = max(featureImportance$Importance) * 0.85,
label = bquote('P =' ~ italic(.(format(p_value, scientific = TRUE)))), size = 6,
hjust = 0, family = "Times New Roman")
}
# 绘图
print(p)
总结 通过使用 bquote 和 italic,您可以在图形中实现文本的部分斜体显示,达到您需要的视觉效果。希望这些修改能够满足您的需求!
s
q
u
a
r
e
d
,
3
))
,
s
i
z
e
=
6 , hj u s t =
0 , f a ce =
" pl ain " , f ami l y =
" T im es N e w R o man " ) +
ann o t a t e ( " t e x t " , x =
n r o w ( f e a t u r e I m p or t an ce ) ∗
0.7 , y =
ma x ( f e a t u r e I m p or t an ce
ha
t
e
T
N
"
,
"
l
e
a
c
ha
t
eD
O
C
"
,
"
l
e
a
c
ha
t
e
P
O
C
"
)]
<
− " S o i l F e a t u r es " f e a t u r e I m p or t an ce
ha
t
e
T
N
"
,
"
l
e
a
c
ha
t
eD
O
C
"
,
"
l
e
a
c
ha
t
e
P
O
C
"
)]
<
− " S o i l F e a t u r es " f e a t u r e I m p or t an ce
s
q
u
a
r
e
d
,
3
))
,
s
i
z
e
=
6 , hj u s t =
0 ) +
ann o t a t e ( " t e x t " , x =
n r o w ( f e a t u r e I m p or t an ce ) ∗
0.5 , y =
ma x ( f e a t u r e I m p or t an ce
ha
t
e
T
N
"
,
"
l
e
a
c
ha
t
eD
O
C
"
,
"
l
e
a
c
ha
t
e
P
O
C
"
)]
<
− " S o i l F e a t u r es " f e a t u r e I m p or t an ce
s
q
u
a
r
e
d
,
3
))
,
s
i
z
e
=
6 , hj u s t =
0 ) +
ann o t a t e ( " t e x t " , x =
n r o w ( f e a t u r e I m p or t an ce ) ∗
0.5 , y =
ma x ( f e a t u r e I m p or t an ce
ha
t
e
T
N
"
,
"
l
e
a
c
ha
t
eD
O
C
"
,
"
l
e
a
c
ha
t
e
P
O
C
"
)]
<
− " S o i l F e a t u r es " f e a t u r e I m p or t an ce
s
q
u
a
r
e
d
,
3
))
,
s
i
z
e
=
6 , hj u s t =
0 ) +
ann o t a t e ( " t e x t " , x =
n r o w ( f e a t u r e I m p or t an ce ) ∗
0.7 , y =
ma x ( f e a t u r e I m p or t an ce
ha
t
e
T
N
"
,
"
l
e
a
c
ha
t
eD
O
C
"
,
"
l
e
a
c
ha
t
e
P
O
C
"
)]
<
− " S o i l F e a t u r es " f e a t u r e I m p or t an ce
s
q
u
a
r
e
d
,
3
))
,
s
i
z
e
=
6 , hj u s t =
0 ) +
ann o t a t e ( " t e x t " , x =
n r o w ( f e a t u r e I m p or t an ce ) ∗
0.7 , y =
ma x ( f e a t u r e I m p or t an ce
ha
t
e
T
N
"
,
"
l
e
a
c
ha
t
eD
O
C
"
,
"
l
e
a
c
ha
t
e
P
O
C
"
)]
<
− " S o i l F e a t u r es " f e a t u r e I m p or t an ce
s
q
u
a
r
e
d
,
3
))
,
s
i
z
e
=
6 , hj u s t =
0 ) +
ann o t a t e ( " t e x t " , x =
n r o w ( f e a t u r e I m p or t an ce ) ∗
0.7 , y =
ma x ( f e a t u r e I m p or t an ce
ha
t
e
T
N
"
,
"
l
e
a
c
ha
t
eD
O
C
"
,
"
l
e
a
c
ha
t
e
P
O
C
"
)]
<
− " S o i l F e a t u r es " f e a t u r e I m p or t an ce
s
q
u
a
r
e
d
,
3
))
,
s
i
z
e
=
6 , hj u s t =
0 ) +
ann o t a t e ( " t e x t " , x =
n r o w ( f e a t u r e I m p or t an ce ) ∗
0.7 , y =
ma x ( f e a t u r e I m p or t an ce
ha
t
e
T
N
"
,
"
l
e
a
c
ha
t
eD
O
C
"
,
"
l
e
a
c
ha
t
e
P
O
C
"
)]
<
− " S o i l F e a t u r es " f e a t u r e I m p or t an ce
s
q
u
a
r
e
d
,
3
))
,
s
i
z
e
=
6 , hj u s t =
0 ) +
ann o t a t e ( " t e x t " , x =
n r o w ( f e a t u r e I m p or t an ce ) ∗
0.7 , y =
ma x ( f e a t u r e I m p or t an ce