【问题标题】:Error in confidence interval mice R package置信区间小鼠R包中的错误
【发布时间】:2021-06-21 20:03:16
【问题描述】:

每个人我都在尝试执行 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)

最好的问候

【问题讨论】:

    标签: r confidence-interval imputation


    【解决方案1】:

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

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

    【讨论】:

    • 完美运行,我运行了代码,现在我可以看到每个方法的置信区间的下限和上限。
    猜你喜欢
    • 2012-09-13
    • 1970-01-01
    • 2012-03-07
    • 2018-01-16
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多