[R] Generate missing data patterns

Andreas Wittmann andreas_wittmann at gmx.de
Wed Dec 2 15:29:29 CET 2009


Dear R-users,

i try to generate missing values in a matrix X according to a given 
missingnes pattern R with the probabilities p per row.

X<-matrix(rnorm(3*100),ncol=3)

## indicator matrix for missingnes (1 observed, 0 missing)
R<-matrix(c(1,1,1,
            0,0,1,
            1,1,0,
            0,1,1),ncol=3,byrow=TRUE)

## probabilities for row 1, row 2, row 3 and row 4
p<-c(0.375,0.25,0.25,0.125)

## does not exactly what i want, because i get rows
## of missinges pattern which are not in R
X[rbinom(100,1,p[1])==1,R[1,]==1] <- NA
X[rbinom(100,1,p[2])==1,R[2,]==1] <- NA
X[rbinom(100,1,p[3])==1,R[3,]==1] <- NA
X[rbinom(100,1,p[4])==1,R[4,]==1] <- NA

So it would be great if i can get any advice how to do this. I also 
tried rmultinom or sample but without any success so far.

Another question is what to to if want the missing pattern R and 
simulate a certain amount of missingnes mybe about 10 %? I guess i have 
to mix the probabilities for each row in such a way to get approximatly 
the wanted missingnes in percent.

best regards

Andreas




More information about the R-help mailing list