---
title: "BayesPower: Output"
output:
  html_document: default
fontsize: 12pt
params:
  ff: NA
  dat: NA
  pc_f: NA
  rela_f: NA
geometry: margin=2cm
classoption: a4paper
---

A Bayes Factor design analysis for regression/ANOVA 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_f_code(params$ff))
cat("\n```")


```

```{r echo=FALSE, results='asis'}
lambda2 <- "\\lambda^2"
rope <- params$ff$ROPE

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

if (params$ff$inter == 1 || is.null(rope)) {
  cat(sprintf(
    "\\(\\mathcal{H}_0: %s = 0\\) vs \\(\\mathcal{H}_1: %s > 0\\)  \n",
    lambda2, lambda2
  ))
} else if (params$ff$inter == 2) {
  upper <- max(rope)
  cat(sprintf(
    "\\(\\mathcal{H}_0: 0 \\leq %s \\leq %.2f\\) vs \\(\\mathcal{H}_1: %s > %.2f\\)  \n",
    lambda2, upper, lambda2, upper
  ))
}
```

```{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_ff_prior_rmd <- function(prior, rscale = NULL, f_m = NULL, dff = NULL) {

  if (is.null(prior) || prior == "Point") {
    return(sprintf("Point (\\(f^2 = %s\\))", fmt(f_m^2)))
  }

  if (prior %in% c("tdis", "effectsize")) {
    return(sprintf(
      "Effect size prior (scale = %s, \\(f^2 = %s\\), df = %s)",
      fmt(rscale), fmt(f_m^2), fmt(dff)
    ))
  }

  if (prior == "Moment") {
    return(sprintf(
      "Moment prior (\\(f^2 = %s\\), df = %s)",
      fmt(f_m^2), fmt(dff)
    ))
  }

  as.character(prior)
}

analysis_prior_text <- print_ff_prior_rmd(
  prior  = params$ff$prior_analysis,
  rscale = params$ff$rscale,
  f_m    = params$ff$f_m,
  dff    = params$ff$dff
)

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

cat("Analysis/design prior under \\(\\mathcal{H}_0\\):  \n")
if (params$ff$inter == 1 || is.null(params$ff$ROPE)) {
  cat("Point prior (location = 0)  \n\n")
} else {
  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$ff$de_an_prior == 1) {
  cat(analysis_prior_text)
} else {
  cat(print_ff_prior_rmd(
    prior  = params$ff$prior_design,
    rscale = params$ff$rscale_d,
    f_m    = params$ff$f_m_d,
    dff    = params$ff$dff_d
  ))
}

cat("  \n")

```

```{r results='asis', echo=FALSE}
cat("**The compared models:**  \n\n")

if (params$ff$anovareg == 1) {

  choices <- c(
    "1" = "Intercept model",
    "2" = "One-factor model",
    "3" = "Two-factor main-effect model",
    "4" = "Two-factor interaction-effect model"
  )

  cat("Reduced model selected: ", choices[as.character(params$ff$reduced_model)], "  \n", sep = "")
  cat("Full model selected: ", choices[as.character(params$ff$full_model)], "  \n", sep = "")

  if (params$ff$full_model > 1) {
    cat(sprintf("Number of levels for factor one: %.0f  \n", params$ff$f1))
  }

  if (params$ff$full_model > 2) {
    cat(sprintf("Number of levels for factor two: %.0f  \n", params$ff$f2))
  }

} else if (params$ff$anovareg == 2) {

  cat("**Number of predictors:**  \n\n")
  cat(sprintf("p (reduced model): %.0f  \n", params$ff$p))
  cat(sprintf("k (full model): %.0f  \n", params$ff$k))
}
```



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

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


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

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


if (params$ff$pc){

print(params$pc_f)
  }

```

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

}

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

print(params$rela_f)
}

```
