我对将我拥有的时间序列数据聚类到6个组中很感兴趣。我拥有的数据将每行表示为单个时间序列数据,就像这样,我有大约800到1000个时间序列数据。但是每个时间序列数据都有不同的长度,例如,时间序列数据“1”有102个值,时间序列数据“2”有56个值,时间序列数据“3”有180个值,以此类推。我在excel中的样本数据如下所示:
A B C D E F G H I J K L M N O P Q R S T
1 7.4 8.1 8.5 9.1 9.6 10.2 10.7 11.3 11.9
2 7.3 7.6 7.9 8.2 8.5 8.8 9.1 9.4 9.7 10.1 10.4 10.7 11.5
3 7.6 8.1 8.6 9.1 9.6 10.2 10.7 11.8
4 7.4 7.8 8.4 8.9 9.4 10.0 10.5 11.1 11.6 12.3 12.8 13.4 13.5 13.9 14.4 14.9 15.4我不知道如何处理不等长的时间序列数据以进行聚类
如何计算两个时间序列数据之间的dtw。
对于具有相同长度的时间序列数据,它使用如下代码
library(dtw)
dm <- dist(sample1, method= "DTW")
hc <- hclust(dm, method="average")
plot(hc, labels=Labels,
cex = 0.5,
hang = -1,
col = 'blue',
main="cluster dendrogram")
rect.hclust(hc, k = 6) # displays the groups in the plot有没有办法把我所有的时间序列都做成等长的。因为如果时间序列数据的长度不相等,则无法计算欧几里德距离/ dtw、计算任何其他特征等。
我正在寻找详细的代码和方法来处理这类数据
提前感谢
发布于 2019-10-01 23:04:03
使用末尾注释中重复显示的数据,我们可以将每条曲线拟合为一条直线,然后对坡度进行聚类。
library(Ckmeans.1d.dp) # univariate clustering package
slopes <- coef(lm(t(DF) ~ seq_along(DF)))[2, ]
fm <- Ckmeans.1d.dp(slopes)
# graph the slopes on X axis identifying each and
# coloring each cluster with a different color
plot(fm)
text(slopes, 1, 1:4, adj = 0:-1)打印后继续

library(zoo)
# plot each series with each cluster having a different color
plot(zoo(t(DF)), screen = 1, col = fm$cluster)打印后继续

备注
以可复制形式输入:
Lines <- "row A B C D E F G H I J K L M N O P Q R S T
1 7.4 8.1 8.5 9.1 9.6 10.2 10.7 11.3 11.9
2 7.3 7.6 7.9 8.2 8.5 8.8 9.1 9.4 9.7 10.1 10.4 10.7 11.5
3 7.6 8.1 8.6 9.1 9.6 10.2 10.7 11.8
4 7.4 7.8 8.4 8.9 9.4 10.0 10.5 11.1 11.6 12.3 12.8 13.4 13.5 13.9 14.4 14.9 15.4"
DF <- read.table(text = Lines, header = TRUE, fill = TRUE)[-1]发布于 2019-10-01 22:59:35
我不太了解如何对时间序列进行聚类,但我尝试了OP的例子中提供的不等长时间序列的方法,它似乎工作得很好……它应该:根据dtw包的作者的说法,
该函数执行动态时间扭曲(DTW),并计算两个时间序列x和y之间的最佳对齐,给定为数值向量。“最佳”对齐可以最小化对齐元素之间的距离总和。X和y的长度可以不同于。
首先加载库
library(dtw); library(ggplot2)然后创建时间序列
xlist <- list(x1 = c(7.4, 8.1, 8.5, 9.1, 9.6, 10.2, 10.7, 11.3, 11.9)
x2 = c(7.3, 7.6, 7.9, 8.2, 8.5, 8.8, 9.1, 9.4, 9.7, 10.1,
10.4, 10.7, 11.5)
x3 = c(7.6, 8.1, 8.6, 9.1, 9.6, 10.2, 10.7, 11.8)
x4 = c(7.4, 7.8, 8.4, 8.9, 9.4, 10, 10.5, 11.1, 11.6, 12.3,
12.8, 13.4, 13.5, 13.9, 14.4, 14.9, 15.4))..。看起来像这样
df <- stack(xlist)
df$i <- unlist(lapply(xlist, seq_along))
ggplot(df, aes(i, values, color = ind)) + geom_point() +
geom_line() + theme_bw() + facet_wrap(.~ind, nrow = 2) +
labs(x="", y="", color = "Time series")

与OP在等长时间序列上使用的代码相同的代码似乎工作得很好。
dm <- dist(xlist, method= "DTW")
hc <- hclust(dm, method="average")
plot(as.dendrogram(hc), hang = -1)
rect.hclust(hc, k = 2)

发布于 2019-10-01 23:32:53
您可以潜在地尝试发现时间序列中的主题,作为查找相似性的一种方法,即查找时间序列中相似的较短片段,而不是基于整个时间序列长度的相似性。然而,如果没有对领域的一些洞察,手头的问题等等,就很难确定这是否合适。
基于Symbolic Aggregate approXimation (SAX)的集群算法可以做到这一点。
https://stackoverflow.com/questions/58187025
复制相似问题