Announcement

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

  • How to find the average (mean) ?

    Hello everyone,

    I'm working with a dataset that includes variables named aar_before, bar_before, aar_after, and bar_after. My task involves generating four new variables: avg_aar_before, avg_bar_before, avg_aar_after, and avg_bar_after. The objective is to calculate the average values when the "during alert" condition is equal to 1, both for the before and after periods separately. The computed mean values should then be assigned to their respective generated variables. It's important to note that this process needs to be repeated for each instance where during alert equals 1 in the dataset.

    Thank you.


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(during_alert before_alert after_alert) float(aar_before bar_before aar_after bar_after)
    . 0 . .20162995  .04260506         .          .
    . 0 . .16719356  .05624989         .          .
    . 0 .  .1674138  .08249592         .          .
    . 0 . .19701643  .07580585         .          .
    . 0 . .20516405  .06666745         .          .
    . 0 . .14018941  .06387276         .          .
    . 0 . .09170595  .15080187         .          .
    . 0 . .11421962   .6173893         .          .
    . 0 .  .1097358   .5896134         .          .
    . 0 . .08595602   .5171338         .          .
    . 0 . .09839484   .1326193         .          .
    . 0 . .11376492 .009647326         .          .
    . 0 . .09372236 .018008037         .          .
    1 . .         .          .         .          .
    1 . .         .          .         .          .
    1 . .         .          .         .          .
    1 . .         .          .         .          .
    1 . .         .          .         .          .
    1 . .         .          .         .          .
    1 . .         .          .         .          .
    1 . .         .          .         .          .
    . . .         .          .         .          .
    . . .         .          .         .          .
    1 . .         .          .         .          .
    1 . .         .          .         .          .
    . . 2         .          . .11574279  .05181787
    . . 2         .          . .12310397   .1470303
    . . 2         .          . .15871197  .09303033
    . . 2         .          . .17397007  .09428933
    . . 2         .          . .19201784  .13814032
    . . 2         .          . .21083596  .06707024
    . . 2         .          . .06125252  .05421273
    . . 2         .          . .10629333  .06610985
    . . 2         .          .  .1511041  .06177737
    . . 2         .          .  .1929069   .0454968
    . . 2         .          .  .2569058  .03125316
    . . 2         .          .  .1495269 .065860316
    . . 2         .          . .14967504  .06347345
    . . 2         .          . .15980107  .06372144
    . . 2         .          . .17556715  .06438817
    . . 2         .          . .16668437  .06492964
    . . 2         .          . .15992224  .07937814
    . . 2         .          .  .1705421  .10644616
    . . 2         .          . .15192135  .05132644
    . . 2         .          . .20728523  .04138765
    . . 2         .          .  .2180869  .05178387
    . . 2         .          . .13485567  .06152795
    . . 2         .          . .16922466  .07246478
    . . 2         .          .  .1477594  .06227433
    . . 2         .          .  .1310007  .02980627
    . . 2         .          . .14637494  .04522794
    . . 2         .          . .17402603  .05784023
    . . 2         .          .  .1879326  .05905496
    . . 2         .          . .19642372  .05652686
    . . 2         .          . .18656853  .05157323
    . . .         .          .         .          .
    . . .         .          .         .          .
     .
    . . .         .          .         .          .
    . . .         .          .         .          .
    . . .         .          .         .          .
    . . .         .          .         .          .
    . . .         .          .         .          .
    . . .         .          .         .          .
    . . .         .          .         .          .
    . 0 . .18682347  .04846417         .          .
    . 0 . .18203865  .05410815         .          .
    . 0 . .19029137  .04600076         .          .
    . 0 . .18831116  .05591399         .          .
    . 0 .  .1826687  .04333993         .          .
    . 0 .  .1983744  .04931277         .          .
    . 0 .  .2106632   .0404925         .          .
    . 0 .  .2075484  .04315845         .          .
    . 0 .  .2073653  .04033995         .          .
    . 0 .  .2142975  .05041032         .          .
    . 0 .  .2089373  .04338308         .          .
    . 0 . .21928607  .05291648         .          .
    . 0 .  .2107713   .0503378         .          .
    . 0 .  .1781201  .05150358         .          .
    . 0 . .13056736  .05822437         .          .
    . 0 . .07920727  .25142273         .          .
    . 0 . .15313464   .6125321         .          .
    . 0 .  .3512289  .45004416         .          .
    . 0 .  .3157814   .0511076         .          .
    . 0 . .25571802   .5715104         .          .
    . 0 . .20302093  .04302251         .          .
    . 0 . .16239607  .04033545         .          .
    . 0 . .06975425  .03884154         .          .
    . 0 . .13212495  .03941067         .          .
    . 0 .  .1186699  .03726508         .          .
    . 0 . .10896995  .03512837         .          .
    . 0 . .07709385  .08729947         .          .
    . 0 .  .1267967 .021012183         .          .
    . 0 . .10955705 .020384533         .          .
    . 0 .  .1121116  .04369621         .          .
    1 . .         .          .         .          .
    . . .         .          .         .          .
    . . .         .          .         .          .
    . . .         .          .         .          .
    
    . . .         .          .         .          .
    . . .         .          .         .          .
    . . .         .          .         .          .
    . . .         .          .         .          .
    . . .         .          .         .          .
    . . .         .          .         .          .
    . . .         .          .         .          .
    . . .         .          .         .          .
    1 . .         .          .         .          .
    . . 2         .          . .16088095  .07666578
    . . 2         .          .  .1606727  .08362246
    . . 2         .          .  .1573334  .07126608
    . . 2         .          . .17286213  .04736475
    . . 2         .          . .20819326  .04837189
    . . 2         .          . .11442523  .05570864
    . . 2         .          . .13974726  .05125762
    . . 2         .          . .18766673  .05887006
    . . 2         .          .  .3325168  .03942842
    . . 2         .          .         .          .
    . . 2         .          .  .2620991  .09743398
    . . 2         .          .  .3336086  .09014753
    . . 2         .          .  .3330782  .03789013
    . . 2         .          .  .3600489 .033888653
    . . 2         .          . .13572246  .07926671
    . . 2         .          .  .2793817  .06111742
    . . 2         .          . .27600557  .05564579
    . . 2         .          .  .1557568  .04432066
    . . 2         .          . .18025704  .06572154
    . . 2         .          .  .1859729  .05975353
    . . 2         .          . .18460834  .02919079
    . . 2         .          . .30906445  .04670005
    . . 2         .          .  .2640435  .08850385
    . . 2         .          . .18724415  .05567451
    . . 2         .          .   .141274   .0551312
    . . 2         .          .  .1772512   .0409829
    . . 2         .          . .20125324  .10353238
    . . 2         .          . .26772735   .5892562
    . . 2         .          .   .337844  .26351514
    . . 2         .          .   .151304  1.0520319
    . . .         .          .         .          .
    . . .         .          .         .          .
    . . .         .          .         .          .
    . . .         .          .         .          .
    . . .         .          .         .          .
    . . .         .          .         .          .
    . . .         .          .         .          .
    end

  • #2
    The objective is to calculate the average values when the "during alert" condition is equal to 1
    The wording here is a little ambiguous, because when the "during alert" condition is 1, all else is missing in your example data. It seems like you might want the mean of aar_before and bar_before when before_alert equals 0 and the mean of aar_after and bar_after when after_alert equals 2 for continuous values of 0 and 2 respectively. If so, something like this should work:

    Code:
    gen original_order = _n
    gen switch = 1 if (before_alert == 0 & missing(before_alert[_n - 1])) ///
                             | (after_alert == 2 & missing(after_alert[_n-1]))
    gen sum = sum(switch) if before_alert ==0 | after_alert == 2
    foreach var in aar_before bar_before aar_after bar_after {
        bysort sum: egen avg_`var' = mean(`var') if !missing(sum)
    }
    sort original_order
    If not, let me know why not.

    Comment


    • #3
      Dear Daniel,

      I wanted to express my appreciation for your idea and code; they are spot-on and provide a viable method for calculating averages. Your work is commendable!

      I've only recently become aware of this approach, which has proven to be a great help. Thank you, Daniel.
      Last edited by reddy sudhar; 19 Nov 2023, 04:28.

      Comment

      Working...
      X