我正在寻找一种将不同长度的时间序列转换为唯一长度的方法。 我认为这个问题已经被我无法找到了。我想我只是没有使用正确的词汇来解决这个问题。
数据1:20个变量x 250个观测值(时间点)
数据2:20个变量x 50个观测值(时间点)
我想将这些数据转换为100个观察值,同时保持两个案例中20个变量的曲线形状。
非常感谢
答案 0 :(得分:2)
示例数据
set.seed(123)
data <- matrix(0, 250, 20)
data[1, ] <- rnorm(20)
for (i in 2:nrow(data)) {
data[i, ] <- data[i - 1, ] + rnorm(20, 0, 0.02)
}
rownames(data) <- 0:249
处理此问题的一种方法是使用reshape2
和dplyr
:
library("reshape2")
library("dplyr")
library("ggplot2")
molten <- melt(data, varnames = c("Time", "Variable"))
原始数据图:
ggplot(molten, aes(x = Time, y = value, colour = factor(Variable))) + geom_line()
现在使用每个时间段的值将data.frame
减少5倍:
shorter <- molten %>%
group_by(Variable, Time %/% 5) %>%
summarise(value = mean(value), Time = mean(Time))
绘制新数据:
ggplot(shorter, aes(x = Time, y = value, colour = factor(Variable))) + geom_line()
如果您想要原始的广泛数据:
shorterWide <- acast(shorter, Time ~ Variable)
答案 1 :(得分:0)
我想我找到了一种使用此功能的方法 Basic two-dimensional cubic spline fitting in R
我猜我丢失的关键字是三次样条曲线。
在我的情况下,我想做类似的事情
样条曲线(Data1,n = 100) 样条(Data1,n = 100)