I am having troubles in understanding the formula in cobalt package used for standardized mean difference calculation in BINARY variables

data("lalonde", package="cobalt")
W.out <- weightit(treat ~ age + educ + race + married + nodegree + re74 + re75,
                  data = lalonde, estimand = "ATE", method = "ps")

table <- bal.tab(W.out, stats = c("m", "v"), thresholds = c(m = .10), disp=c("means", "sds") ,s.d.denom="pooled", un=TRUE,binary = "std"

For the unweighted population, I achieve the same results in binary variables by using this formula (Austin, 2009):

smd_bin <- function(x,y){
  z <- x*(1-x)
  t <- y*(1-y)
  k <- sum(z,t)
  l <- k/2

smd_bin(x,y) #x is frequency in group 1, y frequency in group 2 e.g. race_black 0.8432 and 0.2028
[1] 1.670826

Which is the R formula for this: enter image description here

However, when I have to calculate the SMD for the WEIGHTED population, I am having troubles since I don't obtain the same results. To calculate the SMD in the WEIGHTED population I would apply the same formula as the one I wrote before but with weighted frequencies (Austin,2011), thus:

[1] 0.1011917

But the cobalt package calculates it as: 0.130249813461064

Two questions:

  • What is the formula that cobalt package uses to calculate the weighted SMD categorical variables?
  • If it doesn't calculate a weighted SMD, how can I calculate a weighted SMD for categorical variables?

1 Answer 1


As I mention in my previous answer, cobalt always uses the unweighted variance in the denominator. That means you can't just supply the weighted proportions to your function and hope to get the right results; you need the unweighted proportions to compute the denominator of the SMD.

I have explained this choice here and here (so I won't do it again here). This is the best practice recommended in the literature and is described in the bal.tab() documentation.

So, we can write a new function that takes in the unweighted means and the weighted means.

smd_bin2 <- function(x, y, wx = x, wy = y){
  z <- x * (1 - x)
  t <- y * (1 - y)

  wz <- wx * (1 - wx)
  wt <- wy * (1 - wx)

  k <- z + t
  l <- k/2
  return((wx - wy)/sqrt(l))

For the unweighted SMD, we supply the unweighted means, as you have done:

> smd_bin2(0.843243243243243, 0.202797202797203)
[1] 1.670826

For the weighted SMD, we additionally supply the weighted means:

> smd_bin2(0.843243243243243,0.202797202797203,
[1] 0.1302498
  • $\begingroup$ I understand, however in the Austin's formula for categorical variables, there isn't the variance but only the weighted rate, that's why I keep not understanding the role of unweighted variance here (while for continous I understood). Is the denominator equal to variances? $\endgroup$ Jun 13, 2023 at 15:59
  • $\begingroup$ The quantity $\hat p (1-\hat p)$ is the formula for the variance of a binary variable. It replaces the usual variance $s^2$ in the SMD formula. That's all Austin's formula is. $\endgroup$
    – Noah
    Jun 13, 2023 at 16:05
  • $\begingroup$ Thank you!! Now I understand!! You're great $\endgroup$ Jun 13, 2023 at 16:07
  • $\begingroup$ Happy to help :) $\endgroup$
    – Noah
    Jun 13, 2023 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.