Announcement

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

  • Computing average productivity per weekday and weekend

    Hi everyone,

    I have a dataset which more or less looks like the one provided below. For this dataset, I want to calculate the average number of surveys done per surveyor per weekday and also the average number of surveys done per surveyor per weekend (which for me is only Sunday).

    In my dataset:

    - variable surveyor_id denotes the ID given to the surveyor.
    - variable total_surveys denotes the total surveys done by the surveyor
    - variable total_days denotes the total days the surveyor has worked
    - variable dow denotes the what day of the week it was. For example, 0 = Sunday and 6 = Saturday.

    Please note that weekdays for me would constitute: Tuesday, Wednesday, Thursday, Friday and Saturday and weekend would constitute only Sunday. Monday is a holiday.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(surveyor_id total_surveys total_days dow)
    1 4 2 0
    2 6 3 2
    3 2 1 3
    4 1 1 0
    5 1 1 4
    1 4 2 1
    2 6 3 0
    2 6 3 5
    end
    Would anyone know of a way to compute these?

    Thank you!

  • #2
    Rachita:
    you may want to try something along the following lines:
    Code:
    . bysort surveyor_id dow: tabstat total_surveys
    
    ------------------------------------------------------------------------------------------------------------------------
    -> surveyor_id = 1, dow = 0
    
        variable |      mean
    -------------+----------
    total_surv~s |         4
    ------------------------
    
    ------------------------------------------------------------------------------------------------------------------------
    -> surveyor_id = 1, dow = 1
    
        variable |      mean
    -------------+----------
    total_surv~s |         4
    ------------------------
    
    ------------------------------------------------------------------------------------------------------------------------
    -> surveyor_id = 2, dow = 0
    
        variable |      mean
    -------------+----------
    total_surv~s |         6
    ------------------------
    
    ------------------------------------------------------------------------------------------------------------------------
    -> surveyor_id = 2, dow = 2
    
        variable |      mean
    -------------+----------
    total_surv~s |         6
    ------------------------
    
    ------------------------------------------------------------------------------------------------------------------------
    -> surveyor_id = 2, dow = 5
    
        variable |      mean
    -------------+----------
    total_surv~s |         6
    ------------------------
    
    ------------------------------------------------------------------------------------------------------------------------
    -> surveyor_id = 3, dow = 3
    
        variable |      mean
    -------------+----------
    total_surv~s |         2
    ------------------------
    
    ------------------------------------------------------------------------------------------------------------------------
    -> surveyor_id = 4, dow = 0
    
        variable |      mean
    -------------+----------
    total_surv~s |         1
    ------------------------
    
    ------------------------------------------------------------------------------------------------------------------------
    -> surveyor_id = 5, dow = 4
    
        variable |      mean
    -------------+----------
    total_surv~s |         1
    ------------------------
    
    .
    
    
    .
    Kind regards,
    Carlo
    (Stata 15.1 SE)

    Comment


    • #3
      Hi Carlo,

      Thanks a lot for replying. This would work fantastically to see what is happening at the surveyor level. However, I would also want to create a variable which would give me - on an average "how many surveys does one surveyor in a weekday/weekend" do?

      Basically, I want a variable containing total number of surveys done in a weekday/Sunday for each surveyor (1) and another variable containing total number of weekdays/Sundays worked for each surveyor (2)

      And a third variable which is (1)/(2) = (3). The mean of (3) would then constitute "average number of surveys done say, in a weekday per surveyor.

      Does this make sense to you?

      Thanks,
      Rachita

      Comment


      • #4
        Rachita:
        I would point you to -help collapse-.
        Kind regards,
        Carlo
        (Stata 15.1 SE)

        Comment


        • #5
          Rachita, you couldn't do the table of summary statistics with the totals you provided, but you could with the raw daily data. (I've slightly modified your data and added a couple more observations):

          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input byte(surveyor_id raw_surveys dow)
          1 3 0
          1 2 1
          2 3 0
          2 2 2
          2 1 5
          3 2 3
          4 1 0
          5 1 4
          6 2 0
          6 3 3
          7 3 0
          7 2 3
          7 4 4
          7 1 5
          end
          Code:
          egen total_surveys = total( raw_surveys), by( surveyor_id)
          bysort surveyor_id (dow): gen total_days = _N
          gen is_wknd = (dow==0)
          gen is_wkday = (dow!=0)  // I included Monday (a holiday) here; you may want to re-classify
          rename total_* t_*  // just shortening name so list will print out better here
          
          . list surveyor_id raw_surveys t_surveys t_days dow is_wknd is_wkday, sepby( surveyor_id) noobs abbrev(12)
          
            +---------------------------------------------------------------------------+
            | surveyor_id   raw_surveys   t_surveys   t_days   dow   is_wknd   is_wkday |
            |---------------------------------------------------------------------------|
            |           1             3           5        2     0         1          0 |
            |           1             2           5        2     1         0          1 |
            |---------------------------------------------------------------------------|
            |           2             3           6        3     0         1          0 |
            |           2             2           6        3     2         0          1 |
            |           2             1           6        3     5         0          1 |
            |---------------------------------------------------------------------------|
            |           3             2           2        1     3         0          1 |
            |---------------------------------------------------------------------------|
            |           4             1           1        1     0         1          0 |
            |---------------------------------------------------------------------------|
            |           5             1           1        1     4         0          1 |
            |---------------------------------------------------------------------------|
            |           6             2           5        2     0         1          0 |
            |           6             3           5        2     3         0          1 |
            |---------------------------------------------------------------------------|
            |           7             3          10        4     0         1          0 |
            |           7             2          10        4     3         0          1 |
            |           7             4          10        4     4         0          1 |
            |           7             1          10        4     5         0          1 |
            +---------------------------------------------------------------------------+
          
          
          *** You can get your summary statistics now in different ways
          
          * Summary by each day of the week
          table dow, c(mean raw_surveys p50 raw_surveys min raw_surveys max raw_surveys) row
          
          --------------------------------------------------------------------------
                dow | mean(raw_su~s)   med(raw_su~s)   min(raw_su~s)   max(raw_su~s)
          ----------+---------------------------------------------------------------
                  0 |            2.4               3               1               3
                  1 |              2               2               2               2
                  2 |              2               2               2               2
                  3 |        2.33333               2               2               3
                  4 |            2.5             2.5               1               4
                  5 |              1               1               1               1
                    | 
              Total |        2.14286               2               1               4
          --------------------------------------------------------------------------
          
          * Same, but using tabstat
          . tabstat raw_surveys, by(dow) stats(n mean median min max)
          
               dow |         N      mean       p50       min       max
          ---------+--------------------------------------------------
                 0 |         5       2.4         3         1         3
                 1 |         1         2         2         2         2
                 2 |         1         2         2         2         2
                 3 |         3  2.333333         2         2         3
                 4 |         2       2.5       2.5         1         4
                 5 |         2         1         1         1         1
          ---------+--------------------------------------------------
             Total |        14  2.142857         2         1         4
          ------------------------------------------------------------
          
          
          * Summary by weekday vs weekend
          table is_wkday , c(mean raw_surveys p50 raw_surveys min raw_surveys max raw_surveys) format(%9.2f) row
          
          --------------------------------------------------------------------------
           is_wkday | mean(raw_su~s)   med(raw_su~s)   min(raw_su~s)   max(raw_su~s)
          ----------+---------------------------------------------------------------
                  0 |           2.40            3.00            1.00            3.00
                  1 |           2.00            2.00            1.00            4.00
                    | 
              Total |           2.14            2.00            1.00            4.00
          --------------------------------------------------------------------------
          
          
          * Summary by weekday vs weekend (same as above, but using tabstat)
          . tabstat raw_surveys, by( is_wkday) stats(n mean median min max)
          
          
          is_wkday |         N      mean       p50       min       max
          ---------+--------------------------------------------------
                 0 |         5       2.4         3         1         3
                 1 |         9         2         2         1         4
          ---------+--------------------------------------------------
             Total |        14  2.142857         2         1         4
          ------------------------------------------------------------
          
          
          
          * Summary by surveyor, then weekday vs weekend
          table surveyor_id is_wkday, c(mean raw_surveys) row col
          
          -------------------------------------
          surveyor_ |         is_wkday         
          id        |       0        1    Total
          ----------+--------------------------
                  1 |       3        2      2.5
                  2 |       3      1.5        2
                  3 |                2        2
                  4 |       1                 1
                  5 |                1        1
                  6 |       2        3      2.5
                  7 |       3  2.33333      2.5
                    | 
              Total |     2.4        2  2.14286
          -------------------------------------

          Comment


          • #6
            Thanks a lot, David. Your code is extremely helpful and is what I was looking for.

            Best,
            Rachita

            Comment

            Working...
            X