【问题标题】:Error in `E<-`(`*tmp*`, value = 1:9) : invalid indexing when attempting a graph analysis in R`E<-`(`*tmp*`, value = 1:9) 中的错误:尝试在 R 中进行图形分析时索引无效
【发布时间】:2020-08-11 23:47:31
【问题描述】:

我正在尝试在 R 中进行图形分析,以根据他们的症状体验来寻找合并症的不同群体。我收到此错误:

Error in `E<-`(`*tmp*`, value = 1:9) : invalid indexing

这是我运行的代码:

G <- graph.data.frame(data_piv, directed = FALSE)

E(G)$weight=E(G)$V3


mst_prim = minimum.spanning.tree(G, weights=G$weight, algorithm = "prim")

这是我拥有的数据类型:

fake_data <- 
    structure(list(Comorbidities = c("asthma", "diabetes_type_one", 
    "diabetes_type_two", "heart_disease", "hypertension", "kidney_disease", 
    "liver_disease", "lung_condition", "obesity"), chills = c(25.8992805755396, 
    9.52380952380952, 18.3098591549296, 35.3846153846154, 19.9079401611047, 
    7.69230769230769, 38.2352941176471, 24.5967741935484, 18.3333333333333
    ), cough = c(59.1726618705036, 61.9047619047619, 57.0422535211268, 
    58.4615384615385, 58.3429228998849, 57.6923076923077, 55.8823529411765, 
    58.4677419354839, 57.7777777777778), diarrhoea = c(21.9424460431655, 
    9.52380952380952, 9.85915492957746, 27.6923076923077, 17.8365937859609, 
    19.2307692307692, 20.5882352941176, 19.758064516129, 21.2962962962963
    ), fatigue = c(60.431654676259, 47.6190476190476, 52.8169014084507, 
    61.5384615384615, 52.5891829689298, 46.1538461538462, 64.7058823529412, 
    56.0483870967742, 49.6296296296296), headache = c(43.705035971223, 
    28.5714285714286, 26.056338028169, 43.0769230769231, 37.7445339470656, 
    30.7692307692308, 44.1176470588235, 43.5483870967742, 40.3703703703704
    ), loss_smell_taste = c(20.863309352518, 14.2857142857143, 15.4929577464789, 
    27.6923076923077, 18.8722669735328, 19.2307692307692, 35.2941176470588, 
    20.9677419354839, 18.3333333333333), muscle_ache = c(47.6618705035971, 
    47.6190476190476, 38.0281690140845, 58.4615384615385, 44.1887226697353, 
    38.4615384615385, 61.7647058823529, 45.9677419354839, 44.2592592592593
    ), nasal_congestion = c(33.8129496402878, 19.047619047619, 28.169014084507, 
    30.7692307692308, 31.5304948216341, 30.7692307692308, 38.2352941176471, 
    38.7096774193548, 33.7037037037037), nausea_vomiting = c(10.0719424460432, 
    7.14285714285714, 7.74647887323944, 10.7692307692308, 5.75373993095512, 
    0, 8.82352941176471, 13.7096774193548, 8.14814814814815), shortness_breath = c(61.3309352517986, 
    28.5714285714286, 23.2394366197183, 53.8461538461538, 30.6098964326812, 
    57.6923076923077, 38.2352941176471, 50.8064516129032, 33.3333333333333
    ), sore_throat = c(46.5827338129496, 33.3333333333333, 35.9154929577465, 
    49.2307692307692, 49.2520138089758, 57.6923076923077, 64.7058823529412, 
    45.5645161290323, 48.7037037037037), sputum = c(47.4820143884892, 
    33.3333333333333, 40.1408450704225, 46.1538461538462, 36.9390103567319, 
    38.4615384615385, 44.1176470588235, 46.3709677419355, 39.8148148148148
    ), temperature = c(20.6834532374101, 30.952380952381, 40.8450704225352, 
    27.6923076923077, 21.9792865362486, 23.0769230769231, 32.3529411764706, 
    23.7903225806452, 20.7407407407407)), row.names = c("asthma", 
    "diabetes_type_one", "diabetes_type_two", "heart_disease", "hypertension", 
    "kidney_disease", "liver_disease", "lung_condition", "obesity"
    ), class = "data.frame")

有人可以帮忙吗?我正在尝试进行此分析,但没有成功。

【问题讨论】:

  • E(G)$weight 返回 NULLE(G)$V3 也是如此。
  • 我是这种分析的新手。然而,我希望我能理解如何追求和纠正它。
  • 哪一栏应该是你的体重?我真的不明白你想在这里构建什么样的图表。如果你看看你的顶点和边..它们非常奇怪所以有什么问题吗?
  • 首先你需要弄清楚你想要什么样的图表。 data_piv 需要包含两列 from 和 to,它们确定节点之间的边。您示例中的典型优势是asthma --25.8992805755396,这没什么意义。在这里您可以找到从 data.frames igraph.org/r/doc/graph_from_data_frame.html 制作图表的示例
  • @desval - 我几乎找不到任何例子。我确实找到了一个,我可以说我的节点是合并症,但是,边缘 - 如果我理解得很好,定义就是症状。因为我要解决的是是否存在基于症状表现的合并症集群。这是我尝试遵循的示例 https://www.jessesadler.com/post/network-analysis-with-r/

标签: r igraph minimum-spanning-tree


【解决方案1】:

这看起来像以因子载荷或相关值作为边权重的二分图结构。 如果是这种情况,你首先需要将你的数据集转换成合适的格式pivot from wide to long

library(tidyr)
edge_list <- pivot_longer(data_piv, -Comorbidities)

您可以从那里构建图表:

g <- graph_from_data_frame(edge_list[,c(2:1,3)], directed = T)

你可以像这样得到最小生成树(这里,边权重存储在value属性中):

mst_prim <- minimum.spanning.tree(g, weights=E(g)$value, algorithm = "prim")

【讨论】:

  • 谢谢 Ben,你能解释一下第二个代码在做什么吗?
  • 不客气!它在构建图形时向边缘添加方向。您可以通过编写 graph_from_data_frame(edge_list, directed = T) 或对于无向案例 graph_from_data_frame(edge_list, directed = F) 来重新更改它。
  • 感谢您开箱即用。这就是我需要的!
猜你喜欢
  • 1970-01-01
  • 2014-06-20
  • 1970-01-01
  • 2019-09-25
  • 2018-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-14
相关资源
最近更新 更多