脚本正在改变价值观?

时间:2013-09-11 07:46:59

标签: r

我的R脚本有问题,因为它在我尝试处理数据时会更改输入值。

items <- dataset 

Date    A    B    C    D 
1-2010  0    0    0    1 
2-2010  0    0    1    2 
3-2010  1    1    0    0  

data_test <- NULL
for(i in 1:ncol(items)) {
  col <- ts(items[,i])
  fit1 <- auto.arima(col, trace=TRUE)
  fcast <- forecast(fit1,h=12, level=c(90,95))
data_test <- cbind(data_test, fcast$lower)
}

但如果我写col我得到:

Date    A    B    C    D  
1-2010  3    4    3    3  
2-2010  3    3    2    3 
3-2010  4    3    3    3 

我认为这是因为ts功能(它需要至少一次观察),但我该如何避免/绕过它呢?

1 个答案:

答案 0 :(得分:1)

很抱歉,但我无法重现您的结果:

输入数据看起来没问题:

> print(items)
Date A B C D
1 1-2010 0 0 0 1
2 2-2010 0 0 1 2
3 3-2010 1 1 0 0

这里没什么意外的:

> data_test <- NULL
> print(data_test)
NULL

> for(i in 1:ncol(items)) {
+   col <- ts(items[,i])
+   fit1 <- auto.arima(col, trace=TRUE)
+   fcast <- forecast(fit1,h=12, level=c(90,95))
+ data_test <- cbind(data_test, fcast$lower)
+ }
正如预期的那样,

col包含ts的最后一次调用的结果:

> print(col)
Time Series:
Start = 1 
End = 3 
Frequency = 1 
[1] 1 2 0

输入数据看起来不变:

> print(items)
    Date A B C D
1 1-2010 0 0 0 1
2 2-2010 0 0 1 2
3 3-2010 1 1 0 0

输出:

> print(data_test)
            90%       95%        90%        95%        90%        95%
 [1,] 0.6569826 0.3996961 -0.7020735 -0.8420231 -0.7020735 -0.8420231
 [2,] 0.6569826 0.3996961 -0.3839978 -0.5337011 -0.3839978 -0.5337011
 [3,] 0.6569826 0.3996961 -0.5312107 -0.6822689 -0.5312107 -0.6822689
 [4,] 0.6569826 0.3996961 -0.4790011 -0.6302537 -0.4790011 -0.6302537
 [5,] 0.6569826 0.3996961 -0.4993618 -0.6506425 -0.4993618 -0.6506425
 [6,] 0.6569826 0.3996961 -0.4917056 -0.6429903 -0.4917056 -0.6429903
 [7,] 0.6569826 0.3996961 -0.4946245 -0.6459098 -0.4946245 -0.6459098
 [8,] 0.6569826 0.3996961 -0.4935175 -0.6448029 -0.4935175 -0.6448029
 [9,] 0.6569826 0.3996961 -0.4939381 -0.6452235 -0.4939381 -0.6452235
[10,] 0.6569826 0.3996961 -0.4937784 -0.6450638 -0.4937784 -0.6450638
[11,] 0.6569826 0.3996961 -0.4938391 -0.6451245 -0.4938391 -0.6451245
[12,] 0.6569826 0.3996961 -0.4938160 -0.6451014 -0.4938160 -0.6451014
             90%       95%         90%         95%
 [1,] -0.9496567 -1.131586  1.42604280  1.27350002
 [2,] -0.9496567 -1.131586 -0.64967538 -0.84765489
 [3,] -0.9496567 -1.131586  0.73647195  0.51264884
 [4,] -0.9496567 -1.131586 -0.60589544 -0.84580765
 [5,] -0.9496567 -1.131586  0.38080673  0.13047778
 [6,] -0.9496567 -1.131586 -0.51643718 -0.77365275
 [7,] -0.9496567 -1.131586  0.17206486 -0.08975985
 [8,] -0.9496567 -1.131586 -0.43367240 -0.69860560
 [9,] -0.9496567 -1.131586  0.04304077 -0.22399916
[10,] -0.9496567 -1.131586 -0.36792524 -0.63639759
[11,] -0.9496567 -1.131586 -0.03920847 -0.30865684
[12,] -0.9496567 -1.131586 -0.31885153 -0.58896591

唯一的问题是你应该省略第一列(因为(i​​ in 2:ncol(items)))因为拟合日期标签有点无意义,不是吗?