getSymbolsを使用して取得した時系列から日付を抽出したいのですが、index / index.xts関数を使用すると、返される日付が1日前に表示されます。次のコードでこの動作が発生する理由がわかりません。
ただし、意図された動作は、元の時系列のオブジェクトに対応するDateオブジェクトのリストを取得することです。
コードは次のとおりです。時系列SPYの最終日は2012年8月24日ですが、index(SPY)呼び出しの最後の値は2012年8月23日です。
getSymbols("SPY")
tail(SPY)
SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted
2012-08-17 142.23 142.30 141.86 142.18 90813700 142.18
2012-08-20 141.98 142.22 141.59 142.19 78255700 142.19
2012-08-21 142.54 143.09 141.45 141.76 105581100 141.76
2012-08-22 141.40 142.05 141.07 141.82 132999200 141.82
2012-08-23 141.47 141.48 140.44 140.66 111406800 140.66
2012-08-24 140.31 141.83 140.22 141.51 99431500 141.51
tail(index(SPY))
[1] "2012-08-16" "2012-08-19" "2012-08-20" "2012-08-21" "2012-08-22" "2012-08-23"
tail(index.xts(SPY))
[1] "2012-08-16" "2012-08-19" "2012-08-20" "2012-08-21" "2012-08-22" "2012-08-23"
私の投稿に返信してくださった皆さん、ありがとうございました。
セッションに関する追加情報
>sessionInfo()
R version 2.15.1 (2012-06-22)
Platform: i386-pc-mingw32/i386 (32-bit)
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] rbenchmark_0.3.1 fGarch_2110.80.1
[3] fBasics_2160.81 MASS_7.3-20
[5] timeSeries_2160.95 timeDate_2160.95
[7] tseries_0.10-29 quadprog_1.5-4
[9] PerformanceAnalytics_1.0.4.4 quantstrat_0.6.8
[11] blotter_0.8.10 FinancialInstrument_0.15.2
[13] quantmod_0.3-17 TTR_0.21-1
[15] Defaults_1.1-1 xts_0.8-6
[17] zoo_1.7-7 lubridate_1.1.0
[19] stringr_0.6.1 plyr_1.7.1
[21] XML_3.9-4.1
loaded via a namespace (and not attached):
[1] colorspace_1.1-1 dichromat_1.2-4 digest_0.5.2 ggplot2_0.9.1
[5] grid_2.15.1 labeling_0.2 lattice_0.20-6 memoise_0.1
[9] munsell_0.3 proto_0.3-9.2 RColorBrewer_1.0-5 reshape2_1.2.1
[13] scales_0.2.1 stabledist_0.6-4 tools_2.15.1
> getDefaults(getSymbols)
NULL
> getSymbolLookup("SPY")
NULL
> showSymbols()
SPY GSPC IBM XLF XLP XLE XLY XLV XLI
"yahoo" "yahoo" "yahoo" "yahoo" "yahoo" "yahoo" "yahoo" "yahoo" "yahoo"
XLB XLK XLU IEF AAPL DIA MSFT IWM EEM
"yahoo" "yahoo" "yahoo" "yahoo" "yahoo" "yahoo" "yahoo" "yahoo" "yahoo"
EFA GLD AGG HYG FXE FXY VXX VXZ HIG
"yahoo" "yahoo" "yahoo" "yahoo" "yahoo" "yahoo" "yahoo" "yahoo" "yahoo"
VTI VEU VNQ DBC XAU gold Gold STOXX50E GOLD
"yahoo" "yahoo" "yahoo" "yahoo" "oanda" "oanda" "oanda" "yahoo" "yahoo"
VIX DEXUSEU EURUSD DEXKOUS EUR=X INR=X
"yahoo" "FRED" "oanda" "FRED" "yahoo" "yahoo"
また、コマンドを使用して、SystematicInvestorブログsystematicinvestor.wordpress.comからいくつかのRコードをインストールしたことにも注意してください。
setInternet2(TRUE)
con = gzcon(url('systematicportfolio.com/sit.gz', 'rb'))
source(con)
close(con)
ソリューションと追加の質問
GSeeユーザーは、私のセッションでindex.xtsをマスクしたことを指摘する答えを見つけました(ありがとう!)。したがって、解決策は、index.xts(SPY)だけでなくxts ::: index.xts(SPY)を呼び出して、マスキングをオーバーライドすることです。実際、コマンド
> tail(xts:::index.xts(SPY))
正しい答えを返します
[1] "2012-08-17" "2012-08-20" "2012-08-21" "2012-08-22" "2012-08-23" "2012-08-24" – </ p>
答えは今、別の質問を促しました:「マスキング/オーバーライド」index.xts関数のコードの下に与えられています(1日前に日付を移動して間違った答えを返します):
> index.xts
function (
x # XTS object
)
{
temp = attr(x, 'index')
class(temp)='POSIXct'
if( attr(x, '.indexCLASS')[1] == 'Date')
temp = as.Date(temp)
return(temp)
}
tail(index.xts(SPY))として呼び出されたときに、この関数が間違った結果を返すのはなぜですか?このindex.xts関数のコードの何が問題になっていますか?
2つの出力を比較します(最初の出力は間違っていますが、2番目の出力は正しい答えを提供します)。
tail(index.xts(SPY))
[1] "2012-08-16" "2012-08-19" "2012-08-20" "2012-08-21" "2012-08-22" "2012-08-23"
tail(xts:::index.xts(SPY))
[1] "2012-08-17" "2012-08-20" "2012-08-21" "2012-08-22" "2012-08-23" "2012-08-24"
お手数をおかけしますが、よろしくお願いいたします。