%% Statistics in Matlab
%
% Matlab has a large and growing library of programs for computation and statistical inference.  One entry point is here:

help stats

% In this module, we'll take a brief overview of some of the functions available and give some pointers to how they work.
% Table of Contents
% Why Matlab?
% A brief list of selected statistics functions
% Discovering statistics objects
%     Methods and properties
%     Choices: Objects and classical functions
% Trying out a model on a sample dataset
% Help and doc
% Activity: Explore by fitting a statistics model to a dataset
%% Why Matlab?
% First and foremost, this is the language & environment I know best. Python is more popular and has some substantial advantages, but here are some dimensions I think Matlab is better on:
% Professionally implemented algorithms designed to work well together
% Stable, platform-independent functionality with few external dependencies (runs seamlessly on all computer architectures)
% Long-term backward compatibility (my scripts from 2002 still run!)
% Easy, transparent coding with great editor and great debugging
% Great help tutorials and examples
% % for help:
% doc
% Matlab is not so good at being free, so access without a licence or Uni site license is the biggest issue. It's also not so good at seamless deployment on servers to run processes in the cloud.  They are working on these things within the "not free" constraints, but this is a major reason to use Python.  Matlab is, however, actively working on some new things:
% Matlab online-- cloud-based, in-browser analysis
% Jupyter notebook integration (a Matlab kernel now exists)
% Deep learning -- many pre-trained models and tutorials
% Machine learning (including fair ML, interpretable ML)
% Live scripts (.mlx files)
% Table objects to import, export, and store datasets (improved in recent years!)
% Probably many, many other things I won't cover (e.g., hardware control, simulink)
%% A brief list of selected statistics functions
% Here are some sections in the statistics and machine learning toolbox, along with a selection of functions I think are likely to be particularly useful. This is one of many toolboxes (also not free) that inter-operate with the core Matlab distribution:
% Probability distributions: PDFs, CDFs, parameters, fitting (parameter estimation),  critical values (for inference), and likelihood functions for many distributions.
% Analysis of variance (anovan, manova) and repeated measures (fitrm)
% GLM  (e.g., glmfit, regress), including robust estimation (fitlm, robustfit), generalized models, time series models / autocorrelation, penalized models (ridge, lasso, elasticnet), regression diagnostics (regstats), model selection (stepwisefit) .
% Linear mixed effects models (fitlme),
% Randomization tests: Bootstrapping, permutation,
% Design of experiments
% Clustering (e.g., clusterdata)
% Classification: LDA (e.g., classify, fitcdiscr), KNN (fitcknn), SVM (fitcsvm), Naive Bayes (fitcnb), neural networks (fitcnet), GAMs (fitcgam), classification trees (fitctree, TreeBagger), ensembles (fitcensemble), model search (fitcauto),
% Dimension reduction and component analysis: PCA (pca), ICA (rica), factor analysis (factoran), non-neg matrix factorization (nnmf), tsne (tsne), MDS (cmdscale).
% Multivariate regression/prediction: PLS (plsregress), canonical correlation (canoncorr), SVR (fitrsvm), Gaussian processes(fitrgp), neural networks (fitrnet), GAMs (fitrgam), classification trees (fitrtree), ensembles (fitrensemble), model search (fitrauto),
% There are also Graphic User Interface (GUI) Apps that allow you to explore things (e.g., deep learning, curve fitting, distribution fitting)
% distributionFitter
% robustdemo
% nlintool
% aoctool
% classificationLearner
% regressionLearner
%
%% Discovering statistics objects
% Matlab has been moving steadily towards the use of objects and object-oriented programming, which is a general design feature of modern languages and toolboxes.  Objects are "collections" of variables with pre-defined properties and methods. The have a class, which means each object type is its own unique type of variable.   Properties are data fields attached to the objects. Methods are functions that can be run on the object.  Each object has a constructor method for the object type that builds and returns the object.
% Python and R (and some other packages) work similarly, so what you do with Matlab will transfer to some degree.
% For example, the code below gives you some help on a few of the common types of objects used in stats in Matlab, including their properties and methods:

% Table objects are the preferred way to store datasets
help table

%

% RepeatedMeasuresModel is an object related to ANOVA/RMANOVA/MANOVA models
help RepeatedMeasuresModel

% You would use the function fitrm, for example, to fit a repeated measures model to some data and return a model object with estimated parameters (and other associated properties) as output.
%

% LinearMixedModel is an object related to mixed effects models (akin to
% lmer in R software)
help LinearMixedModel

% You would use the function fitlme, for example, to fit a mixed effects model to  data and return a model object with estimated parameters (and other associated properties) as output.
% Related object types are:  LinearModel, GeneralizedLinearModel, NonLinearModel
%

% ClassificationSVM is an object related to multivariate classification. 
help ClassificationSVM

% Notice the list of related functions at the bottom, including fitcsvm, the constructor method for the object type that builds and returns the object. You may also see related object types.
% The code below lists types of objects related to classification and regression:

disp('Classification models')
classreg.learning.classificationModels

disp(' ')
disp('Regression models')
classreg.learning.regressionModels

% You can then get help on these other object classes using "Classification" or "Regression" joined with the type:
% help ClassificationTree
% help RegressionTree
%
%% Methods and properties
% To get associated methods for an object, type "methods <objectClass>"

properties LinearMixedModel
methods LinearMixedModel

%
%% Choices: Objects and classical functions
% Over time, older functions that run statistical algorithms have been replaced with object-oriented methods. You can use either, depending on your use case and preference.
% For example, to fit a multiple regression model, you can use the functions regress or glmfit (which includes logistic/logit/generalized linear modeling with different link functions), which works perfectly fine.  Or you can use fitlm with the LinearModel object.
% The model objects have shifted from taking vector and matrix inputs to taking datasets stored in table objects as inputs, and allowing you to specify model formulas using Wilkinson notation.
%
%% Trying out a model on a sample dataset
% Many fitting methods include sample code in their help.  e.g., type "help fitlme", and you get the code below. Try it, and you load a dataset and fit a linear mixed effects model with a random intercept:

load carsmall
ds = dataset(MPG,Weight,Model_Year);
lme = fitlme(ds,'MPG ~ Weight + (1|Model_Year)')

% Then, you can explore the model's properties and methods.
% Let's display a couple of the properties of the estimated model:

lme.Rsquared % Variance explained
lme.MSE      % Mean squared error

%
%% Help and doc
% Help <function name> brings up the help text for that function (or object class).
% doc <function name> brings up an interactive browser with even more help and examples.
% doc LinearMixedModel
%
%
%% Activity: Explore by fitting a statistics model to a dataset
% Choose a model object or function.
% Get the help on that function.
% Use the help to fit the model.
% What are the outputs? If an object, what are some of the properties and methods?
%
%