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
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
|
# File 'lib/rails-data-explorer/chart/contingency_table.rb', line 19
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_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(
[
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'
)
] +
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'
)
} +
[
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
|