Announcement

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

  • Calculate the standard deviation of multiple variable at once.

    Hello,
    I would like to calculate the mean and the standard deviation of multiple variable at once

    I know I can use this using the sum command, but I would like to store estimated mean and SD separately to export them in a compacted way into a descriptive statistic table (I do not want to report the number of OBS and the Min and Max values I would get from using the sum command


    For instance take the following example: I am calculating mean values for both the whole sample and foreign cars.

    Code:
    sysuse auto
    mean price mpg rep78 headroom
    eststo avgall
    mean price mpg rep78 headroom if foreign==1
    eststo avgfor
    esttab avgall avgfor ,  nostar not label b(2)
    I would like to add to this table the associated standard deviations. I tried using sd, but it is an invalid command

    Thanks a lot
    Code:
    sd price mpg rep78 headroom
    eststo sdall
    sd price mpg rep78 headroom if foreign==1
    eststo sdfor
    
    esttab avgall sdall avgfor sdfor,  nostar not label b(2)

  • #2
    Code:
    table (var) , stat(mean price) stat(mean mpg) stat(mean headroom) stat(sd price) stat(sd mpg) stat(sd headroom)
    also see: https://blog.stata.com/2021/06/24/cu...assic-table-1/
    Last edited by Maarten Buis; 19 Oct 2022, 06:59.
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      It seems there are some entries in https://www.statalist.org/forums/for...iables-at-once as well.

      Here is another way, using egen:

      Code:
      sysuse auto, clear
      foreach x in price mpg rep78 headroom{
          egen mean_`x' = mean(`x')
          egen sd_`x' = sd(`x')
          egen mean_f_`x' = mean(`x') if foreign == 1
          egen sd_f_`x' = sd(`x') if foreign == 1
      }
      
      collapse (mean) mean_* sd_*
      
      gen id = 1
      reshape long mean_ sd_, i(id) j(vartype, string)
      drop id
      
      list, sep(4)
      Results:

      Code:
           +----------------------------------+
           |    vartype      mean_        sd_ |
           |----------------------------------|
        1. | f_headroom   2.613636   .4862837 |
        2. |      f_mpg   24.77273   6.611187 |
        3. |    f_price   6384.682   2621.915 |
        4. |    f_rep78   4.285714   .7171372 |
           |----------------------------------|
        5. |   headroom   2.993243   .8459948 |
        6. |        mpg    21.2973   5.785503 |
        7. |      price   6165.257   2949.496 |
        8. |      rep78   3.405797   .9899323 |
           +----------------------------------+

      Comment


      • #4
        I think this post was accidentally duplicated; other solutions are discussed at

        https://www.statalist.org/forums/for...iables-at-once

        Comment

        Working...
        X