scale_datlist: Adding a Standardized Variable to a List of Multiply Imputed...

Description Usage Arguments Value Author(s) See Also Examples

View source: R/scale_datlist.R

Description

Adds a standardized variable to a list of multiply imputed datasets or a single dataset. This function extends base::scale for a data frame to a list of multiply imputed datasets.

Usage

1
2
scale_datlist(datlist, orig_var, trafo_var, weights = NULL, M = 0, SD = 1, 
    digits = NULL)

Arguments

datlist

A data frame, a list of multiply imputed datasets of one of the classes datlist or imputationList or a list of nested multiply imputed datasets of one of the classes nested_datlist or NestedImputationList.

orig_var

Vector with names of the variables to be transformed

trafo_var

Vector with names of the standardized variables

weights

Optional vector of sample weights. Alternatively, the weights can also be a string indicating the variable used from datlist.

M

Mean of the transformed variable

SD

Standard deviation of the transformed variable

digits

Number of digits used for rounding the standardized variable

Value

A vector or a matrix

Author(s)

Alexander Robitzsch

See Also

base::scale, ma.scale2

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
## Not run: 	
#############################################################################
# EXAMPLE 1: Standardized variables in list of multiply imputed datasets
#############################################################################
	
data(data.ma02)
datlist <- data.ma02

#--- object of class 'datlist'
datlist <- miceadds::datlist_create( datlist )

# mean and SD of variable hisei
miceadds::ma.wtd.meanNA(data=datlist, weights = datlist[[1]]$studwgt , vars = "hisei" )
mean( unlist( lapply( datlist , FUN = function(data){
        stats::weighted.mean( data$hisei , data$studwgt )  } ) ) )
miceadds::ma.wtd.sdNA(data=datlist, weights = datlist[[1]]$studwgt , vars = "hisei" )
mean( unlist( lapply( datlist , FUN = function(data){
        sqrt( Hmisc::wtd.var( data$hisei , data$studwgt ) ) } ) ) )

# standardize variable hisei to M=100 and SD=15        
datlist1a <- miceadds::scale_datlist( datlist=datlist, orig_var="hisei", 
               trafo_var="hisei100", weights = datlist[[1]]$studwgt , M=100, SD=15 )

# check mean and SD
miceadds::ma.wtd.meanNA(data=datlist1a, weights = datlist[[1]]$studwgt , vars = "hisei100")
miceadds::ma.wtd.sdNA(data=datlist1a, weights = datlist[[1]]$studwgt , vars = "hisei100")

#--- do standardization for unweighted sample with books <= 3
# -> define a weighting variable at first
datlist0 <- mitools::imputationList( datlist )
datlist2a <- miceadds::within.imputationList( datlist0 , {
	         # define weighting variable
                 wgt_books <- 1 * ( books <= 3 )
                    } )

# standardize variable hisei to M=100 and SD=15 with respect to weighting variable
datlist2b <- miceadds::scale_datlist( datlist=datlist2a, orig_var="hisei", trafo_var="hisei100",
         weights = "wgt_books" , M=100, SD=15 )

# check mean and SD (groupwise)
miceadds::ma.wtd.meanNA(data=datlist1a, weights = datlist[[1]]$studwgt , vars = "hisei100")
miceadds::ma.wtd.sdNA(data=datlist1a, weights = datlist[[1]]$studwgt , vars = "hisei100")

#--- transformation for a single dataset
dat0 <- datlist[[1]]
dat0a <- miceadds::scale_datlist( datlist=dat0 , orig_var="hisei" , trafo_var="hisei100" , 
                    weights = dat0$studwgt , M=100, SD=15 )
stats::weighted.mean( dat0a[,"hisei"] ,  w = dat0a$studwgt )                    
stats::weighted.mean( dat0a[,"hisei100"] ,  w = dat0a$studwgt )
sqrt( Hmisc::wtd.var( dat0a[,"hisei100"] ,  weights = dat0a$studwgt ) )

#--- Standardizations for objects of class imputationList
datlist2 <- mitools::imputationList(datlist)   # object class conversion
datlist2a <- miceadds::scale_datlist( datlist=datlist2 , orig_var="hisei", 
                 trafo_var="hisei100", weights = datlist[[1]]$studwgt , M=100, SD=15 )
               
#############################################################################
# EXAMPLE 2: Standardized variables in list of nested multiply imputed datasets
#############################################################################
               
# nested multiply imputed dataset in BIFIEsurvey package
data(data.timss4, package="BIFIEsurvey")
datlist <- data.timss4
wgt <- datlist[[1]][[1]]$TOTWGT

# class nested.datlist
imp1 <- miceadds::nested.datlist_create( datlist )
# class NestedImputationList
imp2 <- miceadds::NestedImputationList( datlist )

# standardize variable scsci
imp1a <- miceadds::scale_datlist( datlist=imp1, orig_var="scsci", trafo_var="zscsci", weights =wgt)
# check descriptives
miceadds::ma.wtd.meanNA( imp1a , weights=wgt , vars = c("scsci" , "zscsci" ) )
miceadds::ma.wtd.sdNA( imp1a , weights=wgt , vars = c("scsci" , "zscsci" ) )

#############################################################################
# EXAMPLE 3: Standardization of variables for imputed data in mice package
#############################################################################

data(nhanes, package="mice")
set.seed(76)

#--- impute nhanes data
imp <- mice::mice(nhanes)
#--- convert into datlist
datlist <- miceadds::mids2datlist(imp)
#--- scale datlist (all variables)
vars <- colnames(nhanes)
sdatlist <- miceadds::scale_datlist(datlist, orig_var = vars, trafo_var = paste0("z",vars) )
#--- reconvert to mids object
imp2 <- miceadds::datlist2mids(sdatlist)
#*** compare descriptive statistics of objects
round( miceadds::mean0( mice::complete(imp, action=1) ) , 2 )
round( miceadds::mean0( mice::complete(imp2, action=1) ) , 2 )

## End(Not run)	

miceadds documentation built on May 19, 2017, 7:26 a.m.

Search within the miceadds package
Search all R packages, documentation and source code