2

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 there 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!

MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
Lily
  • 637
  • 2
  • 7
  • 16
  • Trying to run `a.out` yields `Error in amelia(MIV5, m = 5, idvars = c("STU_ID", "SCH_ID", "BYSTUWT", : object 'MIV5' not found`. Please update question with reproducible example. – Chase Apr 13 '11 at 14:55
  • Chase: Because I used my own dataset. Let me revise it use the example from Zelig. – Lily Apr 13 '11 at 15:20
  • Isn't that an oxymoron? How are we supposed to find MIV5 if it is your own dataset? `Amelia` looks like it comes with two datasets, `africa` and `freetrade`...those I can definitely find since they come up the package...as far as I can tell, `MIV5` only exists on your machine. – Chase Apr 13 '11 at 15:25
  • @Lily : make sure you accept correct answers on your questions by clicking the V sign on the left of it. See also http://stackoverflow.com/faq – Joris Meys Apr 13 '11 at 15:31
  • Hi Chase: I have revised my example. Sorry for the confusion. I am a beginner using R. – Lily Apr 13 '11 at 15:31
  • @Lily do you want to subtract 1 from `a.out$imputations[[n]]$polity` where `n` is `5` in your example, and then use those recoded, imputed data sets in 5 separate calls to `zelig()`. Is that what you are struggling with? – Gavin Simpson Apr 13 '11 at 15:50

1 Answers1

4
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.

Vincent
  • 15,809
  • 7
  • 37
  • 39
  • Hi Vincent: It works works!!!!! Thank you so much! Btw, could you let me know why you use "transform"? If I want to get a mean or compute a standardized score, do I also use "transform"? – Lily Apr 14 '11 at 00:43
  • Happy to hear it works :) I just used transform because it's a convenient shorthand. Nothing special there. I edited my answer to give another example with a log transformation. That should give you a better sense of what you can do with lapply(). If you haven't already, try to read up on the apply family of functions. They're quite handy. – Vincent Apr 14 '11 at 01:36
  • Vincent: really appreciate your prompt reply. I was wondering what tutorials do you recommend for a beginner to learn functions? I am actually reading Zelig. The language in Zelig manual is much more understandable to me. – Lily Apr 14 '11 at 02:03
  • There are many threads on that here. Search for "R books". But you can start with the Quick-R website: http://www.statmethods.net/ Then, the book "Data Manipulation with R" by Phil Spector is pretty good at showing the basics such as subscripting and apply() functions. If you're affiliated to an academic institution you can get the Spector book (and the whole Use R series of books) for free through the springerlink website. – Vincent Apr 14 '11 at 03:02
  • Thanks. I have been following Quick-R since I started learning R.It is helpful for beginners. Thanks again! – Lily Apr 15 '11 at 17:52