File:  [Local Repository] / imach064 / src / imach.c
Revision 1.11: download - view: text, annotated - select for diffs
Thu May 17 16:07:14 2001 UTC (23 years, 5 months ago) by lievre
Branches: MAIN
CVS tags: HEAD
Updated to Windows2000: changes in the path name to execute wgnuplot.

    
/*********************** Imach **************************************        
  This program computes Healthy Life Expectancies from cross-longitudinal 
  data. Cross-longitudinal consist in a first survey ("cross") where 
  individuals from different ages are interviewed on their health status
  or degree of  disability. At least a second wave of interviews 
  ("longitudinal") should  measure each new individual health status. 
  Health expectancies are computed from the transistions observed between 
  waves and are computed for each degree of severity of disability (number
  of life states). More degrees you consider, more time is necessary to
  reach the Maximum Likelihood of the parameters involved in the model. 
  The simplest model is the multinomial logistic model where pij is
  the probabibility to be observed in state j at the second wave conditional
  to be observed in state i at the first wave. Therefore the model is:
  log(pij/pii)= aij + bij*age+ cij*sex + etc , where 'age' is age and 'sex' 
  is a covariate. If you want to have a more complex model than "constant and
  age", you should modify the program where the markup 
    *Covariates have to be included here again* invites you to do it.
  More covariates you add, less is the speed of the convergence.

  The advantage that this computer programme claims, comes from that if the 
  delay between waves is not identical for each individual, or if some 
  individual missed an interview, the information is not rounded or lost, but
  taken into account using an interpolation or extrapolation.
  hPijx is the probability to be
  observed in state i at age x+h conditional to the observed state i at age 
  x. The delay 'h' can be split into an exact number (nh*stepm) of 
  unobserved intermediate  states. This elementary transition (by month or 
  quarter trimester, semester or year) is model as a multinomial logistic. 
  The hPx matrix is simply the matrix product of nh*stepm elementary matrices
  and the contribution of each individual to the likelihood is simply hPijx.

  Also this programme outputs the covariance matrix of the parameters but also
  of the life expectancies. It also computes the prevalence limits. 
  
  Authors: Nicolas Brouard (brouard@ined.fr) and Agnès Lièvre (lievre@ined.fr).
           Institut national d'études démographiques, Paris.
  This software have been partly granted by Euro-REVES, a concerted action
  from the European Union.
  It is copyrighted identically to a GNU software product, ie programme and
  software can be distributed freely for non commercial use. Latest version
  can be accessed at http://euroreves.ined.fr/imach .
  **********************************************************************/
 
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define MAXLINE 256
#define FILENAMELENGTH 80
/*#define DEBUG*/
#define windows
#define	GLOCK_ERROR_NOPATH		-1	/* empty path */
#define	GLOCK_ERROR_GETCWD		-2	/* cannot get cwd */

#define MAXPARM 30 /* Maximum number of parameters for the optimization */
#define NPARMAX 64 /* (nlstate+ndeath-1)*nlstate*ncovmodel */

#define NINTERVMAX 8
#define NLSTATEMAX 8 /* Maximum number of live states (for func) */
#define NDEATHMAX 8 /* Maximum number of dead states (for func) */
#define NCOVMAX 8 /* Maximum number of covariates */
#define MAXN 20000
#define YEARM 12. /* Number of months per year */
#define AGESUP 130
#define AGEBASE 40


int nvar;
int cptcovn, cptcovage=0, cptcoveff=0,cptcov;
int npar=NPARMAX;
int nlstate=2; /* Number of live states */
int ndeath=1; /* Number of dead states */
int ncovmodel, ncov;     /* Total number of covariables including constant a12*1 +b12*x ncovmodel=2 */

int *wav; /* Number of waves for this individuual 0 is possible */
int maxwav; /* Maxim number of waves */
int jmin, jmax; /* min, max spacing between 2 waves */
int mle, weightopt;
int **mw; /* mw[mi][i] is number of the mi wave for this individual */
int **dh; /* dh[mi][i] is number of steps between mi,mi+1 for this individual */
double jmean; /* Mean space between 2 waves */
double **oldm, **newm, **savm; /* Working pointers to matrices */
double **oldms, **newms, **savms; /* Fixed working pointers to matrices */
FILE *fic,*ficpar, *ficparo,*ficres,  *ficrespl, *ficrespij, *ficrest;
FILE *ficgp, *fichtm;
FILE *ficreseij;
  char filerese[FILENAMELENGTH];
 FILE  *ficresvij;
  char fileresv[FILENAMELENGTH];
 FILE  *ficresvpl;
  char fileresvpl[FILENAMELENGTH];

#define NR_END 1
#define FREE_ARG char*
#define FTOL 1.0e-10

#define NRANSI 
#define ITMAX 200 

#define TOL 2.0e-4 

#define CGOLD 0.3819660 
#define ZEPS 1.0e-10 
#define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d); 

#define GOLD 1.618034 
#define GLIMIT 100.0 
#define TINY 1.0e-20 

static double maxarg1,maxarg2;
#define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)>(maxarg2)? (maxarg1):(maxarg2))
#define FMIN(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)<(maxarg2)? (maxarg1):(maxarg2))
 
#define SIGN(a,b) ((b)>0.0 ? fabs(a) : -fabs(a))
#define rint(a) floor(a+0.5)

static double sqrarg;
#define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 :sqrarg*sqrarg)
#define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;} 

int imx; 
int stepm;
/* Stepm, step in month: minimum step interpolation*/

int m,nb;
int *num, firstpass=0, lastpass=4,*cod, *ncodemax, *Tage;
double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint;
double **pmmij;

double *weight;
int **s; /* Status */
double *agedc, **covar, idx;
int **nbcode, *Tcode, *Tvar, **codtab, **Tvard, *Tprod, cptcovprod, *Tvaraff;

double ftol=FTOL; /* Tolerance for computing Max Likelihood */
double ftolhess; /* Tolerance for computing hessian */

/**************** split *************************/
static	int split( char *path, char *dirc, char *name )
{
   char	*s;				/* pointer */
   int	l1, l2;				/* length counters */

   l1 = strlen( path );			/* length of path */
   if ( l1 == 0 ) return( GLOCK_ERROR_NOPATH );
   s = strrchr( path, '\\' );		/* find last / */
   if ( s == NULL ) {			/* no directory, so use current */
#if	defined(__bsd__)		/* get current working directory */
      extern char	*getwd( );

      if ( getwd( dirc ) == NULL ) {
#else
      extern char	*getcwd( );

      if ( getcwd( dirc, FILENAME_MAX ) == NULL ) {
#endif
         return( GLOCK_ERROR_GETCWD );
      }
      strcpy( name, path );		/* we've got it */
   } else {				/* strip direcotry from path */
      s++;				/* after this, the filename */
      l2 = strlen( s );			/* length of filename */
      if ( l2 == 0 ) return( GLOCK_ERROR_NOPATH );
      strcpy( name, s );		/* save file name */
      strncpy( dirc, path, l1 - l2 );	/* now the directory */
      dirc[l1-l2] = 0;			/* add zero */
   }
   l1 = strlen( dirc );			/* length of directory */
   if ( dirc[l1-1] != '\\' ) { dirc[l1] = '\\'; dirc[l1+1] = 0; }
   return( 0 );				/* we're done */
}


/******************************************/

void replace(char *s, char*t)
{
  int i;
  int lg=20;
  i=0;
  lg=strlen(t);
  for(i=0; i<= lg; i++) {
    (s[i] = t[i]);
    if (t[i]== '\\') s[i]='/';
  }
}

int nbocc(char *s, char occ)
{
  int i,j=0;
  int lg=20;
  i=0;
  lg=strlen(s);
  for(i=0; i<= lg; i++) {
  if  (s[i] == occ ) j++;
  }
  return j;
}

void cutv(char *u,char *v, char*t, char occ)
{
  int i,lg,j,p=0;
  i=0;
  for(j=0; j<=strlen(t)-1; j++) {
    if((t[j]!= occ) && (t[j+1]== occ)) p=j+1;
  }

  lg=strlen(t);
  for(j=0; j<p; j++) {
    (u[j] = t[j]);
  }
     u[p]='\0';

   for(j=0; j<= lg; j++) {
    if (j>=(p+1))(v[j-p-1] = t[j]);
  }
}

/********************** nrerror ********************/

void nrerror(char error_text[])
{
  fprintf(stderr,"ERREUR ...\n");
  fprintf(stderr,"%s\n",error_text);
  exit(1);
}
/*********************** vector *******************/
double *vector(int nl, int nh)
{
  double *v;
  v=(double *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(double)));
  if (!v) nrerror("allocation failure in vector");
  return v-nl+NR_END;
}

/************************ free vector ******************/
void free_vector(double*v, int nl, int nh)
{
  free((FREE_ARG)(v+nl-NR_END));
}

/************************ivector *******************************/
int *ivector(long nl,long nh)
{
  int *v;
  v=(int *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(int)));
  if (!v) nrerror("allocation failure in ivector");
  return v-nl+NR_END;
}

/******************free ivector **************************/
void free_ivector(int *v, long nl, long nh)
{
  free((FREE_ARG)(v+nl-NR_END));
}

/******************* imatrix *******************************/
int **imatrix(long nrl, long nrh, long ncl, long nch) 
     /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */ 
{ 
  long i, nrow=nrh-nrl+1,ncol=nch-ncl+1; 
  int **m; 
  
  /* allocate pointers to rows */ 
  m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*))); 
  if (!m) nrerror("allocation failure 1 in matrix()"); 
  m += NR_END; 
  m -= nrl; 
  
  
  /* allocate rows and set pointers to them */ 
  m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int))); 
  if (!m[nrl]) nrerror("allocation failure 2 in matrix()"); 
  m[nrl] += NR_END; 
  m[nrl] -= ncl; 
  
  for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol; 
  
  /* return pointer to array of pointers to rows */ 
  return m; 
} 

/****************** free_imatrix *************************/
void free_imatrix(m,nrl,nrh,ncl,nch)
      int **m;
      long nch,ncl,nrh,nrl; 
     /* free an int matrix allocated by imatrix() */ 
{ 
  free((FREE_ARG) (m[nrl]+ncl-NR_END)); 
  free((FREE_ARG) (m+nrl-NR_END)); 
} 

/******************* matrix *******************************/
double **matrix(long nrl, long nrh, long ncl, long nch)
{
  long i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
  double **m;

  m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
  if (!m) nrerror("allocation failure 1 in matrix()");
  m += NR_END;
  m -= nrl;

  m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
  if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
  m[nrl] += NR_END;
  m[nrl] -= ncl;

  for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
  return m;
}

/*************************free matrix ************************/
void free_matrix(double **m, long nrl, long nrh, long ncl, long nch)
{
  free((FREE_ARG)(m[nrl]+ncl-NR_END));
  free((FREE_ARG)(m+nrl-NR_END));
}

/******************* ma3x *******************************/
double ***ma3x(long nrl, long nrh, long ncl, long nch, long nll, long nlh)
{
  long i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1, nlay=nlh-nll+1;
  double ***m;

  m=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
  if (!m) nrerror("allocation failure 1 in matrix()");
  m += NR_END;
  m -= nrl;

  m[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
  if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
  m[nrl] += NR_END;
  m[nrl] -= ncl;

  for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;

  m[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*nlay+NR_END)*sizeof(double)));
  if (!m[nrl][ncl]) nrerror("allocation failure 3 in matrix()");
  m[nrl][ncl] += NR_END;
  m[nrl][ncl] -= nll;
  for (j=ncl+1; j<=nch; j++) 
    m[nrl][j]=m[nrl][j-1]+nlay;
  
  for (i=nrl+1; i<=nrh; i++) {
    m[i][ncl]=m[i-1l][ncl]+ncol*nlay;
    for (j=ncl+1; j<=nch; j++) 
      m[i][j]=m[i][j-1]+nlay;
  }
  return m;
}

/*************************free ma3x ************************/
void free_ma3x(double ***m, long nrl, long nrh, long ncl, long nch,long nll, long nlh)
{
  free((FREE_ARG)(m[nrl][ncl]+ nll-NR_END));
  free((FREE_ARG)(m[nrl]+ncl-NR_END));
  free((FREE_ARG)(m+nrl-NR_END));
}

/***************** f1dim *************************/
extern int ncom; 
extern double *pcom,*xicom;
extern double (*nrfunc)(double []); 
 
double f1dim(double x) 
{ 
  int j; 
  double f;
  double *xt; 
 
  xt=vector(1,ncom); 
  for (j=1;j<=ncom;j++) xt[j]=pcom[j]+x*xicom[j]; 
  f=(*nrfunc)(xt); 
  free_vector(xt,1,ncom); 
  return f; 
} 

