Probabilistic size-and-shape functional mixed models
Each simulation/real data example has its own main file. See details below.

Contents:
This README file contains step-by-step instructions on how to reproduce figures for simulation/data examples in Section 4 of the main paper.

Remarks:
    * Required R libraries:
	fda_6.1.4
	MASS_7.3-57
	R.matlab
	warpMix_0.1.0

List of functions in './Functions', see each file for usage details
    1. 'gram_schmidt_orthonormalization.m'
    2. 'log_lik_invgamma.m'
    3. 'log_lik_normal.m'
    4. 'qComposeGamma.m'
    5. 'qComposeGamma2.m'
    6. 'warpMixCurves.R'

Instructions for result reproduction

Preparation:
Before running our code, please:
    1. Place all code files in main working folder, and create three subfolders named 'result','figure' and 'data'. In 'figure' folder, create two subfolders, named 'PriorModel1' and 'PriorModel2'.
    2. Place the data files, growth_male_vel.txt, growth_female_vel.txt (Berkeley growth rate data) and ECGdatanew.mat (PQRST complexes data) in the subfolder named 'data'.

To reproduce panels (b)&(c) in Figure 1, run 'plotFigure1.m'.

To reproduce simulated example 1 results in Section 4.1:
1. Run 'SimCurvefromPriorModel1.m' and 'SimCurvefromPriorModel2.m' to simulate data using Prior Model 1 and Prior Model 2, respectively. There are four mandatory inputs for both of the functions: main_basis, type of basis used to model fixed effect ('Fourier' or 'BSpline'); basisSize_m, number of basis functions for fixed effect; random_basis, type of basis used to model size-and-shape altering random effect ('BSpline'); basisSize_v, number of basis functions for size-and-shape altering random effect. One can also customize inputs of other parameters, such as the sample size n, true variance for error process, etc. To simulate data used in Section 4.1, run
	SimCurvefromPriorModel1('Fourier',6,'BSpline',6)
	SimCurvefromPriorModel2('Fourier',6,'BSpline',6)
2. Run 'FMMPriorModel1.m' and 'FMMPriorModel2.m' for the MCMC sampler. There are five mandatory inputs for 'FMMPriorModel1.m': data type ('simulated','BG' or 'ECG'), main_basis, basisSize_m, random_basis, basisSize_v are the same as in step 1. There is one more mandatory input for 'FMMPriorModel2.m', the number of time points for specifying the prior for phase functions (nG=5). To generate results shown in Section 4.1, run
	FMMPriorModel1('simulated','Fourier',6,'BSpline',6)
	FMMPriorModel2('simulated','Fourier',6,'BSpline',6,5)
3. Run warpMixSimDataOurModel.R to implement warpMix algorithm on data simulated from our models.
4. To reproduce Figure 2, run
	'plotSimdataResultPriorModel1.m'
	'plotSimdataResultPriorModel2.m'

To reproduce simulated example 2 results in Section 4.1:
1. Run 'SimCurvewarpMix.R' to simulate data from warpMix model. This calls function warpMixCurves.R
2. Run 'FMMPriorModel1warpMix.m' and 'FMMPriorModel2warpMix.m' for the MCMC sampler. There are 5 or 6 mandatory inputs for the two functions, respectively. main_basis ('Fourier' or 'BSpline'), basisSize_m (=6), random_basis ('BSpline'), basisSize_v (=6), and nG (=7) have the same meaning as in step 2 for Section 4.1 above. The last input dataidx (1,2 or 3) specifies which one of the three different fixed effect functions are used. 
For Model 1-F in Table 1, run
	FMMPriorModel1warpMix('Fourier',6,'BSpline',6,1)
	FMMPriorModel1warpMix('Fourier',6,'BSpline',6,2)
	FMMPriorModel1warpMix('Fourier',6,'BSpline',6,3)
For Model 1-B in Table 1, run
	FMMPriorModel1warpMix('BSpline',6,'BSpline',6,1)
	FMMPriorModel1warpMix('BSpline',6,'BSpline',6,2)
	FMMPriorModel1warpMix('BSpline',6,'BSpline',6,3)
For Model 2-F in Table 1, run
	FMMPriorModel2warpMix('Fourier',6,'BSpline',6,7,1)
	FMMPriorModel2warpMix('Fourier',6,'BSpline',6,7,2)
	FMMPriorModel2warpMix('Fourier',6,'BSpline',6,7,3)
For Model 2-B in Table 1, run
	FMMPriorModel2warpMix('BSpline',6,'BSpline',6,7,1)
	FMMPriorModel2warpMix('BSpline',6,'BSpline',6,7,2)
	FMMPriorModel2warpMix('BSpline',6,'BSpline',6,7,3)
3. Run 'warpMixSimData.R' to implement warpMix algorithm on data simulated from warpMix model. The result file delta_mu.txt comprises the first column in Table 1.
4. To reproduce Figure 3, run 'plotwarpMixResultsPriorModel2.m', input dataidx 1,2 and 3.
	plotwarpMixResultsPriorModel2(1)
	plotwarpMixResultsPriorModel2(2)
	plotwarpMixResultsPriorModel2(3)	
5. To reproduce columns 2-4 in Table 1, run 'warpMixTable1.m'

To reproduce real data results in Section 4.2:
1. For Berkeley growth rate data, run
	FMMPriorModel1('BG','Fourier',6,'BSpline',6)
2. For PQRST data, run
	FMMPriorModel2('ECG','BSpline',12,'BSpline',6, 5)
3. Run 'warpMixBerkeleyGrowthVelocity.R' to implement warpMix algorithm on Berkeley growth rate data
4. To reproduce Figure 4 and panels (a)&(d) in Figure 1, run
	'plotRealdataResultPriorModel1.m'
	'plotRealdataResultPriorModel2.m'

Computing resources:
    1. Simulations and real data examples implementing our models were run on (anonymized university's) computing server, 6 parallel Intel(R) Xeon(R) CPU's, 20GB requested memory for each task, MATLAB R2021a.
    2. Simulations and real data examples implementing warpMix were run on a laptop platform x86_64-apple-darwin17.0 (64-bit), 8-core CPU, 16GB memory, R version 4.2.1.
