From: Agnès Lièvre <agnes.lievre@education.gouv.fr>
Date: Wed, 20 Feb 2002 17:17:09 +0000 (+0000)
Subject: Forecasting
X-Git-Tag: Version-0-8a~33
X-Git-Url: https://henry.ined.fr/git/?a=commitdiff_plain;h=b0d860d2c597c7203bf18104f940ac8be0159681;p=.git

Forecasting
---

diff --git a/src/imach.c b/src/imach.c
index 2733ce0..70851b9 100644
--- a/src/imach.c
+++ b/src/imach.c
@@ -1150,13 +1150,13 @@ void lubksb(double **a, int n, int *indx, double b[])
 } 
 
 /************ Frequencies ********************/
-void  freqsummary(char fileres[], int agemin, int agemax, int **s, double **agev, int nlstate, int imx, int *Tvar, int **nbcode, int *ncodemax, int fprev1,int lprev1,double **mint,double **anint)
+void  freqsummary(char fileres[], int agemin, int agemax, int **s, double **agev, int nlstate, int imx, int *Tvar, int **nbcode, int *ncodemax, int fprev1,int lprev1,double **mint,double **anint, int boolprev, double dateprev1,double dateprev2)
 {  /* Some frequencies */
  
-  int i, m, jk, k1, k2,i1, j1, bool, z1,z2,j;
+  int i, m, jk, k1,i1, j1, bool, z1,z2,j;
   double ***freq; /* Frequencies */
   double *pp;
-  double pos;
+  double pos, k2;
   FILE *ficresp;
   char fileresp[FILENAMELENGTH];
 
@@ -1191,16 +1191,26 @@ void  freqsummary(char fileres[], int agemin, int agemax, int **s, double **agev
 	     if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtab[j1][z1]]) 
 	       bool=0;
 	 }
-	  if (bool==1) {
-	    for(m=fprev1; m<=lprev1; m++){
+	 if (bool==1) {
+	   if (boolprev==1){
+	     for(m=fprev1; m<=lprev1; 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];
+	     }
+	   }
+	   else {
+	    for(m=firstpass; m<=lastpass; m++){
 	     k2=anint[m][i]+(mint[m][i]/12.);
-	     if ((k2>=1984) && (k2<=1988.5)) {
+	     if ((k2>=dateprev1) && (k2<=dateprev2)) {
 	     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) {
@@ -1269,13 +1279,13 @@ void  freqsummary(char fileres[], int agemin, int agemax, int **s, double **agev
 }  /* End of Freq */
 
 /************ Prevalence ********************/
-void prevalence(int agemin, int agemax, int **s, double **agev, int nlstate, int imx, int *Tvar, int **nbcode, int *ncodemax, int fprev1,int lprev1)
+void prevalence(int agemin, int agemax, int **s, double **agev, int nlstate, int imx, int *Tvar, int **nbcode, int *ncodemax, int fprev1,int lprev1, double **mint,double **anint,int boolprev, double dateprev1, double dateprev2)
 {  /* Some frequencies */
  
   int i, m, jk, k1, i1, j1, bool, z1,z2,j;
   double ***freq; /* Frequencies */
   double *pp;
-  double pos;
+  double pos, k2;
 
   pp=vector(1,nlstate);
   probs= ma3x(1,130 ,1,8, 1,8);
@@ -1303,21 +1313,34 @@ void prevalence(int agemin, int agemax, int **s, double **agev, int nlstate, int
 	      bool=0;
 	      }
 	if (bool==1) {
-	  for(m=fprev1; m<=lprev1; 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 (boolprev==1){
+	    for(m=fprev1; m<=lprev1; 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];
+	    }
+	  }
+	  else {
+	    for(m=firstpass; m<=lastpass; m++){
+	      k2=anint[m][i]+(mint[m][i]/12.);
+	      if ((k2>=dateprev1) && (k2<=dateprev2)) {
+		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];
+	      }
+	    }
 	  }
 	}
       }
-       for(i=(int)agemin; i <= (int)agemax+3; 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++)
+	for(i=(int)agemin; i <= (int)agemax+3; 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];
 	}
 	
@@ -1336,7 +1359,7 @@ void prevalence(int agemin, int agemax, int **s, double **agev, int nlstate, int
 	   }
 	 }
 	 
-	 }
+	}
     }
   }
   
@@ -1873,7 +1896,7 @@ int main()
   int jnais,jdc,jint4,jint1,jint2,jint3,**outcome,**adl,*tab; 
   int mobilav=0, fprev, lprev ,fprevfore=1, lprevfore=1,nforecast,popforecast=0;
   int hstepm, nhstepm;
-  int *popage;
+  int *popage,boolprev=0;/*boolprev=0 if date and zero if wave*/
 
   double bage, fage, age, agelim, agebase;
   double ftolpl=FTOL;
@@ -1889,6 +1912,7 @@ int main()
   double *epj, vepp;
   double kk1, kk2;
   double *popeffectif,*popcount;
+  double dateprev1, dateprev2;
 
   char version[80]="Imach version 64b, May 2001, INED-EUROREVES ";
   char *alph[]={"a","a","b","c","d","e"}, str[4];
@@ -1898,6 +1922,8 @@ int main()
 #include <sys/time.h>
 #include <time.h>
   char stra[80], strb[80], strc[80], strd[80],stre[80],modelsav[80];