/*****************brent *************************/
double brent(double ax, double bx, double cx, double (*f)(double), double tol, 	double *xmin) 
{ 
  int iter; 
  double a,b,d,etemp;
  double fu,fv,fw,fx;
  double ftemp;
  double p,q,r,tol1,tol2,u,v,w,x,xm; 
  double e=0.0; 
 
  a=(ax < cx ? ax : cx); 
  b=(ax > cx ? ax : cx); 
  x=w=v=bx; 
  fw=fv=fx=(*f)(x); 
  for (iter=1;iter<=ITMAX;iter++) { 
    xm=0.5*(a+b); 
    tol2=2.0*(tol1=tol*fabs(x)+ZEPS); 
    /*		if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret)))*/
    printf(".");fflush(stdout);
#ifdef DEBUG
    printf("br %d,x=%.10e xm=%.10e b=%.10e a=%.10e tol=%.10e tol1=%.10e tol2=%.10e x-xm=%.10e fx=%.12e fu=%.12e,fw=%.12e,ftemp=%.12e,ftol=%.12e\n",iter,x,xm,b,a,tol,tol1,tol2,(x-xm),fx,fu,fw,ftemp,ftol);
    /*		if ((fabs(x-xm) <= (tol2-0.5*(b-a)))||(2.0*fabs(fu-ftemp) <= ftol*1.e-2*(fabs(fu)+fabs(ftemp)))) { */
#endif
    if (fabs(x-xm) <= (tol2-0.5*(b-a))){ 
      *xmin=x; 
      return fx; 
    } 
    ftemp=fu;
    if (fabs(e) > tol1) { 
      r=(x-w)*(fx-fv); 
      q=(x-v)*(fx-fw); 
      p=(x-v)*q-(x-w)*r; 
      q=2.0*(q-r); 
      if (q > 0.0) p = -p; 
      q=fabs(q); 
      etemp=e; 
      e=d; 
      if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x)) 
	d=CGOLD*(e=(x >= xm ? a-x : b-x)); 
      else { 
	d=p/q; 
	u=x+d; 
	if (u-a < tol2 || b-u < tol2) 
	  d=SIGN(tol1,xm-x); 
      } 
    } else { 
      d=CGOLD*(e=(x >= xm ? a-x : b-x)); 
    } 
    u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d)); 
    fu=(*f)(u); 
    if (fu <= fx) { 
      if (u >= x) a=x; else b=x; 
      SHFT(v,w,x,u) 
	SHFT(fv,fw,fx,fu) 
	} else { 
	  if (u < x) a=u; else b=u; 
	  if (fu <= fw || w == x) { 
	    v=w; 
	    w=u; 
	    fv=fw; 
	    fw=fu; 
	  } else if (fu <= fv || v == x || v == w) { 
	    v=u; 
	    fv=fu; 
	  } 
	} 
  } 
  nrerror("Too many iterations in brent"); 
  *xmin=x; 
  return fx; 
} 

/****************** mnbrak ***********************/

void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, double *fc, 
	    double (*func)(double)) 
{ 
  double ulim,u,r,q, dum;
  double fu; 
 
  *fa=(*func)(*ax); 
  *fb=(*func)(*bx); 
  if (*fb > *fa) { 
    SHFT(dum,*ax,*bx,dum) 
      SHFT(dum,*fb,*fa,dum) 
      } 
  *cx=(*bx)+GOLD*(*bx-*ax); 
  *fc=(*func)(*cx); 
  while (*fb > *fc) { 
    r=(*bx-*ax)*(*fb-*fc); 
    q=(*bx-*cx)*(*fb-*fa); 
    u=(*bx)-((*bx-*cx)*q-(*bx-*ax)*r)/ 
      (2.0*SIGN(FMAX(fabs(q-r),TINY),q-r)); 
    ulim=(*bx)+GLIMIT*(*cx-*bx); 
    if ((*bx-u)*(u-*cx) > 0.0) { 
      fu=(*func)(u); 
    } else if ((*cx-u)*(u-ulim) > 0.0) { 
      fu=(*func)(u); 
      if (fu < *fc) { 
	SHFT(*bx,*cx,u,*cx+GOLD*(*cx-*bx)) 
	  SHFT(*fb,*fc,fu,(*func)(u)) 
	  } 
    } else if ((u-ulim)*(ulim-*cx) >= 0.0) { 
      u=ulim; 
      fu=(*func)(u); 
    } else { 
      u=(*cx)+GOLD*(*cx-*bx); 
      fu=(*func)(u); 
    } 
    SHFT(*ax,*bx,*cx,u) 
      SHFT(*fa,*fb,*fc,fu) 
      } 
} 

/*************** linmin ************************/

int ncom; 
double *pcom,*xicom;
double (*nrfunc)(double []); 
 
void linmin(double p[], double xi[], int n, double *fret,double (*func)(double [])) 
{ 
  double brent(double ax, double bx, double cx, 
	       double (*f)(double), double tol, double *xmin); 
  double f1dim(double x); 
  void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, 
	      double *fc, double (*func)(double)); 
  int j; 
  double xx,xmin,bx,ax; 
  double fx,fb,fa;
 
  ncom=n; 
  pcom=vector(1,n); 
  xicom=vector(1,n); 
  nrfunc=func; 
  for (j=1;j<=n;j++) { 
    pcom[j]=p[j]; 
    xicom[j]=xi[j]; 
  } 
  ax=0.0; 
  xx=1.0; 
  mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim); 
  *fret=brent(ax,xx,bx,f1dim,TOL,&xmin); 
#ifdef DEBUG
  printf("retour brent fret=%.12e xmin=%.12e\n",*fret,xmin);
#endif
  for (j=1;j<=n;j++) { 
    xi[j] *= xmin; 
    p[j] += xi[j]; 
  } 
  free_vector(xicom,1,n); 
  free_vector(pcom,1,n); 
} 

/*************** powell ************************/
void powell(double p[], double **xi, int n, double ftol, int *iter, double *fret, 
	    double (*func)(double [])) 
{ 
  void linmin(double p[], double xi[], int n, double *fret, 
	      double (*func)(double [])); 
  int i,ibig,j; 
  double del,t,*pt,*ptt,*xit;
  double fp,fptt;
  double *xits;
  pt=vector(1,n); 
  ptt=vector(1,n); 
  xit=vector(1,n); 
  xits=vector(1,n); 
  *fret=(*func)(p); 
  for (j=1;j<=n;j++) pt[j]=p[j]; 
  for (*iter=1;;++(*iter)) { 
    fp=(*fret); 
    ibig=0; 
    del=0.0; 
    printf("\nPowell iter=%d -2*LL=%.12f",*iter,*fret);
    for (i=1;i<=n;i++) 
      printf(" %d %.12f",i, p[i]);
    printf("\n");
    for (i=1;i<=n;i++) { 
      for (j=1;j<=n;j++) xit[j]=xi[j][i]; 
      fptt=(*fret); 
#ifdef DEBUG
      printf("fret=%lf \n",*fret);
#endif
      printf("%d",i);fflush(stdout);
      linmin(p,xit,n,fret,func); 
      if (fabs(fptt-(*fret)) > del) { 
	del=fabs(fptt-(*fret)); 
	ibig=i; 
      } 
#ifdef DEBUG
      printf("%d %.12e",i,(*fret));
      for (j=1;j<=n;j++) {
	xits[j]=FMAX(fabs(p[j]-pt[j]),1.e-5);
	printf(" x(%d)=%.12e",j,xit[j]);
      }
      for(j=1;j<=n;j++) 
	printf(" p=%.12e",p[j]);
      printf("\n");
#endif
    } 
    if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret))) {
#ifdef DEBUG
      int k[2],l;
      k[0]=1;
      k[1]=-1;
      printf("Max: %.12e",(*func)(p));
      for (j=1;j<=n;j++) 
	printf(" %.12e",p[j]);
      printf("\n");
      for(l=0;l<=1;l++) {
	for (j=1;j<=n;j++) {
	  ptt[j]=p[j]+(p[j]-pt[j])*k[l];
	  printf("l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
	}
	printf("func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
      }
#endif


      free_vector(xit,1,n); 
      free_vector(xits,1,n); 
      free_vector(ptt,1,n); 
      free_vector(pt,1,n); 
      return; 
    } 
    if (*iter == ITMAX) nrerror("powell exceeding maximum iterations."); 
    for (j=1;j<=n;j++) { 
      ptt[j]=2.0*p[j]-pt[j]; 
      xit[j]=p[j]-pt[j]; 
      pt[j]=p[j]; 
    } 
    fptt=(*func)(ptt); 
    if (fptt < fp) { 
      t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)-del*SQR(fp-fptt); 
      if (t < 0.0) { 
	linmin(p,xit,n,fret,func); 
	for (j=1;j<=n;j++) { 
	  xi[j][ibig]=xi[j][n]; 
	  xi[j][n]=xit[j]; 
	}
#ifdef DEBUG
	printf("Direction changed  last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
	for(j=1;j<=n;j++)
	  printf(" %.12e",xit[j]);
	printf("\n");
#endif
      } 
    } 
  } 
} 

/**** Prevalence limit ****************/

double **prevalim(double **prlim, int nlstate, double x[], double age, double **oldm, double **savm, double ftolpl, int ij)
{
  /* Computes the prevalence limit in each live state at age x by left multiplying the unit
     matrix by transitions matrix until convergence is reached */

  int i, ii,j,k;
  double min, max, maxmin, maxmax,sumnew=0.;
  double **matprod2();
  double **out, cov[NCOVMAX], **pmij();
  double **newm;
  double agefin, delaymax=50 ; /* Max number of years to converge */

  for (ii=1;ii<=nlstate+ndeath;ii++)
    for (j=1;j<=nlstate+ndeath;j++){
      oldm[ii][j]=(ii==j ? 1.0 : 0.0);
    }

   cov[1]=1.;
 
 /* Even if hstepm = 1, at least one multiplication by the unit matrix */
  for(agefin=age-stepm/YEARM; agefin>=age-delaymax; agefin=agefin-stepm/YEARM){
    newm=savm;
    /* Covariates have to be included here again */
     cov[2]=agefin;
  
      for (k=1; k<=cptcovn;k++) {
	cov[2+k]=nbcode[Tvar[k]][codtab[ij][Tvar[k]]];
	/*printf("ij=%d Tvar[k]=%d nbcode=%d cov=%lf\n",ij, Tvar[k],nbcode[Tvar[k]][codtab[ij][Tvar[k]]],cov[2+k]);*/
      }
      for (k=1; k<=cptcovage;k++)
	cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2];
      for (k=1; k<=cptcovprod;k++)
	cov[2+Tprod[k]]=nbcode[Tvard[k][1]][codtab[ij][Tvard[k][1]]]*nbcode[Tvard[k][2]][codtab[ij][Tvard[k][2]]];

      /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
      /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/

    out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm);

    savm=oldm;
    oldm=newm;
    maxmax=0.;
    for(j=1;j<=nlstate;j++){
      min=1.;
      max=0.;
      for(i=1; i<=nlstate; i++) {
	sumnew=0;
	for(k=1; k<=ndeath; k++) sumnew+=newm[i][nlstate+k];
	prlim[i][j]= newm[i][j]/(1-sumnew);
	max=FMAX(max,prlim[i][j]);
	min=FMIN(min,prlim[i][j]);
      }
      maxmin=max-min;
      maxmax=FMAX(maxmax,maxmin);
    }
    if(maxmax < ftolpl){
      return prlim;
    }
  }
}

/*************** transition probabilities **********/ 

double **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
{
  double s1, s2;
  /*double t34;*/
  int i,j,j1, nc, ii, jj;

    for(i=1; i<= nlstate; i++){
    for(j=1; j<i;j++){
      for (nc=1, s2=0.;nc <=ncovmodel; nc++){
	/*s2 += param[i][j][nc]*cov[nc];*/
	s2 += x[(i-1)*nlstate*ncovmodel+(j-1)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];
	/*printf("Int j<i s1=%.17e, s2=%.17e\n",s1,s2);*/
      }
      ps[i][j]=s2;
      /*printf("s1=%.17e, s2=%.17e\n",s1,s2);*/
    }
    for(j=i+1; j<=nlstate+ndeath;j++){
      for (nc=1, s2=0.;nc <=ncovmodel; nc++){
	s2 += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];
	/*printf("Int j>i s1=%.17e, s2=%.17e %lx %lx\n",s1,s2,s1,s2);*/
      }
      ps[i][j]=s2;
    }
  }
  for(i=1; i<= nlstate; i++){
     s1=0;
    for(j=1; j<i; j++)
      s1+=exp(ps[i][j]);
    for(j=i+1; j<=nlstate+ndeath; j++)
      s1+=exp(ps[i][j]);
    ps[i][i]=1./(s1+1.);
    for(j=1; j<i; j++)
      ps[i][j]= exp(ps[i][j])*ps[i][i];
    for(j=i+1; j<=nlstate+ndeath; j++)
      ps[i][j]= exp(ps[i][j])*ps[i][i];
    /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
  } /* end i */

  for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
    for(jj=1; jj<= nlstate+ndeath; jj++){
      ps[ii][jj]=0;
      ps[ii][ii]=1;
    }
  }

  /*   for(ii=1; ii<= nlstate+ndeath; ii++){
    for(jj=1; jj<= nlstate+ndeath; jj++){
     printf("%lf ",ps[ii][jj]);
   }
    printf("\n ");
    }
    printf("\n ");printf("%lf ",cov[2]);*/
