emultimputation: Multiple imputation

View source: R/edar_survey.R

emultimputationR Documentation

Multiple imputation

Description

This is a wrap function for the Multiple Imputation package mice.

Usage

emultimputation(
  data,
  RHS.formula,
  dep.vars,
  ind.vars,
  m = 5,
  maxit = 50,
  method = "pmm",
  seed = 500,
  digits = 4
)

Arguments

data

a data.frame

RHS.formula

a string with the RHS of the regression model

dep.vars

a string vector with the names of the dependent variables.

ind.vars

a string vector with the names of the independent variables.

m

Number of multiple imputations. The default is m=5.

maxit

A scalar giving the number of iterations. The default is 5.

method

Can be either a single string, or a vector of strings with length length(blocks), specifying the imputation method to be used for each column in data. If specified as a single string, the same method will be used for all blocks. The default imputation method (when no argument is specified) depends on the measurement level of the target column, as regulated by the defaultMethod argument. Columns that need not be imputed have the empty method "". See details.

seed

An integer that is used as argument by the set.seed() for offsetting the random number generator. Default is to leave the random number generator alone.

digits

integer, number of significant digits to return in the table

Details

See help(mice)

Value

The function returns a list with the summary output of the models estimated after the multiple imputation is performed

Examples

library(magrittr)

data = tibble::data_frame(x1 = rnorm(200,3,1),
                          x2 = rexp(200),
                          cat.var  = sample(c(0,1), 200, replace=TRUE),
                          cat.var2 = sample(letters[1:4], 200, replace=TRUE),
                          y1 = 10*x1*cat.var+rnorm(200,0,10) +
                              3*x2*(6*(cat.var2=='a') -3*(cat.var2=='b') +
                                    1*(cat.var2=='c') +1*(cat.var2=='d')),
                          y2 = -10*x1*cat.var+rnorm(200,0,10) +
                              10*x2*(3*(cat.var2=='a') -3*(cat.var2=='b') +
                                     1*(cat.var2=='c') -1*(cat.var2=='d'))
                          )  %>%
    dplyr::mutate(cat.var=as.factor(cat.var)) 
data$x1[sample(1:nrow(data), 10)] = NA


formula = "x1*cat.var+x2*cat.var2"
imp = emultimputation(data, formula,  dep.vars = c("y1", "y2"),
                      ind.vars=c("x1", "x2", "cat.var", "cat.var2"))
imp

DiogoFerrari/edar documentation built on May 8, 2022, 8:26 a.m.