Multiple panel data imputation in R

3

I have panel data in the following format:

estado ano var1 var2 var3  

It turns out that variable 2 (var2) does not have data corresponding to one of the years. I tried multiple imputation with Amelia:

 out<-amelia(df,m=5,ts="ano",cs="estado",intercs=T, p2s=2,polytime = 2) 

The problem is that the code runs indefinitely and no result appears. I've used the code on other occasions and it worked.

    
asked by anonymous 11.07.2016 / 19:24

1 answer

3

I tried to replicate the problem by running it here

library(dplyr)
library(Amelia)

set.seed(123)
ufs <- c("AC", "AL", "AM", "AP", "BA", "CE", "DF", "ES", "GO", "MA", 
         "MG", "MS", "MT", "PA", "PB", "PE", "PI", "PR", "RJ", "RN", "RO", 
         "RR", "RS", "SC", "SE", "SP", "TO")
anos <- 2000:2015
df <- expand.grid(estado = ufs, ano = anos) %>% 
  mutate(var1 = rnorm(n()), var2 = rnorm(n()), var3 = rnorm(n())) %>% 
  mutate_at(vars(var1:var3), funs(ifelse(runif(n()) < .1, NA, .)))

(that is, I added 10% of NA's randomly in each variable)

# A tibble: 432 x 5
   estado   ano        var1        var2       var3
   <fctr> <int>       <dbl>       <dbl>      <dbl>
1      AC  2000 -0.56047565  0.30003855 -0.1632849
2      AL  2000 -0.23017749 -1.00563626  2.5530261
3      AM  2000  1.55870831  0.01925927 -1.8602276
4      AP  2000  0.07050839 -1.07742065  1.1310547
5      BA  2000  0.12928774  0.71270333 -0.5272343
6      CE  2000  1.71506499  1.08477509  1.6659909
7      DF  2000  0.46091621 -2.22498770         NA
8      ES  2000 -1.26506123  1.23569346  0.1436232
9      GO  2000 -0.68685285 -1.24104450 -1.0995509
10     MA  2000 -0.44566197  0.45476927  0.9035164
# ... with 422 more rows

I've circled your code exactly

out <- amelia(df,m=5,ts="ano",cs="estado",intercs=T, p2s=2,polytime=2)

And it worked! So I understand that you should do some sanity checks

  • Verify that ano is numeric .
  • Verify that estado is factor .
  • Check that all other variables are numeric .
  • Verify that the variables do not have too many NAs. Maybe that help.
12.07.2016 / 05:35