/*
  for(i=1; i<= npar; i++) printf("%f ",x[i]);
  goto end;*/
    return ps;
}

/**************** Product of 2 matrices ******************/

double **matprod2(double **out, double **in,long nrl, long nrh, long ncl, long nch, long ncolol, long ncoloh, double **b)
{
  /* Computes the matric product of in(1,nrh-nrl+1)(1,nch-ncl+1) times
     b(1,nch-ncl+1)(1,ncoloh-ncolol+1) into out(...) */
  /* in, b, out are matrice of pointers which should have been initialized 
     before: only the contents of out is modified. The function returns
     a pointer to pointers identical to out */
  long i, j, k;
  for(i=nrl; i<= nrh; i++)
    for(k=ncolol; k<=ncoloh; k++)
      for(j=ncl,out[i][k]=0.; j<=nch; j++)
	out[i][k] +=in[i][j]*b[j][k];

  return out;
}


/************* Higher Matrix Product ***************/

double ***hpxij(double ***po, int nhstepm, double age, int hstepm, double *x, int nlstate, int stepm, double **oldm, double **savm, int ij )
{
  /* Computes the transition matrix starting at age 'age' over 'nhstepm*hstepm*stepm' month 
     duration (i.e. until
     age (in years)  age+nhstepm*stepm/12) by multiplying nhstepm*hstepm matrices. 
     Output is stored in matrix po[i][j][h] for h every 'hstepm' step 
     (typically every 2 years instead of every month which is too big).
     Model is determined by parameters x and covariates have to be 
     included manually here. 

     */

  int i, j, d, h, k;
  double **out, cov[NCOVMAX];
  double **newm;

  /* Hstepm could be zero and should return the unit matrix */
  for (i=1;i<=nlstate+ndeath;i++)
    for (j=1;j<=nlstate+ndeath;j++){
      oldm[i][j]=(i==j ? 1.0 : 0.0);
      po[i][j][0]=(i==j ? 1.0 : 0.0);
    }
  /* Even if hstepm = 1, at least one multiplication by the unit matrix */
  for(h=1; h <=nhstepm; h++){
    for(d=1; d <=hstepm; d++){
      newm=savm;
      /* Covariates have to be included here again */
      cov[1]=1.;
      cov[2]=age+((h-1)*hstepm + (d-1))*stepm/YEARM;
      for (k=1; k<=cptcovn;k++) cov[2+k]=nbcode[Tvar[k]][codtab[ij][Tvar[k]]];
for (k=1; k<=cptcovage;k++)
	cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2];
   for (k=1; k<=cptcovprod;k++)
	cov[2+Tprod[k]]=nbcode[Tvard[k][1]][codtab[ij][Tvard[k][1]]]*nbcode[Tvard[k][2]][codtab[ij][Tvard[k][2]]];


      /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
      /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
      out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, 
		   pmij(pmmij,cov,ncovmodel,x,nlstate));
      savm=oldm;
      oldm=newm;
    }
    for(i=1; i<=nlstate+ndeath; i++)
      for(j=1;j<=nlstate+ndeath;j++) {
	po[i][j][h]=newm[i][j];
	/*printf("i=%d j=%d h=%d po[i][j][h]=%f ",i,j,h,po[i][j][h]);
	 */
      }
  } /* end h */
  return po;
}


/*************** log-likelihood *************/
double func( double *x)
{
  int i, ii, j, k, mi, d, kk;
  double l, ll[NLSTATEMAX], cov[NCOVMAX];
  double **out;
  double sw; /* Sum of weights */
  double lli; /* Individual log likelihood */
  long ipmx;
  /*extern weight */
  /* We are differentiating ll according to initial status */
  /*  for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
  /*for(i=1;i<imx;i++) 
    printf(" %d\n",s[4][i]);
  */
  cov[1]=1.;

  for(k=1; k<=nlstate; k++) ll[k]=0.;
  for (i=1,ipmx=0, sw=0.; i<=imx; i++){
    for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i];
    for(mi=1; mi<= wav[i]-1; mi++){
      for (ii=1;ii<=nlstate+ndeath;ii++)
	for (j=1;j<=nlstate+ndeath;j++) oldm[ii][j]=(ii==j ? 1.0 : 0.0);
      for(d=0; d<dh[mi][i]; d++){
	newm=savm;
	cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM;
	for (kk=1; kk<=cptcovage;kk++) {
	  cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
	}
	
	out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
		     1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
	savm=oldm;
	oldm=newm;
	
	
      } /* end mult */
      
      lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);
      /* printf(" %f ",out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/
      ipmx +=1;
      sw += weight[i];
      ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
    } /* end of wave */
  } /* end of individual */

  for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
  /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
  l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
  return -l;
}


/*********** Maximum Likelihood Estimation ***************/

void mlikeli(FILE *ficres,double p[], int npar, int ncovmodel, int nlstate, double ftol, double (*func)(double []))
{
  int i,j, iter;
  double **xi,*delti;
  double fret;
  xi=matrix(1,npar,1,npar);
  for (i=1;i<=npar;i++)
    for (j=1;j<=npar;j++)
      xi[i][j]=(i==j ? 1.0 : 0.0);
  printf("Powell\n");
  powell(p,xi,npar,ftol,&iter,&fret,func);

   printf("\n#Number of iterations = %d, -2 Log likelihood = %.12f\n",iter,func(p));
  fprintf(ficres,"#Number of iterations = %d, -2 Log likelihood = %.12f ",iter,func(p));

}

/**** Computes Hessian and covariance matrix ***/
void hesscov(double **matcov, double p[], int npar, double delti[], double ftolhess, double (*func)(double []))
{
  double  **a,**y,*x,pd;
  double **hess;
  int i, j,jk;
  int *indx;

  double hessii(double p[], double delta, int theta, double delti[]);
  double hessij(double p[], double delti[], int i, int j);
  void lubksb(double **a, int npar, int *indx, double b[]) ;
  void ludcmp(double **a, int npar, int *indx, double *d) ;


  hess=matrix(1,npar,1,npar);

  printf("\nCalculation of the hessian matrix. Wait...\n");
  for (i=1;i<=npar;i++){
    printf("%d",i);fflush(stdout);
    hess[i][i]=hessii(p,ftolhess,i,delti);
    /*printf(" %f ",p[i]);*/
  }

  for (i=1;i<=npar;i++) {
    for (j=1;j<=npar;j++)  {
      if (j>i) { 
	printf(".%d%d",i,j);fflush(stdout);
	hess[i][j]=hessij(p,delti,i,j);
	hess[j][i]=hess[i][j];
      }
    }
  }
  printf("\n");

  printf("\nInverting the hessian to get the covariance matrix. Wait...\n");
  
  a=matrix(1,npar,1,npar);
  y=matrix(1,npar,1,npar);
  x=vector(1,npar);
  indx=ivector(1,npar);
  for (i=1;i<=npar;i++)
    for (j=1;j<=npar;j++) a[i][j]=hess[i][j];
  ludcmp(a,npar,indx,&pd);

  for (j=1;j<=npar;j++) {
    for (i=1;i<=npar;i++) x[i]=0;
    x[j]=1;
    lubksb(a,npar,indx,x);
    for (i=1;i<=npar;i++){ 
      matcov[i][j]=x[i];
    }
  }

  printf("\n#Hessian matrix#\n");
  for (i=1;i<=npar;i++) { 
    for (j=1;j<=npar;j++) { 
      printf("%.3e ",hess[i][j]);
    }
    printf("\n");
  }

  /* Recompute Inverse */
  for (i=1;i<=npar;i++)
    for (j=1;j<=npar;j++) a[i][j]=matcov[i][j];
  ludcmp(a,npar,indx,&pd);

  /*  printf("\n#Hessian matrix recomputed#\n");

  for (j=1;j<=npar;j++) {
    for (i=1;i<=npar;i++) x[i]=0;
    x[j]=1;
    lubksb(a,npar,indx,x);
    for (i=1;i<=npar;i++){ 
      y[i][j]=x[i];
      printf("%.3e ",y[i][j]);
    }
    printf("\n");
  }
  */

  free_matrix(a,1,npar,1,npar);
  free_matrix(y,1,npar,1,npar);
  free_vector(x,1,npar);
  free_ivector(indx,1,npar);
  free_matrix(hess,1,npar,1,npar);


}

/*************** hessian matrix ****************/
double hessii( double x[], double delta, int theta, double delti[])
{
  int i;
  int l=1, lmax=20;
  double k1,k2;
  double p2[NPARMAX+1];
  double res;
  double delt, delts, nkhi=10.,nkhif=1., khi=1.e-4;
  double fx;
  int k=0,kmax=10;
  double l1;

  fx=func(x);
  for (i=1;i<=npar;i++) p2[i]=x[i];
  for(l=0 ; l <=lmax; l++){
    l1=pow(10,l);
    delts=delt;
    for(k=1 ; k <kmax; k=k+1){
      delt = delta*(l1*k);
      p2[theta]=x[theta] +delt;
      k1=func(p2)-fx;
      p2[theta]=x[theta]-delt;
      k2=func(p2)-fx;
      /*res= (k1-2.0*fx+k2)/delt/delt; */
      res= (k1+k2)/delt/delt/2.; /* Divided by because L and not 2*L */
      
#ifdef DEBUG
      printf("%d %d k1=%.12e k2=%.12e xk1=%.12e xk2=%.12e delt=%.12e res=%.12e l=%d k=%d,fx=%.12e\n",theta,theta,k1,k2,x[theta]+delt,x[theta]-delt,delt,res, l, k,fx);
#endif
      /*if(fabs(k1-2.0*fx+k2) <1.e-13){ */
      if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)){
	k=kmax;
      }
      else if((k1 >khi/nkhif) || (k2 >khi/nkhif)){ /* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. */
	k=kmax; l=lmax*10.;
      }
      else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){ 
	delts=delt;
      }
    }
  }
  delti[theta]=delts;
  return res;
  
}

double hessij( double x[], double delti[], int thetai,int thetaj)
{
  int i;
  int l=1, l1, lmax=20;
  double k1,k2,k3,k4,res,fx;
  double p2[NPARMAX+1];
  int k;

  fx=func(x);
  for (k=1; k<=2; k++) {
    for (i=1;i<=npar;i++) p2[i]=x[i];
    p2[thetai]=x[thetai]+delti[thetai]/k;
    p2[thetaj]=x[thetaj]+delti[thetaj]/k;
    k1=func(p2)-fx;
  
    p2[thetai]=x[thetai]+delti[thetai]/k;
    p2[thetaj]=x[thetaj]-delti[thetaj]/k;
    k2=func(p2)-fx;
  
    p2[thetai]=x[thetai]-delti[thetai]/k;
    p2[thetaj]=x[thetaj]+delti[thetaj]/k;
    k3=func(p2)-fx;
  
    p2[thetai]=x[thetai]-delti[thetai]/k;
    p2[thetaj]=x[thetaj]-delti[thetaj]/k;
    k4=func(p2)-fx;
    res=(k1-k2-k3+k4)/4.0/delti[thetai]*k/delti[thetaj]*k/2.; /* Because of L not 2*L */
#ifdef DEBUG
    printf("%d %d k=%d, k1=%.12e k2=%.12e k3=%.12e k4=%.12e delti/k=%.12e deltj/k=%.12e, xi-de/k=%.12e xj-de/k=%.12e  res=%.12e k1234=%.12e,k1-2=%.12e,k3-4=%.12e\n",thetai,thetaj,k,k1,k2,k3,k4,delti[thetai]/k,delti[thetaj]/k,x[thetai]-delti[thetai]/k,x[thetaj]-delti[thetaj]/k, res,k1-k2-k3+k4,k1-k2,k3-k4);
#endif
  }
  return res;
}

