class ANOVA3onerm(ANOVA3rm): '''Only C is an RM factor.''' def __init__(self, A, B, C, SUBJ): self.X = None self.A = Factor(A) self.B = Factor(B) self.C = Factor(C) self.S = FactorNestedTwoWay(SUBJ, self.A, self.B) # self.S = FactorNestedTwoWay(SUBJ, self.B, self.C) self.J = self.A.J self.contrasts = None self.term_labels = [ 'Intercept', 'A', 'B', 'C', 'AB', 'AC', 'BC', 'ABC', 'S', 'SC' ] self.f_terms = [('A', 'S'), ('B', 'S'), ('C', 'SC'), ('AB', 'S'), ('AC', 'SC'), ('BC', 'SC'), ('ABC', 'SC')] self._check_balanced() self._assemble() def _assemble(self): ### assemble design matrix columns: XCONST = self._get_column_const() XA = self.A.get_design_main() XB = self.B.get_design_main() XC = self.C.get_design_main() XS = self.S.get_design_main() XAB = self.A.get_design_interaction(self.B) XAC = self.A.get_design_interaction(self.C) XBC = self.B.get_design_interaction(self.C) XABC = self.C.get_design_interaction_3way(self.A, self.B) XSC = self.S.get_design_interaction(self.C) ### specify builder and add design matrix columns: builder = DesignBuilder(labels=self.term_labels) builder.add_main_columns('Intercept', XCONST) builder.add_main_columns('A', XA) builder.add_main_columns('B', XB) builder.add_main_columns('C', XC) builder.add_main_columns('AB', XAB) builder.add_main_columns('AC', XAC) builder.add_main_columns('BC', XBC) builder.add_main_columns('ABC', XABC) builder.add_main_columns('S', XS) builder.add_main_columns('SC', XSC) ### assemble design matrix and contrasts: self.X = builder.get_design_matrix() self.contrasts = builder.get_contrasts() def _check_balanced(self): if not (self.A.balanced and self.B.balanced and self.C.balanced and self.S.balanced): raise (ValueError('Design must be balanced.')) if not self.A.check_balanced(self.B): raise (ValueError('Design must be balanced.')) if not self.A.check_balanced(self.C): raise (ValueError('Design must be balanced.')) if not self.B.check_balanced(self.C): raise (ValueError('Design must be balanced.')) if not self.S.check_balanced_rm(self.C): raise (ValueError('Design must be balanced.'))
class ANOVA3onerm(ANOVA3rm): '''Only C is an RM factor.''' def __init__(self, A, B, C, SUBJ): self.X = None self.A = Factor(A) self.B = Factor(B) self.C = Factor(C) self.S = FactorNestedTwoWay(SUBJ, self.A, self.B) # self.S = FactorNestedTwoWay(SUBJ, self.B, self.C) self.J = self.A.J self.contrasts = None self.term_labels = ['Intercept', 'A','B','C', 'AB','AC','BC', 'ABC', 'S', 'SC'] self.f_terms = [('A','S'), ('B','S'), ('C','SC'), ('AB','S'), ('AC','SC'), ('BC','SC'), ('ABC','SC')] self._check_balanced() self._assemble() def _assemble(self): ### assemble design matrix columns: XCONST = self._get_column_const() XA = self.A.get_design_main() XB = self.B.get_design_main() XC = self.C.get_design_main() XS = self.S.get_design_main() XAB = self.A.get_design_interaction(self.B) XAC = self.A.get_design_interaction(self.C) XBC = self.B.get_design_interaction(self.C) XABC = self.C.get_design_interaction_3way(self.A, self.B) XSC = self.S.get_design_interaction(self.C) ### specify builder and add design matrix columns: builder = DesignBuilder(labels=self.term_labels) builder.add_main_columns('Intercept', XCONST) builder.add_main_columns('A', XA) builder.add_main_columns('B', XB) builder.add_main_columns('C', XC) builder.add_main_columns('AB', XAB) builder.add_main_columns('AC', XAC) builder.add_main_columns('BC', XBC) builder.add_main_columns('ABC', XABC) builder.add_main_columns('S', XS) builder.add_main_columns('SC', XSC) ### assemble design matrix and contrasts: self.X = builder.get_design_matrix() self.contrasts = builder.get_contrasts() def _check_balanced(self): if not (self.A.balanced and self.B.balanced and self.C.balanced and self.S.balanced): raise( ValueError('Design must be balanced.') ) if not self.A.check_balanced(self.B): raise( ValueError('Design must be balanced.') ) if not self.A.check_balanced(self.C): raise( ValueError('Design must be balanced.') ) if not self.B.check_balanced(self.C): raise( ValueError('Design must be balanced.') ) if not self.S.check_balanced_rm(self.C): raise( ValueError('Design must be balanced.') )
class ANOVA3(_Design): def __init__(self, A, B, C): self.X = None #design matrix self.A = Factor(A) #factor level vector self.B = Factor(B) #factor level vector self.C = Factor(C) #factor level vector self.J = self.A.J #number of observations self.term_labels = [ 'Intercept', 'A', 'B', 'C', 'AB', 'AC', 'BC', 'ABC' ] self.f_terms = [('A', 'Error'), ('B', 'Error'), ('C', 'Error'), ('AB', 'Error'), ('AC', 'Error'), ('BC', 'Error'), ('ABC', 'Error')] self.contrasts = None self._check_balanced() self._assemble() def _assemble(self): ### assemble design matrix columns: XCONST = self._get_column_const() XA = self.A.get_design_main() XB = self.B.get_design_main() XC = self.C.get_design_main() XAB = self.A.get_design_interaction(self.B) XAC = self.A.get_design_interaction(self.C) XBC = self.B.get_design_interaction(self.C) XABC = self.A.get_design_interaction_3way(self.B, self.C) ### specify builder and add design matrix columns: builder = DesignBuilder( labels=['Intercept', 'A', 'B', 'C', 'AB', 'AC', 'BC', 'ABC']) builder.add_main_columns('Intercept', XCONST) builder.add_main_columns('A', XA) builder.add_main_columns('B', XB) builder.add_main_columns('C', XC) builder.add_main_columns('AB', XAB) builder.add_main_columns('AC', XAC) builder.add_main_columns('BC', XBC) builder.add_main_columns('ABC', XABC) ### assemble design matrix and contrasts: self.X = builder.get_design_matrix() self.contrasts = builder.get_contrasts() def _check_balanced(self): if not (self.A.balanced and self.B.balanced and self.C.balanced): raise (ValueError('Design must be balanced.')) if not self.A.check_balanced(self.B): raise (ValueError('Design must be balanced.')) if not self.A.check_balanced(self.C): raise (ValueError('Design must be balanced.')) if not self.B.check_balanced(self.C): raise (ValueError('Design must be balanced.'))
class ANOVA3(_Design): def __init__(self, A, B, C): self.X = None #design matrix self.A = Factor(A) #factor level vector self.B = Factor(B) #factor level vector self.C = Factor(C) #factor level vector self.J = self.A.J #number of observations self.term_labels = ['Intercept', 'A', 'B', 'C', 'AB', 'AC', 'BC', 'ABC'] self.f_terms = [('A','Error'), ('B','Error'), ('C','Error'), ('AB','Error'), ('AC','Error'), ('BC','Error'), ('ABC','Error')] self.contrasts = None self._check_balanced() self._assemble() def _assemble(self): ### assemble design matrix columns: XCONST = self._get_column_const() XA = self.A.get_design_main() XB = self.B.get_design_main() XC = self.C.get_design_main() XAB = self.A.get_design_interaction(self.B) XAC = self.A.get_design_interaction(self.C) XBC = self.B.get_design_interaction(self.C) XABC = self.A.get_design_interaction_3way(self.B, self.C) ### specify builder and add design matrix columns: builder = DesignBuilder(labels=['Intercept', 'A', 'B', 'C', 'AB', 'AC', 'BC', 'ABC']) builder.add_main_columns('Intercept', XCONST) builder.add_main_columns('A', XA) builder.add_main_columns('B', XB) builder.add_main_columns('C', XC) builder.add_main_columns('AB', XAB) builder.add_main_columns('AC', XAC) builder.add_main_columns('BC', XBC) builder.add_main_columns('ABC', XABC) ### assemble design matrix and contrasts: self.X = builder.get_design_matrix() self.contrasts = builder.get_contrasts() def _check_balanced(self): if not (self.A.balanced and self.B.balanced and self.C.balanced): raise( ValueError('Design must be balanced.') ) if not self.A.check_balanced(self.B): raise( ValueError('Design must be balanced.') ) if not self.A.check_balanced(self.C): raise( ValueError('Design must be balanced.') ) if not self.B.check_balanced(self.C): raise( ValueError('Design must be balanced.') )
class ANOVA1rm(_Design): def __init__(self, A, SUBJ): self.X = None #design matrix self.S = Factor(SUBJ) #subjects self.A = Factor(A) #factor levels self.J = self.A.J #number of observations self.contrasts = None #contrast matrix self.term_labels = ['Intercept', 'A', 'S', 'SA'] self.f_terms = [('A', 'SA')] self._check_balanced() self._assemble() def _assemble(self): ### assemble design matrix columns: XCONST = self._get_column_const() XA = self.A.get_design_main() XS = self.S.get_design_main() XSA = self.A.get_design_interaction(self.S) ### specify builder and add design matrix columns: builder = DesignBuilder(labels=self.term_labels) builder.add_main_columns('Intercept', XCONST) builder.add_main_columns('A', XA) builder.add_main_columns('S', XS) builder.add_main_columns('SA', XSA) ### assemble design matrix and contrasts: self.X = builder.get_design_matrix() self.contrasts = builder.get_contrasts() def _check_balanced(self): if not (self.A.balanced and self.S.balanced): raise (ValueError('Design must be balanced.')) if not self.S.check_balanced(self.A): raise (ValueError('Design must be balanced.')) def check_for_single_responses(self): A, S = self.A.A, self.S.A only_single = False for a in self.A.u: s = S[(A == a)] if np.unique(s).size == s.size: only_single = True warnings.warn( '\nWARNING: Only one observation per subject found. Residuals and inference will be approximate. To avoid approximate residuals: (a) Add multiple observations per subject and per condition, and (b) ensure that all subjects and conditions have the same number of observations.\n', UserWarning, stacklevel=2) continue return only_single
class ANOVA1rm(_Design): def __init__(self, A, SUBJ): self.X = None #design matrix self.S = Factor(SUBJ) #subjects self.A = Factor(A) #factor levels self.J = self.A.J #number of observations self.contrasts = None #contrast matrix self.term_labels = ['Intercept', 'A', 'S', 'SA'] self.f_terms = [('A','SA')] self._check_balanced() self._assemble() def _assemble(self): ### assemble design matrix columns: XCONST = self._get_column_const() XA = self.A.get_design_main() XS = self.S.get_design_main() XSA = self.A.get_design_interaction(self.S) ### specify builder and add design matrix columns: builder = DesignBuilder(labels=self.term_labels) builder.add_main_columns('Intercept', XCONST) builder.add_main_columns('A', XA) builder.add_main_columns('S', XS) builder.add_main_columns('SA', XSA) ### assemble design matrix and contrasts: self.X = builder.get_design_matrix() self.contrasts = builder.get_contrasts() def _check_balanced(self): if not (self.A.balanced and self.S.balanced): raise( ValueError('Design must be balanced.') ) if not self.S.check_balanced(self.A): raise( ValueError('Design must be balanced.') ) def check_for_single_responses(self): A,S = self.A.A, self.S.A only_single = False for a in self.A.u: s = S[(A==a)] if np.unique(s).size == s.size: only_single = True warnings.warn('\nWARNING: Only one observation per subject found. Residuals and inference will be approximate. To avoid approximate residuals: (a) Add multiple observations per subject and per condition, and (b) ensure that all subjects and conditions have the same number of observations.\n', UserWarning, stacklevel=2) continue return only_single
class ANOVA3rm(ANOVA3): '''A, B and C are all RM factors.''' def __init__(self, A, B, C, SUBJ): self.X = None self.A = Factor(A) self.B = Factor(B) self.C = Factor(C) self.S = Factor(SUBJ) self.J = self.A.J self.contrasts = None self.term_labels = [ 'Intercept', 'A', 'B', 'C', 'S', 'AB', 'AC', 'BC', 'SA', 'SB', 'SC', 'SAB', 'SAC', 'SBC', 'ABC', 'SABC' ] self.f_terms = [('A', 'SA'), ('B', 'SB'), ('C', 'SC'), ('AB', 'SAB'), ('AC', 'SAC'), ('BC', 'SBC'), ('ABC', 'SABC')] self._check_balanced() self._assemble() def _assemble(self): ### assemble design matrix columns: XCONST = self._get_column_const() XA = self.A.get_design_main() XB = self.B.get_design_main() XC = self.C.get_design_main() XS = self.S.get_design_main() XAB = self.A.get_design_interaction(self.B) XAC = self.A.get_design_interaction(self.C) XBC = self.B.get_design_interaction(self.C) XSA = self.S.get_design_interaction(self.A) XSB = self.S.get_design_interaction(self.B) XSC = self.S.get_design_interaction(self.C) XSAB = self.S.get_design_interaction_3way(self.A, self.B) XSAC = self.S.get_design_interaction_3way(self.A, self.C) XSBC = self.S.get_design_interaction_3way(self.B, self.C) XABC = self.A.get_design_interaction_3way(self.B, self.C) XSABC = self.S.get_design_interaction_4way(self.A, self.B, self.C) ### specify builder and add design matrix columns: builder = DesignBuilder(labels=self.term_labels) builder.add_main_columns('Intercept', XCONST) builder.add_main_columns('A', XA) builder.add_main_columns('B', XB) builder.add_main_columns('C', XC) builder.add_main_columns('S', XS) builder.add_main_columns('AB', XAB) builder.add_main_columns('AC', XAC) builder.add_main_columns('BC', XBC) builder.add_main_columns('SA', XSA) builder.add_main_columns('SB', XSB) builder.add_main_columns('SC', XSC) builder.add_main_columns('SAB', XSAB) builder.add_main_columns('SAC', XSAC) builder.add_main_columns('SBC', XSBC) builder.add_main_columns('ABC', XABC) builder.add_main_columns('SABC', XSABC) ### assemble design matrix and contrasts: self.X = builder.get_design_matrix() self.contrasts = builder.get_contrasts() def _swapAB(self): if self._swap: A, B = self.B, self.A self.A = A self.B = B def _check_balanced(self): if not (self.A.balanced and self.B.balanced and self.C.balanced and self.S.balanced): raise (ValueError('Design must be balanced.')) if not self.A.check_balanced(self.B): raise (ValueError('Design must be balanced.')) if not self.A.check_balanced(self.C): raise (ValueError('Design must be balanced.')) if not self.B.check_balanced(self.C): raise (ValueError('Design must be balanced.')) if not self.S.check_balanced_rm(self.C): raise (ValueError('Design must be balanced.')) def check_for_single_responses(self): A, B, C, S = self.A.A, self.B.A, self.C.A, self.S.A only_single = False for a in self.A.u: for b in self.B.u: for c in self.C.u: s = S[(A == a) & (B == b) & (C == c)] if np.unique(s).size == s.size: only_single = True warnings.warn( '\nWARNING: Only one observation per subject found. Residuals and inference will be approximate. To avoid approximate residuals: (a) Add multiple observations per subject and per condition, and (b) ensure that all subjects and conditions have the same number of observations.\n', UserWarning, stacklevel=2) continue return only_single
class ANOVA3rm(ANOVA3): '''A, B and C are all RM factors.''' def __init__(self, A, B, C, SUBJ): self.X = None self.A = Factor(A) self.B = Factor(B) self.C = Factor(C) self.S = Factor(SUBJ) self.J = self.A.J self.contrasts = None self.term_labels = ['Intercept', 'A','B','C','S', 'AB','AC','BC', 'SA','SB','SC', 'SAB','SAC','SBC', 'ABC', 'SABC'] self.f_terms = [('A','SA'), ('B','SB'), ('C','SC'), ('AB','SAB'), ('AC','SAC'), ('BC','SBC'), ('ABC','SABC')] self._check_balanced() self._assemble() def _assemble(self): ### assemble design matrix columns: XCONST = self._get_column_const() XA = self.A.get_design_main() XB = self.B.get_design_main() XC = self.C.get_design_main() XS = self.S.get_design_main() XAB = self.A.get_design_interaction(self.B) XAC = self.A.get_design_interaction(self.C) XBC = self.B.get_design_interaction(self.C) XSA = self.S.get_design_interaction(self.A) XSB = self.S.get_design_interaction(self.B) XSC = self.S.get_design_interaction(self.C) XSAB = self.S.get_design_interaction_3way(self.A, self.B) XSAC = self.S.get_design_interaction_3way(self.A, self.C) XSBC = self.S.get_design_interaction_3way(self.B, self.C) XABC = self.A.get_design_interaction_3way(self.B, self.C) XSABC = self.S.get_design_interaction_4way(self.A, self.B, self.C) ### specify builder and add design matrix columns: builder = DesignBuilder(labels=self.term_labels) builder.add_main_columns('Intercept', XCONST) builder.add_main_columns('A', XA) builder.add_main_columns('B', XB) builder.add_main_columns('C', XC) builder.add_main_columns('S', XS) builder.add_main_columns('AB', XAB) builder.add_main_columns('AC', XAC) builder.add_main_columns('BC', XBC) builder.add_main_columns('SA', XSA) builder.add_main_columns('SB', XSB) builder.add_main_columns('SC', XSC) builder.add_main_columns('SAB', XSAB) builder.add_main_columns('SAC', XSAC) builder.add_main_columns('SBC', XSBC) builder.add_main_columns('ABC', XABC) builder.add_main_columns('SABC', XSABC) ### assemble design matrix and contrasts: self.X = builder.get_design_matrix() self.contrasts = builder.get_contrasts() def _swapAB(self): if self._swap: A,B = self.B, self.A self.A = A self.B = B def _check_balanced(self): if not (self.A.balanced and self.B.balanced and self.C.balanced and self.S.balanced): raise( ValueError('Design must be balanced.') ) if not self.A.check_balanced(self.B): raise( ValueError('Design must be balanced.') ) if not self.A.check_balanced(self.C): raise( ValueError('Design must be balanced.') ) if not self.B.check_balanced(self.C): raise( ValueError('Design must be balanced.') ) if not self.S.check_balanced_rm(self.C): raise( ValueError('Design must be balanced.') ) def check_for_single_responses(self): A,B,C,S = self.A.A, self.B.A, self.C.A, self.S.A only_single = False for a in self.A.u: for b in self.B.u: for c in self.C.u: s = S[(A==a) & (B==b) & (C==c)] if np.unique(s).size == s.size: only_single = True warnings.warn('\nWARNING: Only one observation per subject found. Residuals and inference will be approximate. To avoid approximate residuals: (a) Add multiple observations per subject and per condition, and (b) ensure that all subjects and conditions have the same number of observations.\n', UserWarning, stacklevel=2) continue return only_single