2017-08-10 154 views
0

我使用for循环创建了不同产品组的预测,并且我想将循环值(不同产品组)存储在预测数据中在循环中创建的集合。将循环值添加到在for循环中生成的预测数据R

不同的数据集存储在一个列表中。

# Creating data set for looping 
library(dplyr) 
prdgrp <- as.vector(mth3['MMITCL']) 
prdgrp %>% distinct(MMITCL) 

mth3$TPC_MTD2 <- mth3$TPC_MTD + 0.000001 
tail(mth3) 

# List for storing the data being created in the loop 
additive <- list() 

#START LOOP 
for (i in 1:length(unique(prdgrp$MMITCL))) 
{ 
mth31 <- ts(mth3[mth3$MMITCL == unique(prdgrp$MMITCL)[i], c("TPC_MTD2")], frequency=12, start=c(2012,1), end=c(2017,1)) 

library(forecast) 
abe <- hw(mth31, h= 2, seasonal="additive") 
abe$z <- eval(parse(text=i), abe) 
additive[[i]]<- abe 
} 

我无法获得abe$z <- eval(parse(text=i), abe)工作,任何想法。

添加剂的数据是这样的:

[[24]] 
     Point Forecast Lo 80 Hi 80  Lo 95 Hi 95 
Feb 2017  533.1243 260.2218 806.0268 115.75597 950.4927 
Mar 2017  430.8104 155.7259 705.8950 10.10498 851.5159 

[[25]] 
     Point Forecast Lo 80 Hi 80  Lo 95 Hi 95 
Feb 2017  244.7862 114.0818 375.4906 44.89103 444.6813 
Mar 2017  327.1901 196.2669 458.1133 126.96038 527.4198 

我想获得的ID([24])成列。因此,它应该是这样的:

[[24]] 
     Point Forecast Lo 80 Hi 80  Lo 95 Hi 95 Name 
Feb 2017  533.1243 260.2218 806.0268 115.75597 950.4927 24 
Mar 2017  430.8104 155.7259 705.8950 10.10498 851.5159 24 

[[25]] 
     Point Forecast Lo 80 Hi 80  Lo 95 Hi 95 Name 
Feb 2017  244.7862 114.0818 375.4906 44.89103 444.6813 25 
Mar 2017  327.1901 196.2669 458.1133 126.96038 527.4198 25 

运行class(additive)返回"forecast"

+0

你'i'是类'integer','eval(parse())'需要'character'。 – LAP

+0

您可能必须使用deparse-substitute。试试:'abe $ z < - deparse(substitute(i))'。 –

+0

@ r.user.05apr - 我尝试了你的建议,但没有运气。我在问题文本中添加了更多信息 –

回答

0

我发现基于输入答案来自@ r.user.05apr:

additive[[i]] <- data.frame(
    ProdGrp = eval(parse(text=i)), 
    Value= hw(mth31, h= 2, seasonal="additive")$mean