Announcement

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

  • How to fill missing baseline values backwards

    In my dataset, baseline values are missing for the "ur_pot" variable. I want to fill using the value before the baseline. I would like to know how to implement this.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float patient_id byte time double urpot
     1  0    .
     1  3 17.8
     1  6   19
     1 12    .
     1 18    .
     1 24 32.7
     1 36 19.7
     1 48 19.6
     2  0    .
     2  3 11.7
     2  6    .
     2 12 11.6
     2 18 12.7
     2 24 13.5
     2 36   11
     2 48 12.8
     3  0    .
     3  3  5.7
     3  6  5.3
     3 12  6.9
     3 18  5.1
     3 24  5.8
     3 36    7
     3 48  6.5
     4  0    .
     4  3  9.7
     4  6 10.3
     4 12    .
     4 18    .
     4 24    .
     4 36    .
     4 48    .
     5  0    .
     5  3 17.9
     5  6 12.3
     5 12    .
     5 18   25
     5 24 16.1
     5 36 10.5
     5 48    .
     6  0    .
     6  3 17.7
     6  6 11.5
     6 12 16.3
     6 18 12.4
     6 24 17.8
     6 36 13.2
     6 48 12.2
     7  0    .
     7  3   29
     7  6 24.5
     7 12 22.9
     7 18    .
     7 24   22
     7 36 25.4
     7 48 24.2
     8  0    .
     8  3 10.2
     8  6 26.9
     8 12 18.4
     8 18 19.2
     8 24 24.1
     8 36 23.2
     8 48 24.9
     9  0    .
     9  3  5.2
     9  6 14.1
     9 12 22.3
     9 18 11.5
     9 24 10.8
     9 36   17
     9 48   10
    10  0    .
    10  3    .
    10  6    .
    10 12    .
    10 18    .
    10 24    .
    10 36 10.5
    10 48    7
    11  0    .
    11  3 16.5
    11  6 14.2
    11 12 15.7
    11 18 14.6
    11 24 15.7
    11 36 13.1
    11 48 16.5
    12  0    .
    12  3 11.5
    12  6 11.8
    12 12 14.2
    12 18 14.1
    12 24  8.7
    12 36 12.9
    12 48 15.3
    13  0    .
    13  3 10.7
    13  6 13.6
    13 12 15.3
    end

  • #2
    In this multidisciplinary forum, you are much more likely to recieve a good answer, if you can explain your problem without using domain-specific terms. I don't know what the baseline is in your data, but you should likely use some form of [_n-1], e.g. 'replace ur_pot = ur_pot[_n-1]'.

    See here for more: https://www.stata.com/support/faqs/d...issing-values/

    Comment


    • #3
      Al:
      what you might have in mind can be (in part) obtained via the following:
      Code:
      . bysort patient_id (time): replace urpot=urpot[_n+1] if urpot[_n]==.
      (17 real changes made)
      
      . bysort patient_id (time): replace urpot=urpot[_n+1] if urpot[_n]==.
      (2 real changes made)
      
      . bysort patient_id (time): replace urpot=urpot[_n+1] if urpot[_n]==.
      (1 real change made)
      
      . list
      
           +-------------------------+
           | patien~d   time   urpot |
           |-------------------------|
        1. |        1      0    17.8 |
        2. |        1      3    17.8 |
        3. |        1      6      19 |
        4. |        1     12    32.7 |
        5. |        1     18    32.7 |
           |-------------------------|
        6. |        1     24    32.7 |
        7. |        1     36    19.7 |
        8. |        1     48    19.6 |
        9. |        2      0    11.7 |
       10. |        2      3    11.7 |
           |-------------------------|
       11. |        2      6    11.6 |
       12. |        2     12    11.6 |
       13. |        2     18    12.7 |
       14. |        2     24    13.5 |
       15. |        2     36      11 |
           |-------------------------|
       16. |        2     48    12.8 |
       17. |        3      0     5.7 |
       18. |        3      3     5.7 |
       19. |        3      6     5.3 |
       20. |        3     12     6.9 |
           |-------------------------|
       21. |        3     18     5.1 |
       22. |        3     24     5.8 |
       23. |        3     36       7 |
       24. |        3     48     6.5 |
       25. |        4      0     9.7 |
           |-------------------------|
       26. |        4      3     9.7 |
       27. |        4      6    10.3 |
       28. |        4     12       . |
       29. |        4     18       . |
       30. |        4     24       . |
           |-------------------------|
       31. |        4     36       . |
       32. |        4     48       . |
       33. |        5      0    17.9 |
       34. |        5      3    17.9 |
       35. |        5      6    12.3 |
           |-------------------------|
       36. |        5     12      25 |
       37. |        5     18      25 |
       38. |        5     24    16.1 |
       39. |        5     36    10.5 |
       40. |        5     48       . |
           |-------------------------|
       41. |        6      0    17.7 |
       42. |        6      3    17.7 |
       43. |        6      6    11.5 |
       44. |        6     12    16.3 |
       45. |        6     18    12.4 |
           |-------------------------|
       46. |        6     24    17.8 |
       47. |        6     36    13.2 |
       48. |        6     48    12.2 |
       49. |        7      0      29 |
       50. |        7      3      29 |
           |-------------------------|
       51. |        7      6    24.5 |
       52. |        7     12    22.9 |
       53. |        7     18      22 |
       54. |        7     24      22 |
       55. |        7     36    25.4 |
           |-------------------------|
       56. |        7     48    24.2 |
       57. |        8      0    10.2 |
       58. |        8      3    10.2 |
       59. |        8      6    26.9 |
       60. |        8     12    18.4 |
           |-------------------------|
       61. |        8     18    19.2 |
       62. |        8     24    24.1 |
       63. |        8     36    23.2 |
       64. |        8     48    24.9 |
       65. |        9      0     5.2 |
           |-------------------------|
       66. |        9      3     5.2 |
       67. |        9      6    14.1 |
       68. |        9     12    22.3 |
       69. |        9     18    11.5 |
       70. |        9     24    10.8 |
           |-------------------------|
       71. |        9     36      17 |
       72. |        9     48      10 |
       73. |       10      0       . |
       74. |       10      3       . |
       75. |       10      6       . |
           |-------------------------|
       76. |       10     12    10.5 |
       77. |       10     18    10.5 |
       78. |       10     24    10.5 |
       79. |       10     36    10.5 |
       80. |       10     48       7 |
           |-------------------------|
       81. |       11      0    16.5 |
       82. |       11      3    16.5 |
       83. |       11      6    14.2 |
       84. |       11     12    15.7 |
       85. |       11     18    14.6 |
           |-------------------------|
       86. |       11     24    15.7 |
       87. |       11     36    13.1 |
       88. |       11     48    16.5 |
       89. |       12      0    11.5 |
       90. |       12      3    11.5 |
           |-------------------------|
       91. |       12      6    11.8 |
       92. |       12     12    14.2 |
       93. |       12     18    14.1 |
       94. |       12     24     8.7 |
       95. |       12     36    12.9 |
           |-------------------------|
       96. |       12     48    15.3 |
       97. |       13      0    10.7 |
       98. |       13      3    10.7 |
       99. |       13      6    13.6 |
      100. |       13     12    15.3 |
           +-------------------------+
      
      .
      .
      As you can see, for some -id- the code does not work and should be tweaked.
      Otherwise, you may want to take a look at -help ipolate-.
      Last edited by Carlo Lazzaro; 09 May 2023, 03:56.
      Kind regards,
      Carlo
      (Stata 19.0)

      Comment


      • #4
        I agree with Emil Alnor in not knowing what "baseline" means here. But if it's filling backwards you want, this is covered in the FAQ that Emil cites. But it's not obvious that that would be better here than some kind of interpolation, or even than leaving the data as they come.


        Code:
        gen negtime = -time 
        clonevar urpot_c = urplot 
        bysort patient_id (negtime) : replace urpot_c = urpot_c[_n-1] if missing(urpot_c) 
        sort patient_id time

        Comment


        • #5
          Should be urpot in #4, whatever it means (I would probably rather not know).

          Comment


          • #6
            Thank you for the solution. I noted the comments about explaining terms and will do better the next time.

            Comment

            Working...
            X