I am trying to do a balance analysis for a data set before and after applying weights. I can output the SMD's for the unweighted data set relatively easily using TableOne. However, I am having an unexpectedly difficult time with the SMD's after weighting. I have tried using the col_w_smd()
function from the cobalt
package, but always get the following error message:
Error in compute_s.d.denom(mat, treat = treat, s.d.denom = s.d.denom, : 'mat', 'treat', 'weighted.weights', 's.weights', and 'subset' must have the same number of units.
Here's the sample code:
#Create dataframe
age <- c("80", "75", "66", "45")
diabetes <- c("1", "0", "1","1")
blood_glucose <- c("12","8.5","10","11")
treat <- c("1","0","0","1")
weight <- c("1.8","3.8","1.9","2.2")
df <- data.frame(age, diabetes, blood_glucose, treat, as.numeric(weight))
#Create table with SMD's before weighting
library(tableone)
#Vector of variables to summarize
myVars <- c("age","diabetes","blood_glucose")
#Vector of categorical variables that need transformation
catVars <- c("diabetes")
tabUnweighted <- CreateTableOne(vars=myVars, strata="treat", data=df, factorVars=catVars)
print(tabUnweighted, smd=TRUE)
#Create table with SMD's after weighting
covs <- data.frame(df$age, df$diabetes, df$blood_glucose)
col_w_smd(covs, df$treat, weights=df$weight, std = TRUE,
s.d.denom = "pooled", abs = FALSE,
s.weights = NULL,
subset = NULL, weighted.weights = weights,
na.rm = TRUE)
Obviously there seems to be a comprehension problem with me. Can anyone help me out?