You can use the Amelia
package to impute the data (full disclosure: I am one of the authors of Amelia
). The package vignette has an extended example of how to use it to impute missing data.
It seems as though you have units which are district-gender-ageGroup observed at the monthly level. First you create a factor variable for each type of unit (that is, one level for each district-gender-ageGroup). Let's call this group
. Then, you would need a variable for time, which is probably the number of months since January 2003. Thus, this variable would be 13 in January of 2004. Call this variable time
. Amelia will allow you to impute based on the time trends with the following commands:
library(Amelia)
a.out <- amelia(my.data, ts = "time", cs = "group", splinetime = 2, intercs = TRUE)
The ts
and cs
arguments simply denote the time and unit variables. The splinetime
argument sets how flexible should time be used to impute the missing data. Here, a 2 means that the imputation will use a quadratic function of time, but higher values will be more flexible. The intercs
argument here tells Amelia to use a separate time trend for each district-gender-ageGroup. This adds many parameters to the model, so if you run into trouble, you can set this to FALSE
to try to debug.
In any event, this will get you imputations using the time information in your data. Since the missing data is bounded at zero, you can use the bounds
argument to force imputations into those logical bounds.
EDIT: How to create group/time variables
The time variable might be the easiest to create, because you just need to count from 2002 (assuming that is the lowest year in your data):
my.data$time <- my.data$Month + 12 * (my.data$Year - 2002)
The group variable is slightly harder but a quick way to do it is using the paste command:
my.data$group <- with(my.data,
as.factor(paste(District, Gender, AgeGroup, sep = ".")))
With these variables created, you want to remove the original variables from the imputation. To do that you can use the idvars
argument:
a.out <- amelia(my.data, ts = "time", cs = "group", splinetime = 2, intercs = TRUE,
idvars = c("District", "Gender", "Month", "Year", "AgeGroup"))
Best Answer
I used mice (multiple imputation by chained equation). It's fairly fast, and quite simple. I used it on 3000 obs. for c.a. 10 variables. Done in 10min on an old computer. Further, I believe it is one of the best multiple-imputation packages out there. It can use regression to impute, among other methods.
You need to create a dataframe with the variable you want to impute, and include every variable that might predict values of that variable (so every var. in your model + possibly other var. as well). The mice package will impute every missing value in that dataframe.
Simplest way of imputing. Gives you a dataframe
Datimp
that has five imputed data + the original data.A better way to do this is:
See if your imputations were any good:
Finally, and importantly. You can't just save your new dataset and use your imputed values as normal observed values. You use pooled regression or pooled lmer ...So the uncertainty of the imputed values is taken into account.