Skip to content

Instantly share code, notes, and snippets.

@calpolystat
Last active May 25, 2022 13:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save calpolystat/200f26d243f4f5bb7334 to your computer and use it in GitHub Desktop.
Save calpolystat/200f26d243f4f5bb7334 to your computer and use it in GitHub Desktop.
Probability Distribution Viewer: Shiny app at http://www.statistics.calpoly.edu/shiny
Probability Distribution Viewer Shiny App
Base R code created by Irvin Alcaraz
Shiny app files created by Irvin Alcaraz
Cal Poly Statistics Dept Shiny Series
http://statistics.calpoly.edu/shiny
Title: Probability Distribution Viewer
Author: Irvin Alcaraz
AuthorUrl: https://www.linkedin.com/in/irvinalcaraz
License: MIT
DisplayMode: Normal
Tags: Probability Distribution
Type: Shiny
The MIT License (MIT)
Copyright (c) 2015 Irvin Alcaraz
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
##############################################
# App Title: Probability Distribution Viewer #
# Author: Irvin Alcaraz #
##############################################
###################
# Misc Functions #
###################
library(shinysky)
library(shiny)
##Function to convert all values to xvalues so I can standardly
##use the density distribution functions
xvalue = function(value,type,dist,params){
switch(type,
p = do.call(paste("q",dist,sep=""),c(value,params)),
d = value
)
}
###################
#Shiny Server Code#
###################
shinyServer(function(input, output, session) {
observe({
if(input$dist == "beta"){
if(input$p1.beta <= 0 || input$p2.beta <= 0 || is.na(input$p1.beta) || is.na(input$p2.beta)){
showshinyalert(session,"shinyalert1",
"Looks like something is wrong in your parameters, please check them and then hit submit again",
"danger")
}else return ()
}else return ()
})
observe({
if(input$dist == "cauchy"){
if(input$p2.cauchy <= 0 || is.na(input$p2.cauchy)){
showshinyalert(session,"shinyalert2",
"Looks like something is wrong in your parameters, please check them and then hit submit again",
"danger")
}else return()
}else return()
})
observe({
if(input$dist == "chisq"){
if(input$p1.chisq <= 0 || is.na(input$p1.chisq)){
showshinyalert(session,"shinyalert3",
"Looks like something is wrong in your parameters, please check them and then hit submit again",
"danger")
}else return()
}else return()
})
observe({
if(input$dist == "exp"){
if(input$p1.exp <= 0 || is.na(input$p1.exp)){
showshinyalert(session,"shinyalert4",
"Looks like something is wrong in your parameters, please check them and then hit submit again",
"danger")
}else return()
}else return()
})
observe({
if(input$dist == "f"){
if(input$p1.f <= 0 || input$p2.f <= 0 || is.na(input$p1.f) || is.na(input$p2.f)){
showshinyalert(session,"shinyalert5",
"Looks like something is wrong in your parameters, please check them and then hit submit again",
"danger")
}else return()
}else return()
})
observe({
if(input$dist == "gamma"){
if(input$p1.gamma <= 0 || input$p2.gamma <= 0 || is.na(input$p1.gamma) || is.na(input$p2.gamma)){
showshinyalert(session,"shinyalert6",
"Looks like something is wrong in your parameters, please check them and then hit submit again",
"danger")
}else return()
}else return()
})
observe({
if(input$dist == "logis"){
if(input$p2.logis <= 0 || is.na(input$p2.logis)){
showshinyalert(session,"shinyalert7",
"Looks like something is wrong in your parameters, please check them and then hit submit again",
"danger")
}else return()
}else return()
})
observe({
if(input$dist == "lnorm"){
if(input$p1.lnorm <= 0 || input$p2.lnorm <= 0 || is.na(input$p1.lnorm) || is.na(input$p2.lnorm)){
showshinyalert(session,"shinyalert8",
"Looks like something is wrong in your parameters, please check them and then hit submit again",
"danger")
}else return()
}else return()
})
observe({
input$go
isolate({
if(input$dist == "t"){
if(input$p1.t <= 0 || is.na(input$p1.t)){
showshinyalert(session,"shinyalert9",
"Looks like something is wrong in your parameters, please check them and then hit submit again",
"danger")
}else return()
}else return()
})
})
observe({
if(input$dist == "weibull"){
if(input$p1.weibull <= 0 || input$p2.weibull <= 0 || is.na(input$p1.weibull) || is.na(input$p2.weibull)){
showshinyalert(session,"shinyalert10",
"Looks like something is wrong in your parameters, please check them and then hit submit again",
"danger")
}else return()
}else return()
})
observe({
if(input$dist == "norm"){
if(input$p2.norm <= 0 || is.na(input$p1.norm) || is.na(input$p2.norm)){
showshinyalert(session,"shinyalert11",
"Looks like something is wrong in your parameters, please check them and then hit submit again",
"danger")
}else return()
}else return()
})
output$distPlot <- renderPlot({
input$go
isolate({
##Each distribution has a different number of parameters
##so I create lists that contain them based on the distribution.
##I omitted the ability to have a threshold and NCP for all distributions.
##I originally had very general code for this but it ended up causing to many
##problems so this method works.
params = switch(input$dist,
"beta"= list(input$p1.beta,input$p2.beta),
"cauchy" = list(input$p1.cauchy,input$p2.cauchy),
"chisq"=list(input$p1.chisq),
"exp" = list(input$p1.exp),
"f"=list(input$p1.f,input$p2.f),
"gamma"= list(input$p1.gamma,input$p2.gamma),
"logis" = list(input$p1.logis,input$p2.logis),
"lnorm" = list(input$p1.lnorm,input$p2.lnorm),
"norm" = list(input$p1.norm,input$p2.norm),
"t" = list(input$p1.t),
"unif" = list(input$p1.unif,input$p2.unif),
"weibull" = list(input$p1.weibull,input$p2.weibull)
)
##Distributions I still can implement
##Continuous
##tukey
##Discrete
##binom geom hypergeometric nbinom pois
##Nonparametric
##Wilcoxon
##These if statements take the value to be shaded and converts it
##to xvalues that will later be used by the polygon function
if (!is.null("input$shadeval2")){
xvalue1 = xvalue(input$shadeval1,input$type,input$dist,params)
xvalue2 = xvalue(input$shadeval2,input$type,input$dist,params)
}else{
xvalue1 = xvalue(input$shadeval1,input$type,input$dist,params)
}
##These if statements draw the graphs based on the different distributions
##Some Distributions were easier to graph through a more specific method so
##they are separated from the others
if (input$dist == 'beta'
|| input$dist == 'logis' || input$dist == 'norm'
|| input$dist == 't' || input$dist == 'unif'){
minx = do.call(paste("q",input$dist,sep=""),c(.0001,params))
maxx = do.call(paste("q",input$dist,sep=""),c(.9999,params))
x = seq(from=minx,to=maxx,length=1000)
hx = x
for(k in 1:1000){
hx[k] = do.call(paste("d",input$dist,sep=""),c(x[k],params))
}
miny = 0
miny = 0
if (is.infinite(max(hx)) || max(hx)>1)
{
maxy = 1
}else{
maxy = round(max(hx),digits=2)
}
plot(x,hx,type="n",xlab="X",ylab="Density",
main="Probability Density",axes=FALSE,ylim=c(miny,maxy))
lines(x,hx)
# axis(1,pos=0,col.axis="grey",col.ticks="grey",col="grey")
axis(1,pos=0)
axis(2,at=round(seq(from=miny,to=maxy,length=5),digits=3),pos=minx)
} else if (input$dist == 'chisq' || input$dist == 'exp'
|| input$dist == 'f' || input$dist == 'gamma'
|| input$dist == 'lnorm' || input$dist == 'weibull'){
minx = 0
maxx = do.call(paste("q",input$dist,sep=""),c(.999,params))
x = NULL
x = seq(from=minx,to=maxx,length=1000)
hx = x
for(k in 1:1000){
hx[k] = do.call(paste("d",input$dist,sep=""),c(x[k],params))
}
miny = 0
if (is.infinite(max(hx)) || max(hx)>1)
{
maxy = 1
}else{
maxy = round(max(hx),digits=2)
}
plot(x,hx,type="n",xlab="X",ylab="Density",
main="Probability Density",axes=FALSE,ylim=c(miny,maxy))
lines(x,hx)
axis(1,pos=0)
axis(2,at=round(seq(from=miny,to=maxy,length=5),digits=3),pos=minx)
} else if (input$dist == 'cauchy'){
minx = do.call(paste("q",input$dist,sep=""),c(.04,params))
maxx = do.call(paste("q",input$dist,sep=""),c(.96,params))
x = NULL
x = seq(from=minx,to=maxx,length=1000)
hx = x
for(k in 1:1000){
hx[k] = do.call(paste("d",input$dist,sep=""),c(x[k],params))
}
miny = 0
if (is.infinite(max(hx)) || max(hx)>1)
{
maxy = 1
}else{
maxy = round(max(hx),digits=2)
}
plot(x,hx,type="n",xlab="X",ylab="Density",
main="Probability Density",axes=FALSE,ylim=c(miny,maxy))
lines(x,hx)
axis(1,pos=0)
axis(2,at=round(seq(from=miny,to=maxy,length=5),digits=3),pos=minx)
}
# # Debug
# plot.new()
# title(paste(maxx))
if (input$type != 'none'){
# These if statements shade the graphs correctly
if(input$shade=="left"){
i = x<=xvalue1
polygon(c(minx,x[i],xvalue1),c(0,hx[i],0),col="deepskyblue3")
area = do.call(paste("p",input$dist,sep=""),c(xvalue1,params))
result = paste("P(X<",signif(xvalue1,digits=4),")=",signif(area,digits=3))
mtext(result,3)
axis(1,at=xvalue1,pos=0,col.ticks="red",col.axis="red",lwd.ticks = 2,cex.axis=2)
}else if(input$shade=="right"){
i = x>=xvalue1
polygon(c(xvalue1,x[i],maxx),c(0,hx[i],0),col="deepskyblue3")
area = 1-do.call(paste("p",input$dist,sep=""),c(xvalue1,params))
result = paste("P(X>",signif(xvalue1,digits=4),")=",signif(area,digits=3))
mtext(result,3)
axis(1,at=xvalue1,pos=0,col.ticks="red",col.axis="red",lwd.ticks = 2,cex.axis=2)
}else if(input$shade=="middle"){
i = (x>=xvalue1)&(x<=xvalue2)
polygon(c(xvalue1,x[i],xvalue2),c(0,hx[i],0),col="deepskyblue3")
area = do.call(paste("p",input$dist,sep=""),c(xvalue2,params))-
do.call(paste("p",input$dist,sep=""),c(xvalue1,params))
result = paste("P(",signif(xvalue1,digits=4),"< X <",signif(xvalue2,digits=4),
")=",signif(area,digits=3))
mtext(result,3)
axis(1,at=c(xvalue1,xvalue2),pos=0,col.ticks="red",col.axis="red",lwd.ticks = 2,,cex.axis=2)
}else if(input$shade=="both"){
i = (x<=xvalue1)
j = (x>=xvalue2)
polygon(c(minx,x[i],xvalue1),c(0,hx[i],0),col="deepskyblue3")
polygon(c(xvalue2,x[j],maxx),c(0,hx[j],0),col="deepskyblue3")
area = (1-do.call(paste("p",input$dist,sep=""),c(xvalue2,params)))+
do.call(paste("p",input$dist,sep=""),c(xvalue1,params))
result = paste("P(X < ",signif(xvalue1,digits=4)," or X > ",signif(xvalue2,digits=4),
")=",signif(area,digits=3))
mtext(result,3)
axis(1,at=c(xvalue1,xvalue2),pos=0,col.ticks="red",col.axis="red",lwd.ticks = 2,cex.axis=2)
}
}
})
})
})
##############################################
# App Title: Probability Distribution Viewer #
# Author: Irvin Alcaraz #
##############################################
if (!require("devtools"))
install.packages("devtools")
if (!require("shinysky")) devtools::install_github("ShinySky","AnalytixWare")
library(shinysky)
library(shiny)
shinyUI(fluidPage(
tags$head(tags$link(rel = "icon", type = "image/x-icon",
href = "https://webresource.its.calpoly.edu/cpwebtemplate/5.0.1/common/images_html/favicon.ico")),
titlePanel("Probability Viewer"),
sidebarLayout(
sidebarPanel(
selectInput("dist",label=h4("Distribution"),
choices=c("Beta" = "beta", "Cauchy" = "cauchy", "Chi-Squared" = "chisq",
"Exponential" = "exp","F" = "f", "Gamma" = "gamma",
"Logistic" = "logis","Log Normal" = "lnorm",
"Normal"="norm","Student t" = "t","Uniform" = "unif",
"Weibull" = "weibull"),selected="norm"),
shinyalert("shinyalert1", TRUE, auto.close.after=5),
shinyalert("shinyalert2", TRUE, auto.close.after=5),
shinyalert("shinyalert3", TRUE, auto.close.after=5),
shinyalert("shinyalert4", TRUE, auto.close.after=5),
shinyalert("shinyalert5", TRUE, auto.close.after=5),
shinyalert("shinyalert6", TRUE, auto.close.after=5),
shinyalert("shinyalert7", TRUE, auto.close.after=5),
shinyalert("shinyalert8", TRUE, auto.close.after=5),
shinyalert("shinyalert9", TRUE, auto.close.after=5),
shinyalert("shinyalert10", TRUE, auto.close.after=5),
shinyalert("shinyalert11", TRUE, auto.close.after=5),
conditionalPanel(condition = "input.dist=='beta'",
numericInput("p1.beta","First Shape",2,min=1)),
conditionalPanel(condition = "input.dist=='beta'",
numericInput("p2.beta","Second Shape",2,min=1)),
conditionalPanel(condition = "input.dist=='cauchy'",
numericInput("p1.cauchy","Location",0)),
conditionalPanel(condition = "input.dist=='cauchy'",
numericInput("p2.cauchy","Scale",2,min=1)),
conditionalPanel(condition = "input.dist=='chisq'",
numericInput("p1.chisq","DF",5,min=1)),
conditionalPanel(condition = "input.dist=='exp'",
numericInput("p1.exp","Rate",1,min=0)),
conditionalPanel(condition = "input.dist=='f'",
numericInput("p1.f","Num DF",20,min=1)),
conditionalPanel(condition = "input.dist=='f'",
numericInput("p2.f","Denom DF",20,min=1)),
conditionalPanel(condition = "input.dist=='gamma'",
numericInput("p1.gamma","Shape",1,min=0)),
conditionalPanel(condition = "input.dist=='gamma'",
numericInput("p2.gamma","Scale",1,min=0)),
conditionalPanel(condition = "input.dist=='logis'",
numericInput("p1.logis","Location",0)),
conditionalPanel(condition = "input.dist=='logis'",
numericInput("p2.logis","Scale",1,min=0)),
conditionalPanel(condition = "input.dist=='lnorm'",
numericInput("p1.lnorm","Log Mean",0,min=0)),
conditionalPanel(condition = "input.dist=='lnorm'",
numericInput("p2.lnorm","Log Standard Deviation",1,min=0)),
conditionalPanel(condition = "input.dist=='norm'",
numericInput("p1.norm","Mean",0)),
conditionalPanel(condition = "input.dist=='norm'",
numericInput("p2.norm","Standard Deviation",1,min = 0)),
conditionalPanel(condition = "input.dist=='t'",
numericInput("p1.t","DF",5,min=1)),
conditionalPanel(condition = "input.dist=='unif'",
numericInput("p1.unif","Minimum",0)),
conditionalPanel(condition = "input.dist=='unif'",
numericInput("p2.unif","Maximum",1)),
conditionalPanel(condition = "input.dist=='weibull'",
numericInput("p1.weibull","Shape",1,min=0)),
conditionalPanel(condition = "input.dist=='weibull'",
numericInput("p2.weibull","Scale",1,min=0)),
HTML("<hr style='height: 2px; color: #F3F3F3; background-color: #F3F3F3; border: none;'>"),
radioButtons("type",label=h4("Define Shaded Area By"),
choices=c("Input percentile and calculate probability"="d",
"Input probability and calculate percentile"="p", "Nothing"="none"),selected="none"),
HTML("<hr style='height: 2px; color: #F3F3F3; background-color: #F3F3F3; border: none;'>"),
conditionalPanel(condition = "input.type != 'none'",
selectInput("shade",label=h4("Area to shade"),
choices=c("Left Tail"="left","Right Tail"="right",
"Both Tails"="both","Middle"="middle"))),
# conditionalPanel(condition = "input.shade == 'left' || input.shade == 'right' ||
# input.shade == 'both' || input.shade == 'middle' ",
conditionalPanel(condition = "input.type != 'none'",
numericInput("shadeval1",label=" ",0)),
conditionalPanel(condition = "(input.shade == 'both' || input.shade=='middle') &&
input.type != 'none'",
numericInput("shadeval2",label=" ",value=0)),
#HTML("<hr style='height: 2px; color: #F3F3F3; background-color: #F3F3F3; border: none;'>"),
actionButton("go","Submit"),
div("Shiny app by",
a(href="https://www.linkedin.com/in/irvinalcaraz",target="_blank",
"Irvin Alcaraz"),align="right", style = "font-size: 8pt"),
div("Base R code by",
a(href="https://www.linkedin.com/in/irvinalcaraz",target="_blank",
"Irvin Alcaraz"),align="right", style = "font-size: 8pt"),
div("Shiny source files:",
a(href="https://gist.github.com/calpolystat/200f26d243f4f5bb7334",
target="_blank","GitHub Gist"),align="right", style = "font-size: 8pt"),
div(a(href="http://www.statistics.calpoly.edu/shiny",target="_blank",
"Cal Poly Statistics Dept Shiny Series"),align="right", style = "font-size: 8pt")
),
mainPanel(
plotOutput("distPlot"),
tags$style(type="text/css",
".shiny-output-error { visibility: hidden; }",
".shiny-output-error:before { visibility: hidden; }"
)
)
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment