-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpowerCalc.js
executable file
·394 lines (346 loc) · 10 KB
/
powerCalc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
'use strict'
BaseCtrl = require 'scripts/BaseClasses/BaseController.coffee'
module.exports = class StatsMainCtrl extends BaseCtrl
@inject 'app_analysis_stats_msgService',
'app_analysis_stats_algorithms',
'$timeout',
'$scope'
initialize: ->
# required basic modules
@d3 = require 'd3'
@ve = require 'vega-embed'
@vt = require 'vega-tooltip/build/vega-tooltip.js'
@distribution = require 'distributome'
@msgService = @app_analysis_stats_msgService
@algorithmService = @app_analysis_stats_algorithms
@title = "Stats Analysis Module"
@selectedAlgorithm = "CI for One Mean"
@loadData()
@$scope.$on 'stats:alpha', (event, data)=>
@algorithmService.passAlphaByName(@selectedAlgorithm, data)
@loadData()
# receive updated algorithm from sidebar area
@$scope.$on 'stats:updateAlgorithm', (event, data)=>
@selectedAlgorithm = data
console.log("algorithms updated:", @selectedAlgorithm)
@loadData()
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
# receive data
@$scope.$on 'stats:Data', (event, data)=>
@algorithmService.passDataByName(@selectedAlgorithm, data)
@loadData()
# load data to a specified calculator
loadData: () ->
if (@selectedAlgorithm is "CI for One Mean") then @CIOMRetrieve()
else if (@selectedAlgorithm is "CI for One Proportion") then @CIOPRetrieve()
else if (@selectedAlgorithm is "Pilot Study") then @PilotStudyRetrieve()
else return
# outdated
update_algo: (evt) ->
console.log(@selectedAlgorithm)
@selectedAlgorithm = evt.currentTarget.value
@msgService.broadcast 'powercalc:updateAlgorithm_back',
@selectedAlgorithm
# call to update data parameters of specified calculator
syncData: (dataIn) ->
@algorithmService.setParamsByName(@selectedAlgorithm, dataIn)
@loadData()
# functions for CIOM only
# retrieve data parameters from specified calculators
CIOMRetrieve: () ->
@params = @algorithmService.getParamsByName(@selectedAlgorithm)
@CIOMN = @params.n
@CIOMNMax = @params.nMax
@CIOMMean = @params.mu
@CIOMMeanMax = @params.meanMax
@CIOMStDev = @params.sigma
@CIOMSigmaMax = @params.sigmaMax
@CIOMTScore = @params.t
@CIOMLowerBound = @params.lowBound
@CIOMUpperBound = @params.upBound
@CIOMMode = @params.mode
@CIOMModes = ["Two Tailed", "One Tailed"]
@CIOMClick()
@CIOMDraw()
return
CIOMDraw: () ->
confidenceInterval = [{"lowerBound":@CIOMLowerBound}, {"mean":@CIOMMean}, {"upperBound":@CIOMUpperBound}]
title = "LowerBound: ".concat (@CIOMLowerBound).toString()
title = title.concat " Mean: "
title = title.concat (@CIOMMean).toString()
title = title.concat " UpperBound: "
title = title.concat (@CIOMUpperBound).toString()
vlSpec =
{
"$schema": "https://vega.github.io/schema/vega-lite/v2.json",
"width": 550,
"height": 200,
"data": {"values": confidenceInterval},
"layer": [{
"mark": {"type": "point", "filled": true},
"encoding": {
"x": {
"field": "mean", "type": "quantitative",
"axis": {"title": title}
},
"color": {"value": "black"},
}
},
{
"mark": "rule",
"encoding": {
"x": {
"aggregate": "ci0", "field": "lowerBound", "type": "quantitative"
},
"x2": {
"aggregate": "ci1", "field": "upperBound", "type": "quantitative"
}
}
}]
}
opt = {mode: "vega-lite", "actions": {export: true, source: false, editor: true}}
@ve('#visCIOM', vlSpec, opt, (error, result) -> return).then((result) =>
@vt.vegaLite(result.view, vlSpec)
)
# call syncData
CIOMSync: () ->
@params.n = @CIOMN
@params.mu = @CIOMMean
@params.sigma = @CIOMStDev
@params.mode = @CIOMMode
@syncData(@params)
return
# if user press enter, then sync data parameters
# otherwise, do nothing
CIOMPress: (evt) ->
key = evt.which or evt.keyCode
if key is 13
@CIOMSync()
return
# update all sliders
# and check deployment of data mode
CIOMClick: () ->
# slider elements
CIOMNUI = $("#CIOMNUI")
CIOMMeanUI = $("#CIOMMeanUI")
CIOMStDevUI= $("#CIOMStDevUI")
CIOMNUI.slider(
value: @CIOMN,
min: 2,
max: @CIOMNMax,
range: "min",
step: 1,
slide: (event, ui) =>
@CIOMN = ui.value
@CIOMSync()
@$scope.$apply()
)
CIOMMeanUI.slider(
value: @CIOMMean,
min: 0,
max: @CIOMMeanMax,
range: "min",
step: 0.001,
slide: (event, ui) =>
@CIOMMean = ui.value
@CIOMSync()
@$scope.$apply()
)
CIOMStDevUI.slider(
value: @CIOMStDev,
min: 0,
max: @CIOMSigmaMax,
range: "min",
step: 0.001,
slide: (event, ui) =>
@CIOMStDev = ui.value
@CIOMSync()
@$scope.$apply()
)
# enable or disable slider
CIOMSliders = [CIOMNUI, CIOMMeanUI, CIOMStDevUI]
if @deployed is true
for sl in CIOMSliders
sl.slider("disable")
sl.find('.ui-slider-handle').hide()
else
for sl in CIOMSliders
sl.slider("enable")
sl.find('.ui-slider-handle').show()
return
# functions for CIOP only
CIOPRetrieve:() ->
@params = @algorithmService.getParamsByName(@selectedAlgorithm)
@CIOPP = @params.p #central point
@CIOPN = @params.n #sample size
@CIOPT = @params.t #t-score
@CIOPTMax = @params.tMax
@zscore = @params.z
@upbound = @params.u #from confinterval
@lowbound = @params.l
@confinterval = @params.ci
@ciAlpha = @params.a #significance level
@standarddev = @params.sd
@cilevel = 1.0 - @ciAlpha #confidence level
@CIOPChart()#show chart
@CIOPClick()
CIOPSync: () ->
@params.p = @CIOPP
@params.n = @CIOPN
@params.t = @CIOPT
@syncData(@params)
CIOPPress: (evt) ->
key = evt.which or evt.keyCode
if key is 13
@CIOMSync()
return
CIOPClick: () ->
#slider elements
CIOPNUI = $("#CIOPNUI")
CIOPTUI = $("#CIOPTUI")
sliders = [CIOPNUI, CIOPTUI]
CIOPNUI.slider(
value: @CIOPN,
min: 0,
max: @CIOPTMax,
range: 'min',
step: 1,
slide: (event, ui) =>
@CIOPN = ui.value
@CIOPSync()
@$scope.$apply()
)
CIOPTUI.slider(
value: @CIOPT,
min: 0,
max: @CIOPTMax,
range: 'min',
step: 1,
slide: (event, ui) =>
@CIOPT = ui.value
@CIOPSync()
@$scope.$apply()
)
if @deployed is true
for sl in sliders
sl.slider("disable")
sl.find('.ui-slider-handle').hide()
else
for sl in sliders
sl.slider("enable")
sl.find('.ui-slider-handle').show()
return
#Chart Visualization
CIOPChart:() ->
@ve = require 'vega-embed'
nums = [{"lower" : @lowbound}, {"upper" : @upbound}, {"center" : @CIOPP}]
title = "LowerBound: ".concat (@lowbound.toFixed(3)).toString()
title = title.concat " Center: "
title = title.concat (@CIOPP.toFixed(3)).toString()
title = title.concat " UpperBound: "
title = title.concat (@upbound.toFixed(3)).toString()
vlSpec = {
"width": 550,
"height": 200,
"$schema": "https://vega.github.io/schema/vega-lite/v2.json",
"data": {"values": nums},
"layer": [{
"mark": {"type": "point", "filled": true},
"encoding": {
"x": {
"aggregate": "mean", "field": "center", "type": "quantitative",
"scale": {"zero": false},
"axis": {"title": "Interval"}
}
"color": {"value": "black"}
}
}, {
"mark": "rule",
"encoding": {
"x": {
"aggregate": "ci0", "field": "lower", "type": "quantitative",
"scale": {"zero": false},
},
"x2": {
"aggregate": "ci1", "field": "upper", "type": "quantitative"
}
}
}]
}
opt = "actions": {export: true, source: false, editor: false}
#Embed the visualization in the container with id `vis`
@ve '#visCIOP', vlSpec, opt, (error, result) ->
#example update function in chi squared service update
PilotStudyRetrieve:() ->
@params = @algorithmService.getParamsByName(@selectedAlgorithm)
@PILOTP = @params.p #Percent Under
@PILOTR = parseFloat(@params.r.toPrecision(3)) #Risk Exceeded
@PILOTD = @params.d #Degrees of Freedom
@prMax = @params.rMax
@pdfMax = @params.dfMax
@ppMax = @params.pMax
@PilotClick()
return
PilotClick: () ->
#slider elements
PILOTPUI = $("#PILOTPUI")
PILOTRUI = $("#PILOTRUI")
PILOTDUI = $("#PILOTDUI")
sliders = [PILOTPUI, PILOTDUI, PILOTDUI]
PILOTPUI.slider(
value: @PILOTP,
min: 0,
max: @ppMax,
range: 'min',
step: 1,
slide: (event, ui) =>
@PILOTP = ui.value
@PilotSync("pctUnder")
@$scope.$apply()
)
PILOTRUI.slider(
value: @PILOTR,
min: 0,
max: @prMax,
range: 'min',
step: 0.01,
slide: (event, ui) =>
@PILOTR = ui.value
@PilotSync("risk")
@$scope.$apply()
)
PILOTDUI.slider(
value: @PILOTD,
min: 0,
max: @pdfMax,
range: 'min',
step: 1,
slide: (event, ui) =>
@PILOTD = ui.value
@PilotSync("df")
@$scope.$apply()
)
PILOTRUI.slider("disable")
PILOTRUI.find('.ui-slider-handle').hide()
#enable or disable slider?
if @deployed is true
for sl in sliders
sl.slider("disable")
sl.find('.ui-slider-handle').hide()
else
for sl in sliders
sl.slider("enable")
sl.find('.ui-slider-handle').show()
return
PilotPress: (evt) ->
key = evt.which or evt.keyCode
if key is 13
@PilotSync("pctUnder")
return
PilotSync: (tar) ->
@params.p = @PILOTP
@params.r = @PILOTR
@params.d = @PILOTD
@params.tar = tar
@syncData(@params)
return