【问题标题】:Construct multiple series ("mts") in R在 R 中构造多个系列(“mts”)
【发布时间】:2017-05-02 08:47:43
【问题描述】:

我找不到工作示例如何为时间序列制作 mts 对象。

我已经做了一个输入数据的例子。有一个频率为 4 的时间序列。我想将它分成 4 个时间序列,表示为 mts 格式(在ts 的文档中描述):


为结果赋予的类,如果为 NULL 或“无”,则为无。单个系列默认为 "ts",多个系列默认为 c("mts", "ts", "matrix")。

如何拆分?我找不到任何有关如何创建“mts”对象的示例文档。

     Qtr1   Qtr2   Qtr3   Qtr4
1  100.00 102.09 102.50 102.60
2  102.66 102.01 100.70 102.21
3  102.50 102.81 102.91 103.08
4  103.25 101.70 101.10 101.49
5  100.12  99.40  99.28  97.75
6   96.65  99.03  99.28  99.91
7   99.29  98.57  98.45 100.27
8  100.65 102.12 101.55 101.55
9   99.51  99.95  98.91  96.87
10  95.14  94.82  96.04  96.91
11  95.53  93.14  92.97  94.54
12  91.72  92.35  94.79  94.78
13  95.50  95.97  94.64  96.56
14  96.45  95.13  94.32  92.92

附录

