1

%*%演算子を使用して時系列を表すベクトルに重みのベクトルを適用するコードを使用しています。時系列にxtsを使用したいのですが、%*%オペレーターはxtsインデックス値を無視する必要があることを理解していません。

coredata()系列の値をベクトルとして引き出し、値を操作して、それらをマージして戻すことができることはわかっていますが、使用すべき優れたネイティブxts関数があるかどうか疑問に思いました。

編集:私が行動に見ている違いを説明するコードサンプル。

library(xts)
data(sample_matrix)
s<-as.xts(sample_matrix)

o_xts<-s$Open
c_xts<-coredata(s$Open)

len <-length(c_xts)
len2<-len/2
xx<-c_xts[1:len]
outp<-0*0:len2
outp[2] <- xx%*%exp((1:(2*len2))*1.i*pi/len2)
#completes without issue

len <-length(o_xts)
len2<-len/2
yy<-o_xts[1:len]
outp<-0*0:len2
outp[2] <- yy%*%exp((1:(2*len2))*1.i*pi/len2)
Warning message:
In outp[2] <- yy %*% exp((1:(2 * len2)) * (0+1i) * pi/len2) :
  number of items to replace is not a multiple of replacement length
4

2 に答える 2

3

チェックすると、デフォルトで。にhelp("[.xts")なっていることがわかります。通常の行列の場合、デフォルトはです。dropFALSETRUE

str(o_xts[1:len])
# An ‘xts’ object from 2007-01-02 to 2007-06-30 containing:
#   Data: num [1:180, 1] 50 50.2 50.4 50.4 50.2 ...
#  - attr(*, "dimnames")=List of 2
#   ..$ : NULL
#   ..$ : chr "Open"
#   Indexed by objects of class: [POSIXct,POSIXt] TZ: 
#   xts Attributes:  
#  NULL

str(c_xts[1:len])
# num [1:180] 50 50.2 50.4 50.4 50.2 ...

つまり、xxは180要素のベクトルであるのに対し、yyは180x1の行列です。どちらの場合も同じ動作を得るには、を使用できますyy <- o_xts[1:len, drop=TRUE]

于 2011-10-12T10:13:26.823 に答える
1

私は(まだ)質問の前提を裏付ける証拠を見たことがありません.help(xts)の最初の例で独自の簡単なテストを行うと、反対の証拠が出てきます。

> data(sample_matrix)
> sample.xts <- as.xts(sample_matrix, descr='my new xts object')
> str(coredata(sample.xts))
 num [1:180, 1:4] 50 50.2 50.4 50.4 50.2 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:4] "Open" "High" "Low" "Close"
> str(coredata(sample.xts) %*% c(3, 3,3,3) )
 num [1:180, 1] 601 604 604 604 602 ...
> str(sample.xts %*% c(3, 3,3,3) )
 num [1:180, 1] 601 604 604 604 602 ...
于 2011-09-24T00:23:43.967 に答える