(编辑:完全提炼的问题)

使用包mitools和调查并遵循Anthony Damico的代码,我正在使用Survey of Consumer Finance数据集几天 . 原始的数据集列表是“scf_imp”,并且插补强加的数据集列表是“scf_design” . 问题如下:

5个多重插补数据帧具有不同的列,因此如果我使用该列变量(在我的情况下为“house”)制作样本的子集,则“house”列中具有缺失值的数据帧与其他数据帧的行为不同 .

我试过的是:

scf_design_owner < - subset(scf_design,houses> 0&income> 0,all = TRUE)

要么

  • 我甚至在创建插补列表之前切断了na值行,如下所示:

lapply(scf_imp,function(x){replace_na(x,list(houses = 0,income = 0))})

我也做了过滤器试验,但有些事情并没有在插入列表中起作用 .

经过那些试验,当我检查错误信息时 . 警告消息:在subset.svyimputationList中(scf_design,houses> 0&income> 0,:子集在插补之间不同

我完全陷入困境,我花了三天多的时间 . 简而言之,我的计划是使用“houses> 0和income> 0”(列表中的两个列名称)过滤的插补列表,并仅使用所有五个插补数据帧都具有的观察值(行) .


我只是R的初学者,所以请耐心等待 . 我坚持使用SCF数据集并进行简单的统计分析 . 我必须削减数据,其中样本仅包括房屋和收入的正值 .

首先,我尝试通过在变量重新编码(http://asdfree.com/survey-of-consumer-finances-scf.html)中指定的Anthony Damico在数据帧列表中添加其他列来实现 . 我无法在那里那样做 . 所以我决定限制整个数据帧列表(scf_design)以包括条件标准如下:

这是我的R代码(最多子集):

setwd( "D:/Dropbox/Data/SCF 2016" )
library(mitools)    # allows analysis of multiply-imputed survey data
library(survey)     # load survey package (analyzes complex design surveys)
library(downloader) # downloads and then runs the source() function on 
scripts from github
library(foreign)    # load foreign package (converts data files into R)
library(Hmisc)      # load Hmisc package (loads a simple wtd.quantile function)

scf_imp <- readRDS("scf 2016.rds" )
scf_rw <- readRDS("scf 2016 rw.rds" )

scf_design <- svrepdesign( 

     # use the main weight within each of the imp# objects
     weights = ~wgt , 

     # use the 999 replicate weights stored in the separate replicate weights file, -1 drops first id column
     repweights = scf_rw[ , -1 ] , 

     # read the data directly from the scf data, list of all five imputation data frames
     data = imputationList( scf_imp ) , 

     scale = 1 ,

     rscales = rep( 1 / 998 , 999 ) ,

     # use the mean of the replicate statistics as the center
     # when calculating the variance, as opposed to the main weight's statistic
     mse = TRUE ,

     type = "other" ,

     combined.weights = TRUE
 )

 scf_design_owner <- subset(scf_design, houses > 0 & income > 0)

如果您没有时间,请查看最后一行,我得到的是以下消息

scf_design_owner <- subset(scf_design, houses > 0 & income > 0)
It seemed to work at first (when I did it with only one criterion..) However, 
it shows the following warnings.

Warning message:
In subset.svyimputationList(scf_design, houses > 0 & income > 0) :
subset differed between imputations

问题是每个插补数据帧中的样本数量似乎不同 . (从SCF创建了五个插补数据帧 . 他们使用多重插补技术..因此,'scf_designer'是五个数据帧的列表)

> lodown:::scf_MIcombine( with( scf_design_owner , svyby( ~ one , ~ one , 
unwtd.count ) ) )
Multiple imputation results:
  with(scf_design_owner, svyby(~one, ~one, unwtd.count))
  lodown:::scf_MIcombine(with(scf_design_owner, svyby(~one, ~one, unwtd.count)))
  results        se
1  4131.6 0.9797959

原始样本的数量是6248.它肯定会减少,但现在它有小数....我怀疑这是由于每个插补列表中的样本数量不同 .

我被困在这里 . 长话短说,这是我的问题 .

  • 有什么方法可以得到 subsetting the dataframe in the right way 所有修改后的插补数据帧都有相同数量的样本?

  • 如果我的方法效率不高,如何在“变量重新编码”部分中执行此操作? (这是我原来的试用版) . 我能够为房屋添加额外的变量,因为SCF宏中有一个可变的hhouses,这是一个标识房主的逻辑var . 但是我的收入没有类似的变数,所以我放弃了 . (SCF的收入从0开始,因此在0点有度量)

我的意思是变量重新编码aprt是Anthony Damico写的如下:

例:

scf_design <- 
    update( 
    scf_design , 
    hhsex = factor( hhsex , labels = c( "male" , "female" ) ) ,
    married = as.numeric( married == 1 ) ,
    edcl = 
        factor( 
            edcl , 
            labels = 
                c( 
                    "less than high school" , 
                    "high school or GED" , 
                    "some college" , 
                    "college degree" 
                ) 
        )

(加成)

我找到了这个,并解决了这个问题 . 如果子集在多个插补之间不同,则默认为采用任何插补的子集中的观察结果,并带有警告 .

d3<-subset(des, HAB1MI>3) 
 Warning message: In subset.svyimputationList(des, HAB1MI > 3) : 
 subset differed between imputations 
 To keep only those observations in the subset for all imputations 
 use the all=TRUE argument to subset