I am using Amelia and Zelig in R to do multiple imputation of my datasets with uncleaned variables. The reproducible dataset is in Zelig package.
require(Zelig)
require(Amelia)
data(freetrade)
a.out <- amelia(freetrade, m = 5, ts = "year", cs = "country")
I want to recode variable in 5 pooled datasets, for example :
> polity <- polity-1
Is th开发者_运维技巧ere any function that could automatically repeat 5 times for 5 MI datasets instead of calling each a.out$imputations[[1]]
, a.out$imputations[[2]]
..... and then procede the following analysis.
> z.out <- zelig(tariff ~ polity + pop + gdp.pc + year +
+ country, data = freetrade, model = "ls")
> summary(z.out)
Let me know if it makes sense. As required by Chase, above is the example from Zelig. But I used my own dataset as below:
require(Amelia)
a.out <- amelia(MIV5, m=5, idvars = c("STU_ID", "SCH_ID", "BYSTUWT", "BYRACE",
"F1SES2","F1TXMSTD", "F2HSSTAT", "BYTXMSTD", "BYURBAN",
"BYTXRSTD", "BYTXCSTD", "BYNELS2M", "BYNELS2R", "BYNELS0M",
"BYPISAME", "BYPISARE", "BYTXMIRR", "BYTXMQU"),
noms = c("BYSEX", "BYSTLANG", "F2B07", "F2EVRAPP"),
ords= c ("BYSTEXP","F1SES2QU"), p2c=0)
Now the thing is I have to recode and clean variables, such as converting "BYRACE" factor to a numeric "race", and getting a math gain score :
race <- as.numeric(BYRACE)
mthgn <- F1TXMSTD-BYTXMSTD
Thanks!
a.out$imputations <- lapply(a.out$imputations, transform, polity=polity-1)
a.out$imputations <- lapply(a.out$imputations, function(i) i[,'polity'] <- log(i[,'polity'])-1)
The imputed datasets are simply included as a list in the Amelia object. So lapply() should work.
精彩评论