Commit fefdc10c authored by jbleher's avatar jbleher
Browse files

Chi2 und t-Test

parent 43c69681
Loading
Loading
Loading
Loading
+73 −0
Original line number Diff line number Diff line
###################################################################
#                Chi-Square Distribution - Shiny App
#           (PDF/CDF + df slider + parameter display)
###################################################################

library(shiny)

# ------------------- User Interface -------------------
ui <- fluidPage(
  titlePanel("Chi-Square Distribution"),
  
  sidebarLayout(
    sidebarPanel(
      selectInput("typ", "Function:",
                  choices = c("Distribution function (CDF)" = 0,
                              "Density function (PDF)" = 1)),
      helpText("Select which function you want to visualize."),
      
      sliderInput("df", "Degrees of freedom (ν):",
                  min = 1, max = 60, value = 5, step = 1),
      
      hr(),
      h4("Current Parameters:"),
      verbatimTextOutput("paramText")
    ),
    
    mainPanel(
      plotOutput("chisqPlot")
    )
  )
)

# ------------------- Server Logic -------------------
server <- function(input, output) {
  
  output$chisqPlot <- renderPlot({
    df <- input$df
    
    # Dynamic x-range: cover almost all probability mass
    xmax <- qchisq(0.999, df = df)
    x <- seq(0, xmax, length.out = 400)
    
    pdf_vals <- dchisq(x, df = df)
    cdf_vals <- pchisq(x, df = df)
    
    if (input$typ == 1) {
      # ---- PDF ----
      plot(x, pdf_vals, type = "l", lwd = 3, col = "coral3",
           main = sprintf("Chi-Square (df = %d) - Density (PDF)", df),
           xlab = "x", ylab = "Density",
           cex.lab = 1.2, cex.axis = 1.2, cex.main = 1.2)
      abline(h = 0, lty = 2)
      
    } else {
      # ---- CDF ----
      plot(x, cdf_vals, type = "l", lwd = 3, col = "steelblue",
           main = sprintf("Chi-Square (df = %d) - Distribution (CDF)", df),
           xlab = "x", ylab = "Cumulative probability",
           cex.lab = 1.2, cex.axis = 1.2, cex.main = 1.2)
      abline(h = c(0, 1), lty = 2)
    }
  })
  
  output$paramText <- renderText({
    df <- input$df
    paste0("Degrees of freedom (ν): ", df,
           "\nMean = ν = ", df,
           "\nVariance = 2ν = ", 2 * df)
  })
}

# ------------------- Run App -------------------
shinyApp(ui = ui, server = server)
+85 −0
Original line number Diff line number Diff line
###################################################################
#              Student's t Distribution - Shiny App
#           (PDF/CDF + df slider + optional Normal overlay)
###################################################################

library(shiny)

ui <- fluidPage(
  titlePanel("Student's t Distribution"),
  
  sidebarLayout(
    sidebarPanel(
      selectInput("typ", "Function:",
                  choices = c("Distribution function (CDF)" = 0,
                              "Density function (PDF)" = 1)),
      helpText("Select which function you want to visualize."),
      
      sliderInput("df", "Degrees of freedom (ν):",
                  min = 1, max = 100, value = 5, step = 1),
      
      checkboxInput("showNormal", "Overlay Standard Normal (N(0,1))", value = TRUE),
      
      hr(),
      h4("Current Parameters:"),
      verbatimTextOutput("paramText")
    ),
    
    mainPanel(
      plotOutput("tPlot")
    )
  )
)

server <- function(input, output) {
  
  output$tPlot <- renderPlot({
    df <- input$df
    
    # Dynamic x-range: central 99.9% interval
    xlim <- qt(0.9995, df = df)
    x <- seq(-xlim, xlim, length.out = 600)
    
    pdf_vals <- dt(x, df = df)
    cdf_vals <- pt(x, df = df)
    
    if (input$typ == 1) {
      # ---- PDF ----
      plot(x, pdf_vals, type = "l", lwd = 3, col = "coral3",
           main = sprintf("Student's t (df = %d) - Density (PDF)", df),
           xlab = "x", ylab = "Density",
           cex.lab = 1.2, cex.axis = 1.2, cex.main = 1.2)
      abline(h = 0, lty = 2)
      
      if (isTRUE(input$showNormal)) {
        lines(x, dnorm(x), lwd = 2, lty = 2, col = "gray30")
        legend("topright",
               legend = c(sprintf("t (df = %d)", df), "N(0,1)"),
               lty = c(1, 2), lwd = c(3, 2),
               col = c("coral3", "gray30"),
               bty = "n")
      }
      
    } else {
      # ---- CDF ----
      plot(x, cdf_vals, type = "l", lwd = 3, col = "steelblue",
           main = sprintf("Student's t (df = %d) - Distribution (CDF)", df),
           xlab = "x", ylab = "Cumulative probability",
           cex.lab = 1.2, cex.axis = 1.2, cex.main = 1.2)
      abline(h = c(0, 1), lty = 2)
    }
  })
  
  output$paramText <- renderText({
    df <- input$df
    
    mean_txt <- if (df > 1) "0" else "undefined (df ≤ 1)"
    var_txt  <- if (df > 2) sprintf("%g", df / (df - 2)) else "infinite (df ≤ 2)"
    
    paste0("Degrees of freedom (ν): ", df,
           "\nMean = ", mean_txt,
           "\nVariance = ", var_txt)
  })
}

shinyApp(ui = ui, server = server)