Exemple #1
0
    def _get_intercept_stats(self, add_slopes=True):
        # start with mean and variance of Y on the link scale
        mod = GLM(
            endog=self.model.y.data,
            exog=np.repeat(1, len(self.model.y.data)),
            family=self.model.family.smfamily(),
            missing="drop" if self.model.dropna else "none",
        ).fit()
        mu = mod.params
        # multiply SE by sqrt(N) to turn it into (approx.) SD(Y) on link scale
        sd = (mod.cov_params()[0] * len(mod.mu)) ** 0.5

        # modify mu and sd based on means and SDs of slope priors.
        if len(self.model.fixed_terms) > 1 and add_slopes:
            means = np.array([x["mu"] for x in self.priors.values()])
            sds = np.array([x["sd"] for x in self.priors.values()])
            # add to intercept prior
            index = list(self.priors.keys())
            mu -= np.dot(means, self.stats["mean_x"][index])
            sd = (sd ** 2 + np.dot(sds ** 2, self.stats["mean_x"][index] ** 2)) ** 0.5

        return mu, sd
Exemple #2
0
    def get_intercept_stats(self, add_slopes=True):
        # Start with mean and variance of Y on the link scale
        mod = GLM(
            endog=self.model.response.data,
            exog=np.repeat(1, len(self.model.response.data)),
            family=self.model.family.smfamily(self.model.family.smlink),
            missing="drop" if self.model.dropna else "none",
        ).fit()
        mu = mod.params

        # Multiply SE by sqrt(N) to turn it into (approx.) sigma(Y) on link scale
        sigma = (mod.cov_params()[0] * len(mod.mu)) ** 0.5

        # Modify mu and sigma based on means and sigmas of slope priors.
        if len(self.model.common_terms) > 1 and add_slopes:
            # prior["mu"] and prior["sigma"] have more than one value when the term is categoric.
            means = np.hstack([prior["mu"] for prior in self.priors.values()])
            sigmas = np.hstack([prior["sigma"] for prior in self.priors.values()])
            x_mean = np.hstack([self.model.terms[term].data.mean(axis=0) for term in self.priors])

            mu -= np.dot(means, x_mean)
            sigma = (sigma ** 2 + np.dot(sigmas ** 2, x_mean ** 2)) ** 0.5
        return mu, sigma