-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.R
71 lines (63 loc) · 2.58 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
library(shiny)
shinyServer(function(input, output) {
data <- reactive({
if (input$dist=="rpois") {
vals <- do.call(input$dist, list(n=input$n, lambda=1))
}
else {
vals <- do.call(input$dist, list(n=input$n))
}
return (list(fun=input$dist, vals=vals))
})
output$plot <- renderPlot({
distname <- switch(input$dist,
rexp = "Exponential distribution", # (n = ",
rnorm = "Normal distribution", # (n = ",
rlnorm = "Log-normal distribution", # (n = ",
runif = "Uniform distribution", # (n = ",
rpois = "Poisson distribution",
rcauchy = "Cauchy distribution") # (n = ")
n <- input$n
k <- input$k
pdist <- data()$vals
if (input$dist=="rpois") {
x <- replicate(k, do.call(input$dist, list(n=n, lambda=1)))
}
else {
x <- replicate(k, do.call(input$dist, list(n=n)))
}
ndist <- rowMeans(x)
expect <- switch(input$dist,
rexp = c(1^-1, 1^-2),
rnorm = c(0, 1),
rlnorm = c(exp(0+(1/2)*1^2), exp(0 + 1^2)*(exp(1^2)-1)),
runif = c(0.5, (1/12)*1),
rpois =c(1, 1),
rcauchy = rep(NA, 2))
obs <- data.frame(pdist=c(mean(pdist), var(pdist)), ndist=c(mean(ndist),
var(ndist)))
par(mfrow=c(2,1))
pdens=density(pdist)
phist=hist(pdist, freq=FALSE, plot=FALSE)
hist(pdist, main=paste(n, " observations from ", distname, sep=""),
xlab="Values", freq=FALSE, ylim=c(0, max(pdens$y, phist$density)))
lines(pdens, col="red", lwd=2)
abline(v=obs$pdist[1], col="blue", lwd=2, lty=2)
abline(v=expect[1], col="red", lwd=2, lty=2)
legend(x="topright", col=c("blue", "red"), lwd=2, lty=2,
legend=c("Observed", "Expected"))
box()
ndens=density(ndist)
nhist=hist(ndist, freq=FALSE, plot=FALSE)
hist(ndist, main=paste("Distribution of mean values from ", k,
" random variables each\nconsisting of ", n,
" observations from ", distname, sep=""),
xlab="Values", freq=FALSE, ylim=c(0, max(ndens$y, nhist$density)))
lines(ndens, col="red", lwd=2)
abline(v=obs$ndist[1], col="blue", lwd=2, lty=2)
abline(v=expect[1], col="red", lwd=2, lty=2)
legend(x="topright", col=c("blue", "red"), lwd=2, lty=2,
legend=c("Observed", "Expected"))
box()
})
})