structure(c(100, 102.09, 102.5, 102.6, 102.66, 102.01, 100.7, 
102.21, 102.5, 102.81, 102.91, 103.08, 103.25, 101.7, 101.1, 
101.49, 100.12, 99.4, 99.28, 97.75, 96.65, 99.03, 99.28, 99.91, 
99.29, 98.57, 98.45, 100.27, 100.65, 102.12, 101.55, 101.55, 
99.51, 99.95, 98.91, 96.87, 95.14, 94.82, 96.04, 96.91, 95.53, 
93.14, 92.97, 94.54, 91.72, 92.35, 94.79, 94.78, 95.5, 95.97, 
94.64, 96.56, 96.45, 95.13, 94.32, 92.92, 92.58, 89.74, 87.7, 
89.5, 86.69, 87.91, 89.12, 88.93, 89.95, 90.42, 90.22, 90.75, 
90.2, 89.5, 90.52, 90.88, 90.98, 91.85, 92.03, 91.85, 92.54, 
92.69, 92.36, 92.35, 91.56, 92.67, 92.88, 92.26, 92.36, 92.51, 
92.26, 92.47, 92.65, 92.89, 91.67, 91.95, 92.77, 92.59, 91.83, 
92.2, 91.47, 92.33, 92.03, 92.74, 92.74, 93.13, 94.61, 95.23, 
95.04, 95.35, 96.46, 96.6, 95.93, 95.61, 95.22, 95.34, 95.58, 
95.64, 94.41, 94.73, 93.64, 92.87, 92.89, 93.67, 94.69, 94.51, 
94.14, 94.53, 94.63, 95.15, 94.67, 95.75, 96.16, 96.99, 97.3, 
97.97, 98.34, 98.28, 97.85, 97.54, 97.48, 97.09, 97.28, 97.8, 
97.08, 96.28, 94.58, 92.94, 91.26, 91.92, 91.03, 92.13, 94.45, 
94.68, 95.02, 95.79, 92.29, 89.45, 91.52, 93.9, 94.51, 95.47, 
95.1, 93.4, 92.55, 93.33, 94.33, 95.8, 96.19, 96.4, 96.72, 96.34, 
96.7, 96.22, 97.27, 96.84, 97.08, 97.14, 97.45, 97.65, 96.6, 
96.99, 95.89, 95.62, 96.48, 97.5, 97.57, 98.43, 98.37, 99.31, 
99.27, 99.38, 98.45, 97.56, 98.06, 97.39, 97.69, 98.21, 98.12, 
97.56, 97.97, 98.01, 98.47, 98.14, 97.87, 99.25, 99.73, 99.55, 
99.8, 99.74, 98.96, 98.12, 98.07, 98.12, 98.53, 97.87, 98.66, 
98.89, 98.9, 99.94, 99.66, 98.7, 98.59, 99.22, 98.68, 98.74, 
99.39, 100.06, 99.77, 99.48, 98.73, 99.41, 98.95, 98.35, 97.54, 
98.64, 97.71, 98.44, 98.85, 98.31, 98.22, 97.72, 97.27, 97.31, 
97.31, 97.27, 96.9, 95.1, 94.73, 94.03, 95.68, 95.94, 97.13, 
97.39, 96.62, 96.93, 97.26, 97.48, 98.23, 98.1, 97.52, 96.88, 
96.81, 97.39, 98.39, 97.93, 98.48, 98.64, 97.77, 97.74, 98.44, 
99.1, 99.16, 99.27, 100.41, 99.54, 100.67, 100.21, 100.87, 101.45, 
101.42, 101.52, 101.55, 101.7, 101.8, 102.18, 102.16, 102.05, 
101.86, 102.97, 103.43, 103.75, 103.85, 103.86, 103.99, 103.87, 
103.39, 104.23, 103.47, 102.98, 103.01, 102.92, 102.75, 102.24, 
102.22, 103.86, 104.07, 103.72, 102.97, 102.86, 103.26, 102.72, 
103.57, 103.35, 103.88, 103.86, 104.56, 104.79, 104.9, 104.66, 
105.25, 105.1, 105.4, 105.61, 106.15, 106.28, 106.15, 105.53, 
105.67, 105.94, 106.88, 107.58, 107.64, 107.61, 107.35, 107.41, 
107.53, 107.91, 108.02, 107.77, 108.07, 108.4, 108.69, 108.71, 
107.81, 107.18, 108.02, 107.87, 107.53, 107.64, 108.33, 108.87, 
108.71, 108.45, 108.64, 108.7, 108.74, 108.79, 109.23, 109.42, 
109.82, 109.52, 108.95, 109.26, 109.36, 109.35, 110.22, 110.72
), .Dim = c(364L, 1L), index = structure(c(1447632000, 1447718400, 
1447804800, 1447891200, 1447977600, 1448236800, 1448323200, 1448409600, 
1448496000, 1448582400, 1448841600, 1448928000, 1449014400, 1449100800, 
1449187200, 1449446400, 1449532800, 1449619200, 1449705600, 1449792000, 
1450051200, 1450137600, 1450224000, 1450310400, 1450396800, 1450656000, 
1450742400, 1450828800, 1451260800, 1451347200, 1451433600, 1451520000, 
1451865600, 1451952000, 1452038400, 1452124800, 1452211200, 1452470400, 
1452556800, 1452643200, 1452729600, 1452816000, 1453075200, 1453161600, 
1453248000, 1453334400, 1453420800, 1453680000, 1453766400, 1453852800, 
1453939200, 1454025600, 1454284800, 1454371200, 1454457600, 1454544000, 
1454630400, 1454889600, 1454976000, 1455062400, 1455148800, 1455235200, 
1455494400, 1455580800, 1455667200, 1455753600, 1455840000, 1456099200, 
1456185600, 1456272000, 1456358400, 1456444800, 1456704000, 1456790400, 
1456876800, 1456963200, 1457049600, 1457308800, 1457395200, 1457481600, 
1457568000, 1457654400, 1457913600, 1.458e+09, 1458086400, 1458172800, 
1458259200, 1458518400, 1458604800, 1458691200, 1458777600, 1459209600, 
1459296000, 1459382400, 1459468800, 1459728000, 1459814400, 1459900800, 
1459987200, 1460073600, 1460332800, 1460419200, 1460505600, 1460592000, 
1460678400, 1460937600, 1461024000, 1461110400, 1461196800, 1461283200, 
1461542400, 1461628800, 1461715200, 1461801600, 1461888000, 1462147200, 
1462233600, 1462320000, 1462492800, 1462752000, 1462838400, 1462924800, 
1463011200, 1463097600, 1463443200, 1463529600, 1463616000, 1463702400, 
1463961600, 1464048000, 1464134400, 1464220800, 1464307200, 1464566400, 
1464652800, 1464739200, 1464825600, 1464912000, 1465171200, 1465257600, 
1465344000, 1465430400, 1465516800, 1465776000, 1465862400, 1465948800, 
1466035200, 1466121600, 1466380800, 1466467200, 1466553600, 1466640000, 
1466726400, 1466985600, 1467072000, 1467158400, 1467244800, 1467331200, 
1467590400, 1467676800, 1467763200, 1467849600, 1467936000, 1468195200, 
1468281600, 1468368000, 1468454400, 1468540800, 1468800000, 1468886400, 
1468972800, 1469059200, 1469145600, 1469404800, 1469491200, 1469577600, 
1469664000, 1469750400, 1470096000, 1470182400, 1470268800, 1470355200, 
1470614400, 1470700800, 1470787200, 1470873600, 1470960000, 1471219200, 
1471305600, 1471392000, 1471478400, 1471564800, 1471824000, 1471910400, 
1471996800, 1472083200, 1472169600, 1472428800, 1472515200, 1472601600, 
1472688000, 1472774400, 1473033600, 1473120000, 1473206400, 1473292800, 
1473379200, 1473638400, 1473724800, 1473811200, 1473897600, 1473984000, 
1474243200, 1474329600, 1474416000, 1474502400, 1474588800, 1474848000, 
1474934400, 1475020800, 1475107200, 1475193600, 1475452800, 1475539200, 
1475625600, 1475712000, 1475798400, 1476057600, 1476144000, 1476230400, 
1476316800, 1476403200, 1476662400, 1476748800, 1476835200, 1476921600, 
1477008000, 1477267200, 1477353600, 1477440000, 1477526400, 1477612800, 
1477872000, 1478044800, 1478131200, 1478217600, 1478476800, 1478563200, 
1478649600, 1478736000, 1478822400, 1479081600, 1479168000, 1479254400, 
1479340800, 1479427200, 1479686400, 1479772800, 1479859200, 1479945600, 
1480032000, 1480291200, 1480377600, 1480464000, 1480550400, 1480636800, 
1480896000, 1480982400, 1481068800, 1481155200, 1481241600, 1481500800, 
1481587200, 1481673600, 1481760000, 1481846400, 1482105600, 1482192000, 
1482278400, 1482364800, 1482451200, 1482796800, 1482883200, 1482969600, 
1483056000, 1483401600, 1483488000, 1483574400, 1483660800, 1483920000, 
1484006400, 1484092800, 1484179200, 1484265600, 1484524800, 1484611200, 
1484697600, 1484784000, 1484870400, 1485129600, 1485216000, 1485302400, 
1485388800, 1485475200, 1485734400, 1485820800, 1485907200, 1485993600, 
1486080000, 1486339200, 1486425600, 1486512000, 1486598400, 1486684800, 
1486944000, 1487030400, 1487116800, 1487203200, 1487289600, 1487548800, 
1487635200, 1487721600, 1487808000, 1487894400, 1488153600, 1488240000, 
1488326400, 1488412800, 1488499200, 1488758400, 1488844800, 1488931200, 
1489017600, 1489104000, 1489363200, 1489449600, 1489536000, 1489622400, 
1489708800, 1489968000, 1490054400, 1490140800, 1490227200, 1490313600, 
1490572800, 1490659200, 1490745600, 1490832000, 1490918400, 1491177600, 
1491264000, 1491350400, 1491436800, 1491523200, 1491782400, 1491868800, 
1491955200, 1492041600, 1492473600, 1492560000, 1492646400, 1492732800, 
1492992000, 1493078400), tzone = "UTC", tclass = "Date"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", .Dimnames = list(
    NULL, "Series 1"), class = "ts", .Tsp = c(1, 91.75, 4))

