# Refit with Saved Parameters

## 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

    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)
    #> All observations fitCode greater than 5. Nothing to refit :)

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

    parDT <- coefWriteout(refit)
    head(parDT)
    #>    Subject  Group LookType       mu        ht     sig1     sig2       base1
    #>     <char> <char>   <char>    <num>     <num>    <num>    <num>       <num>
    #> 1:       1     50   Cohort 417.6899 0.1986711 145.5628 323.1882  0.01586359
    #> 2:       1     65   Cohort 636.8447 0.2632815 306.2330 214.9787 -0.02154793
    #> 3:       2     50   Cohort 647.5295 0.2547779 496.6745 256.4257 -0.18223561
    #> 4:       2     65   Cohort 734.1526 0.2585742 405.6348 240.2926 -0.05751246
    #> 5:       3     50   Cohort 501.1949 0.2258572 398.7760 158.6752 -0.16159477
    #> 6:       3     65   Cohort 460.7152 0.3067659 382.7322 166.0833 -0.24330874
    #>         base2
    #>         <num>
    #> 1: 0.03412371
    #> 2: 0.02858644
    #> 3: 0.01217570
    #> 4: 0.03455280
    #> 5: 0.02529158
    #> 6: 0.03992168

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`

    ## Subject, Group, and LookType
    head(refit)
    #>    Subject  Group LookType        fit        R2    AR1 fitCode
    #>     <char> <char>   <char>     <list>     <num> <lgcl>   <int>
    #> 1:       1     50   Cohort <gnls[19]> 0.9701367  FALSE       0
    #> 2:       1     65   Cohort <gnls[19]> 0.9805103  FALSE       0
    #> 3:       2     50   Cohort <gnls[19]> 0.9813076  FALSE       0
    #> 4:       2     65   Cohort <gnls[19]> 0.9701257  FALSE       0
    #> 5:       3     50   Cohort <gnls[19]> 0.9765418  FALSE       0
    #> 6:       3     65   Cohort <gnls[19]> 0.9534922  FALSE       0

    ## doubleGauss pars
    colnames(coef(refit))
    #> [1] "mu"    "ht"    "sig1"  "sig2"  "base1" "base2"

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

    ## 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

    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))`

    head(new_refit)
    #> Key: <Subject, Group, LookType>
    #>    Subject  Group         LookType        fit        R2    AR1 fitCode
    #>     <char> <char>           <char>     <list>     <num> <lgcl>   <int>
    #> 1:       1     50           Cohort <gnls[19]> 0.9701367  FALSE       0
    #> 2:       1     50 Unrelated_Cohort <gnls[19]> 0.9793524  FALSE       0
    #> 3:       1     65           Cohort <gnls[19]> 0.9805103  FALSE       0
    #> 4:       1     65 Unrelated_Cohort <gnls[19]> 0.8742477  FALSE       1
    #> 5:       2     50           Cohort <gnls[19]> 0.9813076  FALSE       0
    #> 6:       2     50 Unrelated_Cohort <gnls[19]> 0.9561882  FALSE       0