R 错误中的 K-Means 聚类

K-Means clustering in R error

我有一个用 R 创建的数据集。它的结构如下:

> head(btc_data)
           Date btc_close eth_close vix_close gold_close DEXCHUS change
1647 2010-07-18      0.09        NA        NA         NA      NA      0
1648 2010-07-19      0.08        NA     25.97    115.730      NA     -1
1649 2010-07-20      0.07        NA     23.93    116.650      NA     -1
1650 2010-07-21      0.08        NA     25.64    115.850      NA      1
1651 2010-07-22      0.05        NA     24.63    116.863      NA     -1
1652 2010-07-23      0.06        NA     23.47    116.090      NA      1

我正在尝试使用 k-means 对观察结果进行聚类。但是,我收到以下错误消息:

> km <- kmeans(trainingDS, 3)
Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)
In addition: Warning message:
In storage.mode(x) <- "double" : NAs introduced by coercion 

这是什么意思?我是否错误地预处理了数据?我能做些什么来修复它?我不能放弃 NA,因为在 4500 个初始观察中,如果我 运行 complete cases 我只剩下 100 个观察。

基本上我希望基于 change 列的值为 -1,0,1 形成 3 个簇。然后我希望分析每个集群的组成部分以找到最强的变化预测因子。还有哪些其他算法对执行此操作最有用?

我也尝试使用以下代码删除所有 NA 值,但我仍然收到相同的错误消息:

> complete_cases <- btc_data[complete.cases(btc_data), ]
> km <- kmeans(complete_cases, 3, nstart = 20)
Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)
In addition: Warning message:
In storage.mode(x) <- "double" : NAs introduced by coercion

> sum(!sapply(btc_data, is.finite)) 
[1] 8008
> sum(sapply(btc_data, is.nan))
[1] 0
> 
> sum(!sapply(complete_cases, is.finite)) 
[1] 0
> sum(sapply(complete_cases, is.nan))
[1] 0

数据格式如下:

> sapply(btc_data, class)
      Date  btc_close  eth_close  vix_close gold_close    DEXCHUS     change 
    "Date"  "numeric"  "numeric"  "numeric"  "numeric"  "numeric"   "factor" 

收到此错误消息的原因有多种,特别是存在无效数据类型(NA、NaN、Inf)或日期时。让我们来看看它们:

但首先,让我们检查它是否适用于 mtcars 数据集,因为我将使用它:

kmeans(mtcars, 3)
K-means clustering with 3 clusters of sizes 9, 7, 16
--- lengthy output omitted

可能的问题 1:无效的数据类型NA/NaN/Inf

df <- mtcars
df[1,1] <- NA
kmeans(df, 3)
Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)

df[1,1] <- Inf
kmeans(df, 3)
Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)

df[1,1] <- NaN
kmeans(df, 3)
Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)

您可以使用以下方法检查这些值:

df[1:3,1] <- c(NA, Inf, NaN) # one NA, one Inf, one NaN
sum(sapply(df, is.na))
[1] 2
sum(sapply(df, is.infinite))
[1] 1
sum(sapply(df, is.nan))
[1] 1

为了摆脱这些,我们可以删除相应的观察结果。但请注意 complete.cases 不会删除 Inf:

complete_df <- df[complete.cases(df),]
sum(sapply(complete_df, is.infinite))
[1] 1

而是使用例如

df[apply(sapply(df, is.finite), 1, all),]

您也可以重新分配这些值或估算它们,但这是一个完全不同的过程。

可能问题二:日期: 见以下内容:

library(lubridate)
df <- mtcars
df$date <- seq.Date(from=ymd("1990-01-01"), length.out = nrow(df), by=1)
kmeans(df, 3)
Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)
In addition: Warning message:
In kmeans(df, 3) : NAs introduced by coercion

您可以通过排除日期或将日期转换为其他日期来解决此问题,例如

df$newdate <- seq_along(df$date)
df$date <- NULL
kmeans(df, 3)
K-means clustering with 3 clusters of sizes 9, 7, 16
---- lengthy output omitted

或者您可以在将日期传递给 kmeans 之前尝试将日期强制为数字:

df <- mtcars
df$date <- seq.Date(from=ymd("1990-01-01"), length.out = nrow(df), by=1)
df$date <- as.numeric(df$date)
kmeans(df, 3)
K-means clustering with 3 clusters of sizes 9, 16, 7
--- lengthy output omitted

您是否在聚类中使用了 "Date" 列?

在使用 k 均值聚类时应使用数字类型数据。

试试这个,

btc_data$Date = as.numeric(gsub("-", "", as.character(btc_data$Date)))

检查您要聚类的变量的数据类型。如果数据类型是非数字的,很可能会出现错误。在聚类之前也尝试正确处理日期格式。