Module: DaruLite::Vector::Calculatable

Included in:
DaruLite::Vector
Defined in:
lib/daru_lite/vector/calculatable.rb

Instance Method Summary collapse

Instance Method Details

#count_values(*values) ⇒ Integer

Count the number of values specified

Examples:

dv = DaruLite::Vector.new [1, 2, 1, 2, 3, 4, nil, nil]
dv.count_values nil
# => 2

Parameters:

  • values (Array)

    values to count for

Returns:

  • (Integer)

    the number of times the values mentioned occurs



11
12
13
# File 'lib/daru_lite/vector/calculatable.rb', line 11

def count_values(*values)
  positions(*values).size
end

#numeric_summaryString

Displays summary for an numeric type Vector

Returns:

  • (String)

    String containing numeric vector summary



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/daru_lite/vector/calculatable.rb', line 62

def numeric_summary
  summary = "\n  median: #{median}" +
            format("\n  mean: %0.4f", mean)
  if sd
    summary << (format("\n  std.dev.: %0.4f", sd) +
              format("\n  std.err.: %0.4f", se))
  end

  if count_values(*DaruLite::MISSING_VALUES).zero?
    summary << (format("\n  skew: %0.4f", skew) +
              format("\n  kurtosis: %0.4f", kurtosis))
  end
  summary
end

#object_summaryString

Displays summary for an object type Vector

Returns:

  • (String)

    String containing object vector summary



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/daru_lite/vector/calculatable.rb', line 47

def object_summary
  nval = count_values(*DaruLite::MISSING_VALUES)
  summary = "\n  factors: #{factors.to_a.join(',')}" \
            "\n  mode: #{mode.to_a.join(',')}" \
            "\n  Distribution\n"

  data = frequencies.sort.each_with_index.map do |v, k|
    [k, v, format('%0.2f%%', ((nval.zero? ? 1 : v.quo(nval)) * 100))]
  end

  summary + Formatters::Table.format(data)
end

#summary(indent_level = 0) ⇒ String

Create a summary of the Vector

Examples:

dv = DaruLite::Vector.new [1, 2, 3]
puts dv.summary

# =
#   n :3
#   non-missing:3
#   median: 2
#   mean: 2.0000
#   std.dev.: 1.0000
#   std.err.: 0.5774
#   skew: 0.0000
#   kurtosis: -2.3333

Parameters:

  • indent_level (Fixnum) (defaults to: 0)

    indent level

Returns:

  • (String)

    String containing the summary of the Vector



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/daru_lite/vector/calculatable.rb', line 31

def summary(indent_level = 0)
  non_missing = size - count_values(*DaruLite::MISSING_VALUES)
  summary = ('  =' * indent_level) + "= #{name}" \
                                     "\n  n :#{size}" \
                                     "\n  non-missing:#{non_missing}"
  case type
  when :object
    summary << object_summary
  when :numeric
    summary << numeric_summary
  end
  summary.split("\n").join("\n#{'  ' * indent_level}")
end