Announcement

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

  • Matrix of measures of association as a graphical table

    After having inspected the contigency tables and corresponding measures of association (specifically Cramer's V, G&K's gamma and K's tau-b) for all pairs of categorical variables in a dataset, I would like to be able to quickly look at just the measures of association without having to scroll again through multiple crosstabs. I thought of using the module "makematrix" as in the following example (running Stata version SE 16.1 on Windows 10):
    Code:
    ssc install makematrix
    
    clear
    
    set obs 30
    set seed 100
    gen var1 = runiformint(1, 2)
    gen var2 = runiformint(1, 3)
    gen var3 = runiformint(1, 4)
    gen var4 = runiformint(1, 3)
    gen var5 = runiformint(1, 2)
    
    foreach va of varlist var1-var5 {
    foreach vb of varlist var1-var5 {
    display "Measures of association for " "`va label `v''" " by " "`vb label `v'':"
    makematrix, from(r(CramersV) r(gamma) r(taub)) list cols(`va') : tab `vb', V gamma taub
    }
    }
    However, this solution still involves a fair bit of scrolling. Ideally I would be able to output a single matrix showing as a graphical table the Cramer's V, gamma and tau-b for all the variable pairs in the style of the Stata module "corrtable", but unfortunately I don't yet have the programming skills needed to pull off something like that. Do you know of any Stata modules that accomplish this (or do you have a program that does this and you'd be willing to share on this list)?

  • #2
    Temporary Username Come on. please use a full real name.

    https://www.statalist.org/forums/help#realnames

    https://www.statalist.org/forums/help#adviceextras #3

    Further, you should explain where community-contributed commands you refer to come from. https://www.statalist.org/forums/help#stata 12.1

    corrtable is from SSC and was announced in https://www.stata.com/statalist/arch.../msg00978.html and exemplified at https://www.statalist.org/forums/for...20-by-20-table and https://stats.idre.ucla.edu/stata/fa...c-stata-plots/

    makematrix is also from SSC and was discussed in https://www.stata-journal.com/articl...article=pr0011 in 2003. Perhaps my best comment is that I am the author and I haven't used it in years. When I want to loop over variables to produce a matrix of results, I just write the loops I want. It's harder work to read the help for makematrix and rediscover the syntax and to puzzle out whether it covers my problem.

    Wanting all those measures of association seems likely to produce a complicated display. Here I just show the result of a hack at
    corrtable yielding gammatable


    Code:
    *! 1.0.0 NJC 27 Sept 2020 
    *! corrtable 1.0.0 NJC 28 Nov 2007 
    program gammatable
        version 8 
        forval k = 1/7 { 
            local opts `opts' flag`k'(str) howflag`k'(str) 
        }
    
        syntax varlist(numeric) [if] [in] [,                        ///
        COMBine(str asis) DIAGonal(str asis) n ASE LISTwise HAlf   ///
        gformat(str) aseformat(str) gsize(str) `opts' * ]
    
         if "`gsize'" != "" & !index("`gsize'", "r(gamma)") { 
            di as err "gsize() must refer to r(gamma)" 
            exit 198 
        } 
    
        if "`listwise'" != "" marksample touse, novarlist 
        else                  marksample touse 
        qui count if `touse' 
        if r(N) == 0 error 2000 
    
        tokenize "`varlist'" 
        local nv : word count `varlist' 
        local h1 = 2 
        local h2 = `nv'     
    
        qui forval i = 1/`nv' { 
            local I : variable label ``i'' 
            if `"`I'"' == "" local I "``i''"
            local I = trim(`"`I'"') 
    
            if `i' < `nv' local holes `holes' `h1'/`h2' 
    
            local J = cond("`half'" != "", `i', `nv') 
    
            forval j = 1/`J' { 
                tempname g 
    
                if `i' == `j' { 
                    forval l = 1/5 { 
                        local p`l' : piece `l' 16 of "`I'", nobreak 
                    }
                    
                    local y = 5 
                    local call `y' 1 `"`p1'"'
    
                    forval l = 2/5 { 
                        if `"`p`l''"' != "" { 
                            local --y 
                            local call `call' `y' 1 `"`p`l''"' 
                        } 
                    }
    
                        if `y' < 5 { 
                        local --y
                        local scale r(`y' 6) 
                    } 
                    else local scale 
    
                    twoway scatteri `call', mlabsize(*3) mlabpos(0) ms(none ..) name(`g') ///
                    nodraw ysc(`scale' off) yla(, nogrid) xsc(off) `diagonal' 
                }
                else { 
                    tabulate ``i'' ``j'' if `touse', gamma  
    
                    if "`gsize'" != "" { 
                        local glabsize = `gsize' 
                        local gsizecall mlabsize(*`glabsize') 
                    }
                    if "`gformat'" == "" local gformat %4.3f
                    local gamma : di `gformat' r(gamma) 
                    local y = 3 
                    local call `y' 1 "`gamma'" 
    
                    if "`n'" != "" { 
                        local --y 
                        local call `call' `y' 1 "`r(N)'" 
                    }
    
                    if "`ase'" != "" { 
                        if "`aseformat'" == "" local aseformat %4.3f 
                        local ase: di `aseformat' r(ase_gam)  
                        local --y
                        local call `call' `y' 1 "`ase'"
                    }
    
                    local --y 
    
                    local show 
                    forval k = 1/7 {
                        if "`flag`k''" != "" {  
                            if `flag`k'' local show `howflag`k''
                        }
                    } 
    
                    twoway scatteri `call', mlabsize(*4) mlabpos(0) ///
                    ysc(r(`y' 4) off) xsc(r(0 2) off) yla(, nogrid) name(`g')     /// 
                    ms(none ..) nodraw `options' `show' `gsizecall'  
                }
    
                local G `G' `g' 
            }
    
            local h1 = `h1' + `nv' + 1 
            local h2 = `h2' + `nv' 
        }
    
        if "`half'" != "" graph combine `G', holes(`holes') `combine'
        else graph combine `G', `combine' 
    end
    Code:
    
    


    Here is a token example:


    Code:
    webuse nlswork
    set scheme s1color
    gammatable race msp collgrad c_city south union, half  flag1(r(gamma) > 0) howflag1(plotregion(color(blue * 0.2))) flag2(r(gamma) < 0) howflag2(plotregion(color(red*0.2))) gsize(3 + 3 * abs(r(gamma)))


    Click image for larger version

Name:	gammatable.png
Views:	1
Size:	27.2 KB
ID:	1574487



    There is no help file. To understand the syntax look at the code and compare the help for corrtable. A serious display would follow shuffling variables into a good order.




    Comment


    • #3
      This graph looks very much like what I had in mind, thank you! Even though my knowledge of programming is close to nill, being able to compare the code you're providing here against your corrtable code will help me decipher how both of these work (and perhaps even manage to shuffle variable order like you suggest).

      I included the line - ssc install makematrix - at the top of my code snippet, but apologies for not indicating the source of corrtable in my question. Both are great modules for beginners like me who still struggle with loops, by the way.

      About the temporary user name: Before joining the list I read the links you provide in your answer, but the registration FAQ does not say what to do when you have the same name as another list member who has already registered it. I tried Juan Gonzalez2 but the program didn't accept numbers in the name fields. However, the FAQ indicates the list moderators can change your user name if you ask them, so I went ahead and registered with a temporary user name and immediately used the Contact Us button to make the request.

      Comment


      • #4
        Understood and thanks.

        Comment

        Working...
        X