#NEWLINE# def pairwise_logrank_test( event_durations , groups , event_observed = None , alpha = 0.95 , t_0 = ( - 1 ) , bonferroni = True , **kwargs ) : #NEWLINE# #INDENT# "\n    Perform the logrank test pairwise for all n>2 unique groups (use the more appropriate logrank_test for n=2).\n    We have to be careful here: if there are n groups, then there are n*(n-1)/2 pairs -- so many pairs increase\n    the chance that here will exist a significantly different pair purely by chance. For this reason, we use the\n    Bonferroni correction (rewight the alpha value higher to accomidate the multiple tests).\n\n\n    Parameters:\n      event_durations: a (n,) numpy array the (partial) lifetimes of all individuals\n      groups: a (n,) numpy array of unique group labels for each individual.\n      event_observed: a (n,) numpy array of event_observed events: 1 if observed death, 0 if censored. Defaults\n          to all observed.\n      alpha: the level of signifiance desired.\n      t_0: the final time to compare the series' up to. Defaults to all.\n      bonferroni: If true, uses the Bonferroni correction to compare the M=n(n-1)/2 pairs, i.e alpha = alpha/M\n            See (here)[http://en.wikipedia.org/wiki/Bonferroni_correction].\n      kwargs: add keywords and meta-data to the experiment summary.\n\n    Returns:\n        R: a (n,n) dataframe of StatisticalResults (None on the diagonal)\n\n    " #NEWLINE# if ( event_observed is None ) : #NEWLINE# #INDENT# event_observed = np . ones ( ( event_durations . shape [ 0 ] , 1 ) ) #NEWLINE# #UNINDENT# n = np . max ( event_durations . shape ) #NEWLINE# assert ( n == np . max ( event_durations . shape ) == np . max ( event_observed . shape ) ) , 'inputs must be of the same length.' #NEWLINE# ( groups , event_durations , event_observed ) = map ( ( lambda x : pd . Series ( np . reshape ( x , ( n , ) ) ) ) , [ groups , event_durations , event_observed ] ) #NEWLINE# unique_groups = np . unique ( groups ) #NEWLINE# n = unique_groups . shape [ 0 ] #NEWLINE# if bonferroni : #NEWLINE# #INDENT# m = ( ( 0.5 * n ) * ( n - 1 ) ) #NEWLINE# alpha = ( 1 - ( ( 1 - alpha ) / m ) ) #NEWLINE# #UNINDENT# R = np . zeros ( ( n , n ) , dtype = object ) #NEWLINE# np . fill_diagonal ( R , None ) #NEWLINE# for ( i1 , i2 ) in combinations ( np . arange ( n ) , 2 ) : #NEWLINE# #INDENT# ( g1 , g2 ) = unique_groups [ [ i1 , i2 ] ] #NEWLINE# ( ix1 , ix2 ) = ( ( groups == g1 ) , ( groups == g2 ) ) #NEWLINE# test_name = ( ( str ( g1 ) + ' vs. ' ) + str ( g2 ) ) #NEWLINE# result = logrank_test ( event_durations . ix [ ix1 ] , event_durations . ix [ ix2 ] , event_observed . ix [ ix1 ] , event_observed . ix [ ix2 ] , alpha = alpha , t_0 = t_0 , use_bonferroni = bonferroni , test_name = test_name , ** kwargs ) #NEWLINE# ( R [ ( i1 , i2 ) ] , R [ ( i2 , i1 ) ] ) = ( result , result ) #NEWLINE# #UNINDENT# return pd . DataFrame ( R , columns = unique_groups , index = unique_groups )