---
title: "BayesPower: Output"
output:
  html_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()`.The code for reproducing the results:
```{r, echo=FALSE, results='asis'}
# Generate the reproducible code

cat("```r\n")
cat(BayesPower:::show_props_code(params$p2))
cat("\n```")


```


```{r echo=FALSE, results='asis'}
theta0 <- "\\theta_0"
theta1 <- "\\theta_1"
theta2 <- "\\theta_2"

cat("**Hypotheses**  \n")

cat(sprintf(
  "\\(\\mathcal{H}_0: %s = %s = %s\\) vs \\(\\mathcal{H}_1: %s \\neq %s\\)  \n",
  theta1, theta2, theta0, theta1, theta2
))


```

```{r echo=FALSE, results='asis'}
```{r echo=FALSE, results='asis'}
cat("**Analysis/design prior**  \n")

cat("Analysis/design prior under \\(\\mathcal{H}_0\\):  \n")
cat(sprintf(
  "\\(%s \\sim \\mathrm{Beta}(\\alpha = %s, \\beta = %s)\\)  \n\n",
  "\\theta_0", params$p2$a0, params$p2$b0
))

cat("Analysis prior under \\(\\mathcal{H}_1\\):  \n")
cat(sprintf(
  "\\(%s \\sim \\mathrm{Beta}(\\alpha = %s, \\beta = %s),\\; %s \\sim \\mathrm{Beta}(\\alpha = %s, \\beta = %s)\\)  \n\n",
  "\\theta_1", params$p2$a1, params$p2$b1,
  "\\theta_2", params$p2$a2, params$p2$b2
))

cat("Design prior under \\(\\mathcal{H}_1\\):  \n")

print_design_prior_p2 <- function(model, theta, a, b, ad, bd, dp) {
  if (is.null(model) || length(model) == 0 || is.na(model)) model <- "same"

  if (model == "same") {
    sprintf(
      "%s \\sim \\mathrm{Beta}(\\alpha = %s, \\beta = %s)",
      theta, a, b
    )
  } else if (model == "beta") {
    sprintf(
      "%s \\sim \\mathrm{Beta}(\\alpha = %s, \\beta = %s)",
      theta, ad, bd
    )
  } else if (model == "Point") {
    sprintf(
      "%s = %s",
      theta, dp
    )
  } else {
    sprintf("%s: \\text{undefined design prior}", theta)
  }
}

cat(
  "\\(",
  print_design_prior_p2(
    model = params$p2$prior_design_1,
    theta = "\\theta_1",
    a = params$p2$a1,
    b = params$p2$b1,
    ad = params$p2$a1d,
    bd = params$p2$b1d,
    dp = params$p2$dp1
  ),
  ",\\; ",
  print_design_prior_p2(
    model = params$p2$prior_design_2,
    theta = "\\theta_2",
    a = params$p2$a2,
    b = params$p2$b2,
    ad = params$p2$a2d,
    bd = params$p2$b2d,
    dp = params$p2$dp2
  ),
  "\\)  \n",
  sep = ""
)
```



**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")

}

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

if (params$p2$pc){

print(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")

}

```

```{r results='asis', echo=FALSE,  fig.width= 12, fig.height= 8}
if (params$p2$rela){

print(params$rela_p2)
}

```


