Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为hclust位于R中的时间序列数据的每个观测值分配群集编号_R_Dplyr_Time Series_Hierarchical Clustering - Fatal编程技术网

为hclust位于R中的时间序列数据的每个观测值分配群集编号

为hclust位于R中的时间序列数据的每个观测值分配群集编号,r,dplyr,time-series,hierarchical-clustering,R,Dplyr,Time Series,Hierarchical Clustering,我有一个时间序列数据,大约5年内有4个变量。我想使用R中的hclust方法对数据进行聚类。我想对观察值进行聚类。我的代码有效。然而,我想为每项观察确定具体的聚类。也就是说,我想在每次观测旁边加上集群的数量。我的代码给了我一个错误。我理解这个错误。那么,我有什么办法可以达到我的目的呢 以下是我的尝试: library(TSclust) library(cluster) # clustering algorithms library(tseries) library(zoo) library(

我有一个时间序列数据,大约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

我想问题是您的
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
编辑

如果需要,您可以使用hclust on after log和dif尝试此操作:

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()

res非常感谢您,它很有效。但是,我不小心没有添加日志数据,这导致了一个错误<代码>错误:mutate()`input
cluster
有问题。x输入
群集
无法回收到948大小。我输入的
集群
sub\u grp
。如果您愿意帮助我,我输入的
cluster
大小必须是948或1,而不是949`。这将是一个非常感谢的帮助。非常感谢你。我不知道该如何感谢你。我已经编辑了我的问题并添加了新数据。我在sub_grp之前做了日志。哦,我明白了,问题是你已经应用了diff,它删除了一行(在本例中),然后你想“粘贴”sub_grp到它。它不会工作,因为一个是948长度,另一个是949(这是错误)。真正的问题是:你所做的是正确的,将簇附加到差分而不是值上吗?我没有看到一个聪明的方法来选择如何切割聚类向量。将簇向量附加到差分上,而不是值,但已根据值进行计算。
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()