/************** Inverse of matrix **************/
void ludcmp(double **a, int n, int *indx, double *d) 
{ 
  int i,imax,j,k; 
  double big,dum,sum,temp; 
  double *vv; 
 
  vv=vector(1,n); 
  *d=1.0; 
  for (i=1;i<=n;i++) { 
    big=0.0; 
    for (j=1;j<=n;j++) 
      if ((temp=fabs(a[i][j])) > big) big=temp; 
    if (big == 0.0) nrerror("Singular matrix in routine ludcmp"); 
    vv[i]=1.0/big; 
  } 
  for (j=1;j<=n;j++) { 
    for (i=1;i<j;i++) { 
      sum=a[i][j]; 
      for (k=1;k<i;k++) sum -= a[i][k]*a[k][j]; 
      a[i][j]=sum; 
    } 
    big=0.0; 
    for (i=j;i<=n;i++) { 
      sum=a[i][j]; 
      for (k=1;k<j;k++) 
	sum -= a[i][k]*a[k][j]; 
      a[i][j]=sum; 
      if ( (dum=vv[i]*fabs(sum)) >= big) { 
	big=dum; 
	imax=i; 
      } 
    } 
    if (j != imax) { 
      for (k=1;k<=n;k++) { 
	dum=a[imax][k]; 
	a[imax][k]=a[j][k]; 
	a[j][k]=dum; 
      } 
      *d = -(*d); 
      vv[imax]=vv[j]; 
    } 
    indx[j]=imax; 
    if (a[j][j] == 0.0) a[j][j]=TINY; 
    if (j != n) { 
      dum=1.0/(a[j][j]); 
      for (i=j+1;i<=n;i++) a[i][j] *= dum; 
    } 
  } 
  free_vector(vv,1,n);  /* Doesn't work */
;
} 

void lubksb(double **a, int n, int *indx, double b[]) 
{ 
  int i,ii=0,ip,j; 
  double sum; 
 
  for (i=1;i<=n;i++) { 
    ip=indx[i]; 
    sum=b[ip]; 
    b[ip]=b[i]; 
    if (ii) 
      for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j]; 
    else if (sum) ii=i; 
    b[i]=sum; 
  } 
  for (i=n;i>=1;i--) { 
    sum=b[i]; 
    for (j=i+1;j<=n;j++) sum -= a[i][j]*b[j]; 
    b[i]=sum/a[i][i]; 
  } 
} 

/************ Frequencies ********************/
void  freqsummary(char fileres[], int agemin, int agemax, int **s, double **agev, int nlstate, int imx, int *Tvar, int **nbcode, int *ncodemax)
{  /* Some frequencies */
 
  int i, m, jk, k1, i1, j1, bool, z1,z2,j;
  double ***freq; /* Frequencies */
  double *pp;
  double pos;
  FILE *ficresp;
  char fileresp[FILENAMELENGTH];

  pp=vector(1,nlstate);

  strcpy(fileresp,"p");
  strcat(fileresp,fileres);
  if((ficresp=fopen(fileresp,"w"))==NULL) {
    printf("Problem with prevalence resultfile: %s\n", fileresp);
    exit(0);
  }
  freq= ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,agemin,agemax+3);
  j1=0;

  j=cptcoveff;
  if (cptcovn<1) {j=1;ncodemax[1]=1;}

  for(k1=1; k1<=j;k1++){
   for(i1=1; i1<=ncodemax[k1];i1++){
       j1++;
       /*printf("cptcoveff=%d Tvaraff=%d", cptcoveff,Tvaraff[1]);
	 scanf("%d", i);*/
        for (i=-1; i<=nlstate+ndeath; i++)  
	 for (jk=-1; jk<=nlstate+ndeath; jk++)  
	   for(m=agemin; m <= agemax+3; m++)
	     freq[i][jk][m]=0;
       
       for (i=1; i<=imx; i++) {
	 bool=1;
	 if  (cptcovn>0) {
	   for (z1=1; z1<=cptcoveff; z1++) 
	     if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtab[j1][z1]]) 
	       bool=0;
	 }
	  if (bool==1) {
	   for(m=firstpass; m<=lastpass-1; m++){
	     if(agev[m][i]==0) agev[m][i]=agemax+1;
	     if(agev[m][i]==1) agev[m][i]=agemax+2;
	     freq[s[m][i]][s[m+1][i]][(int)agev[m][i]] += weight[i];
	     freq[s[m][i]][s[m+1][i]][(int) agemax+3] += weight[i];
	   }
	 }
       }
        if  (cptcovn>0) {
	 fprintf(ficresp, "\n#********** Variable "); 
	 for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresp, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
       fprintf(ficresp, "**********\n#");
	}
       for(i=1; i<=nlstate;i++) 
	 fprintf(ficresp, " Age Prev(%d) N(%d) N",i,i);
       fprintf(ficresp, "\n");
       
  for(i=(int)agemin; i <= (int)agemax+3; i++){
    if(i==(int)agemax+3)
      printf("Total");
    else
      printf("Age %d", i);
    for(jk=1; jk <=nlstate ; jk++){
      for(m=-1, pp[jk]=0; m <=nlstate+ndeath ; m++)
	pp[jk] += freq[jk][m][i];
    }
    for(jk=1; jk <=nlstate ; jk++){
      for(m=-1, pos=0; m <=0 ; m++)
	pos += freq[jk][m][i];
      if(pp[jk]>=1.e-10)
	printf(" %d.=%.0f loss[%d]=%.1f%%",jk,pp[jk],jk,100*pos/pp[jk]);
      else
        printf(" %d.=%.0f loss[%d]=NaNQ%%",jk,pp[jk],jk);
    }
    for(jk=1; jk <=nlstate ; jk++){
      for(m=1, pp[jk]=0; m <=nlstate+ndeath; m++)
	pp[jk] += freq[jk][m][i];
    }
    for(jk=1,pos=0; jk <=nlstate ; jk++)
      pos += pp[jk];
    for(jk=1; jk <=nlstate ; jk++){
      if(pos>=1.e-5)
	printf(" %d.=%.0f prev[%d]=%.1f%%",jk,pp[jk],jk,100*pp[jk]/pos);
      else
	printf(" %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk);
      if( i <= (int) agemax){
	if(pos>=1.e-5)
	  fprintf(ficresp," %d %.5f %.0f %.0f",i,pp[jk]/pos, pp[jk],pos);
      else
	  fprintf(ficresp," %d NaNq %.0f %.0f",i,pp[jk],pos);
      }
    }
    for(jk=-1; jk <=nlstate+ndeath; jk++)
      for(m=-1; m <=nlstate+ndeath; m++)
	if(freq[jk][m][i] !=0 ) printf(" %d%d=%.0f",jk,m,freq[jk][m][i]);
    if(i <= (int) agemax)
      fprintf(ficresp,"\n");
    printf("\n");
    }
    }
 }
 
  fclose(ficresp);
  free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath,(int) agemin,(int) agemax+3);
  free_vector(pp,1,nlstate);

}  /* End of Freq */

/************* Waves Concatenation ***************/

void  concatwav(int wav[], int **dh, int **mw, int **s, double *agedc, double **agev, int  firstpass, int lastpass, int imx, int nlstate, int stepm)
{
  /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
     Death is a valid wave (if date is known).
     mw[mi][i] is the mi (mi=1 to wav[i])  effective wave of individual i
     dh[m][i] of dh[mw[mi][i][i] is the delay between two effective waves m=mw[mi][i]
     and mw[mi+1][i]. dh depends on stepm.
     */

  int i, mi, m;
  /* int j, k=0,jk, ju, jl,jmin=1e+5, jmax=-1;
     double sum=0., jmean=0.;*/

  int j, k=0,jk, ju, jl;
  double sum=0.;
  jmin=1e+5;
  jmax=-1;
  jmean=0.;
  for(i=1; i<=imx; i++){
    mi=0;
    m=firstpass;
    while(s[m][i] <= nlstate){
      if(s[m][i]>=1)
	mw[++mi][i]=m;
      if(m >=lastpass)
	break;
      else
	m++;
    }/* end while */
    if (s[m][i] > nlstate){
      mi++;	/* Death is another wave */
      /* if(mi==0)  never been interviewed correctly before death */
	 /* Only death is a correct wave */
      mw[mi][i]=m;
    }

    wav[i]=mi;
    if(mi==0)
      printf("Warning, no any valid information for:%d line=%d\n",num[i],i);
  }

  for(i=1; i<=imx; i++){
    for(mi=1; mi<wav[i];mi++){
      if (stepm <=0)
	dh[mi][i]=1;
      else{
	if (s[mw[mi+1][i]][i] > nlstate) {
	  if (agedc[i] < 2*AGESUP) {
	  j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12); 
	  if(j==0) j=1;  /* Survives at least one month after exam */
	  k=k+1;
	  if (j >= jmax) jmax=j;
	  if (j <= jmin) jmin=j;
	  sum=sum+j;
	  if (j<0) printf("j=%d num=%d ",j,i); 
	  }
	}
	else{
	  j= rint( (agev[mw[mi+1][i]][i]*12 - agev[mw[mi][i]][i]*12));
	  k=k+1;
	  if (j >= jmax) jmax=j;
	  else if (j <= jmin)jmin=j;
	  sum=sum+j;
	}
	jk= j/stepm;
	jl= j -jk*stepm;
	ju= j -(jk+1)*stepm;
	if(jl <= -ju)
	  dh[mi][i]=jk;
	else
	  dh[mi][i]=jk+1;
	if(dh[mi][i]==0)
	  dh[mi][i]=1; /* At least one step */
      }
    }
  }
  jmean=sum/k;
  printf("Delay (in months) between two waves Min=%d Max=%d Mean=%f\n\n ",jmin, jmax,jmean);
}
/*********** Tricode ****************************/
void tricode(int *Tvar, int **nbcode, int imx)
{
  int Ndum[20],ij=1, k, j, i;
  int cptcode=0;
  cptcoveff=0; 
 
  for (k=0; k<19; k++) Ndum[k]=0;
  for (k=1; k<=7; k++) ncodemax[k]=0;

  for (j=1; j<=(cptcovn+2*cptcovprod); j++) {
    for (i=1; i<=imx; i++) {
      ij=(int)(covar[Tvar[j]][i]);
      Ndum[ij]++; 
      /*printf("i=%d ij=%d Ndum[ij]=%d imx=%d",i,ij,Ndum[ij],imx);*/
      if (ij > cptcode) cptcode=ij; 
    }

    for (i=0; i<=cptcode; i++) {
      if(Ndum[i]!=0) ncodemax[j]++;
    }
    ij=1; 


    for (i=1; i<=ncodemax[j]; i++) {
      for (k=0; k<=19; k++) {
	if (Ndum[k] != 0) {
	  nbcode[Tvar[j]][ij]=k; 
	  ij++;
	}
	if (ij > ncodemax[j]) break; 
      }  
    } 
  }  

 for (k=0; k<19; k++) Ndum[k]=0;

 for (i=1; i<=ncovmodel; i++) {
      ij=Tvar[i];
      Ndum[ij]++; 
    }

 ij=1;
 for (i=1; i<=10; i++) {
   if((Ndum[i]!=0) && (i<=ncov)){
     Tvaraff[ij]=i; 
     ij++;
   }
 }
 
    cptcoveff=ij-1;
}

/*********** Health Expectancies ****************/

void evsij(char fileres[], double ***eij, double x[], int nlstate, int stepm, int bage, int fage, double **oldm, double **savm, int ij)
{
  /* Health expectancies */
  int i, j, nhstepm, hstepm, h;
  double age, agelim,hf;
  double ***p3mat;
  
  fprintf(ficreseij,"# Health expectancies\n");
  fprintf(ficreseij,"# Age");
  for(i=1; i<=nlstate;i++)
    for(j=1; j<=nlstate;j++)
      fprintf(ficreseij," %1d-%1d",i,j);
  fprintf(ficreseij,"\n");

  hstepm=1*YEARM; /*  Every j years of age (in month) */
  hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */ 

  agelim=AGESUP;
  for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
    /* nhstepm age range expressed in number of stepm */
    nhstepm=(int) rint((agelim-age)*YEARM/stepm); 
    /* Typically if 20 years = 20*12/6=40 stepm */ 
    if (stepm >= YEARM) hstepm=1;
    nhstepm = nhstepm/hstepm;/* Expressed in hstepm, typically 40/4=10 */
    p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
    /* Computed by stepm unit matrices, product of hstepm matrices, stored
       in an array of nhstepm length: nhstepm=10, hstepm=4, stepm=6 months */
    hpxij(p3mat,nhstepm,age,hstepm,x,nlstate,stepm,oldm, savm, ij);  


    for(i=1; i<=nlstate;i++)
      for(j=1; j<=nlstate;j++)
	for (h=0, eij[i][j][(int)age]=0; h<=nhstepm; h++){
	  eij[i][j][(int)age] +=p3mat[i][j][h];
	}
    
    hf=1;
    if (stepm >= YEARM) hf=stepm/YEARM;
    fprintf(ficreseij,"%.0f",age );
    for(i=1; i<=nlstate;i++)
      for(j=1; j<=nlstate;j++){
	fprintf(ficreseij," %.4f", hf*eij[i][j][(int)age]);
      }
    fprintf(ficreseij,"\n");
    free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
  }
}

