Multiple imputation with dashboard data in R

Asked

Viewed 634 times

3

I have panel data in the following format:

estado ano var1 var2 var3  

It turns out that variable 2 (var2) has no data corresponding to one of the years. I tried to perform 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 have used the code on other occasions and it worked.

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 randomly to 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 ran 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 make some Sanity checks

  • Check whether ano is numeric.
  • Check whether estado is factor.
  • Check if the other variables are numeric.
  • Check if variables do not have Nas in the other. Maybe that help.
  • Hi Julio, thank you. I remade and in fact, one of the variables was poorly classified. Vlw.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.