【问题讨论】:

  • ?stats::ts的文档中,这个例子是z <- ts(matrix(rnorm(300), 100, 3), start = c(1961, 1), frequency = 12); class(z);head(z);index(z)。本质上对于多元序列,您需要数字数据的矩阵输入,并将公共索引指定为startfrequency 的输入。
  • @hNu 有什么办法可以使它具有不规则的时间序列(当一个时间索引与另一个不同时)?
  • 据我所知,您无法使用ts 为多元数据设置不规则索引。但是,您可以使用 xts 包中的 xts 函数创建单个时间序列(在您的情况下为 4 个),并使用函数 merge.xts 合并它们。
  • @hNu 你能发表一个答案,所以我结束了这个问题?
  • 您还可以查看出色的 zoo 包以获得类似的功能。我们的成员hrbrmstr 的一些例子是here

标签: r time-series


【解决方案1】:

一个基于每小时数据的多个时间序列示例。

require(stats)
require(lubridate)



data_mts <- ts(data=yourDATA[,c("Qtr1","Qtr2","Qtr3","Qtr4")], 
frequency=61320, start=c(year(min(data_IE$time)),month(min(data_IE$time)),                                                                               date(min(data_IE$time)),hour(min(data_IE$time))))

#This frequency variable is for hourly data,hourly frequency, i.e. 61320.