/************ Variance ******************/
void varevsij(char fileres[], double ***vareij, double **matcov, double x[], double delti[], int nlstate, int stepm, double bage, double fage, double **oldm, double **savm, double **prlim, double ftolpl, int ij)
{
  /* Variance of health expectancies */
  /*  double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);*/
  double **newm;
  double **dnewm,**doldm;
  int i, j, nhstepm, hstepm, h;
  int k, cptcode;
   double *xp;
  double **gp, **gm;
  double ***gradg, ***trgradg;
  double ***p3mat;
  double age,agelim;
  int theta;

   fprintf(ficresvij,"# Covariances of life expectancies\n");
  fprintf(ficresvij,"# Age");
  for(i=1; i<=nlstate;i++)
    for(j=1; j<=nlstate;j++)
      fprintf(ficresvij," Cov(e%1d, e%1d)",i,j);
  fprintf(ficresvij,"\n");

  xp=vector(1,npar);
  dnewm=matrix(1,nlstate,1,npar);
  doldm=matrix(1,nlstate,1,nlstate);
  
  hstepm=1*YEARM; /* Every year of age */
  hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */ 
  agelim = AGESUP;
  for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
    nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */ 
    if (stepm >= YEARM) hstepm=1;
    nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
    p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
    gradg=ma3x(0,nhstepm,1,npar,1,nlstate);
    gp=matrix(0,nhstepm,1,nlstate);
    gm=matrix(0,nhstepm,1,nlstate);

    for(theta=1; theta <=npar; theta++){
      for(i=1; i<=npar; i++){ /* Computes gradient */
	xp[i] = x[i] + (i==theta ?delti[theta]:0);
      }
      hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij);  
      prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ij);
      for(j=1; j<= nlstate; j++){
	for(h=0; h<=nhstepm; h++){
	  for(i=1, gp[h][j]=0.;i<=nlstate;i++)
	    gp[h][j] += prlim[i][i]*p3mat[i][j][h];
	}
      }
    
      for(i=1; i<=npar; i++) /* Computes gradient */
	xp[i] = x[i] - (i==theta ?delti[theta]:0);
      hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij);  
      prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ij);
      for(j=1; j<= nlstate; j++){
	for(h=0; h<=nhstepm; h++){
	  for(i=1, gm[h][j]=0.;i<=nlstate;i++)
	    gm[h][j] += prlim[i][i]*p3mat[i][j][h];
	}
      }
      for(j=1; j<= nlstate; j++)
	for(h=0; h<=nhstepm; h++){
	  gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta];
	}
    } /* End theta */

    trgradg =ma3x(0,nhstepm,1,nlstate,1,npar);

    for(h=0; h<=nhstepm; h++)
      for(j=1; j<=nlstate;j++)
	for(theta=1; theta <=npar; theta++)
	  trgradg[h][j][theta]=gradg[h][theta][j];

    for(i=1;i<=nlstate;i++)
      for(j=1;j<=nlstate;j++)
	vareij[i][j][(int)age] =0.;
    for(h=0;h<=nhstepm;h++){
      for(k=0;k<=nhstepm;k++){
	matprod2(dnewm,trgradg[h],1,nlstate,1,npar,1,npar,matcov);
	matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg[k]);
	for(i=1;i<=nlstate;i++)
	  for(j=1;j<=nlstate;j++)
	    vareij[i][j][(int)age] += doldm[i][j];
      }
    }
    h=1;
    if (stepm >= YEARM) h=stepm/YEARM;
    fprintf(ficresvij,"%.0f ",age );
    for(i=1; i<=nlstate;i++)
      for(j=1; j<=nlstate;j++){
	fprintf(ficresvij," %.4f", h*vareij[i][j][(int)age]);
      }
    fprintf(ficresvij,"\n");
    free_matrix(gp,0,nhstepm,1,nlstate);
    free_matrix(gm,0,nhstepm,1,nlstate);
    free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate);
    free_ma3x(trgradg,0,nhstepm,1,nlstate,1,npar);
    free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
  } /* End age */
 
  free_vector(xp,1,npar);
  free_matrix(doldm,1,nlstate,1,npar);
  free_matrix(dnewm,1,nlstate,1,nlstate);

}

/************ Variance of prevlim ******************/
void varprevlim(char fileres[], double **varpl, double **matcov, double x[], double delti[], int nlstate, int stepm, double bage, double fage, double **oldm, double **savm, double **prlim, double ftolpl, int ij)
{
  /* Variance of prevalence limit */
  /*  double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);*/
  double **newm;
  double **dnewm,**doldm;
  int i, j, nhstepm, hstepm;
  int k, cptcode;
  double *xp;
  double *gp, *gm;
  double **gradg, **trgradg;
  double age,agelim;
  int theta;
   
  fprintf(ficresvpl,"# Standard deviation of prevalences limit\n");
  fprintf(ficresvpl,"# Age");
  for(i=1; i<=nlstate;i++)
      fprintf(ficresvpl," %1d-%1d",i,i);
  fprintf(ficresvpl,"\n");

  xp=vector(1,npar);
  dnewm=matrix(1,nlstate,1,npar);
  doldm=matrix(1,nlstate,1,nlstate);
  
  hstepm=1*YEARM; /* Every year of age */
  hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */ 
  agelim = AGESUP;
  for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
    nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */ 
    if (stepm >= YEARM) hstepm=1;
    nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
    gradg=matrix(1,npar,1,nlstate);
    gp=vector(1,nlstate);
    gm=vector(1,nlstate);

    for(theta=1; theta <=npar; theta++){
      for(i=1; i<=npar; i++){ /* Computes gradient */
	xp[i] = x[i] + (i==theta ?delti[theta]:0);
      }
      prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ij);
      for(i=1;i<=nlstate;i++)
	gp[i] = prlim[i][i];
    
      for(i=1; i<=npar; i++) /* Computes gradient */
	xp[i] = x[i] - (i==theta ?delti[theta]:0);
      prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ij);
      for(i=1;i<=nlstate;i++)
	gm[i] = prlim[i][i];

      for(i=1;i<=nlstate;i++)
	gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
    } /* End theta */

    trgradg =matrix(1,nlstate,1,npar);

    for(j=1; j<=nlstate;j++)
      for(theta=1; theta <=npar; theta++)
	trgradg[j][theta]=gradg[theta][j];

    for(i=1;i<=nlstate;i++)
      varpl[i][(int)age] =0.;
    matprod2(dnewm,trgradg,1,nlstate,1,npar,1,npar,matcov);
    matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg);
    for(i=1;i<=nlstate;i++)
      varpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */

    fprintf(ficresvpl,"%.0f ",age );
    for(i=1; i<=nlstate;i++)
      fprintf(ficresvpl," %.5f (%.5f)",prlim[i][i],sqrt(varpl[i][(int)age]));
    fprintf(ficresvpl,"\n");
    free_vector(gp,1,nlstate);
    free_vector(gm,1,nlstate);
    free_matrix(gradg,1,npar,1,nlstate);
    free_matrix(trgradg,1,nlstate,1,npar);
  } /* End age */

  free_vector(xp,1,npar);
  free_matrix(doldm,1,nlstate,1,npar);
  free_matrix(dnewm,1,nlstate,1,nlstate);

}



/***********************************************/
/**************** Main Program *****************/
/***********************************************/

/*int main(int argc, char *argv[])*/
int main()
{

  int i,j, k, n=MAXN,iter,m,size,cptcode, cptcod;
  double agedeb, agefin,hf;
  double agemin=1.e20, agemax=-1.e20;

  double fret;
  double **xi,tmp,delta;

  double dum; /* Dummy variable */
  double ***p3mat;
  int *indx;
  char line[MAXLINE], linepar[MAXLINE];
  char title[MAXLINE];
  char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH],  filerespl[FILENAMELENGTH], optionfilehtm[FILENAMELENGTH];
  char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filereso[FILENAMELENGTH];
  char filerest[FILENAMELENGTH];
  char fileregp[FILENAMELENGTH];
  char path[80],pathc[80],pathcd[80],pathtot[80],model[20];
  int firstobs=1, lastobs=10;
  int sdeb, sfin; /* Status at beginning and end */
  int c,  h , cpt,l;
  int ju,jl, mi;
  int i1,j1, k1,k2,k3,jk,aa,bb, stepsize, ij;
  int jnais,jdc,jint4,jint1,jint2,jint3,**outcome,**adl,*tab;
  
  int hstepm, nhstepm;
  double bage, fage, age, agelim, agebase;
  double ftolpl=FTOL;
  double **prlim;
  double *severity;
  double ***param; /* Matrix of parameters */
  double  *p;
  double **matcov; /* Matrix of covariance */
  double ***delti3; /* Scale */
  double *delti; /* Scale */
  double ***eij, ***vareij;
  double **varpl; /* Variances of prevalence limits by age */
  double *epj, vepp;
  char version[80]="Imach version 64b, May 2001, INED-EUROREVES ";
  char *alph[]={"a","a","b","c","d","e"}, str[4];

  char z[1]="c", occ;
#include <sys/time.h>
#include <time.h>
  char stra[80], strb[80], strc[80], strd[80],stre[80],modelsav[80];
  /* long total_usecs;
  struct timeval start_time, end_time;
  
  gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */


  printf("\nIMACH, Version 0.64b");
  printf("\nEnter the parameter file name: ");

#ifdef windows
  scanf("%s",pathtot);
  getcwd(pathcd, size);
  /*cygwin_split_path(pathtot,path,optionfile);
    printf("pathtot=%s, path=%s, optionfile=%s\n",pathtot,path,optionfile);*/
  /* cutv(path,optionfile,pathtot,'\\');*/

split(pathtot, path,optionfile);
  chdir(path);
  replace(pathc,path);
#endif
#ifdef unix
  scanf("%s",optionfile);
#endif

