/40
+2 completion (turned in on time, turned in Rmd and Html…)
The below code sets up a data frame, df that you will
use in order to create a shiny app that compares the t distribution to
the Z distribution at different levels of degrees of freedom (n-1). Your
n term will be the input value for your shiny app.
n = 10
x <- seq(-4, 4, by=0.01)
norm_dens <- dnorm(x)
t_dens <- dt(x, df = n-1)
df = data.frame(x = x, z = norm_dens, t = t_dens)
Write your code for the shiny app below. I’ve set up the UI, Server, and Run steps. You will fill in the details so that when you run the user interface and server logic together, the output will include:
Static standard Normal distribution curve in blue
using ggplot geom_line()
Dynamic t distribution curve that is dependent on degrees of
freedom (n-1) in red using ggplot
geom_line()
A slider input that determines the sample size from minimum value of 2 (since df can be no less than 1) to maximum value of 100, stepping by 1
+2 Normal distribution is shown in blue, is static (changing n will not change the shape of the curve) +2 t distribution depends on the sample size (n) via its degrees of freedom (changing n will make the t approach the Normal distribution in shape) +1 t distribution is red (+1) +2 slider changes the sample size from 2 to 100, stepping by 1 +1 Using probability (or density or rel freq) on y axis and x on x-axis where x ranges from -4 to 4
library(shiny)
## Warning: package 'shiny' was built under R version 4.3.3
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.3
## UI function
ui <- fluidPage(
titlePanel("Comparing t-Distribution to the Normal (Z Distribution)"),
sidebarLayout(
sidebarPanel(
sliderInput("n", "Sample Size (n):",
min = 2, max = 100, value = 2, step = 1)
),
mainPanel(
plotOutput("distPlot")
)
)
)
# Server logic
server <- function(input, output) {
output$distPlot <- renderPlot({
x <- seq(-4, 4, by = 0.01)
norm_dens <- dnorm(x)
t_dens <- dt(x, df = input$n - 1)
df <- data.frame(x = x, z = norm_dens, t = t_dens)
ggplot(df, aes(x = x)) +
geom_line(aes(y = z), color = "blue", size = 1) +
geom_line(aes(y = t), color = "red", size = 1) +
labs(title = paste("n =", input$n, "(df =", input$n - 1, ")"),
y = "Density", x = "x") +
theme_minimal()
})
}
## Run shiny app
shinyApp(ui, server)
##
## Listening on http://127.0.0.1:8821
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
Commentary: The blue curve (normal distribution) stays the same since it
does not depend on sample size. Meanwhile, the red curve
(t-distribution) changes as n alters, as its shape depends on degree of
freedom (df=n-1). As n increases, the t distribution tends to approach
the normal distribution.
Using the iris data set, create a scatterplot using the
shiny app. The graph should include some interactive capability to
select which variables to compare on the x and y axes.
+4 x and y variables can be selected in a sidebar layout +1 user interface (sidebar) interacts with the server so that selections appear in graph +1 all combinations of data are displayed as a scatterplot +1 x and y axis selections change correct axes +2 formatting Is legible and seems to be showing the data appropriately
## UI Function
library(shiny)
ui <- fluidPage(
titlePanel("Iris Dataset Interface"),
sidebarLayout(
sidebarPanel(
selectInput("x_var", "X-Axis Variable:",
choices = names(iris)[1:4], selected = "Sepal.Length"),
selectInput("y_var", "Y-Axis Variable:",
choices = names(iris)[1:4], selected = "Sepal.Width")
),
mainPanel(
plotOutput("scatter")
)
)
)
## Server logic
server <- shinyServer(function(input, output) {
output$scatter <- renderPlot({
ggplot(iris, aes(x = .data[[input$x_var]], y = .data[[input$y_var]], color = Species)) +
geom_point(size = 3, alpha = 0.7) +
labs(x = input$x_var, y = input$y_var, title = "Scatterplot of Iris Dimensions") +
theme_light()
})
}
)
## Run shiny app
shinyApp(ui, server)
##
## Listening on http://127.0.0.1:7089
Commentary: The interface displays the iris dataset. The x and y axes can be customized based on user selections. The categorical variable of this dataset (Species) is displayed as colors. The scatterplot graph changes according to the side bar options chosen by users.