I'm doing an MVN model. My procedure section looks like this
PROCEDURE_SECTION
for(int i=1; i<=ncoeffs; i++)
{
coeffs(i)=ucoeffs(i)*scale(i);
}
nLL=0;
for(int p=1; p<=nponds; p++)
{
calc_predictions(p);
calc_varcov(p);
dvar_vector diff=predictions(p)-obs(p);
dvar_matrix tmp_varcov=varcov(p);
nLL+=.5*(nobs(p)*log(2*M_PI)+ln_det(tmp_varcov)+ diff*solve2(tmp_varcov,diff));
}
The first for loop scales all my coefficients to make it conditioned better.
Then I have observations of a bunch of ponds and each pond is assumed to be independent of the others.
I defined functions calc_predictions() and calc_varcov() to fill in those objects; the code in these is pretty specific to my problem. The important thing fir efficiency is that my covariates are in a design matrix created using model.matrix() in R.
Try using solve() where I have solve2(). If you have a problem with solve, I'll explain the complications of why I used this version.
I hope this helps.
cheers,
Mollie