r - 在时间序列中插值数据点
问题描述
我正在学习功能数据分析。但是每个数据的长度是不同的。
有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
解决方案
推荐阅读
- docker - 使用 ABP 模块模板问题运行 Docker
- flutter - 是否可以在颤动中创建多个具有不同状态栏颜色的底部栏?我想要白色和深色状态栏
- asp.net-core - 我想通过 .Net 核心控制台或 Web API 在 SharePoint 列表中创建项目
- sql - SQL,查询应显示大于 2 的数字
- npm - 如何在 gitlab CI 中设置 npm 版本?
- tensorflow - 加载 Tensorflow 数据集进行训练时,何时使用 .repeat?
- java - 如何使用使用 Java 11 的最新 SonarQube 免费版分析 Java 8 代码?
- java - 如何根据模式替换任意位置的任意字符
- python - python复制excel数据
- ros - 用于传感器融合(IMU 和压力)数据的 Ros 包?