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

How to transform annual data into quarterly?

我有一个包含年度数据的 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")))

您可以将 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