---
title: "Refit with Saved Parameters"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Refit with Saved Parameters}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup, include = FALSE}
library(data.table)
library(bdots)
# Make smaller for cran
cohort_unrelated$Subject <- as.numeric(cohort_unrelated$Subject)
cohort_unrelated <- as.data.table(cohort_unrelated)
cohort_unrelated <- cohort_unrelated[Subject < 10, ]
```

## Overview

This vignette walks through using a text file of previously fit model parameters to use in the `brefit` function. This is convenient if you have already gone through the refitting process and would like to save/load the refitted parameters in a new session.

To demonstrate this process, we start with fitting a set of curves to our data

```{r}
library(bdots)

fit <- bfit(data = cohort_unrelated,
            subject = "Subject",
            time = "Time",
            y = "Fixations",
            group = c("Group", "LookType"),
            curveFun = doubleGauss(concave = TRUE),
            cor = TRUE,
            numRefits = 2,
            cores = 2,
            verbose = FALSE)

refit <- brefit(fit, quickRefit = TRUE, fitCode = 5)
```

From this, we can create an appropriate `data.table` that can be used in a later session

```{r}
parDT <- coefWriteout(refit)
head(parDT)
```

It's important that columns are included that match the unique identifying columns in our `bdotsObj`, and that the parameters match the coefficients used from `bfit`

```{r}
## Subject, Group, and LookType
head(refit)

## doubleGauss pars
colnames(coef(refit))
```

We can save our parameter `data.table` for later use, or read in any other appropriately formatted `data.frame`

```{r, eval = FALSE}
## Save this for later using data.table::fwrite
fwrite(parDT, file = "mypars.csv")
parDT <- fread("mypars.csv")
```

Once we have this, we can pass it as an argument to the `brefit` function. Doing so will ignore the remaining arguments

```{r}
new_refit <- brefit(refit, paramDT = parDT)
```

We end up with a `bdotsObj` that matches what we had previously. As seeds have not yet been implemented, the resulting parameters may not be exact. It will, however, assist with not having to go through the entire refitting process again manually (although, there is always the option to save the entire object with `save(refit, file = "refit.RData))`

```{r}
head(new_refit)
```
