Repository navigation
Expand file tree
/
Copy pathserver.R
More file actions
101 lines (83 loc) · 3.94 KB
/
Copy pathserver.R
File metadata and controls
101 lines (83 loc) · 3.94 KB
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
# This is the server logic for a Shiny web application.
# You can find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com
#
library('shiny')
library('ggplot2')
source("functions.R")
makeTypicalTable = function(planTable, sizes, dummy) {
dummy # cause dummy promise to be evaluated (and trigger recalc)
ptab = typicalTable(planTable, sizes)
if(sum(sizes) > 0) {
ptab$expectedSuccessRate = with(ptab, (Successes+0.5)/(Actions+1)) # posteriors from Jeffrey prior
ptab$expectedValuePerAction = ptab$expectedSuccessRate*planTable$ValueSuccess
}
ptab
}
labeledPlan = function(sizes,rates,values,countGoal) {
data.frame(Label=paste("Campaign_", seq_len(length(sizes)), sep=''),
ActionsToMeetErrorGoals=sizes,
ActionsToMeetCountGoals=ceiling(countGoal/rates),
MatchingRates=c(rates[2]*values[2]/values[1],rates[1]*values[1]/values[2]))
}
displayGraph = function(pgraph, doplot) {
if(doplot) {
plotSample(pgraph)
} else NULL
}
# ---------------------------------
assembleResultTable = function(actions, successes, values, wishPrice) {
ptab = data.frame(Label=paste("Campaign_", seq_len(length(successes)), sep=''),
Actions=actions,
Successes=successes,
ValueSuccess=values)
ptab$observedSuccessRate = (successes+0.5)/(actions+1) # posterior Jeffreys prior 0.5,0.5 smoothing
ptab$observedValuePerAction = ptab$observedSuccessRate*values
ptab$pAboveWishPrice = pbeta(wishPrice/values,
shape1=0.5+successes,
shape2=0.5+actions-successes,
lower.tail=FALSE)
ptab
}
shinyServer(function(input, output) {
#
# for planning campaign
#
cprobabilities = reactive(c(input$conv1a, input$conv2a))
values = reactive(c(input$value1a, input$value2a))
sizes = reactive(c(input$sizes1a, input$sizes2a))
proposedsizes = reactive(heuristicPowerPlan(data.frame(Probability=cprobabilities(),
ValueSuccess=values()),
errorProbability=input$errorProb,relativeError=input$relErr))
countGoalV <- reactive(input$countGoal)
docalc = reactive(sum(sizes()) != 0)
planTable = reactive(data.frame(Label=c('Campaign1','Campaign2'),
Probability=cprobabilities(),
ValueSuccess=values()))
typicalTable = reactive(makeTypicalTable(planTable(), sizes(), input$reseed))
pgraph2T = reactive(posteriorGraph(typicalTable()))
output$planGraph2T = renderPlot(plotPosterior(pgraph2T()))
output$probTable2T = renderPrint(computeProbsGEP(typicalTable(),pgraph2T()$graph))
pgraph = reactive(sampleGraph(planTable(),sizes()))
bgraph = reactive(computeProbsGES(planTable(),pgraph()))
output$plan = renderTable(labeledPlan(proposedsizes(),cprobabilities(),values(),countGoalV()),digits=4)
output$typicalTable = renderPrint(typicalTable()) # I'll render it verbatim, rather than as a table.
# Saves me from having to worry about sig figs
output$planGraph = renderPlot(displayGraph(pgraph(), docalc()))
output$probTable = renderPrint(bgraph())
#
# for evaluating campaign
#
actions = reactive(c(input$actions1b, input$actions2b))
successes = reactive(c(input$success1b, input$success2b))
svalues = reactive(c(input$value1b, input$value2b))
resTable = reactive(assembleResultTable(round(input$rescale*actions()),
round(input$rescale*successes()),
svalues(),
input$wishPrice))
pgraph2 = reactive(posteriorGraph(resTable()))
output$resTable = renderPrint(resTable())
output$planGraph2 = renderPlot(plotPosterior(pgraph2(),input$wishPrice))
output$probTable2 = renderPrint(computeProbsGEP(resTable(),pgraph2()$graph))
})