如何将年度数据转换为季度数据?

Posted

技术标签:

【中文标题】如何将年度数据转换为季度数据?【英文标题】:How to transform annual data into quarterly? 【发布时间】:2022-01-01 12:43:02 【问题描述】:

我有一个带有年度数据的 xts 对象。我想将其转换为季度数据,这些值将在一年中保持不变,因此我不必对数据进行插值。

我试过apply.quarterlyto.quarterly,但数据仍然是每年。

这是我的代码:

library(xts)

apply.quarterly(x, FUN = mean)

我的输出:

            Cons_gov      FBKF
1996-12-01 0.1975085 0.1864072
1997-12-01 0.1953536 0.1912290
1998-12-01 0.2007051 0.1854231
1999-12-01 0.1978274 0.1701628
2000-12-01 0.1876777 0.1830450
2001-12-01 0.1934325 0.1841800
2002-12-01 0.1980967 0.1792619
2003-12-01 0.1907744 0.1660478
2004-12-01 0.1846763 0.1732019
2005-12-01 0.1888999 0.1705619
2006-12-01 0.1903895 0.1721032
2007-12-01 0.1894298 0.1799576
2008-12-01 0.1883942 0.1938534
2009-12-01 0.1965066 0.1910200
2010-12-01 0.1901688 0.2053467
2011-12-01 0.1866926 0.2060899
2012-12-01 0.1853010 0.2071671
2013-12-01 0.1889248 0.2091192
2014-12-01 0.1915354 0.1987301
2015-12-01 0.1977682 0.1783580

我的dput()

structure(c(0.197508549153391, 0.195353585641678, 0.200705142210663, 
0.197827364070052, 0.187677686384626, 0.193432520923332, 0.198096706851479, 
0.190774358523613, 0.184676286553441, 0.188899853679931, 0.190389507978999, 
0.189429845570079, 0.188394248767526, 0.1965065515285, 0.190168835777631, 
0.186692569341525, 0.185300994171881, 0.18892479001219, 0.191535387119431, 
0.197768166586277, 0.203792941796483, 0.201619077612893, 0.198950735000909, 
0.200777424344436, 0.204929121903237, 0.183310918318924, 0.186407226330573, 
0.191228971241134, 0.18542307036158, 0.170162846564942, 0.183045018192917, 
0.184180045540359, 0.179261923121358, 0.166047809279776, 0.173201889924658, 
0.170561931719758, 0.172103177073606, 0.179957599687971, 0.193853437018358, 
0.191019967063092, 0.205346736503007, 0.206089870582595, 0.207167084721339, 
0.20911921875888, 0.198730115991599, 0.178358040677889, 0.155243408026601, 
0.145589735475542, 0.150969119553704, 0.153124811412088, 0.16430683828827, 
0.187924188341473), index = structure(c(849398400, 880934400, 
912470400, 944006400, 975628800, 1007164800, 1038700800, 1070236800, 
1101859200, 1133395200, 1164931200, 1196467200, 1228089600, 1259625600, 
1291161600, 1322697600, 1354320000, 1385856000, 1417392000, 1448928000, 
1480550400, 1512086400, 1543622400, 1575158400, 1606780800, 1622505600
), tzone = "UTC", tclass = "Date"), class = c("xts", "zoo"), .Dim = c(26L, 
2L), .Dimnames = list(NULL, c("Cons_gov", "FBKF")))

【问题讨论】:

这里的问题是,您需要在您的年度周期中使用 12 的频率才能将其折叠为季度。 我该怎么做? apply.period 函数将数据从较高频率汇总到较低频率。所以,从每天到每周,或每月,等等。而不是相反。没有数据可指定。如果您想要每季度一次,则必须提供您自己的日期和数字。只除以 4 是一种选择,但可能不正确。 @AlexandreSanches 我认为您需要一个时间序列,其中每个时间点都是例如一年中每个月的开始。通过这种方式,您可以将其汇总为季度频率。 【参考方案1】:

您可以将xts 对象转换为数据框。然后,您可以在给定的时间内重复 data.frame 的每一行。我使用dplyr 进行数据转换,使用lubridate 进行日期信息操作,最后我将其转换为季度格式。

见下面xts要转换的对象:

yearly <- structure(c(0.197508549153391, 0.195353585641678, 0.200705142210663, 
            0.197827364070052, 0.187677686384626, 0.193432520923332, 0.198096706851479, 
            0.190774358523613, 0.184676286553441, 0.188899853679931, 0.190389507978999, 
            0.189429845570079, 0.188394248767526, 0.1965065515285, 0.190168835777631, 
            0.186692569341525, 0.185300994171881, 0.18892479001219, 0.191535387119431, 
            0.197768166586277, 0.203792941796483, 0.201619077612893, 0.198950735000909, 
            0.200777424344436, 0.204929121903237, 0.183310918318924, 0.186407226330573, 
            0.191228971241134, 0.18542307036158, 0.170162846564942, 0.183045018192917, 
            0.184180045540359, 0.179261923121358, 0.166047809279776, 0.173201889924658, 
            0.170561931719758, 0.172103177073606, 0.179957599687971, 0.193853437018358, 
            0.191019967063092, 0.205346736503007, 0.206089870582595, 0.207167084721339, 
            0.20911921875888, 0.198730115991599, 0.178358040677889, 0.155243408026601, 
            0.145589735475542, 0.150969119553704, 0.153124811412088, 0.16430683828827, 
            0.187924188341473), index = structure(c(849398400, 880934400, 
                                                    912470400, 944006400, 975628800, 1007164800, 1038700800, 1070236800, 
                                                    1101859200, 1133395200, 1164931200, 1196467200, 1228089600, 1259625600, 
                                                    1291161600, 1322697600, 1354320000, 1385856000, 1417392000, 1448928000, 
                                                    1480550400, 1512086400, 1543622400, 1575158400, 1606780800, 1622505600
            ), tzone = "UTC", tclass = "Date"), class = c("xts", "zoo"), .Dim = c(26L, 
                                                                                  2L), .Dimnames = list(NULL, c("Cons_gov", "FBKF")))

算法本身:

library(xts)
library(tidyverse)
library(lubridate)

quarterly <- as_tibble(yearly, rownames = "dates") %>%
  slice(rep(1:n(), each = 4)) %>%
  mutate(years = year(dates)) %>%
  mutate(months = rep((1:4) * 3, times = n() / 4)) %>%
  mutate(dates = ymd(paste0(years, "-", months, "-01"))) %>%
  select(-years, -months) %>%
  column_to_rownames(var = "dates") %>%
  as.xts 
xts1 <- to.quarterly(quarterly[, 1])
xts2 <- to.quarterly(quarterly[, 2])
final <- cbind(xts1[, 1], xts2[, 1])
names(final) <- c("Cons_gov", "FBKF")
head(final)

输出:

         Cons_gov      FBKF
1996 Q1 0.1975085 0.1864072
1996 Q2 0.1975085 0.1864072
1996 Q3 0.1975085 0.1864072
1996 Q4 0.1975085 0.1864072
1997 Q1 0.1953536 0.1912290
1997 Q2 0.1953536 0.1912290

【讨论】:

谢谢!你帮了我很多!

以上是关于如何将年度数据转换为季度数据?的主要内容,如果未能解决你的问题,请参考以下文章

在 Python Pandas 中将带有元素日期标签的年度财政数据元组转换为时间序列

如何在熊猫中将月度数据转换为季度

【Mysql】按季度查询结果数据累加

VB 求当前日期的上个季度的数据

将字符串转换为日期 [带年份和季度]

如何在 Python 中将日期转换为季度?