/*-------- arguments in the command line --------*/

  strcpy(fileres,"r");
  strcat(fileres, optionfile);

  /*---------arguments file --------*/

  if((ficpar=fopen(optionfile,"r"))==NULL)    {
    printf("Problem with optionfile %s\n",optionfile);
    goto end;
  }

  strcpy(filereso,"o");
  strcat(filereso,fileres);
  if((ficparo=fopen(filereso,"w"))==NULL) {
    printf("Problem with Output resultfile: %s\n", filereso);goto end;
  }

  /* Reads comments: lines beginning with '#' */
  while((c=getc(ficpar))=='#' && c!= EOF){
    ungetc(c,ficpar);
    fgets(line, MAXLINE, ficpar);
    puts(line);
    fputs(line,ficparo);
  }
  ungetc(c,ficpar);

  fscanf(ficpar,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%lf stepm=%d ncov=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=%s\n",title, datafile, &lastobs, &firstpass,&lastpass,&ftol, &stepm, &ncov, &nlstate,&ndeath, &maxwav, &mle, &weightopt,model);
  printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncov=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=%s\n", title, datafile, lastobs, firstpass,lastpass,ftol, stepm, ncov, nlstate,ndeath, maxwav, mle, weightopt,model);
  fprintf(ficparo,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncov=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=%s\n", title, datafile, lastobs, firstpass,lastpass,ftol,stepm,ncov,nlstate,ndeath,maxwav, mle, weightopt,model);

  covar=matrix(0,NCOVMAX,1,n); 
  cptcovn=0; 
  if (strlen(model)>1) cptcovn=nbocc(model,'+')+1;

  ncovmodel=2+cptcovn;
  nvar=ncovmodel-1; /* Suppressing age as a basic covariate */
  
  /* Read guess parameters */
  /* Reads comments: lines beginning with '#' */
  while((c=getc(ficpar))=='#' && c!= EOF){
    ungetc(c,ficpar);
    fgets(line, MAXLINE, ficpar);
    puts(line);
    fputs(line,ficparo);
  }
  ungetc(c,ficpar);
  
  param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
    for(i=1; i <=nlstate; i++)
    for(j=1; j <=nlstate+ndeath-1; j++){
      fscanf(ficpar,"%1d%1d",&i1,&j1);
      fprintf(ficparo,"%1d%1d",i1,j1);
      printf("%1d%1d",i,j);
      for(k=1; k<=ncovmodel;k++){
	fscanf(ficpar," %lf",&param[i][j][k]);
	printf(" %lf",param[i][j][k]);
	fprintf(ficparo," %lf",param[i][j][k]);
      }
      fscanf(ficpar,"\n");
      printf("\n");
      fprintf(ficparo,"\n");
    }
  
  npar= (nlstate+ndeath-1)*nlstate*ncovmodel;
  p=param[1][1];
  
  /* Reads comments: lines beginning with '#' */
  while((c=getc(ficpar))=='#' && c!= EOF){
    ungetc(c,ficpar);
    fgets(line, MAXLINE, ficpar);
    puts(line);
    fputs(line,ficparo);
  }
  ungetc(c,ficpar);

  delti3= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
  delti=vector(1,npar); /* Scale of each paramater (output from hesscov) */
  for(i=1; i <=nlstate; i++){
    for(j=1; j <=nlstate+ndeath-1; j++){
      fscanf(ficpar,"%1d%1d",&i1,&j1);
      printf("%1d%1d",i,j);
      fprintf(ficparo,"%1d%1d",i1,j1);
      for(k=1; k<=ncovmodel;k++){
	fscanf(ficpar,"%le",&delti3[i][j][k]);
	printf(" %le",delti3[i][j][k]);
	fprintf(ficparo," %le",delti3[i][j][k]);
      }
      fscanf(ficpar,"\n");
      printf("\n");
      fprintf(ficparo,"\n");
    }
  }
  delti=delti3[1][1];
  
  /* Reads comments: lines beginning with '#' */
  while((c=getc(ficpar))=='#' && c!= EOF){
    ungetc(c,ficpar);
    fgets(line, MAXLINE, ficpar);
    puts(line);
    fputs(line,ficparo);
  }
  ungetc(c,ficpar);
  
  matcov=matrix(1,npar,1,npar);
  for(i=1; i <=npar; i++){
    fscanf(ficpar,"%s",&str);
    printf("%s",str);
    fprintf(ficparo,"%s",str);
    for(j=1; j <=i; j++){
      fscanf(ficpar," %le",&matcov[i][j]);
      printf(" %.5le",matcov[i][j]);
      fprintf(ficparo," %.5le",matcov[i][j]);
    }
    fscanf(ficpar,"\n");
    printf("\n");
    fprintf(ficparo,"\n");
  }
  for(i=1; i <=npar; i++)
    for(j=i+1;j<=npar;j++)
      matcov[i][j]=matcov[j][i];
   
  printf("\n");


    /*-------- data file ----------*/
    if((ficres =fopen(fileres,"w"))==NULL) {
      printf("Problem with resultfile: %s\n", fileres);goto end;
    }
    fprintf(ficres,"#%s\n",version);
    
    if((fic=fopen(datafile,"r"))==NULL)    {
      printf("Problem with datafile: %s\n", datafile);goto end;
    }

    n= lastobs;
    severity = vector(1,maxwav);
    outcome=imatrix(1,maxwav+1,1,n);
    num=ivector(1,n);
    moisnais=vector(1,n);
    annais=vector(1,n);
    moisdc=vector(1,n);
    andc=vector(1,n);
    agedc=vector(1,n);
    cod=ivector(1,n);
    weight=vector(1,n);
    for(i=1;i<=n;i++) weight[i]=1.0; /* Equal weights, 1 by default */
    mint=matrix(1,maxwav,1,n);
    anint=matrix(1,maxwav,1,n);
    s=imatrix(1,maxwav+1,1,n);
    adl=imatrix(1,maxwav+1,1,n);    
    tab=ivector(1,NCOVMAX);
    ncodemax=ivector(1,8);

    i=1; 
    while (fgets(line, MAXLINE, fic) != NULL)    {
      if ((i >= firstobs) && (i <=lastobs)) {
	
	for (j=maxwav;j>=1;j--){
	  cutv(stra, strb,line,' '); s[j][i]=atoi(strb); 
	  strcpy(line,stra);
	  cutv(stra, strb,line,'/'); anint[j][i]=(double)(atoi(strb)); strcpy(line,stra);
	  cutv(stra, strb,line,' '); mint[j][i]=(double)(atoi(strb)); strcpy(line,stra);
	}
	
	cutv(stra, strb,line,'/'); andc[i]=(double)(atoi(strb)); strcpy(line,stra);
	cutv(stra, strb,line,' '); moisdc[i]=(double)(atoi(strb)); strcpy(line,stra);

	cutv(stra, strb,line,'/'); annais[i]=(double)(atoi(strb)); strcpy(line,stra);
	cutv(stra, strb,line,' '); moisnais[i]=(double)(atoi(strb)); strcpy(line,stra);

	cutv(stra, strb,line,' '); weight[i]=(double)(atoi(strb)); strcpy(line,stra);
	for (j=ncov;j>=1;j--){
	  cutv(stra, strb,line,' '); covar[j][i]=(double)(atoi(strb)); strcpy(line,stra);
	} 
	num[i]=atol(stra);

	/*printf("%d %.lf %.lf %.lf %.lf/%.lf %.lf/%.lf %.lf/%.lf %d %.lf/%.lf %d %.lf/%.lf %d %.lf/%.lf %d\n",num[i],(covar[1][i]), (covar[2][i]), (weight[i]), (moisnais[i]), (annais[i]), (moisdc[i]), (andc[i]), (mint[1][i]), (anint[1][i]), (s[1][i]),  (mint[2][i]), (anint[2][i]), (s[2][i]),  (mint[3][i]), (anint[3][i]), (s[3][i]),  (mint[4][i]), (anint[4][i]), (s[4][i]));*/

	i=i+1;
      }
    } 

    /*scanf("%d",i);*/
  imx=i-1; /* Number of individuals */

  /* Calculation of the number of parameter from char model*/
  Tvar=ivector(1,15); 
  Tprod=ivector(1,15); 
  Tvaraff=ivector(1,15); 
  Tvard=imatrix(1,15,1,2);
  Tage=ivector(1,15);      
   
  if (strlen(model) >1){
    j=0, j1=0, k1=1, k2=1;
    j=nbocc(model,'+');
    j1=nbocc(model,'*');
    cptcovn=j+1;
    cptcovprod=j1;
    
    
    strcpy(modelsav,model); 
    if ((strcmp(model,"age")==0) || (strcmp(model,"age*age")==0)){
      printf("Error. Non available option model=%s ",model);
      goto end;
    }
    
    for(i=(j+1); i>=1;i--){
      cutv(stra,strb,modelsav,'+');
      if (nbocc(modelsav,'+')==0) strcpy(strb,modelsav); 
      /*      printf("i=%d a=%s b=%s sav=%s\n",i, stra,strb,modelsav);*/
      /*scanf("%d",i);*/
      if (strchr(strb,'*')) {
	cutv(strd,strc,strb,'*');
	if (strcmp(strc,"age")==0) {
	  cptcovprod--;
	  cutv(strb,stre,strd,'V');
	  Tvar[i]=atoi(stre);
	  cptcovage++;
	    Tage[cptcovage]=i;
	    /*printf("stre=%s ", stre);*/
	}
	else if (strcmp(strd,"age")==0) {
	  cptcovprod--;
	  cutv(strb,stre,strc,'V');
	  Tvar[i]=atoi(stre);
	  cptcovage++;
	  Tage[cptcovage]=i;
	}
	else {
	  cutv(strb,stre,strc,'V');
	  Tvar[i]=ncov+k1;
	  cutv(strb,strc,strd,'V'); 
	  Tprod[k1]=i;
	  Tvard[k1][1]=atoi(strc);
	  Tvard[k1][2]=atoi(stre);
	  Tvar[cptcovn+k2]=Tvard[k1][1];
	  Tvar[cptcovn+k2+1]=Tvard[k1][2]; 
	  for (k=1; k<=lastobs;k++) 
	    covar[ncov+k1][k]=covar[atoi(stre)][k]*covar[atoi(strc)][k];
	  k1++;
	  k2=k2+2;
	}
      }
      else {
	/*printf("d=%s c=%s b=%s\n", strd,strc,strb);*/
       /*  scanf("%d",i);*/
      cutv(strd,strc,strb,'V');
      Tvar[i]=atoi(strc);
      }
      strcpy(modelsav,stra);  
      /*printf("a=%s b=%s sav=%s\n", stra,strb,modelsav);
	scanf("%d",i);*/
    }
}
  
  /*printf("tvar1=%d tvar2=%d tvar3=%d cptcovage=%d Tage=%d",Tvar[1],Tvar[2],Tvar[3],cptcovage,Tage[1]);
  printf("cptcovprod=%d ", cptcovprod);
  scanf("%d ",i);*/
    fclose(fic);

    /*  if(mle==1){*/
    if (weightopt != 1) { /* Maximisation without weights*/
      for(i=1;i<=n;i++) weight[i]=1.0;
    }
    /*-calculation of age at interview from date of interview and age at death -*/
    agev=matrix(1,maxwav,1,imx);
    
    for (i=1; i<=imx; i++)  {
      agedc[i]=(moisdc[i]/12.+andc[i])-(moisnais[i]/12.+annais[i]);
      for(m=1; (m<= maxwav); m++){
	if(s[m][i] >0){
	  if (s[m][i] == nlstate+1) {
	    if(agedc[i]>0)
	      if(moisdc[i]!=99 && andc[i]!=9999)
	      agev[m][i]=agedc[i];
	    else {
	      if (andc[i]!=9999){
	      printf("Warning negative age at death: %d line:%d\n",num[i],i);
	      agev[m][i]=-1;
	      }
	    }
	  }
	  else if(s[m][i] !=9){ /* Should no more exist */
	    agev[m][i]=(mint[m][i]/12.+1./24.+anint[m][i])-(moisnais[i]/12.+1./24.+annais[i]);
	    if(mint[m][i]==99 || anint[m][i]==9999)
	      agev[m][i]=1;
	    else if(agev[m][i] <agemin){ 
	      agemin=agev[m][i];
	      /*printf(" Min anint[%d][%d]=%.2f annais[%d]=%.2f, agemin=%.2f\n",m,i,anint[m][i], i,annais[i], agemin);*/
	    }
	    else if(agev[m][i] >agemax){
	      agemax=agev[m][i];
	     /* printf(" anint[%d][%d]=%.0f annais[%d]=%.0f, agemax=%.0f\n",m,i,anint[m][i], i,annais[i], agemax);*/
	    }
	    /*agev[m][i]=anint[m][i]-annais[i];*/
	    /*	 agev[m][i] = age[i]+2*m;*/
	  }
	  else { /* =9 */
	    agev[m][i]=1;
	    s[m][i]=-1;
	  }
	}
	else /*= 0 Unknown */
	  agev[m][i]=1;
      }
    
    }
    for (i=1; i<=imx; i++)  {
      for(m=1; (m<= maxwav); m++){
	if (s[m][i] > (nlstate+ndeath)) {
	  printf("Error: Wrong value in nlstate or ndeath\n");	
	  goto end;
	}
      }
    }

printf("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, agemin, agemax);

    free_vector(severity,1,maxwav);
    free_imatrix(outcome,1,maxwav+1,1,n);
    free_vector(moisnais,1,n);
    free_vector(annais,1,n);
    free_matrix(mint,1,maxwav,1,n);
    free_matrix(anint,1,maxwav,1,n);
    free_vector(moisdc,1,n);
    free_vector(andc,1,n);

   
    wav=ivector(1,imx);
    dh=imatrix(1,lastpass-firstpass+1,1,imx);
    mw=imatrix(1,lastpass-firstpass+1,1,imx);
   
    /* Concatenates waves */
      concatwav(wav, dh, mw, s, agedc, agev,  firstpass, lastpass, imx, nlstate, stepm);


      Tcode=ivector(1,100);
      nbcode=imatrix(0,NCOVMAX,0,NCOVMAX); 
      ncodemax[1]=1;
      if (cptcovn > 0) tricode(Tvar,nbcode,imx);
      
   codtab=imatrix(1,100,1,10);
   h=0;
   m=pow(2,cptcoveff);
 
   for(k=1;k<=cptcoveff; k++){
     for(i=1; i <=(m/pow(2,k));i++){
       for(j=1; j <= ncodemax[k]; j++){
	 for(cpt=1; cpt <=(m/pow(2,cptcoveff+1-k)); cpt++){
	   h++;
	   if (h>m) h=1;codtab[h][k]=j;
	 } 
       }
     }
   } 


   /*for(i=1; i <=m ;i++){ 
     for(k=1; k <=cptcovn; k++){
       printf("i=%d k=%d %d %d",i,k,codtab[i][k], cptcoveff);
     }
     printf("\n");
   }
   scanf("%d",i);*/
    
   /* Calculates basic frequencies. Computes observed prevalence at single age
       and prints on file fileres'p'. */
  freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvar,nbcode, ncodemax);

    pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
    oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
    newms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
    savms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
    oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */
    
    /* For Powell, parameters are in a vector p[] starting at p[1]
       so we point p on param[1][1] so that p[1] maps on param[1][1][1] */
    p=param[1][1]; /* *(*(*(param +1)+1)+0) */

    if(mle==1){
    mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func);
    }
    
    /*--------- results files --------------*/
    fprintf(ficres,"\ntitle=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncov=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=%s\n", title, datafile, lastobs, firstpass,lastpass,ftol, stepm, ncov, nlstate, ndeath, maxwav, mle,weightopt,model);
    
   jk=1;
   fprintf(ficres,"# Parameters\n");
   printf("# Parameters\n");
   for(i=1,jk=1; i <=nlstate; i++){
     for(k=1; k <=(nlstate+ndeath); k++){
       if (k != i) 
	 {
	   printf("%d%d ",i,k);
	   fprintf(ficres,"%1d%1d ",i,k);
	   for(j=1; j <=ncovmodel; j++){
	     printf("%f ",p[jk]);
	     fprintf(ficres,"%f ",p[jk]);
	     jk++; 
	   }
	   printf("\n");
	   fprintf(ficres,"\n");
	 }
     }
   }
 if(mle==1){
    /* Computing hessian and covariance matrix */
    ftolhess=ftol; /* Usually correct */
    hesscov(matcov, p, npar, delti, ftolhess, func);
 }
    fprintf(ficres,"# Scales\n");
    printf("# Scales\n");
     for(i=1,jk=1; i <=nlstate; i++){
      for(j=1; j <=nlstate+ndeath; j++){
	if (j!=i) {
	  fprintf(ficres,"%1d%1d",i,j);
	  printf("%1d%1d",i,j);
	  for(k=1; k<=ncovmodel;k++){
	    printf(" %.5e",delti[jk]);
	    fprintf(ficres," %.5e",delti[jk]);
	    jk++;
	  }
	  printf("\n");
	  fprintf(ficres,"\n");
	}
      }
      }
    
    k=1;
    fprintf(ficres,"# Covariance\n");
    printf("# Covariance\n");
    for(i=1;i<=npar;i++){
      /*  if (k>nlstate) k=1;
      i1=(i-1)/(ncovmodel*nlstate)+1; 
      fprintf(ficres,"%s%d%d",alph[k],i1,tab[i]);
      printf("%s%d%d",alph[k],i1,tab[i]);*/
      fprintf(ficres,"%3d",i);
      printf("%3d",i);
      for(j=1; j<=i;j++){
	fprintf(ficres," %.5e",matcov[i][j]);
	printf(" %.5e",matcov[i][j]);
      }
      fprintf(ficres,"\n");
      printf("\n");
      k++;
    }
    
    while((c=getc(ficpar))=='#' && c!= EOF){
      ungetc(c,ficpar);
      fgets(line, MAXLINE, ficpar);
      puts(line);
      fputs(line,ficparo);
    }
    ungetc(c,ficpar);
  
    fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf\n",&agemin,&agemax, &bage, &fage);
    
    if (fage <= 2) {
      bage = agemin;
      fage = agemax;
    }

    fprintf(ficres,"# agemin agemax for life expectancy, bage fage (if mle==0 ie no data nor Max likelihood).\n");
    fprintf(ficres,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f\n",agemin,agemax,bage,fage);

   
/*------------ gnuplot -------------*/
chdir(pathcd);
  if((ficgp=fopen("graph.plt","w"))==NULL) {
    printf("Problem with file graph.gp");goto end;
  }
#ifdef windows
  fprintf(ficgp,"cd \"%s\" \n",pathc);
#endif
m=pow(2,cptcoveff);
  
 /* 1eme*/
  for (cpt=1; cpt<= nlstate ; cpt ++) {
   for (k1=1; k1<= m ; k1 ++) {

#ifdef windows
    fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \nset ter gif small size 400,300\nplot [%.f:%.f] \"vpl%s\" every :::%d::%d u 1:2 \"\%%lf",agemin,fage,fileres,k1-1,k1-1);
#endif
#ifdef unix
fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \nplot [%.f:%.f] \"vpl%s\" u 1:2 \"\%%lf",agemin,fage,fileres);
#endif

for (i=1; i<= nlstate ; i ++) {
  if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)");
  else fprintf(ficgp," \%%*lf (\%%*lf)");
}
    fprintf(ficgp,"\" t\"Stationary prevalence\" w l 0,\"vpl%s\" every :::%d::%d u 1:($2+2*$3) \"\%%lf",fileres,k1-1,k1-1);
    for (i=1; i<= nlstate ; i ++) {
  if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)");
  else fprintf(ficgp," \%%*lf (\%%*lf)");
} 
  fprintf(ficgp,"\" t\"95\%% CI\" w l 1,\"vpl%s\" every :::%d::%d u 1:($2-2*$3) \"\%%lf",fileres,k1-1,k1-1); 
     for (i=1; i<= nlstate ; i ++) {
  if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)");
  else fprintf(ficgp," \%%*lf (\%%*lf)");
}  
     fprintf(ficgp,"\" t\"\" w l 1,\"p%s\" every :::%d::%d u 1:($%d) t\"Observed prevalence \" w l 2",fileres,k1-1,k1-1,2+4*(cpt-1));
