コード例 #1
0
ファイル: _regression.py プロジェクト: jakereps/q2-gneiss
def lme_regression(output_dir: str,
                   table: pd.DataFrame, tree: skbio.TreeNode,
                   metadata: Metadata, formula: str,
                   groups: str) -> None:
    res = mixedlm(table=table, metadata=metadata._dataframe,
                  formula=formula, groups=groups)
    res.fit()
    lme_summary(output_dir, res, tree)
コード例 #2
0
def lme_regression(output_dir: str,
                   table: pd.DataFrame, tree: skbio.TreeNode,
                   metadata: Metadata, formula: str,
                   groups: str) -> None:
    if np.any(table.var(axis=0) == 0):
        message = ('Detected zero variance balances - '
                   'double check your table for unobserved features.')
        raise UserWarning(message)

    res = mixedlm(table=table, metadata=metadata.to_dataframe(),
                  formula=formula, groups=groups)
    res.fit()
    lme_summary(output_dir, res, tree)
コード例 #3
0
    def test_visualization(self):
        model = mixedlm("x1 + x2", self.table, self.metadata,
                        groups="groups")
        model.fit()
        lme_summary(self.results, model, self.tree)
        pvals = pd.read_csv(os.path.join(self.results, 'pvalues.csv'),
                            index_col=0)
        coefs = pd.read_csv(os.path.join(self.results, 'coefficients.csv'),
                            index_col=0)
        pred = pd.read_csv(os.path.join(self.results, 'predicted.csv'),
                           index_col=0)
        resid = pd.read_csv(os.path.join(self.results, 'residuals.csv'),
                            index_col=0)

        exp_pvals = pd.DataFrame({
            'Intercept': {'Y1': 4.8268860492262526e-236,
                          'Y2': 0.099411090631406948},
            'Group Var': {'Y1': 4.4193804668281966e-05,
                          'Y2': 4.4193804668280984e-05},
            'x1': {'Y1': 3.9704936434633392e-35,
                   'Y2': 3.9704936434628853e-35},
            'x2': {'Y1': 3.56912071867573e-30,
                   'Y2': 3.56912071867573e-30}}).sort_index(axis=1)
        pvals = pvals.sort_index(axis=0).sort_index(axis=1)
        exp_pvals = exp_pvals.sort_index(axis=0).sort_index(axis=1)

        npt.assert_allclose(pvals, exp_pvals, rtol=1e-5)

        exp_coefs = pd.DataFrame({
            'Intercept': {'Y1': 4.2115280233151946,
                          'Y2': 0.211528023315187},
            'Group Var': {'Y1': 0.093578639287859755,
                          'Y2': 0.093578639287860019},
            'x1': {'Y1': 1.0220072967452645,
                   'Y2': 1.0220072967452651},
            'x2': {'Y1': 0.92487193877761575,
                   'Y2': 0.92487193877761564}}
        ).sort_index(axis=1)

        npt.assert_allclose(coefs.sort_index(axis=0),
                            exp_coefs.sort_index(axis=0),
                            rtol=1e-2, atol=1e-2)

        exp_resid = pd.read_csv(get_data_path('exp_resid.csv'), index_col=0)
        npt.assert_allclose(resid, exp_resid.T, rtol=1e-2, atol=1e-2)

        exp_pred = pd.read_csv(get_data_path('exp_pred.csv'), index_col=0)
        npt.assert_allclose(pred, exp_pred.T, rtol=1e-2, atol=1e-2)
コード例 #4
0
    def test_visualization(self):
        model = mixedlm("x1 + x2", self.table, self.metadata,
                        groups="groups")
        model.fit()
        lme_summary(self.results, model, self.tree)
        pvals = pd.read_csv(os.path.join(self.results, 'pvalues.csv'),
                            index_col=0)
        coefs = pd.read_csv(os.path.join(self.results, 'coefficients.csv'),
                            index_col=0)
        pred = pd.read_csv(os.path.join(self.results, 'predicted.csv'),
                           index_col=0)
        resid = pd.read_csv(os.path.join(self.results, 'residuals.csv'),
                            index_col=0)

        exp_pvals = pd.DataFrame({
            'Intercept': {'Y1': 4.8268860492262526e-236,
                          'Y2': 0.099411090631406948},
            'groups RE': {'Y1': 4.4193804668281966e-05,
                          'Y2': 4.4193804668280984e-05},
            'x1': {'Y1': 3.9704936434633392e-35,
                   'Y2': 3.9704936434628853e-35},
            'x2': {'Y1': 3.56912071867573e-30,
                   'Y2': 3.56912071867573e-30}})
        npt.assert_allclose(pvals, exp_pvals, rtol=1e-5)

        exp_coefs = pd.DataFrame({
            'Intercept': {'Y1': 4.2115280233151946,
                          'Y2': 0.211528023315187},
            'groups RE': {'Y1': 0.093578639287859755,
                          'Y2': 0.093578639287860019},
            'x1': {'Y1': 1.0220072967452645,
                   'Y2': 1.0220072967452651},
            'x2': {'Y1': 0.92487193877761575,
                   'Y2': 0.92487193877761564}}
        )
        npt.assert_allclose(coefs, exp_coefs, rtol=1e-2, atol=1e-2)

        exp_resid = pd.read_csv(get_data_path('exp_resid.csv'), index_col=0)
        npt.assert_allclose(resid, exp_resid.T, rtol=1e-2, atol=1e-2)

        exp_pred = pd.read_csv(get_data_path('exp_pred.csv'), index_col=0)
        npt.assert_allclose(pred, exp_pred.T, rtol=1e-2, atol=1e-2)