简体   繁体   English

R-缺少数据的时间序列的插值

[英]R - Interpolation of time series with missing data

I have a database with time data, but for some timestamps there is no data available (NA in database). 我有一个包含时间数据的数据库,但是对于某些时间戳,没有可用的数据(数据库中不适用)。 I would like to do an interpolation for these values. 我想对这些值进行插值。

Dataset: 数据集:

 structure(list(timestamp = structure(1:7, .Label = c("21/01/2012 18:41", 
+ "21/01/2012 18:46", "21/01/2012 18:51", "21/01/2012 18:56", "21/01/2012 19:01", 
+ "21/01/2012 19:06", "21/01/2012 19:11"), class = "factor"), humid = c(47.7, 
+ 44.5, NA, 42.5, 42.5, NA, 41.6), temp = c(14.12, 15.37, NA, 16.17, 
+ 16.31, NA, 16.51)), .Names = c("timestamp", "humid", "temp"), class = "data.frame", row.names = c(NA, 
+ -7L))

Which looks like this: 看起来像这样:

         timestamp              humid               temp
1 21/01/2012 18:41 47.700000000000003 14.119999999999999
2 21/01/2012 18:46 44.500000000000000 15.369999999999999
3 21/01/2012 18:51                 NA                 NA
4 21/01/2012 18:56 42.500000000000000 16.170000000000002
5 21/01/2012 19:01 42.500000000000000 16.309999999999999
6 21/01/2012 19:06                 NA                 NA
7 21/01/2012 19:11 41.600000000000001 16.510000000000002

I already tried the option A: 我已经尝试了选项A:

library(zoo)
Mz <- zoo(TEST)
index(Mz) <- Mz[,1]
Mz_approx <- na.approx(Mz, x=Mz$timestamp)

But this results in following errors: 但这会导致以下错误:

Error in approx(x[!na], y[!na], xout, ...) : 
  need at least two non-NA values to interpolate
In addition: Warning messages:
1: In na.approx.default(object, x = x, xout = xout, na.rm = FALSE,  :
  NAs introduced by coercion
2: In na.approx.default(object, x = x, xout = xout, na.rm = FALSE,  :
  NAs introduced by coercion
3: In xy.coords(x, y) : NAs introduced by coercion

I also tried option B: 我也尝试了选项B:

library(zoo)
Mz <- zoo(TEST)
Mz_approx <- na.approx(Mz)

But this results in the following errors: 但这导致以下错误:

Error in approx(x[!na], y[!na], xout, ...) : 
  need at least two non-NA values to interpolate
In addition: Warning message:
In xy.coords(x, y) : NAs introduced by coercion

What is the best way to overcome these errors and to use the function na.approx correctly? 克服这些错误并正确使用na.approx函数的最佳方法是什么?

read.zoo will convert it to zoo handling the index properly and then na.approx can be used. read.zoo会将其转换为正确处理索引的zoo,然后可以使用na.approx There are several vignettes (pdf manuals) that come with zoo including an entire manual just devoted to read.zoo examples and there are many examples in the zoo help files that you can go through. zoo read.zoo插图(pdf手册),包括仅专门read.zoo示例的完整手册,并且Zoo帮助文件中有许多示例可供您浏览。

library(zoo)

z <- read.zoo(TEST, tz = "", format = "%d/%m/%Y %H:%M")
na.approx(z)

giving: 赠送:

                    humid  temp
2012-01-21 18:41:00 47.70 14.12
2012-01-21 18:46:00 44.50 15.37
2012-01-21 18:51:00 43.50 15.77
2012-01-21 18:56:00 42.50 16.17
2012-01-21 19:01:00 42.50 16.31
2012-01-21 19:06:00 42.05 16.41
2012-01-21 19:11:00 41.60 16.51

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM