【问题标题】:Kmeans cluster back into original data set which included missing valuesKmeans 聚类回包含缺失值的原始数据集
【发布时间】:2019-02-27 18:51:45
【问题描述】:

我一直在处理具有缺失变量的数据集:

> dt %>%
+     as_tibble() 
# A tibble: 652 x 5
   z_pos_2grp z_neg_2grp z_dis_2grp z_iq_2grp condit
        <dbl>      <dbl>      <dbl>     <dbl> <chr> 
 1    NA          NA         NA        NA     2     
 2    NA          NA         NA        NA     2     
 3    -0.0828      0.328     -0.473    -1.23  1     
 4     0.260       0.328     -0.222    -0.441 1     
 5    NA          NA         NA        NA     2     
 6    -0.769      -0.943     -0.725     0.607 3     
 7    NA          NA         NA        NA     NA    
 8    NA          NA         NA        NA     5     
 9    -0.769      -0.943     -0.473     2.44  3     
10    -0.769      -0.943     -0.725     0.607 3 

我想在此数据中执行无监督聚类,这意味着我需要隔离我对聚类感兴趣的变量(z_pos_2grp、z_neg_2grp、z_dis_2grp、z_iq_2grp)并移除 NA。于是我表演了:

dt %>%
    select(z_pos_2grp, z_neg_2grp, z_dis_2grp, z_iq_2grp) %>%
    filter(!is.na(z_pos_2grp) & !is.na(z_neg_2grp) & !is.na(z_dis_2grp) & !is.na(z_iq_2grp)) %>%
    do(augment(kmeans(cbind(.$z_pos_2grp, .$z_neg_2grp, .$z_dis_2grp, .$z_iq_2grp), nstart = 1000, centers = 3),.))

这会生成所需的结果,使用 kmeans 集群创建一个新列,但是我想将此列扩展回原始数据集。这样簇号就在正确的行中,而 NA 则没有簇号的值。

  • 如何实现?

更新

按照 cmets 中的建议,我创建了一个 rowid 变量并将子集 left_join 到原始变量中。但是,这会产生一个问题,我需要将 rowid 变量传递给 kmeans 过程,这并不理想。如何解决?也许有一个纯粹的 dplyr 管道解决方案。见下方代码:

ided <- dt %>%
    as_tibble() %>%
    tibble::rowid_to_column("rowid")

with_clusters<- ided %>%
    as_tibble() %>%
    select(rowid, z_pos_2grp, z_neg_2grp, z_dis_2grp, z_iq_2grp) %>%
    filter(!is.na(z_pos_2grp) & !is.na(z_neg_2grp) & !is.na(z_dis_2grp) & !is.na(z_iq_2grp)) %>%
    do(augment(kmeans(cbind(.$z_pos_2grp, .$z_neg_2grp, .$z_dis_2grp, .$z_iq_2grp), nstart = 1000, centers = 3),.))

【问题讨论】:

  • 我会为您的整个表创建一个行 ID,在数据子集上生成您的集群,并使用行 ID 将子集left join 恢复为原始数据。
  • rowid_to_column() from tibble 让您轻松生成该 ID
  • 谢谢,这会产生问题,因为我必须将 rowid 变量包含到 kmeans 聚类过程中,见上文。
  • 在下面查看我的答案。

标签: r dplyr


【解决方案1】:

dplyr 提供了一些不错的语义功能,但我不觉得有必要做一些不必要的复杂的事情。这是一个完全依赖基本功能的解决方案。如有必要,您可以翻译回dplyr 语法:

#make some random data
x <- data.frame(x1 = rnorm(10), x2 = rnorm(10), x3 = rnorm(10))
#set some NAs in 1st, 7th, and 10th row
x[1,1] <- NA
x[7,2] <- NA
x[10,3] <- NA
#add an ID
x$id <- 1:nrow(x)

#subset of the rows that aren't missing any data
x_complete <- x[complete.cases(x), ]
#run kmeans cluster
set.seed(1)
x_complete$cluster <- kmeans(x_complete[, c("x1", "x2", "x3")], centers = 2)$cluster
#merge back
x_out <- merge(x, x_complete[, c("id", "cluster")], by = "id", all.x = TRUE)
x_out
#>    id          x1         x2         x3 cluster
#> 1   1          NA -0.2504685 -1.8068897      NA
#> 2   2  0.33502109  0.8645490 -0.1011722       1
#> 3   3  0.59668757  0.4612197  0.5527592       1
#> 4   4  0.14814590  0.4826441  0.6722468       1
#> 5   5 -0.52928021  0.9350622 -0.4802504       1
#> 6   6 -0.15841780  0.3299728  1.3266988       2
#> 7   7  0.20500487         NA  0.9150747      NA
#> 8   8 -0.15215172 -0.7769925  1.4218571       2
#> 9   9  0.04083744 -0.4304241  1.7355289       2
#> 10 10 -0.22238132 -1.3202234         NA      NA

reprex package (v0.2.1) 于 2019 年 2 月 27 日创建

【讨论】:

    猜你喜欢
    • 2019-01-23
    • 2015-02-17
    • 2018-04-26
    • 1970-01-01
    • 2020-05-18
    • 1970-01-01
    • 1970-01-01
    • 2017-08-08
    • 2021-10-09
    相关资源
    最近更新 更多