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


```


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

fmt_point <- function(z) {
  if (is.null(z) || length(z) == 0 || any(is.na(z))) return("NA")
  format(as.numeric(z), trim = TRUE, scientific = FALSE)
}

fmt_interval <- function(z) {
  if (is.null(z) || length(z) == 0 || any(is.na(z))) return("NA")
  sprintf("%.2f", as.numeric(z))
}

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

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

  h0 <- params$rr$h0
  h0_txt <- fmt_point(h0)

  cat(sprintf("\\(\\mathcal{H}_0: %s = %s\\) vs ", rho, h0_txt))

  cat(switch(
    params$rr$alternative,
    "two.sided" = sprintf("\\(\\mathcal{H}_1: %s \\neq %s\\)  \n", rho, h0_txt),
    "greater"   = sprintf("\\(\\mathcal{H}_1: %s > %s\\)  \n", rho, h0_txt),
    "less"      = sprintf("\\(\\mathcal{H}_1: %s < %s\\)  \n", rho, h0_txt)
  ))

} else {

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

  lower_txt <- fmt_interval(lower)
  upper_txt <- fmt_interval(upper)
  h0_txt <- fmt_interval(h0)

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

    cat(sprintf(
      "\\(\\mathcal{H}_0: %s \\leq %s \\leq %s\\) vs \\(\\mathcal{H}_1: %s < %s \\cup %s > %s\\)  \n",
      lower_txt, rho, upper_txt, rho, lower_txt, rho, upper_txt
    ))

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

    cat(sprintf(
      "\\(\\mathcal{H}_0: %s \\leq %s \\leq %s\\) vs \\(\\mathcal{H}_1: %s > %s\\)  \n",
      h0_txt, rho, upper_txt, rho, upper_txt
    ))

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

    cat(sprintf(
      "\\(\\mathcal{H}_0: %s \\leq %s \\leq %s\\) vs \\(\\mathcal{H}_1: %s < %s\\)  \n",
      lower_txt, rho, h0_txt, rho, lower_txt
    ))
  }
}
```

```{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_cor_prior_rmd <- function(prior, k = NULL, alpha = NULL, beta = NULL,
                                location = NULL, scale = NULL) {

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

  if (prior == "d_beta") {
    return(sprintf(
      "Default Stretched Beta (k = %s)",
      fmt(k)
    ))
  }

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

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

  if (prior == "t-distribution") {
    return(sprintf(
      "t-distribution (location = %s, scale = %s)",
      fmt(location), fmt(scale)
    ))
  }

  as.character(prior)
}

analysis_prior_text <- print_cor_prior_rmd(
  prior = params$rr$prior_analysis,
  k = params$rr$k,
  alpha = params$rr$alpha,
  beta = params$rr$beta,
  location = params$rr$h0,
  scale = params$rr$scale
)

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

cat("Analysis/design prior under \\(\\mathcal{H}_0\\):  \n")
cat(analysis_prior_text, "  \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$rr$de_an_prior == 1) {

  cat(analysis_prior_text)

} else {

  design_prior_text <- print_cor_prior_rmd(
    prior = params$rr$prior_design,
    k = params$rr$k_d,
    alpha = params$rr$alpha_d,
    beta = params$rr$beta_d,
    location = params$rr$location_d,
    scale = params$rr$scale_d
  )

  cat(design_prior_text)
}

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

```
