我正在尝试使用以下代码来找到一种方法,将fancyRpartPlot之后出现的图像输出保存为pdf,以绘制回归树。 谁知道我怎么能这样做?我无法通过网络找到任何答案。
regression_tree <- data.frame(stock_mag = as.numeric(resData$stock_mag),
LT = as.numeric(resData$Lead_Time),
dmIn = as.numeric(resData$Intermittency),
dmCv = as.numeric(resData$CoV))
fit<-rpart(stock_mag~dmCv+dmIn+LT,data=regression_tree, method="anova", control=rpart.control(minsplit=20))
x11()
fancyRpartPlot(fit, main="test") # Will plot the tree#
谢谢!
答案 0 :(得分:1)
这应该与使用pdf()
:
library(rattle)
library(rpart)
set.seed(42)
ds <- weather
target <- "RainTomorrow"
risk <- "RISK_MM"
ignore <- c("Date", "Location", risk)
vars <- setdiff(names(ds), ignore)
nobs <- nrow(ds)
form <- formula(paste(target, "~ ."))
train <- sample(nobs, 0.7*nobs)
test <- setdiff(seq_len(nobs), train)
actual <- ds[test, target]
risks <- ds[test, risk]
model <- rpart(form, data=ds[train, vars])
pdf(tf <- tempfile(fileext = ".pdf"))
fancyRpartPlot(model)
dev.off()
cat(tf) # filename