# Basic Probability" In PROBShiny: Interactive Document for Working with Basic Probability

```knitr::opts_chunk\$set(echo = TRUE)
library(LaplacesDemon)
library(shinyMatrix)
library(epitools)
library(rpivotTable)
```

## Subjective Approach - Bayes Theorem

```sidebarPanel(
inputPanel(
textAreaInput("eventA",label = "Describe event A in simple sentence", value = "The student attends Classes on Fridays"),
textAreaInput("eventB",label = "Describe event B in simple sentence", value = "The student passes the course")
)
)

mainPanel(
inputPanel(
numericInput("PriorA",label = "Enter Prior Probability of A",min = 0,max =1 ,value = 0.6)
),
inputPanel(
numericInput("BA",label = "Enter Conditional Probability of B given A",min = 0,max =1 ,value = 0.98),
numericInput("BAc",label = "Enter Conditional Probability of B given A_Complement",min = 0,max =1 ,value = 0.2)

))
verbatimTextOutput("rs")

output\$rs <-renderPrint({

PrA <- c(input\$PriorA,1-input\$PriorA)
PrBA <- c(input\$BA, input\$BAc)
result =BayesTheorem(PrA, PrBA)
JPB = (input\$PriorA) * (input\$BA)/(result)
cat(sprintf("\n Let A be the event indicating that %s",input\$eventA))
cat(sprintf("\n Let B be the event indicating that %s",input\$eventB))
cat(sprintf("\nPrior Probability of occurence of event A is %f",input\$PriorA))
cat(sprintf("\nJoint Probability of occurence of all possibilites of event B is %f",JPB))
cat(sprintf("\nRevised Probability of occurence of event A given event B has occured is %f",result))
})
```

## Relative Frequency Approach - Contingency Table

```sidebarPanel(
inputPanel(numericInput("nrows","Enter number of rows",value = 2),
numericInput("ncols","Enter number of cols", value = 3)),
textInput("vname1","Enter the name of the first variable","Variable1"),
textInput("vname2","Enter the name of the second variable","Variable2"),
selectInput("ProbType",label="Select the Probability type",choices = c("Joint","ConditionalbasedonRow","ConditionalbasedonColumn"),selected = "Joint")

)
mainPanel(
h6("Edit the observed frequencies and Enter the dimensions(on the margins)"),
uiOutput("mat"),
tabsetPanel(type = "tab",
tabPanel("ContingencyTable",rpivotTableOutput("Dashboard")),
tabPanel ("Check for statistical Independence",verbatimTextOutput("Ind")),
tabPanel ("Visual  Check for statistical Independence",plotOutput("IndVis"))

),
h6("", tags\$img(src ="K.JPG", height= 400, width=400))

)
output\$mat <- renderUI({
matrixInput(
"myMatrix",
value = matrix(c(35,91,42,104,63,65),input\$nrows,input\$ncols),

rows = list(names= TRUE,editableNames = TRUE),
cols = list( names = TRUE,editableNames = TRUE),
copy = TRUE,
paste = TRUE,
class = 'numeric'
)
})
output\$Dashboard <- renderRpivotTable({
CT =  matrix(input\$myMatrix,nrow = input\$nrows, ncol = input\$ncols)
row.names(CT) = row.names(input\$myMatrix)
colnames(CT) = colnames(input\$myMatrix)

dataset = expand.table(CT)
colnames(dataset)= c(input\$vname1,input\$vname2)
if(input\$ProbType == "Joint")
{agg = "Count as Fraction of Total" }
if(input\$ProbType == "ConditionalbasedonRow")
{agg = "Count as Fraction of Rows" }
if(input\$ProbType == "ConditionalbasedonColumn")
{agg = "Count as Fraction of Columns" }

rpivotTable(data = dataset , rows = input\$vname1,cols=input\$vname2,
aggregatorName = agg, rendererName = "Table")

})

output\$Ind <- renderPrint({
CT =  matrix(input\$myMatrix,nrow = input\$nrows, ncol = input\$ncols)
row.names(CT) = row.names(input\$myMatrix)
colnames(CT) = colnames(input\$myMatrix)
cat(sprintf("\n The observed frequencies are as follows\n"))
print(CT)
cat(sprintf("\n The corresponding expected frequencies are as follows\n"))
print(expected(CT))

if( all(expected(CT) == CT))
{

cat(sprintf("\n Since expected frequencies are equal to corresponding observed frequencies events are  statistically independent "))
cat(sprintf("\n You will see  just a horizontal line when you check it visually"))

}
else
{
cat(sprintf("\n Since expected frequencies are not equal to corresponding observed frequencies events are not statistically independent"))
cat(sprintf("\n You will see  the association plot with bars in either direction when you check this visually as the bars indicate a relationship or association"))

}

})

output\$IndVis <- renderPlot({
CT =  matrix(input\$myMatrix,nrow = input\$nrows, ncol = input\$ncols)
row.names(CT) = row.names(input\$myMatrix)
colnames(CT) = colnames(input\$myMatrix)

assocplot(CT, col = c("green","red"))

})
```

## Try the PROBShiny package in your browser

Any scripts or data that you put into this service are public.

PROBShiny documentation built on May 2, 2019, 8:52 a.m.