[R] manipulating multiply imputed data sets

Donald Braman dbraman at law.gwu.edu
Wed May 28 23:17:34 CEST 2008


Thanks -- that's very helpful! -Don

On Wed, May 28, 2008 at 4:34 PM, John Fox <jfox at mcmaster.ca> wrote:
> Dear Donald,
>
> I can't guarantee that there aren't other problems, but your call to
> update() is in error; you need
>
> allmis <- update(allmis, white = recode(RACE, '1=1; else=0; '))
>
> not
>
> allmis <- update(allmis, white <- recode(RACE, '1=1; else=0; '))
>
> [The last ; in the recode specification is unnecessary, but should do no
> harm; as well, for a simple recode like this you might prefer ifelse() to
> recode().]
>
> Here's an example:
>
> --------- snip ---------
>
>> library(mitools)
>> library(car)
>> data(smi)
>> smi <- update(smi, sex=recode(sex, "0 = 'M'; 1 = 'F'"))
>> with(smi, table(sex, drkfre))
> [[1]]
>   drkfre
> sex Non drinker not in last wk <3 days last wk >=3 days last wk
>  F         207            194             134               35
>  M         282            201             105               12
>
> [[2]]
>   drkfre
> sex Non drinker not in last wk <3 days last wk >=3 days last wk
>  F         200            200             132               38
>  M         282            195             109               14
>
> . . .
>
> attr(,"call")
> with.imputationList(smi, table(sex, drkfre))
>
> ----------- snip -----------
>
> I hope this helps,
>  John
>
> ------------------------------
> John Fox, Professor
> Department of Sociology
> McMaster University
> Hamilton, Ontario, Canada
> web: socserv.mcmaster.ca/jfox
>
>> -----Original Message-----
>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> On
>> Behalf Of Donald Braman
>> Sent: May-28-08 2:21 PM
>> To: r-help at r-project.org
>> Subject: [R] manipulating multiply imputed data sets
>>
>> Hi folks,
>>
>> I have five imputed data sets and would like to apply the same
>> recoding routines to each.  I could do this sort of thing pretty
>> easily in Stata using MIM, but I've decided to go cold turkey on other
>> stats packages as a incentive for learning more about R.  Most of the
>> recoding is for nominal variables, like race, religion, urbanicity,
>> and the like.  So, for example, to recode race for my first dataset,
>> inmi1, I would do the following:
>>
>> miset1$white  <- recode(miset1$RACE, '1=1; else=0; ')
>> miset1$black  <- recode(miset1$RACE, '2=1; else=0; ')
>> miset1$asian  <- recode(miset1$RACE, '3=1; else=0; ')
>> miset1$hispanic <- recode(miset1$RACE, '4=1; else=0; ')
>> miset1$raceother <- recode(miset1$RACE, '5=1; else=0; ')
>>
>> I've tried a number of variations, e.g., on the following using recode
>> (from the car package) with imputationList (from the mitools package),
>> though without success:
>>
>> files.allmisets <- list.files(getwd(),pattern="miset*.csv$",full=TRUE)
>> allmis <- imputationList(lapply(files.allmisets, read.csv))
>> allmis <- update(allmis, white <- recode(RACE, '1=1; else=0; '))
>>
>> I've also tried some basic loops.  I guess I'm also a bit confused as
>> to when R references the original object and when it creates a new
>> one. I suppose I could do this in Python and the use PyR, but I'd
>> really like to learn a bit more about how R syntax.
>>
>> Any help on this specific problem or general advice on manipulating
>> data in multiply imputed datasets in R would be much appreciated.
>>
>> --
>> Donald Braman
>> http://www.law.gwu.edu/Faculty/profile.aspx?id=10123
>> http://research.yale.edu/culturalcognition
>> http://ssrn.com/author=286206
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
>



-- 
Donald Braman
http://www.law.gwu.edu/Faculty/profile.aspx?id=10123
http://research.yale.edu/culturalcognition
http://ssrn.com/author=286206



More information about the R-help mailing list