JuliaStats / MixedModels.jl

A Julia package for fitting (statistical) mixed-effects models
http://juliastats.org/MixedModels.jl/stable
MIT License
402 stars 47 forks source link

save also formula with saveoptsum() #725

Closed kliegl closed 7 months ago

kliegl commented 8 months ago

Is there an argument against saving the model formula along with the fitted model object?

palday commented 8 months ago

There would be some annoying extra bookkeeping to preserve backwards compatibility models saved without it. That's not insurmountable, but the formula doesn't preserve all the information about the model -- we don't save any of the model matrices nor the contrast coding, so that still have to be recovered elsewhere. My question in return is: what's the argument for saving the model formula?

kliegl commented 8 months ago

I see.

I have / had the very frequent problem that an inadvertent slight change of the model formula in the script does not allow me to restore a model object; usually a consequence of my top-down style of model selection. It is probably also interference from the save-load lme4 past. This is annoying because I usually only save model objects that take a long time to fit. No worry: I have trained myself (better: the program trained me) to reduce this problem substantially, but I am sure my frustration is shared with others ;)

Curious: Is there a best practice recommendation from your side on this? My current "style" is that, after the fit, I immediately insert the restore lines, then I comment out the fit() and saveoptsum() lines and save the script as follows:

f = @formula ...;
#m = fit(MixedModel, f, data; contrasts)
#issingular(m)
#MixedModels.PCA(m)
#saveoptsum("fits/m_optsum.json", m);

m = LinearMixedModel(f, data; contrasts);                    
restoreoptsum!(m, "./fits/m_optsum.json");

In the past, my restore lines were at the end of the script, that is far away from the formula definition. You also cannot generate the formula on the fly.

A related question: Would it be possible to have a saveoptsum() mode kwarg to generate read-only model json files?