Announcement

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

  • calculate mean with missing value and score of each variables that they calculated from

    I want to get average of L1-L4 without missing value and also get the score of what variable from L1-L4 they calculated from (L1=1, L2=2, L3=3, L4=4)? For example, ID1=. , ID2 average L1-L4 = (0.83+0.96+1.02)/3 and the score is 2+3+4 = 9 (because we did not use L1 as it is a missing value) or ID 3 average= (0.83+0.885+0.892_0.874)/4 with the score of 1+2+3+4 = 10 as we used every single data. ID8 average= (1.035+1.226+1.214)/3 with the score = 1+2+4 = 7 (L3 was missing)


    input double(L1 L2 L3 L4)
    . . . .
    . .83 .961 1.022
    .83 .885 .892 .874
    .774 .779 .897 .967
    1.222 . . 1.382
    .775 .943 1.017 1.129
    .979 1.087 1.041 1.046
    1.035 1.226 . 1.214

    I think I know the code to calculate average but how can I generate the score?

    gen n_= !missing(L1)+ !missing(L2)+ !missing(L3)+ !missing(L4)

    foreach i in L1 L2 L3 L4{
    replace `i' =. if `i'==0
    }
    egen mean_L = rowmean(L1 L2 L3 L4)

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double(L1 L2 L3 L4)
        .     .     .     .
        .   .83  .961 1.022
      .83  .885  .892  .874
     .774  .779  .897  .967
    1.222     .     . 1.382
     .775  .943 1.017 1.129
     .979 1.087 1.041 1.046
    1.035 1.226     . 1.214
    end
    
    egen wanted1= rowmean(L1-L4)
    gen wanted2= !missing(L1) + (2*!missing(L2))+ (3*!missing(L3))+ (4*!missing(L4))
    Res.:

    Code:
    . l, sep(0)
    
         +----------------------------------------------------+
         |    L1      L2      L3      L4    wanted1   wanted2 |
         |----------------------------------------------------|
      1. |     .       .       .       .          .         0 |
      2. |     .     .83    .961   1.022   .9376667         9 |
      3. |   .83    .885    .892    .874     .87025        10 |
      4. |  .774    .779    .897    .967     .85425        10 |
      5. | 1.222       .       .   1.382      1.302         5 |
      6. |  .775    .943   1.017   1.129       .966        10 |
      7. |  .979   1.087   1.041   1.046    1.03825        10 |
      8. | 1.035   1.226       .   1.214   1.158333         7 |
         +----------------------------------------------------+

    Comment

    Working...
    X