|
| 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 | +}) |
0 commit comments