public static void groupByRange ( double [ ] range , String variable , String groupName , ArrayList < SurvivalInfo > survivalInfoList ) throws Exception { ArrayList < String > labels = new ArrayList < String > ( ) ; for ( int i = 0 ; i < range . length ; i ++ ) { String label = "" ; if ( i == 0 ) { label = "[<=" + range [ i ] + "]" ; } else if ( i == range . length - 1 ) { label = "[" + ( range [ i - 1 ] + 1 ) + "-" + range [ i ] + "]" ; labels . add ( label ) ; label = "[>" + range [ i ] + "]" ; } else { label = "[" + ( range [ i - 1 ] + 1 ) + "-" + range [ i ] + "]" ; } labels . add ( label ) ; } ArrayList < String > validLabels = new ArrayList < String > ( ) ; //need to find the categories so we can set 1 and 0 and not include ranges with no values for ( SurvivalInfo si : survivalInfoList ) { Double value = si . getContinuousVariable ( variable ) ; if ( value == null ) { throw new Exception ( "Variable " + variable + " not found in " + si . toString ( ) ) ; } int rangeIndex = getRangeIndex ( range , value ) ; String label = labels . get ( rangeIndex ) ; if ( ! validLabels . contains ( groupName + "_" + label ) ) { validLabels . add ( groupName + "_" + label ) ; } } Collections . sort ( validLabels ) ; System . out . println ( "Valid Lables:" + validLabels ) ; for ( SurvivalInfo si : survivalInfoList ) { Double value = si . getContinuousVariable ( variable ) ; if ( value == null ) { throw new Exception ( "Variable " + variable + " not found in " + si . toString ( ) ) ; } int rangeIndex = getRangeIndex ( range , value ) ; String label = labels . get ( rangeIndex ) ; String inLable = groupName + "_" + label ; for ( String gl : validLabels ) { if ( gl . equals ( inLable ) ) { si . addContinuousVariable ( gl , 1.0 ) ; } else { si . addContinuousVariable ( gl , 0.0 ) ; } } } }
Need to allow a range of values similar to cut in R and a continuous c