---
title: "BayesPower: Output"
output:
  html_document: default
fontsize: 12pt
params:
  rr: NA
  dat: NA
  pc_r: NA
  rela_r: NA
geometry: margin=2cm
classoption: a4paper
---

A Bayes Factor design analysis for testing correlation 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_cor_code(params$rr))
cat("\n```")


```

**Hypothesis**

```{r echo=FALSE, results='asis'}
if (params$rr$interval == 1) {
  cat("\\(\\mathcal{H}_0: \\rho = 0\\) vs ")

  cat(switch(
    params$rr$alternative,
    "greater" = "\\(\\mathcal{H}_1: \\rho > 0\\)\n",
    "less" = "\\(\\mathcal{H}_1: \\rho < 0\\)\n",
    "\\(\\mathcal{H}_1: \\rho \\neq 0\\)\n"
  ))
} else if (params$rr$interval == 2) {
  # Extract epsilon values
  hyp <- params$rr$alternative
e <- params$rr$ROPE

eps <- switch(hyp,
  "greater"  = list(eps_neg = 0,       eps_pos = e),
  "less"  = list(eps_neg = e,       eps_pos = 0),
  "two.sided" = list(eps_neg = e[1],    eps_pos = 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 == "greater") {
  cat(sprintf("\\(\\mathcal{H}_1: \\rho \\in (%.2f, 1) \\)\n", eps[[2]]))
} else if (hyp == "less") {
  cat(sprintf("\\(\\mathcal{H}_1: \\rho \\in (-1, %.2f) \\)\n", eps[[1]]))
} else if (hyp == "two.sided") {
  cat(sprintf("\\(\\mathcal{H}_1: \\rho \\in (-1, %.2f) \\cup (%.2f, 1) \\)\n", eps[[1]], eps[[2]]))
} else {
  cat("\\(\\mathcal{H}_1:\\ \\text{Undefined Hypothesis}\\)\n")
}

  
  
}

```

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

model <- params$rr$prior_analysis

if (model == "d_beta") {
  cat(sprintf("Default Stretched Beta (k = %s)\n", params$rr$k))

} else if (model == "beta") {
  cat(sprintf("Beta (alpha = %s, beta = %s)\n", params$rr$k, params$rr$beta))

} else if (model == "nlp") {
  cat(sprintf("NLP(location = %s, scale = %s)\n", params$rr$location, params$rr$scale))

} else {
  cat(sprintf("%s(location = %s, scale = %s)\n", 
              model, params$rr$location, params$rr$scale))
}

```


```{r results='asis', echo=FALSE}
if (params$rr$de_an_prior == 0) {
  cat("**Design prior**  \n")
  
  if (params$rr$prior_design == "Point") {
    cat(sprintf("Point(location = %s)  \n", params$rr$location_d))
  } else {
    model <- params$rr$prior_design

if (model == "d_beta") {
  cat(sprintf("Default Stretched Beta (k = %s)\n", params$rr$k_d))

} else if (model == "beta") {
  cat(sprintf("Beta (alpha = %s, beta = %s)\n", params$rr$alpha, params$rr$beta))

} else if (model == "nlp") {
  cat(sprintf("NLP(location = %s, scale = %s)\n", params$rr$location, params$rr$scale_d))

} else {
  cat(sprintf("%s(location = %s, scale = %s)\n", 
              model, params$rr$location, params$rr$scale))
}
  }
}
```



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

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

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

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

if (params$rr$pc){
print(params$pc_r)
  }

```


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

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

```
