Search code examples
stata

Calculate coefficient of variation by more than one group


The command tabstat can generate coefficient of variation estimates by a single group.

How could I calculate the coefficient of variation for two groups?

As if the following was allowed in Stata:

tabstat price, statistics(n mean cv) by(group_var1 group_var2)

Solution

  • You can calculate and display it manually:

    bysort group_var1 group_var2 : egen sd = sd(price)
    bysort group_var1 group_var2 : egen mean = mean(price)
    generate cv = sd / mean
    
    tabdisp group_var1 group_var2, c(cv) 
    

    You can optionally scale by 100, if you like.