[R] multiple imputation manually

Daisy Englert Duursma daisy.duursma at gmail.com
Wed Feb 9 00:24:15 CET 2011


Hello,

I don't really understand you question but if you want to run the same
code four or five times on the same dataset you could write it into a
for loop where yourread in your incomplete dataset back in at the
beginning. A better practice is to change the name of a dataset  when
you make changes especially if you are going to reuse the dataset
later on.

for (x in c(1:5)) {

read in file

your code

}

Cheers

On Mon, Feb 7, 2011 at 8:27 PM, Sarah <s1327720 at student.rug.nl> wrote:
>
> Hi,
>
> I want to impute the missing values in my data set multiple times, and then
> combine the results (like multiple imputation, but manually) to get a mean
> of the parameter(s) from the multiple imputations. Does anyone know how to
> do this?
>
> I have the following script:
> y1 <- rnorm(20,0,3)
> y2 <- rnorm(20,3,3)
> y3 <- rnorm(20,3,3)
> y4 <- rnorm(20,6,3)
> y <- c(y1,y2,y3,y4)
> x1 <- 1+2*y1+ rnorm(20,0,8)
> x2 <- 1+2*y2+ rnorm(20,0,8)
> x3 <- 1+2*y3+ rnorm(20,0,8)
> x4 <- 1+2*y4+ rnorm(20,0,8)
> x <- c(x1,x2,x3,x4)
> mcar.y <- rep(NA,80)
> y.mis <- rep(NA,80)
> df <- data.frame(y=y, y.mis=y.mis, mcar.y=mcar.y, x=x)
> df$y.mis <- df$y
> for (j in 1:80)
> {
> df$mcar.y <- rbinom(80,1,0.15)
> }
> ind0 <- which(df$mcar.y==0)
> ind1 <- which(df$mcar.y==1)
> if (length(ind0) > 68) {
> df$mcar.y[sample(ind0, length(ind0) - 68)] <- 1
> } else {
> df$mcar.y[sample(ind1, 68 - length(ind0))] <- 0
> }
> df$y.mis[df$mcar.y==1] <- NA
>
> This gives me data sets with missing values completely at random. Now I
> would like to apply single imputation:
>
> library(Hmisc)
> lm.y <- lm(df$y.mis~df$x,data=df); lm.y
> library(arm)
> pred.y <- rnorm(length(df$y), predict (lm.y, df), sigma.hat(lm.y))
> y.imp<- df$y.mis
> impute <- function (y, y.impute)
> {
> ifelse (is.na(y), y.impute, y)
> }
> y.imp <- impute (y.imp, pred.y)
> df <- data.frame(df$y, df$y.mis, pred.y, y.imp, x)
>
> and repeat this imputation process a couple of times (say, 5 times) for each
> data set. If I, however, have run this imputation-script (for 1 incomplete
> data set), my data set is already complete. I would like to get back to the
> incompleted data set used before, and repeat the single imputation process
> four times with the same incomplete data set (so I can calculate some mean
> of parameters from the 5 imputed data sets later on). But how?
>
> Thanks.
> --
> View this message in context: http://r.789695.n4.nabble.com/multiple-imputation-manually-tp3263786p3263786.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>



-- 
Daisy Englert Duursma
Department of Biological Sciences
Room E8C156
Macquarie University, North Ryde, NSW 210
Australia

Tel +61 2 9850 9256



Unit 2, 35 Denison St
Hornsby, NSW 2077
Mobile: 0421858456



More information about the R-help mailing list