0

I am trying to add weights produced with the entropy balancing reweighting ebal package to a dataframe in order to use them thereafter in regression analysis with balanced samples. The code that that I used for weighting is:

# install.packages("ebal")
library(ebal)
out.eb<- ebalance(Treatment=Treatment,X=X,constraint.tolerance = 2)

To then add the weights to the dataframe I assumed that treatment units are weighted as 1, and that control units should get the weights that were produced with ebal, so:

data$weights <- ifelse(Treatment==1,1,out.eb$w)

I think that I got it wrong, but am not sure what would be the right solution. Would be grateful for your advice.

user438383
  • 5,716
  • 8
  • 28
  • 43
  • Why do you think that you got it wrong? – cherrywoods Aug 22 '21 at 06:31
  • because when i compare the covariate means of treated and non-treated units in the dataset they differ, and the difference is actually larger than in the original dataset – user6530195 Aug 22 '21 at 08:11
  • Hello! Could you (1) add a reproducible example https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example and (2) specify the package(s) needed to use the function? I believe that here, it is ebal, so I added it. – Rosalie Bruel Aug 23 '21 at 11:20

1 Answers1

0

What you want is the following:

data$weights <- 1
data$weights[Treatment == 1] <- eb$w

This has to do with how ifelse() is structured. The number of elements in each of the arguments to ifelse() should be of length 1 or have the same length at he first argument. Because ebal() only produces weights for the treated units, eb$w has fewer elements than Treatment, so the assignment fails. Using the syntax above avoids this problem.

Another option to look at is the WeightIt package, which also implements entropy balancing and may be easier to use.

Noah
  • 3,437
  • 1
  • 11
  • 27