【问题标题】:Exporting multiple imputed objects with MICE使用 MICE 导出多个估算对象
【发布时间】:2019-11-03 06:57:53
【问题描述】:

我正在使用鼠标输入我丢失的数据。这里的问题是完成估算需要一两个小时。因此,当它完成插补时,我想将其导出以供将来使用,这样我就可以避免重复耗时的插补过程,以防我必须重新进行分析。

我在 Google 上搜索了一个函数 miceadds::write.mice.imputation。我看过说明书。它提供了一个导出示例,但我不知道如何将其导入回来。它似乎生成了一些 .dat 文件。

假设我有以下代码:

# Model 1: Imputation using mice  
imp1 <- mice::mice( nhanes, m=3, maxit=5 )  
# write results 
write.mice.imputation(mi.res=imp1, name="mice_imp1" )

【问题讨论】:

    标签: r imputation r-mice


    【解决方案1】:

    如果您在使用write.mice.imputation 时注意到,它的默认值是将您的估算数据保存在各种类型的文件(csv、spss、dat、Rdata)中。

    我们可以创建一个示例数据: set.seed(1) df

    加载和估算我们的数据:

    require(mice)
    require(miceadds)
    imp1 <- mice::mice(df, m=3, maxit=5 )  
    

    写出我们的结果:

    write.mice.imputation(mi.res=imp1, name="mice_imp1", 
                          include.varnames=TRUE,
                          long=TRUE, mids2spss=TRUE,
                          spss.dec=",", dattype=NULL)
    

    现在,我们可以加载任何适合您的文件类型。比如dat文件:

    oldData <- read.table("mice_imp1/mice_imp1__LONG.dat")
    

    【讨论】:

      【解决方案2】:
      est_long <- parlmice(nhanes, cluster.seed = 123, print = FALSE, n.core = 4, n.imp.core = 100) %>%
        mice::complete("long")
      

      给予

      head(est_long)
        .imp .id age  bmi hyp chl
      1    1   1   1 22.0   1 131
      2    1   2   2 22.7   1 187
      3    1   3   1 27.2   1 187
      4    1   4   3 27.2   2 284
      5    1   5   1 20.4   1 113
      6    1   6   3 20.4   1 184
      

      您可以通过将长数据集分解为包含 400 个数据集的列表来跟进。

          est_long <- parlmice(nhanes, cluster.seed = 123, print = FALSE, n.core = 4, n.imp.core = 100) %>%
            mice::complete("long")%>%
        group_by(.imp) %>% 
        nest()  
      head(est_long)
      # A tibble: 6 x 2
      # Groups:   .imp [6]
         .imp data                 
        <int> <list>               
      1     1 <tibble[,5] [25 × 5]>
      2     2 <tibble[,5] [25 × 5]>
      3     3 <tibble[,5] [25 × 5]>
      4     4 <tibble[,5] [25 × 5]>
      5     5 <tibble[,5] [25 × 5]>
      6     6 <tibble[,5] [25 × 5]>
      

      【讨论】:

        猜你喜欢
        • 2020-10-26
        • 1970-01-01
        • 1970-01-01
        • 2022-08-24
        • 1970-01-01
        • 1970-01-01
        • 2017-02-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多