#ifdef unix
fprintf(ficgp,"\nset ter gif small size 400,300");
#endif
fprintf(ficgp,"\nset out \"v%s%d%d.gif\" \nreplot\n\n",strtok(optionfile, "."),cpt,k1);
   }
  }
  /*2 eme*/

  for (k1=1; k1<= m ; k1 ++) { 
    fprintf(ficgp,"set ylabel \"Years\" \nset ter gif small size 400,300\nplot [%.f:%.f] ",agemin,fage);
    
    for (i=1; i<= nlstate+1 ; i ++) {
      k=2*i;
      fprintf(ficgp,"\"t%s\" every :::%d::%d u 1:2 \"\%%lf",fileres,k1-1,k1-1);
      for (j=1; j<= nlstate+1 ; j ++) {
  if (j==i) fprintf(ficgp," \%%lf (\%%lf)");
  else fprintf(ficgp," \%%*lf (\%%*lf)");
}   
      if (i== 1) fprintf(ficgp,"\" t\"TLE\" w l ,");
      else fprintf(ficgp,"\" t\"LE in state (%d)\" w l ,",i-1);
    fprintf(ficgp,"\"t%s\" every :::%d::%d u 1:($2-$3*2) \"\%%lf",fileres,k1-1,k1-1);
      for (j=1; j<= nlstate+1 ; j ++) {
	if (j==i) fprintf(ficgp," \%%lf (\%%lf)");
	else fprintf(ficgp," \%%*lf (\%%*lf)");
}   
      fprintf(ficgp,"\" t\"\" w l 0,");
     fprintf(ficgp,"\"t%s\" every :::%d::%d u 1:($2+$3*2) \"\%%lf",fileres,k1-1,k1-1);
      for (j=1; j<= nlstate+1 ; j ++) {
  if (j==i) fprintf(ficgp," \%%lf (\%%lf)");
  else fprintf(ficgp," \%%*lf (\%%*lf)");
}   
      if (i== (nlstate+1)) fprintf(ficgp,"\" t\"\" w l 0");
      else fprintf(ficgp,"\" t\"\" w l 0,");
    }
    fprintf(ficgp,"\nset out \"e%s%d.gif\" \nreplot\n\n",strtok(optionfile, "."),k1);
  }
 
  /*3eme*/

  for (k1=1; k1<= m ; k1 ++) { 
    for (cpt=1; cpt<= nlstate ; cpt ++) {
      k=2+nlstate*(cpt-1);
      fprintf(ficgp,"set ter gif small size 400,300\nplot [%.f:%.f] \"e%s\" every :::%d::%d u 1:%d t \"e%d1\" w l",agemin,fage,fileres,k1-1,k1-1,k,cpt);
      for (i=1; i< nlstate ; i ++) {
	fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:%d t \"e%d%d\" w l",fileres,k1-1,k1-1,k+i,cpt,i+1);
      } 
      fprintf(ficgp,"\nset out \"exp%s%d%d.gif\" \nreplot\n\n",strtok(optionfile, "."),cpt,k1);
    }
  }
 
  /* CV preval stat */
  for (k1=1; k1<= m ; k1 ++) { 
    for (cpt=1; cpt<nlstate ; cpt ++) {
      k=3;
      fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \nset ter gif small size 400,300\nplot [%.f:%.f] \"pij%s\" u ($1==%d ? ($3):1/0):($%d/($%d",agemin,agemax,fileres,k1,k+cpt+1,k+1);
      for (i=1; i< nlstate ; i ++)
	fprintf(ficgp,"+$%d",k+i+1);
      fprintf(ficgp,")) t\"prev(%d,%d)\" w l",cpt,cpt+1);
      
      l=3+(nlstate+ndeath)*cpt;
      fprintf(ficgp,",\"pij%s\" u ($1==%d ? ($3):1/0):($%d/($%d",fileres,k1,l+cpt+1,l+1);
      for (i=1; i< nlstate ; i ++) {
	l=3+(nlstate+ndeath)*cpt;
	fprintf(ficgp,"+$%d",l+i+1);
      }
      fprintf(ficgp,")) t\"prev(%d,%d)\" w l\n",cpt+1,cpt+1);   
      fprintf(ficgp,"set out \"p%s%d%d.gif\" \nreplot\n\n",strtok(optionfile, "."),cpt,k1);
    } 
  }

  /* proba elementaires */
   for(i=1,jk=1; i <=nlstate; i++){
    for(k=1; k <=(nlstate+ndeath); k++){
      if (k != i) {
	for(j=1; j <=ncovmodel; j++){
	  /*fprintf(ficgp,"%s%1d%1d=%f ",alph[j],i,k,p[jk]);*/
	  /*fprintf(ficgp,"%s",alph[1]);*/
	  fprintf(ficgp,"p%d=%f ",jk,p[jk]);
	  jk++; 
	  fprintf(ficgp,"\n");
	}
      }
    }
    }

  for(jk=1; jk <=m; jk++) {
  fprintf(ficgp,"\nset ter gif small size 400,300\nset log y\nplot  [%.f:%.f] ",agemin,agemax);
   i=1;
   for(k2=1; k2<=nlstate; k2++) {
     k3=i;
     for(k=1; k<=(nlstate+ndeath); k++) {
       if (k != k2){
    	fprintf(ficgp," exp(p%d+p%d*x",i,i+1);
ij=1;
	for(j=3; j <=ncovmodel; j++) {
	  if(((j-2)==Tage[ij]) &&(ij <=cptcovage)) {
	    fprintf(ficgp,"+p%d*%d*x",i+j-1,nbcode[Tvar[j-2]][codtab[jk][Tvar[j-2]]]);
	    ij++;
	  }
	  else
	  fprintf(ficgp,"+p%d*%d",i+j-1,nbcode[Tvar[j-2]][codtab[jk][j-2]]);
	}
	  fprintf(ficgp,")/(1");
	
	for(k1=1; k1 <=nlstate; k1++){   
	  fprintf(ficgp,"+exp(p%d+p%d*x",k3+(k1-1)*ncovmodel,k3+(k1-1)*ncovmodel+1);
ij=1;
	  for(j=3; j <=ncovmodel; j++){
	  if(((j-2)==Tage[ij]) &&(ij <=cptcovage)) {
	    fprintf(ficgp,"+p%d*%d*x",k3+(k1-1)*ncovmodel+1+j-2,nbcode[Tvar[j-2]][codtab[jk][Tvar[j-2]]]);
	    ij++;
	  }
	  else
	    fprintf(ficgp,"+p%d*%d",k3+(k1-1)*ncovmodel+1+j-2,nbcode[Tvar[j-2]][codtab[jk][j-2]]);
	  }
	  fprintf(ficgp,")");
	}
	fprintf(ficgp,") t \"p%d%d\" ", k2,k);
	if ((k+k2)!= (nlstate*2+ndeath)) fprintf(ficgp,",");
	i=i+ncovmodel;
       }
     }
   }
   fprintf(ficgp,"\nset out \"pe%s%d.gif\" \nreplot\n\n",strtok(optionfile, "."),jk); 
  }
   
  fclose(ficgp);
   
