0

我有这个 excel 文件(参考最左边的图像),它有两列——A 列的期间值从 2005 年 1 月到 2014 年 12 月,B 列包含 AA15 的权重值。我想为 AA15 列做 Holt Winters 预测(未来 24 个月),并创建一个 Excel 文件作为输出(参考中间图像),该文件将在 Excel 表中包含预测值和绘图。

R 代码允许用户选择 .csv excel 文件,该文件具有执行 Holt Winters 预测的输入数据。包含输入 excel 表、输出 excel 表和最终 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()

现在我想应用相同的代码并循环调用所有函数,这样我就可以计算输入文件(参考最右边的图像)不同列的 Holt Winter 预测,其中包含 AA15、AA16、AA17 等的权重值。并在与每个列值对应的单独的 Excel 表中生成与上述相同的输出 - AA15、AA16 等,这将是在工作目录中创建的同一个 Excel 工作簿中的 ggplotFORECASTAA15、ggplotFORECASTAA16、ggplotFORECASTAA17 等。

同样在 Holt Winter Forecast 的输出 excel 表中,我能够打印未来 24 个月的预测值,但无法打印日期(2015 年 1 月至 2016 年 12 月),请告诉我如何获取日期在输出文件中。

在 R 中创建循环的任何帮助将不胜感激。谢谢你。

4

1 回答 1

0

考虑以下内容,它基本上在for loopAA15、AA16、AA17 的列表上运行。所有库在一开始就被调用一次。

相当多的assign(),paste0()get()函数被用于在整个循环中传递列表项。希望我没有错过任何东西。请根据需要调整!

library(tseries)
library(forecast)
library(WriteXLS)
library(ggplot2)
library(reshape2)
library(xlsx)

forecasts <- c("AA15", "AA16", "AA17")

for (AA in forecasts) {

    # Let user select the excel (.csv) file for importing the input data            
    assign(paste0(AA, "file"), read.csv(file.choose(), header = T))

    # convert to time series
    assign(AA, ts(get(paste0(AA, "file"))[,2], start=c(2005,1), end= c(2014,12),frequency = 12))

    get(AA)

    # convert to Holt Winter
    assign(paste0(AA, "HW"), HoltWinters(get(AA), seasonal = "multiplicative",optim.start =    c(alpha=0.3, beta=0.3, gamma =0.3)))

    get(paste0(AA, "HW"))

    get(past0(AA, "HW"))$fitted
    summary(get(paste0(AA, "HW"))

    ###########################################################################

    # Combine Holt Winter Forecast plots

    HWplot<-function(get(AA),  n.ahead=12,  CI=.95,  error.ribbon='green',  line.size=1){

      hw_object<-HoltWinters(get(AA))

      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(get(AA))
   forecast<-forecast.HoltWinters(get(paste0(AA, "HW")))
   forecast$mean

   forecastvalues<-data.frame(forecast$mean)

   forecastvalues


   # Save the plot

   ggsave(filename = paste0("zipggplot", AA ".png"))
   png(filename = paste0("zipggplot", AA ".png"), units = "px", width = 600, height = 600)
   HWplot(get(AA), n.ahead=12, CI=.95, error.ribbon='blue',line.size=1)
   dev.off()


   wb<-createWorkbook(type="xlsx")

   # Create a new sheet to contain the plot
   sheet <-createSheet(wb, sheetName = paste0("ggplotFORECAST", AA))

   # Add the plot created previously
   addPicture(paste0("zipggplot", AA ".png"), sheet, scale = 1, startRow = 4,
           startColumn = 5)

   # Add title
   xlsx.addTitle(sheet, rowIndex=1, title= paste0("ForecastPlotsggplot2", AA),
          titleStyle = TITLE_STYLE)

   # remove the plot from the disk
   res<-file.remove(paste0("zipggplot", AA, ".png"))

   # Save the workbook to a file...    
   saveWorkbook(wb, paste0("ggplotforecastplot", AA ".xlsx"))

   #Add forecast data to excel sheet    
   addDataFrame(forecastvalues, sheet, startRow = 1, startColumn = 1)
   saveWorkbook(wb, paste0("ggplotforecastplot", AA ".xlsx"))

   # The excel file and the sheet will be created in the working directory    
   getwd()

}
于 2015-08-22T00:49:04.560 回答