置信区间 mice R 包中的错误

Error in confidence interval mice R package

我尝试执行的每个人都可以在 2.5.3 部分的“Flexible Imputation of Missing Data 2ed”一书中找到代码,该代码计算两种插补方法的置信区间。问题是我无法重现结果,因为结果总是 NaN

这是代码

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)

此致

test.impute()的最后一行用tab$term == "x"替换"x"

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