Hi,
I am using lagged and forward commands with panel data. In my data set I have some missing values, and I want to create new variables that imputes values both forwards and backwards. In the example, I show below, I impute values very simply, either add 1 to the previous value or subtract 1 from the succeeding value.
As you will see, adding 1 to the previous value will automatically fill in, but subtracting 1 from the succeeding value does not.
Notice that the variable forward_value automatically fills in the values. However, if I impute the values backward:
Notice here that backward_value does not automatically fill in. There are still some missing values. How do I change this so that I can automatically fill in backwards as well?
Thank you for your help in advance!
Vincent
I am using lagged and forward commands with panel data. In my data set I have some missing values, and I want to create new variables that imputes values both forwards and backwards. In the example, I show below, I impute values very simply, either add 1 to the previous value or subtract 1 from the succeeding value.
As you will see, adding 1 to the previous value will automatically fill in, but subtracting 1 from the succeeding value does not.
Code:
. clear . input str8 country year value country year value 1. "Cambodia" 2007 5 2. "Cambodia" 2008 . 3. "Cambodia" 2009 . 4. "Cambodia" 2010 . 5. "Cambodia" 2011 10 6. "China" 2007 100 7. "China" 2008 120 8. "China" 2009 . 9. "China" 2010 . 10. "China" 2011 100 11. end . . list, sepby(country) +-------------------------+ | country year value | |-------------------------| 1. | Cambodia 2007 5 | 2. | Cambodia 2008 . | 3. | Cambodia 2009 . | 4. | Cambodia 2010 . | 5. | Cambodia 2011 10 | |-------------------------| 6. | China 2007 100 | 7. | China 2008 120 | 8. | China 2009 . | 9. | China 2010 . | 10. | China 2011 100 | +-------------------------+ . . encode country, generate(country_id) . tsset country_id year panel variable: country_id (strongly balanced) time variable: year, 2007 to 2011 delta: 1 unit . . /*Forward Impute Values*/ . gen forward_value = value (5 missing values generated) . replace forward_value = L.forward_value + 1 if mi(value) (5 real changes made) . . list, sepby(country) +-----------------------------------------------+ | country year value country_id forward_value | |-----------------------------------------------| 1. | Cambodia 2007 5 Cambodia 5 | 2. | Cambodia 2008 . Cambodia 6 | 3. | Cambodia 2009 . Cambodia 7 | 4. | Cambodia 2010 . Cambodia 8 | 5. | Cambodia 2011 10 Cambodia 10 | |-----------------------------------------------| 6. | China 2007 100 China 100 | 7. | China 2008 120 China 120 | 8. | China 2009 . China 121 | 9. | China 2010 . China 122 | 10. | China 2011 100 China 100 | +-----------------------------------------------+
Code:
. . . /*Backward Impute Values*/ . *For some reason this doesn't fill automatically . gen backward_value = value (5 missing values generated) . replace backward_value = F.backward_value - 1 if mi(value) (2 real changes made) . . list, sepby(country) +----------------------------------------------------------+ | country year value country_id forward_value backward_value | |----------------------------------------------------------| 1. | Cambodia 2007 5 Cambodia 5 5 | 2. | Cambodia 2008 . Cambodia 6 . | 3. | Cambodia 2009 . Cambodia 7 . | 4. | Cambodia 2010 . Cambodia 8 9 | 5. | Cambodia 2011 10 Cambodia 10 10 | |----------------------------------------------------------| 6. | China 2007 100 China 100 100 | 7. | China 2008 120 China 120 120 | 8. | China 2009 . China 121 . | 9. | China 2010 . China 122 99 | 10. | China 2011 100 China 100 100 | +----------------------------------------------------------+
Thank you for your help in advance!
Vincent
Comment