---
title: "BayesPower: Output"
output:
  pdf_document: default
fontsize: 12pt
params:
  p2: NA
  dat: NA
  pc_p2: NA
  rela_p2: NA
geometry: margin=2cm
classoption: a4paper
---

A Bayes Factor design analysis for testing one proportion was conducted on `r Sys.time()`.

**Hypothesis**

```{r echo=FALSE, results='asis'}
# Null hypothesis
cat(sprintf("\\(\\mathcal{H}_0: p_0 = p_1 = p_2,\\quad p_0 \\sim \\mathrm{beta}(\\alpha = %s, \\beta = %s)\\) vs ", params$p2$a0, params$p2$b0))

cat(sprintf("\\(\\mathcal{H}_1: p_1 \\neq p_2\\)"))


```

```{r echo=FALSE, results='asis'}
if (params$p2$de_an_prior == 1) {
  cat("**Analysis and design prior**  \n\n")
} else {
  cat("**Analysis prior**  \n\n")
}


 cat(sprintf("\\(p_1 \\sim \\mathrm{Beta}(\\alpha = %s, \\beta = %s),\\quad p_2 \\sim \\mathrm{Beta}(\\alpha = %s, \\beta = %s)\\)", 
            params$p2$a1, params$p2$b1, params$p2$a2, params$p2$b2))


if (params$p2$de_an_prior != 1) {
  cat("\n\n **Design prior**  \n")
  
switch(params$p2$model1,
  "Point" = {
    cat(sprintf("\\(p_1 \\sim \\mathrm{Point}(\\text{location} = %s),\\quad\\)", params$p2$dp1))
  },
  "same" = {
    cat(sprintf("\\(p_1 \\sim \\mathrm{Beta}(\\alpha = %s, \\beta = %s),\\quad\\)", params$p2$a1, params$p2$b1))
  },
  "beta" = {
    cat(sprintf("\\(p_1 \\sim \\mathrm{Beta}(\\alpha = %s, \\beta = %s),\\quad\\)", params$p2$a1d, params$p2$b1d))
  }
)
  
  switch(params$p2$model2,
  "Point" = {
    cat(sprintf("\\(p_2 \\sim \\mathrm{Point}(\\text{location} = %s)\\)", params$p2$dp2))
  },
  "same" = {
    cat(sprintf("\\(p_2 \\sim \\mathrm{Beta}(\\alpha = %s, \\beta = %s)\\)", params$p2$a2, params$p2$b2))
  },
  "beta" = {
    cat(sprintf("\\(p_2 \\sim \\mathrm{Beta}(\\alpha = %s, \\beta = %s)\\)", params$p2$a2d, params$p2$b2d))
  }
)
  


}

```



**Result**
```{r results='asis', echo=FALSE}
cat("\n Table format \n") 

# Your existing LaTeX table output
table_latex <- paste0(
  '\\begin{array}{l c}
  \\textbf{Probability of Compelling Evidence} & \\\\
  \\hline
  \\text{p(BF}_{10} > ', params$p2$D, '\\, | \\, \\mathcal{H}_1) & ', round(params$dat[[1]][1,1], 3), ' \\\\
  \\text{p(BF}_{01} > ', params$p2$D, '\\, | \\, \\mathcal{H}_0) & ', round(params$dat[[1]][1,3], 3), ' \\\\
  \\textbf{Probability of Misleading Evidence} & \\\\
  \\hline
  \\text{p(BF}_{01} > ', params$p2$D, '\\, | \\, \\mathcal{H}_1) & ', round(params$dat[[1]][1,2], 3), ' \\\\
  \\text{p(BF}_{10} > ', params$p2$D, '\\, | \\, \\mathcal{H}_0) & ', round(params$dat[[1]][1,4], 3), ' \\\\
  \\textbf{Required Sample Size} & \\\\
  \\hline
  \\text{N}_1 & ', params$dat[[1]][1,5], ' \\\\
  \\text{N}_2 & ', params$dat[[1]][1,6], ' \\\\
  \\end{array}'
)

cat('$$', table_latex, '$$\n\n')

cat("\n CSV format \n")

# Prepare a CSV output for dat (excluding params$rr$D)
dat_csv <- params$dat[[1]][1, ]  # assuming dat is a matrix or data frame row

# Remove the D column if you want (assuming D is params$rr$D, not in dat)
# Just print the dat row as CSV
cat("```\n")  # start of code block in markdown
write.csv(as.data.frame(t(dat_csv)), row.names = TRUE, quote = FALSE)
cat("```\n")  # end of code block in markdown
```



```{r results='asis', echo=FALSE}
if (isTRUE(params$p2$pc) || isTRUE(params$p2$rela)) {
  cat("\\newpage\n")
}
```



```{r results='asis', echo=FALSE,fig.width= 12, fig.height= 8}

if (params$p2$pc){
    cat("**Power Curve **\n\n")

params$pc_p2
}

```


```{r results='asis', echo=FALSE,  fig.width= 12, fig.height= 8}
if (params$p2$rela){
    cat("**Relationship between BF and data **\n\n")

params$rela_p2
}

```


