To read the created .tex
file, the following \(\LaTeX\) packages are required:
# Install and load packages ---------------
packages <- c(
"tidyverse",
"haven",
"lfe",
"stargazer",
"broom",
"kableExtra",
"magrittr"
)
pacman::p_load(packages, character.only = TRUE, install = TRUE)
# Load an example dataset---------------
data_baseline <- read_dta("Banerjee2015_replication/2013-0533_data_baseline.dta")
# outcome variables
hh_composition <- c(
"hh_size", "adults", "children", "male_head", "head_age", "head_noeduc"
)
credit_access <- c(
"spandana", "othermfi", "bank", "informal", "anyloan"
)
loan_amt <- c(
"spandana_amt", "othermfi_amt", "bank_amt", "informal_amt", "anyloan_amt"
)
self_emp_activ <- c(
"total_biz", "female_biz", "female_biz_pct"
)
businesses <- c(
"bizrev", "bizexpense", "bizinvestment",
"bizemployees", "hours_weekbiz"
)
businesses_allHH <- c(
"bizrev_allHH", "bizexpense_allHH", "bizinvestment_allHH",
"bizemployees_allHH", "hours_weekbiz_allHH"
)
consumption <- c(
"total_exp_mo", "nondurable_exp_mo", "durables_exp_mo", "home_durable_index"
)
all_varlist <- c(
hh_composition, credit_access, loan_amt, self_emp_activ,
businesses, businesses_allHH, consumption
)
# data_cleaning
data_table1A <- data_baseline %>%
mutate(
across(
.cols = businesses,
.fns = ~ ifelse(total_biz == 0, 0, .x),
.names = "{col}_allHH"
)
)
# variable list
# var_label(data_table1A)[all_varlist] <- c(
var_labels <- c(
"Number members", "Number adults ($>=$ 16 years old)",
"Number children ($<$ 16 years old)",
"Male head", "Head's age", "Head with no education",
"Loan from Spandana", "Loan from other MFI",
"Loan from a bank", "Informal loan",
"Any type of loan",
"Spandana", "Other MFI", "Bank", "Informal loan",
"Total",
"Number of activities", "Number of activities managed by women",
"Share of HH activities managed by women",
"Revenue / month (Rs)", "Expenses / month (Rs)",
"Investment / month (Rs)", "Employment (employees)",
"Self-employment (hours per week)",
"Revenue / month (Rs)", "Expenses / month (Rs)",
"Investment / month (Rs)", "Employment (employees)",
"Self-employment (hours per week)",
"Total consumption (Rs)", "Nondurables consumption (Rs)",
"Durables consumption (Rs)", "Asset index"
)
# function to create a panel for control group
control_group_sumstat <- function(varlist, var_labels, data) {
output <- data %>%
select(all_of(varlist)) %>%
map( ~ tibble(
obs = format(sum(!is.na(.x)), digits = 0, format = "f", big.mark = ","),
mean_var = mean(.x, na.rm = TRUE),
sd_var = sd(.x, na.rm = TRUE)
)
) %>%
tibble() %>%
unnest(".") %>%
mutate(
col_names = var_labels,
mean_var = ifelse(
mean_var >= 100,
formatC(mean_var, digits = 0, format = "f", big.mark = ","),
formatC(mean_var, digits = 3, format = "f", big.mark = ",")
),
sd_var = paste0(
"(",
ifelse(
sd_var >= 100,
formatC(sd_var, digits = 0, format = "f", big.mark = ","),
formatC(sd_var, digits = 3, format = "f", big.mark = ",")
),
")"
)
) %>%
relocate(col_names)
return(output)
}
# function to create a paneld for differeces between treatment and control
treat_control_sumstat <- function(varlist, data) {
output <- map(varlist, function(x) felm(
formula(
paste0(x, "~ treatment | 0 | 0 | areaid"),
),
data = data)
) %>%
enframe("model_no", "model") %>%
mutate(
estimate = map_dbl(
model,
function(x) summary(x)$coefficients["treatment", "Estimate"]
),
p_value = map_dbl(
model,
function(x) waldtest(x, "treatment")["p.F"]
)
) %>%
select(estimate, p_value) %>%
mutate(
across(
.cols = c(estimate, p_value),
.fns = function(x) ifelse(
abs(x) > 10,
formatC(x, digits = 0, format = "f", big.mark = ","),
formatC(x, digits = 3, format = "f")
)
)
)
return(output)
}
cbind(
control_group_sumstat(all_varlist, var_labels, data_table1A %>% filter(treatment == 0)),
treat_control_sumstat(all_varlist, data_table1A)
) %>%
set_colnames(NULL) %>%
kable("latex", booktabs = TRUE, escape = FALSE, align = c("l", rep("r", 5))) %>%
kable_styling(latex_options = "scale_down") %>%
add_header_above(
c(" ", "Obs.", "Mean", "SD", "Coeff.", "$p$-value"),
escape = FALSE
) %>%
add_header_above(c(" " = 1, "Control group" = 3, "Treatment - control" = 2)) %>%
pack_rows(index = c(
"Household composition" = length(hh_composition),
"Access to credit" = length(credit_access),
"Amount borrowed from (in Rs)" = length(loan_amt),
"Self-employment activities" = length(self_emp_activ),
"Businesses" = length(businesses),
"Businesses (all households)" = length(businesses_allHH),
"Consumption (per household per month)" = length(consumption)
)) %>%
save_kable("tex/Banerjee_table1A_replicate.tex")