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


```

**Hypothesis**

```{r echo=FALSE, results='asis'}
if (params$bin$interval == 1) {
# Null hypothesis
cat(sprintf("\\(\\mathcal{H}_0: p_0 = %.2f\\) vs ", params$bin$location))

# Alternative hypothesis
alt_hypothesis <- switch(
  params$bin$alternative,
  "greater"  = sprintf("\\(\\mathcal{H}_1: p_1 \\in \\{ %.2f, 1 \\}\\)\n", params$bin$location),
  "less"  = sprintf("\\(\\mathcal{H}_1: p_1 \\in \\{ 0, %.2f \\}\\)\n", params$bin$location),
  "two.sided" = "\\(\\mathcal{H}_1: p_1 \\ne p_0\\)\n"
)

cat(alt_hypothesis)



}else if (params$bin$interval == 2) {
  # Extract epsilon values
  hyp <- params$bin$alternative
  ROPE <- params$bin$ROPE
  location <-params$bin$location

eps <- switch(hyp,
  "greater"  = list(eps_neg = 0,       eps_pos = location+ROPE),
  "less"  = list(eps_neg =location+ ROPE,       eps_pos = 0),
  "two.sided" = list(eps_neg = location+ROPE[1],    eps_pos = location+ROPE[2]),
  list(eps_neg = NA,             eps_pos = NA)  # default/fallback
)

cat(sprintf("\\(\\mathcal{H}_0: \\rho \\in \\{ %.2f, %.2f \\} \\) vs ", eps[1], eps[2]))

if (hyp == "greater") {
  cat(sprintf("\\(\\mathcal{H}_1: \\rho \\in (%.2f, 1) \\)\n", eps[[2]]))
} else if (hyp == "less") {
  cat(sprintf("\\(\\mathcal{H}_1: \\rho \\in (0, %.2f) \\)\n", eps[[1]]))
} else if (hyp == "two.sided") {
  cat(sprintf("\\(\\mathcal{H}_1: \\rho \\in (0, %.2f) \\cup (%.2f, 1) \\)\n", eps[[1]], eps[[2]]))
} 

  
  
}
```



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

model <- params$bin$prior_analysis

if (model == "beta") {
  cat(sprintf("Beta ($\\alpha = %s$, $\\beta = %s$)\n\n", params$bin$alpha, params$bin$beta))
} else if (model == "Moment") {
  cat(sprintf("NLP(location = %s, scale = %s)\n\n", params$bin$location, params$bin$scale))
}


```

```{r results='asis', echo=FALSE}
if (params$bin$de_an_prior == 0) {
  cat("**Design prior**  \n")
  
  if (params$bin$prior_design == "Point") {
    cat(sprintf("Point(location = %s)  \n", params$bin$location_d))
  } else {
    
    if (model == "beta") {
  cat(sprintf("Beta ($\\alpha = %s$, $\\beta = %s$)\n\n", params$bin$alpha_d, params$bin$beta_d))
} else if (model == "Moment") {
  cat(sprintf("NLP(location = %s, scale = %s)\n\n", params$bin$location_d, params$bin$scale_d))
}
    
    
  }
}
```




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

```
