-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.R
More file actions
77 lines (65 loc) · 1.95 KB
/
server.R
File metadata and controls
77 lines (65 loc) · 1.95 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
library(truncnorm)
shinyServer(
function(input, output, session)
{
observe(
{
updateSliderInput(session,"n_black", max = input$n_drawn)
}
)
priors = reactive(
{
d_total = numeric()
if (input$total_prior == "pois")
{
d_total = rpois(input$n_sims, input$total_lambda)
} else {
d_total = rnbinom(input$n_sims,size = input$total_r, prob = input$total_p)
}
d_prop = numeric()
if (input$prop_prior == "beta")
{
d_prop = rbeta(input$n_sims, input$prop_alpha, input$prop_beta)
} else {
d_prop = rtruncnorm(input$n_sims,0,1,input$prop_mu,input$prop_sigma)
}
data.frame(total = d_total, prop = d_prop)
}
)
sims = reactive(
{
gen_model = function(prior_N_total,prior_p_total_black)
{
n_black_bag = rbinom(1, prior_N_total, prior_p_total_black)
p_black_bag = n_black_bag / prior_N_total
n_black_hand = rbinom(1,input$n_drawn,p_black_bag)
return(n_black_hand)
}
apply(priors(),1, function(x) gen_model(x[1],x[2]))
}
)
posterior = reactive(
{
priors()[sims()==input$n_black,]
}
)
output$total_plot = renderPlot(
{
h = hist(posterior()[,1], plot=FALSE)
d = density(priors()$total)
hist(posterior()[,1], main="Post. of N total", freq=FALSE,
ylim=range(c(h$density,d$y)), xlim=range(d$x), xlab="")
lines(d, col='blue',lwd=2)
}
)
output$prop_plot = renderPlot(
{
h = hist(posterior()[,2], plot=FALSE)
d = density(priors()$prop, from=0, to=1)
hist(posterior()[,2], main="Post. of prop. black", freq=FALSE,
ylim=range(c(h$density,d$y)), xlim=c(0,1), xlab="")
lines(d, col='blue',lwd=2)
}
)
}
)