每季度推介/延期至每月 [英] Interpolate / Extend quarterly to monthly series

查看:157
本文介绍了每季度推介/延期至每月的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含季度观察结果的data.frame。我现在想插入月度值(优选的立方,线性很好)。中间目标应该是创建一个具有 DATE 的数据框架作为索引,并且缺少所有月度观察值。



Google搜索显示,我应该在整个时间范围内创建一个空的data.frame,然后合并 - 但是迄今为止我曾尝试过给了我错误。这是我的程序但是,由于我是一个新客户 r ,我可以接受任何有关变更的建议。

 > str(ger)
'data.frame':93 obs。的2个变量:
$ DATE:日期格式:1991-01-011991-04-011991-07-011991-10-01...
$ VALUE:num 470780 468834 466332 472949 480359 ...
>头(ger)
日期价值
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)

这是我最接近的,但它给我一个不需要的列格式 - 肯定是一个更清洁的方式来获得我想要的?最后,给定格式,获得内插时间序列的最干净的方法是什么?

  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


解决方案

我不清楚您对不需要的列格式的评论,但是如果您想使用三次插值来获得内插值,则可以考虑下列代码: / p>

  ger<  -  data.frame(DATE = as.Date(c(1991-01-01, 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个月)
gerMonthly< - data.frame(DATE = DateSeq, Interp.Value = spline(ger,method =natural,xout = DateSeq)$ y)
merge(ger,gerMonthly,by ='DATE',all.y = T)

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


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.

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)

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天全站免登陆