この Excel ファイル (左端の画像を参照) には 2 つの列があります。列 A には 2005 年 1 月から 2014 年 12 月までの期間の値があり、列 B には AA15 の重み値が含まれています。AA15 列の Holt Winters 予測 (次の 24 か月) を行い、予測値と Excel シートのプロットを含む出力として Excel ファイルを作成します (中央の画像を参照)。
R コードにより、ユーザーは Holt Winters 予測を行うための入力データを含む .csv Excel ファイルを選択できます。
AA15 Holt Winter 予測で機能する R コード:
# Install the packages : tseries, forecast, WriteXLS, ggplot2, reshape2
library(tseries)
library(forecast)
library(WriteXLS)
# Let user select the excel (.csv) file for importing the input data
AA15file <- read.csv(file.choose(), header = T)
# convert to time series
AA15 <- ts(AA15file[,2], start=c(2005,1), end= c(2014,12),frequency = 12)
AA15
# convert to Holt Winter
AA15HW <- HoltWinters(AA15, seasonal = "multiplicative",optim.start = c(alpha=0.3, beta=0.3, gamma =0.3))
AA15HW
AA15HW$fitted
summary(AA15HW)
##########################################################################################
# Combine Holt Winter Forecast plots
library(ggplot2)
library(reshape2)
HWplot<-function(AA15, n.ahead=12, CI=.95, error.ribbon='green', line.size=1){
hw_object<-HoltWinters(AA15)
forecast<-predict(hw_object, n.ahead=24, prediction.interval=T, level=0.95)
for_values<-data.frame(time=round(time(forecast), 3), value_forecast=as.data.frame(forecast)$fit, dev=as.data.frame(forecast)$upr- as.data.frame(forecast)$fit)
fitted_values<-data.frame(time=round(time(hw_object$fitted), 3), value_fitted=as.data.frame(hw_object$fitted)$xhat)
actual_values<-data.frame(time=round(time(hw_object$x), 3), Actual=c(hw_object$x))
graphset<-merge(actual_values, fitted_values, by='time', all=TRUE)
graphset<-merge(graphset, for_values, all=TRUE, by='time')
graphset[is.na(graphset$dev), ]$dev<-0
graphset$Fitted<-c(rep(NA, NROW(graphset)-(NROW(for_values) + NROW(fitted_values))), fitted_values$value_fitted, for_values$value_forecast)
graphset.melt<-melt(graphset[, c('time', 'Actual', 'Fitted')], id='time')
p<-ggplot(graphset.melt, aes(x=time, y=value)) + geom_ribbon(data=graphset, aes(x=time, y=Fitted, ymin=Fitted-dev, ymax=Fitted + dev), alpha=.2, fill=error.ribbon) + geom_line(aes(colour=variable), size=line.size) + geom_vline(x=max(actual_values$time), lty=2) + xlab('Time') + ylab('Value') + theme(legend.position='bottom') + scale_colour_hue('')
return(p)
}
# Calculate the Holt Winter Forecast values
HoltWinters(AA15)
forecast<-forecast.HoltWinters(AA15HW)
forecast$mean
forecastvalues<-data.frame(forecast$mean)
forecastvalues
# Save the plot
ggsave(filename = "zipggplotAA15.png")
png(filename = "zipggplotAA15.png", units = "px", width = 600, height = 600)
HWplot(AA15, n.ahead=12, CI=.95, error.ribbon='blue',line.size=1)
dev.off()
library(xlsx)
wb<-createWorkbook(type="xlsx")
# Create a new sheet to contain the plot
sheet <-createSheet(wb, sheetName = "ggplotFORECASTAA15")
# Add the plot created previously
addPicture("zipggplotAA15.png", sheet, scale = 1, startRow = 4,
startColumn = 5)
# Add title
xlsx.addTitle(sheet, rowIndex=1, title="ForecastPlotsggplot2AA15",
titleStyle = TITLE_STYLE)
# remove the plot from the disk
res<-file.remove("zipggplotAA15.png")
# Save the workbook to a file...
saveWorkbook(wb, "ggplotforecastplotAA15.xlsx")
#Add forecast data to excel sheet
addDataFrame(forecastvalues, sheet, startRow = 1, startColumn = 1)
saveWorkbook(wb, "ggplotforecastplotAA15.xlsx")
# The excel file and the sheet will be created in the working directory
getwd()
ここで、同じコードを適用し、ループ内のすべての関数を呼び出して、AA15、AA16、AA17 などの重み値を持つ入力ファイル (右端の画像を参照) のさまざまな列の Holt Winter 予測を計算できるようにします。上記のような同じ出力を、各列の値に対応する個別の Excel シート (AA15、AA16 など) に生成します。これは、作業ディレクトリに作成された同じ Excel ワークブックで、ggplotFORECASTAA15、ggplotFORECASTAA16、ggplotFORECASTAA17 などになります。
また、Holt Winter Forecast の出力 Excel シートでは、次の 24 か月の予測値を印刷できましたが、日付を印刷できませんでした (2015 年 1 月 - 2016 年 12 月)。日付を取得する方法を教えてください。出力ファイルで。
R でループを作成する際の助けをいただければ幸いです。ありがとうございました。