Annotation of imach/src/imach.c, revision 1.166
1.166 ! brouard 1: /* $Id: imach.c,v 1.165 2014/12/16 11:20:36 brouard Exp $
1.126 brouard 2: $State: Exp $
1.163 brouard 3: $Log: imach.c,v $
1.166 ! brouard 4: Revision 1.165 2014/12/16 11:20:36 brouard
! 5: Summary: After compiling on Visual C
! 6:
! 7: * imach.c (Module): Merging 1.61 to 1.162
! 8:
1.165 brouard 9: Revision 1.164 2014/12/16 10:52:11 brouard
10: Summary: Merging with Visual C after suppressing some warnings for unused variables. Also fixing Saito's bug 0.98Xn
11:
12: * imach.c (Module): Merging 1.61 to 1.162
13:
1.164 brouard 14: Revision 1.163 2014/12/16 10:30:11 brouard
15: * imach.c (Module): Merging 1.61 to 1.162
16:
1.163 brouard 17: Revision 1.162 2014/09/25 11:43:39 brouard
18: Summary: temporary backup 0.99!
19:
1.162 brouard 20: Revision 1.1 2014/09/16 11:06:58 brouard
21: Summary: With some code (wrong) for nlopt
22:
23: Author:
24:
25: Revision 1.161 2014/09/15 20:41:41 brouard
26: Summary: Problem with macro SQR on Intel compiler
27:
1.161 brouard 28: Revision 1.160 2014/09/02 09:24:05 brouard
29: *** empty log message ***
30:
1.160 brouard 31: Revision 1.159 2014/09/01 10:34:10 brouard
32: Summary: WIN32
33: Author: Brouard
34:
1.159 brouard 35: Revision 1.158 2014/08/27 17:11:51 brouard
36: *** empty log message ***
37:
1.158 brouard 38: Revision 1.157 2014/08/27 16:26:55 brouard
39: Summary: Preparing windows Visual studio version
40: Author: Brouard
41:
42: In order to compile on Visual studio, time.h is now correct and time_t
43: and tm struct should be used. difftime should be used but sometimes I
44: just make the differences in raw time format (time(&now).
45: Trying to suppress #ifdef LINUX
46: Add xdg-open for __linux in order to open default browser.
47:
1.157 brouard 48: Revision 1.156 2014/08/25 20:10:10 brouard
49: *** empty log message ***
50:
1.156 brouard 51: Revision 1.155 2014/08/25 18:32:34 brouard
52: Summary: New compile, minor changes
53: Author: Brouard
54:
1.155 brouard 55: Revision 1.154 2014/06/20 17:32:08 brouard
56: Summary: Outputs now all graphs of convergence to period prevalence
57:
1.154 brouard 58: Revision 1.153 2014/06/20 16:45:46 brouard
59: Summary: If 3 live state, convergence to period prevalence on same graph
60: Author: Brouard
61:
1.153 brouard 62: Revision 1.152 2014/06/18 17:54:09 brouard
63: Summary: open browser, use gnuplot on same dir than imach if not found in the path
64:
1.152 brouard 65: Revision 1.151 2014/06/18 16:43:30 brouard
66: *** empty log message ***
67:
1.151 brouard 68: Revision 1.150 2014/06/18 16:42:35 brouard
69: Summary: If gnuplot is not in the path try on same directory than imach binary (OSX)
70: Author: brouard
71:
1.150 brouard 72: Revision 1.149 2014/06/18 15:51:14 brouard
73: Summary: Some fixes in parameter files errors
74: Author: Nicolas Brouard
75:
1.149 brouard 76: Revision 1.148 2014/06/17 17:38:48 brouard
77: Summary: Nothing new
78: Author: Brouard
79:
80: Just a new packaging for OS/X version 0.98nS
81:
1.148 brouard 82: Revision 1.147 2014/06/16 10:33:11 brouard
83: *** empty log message ***
84:
1.147 brouard 85: Revision 1.146 2014/06/16 10:20:28 brouard
86: Summary: Merge
87: Author: Brouard
88:
89: Merge, before building revised version.
90:
1.146 brouard 91: Revision 1.145 2014/06/10 21:23:15 brouard
92: Summary: Debugging with valgrind
93: Author: Nicolas Brouard
94:
95: Lot of changes in order to output the results with some covariates
96: After the Edimburgh REVES conference 2014, it seems mandatory to
97: improve the code.
98: No more memory valgrind error but a lot has to be done in order to
99: continue the work of splitting the code into subroutines.
100: Also, decodemodel has been improved. Tricode is still not
101: optimal. nbcode should be improved. Documentation has been added in
102: the source code.
103:
1.144 brouard 104: Revision 1.143 2014/01/26 09:45:38 brouard
105: Summary: Version 0.98nR (to be improved, but gives same optimization results as 0.98k. Nice, promising
106:
107: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
108: (Module): Version 0.98nR Running ok, but output format still only works for three covariates.
109:
1.143 brouard 110: Revision 1.142 2014/01/26 03:57:36 brouard
111: Summary: gnuplot changed plot w l 1 has to be changed to plot w l lt 2
112:
113: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
114:
1.142 brouard 115: Revision 1.141 2014/01/26 02:42:01 brouard
116: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
117:
1.141 brouard 118: Revision 1.140 2011/09/02 10:37:54 brouard
119: Summary: times.h is ok with mingw32 now.
120:
1.140 brouard 121: Revision 1.139 2010/06/14 07:50:17 brouard
122: After the theft of my laptop, I probably lost some lines of codes which were not uploaded to the CVS tree.
123: I remember having already fixed agemin agemax which are pointers now but not cvs saved.
124:
1.139 brouard 125: Revision 1.138 2010/04/30 18:19:40 brouard
126: *** empty log message ***
127:
1.138 brouard 128: Revision 1.137 2010/04/29 18:11:38 brouard
129: (Module): Checking covariates for more complex models
130: than V1+V2. A lot of change to be done. Unstable.
131:
1.137 brouard 132: Revision 1.136 2010/04/26 20:30:53 brouard
133: (Module): merging some libgsl code. Fixing computation
134: of likelione (using inter/intrapolation if mle = 0) in order to
135: get same likelihood as if mle=1.
136: Some cleaning of code and comments added.
137:
1.136 brouard 138: Revision 1.135 2009/10/29 15:33:14 brouard
139: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
140:
1.135 brouard 141: Revision 1.134 2009/10/29 13:18:53 brouard
142: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
143:
1.134 brouard 144: Revision 1.133 2009/07/06 10:21:25 brouard
145: just nforces
146:
1.133 brouard 147: Revision 1.132 2009/07/06 08:22:05 brouard
148: Many tings
149:
1.132 brouard 150: Revision 1.131 2009/06/20 16:22:47 brouard
151: Some dimensions resccaled
152:
1.131 brouard 153: Revision 1.130 2009/05/26 06:44:34 brouard
154: (Module): Max Covariate is now set to 20 instead of 8. A
155: lot of cleaning with variables initialized to 0. Trying to make
156: V2+V3*age+V1+V4 strb=V3*age+V1+V4 working better.
157:
1.130 brouard 158: Revision 1.129 2007/08/31 13:49:27 lievre
159: Modification of the way of exiting when the covariate is not binary in order to see on the window the error message before exiting
160:
1.129 lievre 161: Revision 1.128 2006/06/30 13:02:05 brouard
162: (Module): Clarifications on computing e.j
163:
1.128 brouard 164: Revision 1.127 2006/04/28 18:11:50 brouard
165: (Module): Yes the sum of survivors was wrong since
166: imach-114 because nhstepm was no more computed in the age
167: loop. Now we define nhstepma in the age loop.
168: (Module): In order to speed up (in case of numerous covariates) we
169: compute health expectancies (without variances) in a first step
170: and then all the health expectancies with variances or standard
171: deviation (needs data from the Hessian matrices) which slows the
172: computation.
173: In the future we should be able to stop the program is only health
174: expectancies and graph are needed without standard deviations.
175:
1.127 brouard 176: Revision 1.126 2006/04/28 17:23:28 brouard
177: (Module): Yes the sum of survivors was wrong since
178: imach-114 because nhstepm was no more computed in the age
179: loop. Now we define nhstepma in the age loop.
180: Version 0.98h
181:
1.126 brouard 182: Revision 1.125 2006/04/04 15:20:31 lievre
183: Errors in calculation of health expectancies. Age was not initialized.
184: Forecasting file added.
185:
186: Revision 1.124 2006/03/22 17:13:53 lievre
187: Parameters are printed with %lf instead of %f (more numbers after the comma).
188: The log-likelihood is printed in the log file
189:
190: Revision 1.123 2006/03/20 10:52:43 brouard
191: * imach.c (Module): <title> changed, corresponds to .htm file
192: name. <head> headers where missing.
193:
194: * imach.c (Module): Weights can have a decimal point as for
195: English (a comma might work with a correct LC_NUMERIC environment,
196: otherwise the weight is truncated).
197: Modification of warning when the covariates values are not 0 or
198: 1.
199: Version 0.98g
200:
201: Revision 1.122 2006/03/20 09:45:41 brouard
202: (Module): Weights can have a decimal point as for
203: English (a comma might work with a correct LC_NUMERIC environment,
204: otherwise the weight is truncated).
205: Modification of warning when the covariates values are not 0 or
206: 1.
207: Version 0.98g
208:
209: Revision 1.121 2006/03/16 17:45:01 lievre
210: * imach.c (Module): Comments concerning covariates added
211:
212: * imach.c (Module): refinements in the computation of lli if
213: status=-2 in order to have more reliable computation if stepm is
214: not 1 month. Version 0.98f
215:
216: Revision 1.120 2006/03/16 15:10:38 lievre
217: (Module): refinements in the computation of lli if
218: status=-2 in order to have more reliable computation if stepm is
219: not 1 month. Version 0.98f
220:
221: Revision 1.119 2006/03/15 17:42:26 brouard
222: (Module): Bug if status = -2, the loglikelihood was
223: computed as likelihood omitting the logarithm. Version O.98e
224:
225: Revision 1.118 2006/03/14 18:20:07 brouard
226: (Module): varevsij Comments added explaining the second
227: table of variances if popbased=1 .
228: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
229: (Module): Function pstamp added
230: (Module): Version 0.98d
231:
232: Revision 1.117 2006/03/14 17:16:22 brouard
233: (Module): varevsij Comments added explaining the second
234: table of variances if popbased=1 .
235: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
236: (Module): Function pstamp added
237: (Module): Version 0.98d
238:
239: Revision 1.116 2006/03/06 10:29:27 brouard
240: (Module): Variance-covariance wrong links and
241: varian-covariance of ej. is needed (Saito).
242:
243: Revision 1.115 2006/02/27 12:17:45 brouard
244: (Module): One freematrix added in mlikeli! 0.98c
245:
246: Revision 1.114 2006/02/26 12:57:58 brouard
247: (Module): Some improvements in processing parameter
248: filename with strsep.
249:
250: Revision 1.113 2006/02/24 14:20:24 brouard
251: (Module): Memory leaks checks with valgrind and:
252: datafile was not closed, some imatrix were not freed and on matrix
253: allocation too.
254:
255: Revision 1.112 2006/01/30 09:55:26 brouard
256: (Module): Back to gnuplot.exe instead of wgnuplot.exe
257:
258: Revision 1.111 2006/01/25 20:38:18 brouard
259: (Module): Lots of cleaning and bugs added (Gompertz)
260: (Module): Comments can be added in data file. Missing date values
261: can be a simple dot '.'.
262:
263: Revision 1.110 2006/01/25 00:51:50 brouard
264: (Module): Lots of cleaning and bugs added (Gompertz)
265:
266: Revision 1.109 2006/01/24 19:37:15 brouard
267: (Module): Comments (lines starting with a #) are allowed in data.
268:
269: Revision 1.108 2006/01/19 18:05:42 lievre
270: Gnuplot problem appeared...
271: To be fixed
272:
273: Revision 1.107 2006/01/19 16:20:37 brouard
274: Test existence of gnuplot in imach path
275:
276: Revision 1.106 2006/01/19 13:24:36 brouard
277: Some cleaning and links added in html output
278:
279: Revision 1.105 2006/01/05 20:23:19 lievre
280: *** empty log message ***
281:
282: Revision 1.104 2005/09/30 16:11:43 lievre
283: (Module): sump fixed, loop imx fixed, and simplifications.
284: (Module): If the status is missing at the last wave but we know
285: that the person is alive, then we can code his/her status as -2
286: (instead of missing=-1 in earlier versions) and his/her
287: contributions to the likelihood is 1 - Prob of dying from last
288: health status (= 1-p13= p11+p12 in the easiest case of somebody in
289: the healthy state at last known wave). Version is 0.98
290:
291: Revision 1.103 2005/09/30 15:54:49 lievre
292: (Module): sump fixed, loop imx fixed, and simplifications.
293:
294: Revision 1.102 2004/09/15 17:31:30 brouard
295: Add the possibility to read data file including tab characters.
296:
297: Revision 1.101 2004/09/15 10:38:38 brouard
298: Fix on curr_time
299:
300: Revision 1.100 2004/07/12 18:29:06 brouard
301: Add version for Mac OS X. Just define UNIX in Makefile
302:
303: Revision 1.99 2004/06/05 08:57:40 brouard
304: *** empty log message ***
305:
306: Revision 1.98 2004/05/16 15:05:56 brouard
307: New version 0.97 . First attempt to estimate force of mortality
308: directly from the data i.e. without the need of knowing the health
309: state at each age, but using a Gompertz model: log u =a + b*age .
310: This is the basic analysis of mortality and should be done before any
311: other analysis, in order to test if the mortality estimated from the
312: cross-longitudinal survey is different from the mortality estimated
313: from other sources like vital statistic data.
314:
315: The same imach parameter file can be used but the option for mle should be -3.
316:
1.133 brouard 317: Agnès, who wrote this part of the code, tried to keep most of the
1.126 brouard 318: former routines in order to include the new code within the former code.
319:
320: The output is very simple: only an estimate of the intercept and of
321: the slope with 95% confident intervals.
322:
323: Current limitations:
324: A) Even if you enter covariates, i.e. with the
325: model= V1+V2 equation for example, the programm does only estimate a unique global model without covariates.
326: B) There is no computation of Life Expectancy nor Life Table.
327:
328: Revision 1.97 2004/02/20 13:25:42 lievre
329: Version 0.96d. Population forecasting command line is (temporarily)
330: suppressed.
331:
332: Revision 1.96 2003/07/15 15:38:55 brouard
333: * imach.c (Repository): Errors in subdirf, 2, 3 while printing tmpout is
334: rewritten within the same printf. Workaround: many printfs.
335:
336: Revision 1.95 2003/07/08 07:54:34 brouard
337: * imach.c (Repository):
338: (Repository): Using imachwizard code to output a more meaningful covariance
339: matrix (cov(a12,c31) instead of numbers.
340:
341: Revision 1.94 2003/06/27 13:00:02 brouard
342: Just cleaning
343:
344: Revision 1.93 2003/06/25 16:33:55 brouard
345: (Module): On windows (cygwin) function asctime_r doesn't
346: exist so I changed back to asctime which exists.
347: (Module): Version 0.96b
348:
349: Revision 1.92 2003/06/25 16:30:45 brouard
350: (Module): On windows (cygwin) function asctime_r doesn't
351: exist so I changed back to asctime which exists.
352:
353: Revision 1.91 2003/06/25 15:30:29 brouard
354: * imach.c (Repository): Duplicated warning errors corrected.
355: (Repository): Elapsed time after each iteration is now output. It
356: helps to forecast when convergence will be reached. Elapsed time
357: is stamped in powell. We created a new html file for the graphs
358: concerning matrix of covariance. It has extension -cov.htm.
359:
360: Revision 1.90 2003/06/24 12:34:15 brouard
361: (Module): Some bugs corrected for windows. Also, when
362: mle=-1 a template is output in file "or"mypar.txt with the design
363: of the covariance matrix to be input.
364:
365: Revision 1.89 2003/06/24 12:30:52 brouard
366: (Module): Some bugs corrected for windows. Also, when
367: mle=-1 a template is output in file "or"mypar.txt with the design
368: of the covariance matrix to be input.
369:
370: Revision 1.88 2003/06/23 17:54:56 brouard
371: * 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.
372:
373: Revision 1.87 2003/06/18 12:26:01 brouard
374: Version 0.96
375:
376: Revision 1.86 2003/06/17 20:04:08 brouard
377: (Module): Change position of html and gnuplot routines and added
378: routine fileappend.
379:
380: Revision 1.85 2003/06/17 13:12:43 brouard
381: * imach.c (Repository): Check when date of death was earlier that
382: current date of interview. It may happen when the death was just
383: prior to the death. In this case, dh was negative and likelihood
384: was wrong (infinity). We still send an "Error" but patch by
385: assuming that the date of death was just one stepm after the
386: interview.
387: (Repository): Because some people have very long ID (first column)
388: we changed int to long in num[] and we added a new lvector for
389: memory allocation. But we also truncated to 8 characters (left
390: truncation)
391: (Repository): No more line truncation errors.
392:
393: Revision 1.84 2003/06/13 21:44:43 brouard
394: * imach.c (Repository): Replace "freqsummary" at a correct
395: place. It differs from routine "prevalence" which may be called
396: many times. Probs is memory consuming and must be used with
397: parcimony.
398: Version 0.95a3 (should output exactly the same maximization than 0.8a2)
399:
400: Revision 1.83 2003/06/10 13:39:11 lievre
401: *** empty log message ***
402:
403: Revision 1.82 2003/06/05 15:57:20 brouard
404: Add log in imach.c and fullversion number is now printed.
405:
406: */
407: /*
408: Interpolated Markov Chain
409:
410: Short summary of the programme:
411:
412: This program computes Healthy Life Expectancies from
413: cross-longitudinal data. Cross-longitudinal data consist in: -1- a
414: first survey ("cross") where individuals from different ages are
415: interviewed on their health status or degree of disability (in the
416: case of a health survey which is our main interest) -2- at least a
417: second wave of interviews ("longitudinal") which measure each change
418: (if any) in individual health status. Health expectancies are
419: computed from the time spent in each health state according to a
420: model. More health states you consider, more time is necessary to reach the
421: Maximum Likelihood of the parameters involved in the model. The
422: simplest model is the multinomial logistic model where pij is the
423: probability to be observed in state j at the second wave
424: conditional to be observed in state i at the first wave. Therefore
425: the model is: log(pij/pii)= aij + bij*age+ cij*sex + etc , where
426: 'age' is age and 'sex' is a covariate. If you want to have a more
427: complex model than "constant and age", you should modify the program
428: where the markup *Covariates have to be included here again* invites
429: you to do it. More covariates you add, slower the
430: convergence.
431:
432: The advantage of this computer programme, compared to a simple
433: multinomial logistic model, is clear when the delay between waves is not
434: identical for each individual. Also, if a individual missed an
435: intermediate interview, the information is lost, but taken into
436: account using an interpolation or extrapolation.
437:
438: hPijx is the probability to be observed in state i at age x+h
439: conditional to the observed state i at age x. The delay 'h' can be
440: split into an exact number (nh*stepm) of unobserved intermediate
441: states. This elementary transition (by month, quarter,
442: semester or year) is modelled as a multinomial logistic. The hPx
443: matrix is simply the matrix product of nh*stepm elementary matrices
444: and the contribution of each individual to the likelihood is simply
445: hPijx.
446:
447: Also this programme outputs the covariance matrix of the parameters but also
448: of the life expectancies. It also computes the period (stable) prevalence.
449:
1.133 brouard 450: Authors: Nicolas Brouard (brouard@ined.fr) and Agnès Lièvre (lievre@ined.fr).
451: Institut national d'études démographiques, Paris.
1.126 brouard 452: This software have been partly granted by Euro-REVES, a concerted action
453: from the European Union.
454: It is copyrighted identically to a GNU software product, ie programme and
455: software can be distributed freely for non commercial use. Latest version
456: can be accessed at http://euroreves.ined.fr/imach .
457:
458: Help to debug: LD_PRELOAD=/usr/local/lib/libnjamd.so ./imach foo.imach
459: or better on gdb : set env LD_PRELOAD=/usr/local/lib/libnjamd.so
460:
461: **********************************************************************/
462: /*
463: main
464: read parameterfile
465: read datafile
466: concatwav
467: freqsummary
468: if (mle >= 1)
469: mlikeli
470: print results files
471: if mle==1
472: computes hessian
473: read end of parameter file: agemin, agemax, bage, fage, estepm
474: begin-prev-date,...
475: open gnuplot file
476: open html file
1.145 brouard 477: period (stable) prevalence | pl_nom 1-1 2-2 etc by covariate
478: for age prevalim() | #****** V1=0 V2=1 V3=1 V4=0 ******
479: | 65 1 0 2 1 3 1 4 0 0.96326 0.03674
480: freexexit2 possible for memory heap.
481:
482: h Pij x | pij_nom ficrestpij
483: # Cov Agex agex+h hpijx with i,j= 1-1 1-2 1-3 2-1 2-2 2-3
484: 1 85 85 1.00000 0.00000 0.00000 0.00000 1.00000 0.00000
485: 1 85 86 0.68299 0.22291 0.09410 0.71093 0.00000 0.28907
486:
487: 1 65 99 0.00364 0.00322 0.99314 0.00350 0.00310 0.99340
488: 1 65 100 0.00214 0.00204 0.99581 0.00206 0.00196 0.99597
489: variance of p one-step probabilities varprob | prob_nom ficresprob #One-step probabilities and stand. devi in ()
490: Standard deviation of one-step probabilities | probcor_nom ficresprobcor #One-step probabilities and correlation matrix
491: Matrix of variance covariance of one-step probabilities | probcov_nom ficresprobcov #One-step probabilities and covariance matrix
492:
1.126 brouard 493: forecasting if prevfcast==1 prevforecast call prevalence()
494: health expectancies
495: Variance-covariance of DFLE
496: prevalence()
497: movingaverage()
498: varevsij()
499: if popbased==1 varevsij(,popbased)
500: total life expectancies
501: Variance of period (stable) prevalence
502: end
503: */
504:
1.165 brouard 505: #define POWELL /* Instead of NLOPT */
1.126 brouard 506:
507: #include <math.h>
508: #include <stdio.h>
509: #include <stdlib.h>
510: #include <string.h>
1.159 brouard 511:
512: #ifdef _WIN32
513: #include <io.h>
514: #else
1.126 brouard 515: #include <unistd.h>
1.159 brouard 516: #endif
1.126 brouard 517:
518: #include <limits.h>
519: #include <sys/types.h>
520: #include <sys/stat.h>
521: #include <errno.h>
1.159 brouard 522: /* extern int errno; */
1.126 brouard 523:
1.157 brouard 524: /* #ifdef LINUX */
525: /* #include <time.h> */
526: /* #include "timeval.h" */
527: /* #else */
528: /* #include <sys/time.h> */
529: /* #endif */
530:
1.126 brouard 531: #include <time.h>
532:
1.136 brouard 533: #ifdef GSL
534: #include <gsl/gsl_errno.h>
535: #include <gsl/gsl_multimin.h>
536: #endif
537:
1.162 brouard 538: #ifdef NLOPT
539: #include <nlopt.h>
540: typedef struct {
541: double (* function)(double [] );
542: } myfunc_data ;
543: #endif
544:
1.126 brouard 545: /* #include <libintl.h> */
546: /* #define _(String) gettext (String) */
547:
1.141 brouard 548: #define MAXLINE 1024 /* Was 256. Overflow with 312 with 2 states and 4 covariates. Should be ok */
1.126 brouard 549:
550: #define GNUPLOTPROGRAM "gnuplot"
551: /*#define GNUPLOTPROGRAM "..\\gp37mgw\\wgnuplot"*/
552: #define FILENAMELENGTH 132
553:
554: #define GLOCK_ERROR_NOPATH -1 /* empty path */
555: #define GLOCK_ERROR_GETCWD -2 /* cannot get cwd */
556:
1.144 brouard 557: #define MAXPARM 128 /**< Maximum number of parameters for the optimization */
558: #define NPARMAX 64 /**< (nlstate+ndeath-1)*nlstate*ncovmodel */
1.126 brouard 559:
560: #define NINTERVMAX 8
1.144 brouard 561: #define NLSTATEMAX 8 /**< Maximum number of live states (for func) */
562: #define NDEATHMAX 8 /**< Maximum number of dead states (for func) */
563: #define NCOVMAX 20 /**< Maximum number of covariates, including generated covariates V1*V2 */
1.145 brouard 564: #define codtabm(h,k) 1 & (h-1) >> (k-1) ;
1.126 brouard 565: #define MAXN 20000
1.144 brouard 566: #define YEARM 12. /**< Number of months per year */
1.126 brouard 567: #define AGESUP 130
568: #define AGEBASE 40
1.164 brouard 569: #define AGEGOMP 10 /**< Minimal age for Gompertz adjustment */
1.157 brouard 570: #ifdef _WIN32
571: #define DIRSEPARATOR '\\'
572: #define CHARSEPARATOR "\\"
573: #define ODIRSEPARATOR '/'
574: #else
1.126 brouard 575: #define DIRSEPARATOR '/'
576: #define CHARSEPARATOR "/"
577: #define ODIRSEPARATOR '\\'
578: #endif
579:
1.166 ! brouard 580: /* $Id: imach.c,v 1.165 2014/12/16 11:20:36 brouard Exp $ */
1.126 brouard 581: /* $State: Exp $ */
582:
1.162 brouard 583: char version[]="Imach version 0.99, September 2014,INED-EUROREVES-Institut de longevite-Japan Society for the Promotion of Science (Grant-in-Aid for Scientific Research 25293121)";
1.166 ! brouard 584: char fullversion[]="$Revision: 1.165 $ $Date: 2014/12/16 11:20:36 $";
1.126 brouard 585: char strstart[80];
586: char optionfilext[10], optionfilefiname[FILENAMELENGTH];
1.130 brouard 587: int erreur=0, nberr=0, nbwarn=0; /* Error number, number of errors number of warnings */
1.133 brouard 588: int nvar=0, nforce=0; /* Number of variables, number of forces */
1.145 brouard 589: /* Number of covariates model=V2+V1+ V3*age+V2*V4 */
590: int cptcovn=0; /**< cptcovn number of covariates added in the model (excepting constant and age and age*product) */
591: int cptcovt=0; /**< cptcovt number of covariates added in the model (excepting constant and age) */
592: int cptcovs=0; /**< cptcovs number of simple covariates V2+V1 =2 */
593: int cptcovage=0; /**< Number of covariates with age: V3*age only =1 */
594: int cptcovprodnoage=0; /**< Number of covariate products without age */
595: int cptcoveff=0; /* Total number of covariates to vary for printing results */
596: int cptcov=0; /* Working variable */
1.126 brouard 597: int npar=NPARMAX;
598: int nlstate=2; /* Number of live states */
599: int ndeath=1; /* Number of dead states */
1.130 brouard 600: int ncovmodel=0, ncovcol=0; /* Total number of covariables including constant a12*1 +b12*x ncovmodel=2 */
1.126 brouard 601: int popbased=0;
602:
603: int *wav; /* Number of waves for this individuual 0 is possible */
1.130 brouard 604: int maxwav=0; /* Maxim number of waves */
605: int jmin=0, jmax=0; /* min, max spacing between 2 waves */
606: int ijmin=0, ijmax=0; /* Individuals having jmin and jmax */
607: int gipmx=0, gsw=0; /* Global variables on the number of contributions
1.126 brouard 608: to the likelihood and the sum of weights (done by funcone)*/
1.130 brouard 609: int mle=1, weightopt=0;
1.126 brouard 610: int **mw; /* mw[mi][i] is number of the mi wave for this individual */
611: int **dh; /* dh[mi][i] is number of steps between mi,mi+1 for this individual */
612: int **bh; /* bh[mi][i] is the bias (+ or -) for this individual if the delay between
613: * wave mi and wave mi+1 is not an exact multiple of stepm. */
1.162 brouard 614: int countcallfunc=0; /* Count the number of calls to func */
1.130 brouard 615: double jmean=1; /* Mean space between 2 waves */
1.145 brouard 616: double **matprod2(); /* test */
1.126 brouard 617: double **oldm, **newm, **savm; /* Working pointers to matrices */
618: double **oldms, **newms, **savms; /* Fixed working pointers to matrices */
1.136 brouard 619: /*FILE *fic ; */ /* Used in readdata only */
620: FILE *ficpar, *ficparo,*ficres, *ficresp, *ficrespl, *ficrespij, *ficrest,*ficresf,*ficrespop;
1.126 brouard 621: FILE *ficlog, *ficrespow;
1.130 brouard 622: int globpr=0; /* Global variable for printing or not */
1.126 brouard 623: double fretone; /* Only one call to likelihood */
1.130 brouard 624: long ipmx=0; /* Number of contributions */
1.126 brouard 625: double sw; /* Sum of weights */
626: char filerespow[FILENAMELENGTH];
627: char fileresilk[FILENAMELENGTH]; /* File of individual contributions to the likelihood */
628: FILE *ficresilk;
629: FILE *ficgp,*ficresprob,*ficpop, *ficresprobcov, *ficresprobcor;
630: FILE *ficresprobmorprev;
631: FILE *fichtm, *fichtmcov; /* Html File */
632: FILE *ficreseij;
633: char filerese[FILENAMELENGTH];
634: FILE *ficresstdeij;
635: char fileresstde[FILENAMELENGTH];
636: FILE *ficrescveij;
637: char filerescve[FILENAMELENGTH];
638: FILE *ficresvij;
639: char fileresv[FILENAMELENGTH];
640: FILE *ficresvpl;
641: char fileresvpl[FILENAMELENGTH];
642: char title[MAXLINE];
643: char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH], filerespl[FILENAMELENGTH];
644: char plotcmd[FILENAMELENGTH], pplotcmd[FILENAMELENGTH];
645: char tmpout[FILENAMELENGTH], tmpout2[FILENAMELENGTH];
646: char command[FILENAMELENGTH];
647: int outcmd=0;
648:
649: char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filereso[FILENAMELENGTH], rfileres[FILENAMELENGTH];
650:
651: char filelog[FILENAMELENGTH]; /* Log file */
652: char filerest[FILENAMELENGTH];
653: char fileregp[FILENAMELENGTH];
654: char popfile[FILENAMELENGTH];
655:
656: char optionfilegnuplot[FILENAMELENGTH], optionfilehtm[FILENAMELENGTH], optionfilehtmcov[FILENAMELENGTH] ;
657:
1.157 brouard 658: /* struct timeval start_time, end_time, curr_time, last_time, forecast_time; */
659: /* struct timezone tzp; */
660: /* extern int gettimeofday(); */
661: struct tm tml, *gmtime(), *localtime();
662:
663: extern time_t time();
664:
665: struct tm start_time, end_time, curr_time, last_time, forecast_time;
666: time_t rstart_time, rend_time, rcurr_time, rlast_time, rforecast_time; /* raw time */
667: struct tm tm;
668:
1.126 brouard 669: char strcurr[80], strfor[80];
670:
671: char *endptr;
672: long lval;
673: double dval;
674:
675: #define NR_END 1
676: #define FREE_ARG char*
677: #define FTOL 1.0e-10
678:
679: #define NRANSI
680: #define ITMAX 200
681:
682: #define TOL 2.0e-4
683:
684: #define CGOLD 0.3819660
685: #define ZEPS 1.0e-10
686: #define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d);
687:
688: #define GOLD 1.618034
689: #define GLIMIT 100.0
690: #define TINY 1.0e-20
691:
692: static double maxarg1,maxarg2;
693: #define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)>(maxarg2)? (maxarg1):(maxarg2))
694: #define FMIN(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)<(maxarg2)? (maxarg1):(maxarg2))
695:
696: #define SIGN(a,b) ((b)>0.0 ? fabs(a) : -fabs(a))
697: #define rint(a) floor(a+0.5)
1.166 ! brouard 698: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/myutils_8h-source.html */
! 699: /* #define mytinydouble 1.0e-16 */
! 700: /* #define DEQUAL(a,b) (fabs((a)-(b))<mytinydouble) */
! 701: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/mynrutils_8h-source.html */
! 702: /* static double dsqrarg; */
! 703: /* #define DSQR(a) (DEQUAL((dsqrarg=(a)),0.0) ? 0.0 : dsqrarg*dsqrarg) */
1.126 brouard 704: static double sqrarg;
705: #define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 :sqrarg*sqrarg)
706: #define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;}
707: int agegomp= AGEGOMP;
708:
709: int imx;
710: int stepm=1;
711: /* Stepm, step in month: minimum step interpolation*/
712:
713: int estepm;
714: /* Estepm, step in month to interpolate survival function in order to approximate Life Expectancy*/
715:
716: int m,nb;
717: long *num;
718: int firstpass=0, lastpass=4,*cod, *ncodemax, *Tage,*cens;
719: double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint;
720: double **pmmij, ***probs;
721: double *ageexmed,*agecens;
722: double dateintmean=0;
723:
724: double *weight;
725: int **s; /* Status */
1.141 brouard 726: double *agedc;
1.145 brouard 727: double **covar; /**< covar[j,i], value of jth covariate for individual i,
1.141 brouard 728: * covar=matrix(0,NCOVMAX,1,n);
729: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2]; */
730: double idx;
731: int **nbcode, *Tvar; /**< model=V2 => Tvar[1]= 2 */
1.145 brouard 732: int *Ndum; /** Freq of modality (tricode */
1.141 brouard 733: int **codtab; /**< codtab=imatrix(1,100,1,10); */
734: int **Tvard, *Tprod, cptcovprod, *Tvaraff;
1.126 brouard 735: double *lsurv, *lpop, *tpop;
736:
1.143 brouard 737: double ftol=FTOL; /**< Tolerance for computing Max Likelihood */
738: double ftolhess; /**< Tolerance for computing hessian */
1.126 brouard 739:
740: /**************** split *************************/
741: static int split( char *path, char *dirc, char *name, char *ext, char *finame )
742: {
743: /* From a file name with (full) path (either Unix or Windows) we extract the directory (dirc)
744: the name of the file (name), its extension only (ext) and its first part of the name (finame)
745: */
746: char *ss; /* pointer */
747: int l1, l2; /* length counters */
748:
749: l1 = strlen(path ); /* length of path */
750: if ( l1 == 0 ) return( GLOCK_ERROR_NOPATH );
751: ss= strrchr( path, DIRSEPARATOR ); /* find last / */
752: if ( ss == NULL ) { /* no directory, so determine current directory */
753: strcpy( name, path ); /* we got the fullname name because no directory */
754: /*if(strrchr(path, ODIRSEPARATOR )==NULL)
755: printf("Warning you should use %s as a separator\n",DIRSEPARATOR);*/
756: /* get current working directory */
757: /* extern char* getcwd ( char *buf , int len);*/
758: if ( getcwd( dirc, FILENAME_MAX ) == NULL ) {
759: return( GLOCK_ERROR_GETCWD );
760: }
761: /* got dirc from getcwd*/
762: printf(" DIRC = %s \n",dirc);
763: } else { /* strip direcotry from path */
764: ss++; /* after this, the filename */
765: l2 = strlen( ss ); /* length of filename */
766: if ( l2 == 0 ) return( GLOCK_ERROR_NOPATH );
767: strcpy( name, ss ); /* save file name */
768: strncpy( dirc, path, l1 - l2 ); /* now the directory */
769: dirc[l1-l2] = 0; /* add zero */
770: printf(" DIRC2 = %s \n",dirc);
771: }
772: /* We add a separator at the end of dirc if not exists */
773: l1 = strlen( dirc ); /* length of directory */
774: if( dirc[l1-1] != DIRSEPARATOR ){
775: dirc[l1] = DIRSEPARATOR;
776: dirc[l1+1] = 0;
777: printf(" DIRC3 = %s \n",dirc);
778: }
779: ss = strrchr( name, '.' ); /* find last / */
780: if (ss >0){
781: ss++;
782: strcpy(ext,ss); /* save extension */
783: l1= strlen( name);
784: l2= strlen(ss)+1;
785: strncpy( finame, name, l1-l2);
786: finame[l1-l2]= 0;
787: }
788:
789: return( 0 ); /* we're done */
790: }
791:
792:
793: /******************************************/
794:
795: void replace_back_to_slash(char *s, char*t)
796: {
797: int i;
798: int lg=0;
799: i=0;
800: lg=strlen(t);
801: for(i=0; i<= lg; i++) {
802: (s[i] = t[i]);
803: if (t[i]== '\\') s[i]='/';
804: }
805: }
806:
1.132 brouard 807: char *trimbb(char *out, char *in)
1.137 brouard 808: { /* Trim multiple blanks in line but keeps first blanks if line starts with blanks */
1.132 brouard 809: char *s;
810: s=out;
811: while (*in != '\0'){
1.137 brouard 812: while( *in == ' ' && *(in+1) == ' '){ /* && *(in+1) != '\0'){*/
1.132 brouard 813: in++;
814: }
815: *out++ = *in++;
816: }
817: *out='\0';
818: return s;
819: }
820:
1.145 brouard 821: char *cutl(char *blocc, char *alocc, char *in, char occ)
822: {
823: /* cuts string in into blocc and alocc where blocc ends before first occurence of char 'occ'
824: and alocc starts after first occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
825: gives blocc="abcdef2ghi" and alocc="j".
826: If occ is not found blocc is null and alocc is equal to in. Returns blocc
827: */
1.160 brouard 828: char *s, *t;
1.145 brouard 829: t=in;s=in;
830: while ((*in != occ) && (*in != '\0')){
831: *alocc++ = *in++;
832: }
833: if( *in == occ){
834: *(alocc)='\0';
835: s=++in;
836: }
837:
838: if (s == t) {/* occ not found */
839: *(alocc-(in-s))='\0';
840: in=s;
841: }
842: while ( *in != '\0'){
843: *blocc++ = *in++;
844: }
845:
846: *blocc='\0';
847: return t;
848: }
1.137 brouard 849: char *cutv(char *blocc, char *alocc, char *in, char occ)
850: {
851: /* cuts string in into blocc and alocc where blocc ends before last occurence of char 'occ'
852: and alocc starts after last occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
853: gives blocc="abcdef2ghi" and alocc="j".
854: If occ is not found blocc is null and alocc is equal to in. Returns alocc
855: */
856: char *s, *t;
857: t=in;s=in;
858: while (*in != '\0'){
859: while( *in == occ){
860: *blocc++ = *in++;
861: s=in;
862: }
863: *blocc++ = *in++;
864: }
865: if (s == t) /* occ not found */
866: *(blocc-(in-s))='\0';
867: else
868: *(blocc-(in-s)-1)='\0';
869: in=s;
870: while ( *in != '\0'){
871: *alocc++ = *in++;
872: }
873:
874: *alocc='\0';
875: return s;
876: }
877:
1.126 brouard 878: int nbocc(char *s, char occ)
879: {
880: int i,j=0;
881: int lg=20;
882: i=0;
883: lg=strlen(s);
884: for(i=0; i<= lg; i++) {
885: if (s[i] == occ ) j++;
886: }
887: return j;
888: }
889:
1.137 brouard 890: /* void cutv(char *u,char *v, char*t, char occ) */
891: /* { */
892: /* /\* cuts string t into u and v where u ends before last occurence of char 'occ' */
893: /* and v starts after last occurence of char 'occ' : ex cutv(u,v,"abcdef2ghi2j",'2') */
894: /* gives u="abcdef2ghi" and v="j" *\/ */
895: /* int i,lg,j,p=0; */
896: /* i=0; */
897: /* lg=strlen(t); */
898: /* for(j=0; j<=lg-1; j++) { */
899: /* if((t[j]!= occ) && (t[j+1]== occ)) p=j+1; */
900: /* } */
1.126 brouard 901:
1.137 brouard 902: /* for(j=0; j<p; j++) { */
903: /* (u[j] = t[j]); */
904: /* } */
905: /* u[p]='\0'; */
1.126 brouard 906:
1.137 brouard 907: /* for(j=0; j<= lg; j++) { */
908: /* if (j>=(p+1))(v[j-p-1] = t[j]); */
909: /* } */
910: /* } */
1.126 brouard 911:
1.160 brouard 912: #ifdef _WIN32
913: char * strsep(char **pp, const char *delim)
914: {
915: char *p, *q;
916:
917: if ((p = *pp) == NULL)
918: return 0;
919: if ((q = strpbrk (p, delim)) != NULL)
920: {
921: *pp = q + 1;
922: *q = '\0';
923: }
924: else
925: *pp = 0;
926: return p;
927: }
928: #endif
929:
1.126 brouard 930: /********************** nrerror ********************/
931:
932: void nrerror(char error_text[])
933: {
934: fprintf(stderr,"ERREUR ...\n");
935: fprintf(stderr,"%s\n",error_text);
936: exit(EXIT_FAILURE);
937: }
938: /*********************** vector *******************/
939: double *vector(int nl, int nh)
940: {
941: double *v;
942: v=(double *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(double)));
943: if (!v) nrerror("allocation failure in vector");
944: return v-nl+NR_END;
945: }
946:
947: /************************ free vector ******************/
948: void free_vector(double*v, int nl, int nh)
949: {
950: free((FREE_ARG)(v+nl-NR_END));
951: }
952:
953: /************************ivector *******************************/
954: int *ivector(long nl,long nh)
955: {
956: int *v;
957: v=(int *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(int)));
958: if (!v) nrerror("allocation failure in ivector");
959: return v-nl+NR_END;
960: }
961:
962: /******************free ivector **************************/
963: void free_ivector(int *v, long nl, long nh)
964: {
965: free((FREE_ARG)(v+nl-NR_END));
966: }
967:
968: /************************lvector *******************************/
969: long *lvector(long nl,long nh)
970: {
971: long *v;
972: v=(long *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(long)));
973: if (!v) nrerror("allocation failure in ivector");
974: return v-nl+NR_END;
975: }
976:
977: /******************free lvector **************************/
978: void free_lvector(long *v, long nl, long nh)
979: {
980: free((FREE_ARG)(v+nl-NR_END));
981: }
982:
983: /******************* imatrix *******************************/
984: int **imatrix(long nrl, long nrh, long ncl, long nch)
985: /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
986: {
987: long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
988: int **m;
989:
990: /* allocate pointers to rows */
991: m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
992: if (!m) nrerror("allocation failure 1 in matrix()");
993: m += NR_END;
994: m -= nrl;
995:
996:
997: /* allocate rows and set pointers to them */
998: m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
999: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1000: m[nrl] += NR_END;
1001: m[nrl] -= ncl;
1002:
1003: for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
1004:
1005: /* return pointer to array of pointers to rows */
1006: return m;
1007: }
1008:
1009: /****************** free_imatrix *************************/
1010: void free_imatrix(m,nrl,nrh,ncl,nch)
1011: int **m;
1012: long nch,ncl,nrh,nrl;
1013: /* free an int matrix allocated by imatrix() */
1014: {
1015: free((FREE_ARG) (m[nrl]+ncl-NR_END));
1016: free((FREE_ARG) (m+nrl-NR_END));
1017: }
1018:
1019: /******************* matrix *******************************/
1020: double **matrix(long nrl, long nrh, long ncl, long nch)
1021: {
1022: long i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
1023: double **m;
1024:
1025: m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
1026: if (!m) nrerror("allocation failure 1 in matrix()");
1027: m += NR_END;
1028: m -= nrl;
1029:
1030: m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
1031: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1032: m[nrl] += NR_END;
1033: m[nrl] -= ncl;
1034:
1035: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
1036: return m;
1.145 brouard 1037: /* print *(*(m+1)+70) or print m[1][70]; print m+1 or print &(m[1]) or &(m[1][0])
1038: m[i] = address of ith row of the table. &(m[i]) is its value which is another adress
1039: that of m[i][0]. In order to get the value p m[i][0] but it is unitialized.
1.126 brouard 1040: */
1041: }
1042:
1043: /*************************free matrix ************************/
1044: void free_matrix(double **m, long nrl, long nrh, long ncl, long nch)
1045: {
1046: free((FREE_ARG)(m[nrl]+ncl-NR_END));
1047: free((FREE_ARG)(m+nrl-NR_END));
1048: }
1049:
1050: /******************* ma3x *******************************/
1051: double ***ma3x(long nrl, long nrh, long ncl, long nch, long nll, long nlh)
1052: {
1053: long i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1, nlay=nlh-nll+1;
1054: double ***m;
1055:
1056: m=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
1057: if (!m) nrerror("allocation failure 1 in matrix()");
1058: m += NR_END;
1059: m -= nrl;
1060:
1061: m[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
1062: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1063: m[nrl] += NR_END;
1064: m[nrl] -= ncl;
1065:
1066: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
1067:
1068: m[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*nlay+NR_END)*sizeof(double)));
1069: if (!m[nrl][ncl]) nrerror("allocation failure 3 in matrix()");
1070: m[nrl][ncl] += NR_END;
1071: m[nrl][ncl] -= nll;
1072: for (j=ncl+1; j<=nch; j++)
1073: m[nrl][j]=m[nrl][j-1]+nlay;
1074:
1075: for (i=nrl+1; i<=nrh; i++) {
1076: m[i][ncl]=m[i-1l][ncl]+ncol*nlay;
1077: for (j=ncl+1; j<=nch; j++)
1078: m[i][j]=m[i][j-1]+nlay;
1079: }
1080: return m;
1081: /* gdb: p *(m+1) <=> p m[1] and p (m+1) <=> p (m+1) <=> p &(m[1])
1082: &(m[i][j][k]) <=> *((*(m+i) + j)+k)
1083: */
1084: }
1085:
1086: /*************************free ma3x ************************/
1087: void free_ma3x(double ***m, long nrl, long nrh, long ncl, long nch,long nll, long nlh)
1088: {
1089: free((FREE_ARG)(m[nrl][ncl]+ nll-NR_END));
1090: free((FREE_ARG)(m[nrl]+ncl-NR_END));
1091: free((FREE_ARG)(m+nrl-NR_END));
1092: }
1093:
1094: /*************** function subdirf ***********/
1095: char *subdirf(char fileres[])
1096: {
1097: /* Caution optionfilefiname is hidden */
1098: strcpy(tmpout,optionfilefiname);
1099: strcat(tmpout,"/"); /* Add to the right */
1100: strcat(tmpout,fileres);
1101: return tmpout;
1102: }
1103:
1104: /*************** function subdirf2 ***********/
1105: char *subdirf2(char fileres[], char *preop)
1106: {
1107:
1108: /* Caution optionfilefiname is hidden */
1109: strcpy(tmpout,optionfilefiname);
1110: strcat(tmpout,"/");
1111: strcat(tmpout,preop);
1112: strcat(tmpout,fileres);
1113: return tmpout;
1114: }
1115:
1116: /*************** function subdirf3 ***********/
1117: char *subdirf3(char fileres[], char *preop, char *preop2)
1118: {
1119:
1120: /* Caution optionfilefiname is hidden */
1121: strcpy(tmpout,optionfilefiname);
1122: strcat(tmpout,"/");
1123: strcat(tmpout,preop);
1124: strcat(tmpout,preop2);
1125: strcat(tmpout,fileres);
1126: return tmpout;
1127: }
1128:
1.162 brouard 1129: char *asc_diff_time(long time_sec, char ascdiff[])
1130: {
1131: long sec_left, days, hours, minutes;
1132: days = (time_sec) / (60*60*24);
1133: sec_left = (time_sec) % (60*60*24);
1134: hours = (sec_left) / (60*60) ;
1135: sec_left = (sec_left) %(60*60);
1136: minutes = (sec_left) /60;
1137: sec_left = (sec_left) % (60);
1138: sprintf(ascdiff,"%ld day(s) %ld hour(s) %ld minute(s) %ld second(s)",days, hours, minutes, sec_left);
1139: return ascdiff;
1140: }
1141:
1.126 brouard 1142: /***************** f1dim *************************/
1143: extern int ncom;
1144: extern double *pcom,*xicom;
1145: extern double (*nrfunc)(double []);
1146:
1147: double f1dim(double x)
1148: {
1149: int j;
1150: double f;
1151: double *xt;
1152:
1153: xt=vector(1,ncom);
1154: for (j=1;j<=ncom;j++) xt[j]=pcom[j]+x*xicom[j];
1155: f=(*nrfunc)(xt);
1156: free_vector(xt,1,ncom);
1157: return f;
1158: }
1159:
1160: /*****************brent *************************/
1161: double brent(double ax, double bx, double cx, double (*f)(double), double tol, double *xmin)
1162: {
1163: int iter;
1164: double a,b,d,etemp;
1.159 brouard 1165: double fu=0,fv,fw,fx;
1.164 brouard 1166: double ftemp=0.;
1.126 brouard 1167: double p,q,r,tol1,tol2,u,v,w,x,xm;
1168: double e=0.0;
1169:
1170: a=(ax < cx ? ax : cx);
1171: b=(ax > cx ? ax : cx);
1172: x=w=v=bx;
1173: fw=fv=fx=(*f)(x);
1174: for (iter=1;iter<=ITMAX;iter++) {
1175: xm=0.5*(a+b);
1176: tol2=2.0*(tol1=tol*fabs(x)+ZEPS);
1177: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret)))*/
1178: printf(".");fflush(stdout);
1179: fprintf(ficlog,".");fflush(ficlog);
1.162 brouard 1180: #ifdef DEBUGBRENT
1.126 brouard 1181: 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);
1182: 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);
1183: /* if ((fabs(x-xm) <= (tol2-0.5*(b-a)))||(2.0*fabs(fu-ftemp) <= ftol*1.e-2*(fabs(fu)+fabs(ftemp)))) { */
1184: #endif
1185: if (fabs(x-xm) <= (tol2-0.5*(b-a))){
1186: *xmin=x;
1187: return fx;
1188: }
1189: ftemp=fu;
1190: if (fabs(e) > tol1) {
1191: r=(x-w)*(fx-fv);
1192: q=(x-v)*(fx-fw);
1193: p=(x-v)*q-(x-w)*r;
1194: q=2.0*(q-r);
1195: if (q > 0.0) p = -p;
1196: q=fabs(q);
1197: etemp=e;
1198: e=d;
1199: if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x))
1200: d=CGOLD*(e=(x >= xm ? a-x : b-x));
1201: else {
1202: d=p/q;
1203: u=x+d;
1204: if (u-a < tol2 || b-u < tol2)
1205: d=SIGN(tol1,xm-x);
1206: }
1207: } else {
1208: d=CGOLD*(e=(x >= xm ? a-x : b-x));
1209: }
1210: u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d));
1211: fu=(*f)(u);
1212: if (fu <= fx) {
1213: if (u >= x) a=x; else b=x;
1214: SHFT(v,w,x,u)
1215: SHFT(fv,fw,fx,fu)
1216: } else {
1217: if (u < x) a=u; else b=u;
1218: if (fu <= fw || w == x) {
1219: v=w;
1220: w=u;
1221: fv=fw;
1222: fw=fu;
1223: } else if (fu <= fv || v == x || v == w) {
1224: v=u;
1225: fv=fu;
1226: }
1227: }
1228: }
1229: nrerror("Too many iterations in brent");
1230: *xmin=x;
1231: return fx;
1232: }
1233:
1234: /****************** mnbrak ***********************/
1235:
1236: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, double *fc,
1237: double (*func)(double))
1238: {
1239: double ulim,u,r,q, dum;
1240: double fu;
1241:
1242: *fa=(*func)(*ax);
1243: *fb=(*func)(*bx);
1244: if (*fb > *fa) {
1245: SHFT(dum,*ax,*bx,dum)
1246: SHFT(dum,*fb,*fa,dum)
1247: }
1248: *cx=(*bx)+GOLD*(*bx-*ax);
1249: *fc=(*func)(*cx);
1.162 brouard 1250: while (*fb > *fc) { /* Declining fa, fb, fc */
1.126 brouard 1251: r=(*bx-*ax)*(*fb-*fc);
1252: q=(*bx-*cx)*(*fb-*fa);
1253: u=(*bx)-((*bx-*cx)*q-(*bx-*ax)*r)/
1.162 brouard 1254: (2.0*SIGN(FMAX(fabs(q-r),TINY),q-r)); /* Minimum abscisse of a parabolic estimated from (a,fa), (b,fb) and (c,fc). */
1255: ulim=(*bx)+GLIMIT*(*cx-*bx); /* Maximum abscisse where function can be evaluated */
1256: if ((*bx-u)*(u-*cx) > 0.0) { /* if u between b and c */
1.126 brouard 1257: fu=(*func)(u);
1.163 brouard 1258: #ifdef DEBUG
1259: /* f(x)=A(x-u)**2+f(u) */
1260: double A, fparabu;
1261: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
1262: fparabu= *fa - A*(*ax-u)*(*ax-u);
1263: printf("mnbrak (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf), (*u=%.12f, fu=%.12lf, fparabu=%.12f)\n",*ax,*fa,*bx,*fb,*cx,*fc,u,fu, fparabu);
1264: fprintf(ficlog, "mnbrak (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf), (*u=%.12f, fu=%.12lf, fparabu=%.12f)\n",*ax,*fa,*bx,*fb,*cx,*fc,u,fu, fparabu);
1265: #endif
1.162 brouard 1266: } else if ((*cx-u)*(u-ulim) > 0.0) { /* u is after c but before ulim */
1.126 brouard 1267: fu=(*func)(u);
1268: if (fu < *fc) {
1269: SHFT(*bx,*cx,u,*cx+GOLD*(*cx-*bx))
1270: SHFT(*fb,*fc,fu,(*func)(u))
1271: }
1.162 brouard 1272: } else if ((u-ulim)*(ulim-*cx) >= 0.0) { /* u outside ulim (verifying that ulim is beyond c) */
1.126 brouard 1273: u=ulim;
1274: fu=(*func)(u);
1275: } else {
1276: u=(*cx)+GOLD*(*cx-*bx);
1277: fu=(*func)(u);
1278: }
1279: SHFT(*ax,*bx,*cx,u)
1280: SHFT(*fa,*fb,*fc,fu)
1281: }
1282: }
1283:
1284: /*************** linmin ************************/
1.162 brouard 1285: /* Given an n -dimensional point p[1..n] and an n -dimensional direction xi[1..n] , moves and
1286: resets p to where the function func(p) takes on a minimum along the direction xi from p ,
1287: and replaces xi by the actual vector displacement that p was moved. Also returns as fret
1288: the value of func at the returned location p . This is actually all accomplished by calling the
1289: routines mnbrak and brent .*/
1.126 brouard 1290: int ncom;
1291: double *pcom,*xicom;
1292: double (*nrfunc)(double []);
1293:
1294: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []))
1295: {
1296: double brent(double ax, double bx, double cx,
1297: double (*f)(double), double tol, double *xmin);
1298: double f1dim(double x);
1299: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb,
1300: double *fc, double (*func)(double));
1301: int j;
1302: double xx,xmin,bx,ax;
1303: double fx,fb,fa;
1304:
1305: ncom=n;
1306: pcom=vector(1,n);
1307: xicom=vector(1,n);
1308: nrfunc=func;
1309: for (j=1;j<=n;j++) {
1310: pcom[j]=p[j];
1311: xicom[j]=xi[j];
1312: }
1313: ax=0.0;
1314: xx=1.0;
1.162 brouard 1315: mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim); /* Find a bracket a,x,b in direction n=xi ie xicom */
1316: *fret=brent(ax,xx,bx,f1dim,TOL,&xmin); /* Find a minimum P+lambda n in that direction (lambdamin), with TOL between abscisses */
1.126 brouard 1317: #ifdef DEBUG
1318: printf("retour brent fret=%.12e xmin=%.12e\n",*fret,xmin);
1319: fprintf(ficlog,"retour brent fret=%.12e xmin=%.12e\n",*fret,xmin);
1320: #endif
1321: for (j=1;j<=n;j++) {
1322: xi[j] *= xmin;
1323: p[j] += xi[j];
1324: }
1325: free_vector(xicom,1,n);
1326: free_vector(pcom,1,n);
1327: }
1328:
1329:
1330: /*************** powell ************************/
1.162 brouard 1331: /*
1332: Minimization of a function func of n variables. Input consists of an initial starting point
1333: p[1..n] ; an initial matrix xi[1..n][1..n] , whose columns contain the initial set of di-
1334: rections (usually the n unit vectors); and ftol , the fractional tolerance in the function value
1335: such that failure to decrease by more than this amount on one iteration signals doneness. On
1336: output, p is set to the best point found, xi is the then-current direction set, fret is the returned
1337: function value at p , and iter is the number of iterations taken. The routine linmin is used.
1338: */
1.126 brouard 1339: void powell(double p[], double **xi, int n, double ftol, int *iter, double *fret,
1340: double (*func)(double []))
1341: {
1342: void linmin(double p[], double xi[], int n, double *fret,
1343: double (*func)(double []));
1344: int i,ibig,j;
1345: double del,t,*pt,*ptt,*xit;
1346: double fp,fptt;
1347: double *xits;
1348: int niterf, itmp;
1349:
1350: pt=vector(1,n);
1351: ptt=vector(1,n);
1352: xit=vector(1,n);
1353: xits=vector(1,n);
1354: *fret=(*func)(p);
1355: for (j=1;j<=n;j++) pt[j]=p[j];
1.157 brouard 1356: rcurr_time = time(NULL);
1.126 brouard 1357: for (*iter=1;;++(*iter)) {
1358: fp=(*fret);
1359: ibig=0;
1360: del=0.0;
1.157 brouard 1361: rlast_time=rcurr_time;
1362: /* (void) gettimeofday(&curr_time,&tzp); */
1363: rcurr_time = time(NULL);
1364: curr_time = *localtime(&rcurr_time);
1365: printf("\nPowell iter=%d -2*LL=%.12f %ld sec. %ld sec.",*iter,*fret, rcurr_time-rlast_time, rcurr_time-rstart_time);fflush(stdout);
1366: fprintf(ficlog,"\nPowell iter=%d -2*LL=%.12f %ld sec. %ld sec.",*iter,*fret,rcurr_time-rlast_time, rcurr_time-rstart_time); fflush(ficlog);
1367: /* fprintf(ficrespow,"%d %.12f %ld",*iter,*fret,curr_time.tm_sec-start_time.tm_sec); */
1.126 brouard 1368: for (i=1;i<=n;i++) {
1369: printf(" %d %.12f",i, p[i]);
1370: fprintf(ficlog," %d %.12lf",i, p[i]);
1371: fprintf(ficrespow," %.12lf", p[i]);
1372: }
1373: printf("\n");
1374: fprintf(ficlog,"\n");
1375: fprintf(ficrespow,"\n");fflush(ficrespow);
1376: if(*iter <=3){
1.157 brouard 1377: tml = *localtime(&rcurr_time);
1378: strcpy(strcurr,asctime(&tml));
1379: rforecast_time=rcurr_time;
1.126 brouard 1380: itmp = strlen(strcurr);
1381: if(strcurr[itmp-1]=='\n') /* Windows outputs with a new line */
1382: strcurr[itmp-1]='\0';
1.162 brouard 1383: printf("\nConsidering the time needed for the last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
1.157 brouard 1384: fprintf(ficlog,"\nConsidering the time needed for this last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
1.126 brouard 1385: for(niterf=10;niterf<=30;niterf+=10){
1.157 brouard 1386: rforecast_time=rcurr_time+(niterf-*iter)*(rcurr_time-rlast_time);
1387: forecast_time = *localtime(&rforecast_time);
1388: strcpy(strfor,asctime(&forecast_time));
1.126 brouard 1389: itmp = strlen(strfor);
1390: if(strfor[itmp-1]=='\n')
1391: strfor[itmp-1]='\0';
1.157 brouard 1392: 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);
1393: 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);
1.126 brouard 1394: }
1395: }
1396: for (i=1;i<=n;i++) {
1397: for (j=1;j<=n;j++) xit[j]=xi[j][i];
1398: fptt=(*fret);
1399: #ifdef DEBUG
1.164 brouard 1400: printf("fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
1401: fprintf(ficlog, "fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
1.126 brouard 1402: #endif
1403: printf("%d",i);fflush(stdout);
1404: fprintf(ficlog,"%d",i);fflush(ficlog);
1405: linmin(p,xit,n,fret,func);
1406: if (fabs(fptt-(*fret)) > del) {
1407: del=fabs(fptt-(*fret));
1408: ibig=i;
1409: }
1410: #ifdef DEBUG
1411: printf("%d %.12e",i,(*fret));
1412: fprintf(ficlog,"%d %.12e",i,(*fret));
1413: for (j=1;j<=n;j++) {
1414: xits[j]=FMAX(fabs(p[j]-pt[j]),1.e-5);
1415: printf(" x(%d)=%.12e",j,xit[j]);
1416: fprintf(ficlog," x(%d)=%.12e",j,xit[j]);
1417: }
1418: for(j=1;j<=n;j++) {
1.162 brouard 1419: printf(" p(%d)=%.12e",j,p[j]);
1420: fprintf(ficlog," p(%d)=%.12e",j,p[j]);
1.126 brouard 1421: }
1422: printf("\n");
1423: fprintf(ficlog,"\n");
1424: #endif
1.162 brouard 1425: } /* end i */
1.126 brouard 1426: if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret))) {
1427: #ifdef DEBUG
1428: int k[2],l;
1429: k[0]=1;
1430: k[1]=-1;
1431: printf("Max: %.12e",(*func)(p));
1432: fprintf(ficlog,"Max: %.12e",(*func)(p));
1433: for (j=1;j<=n;j++) {
1434: printf(" %.12e",p[j]);
1435: fprintf(ficlog," %.12e",p[j]);
1436: }
1437: printf("\n");
1438: fprintf(ficlog,"\n");
1439: for(l=0;l<=1;l++) {
1440: for (j=1;j<=n;j++) {
1441: ptt[j]=p[j]+(p[j]-pt[j])*k[l];
1442: printf("l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
1443: fprintf(ficlog,"l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
1444: }
1445: printf("func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
1446: fprintf(ficlog,"func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
1447: }
1448: #endif
1449:
1450:
1451: free_vector(xit,1,n);
1452: free_vector(xits,1,n);
1453: free_vector(ptt,1,n);
1454: free_vector(pt,1,n);
1455: return;
1456: }
1457: if (*iter == ITMAX) nrerror("powell exceeding maximum iterations.");
1.161 brouard 1458: for (j=1;j<=n;j++) { /* Computes an extrapolated point */
1.126 brouard 1459: ptt[j]=2.0*p[j]-pt[j];
1460: xit[j]=p[j]-pt[j];
1461: pt[j]=p[j];
1462: }
1463: fptt=(*func)(ptt);
1.161 brouard 1464: if (fptt < fp) { /* If extrapolated point is better, decide if we keep that new direction or not */
1.162 brouard 1465: /* (x1 f1=fp), (x2 f2=*fret), (x3 f3=fptt), (xm fm) */
1.161 brouard 1466: /* From x1 (P0) distance of x2 is at h and x3 is 2h */
1.162 brouard 1467: /* Let f"(x2) be the 2nd derivative equal everywhere. */
1468: /* Then the parabolic through (x1,f1), (x2,f2) and (x3,f3) */
1469: /* will reach at f3 = fm + h^2/2 f"m ; f" = (f1 -2f2 +f3 ) / h**2 */
1.161 brouard 1470: /* f1-f3 = delta(2h) = 2 h**2 f'' = 2(f1- 2f2 +f3) */
1471: /* Thus we compare delta(2h) with observed f1-f3 */
1.162 brouard 1472: /* or best gain on one ancient line 'del' with total */
1473: /* gain f1-f2 = f1 - f2 - 'del' with del */
1.161 brouard 1474: /* t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)-del*SQR(fp-fptt); */
1.162 brouard 1475:
1.161 brouard 1476: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del);
1477: t= t- del*SQR(fp-fptt);
1478: printf("t1= %.12lf, t2= %.12lf, t=%.12lf\n", 2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del),del*SQR(fp-fptt),t);
1479: 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);
1480: #ifdef DEBUG
1481: printf("t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
1482: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
1483: fprintf(ficlog,"t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
1484: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
1485: 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);
1486: 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);
1487: #endif
1488: if (t < 0.0) { /* Then we use it for last direction */
1489: linmin(p,xit,n,fret,func); /* computes mean on the extrapolated direction.*/
1.126 brouard 1490: for (j=1;j<=n;j++) {
1.161 brouard 1491: xi[j][ibig]=xi[j][n]; /* Replace the direction with biggest decrease by n */
1492: xi[j][n]=xit[j]; /* and nth direction by the extrapolated */
1.126 brouard 1493: }
1.161 brouard 1494: printf("Gaining to use average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
1495: fprintf(ficlog,"Gaining to use average direction of P0 P%d instead of biggest increase direction :\n",n,ibig);
1496:
1.126 brouard 1497: #ifdef DEBUG
1.164 brouard 1498: printf("Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
1499: fprintf(ficlog,"Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
1.126 brouard 1500: for(j=1;j<=n;j++){
1501: printf(" %.12e",xit[j]);
1502: fprintf(ficlog," %.12e",xit[j]);
1503: }
1504: printf("\n");
1505: fprintf(ficlog,"\n");
1506: #endif
1.162 brouard 1507: } /* end of t negative */
1508: } /* end if (fptt < fp) */
1.126 brouard 1509: }
1510: }
1511:
1512: /**** Prevalence limit (stable or period prevalence) ****************/
1513:
1514: double **prevalim(double **prlim, int nlstate, double x[], double age, double **oldm, double **savm, double ftolpl, int ij)
1515: {
1516: /* Computes the prevalence limit in each live state at age x by left multiplying the unit
1517: matrix by transitions matrix until convergence is reached */
1518:
1519: int i, ii,j,k;
1520: double min, max, maxmin, maxmax,sumnew=0.;
1.145 brouard 1521: /* double **matprod2(); */ /* test */
1.131 brouard 1522: double **out, cov[NCOVMAX+1], **pmij();
1.126 brouard 1523: double **newm;
1524: double agefin, delaymax=50 ; /* Max number of years to converge */
1525:
1526: for (ii=1;ii<=nlstate+ndeath;ii++)
1527: for (j=1;j<=nlstate+ndeath;j++){
1528: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
1529: }
1530:
1531: cov[1]=1.;
1532:
1533: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
1534: for(agefin=age-stepm/YEARM; agefin>=age-delaymax; agefin=agefin-stepm/YEARM){
1535: newm=savm;
1536: /* Covariates have to be included here again */
1.138 brouard 1537: cov[2]=agefin;
1538:
1539: for (k=1; k<=cptcovn;k++) {
1540: cov[2+k]=nbcode[Tvar[k]][codtab[ij][Tvar[k]]];
1.145 brouard 1541: /*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]]);*/
1.138 brouard 1542: }
1.145 brouard 1543: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
1544: /* for (k=1; k<=cptcovprod;k++) /\* Useless *\/ */
1545: /* cov[2+Tprod[k]]=nbcode[Tvard[k][1]][codtab[ij][Tvard[k][1]]] * nbcode[Tvard[k][2]][codtab[ij][Tvard[k][2]]]; */
1.138 brouard 1546:
1547: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
1548: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
1549: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
1.145 brouard 1550: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
1551: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
1.142 brouard 1552: out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /* Bug Valgrind */
1.138 brouard 1553:
1.126 brouard 1554: savm=oldm;
1555: oldm=newm;
1556: maxmax=0.;
1557: for(j=1;j<=nlstate;j++){
1558: min=1.;
1559: max=0.;
1560: for(i=1; i<=nlstate; i++) {
1561: sumnew=0;
1562: for(k=1; k<=ndeath; k++) sumnew+=newm[i][nlstate+k];
1563: prlim[i][j]= newm[i][j]/(1-sumnew);
1.145 brouard 1564: /*printf(" prevalim i=%d, j=%d, prmlim[%d][%d]=%f, agefin=%d \n", i, j, i, j, prlim[i][j],(int)agefin);*/
1.126 brouard 1565: max=FMAX(max,prlim[i][j]);
1566: min=FMIN(min,prlim[i][j]);
1567: }
1568: maxmin=max-min;
1569: maxmax=FMAX(maxmax,maxmin);
1570: }
1571: if(maxmax < ftolpl){
1572: return prlim;
1573: }
1574: }
1575: }
1576:
1577: /*************** transition probabilities ***************/
1578:
1579: double **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
1580: {
1.138 brouard 1581: /* According to parameters values stored in x and the covariate's values stored in cov,
1582: computes the probability to be observed in state j being in state i by appying the
1583: model to the ncovmodel covariates (including constant and age).
1584: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
1585: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
1586: ncth covariate in the global vector x is given by the formula:
1587: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
1588: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
1589: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
1590: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
1591: Outputs ps[i][j] the probability to be observed in j being in j according to
1592: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
1593: */
1594: double s1, lnpijopii;
1.126 brouard 1595: /*double t34;*/
1.164 brouard 1596: int i,j, nc, ii, jj;
1.126 brouard 1597:
1598: for(i=1; i<= nlstate; i++){
1599: for(j=1; j<i;j++){
1.138 brouard 1600: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
1601: /*lnpijopii += param[i][j][nc]*cov[nc];*/
1602: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
1603: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
1.126 brouard 1604: }
1.138 brouard 1605: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
1606: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
1.126 brouard 1607: }
1608: for(j=i+1; j<=nlstate+ndeath;j++){
1.138 brouard 1609: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
1610: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
1611: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
1612: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
1.126 brouard 1613: }
1.138 brouard 1614: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
1.126 brouard 1615: }
1616: }
1617:
1618: for(i=1; i<= nlstate; i++){
1619: s1=0;
1.131 brouard 1620: for(j=1; j<i; j++){
1.138 brouard 1621: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
1.131 brouard 1622: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
1623: }
1624: for(j=i+1; j<=nlstate+ndeath; j++){
1.138 brouard 1625: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
1.131 brouard 1626: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
1627: }
1.138 brouard 1628: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
1.126 brouard 1629: ps[i][i]=1./(s1+1.);
1.138 brouard 1630: /* Computing other pijs */
1.126 brouard 1631: for(j=1; j<i; j++)
1632: ps[i][j]= exp(ps[i][j])*ps[i][i];
1633: for(j=i+1; j<=nlstate+ndeath; j++)
1634: ps[i][j]= exp(ps[i][j])*ps[i][i];
1635: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
1636: } /* end i */
1637:
1638: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
1639: for(jj=1; jj<= nlstate+ndeath; jj++){
1640: ps[ii][jj]=0;
1641: ps[ii][ii]=1;
1642: }
1643: }
1644:
1.145 brouard 1645:
1646: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
1647: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
1648: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
1649: /* } */
1650: /* printf("\n "); */
1651: /* } */
1652: /* printf("\n ");printf("%lf ",cov[2]);*/
1653: /*
1.126 brouard 1654: for(i=1; i<= npar; i++) printf("%f ",x[i]);
1655: goto end;*/
1656: return ps;
1657: }
1658:
1659: /**************** Product of 2 matrices ******************/
1660:
1.145 brouard 1661: double **matprod2(double **out, double **in,int nrl, int nrh, int ncl, int nch, int ncolol, int ncoloh, double **b)
1.126 brouard 1662: {
1663: /* Computes the matrix product of in(1,nrh-nrl+1)(1,nch-ncl+1) times
1664: b(1,nch-ncl+1)(1,ncoloh-ncolol+1) into out(...) */
1665: /* in, b, out are matrice of pointers which should have been initialized
1666: before: only the contents of out is modified. The function returns
1667: a pointer to pointers identical to out */
1.145 brouard 1668: int i, j, k;
1.126 brouard 1669: for(i=nrl; i<= nrh; i++)
1.145 brouard 1670: for(k=ncolol; k<=ncoloh; k++){
1671: out[i][k]=0.;
1672: for(j=ncl; j<=nch; j++)
1673: out[i][k] +=in[i][j]*b[j][k];
1674: }
1.126 brouard 1675: return out;
1676: }
1677:
1678:
1679: /************* Higher Matrix Product ***************/
1680:
1681: double ***hpxij(double ***po, int nhstepm, double age, int hstepm, double *x, int nlstate, int stepm, double **oldm, double **savm, int ij )
1682: {
1683: /* Computes the transition matrix starting at age 'age' over
1684: 'nhstepm*hstepm*stepm' months (i.e. until
1685: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
1686: nhstepm*hstepm matrices.
1687: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
1688: (typically every 2 years instead of every month which is too big
1689: for the memory).
1690: Model is determined by parameters x and covariates have to be
1691: included manually here.
1692:
1693: */
1694:
1695: int i, j, d, h, k;
1.131 brouard 1696: double **out, cov[NCOVMAX+1];
1.126 brouard 1697: double **newm;
1698:
1699: /* Hstepm could be zero and should return the unit matrix */
1700: for (i=1;i<=nlstate+ndeath;i++)
1701: for (j=1;j<=nlstate+ndeath;j++){
1702: oldm[i][j]=(i==j ? 1.0 : 0.0);
1703: po[i][j][0]=(i==j ? 1.0 : 0.0);
1704: }
1705: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
1706: for(h=1; h <=nhstepm; h++){
1707: for(d=1; d <=hstepm; d++){
1708: newm=savm;
1709: /* Covariates have to be included here again */
1710: cov[1]=1.;
1711: cov[2]=age+((h-1)*hstepm + (d-1))*stepm/YEARM;
1.131 brouard 1712: for (k=1; k<=cptcovn;k++)
1713: cov[2+k]=nbcode[Tvar[k]][codtab[ij][Tvar[k]]];
1.126 brouard 1714: for (k=1; k<=cptcovage;k++)
1715: cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2];
1.145 brouard 1716: for (k=1; k<=cptcovprod;k++) /* Useless because included in cptcovn */
1.126 brouard 1717: cov[2+Tprod[k]]=nbcode[Tvard[k][1]][codtab[ij][Tvard[k][1]]]*nbcode[Tvard[k][2]][codtab[ij][Tvard[k][2]]];
1718:
1719:
1720: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
1721: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
1722: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
1723: pmij(pmmij,cov,ncovmodel,x,nlstate));
1724: savm=oldm;
1725: oldm=newm;
1726: }
1727: for(i=1; i<=nlstate+ndeath; i++)
1728: for(j=1;j<=nlstate+ndeath;j++) {
1729: po[i][j][h]=newm[i][j];
1.128 brouard 1730: /*if(h==nhstepm) printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]);*/
1.126 brouard 1731: }
1.128 brouard 1732: /*printf("h=%d ",h);*/
1.126 brouard 1733: } /* end h */
1.128 brouard 1734: /* printf("\n H=%d \n",h); */
1.126 brouard 1735: return po;
1736: }
1737:
1.162 brouard 1738: #ifdef NLOPT
1739: double myfunc(unsigned n, const double *p1, double *grad, void *pd){
1740: double fret;
1741: double *xt;
1742: int j;
1743: myfunc_data *d2 = (myfunc_data *) pd;
1744: /* xt = (p1-1); */
1745: xt=vector(1,n);
1746: for (j=1;j<=n;j++) xt[j]=p1[j-1]; /* xt[1]=p1[0] */
1747:
1748: fret=(d2->function)(xt); /* p xt[1]@8 is fine */
1749: /* fret=(*func)(xt); /\* p xt[1]@8 is fine *\/ */
1750: printf("Function = %.12lf ",fret);
1751: for (j=1;j<=n;j++) printf(" %d %.8lf", j, xt[j]);
1752: printf("\n");
1753: free_vector(xt,1,n);
1754: return fret;
1755: }
1756: #endif
1.126 brouard 1757:
1758: /*************** log-likelihood *************/
1759: double func( double *x)
1760: {
1761: int i, ii, j, k, mi, d, kk;
1.131 brouard 1762: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
1.126 brouard 1763: double **out;
1764: double sw; /* Sum of weights */
1765: double lli; /* Individual log likelihood */
1766: int s1, s2;
1767: double bbh, survp;
1768: long ipmx;
1769: /*extern weight */
1770: /* We are differentiating ll according to initial status */
1771: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
1772: /*for(i=1;i<imx;i++)
1773: printf(" %d\n",s[4][i]);
1774: */
1.162 brouard 1775:
1776: ++countcallfunc;
1777:
1.126 brouard 1778: cov[1]=1.;
1779:
1780: for(k=1; k<=nlstate; k++) ll[k]=0.;
1781:
1782: if(mle==1){
1783: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1.138 brouard 1784: /* Computes the values of the ncovmodel covariates of the model
1785: depending if the covariates are fixed or variying (age dependent) and stores them in cov[]
1786: Then computes with function pmij which return a matrix p[i][j] giving the elementary probability
1787: to be observed in j being in i according to the model.
1788: */
1.145 brouard 1789: for (k=1; k<=cptcovn;k++){ /* Simple and product covariates without age* products */
1790: cov[2+k]=covar[Tvar[k]][i];
1791: }
1.137 brouard 1792: /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4]
1.138 brouard 1793: is 6, Tvar[3=age*V3] should not be computed because of age Tvar[4=V3*V2]
1.137 brouard 1794: has been calculated etc */
1.126 brouard 1795: for(mi=1; mi<= wav[i]-1; mi++){
1796: for (ii=1;ii<=nlstate+ndeath;ii++)
1797: for (j=1;j<=nlstate+ndeath;j++){
1798: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
1799: savm[ii][j]=(ii==j ? 1.0 : 0.0);
1800: }
1801: for(d=0; d<dh[mi][i]; d++){
1802: newm=savm;
1803: cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM;
1804: for (kk=1; kk<=cptcovage;kk++) {
1.137 brouard 1805: cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2]; /* Tage[kk] gives the data-covariate associated with age */
1.126 brouard 1806: }
1807: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
1808: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
1809: savm=oldm;
1810: oldm=newm;
1811: } /* end mult */
1812:
1813: /*lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ /* Original formula */
1814: /* But now since version 0.9 we anticipate for bias at large stepm.
1815: * If stepm is larger than one month (smallest stepm) and if the exact delay
1816: * (in months) between two waves is not a multiple of stepm, we rounded to
1817: * the nearest (and in case of equal distance, to the lowest) interval but now
1818: * we keep into memory the bias bh[mi][i] and also the previous matrix product
1819: * (i.e to dh[mi][i]-1) saved in 'savm'. Then we inter(extra)polate the
1820: * probability in order to take into account the bias as a fraction of the way
1821: * from savm to out if bh is negative or even beyond if bh is positive. bh varies
1822: * -stepm/2 to stepm/2 .
1823: * For stepm=1 the results are the same as for previous versions of Imach.
1824: * For stepm > 1 the results are less biased than in previous versions.
1825: */
1826: s1=s[mw[mi][i]][i];
1827: s2=s[mw[mi+1][i]][i];
1828: bbh=(double)bh[mi][i]/(double)stepm;
1829: /* bias bh is positive if real duration
1830: * is higher than the multiple of stepm and negative otherwise.
1831: */
1832: /* lli= (savm[s1][s2]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2]));*/
1833: if( s2 > nlstate){
1834: /* i.e. if s2 is a death state and if the date of death is known
1835: then the contribution to the likelihood is the probability to
1836: die between last step unit time and current step unit time,
1837: which is also equal to probability to die before dh
1838: minus probability to die before dh-stepm .
1839: In version up to 0.92 likelihood was computed
1840: as if date of death was unknown. Death was treated as any other
1841: health state: the date of the interview describes the actual state
1842: and not the date of a change in health state. The former idea was
1843: to consider that at each interview the state was recorded
1844: (healthy, disable or death) and IMaCh was corrected; but when we
1845: introduced the exact date of death then we should have modified
1846: the contribution of an exact death to the likelihood. This new
1847: contribution is smaller and very dependent of the step unit
1848: stepm. It is no more the probability to die between last interview
1849: and month of death but the probability to survive from last
1850: interview up to one month before death multiplied by the
1851: probability to die within a month. Thanks to Chris
1852: Jackson for correcting this bug. Former versions increased
1853: mortality artificially. The bad side is that we add another loop
1854: which slows down the processing. The difference can be up to 10%
1855: lower mortality.
1856: */
1857: lli=log(out[s1][s2] - savm[s1][s2]);
1858:
1859:
1860: } else if (s2==-2) {
1861: for (j=1,survp=0. ; j<=nlstate; j++)
1862: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
1863: /*survp += out[s1][j]; */
1864: lli= log(survp);
1865: }
1866:
1867: else if (s2==-4) {
1868: for (j=3,survp=0. ; j<=nlstate; j++)
1869: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
1870: lli= log(survp);
1871: }
1872:
1873: else if (s2==-5) {
1874: for (j=1,survp=0. ; j<=2; j++)
1875: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
1876: lli= log(survp);
1877: }
1878:
1879: else{
1880: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
1881: /* 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 */
1882: }
1883: /*lli=(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]);*/
1884: /*if(lli ==000.0)*/
1885: /*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); */
1886: ipmx +=1;
1887: sw += weight[i];
1888: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1889: } /* end of wave */
1890: } /* end of individual */
1891: } else if(mle==2){
1892: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1893: for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i];
1894: for(mi=1; mi<= wav[i]-1; mi++){
1895: for (ii=1;ii<=nlstate+ndeath;ii++)
1896: for (j=1;j<=nlstate+ndeath;j++){
1897: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
1898: savm[ii][j]=(ii==j ? 1.0 : 0.0);
1899: }
1900: for(d=0; d<=dh[mi][i]; d++){
1901: newm=savm;
1902: cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM;
1903: for (kk=1; kk<=cptcovage;kk++) {
1904: cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
1905: }
1906: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
1907: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
1908: savm=oldm;
1909: oldm=newm;
1910: } /* end mult */
1911:
1912: s1=s[mw[mi][i]][i];
1913: s2=s[mw[mi+1][i]][i];
1914: bbh=(double)bh[mi][i]/(double)stepm;
1915: 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 */
1916: ipmx +=1;
1917: sw += weight[i];
1918: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1919: } /* end of wave */
1920: } /* end of individual */
1921: } else if(mle==3){ /* exponential inter-extrapolation */
1922: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1923: for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i];
1924: for(mi=1; mi<= wav[i]-1; mi++){
1925: for (ii=1;ii<=nlstate+ndeath;ii++)
1926: for (j=1;j<=nlstate+ndeath;j++){
1927: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
1928: savm[ii][j]=(ii==j ? 1.0 : 0.0);
1929: }
1930: for(d=0; d<dh[mi][i]; d++){
1931: newm=savm;
1932: cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM;
1933: for (kk=1; kk<=cptcovage;kk++) {
1934: cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
1935: }
1936: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
1937: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
1938: savm=oldm;
1939: oldm=newm;
1940: } /* end mult */
1941:
1942: s1=s[mw[mi][i]][i];
1943: s2=s[mw[mi+1][i]][i];
1944: bbh=(double)bh[mi][i]/(double)stepm;
1945: 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 */
1946: ipmx +=1;
1947: sw += weight[i];
1948: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1949: } /* end of wave */
1950: } /* end of individual */
1951: }else if (mle==4){ /* ml=4 no inter-extrapolation */
1952: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1953: for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i];
1954: for(mi=1; mi<= wav[i]-1; mi++){
1955: for (ii=1;ii<=nlstate+ndeath;ii++)
1956: for (j=1;j<=nlstate+ndeath;j++){
1957: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
1958: savm[ii][j]=(ii==j ? 1.0 : 0.0);
1959: }
1960: for(d=0; d<dh[mi][i]; d++){
1961: newm=savm;
1962: cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM;
1963: for (kk=1; kk<=cptcovage;kk++) {
1964: cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
1965: }
1966:
1967: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
1968: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
1969: savm=oldm;
1970: oldm=newm;
1971: } /* end mult */
1972:
1973: s1=s[mw[mi][i]][i];
1974: s2=s[mw[mi+1][i]][i];
1975: if( s2 > nlstate){
1976: lli=log(out[s1][s2] - savm[s1][s2]);
1977: }else{
1978: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
1979: }
1980: ipmx +=1;
1981: sw += weight[i];
1982: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1983: /* 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]); */
1984: } /* end of wave */
1985: } /* end of individual */
1986: }else{ /* ml=5 no inter-extrapolation no jackson =0.8a */
1987: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1988: for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i];
1989: for(mi=1; mi<= wav[i]-1; mi++){
1990: for (ii=1;ii<=nlstate+ndeath;ii++)
1991: for (j=1;j<=nlstate+ndeath;j++){
1992: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
1993: savm[ii][j]=(ii==j ? 1.0 : 0.0);
1994: }
1995: for(d=0; d<dh[mi][i]; d++){
1996: newm=savm;
1997: cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM;
1998: for (kk=1; kk<=cptcovage;kk++) {
1999: cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
2000: }
2001:
2002: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
2003: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
2004: savm=oldm;
2005: oldm=newm;
2006: } /* end mult */
2007:
2008: s1=s[mw[mi][i]][i];
2009: s2=s[mw[mi+1][i]][i];
2010: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
2011: ipmx +=1;
2012: sw += weight[i];
2013: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
2014: /*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]);*/
2015: } /* end of wave */
2016: } /* end of individual */
2017: } /* End of if */
2018: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
2019: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
2020: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
2021: return -l;
2022: }
2023:
2024: /*************** log-likelihood *************/
2025: double funcone( double *x)
2026: {
2027: /* Same as likeli but slower because of a lot of printf and if */
2028: int i, ii, j, k, mi, d, kk;
1.131 brouard 2029: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
1.126 brouard 2030: double **out;
2031: double lli; /* Individual log likelihood */
2032: double llt;
2033: int s1, s2;
2034: double bbh, survp;
2035: /*extern weight */
2036: /* We are differentiating ll according to initial status */
2037: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
2038: /*for(i=1;i<imx;i++)
2039: printf(" %d\n",s[4][i]);
2040: */
2041: cov[1]=1.;
2042:
2043: for(k=1; k<=nlstate; k++) ll[k]=0.;
2044:
2045: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
2046: for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i];
2047: for(mi=1; mi<= wav[i]-1; mi++){
2048: for (ii=1;ii<=nlstate+ndeath;ii++)
2049: for (j=1;j<=nlstate+ndeath;j++){
2050: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
2051: savm[ii][j]=(ii==j ? 1.0 : 0.0);
2052: }
2053: for(d=0; d<dh[mi][i]; d++){
2054: newm=savm;
2055: cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM;
2056: for (kk=1; kk<=cptcovage;kk++) {
2057: cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
2058: }
1.145 brouard 2059: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
1.126 brouard 2060: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
2061: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
1.145 brouard 2062: /* out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath, */
2063: /* 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate)); */
1.126 brouard 2064: savm=oldm;
2065: oldm=newm;
2066: } /* end mult */
2067:
2068: s1=s[mw[mi][i]][i];
2069: s2=s[mw[mi+1][i]][i];
2070: bbh=(double)bh[mi][i]/(double)stepm;
2071: /* bias is positive if real duration
2072: * is higher than the multiple of stepm and negative otherwise.
2073: */
2074: if( s2 > nlstate && (mle <5) ){ /* Jackson */
2075: lli=log(out[s1][s2] - savm[s1][s2]);
2076: } else if (s2==-2) {
2077: for (j=1,survp=0. ; j<=nlstate; j++)
2078: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
2079: lli= log(survp);
2080: }else if (mle==1){
2081: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
2082: } else if(mle==2){
2083: 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 */
2084: } else if(mle==3){ /* exponential inter-extrapolation */
2085: 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 */
2086: } else if (mle==4){ /* mle=4 no inter-extrapolation */
2087: lli=log(out[s1][s2]); /* Original formula */
1.136 brouard 2088: } else{ /* mle=0 back to 1 */
2089: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
2090: /*lli=log(out[s1][s2]); */ /* Original formula */
1.126 brouard 2091: } /* End of if */
2092: ipmx +=1;
2093: sw += weight[i];
2094: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1.132 brouard 2095: /*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]); */
1.126 brouard 2096: if(globpr){
1.141 brouard 2097: fprintf(ficresilk,"%9ld %6d %2d %2d %1d %1d %3d %11.6f %8.4f\
1.126 brouard 2098: %11.6f %11.6f %11.6f ", \
2099: num[i],i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],
2100: 2*weight[i]*lli,out[s1][s2],savm[s1][s2]);
2101: for(k=1,llt=0.,l=0.; k<=nlstate; k++){
2102: llt +=ll[k]*gipmx/gsw;
2103: fprintf(ficresilk," %10.6f",-ll[k]*gipmx/gsw);
2104: }
2105: fprintf(ficresilk," %10.6f\n", -llt);
2106: }
2107: } /* end of wave */
2108: } /* end of individual */
2109: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
2110: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
2111: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
2112: if(globpr==0){ /* First time we count the contributions and weights */
2113: gipmx=ipmx;
2114: gsw=sw;
2115: }
2116: return -l;
2117: }
2118:
2119:
2120: /*************** function likelione ***********/
2121: void likelione(FILE *ficres,double p[], int npar, int nlstate, int *globpri, long *ipmx, double *sw, double *fretone, double (*funcone)(double []))
2122: {
2123: /* This routine should help understanding what is done with
2124: the selection of individuals/waves and
2125: to check the exact contribution to the likelihood.
2126: Plotting could be done.
2127: */
2128: int k;
2129:
2130: if(*globpri !=0){ /* Just counts and sums, no printings */
2131: strcpy(fileresilk,"ilk");
2132: strcat(fileresilk,fileres);
2133: if((ficresilk=fopen(fileresilk,"w"))==NULL) {
2134: printf("Problem with resultfile: %s\n", fileresilk);
2135: fprintf(ficlog,"Problem with resultfile: %s\n", fileresilk);
2136: }
2137: 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");
2138: fprintf(ficresilk, "#num_i i s1 s2 mi mw dh likeli weight 2wlli out sav ");
2139: /* i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],2*weight[i]*lli,out[s1][s2],savm[s1][s2]); */
2140: for(k=1; k<=nlstate; k++)
2141: fprintf(ficresilk," -2*gipw/gsw*weight*ll[%d]++",k);
2142: fprintf(ficresilk," -2*gipw/gsw*weight*ll(total)\n");
2143: }
2144:
2145: *fretone=(*funcone)(p);
2146: if(*globpri !=0){
2147: fclose(ficresilk);
2148: fprintf(fichtm,"\n<br>File of contributions to the likelihood: <a href=\"%s\">%s</a><br>\n",subdirf(fileresilk),subdirf(fileresilk));
2149: fflush(fichtm);
2150: }
2151: return;
2152: }
2153:
2154:
2155: /*********** Maximum Likelihood Estimation ***************/
2156:
2157: void mlikeli(FILE *ficres,double p[], int npar, int ncovmodel, int nlstate, double ftol, double (*func)(double []))
2158: {
1.165 brouard 2159: int i,j, iter=0;
1.126 brouard 2160: double **xi;
2161: double fret;
2162: double fretone; /* Only one call to likelihood */
2163: /* char filerespow[FILENAMELENGTH];*/
1.162 brouard 2164:
2165: #ifdef NLOPT
2166: int creturn;
2167: nlopt_opt opt;
2168: /* double lb[9] = { -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL }; /\* lower bounds *\/ */
2169: double *lb;
2170: double minf; /* the minimum objective value, upon return */
2171: double * p1; /* Shifted parameters from 0 instead of 1 */
2172: myfunc_data dinst, *d = &dinst;
2173: #endif
2174:
2175:
1.126 brouard 2176: xi=matrix(1,npar,1,npar);
2177: for (i=1;i<=npar;i++)
2178: for (j=1;j<=npar;j++)
2179: xi[i][j]=(i==j ? 1.0 : 0.0);
2180: printf("Powell\n"); fprintf(ficlog,"Powell\n");
2181: strcpy(filerespow,"pow");
2182: strcat(filerespow,fileres);
2183: if((ficrespow=fopen(filerespow,"w"))==NULL) {
2184: printf("Problem with resultfile: %s\n", filerespow);
2185: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
2186: }
2187: fprintf(ficrespow,"# Powell\n# iter -2*LL");
2188: for (i=1;i<=nlstate;i++)
2189: for(j=1;j<=nlstate+ndeath;j++)
2190: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
2191: fprintf(ficrespow,"\n");
1.162 brouard 2192: #ifdef POWELL
1.126 brouard 2193: powell(p,xi,npar,ftol,&iter,&fret,func);
1.162 brouard 2194: #endif
1.126 brouard 2195:
1.162 brouard 2196: #ifdef NLOPT
2197: #ifdef NEWUOA
2198: opt = nlopt_create(NLOPT_LN_NEWUOA,npar);
2199: #else
2200: opt = nlopt_create(NLOPT_LN_BOBYQA,npar);
2201: #endif
2202: lb=vector(0,npar-1);
2203: for (i=0;i<npar;i++) lb[i]= -HUGE_VAL;
2204: nlopt_set_lower_bounds(opt, lb);
2205: nlopt_set_initial_step1(opt, 0.1);
2206:
2207: p1= (p+1); /* p *(p+1)@8 and p *(p1)@8 are equal p1[0]=p[1] */
2208: d->function = func;
2209: printf(" Func %.12lf \n",myfunc(npar,p1,NULL,d));
2210: nlopt_set_min_objective(opt, myfunc, d);
2211: nlopt_set_xtol_rel(opt, ftol);
2212: if ((creturn=nlopt_optimize(opt, p1, &minf)) < 0) {
2213: printf("nlopt failed! %d\n",creturn);
2214: }
2215: else {
2216: printf("found minimum after %d evaluations (NLOPT=%d)\n", countcallfunc ,NLOPT);
2217: printf("found minimum at f(%g,%g) = %0.10g\n", p[0], p[1], minf);
2218: iter=1; /* not equal */
2219: }
2220: nlopt_destroy(opt);
2221: #endif
1.126 brouard 2222: free_matrix(xi,1,npar,1,npar);
2223: fclose(ficrespow);
1.162 brouard 2224: printf("\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
2225: fprintf(ficlog,"\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
2226: fprintf(ficres,"\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
1.126 brouard 2227:
2228: }
2229:
2230: /**** Computes Hessian and covariance matrix ***/
2231: void hesscov(double **matcov, double p[], int npar, double delti[], double ftolhess, double (*func)(double []))
2232: {
2233: double **a,**y,*x,pd;
2234: double **hess;
1.164 brouard 2235: int i, j;
1.126 brouard 2236: int *indx;
2237:
2238: double hessii(double p[], double delta, int theta, double delti[],double (*func)(double []),int npar);
2239: double hessij(double p[], double delti[], int i, int j,double (*func)(double []),int npar);
2240: void lubksb(double **a, int npar, int *indx, double b[]) ;
2241: void ludcmp(double **a, int npar, int *indx, double *d) ;
2242: double gompertz(double p[]);
2243: hess=matrix(1,npar,1,npar);
2244:
2245: printf("\nCalculation of the hessian matrix. Wait...\n");
2246: fprintf(ficlog,"\nCalculation of the hessian matrix. Wait...\n");
2247: for (i=1;i<=npar;i++){
2248: printf("%d",i);fflush(stdout);
2249: fprintf(ficlog,"%d",i);fflush(ficlog);
2250:
2251: hess[i][i]=hessii(p,ftolhess,i,delti,func,npar);
2252:
2253: /* printf(" %f ",p[i]);
2254: printf(" %lf %lf %lf",hess[i][i],ftolhess,delti[i]);*/
2255: }
2256:
2257: for (i=1;i<=npar;i++) {
2258: for (j=1;j<=npar;j++) {
2259: if (j>i) {
2260: printf(".%d%d",i,j);fflush(stdout);
2261: fprintf(ficlog,".%d%d",i,j);fflush(ficlog);
2262: hess[i][j]=hessij(p,delti,i,j,func,npar);
2263:
2264: hess[j][i]=hess[i][j];
2265: /*printf(" %lf ",hess[i][j]);*/
2266: }
2267: }
2268: }
2269: printf("\n");
2270: fprintf(ficlog,"\n");
2271:
2272: printf("\nInverting the hessian to get the covariance matrix. Wait...\n");
2273: fprintf(ficlog,"\nInverting the hessian to get the covariance matrix. Wait...\n");
2274:
2275: a=matrix(1,npar,1,npar);
2276: y=matrix(1,npar,1,npar);
2277: x=vector(1,npar);
2278: indx=ivector(1,npar);
2279: for (i=1;i<=npar;i++)
2280: for (j=1;j<=npar;j++) a[i][j]=hess[i][j];
2281: ludcmp(a,npar,indx,&pd);
2282:
2283: for (j=1;j<=npar;j++) {
2284: for (i=1;i<=npar;i++) x[i]=0;
2285: x[j]=1;
2286: lubksb(a,npar,indx,x);
2287: for (i=1;i<=npar;i++){
2288: matcov[i][j]=x[i];
2289: }
2290: }
2291:
2292: printf("\n#Hessian matrix#\n");
2293: fprintf(ficlog,"\n#Hessian matrix#\n");
2294: for (i=1;i<=npar;i++) {
2295: for (j=1;j<=npar;j++) {
2296: printf("%.3e ",hess[i][j]);
2297: fprintf(ficlog,"%.3e ",hess[i][j]);
2298: }
2299: printf("\n");
2300: fprintf(ficlog,"\n");
2301: }
2302:
2303: /* Recompute Inverse */
2304: for (i=1;i<=npar;i++)
2305: for (j=1;j<=npar;j++) a[i][j]=matcov[i][j];
2306: ludcmp(a,npar,indx,&pd);
2307:
2308: /* printf("\n#Hessian matrix recomputed#\n");
2309:
2310: for (j=1;j<=npar;j++) {
2311: for (i=1;i<=npar;i++) x[i]=0;
2312: x[j]=1;
2313: lubksb(a,npar,indx,x);
2314: for (i=1;i<=npar;i++){
2315: y[i][j]=x[i];
2316: printf("%.3e ",y[i][j]);
2317: fprintf(ficlog,"%.3e ",y[i][j]);
2318: }
2319: printf("\n");
2320: fprintf(ficlog,"\n");
2321: }
2322: */
2323:
2324: free_matrix(a,1,npar,1,npar);
2325: free_matrix(y,1,npar,1,npar);
2326: free_vector(x,1,npar);
2327: free_ivector(indx,1,npar);
2328: free_matrix(hess,1,npar,1,npar);
2329:
2330:
2331: }
2332:
2333: /*************** hessian matrix ****************/
2334: double hessii(double x[], double delta, int theta, double delti[], double (*func)(double []), int npar)
2335: {
2336: int i;
2337: int l=1, lmax=20;
2338: double k1,k2;
1.132 brouard 2339: double p2[MAXPARM+1]; /* identical to x */
1.126 brouard 2340: double res;
2341: double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4;
2342: double fx;
2343: int k=0,kmax=10;
2344: double l1;
2345:
2346: fx=func(x);
2347: for (i=1;i<=npar;i++) p2[i]=x[i];
1.145 brouard 2348: for(l=0 ; l <=lmax; l++){ /* Enlarging the zone around the Maximum */
1.126 brouard 2349: l1=pow(10,l);
2350: delts=delt;
2351: for(k=1 ; k <kmax; k=k+1){
2352: delt = delta*(l1*k);
2353: p2[theta]=x[theta] +delt;
1.145 brouard 2354: k1=func(p2)-fx; /* Might be negative if too close to the theoretical maximum */
1.126 brouard 2355: p2[theta]=x[theta]-delt;
2356: k2=func(p2)-fx;
2357: /*res= (k1-2.0*fx+k2)/delt/delt; */
2358: res= (k1+k2)/delt/delt/2.; /* Divided by because L and not 2*L */
2359:
1.132 brouard 2360: #ifdef DEBUGHESS
1.126 brouard 2361: 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);
2362: 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);
2363: #endif
2364: /*if(fabs(k1-2.0*fx+k2) <1.e-13){ */
2365: if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)){
2366: k=kmax;
2367: }
2368: else if((k1 >khi/nkhif) || (k2 >khi/nkhif)){ /* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. */
1.164 brouard 2369: k=kmax; l=lmax*10;
1.126 brouard 2370: }
2371: else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){
2372: delts=delt;
2373: }
2374: }
2375: }
2376: delti[theta]=delts;
2377: return res;
2378:
2379: }
2380:
2381: double hessij( double x[], double delti[], int thetai,int thetaj,double (*func)(double []),int npar)
2382: {
2383: int i;
1.164 brouard 2384: int l=1, lmax=20;
1.126 brouard 2385: double k1,k2,k3,k4,res,fx;
1.132 brouard 2386: double p2[MAXPARM+1];
1.126 brouard 2387: int k;
2388:
2389: fx=func(x);
2390: for (k=1; k<=2; k++) {
2391: for (i=1;i<=npar;i++) p2[i]=x[i];
2392: p2[thetai]=x[thetai]+delti[thetai]/k;
2393: p2[thetaj]=x[thetaj]+delti[thetaj]/k;
2394: k1=func(p2)-fx;
2395:
2396: p2[thetai]=x[thetai]+delti[thetai]/k;
2397: p2[thetaj]=x[thetaj]-delti[thetaj]/k;
2398: k2=func(p2)-fx;
2399:
2400: p2[thetai]=x[thetai]-delti[thetai]/k;
2401: p2[thetaj]=x[thetaj]+delti[thetaj]/k;
2402: k3=func(p2)-fx;
2403:
2404: p2[thetai]=x[thetai]-delti[thetai]/k;
2405: p2[thetaj]=x[thetaj]-delti[thetaj]/k;
2406: k4=func(p2)-fx;
2407: res=(k1-k2-k3+k4)/4.0/delti[thetai]*k/delti[thetaj]*k/2.; /* Because of L not 2*L */
2408: #ifdef DEBUG
2409: 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);
2410: 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);
2411: #endif
2412: }
2413: return res;
2414: }
2415:
2416: /************** Inverse of matrix **************/
2417: void ludcmp(double **a, int n, int *indx, double *d)
2418: {
2419: int i,imax,j,k;
2420: double big,dum,sum,temp;
2421: double *vv;
2422:
2423: vv=vector(1,n);
2424: *d=1.0;
2425: for (i=1;i<=n;i++) {
2426: big=0.0;
2427: for (j=1;j<=n;j++)
2428: if ((temp=fabs(a[i][j])) > big) big=temp;
2429: if (big == 0.0) nrerror("Singular matrix in routine ludcmp");
2430: vv[i]=1.0/big;
2431: }
2432: for (j=1;j<=n;j++) {
2433: for (i=1;i<j;i++) {
2434: sum=a[i][j];
2435: for (k=1;k<i;k++) sum -= a[i][k]*a[k][j];
2436: a[i][j]=sum;
2437: }
2438: big=0.0;
2439: for (i=j;i<=n;i++) {
2440: sum=a[i][j];
2441: for (k=1;k<j;k++)
2442: sum -= a[i][k]*a[k][j];
2443: a[i][j]=sum;
2444: if ( (dum=vv[i]*fabs(sum)) >= big) {
2445: big=dum;
2446: imax=i;
2447: }
2448: }
2449: if (j != imax) {
2450: for (k=1;k<=n;k++) {
2451: dum=a[imax][k];
2452: a[imax][k]=a[j][k];
2453: a[j][k]=dum;
2454: }
2455: *d = -(*d);
2456: vv[imax]=vv[j];
2457: }
2458: indx[j]=imax;
2459: if (a[j][j] == 0.0) a[j][j]=TINY;
2460: if (j != n) {
2461: dum=1.0/(a[j][j]);
2462: for (i=j+1;i<=n;i++) a[i][j] *= dum;
2463: }
2464: }
2465: free_vector(vv,1,n); /* Doesn't work */
2466: ;
2467: }
2468:
2469: void lubksb(double **a, int n, int *indx, double b[])
2470: {
2471: int i,ii=0,ip,j;
2472: double sum;
2473:
2474: for (i=1;i<=n;i++) {
2475: ip=indx[i];
2476: sum=b[ip];
2477: b[ip]=b[i];
2478: if (ii)
2479: for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j];
2480: else if (sum) ii=i;
2481: b[i]=sum;
2482: }
2483: for (i=n;i>=1;i--) {
2484: sum=b[i];
2485: for (j=i+1;j<=n;j++) sum -= a[i][j]*b[j];
2486: b[i]=sum/a[i][i];
2487: }
2488: }
2489:
2490: void pstamp(FILE *fichier)
2491: {
2492: fprintf(fichier,"# %s.%s\n#%s\n#%s\n# %s", optionfilefiname,optionfilext,version,fullversion,strstart);
2493: }
2494:
2495: /************ Frequencies ********************/
2496: 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[])
2497: { /* Some frequencies */
2498:
1.164 brouard 2499: int i, m, jk, j1, bool, z1,j;
1.126 brouard 2500: int first;
2501: double ***freq; /* Frequencies */
2502: double *pp, **prop;
2503: double pos,posprop, k2, dateintsum=0,k2cpt=0;
2504: char fileresp[FILENAMELENGTH];
2505:
2506: pp=vector(1,nlstate);
2507: prop=matrix(1,nlstate,iagemin,iagemax+3);
2508: strcpy(fileresp,"p");
2509: strcat(fileresp,fileres);
2510: if((ficresp=fopen(fileresp,"w"))==NULL) {
2511: printf("Problem with prevalence resultfile: %s\n", fileresp);
2512: fprintf(ficlog,"Problem with prevalence resultfile: %s\n", fileresp);
2513: exit(0);
2514: }
2515: freq= ma3x(-5,nlstate+ndeath,-5,nlstate+ndeath,iagemin,iagemax+3);
2516: j1=0;
2517:
2518: j=cptcoveff;
2519: if (cptcovn<1) {j=1;ncodemax[1]=1;}
2520:
2521: first=1;
2522:
1.145 brouard 2523: /* for(k1=1; k1<=j ; k1++){ /* Loop on covariates */
2524: /* for(i1=1; i1<=ncodemax[k1];i1++){ /* Now it is 2 */
2525: /* j1++;
2526: */
2527: for (j1 = 1; j1 <= (int) pow(2,cptcoveff); j1++){
1.126 brouard 2528: /*printf("cptcoveff=%d Tvaraff=%d", cptcoveff,Tvaraff[1]);
2529: scanf("%d", i);*/
2530: for (i=-5; i<=nlstate+ndeath; i++)
2531: for (jk=-5; jk<=nlstate+ndeath; jk++)
2532: for(m=iagemin; m <= iagemax+3; m++)
2533: freq[i][jk][m]=0;
1.143 brouard 2534:
2535: for (i=1; i<=nlstate; i++)
2536: for(m=iagemin; m <= iagemax+3; m++)
2537: prop[i][m]=0;
1.126 brouard 2538:
2539: dateintsum=0;
2540: k2cpt=0;
2541: for (i=1; i<=imx; i++) {
2542: bool=1;
1.144 brouard 2543: if (cptcovn>0) { /* Filter is here: Must be looked at for model=V1+V2+V3+V4 */
2544: for (z1=1; z1<=cptcoveff; z1++)
2545: if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtab[j1][z1]]){
1.145 brouard 2546: /* Tests if the value of each of the covariates of i is equal to filter j1 */
1.144 brouard 2547: bool=0;
1.145 brouard 2548: /* 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",
2549: bool,i,z1, z1, Tvaraff[z1],i,covar[Tvaraff[z1]][i],j1,z1,codtab[j1][z1],
2550: j1,z1,nbcode[Tvaraff[z1]][codtab[j1][z1]],j1);*/
1.144 brouard 2551: /* For j1=7 in V1+V2+V3+V4 = 0 1 1 0 and codtab[7][3]=1 and nbcde[3][?]=1*/
2552: }
1.126 brouard 2553: }
1.144 brouard 2554:
1.126 brouard 2555: if (bool==1){
2556: for(m=firstpass; m<=lastpass; m++){
2557: k2=anint[m][i]+(mint[m][i]/12.);
2558: /*if ((k2>=dateprev1) && (k2<=dateprev2)) {*/
2559: if(agev[m][i]==0) agev[m][i]=iagemax+1;
2560: if(agev[m][i]==1) agev[m][i]=iagemax+2;
2561: if (s[m][i]>0 && s[m][i]<=nlstate) prop[s[m][i]][(int)agev[m][i]] += weight[i];
2562: if (m<lastpass) {
2563: freq[s[m][i]][s[m+1][i]][(int)agev[m][i]] += weight[i];
2564: freq[s[m][i]][s[m+1][i]][iagemax+3] += weight[i];
2565: }
2566:
2567: if ((agev[m][i]>1) && (agev[m][i]< (iagemax+3))) {
2568: dateintsum=dateintsum+k2;
2569: k2cpt++;
2570: }
2571: /*}*/
2572: }
2573: }
1.145 brouard 2574: } /* end i */
1.126 brouard 2575:
2576: /* fprintf(ficresp, "#Count between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);*/
2577: pstamp(ficresp);
2578: if (cptcovn>0) {
2579: fprintf(ficresp, "\n#********** Variable ");
2580: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresp, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
2581: fprintf(ficresp, "**********\n#");
1.143 brouard 2582: fprintf(ficlog, "\n#********** Variable ");
2583: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficlog, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
2584: fprintf(ficlog, "**********\n#");
1.126 brouard 2585: }
2586: for(i=1; i<=nlstate;i++)
2587: fprintf(ficresp, " Age Prev(%d) N(%d) N",i,i);
2588: fprintf(ficresp, "\n");
2589:
2590: for(i=iagemin; i <= iagemax+3; i++){
2591: if(i==iagemax+3){
2592: fprintf(ficlog,"Total");
2593: }else{
2594: if(first==1){
2595: first=0;
2596: printf("See log file for details...\n");
2597: }
2598: fprintf(ficlog,"Age %d", i);
2599: }
2600: for(jk=1; jk <=nlstate ; jk++){
2601: for(m=-1, pp[jk]=0; m <=nlstate+ndeath ; m++)
2602: pp[jk] += freq[jk][m][i];
2603: }
2604: for(jk=1; jk <=nlstate ; jk++){
2605: for(m=-1, pos=0; m <=0 ; m++)
2606: pos += freq[jk][m][i];
2607: if(pp[jk]>=1.e-10){
2608: if(first==1){
1.132 brouard 2609: printf(" %d.=%.0f loss[%d]=%.1f%%",jk,pp[jk],jk,100*pos/pp[jk]);
1.126 brouard 2610: }
2611: fprintf(ficlog," %d.=%.0f loss[%d]=%.1f%%",jk,pp[jk],jk,100*pos/pp[jk]);
2612: }else{
2613: if(first==1)
2614: printf(" %d.=%.0f loss[%d]=NaNQ%%",jk,pp[jk],jk);
2615: fprintf(ficlog," %d.=%.0f loss[%d]=NaNQ%%",jk,pp[jk],jk);
2616: }
2617: }
2618:
2619: for(jk=1; jk <=nlstate ; jk++){
2620: for(m=0, pp[jk]=0; m <=nlstate+ndeath; m++)
2621: pp[jk] += freq[jk][m][i];
2622: }
2623: for(jk=1,pos=0,posprop=0; jk <=nlstate ; jk++){
2624: pos += pp[jk];
2625: posprop += prop[jk][i];
2626: }
2627: for(jk=1; jk <=nlstate ; jk++){
2628: if(pos>=1.e-5){
2629: if(first==1)
2630: printf(" %d.=%.0f prev[%d]=%.1f%%",jk,pp[jk],jk,100*pp[jk]/pos);
2631: fprintf(ficlog," %d.=%.0f prev[%d]=%.1f%%",jk,pp[jk],jk,100*pp[jk]/pos);
2632: }else{
2633: if(first==1)
2634: printf(" %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk);
2635: fprintf(ficlog," %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk);
2636: }
2637: if( i <= iagemax){
2638: if(pos>=1.e-5){
2639: fprintf(ficresp," %d %.5f %.0f %.0f",i,prop[jk][i]/posprop, prop[jk][i],posprop);
2640: /*probs[i][jk][j1]= pp[jk]/pos;*/
2641: /*printf("\ni=%d jk=%d j1=%d %.5f %.0f %.0f %f",i,jk,j1,pp[jk]/pos, pp[jk],pos,probs[i][jk][j1]);*/
2642: }
2643: else
2644: fprintf(ficresp," %d NaNq %.0f %.0f",i,prop[jk][i],posprop);
2645: }
2646: }
2647:
2648: for(jk=-1; jk <=nlstate+ndeath; jk++)
2649: for(m=-1; m <=nlstate+ndeath; m++)
2650: if(freq[jk][m][i] !=0 ) {
2651: if(first==1)
2652: printf(" %d%d=%.0f",jk,m,freq[jk][m][i]);
2653: fprintf(ficlog," %d%d=%.0f",jk,m,freq[jk][m][i]);
2654: }
2655: if(i <= iagemax)
2656: fprintf(ficresp,"\n");
2657: if(first==1)
2658: printf("Others in log...\n");
2659: fprintf(ficlog,"\n");
2660: }
1.145 brouard 2661: /*}*/
1.126 brouard 2662: }
2663: dateintmean=dateintsum/k2cpt;
2664:
2665: fclose(ficresp);
2666: free_ma3x(freq,-5,nlstate+ndeath,-5,nlstate+ndeath, iagemin, iagemax+3);
2667: free_vector(pp,1,nlstate);
2668: free_matrix(prop,1,nlstate,iagemin, iagemax+3);
2669: /* End of Freq */
2670: }
2671:
2672: /************ Prevalence ********************/
2673: 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)
2674: {
2675: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
2676: in each health status at the date of interview (if between dateprev1 and dateprev2).
2677: We still use firstpass and lastpass as another selection.
2678: */
2679:
1.164 brouard 2680: int i, m, jk, j1, bool, z1,j;
2681:
2682: double **prop;
2683: double posprop;
1.126 brouard 2684: double y2; /* in fractional years */
2685: int iagemin, iagemax;
1.145 brouard 2686: int first; /** to stop verbosity which is redirected to log file */
1.126 brouard 2687:
2688: iagemin= (int) agemin;
2689: iagemax= (int) agemax;
2690: /*pp=vector(1,nlstate);*/
2691: prop=matrix(1,nlstate,iagemin,iagemax+3);
2692: /* freq=ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,iagemin,iagemax+3);*/
2693: j1=0;
2694:
1.145 brouard 2695: /*j=cptcoveff;*/
1.126 brouard 2696: if (cptcovn<1) {j=1;ncodemax[1]=1;}
2697:
1.145 brouard 2698: first=1;
2699: for(j1=1; j1<= (int) pow(2,cptcoveff);j1++){
2700: /*for(i1=1; i1<=ncodemax[k1];i1++){
2701: j1++;*/
1.126 brouard 2702:
2703: for (i=1; i<=nlstate; i++)
2704: for(m=iagemin; m <= iagemax+3; m++)
2705: prop[i][m]=0.0;
2706:
2707: for (i=1; i<=imx; i++) { /* Each individual */
2708: bool=1;
2709: if (cptcovn>0) {
2710: for (z1=1; z1<=cptcoveff; z1++)
2711: if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtab[j1][z1]])
2712: bool=0;
2713: }
2714: if (bool==1) {
2715: for(m=firstpass; m<=lastpass; m++){/* Other selection (we can limit to certain interviews*/
2716: y2=anint[m][i]+(mint[m][i]/12.); /* Fractional date in year */
2717: if ((y2>=dateprev1) && (y2<=dateprev2)) { /* Here is the main selection (fractional years) */
2718: if(agev[m][i]==0) agev[m][i]=iagemax+1;
2719: if(agev[m][i]==1) agev[m][i]=iagemax+2;
2720: 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);
2721: if (s[m][i]>0 && s[m][i]<=nlstate) {
2722: /*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]]);*/
2723: prop[s[m][i]][(int)agev[m][i]] += weight[i];
2724: prop[s[m][i]][iagemax+3] += weight[i];
2725: }
2726: }
2727: } /* end selection of waves */
2728: }
2729: }
2730: for(i=iagemin; i <= iagemax+3; i++){
2731: for(jk=1,posprop=0; jk <=nlstate ; jk++) {
2732: posprop += prop[jk][i];
2733: }
1.145 brouard 2734:
1.126 brouard 2735: for(jk=1; jk <=nlstate ; jk++){
2736: if( i <= iagemax){
2737: if(posprop>=1.e-5){
2738: probs[i][jk][j1]= prop[jk][i]/posprop;
1.145 brouard 2739: } else{
2740: if(first==1){
2741: first=0;
2742: 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]);
2743: }
2744: }
1.126 brouard 2745: }
2746: }/* end jk */
2747: }/* end i */
1.145 brouard 2748: /*} *//* end i1 */
2749: } /* end j1 */
1.126 brouard 2750:
2751: /* free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath, iagemin, iagemax+3);*/
2752: /*free_vector(pp,1,nlstate);*/
2753: free_matrix(prop,1,nlstate, iagemin,iagemax+3);
2754: } /* End of prevalence */
2755:
2756: /************* Waves Concatenation ***************/
2757:
2758: 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)
2759: {
2760: /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
2761: Death is a valid wave (if date is known).
2762: mw[mi][i] is the mi (mi=1 to wav[i]) effective wave of individual i
2763: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
2764: and mw[mi+1][i]. dh depends on stepm.
2765: */
2766:
2767: int i, mi, m;
2768: /* int j, k=0,jk, ju, jl,jmin=1e+5, jmax=-1;
2769: double sum=0., jmean=0.;*/
2770: int first;
2771: int j, k=0,jk, ju, jl;
2772: double sum=0.;
2773: first=0;
1.164 brouard 2774: jmin=100000;
1.126 brouard 2775: jmax=-1;
2776: jmean=0.;
2777: for(i=1; i<=imx; i++){
2778: mi=0;
2779: m=firstpass;
2780: while(s[m][i] <= nlstate){
2781: if(s[m][i]>=1 || s[m][i]==-2 || s[m][i]==-4 || s[m][i]==-5)
2782: mw[++mi][i]=m;
2783: if(m >=lastpass)
2784: break;
2785: else
2786: m++;
2787: }/* end while */
2788: if (s[m][i] > nlstate){
2789: mi++; /* Death is another wave */
2790: /* if(mi==0) never been interviewed correctly before death */
2791: /* Only death is a correct wave */
2792: mw[mi][i]=m;
2793: }
2794:
2795: wav[i]=mi;
2796: if(mi==0){
2797: nbwarn++;
2798: if(first==0){
2799: printf("Warning! No valid information for individual %ld line=%d (skipped) and may be others, see log file\n",num[i],i);
2800: first=1;
2801: }
2802: if(first==1){
2803: fprintf(ficlog,"Warning! No valid information for individual %ld line=%d (skipped)\n",num[i],i);
2804: }
2805: } /* end mi==0 */
2806: } /* End individuals */
2807:
2808: for(i=1; i<=imx; i++){
2809: for(mi=1; mi<wav[i];mi++){
2810: if (stepm <=0)
2811: dh[mi][i]=1;
2812: else{
2813: if (s[mw[mi+1][i]][i] > nlstate) { /* A death */
2814: if (agedc[i] < 2*AGESUP) {
2815: j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12);
2816: if(j==0) j=1; /* Survives at least one month after exam */
2817: else if(j<0){
2818: nberr++;
2819: 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]);
2820: j=1; /* Temporary Dangerous patch */
2821: 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);
2822: 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]);
2823: 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);
2824: }
2825: k=k+1;
2826: if (j >= jmax){
2827: jmax=j;
2828: ijmax=i;
2829: }
2830: if (j <= jmin){
2831: jmin=j;
2832: ijmin=i;
2833: }
2834: sum=sum+j;
2835: /*if (j<0) printf("j=%d num=%d \n",j,i);*/
2836: /* printf("%d %d %d %d\n", s[mw[mi][i]][i] ,s[mw[mi+1][i]][i],j,i);*/
2837: }
2838: }
2839: else{
2840: j= rint( (agev[mw[mi+1][i]][i]*12 - agev[mw[mi][i]][i]*12));
2841: /* 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]); */
2842:
2843: k=k+1;
2844: if (j >= jmax) {
2845: jmax=j;
2846: ijmax=i;
2847: }
2848: else if (j <= jmin){
2849: jmin=j;
2850: ijmin=i;
2851: }
2852: /* if (j<10) printf("j=%d jmin=%d num=%d ",j,jmin,i); */
2853: /*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]);*/
2854: if(j<0){
2855: nberr++;
2856: 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]);
2857: 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]);
2858: }
2859: sum=sum+j;
2860: }
2861: jk= j/stepm;
2862: jl= j -jk*stepm;
2863: ju= j -(jk+1)*stepm;
2864: if(mle <=1){ /* only if we use a the linear-interpoloation pseudo-likelihood */
2865: if(jl==0){
2866: dh[mi][i]=jk;
2867: bh[mi][i]=0;
2868: }else{ /* We want a negative bias in order to only have interpolation ie
1.136 brouard 2869: * to avoid the price of an extra matrix product in likelihood */
1.126 brouard 2870: dh[mi][i]=jk+1;
2871: bh[mi][i]=ju;
2872: }
2873: }else{
2874: if(jl <= -ju){
2875: dh[mi][i]=jk;
2876: bh[mi][i]=jl; /* bias is positive if real duration
2877: * is higher than the multiple of stepm and negative otherwise.
2878: */
2879: }
2880: else{
2881: dh[mi][i]=jk+1;
2882: bh[mi][i]=ju;
2883: }
2884: if(dh[mi][i]==0){
2885: dh[mi][i]=1; /* At least one step */
2886: bh[mi][i]=ju; /* At least one step */
2887: /* 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);*/
2888: }
2889: } /* end if mle */
2890: }
2891: } /* end wave */
2892: }
2893: jmean=sum/k;
2894: 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);
1.141 brouard 2895: 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);
1.126 brouard 2896: }
2897:
2898: /*********** Tricode ****************************/
1.145 brouard 2899: void tricode(int *Tvar, int **nbcode, int imx, int *Ndum)
1.126 brouard 2900: {
1.144 brouard 2901: /**< Uses cptcovn+2*cptcovprod as the number of covariates */
2902: /* Tvar[i]=atoi(stre); find 'n' in Vn and stores in Tvar. If model=V2+V1 Tvar[1]=2 and Tvar[2]=1
2903: /* Boring subroutine which should only output nbcode[Tvar[j]][k]
1.145 brouard 2904: * Tvar[5] in V2+V1+V3*age+V2*V4 is 2 (V2)
2905: /* nbcode[Tvar[j]][1]=
1.144 brouard 2906: */
1.130 brouard 2907:
1.145 brouard 2908: int ij=1, k=0, j=0, i=0, maxncov=NCOVMAX;
1.136 brouard 2909: int modmaxcovj=0; /* Modality max of covariates j */
1.145 brouard 2910: int cptcode=0; /* Modality max of covariates j */
2911: int modmincovj=0; /* Modality min of covariates j */
2912:
2913:
1.126 brouard 2914: cptcoveff=0;
2915:
1.145 brouard 2916: for (k=-1; k < maxncov; k++) Ndum[k]=0;
1.144 brouard 2917: for (k=1; k <= maxncov; k++) ncodemax[k]=0; /* Horrible constant again replaced by NCOVMAX */
1.126 brouard 2918:
1.145 brouard 2919: /* Loop on covariates without age and products */
2920: for (j=1; j<=(cptcovs); j++) { /* model V1 + V2*age+ V3 + V3*V4 : V1 + V3 = 2 only */
2921: for (i=1; i<=imx; i++) { /* Lopp on individuals: reads the data file to get the maximum value of the
1.136 brouard 2922: modality of this covariate Vj*/
1.145 brouard 2923: ij=(int)(covar[Tvar[j]][i]); /* ij=0 or 1 or -1. Value of the covariate Tvar[j] for individual i
2924: * If product of Vn*Vm, still boolean *:
2925: * If it was coded 1, 2, 3, 4 should be splitted into 3 boolean variables
2926: * 1 => 0 0 0, 2 => 0 0 1, 3 => 0 1 1, 4=1 0 0 */
2927: /* Finds for covariate j, n=Tvar[j] of Vn . ij is the
1.136 brouard 2928: modality of the nth covariate of individual i. */
1.145 brouard 2929: if (ij > modmaxcovj)
2930: modmaxcovj=ij;
2931: else if (ij < modmincovj)
2932: modmincovj=ij;
2933: if ((ij < -1) && (ij > NCOVMAX)){
2934: printf( "Error: minimal is less than -1 or maximal is bigger than %d. Exiting. \n", NCOVMAX );
2935: exit(1);
2936: }else
1.136 brouard 2937: Ndum[ij]++; /*counts and stores the occurence of this modality 0, 1, -1*/
1.145 brouard 2938: /* If coded 1, 2, 3 , counts the number of 1 Ndum[1], number of 2, Ndum[2], etc */
1.126 brouard 2939: /*printf("i=%d ij=%d Ndum[ij]=%d imx=%d",i,ij,Ndum[ij],imx);*/
1.136 brouard 2940: /* getting the maximum value of the modality of the covariate
2941: (should be 0 or 1 now) Tvar[j]. If V=sex and male is coded 0 and
2942: female is 1, then modmaxcovj=1.*/
1.126 brouard 2943: }
1.145 brouard 2944: printf(" Minimal and maximal values of %d th covariate V%d: min=%d max=%d \n", j, Tvar[j], modmincovj, modmaxcovj);
2945: cptcode=modmaxcovj;
1.137 brouard 2946: /* Ndum[0] = frequency of 0 for model-covariate j, Ndum[1] frequency of 1 etc. */
1.145 brouard 2947: /*for (i=0; i<=cptcode; i++) {*/
2948: for (i=modmincovj; i<=modmaxcovj; i++) { /* i=-1 ? 0 and 1*//* For each value of the modality of model-cov j */
2949: printf("Frequencies of covariates %d V%d %d\n", j, Tvar[j], Ndum[i]);
2950: if( Ndum[i] != 0 ){ /* Counts if nobody answered, empty modality */
2951: ncodemax[j]++; /* ncodemax[j]= Number of non-null modalities of the j th covariate. */
2952: }
2953: /* In fact ncodemax[j]=2 (dichotom. variables only) but it could be more for
2954: historical reasons: 3 if coded 1, 2, 3 and 4 and Ndum[2]=0 */
1.131 brouard 2955: } /* Ndum[-1] number of undefined modalities */
1.126 brouard 2956:
1.136 brouard 2957: /* j is a covariate, n=Tvar[j] of Vn; Fills nbcode */
1.145 brouard 2958: /* For covariate j, modalities could be 1, 2, 3, 4. If Ndum[2]=0 ncodemax[j] is not 4 but 3 */
2959: /* If Ndum[3}= 635; Ndum[4]=0; Ndum[5]=0; Ndum[6]=27; Ndum[7]=125;
2960: modmincovj=3; modmaxcovj = 7;
2961: There are only 3 modalities non empty (or 2 if 27 is too few) : ncodemax[j]=3;
2962: which will be coded 0, 1, 2 which in binary on 3-1 digits are 0=00 1=01, 2=10; defining two dummy
2963: variables V1_1 and V1_2.
2964: nbcode[Tvar[j]][ij]=k;
2965: nbcode[Tvar[j]][1]=0;
2966: nbcode[Tvar[j]][2]=1;
2967: nbcode[Tvar[j]][3]=2;
2968: */
2969: ij=1; /* ij is similar to i but can jumps over null modalities */
2970: for (i=modmincovj; i<=modmaxcovj; i++) { /* i= 1 to 2 for dichotomous, or from 1 to 3 */
2971: for (k=0; k<= cptcode; k++) { /* k=-1 ? k=0 to 1 *//* Could be 1 to 4 */
2972: /*recode from 0 */
1.131 brouard 2973: if (Ndum[k] != 0) { /* If at least one individual responded to this modality k */
2974: nbcode[Tvar[j]][ij]=k; /* stores the modality in an array nbcode.
2975: k is a modality. If we have model=V1+V1*sex
2976: then: nbcode[1][1]=0 ; nbcode[1][2]=1; nbcode[2][1]=0 ; nbcode[2][2]=1; */
1.126 brouard 2977: ij++;
2978: }
2979: if (ij > ncodemax[j]) break;
1.137 brouard 2980: } /* end of loop on */
2981: } /* end of loop on modality */
2982: } /* end of loop on model-covariate j. nbcode[Tvarj][1]=0 and nbcode[Tvarj][2]=1 sets the value of covariate j*/
2983:
1.145 brouard 2984: for (k=-1; k< maxncov; k++) Ndum[k]=0;
1.137 brouard 2985:
1.145 brouard 2986: for (i=1; i<=ncovmodel-2; i++) { /* -2, cste and age */
2987: /* Listing of all covariables in statement model to see if some covariates appear twice. For example, V1 appears twice in V1+V1*V2.*/
2988: ij=Tvar[i]; /* Tvar might be -1 if status was unknown */
2989: Ndum[ij]++;
2990: }
1.126 brouard 2991:
2992: ij=1;
1.145 brouard 2993: for (i=0; i<= maxncov-1; i++) { /* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) */
2994: /*printf("Ndum[%d]=%d\n",i, Ndum[i]);*/
1.126 brouard 2995: if((Ndum[i]!=0) && (i<=ncovcol)){
1.145 brouard 2996: /*printf("diff Ndum[%d]=%d\n",i, Ndum[i]);*/
2997: Tvaraff[ij]=i; /*For printing (unclear) */
1.126 brouard 2998: ij++;
1.145 brouard 2999: }else
3000: Tvaraff[ij]=0;
1.126 brouard 3001: }
1.131 brouard 3002: ij--;
1.144 brouard 3003: cptcoveff=ij; /*Number of total covariates*/
1.145 brouard 3004:
1.126 brouard 3005: }
3006:
1.145 brouard 3007:
1.126 brouard 3008: /*********** Health Expectancies ****************/
3009:
1.127 brouard 3010: void evsij(double ***eij, double x[], int nlstate, int stepm, int bage, int fage, double **oldm, double **savm, int cij, int estepm,char strstart[] )
1.126 brouard 3011:
3012: {
3013: /* Health expectancies, no variances */
1.164 brouard 3014: int i, j, nhstepm, hstepm, h, nstepm;
1.126 brouard 3015: int nhstepma, nstepma; /* Decreasing with age */
3016: double age, agelim, hf;
3017: double ***p3mat;
3018: double eip;
3019:
3020: pstamp(ficreseij);
3021: fprintf(ficreseij,"# (a) Life expectancies by health status at initial age and (b) health expectancies by health status at initial age\n");
3022: fprintf(ficreseij,"# Age");
3023: for(i=1; i<=nlstate;i++){
3024: for(j=1; j<=nlstate;j++){
3025: fprintf(ficreseij," e%1d%1d ",i,j);
3026: }
3027: fprintf(ficreseij," e%1d. ",i);
3028: }
3029: fprintf(ficreseij,"\n");
3030:
3031:
3032: if(estepm < stepm){
3033: printf ("Problem %d lower than %d\n",estepm, stepm);
3034: }
3035: else hstepm=estepm;
3036: /* We compute the life expectancy from trapezoids spaced every estepm months
3037: * This is mainly to measure the difference between two models: for example
3038: * if stepm=24 months pijx are given only every 2 years and by summing them
3039: * we are calculating an estimate of the Life Expectancy assuming a linear
3040: * progression in between and thus overestimating or underestimating according
3041: * to the curvature of the survival function. If, for the same date, we
3042: * estimate the model with stepm=1 month, we can keep estepm to 24 months
3043: * to compare the new estimate of Life expectancy with the same linear
3044: * hypothesis. A more precise result, taking into account a more precise
3045: * curvature will be obtained if estepm is as small as stepm. */
3046:
3047: /* For example we decided to compute the life expectancy with the smallest unit */
3048: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
3049: nhstepm is the number of hstepm from age to agelim
3050: nstepm is the number of stepm from age to agelin.
3051: Look at hpijx to understand the reason of that which relies in memory size
3052: and note for a fixed period like estepm months */
3053: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
3054: survival function given by stepm (the optimization length). Unfortunately it
3055: means that if the survival funtion is printed only each two years of age and if
3056: you sum them up and add 1 year (area under the trapezoids) you won't get the same
3057: results. So we changed our mind and took the option of the best precision.
3058: */
3059: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
3060:
3061: agelim=AGESUP;
3062: /* If stepm=6 months */
3063: /* Computed by stepm unit matrices, product of hstepm matrices, stored
3064: in an array of nhstepm length: nhstepm=10, hstepm=4, stepm=6 months */
3065:
3066: /* nhstepm age range expressed in number of stepm */
3067: nstepm=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
3068: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
3069: /* if (stepm >= YEARM) hstepm=1;*/
3070: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
3071: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
3072:
3073: for (age=bage; age<=fage; age ++){
3074: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
3075: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
3076: /* if (stepm >= YEARM) hstepm=1;*/
3077: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
3078:
3079: /* If stepm=6 months */
3080: /* Computed by stepm unit matrices, product of hstepma matrices, stored
3081: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
3082:
3083: hpxij(p3mat,nhstepma,age,hstepm,x,nlstate,stepm,oldm, savm, cij);
3084:
3085: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
3086:
3087: printf("%d|",(int)age);fflush(stdout);
3088: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
3089:
3090: /* Computing expectancies */
3091: for(i=1; i<=nlstate;i++)
3092: for(j=1; j<=nlstate;j++)
3093: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
3094: eij[i][j][(int)age] += (p3mat[i][j][h]+p3mat[i][j][h+1])/2.0*hf;
3095:
3096: /* 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]);*/
3097:
3098: }
3099:
3100: fprintf(ficreseij,"%3.0f",age );
3101: for(i=1; i<=nlstate;i++){
3102: eip=0;
3103: for(j=1; j<=nlstate;j++){
3104: eip +=eij[i][j][(int)age];
3105: fprintf(ficreseij,"%9.4f", eij[i][j][(int)age] );
3106: }
3107: fprintf(ficreseij,"%9.4f", eip );
3108: }
3109: fprintf(ficreseij,"\n");
3110:
3111: }
3112: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
3113: printf("\n");
3114: fprintf(ficlog,"\n");
3115:
3116: }
3117:
1.127 brouard 3118: 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[] )
1.126 brouard 3119:
3120: {
3121: /* Covariances of health expectancies eij and of total life expectancies according
3122: to initial status i, ei. .
3123: */
3124: int i, j, nhstepm, hstepm, h, nstepm, k, cptj, cptj2, i2, j2, ij, ji;
3125: int nhstepma, nstepma; /* Decreasing with age */
3126: double age, agelim, hf;
3127: double ***p3matp, ***p3matm, ***varhe;
3128: double **dnewm,**doldm;
3129: double *xp, *xm;
3130: double **gp, **gm;
3131: double ***gradg, ***trgradg;
3132: int theta;
3133:
3134: double eip, vip;
3135:
3136: varhe=ma3x(1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int) fage);
3137: xp=vector(1,npar);
3138: xm=vector(1,npar);
3139: dnewm=matrix(1,nlstate*nlstate,1,npar);
3140: doldm=matrix(1,nlstate*nlstate,1,nlstate*nlstate);
3141:
3142: pstamp(ficresstdeij);
3143: fprintf(ficresstdeij,"# Health expectancies with standard errors\n");
3144: fprintf(ficresstdeij,"# Age");
3145: for(i=1; i<=nlstate;i++){
3146: for(j=1; j<=nlstate;j++)
3147: fprintf(ficresstdeij," e%1d%1d (SE)",i,j);
3148: fprintf(ficresstdeij," e%1d. ",i);
3149: }
3150: fprintf(ficresstdeij,"\n");
3151:
3152: pstamp(ficrescveij);
3153: fprintf(ficrescveij,"# Subdiagonal matrix of covariances of health expectancies by age: cov(eij,ekl)\n");
3154: fprintf(ficrescveij,"# Age");
3155: for(i=1; i<=nlstate;i++)
3156: for(j=1; j<=nlstate;j++){
3157: cptj= (j-1)*nlstate+i;
3158: for(i2=1; i2<=nlstate;i2++)
3159: for(j2=1; j2<=nlstate;j2++){
3160: cptj2= (j2-1)*nlstate+i2;
3161: if(cptj2 <= cptj)
3162: fprintf(ficrescveij," %1d%1d,%1d%1d",i,j,i2,j2);
3163: }
3164: }
3165: fprintf(ficrescveij,"\n");
3166:
3167: if(estepm < stepm){
3168: printf ("Problem %d lower than %d\n",estepm, stepm);
3169: }
3170: else hstepm=estepm;
3171: /* We compute the life expectancy from trapezoids spaced every estepm months
3172: * This is mainly to measure the difference between two models: for example
3173: * if stepm=24 months pijx are given only every 2 years and by summing them
3174: * we are calculating an estimate of the Life Expectancy assuming a linear
3175: * progression in between and thus overestimating or underestimating according
3176: * to the curvature of the survival function. If, for the same date, we
3177: * estimate the model with stepm=1 month, we can keep estepm to 24 months
3178: * to compare the new estimate of Life expectancy with the same linear
3179: * hypothesis. A more precise result, taking into account a more precise
3180: * curvature will be obtained if estepm is as small as stepm. */
3181:
3182: /* For example we decided to compute the life expectancy with the smallest unit */
3183: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
3184: nhstepm is the number of hstepm from age to agelim
3185: nstepm is the number of stepm from age to agelin.
3186: Look at hpijx to understand the reason of that which relies in memory size
3187: and note for a fixed period like estepm months */
3188: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
3189: survival function given by stepm (the optimization length). Unfortunately it
3190: means that if the survival funtion is printed only each two years of age and if
3191: you sum them up and add 1 year (area under the trapezoids) you won't get the same
3192: results. So we changed our mind and took the option of the best precision.
3193: */
3194: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
3195:
3196: /* If stepm=6 months */
3197: /* nhstepm age range expressed in number of stepm */
3198: agelim=AGESUP;
3199: nstepm=(int) rint((agelim-bage)*YEARM/stepm);
3200: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
3201: /* if (stepm >= YEARM) hstepm=1;*/
3202: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
3203:
3204: p3matp=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
3205: p3matm=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
3206: gradg=ma3x(0,nhstepm,1,npar,1,nlstate*nlstate);
3207: trgradg =ma3x(0,nhstepm,1,nlstate*nlstate,1,npar);
3208: gp=matrix(0,nhstepm,1,nlstate*nlstate);
3209: gm=matrix(0,nhstepm,1,nlstate*nlstate);
3210:
3211: for (age=bage; age<=fage; age ++){
3212: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
3213: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
3214: /* if (stepm >= YEARM) hstepm=1;*/
3215: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
3216:
3217: /* If stepm=6 months */
3218: /* Computed by stepm unit matrices, product of hstepma matrices, stored
3219: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
3220:
3221: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
3222:
3223: /* Computing Variances of health expectancies */
3224: /* Gradient is computed with plus gp and minus gm. Code is duplicated in order to
3225: decrease memory allocation */
3226: for(theta=1; theta <=npar; theta++){
3227: for(i=1; i<=npar; i++){
3228: xp[i] = x[i] + (i==theta ?delti[theta]:0);
3229: xm[i] = x[i] - (i==theta ?delti[theta]:0);
3230: }
3231: hpxij(p3matp,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, cij);
3232: hpxij(p3matm,nhstepm,age,hstepm,xm,nlstate,stepm,oldm,savm, cij);
3233:
3234: for(j=1; j<= nlstate; j++){
3235: for(i=1; i<=nlstate; i++){
3236: for(h=0; h<=nhstepm-1; h++){
3237: gp[h][(j-1)*nlstate + i] = (p3matp[i][j][h]+p3matp[i][j][h+1])/2.;
3238: gm[h][(j-1)*nlstate + i] = (p3matm[i][j][h]+p3matm[i][j][h+1])/2.;
3239: }
3240: }
3241: }
3242:
3243: for(ij=1; ij<= nlstate*nlstate; ij++)
3244: for(h=0; h<=nhstepm-1; h++){
3245: gradg[h][theta][ij]= (gp[h][ij]-gm[h][ij])/2./delti[theta];
3246: }
3247: }/* End theta */
3248:
3249:
3250: for(h=0; h<=nhstepm-1; h++)
3251: for(j=1; j<=nlstate*nlstate;j++)
3252: for(theta=1; theta <=npar; theta++)
3253: trgradg[h][j][theta]=gradg[h][theta][j];
3254:
3255:
3256: for(ij=1;ij<=nlstate*nlstate;ij++)
3257: for(ji=1;ji<=nlstate*nlstate;ji++)
3258: varhe[ij][ji][(int)age] =0.;
3259:
3260: printf("%d|",(int)age);fflush(stdout);
3261: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
3262: for(h=0;h<=nhstepm-1;h++){
3263: for(k=0;k<=nhstepm-1;k++){
3264: matprod2(dnewm,trgradg[h],1,nlstate*nlstate,1,npar,1,npar,matcov);
3265: matprod2(doldm,dnewm,1,nlstate*nlstate,1,npar,1,nlstate*nlstate,gradg[k]);
3266: for(ij=1;ij<=nlstate*nlstate;ij++)
3267: for(ji=1;ji<=nlstate*nlstate;ji++)
3268: varhe[ij][ji][(int)age] += doldm[ij][ji]*hf*hf;
3269: }
3270: }
3271:
3272: /* Computing expectancies */
3273: hpxij(p3matm,nhstepm,age,hstepm,x,nlstate,stepm,oldm, savm, cij);
3274: for(i=1; i<=nlstate;i++)
3275: for(j=1; j<=nlstate;j++)
3276: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
3277: eij[i][j][(int)age] += (p3matm[i][j][h]+p3matm[i][j][h+1])/2.0*hf;
3278:
3279: /* 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]);*/
3280:
3281: }
3282:
3283: fprintf(ficresstdeij,"%3.0f",age );
3284: for(i=1; i<=nlstate;i++){
3285: eip=0.;
3286: vip=0.;
3287: for(j=1; j<=nlstate;j++){
3288: eip += eij[i][j][(int)age];
3289: for(k=1; k<=nlstate;k++) /* Sum on j and k of cov(eij,eik) */
3290: vip += varhe[(j-1)*nlstate+i][(k-1)*nlstate+i][(int)age];
3291: fprintf(ficresstdeij," %9.4f (%.4f)", eij[i][j][(int)age], sqrt(varhe[(j-1)*nlstate+i][(j-1)*nlstate+i][(int)age]) );
3292: }
3293: fprintf(ficresstdeij," %9.4f (%.4f)", eip, sqrt(vip));
3294: }
3295: fprintf(ficresstdeij,"\n");
3296:
3297: fprintf(ficrescveij,"%3.0f",age );
3298: for(i=1; i<=nlstate;i++)
3299: for(j=1; j<=nlstate;j++){
3300: cptj= (j-1)*nlstate+i;
3301: for(i2=1; i2<=nlstate;i2++)
3302: for(j2=1; j2<=nlstate;j2++){
3303: cptj2= (j2-1)*nlstate+i2;
3304: if(cptj2 <= cptj)
3305: fprintf(ficrescveij," %.4f", varhe[cptj][cptj2][(int)age]);
3306: }
3307: }
3308: fprintf(ficrescveij,"\n");
3309:
3310: }
3311: free_matrix(gm,0,nhstepm,1,nlstate*nlstate);
3312: free_matrix(gp,0,nhstepm,1,nlstate*nlstate);
3313: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate*nlstate);
3314: free_ma3x(trgradg,0,nhstepm,1,nlstate*nlstate,1,npar);
3315: free_ma3x(p3matm,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
3316: free_ma3x(p3matp,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
3317: printf("\n");
3318: fprintf(ficlog,"\n");
3319:
3320: free_vector(xm,1,npar);
3321: free_vector(xp,1,npar);
3322: free_matrix(dnewm,1,nlstate*nlstate,1,npar);
3323: free_matrix(doldm,1,nlstate*nlstate,1,nlstate*nlstate);
3324: free_ma3x(varhe,1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int)fage);
3325: }
3326:
3327: /************ Variance ******************/
3328: 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[])
3329: {
3330: /* Variance of health expectancies */
3331: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);*/
3332: /* double **newm;*/
3333: double **dnewm,**doldm;
3334: double **dnewmp,**doldmp;
3335: int i, j, nhstepm, hstepm, h, nstepm ;
1.164 brouard 3336: int k;
1.126 brouard 3337: double *xp;
3338: double **gp, **gm; /* for var eij */
3339: double ***gradg, ***trgradg; /*for var eij */
3340: double **gradgp, **trgradgp; /* for var p point j */
3341: double *gpp, *gmp; /* for var p point j */
3342: double **varppt; /* for var p point j nlstate to nlstate+ndeath */
3343: double ***p3mat;
3344: double age,agelim, hf;
3345: double ***mobaverage;
3346: int theta;
3347: char digit[4];
3348: char digitp[25];
3349:
3350: char fileresprobmorprev[FILENAMELENGTH];
3351:
3352: if(popbased==1){
3353: if(mobilav!=0)
3354: strcpy(digitp,"-populbased-mobilav-");
3355: else strcpy(digitp,"-populbased-nomobil-");
3356: }
3357: else
3358: strcpy(digitp,"-stablbased-");
3359:
3360: if (mobilav!=0) {
3361: mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
3362: if (movingaverage(probs, bage, fage, mobaverage,mobilav)!=0){
3363: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
3364: printf(" Error in movingaverage mobilav=%d\n",mobilav);
3365: }
3366: }
3367:
3368: strcpy(fileresprobmorprev,"prmorprev");
3369: sprintf(digit,"%-d",ij);
3370: /*printf("DIGIT=%s, ij=%d ijr=%-d|\n",digit, ij,ij);*/
3371: strcat(fileresprobmorprev,digit); /* Tvar to be done */
3372: strcat(fileresprobmorprev,digitp); /* Popbased or not, mobilav or not */
3373: strcat(fileresprobmorprev,fileres);
3374: if((ficresprobmorprev=fopen(fileresprobmorprev,"w"))==NULL) {
3375: printf("Problem with resultfile: %s\n", fileresprobmorprev);
3376: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobmorprev);
3377: }
3378: printf("Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
3379:
3380: fprintf(ficlog,"Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
3381: pstamp(ficresprobmorprev);
3382: 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);
3383: fprintf(ficresprobmorprev,"# Age cov=%-d",ij);
3384: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
3385: fprintf(ficresprobmorprev," p.%-d SE",j);
3386: for(i=1; i<=nlstate;i++)
3387: fprintf(ficresprobmorprev," w%1d p%-d%-d",i,i,j);
3388: }
3389: fprintf(ficresprobmorprev,"\n");
3390: fprintf(ficgp,"\n# Routine varevsij");
3391: /* fprintf(fichtm, "#Local time at start: %s", strstart);*/
3392: 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");
3393: fprintf(fichtm,"\n<br>%s <br>\n",digitp);
3394: /* } */
3395: varppt = matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
3396: pstamp(ficresvij);
3397: fprintf(ficresvij,"# Variance and covariance of health expectancies e.j \n# (weighted average of eij where weights are ");
3398: if(popbased==1)
1.128 brouard 3399: 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);
1.126 brouard 3400: else
3401: fprintf(ficresvij,"the age specific period (stable) prevalences in each health state \n");
3402: fprintf(ficresvij,"# Age");
3403: for(i=1; i<=nlstate;i++)
3404: for(j=1; j<=nlstate;j++)
3405: fprintf(ficresvij," Cov(e.%1d, e.%1d)",i,j);
3406: fprintf(ficresvij,"\n");
3407:
3408: xp=vector(1,npar);
3409: dnewm=matrix(1,nlstate,1,npar);
3410: doldm=matrix(1,nlstate,1,nlstate);
3411: dnewmp= matrix(nlstate+1,nlstate+ndeath,1,npar);
3412: doldmp= matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
3413:
3414: gradgp=matrix(1,npar,nlstate+1,nlstate+ndeath);
3415: gpp=vector(nlstate+1,nlstate+ndeath);
3416: gmp=vector(nlstate+1,nlstate+ndeath);
3417: trgradgp =matrix(nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
3418:
3419: if(estepm < stepm){
3420: printf ("Problem %d lower than %d\n",estepm, stepm);
3421: }
3422: else hstepm=estepm;
3423: /* For example we decided to compute the life expectancy with the smallest unit */
3424: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
3425: nhstepm is the number of hstepm from age to agelim
3426: nstepm is the number of stepm from age to agelin.
1.128 brouard 3427: Look at function hpijx to understand why (it is linked to memory size questions) */
1.126 brouard 3428: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
3429: survival function given by stepm (the optimization length). Unfortunately it
3430: means that if the survival funtion is printed every two years of age and if
3431: you sum them up and add 1 year (area under the trapezoids) you won't get the same
3432: results. So we changed our mind and took the option of the best precision.
3433: */
3434: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
3435: agelim = AGESUP;
3436: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
3437: nstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
3438: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
3439: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
3440: gradg=ma3x(0,nhstepm,1,npar,1,nlstate);
3441: gp=matrix(0,nhstepm,1,nlstate);
3442: gm=matrix(0,nhstepm,1,nlstate);
3443:
3444:
3445: for(theta=1; theta <=npar; theta++){
3446: for(i=1; i<=npar; i++){ /* Computes gradient x + delta*/
3447: xp[i] = x[i] + (i==theta ?delti[theta]:0);
3448: }
3449: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij);
3450: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ij);
3451:
3452: if (popbased==1) {
3453: if(mobilav ==0){
3454: for(i=1; i<=nlstate;i++)
3455: prlim[i][i]=probs[(int)age][i][ij];
3456: }else{ /* mobilav */
3457: for(i=1; i<=nlstate;i++)
3458: prlim[i][i]=mobaverage[(int)age][i][ij];
3459: }
3460: }
3461:
3462: for(j=1; j<= nlstate; j++){
3463: for(h=0; h<=nhstepm; h++){
3464: for(i=1, gp[h][j]=0.;i<=nlstate;i++)
3465: gp[h][j] += prlim[i][i]*p3mat[i][j][h];
3466: }
3467: }
3468: /* This for computing probability of death (h=1 means
3469: computed over hstepm matrices product = hstepm*stepm months)
3470: as a weighted average of prlim.
3471: */
3472: for(j=nlstate+1;j<=nlstate+ndeath;j++){
3473: for(i=1,gpp[j]=0.; i<= nlstate; i++)
3474: gpp[j] += prlim[i][i]*p3mat[i][j][1];
3475: }
3476: /* end probability of death */
3477:
3478: for(i=1; i<=npar; i++) /* Computes gradient x - delta */
3479: xp[i] = x[i] - (i==theta ?delti[theta]:0);
3480: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij);
3481: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ij);
3482:
3483: if (popbased==1) {
3484: if(mobilav ==0){
3485: for(i=1; i<=nlstate;i++)
3486: prlim[i][i]=probs[(int)age][i][ij];
3487: }else{ /* mobilav */
3488: for(i=1; i<=nlstate;i++)
3489: prlim[i][i]=mobaverage[(int)age][i][ij];
3490: }
3491: }
3492:
1.128 brouard 3493: for(j=1; j<= nlstate; j++){ /* Sum of wi * eij = e.j */
1.126 brouard 3494: for(h=0; h<=nhstepm; h++){
3495: for(i=1, gm[h][j]=0.;i<=nlstate;i++)
3496: gm[h][j] += prlim[i][i]*p3mat[i][j][h];
3497: }
3498: }
3499: /* This for computing probability of death (h=1 means
3500: computed over hstepm matrices product = hstepm*stepm months)
3501: as a weighted average of prlim.
3502: */
3503: for(j=nlstate+1;j<=nlstate+ndeath;j++){
3504: for(i=1,gmp[j]=0.; i<= nlstate; i++)
3505: gmp[j] += prlim[i][i]*p3mat[i][j][1];
3506: }
3507: /* end probability of death */
3508:
3509: for(j=1; j<= nlstate; j++) /* vareij */
3510: for(h=0; h<=nhstepm; h++){
3511: gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta];
3512: }
3513:
3514: for(j=nlstate+1; j<= nlstate+ndeath; j++){ /* var mu */
3515: gradgp[theta][j]= (gpp[j]-gmp[j])/2./delti[theta];
3516: }
3517:
3518: } /* End theta */
3519:
3520: trgradg =ma3x(0,nhstepm,1,nlstate,1,npar); /* veij */
3521:
3522: for(h=0; h<=nhstepm; h++) /* veij */
3523: for(j=1; j<=nlstate;j++)
3524: for(theta=1; theta <=npar; theta++)
3525: trgradg[h][j][theta]=gradg[h][theta][j];
3526:
3527: for(j=nlstate+1; j<=nlstate+ndeath;j++) /* mu */
3528: for(theta=1; theta <=npar; theta++)
3529: trgradgp[j][theta]=gradgp[theta][j];
3530:
3531:
3532: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
3533: for(i=1;i<=nlstate;i++)
3534: for(j=1;j<=nlstate;j++)
3535: vareij[i][j][(int)age] =0.;
3536:
3537: for(h=0;h<=nhstepm;h++){
3538: for(k=0;k<=nhstepm;k++){
3539: matprod2(dnewm,trgradg[h],1,nlstate,1,npar,1,npar,matcov);
3540: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg[k]);
3541: for(i=1;i<=nlstate;i++)
3542: for(j=1;j<=nlstate;j++)
3543: vareij[i][j][(int)age] += doldm[i][j]*hf*hf;
3544: }
3545: }
3546:
3547: /* pptj */
3548: matprod2(dnewmp,trgradgp,nlstate+1,nlstate+ndeath,1,npar,1,npar,matcov);
3549: matprod2(doldmp,dnewmp,nlstate+1,nlstate+ndeath,1,npar,nlstate+1,nlstate+ndeath,gradgp);
3550: for(j=nlstate+1;j<=nlstate+ndeath;j++)
3551: for(i=nlstate+1;i<=nlstate+ndeath;i++)
3552: varppt[j][i]=doldmp[j][i];
3553: /* end ppptj */
3554: /* x centered again */
3555: hpxij(p3mat,nhstepm,age,hstepm,x,nlstate,stepm,oldm,savm, ij);
3556: prevalim(prlim,nlstate,x,age,oldm,savm,ftolpl,ij);
3557:
3558: if (popbased==1) {
3559: if(mobilav ==0){
3560: for(i=1; i<=nlstate;i++)
3561: prlim[i][i]=probs[(int)age][i][ij];
3562: }else{ /* mobilav */
3563: for(i=1; i<=nlstate;i++)
3564: prlim[i][i]=mobaverage[(int)age][i][ij];
3565: }
3566: }
3567:
3568: /* This for computing probability of death (h=1 means
3569: computed over hstepm (estepm) matrices product = hstepm*stepm months)
3570: as a weighted average of prlim.
3571: */
3572: for(j=nlstate+1;j<=nlstate+ndeath;j++){
3573: for(i=1,gmp[j]=0.;i<= nlstate; i++)
3574: gmp[j] += prlim[i][i]*p3mat[i][j][1];
3575: }
3576: /* end probability of death */
3577:
3578: fprintf(ficresprobmorprev,"%3d %d ",(int) age, ij);
3579: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
3580: fprintf(ficresprobmorprev," %11.3e %11.3e",gmp[j], sqrt(varppt[j][j]));
3581: for(i=1; i<=nlstate;i++){
3582: fprintf(ficresprobmorprev," %11.3e %11.3e ",prlim[i][i],p3mat[i][j][1]);
3583: }
3584: }
3585: fprintf(ficresprobmorprev,"\n");
3586:
3587: fprintf(ficresvij,"%.0f ",age );
3588: for(i=1; i<=nlstate;i++)
3589: for(j=1; j<=nlstate;j++){
3590: fprintf(ficresvij," %.4f", vareij[i][j][(int)age]);
3591: }
3592: fprintf(ficresvij,"\n");
3593: free_matrix(gp,0,nhstepm,1,nlstate);
3594: free_matrix(gm,0,nhstepm,1,nlstate);
3595: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate);
3596: free_ma3x(trgradg,0,nhstepm,1,nlstate,1,npar);
3597: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
3598: } /* End age */
3599: free_vector(gpp,nlstate+1,nlstate+ndeath);
3600: free_vector(gmp,nlstate+1,nlstate+ndeath);
3601: free_matrix(gradgp,1,npar,nlstate+1,nlstate+ndeath);
3602: free_matrix(trgradgp,nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
1.145 brouard 3603: fprintf(ficgp,"\nunset parametric;unset label; set ter png small size 320, 240");
1.126 brouard 3604: /* for(j=nlstate+1; j<= nlstate+ndeath; j++){ *//* Only the first actually */
1.131 brouard 3605: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Force of mortality (year-1)\";");
1.126 brouard 3606: /* fprintf(ficgp,"\n plot \"%s\" u 1:($3*%6.3f) not w l 1 ",fileresprobmorprev,YEARM/estepm); */
3607: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)*%6.3f) t \"95\%% interval\" w l 2 ",fileresprobmorprev,YEARM/estepm); */
3608: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)*%6.3f) not w l 2 ",fileresprobmorprev,YEARM/estepm); */
1.145 brouard 3609: fprintf(ficgp,"\n plot \"%s\" u 1:($3) not w l lt 1 ",subdirf(fileresprobmorprev));
3610: fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)) t \"95\%% interval\" w l lt 2 ",subdirf(fileresprobmorprev));
3611: fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)) not w l lt 2 ",subdirf(fileresprobmorprev));
1.126 brouard 3612: fprintf(fichtm,"\n<br> File (multiple files are possible if covariates are present): <A href=\"%s\">%s</a>\n",subdirf(fileresprobmorprev),subdirf(fileresprobmorprev));
3613: 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);
3614: /* 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);
3615: */
3616: /* fprintf(ficgp,"\nset out \"varmuptjgr%s%s%s.png\";replot;",digitp,optionfilefiname,digit); */
3617: fprintf(ficgp,"\nset out \"%s%s.png\";replot;\n",subdirf3(optionfilefiname,"varmuptjgr",digitp),digit);
3618:
3619: free_vector(xp,1,npar);
3620: free_matrix(doldm,1,nlstate,1,nlstate);
3621: free_matrix(dnewm,1,nlstate,1,npar);
3622: free_matrix(doldmp,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
3623: free_matrix(dnewmp,nlstate+1,nlstate+ndeath,1,npar);
3624: free_matrix(varppt,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
3625: if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
3626: fclose(ficresprobmorprev);
3627: fflush(ficgp);
3628: fflush(fichtm);
3629: } /* end varevsij */
3630:
3631: /************ Variance of prevlim ******************/
3632: 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[])
3633: {
3634: /* Variance of prevalence limit */
3635: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
1.164 brouard 3636:
1.126 brouard 3637: double **dnewm,**doldm;
3638: int i, j, nhstepm, hstepm;
3639: double *xp;
3640: double *gp, *gm;
3641: double **gradg, **trgradg;
3642: double age,agelim;
3643: int theta;
3644:
3645: pstamp(ficresvpl);
3646: fprintf(ficresvpl,"# Standard deviation of period (stable) prevalences \n");
3647: fprintf(ficresvpl,"# Age");
3648: for(i=1; i<=nlstate;i++)
3649: fprintf(ficresvpl," %1d-%1d",i,i);
3650: fprintf(ficresvpl,"\n");
3651:
3652: xp=vector(1,npar);
3653: dnewm=matrix(1,nlstate,1,npar);
3654: doldm=matrix(1,nlstate,1,nlstate);
3655:
3656: hstepm=1*YEARM; /* Every year of age */
3657: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
3658: agelim = AGESUP;
3659: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
3660: nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
3661: if (stepm >= YEARM) hstepm=1;
3662: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
3663: gradg=matrix(1,npar,1,nlstate);
3664: gp=vector(1,nlstate);
3665: gm=vector(1,nlstate);
3666:
3667: for(theta=1; theta <=npar; theta++){
3668: for(i=1; i<=npar; i++){ /* Computes gradient */
3669: xp[i] = x[i] + (i==theta ?delti[theta]:0);
3670: }
3671: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ij);
3672: for(i=1;i<=nlstate;i++)
3673: gp[i] = prlim[i][i];
3674:
3675: for(i=1; i<=npar; i++) /* Computes gradient */
3676: xp[i] = x[i] - (i==theta ?delti[theta]:0);
3677: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ij);
3678: for(i=1;i<=nlstate;i++)
3679: gm[i] = prlim[i][i];
3680:
3681: for(i=1;i<=nlstate;i++)
3682: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
3683: } /* End theta */
3684:
3685: trgradg =matrix(1,nlstate,1,npar);
3686:
3687: for(j=1; j<=nlstate;j++)
3688: for(theta=1; theta <=npar; theta++)
3689: trgradg[j][theta]=gradg[theta][j];
3690:
3691: for(i=1;i<=nlstate;i++)
3692: varpl[i][(int)age] =0.;
3693: matprod2(dnewm,trgradg,1,nlstate,1,npar,1,npar,matcov);
3694: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg);
3695: for(i=1;i<=nlstate;i++)
3696: varpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
3697:
3698: fprintf(ficresvpl,"%.0f ",age );
3699: for(i=1; i<=nlstate;i++)
3700: fprintf(ficresvpl," %.5f (%.5f)",prlim[i][i],sqrt(varpl[i][(int)age]));
3701: fprintf(ficresvpl,"\n");
3702: free_vector(gp,1,nlstate);
3703: free_vector(gm,1,nlstate);
3704: free_matrix(gradg,1,npar,1,nlstate);
3705: free_matrix(trgradg,1,nlstate,1,npar);
3706: } /* End age */
3707:
3708: free_vector(xp,1,npar);
3709: free_matrix(doldm,1,nlstate,1,npar);
3710: free_matrix(dnewm,1,nlstate,1,nlstate);
3711:
3712: }
3713:
3714: /************ Variance of one-step probabilities ******************/
3715: 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[])
3716: {
1.164 brouard 3717: int i, j=0, k1, l1, tj;
1.126 brouard 3718: int k2, l2, j1, z1;
1.164 brouard 3719: int k=0, l;
1.145 brouard 3720: int first=1, first1, first2;
1.126 brouard 3721: double cv12, mu1, mu2, lc1, lc2, v12, v21, v11, v22,v1,v2, c12, tnalp;
3722: double **dnewm,**doldm;
3723: double *xp;
3724: double *gp, *gm;
3725: double **gradg, **trgradg;
3726: double **mu;
1.164 brouard 3727: double age, cov[NCOVMAX+1];
1.126 brouard 3728: double std=2.0; /* Number of standard deviation wide of confidence ellipsoids */
3729: int theta;
3730: char fileresprob[FILENAMELENGTH];
3731: char fileresprobcov[FILENAMELENGTH];
3732: char fileresprobcor[FILENAMELENGTH];
3733: double ***varpij;
3734:
3735: strcpy(fileresprob,"prob");
3736: strcat(fileresprob,fileres);
3737: if((ficresprob=fopen(fileresprob,"w"))==NULL) {
3738: printf("Problem with resultfile: %s\n", fileresprob);
3739: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprob);
3740: }
3741: strcpy(fileresprobcov,"probcov");
3742: strcat(fileresprobcov,fileres);
3743: if((ficresprobcov=fopen(fileresprobcov,"w"))==NULL) {
3744: printf("Problem with resultfile: %s\n", fileresprobcov);
3745: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcov);
3746: }
3747: strcpy(fileresprobcor,"probcor");
3748: strcat(fileresprobcor,fileres);
3749: if((ficresprobcor=fopen(fileresprobcor,"w"))==NULL) {
3750: printf("Problem with resultfile: %s\n", fileresprobcor);
3751: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcor);
3752: }
3753: printf("Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
3754: fprintf(ficlog,"Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
3755: printf("Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
3756: fprintf(ficlog,"Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
3757: printf("and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
3758: fprintf(ficlog,"and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
3759: pstamp(ficresprob);
3760: fprintf(ficresprob,"#One-step probabilities and stand. devi in ()\n");
3761: fprintf(ficresprob,"# Age");
3762: pstamp(ficresprobcov);
3763: fprintf(ficresprobcov,"#One-step probabilities and covariance matrix\n");
3764: fprintf(ficresprobcov,"# Age");
3765: pstamp(ficresprobcor);
3766: fprintf(ficresprobcor,"#One-step probabilities and correlation matrix\n");
3767: fprintf(ficresprobcor,"# Age");
3768:
3769:
3770: for(i=1; i<=nlstate;i++)
3771: for(j=1; j<=(nlstate+ndeath);j++){
3772: fprintf(ficresprob," p%1d-%1d (SE)",i,j);
3773: fprintf(ficresprobcov," p%1d-%1d ",i,j);
3774: fprintf(ficresprobcor," p%1d-%1d ",i,j);
3775: }
3776: /* fprintf(ficresprob,"\n");
3777: fprintf(ficresprobcov,"\n");
3778: fprintf(ficresprobcor,"\n");
3779: */
1.131 brouard 3780: xp=vector(1,npar);
1.126 brouard 3781: dnewm=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
3782: doldm=matrix(1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
3783: mu=matrix(1,(nlstate)*(nlstate+ndeath), (int) bage, (int)fage);
3784: varpij=ma3x(1,nlstate*(nlstate+ndeath),1,nlstate*(nlstate+ndeath),(int) bage, (int) fage);
3785: first=1;
3786: fprintf(ficgp,"\n# Routine varprob");
3787: fprintf(fichtm,"\n<li><h4> Computing and drawing one step probabilities with their confidence intervals</h4></li>\n");
3788: fprintf(fichtm,"\n");
3789:
3790: fprintf(fichtm,"\n<li><h4> <a href=\"%s\">Matrix of variance-covariance of pairs of step probabilities (drawings)</a></h4></li>\n",optionfilehtmcov);
3791: fprintf(fichtmcov,"\n<h4>Matrix of variance-covariance of pairs of step probabilities</h4>\n\
3792: file %s<br>\n",optionfilehtmcov);
3793: fprintf(fichtmcov,"\nEllipsoids of confidence centered on point (p<inf>ij</inf>, p<inf>kl</inf>) are estimated\
3794: and drawn. It helps understanding how is the covariance between two incidences.\
3795: They are expressed in year<sup>-1</sup> in order to be less dependent of stepm.<br>\n");
3796: 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. \
3797: It can be understood this way: if pij and pkl where uncorrelated the (2x2) matrix of covariance \
3798: would have been (1/(var pij), 0 , 0, 1/(var pkl)), and the confidence interval would be 2 \
3799: standard deviations wide on each axis. <br>\
3800: Now, if both incidences are correlated (usual case) we diagonalised the inverse of the covariance matrix\
3801: and made the appropriate rotation to look at the uncorrelated principal directions.<br>\
3802: To be simple, these graphs help to understand the significativity of each parameter in relation to a second other one.<br> \n");
3803:
3804: cov[1]=1;
1.145 brouard 3805: /* tj=cptcoveff; */
3806: tj = (int) pow(2,cptcoveff);
1.126 brouard 3807: if (cptcovn<1) {tj=1;ncodemax[1]=1;}
3808: j1=0;
1.145 brouard 3809: for(j1=1; j1<=tj;j1++){
3810: /*for(i1=1; i1<=ncodemax[t];i1++){ */
3811: /*j1++;*/
1.126 brouard 3812: if (cptcovn>0) {
3813: fprintf(ficresprob, "\n#********** Variable ");
3814: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprob, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
3815: fprintf(ficresprob, "**********\n#\n");
3816: fprintf(ficresprobcov, "\n#********** Variable ");
3817: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcov, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
3818: fprintf(ficresprobcov, "**********\n#\n");
3819:
3820: fprintf(ficgp, "\n#********** Variable ");
3821: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficgp, " V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
3822: fprintf(ficgp, "**********\n#\n");
3823:
3824:
3825: fprintf(fichtmcov, "\n<hr size=\"2\" color=\"#EC5E5E\">********** Variable ");
3826: for (z1=1; z1<=cptcoveff; z1++) fprintf(fichtm, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
3827: fprintf(fichtmcov, "**********\n<hr size=\"2\" color=\"#EC5E5E\">");
3828:
3829: fprintf(ficresprobcor, "\n#********** Variable ");
3830: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcor, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
3831: fprintf(ficresprobcor, "**********\n#");
3832: }
3833:
1.145 brouard 3834: gradg=matrix(1,npar,1,(nlstate)*(nlstate+ndeath));
3835: trgradg=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
3836: gp=vector(1,(nlstate)*(nlstate+ndeath));
3837: gm=vector(1,(nlstate)*(nlstate+ndeath));
1.126 brouard 3838: for (age=bage; age<=fage; age ++){
3839: cov[2]=age;
3840: for (k=1; k<=cptcovn;k++) {
1.145 brouard 3841: cov[2+k]=nbcode[Tvar[k]][codtab[j1][Tvar[k]]];/* j1 1 2 3 4
3842: * 1 1 1 1 1
3843: * 2 2 1 1 1
3844: * 3 1 2 1 1
3845: */
3846: /* nbcode[1][1]=0 nbcode[1][2]=1;*/
1.126 brouard 3847: }
3848: for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2];
3849: for (k=1; k<=cptcovprod;k++)
3850: cov[2+Tprod[k]]=nbcode[Tvard[k][1]][codtab[ij][Tvard[k][1]]]*nbcode[Tvard[k][2]][codtab[ij][Tvard[k][2]]];
3851:
3852:
3853: for(theta=1; theta <=npar; theta++){
3854: for(i=1; i<=npar; i++)
3855: xp[i] = x[i] + (i==theta ?delti[theta]:(double)0);
3856:
3857: pmij(pmmij,cov,ncovmodel,xp,nlstate);
3858:
3859: k=0;
3860: for(i=1; i<= (nlstate); i++){
3861: for(j=1; j<=(nlstate+ndeath);j++){
3862: k=k+1;
3863: gp[k]=pmmij[i][j];
3864: }
3865: }
3866:
3867: for(i=1; i<=npar; i++)
3868: xp[i] = x[i] - (i==theta ?delti[theta]:(double)0);
3869:
3870: pmij(pmmij,cov,ncovmodel,xp,nlstate);
3871: k=0;
3872: for(i=1; i<=(nlstate); i++){
3873: for(j=1; j<=(nlstate+ndeath);j++){
3874: k=k+1;
3875: gm[k]=pmmij[i][j];
3876: }
3877: }
3878:
3879: for(i=1; i<= (nlstate)*(nlstate+ndeath); i++)
3880: gradg[theta][i]=(gp[i]-gm[i])/(double)2./delti[theta];
3881: }
3882:
3883: for(j=1; j<=(nlstate)*(nlstate+ndeath);j++)
3884: for(theta=1; theta <=npar; theta++)
3885: trgradg[j][theta]=gradg[theta][j];
3886:
3887: matprod2(dnewm,trgradg,1,(nlstate)*(nlstate+ndeath),1,npar,1,npar,matcov);
3888: matprod2(doldm,dnewm,1,(nlstate)*(nlstate+ndeath),1,npar,1,(nlstate)*(nlstate+ndeath),gradg);
3889:
3890: pmij(pmmij,cov,ncovmodel,x,nlstate);
3891:
3892: k=0;
3893: for(i=1; i<=(nlstate); i++){
3894: for(j=1; j<=(nlstate+ndeath);j++){
3895: k=k+1;
3896: mu[k][(int) age]=pmmij[i][j];
3897: }
3898: }
3899: for(i=1;i<=(nlstate)*(nlstate+ndeath);i++)
3900: for(j=1;j<=(nlstate)*(nlstate+ndeath);j++)
3901: varpij[i][j][(int)age] = doldm[i][j];
3902:
3903: /*printf("\n%d ",(int)age);
3904: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
3905: printf("%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
3906: fprintf(ficlog,"%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
3907: }*/
3908:
3909: fprintf(ficresprob,"\n%d ",(int)age);
3910: fprintf(ficresprobcov,"\n%d ",(int)age);
3911: fprintf(ficresprobcor,"\n%d ",(int)age);
3912:
3913: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++)
3914: fprintf(ficresprob,"%11.3e (%11.3e) ",mu[i][(int) age],sqrt(varpij[i][i][(int)age]));
3915: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
3916: fprintf(ficresprobcov,"%11.3e ",mu[i][(int) age]);
3917: fprintf(ficresprobcor,"%11.3e ",mu[i][(int) age]);
3918: }
3919: i=0;
3920: for (k=1; k<=(nlstate);k++){
3921: for (l=1; l<=(nlstate+ndeath);l++){
1.145 brouard 3922: i++;
1.126 brouard 3923: fprintf(ficresprobcov,"\n%d %d-%d",(int)age,k,l);
3924: fprintf(ficresprobcor,"\n%d %d-%d",(int)age,k,l);
3925: for (j=1; j<=i;j++){
1.145 brouard 3926: /* printf(" k=%d l=%d i=%d j=%d\n",k,l,i,j);fflush(stdout); */
1.126 brouard 3927: fprintf(ficresprobcov," %11.3e",varpij[i][j][(int)age]);
3928: fprintf(ficresprobcor," %11.3e",varpij[i][j][(int) age]/sqrt(varpij[i][i][(int) age])/sqrt(varpij[j][j][(int)age]));
3929: }
3930: }
3931: }/* end of loop for state */
3932: } /* end of loop for age */
1.145 brouard 3933: free_vector(gp,1,(nlstate+ndeath)*(nlstate+ndeath));
3934: free_vector(gm,1,(nlstate+ndeath)*(nlstate+ndeath));
3935: free_matrix(trgradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
3936: free_matrix(gradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
3937:
1.126 brouard 3938: /* Confidence intervalle of pij */
3939: /*
1.131 brouard 3940: fprintf(ficgp,"\nunset parametric;unset label");
1.126 brouard 3941: fprintf(ficgp,"\nset log y;unset log x; set xlabel \"Age\";set ylabel \"probability (year-1)\"");
3942: fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65");
3943: 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);
3944: fprintf(fichtm,"\n<br><img src=\"pijgr%s.png\"> ",optionfilefiname);
3945: fprintf(ficgp,"\nset out \"pijgr%s.png\"",optionfilefiname);
3946: fprintf(ficgp,"\nplot \"%s\" every :::%d::%d u 1:2 \"\%%lf",k1,k2,xfilevarprob);
3947: */
3948:
3949: /* Drawing ellipsoids of confidence of two variables p(k1-l1,k2-l2)*/
1.145 brouard 3950: first1=1;first2=2;
1.126 brouard 3951: for (k2=1; k2<=(nlstate);k2++){
3952: for (l2=1; l2<=(nlstate+ndeath);l2++){
3953: if(l2==k2) continue;
3954: j=(k2-1)*(nlstate+ndeath)+l2;
3955: for (k1=1; k1<=(nlstate);k1++){
3956: for (l1=1; l1<=(nlstate+ndeath);l1++){
3957: if(l1==k1) continue;
3958: i=(k1-1)*(nlstate+ndeath)+l1;
3959: if(i<=j) continue;
3960: for (age=bage; age<=fage; age ++){
3961: if ((int)age %5==0){
3962: v1=varpij[i][i][(int)age]/stepm*YEARM/stepm*YEARM;
3963: v2=varpij[j][j][(int)age]/stepm*YEARM/stepm*YEARM;
3964: cv12=varpij[i][j][(int)age]/stepm*YEARM/stepm*YEARM;
3965: mu1=mu[i][(int) age]/stepm*YEARM ;
3966: mu2=mu[j][(int) age]/stepm*YEARM;
3967: c12=cv12/sqrt(v1*v2);
3968: /* Computing eigen value of matrix of covariance */
3969: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
3970: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
1.135 brouard 3971: if ((lc2 <0) || (lc1 <0) ){
1.145 brouard 3972: if(first2==1){
3973: first1=0;
3974: 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);
3975: }
3976: 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);
3977: /* lc1=fabs(lc1); */ /* If we want to have them positive */
3978: /* lc2=fabs(lc2); */
1.135 brouard 3979: }
3980:
1.126 brouard 3981: /* Eigen vectors */
3982: v11=(1./sqrt(1+(v1-lc1)*(v1-lc1)/cv12/cv12));
3983: /*v21=sqrt(1.-v11*v11); *//* error */
3984: v21=(lc1-v1)/cv12*v11;
3985: v12=-v21;
3986: v22=v11;
3987: tnalp=v21/v11;
3988: if(first1==1){
3989: first1=0;
3990: 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);
3991: }
3992: 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);
3993: /*printf(fignu*/
3994: /* mu1+ v11*lc1*cost + v12*lc2*sin(t) */
3995: /* mu2+ v21*lc1*cost + v22*lc2*sin(t) */
3996: if(first==1){
3997: first=0;
3998: fprintf(ficgp,"\nset parametric;unset label");
3999: 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);
1.145 brouard 4000: fprintf(ficgp,"\nset ter png small size 320, 240");
1.126 brouard 4001: fprintf(fichtmcov,"\n<br>Ellipsoids of confidence cov(p%1d%1d,p%1d%1d) expressed in year<sup>-1</sup>\
4002: :<a href=\"%s%d%1d%1d-%1d%1d.png\">\
4003: %s%d%1d%1d-%1d%1d.png</A>, ",k1,l1,k2,l2,\
4004: subdirf2(optionfilefiname,"varpijgr"), j1,k1,l1,k2,l2,\
4005: subdirf2(optionfilefiname,"varpijgr"), j1,k1,l1,k2,l2);
4006: fprintf(fichtmcov,"\n<br><img src=\"%s%d%1d%1d-%1d%1d.png\"> ",subdirf2(optionfilefiname,"varpijgr"), j1,k1,l1,k2,l2);
4007: fprintf(fichtmcov,"\n<br> Correlation at age %d (%.3f),",(int) age, c12);
4008: fprintf(ficgp,"\nset out \"%s%d%1d%1d-%1d%1d.png\"",subdirf2(optionfilefiname,"varpijgr"), j1,k1,l1,k2,l2);
4009: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
4010: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
4011: 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",\
4012: mu1,std,v11,sqrt(lc1),v12,sqrt(lc2),\
4013: mu2,std,v21,sqrt(lc1),v22,sqrt(lc2));
4014: }else{
4015: first=0;
4016: fprintf(fichtmcov," %d (%.3f),",(int) age, c12);
4017: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
4018: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
4019: 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",\
4020: mu1,std,v11,sqrt(lc1),v12,sqrt(lc2),\
4021: mu2,std,v21,sqrt(lc1),v22,sqrt(lc2));
4022: }/* if first */
4023: } /* age mod 5 */
4024: } /* end loop age */
4025: fprintf(ficgp,"\nset out \"%s%d%1d%1d-%1d%1d.png\";replot;",subdirf2(optionfilefiname,"varpijgr"), j1,k1,l1,k2,l2);
4026: first=1;
4027: } /*l12 */
4028: } /* k12 */
4029: } /*l1 */
4030: }/* k1 */
1.145 brouard 4031: /* } /* loop covariates */
1.126 brouard 4032: }
4033: free_ma3x(varpij,1,nlstate,1,nlstate+ndeath,(int) bage, (int)fage);
4034: free_matrix(mu,1,(nlstate+ndeath)*(nlstate+ndeath),(int) bage, (int)fage);
4035: free_matrix(doldm,1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
4036: free_matrix(dnewm,1,(nlstate)*(nlstate+ndeath),1,npar);
4037: free_vector(xp,1,npar);
4038: fclose(ficresprob);
4039: fclose(ficresprobcov);
4040: fclose(ficresprobcor);
4041: fflush(ficgp);
4042: fflush(fichtmcov);
4043: }
4044:
4045:
4046: /******************* Printing html file ***********/
4047: void printinghtml(char fileres[], char title[], char datafile[], int firstpass, \
4048: int lastpass, int stepm, int weightopt, char model[],\
4049: int imx,int jmin, int jmax, double jmeanint,char rfileres[],\
4050: int popforecast, int estepm ,\
4051: double jprev1, double mprev1,double anprev1, \
4052: double jprev2, double mprev2,double anprev2){
4053: int jj1, k1, i1, cpt;
4054:
4055: fprintf(fichtm,"<ul><li><a href='#firstorder'>Result files (first order: no variance)</a>\n \
4056: <li><a href='#secondorder'>Result files (second order (variance)</a>\n \
4057: </ul>");
4058: fprintf(fichtm,"<ul><li><h4><a name='firstorder'>Result files (first order: no variance)</a></h4>\n \
4059: - Observed prevalence in each state (during the period defined between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf): <a href=\"%s\">%s</a> <br>\n ",
4060: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirf2(fileres,"p"),subdirf2(fileres,"p"));
4061: fprintf(fichtm,"\
4062: - Estimated transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
4063: stepm,subdirf2(fileres,"pij"),subdirf2(fileres,"pij"));
4064: fprintf(fichtm,"\
4065: - Period (stable) prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
4066: subdirf2(fileres,"pl"),subdirf2(fileres,"pl"));
4067: fprintf(fichtm,"\
1.128 brouard 4068: - (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): \
1.126 brouard 4069: <a href=\"%s\">%s</a> <br>\n",
4070: estepm,subdirf2(fileres,"e"),subdirf2(fileres,"e"));
4071: fprintf(fichtm,"\
4072: - Population projections by age and states: \
4073: <a href=\"%s\">%s</a> <br>\n</li>", subdirf2(fileres,"f"),subdirf2(fileres,"f"));
4074:
4075: fprintf(fichtm," \n<ul><li><b>Graphs</b></li><p>");
4076:
1.145 brouard 4077: m=pow(2,cptcoveff);
1.126 brouard 4078: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
4079:
4080: jj1=0;
4081: for(k1=1; k1<=m;k1++){
4082: for(i1=1; i1<=ncodemax[k1];i1++){
4083: jj1++;
4084: if (cptcovn > 0) {
4085: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
4086: for (cpt=1; cpt<=cptcoveff;cpt++)
4087: fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtab[jj1][cpt]]);
4088: fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
4089: }
4090: /* Pij */
1.145 brouard 4091: 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> \
4092: <img src=\"%s%d_1.png\">",stepm,subdirf2(optionfilefiname,"pe"),jj1,subdirf2(optionfilefiname,"pe"),jj1,subdirf2(optionfilefiname,"pe"),jj1);
1.126 brouard 4093: /* Quasi-incidences */
4094: fprintf(fichtm,"<br>- Pij or Conditional probabilities to be observed in state j being in state i %d (stepm) months\
1.145 brouard 4095: 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> \
4096: <img src=\"%s%d_2.png\">",stepm,subdirf2(optionfilefiname,"pe"),jj1,subdirf2(optionfilefiname,"pe"),jj1,subdirf2(optionfilefiname,"pe"),jj1);
1.126 brouard 4097: /* Period (stable) prevalence in each health state */
1.154 brouard 4098: for(cpt=1; cpt<=nlstate;cpt++){
1.166 ! brouard 4099: fprintf(fichtm,"<br>- Convergence to period (stable) prevalence in state %d. Or probability to be in state %d being in state (1 to %d) at different ages. <a href=\"%s%d_%d.png\">%s%d_%d.png</a><br> \
! 4100: <img src=\"%s%d_%d.png\">", cpt, cpt, nlstate, subdirf2(optionfilefiname,"p"),cpt,jj1,subdirf2(optionfilefiname,"p"),cpt,jj1,subdirf2(optionfilefiname,"p"),cpt,jj1);
1.126 brouard 4101: }
4102: for(cpt=1; cpt<=nlstate;cpt++) {
1.154 brouard 4103: 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> \
4104: <img src=\"%s%d%d.png\">",cpt,nlstate,subdirf2(optionfilefiname,"exp"),cpt,jj1,subdirf2(optionfilefiname,"exp"),cpt,jj1,subdirf2(optionfilefiname,"exp"),cpt,jj1);
1.126 brouard 4105: }
4106: } /* end i1 */
4107: }/* End k1 */
4108: fprintf(fichtm,"</ul>");
4109:
4110:
4111: fprintf(fichtm,"\
4112: \n<br><li><h4> <a name='secondorder'>Result files (second order: variances)</a></h4>\n\
4113: - Parameter file with estimated parameters and covariance matrix: <a href=\"%s\">%s</a> <br>\n", rfileres,rfileres);
4114:
4115: fprintf(fichtm," - Variance of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
4116: subdirf2(fileres,"prob"),subdirf2(fileres,"prob"));
4117: fprintf(fichtm,"\
4118: - Variance-covariance of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
4119: subdirf2(fileres,"probcov"),subdirf2(fileres,"probcov"));
4120:
4121: fprintf(fichtm,"\
4122: - Correlation matrix of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
4123: subdirf2(fileres,"probcor"),subdirf2(fileres,"probcor"));
4124: fprintf(fichtm,"\
4125: - 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): \
4126: <a href=\"%s\">%s</a> <br>\n</li>",
4127: estepm,subdirf2(fileres,"cve"),subdirf2(fileres,"cve"));
4128: fprintf(fichtm,"\
4129: - (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): \
4130: <a href=\"%s\">%s</a> <br>\n</li>",
4131: estepm,subdirf2(fileres,"stde"),subdirf2(fileres,"stde"));
4132: fprintf(fichtm,"\
1.128 brouard 4133: - 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",
1.126 brouard 4134: estepm, subdirf2(fileres,"v"),subdirf2(fileres,"v"));
4135: fprintf(fichtm,"\
1.128 brouard 4136: - 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",
4137: estepm, subdirf2(fileres,"t"),subdirf2(fileres,"t"));
1.126 brouard 4138: fprintf(fichtm,"\
4139: - Standard deviation of period (stable) prevalences: <a href=\"%s\">%s</a> <br>\n",\
4140: subdirf2(fileres,"vpl"),subdirf2(fileres,"vpl"));
4141:
4142: /* if(popforecast==1) fprintf(fichtm,"\n */
4143: /* - Prevalences forecasting: <a href=\"f%s\">f%s</a> <br>\n */
4144: /* - Population forecasting (if popforecast=1): <a href=\"pop%s\">pop%s</a> <br>\n */
4145: /* <br>",fileres,fileres,fileres,fileres); */
4146: /* else */
4147: /* 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); */
4148: fflush(fichtm);
4149: fprintf(fichtm," <ul><li><b>Graphs</b></li><p>");
4150:
1.145 brouard 4151: m=pow(2,cptcoveff);
1.126 brouard 4152: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
4153:
4154: jj1=0;
4155: for(k1=1; k1<=m;k1++){
4156: for(i1=1; i1<=ncodemax[k1];i1++){
4157: jj1++;
4158: if (cptcovn > 0) {
4159: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
4160: for (cpt=1; cpt<=cptcoveff;cpt++)
4161: fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtab[jj1][cpt]]);
4162: fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
4163: }
4164: for(cpt=1; cpt<=nlstate;cpt++) {
4165: fprintf(fichtm,"<br>- Observed (cross-sectional) and period (incidence based) \
1.145 brouard 4166: prevalence (with 95%% confidence interval) in state (%d): %s%d_%d.png <br>\
4167: <img src=\"%s%d_%d.png\">",cpt,subdirf2(optionfilefiname,"v"),cpt,jj1,subdirf2(optionfilefiname,"v"),cpt,jj1);
1.126 brouard 4168: }
4169: fprintf(fichtm,"\n<br>- Total life expectancy by age and \
1.128 brouard 4170: health expectancies in states (1) and (2). If popbased=1 the smooth (due to the model) \
4171: true period expectancies (those weighted with period prevalences are also\
4172: drawn in addition to the population based expectancies computed using\
4173: observed and cahotic prevalences: %s%d.png<br>\
1.126 brouard 4174: <img src=\"%s%d.png\">",subdirf2(optionfilefiname,"e"),jj1,subdirf2(optionfilefiname,"e"),jj1);
4175: } /* end i1 */
4176: }/* End k1 */
4177: fprintf(fichtm,"</ul>");
4178: fflush(fichtm);
4179: }
4180:
4181: /******************* Gnuplot file **************/
4182: void printinggnuplot(char fileres[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){
4183:
4184: char dirfileres[132],optfileres[132];
1.164 brouard 4185: int cpt=0,k1=0,i=0,k=0,j=0,jk=0,k2=0,k3=0,ij=0,l=0;
1.130 brouard 4186: int ng=0;
1.126 brouard 4187: /* if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) { */
4188: /* printf("Problem with file %s",optionfilegnuplot); */
4189: /* fprintf(ficlog,"Problem with file %s",optionfilegnuplot); */
4190: /* } */
4191:
4192: /*#ifdef windows */
4193: fprintf(ficgp,"cd \"%s\" \n",pathc);
4194: /*#endif */
4195: m=pow(2,cptcoveff);
4196:
4197: strcpy(dirfileres,optionfilefiname);
4198: strcpy(optfileres,"vpl");
4199: /* 1eme*/
1.153 brouard 4200: fprintf(ficgp,"\n# 1st: Period (stable) prevalence with CI: 'vpl' files\n");
1.126 brouard 4201: for (cpt=1; cpt<= nlstate ; cpt ++) {
1.145 brouard 4202: for (k1=1; k1<= m ; k1 ++) { /* plot [100000000000000000000:-100000000000000000000] "mysbiaspar/vplrmysbiaspar.txt to check */
4203: fprintf(ficgp,"\nset out \"%s%d_%d.png\" \n",subdirf2(optionfilefiname,"v"),cpt,k1);
4204: fprintf(ficgp,"\n#set out \"v%s%d_%d.png\" \n",optionfilefiname,cpt,k1);
1.126 brouard 4205: fprintf(ficgp,"set xlabel \"Age\" \n\
4206: set ylabel \"Probability\" \n\
1.145 brouard 4207: set ter png small size 320, 240\n\
1.126 brouard 4208: plot [%.f:%.f] \"%s\" every :::%d::%d u 1:2 \"\%%lf",ageminpar,fage,subdirf2(fileres,"vpl"),k1-1,k1-1);
4209:
4210: for (i=1; i<= nlstate ; i ++) {
4211: if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)");
1.131 brouard 4212: else fprintf(ficgp," \%%*lf (\%%*lf)");
1.126 brouard 4213: }
1.145 brouard 4214: 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);
1.126 brouard 4215: for (i=1; i<= nlstate ; i ++) {
4216: if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)");
4217: else fprintf(ficgp," \%%*lf (\%%*lf)");
4218: }
1.145 brouard 4219: 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);
1.126 brouard 4220: for (i=1; i<= nlstate ; i ++) {
4221: if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)");
4222: else fprintf(ficgp," \%%*lf (\%%*lf)");
4223: }
1.145 brouard 4224: 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));
1.126 brouard 4225: }
4226: }
4227: /*2 eme*/
1.153 brouard 4228: fprintf(ficgp,"\n# 2nd: Total life expectancy with CI: 't' files\n");
1.126 brouard 4229: for (k1=1; k1<= m ; k1 ++) {
4230: fprintf(ficgp,"\nset out \"%s%d.png\" \n",subdirf2(optionfilefiname,"e"),k1);
1.145 brouard 4231: fprintf(ficgp,"set ylabel \"Years\" \nset ter png small size 320, 240\nplot [%.f:%.f] ",ageminpar,fage);
1.126 brouard 4232:
4233: for (i=1; i<= nlstate+1 ; i ++) {
4234: k=2*i;
4235: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:2 \"\%%lf",subdirf2(fileres,"t"),k1-1,k1-1);
4236: for (j=1; j<= nlstate+1 ; j ++) {
4237: if (j==i) fprintf(ficgp," \%%lf (\%%lf)");
4238: else fprintf(ficgp," \%%*lf (\%%*lf)");
4239: }
4240: if (i== 1) fprintf(ficgp,"\" t\"TLE\" w l ,");
4241: else fprintf(ficgp,"\" t\"LE in state (%d)\" w l ,",i-1);
4242: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2-$3*2) \"\%%lf",subdirf2(fileres,"t"),k1-1,k1-1);
4243: for (j=1; j<= nlstate+1 ; j ++) {
4244: if (j==i) fprintf(ficgp," \%%lf (\%%lf)");
4245: else fprintf(ficgp," \%%*lf (\%%*lf)");
4246: }
1.145 brouard 4247: fprintf(ficgp,"\" t\"\" w l lt 0,");
1.126 brouard 4248: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2+$3*2) \"\%%lf",subdirf2(fileres,"t"),k1-1,k1-1);
4249: for (j=1; j<= nlstate+1 ; j ++) {
4250: if (j==i) fprintf(ficgp," \%%lf (\%%lf)");
4251: else fprintf(ficgp," \%%*lf (\%%*lf)");
4252: }
1.145 brouard 4253: if (i== (nlstate+1)) fprintf(ficgp,"\" t\"\" w l lt 0");
4254: else fprintf(ficgp,"\" t\"\" w l lt 0,");
1.126 brouard 4255: }
4256: }
4257:
4258: /*3eme*/
4259:
4260: for (k1=1; k1<= m ; k1 ++) {
4261: for (cpt=1; cpt<= nlstate ; cpt ++) {
4262: /* k=2+nlstate*(2*cpt-2); */
4263: k=2+(nlstate+1)*(cpt-1);
4264: fprintf(ficgp,"\nset out \"%s%d%d.png\" \n",subdirf2(optionfilefiname,"exp"),cpt,k1);
1.145 brouard 4265: fprintf(ficgp,"set ter png small size 320, 240\n\
1.126 brouard 4266: 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);
4267: /*fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d-2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
4268: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
4269: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
4270: fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d+2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
4271: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
4272: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
4273:
4274: */
4275: for (i=1; i< nlstate ; i ++) {
4276: 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);
4277: /* 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);*/
4278:
4279: }
4280: fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d.\" w l",subdirf2(fileres,"e"),k1-1,k1-1,k+nlstate,cpt);
4281: }
4282: }
4283:
4284: /* CV preval stable (period) */
1.153 brouard 4285: for (k1=1; k1<= m ; k1 ++) { /* For each multivariate if any */
4286: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.126 brouard 4287: k=3;
1.153 brouard 4288: fprintf(ficgp,"\n#\n#\n#CV preval stable (period): 'pij' files, cov=%d state=%d",k1, cpt);
1.145 brouard 4289: fprintf(ficgp,"\nset out \"%s%d_%d.png\" \n",subdirf2(optionfilefiname,"p"),cpt,k1);
1.126 brouard 4290: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
1.145 brouard 4291: set ter png small size 320, 240\n\
1.126 brouard 4292: unset log y\n\
1.153 brouard 4293: plot [%.f:%.f] ", ageminpar, agemaxpar);
4294: for (i=1; i<= nlstate ; i ++){
4295: if(i==1)
4296: fprintf(ficgp,"\"%s\"",subdirf2(fileres,"pij"));
4297: else
4298: fprintf(ficgp,", '' ");
1.154 brouard 4299: l=(nlstate+ndeath)*(i-1)+1;
4300: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
1.153 brouard 4301: for (j=1; j<= (nlstate-1) ; j ++)
4302: fprintf(ficgp,"+$%d",k+l+j);
4303: fprintf(ficgp,")) t \"prev(%d,%d)\" w l",i,cpt);
4304: } /* nlstate */
4305: fprintf(ficgp,"\n");
4306: } /* end cpt state*/
4307: } /* end covariate */
1.126 brouard 4308:
4309: /* proba elementaires */
4310: for(i=1,jk=1; i <=nlstate; i++){
4311: for(k=1; k <=(nlstate+ndeath); k++){
4312: if (k != i) {
4313: for(j=1; j <=ncovmodel; j++){
4314: fprintf(ficgp,"p%d=%f ",jk,p[jk]);
4315: jk++;
4316: fprintf(ficgp,"\n");
4317: }
4318: }
4319: }
4320: }
1.145 brouard 4321: /*goto avoid;*/
1.126 brouard 4322: for(ng=1; ng<=2;ng++){ /* Number of graphics: first is probabilities second is incidence per year*/
4323: for(jk=1; jk <=m; jk++) {
1.145 brouard 4324: fprintf(ficgp,"\nset out \"%s%d_%d.png\" \n",subdirf2(optionfilefiname,"pe"),jk,ng);
1.126 brouard 4325: if (ng==2)
4326: fprintf(ficgp,"\nset ylabel \"Quasi-incidence per year\"\n");
4327: else
4328: fprintf(ficgp,"\nset title \"Probability\"\n");
1.145 brouard 4329: fprintf(ficgp,"\nset ter png small size 320, 240\nset log y\nplot [%.f:%.f] ",ageminpar,agemaxpar);
1.126 brouard 4330: i=1;
4331: for(k2=1; k2<=nlstate; k2++) {
4332: k3=i;
4333: for(k=1; k<=(nlstate+ndeath); k++) {
4334: if (k != k2){
4335: if(ng==2)
4336: fprintf(ficgp," %f*exp(p%d+p%d*x",YEARM/stepm,i,i+1);
4337: else
4338: fprintf(ficgp," exp(p%d+p%d*x",i,i+1);
1.141 brouard 4339: ij=1;/* To be checked else nbcode[0][0] wrong */
1.126 brouard 4340: for(j=3; j <=ncovmodel; j++) {
1.145 brouard 4341: /* if(((j-2)==Tage[ij]) &&(ij <=cptcovage)) { /\* Bug valgrind *\/ */
4342: /* /\*fprintf(ficgp,"+p%d*%d*x",i+j-1,nbcode[Tvar[j-2]][codtab[jk][Tvar[j-2]]]);*\/ */
4343: /* ij++; */
4344: /* } */
4345: /* else */
1.126 brouard 4346: fprintf(ficgp,"+p%d*%d",i+j-1,nbcode[Tvar[j-2]][codtab[jk][j-2]]);
4347: }
4348: fprintf(ficgp,")/(1");
4349:
4350: for(k1=1; k1 <=nlstate; k1++){
4351: fprintf(ficgp,"+exp(p%d+p%d*x",k3+(k1-1)*ncovmodel,k3+(k1-1)*ncovmodel+1);
4352: ij=1;
4353: for(j=3; j <=ncovmodel; j++){
1.145 brouard 4354: /* if(((j-2)==Tage[ij]) &&(ij <=cptcovage)) { */
4355: /* fprintf(ficgp,"+p%d*%d*x",k3+(k1-1)*ncovmodel+1+j-2,nbcode[Tvar[j-2]][codtab[jk][Tvar[j-2]]]); */
4356: /* ij++; */
4357: /* } */
4358: /* else */
1.126 brouard 4359: fprintf(ficgp,"+p%d*%d",k3+(k1-1)*ncovmodel+1+j-2,nbcode[Tvar[j-2]][codtab[jk][j-2]]);
4360: }
4361: fprintf(ficgp,")");
4362: }
4363: fprintf(ficgp,") t \"p%d%d\" ", k2,k);
4364: if ((k+k2)!= (nlstate*2+ndeath)) fprintf(ficgp,",");
4365: i=i+ncovmodel;
4366: }
4367: } /* end k */
4368: } /* end k2 */
4369: } /* end jk */
4370: } /* end ng */
1.164 brouard 4371: /* avoid: */
1.126 brouard 4372: fflush(ficgp);
4373: } /* end gnuplot */
4374:
4375:
4376: /*************** Moving average **************/
4377: int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav){
4378:
4379: int i, cpt, cptcod;
4380: int modcovmax =1;
4381: int mobilavrange, mob;
4382: double age;
4383:
4384: modcovmax=2*cptcoveff;/* Max number of modalities. We suppose
4385: a covariate has 2 modalities */
4386: if (cptcovn<1) modcovmax=1; /* At least 1 pass */
4387:
4388: if(mobilav==1||mobilav ==3 ||mobilav==5 ||mobilav== 7){
4389: if(mobilav==1) mobilavrange=5; /* default */
4390: else mobilavrange=mobilav;
4391: for (age=bage; age<=fage; age++)
4392: for (i=1; i<=nlstate;i++)
4393: for (cptcod=1;cptcod<=modcovmax;cptcod++)
4394: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
4395: /* We keep the original values on the extreme ages bage, fage and for
4396: fage+1 and bage-1 we use a 3 terms moving average; for fage+2 bage+2
4397: we use a 5 terms etc. until the borders are no more concerned.
4398: */
4399: for (mob=3;mob <=mobilavrange;mob=mob+2){
4400: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){
4401: for (i=1; i<=nlstate;i++){
4402: for (cptcod=1;cptcod<=modcovmax;cptcod++){
4403: mobaverage[(int)age][i][cptcod] =probs[(int)age][i][cptcod];
4404: for (cpt=1;cpt<=(mob-1)/2;cpt++){
4405: mobaverage[(int)age][i][cptcod] +=probs[(int)age-cpt][i][cptcod];
4406: mobaverage[(int)age][i][cptcod] +=probs[(int)age+cpt][i][cptcod];
4407: }
4408: mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/mob;
4409: }
4410: }
4411: }/* end age */
4412: }/* end mob */
4413: }else return -1;
4414: return 0;
4415: }/* End movingaverage */
4416:
4417:
4418: /************** Forecasting ******************/
4419: 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){
4420: /* proj1, year, month, day of starting projection
4421: agemin, agemax range of age
4422: dateprev1 dateprev2 range of dates during which prevalence is computed
4423: anproj2 year of en of projection (same day and month as proj1).
4424: */
1.164 brouard 4425: int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1;
1.126 brouard 4426: double agec; /* generic age */
4427: double agelim, ppij, yp,yp1,yp2,jprojmean,mprojmean,anprojmean;
4428: double *popeffectif,*popcount;
4429: double ***p3mat;
4430: double ***mobaverage;
4431: char fileresf[FILENAMELENGTH];
4432:
4433: agelim=AGESUP;
4434: prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
4435:
4436: strcpy(fileresf,"f");
4437: strcat(fileresf,fileres);
4438: if((ficresf=fopen(fileresf,"w"))==NULL) {
4439: printf("Problem with forecast resultfile: %s\n", fileresf);
4440: fprintf(ficlog,"Problem with forecast resultfile: %s\n", fileresf);
4441: }
4442: printf("Computing forecasting: result on file '%s' \n", fileresf);
4443: fprintf(ficlog,"Computing forecasting: result on file '%s' \n", fileresf);
4444:
4445: if (cptcoveff==0) ncodemax[cptcoveff]=1;
4446:
4447: if (mobilav!=0) {
4448: mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
4449: if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){
4450: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
4451: printf(" Error in movingaverage mobilav=%d\n",mobilav);
4452: }
4453: }
4454:
4455: stepsize=(int) (stepm+YEARM-1)/YEARM;
4456: if (stepm<=12) stepsize=1;
4457: if(estepm < stepm){
4458: printf ("Problem %d lower than %d\n",estepm, stepm);
4459: }
4460: else hstepm=estepm;
4461:
4462: hstepm=hstepm/stepm;
4463: yp1=modf(dateintmean,&yp);/* extracts integral of datemean in yp and
4464: fractional in yp1 */
4465: anprojmean=yp;
4466: yp2=modf((yp1*12),&yp);
4467: mprojmean=yp;
4468: yp1=modf((yp2*30.5),&yp);
4469: jprojmean=yp;
4470: if(jprojmean==0) jprojmean=1;
4471: if(mprojmean==0) jprojmean=1;
4472:
4473: i1=cptcoveff;
4474: if (cptcovn < 1){i1=1;}
4475:
4476: fprintf(ficresf,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jprojmean,mprojmean,anprojmean,dateintmean,dateprev1,dateprev2);
4477:
4478: fprintf(ficresf,"#****** Routine prevforecast **\n");
4479:
4480: /* if (h==(int)(YEARM*yearp)){ */
4481: for(cptcov=1, k=0;cptcov<=i1;cptcov++){
4482: for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){
4483: k=k+1;
4484: fprintf(ficresf,"\n#******");
4485: for(j=1;j<=cptcoveff;j++) {
4486: 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]]);
4487: }
4488: fprintf(ficresf,"******\n");
4489: fprintf(ficresf,"# Covariate valuofcovar yearproj age");
4490: for(j=1; j<=nlstate+ndeath;j++){
4491: for(i=1; i<=nlstate;i++)
4492: fprintf(ficresf," p%d%d",i,j);
4493: fprintf(ficresf," p.%d",j);
4494: }
4495: for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) {
4496: fprintf(ficresf,"\n");
4497: fprintf(ficresf,"\n# Forecasting at date %.lf/%.lf/%.lf ",jproj1,mproj1,anproj1+yearp);
4498:
4499: for (agec=fage; agec>=(ageminpar-1); agec--){
4500: nhstepm=(int) rint((agelim-agec)*YEARM/stepm);
4501: nhstepm = nhstepm/hstepm;
4502: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4503: oldm=oldms;savm=savms;
4504: hpxij(p3mat,nhstepm,agec,hstepm,p,nlstate,stepm,oldm,savm, k);
4505:
4506: for (h=0; h<=nhstepm; h++){
4507: if (h*hstepm/YEARM*stepm ==yearp) {
4508: fprintf(ficresf,"\n");
4509: for(j=1;j<=cptcoveff;j++)
4510: fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
4511: fprintf(ficresf,"%.f %.f ",anproj1+yearp,agec+h*hstepm/YEARM*stepm);
4512: }
4513: for(j=1; j<=nlstate+ndeath;j++) {
4514: ppij=0.;
4515: for(i=1; i<=nlstate;i++) {
4516: if (mobilav==1)
4517: ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][i][cptcod];
4518: else {
4519: ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][cptcod];
4520: }
4521: if (h*hstepm/YEARM*stepm== yearp) {
4522: fprintf(ficresf," %.3f", p3mat[i][j][h]);
4523: }
4524: } /* end i */
4525: if (h*hstepm/YEARM*stepm==yearp) {
4526: fprintf(ficresf," %.3f", ppij);
4527: }
4528: }/* end j */
4529: } /* end h */
4530: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4531: } /* end agec */
4532: } /* end yearp */
4533: } /* end cptcod */
4534: } /* end cptcov */
4535:
4536: if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
4537:
4538: fclose(ficresf);
4539: }
4540:
4541: /************** Forecasting *****not tested NB*************/
4542: 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){
4543:
4544: int cpt, stepsize, hstepm, nhstepm, j,k,c, cptcod, i,h;
4545: int *popage;
4546: double calagedatem, agelim, kk1, kk2;
4547: double *popeffectif,*popcount;
4548: double ***p3mat,***tabpop,***tabpopprev;
4549: double ***mobaverage;
4550: char filerespop[FILENAMELENGTH];
4551:
4552: tabpop= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
4553: tabpopprev= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
4554: agelim=AGESUP;
4555: calagedatem=(anpyram+mpyram/12.+jpyram/365.-dateintmean)*YEARM;
4556:
4557: prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
4558:
4559:
4560: strcpy(filerespop,"pop");
4561: strcat(filerespop,fileres);
4562: if((ficrespop=fopen(filerespop,"w"))==NULL) {
4563: printf("Problem with forecast resultfile: %s\n", filerespop);
4564: fprintf(ficlog,"Problem with forecast resultfile: %s\n", filerespop);
4565: }
4566: printf("Computing forecasting: result on file '%s' \n", filerespop);
4567: fprintf(ficlog,"Computing forecasting: result on file '%s' \n", filerespop);
4568:
4569: if (cptcoveff==0) ncodemax[cptcoveff]=1;
4570:
4571: if (mobilav!=0) {
4572: mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
4573: if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){
4574: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
4575: printf(" Error in movingaverage mobilav=%d\n",mobilav);
4576: }
4577: }
4578:
4579: stepsize=(int) (stepm+YEARM-1)/YEARM;
4580: if (stepm<=12) stepsize=1;
4581:
4582: agelim=AGESUP;
4583:
4584: hstepm=1;
4585: hstepm=hstepm/stepm;
4586:
4587: if (popforecast==1) {
4588: if((ficpop=fopen(popfile,"r"))==NULL) {
4589: printf("Problem with population file : %s\n",popfile);exit(0);
4590: fprintf(ficlog,"Problem with population file : %s\n",popfile);exit(0);
4591: }
4592: popage=ivector(0,AGESUP);
4593: popeffectif=vector(0,AGESUP);
4594: popcount=vector(0,AGESUP);
4595:
4596: i=1;
4597: while ((c=fscanf(ficpop,"%d %lf\n",&popage[i],&popcount[i])) != EOF) i=i+1;
4598:
4599: imx=i;
4600: for (i=1; i<imx;i++) popeffectif[popage[i]]=popcount[i];
4601: }
4602:
4603: for(cptcov=1,k=0;cptcov<=i2;cptcov++){
4604: for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){
4605: k=k+1;
4606: fprintf(ficrespop,"\n#******");
4607: for(j=1;j<=cptcoveff;j++) {
4608: fprintf(ficrespop," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
4609: }
4610: fprintf(ficrespop,"******\n");
4611: fprintf(ficrespop,"# Age");
4612: for(j=1; j<=nlstate+ndeath;j++) fprintf(ficrespop," P.%d",j);
4613: if (popforecast==1) fprintf(ficrespop," [Population]");
4614:
4615: for (cpt=0; cpt<=0;cpt++) {
4616: fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt);
4617:
4618: for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){
4619: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm);
4620: nhstepm = nhstepm/hstepm;
4621:
4622: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4623: oldm=oldms;savm=savms;
4624: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
4625:
4626: for (h=0; h<=nhstepm; h++){
4627: if (h==(int) (calagedatem+YEARM*cpt)) {
4628: fprintf(ficrespop,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm);
4629: }
4630: for(j=1; j<=nlstate+ndeath;j++) {
4631: kk1=0.;kk2=0;
4632: for(i=1; i<=nlstate;i++) {
4633: if (mobilav==1)
4634: kk1=kk1+p3mat[i][j][h]*mobaverage[(int)agedeb+1][i][cptcod];
4635: else {
4636: kk1=kk1+p3mat[i][j][h]*probs[(int)(agedeb+1)][i][cptcod];
4637: }
4638: }
4639: if (h==(int)(calagedatem+12*cpt)){
4640: tabpop[(int)(agedeb)][j][cptcod]=kk1;
4641: /*fprintf(ficrespop," %.3f", kk1);
4642: if (popforecast==1) fprintf(ficrespop," [%.f]", kk1*popeffectif[(int)agedeb+1]);*/
4643: }
4644: }
4645: for(i=1; i<=nlstate;i++){
4646: kk1=0.;
4647: for(j=1; j<=nlstate;j++){
4648: kk1= kk1+tabpop[(int)(agedeb)][j][cptcod];
4649: }
4650: tabpopprev[(int)(agedeb)][i][cptcod]=tabpop[(int)(agedeb)][i][cptcod]/kk1*popeffectif[(int)(agedeb+(calagedatem+12*cpt)*hstepm/YEARM*stepm-1)];
4651: }
4652:
4653: if (h==(int)(calagedatem+12*cpt)) for(j=1; j<=nlstate;j++)
4654: fprintf(ficrespop," %15.2f",tabpopprev[(int)(agedeb+1)][j][cptcod]);
4655: }
4656: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4657: }
4658: }
4659:
4660: /******/
4661:
4662: for (cpt=1; cpt<=(anpyram1-anpyram);cpt++) {
4663: fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt);
4664: for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){
4665: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm);
4666: nhstepm = nhstepm/hstepm;
4667:
4668: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4669: oldm=oldms;savm=savms;
4670: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
4671: for (h=0; h<=nhstepm; h++){
4672: if (h==(int) (calagedatem+YEARM*cpt)) {
4673: fprintf(ficresf,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm);
4674: }
4675: for(j=1; j<=nlstate+ndeath;j++) {
4676: kk1=0.;kk2=0;
4677: for(i=1; i<=nlstate;i++) {
4678: kk1=kk1+p3mat[i][j][h]*tabpopprev[(int)agedeb+1][i][cptcod];
4679: }
4680: if (h==(int)(calagedatem+12*cpt)) fprintf(ficresf," %15.2f", kk1);
4681: }
4682: }
4683: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4684: }
4685: }
4686: }
4687: }
4688:
4689: if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
4690:
4691: if (popforecast==1) {
4692: free_ivector(popage,0,AGESUP);
4693: free_vector(popeffectif,0,AGESUP);
4694: free_vector(popcount,0,AGESUP);
4695: }
4696: free_ma3x(tabpop,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
4697: free_ma3x(tabpopprev,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
4698: fclose(ficrespop);
4699: } /* End of popforecast */
4700:
4701: int fileappend(FILE *fichier, char *optionfich)
4702: {
4703: if((fichier=fopen(optionfich,"a"))==NULL) {
4704: printf("Problem with file: %s\n", optionfich);
4705: fprintf(ficlog,"Problem with file: %s\n", optionfich);
4706: return (0);
4707: }
4708: fflush(fichier);
4709: return (1);
4710: }
4711:
4712:
4713: /**************** function prwizard **********************/
4714: void prwizard(int ncovmodel, int nlstate, int ndeath, char model[], FILE *ficparo)
4715: {
4716:
4717: /* Wizard to print covariance matrix template */
4718:
1.164 brouard 4719: char ca[32], cb[32];
4720: int i,j, k, li, lj, lk, ll, jj, npar, itimes;
1.126 brouard 4721: int numlinepar;
4722:
4723: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
4724: fprintf(ficparo,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
4725: for(i=1; i <=nlstate; i++){
4726: jj=0;
4727: for(j=1; j <=nlstate+ndeath; j++){
4728: if(j==i) continue;
4729: jj++;
4730: /*ca[0]= k+'a'-1;ca[1]='\0';*/
4731: printf("%1d%1d",i,j);
4732: fprintf(ficparo,"%1d%1d",i,j);
4733: for(k=1; k<=ncovmodel;k++){
4734: /* printf(" %lf",param[i][j][k]); */
4735: /* fprintf(ficparo," %lf",param[i][j][k]); */
4736: printf(" 0.");
4737: fprintf(ficparo," 0.");
4738: }
4739: printf("\n");
4740: fprintf(ficparo,"\n");
4741: }
4742: }
4743: printf("# Scales (for hessian or gradient estimation)\n");
4744: fprintf(ficparo,"# Scales (for hessian or gradient estimation)\n");
4745: npar= (nlstate+ndeath-1)*nlstate*ncovmodel; /* Number of parameters*/
4746: for(i=1; i <=nlstate; i++){
4747: jj=0;
4748: for(j=1; j <=nlstate+ndeath; j++){
4749: if(j==i) continue;
4750: jj++;
4751: fprintf(ficparo,"%1d%1d",i,j);
4752: printf("%1d%1d",i,j);
4753: fflush(stdout);
4754: for(k=1; k<=ncovmodel;k++){
4755: /* printf(" %le",delti3[i][j][k]); */
4756: /* fprintf(ficparo," %le",delti3[i][j][k]); */
4757: printf(" 0.");
4758: fprintf(ficparo," 0.");
4759: }
4760: numlinepar++;
4761: printf("\n");
4762: fprintf(ficparo,"\n");
4763: }
4764: }
4765: printf("# Covariance matrix\n");
4766: /* # 121 Var(a12)\n\ */
4767: /* # 122 Cov(b12,a12) Var(b12)\n\ */
4768: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
4769: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
4770: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
4771: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
4772: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
4773: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
4774: fflush(stdout);
4775: fprintf(ficparo,"# Covariance matrix\n");
4776: /* # 121 Var(a12)\n\ */
4777: /* # 122 Cov(b12,a12) Var(b12)\n\ */
4778: /* # ...\n\ */
4779: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
4780:
4781: for(itimes=1;itimes<=2;itimes++){
4782: jj=0;
4783: for(i=1; i <=nlstate; i++){
4784: for(j=1; j <=nlstate+ndeath; j++){
4785: if(j==i) continue;
4786: for(k=1; k<=ncovmodel;k++){
4787: jj++;
4788: ca[0]= k+'a'-1;ca[1]='\0';
4789: if(itimes==1){
4790: printf("#%1d%1d%d",i,j,k);
4791: fprintf(ficparo,"#%1d%1d%d",i,j,k);
4792: }else{
4793: printf("%1d%1d%d",i,j,k);
4794: fprintf(ficparo,"%1d%1d%d",i,j,k);
4795: /* printf(" %.5le",matcov[i][j]); */
4796: }
4797: ll=0;
4798: for(li=1;li <=nlstate; li++){
4799: for(lj=1;lj <=nlstate+ndeath; lj++){
4800: if(lj==li) continue;
4801: for(lk=1;lk<=ncovmodel;lk++){
4802: ll++;
4803: if(ll<=jj){
4804: cb[0]= lk +'a'-1;cb[1]='\0';
4805: if(ll<jj){
4806: if(itimes==1){
4807: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
4808: fprintf(ficparo," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
4809: }else{
4810: printf(" 0.");
4811: fprintf(ficparo," 0.");
4812: }
4813: }else{
4814: if(itimes==1){
4815: printf(" Var(%s%1d%1d)",ca,i,j);
4816: fprintf(ficparo," Var(%s%1d%1d)",ca,i,j);
4817: }else{
4818: printf(" 0.");
4819: fprintf(ficparo," 0.");
4820: }
4821: }
4822: }
4823: } /* end lk */
4824: } /* end lj */
4825: } /* end li */
4826: printf("\n");
4827: fprintf(ficparo,"\n");
4828: numlinepar++;
4829: } /* end k*/
4830: } /*end j */
4831: } /* end i */
4832: } /* end itimes */
4833:
4834: } /* end of prwizard */
4835: /******************* Gompertz Likelihood ******************************/
4836: double gompertz(double x[])
4837: {
4838: double A,B,L=0.0,sump=0.,num=0.;
4839: int i,n=0; /* n is the size of the sample */
4840:
4841: for (i=0;i<=imx-1 ; i++) {
4842: sump=sump+weight[i];
4843: /* sump=sump+1;*/
4844: num=num+1;
4845: }
4846:
4847:
4848: /* for (i=0; i<=imx; i++)
4849: 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]);*/
4850:
4851: for (i=1;i<=imx ; i++)
4852: {
4853: if (cens[i] == 1 && wav[i]>1)
4854: A=-x[1]/(x[2])*(exp(x[2]*(agecens[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)));
4855:
4856: if (cens[i] == 0 && wav[i]>1)
4857: A=-x[1]/(x[2])*(exp(x[2]*(agedc[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)))
4858: +log(x[1]/YEARM)+x[2]*(agedc[i]-agegomp)+log(YEARM);
4859:
4860: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
4861: if (wav[i] > 1 ) { /* ??? */
4862: L=L+A*weight[i];
4863: /* 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]);*/
4864: }
4865: }
4866:
4867: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
4868:
4869: return -2*L*num/sump;
4870: }
4871:
1.136 brouard 4872: #ifdef GSL
4873: /******************* Gompertz_f Likelihood ******************************/
4874: double gompertz_f(const gsl_vector *v, void *params)
4875: {
4876: double A,B,LL=0.0,sump=0.,num=0.;
4877: double *x= (double *) v->data;
4878: int i,n=0; /* n is the size of the sample */
4879:
4880: for (i=0;i<=imx-1 ; i++) {
4881: sump=sump+weight[i];
4882: /* sump=sump+1;*/
4883: num=num+1;
4884: }
4885:
4886:
4887: /* for (i=0; i<=imx; i++)
4888: 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]);*/
4889: printf("x[0]=%lf x[1]=%lf\n",x[0],x[1]);
4890: for (i=1;i<=imx ; i++)
4891: {
4892: if (cens[i] == 1 && wav[i]>1)
4893: A=-x[0]/(x[1])*(exp(x[1]*(agecens[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)));
4894:
4895: if (cens[i] == 0 && wav[i]>1)
4896: A=-x[0]/(x[1])*(exp(x[1]*(agedc[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)))
4897: +log(x[0]/YEARM)+x[1]*(agedc[i]-agegomp)+log(YEARM);
4898:
4899: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
4900: if (wav[i] > 1 ) { /* ??? */
4901: LL=LL+A*weight[i];
4902: /* 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]);*/
4903: }
4904: }
4905:
4906: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
4907: printf("x[0]=%lf x[1]=%lf -2*LL*num/sump=%lf\n",x[0],x[1],-2*LL*num/sump);
4908:
4909: return -2*LL*num/sump;
4910: }
4911: #endif
4912:
1.126 brouard 4913: /******************* Printing html file ***********/
4914: void printinghtmlmort(char fileres[], char title[], char datafile[], int firstpass, \
4915: int lastpass, int stepm, int weightopt, char model[],\
4916: int imx, double p[],double **matcov,double agemortsup){
4917: int i,k;
4918:
4919: fprintf(fichtm,"<ul><li><h4>Result files </h4>\n Force of mortality. Parameters of the Gompertz fit (with confidence interval in brackets):<br>");
4920: fprintf(fichtm," mu(age) =%lf*exp(%lf*(age-%d)) per year<br><br>",p[1],p[2],agegomp);
4921: for (i=1;i<=2;i++)
4922: 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]));
4923: fprintf(fichtm,"<br><br><img src=\"graphmort.png\">");
4924: fprintf(fichtm,"</ul>");
4925:
4926: fprintf(fichtm,"<ul><li><h4>Life table</h4>\n <br>");
4927:
4928: 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>");
4929:
4930: for (k=agegomp;k<(agemortsup-2);k++)
4931: 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]);
4932:
4933:
4934: fflush(fichtm);
4935: }
4936:
4937: /******************* Gnuplot file **************/
4938: void printinggnuplotmort(char fileres[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){
4939:
4940: char dirfileres[132],optfileres[132];
1.164 brouard 4941:
1.126 brouard 4942: int ng;
4943:
4944:
4945: /*#ifdef windows */
4946: fprintf(ficgp,"cd \"%s\" \n",pathc);
4947: /*#endif */
4948:
4949:
4950: strcpy(dirfileres,optionfilefiname);
4951: strcpy(optfileres,"vpl");
4952: fprintf(ficgp,"set out \"graphmort.png\"\n ");
4953: fprintf(ficgp,"set xlabel \"Age\"\n set ylabel \"Force of mortality (per year)\" \n ");
1.145 brouard 4954: fprintf(ficgp, "set ter png small size 320, 240\n set log y\n");
4955: /* fprintf(ficgp, "set size 0.65,0.65\n"); */
1.126 brouard 4956: fprintf(ficgp,"plot [%d:100] %lf*exp(%lf*(x-%d))",agegomp,p[1],p[2],agegomp);
4957:
4958: }
4959:
1.136 brouard 4960: int readdata(char datafile[], int firstobs, int lastobs, int *imax)
4961: {
1.126 brouard 4962:
1.136 brouard 4963: /*-------- data file ----------*/
4964: FILE *fic;
4965: char dummy[]=" ";
1.164 brouard 4966: int i=0, j=0, n=0;
1.136 brouard 4967: int linei, month, year,iout;
4968: char line[MAXLINE], linetmp[MAXLINE];
1.164 brouard 4969: char stra[MAXLINE], strb[MAXLINE];
1.136 brouard 4970: char *stratrunc;
4971: int lstra;
1.126 brouard 4972:
4973:
1.136 brouard 4974: if((fic=fopen(datafile,"r"))==NULL) {
4975: printf("Problem while opening datafile: %s\n", datafile);return 1;
4976: fprintf(ficlog,"Problem while opening datafile: %s\n", datafile);return 1;
4977: }
1.126 brouard 4978:
1.136 brouard 4979: i=1;
4980: linei=0;
4981: while ((fgets(line, MAXLINE, fic) != NULL) &&((i >= firstobs) && (i <=lastobs))) {
4982: linei=linei+1;
4983: for(j=strlen(line); j>=0;j--){ /* Untabifies line */
4984: if(line[j] == '\t')
4985: line[j] = ' ';
4986: }
4987: for(j=strlen(line)-1; (line[j]==' ')||(line[j]==10)||(line[j]==13);j--){
4988: ;
4989: };
4990: line[j+1]=0; /* Trims blanks at end of line */
4991: if(line[0]=='#'){
4992: fprintf(ficlog,"Comment line\n%s\n",line);
4993: printf("Comment line\n%s\n",line);
4994: continue;
4995: }
4996: trimbb(linetmp,line); /* Trims multiple blanks in line */
1.164 brouard 4997: strcpy(line, linetmp);
1.136 brouard 4998:
1.126 brouard 4999:
1.136 brouard 5000: for (j=maxwav;j>=1;j--){
1.137 brouard 5001: cutv(stra, strb, line, ' ');
1.136 brouard 5002: if(strb[0]=='.') { /* Missing status */
5003: lval=-1;
5004: }else{
5005: errno=0;
5006: lval=strtol(strb,&endptr,10);
5007: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
5008: if( strb[0]=='\0' || (*endptr != '\0')){
1.141 brouard 5009: 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);
5010: 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);
1.136 brouard 5011: return 1;
5012: }
5013: }
5014: s[j][i]=lval;
5015:
5016: strcpy(line,stra);
5017: cutv(stra, strb,line,' ');
5018: if(iout=sscanf(strb,"%d/%d",&month, &year) != 0){
5019: }
1.145 brouard 5020: else if(iout=sscanf(strb,"%s.",dummy) != 0){
1.136 brouard 5021: month=99;
5022: year=9999;
5023: }else{
1.141 brouard 5024: 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);
5025: 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);
1.136 brouard 5026: return 1;
5027: }
5028: anint[j][i]= (double) year;
5029: mint[j][i]= (double)month;
5030: strcpy(line,stra);
5031: } /* ENd Waves */
5032:
5033: cutv(stra, strb,line,' ');
5034: if(iout=sscanf(strb,"%d/%d",&month, &year) != 0){
5035: }
5036: else if(iout=sscanf(strb,"%s.",dummy) != 0){
5037: month=99;
5038: year=9999;
5039: }else{
1.141 brouard 5040: 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);
5041: 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);
1.136 brouard 5042: return 1;
5043: }
5044: andc[i]=(double) year;
5045: moisdc[i]=(double) month;
5046: strcpy(line,stra);
5047:
5048: cutv(stra, strb,line,' ');
5049: if(iout=sscanf(strb,"%d/%d",&month, &year) != 0){
5050: }
1.145 brouard 5051: else if(iout=sscanf(strb,"%s.", dummy) != 0){
1.136 brouard 5052: month=99;
5053: year=9999;
5054: }else{
1.141 brouard 5055: 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);
5056: 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);
1.136 brouard 5057: return 1;
5058: }
5059: if (year==9999) {
1.141 brouard 5060: 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);
5061: 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);
1.136 brouard 5062: return 1;
1.126 brouard 5063:
1.136 brouard 5064: }
5065: annais[i]=(double)(year);
5066: moisnais[i]=(double)(month);
5067: strcpy(line,stra);
5068:
5069: cutv(stra, strb,line,' ');
5070: errno=0;
5071: dval=strtod(strb,&endptr);
5072: if( strb[0]=='\0' || (*endptr != '\0')){
1.141 brouard 5073: printf("Error reading data around '%f' at line number %d, \"%s\" for individual %d\nShould be a weight. Exiting.\n",dval, i,line,linei);
5074: 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);
1.136 brouard 5075: fflush(ficlog);
5076: return 1;
5077: }
5078: weight[i]=dval;
5079: strcpy(line,stra);
5080:
5081: for (j=ncovcol;j>=1;j--){
5082: cutv(stra, strb,line,' ');
5083: if(strb[0]=='.') { /* Missing status */
5084: lval=-1;
5085: }else{
5086: errno=0;
5087: lval=strtol(strb,&endptr,10);
5088: if( strb[0]=='\0' || (*endptr != '\0')){
1.141 brouard 5089: 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);
5090: 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);
1.136 brouard 5091: return 1;
5092: }
5093: }
5094: if(lval <-1 || lval >1){
1.141 brouard 5095: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.136 brouard 5096: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
5097: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
5098: For example, for multinomial values like 1, 2 and 3,\n \
5099: build V1=0 V2=0 for the reference value (1),\n \
5100: V1=1 V2=0 for (2) \n \
5101: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
5102: output of IMaCh is often meaningless.\n \
5103: Exiting.\n",lval,linei, i,line,j);
1.141 brouard 5104: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.136 brouard 5105: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
5106: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
5107: For example, for multinomial values like 1, 2 and 3,\n \
5108: build V1=0 V2=0 for the reference value (1),\n \
5109: V1=1 V2=0 for (2) \n \
5110: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
5111: output of IMaCh is often meaningless.\n \
5112: Exiting.\n",lval,linei, i,line,j);fflush(ficlog);
5113: return 1;
5114: }
5115: covar[j][i]=(double)(lval);
5116: strcpy(line,stra);
5117: }
5118: lstra=strlen(stra);
5119:
5120: if(lstra > 9){ /* More than 2**32 or max of what printf can write with %ld */
5121: stratrunc = &(stra[lstra-9]);
5122: num[i]=atol(stratrunc);
5123: }
5124: else
5125: num[i]=atol(stra);
5126: /*if((s[2][i]==2) && (s[3][i]==-1)&&(s[4][i]==9)){
5127: 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;}*/
5128:
5129: i=i+1;
5130: } /* End loop reading data */
1.126 brouard 5131:
1.136 brouard 5132: *imax=i-1; /* Number of individuals */
5133: fclose(fic);
5134:
5135: return (0);
1.164 brouard 5136: /* endread: */
1.136 brouard 5137: printf("Exiting readdata: ");
5138: fclose(fic);
5139: return (1);
1.126 brouard 5140:
5141:
5142:
1.136 brouard 5143: }
1.145 brouard 5144: void removespace(char *str) {
5145: char *p1 = str, *p2 = str;
5146: do
5147: while (*p2 == ' ')
5148: p2++;
5149: while (*p1++ = *p2++);
5150: }
5151:
5152: int decodemodel ( char model[], int lastobs) /**< This routine decode the model and returns:
5153: * Model V1+V2+V3+V8+V7*V8+V5*V6+V8*age+V3*age
5154: * - cptcovt total number of covariates of the model nbocc(+)+1 = 8
5155: * - cptcovn or number of covariates k of the models excluding age*products =6
5156: * - cptcovage number of covariates with age*products =2
5157: * - cptcovs number of simple covariates
5158: * - 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
5159: * which is a new column after the 9 (ncovcol) variables.
5160: * - if k is a product Vn*Vm covar[k][i] is filled with correct values for each individual
5161: * - Tprod[l] gives the kth covariates of the product Vn*Vm l=1 to cptcovprod-cptcovage
5162: * Tprod[1]@2 {5, 6}: position of first product V7*V8 is 5, and second V5*V6 is 6.
5163: * - Tvard[k] p Tvard[1][1]@4 {7, 8, 5, 6} for V7*V8 and V5*V6 .
5164: */
1.136 brouard 5165: {
1.145 brouard 5166: int i, j, k, ks;
1.164 brouard 5167: int j1, k1, k2;
1.136 brouard 5168: char modelsav[80];
1.145 brouard 5169: char stra[80], strb[80], strc[80], strd[80],stre[80];
1.136 brouard 5170:
1.145 brouard 5171: /*removespace(model);*/
1.136 brouard 5172: if (strlen(model) >1){ /* If there is at least 1 covariate */
1.145 brouard 5173: j=0, j1=0, k1=0, k2=-1, ks=0, cptcovn=0;
5174: j=nbocc(model,'+'); /**< j=Number of '+' */
5175: j1=nbocc(model,'*'); /**< j1=Number of '*' */
5176: cptcovs=j+1-j1; /**< Number of simple covariates V1+V2*age+V3 +V3*V4=> V1 + V3 =2 */
5177: cptcovt= j+1; /* Number of total covariates in the model V1 + V2*age+ V3 + V3*V4=> 4*/
5178: /* including age products which are counted in cptcovage.
5179: /* but the covariates which are products must be treated separately: ncovn=4- 2=2 (V1+V3). */
5180: cptcovprod=j1; /**< Number of products V1*V2 +v3*age = 2 */
5181: cptcovprodnoage=0; /**< Number of covariate products without age: V3*V4 =1 */
1.136 brouard 5182: strcpy(modelsav,model);
1.137 brouard 5183: if (strstr(model,"AGE") !=0){
5184: printf("Error. AGE must be in lower case 'age' model=%s ",model);
5185: fprintf(ficlog,"Error. AGE must be in lower case model=%s ",model);fflush(ficlog);
1.136 brouard 5186: return 1;
5187: }
1.141 brouard 5188: if (strstr(model,"v") !=0){
5189: printf("Error. 'v' must be in upper case 'V' model=%s ",model);
5190: fprintf(ficlog,"Error. 'v' must be in upper case model=%s ",model);fflush(ficlog);
5191: return 1;
5192: }
1.136 brouard 5193:
1.145 brouard 5194: /* Design
5195: * V1 V2 V3 V4 V5 V6 V7 V8 V9 Weight
5196: * < ncovcol=8 >
5197: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8
5198: * k= 1 2 3 4 5 6 7 8
5199: * cptcovn number of covariates (not including constant and age ) = # of + plus 1 = 7+1=8
5200: * covar[k,i], value of kth covariate if not including age for individual i:
5201: * covar[1][i]= (V2), covar[4][i]=(V3), covar[8][i]=(V8)
5202: * Tvar[k] # of the kth covariate: Tvar[1]=2 Tvar[4]=3 Tvar[8]=8
5203: * if multiplied by age: V3*age Tvar[3=V3*age]=3 (V3) Tvar[7]=8 and
5204: * Tage[++cptcovage]=k
5205: * if products, new covar are created after ncovcol with k1
5206: * Tvar[k]=ncovcol+k1; # of the kth covariate product: Tvar[5]=ncovcol+1=10 Tvar[6]=ncovcol+1=11
5207: * Tprod[k1]=k; Tprod[1]=5 Tprod[2]= 6; gives the position of the k1th product
5208: * 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
5209: * Tvar[cptcovn+k2]=Tvard[k1][1];Tvar[cptcovn+k2+1]=Tvard[k1][2];
5210: * Tvar[8+1]=5;Tvar[8+2]=6;Tvar[8+3]=7;Tvar[8+4]=8 inverted
5211: * V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11
5212: * < ncovcol=8 >
5213: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8 d1 d1 d2 d2
5214: * k= 1 2 3 4 5 6 7 8 9 10 11 12
5215: * Tvar[k]= 2 1 3 3 10 11 8 8 5 6 7 8
5216: * p Tvar[1]@12={2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
5217: * p Tprod[1]@2={ 6, 5}
5218: *p Tvard[1][1]@4= {7, 8, 5, 6}
5219: * covar[k][i]= V2 V1 ? V3 V5*V6? V7*V8? ? V8
5220: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
5221: *How to reorganize?
5222: * Model V1 + V2 + V3 + V8 + V5*V6 + V7*V8 + V3*age + V8*age
5223: * Tvars {2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
5224: * {2, 1, 4, 8, 5, 6, 3, 7}
5225: * Struct []
5226: */
5227:
1.136 brouard 5228: /* This loop fills the array Tvar from the string 'model'.*/
5229: /* j is the number of + signs in the model V1+V2+V3 j=2 i=3 to 1 */
1.137 brouard 5230: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4 */
5231: /* k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tage[cptcovage=1]=4 */
5232: /* k=3 V4 Tvar[k=3]= 4 (from V4) */
5233: /* k=2 V1 Tvar[k=2]= 1 (from V1) */
5234: /* k=1 Tvar[1]=2 (from V2) */
5235: /* k=5 Tvar[5] */
5236: /* for (k=1; k<=cptcovn;k++) { */
5237: /* cov[2+k]=nbcode[Tvar[k]][codtab[ij][Tvar[k]]]; */
5238: /* } */
5239: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
1.145 brouard 5240: /*
5241: * Treating invertedly V2+V1+V3*age+V2*V4 is as if written V2*V4 +V3*age + V1 + V2 */
5242: for(k=cptcovt; k>=1;k--) /**< Number of covariates */
5243: Tvar[k]=0;
5244: cptcovage=0;
5245: for(k=1; k<=cptcovt;k++){ /* Loop on total covariates of the model */
5246: cutl(stra,strb,modelsav,'+'); /* keeps in strb after the first '+'
5247: modelsav==V2+V1+V4+V3*age strb=V3*age stra=V2+V1+V4 */
1.137 brouard 5248: if (nbocc(modelsav,'+')==0) strcpy(strb,modelsav); /* and analyzes it */
1.136 brouard 5249: /* printf("i=%d a=%s b=%s sav=%s\n",i, stra,strb,modelsav);*/
5250: /*scanf("%d",i);*/
1.145 brouard 5251: if (strchr(strb,'*')) { /**< Model includes a product V2+V1+V4+V3*age strb=V3*age */
5252: cutl(strc,strd,strb,'*'); /**< strd*strc Vm*Vn: strb=V3*age(input) strc=age strd=V3 ; V3*V2 strc=V2, strd=V3 */
5253: if (strcmp(strc,"age")==0) { /**< Model includes age: Vn*age */
5254: /* covar is not filled and then is empty */
1.136 brouard 5255: cptcovprod--;
1.145 brouard 5256: cutl(stre,strb,strd,'V'); /* strd=V3(input): stre="3" */
5257: Tvar[k]=atoi(stre); /* V2+V1+V4+V3*age Tvar[4]=3 ; V1+V2*age Tvar[2]=2 */
1.136 brouard 5258: cptcovage++; /* Sums the number of covariates which include age as a product */
1.137 brouard 5259: Tage[cptcovage]=k; /* Tage[1] = 4 */
1.136 brouard 5260: /*printf("stre=%s ", stre);*/
1.137 brouard 5261: } else if (strcmp(strd,"age")==0) { /* or age*Vn */
1.136 brouard 5262: cptcovprod--;
1.145 brouard 5263: cutl(stre,strb,strc,'V');
1.136 brouard 5264: Tvar[k]=atoi(stre);
5265: cptcovage++;
5266: Tage[cptcovage]=k;
1.137 brouard 5267: } else { /* Age is not in the model product V2+V1+V1*V4+V3*age+V3*V2 strb=V3*V2*/
5268: /* loops on k1=1 (V3*V2) and k1=2 V4*V3 */
1.145 brouard 5269: cptcovn++;
5270: cptcovprodnoage++;k1++;
5271: cutl(stre,strb,strc,'V'); /* strc= Vn, stre is n; strb=V3*V2 stre=3 strc=*/
5272: Tvar[k]=ncovcol+k1; /* For model-covariate k tells which data-covariate to use but
1.137 brouard 5273: because this model-covariate is a construction we invent a new column
5274: ncovcol + k1
5275: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2
5276: Tvar[3=V1*V4]=4+1 Tvar[5=V3*V2]=4 + 2= 6, etc */
1.145 brouard 5277: cutl(strc,strb,strd,'V'); /* strd was Vm, strc is m */
1.137 brouard 5278: Tprod[k1]=k; /* Tprod[1]=3(=V1*V4) for V2+V1+V1*V4+age*V3+V3*V2 */
1.145 brouard 5279: Tvard[k1][1] =atoi(strc); /* m 1 for V1*/
5280: Tvard[k1][2] =atoi(stre); /* n 4 for V4*/
5281: k2=k2+2;
5282: Tvar[cptcovt+k2]=Tvard[k1][1]; /* Tvar[(cptcovt=4+k2=1)=5]= 1 (V1) */
5283: Tvar[cptcovt+k2+1]=Tvard[k1][2]; /* Tvar[(cptcovt=4+(k2=1)+1)=6]= 4 (V4) */
1.137 brouard 5284: for (i=1; i<=lastobs;i++){
5285: /* Computes the new covariate which is a product of
1.145 brouard 5286: covar[n][i]* covar[m][i] and stores it at ncovol+k1 May not be defined */
1.136 brouard 5287: covar[ncovcol+k1][i]=covar[atoi(stre)][i]*covar[atoi(strc)][i];
1.137 brouard 5288: }
5289: } /* End age is not in the model */
5290: } /* End if model includes a product */
1.136 brouard 5291: else { /* no more sum */
5292: /*printf("d=%s c=%s b=%s\n", strd,strc,strb);*/
5293: /* scanf("%d",i);*/
1.145 brouard 5294: cutl(strd,strc,strb,'V');
5295: ks++; /**< Number of simple covariates */
5296: cptcovn++;
5297: Tvar[k]=atoi(strd);
1.136 brouard 5298: }
1.137 brouard 5299: strcpy(modelsav,stra); /* modelsav=V2+V1+V4 stra=V2+V1+V4 */
1.136 brouard 5300: /*printf("a=%s b=%s sav=%s\n", stra,strb,modelsav);
5301: scanf("%d",i);*/
5302: } /* end of loop + */
5303: } /* end model */
5304:
5305: /*The number n of Vn is stored in Tvar. cptcovage =number of age covariate. Tage gives the position of age. cptcovprod= number of products.
5306: If model=V1+V1*age then Tvar[1]=1 Tvar[2]=1 cptcovage=1 Tage[1]=2 cptcovprod=0*/
5307:
5308: /* printf("tvar1=%d tvar2=%d tvar3=%d cptcovage=%d Tage=%d",Tvar[1],Tvar[2],Tvar[3],cptcovage,Tage[1]);
5309: printf("cptcovprod=%d ", cptcovprod);
5310: fprintf(ficlog,"cptcovprod=%d ", cptcovprod);
5311:
5312: scanf("%d ",i);*/
5313:
5314:
1.137 brouard 5315: return (0); /* with covar[new additional covariate if product] and Tage if age */
1.164 brouard 5316: /*endread:*/
1.136 brouard 5317: printf("Exiting decodemodel: ");
5318: return (1);
5319: }
5320:
5321: calandcheckages(int imx, int maxwav, double *agemin, double *agemax, int *nberr, int *nbwarn )
5322: {
5323: int i, m;
5324:
5325: for (i=1; i<=imx; i++) {
5326: for(m=2; (m<= maxwav); m++) {
5327: if (((int)mint[m][i]== 99) && (s[m][i] <= nlstate)){
5328: anint[m][i]=9999;
5329: s[m][i]=-1;
5330: }
5331: if((int)moisdc[i]==99 && (int)andc[i]==9999 && s[m][i]>nlstate){
5332: *nberr++;
5333: 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);
5334: 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);
5335: s[m][i]=-1;
5336: }
5337: if((int)moisdc[i]==99 && (int)andc[i]!=9999 && s[m][i]>nlstate){
5338: *nberr++;
5339: 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]);
5340: 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]);
5341: s[m][i]=-1; /* We prefer to skip it (and to skip it in version 0.8a1 too */
5342: }
5343: }
5344: }
5345:
5346: for (i=1; i<=imx; i++) {
5347: agedc[i]=(moisdc[i]/12.+andc[i])-(moisnais[i]/12.+annais[i]);
5348: for(m=firstpass; (m<= lastpass); m++){
5349: if(s[m][i] >0 || s[m][i]==-2 || s[m][i]==-4 || s[m][i]==-5){
5350: if (s[m][i] >= nlstate+1) {
5351: if(agedc[i]>0)
5352: if((int)moisdc[i]!=99 && (int)andc[i]!=9999)
5353: agev[m][i]=agedc[i];
5354: /*if(moisdc[i]==99 && andc[i]==9999) s[m][i]=-1;*/
5355: else {
5356: if ((int)andc[i]!=9999){
5357: nbwarn++;
5358: printf("Warning negative age at death: %ld line:%d\n",num[i],i);
5359: fprintf(ficlog,"Warning negative age at death: %ld line:%d\n",num[i],i);
5360: agev[m][i]=-1;
5361: }
5362: }
5363: }
5364: else if(s[m][i] !=9){ /* Standard case, age in fractional
5365: years but with the precision of a month */
5366: agev[m][i]=(mint[m][i]/12.+1./24.+anint[m][i])-(moisnais[i]/12.+1./24.+annais[i]);
5367: if((int)mint[m][i]==99 || (int)anint[m][i]==9999)
5368: agev[m][i]=1;
5369: else if(agev[m][i] < *agemin){
5370: *agemin=agev[m][i];
5371: printf(" Min anint[%d][%d]=%.2f annais[%d]=%.2f, agemin=%.2f\n",m,i,anint[m][i], i,annais[i], *agemin);
5372: }
5373: else if(agev[m][i] >*agemax){
5374: *agemax=agev[m][i];
1.156 brouard 5375: /* printf(" Max anint[%d][%d]=%.0f annais[%d]=%.0f, agemax=%.2f\n",m,i,anint[m][i], i,annais[i], *agemax);*/
1.136 brouard 5376: }
5377: /*agev[m][i]=anint[m][i]-annais[i];*/
5378: /* agev[m][i] = age[i]+2*m;*/
5379: }
5380: else { /* =9 */
5381: agev[m][i]=1;
5382: s[m][i]=-1;
5383: }
5384: }
5385: else /*= 0 Unknown */
5386: agev[m][i]=1;
5387: }
5388:
5389: }
5390: for (i=1; i<=imx; i++) {
5391: for(m=firstpass; (m<=lastpass); m++){
5392: if (s[m][i] > (nlstate+ndeath)) {
5393: *nberr++;
5394: 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);
5395: 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);
5396: return 1;
5397: }
5398: }
5399: }
5400:
5401: /*for (i=1; i<=imx; i++){
5402: for (m=firstpass; (m<lastpass); m++){
5403: printf("%ld %d %.lf %d %d\n", num[i],(covar[1][i]),agev[m][i],s[m][i],s[m+1][i]);
5404: }
5405:
5406: }*/
5407:
5408:
1.139 brouard 5409: printf("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
5410: fprintf(ficlog,"Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
1.136 brouard 5411:
5412: return (0);
1.164 brouard 5413: /* endread:*/
1.136 brouard 5414: printf("Exiting calandcheckages: ");
5415: return (1);
5416: }
5417:
5418:
5419: /***********************************************/
5420: /**************** Main Program *****************/
5421: /***********************************************/
5422:
5423: int main(int argc, char *argv[])
5424: {
5425: #ifdef GSL
5426: const gsl_multimin_fminimizer_type *T;
5427: size_t iteri = 0, it;
5428: int rval = GSL_CONTINUE;
5429: int status = GSL_SUCCESS;
5430: double ssval;
5431: #endif
5432: int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav);
1.164 brouard 5433: int i,j, k, n=MAXN,iter=0,m,size=100, cptcod;
5434:
5435: int jj, ll, li, lj, lk;
1.136 brouard 5436: int numlinepar=0; /* Current linenumber of parameter file */
5437: int itimes;
5438: int NDIM=2;
5439: int vpopbased=0;
5440:
1.164 brouard 5441: char ca[32], cb[32];
1.136 brouard 5442: /* FILE *fichtm; *//* Html File */
5443: /* FILE *ficgp;*/ /*Gnuplot File */
5444: struct stat info;
1.164 brouard 5445: double agedeb;
1.136 brouard 5446: double ageminpar=1.e20,agemin=1.e20, agemaxpar=-1.e20, agemax=-1.e20;
5447:
1.165 brouard 5448: double fret;
1.136 brouard 5449: double dum; /* Dummy variable */
5450: double ***p3mat;
5451: double ***mobaverage;
1.164 brouard 5452:
5453: char line[MAXLINE];
1.136 brouard 5454: char path[MAXLINE],pathc[MAXLINE],pathcd[MAXLINE],pathtot[MAXLINE],model[MAXLINE];
5455: char pathr[MAXLINE], pathimach[MAXLINE];
1.164 brouard 5456: char *tok, *val; /* pathtot */
1.136 brouard 5457: int firstobs=1, lastobs=10;
1.164 brouard 5458: int c, h , cpt;
5459: int jl;
5460: int i1, j1, jk, stepsize;
5461: int *tab;
1.136 brouard 5462: int mobilavproj=0 , prevfcast=0 ; /* moving average of prev, If prevfcast=1 prevalence projection */
5463: int mobilav=0,popforecast=0;
5464: int hstepm, nhstepm;
5465: int agemortsup;
5466: float sumlpop=0.;
5467: double jprev1=1, mprev1=1,anprev1=2000,jprev2=1, mprev2=1,anprev2=2000;
5468: double jpyram=1, mpyram=1,anpyram=2000,jpyram1=1, mpyram1=1,anpyram1=2000;
5469:
1.164 brouard 5470: double bage=0, fage=110, age, agelim, agebase;
1.136 brouard 5471: double ftolpl=FTOL;
5472: double **prlim;
5473: double ***param; /* Matrix of parameters */
5474: double *p;
5475: double **matcov; /* Matrix of covariance */
5476: double ***delti3; /* Scale */
5477: double *delti; /* Scale */
5478: double ***eij, ***vareij;
5479: double **varpl; /* Variances of prevalence limits by age */
5480: double *epj, vepp;
1.164 brouard 5481:
1.136 brouard 5482: double dateprev1, dateprev2,jproj1=1,mproj1=1,anproj1=2000,jproj2=1,mproj2=1,anproj2=2000;
5483: double **ximort;
1.145 brouard 5484: char *alph[]={"a","a","b","c","d","e"}, str[4]="1234";
1.136 brouard 5485: int *dcwave;
5486:
1.164 brouard 5487: char z[1]="c";
1.136 brouard 5488:
5489: /*char *strt;*/
5490: char strtend[80];
1.126 brouard 5491:
1.164 brouard 5492:
1.126 brouard 5493: /* setlocale (LC_ALL, ""); */
5494: /* bindtextdomain (PACKAGE, LOCALEDIR); */
5495: /* textdomain (PACKAGE); */
5496: /* setlocale (LC_CTYPE, ""); */
5497: /* setlocale (LC_MESSAGES, ""); */
5498:
5499: /* gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */
1.157 brouard 5500: rstart_time = time(NULL);
5501: /* (void) gettimeofday(&start_time,&tzp);*/
5502: start_time = *localtime(&rstart_time);
1.126 brouard 5503: curr_time=start_time;
1.157 brouard 5504: /*tml = *localtime(&start_time.tm_sec);*/
5505: /* strcpy(strstart,asctime(&tml)); */
5506: strcpy(strstart,asctime(&start_time));
1.126 brouard 5507:
5508: /* printf("Localtime (at start)=%s",strstart); */
1.157 brouard 5509: /* tp.tm_sec = tp.tm_sec +86400; */
5510: /* tm = *localtime(&start_time.tm_sec); */
1.126 brouard 5511: /* tmg.tm_year=tmg.tm_year +dsign*dyear; */
5512: /* tmg.tm_mon=tmg.tm_mon +dsign*dmonth; */
5513: /* tmg.tm_hour=tmg.tm_hour + 1; */
1.157 brouard 5514: /* tp.tm_sec = mktime(&tmg); */
1.126 brouard 5515: /* strt=asctime(&tmg); */
5516: /* printf("Time(after) =%s",strstart); */
5517: /* (void) time (&time_value);
5518: * printf("time=%d,t-=%d\n",time_value,time_value-86400);
5519: * tm = *localtime(&time_value);
5520: * strstart=asctime(&tm);
5521: * printf("tim_value=%d,asctime=%s\n",time_value,strstart);
5522: */
5523:
5524: nberr=0; /* Number of errors and warnings */
5525: nbwarn=0;
5526: getcwd(pathcd, size);
5527:
5528: printf("\n%s\n%s",version,fullversion);
5529: if(argc <=1){
5530: printf("\nEnter the parameter file name: ");
5531: fgets(pathr,FILENAMELENGTH,stdin);
5532: i=strlen(pathr);
5533: if(pathr[i-1]=='\n')
5534: pathr[i-1]='\0';
1.156 brouard 5535: i=strlen(pathr);
5536: if(pathr[i-1]==' ') /* This may happen when dragging on oS/X! */
5537: pathr[i-1]='\0';
1.126 brouard 5538: for (tok = pathr; tok != NULL; ){
5539: printf("Pathr |%s|\n",pathr);
5540: while ((val = strsep(&tok, "\"" )) != NULL && *val == '\0');
5541: printf("val= |%s| pathr=%s\n",val,pathr);
5542: strcpy (pathtot, val);
5543: if(pathr[0] == '\0') break; /* Dirty */
5544: }
5545: }
5546: else{
5547: strcpy(pathtot,argv[1]);
5548: }
5549: /*if(getcwd(pathcd, MAXLINE)!= NULL)printf ("Error pathcd\n");*/
5550: /*cygwin_split_path(pathtot,path,optionfile);
5551: printf("pathtot=%s, path=%s, optionfile=%s\n",pathtot,path,optionfile);*/
5552: /* cutv(path,optionfile,pathtot,'\\');*/
5553:
5554: /* Split argv[0], imach program to get pathimach */
5555: printf("\nargv[0]=%s argv[1]=%s, \n",argv[0],argv[1]);
5556: split(argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
5557: printf("\nargv[0]=%s pathimach=%s, \noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
5558: /* strcpy(pathimach,argv[0]); */
5559: /* Split argv[1]=pathtot, parameter file name to get path, optionfile, extension and name */
5560: split(pathtot,path,optionfile,optionfilext,optionfilefiname);
5561: printf("\npathtot=%s,\npath=%s,\noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname);
5562: chdir(path); /* Can be a relative path */
5563: if(getcwd(pathcd,MAXLINE) > 0) /* So pathcd is the full path */
5564: printf("Current directory %s!\n",pathcd);
5565: strcpy(command,"mkdir ");
5566: strcat(command,optionfilefiname);
5567: if((outcmd=system(command)) != 0){
5568: printf("Problem creating directory or it already exists %s%s, err=%d\n",path,optionfilefiname,outcmd);
5569: /* fprintf(ficlog,"Problem creating directory %s%s\n",path,optionfilefiname); */
5570: /* fclose(ficlog); */
5571: /* exit(1); */
5572: }
5573: /* if((imk=mkdir(optionfilefiname))<0){ */
5574: /* perror("mkdir"); */
5575: /* } */
5576:
5577: /*-------- arguments in the command line --------*/
5578:
5579: /* Log file */
5580: strcat(filelog, optionfilefiname);
5581: strcat(filelog,".log"); /* */
5582: if((ficlog=fopen(filelog,"w"))==NULL) {
5583: printf("Problem with logfile %s\n",filelog);
5584: goto end;
5585: }
5586: fprintf(ficlog,"Log filename:%s\n",filelog);
5587: fprintf(ficlog,"\n%s\n%s",version,fullversion);
5588: fprintf(ficlog,"\nEnter the parameter file name: \n");
5589: fprintf(ficlog,"pathimach=%s\npathtot=%s\n\
5590: path=%s \n\
5591: optionfile=%s\n\
5592: optionfilext=%s\n\
1.156 brouard 5593: optionfilefiname='%s'\n",pathimach,pathtot,path,optionfile,optionfilext,optionfilefiname);
1.126 brouard 5594:
5595: printf("Local time (at start):%s",strstart);
5596: fprintf(ficlog,"Local time (at start): %s",strstart);
5597: fflush(ficlog);
5598: /* (void) gettimeofday(&curr_time,&tzp); */
1.157 brouard 5599: /* printf("Elapsed time %d\n", asc_diff_time(curr_time.tm_sec-start_time.tm_sec,tmpout)); */
1.126 brouard 5600:
5601: /* */
5602: strcpy(fileres,"r");
5603: strcat(fileres, optionfilefiname);
5604: strcat(fileres,".txt"); /* Other files have txt extension */
5605:
5606: /*---------arguments file --------*/
5607:
5608: if((ficpar=fopen(optionfile,"r"))==NULL) {
1.155 brouard 5609: printf("Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
5610: fprintf(ficlog,"Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
1.126 brouard 5611: fflush(ficlog);
1.149 brouard 5612: /* goto end; */
5613: exit(70);
1.126 brouard 5614: }
5615:
5616:
5617:
5618: strcpy(filereso,"o");
5619: strcat(filereso,fileres);
5620: if((ficparo=fopen(filereso,"w"))==NULL) { /* opened on subdirectory */
5621: printf("Problem with Output resultfile: %s\n", filereso);
5622: fprintf(ficlog,"Problem with Output resultfile: %s\n", filereso);
5623: fflush(ficlog);
5624: goto end;
5625: }
5626:
5627: /* Reads comments: lines beginning with '#' */
5628: numlinepar=0;
5629: while((c=getc(ficpar))=='#' && c!= EOF){
5630: ungetc(c,ficpar);
5631: fgets(line, MAXLINE, ficpar);
5632: numlinepar++;
1.141 brouard 5633: fputs(line,stdout);
1.126 brouard 5634: fputs(line,ficparo);
5635: fputs(line,ficlog);
5636: }
5637: ungetc(c,ficpar);
5638:
5639: 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);
5640: numlinepar++;
5641: 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);
5642: 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);
5643: 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);
5644: fflush(ficlog);
5645: while((c=getc(ficpar))=='#' && c!= EOF){
5646: ungetc(c,ficpar);
5647: fgets(line, MAXLINE, ficpar);
5648: numlinepar++;
1.141 brouard 5649: fputs(line, stdout);
5650: //puts(line);
1.126 brouard 5651: fputs(line,ficparo);
5652: fputs(line,ficlog);
5653: }
5654: ungetc(c,ficpar);
5655:
5656:
1.145 brouard 5657: covar=matrix(0,NCOVMAX,1,n); /**< used in readdata */
1.136 brouard 5658: cptcovn=0; /*Number of covariates, i.e. number of '+' in model statement plus one, indepently of n in Vn*/
5659: /* v1+v2+v3+v2*v4+v5*age makes cptcovn = 5
5660: v1+v2*age+v2*v3 makes cptcovn = 3
5661: */
5662: if (strlen(model)>1)
1.145 brouard 5663: 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*/
5664: else
5665: ncovmodel=2;
1.126 brouard 5666: nvar=ncovmodel-1; /* Suppressing age as a basic covariate */
1.133 brouard 5667: nforce= (nlstate+ndeath-1)*nlstate; /* Number of forces ij from state i to j */
5668: npar= nforce*ncovmodel; /* Number of parameters like aij*/
1.131 brouard 5669: if(npar >MAXPARM || nlstate >NLSTATEMAX || ndeath >NDEATHMAX || ncovmodel>NCOVMAX){
5670: 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);
5671: 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);
5672: fflush(stdout);
5673: fclose (ficlog);
5674: goto end;
5675: }
1.126 brouard 5676: delti3= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
5677: delti=delti3[1][1];
5678: /*delti=vector(1,npar); *//* Scale of each paramater (output from hesscov)*/
5679: if(mle==-1){ /* Print a wizard for help writing covariance matrix */
5680: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
5681: printf(" You choose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
5682: fprintf(ficlog," You choose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
5683: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
5684: fclose (ficparo);
5685: fclose (ficlog);
5686: goto end;
5687: exit(0);
5688: }
5689: else if(mle==-3) {
5690: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
5691: printf(" You choose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
5692: fprintf(ficlog," You choose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
5693: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
5694: matcov=matrix(1,npar,1,npar);
5695: }
5696: else{
1.145 brouard 5697: /* Read guessed parameters */
1.126 brouard 5698: /* Reads comments: lines beginning with '#' */
5699: while((c=getc(ficpar))=='#' && c!= EOF){
5700: ungetc(c,ficpar);
5701: fgets(line, MAXLINE, ficpar);
5702: numlinepar++;
1.141 brouard 5703: fputs(line,stdout);
1.126 brouard 5704: fputs(line,ficparo);
5705: fputs(line,ficlog);
5706: }
5707: ungetc(c,ficpar);
5708:
5709: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
5710: for(i=1; i <=nlstate; i++){
5711: j=0;
5712: for(jj=1; jj <=nlstate+ndeath; jj++){
5713: if(jj==i) continue;
5714: j++;
5715: fscanf(ficpar,"%1d%1d",&i1,&j1);
5716: if ((i1 != i) && (j1 != j)){
5717: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n \
5718: It might be a problem of design; if ncovcol and the model are correct\n \
5719: run imach with mle=-1 to get a correct template of the parameter file.\n",numlinepar, i,j, i1, j1);
5720: exit(1);
5721: }
5722: fprintf(ficparo,"%1d%1d",i1,j1);
5723: if(mle==1)
5724: printf("%1d%1d",i,j);
5725: fprintf(ficlog,"%1d%1d",i,j);
5726: for(k=1; k<=ncovmodel;k++){
5727: fscanf(ficpar," %lf",¶m[i][j][k]);
5728: if(mle==1){
5729: printf(" %lf",param[i][j][k]);
5730: fprintf(ficlog," %lf",param[i][j][k]);
5731: }
5732: else
5733: fprintf(ficlog," %lf",param[i][j][k]);
5734: fprintf(ficparo," %lf",param[i][j][k]);
5735: }
5736: fscanf(ficpar,"\n");
5737: numlinepar++;
5738: if(mle==1)
5739: printf("\n");
5740: fprintf(ficlog,"\n");
5741: fprintf(ficparo,"\n");
5742: }
5743: }
5744: fflush(ficlog);
5745:
1.145 brouard 5746: /* Reads scales values */
1.126 brouard 5747: p=param[1][1];
5748:
5749: /* Reads comments: lines beginning with '#' */
5750: while((c=getc(ficpar))=='#' && c!= EOF){
5751: ungetc(c,ficpar);
5752: fgets(line, MAXLINE, ficpar);
5753: numlinepar++;
1.141 brouard 5754: fputs(line,stdout);
1.126 brouard 5755: fputs(line,ficparo);
5756: fputs(line,ficlog);
5757: }
5758: ungetc(c,ficpar);
5759:
5760: for(i=1; i <=nlstate; i++){
5761: for(j=1; j <=nlstate+ndeath-1; j++){
5762: fscanf(ficpar,"%1d%1d",&i1,&j1);
1.164 brouard 5763: if ( (i1-i) * (j1-j) != 0){
1.126 brouard 5764: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n",numlinepar, i,j, i1, j1);
5765: exit(1);
5766: }
5767: printf("%1d%1d",i,j);
5768: fprintf(ficparo,"%1d%1d",i1,j1);
5769: fprintf(ficlog,"%1d%1d",i1,j1);
5770: for(k=1; k<=ncovmodel;k++){
5771: fscanf(ficpar,"%le",&delti3[i][j][k]);
5772: printf(" %le",delti3[i][j][k]);
5773: fprintf(ficparo," %le",delti3[i][j][k]);
5774: fprintf(ficlog," %le",delti3[i][j][k]);
5775: }
5776: fscanf(ficpar,"\n");
5777: numlinepar++;
5778: printf("\n");
5779: fprintf(ficparo,"\n");
5780: fprintf(ficlog,"\n");
5781: }
5782: }
5783: fflush(ficlog);
5784:
1.145 brouard 5785: /* Reads covariance matrix */
1.126 brouard 5786: delti=delti3[1][1];
5787:
5788:
5789: /* free_ma3x(delti3,1,nlstate,1,nlstate+ndeath-1,1,ncovmodel); */ /* Hasn't to to freed here otherwise delti is no more allocated */
5790:
5791: /* Reads comments: lines beginning with '#' */
5792: while((c=getc(ficpar))=='#' && c!= EOF){
5793: ungetc(c,ficpar);
5794: fgets(line, MAXLINE, ficpar);
5795: numlinepar++;
1.141 brouard 5796: fputs(line,stdout);
1.126 brouard 5797: fputs(line,ficparo);
5798: fputs(line,ficlog);
5799: }
5800: ungetc(c,ficpar);
5801:
5802: matcov=matrix(1,npar,1,npar);
1.131 brouard 5803: for(i=1; i <=npar; i++)
5804: for(j=1; j <=npar; j++) matcov[i][j]=0.;
5805:
1.126 brouard 5806: for(i=1; i <=npar; i++){
1.145 brouard 5807: fscanf(ficpar,"%s",str);
1.126 brouard 5808: if(mle==1)
5809: printf("%s",str);
5810: fprintf(ficlog,"%s",str);
5811: fprintf(ficparo,"%s",str);
5812: for(j=1; j <=i; j++){
5813: fscanf(ficpar," %le",&matcov[i][j]);
5814: if(mle==1){
5815: printf(" %.5le",matcov[i][j]);
5816: }
5817: fprintf(ficlog," %.5le",matcov[i][j]);
5818: fprintf(ficparo," %.5le",matcov[i][j]);
5819: }
5820: fscanf(ficpar,"\n");
5821: numlinepar++;
5822: if(mle==1)
5823: printf("\n");
5824: fprintf(ficlog,"\n");
5825: fprintf(ficparo,"\n");
5826: }
5827: for(i=1; i <=npar; i++)
5828: for(j=i+1;j<=npar;j++)
5829: matcov[i][j]=matcov[j][i];
5830:
5831: if(mle==1)
5832: printf("\n");
5833: fprintf(ficlog,"\n");
5834:
5835: fflush(ficlog);
5836:
5837: /*-------- Rewriting parameter file ----------*/
5838: strcpy(rfileres,"r"); /* "Rparameterfile */
5839: strcat(rfileres,optionfilefiname); /* Parameter file first name*/
5840: strcat(rfileres,"."); /* */
5841: strcat(rfileres,optionfilext); /* Other files have txt extension */
5842: if((ficres =fopen(rfileres,"w"))==NULL) {
5843: printf("Problem writing new parameter file: %s\n", fileres);goto end;
5844: fprintf(ficlog,"Problem writing new parameter file: %s\n", fileres);goto end;
5845: }
5846: fprintf(ficres,"#%s\n",version);
5847: } /* End of mle != -3 */
5848:
5849:
5850: n= lastobs;
5851: num=lvector(1,n);
5852: moisnais=vector(1,n);
5853: annais=vector(1,n);
5854: moisdc=vector(1,n);
5855: andc=vector(1,n);
5856: agedc=vector(1,n);
5857: cod=ivector(1,n);
5858: weight=vector(1,n);
5859: for(i=1;i<=n;i++) weight[i]=1.0; /* Equal weights, 1 by default */
5860: mint=matrix(1,maxwav,1,n);
5861: anint=matrix(1,maxwav,1,n);
1.131 brouard 5862: s=imatrix(1,maxwav+1,1,n); /* s[i][j] health state for wave i and individual j */
1.126 brouard 5863: tab=ivector(1,NCOVMAX);
1.144 brouard 5864: ncodemax=ivector(1,NCOVMAX); /* Number of code per covariate; if O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */
1.126 brouard 5865:
1.136 brouard 5866: /* Reads data from file datafile */
5867: if (readdata(datafile, firstobs, lastobs, &imx)==1)
5868: goto end;
5869:
5870: /* Calculation of the number of parameters from char model */
1.137 brouard 5871: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4
5872: k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tag[cptcovage=1]=4
5873: k=3 V4 Tvar[k=3]= 4 (from V4)
5874: k=2 V1 Tvar[k=2]= 1 (from V1)
5875: k=1 Tvar[1]=2 (from V2)
5876: */
5877: Tvar=ivector(1,NCOVMAX); /* Was 15 changed to NCOVMAX. */
5878: /* V2+V1+V4+age*V3 is a model with 4 covariates (3 plus signs).
5879: For each model-covariate stores the data-covariate id. Tvar[1]=2, Tvar[2]=1, Tvar[3]=4,
5880: Tvar[4=age*V3] is 3 and 'age' is recorded in Tage.
5881: */
5882: /* For model-covariate k tells which data-covariate to use but
5883: because this model-covariate is a construction we invent a new column
5884: ncovcol + k1
5885: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3
5886: Tvar[3=V1*V4]=4+1 etc */
1.145 brouard 5887: Tprod=ivector(1,NCOVMAX); /* Gives the position of a product */
1.137 brouard 5888: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3
5889: if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2)
5890: */
1.145 brouard 5891: Tvaraff=ivector(1,NCOVMAX); /* Unclear */
5892: 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
1.141 brouard 5893: * For V3*V2 (in V2+V1+V1*V4+age*V3+V3*V2), V3*V2 position is 2nd.
5894: * Tvard[k1=2][1]=3 (V3) Tvard[k1=2][2]=2(V2) */
1.145 brouard 5895: Tage=ivector(1,NCOVMAX); /* Gives the covariate id of covariates associated with age: V2 + V1 + age*V4 + V3*age
1.137 brouard 5896: 4 covariates (3 plus signs)
5897: Tage[1=V3*age]= 4; Tage[2=age*V4] = 3
5898: */
1.136 brouard 5899:
5900: if(decodemodel(model, lastobs) == 1)
5901: goto end;
5902:
1.137 brouard 5903: if((double)(lastobs-imx)/(double)imx > 1.10){
5904: nbwarn++;
5905: 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);
5906: 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);
5907: }
1.136 brouard 5908: /* if(mle==1){*/
1.137 brouard 5909: if (weightopt != 1) { /* Maximisation without weights. We can have weights different from 1 but want no weight*/
5910: for(i=1;i<=imx;i++) weight[i]=1.0; /* changed to imx */
1.136 brouard 5911: }
5912:
5913: /*-calculation of age at interview from date of interview and age at death -*/
5914: agev=matrix(1,maxwav,1,imx);
5915:
5916: if(calandcheckages(imx, maxwav, &agemin, &agemax, &nberr, &nbwarn) == 1)
5917: goto end;
5918:
1.126 brouard 5919:
1.136 brouard 5920: agegomp=(int)agemin;
5921: free_vector(moisnais,1,n);
5922: free_vector(annais,1,n);
1.126 brouard 5923: /* free_matrix(mint,1,maxwav,1,n);
5924: free_matrix(anint,1,maxwav,1,n);*/
5925: free_vector(moisdc,1,n);
5926: free_vector(andc,1,n);
1.145 brouard 5927: /* */
5928:
1.126 brouard 5929: wav=ivector(1,imx);
5930: dh=imatrix(1,lastpass-firstpass+1,1,imx);
5931: bh=imatrix(1,lastpass-firstpass+1,1,imx);
5932: mw=imatrix(1,lastpass-firstpass+1,1,imx);
5933:
5934: /* Concatenates waves */
5935: concatwav(wav, dh, bh, mw, s, agedc, agev, firstpass, lastpass, imx, nlstate, stepm);
1.145 brouard 5936: /* */
5937:
1.126 brouard 5938: /* Routine tricode is to calculate cptcoveff (real number of unique covariates) and to associate covariable number and modality */
5939:
5940: nbcode=imatrix(0,NCOVMAX,0,NCOVMAX);
5941: ncodemax[1]=1;
1.145 brouard 5942: Ndum =ivector(-1,NCOVMAX);
5943: if (ncovmodel > 2)
5944: tricode(Tvar,nbcode,imx, Ndum); /**< Fills nbcode[Tvar[j]][l]; */
5945:
5946: codtab=imatrix(1,100,1,10); /* codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) */
5947: /*printf(" codtab[1,1],codtab[100,10]=%d,%d\n", codtab[1][1],codtab[100][10]);*/
5948: h=0;
5949:
5950:
5951: /*if (cptcovn > 0) */
1.126 brouard 5952:
1.145 brouard 5953:
1.126 brouard 5954: m=pow(2,cptcoveff);
5955:
1.131 brouard 5956: for(k=1;k<=cptcoveff; k++){ /* scans any effective covariate */
1.143 brouard 5957: 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 */
5958: for(j=1; j <= ncodemax[k]; j++){ /* For each modality of this covariate ncodemax=2*/
5959: for(cpt=1; cpt <=pow(2,k-1); cpt++){ /* cpt=1 to 8/2**(3+1-1 or 3+1-3) =1 or 4 */
1.126 brouard 5960: h++;
1.141 brouard 5961: if (h>m)
1.136 brouard 5962: h=1;
1.144 brouard 5963: /**< codtab(h,k) k = codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) + 1
1.143 brouard 5964: * h 1 2 3 4
5965: *______________________________
5966: * 1 i=1 1 i=1 1 i=1 1 i=1 1
5967: * 2 2 1 1 1
5968: * 3 i=2 1 2 1 1
5969: * 4 2 2 1 1
5970: * 5 i=3 1 i=2 1 2 1
5971: * 6 2 1 2 1
5972: * 7 i=4 1 2 2 1
5973: * 8 2 2 2 1
5974: * 9 i=5 1 i=3 1 i=2 1 1
5975: * 10 2 1 1 1
5976: * 11 i=6 1 2 1 1
5977: * 12 2 2 1 1
5978: * 13 i=7 1 i=4 1 2 1
5979: * 14 2 1 2 1
5980: * 15 i=8 1 2 2 1
5981: * 16 2 2 2 1
5982: */
1.141 brouard 5983: codtab[h][k]=j;
1.145 brouard 5984: /*codtab[h][Tvar[k]]=j;*/
1.130 brouard 5985: 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]]);
1.126 brouard 5986: }
5987: }
5988: }
5989: }
5990: /* printf("codtab[1][2]=%d codtab[2][2]=%d",codtab[1][2],codtab[2][2]);
5991: codtab[1][2]=1;codtab[2][2]=2; */
5992: /* for(i=1; i <=m ;i++){
5993: for(k=1; k <=cptcovn; k++){
1.131 brouard 5994: printf("i=%d k=%d %d %d ",i,k,codtab[i][k], cptcoveff);
1.126 brouard 5995: }
5996: printf("\n");
5997: }
5998: scanf("%d",i);*/
1.145 brouard 5999:
6000: free_ivector(Ndum,-1,NCOVMAX);
6001:
6002:
1.126 brouard 6003:
6004: /*------------ gnuplot -------------*/
6005: strcpy(optionfilegnuplot,optionfilefiname);
6006: if(mle==-3)
6007: strcat(optionfilegnuplot,"-mort");
6008: strcat(optionfilegnuplot,".gp");
6009:
6010: if((ficgp=fopen(optionfilegnuplot,"w"))==NULL) {
6011: printf("Problem with file %s",optionfilegnuplot);
6012: }
6013: else{
6014: fprintf(ficgp,"\n# %s\n", version);
6015: fprintf(ficgp,"# %s\n", optionfilegnuplot);
1.141 brouard 6016: //fprintf(ficgp,"set missing 'NaNq'\n");
6017: fprintf(ficgp,"set datafile missing 'NaNq'\n");
1.126 brouard 6018: }
6019: /* fclose(ficgp);*/
6020: /*--------- index.htm --------*/
6021:
6022: strcpy(optionfilehtm,optionfilefiname); /* Main html file */
6023: if(mle==-3)
6024: strcat(optionfilehtm,"-mort");
6025: strcat(optionfilehtm,".htm");
6026: if((fichtm=fopen(optionfilehtm,"w"))==NULL) {
1.131 brouard 6027: printf("Problem with %s \n",optionfilehtm);
6028: exit(0);
1.126 brouard 6029: }
6030:
6031: strcpy(optionfilehtmcov,optionfilefiname); /* Only for matrix of covariance */
6032: strcat(optionfilehtmcov,"-cov.htm");
6033: if((fichtmcov=fopen(optionfilehtmcov,"w"))==NULL) {
6034: printf("Problem with %s \n",optionfilehtmcov), exit(0);
6035: }
6036: else{
6037: fprintf(fichtmcov,"<html><head>\n<title>IMaCh Cov %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
6038: <hr size=\"2\" color=\"#EC5E5E\"> \n\
6039: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=%s<br>\n",\
6040: optionfilehtmcov,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
6041: }
6042:
6043: fprintf(fichtm,"<html><head>\n<title>IMaCh %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
6044: <hr size=\"2\" color=\"#EC5E5E\"> \n\
6045: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=%s<br>\n\
6046: \n\
6047: <hr size=\"2\" color=\"#EC5E5E\">\
6048: <ul><li><h4>Parameter files</h4>\n\
6049: - Parameter file: <a href=\"%s.%s\">%s.%s</a><br>\n\
6050: - Copy of the parameter file: <a href=\"o%s\">o%s</a><br>\n\
6051: - Log file of the run: <a href=\"%s\">%s</a><br>\n\
6052: - Gnuplot file name: <a href=\"%s\">%s</a><br>\n\
6053: - Date and time at start: %s</ul>\n",\
6054: optionfilehtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model,\
6055: optionfilefiname,optionfilext,optionfilefiname,optionfilext,\
6056: fileres,fileres,\
6057: filelog,filelog,optionfilegnuplot,optionfilegnuplot,strstart);
6058: fflush(fichtm);
6059:
6060: strcpy(pathr,path);
6061: strcat(pathr,optionfilefiname);
6062: chdir(optionfilefiname); /* Move to directory named optionfile */
6063:
6064: /* Calculates basic frequencies. Computes observed prevalence at single age
6065: and prints on file fileres'p'. */
6066: freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart);
6067:
6068: fprintf(fichtm,"\n");
6069: fprintf(fichtm,"<br>Total number of observations=%d <br>\n\
6070: Youngest age at first (selected) pass %.2f, oldest age %.2f<br>\n\
6071: Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n",\
6072: imx,agemin,agemax,jmin,jmax,jmean);
6073: pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
6074: oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
6075: newms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
6076: savms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
6077: oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */
6078:
6079:
6080: /* For Powell, parameters are in a vector p[] starting at p[1]
6081: so we point p on param[1][1] so that p[1] maps on param[1][1][1] */
6082: p=param[1][1]; /* *(*(*(param +1)+1)+0) */
6083:
6084: globpr=0; /* To get the number ipmx of contributions and the sum of weights*/
6085:
6086: if (mle==-3){
1.136 brouard 6087: ximort=matrix(1,NDIM,1,NDIM);
6088: /* ximort=gsl_matrix_alloc(1,NDIM,1,NDIM); */
1.126 brouard 6089: cens=ivector(1,n);
6090: ageexmed=vector(1,n);
6091: agecens=vector(1,n);
6092: dcwave=ivector(1,n);
6093:
6094: for (i=1; i<=imx; i++){
6095: dcwave[i]=-1;
6096: for (m=firstpass; m<=lastpass; m++)
6097: if (s[m][i]>nlstate) {
6098: dcwave[i]=m;
6099: /* printf("i=%d j=%d s=%d dcwave=%d\n",i,j, s[j][i],dcwave[i]);*/
6100: break;
6101: }
6102: }
6103:
6104: for (i=1; i<=imx; i++) {
6105: if (wav[i]>0){
6106: ageexmed[i]=agev[mw[1][i]][i];
6107: j=wav[i];
6108: agecens[i]=1.;
6109:
6110: if (ageexmed[i]> 1 && wav[i] > 0){
6111: agecens[i]=agev[mw[j][i]][i];
6112: cens[i]= 1;
6113: }else if (ageexmed[i]< 1)
6114: cens[i]= -1;
6115: if (agedc[i]< AGESUP && agedc[i]>1 && dcwave[i]>firstpass && dcwave[i]<=lastpass)
6116: cens[i]=0 ;
6117: }
6118: else cens[i]=-1;
6119: }
6120:
6121: for (i=1;i<=NDIM;i++) {
6122: for (j=1;j<=NDIM;j++)
6123: ximort[i][j]=(i == j ? 1.0 : 0.0);
6124: }
6125:
1.145 brouard 6126: /*p[1]=0.0268; p[NDIM]=0.083;*/
1.126 brouard 6127: /*printf("%lf %lf", p[1], p[2]);*/
6128:
6129:
1.136 brouard 6130: #ifdef GSL
6131: printf("GSL optimization\n"); fprintf(ficlog,"Powell\n");
1.162 brouard 6132: #else
1.126 brouard 6133: printf("Powell\n"); fprintf(ficlog,"Powell\n");
1.136 brouard 6134: #endif
1.126 brouard 6135: strcpy(filerespow,"pow-mort");
6136: strcat(filerespow,fileres);
6137: if((ficrespow=fopen(filerespow,"w"))==NULL) {
6138: printf("Problem with resultfile: %s\n", filerespow);
6139: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
6140: }
1.136 brouard 6141: #ifdef GSL
6142: fprintf(ficrespow,"# GSL optimization\n# iter -2*LL");
1.162 brouard 6143: #else
1.126 brouard 6144: fprintf(ficrespow,"# Powell\n# iter -2*LL");
1.136 brouard 6145: #endif
1.126 brouard 6146: /* for (i=1;i<=nlstate;i++)
6147: for(j=1;j<=nlstate+ndeath;j++)
6148: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
6149: */
6150: fprintf(ficrespow,"\n");
1.136 brouard 6151: #ifdef GSL
6152: /* gsl starts here */
6153: T = gsl_multimin_fminimizer_nmsimplex;
6154: gsl_multimin_fminimizer *sfm = NULL;
6155: gsl_vector *ss, *x;
6156: gsl_multimin_function minex_func;
6157:
6158: /* Initial vertex size vector */
6159: ss = gsl_vector_alloc (NDIM);
6160:
6161: if (ss == NULL){
6162: GSL_ERROR_VAL ("failed to allocate space for ss", GSL_ENOMEM, 0);
6163: }
6164: /* Set all step sizes to 1 */
6165: gsl_vector_set_all (ss, 0.001);
6166:
6167: /* Starting point */
1.126 brouard 6168:
1.136 brouard 6169: x = gsl_vector_alloc (NDIM);
6170:
6171: if (x == NULL){
6172: gsl_vector_free(ss);
6173: GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0);
6174: }
6175:
6176: /* Initialize method and iterate */
6177: /* p[1]=0.0268; p[NDIM]=0.083; */
6178: /* gsl_vector_set(x, 0, 0.0268); */
6179: /* gsl_vector_set(x, 1, 0.083); */
6180: gsl_vector_set(x, 0, p[1]);
6181: gsl_vector_set(x, 1, p[2]);
6182:
6183: minex_func.f = &gompertz_f;
6184: minex_func.n = NDIM;
6185: minex_func.params = (void *)&p; /* ??? */
6186:
6187: sfm = gsl_multimin_fminimizer_alloc (T, NDIM);
6188: gsl_multimin_fminimizer_set (sfm, &minex_func, x, ss);
6189:
6190: printf("Iterations beginning .....\n\n");
6191: printf("Iter. # Intercept Slope -Log Likelihood Simplex size\n");
6192:
6193: iteri=0;
6194: while (rval == GSL_CONTINUE){
6195: iteri++;
6196: status = gsl_multimin_fminimizer_iterate(sfm);
6197:
6198: if (status) printf("error: %s\n", gsl_strerror (status));
6199: fflush(0);
6200:
6201: if (status)
6202: break;
6203:
6204: rval = gsl_multimin_test_size (gsl_multimin_fminimizer_size (sfm), 1e-6);
6205: ssval = gsl_multimin_fminimizer_size (sfm);
6206:
6207: if (rval == GSL_SUCCESS)
6208: printf ("converged to a local maximum at\n");
6209:
6210: printf("%5d ", iteri);
6211: for (it = 0; it < NDIM; it++){
6212: printf ("%10.5f ", gsl_vector_get (sfm->x, it));
6213: }
6214: printf("f() = %-10.5f ssize = %.7f\n", sfm->fval, ssval);
6215: }
6216:
6217: printf("\n\n Please note: Program should be run many times with varying starting points to detemine global maximum\n\n");
6218:
6219: gsl_vector_free(x); /* initial values */
6220: gsl_vector_free(ss); /* inital step size */
6221: for (it=0; it<NDIM; it++){
6222: p[it+1]=gsl_vector_get(sfm->x,it);
6223: fprintf(ficrespow," %.12lf", p[it]);
6224: }
6225: gsl_multimin_fminimizer_free (sfm); /* p *(sfm.x.data) et p *(sfm.x.data+1) */
6226: #endif
6227: #ifdef POWELL
6228: powell(p,ximort,NDIM,ftol,&iter,&fret,gompertz);
6229: #endif
1.126 brouard 6230: fclose(ficrespow);
6231:
6232: hesscov(matcov, p, NDIM, delti, 1e-4, gompertz);
6233:
6234: for(i=1; i <=NDIM; i++)
6235: for(j=i+1;j<=NDIM;j++)
6236: matcov[i][j]=matcov[j][i];
6237:
6238: printf("\nCovariance matrix\n ");
6239: for(i=1; i <=NDIM; i++) {
6240: for(j=1;j<=NDIM;j++){
6241: printf("%f ",matcov[i][j]);
6242: }
6243: printf("\n ");
6244: }
6245:
6246: printf("iter=%d MLE=%f Eq=%lf*exp(%lf*(age-%d))\n",iter,-gompertz(p),p[1],p[2],agegomp);
6247: for (i=1;i<=NDIM;i++)
6248: printf("%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
6249:
6250: lsurv=vector(1,AGESUP);
6251: lpop=vector(1,AGESUP);
6252: tpop=vector(1,AGESUP);
6253: lsurv[agegomp]=100000;
6254:
6255: for (k=agegomp;k<=AGESUP;k++) {
6256: agemortsup=k;
6257: if (p[1]*exp(p[2]*(k-agegomp))>1) break;
6258: }
6259:
6260: for (k=agegomp;k<agemortsup;k++)
6261: lsurv[k+1]=lsurv[k]-lsurv[k]*(p[1]*exp(p[2]*(k-agegomp)));
6262:
6263: for (k=agegomp;k<agemortsup;k++){
6264: lpop[k]=(lsurv[k]+lsurv[k+1])/2.;
6265: sumlpop=sumlpop+lpop[k];
6266: }
6267:
6268: tpop[agegomp]=sumlpop;
6269: for (k=agegomp;k<(agemortsup-3);k++){
6270: /* tpop[k+1]=2;*/
6271: tpop[k+1]=tpop[k]-lpop[k];
6272: }
6273:
6274:
6275: printf("\nAge lx qx dx Lx Tx e(x)\n");
6276: for (k=agegomp;k<(agemortsup-2);k++)
6277: 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]);
6278:
6279:
6280: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
6281: printinggnuplotmort(fileres, optionfilefiname,ageminpar,agemaxpar,fage, pathc,p);
6282:
6283: printinghtmlmort(fileres,title,datafile, firstpass, lastpass, \
6284: stepm, weightopt,\
6285: model,imx,p,matcov,agemortsup);
6286:
6287: free_vector(lsurv,1,AGESUP);
6288: free_vector(lpop,1,AGESUP);
6289: free_vector(tpop,1,AGESUP);
1.136 brouard 6290: #ifdef GSL
6291: free_ivector(cens,1,n);
6292: free_vector(agecens,1,n);
6293: free_ivector(dcwave,1,n);
6294: free_matrix(ximort,1,NDIM,1,NDIM);
6295: #endif
1.126 brouard 6296: } /* Endof if mle==-3 */
6297:
6298: else{ /* For mle >=1 */
1.132 brouard 6299: globpr=0;/* debug */
6300: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
1.126 brouard 6301: printf("First Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
6302: for (k=1; k<=npar;k++)
6303: printf(" %d %8.5f",k,p[k]);
6304: printf("\n");
6305: globpr=1; /* to print the contributions */
6306: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
6307: printf("Second Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
6308: for (k=1; k<=npar;k++)
6309: printf(" %d %8.5f",k,p[k]);
6310: printf("\n");
6311: if(mle>=1){ /* Could be 1 or 2 */
6312: mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func);
6313: }
6314:
6315: /*--------- results files --------------*/
6316: 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);
6317:
6318:
6319: fprintf(ficres,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
6320: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
6321: fprintf(ficlog,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
6322: for(i=1,jk=1; i <=nlstate; i++){
6323: for(k=1; k <=(nlstate+ndeath); k++){
6324: if (k != i) {
6325: printf("%d%d ",i,k);
6326: fprintf(ficlog,"%d%d ",i,k);
6327: fprintf(ficres,"%1d%1d ",i,k);
6328: for(j=1; j <=ncovmodel; j++){
6329: printf("%lf ",p[jk]);
6330: fprintf(ficlog,"%lf ",p[jk]);
6331: fprintf(ficres,"%lf ",p[jk]);
6332: jk++;
6333: }
6334: printf("\n");
6335: fprintf(ficlog,"\n");
6336: fprintf(ficres,"\n");
6337: }
6338: }
6339: }
6340: if(mle!=0){
6341: /* Computing hessian and covariance matrix */
6342: ftolhess=ftol; /* Usually correct */
6343: hesscov(matcov, p, npar, delti, ftolhess, func);
6344: }
6345: fprintf(ficres,"# Scales (for hessian or gradient estimation)\n");
6346: printf("# Scales (for hessian or gradient estimation)\n");
6347: fprintf(ficlog,"# Scales (for hessian or gradient estimation)\n");
6348: for(i=1,jk=1; i <=nlstate; i++){
6349: for(j=1; j <=nlstate+ndeath; j++){
6350: if (j!=i) {
6351: fprintf(ficres,"%1d%1d",i,j);
6352: printf("%1d%1d",i,j);
6353: fprintf(ficlog,"%1d%1d",i,j);
6354: for(k=1; k<=ncovmodel;k++){
6355: printf(" %.5e",delti[jk]);
6356: fprintf(ficlog," %.5e",delti[jk]);
6357: fprintf(ficres," %.5e",delti[jk]);
6358: jk++;
6359: }
6360: printf("\n");
6361: fprintf(ficlog,"\n");
6362: fprintf(ficres,"\n");
6363: }
6364: }
6365: }
6366:
6367: 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");
6368: if(mle>=1)
6369: 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");
6370: 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");
6371: /* # 121 Var(a12)\n\ */
6372: /* # 122 Cov(b12,a12) Var(b12)\n\ */
6373: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
6374: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
6375: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
6376: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
6377: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
6378: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
6379:
6380:
6381: /* Just to have a covariance matrix which will be more understandable
6382: even is we still don't want to manage dictionary of variables
6383: */
6384: for(itimes=1;itimes<=2;itimes++){
6385: jj=0;
6386: for(i=1; i <=nlstate; i++){
6387: for(j=1; j <=nlstate+ndeath; j++){
6388: if(j==i) continue;
6389: for(k=1; k<=ncovmodel;k++){
6390: jj++;
6391: ca[0]= k+'a'-1;ca[1]='\0';
6392: if(itimes==1){
6393: if(mle>=1)
6394: printf("#%1d%1d%d",i,j,k);
6395: fprintf(ficlog,"#%1d%1d%d",i,j,k);
6396: fprintf(ficres,"#%1d%1d%d",i,j,k);
6397: }else{
6398: if(mle>=1)
6399: printf("%1d%1d%d",i,j,k);
6400: fprintf(ficlog,"%1d%1d%d",i,j,k);
6401: fprintf(ficres,"%1d%1d%d",i,j,k);
6402: }
6403: ll=0;
6404: for(li=1;li <=nlstate; li++){
6405: for(lj=1;lj <=nlstate+ndeath; lj++){
6406: if(lj==li) continue;
6407: for(lk=1;lk<=ncovmodel;lk++){
6408: ll++;
6409: if(ll<=jj){
6410: cb[0]= lk +'a'-1;cb[1]='\0';
6411: if(ll<jj){
6412: if(itimes==1){
6413: if(mle>=1)
6414: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
6415: fprintf(ficlog," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
6416: fprintf(ficres," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
6417: }else{
6418: if(mle>=1)
6419: printf(" %.5e",matcov[jj][ll]);
6420: fprintf(ficlog," %.5e",matcov[jj][ll]);
6421: fprintf(ficres," %.5e",matcov[jj][ll]);
6422: }
6423: }else{
6424: if(itimes==1){
6425: if(mle>=1)
6426: printf(" Var(%s%1d%1d)",ca,i,j);
6427: fprintf(ficlog," Var(%s%1d%1d)",ca,i,j);
6428: fprintf(ficres," Var(%s%1d%1d)",ca,i,j);
6429: }else{
6430: if(mle>=1)
6431: printf(" %.5e",matcov[jj][ll]);
6432: fprintf(ficlog," %.5e",matcov[jj][ll]);
6433: fprintf(ficres," %.5e",matcov[jj][ll]);
6434: }
6435: }
6436: }
6437: } /* end lk */
6438: } /* end lj */
6439: } /* end li */
6440: if(mle>=1)
6441: printf("\n");
6442: fprintf(ficlog,"\n");
6443: fprintf(ficres,"\n");
6444: numlinepar++;
6445: } /* end k*/
6446: } /*end j */
6447: } /* end i */
6448: } /* end itimes */
6449:
6450: fflush(ficlog);
6451: fflush(ficres);
6452:
6453: while((c=getc(ficpar))=='#' && c!= EOF){
6454: ungetc(c,ficpar);
6455: fgets(line, MAXLINE, ficpar);
1.141 brouard 6456: fputs(line,stdout);
1.126 brouard 6457: fputs(line,ficparo);
6458: }
6459: ungetc(c,ficpar);
6460:
6461: estepm=0;
6462: fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d\n",&ageminpar,&agemaxpar, &bage, &fage, &estepm);
6463: if (estepm==0 || estepm < stepm) estepm=stepm;
6464: if (fage <= 2) {
6465: bage = ageminpar;
6466: fage = agemaxpar;
6467: }
6468:
6469: fprintf(ficres,"# agemin agemax for life expectancy, bage fage (if mle==0 ie no data nor Max likelihood).\n");
6470: fprintf(ficres,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d\n",ageminpar,agemaxpar,bage,fage, estepm);
6471: fprintf(ficparo,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d\n",ageminpar,agemaxpar,bage,fage, estepm);
6472:
6473: while((c=getc(ficpar))=='#' && c!= EOF){
6474: ungetc(c,ficpar);
6475: fgets(line, MAXLINE, ficpar);
1.141 brouard 6476: fputs(line,stdout);
1.126 brouard 6477: fputs(line,ficparo);
6478: }
6479: ungetc(c,ficpar);
6480:
6481: 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);
6482: 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);
6483: 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);
6484: printf("begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mov_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
6485: 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);
6486:
6487: while((c=getc(ficpar))=='#' && c!= EOF){
6488: ungetc(c,ficpar);
6489: fgets(line, MAXLINE, ficpar);
1.141 brouard 6490: fputs(line,stdout);
1.126 brouard 6491: fputs(line,ficparo);
6492: }
6493: ungetc(c,ficpar);
6494:
6495:
6496: dateprev1=anprev1+(mprev1-1)/12.+(jprev1-1)/365.;
6497: dateprev2=anprev2+(mprev2-1)/12.+(jprev2-1)/365.;
6498:
6499: fscanf(ficpar,"pop_based=%d\n",&popbased);
6500: fprintf(ficparo,"pop_based=%d\n",popbased);
6501: fprintf(ficres,"pop_based=%d\n",popbased);
6502:
6503: while((c=getc(ficpar))=='#' && c!= EOF){
6504: ungetc(c,ficpar);
6505: fgets(line, MAXLINE, ficpar);
1.141 brouard 6506: fputs(line,stdout);
1.126 brouard 6507: fputs(line,ficparo);
6508: }
6509: ungetc(c,ficpar);
6510:
6511: 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);
6512: 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);
6513: 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);
6514: 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);
6515: 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);
6516: /* day and month of proj2 are not used but only year anproj2.*/
6517:
6518:
6519:
1.145 brouard 6520: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint); */
6521: /* ,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2); */
1.126 brouard 6522:
6523: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
6524: printinggnuplot(fileres, optionfilefiname,ageminpar,agemaxpar,fage, pathc,p);
6525:
6526: printinghtml(fileres,title,datafile, firstpass, lastpass, stepm, weightopt,\
6527: model,imx,jmin,jmax,jmean,rfileres,popforecast,estepm,\
6528: jprev1,mprev1,anprev1,jprev2,mprev2,anprev2);
6529:
6530: /*------------ free_vector -------------*/
6531: /* chdir(path); */
6532:
6533: free_ivector(wav,1,imx);
6534: free_imatrix(dh,1,lastpass-firstpass+1,1,imx);
6535: free_imatrix(bh,1,lastpass-firstpass+1,1,imx);
6536: free_imatrix(mw,1,lastpass-firstpass+1,1,imx);
6537: free_lvector(num,1,n);
6538: free_vector(agedc,1,n);
6539: /*free_matrix(covar,0,NCOVMAX,1,n);*/
6540: /*free_matrix(covar,1,NCOVMAX,1,n);*/
6541: fclose(ficparo);
6542: fclose(ficres);
6543:
6544:
6545: /*--------------- Prevalence limit (period or stable prevalence) --------------*/
1.145 brouard 6546: #include "prevlim.h" /* Use ficrespl, ficlog */
1.126 brouard 6547: fclose(ficrespl);
6548:
1.145 brouard 6549: #ifdef FREEEXIT2
6550: #include "freeexit2.h"
6551: #endif
6552:
1.126 brouard 6553: /*------------- h Pij x at various ages ------------*/
1.145 brouard 6554: #include "hpijx.h"
6555: fclose(ficrespij);
1.126 brouard 6556:
1.145 brouard 6557: /*-------------- Variance of one-step probabilities---*/
6558: k=1;
1.126 brouard 6559: varprob(optionfilefiname, matcov, p, delti, nlstate, bage, fage,k,Tvar,nbcode, ncodemax,strstart);
6560:
6561:
6562: probs= ma3x(1,AGESUP,1,NCOVMAX, 1,NCOVMAX);
6563: for(i=1;i<=AGESUP;i++)
6564: for(j=1;j<=NCOVMAX;j++)
6565: for(k=1;k<=NCOVMAX;k++)
6566: probs[i][j][k]=0.;
6567:
6568: /*---------- Forecasting ------------------*/
6569: /*if((stepm == 1) && (strcmp(model,".")==0)){*/
6570: if(prevfcast==1){
6571: /* if(stepm ==1){*/
6572: prevforecast(fileres, anproj1, mproj1, jproj1, agemin, agemax, dateprev1, dateprev2, mobilavproj, bage, fage, firstpass, lastpass, anproj2, p, cptcoveff);
6573: /* (popforecast==1) populforecast(fileres, anpyram,mpyram,jpyram, agemin,agemax, dateprev1, dateprev2,mobilav, agedeb, fage, popforecast, popfile, anpyram1,p, i1);*/
6574: /* } */
6575: /* else{ */
6576: /* erreur=108; */
6577: /* 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); */
6578: /* 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); */
6579: /* } */
6580: }
6581:
6582:
1.127 brouard 6583: /* Computes prevalence between agemin (i.e minimal age computed) and no more ageminpar */
6584:
6585: prevalence(probs, agemin, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
6586: /* 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",\
6587: ageminpar, agemax, s[lastpass][imx], agev[lastpass][imx], nlstate, imx, mint[lastpass][imx],anint[lastpass][imx], dateprev1, dateprev2, firstpass, lastpass);
6588: */
1.126 brouard 6589:
1.127 brouard 6590: if (mobilav!=0) {
6591: mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
6592: if (movingaverage(probs, bage, fage, mobaverage,mobilav)!=0){
6593: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
6594: printf(" Error in movingaverage mobilav=%d\n",mobilav);
6595: }
1.126 brouard 6596: }
6597:
6598:
1.127 brouard 6599: /*---------- Health expectancies, no variances ------------*/
6600:
1.126 brouard 6601: strcpy(filerese,"e");
6602: strcat(filerese,fileres);
6603: if((ficreseij=fopen(filerese,"w"))==NULL) {
6604: printf("Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
6605: fprintf(ficlog,"Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
6606: }
6607: printf("Computing Health Expectancies: result on file '%s' \n", filerese);
6608: fprintf(ficlog,"Computing Health Expectancies: result on file '%s' \n", filerese);
1.145 brouard 6609: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
6610: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
6611:
6612: for (k=1; k <= (int) pow(2,cptcoveff); k++){
1.127 brouard 6613: fprintf(ficreseij,"\n#****** ");
6614: for(j=1;j<=cptcoveff;j++) {
6615: fprintf(ficreseij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
6616: }
6617: fprintf(ficreseij,"******\n");
6618:
6619: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
6620: oldm=oldms;savm=savms;
6621: evsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, strstart);
6622:
6623: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
1.145 brouard 6624: /*}*/
1.127 brouard 6625: }
6626: fclose(ficreseij);
6627:
6628:
6629: /*---------- Health expectancies and variances ------------*/
6630:
6631:
6632: strcpy(filerest,"t");
6633: strcat(filerest,fileres);
6634: if((ficrest=fopen(filerest,"w"))==NULL) {
6635: printf("Problem with total LE resultfile: %s\n", filerest);goto end;
6636: fprintf(ficlog,"Problem with total LE resultfile: %s\n", filerest);goto end;
6637: }
6638: printf("Computing Total Life expectancies with their standard errors: file '%s' \n", filerest);
6639: fprintf(ficlog,"Computing Total Life expectancies with their standard errors: file '%s' \n", filerest);
6640:
1.126 brouard 6641:
6642: strcpy(fileresstde,"stde");
6643: strcat(fileresstde,fileres);
6644: if((ficresstdeij=fopen(fileresstde,"w"))==NULL) {
6645: printf("Problem with Health Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
6646: fprintf(ficlog,"Problem with Health Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
6647: }
6648: printf("Computing Health Expectancies and standard errors: result on file '%s' \n", fileresstde);
6649: fprintf(ficlog,"Computing Health Expectancies and standard errors: result on file '%s' \n", fileresstde);
6650:
6651: strcpy(filerescve,"cve");
6652: strcat(filerescve,fileres);
6653: if((ficrescveij=fopen(filerescve,"w"))==NULL) {
6654: printf("Problem with Covar. Health Exp. resultfile: %s\n", filerescve); exit(0);
6655: fprintf(ficlog,"Problem with Covar. Health Exp. resultfile: %s\n", filerescve); exit(0);
6656: }
6657: printf("Computing Covar. of Health Expectancies: result on file '%s' \n", filerescve);
6658: fprintf(ficlog,"Computing Covar. of Health Expectancies: result on file '%s' \n", filerescve);
6659:
6660: strcpy(fileresv,"v");
6661: strcat(fileresv,fileres);
6662: if((ficresvij=fopen(fileresv,"w"))==NULL) {
6663: printf("Problem with variance resultfile: %s\n", fileresv);exit(0);
6664: fprintf(ficlog,"Problem with variance resultfile: %s\n", fileresv);exit(0);
6665: }
6666: printf("Computing Variance-covariance of DFLEs: file '%s' \n", fileresv);
6667: fprintf(ficlog,"Computing Variance-covariance of DFLEs: file '%s' \n", fileresv);
6668:
1.145 brouard 6669: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
6670: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
6671:
6672: for (k=1; k <= (int) pow(2,cptcoveff); k++){
6673: fprintf(ficrest,"\n#****** ");
1.126 brouard 6674: for(j=1;j<=cptcoveff;j++)
6675: fprintf(ficrest,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
6676: fprintf(ficrest,"******\n");
6677:
6678: fprintf(ficresstdeij,"\n#****** ");
6679: fprintf(ficrescveij,"\n#****** ");
6680: for(j=1;j<=cptcoveff;j++) {
6681: fprintf(ficresstdeij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
6682: fprintf(ficrescveij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
6683: }
6684: fprintf(ficresstdeij,"******\n");
6685: fprintf(ficrescveij,"******\n");
6686:
6687: fprintf(ficresvij,"\n#****** ");
6688: for(j=1;j<=cptcoveff;j++)
6689: fprintf(ficresvij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
6690: fprintf(ficresvij,"******\n");
6691:
6692: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
6693: oldm=oldms;savm=savms;
1.127 brouard 6694: cvevsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, delti, matcov, strstart);
1.145 brouard 6695: /*
6696: */
6697: /* goto endfree; */
1.126 brouard 6698:
6699: vareij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
6700: pstamp(ficrest);
1.145 brouard 6701:
6702:
1.128 brouard 6703: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
1.145 brouard 6704: oldm=oldms;savm=savms; /* Segmentation fault */
1.161 brouard 6705: cptcod= 0; /* To be deleted */
6706: 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 */
1.145 brouard 6707: 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 ");
1.128 brouard 6708: if(vpopbased==1)
6709: 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);
6710: else
6711: fprintf(ficrest,"the age specific period (stable) prevalences in each health state \n");
6712: fprintf(ficrest,"# Age e.. (std) ");
6713: for (i=1;i<=nlstate;i++) fprintf(ficrest,"e.%d (std) ",i);
6714: fprintf(ficrest,"\n");
1.126 brouard 6715:
1.128 brouard 6716: epj=vector(1,nlstate+1);
6717: for(age=bage; age <=fage ;age++){
6718: prevalim(prlim, nlstate, p, age, oldm, savm,ftolpl,k);
6719: if (vpopbased==1) {
6720: if(mobilav ==0){
6721: for(i=1; i<=nlstate;i++)
6722: prlim[i][i]=probs[(int)age][i][k];
6723: }else{ /* mobilav */
6724: for(i=1; i<=nlstate;i++)
6725: prlim[i][i]=mobaverage[(int)age][i][k];
6726: }
1.126 brouard 6727: }
6728:
1.128 brouard 6729: fprintf(ficrest," %4.0f",age);
6730: for(j=1, epj[nlstate+1]=0.;j <=nlstate;j++){
6731: for(i=1, epj[j]=0.;i <=nlstate;i++) {
6732: epj[j] += prlim[i][i]*eij[i][j][(int)age];
6733: /* printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]);*/
6734: }
6735: epj[nlstate+1] +=epj[j];
1.126 brouard 6736: }
6737:
1.128 brouard 6738: for(i=1, vepp=0.;i <=nlstate;i++)
6739: for(j=1;j <=nlstate;j++)
6740: vepp += vareij[i][j][(int)age];
6741: fprintf(ficrest," %7.3f (%7.3f)", epj[nlstate+1],sqrt(vepp));
6742: for(j=1;j <=nlstate;j++){
6743: fprintf(ficrest," %7.3f (%7.3f)", epj[j],sqrt(vareij[j][j][(int)age]));
6744: }
6745: fprintf(ficrest,"\n");
1.126 brouard 6746: }
6747: }
6748: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
6749: free_ma3x(vareij,1,nlstate,1,nlstate,(int) bage, (int)fage);
6750: free_vector(epj,1,nlstate+1);
1.145 brouard 6751: /*}*/
1.126 brouard 6752: }
6753: free_vector(weight,1,n);
1.145 brouard 6754: free_imatrix(Tvard,1,NCOVMAX,1,2);
1.126 brouard 6755: free_imatrix(s,1,maxwav+1,1,n);
6756: free_matrix(anint,1,maxwav,1,n);
6757: free_matrix(mint,1,maxwav,1,n);
6758: free_ivector(cod,1,n);
6759: free_ivector(tab,1,NCOVMAX);
6760: fclose(ficresstdeij);
6761: fclose(ficrescveij);
6762: fclose(ficresvij);
6763: fclose(ficrest);
6764: fclose(ficpar);
6765:
6766: /*------- Variance of period (stable) prevalence------*/
6767:
6768: strcpy(fileresvpl,"vpl");
6769: strcat(fileresvpl,fileres);
6770: if((ficresvpl=fopen(fileresvpl,"w"))==NULL) {
6771: printf("Problem with variance of period (stable) prevalence resultfile: %s\n", fileresvpl);
6772: exit(0);
6773: }
6774: printf("Computing Variance-covariance of period (stable) prevalence: file '%s' \n", fileresvpl);
6775:
1.145 brouard 6776: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
6777: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
6778:
6779: for (k=1; k <= (int) pow(2,cptcoveff); k++){
6780: fprintf(ficresvpl,"\n#****** ");
1.126 brouard 6781: for(j=1;j<=cptcoveff;j++)
6782: fprintf(ficresvpl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
6783: fprintf(ficresvpl,"******\n");
6784:
6785: varpl=matrix(1,nlstate,(int) bage, (int) fage);
6786: oldm=oldms;savm=savms;
6787: varprevlim(fileres, varpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl,k,strstart);
6788: free_matrix(varpl,1,nlstate,(int) bage, (int)fage);
1.145 brouard 6789: /*}*/
1.126 brouard 6790: }
6791:
6792: fclose(ficresvpl);
6793:
6794: /*---------- End : free ----------------*/
6795: if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
6796: free_ma3x(probs,1,AGESUP,1,NCOVMAX, 1,NCOVMAX);
6797: } /* mle==-3 arrives here for freeing */
1.164 brouard 6798: /* endfree:*/
1.141 brouard 6799: free_matrix(prlim,1,nlstate,1,nlstate); /*here or after loop ? */
1.126 brouard 6800: free_matrix(pmmij,1,nlstate+ndeath,1,nlstate+ndeath);
6801: free_matrix(oldms, 1,nlstate+ndeath,1,nlstate+ndeath);
6802: free_matrix(newms, 1,nlstate+ndeath,1,nlstate+ndeath);
6803: free_matrix(savms, 1,nlstate+ndeath,1,nlstate+ndeath);
6804: free_matrix(covar,0,NCOVMAX,1,n);
6805: free_matrix(matcov,1,npar,1,npar);
6806: /*free_vector(delti,1,npar);*/
6807: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
6808: free_matrix(agev,1,maxwav,1,imx);
6809: free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
6810:
1.145 brouard 6811: free_ivector(ncodemax,1,NCOVMAX);
6812: free_ivector(Tvar,1,NCOVMAX);
6813: free_ivector(Tprod,1,NCOVMAX);
6814: free_ivector(Tvaraff,1,NCOVMAX);
6815: free_ivector(Tage,1,NCOVMAX);
1.126 brouard 6816:
6817: free_imatrix(nbcode,0,NCOVMAX,0,NCOVMAX);
6818: free_imatrix(codtab,1,100,1,10);
6819: fflush(fichtm);
6820: fflush(ficgp);
6821:
6822:
6823: if((nberr >0) || (nbwarn>0)){
6824: printf("End of Imach with %d errors and/or %d warnings\n",nberr,nbwarn);
6825: fprintf(ficlog,"End of Imach with %d errors and/or warnings %d\n",nberr,nbwarn);
6826: }else{
6827: printf("End of Imach\n");
6828: fprintf(ficlog,"End of Imach\n");
6829: }
6830: printf("See log file on %s\n",filelog);
6831: /* gettimeofday(&end_time, (struct timezone*)0);*/ /* after time */
1.157 brouard 6832: /*(void) gettimeofday(&end_time,&tzp);*/
6833: rend_time = time(NULL);
6834: end_time = *localtime(&rend_time);
6835: /* tml = *localtime(&end_time.tm_sec); */
6836: strcpy(strtend,asctime(&end_time));
1.126 brouard 6837: printf("Local time at start %s\nLocal time at end %s",strstart, strtend);
6838: fprintf(ficlog,"Local time at start %s\nLocal time at end %s\n",strstart, strtend);
1.157 brouard 6839: printf("Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
1.126 brouard 6840:
1.157 brouard 6841: printf("Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
6842: fprintf(ficlog,"Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
6843: fprintf(ficlog,"Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
1.126 brouard 6844: /* printf("Total time was %d uSec.\n", total_usecs);*/
6845: /* if(fileappend(fichtm,optionfilehtm)){ */
6846: fprintf(fichtm,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
6847: fclose(fichtm);
6848: fprintf(fichtmcov,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
6849: fclose(fichtmcov);
6850: fclose(ficgp);
6851: fclose(ficlog);
6852: /*------ End -----------*/
6853:
6854:
6855: printf("Before Current directory %s!\n",pathcd);
6856: if(chdir(pathcd) != 0)
6857: printf("Can't move to directory %s!\n",path);
6858: if(getcwd(pathcd,MAXLINE) > 0)
6859: printf("Current directory %s!\n",pathcd);
6860: /*strcat(plotcmd,CHARSEPARATOR);*/
6861: sprintf(plotcmd,"gnuplot");
1.157 brouard 6862: #ifdef _WIN32
1.126 brouard 6863: sprintf(plotcmd,"\"%sgnuplot.exe\"",pathimach);
6864: #endif
6865: if(!stat(plotcmd,&info)){
1.158 brouard 6866: printf("Error or gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 6867: if(!stat(getenv("GNUPLOTBIN"),&info)){
1.158 brouard 6868: printf("Error or gnuplot program not found: '%s' Environment GNUPLOTBIN not set.\n",plotcmd);fflush(stdout);
1.126 brouard 6869: }else
6870: strcpy(pplotcmd,plotcmd);
1.157 brouard 6871: #ifdef __unix
1.126 brouard 6872: strcpy(plotcmd,GNUPLOTPROGRAM);
6873: if(!stat(plotcmd,&info)){
1.158 brouard 6874: printf("Error gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 6875: }else
6876: strcpy(pplotcmd,plotcmd);
6877: #endif
6878: }else
6879: strcpy(pplotcmd,plotcmd);
6880:
6881: sprintf(plotcmd,"%s %s",pplotcmd, optionfilegnuplot);
1.158 brouard 6882: printf("Starting graphs with: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 6883:
6884: if((outcmd=system(plotcmd)) != 0){
1.158 brouard 6885: printf("gnuplot command might not be in your path: '%s', err=%d\n", plotcmd, outcmd);
1.154 brouard 6886: printf("\n Trying if gnuplot resides on the same directory that IMaCh\n");
1.152 brouard 6887: sprintf(plotcmd,"%sgnuplot %s", pathimach, optionfilegnuplot);
1.150 brouard 6888: if((outcmd=system(plotcmd)) != 0)
1.153 brouard 6889: printf("\n Still a problem with gnuplot command %s, err=%d\n", plotcmd, outcmd);
1.126 brouard 6890: }
1.158 brouard 6891: printf(" Successful, please wait...");
1.126 brouard 6892: while (z[0] != 'q') {
6893: /* chdir(path); */
1.154 brouard 6894: printf("\nType e to edit results with your browser, g to graph again and q for exit: ");
1.126 brouard 6895: scanf("%s",z);
6896: /* if (z[0] == 'c') system("./imach"); */
6897: if (z[0] == 'e') {
1.158 brouard 6898: #ifdef __APPLE__
1.152 brouard 6899: sprintf(pplotcmd, "open %s", optionfilehtm);
1.157 brouard 6900: #elif __linux
6901: sprintf(pplotcmd, "xdg-open %s", optionfilehtm);
1.153 brouard 6902: #else
1.152 brouard 6903: sprintf(pplotcmd, "%s", optionfilehtm);
1.153 brouard 6904: #endif
6905: printf("Starting browser with: %s",pplotcmd);fflush(stdout);
6906: system(pplotcmd);
1.126 brouard 6907: }
6908: else if (z[0] == 'g') system(plotcmd);
6909: else if (z[0] == 'q') exit(0);
6910: }
6911: end:
6912: while (z[0] != 'q') {
6913: printf("\nType q for exiting: ");
6914: scanf("%s",z);
6915: }
6916: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>