dan sawyer dan sawyer - 1 month ago 7
R Question

r class amelia object back to data.frame

Amelia is being used to impute missing values in a data.frame input. That process seems to be working. The input data set has column headers header_1, header_2, etc. The amelia command is:

imp1<-amelia(input, m=1, idvars = idvars)


The result from

str(imp1$imputations, 1)str(imp1$imputations, 1)


is:

$ imp1:'data.frame': 1725 obs. of 29 variables:
- attr(*, "class")= chr [1:2] "mi" "list"


The command

xxx<-as.data.frame(imp1[[1]])


works to create data frame, however the variable names, column headers have been renamed to

imp1.header_1
imp1.header_2
etc


What is the correct way to run amelia to retain the original variable, header names?
How can a data.frame with the original column headers be retrieved?

Answer

imp1$imputations contains a list of data frames. Because you specified m = 1, there's only one data frame there. So you can access it with imp1$imputations$imp1 and not worry about the as.data.frame call.

For example

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

head(a.out$imputations$imp1)

  year      country gdp_pc  infl trade    civlib population
1 1972 Burkina Faso    377 -2.92 29.69 0.5000000    5848380
2 1973 Burkina Faso    376  7.60 31.31 0.5000000    5958700
3 1974 Burkina Faso    393  8.72 35.22 0.3333333    6075700
4 1975 Burkina Faso    416 18.76 40.11 0.3333333    6202000
5 1976 Burkina Faso    435 -8.40 37.76 0.5000000    6341030
6 1977 Burkina Faso    448 29.99 41.11 0.6666667    6486870
Comments