chdir(path);
    free_matrix(agev,1,maxwav,1,imx);
    free_ivector(wav,1,imx);
    free_imatrix(dh,1,lastpass-firstpass+1,1,imx);
    free_imatrix(mw,1,lastpass-firstpass+1,1,imx);
    
    free_imatrix(s,1,maxwav+1,1,n);
    
    
    free_ivector(num,1,n);
    free_vector(agedc,1,n);
    free_vector(weight,1,n);
    /*free_matrix(covar,1,NCOVMAX,1,n);*/
    fclose(ficparo);
    fclose(ficres);
    /*  }*/
   
   /*________fin mle=1_________*/
   

  
    /* No more information from the sample is required now */
  /* Reads comments: lines beginning with '#' */
  while((c=getc(ficpar))=='#' && c!= EOF){
    ungetc(c,ficpar);
    fgets(line, MAXLINE, ficpar);
    puts(line);
    fputs(line,ficparo);
  }
  ungetc(c,ficpar);
  
  fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf\n",&agemin,&agemax, &bage, &fage);
  printf("agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f\n",agemin,agemax, bage, fage);
  fprintf(ficparo,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f\n",agemin,agemax,bage,fage);
/*--------- index.htm --------*/

  strcpy(optionfilehtm,optionfile);
  strcat(optionfilehtm,".htm");
  if((fichtm=fopen(optionfilehtm,"w"))==NULL)    {
    printf("Problem with %s \n",optionfilehtm);goto end;
  }

 fprintf(fichtm,"<body><ul> <font size=\"6\">Imach, Version 0.64b </font> <hr size=\"2\" color=\"#EC5E5E\"> 
Titre=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=%s<br>
Total number of observations=%d <br>
Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>
<hr  size=\"2\" color=\"#EC5E5E\"> 
<li>Outputs files<br><br>\n
        - Observed prevalence in each state: <a href=\"p%s\">p%s</a> <br>\n
- Estimated parameters and the covariance matrix: <a href=\"%s\">%s</a> <br>
        - Stationary prevalence in each state: <a href=\"pl%s\">pl%s</a> <br>
        - Transition probabilities: <a href=\"pij%s\">pij%s</a><br>
        - Copy of the parameter file: <a href=\"o%s\">o%s</a><br>
        - Life expectancies by age and initial health status: <a href=\"e%s\">e%s</a> <br>
        - Variances of life expectancies by age and initial health status: <a href=\"v%s\">v%s</a><br>
        - Health expectancies with their variances: <a href=\"t%s\">t%s</a> <br>
        - Standard deviation of stationary prevalences: <a href=\"vpl%s\">vpl%s</a> <br><br>",title,datafile,firstpass,lastpass,stepm, weightopt,model,imx,jmin,jmax,jmean,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres);

 fprintf(fichtm," <li>Graphs</li><p>");

 m=cptcoveff;
 if (cptcovn < 1) {m=1;ncodemax[1]=1;}

 j1=0;
 for(k1=1; k1<=m;k1++){
   for(i1=1; i1<=ncodemax[k1];i1++){
       j1++;
       if (cptcovn > 0) {
	 fprintf(fichtm,"<hr  size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
	 for (cpt=1; cpt<=cptcoveff;cpt++) 
	   fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtab[j1][cpt]]);
	 fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
       }
       fprintf(fichtm,"<br>- Probabilities: pe%s%d.gif<br>
<img src=\"pe%s%d.gif\">",strtok(optionfile, "."),j1,strtok(optionfile, "."),j1);     
       for(cpt=1; cpt<nlstate;cpt++){
	 fprintf(fichtm,"<br>- Prevalence of disability : p%s%d%d.gif<br>
<img src=\"p%s%d%d.gif\">",strtok(optionfile, "."),cpt,j1,strtok(optionfile, "."),cpt,j1);
       }
    for(cpt=1; cpt<=nlstate;cpt++) {
       fprintf(fichtm,"<br>- Observed and stationary prevalence (with confident
interval) in state (%d): v%s%d%d.gif <br>
<img src=\"v%s%d%d.gif\">",cpt,strtok(optionfile, "."),cpt,j1,strtok(optionfile, "."),cpt,j1);  
     }
     for(cpt=1; cpt<=nlstate;cpt++) {
        fprintf(fichtm,"\n<br>- Health life expectancies by age and initial health state (%d): exp%s%d%d.gif <br>
<img src=\"exp%s%d%d.gif\">",cpt,strtok(optionfile, "."),cpt,j1,strtok(optionfile, "."),cpt,j1);
     }
     fprintf(fichtm,"\n<br>- Total life expectancy by age and
health expectancies in states (1) and (2): e%s%d.gif<br>
<img src=\"e%s%d.gif\">",strtok(optionfile, "."),j1,strtok(optionfile, "."),j1);
fprintf(fichtm,"\n</body>");
   }
 }
fclose(fichtm);

  /*--------------- Prevalence limit --------------*/
  
  strcpy(filerespl,"pl");
  strcat(filerespl,fileres);
  if((ficrespl=fopen(filerespl,"w"))==NULL) {
    printf("Problem with Prev limit resultfile: %s\n", filerespl);goto end;
  }
  printf("Computing prevalence limit: result on file '%s' \n", filerespl);
  fprintf(ficrespl,"#Prevalence limit\n");
  fprintf(ficrespl,"#Age ");
  for(i=1; i<=nlstate;i++) fprintf(ficrespl,"%d-%d ",i,i);
  fprintf(ficrespl,"\n");
  
  prlim=matrix(1,nlstate,1,nlstate);
  pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
  oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
  newms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
  savms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
  oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */
  k=0;
  agebase=agemin;
  agelim=agemax;
  ftolpl=1.e-10;
  i1=cptcoveff;
  if (cptcovn < 1){i1=1;}

  for(cptcov=1;cptcov<=i1;cptcov++){
    for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
	k=k+1;
	/*printf("cptcov=%d cptcod=%d codtab=%d nbcode=%d\n",cptcov, cptcod,Tcode[cptcode],codtab[cptcod][cptcov]);*/
	fprintf(ficrespl,"\n#******");
	for(j=1;j<=cptcoveff;j++) 
	  fprintf(ficrespl," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
	fprintf(ficrespl,"******\n");
	
	for (age=agebase; age<=agelim; age++){
	  prevalim(prlim, nlstate, p, age, oldm, savm,ftolpl,k);
	  fprintf(ficrespl,"%.0f",age );
	  for(i=1; i<=nlstate;i++)
	  fprintf(ficrespl," %.5f", prlim[i][i]);
	  fprintf(ficrespl,"\n");
	}
      }
    }
  fclose(ficrespl);
  /*------------- h Pij x at various ages ------------*/
  
  strcpy(filerespij,"pij");  strcat(filerespij,fileres);
  if((ficrespij=fopen(filerespij,"w"))==NULL) {
    printf("Problem with Pij resultfile: %s\n", filerespij);goto end;
  }
  printf("Computing pij: result on file '%s' \n", filerespij);
  
  stepsize=(int) (stepm+YEARM-1)/YEARM;
  if (stepm<=24) stepsize=2;

  agelim=AGESUP;
  hstepm=stepsize*YEARM; /* Every year of age */
  hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */ 
  
  k=0;
  for(cptcov=1;cptcov<=i1;cptcov++){
    for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
      k=k+1;
	fprintf(ficrespij,"\n#****** ");
	for(j=1;j<=cptcoveff;j++) 
	  fprintf(ficrespij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
	fprintf(ficrespij,"******\n");
	
	for (agedeb=fage; agedeb>=bage; agedeb--){ /* If stepm=6 months */
	  nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */ 
	  nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
	  p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
	  oldm=oldms;savm=savms;
	  hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);  
	  fprintf(ficrespij,"# Age");
	  for(i=1; i<=nlstate;i++)
	    for(j=1; j<=nlstate+ndeath;j++)
	      fprintf(ficrespij," %1d-%1d",i,j);
	  fprintf(ficrespij,"\n");
	  for (h=0; h<=nhstepm; h++){
	    fprintf(ficrespij,"%d %.0f %.0f",k,agedeb, agedeb+ h*hstepm/YEARM*stepm );
	    for(i=1; i<=nlstate;i++)
	      for(j=1; j<=nlstate+ndeath;j++)
		fprintf(ficrespij," %.5f", p3mat[i][j][h]);
	    fprintf(ficrespij,"\n");
	  }
	  free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
	  fprintf(ficrespij,"\n");
	}
    }
  }

  fclose(ficrespij);

  /*---------- Health expectancies and variances ------------*/

  strcpy(filerest,"t");
  strcat(filerest,fileres);
  if((ficrest=fopen(filerest,"w"))==NULL) {
    printf("Problem with total LE resultfile: %s\n", filerest);goto end;
  }
  printf("Computing Total LEs with variances: file '%s' \n", filerest); 


  strcpy(filerese,"e");
  strcat(filerese,fileres);
  if((ficreseij=fopen(filerese,"w"))==NULL) {
    printf("Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
  }
  printf("Computing Health Expectancies: result on file '%s' \n", filerese);

 strcpy(fileresv,"v");
  strcat(fileresv,fileres);
  if((ficresvij=fopen(fileresv,"w"))==NULL) {
    printf("Problem with variance resultfile: %s\n", fileresv);exit(0);
  }
  printf("Computing Variance-covariance of DFLEs: file '%s' \n", fileresv);

  k=0;
  for(cptcov=1;cptcov<=i1;cptcov++){
    for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
      k=k+1;
      fprintf(ficrest,"\n#****** ");
      for(j=1;j<=cptcoveff;j++) 
	fprintf(ficrest,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
      fprintf(ficrest,"******\n");

      fprintf(ficreseij,"\n#****** ");
      for(j=1;j<=cptcoveff;j++) 
	fprintf(ficreseij,"V%d=%d ",j,nbcode[j][codtab[k][j]]);
      fprintf(ficreseij,"******\n");

      fprintf(ficresvij,"\n#****** ");
      for(j=1;j<=cptcoveff;j++) 
	fprintf(ficresvij,"V%d=%d ",j,nbcode[j][codtab[k][j]]);
      fprintf(ficresvij,"******\n");

      eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
      oldm=oldms;savm=savms;
      evsij(fileres, eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k);  
      vareij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
      oldm=oldms;savm=savms;
      varevsij(fileres, vareij, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl,k);
      
      fprintf(ficrest,"#Total LEs with variances: e.. (std) ");
      for (i=1;i<=nlstate;i++) fprintf(ficrest,"e.%d (std) ",i);
      fprintf(ficrest,"\n");
	
      hf=1;
      if (stepm >= YEARM) hf=stepm/YEARM;
      epj=vector(1,nlstate+1);
      for(age=bage; age <=fage ;age++){
	prevalim(prlim, nlstate, p, age, oldm, savm,ftolpl,k);
	fprintf(ficrest," %.0f",age);
	for(j=1, epj[nlstate+1]=0.;j <=nlstate;j++){
	  for(i=1, epj[j]=0.;i <=nlstate;i++) {
	    epj[j] += prlim[i][i]*hf*eij[i][j][(int)age];
	  }
	  epj[nlstate+1] +=epj[j];
	}
	for(i=1, vepp=0.;i <=nlstate;i++)
	  for(j=1;j <=nlstate;j++)
	    vepp += vareij[i][j][(int)age];
	fprintf(ficrest," %.2f (%.2f)", epj[nlstate+1],hf*sqrt(vepp));
	for(j=1;j <=nlstate;j++){
	  fprintf(ficrest," %.2f (%.2f)", epj[j],hf*sqrt(vareij[j][j][(int)age]));
	}
	fprintf(ficrest,"\n");
      }
    }
  }
	
 fclose(ficreseij);
 fclose(ficresvij);
  fclose(ficrest);
  fclose(ficpar);
  free_vector(epj,1,nlstate+1);
  /*  scanf("%d ",i); */

  /*------- Variance limit prevalence------*/   

strcpy(fileresvpl,"vpl");
  strcat(fileresvpl,fileres);
  if((ficresvpl=fopen(fileresvpl,"w"))==NULL) {
    printf("Problem with variance prev lim resultfile: %s\n", fileresvpl);
    exit(0);
  }
  printf("Computing Variance-covariance of Prevalence limit: file '%s' \n", fileresvpl);

 k=0;
 for(cptcov=1;cptcov<=i1;cptcov++){
   for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
     k=k+1;
     fprintf(ficresvpl,"\n#****** ");
     for(j=1;j<=cptcoveff;j++) 
       fprintf(ficresvpl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
     fprintf(ficresvpl,"******\n");
     
     varpl=matrix(1,nlstate,(int) bage, (int) fage);
     oldm=oldms;savm=savms;
     varprevlim(fileres, varpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl,k);
   }
 }

  fclose(ficresvpl);

  /*---------- End : free ----------------*/
  free_matrix(varpl,1,nlstate,(int) bage, (int)fage);
  
  free_ma3x(vareij,1,nlstate,1,nlstate,(int) bage, (int)fage);
  free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
  
  
  free_matrix(pmmij,1,nlstate+ndeath,1,nlstate+ndeath);
  free_matrix(oldms, 1,nlstate+ndeath,1,nlstate+ndeath);
  free_matrix(newms, 1,nlstate+ndeath,1,nlstate+ndeath);
  free_matrix(savms, 1,nlstate+ndeath,1,nlstate+ndeath);
  
  free_matrix(matcov,1,npar,1,npar);
  free_vector(delti,1,npar);
  
  free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);

  printf("End of Imach\n");
  /*  gettimeofday(&end_time, (struct timezone*)0);*/  /* after time */
  
  /* printf("Total time was %d Sec. %d uSec.\n", end_time.tv_sec -start_time.tv_sec, end_time.tv_usec -start_time.tv_usec);*/
  /*printf("Total time was %d uSec.\n", total_usecs);*/
  /*------ End -----------*/

 end:
#ifdef windows
 chdir(pathcd);
#endif 
 /*system("wgnuplot graph.plt");*/
 /*system("../gp37mgw/wgnuplot graph.plt");*/
 /*system("cd ../gp37mgw");*/
 system("..\\gp37mgw\\wgnuplot graph.plt");

#ifdef windows
  while (z[0] != 'q') {
    chdir(pathcd); 
    printf("\nType e to edit output files, c to start again, and q for exiting: ");
    scanf("%s",z);
    if (z[0] == 'c') system("./imach");
    else if (z[0] == 'e') {
      chdir(path);
      system(optionfilehtm);
    }
    else if (z[0] == 'q') exit(0);
  }
#endif 
}



FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>