Module: FDR

Defined in:
lib/rbbt/statistics/fdr.rb

Class Method Summary collapse

Class Method Details

.adjust_hash!(data, field = nil) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/rbbt/statistics/fdr.rb', line 149

def self.adjust_hash!(data, field = nil)
  begin
    if data.respond_to? :unnamed
      unnamed = data.unnamed 
      data.unnamed = true
    end

    values = []
    keys = []

    field_pos = (String === field ) ?  data.fields.index(field) : field

    field_pos = nil if data.respond_to?(:type) and data.type == :single

    data.collect{|k,vs|
      v = field_pos.nil? ? vs : vs[field_pos]
      v = v.first if Array === v
      v = 1.0 if v.nil?
      [k, v.to_f] 
    }.sort{|a,b| 
      a[1].abs <=> b[1].abs
    }.each{|p|
      keys << p[0]
      values << p[1]
    }

    if RUBY_VERSION[0] == "2"
      new_values = FDR.adjust(values)
      keys.zip(new_values).each do |k,v|
        vs = data[k] 
        if field_pos
          vs[field_pos] = v
        else
          if Array === vs
            vs[0] = v
          else
            data[k] = v
          end
        end
      end
    else
      FDR.adjust!(values)
    end

    data
  ensure
    data.unnamed = unnamed if unnamed
  end
end

.adjust_native(values) ⇒ Object

values should be sorted



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rbbt/statistics/fdr.rb', line 20

def self.adjust_native(values)
  total = values.length.to_f

  adjusted = []
  last = 1
  values.reverse.each_with_index do |value, i|
    adj = [last, value * total / (total - i )].min
    last = adj
    adjusted << adj
  end

  adjusted.reverse
end

.step_up_native(values, rate) ⇒ Object

values should be sorted



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rbbt/statistics/fdr.rb', line 6

def self.step_up_native(values, rate)
  total = values.length

  last = 0
  values.each_with_index  do |value, i|
    if value > rate * (i + 1).to_f / total
      return last
    end
    last = value
  end
  return last
end