【讨论】:

    【解决方案2】:

    使用xtszoo 包,您可以创建单独的时间序列并将它们合并如下:

    library("xts") 
    library("zoo") #for as.yearqtr
    
    #tsDF, your input data
    
    #extract individual time series
    tsMat = matrix(tsDF,ncol=4)
    
    #setup hypothetical indices as yearqtr e.g. 1990 Q1, 1992 Q2 etc.
    startYear1 = 1990
    startYear2 = 1990
    startYear3 = 1991
    startYear4 = 1992
    
    index1=seq(startYear1,by=1,length.out=nrow(tsMat))
    index2=seq(startYear2,by=1,length.out=nrow(tsMat))
    index3=seq(startYear3,by=1,length.out=nrow(tsMat))
    index4=seq(startYear4,by=1,length.out=nrow(tsMat))
    
    index1 = as.yearqtr(paste(index1,"Q1",sep=" "))
    index2 = as.yearqtr(paste(index2,"Q2",sep=" "))
    index3 = as.yearqtr(paste(index3,"Q3",sep=" "))
    index4 = as.yearqtr(paste(index4,"Q4",sep=" "))
    
    
    #combine indices in a list
    listIndices = list(index1,index2,index3,index4)
    
    nameVec = paste0("Qtr",1:4)
    
    #Use lapply to create individual xts time series with its correponding index created above
    
    tsList = 
    
    lapply(1:ncol(tsMat), function(x) {
    
    xtsObj = xts(tsMat[,x],order.by=listIndices[[x]])
    colnames(xtsObj) = nameVec[x]
    
    return(xtsObj)
    
    })
    

    输出:

    #Use Reduce function to merge the list output from previous step
    tsAggregate = Reduce(function(x,y) merge.xts(x,y),tsList)
    
    
    
    head(tsAggregate,10)
    #          Qtr1  Qtr2  Qtr3   Qtr4
    #1990 Q1 100.00    NA    NA     NA
    #1990 Q2     NA 91.95    NA     NA
    #1991 Q1 102.09    NA    NA     NA
    #1991 Q2     NA 92.77    NA     NA
    #1991 Q3     NA    NA 97.57     NA
    #1992 Q1 102.50    NA    NA     NA
    #1992 Q2     NA 92.59    NA     NA
    #1992 Q3     NA    NA 98.43     NA
    #1992 Q4     NA    NA    NA 100.21
    #1993 Q1 102.60    NA    NA     NA
    

    【讨论】:

      猜你喜欢
      • 2012-11-15
      • 2021-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-17
      • 2016-07-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多