def fit(self, y, X):
"""Fit the model.
Args:
y (pandas.DataFrame): The vector of endogenous variable.
X (pandas.DataFrame): The matrix of exogenous variables.
"""
# Creating the GLM model from StatsModels and fitting it
model = sm.GLM(y, X, family=sm.families.Binomial())
try:
fitted = model.fit()
except PerfectSeparationError as e:
raise StatsError(str(e))
out = {}
parameters = fitted.params.index
# Results about the model fit.
out = {
"MODEL": {
"log_likelihood": fitted.llf,
"nobs": fitted.nobs
},
}
# Getting the confidence intervals
conf_ints = fitted.conf_int()
for param in parameters:
# If GWAS, check that inference could be done on the SNP.
if param == "SNPs" and np.isnan(fitted.pvalues[param]):
raise StatsError("Inference did not converge.")
out[param] = {
"coef": fitted.params[param],
"std_err": fitted.bse[param],
"lower_ci": conf_ints.loc[param, 0],
"upper_ci": conf_ints.loc[param, 1],
"t_value": fitted.tvalues[param],
"p_value": fitted.pvalues[param],
}
return out
评论列表
文章目录