循环问题 - 存储不同格式的数据

时间:2015-04-15 06:10:24

标签: r loops data-structures time-series forecasting

运行后存储数据时遇到问题。代码正在正确地选择文件并运行预测模型,但它以某种方式存储最后一个文件的值。所有其他人都丢失了。无论如何,我可以将所有结果存储在不同的数组中。问题是输出的格式是“预测”格式,因此我会陷入困境。我查看了所有网站,但找不到类似的东西。

以下是代码:

library(forecast)
library(quantmod)
library(forecast)

fileList <-as.array(length(50))
Forecast1 <- as.array(length(50))


fileList<-list.files(path ='C:\\Users\\User\\Downloads\\wOOLWORTHS\\',recursive =T, pattern = ".csv")
i<- integer()
j<-integer()
i=1
setwd("C:\\Users\\User\\Downloads\\wOOLWORTHS\\")

while (i<51)
{
  a<-fileList[i]
  print(a)
  a <- read.csv(a)

  fileSales<-a$sales
  fileTransform<-log(fileSales)
  plot.ts(fileTransform)
  result1<-HoltWinters(fileTransform,beta = FALSE,gamma =FALSE,seasonal ="multiplicative",optim.control =TRUE)
  result2<-forecast.HoltWinters(result1,h=1)
  summary(result1)
  accuracy(result2)

  #Forecast1[i] <- result2(forecast)
  #print(Forecast1[i])
  i=i+1
}

1 个答案:

答案 0 :(得分:1)

可能就是您存储结果的方式。请尝试填写空列表(例如Forecast1):

setwd("C:\\Users\\User\\Downloads\\wOOLWORTHS\\")

library(forecast)
library(quantmod)
library(forecast)

fileList <- list.files(path ='C:\\Users\\User\\Downloads\\wOOLWORTHS\\',recursive =T, pattern = ".csv")

Forecast1 <- vector(mode="list", 50)

for(i in seq(length(fileList)){
  a <- fileList[[i]]
  #print(a)
  a <- read.csv(a)

  fileSales<-a$sales
  fileTransform<-log(fileSales)
  plot.ts(fileTransform)
  result1<-HoltWinters(fileTransform,beta = FALSE,gamma =FALSE,seasonal ="multiplicative",optim.control =TRUE)
  result2<-forecast.HoltWinters(result1,h=1)
  #summary(result1)
  #accuracy(result2)

  Forecast1[[i]] <- result2
  #print(Forecast1[i])
  print(paste(i, "of", length(fileList), "completed"))
}