从季度系列中形成每月系列 [英] Form a monthly series from a quarterly series

查看:224
本文介绍了从季度系列中形成每月系列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有季度GDP变动数据,如下所示:

Assume that we have quarterly GDP change data like the following:

         Country
1999Q3   0.01
1999Q4   0.01
2000Q1   0.02
2000Q2   0.00
2000Q3  -0.01

现在,我想根据例如前两个季度的平均值,作为代表经济状况的一种度量. IE.根据以上数据,我想得出以下结果:

Now, I would like to turn this into a monthly series based on e.g. the mean of the previous two quarters, as one measure to represent the economic conditions. I.e. with the above data I would like to produce the following:

          Country
2000-01   0.01
2000-02   0.01
2000-03   0.01
2000-04   0.015
2000-05   0.015
2000-06   0.015
2000-07   0.01
2000-08   0.01
2000-09   0.01
2000-10  -0.005
2000-11  -0.005
2000-12  -0.005

因此,我可以对其他每月系列进行回归分析.从高频率到低频率的数据聚合很容易,但是我将如何朝相反的方向进行呢?

This is so that I can run regressions with other monthly series. Aggregating data from more frequent to less frequent is easy, but how would I do it to the opposite direction?

编辑. 看来使用spline是执行此操作的正确方法.问题是,用apply进行spline时,在国家系列的开头如何处理不同数量的NA.像往常一样,数据框中有多个国家/地区作为列,并且在系列开始时它们具有不同数量的NA.

Edit. It seems that using spline would be the right way to do this. The question is then, how does that handle a varying amount of NA's in the beginning of the country series, when doing spline with apply. There are multiple countries in the data frame as columns, as usual, and they have a varying amount of NA's in the beginning of the series.

推荐答案

使用值"yearmon"的类索引转换为zoo,前提是值位于四分之一结尾处.然后执行滚动平均值给出z.mu.现在,将其与包含所有月份的零宽度动物园对象合并,并使用na.spline填写缺失值(或对于不同形式的插值使用na.locfna.approx). (可选)使用fortify.zoo转换回data.frame.

Convert to zoo with "yearmon" class index assuming the values are at the ends of the quarters. Then perform the rolling mean giving z.mu. Now merge that with a zero width zoo object containing all the months and use na.spline to fill in the missing values (or use na.locf or na.approx for different forms of interpolation). Optionally use fortify.zoo to convert back to a data.frame.

library(zoo)

z <- zoo(coredata(DF), as.yearmon(as.yearqtr(rownames(DF)), frac = 1))
z.mu <- rollmeanr(z, 2, partial = TRUE)
ym <- seq(floor(start(z.mu)), floor(end(z.mu)) + 11/12, 1/12)
z.ym <- na.spline(merge(z.mu, zoo(, ym)))

fortify.zoo(z.ym)

给予:

      Index      Country
1  Jan 1999 -0.065000000
2  Feb 1999 -0.052222222
3  Mar 1999 -0.040555556
4  Apr 1999 -0.030000000
5  May 1999 -0.020555556
6  Jun 1999 -0.012222222
7  Jul 1999 -0.005000000
8  Aug 1999  0.001111111
9  Sep 1999  0.006111111
10 Oct 1999  0.010000000
11 Nov 1999  0.012777778
12 Dec 1999  0.014444444
13 Jan 2000  0.015000000
14 Feb 2000  0.014444444
15 Mar 2000  0.012777778
16 Apr 2000  0.010000000
17 May 2000  0.006111111
18 Jun 2000  0.001111111
19 Jul 2000 -0.005000000
20 Aug 2000 -0.012222222
21 Sep 2000 -0.020555556
22 Oct 2000 -0.030000000
23 Nov 2000 -0.040555556
24 Dec 2000 -0.052222222

注意:以可复制的形式使用的输入DF是:

Note: The input DF in reproducible form used is:

Lines <- "         Country
1999Q3   0.01
1999Q4   0.01
2000Q1   0.02
2000Q2   0.00
2000Q3  -0.01"

DF <- read.table(text = Lines)

更新:最初询问该问题是将最后一个值向前移动,但已更改为询问样条插值,因此答案已相应更改.还更改为从1月开始到12月结束,现在假定数据为季度末.

Update: Originally question asked to move last value forward but was changed to ask for spline interpolation so answer has been changed accordingly. Also changed to start in Jan and end in Dec and now assume data is for quarter end.

这篇关于从季度系列中形成每月系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