From 342dea128e752bc2aeecd33720f5697ce0b368a2 Mon Sep 17 00:00:00 2001 From: Isa Stallworthy <31548151+istallworthy@users.noreply.github.com> Date: Thu, 21 Sep 2023 18:16:22 -0400 Subject: [PATCH] factor fix --- R/calcBalStats.R | 21 +++++++++++++++------ examplePipelineRevised.Rmd | 5 ++++- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/R/calcBalStats.R b/R/calcBalStats.R index e2f92740..bc870a64 100644 --- a/R/calcBalStats.R +++ b/R/calcBalStats.R @@ -127,8 +127,13 @@ calcBalStats <- function(home_dir = NA, data, formulas, exposure, exposure_time_ } #split factors - data$"ID" = as.numeric(data$"ID") - data2 <- cobalt::splitfactor(data, names(data)[sapply(data, class ) == "factor"], drop.first = FALSE ) + if(length(names(data)[sapply(data, class ) == "factor"]) > 0){ + data$"ID" = as.numeric(data$"ID") + data2 <- cobalt::splitfactor(data, names(data)[sapply(data, class ) == "factor"], drop.first = FALSE ) + } + else{ + data2 <- data + } #creating initial data frames #data frame with all sampling weights for all exposures at all exposure time points for all histories @@ -150,10 +155,14 @@ calcBalStats <- function(home_dir = NA, data, formulas, exposure, exposure_time_ covars <- as.character(unlist(strsplit(covars, "\\+"))) covars <- gsub(" ", "", covars) - #making factor covars separate variables - data_cov <- data[, covars] - data_cov <- cobalt::splitfactor(data_cov, names(data_cov)[sapply(data_cov, class ) == "factor"], drop.first = FALSE ) - covars <- colnames(data_cov) + + if(length(names(data)[sapply(data, class ) == "factor"]) > 0){ + + #making factor covars separate variables + data_cov <- data[, covars] + data_cov <- cobalt::splitfactor(data_cov, names(data_cov)[sapply(data_cov, class ) == "factor"], drop.first = FALSE ) + covars <- colnames(data_cov) + } # GETTING BALANCE STATS FOR T=1 W/ NO EXPOSURE HISTORY (ok to standardize immediately) diff --git a/examplePipelineRevised.Rmd b/examplePipelineRevised.Rmd index 2db17b36..99ad5deb 100644 --- a/examplePipelineRevised.Rmd +++ b/examplePipelineRevised.Rmd @@ -56,7 +56,7 @@ library(devMSMsHelpers) #prior to re-installing using the code above. Sorry, this is annoying! #conducting package checks & tests from documentation --just for IS to run; comment out when testing the workflow -# devtools::check() +devtools::check() ``` @@ -193,7 +193,10 @@ data <- imputed_data #optional: extract first imputed dataset for testing library(mice) data <- mice::complete(imputed_data, 1) #just for testing purposes + +#testing multi-level factors data$"PmMrSt2" = as.factor(data$"PmMrSt2") +data$HomeOwnd = as.factor(data$HomeOwnd) # library(dplyr) # data <- data %>% # dplyr::select(-c(contains(c(":", "Childhood", "Infancy", "Toddlerhood", "pcx")))) #removing these --came from old dataset