---
title: "BayesPower: Output"
output:
  html_document: default
fontsize: 12pt
params:
  bin: NA
  dat: NA
  pc_bin: NA
  rela_bin: 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_bin_code(params$bin))
cat("\n```")


```


```{r echo=FALSE, results='asis'}
theta <- "\\theta"

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

h0 <- params$bin$location

if (is.null(params$bin$ROPE) || params$bin$interval == 1) {

  cat(sprintf("\\(\\mathcal{H}_0: %s = %.2f\\) vs ", theta, h0))

  cat(switch(
    params$bin$alternative,
    "two.sided" = sprintf("\\(\\mathcal{H}_1: %s \\neq %.2f\\)  \n", theta, h0),
    "greater"   = sprintf("\\(\\mathcal{H}_1: %s > %.2f\\)  \n", theta, h0),
    "less"      = sprintf("\\(\\mathcal{H}_1: %s < %.2f\\)  \n", theta, h0)
  ))

} else {

  lower <- h0 + min(params$bin$ROPE)
  upper <- h0 + max(params$bin$ROPE)

  if (params$bin$alternative == "two.sided") {

    cat(sprintf(
      "\\(\\mathcal{H}_0: %.2f \\leq %s \\leq %.2f\\) vs \\(\\mathcal{H}_1: %s < %.2f \\cup %s > %.2f\\)  \n",
      lower, theta, upper, theta, lower, theta, upper
    ))

  } else if (params$bin$alternative == "greater") {

    cat(sprintf(
      "\\(\\mathcal{H}_0: %.2f \\leq %s \\leq %.2f\\) vs \\(\\mathcal{H}_1: %s > %.2f\\)  \n",
      h0, theta, upper, theta, upper
    ))

  } else if (params$bin$alternative == "less") {

    cat(sprintf(
      "\\(\\mathcal{H}_0: %.2f \\leq %s \\leq %.2f\\) vs \\(\\mathcal{H}_1: %s < %.2f\\)  \n",
      lower, theta, h0, theta, lower
    ))
  }
}

```



```{r results='asis', echo=FALSE}
fmt <- function(z) {
  if (is.null(z) || length(z) == 0 || any(is.na(z))) return("NA")
  format(round(as.numeric(z), 3), nsmall = 0, trim = TRUE)
}

print_bin_prior_rmd <- function(prior, alpha = NULL, beta = NULL,
                                location = NULL, scale = NULL) {

  if (is.null(prior) || prior == "Point") {
    return(sprintf("Point prior (location = %s)", fmt(location)))
  }

  if (prior == "beta") {
    return(sprintf(
      "Stretched Beta (alpha = %s, beta = %s)",
      fmt(alpha), fmt(beta)
    ))
  }

  if (prior == "Moment") {
    return(sprintf(
      "Normal-moment (location = %s, scale = %s)",
      fmt(location), fmt(scale)
    ))
  }

  as.character(prior)
}

analysis_prior_text <- print_bin_prior_rmd(
  prior = params$bin$prior_analysis,
  alpha = params$bin$alpha,
  beta = params$bin$beta,
  location = params$bin$location,
  scale = params$bin$scale
)

cat("**Analysis/design prior**  \n")

cat("Analysis/design prior under \\(\\mathcal{H}_0\\):  \n")
if (is.null(params$bin$ROPE) || params$bin$interval == 1) {
  cat(sprintf("Point prior (location = %s)", fmt(params$bin$location)))
} else {
  cat(analysis_prior_text)
}
cat("  \n\n")

cat("Analysis prior under \\(\\mathcal{H}_1\\):  \n")
cat(analysis_prior_text, "  \n\n")

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

if (params$bin$de_an_prior == 1) {
  cat(analysis_prior_text)
} else {
  cat(print_bin_prior_rmd(
    prior = params$bin$prior_design,
    alpha = params$bin$alpha_d,
    beta = params$bin$beta_d,
    location = params$bin$location_d,
    scale = params$bin$scale_d
  ))
}

cat("  \n")
```




**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$bin$D, '\\, | \\, \\mathcal{H}_1) & ', round(params$dat[1,1], 3), ' \\\\
  \\text{p(BF}_{01} > ', params$bin$D, '\\, | \\, \\mathcal{H}_0) & ', round(params$dat[1,3], 3), ' \\\\
  \\textbf{Probability of Misleading Evidence} & \\\\
  \\hline
  \\text{p(BF}_{01} > ', params$bin$D, '\\, | \\, \\mathcal{H}_1) & ', round(params$dat[1,2], 3), ' \\\\
  \\text{p(BF}_{10} > ', params$bin$D, '\\, | \\, \\mathcal{H}_0) & ', round(params$dat[1,4], 3), ' \\\\
  \\textbf{Required Sample Size} & \\\\
  \\hline
  \\text{N} & ', params$dat[1,5], ' \\\\
  \\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, ]  # 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$bin$pc) || isTRUE(params$bin$rela)) {
  cat("\\newpage\n")
}
```


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

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

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

if (params$bin$pc){
print(params$pc_bin)
  }

```


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

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

```
