-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
183 lines (108 loc) · 4.89 KB
/
app.R
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
library(ggplot2)
library(shiny)
library(scales)
ui <- fluidPage(
titlePanel("How to win on 'The Price is Right'"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Slider for the answer ----
sliderInput("answer", "Move the slider to select an answer:",
min = 1, max = 1000,
value = 500),
textOutput("selected_var")
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Plot ----
plotOutput(outputId = "sysPlot"),
plotOutput(outputId = "guessPlot")
)
)
)
server <- function(input, output) {
sliderValues <- reactive({
input$answer
})
framed_sys <- reactive({
lower_limit <- 1
upper_limit <- 1000
answer <- sliderValues()
guesses <- floor(mean(c(lower_limit, upper_limit)))
count_sys <- 1
this_guess <- guesses[count_sys]
while (this_guess != answer){
if (guesses[count_sys] < answer){
lower_limit <- c(lower_limit, guesses[count_sys])
upper_limit <- c(upper_limit, upper_limit[count_sys])
count_sys <- count_sys + 1
guesses <- c(guesses, ceiling(mean(c(lower_limit[count_sys], upper_limit[count_sys]))))
} else {
lower_limit <- c(lower_limit, lower_limit[count_sys])
upper_limit <- c(upper_limit, guesses[count_sys])
count_sys <- count_sys + 1
guesses <- c(guesses, floor(mean(c(lower_limit[count_sys], upper_limit[count_sys]))))
}
this_guess <- guesses[count_sys]
}
counter_sys <- 1:count_sys
framed <- data.frame(guesses, lower_limit, upper_limit, counter_sys)
return(framed)
})
output$sysPlot <- renderPlot({
if (input$answer == 500){
framed <- data.frame(x = c(0, 1), y = c(0, 1))
ggplot(data = framed, aes(x = x, y = y)) +
geom_text(x = .5, y = .5, label = "If the answer is 500 you get it on the first attempt") +
labs(x = "", y = "")
} else {
ggplot(data = framed_sys(), aes(x = counter_sys)) +
geom_ribbon(aes(ymin = lower_limit, ymax = upper_limit), fill = "red", alpha = .5) +
geom_line(aes(y = guesses)) +
scale_x_continuous(breaks = c(0, 1,4,7,10), limits = c(0, 10)) +
labs(title = "Systematically guessing the mean of the range", x = "Attempt", y = "Guess on each attempt\n(range within this guess is in red)")
}
})
framed_guess <- reactive({
lower_limit <- 1
upper_limit <- 1000
answer <- sliderValues()
guesses <- floor(sample(lower_limit:upper_limit, 1))
count_guess <- 1
this_guess <- guesses[count_guess]
while (this_guess != answer){
if (guesses[count_guess] < answer){
lower_limit <- c(lower_limit, guesses[count_guess])
upper_limit <- c(upper_limit, upper_limit[count_guess])
count_guess <- count_guess + 1
guesses <- c(guesses, ceiling(sample(lower_limit[count_guess]:upper_limit[count_guess], 1)))
} else {
lower_limit <- c(lower_limit, lower_limit[count_guess])
upper_limit <- c(upper_limit, guesses[count_guess])
count_guess <- count_guess + 1
guesses <- c(guesses, floor(sample(lower_limit[count_guess]:upper_limit[count_guess], 1)))
}
this_guess <- guesses[count_guess]
}
counter_guess <- 1:count_guess
framed <- data.frame(guesses, lower_limit, upper_limit, counter_guess)
return(framed)
})
output$guessPlot <- renderPlot({
ggplot(data = framed_guess(), aes(x = counter_guess)) +
geom_ribbon(aes(ymin = lower_limit, ymax = upper_limit), fill = "red", alpha = .5) +
geom_line(aes(y = guesses)) +
scale_x_continuous(breaks = pretty_breaks(), limits = c(0, max(framed_guess()$counter_guess))) +
labs(title = "Random guessing within range", x = "Attempt", y = "Guess on each attempt\n(range within this guess is in red)",
title = "The black line shows the best guess on each turn, the red region is the range the answer could be in on each turn")
})
output$selected_var <- renderText({
if (max(framed_guess()$counter_guess) < max(framed_sys()$counter_sys)){
"Guessing got there got there faster on this trial"
} else {
"Finding the mean of the range systematically got there faster on this trial"
}
})
}
shinyApp(ui, server)