function [theta,etas,lambdas,sigma2,y,xhat] = EMt(id,t,x,a,b,theta,etas,lambdas,sigma2,nk,order,nu,maxiter) %function [theta,etas,lambdas,sigma2,y,xhat] = EMt(id,t,x,a,b,theta,etas,lambdas,sigma2,nk,order,nu,maxiter) %Semiparametric t-estimators of the mean and principal components (via EM algorithm) % %NOTE 1: since the time grids may be sparse and irregular, the data is input % as a concatenated vector rather than a matrix; a vector of labels is used % to identify the individuals % %NOTE 2: although the program accepts arbitrary initial estimators, it is % best used in a stepwise manner, i.e.: fit a mean + error model first % (using program EMt0), then fit a one-component model using the output THETA % from EMt0 as initial THETA, and so on. % %INPUT: % id: Individual labels (N x 1) % t: Time grid (N x 1) % x: Observations (N x 1) % a: Time range, lower bound (scalar) % b: Time range, upper bound (scalar) % theta: Initial mean coefficients ((nk+order) x 1) % (See Note 2 above) % etas: Initial PC coefficients ((nk+order) x q) % (See Note 2 above; we suggest you take etas = [etas0, ones((nk+order),1)] % where etas0 is the output from the previous (q-1) component model) % lambdas: Initial eigenvalues (q x 1) % (See Note 2 above; we suggest you take lambdas = [lambdas0; lambdas0(end)/2] % where lambdas0 is the output from the previous (q-1) component model) % sigma2: Initial variance estimator (scalar) % (See Note 2 above) % nk: Number of knots (scalar) % (Equispaced knots in [a,b] will be used) % order: Spline order (scalar) % nu: t model degrees of freedom (scalar) % (Use nu = 1 for Cauchy estimators) % maxiter: Max. number of iterations (scalar) % %OUTPUT: % theta: Estimated mean coefficients ((nk+order) x 1) % etas: Estimated PC coefficients ((nk+order) x 1) % lambdas: Estimated eigenvalues (q x 1) % sigma2: Estimated variance (scalar) % y: Predicted component scores (q x n) % xhat: Predicted values of X (N x 1) % %External programs called: BSPL indiv = unique(id); n = length(indiv); knots = linspace(a,b,nk+2); p = nk+order; q = length(lambdas); if q==0 disp('For a mean + error model use EMt0') return end if size(etas,1)~=p || size(etas,2)~=q disp('Wrong dimension of ETAS') return end if length(theta)~=p disp('Wrong dimension of THETA') return end H = etas*diag(sqrt(lambdas)); grN = max(200,10*nk); B0 = bspl(linspace(a,b,grN+1),order,knots,0); J0 = B0'*B0*(b-a)/grN; [U,S,V] = svd(H'*J0*H); H = H*U; y = zeros(q,n); xhat = zeros(size(x)); err = 1; iter = 0; while err>1e-4 && iter