2015-07-11 45 views
2

我想在我的數據集測試多個平穩測試:測試多時間

> library(data.table) 
> library(fpp) 
> library(xts) 
> 
> dput(datSel) 
structure(list(gesamtbaukost = c(59373L, 59209L, 58935L, 58551L, 
58496L, 58458L, 58368L, 58039L, 57582L, 57472L, 57289L, 56742L, 
56156L, 56248L, 56046L, 55919L, 55243L, 55075L, 55045L, 53988L, 
53436L, 53298L, 52930L, 52915L, 52947L, 53834L, 52946L, 51567L, 
50433L, 50449L, 50357L, 49557L, 48932L, 48671L, 47722L, 46772L, 
46213L, 45865L, 45919L, 45826L, 45612L, 45276L, 44994L, 44041L, 
43225L, 42983L, 42715L, 42232L, 41870L, 41843L, 41777L, 41321L, 
41132L, 41240L, 41172L, 40743L, 40587L, 40352L, 40127L, 39814L 
), lohn = c(96819L, 96819L, 96090L, 94632L, 94632L, 94632L, 93727L, 
91917L, 91917L, 91917L, 90779L, 88503L, 88416L, 88416L, 88270L, 
87978L, 87996L, 87996L, 87566L, 86706L, 86706L, 86706L, 85794L, 
83970L, 83970L, 83970L, 83007L, 81081L, 81081L, 81081L, 80423L, 
79107L, 79107L, 79107L, 78321L, 76749L, 76533L, 76533L, 75983L, 
74883L, 74883L, 74883L, 74575L, 73959L, 73959L, 73959L, 73167L, 
71583L, 71583L, 71583L, 70858L, 69408L, 69408L, 69408L, 68594L, 
66966L, 66831L, 66342L, 65853L, 64875L), resProp.Dwell = c(144.5, 
146.5, 147.3, 143.3, 140.1, 142.8, 141.2, 140.2, 137.8, 137.4, 
136.6, 137.6, 125.5, 125.7, 120.5, 124.2, 121.5, 119.8, 121.3, 
122, 114.1, 114.4, 114.7, 116.1, 112.8, 111.8, 110.2, 111.7, 
112.2, 113.7, 112.7, 110.5, 107, 107.5, 108, 107.1, 106.7, 103.3, 
104.2, 104.3, 104.1, 101.3, 100.5, 94.3, 105.6, 101, 102, 103.1, 
101.4, 105.5, 100.5, 102.8, 100.5, 105.1, 98.8, 105.1, 98.2, 
98.2, 100.6, 103), resProp.Dwell.1 = c(132.2, 133.9, 133.5, 126, 
125, 122.6, 122.6, 123.8, 124.5, 120.2, 120.2, 123.5, 105.2, 
116.4, 111.5, 116.4, 116.1, 114.3, 117, 117.9, 107.1, 104.5, 
110.6, 110.5, 104.2, 105.4, 106.2, 110.3, 106.8, 111.4, 111.2, 
108.5, 93.5, 101.5, 101.4, 101.3, 101.7, 96.8, 97.3, 100, 97.5, 
99.4, 94.8, 93.8, 101.9, 97.4, 97.7, 98.4, 100.6, 100.1, 96.3, 
98.1, 93.4, 99.3, 97.3, 99.6, 99.2, 97.8, 100.1, 102.9), resProp.Dwell.2 = c(149.8, 
151.9, 153.2, 150.7, 146.5, 151.5, 149.2, 147.3, 143.6, 144.8, 
143.6, 143.7, 134.1, 129.7, 124.3, 127.5, 123.7, 122.2, 123.1, 
123.8, 117.1, 118.6, 116.4, 118.4, 116.4, 114.6, 111.9, 112.2, 
114.5, 114.6, 113.4, 111.3, 112.8, 110.1, 110.8, 109.5, 108.8, 
106.1, 107.1, 106.1, 107, 102.1, 103, 94.5, 107.2, 102.5, 103.9, 
105.1, 101.7, 107.8, 102.4, 104.8, 103.6, 107.6, 99.5, 107.4, 
97.8, 98.4, 100.8, 103), resProp.Dwell.3 = c(155.2, 157.6, 159, 
156.5, 151.4, 155, 152, 149, 146.4, 147.9, 146.6, 146.3, 137.1, 
131.1, 124.5, 127.5, 123.1, 121.9, 123, 123.5, 116.4, 117.7, 
116.4, 118.1, 116.5, 113.7, 110.2, 111, 113.9, 113.9, 113.6, 
110.9, 113.2, 109.9, 111.7, 109.7, 110.1, 106.3, 107.4, 105.9, 
107.2, 101.6, 103.8, 94.1, 108.4, 102.7, 104.1, 105.1, 101.5, 
108.8, 102.3, 105.4, 103, 107.2, 99.3, 107.6, 97.4, 97.6, 101.2, 
103.9), resProp.Dwell.4 = c(112.6, 112.7, 113.6, 110.7, 113.4, 
127.1, 130.1, 135.7, 123.7, 123.2, 123, 125.5, 113.5, 120.2, 
123.3, 128, 128.2, 124.6, 124, 125.8, 122.2, 124.8, 116.6, 120.4, 
115.9, 120.6, 124, 120.6, 119, 120.1, 111.6, 114, 110.2, 111.6, 
104.5, 107.9, 100.4, 104.7, 105, 106.9, 105.1, 105.8, 97.3, 96.6, 
99.1, 101.1, 102.5, 105.2, 103, 101, 102.7, 100.5, 107.4, 110.1, 
101.3, 105.7, 100.3, 104.1, 98.4, 97.2), Date = structure(c(60L, 
30L, 15L, 45L, 59L, 29L, 14L, 44L, 58L, 28L, 13L, 43L, 57L, 27L, 
12L, 42L, 56L, 26L, 11L, 41L, 55L, 25L, 10L, 40L, 54L, 24L, 9L, 
39L, 53L, 23L, 8L, 38L, 52L, 22L, 7L, 37L, 51L, 21L, 6L, 36L, 
50L, 20L, 5L, 35L, 49L, 19L, 4L, 34L, 48L, 18L, 3L, 33L, 47L, 
17L, 2L, 32L, 46L, 16L, 1L, 31L), .Label = c("30.06.2000", "30.06.2001", 
"30.06.2002", "30.06.2003", "30.06.2004", "30.06.2005", "30.06.2006", 
"30.06.2007", "30.06.2008", "30.06.2009", "30.06.2010", "30.06.2011", 
"30.06.2012", "30.06.2013", "30.06.2014", "30.09.2000", "30.09.2001", 
"30.09.2002", "30.09.2003", "30.09.2004", "30.09.2005", "30.09.2006", 
"30.09.2007", "30.09.2008", "30.09.2009", "30.09.2010", "30.09.2011", 
"30.09.2012", "30.09.2013", "30.09.2014", "31.03.2000", "31.03.2001", 
"31.03.2002", "31.03.2003", "31.03.2004", "31.03.2005", "31.03.2006", 
"31.03.2007", "31.03.2008", "31.03.2009", "31.03.2010", "31.03.2011", 
"31.03.2012", "31.03.2013", "31.03.2014", "31.12.2000", "31.12.2001", 
"31.12.2002", "31.12.2003", "31.12.2004", "31.12.2005", "31.12.2006", 
"31.12.2007", "31.12.2008", "31.12.2009", "31.12.2010", "31.12.2011", 
"31.12.2012", "31.12.2013", "31.12.2014"), class = "factor")), .Names = c("gesamtbaukost", 
"lohn", "resProp.Dwell", "resProp.Dwell.1", "resProp.Dwell.2", 
"resProp.Dwell.3", "resProp.Dwell.4", "Date"), row.names = c(NA, 
-60L), class = "data.frame") 
> DT <- as.data.table(datSel) 
> DT[,Date := as.Date(Date,format='%d.%m.%Y')] 
> DT[,{ x = xts(datSel[1:7],Date) 
+  list(box= Box.test(x)$p.value <0.05 , 
+   adf= adf.test(x)$p.value <0.05 , 
+   kpss= kpss.test(x)$p.value >0.05) 
+ }] 
Error in Box.test(x) : x is not a vector or univariate time series 

正如你可以看到我的問題是,x is not a vector or univariate time series。因此,我不能在我的DT函數中使用多個向量。

任何建議我做錯了什麼?

我很感謝你的回覆!

回答

1

如果我們需要的邏輯值作爲輸出

DT[order(Date), lapply(.SD, function(x) c(box=Box.test(x)$p.value < 0.05, 
             adf=adf.test(x)$p.value < 0.05, 
         kpss=kpss.test(x)$p.value>0.05)), .SDcols=1:7] 

或者,如果我們是基於條件轉換爲xts,需要值

x1 <- xts(datSel[1:7], order.by = as.Date(datSel$Date, format='%d.%m.%Y')) 
apply(x1, 2, function(x) { 
     val <- c(box=Box.test(x)$p.value, 
       adf=adf.test(x)$p.value, 
       kpss=kpss.test(x)$p.value) 
       val[1:2] <- ifelse(val[1:2] >=0.05, NA, val[1:2]) 
       val[3] <- ifelse(val[3] <= 0.05, NA, val[3]) 
       val})