Learn R Programming

kbal (version 0.1.4)

getw: Find Weights using Entropy Balancing.

Description

Uses entropy balancing to find and return the weights that produce mean balance on \(\phi(X_i)\), the expanded features of \(X_i\) using a given kernel \(\phi(.)\), for the control or sample group and treated group or target population.

Usage

getw(
  target,
  observed,
  svd.U,
  base.weights = NULL,
  ebal.tol = 1e-06,
  ebal.maxit = 500
)

Value

A list containing:

w

A numeric vector of weights.

converged

boolean indicating if ebalance_custom converged

ebal_error

returns error message if ebalance_custom encounters an error

Arguments

target

binary length-N vector: 1 = target/population, 0 = not in target.

observed

binary length-N vector: 1 = observed/sample, 0 = not observed.

svd.U

a matrix of left singular vectors from performing svd() on the kernel matrix.

base.weights

optional positive length-N vector of base/design weights. These are only used for sample units (observed==1 & target==0); all other units are treated as having base weight 1 inside entropy balancing.

ebal.tol

tolerance level used by custom entropy balancing function ebalance_custom. Default is 1e-6.

ebal.maxit

maximum number of iterations in optimization search used by ebalance_custom. Default is 500.

Examples

Run this code
# \donttest{
#load and clean data
set.seed(123)
data("lalonde")
# Select a random subset of 500 rows
lalonde_sample <- sample(1:nrow(lalonde), 500, replace = FALSE)
lalonde <- lalonde[lalonde_sample, ]

xvars=c("age","black","educ","hisp","married","re74","re75","nodegr","u74","u75")

#need a kernel matrix to run SVD on then find weights with; so get that first with makeK.
#running makeK with the sampled units as the bases
K = makeK(allx = lalonde[,xvars], useasbases = 1-lalonde$nsw)

#SVD on this kernel and get matrix with left singular values
U = svd(K)$u
#Use the first 10 dimensions of U.
U2=U[,1:10]
getw.out=getw(target=lalonde$nsw, 
              observed=1-lalonde$nsw, 
              svd.U=U2)
 # }

Run the code above in your browser using DataLab