Announcement

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

  • bar graph from matrix with CI as error bars

    Hi all,
    I am trying to figure out how to make bar graph out of a matrix. I know there have been many posts about this, but I am struggling to put it all together.
    What I want to do:
    In the example I have three variables with different sensitivity and specificity plus 95% CI, respectively. How can I show a bar for sensitivity and specificity in pairs/adjacent to each other with CI as error bars. This for each variable in the same diagram, with some space between the two bars for each variable and variable labels beneath the bars (x-axis).
    Here is my code, I have tried svmat and svmat2 but I am struggling putting it together from there.
    Stata version 16.1

    Code:
    clear all
    sysuse auto
    // here I generate binary varibles for later use with "diagt", "price_b_RF" will be the reference standard. The output is of course nonsense...
    recode price (0/5006.5=0) (5006.5/16000=1), gen(price_b_RF)
    recode mpg (0/20=0) (20/16000=1), gen(mpg_b)
    recode headroom (0/3=0) (3/5=1), gen(headroom_b)
    recode trunk (0/10=0) (11/20=1), gen(trunk_b)
    
    
    local test_var mpg_b headroom_b trunk_b
    local ntest_var : word count `test_var'
    
    matrix sens_spec = J(`ntest_var', 6, .)
    //labeling av columns
    matrix colnames sens_spec = sens "(CI  " "95%)" spec "(CI  " "95%)"
    //labeling rows
    local lable_rownames_diagt
    
    local row = 1
    foreach var in `test_var' {
        diagt price_b_RF `var'
        matrix sens_spec[`row', 1] = r(sens)
        matrix sens_spec[`row', 2] = r(sens_lb)
        matrix sens_spec[`row', 3] = r(sens_ub)    
        matrix sens_spec[`row', 4] = r(spec)
        matrix sens_spec[`row', 5] = r(spec_lb)
        matrix sens_spec[`row', 6] = r(spec_ub)
        //labeling av matrix rows
        local lable_rownames_diagt `lable_rownames_diagt' "`var'"
        local ++row
    }
    
    matrix rownames sens_spec = `lable_rownames_diagt'
    
    matrix sens_spec_trans = J(6, `ntest_var', .)
    
    matrix sens_spec_trans = sens_spec'
    
    // I do not know which format of the two following is better suited for making a graph afterwards.
    
    matlist sens_spec, format(%5.2f) twidth(10) title(Diagnostic performance)
    
    matlist sens_spec_trans, format(%12.2f) twidth(15) title(Diagnostic performance)
    Any help would be much appreciated.


  • #2
    Various small puzzles and one large puzzle here:

    1. diagt is from the Stata Journal, as you are asked to explain.

    2. As you say the
    recode is nonsense but wanting the code to jump first one way and then the other at the boundary is ambiguous.

    3. What have matrices to do with graphs? Not much. Better to set up variables directly.

    Some technique here may help:


    Code:
    clear all
    sysuse auto
    
    gen price_b_RF = price >= 5007 
    gen mpg_b = mpg >= 20 
    gen headroom_b = headroom >= 3 
    gen trunk_b = trunk >= 11 
    
    tokenize sens sens_lb sens_ub spec spec_lb spec_ub 
    
    forval j = 1/6 { 
         gen result`j' = . 
         label var result`j' "``j''"
    }
    
    local i = 0 
    foreach var in mpg_b headroom_b trunk_b {
        diagt price_b_RF `var'
        
        local ++i 
        forval j = 1/6 { 
            replace result`j' = r(``j'') in `i'
        }
    }
    
    list result* in 1/`i'

    Comment


    • #3
      Dear Nick,
      thank you so much for your advice. I was fixated on the matrices while doing it from variables is much easier. I think the svmat2 command is extremely helpful – thank you also for providing this command in the first place. So, the code you provided above is an alternative.

      Sorry for not explaining the diagt command, for everyone interested it can be installed by typing “ssc install diagt”, make sure that you have the latest version 2.032.

      Comment


      • #4
        Thanks for the closure. svmat2 is one of mine from 2000.

        . search svmat2, historical

        Search of official help files, FAQs, Examples, and Stata Journals

        STB-56 dm79 . . . . . . . . . . . . . . . . . . Yet more new matrix commands
        (help matcorr, matewmf, matvsort, svmat2 if installed) . . N. J. Cox
        7/00 pp.4--8; STB Reprints Vol 10, pp.17--23
        commands to produce a correlation matrix, elementwise monadic
        function of another matrix, selected subsets of matrix rows
        and columns, vec or vech of a matrix, elements sorted within
        a vector, matrix from a vector, and commands to save matrices
        see mata matrix language incorporated into Stata 9.0



        As StataCorp hint heavily in their annotation, some of these things are now better done in Mata, but in essence the Stata commands (should) still work.

        Comment

        Working...
        X