Announcement

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

  • function for partial mean over group

    I would like to request to kindly help me by sharing appropriate state function for creating a new variable which contains partial mean :

    1. A1 = w1/1 . A2 = (w1+w2)/2 . A3 = (w1+w2+w3)/3. ... An= (w1+w2..+wn)/n


    2. if I have a single variable that has groups of data like A= A1.. An, B= B1... Bn, C= C1.. Cn. then how the partial mean of the three variables be calculated using stata functions.

    Looking forward to getting kind help.

    Regards

  • #2
    The code depends on how your data are organized. It is unclear what you mean by "a single variable that has groups of data like A = A1..AN..." How do you know where one group ends and the next group begins? Don't try to just explain it in words. Please show example data along with your explanation. Use the -dataex- command to do that.

    If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      Dear Sir,

      Apologies for creating confusion. Here is the example data set calculated in spread sheet.
      Group= is A or B
      Sl no is the sl no of participants
      value is the value assigned to each participant
      mu= mean of all the variables
      mui uses the formula that I had shared, but have not clearly spelled out.

      mui in first row= value in the first row
      mui in the second row = (value in the first row+ value in the second row ) divided by 2; (5+15)/2
      mui in the third row = (value in the first row+ value in the second row+ value in the third row)/3; (5+15+20)/3
      and so on...


      However, when i want to create a function in stata that can give me the two means each for one group and calculate the mui which is given in the table 2
      Table 1
      Group sl no value mui mu-mui
      A 1 5 5 30.9
      A 2 15 10 25.9
      A 3 20 13.3 22.6
      A 4 35 18.8 17.2
      B 5 35 22.0 13.9
      B 6 35 24.2 11.7
      B 7 40 26.43 9.5
      B 8 45 28.75 7.2
      B 9 50 31.11 4.8
      B 10 55 33.50 2.4
      B 11 60 35.91 0.0
      mu 35.90909091

      Table 2:
      Group sl no value mui mu-mui
      A 1 5 5 13.8
      A 2 15 10 8.8
      A 3 20 13.3 5.4
      A 4 35 18.8 0.0
      B 5 35 35 10.7
      B 6 35 35 10.7
      B 7 40 36.7 9.0
      B 8 45 38.8 7.0
      B 9 50 41.0 4.7
      B 10 55 43.3 2.4
      B 11 60 45.7 0.0
      muA 18.8
      MuB 45.7
      I am new to STATA and using STATA 18 and have not tried dataex, but will try sir.
      However, it will be great help to get the response from you.

      Regards
      Lincoln

      Comment


      • #4
        Code:
        bysort group (slno) : gen wanted = sum(value) / sum(value < .)
        where you may well need to adjust to your real variable names.

        Please read the FAQ Advice before posting.

        Comment


        • #5
          Thanks

          Comment

          Working...
          X