How can I make a KaryTree of this raw-data (raw-data at the end of the question): This is is data from a political survey in which I ask 5 questions with the following possible answers:

  • Q1: 'Y'= Yes, 'N' = No;
  • Q2: 'D' = Democrat, 'R' = Republican, 'O' = Other;
  • Q3: 'C'= Clinton, 'S'= Sanders;
  • Q4: 'C'= Cruz, 'K' = Kasich, 'T'= Trump;
  • Q5: 'C' = Clinton, 'T' = Trump

(A blank is also valid answer for any of these questions)

I want to make a TreeGraph or a KaryTree that has a level for each question, beginning with Q1, but each answer branches into the following question with its own answer. For example, if 'Y' for Q1, then the following answer for the next question will be a child of 'Y', and the same applies for each answer. Also, I want to have an edge represent a participant in the survey (260 participants). Meaning, each level will have 260 edges, but they are divided among the answers the participants answer. So, each answer is a node, each question is a level, and each participant is an edge. Also, is there a way to find a correlation and display the correlation between these answers? The correlation I would calculate is the correlation between a participant's support of political candidates and their answer in Q5.

{{"N", "D", "S", "C", "C"}, {"N", "D", "S", "C", "C"}, {"Y", "R", 
"C", "T", "T"}, {"N", "D", "S", "C", "C"}, {"Y", "D", "C", "C", 
"C"}, {"Y", "R", "C", "K", "T"}, {"Y", "R", "C", "T", "T"}, {"Y", 
"D", "S", "C", "C"}, {"Y", "R", "C", "T", "T"}, {"Y", "R", "C", 
"T", "T"}, {"N", "D", "S", "C", "T"}, {"Y", "D", "C", "K", 
"T"}, {"Y", "D", "S", "C", "C"}, {"Y", "R", "S", "T", "T"}, {"Y", 
"R", "C", "C", "C"}, {"Y", "D", "S", "C", "C"}, {"N", "D", "S", 
"C", "C"}, {"N", "D", "S", "C", "C"}, {"Y", "R", "C", "T", 
"T"}, {"Y", "R", "C", "T", "T"}, {"Y", "R", "C", "T", "T"}, {"Y", 
"R", "C", "T", "T"}, {"Y", "R", "C", "T", "T"}, {"Y", "D", "S", 
"C", "C"}, {"Y", "D", "S", "C", "C"}, {"Y", "R", "C", "T", 
"T"}, {"Y", "R", "C", "T", "T"}, {"Y", "D", "C", "C", "C"}, {"Y", 
"R", "C", "T", "T"}, {"N", "D", "S", "C", "C"}, {"Y", "R", "C", 
"T", "T"}, {"Y", "R", "C", "T", "T"}, {"Y", "R", "C", "T", 
"T"}, {"Y", "D", "S", "C", "C"}, {"Y", "D", "S", "C", "C"}, {"Y", 
"D", "", "K", "C"}, {"Y", "D", "S", "K", "C"}, {"Y", "O", "C", "K",
 "C"}, {"Y", "R", "S", "K", "C"}, {"Y", "D", "S", "", ""}, {"Y", 
"R", "C", "C", "C"}, {"Y", "R", "S", "C", "C"}, , {"Y", "O", "S", 
"K", "C"}, {"Y", "O", "", "", ""}, {"Y", "R", "S", "K", "C"}, {"Y",
 "O", "S", "C", "C"}, {"Y", "R", "S", "T", "T"}, {"N", "O", "S", 
"C", "C"}, {"Y", "O", "", "", ""}, {"Y", "", "S", "", ""}, {"N", 
"", "", "", ""}, {"Y", "D", "S", "T", "T"}, {"Y", "O", "S", "C", 
"T"}, {"Y", "O", "S", "K", "C"}, {"Y", "D", "S", "", "C"}, {"Y", 
"R", "S", "C", "T"}, {"N", "D", "S", "K", "C"}, {"Y", "O", "S", 
"C", "T"}, {"N", "D", "S", "C", "C"}, {"Y", "D", "S", "K", 
"C"}, {"Y", "R", "", "C", ""}, {"N", "O", "S", "C", "C"}, {"Y", 
"D", "", "", "C"}, {"N", "O", "S", "C", "C"}, {"Y", "D", "S", "C", 
"C"}, {"Y", "D", "S", "K", "C"}, {"Y", "D", "S", "", "C"}, {"Y", 
"O", "", "", ""}, {"Y", "D", "S", "C", "C"}, {"Y", "R", "S", "C", 
"T"}, {"Y", "D", "S", "K", "C"}, {"Y", "D", "C", "C", "C"}, {"Y", 
"R", "S", "T", "T"}, {"Y", "R", "C", "T", "T"}, {"N", "D", "S", 
"C", "C"}, {"N", "O", "S", "K", "T"}, {"Y", "O", "C", "K", 
"C"}, {"Y", "D", "S", "K", "C"}, {"N", "O", "S", "K", "C"}, {"Y", 
"D", "S", "C", ""}, {"Y", "D", "S", "C", "C"}, {"Y", "R", "S", "T",
"T"}, {"N", "O", "S", "C", "C"}, {"N", "O", "S", "K", "C"}, {"Y", 
"D", "S", "C", "C"}, {"Y", "R", "C", "T", "C"}, {"N", "D", "S", "",
"C"}, {"Y", "O", "S", "C", "C"}, {"Y", "R", "S", "K", "T"}, {"Y", 
"R", "C", "T", "C"}, {"N", "R", "S", "C", "C"}, {"N", "D", "S", "",
""}, {"Y", "O", "S", "", "C"}, {"Y", "R", "S", "K", "T"}, {"Y", 
"R", "C", "K", "C"}, {"N", "D", "S", "", ""}, {"N", "O", "S", "C", 
""}, {"Y", "", "S", "", "C"}, {"N", "O", "S", "C", "C"}, {"Y", "O",
"C", "K", "C"}, {"Y", "D", "S", "K", "C"}, {"N", "D", "S", "C", 
"C"}, {"N", "D", "S", "C", "C"}, {"Y", "D", "S", "K", "C"}, {"Y", 
"D", "S", "C", "C"}, {"Y", "D", "C", "", "C"}, {"Y", "D", "S", "", 
"C"}, {"Y", "R", "C", "C", "C"}, {"Y", "D", "S", "", ""}, {"N", 
"D", "C", "", "C"}, {"Y", "R", "C", "C", ""}, {"Y", "D", "C", "", 
"C"}, {"N", "D", "S", "", ""}, {"Y", "D", "C", "", "C"}, {"Y", "O",
"C", "C", ""}, {"Y", "D", "S", "", "C"}, {"Y", "R", "", "C", 
""}, {"Y", "D", "S", "", ""}, {"N", "O", "S", "", ""}, {"Y", "D", 
"C", "", "C"}, {"Y", "D", "S", "", ""}, {"Y", "R", "", "C", 
""}, {"N", "D", "S", "", ""}, {"", "", "S", "", "C"}, {"Y", "D", 
"C", "", "C"}, {"Y", "D", "S", "", ""}, {"Y", "D", "C", "", 
"C"}, {"N", "", "S", "", "C"}, {"Y", "D", "C", "", "C"}, {"Y", "D",
"S", "", "C"}, {"", "R", "", "C", ""}, {"N", "D", "S", "", 
""}, {"Y", "D", "C", "", "C"}, {"N", "D", "C", "", "C"}, {"Y", "R",
"", "", ""}, {"N", "D", "C", "", "C"}, {"Y", "D", "S", "", 
"C"}, {"Y", "R", "C", "T", "T"}, {"Y", "R", "C", "T", "T"}, {"N", 
"D", "S", "C", "C"}, {"Y", "R", "C", "T", "T"}, {"Y", "R", "C", 
"T", "T"}, {"Y", "D", "S", "K", "C"}, {"Y", "D", "S", "C", 
"C"}, {"N", "R", "C", "T", "T"}, {"Y", "D", "S", "C", "C"}, {"Y", 
"D", "S", "C", "C"}, {"Y", "R", "C", "T", "C"}, {"Y", "R", "S", 
"K", "C"}, {"Y", "R", "C", "T", "T"}, {"Y", "D", "C", "C", 
"C"}, {"Y", "D", "S", "T", "T"}, {"N", "D", "S", "C", "C"}, {"N", 
"R", "C", "T", "T"}, {"Y", "R", "C", "T", "T"}, {"Y", "R", "C", 
"T", "T"}, {"N", "D", "S", "C", "C"}, {"N", "D", "S", "C", 
"C"}, {"N", "D", "S", "C", "C"}, {"Y", "D", "S", "C", "C"}, {"Y", 
"D", "S", "C", "C"}, {"Y", "R", "C", "T", "T"}, {"Y", "R", "C", 
"T", "T"}, {"Y", "D", "S", "C", "C"}, {"Y", "D", "S", "C", 
"C"}, {"N", "R", "S", "T", "T"}, {"N", "D", "S", "C", "C"}, {"Y", 
"R", "C", "T", "T"}, {"Y", "D", "S", "C", "C"}, {"Y", "D", "S", 
"C", "C"}, {"Y", "D", "S", "C", "C"}, {"Y", "R", "C", "T", 
"T"}, {"Y", "R", "C", "T", "T"}, {"Y", "D", "C", "T", "C"}, {"Y", 
"R", "C", "T", "T"}, {"Y", "R", "S", "C", "T"}, {"Y", "R", "C", 
"T", "T"}, {"N", "D", "S", "C", "C"}, {"Y", "D", "C", "T", 
"C"}, {"N", "O", "S", "K", ""}, {"Y", "R", "C", "T", "T"}, {"Y", 
"R", "C", "C", "C"}, {"Y", "R", "S", "C", "C"}, {"N", "O", "S", 
"C", "C"}, {"Y", "D", "C", "T", "T"}, {"Y", "R", "C", "C", 
"T"}, {"N", "D", "C", "", "C"}, {"N", "O", "S", "C", "C"}, {"Y", 
"R", "C", "T", "T"}, {"Y", "R", "C", "T", "T"}, {"Y", "R", "C", 
"C", "C"}, {"N", "R", "C", "T", "T"}, {"Y", "R", "S", "C", 
"C"}, {"N", "D", "C", "T", "C"}, {"Y", "D", "C", "C", "C"}, {"Y", 
"D", "S", "", "C"}, {"Y", "D", "S", "C", "C"}, {"Y", "O", "S", "C",
"C"}, {"Y", "O", "S", "C", "C"}, {"N", "O", "", "", ""}, {"Y", 
"O", "S", "", ""}, {"Y", "R", "T", "T", "T"}, {"Y", "R", "", "", 
""}, {"Y", "O", "C", "", "C"}, {"N", "R", "T", "T", "T"}, {"N", 
"R", "S", "", "C"}, {"N", "D", "S", "", "C"}, {"Y", "D", "S", "", 
""}, {"Y", "D", "S", "", ""}, {"Y", "D", "C", "C", "C"}, {"Y", "O",
"C", "C", "C"}, {"Y", "O", "C", "C", "C"}, {"Y", "O", "C", "C", 
"C"}, {"Y", "", "", "", "C"}, {"Y", "D", "C", "K", ""}, {"N", "D", 
"C", "C", ""}, {"Y", "O", "T", "T", "T"}, {"Y", "D", "S", "K", 
""}, {"Y", "R", "", "", ""}, {"Y", "D", "C", "", "C"}, {"Y", "", 
"S", "", ""}, {"Y", "D", "C", "", ""}, {"Y", "D", "S", "", 
""}, {"N", "R", "C", "", "C"}, {"N", "R", "S", "C", ""}, {"Y", "D",
"C", "C", ""}, {"Y", "D", "S", "", ""}, {"N", "D", "S", "C", 
"C"}, {"N", "D", "C", "K", "C"}, {"N", "R", "S", "K", "C"}, {"Y", 
"D", "S", "C", "C"}, {"N", "O", "S", "K", "C"}, {"N", "D", "S", 
"K", "C"}, {"Y", "D", "S", "C", "C"}, {"N", "O", "S", "K", 
"C"}, {"N", "", "S", "C", "C"}, {"Y", "D", "C", "C", "C"}, {"Y", 
"D", "S", "C", ""}, {"Y", "D", "S", "C", "C"}, {"Y", "R", "S", "", 
"C"}, {"Y", "D", "S", "C", "C"}, {"Y", "O", "C", "C", "C"}, {"Y", 
"D", "S", "C", "C"}, {"Y", "O", "S", "K", "C"}, {"Y", "R", "S", 
"K", "T"}, {"Y", "R", "S", "C", "C"}, {"Y", "R", "S", "T", 
"T"}, {"N", "R", "C", "T", "C"}, {"Y", "O", "S", "C", "C"}, {"Y", 
"R", "S", "C", "C"}, {"N", "R", "S", "C", "C"}, {"Y", "D", "C", 
"C", "C"}, {"Y", "O", "C", "C", "C"}, {"Y", "D", "S", "K", 
"C"}, {"Y", "D", "", "", "C"}, {"Y", "R", "S", "T", "T"}, {"Y", 
"D", "C", "K", "C"}}
  • $\begingroup$ Could you describe the calculation you envision for the correlation within the survey? $\endgroup$ Apr 29, 2016 at 22:02

