将预测结合到R中的数据框中,然后导出到excel中

时间:2016-01-07 15:57:39

标签: r excel forecasting

我目前在R中运行多个auto.arima()预测,以生成一系列具有置信区间的点预测,我希望能够直接拉入Excel。我的部分数据显示了我目前在下面使用的脚本示例。

require(forecast)


# Customer GM ARIMA Forecasts (1 Quarter Ahead)

F1 <- read.csv("C:/datapath/Desktop/dataname.csv")

F1 <- ts(F1, frequency = 12, start = c(2014, 1), end = c(2015, 12))

Coonan <- F1[,3]
Gallo  <- F1[,4]
Kempton<- F1[,5]
Moore  <- F1[,6]
Nekic  <- F1[,7]


fit.Coonan  <- auto.arima(Coonan, stepwise = FALSE)
fc.Coonan   <- forecast(fit.Coonan, h=3, level = c(20, 40, 80))

fit.Gallo   <- auto.arima(Gallo, stepwise = FALSE)
fc.Gallo    <- forecast(fit.Gallo, h=3, level = c(20, 40, 80))

fit.Kempton <- auto.arima(Kempton, stepwise = FALSE)
fc.Kempton  <- forecast(fit.Kempton, h=3, level = c(20, 40, 80))

fit.Kempton <- auto.arima(Kempton, stepwise = FALSE)
fc.Kempton  <- forecast(fit.Kempton, h=3, level = c(20, 40, 80))

fit.Moore   <- auto.arima(Moore, stepwise = FALSE)
fc.Moore    <- forecast(fit.Moore, h=3, level = c(20, 40, 80))

fit.Nekic   <- auto.arima(Nekic, stepwise = FALSE)
fc.Nekic    <- forecast(fit.Nekic, h=3, level = c(20, 40, 80))



# Save to clipboard to copy and paste into excel

write.excel <- function(x,row.names=TRUE,col.names=TRUE,...) {
  write.table(x,"clipboard",sep="\t",row.names=row.names,col.names=col.names,...)
}

write.excel(fc.Coonan)   # Then can paste Coonan Forecasts Directly into excel

将我的结果粘贴到excel之后,我得到一张看起来像这样的表(我想转移列名,但现在这不是一个大问题)。 enter image description here

按照目前的编写,我需要在底部函数中手动更改模型的名称,运行函数(为了将结果保存到剪贴板),然后将结果复制并粘贴到excel中。这个过程变得非常耗时,我想知道是否有一种简单的方法可以将我的一系列点预测和置信区间组合成一个数据框,然后我可以一次性导出到excel中。

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

这是使用openxlsx的一个,我觉得它比任何其他软件包都要好,因为它使用的是C ++而不是Java,它通常会耗尽内存甚至是小页。

如果收到错误,您可能需要将路径设置为zip

  

错误:压缩工作簿失败。请确保安装了Rtools或者R可以使用zip应用程序。            在Windows上尝试installr :: install.rtools()。

library(forecast)
library(openxlsx)
Sys.setenv(R_ZIPCMD = "C:/RBuildTools/3.1/bin/zip")

# create dummy data
library(data.table)
set.seed(1)
build <- data.table()
F1 <- build[, lapply(seq(7), function(x) runif(24))]

F1 <- ts(F1, frequency = 12, start = c(2014, 1), end = c(2015, 12))

Coonan <- F1[,3]
Gallo  <- F1[,4]
Kempton<- F1[,5]
Moore  <- F1[,6]
Nekic  <- F1[,7]

results <- list()

fit.Coonan  <- auto.arima(Coonan, stepwise = FALSE)
results[["Coonan"]] <- forecast(fit.Coonan, h=3, level = c(20, 40, 80))

fit.Gallo   <- auto.arima(Gallo, stepwise = FALSE)
results[["Gallo"]] <- forecast(fit.Gallo, h=3, level = c(20, 40, 80))

fit.Kempton <- auto.arima(Kempton, stepwise = FALSE)
results[["Kempton"]] <- forecast(fit.Kempton, h=3, level = c(20, 40, 80))

fit.Moore   <- auto.arima(Moore, stepwise = FALSE)
results[["Moore"]] <- forecast(fit.Moore, h=3, level = c(20, 40, 80))

fit.Nekic   <- auto.arima(Nekic, stepwise = FALSE)
results[["Nekic"]] <- forecast(fit.Nekic, h=3, level = c(20, 40, 80))

results_together <- do.call(rbind,lapply(names(results),function(x){
  transform(as.data.frame(results[[x]]), Name = x)
}))

wb <- createWorkbook()

addWorksheet(wb, "Forecasts")
writeData(wb, "Forecasts", results_together, rowNames = TRUE)
saveWorkbook(wb, "Forcasts.xlsx", overwrite = TRUE)

结果如下:

enter image description here

您还可以将每个结果放在自己的标签上(添加或不添加Name列):

wb <- createWorkbook()

for (nm in names(results)){
  addWorksheet(wb, nm)
  writeData(wb, nm, results[[nm]], rowNames = TRUE)
}

saveWorkbook(wb, "Forcasts.xlsx", overwrite = TRUE)  

导致:

enter image description here

答案 1 :(得分:0)

mlegge,

这是一个很棒的解决方案。我遇到了RTools的问题,并使用write.csv遇到了一种解决方法:

write.csv(results_together,file =“results_together.csv”)

-phos