I wrote a program that: (1) runs LASSO (with the elasticregress command) on a set of 52 instruments (with prefix psc_) to select the best ones; (2) regresses an endogenous variable capgw_dist_cum on the selected instruments (first stage) with reghdfe using district, month, and state-year fixed effects and; (3) regresses predicted values from (2) on the dependent variable of interest with reghdfe and the same fixed effects. I manually ran the program a few times and it works fine, but when I bootstrap the program, I get the error "insufficient observations to compute bootstrap standard errors no results will be saved" around the 10th draw (earlier iterations are fine). Note that I eststo the bootstrap because I want to store and tabulate the final regression (second stage) with bootstrapped standard errors later on. How come it starts off working and breaks down later from insufficient observations? Is it a problem with my program or data structure? Thanks


Code:
capture program drop ivreg_lasso
program define ivreg_lasso syntax varlist(numeric ts fv) //1. Parse Equation ** Dependent Var local depvar `1' di "`depvar'" ** Controls local xcontrols : list varlist - depvar di "`xcontrols'" //2. Zero Stage ** Residualize on FE reghdfe capgw_dist_cum capgw_state_cum_pred, /// a(c_code_2001_num month state_code_2001_num#year) /// vce(cl state_code_2001_num#year) residuals(resid_capgw_dist_cum) ** Elastic Net (LASSO) elasticregress resid_capgw_dist_cum psc_* ** Collect Model Vars file open varlistfile using "${DATA}/instrument/varlist_nonzero.txt", write replace file write varlistfile "`e(varlist_nonzero)'" file close varlistfile file open myfile using "${DATA}/instrument/varlist_nonzero.txt", read file read myfile model_vars file close myfile di "`model_vars'" //3. First Stage ** First Stage reghdfe capgw_dist_cum `model_vars' `xcontrols', /// a(c_code_2001_num month state_code_2001_num#year, savefe) /// vce(cl state_code_2001_num#year, suite(mwc)) old ** Predicted Values ren capgw_dist_cum capgw_dist_cum_o predict capgw_dist_cum, xbd la var capgw_dist_cum "Cum. District Capacity (GW)" // 4. Second Stage eststo: reghdfe `depvar' capgw_dist_cum `xcontrols', /// a(c_code_2001_num month state_code_2001_num#year) /// vce(cl state_code_2001_num#year) old
test _b[capgw_dist_cum] = 0 estadd scalar fsf `e(F)', replace estadd scalar p = r(p), replace *estadd scalar jp `e(jp)', replace estadd scalar nclust `e(N_clust)' estadd local dist_fe "$\checkmark$" estadd local month_fe "$\checkmark$" estadd local st_y_fe "$\checkmark$" estadd local clust "S $\times$ Y" estadd local weather "$\checkmark$"
drop capgw_dist_cum resid_capgw_dist_cum ren capgw_dist_cum_o capgw_dist_cum
end

local weather temperature_mean rainfall_mean
local hh_controls bpl clean_cookstove pucca lighting_elec
set seed 99999
eststo: bootstrap, rep(100) nodrop : ivreg_lasso y_var `hh_controls' age_ew mother_educated religion_* caste_* `weather' capgw_state_cum_pred