Search code examples
rimputation

Call amelia imputations in a function to calculate y-hat after multiple imputation


What I ultimately want to do is get y-hat scores after a multiple imputation, but Amelia does not provide fitted values. I have code that does it with a specific data set, but I'm trying to make a function that will do this regardless of the data set. Something like:

yhat<-function(a.out,num.obs,num.imp,model.qe){}

Where num.imp is the number of imputations used in amelia, or m. This is where I am stuck, though:

If a.out is an amelia object and names(a.out$imputations)[1] returns "imp1", why does a.out$imputations$names(a.out$imputations)[1] not return the same thing as a.out$imputations$"imp1"?

For a.out$imputations$names(a.out$imputations)[1], R says: Error: attempt to apply non-function.

How can I create a generic call for the individual imputations and the variables within?


Solution

  • Example taken from the Amelia docs

    library(Amelia)
    
    data(africa)
    a.out <- amelia(x = africa, cs = "country", ts = "year", logs = "gdp_pc")
    

    a.out$imputations is a list

    str(a.out$imputations)
    

    Elements in a list can be called either by name or numeric index

    head(a.out$imputations$imp1)
    head(a.out$imputations[["imp1"]])
    head(a.out$imputations[[1]])
    

    Each imputation is a data frame. A data frame is just a particular kind of list and elements can be called in that manner.

    head(a.out$imputations[[1]]$country)
    head(a.out$imputations[[1]][["country"]])
    head(a.out$imputations[[1]][[2]])
    head(a.out$imputations[[1]][2])
    

    The last of those methods differ from the rest in that it returns a one-column data frame, while the others return a vector.

    To extract an individual value entirely by numeric indices you could do f.ex

    a.out$imputations[[1]][3, 2]
    

    or

    a.out$imputations[[1]][[2]][3]