首页 > 解决方案 > 在时间序列中插值数据点

问题描述

我正在学习功能数据分析。但是每个数据的长度是不同的。
有50个观察。而这些观测有150~200的长度。
所以我使用这种方法,但我想知道更高级别的方法。
这是我的方法。
“创建一个函数,将两点连接在一条直线上,并使用直线函数添加点。”
我的方法并不完美。y 值的结果中有 NA。
我认为可以使用样条曲线代替直线来添加数据点。
或者你能用另一种天才的方式帮助我吗?我会很感激的。

# This is just example. This is temperature data. Original data have 310~370 lengths.
ex[[1]] <- c(12.2, 12.1, 12.1, 12.0, 11.9, 11.8, 11.8, 11.9, 11.8, 11.9, 12.0, 11.9)
ex[[2]] <- c(12.3, 13.4, 13.3, 13.4, 13.3, 13.4, 13.2, 13.4, 13.5, 13.5)
ex[[3]] <- c(12.8, 12.8, 12.7, 12.7, 12.9, 12.5, 12.7, 12.6, 12.7, 13.0, 12.8, 12.7, 12.9, 12.0)

lengths(ex)
## V1 V2 V3 
## 12 10 14

RoundMidf <- function(tv, xv){ # tv : targe vector of list,  xv : reference vector
  x <- c(1:length(xv))*length(tv)/length(xv) # 
  x1 <- ifelse(x < 1, ceiling(x), floor(x))
  x2 <- ceiling(x)
  y1 <- tv[x1]
  y2 <- tv[x2]
  a <- (y1-y2)/(x1-x2) # a of y=ax+b
  b <- y1-a*x1 # b of y=ax+b
  y <- a*x+b # y of y=ax+b
  y[length(xv)] <- tv[length(tv)] # Last point doesn't have next point
  return(y)
}

newex <- as.list(NA)
newex[[1]] <- RoundMidf(ex[[1]], ex[[3]])
newex[[2]] <- RoundMidf(ex[[2]], ex[[3]])
newex[[3]] <- ex[[3]] # reference

# Compare the data lengths before and after
DT <- data.frame(lengths(ex),lengths(newex)); names(DT) <- c("Old", "New"); DT
##    Old New
## V1  12  14
## V2  10  14
## V3  14  14

标签: rtime-seriesinterpolation

解决方案


推荐阅读