Learn R Programming

miceadds (version 2.2-0)

subset_datlist: Subsetting Multiply Imputed Datasets and Nested Multiply Imputed Datasets

Description

Returns a subsets of multiply imputed datasets or nested multiply imputed datasets. These function allows choosing parts of the imputed datasets using the index argument for multiply imputed datasets and index_between and index_within for nested multiply imputed datasets as well as the application of the base::subset S3 method for selecting cases and variables in datasets.

Usage

subset_datlist(datlist, subset = TRUE, select = NULL, expr_subset = NULL , index = NULL, toclass = "datlist")
"subset"(x, subset, select = NULL, expr_subset = NULL , index = NULL , ...) "subset"(x, subset, select = NULL, expr_subset = NULL , index = NULL , ...) "subset"(x, subset, select = NULL, expr_subset = NULL, index = NULL , ...) "subset"(x, subset, select = NULL, expr_subset = NULL , index = NULL , ...) subset_nested.datlist( datlist , subset = TRUE, select = NULL , expr_subset = NULL, index_between = NULL , index_within = NULL , toclass = "nested.datlist" , simplify = FALSE ) "subset"(x, subset , select = NULL , expr_subset = NULL, index_between = NULL, index_within = NULL, simplify=FALSE , ...) "subset"(x, subset , select = NULL , expr_subset = NULL, index_between = NULL, index_within = NULL, simplify=FALSE , ...)

Arguments

datlist
For subset_datlist it is a list of datasets or an object of class datlist, imputationList, mids or mids.1chain. For subset_nested.datlist it is a list of datasets or an object of class nested.datlist or NestedImputationList.
subset
Logical expression indicating elements or rows to keep, see base::subset. subset can also be a numeric vector containing row indices.
select
Expression indicating columns to select from a data frame
expr_subset
Expression indicating a selection criterion for selection rows.
index
Vector of indices indicating which of the multiply imputed datasets should be selected.
toclass
The object class in which the datasets should be saved.
index_between
Index for between nest datasets
index_within
Index for within nest datasets
simplify
Optional logical indicating whether a nested multiply imputed dataset should be simplified to a multiplied imputed dataset.
x
Object containing multiply imputed or nested multiply imputed datasets
...
Further arguments to be passed.

Value

For multiply imputed datasets: Object of class datlist, imputationList or mids For nested multiply imputed datasets: Object of class nested.datlist or NestedImputationList.

See Also

base::subset

Examples

Run this code
#############################################################################
# EXAMPLE 1: Subsetting and selection of multiply imputed datasets
#############################################################################

data(data.ma02)

# define original list of datasets
datlist1a <- data.ma02
# object of class datlist
datlist1b <- datlist_create(datlist1a)
datlist1b
# object of class imputationList
datlist1c <- mitools::imputationList(datlist1a)
datlist1c
# object of class mids
datlist1d <- datlist2mids(datlist1a)
datlist1d

# select some imputed datasets
datlist2a <- subset_datlist( datlist1a , index = c(5,3,7) )
datlist2a
# convert to class imputationList
datlist2b <- subset_datlist( datlist1a , index = c(5,3,7) , toclass = "imputationList")
datlist2b
# convert to class mids
datlist2c <- subset_datlist( datlist1a , index = 1:3 , toclass = "mids")
datlist2c

## Not run: 
# # select some variables
# datlist3a <- subset_datlist( datlist1a , select = c("idstud" , "books")  )
# datlist3a
# # Because datlist1b is a datlist it is equivalent to 
# datlist3b <- subset( datlist1b , select = c("idstud" , "books")  )
# datlist3b
# # operating on imputationList class
# datlist3c <- subset_datlist( datlist1c , select = c("idstud" , "books")  )
# datlist3c
# # operating on mids class
# datlist3d <- subset_datlist( datlist1d , select = c("idstud" , "books")  )
# datlist3d
# # selection of rows and columns in multiply imputed datasets
# datlist4a <- subset_datlist( datlist1a , index = 1:5 , 
#                   subset = datlist1a[[1]]$idschool < 1067 , 
#                   select = c("idstud" , "idschool","hisei") )
# datlist4a
# # convert to class mids
# datlist4b <- subset_datlist( datlist1a , index = 1:5 , 
#                   subset = datlist1a[[1]]$idschool < 1067 , 
#                   select = c("idstud" , "idschool","hisei") , toclass = "mids" )
# datlist4b
# # The same functionality, but now applying to object of class mids datlist1d
# datlist4c <- subset( datlist1d , index = 1:5 , subset = datlist1a[[1]]$idschool < 1067, 
#                      select = c("idstud" , "idschool","hisei")  )
# datlist4c
# 
# # expression for selecting rows specific in each data frame
# # which can result in differently sized datasets (because the variable
# # migrant is imputed)
# datlist5a <- subset_datlist( datlist1a ,  expr_subset = migrant == 1 )
# datlist5a
# 
# # select the first 100 cases
# datlist6a <- subset_datlist( datlist1a , select = c("idstud" , "books"), subset=1:100 )
# datlist6a
# 
# #############################################################################
# # EXAMPLE 2: Subsetting and selection of nested multiply imputed datasets
# #############################################################################
# 
# library(BIFIEsurvey)
# data(data.timss4, package="BIFIEsurvey")
# dat <- data.timss4
# 
# # create object of class 'nested.datlist'
# datlist1a <- nested.datlist_create( dat )
# # create object of class 'NestedImputationList'
# datlist1b <- NestedImputationList(dat)
# # create object of class 'mids.nmi'
# datlist1c <- datlist2mids(dat)
# 
# # select some between datasets
# datlist2a <- subset_nested.datlist( datlist1a , index_between = c(1,3,4) )
# datlist2a
# # shorter version
# datlist2b <- subset( datlist1a , index_between = c(1,3,4) )
# datlist2b
# # conversion of a NestedImputationList
# datlist2c <- subset( datlist1b , index_between = c(1,3,4))
# datlist2c
# # select rows and columns
# sel_cases <- datlist1a[[1]][[1]]$JKZONE <= 42
# datlist3a <- subset( datlist1a , subset = sel_cases , 
#                  select = c("IDSTUD","books", "ASMMAT") )
# datlist3a
# # remove within nest
# datlist4a <- subset( datlist1a , index_within = 1 )
# datlist4a
# # remove within nest and simplify structure
# datlist4b <- subset( datlist1a , index_within = 1 , simplify = TRUE)
# datlist4b
# datlist4c <- subset( datlist1b , index_within = 1 , simplify = TRUE)
# datlist4c
# # remove between nest
# datlist5a <- subset( datlist1a , index_between = 1 , simplify = TRUE)
# datlist5a
# datlist5b <- subset( datlist1b , index_between = 1 , simplify = TRUE)
# datlist5b
# ## End(Not run)

Run the code above in your browser using DataLab