1

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?

Xray
  • 11
  • 2
  • from `?cobalt::col_w_smd` *When s.d.denom is not "weighted", the weighted.weights is ignored*. It also by default takes on the value of whatever is in the `weight` argument. Simply deleting the weighted.weights argument from your function call avoids this error. Further, the first argument *mat* expects numeric input, all character variables get converted to factors. I.e. you probably want to convert age and glucose using `as.numeric` – Donald Seinen Aug 01 '22 at 08:02
  • @Donald : the _weighted.weights_ argument was the key issue. Despite studying the documentation I was not aware of that - thanks! – Xray Aug 02 '22 at 14:07

0 Answers0