Site icon R-bloggers

BlogAnalytics 2014-11-10 12:03:00

[This article was first published on BlogAnalytics, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.


View Confidence Interval Dynamically


This is a simple effort for dynamic view of confidence interval in a normal distribution. The  link provides a interactive interface which helps users to understand normal distribution and confidence interval.



Its a simple application that takes the mean, standard deviation, upper bound and lower bound, and marks the confidence interval in the normal distribution. 


Application is created by Shiny App. Shiny App is a very simple R tool to create interactive apps on browser. Which can be hosted online. 

Shiny app tutorials are available here.
Below is the code for the above app.
ui.R
shinyUI(fluidPage(
  titlePanel("Confidence Interval",windowTitle = "Normal Distribution Confidence Interval"),
  sidebarLayout(
    sidebarPanel(
      p("Provide the mean, Standard Deviation, 
        Upper Bound and Lower Bound to plot confidence Interval on Normal Plot"),
      numericInput("mean", label = h4("Mean"), value = 0),
      numericInput("sd", label = h4("Standard Deviation(not Variance)"), value = 1),
      numericInput("lb", label = h4("Lower Bound"), value = -1),
      numericInput("ub", label = h4("Upper Bound"), value = 1)
 
      ),
    mainPanel(plotOutput("map"))
        )
    )
  )
 
server.R
normal = function(mean, sd, lb,ub){
  x <- seq(-4,4,length=100)*sd + mean
  hx <- dnorm(x,mean,sd)
 
  plot(x, hx, type="n", ylab="",
       main="Normal Distribution", axes=FALSE)
 
  i <- x >= lb & x <= ub
  lines(x, hx)
  polygon(c(lb,x[i],ub), c(0,hx[i],0), col="red")
 
  area <- pnorm(ub, mean, sd) - pnorm(lb, mean, sd)
  result <- paste("P(",lb,"< IQ <",ub,") =",
                  signif(area, digits=3))
  mtext(result,3)
  axis(1, at=seq(mean-4*sd, mean+4*sd, sd), pos=0) 
}
 
shinyServer(function(input, output){
   output$map = renderPlot({normal(input$mean,input$sd,input$lb,input$ub)
 
   })      
 
}
 
  )
To host an app online follow the below steps:
1- sign up here using your google account.
2 – It will ask to create a “shinyapps.io” account (eg . “myshinyapps.io”). once you create your shinyapp.io account, it will give a token.
3- Install packages “shiny” and “shinyApps”. (to install “shinyApp” follow the instruction here.
4- Create your shiny app and test locally.
5- Once done, go to your shinyapp.io account, and deploy your app using below R command


deployApp(“<direcory where app code is present>”, appName = “name of your app”,


          account = “shinyapps.io account name”, upload = TRUE)

this will upload your ui.R and server.R files and your shiney app is hosted online.


find my app at 
https://sahuvaibhav.shinyapps.io/Stats/











To leave a comment for the author, please follow the link and comment on their blog: BlogAnalytics.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.