Learn R Programming

kbal (version 0.1.2)

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, 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

a numeric vector of length equal to the total number of units where population/treated units take a value of 1 and sample/control units take a value of 0.

observed

a numeric vector of length equal to the total number of units where sampled/control units take a value of 1 and population/treated units take a value of 0.

svd.U

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

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