【问题标题】:Include ID variable in imputed data frame在估算的数据框中包含 ID 变量
【发布时间】:2019-05-01 18:25:05
【问题描述】:

我正在使用library(mice) 来估算丢失的数据。我想要一种方法告诉mice ID 变量应该包含在插补数据集中,但不用于插补。

例如

#making a silly data frame with missing data
library(tidyverse)
library(magrittr)
library(mice)

d1 <- data.frame(
  id = str_c(
    letters[1:20] %>% 
      rep(each = 5),
    1:5 %>% 
      rep(times  = 20)
    ),
  v1 = runif(100),
  v2 = runif(100),
  v3 = runif(100)
  )

d1[, -1] %<>%
  map(
    function(i){

      i[extract(sample(1:100, 5, F))] <- NA

      i
      }
    )

这是返回的mids 对象

m1 <- d1 %>% 
  select(-id) %>% 
  mice

如何将d1$id 作为变量包含在每个估算数据帧中?

【问题讨论】:

    标签: r missing-data r-mice


    【解决方案1】:

    有两种方法。首先,只需将id 附加到估算数据集

    d2 <- complete(m1,'long', include = T) # imputed datasets in long format (including the original)
    d3 <- cbind(d1$id,d2) # as datasets are ordered simply cbind `id`
    m2 <- as.mids(d3) # and transform back to mids object
    

    这确保了id在插补过程中没有任何作用,但有点草率且容易出错。另一种方法是简单地将其从预测矩阵中删除。

    Van Buuren & Groothuis-Oudshoorn 的 2011 年 manual 说:“用户可以指定自定义预测矩阵,从而有效地调节每个变量的预测变量数量。例如,假设 bmi 被认为是无关紧要的预测器。将 bmi 列中的所有条目设置为零有效地将其从预测器集中删除...不会使用 bmi 作为预测器,但仍会对其进行估算。"

    这样做

    ini <- mice(d1,maxit=0) # dry run without iterations to get the predictor matrix
    
    pred1 <- ini$predictorMatrix # this is your predictor matrix
    pred1[,'id'] <- 0 # set all id column values to zero to exclude it as a predictor
    
    m1 <-mice(d1, pred = pred1) # use the new matrix in mice
    

    您还可以防止老鼠插补变量,但由于它不包含缺失值,因此没有必要(老鼠会自动跳过它)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多