logistic.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:genetest 作者: pgxcentre 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号