2015-06-18 52 views
4

我想使用lag函數在xts對象內創建滯後矢量。它在使用$表示法(例如x.ts$r1_lag)在xts對象內定義新矢量時起作用,但是它在使用方括號定義新變量時,即xts[,"r1_lag"]。請參見下面的代碼:

library(xts) 
x <- data.frame(date=seq(as.Date('2015-01-01'), by='days', length=100), 
       runif(1e2), runif(1e2), runif(1e2)) 
colnames(x) <- c("date", "r1", "r2", "r3") 

#the following command works 
x.ts <- xts(x, order.by=x$date) 
x.ts$r1_lag <- lag(x.ts$r1) 
# but the following does not (says subscript is out of bounds) 
x.ts <- xts(x, order.by=x$date) 
x.ts[,"r1_lag"] <- lag(x.ts[,"r1"]) 

我需要使用[]符號,而不是$符號,因爲參考矢量如果我想多XTS的列表中運行的向量上的滯後轉型的不止一個XTS對象(矢量

for (i in letters) { 
    for (j in variables) { 
    macro.set.ts$i$paste(j,"_L1",sep="") <- lag(macro.set.ts[[i]][,j]) 
    macro.set.ts$i$paste(j,"_L2",sep="") <- lag(macro.set.ts[[i]][,j], 2) 
    macro.set.ts$i$paste(j,"_L4",sep="") <- lag(macro.set.ts[[i]][,j], 4) 
    } 
} 

感謝:對象),我不能用$符號,即我不能使用符號在下面的程式化循環定義新的向量定義對象中的新的載體!

+1

附註:爲什麼你的xts中有一個日期列?您應該只有數字列並將該日期用作索引。否則,你將不會從使用'xts'獲得任何利益(在你的例子中它只是一種字符矩陣) – agstudy

回答

5

您不需要需要使用[<-.xts。您可以使用merge代替:

for (i in letters) { 
    for (j in variables) { 
    # create all lags 
    mst_ij <- macro.set.ts[[i]][,j] 
    jL <- merge(lag(mst_ij), lag(mst_ij, 2), lag(mst_ij, 4)) 
    colnames(jL) <- paste(j, c("L1","L2","L4"), sep="_") 
    # merge back with original data 
    macro.set.ts[[i]] <- merge(macro.set.ts[[i]], jL) 
    } 
} 
+0

謝謝,merge命令在這裏非常有用。但它似乎是某種錯誤,我的原始方法是簡單地用一個額外的向量重新分配xts對象,而不是像'@< - '操作符那樣使用'[<-'運算符作爲@agstudy提到的。 – lrclark

+0

@lrclark:正如我對agstudy的回答評論的:沒有'$ < - 。xts',所以'$ < - 。zoo'被調度;並且'[< - 。xts'的錯誤與動物園一致(即如果您嘗試使用動物園對象而不是xts),則會出現相同的錯誤。我不確定這是否會被認爲是'[< - 。zoo'中的一個錯誤,因爲這與你使用'matrix'和/或'ts'對象獲得的行爲是一樣的(這是動物園擴展的)。 –

1

我得到了相同的輸出用:

x.ts <- cbind(x.ts,lag(x.ts[,"r1"])) 

而且

x.ts <- transform(x.ts, r1_lag = lag(x.ts[,'r1'])) 

但是,要小心與輸出。它看起來可能相同,但結構有所改變。

1

這應該工作:

x.ts <- merge(x.ts,lag(x.ts[,"r1"])) 

那麼你可能要重命名添加的最後一列:

dimnames(x.ts)[[2]][5] <- "r1_lag" 

這是結果:

> head(x.ts) 
      date   r1   r2   r3    r1_lag  
2015-01-01 "2015-01-01" "0.23171030" "0.44174424" "0.3396816640" NA   
2015-01-02 "2015-01-02" "0.97292220" "0.74909452" "0.2793033421" "0.23171030" 
2015-01-03 "2015-01-03" "0.52320743" "0.49288463" "0.0193637393" "0.97292220" 
2015-01-04 "2015-01-04" "0.36574297" "0.69571803" "0.6411834760" "0.52320743" 
2015-01-05 "2015-01-05" "0.37563137" "0.13841216" "0.3087215754" "0.36574297" 
2015-01-06 "2015-01-06" "0.48089356" "0.32702759" "0.3967609401" "0.37563137" 

> class(x.ts) 
[1] "xts" "zoo" 

希望這幫助。

3

該錯誤與lag函數無關。你得到一個錯誤,因爲你嘗試分配一個xts對象與另一個xts對象。這個例子重現了錯誤:

x.date= seq(as.Date('2015-01-01'), 
         by = 'days' , length = 5) 
x1 <- xts(data.frame(c1=runif(5)), order.by=x.date) 
x2 <- xts(data.frame(c2=runif(5)), order.by=x.date) 
x1[,'r2'] <- x2 

## Error in `[<-.default`(`*tmp*`, , "r2", 
## subscript out of bounds 

我覺得這是xts邏輯內的一致性,因爲xts是索引對象。所以最好在這裏合併對象或加入並保存時間序列的索引本質。

merge(x1,x2) 

這將cbind 2個系列,並修復任何索引問題。其實,cbind只是一個merge

identical(cbind(x1,x2),merge(x1,x2) 

這是說,我認爲這是一種錯誤的,這適用於$<-運營商,而不是與[<-運營商。

+1

請注意,沒有'$ < - 。xts',因此'$ < - 。zoo'被調度。並且'[<。.xts>'的錯誤與動物園一致(即如果您嘗試使用動物園對象而不是xts),則會出現相同的錯誤。 –