将预测组合到 R 中的数据框中,然后导出到 excel
Combining forecasts into a data frame in R and then exporting into excel
我目前 运行在 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 后,我得到一个 table,看起来像这样(我想转移列名,但现在这不是大问题)。
按照目前的写法,我需要手动更改底部函数中模型的名称,运行函数(以便将结果保存到剪贴板),然后将结果复制并粘贴到excel。这个过程变得非常耗时,我想知道是否有一种简单的方法可以将我的一系列点预测和置信区间组合到一个数据框中,然后我可以一次将其全部导出到 excel.
感谢您的帮助。
这是一个使用 openxlsx
的包,我发现它比任何其他包都更可取,因为它使用 C++ 而不是 Java,后者经常在写入小表时耗尽内存。
您可能需要将路径设置为 zip
,但如果出现错误:
Error: zipping up workbook failed. Please make sure Rtools is installed or a zip application is available to R.
Try installr::install.rtools() on Windows.
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)
结果:
您还可以将每个结果放在自己的选项卡上(添加或不添加 Name
列):
wb <- createWorkbook()
for (nm in names(results)){
addWorksheet(wb, nm)
writeData(wb, nm, results[[nm]], rowNames = TRUE)
}
saveWorkbook(wb, "Forcasts.xlsx", overwrite = TRUE)
导致:
腿部,
这是一个绝妙的解决方案。我遇到了 RTools 的问题,并遇到了使用 write.csv:
的解决方法
write.csv(results_together, 文件="results_together.csv")
-phos
我目前 运行在 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 后,我得到一个 table,看起来像这样(我想转移列名,但现在这不是大问题)。
按照目前的写法,我需要手动更改底部函数中模型的名称,运行函数(以便将结果保存到剪贴板),然后将结果复制并粘贴到excel。这个过程变得非常耗时,我想知道是否有一种简单的方法可以将我的一系列点预测和置信区间组合到一个数据框中,然后我可以一次将其全部导出到 excel.
感谢您的帮助。
这是一个使用 openxlsx
的包,我发现它比任何其他包都更可取,因为它使用 C++ 而不是 Java,后者经常在写入小表时耗尽内存。
您可能需要将路径设置为 zip
,但如果出现错误:
Error: zipping up workbook failed. Please make sure Rtools is installed or a zip application is available to R. Try installr::install.rtools() on Windows.
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)
结果:
您还可以将每个结果放在自己的选项卡上(添加或不添加 Name
列):
wb <- createWorkbook()
for (nm in names(results)){
addWorksheet(wb, nm)
writeData(wb, nm, results[[nm]], rowNames = TRUE)
}
saveWorkbook(wb, "Forcasts.xlsx", overwrite = TRUE)
导致:
腿部,
这是一个绝妙的解决方案。我遇到了 RTools 的问题,并遇到了使用 write.csv:
的解决方法write.csv(results_together, 文件="results_together.csv")
-phos