%% Resume Fitting of LDA Model to Word Count Matrix
% To reproduce the results of this example, set rng to 'default'.

rng('default')

% Load the example data. sonnetsCounts.mat contains a matrix of word counts and a corresponding vocabulary of preprocessed versions of Shakespeare's sonnets.

load sonnetsCounts.mat
size(counts)

% Fit an LDA model with 20 topics. Set the LDA solver to be approximate variational Bayes. To suppress the verbose output, set 'Verbose' to 0.

numTopics = 20;
mdl = fitlda(counts,numTopics, ...
    'Solver','avb', ...
    'Verbose',0);

% View the number of iterations the fitlda function used to fit the model.

mdl.FitInfo.NumIterations

% Resume fitting the LDA model with a lower log-likelihood tolerance.

tolerance = 1e-5;
updatedMdl = resume(mdl,counts, ...
    'LogLikelihoodTolerance',tolerance, ...
    'Verbose',0);

% View the number of iterations the resume function used to fit the model.

updatedMdl.FitInfo.NumIterations