【问题标题】:'x' and 'y' lengths differ“x”和“y”长度不同
【发布时间】:2019-04-14 09:50:23
【问题描述】:

我正在尝试确定最佳聚类数。

# Determine optimal number of clusters
wss<-rep(0,2)
wss[1]<-sum(scale(price[,2:2],scale=FALSE)^2)
for(i in 2:16)
wss[i]<-sum(kmeans(price[,2:2],centers=i)$withinss)
plot(4:2,wss,type="b",xlab="Number of clusters",ylab="Within-cluster sum of squares")

除最后一行外,每一行都有效。 最后一个报错:

xy.coords(x, y, xlabel, ylabel, log) 中的错误: “x”和“y”长度不同

我尝试了其他问题的一些解决方案,但没有运气。有什么建议吗? 非常感谢!

样本数据:

    Country        Price

    Albania        1.57
    Andorra        1.24
    Azerbaijan     0.47
    Austria        1.33
    Belarus        0.73
    Belgium        1.54
    Bosnia & Herz. 1.29
    Bulgaria       1.13
    Croatia        1.44
    Czech Rep.     1.32
    Cyprus         1.28
    Denmark        1.74
    Estonia        1.41
    Finland        1.61
    France         1.67
    Georgia        0.9

【问题讨论】:

  • 欢迎来到 SO。您需要与dput 和预期输出共享样本数据
  • 阿富汗 0,67 阿尔巴尼亚 1,57 阿尔及利亚 0,35 安道尔 1,24 安哥拉 0,52 这是样本数据,实际上有 47 行。我希望它有一个“曲线”作为输出,以查看最佳聚类计数是多少。
  • i.stack.imgur.com/VVzNR.png - 预期输出样本
  • 使用dput(yourData)dput(head(yourData))共享数据
  • 样本数据现在应该是可见的。谢谢。

标签: r


【解决方案1】:

wss(y 变量)的长度为 16,但在 x 轴上您使用的是 4:2(长度为 3)。这就是您收到错误消息的原因。

将 4:2 更改为 17:2 以使 x 和 y 变量的长度相同。 喜欢:

plot(17:2,wss,type="b",xlab="Number of clusters",ylab="Within-cluster sum of squares")

【讨论】:

  • 谢谢。我尝试了你的建议,但仍然是同样的错误。 :(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-18
  • 1970-01-01
  • 1970-01-01
  • 2021-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多