简体   繁体   English

如何提取单变量时间序列的时间属性?

[英]How to extract time attributes for a univariate time series?

How can I convert a simple time series object to be of dimensions 2 by adding time attributes so that colnames can be used? 如何通过添加时间属性将简单的时间序列对象转换为尺寸为2的维度,以便可以使用colname?

Edit : 编辑:

In this question , the way to refer to time series was to use colnames and suggested method was to convert it to zoo objects. 在这个问题中 ,引用时间序列的方法是使用别名,建议的方法是将其转换为动物园对象。 In case of single time series, if I do names(tseries), it gives NULL. 在单个时间序列的情况下,如果我执行names(tseries),它将给出NULL。 So to assign the names, tried using colnames which can only be used in objects of dimension 2 and constructed by adding index of dates. 因此,要分配名称,请尝试使用只能在2维对象中使用并通过添加日期索引构造的colnames。 Here I am trying to use the time attributes already there in the list object so that I dont have to convert back and forth from zoo objects. 在这里,我尝试使用列表对象中已经存在的时间属性,以便不必从动物园对象来回转换。

If there is an alternative I will be very happy to know that. 如果有其他选择,我将很高兴知道这一点。 The purpose is to be able to use the name of the time series in title of plots etc. 目的是能够在地块标题等中使用时间序列的名称。

Edit 2: 编辑2:

To rephrase the question, how to give name to a time series ? 换个问题,如何给时间序列命名? Using apply strips the time series attributes as asked in many questions here. 使用应用会剥离时间序列属性,如此处许多问题所问。 Then to get back the time attributes and the name, and in a format which allows using colnames, what are the methods ? 然后,要获取时间属性和名称,并采用允许使用colnames的格式,有哪些方法? I started with series and a column of dates which allowed naming the series but in a vector format returned from the lapply(tslist, function), I need to reallocate the names. 我从系列和一列日期开始,这些日期允许命名系列,但是以从lapply(tslist,function)返回的矢量格式,我需要重新分配名称。 If there is a simpler way to do this, please do let me know as that is the main problem. 如果有更简单的方法,请告诉我,因为这是主要问题。

A somewhat related question is link where answer by @Henrik suggests adding a dummy column to avoid coercion of zoo object to a vector to keep the it of dimension 2. 一个有点相关的问题是链接 ,其中@Henrik的答案建议添加一个虚拟列,以避免将Zoo对象强制转换为向量,以保持其尺寸为2。

In a specific situation where we have a list object with required time info, I followed this method. 在特定的情况下,我们有一个带有所需时间信息的列表对象,我遵循此方法。

When using HP filter to find the cycle, I do the following 使用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 of main object from which subsetting is 子集所在的主要对象的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 is an object of dimension 1 because when I try to assign it a name, c1是维度1的对象,因为当我尝试为其分配名称时,

colnames(c1) <- "x" 

gives this error 给出这个错误

Error in `colnames<-`(`*tmp*`, value = "iip") : 
  attempt to set 'colnames' on an object with less than two dimensions

To help use name of time series, we can either convert c1 to a zoo object by adding an index or cbind it with a column of dates. 为了帮助使用时间序列的名称,我们可以通过添加索引将c1转换为Zoo对象,或将其与日期列绑定。

Since the name and time attributes information is there in the starting object from which we are subsetting, is there a way to use it to extract a component with the time attributes ? 由于名称和时间属性信息存在于我们要进行子设置的起始对象中,是否有一种方法可以使用它来提取具有时间属性的组件?

Edit : 编辑:

The extracted series must have structure like that of inputted series which is there in the list object 提取的序列必须具有与列表对象中存在的输入序列相似的结构

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

which I think can be accessed as 我认为可以作为

attributes(c1) <- attributes(hp$x)

colnames(c1) <- "X1"  # is now possible. 

How can I return hp$cycle along with its attributes ? 如何返回hp $ cycle及其属性?

How can I access chr "Series 1" when allocating colnames ? 分配名字时如何访问chr“系列1”?

colnames(c1) 

# Series 1

Edit : 编辑:

## 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

Thanks a lot. 非常感谢。

Edit : 编辑:

These are the alternatives I used to extract the time attributes from one series and add to another series. 这些是我用来从一个系列中提取时间属性并添加到另一个系列中的替代方法。

  1. Using tsp : This was giving error or dimension mismatch 使用tsp:这给出了错误或尺寸不匹配

    tsp(h1) <- tsp(x) tsp(h1)<-tsp(x)

  2. Using start, end, frequency information in tsp Since tsp for monthly data is in fractions, didnt know how to get the starting month info ie converting 1970.250 to 1970, 4 say, (dont know which is the starting month) Then I could do something like 在tsp中使用开始,结束,频率信息由于tsp的月度数据是零星的,所以不知道如何获取开始月信息,即将1970.250转换为1970,4说((不知道哪个月是开始月)然后我可以做些什么喜欢

    h1.ts <- ts(h1, start =c(tsp 1 , n), frequency = tsp[3]) h1.ts <-ts(h1,开始= c(tsp 1 ,n),频率= tsp [3])

Based on the info provided, I guess you can try: 根据提供的信息,我想您可以尝试:

 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"

If you look at the str(hp) , it is a list of 10 elements. 如果看str(hp) ,它是10个元素的列表。 To access the attributes of different elements, you can use attributes or attr . 要访问不同元素的属性,可以使用attributesattr For example: 例如:

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

Or 要么

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

To get the whole attributes for the list element 获取列表元素的整个attributes

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

 #$class
 #[1] "ts"

Using c concatenates the attributes from different list elements and keep the list structure 使用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

You can use start , end and time : 您可以使用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                                    

If the question is how to create an nx 1 monthly ts series with a name then: 如果问题是如何创建一个名称为nx 1的每月ts系列,则:

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

We can now refer to it by name: 我们现在可以通过名称来引用它:

xx[, "A"]

We can extract various components like this: 我们可以提取各种组件,如下所示:

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)))

We can convert yy below to an nx 1 series with a name: 我们可以将下面的yy转换为具有名称的nx 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

Added If we have: 添加如果我们有:

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

and attributes get stripped: 和属性被剥夺:

xx2 <- c(xx)

then we can do this: 那么我们可以这样做:

xx[] <- xx2

I am not sure what the desired output. 我不确定所需的输出是什么。 Hope this helps. 希望这可以帮助。 Please show the desired output. 请显示所需的输出。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM