R 如何在集群中进行集群
我在地图上有一组点,每个点都有一个给定的参数值。我想:R 如何在集群中进行集群,r,dplyr,cluster-analysis,apply,hierarchical-clustering,R,Dplyr,Cluster Analysis,Apply,Hierarchical Clustering,我在地图上有一组点,每个点都有一个给定的参数值。我想: 在空间上对它们进行聚类,并忽略小于 10分。My df应该为每个点所属的集群设置一列(Clust)[完成] 对每个簇内的参数值进行子簇;在我的df(子聚类)中添加一列,用于按子聚类对每个点进行分类 我不知道如何做第二部分,除了可能与循环 图中显示了一组空间分布的点(左上角),这些点按聚类进行颜色编码,并按右上角绘图中的参数值进行排序。底部一行显示具有>10个点的簇(左)和按参数值排序的每个簇的镶嵌面(右)。正是这些方面,我希望能够根据最小簇
sub_df <- data.frame()
for (i in unique(xy_df_filt$Clust)) {
temp_df <- xy_df_filt %>% dplyr::filter(Clust == i)
# Cluster data by (X,Y) location
a_d = 1
a_chc <- hclust(dist(temp_df$Test_Param), method="single")
# Distance with a d threshold - used d=40 at one time but that changes...
a_chc.d40 <- cutree(a_chc, h=a_d)
# max(chc.d40)
# Join results to main df
sub_df <- bind_rows(sub_df, data.frame(temp_df, subClust=a_chc.d40)) %>% dplyr::select(ID, subClust)
}
xy_df_filt_2 <- left_join(xy_df_filt,sub_df, by=c("ID"="ID"))
p4 <- xy_df_filt_2 %>% dplyr::arrange(Test_Param) %>%
ggplot() +
geom_point(aes(x=1:length(Test_Param),y=Test_Param, colour = subClust)) +
scale_colour_gradient(low="red", high="green") +
facet_wrap(~Clust, scales="free")
grid.arrange(p1, p2, p3, p4, ncol=2, nrow=2)
sub_df您可以为您的子集群执行此操作
xy_df_filt_2 <- xy_df_filt %>%
group_by(Clust) %>%
mutate(subClust = tibble(Test_Param) %>%
dist() %>%
hclust(method="single") %>%
cutree(h=1))
xy\u df\u过滤器2%
分组依据(Clust)%>%
突变(子聚类=TIBLE(测试参数)%>%
dist()%>%
hclust(method=“single”)%%>%
割树(h=1))
嵌套管道很好。我认为您的版本的问题在于您没有将正确类型的对象传递给dist
。
如果只将一列传递给dist
,则不需要使用tibble
术语,但我已将其保留,以防您希望像主集群那样使用多个列
你可以使用相同的公式,但没有分组依据
,从df\u ex
计算xy\u df
,应该有一种方法,使用do
和tidy
的组合来完成,但我总是很难用do
让事情按照我想要的方式排列。相反,我通常所做的是从基本R组合split
,从purr
组合map\u dfr
split
将按Clust
拆分数据帧,并为您提供一个数据帧列表,然后您可以映射这些数据帧map_dfr
映射每个数据帧并返回单个数据帧
我从你的xy_df_filt
开始,生成了我认为应该与你从for循环得到的xy_df_filt_2
相同的内容。我画了两个图,尽管这两组簇有点难以看到
xy\u df\u过滤器2%
拆分(.$Clust)%>%
map_dfr(功能(df){
子簇%cutree(,h=1)
绑定列(df,子集群=子集群)
})
ggplot(xy_df_filt_2,aes(x=x,y=y,颜色=as.factor(子簇),形状=as.factor(Clust)))+
几何点()+
比例、颜色、制浆机(调色板=“设置2”)
刻面更清晰
ggplot(xy_df_filt_2,aes(x=x,y=y,颜色=as.factor(子簇),形状=as.factor(Clust)))+
几何点()+
比例、颜色、制浆机(调色板=“设置2”)+
面_包裹(~Clust)
由(v0.2.0)于2018年4月14日创建。我认为这也是一个很好的答案-使用我不熟悉的工具。谢谢。请看我在安德鲁的回答中的评论;您的方法不会生成警告,而his会生成警告。您的tibble(x,y)应该改为tibble(Test_Param)才是正确的,因为第二个聚类基于Test_Param距离,而不是x,y。但是你的方法有效。ThxYes,当然-很抱歉。我已经修改了答案。当我运行这段代码时,我收到了一系列警告(mutate_impl(.data,dots)中的警告:绑定字符和因子向量,强制转换为字符向量),似乎与此问题有关(),但我无法解决它;我想使用factor()或as.factor()将subClust转换为factor,我想知道tibble()是否会碍事。卡米尔的回答没有这个问题。@val是的,这(我认为)只是一个指示,mutate
必须添加因子级别,这是通过转换为字符来实现的。这只是一个警告——我对其他事情也有过类似的经历,但这并不一定意味着计算不起作用。
sub_df <- data.frame()
for (i in unique(xy_df_filt$Clust)) {
temp_df <- xy_df_filt %>% dplyr::filter(Clust == i)
# Cluster data by (X,Y) location
a_d = 1
a_chc <- hclust(dist(temp_df$Test_Param), method="single")
# Distance with a d threshold - used d=40 at one time but that changes...
a_chc.d40 <- cutree(a_chc, h=a_d)
# max(chc.d40)
# Join results to main df
sub_df <- bind_rows(sub_df, data.frame(temp_df, subClust=a_chc.d40)) %>% dplyr::select(ID, subClust)
}
xy_df_filt_2 <- left_join(xy_df_filt,sub_df, by=c("ID"="ID"))
p4 <- xy_df_filt_2 %>% dplyr::arrange(Test_Param) %>%
ggplot() +
geom_point(aes(x=1:length(Test_Param),y=Test_Param, colour = subClust)) +
scale_colour_gradient(low="red", high="green") +
facet_wrap(~Clust, scales="free")
grid.arrange(p1, p2, p3, p4, ncol=2, nrow=2)
xy_df_filt_2 <- xy_df_filt %>%
group_by(Clust) %>%
mutate(subClust = tibble(Test_Param) %>%
dist() %>%
hclust(method="single") %>%
cutree(h=1))