Объединение списков связи - PullRequest
0 голосов
/ 16 ноября 2018

Я работаю с набором данных Обследования потребительских финансов и собираюсь провести анализ по годам.Сначала я хотел объединить их в один и тот же список svyimputationList, но мои попытки, похоже, на самом деле не объединяют данные.они остаются как разные списки в большем списке svyimputationList.Могут быть другие способы сделать анализ по годам (я в основном рассчитываю на возможность запуска некоторых регрессий), поэтому эти ответы также приветствуются.

Класс наборов scf_design - "svyimuptationList".

Ниже приведен код, который я использовал для сбора наборов данных и моей попытки объединить.

library(lodown)
library(survey)
library(mitools)
library(plyr)

        scf_cat <-
  get_catalog( "scf" ,
               output_dir = file.path( path.expand( "~" ) , "SCF" ) )
 # 2016 only
scf_cat_16 <- subset( scf_cat , year == 2016 )
#2013 only
scf_cat_13 <- subset( scf_cat , year == 2013 )
 # download the microdata to your local computer
scf_cat_16 <- lodown( "scf" , scf_cat_16 )
scf_cat_13 <- lodown( "scf" , scf_cat_13 )
 #setup the data to control for the survey style and MI
scf_imp_16 <- readRDS( file.path( path.expand( "~" ) , "SCF" , "scf 2016.rds" ) )
 scf_rw_16 <- readRDS( file.path( path.expand( "~" ) , "SCF" , "scf 2016 rw.rds" ) )
 scf_imp_13 <- readRDS( file.path( path.expand( "~" ) , "SCF" , "scf 2013.rds" ) )
 scf_rw_13 <- readRDS( file.path( path.expand( "~" ) , "SCF" , "scf 2013 rw.rds" ) )
 #APPLYING THE WEIGHTS TO THE DATA#####
scf_design_16 <- 
  svrepdesign( 
    weights = ~wgt , 
    repweights = scf_rw_16[ , -1 ] , 
    data = imputationList( scf_imp_16 ) , 
    scale = 1 ,
    rscales = rep( 1 / 998 , 999 ) ,
    mse = FALSE ,
    type = "other" ,
    combined.weights = TRUE
  )
 scf_design_13 <- 
  svrepdesign( 
    weights = ~wgt , 
    repweights = scf_rw_13[ , -1 ] , 
    data = imputationList( scf_imp_13 ) , 
    scale = 1 ,
    rscales = rep( 1 / 998 , 999 ) ,
    mse = FALSE ,
    type = "other" ,
    combined.weights = TRUE
  )


scf_design <- rbind(scf_design_16,scf_design_13)

1 Ответ

0 голосов
/ 13 декабря 2018

Это ответ, который я придумал после изучения того, что прокомментировал Энтони Дамико:

Это следует за формой размещения кадров данных в списке вменений и копируемых файлов весов, чтобы в конечном итоге объединить их в svyimputationList.

scf_cat <-
  get_catalog( "scf" ,
               output_dir = file.path( path.expand( "~" ) , "SCF" ) )

yearslist <- list(10,13,16)
y1<- NULL
y2<- NULL
y3<- NULL
y4<- NULL
y5<- NULL
imp <- NULL
rw <- NULL
#loop to load in the desired years and assign the weights
for (i in 1:length(yearslist)){

  year <- yearslist[i]
  fullyear <- as.numeric(paste0("20",year))
  df1 = scf_cat[FALSE,]


  df1 <- subset( scf_cat , year == fullyear )
  df2 <- lodown( "scf" , df1)

  scf_imp <- readRDS( file.path( path.expand( "~" ) , "SCF" , paste0("scf ",fullyear,".rds" ) ))

  for( i in 1:length(scf_imp)){

    scf_imp[[i]][["year"]] <- fullyear

    scf_imp[[i]] <- select(scf_imp[[i]],c("yy1","one","year","wgt","edcl","hhsex","age","married","kids","lf","lifecl",
                                          "networth","racecl4","racecl","occat1","income","norminc","wsaved",
                                          "spendmor","spendless","late","late60","hpayday","bshopnone",
                                          "bshopgrdl","bshopmodr","checking","hcheck",
                                          "saving","hsaving","mmda","cds","irakh","anypen","vehic","hvehic",
                                          "own","nown","newcar2","newcar1","veh_inst","hveh_inst","x7543","x7540",
                                          "x2206","x2209","x2210","x2211","x2212","x2213","x2219"))

  }

  scf_rw <- readRDS( file.path( path.expand( "~" ) , "SCF" , paste0("scf ",fullyear," rw.rds" ) ))

  #piecing together the dataframes
  #figure out a loop for this
  y1<- rbind(scf_imp[[1]],y1)
  y2<- rbind(scf_imp[[2]],y2)
  y3<- rbind(scf_imp[[3]],y3)  
  y4<- rbind(scf_imp[[4]],y4)
  y5<- rbind(scf_imp[[5]],y5)

  rw <- rbind(scf_rw,rw)


  #include or exclude incrimental saving
  assign(paste0("scf_imp_",year),scf_imp)
  assign(paste0("scf_rw_",year),scf_rw)


  assign(paste0("scf_design_",year),
         svrepdesign(
           weights = ~wgt ,
           repweights = scf_rw[ , -1 ] ,
           data = imputationList( scf_imp ) ,
           scale = 1 ,
           rscales = rep( 1 / 998 , 999 ) ,
           mse = FALSE ,
           type = "other" ,
           combined.weights = TRUE
         ))

  rm(df1)
  rm(df2)
  rm(scf_imp)
  rm(scf_rw)
}

#piecing together the imputations
imp <- list(y1,y2,y3,y4,y5)

#creating the design using the set of 3 years
design_full <- svrepdesign(
  weights = ~wgt ,
  repweights = rw[ , -1 ] ,
  data = imputationList( imp ) ,
  scale = 1 ,
  rscales = rep( 1 / 998 , 999 ) ,
  mse = FALSE ,
  type = "other" ,
  combined.weights = TRUE
)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...