def __init__(self, X): """ X : model Model which will be fitted to the data. """ # prepare input self.X = X = asmodel(X) self.n_cases = len(X) if not isbalanced(X): raise NotImplementedError("Unbalanced models") self.X_ = X.full self.full_model = fm = X.df_error == 0 if fm: self.E_MS = hopkins_ems(X) elif hasrandom(X): err = "Models containing random effects need to be fully " "specified." raise NotImplementedError(err) self.max_len = int(2 ** _max_array_size // X.df ** 2) if _lmf_lsq == 0: pass elif _lmf_lsq == 1: self.Xsinv = X.Xsinv else: raise ValueError("version")
def __init__(self, X): super(hopkins_ems, self).__init__() if X.df_error > 0: err = "Hopkins E(MS) estimate requires a fully specified model" raise ValueError(err) if not isbalanced(X): logging.warn("X is not balanced") for e in X.effects: self[e] = _find_hopkins_ems(e, X)
def anova(self, title="ANOVA", empty=True, ems=False): """ returns an ANOVA table for the linear model """ X = self.X values = self.beta * self.X.full if X.df_error == 0: e_ms = hopkins_ems(X) elif hasrandom(X): err = "Models containing random effects need to be fully " "specified." raise NotImplementedError(err) else: e_ms = False # table head table = fmtxt.Table("l" + "r" * (5 + ems)) if title: table.title(title) if not isbalanced(X): table.caption("Warning: model is unbalanced, use anova class") table.cell() headers = ["SS", "df", "MS"] if ems: headers += ["E(MS)"] headers += ["F", "p"] for hd in headers: table.cell(hd, r"\textbf", just="c") table.midrule() # MS for factors (Needed for models involving random effects) MSs = {} SSs = {} for e in X.effects: idx = X.full_index[e] SSs[e] = SS = np.sum(values[:, idx].sum(1) ** 2) MSs[e] = SS / e.df # table body results = {} for e in X.effects: MS = MSs[e] if e_ms: e_EMS = e_ms[e] df_d = sum(c.df for c in e_EMS) MS_d = sum(MSs[c] for c in e_EMS) e_ms_name = " + ".join(repr(c) for c in e_EMS) else: df_d = self.df_res MS_d = self.MS_res e_ms_name = "Res" # F-test if MS_d != False: F = MS / MS_d p = 1 - scipy.stats.distributions.f.cdf(F, e.df, df_d) stars = test.star(p) tex_stars = fmtxt.Stars(stars) F_tex = [F, tex_stars] else: F_tex = None p = None # add to table if e_ms_name or empty: table.cell(e.name) table.cell(SSs[e]) table.cell(e.df, fmt="%i") table.cell(MS) if ems: table.cell(e_ms_name) table.cell(F_tex, mat=True) table.cell(fmtxt.p(p)) # store results results[e.name] = {"SS": SS, "df": e.df, "MS": MS, "E(MS)": e_ms_name, "F": F, "p": p} # Residuals if self.df_res > 0: table.cell("Residuals") table.cell(self.SS_res) table.cell(self.df_res, fmt="%i") table.cell(self.MS_res) return table