`data`必须是一个数据框,或者其他可以被`fortify()`强制的对象,而不是一个带有ranger类的S3对象
我正在使用 R。使用教程,我能够创建一个统计模型并为一些输出生成可视化图:
#load libraries
library(survival)
library(dplyr)
library(ranger)
library(data.table)
library(ggplot2)
#use the built in "lung" data set
#remove missing values (dataset is called "a")
a <- na.omit(lung)
#create id variable
a$ID <- seq_along(a[,1])
#create test set with only the first 3 rows
new <- a[1:3,]
#create a training set by removing first three rows
a <- a[-c(1:3),]
#fit survival model (random survival forest)
r_fit <- ranger(Surv(time,status) ~ age + sex + ph.ecog + ph.karno + pat.karno + meal.cal + wt.loss, data = a, mtry = 4, importance = "permutation", splitrule = "extratrees", verbose = TRUE)
#create new intermediate variables required for the survival curves
death_times <- r_fit$unique.death.times
surv_prob <- data.frame(r_fit$survival)
avg_prob <- sapply(surv_prob, mean)
#use survival model to produce estimated survival curves for the first three observations
pred <- predict(r_fit, new, type = 'response')$survival
pred <- data.table(pred)
colnames(pred) <- as.character(r_fit$unique.death.times)
#plot the results for these 3 patients
plot(r_fit$unique.death.times, pred[1,], type = "l", col = "red")
lines(r_fit$unique.death.times, pred[2,], type = "l", col = "green")
lines(r_fit$unique.death.times, pred[3,], type = "l", col = "blue")
现在,我正在尝试将上面的图转换为 ggplot 格式(并添加 95% 置信区间):
ggplot(r_fit) + geom_line(aes(x = r_fit$unique.death.times, y = pred[1,], group = 1), color = red) + geom_ribbon(aes(ymin = 0.95 * pred[1,], ymax = - 0.95 * pred[1,]), fill = "red") + geom_line(aes(x = r_fit$unique.death.times, y = pred[2,], group = 1), color = blue) + geom_ribbon(aes(ymin = 0.95 * pred[2,], ymax = - 0.95 * pred[2,]), fill = "blue") + geom_line(aes(x = r_fit$unique.death.times, y = pred[3,], group = 1), color = green) + geom_ribbon(aes(ymin = 0.95 * pred[3,], ymax = - 0.95 * pred[3,]), fill = "green") + theme(axis.text.x = element_text(angle = 90)) + ggtitle("sample graph")
但这会产生以下错误:
Error: `data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class ranger
Run `rlang::last_error()` to see where the error occurred.
这个错误的原因是什么?有人可以告诉我如何解决这个问题吗?
谢谢
回答
根据ggplot2 文档,您需要提供data.frame()可以转换(强制)为data.frame(). 在这种情况下,如果要在 ggplot2 中重现上面的图,则需要自己手动设置数据框。
下面是如何设置数据以在 ggplot2 中显示绘图的示例。
数据帧
首先data.frame(),我们使用要绘制的变量创建一个。最简单的方法是将它们全部分组为单独的列。请注意,我已使用该as.numeric()函数首先将预测值强制转换为向量,因为它们以前是data.table一行,如果您不转换它们,它们将保留为行。
ggplot_data <- data.frame(unique.death.times = r_fit$unique.death.times,
pred1 = as.numeric(pred[1,]),
pred2 = as.numeric(pred[2,]),
pred3 = as.numeric(pred[3,]))
head(ggplot_data)
## unique.death.times pred1 pred2 pred3
## 1 5 0.9986676 1.0000000 0.9973369
## 2 11 0.9984678 1.0000000 0.9824642
## 3 12 0.9984678 0.9998182 0.9764154
## 4 13 0.9984678 0.9998182 0.9627118
## 5 15 0.9731656 0.9959416 0.9527424
## 6 26 0.9731656 0.9959416 0.9093876
透视数据
这种格式仍然不理想,因为为了按正确的列(变量)绘制数据和颜色,我们需要“旋转”数据。我们需要为此加载tidyr 包。
library(tidyr)
ggplot_data <- ggplot_data %>%
pivot_longer(cols = !unique.death.times,
names_to = "category", values_to = "predicted.value")
绘图
现在数据的形式使得在 ggplot2 中绘图变得非常容易。
plot <- ggplot(ggplot_data, aes(x = unique.death.times, y = predicted.value, colour = category)) +
geom_line()
plot
如果你真的想匹配基本图的外观,你可以添加theme_classic():
plot + theme_classic()
补充说明
请注意,这不包括 95% 的置信区间,因此它们必须单独计算。要知道,虽然,一个95%的置信区间是不是y值仅为95%,在一个给定的x值。有一些计算可以为您提供正确的置信区间值,包括内置于 R 中的函数。
要快速查看带有预测区间的趋势线,您可以使用geom_smooth()ggplot2 中的函数,但在这种情况下,它默认添加了一条loess 曲线,以及该函数提供的区间。
plot + theme_classic() + geom_smooth()
THE END
二维码