Skip to content

handle more than one factor variables (How to simulate missings) #58

@rabea-a

Description

@rabea-a

Hello @imkemayer,
it's me again, sorry! I think your temporary fix to handle factors fails for more than one factor variable. Below you see a minimal example which causes an error:

set.seed(1)
x1 <- as.factor(sample(c("A","B"), 20, replace = TRUE, prob = c(0.75, 0.25)))
x2 <- as.factor(sample(c("A","B"), 20, replace = TRUE, prob = c(0.75, 0.25)))
x3 <- rnorm(20, mean = 5)
x4 <- rnorm(20, mean = -5)

x <- data.frame(x1,x2,x3,x4)
data_NA <- produce_NA(data = x, mechanism = "MAR", perc.missing = 0.2, 
                      idx.incomplete = c(0, 1, 0, 0), idx.covariates = c(1, 0, 1, 0))

I think the case distinction of the two if queries is not necessary and should be removed. Instead, insert

orig.data <- data
vars_factor <- colnames(data)[!sapply(data, is.numeric)]
# if (length(vars_factor)==1){
#   levels_factor <- list(gdata::mapLevels(x=data[,vars_factor]))
# }
# if (length(vars_factor)>1){
#   levels_factor <- sapply(data[,vars_factor], FUN = function(x) gdata::mapLevels(x=x))
# }
levels_factor <- list(gdata::mapLevels(x=data[,vars_factor]))

for all length of vars_factor.
Furthermore, the correct indexing needs be used in lines 466 and 467 afterwards (also you have to check in case of by.patterns==TRUE):

#gdata::mapLevels(x=tmp$data.init[,vars_factor[[i]]]) <- levels_factor[[i]]
#gdata::mapLevels(x=tmp$data.incomp[,vars_factor[[i]]]) <- levels_factor[[i]]
gdata::mapLevels(x=tmp$data.init[,vars_factor[[i]]]) <- levels_factor[[1]][[i]]
gdata::mapLevels(x=tmp$data.incomp[,vars_factor[[i]]]) <- levels_factor[[1]][[i]]

I look forward to hearing your opinion of my thoughts.

Best,
Rabea.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions