-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.R
171 lines (138 loc) · 6.27 KB
/
server.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
library(shiny)
library(ggplot2)
library(gridExtra)
shinyServer(function(input, output) {
# react when user clicks 'New Sample'
data_sample <- eventReactive(c(input$sample_data), {
y <- rnorm(input$n, input$mu_data, input$sd_data)
mu <- mean(y)
se <- sd(y) / sqrt(length(y))
mu_prior <- input$mu_prior
sd_prior <- input$sd_prior
dat_min <- min(y) - 0.2 * sd(y)
dat_max <- max(y) + 0.2 * sd(y)
xmin <- min(dat_min, qnorm(0.0001, mu, se), qnorm(0.001, mu_prior, sd_prior))
xmax <- max(dat_max, qnorm(0.9999, mu, se), qnorm(0.999, mu_prior, sd_prior))
return(list(y = y, xmin = xmin, xmax = xmax))
}, ignoreNULL = FALSE, ignoreInit = FALSE)
# react when data_sample() changes (when user clicks 'New Sample')
true_mean <- eventReactive(data_sample(), {
return(input$mu_data)
})
# react when user checks 'Lock x-axis'
axis_lock <- eventReactive(input$axis_lock, {
z <- data_sample()
low <- z$xmin
upp <- z$xmax
return(list(low = low, upp = upp))
}, ignoreNULL = FALSE, ignoreInit = FALSE)
# draw plot
output$distPlot <- renderPlot({
# get reactive data
data_sample <- data_sample()
true_mean <- true_mean()
axis_lock <- axis_lock()
# sample data
y <- data_sample$y
# update x-axis limits, depending on whether 'Lock x-axis' is checked
if (input$axis_lock == FALSE) {
lim_low <- NULL
lim_upp <- NULL
xmin <- data_sample$xmin
xmax <- data_sample$xmax
} else {
lim_low <- axis_lock$low
lim_upp <- axis_lock$upp
xmin <- axis_lock$low
xmax <- axis_lock$upp
}
# sample mean and standard error
mu_data <- mean(y)
se_data <- sd(y) / sqrt(length(y))
# prior mean and standard deviation (user-set)
mu_prior <- input$mu_prior
sd_prior <- input$sd_prior
# sequence of x-values for generating probability distributions
x <- seq(xmin, xmax, length.out = 400)
# prior and likelihood
pri <- dnorm(x, mu_prior, sd_prior)
lik <- dnorm(x, mu_data, se_data)
# posterior distribution (product of 2 Gaussian pdfs is also Gaussian)
# https://www.johndcook.com/blog/2012/10/29/product-of-normal-pdfs/
mu_ppr <- (mu_data * se_data^(-2) + mu_prior * sd_prior^(-2)) /
(se_data^(-2) + sd_prior^(-2))
sd_ppr <- sqrt((se_data^2 * sd_prior^2) / (se_data^2 + sd_prior^2))
# posterior probability density
ppr <- dnorm(x, mu_ppr, sd_ppr)
# frequentist confidence intervals
ci_low <- qnorm(0.025, mu_data, se_data)
ci_upp <- qnorm(0.975, mu_data, se_data)
# bayesian credible intervals
cr_low <- qnorm(0.025, mu_ppr, sd_ppr)
cr_upp <- qnorm(0.975, mu_ppr, sd_ppr)
# arrange dfs for prior, likelihood, and posterior
df_lik <- data.frame(x, type = 'lik', val = lik)
df_pri <- data.frame(x, type = 'pri', val = pri)
df_ppr <- data.frame(x, type = 'ppr', val = ppr)
# combine distributions into single df
df <- rbind.data.frame(df_lik, df_pri, df_ppr)
df$type <- factor(df$type, levels = c('pri', 'lik', 'ppr'),
labels = c('Prior', 'Likelihood', 'Posterior'))
# dfs for confidence intervals, sample data, and population mean
fc <- 0.12 # factor for positioning CIs, sample data, and pop mean
df_ci <- data.frame(x = mu_data, xmin = ci_low, xmax = ci_upp, y = -1*fc)
df_cr <- data.frame(x = mu_ppr, xmin = cr_low, xmax = cr_upp, y = -2*fc)
df_sd <- data.frame(x = y, y = -3*fc)
df_pm <- data.frame(x = true_mean, y = -4*fc)
# plot settings
size_labs <- 5.5
ci_size <- 1.6
# plot
p1 <- ggplot(df) +
geom_vline(xintercept = 0, linetype = 2, alpha = 0.7) +
geom_point(data = df_sd, aes(x, y), size = 4.2, stroke = 0, alpha = 0.4,
color = '#e7298a') +
geom_point(data = df_sd, aes(x, y), shape = 1, stroke = 1.2, size = 4.2,
alpha = 0.9, color = '#e7298a') +
geom_errorbarh(data = df_ci, aes(x = x, xmin = xmin, xmax = xmax, y),
height = 0, size = ci_size, color = '#d95f02') +
geom_errorbarh(data = df_cr, aes(x = x, xmin = xmin, xmax = xmax, y),
height = 0, size = ci_size, color = '#7570b3') +
geom_point(data = df_pm, aes(x, y), size = 2.5, stroke = 1.2, shape = 3) +
geom_line(aes(x, val, color = type), lwd = 1.5) +
geom_ribbon(aes(x, ymin = 0, ymax = val, fill = type),
lwd = 0, alpha = 0.5) +
annotate('text', x = Inf, y = -1*fc, label = ' Frequentist 95% CI',
hjust = 0, size = size_labs, color = '#d95f02', fontface = 2) +
annotate('text', x = Inf, y = -2*fc, label = ' Bayesian 95% CI',
hjust = 0, size = size_labs, color = '#7570b3', fontface = 2) +
annotate('text', x = Inf, y = -3*fc, label = ' Sample data',
hjust = 0, size = size_labs, color = '#e7298a', fontface = 2) +
annotate('text', x = Inf, y = -4*fc, label = " (True population mean)",
hjust = 0, size = size_labs) +
coord_cartesian(xlim = c(lim_low, lim_upp), ylim = c(-4*fc, 1.8)) +
scale_x_continuous(expand = c(0, 0)) +
scale_color_manual(values = c('#1b9e77', '#d95f02', '#7570b3')) +
scale_fill_manual(values = c('#1b9e77', '#d95f02', '#7570b3')) +
guides(col = guide_legend(keyheight = 0.9, keywidth = 0.9, default.unit = "cm")) +
xlab('y') + ylab(NULL) +
theme(panel.grid = element_blank(),
text = element_text(size = 18),
axis.title = element_text(size = 18),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
panel.background = element_rect(fill = 'grey94'),
axis.title.x = element_text(margin = margin(.3, 0, 0, 0, unit = 'cm')),
legend.title = element_blank(),
legend.justification = c(0, 1),
legend.box.spacing = unit(1.2, 'pt'),
legend.text = element_text(size = 16, face = 'bold'),
legend.background = element_blank(),
plot.margin = unit(c(5.5, 65.5, 5.5, 5.5), 'pt'))
# allow plotting outside main plot region
g1 <- ggplotGrob(p1)
g1$layout$clip[g1$layout$name == "panel"] <- "off"
# output plot
grid.arrange(g1)
})
})