Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R:index()或index.xts()更改时间序列的日期值,为什么?_R_Date_Indexing_Time Series_Xts - Fatal编程技术网

R:index()或index.xts()更改时间序列的日期值,为什么?

R:index()或index.xts()更改时间序列的日期值,为什么?,r,date,indexing,time-series,xts,R,Date,Indexing,Time Series,Xts,我想从使用getSymbols获得的时间序列中提取日期 但是当我使用index/index.xts函数时,返回的日期似乎早了一天。 我无法理解为什么在下面的代码中会发生这种行为 但是,预期的行为是获取与原始时间序列中的日期对象对应的日期对象列表 代码如下,请注意,时间序列SPY的最后日期是2012年8月24日,但索引(SPY)调用的最后一个值是2012年8月23日: getSymbols("SPY") tail(SPY) SPY.Open SPY.High SPY.Low SPY.C

我想从使用getSymbols获得的时间序列中提取日期 但是当我使用index/index.xts函数时,返回的日期似乎早了一天。 我无法理解为什么在下面的代码中会发生这种行为

但是,预期的行为是获取与原始时间序列中的日期对象对应的日期对象列表

代码如下,请注意,时间序列SPY的最后日期是2012年8月24日,但索引(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" 
还请注意,我已经使用以下命令从Systemic Investor博客Systemic Investor.wordpress.com安装了一些R代码

setInternet2(TRUE)
con = gzcon(url('systematicportfolio.com/sit.gz', 'rb'))
source(con) 
close(con) 
解决方案和附加问题

GSee用户找到了答案(谢谢!),指出在我的会话中我屏蔽了index.xts。因此,一个解决方案是调用xts:::index.xts(SPY)而不仅仅是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”

答案现在提示了另一个问题:下面给出了“屏蔽/覆盖”index.xts函数的代码(该函数返回的错误答案移动了前一天的日期):

为什么这个函数在被称为tail(index.xts(SPY))时返回错误的结果?这个index.xts函数的代码有什么问题

比较两个输出(第一个输出错误,第二个输出提供正确答案):


再次感谢您的时间和关注。

也许值得打开一个干净的R.I get副本(使用R 2.15.0):


这个问题是您运行了一些代码来屏蔽
index.xts
方法和xts包。在OP的评论中,您告诉我们您运行了此代码

setInternet2(TRUE)
con = gzcon(url('http://www.systematicportfolio.com/sit.gz', 'rb'))
source(con)
close(con)
运行该代码后,
index.xts
被重新定义为

如果将其与被屏蔽的
xts:::index.xts
函数进行比较,您会发现它有很大的不同。因此,你不能再期待同样的结果了

如果您从SystemicPortfolio.com/sit.gz下载代码并查看,您将看到以下注释

###############################################################################
# Fast alternative to index(x) for XTS object
# NOTE index.xts is the same name as the index function in the XTS package
###############################################################################
因此,似乎作者有意这样做是为了加快速度。不幸的是,他也降低了它的健壮性

这段代码实际上给了我与
xts:::index.xts
相同的结果,所以我不能100%确定为什么使用它会得到不同的结果。但是,我猜想这与将类从
numeric
更改为
POSIXct
有关,然后再转换回
Date

不管怎样,我可能会跑

rm(index.xts)

在找到代码以删除此“增强功能”之后。

我无法复制。请将
sessionInfo()
的输出添加到您的问题中。另外,显示
getDefaults(getSymbols)
getSymbolLookup(“SPY”)
的输出(如果其中任何一个都不是
NULL
),以及
showSymbols()
的输出感谢GSee回复我的消息,我已经用有关会话数据的附加信息对其进行了更新。看起来您可能屏蔽了
index.xts
。如果您使用
tail(xts:::index.xts(SPY))
,会发生什么?请注意,我已经使用命令setInternet2(TRUE)从Systemic Investor博客安装了一些R代码;con=gzcon(url(“”,'rb'));资料来源(con);关闭(con)GSee,您已发现问题!这是输出>尾部(xts:::index.xts(SPY))[1]“2012-08-17”“2012-08-20”“2012-08-21”“2012-08-22”“2012-08-23”“2012-08-24”你好,亨利,谢谢你尝试我的代码。我同意你的建议,但我想了解我的代码会发生什么,以避免将来再次发生同样的问题。我已经更新了我的帖子,更新了我的会话数据,如果这有助于理解问题的原因的话。+1是为什么不做包的好例子。我曾试图告诉
sit.gz
的作者,但似乎没有太大成功。@DirkEddelbuettel,几个月前,我很好地要求那位作者也制作一个软件包。他说他会…谢谢你的回复Gsee和Dirk,你能指出sit.gz中index.xts代码的错误吗?我已经在主要帖子中添加了代码。我无法理解它有什么问题。简单地说,这不是将
数值
转换为
POSIXct
的正确方法。有一个
as.POSIXct
函数用于此功能。
xts:::index.xts
可以解释时区差异,而“更快”的代码则不能。这就是为什么有些人不能复制这种行为。
setInternet2(TRUE)
con = gzcon(url('http://www.systematicportfolio.com/sit.gz', 'rb'))
source(con)
close(con)
> 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)
}
###############################################################################
# Fast alternative to index(x) for XTS object
# NOTE index.xts is the same name as the index function in the XTS package
###############################################################################
rm(index.xts)