Beispiel #1
0
 def _diff_contrast(self, levels):
     nlevels = len(levels)
     contr = np.zeros((nlevels, nlevels-1))
     int_range = np.arange(1, nlevels)
     upper_int = np.repeat(int_range, int_range)
     row_i, col_i = triu_indices(nlevels-1)
     # we want to iterate down the columns not across the rows
     # it would be nice if the index functions had a row/col order arg
     col_order = np.argsort(col_i)
     contr[row_i[col_order],
           col_i[col_order]] = (upper_int-nlevels)/float(nlevels)
     lower_int = np.repeat(int_range, int_range[::-1])
     row_i, col_i = tril_indices(nlevels-1)
     # we want to iterate down the columns not across the rows
     col_order = np.argsort(col_i)
     contr[row_i[col_order]+1, col_i[col_order]] = lower_int/float(nlevels)
     return contr
Beispiel #2
0
 def _diff_contrast(self, levels):
     nlevels = len(levels)
     contr = np.zeros((nlevels, nlevels-1))
     int_range = np.arange(1, nlevels)
     upper_int = np.repeat(int_range, int_range)
     row_i, col_i = triu_indices(nlevels-1)
     # we want to iterate down the columns not across the rows
     # it would be nice if the index functions had a row/col order arg
     col_order = np.argsort(col_i)
     contr[row_i[col_order],
           col_i[col_order]] = (upper_int-nlevels)/float(nlevels)
     lower_int = np.repeat(int_range, int_range[::-1])
     row_i, col_i = tril_indices(nlevels-1)
     # we want to iterate down the columns not across the rows
     col_order = np.argsort(col_i)
     contr[row_i[col_order]+1, col_i[col_order]] = lower_int/float(nlevels)
     return contr
Beispiel #3
0
    def _helmert_contrast(self, levels):
        n = len(levels)
        #http://www.ats.ucla.edu/stat/sas/webbooks/reg/chapter5/sasreg5.htm#HELMERT
        #contr = np.eye(n - 1)
        #int_range = np.arange(n - 1., 1, -1)
        #denom = np.repeat(int_range, np.arange(n - 2, 0, -1))
        #contr[np.tril_indices(n - 1, -1)] = -1. / denom

        #http://www.ats.ucla.edu/stat/r/library/contrast_coding.htm#HELMERT
        #contr = np.zeros((n - 1., n - 1))
        #int_range = np.arange(n, 1, -1)
        #denom = np.repeat(int_range[:-1], np.arange(n - 2, 0, -1))
        #contr[np.diag_indices(n - 1)] = (int_range - 1.) / int_range
        #contr[np.tril_indices(n - 1, -1)] = -1. / denom
        #contr = np.vstack((contr, -1./int_range))

        #r-like
        contr = np.zeros((n, n - 1))
        contr[1:][diag_indices(n - 1)] = np.arange(1, n)
        contr[triu_indices(n - 1)] = -1
        return contr
Beispiel #4
0
    def _helmert_contrast(self, levels):
        n = len(levels)
        #http://www.ats.ucla.edu/stat/sas/webbooks/reg/chapter5/sasreg5.htm#HELMERT
        #contr = np.eye(n - 1)
        #int_range = np.arange(n - 1., 1, -1)
        #denom = np.repeat(int_range, np.arange(n - 2, 0, -1))
        #contr[np.tril_indices(n - 1, -1)] = -1. / denom

        #http://www.ats.ucla.edu/stat/r/library/contrast_coding.htm#HELMERT
        #contr = np.zeros((n - 1., n - 1))
        #int_range = np.arange(n, 1, -1)
        #denom = np.repeat(int_range[:-1], np.arange(n - 2, 0, -1))
        #contr[np.diag_indices(n - 1)] = (int_range - 1.) / int_range
        #contr[np.tril_indices(n - 1, -1)] = -1. / denom
        #contr = np.vstack((contr, -1./int_range))

        #r-like
        contr = np.zeros((n, n - 1))
        contr[1:][diag_indices(n - 1)] = np.arange(1, n)
        contr[triu_indices(n - 1)] = -1
        return contr