+  char strfprev[10], strlprev[10];
+  char strfprevfore[10], strlprevfore[10];
   /* long total_usecs;
   struct timeval start_time, end_time;
   
@@ -1960,9 +1986,11 @@ while((c=getc(ficpar))=='#' && c!= EOF){
   }
   ungetc(c,ficpar);
   
-  fscanf(ficpar,"fprevalence=%d lprevalence=%d pop_based=%d\n",&fprev,&lprev,&popbased);
-  fprintf(ficparo,"fprevalence=%d lprevalence=%d pop_based=%d\n",fprev,lprev,popbased);
+  fscanf(ficpar,"fprevalence=%s lprevalence=%s pop_based=%d\n",strfprev,strlprev,&popbased);
+  fprintf(ficparo,"fprevalence=%s lprevalence=%s pop_based=%d\n",strfprev,strlprev,popbased);
  
+  /* printf("%s %s",strfprev,strlprev);
+     exit(0);*/
  while((c=getc(ficpar))=='#' && c!= EOF){
     ungetc(c,ficpar);
     fgets(line, MAXLINE, ficpar);
@@ -1971,8 +1999,8 @@ while((c=getc(ficpar))=='#' && c!= EOF){
   }
   ungetc(c,ficpar);
   
-  fscanf(ficpar,"fprevalence=%d lprevalence=%d nforecast=%d mob_average=%d\n",&fprevfore,&lprevfore,&nforecast,&mobilav);
-  fprintf(ficparo,"fprevalence=%d lprevalence=%d nforecast=%d mob_average=%d\n",fprevfore,lprevfore,nforecast,mobilav);
+  fscanf(ficpar,"fprevalence=%s lprevalence=%s nforecast=%d mob_average=%d\n",strfprevfore,strlprevfore,&nforecast,&mobilav);
+  fprintf(ficparo,"fprevalence=%s lprevalence=%s nforecast=%d mob_average=%d\n",strfprevfore,strlprevfore,nforecast,mobilav);
      
   
 while((c=getc(ficpar))=='#' && c!= EOF){
@@ -2329,10 +2357,25 @@ printf("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx,
     
    /* 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, fprev, lprev,mint,anint);
-  
-  free_matrix(mint,1,maxwav,1,n);
-  free_matrix(anint,1,maxwav,1,n);
+
+    if ((nbocc(strfprev,'/')==1) && (nbocc(strlprev,'/')==1)){
+     boolprev=0;
+     cutv(stra,strb,strfprev,'/');
+     dateprev1=(double)(atoi(strb)+atoi(stra)/12.);
+     cutv(stra,strb,strlprev,'/');
+     dateprev2=(double)(atoi(strb)+atoi(stra)/12.);
+   }
+   
+   else if ((nbocc(strfprev,'/')==0) &&(nbocc(strlprev,'/')==0)){
+     boolprev=1;
+     fprev=atoi(strfprev); lprev=atoi(strlprev);
+   }
+    else {
+      printf("Error in statement lprevalence or fprevalence\n");
+      goto end;
+    }
+   
+  freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvar,nbcode, ncodemax, fprev, lprev,mint,anint,boolprev,dateprev1,dateprev2); 
   
   pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
     oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
@@ -2350,8 +2393,8 @@ printf("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx,
     
     /*--------- results files --------------*/
     fprintf(ficres,"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(ficres,"fprevalence=%d lprevalence=%d pop_based=%d\n",fprev,lprev,popbased); 
-   fprintf(ficres,"fprevalence=%d lprevalence=%d nforecast=%d mob_average=%d\n",fprevfore,lprevfore,nforecast,mobilav);
+   fprintf(ficres,"fprevalence=%s lprevalence=%s pop_based=%d\n",strfprev,strlprev,popbased); 
+   fprintf(ficres,"fprevalence=%s lprevalence=%s nforecast=%d mob_average=%d\n",strfprevfore,strlprevfore,nforecast,mobilav);
 
    jk=1;
    fprintf(ficres,"# Parameters\n");
@@ -2393,7 +2436,7 @@ printf("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx,
 	  fprintf(ficres,"\n");
 	}
       }
-      }
+     }
     
     k=1;
     fprintf(ficres,"# Covariance\n");
@@ -2649,7 +2692,7 @@ Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<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>
-        - Prevalences forecasting: <a href=\"f%s\">f%s</a> <br>
+        - Prevalences and population forecasting: <a href=\"f%s\">f%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,fileres,fileres);
 
  fprintf(fichtm," <li>Graphs</li><p>");
@@ -2797,9 +2840,28 @@ fclose(fichtm);
   }
   printf("Computing forecasting: result on file '%s' \n", fileresf);
 
-  prevalence(agemin, agemax, s, agev, nlstate, imx,Tvar,nbcode, ncodemax, fprevfore, lprevfore);
+ if ((nbocc(strfprevfore,'/')==1) && (nbocc(strlprevfore,'/')==1)){
+     boolprev=0;
+     cutv(stra,strb,strfprevfore,'/');
+     dateprev1=(double)(atoi(strb)+atoi(stra)/12.);
+     cutv(stra,strb,strlprevfore,'/');
+     dateprev2=(double)(atoi(strb)+atoi(stra)/12.);
+   }
+   
+   else if ((nbocc(strfprevfore,'/')==0) &&(nbocc(strlprevfore,'/')==0)){
+     boolprev=1;
+     fprev=atoi(strfprevfore); lprev=atoi(strlprevfore);
+   }
+    else {
+      printf("Error in statement lprevalence or fprevalence\n");
+      goto end;
+    }
 
- free_matrix(agev,1,maxwav,1,imx);
+  prevalence(agemin, agemax, s, agev, nlstate, imx,Tvar,nbcode, ncodemax, fprevfore, lprevfore,mint,anint,boolprev,dateprev1,dateprev2);
+  
+  free_matrix(mint,1,maxwav,1,n);
+  free_matrix(anint,1,maxwav,1,n);
+  free_matrix(agev,1,maxwav,1,imx);
   /* Mobile average */
 
   if (cptcoveff==0) ncodemax[cptcoveff]=1;