1 Answer 1

response = {{"N","D","S","C","C"},{"N","D","S","C","C"},

In order for the graph to make some sense, we're going to sub in your response values. We'll do this for each col, because in some causes the keys are repeated. (also note the final nodes for clinton and trump have an appended F to prevent the tree from looping back on its self)

response = Select[response,Length@# ==5&];

response[[All,1]]=response[[All,1]]/.{"Y"->"Yes","N"->"No", ""-> "No q1 response"};
response[[All,2]]=response[[All,2]]/.{"D"->"Democrat","R"->"Republican","O"->"Other", ""-> "No q2 response"};
response[[All,3]]=response[[All,3]]/.{"C"->"Clinton","S"->"Sanders", ""-> "No q3 response"};
response[[All,4]]=response[[All,4]]/.{"C"->"Cruz","K"->"Kasich","T"->"Trump", ""-> "No q4 response"};
response[[All,5]]=response[[All,5]]/.{"C"->"ClintonF","T"->"TrumpF", ""-> "No q5 response"};

Now we'll change the structure into an adjacency list with a common root node so the tree starts from the same location:

adjList = Flatten[{"Q1"->#[[1]],#[[1]]->#[[2]],#[[2]]->#[[3]],
          #[[3]]->#[[4]],#[[4]]-> #[[5]]}&/@ response];

Then, we can graph this, using a layered packing method with a specified "Q1" root node. Note the directed edges to denote the evolution of the tree

TreePlot[adjList,Automatic,"Q1",VertexLabeling->True, DirectedEdges->True,

Plotted for the first 10 values:

enter image description here

Plotted for the first 50 values:

Tree 2

  • $\begingroup$ My TreePlot seems to be very off. Will having null-values with my data affect this? I copy & pasted your code and it came out without any branches. Also, thank so much for your help $\endgroup$
    – Paul Lemus
    Apr 29, 2016 at 22:40
  • $\begingroup$ A null-value should be represented, so each layer would have an additional option of NULL. Is your production data similar to what you have pasted? $\endgroup$ Apr 29, 2016 at 22:43
  • $\begingroup$ I just added the exact data I have, nulls included. $\endgroup$
    – Paul Lemus
    Apr 29, 2016 at 22:45
  • $\begingroup$ just made some edits, this will add another member to each level of the tree for null responses, also added some filtering to only graph records where we have 5 responses (including the nulls) $\endgroup$ Apr 29, 2016 at 22:53
  • $\begingroup$ Here is a link to the my outputted graphic: link $\endgroup$
    – Paul Lemus
    Apr 29, 2016 at 22:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.