def _estimate_bootstrapped_params(self):
"""
Compute the bootstrapped parameters for:
* The path from the predictors to Y (computed using OLS/Logit, depending on the nature of Y)
* The path(s) from the mediator(s) to Y (computed using OLS)
:return: A tuple of (true_betas_y, true_betas_m)
* true_betas_y is a matrix of size n_boots x n_params_y
* true_betas_m is a list of matrices of size n_boots x n_params_y
"""
n_boots = self._options["boot"]
seed = self._options["seed"]
boot_betas_y = np.empty((n_boots, len(self._exog_terms_y)))
boot_betas_m = np.empty((self._n_meds, n_boots, len(self._exog_terms_m)))
n_fail_samples = 0
boot_ind = 0
sampler = bootstrap_sampler(self._n_obs, seed)
while boot_ind < n_boots:
ind = next(sampler)
data_boot = self._data[ind, :]
y_e = data_boot[:, self._ind_y]
y_x = data_boot[:, self._exog_inds_y]
try:
y_b = self._compute_betas_y(y_e, y_x)
m_x = data_boot[:, self._exog_inds_m]
boot_betas_y[boot_ind] = y_b
for j, m_ind in enumerate(self._inds_m):
m_e = data_boot[:, m_ind]
m_b = self._compute_betas_m(m_e, m_x)
boot_betas_m[j][boot_ind] = m_b
boot_ind += 1
except LinAlgError: # Hessian (Logit) or X'X (OLS) cannot be inverted
n_fail_samples += 1
return boot_betas_y, boot_betas_m, n_fail_samples
评论列表
文章目录