r - Network_plot 'names' 属性

标签 r

我正在尝试制作一个网络图来显示相关性。我的代码:

camas_desempleo %>% 
  select(-CCAA) %>%
  correlate() %>% 
  network_plot()

数据:

> dput(camas_desempleo)
structure(list(CCAA = c("andalucía", "cataluña", "comunitat valenciana", 
"madrid, comunidad de", "canarias", "castilla - la mancha", "galicia", 
"castilla y león", "país vasco", "extremadura", "murcia, región de", 
"asturias, principado de", "aragón", "balears, illes", "cantabria"
), paro = c(884121, 418438.25, 393648.25, 368107, 225404, 185089.75, 
175023.5, 151656.5, 125436, 109651.75, 106352, 76787.75, 71575.5, 
63432.75, 40508.5), pub = c(572, 511, 450, 479, 187, 155, 215, 
180, 158, 97, 112, 86, 113, 78, 40), priv = c(162, 141, 101, 
225, 50, 13, 48, 20, 21, 5, 11, 7, 22, 46, 0), total = c(734, 
652, 551, 704, 237, 168, 263, 200, 179, 102, 123, 93, 135, 124, 
40)), row.names = c(NA, -15L), class = c("tbl_df", "tbl", "data.frame"
))

给出的错误如下:

Error in names(x) <- value : 'names' attribute [2] must be the same length as the vector [1] In addition: Warning message: In stats::cmdscale(abs(distance)) : only 1 of the first 2 eigenvalues are > 0

In addition: Warning message: In stats::cmdscale(abs(distance)) :
only 1 of the first 2 eigenvalues are > 0

我在文档中找不到有关此错误的任何信息,并且有一种非常相似的方法,对 mtcars 数据集使用相同的代码。

最佳答案

network_plot ( source code here ) 尝试执行以下步骤(第 188-189 行)时,就会出现问题:

points <- data.frame(stats::cmdscale(distance))
colnames(points) <-  c("x", "y")

network_plot 代码假设它将处理具有 2 列的数据框(k=2 默认情况下在 stats::cmdscale 中) )。 但这不一定是真的。来自引用(?stats::cmdscale):

A set of Euclidean distances on n points can be represented exactly in at most n - 1 dimensions. cmdscale follows the analysis of Mardia (1978), and returns the best-fitting k-dimensional representation, where k may be less than the argument k.

> stats::cmdscale(distance)
              [,1]
paro   0.118717157
pub    0.004476784
priv  -0.106540786
total -0.016653154
Warning message:
In stats::cmdscale(distance) : only 1 of the first 2 eigenvalues are > 0

在您的情况下,您只得到一列,因为内积矩阵只有一个特征值为正。你能找到解决办法吗?不。这与您输入数据的性质有关,即您提供了非欧几里德距离。您可以阅读此 here

关于r - Network_plot 'names' 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62394466/

相关文章:

r - 如何合并r中数据框中的列标题

r - 将两个向量组合成有序对列表

r - 如何将函数的代码写入字符串?

R - 将列值嵌套到单个向量列(类型列表)中

r - dplyr 中的快速字符串计数

r tryCatch 如何将对象传递给错误函数

r - 如何在 ggplot 中向 geom_text() 添加字母数字和希腊字符?

c++ - 将 Google perftools 指定为 g++ 以便在 R 中使用 C++

r - 计算大数据的差异矩阵

r - 如何在R中找到最长的相同数字