# Refit with Saved Parameters

library(bdots)

## Overview

This vignette walks through using a text file of previously fit model parameters to use in the bdotsRefit 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 demonstate this process, we start with fitting a set of curves to our data

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

refit <- bdotsRefit(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)
#>    Subject Group LookType       mu        ht     sig1     sig2        base1
#> 1:       1    50   Cohort 429.7595 0.1985978 159.8869 314.6389  0.009709831
#> 2:       1    65   Cohort 634.9292 0.2635044 303.8080 215.3845 -0.020636088
#> 3:       2    50   Cohort 647.0655 0.2543769 518.9633 255.9870 -0.213087542
#> 4:       2    65   Cohort 723.0547 0.2582110 392.9495 252.9384 -0.054826156
#> 5:       3    50   Cohort 501.4822 0.2247729 500.8480 158.4180 -0.331679043
#> 6:       3    65   Cohort 460.7152 0.3067659 382.7321 166.0833 -0.243308563
#>         base2
#> 1: 0.03376106
#> 2: 0.02892360
#> 3: 0.01368196
#> 4: 0.03197291
#> 5: 0.02522681
#> 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 bdotsFit

## Subject, Group, and LookType
#>    Subject Group LookType        fit        R2   AR1 fitCode
#> 1:       1    50   Cohort <gnls[18]> 0.9697202  TRUE       0
#> 2:       1    65   Cohort <gnls[18]> 0.9804901  TRUE       0
#> 3:       2    50   Cohort <gnls[18]> 0.9811708  TRUE       0
#> 4:       2    65   Cohort <gnls[18]> 0.9697466  TRUE       0
#> 5:       3    50   Cohort <gnls[18]> 0.9761906  TRUE       0
#> 6:       3    65   Cohort <gnls[18]> 0.9534922 FALSE       3

## 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 bdotsRefit function. Doing so will ignore the remaining arguments

new_refit <- bdotsRefit(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)
#>    Subject Group         LookType        fit        R2  AR1 fitCode
#> 1:       1    50           Cohort <gnls[18]> 0.9697202 TRUE       0
#> 2:       1    50 Unrelated_Cohort <gnls[18]> 0.9789994 TRUE       0
#> 3:       1    65           Cohort <gnls[18]> 0.9804901 TRUE       0
#> 4:       1    65 Unrelated_Cohort <gnls[18]> 0.8716404 TRUE       1
#> 5:      10    50           Cohort <gnls[18]> 0.8723338 TRUE       1
#> 6:      10    50 Unrelated_Cohort <gnls[18]> 0.9345526 TRUE       1