%% (Do not use - Delete)Assess Model Specification Using the Lagrange Multiplier Test
% Compare two model specifications for simulated education and income data. The unrestricted model has the following loglikelihood:
% l(\beta,\rho) = \log L(\beta,\rho) = -n\log\Gamma(\rho) + \rho\displaystyle\sum^n_{i=1}\log\beta_i + (\rho-1)\displaystyle\sum^{n}_{i=1}[\log(y_i)-y_i\beta_i],
% where
% \beta_i=\frac{1}{\beta+x_i}.
% x_i is the number of grades that person i completed.
% y_i is the income (in thousands of USD) of person i.
% That is, the income of person i given the number of grades that person i completed is Gamma distributed with shape \rho and rate \beta_i. The restricted model sets \rho = 1, which implies that the income of person i given the number of grades person i completed is exponentially distributed with mean \beta + x_i
% Load the data.

load Data_Income1
x = DataTable.EDU;
y = DataTable.INC;

%
% Set the parameters to their restricted values.

rbeta = 15.6027;    % Restricted beta estimate
rrho = 1;           % Restricted rho
dof = 1;            % Number of restrictions

%
% Estimate the parameter covariance under the restricted model using the outer product of gradients (OPG).

RCG = [-rrho./(rbeta+x)+y.*(rbeta+x).^(-2),...
      -log(rbeta+x)-psi(rrho)+log(y)];
Rscore = sum(RCG)';
REstCov1 = inv(RCG'*RCG);

%
% Test the unrestricted model against the restricted model.

[lmH,lmP,lmStat,lmCv] = lmtest(Rscore,REstCov1,dof)

% lmH = 1 means that there is sufficient evidence to suggest that the unrestricted model fits the data better than the restricted model.
% Copyright 2015 The MathWorks, Inc.