Search code examples
rconfidence-intervalimputation

Error in confidence interval mice R package


everyone I am trying to execute the code in found in the book "Flexible Imputation of Missing Data 2ed" in 2.5.3 section, that calculates a confidence interval for two imputation methods. The problem is that I cannot reproduce the results as the result is always NaN

Here is the code

require(mice)

# function randomly draws artificial data from the specified linear model

create.data <- function(beta = 1, sigma2 = 1, n = 50, run = 1) {
  set.seed(seed = run)
  x <- rnorm(n)
  y <- beta * x + rnorm(n, sd = sqrt(sigma2))
  cbind(x = x, y = y)
}

#Remove some data

make.missing <- function(data, p = 0.5){
  rx <- rbinom(nrow(data), 1, p)
  data[rx == 0, "x"] <- NA
  data
}

# Apply Rubin’s rules to the imputed data

test.impute <- function(data, m = 5, method = "norm", ...) {
  imp <- mice(data, method = method, m = m, print = FALSE, ...)
  fit <- with(imp, lm(y ~ x))
  tab <- summary(pool(fit), "all", conf.int = TRUE)
  as.numeric(tab["x", c("estimate", "2.5 %", "97.5 %")])
}

#Bind everything together

simulate <- function(runs = 10) {
  res <- array(NA, dim = c(2, runs, 3))
  dimnames(res) <- list(c("norm.predict", "norm.nob"),
                        as.character(1:runs),
                        c("estimate", "2.5 %","97.5 %"))
  for(run in 1:runs) {
    data <- create.data(run = run)
    data <- make.missing(data)
    res[1, run, ] <- test.impute(data, method = "norm.predict",
                                 m = 2)
    res[2, run, ] <- test.impute(data, method = "norm.nob")
  }
  res
}

res <- simulate(1000)

#Estimate the lower and upper bounds of the confidence intervals per method

apply(res, c(1, 3), mean, na.rm = TRUE)

Best Regards


Solution

  • Replace "x" by tab$term == "x" in the last line of test.impute():

    as.numeric( tab[ tab$term == "x", c("estimate", "2.5 %", "97.5 %")])