---
title: "BayesPower: Output"
output:
  html_document: default
fontsize: 12pt
params:
  x: NA
  dat: NA
  pc_t1: NA
  rela_t1: NA
geometry: margin=2cm
classoption: a4paper
---

A Bayes Factor design analysis for one-sample/paired t-test 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_t1_code(params$x))
cat("\n```")


```



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

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

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

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

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

} else {

  lower <- min(params$x$ROPE)
  upper <- max(params$x$ROPE)

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

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

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

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

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

    cat(sprintf(
      "\\(\\mathcal{H}_0: %.2f \\leq %s \\leq 0\\) vs \\(\\mathcal{H}_1: %s < %.2f\\)  \n",
      lower, delta, delta, 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_prior_rmd <- function(prior, location, scale = NULL, dff = NULL) {

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

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

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

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

  as.character(prior)
}

analysis_prior_text <- print_prior_rmd(
  prior = params$x$prior_analysis,
  location = params$x$location,
  scale = params$x$scale,
  dff = params$x$dff
)

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$x$de_an_prior == 1) {
  cat(analysis_prior_text)
} else {
  cat(print_prior_rmd(
    prior = params$x$prior_design,
    location = params$x$location_d,
    scale = params$x$scale_d,
    dff = params$x$dff_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$x$D, '\\, | \\, \\mathcal{H}_1) & ', round(params$dat[1,1], 3), ' \\\\
  \\text{p(BF}_{01} > ', params$x$D, '\\, | \\, \\mathcal{H}_0) & ', round(params$dat[1,3], 3), ' \\\\
  \\textbf{Probability of Misleading Evidence} & \\\\
  \\hline
  \\text{p(BF}_{01} > ', params$x$D, '\\, | \\, \\mathcal{H}_1) & ', round(params$dat[1,2], 3), ' \\\\
  \\text{p(BF}_{10} > ', params$x$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$x$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$x$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$x$pc) || isTRUE(params$x$rela)) {
  cat("\\newpage\n\n")
}
```



```{r results='asis', echo=FALSE,  fig.width= 12, fig.height= 8}
if (params$x$pc && !is.null(params$pc_t1)) {

      cat("**Power Curve**\n\n")

}

```

```{r results='asis', echo=FALSE,  fig.width= 12, fig.height= 8}
if (params$x$pc && !is.null(params$pc_t1)) {

  # Print the plot
  print(params$pc_t1)
}

```

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

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

```
