---
title: "BayesPower: Output"
output:
  pdf_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()`.

**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$hypothesis,
  ">"  = sprintf("\\(\\mathcal{H}_1: p_1 \\in \\{ %.2f, 1 \\}\\)\n", params$bin$location),
  "<"  = sprintf("\\(\\mathcal{H}_1: p_1 \\in \\{ 0, %.2f \\}\\)\n", params$bin$location),
  "!=" = "\\(\\mathcal{H}_1: p_1 \\ne p_0\\)\n"
)

cat(alt_hypothesis)



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

eps <- switch(hyp,
  ">"  = list(eps_neg = 0,       eps_pos = location+e),
  "<"  = list(eps_neg =location+ e,       eps_pos = 0),
  "!=" = list(eps_neg = location+e[1],    eps_pos = location+e[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 == ">") {
  cat(sprintf("\\(\\mathcal{H}_1: \\rho \\in (%.2f, 1) \\)\n", eps[[2]]))
} else if (hyp == "<") {
  cat(sprintf("\\(\\mathcal{H}_1: \\rho \\in (0, %.2f) \\)\n", eps[[1]]))
} else if (hyp == "!=") {
  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$model

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$model_d == "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")
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")
params$rela_bin
}

```

