def test_fullrank(self): X = R.standard_normal((40,10)) X[:,0] = X[:,1] + X[:,2] Y = utils.fullrank(X) self.assertEquals(Y.shape, (40,9)) self.assertEquals(utils.rank(Y), 9) X[:,5] = X[:,3] + X[:,4] Y = utils.fullrank(X) self.assertEquals(Y.shape, (40,8)) self.assertEquals(utils.rank(Y), 8)
def isestimable(C, D): """ From an q x p contrast matrix C and an n x p design matrix D, checks if the contrast C is estimable by looking at the rank of vstack([C,D]) and verifying it is the same as the rank of D. """ if C.ndim == 1: C.shape = (C.shape[0], 1) new = np.vstack([C, D]) if utils.rank(new) != utils.rank(D): return False return True
def initialize(self, design, hascons=True): # Jonathan: PLEASE don't assume we have a constant... # TODO: handle case for noconstant regression self.design = design self.wdesign = self.whiten(self.design) self.calc_beta = np.linalg.pinv(self.wdesign) self.normalized_cov_beta = np.dot(self.calc_beta, np.transpose(self.calc_beta)) self.df_total = self.wdesign.shape[0] self.df_model = utils.rank(self.design) self.df_resid = self.df_total - self.df_model
def test_rank(self): X = R.standard_normal((40,10)) self.assertEquals(utils.rank(X), 10) X[:,0] = X[:,1] + X[:,2] self.assertEquals(utils.rank(X), 9)
def rank(self): """ Compute rank of design matrix """ return utils.rank(self.wdesign)
# of dtype np.float contrasts = {'Duration': f1.main_effect, 'Weight': f2.main_effect, 'Interaction': f1.main_effect * f2.main_effect, 'Duration1': f1.get_term(1)} X, cons = twoway.design(D, contrasts=contrasts) Y = D['Days'] # Fit the model beta = np.dot(np.linalg.pinv(X), Y) XTXinv = np.linalg.pinv(np.dot(X.T, X)) resid = Y - np.dot(X, beta) df_resid = (X.shape[0] - rank(X)) # residual degrees of freedom sigmasq = (resid**2).sum() / df_resid SS = {} MS = {} F = {} df = {} p = {} for n, c in cons.items(): cbeta = np.dot(c, beta) cov_cbeta = np.dot(c, np.dot(XTXinv, c.T)) if c.ndim > 1: df[n] = c.shape[0] SS[n] = np.dot(cbeta, np.dot(np.linalg.pinv(cov_cbeta), cbeta)) MS[n] = SS[n] / df[n] F[n] = MS[n] / sigmasq