R/meanbal.R

meanbal <-
function(data_frame_weights, t_ind, bal_covs, weights, target, digits=2) {
	t_ind = as.vector(data_frame_weights[, t_ind])
	bal_covs = as.matrix(data_frame_weights[, bal_covs])
	weights = as.vector(data_frame_weights[, weights])	
	if (target=="treated") {
    	m_target = apply(bal_covs[t_ind==1, ], 2, mean)
    	sd_target = apply(bal_covs, 2, sd)
    }
	if (target=="controls") {
    	m_target = apply(bal_covs[t_ind==0, ], 2, mean)
    	sd_target = apply(bal_covs, 2, sd)
    }
	if (target=="all") {
    	m_target = apply(bal_covs, 2, mean)
    	sd_target = apply(bal_covs, 2, sd)
    }
    m_weighted = as.vector(weights%*%bal_covs)/sum(weights)
    s_d_m = (m_target-m_weighted)/sd_target
    output = cbind(m_target, m_weighted, s_d_m)	
    rownames(output) = colnames(bal_covs)
	colnames(output) = c("Mean Target", "Mean Weighted", "Std. Difference")
	output = round(output, digits)
	output
}
ngreifer/sbw documentation built on May 29, 2019, 3:17 p.m.