diff()函数返回一个空对象

时间:2018-05-07 14:20:15

标签: r zoo

我试图区分时间系列,看起来像那样:time serie to differenciate。但遗憾的是,diff(spread)还给了我this。我也试过了diff(spread,1))。我几乎把一些工作示例的代码复制了一遍,而且我没有发现任何明显的错误。我两个小时前安装了这些模块,所以我得到了所有软件包的最后一个版本。

# chemin espace de travail
setwd("C:/Users/Simon/Desktop/Projet serie temp")

#### Q1 ####
require(zoo)
require(tseries)
require(fUnitRoots)
data <- read.csv("base_form.csv",sep=",") #import .csv
View(data) #visualisation
indice = data$Index
dates = data$Dates
spread <- zoo(indice, order.by=dates)
View(spread)
plot.window(ylim = c(-20,20))

plot(spread) #représentation graphique
dspread <- diff(spread) #différence première
plot(cbind(spread,dspread))

这是我得到的错误:

> plot(dspread)
Error in plot.window(xlim = xlim, ylim = ylim, log = log, yaxs = pars$yaxs) : 
  valeurs finies requises pour 'ylim'
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
3: In min(x) : no non-missing arguments to min; returning Inf
4: In max(x) : no non-missing arguments to max; returning -Inf

以下是dput(head(spread))

的输出
structure(c(83.87, 86.15, 94.07, 90.02, 92.22, 93.18), index = structure(1:6, .Label = c("1990-01", 
"1990-02", "1990-03", "1990-04", "1990-05", "1990-06", "1990-07", 
"1990-08", "1990-09", "1990-10", "1990-11", "1990-12", "1991-01", 
"1991-02", "1991-03", "1991-04", "1991-05", "1991-06", "1991-07", 
"1991-08", "1991-09", "1991-10", "1991-11", "1991-12", "1992-01", 
"1992-02", "1992-03", "1992-04", "1992-05", "1992-06", "1992-07", 
"1992-08", "1992-09", "1992-10", "1992-11", "1992-12", "1993-01", 
"1993-02", "1993-03", "1993-04", "1993-05", "1993-06", "1993-07", 
"1993-08", "1993-09", "1993-10", "1993-11", "1993-12", "1994-01", 
"1994-02", "1994-03", "1994-04", "1994-05", "1994-06", "1994-07", 
"1994-08", "1994-09", "1994-10", "1994-11", "1994-12", "1995-01", 
"1995-02", "1995-03", "1995-04", "1995-05", "1995-06", "1995-07", 
"1995-08", "1995-09", "1995-10", "1995-11", "1995-12", "1996-01", 
"1996-02", "1996-03", "1996-04", "1996-05", "1996-06", "1996-07", 
"1996-08", "1996-09", "1996-10", "1996-11", "1996-12", "1997-01", 
"1997-02", "1997-03", "1997-04", "1997-05", "1997-06", "1997-07", 
"1997-08", "1997-09", "1997-10", "1997-11", "1997-12", "1998-01", 
"1998-02", "1998-03", "1998-04", "1998-05", "1998-06", "1998-07", 
"1998-08", "1998-09", "1998-10", "1998-11", "1998-12", "1999-01", 
"1999-02", "1999-03", "1999-04", "1999-05", "1999-06", "1999-07", 
"1999-08", "1999-09", "1999-10", "1999-11", "1999-12", "2000-01", 
"2000-02", "2000-03", "2000-04", "2000-05", "2000-06", "2000-07", 
"2000-08", "2000-09", "2000-10", "2000-11", "2000-12", "2001-01", 
"2001-02", "2001-03", "2001-04", "2001-05", "2001-06", "2001-07", 
"2001-08", "2001-09", "2001-10", "2001-11", "2001-12", "2002-01", 
"2002-02", "2002-03", "2002-04", "2002-05", "2002-06", "2002-07", 
"2002-08", "2002-09", "2002-10", "2002-11", "2002-12", "2003-01", 
"2003-02", "2003-03", "2003-04", "2003-05", "2003-06", "2003-07", 
"2003-08", "2003-09", "2003-10", "2003-11", "2003-12", "2004-01", 
"2004-02", "2004-03", "2004-04", "2004-05", "2004-06", "2004-07", 
"2004-08", "2004-09", "2004-10", "2004-11", "2004-12", "2005-01", 
"2005-02", "2005-03", "2005-04", "2005-05", "2005-06", "2005-07", 
"2005-08", "2005-09", "2005-10", "2005-11", "2005-12", "2006-01", 
"2006-02", "2006-03", "2006-04", "2006-05", "2006-06", "2006-07", 
"2006-08", "2006-09", "2006-10", "2006-11", "2006-12", "2007-01", 
"2007-02", "2007-03", "2007-04", "2007-05", "2007-06", "2007-07", 
"2007-08", "2007-09", "2007-10", "2007-11", "2007-12", "2008-01", 
"2008-02", "2008-03", "2008-04", "2008-05", "2008-06", "2008-07", 
"2008-08", "2008-09", "2008-10", "2008-11", "2008-12", "2009-01", 
"2009-02", "2009-03", "2009-04", "2009-05", "2009-06", "2009-07", 
"2009-08", "2009-09", "2009-10", "2009-11", "2009-12", "2010-01", 
"2010-02", "2010-03", "2010-04", "2010-05", "2010-06", "2010-07", 
"2010-08", "2010-09", "2010-10", "2010-11", "2010-12", "2011-01", 
"2011-02", "2011-03", "2011-04", "2011-05", "2011-06", "2011-07", 
"2011-08", "2011-09", "2011-10", "2011-11", "2011-12", "2012-01", 
"2012-02", "2012-03", "2012-04", "2012-05", "2012-06", "2012-07", 
"2012-08", "2012-09", "2012-10", "2012-11", "2012-12", "2013-01", 
"2013-02", "2013-03", "2013-04", "2013-05", "2013-06", "2013-07", 
"2013-08", "2013-09", "2013-10", "2013-11", "2013-12", "2014-01", 
"2014-02", "2014-03", "2014-04", "2014-05", "2014-06", "2014-07", 
"2014-08", "2014-09", "2014-10", "2014-11", "2014-12", "2015-01", 
"2015-02", "2015-03", "2015-04", "2015-05", "2015-06", "2015-07", 
"2015-08", "2015-09", "2015-10", "2015-11", "2015-12", "2016-01", 
"2016-02", "2016-03", "2016-04", "2016-05", "2016-06", "2016-07", 
"2016-08", "2016-09", "2016-10", "2016-11", "2016-12", "2017-01", 
"2017-02", "2017-03", "2017-04", "2017-05", "2017-06", "2017-07", 
"2017-08", "2017-09", "2017-10", "2017-11", "2017-12", "2018-01", 
"2018-02"), class = "factor"), class = "zoo")

