插入/扩展季度到月度系列 [英] Interpolate / Extend quarterly to monthly series

查看:23
本文介绍了插入/扩展季度到月度系列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含季度观察结果的 data.frame.我现在想插入每月值(首选三次,线性很好).中间目标应该是创建一个 data.frame,以 DATE 作为索引和所有每月观察的缺失值.

I have a data.frame that contains quarterly observations. I now want to interpolate monthly values (preferred cubic, linear is fine). The intermediate goal should be to create a data.frame with DATE as the index and missing values for all the monthly observations.

谷歌搜索显示我应该为整个时间范围创建一个空的 data.frame 然后合并它 - 但到目前为止我尝试过的任何东西都给了我错误.这是我的程序;但由于我是 r 的新手,我愿意接受任何更改建议.

Googling showed that I should create an empty data.frame for the whole time range and then merge it - but what ever I tried so far gave me errors. Here's my procedure; but since I'm a newb to r, I'm open to any suggestions for changes.

> str(ger)
'data.frame':   93 obs. of  2 variables:
 $ DATE : Date, format: "1991-01-01" "1991-04-01" "1991-07-01" "1991-10-01" ...
 $ VALUE: num  470780 468834 466332 472949 480359 ...
> head(ger)
        DATE    VALUE
1 1991-01-01 470780.3
2 1991-04-01 468834.0
3 1991-07-01 466331.6
4 1991-10-01 472949.0
5 1992-01-01 480359.2
6 1992-04-01 476744.5

emptyIndex <- seq(ger[1, 'DATE'], tail(ger[, 'DATE'], 1), by='1 month')
gerMonthly <- data.frame(DATE = emptyIndex, VALUE = NA)
merge(ger, gerMonthly, by='DATE', all.y = T)

这是我得到的最接近的,但它给了我不想要的列格式 - 肯定有一种更干净的方法来获得我想要的东西?最后,鉴于格式,获得内插时间序列的最简洁方法是什么?

This is the closest I got, but it gives me an undesired column format - there surely is a cleaner way to get what I want? Finally, given the format, what would be the cleanest way to get the interpolated time series?

          DATE  VALUE.x VALUE.y
1   1991-01-01 470780.3      NA
2   1991-02-01       NA      NA
3   1991-03-01       NA      NA
4   1991-04-01 468834.0      NA
5   1991-05-01       NA      NA
6   1991-06-01       NA      NA

推荐答案

我不太清楚您对不需要的列格式的评论,但如果您尝试使用三次插值获得插值,您可能会考虑类似于下面的代码

I'm not quite clear on your comment about the undesired column format but if you're trying to get the interpolated values using a cubic interpolation, you might consider something like the code below

ger <- data.frame(DATE= as.Date(c("1991-01-01", "1991-04-01", "1991-07-01", "1991-10-01", "1992-01-01" )),
              +                   VALUE= c(470780, 468834, 466332, 472949, 480359))
DateSeq <- seq(ger$DATE[1],tail(ger$DATE,1),by="1 month")
gerMonthly <- data.frame(DATE=DateSeq, Interp.Value=spline(ger, method="natural", xout=DateSeq)$y)
merge(ger, gerMonthly, by='DATE', all.y = T)

DATE 列需要采用日期格式,以便插值可以处理数值.我通常使用自然"三次样条,但也可以使用其他选项.此格式同时显示输入值和结果,以便您可以检查插值是否合理,但如果您只需要插值结果,则可以使用 gerMonthly.

The DATE column needs to be in Date format so the interpolation can work with numeric values. I've usually used "natural" cubic splines but other options are available. This format shows both the input values and the results so that you can check that the interpolation looks reasonable but you can use gerMonthly if you just want the interpolated results.

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

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