Skip to content

Instantly share code, notes, and snippets.

@njtierney
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save njtierney/0c30459844ce7d2d2962 to your computer and use it in GitHub Desktop.
Save njtierney/0c30459844ce7d2d2962 to your computer and use it in GitHub Desktop.
create MCAR data (from package `mi`).
mi:::.create.missing
function (data, pct.mis = 10)
{
n <- nrow(data)
J <- ncol(data)
if (length(pct.mis) == 1) {
n.mis <- rep((n * (pct.mis/100)), J)
}
else {
if (length(pct.mis) < J)
stop("The length of missing does not equal to the column of the data")
n.mis <- n * (pct.mis/100)
}
for (i in 1:ncol(data)) {
if (n.mis[i] == 0) {
data[, i] <- data[, i]
}
else {
data[sample(1:n, n.mis[i], replace = FALSE), i] <- NA
}
}
return(as.data.frame(data))
}
<environment: namespace:mi>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment