Annotation of imach/src/imach.c, revision 1.237
1.237 ! brouard 1: /* $Id: imach.c,v 1.236 2016/08/25 10:50:18 brouard Exp $
1.126 brouard 2: $State: Exp $
1.163 brouard 3: $Log: imach.c,v $
1.237 ! brouard 4: Revision 1.236 2016/08/25 10:50:18 brouard
! 5: *** empty log message ***
! 6:
1.236 brouard 7: Revision 1.235 2016/08/25 06:59:23 brouard
8: *** empty log message ***
9:
1.235 brouard 10: Revision 1.234 2016/08/23 16:51:20 brouard
11: *** empty log message ***
12:
1.234 brouard 13: Revision 1.233 2016/08/23 07:40:50 brouard
14: Summary: not working
15:
1.233 brouard 16: Revision 1.232 2016/08/22 14:20:21 brouard
17: Summary: not working
18:
1.232 brouard 19: Revision 1.231 2016/08/22 07:17:15 brouard
20: Summary: not working
21:
1.231 brouard 22: Revision 1.230 2016/08/22 06:55:53 brouard
23: Summary: Not working
24:
1.230 brouard 25: Revision 1.229 2016/07/23 09:45:53 brouard
26: Summary: Completing for func too
27:
1.229 brouard 28: Revision 1.228 2016/07/22 17:45:30 brouard
29: Summary: Fixing some arrays, still debugging
30:
1.227 brouard 31: Revision 1.226 2016/07/12 18:42:34 brouard
32: Summary: temp
33:
1.226 brouard 34: Revision 1.225 2016/07/12 08:40:03 brouard
35: Summary: saving but not running
36:
1.225 brouard 37: Revision 1.224 2016/07/01 13:16:01 brouard
38: Summary: Fixes
39:
1.224 brouard 40: Revision 1.223 2016/02/19 09:23:35 brouard
41: Summary: temporary
42:
1.223 brouard 43: Revision 1.222 2016/02/17 08:14:50 brouard
44: Summary: Probably last 0.98 stable version 0.98r6
45:
1.222 brouard 46: Revision 1.221 2016/02/15 23:35:36 brouard
47: Summary: minor bug
48:
1.220 brouard 49: Revision 1.219 2016/02/15 00:48:12 brouard
50: *** empty log message ***
51:
1.219 brouard 52: Revision 1.218 2016/02/12 11:29:23 brouard
53: Summary: 0.99 Back projections
54:
1.218 brouard 55: Revision 1.217 2015/12/23 17:18:31 brouard
56: Summary: Experimental backcast
57:
1.217 brouard 58: Revision 1.216 2015/12/18 17:32:11 brouard
59: Summary: 0.98r4 Warning and status=-2
60:
61: Version 0.98r4 is now:
62: - displaying an error when status is -1, date of interview unknown and date of death known;
63: - permitting a status -2 when the vital status is unknown at a known date of right truncation.
64: Older changes concerning s=-2, dating from 2005 have been supersed.
65:
1.216 brouard 66: Revision 1.215 2015/12/16 08:52:24 brouard
67: Summary: 0.98r4 working
68:
1.215 brouard 69: Revision 1.214 2015/12/16 06:57:54 brouard
70: Summary: temporary not working
71:
1.214 brouard 72: Revision 1.213 2015/12/11 18:22:17 brouard
73: Summary: 0.98r4
74:
1.213 brouard 75: Revision 1.212 2015/11/21 12:47:24 brouard
76: Summary: minor typo
77:
1.212 brouard 78: Revision 1.211 2015/11/21 12:41:11 brouard
79: Summary: 0.98r3 with some graph of projected cross-sectional
80:
81: Author: Nicolas Brouard
82:
1.211 brouard 83: Revision 1.210 2015/11/18 17:41:20 brouard
84: Summary: Start working on projected prevalences
85:
1.210 brouard 86: Revision 1.209 2015/11/17 22:12:03 brouard
87: Summary: Adding ftolpl parameter
88: Author: N Brouard
89:
90: We had difficulties to get smoothed confidence intervals. It was due
91: to the period prevalence which wasn't computed accurately. The inner
92: parameter ftolpl is now an outer parameter of the .imach parameter
93: file after estepm. If ftolpl is small 1.e-4 and estepm too,
94: computation are long.
95:
1.209 brouard 96: Revision 1.208 2015/11/17 14:31:57 brouard
97: Summary: temporary
98:
1.208 brouard 99: Revision 1.207 2015/10/27 17:36:57 brouard
100: *** empty log message ***
101:
1.207 brouard 102: Revision 1.206 2015/10/24 07:14:11 brouard
103: *** empty log message ***
104:
1.206 brouard 105: Revision 1.205 2015/10/23 15:50:53 brouard
106: Summary: 0.98r3 some clarification for graphs on likelihood contributions
107:
1.205 brouard 108: Revision 1.204 2015/10/01 16:20:26 brouard
109: Summary: Some new graphs of contribution to likelihood
110:
1.204 brouard 111: Revision 1.203 2015/09/30 17:45:14 brouard
112: Summary: looking at better estimation of the hessian
113:
114: Also a better criteria for convergence to the period prevalence And
115: therefore adding the number of years needed to converge. (The
116: prevalence in any alive state shold sum to one
117:
1.203 brouard 118: Revision 1.202 2015/09/22 19:45:16 brouard
119: Summary: Adding some overall graph on contribution to likelihood. Might change
120:
1.202 brouard 121: Revision 1.201 2015/09/15 17:34:58 brouard
122: Summary: 0.98r0
123:
124: - Some new graphs like suvival functions
125: - Some bugs fixed like model=1+age+V2.
126:
1.201 brouard 127: Revision 1.200 2015/09/09 16:53:55 brouard
128: Summary: Big bug thanks to Flavia
129:
130: Even model=1+age+V2. did not work anymore
131:
1.200 brouard 132: Revision 1.199 2015/09/07 14:09:23 brouard
133: Summary: 0.98q6 changing default small png format for graph to vectorized svg.
134:
1.199 brouard 135: Revision 1.198 2015/09/03 07:14:39 brouard
136: Summary: 0.98q5 Flavia
137:
1.198 brouard 138: Revision 1.197 2015/09/01 18:24:39 brouard
139: *** empty log message ***
140:
1.197 brouard 141: Revision 1.196 2015/08/18 23:17:52 brouard
142: Summary: 0.98q5
143:
1.196 brouard 144: Revision 1.195 2015/08/18 16:28:39 brouard
145: Summary: Adding a hack for testing purpose
146:
147: After reading the title, ftol and model lines, if the comment line has
148: a q, starting with #q, the answer at the end of the run is quit. It
149: permits to run test files in batch with ctest. The former workaround was
150: $ echo q | imach foo.imach
151:
1.195 brouard 152: Revision 1.194 2015/08/18 13:32:00 brouard
153: Summary: Adding error when the covariance matrix doesn't contain the exact number of lines required by the model line.
154:
1.194 brouard 155: Revision 1.193 2015/08/04 07:17:42 brouard
156: Summary: 0.98q4
157:
1.193 brouard 158: Revision 1.192 2015/07/16 16:49:02 brouard
159: Summary: Fixing some outputs
160:
1.192 brouard 161: Revision 1.191 2015/07/14 10:00:33 brouard
162: Summary: Some fixes
163:
1.191 brouard 164: Revision 1.190 2015/05/05 08:51:13 brouard
165: Summary: Adding digits in output parameters (7 digits instead of 6)
166:
167: Fix 1+age+.
168:
1.190 brouard 169: Revision 1.189 2015/04/30 14:45:16 brouard
170: Summary: 0.98q2
171:
1.189 brouard 172: Revision 1.188 2015/04/30 08:27:53 brouard
173: *** empty log message ***
174:
1.188 brouard 175: Revision 1.187 2015/04/29 09:11:15 brouard
176: *** empty log message ***
177:
1.187 brouard 178: Revision 1.186 2015/04/23 12:01:52 brouard
179: Summary: V1*age is working now, version 0.98q1
180:
181: Some codes had been disabled in order to simplify and Vn*age was
182: working in the optimization phase, ie, giving correct MLE parameters,
183: but, as usual, outputs were not correct and program core dumped.
184:
1.186 brouard 185: Revision 1.185 2015/03/11 13:26:42 brouard
186: Summary: Inclusion of compile and links command line for Intel Compiler
187:
1.185 brouard 188: Revision 1.184 2015/03/11 11:52:39 brouard
189: Summary: Back from Windows 8. Intel Compiler
190:
1.184 brouard 191: Revision 1.183 2015/03/10 20:34:32 brouard
192: Summary: 0.98q0, trying with directest, mnbrak fixed
193:
194: We use directest instead of original Powell test; probably no
195: incidence on the results, but better justifications;
196: We fixed Numerical Recipes mnbrak routine which was wrong and gave
197: wrong results.
198:
1.183 brouard 199: Revision 1.182 2015/02/12 08:19:57 brouard
200: Summary: Trying to keep directest which seems simpler and more general
201: Author: Nicolas Brouard
202:
1.182 brouard 203: Revision 1.181 2015/02/11 23:22:24 brouard
204: Summary: Comments on Powell added
205:
206: Author:
207:
1.181 brouard 208: Revision 1.180 2015/02/11 17:33:45 brouard
209: Summary: Finishing move from main to function (hpijx and prevalence_limit)
210:
1.180 brouard 211: Revision 1.179 2015/01/04 09:57:06 brouard
212: Summary: back to OS/X
213:
1.179 brouard 214: Revision 1.178 2015/01/04 09:35:48 brouard
215: *** empty log message ***
216:
1.178 brouard 217: Revision 1.177 2015/01/03 18:40:56 brouard
218: Summary: Still testing ilc32 on OSX
219:
1.177 brouard 220: Revision 1.176 2015/01/03 16:45:04 brouard
221: *** empty log message ***
222:
1.176 brouard 223: Revision 1.175 2015/01/03 16:33:42 brouard
224: *** empty log message ***
225:
1.175 brouard 226: Revision 1.174 2015/01/03 16:15:49 brouard
227: Summary: Still in cross-compilation
228:
1.174 brouard 229: Revision 1.173 2015/01/03 12:06:26 brouard
230: Summary: trying to detect cross-compilation
231:
1.173 brouard 232: Revision 1.172 2014/12/27 12:07:47 brouard
233: Summary: Back from Visual Studio and Intel, options for compiling for Windows XP
234:
1.172 brouard 235: Revision 1.171 2014/12/23 13:26:59 brouard
236: Summary: Back from Visual C
237:
238: Still problem with utsname.h on Windows
239:
1.171 brouard 240: Revision 1.170 2014/12/23 11:17:12 brouard
241: Summary: Cleaning some \%% back to %%
242:
243: The escape was mandatory for a specific compiler (which one?), but too many warnings.
244:
1.170 brouard 245: Revision 1.169 2014/12/22 23:08:31 brouard
246: Summary: 0.98p
247:
248: Outputs some informations on compiler used, OS etc. Testing on different platforms.
249:
1.169 brouard 250: Revision 1.168 2014/12/22 15:17:42 brouard
1.170 brouard 251: Summary: update
1.169 brouard 252:
1.168 brouard 253: Revision 1.167 2014/12/22 13:50:56 brouard
254: Summary: Testing uname and compiler version and if compiled 32 or 64
255:
256: Testing on Linux 64
257:
1.167 brouard 258: Revision 1.166 2014/12/22 11:40:47 brouard
259: *** empty log message ***
260:
1.166 brouard 261: Revision 1.165 2014/12/16 11:20:36 brouard
262: Summary: After compiling on Visual C
263:
264: * imach.c (Module): Merging 1.61 to 1.162
265:
1.165 brouard 266: Revision 1.164 2014/12/16 10:52:11 brouard
267: Summary: Merging with Visual C after suppressing some warnings for unused variables. Also fixing Saito's bug 0.98Xn
268:
269: * imach.c (Module): Merging 1.61 to 1.162
270:
1.164 brouard 271: Revision 1.163 2014/12/16 10:30:11 brouard
272: * imach.c (Module): Merging 1.61 to 1.162
273:
1.163 brouard 274: Revision 1.162 2014/09/25 11:43:39 brouard
275: Summary: temporary backup 0.99!
276:
1.162 brouard 277: Revision 1.1 2014/09/16 11:06:58 brouard
278: Summary: With some code (wrong) for nlopt
279:
280: Author:
281:
282: Revision 1.161 2014/09/15 20:41:41 brouard
283: Summary: Problem with macro SQR on Intel compiler
284:
1.161 brouard 285: Revision 1.160 2014/09/02 09:24:05 brouard
286: *** empty log message ***
287:
1.160 brouard 288: Revision 1.159 2014/09/01 10:34:10 brouard
289: Summary: WIN32
290: Author: Brouard
291:
1.159 brouard 292: Revision 1.158 2014/08/27 17:11:51 brouard
293: *** empty log message ***
294:
1.158 brouard 295: Revision 1.157 2014/08/27 16:26:55 brouard
296: Summary: Preparing windows Visual studio version
297: Author: Brouard
298:
299: In order to compile on Visual studio, time.h is now correct and time_t
300: and tm struct should be used. difftime should be used but sometimes I
301: just make the differences in raw time format (time(&now).
302: Trying to suppress #ifdef LINUX
303: Add xdg-open for __linux in order to open default browser.
304:
1.157 brouard 305: Revision 1.156 2014/08/25 20:10:10 brouard
306: *** empty log message ***
307:
1.156 brouard 308: Revision 1.155 2014/08/25 18:32:34 brouard
309: Summary: New compile, minor changes
310: Author: Brouard
311:
1.155 brouard 312: Revision 1.154 2014/06/20 17:32:08 brouard
313: Summary: Outputs now all graphs of convergence to period prevalence
314:
1.154 brouard 315: Revision 1.153 2014/06/20 16:45:46 brouard
316: Summary: If 3 live state, convergence to period prevalence on same graph
317: Author: Brouard
318:
1.153 brouard 319: Revision 1.152 2014/06/18 17:54:09 brouard
320: Summary: open browser, use gnuplot on same dir than imach if not found in the path
321:
1.152 brouard 322: Revision 1.151 2014/06/18 16:43:30 brouard
323: *** empty log message ***
324:
1.151 brouard 325: Revision 1.150 2014/06/18 16:42:35 brouard
326: Summary: If gnuplot is not in the path try on same directory than imach binary (OSX)
327: Author: brouard
328:
1.150 brouard 329: Revision 1.149 2014/06/18 15:51:14 brouard
330: Summary: Some fixes in parameter files errors
331: Author: Nicolas Brouard
332:
1.149 brouard 333: Revision 1.148 2014/06/17 17:38:48 brouard
334: Summary: Nothing new
335: Author: Brouard
336:
337: Just a new packaging for OS/X version 0.98nS
338:
1.148 brouard 339: Revision 1.147 2014/06/16 10:33:11 brouard
340: *** empty log message ***
341:
1.147 brouard 342: Revision 1.146 2014/06/16 10:20:28 brouard
343: Summary: Merge
344: Author: Brouard
345:
346: Merge, before building revised version.
347:
1.146 brouard 348: Revision 1.145 2014/06/10 21:23:15 brouard
349: Summary: Debugging with valgrind
350: Author: Nicolas Brouard
351:
352: Lot of changes in order to output the results with some covariates
353: After the Edimburgh REVES conference 2014, it seems mandatory to
354: improve the code.
355: No more memory valgrind error but a lot has to be done in order to
356: continue the work of splitting the code into subroutines.
357: Also, decodemodel has been improved. Tricode is still not
358: optimal. nbcode should be improved. Documentation has been added in
359: the source code.
360:
1.144 brouard 361: Revision 1.143 2014/01/26 09:45:38 brouard
362: Summary: Version 0.98nR (to be improved, but gives same optimization results as 0.98k. Nice, promising
363:
364: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
365: (Module): Version 0.98nR Running ok, but output format still only works for three covariates.
366:
1.143 brouard 367: Revision 1.142 2014/01/26 03:57:36 brouard
368: Summary: gnuplot changed plot w l 1 has to be changed to plot w l lt 2
369:
370: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
371:
1.142 brouard 372: Revision 1.141 2014/01/26 02:42:01 brouard
373: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
374:
1.141 brouard 375: Revision 1.140 2011/09/02 10:37:54 brouard
376: Summary: times.h is ok with mingw32 now.
377:
1.140 brouard 378: Revision 1.139 2010/06/14 07:50:17 brouard
379: After the theft of my laptop, I probably lost some lines of codes which were not uploaded to the CVS tree.
380: I remember having already fixed agemin agemax which are pointers now but not cvs saved.
381:
1.139 brouard 382: Revision 1.138 2010/04/30 18:19:40 brouard
383: *** empty log message ***
384:
1.138 brouard 385: Revision 1.137 2010/04/29 18:11:38 brouard
386: (Module): Checking covariates for more complex models
387: than V1+V2. A lot of change to be done. Unstable.
388:
1.137 brouard 389: Revision 1.136 2010/04/26 20:30:53 brouard
390: (Module): merging some libgsl code. Fixing computation
391: of likelione (using inter/intrapolation if mle = 0) in order to
392: get same likelihood as if mle=1.
393: Some cleaning of code and comments added.
394:
1.136 brouard 395: Revision 1.135 2009/10/29 15:33:14 brouard
396: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
397:
1.135 brouard 398: Revision 1.134 2009/10/29 13:18:53 brouard
399: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
400:
1.134 brouard 401: Revision 1.133 2009/07/06 10:21:25 brouard
402: just nforces
403:
1.133 brouard 404: Revision 1.132 2009/07/06 08:22:05 brouard
405: Many tings
406:
1.132 brouard 407: Revision 1.131 2009/06/20 16:22:47 brouard
408: Some dimensions resccaled
409:
1.131 brouard 410: Revision 1.130 2009/05/26 06:44:34 brouard
411: (Module): Max Covariate is now set to 20 instead of 8. A
412: lot of cleaning with variables initialized to 0. Trying to make
413: V2+V3*age+V1+V4 strb=V3*age+V1+V4 working better.
414:
1.130 brouard 415: Revision 1.129 2007/08/31 13:49:27 lievre
416: Modification of the way of exiting when the covariate is not binary in order to see on the window the error message before exiting
417:
1.129 lievre 418: Revision 1.128 2006/06/30 13:02:05 brouard
419: (Module): Clarifications on computing e.j
420:
1.128 brouard 421: Revision 1.127 2006/04/28 18:11:50 brouard
422: (Module): Yes the sum of survivors was wrong since
423: imach-114 because nhstepm was no more computed in the age
424: loop. Now we define nhstepma in the age loop.
425: (Module): In order to speed up (in case of numerous covariates) we
426: compute health expectancies (without variances) in a first step
427: and then all the health expectancies with variances or standard
428: deviation (needs data from the Hessian matrices) which slows the
429: computation.
430: In the future we should be able to stop the program is only health
431: expectancies and graph are needed without standard deviations.
432:
1.127 brouard 433: Revision 1.126 2006/04/28 17:23:28 brouard
434: (Module): Yes the sum of survivors was wrong since
435: imach-114 because nhstepm was no more computed in the age
436: loop. Now we define nhstepma in the age loop.
437: Version 0.98h
438:
1.126 brouard 439: Revision 1.125 2006/04/04 15:20:31 lievre
440: Errors in calculation of health expectancies. Age was not initialized.
441: Forecasting file added.
442:
443: Revision 1.124 2006/03/22 17:13:53 lievre
444: Parameters are printed with %lf instead of %f (more numbers after the comma).
445: The log-likelihood is printed in the log file
446:
447: Revision 1.123 2006/03/20 10:52:43 brouard
448: * imach.c (Module): <title> changed, corresponds to .htm file
449: name. <head> headers where missing.
450:
451: * imach.c (Module): Weights can have a decimal point as for
452: English (a comma might work with a correct LC_NUMERIC environment,
453: otherwise the weight is truncated).
454: Modification of warning when the covariates values are not 0 or
455: 1.
456: Version 0.98g
457:
458: Revision 1.122 2006/03/20 09:45:41 brouard
459: (Module): Weights can have a decimal point as for
460: English (a comma might work with a correct LC_NUMERIC environment,
461: otherwise the weight is truncated).
462: Modification of warning when the covariates values are not 0 or
463: 1.
464: Version 0.98g
465:
466: Revision 1.121 2006/03/16 17:45:01 lievre
467: * imach.c (Module): Comments concerning covariates added
468:
469: * imach.c (Module): refinements in the computation of lli if
470: status=-2 in order to have more reliable computation if stepm is
471: not 1 month. Version 0.98f
472:
473: Revision 1.120 2006/03/16 15:10:38 lievre
474: (Module): refinements in the computation of lli if
475: status=-2 in order to have more reliable computation if stepm is
476: not 1 month. Version 0.98f
477:
478: Revision 1.119 2006/03/15 17:42:26 brouard
479: (Module): Bug if status = -2, the loglikelihood was
480: computed as likelihood omitting the logarithm. Version O.98e
481:
482: Revision 1.118 2006/03/14 18:20:07 brouard
483: (Module): varevsij Comments added explaining the second
484: table of variances if popbased=1 .
485: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
486: (Module): Function pstamp added
487: (Module): Version 0.98d
488:
489: Revision 1.117 2006/03/14 17:16:22 brouard
490: (Module): varevsij Comments added explaining the second
491: table of variances if popbased=1 .
492: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
493: (Module): Function pstamp added
494: (Module): Version 0.98d
495:
496: Revision 1.116 2006/03/06 10:29:27 brouard
497: (Module): Variance-covariance wrong links and
498: varian-covariance of ej. is needed (Saito).
499:
500: Revision 1.115 2006/02/27 12:17:45 brouard
501: (Module): One freematrix added in mlikeli! 0.98c
502:
503: Revision 1.114 2006/02/26 12:57:58 brouard
504: (Module): Some improvements in processing parameter
505: filename with strsep.
506:
507: Revision 1.113 2006/02/24 14:20:24 brouard
508: (Module): Memory leaks checks with valgrind and:
509: datafile was not closed, some imatrix were not freed and on matrix
510: allocation too.
511:
512: Revision 1.112 2006/01/30 09:55:26 brouard
513: (Module): Back to gnuplot.exe instead of wgnuplot.exe
514:
515: Revision 1.111 2006/01/25 20:38:18 brouard
516: (Module): Lots of cleaning and bugs added (Gompertz)
517: (Module): Comments can be added in data file. Missing date values
518: can be a simple dot '.'.
519:
520: Revision 1.110 2006/01/25 00:51:50 brouard
521: (Module): Lots of cleaning and bugs added (Gompertz)
522:
523: Revision 1.109 2006/01/24 19:37:15 brouard
524: (Module): Comments (lines starting with a #) are allowed in data.
525:
526: Revision 1.108 2006/01/19 18:05:42 lievre
527: Gnuplot problem appeared...
528: To be fixed
529:
530: Revision 1.107 2006/01/19 16:20:37 brouard
531: Test existence of gnuplot in imach path
532:
533: Revision 1.106 2006/01/19 13:24:36 brouard
534: Some cleaning and links added in html output
535:
536: Revision 1.105 2006/01/05 20:23:19 lievre
537: *** empty log message ***
538:
539: Revision 1.104 2005/09/30 16:11:43 lievre
540: (Module): sump fixed, loop imx fixed, and simplifications.
541: (Module): If the status is missing at the last wave but we know
542: that the person is alive, then we can code his/her status as -2
543: (instead of missing=-1 in earlier versions) and his/her
544: contributions to the likelihood is 1 - Prob of dying from last
545: health status (= 1-p13= p11+p12 in the easiest case of somebody in
546: the healthy state at last known wave). Version is 0.98
547:
548: Revision 1.103 2005/09/30 15:54:49 lievre
549: (Module): sump fixed, loop imx fixed, and simplifications.
550:
551: Revision 1.102 2004/09/15 17:31:30 brouard
552: Add the possibility to read data file including tab characters.
553:
554: Revision 1.101 2004/09/15 10:38:38 brouard
555: Fix on curr_time
556:
557: Revision 1.100 2004/07/12 18:29:06 brouard
558: Add version for Mac OS X. Just define UNIX in Makefile
559:
560: Revision 1.99 2004/06/05 08:57:40 brouard
561: *** empty log message ***
562:
563: Revision 1.98 2004/05/16 15:05:56 brouard
564: New version 0.97 . First attempt to estimate force of mortality
565: directly from the data i.e. without the need of knowing the health
566: state at each age, but using a Gompertz model: log u =a + b*age .
567: This is the basic analysis of mortality and should be done before any
568: other analysis, in order to test if the mortality estimated from the
569: cross-longitudinal survey is different from the mortality estimated
570: from other sources like vital statistic data.
571:
572: The same imach parameter file can be used but the option for mle should be -3.
573:
1.133 brouard 574: Agnès, who wrote this part of the code, tried to keep most of the
1.126 brouard 575: former routines in order to include the new code within the former code.
576:
577: The output is very simple: only an estimate of the intercept and of
578: the slope with 95% confident intervals.
579:
580: Current limitations:
581: A) Even if you enter covariates, i.e. with the
582: model= V1+V2 equation for example, the programm does only estimate a unique global model without covariates.
583: B) There is no computation of Life Expectancy nor Life Table.
584:
585: Revision 1.97 2004/02/20 13:25:42 lievre
586: Version 0.96d. Population forecasting command line is (temporarily)
587: suppressed.
588:
589: Revision 1.96 2003/07/15 15:38:55 brouard
590: * imach.c (Repository): Errors in subdirf, 2, 3 while printing tmpout is
591: rewritten within the same printf. Workaround: many printfs.
592:
593: Revision 1.95 2003/07/08 07:54:34 brouard
594: * imach.c (Repository):
595: (Repository): Using imachwizard code to output a more meaningful covariance
596: matrix (cov(a12,c31) instead of numbers.
597:
598: Revision 1.94 2003/06/27 13:00:02 brouard
599: Just cleaning
600:
601: Revision 1.93 2003/06/25 16:33:55 brouard
602: (Module): On windows (cygwin) function asctime_r doesn't
603: exist so I changed back to asctime which exists.
604: (Module): Version 0.96b
605:
606: Revision 1.92 2003/06/25 16:30:45 brouard
607: (Module): On windows (cygwin) function asctime_r doesn't
608: exist so I changed back to asctime which exists.
609:
610: Revision 1.91 2003/06/25 15:30:29 brouard
611: * imach.c (Repository): Duplicated warning errors corrected.
612: (Repository): Elapsed time after each iteration is now output. It
613: helps to forecast when convergence will be reached. Elapsed time
614: is stamped in powell. We created a new html file for the graphs
615: concerning matrix of covariance. It has extension -cov.htm.
616:
617: Revision 1.90 2003/06/24 12:34:15 brouard
618: (Module): Some bugs corrected for windows. Also, when
619: mle=-1 a template is output in file "or"mypar.txt with the design
620: of the covariance matrix to be input.
621:
622: Revision 1.89 2003/06/24 12:30:52 brouard
623: (Module): Some bugs corrected for windows. Also, when
624: mle=-1 a template is output in file "or"mypar.txt with the design
625: of the covariance matrix to be input.
626:
627: Revision 1.88 2003/06/23 17:54:56 brouard
628: * 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.
629:
630: Revision 1.87 2003/06/18 12:26:01 brouard
631: Version 0.96
632:
633: Revision 1.86 2003/06/17 20:04:08 brouard
634: (Module): Change position of html and gnuplot routines and added
635: routine fileappend.
636:
637: Revision 1.85 2003/06/17 13:12:43 brouard
638: * imach.c (Repository): Check when date of death was earlier that
639: current date of interview. It may happen when the death was just
640: prior to the death. In this case, dh was negative and likelihood
641: was wrong (infinity). We still send an "Error" but patch by
642: assuming that the date of death was just one stepm after the
643: interview.
644: (Repository): Because some people have very long ID (first column)
645: we changed int to long in num[] and we added a new lvector for
646: memory allocation. But we also truncated to 8 characters (left
647: truncation)
648: (Repository): No more line truncation errors.
649:
650: Revision 1.84 2003/06/13 21:44:43 brouard
651: * imach.c (Repository): Replace "freqsummary" at a correct
652: place. It differs from routine "prevalence" which may be called
653: many times. Probs is memory consuming and must be used with
654: parcimony.
655: Version 0.95a3 (should output exactly the same maximization than 0.8a2)
656:
657: Revision 1.83 2003/06/10 13:39:11 lievre
658: *** empty log message ***
659:
660: Revision 1.82 2003/06/05 15:57:20 brouard
661: Add log in imach.c and fullversion number is now printed.
662:
663: */
664: /*
665: Interpolated Markov Chain
666:
667: Short summary of the programme:
668:
1.227 brouard 669: This program computes Healthy Life Expectancies or State-specific
670: (if states aren't health statuses) Expectancies from
671: cross-longitudinal data. Cross-longitudinal data consist in:
672:
673: -1- a first survey ("cross") where individuals from different ages
674: are interviewed on their health status or degree of disability (in
675: the case of a health survey which is our main interest)
676:
677: -2- at least a second wave of interviews ("longitudinal") which
678: measure each change (if any) in individual health status. Health
679: expectancies are computed from the time spent in each health state
680: according to a model. More health states you consider, more time is
681: necessary to reach the Maximum Likelihood of the parameters involved
682: in the model. The simplest model is the multinomial logistic model
683: where pij is the probability to be observed in state j at the second
684: wave conditional to be observed in state i at the first
685: wave. Therefore the model is: log(pij/pii)= aij + bij*age+ cij*sex +
686: etc , where 'age' is age and 'sex' is a covariate. If you want to
687: have a more complex model than "constant and age", you should modify
688: the program where the markup *Covariates have to be included here
689: again* invites you to do it. More covariates you add, slower the
1.126 brouard 690: convergence.
691:
692: The advantage of this computer programme, compared to a simple
693: multinomial logistic model, is clear when the delay between waves is not
694: identical for each individual. Also, if a individual missed an
695: intermediate interview, the information is lost, but taken into
696: account using an interpolation or extrapolation.
697:
698: hPijx is the probability to be observed in state i at age x+h
699: conditional to the observed state i at age x. The delay 'h' can be
700: split into an exact number (nh*stepm) of unobserved intermediate
701: states. This elementary transition (by month, quarter,
702: semester or year) is modelled as a multinomial logistic. The hPx
703: matrix is simply the matrix product of nh*stepm elementary matrices
704: and the contribution of each individual to the likelihood is simply
705: hPijx.
706:
707: Also this programme outputs the covariance matrix of the parameters but also
1.218 brouard 708: of the life expectancies. It also computes the period (stable) prevalence.
709:
710: Back prevalence and projections:
1.227 brouard 711:
712: - back_prevalence_limit(double *p, double **bprlim, double ageminpar,
713: double agemaxpar, double ftolpl, int *ncvyearp, double
714: dateprev1,double dateprev2, int firstpass, int lastpass, int
715: mobilavproj)
716:
717: Computes the back prevalence limit for any combination of
718: covariate values k at any age between ageminpar and agemaxpar and
719: returns it in **bprlim. In the loops,
720:
721: - **bprevalim(**bprlim, ***mobaverage, nlstate, *p, age, **oldm,
722: **savm, **dnewm, **doldm, **dsavm, ftolpl, ncvyearp, k);
723:
724: - hBijx Back Probability to be in state i at age x-h being in j at x
1.218 brouard 725: Computes for any combination of covariates k and any age between bage and fage
726: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
727: oldm=oldms;savm=savms;
1.227 brouard 728:
729: - hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
1.218 brouard 730: Computes the transition matrix starting at age 'age' over
731: 'nhstepm*hstepm*stepm' months (i.e. until
732: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
1.227 brouard 733: nhstepm*hstepm matrices.
734:
735: Returns p3mat[i][j][h] after calling
736: p3mat[i][j][h]=matprod2(newm,
737: bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm,
738: dsavm,ij),\ 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
739: oldm);
1.226 brouard 740:
741: Important routines
742:
743: - func (or funcone), computes logit (pij) distinguishing
744: o fixed variables (single or product dummies or quantitative);
745: o varying variables by:
746: (1) wave (single, product dummies, quantitative),
747: (2) by age (can be month) age (done), age*age (done), age*Vn where Vn can be:
748: % fixed dummy (treated) or quantitative (not done because time-consuming);
749: % varying dummy (not done) or quantitative (not done);
750: - Tricode which tests the modality of dummy variables (in order to warn with wrong or empty modalities)
751: and returns the number of efficient covariates cptcoveff and modalities nbcode[Tvar[k]][1]= 0 and nbcode[Tvar[k]][2]= 1 usually.
752: - printinghtml which outputs results like life expectancy in and from a state for a combination of modalities of dummy variables
753: o There are 2*cptcoveff combinations of (0,1) for cptcoveff variables. Outputting only combinations with people, éliminating 1 1 if
754: race White (0 0), Black vs White (1 0), Hispanic (0 1) and 1 1 being meaningless.
1.218 brouard 755:
1.226 brouard 756:
757:
1.133 brouard 758: Authors: Nicolas Brouard (brouard@ined.fr) and Agnès Lièvre (lievre@ined.fr).
759: Institut national d'études démographiques, Paris.
1.126 brouard 760: This software have been partly granted by Euro-REVES, a concerted action
761: from the European Union.
762: It is copyrighted identically to a GNU software product, ie programme and
763: software can be distributed freely for non commercial use. Latest version
764: can be accessed at http://euroreves.ined.fr/imach .
765:
766: Help to debug: LD_PRELOAD=/usr/local/lib/libnjamd.so ./imach foo.imach
767: or better on gdb : set env LD_PRELOAD=/usr/local/lib/libnjamd.so
768:
769: **********************************************************************/
770: /*
771: main
772: read parameterfile
773: read datafile
774: concatwav
775: freqsummary
776: if (mle >= 1)
777: mlikeli
778: print results files
779: if mle==1
780: computes hessian
781: read end of parameter file: agemin, agemax, bage, fage, estepm
782: begin-prev-date,...
783: open gnuplot file
784: open html file
1.145 brouard 785: period (stable) prevalence | pl_nom 1-1 2-2 etc by covariate
786: for age prevalim() | #****** V1=0 V2=1 V3=1 V4=0 ******
787: | 65 1 0 2 1 3 1 4 0 0.96326 0.03674
788: freexexit2 possible for memory heap.
789:
790: h Pij x | pij_nom ficrestpij
791: # Cov Agex agex+h hpijx with i,j= 1-1 1-2 1-3 2-1 2-2 2-3
792: 1 85 85 1.00000 0.00000 0.00000 0.00000 1.00000 0.00000
793: 1 85 86 0.68299 0.22291 0.09410 0.71093 0.00000 0.28907
794:
795: 1 65 99 0.00364 0.00322 0.99314 0.00350 0.00310 0.99340
796: 1 65 100 0.00214 0.00204 0.99581 0.00206 0.00196 0.99597
797: variance of p one-step probabilities varprob | prob_nom ficresprob #One-step probabilities and stand. devi in ()
798: Standard deviation of one-step probabilities | probcor_nom ficresprobcor #One-step probabilities and correlation matrix
799: Matrix of variance covariance of one-step probabilities | probcov_nom ficresprobcov #One-step probabilities and covariance matrix
800:
1.126 brouard 801: forecasting if prevfcast==1 prevforecast call prevalence()
802: health expectancies
803: Variance-covariance of DFLE
804: prevalence()
805: movingaverage()
806: varevsij()
807: if popbased==1 varevsij(,popbased)
808: total life expectancies
809: Variance of period (stable) prevalence
810: end
811: */
812:
1.187 brouard 813: /* #define DEBUG */
814: /* #define DEBUGBRENT */
1.203 brouard 815: /* #define DEBUGLINMIN */
816: /* #define DEBUGHESS */
817: #define DEBUGHESSIJ
1.224 brouard 818: /* #define LINMINORIGINAL /\* Don't use loop on scale in linmin (accepting nan) *\/ */
1.165 brouard 819: #define POWELL /* Instead of NLOPT */
1.224 brouard 820: #define POWELLNOF3INFF1TEST /* Skip test */
1.186 brouard 821: /* #define POWELLORIGINAL /\* Don't use Directest to decide new direction but original Powell test *\/ */
822: /* #define MNBRAKORIGINAL /\* Don't use mnbrak fix *\/ */
1.126 brouard 823:
824: #include <math.h>
825: #include <stdio.h>
826: #include <stdlib.h>
827: #include <string.h>
1.226 brouard 828: #include <ctype.h>
1.159 brouard 829:
830: #ifdef _WIN32
831: #include <io.h>
1.172 brouard 832: #include <windows.h>
833: #include <tchar.h>
1.159 brouard 834: #else
1.126 brouard 835: #include <unistd.h>
1.159 brouard 836: #endif
1.126 brouard 837:
838: #include <limits.h>
839: #include <sys/types.h>
1.171 brouard 840:
841: #if defined(__GNUC__)
842: #include <sys/utsname.h> /* Doesn't work on Windows */
843: #endif
844:
1.126 brouard 845: #include <sys/stat.h>
846: #include <errno.h>
1.159 brouard 847: /* extern int errno; */
1.126 brouard 848:
1.157 brouard 849: /* #ifdef LINUX */
850: /* #include <time.h> */
851: /* #include "timeval.h" */
852: /* #else */
853: /* #include <sys/time.h> */
854: /* #endif */
855:
1.126 brouard 856: #include <time.h>
857:
1.136 brouard 858: #ifdef GSL
859: #include <gsl/gsl_errno.h>
860: #include <gsl/gsl_multimin.h>
861: #endif
862:
1.167 brouard 863:
1.162 brouard 864: #ifdef NLOPT
865: #include <nlopt.h>
866: typedef struct {
867: double (* function)(double [] );
868: } myfunc_data ;
869: #endif
870:
1.126 brouard 871: /* #include <libintl.h> */
872: /* #define _(String) gettext (String) */
873:
1.141 brouard 874: #define MAXLINE 1024 /* Was 256. Overflow with 312 with 2 states and 4 covariates. Should be ok */
1.126 brouard 875:
876: #define GNUPLOTPROGRAM "gnuplot"
877: /*#define GNUPLOTPROGRAM "..\\gp37mgw\\wgnuplot"*/
878: #define FILENAMELENGTH 132
879:
880: #define GLOCK_ERROR_NOPATH -1 /* empty path */
881: #define GLOCK_ERROR_GETCWD -2 /* cannot get cwd */
882:
1.144 brouard 883: #define MAXPARM 128 /**< Maximum number of parameters for the optimization */
884: #define NPARMAX 64 /**< (nlstate+ndeath-1)*nlstate*ncovmodel */
1.126 brouard 885:
886: #define NINTERVMAX 8
1.144 brouard 887: #define NLSTATEMAX 8 /**< Maximum number of live states (for func) */
888: #define NDEATHMAX 8 /**< Maximum number of dead states (for func) */
889: #define NCOVMAX 20 /**< Maximum number of covariates, including generated covariates V1*V2 */
1.197 brouard 890: #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1.211 brouard 891: /*#define decodtabm(h,k,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (k-1)) & 1) +1 : -1)*/
892: #define decodtabm(h,k,cptcoveff) (((h-1) >> (k-1)) & 1) +1
1.126 brouard 893: #define MAXN 20000
1.144 brouard 894: #define YEARM 12. /**< Number of months per year */
1.218 brouard 895: /* #define AGESUP 130 */
896: #define AGESUP 150
897: #define AGEMARGE 25 /* Marge for agemin and agemax for(iage=agemin-AGEMARGE; iage <= agemax+3+AGEMARGE; iage++) */
1.126 brouard 898: #define AGEBASE 40
1.194 brouard 899: #define AGEOVERFLOW 1.e20
1.164 brouard 900: #define AGEGOMP 10 /**< Minimal age for Gompertz adjustment */
1.157 brouard 901: #ifdef _WIN32
902: #define DIRSEPARATOR '\\'
903: #define CHARSEPARATOR "\\"
904: #define ODIRSEPARATOR '/'
905: #else
1.126 brouard 906: #define DIRSEPARATOR '/'
907: #define CHARSEPARATOR "/"
908: #define ODIRSEPARATOR '\\'
909: #endif
910:
1.237 ! brouard 911: /* $Id: imach.c,v 1.236 2016/08/25 10:50:18 brouard Exp $ */
1.126 brouard 912: /* $State: Exp $ */
1.196 brouard 913: #include "version.h"
914: char version[]=__IMACH_VERSION__;
1.224 brouard 915: char copyright[]="February 2016,INED-EUROREVES-Institut de longevite-Japan Society for the Promotion of Science (Grant-in-Aid for Scientific Research 25293121), Intel Software 2015-2018";
1.237 ! brouard 916: char fullversion[]="$Revision: 1.236 $ $Date: 2016/08/25 10:50:18 $";
1.126 brouard 917: char strstart[80];
918: char optionfilext[10], optionfilefiname[FILENAMELENGTH];
1.130 brouard 919: int erreur=0, nberr=0, nbwarn=0; /* Error number, number of errors number of warnings */
1.187 brouard 920: int nagesqr=0, nforce=0; /* nagesqr=1 if model is including age*age, number of forces */
1.145 brouard 921: /* Number of covariates model=V2+V1+ V3*age+V2*V4 */
922: int cptcovn=0; /**< cptcovn number of covariates added in the model (excepting constant and age and age*product) */
923: int cptcovt=0; /**< cptcovt number of covariates added in the model (excepting constant and age) */
1.225 brouard 924: int cptcovs=0; /**< cptcovs number of simple covariates in the model V2+V1 =2 */
925: int cptcovsnq=0; /**< cptcovsnq number of simple covariates in the model but non quantitative V2+V1 =2 */
1.145 brouard 926: int cptcovage=0; /**< Number of covariates with age: V3*age only =1 */
927: int cptcovprodnoage=0; /**< Number of covariate products without age */
928: int cptcoveff=0; /* Total number of covariates to vary for printing results */
1.233 brouard 929: int ncovf=0; /* Total number of effective fixed covariates (dummy or quantitative) in the model */
930: int ncovv=0; /* Total number of effective (wave) varying covariates (dummy or quantitative) in the model */
1.232 brouard 931: int ncova=0; /* Total number of effective (wave and stepm) varying with age covariates (dummy of quantitative) in the model */
1.234 brouard 932: int nsd=0; /**< Total number of single dummy variables (output) */
933: int nsq=0; /**< Total number of single quantitative variables (output) */
1.232 brouard 934: int ncoveff=0; /* Total number of effective fixed dummy covariates in the model */
1.225 brouard 935: int nqfveff=0; /**< nqfveff Number of Quantitative Fixed Variables Effective */
1.224 brouard 936: int ntveff=0; /**< ntveff number of effective time varying variables */
937: int nqtveff=0; /**< ntqveff number of effective time varying quantitative variables */
1.145 brouard 938: int cptcov=0; /* Working variable */
1.218 brouard 939: int ncovcombmax=NCOVMAX; /* Maximum calculated number of covariate combination = pow(2, cptcoveff) */
1.126 brouard 940: int npar=NPARMAX;
941: int nlstate=2; /* Number of live states */
942: int ndeath=1; /* Number of dead states */
1.130 brouard 943: int ncovmodel=0, ncovcol=0; /* Total number of covariables including constant a12*1 +b12*x ncovmodel=2 */
1.223 brouard 944: int nqv=0, ntv=0, nqtv=0; /* Total number of quantitative variables, time variable (dummy), quantitative and time variable */
1.126 brouard 945: int popbased=0;
946:
947: int *wav; /* Number of waves for this individuual 0 is possible */
1.130 brouard 948: int maxwav=0; /* Maxim number of waves */
949: int jmin=0, jmax=0; /* min, max spacing between 2 waves */
950: int ijmin=0, ijmax=0; /* Individuals having jmin and jmax */
951: int gipmx=0, gsw=0; /* Global variables on the number of contributions
1.126 brouard 952: to the likelihood and the sum of weights (done by funcone)*/
1.130 brouard 953: int mle=1, weightopt=0;
1.126 brouard 954: int **mw; /* mw[mi][i] is number of the mi wave for this individual */
955: int **dh; /* dh[mi][i] is number of steps between mi,mi+1 for this individual */
956: int **bh; /* bh[mi][i] is the bias (+ or -) for this individual if the delay between
957: * wave mi and wave mi+1 is not an exact multiple of stepm. */
1.162 brouard 958: int countcallfunc=0; /* Count the number of calls to func */
1.230 brouard 959: int selected(int kvar); /* Is covariate kvar selected for printing results */
960:
1.130 brouard 961: double jmean=1; /* Mean space between 2 waves */
1.145 brouard 962: double **matprod2(); /* test */
1.126 brouard 963: double **oldm, **newm, **savm; /* Working pointers to matrices */
964: double **oldms, **newms, **savms; /* Fixed working pointers to matrices */
1.218 brouard 965: double **ddnewms, **ddoldms, **ddsavms; /* for freeing later */
966:
1.136 brouard 967: /*FILE *fic ; */ /* Used in readdata only */
1.217 brouard 968: FILE *ficpar, *ficparo,*ficres, *ficresp, *ficresphtm, *ficresphtmfr, *ficrespl, *ficresplb,*ficrespij, *ficrespijb, *ficrest,*ficresf, *ficresfb,*ficrespop;
1.126 brouard 969: FILE *ficlog, *ficrespow;
1.130 brouard 970: int globpr=0; /* Global variable for printing or not */
1.126 brouard 971: double fretone; /* Only one call to likelihood */
1.130 brouard 972: long ipmx=0; /* Number of contributions */
1.126 brouard 973: double sw; /* Sum of weights */
974: char filerespow[FILENAMELENGTH];
975: char fileresilk[FILENAMELENGTH]; /* File of individual contributions to the likelihood */
976: FILE *ficresilk;
977: FILE *ficgp,*ficresprob,*ficpop, *ficresprobcov, *ficresprobcor;
978: FILE *ficresprobmorprev;
979: FILE *fichtm, *fichtmcov; /* Html File */
980: FILE *ficreseij;
981: char filerese[FILENAMELENGTH];
982: FILE *ficresstdeij;
983: char fileresstde[FILENAMELENGTH];
984: FILE *ficrescveij;
985: char filerescve[FILENAMELENGTH];
986: FILE *ficresvij;
987: char fileresv[FILENAMELENGTH];
988: FILE *ficresvpl;
989: char fileresvpl[FILENAMELENGTH];
990: char title[MAXLINE];
1.234 brouard 991: char model[MAXLINE]; /**< The model line */
1.217 brouard 992: char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH], filerespl[FILENAMELENGTH], fileresplb[FILENAMELENGTH];
1.126 brouard 993: char plotcmd[FILENAMELENGTH], pplotcmd[FILENAMELENGTH];
994: char tmpout[FILENAMELENGTH], tmpout2[FILENAMELENGTH];
995: char command[FILENAMELENGTH];
996: int outcmd=0;
997:
1.217 brouard 998: char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filerespijb[FILENAMELENGTH], filereso[FILENAMELENGTH], rfileres[FILENAMELENGTH];
1.202 brouard 999: char fileresu[FILENAMELENGTH]; /* fileres without r in front */
1.126 brouard 1000: char filelog[FILENAMELENGTH]; /* Log file */
1001: char filerest[FILENAMELENGTH];
1002: char fileregp[FILENAMELENGTH];
1003: char popfile[FILENAMELENGTH];
1004:
1005: char optionfilegnuplot[FILENAMELENGTH], optionfilehtm[FILENAMELENGTH], optionfilehtmcov[FILENAMELENGTH] ;
1006:
1.157 brouard 1007: /* struct timeval start_time, end_time, curr_time, last_time, forecast_time; */
1008: /* struct timezone tzp; */
1009: /* extern int gettimeofday(); */
1010: struct tm tml, *gmtime(), *localtime();
1011:
1012: extern time_t time();
1013:
1014: struct tm start_time, end_time, curr_time, last_time, forecast_time;
1015: time_t rstart_time, rend_time, rcurr_time, rlast_time, rforecast_time; /* raw time */
1016: struct tm tm;
1017:
1.126 brouard 1018: char strcurr[80], strfor[80];
1019:
1020: char *endptr;
1021: long lval;
1022: double dval;
1023:
1024: #define NR_END 1
1025: #define FREE_ARG char*
1026: #define FTOL 1.0e-10
1027:
1028: #define NRANSI
1029: #define ITMAX 200
1030:
1031: #define TOL 2.0e-4
1032:
1033: #define CGOLD 0.3819660
1034: #define ZEPS 1.0e-10
1035: #define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d);
1036:
1037: #define GOLD 1.618034
1038: #define GLIMIT 100.0
1039: #define TINY 1.0e-20
1040:
1041: static double maxarg1,maxarg2;
1042: #define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)>(maxarg2)? (maxarg1):(maxarg2))
1043: #define FMIN(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)<(maxarg2)? (maxarg1):(maxarg2))
1044:
1045: #define SIGN(a,b) ((b)>0.0 ? fabs(a) : -fabs(a))
1046: #define rint(a) floor(a+0.5)
1.166 brouard 1047: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/myutils_8h-source.html */
1.183 brouard 1048: #define mytinydouble 1.0e-16
1.166 brouard 1049: /* #define DEQUAL(a,b) (fabs((a)-(b))<mytinydouble) */
1050: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/mynrutils_8h-source.html */
1051: /* static double dsqrarg; */
1052: /* #define DSQR(a) (DEQUAL((dsqrarg=(a)),0.0) ? 0.0 : dsqrarg*dsqrarg) */
1.126 brouard 1053: static double sqrarg;
1054: #define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 :sqrarg*sqrarg)
1055: #define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;}
1056: int agegomp= AGEGOMP;
1057:
1058: int imx;
1059: int stepm=1;
1060: /* Stepm, step in month: minimum step interpolation*/
1061:
1062: int estepm;
1063: /* Estepm, step in month to interpolate survival function in order to approximate Life Expectancy*/
1064:
1065: int m,nb;
1066: long *num;
1.197 brouard 1067: int firstpass=0, lastpass=4,*cod, *cens;
1.192 brouard 1068: int *ncodemax; /* ncodemax[j]= Number of modalities of the j th
1069: covariate for which somebody answered excluding
1070: undefined. Usually 2: 0 and 1. */
1071: int *ncodemaxwundef; /* ncodemax[j]= Number of modalities of the j th
1072: covariate for which somebody answered including
1073: undefined. Usually 3: -1, 0 and 1. */
1.126 brouard 1074: double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint;
1.218 brouard 1075: double **pmmij, ***probs; /* Global pointer */
1.219 brouard 1076: double ***mobaverage, ***mobaverages; /* New global variable */
1.126 brouard 1077: double *ageexmed,*agecens;
1078: double dateintmean=0;
1079:
1080: double *weight;
1081: int **s; /* Status */
1.141 brouard 1082: double *agedc;
1.145 brouard 1083: double **covar; /**< covar[j,i], value of jth covariate for individual i,
1.141 brouard 1084: * covar=matrix(0,NCOVMAX,1,n);
1.187 brouard 1085: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*age; */
1.225 brouard 1086: double **coqvar; /* Fixed quantitative covariate iqv */
1087: double ***cotvar; /* Time varying covariate itv */
1088: double ***cotqvar; /* Time varying quantitative covariate itqv */
1.141 brouard 1089: double idx;
1090: int **nbcode, *Tvar; /**< model=V2 => Tvar[1]= 2 */
1.234 brouard 1091: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1092: /*k 1 2 3 4 5 6 7 8 9 */
1093: /*Tvar[k]= 5 4 3 6 5 2 7 1 1 */
1094: /* Tndvar[k] 1 2 3 4 5 */
1095: /*TDvar 4 3 6 7 1 */ /* For outputs only; combination of dummies fixed or varying */
1096: /* Tns[k] 1 2 2 4 5 */ /* Number of single cova */
1097: /* TvarsD[k] 1 2 3 */ /* Number of single dummy cova */
1098: /* TvarsDind 2 3 9 */ /* position K of single dummy cova */
1099: /* TvarsQ[k] 1 2 */ /* Number of single quantitative cova */
1100: /* TvarsQind 1 6 */ /* position K of single quantitative cova */
1101: /* Tprod[i]=k 4 7 */
1102: /* Tage[i]=k 5 8 */
1103: /* */
1104: /* Type */
1105: /* V 1 2 3 4 5 */
1106: /* F F V V V */
1107: /* D Q D D Q */
1108: /* */
1109: int *TvarsD;
1110: int *TvarsDind;
1111: int *TvarsQ;
1112: int *TvarsQind;
1113:
1.235 brouard 1114: #define MAXRESULTLINES 10
1115: int nresult=0;
1116: int TKresult[MAXRESULTLINES];
1.237 ! brouard 1117: int Tresult[MAXRESULTLINES][NCOVMAX];/* For dummy variable , value (output) */
! 1118: int Tinvresult[MAXRESULTLINES][NCOVMAX];/* For dummy variable , value (output) */
1.235 brouard 1119: int Tvresult[MAXRESULTLINES][NCOVMAX]; /* For dummy variable , variable # (output) */
1120: double Tqresult[MAXRESULTLINES][NCOVMAX]; /* For quantitative variable , value (output) */
1.237 ! brouard 1121: double Tqinvresult[MAXRESULTLINES][NCOVMAX]; /* For quantitative variable , value (output) */
1.235 brouard 1122: int Tvqresult[MAXRESULTLINES][NCOVMAX]; /* For quantitative variable , variable # (output) */
1123:
1.234 brouard 1124: /* int *TDvar; /\**< TDvar[1]=4, TDvarF[2]=3, TDvar[3]=6 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 *\/ */
1.232 brouard 1125: int *TvarF; /**< TvarF[1]=Tvar[6]=2, TvarF[2]=Tvar[7]=7, TvarF[3]=Tvar[9]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1126: int *TvarFind; /**< TvarFind[1]=6, TvarFind[2]=7, Tvarind[3]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1127: int *TvarV; /**< TvarV[1]=Tvar[1]=5, TvarV[2]=Tvar[2]=4 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1128: int *TvarVind; /**< TvarVind[1]=1, TvarVind[2]=2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1129: int *TvarA; /**< TvarA[1]=Tvar[5]=5, TvarA[2]=Tvar[8]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1130: int *TvarAind; /**< TvarindA[1]=5, TvarAind[2]=8 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1.231 brouard 1131: int *TvarFD; /**< TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1132: int *TvarFDind; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1133: int *TvarFQ; /* TvarFQ[1]=V2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1134: int *TvarFQind; /* TvarFQind[1]=6 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1135: int *TvarVD; /* TvarVD[1]=V5 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1136: int *TvarVDind; /* TvarVDind[1]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1137: int *TvarVQ; /* TvarVQ[1]=V5 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple time varying quantitative variable */
1138: int *TvarVQind; /* TvarVQind[1]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple time varying quantitative variable */
1139:
1.230 brouard 1140: int *Tvarsel; /**< Selected covariates for output */
1141: double *Tvalsel; /**< Selected modality value of covariate for output */
1.226 brouard 1142: int *Typevar; /**< 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product */
1.227 brouard 1143: int *Fixed; /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
1144: int *Dummy; /** Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product */
1.197 brouard 1145: int *Tage;
1.227 brouard 1146: int anyvaryingduminmodel=0; /**< Any varying dummy in Model=1 yes, 0 no, to avoid a loop on waves in freq */
1.228 brouard 1147: int *Tmodelind; /** Tmodelind[Tvaraff[3]]=9 for V1 position,Tvaraff[1]@9={4, 3, 1, 0, 0, 0, 0, 0, 0}, model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1*/
1.230 brouard 1148: int *TmodelInvind; /** Tmodelind[Tvaraff[3]]=9 for V1 position,Tvaraff[1]@9={4, 3, 1, 0, 0, 0, 0, 0, 0}, model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1*/
1149: int *TmodelInvQind; /** Tmodelqind[1]=1 for V5(quantitative varying) position,Tvaraff[1]@9={4, 3, 1, 0, 0, 0, 0, 0, 0}, model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1.145 brouard 1150: int *Ndum; /** Freq of modality (tricode */
1.200 brouard 1151: /* int **codtab;*/ /**< codtab=imatrix(1,100,1,10); */
1.227 brouard 1152: int **Tvard;
1153: int *Tprod;/**< Gives the k position of the k1 product */
1154: int *Tposprod; /**< Gives the k1 product from the k position */
1155: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3
1156: if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2)
1157: Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5]=2
1158: */
1159: int cptcovprod, *Tvaraff, *invalidvarcomb;
1.126 brouard 1160: double *lsurv, *lpop, *tpop;
1161:
1.231 brouard 1162: #define FD 1; /* Fixed dummy covariate */
1163: #define FQ 2; /* Fixed quantitative covariate */
1164: #define FP 3; /* Fixed product covariate */
1165: #define FPDD 7; /* Fixed product dummy*dummy covariate */
1166: #define FPDQ 8; /* Fixed product dummy*quantitative covariate */
1167: #define FPQQ 9; /* Fixed product quantitative*quantitative covariate */
1168: #define VD 10; /* Varying dummy covariate */
1169: #define VQ 11; /* Varying quantitative covariate */
1170: #define VP 12; /* Varying product covariate */
1171: #define VPDD 13; /* Varying product dummy*dummy covariate */
1172: #define VPDQ 14; /* Varying product dummy*quantitative covariate */
1173: #define VPQQ 15; /* Varying product quantitative*quantitative covariate */
1174: #define APFD 16; /* Age product * fixed dummy covariate */
1175: #define APFQ 17; /* Age product * fixed quantitative covariate */
1176: #define APVD 18; /* Age product * varying dummy covariate */
1177: #define APVQ 19; /* Age product * varying quantitative covariate */
1178:
1179: #define FTYPE 1; /* Fixed covariate */
1180: #define VTYPE 2; /* Varying covariate (loop in wave) */
1181: #define ATYPE 2; /* Age product covariate (loop in dh within wave)*/
1182:
1183: struct kmodel{
1184: int maintype; /* main type */
1185: int subtype; /* subtype */
1186: };
1187: struct kmodel modell[NCOVMAX];
1188:
1.143 brouard 1189: double ftol=FTOL; /**< Tolerance for computing Max Likelihood */
1190: double ftolhess; /**< Tolerance for computing hessian */
1.126 brouard 1191:
1192: /**************** split *************************/
1193: static int split( char *path, char *dirc, char *name, char *ext, char *finame )
1194: {
1195: /* From a file name with (full) path (either Unix or Windows) we extract the directory (dirc)
1196: the name of the file (name), its extension only (ext) and its first part of the name (finame)
1197: */
1198: char *ss; /* pointer */
1.186 brouard 1199: int l1=0, l2=0; /* length counters */
1.126 brouard 1200:
1201: l1 = strlen(path ); /* length of path */
1202: if ( l1 == 0 ) return( GLOCK_ERROR_NOPATH );
1203: ss= strrchr( path, DIRSEPARATOR ); /* find last / */
1204: if ( ss == NULL ) { /* no directory, so determine current directory */
1205: strcpy( name, path ); /* we got the fullname name because no directory */
1206: /*if(strrchr(path, ODIRSEPARATOR )==NULL)
1207: printf("Warning you should use %s as a separator\n",DIRSEPARATOR);*/
1208: /* get current working directory */
1209: /* extern char* getcwd ( char *buf , int len);*/
1.184 brouard 1210: #ifdef WIN32
1211: if (_getcwd( dirc, FILENAME_MAX ) == NULL ) {
1212: #else
1213: if (getcwd(dirc, FILENAME_MAX) == NULL) {
1214: #endif
1.126 brouard 1215: return( GLOCK_ERROR_GETCWD );
1216: }
1217: /* got dirc from getcwd*/
1218: printf(" DIRC = %s \n",dirc);
1.205 brouard 1219: } else { /* strip directory from path */
1.126 brouard 1220: ss++; /* after this, the filename */
1221: l2 = strlen( ss ); /* length of filename */
1222: if ( l2 == 0 ) return( GLOCK_ERROR_NOPATH );
1223: strcpy( name, ss ); /* save file name */
1224: strncpy( dirc, path, l1 - l2 ); /* now the directory */
1.186 brouard 1225: dirc[l1-l2] = '\0'; /* add zero */
1.126 brouard 1226: printf(" DIRC2 = %s \n",dirc);
1227: }
1228: /* We add a separator at the end of dirc if not exists */
1229: l1 = strlen( dirc ); /* length of directory */
1230: if( dirc[l1-1] != DIRSEPARATOR ){
1231: dirc[l1] = DIRSEPARATOR;
1232: dirc[l1+1] = 0;
1233: printf(" DIRC3 = %s \n",dirc);
1234: }
1235: ss = strrchr( name, '.' ); /* find last / */
1236: if (ss >0){
1237: ss++;
1238: strcpy(ext,ss); /* save extension */
1239: l1= strlen( name);
1240: l2= strlen(ss)+1;
1241: strncpy( finame, name, l1-l2);
1242: finame[l1-l2]= 0;
1243: }
1244:
1245: return( 0 ); /* we're done */
1246: }
1247:
1248:
1249: /******************************************/
1250:
1251: void replace_back_to_slash(char *s, char*t)
1252: {
1253: int i;
1254: int lg=0;
1255: i=0;
1256: lg=strlen(t);
1257: for(i=0; i<= lg; i++) {
1258: (s[i] = t[i]);
1259: if (t[i]== '\\') s[i]='/';
1260: }
1261: }
1262:
1.132 brouard 1263: char *trimbb(char *out, char *in)
1.137 brouard 1264: { /* Trim multiple blanks in line but keeps first blanks if line starts with blanks */
1.132 brouard 1265: char *s;
1266: s=out;
1267: while (*in != '\0'){
1.137 brouard 1268: while( *in == ' ' && *(in+1) == ' '){ /* && *(in+1) != '\0'){*/
1.132 brouard 1269: in++;
1270: }
1271: *out++ = *in++;
1272: }
1273: *out='\0';
1274: return s;
1275: }
1276:
1.187 brouard 1277: /* char *substrchaine(char *out, char *in, char *chain) */
1278: /* { */
1279: /* /\* Substract chain 'chain' from 'in', return and output 'out' *\/ */
1280: /* char *s, *t; */
1281: /* t=in;s=out; */
1282: /* while ((*in != *chain) && (*in != '\0')){ */
1283: /* *out++ = *in++; */
1284: /* } */
1285:
1286: /* /\* *in matches *chain *\/ */
1287: /* while ((*in++ == *chain++) && (*in != '\0')){ */
1288: /* printf("*in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1289: /* } */
1290: /* in--; chain--; */
1291: /* while ( (*in != '\0')){ */
1292: /* printf("Bef *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1293: /* *out++ = *in++; */
1294: /* printf("Aft *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1295: /* } */
1296: /* *out='\0'; */
1297: /* out=s; */
1298: /* return out; */
1299: /* } */
1300: char *substrchaine(char *out, char *in, char *chain)
1301: {
1302: /* Substract chain 'chain' from 'in', return and output 'out' */
1303: /* in="V1+V1*age+age*age+V2", chain="age*age" */
1304:
1305: char *strloc;
1306:
1307: strcpy (out, in);
1308: strloc = strstr(out, chain); /* strloc points to out at age*age+V2 */
1309: printf("Bef strloc=%s chain=%s out=%s \n", strloc, chain, out);
1310: if(strloc != NULL){
1311: /* will affect out */ /* strloc+strlenc(chain)=+V2 */ /* Will also work in Unicode */
1312: memmove(strloc,strloc+strlen(chain), strlen(strloc+strlen(chain))+1);
1313: /* strcpy (strloc, strloc +strlen(chain));*/
1314: }
1315: printf("Aft strloc=%s chain=%s in=%s out=%s \n", strloc, chain, in, out);
1316: return out;
1317: }
1318:
1319:
1.145 brouard 1320: char *cutl(char *blocc, char *alocc, char *in, char occ)
1321: {
1.187 brouard 1322: /* cuts string in into blocc and alocc where blocc ends before FIRST occurence of char 'occ'
1.145 brouard 1323: and alocc starts after first occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
1.187 brouard 1324: gives blocc="abcdef" and alocc="ghi2j".
1.145 brouard 1325: If occ is not found blocc is null and alocc is equal to in. Returns blocc
1326: */
1.160 brouard 1327: char *s, *t;
1.145 brouard 1328: t=in;s=in;
1329: while ((*in != occ) && (*in != '\0')){
1330: *alocc++ = *in++;
1331: }
1332: if( *in == occ){
1333: *(alocc)='\0';
1334: s=++in;
1335: }
1336:
1337: if (s == t) {/* occ not found */
1338: *(alocc-(in-s))='\0';
1339: in=s;
1340: }
1341: while ( *in != '\0'){
1342: *blocc++ = *in++;
1343: }
1344:
1345: *blocc='\0';
1346: return t;
1347: }
1.137 brouard 1348: char *cutv(char *blocc, char *alocc, char *in, char occ)
1349: {
1.187 brouard 1350: /* cuts string in into blocc and alocc where blocc ends before LAST occurence of char 'occ'
1.137 brouard 1351: and alocc starts after last occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
1352: gives blocc="abcdef2ghi" and alocc="j".
1353: If occ is not found blocc is null and alocc is equal to in. Returns alocc
1354: */
1355: char *s, *t;
1356: t=in;s=in;
1357: while (*in != '\0'){
1358: while( *in == occ){
1359: *blocc++ = *in++;
1360: s=in;
1361: }
1362: *blocc++ = *in++;
1363: }
1364: if (s == t) /* occ not found */
1365: *(blocc-(in-s))='\0';
1366: else
1367: *(blocc-(in-s)-1)='\0';
1368: in=s;
1369: while ( *in != '\0'){
1370: *alocc++ = *in++;
1371: }
1372:
1373: *alocc='\0';
1374: return s;
1375: }
1376:
1.126 brouard 1377: int nbocc(char *s, char occ)
1378: {
1379: int i,j=0;
1380: int lg=20;
1381: i=0;
1382: lg=strlen(s);
1383: for(i=0; i<= lg; i++) {
1.234 brouard 1384: if (s[i] == occ ) j++;
1.126 brouard 1385: }
1386: return j;
1387: }
1388:
1.137 brouard 1389: /* void cutv(char *u,char *v, char*t, char occ) */
1390: /* { */
1391: /* /\* cuts string t into u and v where u ends before last occurence of char 'occ' */
1392: /* and v starts after last occurence of char 'occ' : ex cutv(u,v,"abcdef2ghi2j",'2') */
1393: /* gives u="abcdef2ghi" and v="j" *\/ */
1394: /* int i,lg,j,p=0; */
1395: /* i=0; */
1396: /* lg=strlen(t); */
1397: /* for(j=0; j<=lg-1; j++) { */
1398: /* if((t[j]!= occ) && (t[j+1]== occ)) p=j+1; */
1399: /* } */
1.126 brouard 1400:
1.137 brouard 1401: /* for(j=0; j<p; j++) { */
1402: /* (u[j] = t[j]); */
1403: /* } */
1404: /* u[p]='\0'; */
1.126 brouard 1405:
1.137 brouard 1406: /* for(j=0; j<= lg; j++) { */
1407: /* if (j>=(p+1))(v[j-p-1] = t[j]); */
1408: /* } */
1409: /* } */
1.126 brouard 1410:
1.160 brouard 1411: #ifdef _WIN32
1412: char * strsep(char **pp, const char *delim)
1413: {
1414: char *p, *q;
1415:
1416: if ((p = *pp) == NULL)
1417: return 0;
1418: if ((q = strpbrk (p, delim)) != NULL)
1419: {
1420: *pp = q + 1;
1421: *q = '\0';
1422: }
1423: else
1424: *pp = 0;
1425: return p;
1426: }
1427: #endif
1428:
1.126 brouard 1429: /********************** nrerror ********************/
1430:
1431: void nrerror(char error_text[])
1432: {
1433: fprintf(stderr,"ERREUR ...\n");
1434: fprintf(stderr,"%s\n",error_text);
1435: exit(EXIT_FAILURE);
1436: }
1437: /*********************** vector *******************/
1438: double *vector(int nl, int nh)
1439: {
1440: double *v;
1441: v=(double *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(double)));
1442: if (!v) nrerror("allocation failure in vector");
1443: return v-nl+NR_END;
1444: }
1445:
1446: /************************ free vector ******************/
1447: void free_vector(double*v, int nl, int nh)
1448: {
1449: free((FREE_ARG)(v+nl-NR_END));
1450: }
1451:
1452: /************************ivector *******************************/
1453: int *ivector(long nl,long nh)
1454: {
1455: int *v;
1456: v=(int *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(int)));
1457: if (!v) nrerror("allocation failure in ivector");
1458: return v-nl+NR_END;
1459: }
1460:
1461: /******************free ivector **************************/
1462: void free_ivector(int *v, long nl, long nh)
1463: {
1464: free((FREE_ARG)(v+nl-NR_END));
1465: }
1466:
1467: /************************lvector *******************************/
1468: long *lvector(long nl,long nh)
1469: {
1470: long *v;
1471: v=(long *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(long)));
1472: if (!v) nrerror("allocation failure in ivector");
1473: return v-nl+NR_END;
1474: }
1475:
1476: /******************free lvector **************************/
1477: void free_lvector(long *v, long nl, long nh)
1478: {
1479: free((FREE_ARG)(v+nl-NR_END));
1480: }
1481:
1482: /******************* imatrix *******************************/
1483: int **imatrix(long nrl, long nrh, long ncl, long nch)
1484: /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
1485: {
1486: long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
1487: int **m;
1488:
1489: /* allocate pointers to rows */
1490: m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
1491: if (!m) nrerror("allocation failure 1 in matrix()");
1492: m += NR_END;
1493: m -= nrl;
1494:
1495:
1496: /* allocate rows and set pointers to them */
1497: m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
1498: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1499: m[nrl] += NR_END;
1500: m[nrl] -= ncl;
1501:
1502: for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
1503:
1504: /* return pointer to array of pointers to rows */
1505: return m;
1506: }
1507:
1508: /****************** free_imatrix *************************/
1509: void free_imatrix(m,nrl,nrh,ncl,nch)
1510: int **m;
1511: long nch,ncl,nrh,nrl;
1512: /* free an int matrix allocated by imatrix() */
1513: {
1514: free((FREE_ARG) (m[nrl]+ncl-NR_END));
1515: free((FREE_ARG) (m+nrl-NR_END));
1516: }
1517:
1518: /******************* matrix *******************************/
1519: double **matrix(long nrl, long nrh, long ncl, long nch)
1520: {
1521: long i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
1522: double **m;
1523:
1524: m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
1525: if (!m) nrerror("allocation failure 1 in matrix()");
1526: m += NR_END;
1527: m -= nrl;
1528:
1529: m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
1530: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1531: m[nrl] += NR_END;
1532: m[nrl] -= ncl;
1533:
1534: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
1535: return m;
1.145 brouard 1536: /* print *(*(m+1)+70) or print m[1][70]; print m+1 or print &(m[1]) or &(m[1][0])
1537: m[i] = address of ith row of the table. &(m[i]) is its value which is another adress
1538: that of m[i][0]. In order to get the value p m[i][0] but it is unitialized.
1.126 brouard 1539: */
1540: }
1541:
1542: /*************************free matrix ************************/
1543: void free_matrix(double **m, long nrl, long nrh, long ncl, long nch)
1544: {
1545: free((FREE_ARG)(m[nrl]+ncl-NR_END));
1546: free((FREE_ARG)(m+nrl-NR_END));
1547: }
1548:
1549: /******************* ma3x *******************************/
1550: double ***ma3x(long nrl, long nrh, long ncl, long nch, long nll, long nlh)
1551: {
1552: long i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1, nlay=nlh-nll+1;
1553: double ***m;
1554:
1555: m=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
1556: if (!m) nrerror("allocation failure 1 in matrix()");
1557: m += NR_END;
1558: m -= nrl;
1559:
1560: m[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
1561: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1562: m[nrl] += NR_END;
1563: m[nrl] -= ncl;
1564:
1565: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
1566:
1567: m[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*nlay+NR_END)*sizeof(double)));
1568: if (!m[nrl][ncl]) nrerror("allocation failure 3 in matrix()");
1569: m[nrl][ncl] += NR_END;
1570: m[nrl][ncl] -= nll;
1571: for (j=ncl+1; j<=nch; j++)
1572: m[nrl][j]=m[nrl][j-1]+nlay;
1573:
1574: for (i=nrl+1; i<=nrh; i++) {
1575: m[i][ncl]=m[i-1l][ncl]+ncol*nlay;
1576: for (j=ncl+1; j<=nch; j++)
1577: m[i][j]=m[i][j-1]+nlay;
1578: }
1579: return m;
1580: /* gdb: p *(m+1) <=> p m[1] and p (m+1) <=> p (m+1) <=> p &(m[1])
1581: &(m[i][j][k]) <=> *((*(m+i) + j)+k)
1582: */
1583: }
1584:
1585: /*************************free ma3x ************************/
1586: void free_ma3x(double ***m, long nrl, long nrh, long ncl, long nch,long nll, long nlh)
1587: {
1588: free((FREE_ARG)(m[nrl][ncl]+ nll-NR_END));
1589: free((FREE_ARG)(m[nrl]+ncl-NR_END));
1590: free((FREE_ARG)(m+nrl-NR_END));
1591: }
1592:
1593: /*************** function subdirf ***********/
1594: char *subdirf(char fileres[])
1595: {
1596: /* Caution optionfilefiname is hidden */
1597: strcpy(tmpout,optionfilefiname);
1598: strcat(tmpout,"/"); /* Add to the right */
1599: strcat(tmpout,fileres);
1600: return tmpout;
1601: }
1602:
1603: /*************** function subdirf2 ***********/
1604: char *subdirf2(char fileres[], char *preop)
1605: {
1606:
1607: /* Caution optionfilefiname is hidden */
1608: strcpy(tmpout,optionfilefiname);
1609: strcat(tmpout,"/");
1610: strcat(tmpout,preop);
1611: strcat(tmpout,fileres);
1612: return tmpout;
1613: }
1614:
1615: /*************** function subdirf3 ***********/
1616: char *subdirf3(char fileres[], char *preop, char *preop2)
1617: {
1618:
1619: /* Caution optionfilefiname is hidden */
1620: strcpy(tmpout,optionfilefiname);
1621: strcat(tmpout,"/");
1622: strcat(tmpout,preop);
1623: strcat(tmpout,preop2);
1624: strcat(tmpout,fileres);
1625: return tmpout;
1626: }
1.213 brouard 1627:
1628: /*************** function subdirfext ***********/
1629: char *subdirfext(char fileres[], char *preop, char *postop)
1630: {
1631:
1632: strcpy(tmpout,preop);
1633: strcat(tmpout,fileres);
1634: strcat(tmpout,postop);
1635: return tmpout;
1636: }
1.126 brouard 1637:
1.213 brouard 1638: /*************** function subdirfext3 ***********/
1639: char *subdirfext3(char fileres[], char *preop, char *postop)
1640: {
1641:
1642: /* Caution optionfilefiname is hidden */
1643: strcpy(tmpout,optionfilefiname);
1644: strcat(tmpout,"/");
1645: strcat(tmpout,preop);
1646: strcat(tmpout,fileres);
1647: strcat(tmpout,postop);
1648: return tmpout;
1649: }
1650:
1.162 brouard 1651: char *asc_diff_time(long time_sec, char ascdiff[])
1652: {
1653: long sec_left, days, hours, minutes;
1654: days = (time_sec) / (60*60*24);
1655: sec_left = (time_sec) % (60*60*24);
1656: hours = (sec_left) / (60*60) ;
1657: sec_left = (sec_left) %(60*60);
1658: minutes = (sec_left) /60;
1659: sec_left = (sec_left) % (60);
1660: sprintf(ascdiff,"%ld day(s) %ld hour(s) %ld minute(s) %ld second(s)",days, hours, minutes, sec_left);
1661: return ascdiff;
1662: }
1663:
1.126 brouard 1664: /***************** f1dim *************************/
1665: extern int ncom;
1666: extern double *pcom,*xicom;
1667: extern double (*nrfunc)(double []);
1668:
1669: double f1dim(double x)
1670: {
1671: int j;
1672: double f;
1673: double *xt;
1674:
1675: xt=vector(1,ncom);
1676: for (j=1;j<=ncom;j++) xt[j]=pcom[j]+x*xicom[j];
1677: f=(*nrfunc)(xt);
1678: free_vector(xt,1,ncom);
1679: return f;
1680: }
1681:
1682: /*****************brent *************************/
1683: double brent(double ax, double bx, double cx, double (*f)(double), double tol, double *xmin)
1.187 brouard 1684: {
1685: /* Given a function f, and given a bracketing triplet of abscissas ax, bx, cx (such that bx is
1686: * between ax and cx, and f(bx) is less than both f(ax) and f(cx) ), this routine isolates
1687: * the minimum to a fractional precision of about tol using Brent’s method. The abscissa of
1688: * the minimum is returned as xmin, and the minimum function value is returned as brent , the
1689: * returned function value.
1690: */
1.126 brouard 1691: int iter;
1692: double a,b,d,etemp;
1.159 brouard 1693: double fu=0,fv,fw,fx;
1.164 brouard 1694: double ftemp=0.;
1.126 brouard 1695: double p,q,r,tol1,tol2,u,v,w,x,xm;
1696: double e=0.0;
1697:
1698: a=(ax < cx ? ax : cx);
1699: b=(ax > cx ? ax : cx);
1700: x=w=v=bx;
1701: fw=fv=fx=(*f)(x);
1702: for (iter=1;iter<=ITMAX;iter++) {
1703: xm=0.5*(a+b);
1704: tol2=2.0*(tol1=tol*fabs(x)+ZEPS);
1705: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret)))*/
1706: printf(".");fflush(stdout);
1707: fprintf(ficlog,".");fflush(ficlog);
1.162 brouard 1708: #ifdef DEBUGBRENT
1.126 brouard 1709: 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);
1710: 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);
1711: /* if ((fabs(x-xm) <= (tol2-0.5*(b-a)))||(2.0*fabs(fu-ftemp) <= ftol*1.e-2*(fabs(fu)+fabs(ftemp)))) { */
1712: #endif
1713: if (fabs(x-xm) <= (tol2-0.5*(b-a))){
1714: *xmin=x;
1715: return fx;
1716: }
1717: ftemp=fu;
1718: if (fabs(e) > tol1) {
1719: r=(x-w)*(fx-fv);
1720: q=(x-v)*(fx-fw);
1721: p=(x-v)*q-(x-w)*r;
1722: q=2.0*(q-r);
1723: if (q > 0.0) p = -p;
1724: q=fabs(q);
1725: etemp=e;
1726: e=d;
1727: if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x))
1.224 brouard 1728: d=CGOLD*(e=(x >= xm ? a-x : b-x));
1.126 brouard 1729: else {
1.224 brouard 1730: d=p/q;
1731: u=x+d;
1732: if (u-a < tol2 || b-u < tol2)
1733: d=SIGN(tol1,xm-x);
1.126 brouard 1734: }
1735: } else {
1736: d=CGOLD*(e=(x >= xm ? a-x : b-x));
1737: }
1738: u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d));
1739: fu=(*f)(u);
1740: if (fu <= fx) {
1741: if (u >= x) a=x; else b=x;
1742: SHFT(v,w,x,u)
1.183 brouard 1743: SHFT(fv,fw,fx,fu)
1744: } else {
1745: if (u < x) a=u; else b=u;
1746: if (fu <= fw || w == x) {
1.224 brouard 1747: v=w;
1748: w=u;
1749: fv=fw;
1750: fw=fu;
1.183 brouard 1751: } else if (fu <= fv || v == x || v == w) {
1.224 brouard 1752: v=u;
1753: fv=fu;
1.183 brouard 1754: }
1755: }
1.126 brouard 1756: }
1757: nrerror("Too many iterations in brent");
1758: *xmin=x;
1759: return fx;
1760: }
1761:
1762: /****************** mnbrak ***********************/
1763:
1764: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, double *fc,
1765: double (*func)(double))
1.183 brouard 1766: { /* Given a function func , and given distinct initial points ax and bx , this routine searches in
1767: the downhill direction (defined by the function as evaluated at the initial points) and returns
1768: new points ax , bx , cx that bracket a minimum of the function. Also returned are the function
1769: values at the three points, fa, fb , and fc such that fa > fb and fb < fc.
1770: */
1.126 brouard 1771: double ulim,u,r,q, dum;
1772: double fu;
1.187 brouard 1773:
1774: double scale=10.;
1775: int iterscale=0;
1776:
1777: *fa=(*func)(*ax); /* xta[j]=pcom[j]+(*ax)*xicom[j]; fa=f(xta[j])*/
1778: *fb=(*func)(*bx); /* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) */
1779:
1780:
1781: /* while(*fb != *fb){ /\* *ax should be ok, reducing distance to *ax *\/ */
1782: /* printf("Warning mnbrak *fb = %lf, *bx=%lf *ax=%lf *fa==%lf iter=%d\n",*fb, *bx, *ax, *fa, iterscale++); */
1783: /* *bx = *ax - (*ax - *bx)/scale; */
1784: /* *fb=(*func)(*bx); /\* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) *\/ */
1785: /* } */
1786:
1.126 brouard 1787: if (*fb > *fa) {
1788: SHFT(dum,*ax,*bx,dum)
1.183 brouard 1789: SHFT(dum,*fb,*fa,dum)
1790: }
1.126 brouard 1791: *cx=(*bx)+GOLD*(*bx-*ax);
1792: *fc=(*func)(*cx);
1.183 brouard 1793: #ifdef DEBUG
1.224 brouard 1794: printf("mnbrak0 a=%lf *fa=%lf, b=%lf *fb=%lf, c=%lf *fc=%lf\n",*ax,*fa,*bx,*fb,*cx, *fc);
1795: fprintf(ficlog,"mnbrak0 a=%lf *fa=%lf, b=%lf *fb=%lf, c=%lf *fc=%lf\n",*ax,*fa,*bx,*fb,*cx, *fc);
1.183 brouard 1796: #endif
1.224 brouard 1797: while (*fb > *fc) { /* Declining a,b,c with fa> fb > fc. If fc=inf it exits and if flat fb=fc it exits too.*/
1.126 brouard 1798: r=(*bx-*ax)*(*fb-*fc);
1.224 brouard 1799: q=(*bx-*cx)*(*fb-*fa); /* What if fa=inf */
1.126 brouard 1800: u=(*bx)-((*bx-*cx)*q-(*bx-*ax)*r)/
1.183 brouard 1801: (2.0*SIGN(FMAX(fabs(q-r),TINY),q-r)); /* Minimum abscissa of a parabolic estimated from (a,fa), (b,fb) and (c,fc). */
1802: ulim=(*bx)+GLIMIT*(*cx-*bx); /* Maximum abscissa where function should be evaluated */
1803: if ((*bx-u)*(u-*cx) > 0.0) { /* if u_p is between b and c */
1.126 brouard 1804: fu=(*func)(u);
1.163 brouard 1805: #ifdef DEBUG
1806: /* f(x)=A(x-u)**2+f(u) */
1807: double A, fparabu;
1808: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
1809: fparabu= *fa - A*(*ax-u)*(*ax-u);
1.224 brouard 1810: printf("\nmnbrak (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf), (*u=%.12f, fu=%.12lf, fparabu=%.12f, q=%lf < %lf=r)\n",*ax,*fa,*bx,*fb,*cx,*fc,u,fu, fparabu,q,r);
1811: fprintf(ficlog,"\nmnbrak (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf), (*u=%.12f, fu=%.12lf, fparabu=%.12f, q=%lf < %lf=r)\n",*ax,*fa,*bx,*fb,*cx,*fc,u,fu, fparabu,q,r);
1.183 brouard 1812: /* And thus,it can be that fu > *fc even if fparabu < *fc */
1813: /* mnbrak (*ax=7.666299858533, *fa=299039.693133272231), (*bx=8.595447774979, *fb=298976.598289369489),
1814: (*cx=10.098840694817, *fc=298946.631474258087), (*u=9.852501168332, fu=298948.773013752128, fparabu=298945.434711494134) */
1815: /* In that case, there is no bracket in the output! Routine is wrong with many consequences.*/
1.163 brouard 1816: #endif
1.184 brouard 1817: #ifdef MNBRAKORIGINAL
1.183 brouard 1818: #else
1.191 brouard 1819: /* if (fu > *fc) { */
1820: /* #ifdef DEBUG */
1821: /* printf("mnbrak4 fu > fc \n"); */
1822: /* fprintf(ficlog, "mnbrak4 fu > fc\n"); */
1823: /* #endif */
1824: /* /\* SHFT(u,*cx,*cx,u) /\\* ie a=c, c=u and u=c; in that case, next SHFT(a,b,c,u) will give a=b=b, b=c=u, c=u=c and *\\/ *\/ */
1825: /* /\* SHFT(*fa,*fc,fu,*fc) /\\* (b, u, c) is a bracket while test fb > fc will be fu > fc will exit *\\/ *\/ */
1826: /* dum=u; /\* Shifting c and u *\/ */
1827: /* u = *cx; */
1828: /* *cx = dum; */
1829: /* dum = fu; */
1830: /* fu = *fc; */
1831: /* *fc =dum; */
1832: /* } else { /\* end *\/ */
1833: /* #ifdef DEBUG */
1834: /* printf("mnbrak3 fu < fc \n"); */
1835: /* fprintf(ficlog, "mnbrak3 fu < fc\n"); */
1836: /* #endif */
1837: /* dum=u; /\* Shifting c and u *\/ */
1838: /* u = *cx; */
1839: /* *cx = dum; */
1840: /* dum = fu; */
1841: /* fu = *fc; */
1842: /* *fc =dum; */
1843: /* } */
1.224 brouard 1844: #ifdef DEBUGMNBRAK
1845: double A, fparabu;
1846: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
1847: fparabu= *fa - A*(*ax-u)*(*ax-u);
1848: printf("\nmnbrak35 ax=%lf fa=%lf bx=%lf fb=%lf, u=%lf fp=%lf fu=%lf < or >= fc=%lf cx=%lf, q=%lf < %lf=r \n",*ax, *fa, *bx,*fb,u,fparabu,fu,*fc,*cx,q,r);
1849: fprintf(ficlog,"\nmnbrak35 ax=%lf fa=%lf bx=%lf fb=%lf, u=%lf fp=%lf fu=%lf < or >= fc=%lf cx=%lf, q=%lf < %lf=r \n",*ax, *fa, *bx,*fb,u,fparabu,fu,*fc,*cx,q,r);
1.183 brouard 1850: #endif
1.191 brouard 1851: dum=u; /* Shifting c and u */
1852: u = *cx;
1853: *cx = dum;
1854: dum = fu;
1855: fu = *fc;
1856: *fc =dum;
1.183 brouard 1857: #endif
1.162 brouard 1858: } else if ((*cx-u)*(u-ulim) > 0.0) { /* u is after c but before ulim */
1.183 brouard 1859: #ifdef DEBUG
1.224 brouard 1860: printf("\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
1861: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
1.183 brouard 1862: #endif
1.126 brouard 1863: fu=(*func)(u);
1864: if (fu < *fc) {
1.183 brouard 1865: #ifdef DEBUG
1.224 brouard 1866: printf("\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
1867: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
1868: #endif
1869: SHFT(*bx,*cx,u,*cx+GOLD*(*cx-*bx))
1870: SHFT(*fb,*fc,fu,(*func)(u))
1871: #ifdef DEBUG
1872: printf("\nmnbrak2 shift GOLD c=%lf",*cx+GOLD*(*cx-*bx));
1.183 brouard 1873: #endif
1874: }
1.162 brouard 1875: } else if ((u-ulim)*(ulim-*cx) >= 0.0) { /* u outside ulim (verifying that ulim is beyond c) */
1.183 brouard 1876: #ifdef DEBUG
1.224 brouard 1877: printf("\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
1878: fprintf(ficlog,"\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
1.183 brouard 1879: #endif
1.126 brouard 1880: u=ulim;
1881: fu=(*func)(u);
1.183 brouard 1882: } else { /* u could be left to b (if r > q parabola has a maximum) */
1883: #ifdef DEBUG
1.224 brouard 1884: printf("\nmnbrak2 u=%lf could be left to b=%lf (if r=%lf > q=%lf parabola has a maximum)\n",u,*bx,r,q);
1885: fprintf(ficlog,"\nmnbrak2 u=%lf could be left to b=%lf (if r=%lf > q=%lf parabola has a maximum)\n",u,*bx,r,q);
1.183 brouard 1886: #endif
1.126 brouard 1887: u=(*cx)+GOLD*(*cx-*bx);
1888: fu=(*func)(u);
1.224 brouard 1889: #ifdef DEBUG
1890: printf("\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
1891: fprintf(ficlog,"\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
1892: #endif
1.183 brouard 1893: } /* end tests */
1.126 brouard 1894: SHFT(*ax,*bx,*cx,u)
1.183 brouard 1895: SHFT(*fa,*fb,*fc,fu)
1896: #ifdef DEBUG
1.224 brouard 1897: printf("\nmnbrak2 shift (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf)\n",*ax,*fa,*bx,*fb,*cx,*fc);
1898: fprintf(ficlog, "\nmnbrak2 shift (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf)\n",*ax,*fa,*bx,*fb,*cx,*fc);
1.183 brouard 1899: #endif
1900: } /* end while; ie return (a, b, c, fa, fb, fc) such that a < b < c with f(a) > f(b) and fb < f(c) */
1.126 brouard 1901: }
1902:
1903: /*************** linmin ************************/
1.162 brouard 1904: /* Given an n -dimensional point p[1..n] and an n -dimensional direction xi[1..n] , moves and
1905: resets p to where the function func(p) takes on a minimum along the direction xi from p ,
1906: and replaces xi by the actual vector displacement that p was moved. Also returns as fret
1907: the value of func at the returned location p . This is actually all accomplished by calling the
1908: routines mnbrak and brent .*/
1.126 brouard 1909: int ncom;
1910: double *pcom,*xicom;
1911: double (*nrfunc)(double []);
1912:
1.224 brouard 1913: #ifdef LINMINORIGINAL
1.126 brouard 1914: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []))
1.224 brouard 1915: #else
1916: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []), int *flat)
1917: #endif
1.126 brouard 1918: {
1919: double brent(double ax, double bx, double cx,
1920: double (*f)(double), double tol, double *xmin);
1921: double f1dim(double x);
1922: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb,
1923: double *fc, double (*func)(double));
1924: int j;
1925: double xx,xmin,bx,ax;
1926: double fx,fb,fa;
1.187 brouard 1927:
1.203 brouard 1928: #ifdef LINMINORIGINAL
1929: #else
1930: double scale=10., axs, xxs; /* Scale added for infinity */
1931: #endif
1932:
1.126 brouard 1933: ncom=n;
1934: pcom=vector(1,n);
1935: xicom=vector(1,n);
1936: nrfunc=func;
1937: for (j=1;j<=n;j++) {
1938: pcom[j]=p[j];
1.202 brouard 1939: xicom[j]=xi[j]; /* Former scale xi[j] of currrent direction i */
1.126 brouard 1940: }
1.187 brouard 1941:
1.203 brouard 1942: #ifdef LINMINORIGINAL
1943: xx=1.;
1944: #else
1945: axs=0.0;
1946: xxs=1.;
1947: do{
1948: xx= xxs;
1949: #endif
1.187 brouard 1950: ax=0.;
1951: mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim); /* Outputs: xtx[j]=pcom[j]+(*xx)*xicom[j]; fx=f(xtx[j]) */
1952: /* brackets with inputs ax=0 and xx=1, but points, pcom=p, and directions values, xicom=xi, are sent via f1dim(x) */
1953: /* xt[x,j]=pcom[j]+x*xicom[j] f(ax) = f(xt(a,j=1,n)) = f(p(j) + 0 * xi(j)) and f(xx) = f(xt(x, j=1,n)) = f(p(j) + 1 * xi(j)) */
1954: /* Outputs: fa=f(p(j)) and fx=f(p(j) + xxs * xi(j) ) and f(bx)= f(p(j)+ bx* xi(j)) */
1955: /* Given input ax=axs and xx=xxs, xx might be too far from ax to get a finite f(xx) */
1956: /* Searches on line, outputs (ax, xx, bx) such that fx < min(fa and fb) */
1957: /* Find a bracket a,x,b in direction n=xi ie xicom, order may change. Scale is [0:xxs*xi[j]] et non plus [0:xi[j]]*/
1.203 brouard 1958: #ifdef LINMINORIGINAL
1959: #else
1960: if (fx != fx){
1.224 brouard 1961: xxs=xxs/scale; /* Trying a smaller xx, closer to initial ax=0 */
1962: printf("|");
1963: fprintf(ficlog,"|");
1.203 brouard 1964: #ifdef DEBUGLINMIN
1.224 brouard 1965: printf("\nLinmin NAN : input [axs=%lf:xxs=%lf], mnbrak outputs fx=%lf <(fb=%lf and fa=%lf) with xx=%lf in [ax=%lf:bx=%lf] \n", axs, xxs, fx,fb, fa, xx, ax, bx);
1.203 brouard 1966: #endif
1967: }
1.224 brouard 1968: }while(fx != fx && xxs > 1.e-5);
1.203 brouard 1969: #endif
1970:
1.191 brouard 1971: #ifdef DEBUGLINMIN
1972: printf("\nLinmin after mnbrak: ax=%12.7f xx=%12.7f bx=%12.7f fa=%12.2f fx=%12.2f fb=%12.2f\n", ax,xx,bx,fa,fx,fb);
1.202 brouard 1973: fprintf(ficlog,"\nLinmin after mnbrak: ax=%12.7f xx=%12.7f bx=%12.7f fa=%12.2f fx=%12.2f fb=%12.2f\n", ax,xx,bx,fa,fx,fb);
1.191 brouard 1974: #endif
1.224 brouard 1975: #ifdef LINMINORIGINAL
1976: #else
1977: if(fb == fx){ /* Flat function in the direction */
1978: xmin=xx;
1979: *flat=1;
1980: }else{
1981: *flat=0;
1982: #endif
1983: /*Flat mnbrak2 shift (*ax=0.000000000000, *fa=51626.272983130431), (*bx=-1.618034000000, *fb=51590.149499362531), (*cx=-4.236068025156, *fc=51590.149499362531) */
1.187 brouard 1984: *fret=brent(ax,xx,bx,f1dim,TOL,&xmin); /* Giving a bracketting triplet (ax, xx, bx), find a minimum, xmin, according to f1dim, *fret(xmin),*/
1985: /* fa = f(p[j] + ax * xi[j]), fx = f(p[j] + xx * xi[j]), fb = f(p[j] + bx * xi[j]) */
1986: /* fmin = f(p[j] + xmin * xi[j]) */
1987: /* P+lambda n in that direction (lambdamin), with TOL between abscisses */
1988: /* f1dim(xmin): for (j=1;j<=ncom;j++) xt[j]=pcom[j]+xmin*xicom[j]; */
1.126 brouard 1989: #ifdef DEBUG
1.224 brouard 1990: printf("retour brent from bracket (a=%lf fa=%lf, xx=%lf fx=%lf, b=%lf fb=%lf): fret=%lf xmin=%lf\n",ax,fa,xx,fx,bx,fb,*fret,xmin);
1991: fprintf(ficlog,"retour brent from bracket (a=%lf fa=%lf, xx=%lf fx=%lf, b=%lf fb=%lf): fret=%lf xmin=%lf\n",ax,fa,xx,fx,bx,fb,*fret,xmin);
1992: #endif
1993: #ifdef LINMINORIGINAL
1994: #else
1995: }
1.126 brouard 1996: #endif
1.191 brouard 1997: #ifdef DEBUGLINMIN
1998: printf("linmin end ");
1.202 brouard 1999: fprintf(ficlog,"linmin end ");
1.191 brouard 2000: #endif
1.126 brouard 2001: for (j=1;j<=n;j++) {
1.203 brouard 2002: #ifdef LINMINORIGINAL
2003: xi[j] *= xmin;
2004: #else
2005: #ifdef DEBUGLINMIN
2006: if(xxs <1.0)
2007: printf(" before xi[%d]=%12.8f", j,xi[j]);
2008: #endif
2009: xi[j] *= xmin*xxs; /* xi rescaled by xmin and number of loops: if xmin=-1.237 and xi=(1,0,...,0) xi=(-1.237,0,...,0) */
2010: #ifdef DEBUGLINMIN
2011: if(xxs <1.0)
2012: printf(" after xi[%d]=%12.8f, xmin=%12.8f, ax=%12.8f, xx=%12.8f, bx=%12.8f, xxs=%12.8f", j,xi[j], xmin, ax, xx, bx,xxs );
2013: #endif
2014: #endif
1.187 brouard 2015: p[j] += xi[j]; /* Parameters values are updated accordingly */
1.126 brouard 2016: }
1.191 brouard 2017: #ifdef DEBUGLINMIN
1.203 brouard 2018: printf("\n");
1.191 brouard 2019: printf("Comparing last *frec(xmin=%12.8f)=%12.8f from Brent and frec(0.)=%12.8f \n", xmin, *fret, (*func)(p));
1.202 brouard 2020: fprintf(ficlog,"Comparing last *frec(xmin=%12.8f)=%12.8f from Brent and frec(0.)=%12.8f \n", xmin, *fret, (*func)(p));
1.191 brouard 2021: for (j=1;j<=n;j++) {
1.202 brouard 2022: printf(" xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
2023: fprintf(ficlog," xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
2024: if(j % ncovmodel == 0){
1.191 brouard 2025: printf("\n");
1.202 brouard 2026: fprintf(ficlog,"\n");
2027: }
1.191 brouard 2028: }
1.203 brouard 2029: #else
1.191 brouard 2030: #endif
1.126 brouard 2031: free_vector(xicom,1,n);
2032: free_vector(pcom,1,n);
2033: }
2034:
2035:
2036: /*************** powell ************************/
1.162 brouard 2037: /*
2038: Minimization of a function func of n variables. Input consists of an initial starting point
2039: p[1..n] ; an initial matrix xi[1..n][1..n] , whose columns contain the initial set of di-
2040: rections (usually the n unit vectors); and ftol , the fractional tolerance in the function value
2041: such that failure to decrease by more than this amount on one iteration signals doneness. On
2042: output, p is set to the best point found, xi is the then-current direction set, fret is the returned
2043: function value at p , and iter is the number of iterations taken. The routine linmin is used.
2044: */
1.224 brouard 2045: #ifdef LINMINORIGINAL
2046: #else
2047: int *flatdir; /* Function is vanishing in that direction */
1.225 brouard 2048: int flat=0, flatd=0; /* Function is vanishing in that direction */
1.224 brouard 2049: #endif
1.126 brouard 2050: void powell(double p[], double **xi, int n, double ftol, int *iter, double *fret,
2051: double (*func)(double []))
2052: {
1.224 brouard 2053: #ifdef LINMINORIGINAL
2054: void linmin(double p[], double xi[], int n, double *fret,
1.126 brouard 2055: double (*func)(double []));
1.224 brouard 2056: #else
2057: void linmin(double p[], double xi[], int n, double *fret,
2058: double (*func)(double []),int *flat);
2059: #endif
1.126 brouard 2060: int i,ibig,j;
2061: double del,t,*pt,*ptt,*xit;
1.181 brouard 2062: double directest;
1.126 brouard 2063: double fp,fptt;
2064: double *xits;
2065: int niterf, itmp;
1.224 brouard 2066: #ifdef LINMINORIGINAL
2067: #else
2068:
2069: flatdir=ivector(1,n);
2070: for (j=1;j<=n;j++) flatdir[j]=0;
2071: #endif
1.126 brouard 2072:
2073: pt=vector(1,n);
2074: ptt=vector(1,n);
2075: xit=vector(1,n);
2076: xits=vector(1,n);
2077: *fret=(*func)(p);
2078: for (j=1;j<=n;j++) pt[j]=p[j];
1.202 brouard 2079: rcurr_time = time(NULL);
1.126 brouard 2080: for (*iter=1;;++(*iter)) {
1.187 brouard 2081: fp=(*fret); /* From former iteration or initial value */
1.126 brouard 2082: ibig=0;
2083: del=0.0;
1.157 brouard 2084: rlast_time=rcurr_time;
2085: /* (void) gettimeofday(&curr_time,&tzp); */
2086: rcurr_time = time(NULL);
2087: curr_time = *localtime(&rcurr_time);
2088: printf("\nPowell iter=%d -2*LL=%.12f %ld sec. %ld sec.",*iter,*fret, rcurr_time-rlast_time, rcurr_time-rstart_time);fflush(stdout);
2089: fprintf(ficlog,"\nPowell iter=%d -2*LL=%.12f %ld sec. %ld sec.",*iter,*fret,rcurr_time-rlast_time, rcurr_time-rstart_time); fflush(ficlog);
2090: /* fprintf(ficrespow,"%d %.12f %ld",*iter,*fret,curr_time.tm_sec-start_time.tm_sec); */
1.192 brouard 2091: for (i=1;i<=n;i++) {
1.126 brouard 2092: printf(" %d %.12f",i, p[i]);
2093: fprintf(ficlog," %d %.12lf",i, p[i]);
2094: fprintf(ficrespow," %.12lf", p[i]);
2095: }
2096: printf("\n");
2097: fprintf(ficlog,"\n");
2098: fprintf(ficrespow,"\n");fflush(ficrespow);
2099: if(*iter <=3){
1.157 brouard 2100: tml = *localtime(&rcurr_time);
2101: strcpy(strcurr,asctime(&tml));
2102: rforecast_time=rcurr_time;
1.126 brouard 2103: itmp = strlen(strcurr);
2104: if(strcurr[itmp-1]=='\n') /* Windows outputs with a new line */
1.224 brouard 2105: strcurr[itmp-1]='\0';
1.162 brouard 2106: printf("\nConsidering the time needed for the last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
1.157 brouard 2107: fprintf(ficlog,"\nConsidering the time needed for this last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
1.126 brouard 2108: for(niterf=10;niterf<=30;niterf+=10){
1.224 brouard 2109: rforecast_time=rcurr_time+(niterf-*iter)*(rcurr_time-rlast_time);
2110: forecast_time = *localtime(&rforecast_time);
2111: strcpy(strfor,asctime(&forecast_time));
2112: itmp = strlen(strfor);
2113: if(strfor[itmp-1]=='\n')
2114: strfor[itmp-1]='\0';
2115: 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);
2116: 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 2117: }
2118: }
1.187 brouard 2119: for (i=1;i<=n;i++) { /* For each direction i */
2120: for (j=1;j<=n;j++) xit[j]=xi[j][i]; /* Directions stored from previous iteration with previous scales */
1.126 brouard 2121: fptt=(*fret);
2122: #ifdef DEBUG
1.203 brouard 2123: printf("fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
2124: fprintf(ficlog, "fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
1.126 brouard 2125: #endif
1.203 brouard 2126: printf("%d",i);fflush(stdout); /* print direction (parameter) i */
1.126 brouard 2127: fprintf(ficlog,"%d",i);fflush(ficlog);
1.224 brouard 2128: #ifdef LINMINORIGINAL
1.188 brouard 2129: linmin(p,xit,n,fret,func); /* Point p[n]. xit[n] has been loaded for direction i as input.*/
1.224 brouard 2130: #else
2131: linmin(p,xit,n,fret,func,&flat); /* Point p[n]. xit[n] has been loaded for direction i as input.*/
2132: flatdir[i]=flat; /* Function is vanishing in that direction i */
2133: #endif
2134: /* Outputs are fret(new point p) p is updated and xit rescaled */
1.188 brouard 2135: if (fabs(fptt-(*fret)) > del) { /* We are keeping the max gain on each of the n directions */
1.224 brouard 2136: /* because that direction will be replaced unless the gain del is small */
2137: /* in comparison with the 'probable' gain, mu^2, with the last average direction. */
2138: /* Unless the n directions are conjugate some gain in the determinant may be obtained */
2139: /* with the new direction. */
2140: del=fabs(fptt-(*fret));
2141: ibig=i;
1.126 brouard 2142: }
2143: #ifdef DEBUG
2144: printf("%d %.12e",i,(*fret));
2145: fprintf(ficlog,"%d %.12e",i,(*fret));
2146: for (j=1;j<=n;j++) {
1.224 brouard 2147: xits[j]=FMAX(fabs(p[j]-pt[j]),1.e-5);
2148: printf(" x(%d)=%.12e",j,xit[j]);
2149: fprintf(ficlog," x(%d)=%.12e",j,xit[j]);
1.126 brouard 2150: }
2151: for(j=1;j<=n;j++) {
1.225 brouard 2152: printf(" p(%d)=%.12e",j,p[j]);
2153: fprintf(ficlog," p(%d)=%.12e",j,p[j]);
1.126 brouard 2154: }
2155: printf("\n");
2156: fprintf(ficlog,"\n");
2157: #endif
1.187 brouard 2158: } /* end loop on each direction i */
2159: /* Convergence test will use last linmin estimation (fret) and compare former iteration (fp) */
1.188 brouard 2160: /* But p and xit have been updated at the end of linmin, *fret corresponds to new p, xit */
1.187 brouard 2161: /* New value of last point Pn is not computed, P(n-1) */
1.224 brouard 2162: for(j=1;j<=n;j++) {
1.225 brouard 2163: if(flatdir[j] >0){
2164: printf(" p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
2165: fprintf(ficlog," p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
2166: }
2167: /* printf("\n"); */
2168: /* fprintf(ficlog,"\n"); */
2169: }
1.182 brouard 2170: if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret))) { /* Did we reach enough precision? */
1.188 brouard 2171: /* We could compare with a chi^2. chisquare(0.95,ddl=1)=3.84 */
2172: /* By adding age*age in a model, the new -2LL should be lower and the difference follows a */
2173: /* a chisquare statistics with 1 degree. To be significant at the 95% level, it should have */
2174: /* decreased of more than 3.84 */
2175: /* By adding age*age and V1*age the gain (-2LL) should be more than 5.99 (ddl=2) */
2176: /* By using V1+V2+V3, the gain should be 7.82, compared with basic 1+age. */
2177: /* By adding 10 parameters more the gain should be 18.31 */
1.224 brouard 2178:
1.188 brouard 2179: /* Starting the program with initial values given by a former maximization will simply change */
2180: /* the scales of the directions and the directions, because the are reset to canonical directions */
2181: /* Thus the first calls to linmin will give new points and better maximizations until fp-(*fret) is */
2182: /* under the tolerance value. If the tolerance is very small 1.e-9, it could last long. */
1.126 brouard 2183: #ifdef DEBUG
2184: int k[2],l;
2185: k[0]=1;
2186: k[1]=-1;
2187: printf("Max: %.12e",(*func)(p));
2188: fprintf(ficlog,"Max: %.12e",(*func)(p));
2189: for (j=1;j<=n;j++) {
2190: printf(" %.12e",p[j]);
2191: fprintf(ficlog," %.12e",p[j]);
2192: }
2193: printf("\n");
2194: fprintf(ficlog,"\n");
2195: for(l=0;l<=1;l++) {
2196: for (j=1;j<=n;j++) {
2197: ptt[j]=p[j]+(p[j]-pt[j])*k[l];
2198: printf("l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
2199: fprintf(ficlog,"l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
2200: }
2201: printf("func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
2202: fprintf(ficlog,"func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
2203: }
2204: #endif
2205:
1.224 brouard 2206: #ifdef LINMINORIGINAL
2207: #else
2208: free_ivector(flatdir,1,n);
2209: #endif
1.126 brouard 2210: free_vector(xit,1,n);
2211: free_vector(xits,1,n);
2212: free_vector(ptt,1,n);
2213: free_vector(pt,1,n);
2214: return;
1.192 brouard 2215: } /* enough precision */
1.126 brouard 2216: if (*iter == ITMAX) nrerror("powell exceeding maximum iterations.");
1.181 brouard 2217: for (j=1;j<=n;j++) { /* Computes the extrapolated point P_0 + 2 (P_n-P_0) */
1.126 brouard 2218: ptt[j]=2.0*p[j]-pt[j];
2219: xit[j]=p[j]-pt[j];
2220: pt[j]=p[j];
2221: }
1.181 brouard 2222: fptt=(*func)(ptt); /* f_3 */
1.224 brouard 2223: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */
2224: if (*iter <=4) {
1.225 brouard 2225: #else
2226: #endif
1.224 brouard 2227: #ifdef POWELLNOF3INFF1TEST /* skips test F3 <F1 */
1.192 brouard 2228: #else
1.161 brouard 2229: if (fptt < fp) { /* If extrapolated point is better, decide if we keep that new direction or not */
1.192 brouard 2230: #endif
1.162 brouard 2231: /* (x1 f1=fp), (x2 f2=*fret), (x3 f3=fptt), (xm fm) */
1.161 brouard 2232: /* From x1 (P0) distance of x2 is at h and x3 is 2h */
1.162 brouard 2233: /* Let f"(x2) be the 2nd derivative equal everywhere. */
2234: /* Then the parabolic through (x1,f1), (x2,f2) and (x3,f3) */
2235: /* will reach at f3 = fm + h^2/2 f"m ; f" = (f1 -2f2 +f3 ) / h**2 */
1.224 brouard 2236: /* Conditional for using this new direction is that mu^2 = (f1-2f2+f3)^2 /2 < del or directest <0 */
2237: /* also lamda^2=(f1-f2)^2/mu² is a parasite solution of powell */
2238: /* For powell, inclusion of this average direction is only if t(del)<0 or del inbetween mu^2 and lambda^2 */
1.161 brouard 2239: /* t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)-del*SQR(fp-fptt); */
1.224 brouard 2240: /* Even if f3 <f1, directest can be negative and t >0 */
2241: /* mu² and del² are equal when f3=f1 */
2242: /* f3 < f1 : mu² < del <= lambda^2 both test are equivalent */
2243: /* f3 < f1 : mu² < lambda^2 < del then directtest is negative and powell t is positive */
2244: /* f3 > f1 : lambda² < mu^2 < del then t is negative and directest >0 */
2245: /* f3 > f1 : lambda² < del < mu^2 then t is positive and directest >0 */
1.183 brouard 2246: #ifdef NRCORIGINAL
2247: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)- del*SQR(fp-fptt); /* Original Numerical Recipes in C*/
2248: #else
2249: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del); /* Intel compiler doesn't work on one line; bug reported */
1.161 brouard 2250: t= t- del*SQR(fp-fptt);
1.183 brouard 2251: #endif
1.202 brouard 2252: directest = fp-2.0*(*fret)+fptt - 2.0 * del; /* If delta was big enough we change it for a new direction */
1.161 brouard 2253: #ifdef DEBUG
1.181 brouard 2254: printf("t1= %.12lf, t2= %.12lf, t=%.12lf directest=%.12lf\n", 2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del),del*SQR(fp-fptt),t,directest);
2255: fprintf(ficlog,"t1= %.12lf, t2= %.12lf, t=%.12lf directest=%.12lf\n", 2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del),del*SQR(fp-fptt),t,directest);
1.161 brouard 2256: printf("t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
2257: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
2258: fprintf(ficlog,"t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
2259: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
2260: 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);
2261: 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);
2262: #endif
1.183 brouard 2263: #ifdef POWELLORIGINAL
2264: if (t < 0.0) { /* Then we use it for new direction */
2265: #else
1.182 brouard 2266: if (directest*t < 0.0) { /* Contradiction between both tests */
1.224 brouard 2267: printf("directest= %.12lf (if <0 we include P0 Pn as new direction), t= %.12lf, f1= %.12lf,f2= %.12lf,f3= %.12lf, del= %.12lf\n",directest, t, fp,(*fret),fptt,del);
1.192 brouard 2268: printf("f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt);
1.224 brouard 2269: fprintf(ficlog,"directest= %.12lf (if directest<0 or t<0 we include P0 Pn as new direction), t= %.12lf, f1= %.12lf,f2= %.12lf,f3= %.12lf, del= %.12lf\n",directest, t, fp,(*fret),fptt, del);
1.192 brouard 2270: fprintf(ficlog,"f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt);
2271: }
1.181 brouard 2272: if (directest < 0.0) { /* Then we use it for new direction */
2273: #endif
1.191 brouard 2274: #ifdef DEBUGLINMIN
1.234 brouard 2275: printf("Before linmin in direction P%d-P0\n",n);
2276: for (j=1;j<=n;j++) {
2277: printf(" Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2278: fprintf(ficlog," Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2279: if(j % ncovmodel == 0){
2280: printf("\n");
2281: fprintf(ficlog,"\n");
2282: }
2283: }
1.224 brouard 2284: #endif
2285: #ifdef LINMINORIGINAL
1.234 brouard 2286: linmin(p,xit,n,fret,func); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
1.224 brouard 2287: #else
1.234 brouard 2288: linmin(p,xit,n,fret,func,&flat); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
2289: flatdir[i]=flat; /* Function is vanishing in that direction i */
1.191 brouard 2290: #endif
1.234 brouard 2291:
1.191 brouard 2292: #ifdef DEBUGLINMIN
1.234 brouard 2293: for (j=1;j<=n;j++) {
2294: printf("After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2295: fprintf(ficlog,"After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2296: if(j % ncovmodel == 0){
2297: printf("\n");
2298: fprintf(ficlog,"\n");
2299: }
2300: }
1.224 brouard 2301: #endif
1.234 brouard 2302: for (j=1;j<=n;j++) {
2303: xi[j][ibig]=xi[j][n]; /* Replace direction with biggest decrease by last direction n */
2304: xi[j][n]=xit[j]; /* and this nth direction by the by the average p_0 p_n */
2305: }
1.224 brouard 2306: #ifdef LINMINORIGINAL
2307: #else
1.234 brouard 2308: for (j=1, flatd=0;j<=n;j++) {
2309: if(flatdir[j]>0)
2310: flatd++;
2311: }
2312: if(flatd >0){
2313: printf("%d flat directions\n",flatd);
2314: fprintf(ficlog,"%d flat directions\n",flatd);
2315: for (j=1;j<=n;j++) {
2316: if(flatdir[j]>0){
2317: printf("%d ",j);
2318: fprintf(ficlog,"%d ",j);
2319: }
2320: }
2321: printf("\n");
2322: fprintf(ficlog,"\n");
2323: }
1.191 brouard 2324: #endif
1.234 brouard 2325: printf("Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
2326: fprintf(ficlog,"Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
2327:
1.126 brouard 2328: #ifdef DEBUG
1.234 brouard 2329: printf("Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
2330: fprintf(ficlog,"Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
2331: for(j=1;j<=n;j++){
2332: printf(" %lf",xit[j]);
2333: fprintf(ficlog," %lf",xit[j]);
2334: }
2335: printf("\n");
2336: fprintf(ficlog,"\n");
1.126 brouard 2337: #endif
1.192 brouard 2338: } /* end of t or directest negative */
1.224 brouard 2339: #ifdef POWELLNOF3INFF1TEST
1.192 brouard 2340: #else
1.234 brouard 2341: } /* end if (fptt < fp) */
1.192 brouard 2342: #endif
1.225 brouard 2343: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */
1.234 brouard 2344: } /*NODIRECTIONCHANGEDUNTILNITER No change in drections until some iterations are done */
1.225 brouard 2345: #else
1.224 brouard 2346: #endif
1.234 brouard 2347: } /* loop iteration */
1.126 brouard 2348: }
1.234 brouard 2349:
1.126 brouard 2350: /**** Prevalence limit (stable or period prevalence) ****************/
1.234 brouard 2351:
1.235 brouard 2352: double **prevalim(double **prlim, int nlstate, double x[], double age, double **oldm, double **savm, double ftolpl, int *ncvyear, int ij, int nres)
1.234 brouard 2353: {
1.235 brouard 2354: /* Computes the prevalence limit in each live state at age x and for covariate combination ij
2355: (and selected quantitative values in nres)
2356: by left multiplying the unit
1.234 brouard 2357: matrix by transitions matrix until convergence is reached with precision ftolpl */
1.206 brouard 2358: /* Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I */
2359: /* Wx is row vector: population in state 1, population in state 2, population dead */
2360: /* or prevalence in state 1, prevalence in state 2, 0 */
2361: /* newm is the matrix after multiplications, its rows are identical at a factor */
2362: /* Initial matrix pimij */
2363: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
2364: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
2365: /* 0, 0 , 1} */
2366: /*
2367: * and after some iteration: */
2368: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
2369: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
2370: /* 0, 0 , 1} */
2371: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
2372: /* {0.51571254859325999, 0.4842874514067399, */
2373: /* 0.51326036147820708, 0.48673963852179264} */
2374: /* If we start from prlim again, prlim tends to a constant matrix */
1.234 brouard 2375:
1.126 brouard 2376: int i, ii,j,k;
1.209 brouard 2377: double *min, *max, *meandiff, maxmax,sumnew=0.;
1.145 brouard 2378: /* double **matprod2(); */ /* test */
1.218 brouard 2379: double **out, cov[NCOVMAX+1], **pmij(); /* **pmmij is a global variable feeded with oldms etc */
1.126 brouard 2380: double **newm;
1.209 brouard 2381: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
1.203 brouard 2382: int ncvloop=0;
1.169 brouard 2383:
1.209 brouard 2384: min=vector(1,nlstate);
2385: max=vector(1,nlstate);
2386: meandiff=vector(1,nlstate);
2387:
1.218 brouard 2388: /* Starting with matrix unity */
1.126 brouard 2389: for (ii=1;ii<=nlstate+ndeath;ii++)
2390: for (j=1;j<=nlstate+ndeath;j++){
2391: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
2392: }
1.169 brouard 2393:
2394: cov[1]=1.;
2395:
2396: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
1.202 brouard 2397: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
1.126 brouard 2398: for(agefin=age-stepm/YEARM; agefin>=age-delaymax; agefin=agefin-stepm/YEARM){
1.202 brouard 2399: ncvloop++;
1.126 brouard 2400: newm=savm;
2401: /* Covariates have to be included here again */
1.138 brouard 2402: cov[2]=agefin;
1.187 brouard 2403: if(nagesqr==1)
2404: cov[3]= agefin*agefin;;
1.234 brouard 2405: for (k=1; k<=nsd;k++) { /* For single dummy covariates only */
2406: /* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates */
2407: cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)];
1.235 brouard 2408: /* printf("prevalim Dummy combi=%d k=%d TvarsD[%d]=V%d TvarsDind[%d]=%d nbcode=%d cov=%lf codtabm(%d,Tvar[%d])=%d \n",ij,k, k, TvarsD[k],k,TvarsDind[k],nbcode[TvarsD[k]][codtabm(ij,k)],cov[2+nagesqr+TvarsDind[k]], ij, k, codtabm(ij,k)); */
1.234 brouard 2409: }
2410: for (k=1; k<=nsq;k++) { /* For single varying covariates only */
2411: /* Here comes the value of quantitative after renumbering k with single quantitative covariates */
1.235 brouard 2412: cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k];
2413: /* printf("prevalim Quantitative k=%d TvarsQind[%d]=%d, TvarsQ[%d]=V%d,Tqresult[%d][%d]=%f\n",k,k,TvarsQind[k],k,TvarsQ[k],nres,k,Tqresult[nres][k]); */
1.138 brouard 2414: }
1.237 ! brouard 2415: for (k=1; k<=cptcovage;k++){ /* For product with age */
1.234 brouard 2416: if(Dummy[Tvar[Tage[k]]]){
2417: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
2418: } else{
1.235 brouard 2419: cov[2+nagesqr+Tage[k]]=Tqresult[nres][k];
1.234 brouard 2420: }
1.235 brouard 2421: /* printf("prevalim Age combi=%d k=%d Tage[%d]=V%d Tqresult[%d][%d]=%f\n",ij,k,k,Tage[k],nres,k,Tqresult[nres][k]); */
1.234 brouard 2422: }
1.237 ! brouard 2423: for (k=1; k<=cptcovprod;k++){ /* For product without age */
1.235 brouard 2424: /* printf("prevalim Prod ij=%d k=%d Tprod[%d]=%d Tvard[%d][1]=V%d, Tvard[%d][2]=V%d\n",ij,k,k,Tprod[k], k,Tvard[k][1], k,Tvard[k][2]); */
1.237 ! brouard 2425: if(Dummy[Tvard[k][1]==0]){
! 2426: if(Dummy[Tvard[k][2]==0]){
! 2427: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)];
! 2428: }else{
! 2429: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * Tqresult[nres][k];
! 2430: }
! 2431: }else{
! 2432: if(Dummy[Tvard[k][2]==0]){
! 2433: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,k)] * Tqinvresult[nres][Tvard[k][1]];
! 2434: }else{
! 2435: cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]];
! 2436: }
! 2437: }
1.234 brouard 2438: }
1.138 brouard 2439: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
2440: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
2441: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
1.145 brouard 2442: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
2443: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
1.218 brouard 2444: /* age and covariate values of ij are in 'cov' */
1.142 brouard 2445: out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /* Bug Valgrind */
1.138 brouard 2446:
1.126 brouard 2447: savm=oldm;
2448: oldm=newm;
1.209 brouard 2449:
2450: for(j=1; j<=nlstate; j++){
2451: max[j]=0.;
2452: min[j]=1.;
2453: }
2454: for(i=1;i<=nlstate;i++){
2455: sumnew=0;
2456: for(k=1; k<=ndeath; k++) sumnew+=newm[i][nlstate+k];
2457: for(j=1; j<=nlstate; j++){
2458: prlim[i][j]= newm[i][j]/(1-sumnew);
2459: max[j]=FMAX(max[j],prlim[i][j]);
2460: min[j]=FMIN(min[j],prlim[i][j]);
2461: }
2462: }
2463:
1.126 brouard 2464: maxmax=0.;
1.209 brouard 2465: for(j=1; j<=nlstate; j++){
2466: meandiff[j]=(max[j]-min[j])/(max[j]+min[j])*2.; /* mean difference for each column */
2467: maxmax=FMAX(maxmax,meandiff[j]);
2468: /* printf(" age= %d meandiff[%d]=%f, agefin=%d max[%d]=%f min[%d]=%f maxmax=%f\n", (int)age, j, meandiff[j],(int)agefin, j, max[j], j, min[j],maxmax); */
1.169 brouard 2469: } /* j loop */
1.203 brouard 2470: *ncvyear= (int)age- (int)agefin;
1.208 brouard 2471: /* printf("maxmax=%lf maxmin=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, maxmin, ncvloop, (int)age, (int)agefin, *ncvyear); */
1.126 brouard 2472: if(maxmax < ftolpl){
1.209 brouard 2473: /* printf("maxmax=%lf ncvloop=%ld, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
2474: free_vector(min,1,nlstate);
2475: free_vector(max,1,nlstate);
2476: free_vector(meandiff,1,nlstate);
1.126 brouard 2477: return prlim;
2478: }
1.169 brouard 2479: } /* age loop */
1.208 brouard 2480: /* After some age loop it doesn't converge */
1.209 brouard 2481: printf("Warning: the stable prevalence at age %d did not converge with the required precision (%g > ftolpl=%g) within %.0f years. Try to lower 'ftolpl'. \n\
1.208 brouard 2482: Earliest age to start was %d-%d=%d, ncvloop=%d, ncvyear=%d\n", (int)age, maxmax, ftolpl, delaymax, (int)age, (int)delaymax, (int)agefin, ncvloop, *ncvyear);
1.209 brouard 2483: /* Try to lower 'ftol', for example from 1.e-8 to 6.e-9.\n", ftolpl, (int)age, (int)delaymax, (int)agefin, ncvloop, (int)age-(int)agefin); */
2484: free_vector(min,1,nlstate);
2485: free_vector(max,1,nlstate);
2486: free_vector(meandiff,1,nlstate);
1.208 brouard 2487:
1.169 brouard 2488: return prlim; /* should not reach here */
1.126 brouard 2489: }
2490:
1.217 brouard 2491:
2492: /**** Back Prevalence limit (stable or period prevalence) ****************/
2493:
1.218 brouard 2494: /* double **bprevalim(double **bprlim, double ***prevacurrent, int nlstate, double x[], double age, double ageminpar, double agemaxpar, double **oldm, double **savm, double **dnewm, double **doldm, double **dsavm, double ftolpl, int *ncvyear, int ij) */
2495: /* double **bprevalim(double **bprlim, double ***prevacurrent, int nlstate, double x[], double age, double **oldm, double **savm, double **dnewm, double **doldm, double **dsavm, double ftolpl, int *ncvyear, int ij) */
2496: double **bprevalim(double **bprlim, double ***prevacurrent, int nlstate, double x[], double age, double ftolpl, int *ncvyear, int ij)
1.217 brouard 2497: {
1.218 brouard 2498: /* Computes the prevalence limit in each live state at age x and covariate ij by left multiplying the unit
1.217 brouard 2499: matrix by transitions matrix until convergence is reached with precision ftolpl */
2500: /* Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I */
2501: /* Wx is row vector: population in state 1, population in state 2, population dead */
2502: /* or prevalence in state 1, prevalence in state 2, 0 */
2503: /* newm is the matrix after multiplications, its rows are identical at a factor */
2504: /* Initial matrix pimij */
2505: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
2506: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
2507: /* 0, 0 , 1} */
2508: /*
2509: * and after some iteration: */
2510: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
2511: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
2512: /* 0, 0 , 1} */
2513: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
2514: /* {0.51571254859325999, 0.4842874514067399, */
2515: /* 0.51326036147820708, 0.48673963852179264} */
2516: /* If we start from prlim again, prlim tends to a constant matrix */
2517:
2518: int i, ii,j,k;
2519: double *min, *max, *meandiff, maxmax,sumnew=0.;
2520: /* double **matprod2(); */ /* test */
2521: double **out, cov[NCOVMAX+1], **bmij();
2522: double **newm;
1.218 brouard 2523: double **dnewm, **doldm, **dsavm; /* for use */
2524: double **oldm, **savm; /* for use */
2525:
1.217 brouard 2526: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
2527: int ncvloop=0;
2528:
2529: min=vector(1,nlstate);
2530: max=vector(1,nlstate);
2531: meandiff=vector(1,nlstate);
2532:
1.218 brouard 2533: dnewm=ddnewms; doldm=ddoldms; dsavm=ddsavms;
2534: oldm=oldms; savm=savms;
2535:
2536: /* Starting with matrix unity */
2537: for (ii=1;ii<=nlstate+ndeath;ii++)
2538: for (j=1;j<=nlstate+ndeath;j++){
1.217 brouard 2539: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
2540: }
2541:
2542: cov[1]=1.;
2543:
2544: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
2545: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
1.218 brouard 2546: /* for(agefin=age+stepm/YEARM; agefin<=age+delaymax; agefin=agefin+stepm/YEARM){ /\* A changer en age *\/ */
2547: for(agefin=age; agefin<AGESUP; agefin=agefin+stepm/YEARM){ /* A changer en age */
1.217 brouard 2548: ncvloop++;
1.218 brouard 2549: newm=savm; /* oldm should be kept from previous iteration or unity at start */
2550: /* newm points to the allocated table savm passed by the function it can be written, savm could be reallocated */
1.217 brouard 2551: /* Covariates have to be included here again */
2552: cov[2]=agefin;
2553: if(nagesqr==1)
2554: cov[3]= agefin*agefin;;
2555: for (k=1; k<=cptcovn;k++) {
2556: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
2557: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)];
2558: /* printf("prevalim ij=%d k=%d Tvar[%d]=%d nbcode=%d cov=%lf codtabm(%d,Tvar[%d])=%d \n",ij,k, k, Tvar[k],nbcode[Tvar[k]][codtabm(ij,Tvar[k])],cov[2+k], ij, k, codtabm(ij,Tvar[k])]); */
2559: }
2560: for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,k)]*cov[2];
2561: for (k=1; k<=cptcovprod;k++) /* Useless */
2562: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
2563: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)];
2564:
2565: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
2566: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
2567: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
2568: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
2569: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
1.218 brouard 2570: /* ij should be linked to the correct index of cov */
2571: /* age and covariate values ij are in 'cov', but we need to pass
2572: * ij for the observed prevalence at age and status and covariate
2573: * number: prevacurrent[(int)agefin][ii][ij]
2574: */
2575: /* out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, ageminpar, agemaxpar, dnewm, doldm, dsavm,ij)); /\* Bug Valgrind *\/ */
2576: /* out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm, dsavm,ij)); /\* Bug Valgrind *\/ */
2577: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij)); /* Bug Valgrind */
1.217 brouard 2578: savm=oldm;
2579: oldm=newm;
2580: for(j=1; j<=nlstate; j++){
2581: max[j]=0.;
2582: min[j]=1.;
2583: }
2584: for(j=1; j<=nlstate; j++){
2585: for(i=1;i<=nlstate;i++){
1.234 brouard 2586: /* bprlim[i][j]= newm[i][j]/(1-sumnew); */
2587: bprlim[i][j]= newm[i][j];
2588: max[i]=FMAX(max[i],bprlim[i][j]); /* Max in line */
2589: min[i]=FMIN(min[i],bprlim[i][j]);
1.217 brouard 2590: }
2591: }
1.218 brouard 2592:
1.217 brouard 2593: maxmax=0.;
2594: for(i=1; i<=nlstate; i++){
2595: meandiff[i]=(max[i]-min[i])/(max[i]+min[i])*2.; /* mean difference for each column */
2596: maxmax=FMAX(maxmax,meandiff[i]);
2597: /* printf("Back age= %d meandiff[%d]=%f, agefin=%d max[%d]=%f min[%d]=%f maxmax=%f\n", (int)age, i, meandiff[i],(int)agefin, i, max[i], i, min[i],maxmax); */
2598: } /* j loop */
2599: *ncvyear= -( (int)age- (int)agefin);
1.218 brouard 2600: /* printf("Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear);*/
1.217 brouard 2601: if(maxmax < ftolpl){
1.220 brouard 2602: /* printf("OK Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
1.217 brouard 2603: free_vector(min,1,nlstate);
2604: free_vector(max,1,nlstate);
2605: free_vector(meandiff,1,nlstate);
2606: return bprlim;
2607: }
2608: } /* age loop */
2609: /* After some age loop it doesn't converge */
2610: printf("Warning: the back stable prevalence at age %d did not converge with the required precision (%g > ftolpl=%g) within %.0f years. Try to lower 'ftolpl'. \n\
2611: Oldest age to start was %d-%d=%d, ncvloop=%d, ncvyear=%d\n", (int)age, maxmax, ftolpl, delaymax, (int)age, (int)delaymax, (int)agefin, ncvloop, *ncvyear);
2612: /* Try to lower 'ftol', for example from 1.e-8 to 6.e-9.\n", ftolpl, (int)age, (int)delaymax, (int)agefin, ncvloop, (int)age-(int)agefin); */
2613: free_vector(min,1,nlstate);
2614: free_vector(max,1,nlstate);
2615: free_vector(meandiff,1,nlstate);
2616:
2617: return bprlim; /* should not reach here */
2618: }
2619:
1.126 brouard 2620: /*************** transition probabilities ***************/
2621:
2622: double **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
2623: {
1.138 brouard 2624: /* According to parameters values stored in x and the covariate's values stored in cov,
2625: computes the probability to be observed in state j being in state i by appying the
2626: model to the ncovmodel covariates (including constant and age).
2627: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
2628: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
2629: ncth covariate in the global vector x is given by the formula:
2630: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
2631: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
2632: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
2633: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
2634: Outputs ps[i][j] the probability to be observed in j being in j according to
2635: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
2636: */
2637: double s1, lnpijopii;
1.126 brouard 2638: /*double t34;*/
1.164 brouard 2639: int i,j, nc, ii, jj;
1.126 brouard 2640:
1.223 brouard 2641: for(i=1; i<= nlstate; i++){
2642: for(j=1; j<i;j++){
2643: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
2644: /*lnpijopii += param[i][j][nc]*cov[nc];*/
2645: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
2646: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
2647: }
2648: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
2649: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
2650: }
2651: for(j=i+1; j<=nlstate+ndeath;j++){
2652: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
2653: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
2654: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
2655: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
2656: }
2657: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
2658: }
2659: }
1.218 brouard 2660:
1.223 brouard 2661: for(i=1; i<= nlstate; i++){
2662: s1=0;
2663: for(j=1; j<i; j++){
2664: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
2665: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
2666: }
2667: for(j=i+1; j<=nlstate+ndeath; j++){
2668: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
2669: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
2670: }
2671: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
2672: ps[i][i]=1./(s1+1.);
2673: /* Computing other pijs */
2674: for(j=1; j<i; j++)
2675: ps[i][j]= exp(ps[i][j])*ps[i][i];
2676: for(j=i+1; j<=nlstate+ndeath; j++)
2677: ps[i][j]= exp(ps[i][j])*ps[i][i];
2678: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
2679: } /* end i */
1.218 brouard 2680:
1.223 brouard 2681: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
2682: for(jj=1; jj<= nlstate+ndeath; jj++){
2683: ps[ii][jj]=0;
2684: ps[ii][ii]=1;
2685: }
2686: }
1.218 brouard 2687:
2688:
1.223 brouard 2689: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
2690: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
2691: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
2692: /* } */
2693: /* printf("\n "); */
2694: /* } */
2695: /* printf("\n ");printf("%lf ",cov[2]);*/
2696: /*
2697: for(i=1; i<= npar; i++) printf("%f ",x[i]);
1.218 brouard 2698: goto end;*/
1.223 brouard 2699: return ps;
1.126 brouard 2700: }
2701:
1.218 brouard 2702: /*************** backward transition probabilities ***************/
2703:
2704: /* double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, double ageminpar, double agemaxpar, double ***dnewm, double **doldm, double **dsavm, int ij ) */
2705: /* double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, double ***dnewm, double **doldm, double **dsavm, int ij ) */
2706: double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, int ij )
2707: {
1.222 brouard 2708: /* Computes the backward probability at age agefin and covariate ij
2709: * and returns in **ps as well as **bmij.
2710: */
1.218 brouard 2711: int i, ii, j,k;
1.222 brouard 2712:
2713: double **out, **pmij();
2714: double sumnew=0.;
1.218 brouard 2715: double agefin;
1.222 brouard 2716:
2717: double **dnewm, **dsavm, **doldm;
2718: double **bbmij;
2719:
1.218 brouard 2720: doldm=ddoldms; /* global pointers */
1.222 brouard 2721: dnewm=ddnewms;
2722: dsavm=ddsavms;
2723:
2724: agefin=cov[2];
2725: /* bmij *//* age is cov[2], ij is included in cov, but we need for
2726: the observed prevalence (with this covariate ij) */
2727: dsavm=pmij(pmmij,cov,ncovmodel,x,nlstate);
2728: /* We do have the matrix Px in savm and we need pij */
2729: for (j=1;j<=nlstate+ndeath;j++){
2730: sumnew=0.; /* w1 p11 + w2 p21 only on live states */
2731: for (ii=1;ii<=nlstate;ii++){
2732: sumnew+=dsavm[ii][j]*prevacurrent[(int)agefin][ii][ij];
2733: } /* sumnew is (N11+N21)/N..= N.1/N.. = sum on i of w_i pij */
2734: for (ii=1;ii<=nlstate+ndeath;ii++){
2735: if(sumnew >= 1.e-10){
2736: /* if(agefin >= agemaxpar && agefin <= agemaxpar+stepm/YEARM){ */
2737: /* doldm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
2738: /* }else if(agefin >= agemaxpar+stepm/YEARM){ */
2739: /* doldm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
2740: /* }else */
2741: doldm[ii][j]=(ii==j ? 1./sumnew : 0.0);
2742: }else{
2743: printf("ii=%d, i=%d, doldm=%lf dsavm=%lf, probs=%lf, sumnew=%lf,agefin=%d\n",ii,j,doldm[ii][j],dsavm[ii][j],prevacurrent[(int)agefin][ii][ij],sumnew, (int)agefin);
2744: }
2745: } /*End ii */
2746: } /* End j, At the end doldm is diag[1/(w_1p1i+w_2 p2i)] */
2747: /* left Product of this diag matrix by dsavm=Px (newm=dsavm*doldm) */
2748: bbmij=matprod2(dnewm, dsavm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, doldm); /* Bug Valgrind */
2749: /* dsavm=doldm; /\* dsavm is now diag [1/(w_1p1i+w_2 p2i)] but can be overwritten*\/ */
2750: /* doldm=dnewm; /\* doldm is now Px * diag [1/(w_1p1i+w_2 p2i)] *\/ */
2751: /* dnewm=dsavm; /\* doldm is now Px * diag [1/(w_1p1i+w_2 p2i)] *\/ */
2752: /* left Product of this matrix by diag matrix of prevalences (savm) */
2753: for (j=1;j<=nlstate+ndeath;j++){
2754: for (ii=1;ii<=nlstate+ndeath;ii++){
2755: dsavm[ii][j]=(ii==j ? prevacurrent[(int)agefin][ii][ij] : 0.0);
2756: }
2757: } /* End j, At the end oldm is diag[1/(w_1p1i+w_2 p2i)] */
2758: ps=matprod2(doldm, dsavm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, dnewm); /* Bug Valgrind */
2759: /* newm or out is now diag[w_i] * Px * diag [1/(w_1p1i+w_2 p2i)] */
2760: /* end bmij */
2761: return ps;
1.218 brouard 2762: }
1.217 brouard 2763: /*************** transition probabilities ***************/
2764:
1.218 brouard 2765: double **bpmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
1.217 brouard 2766: {
2767: /* According to parameters values stored in x and the covariate's values stored in cov,
2768: computes the probability to be observed in state j being in state i by appying the
2769: model to the ncovmodel covariates (including constant and age).
2770: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
2771: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
2772: ncth covariate in the global vector x is given by the formula:
2773: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
2774: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
2775: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
2776: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
2777: Outputs ps[i][j] the probability to be observed in j being in j according to
2778: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
2779: */
2780: double s1, lnpijopii;
2781: /*double t34;*/
2782: int i,j, nc, ii, jj;
2783:
1.234 brouard 2784: for(i=1; i<= nlstate; i++){
2785: for(j=1; j<i;j++){
2786: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
2787: /*lnpijopii += param[i][j][nc]*cov[nc];*/
2788: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
2789: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
2790: }
2791: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
2792: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
2793: }
2794: for(j=i+1; j<=nlstate+ndeath;j++){
2795: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
2796: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
2797: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
2798: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
2799: }
2800: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
2801: }
2802: }
2803:
2804: for(i=1; i<= nlstate; i++){
2805: s1=0;
2806: for(j=1; j<i; j++){
2807: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
2808: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
2809: }
2810: for(j=i+1; j<=nlstate+ndeath; j++){
2811: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
2812: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
2813: }
2814: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
2815: ps[i][i]=1./(s1+1.);
2816: /* Computing other pijs */
2817: for(j=1; j<i; j++)
2818: ps[i][j]= exp(ps[i][j])*ps[i][i];
2819: for(j=i+1; j<=nlstate+ndeath; j++)
2820: ps[i][j]= exp(ps[i][j])*ps[i][i];
2821: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
2822: } /* end i */
2823:
2824: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
2825: for(jj=1; jj<= nlstate+ndeath; jj++){
2826: ps[ii][jj]=0;
2827: ps[ii][ii]=1;
2828: }
2829: }
2830: /* Added for backcast */ /* Transposed matrix too */
2831: for(jj=1; jj<= nlstate+ndeath; jj++){
2832: s1=0.;
2833: for(ii=1; ii<= nlstate+ndeath; ii++){
2834: s1+=ps[ii][jj];
2835: }
2836: for(ii=1; ii<= nlstate; ii++){
2837: ps[ii][jj]=ps[ii][jj]/s1;
2838: }
2839: }
2840: /* Transposition */
2841: for(jj=1; jj<= nlstate+ndeath; jj++){
2842: for(ii=jj; ii<= nlstate+ndeath; ii++){
2843: s1=ps[ii][jj];
2844: ps[ii][jj]=ps[jj][ii];
2845: ps[jj][ii]=s1;
2846: }
2847: }
2848: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
2849: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
2850: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
2851: /* } */
2852: /* printf("\n "); */
2853: /* } */
2854: /* printf("\n ");printf("%lf ",cov[2]);*/
2855: /*
2856: for(i=1; i<= npar; i++) printf("%f ",x[i]);
2857: goto end;*/
2858: return ps;
1.217 brouard 2859: }
2860:
2861:
1.126 brouard 2862: /**************** Product of 2 matrices ******************/
2863:
1.145 brouard 2864: double **matprod2(double **out, double **in,int nrl, int nrh, int ncl, int nch, int ncolol, int ncoloh, double **b)
1.126 brouard 2865: {
2866: /* Computes the matrix product of in(1,nrh-nrl+1)(1,nch-ncl+1) times
2867: b(1,nch-ncl+1)(1,ncoloh-ncolol+1) into out(...) */
2868: /* in, b, out are matrice of pointers which should have been initialized
2869: before: only the contents of out is modified. The function returns
2870: a pointer to pointers identical to out */
1.145 brouard 2871: int i, j, k;
1.126 brouard 2872: for(i=nrl; i<= nrh; i++)
1.145 brouard 2873: for(k=ncolol; k<=ncoloh; k++){
2874: out[i][k]=0.;
2875: for(j=ncl; j<=nch; j++)
2876: out[i][k] +=in[i][j]*b[j][k];
2877: }
1.126 brouard 2878: return out;
2879: }
2880:
2881:
2882: /************* Higher Matrix Product ***************/
2883:
1.235 brouard 2884: double ***hpxij(double ***po, int nhstepm, double age, int hstepm, double *x, int nlstate, int stepm, double **oldm, double **savm, int ij, int nres )
1.126 brouard 2885: {
1.218 brouard 2886: /* Computes the transition matrix starting at age 'age' and combination of covariate values corresponding to ij over
1.126 brouard 2887: 'nhstepm*hstepm*stepm' months (i.e. until
2888: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
2889: nhstepm*hstepm matrices.
2890: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
2891: (typically every 2 years instead of every month which is too big
2892: for the memory).
2893: Model is determined by parameters x and covariates have to be
2894: included manually here.
2895:
2896: */
2897:
2898: int i, j, d, h, k;
1.131 brouard 2899: double **out, cov[NCOVMAX+1];
1.126 brouard 2900: double **newm;
1.187 brouard 2901: double agexact;
1.214 brouard 2902: double agebegin, ageend;
1.126 brouard 2903:
2904: /* Hstepm could be zero and should return the unit matrix */
2905: for (i=1;i<=nlstate+ndeath;i++)
2906: for (j=1;j<=nlstate+ndeath;j++){
2907: oldm[i][j]=(i==j ? 1.0 : 0.0);
2908: po[i][j][0]=(i==j ? 1.0 : 0.0);
2909: }
2910: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
2911: for(h=1; h <=nhstepm; h++){
2912: for(d=1; d <=hstepm; d++){
2913: newm=savm;
2914: /* Covariates have to be included here again */
2915: cov[1]=1.;
1.214 brouard 2916: agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /* age just before transition */
1.187 brouard 2917: cov[2]=agexact;
2918: if(nagesqr==1)
1.227 brouard 2919: cov[3]= agexact*agexact;
1.235 brouard 2920: for (k=1; k<=nsd;k++) { /* For single dummy covariates only */
2921: /* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates */
2922: cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)];
2923: /* printf("hpxij Dummy combi=%d k=%d TvarsD[%d]=V%d TvarsDind[%d]=%d nbcode=%d cov=%lf codtabm(%d,Tvar[%d])=%d \n",ij,k, k, TvarsD[k],k,TvarsDind[k],nbcode[TvarsD[k]][codtabm(ij,k)],cov[2+nagesqr+TvarsDind[k]], ij, k, codtabm(ij,k)); */
2924: }
2925: for (k=1; k<=nsq;k++) { /* For single varying covariates only */
2926: /* Here comes the value of quantitative after renumbering k with single quantitative covariates */
2927: cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k];
2928: /* printf("hPxij Quantitative k=%d TvarsQind[%d]=%d, TvarsQ[%d]=V%d,Tqresult[%d][%d]=%f\n",k,k,TvarsQind[k],k,TvarsQ[k],nres,k,Tqresult[nres][k]); */
2929: }
2930: for (k=1; k<=cptcovage;k++){
2931: if(Dummy[Tvar[Tage[k]]]){
2932: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
2933: } else{
2934: cov[2+nagesqr+Tage[k]]=Tqresult[nres][k];
2935: }
2936: /* printf("hPxij Age combi=%d k=%d Tage[%d]=V%d Tqresult[%d][%d]=%f\n",ij,k,k,Tage[k],nres,k,Tqresult[nres][k]); */
2937: }
2938: for (k=1; k<=cptcovprod;k++){ /* */
2939: /* printf("hPxij Prod ij=%d k=%d Tprod[%d]=%d Tvard[%d][1]=V%d, Tvard[%d][2]=V%d\n",ij,k,k,Tprod[k], k,Tvard[k][1], k,Tvard[k][2]); */
2940: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)];
2941: }
2942: /* for (k=1; k<=cptcovn;k++) */
2943: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; */
2944: /* for (k=1; k<=cptcovage;k++) /\* Should start at cptcovn+1 *\/ */
2945: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; */
2946: /* for (k=1; k<=cptcovprod;k++) /\* Useless because included in cptcovn *\/ */
2947: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)]; */
1.227 brouard 2948:
2949:
1.126 brouard 2950: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
2951: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
1.218 brouard 2952: /* right multiplication of oldm by the current matrix */
1.126 brouard 2953: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
2954: pmij(pmmij,cov,ncovmodel,x,nlstate));
1.217 brouard 2955: /* if((int)age == 70){ */
2956: /* printf(" Forward hpxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
2957: /* for(i=1; i<=nlstate+ndeath; i++) { */
2958: /* printf("%d pmmij ",i); */
2959: /* for(j=1;j<=nlstate+ndeath;j++) { */
2960: /* printf("%f ",pmmij[i][j]); */
2961: /* } */
2962: /* printf(" oldm "); */
2963: /* for(j=1;j<=nlstate+ndeath;j++) { */
2964: /* printf("%f ",oldm[i][j]); */
2965: /* } */
2966: /* printf("\n"); */
2967: /* } */
2968: /* } */
1.126 brouard 2969: savm=oldm;
2970: oldm=newm;
2971: }
2972: for(i=1; i<=nlstate+ndeath; i++)
2973: for(j=1;j<=nlstate+ndeath;j++) {
1.218 brouard 2974: po[i][j][h]=newm[i][j];
2975: /*if(h==nhstepm) printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]);*/
1.126 brouard 2976: }
1.128 brouard 2977: /*printf("h=%d ",h);*/
1.126 brouard 2978: } /* end h */
1.218 brouard 2979: /* printf("\n H=%d \n",h); */
1.126 brouard 2980: return po;
2981: }
2982:
1.217 brouard 2983: /************* Higher Back Matrix Product ***************/
1.218 brouard 2984: /* double ***hbxij(double ***po, int nhstepm, double age, int hstepm, double *x, double ***prevacurrent, int nlstate, int stepm, double **oldm, double **savm, double **dnewm, double **doldm, double **dsavm, int ij ) */
1.222 brouard 2985: double ***hbxij(double ***po, int nhstepm, double age, int hstepm, double *x, double ***prevacurrent, int nlstate, int stepm, int ij )
1.217 brouard 2986: {
1.218 brouard 2987: /* Computes the transition matrix starting at age 'age' over
1.217 brouard 2988: 'nhstepm*hstepm*stepm' months (i.e. until
1.218 brouard 2989: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
2990: nhstepm*hstepm matrices.
2991: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
2992: (typically every 2 years instead of every month which is too big
1.217 brouard 2993: for the memory).
1.218 brouard 2994: Model is determined by parameters x and covariates have to be
2995: included manually here.
1.217 brouard 2996:
1.222 brouard 2997: */
1.217 brouard 2998:
2999: int i, j, d, h, k;
3000: double **out, cov[NCOVMAX+1];
3001: double **newm;
3002: double agexact;
3003: double agebegin, ageend;
1.222 brouard 3004: double **oldm, **savm;
1.217 brouard 3005:
1.222 brouard 3006: oldm=oldms;savm=savms;
1.217 brouard 3007: /* Hstepm could be zero and should return the unit matrix */
3008: for (i=1;i<=nlstate+ndeath;i++)
3009: for (j=1;j<=nlstate+ndeath;j++){
3010: oldm[i][j]=(i==j ? 1.0 : 0.0);
3011: po[i][j][0]=(i==j ? 1.0 : 0.0);
3012: }
3013: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
3014: for(h=1; h <=nhstepm; h++){
3015: for(d=1; d <=hstepm; d++){
3016: newm=savm;
3017: /* Covariates have to be included here again */
3018: cov[1]=1.;
3019: agexact=age-((h-1)*hstepm + (d-1))*stepm/YEARM; /* age just before transition */
3020: /* agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /\* age just before transition *\/ */
3021: cov[2]=agexact;
3022: if(nagesqr==1)
1.222 brouard 3023: cov[3]= agexact*agexact;
1.218 brouard 3024: for (k=1; k<=cptcovn;k++)
1.222 brouard 3025: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)];
3026: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
1.217 brouard 3027: for (k=1; k<=cptcovage;k++) /* Should start at cptcovn+1 */
1.222 brouard 3028: /* cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
3029: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
3030: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
1.217 brouard 3031: for (k=1; k<=cptcovprod;k++) /* Useless because included in cptcovn */
1.222 brouard 3032: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)];
3033: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])]*nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
1.218 brouard 3034:
3035:
1.217 brouard 3036: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
3037: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
1.218 brouard 3038: /* Careful transposed matrix */
1.222 brouard 3039: /* age is in cov[2] */
1.218 brouard 3040: /* out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm, dsavm,ij),\ */
1.222 brouard 3041: /* 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); */
1.218 brouard 3042: out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij),\
1.222 brouard 3043: 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm);
1.217 brouard 3044: /* if((int)age == 70){ */
3045: /* printf(" Backward hbxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
3046: /* for(i=1; i<=nlstate+ndeath; i++) { */
3047: /* printf("%d pmmij ",i); */
3048: /* for(j=1;j<=nlstate+ndeath;j++) { */
3049: /* printf("%f ",pmmij[i][j]); */
3050: /* } */
3051: /* printf(" oldm "); */
3052: /* for(j=1;j<=nlstate+ndeath;j++) { */
3053: /* printf("%f ",oldm[i][j]); */
3054: /* } */
3055: /* printf("\n"); */
3056: /* } */
3057: /* } */
3058: savm=oldm;
3059: oldm=newm;
3060: }
3061: for(i=1; i<=nlstate+ndeath; i++)
3062: for(j=1;j<=nlstate+ndeath;j++) {
1.222 brouard 3063: po[i][j][h]=newm[i][j];
3064: /*if(h==nhstepm) printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]);*/
1.217 brouard 3065: }
3066: /*printf("h=%d ",h);*/
3067: } /* end h */
1.222 brouard 3068: /* printf("\n H=%d \n",h); */
1.217 brouard 3069: return po;
3070: }
3071:
3072:
1.162 brouard 3073: #ifdef NLOPT
3074: double myfunc(unsigned n, const double *p1, double *grad, void *pd){
3075: double fret;
3076: double *xt;
3077: int j;
3078: myfunc_data *d2 = (myfunc_data *) pd;
3079: /* xt = (p1-1); */
3080: xt=vector(1,n);
3081: for (j=1;j<=n;j++) xt[j]=p1[j-1]; /* xt[1]=p1[0] */
3082:
3083: fret=(d2->function)(xt); /* p xt[1]@8 is fine */
3084: /* fret=(*func)(xt); /\* p xt[1]@8 is fine *\/ */
3085: printf("Function = %.12lf ",fret);
3086: for (j=1;j<=n;j++) printf(" %d %.8lf", j, xt[j]);
3087: printf("\n");
3088: free_vector(xt,1,n);
3089: return fret;
3090: }
3091: #endif
1.126 brouard 3092:
3093: /*************** log-likelihood *************/
3094: double func( double *x)
3095: {
1.226 brouard 3096: int i, ii, j, k, mi, d, kk;
3097: int ioffset=0;
3098: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
3099: double **out;
3100: double lli; /* Individual log likelihood */
3101: int s1, s2;
1.228 brouard 3102: int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate, quantitative time varying covariate */
1.226 brouard 3103: double bbh, survp;
3104: long ipmx;
3105: double agexact;
3106: /*extern weight */
3107: /* We are differentiating ll according to initial status */
3108: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
3109: /*for(i=1;i<imx;i++)
3110: printf(" %d\n",s[4][i]);
3111: */
1.162 brouard 3112:
1.226 brouard 3113: ++countcallfunc;
1.162 brouard 3114:
1.226 brouard 3115: cov[1]=1.;
1.126 brouard 3116:
1.226 brouard 3117: for(k=1; k<=nlstate; k++) ll[k]=0.;
1.224 brouard 3118: ioffset=0;
1.226 brouard 3119: if(mle==1){
3120: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3121: /* Computes the values of the ncovmodel covariates of the model
3122: depending if the covariates are fixed or varying (age dependent) and stores them in cov[]
3123: Then computes with function pmij which return a matrix p[i][j] giving the elementary probability
3124: to be observed in j being in i according to the model.
3125: */
3126: ioffset=2+nagesqr+cptcovage;
1.233 brouard 3127: /* Fixed */
1.234 brouard 3128: for (k=1; k<=ncovf;k++){ /* Simple and product fixed covariates without age* products */
3129: cov[ioffset+TvarFind[k]]=covar[Tvar[TvarFind[k]]][i];/* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, only V1 is fixed (k=6)*/
3130: }
1.226 brouard 3131: /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4]
3132: is 6, Tvar[3=age*V3] should not be computed because of age Tvar[4=V3*V2]
3133: has been calculated etc */
3134: /* For an individual i, wav[i] gives the number of effective waves */
3135: /* We compute the contribution to Likelihood of each effective transition
3136: mw[mi][i] is real wave of the mi th effectve wave */
3137: /* Then statuses are computed at each begin and end of an effective wave s1=s[ mw[mi][i] ][i];
3138: s2=s[mw[mi+1][i]][i];
3139: And the iv th varying covariate is the cotvar[mw[mi+1][i]][iv][i]
3140: But if the variable is not in the model TTvar[iv] is the real variable effective in the model:
3141: meaning that decodemodel should be used cotvar[mw[mi+1][i]][TTvar[iv]][i]
3142: */
3143: for(mi=1; mi<= wav[i]-1; mi++){
1.234 brouard 3144: for(k=1; k <= ncovv ; k++){ /* Varying covariates (single and product but no age )*/
3145: cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i];
3146: }
3147: for (ii=1;ii<=nlstate+ndeath;ii++)
3148: for (j=1;j<=nlstate+ndeath;j++){
3149: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3150: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3151: }
3152: for(d=0; d<dh[mi][i]; d++){
3153: newm=savm;
3154: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3155: cov[2]=agexact;
3156: if(nagesqr==1)
3157: cov[3]= agexact*agexact; /* Should be changed here */
3158: for (kk=1; kk<=cptcovage;kk++) {
3159: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; /* Tage[kk] gives the data-covariate associated with age */
3160: }
3161: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3162: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3163: savm=oldm;
3164: oldm=newm;
3165: } /* end mult */
3166:
3167: /*lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ /* Original formula */
3168: /* But now since version 0.9 we anticipate for bias at large stepm.
3169: * If stepm is larger than one month (smallest stepm) and if the exact delay
3170: * (in months) between two waves is not a multiple of stepm, we rounded to
3171: * the nearest (and in case of equal distance, to the lowest) interval but now
3172: * we keep into memory the bias bh[mi][i] and also the previous matrix product
3173: * (i.e to dh[mi][i]-1) saved in 'savm'. Then we inter(extra)polate the
3174: * probability in order to take into account the bias as a fraction of the way
1.231 brouard 3175: * from savm to out if bh is negative or even beyond if bh is positive. bh varies
3176: * -stepm/2 to stepm/2 .
3177: * For stepm=1 the results are the same as for previous versions of Imach.
3178: * For stepm > 1 the results are less biased than in previous versions.
3179: */
1.234 brouard 3180: s1=s[mw[mi][i]][i];
3181: s2=s[mw[mi+1][i]][i];
3182: bbh=(double)bh[mi][i]/(double)stepm;
3183: /* bias bh is positive if real duration
3184: * is higher than the multiple of stepm and negative otherwise.
3185: */
3186: /* lli= (savm[s1][s2]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2]));*/
3187: if( s2 > nlstate){
3188: /* i.e. if s2 is a death state and if the date of death is known
3189: then the contribution to the likelihood is the probability to
3190: die between last step unit time and current step unit time,
3191: which is also equal to probability to die before dh
3192: minus probability to die before dh-stepm .
3193: In version up to 0.92 likelihood was computed
3194: as if date of death was unknown. Death was treated as any other
3195: health state: the date of the interview describes the actual state
3196: and not the date of a change in health state. The former idea was
3197: to consider that at each interview the state was recorded
3198: (healthy, disable or death) and IMaCh was corrected; but when we
3199: introduced the exact date of death then we should have modified
3200: the contribution of an exact death to the likelihood. This new
3201: contribution is smaller and very dependent of the step unit
3202: stepm. It is no more the probability to die between last interview
3203: and month of death but the probability to survive from last
3204: interview up to one month before death multiplied by the
3205: probability to die within a month. Thanks to Chris
3206: Jackson for correcting this bug. Former versions increased
3207: mortality artificially. The bad side is that we add another loop
3208: which slows down the processing. The difference can be up to 10%
3209: lower mortality.
3210: */
3211: /* If, at the beginning of the maximization mostly, the
3212: cumulative probability or probability to be dead is
3213: constant (ie = 1) over time d, the difference is equal to
3214: 0. out[s1][3] = savm[s1][3]: probability, being at state
3215: s1 at precedent wave, to be dead a month before current
3216: wave is equal to probability, being at state s1 at
3217: precedent wave, to be dead at mont of the current
3218: wave. Then the observed probability (that this person died)
3219: is null according to current estimated parameter. In fact,
3220: it should be very low but not zero otherwise the log go to
3221: infinity.
3222: */
1.183 brouard 3223: /* #ifdef INFINITYORIGINAL */
3224: /* lli=log(out[s1][s2] - savm[s1][s2]); */
3225: /* #else */
3226: /* if ((out[s1][s2] - savm[s1][s2]) < mytinydouble) */
3227: /* lli=log(mytinydouble); */
3228: /* else */
3229: /* lli=log(out[s1][s2] - savm[s1][s2]); */
3230: /* #endif */
1.226 brouard 3231: lli=log(out[s1][s2] - savm[s1][s2]);
1.216 brouard 3232:
1.226 brouard 3233: } else if ( s2==-1 ) { /* alive */
3234: for (j=1,survp=0. ; j<=nlstate; j++)
3235: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3236: /*survp += out[s1][j]; */
3237: lli= log(survp);
3238: }
3239: else if (s2==-4) {
3240: for (j=3,survp=0. ; j<=nlstate; j++)
3241: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3242: lli= log(survp);
3243: }
3244: else if (s2==-5) {
3245: for (j=1,survp=0. ; j<=2; j++)
3246: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3247: lli= log(survp);
3248: }
3249: else{
3250: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
3251: /* 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 */
3252: }
3253: /*lli=(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]);*/
3254: /*if(lli ==000.0)*/
3255: /*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); */
3256: ipmx +=1;
3257: sw += weight[i];
3258: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3259: /* if (lli < log(mytinydouble)){ */
3260: /* printf("Close to inf lli = %.10lf < %.10lf i= %d mi= %d, s[%d][i]=%d s1=%d s2=%d\n", lli,log(mytinydouble), i, mi,mw[mi][i], s[mw[mi][i]][i], s1,s2); */
3261: /* fprintf(ficlog,"Close to inf lli = %.10lf i= %d mi= %d, s[mw[mi][i]][i]=%d\n", lli, i, mi,s[mw[mi][i]][i]); */
3262: /* } */
3263: } /* end of wave */
3264: } /* end of individual */
3265: } else if(mle==2){
3266: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3267: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3268: for(mi=1; mi<= wav[i]-1; mi++){
3269: for (ii=1;ii<=nlstate+ndeath;ii++)
3270: for (j=1;j<=nlstate+ndeath;j++){
3271: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3272: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3273: }
3274: for(d=0; d<=dh[mi][i]; d++){
3275: newm=savm;
3276: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3277: cov[2]=agexact;
3278: if(nagesqr==1)
3279: cov[3]= agexact*agexact;
3280: for (kk=1; kk<=cptcovage;kk++) {
3281: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3282: }
3283: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3284: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3285: savm=oldm;
3286: oldm=newm;
3287: } /* end mult */
3288:
3289: s1=s[mw[mi][i]][i];
3290: s2=s[mw[mi+1][i]][i];
3291: bbh=(double)bh[mi][i]/(double)stepm;
3292: 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 */
3293: ipmx +=1;
3294: sw += weight[i];
3295: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3296: } /* end of wave */
3297: } /* end of individual */
3298: } else if(mle==3){ /* exponential inter-extrapolation */
3299: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3300: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3301: for(mi=1; mi<= wav[i]-1; mi++){
3302: for (ii=1;ii<=nlstate+ndeath;ii++)
3303: for (j=1;j<=nlstate+ndeath;j++){
3304: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3305: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3306: }
3307: for(d=0; d<dh[mi][i]; d++){
3308: newm=savm;
3309: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3310: cov[2]=agexact;
3311: if(nagesqr==1)
3312: cov[3]= agexact*agexact;
3313: for (kk=1; kk<=cptcovage;kk++) {
3314: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3315: }
3316: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3317: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3318: savm=oldm;
3319: oldm=newm;
3320: } /* end mult */
3321:
3322: s1=s[mw[mi][i]][i];
3323: s2=s[mw[mi+1][i]][i];
3324: bbh=(double)bh[mi][i]/(double)stepm;
3325: 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 */
3326: ipmx +=1;
3327: sw += weight[i];
3328: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3329: } /* end of wave */
3330: } /* end of individual */
3331: }else if (mle==4){ /* ml=4 no inter-extrapolation */
3332: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3333: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3334: for(mi=1; mi<= wav[i]-1; mi++){
3335: for (ii=1;ii<=nlstate+ndeath;ii++)
3336: for (j=1;j<=nlstate+ndeath;j++){
3337: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3338: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3339: }
3340: for(d=0; d<dh[mi][i]; d++){
3341: newm=savm;
3342: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3343: cov[2]=agexact;
3344: if(nagesqr==1)
3345: cov[3]= agexact*agexact;
3346: for (kk=1; kk<=cptcovage;kk++) {
3347: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3348: }
1.126 brouard 3349:
1.226 brouard 3350: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3351: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3352: savm=oldm;
3353: oldm=newm;
3354: } /* end mult */
3355:
3356: s1=s[mw[mi][i]][i];
3357: s2=s[mw[mi+1][i]][i];
3358: if( s2 > nlstate){
3359: lli=log(out[s1][s2] - savm[s1][s2]);
3360: } else if ( s2==-1 ) { /* alive */
3361: for (j=1,survp=0. ; j<=nlstate; j++)
3362: survp += out[s1][j];
3363: lli= log(survp);
3364: }else{
3365: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
3366: }
3367: ipmx +=1;
3368: sw += weight[i];
3369: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1.126 brouard 3370: /* 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.226 brouard 3371: } /* end of wave */
3372: } /* end of individual */
3373: }else{ /* ml=5 no inter-extrapolation no jackson =0.8a */
3374: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3375: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3376: for(mi=1; mi<= wav[i]-1; mi++){
3377: for (ii=1;ii<=nlstate+ndeath;ii++)
3378: for (j=1;j<=nlstate+ndeath;j++){
3379: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3380: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3381: }
3382: for(d=0; d<dh[mi][i]; d++){
3383: newm=savm;
3384: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3385: cov[2]=agexact;
3386: if(nagesqr==1)
3387: cov[3]= agexact*agexact;
3388: for (kk=1; kk<=cptcovage;kk++) {
3389: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3390: }
1.126 brouard 3391:
1.226 brouard 3392: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3393: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3394: savm=oldm;
3395: oldm=newm;
3396: } /* end mult */
3397:
3398: s1=s[mw[mi][i]][i];
3399: s2=s[mw[mi+1][i]][i];
3400: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
3401: ipmx +=1;
3402: sw += weight[i];
3403: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3404: /*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]);*/
3405: } /* end of wave */
3406: } /* end of individual */
3407: } /* End of if */
3408: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
3409: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
3410: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
3411: return -l;
1.126 brouard 3412: }
3413:
3414: /*************** log-likelihood *************/
3415: double funcone( double *x)
3416: {
1.228 brouard 3417: /* Same as func but slower because of a lot of printf and if */
1.126 brouard 3418: int i, ii, j, k, mi, d, kk;
1.228 brouard 3419: int ioffset=0;
1.131 brouard 3420: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
1.126 brouard 3421: double **out;
3422: double lli; /* Individual log likelihood */
3423: double llt;
3424: int s1, s2;
1.228 brouard 3425: int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate, quantitative time varying covariate */
3426:
1.126 brouard 3427: double bbh, survp;
1.187 brouard 3428: double agexact;
1.214 brouard 3429: double agebegin, ageend;
1.126 brouard 3430: /*extern weight */
3431: /* We are differentiating ll according to initial status */
3432: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
3433: /*for(i=1;i<imx;i++)
3434: printf(" %d\n",s[4][i]);
3435: */
3436: cov[1]=1.;
3437:
3438: for(k=1; k<=nlstate; k++) ll[k]=0.;
1.224 brouard 3439: ioffset=0;
3440: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1.225 brouard 3441: ioffset=2+nagesqr+cptcovage;
1.232 brouard 3442: /* Fixed */
1.224 brouard 3443: /* for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i]; */
1.232 brouard 3444: /* for (k=1; k<=ncoveff;k++){ /\* Simple and product fixed Dummy covariates without age* products *\/ */
3445: for (k=1; k<=ncovf;k++){ /* Simple and product fixed covariates without age* products */
3446: cov[ioffset+TvarFind[k]]=covar[Tvar[TvarFind[k]]][i];/* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, only V1 is fixed (k=6)*/
3447: /* cov[ioffset+TvarFind[1]]=covar[Tvar[TvarFind[1]]][i]; */
3448: /* cov[2+6]=covar[Tvar[6]][i]; */
3449: /* cov[2+6]=covar[2][i]; V2 */
3450: /* cov[TvarFind[2]]=covar[Tvar[TvarFind[2]]][i]; */
3451: /* cov[2+7]=covar[Tvar[7]][i]; */
3452: /* cov[2+7]=covar[7][i]; V7=V1*V2 */
3453: /* cov[TvarFind[3]]=covar[Tvar[TvarFind[3]]][i]; */
3454: /* cov[2+9]=covar[Tvar[9]][i]; */
3455: /* cov[2+9]=covar[1][i]; V1 */
1.225 brouard 3456: }
1.232 brouard 3457: /* for (k=1; k<=nqfveff;k++){ /\* Simple and product fixed Quantitative covariates without age* products *\/ */
3458: /* cov[++ioffset]=coqvar[TvarFQ[k]][i];/\* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, only V2 and V1*V2 is fixed (k=6 and 7?)*\/ */
3459: /* } */
1.231 brouard 3460: /* for(iqv=1; iqv <= nqfveff; iqv++){ /\* Quantitative fixed covariates *\/ */
3461: /* cov[++ioffset]=coqvar[Tvar[iqv]][i]; /\* Only V2 k=6 and V1*V2 7 *\/ */
3462: /* } */
1.225 brouard 3463:
1.233 brouard 3464:
3465: for(mi=1; mi<= wav[i]-1; mi++){ /* Varying with waves */
1.232 brouard 3466: /* Wave varying (but not age varying) */
3467: for(k=1; k <= ncovv ; k++){ /* Varying covariates (single and product but no age )*/
1.233 brouard 3468: cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i];
1.232 brouard 3469: }
3470: /* for(itv=1; itv <= ntveff; itv++){ /\* Varying dummy covariates (single??)*\/ */
1.231 brouard 3471: /* iv= Tvar[Tmodelind[ioffset-2-nagesqr-cptcovage+itv]]-ncovcol-nqv; /\* Counting the # varying covariate from 1 to ntveff *\/ */
3472: /* cov[ioffset+iv]=cotvar[mw[mi][i]][iv][i]; */
1.232 brouard 3473: /* k=ioffset-2-nagesqr-cptcovage+itv; /\* position in simple model *\/ */
3474: /* cov[ioffset+itv]=cotvar[mw[mi][i]][TmodelInvind[itv]][i]; */
1.231 brouard 3475: /* printf(" i=%d,mi=%d,itv=%d,TmodelInvind[itv]=%d,cotvar[mw[mi][i]][TmodelInvind[itv]][i]=%f\n", i, mi, itv, TmodelInvind[itv],cotvar[mw[mi][i]][TmodelInvind[itv]][i]); */
1.232 brouard 3476: /* for(iqtv=1; iqtv <= nqtveff; iqtv++){ /\* Varying quantitatives covariates *\/ */
3477: /* iv=TmodelInvQind[iqtv]; /\* Counting the # varying covariate from 1 to ntveff *\/ */
3478: /* /\* printf(" i=%d,mi=%d,iqtv=%d,TmodelInvQind[iqtv]=%d,cotqvar[mw[mi][i]][TmodelInvQind[iqtv]][i]=%f\n", i, mi, iqtv, TmodelInvQind[iqtv],cotqvar[mw[mi][i]][TmodelInvQind[iqtv]][i]); *\/ */
3479: /* cov[ioffset+ntveff+iqtv]=cotqvar[mw[mi][i]][TmodelInvQind[iqtv]][i]; */
3480: /* } */
1.126 brouard 3481: for (ii=1;ii<=nlstate+ndeath;ii++)
1.231 brouard 3482: for (j=1;j<=nlstate+ndeath;j++){
3483: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3484: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3485: }
1.214 brouard 3486:
3487: agebegin=agev[mw[mi][i]][i]; /* Age at beginning of effective wave */
3488: ageend=agev[mw[mi][i]][i] + (dh[mi][i])*stepm/YEARM; /* Age at end of effective wave and at the end of transition */
3489: for(d=0; d<dh[mi][i]; d++){ /* Delay between two effective waves */
1.231 brouard 3490: /*dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
3491: and mw[mi+1][i]. dh depends on stepm.*/
3492: newm=savm;
3493: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3494: cov[2]=agexact;
3495: if(nagesqr==1)
3496: cov[3]= agexact*agexact;
3497: for (kk=1; kk<=cptcovage;kk++) {
3498: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3499: }
3500: /* printf("i=%d,mi=%d,d=%d,mw[mi][i]=%d\n",i, mi,d,mw[mi][i]); */
3501: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
3502: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3503: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3504: /* out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath, */
3505: /* 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate)); */
3506: savm=oldm;
3507: oldm=newm;
1.126 brouard 3508: } /* end mult */
3509:
3510: s1=s[mw[mi][i]][i];
3511: s2=s[mw[mi+1][i]][i];
1.217 brouard 3512: /* if(s2==-1){ */
3513: /* printf(" s1=%d, s2=%d i=%d \n", s1, s2, i); */
3514: /* /\* exit(1); *\/ */
3515: /* } */
1.126 brouard 3516: bbh=(double)bh[mi][i]/(double)stepm;
3517: /* bias is positive if real duration
3518: * is higher than the multiple of stepm and negative otherwise.
3519: */
3520: if( s2 > nlstate && (mle <5) ){ /* Jackson */
1.232 brouard 3521: lli=log(out[s1][s2] - savm[s1][s2]);
1.216 brouard 3522: } else if ( s2==-1 ) { /* alive */
1.232 brouard 3523: for (j=1,survp=0. ; j<=nlstate; j++)
3524: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3525: lli= log(survp);
1.126 brouard 3526: }else if (mle==1){
1.232 brouard 3527: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
1.126 brouard 3528: } else if(mle==2){
1.232 brouard 3529: 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 */
1.126 brouard 3530: } else if(mle==3){ /* exponential inter-extrapolation */
1.232 brouard 3531: 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 */
1.126 brouard 3532: } else if (mle==4){ /* mle=4 no inter-extrapolation */
1.232 brouard 3533: lli=log(out[s1][s2]); /* Original formula */
1.136 brouard 3534: } else{ /* mle=0 back to 1 */
1.232 brouard 3535: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
3536: /*lli=log(out[s1][s2]); */ /* Original formula */
1.126 brouard 3537: } /* End of if */
3538: ipmx +=1;
3539: sw += weight[i];
3540: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1.132 brouard 3541: /*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 3542: if(globpr){
1.232 brouard 3543: fprintf(ficresilk,"%9ld %6.1f %6.1f %6d %2d %2d %2d %2d %3d %15.6f %8.4f %8.3f\
1.126 brouard 3544: %11.6f %11.6f %11.6f ", \
1.232 brouard 3545: num[i], agebegin, ageend, i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],weight[i]*gipmx/gsw,
3546: 2*weight[i]*lli,out[s1][s2],savm[s1][s2]);
3547: for(k=1,llt=0.,l=0.; k<=nlstate; k++){
3548: llt +=ll[k]*gipmx/gsw;
3549: fprintf(ficresilk," %10.6f",-ll[k]*gipmx/gsw);
3550: }
3551: fprintf(ficresilk," %10.6f\n", -llt);
1.126 brouard 3552: }
1.232 brouard 3553: } /* end of wave */
3554: } /* end of individual */
3555: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
3556: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
3557: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
3558: if(globpr==0){ /* First time we count the contributions and weights */
3559: gipmx=ipmx;
3560: gsw=sw;
3561: }
3562: return -l;
1.126 brouard 3563: }
3564:
3565:
3566: /*************** function likelione ***********/
3567: void likelione(FILE *ficres,double p[], int npar, int nlstate, int *globpri, long *ipmx, double *sw, double *fretone, double (*funcone)(double []))
3568: {
3569: /* This routine should help understanding what is done with
3570: the selection of individuals/waves and
3571: to check the exact contribution to the likelihood.
3572: Plotting could be done.
3573: */
3574: int k;
3575:
3576: if(*globpri !=0){ /* Just counts and sums, no printings */
1.201 brouard 3577: strcpy(fileresilk,"ILK_");
1.202 brouard 3578: strcat(fileresilk,fileresu);
1.126 brouard 3579: if((ficresilk=fopen(fileresilk,"w"))==NULL) {
3580: printf("Problem with resultfile: %s\n", fileresilk);
3581: fprintf(ficlog,"Problem with resultfile: %s\n", fileresilk);
3582: }
1.214 brouard 3583: fprintf(ficresilk, "#individual(line's_record) count ageb ageend s1 s2 wave# effective_wave# number_of_matrices_product pij weight weight/gpw -2ln(pij)*weight 0pij_x 0pij_(x-stepm) cumulating_loglikeli_by_health_state(reweighted=-2ll*weightXnumber_of_contribs/sum_of_weights) and_total\n");
3584: fprintf(ficresilk, "#num_i ageb agend i s1 s2 mi mw dh likeli weight %%weight 2wlli out sav ");
1.126 brouard 3585: /* i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],2*weight[i]*lli,out[s1][s2],savm[s1][s2]); */
3586: for(k=1; k<=nlstate; k++)
3587: fprintf(ficresilk," -2*gipw/gsw*weight*ll[%d]++",k);
3588: fprintf(ficresilk," -2*gipw/gsw*weight*ll(total)\n");
3589: }
3590:
3591: *fretone=(*funcone)(p);
3592: if(*globpri !=0){
3593: fclose(ficresilk);
1.205 brouard 3594: if (mle ==0)
3595: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with initial parameters and mle = %d.",mle);
3596: else if(mle >=1)
3597: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with optimized parameters mle = %d.",mle);
3598: fprintf(fichtm," You should at least run with mle >= 1 to get starting values corresponding to the optimized parameters in order to visualize the real contribution of each individual/wave: <a href=\"%s\">%s</a><br>\n",subdirf(fileresilk),subdirf(fileresilk));
1.207 brouard 3599:
1.208 brouard 3600:
3601: for (k=1; k<= nlstate ; k++) {
1.211 brouard 3602: fprintf(fichtm,"<br>- Probability p<sub>%dj</sub> by origin %d and destination j. Dot's sizes are related to corresponding weight: <a href=\"%s-p%dj.png\">%s-p%dj.png</a><br> \
1.208 brouard 3603: <img src=\"%s-p%dj.png\">",k,k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k);
3604: }
1.207 brouard 3605: fprintf(fichtm,"<br>- The function drawn is -2Log(L) in Log scale: by state of origin <a href=\"%s-ori.png\">%s-ori.png</a><br> \
1.204 brouard 3606: <img src=\"%s-ori.png\">",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
1.207 brouard 3607: fprintf(fichtm,"<br>- and by state of destination <a href=\"%s-dest.png\">%s-dest.png</a><br> \
1.204 brouard 3608: <img src=\"%s-dest.png\">",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
1.207 brouard 3609: fflush(fichtm);
1.205 brouard 3610: }
1.126 brouard 3611: return;
3612: }
3613:
3614:
3615: /*********** Maximum Likelihood Estimation ***************/
3616:
3617: void mlikeli(FILE *ficres,double p[], int npar, int ncovmodel, int nlstate, double ftol, double (*func)(double []))
3618: {
1.165 brouard 3619: int i,j, iter=0;
1.126 brouard 3620: double **xi;
3621: double fret;
3622: double fretone; /* Only one call to likelihood */
3623: /* char filerespow[FILENAMELENGTH];*/
1.162 brouard 3624:
3625: #ifdef NLOPT
3626: int creturn;
3627: nlopt_opt opt;
3628: /* double lb[9] = { -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL }; /\* lower bounds *\/ */
3629: double *lb;
3630: double minf; /* the minimum objective value, upon return */
3631: double * p1; /* Shifted parameters from 0 instead of 1 */
3632: myfunc_data dinst, *d = &dinst;
3633: #endif
3634:
3635:
1.126 brouard 3636: xi=matrix(1,npar,1,npar);
3637: for (i=1;i<=npar;i++)
3638: for (j=1;j<=npar;j++)
3639: xi[i][j]=(i==j ? 1.0 : 0.0);
3640: printf("Powell\n"); fprintf(ficlog,"Powell\n");
1.201 brouard 3641: strcpy(filerespow,"POW_");
1.126 brouard 3642: strcat(filerespow,fileres);
3643: if((ficrespow=fopen(filerespow,"w"))==NULL) {
3644: printf("Problem with resultfile: %s\n", filerespow);
3645: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
3646: }
3647: fprintf(ficrespow,"# Powell\n# iter -2*LL");
3648: for (i=1;i<=nlstate;i++)
3649: for(j=1;j<=nlstate+ndeath;j++)
3650: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
3651: fprintf(ficrespow,"\n");
1.162 brouard 3652: #ifdef POWELL
1.126 brouard 3653: powell(p,xi,npar,ftol,&iter,&fret,func);
1.162 brouard 3654: #endif
1.126 brouard 3655:
1.162 brouard 3656: #ifdef NLOPT
3657: #ifdef NEWUOA
3658: opt = nlopt_create(NLOPT_LN_NEWUOA,npar);
3659: #else
3660: opt = nlopt_create(NLOPT_LN_BOBYQA,npar);
3661: #endif
3662: lb=vector(0,npar-1);
3663: for (i=0;i<npar;i++) lb[i]= -HUGE_VAL;
3664: nlopt_set_lower_bounds(opt, lb);
3665: nlopt_set_initial_step1(opt, 0.1);
3666:
3667: p1= (p+1); /* p *(p+1)@8 and p *(p1)@8 are equal p1[0]=p[1] */
3668: d->function = func;
3669: printf(" Func %.12lf \n",myfunc(npar,p1,NULL,d));
3670: nlopt_set_min_objective(opt, myfunc, d);
3671: nlopt_set_xtol_rel(opt, ftol);
3672: if ((creturn=nlopt_optimize(opt, p1, &minf)) < 0) {
3673: printf("nlopt failed! %d\n",creturn);
3674: }
3675: else {
3676: printf("found minimum after %d evaluations (NLOPT=%d)\n", countcallfunc ,NLOPT);
3677: printf("found minimum at f(%g,%g) = %0.10g\n", p[0], p[1], minf);
3678: iter=1; /* not equal */
3679: }
3680: nlopt_destroy(opt);
3681: #endif
1.126 brouard 3682: free_matrix(xi,1,npar,1,npar);
3683: fclose(ficrespow);
1.203 brouard 3684: printf("\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
3685: fprintf(ficlog,"\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
1.180 brouard 3686: fprintf(ficres,"#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
1.126 brouard 3687:
3688: }
3689:
3690: /**** Computes Hessian and covariance matrix ***/
1.203 brouard 3691: void hesscov(double **matcov, double **hess, double p[], int npar, double delti[], double ftolhess, double (*func)(double []))
1.126 brouard 3692: {
3693: double **a,**y,*x,pd;
1.203 brouard 3694: /* double **hess; */
1.164 brouard 3695: int i, j;
1.126 brouard 3696: int *indx;
3697:
3698: double hessii(double p[], double delta, int theta, double delti[],double (*func)(double []),int npar);
1.203 brouard 3699: double hessij(double p[], double **hess, double delti[], int i, int j,double (*func)(double []),int npar);
1.126 brouard 3700: void lubksb(double **a, int npar, int *indx, double b[]) ;
3701: void ludcmp(double **a, int npar, int *indx, double *d) ;
3702: double gompertz(double p[]);
1.203 brouard 3703: /* hess=matrix(1,npar,1,npar); */
1.126 brouard 3704:
3705: printf("\nCalculation of the hessian matrix. Wait...\n");
3706: fprintf(ficlog,"\nCalculation of the hessian matrix. Wait...\n");
3707: for (i=1;i<=npar;i++){
1.203 brouard 3708: printf("%d-",i);fflush(stdout);
3709: fprintf(ficlog,"%d-",i);fflush(ficlog);
1.126 brouard 3710:
3711: hess[i][i]=hessii(p,ftolhess,i,delti,func,npar);
3712:
3713: /* printf(" %f ",p[i]);
3714: printf(" %lf %lf %lf",hess[i][i],ftolhess,delti[i]);*/
3715: }
3716:
3717: for (i=1;i<=npar;i++) {
3718: for (j=1;j<=npar;j++) {
3719: if (j>i) {
1.203 brouard 3720: printf(".%d-%d",i,j);fflush(stdout);
3721: fprintf(ficlog,".%d-%d",i,j);fflush(ficlog);
3722: hess[i][j]=hessij(p,hess, delti,i,j,func,npar);
1.126 brouard 3723:
3724: hess[j][i]=hess[i][j];
3725: /*printf(" %lf ",hess[i][j]);*/
3726: }
3727: }
3728: }
3729: printf("\n");
3730: fprintf(ficlog,"\n");
3731:
3732: printf("\nInverting the hessian to get the covariance matrix. Wait...\n");
3733: fprintf(ficlog,"\nInverting the hessian to get the covariance matrix. Wait...\n");
3734:
3735: a=matrix(1,npar,1,npar);
3736: y=matrix(1,npar,1,npar);
3737: x=vector(1,npar);
3738: indx=ivector(1,npar);
3739: for (i=1;i<=npar;i++)
3740: for (j=1;j<=npar;j++) a[i][j]=hess[i][j];
3741: ludcmp(a,npar,indx,&pd);
3742:
3743: for (j=1;j<=npar;j++) {
3744: for (i=1;i<=npar;i++) x[i]=0;
3745: x[j]=1;
3746: lubksb(a,npar,indx,x);
3747: for (i=1;i<=npar;i++){
3748: matcov[i][j]=x[i];
3749: }
3750: }
3751:
3752: printf("\n#Hessian matrix#\n");
3753: fprintf(ficlog,"\n#Hessian matrix#\n");
3754: for (i=1;i<=npar;i++) {
3755: for (j=1;j<=npar;j++) {
1.203 brouard 3756: printf("%.6e ",hess[i][j]);
3757: fprintf(ficlog,"%.6e ",hess[i][j]);
1.126 brouard 3758: }
3759: printf("\n");
3760: fprintf(ficlog,"\n");
3761: }
3762:
1.203 brouard 3763: /* printf("\n#Covariance matrix#\n"); */
3764: /* fprintf(ficlog,"\n#Covariance matrix#\n"); */
3765: /* for (i=1;i<=npar;i++) { */
3766: /* for (j=1;j<=npar;j++) { */
3767: /* printf("%.6e ",matcov[i][j]); */
3768: /* fprintf(ficlog,"%.6e ",matcov[i][j]); */
3769: /* } */
3770: /* printf("\n"); */
3771: /* fprintf(ficlog,"\n"); */
3772: /* } */
3773:
1.126 brouard 3774: /* Recompute Inverse */
1.203 brouard 3775: /* for (i=1;i<=npar;i++) */
3776: /* for (j=1;j<=npar;j++) a[i][j]=matcov[i][j]; */
3777: /* ludcmp(a,npar,indx,&pd); */
3778:
3779: /* printf("\n#Hessian matrix recomputed#\n"); */
3780:
3781: /* for (j=1;j<=npar;j++) { */
3782: /* for (i=1;i<=npar;i++) x[i]=0; */
3783: /* x[j]=1; */
3784: /* lubksb(a,npar,indx,x); */
3785: /* for (i=1;i<=npar;i++){ */
3786: /* y[i][j]=x[i]; */
3787: /* printf("%.3e ",y[i][j]); */
3788: /* fprintf(ficlog,"%.3e ",y[i][j]); */
3789: /* } */
3790: /* printf("\n"); */
3791: /* fprintf(ficlog,"\n"); */
3792: /* } */
3793:
3794: /* Verifying the inverse matrix */
3795: #ifdef DEBUGHESS
3796: y=matprod2(y,hess,1,npar,1,npar,1,npar,matcov);
1.126 brouard 3797:
1.203 brouard 3798: printf("\n#Verification: multiplying the matrix of covariance by the Hessian matrix, should be unity:#\n");
3799: fprintf(ficlog,"\n#Verification: multiplying the matrix of covariance by the Hessian matrix. Should be unity:#\n");
1.126 brouard 3800:
3801: for (j=1;j<=npar;j++) {
3802: for (i=1;i<=npar;i++){
1.203 brouard 3803: printf("%.2f ",y[i][j]);
3804: fprintf(ficlog,"%.2f ",y[i][j]);
1.126 brouard 3805: }
3806: printf("\n");
3807: fprintf(ficlog,"\n");
3808: }
1.203 brouard 3809: #endif
1.126 brouard 3810:
3811: free_matrix(a,1,npar,1,npar);
3812: free_matrix(y,1,npar,1,npar);
3813: free_vector(x,1,npar);
3814: free_ivector(indx,1,npar);
1.203 brouard 3815: /* free_matrix(hess,1,npar,1,npar); */
1.126 brouard 3816:
3817:
3818: }
3819:
3820: /*************** hessian matrix ****************/
3821: double hessii(double x[], double delta, int theta, double delti[], double (*func)(double []), int npar)
1.203 brouard 3822: { /* Around values of x, computes the function func and returns the scales delti and hessian */
1.126 brouard 3823: int i;
3824: int l=1, lmax=20;
1.203 brouard 3825: double k1,k2, res, fx;
1.132 brouard 3826: double p2[MAXPARM+1]; /* identical to x */
1.126 brouard 3827: double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4;
3828: int k=0,kmax=10;
3829: double l1;
3830:
3831: fx=func(x);
3832: for (i=1;i<=npar;i++) p2[i]=x[i];
1.145 brouard 3833: for(l=0 ; l <=lmax; l++){ /* Enlarging the zone around the Maximum */
1.126 brouard 3834: l1=pow(10,l);
3835: delts=delt;
3836: for(k=1 ; k <kmax; k=k+1){
3837: delt = delta*(l1*k);
3838: p2[theta]=x[theta] +delt;
1.145 brouard 3839: k1=func(p2)-fx; /* Might be negative if too close to the theoretical maximum */
1.126 brouard 3840: p2[theta]=x[theta]-delt;
3841: k2=func(p2)-fx;
3842: /*res= (k1-2.0*fx+k2)/delt/delt; */
1.203 brouard 3843: res= (k1+k2)/delt/delt/2.; /* Divided by 2 because L and not 2*L */
1.126 brouard 3844:
1.203 brouard 3845: #ifdef DEBUGHESSII
1.126 brouard 3846: 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);
3847: 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);
3848: #endif
3849: /*if(fabs(k1-2.0*fx+k2) <1.e-13){ */
3850: if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)){
3851: k=kmax;
3852: }
3853: else if((k1 >khi/nkhif) || (k2 >khi/nkhif)){ /* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. */
1.164 brouard 3854: k=kmax; l=lmax*10;
1.126 brouard 3855: }
3856: else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){
3857: delts=delt;
3858: }
1.203 brouard 3859: } /* End loop k */
1.126 brouard 3860: }
3861: delti[theta]=delts;
3862: return res;
3863:
3864: }
3865:
1.203 brouard 3866: double hessij( double x[], double **hess, double delti[], int thetai,int thetaj,double (*func)(double []),int npar)
1.126 brouard 3867: {
3868: int i;
1.164 brouard 3869: int l=1, lmax=20;
1.126 brouard 3870: double k1,k2,k3,k4,res,fx;
1.132 brouard 3871: double p2[MAXPARM+1];
1.203 brouard 3872: int k, kmax=1;
3873: double v1, v2, cv12, lc1, lc2;
1.208 brouard 3874:
3875: int firstime=0;
1.203 brouard 3876:
1.126 brouard 3877: fx=func(x);
1.203 brouard 3878: for (k=1; k<=kmax; k=k+10) {
1.126 brouard 3879: for (i=1;i<=npar;i++) p2[i]=x[i];
1.203 brouard 3880: p2[thetai]=x[thetai]+delti[thetai]*k;
3881: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
1.126 brouard 3882: k1=func(p2)-fx;
3883:
1.203 brouard 3884: p2[thetai]=x[thetai]+delti[thetai]*k;
3885: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
1.126 brouard 3886: k2=func(p2)-fx;
3887:
1.203 brouard 3888: p2[thetai]=x[thetai]-delti[thetai]*k;
3889: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
1.126 brouard 3890: k3=func(p2)-fx;
3891:
1.203 brouard 3892: p2[thetai]=x[thetai]-delti[thetai]*k;
3893: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
1.126 brouard 3894: k4=func(p2)-fx;
1.203 brouard 3895: res=(k1-k2-k3+k4)/4.0/delti[thetai]/k/delti[thetaj]/k/2.; /* Because of L not 2*L */
3896: if(k1*k2*k3*k4 <0.){
1.208 brouard 3897: firstime=1;
1.203 brouard 3898: kmax=kmax+10;
1.208 brouard 3899: }
3900: if(kmax >=10 || firstime ==1){
1.218 brouard 3901: printf("Warning: directions %d-%d, you are not estimating the Hessian at the exact maximum likelihood; you may increase ftol=%.2e\n",thetai,thetaj, ftol);
3902: fprintf(ficlog,"Warning: directions %d-%d, you are not estimating the Hessian at the exact maximum likelihood; you may increase ftol=%.2e\n",thetai,thetaj, ftol);
1.203 brouard 3903: 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);
3904: 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);
3905: }
3906: #ifdef DEBUGHESSIJ
3907: v1=hess[thetai][thetai];
3908: v2=hess[thetaj][thetaj];
3909: cv12=res;
3910: /* Computing eigen value of Hessian matrix */
3911: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
3912: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
3913: if ((lc2 <0) || (lc1 <0) ){
3914: printf("Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
3915: fprintf(ficlog, "Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
3916: 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);
3917: 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);
3918: }
1.126 brouard 3919: #endif
3920: }
3921: return res;
3922: }
3923:
1.203 brouard 3924: /* Not done yet: Was supposed to fix if not exactly at the maximum */
3925: /* double hessij( double x[], double delti[], int thetai,int thetaj,double (*func)(double []),int npar) */
3926: /* { */
3927: /* int i; */
3928: /* int l=1, lmax=20; */
3929: /* double k1,k2,k3,k4,res,fx; */
3930: /* double p2[MAXPARM+1]; */
3931: /* double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4; */
3932: /* int k=0,kmax=10; */
3933: /* double l1; */
3934:
3935: /* fx=func(x); */
3936: /* for(l=0 ; l <=lmax; l++){ /\* Enlarging the zone around the Maximum *\/ */
3937: /* l1=pow(10,l); */
3938: /* delts=delt; */
3939: /* for(k=1 ; k <kmax; k=k+1){ */
3940: /* delt = delti*(l1*k); */
3941: /* for (i=1;i<=npar;i++) p2[i]=x[i]; */
3942: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
3943: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
3944: /* k1=func(p2)-fx; */
3945:
3946: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
3947: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
3948: /* k2=func(p2)-fx; */
3949:
3950: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
3951: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
3952: /* k3=func(p2)-fx; */
3953:
3954: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
3955: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
3956: /* k4=func(p2)-fx; */
3957: /* res=(k1-k2-k3+k4)/4.0/delti[thetai]*k/delti[thetaj]*k/2.; /\* Because of L not 2*L *\/ */
3958: /* #ifdef DEBUGHESSIJ */
3959: /* 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); */
3960: /* 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); */
3961: /* #endif */
3962: /* if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)){ */
3963: /* k=kmax; */
3964: /* } */
3965: /* else if((k1 >khi/nkhif) || (k2 >khi/nkhif) || (k4 >khi/nkhif) || (k4 >khi/nkhif)){ /\* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. *\/ */
3966: /* k=kmax; l=lmax*10; */
3967: /* } */
3968: /* else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){ */
3969: /* delts=delt; */
3970: /* } */
3971: /* } /\* End loop k *\/ */
3972: /* } */
3973: /* delti[theta]=delts; */
3974: /* return res; */
3975: /* } */
3976:
3977:
1.126 brouard 3978: /************** Inverse of matrix **************/
3979: void ludcmp(double **a, int n, int *indx, double *d)
3980: {
3981: int i,imax,j,k;
3982: double big,dum,sum,temp;
3983: double *vv;
3984:
3985: vv=vector(1,n);
3986: *d=1.0;
3987: for (i=1;i<=n;i++) {
3988: big=0.0;
3989: for (j=1;j<=n;j++)
3990: if ((temp=fabs(a[i][j])) > big) big=temp;
3991: if (big == 0.0) nrerror("Singular matrix in routine ludcmp");
3992: vv[i]=1.0/big;
3993: }
3994: for (j=1;j<=n;j++) {
3995: for (i=1;i<j;i++) {
3996: sum=a[i][j];
3997: for (k=1;k<i;k++) sum -= a[i][k]*a[k][j];
3998: a[i][j]=sum;
3999: }
4000: big=0.0;
4001: for (i=j;i<=n;i++) {
4002: sum=a[i][j];
4003: for (k=1;k<j;k++)
4004: sum -= a[i][k]*a[k][j];
4005: a[i][j]=sum;
4006: if ( (dum=vv[i]*fabs(sum)) >= big) {
4007: big=dum;
4008: imax=i;
4009: }
4010: }
4011: if (j != imax) {
4012: for (k=1;k<=n;k++) {
4013: dum=a[imax][k];
4014: a[imax][k]=a[j][k];
4015: a[j][k]=dum;
4016: }
4017: *d = -(*d);
4018: vv[imax]=vv[j];
4019: }
4020: indx[j]=imax;
4021: if (a[j][j] == 0.0) a[j][j]=TINY;
4022: if (j != n) {
4023: dum=1.0/(a[j][j]);
4024: for (i=j+1;i<=n;i++) a[i][j] *= dum;
4025: }
4026: }
4027: free_vector(vv,1,n); /* Doesn't work */
4028: ;
4029: }
4030:
4031: void lubksb(double **a, int n, int *indx, double b[])
4032: {
4033: int i,ii=0,ip,j;
4034: double sum;
4035:
4036: for (i=1;i<=n;i++) {
4037: ip=indx[i];
4038: sum=b[ip];
4039: b[ip]=b[i];
4040: if (ii)
4041: for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j];
4042: else if (sum) ii=i;
4043: b[i]=sum;
4044: }
4045: for (i=n;i>=1;i--) {
4046: sum=b[i];
4047: for (j=i+1;j<=n;j++) sum -= a[i][j]*b[j];
4048: b[i]=sum/a[i][i];
4049: }
4050: }
4051:
4052: void pstamp(FILE *fichier)
4053: {
1.196 brouard 4054: fprintf(fichier,"# %s.%s\n#IMaCh version %s, %s\n#%s\n# %s", optionfilefiname,optionfilext,version,copyright, fullversion, strstart);
1.126 brouard 4055: }
4056:
4057: /************ Frequencies ********************/
1.226 brouard 4058: void freqsummary(char fileres[], int iagemin, int iagemax, int **s, double **agev, int nlstate, int imx, \
4059: int *Tvaraff, int *invalidvarcomb, int **nbcode, int *ncodemax,double **mint,double **anint, char strstart[], \
4060: int firstpass, int lastpass, int stepm, int weightopt, char model[])
4061: { /* Some frequencies */
4062:
1.227 brouard 4063: int i, m, jk, j1, bool, z1,j, k, iv;
1.226 brouard 4064: int iind=0, iage=0;
4065: int mi; /* Effective wave */
4066: int first;
4067: double ***freq; /* Frequencies */
4068: double *meanq;
4069: double **meanqt;
4070: double *pp, **prop, *posprop, *pospropt;
4071: double pos=0., posproptt=0., pospropta=0., k2, dateintsum=0,k2cpt=0;
4072: char fileresp[FILENAMELENGTH], fileresphtm[FILENAMELENGTH], fileresphtmfr[FILENAMELENGTH];
4073: double agebegin, ageend;
4074:
4075: pp=vector(1,nlstate);
4076: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+3+AGEMARGE);
4077: posprop=vector(1,nlstate); /* Counting the number of transition starting from a live state per age */
4078: pospropt=vector(1,nlstate); /* Counting the number of transition starting from a live state */
4079: /* prop=matrix(1,nlstate,iagemin,iagemax+3); */
4080: meanq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
4081: meanqt=matrix(1,lastpass,1,nqtveff);
4082: strcpy(fileresp,"P_");
4083: strcat(fileresp,fileresu);
4084: /*strcat(fileresphtm,fileresu);*/
4085: if((ficresp=fopen(fileresp,"w"))==NULL) {
4086: printf("Problem with prevalence resultfile: %s\n", fileresp);
4087: fprintf(ficlog,"Problem with prevalence resultfile: %s\n", fileresp);
4088: exit(0);
4089: }
1.214 brouard 4090:
1.226 brouard 4091: strcpy(fileresphtm,subdirfext(optionfilefiname,"PHTM_",".htm"));
4092: if((ficresphtm=fopen(fileresphtm,"w"))==NULL) {
4093: printf("Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
4094: fprintf(ficlog,"Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
4095: fflush(ficlog);
4096: exit(70);
4097: }
4098: else{
4099: fprintf(ficresphtm,"<html><head>\n<title>IMaCh PHTM_ %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
1.214 brouard 4100: <hr size=\"2\" color=\"#EC5E5E\"> \n\
4101: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.226 brouard 4102: fileresphtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
4103: }
1.237 ! brouard 4104: fprintf(ficresphtm,"Current page is file <a href=\"%s\">%s</a><br>\n\n<h4>Frequencies and prevalence by age at begin of transition and dummy covariate value at beginning of transition</h4>\n",fileresphtm, fileresphtm);
1.214 brouard 4105:
1.226 brouard 4106: strcpy(fileresphtmfr,subdirfext(optionfilefiname,"PHTMFR_",".htm"));
4107: if((ficresphtmfr=fopen(fileresphtmfr,"w"))==NULL) {
4108: printf("Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
4109: fprintf(ficlog,"Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
4110: fflush(ficlog);
4111: exit(70);
4112: }
4113: else{
4114: fprintf(ficresphtmfr,"<html><head>\n<title>IMaCh PHTM_Frequency table %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
1.214 brouard 4115: <hr size=\"2\" color=\"#EC5E5E\"> \n\
4116: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.226 brouard 4117: fileresphtmfr,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
4118: }
4119: fprintf(ficresphtmfr,"Current page is file <a href=\"%s\">%s</a><br>\n\n<h4>Frequencies of all effective transitions by age at begin of transition </h4>Unknown status is -1<br/>\n",fileresphtmfr, fileresphtmfr);
1.214 brouard 4120:
1.226 brouard 4121: freq= ma3x(-5,nlstate+ndeath,-5,nlstate+ndeath,iagemin-AGEMARGE,iagemax+3+AGEMARGE);
4122: j1=0;
1.126 brouard 4123:
1.227 brouard 4124: /* j=ncoveff; /\* Only fixed dummy covariates *\/ */
4125: j=cptcoveff; /* Only dummy covariates of the model */
1.226 brouard 4126: if (cptcovn<1) {j=1;ncodemax[1]=1;}
1.220 brouard 4127:
1.226 brouard 4128: first=1;
1.220 brouard 4129:
1.226 brouard 4130: /* Detects if a combination j1 is empty: for a multinomial variable like 3 education levels:
4131: reference=low_education V1=0,V2=0
4132: med_educ V1=1 V2=0,
4133: high_educ V1=0 V2=1
4134: Then V1=1 and V2=1 is a noisy combination that we want to exclude for the list 2**cptcoveff
4135: */
1.126 brouard 4136:
1.227 brouard 4137: for (j1 = 1; j1 <= (int) pow(2,j); j1++){ /* Loop on covariates combination in order of model, excluding quantitatives V4=0, V3=0 for example, fixed or varying covariates */
1.226 brouard 4138: posproptt=0.;
4139: /*printf("cptcoveff=%d Tvaraff=%d", cptcoveff,Tvaraff[1]);
4140: scanf("%d", i);*/
4141: for (i=-5; i<=nlstate+ndeath; i++)
4142: for (jk=-5; jk<=nlstate+ndeath; jk++)
1.231 brouard 4143: for(m=iagemin; m <= iagemax+3; m++)
4144: freq[i][jk][m]=0;
4145:
1.226 brouard 4146: for (i=1; i<=nlstate; i++) {
4147: for(m=iagemin; m <= iagemax+3; m++)
1.231 brouard 4148: prop[i][m]=0;
1.226 brouard 4149: posprop[i]=0;
4150: pospropt[i]=0;
4151: }
1.227 brouard 4152: /* for (z1=1; z1<= nqfveff; z1++) { */
4153: /* meanq[z1]+=0.; */
4154: /* for(m=1;m<=lastpass;m++){ */
4155: /* meanqt[m][z1]=0.; */
4156: /* } */
4157: /* } */
1.231 brouard 4158:
1.226 brouard 4159: dateintsum=0;
4160: k2cpt=0;
1.227 brouard 4161: /* For that combination of covariate j1, we count and print the frequencies in one pass */
1.226 brouard 4162: for (iind=1; iind<=imx; iind++) { /* For each individual iind */
4163: bool=1;
1.227 brouard 4164: if(anyvaryingduminmodel==0){ /* If All fixed covariates */
1.234 brouard 4165: if (cptcoveff >0) { /* Filter is here: Must be looked at for model=V1+V2+V3+V4 */
1.227 brouard 4166: /* for (z1=1; z1<= nqfveff; z1++) { */
4167: /* meanq[z1]+=coqvar[Tvar[z1]][iind]; /\* Computes mean of quantitative with selected filter *\/ */
4168: /* } */
1.234 brouard 4169: for (z1=1; z1<=cptcoveff; z1++) {
4170: /* if(Tvaraff[z1] ==-20){ */
4171: /* /\* sumnew+=cotvar[mw[mi][iind]][z1][iind]; *\/ */
4172: /* }else if(Tvaraff[z1] ==-10){ */
4173: /* /\* sumnew+=coqvar[z1][iind]; *\/ */
4174: /* }else */
4175: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]){
4176: /* Tests if this individual iind responded to j1 (V4=1 V3=0) */
4177: bool=0;
4178: /* printf("bool=%d i=%d, z1=%d, Tvaraff[%d]=%d, covar[Tvarff][%d]=%2f, codtabm(%d,%d)=%d, nbcode[Tvaraff][codtabm(%d,%d)=%d, j1=%d\n",
4179: bool,i,z1, z1, Tvaraff[z1],i,covar[Tvaraff[z1]][i],j1,z1,codtabm(j1,z1),
4180: j1,z1,nbcode[Tvaraff[z1]][codtabm(j1,z1)],j1);*/
4181: /* For j1=7 in V1+V2+V3+V4 = 0 1 1 0 and codtabm(7,3)=1 and nbcde[3][?]=1*/
4182: } /* Onlyf fixed */
4183: } /* end z1 */
4184: } /* cptcovn > 0 */
1.227 brouard 4185: } /* end any */
4186: if (bool==1){ /* We selected an individual iind satisfying combination j1 or all fixed */
1.234 brouard 4187: /* for(m=firstpass; m<=lastpass; m++){ */
4188: for(mi=1; mi<wav[iind];mi++){ /* For that wave */
4189: m=mw[mi][iind];
4190: if(anyvaryingduminmodel==1){ /* Some are varying covariates */
4191: for (z1=1; z1<=cptcoveff; z1++) {
4192: if( Fixed[Tmodelind[z1]]==1){
4193: iv= Tvar[Tmodelind[z1]]-ncovcol-nqv;
4194: if (cotvar[m][iv][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) /* iv=1 to ntv, right modality */
4195: bool=0;
4196: }else if( Fixed[Tmodelind[z1]]== 0) { /* fixed */
4197: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) {
4198: bool=0;
4199: }
4200: }
4201: }
4202: }/* Some are varying covariates, we tried to speed up if all fixed covariates in the model, avoiding waves loop */
4203: /* bool =0 we keep that guy which corresponds to the combination of dummy values */
4204: if(bool==1){
4205: /* dh[m][iind] or dh[mw[mi][iind]][iind] is the delay between two effective (mi) waves m=mw[mi][iind]
4206: and mw[mi+1][iind]. dh depends on stepm. */
4207: agebegin=agev[m][iind]; /* Age at beginning of wave before transition*/
4208: ageend=agev[m][iind]+(dh[m][iind])*stepm/YEARM; /* Age at end of wave and transition */
4209: if(m >=firstpass && m <=lastpass){
4210: k2=anint[m][iind]+(mint[m][iind]/12.);
4211: /*if ((k2>=dateprev1) && (k2<=dateprev2)) {*/
4212: if(agev[m][iind]==0) agev[m][iind]=iagemax+1; /* All ages equal to 0 are in iagemax+1 */
4213: if(agev[m][iind]==1) agev[m][iind]=iagemax+2; /* All ages equal to 1 are in iagemax+2 */
4214: if (s[m][iind]>0 && s[m][iind]<=nlstate) /* If status at wave m is known and a live state */
4215: prop[s[m][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */
4216: if (m<lastpass) {
4217: /* if(s[m][iind]==4 && s[m+1][iind]==4) */
4218: /* printf(" num=%ld m=%d, iind=%d s1=%d s2=%d agev at m=%d\n", num[iind], m, iind,s[m][iind],s[m+1][iind], (int)agev[m][iind]); */
4219: if(s[m][iind]==-1)
4220: printf(" num=%ld m=%d, iind=%d s1=%d s2=%d agev at m=%d agebegin=%.2f ageend=%.2f, agemed=%d\n", num[iind], m, iind,s[m][iind],s[m+1][iind], (int)agev[m][iind],agebegin, ageend, (int)((agebegin+ageend)/2.));
4221: freq[s[m][iind]][s[m+1][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */
4222: /* freq[s[m][iind]][s[m+1][iind]][(int)((agebegin+ageend)/2.)] += weight[iind]; */
4223: freq[s[m][iind]][s[m+1][iind]][iagemax+3] += weight[iind]; /* Total is in iagemax+3 *//* At age of beginning of transition, where status is known */
4224: }
4225: } /* end if between passes */
4226: if ((agev[m][iind]>1) && (agev[m][iind]< (iagemax+3)) && (anint[m][iind]!=9999) && (mint[m][iind]!=99)) {
4227: dateintsum=dateintsum+k2;
4228: k2cpt++;
4229: /* printf("iind=%ld dateintmean = %lf dateintsum=%lf k2cpt=%lf k2=%lf\n",iind, dateintsum/k2cpt, dateintsum,k2cpt, k2); */
4230: }
4231: } /* end bool 2 */
4232: } /* end m */
1.226 brouard 4233: } /* end bool */
4234: } /* end iind = 1 to imx */
4235: /* prop[s][age] is feeded for any initial and valid live state as well as
4236: freq[s1][s2][age] at single age of beginning the transition, for a combination j1 */
1.231 brouard 4237:
4238:
1.226 brouard 4239: /* fprintf(ficresp, "#Count between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);*/
4240: pstamp(ficresp);
1.227 brouard 4241: /* if (ncoveff>0) { */
4242: if (cptcoveff>0) {
1.226 brouard 4243: fprintf(ficresp, "\n#********** Variable ");
4244: fprintf(ficresphtm, "\n<br/><br/><h3>********** Variable ");
4245: fprintf(ficresphtmfr, "\n<br/><br/><h3>********** Variable ");
1.227 brouard 4246: for (z1=1; z1<=cptcoveff; z1++){
1.234 brouard 4247: fprintf(ficresp, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4248: fprintf(ficresphtm, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4249: fprintf(ficresphtmfr, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.226 brouard 4250: }
4251: fprintf(ficresp, "**********\n#");
4252: fprintf(ficresphtm, "**********</h3>\n");
4253: fprintf(ficresphtmfr, "**********</h3>\n");
4254: fprintf(ficlog, "\n#********** Variable ");
1.227 brouard 4255: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficlog, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.226 brouard 4256: fprintf(ficlog, "**********\n");
4257: }
4258: fprintf(ficresphtm,"<table style=\"text-align:center; border: 1px solid\">");
4259: for(i=1; i<=nlstate;i++) {
4260: fprintf(ficresp, " Age Prev(%d) N(%d) N",i,i);
4261: fprintf(ficresphtm, "<th>Age</th><th>Prev(%d)</th><th>N(%d)</th><th>N</th>",i,i);
4262: }
4263: fprintf(ficresp, "\n");
4264: fprintf(ficresphtm, "\n");
1.231 brouard 4265:
1.226 brouard 4266: /* Header of frequency table by age */
4267: fprintf(ficresphtmfr,"<table style=\"text-align:center; border: 1px solid\">");
4268: fprintf(ficresphtmfr,"<th>Age</th> ");
4269: for(jk=-1; jk <=nlstate+ndeath; jk++){
4270: for(m=-1; m <=nlstate+ndeath; m++){
1.234 brouard 4271: if(jk!=0 && m!=0)
4272: fprintf(ficresphtmfr,"<th>%d%d</th> ",jk,m);
1.226 brouard 4273: }
4274: }
4275: fprintf(ficresphtmfr, "\n");
1.231 brouard 4276:
1.226 brouard 4277: /* For each age */
4278: for(iage=iagemin; iage <= iagemax+3; iage++){
4279: fprintf(ficresphtm,"<tr>");
4280: if(iage==iagemax+1){
1.231 brouard 4281: fprintf(ficlog,"1");
4282: fprintf(ficresphtmfr,"<tr><th>0</th> ");
1.226 brouard 4283: }else if(iage==iagemax+2){
1.231 brouard 4284: fprintf(ficlog,"0");
4285: fprintf(ficresphtmfr,"<tr><th>Unknown</th> ");
1.226 brouard 4286: }else if(iage==iagemax+3){
1.231 brouard 4287: fprintf(ficlog,"Total");
4288: fprintf(ficresphtmfr,"<tr><th>Total</th> ");
1.226 brouard 4289: }else{
1.231 brouard 4290: if(first==1){
4291: first=0;
4292: printf("See log file for details...\n");
4293: }
4294: fprintf(ficresphtmfr,"<tr><th>%d</th> ",iage);
4295: fprintf(ficlog,"Age %d", iage);
1.226 brouard 4296: }
4297: for(jk=1; jk <=nlstate ; jk++){
1.231 brouard 4298: for(m=-1, pp[jk]=0; m <=nlstate+ndeath ; m++)
4299: pp[jk] += freq[jk][m][iage];
1.226 brouard 4300: }
4301: for(jk=1; jk <=nlstate ; jk++){
1.231 brouard 4302: for(m=-1, pos=0; m <=0 ; m++)
4303: pos += freq[jk][m][iage];
4304: if(pp[jk]>=1.e-10){
4305: if(first==1){
4306: printf(" %d.=%.0f loss[%d]=%.1f%%",jk,pp[jk],jk,100*pos/pp[jk]);
4307: }
4308: fprintf(ficlog," %d.=%.0f loss[%d]=%.1f%%",jk,pp[jk],jk,100*pos/pp[jk]);
4309: }else{
4310: if(first==1)
4311: printf(" %d.=%.0f loss[%d]=NaNQ%%",jk,pp[jk],jk);
4312: fprintf(ficlog," %d.=%.0f loss[%d]=NaNQ%%",jk,pp[jk],jk);
4313: }
1.226 brouard 4314: }
1.231 brouard 4315:
1.226 brouard 4316: for(jk=1; jk <=nlstate ; jk++){
1.231 brouard 4317: /* posprop[jk]=0; */
4318: for(m=0, pp[jk]=0; m <=nlstate+ndeath; m++)/* Summing on all ages */
4319: pp[jk] += freq[jk][m][iage];
1.226 brouard 4320: } /* pp[jk] is the total number of transitions starting from state jk and any ending status until this age */
1.231 brouard 4321:
1.226 brouard 4322: for(jk=1,pos=0, pospropta=0.; jk <=nlstate ; jk++){
1.231 brouard 4323: pos += pp[jk]; /* pos is the total number of transitions until this age */
4324: posprop[jk] += prop[jk][iage]; /* prop is the number of transitions from a live state
4325: from jk at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
4326: pospropta += prop[jk][iage]; /* prop is the number of transitions from a live state
4327: from jk at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
1.226 brouard 4328: }
4329: for(jk=1; jk <=nlstate ; jk++){
1.231 brouard 4330: if(pos>=1.e-5){
4331: if(first==1)
4332: printf(" %d.=%.0f prev[%d]=%.1f%%",jk,pp[jk],jk,100*pp[jk]/pos);
4333: fprintf(ficlog," %d.=%.0f prev[%d]=%.1f%%",jk,pp[jk],jk,100*pp[jk]/pos);
4334: }else{
4335: if(first==1)
4336: printf(" %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk);
4337: fprintf(ficlog," %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk);
4338: }
4339: if( iage <= iagemax){
4340: if(pos>=1.e-5){
4341: fprintf(ficresp," %d %.5f %.0f %.0f",iage,prop[jk][iage]/pospropta, prop[jk][iage],pospropta);
4342: fprintf(ficresphtm,"<th>%d</th><td>%.5f</td><td>%.0f</td><td>%.0f</td>",iage,prop[jk][iage]/pospropta, prop[jk][iage],pospropta);
4343: /*probs[iage][jk][j1]= pp[jk]/pos;*/
4344: /*printf("\niage=%d jk=%d j1=%d %.5f %.0f %.0f %f",iage,jk,j1,pp[jk]/pos, pp[jk],pos,probs[iage][jk][j1]);*/
4345: }
4346: else{
4347: fprintf(ficresp," %d NaNq %.0f %.0f",iage,prop[jk][iage],pospropta);
4348: fprintf(ficresphtm,"<th>%d</th><td>NaNq</td><td>%.0f</td><td>%.0f</td>",iage, prop[jk][iage],pospropta);
4349: }
4350: }
4351: pospropt[jk] +=posprop[jk];
1.226 brouard 4352: } /* end loop jk */
4353: /* pospropt=0.; */
4354: for(jk=-1; jk <=nlstate+ndeath; jk++){
1.231 brouard 4355: for(m=-1; m <=nlstate+ndeath; m++){
4356: if(freq[jk][m][iage] !=0 ) { /* minimizing output */
4357: if(first==1){
4358: printf(" %d%d=%.0f",jk,m,freq[jk][m][iage]);
4359: }
4360: fprintf(ficlog," %d%d=%.0f",jk,m,freq[jk][m][iage]);
4361: }
4362: if(jk!=0 && m!=0)
4363: fprintf(ficresphtmfr,"<td>%.0f</td> ",freq[jk][m][iage]);
4364: }
1.226 brouard 4365: } /* end loop jk */
4366: posproptt=0.;
4367: for(jk=1; jk <=nlstate; jk++){
1.231 brouard 4368: posproptt += pospropt[jk];
1.226 brouard 4369: }
4370: fprintf(ficresphtmfr,"</tr>\n ");
4371: if(iage <= iagemax){
1.231 brouard 4372: fprintf(ficresp,"\n");
4373: fprintf(ficresphtm,"</tr>\n");
1.226 brouard 4374: }
4375: if(first==1)
1.231 brouard 4376: printf("Others in log...\n");
1.226 brouard 4377: fprintf(ficlog,"\n");
4378: } /* end loop age iage */
4379: fprintf(ficresphtm,"<tr><th>Tot</th>");
4380: for(jk=1; jk <=nlstate ; jk++){
4381: if(posproptt < 1.e-5){
1.231 brouard 4382: fprintf(ficresphtm,"<td>Nanq</td><td>%.0f</td><td>%.0f</td>",pospropt[jk],posproptt);
1.226 brouard 4383: }else{
1.231 brouard 4384: fprintf(ficresphtm,"<td>%.5f</td><td>%.0f</td><td>%.0f</td>",pospropt[jk]/posproptt,pospropt[jk],posproptt);
1.226 brouard 4385: }
4386: }
4387: fprintf(ficresphtm,"</tr>\n");
4388: fprintf(ficresphtm,"</table>\n");
4389: fprintf(ficresphtmfr,"</table>\n");
4390: if(posproptt < 1.e-5){
4391: fprintf(ficresphtm,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
4392: fprintf(ficresphtmfr,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
4393: fprintf(ficres,"\n This combination (%d) is not valid and no result will be produced\n\n",j1);
4394: invalidvarcomb[j1]=1;
4395: }else{
4396: fprintf(ficresphtm,"\n <p> This combination (%d) is valid and result will be produced.</p>",j1);
4397: invalidvarcomb[j1]=0;
4398: }
4399: fprintf(ficresphtmfr,"</table>\n");
4400: } /* end selected combination of covariate j1 */
4401: dateintmean=dateintsum/k2cpt;
1.231 brouard 4402:
1.226 brouard 4403: fclose(ficresp);
4404: fclose(ficresphtm);
4405: fclose(ficresphtmfr);
4406: free_vector(meanq,1,nqfveff);
4407: free_matrix(meanqt,1,lastpass,1,nqtveff);
4408: free_ma3x(freq,-5,nlstate+ndeath,-5,nlstate+ndeath, iagemin-AGEMARGE, iagemax+3+AGEMARGE);
4409: free_vector(pospropt,1,nlstate);
4410: free_vector(posprop,1,nlstate);
4411: free_matrix(prop,1,nlstate,iagemin-AGEMARGE, iagemax+3+AGEMARGE);
4412: free_vector(pp,1,nlstate);
4413: /* End of freqsummary */
4414: }
1.126 brouard 4415:
4416: /************ Prevalence ********************/
1.227 brouard 4417: 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)
4418: {
4419: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
4420: in each health status at the date of interview (if between dateprev1 and dateprev2).
4421: We still use firstpass and lastpass as another selection.
4422: */
1.126 brouard 4423:
1.227 brouard 4424: int i, m, jk, j1, bool, z1,j, iv;
4425: int mi; /* Effective wave */
4426: int iage;
4427: double agebegin, ageend;
4428:
4429: double **prop;
4430: double posprop;
4431: double y2; /* in fractional years */
4432: int iagemin, iagemax;
4433: int first; /** to stop verbosity which is redirected to log file */
4434:
4435: iagemin= (int) agemin;
4436: iagemax= (int) agemax;
4437: /*pp=vector(1,nlstate);*/
4438: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+3+AGEMARGE);
4439: /* freq=ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,iagemin,iagemax+3);*/
4440: j1=0;
1.222 brouard 4441:
1.227 brouard 4442: /*j=cptcoveff;*/
4443: if (cptcovn<1) {j=1;ncodemax[1]=1;}
1.222 brouard 4444:
1.227 brouard 4445: first=1;
4446: for(j1=1; j1<= (int) pow(2,cptcoveff);j1++){ /* For each combination of covariate */
4447: for (i=1; i<=nlstate; i++)
4448: for(iage=iagemin-AGEMARGE; iage <= iagemax+3+AGEMARGE; iage++)
4449: prop[i][iage]=0.0;
4450: printf("Prevalence combination of varying and fixed dummies %d\n",j1);
4451: /* fprintf(ficlog," V%d=%d ",Tvaraff[j1],nbcode[Tvaraff[j1]][codtabm(k,j1)]); */
4452: fprintf(ficlog,"Prevalence combination of varying and fixed dummies %d\n",j1);
4453:
4454: for (i=1; i<=imx; i++) { /* Each individual */
4455: bool=1;
4456: /* for(m=firstpass; m<=lastpass; m++){/\* Other selection (we can limit to certain interviews*\/ */
4457: for(mi=1; mi<wav[i];mi++){ /* For this wave too look where individual can be counted V4=0 V3=0 */
4458: m=mw[mi][i];
4459: /* Tmodelind[z1]=k is the position of the varying covariate in the model, but which # within 1 to ntv? */
4460: /* Tvar[Tmodelind[z1]] is the n of Vn; n-ncovcol-nqv is the first time varying covariate or iv */
4461: for (z1=1; z1<=cptcoveff; z1++){
4462: if( Fixed[Tmodelind[z1]]==1){
4463: iv= Tvar[Tmodelind[z1]]-ncovcol-nqv;
4464: if (cotvar[m][iv][i]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) /* iv=1 to ntv, right modality */
4465: bool=0;
4466: }else if( Fixed[Tmodelind[z1]]== 0) /* fixed */
4467: if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) {
4468: bool=0;
4469: }
4470: }
4471: if(bool==1){ /* Otherwise we skip that wave/person */
4472: agebegin=agev[m][i]; /* Age at beginning of wave before transition*/
4473: /* ageend=agev[m][i]+(dh[m][i])*stepm/YEARM; /\* Age at end of wave and transition *\/ */
4474: if(m >=firstpass && m <=lastpass){
4475: y2=anint[m][i]+(mint[m][i]/12.); /* Fractional date in year */
4476: if ((y2>=dateprev1) && (y2<=dateprev2)) { /* Here is the main selection (fractional years) */
4477: if(agev[m][i]==0) agev[m][i]=iagemax+1;
4478: if(agev[m][i]==1) agev[m][i]=iagemax+2;
4479: if((int)agev[m][i] <iagemin-AGEMARGE || (int)agev[m][i] >iagemax+3+AGEMARGE){
4480: printf("Error on individual # %d agev[m][i]=%f <%d-%d or > %d+3+%d m=%d; either change agemin or agemax or fix data\n",i, agev[m][i],iagemin,AGEMARGE, iagemax,AGEMARGE,m);
4481: exit(1);
4482: }
4483: if (s[m][i]>0 && s[m][i]<=nlstate) {
4484: /*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]]);*/
4485: prop[s[m][i]][(int)agev[m][i]] += weight[i];/* At age of beginning of transition, where status is known */
4486: prop[s[m][i]][iagemax+3] += weight[i];
4487: } /* end valid statuses */
4488: } /* end selection of dates */
4489: } /* end selection of waves */
4490: } /* end bool */
4491: } /* end wave */
4492: } /* end individual */
4493: for(i=iagemin; i <= iagemax+3; i++){
4494: for(jk=1,posprop=0; jk <=nlstate ; jk++) {
4495: posprop += prop[jk][i];
4496: }
4497:
4498: for(jk=1; jk <=nlstate ; jk++){
4499: if( i <= iagemax){
4500: if(posprop>=1.e-5){
4501: probs[i][jk][j1]= prop[jk][i]/posprop;
4502: } else{
4503: if(first==1){
4504: first=0;
4505: printf("Warning Observed prevalence probs[%d][%d][%d]=%lf because of lack of cases\nSee others in log file...\n",jk,i,j1,probs[i][jk][j1]);
4506: }
4507: }
4508: }
4509: }/* end jk */
4510: }/* end i */
1.222 brouard 4511: /*} *//* end i1 */
1.227 brouard 4512: } /* end j1 */
1.222 brouard 4513:
1.227 brouard 4514: /* free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath, iagemin, iagemax+3);*/
4515: /*free_vector(pp,1,nlstate);*/
4516: free_matrix(prop,1,nlstate, iagemin-AGEMARGE,iagemax+3+AGEMARGE);
4517: } /* End of prevalence */
1.126 brouard 4518:
4519: /************* Waves Concatenation ***************/
4520:
4521: 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)
4522: {
4523: /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
4524: Death is a valid wave (if date is known).
4525: mw[mi][i] is the mi (mi=1 to wav[i]) effective wave of individual i
4526: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
4527: and mw[mi+1][i]. dh depends on stepm.
1.227 brouard 4528: */
1.126 brouard 4529:
1.224 brouard 4530: int i=0, mi=0, m=0, mli=0;
1.126 brouard 4531: /* int j, k=0,jk, ju, jl,jmin=1e+5, jmax=-1;
4532: double sum=0., jmean=0.;*/
1.224 brouard 4533: int first=0, firstwo=0, firsthree=0, firstfour=0, firstfiv=0;
1.126 brouard 4534: int j, k=0,jk, ju, jl;
4535: double sum=0.;
4536: first=0;
1.214 brouard 4537: firstwo=0;
1.217 brouard 4538: firsthree=0;
1.218 brouard 4539: firstfour=0;
1.164 brouard 4540: jmin=100000;
1.126 brouard 4541: jmax=-1;
4542: jmean=0.;
1.224 brouard 4543:
4544: /* Treating live states */
1.214 brouard 4545: for(i=1; i<=imx; i++){ /* For simple cases and if state is death */
1.224 brouard 4546: mi=0; /* First valid wave */
1.227 brouard 4547: mli=0; /* Last valid wave */
1.126 brouard 4548: m=firstpass;
1.214 brouard 4549: while(s[m][i] <= nlstate){ /* a live state */
1.227 brouard 4550: if(m >firstpass && s[m][i]==s[m-1][i] && mint[m][i]==mint[m-1][i] && anint[m][i]==anint[m-1][i]){/* Two succesive identical information on wave m */
4551: mli=m-1;/* mw[++mi][i]=m-1; */
4552: }else if(s[m][i]>=1 || s[m][i]==-4 || s[m][i]==-5){ /* Since 0.98r4 if status=-2 vital status is really unknown, wave should be skipped */
4553: mw[++mi][i]=m;
4554: mli=m;
1.224 brouard 4555: } /* else might be a useless wave -1 and mi is not incremented and mw[mi] not updated */
4556: if(m < lastpass){ /* m < lastpass, standard case */
1.227 brouard 4557: m++; /* mi gives the "effective" current wave, m the current wave, go to next wave by incrementing m */
1.216 brouard 4558: }
1.227 brouard 4559: else{ /* m >= lastpass, eventual special issue with warning */
1.224 brouard 4560: #ifdef UNKNOWNSTATUSNOTCONTRIBUTING
1.227 brouard 4561: break;
1.224 brouard 4562: #else
1.227 brouard 4563: if(s[m][i]==-1 && (int) andc[i] == 9999 && (int)anint[m][i] != 9999){
4564: if(firsthree == 0){
4565: printf("Information! Unknown status for individual %ld line=%d occurred at last wave %d at known date %d/%d. Please, check if your unknown date of death %d/%d means a live state %d at wave %d. This case(%d)/wave(%d) contributes to the likelihood as pi. .\nOthers in log file only\n",num[i],i,lastpass,(int)mint[m][i],(int)anint[m][i], (int) moisdc[i], (int) andc[i], s[m][i], m, i, m);
4566: firsthree=1;
4567: }
4568: fprintf(ficlog,"Information! Unknown status for individual %ld line=%d occurred at last wave %d at known date %d/%d. Please, check if your unknown date of death %d/%d means a live state %d at wave %d. This case(%d)/wave(%d) contributes to the likelihood as pi. .\n",num[i],i,lastpass,(int)mint[m][i],(int)anint[m][i], (int) moisdc[i], (int) andc[i], s[m][i], m, i, m);
4569: mw[++mi][i]=m;
4570: mli=m;
4571: }
4572: if(s[m][i]==-2){ /* Vital status is really unknown */
4573: nbwarn++;
4574: if((int)anint[m][i] == 9999){ /* Has the vital status really been verified? */
4575: printf("Warning! Vital status for individual %ld (line=%d) at last wave %d interviewed at date %d/%d is unknown %d. Please, check if the vital status and the date of death %d/%d are really unknown. This case (%d)/wave (%d) is skipped, no contribution to likelihood.\nOthers in log file only\n",num[i],i,lastpass,(int)mint[m][i],(int)anint[m][i], s[m][i], (int) moisdc[i], (int) andc[i], i, m);
4576: fprintf(ficlog,"Warning! Vital status for individual %ld (line=%d) at last wave %d interviewed at date %d/%d is unknown %d. Please, check if the vital status and the date of death %d/%d are really unknown. This case (%d)/wave (%d) is skipped, no contribution to likelihood.\n",num[i],i,lastpass,(int)mint[m][i],(int)anint[m][i], s[m][i], (int) moisdc[i], (int) andc[i], i, m);
4577: }
4578: break;
4579: }
4580: break;
1.224 brouard 4581: #endif
1.227 brouard 4582: }/* End m >= lastpass */
1.126 brouard 4583: }/* end while */
1.224 brouard 4584:
1.227 brouard 4585: /* mi is the last effective wave, m is lastpass, mw[j][i] gives the # of j-th effective wave for individual i */
1.216 brouard 4586: /* After last pass */
1.224 brouard 4587: /* Treating death states */
1.214 brouard 4588: if (s[m][i] > nlstate){ /* In a death state */
1.227 brouard 4589: /* if( mint[m][i]==mdc[m][i] && anint[m][i]==andc[m][i]){ /\* same date of death and date of interview *\/ */
4590: /* } */
1.126 brouard 4591: mi++; /* Death is another wave */
4592: /* if(mi==0) never been interviewed correctly before death */
1.227 brouard 4593: /* Only death is a correct wave */
1.126 brouard 4594: mw[mi][i]=m;
1.224 brouard 4595: }
4596: #ifndef DISPATCHINGKNOWNDEATHAFTERLASTWAVE
1.227 brouard 4597: else if ((int) andc[i] != 9999) { /* Status is negative. A death occured after lastpass, we can't take it into account because of potential bias */
1.216 brouard 4598: /* m++; */
4599: /* mi++; */
4600: /* s[m][i]=nlstate+1; /\* We are setting the status to the last of non live state *\/ */
4601: /* mw[mi][i]=m; */
1.218 brouard 4602: if ((int)anint[m][i]!= 9999) { /* date of last interview is known */
1.227 brouard 4603: if((andc[i]+moisdc[i]/12.) <=(anint[m][i]+mint[m][i]/12.)){ /* death occured before last wave and status should have been death instead of -1 */
4604: nbwarn++;
4605: if(firstfiv==0){
4606: printf("Warning! Death for individual %ld line=%d occurred at %d/%d before last wave %d interviewed at %d/%d and should have been coded as death instead of '%d'. This case (%d)/wave (%d) is contributing to likelihood.\nOthers in log file only\n",num[i],i,(int) moisdc[i], (int) andc[i], lastpass,(int)mint[m][i],(int)anint[m][i], s[m][i], i,m );
4607: firstfiv=1;
4608: }else{
4609: fprintf(ficlog,"Warning! Death for individual %ld line=%d occurred at %d/%d before last wave %d interviewed at %d/%d and should have been coded as death instead of '%d'. This case (%d)/wave (%d) is contributing to likelihood.\n",num[i],i,(int) moisdc[i], (int) andc[i], lastpass,(int)mint[m][i],(int)anint[m][i], s[m][i], i,m );
4610: }
4611: }else{ /* Death occured afer last wave potential bias */
4612: nberr++;
4613: if(firstwo==0){
4614: printf("Error! Death for individual %ld line=%d occurred at %d/%d after last wave %d interviewed at %d/%d. Potential bias if other individuals are still alive at this date but ignored. This case (%d)/wave (%d) is skipped, no contribution to likelihood.\nOthers in log file only\n",num[i],i,(int) moisdc[i], (int) andc[i], lastpass,(int)mint[m][i],(int)anint[m][i], i,m );
4615: firstwo=1;
4616: }
4617: fprintf(ficlog,"Error! Death for individual %ld line=%d occurred at %d/%d after last wave %d interviewed at %d/%d. Potential bias if other individuals are still alive at this date but ignored. This case (%d)/wave (%d) is skipped, no contribution to likelihood.\n",num[i],i,(int) moisdc[i], (int) andc[i], lastpass,(int)mint[m][i],(int)anint[m][i], i,m );
4618: }
1.218 brouard 4619: }else{ /* end date of interview is known */
1.227 brouard 4620: /* death is known but not confirmed by death status at any wave */
4621: if(firstfour==0){
4622: printf("Error! Death for individual %ld line=%d occurred %d/%d but not confirmed by any death status for any wave, including last wave %d at unknown date %d/%d. Potential bias if other individuals are still alive at this date but ignored. This case (%d)/wave (%d) is skipped, no contribution to likelihood.\nOthers in log file only\n",num[i],i,(int) moisdc[i], (int) andc[i], lastpass,(int)mint[m][i],(int)anint[m][i], i,m );
4623: firstfour=1;
4624: }
4625: fprintf(ficlog,"Error! Death for individual %ld line=%d occurred %d/%d but not confirmed by any death status for any wave, including last wave %d at unknown date %d/%d. Potential bias if other individuals are still alive at this date but ignored. This case (%d)/wave (%d) is skipped, no contribution to likelihood.\n",num[i],i,(int) moisdc[i], (int) andc[i], lastpass,(int)mint[m][i],(int)anint[m][i], i,m );
1.214 brouard 4626: }
1.224 brouard 4627: } /* end if date of death is known */
4628: #endif
4629: wav[i]=mi; /* mi should be the last effective wave (or mli) */
4630: /* wav[i]=mw[mi][i]; */
1.126 brouard 4631: if(mi==0){
4632: nbwarn++;
4633: if(first==0){
1.227 brouard 4634: printf("Warning! No valid information for individual %ld line=%d (skipped) and may be others, see log file\n",num[i],i);
4635: first=1;
1.126 brouard 4636: }
4637: if(first==1){
1.227 brouard 4638: fprintf(ficlog,"Warning! No valid information for individual %ld line=%d (skipped)\n",num[i],i);
1.126 brouard 4639: }
4640: } /* end mi==0 */
4641: } /* End individuals */
1.214 brouard 4642: /* wav and mw are no more changed */
1.223 brouard 4643:
1.214 brouard 4644:
1.126 brouard 4645: for(i=1; i<=imx; i++){
4646: for(mi=1; mi<wav[i];mi++){
4647: if (stepm <=0)
1.227 brouard 4648: dh[mi][i]=1;
1.126 brouard 4649: else{
1.227 brouard 4650: if (s[mw[mi+1][i]][i] > nlstate) { /* A death */
4651: if (agedc[i] < 2*AGESUP) {
4652: j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12);
4653: if(j==0) j=1; /* Survives at least one month after exam */
4654: else if(j<0){
4655: nberr++;
4656: 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]);
4657: j=1; /* Temporary Dangerous patch */
4658: 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);
4659: 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]);
4660: 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);
4661: }
4662: k=k+1;
4663: if (j >= jmax){
4664: jmax=j;
4665: ijmax=i;
4666: }
4667: if (j <= jmin){
4668: jmin=j;
4669: ijmin=i;
4670: }
4671: sum=sum+j;
4672: /*if (j<0) printf("j=%d num=%d \n",j,i);*/
4673: /* printf("%d %d %d %d\n", s[mw[mi][i]][i] ,s[mw[mi+1][i]][i],j,i);*/
4674: }
4675: }
4676: else{
4677: j= rint( (agev[mw[mi+1][i]][i]*12 - agev[mw[mi][i]][i]*12));
1.126 brouard 4678: /* 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]); */
1.223 brouard 4679:
1.227 brouard 4680: k=k+1;
4681: if (j >= jmax) {
4682: jmax=j;
4683: ijmax=i;
4684: }
4685: else if (j <= jmin){
4686: jmin=j;
4687: ijmin=i;
4688: }
4689: /* if (j<10) printf("j=%d jmin=%d num=%d ",j,jmin,i); */
4690: /*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]);*/
4691: if(j<0){
4692: nberr++;
4693: 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]);
4694: 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]);
4695: }
4696: sum=sum+j;
4697: }
4698: jk= j/stepm;
4699: jl= j -jk*stepm;
4700: ju= j -(jk+1)*stepm;
4701: if(mle <=1){ /* only if we use a the linear-interpoloation pseudo-likelihood */
4702: if(jl==0){
4703: dh[mi][i]=jk;
4704: bh[mi][i]=0;
4705: }else{ /* We want a negative bias in order to only have interpolation ie
4706: * to avoid the price of an extra matrix product in likelihood */
4707: dh[mi][i]=jk+1;
4708: bh[mi][i]=ju;
4709: }
4710: }else{
4711: if(jl <= -ju){
4712: dh[mi][i]=jk;
4713: bh[mi][i]=jl; /* bias is positive if real duration
4714: * is higher than the multiple of stepm and negative otherwise.
4715: */
4716: }
4717: else{
4718: dh[mi][i]=jk+1;
4719: bh[mi][i]=ju;
4720: }
4721: if(dh[mi][i]==0){
4722: dh[mi][i]=1; /* At least one step */
4723: bh[mi][i]=ju; /* At least one step */
4724: /* 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);*/
4725: }
4726: } /* end if mle */
1.126 brouard 4727: }
4728: } /* end wave */
4729: }
4730: jmean=sum/k;
4731: 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 4732: 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.227 brouard 4733: }
1.126 brouard 4734:
4735: /*********** Tricode ****************************/
1.220 brouard 4736: void tricode(int *cptcov, int *Tvar, int **nbcode, int imx, int *Ndum)
1.126 brouard 4737: {
1.144 brouard 4738: /**< Uses cptcovn+2*cptcovprod as the number of covariates */
4739: /* Tvar[i]=atoi(stre); find 'n' in Vn and stores in Tvar. If model=V2+V1 Tvar[1]=2 and Tvar[2]=1
1.169 brouard 4740: * Boring subroutine which should only output nbcode[Tvar[j]][k]
1.224 brouard 4741: * Tvar[5] in V2+V1+V3*age+V2*V4 is 4 (V4) even it is a time varying or quantitative variable
4742: * nbcode[Tvar[5]][1]= nbcode[4][1]=0, nbcode[4][2]=1 (usually);
1.144 brouard 4743: */
1.130 brouard 4744:
1.145 brouard 4745: int ij=1, k=0, j=0, i=0, maxncov=NCOVMAX;
1.136 brouard 4746: int modmaxcovj=0; /* Modality max of covariates j */
1.145 brouard 4747: int cptcode=0; /* Modality max of covariates j */
4748: int modmincovj=0; /* Modality min of covariates j */
4749:
4750:
1.220 brouard 4751: /* cptcoveff=0; */
1.224 brouard 4752: /* *cptcov=0; */
1.126 brouard 4753:
1.144 brouard 4754: for (k=1; k <= maxncov; k++) ncodemax[k]=0; /* Horrible constant again replaced by NCOVMAX */
1.126 brouard 4755:
1.224 brouard 4756: /* Loop on covariates without age and products and no quantitative variable */
4757: /* for (j=1; j<=(cptcovs); j++) { /\* From model V1 + V2*age+ V3 + V3*V4 keeps V1 + V3 = 2 only *\/ */
1.227 brouard 4758: for (k=1; k<=cptcovt; k++) { /* From model V1 + V2*age + V3 + V3*V4 keeps V1 + V3 = 2 only */
4759: for (j=-1; (j < maxncov); j++) Ndum[j]=0;
4760: if(Dummy[k]==0 && Typevar[k] !=1){ /* Dummy covariate and not age product */
4761: switch(Fixed[k]) {
4762: case 0: /* Testing on fixed dummy covariate, simple or product of fixed */
1.231 brouard 4763: for (i=1; i<=imx; i++) { /* Loop on individuals: reads the data file to get the maximum value of the modality of this covariate Vj*/
4764: ij=(int)(covar[Tvar[k]][i]);
4765: /* ij=0 or 1 or -1. Value of the covariate Tvar[j] for individual i
4766: * If product of Vn*Vm, still boolean *:
4767: * If it was coded 1, 2, 3, 4 should be splitted into 3 boolean variables
4768: * 1 => 0 0 0, 2 => 0 0 1, 3 => 0 1 1, 4=1 0 0 */
4769: /* Finds for covariate j, n=Tvar[j] of Vn . ij is the
4770: modality of the nth covariate of individual i. */
4771: if (ij > modmaxcovj)
4772: modmaxcovj=ij;
4773: else if (ij < modmincovj)
4774: modmincovj=ij;
4775: if ((ij < -1) && (ij > NCOVMAX)){
4776: printf( "Error: minimal is less than -1 or maximal is bigger than %d. Exiting. \n", NCOVMAX );
4777: exit(1);
4778: }else
4779: Ndum[ij]++; /*counts and stores the occurence of this modality 0, 1, -1*/
4780: /* If coded 1, 2, 3 , counts the number of 1 Ndum[1], number of 2, Ndum[2], etc */
4781: /*printf("i=%d ij=%d Ndum[ij]=%d imx=%d",i,ij,Ndum[ij],imx);*/
4782: /* getting the maximum value of the modality of the covariate
4783: (should be 0 or 1 now) Tvar[j]. If V=sex and male is coded 0 and
4784: female ies 1, then modmaxcovj=1.
4785: */
4786: } /* end for loop on individuals i */
4787: printf(" Minimal and maximal values of %d th covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj);
4788: fprintf(ficlog," Minimal and maximal values of %d th covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj);
4789: cptcode=modmaxcovj;
4790: /* Ndum[0] = frequency of 0 for model-covariate j, Ndum[1] frequency of 1 etc. */
4791: /*for (i=0; i<=cptcode; i++) {*/
4792: for (j=modmincovj; j<=modmaxcovj; j++) { /* j=-1 ? 0 and 1*//* For each value j of the modality of model-cov k */
4793: printf("Frequencies of covariates %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]);
4794: fprintf(ficlog, "Frequencies of covariates %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]);
4795: if( Ndum[j] != 0 ){ /* Counts if nobody answered modality j ie empty modality, we skip it and reorder */
4796: if( j != -1){
4797: ncodemax[k]++; /* ncodemax[k]= Number of modalities of the k th
4798: covariate for which somebody answered excluding
4799: undefined. Usually 2: 0 and 1. */
4800: }
4801: ncodemaxwundef[k]++; /* ncodemax[j]= Number of modalities of the k th
4802: covariate for which somebody answered including
4803: undefined. Usually 3: -1, 0 and 1. */
4804: } /* In fact ncodemax[k]=2 (dichotom. variables only) but it could be more for
4805: * historical reasons: 3 if coded 1, 2, 3 and 4 and Ndum[2]=0 */
4806: } /* Ndum[-1] number of undefined modalities */
4807:
4808: /* j is a covariate, n=Tvar[j] of Vn; Fills nbcode */
4809: /* For covariate j, modalities could be 1, 2, 3, 4, 5, 6, 7. */
4810: /* If Ndum[1]=0, Ndum[2]=0, Ndum[3]= 635, Ndum[4]=0, Ndum[5]=0, Ndum[6]=27, Ndum[7]=125; */
4811: /* modmincovj=3; modmaxcovj = 7; */
4812: /* There are only 3 modalities non empty 3, 6, 7 (or 2 if 27 is too few) : ncodemax[j]=3; */
4813: /* which will be coded 0, 1, 2 which in binary on 2=3-1 digits are 0=00 1=01, 2=10; */
4814: /* defining two dummy variables: variables V1_1 and V1_2.*/
4815: /* nbcode[Tvar[j]][ij]=k; */
4816: /* nbcode[Tvar[j]][1]=0; */
4817: /* nbcode[Tvar[j]][2]=1; */
4818: /* nbcode[Tvar[j]][3]=2; */
4819: /* To be continued (not working yet). */
4820: ij=0; /* ij is similar to i but can jump over null modalities */
4821: for (i=modmincovj; i<=modmaxcovj; i++) { /* i= 1 to 2 for dichotomous, or from 1 to 3 or from -1 or 0 to 1 currently*/
4822: if (Ndum[i] == 0) { /* If nobody responded to this modality k */
4823: break;
4824: }
4825: ij++;
4826: nbcode[Tvar[k]][ij]=i; /* stores the original value of modality i in an array nbcode, ij modality from 1 to last non-nul modality. nbcode[1][1]=0 nbcode[1][2]=1*/
4827: cptcode = ij; /* New max modality for covar j */
4828: } /* end of loop on modality i=-1 to 1 or more */
4829: break;
1.227 brouard 4830: case 1: /* Testing on varying covariate, could be simple and
4831: * should look at waves or product of fixed *
4832: * varying. No time to test -1, assuming 0 and 1 only */
1.231 brouard 4833: ij=0;
4834: for(i=0; i<=1;i++){
4835: nbcode[Tvar[k]][++ij]=i;
4836: }
4837: break;
1.227 brouard 4838: default:
1.231 brouard 4839: break;
1.227 brouard 4840: } /* end switch */
4841: } /* end dummy test */
1.225 brouard 4842:
1.192 brouard 4843: /* for (k=0; k<= cptcode; k++) { /\* k=-1 ? k=0 to 1 *\//\* Could be 1 to 4 *\//\* cptcode=modmaxcovj *\/ */
4844: /* /\*recode from 0 *\/ */
4845: /* k is a modality. If we have model=V1+V1*sex */
4846: /* then: nbcode[1][1]=0 ; nbcode[1][2]=1; nbcode[2][1]=0 ; nbcode[2][2]=1; */
4847: /* But if some modality were not used, it is recoded from 0 to a newer modmaxcovj=cptcode *\/ */
4848: /* } */
4849: /* /\* cptcode = ij; *\/ /\* New max modality for covar j *\/ */
4850: /* if (ij > ncodemax[j]) { */
4851: /* printf( " Error ij=%d > ncodemax[%d]=%d\n", ij, j, ncodemax[j]); */
4852: /* fprintf(ficlog, " Error ij=%d > ncodemax[%d]=%d\n", ij, j, ncodemax[j]); */
4853: /* break; */
4854: /* } */
4855: /* } /\* end of loop on modality k *\/ */
1.137 brouard 4856: } /* end of loop on model-covariate j. nbcode[Tvarj][1]=0 and nbcode[Tvarj][2]=1 sets the value of covariate j*/
4857:
1.225 brouard 4858: for (k=-1; k< maxncov; k++) Ndum[k]=0;
1.227 brouard 4859: /* Look at fixed dummy (single or product) covariates to check empty modalities */
1.187 brouard 4860: for (i=1; i<=ncovmodel-2-nagesqr; i++) { /* -2, cste and age and eventually age*age */
1.225 brouard 4861: /* Listing of all covariables in statement model to see if some covariates appear twice. For example, V1 appears twice in V1+V1*V2.*/
1.227 brouard 4862: ij=Tvar[i]; /* Tvar 5,4,3,6,5,7,1,4 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V4*age */
4863: Ndum[ij]++; /* Count the # of 1, 2 etc: {1,1,1,2,2,1,1} because V1 once, V2 once, two V4 and V5 in above */
4864: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, {2, 1, 1, 1, 2, 1, 1, 0, 0} */
1.225 brouard 4865: } /* V4+V3+V5, Ndum[1]@5={0, 0, 1, 1, 1} */
4866:
4867: ij=0;
1.227 brouard 4868: /* for (i=0; i<= maxncov-1; i++) { /\* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) *\/ */
4869: for (k=1; k<= cptcovt; k++) { /* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) */
1.225 brouard 4870: /*printf("Ndum[%d]=%d\n",i, Ndum[i]);*/
1.227 brouard 4871: /* if((Ndum[i]!=0) && (i<=ncovcol)){ /\* Tvar[i] <= ncovmodel ? *\/ */
4872: if(Ndum[Tvar[k]]!=0 && Dummy[k] == 0 && Typevar[k]==0){ /* Only Dummy and non empty in the model */
4873: /* If product not in single variable we don't print results */
1.225 brouard 4874: /*printf("diff Ndum[%d]=%d\n",i, Ndum[i]);*/
1.230 brouard 4875: ++ij;/* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, */
4876: Tvaraff[ij]=Tvar[k]; /* For printing combination *//* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, Tvar {5, 4, 3, 6, 5, 2, 7, 1, 1} Tvaraff={4, 3, 1} V4, V3, V1*/
4877: Tmodelind[ij]=k; /* Tmodelind: index in model of dummies Tmodelind[1]=2 V4: pos=2; V3: pos=3, V1=9 {2, 3, 9, ?, ?,} */
1.231 brouard 4878: TmodelInvind[ij]=Tvar[k]- ncovcol-nqv; /* Inverse TmodelInvind[2=V4]=2 second dummy varying cov (V4)4-1-1 {0, 2, 1, } TmodelInvind[3]=1 */
1.227 brouard 4879: if(Fixed[k]!=0)
4880: anyvaryingduminmodel=1;
1.231 brouard 4881: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv)){ */
4882: /* Tvaraff[++ij]=-10; /\* Dont'n know how to treat quantitative variables yet *\/ */
4883: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv)){ */
4884: /* Tvaraff[++ij]=i; /\*For printing (unclear) *\/ */
4885: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv+nqtv)){ */
4886: /* Tvaraff[++ij]=-20; /\* Dont'n know how to treat quantitative variables yet *\/ */
1.227 brouard 4887: }
1.225 brouard 4888: } /* Tvaraff[1]@5 {3, 4, -20, 0, 0} Very strange */
4889: /* ij--; */
4890: /* cptcoveff=ij; /\*Number of total covariates*\/ */
4891: *cptcov=ij; /*Number of total real effective covariates: effective
1.231 brouard 4892: * because they can be excluded from the model and real
4893: * if in the model but excluded because missing values, but how to get k from ij?*/
1.227 brouard 4894: for(j=ij+1; j<= cptcovt; j++){
4895: Tvaraff[j]=0;
4896: Tmodelind[j]=0;
4897: }
1.228 brouard 4898: for(j=ntveff+1; j<= cptcovt; j++){
4899: TmodelInvind[j]=0;
4900: }
1.227 brouard 4901: /* To be sorted */
4902: ;
1.126 brouard 4903: }
4904:
1.145 brouard 4905:
1.126 brouard 4906: /*********** Health Expectancies ****************/
4907:
1.235 brouard 4908: void evsij(double ***eij, double x[], int nlstate, int stepm, int bage, int fage, double **oldm, double **savm, int cij, int estepm,char strstart[], int nres )
1.126 brouard 4909:
4910: {
4911: /* Health expectancies, no variances */
1.164 brouard 4912: int i, j, nhstepm, hstepm, h, nstepm;
1.126 brouard 4913: int nhstepma, nstepma; /* Decreasing with age */
4914: double age, agelim, hf;
4915: double ***p3mat;
4916: double eip;
4917:
4918: pstamp(ficreseij);
4919: fprintf(ficreseij,"# (a) Life expectancies by health status at initial age and (b) health expectancies by health status at initial age\n");
4920: fprintf(ficreseij,"# Age");
4921: for(i=1; i<=nlstate;i++){
4922: for(j=1; j<=nlstate;j++){
4923: fprintf(ficreseij," e%1d%1d ",i,j);
4924: }
4925: fprintf(ficreseij," e%1d. ",i);
4926: }
4927: fprintf(ficreseij,"\n");
4928:
4929:
4930: if(estepm < stepm){
4931: printf ("Problem %d lower than %d\n",estepm, stepm);
4932: }
4933: else hstepm=estepm;
4934: /* We compute the life expectancy from trapezoids spaced every estepm months
4935: * This is mainly to measure the difference between two models: for example
4936: * if stepm=24 months pijx are given only every 2 years and by summing them
4937: * we are calculating an estimate of the Life Expectancy assuming a linear
4938: * progression in between and thus overestimating or underestimating according
4939: * to the curvature of the survival function. If, for the same date, we
4940: * estimate the model with stepm=1 month, we can keep estepm to 24 months
4941: * to compare the new estimate of Life expectancy with the same linear
4942: * hypothesis. A more precise result, taking into account a more precise
4943: * curvature will be obtained if estepm is as small as stepm. */
4944:
4945: /* For example we decided to compute the life expectancy with the smallest unit */
4946: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
4947: nhstepm is the number of hstepm from age to agelim
4948: nstepm is the number of stepm from age to agelin.
4949: Look at hpijx to understand the reason of that which relies in memory size
4950: and note for a fixed period like estepm months */
4951: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
4952: survival function given by stepm (the optimization length). Unfortunately it
4953: means that if the survival funtion is printed only each two years of age and if
4954: you sum them up and add 1 year (area under the trapezoids) you won't get the same
4955: results. So we changed our mind and took the option of the best precision.
4956: */
4957: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
4958:
4959: agelim=AGESUP;
4960: /* If stepm=6 months */
4961: /* Computed by stepm unit matrices, product of hstepm matrices, stored
4962: in an array of nhstepm length: nhstepm=10, hstepm=4, stepm=6 months */
4963:
4964: /* nhstepm age range expressed in number of stepm */
4965: nstepm=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
4966: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
4967: /* if (stepm >= YEARM) hstepm=1;*/
4968: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
4969: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4970:
4971: for (age=bage; age<=fage; age ++){
4972: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
4973: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
4974: /* if (stepm >= YEARM) hstepm=1;*/
4975: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
4976:
4977: /* If stepm=6 months */
4978: /* Computed by stepm unit matrices, product of hstepma matrices, stored
4979: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
4980:
1.235 brouard 4981: hpxij(p3mat,nhstepma,age,hstepm,x,nlstate,stepm,oldm, savm, cij, nres);
1.126 brouard 4982:
4983: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
4984:
4985: printf("%d|",(int)age);fflush(stdout);
4986: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
4987:
4988: /* Computing expectancies */
4989: for(i=1; i<=nlstate;i++)
4990: for(j=1; j<=nlstate;j++)
4991: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
4992: eij[i][j][(int)age] += (p3mat[i][j][h]+p3mat[i][j][h+1])/2.0*hf;
4993:
4994: /* 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]);*/
4995:
4996: }
4997:
4998: fprintf(ficreseij,"%3.0f",age );
4999: for(i=1; i<=nlstate;i++){
5000: eip=0;
5001: for(j=1; j<=nlstate;j++){
5002: eip +=eij[i][j][(int)age];
5003: fprintf(ficreseij,"%9.4f", eij[i][j][(int)age] );
5004: }
5005: fprintf(ficreseij,"%9.4f", eip );
5006: }
5007: fprintf(ficreseij,"\n");
5008:
5009: }
5010: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5011: printf("\n");
5012: fprintf(ficlog,"\n");
5013:
5014: }
5015:
1.235 brouard 5016: 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[], int nres )
1.126 brouard 5017:
5018: {
5019: /* Covariances of health expectancies eij and of total life expectancies according
1.222 brouard 5020: to initial status i, ei. .
1.126 brouard 5021: */
5022: int i, j, nhstepm, hstepm, h, nstepm, k, cptj, cptj2, i2, j2, ij, ji;
5023: int nhstepma, nstepma; /* Decreasing with age */
5024: double age, agelim, hf;
5025: double ***p3matp, ***p3matm, ***varhe;
5026: double **dnewm,**doldm;
5027: double *xp, *xm;
5028: double **gp, **gm;
5029: double ***gradg, ***trgradg;
5030: int theta;
5031:
5032: double eip, vip;
5033:
5034: varhe=ma3x(1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int) fage);
5035: xp=vector(1,npar);
5036: xm=vector(1,npar);
5037: dnewm=matrix(1,nlstate*nlstate,1,npar);
5038: doldm=matrix(1,nlstate*nlstate,1,nlstate*nlstate);
5039:
5040: pstamp(ficresstdeij);
5041: fprintf(ficresstdeij,"# Health expectancies with standard errors\n");
5042: fprintf(ficresstdeij,"# Age");
5043: for(i=1; i<=nlstate;i++){
5044: for(j=1; j<=nlstate;j++)
5045: fprintf(ficresstdeij," e%1d%1d (SE)",i,j);
5046: fprintf(ficresstdeij," e%1d. ",i);
5047: }
5048: fprintf(ficresstdeij,"\n");
5049:
5050: pstamp(ficrescveij);
5051: fprintf(ficrescveij,"# Subdiagonal matrix of covariances of health expectancies by age: cov(eij,ekl)\n");
5052: fprintf(ficrescveij,"# Age");
5053: for(i=1; i<=nlstate;i++)
5054: for(j=1; j<=nlstate;j++){
5055: cptj= (j-1)*nlstate+i;
5056: for(i2=1; i2<=nlstate;i2++)
5057: for(j2=1; j2<=nlstate;j2++){
5058: cptj2= (j2-1)*nlstate+i2;
5059: if(cptj2 <= cptj)
5060: fprintf(ficrescveij," %1d%1d,%1d%1d",i,j,i2,j2);
5061: }
5062: }
5063: fprintf(ficrescveij,"\n");
5064:
5065: if(estepm < stepm){
5066: printf ("Problem %d lower than %d\n",estepm, stepm);
5067: }
5068: else hstepm=estepm;
5069: /* We compute the life expectancy from trapezoids spaced every estepm months
5070: * This is mainly to measure the difference between two models: for example
5071: * if stepm=24 months pijx are given only every 2 years and by summing them
5072: * we are calculating an estimate of the Life Expectancy assuming a linear
5073: * progression in between and thus overestimating or underestimating according
5074: * to the curvature of the survival function. If, for the same date, we
5075: * estimate the model with stepm=1 month, we can keep estepm to 24 months
5076: * to compare the new estimate of Life expectancy with the same linear
5077: * hypothesis. A more precise result, taking into account a more precise
5078: * curvature will be obtained if estepm is as small as stepm. */
5079:
5080: /* For example we decided to compute the life expectancy with the smallest unit */
5081: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
5082: nhstepm is the number of hstepm from age to agelim
5083: nstepm is the number of stepm from age to agelin.
5084: Look at hpijx to understand the reason of that which relies in memory size
5085: and note for a fixed period like estepm months */
5086: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
5087: survival function given by stepm (the optimization length). Unfortunately it
5088: means that if the survival funtion is printed only each two years of age and if
5089: you sum them up and add 1 year (area under the trapezoids) you won't get the same
5090: results. So we changed our mind and took the option of the best precision.
5091: */
5092: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
5093:
5094: /* If stepm=6 months */
5095: /* nhstepm age range expressed in number of stepm */
5096: agelim=AGESUP;
5097: nstepm=(int) rint((agelim-bage)*YEARM/stepm);
5098: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
5099: /* if (stepm >= YEARM) hstepm=1;*/
5100: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
5101:
5102: p3matp=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5103: p3matm=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5104: gradg=ma3x(0,nhstepm,1,npar,1,nlstate*nlstate);
5105: trgradg =ma3x(0,nhstepm,1,nlstate*nlstate,1,npar);
5106: gp=matrix(0,nhstepm,1,nlstate*nlstate);
5107: gm=matrix(0,nhstepm,1,nlstate*nlstate);
5108:
5109: for (age=bage; age<=fage; age ++){
5110: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
5111: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
5112: /* if (stepm >= YEARM) hstepm=1;*/
5113: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
1.218 brouard 5114:
1.126 brouard 5115: /* If stepm=6 months */
5116: /* Computed by stepm unit matrices, product of hstepma matrices, stored
5117: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
5118:
5119: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
1.218 brouard 5120:
1.126 brouard 5121: /* Computing Variances of health expectancies */
5122: /* Gradient is computed with plus gp and minus gm. Code is duplicated in order to
5123: decrease memory allocation */
5124: for(theta=1; theta <=npar; theta++){
5125: for(i=1; i<=npar; i++){
1.222 brouard 5126: xp[i] = x[i] + (i==theta ?delti[theta]:0);
5127: xm[i] = x[i] - (i==theta ?delti[theta]:0);
1.126 brouard 5128: }
1.235 brouard 5129: hpxij(p3matp,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, cij, nres);
5130: hpxij(p3matm,nhstepm,age,hstepm,xm,nlstate,stepm,oldm,savm, cij, nres);
1.218 brouard 5131:
1.126 brouard 5132: for(j=1; j<= nlstate; j++){
1.222 brouard 5133: for(i=1; i<=nlstate; i++){
5134: for(h=0; h<=nhstepm-1; h++){
5135: gp[h][(j-1)*nlstate + i] = (p3matp[i][j][h]+p3matp[i][j][h+1])/2.;
5136: gm[h][(j-1)*nlstate + i] = (p3matm[i][j][h]+p3matm[i][j][h+1])/2.;
5137: }
5138: }
1.126 brouard 5139: }
1.218 brouard 5140:
1.126 brouard 5141: for(ij=1; ij<= nlstate*nlstate; ij++)
1.222 brouard 5142: for(h=0; h<=nhstepm-1; h++){
5143: gradg[h][theta][ij]= (gp[h][ij]-gm[h][ij])/2./delti[theta];
5144: }
1.126 brouard 5145: }/* End theta */
5146:
5147:
5148: for(h=0; h<=nhstepm-1; h++)
5149: for(j=1; j<=nlstate*nlstate;j++)
1.222 brouard 5150: for(theta=1; theta <=npar; theta++)
5151: trgradg[h][j][theta]=gradg[h][theta][j];
1.126 brouard 5152:
1.218 brouard 5153:
1.222 brouard 5154: for(ij=1;ij<=nlstate*nlstate;ij++)
1.126 brouard 5155: for(ji=1;ji<=nlstate*nlstate;ji++)
1.222 brouard 5156: varhe[ij][ji][(int)age] =0.;
1.218 brouard 5157:
1.222 brouard 5158: printf("%d|",(int)age);fflush(stdout);
5159: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
5160: for(h=0;h<=nhstepm-1;h++){
1.126 brouard 5161: for(k=0;k<=nhstepm-1;k++){
1.222 brouard 5162: matprod2(dnewm,trgradg[h],1,nlstate*nlstate,1,npar,1,npar,matcov);
5163: matprod2(doldm,dnewm,1,nlstate*nlstate,1,npar,1,nlstate*nlstate,gradg[k]);
5164: for(ij=1;ij<=nlstate*nlstate;ij++)
5165: for(ji=1;ji<=nlstate*nlstate;ji++)
5166: varhe[ij][ji][(int)age] += doldm[ij][ji]*hf*hf;
1.126 brouard 5167: }
5168: }
1.218 brouard 5169:
1.126 brouard 5170: /* Computing expectancies */
1.235 brouard 5171: hpxij(p3matm,nhstepm,age,hstepm,x,nlstate,stepm,oldm, savm, cij,nres);
1.126 brouard 5172: for(i=1; i<=nlstate;i++)
5173: for(j=1; j<=nlstate;j++)
1.222 brouard 5174: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
5175: eij[i][j][(int)age] += (p3matm[i][j][h]+p3matm[i][j][h+1])/2.0*hf;
1.218 brouard 5176:
1.222 brouard 5177: /* 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]);*/
1.218 brouard 5178:
1.222 brouard 5179: }
1.218 brouard 5180:
1.126 brouard 5181: fprintf(ficresstdeij,"%3.0f",age );
5182: for(i=1; i<=nlstate;i++){
5183: eip=0.;
5184: vip=0.;
5185: for(j=1; j<=nlstate;j++){
1.222 brouard 5186: eip += eij[i][j][(int)age];
5187: for(k=1; k<=nlstate;k++) /* Sum on j and k of cov(eij,eik) */
5188: vip += varhe[(j-1)*nlstate+i][(k-1)*nlstate+i][(int)age];
5189: fprintf(ficresstdeij," %9.4f (%.4f)", eij[i][j][(int)age], sqrt(varhe[(j-1)*nlstate+i][(j-1)*nlstate+i][(int)age]) );
1.126 brouard 5190: }
5191: fprintf(ficresstdeij," %9.4f (%.4f)", eip, sqrt(vip));
5192: }
5193: fprintf(ficresstdeij,"\n");
1.218 brouard 5194:
1.126 brouard 5195: fprintf(ficrescveij,"%3.0f",age );
5196: for(i=1; i<=nlstate;i++)
5197: for(j=1; j<=nlstate;j++){
1.222 brouard 5198: cptj= (j-1)*nlstate+i;
5199: for(i2=1; i2<=nlstate;i2++)
5200: for(j2=1; j2<=nlstate;j2++){
5201: cptj2= (j2-1)*nlstate+i2;
5202: if(cptj2 <= cptj)
5203: fprintf(ficrescveij," %.4f", varhe[cptj][cptj2][(int)age]);
5204: }
1.126 brouard 5205: }
5206: fprintf(ficrescveij,"\n");
1.218 brouard 5207:
1.126 brouard 5208: }
5209: free_matrix(gm,0,nhstepm,1,nlstate*nlstate);
5210: free_matrix(gp,0,nhstepm,1,nlstate*nlstate);
5211: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate*nlstate);
5212: free_ma3x(trgradg,0,nhstepm,1,nlstate*nlstate,1,npar);
5213: free_ma3x(p3matm,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5214: free_ma3x(p3matp,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5215: printf("\n");
5216: fprintf(ficlog,"\n");
1.218 brouard 5217:
1.126 brouard 5218: free_vector(xm,1,npar);
5219: free_vector(xp,1,npar);
5220: free_matrix(dnewm,1,nlstate*nlstate,1,npar);
5221: free_matrix(doldm,1,nlstate*nlstate,1,nlstate*nlstate);
5222: free_ma3x(varhe,1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int)fage);
5223: }
1.218 brouard 5224:
1.126 brouard 5225: /************ Variance ******************/
1.235 brouard 5226: 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 *ncvyearp, int ij, int estepm, int cptcov, int cptcod, int popbased, int mobilav, char strstart[], int nres)
1.218 brouard 5227: {
5228: /* Variance of health expectancies */
5229: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);*/
5230: /* double **newm;*/
5231: /* int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav)*/
5232:
5233: /* int movingaverage(); */
5234: double **dnewm,**doldm;
5235: double **dnewmp,**doldmp;
5236: int i, j, nhstepm, hstepm, h, nstepm ;
5237: int k;
5238: double *xp;
5239: double **gp, **gm; /* for var eij */
5240: double ***gradg, ***trgradg; /*for var eij */
5241: double **gradgp, **trgradgp; /* for var p point j */
5242: double *gpp, *gmp; /* for var p point j */
5243: double **varppt; /* for var p point j nlstate to nlstate+ndeath */
5244: double ***p3mat;
5245: double age,agelim, hf;
5246: /* double ***mobaverage; */
5247: int theta;
5248: char digit[4];
5249: char digitp[25];
5250:
5251: char fileresprobmorprev[FILENAMELENGTH];
5252:
5253: if(popbased==1){
5254: if(mobilav!=0)
5255: strcpy(digitp,"-POPULBASED-MOBILAV_");
5256: else strcpy(digitp,"-POPULBASED-NOMOBIL_");
5257: }
5258: else
5259: strcpy(digitp,"-STABLBASED_");
1.126 brouard 5260:
1.218 brouard 5261: /* if (mobilav!=0) { */
5262: /* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
5263: /* if (movingaverage(probs, bage, fage, mobaverage,mobilav)!=0){ */
5264: /* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); */
5265: /* printf(" Error in movingaverage mobilav=%d\n",mobilav); */
5266: /* } */
5267: /* } */
5268:
5269: strcpy(fileresprobmorprev,"PRMORPREV-");
5270: sprintf(digit,"%-d",ij);
5271: /*printf("DIGIT=%s, ij=%d ijr=%-d|\n",digit, ij,ij);*/
5272: strcat(fileresprobmorprev,digit); /* Tvar to be done */
5273: strcat(fileresprobmorprev,digitp); /* Popbased or not, mobilav or not */
5274: strcat(fileresprobmorprev,fileresu);
5275: if((ficresprobmorprev=fopen(fileresprobmorprev,"w"))==NULL) {
5276: printf("Problem with resultfile: %s\n", fileresprobmorprev);
5277: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobmorprev);
5278: }
5279: printf("Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
5280: fprintf(ficlog,"Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
5281: pstamp(ficresprobmorprev);
5282: 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);
5283: fprintf(ficresprobmorprev,"# Age cov=%-d",ij);
5284: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
5285: fprintf(ficresprobmorprev," p.%-d SE",j);
5286: for(i=1; i<=nlstate;i++)
5287: fprintf(ficresprobmorprev," w%1d p%-d%-d",i,i,j);
5288: }
5289: fprintf(ficresprobmorprev,"\n");
5290:
5291: fprintf(ficgp,"\n# Routine varevsij");
5292: fprintf(ficgp,"\nunset title \n");
5293: /* fprintf(fichtm, "#Local time at start: %s", strstart);*/
5294: 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");
5295: fprintf(fichtm,"\n<br>%s <br>\n",digitp);
5296: /* } */
5297: varppt = matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
5298: pstamp(ficresvij);
5299: fprintf(ficresvij,"# Variance and covariance of health expectancies e.j \n# (weighted average of eij where weights are ");
5300: if(popbased==1)
5301: 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);
5302: else
5303: fprintf(ficresvij,"the age specific period (stable) prevalences in each health state \n");
5304: fprintf(ficresvij,"# Age");
5305: for(i=1; i<=nlstate;i++)
5306: for(j=1; j<=nlstate;j++)
5307: fprintf(ficresvij," Cov(e.%1d, e.%1d)",i,j);
5308: fprintf(ficresvij,"\n");
5309:
5310: xp=vector(1,npar);
5311: dnewm=matrix(1,nlstate,1,npar);
5312: doldm=matrix(1,nlstate,1,nlstate);
5313: dnewmp= matrix(nlstate+1,nlstate+ndeath,1,npar);
5314: doldmp= matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
5315:
5316: gradgp=matrix(1,npar,nlstate+1,nlstate+ndeath);
5317: gpp=vector(nlstate+1,nlstate+ndeath);
5318: gmp=vector(nlstate+1,nlstate+ndeath);
5319: trgradgp =matrix(nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
1.126 brouard 5320:
1.218 brouard 5321: if(estepm < stepm){
5322: printf ("Problem %d lower than %d\n",estepm, stepm);
5323: }
5324: else hstepm=estepm;
5325: /* For example we decided to compute the life expectancy with the smallest unit */
5326: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
5327: nhstepm is the number of hstepm from age to agelim
5328: nstepm is the number of stepm from age to agelim.
5329: Look at function hpijx to understand why because of memory size limitations,
5330: we decided (b) to get a life expectancy respecting the most precise curvature of the
5331: survival function given by stepm (the optimization length). Unfortunately it
5332: means that if the survival funtion is printed every two years of age and if
5333: you sum them up and add 1 year (area under the trapezoids) you won't get the same
5334: results. So we changed our mind and took the option of the best precision.
5335: */
5336: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
5337: agelim = AGESUP;
5338: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
5339: nstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
5340: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
5341: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5342: gradg=ma3x(0,nhstepm,1,npar,1,nlstate);
5343: gp=matrix(0,nhstepm,1,nlstate);
5344: gm=matrix(0,nhstepm,1,nlstate);
5345:
5346:
5347: for(theta=1; theta <=npar; theta++){
5348: for(i=1; i<=npar; i++){ /* Computes gradient x + delta*/
5349: xp[i] = x[i] + (i==theta ?delti[theta]:0);
5350: }
5351:
1.235 brouard 5352: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij, nresult);
1.218 brouard 5353:
5354: if (popbased==1) {
5355: if(mobilav ==0){
5356: for(i=1; i<=nlstate;i++)
5357: prlim[i][i]=probs[(int)age][i][ij];
5358: }else{ /* mobilav */
5359: for(i=1; i<=nlstate;i++)
5360: prlim[i][i]=mobaverage[(int)age][i][ij];
5361: }
5362: }
5363:
1.235 brouard 5364: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij,nres); /* Returns p3mat[i][j][h] for h=1 to nhstepm */
1.218 brouard 5365: for(j=1; j<= nlstate; j++){
5366: for(h=0; h<=nhstepm; h++){
5367: for(i=1, gp[h][j]=0.;i<=nlstate;i++)
5368: gp[h][j] += prlim[i][i]*p3mat[i][j][h];
5369: }
5370: }
5371: /* Next for computing probability of death (h=1 means
5372: computed over hstepm matrices product = hstepm*stepm months)
5373: as a weighted average of prlim.
5374: */
5375: for(j=nlstate+1;j<=nlstate+ndeath;j++){
5376: for(i=1,gpp[j]=0.; i<= nlstate; i++)
5377: gpp[j] += prlim[i][i]*p3mat[i][j][1];
5378: }
5379: /* end probability of death */
5380:
5381: for(i=1; i<=npar; i++) /* Computes gradient x - delta */
5382: xp[i] = x[i] - (i==theta ?delti[theta]:0);
5383:
1.235 brouard 5384: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp, ij, nresult);
1.218 brouard 5385:
5386: if (popbased==1) {
5387: if(mobilav ==0){
5388: for(i=1; i<=nlstate;i++)
5389: prlim[i][i]=probs[(int)age][i][ij];
5390: }else{ /* mobilav */
5391: for(i=1; i<=nlstate;i++)
5392: prlim[i][i]=mobaverage[(int)age][i][ij];
5393: }
5394: }
5395:
1.235 brouard 5396: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij,nres);
1.218 brouard 5397:
5398: for(j=1; j<= nlstate; j++){ /* Sum of wi * eij = e.j */
5399: for(h=0; h<=nhstepm; h++){
5400: for(i=1, gm[h][j]=0.;i<=nlstate;i++)
5401: gm[h][j] += prlim[i][i]*p3mat[i][j][h];
5402: }
5403: }
5404: /* This for computing probability of death (h=1 means
5405: computed over hstepm matrices product = hstepm*stepm months)
5406: as a weighted average of prlim.
5407: */
5408: for(j=nlstate+1;j<=nlstate+ndeath;j++){
5409: for(i=1,gmp[j]=0.; i<= nlstate; i++)
5410: gmp[j] += prlim[i][i]*p3mat[i][j][1];
5411: }
5412: /* end probability of death */
5413:
5414: for(j=1; j<= nlstate; j++) /* vareij */
5415: for(h=0; h<=nhstepm; h++){
5416: gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta];
5417: }
5418:
5419: for(j=nlstate+1; j<= nlstate+ndeath; j++){ /* var mu */
5420: gradgp[theta][j]= (gpp[j]-gmp[j])/2./delti[theta];
5421: }
5422:
5423: } /* End theta */
5424:
5425: trgradg =ma3x(0,nhstepm,1,nlstate,1,npar); /* veij */
5426:
5427: for(h=0; h<=nhstepm; h++) /* veij */
5428: for(j=1; j<=nlstate;j++)
5429: for(theta=1; theta <=npar; theta++)
5430: trgradg[h][j][theta]=gradg[h][theta][j];
5431:
5432: for(j=nlstate+1; j<=nlstate+ndeath;j++) /* mu */
5433: for(theta=1; theta <=npar; theta++)
5434: trgradgp[j][theta]=gradgp[theta][j];
5435:
5436:
5437: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
5438: for(i=1;i<=nlstate;i++)
5439: for(j=1;j<=nlstate;j++)
5440: vareij[i][j][(int)age] =0.;
5441:
5442: for(h=0;h<=nhstepm;h++){
5443: for(k=0;k<=nhstepm;k++){
5444: matprod2(dnewm,trgradg[h],1,nlstate,1,npar,1,npar,matcov);
5445: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg[k]);
5446: for(i=1;i<=nlstate;i++)
5447: for(j=1;j<=nlstate;j++)
5448: vareij[i][j][(int)age] += doldm[i][j]*hf*hf;
5449: }
5450: }
5451:
5452: /* pptj */
5453: matprod2(dnewmp,trgradgp,nlstate+1,nlstate+ndeath,1,npar,1,npar,matcov);
5454: matprod2(doldmp,dnewmp,nlstate+1,nlstate+ndeath,1,npar,nlstate+1,nlstate+ndeath,gradgp);
5455: for(j=nlstate+1;j<=nlstate+ndeath;j++)
5456: for(i=nlstate+1;i<=nlstate+ndeath;i++)
5457: varppt[j][i]=doldmp[j][i];
5458: /* end ppptj */
5459: /* x centered again */
5460:
1.235 brouard 5461: prevalim(prlim,nlstate,x,age,oldm,savm,ftolpl,ncvyearp,ij, nresult);
1.218 brouard 5462:
5463: if (popbased==1) {
5464: if(mobilav ==0){
5465: for(i=1; i<=nlstate;i++)
5466: prlim[i][i]=probs[(int)age][i][ij];
5467: }else{ /* mobilav */
5468: for(i=1; i<=nlstate;i++)
5469: prlim[i][i]=mobaverage[(int)age][i][ij];
5470: }
5471: }
5472:
5473: /* This for computing probability of death (h=1 means
5474: computed over hstepm (estepm) matrices product = hstepm*stepm months)
5475: as a weighted average of prlim.
5476: */
1.235 brouard 5477: hpxij(p3mat,nhstepm,age,hstepm,x,nlstate,stepm,oldm,savm, ij, nres);
1.218 brouard 5478: for(j=nlstate+1;j<=nlstate+ndeath;j++){
5479: for(i=1,gmp[j]=0.;i<= nlstate; i++)
5480: gmp[j] += prlim[i][i]*p3mat[i][j][1];
5481: }
5482: /* end probability of death */
5483:
5484: fprintf(ficresprobmorprev,"%3d %d ",(int) age, ij);
5485: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
5486: fprintf(ficresprobmorprev," %11.3e %11.3e",gmp[j], sqrt(varppt[j][j]));
5487: for(i=1; i<=nlstate;i++){
5488: fprintf(ficresprobmorprev," %11.3e %11.3e ",prlim[i][i],p3mat[i][j][1]);
5489: }
5490: }
5491: fprintf(ficresprobmorprev,"\n");
5492:
5493: fprintf(ficresvij,"%.0f ",age );
5494: for(i=1; i<=nlstate;i++)
5495: for(j=1; j<=nlstate;j++){
5496: fprintf(ficresvij," %.4f", vareij[i][j][(int)age]);
5497: }
5498: fprintf(ficresvij,"\n");
5499: free_matrix(gp,0,nhstepm,1,nlstate);
5500: free_matrix(gm,0,nhstepm,1,nlstate);
5501: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate);
5502: free_ma3x(trgradg,0,nhstepm,1,nlstate,1,npar);
5503: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5504: } /* End age */
5505: free_vector(gpp,nlstate+1,nlstate+ndeath);
5506: free_vector(gmp,nlstate+1,nlstate+ndeath);
5507: free_matrix(gradgp,1,npar,nlstate+1,nlstate+ndeath);
5508: free_matrix(trgradgp,nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
5509: /* fprintf(ficgp,"\nunset parametric;unset label; set ter png small size 320, 240"); */
5510: fprintf(ficgp,"\nunset parametric;unset label; set ter svg size 640, 480");
5511: /* for(j=nlstate+1; j<= nlstate+ndeath; j++){ *//* Only the first actually */
5512: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Force of mortality (year-1)\";");
5513: fprintf(ficgp,"\nset out \"%s%s.svg\";",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
5514: /* fprintf(ficgp,"\n plot \"%s\" u 1:($3*%6.3f) not w l 1 ",fileresprobmorprev,YEARM/estepm); */
5515: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)*%6.3f) t \"95\%% interval\" w l 2 ",fileresprobmorprev,YEARM/estepm); */
5516: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)*%6.3f) not w l 2 ",fileresprobmorprev,YEARM/estepm); */
5517: fprintf(ficgp,"\n plot \"%s\" u 1:($3) not w l lt 1 ",subdirf(fileresprobmorprev));
5518: fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)) t \"95%% interval\" w l lt 2 ",subdirf(fileresprobmorprev));
5519: fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)) not w l lt 2 ",subdirf(fileresprobmorprev));
5520: fprintf(fichtm,"\n<br> File (multiple files are possible if covariates are present): <A href=\"%s\">%s</a>\n",subdirf(fileresprobmorprev),subdirf(fileresprobmorprev));
5521: fprintf(fichtm,"\n<br> Probability is computed over estepm=%d months. <br> <img src=\"%s%s.svg\"> <br>\n", estepm,subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
5522: /* 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.svg\"> <br>\n", stepm,YEARM,digitp,digit);
1.126 brouard 5523: */
1.218 brouard 5524: /* fprintf(ficgp,"\nset out \"varmuptjgr%s%s%s.svg\";replot;",digitp,optionfilefiname,digit); */
5525: fprintf(ficgp,"\nset out;\nset out \"%s%s.svg\";replot;set out;\n",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
1.126 brouard 5526:
1.218 brouard 5527: free_vector(xp,1,npar);
5528: free_matrix(doldm,1,nlstate,1,nlstate);
5529: free_matrix(dnewm,1,nlstate,1,npar);
5530: free_matrix(doldmp,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
5531: free_matrix(dnewmp,nlstate+1,nlstate+ndeath,1,npar);
5532: free_matrix(varppt,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
5533: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
5534: fclose(ficresprobmorprev);
5535: fflush(ficgp);
5536: fflush(fichtm);
5537: } /* end varevsij */
1.126 brouard 5538:
5539: /************ Variance of prevlim ******************/
1.235 brouard 5540: 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 *ncvyearp, int ij, char strstart[], int nres)
1.126 brouard 5541: {
1.205 brouard 5542: /* Variance of prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/
1.126 brouard 5543: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
1.164 brouard 5544:
1.126 brouard 5545: double **dnewm,**doldm;
5546: int i, j, nhstepm, hstepm;
5547: double *xp;
5548: double *gp, *gm;
5549: double **gradg, **trgradg;
1.208 brouard 5550: double **mgm, **mgp;
1.126 brouard 5551: double age,agelim;
5552: int theta;
5553:
5554: pstamp(ficresvpl);
5555: fprintf(ficresvpl,"# Standard deviation of period (stable) prevalences \n");
5556: fprintf(ficresvpl,"# Age");
5557: for(i=1; i<=nlstate;i++)
5558: fprintf(ficresvpl," %1d-%1d",i,i);
5559: fprintf(ficresvpl,"\n");
5560:
5561: xp=vector(1,npar);
5562: dnewm=matrix(1,nlstate,1,npar);
5563: doldm=matrix(1,nlstate,1,nlstate);
5564:
5565: hstepm=1*YEARM; /* Every year of age */
5566: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
5567: agelim = AGESUP;
5568: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
5569: nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
5570: if (stepm >= YEARM) hstepm=1;
5571: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
5572: gradg=matrix(1,npar,1,nlstate);
1.208 brouard 5573: mgp=matrix(1,npar,1,nlstate);
5574: mgm=matrix(1,npar,1,nlstate);
1.126 brouard 5575: gp=vector(1,nlstate);
5576: gm=vector(1,nlstate);
5577:
5578: for(theta=1; theta <=npar; theta++){
5579: for(i=1; i<=npar; i++){ /* Computes gradient */
5580: xp[i] = x[i] + (i==theta ?delti[theta]:0);
5581: }
1.209 brouard 5582: if((int)age==79 ||(int)age== 80 ||(int)age== 81 )
1.235 brouard 5583: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
1.209 brouard 5584: else
1.235 brouard 5585: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
1.208 brouard 5586: for(i=1;i<=nlstate;i++){
1.126 brouard 5587: gp[i] = prlim[i][i];
1.208 brouard 5588: mgp[theta][i] = prlim[i][i];
5589: }
1.126 brouard 5590: for(i=1; i<=npar; i++) /* Computes gradient */
5591: xp[i] = x[i] - (i==theta ?delti[theta]:0);
1.209 brouard 5592: if((int)age==79 ||(int)age== 80 ||(int)age== 81 )
1.235 brouard 5593: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
1.209 brouard 5594: else
1.235 brouard 5595: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
1.208 brouard 5596: for(i=1;i<=nlstate;i++){
1.126 brouard 5597: gm[i] = prlim[i][i];
1.208 brouard 5598: mgm[theta][i] = prlim[i][i];
5599: }
1.126 brouard 5600: for(i=1;i<=nlstate;i++)
5601: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
1.209 brouard 5602: /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */
1.126 brouard 5603: } /* End theta */
5604:
5605: trgradg =matrix(1,nlstate,1,npar);
5606:
5607: for(j=1; j<=nlstate;j++)
5608: for(theta=1; theta <=npar; theta++)
5609: trgradg[j][theta]=gradg[theta][j];
1.209 brouard 5610: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
5611: /* printf("\nmgm mgp %d ",(int)age); */
5612: /* for(j=1; j<=nlstate;j++){ */
5613: /* printf(" %d ",j); */
5614: /* for(theta=1; theta <=npar; theta++) */
5615: /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */
5616: /* printf("\n "); */
5617: /* } */
5618: /* } */
5619: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
5620: /* printf("\n gradg %d ",(int)age); */
5621: /* for(j=1; j<=nlstate;j++){ */
5622: /* printf("%d ",j); */
5623: /* for(theta=1; theta <=npar; theta++) */
5624: /* printf("%d %lf ",theta,gradg[theta][j]); */
5625: /* printf("\n "); */
5626: /* } */
5627: /* } */
1.126 brouard 5628:
5629: for(i=1;i<=nlstate;i++)
5630: varpl[i][(int)age] =0.;
1.209 brouard 5631: if((int)age==79 ||(int)age== 80 ||(int)age== 81){
1.205 brouard 5632: matprod2(dnewm,trgradg,1,nlstate,1,npar,1,npar,matcov);
5633: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg);
5634: }else{
1.126 brouard 5635: matprod2(dnewm,trgradg,1,nlstate,1,npar,1,npar,matcov);
5636: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg);
1.205 brouard 5637: }
1.126 brouard 5638: for(i=1;i<=nlstate;i++)
5639: varpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
5640:
5641: fprintf(ficresvpl,"%.0f ",age );
5642: for(i=1; i<=nlstate;i++)
5643: fprintf(ficresvpl," %.5f (%.5f)",prlim[i][i],sqrt(varpl[i][(int)age]));
5644: fprintf(ficresvpl,"\n");
5645: free_vector(gp,1,nlstate);
5646: free_vector(gm,1,nlstate);
1.208 brouard 5647: free_matrix(mgm,1,npar,1,nlstate);
5648: free_matrix(mgp,1,npar,1,nlstate);
1.126 brouard 5649: free_matrix(gradg,1,npar,1,nlstate);
5650: free_matrix(trgradg,1,nlstate,1,npar);
5651: } /* End age */
5652:
5653: free_vector(xp,1,npar);
5654: free_matrix(doldm,1,nlstate,1,npar);
5655: free_matrix(dnewm,1,nlstate,1,nlstate);
5656:
5657: }
5658:
5659: /************ Variance of one-step probabilities ******************/
5660: 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[])
1.222 brouard 5661: {
5662: int i, j=0, k1, l1, tj;
5663: int k2, l2, j1, z1;
5664: int k=0, l;
5665: int first=1, first1, first2;
5666: double cv12, mu1, mu2, lc1, lc2, v12, v21, v11, v22,v1,v2, c12, tnalp;
5667: double **dnewm,**doldm;
5668: double *xp;
5669: double *gp, *gm;
5670: double **gradg, **trgradg;
5671: double **mu;
5672: double age, cov[NCOVMAX+1];
5673: double std=2.0; /* Number of standard deviation wide of confidence ellipsoids */
5674: int theta;
5675: char fileresprob[FILENAMELENGTH];
5676: char fileresprobcov[FILENAMELENGTH];
5677: char fileresprobcor[FILENAMELENGTH];
5678: double ***varpij;
5679:
5680: strcpy(fileresprob,"PROB_");
5681: strcat(fileresprob,fileres);
5682: if((ficresprob=fopen(fileresprob,"w"))==NULL) {
5683: printf("Problem with resultfile: %s\n", fileresprob);
5684: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprob);
5685: }
5686: strcpy(fileresprobcov,"PROBCOV_");
5687: strcat(fileresprobcov,fileresu);
5688: if((ficresprobcov=fopen(fileresprobcov,"w"))==NULL) {
5689: printf("Problem with resultfile: %s\n", fileresprobcov);
5690: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcov);
5691: }
5692: strcpy(fileresprobcor,"PROBCOR_");
5693: strcat(fileresprobcor,fileresu);
5694: if((ficresprobcor=fopen(fileresprobcor,"w"))==NULL) {
5695: printf("Problem with resultfile: %s\n", fileresprobcor);
5696: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcor);
5697: }
5698: printf("Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
5699: fprintf(ficlog,"Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
5700: printf("Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
5701: fprintf(ficlog,"Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
5702: printf("and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
5703: fprintf(ficlog,"and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
5704: pstamp(ficresprob);
5705: fprintf(ficresprob,"#One-step probabilities and stand. devi in ()\n");
5706: fprintf(ficresprob,"# Age");
5707: pstamp(ficresprobcov);
5708: fprintf(ficresprobcov,"#One-step probabilities and covariance matrix\n");
5709: fprintf(ficresprobcov,"# Age");
5710: pstamp(ficresprobcor);
5711: fprintf(ficresprobcor,"#One-step probabilities and correlation matrix\n");
5712: fprintf(ficresprobcor,"# Age");
1.126 brouard 5713:
5714:
1.222 brouard 5715: for(i=1; i<=nlstate;i++)
5716: for(j=1; j<=(nlstate+ndeath);j++){
5717: fprintf(ficresprob," p%1d-%1d (SE)",i,j);
5718: fprintf(ficresprobcov," p%1d-%1d ",i,j);
5719: fprintf(ficresprobcor," p%1d-%1d ",i,j);
5720: }
5721: /* fprintf(ficresprob,"\n");
5722: fprintf(ficresprobcov,"\n");
5723: fprintf(ficresprobcor,"\n");
5724: */
5725: xp=vector(1,npar);
5726: dnewm=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
5727: doldm=matrix(1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
5728: mu=matrix(1,(nlstate)*(nlstate+ndeath), (int) bage, (int)fage);
5729: varpij=ma3x(1,nlstate*(nlstate+ndeath),1,nlstate*(nlstate+ndeath),(int) bage, (int) fage);
5730: first=1;
5731: fprintf(ficgp,"\n# Routine varprob");
5732: fprintf(fichtm,"\n<li><h4> Computing and drawing one step probabilities with their confidence intervals</h4></li>\n");
5733: fprintf(fichtm,"\n");
5734:
5735: fprintf(fichtm,"\n<li><h4> <a href=\"%s\">Matrix of variance-covariance of one-step probabilities (drawings)</a></h4> this page is important in order to visualize confidence intervals and especially correlation between disability and recovery, or more generally, way in and way back.</li>\n",optionfilehtmcov);
5736: fprintf(fichtmcov,"Current page is file <a href=\"%s\">%s</a><br>\n\n<h4>Matrix of variance-covariance of pairs of step probabilities</h4>\n",optionfilehtmcov, optionfilehtmcov);
5737: fprintf(fichtmcov,"\nEllipsoids of confidence centered on point (p<inf>ij</inf>, p<inf>kl</inf>) are estimated \
1.126 brouard 5738: and drawn. It helps understanding how is the covariance between two incidences.\
5739: They are expressed in year<sup>-1</sup> in order to be less dependent of stepm.<br>\n");
1.222 brouard 5740: 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. \
1.126 brouard 5741: It can be understood this way: if pij and pkl where uncorrelated the (2x2) matrix of covariance \
5742: would have been (1/(var pij), 0 , 0, 1/(var pkl)), and the confidence interval would be 2 \
5743: standard deviations wide on each axis. <br>\
5744: Now, if both incidences are correlated (usual case) we diagonalised the inverse of the covariance matrix\
5745: and made the appropriate rotation to look at the uncorrelated principal directions.<br>\
5746: To be simple, these graphs help to understand the significativity of each parameter in relation to a second other one.<br> \n");
5747:
1.222 brouard 5748: cov[1]=1;
5749: /* tj=cptcoveff; */
1.225 brouard 5750: tj = (int) pow(2,cptcoveff);
1.222 brouard 5751: if (cptcovn<1) {tj=1;ncodemax[1]=1;}
5752: j1=0;
1.224 brouard 5753: for(j1=1; j1<=tj;j1++){ /* For each valid combination of covariates or only once*/
1.222 brouard 5754: if (cptcovn>0) {
5755: fprintf(ficresprob, "\n#********** Variable ");
1.225 brouard 5756: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprob, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5757: fprintf(ficresprob, "**********\n#\n");
5758: fprintf(ficresprobcov, "\n#********** Variable ");
1.225 brouard 5759: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcov, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5760: fprintf(ficresprobcov, "**********\n#\n");
1.220 brouard 5761:
1.222 brouard 5762: fprintf(ficgp, "\n#********** Variable ");
1.225 brouard 5763: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficgp, " V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5764: fprintf(ficgp, "**********\n#\n");
1.220 brouard 5765:
5766:
1.222 brouard 5767: fprintf(fichtmcov, "\n<hr size=\"2\" color=\"#EC5E5E\">********** Variable ");
1.225 brouard 5768: for (z1=1; z1<=cptcoveff; z1++) fprintf(fichtm, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5769: fprintf(fichtmcov, "**********\n<hr size=\"2\" color=\"#EC5E5E\">");
1.220 brouard 5770:
1.222 brouard 5771: fprintf(ficresprobcor, "\n#********** Variable ");
1.225 brouard 5772: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcor, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5773: fprintf(ficresprobcor, "**********\n#");
5774: if(invalidvarcomb[j1]){
5775: fprintf(ficgp,"\n#Combination (%d) ignored because no cases \n",j1);
5776: fprintf(fichtmcov,"\n<h3>Combination (%d) ignored because no cases </h3>\n",j1);
5777: continue;
5778: }
5779: }
5780: gradg=matrix(1,npar,1,(nlstate)*(nlstate+ndeath));
5781: trgradg=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
5782: gp=vector(1,(nlstate)*(nlstate+ndeath));
5783: gm=vector(1,(nlstate)*(nlstate+ndeath));
5784: for (age=bage; age<=fage; age ++){
5785: cov[2]=age;
5786: if(nagesqr==1)
5787: cov[3]= age*age;
5788: for (k=1; k<=cptcovn;k++) {
5789: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,k)];
5790: /*cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,Tvar[k])];*//* j1 1 2 3 4
5791: * 1 1 1 1 1
5792: * 2 2 1 1 1
5793: * 3 1 2 1 1
5794: */
5795: /* nbcode[1][1]=0 nbcode[1][2]=1;*/
5796: }
5797: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
5798: for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
5799: for (k=1; k<=cptcovprod;k++)
5800: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)];
1.220 brouard 5801:
5802:
1.222 brouard 5803: for(theta=1; theta <=npar; theta++){
5804: for(i=1; i<=npar; i++)
5805: xp[i] = x[i] + (i==theta ?delti[theta]:(double)0);
1.220 brouard 5806:
1.222 brouard 5807: pmij(pmmij,cov,ncovmodel,xp,nlstate);
1.220 brouard 5808:
1.222 brouard 5809: k=0;
5810: for(i=1; i<= (nlstate); i++){
5811: for(j=1; j<=(nlstate+ndeath);j++){
5812: k=k+1;
5813: gp[k]=pmmij[i][j];
5814: }
5815: }
1.220 brouard 5816:
1.222 brouard 5817: for(i=1; i<=npar; i++)
5818: xp[i] = x[i] - (i==theta ?delti[theta]:(double)0);
1.220 brouard 5819:
1.222 brouard 5820: pmij(pmmij,cov,ncovmodel,xp,nlstate);
5821: k=0;
5822: for(i=1; i<=(nlstate); i++){
5823: for(j=1; j<=(nlstate+ndeath);j++){
5824: k=k+1;
5825: gm[k]=pmmij[i][j];
5826: }
5827: }
1.220 brouard 5828:
1.222 brouard 5829: for(i=1; i<= (nlstate)*(nlstate+ndeath); i++)
5830: gradg[theta][i]=(gp[i]-gm[i])/(double)2./delti[theta];
5831: }
1.126 brouard 5832:
1.222 brouard 5833: for(j=1; j<=(nlstate)*(nlstate+ndeath);j++)
5834: for(theta=1; theta <=npar; theta++)
5835: trgradg[j][theta]=gradg[theta][j];
1.220 brouard 5836:
1.222 brouard 5837: matprod2(dnewm,trgradg,1,(nlstate)*(nlstate+ndeath),1,npar,1,npar,matcov);
5838: matprod2(doldm,dnewm,1,(nlstate)*(nlstate+ndeath),1,npar,1,(nlstate)*(nlstate+ndeath),gradg);
1.220 brouard 5839:
1.222 brouard 5840: pmij(pmmij,cov,ncovmodel,x,nlstate);
1.220 brouard 5841:
1.222 brouard 5842: k=0;
5843: for(i=1; i<=(nlstate); i++){
5844: for(j=1; j<=(nlstate+ndeath);j++){
5845: k=k+1;
5846: mu[k][(int) age]=pmmij[i][j];
5847: }
5848: }
5849: for(i=1;i<=(nlstate)*(nlstate+ndeath);i++)
5850: for(j=1;j<=(nlstate)*(nlstate+ndeath);j++)
5851: varpij[i][j][(int)age] = doldm[i][j];
1.220 brouard 5852:
1.222 brouard 5853: /*printf("\n%d ",(int)age);
5854: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
5855: printf("%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
5856: fprintf(ficlog,"%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
5857: }*/
1.220 brouard 5858:
1.222 brouard 5859: fprintf(ficresprob,"\n%d ",(int)age);
5860: fprintf(ficresprobcov,"\n%d ",(int)age);
5861: fprintf(ficresprobcor,"\n%d ",(int)age);
1.220 brouard 5862:
1.222 brouard 5863: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++)
5864: fprintf(ficresprob,"%11.3e (%11.3e) ",mu[i][(int) age],sqrt(varpij[i][i][(int)age]));
5865: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
5866: fprintf(ficresprobcov,"%11.3e ",mu[i][(int) age]);
5867: fprintf(ficresprobcor,"%11.3e ",mu[i][(int) age]);
5868: }
5869: i=0;
5870: for (k=1; k<=(nlstate);k++){
5871: for (l=1; l<=(nlstate+ndeath);l++){
5872: i++;
5873: fprintf(ficresprobcov,"\n%d %d-%d",(int)age,k,l);
5874: fprintf(ficresprobcor,"\n%d %d-%d",(int)age,k,l);
5875: for (j=1; j<=i;j++){
5876: /* printf(" k=%d l=%d i=%d j=%d\n",k,l,i,j);fflush(stdout); */
5877: fprintf(ficresprobcov," %11.3e",varpij[i][j][(int)age]);
5878: fprintf(ficresprobcor," %11.3e",varpij[i][j][(int) age]/sqrt(varpij[i][i][(int) age])/sqrt(varpij[j][j][(int)age]));
5879: }
5880: }
5881: }/* end of loop for state */
5882: } /* end of loop for age */
5883: free_vector(gp,1,(nlstate+ndeath)*(nlstate+ndeath));
5884: free_vector(gm,1,(nlstate+ndeath)*(nlstate+ndeath));
5885: free_matrix(trgradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
5886: free_matrix(gradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
5887:
5888: /* Confidence intervalle of pij */
5889: /*
5890: fprintf(ficgp,"\nunset parametric;unset label");
5891: fprintf(ficgp,"\nset log y;unset log x; set xlabel \"Age\";set ylabel \"probability (year-1)\"");
5892: fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65");
5893: 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);
5894: fprintf(fichtm,"\n<br><img src=\"pijgr%s.png\"> ",optionfilefiname);
5895: fprintf(ficgp,"\nset out \"pijgr%s.png\"",optionfilefiname);
5896: fprintf(ficgp,"\nplot \"%s\" every :::%d::%d u 1:2 \"\%%lf",k1,k2,xfilevarprob);
5897: */
5898:
5899: /* Drawing ellipsoids of confidence of two variables p(k1-l1,k2-l2)*/
5900: first1=1;first2=2;
5901: for (k2=1; k2<=(nlstate);k2++){
5902: for (l2=1; l2<=(nlstate+ndeath);l2++){
5903: if(l2==k2) continue;
5904: j=(k2-1)*(nlstate+ndeath)+l2;
5905: for (k1=1; k1<=(nlstate);k1++){
5906: for (l1=1; l1<=(nlstate+ndeath);l1++){
5907: if(l1==k1) continue;
5908: i=(k1-1)*(nlstate+ndeath)+l1;
5909: if(i<=j) continue;
5910: for (age=bage; age<=fage; age ++){
5911: if ((int)age %5==0){
5912: v1=varpij[i][i][(int)age]/stepm*YEARM/stepm*YEARM;
5913: v2=varpij[j][j][(int)age]/stepm*YEARM/stepm*YEARM;
5914: cv12=varpij[i][j][(int)age]/stepm*YEARM/stepm*YEARM;
5915: mu1=mu[i][(int) age]/stepm*YEARM ;
5916: mu2=mu[j][(int) age]/stepm*YEARM;
5917: c12=cv12/sqrt(v1*v2);
5918: /* Computing eigen value of matrix of covariance */
5919: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
5920: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
5921: if ((lc2 <0) || (lc1 <0) ){
5922: if(first2==1){
5923: first1=0;
5924: 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);
5925: }
5926: 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);
5927: /* lc1=fabs(lc1); */ /* If we want to have them positive */
5928: /* lc2=fabs(lc2); */
5929: }
1.220 brouard 5930:
1.222 brouard 5931: /* Eigen vectors */
5932: v11=(1./sqrt(1+(v1-lc1)*(v1-lc1)/cv12/cv12));
5933: /*v21=sqrt(1.-v11*v11); *//* error */
5934: v21=(lc1-v1)/cv12*v11;
5935: v12=-v21;
5936: v22=v11;
5937: tnalp=v21/v11;
5938: if(first1==1){
5939: first1=0;
5940: 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);
5941: }
5942: 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);
5943: /*printf(fignu*/
5944: /* mu1+ v11*lc1*cost + v12*lc2*sin(t) */
5945: /* mu2+ v21*lc1*cost + v22*lc2*sin(t) */
5946: if(first==1){
5947: first=0;
5948: fprintf(ficgp,"\n# Ellipsoids of confidence\n#\n");
5949: fprintf(ficgp,"\nset parametric;unset label");
5950: 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);
5951: fprintf(ficgp,"\nset ter svg size 640, 480");
5952: fprintf(fichtmcov,"\n<br>Ellipsoids of confidence cov(p%1d%1d,p%1d%1d) expressed in year<sup>-1</sup>\
1.220 brouard 5953: :<a href=\"%s_%d%1d%1d-%1d%1d.svg\"> \
1.201 brouard 5954: %s_%d%1d%1d-%1d%1d.svg</A>, ",k1,l1,k2,l2,\
1.222 brouard 5955: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2, \
5956: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
5957: fprintf(fichtmcov,"\n<br><img src=\"%s_%d%1d%1d-%1d%1d.svg\"> ",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
5958: fprintf(fichtmcov,"\n<br> Correlation at age %d (%.3f),",(int) age, c12);
5959: fprintf(ficgp,"\nset out \"%s_%d%1d%1d-%1d%1d.svg\"",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
5960: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
5961: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
5962: 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", \
5963: mu1,std,v11,sqrt(lc1),v12,sqrt(lc2), \
5964: mu2,std,v21,sqrt(lc1),v22,sqrt(lc2));
5965: }else{
5966: first=0;
5967: fprintf(fichtmcov," %d (%.3f),",(int) age, c12);
5968: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
5969: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
5970: 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", \
5971: mu1,std,v11,sqrt(lc1),v12,sqrt(lc2), \
5972: mu2,std,v21,sqrt(lc1),v22,sqrt(lc2));
5973: }/* if first */
5974: } /* age mod 5 */
5975: } /* end loop age */
5976: fprintf(ficgp,"\nset out;\nset out \"%s_%d%1d%1d-%1d%1d.svg\";replot;set out;",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
5977: first=1;
5978: } /*l12 */
5979: } /* k12 */
5980: } /*l1 */
5981: }/* k1 */
5982: } /* loop on combination of covariates j1 */
5983: free_ma3x(varpij,1,nlstate,1,nlstate+ndeath,(int) bage, (int)fage);
5984: free_matrix(mu,1,(nlstate+ndeath)*(nlstate+ndeath),(int) bage, (int)fage);
5985: free_matrix(doldm,1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
5986: free_matrix(dnewm,1,(nlstate)*(nlstate+ndeath),1,npar);
5987: free_vector(xp,1,npar);
5988: fclose(ficresprob);
5989: fclose(ficresprobcov);
5990: fclose(ficresprobcor);
5991: fflush(ficgp);
5992: fflush(fichtmcov);
5993: }
1.126 brouard 5994:
5995:
5996: /******************* Printing html file ***********/
1.201 brouard 5997: void printinghtml(char fileresu[], char title[], char datafile[], int firstpass, \
1.126 brouard 5998: int lastpass, int stepm, int weightopt, char model[],\
5999: int imx,int jmin, int jmax, double jmeanint,char rfileres[],\
1.217 brouard 6000: int popforecast, int prevfcast, int backcast, int estepm , \
1.213 brouard 6001: double jprev1, double mprev1,double anprev1, double dateprev1, \
6002: double jprev2, double mprev2,double anprev2, double dateprev2){
1.237 ! brouard 6003: int jj1, k1, i1, cpt, k4, nres;
1.126 brouard 6004:
6005: fprintf(fichtm,"<ul><li><a href='#firstorder'>Result files (first order: no variance)</a>\n \
6006: <li><a href='#secondorder'>Result files (second order (variance)</a>\n \
6007: </ul>");
1.237 ! brouard 6008: fprintf(fichtm,"<ul><li> model=1+age+%s\n \
! 6009: </ul>", model);
1.214 brouard 6010: fprintf(fichtm,"<ul><li><h4><a name='firstorder'>Result files (first order: no variance)</a></h4>\n");
6011: fprintf(fichtm,"<li>- Observed frequency between two states (during the period defined between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf): <a href=\"%s\">%s</a> (html file)<br/>\n",
6012: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTMFR_",".htm"),subdirfext3(optionfilefiname,"PHTMFR_",".htm"));
6013: fprintf(fichtm,"<li> - Observed prevalence in each state (during the period defined between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf): <a href=\"%s\">%s</a> (html file) ",
1.213 brouard 6014: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTM_",".htm"),subdirfext3(optionfilefiname,"PHTM_",".htm"));
6015: fprintf(fichtm,", <a href=\"%s\">%s</a> (text file) <br>\n",subdirf2(fileresu,"P_"),subdirf2(fileresu,"P_"));
1.126 brouard 6016: fprintf(fichtm,"\
6017: - Estimated transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
1.201 brouard 6018: stepm,subdirf2(fileresu,"PIJ_"),subdirf2(fileresu,"PIJ_"));
1.126 brouard 6019: fprintf(fichtm,"\
1.217 brouard 6020: - Estimated back transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
6021: stepm,subdirf2(fileresu,"PIJB_"),subdirf2(fileresu,"PIJB_"));
6022: fprintf(fichtm,"\
1.126 brouard 6023: - Period (stable) prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
1.201 brouard 6024: subdirf2(fileresu,"PL_"),subdirf2(fileresu,"PL_"));
1.126 brouard 6025: fprintf(fichtm,"\
1.217 brouard 6026: - Period (stable) back prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
6027: subdirf2(fileresu,"PLB_"),subdirf2(fileresu,"PLB_"));
6028: fprintf(fichtm,"\
1.211 brouard 6029: - (a) Life expectancies by health status at initial age, e<sub>i.</sub> (b) health expectancies by health status at initial age, e<sub>ij</sub> . 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 6030: <a href=\"%s\">%s</a> <br>\n",
1.201 brouard 6031: estepm,subdirf2(fileresu,"E_"),subdirf2(fileresu,"E_"));
1.211 brouard 6032: if(prevfcast==1){
6033: fprintf(fichtm,"\
6034: - Prevalence projections by age and states: \
1.201 brouard 6035: <a href=\"%s\">%s</a> <br>\n</li>", subdirf2(fileresu,"F_"),subdirf2(fileresu,"F_"));
1.211 brouard 6036: }
1.126 brouard 6037:
1.222 brouard 6038: fprintf(fichtm," \n<ul><li><b>Graphs</b></li><p>");
1.126 brouard 6039:
1.225 brouard 6040: m=pow(2,cptcoveff);
1.222 brouard 6041: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
1.126 brouard 6042:
1.222 brouard 6043: jj1=0;
1.237 ! brouard 6044:
! 6045: for(nres=1; nres <= nresult; nres++) /* For each resultline */
1.222 brouard 6046: for(k1=1; k1<=m;k1++){
1.237 ! brouard 6047: if(TKresult[nres]!= k1)
! 6048: continue;
1.220 brouard 6049:
1.222 brouard 6050: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
6051: jj1++;
6052: if (cptcovn > 0) {
6053: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
1.225 brouard 6054: for (cpt=1; cpt<=cptcoveff;cpt++){
1.237 ! brouard 6055: fprintf(fichtm," V%d=%d ",Tvresult[nres][cpt],(int)Tresult[nres][cpt]);
! 6056: printf(" V%d=%d ",Tvresult[nres][cpt],Tresult[nres][cpt]);fflush(stdout);
! 6057: /* fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]); */
! 6058: /* printf(" V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]);fflush(stdout); */
1.222 brouard 6059: }
1.237 ! brouard 6060: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
! 6061: fprintf(fichtm," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
! 6062: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);fflush(stdout);
! 6063: }
! 6064:
1.230 brouard 6065: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
1.222 brouard 6066: fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
6067: if(invalidvarcomb[k1]){
6068: fprintf(fichtm,"\n<h3>Combination (%d) ignored because no cases </h3>\n",k1);
6069: printf("\nCombination (%d) ignored because no cases \n",k1);
6070: continue;
6071: }
6072: }
6073: /* aij, bij */
6074: fprintf(fichtm,"<br>- Logit model (yours is: 1+age+%s), for example: logit(pij)=log(pij/pii)= aij+ bij age + V1 age + etc. as a function of age: <a href=\"%s_%d-1.svg\">%s_%d-1.svg</a><br> \
1.211 brouard 6075: <img src=\"%s_%d-1.svg\">",model,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1);
1.222 brouard 6076: /* Pij */
6077: fprintf(fichtm,"<br>\n- P<sub>ij</sub> or conditional probabilities to be observed in state j being in state i, %d (stepm) months before: <a href=\"%s_%d-2.svg\">%s_%d-2.svg</a><br> \
1.201 brouard 6078: <img src=\"%s_%d-2.svg\">",stepm,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1);
1.222 brouard 6079: /* Quasi-incidences */
6080: fprintf(fichtm,"<br>\n- I<sub>ij</sub> or Conditional probabilities to be observed in state j being in state i %d (stepm) months\
1.220 brouard 6081: before but expressed in per year i.e. quasi incidences if stepm is small and probabilities too, \
1.211 brouard 6082: incidence (rates) are the limit when h tends to zero of the ratio of the probability <sub>h</sub>P<sub>ij</sub> \
6083: divided by h: <sub>h</sub>P<sub>ij</sub>/h : <a href=\"%s_%d-3.svg\">%s_%d-3.svg</a><br> \
1.201 brouard 6084: <img src=\"%s_%d-3.svg\">",stepm,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1);
1.222 brouard 6085: /* Survival functions (period) in state j */
6086: for(cpt=1; cpt<=nlstate;cpt++){
6087: fprintf(fichtm,"<br>\n- Survival functions in state %d. Or probability to survive in state %d being in state (1 to %d) at different ages. <a href=\"%s%d_%d.svg\">%s%d_%d.svg</a><br> \
1.201 brouard 6088: <img src=\"%s_%d-%d.svg\">", cpt, cpt, nlstate, subdirf2(optionfilefiname,"LIJ_"),cpt,jj1,subdirf2(optionfilefiname,"LIJ_"),cpt,jj1,subdirf2(optionfilefiname,"LIJ_"),cpt,jj1);
1.222 brouard 6089: }
6090: /* State specific survival functions (period) */
6091: for(cpt=1; cpt<=nlstate;cpt++){
6092: fprintf(fichtm,"<br>\n- Survival functions from state %d in each live state and total.\
1.220 brouard 6093: Or probability to survive in various states (1 to %d) being in state %d at different ages. \
1.201 brouard 6094: <a href=\"%s%d_%d.svg\">%s%d_%d.svg</a><br> <img src=\"%s_%d-%d.svg\">", cpt, nlstate, cpt, subdirf2(optionfilefiname,"LIJT_"),cpt,jj1,subdirf2(optionfilefiname,"LIJT_"),cpt,jj1,subdirf2(optionfilefiname,"LIJT_"),cpt,jj1);
1.222 brouard 6095: }
6096: /* Period (stable) prevalence in each health state */
6097: for(cpt=1; cpt<=nlstate;cpt++){
6098: fprintf(fichtm,"<br>\n- 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.svg\">%s_%d-%d.svg</a><br> \
1.201 brouard 6099: <img src=\"%s_%d-%d.svg\">", cpt, cpt, nlstate, subdirf2(optionfilefiname,"P_"),cpt,jj1,subdirf2(optionfilefiname,"P_"),cpt,jj1,subdirf2(optionfilefiname,"P_"),cpt,jj1);
1.222 brouard 6100: }
6101: if(backcast==1){
6102: /* Period (stable) back prevalence in each health state */
6103: for(cpt=1; cpt<=nlstate;cpt++){
6104: fprintf(fichtm,"<br>\n- Convergence to period (stable) back 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.svg\">%s_%d-%d.svg</a><br> \
1.217 brouard 6105: <img src=\"%s_%d-%d.svg\">", cpt, cpt, nlstate, subdirf2(optionfilefiname,"PB_"),cpt,jj1,subdirf2(optionfilefiname,"PB_"),cpt,jj1,subdirf2(optionfilefiname,"PB_"),cpt,jj1);
1.222 brouard 6106: }
1.217 brouard 6107: }
1.222 brouard 6108: if(prevfcast==1){
6109: /* Projection of prevalence up to period (stable) prevalence in each health state */
6110: for(cpt=1; cpt<=nlstate;cpt++){
6111: fprintf(fichtm,"<br>\n- Projection of cross-sectional prevalence (estimated with cases observed from %.1f to %.1f) up 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.svg\">%s%d_%d.svg</a><br> \
1.213 brouard 6112: <img src=\"%s_%d-%d.svg\">", dateprev1, dateprev2, cpt, cpt, nlstate, subdirf2(optionfilefiname,"PROJ_"),cpt,jj1,subdirf2(optionfilefiname,"PROJ_"),cpt,jj1,subdirf2(optionfilefiname,"PROJ_"),cpt,jj1);
1.222 brouard 6113: }
6114: }
1.220 brouard 6115:
1.222 brouard 6116: for(cpt=1; cpt<=nlstate;cpt++) {
6117: 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) (or area under each survival functions): <a href=\"%s_%d%d.svg\">%s_%d%d.svg</a> <br> \
1.201 brouard 6118: <img src=\"%s_%d%d.svg\">",cpt,nlstate,subdirf2(optionfilefiname,"EXP_"),cpt,jj1,subdirf2(optionfilefiname,"EXP_"),cpt,jj1,subdirf2(optionfilefiname,"EXP_"),cpt,jj1);
1.222 brouard 6119: }
6120: /* } /\* end i1 *\/ */
6121: }/* End k1 */
6122: fprintf(fichtm,"</ul>");
1.126 brouard 6123:
1.222 brouard 6124: fprintf(fichtm,"\
1.126 brouard 6125: \n<br><li><h4> <a name='secondorder'>Result files (second order: variances)</a></h4>\n\
1.193 brouard 6126: - Parameter file with estimated parameters and covariance matrix: <a href=\"%s\">%s</a> <br> \
1.203 brouard 6127: - 95%% confidence intervals and Wald tests of the estimated parameters are in the log file if optimization has been done (mle != 0).<br> \
1.197 brouard 6128: But because parameters are usually highly correlated (a higher incidence of disability \
6129: and a higher incidence of recovery can give very close observed transition) it might \
6130: be very useful to look not only at linear confidence intervals estimated from the \
6131: variances but at the covariance matrix. And instead of looking at the estimated coefficients \
6132: (parameters) of the logistic regression, it might be more meaningful to visualize the \
6133: covariance matrix of the one-step probabilities. \
6134: See page 'Matrix of variance-covariance of one-step probabilities' below. \n", rfileres,rfileres);
1.126 brouard 6135:
1.222 brouard 6136: fprintf(fichtm," - Standard deviation of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
6137: subdirf2(fileresu,"PROB_"),subdirf2(fileresu,"PROB_"));
6138: fprintf(fichtm,"\
1.126 brouard 6139: - Variance-covariance of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
1.222 brouard 6140: subdirf2(fileresu,"PROBCOV_"),subdirf2(fileresu,"PROBCOV_"));
1.126 brouard 6141:
1.222 brouard 6142: fprintf(fichtm,"\
1.126 brouard 6143: - Correlation matrix of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
1.222 brouard 6144: subdirf2(fileresu,"PROBCOR_"),subdirf2(fileresu,"PROBCOR_"));
6145: fprintf(fichtm,"\
1.126 brouard 6146: - 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): \
6147: <a href=\"%s\">%s</a> <br>\n</li>",
1.201 brouard 6148: estepm,subdirf2(fileresu,"CVE_"),subdirf2(fileresu,"CVE_"));
1.222 brouard 6149: fprintf(fichtm,"\
1.126 brouard 6150: - (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): \
6151: <a href=\"%s\">%s</a> <br>\n</li>",
1.201 brouard 6152: estepm,subdirf2(fileresu,"STDE_"),subdirf2(fileresu,"STDE_"));
1.222 brouard 6153: fprintf(fichtm,"\
1.128 brouard 6154: - 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.222 brouard 6155: estepm, subdirf2(fileresu,"V_"),subdirf2(fileresu,"V_"));
6156: fprintf(fichtm,"\
1.128 brouard 6157: - 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",
1.222 brouard 6158: estepm, subdirf2(fileresu,"T_"),subdirf2(fileresu,"T_"));
6159: fprintf(fichtm,"\
1.126 brouard 6160: - Standard deviation of period (stable) prevalences: <a href=\"%s\">%s</a> <br>\n",\
1.222 brouard 6161: subdirf2(fileresu,"VPL_"),subdirf2(fileresu,"VPL_"));
1.126 brouard 6162:
6163: /* if(popforecast==1) fprintf(fichtm,"\n */
6164: /* - Prevalences forecasting: <a href=\"f%s\">f%s</a> <br>\n */
6165: /* - Population forecasting (if popforecast=1): <a href=\"pop%s\">pop%s</a> <br>\n */
6166: /* <br>",fileres,fileres,fileres,fileres); */
6167: /* else */
6168: /* 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); */
1.222 brouard 6169: fflush(fichtm);
6170: fprintf(fichtm," <ul><li><b>Graphs</b></li><p>");
1.126 brouard 6171:
1.225 brouard 6172: m=pow(2,cptcoveff);
1.222 brouard 6173: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
1.126 brouard 6174:
1.222 brouard 6175: jj1=0;
1.237 ! brouard 6176:
! 6177: for(nres=1; nres <= nresult; nres++) /* For each resultline */
1.222 brouard 6178: for(k1=1; k1<=m;k1++){
1.237 ! brouard 6179: if(TKresult[nres]!= k1)
! 6180: continue;
1.222 brouard 6181: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
6182: jj1++;
1.126 brouard 6183: if (cptcovn > 0) {
6184: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
1.225 brouard 6185: for (cpt=1; cpt<=cptcoveff;cpt++) /**< cptcoveff number of variables */
1.237 ! brouard 6186: fprintf(fichtm," V%d=%d ",Tvresult[nres][cpt],Tresult[nres][cpt]);
! 6187: /* fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]); */
! 6188: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
! 6189: fprintf(fichtm," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
! 6190: }
! 6191:
1.126 brouard 6192: fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
1.220 brouard 6193:
1.222 brouard 6194: if(invalidvarcomb[k1]){
6195: fprintf(fichtm,"\n<h4>Combination (%d) ignored because no cases </h4>\n",k1);
6196: continue;
6197: }
1.126 brouard 6198: }
6199: for(cpt=1; cpt<=nlstate;cpt++) {
1.218 brouard 6200: fprintf(fichtm,"\n<br>- Observed (cross-sectional) and period (incidence based) \
6201: prevalence (with 95%% confidence interval) in state (%d): <a href=\"%s_%d-%d.svg\"> %s_%d-%d.svg</a>\n <br>\
1.205 brouard 6202: <img src=\"%s_%d-%d.svg\">",cpt,subdirf2(optionfilefiname,"V_"),cpt,jj1,subdirf2(optionfilefiname,"V_"),cpt,jj1,subdirf2(optionfilefiname,"V_"),cpt,jj1);
1.126 brouard 6203: }
6204: fprintf(fichtm,"\n<br>- Total life expectancy by age and \
1.128 brouard 6205: health expectancies in states (1) and (2). If popbased=1 the smooth (due to the model) \
6206: true period expectancies (those weighted with period prevalences are also\
6207: drawn in addition to the population based expectancies computed using\
1.218 brouard 6208: observed and cahotic prevalences: <a href=\"%s_%d.svg\">%s_%d.svg</a>\n<br>\
1.205 brouard 6209: <img src=\"%s_%d.svg\">",subdirf2(optionfilefiname,"E_"),jj1,subdirf2(optionfilefiname,"E_"),jj1,subdirf2(optionfilefiname,"E_"),jj1);
1.222 brouard 6210: /* } /\* end i1 *\/ */
6211: }/* End k1 */
6212: fprintf(fichtm,"</ul>");
6213: fflush(fichtm);
1.126 brouard 6214: }
6215:
6216: /******************* Gnuplot file **************/
1.223 brouard 6217: void printinggnuplot(char fileresu[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , int prevfcast, int backcast, char pathc[], double p[]){
1.126 brouard 6218:
6219: char dirfileres[132],optfileres[132];
1.223 brouard 6220: char gplotcondition[132];
1.237 ! brouard 6221: int cpt=0,k1=0,i=0,k=0,j=0,jk=0,k2=0,k3=0,k4=0,ij=0, ijp=0, l=0;
1.211 brouard 6222: int lv=0, vlv=0, kl=0;
1.130 brouard 6223: int ng=0;
1.201 brouard 6224: int vpopbased;
1.223 brouard 6225: int ioffset; /* variable offset for columns */
1.235 brouard 6226: int nres=0; /* Index of resultline */
1.219 brouard 6227:
1.126 brouard 6228: /* if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) { */
6229: /* printf("Problem with file %s",optionfilegnuplot); */
6230: /* fprintf(ficlog,"Problem with file %s",optionfilegnuplot); */
6231: /* } */
6232:
6233: /*#ifdef windows */
6234: fprintf(ficgp,"cd \"%s\" \n",pathc);
1.223 brouard 6235: /*#endif */
1.225 brouard 6236: m=pow(2,cptcoveff);
1.126 brouard 6237:
1.202 brouard 6238: /* Contribution to likelihood */
6239: /* Plot the probability implied in the likelihood */
1.223 brouard 6240: fprintf(ficgp,"\n# Contributions to the Likelihood, mle >=1. For mle=4 no interpolation, pure matrix products.\n#\n");
6241: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Likelihood (-2Log(L))\";");
6242: /* fprintf(ficgp,"\nset ter svg size 640, 480"); */ /* Too big for svg */
6243: fprintf(ficgp,"\nset ter pngcairo size 640, 480");
1.204 brouard 6244: /* nice for mle=4 plot by number of matrix products.
1.202 brouard 6245: replot "rrtest1/toto.txt" u 2:($4 == 1 && $5==2 ? $9 : 1/0):5 t "p12" with point lc 1 */
6246: /* replot exp(p1+p2*x)/(1+exp(p1+p2*x)+exp(p3+p4*x)+exp(p5+p6*x)) t "p12(x)" */
1.223 brouard 6247: /* fprintf(ficgp,"\nset out \"%s.svg\";",subdirf2(optionfilefiname,"ILK_")); */
6248: fprintf(ficgp,"\nset out \"%s-dest.png\";",subdirf2(optionfilefiname,"ILK_"));
6249: fprintf(ficgp,"\nset log y;plot \"%s\" u 2:(-$13):6 t \"All sample, transitions colored by destination\" with dots lc variable; set out;\n",subdirf(fileresilk));
6250: fprintf(ficgp,"\nset out \"%s-ori.png\";",subdirf2(optionfilefiname,"ILK_"));
6251: fprintf(ficgp,"\nset log y;plot \"%s\" u 2:(-$13):5 t \"All sample, transitions colored by origin\" with dots lc variable; set out;\n\n",subdirf(fileresilk));
6252: for (i=1; i<= nlstate ; i ++) {
6253: fprintf(ficgp,"\nset out \"%s-p%dj.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i);
6254: fprintf(ficgp,"unset log;\n# plot weighted, mean weight should have point size of 0.5\n plot \"%s\"",subdirf(fileresilk));
6255: fprintf(ficgp," u 2:($5 == %d && $6==%d ? $10 : 1/0):($12/4.):6 t \"p%d%d\" with points pointtype 7 ps variable lc variable \\\n",i,1,i,1);
6256: for (j=2; j<= nlstate+ndeath ; j ++) {
6257: fprintf(ficgp,",\\\n \"\" u 2:($5 == %d && $6==%d ? $10 : 1/0):($12/4.):6 t \"p%d%d\" with points pointtype 7 ps variable lc variable ",i,j,i,j);
6258: }
6259: fprintf(ficgp,";\nset out; unset ylabel;\n");
6260: }
6261: /* unset log; plot "rrtest1_sorted_4/ILK_rrtest1_sorted_4.txt" u 2:($4 == 1 && $5==2 ? $9 : 1/0):5 t "p12" with points lc variable */
6262: /* fprintf(ficgp,"\nset log y;plot \"%s\" u 2:(-$11):3 t \"All sample, all transitions\" with dots lc variable",subdirf(fileresilk)); */
6263: /* fprintf(ficgp,"\nreplot \"%s\" u 2:($3 <= 3 ? -$11 : 1/0):3 t \"First 3 individuals\" with line lc variable", subdirf(fileresilk)); */
6264: fprintf(ficgp,"\nset out;unset log\n");
6265: /* fprintf(ficgp,"\nset out \"%s.svg\"; replot; set out; # bug gnuplot",subdirf2(optionfilefiname,"ILK_")); */
1.202 brouard 6266:
1.126 brouard 6267: strcpy(dirfileres,optionfilefiname);
6268: strcpy(optfileres,"vpl");
1.223 brouard 6269: /* 1eme*/
1.211 brouard 6270: for (cpt=1; cpt<= nlstate ; cpt ++) { /* For each live state */
1.236 brouard 6271: for (k1=1; k1<= m ; k1 ++) /* For each valid combination of covariate */
6272: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.235 brouard 6273: /* plot [100000000000000000000:-100000000000000000000] "mysbiaspar/vplrmysbiaspar.txt to check */
6274: if(TKresult[nres]!= k1)
6275: continue;
6276: /* We are interested in selected combination by the resultline */
6277: printf("\n# 1st: Period (stable) prevalence with CI: 'VPL_' files and live state =%d ", cpt);
6278: fprintf(ficgp,"\n# 1st: Period (stable) prevalence with CI: 'VPL_' files and live state =%d ", cpt);
1.225 brouard 6279: for (k=1; k<=cptcoveff; k++){ /* For each covariate k get corresponding value lv for combination k1 */
6280: lv= decodtabm(k1,k,cptcoveff); /* Should be the value of the covariate corresponding to k1 combination */
1.223 brouard 6281: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6282: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6283: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6284: vlv= nbcode[Tvaraff[k]][lv]; /* vlv is the value of the covariate lv, 0 or 1 */
6285: /* For each combination of covariate k1 (V1=1, V3=0), we printed the current covariate k and its value vlv */
1.235 brouard 6286: printf(" V%d=%d ",Tvaraff[k],vlv);
1.223 brouard 6287: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 6288: }
1.235 brouard 6289: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
6290: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
6291: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
6292: }
6293: printf("\n#\n");
1.211 brouard 6294: fprintf(ficgp,"\n#\n");
1.223 brouard 6295: if(invalidvarcomb[k1]){
6296: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6297: continue;
6298: }
1.235 brouard 6299:
1.223 brouard 6300: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"V_"),cpt,k1);
6301: fprintf(ficgp,"\n#set out \"V_%s_%d-%d.svg\" \n",optionfilefiname,cpt,k1);
6302: fprintf(ficgp,"set xlabel \"Age\" \n\
1.235 brouard 6303: set ylabel \"Probability\" \n \
6304: set ter svg size 640, 480\n \
1.201 brouard 6305: plot [%.f:%.f] \"%s\" every :::%d::%d u 1:2 \"%%lf",ageminpar,fage,subdirf2(fileresu,"VPL_"),k1-1,k1-1);
1.235 brouard 6306:
1.223 brouard 6307: for (i=1; i<= nlstate ; i ++) {
6308: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
6309: else fprintf(ficgp," %%*lf (%%*lf)");
6310: }
6311: fprintf(ficgp,"\" t\"Period (stable) prevalence\" w l lt 0,\"%s\" every :::%d::%d u 1:($2+1.96*$3) \"%%lf",subdirf2(fileresu,"VPL_"),k1-1,k1-1);
6312: for (i=1; i<= nlstate ; i ++) {
6313: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
6314: else fprintf(ficgp," %%*lf (%%*lf)");
6315: }
6316: fprintf(ficgp,"\" t\"95%% CI\" w l lt 1,\"%s\" every :::%d::%d u 1:($2-1.96*$3) \"%%lf",subdirf2(fileresu,"VPL_"),k1-1,k1-1);
6317: for (i=1; i<= nlstate ; i ++) {
6318: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
6319: else fprintf(ficgp," %%*lf (%%*lf)");
6320: }
6321: fprintf(ficgp,"\" t\"\" w l lt 1,\"%s\" every :::%d::%d u 1:($%d) t\"Observed prevalence\" w l lt 2",subdirf2(fileresu,"P_"),k1-1,k1-1,2+4*(cpt-1));
6322: if(backcast==1){ /* We need to get the corresponding values of the covariates involved in this combination k1 */
6323: /* fprintf(ficgp,",\"%s\" every :::%d::%d u 1:($%d) t\"Backward stable prevalence\" w l lt 3",subdirf2(fileresu,"PLB_"),k1-1,k1-1,1+cpt); */
6324: fprintf(ficgp,",\"%s\" u 1:((",subdirf2(fileresu,"PLB_")); /* Age is in 1 */
1.225 brouard 6325: if(cptcoveff ==0){
1.223 brouard 6326: fprintf(ficgp,"$%d)) t 'Backward prevalence in state %d' with line ", 2+(cpt-1), cpt );
6327: }else{
6328: kl=0;
1.225 brouard 6329: for (k=1; k<=cptcoveff; k++){ /* For each combination of covariate */
6330: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
1.223 brouard 6331: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6332: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6333: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6334: vlv= nbcode[Tvaraff[k]][lv];
6335: kl++;
6336: /* kl=6+(cpt-1)*(nlstate+1)+1+(i-1); /\* 6+(1-1)*(2+1)+1+(1-1)=7, 6+(2-1)(2+1)+1+(1-1)=10 *\/ */
6337: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
6338: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
6339: /* '' u 6:(($1==1 && $2==0 && $3==2 && $4==0)? $9/(1.-$15) : 1/0):($5==2000? 3:2) t 'p.1' with line lc variable*/
1.225 brouard 6340: if(k==cptcoveff){
1.227 brouard 6341: fprintf(ficgp,"$%d==%d && $%d==%d)? $%d : 1/0) t 'Backward prevalence in state %d' ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv], \
6342: 4+(cpt-1), cpt ); /* 4 or 6 ?*/
1.223 brouard 6343: }else{
6344: fprintf(ficgp,"$%d==%d && $%d==%d && ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv]);
6345: kl++;
6346: }
6347: } /* end covariate */
6348: } /* end if no covariate */
6349: } /* end if backcast */
6350: fprintf(ficgp,"\nset out \n");
1.201 brouard 6351: } /* k1 */
6352: } /* cpt */
1.235 brouard 6353:
6354:
1.126 brouard 6355: /*2 eme*/
1.236 brouard 6356: for (k1=1; k1<= m ; k1 ++)
6357: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
6358: if(TKresult[nres]!= k1)
6359: continue;
1.223 brouard 6360: fprintf(ficgp,"\n# 2nd: Total life expectancy with CI: 't' files ");
1.225 brouard 6361: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
6362: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
1.223 brouard 6363: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6364: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6365: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6366: vlv= nbcode[Tvaraff[k]][lv];
6367: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.236 brouard 6368: }
6369: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
6370: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
6371: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.223 brouard 6372: }
6373: fprintf(ficgp,"\n#\n");
6374: if(invalidvarcomb[k1]){
6375: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6376: continue;
6377: }
1.219 brouard 6378:
1.223 brouard 6379: fprintf(ficgp,"\nset out \"%s_%d.svg\" \n",subdirf2(optionfilefiname,"E_"),k1);
6380: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
6381: if(vpopbased==0)
6382: fprintf(ficgp,"set ylabel \"Years\" \nset ter svg size 640, 480\nplot [%.f:%.f] ",ageminpar,fage);
6383: else
6384: fprintf(ficgp,"\nreplot ");
6385: for (i=1; i<= nlstate+1 ; i ++) {
6386: k=2*i;
6387: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2==%d && $4!=0 ?$4 : 1/0) \"%%lf %%lf %%lf",subdirf2(fileresu,"T_"),k1-1,k1-1, vpopbased);
6388: for (j=1; j<= nlstate+1 ; j ++) {
6389: if (j==i) fprintf(ficgp," %%lf (%%lf)");
6390: else fprintf(ficgp," %%*lf (%%*lf)");
6391: }
6392: if (i== 1) fprintf(ficgp,"\" t\"TLE\" w l lt %d, \\\n",i);
6393: else fprintf(ficgp,"\" t\"LE in state (%d)\" w l lt %d, \\\n",i-1,i+1);
6394: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2==%d && $4!=0 ? $4-$5*2 : 1/0) \"%%lf %%lf %%lf",subdirf2(fileresu,"T_"),k1-1,k1-1,vpopbased);
6395: for (j=1; j<= nlstate+1 ; j ++) {
6396: if (j==i) fprintf(ficgp," %%lf (%%lf)");
6397: else fprintf(ficgp," %%*lf (%%*lf)");
6398: }
6399: fprintf(ficgp,"\" t\"\" w l lt 0,");
6400: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2==%d && $4!=0 ? $4+$5*2 : 1/0) \"%%lf %%lf %%lf",subdirf2(fileresu,"T_"),k1-1,k1-1,vpopbased);
6401: for (j=1; j<= nlstate+1 ; j ++) {
6402: if (j==i) fprintf(ficgp," %%lf (%%lf)");
6403: else fprintf(ficgp," %%*lf (%%*lf)");
6404: }
6405: if (i== (nlstate+1)) fprintf(ficgp,"\" t\"\" w l lt 0");
6406: else fprintf(ficgp,"\" t\"\" w l lt 0,\\\n");
6407: } /* state */
6408: } /* vpopbased */
6409: fprintf(ficgp,"\nset out;set out \"%s_%d.svg\"; replot; set out; \n",subdirf2(optionfilefiname,"E_"),k1); /* Buggy gnuplot */
1.235 brouard 6410: } /* k1 end 2 eme*/
1.219 brouard 6411:
6412:
1.126 brouard 6413: /*3eme*/
1.236 brouard 6414: for (k1=1; k1<= m ; k1 ++)
6415: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
6416: if(TKresult[nres]!= k)
6417: continue;
1.220 brouard 6418:
1.126 brouard 6419: for (cpt=1; cpt<= nlstate ; cpt ++) {
1.236 brouard 6420: fprintf(ficgp,"\n# 3d: Life expectancy with EXP_ files: combination=%d state=%d",k1, cpt);
1.237 ! brouard 6421: for (k=1; k<=cptcoveff; k++){ /* For each covariate dummy combination and each value */
1.225 brouard 6422: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
1.223 brouard 6423: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6424: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6425: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6426: vlv= nbcode[Tvaraff[k]][lv];
6427: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 6428: }
1.237 ! brouard 6429: /* for(k=1; k <= ncovds; k++){ */
1.236 brouard 6430: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
6431: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
6432: }
1.211 brouard 6433: fprintf(ficgp,"\n#\n");
1.223 brouard 6434: if(invalidvarcomb[k1]){
6435: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6436: continue;
6437: }
1.219 brouard 6438:
1.126 brouard 6439: /* k=2+nlstate*(2*cpt-2); */
6440: k=2+(nlstate+1)*(cpt-1);
1.201 brouard 6441: fprintf(ficgp,"\nset out \"%s_%d%d.svg\" \n",subdirf2(optionfilefiname,"EXP_"),cpt,k1);
1.199 brouard 6442: fprintf(ficgp,"set ter svg size 640, 480\n\
1.201 brouard 6443: plot [%.f:%.f] \"%s\" every :::%d::%d u 1:%d t \"e%d1\" w l",ageminpar,fage,subdirf2(fileresu,"E_"),k1-1,k1-1,k,cpt);
1.126 brouard 6444: /*fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d-2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
1.223 brouard 6445: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
6446: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
6447: fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d+2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
6448: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
6449: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
1.219 brouard 6450:
1.126 brouard 6451: */
6452: for (i=1; i< nlstate ; i ++) {
1.223 brouard 6453: fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d%d\" w l",subdirf2(fileresu,"E_"),k1-1,k1-1,k+i,cpt,i+1);
6454: /* 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);*/
1.219 brouard 6455:
1.126 brouard 6456: }
1.201 brouard 6457: fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d.\" w l",subdirf2(fileresu,"E_"),k1-1,k1-1,k+nlstate,cpt);
1.126 brouard 6458: }
6459: }
6460:
1.223 brouard 6461: /* 4eme */
1.201 brouard 6462: /* Survival functions (period) from state i in state j by initial state i */
1.237 ! brouard 6463: for (k1=1; k1<= m ; k1 ++) /* For each covariate combination if any */
! 6464: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
! 6465: if(TKresult[nres]!= k1)
1.236 brouard 6466: continue;
1.220 brouard 6467:
1.201 brouard 6468: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.211 brouard 6469: fprintf(ficgp,"\n#\n#\n# Survival functions in state j : 'LIJ_' files, cov=%d state=%d",k1, cpt);
1.237 ! brouard 6470: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
1.225 brouard 6471: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
1.223 brouard 6472: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6473: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6474: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6475: vlv= nbcode[Tvaraff[k]][lv];
6476: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 6477: }
1.236 brouard 6478: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
6479: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
6480: }
1.211 brouard 6481: fprintf(ficgp,"\n#\n");
1.223 brouard 6482: if(invalidvarcomb[k1]){
6483: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6484: continue;
6485: }
1.220 brouard 6486:
1.201 brouard 6487: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJ_"),cpt,k1);
6488: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
1.220 brouard 6489: set ter svg size 640, 480\n \
6490: unset log y\n \
1.201 brouard 6491: plot [%.f:%.f] ", ageminpar, agemaxpar);
1.211 brouard 6492: k=3;
1.201 brouard 6493: for (i=1; i<= nlstate ; i ++){
1.223 brouard 6494: if(i==1){
6495: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
6496: }else{
6497: fprintf(ficgp,", '' ");
6498: }
6499: l=(nlstate+ndeath)*(i-1)+1;
6500: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
6501: for (j=2; j<= nlstate+ndeath ; j ++)
6502: fprintf(ficgp,"+$%d",k+l+j-1);
6503: fprintf(ficgp,")) t \"l(%d,%d)\" w l",i,cpt);
1.201 brouard 6504: } /* nlstate */
6505: fprintf(ficgp,"\nset out\n");
6506: } /* end cpt state*/
6507: } /* end covariate */
1.220 brouard 6508:
6509: /* 5eme */
1.201 brouard 6510: /* Survival functions (period) from state i in state j by final state j */
1.236 brouard 6511: for (k1=1; k1<= m ; k1 ++) /* For each covariate combination if any */
6512: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
6513: if(TKresult[nres]!= k1)
6514: continue;
1.201 brouard 6515: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each inital state */
1.223 brouard 6516:
1.201 brouard 6517: fprintf(ficgp,"\n#\n#\n# Survival functions in state j and all livestates from state i by final state j: 'lij' files, cov=%d state=%d",k1, cpt);
1.225 brouard 6518: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
1.227 brouard 6519: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
6520: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6521: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6522: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6523: vlv= nbcode[Tvaraff[k]][lv];
6524: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 6525: }
1.236 brouard 6526: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
6527: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
6528: }
1.211 brouard 6529: fprintf(ficgp,"\n#\n");
1.223 brouard 6530: if(invalidvarcomb[k1]){
1.227 brouard 6531: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6532: continue;
1.223 brouard 6533: }
1.227 brouard 6534:
1.201 brouard 6535: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJT_"),cpt,k1);
6536: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
1.227 brouard 6537: set ter svg size 640, 480\n \
6538: unset log y\n \
1.201 brouard 6539: plot [%.f:%.f] ", ageminpar, agemaxpar);
1.211 brouard 6540: k=3;
1.201 brouard 6541: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
1.227 brouard 6542: if(j==1)
6543: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
6544: else
6545: fprintf(ficgp,", '' ");
6546: l=(nlstate+ndeath)*(cpt-1) +j;
6547: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):($%d",k1,k+l);
6548: /* for (i=2; i<= nlstate+ndeath ; i ++) */
6549: /* fprintf(ficgp,"+$%d",k+l+i-1); */
6550: fprintf(ficgp,") t \"l(%d,%d)\" w l",cpt,j);
1.201 brouard 6551: } /* nlstate */
6552: fprintf(ficgp,", '' ");
6553: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):(",k1);
6554: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
1.227 brouard 6555: l=(nlstate+ndeath)*(cpt-1) +j;
6556: if(j < nlstate)
6557: fprintf(ficgp,"$%d +",k+l);
6558: else
6559: fprintf(ficgp,"$%d) t\"l(%d,.)\" w l",k+l,cpt);
1.201 brouard 6560: }
6561: fprintf(ficgp,"\nset out\n");
6562: } /* end cpt state*/
6563: } /* end covariate */
1.227 brouard 6564:
1.220 brouard 6565: /* 6eme */
1.202 brouard 6566: /* CV preval stable (period) for each covariate */
1.237 ! brouard 6567: for (k1=1; k1<= m ; k1 ++) /* For each covariate combination if any */
! 6568: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
! 6569: if(TKresult[nres]!= k1)
! 6570: continue;
1.153 brouard 6571: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.227 brouard 6572:
1.211 brouard 6573: fprintf(ficgp,"\n#\n#\n#CV preval stable (period): 'pij' files, covariatecombination#=%d state=%d",k1, cpt);
1.225 brouard 6574: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
1.227 brouard 6575: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
6576: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6577: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6578: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6579: vlv= nbcode[Tvaraff[k]][lv];
6580: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 6581: }
1.237 ! brouard 6582: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
! 6583: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
! 6584: }
1.211 brouard 6585: fprintf(ficgp,"\n#\n");
1.223 brouard 6586: if(invalidvarcomb[k1]){
1.227 brouard 6587: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6588: continue;
1.223 brouard 6589: }
1.227 brouard 6590:
1.201 brouard 6591: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"P_"),cpt,k1);
1.126 brouard 6592: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
1.227 brouard 6593: set ter svg size 640, 480\n \
6594: unset log y\n \
1.153 brouard 6595: plot [%.f:%.f] ", ageminpar, agemaxpar);
1.211 brouard 6596: k=3; /* Offset */
1.153 brouard 6597: for (i=1; i<= nlstate ; i ++){
1.227 brouard 6598: if(i==1)
6599: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
6600: else
6601: fprintf(ficgp,", '' ");
6602: l=(nlstate+ndeath)*(i-1)+1;
6603: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
6604: for (j=2; j<= nlstate ; j ++)
6605: fprintf(ficgp,"+$%d",k+l+j-1);
6606: fprintf(ficgp,")) t \"prev(%d,%d)\" w l",i,cpt);
1.153 brouard 6607: } /* nlstate */
1.201 brouard 6608: fprintf(ficgp,"\nset out\n");
1.153 brouard 6609: } /* end cpt state*/
6610: } /* end covariate */
1.227 brouard 6611:
6612:
1.220 brouard 6613: /* 7eme */
1.218 brouard 6614: if(backcast == 1){
1.217 brouard 6615: /* CV back preval stable (period) for each covariate */
1.237 ! brouard 6616: for (k1=1; k1<= m ; k1 ++) /* For each covariate combination if any */
! 6617: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
! 6618: if(TKresult[nres]!= k1)
! 6619: continue;
1.218 brouard 6620: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.227 brouard 6621: fprintf(ficgp,"\n#\n#\n#CV Back preval stable (period): 'pij' files, covariatecombination#=%d state=%d",k1, cpt);
6622: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
6623: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
6624: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6625: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
1.223 brouard 6626: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
1.227 brouard 6627: vlv= nbcode[Tvaraff[k]][lv];
6628: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
6629: }
1.237 ! brouard 6630: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
! 6631: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
! 6632: }
1.227 brouard 6633: fprintf(ficgp,"\n#\n");
6634: if(invalidvarcomb[k1]){
6635: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6636: continue;
6637: }
6638:
6639: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"PB_"),cpt,k1);
6640: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
6641: set ter svg size 640, 480\n \
6642: unset log y\n \
1.218 brouard 6643: plot [%.f:%.f] ", ageminpar, agemaxpar);
1.227 brouard 6644: k=3; /* Offset */
6645: for (i=1; i<= nlstate ; i ++){
6646: if(i==1)
6647: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJB_"));
6648: else
6649: fprintf(ficgp,", '' ");
6650: /* l=(nlstate+ndeath)*(i-1)+1; */
6651: l=(nlstate+ndeath)*(cpt-1)+1;
6652: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l); /\* a vérifier *\/ */
6653: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l+(cpt-1)+i-1); /\* a vérifier *\/ */
6654: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d",k1,k+l+(cpt-1)+i-1); /* a vérifier */
6655: /* for (j=2; j<= nlstate ; j ++) */
6656: /* fprintf(ficgp,"+$%d",k+l+j-1); */
6657: /* /\* fprintf(ficgp,"+$%d",k+l+j-1); *\/ */
6658: fprintf(ficgp,") t \"bprev(%d,%d)\" w l",i,cpt);
6659: } /* nlstate */
6660: fprintf(ficgp,"\nset out\n");
1.218 brouard 6661: } /* end cpt state*/
6662: } /* end covariate */
6663: } /* End if backcast */
6664:
1.223 brouard 6665: /* 8eme */
1.218 brouard 6666: if(prevfcast==1){
6667: /* Projection from cross-sectional to stable (period) for each covariate */
6668:
1.237 ! brouard 6669: for (k1=1; k1<= m ; k1 ++) /* For each covariate combination if any */
! 6670: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
! 6671: if(TKresult[nres]!= k1)
! 6672: continue;
1.211 brouard 6673: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.227 brouard 6674: fprintf(ficgp,"\n#\n#\n#Projection of prevalence to stable (period): 'PROJ_' files, covariatecombination#=%d state=%d",k1, cpt);
6675: for (k=1; k<=cptcoveff; k++){ /* For each correspondig covariate value */
6676: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
6677: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6678: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6679: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6680: vlv= nbcode[Tvaraff[k]][lv];
6681: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
6682: }
1.237 ! brouard 6683: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
! 6684: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
! 6685: }
1.227 brouard 6686: fprintf(ficgp,"\n#\n");
6687: if(invalidvarcomb[k1]){
6688: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6689: continue;
6690: }
6691:
6692: fprintf(ficgp,"# hpijx=probability over h years, hp.jx is weighted by observed prev\n ");
6693: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"PROJ_"),cpt,k1);
6694: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\
6695: set ter svg size 640, 480\n \
6696: unset log y\n \
1.219 brouard 6697: plot [%.f:%.f] ", ageminpar, agemaxpar);
1.227 brouard 6698: for (i=1; i<= nlstate+1 ; i ++){ /* nlstate +1 p11 p21 p.1 */
6699: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
6700: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
6701: /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
6702: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
6703: if(i==1){
6704: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"F_"));
6705: }else{
6706: fprintf(ficgp,",\\\n '' ");
6707: }
6708: if(cptcoveff ==0){ /* No covariate */
6709: ioffset=2; /* Age is in 2 */
6710: /*# yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
6711: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
6712: /*# V1 = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
6713: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
6714: fprintf(ficgp," u %d:(", ioffset);
6715: if(i==nlstate+1)
6716: fprintf(ficgp," $%d/(1.-$%d)) t 'pw.%d' with line ", \
6717: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
6718: else
6719: fprintf(ficgp," $%d/(1.-$%d)) t 'p%d%d' with line ", \
6720: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,i,cpt );
6721: }else{ /* more than 2 covariates */
6722: if(cptcoveff ==1){
6723: ioffset=4; /* Age is in 4 */
6724: }else{
6725: ioffset=6; /* Age is in 6 */
6726: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
6727: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
6728: }
6729: fprintf(ficgp," u %d:(",ioffset);
6730: kl=0;
6731: strcpy(gplotcondition,"(");
6732: for (k=1; k<=cptcoveff; k++){ /* For each covariate writing the chain of conditions */
6733: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to combination k1 and covariate k */
6734: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6735: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6736: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6737: vlv= nbcode[Tvaraff[k]][lv]; /* Value of the modality of Tvaraff[k] */
6738: kl++;
6739: sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,Tvaraff[k], kl+1, nbcode[Tvaraff[k]][lv]);
6740: kl++;
6741: if(k <cptcoveff && cptcoveff>1)
6742: sprintf(gplotcondition+strlen(gplotcondition)," && ");
6743: }
6744: strcpy(gplotcondition+strlen(gplotcondition),")");
6745: /* kl=6+(cpt-1)*(nlstate+1)+1+(i-1); /\* 6+(1-1)*(2+1)+1+(1-1)=7, 6+(2-1)(2+1)+1+(1-1)=10 *\/ */
6746: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
6747: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
6748: /* '' u 6:(($1==1 && $2==0 && $3==2 && $4==0)? $9/(1.-$15) : 1/0):($5==2000? 3:2) t 'p.1' with line lc variable*/
6749: if(i==nlstate+1){
6750: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p.%d' with line ", gplotcondition, \
6751: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
6752: }else{
6753: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \
6754: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset +1+(i-1)+(nlstate+1)*nlstate,i,cpt );
6755: }
6756: } /* end if covariate */
6757: } /* nlstate */
6758: fprintf(ficgp,"\nset out\n");
1.223 brouard 6759: } /* end cpt state*/
6760: } /* end covariate */
6761: } /* End if prevfcast */
1.227 brouard 6762:
6763:
1.223 brouard 6764: /* proba elementaires */
6765: fprintf(ficgp,"\n##############\n#MLE estimated parameters\n#############\n");
1.126 brouard 6766: for(i=1,jk=1; i <=nlstate; i++){
1.187 brouard 6767: fprintf(ficgp,"# initial state %d\n",i);
1.126 brouard 6768: for(k=1; k <=(nlstate+ndeath); k++){
6769: if (k != i) {
1.227 brouard 6770: fprintf(ficgp,"# current state %d\n",k);
6771: for(j=1; j <=ncovmodel; j++){
6772: fprintf(ficgp,"p%d=%f; ",jk,p[jk]);
6773: jk++;
6774: }
6775: fprintf(ficgp,"\n");
1.126 brouard 6776: }
6777: }
1.223 brouard 6778: }
1.187 brouard 6779: fprintf(ficgp,"##############\n#\n");
1.227 brouard 6780:
1.145 brouard 6781: /*goto avoid;*/
1.200 brouard 6782: fprintf(ficgp,"\n##############\n#Graphics of probabilities or incidences\n#############\n");
1.187 brouard 6783: fprintf(ficgp,"# logi(p12/p11)=a12+b12*age+c12age*age+d12*V1+e12*V1*age\n");
6784: fprintf(ficgp,"# logi(p12/p11)=p1 +p2*age +p3*age*age+ p4*V1+ p5*V1*age\n");
6785: fprintf(ficgp,"# logi(p13/p11)=a13+b13*age+c13age*age+d13*V1+e13*V1*age\n");
6786: fprintf(ficgp,"# logi(p13/p11)=p6 +p7*age +p8*age*age+ p9*V1+ p10*V1*age\n");
6787: fprintf(ficgp,"# p12+p13+p14+p11=1=p11(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
6788: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
6789: fprintf(ficgp,"# p11=1/(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
6790: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
6791: fprintf(ficgp,"# p12=exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)/\n");
6792: fprintf(ficgp,"# (1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
6793: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age))\n");
6794: fprintf(ficgp,"# +exp(a14+b14*age+c14age*age+d14*V1+e14*V1*age)+...)\n");
6795: fprintf(ficgp,"#\n");
1.223 brouard 6796: for(ng=1; ng<=3;ng++){ /* Number of graphics: first is logit, 2nd is probabilities, third is incidences per year*/
1.237 ! brouard 6797: fprintf(ficgp,"#Number of graphics: first is logit, 2nd is probabilities, third is incidences per year \n");
! 6798: fprintf(ficgp,"#model=%s \n",model);
1.223 brouard 6799: fprintf(ficgp,"# ng=%d\n",ng);
1.237 ! brouard 6800: fprintf(ficgp,"# jk=1 to 2^%d=%d\n",cptcoveff,m);/* to be checked */
! 6801: for(jk=1; jk <=m; jk++) /* For each combination of covariate */
! 6802: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
! 6803: if(TKresult[nres]!= jk)
! 6804: continue;
! 6805: fprintf(ficgp,"# Combination of dummy jk=%d and ",jk);
! 6806: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
! 6807: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
! 6808: }
! 6809: fprintf(ficgp,"\n#\n");
1.223 brouard 6810: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" ",subdirf2(optionfilefiname,"PE_"),jk,ng);
6811: fprintf(ficgp,"\nset ter svg size 640, 480 ");
6812: if (ng==1){
6813: fprintf(ficgp,"\nset ylabel \"Value of the logit of the model\"\n"); /* exp(a12+b12*x) could be nice */
6814: fprintf(ficgp,"\nunset log y");
6815: }else if (ng==2){
6816: fprintf(ficgp,"\nset ylabel \"Probability\"\n");
6817: fprintf(ficgp,"\nset log y");
6818: }else if (ng==3){
6819: fprintf(ficgp,"\nset ylabel \"Quasi-incidence per year\"\n");
6820: fprintf(ficgp,"\nset log y");
6821: }else
6822: fprintf(ficgp,"\nunset title ");
6823: fprintf(ficgp,"\nplot [%.f:%.f] ",ageminpar,agemaxpar);
6824: i=1;
6825: for(k2=1; k2<=nlstate; k2++) {
6826: k3=i;
6827: for(k=1; k<=(nlstate+ndeath); k++) {
6828: if (k != k2){
6829: switch( ng) {
6830: case 1:
6831: if(nagesqr==0)
6832: fprintf(ficgp," p%d+p%d*x",i,i+1);
6833: else /* nagesqr =1 */
6834: fprintf(ficgp," p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
6835: break;
6836: case 2: /* ng=2 */
6837: if(nagesqr==0)
6838: fprintf(ficgp," exp(p%d+p%d*x",i,i+1);
6839: else /* nagesqr =1 */
6840: fprintf(ficgp," exp(p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
6841: break;
6842: case 3:
6843: if(nagesqr==0)
6844: fprintf(ficgp," %f*exp(p%d+p%d*x",YEARM/stepm,i,i+1);
6845: else /* nagesqr =1 */
6846: fprintf(ficgp," %f*exp(p%d+p%d*x+p%d*x*x",YEARM/stepm,i,i+1,i+1+nagesqr);
6847: break;
6848: }
6849: ij=1;/* To be checked else nbcode[0][0] wrong */
1.237 ! brouard 6850: ijp=1; /* product no age */
! 6851: /* for(j=3; j <=ncovmodel-nagesqr; j++) { */
! 6852: for(j=1; j <=cptcovt; j++) { /* For each covariate of the simplified model */
1.223 brouard 6853: /* printf("Tage[%d]=%d, j=%d\n", ij, Tage[ij], j); */
1.237 ! brouard 6854: if(j==Tage[ij]) { /* Product by age */
! 6855: if(ij <=cptcovage) { /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, 2 V5 and V1 */
! 6856: if(Dummy[j]==0){
! 6857: fprintf(ficgp,"+p%d*%d*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]);;
! 6858: }else{ /* quantitative */
! 6859: fprintf(ficgp,"+p%d*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /* Tqinvresult in decoderesult */
! 6860: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(jk,Tvar[j-2])]); */
! 6861: }
! 6862: ij++;
! 6863: }
! 6864: }else if(j==Tprod[ijp]) { /* */
! 6865: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
! 6866: if(ijp <=cptcovprod) { /* Product */
! 6867: if(Dummy[Tvard[ijp][1]]==0){/* Vn is dummy */
! 6868: if(Dummy[Tvard[ijp][2]]==0){/* Vn and Vm are dummy */
! 6869: /* fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(jk,j)],nbcode[Tvard[ijp][2]][codtabm(jk,j)]); */
! 6870: fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]);
! 6871: }else{ /* Vn is dummy and Vm is quanti */
! 6872: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(jk,j)],Tqinvresult[nres][Tvard[ijp][2]]); */
! 6873: fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
! 6874: }
! 6875: }else{ /* Vn*Vm Vn is quanti */
! 6876: if(Dummy[Tvard[ijp][2]]==0){
! 6877: fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]);
! 6878: }else{ /* Both quanti */
! 6879: fprintf(ficgp,"+p%d*%f*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
! 6880: }
! 6881: }
! 6882: }
! 6883: } else{ /* simple covariate */
! 6884: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,nbcode[Tvar[j]][codtabm(jk,j)]); /\* Valgrind bug nbcode *\/ */
! 6885: if(Dummy[j]==0){
! 6886: fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]); /* */
! 6887: }else{ /* quantitative */
! 6888: fprintf(ficgp,"+p%d*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /* */
1.223 brouard 6889: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(jk,Tvar[j-2])]); */
6890: }
1.237 ! brouard 6891: } /* end simple */
! 6892: } /* end j */
1.223 brouard 6893: }else{
6894: i=i-ncovmodel;
6895: if(ng !=1 ) /* For logit formula of log p11 is more difficult to get */
6896: fprintf(ficgp," (1.");
6897: }
1.227 brouard 6898:
1.223 brouard 6899: if(ng != 1){
6900: fprintf(ficgp,")/(1");
1.227 brouard 6901:
1.223 brouard 6902: for(k1=1; k1 <=nlstate; k1++){
6903: if(nagesqr==0)
6904: fprintf(ficgp,"+exp(p%d+p%d*x",k3+(k1-1)*ncovmodel,k3+(k1-1)*ncovmodel+1);
6905: else /* nagesqr =1 */
6906: fprintf(ficgp,"+exp(p%d+p%d*x+p%d*x*x",k3+(k1-1)*ncovmodel,k3+(k1-1)*ncovmodel+1,k3+(k1-1)*ncovmodel+1+nagesqr);
1.217 brouard 6907:
1.223 brouard 6908: ij=1;
6909: for(j=3; j <=ncovmodel-nagesqr; j++){
1.237 ! brouard 6910: if((j-2)==Tage[ij]) { /* Bug valgrind */
! 6911: if(ij <=cptcovage) { /* Bug valgrind */
1.223 brouard 6912: fprintf(ficgp,"+p%d*%d*x",k3+(k1-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(jk,j-2)]);
6913: /* fprintf(ficgp,"+p%d*%d*x",k3+(k1-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(jk,Tvar[j-2])]); */
6914: ij++;
6915: }
6916: }
6917: else
1.225 brouard 6918: fprintf(ficgp,"+p%d*%d",k3+(k1-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(jk,j-2)]);/* Valgrind bug nbcode */
1.223 brouard 6919: }
6920: fprintf(ficgp,")");
6921: }
6922: fprintf(ficgp,")");
6923: if(ng ==2)
6924: fprintf(ficgp," t \"p%d%d\" ", k2,k);
6925: else /* ng= 3 */
6926: fprintf(ficgp," t \"i%d%d\" ", k2,k);
6927: }else{ /* end ng <> 1 */
6928: if( k !=k2) /* logit p11 is hard to draw */
6929: fprintf(ficgp," t \"logit(p%d%d)\" ", k2,k);
6930: }
6931: if ((k+k2)!= (nlstate*2+ndeath) && ng != 1)
6932: fprintf(ficgp,",");
6933: if (ng == 1 && k!=k2 && (k+k2)!= (nlstate*2+ndeath))
6934: fprintf(ficgp,",");
6935: i=i+ncovmodel;
6936: } /* end k */
6937: } /* end k2 */
6938: fprintf(ficgp,"\n set out\n");
6939: } /* end jk */
6940: } /* end ng */
6941: /* avoid: */
6942: fflush(ficgp);
1.126 brouard 6943: } /* end gnuplot */
6944:
6945:
6946: /*************** Moving average **************/
1.219 brouard 6947: /* int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav, double bageout, double fageout){ */
1.222 brouard 6948: int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav){
1.218 brouard 6949:
1.222 brouard 6950: int i, cpt, cptcod;
6951: int modcovmax =1;
6952: int mobilavrange, mob;
6953: int iage=0;
6954:
6955: double sum=0.;
6956: double age;
6957: double *sumnewp, *sumnewm;
6958: double *agemingood, *agemaxgood; /* Currently identical for all covariates */
6959:
6960:
1.225 brouard 6961: /* modcovmax=2*cptcoveff;/\* Max number of modalities. We suppose */
1.222 brouard 6962: /* a covariate has 2 modalities, should be equal to ncovcombmax *\/ */
6963:
6964: sumnewp = vector(1,ncovcombmax);
6965: sumnewm = vector(1,ncovcombmax);
6966: agemingood = vector(1,ncovcombmax);
6967: agemaxgood = vector(1,ncovcombmax);
6968:
6969: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
6970: sumnewm[cptcod]=0.;
6971: sumnewp[cptcod]=0.;
6972: agemingood[cptcod]=0;
6973: agemaxgood[cptcod]=0;
6974: }
6975: if (cptcovn<1) ncovcombmax=1; /* At least 1 pass */
6976:
6977: if(mobilav==1||mobilav ==3 ||mobilav==5 ||mobilav== 7){
6978: if(mobilav==1) mobilavrange=5; /* default */
6979: else mobilavrange=mobilav;
6980: for (age=bage; age<=fage; age++)
6981: for (i=1; i<=nlstate;i++)
6982: for (cptcod=1;cptcod<=ncovcombmax;cptcod++)
6983: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
6984: /* We keep the original values on the extreme ages bage, fage and for
6985: fage+1 and bage-1 we use a 3 terms moving average; for fage+2 bage+2
6986: we use a 5 terms etc. until the borders are no more concerned.
6987: */
6988: for (mob=3;mob <=mobilavrange;mob=mob+2){
6989: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){
6990: for (i=1; i<=nlstate;i++){
6991: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
6992: mobaverage[(int)age][i][cptcod] =probs[(int)age][i][cptcod];
6993: for (cpt=1;cpt<=(mob-1)/2;cpt++){
6994: mobaverage[(int)age][i][cptcod] +=probs[(int)age-cpt][i][cptcod];
6995: mobaverage[(int)age][i][cptcod] +=probs[(int)age+cpt][i][cptcod];
6996: }
6997: mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/mob;
6998: }
6999: }
7000: }/* end age */
7001: }/* end mob */
7002: }else
7003: return -1;
7004: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
7005: /* for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ */
7006: if(invalidvarcomb[cptcod]){
7007: printf("\nCombination (%d) ignored because no cases \n",cptcod);
7008: continue;
7009: }
1.219 brouard 7010:
1.222 brouard 7011: agemingood[cptcod]=fage-(mob-1)/2;
7012: for (age=fage-(mob-1)/2; age>=bage; age--){/* From oldest to youngest, finding the youngest wrong */
7013: sumnewm[cptcod]=0.;
7014: for (i=1; i<=nlstate;i++){
7015: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
7016: }
7017: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
7018: agemingood[cptcod]=age;
7019: }else{ /* bad */
7020: for (i=1; i<=nlstate;i++){
7021: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod];
7022: } /* i */
7023: } /* end bad */
7024: }/* age */
7025: sum=0.;
7026: for (i=1; i<=nlstate;i++){
7027: sum+=mobaverage[(int)agemingood[cptcod]][i][cptcod];
7028: }
7029: if(fabs(sum - 1.) > 1.e-3) { /* bad */
7030: printf("For this combination of covariate cptcod=%d, we can't get a smoothed prevalence which sums to one at any descending age!\n",cptcod);
7031: /* for (i=1; i<=nlstate;i++){ */
7032: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod]; */
7033: /* } /\* i *\/ */
7034: } /* end bad */
7035: /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */
7036: /* From youngest, finding the oldest wrong */
7037: agemaxgood[cptcod]=bage+(mob-1)/2;
7038: for (age=bage+(mob-1)/2; age<=fage; age++){
7039: sumnewm[cptcod]=0.;
7040: for (i=1; i<=nlstate;i++){
7041: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
7042: }
7043: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
7044: agemaxgood[cptcod]=age;
7045: }else{ /* bad */
7046: for (i=1; i<=nlstate;i++){
7047: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
7048: } /* i */
7049: } /* end bad */
7050: }/* age */
7051: sum=0.;
7052: for (i=1; i<=nlstate;i++){
7053: sum+=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
7054: }
7055: if(fabs(sum - 1.) > 1.e-3) { /* bad */
7056: printf("For this combination of covariate cptcod=%d, we can't get a smoothed prevalence which sums to one at any ascending age!\n",cptcod);
7057: /* for (i=1; i<=nlstate;i++){ */
7058: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod]; */
7059: /* } /\* i *\/ */
7060: } /* end bad */
7061:
7062: for (age=bage; age<=fage; age++){
1.235 brouard 7063: /* printf("%d %d ", cptcod, (int)age); */
1.222 brouard 7064: sumnewp[cptcod]=0.;
7065: sumnewm[cptcod]=0.;
7066: for (i=1; i<=nlstate;i++){
7067: sumnewp[cptcod]+=probs[(int)age][i][cptcod];
7068: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
7069: /* printf("%.4f %.4f ",probs[(int)age][i][cptcod], mobaverage[(int)age][i][cptcod]); */
7070: }
7071: /* printf("%.4f %.4f \n",sumnewp[cptcod], sumnewm[cptcod]); */
7072: }
7073: /* printf("\n"); */
7074: /* } */
7075: /* brutal averaging */
7076: for (i=1; i<=nlstate;i++){
7077: for (age=1; age<=bage; age++){
7078: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod];
7079: /* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); */
7080: }
7081: for (age=fage; age<=AGESUP; age++){
7082: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
7083: /* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); */
7084: }
7085: } /* end i status */
7086: for (i=nlstate+1; i<=nlstate+ndeath;i++){
7087: for (age=1; age<=AGESUP; age++){
7088: /*printf("i=%d, age=%d, cptcod=%d\n",i, (int)age, cptcod);*/
7089: mobaverage[(int)age][i][cptcod]=0.;
7090: }
7091: }
7092: }/* end cptcod */
7093: free_vector(sumnewm,1, ncovcombmax);
7094: free_vector(sumnewp,1, ncovcombmax);
7095: free_vector(agemaxgood,1, ncovcombmax);
7096: free_vector(agemingood,1, ncovcombmax);
7097: return 0;
7098: }/* End movingaverage */
1.218 brouard 7099:
1.126 brouard 7100:
7101: /************** Forecasting ******************/
1.235 brouard 7102: void 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){
1.126 brouard 7103: /* proj1, year, month, day of starting projection
7104: agemin, agemax range of age
7105: dateprev1 dateprev2 range of dates during which prevalence is computed
7106: anproj2 year of en of projection (same day and month as proj1).
7107: */
1.235 brouard 7108: int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1, k4, nres=0;
1.126 brouard 7109: double agec; /* generic age */
7110: double agelim, ppij, yp,yp1,yp2,jprojmean,mprojmean,anprojmean;
7111: double *popeffectif,*popcount;
7112: double ***p3mat;
1.218 brouard 7113: /* double ***mobaverage; */
1.126 brouard 7114: char fileresf[FILENAMELENGTH];
7115:
7116: agelim=AGESUP;
1.211 brouard 7117: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
7118: in each health status at the date of interview (if between dateprev1 and dateprev2).
7119: We still use firstpass and lastpass as another selection.
7120: */
1.214 brouard 7121: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ */
7122: /* firstpass, lastpass, stepm, weightopt, model); */
1.126 brouard 7123:
1.201 brouard 7124: strcpy(fileresf,"F_");
7125: strcat(fileresf,fileresu);
1.126 brouard 7126: if((ficresf=fopen(fileresf,"w"))==NULL) {
7127: printf("Problem with forecast resultfile: %s\n", fileresf);
7128: fprintf(ficlog,"Problem with forecast resultfile: %s\n", fileresf);
7129: }
1.235 brouard 7130: printf("\nComputing forecasting: result on file '%s', please wait... \n", fileresf);
7131: fprintf(ficlog,"\nComputing forecasting: result on file '%s', please wait... \n", fileresf);
1.126 brouard 7132:
1.225 brouard 7133: if (cptcoveff==0) ncodemax[cptcoveff]=1;
1.126 brouard 7134:
7135:
7136: stepsize=(int) (stepm+YEARM-1)/YEARM;
7137: if (stepm<=12) stepsize=1;
7138: if(estepm < stepm){
7139: printf ("Problem %d lower than %d\n",estepm, stepm);
7140: }
7141: else hstepm=estepm;
7142:
7143: hstepm=hstepm/stepm;
7144: yp1=modf(dateintmean,&yp);/* extracts integral of datemean in yp and
7145: fractional in yp1 */
7146: anprojmean=yp;
7147: yp2=modf((yp1*12),&yp);
7148: mprojmean=yp;
7149: yp1=modf((yp2*30.5),&yp);
7150: jprojmean=yp;
7151: if(jprojmean==0) jprojmean=1;
7152: if(mprojmean==0) jprojmean=1;
7153:
1.227 brouard 7154: i1=pow(2,cptcoveff);
1.126 brouard 7155: if (cptcovn < 1){i1=1;}
7156:
7157: fprintf(ficresf,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jprojmean,mprojmean,anprojmean,dateintmean,dateprev1,dateprev2);
7158:
7159: fprintf(ficresf,"#****** Routine prevforecast **\n");
1.227 brouard 7160:
1.126 brouard 7161: /* if (h==(int)(YEARM*yearp)){ */
1.235 brouard 7162: for(nres=1; nres <= nresult; nres++) /* For each resultline */
7163: for(k=1; k<=i1;k++){
7164: if(TKresult[nres]!= k)
7165: continue;
1.227 brouard 7166: if(invalidvarcomb[k]){
7167: printf("\nCombination (%d) projection ignored because no cases \n",k);
7168: continue;
7169: }
7170: fprintf(ficresf,"\n#****** hpijx=probability over h years, hp.jx is weighted by observed prev \n#");
7171: for(j=1;j<=cptcoveff;j++) {
7172: fprintf(ficresf," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
7173: }
1.235 brouard 7174: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7175: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
7176: fprintf(ficlog," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
7177: }
1.227 brouard 7178: fprintf(ficresf," yearproj age");
7179: for(j=1; j<=nlstate+ndeath;j++){
7180: for(i=1; i<=nlstate;i++)
7181: fprintf(ficresf," p%d%d",i,j);
7182: fprintf(ficresf," wp.%d",j);
7183: }
7184: for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) {
7185: fprintf(ficresf,"\n");
7186: fprintf(ficresf,"\n# Forecasting at date %.lf/%.lf/%.lf ",jproj1,mproj1,anproj1+yearp);
7187: for (agec=fage; agec>=(ageminpar-1); agec--){
7188: nhstepm=(int) rint((agelim-agec)*YEARM/stepm);
7189: nhstepm = nhstepm/hstepm;
7190: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
7191: oldm=oldms;savm=savms;
1.235 brouard 7192: hpxij(p3mat,nhstepm,agec,hstepm,p,nlstate,stepm,oldm,savm, k,nres);
1.227 brouard 7193:
7194: for (h=0; h<=nhstepm; h++){
7195: if (h*hstepm/YEARM*stepm ==yearp) {
7196: fprintf(ficresf,"\n");
7197: for(j=1;j<=cptcoveff;j++)
7198: fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
7199: fprintf(ficresf,"%.f %.f ",anproj1+yearp,agec+h*hstepm/YEARM*stepm);
7200: }
7201: for(j=1; j<=nlstate+ndeath;j++) {
7202: ppij=0.;
7203: for(i=1; i<=nlstate;i++) {
7204: if (mobilav==1)
7205: ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][i][k];
7206: else {
7207: ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][k];
7208: }
7209: if (h*hstepm/YEARM*stepm== yearp) {
7210: fprintf(ficresf," %.3f", p3mat[i][j][h]);
7211: }
7212: } /* end i */
7213: if (h*hstepm/YEARM*stepm==yearp) {
7214: fprintf(ficresf," %.3f", ppij);
7215: }
7216: }/* end j */
7217: } /* end h */
7218: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
7219: } /* end agec */
7220: } /* end yearp */
7221: } /* end k */
1.219 brouard 7222:
1.126 brouard 7223: fclose(ficresf);
1.215 brouard 7224: printf("End of Computing forecasting \n");
7225: fprintf(ficlog,"End of Computing forecasting\n");
7226:
1.126 brouard 7227: }
7228:
1.218 brouard 7229: /* /\************** Back Forecasting ******************\/ */
1.225 brouard 7230: /* void prevbackforecast(char fileres[], double anback1, double mback1, double jback1, double ageminpar, double agemax, double dateprev1, double dateprev2, int mobilav, double bage, double fage, int firstpass, int lastpass, double anback2, double p[], int cptcoveff){ */
1.218 brouard 7231: /* /\* back1, year, month, day of starting backection */
7232: /* agemin, agemax range of age */
7233: /* dateprev1 dateprev2 range of dates during which prevalence is computed */
7234: /* anback2 year of en of backection (same day and month as back1). */
7235: /* *\/ */
7236: /* int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1; */
7237: /* double agec; /\* generic age *\/ */
7238: /* double agelim, ppij, yp,yp1,yp2,jprojmean,mprojmean,anprojmean; */
7239: /* double *popeffectif,*popcount; */
7240: /* double ***p3mat; */
7241: /* /\* double ***mobaverage; *\/ */
7242: /* char fileresfb[FILENAMELENGTH]; */
7243:
7244: /* agelim=AGESUP; */
7245: /* /\* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people */
7246: /* in each health status at the date of interview (if between dateprev1 and dateprev2). */
7247: /* We still use firstpass and lastpass as another selection. */
7248: /* *\/ */
7249: /* /\* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ *\/ */
7250: /* /\* firstpass, lastpass, stepm, weightopt, model); *\/ */
7251: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
7252:
7253: /* strcpy(fileresfb,"FB_"); */
7254: /* strcat(fileresfb,fileresu); */
7255: /* if((ficresfb=fopen(fileresfb,"w"))==NULL) { */
7256: /* printf("Problem with back forecast resultfile: %s\n", fileresfb); */
7257: /* fprintf(ficlog,"Problem with back forecast resultfile: %s\n", fileresfb); */
7258: /* } */
7259: /* printf("Computing back forecasting: result on file '%s', please wait... \n", fileresfb); */
7260: /* fprintf(ficlog,"Computing back forecasting: result on file '%s', please wait... \n", fileresfb); */
7261:
1.225 brouard 7262: /* if (cptcoveff==0) ncodemax[cptcoveff]=1; */
1.218 brouard 7263:
7264: /* /\* if (mobilav!=0) { *\/ */
7265: /* /\* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
7266: /* /\* if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){ *\/ */
7267: /* /\* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); *\/ */
7268: /* /\* printf(" Error in movingaverage mobilav=%d\n",mobilav); *\/ */
7269: /* /\* } *\/ */
7270: /* /\* } *\/ */
7271:
7272: /* stepsize=(int) (stepm+YEARM-1)/YEARM; */
7273: /* if (stepm<=12) stepsize=1; */
7274: /* if(estepm < stepm){ */
7275: /* printf ("Problem %d lower than %d\n",estepm, stepm); */
7276: /* } */
7277: /* else hstepm=estepm; */
7278:
7279: /* hstepm=hstepm/stepm; */
7280: /* yp1=modf(dateintmean,&yp);/\* extracts integral of datemean in yp and */
7281: /* fractional in yp1 *\/ */
7282: /* anprojmean=yp; */
7283: /* yp2=modf((yp1*12),&yp); */
7284: /* mprojmean=yp; */
7285: /* yp1=modf((yp2*30.5),&yp); */
7286: /* jprojmean=yp; */
7287: /* if(jprojmean==0) jprojmean=1; */
7288: /* if(mprojmean==0) jprojmean=1; */
7289:
1.225 brouard 7290: /* i1=cptcoveff; */
1.218 brouard 7291: /* if (cptcovn < 1){i1=1;} */
1.217 brouard 7292:
1.218 brouard 7293: /* fprintf(ficresfb,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jprojmean,mprojmean,anprojmean,dateintmean,dateprev1,dateprev2); */
1.217 brouard 7294:
1.218 brouard 7295: /* fprintf(ficresfb,"#****** Routine prevbackforecast **\n"); */
7296:
7297: /* /\* if (h==(int)(YEARM*yearp)){ *\/ */
7298: /* for(cptcov=1, k=0;cptcov<=i1;cptcov++){ */
1.225 brouard 7299: /* for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){ */
1.218 brouard 7300: /* k=k+1; */
7301: /* fprintf(ficresfb,"\n#****** hbijx=probability over h years, hp.jx is weighted by observed prev \n#"); */
1.225 brouard 7302: /* for(j=1;j<=cptcoveff;j++) { */
1.218 brouard 7303: /* fprintf(ficresfb," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */
7304: /* } */
7305: /* fprintf(ficresfb," yearbproj age"); */
7306: /* for(j=1; j<=nlstate+ndeath;j++){ */
7307: /* for(i=1; i<=nlstate;i++) */
7308: /* fprintf(ficresfb," p%d%d",i,j); */
7309: /* fprintf(ficresfb," p.%d",j); */
7310: /* } */
7311: /* for (yearp=0; yearp>=(anback2-anback1);yearp -=stepsize) { */
7312: /* /\* for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) { *\/ */
7313: /* fprintf(ficresfb,"\n"); */
7314: /* fprintf(ficresfb,"\n# Back Forecasting at date %.lf/%.lf/%.lf ",jback1,mback1,anback1+yearp); */
7315: /* for (agec=fage; agec>=(ageminpar-1); agec--){ */
7316: /* nhstepm=(int) rint((agelim-agec)*YEARM/stepm); */
7317: /* nhstepm = nhstepm/hstepm; */
7318: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
7319: /* oldm=oldms;savm=savms; */
7320: /* hbxij(p3mat,nhstepm,agec,hstepm,p,prevacurrent,nlstate,stepm,oldm,savm,oldm,savm, dnewm, doldm, dsavm, k); */
7321: /* for (h=0; h<=nhstepm; h++){ */
7322: /* if (h*hstepm/YEARM*stepm ==yearp) { */
7323: /* fprintf(ficresfb,"\n"); */
1.225 brouard 7324: /* for(j=1;j<=cptcoveff;j++) */
1.218 brouard 7325: /* fprintf(ficresfb,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */
7326: /* fprintf(ficresfb,"%.f %.f ",anback1+yearp,agec+h*hstepm/YEARM*stepm); */
7327: /* } */
7328: /* for(j=1; j<=nlstate+ndeath;j++) { */
7329: /* ppij=0.; */
7330: /* for(i=1; i<=nlstate;i++) { */
7331: /* if (mobilav==1) */
7332: /* ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][i][cptcod]; */
7333: /* else { */
7334: /* ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][cptcod]; */
7335: /* } */
7336: /* if (h*hstepm/YEARM*stepm== yearp) { */
7337: /* fprintf(ficresfb," %.3f", p3mat[i][j][h]); */
7338: /* } */
7339: /* } /\* end i *\/ */
7340: /* if (h*hstepm/YEARM*stepm==yearp) { */
7341: /* fprintf(ficresfb," %.3f", ppij); */
7342: /* } */
7343: /* }/\* end j *\/ */
7344: /* } /\* end h *\/ */
7345: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
7346: /* } /\* end agec *\/ */
7347: /* } /\* end yearp *\/ */
7348: /* } /\* end cptcod *\/ */
7349: /* } /\* end cptcov *\/ */
7350:
7351: /* /\* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
7352:
7353: /* fclose(ficresfb); */
7354: /* printf("End of Computing Back forecasting \n"); */
7355: /* fprintf(ficlog,"End of Computing Back forecasting\n"); */
1.217 brouard 7356:
1.218 brouard 7357: /* } */
1.217 brouard 7358:
1.126 brouard 7359: /************** Forecasting *****not tested NB*************/
1.227 brouard 7360: /* void populforecast(char fileres[], double anpyram,double mpyram,double jpyram,double ageminpar, double agemax,double dateprev1, double dateprev2s, int mobilav, double agedeb, double fage, int popforecast, char popfile[], double anpyram1,double p[], int i2){ */
1.126 brouard 7361:
1.227 brouard 7362: /* int cpt, stepsize, hstepm, nhstepm, j,k,c, cptcod, i,h; */
7363: /* int *popage; */
7364: /* double calagedatem, agelim, kk1, kk2; */
7365: /* double *popeffectif,*popcount; */
7366: /* double ***p3mat,***tabpop,***tabpopprev; */
7367: /* /\* double ***mobaverage; *\/ */
7368: /* char filerespop[FILENAMELENGTH]; */
1.126 brouard 7369:
1.227 brouard 7370: /* tabpop= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
7371: /* tabpopprev= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
7372: /* agelim=AGESUP; */
7373: /* calagedatem=(anpyram+mpyram/12.+jpyram/365.-dateintmean)*YEARM; */
1.126 brouard 7374:
1.227 brouard 7375: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
1.126 brouard 7376:
7377:
1.227 brouard 7378: /* strcpy(filerespop,"POP_"); */
7379: /* strcat(filerespop,fileresu); */
7380: /* if((ficrespop=fopen(filerespop,"w"))==NULL) { */
7381: /* printf("Problem with forecast resultfile: %s\n", filerespop); */
7382: /* fprintf(ficlog,"Problem with forecast resultfile: %s\n", filerespop); */
7383: /* } */
7384: /* printf("Computing forecasting: result on file '%s' \n", filerespop); */
7385: /* fprintf(ficlog,"Computing forecasting: result on file '%s' \n", filerespop); */
1.126 brouard 7386:
1.227 brouard 7387: /* if (cptcoveff==0) ncodemax[cptcoveff]=1; */
1.126 brouard 7388:
1.227 brouard 7389: /* /\* if (mobilav!=0) { *\/ */
7390: /* /\* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
7391: /* /\* if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){ *\/ */
7392: /* /\* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); *\/ */
7393: /* /\* printf(" Error in movingaverage mobilav=%d\n",mobilav); *\/ */
7394: /* /\* } *\/ */
7395: /* /\* } *\/ */
1.126 brouard 7396:
1.227 brouard 7397: /* stepsize=(int) (stepm+YEARM-1)/YEARM; */
7398: /* if (stepm<=12) stepsize=1; */
1.126 brouard 7399:
1.227 brouard 7400: /* agelim=AGESUP; */
1.126 brouard 7401:
1.227 brouard 7402: /* hstepm=1; */
7403: /* hstepm=hstepm/stepm; */
1.218 brouard 7404:
1.227 brouard 7405: /* if (popforecast==1) { */
7406: /* if((ficpop=fopen(popfile,"r"))==NULL) { */
7407: /* printf("Problem with population file : %s\n",popfile);exit(0); */
7408: /* fprintf(ficlog,"Problem with population file : %s\n",popfile);exit(0); */
7409: /* } */
7410: /* popage=ivector(0,AGESUP); */
7411: /* popeffectif=vector(0,AGESUP); */
7412: /* popcount=vector(0,AGESUP); */
1.126 brouard 7413:
1.227 brouard 7414: /* i=1; */
7415: /* while ((c=fscanf(ficpop,"%d %lf\n",&popage[i],&popcount[i])) != EOF) i=i+1; */
1.218 brouard 7416:
1.227 brouard 7417: /* imx=i; */
7418: /* for (i=1; i<imx;i++) popeffectif[popage[i]]=popcount[i]; */
7419: /* } */
1.218 brouard 7420:
1.227 brouard 7421: /* for(cptcov=1,k=0;cptcov<=i2;cptcov++){ */
7422: /* for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){ */
7423: /* k=k+1; */
7424: /* fprintf(ficrespop,"\n#******"); */
7425: /* for(j=1;j<=cptcoveff;j++) { */
7426: /* fprintf(ficrespop," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */
7427: /* } */
7428: /* fprintf(ficrespop,"******\n"); */
7429: /* fprintf(ficrespop,"# Age"); */
7430: /* for(j=1; j<=nlstate+ndeath;j++) fprintf(ficrespop," P.%d",j); */
7431: /* if (popforecast==1) fprintf(ficrespop," [Population]"); */
1.126 brouard 7432:
1.227 brouard 7433: /* for (cpt=0; cpt<=0;cpt++) { */
7434: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */
1.126 brouard 7435:
1.227 brouard 7436: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */
7437: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */
7438: /* nhstepm = nhstepm/hstepm; */
1.126 brouard 7439:
1.227 brouard 7440: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
7441: /* oldm=oldms;savm=savms; */
7442: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
1.218 brouard 7443:
1.227 brouard 7444: /* for (h=0; h<=nhstepm; h++){ */
7445: /* if (h==(int) (calagedatem+YEARM*cpt)) { */
7446: /* fprintf(ficrespop,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */
7447: /* } */
7448: /* for(j=1; j<=nlstate+ndeath;j++) { */
7449: /* kk1=0.;kk2=0; */
7450: /* for(i=1; i<=nlstate;i++) { */
7451: /* if (mobilav==1) */
7452: /* kk1=kk1+p3mat[i][j][h]*mobaverage[(int)agedeb+1][i][cptcod]; */
7453: /* else { */
7454: /* kk1=kk1+p3mat[i][j][h]*probs[(int)(agedeb+1)][i][cptcod]; */
7455: /* } */
7456: /* } */
7457: /* if (h==(int)(calagedatem+12*cpt)){ */
7458: /* tabpop[(int)(agedeb)][j][cptcod]=kk1; */
7459: /* /\*fprintf(ficrespop," %.3f", kk1); */
7460: /* if (popforecast==1) fprintf(ficrespop," [%.f]", kk1*popeffectif[(int)agedeb+1]);*\/ */
7461: /* } */
7462: /* } */
7463: /* for(i=1; i<=nlstate;i++){ */
7464: /* kk1=0.; */
7465: /* for(j=1; j<=nlstate;j++){ */
7466: /* kk1= kk1+tabpop[(int)(agedeb)][j][cptcod]; */
7467: /* } */
7468: /* tabpopprev[(int)(agedeb)][i][cptcod]=tabpop[(int)(agedeb)][i][cptcod]/kk1*popeffectif[(int)(agedeb+(calagedatem+12*cpt)*hstepm/YEARM*stepm-1)]; */
7469: /* } */
1.218 brouard 7470:
1.227 brouard 7471: /* if (h==(int)(calagedatem+12*cpt)) */
7472: /* for(j=1; j<=nlstate;j++) */
7473: /* fprintf(ficrespop," %15.2f",tabpopprev[(int)(agedeb+1)][j][cptcod]); */
7474: /* } */
7475: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
7476: /* } */
7477: /* } */
1.218 brouard 7478:
1.227 brouard 7479: /* /\******\/ */
1.218 brouard 7480:
1.227 brouard 7481: /* for (cpt=1; cpt<=(anpyram1-anpyram);cpt++) { */
7482: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */
7483: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */
7484: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */
7485: /* nhstepm = nhstepm/hstepm; */
1.126 brouard 7486:
1.227 brouard 7487: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
7488: /* oldm=oldms;savm=savms; */
7489: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
7490: /* for (h=0; h<=nhstepm; h++){ */
7491: /* if (h==(int) (calagedatem+YEARM*cpt)) { */
7492: /* fprintf(ficresf,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */
7493: /* } */
7494: /* for(j=1; j<=nlstate+ndeath;j++) { */
7495: /* kk1=0.;kk2=0; */
7496: /* for(i=1; i<=nlstate;i++) { */
7497: /* kk1=kk1+p3mat[i][j][h]*tabpopprev[(int)agedeb+1][i][cptcod]; */
7498: /* } */
7499: /* if (h==(int)(calagedatem+12*cpt)) fprintf(ficresf," %15.2f", kk1); */
7500: /* } */
7501: /* } */
7502: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
7503: /* } */
7504: /* } */
7505: /* } */
7506: /* } */
1.218 brouard 7507:
1.227 brouard 7508: /* /\* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
1.218 brouard 7509:
1.227 brouard 7510: /* if (popforecast==1) { */
7511: /* free_ivector(popage,0,AGESUP); */
7512: /* free_vector(popeffectif,0,AGESUP); */
7513: /* free_vector(popcount,0,AGESUP); */
7514: /* } */
7515: /* free_ma3x(tabpop,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
7516: /* free_ma3x(tabpopprev,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
7517: /* fclose(ficrespop); */
7518: /* } /\* End of popforecast *\/ */
1.218 brouard 7519:
1.126 brouard 7520: int fileappend(FILE *fichier, char *optionfich)
7521: {
7522: if((fichier=fopen(optionfich,"a"))==NULL) {
7523: printf("Problem with file: %s\n", optionfich);
7524: fprintf(ficlog,"Problem with file: %s\n", optionfich);
7525: return (0);
7526: }
7527: fflush(fichier);
7528: return (1);
7529: }
7530:
7531:
7532: /**************** function prwizard **********************/
7533: void prwizard(int ncovmodel, int nlstate, int ndeath, char model[], FILE *ficparo)
7534: {
7535:
7536: /* Wizard to print covariance matrix template */
7537:
1.164 brouard 7538: char ca[32], cb[32];
7539: int i,j, k, li, lj, lk, ll, jj, npar, itimes;
1.126 brouard 7540: int numlinepar;
7541:
7542: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
7543: fprintf(ficparo,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
7544: for(i=1; i <=nlstate; i++){
7545: jj=0;
7546: for(j=1; j <=nlstate+ndeath; j++){
7547: if(j==i) continue;
7548: jj++;
7549: /*ca[0]= k+'a'-1;ca[1]='\0';*/
7550: printf("%1d%1d",i,j);
7551: fprintf(ficparo,"%1d%1d",i,j);
7552: for(k=1; k<=ncovmodel;k++){
7553: /* printf(" %lf",param[i][j][k]); */
7554: /* fprintf(ficparo," %lf",param[i][j][k]); */
7555: printf(" 0.");
7556: fprintf(ficparo," 0.");
7557: }
7558: printf("\n");
7559: fprintf(ficparo,"\n");
7560: }
7561: }
7562: printf("# Scales (for hessian or gradient estimation)\n");
7563: fprintf(ficparo,"# Scales (for hessian or gradient estimation)\n");
7564: npar= (nlstate+ndeath-1)*nlstate*ncovmodel; /* Number of parameters*/
7565: for(i=1; i <=nlstate; i++){
7566: jj=0;
7567: for(j=1; j <=nlstate+ndeath; j++){
7568: if(j==i) continue;
7569: jj++;
7570: fprintf(ficparo,"%1d%1d",i,j);
7571: printf("%1d%1d",i,j);
7572: fflush(stdout);
7573: for(k=1; k<=ncovmodel;k++){
7574: /* printf(" %le",delti3[i][j][k]); */
7575: /* fprintf(ficparo," %le",delti3[i][j][k]); */
7576: printf(" 0.");
7577: fprintf(ficparo," 0.");
7578: }
7579: numlinepar++;
7580: printf("\n");
7581: fprintf(ficparo,"\n");
7582: }
7583: }
7584: printf("# Covariance matrix\n");
7585: /* # 121 Var(a12)\n\ */
7586: /* # 122 Cov(b12,a12) Var(b12)\n\ */
7587: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
7588: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
7589: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
7590: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
7591: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
7592: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
7593: fflush(stdout);
7594: fprintf(ficparo,"# Covariance matrix\n");
7595: /* # 121 Var(a12)\n\ */
7596: /* # 122 Cov(b12,a12) Var(b12)\n\ */
7597: /* # ...\n\ */
7598: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
7599:
7600: for(itimes=1;itimes<=2;itimes++){
7601: jj=0;
7602: for(i=1; i <=nlstate; i++){
7603: for(j=1; j <=nlstate+ndeath; j++){
7604: if(j==i) continue;
7605: for(k=1; k<=ncovmodel;k++){
7606: jj++;
7607: ca[0]= k+'a'-1;ca[1]='\0';
7608: if(itimes==1){
7609: printf("#%1d%1d%d",i,j,k);
7610: fprintf(ficparo,"#%1d%1d%d",i,j,k);
7611: }else{
7612: printf("%1d%1d%d",i,j,k);
7613: fprintf(ficparo,"%1d%1d%d",i,j,k);
7614: /* printf(" %.5le",matcov[i][j]); */
7615: }
7616: ll=0;
7617: for(li=1;li <=nlstate; li++){
7618: for(lj=1;lj <=nlstate+ndeath; lj++){
7619: if(lj==li) continue;
7620: for(lk=1;lk<=ncovmodel;lk++){
7621: ll++;
7622: if(ll<=jj){
7623: cb[0]= lk +'a'-1;cb[1]='\0';
7624: if(ll<jj){
7625: if(itimes==1){
7626: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
7627: fprintf(ficparo," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
7628: }else{
7629: printf(" 0.");
7630: fprintf(ficparo," 0.");
7631: }
7632: }else{
7633: if(itimes==1){
7634: printf(" Var(%s%1d%1d)",ca,i,j);
7635: fprintf(ficparo," Var(%s%1d%1d)",ca,i,j);
7636: }else{
7637: printf(" 0.");
7638: fprintf(ficparo," 0.");
7639: }
7640: }
7641: }
7642: } /* end lk */
7643: } /* end lj */
7644: } /* end li */
7645: printf("\n");
7646: fprintf(ficparo,"\n");
7647: numlinepar++;
7648: } /* end k*/
7649: } /*end j */
7650: } /* end i */
7651: } /* end itimes */
7652:
7653: } /* end of prwizard */
7654: /******************* Gompertz Likelihood ******************************/
7655: double gompertz(double x[])
7656: {
7657: double A,B,L=0.0,sump=0.,num=0.;
7658: int i,n=0; /* n is the size of the sample */
7659:
1.220 brouard 7660: for (i=1;i<=imx ; i++) {
1.126 brouard 7661: sump=sump+weight[i];
7662: /* sump=sump+1;*/
7663: num=num+1;
7664: }
7665:
7666:
7667: /* for (i=0; i<=imx; i++)
7668: 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]);*/
7669:
7670: for (i=1;i<=imx ; i++)
7671: {
7672: if (cens[i] == 1 && wav[i]>1)
7673: A=-x[1]/(x[2])*(exp(x[2]*(agecens[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)));
7674:
7675: if (cens[i] == 0 && wav[i]>1)
7676: A=-x[1]/(x[2])*(exp(x[2]*(agedc[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)))
7677: +log(x[1]/YEARM)+x[2]*(agedc[i]-agegomp)+log(YEARM);
7678:
7679: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
7680: if (wav[i] > 1 ) { /* ??? */
7681: L=L+A*weight[i];
7682: /* 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]);*/
7683: }
7684: }
7685:
7686: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
7687:
7688: return -2*L*num/sump;
7689: }
7690:
1.136 brouard 7691: #ifdef GSL
7692: /******************* Gompertz_f Likelihood ******************************/
7693: double gompertz_f(const gsl_vector *v, void *params)
7694: {
7695: double A,B,LL=0.0,sump=0.,num=0.;
7696: double *x= (double *) v->data;
7697: int i,n=0; /* n is the size of the sample */
7698:
7699: for (i=0;i<=imx-1 ; i++) {
7700: sump=sump+weight[i];
7701: /* sump=sump+1;*/
7702: num=num+1;
7703: }
7704:
7705:
7706: /* for (i=0; i<=imx; i++)
7707: 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]);*/
7708: printf("x[0]=%lf x[1]=%lf\n",x[0],x[1]);
7709: for (i=1;i<=imx ; i++)
7710: {
7711: if (cens[i] == 1 && wav[i]>1)
7712: A=-x[0]/(x[1])*(exp(x[1]*(agecens[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)));
7713:
7714: if (cens[i] == 0 && wav[i]>1)
7715: A=-x[0]/(x[1])*(exp(x[1]*(agedc[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)))
7716: +log(x[0]/YEARM)+x[1]*(agedc[i]-agegomp)+log(YEARM);
7717:
7718: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
7719: if (wav[i] > 1 ) { /* ??? */
7720: LL=LL+A*weight[i];
7721: /* 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]);*/
7722: }
7723: }
7724:
7725: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
7726: printf("x[0]=%lf x[1]=%lf -2*LL*num/sump=%lf\n",x[0],x[1],-2*LL*num/sump);
7727:
7728: return -2*LL*num/sump;
7729: }
7730: #endif
7731:
1.126 brouard 7732: /******************* Printing html file ***********/
1.201 brouard 7733: void printinghtmlmort(char fileresu[], char title[], char datafile[], int firstpass, \
1.126 brouard 7734: int lastpass, int stepm, int weightopt, char model[],\
7735: int imx, double p[],double **matcov,double agemortsup){
7736: int i,k;
7737:
7738: fprintf(fichtm,"<ul><li><h4>Result files </h4>\n Force of mortality. Parameters of the Gompertz fit (with confidence interval in brackets):<br>");
7739: fprintf(fichtm," mu(age) =%lf*exp(%lf*(age-%d)) per year<br><br>",p[1],p[2],agegomp);
7740: for (i=1;i<=2;i++)
7741: 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]));
1.199 brouard 7742: fprintf(fichtm,"<br><br><img src=\"graphmort.svg\">");
1.126 brouard 7743: fprintf(fichtm,"</ul>");
7744:
7745: fprintf(fichtm,"<ul><li><h4>Life table</h4>\n <br>");
7746:
7747: 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>");
7748:
7749: for (k=agegomp;k<(agemortsup-2);k++)
7750: 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]);
7751:
7752:
7753: fflush(fichtm);
7754: }
7755:
7756: /******************* Gnuplot file **************/
1.201 brouard 7757: void printinggnuplotmort(char fileresu[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){
1.126 brouard 7758:
7759: char dirfileres[132],optfileres[132];
1.164 brouard 7760:
1.126 brouard 7761: int ng;
7762:
7763:
7764: /*#ifdef windows */
7765: fprintf(ficgp,"cd \"%s\" \n",pathc);
7766: /*#endif */
7767:
7768:
7769: strcpy(dirfileres,optionfilefiname);
7770: strcpy(optfileres,"vpl");
1.199 brouard 7771: fprintf(ficgp,"set out \"graphmort.svg\"\n ");
1.126 brouard 7772: fprintf(ficgp,"set xlabel \"Age\"\n set ylabel \"Force of mortality (per year)\" \n ");
1.199 brouard 7773: fprintf(ficgp, "set ter svg size 640, 480\n set log y\n");
1.145 brouard 7774: /* fprintf(ficgp, "set size 0.65,0.65\n"); */
1.126 brouard 7775: fprintf(ficgp,"plot [%d:100] %lf*exp(%lf*(x-%d))",agegomp,p[1],p[2],agegomp);
7776:
7777: }
7778:
1.136 brouard 7779: int readdata(char datafile[], int firstobs, int lastobs, int *imax)
7780: {
1.126 brouard 7781:
1.136 brouard 7782: /*-------- data file ----------*/
7783: FILE *fic;
7784: char dummy[]=" ";
1.223 brouard 7785: int i=0, j=0, n=0, iv=0;
7786: int lstra;
1.136 brouard 7787: int linei, month, year,iout;
7788: char line[MAXLINE], linetmp[MAXLINE];
1.164 brouard 7789: char stra[MAXLINE], strb[MAXLINE];
1.136 brouard 7790: char *stratrunc;
1.223 brouard 7791:
1.126 brouard 7792:
7793:
1.136 brouard 7794: if((fic=fopen(datafile,"r"))==NULL) {
1.218 brouard 7795: printf("Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(stdout);
7796: fprintf(ficlog,"Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(ficlog);return 1;
1.136 brouard 7797: }
1.126 brouard 7798:
1.136 brouard 7799: i=1;
7800: linei=0;
7801: while ((fgets(line, MAXLINE, fic) != NULL) &&((i >= firstobs) && (i <=lastobs))) {
7802: linei=linei+1;
7803: for(j=strlen(line); j>=0;j--){ /* Untabifies line */
7804: if(line[j] == '\t')
7805: line[j] = ' ';
7806: }
7807: for(j=strlen(line)-1; (line[j]==' ')||(line[j]==10)||(line[j]==13);j--){
7808: ;
7809: };
7810: line[j+1]=0; /* Trims blanks at end of line */
7811: if(line[0]=='#'){
7812: fprintf(ficlog,"Comment line\n%s\n",line);
7813: printf("Comment line\n%s\n",line);
7814: continue;
7815: }
7816: trimbb(linetmp,line); /* Trims multiple blanks in line */
1.164 brouard 7817: strcpy(line, linetmp);
1.223 brouard 7818:
7819: /* Loops on waves */
7820: for (j=maxwav;j>=1;j--){
7821: for (iv=nqtv;iv>=1;iv--){ /* Loop on time varying quantitative variables */
1.232 brouard 7822: cutv(stra, strb, line, ' ');
7823: if(strb[0]=='.') { /* Missing value */
7824: lval=-1;
7825: cotqvar[j][iv][i]=-1; /* 0.0/0.0 */
7826: cotvar[j][ntv+iv][i]=-1; /* For performance reasons */
7827: if(isalpha(strb[1])) { /* .m or .d Really Missing value */
7828: printf("Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be the %d th quantitative value out of %d measured at wave %d. If missing, you should remove this individual or impute a value. Exiting.\n", strb, linei,i,line,iv, nqtv, j);
7829: fprintf(ficlog,"Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be the %d th quantitative value out of %d measured at wave %d. If missing, you should remove this individual or impute a value. Exiting.\n", strb, linei,i,line,iv, nqtv, j);fflush(ficlog);
7830: return 1;
7831: }
7832: }else{
7833: errno=0;
7834: /* what_kind_of_number(strb); */
7835: dval=strtod(strb,&endptr);
7836: /* if( strb[0]=='\0' || (*endptr != '\0')){ */
7837: /* if(strb != endptr && *endptr == '\0') */
7838: /* dval=dlval; */
7839: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
7840: if( strb[0]=='\0' || (*endptr != '\0')){
7841: printf("Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be the %d th quantitative value out of %d measured at wave %d. Setting maxwav=%d might be wrong. Exiting.\n", strb, linei,i,line,iv, nqtv, j,maxwav);
7842: fprintf(ficlog,"Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be the %d th quantitative value out of %d measured at wave %d. Setting maxwav=%d might be wrong. Exiting.\n", strb, linei,i,line, iv, nqtv, j,maxwav);fflush(ficlog);
7843: return 1;
7844: }
7845: cotqvar[j][iv][i]=dval;
7846: cotvar[j][ntv+iv][i]=dval;
7847: }
7848: strcpy(line,stra);
1.223 brouard 7849: }/* end loop ntqv */
1.225 brouard 7850:
1.223 brouard 7851: for (iv=ntv;iv>=1;iv--){ /* Loop on time varying dummies */
1.232 brouard 7852: cutv(stra, strb, line, ' ');
7853: if(strb[0]=='.') { /* Missing value */
7854: lval=-1;
7855: }else{
7856: errno=0;
7857: lval=strtol(strb,&endptr,10);
7858: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
7859: if( strb[0]=='\0' || (*endptr != '\0')){
7860: printf("Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be the %d th dummy covariate out of %d measured at wave %d. Setting maxwav=%d might be wrong. Exiting.\n", strb, linei,i,line,iv, ntv, j,maxwav);
7861: fprintf(ficlog,"Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be the %d dummy covariate out of %d measured wave %d. Setting maxwav=%d might be wrong. Exiting.\n", strb, linei,i,line,iv, ntv,j,maxwav);fflush(ficlog);
7862: return 1;
7863: }
7864: }
7865: if(lval <-1 || lval >1){
7866: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.223 brouard 7867: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
7868: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.232 brouard 7869: For example, for multinomial values like 1, 2 and 3,\n \
7870: build V1=0 V2=0 for the reference value (1),\n \
7871: V1=1 V2=0 for (2) \n \
1.223 brouard 7872: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.232 brouard 7873: output of IMaCh is often meaningless.\n \
1.223 brouard 7874: Exiting.\n",lval,linei, i,line,j);
1.232 brouard 7875: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.223 brouard 7876: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
7877: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.232 brouard 7878: For example, for multinomial values like 1, 2 and 3,\n \
7879: build V1=0 V2=0 for the reference value (1),\n \
7880: V1=1 V2=0 for (2) \n \
1.223 brouard 7881: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.232 brouard 7882: output of IMaCh is often meaningless.\n \
1.223 brouard 7883: Exiting.\n",lval,linei, i,line,j);fflush(ficlog);
1.232 brouard 7884: return 1;
7885: }
7886: cotvar[j][iv][i]=(double)(lval);
7887: strcpy(line,stra);
1.223 brouard 7888: }/* end loop ntv */
1.225 brouard 7889:
1.223 brouard 7890: /* Statuses at wave */
1.137 brouard 7891: cutv(stra, strb, line, ' ');
1.223 brouard 7892: if(strb[0]=='.') { /* Missing value */
1.232 brouard 7893: lval=-1;
1.136 brouard 7894: }else{
1.232 brouard 7895: errno=0;
7896: lval=strtol(strb,&endptr,10);
7897: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
7898: if( strb[0]=='\0' || (*endptr != '\0')){
7899: 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);
7900: 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);
7901: return 1;
7902: }
1.136 brouard 7903: }
1.225 brouard 7904:
1.136 brouard 7905: s[j][i]=lval;
1.225 brouard 7906:
1.223 brouard 7907: /* Date of Interview */
1.136 brouard 7908: strcpy(line,stra);
7909: cutv(stra, strb,line,' ');
1.169 brouard 7910: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 7911: }
1.169 brouard 7912: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
1.225 brouard 7913: month=99;
7914: year=9999;
1.136 brouard 7915: }else{
1.225 brouard 7916: 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);
7917: 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);
7918: return 1;
1.136 brouard 7919: }
7920: anint[j][i]= (double) year;
7921: mint[j][i]= (double)month;
7922: strcpy(line,stra);
1.223 brouard 7923: } /* End loop on waves */
1.225 brouard 7924:
1.223 brouard 7925: /* Date of death */
1.136 brouard 7926: cutv(stra, strb,line,' ');
1.169 brouard 7927: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 7928: }
1.169 brouard 7929: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
1.136 brouard 7930: month=99;
7931: year=9999;
7932: }else{
1.141 brouard 7933: 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);
1.225 brouard 7934: 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);
7935: return 1;
1.136 brouard 7936: }
7937: andc[i]=(double) year;
7938: moisdc[i]=(double) month;
7939: strcpy(line,stra);
7940:
1.223 brouard 7941: /* Date of birth */
1.136 brouard 7942: cutv(stra, strb,line,' ');
1.169 brouard 7943: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 7944: }
1.169 brouard 7945: else if( (iout=sscanf(strb,"%s.", dummy)) != 0){
1.136 brouard 7946: month=99;
7947: year=9999;
7948: }else{
1.141 brouard 7949: 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);
7950: 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.225 brouard 7951: return 1;
1.136 brouard 7952: }
7953: if (year==9999) {
1.141 brouard 7954: 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);
7955: 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.225 brouard 7956: return 1;
7957:
1.136 brouard 7958: }
7959: annais[i]=(double)(year);
7960: moisnais[i]=(double)(month);
7961: strcpy(line,stra);
1.225 brouard 7962:
1.223 brouard 7963: /* Sample weight */
1.136 brouard 7964: cutv(stra, strb,line,' ');
7965: errno=0;
7966: dval=strtod(strb,&endptr);
7967: if( strb[0]=='\0' || (*endptr != '\0')){
1.141 brouard 7968: printf("Error reading data around '%f' at line number %d, \"%s\" for individual %d\nShould be a weight. Exiting.\n",dval, i,line,linei);
7969: 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 7970: fflush(ficlog);
7971: return 1;
7972: }
7973: weight[i]=dval;
7974: strcpy(line,stra);
1.225 brouard 7975:
1.223 brouard 7976: for (iv=nqv;iv>=1;iv--){ /* Loop on fixed quantitative variables */
7977: cutv(stra, strb, line, ' ');
7978: if(strb[0]=='.') { /* Missing value */
1.225 brouard 7979: lval=-1;
1.223 brouard 7980: }else{
1.225 brouard 7981: errno=0;
7982: /* what_kind_of_number(strb); */
7983: dval=strtod(strb,&endptr);
7984: /* if(strb != endptr && *endptr == '\0') */
7985: /* dval=dlval; */
7986: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
7987: if( strb[0]=='\0' || (*endptr != '\0')){
7988: printf("Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be the %d th quantitative value (out of %d) constant for all waves. Setting maxwav=%d might be wrong. Exiting.\n", strb, linei,i,line, iv, nqv, maxwav);
7989: fprintf(ficlog,"Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be the %d th quantitative value (out of %d) constant for all waves. Setting maxwav=%d might be wrong. Exiting.\n", strb, linei,i,line, iv, nqv, maxwav);fflush(ficlog);
7990: return 1;
7991: }
7992: coqvar[iv][i]=dval;
1.226 brouard 7993: covar[ncovcol+iv][i]=dval; /* including qvar in standard covar for performance reasons */
1.223 brouard 7994: }
7995: strcpy(line,stra);
7996: }/* end loop nqv */
1.136 brouard 7997:
1.223 brouard 7998: /* Covariate values */
1.136 brouard 7999: for (j=ncovcol;j>=1;j--){
8000: cutv(stra, strb,line,' ');
1.223 brouard 8001: if(strb[0]=='.') { /* Missing covariate value */
1.225 brouard 8002: lval=-1;
1.136 brouard 8003: }else{
1.225 brouard 8004: errno=0;
8005: lval=strtol(strb,&endptr,10);
8006: if( strb[0]=='\0' || (*endptr != '\0')){
8007: 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);
8008: 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);
8009: return 1;
8010: }
1.136 brouard 8011: }
8012: if(lval <-1 || lval >1){
1.225 brouard 8013: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.136 brouard 8014: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
8015: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.225 brouard 8016: For example, for multinomial values like 1, 2 and 3,\n \
8017: build V1=0 V2=0 for the reference value (1),\n \
8018: V1=1 V2=0 for (2) \n \
1.136 brouard 8019: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.225 brouard 8020: output of IMaCh is often meaningless.\n \
1.136 brouard 8021: Exiting.\n",lval,linei, i,line,j);
1.225 brouard 8022: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.136 brouard 8023: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
8024: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.225 brouard 8025: For example, for multinomial values like 1, 2 and 3,\n \
8026: build V1=0 V2=0 for the reference value (1),\n \
8027: V1=1 V2=0 for (2) \n \
1.136 brouard 8028: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.225 brouard 8029: output of IMaCh is often meaningless.\n \
1.136 brouard 8030: Exiting.\n",lval,linei, i,line,j);fflush(ficlog);
1.225 brouard 8031: return 1;
1.136 brouard 8032: }
8033: covar[j][i]=(double)(lval);
8034: strcpy(line,stra);
8035: }
8036: lstra=strlen(stra);
1.225 brouard 8037:
1.136 brouard 8038: if(lstra > 9){ /* More than 2**32 or max of what printf can write with %ld */
8039: stratrunc = &(stra[lstra-9]);
8040: num[i]=atol(stratrunc);
8041: }
8042: else
8043: num[i]=atol(stra);
8044: /*if((s[2][i]==2) && (s[3][i]==-1)&&(s[4][i]==9)){
8045: 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;}*/
8046:
8047: i=i+1;
8048: } /* End loop reading data */
1.225 brouard 8049:
1.136 brouard 8050: *imax=i-1; /* Number of individuals */
8051: fclose(fic);
1.225 brouard 8052:
1.136 brouard 8053: return (0);
1.164 brouard 8054: /* endread: */
1.225 brouard 8055: printf("Exiting readdata: ");
8056: fclose(fic);
8057: return (1);
1.223 brouard 8058: }
1.126 brouard 8059:
1.234 brouard 8060: void removefirstspace(char **stri){/*, char stro[]) {*/
1.230 brouard 8061: char *p1 = *stri, *p2 = *stri;
1.235 brouard 8062: while (*p2 == ' ')
1.234 brouard 8063: p2++;
8064: /* while ((*p1++ = *p2++) !=0) */
8065: /* ; */
8066: /* do */
8067: /* while (*p2 == ' ') */
8068: /* p2++; */
8069: /* while (*p1++ == *p2++); */
8070: *stri=p2;
1.145 brouard 8071: }
8072:
1.235 brouard 8073: int decoderesult ( char resultline[], int nres)
1.230 brouard 8074: /**< This routine decode one result line and returns the combination # of dummy covariates only **/
8075: {
1.235 brouard 8076: int j=0, k=0, k1=0, k2=0, k3=0, k4=0, match=0, k2q=0, k3q=0, k4q=0;
1.230 brouard 8077: char resultsav[MAXLINE];
1.234 brouard 8078: int resultmodel[MAXLINE];
8079: int modelresult[MAXLINE];
1.230 brouard 8080: char stra[80], strb[80], strc[80], strd[80],stre[80];
8081:
1.234 brouard 8082: removefirstspace(&resultline);
1.233 brouard 8083: printf("decoderesult:%s\n",resultline);
1.230 brouard 8084:
8085: if (strstr(resultline,"v") !=0){
8086: printf("Error. 'v' must be in upper case 'V' result: %s ",resultline);
8087: fprintf(ficlog,"Error. 'v' must be in upper case result: %s ",resultline);fflush(ficlog);
8088: return 1;
8089: }
8090: trimbb(resultsav, resultline);
8091: if (strlen(resultsav) >1){
8092: j=nbocc(resultsav,'='); /**< j=Number of covariate values'=' */
8093: }
1.234 brouard 8094: if( j != cptcovs ){ /* Be careful if a variable is in a product but not single */
8095: printf("ERROR: the number of variable in the resultline, %d, differs from the number of variable used in the model line, %d.\n",j, cptcovs);
8096: fprintf(ficlog,"ERROR: the number of variable in the resultline, %d, differs from the number of variable used in the model line, %d.\n",j, cptcovs);
8097: }
8098: for(k=1; k<=j;k++){ /* Loop on any covariate of the result line */
8099: if(nbocc(resultsav,'=') >1){
8100: cutl(stra,strb,resultsav,' '); /* keeps in strb after the first ' '
8101: resultsav= V4=1 V5=25.1 V3=0 strb=V3=0 stra= V4=1 V5=25.1 */
8102: cutl(strc,strd,strb,'='); /* strb:V4=1 strc=1 strd=V4 */
8103: }else
8104: cutl(strc,strd,resultsav,'=');
1.230 brouard 8105: Tvalsel[k]=atof(strc); /* 1 */
1.234 brouard 8106:
1.230 brouard 8107: cutl(strc,stre,strd,'V'); /* strd='V4' strc=4 stre='V' */;
8108: Tvarsel[k]=atoi(strc);
8109: /* Typevarsel[k]=1; /\* 1 for age product *\/ */
8110: /* cptcovsel++; */
8111: if (nbocc(stra,'=') >0)
8112: strcpy(resultsav,stra); /* and analyzes it */
8113: }
1.235 brouard 8114: /* Checking for missing or useless values in comparison of current model needs */
1.236 brouard 8115: for(k1=1; k1<= cptcovt ;k1++){ /* model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
8116: if(Typevar[k1]==0){ /* Single covariate in model */
1.234 brouard 8117: match=0;
1.236 brouard 8118: for(k2=1; k2 <=j;k2++){/* result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
1.237 ! brouard 8119: if(Tvar[k1]==Tvarsel[k2]) {/* Tvar[1]=5 == Tvarsel[2]=5 */
1.236 brouard 8120: modelresult[k2]=k1;/* modelresult[2]=1 modelresult[1]=2 modelresult[3]=3 modelresult[6]=4 modelresult[9]=5 */
1.234 brouard 8121: match=1;
8122: break;
8123: }
8124: }
8125: if(match == 0){
8126: printf("Error in result line: %d value missing; result: %s, model=%s\n",k1, resultline, model);
8127: }
8128: }
8129: }
1.235 brouard 8130: /* Checking for missing or useless values in comparison of current model needs */
8131: for(k2=1; k2 <=j;k2++){ /* result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
1.234 brouard 8132: match=0;
1.235 brouard 8133: for(k1=1; k1<= cptcovt ;k1++){ /* model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
8134: if(Typevar[k1]==0){ /* Single */
1.237 ! brouard 8135: if(Tvar[k1]==Tvarsel[k2]) { /* Tvar[2]=4 == Tvarsel[1]=4 */
1.235 brouard 8136: resultmodel[k1]=k2; /* resultmodel[2]=1 resultmodel[1]=2 resultmodel[3]=3 resultmodel[6]=4 resultmodel[9]=5 */
1.234 brouard 8137: ++match;
8138: }
8139: }
8140: }
8141: if(match == 0){
8142: printf("Error in result line: %d value missing; result: %s, model=%s\n",k1, resultline, model);
8143: }else if(match > 1){
8144: printf("Error in result line: %d doubled; result: %s, model=%s\n",k2, resultline, model);
8145: }
8146: }
1.235 brouard 8147:
1.234 brouard 8148: /* We need to deduce which combination number is chosen and save quantitative values */
1.235 brouard 8149: /* model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
8150: /* result line V4=1 V5=25.1 V3=0 V2=8 V1=1 */
8151: /* should give a combination of dummy V4=1, V3=0, V1=1 => V4*2**(0) + V3*2**(1) + V1*2**(2) = 5 + (1offset) = 6*/
8152: /* result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
8153: /* should give a combination of dummy V4=1, V3=1, V1=0 => V4*2**(0) + V3*2**(1) + V1*2**(2) = 3 + (1offset) = 4*/
8154: /* 1 0 0 0 */
8155: /* 2 1 0 0 */
8156: /* 3 0 1 0 */
8157: /* 4 1 1 0 */ /* V4=1, V3=1, V1=0 */
8158: /* 5 0 0 1 */
8159: /* 6 1 0 1 */ /* V4=1, V3=0, V1=1 */
8160: /* 7 0 1 1 */
8161: /* 8 1 1 1 */
1.237 ! brouard 8162: /* V(Tvresult)=Tresult V4=1 V3=0 V1=1 Tresult[nres=1][2]=0 */
! 8163: /* V(Tvqresult)=Tqresult V5=25.1 V2=8 Tqresult[nres=1][1]=25.1 */
! 8164: /* V5*age V5 known which value for nres? */
! 8165: /* Tqinvresult[2]=8 Tqinvresult[1]=25.1 */
1.235 brouard 8166: for(k1=1, k=0, k4=0, k4q=0; k1 <=cptcovt;k1++){ /* model line */
8167: if( Dummy[k1]==0 && Typevar[k1]==0 ){ /* Single dummy */
1.237 ! brouard 8168: k3= resultmodel[k1]; /* resultmodel[2(V4)] = 1=k3 */
1.235 brouard 8169: k2=(int)Tvarsel[k3]; /* Tvarsel[resultmodel[2]]= Tvarsel[1] = 4=k2 */
8170: k+=Tvalsel[k3]*pow(2,k4); /* Tvalsel[1]=1 */
1.237 ! brouard 8171: Tresult[nres][k4+1]=Tvalsel[k3];/* Tresult[nres][1]=1(V4=1) Tresult[nres][2]=0(V3=0) */
! 8172: Tvresult[nres][k4+1]=(int)Tvarsel[k3];/* Tvresult[nres][1]=4 Tvresult[nres][3]=1 */
! 8173: Tinvresult[nres][(int)Tvarsel[k3]]=Tvalsel[k3]; /* Tinvresult[nres][4]=1 */
1.235 brouard 8174: printf("Decoderesult Dummy k=%d, V(k2=V%d)= Tvalsel[%d]=%d, 2**(%d)\n",k, k2, k3, (int)Tvalsel[k3], k4);
8175: k4++;;
8176: } else if( Dummy[k1]==1 && Typevar[k1]==0 ){ /* Single quantitative */
8177: k3q= resultmodel[k1]; /* resultmodel[2] = 1=k3 */
8178: k2q=(int)Tvarsel[k3q]; /* Tvarsel[resultmodel[2]]= Tvarsel[1] = 4=k2 */
1.237 ! brouard 8179: Tqresult[nres][k4q+1]=Tvalsel[k3q]; /* Tqresult[nres][1]=25.1 */
! 8180: Tvqresult[nres][k4q+1]=(int)Tvarsel[k3q]; /* Tvqresult[nres][1]=5 */
! 8181: Tqinvresult[nres][(int)Tvarsel[k3q]]=Tvalsel[k3q]; /* Tqinvresult[nres][5]=25.1 */
1.235 brouard 8182: printf("Decoderesult Quantitative nres=%d, V(k2q=V%d)= Tvalsel[%d]=%d, Tvarsel[%d]=%f\n",nres, k2q, k3q, Tvarsel[k3q], k3q, Tvalsel[k3q]);
8183: k4q++;;
8184: }
8185: }
1.234 brouard 8186:
1.235 brouard 8187: TKresult[nres]=++k; /* Combination for the nresult and the model */
1.230 brouard 8188: return (0);
8189: }
1.235 brouard 8190:
1.230 brouard 8191: int decodemodel( char model[], int lastobs)
8192: /**< This routine decodes the model and returns:
1.224 brouard 8193: * Model V1+V2+V3+V8+V7*V8+V5*V6+V8*age+V3*age+age*age
8194: * - nagesqr = 1 if age*age in the model, otherwise 0.
8195: * - cptcovt total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age
8196: * - cptcovn or number of covariates k of the models excluding age*products =6 and age*age
8197: * - cptcovage number of covariates with age*products =2
8198: * - cptcovs number of simple covariates
8199: * - 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
8200: * which is a new column after the 9 (ncovcol) variables.
8201: * - if k is a product Vn*Vm covar[k][i] is filled with correct values for each individual
8202: * - Tprod[l] gives the kth covariates of the product Vn*Vm l=1 to cptcovprod-cptcovage
8203: * Tprod[1]@2 {5, 6}: position of first product V7*V8 is 5, and second V5*V6 is 6.
8204: * - Tvard[k] p Tvard[1][1]@4 {7, 8, 5, 6} for V7*V8 and V5*V6 .
8205: */
1.136 brouard 8206: {
1.145 brouard 8207: int i, j, k, ks;
1.227 brouard 8208: int j1, k1, k2, k3, k4;
1.136 brouard 8209: char modelsav[80];
1.145 brouard 8210: char stra[80], strb[80], strc[80], strd[80],stre[80];
1.187 brouard 8211: char *strpt;
1.136 brouard 8212:
1.145 brouard 8213: /*removespace(model);*/
1.136 brouard 8214: if (strlen(model) >1){ /* If there is at least 1 covariate */
1.145 brouard 8215: j=0, j1=0, k1=0, k2=-1, ks=0, cptcovn=0;
1.137 brouard 8216: if (strstr(model,"AGE") !=0){
1.192 brouard 8217: printf("Error. AGE must be in lower case 'age' model=1+age+%s. ",model);
8218: fprintf(ficlog,"Error. AGE must be in lower case model=1+age+%s. ",model);fflush(ficlog);
1.136 brouard 8219: return 1;
8220: }
1.141 brouard 8221: if (strstr(model,"v") !=0){
8222: printf("Error. 'v' must be in upper case 'V' model=%s ",model);
8223: fprintf(ficlog,"Error. 'v' must be in upper case model=%s ",model);fflush(ficlog);
8224: return 1;
8225: }
1.187 brouard 8226: strcpy(modelsav,model);
8227: if ((strpt=strstr(model,"age*age")) !=0){
8228: printf(" strpt=%s, model=%s\n",strpt, model);
8229: if(strpt != model){
1.234 brouard 8230: printf("Error in model: 'model=%s'; 'age*age' should in first place before other covariates\n \
1.192 brouard 8231: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
1.187 brouard 8232: corresponding column of parameters.\n",model);
1.234 brouard 8233: fprintf(ficlog,"Error in model: 'model=%s'; 'age*age' should in first place before other covariates\n \
1.192 brouard 8234: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
1.187 brouard 8235: corresponding column of parameters.\n",model); fflush(ficlog);
1.234 brouard 8236: return 1;
1.225 brouard 8237: }
1.187 brouard 8238: nagesqr=1;
8239: if (strstr(model,"+age*age") !=0)
1.234 brouard 8240: substrchaine(modelsav, model, "+age*age");
1.187 brouard 8241: else if (strstr(model,"age*age+") !=0)
1.234 brouard 8242: substrchaine(modelsav, model, "age*age+");
1.187 brouard 8243: else
1.234 brouard 8244: substrchaine(modelsav, model, "age*age");
1.187 brouard 8245: }else
8246: nagesqr=0;
8247: if (strlen(modelsav) >1){
8248: j=nbocc(modelsav,'+'); /**< j=Number of '+' */
8249: j1=nbocc(modelsav,'*'); /**< j1=Number of '*' */
1.224 brouard 8250: cptcovs=j+1-j1; /**< Number of simple covariates V1+V1*age+V3 +V3*V4+age*age=> V1 + V3 =5-3=2 */
1.187 brouard 8251: cptcovt= j+1; /* Number of total covariates in the model, not including
1.225 brouard 8252: * cst, age and age*age
8253: * V1+V1*age+ V3 + V3*V4+age*age=> 3+1=4*/
8254: /* including age products which are counted in cptcovage.
8255: * but the covariates which are products must be treated
8256: * separately: ncovn=4- 2=2 (V1+V3). */
1.187 brouard 8257: cptcovprod=j1; /**< Number of products V1*V2 +v3*age = 2 */
8258: cptcovprodnoage=0; /**< Number of covariate products without age: V3*V4 =1 */
1.225 brouard 8259:
8260:
1.187 brouard 8261: /* Design
8262: * V1 V2 V3 V4 V5 V6 V7 V8 V9 Weight
8263: * < ncovcol=8 >
8264: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8
8265: * k= 1 2 3 4 5 6 7 8
8266: * cptcovn number of covariates (not including constant and age ) = # of + plus 1 = 7+1=8
8267: * covar[k,i], value of kth covariate if not including age for individual i:
1.224 brouard 8268: * covar[1][i]= (V1), covar[4][i]=(V4), covar[8][i]=(V8)
8269: * Tvar[k] # of the kth covariate: Tvar[1]=2 Tvar[2]=1 Tvar[4]=3 Tvar[8]=8
1.187 brouard 8270: * if multiplied by age: V3*age Tvar[3=V3*age]=3 (V3) Tvar[7]=8 and
8271: * Tage[++cptcovage]=k
8272: * if products, new covar are created after ncovcol with k1
8273: * Tvar[k]=ncovcol+k1; # of the kth covariate product: Tvar[5]=ncovcol+1=10 Tvar[6]=ncovcol+1=11
8274: * Tprod[k1]=k; Tprod[1]=5 Tprod[2]= 6; gives the position of the k1th product
8275: * 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
8276: * Tvar[cptcovn+k2]=Tvard[k1][1];Tvar[cptcovn+k2+1]=Tvard[k1][2];
8277: * Tvar[8+1]=5;Tvar[8+2]=6;Tvar[8+3]=7;Tvar[8+4]=8 inverted
8278: * V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11
8279: * < ncovcol=8 >
8280: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8 d1 d1 d2 d2
8281: * k= 1 2 3 4 5 6 7 8 9 10 11 12
8282: * Tvar[k]= 2 1 3 3 10 11 8 8 5 6 7 8
8283: * p Tvar[1]@12={2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
8284: * p Tprod[1]@2={ 6, 5}
8285: *p Tvard[1][1]@4= {7, 8, 5, 6}
8286: * covar[k][i]= V2 V1 ? V3 V5*V6? V7*V8? ? V8
8287: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
8288: *How to reorganize?
8289: * Model V1 + V2 + V3 + V8 + V5*V6 + V7*V8 + V3*age + V8*age
8290: * Tvars {2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
8291: * {2, 1, 4, 8, 5, 6, 3, 7}
8292: * Struct []
8293: */
1.225 brouard 8294:
1.187 brouard 8295: /* This loop fills the array Tvar from the string 'model'.*/
8296: /* j is the number of + signs in the model V1+V2+V3 j=2 i=3 to 1 */
8297: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4 */
8298: /* k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tage[cptcovage=1]=4 */
8299: /* k=3 V4 Tvar[k=3]= 4 (from V4) */
8300: /* k=2 V1 Tvar[k=2]= 1 (from V1) */
8301: /* k=1 Tvar[1]=2 (from V2) */
8302: /* k=5 Tvar[5] */
8303: /* for (k=1; k<=cptcovn;k++) { */
1.198 brouard 8304: /* cov[2+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
1.187 brouard 8305: /* } */
1.198 brouard 8306: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k])]]*cov[2]; */
1.187 brouard 8307: /*
8308: * Treating invertedly V2+V1+V3*age+V2*V4 is as if written V2*V4 +V3*age + V1 + V2 */
1.227 brouard 8309: for(k=cptcovt; k>=1;k--){ /**< Number of covariates not including constant and age, neither age*age*/
8310: Tvar[k]=0; Tprod[k]=0; Tposprod[k]=0;
8311: }
1.187 brouard 8312: cptcovage=0;
8313: for(k=1; k<=cptcovt;k++){ /* Loop on total covariates of the model */
1.234 brouard 8314: cutl(stra,strb,modelsav,'+'); /* keeps in strb after the first '+'
1.225 brouard 8315: modelsav==V2+V1+V4+V3*age strb=V3*age stra=V2+V1+V4 */
1.234 brouard 8316: if (nbocc(modelsav,'+')==0) strcpy(strb,modelsav); /* and analyzes it */
8317: /* printf("i=%d a=%s b=%s sav=%s\n",i, stra,strb,modelsav);*/
8318: /*scanf("%d",i);*/
8319: if (strchr(strb,'*')) { /**< Model includes a product V2+V1+V4+V3*age strb=V3*age */
8320: cutl(strc,strd,strb,'*'); /**< strd*strc Vm*Vn: strb=V3*age(input) strc=age strd=V3 ; V3*V2 strc=V2, strd=V3 */
8321: if (strcmp(strc,"age")==0) { /**< Model includes age: Vn*age */
8322: /* covar is not filled and then is empty */
8323: cptcovprod--;
8324: cutl(stre,strb,strd,'V'); /* strd=V3(input): stre="3" */
8325: Tvar[k]=atoi(stre); /* V2+V1+V4+V3*age Tvar[4]=3 ; V1+V2*age Tvar[2]=2; V1+V1*age Tvar[2]=1 */
8326: Typevar[k]=1; /* 1 for age product */
8327: cptcovage++; /* Sums the number of covariates which include age as a product */
8328: Tage[cptcovage]=k; /* Tvar[4]=3, Tage[1] = 4 or V1+V1*age Tvar[2]=1, Tage[1]=2 */
8329: /*printf("stre=%s ", stre);*/
8330: } else if (strcmp(strd,"age")==0) { /* or age*Vn */
8331: cptcovprod--;
8332: cutl(stre,strb,strc,'V');
8333: Tvar[k]=atoi(stre);
8334: Typevar[k]=1; /* 1 for age product */
8335: cptcovage++;
8336: Tage[cptcovage]=k;
8337: } else { /* Age is not in the model product V2+V1+V1*V4+V3*age+V3*V2 strb=V3*V2*/
8338: /* loops on k1=1 (V3*V2) and k1=2 V4*V3 */
8339: cptcovn++;
8340: cptcovprodnoage++;k1++;
8341: cutl(stre,strb,strc,'V'); /* strc= Vn, stre is n; strb=V3*V2 stre=3 strc=*/
8342: Tvar[k]=ncovcol+nqv+ntv+nqtv+k1; /* For model-covariate k tells which data-covariate to use but
8343: because this model-covariate is a construction we invent a new column
8344: which is after existing variables ncovcol+nqv+ntv+nqtv + k1
8345: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2
8346: Tvar[3=V1*V4]=4+1 Tvar[5=V3*V2]=4 + 2= 6, etc */
8347: Typevar[k]=2; /* 2 for double fixed dummy covariates */
8348: cutl(strc,strb,strd,'V'); /* strd was Vm, strc is m */
8349: Tprod[k1]=k; /* Tprod[1]=3(=V1*V4) for V2+V1+V1*V4+age*V3+V3*V2 */
8350: Tposprod[k]=k1; /* Tpsprod[3]=1, Tposprod[2]=5 */
8351: Tvard[k1][1] =atoi(strc); /* m 1 for V1*/
8352: Tvard[k1][2] =atoi(stre); /* n 4 for V4*/
8353: k2=k2+2; /* k2 is initialize to -1, We want to store the n and m in Vn*Vm at the end of Tvar */
8354: /* Tvar[cptcovt+k2]=Tvard[k1][1]; /\* Tvar[(cptcovt=4+k2=1)=5]= 1 (V1) *\/ */
8355: /* Tvar[cptcovt+k2+1]=Tvard[k1][2]; /\* Tvar[(cptcovt=4+(k2=1)+1)=6]= 4 (V4) *\/ */
1.225 brouard 8356: /*ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2, Tvar[3]=5, Tvar[4]=6, cptcovt=5 */
1.234 brouard 8357: /* 1 2 3 4 5 | Tvar[5+1)=1, Tvar[7]=2 */
8358: for (i=1; i<=lastobs;i++){
8359: /* Computes the new covariate which is a product of
8360: covar[n][i]* covar[m][i] and stores it at ncovol+k1 May not be defined */
8361: covar[ncovcol+k1][i]=covar[atoi(stre)][i]*covar[atoi(strc)][i];
8362: }
8363: } /* End age is not in the model */
8364: } /* End if model includes a product */
8365: else { /* no more sum */
8366: /*printf("d=%s c=%s b=%s\n", strd,strc,strb);*/
8367: /* scanf("%d",i);*/
8368: cutl(strd,strc,strb,'V');
8369: ks++; /**< Number of simple covariates dummy or quantitative, fixe or varying */
8370: cptcovn++; /** V4+V3+V5: V4 and V3 timevarying dummy covariates, V5 timevarying quantitative */
8371: Tvar[k]=atoi(strd);
8372: Typevar[k]=0; /* 0 for simple covariates */
8373: }
8374: strcpy(modelsav,stra); /* modelsav=V2+V1+V4 stra=V2+V1+V4 */
1.223 brouard 8375: /*printf("a=%s b=%s sav=%s\n", stra,strb,modelsav);
1.225 brouard 8376: scanf("%d",i);*/
1.187 brouard 8377: } /* end of loop + on total covariates */
8378: } /* end if strlen(modelsave == 0) age*age might exist */
8379: } /* end if strlen(model == 0) */
1.136 brouard 8380:
8381: /*The number n of Vn is stored in Tvar. cptcovage =number of age covariate. Tage gives the position of age. cptcovprod= number of products.
8382: If model=V1+V1*age then Tvar[1]=1 Tvar[2]=1 cptcovage=1 Tage[1]=2 cptcovprod=0*/
1.225 brouard 8383:
1.136 brouard 8384: /* printf("tvar1=%d tvar2=%d tvar3=%d cptcovage=%d Tage=%d",Tvar[1],Tvar[2],Tvar[3],cptcovage,Tage[1]);
1.225 brouard 8385: printf("cptcovprod=%d ", cptcovprod);
8386: fprintf(ficlog,"cptcovprod=%d ", cptcovprod);
8387: scanf("%d ",i);*/
8388:
8389:
1.230 brouard 8390: /* Until here, decodemodel knows only the grammar (simple, product, age*) of the model but not what kind
8391: of variable (dummy vs quantitative, fixed vs time varying) is behind. But we know the # of each. */
1.226 brouard 8392: /* ncovcol= 1, nqv=1 | ntv=2, nqtv= 1 = 5 possible variables data: 2 fixed 3, varying
8393: model= V5 + V4 +V3 + V4*V3 + V5*age + V2 + V1*V2 + V1*age + V5*age, V1 is not used saving its place
8394: k = 1 2 3 4 5 6 7 8 9
8395: Tvar[k]= 5 4 3 1+1+2+1+1=6 5 2 7 1 5
8396: Typevar[k]= 0 0 0 2 1 0 2 1 1
1.227 brouard 8397: Fixed[k] 1 1 1 1 3 0 0 or 2 2 3
8398: Dummy[k] 1 0 0 0 3 1 1 2 3
8399: Tmodelind[combination of covar]=k;
1.225 brouard 8400: */
8401: /* Dispatching between quantitative and time varying covariates */
1.226 brouard 8402: /* If Tvar[k] >ncovcol it is a product */
1.225 brouard 8403: /* Tvar[k] is the value n of Vn with n varying for 1 to nvcol, or p Vp=Vn*Vm for product */
1.226 brouard 8404: /* Computing effective variables, ie used by the model, that is from the cptcovt variables */
1.227 brouard 8405: printf("Model=%s\n\
8406: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product \n\
8407: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\
8408: Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product\n",model);
8409: fprintf(ficlog,"Model=%s\n\
8410: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product \n\
8411: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\
8412: Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product\n",model);
8413:
1.234 brouard 8414: for(k=1, ncovf=0, nsd=0, nsq=0, ncovv=0, ncova=0, ncoveff=0, nqfveff=0, ntveff=0, nqtveff=0;k<=cptcovt; k++){ /* or cptocvt */
8415: if (Tvar[k] <=ncovcol && Typevar[k]==0 ){ /* Simple fixed dummy (<=ncovcol) covariates */
1.227 brouard 8416: Fixed[k]= 0;
8417: Dummy[k]= 0;
1.225 brouard 8418: ncoveff++;
1.232 brouard 8419: ncovf++;
1.234 brouard 8420: nsd++;
8421: modell[k].maintype= FTYPE;
8422: TvarsD[nsd]=Tvar[k];
8423: TvarsDind[nsd]=k;
8424: TvarF[ncovf]=Tvar[k];
8425: TvarFind[ncovf]=k;
8426: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
8427: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
8428: }else if( Tvar[k] <=ncovcol && Typevar[k]==2){ /* Product of fixed dummy (<=ncovcol) covariates */
8429: Fixed[k]= 0;
8430: Dummy[k]= 0;
8431: ncoveff++;
8432: ncovf++;
8433: modell[k].maintype= FTYPE;
8434: TvarF[ncovf]=Tvar[k];
8435: TvarFind[ncovf]=k;
1.230 brouard 8436: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1.231 brouard 8437: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
8438: }else if( Tvar[k] <=ncovcol+nqv && Typevar[k]==0){ /* Remind that product Vn*Vm are added in k*/ /* Only simple fixed quantitative variable */
1.227 brouard 8439: Fixed[k]= 0;
8440: Dummy[k]= 1;
1.230 brouard 8441: nqfveff++;
1.234 brouard 8442: modell[k].maintype= FTYPE;
8443: modell[k].subtype= FQ;
8444: nsq++;
8445: TvarsQ[nsq]=Tvar[k];
8446: TvarsQind[nsq]=k;
1.232 brouard 8447: ncovf++;
1.234 brouard 8448: TvarF[ncovf]=Tvar[k];
8449: TvarFind[ncovf]=k;
1.231 brouard 8450: TvarFQ[nqfveff]=Tvar[k]-ncovcol; /* TvarFQ[1]=V2-1=1st in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1.230 brouard 8451: TvarFQind[nqfveff]=k; /* TvarFQind[1]=6 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1.234 brouard 8452: }else if( Tvar[k] <=ncovcol+nqv+ntv && Typevar[k]==0){/* Only simple time varying variables */
1.227 brouard 8453: Fixed[k]= 1;
8454: Dummy[k]= 0;
1.225 brouard 8455: ntveff++; /* Only simple time varying dummy variable */
1.234 brouard 8456: modell[k].maintype= VTYPE;
8457: modell[k].subtype= VD;
8458: nsd++;
8459: TvarsD[nsd]=Tvar[k];
8460: TvarsDind[nsd]=k;
8461: ncovv++; /* Only simple time varying variables */
8462: TvarV[ncovv]=Tvar[k];
8463: TvarVind[ncovv]=k;
1.231 brouard 8464: TvarVD[ntveff]=Tvar[k]; /* TvarVD[1]=V4 TvarVD[2]=V3 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple time varying dummy variable */
8465: TvarVDind[ntveff]=k; /* TvarVDind[1]=2 TvarVDind[2]=3 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple time varying dummy variable */
1.228 brouard 8466: printf("Quasi Tmodelind[%d]=%d,Tvar[Tmodelind[%d]]=V%d, ncovcol=%d, nqv=%d,Tvar[k]- ncovcol-nqv=%d\n",ntveff,k,ntveff,Tvar[k], ncovcol, nqv,Tvar[k]- ncovcol-nqv);
8467: printf("Quasi TmodelInvind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv);
1.231 brouard 8468: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv && Typevar[k]==0){ /* Only simple time varying quantitative variable V5*/
1.234 brouard 8469: Fixed[k]= 1;
8470: Dummy[k]= 1;
8471: nqtveff++;
8472: modell[k].maintype= VTYPE;
8473: modell[k].subtype= VQ;
8474: ncovv++; /* Only simple time varying variables */
8475: nsq++;
8476: TvarsQ[nsq]=Tvar[k];
8477: TvarsQind[nsq]=k;
8478: TvarV[ncovv]=Tvar[k];
8479: TvarVind[ncovv]=k;
1.231 brouard 8480: TvarVQ[nqtveff]=Tvar[k]; /* TvarVQ[1]=V5 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple time varying quantitative variable */
8481: TvarVQind[nqtveff]=k; /* TvarVQind[1]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple time varying quantitative variable */
1.234 brouard 8482: TmodelInvQind[nqtveff]=Tvar[k]- ncovcol-nqv-ntv;/* Only simple time varying quantitative variable */
8483: /* Tmodeliqind[k]=nqtveff;/\* Only simple time varying quantitative variable *\/ */
8484: printf("Quasi TmodelQind[%d]=%d,Tvar[TmodelQind[%d]]=V%d, ncovcol=%d, nqv=%d, ntv=%d,Tvar[k]- ncovcol-nqv-ntv=%d\n",nqtveff,k,nqtveff,Tvar[k], ncovcol, nqv, ntv, Tvar[k]- ncovcol-nqv-ntv);
1.228 brouard 8485: printf("Quasi TmodelInvQind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv-ntv);
1.227 brouard 8486: }else if (Typevar[k] == 1) { /* product with age */
1.234 brouard 8487: ncova++;
8488: TvarA[ncova]=Tvar[k];
8489: TvarAind[ncova]=k;
1.231 brouard 8490: if (Tvar[k] <=ncovcol ){ /* Product age with fixed dummy covariatee */
1.234 brouard 8491: Fixed[k]= 2;
8492: Dummy[k]= 2;
8493: modell[k].maintype= ATYPE;
8494: modell[k].subtype= APFD;
8495: /* ncoveff++; */
1.227 brouard 8496: }else if( Tvar[k] <=ncovcol+nqv) { /* Remind that product Vn*Vm are added in k*/
1.234 brouard 8497: Fixed[k]= 2;
8498: Dummy[k]= 3;
8499: modell[k].maintype= ATYPE;
8500: modell[k].subtype= APFQ; /* Product age * fixed quantitative */
8501: /* nqfveff++; /\* Only simple fixed quantitative variable *\/ */
1.227 brouard 8502: }else if( Tvar[k] <=ncovcol+nqv+ntv ){
1.234 brouard 8503: Fixed[k]= 3;
8504: Dummy[k]= 2;
8505: modell[k].maintype= ATYPE;
8506: modell[k].subtype= APVD; /* Product age * varying dummy */
8507: /* ntveff++; /\* Only simple time varying dummy variable *\/ */
1.227 brouard 8508: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv){
1.234 brouard 8509: Fixed[k]= 3;
8510: Dummy[k]= 3;
8511: modell[k].maintype= ATYPE;
8512: modell[k].subtype= APVQ; /* Product age * varying quantitative */
8513: /* nqtveff++;/\* Only simple time varying quantitative variable *\/ */
1.227 brouard 8514: }
8515: }else if (Typevar[k] == 2) { /* product without age */
8516: k1=Tposprod[k];
8517: if(Tvard[k1][1] <=ncovcol){
1.234 brouard 8518: if(Tvard[k1][2] <=ncovcol){
8519: Fixed[k]= 1;
8520: Dummy[k]= 0;
8521: modell[k].maintype= FTYPE;
8522: modell[k].subtype= FPDD; /* Product fixed dummy * fixed dummy */
8523: ncovf++; /* Fixed variables without age */
8524: TvarF[ncovf]=Tvar[k];
8525: TvarFind[ncovf]=k;
8526: }else if(Tvard[k1][2] <=ncovcol+nqv){
8527: Fixed[k]= 0; /* or 2 ?*/
8528: Dummy[k]= 1;
8529: modell[k].maintype= FTYPE;
8530: modell[k].subtype= FPDQ; /* Product fixed dummy * fixed quantitative */
8531: ncovf++; /* Varying variables without age */
8532: TvarF[ncovf]=Tvar[k];
8533: TvarFind[ncovf]=k;
8534: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
8535: Fixed[k]= 1;
8536: Dummy[k]= 0;
8537: modell[k].maintype= VTYPE;
8538: modell[k].subtype= VPDD; /* Product fixed dummy * varying dummy */
8539: ncovv++; /* Varying variables without age */
8540: TvarV[ncovv]=Tvar[k];
8541: TvarVind[ncovv]=k;
8542: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
8543: Fixed[k]= 1;
8544: Dummy[k]= 1;
8545: modell[k].maintype= VTYPE;
8546: modell[k].subtype= VPDQ; /* Product fixed dummy * varying quantitative */
8547: ncovv++; /* Varying variables without age */
8548: TvarV[ncovv]=Tvar[k];
8549: TvarVind[ncovv]=k;
8550: }
1.227 brouard 8551: }else if(Tvard[k1][1] <=ncovcol+nqv){
1.234 brouard 8552: if(Tvard[k1][2] <=ncovcol){
8553: Fixed[k]= 0; /* or 2 ?*/
8554: Dummy[k]= 1;
8555: modell[k].maintype= FTYPE;
8556: modell[k].subtype= FPDQ; /* Product fixed quantitative * fixed dummy */
8557: ncovf++; /* Fixed variables without age */
8558: TvarF[ncovf]=Tvar[k];
8559: TvarFind[ncovf]=k;
8560: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
8561: Fixed[k]= 1;
8562: Dummy[k]= 1;
8563: modell[k].maintype= VTYPE;
8564: modell[k].subtype= VPDQ; /* Product fixed quantitative * varying dummy */
8565: ncovv++; /* Varying variables without age */
8566: TvarV[ncovv]=Tvar[k];
8567: TvarVind[ncovv]=k;
8568: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
8569: Fixed[k]= 1;
8570: Dummy[k]= 1;
8571: modell[k].maintype= VTYPE;
8572: modell[k].subtype= VPQQ; /* Product fixed quantitative * varying quantitative */
8573: ncovv++; /* Varying variables without age */
8574: TvarV[ncovv]=Tvar[k];
8575: TvarVind[ncovv]=k;
8576: ncovv++; /* Varying variables without age */
8577: TvarV[ncovv]=Tvar[k];
8578: TvarVind[ncovv]=k;
8579: }
1.227 brouard 8580: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv){
1.234 brouard 8581: if(Tvard[k1][2] <=ncovcol){
8582: Fixed[k]= 1;
8583: Dummy[k]= 1;
8584: modell[k].maintype= VTYPE;
8585: modell[k].subtype= VPDD; /* Product time varying dummy * fixed dummy */
8586: ncovv++; /* Varying variables without age */
8587: TvarV[ncovv]=Tvar[k];
8588: TvarVind[ncovv]=k;
8589: }else if(Tvard[k1][2] <=ncovcol+nqv){
8590: Fixed[k]= 1;
8591: Dummy[k]= 1;
8592: modell[k].maintype= VTYPE;
8593: modell[k].subtype= VPDQ; /* Product time varying dummy * fixed quantitative */
8594: ncovv++; /* Varying variables without age */
8595: TvarV[ncovv]=Tvar[k];
8596: TvarVind[ncovv]=k;
8597: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
8598: Fixed[k]= 1;
8599: Dummy[k]= 0;
8600: modell[k].maintype= VTYPE;
8601: modell[k].subtype= VPDD; /* Product time varying dummy * time varying dummy */
8602: ncovv++; /* Varying variables without age */
8603: TvarV[ncovv]=Tvar[k];
8604: TvarVind[ncovv]=k;
8605: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
8606: Fixed[k]= 1;
8607: Dummy[k]= 1;
8608: modell[k].maintype= VTYPE;
8609: modell[k].subtype= VPDQ; /* Product time varying dummy * time varying quantitative */
8610: ncovv++; /* Varying variables without age */
8611: TvarV[ncovv]=Tvar[k];
8612: TvarVind[ncovv]=k;
8613: }
1.227 brouard 8614: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv+nqtv){
1.234 brouard 8615: if(Tvard[k1][2] <=ncovcol){
8616: Fixed[k]= 1;
8617: Dummy[k]= 1;
8618: modell[k].maintype= VTYPE;
8619: modell[k].subtype= VPDQ; /* Product time varying quantitative * fixed dummy */
8620: ncovv++; /* Varying variables without age */
8621: TvarV[ncovv]=Tvar[k];
8622: TvarVind[ncovv]=k;
8623: }else if(Tvard[k1][2] <=ncovcol+nqv){
8624: Fixed[k]= 1;
8625: Dummy[k]= 1;
8626: modell[k].maintype= VTYPE;
8627: modell[k].subtype= VPQQ; /* Product time varying quantitative * fixed quantitative */
8628: ncovv++; /* Varying variables without age */
8629: TvarV[ncovv]=Tvar[k];
8630: TvarVind[ncovv]=k;
8631: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
8632: Fixed[k]= 1;
8633: Dummy[k]= 1;
8634: modell[k].maintype= VTYPE;
8635: modell[k].subtype= VPDQ; /* Product time varying quantitative * time varying dummy */
8636: ncovv++; /* Varying variables without age */
8637: TvarV[ncovv]=Tvar[k];
8638: TvarVind[ncovv]=k;
8639: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
8640: Fixed[k]= 1;
8641: Dummy[k]= 1;
8642: modell[k].maintype= VTYPE;
8643: modell[k].subtype= VPQQ; /* Product time varying quantitative * time varying quantitative */
8644: ncovv++; /* Varying variables without age */
8645: TvarV[ncovv]=Tvar[k];
8646: TvarVind[ncovv]=k;
8647: }
1.227 brouard 8648: }else{
1.234 brouard 8649: printf("Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
8650: fprintf(ficlog,"Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
1.226 brouard 8651: } /* end k1 */
1.225 brouard 8652: }else{
1.226 brouard 8653: printf("Error, current version can't treat for performance reasons, Tvar[%d]=%d, Typevar[%d]=%d\n", k, Tvar[k], k, Typevar[k]);
8654: fprintf(ficlog,"Error, current version can't treat for performance reasons, Tvar[%d]=%d, Typevar[%d]=%d\n", k, Tvar[k], k, Typevar[k]);
1.225 brouard 8655: }
1.227 brouard 8656: printf("Decodemodel, k=%d, Tvar[%d]=V%d,Typevar=%d, Fixed=%d, Dummy=%d\n",k, k,Tvar[k],Typevar[k],Fixed[k],Dummy[k]);
1.231 brouard 8657: printf(" modell[%d].maintype=%d, modell[%d].subtype=%d\n",k,modell[k].maintype,k,modell[k].subtype);
1.227 brouard 8658: fprintf(ficlog,"Decodemodel, k=%d, Tvar[%d]=V%d,Typevar=%d, Fixed=%d, Dummy=%d\n",k, k,Tvar[k],Typevar[k],Fixed[k],Dummy[k]);
8659: }
8660: /* Searching for doublons in the model */
8661: for(k1=1; k1<= cptcovt;k1++){
8662: for(k2=1; k2 <k1;k2++){
8663: if((Typevar[k1]==Typevar[k2]) && (Fixed[Tvar[k1]]==Fixed[Tvar[k2]]) && (Dummy[Tvar[k1]]==Dummy[Tvar[k2]] )){
1.234 brouard 8664: if((Typevar[k1] == 0 || Typevar[k1] == 1)){ /* Simple or age product */
8665: if(Tvar[k1]==Tvar[k2]){
8666: printf("Error duplication in the model=%s at positions (+) %d and %d, Tvar[%d]=V%d, Tvar[%d]=V%d, Typevar=%d, Fixed=%d, Dummy=%d\n", model, k1,k2, k1, Tvar[k1], k2, Tvar[k2],Typevar[k1],Fixed[Tvar[k1]],Dummy[Tvar[k1]]);
8667: fprintf(ficlog,"Error duplication in the model=%s at positions (+) %d and %d, Tvar[%d]=V%d, Tvar[%d]=V%d, Typevar=%d, Fixed=%d, Dummy=%d\n", model, k1,k2, k1, Tvar[k1], k2, Tvar[k2],Typevar[k1],Fixed[Tvar[k1]],Dummy[Tvar[k1]]); fflush(ficlog);
8668: return(1);
8669: }
8670: }else if (Typevar[k1] ==2){
8671: k3=Tposprod[k1];
8672: k4=Tposprod[k2];
8673: if( ((Tvard[k3][1]== Tvard[k4][1])&&(Tvard[k3][2]== Tvard[k4][2])) || ((Tvard[k3][1]== Tvard[k4][2])&&(Tvard[k3][2]== Tvard[k4][1])) ){
8674: printf("Error duplication in the model=%s at positions (+) %d and %d, V%d*V%d, Typevar=%d, Fixed=%d, Dummy=%d\n",model, k1,k2, Tvard[k3][1], Tvard[k3][2],Typevar[k1],Fixed[Tvar[k1]],Dummy[Tvar[k1]]);
8675: fprintf(ficlog,"Error duplication in the model=%s at positions (+) %d and %d, V%d*V%d, Typevar=%d, Fixed=%d, Dummy=%d\n",model, k1,k2, Tvard[k3][1], Tvard[k3][2],Typevar[k1],Fixed[Tvar[k1]],Dummy[Tvar[k1]]); fflush(ficlog);
8676: return(1);
8677: }
8678: }
1.227 brouard 8679: }
8680: }
1.225 brouard 8681: }
8682: printf("ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
8683: fprintf(ficlog,"ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
1.234 brouard 8684: printf("ncovf=%d, ncovv=%d, ncova=%d, nsd=%d, nsq=%d\n",ncovf,ncovv,ncova,nsd,nsq);
8685: fprintf(ficlog,"ncovf=%d, ncovv=%d, ncova=%d, nsd=%d, nsq=%d\n",ncovf,ncovv,ncova,nsd, nsq);
1.137 brouard 8686: return (0); /* with covar[new additional covariate if product] and Tage if age */
1.164 brouard 8687: /*endread:*/
1.225 brouard 8688: printf("Exiting decodemodel: ");
8689: return (1);
1.136 brouard 8690: }
8691:
1.169 brouard 8692: int calandcheckages(int imx, int maxwav, double *agemin, double *agemax, int *nberr, int *nbwarn )
1.136 brouard 8693: {
8694: int i, m;
1.218 brouard 8695: int firstone=0;
8696:
1.136 brouard 8697: for (i=1; i<=imx; i++) {
8698: for(m=2; (m<= maxwav); m++) {
8699: if (((int)mint[m][i]== 99) && (s[m][i] <= nlstate)){
8700: anint[m][i]=9999;
1.216 brouard 8701: if (s[m][i] != -2) /* Keeping initial status of unknown vital status */
8702: s[m][i]=-1;
1.136 brouard 8703: }
8704: if((int)moisdc[i]==99 && (int)andc[i]==9999 && s[m][i]>nlstate){
1.169 brouard 8705: *nberr = *nberr + 1;
1.218 brouard 8706: if(firstone == 0){
8707: firstone=1;
8708: 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 can be biased (%d) because status is a death state %d at wave %d. Wave dropped.\nOther similar cases in log file\n",(int)moisdc[i],(int)andc[i],num[i],i, *nberr,s[m][i],m);
8709: }
8710: 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 can be biased (%d) because status is a death state %d at wave %d. Wave dropped.\n",(int)moisdc[i],(int)andc[i],num[i],i, *nberr,s[m][i],m);
1.136 brouard 8711: s[m][i]=-1;
8712: }
8713: if((int)moisdc[i]==99 && (int)andc[i]!=9999 && s[m][i]>nlstate){
1.169 brouard 8714: (*nberr)++;
1.136 brouard 8715: 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]);
8716: 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]);
8717: s[m][i]=-1; /* We prefer to skip it (and to skip it in version 0.8a1 too */
8718: }
8719: }
8720: }
8721:
8722: for (i=1; i<=imx; i++) {
8723: agedc[i]=(moisdc[i]/12.+andc[i])-(moisnais[i]/12.+annais[i]);
8724: for(m=firstpass; (m<= lastpass); m++){
1.214 brouard 8725: if(s[m][i] >0 || s[m][i]==-1 || s[m][i]==-2 || s[m][i]==-4 || s[m][i]==-5){ /* What if s[m][i]=-1 */
1.136 brouard 8726: if (s[m][i] >= nlstate+1) {
1.169 brouard 8727: if(agedc[i]>0){
8728: if((int)moisdc[i]!=99 && (int)andc[i]!=9999){
1.136 brouard 8729: agev[m][i]=agedc[i];
1.214 brouard 8730: /*if(moisdc[i]==99 && andc[i]==9999) s[m][i]=-1;*/
1.169 brouard 8731: }else {
1.136 brouard 8732: if ((int)andc[i]!=9999){
8733: nbwarn++;
8734: printf("Warning negative age at death: %ld line:%d\n",num[i],i);
8735: fprintf(ficlog,"Warning negative age at death: %ld line:%d\n",num[i],i);
8736: agev[m][i]=-1;
8737: }
8738: }
1.169 brouard 8739: } /* agedc > 0 */
1.214 brouard 8740: } /* end if */
1.136 brouard 8741: else if(s[m][i] !=9){ /* Standard case, age in fractional
8742: years but with the precision of a month */
8743: agev[m][i]=(mint[m][i]/12.+1./24.+anint[m][i])-(moisnais[i]/12.+1./24.+annais[i]);
8744: if((int)mint[m][i]==99 || (int)anint[m][i]==9999)
8745: agev[m][i]=1;
8746: else if(agev[m][i] < *agemin){
8747: *agemin=agev[m][i];
8748: printf(" Min anint[%d][%d]=%.2f annais[%d]=%.2f, agemin=%.2f\n",m,i,anint[m][i], i,annais[i], *agemin);
8749: }
8750: else if(agev[m][i] >*agemax){
8751: *agemax=agev[m][i];
1.156 brouard 8752: /* printf(" Max anint[%d][%d]=%.0f annais[%d]=%.0f, agemax=%.2f\n",m,i,anint[m][i], i,annais[i], *agemax);*/
1.136 brouard 8753: }
8754: /*agev[m][i]=anint[m][i]-annais[i];*/
8755: /* agev[m][i] = age[i]+2*m;*/
1.214 brouard 8756: } /* en if 9*/
1.136 brouard 8757: else { /* =9 */
1.214 brouard 8758: /* printf("Debug num[%d]=%ld s[%d][%d]=%d\n",i,num[i], m,i, s[m][i]); */
1.136 brouard 8759: agev[m][i]=1;
8760: s[m][i]=-1;
8761: }
8762: }
1.214 brouard 8763: else if(s[m][i]==0) /*= 0 Unknown */
1.136 brouard 8764: agev[m][i]=1;
1.214 brouard 8765: else{
8766: printf("Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
8767: fprintf(ficlog, "Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
8768: agev[m][i]=0;
8769: }
8770: } /* End for lastpass */
8771: }
1.136 brouard 8772:
8773: for (i=1; i<=imx; i++) {
8774: for(m=firstpass; (m<=lastpass); m++){
8775: if (s[m][i] > (nlstate+ndeath)) {
1.169 brouard 8776: (*nberr)++;
1.136 brouard 8777: 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);
8778: 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);
8779: return 1;
8780: }
8781: }
8782: }
8783:
8784: /*for (i=1; i<=imx; i++){
8785: for (m=firstpass; (m<lastpass); m++){
8786: printf("%ld %d %.lf %d %d\n", num[i],(covar[1][i]),agev[m][i],s[m][i],s[m+1][i]);
8787: }
8788:
8789: }*/
8790:
8791:
1.139 brouard 8792: printf("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
8793: fprintf(ficlog,"Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
1.136 brouard 8794:
8795: return (0);
1.164 brouard 8796: /* endread:*/
1.136 brouard 8797: printf("Exiting calandcheckages: ");
8798: return (1);
8799: }
8800:
1.172 brouard 8801: #if defined(_MSC_VER)
8802: /*printf("Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
8803: /*fprintf(ficlog, "Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
8804: //#include "stdafx.h"
8805: //#include <stdio.h>
8806: //#include <tchar.h>
8807: //#include <windows.h>
8808: //#include <iostream>
8809: typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
8810:
8811: LPFN_ISWOW64PROCESS fnIsWow64Process;
8812:
8813: BOOL IsWow64()
8814: {
8815: BOOL bIsWow64 = FALSE;
8816:
8817: //typedef BOOL (APIENTRY *LPFN_ISWOW64PROCESS)
8818: // (HANDLE, PBOOL);
8819:
8820: //LPFN_ISWOW64PROCESS fnIsWow64Process;
8821:
8822: HMODULE module = GetModuleHandle(_T("kernel32"));
8823: const char funcName[] = "IsWow64Process";
8824: fnIsWow64Process = (LPFN_ISWOW64PROCESS)
8825: GetProcAddress(module, funcName);
8826:
8827: if (NULL != fnIsWow64Process)
8828: {
8829: if (!fnIsWow64Process(GetCurrentProcess(),
8830: &bIsWow64))
8831: //throw std::exception("Unknown error");
8832: printf("Unknown error\n");
8833: }
8834: return bIsWow64 != FALSE;
8835: }
8836: #endif
1.177 brouard 8837:
1.191 brouard 8838: void syscompilerinfo(int logged)
1.167 brouard 8839: {
8840: /* #include "syscompilerinfo.h"*/
1.185 brouard 8841: /* command line Intel compiler 32bit windows, XP compatible:*/
8842: /* /GS /W3 /Gy
8843: /Zc:wchar_t /Zi /O2 /Fd"Release\vc120.pdb" /D "WIN32" /D "NDEBUG" /D
8844: "_CONSOLE" /D "_LIB" /D "_USING_V110_SDK71_" /D "_UNICODE" /D
8845: "UNICODE" /Qipo /Zc:forScope /Gd /Oi /MT /Fa"Release\" /EHsc /nologo
1.186 brouard 8846: /Fo"Release\" /Qprof-dir "Release\" /Fp"Release\IMaCh.pch"
8847: */
8848: /* 64 bits */
1.185 brouard 8849: /*
8850: /GS /W3 /Gy
8851: /Zc:wchar_t /Zi /O2 /Fd"x64\Release\vc120.pdb" /D "WIN32" /D "NDEBUG"
8852: /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo /Zc:forScope
8853: /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Fo"x64\Release\" /Qprof-dir
8854: "x64\Release\" /Fp"x64\Release\IMaCh.pch" */
8855: /* Optimization are useless and O3 is slower than O2 */
8856: /*
8857: /GS /W3 /Gy /Zc:wchar_t /Zi /O3 /Fd"x64\Release\vc120.pdb" /D "WIN32"
8858: /D "NDEBUG" /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo
8859: /Zc:forScope /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Qparallel
8860: /Fo"x64\Release\" /Qprof-dir "x64\Release\" /Fp"x64\Release\IMaCh.pch"
8861: */
1.186 brouard 8862: /* Link is */ /* /OUT:"visual studio
1.185 brouard 8863: 2013\Projects\IMaCh\Release\IMaCh.exe" /MANIFEST /NXCOMPAT
8864: /PDB:"visual studio
8865: 2013\Projects\IMaCh\Release\IMaCh.pdb" /DYNAMICBASE
8866: "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib"
8867: "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib"
8868: "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib"
8869: /MACHINE:X86 /OPT:REF /SAFESEH /INCREMENTAL:NO
8870: /SUBSYSTEM:CONSOLE",5.01" /MANIFESTUAC:"level='asInvoker'
8871: uiAccess='false'"
8872: /ManifestFile:"Release\IMaCh.exe.intermediate.manifest" /OPT:ICF
8873: /NOLOGO /TLBID:1
8874: */
1.177 brouard 8875: #if defined __INTEL_COMPILER
1.178 brouard 8876: #if defined(__GNUC__)
8877: struct utsname sysInfo; /* For Intel on Linux and OS/X */
8878: #endif
1.177 brouard 8879: #elif defined(__GNUC__)
1.179 brouard 8880: #ifndef __APPLE__
1.174 brouard 8881: #include <gnu/libc-version.h> /* Only on gnu */
1.179 brouard 8882: #endif
1.177 brouard 8883: struct utsname sysInfo;
1.178 brouard 8884: int cross = CROSS;
8885: if (cross){
8886: printf("Cross-");
1.191 brouard 8887: if(logged) fprintf(ficlog, "Cross-");
1.178 brouard 8888: }
1.174 brouard 8889: #endif
8890:
1.171 brouard 8891: #include <stdint.h>
1.178 brouard 8892:
1.191 brouard 8893: printf("Compiled with:");if(logged)fprintf(ficlog,"Compiled with:");
1.169 brouard 8894: #if defined(__clang__)
1.191 brouard 8895: printf(" Clang/LLVM");if(logged)fprintf(ficlog," Clang/LLVM"); /* Clang/LLVM. ---------------------------------------------- */
1.169 brouard 8896: #endif
8897: #if defined(__ICC) || defined(__INTEL_COMPILER)
1.191 brouard 8898: printf(" Intel ICC/ICPC");if(logged)fprintf(ficlog," Intel ICC/ICPC");/* Intel ICC/ICPC. ------------------------------------------ */
1.169 brouard 8899: #endif
8900: #if defined(__GNUC__) || defined(__GNUG__)
1.191 brouard 8901: printf(" GNU GCC/G++");if(logged)fprintf(ficlog," GNU GCC/G++");/* GNU GCC/G++. --------------------------------------------- */
1.169 brouard 8902: #endif
8903: #if defined(__HP_cc) || defined(__HP_aCC)
1.191 brouard 8904: printf(" Hewlett-Packard C/aC++");if(logged)fprintf(fcilog," Hewlett-Packard C/aC++"); /* Hewlett-Packard C/aC++. ---------------------------------- */
1.169 brouard 8905: #endif
8906: #if defined(__IBMC__) || defined(__IBMCPP__)
1.191 brouard 8907: printf(" IBM XL C/C++"); if(logged) fprintf(ficlog," IBM XL C/C++");/* IBM XL C/C++. -------------------------------------------- */
1.169 brouard 8908: #endif
8909: #if defined(_MSC_VER)
1.191 brouard 8910: printf(" Microsoft Visual Studio");if(logged)fprintf(ficlog," Microsoft Visual Studio");/* Microsoft Visual Studio. --------------------------------- */
1.169 brouard 8911: #endif
8912: #if defined(__PGI)
1.191 brouard 8913: printf(" Portland Group PGCC/PGCPP");if(logged) fprintf(ficlog," Portland Group PGCC/PGCPP");/* Portland Group PGCC/PGCPP. ------------------------------- */
1.169 brouard 8914: #endif
8915: #if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
1.191 brouard 8916: printf(" Oracle Solaris Studio");if(logged)fprintf(ficlog," Oracle Solaris Studio\n");/* Oracle Solaris Studio. ----------------------------------- */
1.167 brouard 8917: #endif
1.191 brouard 8918: printf(" for "); if (logged) fprintf(ficlog, " for ");
1.169 brouard 8919:
1.167 brouard 8920: // http://stackoverflow.com/questions/4605842/how-to-identify-platform-compiler-from-preprocessor-macros
8921: #ifdef _WIN32 // note the underscore: without it, it's not msdn official!
8922: // Windows (x64 and x86)
1.191 brouard 8923: printf("Windows (x64 and x86) ");if(logged) fprintf(ficlog,"Windows (x64 and x86) ");
1.167 brouard 8924: #elif __unix__ // all unices, not all compilers
8925: // Unix
1.191 brouard 8926: printf("Unix ");if(logged) fprintf(ficlog,"Unix ");
1.167 brouard 8927: #elif __linux__
8928: // linux
1.191 brouard 8929: printf("linux ");if(logged) fprintf(ficlog,"linux ");
1.167 brouard 8930: #elif __APPLE__
1.174 brouard 8931: // Mac OS, not sure if this is covered by __posix__ and/or __unix__ though..
1.191 brouard 8932: printf("Mac OS ");if(logged) fprintf(ficlog,"Mac OS ");
1.167 brouard 8933: #endif
8934:
8935: /* __MINGW32__ */
8936: /* __CYGWIN__ */
8937: /* __MINGW64__ */
8938: // http://msdn.microsoft.com/en-us/library/b0084kay.aspx
8939: /* _MSC_VER //the Visual C++ compiler is 17.00.51106.1, the _MSC_VER macro evaluates to 1700. Type cl /? */
8940: /* _MSC_FULL_VER //the Visual C++ compiler is 15.00.20706.01, the _MSC_FULL_VER macro evaluates to 150020706 */
8941: /* _WIN64 // Defined for applications for Win64. */
8942: /* _M_X64 // Defined for compilations that target x64 processors. */
8943: /* _DEBUG // Defined when you compile with /LDd, /MDd, and /MTd. */
1.171 brouard 8944:
1.167 brouard 8945: #if UINTPTR_MAX == 0xffffffff
1.191 brouard 8946: printf(" 32-bit"); if(logged) fprintf(ficlog," 32-bit");/* 32-bit */
1.167 brouard 8947: #elif UINTPTR_MAX == 0xffffffffffffffff
1.191 brouard 8948: printf(" 64-bit"); if(logged) fprintf(ficlog," 64-bit");/* 64-bit */
1.167 brouard 8949: #else
1.191 brouard 8950: printf(" wtf-bit"); if(logged) fprintf(ficlog," wtf-bit");/* wtf */
1.167 brouard 8951: #endif
8952:
1.169 brouard 8953: #if defined(__GNUC__)
8954: # if defined(__GNUC_PATCHLEVEL__)
8955: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
8956: + __GNUC_MINOR__ * 100 \
8957: + __GNUC_PATCHLEVEL__)
8958: # else
8959: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
8960: + __GNUC_MINOR__ * 100)
8961: # endif
1.174 brouard 8962: printf(" using GNU C version %d.\n", __GNUC_VERSION__);
1.191 brouard 8963: if(logged) fprintf(ficlog, " using GNU C version %d.\n", __GNUC_VERSION__);
1.176 brouard 8964:
8965: if (uname(&sysInfo) != -1) {
8966: printf("Running on: %s %s %s %s %s\n",sysInfo.sysname, sysInfo.nodename, sysInfo.release, sysInfo.version, sysInfo.machine);
1.191 brouard 8967: if(logged) fprintf(ficlog,"Running on: %s %s %s %s %s\n ",sysInfo.sysname, sysInfo.nodename, sysInfo.release, sysInfo.version, sysInfo.machine);
1.176 brouard 8968: }
8969: else
8970: perror("uname() error");
1.179 brouard 8971: //#ifndef __INTEL_COMPILER
8972: #if !defined (__INTEL_COMPILER) && !defined(__APPLE__)
1.174 brouard 8973: printf("GNU libc version: %s\n", gnu_get_libc_version());
1.191 brouard 8974: if(logged) fprintf(ficlog,"GNU libc version: %s\n", gnu_get_libc_version());
1.177 brouard 8975: #endif
1.169 brouard 8976: #endif
1.172 brouard 8977:
8978: // void main()
8979: // {
1.169 brouard 8980: #if defined(_MSC_VER)
1.174 brouard 8981: if (IsWow64()){
1.191 brouard 8982: printf("\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
8983: if (logged) fprintf(ficlog, "\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
1.174 brouard 8984: }
8985: else{
1.191 brouard 8986: printf("\nThe program is not running under WOW64 (i.e probably on a 64bit Windows).\n");
8987: if (logged) fprintf(ficlog, "\nThe programm is not running under WOW64 (i.e probably on a 64bit Windows).\n");
1.174 brouard 8988: }
1.172 brouard 8989: // printf("\nPress Enter to continue...");
8990: // getchar();
8991: // }
8992:
1.169 brouard 8993: #endif
8994:
1.167 brouard 8995:
1.219 brouard 8996: }
1.136 brouard 8997:
1.219 brouard 8998: int prevalence_limit(double *p, double **prlim, double ageminpar, double agemaxpar, double ftolpl, int *ncvyearp){
1.180 brouard 8999: /*--------------- Prevalence limit (period or stable prevalence) --------------*/
1.235 brouard 9000: int i, j, k, i1, k4=0, nres=0 ;
1.202 brouard 9001: /* double ftolpl = 1.e-10; */
1.180 brouard 9002: double age, agebase, agelim;
1.203 brouard 9003: double tot;
1.180 brouard 9004:
1.202 brouard 9005: strcpy(filerespl,"PL_");
9006: strcat(filerespl,fileresu);
9007: if((ficrespl=fopen(filerespl,"w"))==NULL) {
9008: printf("Problem with period (stable) prevalence resultfile: %s\n", filerespl);return 1;
9009: fprintf(ficlog,"Problem with period (stable) prevalence resultfile: %s\n", filerespl);return 1;
9010: }
1.227 brouard 9011: printf("\nComputing period (stable) prevalence: result on file '%s' \n", filerespl);
9012: fprintf(ficlog,"\nComputing period (stable) prevalence: result on file '%s' \n", filerespl);
1.202 brouard 9013: pstamp(ficrespl);
1.203 brouard 9014: fprintf(ficrespl,"# Period (stable) prevalence. Precision given by ftolpl=%g \n", ftolpl);
1.202 brouard 9015: fprintf(ficrespl,"#Age ");
9016: for(i=1; i<=nlstate;i++) fprintf(ficrespl,"%d-%d ",i,i);
9017: fprintf(ficrespl,"\n");
1.180 brouard 9018:
1.219 brouard 9019: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
1.180 brouard 9020:
1.219 brouard 9021: agebase=ageminpar;
9022: agelim=agemaxpar;
1.180 brouard 9023:
1.227 brouard 9024: /* i1=pow(2,ncoveff); */
1.234 brouard 9025: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
1.219 brouard 9026: if (cptcovn < 1){i1=1;}
1.180 brouard 9027:
1.235 brouard 9028: for(nres=1; nres <= nresult; nres++) /* For each resultline */
1.220 brouard 9029: for(k=1; k<=i1;k++){
1.235 brouard 9030: if(TKresult[nres]!= k)
9031: continue;
9032:
1.220 brouard 9033: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
1.180 brouard 9034: /* for(cptcov=1,k=0;cptcov<=1;cptcov++){ */
1.219 brouard 9035: //for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
1.220 brouard 9036: /* k=k+1; */
1.219 brouard 9037: /* to clean */
9038: //printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));
9039: fprintf(ficrespl,"#******");
9040: printf("#******");
9041: fprintf(ficlog,"#******");
1.227 brouard 9042: for(j=1;j<=cptcoveff ;j++) {/* all covariates */
9043: fprintf(ficrespl," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); /* Here problem for varying dummy*/
1.219 brouard 9044: printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9045: fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9046: }
1.235 brouard 9047: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
9048: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
9049: fprintf(ficlog," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
9050: }
1.219 brouard 9051: fprintf(ficrespl,"******\n");
9052: printf("******\n");
9053: fprintf(ficlog,"******\n");
1.227 brouard 9054: if(invalidvarcomb[k]){
9055: printf("\nCombination (%d) ignored because no case \n",k);
9056: fprintf(ficrespl,"#Combination (%d) ignored because no case \n",k);
9057: fprintf(ficlog,"\nCombination (%d) ignored because no case \n",k);
1.220 brouard 9058: continue;
1.227 brouard 9059: }
1.219 brouard 9060:
9061: fprintf(ficrespl,"#Age ");
1.227 brouard 9062: for(j=1;j<=cptcoveff;j++) {
1.219 brouard 9063: fprintf(ficrespl,"V%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9064: }
9065: for(i=1; i<=nlstate;i++) fprintf(ficrespl," %d-%d ",i,i);
9066: fprintf(ficrespl,"Total Years_to_converge\n");
1.227 brouard 9067:
1.219 brouard 9068: for (age=agebase; age<=agelim; age++){
9069: /* for (age=agebase; age<=agebase; age++){ */
1.235 brouard 9070: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, ncvyearp, k, nres);
1.219 brouard 9071: fprintf(ficrespl,"%.0f ",age );
1.227 brouard 9072: for(j=1;j<=cptcoveff;j++)
9073: fprintf(ficrespl,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.219 brouard 9074: tot=0.;
9075: for(i=1; i<=nlstate;i++){
1.227 brouard 9076: tot += prlim[i][i];
9077: fprintf(ficrespl," %.5f", prlim[i][i]);
1.219 brouard 9078: }
9079: fprintf(ficrespl," %.3f %d\n", tot, *ncvyearp);
9080: } /* Age */
9081: /* was end of cptcod */
9082: } /* cptcov */
9083: return 0;
1.180 brouard 9084: }
9085:
1.218 brouard 9086: int back_prevalence_limit(double *p, double **bprlim, double ageminpar, double agemaxpar, double ftolpl, int *ncvyearp, double dateprev1,double dateprev2, int firstpass, int lastpass, int mobilavproj){
9087: /*--------------- Back Prevalence limit (period or stable prevalence) --------------*/
9088:
9089: /* Computes the back prevalence limit for any combination of covariate values
9090: * at any age between ageminpar and agemaxpar
9091: */
1.235 brouard 9092: int i, j, k, i1, nres=0 ;
1.217 brouard 9093: /* double ftolpl = 1.e-10; */
9094: double age, agebase, agelim;
9095: double tot;
1.218 brouard 9096: /* double ***mobaverage; */
9097: /* double **dnewm, **doldm, **dsavm; /\* for use *\/ */
1.217 brouard 9098:
9099: strcpy(fileresplb,"PLB_");
9100: strcat(fileresplb,fileresu);
9101: if((ficresplb=fopen(fileresplb,"w"))==NULL) {
9102: printf("Problem with period (stable) back prevalence resultfile: %s\n", fileresplb);return 1;
9103: fprintf(ficlog,"Problem with period (stable) back prevalence resultfile: %s\n", fileresplb);return 1;
9104: }
9105: printf("Computing period (stable) back prevalence: result on file '%s' \n", fileresplb);
9106: fprintf(ficlog,"Computing period (stable) back prevalence: result on file '%s' \n", fileresplb);
9107: pstamp(ficresplb);
9108: fprintf(ficresplb,"# Period (stable) back prevalence. Precision given by ftolpl=%g \n", ftolpl);
9109: fprintf(ficresplb,"#Age ");
9110: for(i=1; i<=nlstate;i++) fprintf(ficresplb,"%d-%d ",i,i);
9111: fprintf(ficresplb,"\n");
9112:
1.218 brouard 9113:
9114: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
9115:
9116: agebase=ageminpar;
9117: agelim=agemaxpar;
9118:
9119:
1.227 brouard 9120: i1=pow(2,cptcoveff);
1.218 brouard 9121: if (cptcovn < 1){i1=1;}
1.227 brouard 9122:
1.235 brouard 9123: for(nres=1; nres <= nresult; nres++) /* For each resultline */
9124: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying */
9125: if(TKresult[nres]!= k)
9126: continue;
1.218 brouard 9127: //printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));
9128: fprintf(ficresplb,"#******");
9129: printf("#******");
9130: fprintf(ficlog,"#******");
1.227 brouard 9131: for(j=1;j<=cptcoveff ;j++) {/* all covariates */
1.218 brouard 9132: fprintf(ficresplb," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9133: printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9134: fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9135: }
1.235 brouard 9136: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
9137: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
9138: fprintf(ficresplb," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
9139: fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
9140: }
1.218 brouard 9141: fprintf(ficresplb,"******\n");
9142: printf("******\n");
9143: fprintf(ficlog,"******\n");
1.227 brouard 9144: if(invalidvarcomb[k]){
9145: printf("\nCombination (%d) ignored because no cases \n",k);
9146: fprintf(ficresplb,"#Combination (%d) ignored because no cases \n",k);
9147: fprintf(ficlog,"\nCombination (%d) ignored because no cases \n",k);
9148: continue;
9149: }
1.218 brouard 9150:
9151: fprintf(ficresplb,"#Age ");
1.227 brouard 9152: for(j=1;j<=cptcoveff;j++) {
1.218 brouard 9153: fprintf(ficresplb,"V%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9154: }
9155: for(i=1; i<=nlstate;i++) fprintf(ficresplb," %d-%d ",i,i);
9156: fprintf(ficresplb,"Total Years_to_converge\n");
9157:
9158:
9159: for (age=agebase; age<=agelim; age++){
9160: /* for (age=agebase; age<=agebase; age++){ */
9161: if(mobilavproj > 0){
9162: /* bprevalim(bprlim, mobaverage, nlstate, p, age, ageminpar, agemaxpar, oldm, savm, doldm, dsavm, ftolpl, ncvyearp, k); */
9163: /* bprevalim(bprlim, mobaverage, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
1.227 brouard 9164: bprevalim(bprlim, mobaverage, nlstate, p, age, ftolpl, ncvyearp, k);
1.218 brouard 9165: }else if (mobilavproj == 0){
1.227 brouard 9166: printf("There is no chance to get back prevalence limit if data aren't non zero and summing to 1, please try a non null mobil_average(=%d) parameter or mobil_average=-1 if you want to try at your own risk.\n",mobilavproj);
9167: fprintf(ficlog,"There is no chance to get back prevalence limit if data aren't non zero and summing to 1, please try a non null mobil_average(=%d) parameter or mobil_average=-1 if you want to try at your own risk.\n",mobilavproj);
9168: exit(1);
1.218 brouard 9169: }else{
1.227 brouard 9170: /* bprevalim(bprlim, probs, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
9171: bprevalim(bprlim, probs, nlstate, p, age, ftolpl, ncvyearp, k);
1.218 brouard 9172: }
9173: fprintf(ficresplb,"%.0f ",age );
1.227 brouard 9174: for(j=1;j<=cptcoveff;j++)
9175: fprintf(ficresplb,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.218 brouard 9176: tot=0.;
9177: for(i=1; i<=nlstate;i++){
1.227 brouard 9178: tot += bprlim[i][i];
9179: fprintf(ficresplb," %.5f", bprlim[i][i]);
1.218 brouard 9180: }
9181: fprintf(ficresplb," %.3f %d\n", tot, *ncvyearp);
9182: } /* Age */
9183: /* was end of cptcod */
9184: } /* cptcov */
9185:
9186: /* hBijx(p, bage, fage); */
9187: /* fclose(ficrespijb); */
9188:
9189: return 0;
1.217 brouard 9190: }
1.218 brouard 9191:
1.180 brouard 9192: int hPijx(double *p, int bage, int fage){
9193: /*------------- h Pij x at various ages ------------*/
9194:
9195: int stepsize;
9196: int agelim;
9197: int hstepm;
9198: int nhstepm;
1.235 brouard 9199: int h, i, i1, j, k, k4, nres=0;
1.180 brouard 9200:
9201: double agedeb;
9202: double ***p3mat;
9203:
1.201 brouard 9204: strcpy(filerespij,"PIJ_"); strcat(filerespij,fileresu);
1.180 brouard 9205: if((ficrespij=fopen(filerespij,"w"))==NULL) {
9206: printf("Problem with Pij resultfile: %s\n", filerespij); return 1;
9207: fprintf(ficlog,"Problem with Pij resultfile: %s\n", filerespij); return 1;
9208: }
9209: printf("Computing pij: result on file '%s' \n", filerespij);
9210: fprintf(ficlog,"Computing pij: result on file '%s' \n", filerespij);
9211:
9212: stepsize=(int) (stepm+YEARM-1)/YEARM;
9213: /*if (stepm<=24) stepsize=2;*/
9214:
9215: agelim=AGESUP;
9216: hstepm=stepsize*YEARM; /* Every year of age */
9217: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
1.218 brouard 9218:
1.180 brouard 9219: /* hstepm=1; aff par mois*/
9220: pstamp(ficrespij);
9221: fprintf(ficrespij,"#****** h Pij x Probability to be in state j at age x+h being in i at x ");
1.227 brouard 9222: i1= pow(2,cptcoveff);
1.218 brouard 9223: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
9224: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
9225: /* k=k+1; */
1.235 brouard 9226: for(nres=1; nres <= nresult; nres++) /* For each resultline */
9227: for(k=1; k<=i1;k++){
9228: if(TKresult[nres]!= k)
9229: continue;
1.183 brouard 9230: fprintf(ficrespij,"\n#****** ");
1.227 brouard 9231: for(j=1;j<=cptcoveff;j++)
1.198 brouard 9232: fprintf(ficrespij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.235 brouard 9233: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
9234: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
9235: fprintf(ficrespij," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
9236: }
1.183 brouard 9237: fprintf(ficrespij,"******\n");
9238:
9239: for (agedeb=fage; agedeb>=bage; agedeb--){ /* If stepm=6 months */
9240: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
9241: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
9242:
9243: /* nhstepm=nhstepm*YEARM; aff par mois*/
1.180 brouard 9244:
1.183 brouard 9245: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
9246: oldm=oldms;savm=savms;
1.235 brouard 9247: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k, nres);
1.183 brouard 9248: fprintf(ficrespij,"# Cov Agex agex+h hpijx with i,j=");
9249: for(i=1; i<=nlstate;i++)
9250: for(j=1; j<=nlstate+ndeath;j++)
9251: fprintf(ficrespij," %1d-%1d",i,j);
9252: fprintf(ficrespij,"\n");
9253: for (h=0; h<=nhstepm; h++){
9254: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
9255: fprintf(ficrespij,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm );
1.180 brouard 9256: for(i=1; i<=nlstate;i++)
9257: for(j=1; j<=nlstate+ndeath;j++)
1.183 brouard 9258: fprintf(ficrespij," %.5f", p3mat[i][j][h]);
1.180 brouard 9259: fprintf(ficrespij,"\n");
9260: }
1.183 brouard 9261: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
9262: fprintf(ficrespij,"\n");
9263: }
1.180 brouard 9264: /*}*/
9265: }
1.218 brouard 9266: return 0;
1.180 brouard 9267: }
1.218 brouard 9268:
9269: int hBijx(double *p, int bage, int fage, double ***prevacurrent){
1.217 brouard 9270: /*------------- h Bij x at various ages ------------*/
9271:
9272: int stepsize;
1.218 brouard 9273: /* int agelim; */
9274: int ageminl;
1.217 brouard 9275: int hstepm;
9276: int nhstepm;
9277: int h, i, i1, j, k;
1.218 brouard 9278:
1.217 brouard 9279: double agedeb;
9280: double ***p3mat;
1.218 brouard 9281:
9282: strcpy(filerespijb,"PIJB_"); strcat(filerespijb,fileresu);
9283: if((ficrespijb=fopen(filerespijb,"w"))==NULL) {
9284: printf("Problem with Pij back resultfile: %s\n", filerespijb); return 1;
9285: fprintf(ficlog,"Problem with Pij back resultfile: %s\n", filerespijb); return 1;
9286: }
9287: printf("Computing pij back: result on file '%s' \n", filerespijb);
9288: fprintf(ficlog,"Computing pij back: result on file '%s' \n", filerespijb);
9289:
9290: stepsize=(int) (stepm+YEARM-1)/YEARM;
9291: /*if (stepm<=24) stepsize=2;*/
1.217 brouard 9292:
1.218 brouard 9293: /* agelim=AGESUP; */
9294: ageminl=30;
9295: hstepm=stepsize*YEARM; /* Every year of age */
9296: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
9297:
9298: /* hstepm=1; aff par mois*/
9299: pstamp(ficrespijb);
9300: fprintf(ficrespijb,"#****** h Pij x Back Probability to be in state i at age x-h being in j at x ");
1.227 brouard 9301: i1= pow(2,cptcoveff);
1.218 brouard 9302: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
9303: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
9304: /* k=k+1; */
1.227 brouard 9305: for (k=1; k <= (int) pow(2,cptcoveff); k++){
1.218 brouard 9306: fprintf(ficrespijb,"\n#****** ");
1.227 brouard 9307: for(j=1;j<=cptcoveff;j++)
1.218 brouard 9308: fprintf(ficrespijb,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9309: fprintf(ficrespijb,"******\n");
1.222 brouard 9310: if(invalidvarcomb[k]){
9311: fprintf(ficrespijb,"\n#Combination (%d) ignored because no cases \n",k);
9312: continue;
9313: }
1.218 brouard 9314:
9315: /* for (agedeb=fage; agedeb>=bage; agedeb--){ /\* If stepm=6 months *\/ */
9316: for (agedeb=bage; agedeb<=fage; agedeb++){ /* If stepm=6 months and estepm=24 (2 years) */
9317: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /\* Typically 20 years = 20*12/6=40 *\/ */
9318: nhstepm=(int) rint((agedeb-ageminl)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
9319: nhstepm = nhstepm/hstepm; /* Typically 40/4=10, because estepm=24 stepm=6 => hstepm=24/6=4 */
9320:
9321: /* nhstepm=nhstepm*YEARM; aff par mois*/
9322:
9323: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
9324: /* oldm=oldms;savm=savms; */
9325: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
9326: hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm, k);
9327: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm,oldm,savm, dnewm, doldm, dsavm, k); */
9328: fprintf(ficrespijb,"# Cov Agex agex-h hpijx with i,j=");
9329: for(i=1; i<=nlstate;i++)
9330: for(j=1; j<=nlstate+ndeath;j++)
9331: fprintf(ficrespijb," %1d-%1d",i,j);
9332: fprintf(ficrespijb,"\n");
9333: for (h=0; h<=nhstepm; h++){
9334: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
9335: fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb - h*hstepm/YEARM*stepm );
9336: /* fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm ); */
1.217 brouard 9337: for(i=1; i<=nlstate;i++)
9338: for(j=1; j<=nlstate+ndeath;j++)
1.218 brouard 9339: fprintf(ficrespijb," %.5f", p3mat[i][j][h]);
1.217 brouard 9340: fprintf(ficrespijb,"\n");
9341: }
1.218 brouard 9342: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
9343: fprintf(ficrespijb,"\n");
1.217 brouard 9344: }
1.218 brouard 9345: /*}*/
9346: }
9347: return 0;
9348: } /* hBijx */
1.217 brouard 9349:
1.180 brouard 9350:
1.136 brouard 9351: /***********************************************/
9352: /**************** Main Program *****************/
9353: /***********************************************/
9354:
9355: int main(int argc, char *argv[])
9356: {
9357: #ifdef GSL
9358: const gsl_multimin_fminimizer_type *T;
9359: size_t iteri = 0, it;
9360: int rval = GSL_CONTINUE;
9361: int status = GSL_SUCCESS;
9362: double ssval;
9363: #endif
9364: int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav);
1.164 brouard 9365: int i,j, k, n=MAXN,iter=0,m,size=100, cptcod;
1.209 brouard 9366: int ncvyear=0; /* Number of years needed for the period prevalence to converge */
1.164 brouard 9367: int jj, ll, li, lj, lk;
1.136 brouard 9368: int numlinepar=0; /* Current linenumber of parameter file */
1.197 brouard 9369: int num_filled;
1.136 brouard 9370: int itimes;
9371: int NDIM=2;
9372: int vpopbased=0;
1.235 brouard 9373: int nres=0;
1.136 brouard 9374:
1.164 brouard 9375: char ca[32], cb[32];
1.136 brouard 9376: /* FILE *fichtm; *//* Html File */
9377: /* FILE *ficgp;*/ /*Gnuplot File */
9378: struct stat info;
1.191 brouard 9379: double agedeb=0.;
1.194 brouard 9380:
9381: double ageminpar=AGEOVERFLOW,agemin=AGEOVERFLOW, agemaxpar=-AGEOVERFLOW, agemax=-AGEOVERFLOW;
1.219 brouard 9382: double ageminout=-AGEOVERFLOW,agemaxout=AGEOVERFLOW; /* Smaller Age range redefined after movingaverage */
1.136 brouard 9383:
1.165 brouard 9384: double fret;
1.191 brouard 9385: double dum=0.; /* Dummy variable */
1.136 brouard 9386: double ***p3mat;
1.218 brouard 9387: /* double ***mobaverage; */
1.164 brouard 9388:
9389: char line[MAXLINE];
1.197 brouard 9390: char path[MAXLINE],pathc[MAXLINE],pathcd[MAXLINE],pathtot[MAXLINE];
9391:
1.234 brouard 9392: char modeltemp[MAXLINE];
1.230 brouard 9393: char resultline[MAXLINE];
9394:
1.136 brouard 9395: char pathr[MAXLINE], pathimach[MAXLINE];
1.164 brouard 9396: char *tok, *val; /* pathtot */
1.136 brouard 9397: int firstobs=1, lastobs=10;
1.195 brouard 9398: int c, h , cpt, c2;
1.191 brouard 9399: int jl=0;
9400: int i1, j1, jk, stepsize=0;
1.194 brouard 9401: int count=0;
9402:
1.164 brouard 9403: int *tab;
1.136 brouard 9404: int mobilavproj=0 , prevfcast=0 ; /* moving average of prev, If prevfcast=1 prevalence projection */
1.217 brouard 9405: int backcast=0;
1.136 brouard 9406: int mobilav=0,popforecast=0;
1.191 brouard 9407: int hstepm=0, nhstepm=0;
1.136 brouard 9408: int agemortsup;
9409: float sumlpop=0.;
9410: double jprev1=1, mprev1=1,anprev1=2000,jprev2=1, mprev2=1,anprev2=2000;
9411: double jpyram=1, mpyram=1,anpyram=2000,jpyram1=1, mpyram1=1,anpyram1=2000;
9412:
1.191 brouard 9413: double bage=0, fage=110., age, agelim=0., agebase=0.;
1.136 brouard 9414: double ftolpl=FTOL;
9415: double **prlim;
1.217 brouard 9416: double **bprlim;
1.136 brouard 9417: double ***param; /* Matrix of parameters */
9418: double *p;
9419: double **matcov; /* Matrix of covariance */
1.203 brouard 9420: double **hess; /* Hessian matrix */
1.136 brouard 9421: double ***delti3; /* Scale */
9422: double *delti; /* Scale */
9423: double ***eij, ***vareij;
9424: double **varpl; /* Variances of prevalence limits by age */
9425: double *epj, vepp;
1.164 brouard 9426:
1.136 brouard 9427: double dateprev1, dateprev2,jproj1=1,mproj1=1,anproj1=2000,jproj2=1,mproj2=1,anproj2=2000;
1.217 brouard 9428: double jback1=1,mback1=1,anback1=2000,jback2=1,mback2=1,anback2=2000;
9429:
1.136 brouard 9430: double **ximort;
1.145 brouard 9431: char *alph[]={"a","a","b","c","d","e"}, str[4]="1234";
1.136 brouard 9432: int *dcwave;
9433:
1.164 brouard 9434: char z[1]="c";
1.136 brouard 9435:
9436: /*char *strt;*/
9437: char strtend[80];
1.126 brouard 9438:
1.164 brouard 9439:
1.126 brouard 9440: /* setlocale (LC_ALL, ""); */
9441: /* bindtextdomain (PACKAGE, LOCALEDIR); */
9442: /* textdomain (PACKAGE); */
9443: /* setlocale (LC_CTYPE, ""); */
9444: /* setlocale (LC_MESSAGES, ""); */
9445:
9446: /* gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */
1.157 brouard 9447: rstart_time = time(NULL);
9448: /* (void) gettimeofday(&start_time,&tzp);*/
9449: start_time = *localtime(&rstart_time);
1.126 brouard 9450: curr_time=start_time;
1.157 brouard 9451: /*tml = *localtime(&start_time.tm_sec);*/
9452: /* strcpy(strstart,asctime(&tml)); */
9453: strcpy(strstart,asctime(&start_time));
1.126 brouard 9454:
9455: /* printf("Localtime (at start)=%s",strstart); */
1.157 brouard 9456: /* tp.tm_sec = tp.tm_sec +86400; */
9457: /* tm = *localtime(&start_time.tm_sec); */
1.126 brouard 9458: /* tmg.tm_year=tmg.tm_year +dsign*dyear; */
9459: /* tmg.tm_mon=tmg.tm_mon +dsign*dmonth; */
9460: /* tmg.tm_hour=tmg.tm_hour + 1; */
1.157 brouard 9461: /* tp.tm_sec = mktime(&tmg); */
1.126 brouard 9462: /* strt=asctime(&tmg); */
9463: /* printf("Time(after) =%s",strstart); */
9464: /* (void) time (&time_value);
9465: * printf("time=%d,t-=%d\n",time_value,time_value-86400);
9466: * tm = *localtime(&time_value);
9467: * strstart=asctime(&tm);
9468: * printf("tim_value=%d,asctime=%s\n",time_value,strstart);
9469: */
9470:
9471: nberr=0; /* Number of errors and warnings */
9472: nbwarn=0;
1.184 brouard 9473: #ifdef WIN32
9474: _getcwd(pathcd, size);
9475: #else
1.126 brouard 9476: getcwd(pathcd, size);
1.184 brouard 9477: #endif
1.191 brouard 9478: syscompilerinfo(0);
1.196 brouard 9479: printf("\nIMaCh version %s, %s\n%s",version, copyright, fullversion);
1.126 brouard 9480: if(argc <=1){
9481: printf("\nEnter the parameter file name: ");
1.205 brouard 9482: if(!fgets(pathr,FILENAMELENGTH,stdin)){
9483: printf("ERROR Empty parameter file name\n");
9484: goto end;
9485: }
1.126 brouard 9486: i=strlen(pathr);
9487: if(pathr[i-1]=='\n')
9488: pathr[i-1]='\0';
1.156 brouard 9489: i=strlen(pathr);
1.205 brouard 9490: if(i >= 1 && pathr[i-1]==' ') {/* This may happen when dragging on oS/X! */
1.156 brouard 9491: pathr[i-1]='\0';
1.205 brouard 9492: }
9493: i=strlen(pathr);
9494: if( i==0 ){
9495: printf("ERROR Empty parameter file name\n");
9496: goto end;
9497: }
9498: for (tok = pathr; tok != NULL; ){
1.126 brouard 9499: printf("Pathr |%s|\n",pathr);
9500: while ((val = strsep(&tok, "\"" )) != NULL && *val == '\0');
9501: printf("val= |%s| pathr=%s\n",val,pathr);
9502: strcpy (pathtot, val);
9503: if(pathr[0] == '\0') break; /* Dirty */
9504: }
9505: }
9506: else{
9507: strcpy(pathtot,argv[1]);
9508: }
9509: /*if(getcwd(pathcd, MAXLINE)!= NULL)printf ("Error pathcd\n");*/
9510: /*cygwin_split_path(pathtot,path,optionfile);
9511: printf("pathtot=%s, path=%s, optionfile=%s\n",pathtot,path,optionfile);*/
9512: /* cutv(path,optionfile,pathtot,'\\');*/
9513:
9514: /* Split argv[0], imach program to get pathimach */
9515: printf("\nargv[0]=%s argv[1]=%s, \n",argv[0],argv[1]);
9516: split(argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
9517: printf("\nargv[0]=%s pathimach=%s, \noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
9518: /* strcpy(pathimach,argv[0]); */
9519: /* Split argv[1]=pathtot, parameter file name to get path, optionfile, extension and name */
9520: split(pathtot,path,optionfile,optionfilext,optionfilefiname);
9521: printf("\npathtot=%s,\npath=%s,\noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname);
1.184 brouard 9522: #ifdef WIN32
9523: _chdir(path); /* Can be a relative path */
9524: if(_getcwd(pathcd,MAXLINE) > 0) /* So pathcd is the full path */
9525: #else
1.126 brouard 9526: chdir(path); /* Can be a relative path */
1.184 brouard 9527: if (getcwd(pathcd, MAXLINE) > 0) /* So pathcd is the full path */
9528: #endif
9529: printf("Current directory %s!\n",pathcd);
1.126 brouard 9530: strcpy(command,"mkdir ");
9531: strcat(command,optionfilefiname);
9532: if((outcmd=system(command)) != 0){
1.169 brouard 9533: printf("Directory already exists (or can't create it) %s%s, err=%d\n",path,optionfilefiname,outcmd);
1.126 brouard 9534: /* fprintf(ficlog,"Problem creating directory %s%s\n",path,optionfilefiname); */
9535: /* fclose(ficlog); */
9536: /* exit(1); */
9537: }
9538: /* if((imk=mkdir(optionfilefiname))<0){ */
9539: /* perror("mkdir"); */
9540: /* } */
9541:
9542: /*-------- arguments in the command line --------*/
9543:
1.186 brouard 9544: /* Main Log file */
1.126 brouard 9545: strcat(filelog, optionfilefiname);
9546: strcat(filelog,".log"); /* */
9547: if((ficlog=fopen(filelog,"w"))==NULL) {
9548: printf("Problem with logfile %s\n",filelog);
9549: goto end;
9550: }
9551: fprintf(ficlog,"Log filename:%s\n",filelog);
1.197 brouard 9552: fprintf(ficlog,"Version %s %s",version,fullversion);
1.126 brouard 9553: fprintf(ficlog,"\nEnter the parameter file name: \n");
9554: fprintf(ficlog,"pathimach=%s\npathtot=%s\n\
9555: path=%s \n\
9556: optionfile=%s\n\
9557: optionfilext=%s\n\
1.156 brouard 9558: optionfilefiname='%s'\n",pathimach,pathtot,path,optionfile,optionfilext,optionfilefiname);
1.126 brouard 9559:
1.197 brouard 9560: syscompilerinfo(1);
1.167 brouard 9561:
1.126 brouard 9562: printf("Local time (at start):%s",strstart);
9563: fprintf(ficlog,"Local time (at start): %s",strstart);
9564: fflush(ficlog);
9565: /* (void) gettimeofday(&curr_time,&tzp); */
1.157 brouard 9566: /* printf("Elapsed time %d\n", asc_diff_time(curr_time.tm_sec-start_time.tm_sec,tmpout)); */
1.126 brouard 9567:
9568: /* */
9569: strcpy(fileres,"r");
9570: strcat(fileres, optionfilefiname);
1.201 brouard 9571: strcat(fileresu, optionfilefiname); /* Without r in front */
1.126 brouard 9572: strcat(fileres,".txt"); /* Other files have txt extension */
1.201 brouard 9573: strcat(fileresu,".txt"); /* Other files have txt extension */
1.126 brouard 9574:
1.186 brouard 9575: /* Main ---------arguments file --------*/
1.126 brouard 9576:
9577: if((ficpar=fopen(optionfile,"r"))==NULL) {
1.155 brouard 9578: printf("Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
9579: fprintf(ficlog,"Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
1.126 brouard 9580: fflush(ficlog);
1.149 brouard 9581: /* goto end; */
9582: exit(70);
1.126 brouard 9583: }
9584:
9585:
9586:
9587: strcpy(filereso,"o");
1.201 brouard 9588: strcat(filereso,fileresu);
1.126 brouard 9589: if((ficparo=fopen(filereso,"w"))==NULL) { /* opened on subdirectory */
9590: printf("Problem with Output resultfile: %s\n", filereso);
9591: fprintf(ficlog,"Problem with Output resultfile: %s\n", filereso);
9592: fflush(ficlog);
9593: goto end;
9594: }
9595:
9596: /* Reads comments: lines beginning with '#' */
9597: numlinepar=0;
1.197 brouard 9598:
9599: /* First parameter line */
9600: while(fgets(line, MAXLINE, ficpar)) {
9601: /* If line starts with a # it is a comment */
9602: if (line[0] == '#') {
9603: numlinepar++;
9604: fputs(line,stdout);
9605: fputs(line,ficparo);
9606: fputs(line,ficlog);
9607: continue;
9608: }else
9609: break;
9610: }
9611: if((num_filled=sscanf(line,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", \
9612: title, datafile, &lastobs, &firstpass,&lastpass)) !=EOF){
9613: if (num_filled != 5) {
9614: printf("Should be 5 parameters\n");
9615: }
1.126 brouard 9616: numlinepar++;
1.197 brouard 9617: printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
9618: }
9619: /* Second parameter line */
9620: while(fgets(line, MAXLINE, ficpar)) {
9621: /* If line starts with a # it is a comment */
9622: if (line[0] == '#') {
9623: numlinepar++;
9624: fputs(line,stdout);
9625: fputs(line,ficparo);
9626: fputs(line,ficlog);
9627: continue;
9628: }else
9629: break;
9630: }
1.223 brouard 9631: if((num_filled=sscanf(line,"ftol=%lf stepm=%d ncovcol=%d nqv=%d ntv=%d nqtv=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\n", \
9632: &ftol, &stepm, &ncovcol, &nqv, &ntv, &nqtv, &nlstate, &ndeath, &maxwav, &mle, &weightopt)) !=EOF){
9633: if (num_filled != 11) {
9634: printf("Not 11 parameters, for example:ftol=1.e-8 stepm=12 ncovcol=2 nqv=1 ntv=2 nqtv=1 nlstate=2 ndeath=1 maxwav=3 mle=1 weight=1\n");
1.209 brouard 9635: printf("but line=%s\n",line);
1.197 brouard 9636: }
1.223 brouard 9637: printf("ftol=%e stepm=%d ncovcol=%d nqv=%d ntv=%d nqtv=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\n",ftol, stepm, ncovcol, nqv, ntv, nqtv, nlstate, ndeath, maxwav, mle, weightopt);
1.126 brouard 9638: }
1.203 brouard 9639: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
1.209 brouard 9640: /*ftolpl=6.e-4; *//* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
1.197 brouard 9641: /* Third parameter line */
9642: while(fgets(line, MAXLINE, ficpar)) {
9643: /* If line starts with a # it is a comment */
9644: if (line[0] == '#') {
9645: numlinepar++;
9646: fputs(line,stdout);
9647: fputs(line,ficparo);
9648: fputs(line,ficlog);
9649: continue;
9650: }else
9651: break;
9652: }
1.201 brouard 9653: if((num_filled=sscanf(line,"model=1+age%[^.\n]", model)) !=EOF){
9654: if (num_filled == 0)
9655: model[0]='\0';
9656: else if (num_filled != 1){
1.197 brouard 9657: printf("ERROR %d: Model should be at minimum 'model=1+age.' %s\n",num_filled, line);
9658: fprintf(ficlog,"ERROR %d: Model should be at minimum 'model=1+age.' %s\n",num_filled, line);
9659: model[0]='\0';
9660: goto end;
9661: }
9662: else{
9663: if (model[0]=='+'){
9664: for(i=1; i<=strlen(model);i++)
9665: modeltemp[i-1]=model[i];
1.201 brouard 9666: strcpy(model,modeltemp);
1.197 brouard 9667: }
9668: }
1.199 brouard 9669: /* printf(" model=1+age%s modeltemp= %s, model=%s\n",model, modeltemp, model);fflush(stdout); */
1.203 brouard 9670: printf("model=1+age+%s\n",model);fflush(stdout);
1.197 brouard 9671: }
9672: /* 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=1+age+%s\n",title, datafile, &lastobs, &firstpass,&lastpass,&ftol, &stepm, &ncovcol, &nlstate,&ndeath, &maxwav, &mle, &weightopt,model); */
9673: /* numlinepar=numlinepar+3; /\* In general *\/ */
9674: /* 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=1+age+%s\n", title, datafile, lastobs, firstpass,lastpass,ftol, stepm, ncovcol, nlstate,ndeath, maxwav, mle, weightopt,model); */
1.223 brouard 9675: fprintf(ficparo,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nqv=%d ntv=%d nqtv=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=1+age+%s.\n", title, datafile, lastobs, firstpass,lastpass,ftol,stepm,ncovcol, nqv, ntv, nqtv, nlstate,ndeath,maxwav, mle, weightopt,model);
9676: fprintf(ficlog,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nqv=%d ntv=%d nqtv=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=1+age+%s.\n", title, datafile, lastobs, firstpass,lastpass,ftol,stepm,ncovcol, nqv, ntv, nqtv, nlstate,ndeath,maxwav, mle, weightopt,model);
1.126 brouard 9677: fflush(ficlog);
1.190 brouard 9678: /* if(model[0]=='#'|| model[0]== '\0'){ */
9679: if(model[0]=='#'){
1.187 brouard 9680: printf("Error in 'model' line: model should start with 'model=1+age+' and end with '.' \n \
9681: 'model=1+age+.' or 'model=1+age+V1.' or 'model=1+age+age*age+V1+V1*age.' or \n \
9682: 'model=1+age+V1+V2.' or 'model=1+age+V1+V2+V1*V2.' etc. \n"); \
9683: if(mle != -1){
9684: printf("Fix the model line and run imach with mle=-1 to get a correct template of the parameter file.\n");
9685: exit(1);
9686: }
9687: }
1.126 brouard 9688: while((c=getc(ficpar))=='#' && c!= EOF){
9689: ungetc(c,ficpar);
9690: fgets(line, MAXLINE, ficpar);
9691: numlinepar++;
1.195 brouard 9692: if(line[1]=='q'){ /* This #q will quit imach (the answer is q) */
9693: z[0]=line[1];
9694: }
9695: /* printf("****line [1] = %c \n",line[1]); */
1.141 brouard 9696: fputs(line, stdout);
9697: //puts(line);
1.126 brouard 9698: fputs(line,ficparo);
9699: fputs(line,ficlog);
9700: }
9701: ungetc(c,ficpar);
9702:
9703:
1.145 brouard 9704: covar=matrix(0,NCOVMAX,1,n); /**< used in readdata */
1.225 brouard 9705: coqvar=matrix(1,nqv,1,n); /**< Fixed quantitative covariate */
1.233 brouard 9706: cotvar=ma3x(1,maxwav,1,ntv+nqtv,1,n); /**< Time varying covariate (dummy and quantitative)*/
1.225 brouard 9707: cotqvar=ma3x(1,maxwav,1,nqtv,1,n); /**< Time varying quantitative covariate */
1.136 brouard 9708: cptcovn=0; /*Number of covariates, i.e. number of '+' in model statement plus one, indepently of n in Vn*/
9709: /* v1+v2+v3+v2*v4+v5*age makes cptcovn = 5
9710: v1+v2*age+v2*v3 makes cptcovn = 3
9711: */
9712: if (strlen(model)>1)
1.187 brouard 9713: 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,age*age makes 3*/
1.145 brouard 9714: else
1.187 brouard 9715: ncovmodel=2; /* Constant and age */
1.133 brouard 9716: nforce= (nlstate+ndeath-1)*nlstate; /* Number of forces ij from state i to j */
9717: npar= nforce*ncovmodel; /* Number of parameters like aij*/
1.131 brouard 9718: if(npar >MAXPARM || nlstate >NLSTATEMAX || ndeath >NDEATHMAX || ncovmodel>NCOVMAX){
9719: 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);
9720: 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);
9721: fflush(stdout);
9722: fclose (ficlog);
9723: goto end;
9724: }
1.126 brouard 9725: delti3= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
9726: delti=delti3[1][1];
9727: /*delti=vector(1,npar); *//* Scale of each paramater (output from hesscov)*/
9728: if(mle==-1){ /* Print a wizard for help writing covariance matrix */
9729: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
1.191 brouard 9730: printf(" You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
9731: fprintf(ficlog," You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
1.126 brouard 9732: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
9733: fclose (ficparo);
9734: fclose (ficlog);
9735: goto end;
9736: exit(0);
1.220 brouard 9737: } else if(mle==-5) { /* Main Wizard */
1.126 brouard 9738: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
1.192 brouard 9739: printf(" You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
9740: fprintf(ficlog," You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
1.126 brouard 9741: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
9742: matcov=matrix(1,npar,1,npar);
1.203 brouard 9743: hess=matrix(1,npar,1,npar);
1.220 brouard 9744: } else{ /* Begin of mle != -1 or -5 */
1.145 brouard 9745: /* Read guessed parameters */
1.126 brouard 9746: /* Reads comments: lines beginning with '#' */
9747: while((c=getc(ficpar))=='#' && c!= EOF){
9748: ungetc(c,ficpar);
9749: fgets(line, MAXLINE, ficpar);
9750: numlinepar++;
1.141 brouard 9751: fputs(line,stdout);
1.126 brouard 9752: fputs(line,ficparo);
9753: fputs(line,ficlog);
9754: }
9755: ungetc(c,ficpar);
9756:
9757: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
9758: for(i=1; i <=nlstate; i++){
1.234 brouard 9759: j=0;
1.126 brouard 9760: for(jj=1; jj <=nlstate+ndeath; jj++){
1.234 brouard 9761: if(jj==i) continue;
9762: j++;
9763: fscanf(ficpar,"%1d%1d",&i1,&j1);
9764: if ((i1 != i) || (j1 != jj)){
9765: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n \
1.126 brouard 9766: It might be a problem of design; if ncovcol and the model are correct\n \
9767: run imach with mle=-1 to get a correct template of the parameter file.\n",numlinepar, i,j, i1, j1);
1.234 brouard 9768: exit(1);
9769: }
9770: fprintf(ficparo,"%1d%1d",i1,j1);
9771: if(mle==1)
9772: printf("%1d%1d",i,jj);
9773: fprintf(ficlog,"%1d%1d",i,jj);
9774: for(k=1; k<=ncovmodel;k++){
9775: fscanf(ficpar," %lf",¶m[i][j][k]);
9776: if(mle==1){
9777: printf(" %lf",param[i][j][k]);
9778: fprintf(ficlog," %lf",param[i][j][k]);
9779: }
9780: else
9781: fprintf(ficlog," %lf",param[i][j][k]);
9782: fprintf(ficparo," %lf",param[i][j][k]);
9783: }
9784: fscanf(ficpar,"\n");
9785: numlinepar++;
9786: if(mle==1)
9787: printf("\n");
9788: fprintf(ficlog,"\n");
9789: fprintf(ficparo,"\n");
1.126 brouard 9790: }
9791: }
9792: fflush(ficlog);
1.234 brouard 9793:
1.145 brouard 9794: /* Reads scales values */
1.126 brouard 9795: p=param[1][1];
9796:
9797: /* Reads comments: lines beginning with '#' */
9798: while((c=getc(ficpar))=='#' && c!= EOF){
9799: ungetc(c,ficpar);
9800: fgets(line, MAXLINE, ficpar);
9801: numlinepar++;
1.141 brouard 9802: fputs(line,stdout);
1.126 brouard 9803: fputs(line,ficparo);
9804: fputs(line,ficlog);
9805: }
9806: ungetc(c,ficpar);
9807:
9808: for(i=1; i <=nlstate; i++){
9809: for(j=1; j <=nlstate+ndeath-1; j++){
1.234 brouard 9810: fscanf(ficpar,"%1d%1d",&i1,&j1);
9811: if ( (i1-i) * (j1-j) != 0){
9812: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n",numlinepar, i,j, i1, j1);
9813: exit(1);
9814: }
9815: printf("%1d%1d",i,j);
9816: fprintf(ficparo,"%1d%1d",i1,j1);
9817: fprintf(ficlog,"%1d%1d",i1,j1);
9818: for(k=1; k<=ncovmodel;k++){
9819: fscanf(ficpar,"%le",&delti3[i][j][k]);
9820: printf(" %le",delti3[i][j][k]);
9821: fprintf(ficparo," %le",delti3[i][j][k]);
9822: fprintf(ficlog," %le",delti3[i][j][k]);
9823: }
9824: fscanf(ficpar,"\n");
9825: numlinepar++;
9826: printf("\n");
9827: fprintf(ficparo,"\n");
9828: fprintf(ficlog,"\n");
1.126 brouard 9829: }
9830: }
9831: fflush(ficlog);
1.234 brouard 9832:
1.145 brouard 9833: /* Reads covariance matrix */
1.126 brouard 9834: delti=delti3[1][1];
1.220 brouard 9835:
9836:
1.126 brouard 9837: /* free_ma3x(delti3,1,nlstate,1,nlstate+ndeath-1,1,ncovmodel); */ /* Hasn't to to freed here otherwise delti is no more allocated */
1.220 brouard 9838:
1.126 brouard 9839: /* Reads comments: lines beginning with '#' */
9840: while((c=getc(ficpar))=='#' && c!= EOF){
9841: ungetc(c,ficpar);
9842: fgets(line, MAXLINE, ficpar);
9843: numlinepar++;
1.141 brouard 9844: fputs(line,stdout);
1.126 brouard 9845: fputs(line,ficparo);
9846: fputs(line,ficlog);
9847: }
9848: ungetc(c,ficpar);
1.220 brouard 9849:
1.126 brouard 9850: matcov=matrix(1,npar,1,npar);
1.203 brouard 9851: hess=matrix(1,npar,1,npar);
1.131 brouard 9852: for(i=1; i <=npar; i++)
9853: for(j=1; j <=npar; j++) matcov[i][j]=0.;
1.220 brouard 9854:
1.194 brouard 9855: /* Scans npar lines */
1.126 brouard 9856: for(i=1; i <=npar; i++){
1.226 brouard 9857: count=fscanf(ficpar,"%1d%1d%d",&i1,&j1,&jk);
1.194 brouard 9858: if(count != 3){
1.226 brouard 9859: printf("Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
1.194 brouard 9860: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
9861: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
1.226 brouard 9862: fprintf(ficlog,"Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
1.194 brouard 9863: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
9864: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
1.226 brouard 9865: exit(1);
1.220 brouard 9866: }else{
1.226 brouard 9867: if(mle==1)
9868: printf("%1d%1d%d",i1,j1,jk);
9869: }
9870: fprintf(ficlog,"%1d%1d%d",i1,j1,jk);
9871: fprintf(ficparo,"%1d%1d%d",i1,j1,jk);
1.126 brouard 9872: for(j=1; j <=i; j++){
1.226 brouard 9873: fscanf(ficpar," %le",&matcov[i][j]);
9874: if(mle==1){
9875: printf(" %.5le",matcov[i][j]);
9876: }
9877: fprintf(ficlog," %.5le",matcov[i][j]);
9878: fprintf(ficparo," %.5le",matcov[i][j]);
1.126 brouard 9879: }
9880: fscanf(ficpar,"\n");
9881: numlinepar++;
9882: if(mle==1)
1.220 brouard 9883: printf("\n");
1.126 brouard 9884: fprintf(ficlog,"\n");
9885: fprintf(ficparo,"\n");
9886: }
1.194 brouard 9887: /* End of read covariance matrix npar lines */
1.126 brouard 9888: for(i=1; i <=npar; i++)
9889: for(j=i+1;j<=npar;j++)
1.226 brouard 9890: matcov[i][j]=matcov[j][i];
1.126 brouard 9891:
9892: if(mle==1)
9893: printf("\n");
9894: fprintf(ficlog,"\n");
9895:
9896: fflush(ficlog);
9897:
9898: /*-------- Rewriting parameter file ----------*/
9899: strcpy(rfileres,"r"); /* "Rparameterfile */
9900: strcat(rfileres,optionfilefiname); /* Parameter file first name*/
9901: strcat(rfileres,"."); /* */
9902: strcat(rfileres,optionfilext); /* Other files have txt extension */
9903: if((ficres =fopen(rfileres,"w"))==NULL) {
1.201 brouard 9904: printf("Problem writing new parameter file: %s\n", rfileres);goto end;
9905: fprintf(ficlog,"Problem writing new parameter file: %s\n", rfileres);goto end;
1.126 brouard 9906: }
9907: fprintf(ficres,"#%s\n",version);
9908: } /* End of mle != -3 */
1.218 brouard 9909:
1.186 brouard 9910: /* Main data
9911: */
1.126 brouard 9912: n= lastobs;
9913: num=lvector(1,n);
9914: moisnais=vector(1,n);
9915: annais=vector(1,n);
9916: moisdc=vector(1,n);
9917: andc=vector(1,n);
1.220 brouard 9918: weight=vector(1,n);
1.126 brouard 9919: agedc=vector(1,n);
9920: cod=ivector(1,n);
1.220 brouard 9921: for(i=1;i<=n;i++){
1.234 brouard 9922: num[i]=0;
9923: moisnais[i]=0;
9924: annais[i]=0;
9925: moisdc[i]=0;
9926: andc[i]=0;
9927: agedc[i]=0;
9928: cod[i]=0;
9929: weight[i]=1.0; /* Equal weights, 1 by default */
9930: }
1.126 brouard 9931: mint=matrix(1,maxwav,1,n);
9932: anint=matrix(1,maxwav,1,n);
1.131 brouard 9933: s=imatrix(1,maxwav+1,1,n); /* s[i][j] health state for wave i and individual j */
1.126 brouard 9934: tab=ivector(1,NCOVMAX);
1.144 brouard 9935: ncodemax=ivector(1,NCOVMAX); /* Number of code per covariate; if O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */
1.192 brouard 9936: ncodemaxwundef=ivector(1,NCOVMAX); /* Number of code per covariate; if - 1 O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */
1.126 brouard 9937:
1.136 brouard 9938: /* Reads data from file datafile */
9939: if (readdata(datafile, firstobs, lastobs, &imx)==1)
9940: goto end;
9941:
9942: /* Calculation of the number of parameters from char model */
1.234 brouard 9943: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4
1.137 brouard 9944: k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tag[cptcovage=1]=4
9945: k=3 V4 Tvar[k=3]= 4 (from V4)
9946: k=2 V1 Tvar[k=2]= 1 (from V1)
9947: k=1 Tvar[1]=2 (from V2)
1.234 brouard 9948: */
9949:
9950: Tvar=ivector(1,NCOVMAX); /* Was 15 changed to NCOVMAX. */
9951: TvarsDind=ivector(1,NCOVMAX); /* */
9952: TvarsD=ivector(1,NCOVMAX); /* */
9953: TvarsQind=ivector(1,NCOVMAX); /* */
9954: TvarsQ=ivector(1,NCOVMAX); /* */
1.232 brouard 9955: TvarF=ivector(1,NCOVMAX); /* */
9956: TvarFind=ivector(1,NCOVMAX); /* */
9957: TvarV=ivector(1,NCOVMAX); /* */
9958: TvarVind=ivector(1,NCOVMAX); /* */
9959: TvarA=ivector(1,NCOVMAX); /* */
9960: TvarAind=ivector(1,NCOVMAX); /* */
1.231 brouard 9961: TvarFD=ivector(1,NCOVMAX); /* */
9962: TvarFDind=ivector(1,NCOVMAX); /* */
9963: TvarFQ=ivector(1,NCOVMAX); /* */
9964: TvarFQind=ivector(1,NCOVMAX); /* */
9965: TvarVD=ivector(1,NCOVMAX); /* */
9966: TvarVDind=ivector(1,NCOVMAX); /* */
9967: TvarVQ=ivector(1,NCOVMAX); /* */
9968: TvarVQind=ivector(1,NCOVMAX); /* */
9969:
1.230 brouard 9970: Tvalsel=vector(1,NCOVMAX); /* */
1.233 brouard 9971: Tvarsel=ivector(1,NCOVMAX); /* */
1.226 brouard 9972: Typevar=ivector(-1,NCOVMAX); /* -1 to 2 */
9973: Fixed=ivector(-1,NCOVMAX); /* -1 to 3 */
9974: Dummy=ivector(-1,NCOVMAX); /* -1 to 3 */
1.137 brouard 9975: /* V2+V1+V4+age*V3 is a model with 4 covariates (3 plus signs).
9976: For each model-covariate stores the data-covariate id. Tvar[1]=2, Tvar[2]=1, Tvar[3]=4,
9977: Tvar[4=age*V3] is 3 and 'age' is recorded in Tage.
9978: */
9979: /* For model-covariate k tells which data-covariate to use but
9980: because this model-covariate is a construction we invent a new column
9981: ncovcol + k1
9982: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3
9983: Tvar[3=V1*V4]=4+1 etc */
1.227 brouard 9984: Tprod=ivector(1,NCOVMAX); /* Gives the k position of the k1 product */
9985: Tposprod=ivector(1,NCOVMAX); /* Gives the k1 product from the k position */
1.137 brouard 9986: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3
9987: if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2)
1.227 brouard 9988: Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5]=2
1.137 brouard 9989: */
1.145 brouard 9990: Tvaraff=ivector(1,NCOVMAX); /* Unclear */
9991: 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 9992: * For V3*V2 (in V2+V1+V1*V4+age*V3+V3*V2), V3*V2 position is 2nd.
9993: * Tvard[k1=2][1]=3 (V3) Tvard[k1=2][2]=2(V2) */
1.145 brouard 9994: Tage=ivector(1,NCOVMAX); /* Gives the covariate id of covariates associated with age: V2 + V1 + age*V4 + V3*age
1.137 brouard 9995: 4 covariates (3 plus signs)
9996: Tage[1=V3*age]= 4; Tage[2=age*V4] = 3
9997: */
1.230 brouard 9998: Tmodelind=ivector(1,NCOVMAX);/** gives the k model position of an
1.227 brouard 9999: * individual dummy, fixed or varying:
10000: * Tmodelind[Tvaraff[3]]=9,Tvaraff[1]@9={4,
10001: * 3, 1, 0, 0, 0, 0, 0, 0},
1.230 brouard 10002: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 ,
10003: * V1 df, V2 qf, V3 & V4 dv, V5 qv
10004: * Tmodelind[1]@9={9,0,3,2,}*/
10005: TmodelInvind=ivector(1,NCOVMAX); /* TmodelInvind=Tvar[k]- ncovcol-nqv={5-2-1=2,*/
10006: TmodelInvQind=ivector(1,NCOVMAX);/** gives the k model position of an
1.228 brouard 10007: * individual quantitative, fixed or varying:
10008: * Tmodelqind[1]=1,Tvaraff[1]@9={4,
10009: * 3, 1, 0, 0, 0, 0, 0, 0},
10010: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1*/
1.186 brouard 10011: /* Main decodemodel */
10012:
1.187 brouard 10013:
1.223 brouard 10014: if(decodemodel(model, lastobs) == 1) /* In order to get Tvar[k] V4+V3+V5 p Tvar[1]@3 = {4, 3, 5}*/
1.136 brouard 10015: goto end;
10016:
1.137 brouard 10017: if((double)(lastobs-imx)/(double)imx > 1.10){
10018: nbwarn++;
10019: 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);
10020: 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);
10021: }
1.136 brouard 10022: /* if(mle==1){*/
1.137 brouard 10023: if (weightopt != 1) { /* Maximisation without weights. We can have weights different from 1 but want no weight*/
10024: for(i=1;i<=imx;i++) weight[i]=1.0; /* changed to imx */
1.136 brouard 10025: }
10026:
10027: /*-calculation of age at interview from date of interview and age at death -*/
10028: agev=matrix(1,maxwav,1,imx);
10029:
10030: if(calandcheckages(imx, maxwav, &agemin, &agemax, &nberr, &nbwarn) == 1)
10031: goto end;
10032:
1.126 brouard 10033:
1.136 brouard 10034: agegomp=(int)agemin;
10035: free_vector(moisnais,1,n);
10036: free_vector(annais,1,n);
1.126 brouard 10037: /* free_matrix(mint,1,maxwav,1,n);
10038: free_matrix(anint,1,maxwav,1,n);*/
1.215 brouard 10039: /* free_vector(moisdc,1,n); */
10040: /* free_vector(andc,1,n); */
1.145 brouard 10041: /* */
10042:
1.126 brouard 10043: wav=ivector(1,imx);
1.214 brouard 10044: /* dh=imatrix(1,lastpass-firstpass+1,1,imx); */
10045: /* bh=imatrix(1,lastpass-firstpass+1,1,imx); */
10046: /* mw=imatrix(1,lastpass-firstpass+1,1,imx); */
10047: dh=imatrix(1,lastpass-firstpass+2,1,imx); /* We are adding a wave if status is unknown at last wave but death occurs after last wave.*/
10048: bh=imatrix(1,lastpass-firstpass+2,1,imx);
10049: mw=imatrix(1,lastpass-firstpass+2,1,imx);
1.126 brouard 10050:
10051: /* Concatenates waves */
1.214 brouard 10052: /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
10053: Death is a valid wave (if date is known).
10054: mw[mi][i] is the number of (mi=1 to wav[i]) effective wave out of mi of individual i
10055: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
10056: and mw[mi+1][i]. dh depends on stepm.
10057: */
10058:
1.126 brouard 10059: concatwav(wav, dh, bh, mw, s, agedc, agev, firstpass, lastpass, imx, nlstate, stepm);
1.145 brouard 10060: /* */
10061:
1.215 brouard 10062: free_vector(moisdc,1,n);
10063: free_vector(andc,1,n);
10064:
1.126 brouard 10065: /* Routine tricode is to calculate cptcoveff (real number of unique covariates) and to associate covariable number and modality */
10066: nbcode=imatrix(0,NCOVMAX,0,NCOVMAX);
10067: ncodemax[1]=1;
1.145 brouard 10068: Ndum =ivector(-1,NCOVMAX);
1.225 brouard 10069: cptcoveff=0;
1.220 brouard 10070: if (ncovmodel-nagesqr > 2 ){ /* That is if covariate other than cst, age and age*age */
10071: tricode(&cptcoveff,Tvar,nbcode,imx, Ndum); /**< Fills nbcode[Tvar[j]][l]; */
1.227 brouard 10072: }
10073:
10074: ncovcombmax=pow(2,cptcoveff);
10075: invalidvarcomb=ivector(1, ncovcombmax);
10076: for(i=1;i<ncovcombmax;i++)
10077: invalidvarcomb[i]=0;
10078:
1.211 brouard 10079: /* Nbcode gives the value of the lth modality (currently 1 to 2) of jth covariate, in
1.186 brouard 10080: V2+V1*age, there are 3 covariates Tvar[2]=1 (V1).*/
1.211 brouard 10081: /* 1 to ncodemax[j] which is the maximum value of this jth covariate */
1.227 brouard 10082:
1.200 brouard 10083: /* codtab=imatrix(1,100,1,10);*/ /* codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) */
1.198 brouard 10084: /*printf(" codtab[1,1],codtab[100,10]=%d,%d\n", codtab[1][1],codtabm(100,10));*/
1.186 brouard 10085: /* codtab gives the value 1 or 2 of the hth combination of k covariates (1 or 2).*/
1.211 brouard 10086: /* nbcode[Tvaraff[j]][codtabm(h,j)]) : if there are only 2 modalities for a covariate j,
10087: * codtabm(h,j) gives its value classified at position h and nbcode gives how it is coded
10088: * (currently 0 or 1) in the data.
10089: * In a loop on h=1 to 2**k, and a loop on j (=1 to k), we get the value of
10090: * corresponding modality (h,j).
10091: */
10092:
1.145 brouard 10093: h=0;
10094: /*if (cptcovn > 0) */
1.126 brouard 10095: m=pow(2,cptcoveff);
10096:
1.144 brouard 10097: /**< codtab(h,k) k = codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) + 1
1.211 brouard 10098: * For k=4 covariates, h goes from 1 to m=2**k
10099: * codtabm(h,k)= (1 & (h-1) >> (k-1)) + 1;
10100: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1.186 brouard 10101: * h\k 1 2 3 4
1.143 brouard 10102: *______________________________
10103: * 1 i=1 1 i=1 1 i=1 1 i=1 1
10104: * 2 2 1 1 1
10105: * 3 i=2 1 2 1 1
10106: * 4 2 2 1 1
10107: * 5 i=3 1 i=2 1 2 1
10108: * 6 2 1 2 1
10109: * 7 i=4 1 2 2 1
10110: * 8 2 2 2 1
1.197 brouard 10111: * 9 i=5 1 i=3 1 i=2 1 2
10112: * 10 2 1 1 2
10113: * 11 i=6 1 2 1 2
10114: * 12 2 2 1 2
10115: * 13 i=7 1 i=4 1 2 2
10116: * 14 2 1 2 2
10117: * 15 i=8 1 2 2 2
10118: * 16 2 2 2 2
1.143 brouard 10119: */
1.212 brouard 10120: /* How to do the opposite? From combination h (=1 to 2**k) how to get the value on the covariates? */
1.211 brouard 10121: /* from h=5 and m, we get then number of covariates k=log(m)/log(2)=4
10122: * and the value of each covariate?
10123: * V1=1, V2=1, V3=2, V4=1 ?
10124: * h-1=4 and 4 is 0100 or reverse 0010, and +1 is 1121 ok.
10125: * h=6, 6-1=5, 5 is 0101, 1010, 2121, V1=2nd, V2=1st, V3=2nd, V4=1st.
10126: * In order to get the real value in the data, we use nbcode
10127: * nbcode[Tvar[3][2nd]]=1 and nbcode[Tvar[4][1]]=0
10128: * We are keeping this crazy system in order to be able (in the future?)
10129: * to have more than 2 values (0 or 1) for a covariate.
10130: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
10131: * h=6, k=2? h-1=5=0101, reverse 1010, +1=2121, k=2nd position: value is 1: codtabm(6,2)=1
10132: * bbbbbbbb
10133: * 76543210
10134: * h-1 00000101 (6-1=5)
1.219 brouard 10135: *(h-1)>>(k-1)= 00000010 >> (2-1) = 1 right shift
1.211 brouard 10136: * &
10137: * 1 00000001 (1)
1.219 brouard 10138: * 00000000 = 1 & ((h-1) >> (k-1))
10139: * +1= 00000001 =1
1.211 brouard 10140: *
10141: * h=14, k=3 => h'=h-1=13, k'=k-1=2
10142: * h' 1101 =2^3+2^2+0x2^1+2^0
10143: * >>k' 11
10144: * & 00000001
10145: * = 00000001
10146: * +1 = 00000010=2 = codtabm(14,3)
10147: * Reverse h=6 and m=16?
10148: * cptcoveff=log(16)/log(2)=4 covariate: 6-1=5=0101 reversed=1010 +1=2121 =>V1=2, V2=1, V3=2, V4=1.
10149: * for (j=1 to cptcoveff) Vj=decodtabm(j,h,cptcoveff)
10150: * decodtabm(h,j,cptcoveff)= (((h-1) >> (j-1)) & 1) +1
10151: * decodtabm(h,j,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (j-1)) & 1) +1 : -1)
10152: * V3=decodtabm(14,3,2**4)=2
10153: * h'=13 1101 =2^3+2^2+0x2^1+2^0
10154: *(h-1) >> (j-1) 0011 =13 >> 2
10155: * &1 000000001
10156: * = 000000001
10157: * +1= 000000010 =2
10158: * 2211
10159: * V1=1+1, V2=0+1, V3=1+1, V4=1+1
10160: * V3=2
1.220 brouard 10161: * codtabm and decodtabm are identical
1.211 brouard 10162: */
10163:
1.145 brouard 10164:
10165: free_ivector(Ndum,-1,NCOVMAX);
10166:
10167:
1.126 brouard 10168:
1.186 brouard 10169: /* Initialisation of ----------- gnuplot -------------*/
1.126 brouard 10170: strcpy(optionfilegnuplot,optionfilefiname);
10171: if(mle==-3)
1.201 brouard 10172: strcat(optionfilegnuplot,"-MORT_");
1.126 brouard 10173: strcat(optionfilegnuplot,".gp");
10174:
10175: if((ficgp=fopen(optionfilegnuplot,"w"))==NULL) {
10176: printf("Problem with file %s",optionfilegnuplot);
10177: }
10178: else{
1.204 brouard 10179: fprintf(ficgp,"\n# IMaCh-%s\n", version);
1.126 brouard 10180: fprintf(ficgp,"# %s\n", optionfilegnuplot);
1.141 brouard 10181: //fprintf(ficgp,"set missing 'NaNq'\n");
10182: fprintf(ficgp,"set datafile missing 'NaNq'\n");
1.126 brouard 10183: }
10184: /* fclose(ficgp);*/
1.186 brouard 10185:
10186:
10187: /* Initialisation of --------- index.htm --------*/
1.126 brouard 10188:
10189: strcpy(optionfilehtm,optionfilefiname); /* Main html file */
10190: if(mle==-3)
1.201 brouard 10191: strcat(optionfilehtm,"-MORT_");
1.126 brouard 10192: strcat(optionfilehtm,".htm");
10193: if((fichtm=fopen(optionfilehtm,"w"))==NULL) {
1.131 brouard 10194: printf("Problem with %s \n",optionfilehtm);
10195: exit(0);
1.126 brouard 10196: }
10197:
10198: strcpy(optionfilehtmcov,optionfilefiname); /* Only for matrix of covariance */
10199: strcat(optionfilehtmcov,"-cov.htm");
10200: if((fichtmcov=fopen(optionfilehtmcov,"w"))==NULL) {
10201: printf("Problem with %s \n",optionfilehtmcov), exit(0);
10202: }
10203: else{
10204: fprintf(fichtmcov,"<html><head>\n<title>IMaCh Cov %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
10205: <hr size=\"2\" color=\"#EC5E5E\"> \n\
1.204 brouard 10206: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.126 brouard 10207: optionfilehtmcov,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
10208: }
10209:
1.213 brouard 10210: fprintf(fichtm,"<html><head>\n<head>\n<meta charset=\"utf-8\"/><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n<title>IMaCh %s</title></head>\n <body><font size=\"7\"><a href=http:/euroreves.ined.fr/imach>IMaCh for Interpolated Markov Chain</a> </font><br>\n<font size=\"3\">Sponsored by Copyright (C) 2002-2015 <a href=http://www.ined.fr>INED</a>-EUROREVES-Institut de longévité-2013-2016-Japan Society for the Promotion of Sciences 日本学術振興会 (<a href=https://www.jsps.go.jp/english/e-grants/>Grant-in-Aid for Scientific Research 25293121</a>) - <a href=https://software.intel.com/en-us>Intel Software 2015-2018</a></font><br> \
1.204 brouard 10211: <hr size=\"2\" color=\"#EC5E5E\"> \n\
10212: <font size=\"2\">IMaCh-%s <br> %s</font> \
1.126 brouard 10213: <hr size=\"2\" color=\"#EC5E5E\"> \n\
1.204 brouard 10214: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n\
1.126 brouard 10215: \n\
10216: <hr size=\"2\" color=\"#EC5E5E\">\
10217: <ul><li><h4>Parameter files</h4>\n\
10218: - Parameter file: <a href=\"%s.%s\">%s.%s</a><br>\n\
10219: - Copy of the parameter file: <a href=\"o%s\">o%s</a><br>\n\
10220: - Log file of the run: <a href=\"%s\">%s</a><br>\n\
10221: - Gnuplot file name: <a href=\"%s\">%s</a><br>\n\
10222: - Date and time at start: %s</ul>\n",\
10223: optionfilehtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model,\
10224: optionfilefiname,optionfilext,optionfilefiname,optionfilext,\
10225: fileres,fileres,\
10226: filelog,filelog,optionfilegnuplot,optionfilegnuplot,strstart);
10227: fflush(fichtm);
10228:
10229: strcpy(pathr,path);
10230: strcat(pathr,optionfilefiname);
1.184 brouard 10231: #ifdef WIN32
10232: _chdir(optionfilefiname); /* Move to directory named optionfile */
10233: #else
1.126 brouard 10234: chdir(optionfilefiname); /* Move to directory named optionfile */
1.184 brouard 10235: #endif
10236:
1.126 brouard 10237:
1.220 brouard 10238: /* Calculates basic frequencies. Computes observed prevalence at single age
10239: and for any valid combination of covariates
1.126 brouard 10240: and prints on file fileres'p'. */
1.227 brouard 10241: freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx, Tvaraff, invalidvarcomb, nbcode, ncodemax,mint,anint,strstart, \
10242: firstpass, lastpass, stepm, weightopt, model);
1.126 brouard 10243:
10244: fprintf(fichtm,"\n");
10245: fprintf(fichtm,"<br>Total number of observations=%d <br>\n\
10246: Youngest age at first (selected) pass %.2f, oldest age %.2f<br>\n\
10247: Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n",\
10248: imx,agemin,agemax,jmin,jmax,jmean);
10249: pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
1.220 brouard 10250: oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
10251: newms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
10252: savms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
10253: oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */
1.218 brouard 10254:
1.126 brouard 10255: /* For Powell, parameters are in a vector p[] starting at p[1]
10256: so we point p on param[1][1] so that p[1] maps on param[1][1][1] */
10257: p=param[1][1]; /* *(*(*(param +1)+1)+0) */
10258:
10259: globpr=0; /* To get the number ipmx of contributions and the sum of weights*/
1.186 brouard 10260: /* For mortality only */
1.126 brouard 10261: if (mle==-3){
1.136 brouard 10262: ximort=matrix(1,NDIM,1,NDIM);
1.220 brouard 10263: for(i=1;i<=NDIM;i++)
10264: for(j=1;j<=NDIM;j++)
10265: ximort[i][j]=0.;
1.186 brouard 10266: /* ximort=gsl_matrix_alloc(1,NDIM,1,NDIM); */
1.126 brouard 10267: cens=ivector(1,n);
10268: ageexmed=vector(1,n);
10269: agecens=vector(1,n);
10270: dcwave=ivector(1,n);
1.223 brouard 10271:
1.126 brouard 10272: for (i=1; i<=imx; i++){
10273: dcwave[i]=-1;
10274: for (m=firstpass; m<=lastpass; m++)
1.226 brouard 10275: if (s[m][i]>nlstate) {
10276: dcwave[i]=m;
10277: /* printf("i=%d j=%d s=%d dcwave=%d\n",i,j, s[j][i],dcwave[i]);*/
10278: break;
10279: }
1.126 brouard 10280: }
1.226 brouard 10281:
1.126 brouard 10282: for (i=1; i<=imx; i++) {
10283: if (wav[i]>0){
1.226 brouard 10284: ageexmed[i]=agev[mw[1][i]][i];
10285: j=wav[i];
10286: agecens[i]=1.;
10287:
10288: if (ageexmed[i]> 1 && wav[i] > 0){
10289: agecens[i]=agev[mw[j][i]][i];
10290: cens[i]= 1;
10291: }else if (ageexmed[i]< 1)
10292: cens[i]= -1;
10293: if (agedc[i]< AGESUP && agedc[i]>1 && dcwave[i]>firstpass && dcwave[i]<=lastpass)
10294: cens[i]=0 ;
1.126 brouard 10295: }
10296: else cens[i]=-1;
10297: }
10298:
10299: for (i=1;i<=NDIM;i++) {
10300: for (j=1;j<=NDIM;j++)
1.226 brouard 10301: ximort[i][j]=(i == j ? 1.0 : 0.0);
1.126 brouard 10302: }
10303:
1.145 brouard 10304: /*p[1]=0.0268; p[NDIM]=0.083;*/
1.126 brouard 10305: /*printf("%lf %lf", p[1], p[2]);*/
10306:
10307:
1.136 brouard 10308: #ifdef GSL
10309: printf("GSL optimization\n"); fprintf(ficlog,"Powell\n");
1.162 brouard 10310: #else
1.126 brouard 10311: printf("Powell\n"); fprintf(ficlog,"Powell\n");
1.136 brouard 10312: #endif
1.201 brouard 10313: strcpy(filerespow,"POW-MORT_");
10314: strcat(filerespow,fileresu);
1.126 brouard 10315: if((ficrespow=fopen(filerespow,"w"))==NULL) {
10316: printf("Problem with resultfile: %s\n", filerespow);
10317: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
10318: }
1.136 brouard 10319: #ifdef GSL
10320: fprintf(ficrespow,"# GSL optimization\n# iter -2*LL");
1.162 brouard 10321: #else
1.126 brouard 10322: fprintf(ficrespow,"# Powell\n# iter -2*LL");
1.136 brouard 10323: #endif
1.126 brouard 10324: /* for (i=1;i<=nlstate;i++)
10325: for(j=1;j<=nlstate+ndeath;j++)
10326: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
10327: */
10328: fprintf(ficrespow,"\n");
1.136 brouard 10329: #ifdef GSL
10330: /* gsl starts here */
10331: T = gsl_multimin_fminimizer_nmsimplex;
10332: gsl_multimin_fminimizer *sfm = NULL;
10333: gsl_vector *ss, *x;
10334: gsl_multimin_function minex_func;
10335:
10336: /* Initial vertex size vector */
10337: ss = gsl_vector_alloc (NDIM);
10338:
10339: if (ss == NULL){
10340: GSL_ERROR_VAL ("failed to allocate space for ss", GSL_ENOMEM, 0);
10341: }
10342: /* Set all step sizes to 1 */
10343: gsl_vector_set_all (ss, 0.001);
10344:
10345: /* Starting point */
1.126 brouard 10346:
1.136 brouard 10347: x = gsl_vector_alloc (NDIM);
10348:
10349: if (x == NULL){
10350: gsl_vector_free(ss);
10351: GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0);
10352: }
10353:
10354: /* Initialize method and iterate */
10355: /* p[1]=0.0268; p[NDIM]=0.083; */
1.186 brouard 10356: /* gsl_vector_set(x, 0, 0.0268); */
10357: /* gsl_vector_set(x, 1, 0.083); */
1.136 brouard 10358: gsl_vector_set(x, 0, p[1]);
10359: gsl_vector_set(x, 1, p[2]);
10360:
10361: minex_func.f = &gompertz_f;
10362: minex_func.n = NDIM;
10363: minex_func.params = (void *)&p; /* ??? */
10364:
10365: sfm = gsl_multimin_fminimizer_alloc (T, NDIM);
10366: gsl_multimin_fminimizer_set (sfm, &minex_func, x, ss);
10367:
10368: printf("Iterations beginning .....\n\n");
10369: printf("Iter. # Intercept Slope -Log Likelihood Simplex size\n");
10370:
10371: iteri=0;
10372: while (rval == GSL_CONTINUE){
10373: iteri++;
10374: status = gsl_multimin_fminimizer_iterate(sfm);
10375:
10376: if (status) printf("error: %s\n", gsl_strerror (status));
10377: fflush(0);
10378:
10379: if (status)
10380: break;
10381:
10382: rval = gsl_multimin_test_size (gsl_multimin_fminimizer_size (sfm), 1e-6);
10383: ssval = gsl_multimin_fminimizer_size (sfm);
10384:
10385: if (rval == GSL_SUCCESS)
10386: printf ("converged to a local maximum at\n");
10387:
10388: printf("%5d ", iteri);
10389: for (it = 0; it < NDIM; it++){
10390: printf ("%10.5f ", gsl_vector_get (sfm->x, it));
10391: }
10392: printf("f() = %-10.5f ssize = %.7f\n", sfm->fval, ssval);
10393: }
10394:
10395: printf("\n\n Please note: Program should be run many times with varying starting points to detemine global maximum\n\n");
10396:
10397: gsl_vector_free(x); /* initial values */
10398: gsl_vector_free(ss); /* inital step size */
10399: for (it=0; it<NDIM; it++){
10400: p[it+1]=gsl_vector_get(sfm->x,it);
10401: fprintf(ficrespow," %.12lf", p[it]);
10402: }
10403: gsl_multimin_fminimizer_free (sfm); /* p *(sfm.x.data) et p *(sfm.x.data+1) */
10404: #endif
10405: #ifdef POWELL
10406: powell(p,ximort,NDIM,ftol,&iter,&fret,gompertz);
10407: #endif
1.126 brouard 10408: fclose(ficrespow);
10409:
1.203 brouard 10410: hesscov(matcov, hess, p, NDIM, delti, 1e-4, gompertz);
1.126 brouard 10411:
10412: for(i=1; i <=NDIM; i++)
10413: for(j=i+1;j<=NDIM;j++)
1.220 brouard 10414: matcov[i][j]=matcov[j][i];
1.126 brouard 10415:
10416: printf("\nCovariance matrix\n ");
1.203 brouard 10417: fprintf(ficlog,"\nCovariance matrix\n ");
1.126 brouard 10418: for(i=1; i <=NDIM; i++) {
10419: for(j=1;j<=NDIM;j++){
1.220 brouard 10420: printf("%f ",matcov[i][j]);
10421: fprintf(ficlog,"%f ",matcov[i][j]);
1.126 brouard 10422: }
1.203 brouard 10423: printf("\n "); fprintf(ficlog,"\n ");
1.126 brouard 10424: }
10425:
10426: printf("iter=%d MLE=%f Eq=%lf*exp(%lf*(age-%d))\n",iter,-gompertz(p),p[1],p[2],agegomp);
1.193 brouard 10427: for (i=1;i<=NDIM;i++) {
1.126 brouard 10428: printf("%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
1.193 brouard 10429: fprintf(ficlog,"%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
10430: }
1.126 brouard 10431: lsurv=vector(1,AGESUP);
10432: lpop=vector(1,AGESUP);
10433: tpop=vector(1,AGESUP);
10434: lsurv[agegomp]=100000;
10435:
10436: for (k=agegomp;k<=AGESUP;k++) {
10437: agemortsup=k;
10438: if (p[1]*exp(p[2]*(k-agegomp))>1) break;
10439: }
10440:
10441: for (k=agegomp;k<agemortsup;k++)
10442: lsurv[k+1]=lsurv[k]-lsurv[k]*(p[1]*exp(p[2]*(k-agegomp)));
10443:
10444: for (k=agegomp;k<agemortsup;k++){
10445: lpop[k]=(lsurv[k]+lsurv[k+1])/2.;
10446: sumlpop=sumlpop+lpop[k];
10447: }
10448:
10449: tpop[agegomp]=sumlpop;
10450: for (k=agegomp;k<(agemortsup-3);k++){
10451: /* tpop[k+1]=2;*/
10452: tpop[k+1]=tpop[k]-lpop[k];
10453: }
10454:
10455:
10456: printf("\nAge lx qx dx Lx Tx e(x)\n");
10457: for (k=agegomp;k<(agemortsup-2);k++)
10458: 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]);
10459:
10460:
10461: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
1.220 brouard 10462: ageminpar=50;
10463: agemaxpar=100;
1.194 brouard 10464: if(ageminpar == AGEOVERFLOW ||agemaxpar == AGEOVERFLOW){
10465: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
10466: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
10467: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
10468: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
10469: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
10470: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.220 brouard 10471: }else{
10472: printf("Warning! ageminpar %f and agemaxpar %f have been fixed because for simplification until it is fixed...\n\n",ageminpar,agemaxpar);
10473: fprintf(ficlog,"Warning! ageminpar %f and agemaxpar %f have been fixed because for simplification until it is fixed...\n\n",ageminpar,agemaxpar);
1.201 brouard 10474: printinggnuplotmort(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, pathc,p);
1.220 brouard 10475: }
1.201 brouard 10476: printinghtmlmort(fileresu,title,datafile, firstpass, lastpass, \
1.126 brouard 10477: stepm, weightopt,\
10478: model,imx,p,matcov,agemortsup);
10479:
10480: free_vector(lsurv,1,AGESUP);
10481: free_vector(lpop,1,AGESUP);
10482: free_vector(tpop,1,AGESUP);
1.220 brouard 10483: free_matrix(ximort,1,NDIM,1,NDIM);
1.136 brouard 10484: free_ivector(cens,1,n);
10485: free_vector(agecens,1,n);
10486: free_ivector(dcwave,1,n);
1.220 brouard 10487: #ifdef GSL
1.136 brouard 10488: #endif
1.186 brouard 10489: } /* Endof if mle==-3 mortality only */
1.205 brouard 10490: /* Standard */
10491: else{ /* For mle !=- 3, could be 0 or 1 or 4 etc. */
10492: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
10493: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
1.132 brouard 10494: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
1.126 brouard 10495: printf("First Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
10496: for (k=1; k<=npar;k++)
10497: printf(" %d %8.5f",k,p[k]);
10498: printf("\n");
1.205 brouard 10499: if(mle>=1){ /* Could be 1 or 2, Real Maximization */
10500: /* mlikeli uses func not funcone */
10501: mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func);
10502: }
10503: if(mle==0) {/* No optimization, will print the likelihoods for the datafile */
10504: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
10505: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
10506: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
10507: }
10508: globpr=1; /* again, to print the individual contributions using computed gpimx and gsw */
1.126 brouard 10509: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
10510: printf("Second Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
10511: for (k=1; k<=npar;k++)
10512: printf(" %d %8.5f",k,p[k]);
10513: printf("\n");
10514:
10515: /*--------- results files --------------*/
1.224 brouard 10516: fprintf(ficres,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nqv=%d ntv=%d nqtv=%d nlstate=%d ndeath=%d maxwav=%d mle= 0 weight=%d\nmodel=1+age+%s.\n", title, datafile, lastobs, firstpass,lastpass,ftol, stepm, ncovcol, nqv, ntv, nqtv, nlstate, ndeath, maxwav, weightopt,model);
1.126 brouard 10517:
10518:
10519: fprintf(ficres,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
10520: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
10521: fprintf(ficlog,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
10522: for(i=1,jk=1; i <=nlstate; i++){
10523: for(k=1; k <=(nlstate+ndeath); k++){
1.225 brouard 10524: if (k != i) {
10525: printf("%d%d ",i,k);
10526: fprintf(ficlog,"%d%d ",i,k);
10527: fprintf(ficres,"%1d%1d ",i,k);
10528: for(j=1; j <=ncovmodel; j++){
10529: printf("%12.7f ",p[jk]);
10530: fprintf(ficlog,"%12.7f ",p[jk]);
10531: fprintf(ficres,"%12.7f ",p[jk]);
10532: jk++;
10533: }
10534: printf("\n");
10535: fprintf(ficlog,"\n");
10536: fprintf(ficres,"\n");
10537: }
1.126 brouard 10538: }
10539: }
1.203 brouard 10540: if(mle != 0){
10541: /* Computing hessian and covariance matrix only at a peak of the Likelihood, that is after optimization */
1.126 brouard 10542: ftolhess=ftol; /* Usually correct */
1.203 brouard 10543: hesscov(matcov, hess, p, npar, delti, ftolhess, func);
10544: printf("Parameters and 95%% confidence intervals\n W is simply the result of the division of the parameter by the square root of covariance of the parameter.\n And Wald-based confidence intervals plus and minus 1.96 * W .\n But be careful that parameters are highly correlated because incidence of disability is highly correlated to incidence of recovery.\n It might be better to visualize the covariance matrix. See the page 'Matrix of variance-covariance of one-step probabilities' and its graphs.\n");
10545: fprintf(ficlog, "Parameters, Wald tests and Wald-based confidence intervals\n W is simply the result of the division of the parameter by the square root of covariance of the parameter.\n And Wald-based confidence intervals plus and minus 1.96 * W \n It might be better to visualize the covariance matrix. See the page 'Matrix of variance-covariance of one-step probabilities' and its graphs.\n");
10546: for(i=1,jk=1; i <=nlstate; i++){
1.225 brouard 10547: for(k=1; k <=(nlstate+ndeath); k++){
10548: if (k != i) {
10549: printf("%d%d ",i,k);
10550: fprintf(ficlog,"%d%d ",i,k);
10551: for(j=1; j <=ncovmodel; j++){
10552: printf("%12.7f W=%8.3f CI=[%12.7f ; %12.7f] ",p[jk], p[jk]/sqrt(matcov[jk][jk]), p[jk]-1.96*sqrt(matcov[jk][jk]),p[jk]+1.96*sqrt(matcov[jk][jk]));
10553: fprintf(ficlog,"%12.7f W=%8.3f CI=[%12.7f ; %12.7f] ",p[jk], p[jk]/sqrt(matcov[jk][jk]), p[jk]-1.96*sqrt(matcov[jk][jk]),p[jk]+1.96*sqrt(matcov[jk][jk]));
10554: jk++;
10555: }
10556: printf("\n");
10557: fprintf(ficlog,"\n");
10558: }
10559: }
1.193 brouard 10560: }
1.203 brouard 10561: } /* end of hesscov and Wald tests */
1.225 brouard 10562:
1.203 brouard 10563: /* */
1.126 brouard 10564: fprintf(ficres,"# Scales (for hessian or gradient estimation)\n");
10565: printf("# Scales (for hessian or gradient estimation)\n");
10566: fprintf(ficlog,"# Scales (for hessian or gradient estimation)\n");
10567: for(i=1,jk=1; i <=nlstate; i++){
10568: for(j=1; j <=nlstate+ndeath; j++){
1.225 brouard 10569: if (j!=i) {
10570: fprintf(ficres,"%1d%1d",i,j);
10571: printf("%1d%1d",i,j);
10572: fprintf(ficlog,"%1d%1d",i,j);
10573: for(k=1; k<=ncovmodel;k++){
10574: printf(" %.5e",delti[jk]);
10575: fprintf(ficlog," %.5e",delti[jk]);
10576: fprintf(ficres," %.5e",delti[jk]);
10577: jk++;
10578: }
10579: printf("\n");
10580: fprintf(ficlog,"\n");
10581: fprintf(ficres,"\n");
10582: }
1.126 brouard 10583: }
10584: }
10585:
10586: 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");
1.203 brouard 10587: if(mle >= 1) /* To big for the screen */
1.126 brouard 10588: 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");
10589: 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");
10590: /* # 121 Var(a12)\n\ */
10591: /* # 122 Cov(b12,a12) Var(b12)\n\ */
10592: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
10593: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
10594: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
10595: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
10596: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
10597: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
10598:
10599:
10600: /* Just to have a covariance matrix which will be more understandable
10601: even is we still don't want to manage dictionary of variables
10602: */
10603: for(itimes=1;itimes<=2;itimes++){
10604: jj=0;
10605: for(i=1; i <=nlstate; i++){
1.225 brouard 10606: for(j=1; j <=nlstate+ndeath; j++){
10607: if(j==i) continue;
10608: for(k=1; k<=ncovmodel;k++){
10609: jj++;
10610: ca[0]= k+'a'-1;ca[1]='\0';
10611: if(itimes==1){
10612: if(mle>=1)
10613: printf("#%1d%1d%d",i,j,k);
10614: fprintf(ficlog,"#%1d%1d%d",i,j,k);
10615: fprintf(ficres,"#%1d%1d%d",i,j,k);
10616: }else{
10617: if(mle>=1)
10618: printf("%1d%1d%d",i,j,k);
10619: fprintf(ficlog,"%1d%1d%d",i,j,k);
10620: fprintf(ficres,"%1d%1d%d",i,j,k);
10621: }
10622: ll=0;
10623: for(li=1;li <=nlstate; li++){
10624: for(lj=1;lj <=nlstate+ndeath; lj++){
10625: if(lj==li) continue;
10626: for(lk=1;lk<=ncovmodel;lk++){
10627: ll++;
10628: if(ll<=jj){
10629: cb[0]= lk +'a'-1;cb[1]='\0';
10630: if(ll<jj){
10631: if(itimes==1){
10632: if(mle>=1)
10633: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
10634: fprintf(ficlog," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
10635: fprintf(ficres," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
10636: }else{
10637: if(mle>=1)
10638: printf(" %.5e",matcov[jj][ll]);
10639: fprintf(ficlog," %.5e",matcov[jj][ll]);
10640: fprintf(ficres," %.5e",matcov[jj][ll]);
10641: }
10642: }else{
10643: if(itimes==1){
10644: if(mle>=1)
10645: printf(" Var(%s%1d%1d)",ca,i,j);
10646: fprintf(ficlog," Var(%s%1d%1d)",ca,i,j);
10647: fprintf(ficres," Var(%s%1d%1d)",ca,i,j);
10648: }else{
10649: if(mle>=1)
10650: printf(" %.7e",matcov[jj][ll]);
10651: fprintf(ficlog," %.7e",matcov[jj][ll]);
10652: fprintf(ficres," %.7e",matcov[jj][ll]);
10653: }
10654: }
10655: }
10656: } /* end lk */
10657: } /* end lj */
10658: } /* end li */
10659: if(mle>=1)
10660: printf("\n");
10661: fprintf(ficlog,"\n");
10662: fprintf(ficres,"\n");
10663: numlinepar++;
10664: } /* end k*/
10665: } /*end j */
1.126 brouard 10666: } /* end i */
10667: } /* end itimes */
10668:
10669: fflush(ficlog);
10670: fflush(ficres);
1.225 brouard 10671: while(fgets(line, MAXLINE, ficpar)) {
10672: /* If line starts with a # it is a comment */
10673: if (line[0] == '#') {
10674: numlinepar++;
10675: fputs(line,stdout);
10676: fputs(line,ficparo);
10677: fputs(line,ficlog);
10678: continue;
10679: }else
10680: break;
10681: }
10682:
1.209 brouard 10683: /* while((c=getc(ficpar))=='#' && c!= EOF){ */
10684: /* ungetc(c,ficpar); */
10685: /* fgets(line, MAXLINE, ficpar); */
10686: /* fputs(line,stdout); */
10687: /* fputs(line,ficparo); */
10688: /* } */
10689: /* ungetc(c,ficpar); */
1.126 brouard 10690:
10691: estepm=0;
1.209 brouard 10692: if((num_filled=sscanf(line,"agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%lf\n",&ageminpar,&agemaxpar, &bage, &fage, &estepm, &ftolpl)) !=EOF){
1.225 brouard 10693:
10694: if (num_filled != 6) {
10695: printf("Error: Not 6 parameters in line, for example:agemin=60 agemax=95 bage=55 fage=95 estepm=24 ftolpl=6e-4\n, your line=%s . Probably you are running an older format.\n",line);
10696: fprintf(ficlog,"Error: Not 6 parameters in line, for example:agemin=60 agemax=95 bage=55 fage=95 estepm=24 ftolpl=6e-4\n, your line=%s . Probably you are running an older format.\n",line);
10697: goto end;
10698: }
10699: printf("agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%lf\n",ageminpar,agemaxpar, bage, fage, estepm, ftolpl);
10700: }
10701: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
10702: /*ftolpl=6.e-4;*/ /* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
10703:
1.209 brouard 10704: /* fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%\n",&ageminpar,&agemaxpar, &bage, &fage, &estepm); */
1.126 brouard 10705: if (estepm==0 || estepm < stepm) estepm=stepm;
10706: if (fage <= 2) {
10707: bage = ageminpar;
10708: fage = agemaxpar;
10709: }
10710:
10711: fprintf(ficres,"# agemin agemax for life expectancy, bage fage (if mle==0 ie no data nor Max likelihood).\n");
1.211 brouard 10712: fprintf(ficres,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
10713: fprintf(ficparo,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d, ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
1.220 brouard 10714:
1.186 brouard 10715: /* Other stuffs, more or less useful */
1.126 brouard 10716: while((c=getc(ficpar))=='#' && c!= EOF){
10717: ungetc(c,ficpar);
10718: fgets(line, MAXLINE, ficpar);
1.141 brouard 10719: fputs(line,stdout);
1.126 brouard 10720: fputs(line,ficparo);
10721: }
10722: ungetc(c,ficpar);
10723:
10724: 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);
10725: 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);
10726: 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);
10727: printf("begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mov_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
10728: 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);
10729:
10730: while((c=getc(ficpar))=='#' && c!= EOF){
10731: ungetc(c,ficpar);
10732: fgets(line, MAXLINE, ficpar);
1.141 brouard 10733: fputs(line,stdout);
1.126 brouard 10734: fputs(line,ficparo);
10735: }
10736: ungetc(c,ficpar);
10737:
10738:
10739: dateprev1=anprev1+(mprev1-1)/12.+(jprev1-1)/365.;
10740: dateprev2=anprev2+(mprev2-1)/12.+(jprev2-1)/365.;
10741:
10742: fscanf(ficpar,"pop_based=%d\n",&popbased);
1.193 brouard 10743: fprintf(ficlog,"pop_based=%d\n",popbased);
1.126 brouard 10744: fprintf(ficparo,"pop_based=%d\n",popbased);
10745: fprintf(ficres,"pop_based=%d\n",popbased);
10746:
10747: while((c=getc(ficpar))=='#' && c!= EOF){
10748: ungetc(c,ficpar);
10749: fgets(line, MAXLINE, ficpar);
1.141 brouard 10750: fputs(line,stdout);
1.126 brouard 10751: fputs(line,ficparo);
10752: }
10753: ungetc(c,ficpar);
10754:
10755: 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);
10756: 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);
10757: 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);
10758: 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);
10759: 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);
10760: /* day and month of proj2 are not used but only year anproj2.*/
10761:
1.217 brouard 10762: while((c=getc(ficpar))=='#' && c!= EOF){
10763: ungetc(c,ficpar);
10764: fgets(line, MAXLINE, ficpar);
10765: fputs(line,stdout);
10766: fputs(line,ficparo);
10767: }
10768: ungetc(c,ficpar);
10769:
10770: fscanf(ficpar,"backcast=%d starting-back-date=%lf/%lf/%lf final-back-date=%lf/%lf/%lf mobil_average=%d\n",&backcast,&jback1,&mback1,&anback1,&jback2,&mback2,&anback2,&mobilavproj);
1.223 brouard 10771: fprintf(ficparo,"backcast=%d starting-back-date=%.lf/%.lf/%.lf final-back-date=%.lf/%.lf/%.lf mobil_average=%d\n",backcast,jback1,mback1,anback1,jback2,mback2,anback2,mobilavproj);
10772: fprintf(ficlog,"backcast=%d starting-back-date=%.lf/%.lf/%.lf final-back-date=%.lf/%.lf/%.lf mobil_average=%d\n",backcast,jback1,mback1,anback1,jback2,mback2,anback2,mobilavproj);
10773: fprintf(ficres,"backcast=%d starting-back-date=%.lf/%.lf/%.lf final-back-date=%.lf/%.lf/%.lf mobil_average=%d\n",backcast,jback1,mback1,anback1,jback2,mback2,anback2,mobilavproj);
1.217 brouard 10774: /* day and month of proj2 are not used but only year anproj2.*/
1.126 brouard 10775:
1.230 brouard 10776: /* Results */
1.235 brouard 10777: nresult=0;
1.230 brouard 10778: while(fgets(line, MAXLINE, ficpar)) {
10779: /* If line starts with a # it is a comment */
10780: if (line[0] == '#') {
10781: numlinepar++;
10782: fputs(line,stdout);
10783: fputs(line,ficparo);
10784: fputs(line,ficlog);
10785: continue;
10786: }else
10787: break;
10788: }
10789: while((num_filled=sscanf(line,"result:%[^\n]\n",resultline)) !=EOF){
10790: if (num_filled == 0)
10791: resultline[0]='\0';
10792: else if (num_filled != 1){
10793: printf("ERROR %d: result line should be at minimum 'result=' %s\n",num_filled, line);
10794: }
1.235 brouard 10795: nresult++; /* Sum of resultlines */
10796: printf("Result %d: result=%s\n",nresult, resultline);
10797: if(nresult > MAXRESULTLINES){
10798: printf("ERROR: Current version of IMaCh limits the number of resultlines to %d, you used %d\n",MAXRESULTLINES,nresult);
10799: fprintf(ficlog,"ERROR: Current version of IMaCh limits the number of resultlines to %d, you used %d\n",MAXRESULTLINES,nresult);
10800: goto end;
10801: }
10802: decoderesult(resultline, nresult); /* Fills TKresult[nresult] combination and Tresult[nresult][k4+1] combination values */
1.230 brouard 10803: while(fgets(line, MAXLINE, ficpar)) {
10804: /* If line starts with a # it is a comment */
10805: if (line[0] == '#') {
10806: numlinepar++;
10807: fputs(line,stdout);
10808: fputs(line,ficparo);
10809: fputs(line,ficlog);
10810: continue;
10811: }else
10812: break;
10813: }
10814: if (feof(ficpar))
10815: break;
10816: else{ /* Processess output results for this combination of covariate values */
10817: }
10818: }
10819:
10820:
1.126 brouard 10821:
1.230 brouard 10822: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint); */
1.145 brouard 10823: /* ,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2); */
1.126 brouard 10824:
10825: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
1.194 brouard 10826: if(ageminpar == AGEOVERFLOW ||agemaxpar == -AGEOVERFLOW){
1.230 brouard 10827: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
1.194 brouard 10828: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
10829: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.230 brouard 10830: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
1.194 brouard 10831: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
10832: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.220 brouard 10833: }else{
1.218 brouard 10834: printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, prevfcast, backcast, pathc,p);
1.220 brouard 10835: }
10836: printinghtml(fileresu,title,datafile, firstpass, lastpass, stepm, weightopt, \
1.225 brouard 10837: model,imx,jmin,jmax,jmean,rfileres,popforecast,prevfcast,backcast, estepm, \
10838: jprev1,mprev1,anprev1,dateprev1,jprev2,mprev2,anprev2,dateprev2);
1.220 brouard 10839:
1.225 brouard 10840: /*------------ free_vector -------------*/
10841: /* chdir(path); */
1.220 brouard 10842:
1.215 brouard 10843: /* free_ivector(wav,1,imx); */ /* Moved after last prevalence call */
10844: /* free_imatrix(dh,1,lastpass-firstpass+2,1,imx); */
10845: /* free_imatrix(bh,1,lastpass-firstpass+2,1,imx); */
10846: /* free_imatrix(mw,1,lastpass-firstpass+2,1,imx); */
1.126 brouard 10847: free_lvector(num,1,n);
10848: free_vector(agedc,1,n);
10849: /*free_matrix(covar,0,NCOVMAX,1,n);*/
10850: /*free_matrix(covar,1,NCOVMAX,1,n);*/
10851: fclose(ficparo);
10852: fclose(ficres);
1.220 brouard 10853:
10854:
1.186 brouard 10855: /* Other results (useful)*/
1.220 brouard 10856:
10857:
1.126 brouard 10858: /*--------------- Prevalence limit (period or stable prevalence) --------------*/
1.180 brouard 10859: /*#include "prevlim.h"*/ /* Use ficrespl, ficlog */
10860: prlim=matrix(1,nlstate,1,nlstate);
1.209 brouard 10861: prevalence_limit(p, prlim, ageminpar, agemaxpar, ftolpl, &ncvyear);
1.126 brouard 10862: fclose(ficrespl);
10863:
10864: /*------------- h Pij x at various ages ------------*/
1.180 brouard 10865: /*#include "hpijx.h"*/
10866: hPijx(p, bage, fage);
1.145 brouard 10867: fclose(ficrespij);
1.227 brouard 10868:
1.220 brouard 10869: /* ncovcombmax= pow(2,cptcoveff); */
1.219 brouard 10870: /*-------------- Variance of one-step probabilities---*/
1.145 brouard 10871: k=1;
1.126 brouard 10872: varprob(optionfilefiname, matcov, p, delti, nlstate, bage, fage,k,Tvar,nbcode, ncodemax,strstart);
1.227 brouard 10873:
1.219 brouard 10874: /* Prevalence for each covariates in probs[age][status][cov] */
1.218 brouard 10875: probs= ma3x(1,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
1.126 brouard 10876: for(i=1;i<=AGESUP;i++)
1.219 brouard 10877: for(j=1;j<=nlstate+ndeath;j++) /* ndeath is useless but a necessity to be compared with mobaverages */
1.225 brouard 10878: for(k=1;k<=ncovcombmax;k++)
10879: probs[i][j][k]=0.;
1.219 brouard 10880: prevalence(probs, ageminpar, agemaxpar, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
10881: if (mobilav!=0 ||mobilavproj !=0 ) {
10882: mobaverages= ma3x(1, AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
1.227 brouard 10883: for(i=1;i<=AGESUP;i++)
10884: for(j=1;j<=nlstate;j++)
10885: for(k=1;k<=ncovcombmax;k++)
10886: mobaverages[i][j][k]=0.;
1.219 brouard 10887: mobaverage=mobaverages;
10888: if (mobilav!=0) {
1.235 brouard 10889: printf("Movingaveraging observed prevalence\n");
1.227 brouard 10890: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilav)!=0){
10891: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
10892: printf(" Error in movingaverage mobilav=%d\n",mobilav);
10893: }
1.219 brouard 10894: }
10895: /* /\* Prevalence for each covariates in probs[age][status][cov] *\/ */
10896: /* prevalence(probs, ageminpar, agemaxpar, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
10897: else if (mobilavproj !=0) {
1.235 brouard 10898: printf("Movingaveraging projected observed prevalence\n");
1.227 brouard 10899: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilavproj)!=0){
10900: fprintf(ficlog," Error in movingaverage mobilavproj=%d\n",mobilavproj);
10901: printf(" Error in movingaverage mobilavproj=%d\n",mobilavproj);
10902: }
1.219 brouard 10903: }
10904: }/* end if moving average */
1.227 brouard 10905:
1.126 brouard 10906: /*---------- Forecasting ------------------*/
10907: /*if((stepm == 1) && (strcmp(model,".")==0)){*/
10908: if(prevfcast==1){
10909: /* if(stepm ==1){*/
1.225 brouard 10910: prevforecast(fileresu, anproj1, mproj1, jproj1, agemin, agemax, dateprev1, dateprev2, mobilavproj, bage, fage, firstpass, lastpass, anproj2, p, cptcoveff);
1.126 brouard 10911: }
1.217 brouard 10912: if(backcast==1){
1.219 brouard 10913: ddnewms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
10914: ddoldms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
10915: ddsavms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
10916:
10917: /*--------------- Back Prevalence limit (period or stable prevalence) --------------*/
10918:
10919: bprlim=matrix(1,nlstate,1,nlstate);
10920: back_prevalence_limit(p, bprlim, ageminpar, agemaxpar, ftolpl, &ncvyear, dateprev1, dateprev2, firstpass, lastpass, mobilavproj);
10921: fclose(ficresplb);
10922:
1.222 brouard 10923: hBijx(p, bage, fage, mobaverage);
10924: fclose(ficrespijb);
1.219 brouard 10925: free_matrix(bprlim,1,nlstate,1,nlstate); /*here or after loop ? */
10926:
10927: /* prevbackforecast(fileresu, anback1, mback1, jback1, agemin, agemax, dateprev1, dateprev2, mobilavproj,
1.225 brouard 10928: bage, fage, firstpass, lastpass, anback2, p, cptcoveff); */
1.219 brouard 10929: free_matrix(ddnewms, 1, nlstate+ndeath, 1, nlstate+ndeath);
10930: free_matrix(ddsavms, 1, nlstate+ndeath, 1, nlstate+ndeath);
10931: free_matrix(ddoldms, 1, nlstate+ndeath, 1, nlstate+ndeath);
10932: }
1.217 brouard 10933:
1.186 brouard 10934:
10935: /* ------ Other prevalence ratios------------ */
1.126 brouard 10936:
1.215 brouard 10937: free_ivector(wav,1,imx);
10938: free_imatrix(dh,1,lastpass-firstpass+2,1,imx);
10939: free_imatrix(bh,1,lastpass-firstpass+2,1,imx);
10940: free_imatrix(mw,1,lastpass-firstpass+2,1,imx);
1.218 brouard 10941:
10942:
1.127 brouard 10943: /*---------- Health expectancies, no variances ------------*/
1.218 brouard 10944:
1.201 brouard 10945: strcpy(filerese,"E_");
10946: strcat(filerese,fileresu);
1.126 brouard 10947: if((ficreseij=fopen(filerese,"w"))==NULL) {
10948: printf("Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
10949: fprintf(ficlog,"Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
10950: }
1.208 brouard 10951: printf("Computing Health Expectancies: result on file '%s' ...", filerese);fflush(stdout);
10952: fprintf(ficlog,"Computing Health Expectancies: result on file '%s' ...", filerese);fflush(ficlog);
1.219 brouard 10953:
1.235 brouard 10954: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
10955: if (cptcovn < 1){i1=1;}
10956:
10957: for(nres=1; nres <= nresult; nres++) /* For each resultline */
10958: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying */
10959: if(TKresult[nres]!= k)
10960: continue;
1.219 brouard 10961: fprintf(ficreseij,"\n#****** ");
1.235 brouard 10962: printf("\n#****** ");
1.225 brouard 10963: for(j=1;j<=cptcoveff;j++) {
1.227 brouard 10964: fprintf(ficreseij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.235 brouard 10965: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10966: }
10967: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
10968: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
10969: fprintf(ficreseij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
1.219 brouard 10970: }
10971: fprintf(ficreseij,"******\n");
1.235 brouard 10972: printf("******\n");
1.219 brouard 10973:
10974: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
10975: oldm=oldms;savm=savms;
1.235 brouard 10976: evsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, strstart, nres);
1.127 brouard 10977:
1.219 brouard 10978: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
1.127 brouard 10979: }
10980: fclose(ficreseij);
1.208 brouard 10981: printf("done evsij\n");fflush(stdout);
10982: fprintf(ficlog,"done evsij\n");fflush(ficlog);
1.218 brouard 10983:
1.227 brouard 10984: /*---------- State-specific expectancies and variances ------------*/
1.218 brouard 10985:
10986:
1.201 brouard 10987: strcpy(filerest,"T_");
10988: strcat(filerest,fileresu);
1.127 brouard 10989: if((ficrest=fopen(filerest,"w"))==NULL) {
10990: printf("Problem with total LE resultfile: %s\n", filerest);goto end;
10991: fprintf(ficlog,"Problem with total LE resultfile: %s\n", filerest);goto end;
10992: }
1.208 brouard 10993: printf("Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(stdout);
10994: fprintf(ficlog,"Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(ficlog);
1.218 brouard 10995:
1.126 brouard 10996:
1.201 brouard 10997: strcpy(fileresstde,"STDE_");
10998: strcat(fileresstde,fileresu);
1.126 brouard 10999: if((ficresstdeij=fopen(fileresstde,"w"))==NULL) {
1.227 brouard 11000: printf("Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
11001: fprintf(ficlog,"Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
1.126 brouard 11002: }
1.227 brouard 11003: printf(" Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde);
11004: fprintf(ficlog," Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde);
1.126 brouard 11005:
1.201 brouard 11006: strcpy(filerescve,"CVE_");
11007: strcat(filerescve,fileresu);
1.126 brouard 11008: if((ficrescveij=fopen(filerescve,"w"))==NULL) {
1.227 brouard 11009: printf("Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0);
11010: fprintf(ficlog,"Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0);
1.126 brouard 11011: }
1.227 brouard 11012: printf(" Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve);
11013: fprintf(ficlog," Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve);
1.126 brouard 11014:
1.201 brouard 11015: strcpy(fileresv,"V_");
11016: strcat(fileresv,fileresu);
1.126 brouard 11017: if((ficresvij=fopen(fileresv,"w"))==NULL) {
11018: printf("Problem with variance resultfile: %s\n", fileresv);exit(0);
11019: fprintf(ficlog,"Problem with variance resultfile: %s\n", fileresv);exit(0);
11020: }
1.227 brouard 11021: printf(" Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(stdout);
11022: fprintf(ficlog," Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(ficlog);
1.126 brouard 11023:
1.145 brouard 11024: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
11025: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
11026:
1.235 brouard 11027: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
11028: if (cptcovn < 1){i1=1;}
11029:
11030: for(nres=1; nres <= nresult; nres++) /* For each resultline */
11031: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying */
11032: if(TKresult[nres]!= k)
11033: continue;
11034: printf("\n#****** Selected:");
11035: fprintf(ficrest,"\n#****** Selected:");
11036: fprintf(ficlog,"\n#****** Selected:");
1.227 brouard 11037: for(j=1;j<=cptcoveff;j++){
11038: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
11039: fprintf(ficrest,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
11040: fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
11041: }
1.235 brouard 11042: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
11043: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
11044: fprintf(ficrest," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
11045: fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
11046: }
1.208 brouard 11047: fprintf(ficrest,"******\n");
1.227 brouard 11048: fprintf(ficlog,"******\n");
11049: printf("******\n");
1.208 brouard 11050:
11051: fprintf(ficresstdeij,"\n#****** ");
11052: fprintf(ficrescveij,"\n#****** ");
1.225 brouard 11053: for(j=1;j<=cptcoveff;j++) {
1.227 brouard 11054: fprintf(ficresstdeij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
11055: fprintf(ficrescveij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.208 brouard 11056: }
1.235 brouard 11057: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
11058: fprintf(ficresstdeij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
11059: fprintf(ficrescveij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
11060: }
1.208 brouard 11061: fprintf(ficresstdeij,"******\n");
11062: fprintf(ficrescveij,"******\n");
11063:
11064: fprintf(ficresvij,"\n#****** ");
1.225 brouard 11065: for(j=1;j<=cptcoveff;j++)
1.227 brouard 11066: fprintf(ficresvij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.235 brouard 11067: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
11068: fprintf(ficresvij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
11069: }
1.208 brouard 11070: fprintf(ficresvij,"******\n");
11071:
11072: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
11073: oldm=oldms;savm=savms;
1.235 brouard 11074: printf(" cvevsij ");
11075: fprintf(ficlog, " cvevsij ");
11076: cvevsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, delti, matcov, strstart, nres);
1.208 brouard 11077: printf(" end cvevsij \n ");
11078: fprintf(ficlog, " end cvevsij \n ");
11079:
11080: /*
11081: */
11082: /* goto endfree; */
11083:
11084: vareij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
11085: pstamp(ficrest);
11086:
11087:
11088: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
1.227 brouard 11089: oldm=oldms;savm=savms; /* ZZ Segmentation fault */
11090: cptcod= 0; /* To be deleted */
11091: printf("varevsij vpopbased=%d \n",vpopbased);
11092: fprintf(ficlog, "varevsij vpopbased=%d \n",vpopbased);
1.235 brouard 11093: varevsij(optionfilefiname, vareij, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl, &ncvyear, k, estepm, cptcov,cptcod,vpopbased,mobilav, strstart, nres); /* cptcod not initialized Intel */
1.227 brouard 11094: 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 ");
11095: if(vpopbased==1)
11096: 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);
11097: else
11098: fprintf(ficrest,"the age specific period (stable) prevalences in each health state \n");
11099: fprintf(ficrest,"# Age popbased mobilav e.. (std) ");
11100: for (i=1;i<=nlstate;i++) fprintf(ficrest,"e.%d (std) ",i);
11101: fprintf(ficrest,"\n");
11102: /* printf("Which p?\n"); for(i=1;i<=npar;i++)printf("p[i=%d]=%lf,",i,p[i]);printf("\n"); */
11103: epj=vector(1,nlstate+1);
11104: printf("Computing age specific period (stable) prevalences in each health state \n");
11105: fprintf(ficlog,"Computing age specific period (stable) prevalences in each health state \n");
11106: for(age=bage; age <=fage ;age++){
1.235 brouard 11107: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, &ncvyear, k, nres); /*ZZ Is it the correct prevalim */
1.227 brouard 11108: if (vpopbased==1) {
11109: if(mobilav ==0){
11110: for(i=1; i<=nlstate;i++)
11111: prlim[i][i]=probs[(int)age][i][k];
11112: }else{ /* mobilav */
11113: for(i=1; i<=nlstate;i++)
11114: prlim[i][i]=mobaverage[(int)age][i][k];
11115: }
11116: }
1.219 brouard 11117:
1.227 brouard 11118: fprintf(ficrest," %4.0f %d %d",age, vpopbased, mobilav);
11119: /* fprintf(ficrest," %4.0f %d %d %d %d",age, vpopbased, mobilav,Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */ /* to be done */
11120: /* printf(" age %4.0f ",age); */
11121: for(j=1, epj[nlstate+1]=0.;j <=nlstate;j++){
11122: for(i=1, epj[j]=0.;i <=nlstate;i++) {
11123: epj[j] += prlim[i][i]*eij[i][j][(int)age];
11124: /*ZZZ printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]);*/
11125: /* printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]); */
11126: }
11127: epj[nlstate+1] +=epj[j];
11128: }
11129: /* printf(" age %4.0f \n",age); */
1.219 brouard 11130:
1.227 brouard 11131: for(i=1, vepp=0.;i <=nlstate;i++)
11132: for(j=1;j <=nlstate;j++)
11133: vepp += vareij[i][j][(int)age];
11134: fprintf(ficrest," %7.3f (%7.3f)", epj[nlstate+1],sqrt(vepp));
11135: for(j=1;j <=nlstate;j++){
11136: fprintf(ficrest," %7.3f (%7.3f)", epj[j],sqrt(vareij[j][j][(int)age]));
11137: }
11138: fprintf(ficrest,"\n");
11139: }
1.208 brouard 11140: } /* End vpopbased */
11141: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
11142: free_ma3x(vareij,1,nlstate,1,nlstate,(int) bage, (int)fage);
11143: free_vector(epj,1,nlstate+1);
1.235 brouard 11144: printf("done selection\n");fflush(stdout);
11145: fprintf(ficlog,"done selection\n");fflush(ficlog);
1.208 brouard 11146:
1.145 brouard 11147: /*}*/
1.235 brouard 11148: } /* End k selection */
1.227 brouard 11149:
11150: printf("done State-specific expectancies\n");fflush(stdout);
11151: fprintf(ficlog,"done State-specific expectancies\n");fflush(ficlog);
11152:
1.126 brouard 11153: /*------- Variance of period (stable) prevalence------*/
1.227 brouard 11154:
1.201 brouard 11155: strcpy(fileresvpl,"VPL_");
11156: strcat(fileresvpl,fileresu);
1.126 brouard 11157: if((ficresvpl=fopen(fileresvpl,"w"))==NULL) {
11158: printf("Problem with variance of period (stable) prevalence resultfile: %s\n", fileresvpl);
11159: exit(0);
11160: }
1.208 brouard 11161: printf("Computing Variance-covariance of period (stable) prevalence: file '%s' ...", fileresvpl);fflush(stdout);
11162: fprintf(ficlog, "Computing Variance-covariance of period (stable) prevalence: file '%s' ...", fileresvpl);fflush(ficlog);
1.227 brouard 11163:
1.145 brouard 11164: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
11165: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
1.227 brouard 11166:
1.235 brouard 11167: i1=pow(2,cptcoveff);
11168: if (cptcovn < 1){i1=1;}
11169:
11170: for(nres=1; nres <= nresult; nres++) /* For each resultline */
11171: for(k=1; k<=i1;k++){
11172: if(TKresult[nres]!= k)
11173: continue;
1.227 brouard 11174: fprintf(ficresvpl,"\n#****** ");
11175: printf("\n#****** ");
11176: fprintf(ficlog,"\n#****** ");
11177: for(j=1;j<=cptcoveff;j++) {
11178: fprintf(ficresvpl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
11179: fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
11180: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
11181: }
1.235 brouard 11182: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
11183: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
11184: fprintf(ficresvpl," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
11185: fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
11186: }
1.227 brouard 11187: fprintf(ficresvpl,"******\n");
11188: printf("******\n");
11189: fprintf(ficlog,"******\n");
11190:
11191: varpl=matrix(1,nlstate,(int) bage, (int) fage);
11192: oldm=oldms;savm=savms;
1.235 brouard 11193: varprevlim(fileres, varpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl, &ncvyear, k, strstart, nres);
1.227 brouard 11194: free_matrix(varpl,1,nlstate,(int) bage, (int)fage);
1.145 brouard 11195: /*}*/
1.126 brouard 11196: }
1.227 brouard 11197:
1.126 brouard 11198: fclose(ficresvpl);
1.208 brouard 11199: printf("done variance-covariance of period prevalence\n");fflush(stdout);
11200: fprintf(ficlog,"done variance-covariance of period prevalence\n");fflush(ficlog);
1.227 brouard 11201:
11202: free_vector(weight,1,n);
11203: free_imatrix(Tvard,1,NCOVMAX,1,2);
11204: free_imatrix(s,1,maxwav+1,1,n);
11205: free_matrix(anint,1,maxwav,1,n);
11206: free_matrix(mint,1,maxwav,1,n);
11207: free_ivector(cod,1,n);
11208: free_ivector(tab,1,NCOVMAX);
11209: fclose(ficresstdeij);
11210: fclose(ficrescveij);
11211: fclose(ficresvij);
11212: fclose(ficrest);
11213: fclose(ficpar);
11214:
11215:
1.126 brouard 11216: /*---------- End : free ----------------*/
1.219 brouard 11217: if (mobilav!=0 ||mobilavproj !=0)
11218: free_ma3x(mobaverages,1, AGESUP,1,nlstate+ndeath, 1,ncovcombmax); /* We need to have a squared matrix with prevalence of the dead! */
1.218 brouard 11219: free_ma3x(probs,1,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
1.220 brouard 11220: free_matrix(prlim,1,nlstate,1,nlstate); /*here or after loop ? */
11221: free_matrix(pmmij,1,nlstate+ndeath,1,nlstate+ndeath);
1.126 brouard 11222: } /* mle==-3 arrives here for freeing */
1.227 brouard 11223: /* endfree:*/
11224: free_matrix(oldms, 1,nlstate+ndeath,1,nlstate+ndeath);
11225: free_matrix(newms, 1,nlstate+ndeath,1,nlstate+ndeath);
11226: free_matrix(savms, 1,nlstate+ndeath,1,nlstate+ndeath);
11227: free_ma3x(cotqvar,1,maxwav,1,nqtv,1,n);
1.233 brouard 11228: free_ma3x(cotvar,1,maxwav,1,ntv+nqtv,1,n);
1.227 brouard 11229: free_matrix(coqvar,1,maxwav,1,n);
11230: free_matrix(covar,0,NCOVMAX,1,n);
11231: free_matrix(matcov,1,npar,1,npar);
11232: free_matrix(hess,1,npar,1,npar);
11233: /*free_vector(delti,1,npar);*/
11234: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
11235: free_matrix(agev,1,maxwav,1,imx);
11236: free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
11237:
11238: free_ivector(ncodemax,1,NCOVMAX);
11239: free_ivector(ncodemaxwundef,1,NCOVMAX);
11240: free_ivector(Dummy,-1,NCOVMAX);
11241: free_ivector(Fixed,-1,NCOVMAX);
11242: free_ivector(Typevar,-1,NCOVMAX);
11243: free_ivector(Tvar,1,NCOVMAX);
1.234 brouard 11244: free_ivector(TvarsQ,1,NCOVMAX);
11245: free_ivector(TvarsQind,1,NCOVMAX);
11246: free_ivector(TvarsD,1,NCOVMAX);
11247: free_ivector(TvarsDind,1,NCOVMAX);
1.231 brouard 11248: free_ivector(TvarFD,1,NCOVMAX);
11249: free_ivector(TvarFDind,1,NCOVMAX);
1.232 brouard 11250: free_ivector(TvarF,1,NCOVMAX);
11251: free_ivector(TvarFind,1,NCOVMAX);
11252: free_ivector(TvarV,1,NCOVMAX);
11253: free_ivector(TvarVind,1,NCOVMAX);
11254: free_ivector(TvarA,1,NCOVMAX);
11255: free_ivector(TvarAind,1,NCOVMAX);
1.231 brouard 11256: free_ivector(TvarFQ,1,NCOVMAX);
11257: free_ivector(TvarFQind,1,NCOVMAX);
11258: free_ivector(TvarVD,1,NCOVMAX);
11259: free_ivector(TvarVDind,1,NCOVMAX);
11260: free_ivector(TvarVQ,1,NCOVMAX);
11261: free_ivector(TvarVQind,1,NCOVMAX);
1.230 brouard 11262: free_ivector(Tvarsel,1,NCOVMAX);
11263: free_vector(Tvalsel,1,NCOVMAX);
1.227 brouard 11264: free_ivector(Tposprod,1,NCOVMAX);
11265: free_ivector(Tprod,1,NCOVMAX);
11266: free_ivector(Tvaraff,1,NCOVMAX);
11267: free_ivector(invalidvarcomb,1,ncovcombmax);
11268: free_ivector(Tage,1,NCOVMAX);
11269: free_ivector(Tmodelind,1,NCOVMAX);
1.228 brouard 11270: free_ivector(TmodelInvind,1,NCOVMAX);
11271: free_ivector(TmodelInvQind,1,NCOVMAX);
1.227 brouard 11272:
11273: free_imatrix(nbcode,0,NCOVMAX,0,NCOVMAX);
11274: /* free_imatrix(codtab,1,100,1,10); */
1.126 brouard 11275: fflush(fichtm);
11276: fflush(ficgp);
11277:
1.227 brouard 11278:
1.126 brouard 11279: if((nberr >0) || (nbwarn>0)){
1.216 brouard 11280: printf("End of Imach with %d errors and/or %d warnings. Please look at the log file for details.\n",nberr,nbwarn);
11281: fprintf(ficlog,"End of Imach with %d errors and/or warnings %d. Please look at the log file for details.\n",nberr,nbwarn);
1.126 brouard 11282: }else{
11283: printf("End of Imach\n");
11284: fprintf(ficlog,"End of Imach\n");
11285: }
11286: printf("See log file on %s\n",filelog);
11287: /* gettimeofday(&end_time, (struct timezone*)0);*/ /* after time */
1.157 brouard 11288: /*(void) gettimeofday(&end_time,&tzp);*/
11289: rend_time = time(NULL);
11290: end_time = *localtime(&rend_time);
11291: /* tml = *localtime(&end_time.tm_sec); */
11292: strcpy(strtend,asctime(&end_time));
1.126 brouard 11293: printf("Local time at start %s\nLocal time at end %s",strstart, strtend);
11294: fprintf(ficlog,"Local time at start %s\nLocal time at end %s\n",strstart, strtend);
1.157 brouard 11295: printf("Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
1.227 brouard 11296:
1.157 brouard 11297: printf("Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
11298: fprintf(ficlog,"Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
11299: fprintf(ficlog,"Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
1.126 brouard 11300: /* printf("Total time was %d uSec.\n", total_usecs);*/
11301: /* if(fileappend(fichtm,optionfilehtm)){ */
11302: fprintf(fichtm,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
11303: fclose(fichtm);
11304: fprintf(fichtmcov,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
11305: fclose(fichtmcov);
11306: fclose(ficgp);
11307: fclose(ficlog);
11308: /*------ End -----------*/
1.227 brouard 11309:
11310:
11311: printf("Before Current directory %s!\n",pathcd);
1.184 brouard 11312: #ifdef WIN32
1.227 brouard 11313: if (_chdir(pathcd) != 0)
11314: printf("Can't move to directory %s!\n",path);
11315: if(_getcwd(pathcd,MAXLINE) > 0)
1.184 brouard 11316: #else
1.227 brouard 11317: if(chdir(pathcd) != 0)
11318: printf("Can't move to directory %s!\n", path);
11319: if (getcwd(pathcd, MAXLINE) > 0)
1.184 brouard 11320: #endif
1.126 brouard 11321: printf("Current directory %s!\n",pathcd);
11322: /*strcat(plotcmd,CHARSEPARATOR);*/
11323: sprintf(plotcmd,"gnuplot");
1.157 brouard 11324: #ifdef _WIN32
1.126 brouard 11325: sprintf(plotcmd,"\"%sgnuplot.exe\"",pathimach);
11326: #endif
11327: if(!stat(plotcmd,&info)){
1.158 brouard 11328: printf("Error or gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 11329: if(!stat(getenv("GNUPLOTBIN"),&info)){
1.158 brouard 11330: printf("Error or gnuplot program not found: '%s' Environment GNUPLOTBIN not set.\n",plotcmd);fflush(stdout);
1.126 brouard 11331: }else
11332: strcpy(pplotcmd,plotcmd);
1.157 brouard 11333: #ifdef __unix
1.126 brouard 11334: strcpy(plotcmd,GNUPLOTPROGRAM);
11335: if(!stat(plotcmd,&info)){
1.158 brouard 11336: printf("Error gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 11337: }else
11338: strcpy(pplotcmd,plotcmd);
11339: #endif
11340: }else
11341: strcpy(pplotcmd,plotcmd);
11342:
11343: sprintf(plotcmd,"%s %s",pplotcmd, optionfilegnuplot);
1.158 brouard 11344: printf("Starting graphs with: '%s'\n",plotcmd);fflush(stdout);
1.227 brouard 11345:
1.126 brouard 11346: if((outcmd=system(plotcmd)) != 0){
1.158 brouard 11347: printf("gnuplot command might not be in your path: '%s', err=%d\n", plotcmd, outcmd);
1.154 brouard 11348: printf("\n Trying if gnuplot resides on the same directory that IMaCh\n");
1.152 brouard 11349: sprintf(plotcmd,"%sgnuplot %s", pathimach, optionfilegnuplot);
1.150 brouard 11350: if((outcmd=system(plotcmd)) != 0)
1.153 brouard 11351: printf("\n Still a problem with gnuplot command %s, err=%d\n", plotcmd, outcmd);
1.126 brouard 11352: }
1.158 brouard 11353: printf(" Successful, please wait...");
1.126 brouard 11354: while (z[0] != 'q') {
11355: /* chdir(path); */
1.154 brouard 11356: printf("\nType e to edit results with your browser, g to graph again and q for exit: ");
1.126 brouard 11357: scanf("%s",z);
11358: /* if (z[0] == 'c') system("./imach"); */
11359: if (z[0] == 'e') {
1.158 brouard 11360: #ifdef __APPLE__
1.152 brouard 11361: sprintf(pplotcmd, "open %s", optionfilehtm);
1.157 brouard 11362: #elif __linux
11363: sprintf(pplotcmd, "xdg-open %s", optionfilehtm);
1.153 brouard 11364: #else
1.152 brouard 11365: sprintf(pplotcmd, "%s", optionfilehtm);
1.153 brouard 11366: #endif
11367: printf("Starting browser with: %s",pplotcmd);fflush(stdout);
11368: system(pplotcmd);
1.126 brouard 11369: }
11370: else if (z[0] == 'g') system(plotcmd);
11371: else if (z[0] == 'q') exit(0);
11372: }
1.227 brouard 11373: end:
1.126 brouard 11374: while (z[0] != 'q') {
1.195 brouard 11375: printf("\nType q for exiting: "); fflush(stdout);
1.126 brouard 11376: scanf("%s",z);
11377: }
11378: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>