Spaces:
Sleeping
Sleeping
Commit
•
500a850
1
Parent(s):
a6da98f
Upload 2 files
Browse files
server.R
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
shinyServer(function(input, output) {
|
2 |
+
|
3 |
+
output$k <- renderUI({
|
4 |
+
sliderInput(inputId = "k",
|
5 |
+
label = HTML("Number of units sampled from the population:"),
|
6 |
+
min=1,
|
7 |
+
max=input$m+input$n,
|
8 |
+
value=1,
|
9 |
+
step= 1)
|
10 |
+
})
|
11 |
+
|
12 |
+
output$grafico1 <- renderPlot({
|
13 |
+
min_x <- max(c(0, input$k-input$n))
|
14 |
+
max_x <- min(c(input$k, input$m))
|
15 |
+
prob <- dhyper(x=min_x:max_x, m=input$m, n=input$n, k=input$k)
|
16 |
+
barplot(prob, ylim=c(0, 1), names.arg=min_x:max_x,
|
17 |
+
xlab="X: number of successes observed in the sample",
|
18 |
+
ylab=expression(P(X==x)),
|
19 |
+
col="deepskyblue3", las=1)
|
20 |
+
grid()
|
21 |
+
})
|
22 |
+
|
23 |
+
output$med_var <- renderText({
|
24 |
+
p <- input$m / (input$m + input$n)
|
25 |
+
esperanza <- input$k * p
|
26 |
+
varianza <- input$k * p * (1-p) * (input$m+input$n-input$k) / (input$m+input$n-1)
|
27 |
+
paste(c("For this configuration E(X) =", round(esperanza, 2),
|
28 |
+
"and Var(X) =", round(varianza, 2)))
|
29 |
+
})
|
30 |
+
|
31 |
+
})
|
ui.R
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
library(shiny)
|
2 |
+
|
3 |
+
# Define UI for application that draws a histogram
|
4 |
+
shinyUI(fluidPage(
|
5 |
+
|
6 |
+
# Application title
|
7 |
+
titlePanel("Hypergeometric distribution"),
|
8 |
+
|
9 |
+
# Sidebar with a slider input for the number of bins
|
10 |
+
sidebarLayout(
|
11 |
+
sidebarPanel(
|
12 |
+
p("Modify the parameter values and observe what happens in the barplot"),
|
13 |
+
br(),
|
14 |
+
br(),
|
15 |
+
sliderInput(inputId = "m",
|
16 |
+
label = HTML("Number of successes in the population:"),
|
17 |
+
min = 1,
|
18 |
+
max = 50,
|
19 |
+
value = 6,
|
20 |
+
step= 1,
|
21 |
+
animate = TRUE),
|
22 |
+
sliderInput(inputId = "n",
|
23 |
+
label = HTML("Number of failures in the population:"),
|
24 |
+
min = 1,
|
25 |
+
max = 50,
|
26 |
+
value = 4,
|
27 |
+
step= 1,
|
28 |
+
animate = TRUE),
|
29 |
+
uiOutput("k"),
|
30 |
+
br(),
|
31 |
+
p("App created by Semillero de R at Universidad Nacional de Colombia:"),
|
32 |
+
tags$a(href="https://srunal.github.io/", "https://srunal.github.io/")
|
33 |
+
),
|
34 |
+
|
35 |
+
# Show a plot of the generated distribution
|
36 |
+
mainPanel(
|
37 |
+
h3("Probability distribution", align = "center"),
|
38 |
+
plotOutput("grafico1"),
|
39 |
+
verbatimTextOutput('med_var')
|
40 |
+
)
|
41 |
+
)
|
42 |
+
))
|