1: /* $Id: imach-nlopt.c,v 1.1 2014/09/16 11:06:58 brouard Exp $
2: $State: Exp $
3: $Log: imach-nlopt.c,v $
4: Revision 1.1 2014/09/16 11:06:58 brouard
5: Summary: With some code (wrong) for nlopt
6:
7: Author:
8:
9: Revision 1.161 2014/09/15 20:41:41 brouard
10: Summary: Problem with macro SQR on Intel compiler
11:
12: Revision 1.160 2014/09/02 09:24:05 brouard
13: *** empty log message ***
14:
15: Revision 1.159 2014/09/01 10:34:10 brouard
16: Summary: WIN32
17: Author: Brouard
18:
19: Revision 1.158 2014/08/27 17:11:51 brouard
20: *** empty log message ***
21:
22: Revision 1.157 2014/08/27 16:26:55 brouard
23: Summary: Preparing windows Visual studio version
24: Author: Brouard
25:
26: In order to compile on Visual studio, time.h is now correct and time_t
27: and tm struct should be used. difftime should be used but sometimes I
28: just make the differences in raw time format (time(&now).
29: Trying to suppress #ifdef LINUX
30: Add xdg-open for __linux in order to open default browser.
31:
32: Revision 1.156 2014/08/25 20:10:10 brouard
33: *** empty log message ***
34:
35: Revision 1.155 2014/08/25 18:32:34 brouard
36: Summary: New compile, minor changes
37: Author: Brouard
38:
39: Revision 1.154 2014/06/20 17:32:08 brouard
40: Summary: Outputs now all graphs of convergence to period prevalence
41:
42: Revision 1.153 2014/06/20 16:45:46 brouard
43: Summary: If 3 live state, convergence to period prevalence on same graph
44: Author: Brouard
45:
46: Revision 1.152 2014/06/18 17:54:09 brouard
47: Summary: open browser, use gnuplot on same dir than imach if not found in the path
48:
49: Revision 1.151 2014/06/18 16:43:30 brouard
50: *** empty log message ***
51:
52: Revision 1.150 2014/06/18 16:42:35 brouard
53: Summary: If gnuplot is not in the path try on same directory than imach binary (OSX)
54: Author: brouard
55:
56: Revision 1.149 2014/06/18 15:51:14 brouard
57: Summary: Some fixes in parameter files errors
58: Author: Nicolas Brouard
59:
60: Revision 1.148 2014/06/17 17:38:48 brouard
61: Summary: Nothing new
62: Author: Brouard
63:
64: Just a new packaging for OS/X version 0.98nS
65:
66: Revision 1.147 2014/06/16 10:33:11 brouard
67: *** empty log message ***
68:
69: Revision 1.146 2014/06/16 10:20:28 brouard
70: Summary: Merge
71: Author: Brouard
72:
73: Merge, before building revised version.
74:
75: Revision 1.145 2014/06/10 21:23:15 brouard
76: Summary: Debugging with valgrind
77: Author: Nicolas Brouard
78:
79: Lot of changes in order to output the results with some covariates
80: After the Edimburgh REVES conference 2014, it seems mandatory to
81: improve the code.
82: No more memory valgrind error but a lot has to be done in order to
83: continue the work of splitting the code into subroutines.
84: Also, decodemodel has been improved. Tricode is still not
85: optimal. nbcode should be improved. Documentation has been added in
86: the source code.
87:
88: Revision 1.143 2014/01/26 09:45:38 brouard
89: Summary: Version 0.98nR (to be improved, but gives same optimization results as 0.98k. Nice, promising
90:
91: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
92: (Module): Version 0.98nR Running ok, but output format still only works for three covariates.
93:
94: Revision 1.142 2014/01/26 03:57:36 brouard
95: Summary: gnuplot changed plot w l 1 has to be changed to plot w l lt 2
96:
97: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
98:
99: Revision 1.141 2014/01/26 02:42:01 brouard
100: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
101:
102: Revision 1.140 2011/09/02 10:37:54 brouard
103: Summary: times.h is ok with mingw32 now.
104:
105: Revision 1.139 2010/06/14 07:50:17 brouard
106: After the theft of my laptop, I probably lost some lines of codes which were not uploaded to the CVS tree.
107: I remember having already fixed agemin agemax which are pointers now but not cvs saved.
108:
109: Revision 1.138 2010/04/30 18:19:40 brouard
110: *** empty log message ***
111:
112: Revision 1.137 2010/04/29 18:11:38 brouard
113: (Module): Checking covariates for more complex models
114: than V1+V2. A lot of change to be done. Unstable.
115:
116: Revision 1.136 2010/04/26 20:30:53 brouard
117: (Module): merging some libgsl code. Fixing computation
118: of likelione (using inter/intrapolation if mle = 0) in order to
119: get same likelihood as if mle=1.
120: Some cleaning of code and comments added.
121:
122: Revision 1.135 2009/10/29 15:33:14 brouard
123: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
124:
125: Revision 1.134 2009/10/29 13:18:53 brouard
126: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
127:
128: Revision 1.133 2009/07/06 10:21:25 brouard
129: just nforces
130:
131: Revision 1.132 2009/07/06 08:22:05 brouard
132: Many tings
133:
134: Revision 1.131 2009/06/20 16:22:47 brouard
135: Some dimensions resccaled
136:
137: Revision 1.130 2009/05/26 06:44:34 brouard
138: (Module): Max Covariate is now set to 20 instead of 8. A
139: lot of cleaning with variables initialized to 0. Trying to make
140: V2+V3*age+V1+V4 strb=V3*age+V1+V4 working better.
141:
142: Revision 1.129 2007/08/31 13:49:27 lievre
143: Modification of the way of exiting when the covariate is not binary in order to see on the window the error message before exiting
144:
145: Revision 1.128 2006/06/30 13:02:05 brouard
146: (Module): Clarifications on computing e.j
147:
148: Revision 1.127 2006/04/28 18:11:50 brouard
149: (Module): Yes the sum of survivors was wrong since
150: imach-114 because nhstepm was no more computed in the age
151: loop. Now we define nhstepma in the age loop.
152: (Module): In order to speed up (in case of numerous covariates) we
153: compute health expectancies (without variances) in a first step
154: and then all the health expectancies with variances or standard
155: deviation (needs data from the Hessian matrices) which slows the
156: computation.
157: In the future we should be able to stop the program is only health
158: expectancies and graph are needed without standard deviations.
159:
160: Revision 1.126 2006/04/28 17:23:28 brouard
161: (Module): Yes the sum of survivors was wrong since
162: imach-114 because nhstepm was no more computed in the age
163: loop. Now we define nhstepma in the age loop.
164: Version 0.98h
165:
166: Revision 1.125 2006/04/04 15:20:31 lievre
167: Errors in calculation of health expectancies. Age was not initialized.
168: Forecasting file added.
169:
170: Revision 1.124 2006/03/22 17:13:53 lievre
171: Parameters are printed with %lf instead of %f (more numbers after the comma).
172: The log-likelihood is printed in the log file
173:
174: Revision 1.123 2006/03/20 10:52:43 brouard
175: * imach.c (Module): <title> changed, corresponds to .htm file
176: name. <head> headers where missing.
177:
178: * imach.c (Module): Weights can have a decimal point as for
179: English (a comma might work with a correct LC_NUMERIC environment,
180: otherwise the weight is truncated).
181: Modification of warning when the covariates values are not 0 or
182: 1.
183: Version 0.98g
184:
185: Revision 1.122 2006/03/20 09:45:41 brouard
186: (Module): Weights can have a decimal point as for
187: English (a comma might work with a correct LC_NUMERIC environment,
188: otherwise the weight is truncated).
189: Modification of warning when the covariates values are not 0 or
190: 1.
191: Version 0.98g
192:
193: Revision 1.121 2006/03/16 17:45:01 lievre
194: * imach.c (Module): Comments concerning covariates added
195:
196: * imach.c (Module): refinements in the computation of lli if
197: status=-2 in order to have more reliable computation if stepm is
198: not 1 month. Version 0.98f
199:
200: Revision 1.120 2006/03/16 15:10:38 lievre
201: (Module): refinements in the computation of lli if
202: status=-2 in order to have more reliable computation if stepm is
203: not 1 month. Version 0.98f
204:
205: Revision 1.119 2006/03/15 17:42:26 brouard
206: (Module): Bug if status = -2, the loglikelihood was
207: computed as likelihood omitting the logarithm. Version O.98e
208:
209: Revision 1.118 2006/03/14 18:20:07 brouard
210: (Module): varevsij Comments added explaining the second
211: table of variances if popbased=1 .
212: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
213: (Module): Function pstamp added
214: (Module): Version 0.98d
215:
216: Revision 1.117 2006/03/14 17:16:22 brouard
217: (Module): varevsij Comments added explaining the second
218: table of variances if popbased=1 .
219: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
220: (Module): Function pstamp added
221: (Module): Version 0.98d
222:
223: Revision 1.116 2006/03/06 10:29:27 brouard
224: (Module): Variance-covariance wrong links and
225: varian-covariance of ej. is needed (Saito).
226:
227: Revision 1.115 2006/02/27 12:17:45 brouard
228: (Module): One freematrix added in mlikeli! 0.98c
229:
230: Revision 1.114 2006/02/26 12:57:58 brouard
231: (Module): Some improvements in processing parameter
232: filename with strsep.
233:
234: Revision 1.113 2006/02/24 14:20:24 brouard
235: (Module): Memory leaks checks with valgrind and:
236: datafile was not closed, some imatrix were not freed and on matrix
237: allocation too.
238:
239: Revision 1.112 2006/01/30 09:55:26 brouard
240: (Module): Back to gnuplot.exe instead of wgnuplot.exe
241:
242: Revision 1.111 2006/01/25 20:38:18 brouard
243: (Module): Lots of cleaning and bugs added (Gompertz)
244: (Module): Comments can be added in data file. Missing date values
245: can be a simple dot '.'.
246:
247: Revision 1.110 2006/01/25 00:51:50 brouard
248: (Module): Lots of cleaning and bugs added (Gompertz)
249:
250: Revision 1.109 2006/01/24 19:37:15 brouard
251: (Module): Comments (lines starting with a #) are allowed in data.
252:
253: Revision 1.108 2006/01/19 18:05:42 lievre
254: Gnuplot problem appeared...
255: To be fixed
256:
257: Revision 1.107 2006/01/19 16:20:37 brouard
258: Test existence of gnuplot in imach path
259:
260: Revision 1.106 2006/01/19 13:24:36 brouard
261: Some cleaning and links added in html output
262:
263: Revision 1.105 2006/01/05 20:23:19 lievre
264: *** empty log message ***
265:
266: Revision 1.104 2005/09/30 16:11:43 lievre
267: (Module): sump fixed, loop imx fixed, and simplifications.
268: (Module): If the status is missing at the last wave but we know
269: that the person is alive, then we can code his/her status as -2
270: (instead of missing=-1 in earlier versions) and his/her
271: contributions to the likelihood is 1 - Prob of dying from last
272: health status (= 1-p13= p11+p12 in the easiest case of somebody in
273: the healthy state at last known wave). Version is 0.98
274:
275: Revision 1.103 2005/09/30 15:54:49 lievre
276: (Module): sump fixed, loop imx fixed, and simplifications.
277:
278: Revision 1.102 2004/09/15 17:31:30 brouard
279: Add the possibility to read data file including tab characters.
280:
281: Revision 1.101 2004/09/15 10:38:38 brouard
282: Fix on curr_time
283:
284: Revision 1.100 2004/07/12 18:29:06 brouard
285: Add version for Mac OS X. Just define UNIX in Makefile
286:
287: Revision 1.99 2004/06/05 08:57:40 brouard
288: *** empty log message ***
289:
290: Revision 1.98 2004/05/16 15:05:56 brouard
291: New version 0.97 . First attempt to estimate force of mortality
292: directly from the data i.e. without the need of knowing the health
293: state at each age, but using a Gompertz model: log u =a + b*age .
294: This is the basic analysis of mortality and should be done before any
295: other analysis, in order to test if the mortality estimated from the
296: cross-longitudinal survey is different from the mortality estimated
297: from other sources like vital statistic data.
298:
299: The same imach parameter file can be used but the option for mle should be -3.
300:
301: Agnès, who wrote this part of the code, tried to keep most of the
302: former routines in order to include the new code within the former code.
303:
304: The output is very simple: only an estimate of the intercept and of
305: the slope with 95% confident intervals.
306:
307: Current limitations:
308: A) Even if you enter covariates, i.e. with the
309: model= V1+V2 equation for example, the programm does only estimate a unique global model without covariates.
310: B) There is no computation of Life Expectancy nor Life Table.
311:
312: Revision 1.97 2004/02/20 13:25:42 lievre
313: Version 0.96d. Population forecasting command line is (temporarily)
314: suppressed.
315:
316: Revision 1.96 2003/07/15 15:38:55 brouard
317: * imach.c (Repository): Errors in subdirf, 2, 3 while printing tmpout is
318: rewritten within the same printf. Workaround: many printfs.
319:
320: Revision 1.95 2003/07/08 07:54:34 brouard
321: * imach.c (Repository):
322: (Repository): Using imachwizard code to output a more meaningful covariance
323: matrix (cov(a12,c31) instead of numbers.
324:
325: Revision 1.94 2003/06/27 13:00:02 brouard
326: Just cleaning
327:
328: Revision 1.93 2003/06/25 16:33:55 brouard
329: (Module): On windows (cygwin) function asctime_r doesn't
330: exist so I changed back to asctime which exists.
331: (Module): Version 0.96b
332:
333: Revision 1.92 2003/06/25 16:30:45 brouard
334: (Module): On windows (cygwin) function asctime_r doesn't
335: exist so I changed back to asctime which exists.
336:
337: Revision 1.91 2003/06/25 15:30:29 brouard
338: * imach.c (Repository): Duplicated warning errors corrected.
339: (Repository): Elapsed time after each iteration is now output. It
340: helps to forecast when convergence will be reached. Elapsed time
341: is stamped in powell. We created a new html file for the graphs
342: concerning matrix of covariance. It has extension -cov.htm.
343:
344: Revision 1.90 2003/06/24 12:34:15 brouard
345: (Module): Some bugs corrected for windows. Also, when
346: mle=-1 a template is output in file "or"mypar.txt with the design
347: of the covariance matrix to be input.
348:
349: Revision 1.89 2003/06/24 12:30:52 brouard
350: (Module): Some bugs corrected for windows. Also, when
351: mle=-1 a template is output in file "or"mypar.txt with the design
352: of the covariance matrix to be input.
353:
354: Revision 1.88 2003/06/23 17:54:56 brouard
355: * imach.c (Repository): Create a sub-directory where all the secondary files are. Only imach, htm, gp and r(imach) are on the main directory. Correct time and other things.
356:
357: Revision 1.87 2003/06/18 12:26:01 brouard
358: Version 0.96
359:
360: Revision 1.86 2003/06/17 20:04:08 brouard
361: (Module): Change position of html and gnuplot routines and added
362: routine fileappend.
363:
364: Revision 1.85 2003/06/17 13:12:43 brouard
365: * imach.c (Repository): Check when date of death was earlier that
366: current date of interview. It may happen when the death was just
367: prior to the death. In this case, dh was negative and likelihood
368: was wrong (infinity). We still send an "Error" but patch by
369: assuming that the date of death was just one stepm after the
370: interview.
371: (Repository): Because some people have very long ID (first column)
372: we changed int to long in num[] and we added a new lvector for
373: memory allocation. But we also truncated to 8 characters (left
374: truncation)
375: (Repository): No more line truncation errors.
376:
377: Revision 1.84 2003/06/13 21:44:43 brouard
378: * imach.c (Repository): Replace "freqsummary" at a correct
379: place. It differs from routine "prevalence" which may be called
380: many times. Probs is memory consuming and must be used with
381: parcimony.
382: Version 0.95a3 (should output exactly the same maximization than 0.8a2)
383:
384: Revision 1.83 2003/06/10 13:39:11 lievre
385: *** empty log message ***
386:
387: Revision 1.82 2003/06/05 15:57:20 brouard
388: Add log in imach.c and fullversion number is now printed.
389:
390: */
391: /*
392: Interpolated Markov Chain
393:
394: Short summary of the programme:
395:
396: This program computes Healthy Life Expectancies from
397: cross-longitudinal data. Cross-longitudinal data consist in: -1- a
398: first survey ("cross") where individuals from different ages are
399: interviewed on their health status or degree of disability (in the
400: case of a health survey which is our main interest) -2- at least a
401: second wave of interviews ("longitudinal") which measure each change
402: (if any) in individual health status. Health expectancies are
403: computed from the time spent in each health state according to a
404: model. More health states you consider, more time is necessary to reach the
405: Maximum Likelihood of the parameters involved in the model. The
406: simplest model is the multinomial logistic model where pij is the
407: probability to be observed in state j at the second wave
408: conditional to be observed in state i at the first wave. Therefore
409: the model is: log(pij/pii)= aij + bij*age+ cij*sex + etc , where
410: 'age' is age and 'sex' is a covariate. If you want to have a more
411: complex model than "constant and age", you should modify the program
412: where the markup *Covariates have to be included here again* invites
413: you to do it. More covariates you add, slower the
414: convergence.
415:
416: The advantage of this computer programme, compared to a simple
417: multinomial logistic model, is clear when the delay between waves is not
418: identical for each individual. Also, if a individual missed an
419: intermediate interview, the information is lost, but taken into
420: account using an interpolation or extrapolation.
421:
422: hPijx is the probability to be observed in state i at age x+h
423: conditional to the observed state i at age x. The delay 'h' can be
424: split into an exact number (nh*stepm) of unobserved intermediate
425: states. This elementary transition (by month, quarter,
426: semester or year) is modelled as a multinomial logistic. The hPx
427: matrix is simply the matrix product of nh*stepm elementary matrices
428: and the contribution of each individual to the likelihood is simply
429: hPijx.
430:
431: Also this programme outputs the covariance matrix of the parameters but also
432: of the life expectancies. It also computes the period (stable) prevalence.
433:
434: Authors: Nicolas Brouard (brouard@ined.fr) and Agnès Lièvre (lievre@ined.fr).
435: Institut national d'études démographiques, Paris.
436: This software have been partly granted by Euro-REVES, a concerted action
437: from the European Union.
438: It is copyrighted identically to a GNU software product, ie programme and
439: software can be distributed freely for non commercial use. Latest version
440: can be accessed at http://euroreves.ined.fr/imach .
441:
442: Help to debug: LD_PRELOAD=/usr/local/lib/libnjamd.so ./imach foo.imach
443: or better on gdb : set env LD_PRELOAD=/usr/local/lib/libnjamd.so
444:
445: **********************************************************************/
446: /*
447: main
448: read parameterfile
449: read datafile
450: concatwav
451: freqsummary
452: if (mle >= 1)
453: mlikeli
454: print results files
455: if mle==1
456: computes hessian
457: read end of parameter file: agemin, agemax, bage, fage, estepm
458: begin-prev-date,...
459: open gnuplot file
460: open html file
461: period (stable) prevalence | pl_nom 1-1 2-2 etc by covariate
462: for age prevalim() | #****** V1=0 V2=1 V3=1 V4=0 ******
463: | 65 1 0 2 1 3 1 4 0 0.96326 0.03674
464: freexexit2 possible for memory heap.
465:
466: h Pij x | pij_nom ficrestpij
467: # Cov Agex agex+h hpijx with i,j= 1-1 1-2 1-3 2-1 2-2 2-3
468: 1 85 85 1.00000 0.00000 0.00000 0.00000 1.00000 0.00000
469: 1 85 86 0.68299 0.22291 0.09410 0.71093 0.00000 0.28907
470:
471: 1 65 99 0.00364 0.00322 0.99314 0.00350 0.00310 0.99340
472: 1 65 100 0.00214 0.00204 0.99581 0.00206 0.00196 0.99597
473: variance of p one-step probabilities varprob | prob_nom ficresprob #One-step probabilities and stand. devi in ()
474: Standard deviation of one-step probabilities | probcor_nom ficresprobcor #One-step probabilities and correlation matrix
475: Matrix of variance covariance of one-step probabilities | probcov_nom ficresprobcov #One-step probabilities and covariance matrix
476:
477: forecasting if prevfcast==1 prevforecast call prevalence()
478: health expectancies
479: Variance-covariance of DFLE
480: prevalence()
481: movingaverage()
482: varevsij()
483: if popbased==1 varevsij(,popbased)
484: total life expectancies
485: Variance of period (stable) prevalence
486: end
487: */
488:
489:
490:
491:
492: #include <math.h>
493: #include <stdio.h>
494: #include <stdlib.h>
495: #include <string.h>
496:
497: #ifdef _WIN32
498: #include <io.h>
499: #else
500: #include <unistd.h>
501: #endif
502:
503: #include <limits.h>
504: #include <sys/types.h>
505: #include <sys/stat.h>
506: #include <errno.h>
507: /* extern int errno; */
508:
509: /* #ifdef LINUX */
510: /* #include <time.h> */
511: /* #include "timeval.h" */
512: /* #else */
513: /* #include <sys/time.h> */
514: /* #endif */
515:
516: #include <time.h>
517:
518: #ifdef GSL
519: #include <gsl/gsl_errno.h>
520: #include <gsl/gsl_multimin.h>
521: #endif
522:
523: #ifdef NLOPT
524: #include <nlopt.h>
525: #endif
526:
527: /* #include <libintl.h> */
528: /* #define _(String) gettext (String) */
529:
530: #define MAXLINE 1024 /* Was 256. Overflow with 312 with 2 states and 4 covariates. Should be ok */
531:
532: #define GNUPLOTPROGRAM "gnuplot"
533: /*#define GNUPLOTPROGRAM "..\\gp37mgw\\wgnuplot"*/
534: #define FILENAMELENGTH 132
535:
536: #define GLOCK_ERROR_NOPATH -1 /* empty path */
537: #define GLOCK_ERROR_GETCWD -2 /* cannot get cwd */
538:
539: #define MAXPARM 128 /**< Maximum number of parameters for the optimization */
540: #define NPARMAX 64 /**< (nlstate+ndeath-1)*nlstate*ncovmodel */
541:
542: #define NINTERVMAX 8
543: #define NLSTATEMAX 8 /**< Maximum number of live states (for func) */
544: #define NDEATHMAX 8 /**< Maximum number of dead states (for func) */
545: #define NCOVMAX 20 /**< Maximum number of covariates, including generated covariates V1*V2 */
546: #define codtabm(h,k) 1 & (h-1) >> (k-1) ;
547: #define MAXN 20000
548: #define YEARM 12. /**< Number of months per year */
549: #define AGESUP 130
550: #define AGEBASE 40
551: #define AGEGOMP 10. /**< Minimal age for Gompertz adjustment */
552: #ifdef _WIN32
553: #define DIRSEPARATOR '\\'
554: #define CHARSEPARATOR "\\"
555: #define ODIRSEPARATOR '/'
556: #else
557: #define DIRSEPARATOR '/'
558: #define CHARSEPARATOR "/"
559: #define ODIRSEPARATOR '\\'
560: #endif
561:
562: /* $Id: imach-nlopt.c,v 1.1 2014/09/16 11:06:58 brouard Exp $ */
563: /* $State: Exp $ */
564:
565: char version[]="Imach version 0.98nX, August 2014,INED-EUROREVES-Institut de longevite-Japan Society for the Promotion of Science (Grant-in-Aid for Scientific Research 25293121)";
566: char fullversion[]="$Revision: 1.1 $ $Date: 2014/09/16 11:06:58 $";
567: char strstart[80];
568: char optionfilext[10], optionfilefiname[FILENAMELENGTH];
569: int erreur=0, nberr=0, nbwarn=0; /* Error number, number of errors number of warnings */
570: int nvar=0, nforce=0; /* Number of variables, number of forces */
571: /* Number of covariates model=V2+V1+ V3*age+V2*V4 */
572: int cptcovn=0; /**< cptcovn number of covariates added in the model (excepting constant and age and age*product) */
573: int cptcovt=0; /**< cptcovt number of covariates added in the model (excepting constant and age) */
574: int cptcovs=0; /**< cptcovs number of simple covariates V2+V1 =2 */
575: int cptcovage=0; /**< Number of covariates with age: V3*age only =1 */
576: int cptcovprodnoage=0; /**< Number of covariate products without age */
577: int cptcoveff=0; /* Total number of covariates to vary for printing results */
578: int cptcov=0; /* Working variable */
579: int npar=NPARMAX;
580: int nlstate=2; /* Number of live states */
581: int ndeath=1; /* Number of dead states */
582: int ncovmodel=0, ncovcol=0; /* Total number of covariables including constant a12*1 +b12*x ncovmodel=2 */
583: int popbased=0;
584:
585: int *wav; /* Number of waves for this individuual 0 is possible */
586: int maxwav=0; /* Maxim number of waves */
587: int jmin=0, jmax=0; /* min, max spacing between 2 waves */
588: int ijmin=0, ijmax=0; /* Individuals having jmin and jmax */
589: int gipmx=0, gsw=0; /* Global variables on the number of contributions
590: to the likelihood and the sum of weights (done by funcone)*/
591: int mle=1, weightopt=0;
592: int **mw; /* mw[mi][i] is number of the mi wave for this individual */
593: int **dh; /* dh[mi][i] is number of steps between mi,mi+1 for this individual */
594: int **bh; /* bh[mi][i] is the bias (+ or -) for this individual if the delay between
595: * wave mi and wave mi+1 is not an exact multiple of stepm. */
596: int countcallfunc=0; /* Count the number of calls to func */
597: double jmean=1; /* Mean space between 2 waves */
598: double **matprod2(); /* test */
599: double **oldm, **newm, **savm; /* Working pointers to matrices */
600: double **oldms, **newms, **savms; /* Fixed working pointers to matrices */
601: /*FILE *fic ; */ /* Used in readdata only */
602: FILE *ficpar, *ficparo,*ficres, *ficresp, *ficrespl, *ficrespij, *ficrest,*ficresf,*ficrespop;
603: FILE *ficlog, *ficrespow;
604: int globpr=0; /* Global variable for printing or not */
605: double fretone; /* Only one call to likelihood */
606: long ipmx=0; /* Number of contributions */
607: double sw; /* Sum of weights */
608: char filerespow[FILENAMELENGTH];
609: char fileresilk[FILENAMELENGTH]; /* File of individual contributions to the likelihood */
610: FILE *ficresilk;
611: FILE *ficgp,*ficresprob,*ficpop, *ficresprobcov, *ficresprobcor;
612: FILE *ficresprobmorprev;
613: FILE *fichtm, *fichtmcov; /* Html File */
614: FILE *ficreseij;
615: char filerese[FILENAMELENGTH];
616: FILE *ficresstdeij;
617: char fileresstde[FILENAMELENGTH];
618: FILE *ficrescveij;
619: char filerescve[FILENAMELENGTH];
620: FILE *ficresvij;
621: char fileresv[FILENAMELENGTH];
622: FILE *ficresvpl;
623: char fileresvpl[FILENAMELENGTH];
624: char title[MAXLINE];
625: char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH], filerespl[FILENAMELENGTH];
626: char plotcmd[FILENAMELENGTH], pplotcmd[FILENAMELENGTH];
627: char tmpout[FILENAMELENGTH], tmpout2[FILENAMELENGTH];
628: char command[FILENAMELENGTH];
629: int outcmd=0;
630:
631: char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filereso[FILENAMELENGTH], rfileres[FILENAMELENGTH];
632:
633: char filelog[FILENAMELENGTH]; /* Log file */
634: char filerest[FILENAMELENGTH];
635: char fileregp[FILENAMELENGTH];
636: char popfile[FILENAMELENGTH];
637:
638: char optionfilegnuplot[FILENAMELENGTH], optionfilehtm[FILENAMELENGTH], optionfilehtmcov[FILENAMELENGTH] ;
639:
640: /* struct timeval start_time, end_time, curr_time, last_time, forecast_time; */
641: /* struct timezone tzp; */
642: /* extern int gettimeofday(); */
643: struct tm tml, *gmtime(), *localtime();
644:
645: extern time_t time();
646:
647: struct tm start_time, end_time, curr_time, last_time, forecast_time;
648: time_t rstart_time, rend_time, rcurr_time, rlast_time, rforecast_time; /* raw time */
649: struct tm tm;
650:
651: char strcurr[80], strfor[80];
652:
653: char *endptr;
654: long lval;
655: double dval;
656:
657: #define NR_END 1
658: #define FREE_ARG char*
659: #define FTOL 1.0e-10
660:
661: #define NRANSI
662: #define ITMAX 200
663:
664: #define TOL 2.0e-4
665:
666: #define CGOLD 0.3819660
667: #define ZEPS 1.0e-10
668: #define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d);
669:
670: #define GOLD 1.618034
671: #define GLIMIT 100.0
672: #define TINY 1.0e-20
673:
674: static double maxarg1,maxarg2;
675: #define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)>(maxarg2)? (maxarg1):(maxarg2))
676: #define FMIN(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)<(maxarg2)? (maxarg1):(maxarg2))
677:
678: #define SIGN(a,b) ((b)>0.0 ? fabs(a) : -fabs(a))
679: #define rint(a) floor(a+0.5)
680:
681: static double sqrarg;
682: #define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 :sqrarg*sqrarg)
683: #define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;}
684: int agegomp= AGEGOMP;
685:
686: int imx;
687: int stepm=1;
688: /* Stepm, step in month: minimum step interpolation*/
689:
690: int estepm;
691: /* Estepm, step in month to interpolate survival function in order to approximate Life Expectancy*/
692:
693: int m,nb;
694: long *num;
695: int firstpass=0, lastpass=4,*cod, *ncodemax, *Tage,*cens;
696: double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint;
697: double **pmmij, ***probs;
698: double *ageexmed,*agecens;
699: double dateintmean=0;
700:
701: double *weight;
702: int **s; /* Status */
703: double *agedc;
704: double **covar; /**< covar[j,i], value of jth covariate for individual i,
705: * covar=matrix(0,NCOVMAX,1,n);
706: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2]; */
707: double idx;
708: int **nbcode, *Tvar; /**< model=V2 => Tvar[1]= 2 */
709: int *Ndum; /** Freq of modality (tricode */
710: int **codtab; /**< codtab=imatrix(1,100,1,10); */
711: int **Tvard, *Tprod, cptcovprod, *Tvaraff;
712: double *lsurv, *lpop, *tpop;
713:
714: double ftol=FTOL; /**< Tolerance for computing Max Likelihood */
715: double ftolhess; /**< Tolerance for computing hessian */
716:
717: /**************** split *************************/
718: static int split( char *path, char *dirc, char *name, char *ext, char *finame )
719: {
720: /* From a file name with (full) path (either Unix or Windows) we extract the directory (dirc)
721: the name of the file (name), its extension only (ext) and its first part of the name (finame)
722: */
723: char *ss; /* pointer */
724: int l1, l2; /* length counters */
725:
726: l1 = strlen(path ); /* length of path */
727: if ( l1 == 0 ) return( GLOCK_ERROR_NOPATH );
728: ss= strrchr( path, DIRSEPARATOR ); /* find last / */
729: if ( ss == NULL ) { /* no directory, so determine current directory */
730: strcpy( name, path ); /* we got the fullname name because no directory */
731: /*if(strrchr(path, ODIRSEPARATOR )==NULL)
732: printf("Warning you should use %s as a separator\n",DIRSEPARATOR);*/
733: /* get current working directory */
734: /* extern char* getcwd ( char *buf , int len);*/
735: if ( getcwd( dirc, FILENAME_MAX ) == NULL ) {
736: return( GLOCK_ERROR_GETCWD );
737: }
738: /* got dirc from getcwd*/
739: printf(" DIRC = %s \n",dirc);
740: } else { /* strip direcotry from path */
741: ss++; /* after this, the filename */
742: l2 = strlen( ss ); /* length of filename */
743: if ( l2 == 0 ) return( GLOCK_ERROR_NOPATH );
744: strcpy( name, ss ); /* save file name */
745: strncpy( dirc, path, l1 - l2 ); /* now the directory */
746: dirc[l1-l2] = 0; /* add zero */
747: printf(" DIRC2 = %s \n",dirc);
748: }
749: /* We add a separator at the end of dirc if not exists */
750: l1 = strlen( dirc ); /* length of directory */
751: if( dirc[l1-1] != DIRSEPARATOR ){
752: dirc[l1] = DIRSEPARATOR;
753: dirc[l1+1] = 0;
754: printf(" DIRC3 = %s \n",dirc);
755: }
756: ss = strrchr( name, '.' ); /* find last / */
757: if (ss >0){
758: ss++;
759: strcpy(ext,ss); /* save extension */
760: l1= strlen( name);
761: l2= strlen(ss)+1;
762: strncpy( finame, name, l1-l2);
763: finame[l1-l2]= 0;
764: }
765:
766: return( 0 ); /* we're done */
767: }
768:
769:
770: /******************************************/
771:
772: void replace_back_to_slash(char *s, char*t)
773: {
774: int i;
775: int lg=0;
776: i=0;
777: lg=strlen(t);
778: for(i=0; i<= lg; i++) {
779: (s[i] = t[i]);
780: if (t[i]== '\\') s[i]='/';
781: }
782: }
783:
784: char *trimbb(char *out, char *in)
785: { /* Trim multiple blanks in line but keeps first blanks if line starts with blanks */
786: char *s;
787: s=out;
788: while (*in != '\0'){
789: while( *in == ' ' && *(in+1) == ' '){ /* && *(in+1) != '\0'){*/
790: in++;
791: }
792: *out++ = *in++;
793: }
794: *out='\0';
795: return s;
796: }
797:
798: char *cutl(char *blocc, char *alocc, char *in, char occ)
799: {
800: /* cuts string in into blocc and alocc where blocc ends before first occurence of char 'occ'
801: and alocc starts after first occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
802: gives blocc="abcdef2ghi" and alocc="j".
803: If occ is not found blocc is null and alocc is equal to in. Returns blocc
804: */
805: char *s, *t;
806: t=in;s=in;
807: while ((*in != occ) && (*in != '\0')){
808: *alocc++ = *in++;
809: }
810: if( *in == occ){
811: *(alocc)='\0';
812: s=++in;
813: }
814:
815: if (s == t) {/* occ not found */
816: *(alocc-(in-s))='\0';
817: in=s;
818: }
819: while ( *in != '\0'){
820: *blocc++ = *in++;
821: }
822:
823: *blocc='\0';
824: return t;
825: }
826: char *cutv(char *blocc, char *alocc, char *in, char occ)
827: {
828: /* cuts string in into blocc and alocc where blocc ends before last occurence of char 'occ'
829: and alocc starts after last occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
830: gives blocc="abcdef2ghi" and alocc="j".
831: If occ is not found blocc is null and alocc is equal to in. Returns alocc
832: */
833: char *s, *t;
834: t=in;s=in;
835: while (*in != '\0'){
836: while( *in == occ){
837: *blocc++ = *in++;
838: s=in;
839: }
840: *blocc++ = *in++;
841: }
842: if (s == t) /* occ not found */
843: *(blocc-(in-s))='\0';
844: else
845: *(blocc-(in-s)-1)='\0';
846: in=s;
847: while ( *in != '\0'){
848: *alocc++ = *in++;
849: }
850:
851: *alocc='\0';
852: return s;
853: }
854:
855: int nbocc(char *s, char occ)
856: {
857: int i,j=0;
858: int lg=20;
859: i=0;
860: lg=strlen(s);
861: for(i=0; i<= lg; i++) {
862: if (s[i] == occ ) j++;
863: }
864: return j;
865: }
866:
867: /* void cutv(char *u,char *v, char*t, char occ) */
868: /* { */
869: /* /\* cuts string t into u and v where u ends before last occurence of char 'occ' */
870: /* and v starts after last occurence of char 'occ' : ex cutv(u,v,"abcdef2ghi2j",'2') */
871: /* gives u="abcdef2ghi" and v="j" *\/ */
872: /* int i,lg,j,p=0; */
873: /* i=0; */
874: /* lg=strlen(t); */
875: /* for(j=0; j<=lg-1; j++) { */
876: /* if((t[j]!= occ) && (t[j+1]== occ)) p=j+1; */
877: /* } */
878:
879: /* for(j=0; j<p; j++) { */
880: /* (u[j] = t[j]); */
881: /* } */
882: /* u[p]='\0'; */
883:
884: /* for(j=0; j<= lg; j++) { */
885: /* if (j>=(p+1))(v[j-p-1] = t[j]); */
886: /* } */
887: /* } */
888:
889: #ifdef _WIN32
890: char * strsep(char **pp, const char *delim)
891: {
892: char *p, *q;
893:
894: if ((p = *pp) == NULL)
895: return 0;
896: if ((q = strpbrk (p, delim)) != NULL)
897: {
898: *pp = q + 1;
899: *q = '\0';
900: }
901: else
902: *pp = 0;
903: return p;
904: }
905: #endif
906:
907: /********************** nrerror ********************/
908:
909: void nrerror(char error_text[])
910: {
911: fprintf(stderr,"ERREUR ...\n");
912: fprintf(stderr,"%s\n",error_text);
913: exit(EXIT_FAILURE);
914: }
915: /*********************** vector *******************/
916: double *vector(int nl, int nh)
917: {
918: double *v;
919: v=(double *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(double)));
920: if (!v) nrerror("allocation failure in vector");
921: return v-nl+NR_END;
922: }
923:
924: /************************ free vector ******************/
925: void free_vector(double*v, int nl, int nh)
926: {
927: free((FREE_ARG)(v+nl-NR_END));
928: }
929:
930: /************************ivector *******************************/
931: int *ivector(long nl,long nh)
932: {
933: int *v;
934: v=(int *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(int)));
935: if (!v) nrerror("allocation failure in ivector");
936: return v-nl+NR_END;
937: }
938:
939: /******************free ivector **************************/
940: void free_ivector(int *v, long nl, long nh)
941: {
942: free((FREE_ARG)(v+nl-NR_END));
943: }
944:
945: /************************lvector *******************************/
946: long *lvector(long nl,long nh)
947: {
948: long *v;
949: v=(long *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(long)));
950: if (!v) nrerror("allocation failure in ivector");
951: return v-nl+NR_END;
952: }
953:
954: /******************free lvector **************************/
955: void free_lvector(long *v, long nl, long nh)
956: {
957: free((FREE_ARG)(v+nl-NR_END));
958: }
959:
960: /******************* imatrix *******************************/
961: int **imatrix(long nrl, long nrh, long ncl, long nch)
962: /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
963: {
964: long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
965: int **m;
966:
967: /* allocate pointers to rows */
968: m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
969: if (!m) nrerror("allocation failure 1 in matrix()");
970: m += NR_END;
971: m -= nrl;
972:
973:
974: /* allocate rows and set pointers to them */
975: m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
976: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
977: m[nrl] += NR_END;
978: m[nrl] -= ncl;
979:
980: for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
981:
982: /* return pointer to array of pointers to rows */
983: return m;
984: }
985:
986: /****************** free_imatrix *************************/
987: void free_imatrix(m,nrl,nrh,ncl,nch)
988: int **m;
989: long nch,ncl,nrh,nrl;
990: /* free an int matrix allocated by imatrix() */
991: {
992: free((FREE_ARG) (m[nrl]+ncl-NR_END));
993: free((FREE_ARG) (m+nrl-NR_END));
994: }
995:
996: /******************* matrix *******************************/
997: double **matrix(long nrl, long nrh, long ncl, long nch)
998: {
999: long i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
1000: double **m;
1001:
1002: m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
1003: if (!m) nrerror("allocation failure 1 in matrix()");
1004: m += NR_END;
1005: m -= nrl;
1006:
1007: m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
1008: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1009: m[nrl] += NR_END;
1010: m[nrl] -= ncl;
1011:
1012: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
1013: return m;
1014: /* print *(*(m+1)+70) or print m[1][70]; print m+1 or print &(m[1]) or &(m[1][0])
1015: m[i] = address of ith row of the table. &(m[i]) is its value which is another adress
1016: that of m[i][0]. In order to get the value p m[i][0] but it is unitialized.
1017: */
1018: }
1019:
1020: /*************************free matrix ************************/
1021: void free_matrix(double **m, long nrl, long nrh, long ncl, long nch)
1022: {
1023: free((FREE_ARG)(m[nrl]+ncl-NR_END));
1024: free((FREE_ARG)(m+nrl-NR_END));
1025: }
1026:
1027: /******************* ma3x *******************************/
1028: double ***ma3x(long nrl, long nrh, long ncl, long nch, long nll, long nlh)
1029: {
1030: long i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1, nlay=nlh-nll+1;
1031: double ***m;
1032:
1033: m=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
1034: if (!m) nrerror("allocation failure 1 in matrix()");
1035: m += NR_END;
1036: m -= nrl;
1037:
1038: m[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
1039: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1040: m[nrl] += NR_END;
1041: m[nrl] -= ncl;
1042:
1043: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
1044:
1045: m[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*nlay+NR_END)*sizeof(double)));
1046: if (!m[nrl][ncl]) nrerror("allocation failure 3 in matrix()");
1047: m[nrl][ncl] += NR_END;
1048: m[nrl][ncl] -= nll;
1049: for (j=ncl+1; j<=nch; j++)
1050: m[nrl][j]=m[nrl][j-1]+nlay;
1051:
1052: for (i=nrl+1; i<=nrh; i++) {
1053: m[i][ncl]=m[i-1l][ncl]+ncol*nlay;
1054: for (j=ncl+1; j<=nch; j++)
1055: m[i][j]=m[i][j-1]+nlay;
1056: }
1057: return m;
1058: /* gdb: p *(m+1) <=> p m[1] and p (m+1) <=> p (m+1) <=> p &(m[1])
1059: &(m[i][j][k]) <=> *((*(m+i) + j)+k)
1060: */
1061: }
1062:
1063: /*************************free ma3x ************************/
1064: void free_ma3x(double ***m, long nrl, long nrh, long ncl, long nch,long nll, long nlh)
1065: {
1066: free((FREE_ARG)(m[nrl][ncl]+ nll-NR_END));
1067: free((FREE_ARG)(m[nrl]+ncl-NR_END));
1068: free((FREE_ARG)(m+nrl-NR_END));
1069: }
1070:
1071: /*************** function subdirf ***********/
1072: char *subdirf(char fileres[])
1073: {
1074: /* Caution optionfilefiname is hidden */
1075: strcpy(tmpout,optionfilefiname);
1076: strcat(tmpout,"/"); /* Add to the right */
1077: strcat(tmpout,fileres);
1078: return tmpout;
1079: }
1080:
1081: /*************** function subdirf2 ***********/
1082: char *subdirf2(char fileres[], char *preop)
1083: {
1084:
1085: /* Caution optionfilefiname is hidden */
1086: strcpy(tmpout,optionfilefiname);
1087: strcat(tmpout,"/");
1088: strcat(tmpout,preop);
1089: strcat(tmpout,fileres);
1090: return tmpout;
1091: }
1092:
1093: /*************** function subdirf3 ***********/
1094: char *subdirf3(char fileres[], char *preop, char *preop2)
1095: {
1096:
1097: /* Caution optionfilefiname is hidden */
1098: strcpy(tmpout,optionfilefiname);
1099: strcat(tmpout,"/");
1100: strcat(tmpout,preop);
1101: strcat(tmpout,preop2);
1102: strcat(tmpout,fileres);
1103: return tmpout;
1104: }
1105:
1106: /***************** f1dim *************************/
1107: extern int ncom;
1108: extern double *pcom,*xicom;
1109: extern double (*nrfunc)(double []);
1110:
1111: double f1dim(double x)
1112: {
1113: int j;
1114: double f;
1115: double *xt;
1116:
1117: xt=vector(1,ncom);
1118: for (j=1;j<=ncom;j++) xt[j]=pcom[j]+x*xicom[j];
1119: f=(*nrfunc)(xt);
1120: free_vector(xt,1,ncom);
1121: return f;
1122: }
1123:
1124: /*****************brent *************************/
1125: double brent(double ax, double bx, double cx, double (*f)(double), double tol, double *xmin)
1126: {
1127: int iter;
1128: double a,b,d,etemp;
1129: double fu=0,fv,fw,fx;
1130: double ftemp;
1131: double p,q,r,tol1,tol2,u,v,w,x,xm;
1132: double e=0.0;
1133:
1134: a=(ax < cx ? ax : cx);
1135: b=(ax > cx ? ax : cx);
1136: x=w=v=bx;
1137: fw=fv=fx=(*f)(x);
1138: for (iter=1;iter<=ITMAX;iter++) {
1139: xm=0.5*(a+b);
1140: tol2=2.0*(tol1=tol*fabs(x)+ZEPS);
1141: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret)))*/
1142: printf(".");fflush(stdout);
1143: fprintf(ficlog,".");fflush(ficlog);
1144: #ifdef DEBUG
1145: 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);
1146: fprintf(ficlog,"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);
1147: /* if ((fabs(x-xm) <= (tol2-0.5*(b-a)))||(2.0*fabs(fu-ftemp) <= ftol*1.e-2*(fabs(fu)+fabs(ftemp)))) { */
1148: #endif
1149: if (fabs(x-xm) <= (tol2-0.5*(b-a))){
1150: *xmin=x;
1151: return fx;
1152: }
1153: ftemp=fu;
1154: if (fabs(e) > tol1) {
1155: r=(x-w)*(fx-fv);
1156: q=(x-v)*(fx-fw);
1157: p=(x-v)*q-(x-w)*r;
1158: q=2.0*(q-r);
1159: if (q > 0.0) p = -p;
1160: q=fabs(q);
1161: etemp=e;
1162: e=d;
1163: if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x))
1164: d=CGOLD*(e=(x >= xm ? a-x : b-x));
1165: else {
1166: d=p/q;
1167: u=x+d;
1168: if (u-a < tol2 || b-u < tol2)
1169: d=SIGN(tol1,xm-x);
1170: }
1171: } else {
1172: d=CGOLD*(e=(x >= xm ? a-x : b-x));
1173: }
1174: u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d));
1175: fu=(*f)(u);
1176: if (fu <= fx) {
1177: if (u >= x) a=x; else b=x;
1178: SHFT(v,w,x,u)
1179: SHFT(fv,fw,fx,fu)
1180: } else {
1181: if (u < x) a=u; else b=u;
1182: if (fu <= fw || w == x) {
1183: v=w;
1184: w=u;
1185: fv=fw;
1186: fw=fu;
1187: } else if (fu <= fv || v == x || v == w) {
1188: v=u;
1189: fv=fu;
1190: }
1191: }
1192: }
1193: nrerror("Too many iterations in brent");
1194: *xmin=x;
1195: return fx;
1196: }
1197:
1198: /****************** mnbrak ***********************/
1199:
1200: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, double *fc,
1201: double (*func)(double))
1202: {
1203: double ulim,u,r,q, dum;
1204: double fu;
1205:
1206: *fa=(*func)(*ax);
1207: *fb=(*func)(*bx);
1208: if (*fb > *fa) {
1209: SHFT(dum,*ax,*bx,dum)
1210: SHFT(dum,*fb,*fa,dum)
1211: }
1212: *cx=(*bx)+GOLD*(*bx-*ax);
1213: *fc=(*func)(*cx);
1214: while (*fb > *fc) {
1215: r=(*bx-*ax)*(*fb-*fc);
1216: q=(*bx-*cx)*(*fb-*fa);
1217: u=(*bx)-((*bx-*cx)*q-(*bx-*ax)*r)/
1218: (2.0*SIGN(FMAX(fabs(q-r),TINY),q-r));
1219: ulim=(*bx)+GLIMIT*(*cx-*bx);
1220: if ((*bx-u)*(u-*cx) > 0.0) {
1221: fu=(*func)(u);
1222: } else if ((*cx-u)*(u-ulim) > 0.0) {
1223: fu=(*func)(u);
1224: if (fu < *fc) {
1225: SHFT(*bx,*cx,u,*cx+GOLD*(*cx-*bx))
1226: SHFT(*fb,*fc,fu,(*func)(u))
1227: }
1228: } else if ((u-ulim)*(ulim-*cx) >= 0.0) {
1229: u=ulim;
1230: fu=(*func)(u);
1231: } else {
1232: u=(*cx)+GOLD*(*cx-*bx);
1233: fu=(*func)(u);
1234: }
1235: SHFT(*ax,*bx,*cx,u)
1236: SHFT(*fa,*fb,*fc,fu)
1237: }
1238: }
1239:
1240: /*************** linmin ************************/
1241:
1242: int ncom;
1243: double *pcom,*xicom;
1244: double (*nrfunc)(double []);
1245:
1246: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []))
1247: {
1248: double brent(double ax, double bx, double cx,
1249: double (*f)(double), double tol, double *xmin);
1250: double f1dim(double x);
1251: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb,
1252: double *fc, double (*func)(double));
1253: int j;
1254: double xx,xmin,bx,ax;
1255: double fx,fb,fa;
1256:
1257: ncom=n;
1258: pcom=vector(1,n);
1259: xicom=vector(1,n);
1260: nrfunc=func;
1261: for (j=1;j<=n;j++) {
1262: pcom[j]=p[j];
1263: xicom[j]=xi[j];
1264: }
1265: ax=0.0;
1266: xx=1.0;
1267: mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim);
1268: *fret=brent(ax,xx,bx,f1dim,TOL,&xmin);
1269: #ifdef DEBUG
1270: printf("retour brent fret=%.12e xmin=%.12e\n",*fret,xmin);
1271: fprintf(ficlog,"retour brent fret=%.12e xmin=%.12e\n",*fret,xmin);
1272: #endif
1273: for (j=1;j<=n;j++) {
1274: xi[j] *= xmin;
1275: p[j] += xi[j];
1276: }
1277: free_vector(xicom,1,n);
1278: free_vector(pcom,1,n);
1279: }
1280:
1281: char *asc_diff_time(long time_sec, char ascdiff[])
1282: {
1283: long sec_left, days, hours, minutes;
1284: days = (time_sec) / (60*60*24);
1285: sec_left = (time_sec) % (60*60*24);
1286: hours = (sec_left) / (60*60) ;
1287: sec_left = (sec_left) %(60*60);
1288: minutes = (sec_left) /60;
1289: sec_left = (sec_left) % (60);
1290: sprintf(ascdiff,"%ld day(s) %ld hour(s) %ld minute(s) %ld second(s)",days, hours, minutes, sec_left);
1291: return ascdiff;
1292: }
1293:
1294: /*************** powell ************************/
1295: void powell(double p[], double **xi, int n, double ftol, int *iter, double *fret,
1296: double (*func)(double []))
1297: {
1298: void linmin(double p[], double xi[], int n, double *fret,
1299: double (*func)(double []));
1300: int i,ibig,j;
1301: double del,t,*pt,*ptt,*xit;
1302: double fp,fptt;
1303: double *xits;
1304: int niterf, itmp;
1305:
1306: pt=vector(1,n);
1307: ptt=vector(1,n);
1308: xit=vector(1,n);
1309: xits=vector(1,n);
1310: *fret=(*func)(p);
1311: for (j=1;j<=n;j++) pt[j]=p[j];
1312: rcurr_time = time(NULL);
1313: for (*iter=1;;++(*iter)) {
1314: fp=(*fret);
1315: ibig=0;
1316: del=0.0;
1317: rlast_time=rcurr_time;
1318: /* (void) gettimeofday(&curr_time,&tzp); */
1319: rcurr_time = time(NULL);
1320: curr_time = *localtime(&rcurr_time);
1321: printf("\nPowell iter=%d -2*LL=%.12f %ld sec. %ld sec.",*iter,*fret, rcurr_time-rlast_time, rcurr_time-rstart_time);fflush(stdout);
1322: fprintf(ficlog,"\nPowell iter=%d -2*LL=%.12f %ld sec. %ld sec.",*iter,*fret,rcurr_time-rlast_time, rcurr_time-rstart_time); fflush(ficlog);
1323: /* fprintf(ficrespow,"%d %.12f %ld",*iter,*fret,curr_time.tm_sec-start_time.tm_sec); */
1324: for (i=1;i<=n;i++) {
1325: printf(" %d %.12f",i, p[i]);
1326: fprintf(ficlog," %d %.12lf",i, p[i]);
1327: fprintf(ficrespow," %.12lf", p[i]);
1328: }
1329: printf("\n");
1330: fprintf(ficlog,"\n");
1331: fprintf(ficrespow,"\n");fflush(ficrespow);
1332: if(*iter <=3){
1333: tml = *localtime(&rcurr_time);
1334: strcpy(strcurr,asctime(&tml));
1335: /* asctime_r(&tm,strcurr); */
1336: rforecast_time=rcurr_time;
1337: itmp = strlen(strcurr);
1338: if(strcurr[itmp-1]=='\n') /* Windows outputs with a new line */
1339: strcurr[itmp-1]='\0';
1340: printf("\nConsidering the time needed for this last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
1341: fprintf(ficlog,"\nConsidering the time needed for this last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
1342: for(niterf=10;niterf<=30;niterf+=10){
1343: rforecast_time=rcurr_time+(niterf-*iter)*(rcurr_time-rlast_time);
1344: forecast_time = *localtime(&rforecast_time);
1345: /* asctime_r(&tmf,strfor); */
1346: strcpy(strfor,asctime(&forecast_time));
1347: itmp = strlen(strfor);
1348: if(strfor[itmp-1]=='\n')
1349: strfor[itmp-1]='\0';
1350: printf(" - if your program needs %d iterations to converge, convergence will be \n reached in %s i.e.\n on %s (current time is %s);\n",niterf, asc_diff_time(rforecast_time-rcurr_time,tmpout),strfor,strcurr);
1351: fprintf(ficlog," - if your program needs %d iterations to converge, convergence will be \n reached in %s i.e.\n on %s (current time is %s);\n",niterf, asc_diff_time(rforecast_time-rcurr_time,tmpout),strfor,strcurr);
1352: }
1353: }
1354: for (i=1;i<=n;i++) {
1355: for (j=1;j<=n;j++) xit[j]=xi[j][i];
1356: fptt=(*fret);
1357: #ifdef DEBUG
1358: printf("fret=%lf \n",*fret);
1359: fprintf(ficlog,"fret=%lf \n",*fret);
1360: #endif
1361: printf("%d",i);fflush(stdout);
1362: fprintf(ficlog,"%d",i);fflush(ficlog);
1363: linmin(p,xit,n,fret,func);
1364: if (fabs(fptt-(*fret)) > del) {
1365: del=fabs(fptt-(*fret));
1366: ibig=i;
1367: }
1368: #ifdef DEBUG
1369: printf("%d %.12e",i,(*fret));
1370: fprintf(ficlog,"%d %.12e",i,(*fret));
1371: for (j=1;j<=n;j++) {
1372: xits[j]=FMAX(fabs(p[j]-pt[j]),1.e-5);
1373: printf(" x(%d)=%.12e",j,xit[j]);
1374: fprintf(ficlog," x(%d)=%.12e",j,xit[j]);
1375: }
1376: for(j=1;j<=n;j++) {
1377: printf(" p=%.12e",p[j]);
1378: fprintf(ficlog," p=%.12e",p[j]);
1379: }
1380: printf("\n");
1381: fprintf(ficlog,"\n");
1382: #endif
1383: }
1384: if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret))) {
1385: #ifdef DEBUG
1386: int k[2],l;
1387: k[0]=1;
1388: k[1]=-1;
1389: printf("Max: %.12e",(*func)(p));
1390: fprintf(ficlog,"Max: %.12e",(*func)(p));
1391: for (j=1;j<=n;j++) {
1392: printf(" %.12e",p[j]);
1393: fprintf(ficlog," %.12e",p[j]);
1394: }
1395: printf("\n");
1396: fprintf(ficlog,"\n");
1397: for(l=0;l<=1;l++) {
1398: for (j=1;j<=n;j++) {
1399: ptt[j]=p[j]+(p[j]-pt[j])*k[l];
1400: printf("l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
1401: fprintf(ficlog,"l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
1402: }
1403: printf("func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
1404: fprintf(ficlog,"func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
1405: }
1406: #endif
1407:
1408:
1409: free_vector(xit,1,n);
1410: free_vector(xits,1,n);
1411: free_vector(ptt,1,n);
1412: free_vector(pt,1,n);
1413: return;
1414: }
1415: if (*iter == ITMAX) nrerror("powell exceeding maximum iterations.");
1416: for (j=1;j<=n;j++) { /* Computes an extrapolated point */
1417: ptt[j]=2.0*p[j]-pt[j];
1418: xit[j]=p[j]-pt[j];
1419: pt[j]=p[j];
1420: }
1421: fptt=(*func)(ptt);
1422: if (fptt < fp) { /* If extrapolated point is better, decide if we keep that new direction or not */
1423: /* x1 f1=fp x2 f2=*fret x3 f3=fptt, xm fm */
1424: /* From x1 (P0) distance of x2 is at h and x3 is 2h */
1425: /* Let f"(x2) be the 2nd derivative equal everywhere. Then the parabolic through (x1,f1), (x2,f2) and (x3,f3)
1426: will reach at f3 = fm + h^2/2 f''m ; f" = (f1 -2f2 +f3 ) / h**2 */
1427: /* f1-f3 = delta(2h) = 2 h**2 f'' = 2(f1- 2f2 +f3) */
1428: /* Thus we compare delta(2h) with observed f1-f3 */
1429: /* or best gain on one ancient line 'del' with total gain f1-f2 = f1 - f2 - 'del' with del */
1430: /* t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)-del*SQR(fp-fptt); */
1431: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del);
1432: t= t- del*SQR(fp-fptt);
1433: printf("t1= %.12lf, t2= %.12lf, t=%.12lf\n", 2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del),del*SQR(fp-fptt),t);
1434: fprintf(ficlog,"t1= %.12lf, t2= %.12lf, t=%.12lf\n", 2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del),del*SQR(fp-fptt),t);
1435: #ifdef DEBUG
1436: printf("t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
1437: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
1438: fprintf(ficlog,"t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
1439: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
1440: printf("tt= %.12lf, t=%.12lf\n",2.0*(fp-2.0*(*fret)+fptt)*(fp-(*fret)-del)*(fp-(*fret)-del)-del*(fp-fptt)*(fp-fptt),t);
1441: fprintf(ficlog, "tt= %.12lf, t=%.12lf\n",2.0*(fp-2.0*(*fret)+fptt)*(fp-(*fret)-del)*(fp-(*fret)-del)-del*(fp-fptt)*(fp-fptt),t);
1442: #endif
1443: if (t < 0.0) { /* Then we use it for last direction */
1444: linmin(p,xit,n,fret,func); /* computes mean on the extrapolated direction.*/
1445: for (j=1;j<=n;j++) {
1446: xi[j][ibig]=xi[j][n]; /* Replace the direction with biggest decrease by n */
1447: xi[j][n]=xit[j]; /* and nth direction by the extrapolated */
1448: }
1449: printf("Gaining to use average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
1450: fprintf(ficlog,"Gaining to use average direction of P0 P%d instead of biggest increase direction :\n",n,ibig);
1451:
1452: #ifdef DEBUG
1453: for(j=1;j<=n;j++){
1454: printf(" %.12e",xit[j]);
1455: fprintf(ficlog," %.12e",xit[j]);
1456: }
1457: printf("\n");
1458: fprintf(ficlog,"\n");
1459: #endif
1460: }
1461: }
1462: }
1463: }
1464:
1465: /**** Prevalence limit (stable or period prevalence) ****************/
1466:
1467: double **prevalim(double **prlim, int nlstate, double x[], double age, double **oldm, double **savm, double ftolpl, int ij)
1468: {
1469: /* Computes the prevalence limit in each live state at age x by left multiplying the unit
1470: matrix by transitions matrix until convergence is reached */
1471:
1472: int i, ii,j,k;
1473: double min, max, maxmin, maxmax,sumnew=0.;
1474: /* double **matprod2(); */ /* test */
1475: double **out, cov[NCOVMAX+1], **pmij();
1476: double **newm;
1477: double agefin, delaymax=50 ; /* Max number of years to converge */
1478:
1479: for (ii=1;ii<=nlstate+ndeath;ii++)
1480: for (j=1;j<=nlstate+ndeath;j++){
1481: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
1482: }
1483:
1484: cov[1]=1.;
1485:
1486: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
1487: for(agefin=age-stepm/YEARM; agefin>=age-delaymax; agefin=agefin-stepm/YEARM){
1488: newm=savm;
1489: /* Covariates have to be included here again */
1490: cov[2]=agefin;
1491:
1492: for (k=1; k<=cptcovn;k++) {
1493: cov[2+k]=nbcode[Tvar[k]][codtab[ij][Tvar[k]]];
1494: /*printf("prevalim ij=%d k=%d Tvar[%d]=%d nbcode=%d cov=%lf codtab[%d][Tvar[%d]]=%d \n",ij,k, k, Tvar[k],nbcode[Tvar[k]][codtab[ij][Tvar[k]]],cov[2+k], ij, k, codtab[ij][Tvar[k]]);*/
1495: }
1496: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
1497: /* for (k=1; k<=cptcovprod;k++) /\* Useless *\/ */
1498: /* cov[2+Tprod[k]]=nbcode[Tvard[k][1]][codtab[ij][Tvard[k][1]]] * nbcode[Tvard[k][2]][codtab[ij][Tvard[k][2]]]; */
1499:
1500: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
1501: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
1502: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
1503: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
1504: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
1505: out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /* Bug Valgrind */
1506:
1507: savm=oldm;
1508: oldm=newm;
1509: maxmax=0.;
1510: for(j=1;j<=nlstate;j++){
1511: min=1.;
1512: max=0.;
1513: for(i=1; i<=nlstate; i++) {
1514: sumnew=0;
1515: for(k=1; k<=ndeath; k++) sumnew+=newm[i][nlstate+k];
1516: prlim[i][j]= newm[i][j]/(1-sumnew);
1517: /*printf(" prevalim i=%d, j=%d, prmlim[%d][%d]=%f, agefin=%d \n", i, j, i, j, prlim[i][j],(int)agefin);*/
1518: max=FMAX(max,prlim[i][j]);
1519: min=FMIN(min,prlim[i][j]);
1520: }
1521: maxmin=max-min;
1522: maxmax=FMAX(maxmax,maxmin);
1523: }
1524: if(maxmax < ftolpl){
1525: return prlim;
1526: }
1527: }
1528: }
1529:
1530: /*************** transition probabilities ***************/
1531:
1532: double **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
1533: {
1534: /* According to parameters values stored in x and the covariate's values stored in cov,
1535: computes the probability to be observed in state j being in state i by appying the
1536: model to the ncovmodel covariates (including constant and age).
1537: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
1538: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
1539: ncth covariate in the global vector x is given by the formula:
1540: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
1541: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
1542: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
1543: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
1544: Outputs ps[i][j] the probability to be observed in j being in j according to
1545: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
1546: */
1547: double s1, lnpijopii;
1548: /*double t34;*/
1549: int i,j,j1, nc, ii, jj;
1550:
1551: for(i=1; i<= nlstate; i++){
1552: for(j=1; j<i;j++){
1553: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
1554: /*lnpijopii += param[i][j][nc]*cov[nc];*/
1555: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
1556: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
1557: }
1558: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
1559: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
1560: }
1561: for(j=i+1; j<=nlstate+ndeath;j++){
1562: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
1563: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
1564: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
1565: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
1566: }
1567: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
1568: }
1569: }
1570:
1571: for(i=1; i<= nlstate; i++){
1572: s1=0;
1573: for(j=1; j<i; j++){
1574: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
1575: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
1576: }
1577: for(j=i+1; j<=nlstate+ndeath; j++){
1578: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
1579: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
1580: }
1581: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
1582: ps[i][i]=1./(s1+1.);
1583: /* Computing other pijs */
1584: for(j=1; j<i; j++)
1585: ps[i][j]= exp(ps[i][j])*ps[i][i];
1586: for(j=i+1; j<=nlstate+ndeath; j++)
1587: ps[i][j]= exp(ps[i][j])*ps[i][i];
1588: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
1589: } /* end i */
1590:
1591: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
1592: for(jj=1; jj<= nlstate+ndeath; jj++){
1593: ps[ii][jj]=0;
1594: ps[ii][ii]=1;
1595: }
1596: }
1597:
1598:
1599: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
1600: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
1601: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
1602: /* } */
1603: /* printf("\n "); */
1604: /* } */
1605: /* printf("\n ");printf("%lf ",cov[2]);*/
1606: /*
1607: for(i=1; i<= npar; i++) printf("%f ",x[i]);
1608: goto end;*/
1609: return ps;
1610: }
1611:
1612: /**************** Product of 2 matrices ******************/
1613:
1614: double **matprod2(double **out, double **in,int nrl, int nrh, int ncl, int nch, int ncolol, int ncoloh, double **b)
1615: {
1616: /* Computes the matrix product of in(1,nrh-nrl+1)(1,nch-ncl+1) times
1617: b(1,nch-ncl+1)(1,ncoloh-ncolol+1) into out(...) */
1618: /* in, b, out are matrice of pointers which should have been initialized
1619: before: only the contents of out is modified. The function returns
1620: a pointer to pointers identical to out */
1621: int i, j, k;
1622: for(i=nrl; i<= nrh; i++)
1623: for(k=ncolol; k<=ncoloh; k++){
1624: out[i][k]=0.;
1625: for(j=ncl; j<=nch; j++)
1626: out[i][k] +=in[i][j]*b[j][k];
1627: }
1628: return out;
1629: }
1630:
1631:
1632: /************* Higher Matrix Product ***************/
1633:
1634: double ***hpxij(double ***po, int nhstepm, double age, int hstepm, double *x, int nlstate, int stepm, double **oldm, double **savm, int ij )
1635: {
1636: /* Computes the transition matrix starting at age 'age' over
1637: 'nhstepm*hstepm*stepm' months (i.e. until
1638: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
1639: nhstepm*hstepm matrices.
1640: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
1641: (typically every 2 years instead of every month which is too big
1642: for the memory).
1643: Model is determined by parameters x and covariates have to be
1644: included manually here.
1645:
1646: */
1647:
1648: int i, j, d, h, k;
1649: double **out, cov[NCOVMAX+1];
1650: double **newm;
1651:
1652: /* Hstepm could be zero and should return the unit matrix */
1653: for (i=1;i<=nlstate+ndeath;i++)
1654: for (j=1;j<=nlstate+ndeath;j++){
1655: oldm[i][j]=(i==j ? 1.0 : 0.0);
1656: po[i][j][0]=(i==j ? 1.0 : 0.0);
1657: }
1658: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
1659: for(h=1; h <=nhstepm; h++){
1660: for(d=1; d <=hstepm; d++){
1661: newm=savm;
1662: /* Covariates have to be included here again */
1663: cov[1]=1.;
1664: cov[2]=age+((h-1)*hstepm + (d-1))*stepm/YEARM;
1665: for (k=1; k<=cptcovn;k++)
1666: cov[2+k]=nbcode[Tvar[k]][codtab[ij][Tvar[k]]];
1667: for (k=1; k<=cptcovage;k++)
1668: cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2];
1669: for (k=1; k<=cptcovprod;k++) /* Useless because included in cptcovn */
1670: cov[2+Tprod[k]]=nbcode[Tvard[k][1]][codtab[ij][Tvard[k][1]]]*nbcode[Tvard[k][2]][codtab[ij][Tvard[k][2]]];
1671:
1672:
1673: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
1674: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
1675: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
1676: pmij(pmmij,cov,ncovmodel,x,nlstate));
1677: savm=oldm;
1678: oldm=newm;
1679: }
1680: for(i=1; i<=nlstate+ndeath; i++)
1681: for(j=1;j<=nlstate+ndeath;j++) {
1682: po[i][j][h]=newm[i][j];
1683: /*if(h==nhstepm) printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]);*/
1684: }
1685: /*printf("h=%d ",h);*/
1686: } /* end h */
1687: /* printf("\n H=%d \n",h); */
1688: return po;
1689: }
1690:
1691: double myfunc(unsigned n, const double *p, double *grad, double (*func) (double [])){
1692: /* double (*nrfunc)(double []); */
1693: /* double myfunc(unsigned n, const double *p, double *grad, void *my_func_data){ */
1694: double fret;
1695: double *xt;
1696: int j;
1697:
1698: xt=vector(1,n);
1699: for (j=1;j<=n;j++)
1700: xt[j]=p[j];
1701: ++countcallfunc;
1702:
1703: fret=(*func)(xt);
1704: printf("Function = %.12lf ",fret);
1705: for (j=1;j<=n;j++)
1706: printf(" %d %.8lf", j, p[j]);
1707: printf("\n");
1708: free_vector(xt,1,n);
1709: return fret;
1710: }
1711: /*************** log-likelihood *************/
1712: double func( double *x)
1713: {
1714: int i, ii, j, k, mi, d, kk;
1715: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
1716: double **out;
1717: double sw; /* Sum of weights */
1718: double lli; /* Individual log likelihood */
1719: int s1, s2;
1720: double bbh, survp;
1721: long ipmx;
1722: /*extern weight */
1723: /* We are differentiating ll according to initial status */
1724: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
1725: /*for(i=1;i<imx;i++)
1726: printf(" %d\n",s[4][i]);
1727: */
1728: cov[1]=1.;
1729:
1730: for(k=1; k<=nlstate; k++) ll[k]=0.;
1731:
1732: if(mle==1){
1733: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1734: /* Computes the values of the ncovmodel covariates of the model
1735: depending if the covariates are fixed or variying (age dependent) and stores them in cov[]
1736: Then computes with function pmij which return a matrix p[i][j] giving the elementary probability
1737: to be observed in j being in i according to the model.
1738: */
1739: for (k=1; k<=cptcovn;k++){ /* Simple and product covariates without age* products */
1740: cov[2+k]=covar[Tvar[k]][i];
1741: }
1742: /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4]
1743: is 6, Tvar[3=age*V3] should not be computed because of age Tvar[4=V3*V2]
1744: has been calculated etc */
1745: for(mi=1; mi<= wav[i]-1; mi++){
1746: for (ii=1;ii<=nlstate+ndeath;ii++)
1747: for (j=1;j<=nlstate+ndeath;j++){
1748: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
1749: savm[ii][j]=(ii==j ? 1.0 : 0.0);
1750: }
1751: for(d=0; d<dh[mi][i]; d++){
1752: newm=savm;
1753: cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM;
1754: for (kk=1; kk<=cptcovage;kk++) {
1755: cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2]; /* Tage[kk] gives the data-covariate associated with age */
1756: }
1757: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
1758: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
1759: savm=oldm;
1760: oldm=newm;
1761: } /* end mult */
1762:
1763: /*lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ /* Original formula */
1764: /* But now since version 0.9 we anticipate for bias at large stepm.
1765: * If stepm is larger than one month (smallest stepm) and if the exact delay
1766: * (in months) between two waves is not a multiple of stepm, we rounded to
1767: * the nearest (and in case of equal distance, to the lowest) interval but now
1768: * we keep into memory the bias bh[mi][i] and also the previous matrix product
1769: * (i.e to dh[mi][i]-1) saved in 'savm'. Then we inter(extra)polate the
1770: * probability in order to take into account the bias as a fraction of the way
1771: * from savm to out if bh is negative or even beyond if bh is positive. bh varies
1772: * -stepm/2 to stepm/2 .
1773: * For stepm=1 the results are the same as for previous versions of Imach.
1774: * For stepm > 1 the results are less biased than in previous versions.
1775: */
1776: s1=s[mw[mi][i]][i];
1777: s2=s[mw[mi+1][i]][i];
1778: bbh=(double)bh[mi][i]/(double)stepm;
1779: /* bias bh is positive if real duration
1780: * is higher than the multiple of stepm and negative otherwise.
1781: */
1782: /* lli= (savm[s1][s2]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2]));*/
1783: if( s2 > nlstate){
1784: /* i.e. if s2 is a death state and if the date of death is known
1785: then the contribution to the likelihood is the probability to
1786: die between last step unit time and current step unit time,
1787: which is also equal to probability to die before dh
1788: minus probability to die before dh-stepm .
1789: In version up to 0.92 likelihood was computed
1790: as if date of death was unknown. Death was treated as any other
1791: health state: the date of the interview describes the actual state
1792: and not the date of a change in health state. The former idea was
1793: to consider that at each interview the state was recorded
1794: (healthy, disable or death) and IMaCh was corrected; but when we
1795: introduced the exact date of death then we should have modified
1796: the contribution of an exact death to the likelihood. This new
1797: contribution is smaller and very dependent of the step unit
1798: stepm. It is no more the probability to die between last interview
1799: and month of death but the probability to survive from last
1800: interview up to one month before death multiplied by the
1801: probability to die within a month. Thanks to Chris
1802: Jackson for correcting this bug. Former versions increased
1803: mortality artificially. The bad side is that we add another loop
1804: which slows down the processing. The difference can be up to 10%
1805: lower mortality.
1806: */
1807: lli=log(out[s1][s2] - savm[s1][s2]);
1808:
1809:
1810: } else if (s2==-2) {
1811: for (j=1,survp=0. ; j<=nlstate; j++)
1812: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
1813: /*survp += out[s1][j]; */
1814: lli= log(survp);
1815: }
1816:
1817: else if (s2==-4) {
1818: for (j=3,survp=0. ; j<=nlstate; j++)
1819: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
1820: lli= log(survp);
1821: }
1822:
1823: else if (s2==-5) {
1824: for (j=1,survp=0. ; j<=2; j++)
1825: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
1826: lli= log(survp);
1827: }
1828:
1829: else{
1830: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
1831: /* lli= (savm[s1][s2]>(double)1.e-8 ?log((1.+bbh)*out[s1][s2]- bbh*(savm[s1][s2])):log((1.+bbh)*out[s1][s2]));*/ /* linear interpolation */
1832: }
1833: /*lli=(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]);*/
1834: /*if(lli ==000.0)*/
1835: /*printf("bbh= %f lli=%f savm=%f out=%f %d\n",bbh,lli,savm[s1][s2], out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]],i); */
1836: ipmx +=1;
1837: sw += weight[i];
1838: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1839: } /* end of wave */
1840: } /* end of individual */
1841: } else if(mle==2){
1842: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1843: for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i];
1844: for(mi=1; mi<= wav[i]-1; mi++){
1845: for (ii=1;ii<=nlstate+ndeath;ii++)
1846: for (j=1;j<=nlstate+ndeath;j++){
1847: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
1848: savm[ii][j]=(ii==j ? 1.0 : 0.0);
1849: }
1850: for(d=0; d<=dh[mi][i]; d++){
1851: newm=savm;
1852: cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM;
1853: for (kk=1; kk<=cptcovage;kk++) {
1854: cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
1855: }
1856: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
1857: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
1858: savm=oldm;
1859: oldm=newm;
1860: } /* end mult */
1861:
1862: s1=s[mw[mi][i]][i];
1863: s2=s[mw[mi+1][i]][i];
1864: bbh=(double)bh[mi][i]/(double)stepm;
1865: lli= (savm[s1][s2]>(double)1.e-8 ?log((1.+bbh)*out[s1][s2]- bbh*(savm[s1][s2])):log((1.+bbh)*out[s1][s2])); /* linear interpolation */
1866: ipmx +=1;
1867: sw += weight[i];
1868: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1869: } /* end of wave */
1870: } /* end of individual */
1871: } else if(mle==3){ /* exponential inter-extrapolation */
1872: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1873: for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i];
1874: for(mi=1; mi<= wav[i]-1; mi++){
1875: for (ii=1;ii<=nlstate+ndeath;ii++)
1876: for (j=1;j<=nlstate+ndeath;j++){
1877: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
1878: savm[ii][j]=(ii==j ? 1.0 : 0.0);
1879: }
1880: for(d=0; d<dh[mi][i]; d++){
1881: newm=savm;
1882: cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM;
1883: for (kk=1; kk<=cptcovage;kk++) {
1884: cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
1885: }
1886: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
1887: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
1888: savm=oldm;
1889: oldm=newm;
1890: } /* end mult */
1891:
1892: s1=s[mw[mi][i]][i];
1893: s2=s[mw[mi+1][i]][i];
1894: bbh=(double)bh[mi][i]/(double)stepm;
1895: lli= (savm[s1][s2]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2])); /* exponential inter-extrapolation */
1896: ipmx +=1;
1897: sw += weight[i];
1898: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1899: } /* end of wave */
1900: } /* end of individual */
1901: }else if (mle==4){ /* ml=4 no inter-extrapolation */
1902: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1903: for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i];
1904: for(mi=1; mi<= wav[i]-1; mi++){
1905: for (ii=1;ii<=nlstate+ndeath;ii++)
1906: for (j=1;j<=nlstate+ndeath;j++){
1907: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
1908: savm[ii][j]=(ii==j ? 1.0 : 0.0);
1909: }
1910: for(d=0; d<dh[mi][i]; d++){
1911: newm=savm;
1912: cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM;
1913: for (kk=1; kk<=cptcovage;kk++) {
1914: cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
1915: }
1916:
1917: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
1918: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
1919: savm=oldm;
1920: oldm=newm;
1921: } /* end mult */
1922:
1923: s1=s[mw[mi][i]][i];
1924: s2=s[mw[mi+1][i]][i];
1925: if( s2 > nlstate){
1926: lli=log(out[s1][s2] - savm[s1][s2]);
1927: }else{
1928: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
1929: }
1930: ipmx +=1;
1931: sw += weight[i];
1932: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1933: /* printf("i=%6d s1=%1d s2=%1d mi=%1d mw=%1d dh=%3d prob=%10.6f w=%6.4f out=%10.6f sav=%10.6f\n",i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],out[s1][s2],savm[s1][s2]); */
1934: } /* end of wave */
1935: } /* end of individual */
1936: }else{ /* ml=5 no inter-extrapolation no jackson =0.8a */
1937: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1938: for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i];
1939: for(mi=1; mi<= wav[i]-1; mi++){
1940: for (ii=1;ii<=nlstate+ndeath;ii++)
1941: for (j=1;j<=nlstate+ndeath;j++){
1942: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
1943: savm[ii][j]=(ii==j ? 1.0 : 0.0);
1944: }
1945: for(d=0; d<dh[mi][i]; d++){
1946: newm=savm;
1947: cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM;
1948: for (kk=1; kk<=cptcovage;kk++) {
1949: cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
1950: }
1951:
1952: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
1953: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
1954: savm=oldm;
1955: oldm=newm;
1956: } /* end mult */
1957:
1958: s1=s[mw[mi][i]][i];
1959: s2=s[mw[mi+1][i]][i];
1960: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
1961: ipmx +=1;
1962: sw += weight[i];
1963: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1964: /*printf("i=%6d s1=%1d s2=%1d mi=%1d mw=%1d dh=%3d prob=%10.6f w=%6.4f out=%10.6f sav=%10.6f\n",i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],out[s1][s2],savm[s1][s2]);*/
1965: } /* end of wave */
1966: } /* end of individual */
1967: } /* End of if */
1968: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
1969: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
1970: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
1971: return -l;
1972: }
1973:
1974: /*************** log-likelihood *************/
1975: double funcone( double *x)
1976: {
1977: /* Same as likeli but slower because of a lot of printf and if */
1978: int i, ii, j, k, mi, d, kk;
1979: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
1980: double **out;
1981: double lli; /* Individual log likelihood */
1982: double llt;
1983: int s1, s2;
1984: double bbh, survp;
1985: /*extern weight */
1986: /* We are differentiating ll according to initial status */
1987: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
1988: /*for(i=1;i<imx;i++)
1989: printf(" %d\n",s[4][i]);
1990: */
1991: cov[1]=1.;
1992:
1993: for(k=1; k<=nlstate; k++) ll[k]=0.;
1994:
1995: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1996: for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i];
1997: for(mi=1; mi<= wav[i]-1; mi++){
1998: for (ii=1;ii<=nlstate+ndeath;ii++)
1999: for (j=1;j<=nlstate+ndeath;j++){
2000: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
2001: savm[ii][j]=(ii==j ? 1.0 : 0.0);
2002: }
2003: for(d=0; d<dh[mi][i]; d++){
2004: newm=savm;
2005: cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM;
2006: for (kk=1; kk<=cptcovage;kk++) {
2007: cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
2008: }
2009: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
2010: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
2011: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
2012: /* out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath, */
2013: /* 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate)); */
2014: savm=oldm;
2015: oldm=newm;
2016: } /* end mult */
2017:
2018: s1=s[mw[mi][i]][i];
2019: s2=s[mw[mi+1][i]][i];
2020: bbh=(double)bh[mi][i]/(double)stepm;
2021: /* bias is positive if real duration
2022: * is higher than the multiple of stepm and negative otherwise.
2023: */
2024: if( s2 > nlstate && (mle <5) ){ /* Jackson */
2025: lli=log(out[s1][s2] - savm[s1][s2]);
2026: } else if (s2==-2) {
2027: for (j=1,survp=0. ; j<=nlstate; j++)
2028: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
2029: lli= log(survp);
2030: }else if (mle==1){
2031: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
2032: } else if(mle==2){
2033: lli= (savm[s1][s2]>(double)1.e-8 ?log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]):log((1.+bbh)*out[s1][s2])); /* linear interpolation */
2034: } else if(mle==3){ /* exponential inter-extrapolation */
2035: lli= (savm[s1][s2]>(double)1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2])); /* exponential inter-extrapolation */
2036: } else if (mle==4){ /* mle=4 no inter-extrapolation */
2037: lli=log(out[s1][s2]); /* Original formula */
2038: } else{ /* mle=0 back to 1 */
2039: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
2040: /*lli=log(out[s1][s2]); */ /* Original formula */
2041: } /* End of if */
2042: ipmx +=1;
2043: sw += weight[i];
2044: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
2045: /*printf("i=%6d s1=%1d s2=%1d mi=%1d mw=%1d dh=%3d prob=%10.6f w=%6.4f out=%10.6f sav=%10.6f\n",i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],out[s1][s2],savm[s1][s2]); */
2046: if(globpr){
2047: fprintf(ficresilk,"%9ld %6d %2d %2d %1d %1d %3d %11.6f %8.4f\
2048: %11.6f %11.6f %11.6f ", \
2049: num[i],i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],
2050: 2*weight[i]*lli,out[s1][s2],savm[s1][s2]);
2051: for(k=1,llt=0.,l=0.; k<=nlstate; k++){
2052: llt +=ll[k]*gipmx/gsw;
2053: fprintf(ficresilk," %10.6f",-ll[k]*gipmx/gsw);
2054: }
2055: fprintf(ficresilk," %10.6f\n", -llt);
2056: }
2057: } /* end of wave */
2058: } /* end of individual */
2059: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
2060: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
2061: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
2062: if(globpr==0){ /* First time we count the contributions and weights */
2063: gipmx=ipmx;
2064: gsw=sw;
2065: }
2066: return -l;
2067: }
2068:
2069:
2070: /*************** function likelione ***********/
2071: void likelione(FILE *ficres,double p[], int npar, int nlstate, int *globpri, long *ipmx, double *sw, double *fretone, double (*funcone)(double []))
2072: {
2073: /* This routine should help understanding what is done with
2074: the selection of individuals/waves and
2075: to check the exact contribution to the likelihood.
2076: Plotting could be done.
2077: */
2078: int k;
2079:
2080: if(*globpri !=0){ /* Just counts and sums, no printings */
2081: strcpy(fileresilk,"ilk");
2082: strcat(fileresilk,fileres);
2083: if((ficresilk=fopen(fileresilk,"w"))==NULL) {
2084: printf("Problem with resultfile: %s\n", fileresilk);
2085: fprintf(ficlog,"Problem with resultfile: %s\n", fileresilk);
2086: }
2087: fprintf(ficresilk, "#individual(line's_record) s1 s2 wave# effective_wave# number_of_matrices_product pij weight -2ln(pij)*weight 0pij_x 0pij_(x-stepm) cumulating_loglikeli_by_health_state(reweighted=-2ll*weightXnumber_of_contribs/sum_of_weights) and_total\n");
2088: fprintf(ficresilk, "#num_i i s1 s2 mi mw dh likeli weight 2wlli out sav ");
2089: /* i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],2*weight[i]*lli,out[s1][s2],savm[s1][s2]); */
2090: for(k=1; k<=nlstate; k++)
2091: fprintf(ficresilk," -2*gipw/gsw*weight*ll[%d]++",k);
2092: fprintf(ficresilk," -2*gipw/gsw*weight*ll(total)\n");
2093: }
2094:
2095: *fretone=(*funcone)(p);
2096: if(*globpri !=0){
2097: fclose(ficresilk);
2098: fprintf(fichtm,"\n<br>File of contributions to the likelihood: <a href=\"%s\">%s</a><br>\n",subdirf(fileresilk),subdirf(fileresilk));
2099: fflush(fichtm);
2100: }
2101: return;
2102: }
2103:
2104:
2105: /*********** Maximum Likelihood Estimation ***************/
2106:
2107: void mlikeli(FILE *ficres,double p[], int npar, int ncovmodel, int nlstate, double ftol, double (*func)(double []))
2108: {
2109: int i,j, iter;
2110: double **xi;
2111: double fret;
2112: double fretone; /* Only one call to likelihood */
2113: /* char filerespow[FILENAMELENGTH];*/
2114: xi=matrix(1,npar,1,npar);
2115: for (i=1;i<=npar;i++)
2116: for (j=1;j<=npar;j++)
2117: xi[i][j]=(i==j ? 1.0 : 0.0);
2118: printf("Powell\n"); fprintf(ficlog,"Powell\n");
2119: strcpy(filerespow,"pow");
2120: strcat(filerespow,fileres);
2121: if((ficrespow=fopen(filerespow,"w"))==NULL) {
2122: printf("Problem with resultfile: %s\n", filerespow);
2123: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
2124: }
2125: fprintf(ficrespow,"# Powell\n# iter -2*LL");
2126: for (i=1;i<=nlstate;i++)
2127: for(j=1;j<=nlstate+ndeath;j++)
2128: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
2129: fprintf(ficrespow,"\n");
2130: #ifdef POWELL
2131: powell(p,xi,npar,ftol,&iter,&fret,func);
2132: #endif
2133: #ifdef NLOPT
2134: int creturn;
2135: nlopt_opt opt;
2136: double lb[9] = { -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL }; /* lower bounds */
2137: double minf; /* the minimum objective value, upon return */
2138: /* opt = nlopt_create(NLOPT_LN_NEWUOA,npar); */
2139: opt = nlopt_create(NLOPT_LN_BOBYQA,npar);
2140: nlopt_set_lower_bounds(opt, lb);
2141: printf(" Func %.12lf \n",myfunc(npar,*(&p),NULL,func));
2142: /* printf(" Func %.12lf /n",myfunc(npar,p,NULL,NULL)); */
2143: /* nlopt_set_min_objective(opt, myfunc, NULL); */
2144: nlopt_set_min_objective(opt, myfunc, func);
2145: nlopt_set_xtol_rel(opt, ftol);
2146: /* printf(" Func %.12lf /n",myfunc(npar,p,NULL,func)); */
2147: if ((creturn=nlopt_optimize(opt, p, &minf)) < 0) {
2148: printf("nlopt failed! %d\n",creturn);
2149: }
2150: else {
2151: printf("found minimum after %d evaluations (NLOPT=%d)\n", countcallfunc ,NLOPT);
2152: printf("found minimum at f(%g,%g) = %0.10g\n", p[0], p[1], minf);
2153: iter=countcallfunc; /* not equal */
2154: }
2155: nlopt_destroy(opt);
2156: #endif
2157: free_matrix(xi,1,npar,1,npar);
2158: fclose(ficrespow);
2159: printf("\n#Number of iterations = %d, -2 Log likelihood = %.12f\n",iter,func(p));
2160: fprintf(ficlog,"\n#Number of iterations = %d, -2 Log likelihood = %.12f \n",iter,func(p));
2161: fprintf(ficres,"#Number of iterations = %d, -2 Log likelihood = %.12f \n",iter,func(p));
2162:
2163: }
2164:
2165: /**** Computes Hessian and covariance matrix ***/
2166: void hesscov(double **matcov, double p[], int npar, double delti[], double ftolhess, double (*func)(double []))
2167: {
2168: double **a,**y,*x,pd;
2169: double **hess;
2170: int i, j,jk;
2171: int *indx;
2172:
2173: double hessii(double p[], double delta, int theta, double delti[],double (*func)(double []),int npar);
2174: double hessij(double p[], double delti[], int i, int j,double (*func)(double []),int npar);
2175: void lubksb(double **a, int npar, int *indx, double b[]) ;
2176: void ludcmp(double **a, int npar, int *indx, double *d) ;
2177: double gompertz(double p[]);
2178: hess=matrix(1,npar,1,npar);
2179:
2180: printf("\nCalculation of the hessian matrix. Wait...\n");
2181: fprintf(ficlog,"\nCalculation of the hessian matrix. Wait...\n");
2182: for (i=1;i<=npar;i++){
2183: printf("%d",i);fflush(stdout);
2184: fprintf(ficlog,"%d",i);fflush(ficlog);
2185:
2186: hess[i][i]=hessii(p,ftolhess,i,delti,func,npar);
2187:
2188: /* printf(" %f ",p[i]);
2189: printf(" %lf %lf %lf",hess[i][i],ftolhess,delti[i]);*/
2190: }
2191:
2192: for (i=1;i<=npar;i++) {
2193: for (j=1;j<=npar;j++) {
2194: if (j>i) {
2195: printf(".%d%d",i,j);fflush(stdout);
2196: fprintf(ficlog,".%d%d",i,j);fflush(ficlog);
2197: hess[i][j]=hessij(p,delti,i,j,func,npar);
2198:
2199: hess[j][i]=hess[i][j];
2200: /*printf(" %lf ",hess[i][j]);*/
2201: }
2202: }
2203: }
2204: printf("\n");
2205: fprintf(ficlog,"\n");
2206:
2207: printf("\nInverting the hessian to get the covariance matrix. Wait...\n");
2208: fprintf(ficlog,"\nInverting the hessian to get the covariance matrix. Wait...\n");
2209:
2210: a=matrix(1,npar,1,npar);
2211: y=matrix(1,npar,1,npar);
2212: x=vector(1,npar);
2213: indx=ivector(1,npar);
2214: for (i=1;i<=npar;i++)
2215: for (j=1;j<=npar;j++) a[i][j]=hess[i][j];
2216: ludcmp(a,npar,indx,&pd);
2217:
2218: for (j=1;j<=npar;j++) {
2219: for (i=1;i<=npar;i++) x[i]=0;
2220: x[j]=1;
2221: lubksb(a,npar,indx,x);
2222: for (i=1;i<=npar;i++){
2223: matcov[i][j]=x[i];
2224: }
2225: }
2226:
2227: printf("\n#Hessian matrix#\n");
2228: fprintf(ficlog,"\n#Hessian matrix#\n");
2229: for (i=1;i<=npar;i++) {
2230: for (j=1;j<=npar;j++) {
2231: printf("%.3e ",hess[i][j]);
2232: fprintf(ficlog,"%.3e ",hess[i][j]);
2233: }
2234: printf("\n");
2235: fprintf(ficlog,"\n");
2236: }
2237:
2238: /* Recompute Inverse */
2239: for (i=1;i<=npar;i++)
2240: for (j=1;j<=npar;j++) a[i][j]=matcov[i][j];
2241: ludcmp(a,npar,indx,&pd);
2242:
2243: /* printf("\n#Hessian matrix recomputed#\n");
2244:
2245: for (j=1;j<=npar;j++) {
2246: for (i=1;i<=npar;i++) x[i]=0;
2247: x[j]=1;
2248: lubksb(a,npar,indx,x);
2249: for (i=1;i<=npar;i++){
2250: y[i][j]=x[i];
2251: printf("%.3e ",y[i][j]);
2252: fprintf(ficlog,"%.3e ",y[i][j]);
2253: }
2254: printf("\n");
2255: fprintf(ficlog,"\n");
2256: }
2257: */
2258:
2259: free_matrix(a,1,npar,1,npar);
2260: free_matrix(y,1,npar,1,npar);
2261: free_vector(x,1,npar);
2262: free_ivector(indx,1,npar);
2263: free_matrix(hess,1,npar,1,npar);
2264:
2265:
2266: }
2267:
2268: /*************** hessian matrix ****************/
2269: double hessii(double x[], double delta, int theta, double delti[], double (*func)(double []), int npar)
2270: {
2271: int i;
2272: int l=1, lmax=20;
2273: double k1,k2;
2274: double p2[MAXPARM+1]; /* identical to x */
2275: double res;
2276: double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4;
2277: double fx;
2278: int k=0,kmax=10;
2279: double l1;
2280:
2281: fx=func(x);
2282: for (i=1;i<=npar;i++) p2[i]=x[i];
2283: for(l=0 ; l <=lmax; l++){ /* Enlarging the zone around the Maximum */
2284: l1=pow(10,l);
2285: delts=delt;
2286: for(k=1 ; k <kmax; k=k+1){
2287: delt = delta*(l1*k);
2288: p2[theta]=x[theta] +delt;
2289: k1=func(p2)-fx; /* Might be negative if too close to the theoretical maximum */
2290: p2[theta]=x[theta]-delt;
2291: k2=func(p2)-fx;
2292: /*res= (k1-2.0*fx+k2)/delt/delt; */
2293: res= (k1+k2)/delt/delt/2.; /* Divided by because L and not 2*L */
2294:
2295: #ifdef DEBUGHESS
2296: 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);
2297: fprintf(ficlog,"%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);
2298: #endif
2299: /*if(fabs(k1-2.0*fx+k2) <1.e-13){ */
2300: if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)){
2301: k=kmax;
2302: }
2303: else if((k1 >khi/nkhif) || (k2 >khi/nkhif)){ /* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. */
2304: k=kmax; l=lmax*10.;
2305: }
2306: else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){
2307: delts=delt;
2308: }
2309: }
2310: }
2311: delti[theta]=delts;
2312: return res;
2313:
2314: }
2315:
2316: double hessij( double x[], double delti[], int thetai,int thetaj,double (*func)(double []),int npar)
2317: {
2318: int i;
2319: int l=1, l1, lmax=20;
2320: double k1,k2,k3,k4,res,fx;
2321: double p2[MAXPARM+1];
2322: int k;
2323:
2324: fx=func(x);
2325: for (k=1; k<=2; k++) {
2326: for (i=1;i<=npar;i++) p2[i]=x[i];
2327: p2[thetai]=x[thetai]+delti[thetai]/k;
2328: p2[thetaj]=x[thetaj]+delti[thetaj]/k;
2329: k1=func(p2)-fx;
2330:
2331: p2[thetai]=x[thetai]+delti[thetai]/k;
2332: p2[thetaj]=x[thetaj]-delti[thetaj]/k;
2333: k2=func(p2)-fx;
2334:
2335: p2[thetai]=x[thetai]-delti[thetai]/k;
2336: p2[thetaj]=x[thetaj]+delti[thetaj]/k;
2337: k3=func(p2)-fx;
2338:
2339: p2[thetai]=x[thetai]-delti[thetai]/k;
2340: p2[thetaj]=x[thetaj]-delti[thetaj]/k;
2341: k4=func(p2)-fx;
2342: res=(k1-k2-k3+k4)/4.0/delti[thetai]*k/delti[thetaj]*k/2.; /* Because of L not 2*L */
2343: #ifdef DEBUG
2344: 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);
2345: fprintf(ficlog,"%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);
2346: #endif
2347: }
2348: return res;
2349: }
2350:
2351: /************** Inverse of matrix **************/
2352: void ludcmp(double **a, int n, int *indx, double *d)
2353: {
2354: int i,imax,j,k;
2355: double big,dum,sum,temp;
2356: double *vv;
2357:
2358: vv=vector(1,n);
2359: *d=1.0;
2360: for (i=1;i<=n;i++) {
2361: big=0.0;
2362: for (j=1;j<=n;j++)
2363: if ((temp=fabs(a[i][j])) > big) big=temp;
2364: if (big == 0.0) nrerror("Singular matrix in routine ludcmp");
2365: vv[i]=1.0/big;
2366: }
2367: for (j=1;j<=n;j++) {
2368: for (i=1;i<j;i++) {
2369: sum=a[i][j];
2370: for (k=1;k<i;k++) sum -= a[i][k]*a[k][j];
2371: a[i][j]=sum;
2372: }
2373: big=0.0;
2374: for (i=j;i<=n;i++) {
2375: sum=a[i][j];
2376: for (k=1;k<j;k++)
2377: sum -= a[i][k]*a[k][j];
2378: a[i][j]=sum;
2379: if ( (dum=vv[i]*fabs(sum)) >= big) {
2380: big=dum;
2381: imax=i;
2382: }
2383: }
2384: if (j != imax) {
2385: for (k=1;k<=n;k++) {
2386: dum=a[imax][k];
2387: a[imax][k]=a[j][k];
2388: a[j][k]=dum;
2389: }
2390: *d = -(*d);
2391: vv[imax]=vv[j];
2392: }
2393: indx[j]=imax;
2394: if (a[j][j] == 0.0) a[j][j]=TINY;
2395: if (j != n) {
2396: dum=1.0/(a[j][j]);
2397: for (i=j+1;i<=n;i++) a[i][j] *= dum;
2398: }
2399: }
2400: free_vector(vv,1,n); /* Doesn't work */
2401: ;
2402: }
2403:
2404: void lubksb(double **a, int n, int *indx, double b[])
2405: {
2406: int i,ii=0,ip,j;
2407: double sum;
2408:
2409: for (i=1;i<=n;i++) {
2410: ip=indx[i];
2411: sum=b[ip];
2412: b[ip]=b[i];
2413: if (ii)
2414: for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j];
2415: else if (sum) ii=i;
2416: b[i]=sum;
2417: }
2418: for (i=n;i>=1;i--) {
2419: sum=b[i];
2420: for (j=i+1;j<=n;j++) sum -= a[i][j]*b[j];
2421: b[i]=sum/a[i][i];
2422: }
2423: }
2424:
2425: void pstamp(FILE *fichier)
2426: {
2427: fprintf(fichier,"# %s.%s\n#%s\n#%s\n# %s", optionfilefiname,optionfilext,version,fullversion,strstart);
2428: }
2429:
2430: /************ Frequencies ********************/
2431: void freqsummary(char fileres[], int iagemin, int iagemax, int **s, double **agev, int nlstate, int imx, int *Tvaraff, int **nbcode, int *ncodemax,double **mint,double **anint, char strstart[])
2432: { /* Some frequencies */
2433:
2434: int i, m, jk, k1,i1, j1, bool, z1,j;
2435: int first;
2436: double ***freq; /* Frequencies */
2437: double *pp, **prop;
2438: double pos,posprop, k2, dateintsum=0,k2cpt=0;
2439: char fileresp[FILENAMELENGTH];
2440:
2441: pp=vector(1,nlstate);
2442: prop=matrix(1,nlstate,iagemin,iagemax+3);
2443: strcpy(fileresp,"p");
2444: strcat(fileresp,fileres);
2445: if((ficresp=fopen(fileresp,"w"))==NULL) {
2446: printf("Problem with prevalence resultfile: %s\n", fileresp);
2447: fprintf(ficlog,"Problem with prevalence resultfile: %s\n", fileresp);
2448: exit(0);
2449: }
2450: freq= ma3x(-5,nlstate+ndeath,-5,nlstate+ndeath,iagemin,iagemax+3);
2451: j1=0;
2452:
2453: j=cptcoveff;
2454: if (cptcovn<1) {j=1;ncodemax[1]=1;}
2455:
2456: first=1;
2457:
2458: /* for(k1=1; k1<=j ; k1++){ /* Loop on covariates */
2459: /* for(i1=1; i1<=ncodemax[k1];i1++){ /* Now it is 2 */
2460: /* j1++;
2461: */
2462: for (j1 = 1; j1 <= (int) pow(2,cptcoveff); j1++){
2463: /*printf("cptcoveff=%d Tvaraff=%d", cptcoveff,Tvaraff[1]);
2464: scanf("%d", i);*/
2465: for (i=-5; i<=nlstate+ndeath; i++)
2466: for (jk=-5; jk<=nlstate+ndeath; jk++)
2467: for(m=iagemin; m <= iagemax+3; m++)
2468: freq[i][jk][m]=0;
2469:
2470: for (i=1; i<=nlstate; i++)
2471: for(m=iagemin; m <= iagemax+3; m++)
2472: prop[i][m]=0;
2473:
2474: dateintsum=0;
2475: k2cpt=0;
2476: for (i=1; i<=imx; i++) {
2477: bool=1;
2478: if (cptcovn>0) { /* Filter is here: Must be looked at for model=V1+V2+V3+V4 */
2479: for (z1=1; z1<=cptcoveff; z1++)
2480: if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtab[j1][z1]]){
2481: /* Tests if the value of each of the covariates of i is equal to filter j1 */
2482: bool=0;
2483: /* printf("bool=%d i=%d, z1=%d, Tvaraff[%d]=%d, covar[Tvarff][%d]=%2f, codtab[%d][%d]=%d, nbcode[Tvaraff][codtab[%d][%d]=%d, j1=%d\n",
2484: bool,i,z1, z1, Tvaraff[z1],i,covar[Tvaraff[z1]][i],j1,z1,codtab[j1][z1],
2485: j1,z1,nbcode[Tvaraff[z1]][codtab[j1][z1]],j1);*/
2486: /* For j1=7 in V1+V2+V3+V4 = 0 1 1 0 and codtab[7][3]=1 and nbcde[3][?]=1*/
2487: }
2488: }
2489:
2490: if (bool==1){
2491: for(m=firstpass; m<=lastpass; m++){
2492: k2=anint[m][i]+(mint[m][i]/12.);
2493: /*if ((k2>=dateprev1) && (k2<=dateprev2)) {*/
2494: if(agev[m][i]==0) agev[m][i]=iagemax+1;
2495: if(agev[m][i]==1) agev[m][i]=iagemax+2;
2496: if (s[m][i]>0 && s[m][i]<=nlstate) prop[s[m][i]][(int)agev[m][i]] += weight[i];
2497: if (m<lastpass) {
2498: freq[s[m][i]][s[m+1][i]][(int)agev[m][i]] += weight[i];
2499: freq[s[m][i]][s[m+1][i]][iagemax+3] += weight[i];
2500: }
2501:
2502: if ((agev[m][i]>1) && (agev[m][i]< (iagemax+3))) {
2503: dateintsum=dateintsum+k2;
2504: k2cpt++;
2505: }
2506: /*}*/
2507: }
2508: }
2509: } /* end i */
2510:
2511: /* fprintf(ficresp, "#Count between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);*/
2512: pstamp(ficresp);
2513: if (cptcovn>0) {
2514: fprintf(ficresp, "\n#********** Variable ");
2515: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresp, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
2516: fprintf(ficresp, "**********\n#");
2517: fprintf(ficlog, "\n#********** Variable ");
2518: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficlog, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
2519: fprintf(ficlog, "**********\n#");
2520: }
2521: for(i=1; i<=nlstate;i++)
2522: fprintf(ficresp, " Age Prev(%d) N(%d) N",i,i);
2523: fprintf(ficresp, "\n");
2524:
2525: for(i=iagemin; i <= iagemax+3; i++){
2526: if(i==iagemax+3){
2527: fprintf(ficlog,"Total");
2528: }else{
2529: if(first==1){
2530: first=0;
2531: printf("See log file for details...\n");
2532: }
2533: fprintf(ficlog,"Age %d", i);
2534: }
2535: for(jk=1; jk <=nlstate ; jk++){
2536: for(m=-1, pp[jk]=0; m <=nlstate+ndeath ; m++)
2537: pp[jk] += freq[jk][m][i];
2538: }
2539: for(jk=1; jk <=nlstate ; jk++){
2540: for(m=-1, pos=0; m <=0 ; m++)
2541: pos += freq[jk][m][i];
2542: if(pp[jk]>=1.e-10){
2543: if(first==1){
2544: printf(" %d.=%.0f loss[%d]=%.1f%%",jk,pp[jk],jk,100*pos/pp[jk]);
2545: }
2546: fprintf(ficlog," %d.=%.0f loss[%d]=%.1f%%",jk,pp[jk],jk,100*pos/pp[jk]);
2547: }else{
2548: if(first==1)
2549: printf(" %d.=%.0f loss[%d]=NaNQ%%",jk,pp[jk],jk);
2550: fprintf(ficlog," %d.=%.0f loss[%d]=NaNQ%%",jk,pp[jk],jk);
2551: }
2552: }
2553:
2554: for(jk=1; jk <=nlstate ; jk++){
2555: for(m=0, pp[jk]=0; m <=nlstate+ndeath; m++)
2556: pp[jk] += freq[jk][m][i];
2557: }
2558: for(jk=1,pos=0,posprop=0; jk <=nlstate ; jk++){
2559: pos += pp[jk];
2560: posprop += prop[jk][i];
2561: }
2562: for(jk=1; jk <=nlstate ; jk++){
2563: if(pos>=1.e-5){
2564: if(first==1)
2565: printf(" %d.=%.0f prev[%d]=%.1f%%",jk,pp[jk],jk,100*pp[jk]/pos);
2566: fprintf(ficlog," %d.=%.0f prev[%d]=%.1f%%",jk,pp[jk],jk,100*pp[jk]/pos);
2567: }else{
2568: if(first==1)
2569: printf(" %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk);
2570: fprintf(ficlog," %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk);
2571: }
2572: if( i <= iagemax){
2573: if(pos>=1.e-5){
2574: fprintf(ficresp," %d %.5f %.0f %.0f",i,prop[jk][i]/posprop, prop[jk][i],posprop);
2575: /*probs[i][jk][j1]= pp[jk]/pos;*/
2576: /*printf("\ni=%d jk=%d j1=%d %.5f %.0f %.0f %f",i,jk,j1,pp[jk]/pos, pp[jk],pos,probs[i][jk][j1]);*/
2577: }
2578: else
2579: fprintf(ficresp," %d NaNq %.0f %.0f",i,prop[jk][i],posprop);
2580: }
2581: }
2582:
2583: for(jk=-1; jk <=nlstate+ndeath; jk++)
2584: for(m=-1; m <=nlstate+ndeath; m++)
2585: if(freq[jk][m][i] !=0 ) {
2586: if(first==1)
2587: printf(" %d%d=%.0f",jk,m,freq[jk][m][i]);
2588: fprintf(ficlog," %d%d=%.0f",jk,m,freq[jk][m][i]);
2589: }
2590: if(i <= iagemax)
2591: fprintf(ficresp,"\n");
2592: if(first==1)
2593: printf("Others in log...\n");
2594: fprintf(ficlog,"\n");
2595: }
2596: /*}*/
2597: }
2598: dateintmean=dateintsum/k2cpt;
2599:
2600: fclose(ficresp);
2601: free_ma3x(freq,-5,nlstate+ndeath,-5,nlstate+ndeath, iagemin, iagemax+3);
2602: free_vector(pp,1,nlstate);
2603: free_matrix(prop,1,nlstate,iagemin, iagemax+3);
2604: /* End of Freq */
2605: }
2606:
2607: /************ Prevalence ********************/
2608: void prevalence(double ***probs, double agemin, double agemax, int **s, double **agev, int nlstate, int imx, int *Tvar, int **nbcode, int *ncodemax,double **mint,double **anint, double dateprev1,double dateprev2, int firstpass, int lastpass)
2609: {
2610: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
2611: in each health status at the date of interview (if between dateprev1 and dateprev2).
2612: We still use firstpass and lastpass as another selection.
2613: */
2614:
2615: int i, m, jk, k1, i1, j1, bool, z1,j;
2616: double ***freq; /* Frequencies */
2617: double *pp, **prop;
2618: double pos,posprop;
2619: double y2; /* in fractional years */
2620: int iagemin, iagemax;
2621: int first; /** to stop verbosity which is redirected to log file */
2622:
2623: iagemin= (int) agemin;
2624: iagemax= (int) agemax;
2625: /*pp=vector(1,nlstate);*/
2626: prop=matrix(1,nlstate,iagemin,iagemax+3);
2627: /* freq=ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,iagemin,iagemax+3);*/
2628: j1=0;
2629:
2630: /*j=cptcoveff;*/
2631: if (cptcovn<1) {j=1;ncodemax[1]=1;}
2632:
2633: first=1;
2634: for(j1=1; j1<= (int) pow(2,cptcoveff);j1++){
2635: /*for(i1=1; i1<=ncodemax[k1];i1++){
2636: j1++;*/
2637:
2638: for (i=1; i<=nlstate; i++)
2639: for(m=iagemin; m <= iagemax+3; m++)
2640: prop[i][m]=0.0;
2641:
2642: for (i=1; i<=imx; i++) { /* Each individual */
2643: bool=1;
2644: if (cptcovn>0) {
2645: for (z1=1; z1<=cptcoveff; z1++)
2646: if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtab[j1][z1]])
2647: bool=0;
2648: }
2649: if (bool==1) {
2650: for(m=firstpass; m<=lastpass; m++){/* Other selection (we can limit to certain interviews*/
2651: y2=anint[m][i]+(mint[m][i]/12.); /* Fractional date in year */
2652: if ((y2>=dateprev1) && (y2<=dateprev2)) { /* Here is the main selection (fractional years) */
2653: if(agev[m][i]==0) agev[m][i]=iagemax+1;
2654: if(agev[m][i]==1) agev[m][i]=iagemax+2;
2655: if((int)agev[m][i] <iagemin || (int)agev[m][i] >iagemax+3) printf("Error on individual =%d agev[m][i]=%f m=%d\n",i, agev[m][i],m);
2656: if (s[m][i]>0 && s[m][i]<=nlstate) {
2657: /*if(i>4620) printf(" i=%d m=%d s[m][i]=%d (int)agev[m][i]=%d weight[i]=%f prop=%f\n",i,m,s[m][i],(int)agev[m][m],weight[i],prop[s[m][i]][(int)agev[m][i]]);*/
2658: prop[s[m][i]][(int)agev[m][i]] += weight[i];
2659: prop[s[m][i]][iagemax+3] += weight[i];
2660: }
2661: }
2662: } /* end selection of waves */
2663: }
2664: }
2665: for(i=iagemin; i <= iagemax+3; i++){
2666: for(jk=1,posprop=0; jk <=nlstate ; jk++) {
2667: posprop += prop[jk][i];
2668: }
2669:
2670: for(jk=1; jk <=nlstate ; jk++){
2671: if( i <= iagemax){
2672: if(posprop>=1.e-5){
2673: probs[i][jk][j1]= prop[jk][i]/posprop;
2674: } else{
2675: if(first==1){
2676: first=0;
2677: printf("Warning Observed prevalence probs[%d][%d][%d]=%lf because of lack of cases\nSee others on log file...\n",jk,i,j1,probs[i][jk][j1]);
2678: }
2679: }
2680: }
2681: }/* end jk */
2682: }/* end i */
2683: /*} *//* end i1 */
2684: } /* end j1 */
2685:
2686: /* free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath, iagemin, iagemax+3);*/
2687: /*free_vector(pp,1,nlstate);*/
2688: free_matrix(prop,1,nlstate, iagemin,iagemax+3);
2689: } /* End of prevalence */
2690:
2691: /************* Waves Concatenation ***************/
2692:
2693: void concatwav(int wav[], int **dh, int **bh, int **mw, int **s, double *agedc, double **agev, int firstpass, int lastpass, int imx, int nlstate, int stepm)
2694: {
2695: /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
2696: Death is a valid wave (if date is known).
2697: mw[mi][i] is the mi (mi=1 to wav[i]) effective wave of individual i
2698: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
2699: and mw[mi+1][i]. dh depends on stepm.
2700: */
2701:
2702: int i, mi, m;
2703: /* int j, k=0,jk, ju, jl,jmin=1e+5, jmax=-1;
2704: double sum=0., jmean=0.;*/
2705: int first;
2706: int j, k=0,jk, ju, jl;
2707: double sum=0.;
2708: first=0;
2709: jmin=1e+5;
2710: jmax=-1;
2711: jmean=0.;
2712: for(i=1; i<=imx; i++){
2713: mi=0;
2714: m=firstpass;
2715: while(s[m][i] <= nlstate){
2716: if(s[m][i]>=1 || s[m][i]==-2 || s[m][i]==-4 || s[m][i]==-5)
2717: mw[++mi][i]=m;
2718: if(m >=lastpass)
2719: break;
2720: else
2721: m++;
2722: }/* end while */
2723: if (s[m][i] > nlstate){
2724: mi++; /* Death is another wave */
2725: /* if(mi==0) never been interviewed correctly before death */
2726: /* Only death is a correct wave */
2727: mw[mi][i]=m;
2728: }
2729:
2730: wav[i]=mi;
2731: if(mi==0){
2732: nbwarn++;
2733: if(first==0){
2734: printf("Warning! No valid information for individual %ld line=%d (skipped) and may be others, see log file\n",num[i],i);
2735: first=1;
2736: }
2737: if(first==1){
2738: fprintf(ficlog,"Warning! No valid information for individual %ld line=%d (skipped)\n",num[i],i);
2739: }
2740: } /* end mi==0 */
2741: } /* End individuals */
2742:
2743: for(i=1; i<=imx; i++){
2744: for(mi=1; mi<wav[i];mi++){
2745: if (stepm <=0)
2746: dh[mi][i]=1;
2747: else{
2748: if (s[mw[mi+1][i]][i] > nlstate) { /* A death */
2749: if (agedc[i] < 2*AGESUP) {
2750: j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12);
2751: if(j==0) j=1; /* Survives at least one month after exam */
2752: else if(j<0){
2753: nberr++;
2754: printf("Error! Negative delay (%d to death) between waves %d and %d of individual %ld at line %d who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);
2755: j=1; /* Temporary Dangerous patch */
2756: printf(" We assumed that the date of interview was correct (and not the date of death) and postponed the death %d month(s) (one stepm) after the interview. You MUST fix the contradiction between dates.\n",stepm);
2757: fprintf(ficlog,"Error! Negative delay (%d to death) between waves %d and %d of individual %ld at line %d who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);
2758: fprintf(ficlog," We assumed that the date of interview was correct (and not the date of death) and postponed the death %d month(s) (one stepm) after the interview. You MUST fix the contradiction between dates.\n",stepm);
2759: }
2760: k=k+1;
2761: if (j >= jmax){
2762: jmax=j;
2763: ijmax=i;
2764: }
2765: if (j <= jmin){
2766: jmin=j;
2767: ijmin=i;
2768: }
2769: sum=sum+j;
2770: /*if (j<0) printf("j=%d num=%d \n",j,i);*/
2771: /* printf("%d %d %d %d\n", s[mw[mi][i]][i] ,s[mw[mi+1][i]][i],j,i);*/
2772: }
2773: }
2774: else{
2775: j= rint( (agev[mw[mi+1][i]][i]*12 - agev[mw[mi][i]][i]*12));
2776: /* if (j<0) printf("%d %lf %lf %d %d %d\n", i,agev[mw[mi+1][i]][i], agev[mw[mi][i]][i],j,s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]); */
2777:
2778: k=k+1;
2779: if (j >= jmax) {
2780: jmax=j;
2781: ijmax=i;
2782: }
2783: else if (j <= jmin){
2784: jmin=j;
2785: ijmin=i;
2786: }
2787: /* if (j<10) printf("j=%d jmin=%d num=%d ",j,jmin,i); */
2788: /*printf("%d %lf %d %d %d\n", i,agev[mw[mi][i]][i],j,s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);*/
2789: if(j<0){
2790: nberr++;
2791: printf("Error! Negative delay (%d) between waves %d and %d of individual %ld at line %d who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);
2792: fprintf(ficlog,"Error! Negative delay (%d) between waves %d and %d of individual %ld at line %d who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);
2793: }
2794: sum=sum+j;
2795: }
2796: jk= j/stepm;
2797: jl= j -jk*stepm;
2798: ju= j -(jk+1)*stepm;
2799: if(mle <=1){ /* only if we use a the linear-interpoloation pseudo-likelihood */
2800: if(jl==0){
2801: dh[mi][i]=jk;
2802: bh[mi][i]=0;
2803: }else{ /* We want a negative bias in order to only have interpolation ie
2804: * to avoid the price of an extra matrix product in likelihood */
2805: dh[mi][i]=jk+1;
2806: bh[mi][i]=ju;
2807: }
2808: }else{
2809: if(jl <= -ju){
2810: dh[mi][i]=jk;
2811: bh[mi][i]=jl; /* bias is positive if real duration
2812: * is higher than the multiple of stepm and negative otherwise.
2813: */
2814: }
2815: else{
2816: dh[mi][i]=jk+1;
2817: bh[mi][i]=ju;
2818: }
2819: if(dh[mi][i]==0){
2820: dh[mi][i]=1; /* At least one step */
2821: bh[mi][i]=ju; /* At least one step */
2822: /* printf(" bh=%d ju=%d jl=%d dh=%d jk=%d stepm=%d %d\n",bh[mi][i],ju,jl,dh[mi][i],jk,stepm,i);*/
2823: }
2824: } /* end if mle */
2825: }
2826: } /* end wave */
2827: }
2828: jmean=sum/k;
2829: printf("Delay (in months) between two waves Min=%d (for indiviudal %ld) Max=%d (%ld) Mean=%f\n\n ",jmin, num[ijmin], jmax, num[ijmax], jmean);
2830: fprintf(ficlog,"Delay (in months) between two waves Min=%d (for indiviudal %d) Max=%d (%d) Mean=%f\n\n ",jmin, ijmin, jmax, ijmax, jmean);
2831: }
2832:
2833: /*********** Tricode ****************************/
2834: void tricode(int *Tvar, int **nbcode, int imx, int *Ndum)
2835: {
2836: /**< Uses cptcovn+2*cptcovprod as the number of covariates */
2837: /* Tvar[i]=atoi(stre); find 'n' in Vn and stores in Tvar. If model=V2+V1 Tvar[1]=2 and Tvar[2]=1
2838: /* Boring subroutine which should only output nbcode[Tvar[j]][k]
2839: * Tvar[5] in V2+V1+V3*age+V2*V4 is 2 (V2)
2840: /* nbcode[Tvar[j]][1]=
2841: */
2842:
2843: int ij=1, k=0, j=0, i=0, maxncov=NCOVMAX;
2844: int modmaxcovj=0; /* Modality max of covariates j */
2845: int cptcode=0; /* Modality max of covariates j */
2846: int modmincovj=0; /* Modality min of covariates j */
2847:
2848:
2849: cptcoveff=0;
2850:
2851: for (k=-1; k < maxncov; k++) Ndum[k]=0;
2852: for (k=1; k <= maxncov; k++) ncodemax[k]=0; /* Horrible constant again replaced by NCOVMAX */
2853:
2854: /* Loop on covariates without age and products */
2855: for (j=1; j<=(cptcovs); j++) { /* model V1 + V2*age+ V3 + V3*V4 : V1 + V3 = 2 only */
2856: for (i=1; i<=imx; i++) { /* Lopp on individuals: reads the data file to get the maximum value of the
2857: modality of this covariate Vj*/
2858: ij=(int)(covar[Tvar[j]][i]); /* ij=0 or 1 or -1. Value of the covariate Tvar[j] for individual i
2859: * If product of Vn*Vm, still boolean *:
2860: * If it was coded 1, 2, 3, 4 should be splitted into 3 boolean variables
2861: * 1 => 0 0 0, 2 => 0 0 1, 3 => 0 1 1, 4=1 0 0 */
2862: /* Finds for covariate j, n=Tvar[j] of Vn . ij is the
2863: modality of the nth covariate of individual i. */
2864: if (ij > modmaxcovj)
2865: modmaxcovj=ij;
2866: else if (ij < modmincovj)
2867: modmincovj=ij;
2868: if ((ij < -1) && (ij > NCOVMAX)){
2869: printf( "Error: minimal is less than -1 or maximal is bigger than %d. Exiting. \n", NCOVMAX );
2870: exit(1);
2871: }else
2872: Ndum[ij]++; /*counts and stores the occurence of this modality 0, 1, -1*/
2873: /* If coded 1, 2, 3 , counts the number of 1 Ndum[1], number of 2, Ndum[2], etc */
2874: /*printf("i=%d ij=%d Ndum[ij]=%d imx=%d",i,ij,Ndum[ij],imx);*/
2875: /* getting the maximum value of the modality of the covariate
2876: (should be 0 or 1 now) Tvar[j]. If V=sex and male is coded 0 and
2877: female is 1, then modmaxcovj=1.*/
2878: }
2879: printf(" Minimal and maximal values of %d th covariate V%d: min=%d max=%d \n", j, Tvar[j], modmincovj, modmaxcovj);
2880: cptcode=modmaxcovj;
2881: /* Ndum[0] = frequency of 0 for model-covariate j, Ndum[1] frequency of 1 etc. */
2882: /*for (i=0; i<=cptcode; i++) {*/
2883: for (i=modmincovj; i<=modmaxcovj; i++) { /* i=-1 ? 0 and 1*//* For each value of the modality of model-cov j */
2884: printf("Frequencies of covariates %d V%d %d\n", j, Tvar[j], Ndum[i]);
2885: if( Ndum[i] != 0 ){ /* Counts if nobody answered, empty modality */
2886: ncodemax[j]++; /* ncodemax[j]= Number of non-null modalities of the j th covariate. */
2887: }
2888: /* In fact ncodemax[j]=2 (dichotom. variables only) but it could be more for
2889: historical reasons: 3 if coded 1, 2, 3 and 4 and Ndum[2]=0 */
2890: } /* Ndum[-1] number of undefined modalities */
2891:
2892: /* j is a covariate, n=Tvar[j] of Vn; Fills nbcode */
2893: /* For covariate j, modalities could be 1, 2, 3, 4. If Ndum[2]=0 ncodemax[j] is not 4 but 3 */
2894: /* If Ndum[3}= 635; Ndum[4]=0; Ndum[5]=0; Ndum[6]=27; Ndum[7]=125;
2895: modmincovj=3; modmaxcovj = 7;
2896: There are only 3 modalities non empty (or 2 if 27 is too few) : ncodemax[j]=3;
2897: which will be coded 0, 1, 2 which in binary on 3-1 digits are 0=00 1=01, 2=10; defining two dummy
2898: variables V1_1 and V1_2.
2899: nbcode[Tvar[j]][ij]=k;
2900: nbcode[Tvar[j]][1]=0;
2901: nbcode[Tvar[j]][2]=1;
2902: nbcode[Tvar[j]][3]=2;
2903: */
2904: ij=1; /* ij is similar to i but can jumps over null modalities */
2905: for (i=modmincovj; i<=modmaxcovj; i++) { /* i= 1 to 2 for dichotomous, or from 1 to 3 */
2906: for (k=0; k<= cptcode; k++) { /* k=-1 ? k=0 to 1 *//* Could be 1 to 4 */
2907: /*recode from 0 */
2908: if (Ndum[k] != 0) { /* If at least one individual responded to this modality k */
2909: nbcode[Tvar[j]][ij]=k; /* stores the modality in an array nbcode.
2910: k is a modality. If we have model=V1+V1*sex
2911: then: nbcode[1][1]=0 ; nbcode[1][2]=1; nbcode[2][1]=0 ; nbcode[2][2]=1; */
2912: ij++;
2913: }
2914: if (ij > ncodemax[j]) break;
2915: } /* end of loop on */
2916: } /* end of loop on modality */
2917: } /* end of loop on model-covariate j. nbcode[Tvarj][1]=0 and nbcode[Tvarj][2]=1 sets the value of covariate j*/
2918:
2919: for (k=-1; k< maxncov; k++) Ndum[k]=0;
2920:
2921: for (i=1; i<=ncovmodel-2; i++) { /* -2, cste and age */
2922: /* Listing of all covariables in statement model to see if some covariates appear twice. For example, V1 appears twice in V1+V1*V2.*/
2923: ij=Tvar[i]; /* Tvar might be -1 if status was unknown */
2924: Ndum[ij]++;
2925: }
2926:
2927: ij=1;
2928: for (i=0; i<= maxncov-1; i++) { /* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) */
2929: /*printf("Ndum[%d]=%d\n",i, Ndum[i]);*/
2930: if((Ndum[i]!=0) && (i<=ncovcol)){
2931: /*printf("diff Ndum[%d]=%d\n",i, Ndum[i]);*/
2932: Tvaraff[ij]=i; /*For printing (unclear) */
2933: ij++;
2934: }else
2935: Tvaraff[ij]=0;
2936: }
2937: ij--;
2938: cptcoveff=ij; /*Number of total covariates*/
2939:
2940: }
2941:
2942:
2943: /*********** Health Expectancies ****************/
2944:
2945: void evsij(double ***eij, double x[], int nlstate, int stepm, int bage, int fage, double **oldm, double **savm, int cij, int estepm,char strstart[] )
2946:
2947: {
2948: /* Health expectancies, no variances */
2949: int i, j, nhstepm, hstepm, h, nstepm, k, cptj, cptj2, i2, j2;
2950: int nhstepma, nstepma; /* Decreasing with age */
2951: double age, agelim, hf;
2952: double ***p3mat;
2953: double eip;
2954:
2955: pstamp(ficreseij);
2956: fprintf(ficreseij,"# (a) Life expectancies by health status at initial age and (b) health expectancies by health status at initial age\n");
2957: fprintf(ficreseij,"# Age");
2958: for(i=1; i<=nlstate;i++){
2959: for(j=1; j<=nlstate;j++){
2960: fprintf(ficreseij," e%1d%1d ",i,j);
2961: }
2962: fprintf(ficreseij," e%1d. ",i);
2963: }
2964: fprintf(ficreseij,"\n");
2965:
2966:
2967: if(estepm < stepm){
2968: printf ("Problem %d lower than %d\n",estepm, stepm);
2969: }
2970: else hstepm=estepm;
2971: /* We compute the life expectancy from trapezoids spaced every estepm months
2972: * This is mainly to measure the difference between two models: for example
2973: * if stepm=24 months pijx are given only every 2 years and by summing them
2974: * we are calculating an estimate of the Life Expectancy assuming a linear
2975: * progression in between and thus overestimating or underestimating according
2976: * to the curvature of the survival function. If, for the same date, we
2977: * estimate the model with stepm=1 month, we can keep estepm to 24 months
2978: * to compare the new estimate of Life expectancy with the same linear
2979: * hypothesis. A more precise result, taking into account a more precise
2980: * curvature will be obtained if estepm is as small as stepm. */
2981:
2982: /* For example we decided to compute the life expectancy with the smallest unit */
2983: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
2984: nhstepm is the number of hstepm from age to agelim
2985: nstepm is the number of stepm from age to agelin.
2986: Look at hpijx to understand the reason of that which relies in memory size
2987: and note for a fixed period like estepm months */
2988: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
2989: survival function given by stepm (the optimization length). Unfortunately it
2990: means that if the survival funtion is printed only each two years of age and if
2991: you sum them up and add 1 year (area under the trapezoids) you won't get the same
2992: results. So we changed our mind and took the option of the best precision.
2993: */
2994: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
2995:
2996: agelim=AGESUP;
2997: /* If stepm=6 months */
2998: /* Computed by stepm unit matrices, product of hstepm matrices, stored
2999: in an array of nhstepm length: nhstepm=10, hstepm=4, stepm=6 months */
3000:
3001: /* nhstepm age range expressed in number of stepm */
3002: nstepm=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
3003: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
3004: /* if (stepm >= YEARM) hstepm=1;*/
3005: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
3006: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
3007:
3008: for (age=bage; age<=fage; age ++){
3009: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
3010: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
3011: /* if (stepm >= YEARM) hstepm=1;*/
3012: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
3013:
3014: /* If stepm=6 months */
3015: /* Computed by stepm unit matrices, product of hstepma matrices, stored
3016: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
3017:
3018: hpxij(p3mat,nhstepma,age,hstepm,x,nlstate,stepm,oldm, savm, cij);
3019:
3020: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
3021:
3022: printf("%d|",(int)age);fflush(stdout);
3023: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
3024:
3025: /* Computing expectancies */
3026: for(i=1; i<=nlstate;i++)
3027: for(j=1; j<=nlstate;j++)
3028: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
3029: eij[i][j][(int)age] += (p3mat[i][j][h]+p3mat[i][j][h+1])/2.0*hf;
3030:
3031: /* if((int)age==70)printf("i=%2d,j=%2d,h=%2d,age=%3d,%9.4f,%9.4f,%9.4f\n",i,j,h,(int)age,p3mat[i][j][h],hf,eij[i][j][(int)age]);*/
3032:
3033: }
3034:
3035: fprintf(ficreseij,"%3.0f",age );
3036: for(i=1; i<=nlstate;i++){
3037: eip=0;
3038: for(j=1; j<=nlstate;j++){
3039: eip +=eij[i][j][(int)age];
3040: fprintf(ficreseij,"%9.4f", eij[i][j][(int)age] );
3041: }
3042: fprintf(ficreseij,"%9.4f", eip );
3043: }
3044: fprintf(ficreseij,"\n");
3045:
3046: }
3047: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
3048: printf("\n");
3049: fprintf(ficlog,"\n");
3050:
3051: }
3052:
3053: void cvevsij(double ***eij, double x[], int nlstate, int stepm, int bage, int fage, double **oldm, double **savm, int cij, int estepm,double delti[],double **matcov,char strstart[] )
3054:
3055: {
3056: /* Covariances of health expectancies eij and of total life expectancies according
3057: to initial status i, ei. .
3058: */
3059: int i, j, nhstepm, hstepm, h, nstepm, k, cptj, cptj2, i2, j2, ij, ji;
3060: int nhstepma, nstepma; /* Decreasing with age */
3061: double age, agelim, hf;
3062: double ***p3matp, ***p3matm, ***varhe;
3063: double **dnewm,**doldm;
3064: double *xp, *xm;
3065: double **gp, **gm;
3066: double ***gradg, ***trgradg;
3067: int theta;
3068:
3069: double eip, vip;
3070:
3071: varhe=ma3x(1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int) fage);
3072: xp=vector(1,npar);
3073: xm=vector(1,npar);
3074: dnewm=matrix(1,nlstate*nlstate,1,npar);
3075: doldm=matrix(1,nlstate*nlstate,1,nlstate*nlstate);
3076:
3077: pstamp(ficresstdeij);
3078: fprintf(ficresstdeij,"# Health expectancies with standard errors\n");
3079: fprintf(ficresstdeij,"# Age");
3080: for(i=1; i<=nlstate;i++){
3081: for(j=1; j<=nlstate;j++)
3082: fprintf(ficresstdeij," e%1d%1d (SE)",i,j);
3083: fprintf(ficresstdeij," e%1d. ",i);
3084: }
3085: fprintf(ficresstdeij,"\n");
3086:
3087: pstamp(ficrescveij);
3088: fprintf(ficrescveij,"# Subdiagonal matrix of covariances of health expectancies by age: cov(eij,ekl)\n");
3089: fprintf(ficrescveij,"# Age");
3090: for(i=1; i<=nlstate;i++)
3091: for(j=1; j<=nlstate;j++){
3092: cptj= (j-1)*nlstate+i;
3093: for(i2=1; i2<=nlstate;i2++)
3094: for(j2=1; j2<=nlstate;j2++){
3095: cptj2= (j2-1)*nlstate+i2;
3096: if(cptj2 <= cptj)
3097: fprintf(ficrescveij," %1d%1d,%1d%1d",i,j,i2,j2);
3098: }
3099: }
3100: fprintf(ficrescveij,"\n");
3101:
3102: if(estepm < stepm){
3103: printf ("Problem %d lower than %d\n",estepm, stepm);
3104: }
3105: else hstepm=estepm;
3106: /* We compute the life expectancy from trapezoids spaced every estepm months
3107: * This is mainly to measure the difference between two models: for example
3108: * if stepm=24 months pijx are given only every 2 years and by summing them
3109: * we are calculating an estimate of the Life Expectancy assuming a linear
3110: * progression in between and thus overestimating or underestimating according
3111: * to the curvature of the survival function. If, for the same date, we
3112: * estimate the model with stepm=1 month, we can keep estepm to 24 months
3113: * to compare the new estimate of Life expectancy with the same linear
3114: * hypothesis. A more precise result, taking into account a more precise
3115: * curvature will be obtained if estepm is as small as stepm. */
3116:
3117: /* For example we decided to compute the life expectancy with the smallest unit */
3118: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
3119: nhstepm is the number of hstepm from age to agelim
3120: nstepm is the number of stepm from age to agelin.
3121: Look at hpijx to understand the reason of that which relies in memory size
3122: and note for a fixed period like estepm months */
3123: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
3124: survival function given by stepm (the optimization length). Unfortunately it
3125: means that if the survival funtion is printed only each two years of age and if
3126: you sum them up and add 1 year (area under the trapezoids) you won't get the same
3127: results. So we changed our mind and took the option of the best precision.
3128: */
3129: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
3130:
3131: /* If stepm=6 months */
3132: /* nhstepm age range expressed in number of stepm */
3133: agelim=AGESUP;
3134: nstepm=(int) rint((agelim-bage)*YEARM/stepm);
3135: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
3136: /* if (stepm >= YEARM) hstepm=1;*/
3137: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
3138:
3139: p3matp=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
3140: p3matm=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
3141: gradg=ma3x(0,nhstepm,1,npar,1,nlstate*nlstate);
3142: trgradg =ma3x(0,nhstepm,1,nlstate*nlstate,1,npar);
3143: gp=matrix(0,nhstepm,1,nlstate*nlstate);
3144: gm=matrix(0,nhstepm,1,nlstate*nlstate);
3145:
3146: for (age=bage; age<=fage; age ++){
3147: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
3148: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
3149: /* if (stepm >= YEARM) hstepm=1;*/
3150: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
3151:
3152: /* If stepm=6 months */
3153: /* Computed by stepm unit matrices, product of hstepma matrices, stored
3154: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
3155:
3156: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
3157:
3158: /* Computing Variances of health expectancies */
3159: /* Gradient is computed with plus gp and minus gm. Code is duplicated in order to
3160: decrease memory allocation */
3161: for(theta=1; theta <=npar; theta++){
3162: for(i=1; i<=npar; i++){
3163: xp[i] = x[i] + (i==theta ?delti[theta]:0);
3164: xm[i] = x[i] - (i==theta ?delti[theta]:0);
3165: }
3166: hpxij(p3matp,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, cij);
3167: hpxij(p3matm,nhstepm,age,hstepm,xm,nlstate,stepm,oldm,savm, cij);
3168:
3169: for(j=1; j<= nlstate; j++){
3170: for(i=1; i<=nlstate; i++){
3171: for(h=0; h<=nhstepm-1; h++){
3172: gp[h][(j-1)*nlstate + i] = (p3matp[i][j][h]+p3matp[i][j][h+1])/2.;
3173: gm[h][(j-1)*nlstate + i] = (p3matm[i][j][h]+p3matm[i][j][h+1])/2.;
3174: }
3175: }
3176: }
3177:
3178: for(ij=1; ij<= nlstate*nlstate; ij++)
3179: for(h=0; h<=nhstepm-1; h++){
3180: gradg[h][theta][ij]= (gp[h][ij]-gm[h][ij])/2./delti[theta];
3181: }
3182: }/* End theta */
3183:
3184:
3185: for(h=0; h<=nhstepm-1; h++)
3186: for(j=1; j<=nlstate*nlstate;j++)
3187: for(theta=1; theta <=npar; theta++)
3188: trgradg[h][j][theta]=gradg[h][theta][j];
3189:
3190:
3191: for(ij=1;ij<=nlstate*nlstate;ij++)
3192: for(ji=1;ji<=nlstate*nlstate;ji++)
3193: varhe[ij][ji][(int)age] =0.;
3194:
3195: printf("%d|",(int)age);fflush(stdout);
3196: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
3197: for(h=0;h<=nhstepm-1;h++){
3198: for(k=0;k<=nhstepm-1;k++){
3199: matprod2(dnewm,trgradg[h],1,nlstate*nlstate,1,npar,1,npar,matcov);
3200: matprod2(doldm,dnewm,1,nlstate*nlstate,1,npar,1,nlstate*nlstate,gradg[k]);
3201: for(ij=1;ij<=nlstate*nlstate;ij++)
3202: for(ji=1;ji<=nlstate*nlstate;ji++)
3203: varhe[ij][ji][(int)age] += doldm[ij][ji]*hf*hf;
3204: }
3205: }
3206:
3207: /* Computing expectancies */
3208: hpxij(p3matm,nhstepm,age,hstepm,x,nlstate,stepm,oldm, savm, cij);
3209: for(i=1; i<=nlstate;i++)
3210: for(j=1; j<=nlstate;j++)
3211: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
3212: eij[i][j][(int)age] += (p3matm[i][j][h]+p3matm[i][j][h+1])/2.0*hf;
3213:
3214: /* if((int)age==70)printf("i=%2d,j=%2d,h=%2d,age=%3d,%9.4f,%9.4f,%9.4f\n",i,j,h,(int)age,p3mat[i][j][h],hf,eij[i][j][(int)age]);*/
3215:
3216: }
3217:
3218: fprintf(ficresstdeij,"%3.0f",age );
3219: for(i=1; i<=nlstate;i++){
3220: eip=0.;
3221: vip=0.;
3222: for(j=1; j<=nlstate;j++){
3223: eip += eij[i][j][(int)age];
3224: for(k=1; k<=nlstate;k++) /* Sum on j and k of cov(eij,eik) */
3225: vip += varhe[(j-1)*nlstate+i][(k-1)*nlstate+i][(int)age];
3226: fprintf(ficresstdeij," %9.4f (%.4f)", eij[i][j][(int)age], sqrt(varhe[(j-1)*nlstate+i][(j-1)*nlstate+i][(int)age]) );
3227: }
3228: fprintf(ficresstdeij," %9.4f (%.4f)", eip, sqrt(vip));
3229: }
3230: fprintf(ficresstdeij,"\n");
3231:
3232: fprintf(ficrescveij,"%3.0f",age );
3233: for(i=1; i<=nlstate;i++)
3234: for(j=1; j<=nlstate;j++){
3235: cptj= (j-1)*nlstate+i;
3236: for(i2=1; i2<=nlstate;i2++)
3237: for(j2=1; j2<=nlstate;j2++){
3238: cptj2= (j2-1)*nlstate+i2;
3239: if(cptj2 <= cptj)
3240: fprintf(ficrescveij," %.4f", varhe[cptj][cptj2][(int)age]);
3241: }
3242: }
3243: fprintf(ficrescveij,"\n");
3244:
3245: }
3246: free_matrix(gm,0,nhstepm,1,nlstate*nlstate);
3247: free_matrix(gp,0,nhstepm,1,nlstate*nlstate);
3248: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate*nlstate);
3249: free_ma3x(trgradg,0,nhstepm,1,nlstate*nlstate,1,npar);
3250: free_ma3x(p3matm,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
3251: free_ma3x(p3matp,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
3252: printf("\n");
3253: fprintf(ficlog,"\n");
3254:
3255: free_vector(xm,1,npar);
3256: free_vector(xp,1,npar);
3257: free_matrix(dnewm,1,nlstate*nlstate,1,npar);
3258: free_matrix(doldm,1,nlstate*nlstate,1,nlstate*nlstate);
3259: free_ma3x(varhe,1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int)fage);
3260: }
3261:
3262: /************ Variance ******************/
3263: void varevsij(char optionfilefiname[], 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, int estepm, int cptcov, int cptcod, int popbased, int mobilav, char strstart[])
3264: {
3265: /* Variance of health expectancies */
3266: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);*/
3267: /* double **newm;*/
3268: double **dnewm,**doldm;
3269: double **dnewmp,**doldmp;
3270: int i, j, nhstepm, hstepm, h, nstepm ;
3271: int k, cptcode;
3272: double *xp;
3273: double **gp, **gm; /* for var eij */
3274: double ***gradg, ***trgradg; /*for var eij */
3275: double **gradgp, **trgradgp; /* for var p point j */
3276: double *gpp, *gmp; /* for var p point j */
3277: double **varppt; /* for var p point j nlstate to nlstate+ndeath */
3278: double ***p3mat;
3279: double age,agelim, hf;
3280: double ***mobaverage;
3281: int theta;
3282: char digit[4];
3283: char digitp[25];
3284:
3285: char fileresprobmorprev[FILENAMELENGTH];
3286:
3287: if(popbased==1){
3288: if(mobilav!=0)
3289: strcpy(digitp,"-populbased-mobilav-");
3290: else strcpy(digitp,"-populbased-nomobil-");
3291: }
3292: else
3293: strcpy(digitp,"-stablbased-");
3294:
3295: if (mobilav!=0) {
3296: mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
3297: if (movingaverage(probs, bage, fage, mobaverage,mobilav)!=0){
3298: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
3299: printf(" Error in movingaverage mobilav=%d\n",mobilav);
3300: }
3301: }
3302:
3303: strcpy(fileresprobmorprev,"prmorprev");
3304: sprintf(digit,"%-d",ij);
3305: /*printf("DIGIT=%s, ij=%d ijr=%-d|\n",digit, ij,ij);*/
3306: strcat(fileresprobmorprev,digit); /* Tvar to be done */
3307: strcat(fileresprobmorprev,digitp); /* Popbased or not, mobilav or not */
3308: strcat(fileresprobmorprev,fileres);
3309: if((ficresprobmorprev=fopen(fileresprobmorprev,"w"))==NULL) {
3310: printf("Problem with resultfile: %s\n", fileresprobmorprev);
3311: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobmorprev);
3312: }
3313: printf("Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
3314:
3315: fprintf(ficlog,"Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
3316: pstamp(ficresprobmorprev);
3317: fprintf(ficresprobmorprev,"# probabilities of dying before estepm=%d months for people of exact age and weighted probabilities w1*p1j+w2*p2j+... stand dev in()\n",estepm);
3318: fprintf(ficresprobmorprev,"# Age cov=%-d",ij);
3319: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
3320: fprintf(ficresprobmorprev," p.%-d SE",j);
3321: for(i=1; i<=nlstate;i++)
3322: fprintf(ficresprobmorprev," w%1d p%-d%-d",i,i,j);
3323: }
3324: fprintf(ficresprobmorprev,"\n");
3325: fprintf(ficgp,"\n# Routine varevsij");
3326: /* fprintf(fichtm, "#Local time at start: %s", strstart);*/
3327: fprintf(fichtm,"\n<li><h4> Computing probabilities of dying over estepm months as a weighted average (i.e global mortality independent of initial healh state)</h4></li>\n");
3328: fprintf(fichtm,"\n<br>%s <br>\n",digitp);
3329: /* } */
3330: varppt = matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
3331: pstamp(ficresvij);
3332: fprintf(ficresvij,"# Variance and covariance of health expectancies e.j \n# (weighted average of eij where weights are ");
3333: if(popbased==1)
3334: fprintf(ficresvij,"the age specific prevalence observed (cross-sectionally) in the population i.e cross-sectionally\n in each health state (popbased=1) (mobilav=%d\n",mobilav);
3335: else
3336: fprintf(ficresvij,"the age specific period (stable) prevalences in each health state \n");
3337: fprintf(ficresvij,"# Age");
3338: for(i=1; i<=nlstate;i++)
3339: for(j=1; j<=nlstate;j++)
3340: fprintf(ficresvij," Cov(e.%1d, e.%1d)",i,j);
3341: fprintf(ficresvij,"\n");
3342:
3343: xp=vector(1,npar);
3344: dnewm=matrix(1,nlstate,1,npar);
3345: doldm=matrix(1,nlstate,1,nlstate);
3346: dnewmp= matrix(nlstate+1,nlstate+ndeath,1,npar);
3347: doldmp= matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
3348:
3349: gradgp=matrix(1,npar,nlstate+1,nlstate+ndeath);
3350: gpp=vector(nlstate+1,nlstate+ndeath);
3351: gmp=vector(nlstate+1,nlstate+ndeath);
3352: trgradgp =matrix(nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
3353:
3354: if(estepm < stepm){
3355: printf ("Problem %d lower than %d\n",estepm, stepm);
3356: }
3357: else hstepm=estepm;
3358: /* For example we decided to compute the life expectancy with the smallest unit */
3359: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
3360: nhstepm is the number of hstepm from age to agelim
3361: nstepm is the number of stepm from age to agelin.
3362: Look at function hpijx to understand why (it is linked to memory size questions) */
3363: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
3364: survival function given by stepm (the optimization length). Unfortunately it
3365: means that if the survival funtion is printed every two years of age and if
3366: you sum them up and add 1 year (area under the trapezoids) you won't get the same
3367: results. So we changed our mind and took the option of the best precision.
3368: */
3369: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
3370: agelim = AGESUP;
3371: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
3372: nstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
3373: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
3374: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
3375: gradg=ma3x(0,nhstepm,1,npar,1,nlstate);
3376: gp=matrix(0,nhstepm,1,nlstate);
3377: gm=matrix(0,nhstepm,1,nlstate);
3378:
3379:
3380: for(theta=1; theta <=npar; theta++){
3381: for(i=1; i<=npar; i++){ /* Computes gradient x + delta*/
3382: xp[i] = x[i] + (i==theta ?delti[theta]:0);
3383: }
3384: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij);
3385: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ij);
3386:
3387: if (popbased==1) {
3388: if(mobilav ==0){
3389: for(i=1; i<=nlstate;i++)
3390: prlim[i][i]=probs[(int)age][i][ij];
3391: }else{ /* mobilav */
3392: for(i=1; i<=nlstate;i++)
3393: prlim[i][i]=mobaverage[(int)age][i][ij];
3394: }
3395: }
3396:
3397: for(j=1; j<= nlstate; j++){
3398: for(h=0; h<=nhstepm; h++){
3399: for(i=1, gp[h][j]=0.;i<=nlstate;i++)
3400: gp[h][j] += prlim[i][i]*p3mat[i][j][h];
3401: }
3402: }
3403: /* This for computing probability of death (h=1 means
3404: computed over hstepm matrices product = hstepm*stepm months)
3405: as a weighted average of prlim.
3406: */
3407: for(j=nlstate+1;j<=nlstate+ndeath;j++){
3408: for(i=1,gpp[j]=0.; i<= nlstate; i++)
3409: gpp[j] += prlim[i][i]*p3mat[i][j][1];
3410: }
3411: /* end probability of death */
3412:
3413: for(i=1; i<=npar; i++) /* Computes gradient x - delta */
3414: xp[i] = x[i] - (i==theta ?delti[theta]:0);
3415: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij);
3416: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ij);
3417:
3418: if (popbased==1) {
3419: if(mobilav ==0){
3420: for(i=1; i<=nlstate;i++)
3421: prlim[i][i]=probs[(int)age][i][ij];
3422: }else{ /* mobilav */
3423: for(i=1; i<=nlstate;i++)
3424: prlim[i][i]=mobaverage[(int)age][i][ij];
3425: }
3426: }
3427:
3428: for(j=1; j<= nlstate; j++){ /* Sum of wi * eij = e.j */
3429: for(h=0; h<=nhstepm; h++){
3430: for(i=1, gm[h][j]=0.;i<=nlstate;i++)
3431: gm[h][j] += prlim[i][i]*p3mat[i][j][h];
3432: }
3433: }
3434: /* This for computing probability of death (h=1 means
3435: computed over hstepm matrices product = hstepm*stepm months)
3436: as a weighted average of prlim.
3437: */
3438: for(j=nlstate+1;j<=nlstate+ndeath;j++){
3439: for(i=1,gmp[j]=0.; i<= nlstate; i++)
3440: gmp[j] += prlim[i][i]*p3mat[i][j][1];
3441: }
3442: /* end probability of death */
3443:
3444: for(j=1; j<= nlstate; j++) /* vareij */
3445: for(h=0; h<=nhstepm; h++){
3446: gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta];
3447: }
3448:
3449: for(j=nlstate+1; j<= nlstate+ndeath; j++){ /* var mu */
3450: gradgp[theta][j]= (gpp[j]-gmp[j])/2./delti[theta];
3451: }
3452:
3453: } /* End theta */
3454:
3455: trgradg =ma3x(0,nhstepm,1,nlstate,1,npar); /* veij */
3456:
3457: for(h=0; h<=nhstepm; h++) /* veij */
3458: for(j=1; j<=nlstate;j++)
3459: for(theta=1; theta <=npar; theta++)
3460: trgradg[h][j][theta]=gradg[h][theta][j];
3461:
3462: for(j=nlstate+1; j<=nlstate+ndeath;j++) /* mu */
3463: for(theta=1; theta <=npar; theta++)
3464: trgradgp[j][theta]=gradgp[theta][j];
3465:
3466:
3467: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
3468: for(i=1;i<=nlstate;i++)
3469: for(j=1;j<=nlstate;j++)
3470: vareij[i][j][(int)age] =0.;
3471:
3472: for(h=0;h<=nhstepm;h++){
3473: for(k=0;k<=nhstepm;k++){
3474: matprod2(dnewm,trgradg[h],1,nlstate,1,npar,1,npar,matcov);
3475: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg[k]);
3476: for(i=1;i<=nlstate;i++)
3477: for(j=1;j<=nlstate;j++)
3478: vareij[i][j][(int)age] += doldm[i][j]*hf*hf;
3479: }
3480: }
3481:
3482: /* pptj */
3483: matprod2(dnewmp,trgradgp,nlstate+1,nlstate+ndeath,1,npar,1,npar,matcov);
3484: matprod2(doldmp,dnewmp,nlstate+1,nlstate+ndeath,1,npar,nlstate+1,nlstate+ndeath,gradgp);
3485: for(j=nlstate+1;j<=nlstate+ndeath;j++)
3486: for(i=nlstate+1;i<=nlstate+ndeath;i++)
3487: varppt[j][i]=doldmp[j][i];
3488: /* end ppptj */
3489: /* x centered again */
3490: hpxij(p3mat,nhstepm,age,hstepm,x,nlstate,stepm,oldm,savm, ij);
3491: prevalim(prlim,nlstate,x,age,oldm,savm,ftolpl,ij);
3492:
3493: if (popbased==1) {
3494: if(mobilav ==0){
3495: for(i=1; i<=nlstate;i++)
3496: prlim[i][i]=probs[(int)age][i][ij];
3497: }else{ /* mobilav */
3498: for(i=1; i<=nlstate;i++)
3499: prlim[i][i]=mobaverage[(int)age][i][ij];
3500: }
3501: }
3502:
3503: /* This for computing probability of death (h=1 means
3504: computed over hstepm (estepm) matrices product = hstepm*stepm months)
3505: as a weighted average of prlim.
3506: */
3507: for(j=nlstate+1;j<=nlstate+ndeath;j++){
3508: for(i=1,gmp[j]=0.;i<= nlstate; i++)
3509: gmp[j] += prlim[i][i]*p3mat[i][j][1];
3510: }
3511: /* end probability of death */
3512:
3513: fprintf(ficresprobmorprev,"%3d %d ",(int) age, ij);
3514: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
3515: fprintf(ficresprobmorprev," %11.3e %11.3e",gmp[j], sqrt(varppt[j][j]));
3516: for(i=1; i<=nlstate;i++){
3517: fprintf(ficresprobmorprev," %11.3e %11.3e ",prlim[i][i],p3mat[i][j][1]);
3518: }
3519: }
3520: fprintf(ficresprobmorprev,"\n");
3521:
3522: fprintf(ficresvij,"%.0f ",age );
3523: for(i=1; i<=nlstate;i++)
3524: for(j=1; j<=nlstate;j++){
3525: fprintf(ficresvij," %.4f", vareij[i][j][(int)age]);
3526: }
3527: fprintf(ficresvij,"\n");
3528: free_matrix(gp,0,nhstepm,1,nlstate);
3529: free_matrix(gm,0,nhstepm,1,nlstate);
3530: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate);
3531: free_ma3x(trgradg,0,nhstepm,1,nlstate,1,npar);
3532: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
3533: } /* End age */
3534: free_vector(gpp,nlstate+1,nlstate+ndeath);
3535: free_vector(gmp,nlstate+1,nlstate+ndeath);
3536: free_matrix(gradgp,1,npar,nlstate+1,nlstate+ndeath);
3537: free_matrix(trgradgp,nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
3538: fprintf(ficgp,"\nunset parametric;unset label; set ter png small size 320, 240");
3539: /* for(j=nlstate+1; j<= nlstate+ndeath; j++){ *//* Only the first actually */
3540: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Force of mortality (year-1)\";");
3541: /* fprintf(ficgp,"\n plot \"%s\" u 1:($3*%6.3f) not w l 1 ",fileresprobmorprev,YEARM/estepm); */
3542: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)*%6.3f) t \"95\%% interval\" w l 2 ",fileresprobmorprev,YEARM/estepm); */
3543: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)*%6.3f) not w l 2 ",fileresprobmorprev,YEARM/estepm); */
3544: fprintf(ficgp,"\n plot \"%s\" u 1:($3) not w l lt 1 ",subdirf(fileresprobmorprev));
3545: fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)) t \"95\%% interval\" w l lt 2 ",subdirf(fileresprobmorprev));
3546: fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)) not w l lt 2 ",subdirf(fileresprobmorprev));
3547: fprintf(fichtm,"\n<br> File (multiple files are possible if covariates are present): <A href=\"%s\">%s</a>\n",subdirf(fileresprobmorprev),subdirf(fileresprobmorprev));
3548: fprintf(fichtm,"\n<br> Probability is computed over estepm=%d months. <br> <img src=\"%s%s.png\"> <br>\n", estepm,subdirf3(optionfilefiname,"varmuptjgr",digitp),digit);
3549: /* fprintf(fichtm,"\n<br> Probability is computed over estepm=%d months and then divided by estepm and multiplied by %.0f in order to have the probability to die over a year <br> <img src=\"varmuptjgr%s%s.png\"> <br>\n", stepm,YEARM,digitp,digit);
3550: */
3551: /* fprintf(ficgp,"\nset out \"varmuptjgr%s%s%s.png\";replot;",digitp,optionfilefiname,digit); */
3552: fprintf(ficgp,"\nset out \"%s%s.png\";replot;\n",subdirf3(optionfilefiname,"varmuptjgr",digitp),digit);
3553:
3554: free_vector(xp,1,npar);
3555: free_matrix(doldm,1,nlstate,1,nlstate);
3556: free_matrix(dnewm,1,nlstate,1,npar);
3557: free_matrix(doldmp,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
3558: free_matrix(dnewmp,nlstate+1,nlstate+ndeath,1,npar);
3559: free_matrix(varppt,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
3560: if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
3561: fclose(ficresprobmorprev);
3562: fflush(ficgp);
3563: fflush(fichtm);
3564: } /* end varevsij */
3565:
3566: /************ Variance of prevlim ******************/
3567: 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, char strstart[])
3568: {
3569: /* Variance of prevalence limit */
3570: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
3571: double **newm;
3572: double **dnewm,**doldm;
3573: int i, j, nhstepm, hstepm;
3574: int k, cptcode;
3575: double *xp;
3576: double *gp, *gm;
3577: double **gradg, **trgradg;
3578: double age,agelim;
3579: int theta;
3580:
3581: pstamp(ficresvpl);
3582: fprintf(ficresvpl,"# Standard deviation of period (stable) prevalences \n");
3583: fprintf(ficresvpl,"# Age");
3584: for(i=1; i<=nlstate;i++)
3585: fprintf(ficresvpl," %1d-%1d",i,i);
3586: fprintf(ficresvpl,"\n");
3587:
3588: xp=vector(1,npar);
3589: dnewm=matrix(1,nlstate,1,npar);
3590: doldm=matrix(1,nlstate,1,nlstate);
3591:
3592: hstepm=1*YEARM; /* Every year of age */
3593: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
3594: agelim = AGESUP;
3595: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
3596: nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
3597: if (stepm >= YEARM) hstepm=1;
3598: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
3599: gradg=matrix(1,npar,1,nlstate);
3600: gp=vector(1,nlstate);
3601: gm=vector(1,nlstate);
3602:
3603: for(theta=1; theta <=npar; theta++){
3604: for(i=1; i<=npar; i++){ /* Computes gradient */
3605: xp[i] = x[i] + (i==theta ?delti[theta]:0);
3606: }
3607: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ij);
3608: for(i=1;i<=nlstate;i++)
3609: gp[i] = prlim[i][i];
3610:
3611: for(i=1; i<=npar; i++) /* Computes gradient */
3612: xp[i] = x[i] - (i==theta ?delti[theta]:0);
3613: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ij);
3614: for(i=1;i<=nlstate;i++)
3615: gm[i] = prlim[i][i];
3616:
3617: for(i=1;i<=nlstate;i++)
3618: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
3619: } /* End theta */
3620:
3621: trgradg =matrix(1,nlstate,1,npar);
3622:
3623: for(j=1; j<=nlstate;j++)
3624: for(theta=1; theta <=npar; theta++)
3625: trgradg[j][theta]=gradg[theta][j];
3626:
3627: for(i=1;i<=nlstate;i++)
3628: varpl[i][(int)age] =0.;
3629: matprod2(dnewm,trgradg,1,nlstate,1,npar,1,npar,matcov);
3630: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg);
3631: for(i=1;i<=nlstate;i++)
3632: varpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
3633:
3634: fprintf(ficresvpl,"%.0f ",age );
3635: for(i=1; i<=nlstate;i++)
3636: fprintf(ficresvpl," %.5f (%.5f)",prlim[i][i],sqrt(varpl[i][(int)age]));
3637: fprintf(ficresvpl,"\n");
3638: free_vector(gp,1,nlstate);
3639: free_vector(gm,1,nlstate);
3640: free_matrix(gradg,1,npar,1,nlstate);
3641: free_matrix(trgradg,1,nlstate,1,npar);
3642: } /* End age */
3643:
3644: free_vector(xp,1,npar);
3645: free_matrix(doldm,1,nlstate,1,npar);
3646: free_matrix(dnewm,1,nlstate,1,nlstate);
3647:
3648: }
3649:
3650: /************ Variance of one-step probabilities ******************/
3651: void varprob(char optionfilefiname[], double **matcov, double x[], double delti[], int nlstate, double bage, double fage, int ij, int *Tvar, int **nbcode, int *ncodemax, char strstart[])
3652: {
3653: int i, j=0, i1, k1, l1, t, tj;
3654: int k2, l2, j1, z1;
3655: int k=0,l, cptcode;
3656: int first=1, first1, first2;
3657: double cv12, mu1, mu2, lc1, lc2, v12, v21, v11, v22,v1,v2, c12, tnalp;
3658: double **dnewm,**doldm;
3659: double *xp;
3660: double *gp, *gm;
3661: double **gradg, **trgradg;
3662: double **mu;
3663: double age,agelim, cov[NCOVMAX+1];
3664: double std=2.0; /* Number of standard deviation wide of confidence ellipsoids */
3665: int theta;
3666: char fileresprob[FILENAMELENGTH];
3667: char fileresprobcov[FILENAMELENGTH];
3668: char fileresprobcor[FILENAMELENGTH];
3669: double ***varpij;
3670:
3671: strcpy(fileresprob,"prob");
3672: strcat(fileresprob,fileres);
3673: if((ficresprob=fopen(fileresprob,"w"))==NULL) {
3674: printf("Problem with resultfile: %s\n", fileresprob);
3675: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprob);
3676: }
3677: strcpy(fileresprobcov,"probcov");
3678: strcat(fileresprobcov,fileres);
3679: if((ficresprobcov=fopen(fileresprobcov,"w"))==NULL) {
3680: printf("Problem with resultfile: %s\n", fileresprobcov);
3681: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcov);
3682: }
3683: strcpy(fileresprobcor,"probcor");
3684: strcat(fileresprobcor,fileres);
3685: if((ficresprobcor=fopen(fileresprobcor,"w"))==NULL) {
3686: printf("Problem with resultfile: %s\n", fileresprobcor);
3687: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcor);
3688: }
3689: printf("Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
3690: fprintf(ficlog,"Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
3691: printf("Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
3692: fprintf(ficlog,"Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
3693: printf("and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
3694: fprintf(ficlog,"and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
3695: pstamp(ficresprob);
3696: fprintf(ficresprob,"#One-step probabilities and stand. devi in ()\n");
3697: fprintf(ficresprob,"# Age");
3698: pstamp(ficresprobcov);
3699: fprintf(ficresprobcov,"#One-step probabilities and covariance matrix\n");
3700: fprintf(ficresprobcov,"# Age");
3701: pstamp(ficresprobcor);
3702: fprintf(ficresprobcor,"#One-step probabilities and correlation matrix\n");
3703: fprintf(ficresprobcor,"# Age");
3704:
3705:
3706: for(i=1; i<=nlstate;i++)
3707: for(j=1; j<=(nlstate+ndeath);j++){
3708: fprintf(ficresprob," p%1d-%1d (SE)",i,j);
3709: fprintf(ficresprobcov," p%1d-%1d ",i,j);
3710: fprintf(ficresprobcor," p%1d-%1d ",i,j);
3711: }
3712: /* fprintf(ficresprob,"\n");
3713: fprintf(ficresprobcov,"\n");
3714: fprintf(ficresprobcor,"\n");
3715: */
3716: xp=vector(1,npar);
3717: dnewm=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
3718: doldm=matrix(1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
3719: mu=matrix(1,(nlstate)*(nlstate+ndeath), (int) bage, (int)fage);
3720: varpij=ma3x(1,nlstate*(nlstate+ndeath),1,nlstate*(nlstate+ndeath),(int) bage, (int) fage);
3721: first=1;
3722: fprintf(ficgp,"\n# Routine varprob");
3723: fprintf(fichtm,"\n<li><h4> Computing and drawing one step probabilities with their confidence intervals</h4></li>\n");
3724: fprintf(fichtm,"\n");
3725:
3726: fprintf(fichtm,"\n<li><h4> <a href=\"%s\">Matrix of variance-covariance of pairs of step probabilities (drawings)</a></h4></li>\n",optionfilehtmcov);
3727: fprintf(fichtmcov,"\n<h4>Matrix of variance-covariance of pairs of step probabilities</h4>\n\
3728: file %s<br>\n",optionfilehtmcov);
3729: fprintf(fichtmcov,"\nEllipsoids of confidence centered on point (p<inf>ij</inf>, p<inf>kl</inf>) are estimated\
3730: and drawn. It helps understanding how is the covariance between two incidences.\
3731: They are expressed in year<sup>-1</sup> in order to be less dependent of stepm.<br>\n");
3732: fprintf(fichtmcov,"\n<br> Contour plot corresponding to x'cov<sup>-1</sup>x = 4 (where x is the column vector (pij,pkl)) are drawn. \
3733: It can be understood this way: if pij and pkl where uncorrelated the (2x2) matrix of covariance \
3734: would have been (1/(var pij), 0 , 0, 1/(var pkl)), and the confidence interval would be 2 \
3735: standard deviations wide on each axis. <br>\
3736: Now, if both incidences are correlated (usual case) we diagonalised the inverse of the covariance matrix\
3737: and made the appropriate rotation to look at the uncorrelated principal directions.<br>\
3738: To be simple, these graphs help to understand the significativity of each parameter in relation to a second other one.<br> \n");
3739:
3740: cov[1]=1;
3741: /* tj=cptcoveff; */
3742: tj = (int) pow(2,cptcoveff);
3743: if (cptcovn<1) {tj=1;ncodemax[1]=1;}
3744: j1=0;
3745: for(j1=1; j1<=tj;j1++){
3746: /*for(i1=1; i1<=ncodemax[t];i1++){ */
3747: /*j1++;*/
3748: if (cptcovn>0) {
3749: fprintf(ficresprob, "\n#********** Variable ");
3750: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprob, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
3751: fprintf(ficresprob, "**********\n#\n");
3752: fprintf(ficresprobcov, "\n#********** Variable ");
3753: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcov, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
3754: fprintf(ficresprobcov, "**********\n#\n");
3755:
3756: fprintf(ficgp, "\n#********** Variable ");
3757: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficgp, " V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
3758: fprintf(ficgp, "**********\n#\n");
3759:
3760:
3761: fprintf(fichtmcov, "\n<hr size=\"2\" color=\"#EC5E5E\">********** Variable ");
3762: for (z1=1; z1<=cptcoveff; z1++) fprintf(fichtm, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
3763: fprintf(fichtmcov, "**********\n<hr size=\"2\" color=\"#EC5E5E\">");
3764:
3765: fprintf(ficresprobcor, "\n#********** Variable ");
3766: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcor, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
3767: fprintf(ficresprobcor, "**********\n#");
3768: }
3769:
3770: gradg=matrix(1,npar,1,(nlstate)*(nlstate+ndeath));
3771: trgradg=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
3772: gp=vector(1,(nlstate)*(nlstate+ndeath));
3773: gm=vector(1,(nlstate)*(nlstate+ndeath));
3774: for (age=bage; age<=fage; age ++){
3775: cov[2]=age;
3776: for (k=1; k<=cptcovn;k++) {
3777: cov[2+k]=nbcode[Tvar[k]][codtab[j1][Tvar[k]]];/* j1 1 2 3 4
3778: * 1 1 1 1 1
3779: * 2 2 1 1 1
3780: * 3 1 2 1 1
3781: */
3782: /* nbcode[1][1]=0 nbcode[1][2]=1;*/
3783: }
3784: for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2];
3785: for (k=1; k<=cptcovprod;k++)
3786: cov[2+Tprod[k]]=nbcode[Tvard[k][1]][codtab[ij][Tvard[k][1]]]*nbcode[Tvard[k][2]][codtab[ij][Tvard[k][2]]];
3787:
3788:
3789: for(theta=1; theta <=npar; theta++){
3790: for(i=1; i<=npar; i++)
3791: xp[i] = x[i] + (i==theta ?delti[theta]:(double)0);
3792:
3793: pmij(pmmij,cov,ncovmodel,xp,nlstate);
3794:
3795: k=0;
3796: for(i=1; i<= (nlstate); i++){
3797: for(j=1; j<=(nlstate+ndeath);j++){
3798: k=k+1;
3799: gp[k]=pmmij[i][j];
3800: }
3801: }
3802:
3803: for(i=1; i<=npar; i++)
3804: xp[i] = x[i] - (i==theta ?delti[theta]:(double)0);
3805:
3806: pmij(pmmij,cov,ncovmodel,xp,nlstate);
3807: k=0;
3808: for(i=1; i<=(nlstate); i++){
3809: for(j=1; j<=(nlstate+ndeath);j++){
3810: k=k+1;
3811: gm[k]=pmmij[i][j];
3812: }
3813: }
3814:
3815: for(i=1; i<= (nlstate)*(nlstate+ndeath); i++)
3816: gradg[theta][i]=(gp[i]-gm[i])/(double)2./delti[theta];
3817: }
3818:
3819: for(j=1; j<=(nlstate)*(nlstate+ndeath);j++)
3820: for(theta=1; theta <=npar; theta++)
3821: trgradg[j][theta]=gradg[theta][j];
3822:
3823: matprod2(dnewm,trgradg,1,(nlstate)*(nlstate+ndeath),1,npar,1,npar,matcov);
3824: matprod2(doldm,dnewm,1,(nlstate)*(nlstate+ndeath),1,npar,1,(nlstate)*(nlstate+ndeath),gradg);
3825:
3826: pmij(pmmij,cov,ncovmodel,x,nlstate);
3827:
3828: k=0;
3829: for(i=1; i<=(nlstate); i++){
3830: for(j=1; j<=(nlstate+ndeath);j++){
3831: k=k+1;
3832: mu[k][(int) age]=pmmij[i][j];
3833: }
3834: }
3835: for(i=1;i<=(nlstate)*(nlstate+ndeath);i++)
3836: for(j=1;j<=(nlstate)*(nlstate+ndeath);j++)
3837: varpij[i][j][(int)age] = doldm[i][j];
3838:
3839: /*printf("\n%d ",(int)age);
3840: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
3841: printf("%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
3842: fprintf(ficlog,"%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
3843: }*/
3844:
3845: fprintf(ficresprob,"\n%d ",(int)age);
3846: fprintf(ficresprobcov,"\n%d ",(int)age);
3847: fprintf(ficresprobcor,"\n%d ",(int)age);
3848:
3849: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++)
3850: fprintf(ficresprob,"%11.3e (%11.3e) ",mu[i][(int) age],sqrt(varpij[i][i][(int)age]));
3851: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
3852: fprintf(ficresprobcov,"%11.3e ",mu[i][(int) age]);
3853: fprintf(ficresprobcor,"%11.3e ",mu[i][(int) age]);
3854: }
3855: i=0;
3856: for (k=1; k<=(nlstate);k++){
3857: for (l=1; l<=(nlstate+ndeath);l++){
3858: i++;
3859: fprintf(ficresprobcov,"\n%d %d-%d",(int)age,k,l);
3860: fprintf(ficresprobcor,"\n%d %d-%d",(int)age,k,l);
3861: for (j=1; j<=i;j++){
3862: /* printf(" k=%d l=%d i=%d j=%d\n",k,l,i,j);fflush(stdout); */
3863: fprintf(ficresprobcov," %11.3e",varpij[i][j][(int)age]);
3864: fprintf(ficresprobcor," %11.3e",varpij[i][j][(int) age]/sqrt(varpij[i][i][(int) age])/sqrt(varpij[j][j][(int)age]));
3865: }
3866: }
3867: }/* end of loop for state */
3868: } /* end of loop for age */
3869: free_vector(gp,1,(nlstate+ndeath)*(nlstate+ndeath));
3870: free_vector(gm,1,(nlstate+ndeath)*(nlstate+ndeath));
3871: free_matrix(trgradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
3872: free_matrix(gradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
3873:
3874: /* Confidence intervalle of pij */
3875: /*
3876: fprintf(ficgp,"\nunset parametric;unset label");
3877: fprintf(ficgp,"\nset log y;unset log x; set xlabel \"Age\";set ylabel \"probability (year-1)\"");
3878: fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65");
3879: fprintf(fichtm,"\n<br>Probability with confidence intervals expressed in year<sup>-1</sup> :<a href=\"pijgr%s.png\">pijgr%s.png</A>, ",optionfilefiname,optionfilefiname);
3880: fprintf(fichtm,"\n<br><img src=\"pijgr%s.png\"> ",optionfilefiname);
3881: fprintf(ficgp,"\nset out \"pijgr%s.png\"",optionfilefiname);
3882: fprintf(ficgp,"\nplot \"%s\" every :::%d::%d u 1:2 \"\%%lf",k1,k2,xfilevarprob);
3883: */
3884:
3885: /* Drawing ellipsoids of confidence of two variables p(k1-l1,k2-l2)*/
3886: first1=1;first2=2;
3887: for (k2=1; k2<=(nlstate);k2++){
3888: for (l2=1; l2<=(nlstate+ndeath);l2++){
3889: if(l2==k2) continue;
3890: j=(k2-1)*(nlstate+ndeath)+l2;
3891: for (k1=1; k1<=(nlstate);k1++){
3892: for (l1=1; l1<=(nlstate+ndeath);l1++){
3893: if(l1==k1) continue;
3894: i=(k1-1)*(nlstate+ndeath)+l1;
3895: if(i<=j) continue;
3896: for (age=bage; age<=fage; age ++){
3897: if ((int)age %5==0){
3898: v1=varpij[i][i][(int)age]/stepm*YEARM/stepm*YEARM;
3899: v2=varpij[j][j][(int)age]/stepm*YEARM/stepm*YEARM;
3900: cv12=varpij[i][j][(int)age]/stepm*YEARM/stepm*YEARM;
3901: mu1=mu[i][(int) age]/stepm*YEARM ;
3902: mu2=mu[j][(int) age]/stepm*YEARM;
3903: c12=cv12/sqrt(v1*v2);
3904: /* Computing eigen value of matrix of covariance */
3905: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
3906: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
3907: if ((lc2 <0) || (lc1 <0) ){
3908: if(first2==1){
3909: first1=0;
3910: printf("Strange: j1=%d One eigen value of 2x2 matrix of covariance is negative, lc1=%11.3e, lc2=%11.3e, v1=%11.3e, v2=%11.3e, cv12=%11.3e.\n It means that the matrix was not well estimated (varpij), for i=%2d, j=%2d, age=%4d .\n See files %s and %s. Probably WRONG RESULTS. See log file for details...\n", j1, lc1, lc2, v1, v2, cv12, i, j, (int)age,fileresprobcov, fileresprobcor);
3911: }
3912: fprintf(ficlog,"Strange: j1=%d One eigen value of 2x2 matrix of covariance is negative, lc1=%11.3e, lc2=%11.3e, v1=%11.3e, v2=%11.3e, cv12=%11.3e.\n It means that the matrix was not well estimated (varpij), for i=%2d, j=%2d, age=%4d .\n See files %s and %s. Probably WRONG RESULTS.\n", j1, lc1, lc2, v1, v2, cv12, i, j, (int)age,fileresprobcov, fileresprobcor);fflush(ficlog);
3913: /* lc1=fabs(lc1); */ /* If we want to have them positive */
3914: /* lc2=fabs(lc2); */
3915: }
3916:
3917: /* Eigen vectors */
3918: v11=(1./sqrt(1+(v1-lc1)*(v1-lc1)/cv12/cv12));
3919: /*v21=sqrt(1.-v11*v11); *//* error */
3920: v21=(lc1-v1)/cv12*v11;
3921: v12=-v21;
3922: v22=v11;
3923: tnalp=v21/v11;
3924: if(first1==1){
3925: first1=0;
3926: printf("%d %d%d-%d%d mu %.4e %.4e Var %.4e %.4e cor %.3f cov %.4e Eig %.3e %.3e 1stv %.3f %.3f tang %.3f\nOthers in log...\n",(int) age,k1,l1,k2,l2,mu1,mu2,v1,v2,c12,cv12,lc1,lc2,v11,v21,tnalp);
3927: }
3928: fprintf(ficlog,"%d %d%d-%d%d mu %.4e %.4e Var %.4e %.4e cor %.3f cov %.4e Eig %.3e %.3e 1stv %.3f %.3f tan %.3f\n",(int) age,k1,l1,k2,l2,mu1,mu2,v1,v2,c12,cv12,lc1,lc2,v11,v21,tnalp);
3929: /*printf(fignu*/
3930: /* mu1+ v11*lc1*cost + v12*lc2*sin(t) */
3931: /* mu2+ v21*lc1*cost + v22*lc2*sin(t) */
3932: if(first==1){
3933: first=0;
3934: fprintf(ficgp,"\nset parametric;unset label");
3935: fprintf(ficgp,"\nset log y;set log x; set xlabel \"p%1d%1d (year-1)\";set ylabel \"p%1d%1d (year-1)\"",k1,l1,k2,l2);
3936: fprintf(ficgp,"\nset ter png small size 320, 240");
3937: fprintf(fichtmcov,"\n<br>Ellipsoids of confidence cov(p%1d%1d,p%1d%1d) expressed in year<sup>-1</sup>\
3938: :<a href=\"%s%d%1d%1d-%1d%1d.png\">\
3939: %s%d%1d%1d-%1d%1d.png</A>, ",k1,l1,k2,l2,\
3940: subdirf2(optionfilefiname,"varpijgr"), j1,k1,l1,k2,l2,\
3941: subdirf2(optionfilefiname,"varpijgr"), j1,k1,l1,k2,l2);
3942: fprintf(fichtmcov,"\n<br><img src=\"%s%d%1d%1d-%1d%1d.png\"> ",subdirf2(optionfilefiname,"varpijgr"), j1,k1,l1,k2,l2);
3943: fprintf(fichtmcov,"\n<br> Correlation at age %d (%.3f),",(int) age, c12);
3944: fprintf(ficgp,"\nset out \"%s%d%1d%1d-%1d%1d.png\"",subdirf2(optionfilefiname,"varpijgr"), j1,k1,l1,k2,l2);
3945: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
3946: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
3947: fprintf(ficgp,"\nplot [-pi:pi] %11.3e+ %.3f*(%11.3e*%11.3e*cos(t)+%11.3e*%11.3e*sin(t)), %11.3e +%.3f*(%11.3e*%11.3e*cos(t)+%11.3e*%11.3e*sin(t)) not",\
3948: mu1,std,v11,sqrt(lc1),v12,sqrt(lc2),\
3949: mu2,std,v21,sqrt(lc1),v22,sqrt(lc2));
3950: }else{
3951: first=0;
3952: fprintf(fichtmcov," %d (%.3f),",(int) age, c12);
3953: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
3954: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
3955: fprintf(ficgp,"\nreplot %11.3e+ %.3f*(%11.3e*%11.3e*cos(t)+%11.3e*%11.3e*sin(t)), %11.3e +%.3f*(%11.3e*%11.3e*cos(t)+%11.3e*%11.3e*sin(t)) not",\
3956: mu1,std,v11,sqrt(lc1),v12,sqrt(lc2),\
3957: mu2,std,v21,sqrt(lc1),v22,sqrt(lc2));
3958: }/* if first */
3959: } /* age mod 5 */
3960: } /* end loop age */
3961: fprintf(ficgp,"\nset out \"%s%d%1d%1d-%1d%1d.png\";replot;",subdirf2(optionfilefiname,"varpijgr"), j1,k1,l1,k2,l2);
3962: first=1;
3963: } /*l12 */
3964: } /* k12 */
3965: } /*l1 */
3966: }/* k1 */
3967: /* } /* loop covariates */
3968: }
3969: free_ma3x(varpij,1,nlstate,1,nlstate+ndeath,(int) bage, (int)fage);
3970: free_matrix(mu,1,(nlstate+ndeath)*(nlstate+ndeath),(int) bage, (int)fage);
3971: free_matrix(doldm,1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
3972: free_matrix(dnewm,1,(nlstate)*(nlstate+ndeath),1,npar);
3973: free_vector(xp,1,npar);
3974: fclose(ficresprob);
3975: fclose(ficresprobcov);
3976: fclose(ficresprobcor);
3977: fflush(ficgp);
3978: fflush(fichtmcov);
3979: }
3980:
3981:
3982: /******************* Printing html file ***********/
3983: void printinghtml(char fileres[], char title[], char datafile[], int firstpass, \
3984: int lastpass, int stepm, int weightopt, char model[],\
3985: int imx,int jmin, int jmax, double jmeanint,char rfileres[],\
3986: int popforecast, int estepm ,\
3987: double jprev1, double mprev1,double anprev1, \
3988: double jprev2, double mprev2,double anprev2){
3989: int jj1, k1, i1, cpt;
3990:
3991: fprintf(fichtm,"<ul><li><a href='#firstorder'>Result files (first order: no variance)</a>\n \
3992: <li><a href='#secondorder'>Result files (second order (variance)</a>\n \
3993: </ul>");
3994: fprintf(fichtm,"<ul><li><h4><a name='firstorder'>Result files (first order: no variance)</a></h4>\n \
3995: - Observed prevalence in each state (during the period defined between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf): <a href=\"%s\">%s</a> <br>\n ",
3996: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirf2(fileres,"p"),subdirf2(fileres,"p"));
3997: fprintf(fichtm,"\
3998: - Estimated transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
3999: stepm,subdirf2(fileres,"pij"),subdirf2(fileres,"pij"));
4000: fprintf(fichtm,"\
4001: - Period (stable) prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
4002: subdirf2(fileres,"pl"),subdirf2(fileres,"pl"));
4003: fprintf(fichtm,"\
4004: - (a) Life expectancies by health status at initial age, ei. (b) health expectancies by health status at initial age, eij . If one or more covariates are included, specific tables for each value of the covariate are output in sequences within the same file (estepm=%2d months): \
4005: <a href=\"%s\">%s</a> <br>\n",
4006: estepm,subdirf2(fileres,"e"),subdirf2(fileres,"e"));
4007: fprintf(fichtm,"\
4008: - Population projections by age and states: \
4009: <a href=\"%s\">%s</a> <br>\n</li>", subdirf2(fileres,"f"),subdirf2(fileres,"f"));
4010:
4011: fprintf(fichtm," \n<ul><li><b>Graphs</b></li><p>");
4012:
4013: m=pow(2,cptcoveff);
4014: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
4015:
4016: jj1=0;
4017: for(k1=1; k1<=m;k1++){
4018: for(i1=1; i1<=ncodemax[k1];i1++){
4019: jj1++;
4020: if (cptcovn > 0) {
4021: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
4022: for (cpt=1; cpt<=cptcoveff;cpt++)
4023: fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtab[jj1][cpt]]);
4024: fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
4025: }
4026: /* Pij */
4027: fprintf(fichtm,"<br>- Pij or Conditional probabilities to be observed in state j being in state i, %d (stepm) months before: <a href=\"%s%d_1.png\">%s%d_1.png</a><br> \
4028: <img src=\"%s%d_1.png\">",stepm,subdirf2(optionfilefiname,"pe"),jj1,subdirf2(optionfilefiname,"pe"),jj1,subdirf2(optionfilefiname,"pe"),jj1);
4029: /* Quasi-incidences */
4030: fprintf(fichtm,"<br>- Pij or Conditional probabilities to be observed in state j being in state i %d (stepm) months\
4031: before but expressed in per year i.e. quasi incidences if stepm is small and probabilities too: <a href=\"%s%d_2.png\">%s%d_2.png</a><br> \
4032: <img src=\"%s%d_2.png\">",stepm,subdirf2(optionfilefiname,"pe"),jj1,subdirf2(optionfilefiname,"pe"),jj1,subdirf2(optionfilefiname,"pe"),jj1);
4033: /* Period (stable) prevalence in each health state */
4034: for(cpt=1; cpt<=nlstate;cpt++){
4035: fprintf(fichtm,"<br>- Convergence from each state (1 to %d) to period (stable) prevalence in state %d <a href=\"%s%d_%d.png\">%s%d_%d.png</a><br> \
4036: <img src=\"%s%d_%d.png\">",nlstate, cpt, subdirf2(optionfilefiname,"p"),cpt,jj1,subdirf2(optionfilefiname,"p"),cpt,jj1,subdirf2(optionfilefiname,"p"),cpt,jj1);
4037: }
4038: for(cpt=1; cpt<=nlstate;cpt++) {
4039: fprintf(fichtm,"\n<br>- Life expectancy by health state (%d) at initial age and its decomposition into health expectancies in each alive state (1 to %d) : <a href=\"%s%d%d.png\">%s%d%d.png</a> <br> \
4040: <img src=\"%s%d%d.png\">",cpt,nlstate,subdirf2(optionfilefiname,"exp"),cpt,jj1,subdirf2(optionfilefiname,"exp"),cpt,jj1,subdirf2(optionfilefiname,"exp"),cpt,jj1);
4041: }
4042: } /* end i1 */
4043: }/* End k1 */
4044: fprintf(fichtm,"</ul>");
4045:
4046:
4047: fprintf(fichtm,"\
4048: \n<br><li><h4> <a name='secondorder'>Result files (second order: variances)</a></h4>\n\
4049: - Parameter file with estimated parameters and covariance matrix: <a href=\"%s\">%s</a> <br>\n", rfileres,rfileres);
4050:
4051: fprintf(fichtm," - Variance of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
4052: subdirf2(fileres,"prob"),subdirf2(fileres,"prob"));
4053: fprintf(fichtm,"\
4054: - Variance-covariance of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
4055: subdirf2(fileres,"probcov"),subdirf2(fileres,"probcov"));
4056:
4057: fprintf(fichtm,"\
4058: - Correlation matrix of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
4059: subdirf2(fileres,"probcor"),subdirf2(fileres,"probcor"));
4060: fprintf(fichtm,"\
4061: - Variances and covariances of health expectancies by age and <b>initial health status</b> (cov(e<sup>ij</sup>,e<sup>kl</sup>)(estepm=%2d months): \
4062: <a href=\"%s\">%s</a> <br>\n</li>",
4063: estepm,subdirf2(fileres,"cve"),subdirf2(fileres,"cve"));
4064: fprintf(fichtm,"\
4065: - (a) Health expectancies by health status at initial age (e<sup>ij</sup>) and standard errors (in parentheses) (b) life expectancies and standard errors (e<sup>i.</sup>=e<sup>i1</sup>+e<sup>i2</sup>+...)(estepm=%2d months): \
4066: <a href=\"%s\">%s</a> <br>\n</li>",
4067: estepm,subdirf2(fileres,"stde"),subdirf2(fileres,"stde"));
4068: fprintf(fichtm,"\
4069: - Variances and covariances of health expectancies by age. Status (i) based health expectancies (in state j), e<sup>ij</sup> are weighted by the period prevalences in each state i (if popbased=1, an additional computation is done using the cross-sectional prevalences, i.e population based) (estepm=%d months): <a href=\"%s\">%s</a><br>\n",
4070: estepm, subdirf2(fileres,"v"),subdirf2(fileres,"v"));
4071: fprintf(fichtm,"\
4072: - Total life expectancy and total health expectancies to be spent in each health state e<sup>.j</sup> with their standard errors (if popbased=1, an additional computation is done using the cross-sectional prevalences, i.e population based) (estepm=%d months): <a href=\"%s\">%s</a> <br>\n",
4073: estepm, subdirf2(fileres,"t"),subdirf2(fileres,"t"));
4074: fprintf(fichtm,"\
4075: - Standard deviation of period (stable) prevalences: <a href=\"%s\">%s</a> <br>\n",\
4076: subdirf2(fileres,"vpl"),subdirf2(fileres,"vpl"));
4077:
4078: /* if(popforecast==1) fprintf(fichtm,"\n */
4079: /* - Prevalences forecasting: <a href=\"f%s\">f%s</a> <br>\n */
4080: /* - Population forecasting (if popforecast=1): <a href=\"pop%s\">pop%s</a> <br>\n */
4081: /* <br>",fileres,fileres,fileres,fileres); */
4082: /* else */
4083: /* fprintf(fichtm,"\n No population forecast: popforecast = %d (instead of 1) or stepm = %d (instead of 1) or model=%s (instead of .)<br><br></li>\n",popforecast, stepm, model); */
4084: fflush(fichtm);
4085: fprintf(fichtm," <ul><li><b>Graphs</b></li><p>");
4086:
4087: m=pow(2,cptcoveff);
4088: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
4089:
4090: jj1=0;
4091: for(k1=1; k1<=m;k1++){
4092: for(i1=1; i1<=ncodemax[k1];i1++){
4093: jj1++;
4094: if (cptcovn > 0) {
4095: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
4096: for (cpt=1; cpt<=cptcoveff;cpt++)
4097: fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtab[jj1][cpt]]);
4098: fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
4099: }
4100: for(cpt=1; cpt<=nlstate;cpt++) {
4101: fprintf(fichtm,"<br>- Observed (cross-sectional) and period (incidence based) \
4102: prevalence (with 95%% confidence interval) in state (%d): %s%d_%d.png <br>\
4103: <img src=\"%s%d_%d.png\">",cpt,subdirf2(optionfilefiname,"v"),cpt,jj1,subdirf2(optionfilefiname,"v"),cpt,jj1);
4104: }
4105: fprintf(fichtm,"\n<br>- Total life expectancy by age and \
4106: health expectancies in states (1) and (2). If popbased=1 the smooth (due to the model) \
4107: true period expectancies (those weighted with period prevalences are also\
4108: drawn in addition to the population based expectancies computed using\
4109: observed and cahotic prevalences: %s%d.png<br>\
4110: <img src=\"%s%d.png\">",subdirf2(optionfilefiname,"e"),jj1,subdirf2(optionfilefiname,"e"),jj1);
4111: } /* end i1 */
4112: }/* End k1 */
4113: fprintf(fichtm,"</ul>");
4114: fflush(fichtm);
4115: }
4116:
4117: /******************* Gnuplot file **************/
4118: void printinggnuplot(char fileres[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){
4119:
4120: char dirfileres[132],optfileres[132];
4121: int m0,cpt=0,k1=0,i=0,k=0,j=0,jk=0,k2=0,k3=0,ij=0,l=0;
4122: int ng=0;
4123: /* if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) { */
4124: /* printf("Problem with file %s",optionfilegnuplot); */
4125: /* fprintf(ficlog,"Problem with file %s",optionfilegnuplot); */
4126: /* } */
4127:
4128: /*#ifdef windows */
4129: fprintf(ficgp,"cd \"%s\" \n",pathc);
4130: /*#endif */
4131: m=pow(2,cptcoveff);
4132:
4133: strcpy(dirfileres,optionfilefiname);
4134: strcpy(optfileres,"vpl");
4135: /* 1eme*/
4136: fprintf(ficgp,"\n# 1st: Period (stable) prevalence with CI: 'vpl' files\n");
4137: for (cpt=1; cpt<= nlstate ; cpt ++) {
4138: for (k1=1; k1<= m ; k1 ++) { /* plot [100000000000000000000:-100000000000000000000] "mysbiaspar/vplrmysbiaspar.txt to check */
4139: fprintf(ficgp,"\nset out \"%s%d_%d.png\" \n",subdirf2(optionfilefiname,"v"),cpt,k1);
4140: fprintf(ficgp,"\n#set out \"v%s%d_%d.png\" \n",optionfilefiname,cpt,k1);
4141: fprintf(ficgp,"set xlabel \"Age\" \n\
4142: set ylabel \"Probability\" \n\
4143: set ter png small size 320, 240\n\
4144: plot [%.f:%.f] \"%s\" every :::%d::%d u 1:2 \"\%%lf",ageminpar,fage,subdirf2(fileres,"vpl"),k1-1,k1-1);
4145:
4146: for (i=1; i<= nlstate ; i ++) {
4147: if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)");
4148: else fprintf(ficgp," \%%*lf (\%%*lf)");
4149: }
4150: fprintf(ficgp,"\" t\"Period (stable) prevalence\" w l lt 0,\"%s\" every :::%d::%d u 1:($2+1.96*$3) \"\%%lf",subdirf2(fileres,"vpl"),k1-1,k1-1);
4151: for (i=1; i<= nlstate ; i ++) {
4152: if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)");
4153: else fprintf(ficgp," \%%*lf (\%%*lf)");
4154: }
4155: fprintf(ficgp,"\" t\"95\%% CI\" w l lt 1,\"%s\" every :::%d::%d u 1:($2-1.96*$3) \"\%%lf",subdirf2(fileres,"vpl"),k1-1,k1-1);
4156: for (i=1; i<= nlstate ; i ++) {
4157: if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)");
4158: else fprintf(ficgp," \%%*lf (\%%*lf)");
4159: }
4160: fprintf(ficgp,"\" t\"\" w l lt 1,\"%s\" every :::%d::%d u 1:($%d) t\"Observed prevalence \" w l lt 2",subdirf2(fileres,"p"),k1-1,k1-1,2+4*(cpt-1));
4161: }
4162: }
4163: /*2 eme*/
4164: fprintf(ficgp,"\n# 2nd: Total life expectancy with CI: 't' files\n");
4165: for (k1=1; k1<= m ; k1 ++) {
4166: fprintf(ficgp,"\nset out \"%s%d.png\" \n",subdirf2(optionfilefiname,"e"),k1);
4167: fprintf(ficgp,"set ylabel \"Years\" \nset ter png small size 320, 240\nplot [%.f:%.f] ",ageminpar,fage);
4168:
4169: for (i=1; i<= nlstate+1 ; i ++) {
4170: k=2*i;
4171: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:2 \"\%%lf",subdirf2(fileres,"t"),k1-1,k1-1);
4172: for (j=1; j<= nlstate+1 ; j ++) {
4173: if (j==i) fprintf(ficgp," \%%lf (\%%lf)");
4174: else fprintf(ficgp," \%%*lf (\%%*lf)");
4175: }
4176: if (i== 1) fprintf(ficgp,"\" t\"TLE\" w l ,");
4177: else fprintf(ficgp,"\" t\"LE in state (%d)\" w l ,",i-1);
4178: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2-$3*2) \"\%%lf",subdirf2(fileres,"t"),k1-1,k1-1);
4179: for (j=1; j<= nlstate+1 ; j ++) {
4180: if (j==i) fprintf(ficgp," \%%lf (\%%lf)");
4181: else fprintf(ficgp," \%%*lf (\%%*lf)");
4182: }
4183: fprintf(ficgp,"\" t\"\" w l lt 0,");
4184: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2+$3*2) \"\%%lf",subdirf2(fileres,"t"),k1-1,k1-1);
4185: for (j=1; j<= nlstate+1 ; j ++) {
4186: if (j==i) fprintf(ficgp," \%%lf (\%%lf)");
4187: else fprintf(ficgp," \%%*lf (\%%*lf)");
4188: }
4189: if (i== (nlstate+1)) fprintf(ficgp,"\" t\"\" w l lt 0");
4190: else fprintf(ficgp,"\" t\"\" w l lt 0,");
4191: }
4192: }
4193:
4194: /*3eme*/
4195:
4196: for (k1=1; k1<= m ; k1 ++) {
4197: for (cpt=1; cpt<= nlstate ; cpt ++) {
4198: /* k=2+nlstate*(2*cpt-2); */
4199: k=2+(nlstate+1)*(cpt-1);
4200: fprintf(ficgp,"\nset out \"%s%d%d.png\" \n",subdirf2(optionfilefiname,"exp"),cpt,k1);
4201: fprintf(ficgp,"set ter png small size 320, 240\n\
4202: plot [%.f:%.f] \"%s\" every :::%d::%d u 1:%d t \"e%d1\" w l",ageminpar,fage,subdirf2(fileres,"e"),k1-1,k1-1,k,cpt);
4203: /*fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d-2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
4204: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
4205: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
4206: fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d+2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
4207: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
4208: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
4209:
4210: */
4211: for (i=1; i< nlstate ; i ++) {
4212: fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d%d\" w l",subdirf2(fileres,"e"),k1-1,k1-1,k+i,cpt,i+1);
4213: /* fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d%d\" w l",subdirf2(fileres,"e"),k1-1,k1-1,k+2*i,cpt,i+1);*/
4214:
4215: }
4216: fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d.\" w l",subdirf2(fileres,"e"),k1-1,k1-1,k+nlstate,cpt);
4217: }
4218: }
4219:
4220: /* CV preval stable (period) */
4221: for (k1=1; k1<= m ; k1 ++) { /* For each multivariate if any */
4222: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
4223: k=3;
4224: fprintf(ficgp,"\n#\n#\n#CV preval stable (period): 'pij' files, cov=%d state=%d",k1, cpt);
4225: fprintf(ficgp,"\nset out \"%s%d_%d.png\" \n",subdirf2(optionfilefiname,"p"),cpt,k1);
4226: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
4227: set ter png small size 320, 240\n\
4228: unset log y\n\
4229: plot [%.f:%.f] ", ageminpar, agemaxpar);
4230: for (i=1; i<= nlstate ; i ++){
4231: if(i==1)
4232: fprintf(ficgp,"\"%s\"",subdirf2(fileres,"pij"));
4233: else
4234: fprintf(ficgp,", '' ");
4235: l=(nlstate+ndeath)*(i-1)+1;
4236: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
4237: for (j=1; j<= (nlstate-1) ; j ++)
4238: fprintf(ficgp,"+$%d",k+l+j);
4239: fprintf(ficgp,")) t \"prev(%d,%d)\" w l",i,cpt);
4240: } /* nlstate */
4241: fprintf(ficgp,"\n");
4242: } /* end cpt state*/
4243: } /* end covariate */
4244:
4245: /* proba elementaires */
4246: for(i=1,jk=1; i <=nlstate; i++){
4247: for(k=1; k <=(nlstate+ndeath); k++){
4248: if (k != i) {
4249: for(j=1; j <=ncovmodel; j++){
4250: fprintf(ficgp,"p%d=%f ",jk,p[jk]);
4251: jk++;
4252: fprintf(ficgp,"\n");
4253: }
4254: }
4255: }
4256: }
4257: /*goto avoid;*/
4258: for(ng=1; ng<=2;ng++){ /* Number of graphics: first is probabilities second is incidence per year*/
4259: for(jk=1; jk <=m; jk++) {
4260: fprintf(ficgp,"\nset out \"%s%d_%d.png\" \n",subdirf2(optionfilefiname,"pe"),jk,ng);
4261: if (ng==2)
4262: fprintf(ficgp,"\nset ylabel \"Quasi-incidence per year\"\n");
4263: else
4264: fprintf(ficgp,"\nset title \"Probability\"\n");
4265: fprintf(ficgp,"\nset ter png small size 320, 240\nset log y\nplot [%.f:%.f] ",ageminpar,agemaxpar);
4266: i=1;
4267: for(k2=1; k2<=nlstate; k2++) {
4268: k3=i;
4269: for(k=1; k<=(nlstate+ndeath); k++) {
4270: if (k != k2){
4271: if(ng==2)
4272: fprintf(ficgp," %f*exp(p%d+p%d*x",YEARM/stepm,i,i+1);
4273: else
4274: fprintf(ficgp," exp(p%d+p%d*x",i,i+1);
4275: ij=1;/* To be checked else nbcode[0][0] wrong */
4276: for(j=3; j <=ncovmodel; j++) {
4277: /* if(((j-2)==Tage[ij]) &&(ij <=cptcovage)) { /\* Bug valgrind *\/ */
4278: /* /\*fprintf(ficgp,"+p%d*%d*x",i+j-1,nbcode[Tvar[j-2]][codtab[jk][Tvar[j-2]]]);*\/ */
4279: /* ij++; */
4280: /* } */
4281: /* else */
4282: fprintf(ficgp,"+p%d*%d",i+j-1,nbcode[Tvar[j-2]][codtab[jk][j-2]]);
4283: }
4284: fprintf(ficgp,")/(1");
4285:
4286: for(k1=1; k1 <=nlstate; k1++){
4287: fprintf(ficgp,"+exp(p%d+p%d*x",k3+(k1-1)*ncovmodel,k3+(k1-1)*ncovmodel+1);
4288: ij=1;
4289: for(j=3; j <=ncovmodel; j++){
4290: /* if(((j-2)==Tage[ij]) &&(ij <=cptcovage)) { */
4291: /* fprintf(ficgp,"+p%d*%d*x",k3+(k1-1)*ncovmodel+1+j-2,nbcode[Tvar[j-2]][codtab[jk][Tvar[j-2]]]); */
4292: /* ij++; */
4293: /* } */
4294: /* else */
4295: fprintf(ficgp,"+p%d*%d",k3+(k1-1)*ncovmodel+1+j-2,nbcode[Tvar[j-2]][codtab[jk][j-2]]);
4296: }
4297: fprintf(ficgp,")");
4298: }
4299: fprintf(ficgp,") t \"p%d%d\" ", k2,k);
4300: if ((k+k2)!= (nlstate*2+ndeath)) fprintf(ficgp,",");
4301: i=i+ncovmodel;
4302: }
4303: } /* end k */
4304: } /* end k2 */
4305: } /* end jk */
4306: } /* end ng */
4307: avoid:
4308: fflush(ficgp);
4309: } /* end gnuplot */
4310:
4311:
4312: /*************** Moving average **************/
4313: int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav){
4314:
4315: int i, cpt, cptcod;
4316: int modcovmax =1;
4317: int mobilavrange, mob;
4318: double age;
4319:
4320: modcovmax=2*cptcoveff;/* Max number of modalities. We suppose
4321: a covariate has 2 modalities */
4322: if (cptcovn<1) modcovmax=1; /* At least 1 pass */
4323:
4324: if(mobilav==1||mobilav ==3 ||mobilav==5 ||mobilav== 7){
4325: if(mobilav==1) mobilavrange=5; /* default */
4326: else mobilavrange=mobilav;
4327: for (age=bage; age<=fage; age++)
4328: for (i=1; i<=nlstate;i++)
4329: for (cptcod=1;cptcod<=modcovmax;cptcod++)
4330: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
4331: /* We keep the original values on the extreme ages bage, fage and for
4332: fage+1 and bage-1 we use a 3 terms moving average; for fage+2 bage+2
4333: we use a 5 terms etc. until the borders are no more concerned.
4334: */
4335: for (mob=3;mob <=mobilavrange;mob=mob+2){
4336: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){
4337: for (i=1; i<=nlstate;i++){
4338: for (cptcod=1;cptcod<=modcovmax;cptcod++){
4339: mobaverage[(int)age][i][cptcod] =probs[(int)age][i][cptcod];
4340: for (cpt=1;cpt<=(mob-1)/2;cpt++){
4341: mobaverage[(int)age][i][cptcod] +=probs[(int)age-cpt][i][cptcod];
4342: mobaverage[(int)age][i][cptcod] +=probs[(int)age+cpt][i][cptcod];
4343: }
4344: mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/mob;
4345: }
4346: }
4347: }/* end age */
4348: }/* end mob */
4349: }else return -1;
4350: return 0;
4351: }/* End movingaverage */
4352:
4353:
4354: /************** Forecasting ******************/
4355: prevforecast(char fileres[], double anproj1, double mproj1, double jproj1, double ageminpar, double agemax, double dateprev1, double dateprev2, int mobilav, double bage, double fage, int firstpass, int lastpass, double anproj2, double p[], int cptcoveff){
4356: /* proj1, year, month, day of starting projection
4357: agemin, agemax range of age
4358: dateprev1 dateprev2 range of dates during which prevalence is computed
4359: anproj2 year of en of projection (same day and month as proj1).
4360: */
4361: int yearp, stepsize, hstepm, nhstepm, j, k, c, cptcod, i, h, i1;
4362: int *popage;
4363: double agec; /* generic age */
4364: double agelim, ppij, yp,yp1,yp2,jprojmean,mprojmean,anprojmean;
4365: double *popeffectif,*popcount;
4366: double ***p3mat;
4367: double ***mobaverage;
4368: char fileresf[FILENAMELENGTH];
4369:
4370: agelim=AGESUP;
4371: prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
4372:
4373: strcpy(fileresf,"f");
4374: strcat(fileresf,fileres);
4375: if((ficresf=fopen(fileresf,"w"))==NULL) {
4376: printf("Problem with forecast resultfile: %s\n", fileresf);
4377: fprintf(ficlog,"Problem with forecast resultfile: %s\n", fileresf);
4378: }
4379: printf("Computing forecasting: result on file '%s' \n", fileresf);
4380: fprintf(ficlog,"Computing forecasting: result on file '%s' \n", fileresf);
4381:
4382: if (cptcoveff==0) ncodemax[cptcoveff]=1;
4383:
4384: if (mobilav!=0) {
4385: mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
4386: if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){
4387: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
4388: printf(" Error in movingaverage mobilav=%d\n",mobilav);
4389: }
4390: }
4391:
4392: stepsize=(int) (stepm+YEARM-1)/YEARM;
4393: if (stepm<=12) stepsize=1;
4394: if(estepm < stepm){
4395: printf ("Problem %d lower than %d\n",estepm, stepm);
4396: }
4397: else hstepm=estepm;
4398:
4399: hstepm=hstepm/stepm;
4400: yp1=modf(dateintmean,&yp);/* extracts integral of datemean in yp and
4401: fractional in yp1 */
4402: anprojmean=yp;
4403: yp2=modf((yp1*12),&yp);
4404: mprojmean=yp;
4405: yp1=modf((yp2*30.5),&yp);
4406: jprojmean=yp;
4407: if(jprojmean==0) jprojmean=1;
4408: if(mprojmean==0) jprojmean=1;
4409:
4410: i1=cptcoveff;
4411: if (cptcovn < 1){i1=1;}
4412:
4413: fprintf(ficresf,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jprojmean,mprojmean,anprojmean,dateintmean,dateprev1,dateprev2);
4414:
4415: fprintf(ficresf,"#****** Routine prevforecast **\n");
4416:
4417: /* if (h==(int)(YEARM*yearp)){ */
4418: for(cptcov=1, k=0;cptcov<=i1;cptcov++){
4419: for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){
4420: k=k+1;
4421: fprintf(ficresf,"\n#******");
4422: for(j=1;j<=cptcoveff;j++) {
4423: fprintf(ficresf," V%d=%d, hpijx=probability over h years, hp.jx is weighted by observed prev ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
4424: }
4425: fprintf(ficresf,"******\n");
4426: fprintf(ficresf,"# Covariate valuofcovar yearproj age");
4427: for(j=1; j<=nlstate+ndeath;j++){
4428: for(i=1; i<=nlstate;i++)
4429: fprintf(ficresf," p%d%d",i,j);
4430: fprintf(ficresf," p.%d",j);
4431: }
4432: for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) {
4433: fprintf(ficresf,"\n");
4434: fprintf(ficresf,"\n# Forecasting at date %.lf/%.lf/%.lf ",jproj1,mproj1,anproj1+yearp);
4435:
4436: for (agec=fage; agec>=(ageminpar-1); agec--){
4437: nhstepm=(int) rint((agelim-agec)*YEARM/stepm);
4438: nhstepm = nhstepm/hstepm;
4439: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4440: oldm=oldms;savm=savms;
4441: hpxij(p3mat,nhstepm,agec,hstepm,p,nlstate,stepm,oldm,savm, k);
4442:
4443: for (h=0; h<=nhstepm; h++){
4444: if (h*hstepm/YEARM*stepm ==yearp) {
4445: fprintf(ficresf,"\n");
4446: for(j=1;j<=cptcoveff;j++)
4447: fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
4448: fprintf(ficresf,"%.f %.f ",anproj1+yearp,agec+h*hstepm/YEARM*stepm);
4449: }
4450: for(j=1; j<=nlstate+ndeath;j++) {
4451: ppij=0.;
4452: for(i=1; i<=nlstate;i++) {
4453: if (mobilav==1)
4454: ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][i][cptcod];
4455: else {
4456: ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][cptcod];
4457: }
4458: if (h*hstepm/YEARM*stepm== yearp) {
4459: fprintf(ficresf," %.3f", p3mat[i][j][h]);
4460: }
4461: } /* end i */
4462: if (h*hstepm/YEARM*stepm==yearp) {
4463: fprintf(ficresf," %.3f", ppij);
4464: }
4465: }/* end j */
4466: } /* end h */
4467: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4468: } /* end agec */
4469: } /* end yearp */
4470: } /* end cptcod */
4471: } /* end cptcov */
4472:
4473: if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
4474:
4475: fclose(ficresf);
4476: }
4477:
4478: /************** Forecasting *****not tested NB*************/
4479: populforecast(char fileres[], double anpyram,double mpyram,double jpyram,double ageminpar, double agemax,double dateprev1, double dateprev2, int mobilav, double agedeb, double fage, int popforecast, char popfile[], double anpyram1,double p[], int i2){
4480:
4481: int cpt, stepsize, hstepm, nhstepm, j,k,c, cptcod, i,h;
4482: int *popage;
4483: double calagedatem, agelim, kk1, kk2;
4484: double *popeffectif,*popcount;
4485: double ***p3mat,***tabpop,***tabpopprev;
4486: double ***mobaverage;
4487: char filerespop[FILENAMELENGTH];
4488:
4489: tabpop= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
4490: tabpopprev= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
4491: agelim=AGESUP;
4492: calagedatem=(anpyram+mpyram/12.+jpyram/365.-dateintmean)*YEARM;
4493:
4494: prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
4495:
4496:
4497: strcpy(filerespop,"pop");
4498: strcat(filerespop,fileres);
4499: if((ficrespop=fopen(filerespop,"w"))==NULL) {
4500: printf("Problem with forecast resultfile: %s\n", filerespop);
4501: fprintf(ficlog,"Problem with forecast resultfile: %s\n", filerespop);
4502: }
4503: printf("Computing forecasting: result on file '%s' \n", filerespop);
4504: fprintf(ficlog,"Computing forecasting: result on file '%s' \n", filerespop);
4505:
4506: if (cptcoveff==0) ncodemax[cptcoveff]=1;
4507:
4508: if (mobilav!=0) {
4509: mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
4510: if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){
4511: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
4512: printf(" Error in movingaverage mobilav=%d\n",mobilav);
4513: }
4514: }
4515:
4516: stepsize=(int) (stepm+YEARM-1)/YEARM;
4517: if (stepm<=12) stepsize=1;
4518:
4519: agelim=AGESUP;
4520:
4521: hstepm=1;
4522: hstepm=hstepm/stepm;
4523:
4524: if (popforecast==1) {
4525: if((ficpop=fopen(popfile,"r"))==NULL) {
4526: printf("Problem with population file : %s\n",popfile);exit(0);
4527: fprintf(ficlog,"Problem with population file : %s\n",popfile);exit(0);
4528: }
4529: popage=ivector(0,AGESUP);
4530: popeffectif=vector(0,AGESUP);
4531: popcount=vector(0,AGESUP);
4532:
4533: i=1;
4534: while ((c=fscanf(ficpop,"%d %lf\n",&popage[i],&popcount[i])) != EOF) i=i+1;
4535:
4536: imx=i;
4537: for (i=1; i<imx;i++) popeffectif[popage[i]]=popcount[i];
4538: }
4539:
4540: for(cptcov=1,k=0;cptcov<=i2;cptcov++){
4541: for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){
4542: k=k+1;
4543: fprintf(ficrespop,"\n#******");
4544: for(j=1;j<=cptcoveff;j++) {
4545: fprintf(ficrespop," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
4546: }
4547: fprintf(ficrespop,"******\n");
4548: fprintf(ficrespop,"# Age");
4549: for(j=1; j<=nlstate+ndeath;j++) fprintf(ficrespop," P.%d",j);
4550: if (popforecast==1) fprintf(ficrespop," [Population]");
4551:
4552: for (cpt=0; cpt<=0;cpt++) {
4553: fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt);
4554:
4555: for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){
4556: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm);
4557: nhstepm = nhstepm/hstepm;
4558:
4559: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4560: oldm=oldms;savm=savms;
4561: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
4562:
4563: for (h=0; h<=nhstepm; h++){
4564: if (h==(int) (calagedatem+YEARM*cpt)) {
4565: fprintf(ficrespop,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm);
4566: }
4567: for(j=1; j<=nlstate+ndeath;j++) {
4568: kk1=0.;kk2=0;
4569: for(i=1; i<=nlstate;i++) {
4570: if (mobilav==1)
4571: kk1=kk1+p3mat[i][j][h]*mobaverage[(int)agedeb+1][i][cptcod];
4572: else {
4573: kk1=kk1+p3mat[i][j][h]*probs[(int)(agedeb+1)][i][cptcod];
4574: }
4575: }
4576: if (h==(int)(calagedatem+12*cpt)){
4577: tabpop[(int)(agedeb)][j][cptcod]=kk1;
4578: /*fprintf(ficrespop," %.3f", kk1);
4579: if (popforecast==1) fprintf(ficrespop," [%.f]", kk1*popeffectif[(int)agedeb+1]);*/
4580: }
4581: }
4582: for(i=1; i<=nlstate;i++){
4583: kk1=0.;
4584: for(j=1; j<=nlstate;j++){
4585: kk1= kk1+tabpop[(int)(agedeb)][j][cptcod];
4586: }
4587: tabpopprev[(int)(agedeb)][i][cptcod]=tabpop[(int)(agedeb)][i][cptcod]/kk1*popeffectif[(int)(agedeb+(calagedatem+12*cpt)*hstepm/YEARM*stepm-1)];
4588: }
4589:
4590: if (h==(int)(calagedatem+12*cpt)) for(j=1; j<=nlstate;j++)
4591: fprintf(ficrespop," %15.2f",tabpopprev[(int)(agedeb+1)][j][cptcod]);
4592: }
4593: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4594: }
4595: }
4596:
4597: /******/
4598:
4599: for (cpt=1; cpt<=(anpyram1-anpyram);cpt++) {
4600: fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt);
4601: for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){
4602: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm);
4603: nhstepm = nhstepm/hstepm;
4604:
4605: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4606: oldm=oldms;savm=savms;
4607: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
4608: for (h=0; h<=nhstepm; h++){
4609: if (h==(int) (calagedatem+YEARM*cpt)) {
4610: fprintf(ficresf,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm);
4611: }
4612: for(j=1; j<=nlstate+ndeath;j++) {
4613: kk1=0.;kk2=0;
4614: for(i=1; i<=nlstate;i++) {
4615: kk1=kk1+p3mat[i][j][h]*tabpopprev[(int)agedeb+1][i][cptcod];
4616: }
4617: if (h==(int)(calagedatem+12*cpt)) fprintf(ficresf," %15.2f", kk1);
4618: }
4619: }
4620: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4621: }
4622: }
4623: }
4624: }
4625:
4626: if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
4627:
4628: if (popforecast==1) {
4629: free_ivector(popage,0,AGESUP);
4630: free_vector(popeffectif,0,AGESUP);
4631: free_vector(popcount,0,AGESUP);
4632: }
4633: free_ma3x(tabpop,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
4634: free_ma3x(tabpopprev,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
4635: fclose(ficrespop);
4636: } /* End of popforecast */
4637:
4638: int fileappend(FILE *fichier, char *optionfich)
4639: {
4640: if((fichier=fopen(optionfich,"a"))==NULL) {
4641: printf("Problem with file: %s\n", optionfich);
4642: fprintf(ficlog,"Problem with file: %s\n", optionfich);
4643: return (0);
4644: }
4645: fflush(fichier);
4646: return (1);
4647: }
4648:
4649:
4650: /**************** function prwizard **********************/
4651: void prwizard(int ncovmodel, int nlstate, int ndeath, char model[], FILE *ficparo)
4652: {
4653:
4654: /* Wizard to print covariance matrix template */
4655:
4656: char ca[32], cb[32], cc[32];
4657: int i,j, k, l, li, lj, lk, ll, jj, npar, itimes;
4658: int numlinepar;
4659:
4660: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
4661: fprintf(ficparo,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
4662: for(i=1; i <=nlstate; i++){
4663: jj=0;
4664: for(j=1; j <=nlstate+ndeath; j++){
4665: if(j==i) continue;
4666: jj++;
4667: /*ca[0]= k+'a'-1;ca[1]='\0';*/
4668: printf("%1d%1d",i,j);
4669: fprintf(ficparo,"%1d%1d",i,j);
4670: for(k=1; k<=ncovmodel;k++){
4671: /* printf(" %lf",param[i][j][k]); */
4672: /* fprintf(ficparo," %lf",param[i][j][k]); */
4673: printf(" 0.");
4674: fprintf(ficparo," 0.");
4675: }
4676: printf("\n");
4677: fprintf(ficparo,"\n");
4678: }
4679: }
4680: printf("# Scales (for hessian or gradient estimation)\n");
4681: fprintf(ficparo,"# Scales (for hessian or gradient estimation)\n");
4682: npar= (nlstate+ndeath-1)*nlstate*ncovmodel; /* Number of parameters*/
4683: for(i=1; i <=nlstate; i++){
4684: jj=0;
4685: for(j=1; j <=nlstate+ndeath; j++){
4686: if(j==i) continue;
4687: jj++;
4688: fprintf(ficparo,"%1d%1d",i,j);
4689: printf("%1d%1d",i,j);
4690: fflush(stdout);
4691: for(k=1; k<=ncovmodel;k++){
4692: /* printf(" %le",delti3[i][j][k]); */
4693: /* fprintf(ficparo," %le",delti3[i][j][k]); */
4694: printf(" 0.");
4695: fprintf(ficparo," 0.");
4696: }
4697: numlinepar++;
4698: printf("\n");
4699: fprintf(ficparo,"\n");
4700: }
4701: }
4702: printf("# Covariance matrix\n");
4703: /* # 121 Var(a12)\n\ */
4704: /* # 122 Cov(b12,a12) Var(b12)\n\ */
4705: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
4706: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
4707: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
4708: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
4709: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
4710: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
4711: fflush(stdout);
4712: fprintf(ficparo,"# Covariance matrix\n");
4713: /* # 121 Var(a12)\n\ */
4714: /* # 122 Cov(b12,a12) Var(b12)\n\ */
4715: /* # ...\n\ */
4716: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
4717:
4718: for(itimes=1;itimes<=2;itimes++){
4719: jj=0;
4720: for(i=1; i <=nlstate; i++){
4721: for(j=1; j <=nlstate+ndeath; j++){
4722: if(j==i) continue;
4723: for(k=1; k<=ncovmodel;k++){
4724: jj++;
4725: ca[0]= k+'a'-1;ca[1]='\0';
4726: if(itimes==1){
4727: printf("#%1d%1d%d",i,j,k);
4728: fprintf(ficparo,"#%1d%1d%d",i,j,k);
4729: }else{
4730: printf("%1d%1d%d",i,j,k);
4731: fprintf(ficparo,"%1d%1d%d",i,j,k);
4732: /* printf(" %.5le",matcov[i][j]); */
4733: }
4734: ll=0;
4735: for(li=1;li <=nlstate; li++){
4736: for(lj=1;lj <=nlstate+ndeath; lj++){
4737: if(lj==li) continue;
4738: for(lk=1;lk<=ncovmodel;lk++){
4739: ll++;
4740: if(ll<=jj){
4741: cb[0]= lk +'a'-1;cb[1]='\0';
4742: if(ll<jj){
4743: if(itimes==1){
4744: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
4745: fprintf(ficparo," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
4746: }else{
4747: printf(" 0.");
4748: fprintf(ficparo," 0.");
4749: }
4750: }else{
4751: if(itimes==1){
4752: printf(" Var(%s%1d%1d)",ca,i,j);
4753: fprintf(ficparo," Var(%s%1d%1d)",ca,i,j);
4754: }else{
4755: printf(" 0.");
4756: fprintf(ficparo," 0.");
4757: }
4758: }
4759: }
4760: } /* end lk */
4761: } /* end lj */
4762: } /* end li */
4763: printf("\n");
4764: fprintf(ficparo,"\n");
4765: numlinepar++;
4766: } /* end k*/
4767: } /*end j */
4768: } /* end i */
4769: } /* end itimes */
4770:
4771: } /* end of prwizard */
4772: /******************* Gompertz Likelihood ******************************/
4773: double gompertz(double x[])
4774: {
4775: double A,B,L=0.0,sump=0.,num=0.;
4776: int i,n=0; /* n is the size of the sample */
4777:
4778: for (i=0;i<=imx-1 ; i++) {
4779: sump=sump+weight[i];
4780: /* sump=sump+1;*/
4781: num=num+1;
4782: }
4783:
4784:
4785: /* for (i=0; i<=imx; i++)
4786: if (wav[i]>0) printf("i=%d ageex=%lf agecens=%lf agedc=%lf cens=%d %d\n" ,i,ageexmed[i],agecens[i],agedc[i],cens[i],wav[i]);*/
4787:
4788: for (i=1;i<=imx ; i++)
4789: {
4790: if (cens[i] == 1 && wav[i]>1)
4791: A=-x[1]/(x[2])*(exp(x[2]*(agecens[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)));
4792:
4793: if (cens[i] == 0 && wav[i]>1)
4794: A=-x[1]/(x[2])*(exp(x[2]*(agedc[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)))
4795: +log(x[1]/YEARM)+x[2]*(agedc[i]-agegomp)+log(YEARM);
4796:
4797: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
4798: if (wav[i] > 1 ) { /* ??? */
4799: L=L+A*weight[i];
4800: /* printf("\ni=%d A=%f L=%lf x[1]=%lf x[2]=%lf ageex=%lf agecens=%lf cens=%d agedc=%lf weight=%lf\n",i,A,L,x[1],x[2],ageexmed[i]*12,agecens[i]*12,cens[i],agedc[i]*12,weight[i]);*/
4801: }
4802: }
4803:
4804: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
4805:
4806: return -2*L*num/sump;
4807: }
4808:
4809: #ifdef GSL
4810: /******************* Gompertz_f Likelihood ******************************/
4811: double gompertz_f(const gsl_vector *v, void *params)
4812: {
4813: double A,B,LL=0.0,sump=0.,num=0.;
4814: double *x= (double *) v->data;
4815: int i,n=0; /* n is the size of the sample */
4816:
4817: for (i=0;i<=imx-1 ; i++) {
4818: sump=sump+weight[i];
4819: /* sump=sump+1;*/
4820: num=num+1;
4821: }
4822:
4823:
4824: /* for (i=0; i<=imx; i++)
4825: if (wav[i]>0) printf("i=%d ageex=%lf agecens=%lf agedc=%lf cens=%d %d\n" ,i,ageexmed[i],agecens[i],agedc[i],cens[i],wav[i]);*/
4826: printf("x[0]=%lf x[1]=%lf\n",x[0],x[1]);
4827: for (i=1;i<=imx ; i++)
4828: {
4829: if (cens[i] == 1 && wav[i]>1)
4830: A=-x[0]/(x[1])*(exp(x[1]*(agecens[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)));
4831:
4832: if (cens[i] == 0 && wav[i]>1)
4833: A=-x[0]/(x[1])*(exp(x[1]*(agedc[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)))
4834: +log(x[0]/YEARM)+x[1]*(agedc[i]-agegomp)+log(YEARM);
4835:
4836: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
4837: if (wav[i] > 1 ) { /* ??? */
4838: LL=LL+A*weight[i];
4839: /* printf("\ni=%d A=%f L=%lf x[1]=%lf x[2]=%lf ageex=%lf agecens=%lf cens=%d agedc=%lf weight=%lf\n",i,A,L,x[1],x[2],ageexmed[i]*12,agecens[i]*12,cens[i],agedc[i]*12,weight[i]);*/
4840: }
4841: }
4842:
4843: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
4844: printf("x[0]=%lf x[1]=%lf -2*LL*num/sump=%lf\n",x[0],x[1],-2*LL*num/sump);
4845:
4846: return -2*LL*num/sump;
4847: }
4848: #endif
4849:
4850: /******************* Printing html file ***********/
4851: void printinghtmlmort(char fileres[], char title[], char datafile[], int firstpass, \
4852: int lastpass, int stepm, int weightopt, char model[],\
4853: int imx, double p[],double **matcov,double agemortsup){
4854: int i,k;
4855:
4856: fprintf(fichtm,"<ul><li><h4>Result files </h4>\n Force of mortality. Parameters of the Gompertz fit (with confidence interval in brackets):<br>");
4857: fprintf(fichtm," mu(age) =%lf*exp(%lf*(age-%d)) per year<br><br>",p[1],p[2],agegomp);
4858: for (i=1;i<=2;i++)
4859: fprintf(fichtm," p[%d] = %lf [%f ; %f]<br>\n",i,p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
4860: fprintf(fichtm,"<br><br><img src=\"graphmort.png\">");
4861: fprintf(fichtm,"</ul>");
4862:
4863: fprintf(fichtm,"<ul><li><h4>Life table</h4>\n <br>");
4864:
4865: fprintf(fichtm,"\nAge l<inf>x</inf> q<inf>x</inf> d(x,x+1) L<inf>x</inf> T<inf>x</inf> e<infx</inf><br>");
4866:
4867: for (k=agegomp;k<(agemortsup-2);k++)
4868: fprintf(fichtm,"%d %.0lf %lf %.0lf %.0lf %.0lf %lf<br>\n",k,lsurv[k],p[1]*exp(p[2]*(k-agegomp)),(p[1]*exp(p[2]*(k-agegomp)))*lsurv[k],lpop[k],tpop[k],tpop[k]/lsurv[k]);
4869:
4870:
4871: fflush(fichtm);
4872: }
4873:
4874: /******************* Gnuplot file **************/
4875: void printinggnuplotmort(char fileres[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){
4876:
4877: char dirfileres[132],optfileres[132];
4878: int m,cpt,k1,i,k,j,jk,k2,k3,ij,l;
4879: int ng;
4880:
4881:
4882: /*#ifdef windows */
4883: fprintf(ficgp,"cd \"%s\" \n",pathc);
4884: /*#endif */
4885:
4886:
4887: strcpy(dirfileres,optionfilefiname);
4888: strcpy(optfileres,"vpl");
4889: fprintf(ficgp,"set out \"graphmort.png\"\n ");
4890: fprintf(ficgp,"set xlabel \"Age\"\n set ylabel \"Force of mortality (per year)\" \n ");
4891: fprintf(ficgp, "set ter png small size 320, 240\n set log y\n");
4892: /* fprintf(ficgp, "set size 0.65,0.65\n"); */
4893: fprintf(ficgp,"plot [%d:100] %lf*exp(%lf*(x-%d))",agegomp,p[1],p[2],agegomp);
4894:
4895: }
4896:
4897: int readdata(char datafile[], int firstobs, int lastobs, int *imax)
4898: {
4899:
4900: /*-------- data file ----------*/
4901: FILE *fic;
4902: char dummy[]=" ";
4903: int i, j, n;
4904: int linei, month, year,iout;
4905: char line[MAXLINE], linetmp[MAXLINE];
4906: char stra[80], strb[80];
4907: char *stratrunc;
4908: int lstra;
4909:
4910:
4911: if((fic=fopen(datafile,"r"))==NULL) {
4912: printf("Problem while opening datafile: %s\n", datafile);return 1;
4913: fprintf(ficlog,"Problem while opening datafile: %s\n", datafile);return 1;
4914: }
4915:
4916: i=1;
4917: linei=0;
4918: while ((fgets(line, MAXLINE, fic) != NULL) &&((i >= firstobs) && (i <=lastobs))) {
4919: linei=linei+1;
4920: for(j=strlen(line); j>=0;j--){ /* Untabifies line */
4921: if(line[j] == '\t')
4922: line[j] = ' ';
4923: }
4924: for(j=strlen(line)-1; (line[j]==' ')||(line[j]==10)||(line[j]==13);j--){
4925: ;
4926: };
4927: line[j+1]=0; /* Trims blanks at end of line */
4928: if(line[0]=='#'){
4929: fprintf(ficlog,"Comment line\n%s\n",line);
4930: printf("Comment line\n%s\n",line);
4931: continue;
4932: }
4933: trimbb(linetmp,line); /* Trims multiple blanks in line */
4934: for (j=0; line[j]!='\0';j++){
4935: line[j]=linetmp[j];
4936: }
4937:
4938:
4939: for (j=maxwav;j>=1;j--){
4940: cutv(stra, strb, line, ' ');
4941: if(strb[0]=='.') { /* Missing status */
4942: lval=-1;
4943: }else{
4944: errno=0;
4945: lval=strtol(strb,&endptr,10);
4946: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
4947: if( strb[0]=='\0' || (*endptr != '\0')){
4948: printf("Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be a status of wave %d. Setting maxwav=%d might be wrong. Exiting.\n", strb, linei,i,line,j,maxwav);
4949: fprintf(ficlog,"Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be a status of wave %d. Setting maxwav=%d might be wrong. Exiting.\n", strb, linei,i,line,j,maxwav);fflush(ficlog);
4950: return 1;
4951: }
4952: }
4953: s[j][i]=lval;
4954:
4955: strcpy(line,stra);
4956: cutv(stra, strb,line,' ');
4957: if(iout=sscanf(strb,"%d/%d",&month, &year) != 0){
4958: }
4959: else if(iout=sscanf(strb,"%s.",dummy) != 0){
4960: month=99;
4961: year=9999;
4962: }else{
4963: printf("Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be a date of interview (mm/yyyy or .) at wave %d. Exiting.\n",strb, linei,i, line,j);
4964: fprintf(ficlog,"Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be a date of interview (mm/yyyy or .) at wave %d. Exiting.\n",strb, linei,i, line,j);fflush(ficlog);
4965: return 1;
4966: }
4967: anint[j][i]= (double) year;
4968: mint[j][i]= (double)month;
4969: strcpy(line,stra);
4970: } /* ENd Waves */
4971:
4972: cutv(stra, strb,line,' ');
4973: if(iout=sscanf(strb,"%d/%d",&month, &year) != 0){
4974: }
4975: else if(iout=sscanf(strb,"%s.",dummy) != 0){
4976: month=99;
4977: year=9999;
4978: }else{
4979: printf("Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be a date of death (mm/yyyy or .). Exiting.\n",strb, linei,i,line);
4980: fprintf(ficlog,"Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be a date of death (mm/yyyy or .). Exiting.\n",strb, linei,i,line);fflush(ficlog);
4981: return 1;
4982: }
4983: andc[i]=(double) year;
4984: moisdc[i]=(double) month;
4985: strcpy(line,stra);
4986:
4987: cutv(stra, strb,line,' ');
4988: if(iout=sscanf(strb,"%d/%d",&month, &year) != 0){
4989: }
4990: else if(iout=sscanf(strb,"%s.", dummy) != 0){
4991: month=99;
4992: year=9999;
4993: }else{
4994: printf("Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be a date of birth (mm/yyyy or .). Exiting.\n",strb, linei,i,line);
4995: fprintf(ficlog,"Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be a date of birth (mm/yyyy or .). Exiting.\n",strb, linei,i,line);fflush(ficlog);
4996: return 1;
4997: }
4998: if (year==9999) {
4999: printf("Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be a date of birth (mm/yyyy) but at least the year of birth should be given. Exiting.\n",strb, linei,i,line);
5000: fprintf(ficlog,"Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be a date of birth (mm/yyyy) but at least the year of birth should be given. Exiting.\n",strb, linei,i,line);fflush(ficlog);
5001: return 1;
5002:
5003: }
5004: annais[i]=(double)(year);
5005: moisnais[i]=(double)(month);
5006: strcpy(line,stra);
5007:
5008: cutv(stra, strb,line,' ');
5009: errno=0;
5010: dval=strtod(strb,&endptr);
5011: if( strb[0]=='\0' || (*endptr != '\0')){
5012: printf("Error reading data around '%f' at line number %d, \"%s\" for individual %d\nShould be a weight. Exiting.\n",dval, i,line,linei);
5013: fprintf(ficlog,"Error reading data around '%f' at line number %d, \"%s\" for individual %d\nShould be a weight. Exiting.\n",dval, i,line,linei);
5014: fflush(ficlog);
5015: return 1;
5016: }
5017: weight[i]=dval;
5018: strcpy(line,stra);
5019:
5020: for (j=ncovcol;j>=1;j--){
5021: cutv(stra, strb,line,' ');
5022: if(strb[0]=='.') { /* Missing status */
5023: lval=-1;
5024: }else{
5025: errno=0;
5026: lval=strtol(strb,&endptr,10);
5027: if( strb[0]=='\0' || (*endptr != '\0')){
5028: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\nShould be a covariate value (=0 for the reference or 1 for alternative). Exiting.\n",lval, linei,i, line);
5029: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\nShould be a covariate value (=0 for the reference or 1 for alternative). Exiting.\n",lval, linei,i, line);fflush(ficlog);
5030: return 1;
5031: }
5032: }
5033: if(lval <-1 || lval >1){
5034: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
5035: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
5036: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
5037: For example, for multinomial values like 1, 2 and 3,\n \
5038: build V1=0 V2=0 for the reference value (1),\n \
5039: V1=1 V2=0 for (2) \n \
5040: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
5041: output of IMaCh is often meaningless.\n \
5042: Exiting.\n",lval,linei, i,line,j);
5043: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
5044: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
5045: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
5046: For example, for multinomial values like 1, 2 and 3,\n \
5047: build V1=0 V2=0 for the reference value (1),\n \
5048: V1=1 V2=0 for (2) \n \
5049: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
5050: output of IMaCh is often meaningless.\n \
5051: Exiting.\n",lval,linei, i,line,j);fflush(ficlog);
5052: return 1;
5053: }
5054: covar[j][i]=(double)(lval);
5055: strcpy(line,stra);
5056: }
5057: lstra=strlen(stra);
5058:
5059: if(lstra > 9){ /* More than 2**32 or max of what printf can write with %ld */
5060: stratrunc = &(stra[lstra-9]);
5061: num[i]=atol(stratrunc);
5062: }
5063: else
5064: num[i]=atol(stra);
5065: /*if((s[2][i]==2) && (s[3][i]==-1)&&(s[4][i]==9)){
5066: printf("%ld %.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])); ij=ij+1;}*/
5067:
5068: i=i+1;
5069: } /* End loop reading data */
5070:
5071: *imax=i-1; /* Number of individuals */
5072: fclose(fic);
5073:
5074: return (0);
5075: endread:
5076: printf("Exiting readdata: ");
5077: fclose(fic);
5078: return (1);
5079:
5080:
5081:
5082: }
5083: void removespace(char *str) {
5084: char *p1 = str, *p2 = str;
5085: do
5086: while (*p2 == ' ')
5087: p2++;
5088: while (*p1++ = *p2++);
5089: }
5090:
5091: int decodemodel ( char model[], int lastobs) /**< This routine decode the model and returns:
5092: * Model V1+V2+V3+V8+V7*V8+V5*V6+V8*age+V3*age
5093: * - cptcovt total number of covariates of the model nbocc(+)+1 = 8
5094: * - cptcovn or number of covariates k of the models excluding age*products =6
5095: * - cptcovage number of covariates with age*products =2
5096: * - cptcovs number of simple covariates
5097: * - Tvar[k] is the id of the kth covariate Tvar[1]@12 {1, 2, 3, 8, 10, 11, 8, 3, 7, 8, 5, 6}, thus Tvar[5=V7*V8]=10
5098: * which is a new column after the 9 (ncovcol) variables.
5099: * - if k is a product Vn*Vm covar[k][i] is filled with correct values for each individual
5100: * - Tprod[l] gives the kth covariates of the product Vn*Vm l=1 to cptcovprod-cptcovage
5101: * Tprod[1]@2 {5, 6}: position of first product V7*V8 is 5, and second V5*V6 is 6.
5102: * - Tvard[k] p Tvard[1][1]@4 {7, 8, 5, 6} for V7*V8 and V5*V6 .
5103: */
5104: {
5105: int i, j, k, ks;
5106: int i1, j1, k1, k2;
5107: char modelsav[80];
5108: char stra[80], strb[80], strc[80], strd[80],stre[80];
5109:
5110: /*removespace(model);*/
5111: if (strlen(model) >1){ /* If there is at least 1 covariate */
5112: j=0, j1=0, k1=0, k2=-1, ks=0, cptcovn=0;
5113: j=nbocc(model,'+'); /**< j=Number of '+' */
5114: j1=nbocc(model,'*'); /**< j1=Number of '*' */
5115: cptcovs=j+1-j1; /**< Number of simple covariates V1+V2*age+V3 +V3*V4=> V1 + V3 =2 */
5116: cptcovt= j+1; /* Number of total covariates in the model V1 + V2*age+ V3 + V3*V4=> 4*/
5117: /* including age products which are counted in cptcovage.
5118: /* but the covariates which are products must be treated separately: ncovn=4- 2=2 (V1+V3). */
5119: cptcovprod=j1; /**< Number of products V1*V2 +v3*age = 2 */
5120: cptcovprodnoage=0; /**< Number of covariate products without age: V3*V4 =1 */
5121: strcpy(modelsav,model);
5122: if (strstr(model,"AGE") !=0){
5123: printf("Error. AGE must be in lower case 'age' model=%s ",model);
5124: fprintf(ficlog,"Error. AGE must be in lower case model=%s ",model);fflush(ficlog);
5125: return 1;
5126: }
5127: if (strstr(model,"v") !=0){
5128: printf("Error. 'v' must be in upper case 'V' model=%s ",model);
5129: fprintf(ficlog,"Error. 'v' must be in upper case model=%s ",model);fflush(ficlog);
5130: return 1;
5131: }
5132:
5133: /* Design
5134: * V1 V2 V3 V4 V5 V6 V7 V8 V9 Weight
5135: * < ncovcol=8 >
5136: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8
5137: * k= 1 2 3 4 5 6 7 8
5138: * cptcovn number of covariates (not including constant and age ) = # of + plus 1 = 7+1=8
5139: * covar[k,i], value of kth covariate if not including age for individual i:
5140: * covar[1][i]= (V2), covar[4][i]=(V3), covar[8][i]=(V8)
5141: * Tvar[k] # of the kth covariate: Tvar[1]=2 Tvar[4]=3 Tvar[8]=8
5142: * if multiplied by age: V3*age Tvar[3=V3*age]=3 (V3) Tvar[7]=8 and
5143: * Tage[++cptcovage]=k
5144: * if products, new covar are created after ncovcol with k1
5145: * Tvar[k]=ncovcol+k1; # of the kth covariate product: Tvar[5]=ncovcol+1=10 Tvar[6]=ncovcol+1=11
5146: * Tprod[k1]=k; Tprod[1]=5 Tprod[2]= 6; gives the position of the k1th product
5147: * Tvard[k1][1]=m Tvard[k1][2]=m; Tvard[1][1]=5 (V5) Tvard[1][2]=6 Tvard[2][1]=7 (V7) Tvard[2][2]=8
5148: * Tvar[cptcovn+k2]=Tvard[k1][1];Tvar[cptcovn+k2+1]=Tvard[k1][2];
5149: * Tvar[8+1]=5;Tvar[8+2]=6;Tvar[8+3]=7;Tvar[8+4]=8 inverted
5150: * V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11
5151: * < ncovcol=8 >
5152: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8 d1 d1 d2 d2
5153: * k= 1 2 3 4 5 6 7 8 9 10 11 12
5154: * Tvar[k]= 2 1 3 3 10 11 8 8 5 6 7 8
5155: * p Tvar[1]@12={2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
5156: * p Tprod[1]@2={ 6, 5}
5157: *p Tvard[1][1]@4= {7, 8, 5, 6}
5158: * covar[k][i]= V2 V1 ? V3 V5*V6? V7*V8? ? V8
5159: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
5160: *How to reorganize?
5161: * Model V1 + V2 + V3 + V8 + V5*V6 + V7*V8 + V3*age + V8*age
5162: * Tvars {2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
5163: * {2, 1, 4, 8, 5, 6, 3, 7}
5164: * Struct []
5165: */
5166:
5167: /* This loop fills the array Tvar from the string 'model'.*/
5168: /* j is the number of + signs in the model V1+V2+V3 j=2 i=3 to 1 */
5169: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4 */
5170: /* k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tage[cptcovage=1]=4 */
5171: /* k=3 V4 Tvar[k=3]= 4 (from V4) */
5172: /* k=2 V1 Tvar[k=2]= 1 (from V1) */
5173: /* k=1 Tvar[1]=2 (from V2) */
5174: /* k=5 Tvar[5] */
5175: /* for (k=1; k<=cptcovn;k++) { */
5176: /* cov[2+k]=nbcode[Tvar[k]][codtab[ij][Tvar[k]]]; */
5177: /* } */
5178: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
5179: /*
5180: * Treating invertedly V2+V1+V3*age+V2*V4 is as if written V2*V4 +V3*age + V1 + V2 */
5181: for(k=cptcovt; k>=1;k--) /**< Number of covariates */
5182: Tvar[k]=0;
5183: cptcovage=0;
5184: for(k=1; k<=cptcovt;k++){ /* Loop on total covariates of the model */
5185: cutl(stra,strb,modelsav,'+'); /* keeps in strb after the first '+'
5186: modelsav==V2+V1+V4+V3*age strb=V3*age stra=V2+V1+V4 */
5187: if (nbocc(modelsav,'+')==0) strcpy(strb,modelsav); /* and analyzes it */
5188: /* printf("i=%d a=%s b=%s sav=%s\n",i, stra,strb,modelsav);*/
5189: /*scanf("%d",i);*/
5190: if (strchr(strb,'*')) { /**< Model includes a product V2+V1+V4+V3*age strb=V3*age */
5191: cutl(strc,strd,strb,'*'); /**< strd*strc Vm*Vn: strb=V3*age(input) strc=age strd=V3 ; V3*V2 strc=V2, strd=V3 */
5192: if (strcmp(strc,"age")==0) { /**< Model includes age: Vn*age */
5193: /* covar is not filled and then is empty */
5194: cptcovprod--;
5195: cutl(stre,strb,strd,'V'); /* strd=V3(input): stre="3" */
5196: Tvar[k]=atoi(stre); /* V2+V1+V4+V3*age Tvar[4]=3 ; V1+V2*age Tvar[2]=2 */
5197: cptcovage++; /* Sums the number of covariates which include age as a product */
5198: Tage[cptcovage]=k; /* Tage[1] = 4 */
5199: /*printf("stre=%s ", stre);*/
5200: } else if (strcmp(strd,"age")==0) { /* or age*Vn */
5201: cptcovprod--;
5202: cutl(stre,strb,strc,'V');
5203: Tvar[k]=atoi(stre);
5204: cptcovage++;
5205: Tage[cptcovage]=k;
5206: } else { /* Age is not in the model product V2+V1+V1*V4+V3*age+V3*V2 strb=V3*V2*/
5207: /* loops on k1=1 (V3*V2) and k1=2 V4*V3 */
5208: cptcovn++;
5209: cptcovprodnoage++;k1++;
5210: cutl(stre,strb,strc,'V'); /* strc= Vn, stre is n; strb=V3*V2 stre=3 strc=*/
5211: Tvar[k]=ncovcol+k1; /* For model-covariate k tells which data-covariate to use but
5212: because this model-covariate is a construction we invent a new column
5213: ncovcol + k1
5214: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2
5215: Tvar[3=V1*V4]=4+1 Tvar[5=V3*V2]=4 + 2= 6, etc */
5216: cutl(strc,strb,strd,'V'); /* strd was Vm, strc is m */
5217: Tprod[k1]=k; /* Tprod[1]=3(=V1*V4) for V2+V1+V1*V4+age*V3+V3*V2 */
5218: Tvard[k1][1] =atoi(strc); /* m 1 for V1*/
5219: Tvard[k1][2] =atoi(stre); /* n 4 for V4*/
5220: k2=k2+2;
5221: Tvar[cptcovt+k2]=Tvard[k1][1]; /* Tvar[(cptcovt=4+k2=1)=5]= 1 (V1) */
5222: Tvar[cptcovt+k2+1]=Tvard[k1][2]; /* Tvar[(cptcovt=4+(k2=1)+1)=6]= 4 (V4) */
5223: for (i=1; i<=lastobs;i++){
5224: /* Computes the new covariate which is a product of
5225: covar[n][i]* covar[m][i] and stores it at ncovol+k1 May not be defined */
5226: covar[ncovcol+k1][i]=covar[atoi(stre)][i]*covar[atoi(strc)][i];
5227: }
5228: } /* End age is not in the model */
5229: } /* End if model includes a product */
5230: else { /* no more sum */
5231: /*printf("d=%s c=%s b=%s\n", strd,strc,strb);*/
5232: /* scanf("%d",i);*/
5233: cutl(strd,strc,strb,'V');
5234: ks++; /**< Number of simple covariates */
5235: cptcovn++;
5236: Tvar[k]=atoi(strd);
5237: }
5238: strcpy(modelsav,stra); /* modelsav=V2+V1+V4 stra=V2+V1+V4 */
5239: /*printf("a=%s b=%s sav=%s\n", stra,strb,modelsav);
5240: scanf("%d",i);*/
5241: } /* end of loop + */
5242: } /* end model */
5243:
5244: /*The number n of Vn is stored in Tvar. cptcovage =number of age covariate. Tage gives the position of age. cptcovprod= number of products.
5245: If model=V1+V1*age then Tvar[1]=1 Tvar[2]=1 cptcovage=1 Tage[1]=2 cptcovprod=0*/
5246:
5247: /* printf("tvar1=%d tvar2=%d tvar3=%d cptcovage=%d Tage=%d",Tvar[1],Tvar[2],Tvar[3],cptcovage,Tage[1]);
5248: printf("cptcovprod=%d ", cptcovprod);
5249: fprintf(ficlog,"cptcovprod=%d ", cptcovprod);
5250:
5251: scanf("%d ",i);*/
5252:
5253:
5254: return (0); /* with covar[new additional covariate if product] and Tage if age */
5255: endread:
5256: printf("Exiting decodemodel: ");
5257: return (1);
5258: }
5259:
5260: calandcheckages(int imx, int maxwav, double *agemin, double *agemax, int *nberr, int *nbwarn )
5261: {
5262: int i, m;
5263:
5264: for (i=1; i<=imx; i++) {
5265: for(m=2; (m<= maxwav); m++) {
5266: if (((int)mint[m][i]== 99) && (s[m][i] <= nlstate)){
5267: anint[m][i]=9999;
5268: s[m][i]=-1;
5269: }
5270: if((int)moisdc[i]==99 && (int)andc[i]==9999 && s[m][i]>nlstate){
5271: *nberr++;
5272: printf("Error! Date of death (month %2d and year %4d) of individual %ld on line %d was unknown, you must set an arbitrary year of death or he/she is skipped and results are biased\n",(int)moisdc[i],(int)andc[i],num[i],i);
5273: fprintf(ficlog,"Error! Date of death (month %2d and year %4d) of individual %ld on line %d was unknown, you must set an arbitrary year of death or he/she is skipped and results are biased\n",(int)moisdc[i],(int)andc[i],num[i],i);
5274: s[m][i]=-1;
5275: }
5276: if((int)moisdc[i]==99 && (int)andc[i]!=9999 && s[m][i]>nlstate){
5277: *nberr++;
5278: printf("Error! Month of death of individual %ld on line %d was unknown %2d, you should set it otherwise the information on the death is skipped and results are biased.\n",num[i],i,(int)moisdc[i]);
5279: fprintf(ficlog,"Error! Month of death of individual %ld on line %d was unknown %f, you should set it otherwise the information on the death is skipped and results are biased.\n",num[i],i,moisdc[i]);
5280: s[m][i]=-1; /* We prefer to skip it (and to skip it in version 0.8a1 too */
5281: }
5282: }
5283: }
5284:
5285: for (i=1; i<=imx; i++) {
5286: agedc[i]=(moisdc[i]/12.+andc[i])-(moisnais[i]/12.+annais[i]);
5287: for(m=firstpass; (m<= lastpass); m++){
5288: if(s[m][i] >0 || s[m][i]==-2 || s[m][i]==-4 || s[m][i]==-5){
5289: if (s[m][i] >= nlstate+1) {
5290: if(agedc[i]>0)
5291: if((int)moisdc[i]!=99 && (int)andc[i]!=9999)
5292: agev[m][i]=agedc[i];
5293: /*if(moisdc[i]==99 && andc[i]==9999) s[m][i]=-1;*/
5294: else {
5295: if ((int)andc[i]!=9999){
5296: nbwarn++;
5297: printf("Warning negative age at death: %ld line:%d\n",num[i],i);
5298: fprintf(ficlog,"Warning negative age at death: %ld line:%d\n",num[i],i);
5299: agev[m][i]=-1;
5300: }
5301: }
5302: }
5303: else if(s[m][i] !=9){ /* Standard case, age in fractional
5304: years but with the precision of a month */
5305: agev[m][i]=(mint[m][i]/12.+1./24.+anint[m][i])-(moisnais[i]/12.+1./24.+annais[i]);
5306: if((int)mint[m][i]==99 || (int)anint[m][i]==9999)
5307: agev[m][i]=1;
5308: else if(agev[m][i] < *agemin){
5309: *agemin=agev[m][i];
5310: printf(" Min anint[%d][%d]=%.2f annais[%d]=%.2f, agemin=%.2f\n",m,i,anint[m][i], i,annais[i], *agemin);
5311: }
5312: else if(agev[m][i] >*agemax){
5313: *agemax=agev[m][i];
5314: /* printf(" Max anint[%d][%d]=%.0f annais[%d]=%.0f, agemax=%.2f\n",m,i,anint[m][i], i,annais[i], *agemax);*/
5315: }
5316: /*agev[m][i]=anint[m][i]-annais[i];*/
5317: /* agev[m][i] = age[i]+2*m;*/
5318: }
5319: else { /* =9 */
5320: agev[m][i]=1;
5321: s[m][i]=-1;
5322: }
5323: }
5324: else /*= 0 Unknown */
5325: agev[m][i]=1;
5326: }
5327:
5328: }
5329: for (i=1; i<=imx; i++) {
5330: for(m=firstpass; (m<=lastpass); m++){
5331: if (s[m][i] > (nlstate+ndeath)) {
5332: *nberr++;
5333: printf("Error: on wave %d of individual %d status %d > (nlstate+ndeath)=(%d+%d)=%d\n",m,i,s[m][i],nlstate, ndeath, nlstate+ndeath);
5334: fprintf(ficlog,"Error: on wave %d of individual %d status %d > (nlstate+ndeath)=(%d+%d)=%d\n",m,i,s[m][i],nlstate, ndeath, nlstate+ndeath);
5335: return 1;
5336: }
5337: }
5338: }
5339:
5340: /*for (i=1; i<=imx; i++){
5341: for (m=firstpass; (m<lastpass); m++){
5342: printf("%ld %d %.lf %d %d\n", num[i],(covar[1][i]),agev[m][i],s[m][i],s[m+1][i]);
5343: }
5344:
5345: }*/
5346:
5347:
5348: printf("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
5349: fprintf(ficlog,"Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
5350:
5351: return (0);
5352: endread:
5353: printf("Exiting calandcheckages: ");
5354: return (1);
5355: }
5356:
5357:
5358: /***********************************************/
5359: /**************** Main Program *****************/
5360: /***********************************************/
5361:
5362: int main(int argc, char *argv[])
5363: {
5364: #ifdef GSL
5365: const gsl_multimin_fminimizer_type *T;
5366: size_t iteri = 0, it;
5367: int rval = GSL_CONTINUE;
5368: int status = GSL_SUCCESS;
5369: double ssval;
5370: #endif
5371: int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav);
5372: int i,j, k, n=MAXN,iter,m,size=100,cptcode, cptcod;
5373: int linei, month, year,iout;
5374: int jj, ll, li, lj, lk, imk;
5375: int numlinepar=0; /* Current linenumber of parameter file */
5376: int itimes;
5377: int NDIM=2;
5378: int vpopbased=0;
5379:
5380: char ca[32], cb[32], cc[32];
5381: /* FILE *fichtm; *//* Html File */
5382: /* FILE *ficgp;*/ /*Gnuplot File */
5383: struct stat info;
5384: double agedeb, agefin,hf;
5385: double ageminpar=1.e20,agemin=1.e20, agemaxpar=-1.e20, agemax=-1.e20;
5386:
5387: double fret;
5388: double **xi,tmp,delta;
5389:
5390: double dum; /* Dummy variable */
5391: double ***p3mat;
5392: double ***mobaverage;
5393: int *indx;
5394: char line[MAXLINE], linepar[MAXLINE];
5395: char path[MAXLINE],pathc[MAXLINE],pathcd[MAXLINE],pathtot[MAXLINE],model[MAXLINE];
5396: char pathr[MAXLINE], pathimach[MAXLINE];
5397: char **bp, *tok, *val; /* pathtot */
5398: int firstobs=1, lastobs=10;
5399: int sdeb, sfin; /* Status at beginning and end */
5400: int c, h , cpt,l;
5401: int ju,jl, mi;
5402: int i1,j1, jk,aa,bb, stepsize, ij;
5403: int jnais,jdc,jint4,jint1,jint2,jint3,*tab;
5404: int mobilavproj=0 , prevfcast=0 ; /* moving average of prev, If prevfcast=1 prevalence projection */
5405: int mobilav=0,popforecast=0;
5406: int hstepm, nhstepm;
5407: int agemortsup;
5408: float sumlpop=0.;
5409: double jprev1=1, mprev1=1,anprev1=2000,jprev2=1, mprev2=1,anprev2=2000;
5410: double jpyram=1, mpyram=1,anpyram=2000,jpyram1=1, mpyram1=1,anpyram1=2000;
5411:
5412: double bage, fage, age, agelim, agebase;
5413: double ftolpl=FTOL;
5414: double **prlim;
5415: double ***param; /* Matrix of parameters */
5416: double *p;
5417: double **matcov; /* Matrix of covariance */
5418: double ***delti3; /* Scale */
5419: double *delti; /* Scale */
5420: double ***eij, ***vareij;
5421: double **varpl; /* Variances of prevalence limits by age */
5422: double *epj, vepp;
5423: double kk1, kk2;
5424: double dateprev1, dateprev2,jproj1=1,mproj1=1,anproj1=2000,jproj2=1,mproj2=1,anproj2=2000;
5425: double **ximort;
5426: char *alph[]={"a","a","b","c","d","e"}, str[4]="1234";
5427: int *dcwave;
5428:
5429: char z[1]="c", occ;
5430:
5431: /*char *strt;*/
5432: char strtend[80];
5433:
5434: long total_usecs;
5435:
5436: /* setlocale (LC_ALL, ""); */
5437: /* bindtextdomain (PACKAGE, LOCALEDIR); */
5438: /* textdomain (PACKAGE); */
5439: /* setlocale (LC_CTYPE, ""); */
5440: /* setlocale (LC_MESSAGES, ""); */
5441:
5442: /* gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */
5443: rstart_time = time(NULL);
5444: /* (void) gettimeofday(&start_time,&tzp);*/
5445: start_time = *localtime(&rstart_time);
5446: curr_time=start_time;
5447: /*tml = *localtime(&start_time.tm_sec);*/
5448: /* strcpy(strstart,asctime(&tml)); */
5449: strcpy(strstart,asctime(&start_time));
5450:
5451: /* printf("Localtime (at start)=%s",strstart); */
5452: /* tp.tm_sec = tp.tm_sec +86400; */
5453: /* tm = *localtime(&start_time.tm_sec); */
5454: /* tmg.tm_year=tmg.tm_year +dsign*dyear; */
5455: /* tmg.tm_mon=tmg.tm_mon +dsign*dmonth; */
5456: /* tmg.tm_hour=tmg.tm_hour + 1; */
5457: /* tp.tm_sec = mktime(&tmg); */
5458: /* strt=asctime(&tmg); */
5459: /* printf("Time(after) =%s",strstart); */
5460: /* (void) time (&time_value);
5461: * printf("time=%d,t-=%d\n",time_value,time_value-86400);
5462: * tm = *localtime(&time_value);
5463: * strstart=asctime(&tm);
5464: * printf("tim_value=%d,asctime=%s\n",time_value,strstart);
5465: */
5466:
5467: nberr=0; /* Number of errors and warnings */
5468: nbwarn=0;
5469: getcwd(pathcd, size);
5470:
5471: printf("\n%s\n%s",version,fullversion);
5472: if(argc <=1){
5473: printf("\nEnter the parameter file name: ");
5474: fgets(pathr,FILENAMELENGTH,stdin);
5475: i=strlen(pathr);
5476: if(pathr[i-1]=='\n')
5477: pathr[i-1]='\0';
5478: i=strlen(pathr);
5479: if(pathr[i-1]==' ') /* This may happen when dragging on oS/X! */
5480: pathr[i-1]='\0';
5481: for (tok = pathr; tok != NULL; ){
5482: printf("Pathr |%s|\n",pathr);
5483: while ((val = strsep(&tok, "\"" )) != NULL && *val == '\0');
5484: printf("val= |%s| pathr=%s\n",val,pathr);
5485: strcpy (pathtot, val);
5486: if(pathr[0] == '\0') break; /* Dirty */
5487: }
5488: }
5489: else{
5490: strcpy(pathtot,argv[1]);
5491: }
5492: /*if(getcwd(pathcd, MAXLINE)!= NULL)printf ("Error pathcd\n");*/
5493: /*cygwin_split_path(pathtot,path,optionfile);
5494: printf("pathtot=%s, path=%s, optionfile=%s\n",pathtot,path,optionfile);*/
5495: /* cutv(path,optionfile,pathtot,'\\');*/
5496:
5497: /* Split argv[0], imach program to get pathimach */
5498: printf("\nargv[0]=%s argv[1]=%s, \n",argv[0],argv[1]);
5499: split(argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
5500: printf("\nargv[0]=%s pathimach=%s, \noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
5501: /* strcpy(pathimach,argv[0]); */
5502: /* Split argv[1]=pathtot, parameter file name to get path, optionfile, extension and name */
5503: split(pathtot,path,optionfile,optionfilext,optionfilefiname);
5504: printf("\npathtot=%s,\npath=%s,\noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname);
5505: chdir(path); /* Can be a relative path */
5506: if(getcwd(pathcd,MAXLINE) > 0) /* So pathcd is the full path */
5507: printf("Current directory %s!\n",pathcd);
5508: strcpy(command,"mkdir ");
5509: strcat(command,optionfilefiname);
5510: if((outcmd=system(command)) != 0){
5511: printf("Problem creating directory or it already exists %s%s, err=%d\n",path,optionfilefiname,outcmd);
5512: /* fprintf(ficlog,"Problem creating directory %s%s\n",path,optionfilefiname); */
5513: /* fclose(ficlog); */
5514: /* exit(1); */
5515: }
5516: /* if((imk=mkdir(optionfilefiname))<0){ */
5517: /* perror("mkdir"); */
5518: /* } */
5519:
5520: /*-------- arguments in the command line --------*/
5521:
5522: /* Log file */
5523: strcat(filelog, optionfilefiname);
5524: strcat(filelog,".log"); /* */
5525: if((ficlog=fopen(filelog,"w"))==NULL) {
5526: printf("Problem with logfile %s\n",filelog);
5527: goto end;
5528: }
5529: fprintf(ficlog,"Log filename:%s\n",filelog);
5530: fprintf(ficlog,"\n%s\n%s",version,fullversion);
5531: fprintf(ficlog,"\nEnter the parameter file name: \n");
5532: fprintf(ficlog,"pathimach=%s\npathtot=%s\n\
5533: path=%s \n\
5534: optionfile=%s\n\
5535: optionfilext=%s\n\
5536: optionfilefiname='%s'\n",pathimach,pathtot,path,optionfile,optionfilext,optionfilefiname);
5537:
5538: printf("Local time (at start):%s",strstart);
5539: fprintf(ficlog,"Local time (at start): %s",strstart);
5540: fflush(ficlog);
5541: /* (void) gettimeofday(&curr_time,&tzp); */
5542: /* printf("Elapsed time %d\n", asc_diff_time(curr_time.tm_sec-start_time.tm_sec,tmpout)); */
5543:
5544: /* */
5545: strcpy(fileres,"r");
5546: strcat(fileres, optionfilefiname);
5547: strcat(fileres,".txt"); /* Other files have txt extension */
5548:
5549: /*---------arguments file --------*/
5550:
5551: if((ficpar=fopen(optionfile,"r"))==NULL) {
5552: printf("Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
5553: fprintf(ficlog,"Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
5554: fflush(ficlog);
5555: /* goto end; */
5556: exit(70);
5557: }
5558:
5559:
5560:
5561: strcpy(filereso,"o");
5562: strcat(filereso,fileres);
5563: if((ficparo=fopen(filereso,"w"))==NULL) { /* opened on subdirectory */
5564: printf("Problem with Output resultfile: %s\n", filereso);
5565: fprintf(ficlog,"Problem with Output resultfile: %s\n", filereso);
5566: fflush(ficlog);
5567: goto end;
5568: }
5569:
5570: /* Reads comments: lines beginning with '#' */
5571: numlinepar=0;
5572: while((c=getc(ficpar))=='#' && c!= EOF){
5573: ungetc(c,ficpar);
5574: fgets(line, MAXLINE, ficpar);
5575: numlinepar++;
5576: fputs(line,stdout);
5577: fputs(line,ficparo);
5578: fputs(line,ficlog);
5579: }
5580: ungetc(c,ficpar);
5581:
5582: fscanf(ficpar,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%lf stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d model=%s\n",title, datafile, &lastobs, &firstpass,&lastpass,&ftol, &stepm, &ncovcol, &nlstate,&ndeath, &maxwav, &mle, &weightopt,model);
5583: numlinepar++;
5584: printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=%s\n", title, datafile, lastobs, firstpass,lastpass,ftol, stepm, ncovcol, nlstate,ndeath, maxwav, mle, weightopt,model);
5585: fprintf(ficparo,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=%s\n", title, datafile, lastobs, firstpass,lastpass,ftol,stepm,ncovcol,nlstate,ndeath,maxwav, mle, weightopt,model);
5586: fprintf(ficlog,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=%s\n", title, datafile, lastobs, firstpass,lastpass,ftol,stepm,ncovcol,nlstate,ndeath,maxwav, mle, weightopt,model);
5587: fflush(ficlog);
5588: while((c=getc(ficpar))=='#' && c!= EOF){
5589: ungetc(c,ficpar);
5590: fgets(line, MAXLINE, ficpar);
5591: numlinepar++;
5592: fputs(line, stdout);
5593: //puts(line);
5594: fputs(line,ficparo);
5595: fputs(line,ficlog);
5596: }
5597: ungetc(c,ficpar);
5598:
5599:
5600: covar=matrix(0,NCOVMAX,1,n); /**< used in readdata */
5601: cptcovn=0; /*Number of covariates, i.e. number of '+' in model statement plus one, indepently of n in Vn*/
5602: /* v1+v2+v3+v2*v4+v5*age makes cptcovn = 5
5603: v1+v2*age+v2*v3 makes cptcovn = 3
5604: */
5605: if (strlen(model)>1)
5606: ncovmodel=2+nbocc(model,'+')+1; /*Number of variables including intercept and age = cptcovn + intercept + age : v1+v2+v3+v2*v4+v5*age makes 5+2=7*/
5607: else
5608: ncovmodel=2;
5609: nvar=ncovmodel-1; /* Suppressing age as a basic covariate */
5610: nforce= (nlstate+ndeath-1)*nlstate; /* Number of forces ij from state i to j */
5611: npar= nforce*ncovmodel; /* Number of parameters like aij*/
5612: if(npar >MAXPARM || nlstate >NLSTATEMAX || ndeath >NDEATHMAX || ncovmodel>NCOVMAX){
5613: printf("Too complex model for current IMaCh: npar=(nlstate+ndeath-1)*nlstate*ncovmodel=%d >= %d(MAXPARM) or nlstate=%d >= %d(NLSTATEMAX) or ndeath=%d >= %d(NDEATHMAX) or ncovmodel=(k+age+#of+signs)=%d(NCOVMAX) >= %d\n",npar, MAXPARM, nlstate, NLSTATEMAX, ndeath, NDEATHMAX, ncovmodel, NCOVMAX);
5614: fprintf(ficlog,"Too complex model for current IMaCh: %d >=%d(MAXPARM) or %d >=%d(NLSTATEMAX) or %d >=%d(NDEATHMAX) or %d(NCOVMAX) >=%d\n",npar, MAXPARM, nlstate, NLSTATEMAX, ndeath, NDEATHMAX, ncovmodel, NCOVMAX);
5615: fflush(stdout);
5616: fclose (ficlog);
5617: goto end;
5618: }
5619: delti3= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
5620: delti=delti3[1][1];
5621: /*delti=vector(1,npar); *//* Scale of each paramater (output from hesscov)*/
5622: if(mle==-1){ /* Print a wizard for help writing covariance matrix */
5623: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
5624: printf(" You choose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
5625: fprintf(ficlog," You choose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
5626: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
5627: fclose (ficparo);
5628: fclose (ficlog);
5629: goto end;
5630: exit(0);
5631: }
5632: else if(mle==-3) {
5633: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
5634: printf(" You choose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
5635: fprintf(ficlog," You choose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
5636: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
5637: matcov=matrix(1,npar,1,npar);
5638: }
5639: else{
5640: /* Read guessed parameters */
5641: /* Reads comments: lines beginning with '#' */
5642: while((c=getc(ficpar))=='#' && c!= EOF){
5643: ungetc(c,ficpar);
5644: fgets(line, MAXLINE, ficpar);
5645: numlinepar++;
5646: fputs(line,stdout);
5647: fputs(line,ficparo);
5648: fputs(line,ficlog);
5649: }
5650: ungetc(c,ficpar);
5651:
5652: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
5653: for(i=1; i <=nlstate; i++){
5654: j=0;
5655: for(jj=1; jj <=nlstate+ndeath; jj++){
5656: if(jj==i) continue;
5657: j++;
5658: fscanf(ficpar,"%1d%1d",&i1,&j1);
5659: if ((i1 != i) && (j1 != j)){
5660: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n \
5661: It might be a problem of design; if ncovcol and the model are correct\n \
5662: run imach with mle=-1 to get a correct template of the parameter file.\n",numlinepar, i,j, i1, j1);
5663: exit(1);
5664: }
5665: fprintf(ficparo,"%1d%1d",i1,j1);
5666: if(mle==1)
5667: printf("%1d%1d",i,j);
5668: fprintf(ficlog,"%1d%1d",i,j);
5669: for(k=1; k<=ncovmodel;k++){
5670: fscanf(ficpar," %lf",¶m[i][j][k]);
5671: if(mle==1){
5672: printf(" %lf",param[i][j][k]);
5673: fprintf(ficlog," %lf",param[i][j][k]);
5674: }
5675: else
5676: fprintf(ficlog," %lf",param[i][j][k]);
5677: fprintf(ficparo," %lf",param[i][j][k]);
5678: }
5679: fscanf(ficpar,"\n");
5680: numlinepar++;
5681: if(mle==1)
5682: printf("\n");
5683: fprintf(ficlog,"\n");
5684: fprintf(ficparo,"\n");
5685: }
5686: }
5687: fflush(ficlog);
5688:
5689: /* Reads scales values */
5690: p=param[1][1];
5691:
5692: /* Reads comments: lines beginning with '#' */
5693: while((c=getc(ficpar))=='#' && c!= EOF){
5694: ungetc(c,ficpar);
5695: fgets(line, MAXLINE, ficpar);
5696: numlinepar++;
5697: fputs(line,stdout);
5698: fputs(line,ficparo);
5699: fputs(line,ficlog);
5700: }
5701: ungetc(c,ficpar);
5702:
5703: for(i=1; i <=nlstate; i++){
5704: for(j=1; j <=nlstate+ndeath-1; j++){
5705: fscanf(ficpar,"%1d%1d",&i1,&j1);
5706: if ((i1-i)*(j1-j)!=0){
5707: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n",numlinepar, i,j, i1, j1);
5708: exit(1);
5709: }
5710: printf("%1d%1d",i,j);
5711: fprintf(ficparo,"%1d%1d",i1,j1);
5712: fprintf(ficlog,"%1d%1d",i1,j1);
5713: for(k=1; k<=ncovmodel;k++){
5714: fscanf(ficpar,"%le",&delti3[i][j][k]);
5715: printf(" %le",delti3[i][j][k]);
5716: fprintf(ficparo," %le",delti3[i][j][k]);
5717: fprintf(ficlog," %le",delti3[i][j][k]);
5718: }
5719: fscanf(ficpar,"\n");
5720: numlinepar++;
5721: printf("\n");
5722: fprintf(ficparo,"\n");
5723: fprintf(ficlog,"\n");
5724: }
5725: }
5726: fflush(ficlog);
5727:
5728: /* Reads covariance matrix */
5729: delti=delti3[1][1];
5730:
5731:
5732: /* free_ma3x(delti3,1,nlstate,1,nlstate+ndeath-1,1,ncovmodel); */ /* Hasn't to to freed here otherwise delti is no more allocated */
5733:
5734: /* Reads comments: lines beginning with '#' */
5735: while((c=getc(ficpar))=='#' && c!= EOF){
5736: ungetc(c,ficpar);
5737: fgets(line, MAXLINE, ficpar);
5738: numlinepar++;
5739: fputs(line,stdout);
5740: fputs(line,ficparo);
5741: fputs(line,ficlog);
5742: }
5743: ungetc(c,ficpar);
5744:
5745: matcov=matrix(1,npar,1,npar);
5746: for(i=1; i <=npar; i++)
5747: for(j=1; j <=npar; j++) matcov[i][j]=0.;
5748:
5749: for(i=1; i <=npar; i++){
5750: fscanf(ficpar,"%s",str);
5751: if(mle==1)
5752: printf("%s",str);
5753: fprintf(ficlog,"%s",str);
5754: fprintf(ficparo,"%s",str);
5755: for(j=1; j <=i; j++){
5756: fscanf(ficpar," %le",&matcov[i][j]);
5757: if(mle==1){
5758: printf(" %.5le",matcov[i][j]);
5759: }
5760: fprintf(ficlog," %.5le",matcov[i][j]);
5761: fprintf(ficparo," %.5le",matcov[i][j]);
5762: }
5763: fscanf(ficpar,"\n");
5764: numlinepar++;
5765: if(mle==1)
5766: printf("\n");
5767: fprintf(ficlog,"\n");
5768: fprintf(ficparo,"\n");
5769: }
5770: for(i=1; i <=npar; i++)
5771: for(j=i+1;j<=npar;j++)
5772: matcov[i][j]=matcov[j][i];
5773:
5774: if(mle==1)
5775: printf("\n");
5776: fprintf(ficlog,"\n");
5777:
5778: fflush(ficlog);
5779:
5780: /*-------- Rewriting parameter file ----------*/
5781: strcpy(rfileres,"r"); /* "Rparameterfile */
5782: strcat(rfileres,optionfilefiname); /* Parameter file first name*/
5783: strcat(rfileres,"."); /* */
5784: strcat(rfileres,optionfilext); /* Other files have txt extension */
5785: if((ficres =fopen(rfileres,"w"))==NULL) {
5786: printf("Problem writing new parameter file: %s\n", fileres);goto end;
5787: fprintf(ficlog,"Problem writing new parameter file: %s\n", fileres);goto end;
5788: }
5789: fprintf(ficres,"#%s\n",version);
5790: } /* End of mle != -3 */
5791:
5792:
5793: n= lastobs;
5794: num=lvector(1,n);
5795: moisnais=vector(1,n);
5796: annais=vector(1,n);
5797: moisdc=vector(1,n);
5798: andc=vector(1,n);
5799: agedc=vector(1,n);
5800: cod=ivector(1,n);
5801: weight=vector(1,n);
5802: for(i=1;i<=n;i++) weight[i]=1.0; /* Equal weights, 1 by default */
5803: mint=matrix(1,maxwav,1,n);
5804: anint=matrix(1,maxwav,1,n);
5805: s=imatrix(1,maxwav+1,1,n); /* s[i][j] health state for wave i and individual j */
5806: tab=ivector(1,NCOVMAX);
5807: ncodemax=ivector(1,NCOVMAX); /* Number of code per covariate; if O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */
5808:
5809: /* Reads data from file datafile */
5810: if (readdata(datafile, firstobs, lastobs, &imx)==1)
5811: goto end;
5812:
5813: /* Calculation of the number of parameters from char model */
5814: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4
5815: k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tag[cptcovage=1]=4
5816: k=3 V4 Tvar[k=3]= 4 (from V4)
5817: k=2 V1 Tvar[k=2]= 1 (from V1)
5818: k=1 Tvar[1]=2 (from V2)
5819: */
5820: Tvar=ivector(1,NCOVMAX); /* Was 15 changed to NCOVMAX. */
5821: /* V2+V1+V4+age*V3 is a model with 4 covariates (3 plus signs).
5822: For each model-covariate stores the data-covariate id. Tvar[1]=2, Tvar[2]=1, Tvar[3]=4,
5823: Tvar[4=age*V3] is 3 and 'age' is recorded in Tage.
5824: */
5825: /* For model-covariate k tells which data-covariate to use but
5826: because this model-covariate is a construction we invent a new column
5827: ncovcol + k1
5828: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3
5829: Tvar[3=V1*V4]=4+1 etc */
5830: Tprod=ivector(1,NCOVMAX); /* Gives the position of a product */
5831: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3
5832: if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2)
5833: */
5834: Tvaraff=ivector(1,NCOVMAX); /* Unclear */
5835: Tvard=imatrix(1,NCOVMAX,1,2); /* n=Tvard[k1][1] and m=Tvard[k1][2] gives the couple n,m of the k1 th product Vn*Vm
5836: * For V3*V2 (in V2+V1+V1*V4+age*V3+V3*V2), V3*V2 position is 2nd.
5837: * Tvard[k1=2][1]=3 (V3) Tvard[k1=2][2]=2(V2) */
5838: Tage=ivector(1,NCOVMAX); /* Gives the covariate id of covariates associated with age: V2 + V1 + age*V4 + V3*age
5839: 4 covariates (3 plus signs)
5840: Tage[1=V3*age]= 4; Tage[2=age*V4] = 3
5841: */
5842:
5843: if(decodemodel(model, lastobs) == 1)
5844: goto end;
5845:
5846: if((double)(lastobs-imx)/(double)imx > 1.10){
5847: nbwarn++;
5848: printf("Warning: The value of parameter lastobs=%d is big compared to the \n effective number of cases imx=%d, please adjust, \n otherwise you are allocating more memory than necessary.\n",lastobs, imx);
5849: fprintf(ficlog,"Warning: The value of parameter lastobs=%d is big compared to the \n effective number of cases imx=%d, please adjust, \n otherwise you are allocating more memory than necessary.\n",lastobs, imx);
5850: }
5851: /* if(mle==1){*/
5852: if (weightopt != 1) { /* Maximisation without weights. We can have weights different from 1 but want no weight*/
5853: for(i=1;i<=imx;i++) weight[i]=1.0; /* changed to imx */
5854: }
5855:
5856: /*-calculation of age at interview from date of interview and age at death -*/
5857: agev=matrix(1,maxwav,1,imx);
5858:
5859: if(calandcheckages(imx, maxwav, &agemin, &agemax, &nberr, &nbwarn) == 1)
5860: goto end;
5861:
5862:
5863: agegomp=(int)agemin;
5864: free_vector(moisnais,1,n);
5865: free_vector(annais,1,n);
5866: /* free_matrix(mint,1,maxwav,1,n);
5867: free_matrix(anint,1,maxwav,1,n);*/
5868: free_vector(moisdc,1,n);
5869: free_vector(andc,1,n);
5870: /* */
5871:
5872: wav=ivector(1,imx);
5873: dh=imatrix(1,lastpass-firstpass+1,1,imx);
5874: bh=imatrix(1,lastpass-firstpass+1,1,imx);
5875: mw=imatrix(1,lastpass-firstpass+1,1,imx);
5876:
5877: /* Concatenates waves */
5878: concatwav(wav, dh, bh, mw, s, agedc, agev, firstpass, lastpass, imx, nlstate, stepm);
5879: /* */
5880:
5881: /* Routine tricode is to calculate cptcoveff (real number of unique covariates) and to associate covariable number and modality */
5882:
5883: nbcode=imatrix(0,NCOVMAX,0,NCOVMAX);
5884: ncodemax[1]=1;
5885: Ndum =ivector(-1,NCOVMAX);
5886: if (ncovmodel > 2)
5887: tricode(Tvar,nbcode,imx, Ndum); /**< Fills nbcode[Tvar[j]][l]; */
5888:
5889: codtab=imatrix(1,100,1,10); /* codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) */
5890: /*printf(" codtab[1,1],codtab[100,10]=%d,%d\n", codtab[1][1],codtab[100][10]);*/
5891: h=0;
5892:
5893:
5894: /*if (cptcovn > 0) */
5895:
5896:
5897: m=pow(2,cptcoveff);
5898:
5899: for(k=1;k<=cptcoveff; k++){ /* scans any effective covariate */
5900: for(i=1; i <=pow(2,cptcoveff-k);i++){ /* i=1 to 8/1=8; i=1 to 8/2=4; i=1 to 8/8=1 */
5901: for(j=1; j <= ncodemax[k]; j++){ /* For each modality of this covariate ncodemax=2*/
5902: for(cpt=1; cpt <=pow(2,k-1); cpt++){ /* cpt=1 to 8/2**(3+1-1 or 3+1-3) =1 or 4 */
5903: h++;
5904: if (h>m)
5905: h=1;
5906: /**< codtab(h,k) k = codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) + 1
5907: * h 1 2 3 4
5908: *______________________________
5909: * 1 i=1 1 i=1 1 i=1 1 i=1 1
5910: * 2 2 1 1 1
5911: * 3 i=2 1 2 1 1
5912: * 4 2 2 1 1
5913: * 5 i=3 1 i=2 1 2 1
5914: * 6 2 1 2 1
5915: * 7 i=4 1 2 2 1
5916: * 8 2 2 2 1
5917: * 9 i=5 1 i=3 1 i=2 1 1
5918: * 10 2 1 1 1
5919: * 11 i=6 1 2 1 1
5920: * 12 2 2 1 1
5921: * 13 i=7 1 i=4 1 2 1
5922: * 14 2 1 2 1
5923: * 15 i=8 1 2 2 1
5924: * 16 2 2 2 1
5925: */
5926: codtab[h][k]=j;
5927: /*codtab[h][Tvar[k]]=j;*/
5928: printf("h=%d k=%d j=%d codtab[h][k]=%d Tvar[k]=%d codtab[h][Tvar[k]]=%d \n",h, k,j,codtab[h][k],Tvar[k],codtab[h][Tvar[k]]);
5929: }
5930: }
5931: }
5932: }
5933: /* printf("codtab[1][2]=%d codtab[2][2]=%d",codtab[1][2],codtab[2][2]);
5934: codtab[1][2]=1;codtab[2][2]=2; */
5935: /* for(i=1; i <=m ;i++){
5936: for(k=1; k <=cptcovn; k++){
5937: printf("i=%d k=%d %d %d ",i,k,codtab[i][k], cptcoveff);
5938: }
5939: printf("\n");
5940: }
5941: scanf("%d",i);*/
5942:
5943: free_ivector(Ndum,-1,NCOVMAX);
5944:
5945:
5946:
5947: /*------------ gnuplot -------------*/
5948: strcpy(optionfilegnuplot,optionfilefiname);
5949: if(mle==-3)
5950: strcat(optionfilegnuplot,"-mort");
5951: strcat(optionfilegnuplot,".gp");
5952:
5953: if((ficgp=fopen(optionfilegnuplot,"w"))==NULL) {
5954: printf("Problem with file %s",optionfilegnuplot);
5955: }
5956: else{
5957: fprintf(ficgp,"\n# %s\n", version);
5958: fprintf(ficgp,"# %s\n", optionfilegnuplot);
5959: //fprintf(ficgp,"set missing 'NaNq'\n");
5960: fprintf(ficgp,"set datafile missing 'NaNq'\n");
5961: }
5962: /* fclose(ficgp);*/
5963: /*--------- index.htm --------*/
5964:
5965: strcpy(optionfilehtm,optionfilefiname); /* Main html file */
5966: if(mle==-3)
5967: strcat(optionfilehtm,"-mort");
5968: strcat(optionfilehtm,".htm");
5969: if((fichtm=fopen(optionfilehtm,"w"))==NULL) {
5970: printf("Problem with %s \n",optionfilehtm);
5971: exit(0);
5972: }
5973:
5974: strcpy(optionfilehtmcov,optionfilefiname); /* Only for matrix of covariance */
5975: strcat(optionfilehtmcov,"-cov.htm");
5976: if((fichtmcov=fopen(optionfilehtmcov,"w"))==NULL) {
5977: printf("Problem with %s \n",optionfilehtmcov), exit(0);
5978: }
5979: else{
5980: fprintf(fichtmcov,"<html><head>\n<title>IMaCh Cov %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
5981: <hr size=\"2\" color=\"#EC5E5E\"> \n\
5982: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=%s<br>\n",\
5983: optionfilehtmcov,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
5984: }
5985:
5986: fprintf(fichtm,"<html><head>\n<title>IMaCh %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
5987: <hr size=\"2\" color=\"#EC5E5E\"> \n\
5988: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=%s<br>\n\
5989: \n\
5990: <hr size=\"2\" color=\"#EC5E5E\">\
5991: <ul><li><h4>Parameter files</h4>\n\
5992: - Parameter file: <a href=\"%s.%s\">%s.%s</a><br>\n\
5993: - Copy of the parameter file: <a href=\"o%s\">o%s</a><br>\n\
5994: - Log file of the run: <a href=\"%s\">%s</a><br>\n\
5995: - Gnuplot file name: <a href=\"%s\">%s</a><br>\n\
5996: - Date and time at start: %s</ul>\n",\
5997: optionfilehtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model,\
5998: optionfilefiname,optionfilext,optionfilefiname,optionfilext,\
5999: fileres,fileres,\
6000: filelog,filelog,optionfilegnuplot,optionfilegnuplot,strstart);
6001: fflush(fichtm);
6002:
6003: strcpy(pathr,path);
6004: strcat(pathr,optionfilefiname);
6005: chdir(optionfilefiname); /* Move to directory named optionfile */
6006:
6007: /* Calculates basic frequencies. Computes observed prevalence at single age
6008: and prints on file fileres'p'. */
6009: freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart);
6010:
6011: fprintf(fichtm,"\n");
6012: fprintf(fichtm,"<br>Total number of observations=%d <br>\n\
6013: Youngest age at first (selected) pass %.2f, oldest age %.2f<br>\n\
6014: Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n",\
6015: imx,agemin,agemax,jmin,jmax,jmean);
6016: pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
6017: oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
6018: newms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
6019: savms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
6020: oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */
6021:
6022:
6023: /* For Powell, parameters are in a vector p[] starting at p[1]
6024: so we point p on param[1][1] so that p[1] maps on param[1][1][1] */
6025: p=param[1][1]; /* *(*(*(param +1)+1)+0) */
6026:
6027: globpr=0; /* To get the number ipmx of contributions and the sum of weights*/
6028:
6029: if (mle==-3){
6030: ximort=matrix(1,NDIM,1,NDIM);
6031: /* ximort=gsl_matrix_alloc(1,NDIM,1,NDIM); */
6032: cens=ivector(1,n);
6033: ageexmed=vector(1,n);
6034: agecens=vector(1,n);
6035: dcwave=ivector(1,n);
6036:
6037: for (i=1; i<=imx; i++){
6038: dcwave[i]=-1;
6039: for (m=firstpass; m<=lastpass; m++)
6040: if (s[m][i]>nlstate) {
6041: dcwave[i]=m;
6042: /* printf("i=%d j=%d s=%d dcwave=%d\n",i,j, s[j][i],dcwave[i]);*/
6043: break;
6044: }
6045: }
6046:
6047: for (i=1; i<=imx; i++) {
6048: if (wav[i]>0){
6049: ageexmed[i]=agev[mw[1][i]][i];
6050: j=wav[i];
6051: agecens[i]=1.;
6052:
6053: if (ageexmed[i]> 1 && wav[i] > 0){
6054: agecens[i]=agev[mw[j][i]][i];
6055: cens[i]= 1;
6056: }else if (ageexmed[i]< 1)
6057: cens[i]= -1;
6058: if (agedc[i]< AGESUP && agedc[i]>1 && dcwave[i]>firstpass && dcwave[i]<=lastpass)
6059: cens[i]=0 ;
6060: }
6061: else cens[i]=-1;
6062: }
6063:
6064: for (i=1;i<=NDIM;i++) {
6065: for (j=1;j<=NDIM;j++)
6066: ximort[i][j]=(i == j ? 1.0 : 0.0);
6067: }
6068:
6069: /*p[1]=0.0268; p[NDIM]=0.083;*/
6070: /*printf("%lf %lf", p[1], p[2]);*/
6071:
6072:
6073: #ifdef GSL
6074: printf("GSL optimization\n"); fprintf(ficlog,"Powell\n");
6075: #else
6076: printf("Powell\n"); fprintf(ficlog,"Powell\n");
6077: #endif
6078: strcpy(filerespow,"pow-mort");
6079: strcat(filerespow,fileres);
6080: if((ficrespow=fopen(filerespow,"w"))==NULL) {
6081: printf("Problem with resultfile: %s\n", filerespow);
6082: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
6083: }
6084: #ifdef GSL
6085: fprintf(ficrespow,"# GSL optimization\n# iter -2*LL");
6086: #else
6087: fprintf(ficrespow,"# Powell\n# iter -2*LL");
6088: #endif
6089: /* for (i=1;i<=nlstate;i++)
6090: for(j=1;j<=nlstate+ndeath;j++)
6091: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
6092: */
6093: fprintf(ficrespow,"\n");
6094: #ifdef GSL
6095: /* gsl starts here */
6096: T = gsl_multimin_fminimizer_nmsimplex;
6097: gsl_multimin_fminimizer *sfm = NULL;
6098: gsl_vector *ss, *x;
6099: gsl_multimin_function minex_func;
6100:
6101: /* Initial vertex size vector */
6102: ss = gsl_vector_alloc (NDIM);
6103:
6104: if (ss == NULL){
6105: GSL_ERROR_VAL ("failed to allocate space for ss", GSL_ENOMEM, 0);
6106: }
6107: /* Set all step sizes to 1 */
6108: gsl_vector_set_all (ss, 0.001);
6109:
6110: /* Starting point */
6111:
6112: x = gsl_vector_alloc (NDIM);
6113:
6114: if (x == NULL){
6115: gsl_vector_free(ss);
6116: GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0);
6117: }
6118:
6119: /* Initialize method and iterate */
6120: /* p[1]=0.0268; p[NDIM]=0.083; */
6121: /* gsl_vector_set(x, 0, 0.0268); */
6122: /* gsl_vector_set(x, 1, 0.083); */
6123: gsl_vector_set(x, 0, p[1]);
6124: gsl_vector_set(x, 1, p[2]);
6125:
6126: minex_func.f = &gompertz_f;
6127: minex_func.n = NDIM;
6128: minex_func.params = (void *)&p; /* ??? */
6129:
6130: sfm = gsl_multimin_fminimizer_alloc (T, NDIM);
6131: gsl_multimin_fminimizer_set (sfm, &minex_func, x, ss);
6132:
6133: printf("Iterations beginning .....\n\n");
6134: printf("Iter. # Intercept Slope -Log Likelihood Simplex size\n");
6135:
6136: iteri=0;
6137: while (rval == GSL_CONTINUE){
6138: iteri++;
6139: status = gsl_multimin_fminimizer_iterate(sfm);
6140:
6141: if (status) printf("error: %s\n", gsl_strerror (status));
6142: fflush(0);
6143:
6144: if (status)
6145: break;
6146:
6147: rval = gsl_multimin_test_size (gsl_multimin_fminimizer_size (sfm), 1e-6);
6148: ssval = gsl_multimin_fminimizer_size (sfm);
6149:
6150: if (rval == GSL_SUCCESS)
6151: printf ("converged to a local maximum at\n");
6152:
6153: printf("%5d ", iteri);
6154: for (it = 0; it < NDIM; it++){
6155: printf ("%10.5f ", gsl_vector_get (sfm->x, it));
6156: }
6157: printf("f() = %-10.5f ssize = %.7f\n", sfm->fval, ssval);
6158: }
6159:
6160: printf("\n\n Please note: Program should be run many times with varying starting points to detemine global maximum\n\n");
6161:
6162: gsl_vector_free(x); /* initial values */
6163: gsl_vector_free(ss); /* inital step size */
6164: for (it=0; it<NDIM; it++){
6165: p[it+1]=gsl_vector_get(sfm->x,it);
6166: fprintf(ficrespow," %.12lf", p[it]);
6167: }
6168: gsl_multimin_fminimizer_free (sfm); /* p *(sfm.x.data) et p *(sfm.x.data+1) */
6169: #endif
6170: #ifdef POWELL
6171: powell(p,ximort,NDIM,ftol,&iter,&fret,gompertz);
6172: #endif
6173: fclose(ficrespow);
6174:
6175: hesscov(matcov, p, NDIM, delti, 1e-4, gompertz);
6176:
6177: for(i=1; i <=NDIM; i++)
6178: for(j=i+1;j<=NDIM;j++)
6179: matcov[i][j]=matcov[j][i];
6180:
6181: printf("\nCovariance matrix\n ");
6182: for(i=1; i <=NDIM; i++) {
6183: for(j=1;j<=NDIM;j++){
6184: printf("%f ",matcov[i][j]);
6185: }
6186: printf("\n ");
6187: }
6188:
6189: printf("iter=%d MLE=%f Eq=%lf*exp(%lf*(age-%d))\n",iter,-gompertz(p),p[1],p[2],agegomp);
6190: for (i=1;i<=NDIM;i++)
6191: printf("%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
6192:
6193: lsurv=vector(1,AGESUP);
6194: lpop=vector(1,AGESUP);
6195: tpop=vector(1,AGESUP);
6196: lsurv[agegomp]=100000;
6197:
6198: for (k=agegomp;k<=AGESUP;k++) {
6199: agemortsup=k;
6200: if (p[1]*exp(p[2]*(k-agegomp))>1) break;
6201: }
6202:
6203: for (k=agegomp;k<agemortsup;k++)
6204: lsurv[k+1]=lsurv[k]-lsurv[k]*(p[1]*exp(p[2]*(k-agegomp)));
6205:
6206: for (k=agegomp;k<agemortsup;k++){
6207: lpop[k]=(lsurv[k]+lsurv[k+1])/2.;
6208: sumlpop=sumlpop+lpop[k];
6209: }
6210:
6211: tpop[agegomp]=sumlpop;
6212: for (k=agegomp;k<(agemortsup-3);k++){
6213: /* tpop[k+1]=2;*/
6214: tpop[k+1]=tpop[k]-lpop[k];
6215: }
6216:
6217:
6218: printf("\nAge lx qx dx Lx Tx e(x)\n");
6219: for (k=agegomp;k<(agemortsup-2);k++)
6220: printf("%d %.0lf %lf %.0lf %.0lf %.0lf %lf\n",k,lsurv[k],p[1]*exp(p[2]*(k-agegomp)),(p[1]*exp(p[2]*(k-agegomp)))*lsurv[k],lpop[k],tpop[k],tpop[k]/lsurv[k]);
6221:
6222:
6223: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
6224: printinggnuplotmort(fileres, optionfilefiname,ageminpar,agemaxpar,fage, pathc,p);
6225:
6226: printinghtmlmort(fileres,title,datafile, firstpass, lastpass, \
6227: stepm, weightopt,\
6228: model,imx,p,matcov,agemortsup);
6229:
6230: free_vector(lsurv,1,AGESUP);
6231: free_vector(lpop,1,AGESUP);
6232: free_vector(tpop,1,AGESUP);
6233: #ifdef GSL
6234: free_ivector(cens,1,n);
6235: free_vector(agecens,1,n);
6236: free_ivector(dcwave,1,n);
6237: free_matrix(ximort,1,NDIM,1,NDIM);
6238: #endif
6239: } /* Endof if mle==-3 */
6240:
6241: else{ /* For mle >=1 */
6242: globpr=0;/* debug */
6243: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
6244: printf("First Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
6245: for (k=1; k<=npar;k++)
6246: printf(" %d %8.5f",k,p[k]);
6247: printf("\n");
6248: globpr=1; /* to print the contributions */
6249: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
6250: printf("Second Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
6251: for (k=1; k<=npar;k++)
6252: printf(" %d %8.5f",k,p[k]);
6253: printf("\n");
6254: if(mle>=1){ /* Could be 1 or 2 */
6255: mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func);
6256: }
6257:
6258: /*--------- results files --------------*/
6259: fprintf(ficres,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle= 0 weight=%d\nmodel=%s\n", title, datafile, lastobs, firstpass,lastpass,ftol, stepm, ncovcol, nlstate, ndeath, maxwav, weightopt,model);
6260:
6261:
6262: fprintf(ficres,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
6263: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
6264: fprintf(ficlog,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
6265: for(i=1,jk=1; i <=nlstate; i++){
6266: for(k=1; k <=(nlstate+ndeath); k++){
6267: if (k != i) {
6268: printf("%d%d ",i,k);
6269: fprintf(ficlog,"%d%d ",i,k);
6270: fprintf(ficres,"%1d%1d ",i,k);
6271: for(j=1; j <=ncovmodel; j++){
6272: printf("%lf ",p[jk]);
6273: fprintf(ficlog,"%lf ",p[jk]);
6274: fprintf(ficres,"%lf ",p[jk]);
6275: jk++;
6276: }
6277: printf("\n");
6278: fprintf(ficlog,"\n");
6279: fprintf(ficres,"\n");
6280: }
6281: }
6282: }
6283: if(mle!=0){
6284: /* Computing hessian and covariance matrix */
6285: ftolhess=ftol; /* Usually correct */
6286: hesscov(matcov, p, npar, delti, ftolhess, func);
6287: }
6288: fprintf(ficres,"# Scales (for hessian or gradient estimation)\n");
6289: printf("# Scales (for hessian or gradient estimation)\n");
6290: fprintf(ficlog,"# Scales (for hessian or gradient estimation)\n");
6291: for(i=1,jk=1; i <=nlstate; i++){
6292: for(j=1; j <=nlstate+ndeath; j++){
6293: if (j!=i) {
6294: fprintf(ficres,"%1d%1d",i,j);
6295: printf("%1d%1d",i,j);
6296: fprintf(ficlog,"%1d%1d",i,j);
6297: for(k=1; k<=ncovmodel;k++){
6298: printf(" %.5e",delti[jk]);
6299: fprintf(ficlog," %.5e",delti[jk]);
6300: fprintf(ficres," %.5e",delti[jk]);
6301: jk++;
6302: }
6303: printf("\n");
6304: fprintf(ficlog,"\n");
6305: fprintf(ficres,"\n");
6306: }
6307: }
6308: }
6309:
6310: fprintf(ficres,"# Covariance matrix \n# 121 Var(a12)\n# 122 Cov(b12,a12) Var(b12)\n# ...\n# 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n");
6311: if(mle>=1)
6312: printf("# Covariance matrix \n# 121 Var(a12)\n# 122 Cov(b12,a12) Var(b12)\n# ...\n# 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n");
6313: fprintf(ficlog,"# Covariance matrix \n# 121 Var(a12)\n# 122 Cov(b12,a12) Var(b12)\n# ...\n# 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n");
6314: /* # 121 Var(a12)\n\ */
6315: /* # 122 Cov(b12,a12) Var(b12)\n\ */
6316: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
6317: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
6318: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
6319: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
6320: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
6321: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
6322:
6323:
6324: /* Just to have a covariance matrix which will be more understandable
6325: even is we still don't want to manage dictionary of variables
6326: */
6327: for(itimes=1;itimes<=2;itimes++){
6328: jj=0;
6329: for(i=1; i <=nlstate; i++){
6330: for(j=1; j <=nlstate+ndeath; j++){
6331: if(j==i) continue;
6332: for(k=1; k<=ncovmodel;k++){
6333: jj++;
6334: ca[0]= k+'a'-1;ca[1]='\0';
6335: if(itimes==1){
6336: if(mle>=1)
6337: printf("#%1d%1d%d",i,j,k);
6338: fprintf(ficlog,"#%1d%1d%d",i,j,k);
6339: fprintf(ficres,"#%1d%1d%d",i,j,k);
6340: }else{
6341: if(mle>=1)
6342: printf("%1d%1d%d",i,j,k);
6343: fprintf(ficlog,"%1d%1d%d",i,j,k);
6344: fprintf(ficres,"%1d%1d%d",i,j,k);
6345: }
6346: ll=0;
6347: for(li=1;li <=nlstate; li++){
6348: for(lj=1;lj <=nlstate+ndeath; lj++){
6349: if(lj==li) continue;
6350: for(lk=1;lk<=ncovmodel;lk++){
6351: ll++;
6352: if(ll<=jj){
6353: cb[0]= lk +'a'-1;cb[1]='\0';
6354: if(ll<jj){
6355: if(itimes==1){
6356: if(mle>=1)
6357: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
6358: fprintf(ficlog," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
6359: fprintf(ficres," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
6360: }else{
6361: if(mle>=1)
6362: printf(" %.5e",matcov[jj][ll]);
6363: fprintf(ficlog," %.5e",matcov[jj][ll]);
6364: fprintf(ficres," %.5e",matcov[jj][ll]);
6365: }
6366: }else{
6367: if(itimes==1){
6368: if(mle>=1)
6369: printf(" Var(%s%1d%1d)",ca,i,j);
6370: fprintf(ficlog," Var(%s%1d%1d)",ca,i,j);
6371: fprintf(ficres," Var(%s%1d%1d)",ca,i,j);
6372: }else{
6373: if(mle>=1)
6374: printf(" %.5e",matcov[jj][ll]);
6375: fprintf(ficlog," %.5e",matcov[jj][ll]);
6376: fprintf(ficres," %.5e",matcov[jj][ll]);
6377: }
6378: }
6379: }
6380: } /* end lk */
6381: } /* end lj */
6382: } /* end li */
6383: if(mle>=1)
6384: printf("\n");
6385: fprintf(ficlog,"\n");
6386: fprintf(ficres,"\n");
6387: numlinepar++;
6388: } /* end k*/
6389: } /*end j */
6390: } /* end i */
6391: } /* end itimes */
6392:
6393: fflush(ficlog);
6394: fflush(ficres);
6395:
6396: while((c=getc(ficpar))=='#' && c!= EOF){
6397: ungetc(c,ficpar);
6398: fgets(line, MAXLINE, ficpar);
6399: fputs(line,stdout);
6400: fputs(line,ficparo);
6401: }
6402: ungetc(c,ficpar);
6403:
6404: estepm=0;
6405: fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d\n",&ageminpar,&agemaxpar, &bage, &fage, &estepm);
6406: if (estepm==0 || estepm < stepm) estepm=stepm;
6407: if (fage <= 2) {
6408: bage = ageminpar;
6409: fage = agemaxpar;
6410: }
6411:
6412: fprintf(ficres,"# agemin agemax for life expectancy, bage fage (if mle==0 ie no data nor Max likelihood).\n");
6413: fprintf(ficres,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d\n",ageminpar,agemaxpar,bage,fage, estepm);
6414: fprintf(ficparo,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d\n",ageminpar,agemaxpar,bage,fage, estepm);
6415:
6416: while((c=getc(ficpar))=='#' && c!= EOF){
6417: ungetc(c,ficpar);
6418: fgets(line, MAXLINE, ficpar);
6419: fputs(line,stdout);
6420: fputs(line,ficparo);
6421: }
6422: ungetc(c,ficpar);
6423:
6424: fscanf(ficpar,"begin-prev-date=%lf/%lf/%lf end-prev-date=%lf/%lf/%lf mov_average=%d\n",&jprev1, &mprev1,&anprev1,&jprev2, &mprev2,&anprev2,&mobilav);
6425: fprintf(ficparo,"begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mov_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
6426: fprintf(ficres,"begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mov_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
6427: printf("begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mov_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
6428: fprintf(ficlog,"begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mov_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
6429:
6430: while((c=getc(ficpar))=='#' && c!= EOF){
6431: ungetc(c,ficpar);
6432: fgets(line, MAXLINE, ficpar);
6433: fputs(line,stdout);
6434: fputs(line,ficparo);
6435: }
6436: ungetc(c,ficpar);
6437:
6438:
6439: dateprev1=anprev1+(mprev1-1)/12.+(jprev1-1)/365.;
6440: dateprev2=anprev2+(mprev2-1)/12.+(jprev2-1)/365.;
6441:
6442: fscanf(ficpar,"pop_based=%d\n",&popbased);
6443: fprintf(ficparo,"pop_based=%d\n",popbased);
6444: fprintf(ficres,"pop_based=%d\n",popbased);
6445:
6446: while((c=getc(ficpar))=='#' && c!= EOF){
6447: ungetc(c,ficpar);
6448: fgets(line, MAXLINE, ficpar);
6449: fputs(line,stdout);
6450: fputs(line,ficparo);
6451: }
6452: ungetc(c,ficpar);
6453:
6454: fscanf(ficpar,"prevforecast=%d starting-proj-date=%lf/%lf/%lf final-proj-date=%lf/%lf/%lf mobil_average=%d\n",&prevfcast,&jproj1,&mproj1,&anproj1,&jproj2,&mproj2,&anproj2,&mobilavproj);
6455: fprintf(ficparo,"prevforecast=%d starting-proj-date=%.lf/%.lf/%.lf final-proj-date=%.lf/%.lf/%.lf mobil_average=%d\n",prevfcast,jproj1,mproj1,anproj1,jproj2,mproj2,anproj2,mobilavproj);
6456: printf("prevforecast=%d starting-proj-date=%.lf/%.lf/%.lf final-proj-date=%.lf/%.lf/%.lf mobil_average=%d\n",prevfcast,jproj1,mproj1,anproj1,jproj2,mproj2,anproj2,mobilavproj);
6457: fprintf(ficlog,"prevforecast=%d starting-proj-date=%.lf/%.lf/%.lf final-proj-date=%.lf/%.lf/%.lf mobil_average=%d\n",prevfcast,jproj1,mproj1,anproj1,jproj2,mproj2,anproj2,mobilavproj);
6458: fprintf(ficres,"prevforecast=%d starting-proj-date=%.lf/%.lf/%.lf final-proj-date=%.lf/%.lf/%.lf mobil_average=%d\n",prevfcast,jproj1,mproj1,anproj1,jproj2,mproj2,anproj2,mobilavproj);
6459: /* day and month of proj2 are not used but only year anproj2.*/
6460:
6461:
6462:
6463: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint); */
6464: /* ,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2); */
6465:
6466: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
6467: printinggnuplot(fileres, optionfilefiname,ageminpar,agemaxpar,fage, pathc,p);
6468:
6469: printinghtml(fileres,title,datafile, firstpass, lastpass, stepm, weightopt,\
6470: model,imx,jmin,jmax,jmean,rfileres,popforecast,estepm,\
6471: jprev1,mprev1,anprev1,jprev2,mprev2,anprev2);
6472:
6473: /*------------ free_vector -------------*/
6474: /* chdir(path); */
6475:
6476: free_ivector(wav,1,imx);
6477: free_imatrix(dh,1,lastpass-firstpass+1,1,imx);
6478: free_imatrix(bh,1,lastpass-firstpass+1,1,imx);
6479: free_imatrix(mw,1,lastpass-firstpass+1,1,imx);
6480: free_lvector(num,1,n);
6481: free_vector(agedc,1,n);
6482: /*free_matrix(covar,0,NCOVMAX,1,n);*/
6483: /*free_matrix(covar,1,NCOVMAX,1,n);*/
6484: fclose(ficparo);
6485: fclose(ficres);
6486:
6487:
6488: /*--------------- Prevalence limit (period or stable prevalence) --------------*/
6489: #include "prevlim.h" /* Use ficrespl, ficlog */
6490: fclose(ficrespl);
6491:
6492: #ifdef FREEEXIT2
6493: #include "freeexit2.h"
6494: #endif
6495:
6496: /*------------- h Pij x at various ages ------------*/
6497: #include "hpijx.h"
6498: fclose(ficrespij);
6499:
6500: /*-------------- Variance of one-step probabilities---*/
6501: k=1;
6502: varprob(optionfilefiname, matcov, p, delti, nlstate, bage, fage,k,Tvar,nbcode, ncodemax,strstart);
6503:
6504:
6505: probs= ma3x(1,AGESUP,1,NCOVMAX, 1,NCOVMAX);
6506: for(i=1;i<=AGESUP;i++)
6507: for(j=1;j<=NCOVMAX;j++)
6508: for(k=1;k<=NCOVMAX;k++)
6509: probs[i][j][k]=0.;
6510:
6511: /*---------- Forecasting ------------------*/
6512: /*if((stepm == 1) && (strcmp(model,".")==0)){*/
6513: if(prevfcast==1){
6514: /* if(stepm ==1){*/
6515: prevforecast(fileres, anproj1, mproj1, jproj1, agemin, agemax, dateprev1, dateprev2, mobilavproj, bage, fage, firstpass, lastpass, anproj2, p, cptcoveff);
6516: /* (popforecast==1) populforecast(fileres, anpyram,mpyram,jpyram, agemin,agemax, dateprev1, dateprev2,mobilav, agedeb, fage, popforecast, popfile, anpyram1,p, i1);*/
6517: /* } */
6518: /* else{ */
6519: /* erreur=108; */
6520: /* printf("Warning %d!! You can only forecast the prevalences if the optimization\n has been performed with stepm = 1 (month) instead of %d or model=. instead of '%s'\n", erreur, stepm, model); */
6521: /* fprintf(ficlog,"Warning %d!! You can only forecast the prevalences if the optimization\n has been performed with stepm = 1 (month) instead of %d or model=. instead of '%s'\n", erreur, stepm, model); */
6522: /* } */
6523: }
6524:
6525:
6526: /* Computes prevalence between agemin (i.e minimal age computed) and no more ageminpar */
6527:
6528: prevalence(probs, agemin, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
6529: /* printf("ageminpar=%f, agemax=%f, s[lastpass][imx]=%d, agev[lastpass][imx]=%f, nlstate=%d, imx=%d, mint[lastpass][imx]=%f, anint[lastpass][imx]=%f,dateprev1=%f, dateprev2=%f, firstpass=%d, lastpass=%d\n",\
6530: ageminpar, agemax, s[lastpass][imx], agev[lastpass][imx], nlstate, imx, mint[lastpass][imx],anint[lastpass][imx], dateprev1, dateprev2, firstpass, lastpass);
6531: */
6532:
6533: if (mobilav!=0) {
6534: mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
6535: if (movingaverage(probs, bage, fage, mobaverage,mobilav)!=0){
6536: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
6537: printf(" Error in movingaverage mobilav=%d\n",mobilav);
6538: }
6539: }
6540:
6541:
6542: /*---------- Health expectancies, no variances ------------*/
6543:
6544: strcpy(filerese,"e");
6545: strcat(filerese,fileres);
6546: if((ficreseij=fopen(filerese,"w"))==NULL) {
6547: printf("Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
6548: fprintf(ficlog,"Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
6549: }
6550: printf("Computing Health Expectancies: result on file '%s' \n", filerese);
6551: fprintf(ficlog,"Computing Health Expectancies: result on file '%s' \n", filerese);
6552: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
6553: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
6554:
6555: for (k=1; k <= (int) pow(2,cptcoveff); k++){
6556: fprintf(ficreseij,"\n#****** ");
6557: for(j=1;j<=cptcoveff;j++) {
6558: fprintf(ficreseij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
6559: }
6560: fprintf(ficreseij,"******\n");
6561:
6562: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
6563: oldm=oldms;savm=savms;
6564: evsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, strstart);
6565:
6566: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
6567: /*}*/
6568: }
6569: fclose(ficreseij);
6570:
6571:
6572: /*---------- Health expectancies and variances ------------*/
6573:
6574:
6575: strcpy(filerest,"t");
6576: strcat(filerest,fileres);
6577: if((ficrest=fopen(filerest,"w"))==NULL) {
6578: printf("Problem with total LE resultfile: %s\n", filerest);goto end;
6579: fprintf(ficlog,"Problem with total LE resultfile: %s\n", filerest);goto end;
6580: }
6581: printf("Computing Total Life expectancies with their standard errors: file '%s' \n", filerest);
6582: fprintf(ficlog,"Computing Total Life expectancies with their standard errors: file '%s' \n", filerest);
6583:
6584:
6585: strcpy(fileresstde,"stde");
6586: strcat(fileresstde,fileres);
6587: if((ficresstdeij=fopen(fileresstde,"w"))==NULL) {
6588: printf("Problem with Health Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
6589: fprintf(ficlog,"Problem with Health Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
6590: }
6591: printf("Computing Health Expectancies and standard errors: result on file '%s' \n", fileresstde);
6592: fprintf(ficlog,"Computing Health Expectancies and standard errors: result on file '%s' \n", fileresstde);
6593:
6594: strcpy(filerescve,"cve");
6595: strcat(filerescve,fileres);
6596: if((ficrescveij=fopen(filerescve,"w"))==NULL) {
6597: printf("Problem with Covar. Health Exp. resultfile: %s\n", filerescve); exit(0);
6598: fprintf(ficlog,"Problem with Covar. Health Exp. resultfile: %s\n", filerescve); exit(0);
6599: }
6600: printf("Computing Covar. of Health Expectancies: result on file '%s' \n", filerescve);
6601: fprintf(ficlog,"Computing Covar. of Health Expectancies: result on file '%s' \n", filerescve);
6602:
6603: strcpy(fileresv,"v");
6604: strcat(fileresv,fileres);
6605: if((ficresvij=fopen(fileresv,"w"))==NULL) {
6606: printf("Problem with variance resultfile: %s\n", fileresv);exit(0);
6607: fprintf(ficlog,"Problem with variance resultfile: %s\n", fileresv);exit(0);
6608: }
6609: printf("Computing Variance-covariance of DFLEs: file '%s' \n", fileresv);
6610: fprintf(ficlog,"Computing Variance-covariance of DFLEs: file '%s' \n", fileresv);
6611:
6612: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
6613: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
6614:
6615: for (k=1; k <= (int) pow(2,cptcoveff); k++){
6616: fprintf(ficrest,"\n#****** ");
6617: for(j=1;j<=cptcoveff;j++)
6618: fprintf(ficrest,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
6619: fprintf(ficrest,"******\n");
6620:
6621: fprintf(ficresstdeij,"\n#****** ");
6622: fprintf(ficrescveij,"\n#****** ");
6623: for(j=1;j<=cptcoveff;j++) {
6624: fprintf(ficresstdeij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
6625: fprintf(ficrescveij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
6626: }
6627: fprintf(ficresstdeij,"******\n");
6628: fprintf(ficrescveij,"******\n");
6629:
6630: fprintf(ficresvij,"\n#****** ");
6631: for(j=1;j<=cptcoveff;j++)
6632: fprintf(ficresvij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
6633: fprintf(ficresvij,"******\n");
6634:
6635: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
6636: oldm=oldms;savm=savms;
6637: cvevsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, delti, matcov, strstart);
6638: /*
6639: */
6640: /* goto endfree; */
6641:
6642: vareij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
6643: pstamp(ficrest);
6644:
6645:
6646: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
6647: oldm=oldms;savm=savms; /* Segmentation fault */
6648: cptcod= 0; /* To be deleted */
6649: varevsij(optionfilefiname, vareij, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl,k, estepm, cptcov,cptcod,vpopbased,mobilav, strstart); /* cptcod not initialized Intel */
6650: fprintf(ficrest,"# Total life expectancy with std error and decomposition into time to be expected in each health state\n# (weighted average of eij where weights are ");
6651: if(vpopbased==1)
6652: fprintf(ficrest,"the age specific prevalence observed (cross-sectionally) in the population i.e cross-sectionally\n in each health state (popbased=1) (mobilav=%d)\n",mobilav);
6653: else
6654: fprintf(ficrest,"the age specific period (stable) prevalences in each health state \n");
6655: fprintf(ficrest,"# Age e.. (std) ");
6656: for (i=1;i<=nlstate;i++) fprintf(ficrest,"e.%d (std) ",i);
6657: fprintf(ficrest,"\n");
6658:
6659: epj=vector(1,nlstate+1);
6660: for(age=bage; age <=fage ;age++){
6661: prevalim(prlim, nlstate, p, age, oldm, savm,ftolpl,k);
6662: if (vpopbased==1) {
6663: if(mobilav ==0){
6664: for(i=1; i<=nlstate;i++)
6665: prlim[i][i]=probs[(int)age][i][k];
6666: }else{ /* mobilav */
6667: for(i=1; i<=nlstate;i++)
6668: prlim[i][i]=mobaverage[(int)age][i][k];
6669: }
6670: }
6671:
6672: fprintf(ficrest," %4.0f",age);
6673: for(j=1, epj[nlstate+1]=0.;j <=nlstate;j++){
6674: for(i=1, epj[j]=0.;i <=nlstate;i++) {
6675: epj[j] += prlim[i][i]*eij[i][j][(int)age];
6676: /* printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]);*/
6677: }
6678: epj[nlstate+1] +=epj[j];
6679: }
6680:
6681: for(i=1, vepp=0.;i <=nlstate;i++)
6682: for(j=1;j <=nlstate;j++)
6683: vepp += vareij[i][j][(int)age];
6684: fprintf(ficrest," %7.3f (%7.3f)", epj[nlstate+1],sqrt(vepp));
6685: for(j=1;j <=nlstate;j++){
6686: fprintf(ficrest," %7.3f (%7.3f)", epj[j],sqrt(vareij[j][j][(int)age]));
6687: }
6688: fprintf(ficrest,"\n");
6689: }
6690: }
6691: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
6692: free_ma3x(vareij,1,nlstate,1,nlstate,(int) bage, (int)fage);
6693: free_vector(epj,1,nlstate+1);
6694: /*}*/
6695: }
6696: free_vector(weight,1,n);
6697: free_imatrix(Tvard,1,NCOVMAX,1,2);
6698: free_imatrix(s,1,maxwav+1,1,n);
6699: free_matrix(anint,1,maxwav,1,n);
6700: free_matrix(mint,1,maxwav,1,n);
6701: free_ivector(cod,1,n);
6702: free_ivector(tab,1,NCOVMAX);
6703: fclose(ficresstdeij);
6704: fclose(ficrescveij);
6705: fclose(ficresvij);
6706: fclose(ficrest);
6707: fclose(ficpar);
6708:
6709: /*------- Variance of period (stable) prevalence------*/
6710:
6711: strcpy(fileresvpl,"vpl");
6712: strcat(fileresvpl,fileres);
6713: if((ficresvpl=fopen(fileresvpl,"w"))==NULL) {
6714: printf("Problem with variance of period (stable) prevalence resultfile: %s\n", fileresvpl);
6715: exit(0);
6716: }
6717: printf("Computing Variance-covariance of period (stable) prevalence: file '%s' \n", fileresvpl);
6718:
6719: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
6720: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
6721:
6722: for (k=1; k <= (int) pow(2,cptcoveff); k++){
6723: fprintf(ficresvpl,"\n#****** ");
6724: for(j=1;j<=cptcoveff;j++)
6725: fprintf(ficresvpl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
6726: fprintf(ficresvpl,"******\n");
6727:
6728: varpl=matrix(1,nlstate,(int) bage, (int) fage);
6729: oldm=oldms;savm=savms;
6730: varprevlim(fileres, varpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl,k,strstart);
6731: free_matrix(varpl,1,nlstate,(int) bage, (int)fage);
6732: /*}*/
6733: }
6734:
6735: fclose(ficresvpl);
6736:
6737: /*---------- End : free ----------------*/
6738: if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
6739: free_ma3x(probs,1,AGESUP,1,NCOVMAX, 1,NCOVMAX);
6740: } /* mle==-3 arrives here for freeing */
6741: endfree:
6742: free_matrix(prlim,1,nlstate,1,nlstate); /*here or after loop ? */
6743: free_matrix(pmmij,1,nlstate+ndeath,1,nlstate+ndeath);
6744: free_matrix(oldms, 1,nlstate+ndeath,1,nlstate+ndeath);
6745: free_matrix(newms, 1,nlstate+ndeath,1,nlstate+ndeath);
6746: free_matrix(savms, 1,nlstate+ndeath,1,nlstate+ndeath);
6747: free_matrix(covar,0,NCOVMAX,1,n);
6748: free_matrix(matcov,1,npar,1,npar);
6749: /*free_vector(delti,1,npar);*/
6750: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
6751: free_matrix(agev,1,maxwav,1,imx);
6752: free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
6753:
6754: free_ivector(ncodemax,1,NCOVMAX);
6755: free_ivector(Tvar,1,NCOVMAX);
6756: free_ivector(Tprod,1,NCOVMAX);
6757: free_ivector(Tvaraff,1,NCOVMAX);
6758: free_ivector(Tage,1,NCOVMAX);
6759:
6760: free_imatrix(nbcode,0,NCOVMAX,0,NCOVMAX);
6761: free_imatrix(codtab,1,100,1,10);
6762: fflush(fichtm);
6763: fflush(ficgp);
6764:
6765:
6766: if((nberr >0) || (nbwarn>0)){
6767: printf("End of Imach with %d errors and/or %d warnings\n",nberr,nbwarn);
6768: fprintf(ficlog,"End of Imach with %d errors and/or warnings %d\n",nberr,nbwarn);
6769: }else{
6770: printf("End of Imach\n");
6771: fprintf(ficlog,"End of Imach\n");
6772: }
6773: printf("See log file on %s\n",filelog);
6774: /* gettimeofday(&end_time, (struct timezone*)0);*/ /* after time */
6775: /*(void) gettimeofday(&end_time,&tzp);*/
6776: rend_time = time(NULL);
6777: end_time = *localtime(&rend_time);
6778: /* tml = *localtime(&end_time.tm_sec); */
6779: strcpy(strtend,asctime(&end_time));
6780: printf("Local time at start %s\nLocal time at end %s",strstart, strtend);
6781: fprintf(ficlog,"Local time at start %s\nLocal time at end %s\n",strstart, strtend);
6782: printf("Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
6783:
6784: printf("Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
6785: fprintf(ficlog,"Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
6786: fprintf(ficlog,"Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
6787: /* printf("Total time was %d uSec.\n", total_usecs);*/
6788: /* if(fileappend(fichtm,optionfilehtm)){ */
6789: fprintf(fichtm,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
6790: fclose(fichtm);
6791: fprintf(fichtmcov,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
6792: fclose(fichtmcov);
6793: fclose(ficgp);
6794: fclose(ficlog);
6795: /*------ End -----------*/
6796:
6797:
6798: printf("Before Current directory %s!\n",pathcd);
6799: if(chdir(pathcd) != 0)
6800: printf("Can't move to directory %s!\n",path);
6801: if(getcwd(pathcd,MAXLINE) > 0)
6802: printf("Current directory %s!\n",pathcd);
6803: /*strcat(plotcmd,CHARSEPARATOR);*/
6804: sprintf(plotcmd,"gnuplot");
6805: #ifdef _WIN32
6806: sprintf(plotcmd,"\"%sgnuplot.exe\"",pathimach);
6807: #endif
6808: if(!stat(plotcmd,&info)){
6809: printf("Error or gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
6810: if(!stat(getenv("GNUPLOTBIN"),&info)){
6811: printf("Error or gnuplot program not found: '%s' Environment GNUPLOTBIN not set.\n",plotcmd);fflush(stdout);
6812: }else
6813: strcpy(pplotcmd,plotcmd);
6814: #ifdef __unix
6815: strcpy(plotcmd,GNUPLOTPROGRAM);
6816: if(!stat(plotcmd,&info)){
6817: printf("Error gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
6818: }else
6819: strcpy(pplotcmd,plotcmd);
6820: #endif
6821: }else
6822: strcpy(pplotcmd,plotcmd);
6823:
6824: sprintf(plotcmd,"%s %s",pplotcmd, optionfilegnuplot);
6825: printf("Starting graphs with: '%s'\n",plotcmd);fflush(stdout);
6826:
6827: if((outcmd=system(plotcmd)) != 0){
6828: printf("gnuplot command might not be in your path: '%s', err=%d\n", plotcmd, outcmd);
6829: printf("\n Trying if gnuplot resides on the same directory that IMaCh\n");
6830: sprintf(plotcmd,"%sgnuplot %s", pathimach, optionfilegnuplot);
6831: if((outcmd=system(plotcmd)) != 0)
6832: printf("\n Still a problem with gnuplot command %s, err=%d\n", plotcmd, outcmd);
6833: }
6834: printf(" Successful, please wait...");
6835: while (z[0] != 'q') {
6836: /* chdir(path); */
6837: printf("\nType e to edit results with your browser, g to graph again and q for exit: ");
6838: scanf("%s",z);
6839: /* if (z[0] == 'c') system("./imach"); */
6840: if (z[0] == 'e') {
6841: #ifdef __APPLE__
6842: sprintf(pplotcmd, "open %s", optionfilehtm);
6843: #elif __linux
6844: sprintf(pplotcmd, "xdg-open %s", optionfilehtm);
6845: #else
6846: sprintf(pplotcmd, "%s", optionfilehtm);
6847: #endif
6848: printf("Starting browser with: %s",pplotcmd);fflush(stdout);
6849: system(pplotcmd);
6850: }
6851: else if (z[0] == 'g') system(plotcmd);
6852: else if (z[0] == 'q') exit(0);
6853: }
6854: end:
6855: while (z[0] != 'q') {
6856: printf("\nType q for exiting: ");
6857: scanf("%s",z);
6858: }
6859: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>