Form a monthly series from a quarterly series

给你一囗甜甜゛ 提交于 2019-12-20 04:24:38

问题


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

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?

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.


回答1:


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)

giving:

      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

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)

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.



来源:https://stackoverflow.com/questions/36387160/form-a-monthly-series-from-a-quarterly-series

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!