Class: Charty::Plotters::EstimateAggregator

Inherits:
Object
  • Object
show all
Defined in:
lib/charty/plotters/line_plotter.rb

Instance Method Summary collapse

Constructor Details

#initialize(estimator, error_bar, n_boot, random) ⇒ EstimateAggregator

Returns a new instance of EstimateAggregator.



4
5
6
7
8
9
# File 'lib/charty/plotters/line_plotter.rb', line 4

def initialize(estimator, error_bar, n_boot, random)
  @estimator = estimator
  @method, @level = error_bar
  @n_boot = n_boot
  @random = random
end

Instance Method Details

#aggregate(data, var_name) ⇒ Object

Perform aggregation

Parameters:

  • data (Hash<Any, Charty::Table>)
  • var_name (Symbol, String)

    A column name to be aggregated



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/charty/plotters/line_plotter.rb', line 15

def aggregate(data, var_name)
  values = data[var_name]
  estimation = case @estimator
               when :count
                 values.length
               when :mean
                 values.mean
               end

  n = values.length
  case
  # No error bars
  when @method.nil?
    err_min = err_max = Float::NAN
  when n <= 1
    err_min = err_max = Float::NAN

  # User-defined method
  when @method.respond_to?(:call)
    err_min, err_max = @method.call(values)

  # Parametric
  when @method == :sd
    err_radius = values.stdev * @level
    err_min = estimation - err_radius
    err_max = estimation + err_radius
  when @method == :se
    err_radius = values.stdev / Math.sqrt(n)
    err_min = estimation - err_radius
    err_max = estimation + err_radius

  # Nonparametric
  when @method == :pi
    err_min, err_max = percentile_interval(values, @level)
  when @method == :ci
    # TODO: Support units
    err_min, err_max =
      Statistics.bootstrap_ci(values, @level, units: nil, func: @estimator,
                              n_boot: @n_boot, random: @random)
  end

  {
    var_name => estimation,
    "#{var_name}_min" => err_min,
    "#{var_name}_max" => err_max
  }
end

#percentile_interval(values, width) ⇒ Object



63
64
65
66
# File 'lib/charty/plotters/line_plotter.rb', line 63

def percentile_interval(values, width)
  q = [50 - width / 2, 50 + width / 2]
  Statistics.percentile(values, q)
end