% Script file name: Simulate_LCM.m
%  Purpose:
%   Simulate data base on LCM.
%  Define variables:
%   Input
%         : Number of subjects (N) 
%         : Vector of Latent Class Probability
%         : Matrix of Conditional Response Probability
%         : Number of subjects (N) 
%   Output
%         : `DataNJ' (N \times J) data set
%         : save in LCMM%dN%dJ%dData%d with all true values
%            e.g.,  LCMM2N2N400J6Data1 
DataN = input('How many data sets: ');
N= input('Number of subjects: ');
M= input('Input the number of latent classes (M): ');
PX=input('Specify latent class probability:(e.g., [0.3 0.7])\n');
disp('Specify matrix of Conditional Response Prob:')
disp('Matrix need to have the dimension of M X J')
disp('e.g., [0.2 0.2 0.2 0.2 0.2 0.2; 0.7 0.7 0.7 0.7 0.7 0.7]')
CondResProd=input('Conditional Response Probability\n');
True_M=length(PX);
True_PX=PX;
J=length(CondResProd(1,:));
True_CondResProd=CondResProd;
for DataID=1:DataN,
    for i=1:N
        LatentState(i) = sample_discrete(PX);
    end
    TrueState=LatentState;
    for i=1:N
        for j=1:J
            DataNJ(i,j)=binornd(1,CondResProd(LatentState(i),j));
        end
    end
fname=sprintf('LCMM%dN%dJ%dData%d',M,N,J,DataID);
save(fname,'DataNJ','True_PX','True_CondResProd','TrueState','True_M','J','N')
end