---
title: "BayesPower: Output"
output:
  html_document: default
fontsize: 12pt
params:
  t2: NA
  dat: NA
  pc_t2: NA
  rela_t2: NA
geometry: margin=2cm
classoption: a4paper
---

A Bayes Factor design analysis for independent samples 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_t2_code(params$t2))
cat("\n```")


```



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

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

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

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

  cat(switch(
    params$t2$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$t2$ROPE)
  upper <- max(params$t2$ROPE)

  if (params$t2$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$t2$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$t2$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$t2$prior_analysis,
  location = params$t2$location,
  scale = params$t2$scale,
  dff = params$t2$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$t2$de_an_prior == 1) {
  cat(analysis_prior_text)
} else {
  cat(print_prior_rmd(
    prior = params$t2$prior_design,
    location = params$t2$location_d,
    scale = params$t2$scale_d,
    dff = params$t2$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$t2$D, '\\, | \\, \\mathcal{H}_1) & ', round(params$dat[1,1], 3), ' \\\\
  \\text{p(BF}_{01} > ', params$t2$D, '\\, | \\, \\mathcal{H}_0) & ', round(params$dat[1,3], 3), ' \\\\
  \\textbf{Probability of Misleading Evidence} & \\\\
  \\hline
  \\text{p(BF}_{01} > ', params$t2$D, '\\, | \\, \\mathcal{H}_1) & ', round(params$dat[1,2], 3), ' \\\\
  \\text{p(BF}_{10} > ', params$t2$D, '\\, | \\, \\mathcal{H}_0) & ', round(params$dat[1,4], 3), ' \\\\
  \\textbf{Required Sample Size} & \\\\
  \\hline
  \\text{N}_1 & ', params$dat[1,5], ' \\\\
  \\text{N}_2 & ', params$dat[1,6], ' \\\\
  \\end{array}'
)

cat('$$', table_latex, '$$\n\n')

cat("\n CSV format \n")

dat_csv <- params$dat[1, ]  


cat("```\n")  
write.csv(as.data.frame(t(dat_csv)), row.names = TRUE, quote = FALSE)
cat("```\n")  
```

```{r results='asis', echo=FALSE}
if (isTRUE(params$t2$pc) || isTRUE(params$t2$rela)) {
  cat("\\newpage\n")
}
```

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

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

}

```

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

if (params$t2$pc){

print(params$pc_t2)
}

```

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

}

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

print(params$rela_t2)
}

```
