try_again

Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.

Running Code

When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:

1 + 1
[1] 2

You can add options to executable code like this

[1] 4

The echo: false option disables the printing of code (only output is displayed).

#| '!! shinylive warning !!': |
#|   shinylive does not work in self-contained HTML documents.
#|   Please set `embed-resources: false` in your metadata.
#| standalone: true
#| viewerHeight: 650

library(shiny)
library(bslib)

ui <- fluidPage(
  titlePanel("Linear Transformation of a Normally Distributed Random Variable"),
  h4(HTML("Assume X is i.i.d. distributed from a N(μ<sub>x</sub>, 1) distribution")),
  sidebarLayout(
    sidebarPanel(
      br(), br(),  # Adding vertical spacing for centering
      sliderInput("mu_X", HTML("Select &mu;<sub>x</sub>:"), min = 2, max = 10, value = 5, step = 1),
      textInput("custom_transform", "Enter Linear Transformation (e.g., 2 + 4*X):", "2 + 4*X"),
      br(), br(),br(), br()
    ),
    mainPanel(
      plotOutput("distPlotX"),
      plotOutput("distPlotY"),
      plotOutput("distPlotCustom")
    )
  )
)

server <- function(input, output) {
  output$distPlotX <- renderPlot({
    mu_X <- input$mu_X
    sd_X <- 1  # Given that X ~ N(μ_x, 1)
    x_vals <- seq(mu_X - 4 * sd_X, mu_X + 4 * sd_X, length.out = 100)
    x_density_vals <- dnorm(x_vals, mean = mu_X, sd = sd_X)
    
    # Plot X distribution
    plot(x_vals, x_density_vals, type = "l", lwd = 2, col = "green",
         xlab = "X Values", ylab = "Density",
         main = bquote("Distribution of X (" ~ mu[x] ~ "= " * .(mu_X) * ")"))
    
    # Clip abline within the range of the density plot
    lines(rep(mu_X, 2), c(0, max(x_density_vals)), col = "red", lwd = 2, lty = 2)
  })
  
  output$distPlotY <- renderPlot({
    mu_X <- input$mu_X
    mu_Y <- 2 + 2 * mu_X
    sd_Y <- 2  # Given variance of 4, so std dev is sqrt(4) = 2
    y_vals <- seq(mu_Y - 4 * sd_Y, mu_Y + 4 * sd_Y, length.out = 100)
    y_density_vals <- dnorm(y_vals, mean = mu_Y, sd = sd_Y)
    
    # Plot Y distribution
    plot(y_vals, y_density_vals, type = "l", lwd = 2, col = "blue",
         xlab = "Y Values", ylab = "Density",
         main = bquote("Conditional Distribution of Y (" ~ mu[x] ~ "= " * .(mu_X) * ")"))
    
    # Clip abline within the range of the density plot
    lines(rep(mu_Y, 2), c(0, max(y_density_vals)), col = "red", lwd = 2, lty = 2)
  })
  
  output$distPlotCustom <- renderPlot({
    mu_X <- input$mu_X
    
    # Parse user input for transformation
    transform_expr <- gsub("X", as.character(mu_X), input$custom_transform)
    mu_Custom <- eval(parse(text = transform_expr))
    
    # Assume same variance transformation as Y (scaling variance by b^2 if transformation is a + bX)
    sd_Custom <- 2  # Placeholder for variance scaling logic
    custom_vals <- seq(mu_Custom - 4 * sd_Custom, mu_Custom + 4 * sd_Custom, length.out = 100)
    custom_density_vals <- dnorm(custom_vals, mean = mu_Custom, sd = sd_Custom)
    
    # Plot Custom transformation distribution
    plot(custom_vals, custom_density_vals, type = "l", lwd = 2, col = "purple",
         xlab = "Transformed Values", ylab = "Density",
         main = bquote("Conditional Distribution of " ~ .(input$custom_transform) ~ "(" ~ mu[x] ~ "= " * .(mu_X) * ")"))
    
    # Clip abline within the range of the density plot
    lines(rep(mu_Custom, 2), c(0, max(custom_density_vals)), col = "red", lwd = 2, lty = 2)
  })
}

shinyApp(ui = ui, server = server)

Another?