---
title: "Parameter fitting log"
output:
  html_document:
    toc: true
    theme: cosmo
date: "`r format(Sys.time(), '%F %R %Z')`"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
if(!exists("fit")) {
  # HACK: load environment to have (hopefully) some testing data in place for
  # vignette development
  message("Loading environment from `.Rdata` file ...")
  load(".Rdata")
}
```
<style>
r { color: Red }
g { color: Green }
</style>

# R environment

Session information and package versions.

```{r envir_setup}
si <- sessionInfo()
# imitates the behavior of print.sessionInfo() but looks nicer
```

* `r si$R.version$version.string`
* cvasi version: `r packageVersion("cvasi")`
* Platform: `r si$platform`
* Running under: `r si$running`
* Matrix products: `r si$matprod`
* Locale: `r gsub(";", ", ", si$locale)`
* Attached base packages: `r paste(si$basePkgs, sep="", collapse=", ")`

```{r envir_tables}
df <- data.frame(name=sapply(si$otherPkgs, `[[`, "Package"), version=sapply(si$otherPkgs, `[[`, "Version"))
if(nrow(df) == 0)
  df <- data.frame(name="none", version="-")
knitr::kable(df, caption="Other attached packages", row.names=FALSE)

df <- data.frame(name=sapply(si$loadedOnly, `[[`, "Package"), version=sapply(si$loadedOnly, `[[`, "Version"))
knitr::kable(df, caption="Loaded via namespace (not attached)", row.names=FALSE)

rm(si, df)
```


# Configuration

calibration settings
  start values (> calibrate)
  error function (> calibrate)
  optim method > calibrate
  parameter boundaries (if used)  > calibrate
  solver settings (method, rtol, atol?)  > impossible atm


# Experimental data

trial data by trial id
  observed  > fit/cs
  weights?  > fit/cs
  exposure  > fit/cs

## Fitted scenarios

scenario infos by trial id  > fit, attach calisets
  name   
  parameters
  initial state
  transfers
  forcings


# Results

`r ifelse(fit$convergence == 0, "The parameter fit was **<g>SUCCESSFUL</g>**", "Problems occurred during fit. Please **<r>check results</r>**")`.

### Fitted parameter values

```{r fitted,results='asis'}
cat(paste("- ", names(fit$par), ": ", fit$par, sep="", collapse="\n"))
```

### Confidence intervals

Calculated using the Hessian matrix (`r fit$dof` degrees of freedom):

```{r ci,results='asis'}
cat(paste("- ", names(fit$ci), ": [", paste(unlist(fit$ci), collapse="; "), "]",  sep="", collapse="\n"))
```

### Additional outputs

* Error function value: `r fit$value` (`r ifelse(is.character(fit$err_fun), fit$err_fun, "custom function")`)
* Convergence: `r fit$convergence` (`r ifelse(fit$convergence == 0, "successful", "error")`)
* Message: `r ifelse(is.null(fit$message), "*none*", fit$message)`

```{r hessian}
knitr::kable(fit$hessian, caption="Hessian matrix")
```

```{r}
# Original values of observed vs predicted
if(fit$err_fun != "sse") {
  knitr::kable(fit$hessian, caption="Hessian matrix")
}
```


fit result   > fit return value
  obs vs pred (original, if necessary)    > fit ✓
  obs vs pred vs residuals (can be transformed)  > fit ✓
  
  
