R中的插值 [英] Interpolation in R
本文介绍了R中的插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有每小时的时间序列,并且希望每15分钟插入一次每小时的值.线性插值即可.但是,如果有什么方法可以指定高斯多项式,那就太好了.
例如,如果我有
p <a <-c(4.5、7、3.3),这是前三个小时的数据.在这种情况下,如何获取15分钟的亚小时数据,总共9个值?我一直在使用近似函数并研究Zoo软件包,但仍然不知道该怎么做.非常感谢你!解决方案
在 xts
包中,您可以 na.approx
或 na.spline 代码>.
- 强迫您将序列乘到
xts
对象 - 创建间隔为15分钟的新索引
- 使用此新索引创建与您的对象合并的NULL xts对象
- 使用
na.approx
线性/常数近似值或na.spline
多项式1近似丢失值.
这里是一个完整的示例:
库(xts)set.seed(21)##您创建了xts对象x<-xts(rnorm(10),seq(from = as.POSIXct(Sys.Date()),length.out = 10,by = as.difftime(1,units ='hours')))##要使用的新索引新索引<-seq(min(index(x)),max(index(x)),by = as.difftime(15,units ='mins'))##线性近似na.approx(合并(x,xts(NULL,new.index)))##多项式约na.spline(合并(x,xts(NULL,new.index)))
I have hourly time series and would like to interpolate sub-hourly values like every 15 min. Linear interpolation will do. But if there is any way to specify Gaussian, Polynomial, that would be great.
For example if I have
a<-c(4.5,7,3.3) which is the first three hour data. How can I get 15 min sub-hourly data, total of 9 values in this case? I have been using approx function and studying zoo package and still don't know how I can do it. Thank you very much!
解决方案
Within xts
package, you can either na.approx
or na.spline
.
- Coerce you times series to an
xts
object - Create a new index having 15 minutes intervals
- Use this new index to create a NULL xts object that you merge with your object
- Approximate missing values using
na.approx
for linear/constant approx orna.spline
for polynomial one.
here a complete example:
library(xts)
set.seed(21)
## you create the xts object
x <- xts(rnorm(10),
seq(from=as.POSIXct(Sys.Date()),
length.out=10,
by=as.difftime(1,units='hours')))
## new index to be used
new.index <-
seq(min(index(x)),max(index(x)), by=as.difftime(15,units='mins'))
## linear approx
na.approx(merge(x,xts(NULL,new.index)))
## polynomial approx
na.spline(merge(x,xts(NULL,new.index)))
这篇关于R中的插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文