Class: RailsDataExplorer::Chart::ContingencyTable

Inherits:
RailsDataExplorer::Chart show all
Defined in:
lib/rails_data_explorer/chart/contingency_table.rb

Overview

Contingency table and chi squared test are great tools for interpreting A/B tests.

Responsibilities:

* Render a contingency table for bivariate analysis of two categorical
  data series.

Collaborators:

* DataSet

See this project for code to compute chi_square and contingency_coefficient github.com/bioruby/bioruby/blob/master/lib/bio/util/contingency_table.rb

Resources for Chi Squared Test

Instance Attribute Summary

Attributes inherited from RailsDataExplorer::Chart

#output_buffer

Instance Method Summary collapse

Methods inherited from RailsDataExplorer::Chart

#dom_id

Constructor Details

#initialize(_data_set, options = {}) ⇒ ContingencyTable

Returns a new instance of ContingencyTable.



25
26
27
28
# File 'lib/rails_data_explorer/chart/contingency_table.rb', line 25

def initialize(_data_set, options = {})
  @data_set = _data_set
  @options = {}.merge(options)
end

Instance Method Details

#compute_chart_attrsObject



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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rails_data_explorer/chart/contingency_table.rb', line 30

def compute_chart_attrs
  x_candidates = @data_set.data_series.find_all { |ds|
    (ds.chart_roles[Chart::ContingencyTable] & [:x, :any]).any?
  }
  y_candidates = @data_set.data_series.find_all { |ds|
    (ds.chart_roles[Chart::ContingencyTable] & [:y, :any]).any?
  }

  x_ds = x_candidates.first
  y_ds = (y_candidates - [x_ds]).first
  return false  if x_ds.nil? || y_ds.nil?

  # Compute @observed_vals, @expected_vals, etc.
  compute_contingency_and_chi_squared!(x_ds, y_ds)

  x_sorted_keys = x_ds.uniq_vals.sort(
    &x_ds.label_sorter(
      nil,
      lambda { |a,b| @observed_vals[b][:_sum] <=> @observed_vals[a][:_sum] }
    )
  )
  y_sorted_keys = y_ds.uniq_vals.sort(
    &y_ds.label_sorter(
      nil,
      lambda { |a,b| @observed_vals[:_sum][b] <=> @observed_vals[:_sum][a] }
    )
  )

  ca = case @data_set.dimensions_count
  when 2
    Utils::RdeTable.new(
      # Top header row
      [
        Utils::RdeTableRow.new(
          :tr,
          [Utils::RdeTableCell.new(:th, '')] +
          x_sorted_keys.map { |x_val| Utils::RdeTableCell.new(:th, x_val) } +
          [Utils::RdeTableCell.new(:th, 'Totals')],
          css_class: 'rde-column_header'
        )
      ] +
      # Data rows
      y_sorted_keys.map { |y_val|
        Utils::RdeTableRow.new(
          :tr,
          [
            Utils::RdeTableCell.new(:th, y_val, css_class: 'rde-row_header')
          ] +
          x_sorted_keys.map { |x_val|
            Utils::RdeTableCell.new(
              :td,
              @observed_vals[x_val][y_val],
              css_class: 'rde-numerical',
              title: [
                "Expected value: #{ number_with_precision(@expected_vals[x_val][y_val], precision: 3, significant: true) }",
                "Percentage of row: #{ number_to_percentage(@delta_attrs[x_val][y_val][:percentage_of_row], precision: 3, significant: true) }",
                "Percentage of col: #{ number_to_percentage(@delta_attrs[x_val][y_val][:percentage_of_col], precision: 3, significant: true) }",
              ].join("\n"),
              style: "color: #{ @delta_attrs[x_val][y_val][:color] };",
            )
          } +
          [
            Utils::RdeTableCell.new(
              :th,
              @observed_vals[:_sum][y_val],
              title: "Percentage of col: #{ number_to_percentage(@delta_attrs[:_sum][y_val][:percentage_of_col], precision: 3, significant: true) }"
            )
          ],
          css_class: 'rde-data_row'
        )
      } +
      # Footer row
      [
        Utils::RdeTableRow.new(
          :tr,
          [Utils::RdeTableCell.new(:th, 'Totals', css_class: 'rde-row_header')] +
          x_sorted_keys.map { |x_val|
            Utils::RdeTableCell.new(
              :th,
              @observed_vals[x_val][:_sum],
              title: "Percentage of row: #{ number_to_percentage(@delta_attrs[x_val][:_sum][:percentage_of_row], precision: 3, significant: true) }"
            )
          } +
          [Utils::RdeTableCell.new(:th, @observed_vals[:_sum][:_sum])],
          css_class: 'rde-column_header'
        )
      ]
    )
  else
    raise(ArgumentError.new("Exactly two data series required for contingency table."))
  end
  ca
end

#renderObject



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/rails_data_explorer/chart/contingency_table.rb', line 124

def render
  return ''  unless render?
  ca = compute_chart_attrs
  return ''  unless ca

  (:div, class: 'rde-chart rde-contingency-table', id: dom_id) do
    (:h3, "Contingency Table", class: 'rde-chart-title') +
    render_html_table(ca)
  end +
  (:p, @conclusion)
end

#render?Boolean

Returns:

  • (Boolean)


136
137
138
139
# File 'lib/rails_data_explorer/chart/contingency_table.rb', line 136

def render?
  # http://en.wikipedia.org/wiki/Pearson's_chi-squared_test#Assumptions
  true
end