3 个答案:

答案 0 :(得分:2)

我无法完美地重现这个问题,但我有一些想法。

TL; DR: 修改:请勿使用factor,使用characterDate个对象在zoo之前 - 等待事情。

我通过查看zoo:::diff.zoo的来源来追捕这一点。也就是说,它失败了

x - lag(x, k=-1)
# Data:
# numeric(0)
# Index:
# factor(0)
# 338 Levels: 1990-01 1990-02 1990-03 1990-04 1990-05 1990-06 1990-07 1990-08 1990-09 1990-10 1990-11 1990-12 1991-01 1991-02 1991-03 1991-04 1991-05 1991-06 1991-07 1991-08 1991-09 1991-10 1991-11 1991-12 1992-01 1992-02 1992-03 1992-04 ... 2018-02

我认为通常会根据某种形式的时间进展对zoo个对象进行索引。这可能是简单的整数,如

str(zoo(2:5))
# 'zoo' series from 1 to 4
#   Data: int [1:4] 2 3 4 5
#   Index:  int [1:4] 1 2 3 4

或更明确/有意的内容,例如DatePOSIXct时间戳。在您的情况下,它是factor。我不知道zoo是否试图将其视为整数(可能不是,否则它应该提出某些东西),或者像某些分类{{1} } ,很可能不是你想要的时间序列。更正:正如42指出的那样,这实际上非常好。)

因此,即使character智能地处理因素,也存在一个问题,即您列出的日期并不是完全明确的(不是基于时间的对象)。例如,zoo"1990-01"是什么意思?虽然做出这个假设看起来似乎很直观和明显,但R通常不会跟随你的那个跳跃。

