Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Use variable labels to label leafs in dendrogram instead of variable names

    Here's an example of some code:

    Code:
    use http://www.stata-press.com/data/r13/wclub, clear
    matrix dissimilarity clubD = , variables Jaccard dissim(oneminus)
    clustermat singlelink clubD, name(club) clear labelvar(question)
    cluster dendrogram club, labels(question) xlabel(, angle(90) labsize(*.75)) title(Single-linkage clustering) ytitle(1 - Jaccard similarity, suffix)
    This is the graph it produces:

    Click image for larger version

Name:	Graph.jpg
Views:	1
Size:	206.1 KB
ID:	175069

    Would it be possible to get Stata to use the variable labels instead of their names at the bottom? For instance, to have "enjoy bicycle riding Y/N" instead of bike?
    Last edited by Dimitriy V. Masterov; 22 Aug 2014, 19:00.

  • #2
    How about:

    Code:
    clear all
    set more off
    
    use132 wclub, clear
    
    describe
    
    set obs `r(k)'
    
    gen vnames = ""
    gen vlabels = ""
    local i = 1
    foreach v of varlist _all {
    
        capture replace vnames = "`v'" in `i'
        capture replace vlabels = "`:variable label `v''" in `i'
        local i = `i' + 1
        
    }
    
    keep vnames vlabels
    rename vnames question
    
    tempfile text
    save "`text'"
    
    use132 wclub, clear
    
    matrix dissimilarity clubD = , variables Jaccard dissim(oneminus)
    
    clustermat singlelink clubD, name(club) clear labelvar(question)
    
    merge 1:1 question using "`text'", assert(match) nogen
    
    cluster dendrogram club, labels(vlabels) xlabel(, angle(90) labsize(*.75)) ///
        title(Single-linkage clustering) ytitle(1 - Jaccard similarity, suffix)
    It can be made a bit shorter, but the idea is the same. Substitute out -use132 ...-, which is a personal command (based on -use13-, of course).

    Click image for larger version

Name:	Graph.png
Views:	1
Size:	54.2 KB
ID:	175073
    You should:

    1. Read the FAQ carefully.

    2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

    3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

    4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

    Comment


    • #3
      A slightly different version:

      Code:
      clear all
      set more off
      
      use132 wclub, clear
      
      quietly describe
      local rk = `r(k)'
      
      set obs `rk'
      
      unab allvars: _all
      tokenize `allvars'
      
      gen vnames = ""
      gen vlabels = ""
      forvalues i = 1/`rk' {
          
          replace vnames = "``i''" in `i'
          replace vlabels = "`:variable label ``i'''" in `i'
      
      }
      
      <snip>
      You should:

      1. Read the FAQ carefully.

      2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

      3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

      4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

      Comment


      • #4
        Thanks, Roberto! This works very nicely.

        Comment

        Working...
        X