Announcement

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

  • Interpolating for a maximum number of years

    Dear Sir/Madam,

    I currently have an unbalanced panel dataset regarding income inequality, where the variable d10 has about 5460 missing values. I have a variable for the country code, _ISO3N_ , and a variable for the year, year , where I would like to interpolate d10 for a maximum of 3 years within a country's observation, so within the same ISO3N code. Could you please help me with the commands as to how I can set the boundaries to limit the interpolation? Please let me know if there is any information missing, thank you very much in advance for your aid.

    Kind regards,

    Isabel Wessels

  • #2
    No data example here. By interpolation perhaps you mean linear interpolation, but the principle here applies to other methods.


    ipolate has no such handle. The only method I can think of interpolates everywhere possible and then throws away the result if the gap is too long.

    Here tsspell is from SSC and must be installed before you can use it. Or else you need some other way to calculate gap,length. Principles were discussed in https://www.stata-journal.com/articl...article=dm0029


    Code:
    . clear 
    
    . set obs 16 
    Number of observations (_N) was 0, now 16.
    
    . gen t = _n 
    
    . gen id = 1 
    
    . gen whatever = real(word("1 2 . 4 . . 7 . . . 11 . . . . 16"), _n)
    (10 missing values generated)
    
    . 
    . tsset id t 
    
    Panel variable: id (strongly balanced)
     Time variable: t, 1 to 16
             Delta: 1 unit
    
    . tsspell, cond(missing(whatever))
    
    . egen length = max(_seq), by(id _spell)
    
    . 
    . l, sepby(id _spell) 
    
         +----------------------------------------------------+
         |  t   id   whatever   _seq   _spell   _end   length |
         |----------------------------------------------------|
      1. |  1    1          1      0        0      0        0 |
      2. |  2    1          2      0        0      0        0 |
         |----------------------------------------------------|
      3. |  3    1          .      1        1      1        1 |
         |----------------------------------------------------|
      4. |  4    1          4      0        0      0        0 |
         |----------------------------------------------------|
      5. |  5    1          .      1        2      0        2 |
      6. |  6    1          .      2        2      1        2 |
         |----------------------------------------------------|
      7. |  7    1          7      0        0      0        0 |
         |----------------------------------------------------|
      8. |  8    1          .      1        3      0        3 |
      9. |  9    1          .      2        3      0        3 |
     10. | 10    1          .      3        3      1        3 |
         |----------------------------------------------------|
     11. | 11    1         11      0        0      0        0 |
         |----------------------------------------------------|
     12. | 12    1          .      1        4      0        4 |
     13. | 13    1          .      2        4      0        4 |
     14. | 14    1          .      3        4      0        4 |
     15. | 15    1          .      4        4      1        4 |
         |----------------------------------------------------|
     16. | 16    1         16      0        0      0        0 |
         +----------------------------------------------------+
    
    . 
    . ipolate whatever t, gen(better) by(id)
    
    . replace better = . if length > 3 
    (4 real changes made, 4 to missing)
    
    . 
    . list, sep(0)
    
         +-------------------------------------------------------------+
         |  t   id   whatever   _seq   _spell   _end   length   better |
         |-------------------------------------------------------------|
      1. |  1    1          1      0        0      0        0        1 |
      2. |  2    1          2      0        0      0        0        2 |
      3. |  3    1          .      1        1      1        1        3 |
      4. |  4    1          4      0        0      0        0        4 |
      5. |  5    1          .      1        2      0        2        5 |
      6. |  6    1          .      2        2      1        2        6 |
      7. |  7    1          7      0        0      0        0        7 |
      8. |  8    1          .      1        3      0        3        8 |
      9. |  9    1          .      2        3      0        3        9 |
     10. | 10    1          .      3        3      1        3       10 |
     11. | 11    1         11      0        0      0        0       11 |
     12. | 12    1          .      1        4      0        4        . |
     13. | 13    1          .      2        4      0        4        . |
     14. | 14    1          .      3        4      0        4        . |
     15. | 15    1          .      4        4      1        4        . |
     16. | 16    1         16      0        0      0        0       16 |
         +-------------------------------------------------------------+

    Comment

    Working...
    X