试试这个:

"1990-01-01"

(周围的括号只是转储后分配输出的快捷方式。可以安全地删除它们以进行生产。)现在允许

(ind <- index(x))
# [1] 1990-01 1990-02 1990-03 1990-04 1990-05 1990-06
# 338 Levels: 1990-01 1990-02 1990-03 1990-04 1990-05 1990-06 1990-07 1990-08 1990-09 1990-10 1990-11 1990-12 ... 2018-02
(ind <- as.Date(paste0(ind, "-01"), format="%Y-%m-%d"))
# [1] "1990-01-01" "1990-02-01" "1990-03-01" "1990-04-01" "1990-05-01" "1990-06-01"
index(x) <- ind

这意味着您的x - lag(x, k=-1) # 1990-01-01 1990-02-01 1990-03-01 1990-04-01 1990-05-01 1990-06-01 # NA 2.28 7.92 -4.05 2.20 0.96 现在可能正在运作:

spread

我的猜测意味着您的数据导入应该是:

diff(x)
# 1990-02-01 1990-03-01 1990-04-01 1990-05-01 1990-06-01 
#       2.28       7.92      -4.05       2.20       0.96 

或更简单

data <- read.csv("base_form.csv",sep=",") #import .csv
indice = data$Index
dates = as.Date(paste0(data$Dates, "-01"), format="%Y-%m-%d")
spread <- zoo(indice, order.by=dates)

甚至更简单

data <- read.csv("base_form.csv",sep=",")
dates = as.character(data$Dates)

答案 1 :(得分:1)

问题似乎是将日期编码为因子。如果我们手动构建spread,请注意区别:

> indice <- c(83.87, 86.15, 94.07, 90.02, 92.22, 93.18)
> dates <- as.factor(c("1990-01", "1990-02", "1990-03", "1990-04", "1990-05", "1990-06"))
> spread <- zoo(indice, order.by = dates)
> diff(spread)
Data:
  numeric(0)

Index:
  factor(0)
Levels: 1990-01 1990-02 1990-03 1990-04 1990-05 1990-06


> dates <- c("1990-01", "1990-02", "1990-03", "1990-04", "1990-05", "1990-06")
> spread <- zoo(indice, order.by = dates)
> diff(spread)
1990-02 1990-03 1990-04 1990-05 1990-06 
2.28    7.92   -4.05    2.20    0.96 

要解决此问题,您可以尝试将stringsAsFactors = FALSE添加到read.csv

data <- read.csv("base_form.csv", stringsAsFactors = FALSE)

(请注意,sep = ","read.csv的默认设置,因此您无需真正指定它。)

编辑:我应该添加更多zoo - 正确阅读日期的方式,请参阅https://cran.r-project.org/web/packages/zoo/vignettes/zoo-read.pdf

答案 2 :(得分:1)

我发帖是为了纠正我认为r2evans问题分析中的一些不准确之处。确实,问题源于使用因子作为指数。 R中的因子类不支持排序操作,并且名称“zoo”中的至少一个“o”代表“有序”。它可以通过以下方式快速解决:

index(spread) <- as.character(index(spread))

然后diff - 操作会成功,cbind操作也会成功,因为有一个cbind.zoo函数可以识别行数的差异并自动填充较短的行在开头有NA的列。

> cbind(  diff(spread), spread )
        diff(spread) spread
1990-01           NA  83.87
1990-02         2.28  86.15
1990-03         7.92  94.07
1990-04        -4.05  90.02
1990-05         2.20  92.22
1990-06         0.96  93.18
> cbind(  diff(diff(spread)), spread )
        diff(diff(spread)) spread
1990-01                 NA  83.87
1990-02                 NA  86.15
1990-03               5.64  94.07
1990-04             -11.97  90.02
1990-05               6.25  92.22
1990-06              -1.24  93.18

字符向量是动物园完全可以接受的索引类。它们将作为词汇值排序。制作“&lt;”完全可以接受或“&gt;”对两个字符值进行操作,因此在这种情况下没有歧义。 zoo-package还有一个yearmon类,如果需要,该索引可以成为该类。