---
title: "Spectral GLM results"
output: html_document
params:
  data: ""
  label: ""
  regressor_df: ""
  mrs_data_plot: ""
  xlim: ""
  vline: ""
  exclude_labels: ""
---

```{r setup, include=FALSE}
library(spant)
library(ggplot2)
knitr::opts_chunk$set(echo = FALSE)
theme_set(theme_bw())
```

## `r params$label`

### Mean spectrum
```{r, fig.width=10}
par(mfcol = c(1, 2))
plot(params$mrs_data_plot, xlim = xlim, restore_def_par = FALSE,
     vline = params$vline)
```

```{r, fig.width=10, results='asis'}
Nregressors <- ncol(params$regressor_df) - 1

for (n in 1:Nregressors) {
  cat(paste0("\n\n### regressor : ", names(params$regressor_df)[n + 1], "\n"))
  par(mfcol = c(1, 2))
  log_p_val_spec <- get_dyns(params$data$p_value_log_mrs, n)
  plot(log_p_val_spec, y_scale = TRUE,
       yaxis_lab = expression("-log"[10]*"(p-value)"),
       restore_def_par = FALSE, main = "Manhattan plot", xlim = params$xlim,
       vline = params$vline)
  beta_weight_spec <- get_dyns(params$data$beta_weight_mrs, n)
  plot(beta_weight_spec, y_scale = TRUE, yaxis_lab = "Intensity (au)",
       main = "beta weights", xlim = params$xlim, vline = params$vline)
}
```

### Design matrix
```{r, fig.width=8, fig.height=8}
plot_reg(params$regressor_df)
```

```{r results='asis'}
if (!is.null(params$exclude_labels)) {
  cat("\n\n### Excluded scan labels\n")
  for (x in params$exclude_labels) cat("- ", x, "\n")
}
```