簡體   English   中英

如何提取單變量時間序列的時間屬性?

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

如何通過添加時間屬性將簡單的時間序列對象轉換為尺寸為2的維度,以便可以使用colname?

編輯:

在這個問題中 ,引用時間序列的方法是使用別名,建議的方法是將其轉換為動物園對象。 在單個時間序列的情況下,如果我執行names(tseries),它將給出NULL。 因此,要分配名稱,請嘗試使用只能在2維對象中使用並通過添加日期索引構造的colnames。 在這里,我嘗試使用列表對象中已經存在的時間屬性,以便不必從動物園對象來回轉換。

如果有其他選擇,我將很高興知道這一點。 目的是能夠在地塊標題等中使用時間序列的名稱。

編輯2:

換個問題,如何給時間序列命名? 使用應用會剝離時間序列屬性,如此處許多問題所問。 然后,要獲取時間屬性和名稱,並采用允許使用colnames的格式,有哪些方法? 我從系列和一列日期開始,這些日期允許命名系列,但是以從lapply(tslist,function)返回的矢量格式,我需要重新分配名稱。 如果有更簡單的方法,請告訴我,因為這是主要問題。

一個有點相關的問題是鏈接 ,其中@Henrik的答案建議添加一個虛擬列,以避免將Zoo對象強制轉換為向量,以保持其尺寸為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對象,或將其與日期列綁定。

由於名稱和時間屬性信息存在於我們要進行子設置的起始對象中,是否有一種方法可以使用它來提取具有時間屬性的組件?

編輯:

提取的序列必須具有與列表對象中存在的輸入序列相似的結構

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及其屬性?

分配名字時如何訪問chr“系列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)<-tsp(x)

  2. 在tsp中使用開始,結束,頻率信息由於tsp的月度數據是零星的,所以不知道如何獲取開始月信息,即將1970.250轉換為1970,4說((不知道哪個月是開始月)然后我可以做些什么喜歡

    h1.ts <-ts(h1,開始= c(tsp 1 ,n),頻率= tsp [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

您可以使用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                                    

如果問題是如何創建一個名稱為nx 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轉換為具有名稱的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

添加如果我們有:

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

和屬性被剝奪:

xx2 <- c(xx)

那么我們可以這樣做:

xx[] <- xx2

我不確定所需的輸出是什么。 希望這可以幫助。 請顯示所需的輸出。

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