diff --git a/04_testing/x_score/index.qmd b/04_testing/x_score/index.qmd new file mode 100644 index 0000000..44c306b --- /dev/null +++ b/04_testing/x_score/index.qmd @@ -0,0 +1,292 @@ +--- +title: "x-점수 확률계산" +author: "이광춘" +date: today +image: thumbnail.png +categories: ["z-점수", "t-점수", "카이제곱-점수", "확률계산"] +editor_options: + chunk_output_type: console +--- + +"z-점수", "t-점수", "카이제곱-점수", 확률계산은 가설 검정과 신뢰구간 계산에서 유용합니다. 많은 통계적 가설 검정에서 z-점수를 사용하여 유의 확률(p-value)을 계산하며, 특정 z-점수에 해당하는 누적 확률을 빠르게 찾을 수 있습니다. 또한, 정규분포를 따르는 데이터의 신뢰구간 계산 시, z-점수를 사용하여 신뢰구간의 상한과 하한을 결정할 수 있습니다. t-점수는 주로 표본 크기가 작거나 모집단의 표준편차를 모를 때 사용되며, t-검정을 통해 평균 간의 차이를 검정하거나 신뢰구간을 계산하는 데 사용됩니다. 카이제곱-점수는 주로 범주형 데이터의 독립성 검정이나 적합도 검정에서 사용되며, 관측된 빈도와 기대 빈도 간의 차이를 평가하는 데 유용합니다. + + +# Shiny 앱 + +:::{.column-page} + +```{shinylive-r} +#| label: shinylive-testing-score +#| viewerWidth: 800 +#| viewerHeight: 700 +#| standalone: true + +library(shiny) +library(showtext) +showtext_auto() + +# Define UI for application that draws a histogram +ui <- fluidPage( + # Application title + titlePanel("통계 검정에 중요한 점수의 변화에 따른 확률 계산"), + + tags$div(HTML(" + ")), + + # Sidebar with inputs and options + sidebarLayout( + sidebarPanel( + radioButtons("scoreType", "점수 선택:", + c("Z-score" = "z", + "T-score" = "t", + "Chi-square 점수" = "chisq")), + conditionalPanel( + condition = "input.scoreType == 'z'", + sliderInput("z", "Z-score", min = -5, max = 5, step = 0.01, ticks = TRUE, value = 1.96) + ), + conditionalPanel( + condition = "input.scoreType == 't'", + sliderInput("t", "T-score", min = -5, max = 5, step = 0.01, ticks = TRUE, value = 1.96), + numericInput("df_t", "자유도", value = 10, min = 1, step = 1) + ), + conditionalPanel( + condition = "input.scoreType == 'chisq'", + sliderInput("chisq", "Chi-square 점수", min = 0, max = 20, step = 0.01, ticks = TRUE, value = 3.84), + numericInput("df_chisq", "자유도", value = 1, min = 1, step = 1) + ), + withMathJax(), + p("$P(X \\leq x) =$"), + textOutput("prob"), + hr(), + p("신뢰수준 (양측):"), + textOutput("conf_level"), + p("대응하는 점수:"), + textOutput("score") + ), + + # Show a plot of the generated distribution + mainPanel( + plotOutput("plot") + ) + ) +) + +# Define server logic required to draw a histogram +server <- function(input, output) { + + score <- reactive({ + switch(input$scoreType, + "z" = input$z, + "t" = input$t, + "chisq" = input$chisq) + }) + + output$prob <- renderPrint({ + switch(input$scoreType, + "z" = pnorm(score()), + "t" = pt(score(), df = input$df_t), + "chisq" = pchisq(score(), df = input$df_chisq)) + }) + + library(ggplot2) + library(gridExtra) + + # manually save colors + col1 <- "#3B429F" + col2 <- "#76BED0" + col3 <- "#F55D3E" + + output$plot <- renderPlot({ + # useful "shader" function taken from: https://t-redactyl.io/blog/2016/03/creating-plots-in-r-using-ggplot2-part-9-function-plots.html + funcShaded <- function(x) { + y <- switch(input$scoreType, + "z" = dnorm(x), + "t" = dt(x, df = input$df_t), + "chisq" = dchisq(x, df = input$df_chisq)) + y[x > score()] <- NA + return(y) + } + + p1 <- ggplot(data.frame(x = c(ifelse(input$scoreType == "chisq", 0, -20), 20)), aes(x = x)) + + stat_function(fun=funcShaded, geom="area", fill=col2, alpha=0.6) + + stat_function(fun = switch(input$scoreType, + "z" = dnorm, + "t" = function(x) dt(x, df = input$df_t), + "chisq" = function(x) dchisq(x, df = input$df_chisq)), + color=col1, size = 1.4) + + ggtitle(switch(input$scoreType, + "z" = "표준정규분포 확률 밀도 함수", + "t" = "t 분포 확률 밀도 함수", + "chisq" = "카이제곱 분포 확률 밀도 함수")) + + labs(x="", y="") + + theme_bw() + + scale_x_continuous(limits = c(ifelse(input$scoreType == "chisq", 0, -5), + ifelse(input$scoreType == "chisq", max(20, score() + 2), 5)), + expand = c(0, 0)) + + scale_y_continuous(limits = c(0, 0.5), expand = c(0, 0)) + + geom_vline(xintercept=score(), lty=2, size=1.2, color=col3) + + annotate("text", x=ifelse(score()<0 | input$scoreType == "chisq", score() + 0.4, score() - 0.4), + y=funcShaded(score()) + 0.05, label=toupper(input$scoreType), + parse=TRUE, size=5, color=col3) + + theme(axis.line = element_line(size=1, colour = "black"), + panel.grid.major = element_blank(), + panel.grid.minor = element_blank(), + panel.border = element_blank(), + panel.background = element_blank(), + plot.title = element_text(size = 20, family = "Tahoma", face = "bold"), + text=element_text(family="Tahoma"), + axis.text.x=element_text(colour="black", size = 11), + axis.text.y=element_text(colour="black", size = 11)) + + p2 <- ggplot(data.frame(x = c(ifelse(input$scoreType == "chisq", 0, -20), 20)), aes(x = x)) + + annotate("segment", x=score(), xend=score(), + y=0, yend=switch(input$scoreType, + "z" = pnorm(score()), + "t" = pt(score(), df = input$df_t), + "chisq" = pchisq(score(), df = input$df_chisq)), + color=col3, lty=2, size=1.4) + + annotate("segment", x=ifelse(input$scoreType == "chisq", 0, -5), xend=score(), + y=switch(input$scoreType, + "z" = pnorm(score()), + "t" = pt(score(), df = input$df_t), + "chisq" = pchisq(score(), df = input$df_chisq)), + yend=switch(input$scoreType, + "z" = pnorm(score()), + "t" = pt(score(), df = input$df_t), + "chisq" = pchisq(score(), df = input$df_chisq)), + color=col2, lty=2, size=1.4) + + stat_function(fun = switch(input$scoreType, + "z" = pnorm, + "t" = function(x) pt(x, df = input$df_t), + "chisq" = function(x) pchisq(x, df = input$df_chisq)), + color=col1, size = 1.4) + + ggtitle(switch(input$scoreType, + "z" = "표준정규분포 누적 분포 함수", + "t" = "t 분포 누적 분포 함수", + "chisq" = "카이제곱 분포 누적 분포 함수")) + + labs(x="", y="") + + theme_bw() + + scale_x_continuous(limits = c(ifelse(input$scoreType == "chisq", 0, -5), + ifelse(input$scoreType == "chisq", max(20, score() + 2), 5)), + expand = c(0, 0)) + + scale_y_continuous(limits = c(0, 1.14), breaks=c(0, 0.2, 0.4, 0.6, 0.8, 1), expand = c(0, 0)) + + annotate("text", x=score() + 0.4, + y=switch(input$scoreType, + "z" = pnorm(score()), + "t" = pt(score(), df = input$df_t), + "chisq" = pchisq(score(), df = input$df_chisq)) - 0.1, + label=toupper(input$scoreType), + parse=TRUE, size=5, color=col3) + + annotate("text", x=ifelse(input$scoreType == "chisq", 1, -3), + y=(switch(input$scoreType, + "z" = pnorm(score()), + "t" = pt(score(), df = input$df_t), + "chisq" = pchisq(score(), df = input$df_chisq)) + 0.05), + label=("'P(X' <= x ~ ')'"), + parse=TRUE, size=5, color=col2) + + theme(axis.line = element_line(size=1, colour = "black"), + panel.grid.major = element_blank(), + panel.grid.minor = element_blank(), + panel.border = element_blank(), + panel.background = element_blank(), + plot.title = element_text(size = 20, family = "Tahoma", face = "bold"), + text=element_text(family="Tahoma"), + axis.text.x=element_text(colour="black", size = 11), + axis.text.y=element_text(colour="black", size = 11)) + + grid.arrange(p1, p2, nrow=1) + }) + + # 신뢰수준 계산 + output$conf_level <- renderText({ + conf_level <- round((1 - 2 * (1 - switch(input$scoreType, + "z" = pnorm(abs(score())), + "t" = pt(abs(score()), df = input$df_t), + "chisq" = pchisq(score(), df = input$df_chisq)))) * 100, 2) + paste0(conf_level, "%") + }) + + # 양측 검정에서의 점수 계산 + output$score <- renderText({ + conf_level <- (1 - 2 * (1 - switch(input$scoreType, + "z" = pnorm(abs(score())), + "t" = pt(abs(score()), df = input$df_t), + "chisq" = pchisq(score(), df = input$df_chisq)))) + score_val <- round(switch(input$scoreType, + "z" = qnorm(1 - (1 - conf_level) / 2), + "t" = qt(1 - (1 - conf_level) / 2, df = input$df_t), + "chisq" = qchisq(conf_level, df = input$df_chisq)), 2) + paste0(score_val) + }) +} + +# Run the application +shinyApp(ui = ui, server = server) + +``` + +::: + +# 코딩 + +```{webr-r} +# Z-score에 대한 확률 계산 함수 +calc_prob_z <- function(z) { + prob <- pnorm(z) + cat("P(X ≤", z, ") =", prob, "\n") + + conf_level <- round((1 - 2 * (1 - pnorm(abs(z)))) * 100, 2) + cat("신뢰수준 (양측):", conf_level, "%\n") + + score_val <- round(qnorm(1 - (1 - conf_level/100) / 2), 2) + cat("대응하는 점수:", score_val, "\n") +} + +# t-score에 대한 확률 계산 함수 +calc_prob_t <- function(t, df) { + prob <- pt(t, df) + cat("P(X ≤", t, ") =", prob, "\n") + + conf_level <- round((1 - 2 * (1 - pt(abs(t), df))) * 100, 2) + cat("신뢰수준 (양측):", conf_level, "%\n") + + score_val <- round(qt(1 - (1 - conf_level/100) / 2, df), 2) + cat("대응하는 점수:", score_val, "\n") +} + +# 카이제곱-점수에 대한 확률 계산 함수 +calc_prob_chisq <- function(chisq, df) { + prob <- pchisq(chisq, df) + cat("P(X ≤", chisq, ") =", prob, "\n") + + conf_level <- round(pchisq(chisq, df) * 100, 2) + cat("신뢰수준 (양측):", conf_level, "%\n") + + score_val <- round(qchisq(conf_level/100, df), 2) + cat("대응하는 점수:", score_val, "\n") +} + +# Z-score 예시 +calc_prob_z(1.96) +# P(X ≤ 1.96 ) = 0.9750021 +# 신뢰수준 (양측): 95 % +# 대응하는 점수: 1.96 + +# t-score 예시 +calc_prob_t(2.262, 10) +# P(X ≤ 2.262 ) = 0.9750022 +# 신뢰수준 (양측): 95 % +# 대응하는 점수: 2.23 + +# 카이제곱-점수 예시 +calc_prob_chisq(3.84, 1) +# P(X ≤ 3.84 ) = 0.9500042 +# 신뢰수준 (양측): 95 % +# 대응하는 점수: 3.84 +``` + + diff --git a/04_testing/x_score/shiny/app.R b/04_testing/x_score/shiny/app.R new file mode 100644 index 0000000..734ddc0 --- /dev/null +++ b/04_testing/x_score/shiny/app.R @@ -0,0 +1,205 @@ +library(shiny) +library(showtext) +showtext_auto() + +# Define UI for application that draws a histogram +ui <- fluidPage( + # Application title + titlePanel("통계 검정에 중요한 점수의 변화에 따른 확률 계산"), + + tags$div(HTML(" + ")), + + # Sidebar with inputs and options + sidebarLayout( + sidebarPanel( + radioButtons("scoreType", "점수 선택:", + c("Z-score" = "z", + "T-score" = "t", + "Chi-square 점수" = "chisq")), + conditionalPanel( + condition = "input.scoreType == 'z'", + sliderInput("z", "Z-score", min = -5, max = 5, step = 0.01, ticks = TRUE, value = 1.96) + ), + conditionalPanel( + condition = "input.scoreType == 't'", + sliderInput("t", "T-score", min = -5, max = 5, step = 0.01, ticks = TRUE, value = 1.96), + numericInput("df_t", "자유도", value = 10, min = 1, step = 1) + ), + conditionalPanel( + condition = "input.scoreType == 'chisq'", + sliderInput("chisq", "Chi-square 점수", min = 0, max = 20, step = 0.01, ticks = TRUE, value = 3.84), + numericInput("df_chisq", "자유도", value = 1, min = 1, step = 1) + ), + withMathJax(), + p("$P(X \\leq x) =$"), + textOutput("prob"), + hr(), + p("신뢰수준 (양측):"), + textOutput("conf_level"), + p("대응하는 점수:"), + textOutput("score") + ), + + # Show a plot of the generated distribution + mainPanel( + plotOutput("plot") + ) + ) +) + +# Define server logic required to draw a histogram +server <- function(input, output) { + + score <- reactive({ + switch(input$scoreType, + "z" = input$z, + "t" = input$t, + "chisq" = input$chisq) + }) + + output$prob <- renderPrint({ + switch(input$scoreType, + "z" = pnorm(score()), + "t" = pt(score(), df = input$df_t), + "chisq" = pchisq(score(), df = input$df_chisq)) + }) + + library(ggplot2) + library(gridExtra) + + # manually save colors + col1 <- "#3B429F" + col2 <- "#76BED0" + col3 <- "#F55D3E" + + output$plot <- renderPlot({ + # useful "shader" function taken from: https://t-redactyl.io/blog/2016/03/creating-plots-in-r-using-ggplot2-part-9-function-plots.html + funcShaded <- function(x) { + y <- switch(input$scoreType, + "z" = dnorm(x), + "t" = dt(x, df = input$df_t), + "chisq" = dchisq(x, df = input$df_chisq)) + y[x > score()] <- NA + return(y) + } + + p1 <- ggplot(data.frame(x = c(ifelse(input$scoreType == "chisq", 0, -20), 20)), aes(x = x)) + + stat_function(fun=funcShaded, geom="area", fill=col2, alpha=0.6) + + stat_function(fun = switch(input$scoreType, + "z" = dnorm, + "t" = function(x) dt(x, df = input$df_t), + "chisq" = function(x) dchisq(x, df = input$df_chisq)), + color=col1, size = 1.4) + + ggtitle(switch(input$scoreType, + "z" = "표준정규분포 확률 밀도 함수", + "t" = "t 분포 확률 밀도 함수", + "chisq" = "카이제곱 분포 확률 밀도 함수")) + + labs(x="", y="") + + theme_bw() + + scale_x_continuous(limits = c(ifelse(input$scoreType == "chisq", 0, -5), + ifelse(input$scoreType == "chisq", max(20, score() + 2), 5)), + expand = c(0, 0)) + + scale_y_continuous(limits = c(0, 0.5), expand = c(0, 0)) + + geom_vline(xintercept=score(), lty=2, size=1.2, color=col3) + + annotate("text", x=ifelse(score()<0 | input$scoreType == "chisq", score() + 0.4, score() - 0.4), + y=funcShaded(score()) + 0.05, label=toupper(input$scoreType), + parse=TRUE, size=5, color=col3) + + theme(axis.line = element_line(size=1, colour = "black"), + panel.grid.major = element_blank(), + panel.grid.minor = element_blank(), + panel.border = element_blank(), + panel.background = element_blank(), + plot.title = element_text(size = 20, family = "Tahoma", face = "bold"), + text=element_text(family="Tahoma"), + axis.text.x=element_text(colour="black", size = 11), + axis.text.y=element_text(colour="black", size = 11)) + + p2 <- ggplot(data.frame(x = c(ifelse(input$scoreType == "chisq", 0, -20), 20)), aes(x = x)) + + annotate("segment", x=score(), xend=score(), + y=0, yend=switch(input$scoreType, + "z" = pnorm(score()), + "t" = pt(score(), df = input$df_t), + "chisq" = pchisq(score(), df = input$df_chisq)), + color=col3, lty=2, size=1.4) + + annotate("segment", x=ifelse(input$scoreType == "chisq", 0, -5), xend=score(), + y=switch(input$scoreType, + "z" = pnorm(score()), + "t" = pt(score(), df = input$df_t), + "chisq" = pchisq(score(), df = input$df_chisq)), + yend=switch(input$scoreType, + "z" = pnorm(score()), + "t" = pt(score(), df = input$df_t), + "chisq" = pchisq(score(), df = input$df_chisq)), + color=col2, lty=2, size=1.4) + + stat_function(fun = switch(input$scoreType, + "z" = pnorm, + "t" = function(x) pt(x, df = input$df_t), + "chisq" = function(x) pchisq(x, df = input$df_chisq)), + color=col1, size = 1.4) + + ggtitle(switch(input$scoreType, + "z" = "표준정규분포 누적 분포 함수", + "t" = "t 분포 누적 분포 함수", + "chisq" = "카이제곱 분포 누적 분포 함수")) + + labs(x="", y="") + + theme_bw() + + scale_x_continuous(limits = c(ifelse(input$scoreType == "chisq", 0, -5), + ifelse(input$scoreType == "chisq", max(20, score() + 2), 5)), + expand = c(0, 0)) + + scale_y_continuous(limits = c(0, 1.14), breaks=c(0, 0.2, 0.4, 0.6, 0.8, 1), expand = c(0, 0)) + + annotate("text", x=score() + 0.4, + y=switch(input$scoreType, + "z" = pnorm(score()), + "t" = pt(score(), df = input$df_t), + "chisq" = pchisq(score(), df = input$df_chisq)) - 0.1, + label=toupper(input$scoreType), + parse=TRUE, size=5, color=col3) + + annotate("text", x=ifelse(input$scoreType == "chisq", 1, -3), + y=(switch(input$scoreType, + "z" = pnorm(score()), + "t" = pt(score(), df = input$df_t), + "chisq" = pchisq(score(), df = input$df_chisq)) + 0.05), + label=("'P(X' <= x ~ ')'"), + parse=TRUE, size=5, color=col2) + + theme(axis.line = element_line(size=1, colour = "black"), + panel.grid.major = element_blank(), + panel.grid.minor = element_blank(), + panel.border = element_blank(), + panel.background = element_blank(), + plot.title = element_text(size = 20, family = "Tahoma", face = "bold"), + text=element_text(family="Tahoma"), + axis.text.x=element_text(colour="black", size = 11), + axis.text.y=element_text(colour="black", size = 11)) + + grid.arrange(p1, p2, nrow=1) + }) + + # 신뢰수준 계산 + output$conf_level <- renderText({ + conf_level <- round((1 - 2 * (1 - switch(input$scoreType, + "z" = pnorm(abs(score())), + "t" = pt(abs(score()), df = input$df_t), + "chisq" = pchisq(score(), df = input$df_chisq)))) * 100, 2) + paste0(conf_level, "%") + }) + + # 양측 검정에서의 점수 계산 + output$score <- renderText({ + conf_level <- (1 - 2 * (1 - switch(input$scoreType, + "z" = pnorm(abs(score())), + "t" = pt(abs(score()), df = input$df_t), + "chisq" = pchisq(score(), df = input$df_chisq)))) + score_val <- round(switch(input$scoreType, + "z" = qnorm(1 - (1 - conf_level) / 2), + "t" = qt(1 - (1 - conf_level) / 2, df = input$df_t), + "chisq" = qchisq(conf_level, df = input$df_chisq)), 2) + paste0(score_val) + }) +} + +# Run the application +shinyApp(ui = ui, server = server) diff --git a/04_testing/x_score/thumbnail.png b/04_testing/x_score/thumbnail.png new file mode 100644 index 0000000..947eafb Binary files /dev/null and b/04_testing/x_score/thumbnail.png differ diff --git a/05_infer/ci/shiny/app.R b/05_infer/ci/shiny/app.R index 54789d5..1559d93 100644 --- a/05_infer/ci/shiny/app.R +++ b/05_infer/ci/shiny/app.R @@ -1,6 +1,8 @@ library(shiny) library(ggplot2) library(tidyverse) +library(showtext) +showtext_auto() ui <- fluidPage( title = "신뢰구간 모의실험", diff --git a/app.R b/app.R new file mode 100644 index 0000000..0303f46 --- /dev/null +++ b/app.R @@ -0,0 +1,189 @@ +library(shiny) + +library(ggplot2) +library(grid) +library(gridExtra) + +# Define UI for application that draws a histogram +ui <- fluidPage( + + # Application title + titlePanel("Bayes' Rule Visualization"), + + + tags$div(HTML(" + ")), + + verticalLayout( + withMathJax(), + p(""), + p("Suppose that for two events, $\\color{red}{A}$ and $\\color{blue}{B}$, we know $P(\\color{red}{A})$, $P(\\color{blue}{B})$, and $P(\\color{blue}{B}|\\color{red}{A})$... can we find $P(\\color{red}{A}|\\color{blue}{B})$?"), + p("Yes! To see why, remember that we can write $P(\\color{red}{A} \\& \\color{blue}{B})$ in two ways: $ P(\\color{blue}{B}|\\color{red}{A}) \\times P(\\color{red}{A}) = P(\\color{red}{A} \\& \\color{blue}{B}) = P(\\color{red}{A}|\\color{blue}{B}) \\times P(\\color{blue}{B}) $"), + p("This means that the yellow area on both squares equals $P(\\color{red}{A} \\& \\color{blue}{B})$. So if we have $P(\\color{red}{A})$, $P(\\color{blue}{B})$, and $P(\\color{blue}{B}|\\color{red}{A})$, then we can calculate: $ P(\\color{red}{A}|\\color{blue}{B}) = \\frac{ P(\\color{blue}{B}|\\color{red}{A}) \\times P(\\color{red}{A}) }{P(\\color{blue}{B})} $"), + p(""), + p(""), + p(""), + + # Sidebar with a slider input for number of bins + sidebarLayout( + sidebarPanel( + sliderInput("p.a", "Probability of A:", value=0.5, step=0.01, min = 0, max = 1), + sliderInput("p.b", "Probability of B:", value=0.8, step=0.01, min = 0, max = 1), + sliderInput("p.b.given.a", "Probability of B given A:", value=0.6, step=0.01, min = 0, max = 1) + ), + + # Show a plot of the generated distribution + mainPanel( + + # Output: Tabset w/ plot, summary, and table ---- + tabsetPanel(type = "tabs", + tabPanel("Visualization", br(), plotOutput("bayes_viz")), + tabPanel("Raw numbers", br(), br(), br(), tableOutput("table")) + ), + + ) + ) + ) +) + +# Define server logic +server <- function(input, output) { + + + output$table <- renderTable({ + p.a <- input$p.a + p.b <- input$p.b + p.b.given.a <- input$p.b.given.a + p.a.and.b <- p.b.given.a * p.a + p.a.given.b <- (p.b.given.a * p.a)/p.b + dat <- cbind(p.a, p.b, p.b.given.a, p.a.and.b, p.a.given.b) + datt <- t(dat) + datt <- round(datt, digits=3) + labels <- c("P(A)", "P(B)", "P(B|A)", "P(A&B)", "P(A|B)") + type <- c("Input", "Input", "Input", "Output", "Output") + datt <- cbind(labels, datt, type) + colnames(datt) <- c("Variable", "Value", "Type") + datt + }) + + + output$bayes_viz <- renderPlot({ + + p.a <- input$p.a + p.b <- input$p.b + p.b.given.a <- input$p.b.given.a + # use Bayes' rule to compute P(A|B) + p.a.given.b <- (p.b.given.a * p.a)/p.b + p.a.and.b <- p.b.given.a * p.a + + # create dataset for blank square plot + x <- c(0,1) + y <- c(0,1) + df <- expand.grid(x, y) + names(df) <- c("x", "y") + + + p <- ggplot(df) + + # bottom left + geom_rect(xmin = 0, xmax = p.a, ymin = 0, ymax = 1-p.b.given.a, fill = "#a6cee3") + + # top left + geom_rect(xmin = 0, xmax = p.a, ymin = 1-p.b.given.a, ymax = 1, fill = "#f3f470") + + # RHS (not broken up into 2 blocks, because we don't actually need to know P(B|¬A)) + geom_rect(xmin = p.a, xmax = 1, ymin = 0, ymax = 1, fill = "#1f78b4") + + labs(title="Factoring by P(A)") + + coord_cartesian(clip = "off") + coord_fixed(ratio=1, ylim=c(0, 1.3), xlim=c(-0.3, 1)) + + theme(plot.margin= unit(c(-3,0,0,0), "lines"), + plot.title = element_text(margin=margin(b = -31, unit = "pt"), face="bold", size=18), + panel.background = element_rect(fill = "white", + colour = "white")) + + + p1 = p + annotation_custom(grob = textGrob("P(B | A)"), + xmin = -0.2, xmax = -0.2, ymin = 1-p.b.given.a, ymax = 1) + + annotation_custom(grob = linesGrob(arrow=arrow(type="open", ends="both", length=unit(2,"mm")), + gp=gpar(col="black", lwd=1.5)), + xmin = -0.05, xmax = -0.05, ymin = 1-p.b.given.a, ymax = 1) + + p1 = p1 + annotation_custom(grob = textGrob("P(¬B | A)"), + xmin = -0.2, xmax = -0.2, ymin = 0, ymax = 1-p.b.given.a) + + annotation_custom(grob = linesGrob(arrow=arrow(type="open", ends="both", length=unit(2,"mm")), + gp=gpar(col="black", lwd=1.5)), + xmin = -0.05, xmax = -0.05, ymin = 0, ymax = 1-p.b.given.a) + + + p1 = p1 + annotation_custom(grob = textGrob("P(A)"), + xmin = 0, xmax = p.a, ymin = 1.1, ymax = 1.1) + + annotation_custom(grob = linesGrob(arrow=arrow(type="open", ends="both", length=unit(2,"mm")), + gp=gpar(col="black", lwd=1.5)), + xmin = 0, xmax = p.a, ymin = 1.05, ymax=1.05) + + p1 = p1 + annotation_custom(grob = textGrob("P(¬A)"), + xmin = p.a, xmax = 1, ymin = 1.1, ymax = 1.1) + + annotation_custom(grob = linesGrob(arrow=arrow(type="open", ends="both", length=unit(2,"mm")), + gp=gpar(col="black", lwd=1.5)), + xmin = p.a, xmax = 1, ymin = 1.05, ymax=1.05) + + + p1 = p1 + annotation_custom(grob = textGrob("P(A&B)"), + xmin = 0, xmax = p.a, ymin = 1-p.b.given.a, ymax = 1) + + p2 <- ggplot(df) + + # bottom left + geom_rect(xmin = 0, xmax = p.b, ymin = 0, ymax = 1-p.a.given.b, fill = "#b7aef0") + + # top left + geom_rect(xmin = 0, xmax = p.b, ymin = 1-p.a.given.b, ymax = 1, fill = "#f3f470") + + # RHS (not broken up into 2 blocks, because we don't actually need to know P(B|¬A)) + geom_rect(xmin = p.b, xmax = 1, ymin = 0, ymax = 1, fill = "#7b72b8") + + labs(title="Factoring by P(B)") + + coord_cartesian(clip = "off") + coord_fixed(ratio=1, ylim=c(0, 1.3), xlim=c(-0.3, 1)) + + theme(plot.margin= unit(c(-3,0,0,0), "lines"), + plot.title = element_text(margin=margin(b = -31, unit = "pt"), face="bold", size=18), + panel.background = element_rect(fill = "white", + colour = "white")) + + + p2 = p2 + annotation_custom(grob = rectGrob(gp=gpar(fill="red", alpha=0.5)), + xmin = -0.33, xmax = -0.08, ymin = 1-p.a.given.b/2 -0.04, ymax = 1-p.a.given.b/2 +0.04) + + annotation_custom(grob=textGrob("P(A | B)"), + xmin = -0.3, xmax = -0.1, ymin = 1-p.a.given.b, ymax = 1) + + annotation_custom(grob = linesGrob(arrow=arrow(type="open", ends="both", length=unit(2,"mm")), + gp=gpar(col="black", lwd=1.5)), + xmin = -0.05, xmax = -0.05, ymin = 1-p.a.given.b, ymax = 1) + # add box to highlight the unknown quantity P(A|B) + + p2 = p2 + annotation_custom(grob = textGrob("P(¬A | B)"), + xmin = -0.2, xmax = -0.2, ymin = 0, ymax = 1-p.a.given.b) + + annotation_custom(grob = linesGrob(arrow=arrow(type="open", ends="both", length=unit(2,"mm")), + gp=gpar(col="black", lwd=1.5)), + xmin = -0.05, xmax = -0.05, ymin = 0, ymax = 1-p.a.given.b) + + p2 = p2 + annotation_custom(grob = textGrob("P(B)"), + xmin = 0, xmax = p.b, ymin = 1.1, ymax = 1.1) + + annotation_custom(grob = linesGrob(arrow=arrow(type="open", ends="both", length=unit(2,"mm")), + gp=gpar(col="black", lwd=1.5)), + xmin = 0, xmax = p.b, ymin = 1.05, ymax=1.05) + + p2 = p2 + annotation_custom(grob = textGrob("P(¬B)"), + xmin = p.b, xmax = 1, ymin = 1.1, ymax = 1.1) + + annotation_custom(grob = linesGrob(arrow=arrow(type="open", ends="both", length=unit(2,"mm")), + gp=gpar(col="black", lwd=1.5)), + xmin = p.b, xmax = 1, ymin = 1.05, ymax=1.05) + + + p2 = p2 + annotation_custom(grob = textGrob("P(A&B)"), + xmin = 0, xmax = p.b, ymin = 1-p.a.given.b, ymax = 1) + + + bayes_viz = grid.arrange(p1, p2, ncol=2) + + bayes_viz + + }) +} + +# Run the application +shinyApp(ui = ui, server = server) diff --git a/docs/04_testing.html b/docs/04_testing.html index f7a54bc..6ab0a14 100644 --- a/docs/04_testing.html +++ b/docs/04_testing.html @@ -252,7 +252,7 @@