Below is the code for the Shiny App:
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
library(ggplot2)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Exploring Confidence Levels"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
numericInput("conflevel","Type your confidence level as a decimal between 0 and 1:",min=0.50,max=0.99,value=0.95,step=0.01),
numericInput("popmean", "Choose your population mean between 0 and 100:", min=0,max=100,value=75,step=5),
numericInput("Iterations", "Choose number of iterations between 100 and 1000:", min=100,max=1000,value=100, step=100)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot"),
br(),
br(),
textOutput("text"),
br(),
br(),
plotOutput("scatter")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
simdata <- reactive({
match <- NULL
CIlower <- NULL
CIupper <- NULL
CImiddle <- NULL
for(i in 1:input$Iterations){
x <- rnorm(10,mean=input$popmean)
y <- t.test(x,alternative="two.sided",conf.level = input$conflevel)
CIlower[i] <- y$conf.int[1]
CIupper[i] <- y$conf.int[2]
CImiddle[i] <- (CIlower[i]+CIupper[i])/2
if(CIlower[i]<=input$popmean && CIupper[i]>=input$popmean){
match[i] <- "YES"
}
else if(CIlower[i]>input$popmean || CIupper[i]<input$popmean){
match[i] <- "NO"
}
}
data.frame(i=1:input$Iterations, iend=1:input$Iterations, lower=CIlower, upper=CIupper, middle=CImiddle, match1=match)
})
output$distPlot <- renderPlot({
simdata <- simdata()
ggplot(,aes(x=simdata$middle,y=simdata$i,color=simdata$match1))+
geom_point()+geom_segment(data=simdata, aes(x=simdata$lower,xend=simdata$upper,y=simdata$i,yend=simdata$iend))+
geom_vline(xintercept=input$popmean)+
labs(title="How many confidence intervals capture the true population mean?", y="Intervals 1 to # of Iterations", x="Estimated Population Mean with Standard Error")+scale_color_manual(values=c("red", "green"), breaks=c("YES", "NO"), name="Captures the True Population Mean?")
})
output$scatter <- renderPlot({
simdata <- simdata()
plot1 <- ggplot(, aes(x=simdata$lower, y=simdata$upper, color=simdata$match1))
plot1+geom_point()+geom_vline(xintercept=input$popmean)+
geom_hline(yintercept=input$popmean)+
labs(title="What do these results look like in scatterplot form?", y="Upper Bound", x="Lower Bound")+scale_color_manual(values=c("red", "green"), breaks=c("YES", "NO"), name="Captures the True Population Mean?")
})
output$text <- renderText({
simdata <- simdata()
sent <- paste((sum(simdata$match1=="YES")/input$Iterations)*100, "% of the calculated CIs captured the true population mean.")
print(sent)
})
}
# Run the application
shinyApp(ui = ui, server = server)
Login