首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >函数返回空对象。

函数返回空对象。
EN

Stack Overflow用户
提问于 2018-05-07 14:20:14
回答 2查看 385关注 0票数 0

我正在尝试区分一个时间意甲,看起来是这样的:不同的时间意境。但遗憾的是,diff(spread)还给了我。我还试过diff(spread,1))。我几乎复制了一个工作示例的一些代码,并且没有发现任何明显的错误。我两个小时前安装了这些模块,所以我已经得到了所有使用的软件包的最后一个版本。

代码语言:javascript
复制
# 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))

下面是我遇到的错误:

代码语言:javascript
复制
> 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))的输出

代码语言:javascript
复制
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")
EN

回答 2

Stack Overflow用户

发布于 2018-05-07 14:55:05

问题似乎是日期被编码为因素。如果我们手动构造spread,请注意其中的区别:

代码语言:javascript
复制
> 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中。

代码语言:javascript
复制
data <- read.csv("base_form.csv", stringsAsFactors = FALSE)

(请注意,sep = ","read.csv的缺省值,因此您实际上不需要指定它。)

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

票数 1
EN

Stack Overflow用户

发布于 2018-05-07 16:07:46

我发帖是为了纠正我认为在r2evans问题分析中的一些不准确之处。的确,问题的根源在于使用一个因素作为一个指标。R中的因子类不支持订购操作,“动物园”名称中至少有一个“o”表示"ordered“。它可以通过以下方法快速解决:

代码语言:javascript
复制
index(spread) <- as.character(index(spread))

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

代码语言:javascript
复制
> 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

字符向量是动物园完全可以接受的索引类。它们将被排序为词法值。对两个字符值进行"<“或">”操作是完全可以接受的,因此在这种情况下没有歧义。这个动物园包还有一个yearmon类,如果需要的话,这个索引可以变成这个类。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50216473

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档