Skip to content

Commit

Permalink
factor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
istallworthy committed Sep 21, 2023
1 parent fbbb6b6 commit 342dea1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
21 changes: 15 additions & 6 deletions R/calcBalStats.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion examplePipelineRevised.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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()
```


Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 342dea1

Please sign in to comment.