Skip to content

Commit 354db02

Browse files
new shiny app 09_ic
1 parent a7949a0 commit 354db02

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed

inst/shiny/09_ic/server.R

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#
2+
library(shiny)
3+
4+
# Define server logic required to draw a histogram
5+
shinyServer(function(input, output) {
6+
7+
student_plot <- function(mu = 0, sigma = 1, degrees_of_fredom, quantiles = NULL,
8+
seuil_alpha = NULL,
9+
alternative = c("two.sided", "less", "greater"),
10+
xlab = "Quantile", ylab = "Densité de probabilité", ...) {
11+
12+
.x <- seq(-4.5*sigma+mu, 4.5*sigma+mu, l = 1000) # Quantiles
13+
.d <- function (x) dt((x-mu)/sigma, df = degrees_of_fredom)/sigma # Distribution function
14+
.q <- function (p) qt(p, df = degrees_of_fredom) * sigma + mu
15+
16+
a <- chart::chart(
17+
data = tibble::tibble(
18+
quantiles = .x, prob = .d(.x)), prob ~ quantiles) +
19+
ggplot2::geom_hline(yintercept = 0, col = "Black") +
20+
ggplot2::geom_ribbon(
21+
x = .x, ymin = 0, ymax = .d(.x), fill = "gray", alpha = 0.2) +
22+
ggplot2::geom_line() +
23+
ggplot2::labs(x = xlab, y = ylab, ...)
24+
25+
if (!is.null(quantiles)) {
26+
a <- a +
27+
ggplot2::geom_vline(xintercept = quantiles, col = "Red")
28+
}
29+
30+
if (!is.null(seuil_alpha)) {
31+
32+
if (isTRUE(alternative == "two.sided")) {
33+
alpha2 <- seuil_alpha/2
34+
q_ref_left <- mu + sigma * qt(alpha2, df = degrees_of_fredom, lower.tail = TRUE)
35+
q_ref_right <- mu + sigma * qt(alpha2, df = degrees_of_fredom, lower.tail = FALSE)
36+
37+
.x2 <- .x1 <- .x
38+
.x1[.x1 > q_ref_left] <- NA
39+
.x2[.x2 < q_ref_right] <- NA
40+
41+
a <- a +
42+
ggplot2::geom_ribbon(x = .x1, ymin = 0, ymax = .d(.x1),
43+
fill = "red", alpha = 0.2) +
44+
ggplot2::geom_ribbon(x = .x2, ymin = 0, ymax = .d(.x2),
45+
fill = "red", alpha = 0.2)
46+
}
47+
48+
if (isTRUE(alternative == "less")) {
49+
q_ref_left <- mu + sigma * qt(seuil_alpha, df = degrees_of_fredom, lower.tail = TRUE)
50+
.x1 <- .x
51+
.x1[.x1 > q_ref_left] <- NA
52+
53+
a <- a +
54+
ggplot2::geom_ribbon(x = .x1, ymin = 0, ymax = .d(.x1),
55+
fill = "red", alpha = 0.2)
56+
}
57+
58+
if (isTRUE(alternative == "greater")) {
59+
q_ref_right <- mu + sigma * qt(seuil_alpha, df = degrees_of_fredom, lower.tail = FALSE)
60+
.x2 <- .x
61+
.x2[.x2 < q_ref_right] <- NA
62+
63+
a <- a +
64+
ggplot2::geom_ribbon(x = .x2, ymin = 0, ymax = .d(.x2),
65+
fill = "red", alpha = 0.2)
66+
}
67+
68+
}
69+
#print(a)
70+
a
71+
}
72+
73+
output$stu <- renderText({
74+
ic <- (1 - (input$numb))
75+
ic
76+
})
77+
78+
output$stu1 <- renderText({
79+
alpha2 <- (input$numb)/2
80+
81+
q_ref_left <- 8 + 2 * qt(alpha2, df = 1000, lower.tail = TRUE)
82+
83+
q_ref_left
84+
})
85+
86+
output$stu2 <- renderText({
87+
alpha2 <- (input$numb)/2
88+
89+
q_ref_right <- 8 + 2 * qt(alpha2, df = 1000, lower.tail = FALSE)
90+
91+
q_ref_right
92+
93+
})
94+
95+
output$stu_plot <- renderPlot({
96+
97+
student_plot(mu = 8, sigma = 2, degrees_of_fredom = 1000,
98+
seuil_alpha = input$numb, alternative = "two.sided")
99+
100+
})
101+
102+
})

inst/shiny/09_ic/ui.R

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#
2+
library(shiny)
3+
4+
# Define UI for application that draws a histogram
5+
shinyUI(fluidPage(
6+
# Application title
7+
titlePanel("Intervalle de confiance"),
8+
9+
# Sidebar with a slider input for number of bins
10+
sidebarLayout(
11+
sidebarPanel(
12+
withMathJax(),
13+
p("L'intervalle de confiance suit l'équation suivante :"),
14+
helpText("$$\\mathrm{IC}(1 - \\alpha) \\simeq \\bar{x} \\pm t_{\\alpha/2}^{n-1} \\cdot \\frac{s_x}{\\sqrt{n}}$$"),
15+
sliderInput("numb",
16+
"Valeur du seuil \\(\\alpha\\)",
17+
min = 0,
18+
max = 0.999,
19+
value = 0.05),
20+
hr(),
21+
helpText("Valeur de l'IC à ... \\(%\\)"),
22+
verbatimTextOutput("stu"),
23+
helpText("Valeur du quantile à gauche"),
24+
verbatimTextOutput("stu1"),
25+
helpText("Valeur du quantile à droite"),
26+
verbatimTextOutput("stu2"),
27+
hr()
28+
),
29+
30+
# Show a plot of the generated distribution
31+
mainPanel(
32+
withMathJax(),
33+
helpText("Partons d'une distribution théorique de student la population
34+
qui soit normale, de moyenne \\(\\bar{x} = 8\\) et d'écart
35+
type \\(s_x = 2\\)."),
36+
strong("Comment varie l'intervalle de confiance en fonction de la valeur de \\(\\alpha\\) ?"),
37+
plotOutput("stu_plot"),
38+
hr()
39+
)
40+
)
41+
))

0 commit comments

Comments
 (0)