12/05/2020

Idea of the project

In this shiny project I created simple Z-score calculator.
It has 2 static input fields: mean and standard deviation.
Third field for X - is created dynamically as per values of mean and standard deviation.
User can change any of the fields - and slider for X will be rebuilt accordingly.

Code of UI

sidebarPanel(
            numericInput("mu", "Population mean:", value = 5),
            numericInput("sd", "Standard deviation:", value=1),

  # Dynamic field, it will be instantiated from server.R code:
            uiOutput("valControl"),

  # Two lines of instructions about the applet:
            textOutput("info1"),
            textOutput("info2")
        )

Plot code is not included, you can find it at: https://github.com/A000ANB/zcalc.git

Code of server.R

Initialization of slider. Minimal and maximum values of the slider depend on the mean and sd values!

output$valControl<-renderUI({
        sliderInput("x", "Raw value:", min=input$mu - 5*input$sd, 
               max=input$mu + 5*input$sd, value=input$mu, step=0.2)
    })

Another interesting piece of code for calculation of qnorm(Z):

qn<-reactive({
       round(pnorm( as.double((input$x-input$mu)/input$sd)), 5)
   })

Code of server.R - 2

    output$zval<-renderText(paste("Z-score = ", 
                             as.double((input$x-input$mu)/input$sd)))
    output$zval1<-renderText(paste(" Probability of x <",
                                   input$x, "is", qn()))
    output$zval2<-renderText(paste(" Probability of x >",
                                   input$x, "is", 1-qn()))                              
    output$distPlot <- renderPlot({
         # draw the normal curve
         curve(dnorm(x,0,1), xlim=c(-5, 5))
         # define shaded region
         from.z <- -5
         to.z <- (input$x-input$mu)
         S.x  <- c(from.z, seq(from.z, to.z, 0.01), to.z)
         S.y  <- c(0, dnorm(seq(from.z, to.z, 0.01)), 0)
         polygon(S.x,S.y, col="red")
    })

Instructions

Change mean, standard deviation and slide X to see calculated Z-score and probabilities of P(x>Z) and P(x<Z)
Note, the graph mathematically doesn’t work correctly, but all digits are!

https://a000anb.shinyapps.io/Z-calculator/