Announcement

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

  • Mean of frequency from tab

    Hello,
    i'm using Stata 16.

    I am trying to calculate the mean of the frequency values (second column) of a tab result:

    Code:
    . tab bin if year == 2005
    
            bin |      Freq.     Percent        Cum.
    ------------+-----------------------------------
          -97.5 |          6        0.26        0.26
          -94.5 |          4        0.17        0.43
          -91.5 |         10        0.43        0.86
          -88.5 |         10        0.43        1.28
          -85.5 |          9        0.39        1.67
          -82.5 |          6        0.26        1.93
          -79.5 |         10        0.43        2.35
          -76.5 |         14        0.60        2.95
          -73.5 |         11        0.47        3.42
          -70.5 |         22        0.94        4.37
          -67.5 |         12        0.51        4.88
          -64.5 |         20        0.86        5.74
          -61.5 |         18        0.77        6.51
          -58.5 |         22        0.94        7.45
          -55.5 |         21        0.90        8.35
          -52.5 |         22        0.94        9.29
          -49.5 |         29        1.24       10.53
          -46.5 |         35        1.50       12.03
          -43.5 |         31        1.33       13.36
          -40.5 |         18        0.77       14.13
    .
    .
    .
    ------------+-----------------------------------
          Total |      2,336      100.00
    I found out that I can save my results in a matrix using the command
    Code:
    tab bin if year == 2005, matcell(freqs)
    but I don't know if that's going to get me anywhere.

  • #2
    The mean frequency is just the total count divided by the number of bins.

    Code:
    count if bin < . &  year == 2005
    local N = r(N)
    tab bin if year == 2005
    di `N' / r(r)
    I suspect that you want, or need, something quite different, possibly starting with


    Code:
    contract bin year

    Comment


    • #3
      Code:
      sysuse auto, clear
      
      tabulate rep78, matcell(F)
      matlist F
      mata: st_numscalar("r(mf)",mean(st_matrix("F")))
      di "Mean frequencies = `r(mf)'"
      Result:
      Code:
      . di "Mean frequencies = `r(mf)'"
      Mean frequencies = 13.8
      After reading Nick's post:
      Code:
      sysuse auto, clear
      tabulate rep78
      di "Mean frequencies = " r(N)/r(r)
      Last edited by Dirk Enzmann; 28 May 2021, 08:26. Reason: Added alternative solution after reading Nick's post

      Comment

      Working...
      X