使用 R 中的 hclust 为时间序列数据的每个观察分配簇号
Assign the cluster number for each observation for time series data with hclust in R
我有一个时间序列数据,其中包含大约 5 年的 4 个变量。我想在 R 中使用 hclust
方法对数据进行聚类。我想对观察结果进行聚类。我的代码有效。但是,我想为每个观察确定特定的集群。也就是说,我想在每个观察值旁边添加集群的数量。我的代码给了我一个错误。我明白这个错误。那么有什么办法可以实现我的观点。
这是我的尝试:
library(TSclust)
library(cluster) # clustering algorithms
library(tseries)
library(zoo)
library(dtw)
library(dtwclust)
library(dplyr)
##Load the data
data("EuStockMarkets")
##Save the data
dat <- EuStockMarkets
res <- lapply(split(as.zoo(EuStockMarkets), as.integer(time(EuStockMarkets))), as.ts)
## Re-define the data
datNew <- ts(rbind(res$`1995`,res$`1996`,res$`1997`, res$`1998`))
d <- dist(datNew, method = "DTW")
hc1 <- hclust(d, method = "average" )
sub_grp <- cutree(hc1, k = 4)
table(sub_grp)
datNew%>%
mutate(cluster = sub_grp) %>%
head
它 returns 我出错了:
Error in UseMethod("mutate_") :
no applicable method for 'mutate_' applied to an object of class "c('mts', 'ts', 'matrix')"
In addition: Warning message:
`mutate_()` is deprecated as of dplyr 0.7.0.
Please use `mutate()` instead.
See vignette('programming') for more help
我想问题是你的 datNew
不是 data.frame
。正在查看:
class(datNew)
[1] "mts" "ts" "matrix"
这就是你的错误。如果你把它写成 data.frame
:
library(dplyr)
data.frame(timeseries=as.matrix(datNew), date=time(datNew))%>%
# use mutate() instead of mutate_()
mutate(cluster = sub_grp) %>%
head()
应该可以,希望这是您需要的结果。
timeseries.DAX timeseries.SMI timeseries.CAC timeseries.FTSE date cluster
1 2110.77 2673.5 1956.0 3083.4 1 1
2 2097.34 2656.2 1927.8 3095.8 2 1
3 2074.68 2628.8 1894.2 3065.6 3 1
4 2097.51 2628.8 1881.2 3065.5 4 1
5 2079.19 2628.8 1881.2 3065.5 5 1
6 2068.92 2612.3 1885.9 3065.7 6 1
编辑
如果需要,您可以在登录和 dif 后使用 hclust on 尝试此操作:
res <- lapply(split(as.zoo(EuStockMarkets), as.integer(time(EuStockMarkets))), as.ts)
datNew <- ts(rbind(res$`1995`,res$`1996`,res$`1997`, res$`1998`))
dat.log <- log(datNew)
dat.diff <- diff(dat.log)
Logreturns <- dat.diff
# using a different dist, due an error, the idea is the same
d <- dist(Logreturns)
hc1 <- hclust(d, method = "average" )
sub_grp <- cutree(hc1, k = 4)
data.frame(timeseries=as.matrix(Logreturns), date=time(Logreturns))%>%
mutate(cluster = sub_grp) %>%
head()
我有一个时间序列数据,其中包含大约 5 年的 4 个变量。我想在 R 中使用 hclust
方法对数据进行聚类。我想对观察结果进行聚类。我的代码有效。但是,我想为每个观察确定特定的集群。也就是说,我想在每个观察值旁边添加集群的数量。我的代码给了我一个错误。我明白这个错误。那么有什么办法可以实现我的观点。
这是我的尝试:
library(TSclust)
library(cluster) # clustering algorithms
library(tseries)
library(zoo)
library(dtw)
library(dtwclust)
library(dplyr)
##Load the data
data("EuStockMarkets")
##Save the data
dat <- EuStockMarkets
res <- lapply(split(as.zoo(EuStockMarkets), as.integer(time(EuStockMarkets))), as.ts)
## Re-define the data
datNew <- ts(rbind(res$`1995`,res$`1996`,res$`1997`, res$`1998`))
d <- dist(datNew, method = "DTW")
hc1 <- hclust(d, method = "average" )
sub_grp <- cutree(hc1, k = 4)
table(sub_grp)
datNew%>%
mutate(cluster = sub_grp) %>%
head
它 returns 我出错了:
Error in UseMethod("mutate_") :
no applicable method for 'mutate_' applied to an object of class "c('mts', 'ts', 'matrix')"
In addition: Warning message:
`mutate_()` is deprecated as of dplyr 0.7.0.
Please use `mutate()` instead.
See vignette('programming') for more help
我想问题是你的 datNew
不是 data.frame
。正在查看:
class(datNew)
[1] "mts" "ts" "matrix"
这就是你的错误。如果你把它写成 data.frame
:
library(dplyr)
data.frame(timeseries=as.matrix(datNew), date=time(datNew))%>%
# use mutate() instead of mutate_()
mutate(cluster = sub_grp) %>%
head()
应该可以,希望这是您需要的结果。
timeseries.DAX timeseries.SMI timeseries.CAC timeseries.FTSE date cluster
1 2110.77 2673.5 1956.0 3083.4 1 1
2 2097.34 2656.2 1927.8 3095.8 2 1
3 2074.68 2628.8 1894.2 3065.6 3 1
4 2097.51 2628.8 1881.2 3065.5 4 1
5 2079.19 2628.8 1881.2 3065.5 5 1
6 2068.92 2612.3 1885.9 3065.7 6 1
编辑
如果需要,您可以在登录和 dif 后使用 hclust on 尝试此操作:
res <- lapply(split(as.zoo(EuStockMarkets), as.integer(time(EuStockMarkets))), as.ts)
datNew <- ts(rbind(res$`1995`,res$`1996`,res$`1997`, res$`1998`))
dat.log <- log(datNew)
dat.diff <- diff(dat.log)
Logreturns <- dat.diff
# using a different dist, due an error, the idea is the same
d <- dist(Logreturns)
hc1 <- hclust(d, method = "average" )
sub_grp <- cutree(hc1, k = 4)
data.frame(timeseries=as.matrix(Logreturns), date=time(Logreturns))%>%
mutate(cluster = sub_grp) %>%
head()