我有一个数据集,我必须从中删除异常值。我使用 boxplot 方法删除了我的异常值,但是我觉得这种方法已经将我的数据结构从类似表的结构更改为一个列表。我正在尝试使用 NbClust 来预测我应该使用的集群数量。在尝试使用 NbClust 之前,我还应用了 z-score 缩放。我真的是 R 的新手,我不知道如何将它改回来和/或这是否是 NbClust 发生错误的原因
在去除异常值之前,数据还显示为“846 个观测值,共 18 个变量” 删除离群值后到“18 个列表”(显示在全局环境面板中)
错误:t(jeu) %*% jeu 中的错误: 需要数字/复杂矩阵/向量参数
我认为正确的做法是将其更改为数据框,但我不太确定如何正确执行此操作。
可重现的例子
library(reshape2)
library(NbClust)
vehData <-
structure(
list(
Samples = 1:6,
Comp = c(95L, 91L, 104L, 93L, 85L,
107L),
Circ = c(48L, 41L, 50L, 41L, 44L, 57L),
D.Circ = c(83L,
84L, 106L, 82L, 70L, 106L),
Rad.Ra = c(178L, 141L, 209L, 159L,
205L, 172L),
Pr.Axis.Ra = c(72L, 57L, 66L, 63L, 103L, 50L),
Max.L.Ra = c(10L,
9L, 10L, 9L, 52L, 6L),
Scat.Ra = c(162L, 149L, 207L, 144L, 149L,
255L),
Elong = c(42L, 45L, 32L, 46L, 45L, 26L),
Pr.Axis.Rect = c(20L,
19L, 23L, 19L, 19L, 28L),
Max.L.Rect = c(159L, 143L, 158L, 143L,
144L, 169L),
Sc.Var.Maxis = c(176L, 170L, 223L, 160L, 241L, 280L),
Sc.Var.maxis = c(379L, 330L, 635L, 309L, 325L, 957L),
Ra.Gyr = c(184L,
158L, 220L, 127L, 188L, 264L),
Skew.Maxis = c(70L, 72L, 73L,
63L, 127L, 85L),
Skew.maxis = c(6L, 9L, 14L, 6L, 9L, 5L),
Kurt.maxis = c(16L,
14L, 9L, 10L, 11L, 9L),
Kurt.Maxis = c(187L, 189L, 188L, 199L,
180L, 181L),
Holl.Ra = c(197L, 199L, 196L, 207L, 183L, 183L),
Class = c("van", "van", "saab", "van", "bus", "bus")
),
row.names = c(NA,
6L), class = "data.frame")
#Remove outliers
removeOutliers <- function(data) {
OutVals <- boxplot(data)$out
remOutliers <- sapply(data, function(x) x[!x %in% OutVals])
return (remOutliers)
}
# Scale data -> same as scale() function
z_score <- function(x){
return ((x - mean(x))/sd(x))
}
vehDataRemove1 <- vehData[, -1]
vehDataRemove2 <- vehDataRemove1[,-19]
vehData <- vehDataRemove2
vehClass <- vehData$Class
#Begin removing outliers
removeOutliers1 <- removeOutliers(vehData)
removeOutliers2 <- removeOutliers(removeOutliers1)
removeOutliers3 <- removeOutliers(removeOutliers2)
removeOutliers4 <- removeOutliers(removeOutliers3)
cleanVehicleData <- removeOutliers4
cl_vehDataScale <- lapply(cleanVehicleData, z_score)
set.seed(26)
clusterNo <- NbClust(cl_vehDataScale, distance="euclidean", min.nc=2, max.nc=10,
method="kmeans", index="all")