【问题标题】:How to extract time attributes for a univariate time series?如何提取单变量时间序列的时间属性?
【发布时间】: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

非常感谢。

编辑:

这些是我用来从一个系列中提取时间属性并添加到另一个系列的替代方法。

  1. 使用 tsp : 这导致错误或尺寸不匹配

    tsp(h1)

  2. 在tsp中使用开始、结束、频率信息 由于每月数据的 tsp 是分数,所以不知道如何获取起始月份信息 即将 1970.250 转换为 1970,4 说,(不知道哪个是开始月份) 然后我可以做类似的事情

    h1.ts 1, n), 频率 = tsp[3])

【问题讨论】:

  • c1 不是矩阵,它是ts 对象。您不能使用colnamesattr(c1,"dimnames")[[2]]"Series 1"
  • 对此我还不是很清楚,但您可以通过attributes(hp$trend)$dimnames[[2]] 访问"Series 1"。这有帮助吗?
  • 您能否建议一个替代方案,通过它我可以为系列分配名称,该系列可用于系列列表或情节中的标题?当我做名称(tseries)时,它给出NULL。
  • @Anusha 我不确定您的预期结果是什么。这有帮助吗? attributes(c1) &lt;- attributes(hp$trend); colnames(c1)#[1] "Series 1"
  • attributes(h1) &lt;- 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


【解决方案1】:

您可以使用startendtime

  start(c1)
 [1] 1990    1

  end(c1)
 [1] 1998    4

  time(c1)
          Jan      Feb      Mar      Apr      May      Jun      Jul      Aug
1990 1990.000 1990.083 1990.167 1990.250 1990.333 1990.417 1990.500 1990.583
1991 1991.000 1991.083 1991.167 1991.250 1991.333 1991.417 1991.500 1991.583
1992 1992.000 1992.083 1992.167 1992.250 1992.333 1992.417 1992.500 1992.583
1993 1993.000 1993.083 1993.167 1993.250 1993.333 1993.417 1993.500 1993.583
1994 1994.000 1994.083 1994.167 1994.250 1994.333 1994.417 1994.500 1994.583
1995 1995.000 1995.083 1995.167 1995.250 1995.333 1995.417 1995.500 1995.583
1996 1996.000 1996.083 1996.167 1996.250 1996.333 1996.417 1996.500 1996.583
1997 1997.000 1997.083 1997.167 1997.250 1997.333 1997.417 1997.500 1997.583
1998 1998.000 1998.083 1998.167 1998.250                                    
          Sep      Oct      Nov      Dec
1990 1990.667 1990.750 1990.833 1990.917
1991 1991.667 1991.750 1991.833 1991.917
1992 1992.667 1992.750 1992.833 1992.917
1993 1993.667 1993.750 1993.833 1993.917
1994 1994.667 1994.750 1994.833 1994.917
1995 1995.667 1995.750 1995.833 1995.917
1996 1996.667 1996.750 1996.833 1996.917
1997 1997.667 1997.750 1997.833 1997.917
1998                                    

【讨论】:

  • 感谢 Pascal,但这无助于分配 colnames,因为维度仍然是 1。
  • 不是矩阵,所以不能指定列名。
  • @Anusha - 没有可分配名称的列。您是否尝试将其转换为矩阵然后分配列名?您可能想要剥离属性并构建矩阵
  • 我想使用列表对象中的属性信息,并将其转换为维度为 2 的 ts 对象,以便可以从列表对象内部或外部分配列名。
  • 我想只要colnames(hp$x)dimnames(hp$x)[[2]]attr(hp$x,"dimnames")[[2]] 如果我得到你想要的(但可能没有)。
【解决方案2】:

我不确定想要的输出是什么。希望这可以帮助。请显示所需的输出。

newdf<-as.data.frame(hp$x)
colnames(newdf)
[1] "Series 1"

【讨论】:

  • 从所有 cmets 的混乱情况来看,我猜这不是想要的输出。
【解决方案3】:

根据提供的信息,我想你可以试试:

 h1 <- hp$cycle
 str(h1)
 #Time-Series [1:100] from 1990 to 1998: -0.516 1.101 -0.756 0.786 0.926 ...
 attributes(h1)
 #$tsp
 #[1] 1990.00 1998.25   12.00

 #$class
 #[1] "ts"

如果您查看str(hp),它是一个包含 10 个元素的列表。要访问不同元素的属性,可以使用attributesattr。例如:

 attr(hp$cycle, "tsp")
 #[1] 1990.00 1998.25   12.00

或者

  attributes(hp$cycle)[["tsp"]] #
 #[1] 1990.00 1998.25   12.00

获取列表元素的整个attributes

 attributes(hp$cycle)
 #$tsp
 #[1] 1990.00 1998.25   12.00

 #$class
 #[1] "ts"

使用c 连接来自不同列表元素的属性并保持列表结构

 attributes(h1) <- c(attributes(hp$cycle), attributes(hp$trend))
 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"

 tsp(h1)
 #[1] 1990.00 1998.25   12.00
 tsp(h1) <- tsp(x) #no errors here

【讨论】:

    【解决方案4】:

    如果问题是如何创建一个 n x 1 月度 ts 系列并带有名称,那么:

    xx <- ts(cbind(A = 1:5), start = c(2000, 1), freq = 12)
    

    我们现在可以通过名称来引用它:

    xx[, "A"]
    

    我们可以像这样提取各种组件:

    xx[2] # second point
    xx[2, 1] # second point with name
    xx[2, "A"] # same
    
    start(xx) # start time
    time(xx)[1]
    
    end(xx) # end time
    time(xx)[NROW(xx)]
    
    time(xx) # times
    c(time(xx))
    
    frequency(xx)
    
    cycle(xx) # months as a ts series
    c(cycle(xx)) # plain
    
    floor(time(xx)) # years as a ts series
    c(floor(time(xx)))
    

    我们可以将下面的yy 转换为具有名称的 n x 1 系列:

    yy <- ts(1:5, start = c(2000, 1), frequency = 12)
    
    dim(yy) <- c(length(yy), 1)
    colnames(yy) <- "A"
    
    yy[2, 1] # get second point with name
    

    添加如果我们有:

    xx <- ts(cbind(A = 1:5), start = c(2000, 1), freq = 12)
    

    属性被剥离:

    xx2 <- c(xx)
    

    那么我们可以这样做:

    xx[] <- xx2
    

    【讨论】:

    • 感谢您的回复。我从这样定义的系列开始,但有些函数会剥离 ts 属性,问题是如何重新分配它们。很高兴知道 dim() 方法。
    • 见最后添加的部分。
    • 您能否详细介绍一下 xx[] 如何存储时间信息?这对我来说是新概念。
    • dim() 方法是否通过添加虚拟列来工作?
    • xx[]&lt;-whateverxx 的数据部分替换为任何内容。 xx 的属性不变。 dim 如图所示将数据组件重塑为列列矩阵。
    猜你喜欢
    • 2021-02-18
    • 2023-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-09
    • 2015-08-08
    • 1970-01-01
    • 2020-05-08
    相关资源
    最近更新 更多