【发布时间】:2014-11-12 07:27:05
【问题描述】:
如何通过添加时间属性将简单的时间序列对象转换为维度 2 以便可以使用列名?
编辑:
在question 中,引用时间序列的方法是使用 colnames,建议的方法是将其转换为 zoo 对象。在单个时间序列的情况下,如果我使用名称(tseries),它会给出 NULL。因此,要分配名称,请尝试使用只能在维度 2 的对象中使用并通过添加日期索引来构造的 colnames。在这里,我尝试使用列表对象中已经存在的时间属性,这样我就不必从动物园对象来回转换。
如果有其他选择,我会很高兴知道这一点。目的是能够在情节标题等中使用时间序列的名称。
编辑 2:
换个说法,如何给时间序列命名?使用 apply 剥离时间序列属性,如这里的许多问题中所问的那样。然后以允许使用 colnames 的格式取回时间属性和名称,方法是什么?我从系列和允许命名系列的日期列开始,但是以从 lapply(tslist, function) 返回的矢量格式,我需要重新分配名称。如果有更简单的方法可以做到这一点,请告诉我,因为这是主要问题。
一个有点相关的问题是link@Henrik 的回答建议添加一个虚拟列以避免将动物园对象强制转换为向量以保持维数为 2。
在我们有一个包含所需时间信息的列表对象的特定情况下,我遵循了这个方法。
使用HP过滤器查找循环时,我执行以下操作
x <-ts(rnorm(100), start = c(1990, 1), frequency = 12)
library(mFilter)
hp <- hpfilter(x, 1600)
c1 <- hp$cycle
str(c1)
#Time-Series [1:100] from 1990 to 1998: 1.852 -0.368 -0.942 -0.756 1.006 ...
str(hp) # shows that it has the attributes of time series and tsp etc.
子集所在的主对象的str
List of 10
$ cycle : Time-Series [1:100] from 1990 to 1998: 1.852 -0.368 -0.942 -0.756 1.006 ...
$ trend : ts [1:100, 1] -0.191 -0.193 -0.193 -0.191 -0.187 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : NULL
.. ..$ : chr "Series 1"
..- attr(*, "tsp")= num [1:3] 1990 1998 12
$ fmatrix: num [1:100, 1:100] 0.799 -0.178 -0.156 -0.135 -0.116 ...
$ title : chr "Hodrick-Prescott Filter"
$ xname : chr "x"
$ call : language hpfilter(x = x, freq = 1600)
$ type : chr "lambda"
$ lambda : num 1600
$ method : chr "hpfilter"
$ x : ts [1:100, 1] 1.661 -0.561 -1.135 -0.947 0.819 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : NULL
.. ..$ : chr "Series 1"
..- attr(*, "tsp")= num [1:3] 1990 1998 12
- attr(*, "class")= chr "mFilter"
c1 是维度 1 的对象,因为当我尝试为其命名时,
colnames(c1) <- "x"
给出这个错误
Error in `colnames<-`(`*tmp*`, value = "iip") :
attempt to set 'colnames' on an object with less than two dimensions
为了帮助使用时间序列的名称,我们可以通过添加索引将 c1 转换为 zoo 对象,或者将其与日期列 cbind。
由于名称和时间属性信息存在于我们从中提取子集的起始对象中,有没有办法使用它来提取具有时间属性的组件?
编辑:
提取的系列必须具有类似于列表对象中输入系列的结构
ts [1:100, 1] 1.661 -0.561 -1.135 -0.947 0.819 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : NULL
.. ..$ : chr "Series 1"
..- attr(*, "tsp")= num [1:3] 1990 1998 12
我认为可以访问为
attributes(c1) <- attributes(hp$x)
colnames(c1) <- "X1" # is now possible.
如何返回 hp$cycle 及其属性?
在分配 colnames 时如何访问 chr "Series 1"?
colnames(c1)
# Series 1
编辑:
## Return hp$cycle along with the time attributes so that str(h1) is as follows
h1 <- hp$cycle
str(h1)
# this gives
Time-Series [1:100] from 1990 to 1998: 1.852 -0.368 -0.942 -0.756 1.006 ..
#I want it to be of this form
ts [1:100, 1] 1.661 -0.561 -1.135 -0.947 0.819 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : NULL
.. ..$ : chr "Series 1"
..- attr(*, "tsp")= num [1:3] 1990 1998 12
非常感谢。
编辑:
这些是我用来从一个系列中提取时间属性并添加到另一个系列的替代方法。
-
使用 tsp : 这导致错误或尺寸不匹配
tsp(h1)
-
在tsp中使用开始、结束、频率信息 由于每月数据的 tsp 是分数,所以不知道如何获取起始月份信息 即将 1970.250 转换为 1970,4 说,(不知道哪个是开始月份) 然后我可以做类似的事情
h1.ts 1, n), 频率 = tsp[3])
【问题讨论】:
-
c1不是矩阵,它是ts对象。您不能使用colnames。attr(c1,"dimnames")[[2]]给"Series 1"。 -
对此我还不是很清楚,但您可以通过
attributes(hp$trend)$dimnames[[2]]访问"Series 1"。这有帮助吗? -
您能否建议一个替代方案,通过它我可以为系列分配名称,该系列可用于系列列表或情节中的标题?当我做名称(tseries)时,它给出NULL。
-
@Anusha 我不确定您的预期结果是什么。这有帮助吗?
attributes(c1) <- attributes(hp$trend); colnames(c1)#[1] "Series 1" -
attributes(h1) <- c(attributes(hp[[1]]),attributes(hp[[2]]))这可能足够接近吗?看着str(h1)# ts [1:100, 1] -0.516 1.101 -0.756 0.786 0.926 ... - attr(*, "tsp")= num [1:3] 1990 1998 12 - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr "Series 1"
标签: r