Announcement

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

  • Coefficient of Variation

    Dear all Statalist,

    Just an apparent easy question, even if I am having problems in computation: if I have three variables (three columns) namely sbp_1, sbp_2 and sbp_3 (different values of blood pressure for each patient's ID), How can I compute the coefficient of variation for each patient in a separate column ?

    thank you in advance

  • #2
    You could try something like the following.
    Code:
    egen double mean = rowmean(sbp_?)
    egen double sd = rowsd(sbp_?)
    generate double cv = sd / mean
    drop mean sd

    Comment


    • #3
      I'd like to take the liberty of going beyond the "how" question to the unasked "Is this useful" question. I'm a "fan" of the CV, having published about it and with it some years ago. In Mich's situation, I'd presume that goal is to characterized the BP CV in each patient's theoretical population of BP values. If that's right, the precision of a CV based on only three values will be low, and perhaps therefore not a helpful estimate. I do understand, of course, that obtaining obtain, say, 20 or 30 BP measures for each patient is impossible, but I'd be sensitive to the likely instability of such a CV.

      Comment


      • #4
        Note that a community contributed package, created by Lorenz Graf-Vlachy, to compute the coefficient of variation is available:
        Code:
        findit cv2
        * or
        ssc install cv2
        http://publicationslist.org/eric.melse

        Comment


        • #5
          Thank you all for the precious help... for Mike Lacy, I appreciated your clarification and I think I can find more than 3 measurements! Thank you, I will work on this

          Comment


          • #6
            Originally posted by Joseph Coveney View Post
            You could try something like the following.
            Code:
            egen double mean = rowmean(sbp_?)
            egen double sd = rowsd(sbp_?)
            generate double cv = sd / mean
            drop mean sd

            It works very well, thank yoy Joseph !!! Just a short question: is it possible to replace the mean with the geometric mean in that command?

            Have a nice day!

            Comment


            • #7
              I am not aware of an egen function for geometric means calculated rowwise. Unless someone flags where one is, you need your own loop: This one goes as far as ignoring negative, zero and missing arguments to the extent possible.


              Code:
              clear 
              
              * sandbox (no data example provided) 
              set obs 10 
              set seed 2803
              
              forval j = 1/3 { 
                  gen sbp_`j'  = floor(10^rnormal(2, 0.1))
              }
              
              * start here 
              gen double gmean = 0 
              gen count = 0 
              
              ^ use your own varlist 
              foreach v of var sbp_* { 
                  replace gmean = gmean + cond(log(`v') < ., log(`v'), 0) 
                  replace count = count + (log(`v') < .) 
              }
              
              replace gmean = exp(gmean/count)
              format gmean %2.1f 
              
              list 
              
                   +---------------------------------------+
                   | sbp_1   sbp_2   sbp_3   gmean   count |
                   |---------------------------------------|
                1. |    89     121     118   108.3       3 |
                2. |   178     136      93   131.1       3 |
                3. |   116      83      89    95.0       3 |
                4. |   101      93     134   108.0       3 |
                5. |   103     106     123   110.3       3 |
                   |---------------------------------------|
                6. |   110     153     139   132.7       3 |
                7. |    65     146      84    92.7       3 |
                8. |    93      61      96    81.7       3 |
                9. |   116      78      98    96.1       3 |
               10. |   141     128      82   114.0       3 |
                   +---------------------------------------+
              Code:
              
              


              Interest in coefficients of variation is often better channelled into thinking on logarithmic scale and therefore into working with geometric means. See for example https://stats.stackexchange.com/ques...t-of-variation

              Comment


              • #8
                Thank a lot Nick !!!

                Comment

                Working...
                X