class Charty::Plotters::EstimateAggregator

Public Class Methods

new(estimator, error_bar, n_boot, random) click to toggle source
# 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

Public Instance Methods

aggregate(data, var_name) click to toggle source

Perform aggregation

@param data [Hash<Any, Charty::Table>] @param var_name [Symbol, String] A column name to be aggregated

# 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) click to toggle source
# 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