Course Project for Data Science Specialization (Developing Data Products)

Christopher Han
February 15, 2019

Introduction

The application takes in the sample mean, sample standard deviation, and the confidence level to calculate the corresponding lower and upper limits of a two-sided confidence interval. It also displays a graph with the lower and upper limits on the normal distribution.

The UI

The UI is quite simple with textbox, slider input and text, graph output.

shinyUI(fluidPage(

  # Application title
  titlePanel("Calculate Two-sided Confidence Intervals Using the Normal Distribution"),

  # code omitted

Calculations Performed on the Server

To summarize, the calculations performed on the server side takes in the input and calculates the confidence interval and outputs a plot corresponding to it.

library(shiny)
lower <- reactive({
                samplemean <- as.numeric(input$mu)
                samplesd <- as.numeric(input$sd)
                conflevel <- as.numeric(input$cl)
                round(qnorm(conflevel/100 + ((1-conflevel/100)/2),
                            mean = samplemean, sd = samplesd, lower.tail = FALSE), 3)
        })
#more code omitted below for presentation sake

Forming the Plot

In order to generate the plot, the lower and upper limits of the x-axis was calculated using two standard deviations each way. Then, we generated the density vector using dnorm then plotted using the base graphics.

output$plot1 <- renderPlot({
#more code omitted above for presentation sake
                lowerlim <- samplemean - 2*samplesd
                upperlim <- samplemean + 2*samplesd

                xseq <- seq(lowerlim, upperlim, 0.01)
                densities <- dnorm(xseq, samplemean, samplesd)

                plot(xseq, densities, col="darkgreen", xlab="", ylab="Density", type="l")
                abline(v = qnorm(conflevel/100 + ((1-conflevel/100)/2), mean = samplemean, sd = samplesd), col = "blue")
                abline(v = qnorm(conflevel/100 + ((1-conflevel/100)/2), mean = samplemean, sd = samplesd, lower.tail= FALSE), col = "blue")

})