Search code examples
rigraphminimum-spanning-tree

Error in `E<-`(`*tmp*`, value = 1:9) : invalid indexing when attempting a graph analysis in R


I am trying to get into graph analysis in R. in looking for different groups in comorbidities based on their experiences with symptoms. I get this error:

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

This is the code I have run:

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")

This is the type of data I have:

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")

Can someone help? I am trying to get this analysis yet not succeeding.


Solution

  • This looks like the structure of a bipartite graph with factor loadings or correlation values as edge weights. If that's the case, you first need to transform your data set into a suitable format pivot from wide to long:

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

    You can build your graph from there:

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

    enter image description here

    And you can get the minimal spanning tree like this (here, edge weights are stored in the value attribute):

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