Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
else:
dv = self.formula.split("~")[0]
weight_groups = self.data.groupby(weights)
weight_vals = 1 / weight_groups[dv].transform(np.var, ddof=1)
else:
weight_vals = weights
if weights is None:
self.estimator = "OLS"
else:
self.estimator = "WLS"
y, x = dmatrices(self.formula, ddat, 1, return_type="dataframe")
self.design_matrix = x
# Compute standard estimates
b, se, t, res = _ols(
x,
y,
robust,
all_stats=True,
n_lags=n_lags,
cluster=cluster,
weights=weight_vals,
)
if cluster is not None:
# Cluster corrected dof (num clusters - num coef)
# Differs from stats and statsmodels which do num cluster - 1
# Ref: http://cameron.econ.ucdavis.edu/research/Cameron_Miller_JHR_2015_February.pdf
df = cluster.nunique() - x.shape[1]
else:
df = x.shape[0] - x.shape[1]
if isinstance(weights, str) and wls_dof_correction:
"Model must be fit before partial correlations can be computed"
)
if corr_type not in ["semi", "partial"]:
raise ValueError("corr_type must be 'semi' or 'partial'")
from scipy.stats import pearsonr
corrs = []
corrs.append(np.nan) # don't compute for intercept
for c in self.design_matrix.columns[1:]:
dv = self.formula.split("~")[0]
other_preds = [e for e in self.design_matrix.columns[1:] if e != c]
right_side = "+".join(other_preds)
y, x = dmatrices(
c + "~" + right_side, self.data, 1, return_type="dataframe"
)
pred_m_resid = _ols(
x,
y,
robust=False,
n_lags=1,
cluster=None,
all_stats=False,
resid_only=True,
)
y, x = dmatrices(
dv + "~" + right_side, self.data, 1, return_type="dataframe"
)
if corr_type == "semi":
dv_m_resid = y.values.squeeze()
elif corr_type == "partial":
dv_m_resid = _ols(
x,
pred_m_resid = _ols(
x,
y,
robust=False,
n_lags=1,
cluster=None,
all_stats=False,
resid_only=True,
)
y, x = dmatrices(
dv + "~" + right_side, self.data, 1, return_type="dataframe"
)
if corr_type == "semi":
dv_m_resid = y.values.squeeze()
elif corr_type == "partial":
dv_m_resid = _ols(
x,
y,
robust=False,
n_lags=1,
cluster=None,
all_stats=False,
resid_only=True,
)
corrs.append(pearsonr(dv_m_resid, pred_m_resid)[0])
if ztrans_corrs:
corrs = np.arctanh(corrs)
return pd.Series(corrs, index=self.coefs.index)