1: /* $Id: imachprax.c,v 1.5 2023/10/09 09:10:01 brouard Exp $
2: $State: Exp $
3: $Log: imachprax.c,v $
4: Revision 1.5 2023/10/09 09:10:01 brouard
5: Summary: trying to reconsider
6:
7: Revision 1.4 2023/06/22 12:50:51 brouard
8: Summary: stil on going
9:
10: Revision 1.3 2023/06/22 11:28:07 brouard
11: *** empty log message ***
12:
13: Revision 1.2 2023/06/22 11:22:40 brouard
14: Summary: with svd but not working yet
15:
16: Revision 1.353 2023/05/08 18:48:22 brouard
17: *** empty log message ***
18:
19: Revision 1.352 2023/04/29 10:46:21 brouard
20: *** empty log message ***
21:
22: Revision 1.351 2023/04/29 10:43:47 brouard
23: Summary: 099r45
24:
25: Revision 1.350 2023/04/24 11:38:06 brouard
26: *** empty log message ***
27:
28: Revision 1.349 2023/01/31 09:19:37 brouard
29: Summary: Improvements in models with age*Vn*Vm
30:
31: Revision 1.347 2022/09/18 14:36:44 brouard
32: Summary: version 0.99r42
33:
34: Revision 1.346 2022/09/16 13:52:36 brouard
35: * src/imach.c (Module): 0.99r41 Was an error when product of timevarying and fixed. Using FixedV[of name] now. Thank you Feinuo
36:
37: Revision 1.345 2022/09/16 13:40:11 brouard
38: Summary: Version 0.99r41
39:
40: * imach.c (Module): 0.99r41 Was an error when product of timevarying and fixed. Using FixedV[of name] now. Thank you Feinuo
41:
42: Revision 1.344 2022/09/14 19:33:30 brouard
43: Summary: version 0.99r40
44:
45: * imach.c (Module): Fixing names of variables in T_ (thanks to Feinuo)
46:
47: Revision 1.343 2022/09/14 14:22:16 brouard
48: Summary: version 0.99r39
49:
50: * imach.c (Module): Version 0.99r39 with colored dummy covariates
51: (fixed or time varying), using new last columns of
52: ILK_parameter.txt file.
53:
54: Revision 1.342 2022/09/11 19:54:09 brouard
55: Summary: 0.99r38
56:
57: * imach.c (Module): Adding timevarying products of any kinds,
58: should work before shifting cotvar from ncovcol+nqv columns in
59: order to have a correspondance between the column of cotvar and
60: the id of column.
61: (Module): Some cleaning and adding covariates in ILK.txt
62:
63: Revision 1.341 2022/09/11 07:58:42 brouard
64: Summary: Version 0.99r38
65:
66: After adding change in cotvar.
67:
68: Revision 1.340 2022/09/11 07:53:11 brouard
69: Summary: Version imach 0.99r37
70:
71: * imach.c (Module): Adding timevarying products of any kinds,
72: should work before shifting cotvar from ncovcol+nqv columns in
73: order to have a correspondance between the column of cotvar and
74: the id of column.
75:
76: Revision 1.339 2022/09/09 17:55:22 brouard
77: Summary: version 0.99r37
78:
79: * imach.c (Module): Many improvements for fixing products of fixed
80: timevarying as well as fixed * fixed, and test with quantitative
81: covariate.
82:
83: Revision 1.338 2022/09/04 17:40:33 brouard
84: Summary: 0.99r36
85:
86: * imach.c (Module): Now the easy runs i.e. without result or
87: model=1+age only did not work. The defautl combination should be 1
88: and not 0 because everything hasn't been tranformed yet.
89:
90: Revision 1.337 2022/09/02 14:26:02 brouard
91: Summary: version 0.99r35
92:
93: * src/imach.c: Version 0.99r35 because it outputs same results with
94: 1+age+V1+V1*age for females and 1+age for females only
95: (education=1 noweight)
96:
97: Revision 1.336 2022/08/31 09:52:36 brouard
98: *** empty log message ***
99:
100: Revision 1.335 2022/08/31 08:23:16 brouard
101: Summary: improvements...
102:
103: Revision 1.334 2022/08/25 09:08:41 brouard
104: Summary: In progress for quantitative
105:
106: Revision 1.333 2022/08/21 09:10:30 brouard
107: * src/imach.c (Module): Version 0.99r33 A lot of changes in
108: reassigning covariates: my first idea was that people will always
109: use the first covariate V1 into the model but in fact they are
110: producing data with many covariates and can use an equation model
111: with some of the covariate; it means that in a model V2+V3 instead
112: of codtabm(k,Tvaraff[j]) which calculates for combination k, for
113: three covariates (V1, V2, V3) the value of Tvaraff[j], but in fact
114: the equation model is restricted to two variables only (V2, V3)
115: and the combination for V2 should be codtabm(k,1) instead of
116: (codtabm(k,2), and the code should be
117: codtabm(k,TnsdVar[Tvaraff[j]]. Many many changes have been
118: made. All of these should be simplified once a day like we did in
119: hpxij() for example by using precov[nres] which is computed in
120: decoderesult for each nres of each resultline. Loop should be done
121: on the equation model globally by distinguishing only product with
122: age (which are changing with age) and no more on type of
123: covariates, single dummies, single covariates.
124:
125: Revision 1.332 2022/08/21 09:06:25 brouard
126: Summary: Version 0.99r33
127:
128: * src/imach.c (Module): Version 0.99r33 A lot of changes in
129: reassigning covariates: my first idea was that people will always
130: use the first covariate V1 into the model but in fact they are
131: producing data with many covariates and can use an equation model
132: with some of the covariate; it means that in a model V2+V3 instead
133: of codtabm(k,Tvaraff[j]) which calculates for combination k, for
134: three covariates (V1, V2, V3) the value of Tvaraff[j], but in fact
135: the equation model is restricted to two variables only (V2, V3)
136: and the combination for V2 should be codtabm(k,1) instead of
137: (codtabm(k,2), and the code should be
138: codtabm(k,TnsdVar[Tvaraff[j]]. Many many changes have been
139: made. All of these should be simplified once a day like we did in
140: hpxij() for example by using precov[nres] which is computed in
141: decoderesult for each nres of each resultline. Loop should be done
142: on the equation model globally by distinguishing only product with
143: age (which are changing with age) and no more on type of
144: covariates, single dummies, single covariates.
145:
146: Revision 1.331 2022/08/07 05:40:09 brouard
147: *** empty log message ***
148:
149: Revision 1.330 2022/08/06 07:18:25 brouard
150: Summary: last 0.99r31
151:
152: * imach.c (Module): Version of imach using partly decoderesult to rebuild xpxij function
153:
154: Revision 1.329 2022/08/03 17:29:54 brouard
155: * imach.c (Module): Many errors in graphs fixed with Vn*age covariates.
156:
157: Revision 1.328 2022/07/27 17:40:48 brouard
158: Summary: valgrind bug fixed by initializing to zero DummyV as well as Tage
159:
160: Revision 1.327 2022/07/27 14:47:35 brouard
161: Summary: Still a problem for one-step probabilities in case of quantitative variables
162:
163: Revision 1.326 2022/07/26 17:33:55 brouard
164: Summary: some test with nres=1
165:
166: Revision 1.325 2022/07/25 14:27:23 brouard
167: Summary: r30
168:
169: * imach.c (Module): Error cptcovn instead of nsd in bmij (was
170: coredumped, revealed by Feiuno, thank you.
171:
172: Revision 1.324 2022/07/23 17:44:26 brouard
173: *** empty log message ***
174:
175: Revision 1.323 2022/07/22 12:30:08 brouard
176: * imach.c (Module): Output of Wald test in the htm file and not only in the log.
177:
178: Revision 1.322 2022/07/22 12:27:48 brouard
179: * imach.c (Module): Output of Wald test in the htm file and not only in the log.
180:
181: Revision 1.321 2022/07/22 12:04:24 brouard
182: Summary: r28
183:
184: * imach.c (Module): Output of Wald test in the htm file and not only in the log.
185:
186: Revision 1.320 2022/06/02 05:10:11 brouard
187: *** empty log message ***
188:
189: Revision 1.319 2022/06/02 04:45:11 brouard
190: * imach.c (Module): Adding the Wald tests from the log to the main
191: htm for better display of the maximum likelihood estimators.
192:
193: Revision 1.318 2022/05/24 08:10:59 brouard
194: * imach.c (Module): Some attempts to find a bug of wrong estimates
195: of confidencce intervals with product in the equation modelC
196:
197: Revision 1.317 2022/05/15 15:06:23 brouard
198: * imach.c (Module): Some minor improvements
199:
200: Revision 1.316 2022/05/11 15:11:31 brouard
201: Summary: r27
202:
203: Revision 1.315 2022/05/11 15:06:32 brouard
204: *** empty log message ***
205:
206: Revision 1.314 2022/04/13 17:43:09 brouard
207: * imach.c (Module): Adding link to text data files
208:
209: Revision 1.313 2022/04/11 15:57:42 brouard
210: * imach.c (Module): Error in rewriting the 'r' file with yearsfproj or yearsbproj fixed
211:
212: Revision 1.312 2022/04/05 21:24:39 brouard
213: *** empty log message ***
214:
215: Revision 1.311 2022/04/05 21:03:51 brouard
216: Summary: Fixed quantitative covariates
217:
218: Fixed covariates (dummy or quantitative)
219: with missing values have never been allowed but are ERRORS and
220: program quits. Standard deviations of fixed covariates were
221: wrongly computed. Mean and standard deviations of time varying
222: covariates are still not computed.
223:
224: Revision 1.310 2022/03/17 08:45:53 brouard
225: Summary: 99r25
226:
227: Improving detection of errors: result lines should be compatible with
228: the model.
229:
230: Revision 1.309 2021/05/20 12:39:14 brouard
231: Summary: Version 0.99r24
232:
233: Revision 1.308 2021/03/31 13:11:57 brouard
234: Summary: Version 0.99r23
235:
236:
237: * imach.c (Module): Still bugs in the result loop. Thank to Holly Benett
238:
239: Revision 1.307 2021/03/08 18:11:32 brouard
240: Summary: 0.99r22 fixed bug on result:
241:
242: Revision 1.306 2021/02/20 15:44:02 brouard
243: Summary: Version 0.99r21
244:
245: * imach.c (Module): Fix bug on quitting after result lines!
246: (Module): Version 0.99r21
247:
248: Revision 1.305 2021/02/20 15:28:30 brouard
249: * imach.c (Module): Fix bug on quitting after result lines!
250:
251: Revision 1.304 2021/02/12 11:34:20 brouard
252: * imach.c (Module): The use of a Windows BOM (huge) file is now an error
253:
254: Revision 1.303 2021/02/11 19:50:15 brouard
255: * (Module): imach.c Someone entered 'results:' instead of 'result:'. Now it is an error which is printed.
256:
257: Revision 1.302 2020/02/22 21:00:05 brouard
258: * (Module): imach.c Update mle=-3 (for computing Life expectancy
259: and life table from the data without any state)
260:
261: Revision 1.301 2019/06/04 13:51:20 brouard
262: Summary: Error in 'r'parameter file backcast yearsbproj instead of yearsfproj
263:
264: Revision 1.300 2019/05/22 19:09:45 brouard
265: Summary: version 0.99r19 of May 2019
266:
267: Revision 1.299 2019/05/22 18:37:08 brouard
268: Summary: Cleaned 0.99r19
269:
270: Revision 1.298 2019/05/22 18:19:56 brouard
271: *** empty log message ***
272:
273: Revision 1.297 2019/05/22 17:56:10 brouard
274: Summary: Fix bug by moving date2dmy and nhstepm which gaefin=-1
275:
276: Revision 1.296 2019/05/20 13:03:18 brouard
277: Summary: Projection syntax simplified
278:
279:
280: We can now start projections, forward or backward, from the mean date
281: of inteviews up to or down to a number of years of projection:
282: prevforecast=1 yearsfproj=15.3 mobil_average=0
283: or
284: prevforecast=1 starting-proj-date=1/1/2007 final-proj-date=12/31/2017 mobil_average=0
285: or
286: prevbackcast=1 yearsbproj=12.3 mobil_average=1
287: or
288: prevbackcast=1 starting-back-date=1/10/1999 final-back-date=1/1/1985 mobil_average=1
289:
290: Revision 1.295 2019/05/18 09:52:50 brouard
291: Summary: doxygen tex bug
292:
293: Revision 1.294 2019/05/16 14:54:33 brouard
294: Summary: There was some wrong lines added
295:
296: Revision 1.293 2019/05/09 15:17:34 brouard
297: *** empty log message ***
298:
299: Revision 1.292 2019/05/09 14:17:20 brouard
300: Summary: Some updates
301:
302: Revision 1.291 2019/05/09 13:44:18 brouard
303: Summary: Before ncovmax
304:
305: Revision 1.290 2019/05/09 13:39:37 brouard
306: Summary: 0.99r18 unlimited number of individuals
307:
308: The number n which was limited to 20,000 cases is now unlimited, from firstobs to lastobs. If the number is too for the virtual memory, probably an error will occur.
309:
310: Revision 1.289 2018/12/13 09:16:26 brouard
311: Summary: Bug for young ages (<-30) will be in r17
312:
313: Revision 1.288 2018/05/02 20:58:27 brouard
314: Summary: Some bugs fixed
315:
316: Revision 1.287 2018/05/01 17:57:25 brouard
317: Summary: Bug fixed by providing frequencies only for non missing covariates
318:
319: Revision 1.286 2018/04/27 14:27:04 brouard
320: Summary: some minor bugs
321:
322: Revision 1.285 2018/04/21 21:02:16 brouard
323: Summary: Some bugs fixed, valgrind tested
324:
325: Revision 1.284 2018/04/20 05:22:13 brouard
326: Summary: Computing mean and stdeviation of fixed quantitative variables
327:
328: Revision 1.283 2018/04/19 14:49:16 brouard
329: Summary: Some minor bugs fixed
330:
331: Revision 1.282 2018/02/27 22:50:02 brouard
332: *** empty log message ***
333:
334: Revision 1.281 2018/02/27 19:25:23 brouard
335: Summary: Adding second argument for quitting
336:
337: Revision 1.280 2018/02/21 07:58:13 brouard
338: Summary: 0.99r15
339:
340: New Makefile with recent VirtualBox 5.26. Bug in sqrt negatve in imach.c
341:
342: Revision 1.279 2017/07/20 13:35:01 brouard
343: Summary: temporary working
344:
345: Revision 1.278 2017/07/19 14:09:02 brouard
346: Summary: Bug for mobil_average=0 and prevforecast fixed(?)
347:
348: Revision 1.277 2017/07/17 08:53:49 brouard
349: Summary: BOM files can be read now
350:
351: Revision 1.276 2017/06/30 15:48:31 brouard
352: Summary: Graphs improvements
353:
354: Revision 1.275 2017/06/30 13:39:33 brouard
355: Summary: Saito's color
356:
357: Revision 1.274 2017/06/29 09:47:08 brouard
358: Summary: Version 0.99r14
359:
360: Revision 1.273 2017/06/27 11:06:02 brouard
361: Summary: More documentation on projections
362:
363: Revision 1.272 2017/06/27 10:22:40 brouard
364: Summary: Color of backprojection changed from 6 to 5(yellow)
365:
366: Revision 1.271 2017/06/27 10:17:50 brouard
367: Summary: Some bug with rint
368:
369: Revision 1.270 2017/05/24 05:45:29 brouard
370: *** empty log message ***
371:
372: Revision 1.269 2017/05/23 08:39:25 brouard
373: Summary: Code into subroutine, cleanings
374:
375: Revision 1.268 2017/05/18 20:09:32 brouard
376: Summary: backprojection and confidence intervals of backprevalence
377:
378: Revision 1.267 2017/05/13 10:25:05 brouard
379: Summary: temporary save for backprojection
380:
381: Revision 1.266 2017/05/13 07:26:12 brouard
382: Summary: Version 0.99r13 (improvements and bugs fixed)
383:
384: Revision 1.265 2017/04/26 16:22:11 brouard
385: Summary: imach 0.99r13 Some bugs fixed
386:
387: Revision 1.264 2017/04/26 06:01:29 brouard
388: Summary: Labels in graphs
389:
390: Revision 1.263 2017/04/24 15:23:15 brouard
391: Summary: to save
392:
393: Revision 1.262 2017/04/18 16:48:12 brouard
394: *** empty log message ***
395:
396: Revision 1.261 2017/04/05 10:14:09 brouard
397: Summary: Bug in E_ as well as in T_ fixed nres-1 vs k1-1
398:
399: Revision 1.260 2017/04/04 17:46:59 brouard
400: Summary: Gnuplot indexations fixed (humm)
401:
402: Revision 1.259 2017/04/04 13:01:16 brouard
403: Summary: Some errors to warnings only if date of death is unknown but status is death we could set to pi3
404:
405: Revision 1.258 2017/04/03 10:17:47 brouard
406: Summary: Version 0.99r12
407:
408: Some cleanings, conformed with updated documentation.
409:
410: Revision 1.257 2017/03/29 16:53:30 brouard
411: Summary: Temp
412:
413: Revision 1.256 2017/03/27 05:50:23 brouard
414: Summary: Temporary
415:
416: Revision 1.255 2017/03/08 16:02:28 brouard
417: Summary: IMaCh version 0.99r10 bugs in gnuplot fixed
418:
419: Revision 1.254 2017/03/08 07:13:00 brouard
420: Summary: Fixing data parameter line
421:
422: Revision 1.253 2016/12/15 11:59:41 brouard
423: Summary: 0.99 in progress
424:
425: Revision 1.252 2016/09/15 21:15:37 brouard
426: *** empty log message ***
427:
428: Revision 1.251 2016/09/15 15:01:13 brouard
429: Summary: not working
430:
431: Revision 1.250 2016/09/08 16:07:27 brouard
432: Summary: continue
433:
434: Revision 1.249 2016/09/07 17:14:18 brouard
435: Summary: Starting values from frequencies
436:
437: Revision 1.248 2016/09/07 14:10:18 brouard
438: *** empty log message ***
439:
440: Revision 1.247 2016/09/02 11:11:21 brouard
441: *** empty log message ***
442:
443: Revision 1.246 2016/09/02 08:49:22 brouard
444: *** empty log message ***
445:
446: Revision 1.245 2016/09/02 07:25:01 brouard
447: *** empty log message ***
448:
449: Revision 1.244 2016/09/02 07:17:34 brouard
450: *** empty log message ***
451:
452: Revision 1.243 2016/09/02 06:45:35 brouard
453: *** empty log message ***
454:
455: Revision 1.242 2016/08/30 15:01:20 brouard
456: Summary: Fixing a lots
457:
458: Revision 1.241 2016/08/29 17:17:25 brouard
459: Summary: gnuplot problem in Back projection to fix
460:
461: Revision 1.240 2016/08/29 07:53:18 brouard
462: Summary: Better
463:
464: Revision 1.239 2016/08/26 15:51:03 brouard
465: Summary: Improvement in Powell output in order to copy and paste
466:
467: Author:
468:
469: Revision 1.238 2016/08/26 14:23:35 brouard
470: Summary: Starting tests of 0.99
471:
472: Revision 1.237 2016/08/26 09:20:19 brouard
473: Summary: to valgrind
474:
475: Revision 1.236 2016/08/25 10:50:18 brouard
476: *** empty log message ***
477:
478: Revision 1.235 2016/08/25 06:59:23 brouard
479: *** empty log message ***
480:
481: Revision 1.234 2016/08/23 16:51:20 brouard
482: *** empty log message ***
483:
484: Revision 1.233 2016/08/23 07:40:50 brouard
485: Summary: not working
486:
487: Revision 1.232 2016/08/22 14:20:21 brouard
488: Summary: not working
489:
490: Revision 1.231 2016/08/22 07:17:15 brouard
491: Summary: not working
492:
493: Revision 1.230 2016/08/22 06:55:53 brouard
494: Summary: Not working
495:
496: Revision 1.229 2016/07/23 09:45:53 brouard
497: Summary: Completing for func too
498:
499: Revision 1.228 2016/07/22 17:45:30 brouard
500: Summary: Fixing some arrays, still debugging
501:
502: Revision 1.226 2016/07/12 18:42:34 brouard
503: Summary: temp
504:
505: Revision 1.225 2016/07/12 08:40:03 brouard
506: Summary: saving but not running
507:
508: Revision 1.224 2016/07/01 13:16:01 brouard
509: Summary: Fixes
510:
511: Revision 1.223 2016/02/19 09:23:35 brouard
512: Summary: temporary
513:
514: Revision 1.222 2016/02/17 08:14:50 brouard
515: Summary: Probably last 0.98 stable version 0.98r6
516:
517: Revision 1.221 2016/02/15 23:35:36 brouard
518: Summary: minor bug
519:
520: Revision 1.219 2016/02/15 00:48:12 brouard
521: *** empty log message ***
522:
523: Revision 1.218 2016/02/12 11:29:23 brouard
524: Summary: 0.99 Back projections
525:
526: Revision 1.217 2015/12/23 17:18:31 brouard
527: Summary: Experimental backcast
528:
529: Revision 1.216 2015/12/18 17:32:11 brouard
530: Summary: 0.98r4 Warning and status=-2
531:
532: Version 0.98r4 is now:
533: - displaying an error when status is -1, date of interview unknown and date of death known;
534: - permitting a status -2 when the vital status is unknown at a known date of right truncation.
535: Older changes concerning s=-2, dating from 2005 have been supersed.
536:
537: Revision 1.215 2015/12/16 08:52:24 brouard
538: Summary: 0.98r4 working
539:
540: Revision 1.214 2015/12/16 06:57:54 brouard
541: Summary: temporary not working
542:
543: Revision 1.213 2015/12/11 18:22:17 brouard
544: Summary: 0.98r4
545:
546: Revision 1.212 2015/11/21 12:47:24 brouard
547: Summary: minor typo
548:
549: Revision 1.211 2015/11/21 12:41:11 brouard
550: Summary: 0.98r3 with some graph of projected cross-sectional
551:
552: Author: Nicolas Brouard
553:
554: Revision 1.210 2015/11/18 17:41:20 brouard
555: Summary: Start working on projected prevalences Revision 1.209 2015/11/17 22:12:03 brouard
556: Summary: Adding ftolpl parameter
557: Author: N Brouard
558:
559: We had difficulties to get smoothed confidence intervals. It was due
560: to the period prevalence which wasn't computed accurately. The inner
561: parameter ftolpl is now an outer parameter of the .imach parameter
562: file after estepm. If ftolpl is small 1.e-4 and estepm too,
563: computation are long.
564:
565: Revision 1.208 2015/11/17 14:31:57 brouard
566: Summary: temporary
567:
568: Revision 1.207 2015/10/27 17:36:57 brouard
569: *** empty log message ***
570:
571: Revision 1.206 2015/10/24 07:14:11 brouard
572: *** empty log message ***
573:
574: Revision 1.205 2015/10/23 15:50:53 brouard
575: Summary: 0.98r3 some clarification for graphs on likelihood contributions
576:
577: Revision 1.204 2015/10/01 16:20:26 brouard
578: Summary: Some new graphs of contribution to likelihood
579:
580: Revision 1.203 2015/09/30 17:45:14 brouard
581: Summary: looking at better estimation of the hessian
582:
583: Also a better criteria for convergence to the period prevalence And
584: therefore adding the number of years needed to converge. (The
585: prevalence in any alive state shold sum to one
586:
587: Revision 1.202 2015/09/22 19:45:16 brouard
588: Summary: Adding some overall graph on contribution to likelihood. Might change
589:
590: Revision 1.201 2015/09/15 17:34:58 brouard
591: Summary: 0.98r0
592:
593: - Some new graphs like suvival functions
594: - Some bugs fixed like model=1+age+V2.
595:
596: Revision 1.200 2015/09/09 16:53:55 brouard
597: Summary: Big bug thanks to Flavia
598:
599: Even model=1+age+V2. did not work anymore
600:
601: Revision 1.199 2015/09/07 14:09:23 brouard
602: Summary: 0.98q6 changing default small png format for graph to vectorized svg.
603:
604: Revision 1.198 2015/09/03 07:14:39 brouard
605: Summary: 0.98q5 Flavia
606:
607: Revision 1.197 2015/09/01 18:24:39 brouard
608: *** empty log message ***
609:
610: Revision 1.196 2015/08/18 23:17:52 brouard
611: Summary: 0.98q5
612:
613: Revision 1.195 2015/08/18 16:28:39 brouard
614: Summary: Adding a hack for testing purpose
615:
616: After reading the title, ftol and model lines, if the comment line has
617: a q, starting with #q, the answer at the end of the run is quit. It
618: permits to run test files in batch with ctest. The former workaround was
619: $ echo q | imach foo.imach
620:
621: Revision 1.194 2015/08/18 13:32:00 brouard
622: Summary: Adding error when the covariance matrix doesn't contain the exact number of lines required by the model line.
623:
624: Revision 1.193 2015/08/04 07:17:42 brouard
625: Summary: 0.98q4
626:
627: Revision 1.192 2015/07/16 16:49:02 brouard
628: Summary: Fixing some outputs
629:
630: Revision 1.191 2015/07/14 10:00:33 brouard
631: Summary: Some fixes
632:
633: Revision 1.190 2015/05/05 08:51:13 brouard
634: Summary: Adding digits in output parameters (7 digits instead of 6)
635:
636: Fix 1+age+.
637:
638: Revision 1.189 2015/04/30 14:45:16 brouard
639: Summary: 0.98q2
640:
641: Revision 1.188 2015/04/30 08:27:53 brouard
642: *** empty log message ***
643:
644: Revision 1.187 2015/04/29 09:11:15 brouard
645: *** empty log message ***
646:
647: Revision 1.186 2015/04/23 12:01:52 brouard
648: Summary: V1*age is working now, version 0.98q1
649:
650: Some codes had been disabled in order to simplify and Vn*age was
651: working in the optimization phase, ie, giving correct MLE parameters,
652: but, as usual, outputs were not correct and program core dumped.
653:
654: Revision 1.185 2015/03/11 13:26:42 brouard
655: Summary: Inclusion of compile and links command line for Intel Compiler
656:
657: Revision 1.184 2015/03/11 11:52:39 brouard
658: Summary: Back from Windows 8. Intel Compiler
659:
660: Revision 1.183 2015/03/10 20:34:32 brouard
661: Summary: 0.98q0, trying with directest, mnbrak fixed
662:
663: We use directest instead of original Powell test; probably no
664: incidence on the results, but better justifications;
665: We fixed Numerical Recipes mnbrak routine which was wrong and gave
666: wrong results.
667:
668: Revision 1.182 2015/02/12 08:19:57 brouard
669: Summary: Trying to keep directest which seems simpler and more general
670: Author: Nicolas Brouard
671:
672: Revision 1.181 2015/02/11 23:22:24 brouard
673: Summary: Comments on Powell added
674:
675: Author:
676:
677: Revision 1.180 2015/02/11 17:33:45 brouard
678: Summary: Finishing move from main to function (hpijx and prevalence_limit)
679:
680: Revision 1.179 2015/01/04 09:57:06 brouard
681: Summary: back to OS/X
682:
683: Revision 1.178 2015/01/04 09:35:48 brouard
684: *** empty log message ***
685:
686: Revision 1.177 2015/01/03 18:40:56 brouard
687: Summary: Still testing ilc32 on OSX
688:
689: Revision 1.176 2015/01/03 16:45:04 brouard
690: *** empty log message ***
691:
692: Revision 1.175 2015/01/03 16:33:42 brouard
693: *** empty log message ***
694:
695: Revision 1.174 2015/01/03 16:15:49 brouard
696: Summary: Still in cross-compilation
697:
698: Revision 1.173 2015/01/03 12:06:26 brouard
699: Summary: trying to detect cross-compilation
700:
701: Revision 1.172 2014/12/27 12:07:47 brouard
702: Summary: Back from Visual Studio and Intel, options for compiling for Windows XP
703:
704: Revision 1.171 2014/12/23 13:26:59 brouard
705: Summary: Back from Visual C
706:
707: Still problem with utsname.h on Windows
708:
709: Revision 1.170 2014/12/23 11:17:12 brouard
710: Summary: Cleaning some \%% back to %%
711:
712: The escape was mandatory for a specific compiler (which one?), but too many warnings.
713:
714: Revision 1.169 2014/12/22 23:08:31 brouard
715: Summary: 0.98p
716:
717: Outputs some informations on compiler used, OS etc. Testing on different platforms.
718:
719: Revision 1.168 2014/12/22 15:17:42 brouard
720: Summary: update
721:
722: Revision 1.167 2014/12/22 13:50:56 brouard
723: Summary: Testing uname and compiler version and if compiled 32 or 64
724:
725: Testing on Linux 64
726:
727: Revision 1.166 2014/12/22 11:40:47 brouard
728: *** empty log message ***
729:
730: Revision 1.165 2014/12/16 11:20:36 brouard
731: Summary: After compiling on Visual C
732:
733: * imach.c (Module): Merging 1.61 to 1.162
734:
735: Revision 1.164 2014/12/16 10:52:11 brouard
736: Summary: Merging with Visual C after suppressing some warnings for unused variables. Also fixing Saito's bug 0.98Xn
737:
738: * imach.c (Module): Merging 1.61 to 1.162
739:
740: Revision 1.163 2014/12/16 10:30:11 brouard
741: * imach.c (Module): Merging 1.61 to 1.162
742:
743: Revision 1.162 2014/09/25 11:43:39 brouard
744: Summary: temporary backup 0.99!
745:
746: Revision 1.1 2014/09/16 11:06:58 brouard
747: Summary: With some code (wrong) for nlopt
748:
749: Author:
750:
751: Revision 1.161 2014/09/15 20:41:41 brouard
752: Summary: Problem with macro SQR on Intel compiler
753:
754: Revision 1.160 2014/09/02 09:24:05 brouard
755: *** empty log message ***
756:
757: Revision 1.159 2014/09/01 10:34:10 brouard
758: Summary: WIN32
759: Author: Brouard
760:
761: Revision 1.158 2014/08/27 17:11:51 brouard
762: *** empty log message ***
763:
764: Revision 1.157 2014/08/27 16:26:55 brouard
765: Summary: Preparing windows Visual studio version
766: Author: Brouard
767:
768: In order to compile on Visual studio, time.h is now correct and time_t
769: and tm struct should be used. difftime should be used but sometimes I
770: just make the differences in raw time format (time(&now).
771: Trying to suppress #ifdef LINUX
772: Add xdg-open for __linux in order to open default browser.
773:
774: Revision 1.156 2014/08/25 20:10:10 brouard
775: *** empty log message ***
776:
777: Revision 1.155 2014/08/25 18:32:34 brouard
778: Summary: New compile, minor changes
779: Author: Brouard
780:
781: Revision 1.154 2014/06/20 17:32:08 brouard
782: Summary: Outputs now all graphs of convergence to period prevalence
783:
784: Revision 1.153 2014/06/20 16:45:46 brouard
785: Summary: If 3 live state, convergence to period prevalence on same graph
786: Author: Brouard
787:
788: Revision 1.152 2014/06/18 17:54:09 brouard
789: Summary: open browser, use gnuplot on same dir than imach if not found in the path
790:
791: Revision 1.151 2014/06/18 16:43:30 brouard
792: *** empty log message ***
793:
794: Revision 1.150 2014/06/18 16:42:35 brouard
795: Summary: If gnuplot is not in the path try on same directory than imach binary (OSX)
796: Author: brouard
797:
798: Revision 1.149 2014/06/18 15:51:14 brouard
799: Summary: Some fixes in parameter files errors
800: Author: Nicolas Brouard
801:
802: Revision 1.148 2014/06/17 17:38:48 brouard
803: Summary: Nothing new
804: Author: Brouard
805:
806: Just a new packaging for OS/X version 0.98nS
807:
808: Revision 1.147 2014/06/16 10:33:11 brouard
809: *** empty log message ***
810:
811: Revision 1.146 2014/06/16 10:20:28 brouard
812: Summary: Merge
813: Author: Brouard
814:
815: Merge, before building revised version.
816:
817: Revision 1.145 2014/06/10 21:23:15 brouard
818: Summary: Debugging with valgrind
819: Author: Nicolas Brouard
820:
821: Lot of changes in order to output the results with some covariates
822: After the Edimburgh REVES conference 2014, it seems mandatory to
823: improve the code.
824: No more memory valgrind error but a lot has to be done in order to
825: continue the work of splitting the code into subroutines.
826: Also, decodemodel has been improved. Tricode is still not
827: optimal. nbcode should be improved. Documentation has been added in
828: the source code.
829:
830: Revision 1.143 2014/01/26 09:45:38 brouard
831: Summary: Version 0.98nR (to be improved, but gives same optimization results as 0.98k. Nice, promising
832:
833: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
834: (Module): Version 0.98nR Running ok, but output format still only works for three covariates.
835:
836: Revision 1.142 2014/01/26 03:57:36 brouard
837: Summary: gnuplot changed plot w l 1 has to be changed to plot w l lt 2
838:
839: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
840:
841: Revision 1.141 2014/01/26 02:42:01 brouard
842: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
843:
844: Revision 1.140 2011/09/02 10:37:54 brouard
845: Summary: times.h is ok with mingw32 now.
846:
847: Revision 1.139 2010/06/14 07:50:17 brouard
848: After the theft of my laptop, I probably lost some lines of codes which were not uploaded to the CVS tree.
849: I remember having already fixed agemin agemax which are pointers now but not cvs saved.
850:
851: Revision 1.138 2010/04/30 18:19:40 brouard
852: *** empty log message ***
853:
854: Revision 1.137 2010/04/29 18:11:38 brouard
855: (Module): Checking covariates for more complex models
856: than V1+V2. A lot of change to be done. Unstable.
857:
858: Revision 1.136 2010/04/26 20:30:53 brouard
859: (Module): merging some libgsl code. Fixing computation
860: of likelione (using inter/intrapolation if mle = 0) in order to
861: get same likelihood as if mle=1.
862: Some cleaning of code and comments added.
863:
864: Revision 1.135 2009/10/29 15:33:14 brouard
865: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
866:
867: Revision 1.134 2009/10/29 13:18:53 brouard
868: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
869:
870: Revision 1.133 2009/07/06 10:21:25 brouard
871: just nforces
872:
873: Revision 1.132 2009/07/06 08:22:05 brouard
874: Many tings
875:
876: Revision 1.131 2009/06/20 16:22:47 brouard
877: Some dimensions resccaled
878:
879: Revision 1.130 2009/05/26 06:44:34 brouard
880: (Module): Max Covariate is now set to 20 instead of 8. A
881: lot of cleaning with variables initialized to 0. Trying to make
882: V2+V3*age+V1+V4 strb=V3*age+V1+V4 working better.
883:
884: Revision 1.129 2007/08/31 13:49:27 lievre
885: Modification of the way of exiting when the covariate is not binary in order to see on the window the error message before exiting
886:
887: Revision 1.128 2006/06/30 13:02:05 brouard
888: (Module): Clarifications on computing e.j
889:
890: Revision 1.127 2006/04/28 18:11:50 brouard
891: (Module): Yes the sum of survivors was wrong since
892: imach-114 because nhstepm was no more computed in the age
893: loop. Now we define nhstepma in the age loop.
894: (Module): In order to speed up (in case of numerous covariates) we
895: compute health expectancies (without variances) in a first step
896: and then all the health expectancies with variances or standard
897: deviation (needs data from the Hessian matrices) which slows the
898: computation.
899: In the future we should be able to stop the program is only health
900: expectancies and graph are needed without standard deviations.
901:
902: Revision 1.126 2006/04/28 17:23:28 brouard
903: (Module): Yes the sum of survivors was wrong since
904: imach-114 because nhstepm was no more computed in the age
905: loop. Now we define nhstepma in the age loop.
906: Version 0.98h
907:
908: Revision 1.125 2006/04/04 15:20:31 lievre
909: Errors in calculation of health expectancies. Age was not initialized.
910: Forecasting file added.
911:
912: Revision 1.124 2006/03/22 17:13:53 lievre
913: Parameters are printed with %lf instead of %f (more numbers after the comma).
914: The log-likelihood is printed in the log file
915:
916: Revision 1.123 2006/03/20 10:52:43 brouard
917: * imach.c (Module): <title> changed, corresponds to .htm file
918: name. <head> headers where missing.
919:
920: * imach.c (Module): Weights can have a decimal point as for
921: English (a comma might work with a correct LC_NUMERIC environment,
922: otherwise the weight is truncated).
923: Modification of warning when the covariates values are not 0 or
924: 1.
925: Version 0.98g
926:
927: Revision 1.122 2006/03/20 09:45:41 brouard
928: (Module): Weights can have a decimal point as for
929: English (a comma might work with a correct LC_NUMERIC environment,
930: otherwise the weight is truncated).
931: Modification of warning when the covariates values are not 0 or
932: 1.
933: Version 0.98g
934:
935: Revision 1.121 2006/03/16 17:45:01 lievre
936: * imach.c (Module): Comments concerning covariates added
937:
938: * imach.c (Module): refinements in the computation of lli if
939: status=-2 in order to have more reliable computation if stepm is
940: not 1 month. Version 0.98f
941:
942: Revision 1.120 2006/03/16 15:10:38 lievre
943: (Module): refinements in the computation of lli if
944: status=-2 in order to have more reliable computation if stepm is
945: not 1 month. Version 0.98f
946:
947: Revision 1.119 2006/03/15 17:42:26 brouard
948: (Module): Bug if status = -2, the loglikelihood was
949: computed as likelihood omitting the logarithm. Version O.98e
950:
951: Revision 1.118 2006/03/14 18:20:07 brouard
952: (Module): varevsij Comments added explaining the second
953: table of variances if popbased=1 .
954: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
955: (Module): Function pstamp added
956: (Module): Version 0.98d
957:
958: Revision 1.117 2006/03/14 17:16:22 brouard
959: (Module): varevsij Comments added explaining the second
960: table of variances if popbased=1 .
961: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
962: (Module): Function pstamp added
963: (Module): Version 0.98d
964:
965: Revision 1.116 2006/03/06 10:29:27 brouard
966: (Module): Variance-covariance wrong links and
967: varian-covariance of ej. is needed (Saito).
968:
969: Revision 1.115 2006/02/27 12:17:45 brouard
970: (Module): One freematrix added in mlikeli! 0.98c
971:
972: Revision 1.114 2006/02/26 12:57:58 brouard
973: (Module): Some improvements in processing parameter
974: filename with strsep.
975:
976: Revision 1.113 2006/02/24 14:20:24 brouard
977: (Module): Memory leaks checks with valgrind and:
978: datafile was not closed, some imatrix were not freed and on matrix
979: allocation too.
980:
981: Revision 1.112 2006/01/30 09:55:26 brouard
982: (Module): Back to gnuplot.exe instead of wgnuplot.exe
983:
984: Revision 1.111 2006/01/25 20:38:18 brouard
985: (Module): Lots of cleaning and bugs added (Gompertz)
986: (Module): Comments can be added in data file. Missing date values
987: can be a simple dot '.'.
988:
989: Revision 1.110 2006/01/25 00:51:50 brouard
990: (Module): Lots of cleaning and bugs added (Gompertz)
991:
992: Revision 1.109 2006/01/24 19:37:15 brouard
993: (Module): Comments (lines starting with a #) are allowed in data.
994:
995: Revision 1.108 2006/01/19 18:05:42 lievre
996: Gnuplot problem appeared...
997: To be fixed
998:
999: Revision 1.107 2006/01/19 16:20:37 brouard
1000: Test existence of gnuplot in imach path
1001:
1002: Revision 1.106 2006/01/19 13:24:36 brouard
1003: Some cleaning and links added in html output
1004:
1005: Revision 1.105 2006/01/05 20:23:19 lievre
1006: *** empty log message ***
1007:
1008: Revision 1.104 2005/09/30 16:11:43 lievre
1009: (Module): sump fixed, loop imx fixed, and simplifications.
1010: (Module): If the status is missing at the last wave but we know
1011: that the person is alive, then we can code his/her status as -2
1012: (instead of missing=-1 in earlier versions) and his/her
1013: contributions to the likelihood is 1 - Prob of dying from last
1014: health status (= 1-p13= p11+p12 in the easiest case of somebody in
1015: the healthy state at last known wave). Version is 0.98
1016:
1017: Revision 1.103 2005/09/30 15:54:49 lievre
1018: (Module): sump fixed, loop imx fixed, and simplifications.
1019:
1020: Revision 1.102 2004/09/15 17:31:30 brouard
1021: Add the possibility to read data file including tab characters.
1022:
1023: Revision 1.101 2004/09/15 10:38:38 brouard
1024: Fix on curr_time
1025:
1026: Revision 1.100 2004/07/12 18:29:06 brouard
1027: Add version for Mac OS X. Just define UNIX in Makefile
1028:
1029: Revision 1.99 2004/06/05 08:57:40 brouard
1030: *** empty log message ***
1031:
1032: Revision 1.98 2004/05/16 15:05:56 brouard
1033: New version 0.97 . First attempt to estimate force of mortality
1034: directly from the data i.e. without the need of knowing the health
1035: state at each age, but using a Gompertz model: log u =a + b*age .
1036: This is the basic analysis of mortality and should be done before any
1037: other analysis, in order to test if the mortality estimated from the
1038: cross-longitudinal survey is different from the mortality estimated
1039: from other sources like vital statistic data.
1040:
1041: The same imach parameter file can be used but the option for mle should be -3.
1042:
1043: Agnès, who wrote this part of the code, tried to keep most of the
1044: former routines in order to include the new code within the former code.
1045:
1046: The output is very simple: only an estimate of the intercept and of
1047: the slope with 95% confident intervals.
1048:
1049: Current limitations:
1050: A) Even if you enter covariates, i.e. with the
1051: model= V1+V2 equation for example, the programm does only estimate a unique global model without covariates.
1052: B) There is no computation of Life Expectancy nor Life Table.
1053:
1054: Revision 1.97 2004/02/20 13:25:42 lievre
1055: Version 0.96d. Population forecasting command line is (temporarily)
1056: suppressed.
1057:
1058: Revision 1.96 2003/07/15 15:38:55 brouard
1059: * imach.c (Repository): Errors in subdirf, 2, 3 while printing tmpout is
1060: rewritten within the same printf. Workaround: many printfs.
1061:
1062: Revision 1.95 2003/07/08 07:54:34 brouard
1063: * imach.c (Repository):
1064: (Repository): Using imachwizard code to output a more meaningful covariance
1065: matrix (cov(a12,c31) instead of numbers.
1066:
1067: Revision 1.94 2003/06/27 13:00:02 brouard
1068: Just cleaning
1069:
1070: Revision 1.93 2003/06/25 16:33:55 brouard
1071: (Module): On windows (cygwin) function asctime_r doesn't
1072: exist so I changed back to asctime which exists.
1073: (Module): Version 0.96b
1074:
1075: Revision 1.92 2003/06/25 16:30:45 brouard
1076: (Module): On windows (cygwin) function asctime_r doesn't
1077: exist so I changed back to asctime which exists.
1078:
1079: Revision 1.91 2003/06/25 15:30:29 brouard
1080: * imach.c (Repository): Duplicated warning errors corrected.
1081: (Repository): Elapsed time after each iteration is now output. It
1082: helps to forecast when convergence will be reached. Elapsed time
1083: is stamped in powell. We created a new html file for the graphs
1084: concerning matrix of covariance. It has extension -cov.htm.
1085:
1086: Revision 1.90 2003/06/24 12:34:15 brouard
1087: (Module): Some bugs corrected for windows. Also, when
1088: mle=-1 a template is output in file "or"mypar.txt with the design
1089: of the covariance matrix to be input.
1090:
1091: Revision 1.89 2003/06/24 12:30:52 brouard
1092: (Module): Some bugs corrected for windows. Also, when
1093: mle=-1 a template is output in file "or"mypar.txt with the design
1094: of the covariance matrix to be input.
1095:
1096: Revision 1.88 2003/06/23 17:54:56 brouard
1097: * 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.
1098:
1099: Revision 1.87 2003/06/18 12:26:01 brouard
1100: Version 0.96
1101:
1102: Revision 1.86 2003/06/17 20:04:08 brouard
1103: (Module): Change position of html and gnuplot routines and added
1104: routine fileappend.
1105:
1106: Revision 1.85 2003/06/17 13:12:43 brouard
1107: * imach.c (Repository): Check when date of death was earlier that
1108: current date of interview. It may happen when the death was just
1109: prior to the death. In this case, dh was negative and likelihood
1110: was wrong (infinity). We still send an "Error" but patch by
1111: assuming that the date of death was just one stepm after the
1112: interview.
1113: (Repository): Because some people have very long ID (first column)
1114: we changed int to long in num[] and we added a new lvector for
1115: memory allocation. But we also truncated to 8 characters (left
1116: truncation)
1117: (Repository): No more line truncation errors.
1118:
1119: Revision 1.84 2003/06/13 21:44:43 brouard
1120: * imach.c (Repository): Replace "freqsummary" at a correct
1121: place. It differs from routine "prevalence" which may be called
1122: many times. Probs is memory consuming and must be used with
1123: parcimony.
1124: Version 0.95a3 (should output exactly the same maximization than 0.8a2)
1125:
1126: Revision 1.83 2003/06/10 13:39:11 lievre
1127: *** empty log message ***
1128:
1129: Revision 1.82 2003/06/05 15:57:20 brouard
1130: Add log in imach.c and fullversion number is now printed.
1131:
1132: */
1133: /*
1134: Interpolated Markov Chain
1135:
1136: Short summary of the programme:
1137:
1138: This program computes Healthy Life Expectancies or State-specific
1139: (if states aren't health statuses) Expectancies from
1140: cross-longitudinal data. Cross-longitudinal data consist in:
1141:
1142: -1- a first survey ("cross") where individuals from different ages
1143: are interviewed on their health status or degree of disability (in
1144: the case of a health survey which is our main interest)
1145:
1146: -2- at least a second wave of interviews ("longitudinal") which
1147: measure each change (if any) in individual health status. Health
1148: expectancies are computed from the time spent in each health state
1149: according to a model. More health states you consider, more time is
1150: necessary to reach the Maximum Likelihood of the parameters involved
1151: in the model. The simplest model is the multinomial logistic model
1152: where pij is the probability to be observed in state j at the second
1153: wave conditional to be observed in state i at the first
1154: wave. Therefore the model is: log(pij/pii)= aij + bij*age+ cij*sex +
1155: etc , where 'age' is age and 'sex' is a covariate. If you want to
1156: have a more complex model than "constant and age", you should modify
1157: the program where the markup *Covariates have to be included here
1158: again* invites you to do it. More covariates you add, slower the
1159: convergence.
1160:
1161: The advantage of this computer programme, compared to a simple
1162: multinomial logistic model, is clear when the delay between waves is not
1163: identical for each individual. Also, if a individual missed an
1164: intermediate interview, the information is lost, but taken into
1165: account using an interpolation or extrapolation.
1166:
1167: hPijx is the probability to be observed in state i at age x+h
1168: conditional to the observed state i at age x. The delay 'h' can be
1169: split into an exact number (nh*stepm) of unobserved intermediate
1170: states. This elementary transition (by month, quarter,
1171: semester or year) is modelled as a multinomial logistic. The hPx
1172: matrix is simply the matrix product of nh*stepm elementary matrices
1173: and the contribution of each individual to the likelihood is simply
1174: hPijx.
1175:
1176: Also this programme outputs the covariance matrix of the parameters but also
1177: of the life expectancies. It also computes the period (stable) prevalence.
1178:
1179: Back prevalence and projections:
1180:
1181: - back_prevalence_limit(double *p, double **bprlim, double ageminpar,
1182: double agemaxpar, double ftolpl, int *ncvyearp, double
1183: dateprev1,double dateprev2, int firstpass, int lastpass, int
1184: mobilavproj)
1185:
1186: Computes the back prevalence limit for any combination of
1187: covariate values k at any age between ageminpar and agemaxpar and
1188: returns it in **bprlim. In the loops,
1189:
1190: - **bprevalim(**bprlim, ***mobaverage, nlstate, *p, age, **oldm,
1191: **savm, **dnewm, **doldm, **dsavm, ftolpl, ncvyearp, k);
1192:
1193: - hBijx Back Probability to be in state i at age x-h being in j at x
1194: Computes for any combination of covariates k and any age between bage and fage
1195: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
1196: oldm=oldms;savm=savms;
1197:
1198: - hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k, nres);
1199: Computes the transition matrix starting at age 'age' over
1200: 'nhstepm*hstepm*stepm' months (i.e. until
1201: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
1202: nhstepm*hstepm matrices.
1203:
1204: Returns p3mat[i][j][h] after calling
1205: p3mat[i][j][h]=matprod2(newm,
1206: bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm,
1207: dsavm,ij),\ 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
1208: oldm);
1209:
1210: Important routines
1211:
1212: - func (or funcone), computes logit (pij) distinguishing
1213: o fixed variables (single or product dummies or quantitative);
1214: o varying variables by:
1215: (1) wave (single, product dummies, quantitative),
1216: (2) by age (can be month) age (done), age*age (done), age*Vn where Vn can be:
1217: % fixed dummy (treated) or quantitative (not done because time-consuming);
1218: % varying dummy (not done) or quantitative (not done);
1219: - Tricode which tests the modality of dummy variables (in order to warn with wrong or empty modalities)
1220: and returns the number of efficient covariates cptcoveff and modalities nbcode[Tvar[k]][1]= 0 and nbcode[Tvar[k]][2]= 1 usually.
1221: - printinghtml which outputs results like life expectancy in and from a state for a combination of modalities of dummy variables
1222: o There are 2**cptcoveff combinations of (0,1) for cptcoveff variables. Outputting only combinations with people, éliminating 1 1 if
1223: race White (0 0), Black vs White (1 0), Hispanic (0 1) and 1 1 being meaningless.
1224:
1225:
1226:
1227: Authors: Nicolas Brouard (brouard@ined.fr) and Agnès Lièvre (lievre@ined.fr).
1228: Institut national d'études démographiques, Paris.
1229: This software have been partly granted by Euro-REVES, a concerted action
1230: from the European Union.
1231: It is copyrighted identically to a GNU software product, ie programme and
1232: software can be distributed freely for non commercial use. Latest version
1233: can be accessed at http://euroreves.ined.fr/imach .
1234:
1235: Help to debug: LD_PRELOAD=/usr/local/lib/libnjamd.so ./imach foo.imach
1236: or better on gdb : set env LD_PRELOAD=/usr/local/lib/libnjamd.so
1237:
1238: **********************************************************************/
1239: /*
1240: main
1241: read parameterfile
1242: read datafile
1243: concatwav
1244: freqsummary
1245: if (mle >= 1)
1246: mlikeli
1247: print results files
1248: if mle==1
1249: computes hessian
1250: read end of parameter file: agemin, agemax, bage, fage, estepm
1251: begin-prev-date,...
1252: open gnuplot file
1253: open html file
1254: period (stable) prevalence | pl_nom 1-1 2-2 etc by covariate
1255: for age prevalim() | #****** V1=0 V2=1 V3=1 V4=0 ******
1256: | 65 1 0 2 1 3 1 4 0 0.96326 0.03674
1257: freexexit2 possible for memory heap.
1258:
1259: h Pij x | pij_nom ficrestpij
1260: # Cov Agex agex+h hpijx with i,j= 1-1 1-2 1-3 2-1 2-2 2-3
1261: 1 85 85 1.00000 0.00000 0.00000 0.00000 1.00000 0.00000
1262: 1 85 86 0.68299 0.22291 0.09410 0.71093 0.00000 0.28907
1263:
1264: 1 65 99 0.00364 0.00322 0.99314 0.00350 0.00310 0.99340
1265: 1 65 100 0.00214 0.00204 0.99581 0.00206 0.00196 0.99597
1266: variance of p one-step probabilities varprob | prob_nom ficresprob #One-step probabilities and stand. devi in ()
1267: Standard deviation of one-step probabilities | probcor_nom ficresprobcor #One-step probabilities and correlation matrix
1268: Matrix of variance covariance of one-step probabilities | probcov_nom ficresprobcov #One-step probabilities and covariance matrix
1269:
1270: forecasting if prevfcast==1 prevforecast call prevalence()
1271: health expectancies
1272: Variance-covariance of DFLE
1273: prevalence()
1274: movingaverage()
1275: varevsij()
1276: if popbased==1 varevsij(,popbased)
1277: total life expectancies
1278: Variance of period (stable) prevalence
1279: end
1280: */
1281:
1282: /* #define DEBUG */
1283: /* #define DEBUGBRENT */
1284: /* #define DEBUGLINMIN */
1285: /* #define DEBUGHESS */
1286: #define DEBUGHESSIJ
1287: /* #define LINMINORIGINAL /\* Don't use loop on scale in linmin (accepting nan) *\/ */
1288: #define POWELL /* Instead of NLOPT */
1289: #define POWELLNOF3INFF1TEST /* Skip test */
1290: /* #define POWELLORIGINAL /\* Don't use Directest to decide new direction but original Powell test *\/ */
1291: /* #define MNBRAKORIGINAL /\* Don't use mnbrak fix *\/ */
1292: /* #define FLATSUP *//* Suppresses directions where likelihood is flat */
1293: /* #define POWELLORIGINCONJUGATE /\* Don't use conjugate but biggest decrease if valuable *\/ */
1294: /* #define NOTMINFIT */
1295:
1296: #include <math.h>
1297: #include <stdio.h>
1298: #include <stdlib.h>
1299: #include <string.h>
1300: #include <ctype.h>
1301:
1302: #ifdef _WIN32
1303: #include <io.h>
1304: #include <windows.h>
1305: #include <tchar.h>
1306: #else
1307: #include <unistd.h>
1308: #endif
1309:
1310: #include <limits.h>
1311: #include <sys/types.h>
1312:
1313: #if defined(__GNUC__)
1314: #include <sys/utsname.h> /* Doesn't work on Windows */
1315: #endif
1316:
1317: #include <sys/stat.h>
1318: #include <errno.h>
1319: /* extern int errno; */
1320:
1321: /* #ifdef LINUX */
1322: /* #include <time.h> */
1323: /* #include "timeval.h" */
1324: /* #else */
1325: /* #include <sys/time.h> */
1326: /* #endif */
1327:
1328: #include <time.h>
1329:
1330: #ifdef GSL
1331: #include <gsl/gsl_errno.h>
1332: #include <gsl/gsl_multimin.h>
1333: #endif
1334:
1335:
1336: #ifdef NLOPT
1337: #include <nlopt.h>
1338: typedef struct {
1339: double (* function)(double [] );
1340: } myfunc_data ;
1341: #endif
1342:
1343: /* #include <libintl.h> */
1344: /* #define _(String) gettext (String) */
1345:
1346: #define MAXLINE 16384 /* Was 256 and 1024 and 2048. Overflow with 312 with 2 states and 4 covariates. Should be ok */
1347:
1348: #define GNUPLOTPROGRAM "gnuplot"
1349: #define GNUPLOTVERSION 5.1
1350: double gnuplotversion=GNUPLOTVERSION;
1351: /*#define GNUPLOTPROGRAM "..\\gp37mgw\\wgnuplot"*/
1352: #define FILENAMELENGTH 256
1353:
1354: #define GLOCK_ERROR_NOPATH -1 /* empty path */
1355: #define GLOCK_ERROR_GETCWD -2 /* cannot get cwd */
1356:
1357: #define MAXPARM 216 /**< Maximum number of parameters for the optimization was 128 */
1358: #define NPARMAX 64 /**< (nlstate+ndeath-1)*nlstate*ncovmodel */
1359:
1360: #define NINTERVMAX 8
1361: #define NLSTATEMAX 8 /**< Maximum number of live states (for func) */
1362: #define NDEATHMAX 8 /**< Maximum number of dead states (for func) */
1363: #define NCOVMAX 30 /**< Maximum number of covariates used in the model, including generated covariates V1*V2 or V1*age */
1364: #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1365: /*#define decodtabm(h,k,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (k-1)) & 1) +1 : -1)*/
1366: #define decodtabm(h,k,cptcoveff) (((h-1) >> (k-1)) & 1) +1
1367: /*#define MAXN 20000 */ /* Should by replaced by nobs, real number of observations and unlimited */
1368: #define YEARM 12. /**< Number of months per year */
1369: /* #define AGESUP 130 */
1370: /* #define AGESUP 150 */
1371: #define AGESUP 200
1372: #define AGEINF 0
1373: #define AGEMARGE 25 /* Marge for agemin and agemax for(iage=agemin-AGEMARGE; iage <= agemax+3+AGEMARGE; iage++) */
1374: #define AGEBASE 40
1375: #define AGEOVERFLOW 1.e20
1376: #define AGEGOMP 10 /**< Minimal age for Gompertz adjustment */
1377: #ifdef _WIN32
1378: #define DIRSEPARATOR '\\'
1379: #define CHARSEPARATOR "\\"
1380: #define ODIRSEPARATOR '/'
1381: #else
1382: #define DIRSEPARATOR '/'
1383: #define CHARSEPARATOR "/"
1384: #define ODIRSEPARATOR '\\'
1385: #endif
1386:
1387: /* $Id: imachprax.c,v 1.5 2023/10/09 09:10:01 brouard Exp $ */
1388: /* $State: Exp $ */
1389: #include "version.h"
1390: char version[]=__IMACH_VERSION__;
1391: char copyright[]="April 2023,INED-EUROREVES-Institut de longevite-Japan Society for the Promotion of Science (Grant-in-Aid for Scientific Research 25293121), Intel Software 2015-2020, Nihon University 2021-202, INED 2000-2022";
1392: char fullversion[]="$Revision: 1.5 $ $Date: 2023/10/09 09:10:01 $";
1393: char strstart[80];
1394: char optionfilext[10], optionfilefiname[FILENAMELENGTH];
1395: int erreur=0, nberr=0, nbwarn=0; /* Error number, number of errors number of warnings */
1396: int debugILK=0; /* debugILK is set by a #d in a comment line */
1397: int nagesqr=0, nforce=0; /* nagesqr=1 if model is including age*age, number of forces */
1398: /* Number of covariates model (1)=V2+V1+ V3*age+V2*V4 */
1399: /* Model(2) V1 + V2 + V3 + V8 + V7*V8 + V5*V6 + V8*age + V3*age + age*age */
1400: int cptcovn=0; /**< cptcovn decodemodel: number of covariates k of the models excluding age*products =6 and age*age but including products */
1401: int cptcovt=0; /**< cptcovt: total number of covariates of the model (2) nbocc(+)+1 = 8 excepting constant and age and age*age */
1402: int cptcovs=0; /**< cptcovs number of SIMPLE covariates in the model V2+V1 =2 (dummy or quantit or time varying) */
1403: int cptcovsnq=0; /**< cptcovsnq number of SIMPLE covariates in the model but non quantitative V2+V1 =2 */
1404: int cptcovage=0; /**< Number of covariates with age: V3*age only =1 */
1405: int cptcovprodage=0; /**< Number of fixed covariates with age: V3*age or V2*V3*age 1 */
1406: int cptcovprodvage=0; /**< Number of varying covariates with age: V7*age or V7*V6*age */
1407: int cptcovdageprod=0; /**< Number of doubleproducts with age, since 0.99r44 only: age*Vn*Vm for gnuplot printing*/
1408: int cptcovprodnoage=0; /**< Number of covariate products without age */
1409: int cptcoveff=0; /* Total number of single dummy covariates (fixed or time varying) to vary for printing results (2**cptcoveff combinations of dummies)(computed in tricode as cptcov) */
1410: int ncovf=0; /* Total number of effective fixed covariates (dummy or quantitative) in the model */
1411: int ncovv=0; /* Total number of effective (wave) varying covariates (dummy or quantitative) in the model */
1412: int ncovvt=0; /* Total number of effective (wave) varying covariates (dummy or quantitative or products [without age]) in the model */
1413: int ncovvta=0; /* +age*V6 + age*V7+ age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 Total number of expandend products [with age]) in the model */
1414: int ncovta=0; /*age*V3*V2 +age*V2+agev3+ageV4 +age*V6 + age*V7+ age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 Total number of expandend products [with age]) in the model */
1415: int ncova=0; /* Total number of effective (wave and stepm) varying with age covariates (single or product, dummy or quantitative) in the model */
1416: int ncovva=0; /* +age*V6 + age*V7+ge*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 Total number of effective (wave and stepm) varying with age covariates (single or product, dummy or quantitative) in the model */
1417: int nsd=0; /**< Total number of single dummy variables (output) */
1418: int nsq=0; /**< Total number of single quantitative variables (output) */
1419: int ncoveff=0; /* Total number of effective fixed dummy covariates in the model */
1420: int nqfveff=0; /**< nqfveff Number of Quantitative Fixed Variables Effective */
1421: int ntveff=0; /**< ntveff number of effective time varying variables */
1422: int nqtveff=0; /**< ntqveff number of effective time varying quantitative variables */
1423: int cptcov=0; /* Working variable */
1424: int firstobs=1, lastobs=10; /* nobs = lastobs-firstobs+1 declared globally ;*/
1425: int nobs=10; /* Number of observations in the data lastobs-firstobs */
1426: int ncovcombmax=NCOVMAX; /* Maximum calculated number of covariate combination = pow(2, cptcoveff) */
1427: int npar=NPARMAX; /* Number of parameters (nlstate+ndeath-1)*nlstate*ncovmodel; */
1428: int nlstate=2; /* Number of live states */
1429: int ndeath=1; /* Number of dead states */
1430: int ncovmodel=0, ncovcol=0; /* Total number of covariables including constant a12*1 +b12*x ncovmodel=2 */
1431: int nqv=0, ntv=0, nqtv=0; /* Total number of quantitative variables, time variable (dummy), quantitative and time variable*/
1432: int ncovcolt=0; /* ncovcolt=ncovcol+nqv+ntv+nqtv; total of covariates in the data, not in the model equation*/
1433: int popbased=0;
1434:
1435: int *wav; /* Number of waves for this individuual 0 is possible */
1436: int maxwav=0; /* Maxim number of waves */
1437: int jmin=0, jmax=0; /* min, max spacing between 2 waves */
1438: int ijmin=0, ijmax=0; /* Individuals having jmin and jmax */
1439: int gipmx=0, gsw=0; /* Global variables on the number of contributions
1440: to the likelihood and the sum of weights (done by funcone)*/
1441: int mle=1, weightopt=0;
1442: int **mw; /* mw[mi][i] is number of the mi wave for this individual */
1443: int **dh; /* dh[mi][i] is number of steps between mi,mi+1 for this individual */
1444: int **bh; /* bh[mi][i] is the bias (+ or -) for this individual if the delay between
1445: * wave mi and wave mi+1 is not an exact multiple of stepm. */
1446: int countcallfunc=0; /* Count the number of calls to func */
1447: int selected(int kvar); /* Is covariate kvar selected for printing results */
1448:
1449: double jmean=1; /* Mean space between 2 waves */
1450: double **matprod2(); /* test */
1451: double **oldm, **newm, **savm; /* Working pointers to matrices */
1452: double **oldms, **newms, **savms; /* Fixed working pointers to matrices */
1453: double **ddnewms, **ddoldms, **ddsavms; /* for freeing later */
1454:
1455: /*FILE *fic ; */ /* Used in readdata only */
1456: FILE *ficpar, *ficparo,*ficres, *ficresp, *ficresphtm, *ficresphtmfr, *ficrespl, *ficresplb,*ficrespij, *ficrespijb, *ficrest,*ficresf, *ficresfb,*ficrespop;
1457: FILE *ficlog, *ficrespow;
1458: int globpr=0; /* Global variable for printing or not */
1459: double fretone; /* Only one call to likelihood */
1460: long ipmx=0; /* Number of contributions */
1461: double sw; /* Sum of weights */
1462: char filerespow[FILENAMELENGTH];
1463: char fileresilk[FILENAMELENGTH]; /* File of individual contributions to the likelihood */
1464: FILE *ficresilk;
1465: FILE *ficgp,*ficresprob,*ficpop, *ficresprobcov, *ficresprobcor;
1466: FILE *ficresprobmorprev;
1467: FILE *fichtm, *fichtmcov; /* Html File */
1468: FILE *ficreseij;
1469: char filerese[FILENAMELENGTH];
1470: FILE *ficresstdeij;
1471: char fileresstde[FILENAMELENGTH];
1472: FILE *ficrescveij;
1473: char filerescve[FILENAMELENGTH];
1474: FILE *ficresvij;
1475: char fileresv[FILENAMELENGTH];
1476:
1477: char title[MAXLINE];
1478: char model[MAXLINE]; /**< The model line */
1479: char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH], filerespl[FILENAMELENGTH], fileresplb[FILENAMELENGTH];
1480: char plotcmd[FILENAMELENGTH], pplotcmd[FILENAMELENGTH];
1481: char tmpout[FILENAMELENGTH], tmpout2[FILENAMELENGTH];
1482: char command[FILENAMELENGTH];
1483: int outcmd=0;
1484:
1485: char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filerespijb[FILENAMELENGTH], filereso[FILENAMELENGTH], rfileres[FILENAMELENGTH];
1486: char fileresu[FILENAMELENGTH]; /* fileres without r in front */
1487: char filelog[FILENAMELENGTH]; /* Log file */
1488: char filerest[FILENAMELENGTH];
1489: char fileregp[FILENAMELENGTH];
1490: char popfile[FILENAMELENGTH];
1491:
1492: char optionfilegnuplot[FILENAMELENGTH], optionfilehtm[FILENAMELENGTH], optionfilehtmcov[FILENAMELENGTH] ;
1493:
1494: /* struct timeval start_time, end_time, curr_time, last_time, forecast_time; */
1495: /* struct timezone tzp; */
1496: /* extern int gettimeofday(); */
1497: struct tm tml, *gmtime(), *localtime();
1498:
1499: extern time_t time();
1500:
1501: struct tm start_time, end_time, curr_time, last_time, forecast_time;
1502: time_t rstart_time, rend_time, rcurr_time, rlast_time, rforecast_time; /* raw time */
1503: time_t rlast_btime; /* raw time */
1504: struct tm tm;
1505:
1506: char strcurr[80], strfor[80];
1507:
1508: char *endptr;
1509: long lval;
1510: double dval;
1511:
1512: #define NR_END 1
1513: #define FREE_ARG char*
1514: #define FTOL 1.0e-10
1515:
1516: #define NRANSI
1517: #define ITMAX 200
1518: #define ITPOWMAX 20 /* This is now multiplied by the number of parameters */
1519:
1520: #define TOL 2.0e-4
1521:
1522: #define CGOLD 0.3819660
1523: #define ZEPS 1.0e-10
1524: #define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d);
1525:
1526: #define GOLD 1.618034
1527: #define GLIMIT 100.0
1528: #define TINY 1.0e-20
1529:
1530: static double maxarg1,maxarg2;
1531: #define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)>(maxarg2)? (maxarg1):(maxarg2))
1532: #define FMIN(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)<(maxarg2)? (maxarg1):(maxarg2))
1533:
1534: #define SIGN(a,b) ((b)>0.0 ? fabs(a) : -fabs(a))
1535: #define rint(a) floor(a+0.5)
1536: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/myutils_8h-source.html */
1537: #define mytinydouble 1.0e-16
1538: /* #define DEQUAL(a,b) (fabs((a)-(b))<mytinydouble) */
1539: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/mynrutils_8h-source.html */
1540: /* static double dsqrarg; */
1541: /* #define DSQR(a) (DEQUAL((dsqrarg=(a)),0.0) ? 0.0 : dsqrarg*dsqrarg) */
1542: static double sqrarg;
1543: #define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 :sqrarg*sqrarg)
1544: #define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;}
1545: int agegomp= AGEGOMP;
1546:
1547: int imx;
1548: int stepm=1;
1549: /* Stepm, step in month: minimum step interpolation*/
1550:
1551: int estepm;
1552: /* Estepm, step in month to interpolate survival function in order to approximate Life Expectancy*/
1553:
1554: int m,nb;
1555: long *num;
1556: int firstpass=0, lastpass=4,*cod, *cens;
1557: int *ncodemax; /* ncodemax[j]= Number of modalities of the j th
1558: covariate for which somebody answered excluding
1559: undefined. Usually 2: 0 and 1. */
1560: int *ncodemaxwundef; /* ncodemax[j]= Number of modalities of the j th
1561: covariate for which somebody answered including
1562: undefined. Usually 3: -1, 0 and 1. */
1563: double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint;
1564: double **pmmij, ***probs; /* Global pointer */
1565: double ***mobaverage, ***mobaverages; /* New global variable */
1566: double **precov; /* New global variable to store for each resultline, values of model covariates given by the resultlines (in order to speed up) */
1567: double *ageexmed,*agecens;
1568: double dateintmean=0;
1569: double anprojd, mprojd, jprojd; /* For eventual projections */
1570: double anprojf, mprojf, jprojf;
1571:
1572: double anbackd, mbackd, jbackd; /* For eventual backprojections */
1573: double anbackf, mbackf, jbackf;
1574: double jintmean,mintmean,aintmean;
1575: double *weight;
1576: int **s; /* Status */
1577: double *agedc;
1578: double **covar; /**< covar[j,i], value of jth covariate for individual i,
1579: * covar=matrix(0,NCOVMAX,1,n);
1580: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*age; */
1581: double **coqvar; /* Fixed quantitative covariate nqv */
1582: double ***cotvar; /* Time varying covariate start at ncovcol + nqv + (1 to ntv) */
1583: double ***cotqvar; /* Time varying quantitative covariate itqv */
1584: double idx;
1585: int **nbcode, *Tvar; /**< model=V2 => Tvar[1]= 2 */
1586: /* Some documentation */
1587: /* Design original data
1588: * V1 V2 V3 V4 V5 V6 V7 V8 Weight ddb ddth d1st s1 V9 V10 V11 V12 s2 V9 V10 V11 V12
1589: * < ncovcol=6 > nqv=2 (V7 V8) dv dv dv qtv dv dv dvv qtv
1590: * ntv=3 nqtv=1
1591: * cptcovn number of covariates (not including constant and age or age*age) = number of plus sign + 1 = 10+1=11
1592: * For time varying covariate, quanti or dummies
1593: * cotqvar[wav][iv(1 to nqtv)][i]= [1][12][i]=(V12) quanti
1594: * cotvar[wav][ncovcol+nqv+ iv(1 to nqtv)][i]= [(1 to nqtv)][i]=(V12) quanti
1595: * cotvar[wav][iv(1 to ntv)][i]= [1][1][i]=(V9) dummies at wav 1
1596: * cotvar[wav][iv(1 to ntv)][i]= [1][2][i]=(V10) dummies at wav 1
1597: * covar[Vk,i], value of the Vkth fixed covariate dummy or quanti for individual i:
1598: * covar[1][i]= (V1), covar[4][i]=(V4), covar[8][i]=(V8)
1599: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8 + V9 + V9*age + V10
1600: * k= 1 2 3 4 5 6 7 8 9 10 11
1601: */
1602: /* According to the model, more columns can be added to covar by the product of covariates */
1603: /* ncovcol=1(Males=0 Females=1) nqv=1(raedyrs) ntv=2(withoutiadl=0 withiadl=1, witoutadl=0 withoutadl=1) nqtv=1(bmi) nlstate=3 ndeath=1
1604: # States 1=Coresidence, 2 Living alone, 3 Institution
1605: # V1=sex, V2=raedyrs Quant Fixed, State=livarnb4..livarnb11, V3=iadl4..iald11, V4=adlw4..adlw11, V5=r4bmi..r11bmi
1606: */
1607: /* V5+V4+ V3+V4*V3 +V5*age+V2 +V1*V2+V1*age+V1+V4*V3*age */
1608: /* kmodel 1 2 3 4 5 6 7 8 9 10 */
1609: /*Typevar[k]= 0 0 0 2 1 0 2 1 0 3 *//*0 for simple covariate (dummy, quantitative,*/
1610: /* fixed or varying), 1 for age product, 2 for*/
1611: /* product without age, 3 for age and double product */
1612: /*Dummy[k]= 1 0 0 1 3 1 1 2 0 3 *//*Dummy[k] 0=dummy (0 1), 1 quantitative */
1613: /*(single or product without age), 2 dummy*/
1614: /* with age product, 3 quant with age product*/
1615: /*Tvar[k]= 5 4 3 6 5 2 7 1 1 6 */
1616: /* nsd 1 2 3 */ /* Counting single dummies covar fixed or tv */
1617: /*TnsdVar[Tvar] 1 2 3 */
1618: /*Tvaraff[nsd] 4 3 1 */ /* ID of single dummy cova fixed or timevary*/
1619: /*TvarsD[nsd] 4 3 1 */ /* ID of single dummy cova fixed or timevary*/
1620: /*TvarsDind[nsd] 2 3 9 */ /* position K of single dummy cova */
1621: /* nsq 1 2 */ /* Counting single quantit tv */
1622: /* TvarsQ[k] 5 2 */ /* Number of single quantitative cova */
1623: /* TvarsQind 1 6 */ /* position K of single quantitative cova */
1624: /* Tprod[i]=k 1 2 */ /* Position in model of the ith prod without age */
1625: /* cptcovage 1 2 3 */ /* Counting cov*age in the model equation */
1626: /* Tage[cptcovage]=k 5 8 10 */ /* Position in the model of ith cov*age */
1627: /* model="V2+V3+V4+V6+V7+V6*V2+V7*V2+V6*V3+V7*V3+V6*V4+V7*V4+age*V2+age*V3+age*V4+age*V6+age*V7+age*V6*V2+age*V6*V3+age*V7*V3+age*V6*V4+age*V7*V4\r"*/
1628: /* p Tvard[1][1]@21 = {6, 2, 7, 2, 6, 3, 7, 3, 6, 4, 7, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0}*/
1629: /* p Tvard[2][1]@21 = {7, 2, 6, 3, 7, 3, 6, 4, 7, 4, 0 <repeats 11 times>} */
1630: /* p Tvardk[1][1]@24 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 2, 7, 2, 6, 3, 7, 3, 6, 4, 7, 4, 0, 0}*/
1631: /* p Tvardk[1][1]@22 = {0, 0, 0, 0, 0, 0, 0, 0, 6, 2, 7, 2, 6, 3, 7, 3, 6, 4, 7, 4, 0, 0} */
1632: /* Tvard[1][1]@4={4,3,1,2} V4*V3 V1*V2 */ /* Position in model of the ith prod without age */
1633: /* Tvardk[4][1]=4;Tvardk[4][2]=3;Tvardk[7][1]=1;Tvardk[7][2]=2 */ /* Variables of a prod at position in the model equation*/
1634: /* TvarF TvarF[1]=Tvar[6]=2, TvarF[2]=Tvar[7]=7, TvarF[3]=Tvar[9]=1 ID of fixed covariates or product V2, V1*V2, V1 */
1635: /* TvarFind; TvarFind[1]=6, TvarFind[2]=7, TvarFind[3]=9 *//* Inverse V2(6) is first fixed (single or prod) */
1636: /* Type */
1637: /* V 1 2 3 4 5 */
1638: /* F F V V V */
1639: /* D Q D D Q */
1640: /* */
1641: int *TvarsD;
1642: int *TnsdVar;
1643: int *TvarsDind;
1644: int *TvarsQ;
1645: int *TvarsQind;
1646:
1647: #define MAXRESULTLINESPONE 10+1
1648: int nresult=0;
1649: int parameterline=0; /* # of the parameter (type) line */
1650: int TKresult[MAXRESULTLINESPONE]; /* TKresult[nres]=k for each resultline nres give the corresponding combination of dummies */
1651: int resultmodel[MAXRESULTLINESPONE][NCOVMAX];/* resultmodel[k1]=k3: k1th position in the model corresponds to the k3 position in the resultline */
1652: int modelresult[MAXRESULTLINESPONE][NCOVMAX];/* modelresult[k3]=k1: k1th position in the model corresponds to the k3 position in the resultline */
1653: int Tresult[MAXRESULTLINESPONE][NCOVMAX];/* Tresult[nres][result_position]= value of the dummy variable at the result_position in the nres resultline */
1654: int Tinvresult[MAXRESULTLINESPONE][NCOVMAX];/* Tinvresult[nres][Name of a dummy variable]= value of the variable in the result line */
1655: double TinvDoQresult[MAXRESULTLINESPONE][NCOVMAX];/* TinvDoQresult[nres][Name of a Dummy or Q variable]= value of the variable in the result line */
1656: int Tvresult[MAXRESULTLINESPONE][NCOVMAX]; /* Tvresult[nres][result_position]= name of the dummy variable at the result_position in the nres resultline */
1657: double Tqresult[MAXRESULTLINESPONE][NCOVMAX]; /* Tqresult[nres][result_position]= value of the variable at the result_position in the nres resultline */
1658: double Tqinvresult[MAXRESULTLINESPONE][NCOVMAX]; /* For quantitative variable , value (output) */
1659: int Tvqresult[MAXRESULTLINESPONE][NCOVMAX]; /* Tvqresult[nres][result_position]= id of the variable at the result_position in the nres resultline */
1660:
1661: /* ncovcol=1(Males=0 Females=1) nqv=1(raedyrs) ntv=2(withoutiadl=0 withiadl=1, witoutadl=0 withoutadl=1) nqtv=1(bmi) nlstate=3 ndeath=1
1662: # States 1=Coresidence, 2 Living alone, 3 Institution
1663: # V1=sex, V2=raedyrs Quant Fixed, State=livarnb4..livarnb11, V3=iadl4..iald11, V4=adlw4..adlw11, V5=r4bmi..r11bmi
1664: */
1665: /* 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 *\/ */
1666: 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 */
1667: 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 */
1668: 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 */
1669: int *TvarVind; /**< TvarVind[1]=1, TvarVind[2]=2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1670: 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 */
1671: int *TvarAind; /**< TvarindA[1]=5, TvarAind[2]=8 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1672: int *TvarFD; /**< TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1673: int *TvarFDind; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1674: int *TvarFQ; /* TvarFQ[1]=V2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1675: int *TvarFQind; /* TvarFQind[1]=6 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1676: int *TvarVD; /* TvarVD[1]=V5 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1677: int *TvarVDind; /* TvarVDind[1]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1678: 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 */
1679: 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 */
1680: int *TvarVV; /* We count ncovvt time varying covariates (single or products without age) and put their name into TvarVV */
1681: int *TvarVVind; /* We count ncovvt time varying covariates (single or products without age) and put their name into TvarVV */
1682: int *TvarVVA; /* We count ncovvt time varying covariates (single or products with age) and put their name into TvarVVA */
1683: int *TvarVVAind; /* We count ncovvt time varying covariates (single or products without age) and put their name into TvarVV */
1684: int *TvarAVVA; /* We count ALL ncovta time varying covariates (single or products with age) and put their name into TvarVVA */
1685: int *TvarAVVAind; /* We count ALL ncovta time varying covariates (single or products without age) and put their name into TvarVV */
1686: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
1687: /* model V1+V3+age*V1+age*V3+V1*V3 + V1*V3*age */
1688: /* Tvar={1, 3, 1, 3, 6, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
1689: /* TvarVV={3,1,3,1,3}, for V3 and then the product V1*V3 is decomposed into V1 and V3 */
1690: /* TvarVVind={2,5,5,6,6}, for V3 and then the product V1*V3 is decomposed into V1 and V3 and V1*V3*age into 6,6 */
1691: int *Tvarsel; /**< Selected covariates for output */
1692: double *Tvalsel; /**< Selected modality value of covariate for output */
1693: int *Typevar; /**< 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product, 3 age*Vn*Vm */
1694: int *Fixed; /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
1695: 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 */
1696: int *DummyV; /** Dummy[v] 0=dummy (0 1), 1 quantitative */
1697: int *FixedV; /** FixedV[v] 0 fixed, 1 varying */
1698: int *Tage;
1699: int anyvaryingduminmodel=0; /**< Any varying dummy in Model=1 yes, 0 no, to avoid a loop on waves in freq */
1700: 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*/
1701: 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*/
1702: 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 */
1703: int *Ndum; /** Freq of modality (tricode */
1704: /* int **codtab;*/ /**< codtab=imatrix(1,100,1,10); */
1705: int **Tvard;
1706: int **Tvardk;
1707: int *Tprod;/**< Gives the k position of the k1 product */
1708: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3 */
1709: int *Tposprod; /**< Gives the k1 product from the k position */
1710: /* if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2) */
1711: /* Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5(V3*V2)]=2 (2nd product without age) */
1712: int cptcovprod, *Tvaraff, *invalidvarcomb;
1713: double *lsurv, *lpop, *tpop;
1714:
1715: #define FD 1; /* Fixed dummy covariate */
1716: #define FQ 2; /* Fixed quantitative covariate */
1717: #define FP 3; /* Fixed product covariate */
1718: #define FPDD 7; /* Fixed product dummy*dummy covariate */
1719: #define FPDQ 8; /* Fixed product dummy*quantitative covariate */
1720: #define FPQQ 9; /* Fixed product quantitative*quantitative covariate */
1721: #define VD 10; /* Varying dummy covariate */
1722: #define VQ 11; /* Varying quantitative covariate */
1723: #define VP 12; /* Varying product covariate */
1724: #define VPDD 13; /* Varying product dummy*dummy covariate */
1725: #define VPDQ 14; /* Varying product dummy*quantitative covariate */
1726: #define VPQQ 15; /* Varying product quantitative*quantitative covariate */
1727: #define APFD 16; /* Age product * fixed dummy covariate */
1728: #define APFQ 17; /* Age product * fixed quantitative covariate */
1729: #define APVD 18; /* Age product * varying dummy covariate */
1730: #define APVQ 19; /* Age product * varying quantitative covariate */
1731:
1732: #define FTYPE 1; /* Fixed covariate */
1733: #define VTYPE 2; /* Varying covariate (loop in wave) */
1734: #define ATYPE 2; /* Age product covariate (loop in dh within wave)*/
1735:
1736: struct kmodel{
1737: int maintype; /* main type */
1738: int subtype; /* subtype */
1739: };
1740: struct kmodel modell[NCOVMAX];
1741:
1742: double ftol=FTOL; /**< Tolerance for computing Max Likelihood */
1743: double ftolhess; /**< Tolerance for computing hessian */
1744:
1745: /**************** split *************************/
1746: static int split( char *path, char *dirc, char *name, char *ext, char *finame )
1747: {
1748: /* From a file name with (full) path (either Unix or Windows) we extract the directory (dirc)
1749: the name of the file (name), its extension only (ext) and its first part of the name (finame)
1750: */
1751: char *ss; /* pointer */
1752: int l1=0, l2=0; /* length counters */
1753:
1754: l1 = strlen(path ); /* length of path */
1755: if ( l1 == 0 ) return( GLOCK_ERROR_NOPATH );
1756: ss= strrchr( path, DIRSEPARATOR ); /* find last / */
1757: if ( ss == NULL ) { /* no directory, so determine current directory */
1758: strcpy( name, path ); /* we got the fullname name because no directory */
1759: /*if(strrchr(path, ODIRSEPARATOR )==NULL)
1760: printf("Warning you should use %s as a separator\n",DIRSEPARATOR);*/
1761: /* get current working directory */
1762: /* extern char* getcwd ( char *buf , int len);*/
1763: #ifdef WIN32
1764: if (_getcwd( dirc, FILENAME_MAX ) == NULL ) {
1765: #else
1766: if (getcwd(dirc, FILENAME_MAX) == NULL) {
1767: #endif
1768: return( GLOCK_ERROR_GETCWD );
1769: }
1770: /* got dirc from getcwd*/
1771: printf(" DIRC = %s \n",dirc);
1772: } else { /* strip directory from path */
1773: ss++; /* after this, the filename */
1774: l2 = strlen( ss ); /* length of filename */
1775: if ( l2 == 0 ) return( GLOCK_ERROR_NOPATH );
1776: strcpy( name, ss ); /* save file name */
1777: strncpy( dirc, path, l1 - l2 ); /* now the directory */
1778: dirc[l1-l2] = '\0'; /* add zero */
1779: printf(" DIRC2 = %s \n",dirc);
1780: }
1781: /* We add a separator at the end of dirc if not exists */
1782: l1 = strlen( dirc ); /* length of directory */
1783: if( dirc[l1-1] != DIRSEPARATOR ){
1784: dirc[l1] = DIRSEPARATOR;
1785: dirc[l1+1] = 0;
1786: printf(" DIRC3 = %s \n",dirc);
1787: }
1788: ss = strrchr( name, '.' ); /* find last / */
1789: if (ss >0){
1790: ss++;
1791: strcpy(ext,ss); /* save extension */
1792: l1= strlen( name);
1793: l2= strlen(ss)+1;
1794: strncpy( finame, name, l1-l2);
1795: finame[l1-l2]= 0;
1796: }
1797:
1798: return( 0 ); /* we're done */
1799: }
1800:
1801:
1802: /******************************************/
1803:
1804: void replace_back_to_slash(char *s, char*t)
1805: {
1806: int i;
1807: int lg=0;
1808: i=0;
1809: lg=strlen(t);
1810: for(i=0; i<= lg; i++) {
1811: (s[i] = t[i]);
1812: if (t[i]== '\\') s[i]='/';
1813: }
1814: }
1815:
1816: char *trimbb(char *out, char *in)
1817: { /* Trim multiple blanks in line but keeps first blanks if line starts with blanks */
1818: char *s;
1819: s=out;
1820: while (*in != '\0'){
1821: while( *in == ' ' && *(in+1) == ' '){ /* && *(in+1) != '\0'){*/
1822: in++;
1823: }
1824: *out++ = *in++;
1825: }
1826: *out='\0';
1827: return s;
1828: }
1829:
1830: char *trimbtab(char *out, char *in)
1831: { /* Trim blanks or tabs in line but keeps first blanks if line starts with blanks */
1832: char *s;
1833: s=out;
1834: while (*in != '\0'){
1835: while( (*in == ' ' || *in == '\t')){ /* && *(in+1) != '\0'){*/
1836: in++;
1837: }
1838: *out++ = *in++;
1839: }
1840: *out='\0';
1841: return s;
1842: }
1843:
1844: /* char *substrchaine(char *out, char *in, char *chain) */
1845: /* { */
1846: /* /\* Substract chain 'chain' from 'in', return and output 'out' *\/ */
1847: /* char *s, *t; */
1848: /* t=in;s=out; */
1849: /* while ((*in != *chain) && (*in != '\0')){ */
1850: /* *out++ = *in++; */
1851: /* } */
1852:
1853: /* /\* *in matches *chain *\/ */
1854: /* while ((*in++ == *chain++) && (*in != '\0')){ */
1855: /* printf("*in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1856: /* } */
1857: /* in--; chain--; */
1858: /* while ( (*in != '\0')){ */
1859: /* printf("Bef *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1860: /* *out++ = *in++; */
1861: /* printf("Aft *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1862: /* } */
1863: /* *out='\0'; */
1864: /* out=s; */
1865: /* return out; */
1866: /* } */
1867: char *substrchaine(char *out, char *in, char *chain)
1868: {
1869: /* Substract chain 'chain' from 'in', return and output 'out' */
1870: /* in="V1+V1*age+age*age+V2", chain="+age*age" out="V1+V1*age+V2" */
1871:
1872: char *strloc;
1873:
1874: strcpy (out, in); /* out="V1+V1*age+age*age+V2" */
1875: strloc = strstr(out, chain); /* strloc points to out at "+age*age+V2" */
1876: printf("Bef strloc=%s chain=%s out=%s \n", strloc, chain, out); /* strloc=+age*age+V2 chain="+age*age", out="V1+V1*age+age*age+V2" */
1877: if(strloc != NULL){
1878: /* will affect out */ /* strloc+strlen(chain)=|+V2 = "V1+V1*age+age*age|+V2" */ /* Will also work in Unicodek */
1879: memmove(strloc,strloc+strlen(chain), strlen(strloc+strlen(chain))+1); /* move number of bytes corresponding to the length of "+V2" which is 3, plus one is 4 (including the null)*/
1880: /* equivalent to strcpy (strloc, strloc +strlen(chain)) if no overlap; Copies from "+V2" to V1+V1*age+ */
1881: }
1882: printf("Aft strloc=%s chain=%s in=%s out=%s \n", strloc, chain, in, out); /* strloc=+V2 chain="+age*age", in="V1+V1*age+age*age+V2", out="V1+V1*age+V2" */
1883: return out;
1884: }
1885:
1886:
1887: char *cutl(char *blocc, char *alocc, char *in, char occ)
1888: {
1889: /* cuts string in into blocc and alocc where blocc ends before FIRST occurence of char 'occ'
1890: and alocc starts after first occurence of char 'occ' : ex cutl(blocc,alocc,"abcdef2ghi2j",'2')
1891: gives alocc="abcdef" and blocc="ghi2j".
1892: If occ is not found blocc is null and alocc is equal to in. Returns blocc
1893: */
1894: char *s, *t;
1895: t=in;s=in;
1896: while ((*in != occ) && (*in != '\0')){
1897: *alocc++ = *in++;
1898: }
1899: if( *in == occ){
1900: *(alocc)='\0';
1901: s=++in;
1902: }
1903:
1904: if (s == t) {/* occ not found */
1905: *(alocc-(in-s))='\0';
1906: in=s;
1907: }
1908: while ( *in != '\0'){
1909: *blocc++ = *in++;
1910: }
1911:
1912: *blocc='\0';
1913: return t;
1914: }
1915: char *cutv(char *blocc, char *alocc, char *in, char occ)
1916: {
1917: /* cuts string in into blocc and alocc where blocc ends before LAST occurence of char 'occ'
1918: and alocc starts after last occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
1919: gives blocc="abcdef2ghi" and alocc="j".
1920: If occ is not found blocc is null and alocc is equal to in. Returns alocc
1921: */
1922: char *s, *t;
1923: t=in;s=in;
1924: while (*in != '\0'){
1925: while( *in == occ){
1926: *blocc++ = *in++;
1927: s=in;
1928: }
1929: *blocc++ = *in++;
1930: }
1931: if (s == t) /* occ not found */
1932: *(blocc-(in-s))='\0';
1933: else
1934: *(blocc-(in-s)-1)='\0';
1935: in=s;
1936: while ( *in != '\0'){
1937: *alocc++ = *in++;
1938: }
1939:
1940: *alocc='\0';
1941: return s;
1942: }
1943:
1944: int nbocc(char *s, char occ)
1945: {
1946: int i,j=0;
1947: int lg=20;
1948: i=0;
1949: lg=strlen(s);
1950: for(i=0; i<= lg; i++) {
1951: if (s[i] == occ ) j++;
1952: }
1953: return j;
1954: }
1955:
1956: int nboccstr(char *textin, char *chain)
1957: {
1958: /* Counts the number of occurence of "chain" in string textin */
1959: /* in="+V7*V4+age*V2+age*V3+age*V4" chain="age" */
1960: char *strloc;
1961:
1962: int i,j=0;
1963:
1964: i=0;
1965:
1966: strloc=textin; /* strloc points to "^+V7*V4+age+..." in textin */
1967: for(;;) {
1968: strloc= strstr(strloc,chain); /* strloc points to first character of chain in textin if found. Example strloc points^ to "+V7*V4+^age" in textin */
1969: if(strloc != NULL){
1970: strloc = strloc+strlen(chain); /* strloc points to "+V7*V4+age^" in textin */
1971: j++;
1972: }else
1973: break;
1974: }
1975: return j;
1976:
1977: }
1978: /* void cutv(char *u,char *v, char*t, char occ) */
1979: /* { */
1980: /* /\* cuts string t into u and v where u ends before last occurence of char 'occ' */
1981: /* and v starts after last occurence of char 'occ' : ex cutv(u,v,"abcdef2ghi2j",'2') */
1982: /* gives u="abcdef2ghi" and v="j" *\/ */
1983: /* int i,lg,j,p=0; */
1984: /* i=0; */
1985: /* lg=strlen(t); */
1986: /* for(j=0; j<=lg-1; j++) { */
1987: /* if((t[j]!= occ) && (t[j+1]== occ)) p=j+1; */
1988: /* } */
1989:
1990: /* for(j=0; j<p; j++) { */
1991: /* (u[j] = t[j]); */
1992: /* } */
1993: /* u[p]='\0'; */
1994:
1995: /* for(j=0; j<= lg; j++) { */
1996: /* if (j>=(p+1))(v[j-p-1] = t[j]); */
1997: /* } */
1998: /* } */
1999:
2000: #ifdef _WIN32
2001: char * strsep(char **pp, const char *delim)
2002: {
2003: char *p, *q;
2004:
2005: if ((p = *pp) == NULL)
2006: return 0;
2007: if ((q = strpbrk (p, delim)) != NULL)
2008: {
2009: *pp = q + 1;
2010: *q = '\0';
2011: }
2012: else
2013: *pp = 0;
2014: return p;
2015: }
2016: #endif
2017:
2018: /********************** nrerror ********************/
2019:
2020: void nrerror(char error_text[])
2021: {
2022: fprintf(stderr,"ERREUR ...\n");
2023: fprintf(stderr,"%s\n",error_text);
2024: exit(EXIT_FAILURE);
2025: }
2026: /*********************** vector *******************/
2027: double *vector(int nl, int nh)
2028: {
2029: double *v;
2030: v=(double *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(double)));
2031: if (!v) nrerror("allocation failure in vector");
2032: return v-nl+NR_END;
2033: }
2034:
2035: /************************ free vector ******************/
2036: void free_vector(double*v, int nl, int nh)
2037: {
2038: free((FREE_ARG)(v+nl-NR_END));
2039: }
2040:
2041: /************************ivector *******************************/
2042: int *ivector(long nl,long nh)
2043: {
2044: int *v;
2045: v=(int *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(int)));
2046: if (!v) nrerror("allocation failure in ivector");
2047: return v-nl+NR_END;
2048: }
2049:
2050: /******************free ivector **************************/
2051: void free_ivector(int *v, long nl, long nh)
2052: {
2053: free((FREE_ARG)(v+nl-NR_END));
2054: }
2055:
2056: /************************lvector *******************************/
2057: long *lvector(long nl,long nh)
2058: {
2059: long *v;
2060: v=(long *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(long)));
2061: if (!v) nrerror("allocation failure in ivector");
2062: return v-nl+NR_END;
2063: }
2064:
2065: /******************free lvector **************************/
2066: void free_lvector(long *v, long nl, long nh)
2067: {
2068: free((FREE_ARG)(v+nl-NR_END));
2069: }
2070:
2071: /******************* imatrix *******************************/
2072: int **imatrix(long nrl, long nrh, long ncl, long nch)
2073: /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
2074: {
2075: long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
2076: int **m;
2077:
2078: /* allocate pointers to rows */
2079: m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
2080: if (!m) nrerror("allocation failure 1 in matrix()");
2081: m += NR_END;
2082: m -= nrl;
2083:
2084:
2085: /* allocate rows and set pointers to them */
2086: m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
2087: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
2088: m[nrl] += NR_END;
2089: m[nrl] -= ncl;
2090:
2091: for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
2092:
2093: /* return pointer to array of pointers to rows */
2094: return m;
2095: }
2096:
2097: /****************** free_imatrix *************************/
2098: void free_imatrix(m,nrl,nrh,ncl,nch)
2099: int **m;
2100: long nch,ncl,nrh,nrl;
2101: /* free an int matrix allocated by imatrix() */
2102: {
2103: free((FREE_ARG) (m[nrl]+ncl-NR_END));
2104: free((FREE_ARG) (m+nrl-NR_END));
2105: }
2106:
2107: /******************* matrix *******************************/
2108: double **matrix(long nrl, long nrh, long ncl, long nch)
2109: {
2110: long i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
2111: double **m;
2112:
2113: m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
2114: if (!m) nrerror("allocation failure 1 in matrix()");
2115: m += NR_END;
2116: m -= nrl;
2117:
2118: m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
2119: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
2120: m[nrl] += NR_END;
2121: m[nrl] -= ncl;
2122:
2123: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
2124: return m;
2125: /* print *(*(m+1)+70) or print m[1][70]; print m+1 or print &(m[1]) or &(m[1][0])
2126: m[i] = address of ith row of the table. &(m[i]) is its value which is another adress
2127: that of m[i][0]. In order to get the value p m[i][0] but it is unitialized.
2128: */
2129: }
2130:
2131: /*************************free matrix ************************/
2132: void free_matrix(double **m, long nrl, long nrh, long ncl, long nch)
2133: {
2134: free((FREE_ARG)(m[nrl]+ncl-NR_END));
2135: free((FREE_ARG)(m+nrl-NR_END));
2136: }
2137:
2138: /******************* ma3x *******************************/
2139: double ***ma3x(long nrl, long nrh, long ncl, long nch, long nll, long nlh)
2140: {
2141: long i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1, nlay=nlh-nll+1;
2142: double ***m;
2143:
2144: m=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
2145: if (!m) nrerror("allocation failure 1 in matrix()");
2146: m += NR_END;
2147: m -= nrl;
2148:
2149: m[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
2150: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
2151: m[nrl] += NR_END;
2152: m[nrl] -= ncl;
2153:
2154: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
2155:
2156: m[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*nlay+NR_END)*sizeof(double)));
2157: if (!m[nrl][ncl]) nrerror("allocation failure 3 in matrix()");
2158: m[nrl][ncl] += NR_END;
2159: m[nrl][ncl] -= nll;
2160: for (j=ncl+1; j<=nch; j++)
2161: m[nrl][j]=m[nrl][j-1]+nlay;
2162:
2163: for (i=nrl+1; i<=nrh; i++) {
2164: m[i][ncl]=m[i-1l][ncl]+ncol*nlay;
2165: for (j=ncl+1; j<=nch; j++)
2166: m[i][j]=m[i][j-1]+nlay;
2167: }
2168: return m;
2169: /* gdb: p *(m+1) <=> p m[1] and p (m+1) <=> p (m+1) <=> p &(m[1])
2170: &(m[i][j][k]) <=> *((*(m+i) + j)+k)
2171: */
2172: }
2173:
2174: /*************************free ma3x ************************/
2175: void free_ma3x(double ***m, long nrl, long nrh, long ncl, long nch,long nll, long nlh)
2176: {
2177: free((FREE_ARG)(m[nrl][ncl]+ nll-NR_END));
2178: free((FREE_ARG)(m[nrl]+ncl-NR_END));
2179: free((FREE_ARG)(m+nrl-NR_END));
2180: }
2181:
2182: /*************** function subdirf ***********/
2183: char *subdirf(char fileres[])
2184: {
2185: /* Caution optionfilefiname is hidden */
2186: strcpy(tmpout,optionfilefiname);
2187: strcat(tmpout,"/"); /* Add to the right */
2188: strcat(tmpout,fileres);
2189: return tmpout;
2190: }
2191:
2192: /*************** function subdirf2 ***********/
2193: char *subdirf2(char fileres[], char *preop)
2194: {
2195: /* Example subdirf2(optionfilefiname,"FB_") with optionfilefiname="texte", result="texte/FB_texte"
2196: Errors in subdirf, 2, 3 while printing tmpout is
2197: rewritten within the same printf. Workaround: many printfs */
2198: /* Caution optionfilefiname is hidden */
2199: strcpy(tmpout,optionfilefiname);
2200: strcat(tmpout,"/");
2201: strcat(tmpout,preop);
2202: strcat(tmpout,fileres);
2203: return tmpout;
2204: }
2205:
2206: /*************** function subdirf3 ***********/
2207: char *subdirf3(char fileres[], char *preop, char *preop2)
2208: {
2209:
2210: /* Caution optionfilefiname is hidden */
2211: strcpy(tmpout,optionfilefiname);
2212: strcat(tmpout,"/");
2213: strcat(tmpout,preop);
2214: strcat(tmpout,preop2);
2215: strcat(tmpout,fileres);
2216: return tmpout;
2217: }
2218:
2219: /*************** function subdirfext ***********/
2220: char *subdirfext(char fileres[], char *preop, char *postop)
2221: {
2222:
2223: strcpy(tmpout,preop);
2224: strcat(tmpout,fileres);
2225: strcat(tmpout,postop);
2226: return tmpout;
2227: }
2228:
2229: /*************** function subdirfext3 ***********/
2230: char *subdirfext3(char fileres[], char *preop, char *postop)
2231: {
2232:
2233: /* Caution optionfilefiname is hidden */
2234: strcpy(tmpout,optionfilefiname);
2235: strcat(tmpout,"/");
2236: strcat(tmpout,preop);
2237: strcat(tmpout,fileres);
2238: strcat(tmpout,postop);
2239: return tmpout;
2240: }
2241:
2242: char *asc_diff_time(long time_sec, char ascdiff[])
2243: {
2244: long sec_left, days, hours, minutes;
2245: days = (time_sec) / (60*60*24);
2246: sec_left = (time_sec) % (60*60*24);
2247: hours = (sec_left) / (60*60) ;
2248: sec_left = (sec_left) %(60*60);
2249: minutes = (sec_left) /60;
2250: sec_left = (sec_left) % (60);
2251: sprintf(ascdiff,"%ld day(s) %ld hour(s) %ld minute(s) %ld second(s)",days, hours, minutes, sec_left);
2252: return ascdiff;
2253: }
2254:
2255: /***************** f1dim *************************/
2256: extern int ncom;
2257: extern double *pcom,*xicom;
2258: extern double (*nrfunc)(double []);
2259:
2260: double f1dim(double x)
2261: {
2262: int j;
2263: double f;
2264: double *xt;
2265:
2266: xt=vector(1,ncom);
2267: for (j=1;j<=ncom;j++) xt[j]=pcom[j]+x*xicom[j];
2268: f=(*nrfunc)(xt);
2269: free_vector(xt,1,ncom);
2270: return f;
2271: }
2272:
2273: /*****************brent *************************/
2274: double brent(double ax, double bx, double cx, double (*f)(double), double tol, double *xmin)
2275: {
2276: /* Given a function f, and given a bracketing triplet of abscissas ax, bx, cx (such that bx is
2277: * between ax and cx, and f(bx) is less than both f(ax) and f(cx) ), this routine isolates
2278: * the minimum to a fractional precision of about tol using Brent’s method. The abscissa of
2279: * the minimum is returned as xmin, and the minimum function value is returned as brent , the
2280: * returned function value.
2281: */
2282: int iter;
2283: double a,b,d,etemp;
2284: double fu=0,fv,fw,fx;
2285: double ftemp=0.;
2286: double p,q,r,tol1,tol2,u,v,w,x,xm;
2287: double e=0.0;
2288:
2289: a=(ax < cx ? ax : cx);
2290: b=(ax > cx ? ax : cx);
2291: x=w=v=bx;
2292: fw=fv=fx=(*f)(x);
2293: for (iter=1;iter<=ITMAX;iter++) {
2294: xm=0.5*(a+b);
2295: tol2=2.0*(tol1=tol*fabs(x)+ZEPS);
2296: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret)))*/
2297: printf(".");fflush(stdout);
2298: fprintf(ficlog,".");fflush(ficlog);
2299: #ifdef DEBUGBRENT
2300: 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);
2301: 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);
2302: /* if ((fabs(x-xm) <= (tol2-0.5*(b-a)))||(2.0*fabs(fu-ftemp) <= ftol*1.e-2*(fabs(fu)+fabs(ftemp)))) { */
2303: #endif
2304: if (fabs(x-xm) <= (tol2-0.5*(b-a))){
2305: *xmin=x;
2306: return fx;
2307: }
2308: ftemp=fu;
2309: if (fabs(e) > tol1) {
2310: r=(x-w)*(fx-fv);
2311: q=(x-v)*(fx-fw);
2312: p=(x-v)*q-(x-w)*r;
2313: q=2.0*(q-r);
2314: if (q > 0.0) p = -p;
2315: q=fabs(q);
2316: etemp=e;
2317: e=d;
2318: if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x))
2319: d=CGOLD*(e=(x >= xm ? a-x : b-x));
2320: else {
2321: d=p/q;
2322: u=x+d;
2323: if (u-a < tol2 || b-u < tol2)
2324: d=SIGN(tol1,xm-x);
2325: }
2326: } else {
2327: d=CGOLD*(e=(x >= xm ? a-x : b-x));
2328: }
2329: u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d));
2330: fu=(*f)(u);
2331: if (fu <= fx) {
2332: if (u >= x) a=x; else b=x;
2333: SHFT(v,w,x,u)
2334: SHFT(fv,fw,fx,fu)
2335: } else {
2336: if (u < x) a=u; else b=u;
2337: if (fu <= fw || w == x) {
2338: v=w;
2339: w=u;
2340: fv=fw;
2341: fw=fu;
2342: } else if (fu <= fv || v == x || v == w) {
2343: v=u;
2344: fv=fu;
2345: }
2346: }
2347: }
2348: nrerror("Too many iterations in brent");
2349: *xmin=x;
2350: return fx;
2351: }
2352:
2353: /****************** mnbrak ***********************/
2354:
2355: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, double *fc,
2356: double (*func)(double))
2357: { /* Given a function func , and given distinct initial points ax and bx , this routine searches in
2358: the downhill direction (defined by the function as evaluated at the initial points) and returns
2359: new points ax , bx , cx that bracket a minimum of the function. Also returned are the function
2360: values at the three points, fa, fb , and fc such that fa > fb and fb < fc.
2361: */
2362: double ulim,u,r,q, dum;
2363: double fu;
2364:
2365: double scale=10.;
2366: int iterscale=0;
2367:
2368: *fa=(*func)(*ax); /* xta[j]=pcom[j]+(*ax)*xicom[j]; fa=f(xta[j])*/
2369: *fb=(*func)(*bx); /* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) */
2370:
2371:
2372: /* while(*fb != *fb){ /\* *ax should be ok, reducing distance to *ax *\/ */
2373: /* printf("Warning mnbrak *fb = %lf, *bx=%lf *ax=%lf *fa==%lf iter=%d\n",*fb, *bx, *ax, *fa, iterscale++); */
2374: /* *bx = *ax - (*ax - *bx)/scale; */
2375: /* *fb=(*func)(*bx); /\* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) *\/ */
2376: /* } */
2377:
2378: if (*fb > *fa) {
2379: SHFT(dum,*ax,*bx,dum)
2380: SHFT(dum,*fb,*fa,dum)
2381: }
2382: *cx=(*bx)+GOLD*(*bx-*ax);
2383: *fc=(*func)(*cx);
2384: #ifdef DEBUG
2385: printf("mnbrak0 a=%lf *fa=%lf, b=%lf *fb=%lf, c=%lf *fc=%lf\n",*ax,*fa,*bx,*fb,*cx, *fc);
2386: fprintf(ficlog,"mnbrak0 a=%lf *fa=%lf, b=%lf *fb=%lf, c=%lf *fc=%lf\n",*ax,*fa,*bx,*fb,*cx, *fc);
2387: #endif
2388: while (*fb > *fc) { /* Declining a,b,c with fa> fb > fc. If fc=inf it exits and if flat fb=fc it exits too.*/
2389: r=(*bx-*ax)*(*fb-*fc);
2390: q=(*bx-*cx)*(*fb-*fa); /* What if fa=inf */
2391: u=(*bx)-((*bx-*cx)*q-(*bx-*ax)*r)/
2392: (2.0*SIGN(FMAX(fabs(q-r),TINY),q-r)); /* Minimum abscissa of a parabolic estimated from (a,fa), (b,fb) and (c,fc). */
2393: ulim=(*bx)+GLIMIT*(*cx-*bx); /* Maximum abscissa where function should be evaluated */
2394: if ((*bx-u)*(u-*cx) > 0.0) { /* if u_p is between b and c */
2395: fu=(*func)(u);
2396: #ifdef DEBUG
2397: /* f(x)=A(x-u)**2+f(u) */
2398: double A, fparabu;
2399: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
2400: fparabu= *fa - A*(*ax-u)*(*ax-u);
2401: 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);
2402: 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);
2403: /* And thus,it can be that fu > *fc even if fparabu < *fc */
2404: /* mnbrak (*ax=7.666299858533, *fa=299039.693133272231), (*bx=8.595447774979, *fb=298976.598289369489),
2405: (*cx=10.098840694817, *fc=298946.631474258087), (*u=9.852501168332, fu=298948.773013752128, fparabu=298945.434711494134) */
2406: /* In that case, there is no bracket in the output! Routine is wrong with many consequences.*/
2407: #endif
2408: #ifdef MNBRAKORIGINAL
2409: #else
2410: /* if (fu > *fc) { */
2411: /* #ifdef DEBUG */
2412: /* printf("mnbrak4 fu > fc \n"); */
2413: /* fprintf(ficlog, "mnbrak4 fu > fc\n"); */
2414: /* #endif */
2415: /* /\* 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 *\\/ *\/ */
2416: /* /\* SHFT(*fa,*fc,fu,*fc) /\\* (b, u, c) is a bracket while test fb > fc will be fu > fc will exit *\\/ *\/ */
2417: /* dum=u; /\* Shifting c and u *\/ */
2418: /* u = *cx; */
2419: /* *cx = dum; */
2420: /* dum = fu; */
2421: /* fu = *fc; */
2422: /* *fc =dum; */
2423: /* } else { /\* end *\/ */
2424: /* #ifdef DEBUG */
2425: /* printf("mnbrak3 fu < fc \n"); */
2426: /* fprintf(ficlog, "mnbrak3 fu < fc\n"); */
2427: /* #endif */
2428: /* dum=u; /\* Shifting c and u *\/ */
2429: /* u = *cx; */
2430: /* *cx = dum; */
2431: /* dum = fu; */
2432: /* fu = *fc; */
2433: /* *fc =dum; */
2434: /* } */
2435: #ifdef DEBUGMNBRAK
2436: double A, fparabu;
2437: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
2438: fparabu= *fa - A*(*ax-u)*(*ax-u);
2439: 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);
2440: 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);
2441: #endif
2442: dum=u; /* Shifting c and u */
2443: u = *cx;
2444: *cx = dum;
2445: dum = fu;
2446: fu = *fc;
2447: *fc =dum;
2448: #endif
2449: } else if ((*cx-u)*(u-ulim) > 0.0) { /* u is after c but before ulim */
2450: #ifdef DEBUG
2451: printf("\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
2452: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
2453: #endif
2454: fu=(*func)(u);
2455: if (fu < *fc) {
2456: #ifdef DEBUG
2457: printf("\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
2458: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
2459: #endif
2460: SHFT(*bx,*cx,u,*cx+GOLD*(*cx-*bx))
2461: SHFT(*fb,*fc,fu,(*func)(u))
2462: #ifdef DEBUG
2463: printf("\nmnbrak2 shift GOLD c=%lf",*cx+GOLD*(*cx-*bx));
2464: #endif
2465: }
2466: } else if ((u-ulim)*(ulim-*cx) >= 0.0) { /* u outside ulim (verifying that ulim is beyond c) */
2467: #ifdef DEBUG
2468: printf("\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
2469: fprintf(ficlog,"\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
2470: #endif
2471: u=ulim;
2472: fu=(*func)(u);
2473: } else { /* u could be left to b (if r > q parabola has a maximum) */
2474: #ifdef DEBUG
2475: printf("\nmnbrak2 u=%lf could be left to b=%lf (if r=%lf > q=%lf parabola has a maximum)\n",u,*bx,r,q);
2476: 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);
2477: #endif
2478: u=(*cx)+GOLD*(*cx-*bx);
2479: fu=(*func)(u);
2480: #ifdef DEBUG
2481: printf("\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
2482: fprintf(ficlog,"\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
2483: #endif
2484: } /* end tests */
2485: SHFT(*ax,*bx,*cx,u)
2486: SHFT(*fa,*fb,*fc,fu)
2487: #ifdef DEBUG
2488: printf("\nmnbrak2 shift (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf)\n",*ax,*fa,*bx,*fb,*cx,*fc);
2489: fprintf(ficlog, "\nmnbrak2 shift (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf)\n",*ax,*fa,*bx,*fb,*cx,*fc);
2490: #endif
2491: } /* end while; ie return (a, b, c, fa, fb, fc) such that a < b < c with f(a) > f(b) and fb < f(c) */
2492: }
2493:
2494: /*************** linmin ************************/
2495: /* Given an n -dimensional point p[1..n] and an n -dimensional direction xi[1..n] , moves and
2496: resets p to where the function func(p) takes on a minimum along the direction xi from p ,
2497: and replaces xi by the actual vector displacement that p was moved. Also returns as fret
2498: the value of func at the returned location p . This is actually all accomplished by calling the
2499: routines mnbrak and brent .*/
2500: int ncom;
2501: double *pcom,*xicom;
2502: double (*nrfunc)(double []);
2503:
2504: #ifdef LINMINORIGINAL
2505: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []))
2506: #else
2507: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []), int *flat)
2508: #endif
2509: {
2510: double brent(double ax, double bx, double cx,
2511: double (*f)(double), double tol, double *xmin);
2512: double f1dim(double x);
2513: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb,
2514: double *fc, double (*func)(double));
2515: int j;
2516: double xx,xmin,bx,ax;
2517: double fx,fb,fa;
2518:
2519: #ifdef LINMINORIGINAL
2520: #else
2521: double scale=10., axs, xxs; /* Scale added for infinity */
2522: #endif
2523:
2524: ncom=n;
2525: pcom=vector(1,n);
2526: xicom=vector(1,n);
2527: nrfunc=func;
2528: for (j=1;j<=n;j++) {
2529: pcom[j]=p[j];
2530: xicom[j]=xi[j]; /* Former scale xi[j] of currrent direction i */
2531: }
2532:
2533: #ifdef LINMINORIGINAL
2534: xx=1.;
2535: #else
2536: axs=0.0;
2537: xxs=1.;
2538: do{
2539: xx= xxs;
2540: #endif
2541: ax=0.;
2542: mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim); /* Outputs: xtx[j]=pcom[j]+(*xx)*xicom[j]; fx=f(xtx[j]) */
2543: /* brackets with inputs ax=0 and xx=1, but points, pcom=p, and directions values, xicom=xi, are sent via f1dim(x) */
2544: /* 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)) */
2545: /* Outputs: fa=f(p(j)) and fx=f(p(j) + xxs * xi(j) ) and f(bx)= f(p(j)+ bx* xi(j)) */
2546: /* Given input ax=axs and xx=xxs, xx might be too far from ax to get a finite f(xx) */
2547: /* Searches on line, outputs (ax, xx, bx) such that fx < min(fa and fb) */
2548: /* 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]]*/
2549: #ifdef LINMINORIGINAL
2550: #else
2551: if (fx != fx){
2552: xxs=xxs/scale; /* Trying a smaller xx, closer to initial ax=0 */
2553: printf("|");
2554: fprintf(ficlog,"|");
2555: #ifdef DEBUGLINMIN
2556: 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);
2557: #endif
2558: }
2559: }while(fx != fx && xxs > 1.e-5);
2560: #endif
2561:
2562: #ifdef DEBUGLINMIN
2563: 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);
2564: 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);
2565: #endif
2566: #ifdef LINMINORIGINAL
2567: #else
2568: if(fb == fx){ /* Flat function in the direction */
2569: xmin=xx;
2570: *flat=1;
2571: }else{
2572: *flat=0;
2573: #endif
2574: /*Flat mnbrak2 shift (*ax=0.000000000000, *fa=51626.272983130431), (*bx=-1.618034000000, *fb=51590.149499362531), (*cx=-4.236068025156, *fc=51590.149499362531) */
2575: *fret=brent(ax,xx,bx,f1dim,TOL,&xmin); /* Giving a bracketting triplet (ax, xx, bx), find a minimum, xmin, according to f1dim, *fret(xmin),*/
2576: /* fa = f(p[j] + ax * xi[j]), fx = f(p[j] + xx * xi[j]), fb = f(p[j] + bx * xi[j]) */
2577: /* fmin = f(p[j] + xmin * xi[j]) */
2578: /* P+lambda n in that direction (lambdamin), with TOL between abscisses */
2579: /* f1dim(xmin): for (j=1;j<=ncom;j++) xt[j]=pcom[j]+xmin*xicom[j]; */
2580: #ifdef DEBUG
2581: 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);
2582: 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);
2583: #endif
2584: #ifdef LINMINORIGINAL
2585: #else
2586: }
2587: #endif
2588: #ifdef DEBUGLINMIN
2589: printf("linmin end ");
2590: fprintf(ficlog,"linmin end ");
2591: #endif
2592: for (j=1;j<=n;j++) {
2593: #ifdef LINMINORIGINAL
2594: xi[j] *= xmin;
2595: #else
2596: #ifdef DEBUGLINMIN
2597: if(xxs <1.0)
2598: printf(" before xi[%d]=%12.8f", j,xi[j]);
2599: #endif
2600: 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) */
2601: #ifdef DEBUGLINMIN
2602: if(xxs <1.0)
2603: 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 );
2604: #endif
2605: #endif
2606: p[j] += xi[j]; /* Parameters values are updated accordingly */
2607: }
2608: #ifdef DEBUGLINMIN
2609: printf("\n");
2610: printf("Comparing last *frec(xmin=%12.8f)=%12.8f from Brent and frec(0.)=%12.8f \n", xmin, *fret, (*func)(p));
2611: fprintf(ficlog,"Comparing last *frec(xmin=%12.8f)=%12.8f from Brent and frec(0.)=%12.8f \n", xmin, *fret, (*func)(p));
2612: for (j=1;j<=n;j++) {
2613: printf(" xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
2614: fprintf(ficlog," xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
2615: if(j % ncovmodel == 0){
2616: printf("\n");
2617: fprintf(ficlog,"\n");
2618: }
2619: }
2620: #else
2621: #endif
2622: free_vector(xicom,1,n);
2623: free_vector(pcom,1,n);
2624: }
2625:
2626: /**** praxis ****/
2627: # include <float.h>
2628:
2629: void transpose_in_place ( int n, double **a )
2630:
2631: /******************************************************************************/
2632: /*
2633: Purpose:
2634:
2635: TRANSPOSE_IN_PLACE transposes a square matrix in place.
2636: Licensing:
2637: This code is distributed under the GNU LGPL license.
2638:
2639: Input, int N, the number of rows and columns of the matrix A.
2640:
2641: Input/output, double A[N*N], the matrix to be transposed.
2642: */
2643: {
2644: int i;
2645: int j;
2646: double t;
2647:
2648: /* for ( j = 0; j < n; j++ ){ */
2649: /* for ( i = 0; i < j; i++ ) { */
2650: for ( j = 1; j <= n; j++ ){
2651: for ( i = 1; i < j; i++ ) {
2652: /* t = a[i+j*n]; */
2653: /* a[i+j*n] = a[j+i*n]; */
2654: /* a[j+i*n] = t; */
2655: t = a[i][j];
2656: a[i][j] = a[j][i];
2657: a[j][i] = t;
2658: }
2659: }
2660: return;
2661: }
2662:
2663: double pythag( double x, double y )
2664:
2665: /******************************************************************************/
2666: /*
2667: Purpose:
2668: R8_HYPOT returns the value of sqrt ( X^2 + Y^2 ).
2669: Licensing:
2670: This code is distributed under the GNU LGPL license.
2671: Modified:
2672: 26 March 2012
2673: Author:
2674: John Burkardt
2675: Parameters:
2676: Input, double X, Y, the arguments.
2677: Output, double R8_HYPOT, the value of sqrt ( X^2 + Y^2 ).
2678: */
2679: {
2680: double a;
2681: double b;
2682: double value;
2683:
2684: if ( fabs ( x ) < fabs ( y ) ) {
2685: a = fabs ( y );
2686: b = fabs ( x );
2687: } else {
2688: a = fabs ( x );
2689: b = fabs ( y );
2690: }
2691: /*
2692: A contains the larger value.
2693: */
2694: if ( a == 0.0 ) {
2695: value = 0.0;
2696: } else {
2697: value = a * sqrt ( 1.0 + ( b / a ) * ( b / a ) );
2698: }
2699: return value;
2700: }
2701:
2702: void svsort ( int n, double d[], double **v )
2703:
2704: /******************************************************************************/
2705: /*
2706: Purpose:
2707:
2708: SVSORT descending sorts D and adjusts the corresponding columns of V.
2709:
2710: Discussion:
2711: A simple bubble sort is used on D.
2712: In our application, D contains singular values, and the columns of V are
2713: the corresponding right singular vectors.
2714: Author:
2715: Original FORTRAN77 version by Richard Brent.
2716: Richard Brent,
2717: Algorithms for Minimization with Derivatives,
2718: Prentice Hall, 1973,
2719: Reprinted by Dover, 2002.
2720:
2721: Parameters:
2722: Input, int N, the length of D, and the order of V.
2723: Input/output, double D[N], the vector to be sorted.
2724: On output, the entries of D are in descending order.
2725:
2726: Input/output, double V[N,N], an N by N array to be adjusted
2727: as D is sorted. In particular, if the value that was in D(I) on input is
2728: moved to D(J) on output, then the input column V(*,I) is moved to
2729: the output column V(*,J).
2730: */
2731: {
2732: int i, j1, j2, j3;
2733: double t;
2734:
2735: for (j1 = 1; j1 < n; j1++) {
2736: /*
2737: * Find J3, the index of the largest entry in D[J1:N-1].
2738: * MAXLOC apparently requires its output to be an array.
2739: */
2740: j3 = j1;
2741: for (j2 = j1+1; j2 < n; j2++) {
2742: if (d[j3] < d[j2]) {
2743: j3 = j2;
2744: }
2745: }
2746: /*
2747: * If J1 != J3, swap D[J1] and D[J3], and columns J1 and J3 of V.
2748: */
2749: if (j1 != j3) {
2750: t = d[j1];
2751: d[j1] = d[j3];
2752: d[j3] = t;
2753: for (i = 1; i <= n; i++) {
2754: t = v[i][j1];
2755: v[i][j1] = v[i][j3];
2756: v[i][j3] = t;
2757: } /* end i */
2758: } /* end j1 != j3 */
2759: } /* end j1 */
2760: return;
2761: }
2762:
2763: /* void svdcmp(double **a, int m, int n, double w[], double **v) */
2764: void svdminfit(double **a, int m, int n, double w[])
2765: {
2766: /* From numerical recipes */
2767: /* Given a matrix a[1..m][1..n], this routine computes its singular value */
2768: /* decomposition, A = U ·W ·V T . The matrix U replaces a on output. */
2769: /* The diagonal matrix of singular values W is out- put as a vector w[1..n]. */
2770: /* The matrix V (not the transpose V T ) is output as v[1..n][1..n]. */
2771:
2772: /* But in fact from Golub 1970 Algol60 */
2773:
2774: /* Computation of the singular values and complete orthogonal decom- */
2775: /* position of a real rectangular matrix A, */
2776: /* A = U diag (q) V^T, U^T U = V^T V =I , */
2777: /* where the arrays a[1:m, 1:n], u[1:m, 1 :n], v[1:n, 1:n], q[1:n] re- */
2778: /* present A, U, V, q respectively. The actual parameters corresponding */
2779: /* to a, u, v may all be identical unless withu=withv = true . In this */
2780: /* case, the actual parameters corresponding to u and v must differ. */
2781: /* m >= n is assumed; */
2782:
2783: /* Simplified (as in praxis) in order to output only V (replacing A), w is the diagonal matrix q */
2784:
2785:
2786: double pythag(double a, double b);
2787: int flag,i,its,j,jj,k,l,nm;
2788: double anorm,c,f,g,h,s,scale,x,y,z,*rv1;
2789:
2790: rv1=vector(1,n);
2791: /* Householder's reduction to bidiagonal form; */
2792: g=scale=anorm=0.0;
2793: for (i=1;i<=n;i++) {
2794: l=i+1;
2795: rv1[i]=scale*g;
2796: g=s=scale=0.0;
2797: if (i <= m) {
2798: for (k=i;k<=m;k++) scale += fabs(a[k][i]);
2799: if (scale) {
2800: for (k=i;k<=m;k++) {
2801: a[k][i] /= scale;
2802: s += a[k][i]*a[k][i];
2803: }
2804: f=a[i][i];
2805: g = -SIGN(sqrt(s),f);
2806: h=f*g-s;
2807: a[i][i]=f-g;
2808: for (j=l;j<=n;j++) {
2809: for (s=0.0,k=i;k<=m;k++) s += a[k][i]*a[k][j];
2810: f=s/h;
2811: for (k=i;k<=m;k++) a[k][j] += f*a[k][i];
2812: }
2813: for (k=i;k<=m;k++) a[k][i] *= scale;
2814: }
2815: }
2816: w[i]=scale *g; /* p 411*/
2817: g=s=scale=0.0;
2818: if (i <= m && i != n) {
2819: for (k=l;k<=n;k++) scale += fabs(a[i][k]);
2820: if (scale) {
2821: for (k=l;k<=n;k++) {
2822: a[i][k] /= scale;
2823: s += a[i][k]*a[i][k];
2824: }
2825: f=a[i][l];
2826: g = -SIGN(sqrt(s),f);
2827: h=f*g-s;
2828: a[i][l]=f-g;
2829: for (k=l;k<=n;k++) rv1[k]=a[i][k]/h;
2830: for (j=l;j<=m;j++) {
2831: for (s=0.0,k=l;k<=n;k++) s += a[j][k]*a[i][k];
2832: for (k=l;k<=n;k++) a[j][k] += s*rv1[k];
2833: }
2834: for (k=l;k<=n;k++) a[i][k] *= scale;
2835: }
2836: }
2837: /* y : = abs(q[i]) +abs(e[i]); if y > x then x : = y */
2838: /* anorm=DMAX(anorm,(fabs(w[i])+fabs(rv1[i]))); */
2839: anorm=FMAX(anorm,(fabs(w[i])+fabs(rv1[i])));
2840: }
2841: /* Comment accumulation of right-hand transformations */
2842: for (i=n;i>=1;i--) {
2843: if (i < n) {
2844: if (g) {
2845: for (j=l;j<=n;j++) a[j][i]=(a[i][j]/a[i][l])/g; /* Double division to avoid possible underflow. */
2846: /* for (j=l;j<=n;j++) v[j][i]=(a[i][j]/a[i][l])/g; */
2847: for (j=l;j<=n;j++) {
2848: /* for (s=0.0,k=l;k<=n;k++) s += a[i][k]*v[k][j]; */
2849: for (s=0.0,k=l;k<=n;k++) s += a[i][k]*a[k][j];
2850: /* for (k=l;k<=n;k++) v[k][j] += s*v[k][i]; */
2851: for (k=l;k<=n;k++) a[k][j] += s*a[k][i];
2852: }
2853: }
2854: /* for (j=l;j<=n;j++) v[i][j]=v[j][i]=0.0; */
2855: for (j=l;j<=n;j++) a[i][j]=a[j][i]=0.0;
2856: }
2857: /* v[i][i]=1.0; */
2858: a[i][i]=1.0;
2859: g=rv1[i];
2860: l=i;
2861: }
2862: /* Comment accumulation of left-hand transformations; */
2863: /* for (i=IMIN(m,n);i>=1;i--) { */
2864: for (i=FMIN(m,n);i>=1;i--) {
2865: l=i+1;
2866: g=w[i];
2867: for (j=l;j<=n;j++) a[i][j]=0.0;
2868: if (g) {
2869: g=1.0/g;
2870: for (j=l;j<=n;j++) {
2871: for (s=0.0,k=l;k<=m;k++) s += a[k][i]*a[k][j];
2872: f=(s/a[i][i])*g;
2873: for (k=i;k<=m;k++) a[k][j] += f*a[k][i];
2874: }
2875: for (j=i;j<=m;j++) a[j][i] *= g;
2876: } else for (j=i;j<=m;j++) a[j][i]=0.0;
2877: ++a[i][i];
2878: }
2879: /* comment diagonalization of the bidiagonal form; */
2880: for (k=n;k>=1;k--) {
2881: for (its=1;its<=30;its++) { /* Loop over singular values, and over allowed iterations. */
2882: flag=1;
2883: for (l=k;l>=1;l--) { /* Test for splitting. */
2884: nm=l-1; /* Note that rv1[1] is always zero. */
2885: if ((double)(fabs(rv1[l])+anorm) == anorm) {
2886: flag=0;
2887: break;
2888: }
2889: if ((double)(fabs(w[nm])+anorm) == anorm) break;
2890: }
2891: if (flag) {
2892: c=0.0; /* Cancellation of rv1[l], if l > 1. */
2893: s=1.0;
2894: for (i=l;i<=k;i++) {
2895: f=s*rv1[i];
2896: rv1[i]=c*rv1[i];
2897: if ((double)(fabs(f)+anorm) == anorm) break;
2898: g=w[i];
2899: h=pythag(f,g);
2900: w[i]=h;
2901: h=1.0/h;
2902: c=g*h;
2903: s = -f*h;
2904: for (j=1;j<=m;j++) {
2905: y=a[j][nm];
2906: z=a[j][i];
2907: a[j][nm]=y*c+z*s;
2908: a[j][i]=z*c-y*s;
2909: }
2910: }
2911: }
2912: z=w[k];
2913: if (l == k) { /* Convergence */
2914: if (z < 0.0) { /* Singular value is made nonnegative. */
2915: w[k] = -z;
2916: /* for (j=1;j<=n;j++) v[j][k] = -v[j][k]; */
2917: for (j=1;j<=n;j++) a[j][k] = -a[j][k];
2918: }
2919: break;
2920: }
2921: if (its == 30) nrerror("no convergence in 30 svdcmp iterations");
2922: x=w[l]; /* shift from bottom 2 x 2 minor; */
2923: nm=k-1;
2924: y=w[nm];
2925: g=rv1[nm];
2926: h=rv1[k];
2927: f=((y-z)*(y+z)+(g-h)*(g+h))/(2.0*h*y);
2928: g=pythag(f,1.0);
2929: /* g=dpythag(f,1.0); */
2930: f=((x-z)*(x+z)+h*((y/(f+SIGN(g,f)))-h))/x;
2931: c=s=1.0; /* next QR transformation */
2932: for (j=l;j<=nm;j++) {
2933: i=j+1;
2934: g=rv1[i];
2935: y=w[i];
2936: h=s*g;
2937: g=c*g;
2938: /* z=dpythag(f,h); */
2939: z=pythag(f,h);
2940: rv1[j]=z;
2941: c=f/z;
2942: s=h/z;
2943: f=x*c+g*s;
2944: g = g*c-x*s;
2945: h=y*s;
2946: y *= c;
2947: /* if withv then for j:= 1 step 1 until n do */
2948: for (jj=1;jj<=n;jj++) {
2949: /* x=v[jj][j]; */
2950: /* z=v[jj][i]; */
2951: x=a[jj][j];
2952: z=a[jj][i];
2953: /* v[jj][j]=x*c+z*s; */
2954: /* v[jj][i]=z*c-x*s; */
2955: a[jj][j]=x*c+z*s;
2956: a[jj][i]=z*c-x*s;
2957: }
2958: /* z=dpythag(f,h); */
2959: z=pythag(f,h);
2960: w[j]=z;
2961: if (z) {
2962: z=1.0/z;
2963: c=f*z;
2964: s=h*z;
2965: }
2966: f=c*g+s*y;
2967: x=c*y-s*g;
2968: /* if withu then for j:=1 step 1 until m do */
2969: /* for (jj=1;jj<=m;jj++) { */
2970: /* y=a[jj][j]; */
2971: /* z=a[jj][i]; */
2972: /* a[jj][j]=y*c+z*s; */
2973: /* a[jj][i]=z*c-y*s; */
2974: /* } */
2975: }
2976: rv1[l]=0.0;
2977: rv1[k]=f;
2978: w[k]=x;
2979: }
2980: }
2981: free_vector(rv1,1,n);
2982: }
2983:
2984: /* end praxis */
2985:
2986: /*************** powell ************************/
2987: /*
2988: Minimization of a function func of n variables. Input consists in an initial starting point
2989: p[1..n] ; an initial matrix xi[1..n][1..n] whose columns contain the initial set of di-
2990: rections (usually the n unit vectors); and ftol, the fractional tolerance in the function value
2991: such that failure to decrease by more than this amount in one iteration signals doneness. On
2992: output, p is set to the best point found, xi is the then-current direction set, fret is the returned
2993: function value at p , and iter is the number of iterations taken. The routine linmin is used.
2994: */
2995: #ifdef LINMINORIGINAL
2996: #else
2997: int *flatdir; /* Function is vanishing in that direction */
2998: int flat=0, flatd=0; /* Function is vanishing in that direction */
2999: #endif
3000: void powell(double p[], double **xi, int n, double ftol, int *iter, double *fret,
3001: double (*func)(double []))
3002: {
3003: #ifdef LINMINORIGINAL
3004: void linmin(double p[], double xi[], int n, double *fret,
3005: double (*func)(double []));
3006: #else
3007: void linmin(double p[], double xi[], int n, double *fret,
3008: double (*func)(double []),int *flat);
3009: #endif
3010: int i,ibig,j,jk,k;
3011: double del,t,*pt,*ptt,*xit;
3012: double directest;
3013: double fp,fptt;
3014: double *xits;
3015: int niterf, itmp;
3016: int Bigter=0, nBigterf=1;
3017:
3018: pt=vector(1,n);
3019: ptt=vector(1,n);
3020: xit=vector(1,n);
3021: xits=vector(1,n);
3022: *fret=(*func)(p);
3023: for (j=1;j<=n;j++) pt[j]=p[j];
3024: rcurr_time = time(NULL);
3025: fp=(*fret); /* Initialisation */
3026: for (*iter=1;;++(*iter)) {
3027: ibig=0;
3028: del=0.0;
3029: rlast_time=rcurr_time;
3030: rlast_btime=rcurr_time;
3031: /* (void) gettimeofday(&curr_time,&tzp); */
3032: rcurr_time = time(NULL);
3033: curr_time = *localtime(&rcurr_time);
3034: /* printf("\nPowell iter=%d -2*LL=%.12f gain=%.12f=%.3g %ld sec. %ld sec.",*iter,*fret, fp-*fret,fp-*fret, rcurr_time-rlast_time, rcurr_time-rstart_time);fflush(stdout); */
3035: /* fprintf(ficlog,"\nPowell iter=%d -2*LL=%.12f gain=%.12f=%.3g %ld sec. %ld sec.",*iter,*fret, fp-*fret,fp-*fret,rcurr_time-rlast_time, rcurr_time-rstart_time); fflush(ficlog); */
3036: /* Bigter=(*iter - *iter % ncovmodel)/ncovmodel +1; /\* Big iteration, i.e on ncovmodel cycle *\/ */
3037: Bigter=(*iter - (*iter-1) % n)/n +1; /* Big iteration, i.e on ncovmodel cycle */
3038: printf("\nPowell iter=%d Big Iter=%d -2*LL=%.12f gain=%.3lg %ld sec. %ld sec.",*iter,Bigter,*fret,fp-*fret, rcurr_time-rlast_time, rcurr_time-rstart_time);fflush(stdout);
3039: fprintf(ficlog,"\nPowell iter=%d Big Iter=%d -2*LL=%.12f gain=%.3lg %ld sec. %ld sec.",*iter,Bigter,*fret,fp-*fret,rcurr_time-rlast_time, rcurr_time-rstart_time); fflush(ficlog);
3040: fprintf(ficrespow,"%d %d %.12f %d",*iter,Bigter, *fret,curr_time.tm_sec-start_time.tm_sec);
3041: fp=(*fret); /* From former iteration or initial value */
3042: for (i=1;i<=n;i++) {
3043: fprintf(ficrespow," %.12lf", p[i]);
3044: }
3045: fprintf(ficrespow,"\n");fflush(ficrespow);
3046: printf("\n#model= 1 + age ");
3047: fprintf(ficlog,"\n#model= 1 + age ");
3048: if(nagesqr==1){
3049: printf(" + age*age ");
3050: fprintf(ficlog," + age*age ");
3051: }
3052: for(j=1;j <=ncovmodel-2;j++){
3053: if(Typevar[j]==0) {
3054: printf(" + V%d ",Tvar[j]);
3055: fprintf(ficlog," + V%d ",Tvar[j]);
3056: }else if(Typevar[j]==1) {
3057: printf(" + V%d*age ",Tvar[j]);
3058: fprintf(ficlog," + V%d*age ",Tvar[j]);
3059: }else if(Typevar[j]==2) {
3060: printf(" + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
3061: fprintf(ficlog," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
3062: }else if(Typevar[j]==3) {
3063: printf(" + V%d*V%d*age ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
3064: fprintf(ficlog," + V%d*V%d*age ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
3065: }
3066: }
3067: printf("\n");
3068: /* printf("12 47.0114589 0.0154322 33.2424412 0.3279905 2.3731903 */
3069: /* 13 -21.5392400 0.1118147 1.2680506 1.2973408 -1.0663662 */
3070: fprintf(ficlog,"\n");
3071: for(i=1,jk=1; i <=nlstate; i++){
3072: for(k=1; k <=(nlstate+ndeath); k++){
3073: if (k != i) {
3074: printf("%d%d ",i,k);
3075: fprintf(ficlog,"%d%d ",i,k);
3076: for(j=1; j <=ncovmodel; j++){
3077: printf("%12.7f ",p[jk]);
3078: fprintf(ficlog,"%12.7f ",p[jk]);
3079: jk++;
3080: }
3081: printf("\n");
3082: fprintf(ficlog,"\n");
3083: }
3084: }
3085: }
3086: if(*iter <=3 && *iter >1){
3087: tml = *localtime(&rcurr_time);
3088: strcpy(strcurr,asctime(&tml));
3089: rforecast_time=rcurr_time;
3090: itmp = strlen(strcurr);
3091: if(strcurr[itmp-1]=='\n') /* Windows outputs with a new line */
3092: strcurr[itmp-1]='\0';
3093: printf("\nConsidering the time needed for the last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
3094: fprintf(ficlog,"\nConsidering the time needed for this last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
3095: for(nBigterf=1;nBigterf<=31;nBigterf+=10){
3096: niterf=nBigterf*ncovmodel;
3097: /* rforecast_time=rcurr_time+(niterf-*iter)*(rcurr_time-rlast_time); */
3098: rforecast_time=rcurr_time+(niterf-*iter)*(rcurr_time-rlast_time);
3099: forecast_time = *localtime(&rforecast_time);
3100: strcpy(strfor,asctime(&forecast_time));
3101: itmp = strlen(strfor);
3102: if(strfor[itmp-1]=='\n')
3103: strfor[itmp-1]='\0';
3104: printf(" - if your program needs %d BIG iterations (%d iterations) to converge, convergence will be \n reached in %s i.e.\n on %s (current time is %s);\n",nBigterf, niterf, asc_diff_time(rforecast_time-rcurr_time,tmpout),strfor,strcurr);
3105: fprintf(ficlog," - if your program needs %d BIG iterations (%d iterations) to converge, convergence will be \n reached in %s i.e.\n on %s (current time is %s);\n",nBigterf, niterf, asc_diff_time(rforecast_time-rcurr_time,tmpout),strfor,strcurr);
3106: }
3107: }
3108: for (i=1;i<=n;i++) { /* For each direction i, maximisation after loading directions */
3109: for (j=1;j<=n;j++) xit[j]=xi[j][i]; /* Directions stored from previous iteration with previous scales */
3110: fptt=(*fret);
3111: #ifdef DEBUG
3112: printf("fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
3113: fprintf(ficlog, "fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
3114: #endif
3115: printf("%d",i);fflush(stdout); /* print direction (parameter) i */
3116: fprintf(ficlog,"%d",i);fflush(ficlog);
3117: #ifdef LINMINORIGINAL
3118: linmin(p,xit,n,fret,func); /* New point i minimizing in direction xit i has coordinates p[j].*/
3119: /* xit[j] gives the n coordinates of direction i as input.*/
3120: /* *fret gives the maximum value on direction xit */
3121: #else
3122: linmin(p,xit,n,fret,func,&flat); /* Point p[n]. xit[n] has been loaded for direction i as input.*/
3123: flatdir[i]=flat; /* Function is vanishing in that direction i */
3124: #endif
3125: /* Outputs are fret(new point p) p is updated and xit rescaled */
3126: if (fabs(fptt-(*fret)) > del) { /* We are keeping the max gain on each of the n directions */
3127: /* because that direction will be replaced unless the gain del is small */
3128: /* in comparison with the 'probable' gain, mu^2, with the last average direction. */
3129: /* Unless the n directions are conjugate some gain in the determinant may be obtained */
3130: /* with the new direction. */
3131: del=fabs(fptt-(*fret));
3132: ibig=i;
3133: }
3134: #ifdef DEBUG
3135: printf("%d %.12e",i,(*fret));
3136: fprintf(ficlog,"%d %.12e",i,(*fret));
3137: for (j=1;j<=n;j++) {
3138: xits[j]=FMAX(fabs(p[j]-pt[j]),1.e-5);
3139: printf(" x(%d)=%.12e",j,xit[j]);
3140: fprintf(ficlog," x(%d)=%.12e",j,xit[j]);
3141: }
3142: for(j=1;j<=n;j++) {
3143: printf(" p(%d)=%.12e",j,p[j]);
3144: fprintf(ficlog," p(%d)=%.12e",j,p[j]);
3145: }
3146: printf("\n");
3147: fprintf(ficlog,"\n");
3148: #endif
3149: } /* end loop on each direction i */
3150: /* Convergence test will use last linmin estimation (fret) and compare to former iteration (fp) */
3151: /* But p and xit have been updated at the end of linmin, *fret corresponds to new p, xit */
3152: for(j=1;j<=n;j++) {
3153: if(flatdir[j] >0){
3154: printf(" p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
3155: fprintf(ficlog," p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
3156: }
3157: /* printf("\n"); */
3158: /* fprintf(ficlog,"\n"); */
3159: }
3160: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret))) { /\* Did we reach enough precision? *\/ */
3161: if (2.0*fabs(fp-(*fret)) <= ftol) { /* Did we reach enough precision? */
3162: /* We could compare with a chi^2. chisquare(0.95,ddl=1)=3.84 */
3163: /* By adding age*age in a model, the new -2LL should be lower and the difference follows a */
3164: /* a chisquare statistics with 1 degree. To be significant at the 95% level, it should have */
3165: /* decreased of more than 3.84 */
3166: /* By adding age*age and V1*age the gain (-2LL) should be more than 5.99 (ddl=2) */
3167: /* By using V1+V2+V3, the gain should be 7.82, compared with basic 1+age. */
3168: /* By adding 10 parameters more the gain should be 18.31 */
3169:
3170: /* Starting the program with initial values given by a former maximization will simply change */
3171: /* the scales of the directions and the directions, because the are reset to canonical directions */
3172: /* Thus the first calls to linmin will give new points and better maximizations until fp-(*fret) is */
3173: /* under the tolerance value. If the tolerance is very small 1.e-9, it could last long. */
3174:
3175: free_vector(xit,1,n);
3176: free_vector(xits,1,n);
3177: free_vector(ptt,1,n);
3178: free_vector(pt,1,n);
3179: return;
3180: } /* enough precision */
3181: if (*iter == ITMAX*n) nrerror("powell exceeding maximum iterations.");
3182: for (j=1;j<=n;j++) { /* Computes the extrapolated point P_0 + 2 (P_n-P_0)=2Pn-P0 */
3183: ptt[j]=2.0*p[j]-pt[j];
3184: xit[j]=p[j]-pt[j]; /* Coordinate j of last direction xi_n=P_n-_0 */
3185: printf("\n %d xit=%12.7g p=%12.7g pt=%12.7g ",j,xit[j],p[j],pt[j]);
3186: pt[j]=p[j];
3187: }
3188: printf("\n");
3189: fptt=(*func)(ptt); /* f_3 */
3190: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */
3191: if (*iter <=4) {
3192: #else
3193: #endif
3194: #ifdef POWELLNOF3INFF1TEST /* skips test F3 <F1 */
3195: #else
3196: if (fptt < fp) { /* If extrapolated point is better, decide if we keep that new direction or not */
3197: #endif
3198: /* (x1 f1=fp), (x2 f2=*fret), (x3 f3=fptt), (xm fm) */
3199: /* From x1 (P0) distance of x2 is at h and x3 is 2h */
3200: /* Let f"(x2) be the 2nd derivative equal everywhere. */
3201: /* Then the parabolic through (x1,f1), (x2,f2) and (x3,f3) */
3202: /* will reach at f3 = fm + h^2/2 f"m ; f" = (f1 -2f2 +f3 ) / h**2 */
3203: /* Conditional for using this new direction is that mu^2 = (f1-2f2+f3)^2 /2 < del or directest <0 */
3204: /* also lamda^2=(f1-f2)^2/mu² is a parasite solution of powell */
3205: /* For powell, inclusion of this average direction is only if t(del)<0 or del inbetween mu^2 and lambda^2 */
3206: /* t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)-del*SQR(fp-fptt); */
3207: /* Even if f3 <f1, directest can be negative and t >0 */
3208: /* mu² and del² are equal when f3=f1 */
3209: /* f3 < f1 : mu² < del <= lambda^2 both test are equivalent */
3210: /* f3 < f1 : mu² < lambda^2 < del then directtest is negative and powell t is positive */
3211: /* f3 > f1 : lambda² < mu^2 < del then t is negative and directest >0 */
3212: /* f3 > f1 : lambda² < del < mu^2 then t is positive and directest >0 */
3213: #ifdef NRCORIGINAL
3214: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)- del*SQR(fp-fptt); /* Original Numerical Recipes in C*/
3215: #else
3216: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del); /* Intel compiler doesn't work on one line; bug reported */
3217: t= t- del*SQR(fp-fptt);
3218: #endif
3219: directest = fp-2.0*(*fret)+fptt - 2.0 * del; /* If delta was big enough we change it for a new direction */
3220: printf(" t=%g, directest=%g\n",t, directest);
3221: #ifdef POWELLNOTTRUECONJUGATE /* Searching for IBIG and testing for replacement */
3222: #ifdef POWELLORIGINAL
3223: if (t < 0.0) { /* Then we use it for new direction */
3224: #else /* Not POWELLOriginal but Brouard's */
3225: if (directest*t < 0.0) { /* Contradiction between both tests */
3226: 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);
3227: printf("f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt);
3228: 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);
3229: fprintf(ficlog,"f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt);
3230: }
3231: if (directest < 0.0) { /* Then we use (P0, Pn) for new direction Xi_n or Xi_iBig */
3232: #endif /* end POWELLOriginal */
3233: #endif /* POWELLNOTTRUECONJUGATE else means systematic replacement by new direction P_0P_n */
3234: #ifdef LINMINORIGINAL
3235: /* xit[j]=p[j]-pt[j] */
3236: printf("\n Computes min on P_0, P_n direction iter=%d Bigter=%d\n",*iter,Bigter);
3237: linmin(p,xit,n,fret,func); /* computes minimum on P_0,P_n direction: changes p and rescales xit.*/
3238: #else /* NOT LINMINORIGINAL but with searching for flat directions */
3239: printf("\n Flat Computes min on P_0, P_n direction iter=%d Bigter=%d\n",*iter,Bigter);
3240: linmin(p,xit,n,fret,func,&flat); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
3241: flatdir[i]=flat; /* Function is vanishing in that direction i */
3242: #endif
3243:
3244: #ifdef POWELLNOTTRUECONJUGATE
3245: #else
3246: #ifdef POWELLORIGINCONJUGATE
3247: for (j=1;j<=n;j++) {
3248: xi[j][ibig]=xi[j][n]; /* Replace direction with biggest decrease by last direction n */
3249: xi[j][n]=xit[j]; /* and this nth direction by the by the average p_0 p_n */
3250: }
3251: #else
3252: for (i=1;i<=n-1;i++) {
3253: for (j=1;j<=n;j++) {
3254: xi[j][i]=xi[j][i+1]; /* Standard method of conjugate directions, not Powell who changes the nth direction by p0 pn . */
3255: }
3256: }
3257: for (j=1;j<=n;j++) {
3258: xi[j][n]=xit[j]; /* and this nth direction by the by the average p_0 p_n */
3259: }
3260: #endif /* POWELLORIGINCONJUGATE*/
3261: #endif /*POWELLNOTTRUECONJUGATE*/
3262: printf(" Standard method of conjugate directions\n");
3263: printf("\n#A Before prax Bigter=%d model= 1 + age ", Bigter);
3264: for(j=1;j<=n;j++){
3265: printf("%d \n",j);
3266: for(i=1;i<=n;i++){
3267: printf(" %f",xi[j][i]);
3268: }
3269: }
3270: printf("\n");
3271:
3272: #ifdef NOTMINFIT
3273: #else
3274: if(*iter >n){
3275: /* if(Bigter >n){ */
3276: printf("\n#Before prax Bigter=%d model= 1 + age ", Bigter);
3277: printf("\n");
3278: for(j=1;j<=n;j++){
3279: printf("%d \n",j);
3280: for(i=1;i<=n;i++){
3281: printf(" %f",xi[j][i]);
3282: }
3283: }
3284: printf("\n");
3285: /*
3286: * Calculate a new set of orthogonal directions before repeating
3287: * the main loop.
3288: * Transpose V for SVD (minfit) (because minfit returns the right V in ULV=A):
3289: */
3290: printf(" Bigter=%d Calculate a new set of orthogonal directions before repeating the main loop.\n Transpose V for MINFIT:...\n",Bigter);
3291: transpose_in_place ( n, xi );
3292: /*
3293: SVD/MINFIT finds the singular value decomposition of V.
3294:
3295: This gives the principal values and principal directions of the
3296: approximating quadratic form without squaring the condition number.
3297: */
3298: printf(" SVDMINFIT finds the singular value decomposition of V. \n This gives the principal values and principal directions of the\n approximating quadratic form without squaring the condition number...\n");
3299: double *d; /* eigenvalues of principal directions */
3300: d=vector(1,n);
3301:
3302:
3303: svdminfit (xi, n, n, d ); /* In Brent's notation find d such that V=Q Diagonal(d) R, and Lambda=d^(-1/2) */
3304:
3305: printf("\n#After prax model= 1 + age ");
3306: fprintf(ficlog,"\n#model= 1 + age ");
3307:
3308: if(nagesqr==1){
3309: printf(" + age*age ");
3310: fprintf(ficlog," + age*age ");
3311: }
3312: for(j=1;j <=ncovmodel-2;j++){
3313: if(Typevar[j]==0) {
3314: printf(" + V%d ",Tvar[j]);
3315: fprintf(ficlog," + V%d ",Tvar[j]);
3316: }else if(Typevar[j]==1) {
3317: printf(" + V%d*age ",Tvar[j]);
3318: fprintf(ficlog," + V%d*age ",Tvar[j]);
3319: }else if(Typevar[j]==2) {
3320: printf(" + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
3321: fprintf(ficlog," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
3322: }else if(Typevar[j]==3) {
3323: printf(" + V%d*V%d*age ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
3324: fprintf(ficlog," + V%d*V%d*age ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
3325: }
3326: }
3327: printf("\n");
3328: fprintf(ficlog,"\n");
3329: for(i=1,jk=1; i <=nlstate; i++){
3330: for(k=1; k <=(nlstate+ndeath); k++){
3331: if (k != i) {
3332: printf("%d%d ",i,k);
3333: fprintf(ficlog,"%d%d ",i,k);
3334: for(j=1; j <=ncovmodel; j++){
3335: printf("%12.7f ",p[jk]);
3336: fprintf(ficlog,"%12.7f ",p[jk]);
3337: jk++;
3338: }
3339: printf("\n");
3340: fprintf(ficlog,"\n");
3341: }
3342: }
3343: }
3344: /* minfit ( n, vsmall, v, d ); */
3345: /* v is overwritten with R. */
3346: /*
3347: Heuristic numbers:
3348: If the axes may be badly scaled (which is to be avoided if
3349: possible), then set SCBD = 10. Otherwise set SCBD = 1.
3350:
3351: If the problem is known to be ill-conditioned, initialize ILLC = true.
3352: KTM is the number of iterations without improvement before the
3353: algorithm terminates. KTM = 4 is very cautious; usually KTM = 1
3354: is satisfactory.
3355: */
3356: double machep, small;
3357: double dmin;
3358: int illc=0; /* Local, int ILLC, is TRUE if the system is ill-conditioned. */
3359: machep = DBL_EPSILON;
3360: small = machep * machep;
3361: /* m2 = dsqrt(machep); */
3362:
3363: /*
3364: * Sort the eigenvalues and eigenvectors.
3365: */
3366: printf(" Sort the eigenvalues and eigenvectors....\n");
3367: svsort ( n, d, xi );
3368: printf("Sorted Eigenvalues:\n");
3369: for(i=1; i<=n;i++){
3370: printf(" d[%d]=%g",i,d[i]);
3371: }
3372: printf("\n");
3373: /*
3374: * Determine the smallest eigenvalue.
3375: */
3376: printf(" Determine the smallest eigenvalue.\n");
3377: dmin = fmax ( d[n], small );
3378: /*
3379: * The ratio of the smallest to largest eigenvalue determines whether
3380: * the system is ill conditioned.
3381: */
3382: if ( dmin < sqrt(machep) * d[1] ) { /* m2*d[0] */
3383: illc = 1;
3384: } else {
3385: illc = 0;
3386: }
3387: printf(" The ratio of the smallest to largest eigenvalue determines whether\n \
3388: the system is ill conditioned=%d . dmin=%.12lf < m2=%.12lf * d[1]=%.12lf \n",illc, dmin,sqrt(machep), d[1]);
3389: /* if ( 1.0 < scbd ) { */
3390: /* r8vec_print ( n, z, " The scale factors:" ); */
3391: /* } */
3392: /* r8vec_print ( n, d, " Principal values of the quadratic form:" ); */
3393: /* } */
3394: /* if ( 3 < prin ) { */
3395: /* r8mat_print ( n, n, v, " The principal axes:" ); */
3396: /* } */
3397: free_vector(d,1,n);
3398: /*
3399: * The main loop ends here.
3400: */
3401:
3402: /* if ( 0 < prin ) */
3403: /* { */
3404: /* r8vec_print ( n, x, " X:" ); */
3405: /* } */
3406: }
3407: #endif /* NOTMINFIT */
3408: #ifdef LINMINORIGINAL
3409: #else
3410: for (j=1, flatd=0;j<=n;j++) {
3411: if(flatdir[j]>0)
3412: flatd++;
3413: }
3414: if(flatd >0){
3415: printf("%d flat directions: ",flatd);
3416: fprintf(ficlog,"%d flat directions :",flatd);
3417: for (j=1;j<=n;j++) {
3418: if(flatdir[j]>0){
3419: printf("%d ",j);
3420: fprintf(ficlog,"%d ",j);
3421: }
3422: }
3423: printf("\n");
3424: fprintf(ficlog,"\n");
3425: #ifdef FLATSUP
3426: free_vector(xit,1,n);
3427: free_vector(xits,1,n);
3428: free_vector(ptt,1,n);
3429: free_vector(pt,1,n);
3430: return;
3431: #endif
3432: } /* endif(flatd >0) */
3433: #endif
3434: printf("Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
3435: fprintf(ficlog,"Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
3436: /* The minimization in direction $\xi_1$ gives $P_1$. From $P_1$ minimization in direction $\xi_2$ gives */
3437: /* $P_2$. Minimization of line $P_2-P_1$ gives new starting point $P^{(1)}_0$ and direction $\xi_1$ is dropped and replaced by second */
3438: /* direction $\xi_1^{(1)}=\xi_2$. Also second direction is replaced by new direction $\xi^{(1)}_2=P_2-P_0$. */
3439:
3440: /* At the second iteration, starting from $P_0^{(1)}$, minimization amongst $\xi^{(1)}_1$ gives point $P^{(1)}_1$. */
3441: /* Minimization amongst $\xi^{(1)}_2=(P_2-P_0)$ gives point $P^{(1)}_2$. As $P^{(2)}_1$ and */
3442: /* $P^{(1)}_0$ are minimizing in the same direction $P^{(1)}_2 - P^{(1)}_1= P_2-P_0$, directions $P^{(1)}_2-P^{(1)}_0$ */
3443: /* and $P_2-P_0$ (parallel to $\xi$ and $\xi^c$) are conjugate. } */
3444: #ifdef POWELLNOTTRUECONJUGATE
3445: } /* end of t or directest negative */
3446: #endif
3447: #ifdef POWELLNOF3INFF1TEST
3448: #else
3449: } /* end if (fptt < fp) */
3450: #endif
3451: #ifdef POWELLORIGINCONJUGATE
3452: } /* if (t < 0.0) */
3453: #endif
3454: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */
3455: } /*NODIRECTIONCHANGEDUNTILNITER No change in drections until some iterations are done */
3456: #else
3457: #endif
3458: } /* loop iteration */
3459: }
3460:
3461: /**** Prevalence limit (stable or period prevalence) ****************/
3462:
3463: double **prevalim(double **prlim, int nlstate, double x[], double age, double **oldm, double **savm, double ftolpl, int *ncvyear, int ij, int nres)
3464: {
3465: /**< Computes the prevalence limit in each live state at age x and for covariate combination ij . Nicely done
3466: * (and selected quantitative values in nres)
3467: * by left multiplying the unit
3468: * matrix by transitions matrix until convergence is reached with precision ftolpl
3469: * Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I
3470: * Wx is row vector: population in state 1, population in state 2, population dead
3471: * or prevalence in state 1, prevalence in state 2, 0
3472: * newm is the matrix after multiplications, its rows are identical at a factor.
3473: * Inputs are the parameter, age, a tolerance for the prevalence limit ftolpl.
3474: * Output is prlim.
3475: * Initial matrix pimij
3476: */
3477: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
3478: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
3479: /* 0, 0 , 1} */
3480: /*
3481: * and after some iteration: */
3482: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
3483: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
3484: /* 0, 0 , 1} */
3485: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
3486: /* {0.51571254859325999, 0.4842874514067399, */
3487: /* 0.51326036147820708, 0.48673963852179264} */
3488: /* If we start from prlim again, prlim tends to a constant matrix */
3489:
3490: int i, ii,j,k, k1;
3491: double *min, *max, *meandiff, maxmax,sumnew=0.;
3492: /* double **matprod2(); */ /* test */
3493: double **out, cov[NCOVMAX+1], **pmij(); /* **pmmij is a global variable feeded with oldms etc */
3494: double **newm;
3495: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
3496: int ncvloop=0;
3497: int first=0;
3498:
3499: min=vector(1,nlstate);
3500: max=vector(1,nlstate);
3501: meandiff=vector(1,nlstate);
3502:
3503: /* Starting with matrix unity */
3504: for (ii=1;ii<=nlstate+ndeath;ii++)
3505: for (j=1;j<=nlstate+ndeath;j++){
3506: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3507: }
3508:
3509: cov[1]=1.;
3510:
3511: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
3512: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
3513: for(agefin=age-stepm/YEARM; agefin>=age-delaymax; agefin=agefin-stepm/YEARM){
3514: ncvloop++;
3515: newm=savm;
3516: /* Covariates have to be included here again */
3517: cov[2]=agefin;
3518: if(nagesqr==1){
3519: cov[3]= agefin*agefin;
3520: }
3521: /* Model(2) V1 + V2 + V3 + V8 + V7*V8 + V5*V6 + V8*age + V3*age + age*age */
3522: /* total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age */
3523: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
3524: if(Typevar[k1]==1 || Typevar[k1]==3){ /* A product with age */
3525: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
3526: }else{
3527: cov[2+nagesqr+k1]=precov[nres][k1];
3528: }
3529: }/* End of loop on model equation */
3530:
3531: /* Start of old code (replaced by a loop on position in the model equation */
3532: /* for (k=1; k<=nsd;k++) { /\* For single dummy covariates only of the model *\/ */
3533: /* /\* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates *\/ */
3534: /* /\* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,TvarsD[k])]; *\/ */
3535: /* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,TnsdVar[TvarsD[k]])]; */
3536: /* /\* model = 1 +age + V1*V3 + age*V1 + V2 + V1 + age*V2 + V3 + V3*age + V1*V2 */
3537: /* * k 1 2 3 4 5 6 7 8 */
3538: /* *cov[] 1 2 3 4 5 6 7 8 9 10 */
3539: /* *TypeVar[k] 2 1 0 0 1 0 1 2 */
3540: /* *Dummy[k] 0 2 0 0 2 0 2 0 */
3541: /* *Tvar[k] 4 1 2 1 2 3 3 5 */
3542: /* *nsd=3 (1) (2) (3) */
3543: /* *TvarsD[nsd] [1]=2 1 3 */
3544: /* *TnsdVar [2]=2 [1]=1 [3]=3 */
3545: /* *TvarsDind[nsd](=k) [1]=3 [2]=4 [3]=6 */
3546: /* *Tage[] [1]=1 [2]=2 [3]=3 */
3547: /* *Tvard[] [1][1]=1 [2][1]=1 */
3548: /* * [1][2]=3 [2][2]=2 */
3549: /* *Tprod[](=k) [1]=1 [2]=8 */
3550: /* *TvarsDp(=Tvar) [1]=1 [2]=2 [3]=3 [4]=5 */
3551: /* *TvarD (=k) [1]=1 [2]=3 [3]=4 [3]=6 [4]=6 */
3552: /* *TvarsDpType */
3553: /* *si model= 1 + age + V3 + V2*age + V2 + V3*age */
3554: /* * nsd=1 (1) (2) */
3555: /* *TvarsD[nsd] 3 2 */
3556: /* *TnsdVar (3)=1 (2)=2 */
3557: /* *TvarsDind[nsd](=k) [1]=1 [2]=3 */
3558: /* *Tage[] [1]=2 [2]= 3 */
3559: /* *\/ */
3560: /* /\* cov[++k1]=nbcode[TvarsD[k]][codtabm(ij,k)]; *\/ */
3561: /* /\* 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)); *\/ */
3562: /* } */
3563: /* for (k=1; k<=nsq;k++) { /\* For single quantitative varying covariates only of the model *\/ */
3564: /* /\* Here comes the value of quantitative after renumbering k with single quantitative covariates *\/ */
3565: /* /\* Tqresult[nres][result_position]= value of the variable at the result_position in the nres resultline *\/ */
3566: /* /\* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; *\/ */
3567: /* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][resultmodel[nres][k1]] */
3568: /* /\* cov[++k1]=Tqresult[nres][k]; *\/ */
3569: /* /\* 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]); *\/ */
3570: /* } */
3571: /* for (k=1; k<=cptcovage;k++){ /\* For product with age *\/ */
3572: /* if(Dummy[Tage[k]]==2){ /\* dummy with age *\/ */
3573: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
3574: /* /\* cov[++k1]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; *\/ */
3575: /* } else if(Dummy[Tage[k]]==3){ /\* quantitative with age *\/ */
3576: /* cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; */
3577: /* /\* cov[++k1]=Tqresult[nres][k]; *\/ */
3578: /* } */
3579: /* /\* 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]); *\/ */
3580: /* } */
3581: /* for (k=1; k<=cptcovprod;k++){ /\* For product without age *\/ */
3582: /* /\* 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]); *\/ */
3583: /* if(Dummy[Tvard[k][1]]==0){ */
3584: /* if(Dummy[Tvard[k][2]]==0){ */
3585: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
3586: /* /\* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
3587: /* }else{ */
3588: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * Tqresult[nres][k]; */
3589: /* /\* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * Tqresult[nres][k]; *\/ */
3590: /* } */
3591: /* }else{ */
3592: /* if(Dummy[Tvard[k][2]]==0){ */
3593: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])] * Tqinvresult[nres][Tvard[k][1]]; */
3594: /* /\* cov[++k1]=nbcode[Tvard[k][2]][codtabm(ij,k)] * Tqinvresult[nres][Tvard[k][1]]; *\/ */
3595: /* }else{ */
3596: /* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; */
3597: /* /\* cov[++k1]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; *\/ */
3598: /* } */
3599: /* } */
3600: /* } /\* End product without age *\/ */
3601: /* ENd of old code */
3602: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
3603: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
3604: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
3605: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
3606: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
3607: /* age and covariate values of ij are in 'cov' */
3608: out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /* Bug Valgrind */
3609:
3610: savm=oldm;
3611: oldm=newm;
3612:
3613: for(j=1; j<=nlstate; j++){
3614: max[j]=0.;
3615: min[j]=1.;
3616: }
3617: for(i=1;i<=nlstate;i++){
3618: sumnew=0;
3619: for(k=1; k<=ndeath; k++) sumnew+=newm[i][nlstate+k];
3620: for(j=1; j<=nlstate; j++){
3621: prlim[i][j]= newm[i][j]/(1-sumnew);
3622: max[j]=FMAX(max[j],prlim[i][j]);
3623: min[j]=FMIN(min[j],prlim[i][j]);
3624: }
3625: }
3626:
3627: maxmax=0.;
3628: for(j=1; j<=nlstate; j++){
3629: meandiff[j]=(max[j]-min[j])/(max[j]+min[j])*2.; /* mean difference for each column */
3630: maxmax=FMAX(maxmax,meandiff[j]);
3631: /* 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); */
3632: } /* j loop */
3633: *ncvyear= (int)age- (int)agefin;
3634: /* printf("maxmax=%lf maxmin=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, maxmin, ncvloop, (int)age, (int)agefin, *ncvyear); */
3635: if(maxmax < ftolpl){
3636: /* printf("maxmax=%lf ncvloop=%ld, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
3637: free_vector(min,1,nlstate);
3638: free_vector(max,1,nlstate);
3639: free_vector(meandiff,1,nlstate);
3640: return prlim;
3641: }
3642: } /* agefin loop */
3643: /* After some age loop it doesn't converge */
3644: if(!first){
3645: first=1;
3646: printf("Warning: the stable prevalence at age %d did not converge with the required precision (%g > ftolpl=%g) within %.d years and %d loops. Try to lower 'ftolpl'. Youngest age to start was %d=(%d-%d). Others in log file only...\n", (int)age, maxmax, ftolpl, *ncvyear, ncvloop, (int)(agefin+stepm/YEARM), (int)(age-stepm/YEARM), (int)delaymax);
3647: fprintf(ficlog, "Warning: the stable prevalence at age %d did not converge with the required precision (%g > ftolpl=%g) within %.d years and %d loops. Try to lower 'ftolpl'. Youngest age to start was %d=(%d-%d).\n", (int)age, maxmax, ftolpl, *ncvyear, ncvloop, (int)(agefin+stepm/YEARM), (int)(age-stepm/YEARM), (int)delaymax);
3648: }else if (first >=1 && first <10){
3649: fprintf(ficlog, "Warning: the stable prevalence at age %d did not converge with the required precision (%g > ftolpl=%g) within %.d years and %d loops. Try to lower 'ftolpl'. Youngest age to start was %d=(%d-%d).\n", (int)age, maxmax, ftolpl, *ncvyear, ncvloop, (int)(agefin+stepm/YEARM), (int)(age-stepm/YEARM), (int)delaymax);
3650: first++;
3651: }else if (first ==10){
3652: fprintf(ficlog, "Warning: the stable prevalence at age %d did not converge with the required precision (%g > ftolpl=%g) within %.d years and %d loops. Try to lower 'ftolpl'. Youngest age to start was %d=(%d-%d).\n", (int)age, maxmax, ftolpl, *ncvyear, ncvloop, (int)(agefin+stepm/YEARM), (int)(age-stepm/YEARM), (int)delaymax);
3653: printf("Warning: the stable prevalence dit not converge. This warning came too often, IMaCh will stop notifying, even in its log file. Look at the graphs to appreciate the non convergence.\n");
3654: fprintf(ficlog,"Warning: the stable prevalence no convergence; too many cases, giving up noticing, even in log file\n");
3655: first++;
3656: }
3657:
3658: /* 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); */
3659: free_vector(min,1,nlstate);
3660: free_vector(max,1,nlstate);
3661: free_vector(meandiff,1,nlstate);
3662:
3663: return prlim; /* should not reach here */
3664: }
3665:
3666:
3667: /**** Back Prevalence limit (stable or period prevalence) ****************/
3668:
3669: /* 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) */
3670: /* 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) */
3671: double **bprevalim(double **bprlim, double ***prevacurrent, int nlstate, double x[], double age, double ftolpl, int *ncvyear, int ij, int nres)
3672: {
3673: /* Computes the prevalence limit in each live state at age x and for covariate combination ij (<=2**cptcoveff) by left multiplying the unit
3674: matrix by transitions matrix until convergence is reached with precision ftolpl */
3675: /* Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I */
3676: /* Wx is row vector: population in state 1, population in state 2, population dead */
3677: /* or prevalence in state 1, prevalence in state 2, 0 */
3678: /* newm is the matrix after multiplications, its rows are identical at a factor */
3679: /* Initial matrix pimij */
3680: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
3681: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
3682: /* 0, 0 , 1} */
3683: /*
3684: * and after some iteration: */
3685: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
3686: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
3687: /* 0, 0 , 1} */
3688: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
3689: /* {0.51571254859325999, 0.4842874514067399, */
3690: /* 0.51326036147820708, 0.48673963852179264} */
3691: /* If we start from prlim again, prlim tends to a constant matrix */
3692:
3693: int i, ii,j,k, k1;
3694: int first=0;
3695: double *min, *max, *meandiff, maxmax,sumnew=0.;
3696: /* double **matprod2(); */ /* test */
3697: double **out, cov[NCOVMAX+1], **bmij();
3698: double **newm;
3699: double **dnewm, **doldm, **dsavm; /* for use */
3700: double **oldm, **savm; /* for use */
3701:
3702: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
3703: int ncvloop=0;
3704:
3705: min=vector(1,nlstate);
3706: max=vector(1,nlstate);
3707: meandiff=vector(1,nlstate);
3708:
3709: dnewm=ddnewms; doldm=ddoldms; dsavm=ddsavms;
3710: oldm=oldms; savm=savms;
3711:
3712: /* Starting with matrix unity */
3713: for (ii=1;ii<=nlstate+ndeath;ii++)
3714: for (j=1;j<=nlstate+ndeath;j++){
3715: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3716: }
3717:
3718: cov[1]=1.;
3719:
3720: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
3721: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
3722: /* for(agefin=age+stepm/YEARM; agefin<=age+delaymax; agefin=agefin+stepm/YEARM){ /\* A changer en age *\/ */
3723: /* for(agefin=age; agefin<AGESUP; agefin=agefin+stepm/YEARM){ /\* A changer en age *\/ */
3724: for(agefin=age; agefin<FMIN(AGESUP,age+delaymax); agefin=agefin+stepm/YEARM){ /* A changer en age */
3725: ncvloop++;
3726: newm=savm; /* oldm should be kept from previous iteration or unity at start */
3727: /* newm points to the allocated table savm passed by the function it can be written, savm could be reallocated */
3728: /* Covariates have to be included here again */
3729: cov[2]=agefin;
3730: if(nagesqr==1){
3731: cov[3]= agefin*agefin;;
3732: }
3733: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
3734: if(Typevar[k1]==1 || Typevar[k1]==3){ /* A product with age */
3735: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
3736: }else{
3737: cov[2+nagesqr+k1]=precov[nres][k1];
3738: }
3739: }/* End of loop on model equation */
3740:
3741: /* Old code */
3742:
3743: /* for (k=1; k<=nsd;k++) { /\* For single dummy covariates only *\/ */
3744: /* /\* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates *\/ */
3745: /* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,TvarsD[k])]; */
3746: /* /\* printf("bprevalim Dummy agefin=%.0f combi=%d k=%d TvarsD[%d]=V%d TvarsDind[%d]=%d nbcode=%d cov[%d]=%lf codtabm(%d,Tvar[%d])=%d \n",agefin,ij,k, k, TvarsD[k],k,TvarsDind[k],nbcode[TvarsD[k]][codtabm(ij,k)],2+nagesqr+TvarsDind[k],cov[2+nagesqr+TvarsDind[k]], ij, k, codtabm(ij,k)); *\/ */
3747: /* } */
3748: /* /\* for (k=1; k<=cptcovn;k++) { *\/ */
3749: /* /\* /\\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; *\\/ *\/ */
3750: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; *\/ */
3751: /* /\* /\\* 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])]); *\\/ *\/ */
3752: /* /\* } *\/ */
3753: /* for (k=1; k<=nsq;k++) { /\* For single varying covariates only *\/ */
3754: /* /\* Here comes the value of quantitative after renumbering k with single quantitative covariates *\/ */
3755: /* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; */
3756: /* /\* 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]); *\/ */
3757: /* } */
3758: /* /\* for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,k)]*cov[2]; *\/ */
3759: /* /\* for (k=1; k<=cptcovprod;k++) /\\* Useless *\\/ *\/ */
3760: /* /\* /\\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; *\\/ *\/ */
3761: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
3762: /* for (k=1; k<=cptcovage;k++){ /\* For product with age *\/ */
3763: /* /\* if(Dummy[Tvar[Tage[k]]]== 2){ /\\* dummy with age *\\/ ERROR ???*\/ */
3764: /* if(Dummy[Tage[k]]== 2){ /\* dummy with age *\/ */
3765: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
3766: /* } else if(Dummy[Tage[k]]== 3){ /\* quantitative with age *\/ */
3767: /* cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; */
3768: /* } */
3769: /* /\* 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]); *\/ */
3770: /* } */
3771: /* for (k=1; k<=cptcovprod;k++){ /\* For product without age *\/ */
3772: /* /\* 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]); *\/ */
3773: /* if(Dummy[Tvard[k][1]]==0){ */
3774: /* if(Dummy[Tvard[k][2]]==0){ */
3775: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
3776: /* }else{ */
3777: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * Tqresult[nres][k]; */
3778: /* } */
3779: /* }else{ */
3780: /* if(Dummy[Tvard[k][2]]==0){ */
3781: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])] * Tqinvresult[nres][Tvard[k][1]]; */
3782: /* }else{ */
3783: /* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; */
3784: /* } */
3785: /* } */
3786: /* } */
3787:
3788: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
3789: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
3790: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
3791: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
3792: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
3793: /* ij should be linked to the correct index of cov */
3794: /* age and covariate values ij are in 'cov', but we need to pass
3795: * ij for the observed prevalence at age and status and covariate
3796: * number: prevacurrent[(int)agefin][ii][ij]
3797: */
3798: /* 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 *\/ */
3799: /* 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 *\/ */
3800: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij)); /* Bug Valgrind */
3801: /* if((int)age == 86 || (int)age == 87){ */
3802: /* printf(" Backward prevalim age=%d agefin=%d \n", (int) age, (int) agefin); */
3803: /* for(i=1; i<=nlstate+ndeath; i++) { */
3804: /* printf("%d newm= ",i); */
3805: /* for(j=1;j<=nlstate+ndeath;j++) { */
3806: /* printf("%f ",newm[i][j]); */
3807: /* } */
3808: /* printf("oldm * "); */
3809: /* for(j=1;j<=nlstate+ndeath;j++) { */
3810: /* printf("%f ",oldm[i][j]); */
3811: /* } */
3812: /* printf(" bmmij "); */
3813: /* for(j=1;j<=nlstate+ndeath;j++) { */
3814: /* printf("%f ",pmmij[i][j]); */
3815: /* } */
3816: /* printf("\n"); */
3817: /* } */
3818: /* } */
3819: savm=oldm;
3820: oldm=newm;
3821:
3822: for(j=1; j<=nlstate; j++){
3823: max[j]=0.;
3824: min[j]=1.;
3825: }
3826: for(j=1; j<=nlstate; j++){
3827: for(i=1;i<=nlstate;i++){
3828: /* bprlim[i][j]= newm[i][j]/(1-sumnew); */
3829: bprlim[i][j]= newm[i][j];
3830: max[i]=FMAX(max[i],bprlim[i][j]); /* Max in line */
3831: min[i]=FMIN(min[i],bprlim[i][j]);
3832: }
3833: }
3834:
3835: maxmax=0.;
3836: for(i=1; i<=nlstate; i++){
3837: meandiff[i]=(max[i]-min[i])/(max[i]+min[i])*2.; /* mean difference for each column, could be nan! */
3838: maxmax=FMAX(maxmax,meandiff[i]);
3839: /* 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); */
3840: } /* i loop */
3841: *ncvyear= -( (int)age- (int)agefin);
3842: /* printf("Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
3843: if(maxmax < ftolpl){
3844: /* printf("OK Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
3845: free_vector(min,1,nlstate);
3846: free_vector(max,1,nlstate);
3847: free_vector(meandiff,1,nlstate);
3848: return bprlim;
3849: }
3850: } /* agefin loop */
3851: /* After some age loop it doesn't converge */
3852: if(!first){
3853: first=1;
3854: 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'. Others in log file only...\n\
3855: 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);
3856: }
3857: fprintf(ficlog,"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\
3858: 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);
3859: /* 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); */
3860: free_vector(min,1,nlstate);
3861: free_vector(max,1,nlstate);
3862: free_vector(meandiff,1,nlstate);
3863:
3864: return bprlim; /* should not reach here */
3865: }
3866:
3867: /*************** transition probabilities ***************/
3868:
3869: double **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
3870: {
3871: /* According to parameters values stored in x and the covariate's values stored in cov,
3872: computes the probability to be observed in state j (after stepm years) being in state i by appying the
3873: model to the ncovmodel covariates (including constant and age).
3874: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
3875: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
3876: ncth covariate in the global vector x is given by the formula:
3877: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
3878: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
3879: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
3880: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
3881: Outputs ps[i][j] or probability to be observed in j being in i according to
3882: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
3883: Sum on j ps[i][j] should equal to 1.
3884: */
3885: double s1, lnpijopii;
3886: /*double t34;*/
3887: int i,j, nc, ii, jj;
3888:
3889: for(i=1; i<= nlstate; i++){
3890: for(j=1; j<i;j++){
3891: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
3892: /*lnpijopii += param[i][j][nc]*cov[nc];*/
3893: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
3894: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
3895: }
3896: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
3897: /* printf("Debug pmij() i=%d j=%d nc=%d s1=%.17f, lnpijopii=%.17f\n",i,j,nc, s1,lnpijopii); */
3898: }
3899: for(j=i+1; j<=nlstate+ndeath;j++){
3900: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
3901: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
3902: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
3903: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
3904: }
3905: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
3906: /* printf("Debug pmij() i=%d j=%d nc=%d s1=%.17f, lnpijopii=%.17f\n",i,j,nc, s1,lnpijopii); */
3907: }
3908: }
3909:
3910: for(i=1; i<= nlstate; i++){
3911: s1=0;
3912: for(j=1; j<i; j++){
3913: /* printf("debug1 %d %d ps=%lf exp(ps)=%lf \n",i,j,ps[i][j],exp(ps[i][j])); */
3914: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
3915: }
3916: for(j=i+1; j<=nlstate+ndeath; j++){
3917: /* printf("debug2 %d %d ps=%lf exp(ps)=%lf \n",i,j,ps[i][j],exp(ps[i][j])); */
3918: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
3919: }
3920: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
3921: ps[i][i]=1./(s1+1.);
3922: /* Computing other pijs */
3923: for(j=1; j<i; j++)
3924: ps[i][j]= exp(ps[i][j])*ps[i][i];/* Bug valgrind */
3925: for(j=i+1; j<=nlstate+ndeath; j++)
3926: ps[i][j]= exp(ps[i][j])*ps[i][i];
3927: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
3928: } /* end i */
3929:
3930: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
3931: for(jj=1; jj<= nlstate+ndeath; jj++){
3932: ps[ii][jj]=0;
3933: ps[ii][ii]=1;
3934: }
3935: }
3936:
3937:
3938: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
3939: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
3940: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
3941: /* } */
3942: /* printf("\n "); */
3943: /* } */
3944: /* printf("\n ");printf("%lf ",cov[2]);*/
3945: /*
3946: for(i=1; i<= npar; i++) printf("%f ",x[i]);
3947: goto end;*/
3948: return ps; /* Pointer is unchanged since its call */
3949: }
3950:
3951: /*************** backward transition probabilities ***************/
3952:
3953: /* 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 ) */
3954: /* double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, double ***dnewm, double **doldm, double **dsavm, int ij ) */
3955: double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, int ij )
3956: {
3957: /* Computes the backward probability at age agefin, cov[2], and covariate combination 'ij'. In fact cov is already filled and x too.
3958: * Call to pmij(cov and x), call to cross prevalence, sums and inverses, left multiply, and returns in **ps as well as **bmij.
3959: */
3960: int i, ii, j,k;
3961:
3962: double **out, **pmij();
3963: double sumnew=0.;
3964: double agefin;
3965: double k3=0.; /* constant of the w_x diagonal matrix (in order for B to sum to 1 even for death state) */
3966: double **dnewm, **dsavm, **doldm;
3967: double **bbmij;
3968:
3969: doldm=ddoldms; /* global pointers */
3970: dnewm=ddnewms;
3971: dsavm=ddsavms;
3972:
3973: /* Debug */
3974: /* printf("Bmij ij=%d, cov[2}=%f\n", ij, cov[2]); */
3975: agefin=cov[2];
3976: /* Bx = Diag(w_x) P_x Diag(Sum_i w^i_x p^ij_x */
3977: /* bmij *//* age is cov[2], ij is included in cov, but we need for
3978: the observed prevalence (with this covariate ij) at beginning of transition */
3979: /* dsavm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
3980:
3981: /* P_x */
3982: pmmij=pmij(pmmij,cov,ncovmodel,x,nlstate); /*This is forward probability from agefin to agefin + stepm *//* Bug valgrind */
3983: /* outputs pmmij which is a stochastic matrix in row */
3984:
3985: /* Diag(w_x) */
3986: /* Rescaling the cross-sectional prevalence: Problem with prevacurrent which can be zero */
3987: sumnew=0.;
3988: /*for (ii=1;ii<=nlstate+ndeath;ii++){*/
3989: for (ii=1;ii<=nlstate;ii++){ /* Only on live states */
3990: /* printf(" agefin=%d, ii=%d, ij=%d, prev=%f\n",(int)agefin,ii, ij, prevacurrent[(int)agefin][ii][ij]); */
3991: sumnew+=prevacurrent[(int)agefin][ii][ij];
3992: }
3993: if(sumnew >0.01){ /* At least some value in the prevalence */
3994: for (ii=1;ii<=nlstate+ndeath;ii++){
3995: for (j=1;j<=nlstate+ndeath;j++)
3996: doldm[ii][j]=(ii==j ? prevacurrent[(int)agefin][ii][ij]/sumnew : 0.0);
3997: }
3998: }else{
3999: for (ii=1;ii<=nlstate+ndeath;ii++){
4000: for (j=1;j<=nlstate+ndeath;j++)
4001: doldm[ii][j]=(ii==j ? 1./nlstate : 0.0);
4002: }
4003: /* if(sumnew <0.9){ */
4004: /* printf("Problem internal bmij B: sum on i wi <0.9: j=%d, sum_i wi=%lf,agefin=%d\n",j,sumnew, (int)agefin); */
4005: /* } */
4006: }
4007: k3=0.0; /* We put the last diagonal to 0 */
4008: for (ii=nlstate+1;ii<=nlstate+ndeath;ii++){
4009: doldm[ii][ii]= k3;
4010: }
4011: /* End doldm, At the end doldm is diag[(w_i)] */
4012:
4013: /* Left product of this diag matrix by pmmij=Px (dnewm=dsavm*doldm): diag[(w_i)*Px */
4014: bbmij=matprod2(dnewm, doldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, pmmij); /* was a Bug Valgrind */
4015:
4016: /* Diag(Sum_i w^i_x p^ij_x, should be the prevalence at age x+stepm */
4017: /* w1 p11 + w2 p21 only on live states N1./N..*N11/N1. + N2./N..*N21/N2.=(N11+N21)/N..=N.1/N.. */
4018: for (j=1;j<=nlstate+ndeath;j++){
4019: sumnew=0.;
4020: for (ii=1;ii<=nlstate;ii++){
4021: /* sumnew+=dsavm[ii][j]*prevacurrent[(int)agefin][ii][ij]; */
4022: sumnew+=pmmij[ii][j]*doldm[ii][ii]; /* Yes prevalence at beginning of transition */
4023: } /* sumnew is (N11+N21)/N..= N.1/N.. = sum on i of w_i pij */
4024: for (ii=1;ii<=nlstate+ndeath;ii++){
4025: /* if(agefin >= agemaxpar && agefin <= agemaxpar+stepm/YEARM){ */
4026: /* dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
4027: /* }else if(agefin >= agemaxpar+stepm/YEARM){ */
4028: /* dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
4029: /* }else */
4030: dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0);
4031: } /*End ii */
4032: } /* End j, At the end dsavm is diag[1/(w_1p1i+w_2 p2i)] for ALL states even if the sum is only for live states */
4033:
4034: ps=matprod2(ps, dnewm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, dsavm); /* was a Bug Valgrind */
4035: /* ps is now diag[w_i] * Px * diag [1/(w_1p1i+w_2 p2i)] */
4036: /* end bmij */
4037: return ps; /*pointer is unchanged */
4038: }
4039: /*************** transition probabilities ***************/
4040:
4041: double **bpmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
4042: {
4043: /* According to parameters values stored in x and the covariate's values stored in cov,
4044: computes the probability to be observed in state j being in state i by appying the
4045: model to the ncovmodel covariates (including constant and age).
4046: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
4047: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
4048: ncth covariate in the global vector x is given by the formula:
4049: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
4050: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
4051: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
4052: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
4053: Outputs ps[i][j] the probability to be observed in j being in j according to
4054: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
4055: */
4056: double s1, lnpijopii;
4057: /*double t34;*/
4058: int i,j, nc, ii, jj;
4059:
4060: for(i=1; i<= nlstate; i++){
4061: for(j=1; j<i;j++){
4062: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
4063: /*lnpijopii += param[i][j][nc]*cov[nc];*/
4064: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
4065: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
4066: }
4067: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
4068: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
4069: }
4070: for(j=i+1; j<=nlstate+ndeath;j++){
4071: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
4072: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
4073: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
4074: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
4075: }
4076: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
4077: }
4078: }
4079:
4080: for(i=1; i<= nlstate; i++){
4081: s1=0;
4082: for(j=1; j<i; j++){
4083: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
4084: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
4085: }
4086: for(j=i+1; j<=nlstate+ndeath; j++){
4087: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
4088: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
4089: }
4090: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
4091: ps[i][i]=1./(s1+1.);
4092: /* Computing other pijs */
4093: for(j=1; j<i; j++)
4094: ps[i][j]= exp(ps[i][j])*ps[i][i];
4095: for(j=i+1; j<=nlstate+ndeath; j++)
4096: ps[i][j]= exp(ps[i][j])*ps[i][i];
4097: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
4098: } /* end i */
4099:
4100: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
4101: for(jj=1; jj<= nlstate+ndeath; jj++){
4102: ps[ii][jj]=0;
4103: ps[ii][ii]=1;
4104: }
4105: }
4106: /* Added for prevbcast */ /* Transposed matrix too */
4107: for(jj=1; jj<= nlstate+ndeath; jj++){
4108: s1=0.;
4109: for(ii=1; ii<= nlstate+ndeath; ii++){
4110: s1+=ps[ii][jj];
4111: }
4112: for(ii=1; ii<= nlstate; ii++){
4113: ps[ii][jj]=ps[ii][jj]/s1;
4114: }
4115: }
4116: /* Transposition */
4117: for(jj=1; jj<= nlstate+ndeath; jj++){
4118: for(ii=jj; ii<= nlstate+ndeath; ii++){
4119: s1=ps[ii][jj];
4120: ps[ii][jj]=ps[jj][ii];
4121: ps[jj][ii]=s1;
4122: }
4123: }
4124: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
4125: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
4126: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
4127: /* } */
4128: /* printf("\n "); */
4129: /* } */
4130: /* printf("\n ");printf("%lf ",cov[2]);*/
4131: /*
4132: for(i=1; i<= npar; i++) printf("%f ",x[i]);
4133: goto end;*/
4134: return ps;
4135: }
4136:
4137:
4138: /**************** Product of 2 matrices ******************/
4139:
4140: double **matprod2(double **out, double **in,int nrl, int nrh, int ncl, int nch, int ncolol, int ncoloh, double **b)
4141: {
4142: /* Computes the matrix product of in(1,nrh-nrl+1)(1,nch-ncl+1) times
4143: b(1,nch-ncl+1)(1,ncoloh-ncolol+1) into out(...) */
4144: /* in, b, out are matrice of pointers which should have been initialized
4145: before: only the contents of out is modified. The function returns
4146: a pointer to pointers identical to out */
4147: int i, j, k;
4148: for(i=nrl; i<= nrh; i++)
4149: for(k=ncolol; k<=ncoloh; k++){
4150: out[i][k]=0.;
4151: for(j=ncl; j<=nch; j++)
4152: out[i][k] +=in[i][j]*b[j][k];
4153: }
4154: return out;
4155: }
4156:
4157:
4158: /************* Higher Matrix Product ***************/
4159:
4160: double ***hpxij(double ***po, int nhstepm, double age, int hstepm, double *x, int nlstate, int stepm, double **oldm, double **savm, int ij, int nres )
4161: {
4162: /* Already optimized with precov.
4163: Computes the transition matrix starting at age 'age' and dummies values in each resultline (loop on ij to find the corresponding combination) to over
4164: 'nhstepm*hstepm*stepm' months (i.e. until
4165: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
4166: nhstepm*hstepm matrices.
4167: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
4168: (typically every 2 years instead of every month which is too big
4169: for the memory).
4170: Model is determined by parameters x and covariates have to be
4171: included manually here.
4172:
4173: */
4174:
4175: int i, j, d, h, k, k1;
4176: double **out, cov[NCOVMAX+1];
4177: double **newm;
4178: double agexact;
4179: double agebegin, ageend;
4180:
4181: /* Hstepm could be zero and should return the unit matrix */
4182: for (i=1;i<=nlstate+ndeath;i++)
4183: for (j=1;j<=nlstate+ndeath;j++){
4184: oldm[i][j]=(i==j ? 1.0 : 0.0);
4185: po[i][j][0]=(i==j ? 1.0 : 0.0);
4186: }
4187: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
4188: for(h=1; h <=nhstepm; h++){
4189: for(d=1; d <=hstepm; d++){
4190: newm=savm;
4191: /* Covariates have to be included here again */
4192: cov[1]=1.;
4193: agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /* age just before transition */
4194: cov[2]=agexact;
4195: if(nagesqr==1){
4196: cov[3]= agexact*agexact;
4197: }
4198: /* Model(2) V1 + V2 + V3 + V8 + V7*V8 + V5*V6 + V8*age + V3*age + age*age */
4199: /* total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age */
4200: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
4201: if(Typevar[k1]==1 || Typevar[k1]==3){ /* A product with age */
4202: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
4203: }else{
4204: cov[2+nagesqr+k1]=precov[nres][k1];
4205: }
4206: }/* End of loop on model equation */
4207: /* Old code */
4208: /* if( Dummy[k1]==0 && Typevar[k1]==0 ){ /\* Single dummy *\/ */
4209: /* /\* V(Tvarsel)=Tvalsel=Tresult[nres][pos](value); V(Tvresult[nres][pos] (variable): V(variable)=value) *\/ */
4210: /* /\* for (k=1; k<=nsd;k++) { /\\* For single dummy covariates only *\\/ *\/ */
4211: /* /\* /\\* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates *\\/ *\/ */
4212: /* /\* codtabm(ij,k) (1 & (ij-1) >> (k-1))+1 *\/ */
4213: /* /\* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 *\/ */
4214: /* /\* k 1 2 3 4 5 6 7 8 9 *\/ */
4215: /* /\*Tvar[k]= 5 4 3 6 5 2 7 1 1 *\/ */
4216: /* /\* nsd 1 2 3 *\/ /\* Counting single dummies covar fixed or tv *\/ */
4217: /* /\*TvarsD[nsd] 4 3 1 *\/ /\* ID of single dummy cova fixed or timevary*\/ */
4218: /* /\*TvarsDind[k] 2 3 9 *\/ /\* position K of single dummy cova *\/ */
4219: /* /\* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)];or [codtabm(ij,TnsdVar[TvarsD[k]] *\/ */
4220: /* cov[2+nagesqr+k1]=Tresult[nres][resultmodel[nres][k1]]; */
4221: /* /\* printf("hpxij Dummy combi=%d k=%d TvarsD[%d]=V%d TvarsDind[%d]=%d nbcode=%d cov=%lf codtabm(%d,TnsdVar[TvarsD[%d])=%d \n",ij,k, k, TvarsD[k],k,TvarsDind[k],nbcode[TvarsD[k]][codtabm(ij,TnsdVar[TvarsD[k]])],cov[2+nagesqr+TvarsDind[k]], ij, k, codtabm(ij,TnsdVar[TvarsD[k]])); *\/ */
4222: /* printf("hpxij Dummy combi=%d k1=%d Tvar[%d]=V%d cov[2+%d+%d]=%lf resultmodel[nres][%d]=%d nres/nresult=%d/%d \n",ij,k1,k1, Tvar[k1],nagesqr,k1,cov[2+nagesqr+k1],k1,resultmodel[nres][k1],nres,nresult); */
4223: /* printf("hpxij new Dummy precov[nres=%d][k1=%d]=%.4f\n", nres, k1, precov[nres][k1]); */
4224: /* }else if( Dummy[k1]==1 && Typevar[k1]==0 ){ /\* Single quantitative variables *\/ */
4225: /* /\* resultmodel[nres][k1]=k3: k1th position in the model correspond to the k3 position in the resultline *\/ */
4226: /* cov[2+nagesqr+k1]=Tqresult[nres][resultmodel[nres][k1]]; */
4227: /* /\* for (k=1; k<=nsq;k++) { /\\* For single varying covariates only *\\/ *\/ */
4228: /* /\* /\\* Here comes the value of quantitative after renumbering k with single quantitative covariates *\\/ *\/ */
4229: /* /\* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; *\/ */
4230: /* printf("hPxij Quantitative k1=%d resultmodel[nres][%d]=%d,Tqresult[%d][%d]=%f\n",k1,k1,resultmodel[nres][k1],nres,resultmodel[nres][k1],Tqresult[nres][resultmodel[nres][k1]]); */
4231: /* printf("hpxij new Quanti precov[nres=%d][k1=%d]=%.4f\n", nres, k1, precov[nres][k1]); */
4232: /* }else if( Dummy[k1]==2 ){ /\* For dummy with age product *\/ */
4233: /* /\* Tvar[k1] Variable in the age product age*V1 is 1 *\/ */
4234: /* /\* [Tinvresult[nres][V1] is its value in the resultline nres *\/ */
4235: /* cov[2+nagesqr+k1]=TinvDoQresult[nres][Tvar[k1]]*cov[2]; */
4236: /* printf("DhPxij Dummy with age k1=%d Tvar[%d]=%d TinvDoQresult[nres=%d][%d]=%.f age=%.2f,cov[2+%d+%d]=%.3f\n",k1,k1,Tvar[k1],nres,TinvDoQresult[nres][Tvar[k1]],cov[2],nagesqr,k1,cov[2+nagesqr+k1]); */
4237: /* printf("hpxij new Dummy with age product precov[nres=%d][k1=%d]=%.4f * age=%.2f\n", nres, k1, precov[nres][k1], cov[2]); */
4238:
4239: /* /\* cov[2+nagesqr+k1]=Tresult[nres][resultmodel[nres][k1]]; *\/ */
4240: /* /\* for (k=1; k<=cptcovage;k++){ /\\* For product with age V1+V1*age +V4 +age*V3 *\\/ *\/ */
4241: /* /\* 1+2 Tage[1]=2 TVar[2]=1 Dummy[2]=2, Tage[2]=4 TVar[4]=3 Dummy[4]=3 quant*\/ */
4242: /* /\* *\/ */
4243: /* /\* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 *\/ */
4244: /* /\* k 1 2 3 4 5 6 7 8 9 *\/ */
4245: /* /\*Tvar[k]= 5 4 3 6 5 2 7 1 1 *\/ */
4246: /* /\*cptcovage=2 1 2 *\/ */
4247: /* /\*Tage[k]= 5 8 *\/ */
4248: /* }else if( Dummy[k1]==3 ){ /\* For quant with age product *\/ */
4249: /* cov[2+nagesqr+k1]=Tresult[nres][resultmodel[nres][k1]]; */
4250: /* printf("QhPxij Quant with age k1=%d resultmodel[nres][%d]=%d,Tqresult[%d][%d]=%f\n",k1,k1,resultmodel[nres][k1],nres,resultmodel[nres][k1],Tqresult[nres][resultmodel[nres][k1]]); */
4251: /* printf("hpxij new Quanti with age product precov[nres=%d][k1=%d] * age=%.2f\n", nres, k1, precov[nres][k1], cov[2]); */
4252: /* /\* if(Dummy[Tage[k]]== 2){ /\\* dummy with age *\\/ *\/ */
4253: /* /\* /\\* if(Dummy[Tvar[Tage[k]]]== 2){ /\\\* dummy with age *\\\/ *\\/ *\/ */
4254: /* /\* /\\* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; *\\/ *\/ */
4255: /* /\* /\\* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,TnsdVar[TvarsD[Tvar[Tage[k]]]])]*cov[2]; *\\/ *\/ */
4256: /* /\* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,TnsdVar[TvarsD[Tvar[Tage[k]]]])]*cov[2]; *\/ */
4257: /* /\* printf("hPxij Age combi=%d k=%d cptcovage=%d Tage[%d]=%d Tvar[Tage[%d]]=V%d nbcode[Tvar[Tage[k]]][codtabm(ij,TnsdVar[Tvar[Tage[k]]]])]=%d nres=%d\n",ij,k,cptcovage,k,Tage[k],k,Tvar[Tage[k]], nbcode[Tvar[Tage[k]]][codtabm(ij,TnsdVar[Tvar[Tage[k]]])],nres); *\/ */
4258: /* /\* } else if(Dummy[Tage[k]]== 3){ /\\* quantitative with age *\\/ *\/ */
4259: /* /\* cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; *\/ */
4260: /* /\* } *\/ */
4261: /* /\* 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]); *\/ */
4262: /* }else if(Typevar[k1]==2 ){ /\* For product (not with age) *\/ */
4263: /* /\* for (k=1; k<=cptcovprod;k++){ /\\* For product without age *\\/ *\/ */
4264: /* /\* /\\* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 *\\/ *\/ */
4265: /* /\* /\\* k 1 2 3 4 5 6 7 8 9 *\\/ *\/ */
4266: /* /\* /\\*Tvar[k]= 5 4 3 6 5 2 7 1 1 *\\/ *\/ */
4267: /* /\* /\\*cptcovprod=1 1 2 *\\/ *\/ */
4268: /* /\* /\\*Tprod[]= 4 7 *\\/ *\/ */
4269: /* /\* /\\*Tvard[][1] 4 1 *\\/ *\/ */
4270: /* /\* /\\*Tvard[][2] 3 2 *\\/ *\/ */
4271:
4272: /* /\* printf("hPxij Prod ij=%d k=%d Tprod[%d]=%d Tvard[%d][1]=V%d, Tvard[%d][2]=V%d nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])]=%d nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][1])]=%d\n",ij,k,k,Tprod[k], k,Tvard[k][1], k,Tvard[k][2],nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])],nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]); *\/ */
4273: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
4274: /* cov[2+nagesqr+k1]=TinvDoQresult[nres][Tvardk[k1][1]] * TinvDoQresult[nres][Tvardk[k1][2]]; */
4275: /* printf("hPxij Prod ij=%d k1=%d cov[2+%d+%d]=%.5f Tvard[%d][1]=V%d * Tvard[%d][2]=V%d ; TinvDoQresult[nres][Tvardk[k1][1]]=%.4f * TinvDoQresult[nres][Tvardk[k1][1]]=%.4f\n",ij,k1,nagesqr,k1,cov[2+nagesqr+k1],k1,Tvardk[k1][1], k1,Tvardk[k1][2], TinvDoQresult[nres][Tvardk[k1][1]], TinvDoQresult[nres][Tvardk[k1][2]]); */
4276: /* printf("hpxij new Product no age product precov[nres=%d][k1=%d]=%.4f\n", nres, k1, precov[nres][k1]); */
4277:
4278: /* /\* if(Dummy[Tvardk[k1][1]]==0){ *\/ */
4279: /* /\* if(Dummy[Tvardk[k1][2]]==0){ /\\* Product of dummies *\\/ *\/ */
4280: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
4281: /* /\* cov[2+nagesqr+k1]=Tinvresult[nres][Tvardk[k1][1]] * Tinvresult[nres][Tvardk[k1][2]]; *\/ */
4282: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,TnsdVar[Tvard[k][1]])] * nbcode[Tvard[k][2]][codtabm(ij,TnsdVar[Tvard[k][2]])]; *\/ */
4283: /* /\* }else{ /\\* Product of dummy by quantitative *\\/ *\/ */
4284: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,TnsdVar[Tvard[k][1]])] * Tqresult[nres][k]; *\/ */
4285: /* /\* cov[2+nagesqr+k1]=Tresult[nres][Tinvresult[nres][Tvardk[k1][1]]] * Tqresult[nres][Tinvresult[nres][Tvardk[k1][2]]]; *\/ */
4286: /* /\* } *\/ */
4287: /* /\* }else{ /\\* Product of quantitative by...*\\/ *\/ */
4288: /* /\* if(Dummy[Tvard[k][2]]==0){ /\\* quant by dummy *\\/ *\/ */
4289: /* /\* /\\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,TnsdVar[Tvard[k][2]])] * Tqinvresult[nres][Tvard[k][1]]; *\\/ *\/ */
4290: /* /\* cov[2+nagesqr+k1]=Tqresult[nres][Tinvresult[nres][Tvardk[k1][1]]] * Tresult[nres][Tinvresult[nres][Tvardk[k1][2]]] ; *\/ */
4291: /* /\* }else{ /\\* Product of two quant *\\/ *\/ */
4292: /* /\* /\\* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; *\\/ *\/ */
4293: /* /\* cov[2+nagesqr+k1]=Tqresult[nres][Tinvresult[nres][Tvardk[k1][1]]] * Tqresult[nres][Tinvresult[nres][Tvardk[k1][2]]] ; *\/ */
4294: /* /\* } *\/ */
4295: /* /\* }/\\*end of products quantitative *\\/ *\/ */
4296: /* }/\*end of products *\/ */
4297: /* } /\* End of loop on model equation *\/ */
4298: /* for (k=1; k<=cptcovn;k++) */
4299: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; */
4300: /* for (k=1; k<=cptcovage;k++) /\* Should start at cptcovn+1 *\/ */
4301: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; */
4302: /* for (k=1; k<=cptcovprod;k++) /\* Useless because included in cptcovn *\/ */
4303: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)]; */
4304:
4305:
4306: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
4307: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
4308: /* right multiplication of oldm by the current matrix */
4309: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
4310: pmij(pmmij,cov,ncovmodel,x,nlstate));
4311: /* if((int)age == 70){ */
4312: /* printf(" Forward hpxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
4313: /* for(i=1; i<=nlstate+ndeath; i++) { */
4314: /* printf("%d pmmij ",i); */
4315: /* for(j=1;j<=nlstate+ndeath;j++) { */
4316: /* printf("%f ",pmmij[i][j]); */
4317: /* } */
4318: /* printf(" oldm "); */
4319: /* for(j=1;j<=nlstate+ndeath;j++) { */
4320: /* printf("%f ",oldm[i][j]); */
4321: /* } */
4322: /* printf("\n"); */
4323: /* } */
4324: /* } */
4325: savm=oldm;
4326: oldm=newm;
4327: }
4328: for(i=1; i<=nlstate+ndeath; i++)
4329: for(j=1;j<=nlstate+ndeath;j++) {
4330: po[i][j][h]=newm[i][j];
4331: /*if(h==nhstepm) printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]);*/
4332: }
4333: /*printf("h=%d ",h);*/
4334: } /* end h */
4335: /* printf("\n H=%d \n",h); */
4336: return po;
4337: }
4338:
4339: /************* Higher Back Matrix Product ***************/
4340: /* 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 ) */
4341: double ***hbxij(double ***po, int nhstepm, double age, int hstepm, double *x, double ***prevacurrent, int nlstate, int stepm, int ij, int nres )
4342: {
4343: /* For dummy covariates given in each resultline (for historical, computes the corresponding combination ij),
4344: computes the transition matrix starting at age 'age' over
4345: 'nhstepm*hstepm*stepm' months (i.e. until
4346: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
4347: nhstepm*hstepm matrices.
4348: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
4349: (typically every 2 years instead of every month which is too big
4350: for the memory).
4351: Model is determined by parameters x and covariates have to be
4352: included manually here. Then we use a call to bmij(x and cov)
4353: The addresss of po (p3mat allocated to the dimension of nhstepm) should be stored for output
4354: */
4355:
4356: int i, j, d, h, k, k1;
4357: double **out, cov[NCOVMAX+1], **bmij();
4358: double **newm, ***newmm;
4359: double agexact;
4360: double agebegin, ageend;
4361: double **oldm, **savm;
4362:
4363: newmm=po; /* To be saved */
4364: oldm=oldms;savm=savms; /* Global pointers */
4365: /* Hstepm could be zero and should return the unit matrix */
4366: for (i=1;i<=nlstate+ndeath;i++)
4367: for (j=1;j<=nlstate+ndeath;j++){
4368: oldm[i][j]=(i==j ? 1.0 : 0.0);
4369: po[i][j][0]=(i==j ? 1.0 : 0.0);
4370: }
4371: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
4372: for(h=1; h <=nhstepm; h++){
4373: for(d=1; d <=hstepm; d++){
4374: newm=savm;
4375: /* Covariates have to be included here again */
4376: cov[1]=1.;
4377: agexact=age-( (h-1)*hstepm + (d) )*stepm/YEARM; /* age just before transition, d or d-1? */
4378: /* agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /\* age just before transition *\/ */
4379: /* Debug */
4380: /* printf("hBxij age=%lf, agexact=%lf\n", age, agexact); */
4381: cov[2]=agexact;
4382: if(nagesqr==1){
4383: cov[3]= agexact*agexact;
4384: }
4385: /** New code */
4386: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
4387: if(Typevar[k1]==1 || Typevar[k1]==3){ /* A product with age */
4388: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
4389: }else{
4390: cov[2+nagesqr+k1]=precov[nres][k1];
4391: }
4392: }/* End of loop on model equation */
4393: /** End of new code */
4394: /** This was old code */
4395: /* for (k=1; k<=nsd;k++){ /\* For single dummy covariates only *\//\* cptcovn error *\/ */
4396: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; *\/ */
4397: /* /\* /\\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; *\\/ *\/ */
4398: /* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,TvarsD[k])];/\* Bug valgrind *\/ */
4399: /* /\* printf("hbxij Dummy agexact=%.0f combi=%d k=%d TvarsD[%d]=V%d TvarsDind[%d]=%d nbcode=%d cov[%d]=%lf codtabm(%d,Tvar[%d])=%d \n",agexact,ij,k, k, TvarsD[k],k,TvarsDind[k],nbcode[TvarsD[k]][codtabm(ij,k)],2+nagesqr+TvarsDind[k],cov[2+nagesqr+TvarsDind[k]], ij, k, codtabm(ij,k)); *\/ */
4400: /* } */
4401: /* for (k=1; k<=nsq;k++) { /\* For single varying covariates only *\/ */
4402: /* /\* Here comes the value of quantitative after renumbering k with single quantitative covariates *\/ */
4403: /* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; */
4404: /* /\* 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]); *\/ */
4405: /* } */
4406: /* for (k=1; k<=cptcovage;k++){ /\* Should start at cptcovn+1 *\//\* For product with age *\/ */
4407: /* /\* if(Dummy[Tvar[Tage[k]]]== 2){ /\\* dummy with age error!!!*\\/ *\/ */
4408: /* if(Dummy[Tage[k]]== 2){ /\* dummy with age *\/ */
4409: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
4410: /* } else if(Dummy[Tage[k]]== 3){ /\* quantitative with age *\/ */
4411: /* cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; */
4412: /* } */
4413: /* /\* printf("hBxij Age combi=%d k=%d Tage[%d]=V%d Tqresult[%d][%d]=%f\n",ij,k,k,Tage[k],nres,k,Tqresult[nres][k]); *\/ */
4414: /* } */
4415: /* for (k=1; k<=cptcovprod;k++){ /\* Useless because included in cptcovn *\/ */
4416: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])]*nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
4417: /* if(Dummy[Tvard[k][1]]==0){ */
4418: /* if(Dummy[Tvard[k][2]]==0){ */
4419: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][1])]; */
4420: /* }else{ */
4421: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * Tqresult[nres][k]; */
4422: /* } */
4423: /* }else{ */
4424: /* if(Dummy[Tvard[k][2]]==0){ */
4425: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])] * Tqinvresult[nres][Tvard[k][1]]; */
4426: /* }else{ */
4427: /* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; */
4428: /* } */
4429: /* } */
4430: /* } */
4431: /* /\*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*\/ */
4432: /* /\*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*\/ */
4433: /** End of old code */
4434:
4435: /* Careful transposed matrix */
4436: /* age is in cov[2], prevacurrent at beginning of transition. */
4437: /* out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm, dsavm,ij),\ */
4438: /* 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); */
4439: out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij),\
4440: 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm);/* Bug valgrind */
4441: /* if((int)age == 70){ */
4442: /* printf(" Backward hbxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
4443: /* for(i=1; i<=nlstate+ndeath; i++) { */
4444: /* printf("%d pmmij ",i); */
4445: /* for(j=1;j<=nlstate+ndeath;j++) { */
4446: /* printf("%f ",pmmij[i][j]); */
4447: /* } */
4448: /* printf(" oldm "); */
4449: /* for(j=1;j<=nlstate+ndeath;j++) { */
4450: /* printf("%f ",oldm[i][j]); */
4451: /* } */
4452: /* printf("\n"); */
4453: /* } */
4454: /* } */
4455: savm=oldm;
4456: oldm=newm;
4457: }
4458: for(i=1; i<=nlstate+ndeath; i++)
4459: for(j=1;j<=nlstate+ndeath;j++) {
4460: po[i][j][h]=newm[i][j];
4461: /* if(h==nhstepm) */
4462: /* printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]); */
4463: }
4464: /* printf("h=%d %.1f ",h, agexact); */
4465: } /* end h */
4466: /* printf("\n H=%d nhs=%d \n",h, nhstepm); */
4467: return po;
4468: }
4469:
4470:
4471: #ifdef NLOPT
4472: double myfunc(unsigned n, const double *p1, double *grad, void *pd){
4473: double fret;
4474: double *xt;
4475: int j;
4476: myfunc_data *d2 = (myfunc_data *) pd;
4477: /* xt = (p1-1); */
4478: xt=vector(1,n);
4479: for (j=1;j<=n;j++) xt[j]=p1[j-1]; /* xt[1]=p1[0] */
4480:
4481: fret=(d2->function)(xt); /* p xt[1]@8 is fine */
4482: /* fret=(*func)(xt); /\* p xt[1]@8 is fine *\/ */
4483: printf("Function = %.12lf ",fret);
4484: for (j=1;j<=n;j++) printf(" %d %.8lf", j, xt[j]);
4485: printf("\n");
4486: free_vector(xt,1,n);
4487: return fret;
4488: }
4489: #endif
4490:
4491: /*************** log-likelihood *************/
4492: double func( double *x)
4493: {
4494: int i, ii, j, k, mi, d, kk, kf=0;
4495: int ioffset=0;
4496: int ipos=0,iposold=0,ncovv=0;
4497:
4498: double cotvarv, cotvarvold;
4499: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
4500: double **out;
4501: double lli; /* Individual log likelihood */
4502: int s1, s2;
4503: int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate, quantitative time varying covariate */
4504:
4505: double bbh, survp;
4506: double agexact;
4507: double agebegin, ageend;
4508: /*extern weight */
4509: /* We are differentiating ll according to initial status */
4510: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
4511: /*for(i=1;i<imx;i++)
4512: printf(" %d\n",s[4][i]);
4513: */
4514:
4515: ++countcallfunc;
4516:
4517: cov[1]=1.;
4518:
4519: for(k=1; k<=nlstate; k++) ll[k]=0.;
4520: ioffset=0;
4521: if(mle==1){
4522: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
4523: /* Computes the values of the ncovmodel covariates of the model
4524: depending if the covariates are fixed or varying (age dependent) and stores them in cov[]
4525: Then computes with function pmij which return a matrix p[i][j] giving the elementary probability
4526: to be observed in j being in i according to the model.
4527: */
4528: ioffset=2+nagesqr ;
4529: /* Fixed */
4530: for (kf=1; kf<=ncovf;kf++){ /* For each fixed covariate dummy or quant or prod */
4531: /* # V1=sex, V2=raedyrs Quant Fixed, State=livarnb4..livarnb11, V3=iadl4..iald11, V4=adlw4..adlw11, V5=r4bmi..r11bmi */
4532: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
4533: /* TvarF[1]=Tvar[6]=2, TvarF[2]=Tvar[7]=7, TvarF[3]=Tvar[9]=1 ID of fixed covariates or product V2, V1*V2, V1 */
4534: /* TvarFind; TvarFind[1]=6, TvarFind[2]=7, TvarFind[3]=9 *//* Inverse V2(6) is first fixed (single or prod) */
4535: cov[ioffset+TvarFind[kf]]=covar[Tvar[TvarFind[kf]]][i];/* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, only V1 is fixed (TvarFind[1]=6)*/
4536: /* V1*V2 (7) TvarFind[2]=7, TvarFind[3]=9 */
4537: }
4538: /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4]
4539: is 5, Tvar[3=age*V3] should not be computed because of age Tvar[4=V3*V2]=6
4540: has been calculated etc */
4541: /* For an individual i, wav[i] gives the number of effective waves */
4542: /* We compute the contribution to Likelihood of each effective transition
4543: mw[mi][i] is real wave of the mi th effectve wave */
4544: /* Then statuses are computed at each begin and end of an effective wave s1=s[ mw[mi][i] ][i];
4545: s2=s[mw[mi+1][i]][i];
4546: And the iv th varying covariate is the cotvar[mw[mi+1][i]][iv][i] because now is moved after nvocol+nqv
4547: But if the variable is not in the model TTvar[iv] is the real variable effective in the model:
4548: meaning that decodemodel should be used cotvar[mw[mi+1][i]][TTvar[iv]][i]
4549: */
4550: for(mi=1; mi<= wav[i]-1; mi++){ /* Varying with waves */
4551: /* Wave varying (but not age varying) */
4552: /* for(k=1; k <= ncovv ; k++){ /\* Varying covariates in the model (single and product but no age )"V5+V4+V3+V4*V3+V5*age+V1*age+V1" +TvarVind 1,2,3,4(V4*V3) Tvar[1]@7{5, 4, 3, 6, 5, 1, 1 ; 6 because the created covar is after V5 and is 6, minus 1+1, 3,2,1,4 positions in cotvar*\/ */
4553: /* /\* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; but where is the crossproduct? *\/ */
4554: /* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]-ncovcol-nqv][i]; */
4555: /* } */
4556: for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){ /* Varying covariates (single and product but no age )*/
4557: itv=TvarVV[ncovv]; /* TvarVV={3, 1, 3} gives the name of each varying covariate */
4558: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
4559: if(FixedV[itv]!=0){ /* Not a fixed covariate */
4560: cotvarv=cotvar[mw[mi][i]][TvarVV[ncovv]][i]; /* cotvar[wav][ncovcol+nqv+iv][i] */
4561: }else{ /* fixed covariate */
4562: cotvarv=covar[itv][i]; /* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model */
4563: }
4564: if(ipos!=iposold){ /* Not a product or first of a product */
4565: cotvarvold=cotvarv;
4566: }else{ /* A second product */
4567: cotvarv=cotvarv*cotvarvold;
4568: }
4569: iposold=ipos;
4570: cov[ioffset+ipos]=cotvarv;
4571: }
4572: /* for products of time varying to be done */
4573: for (ii=1;ii<=nlstate+ndeath;ii++)
4574: for (j=1;j<=nlstate+ndeath;j++){
4575: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
4576: savm[ii][j]=(ii==j ? 1.0 : 0.0);
4577: }
4578:
4579: agebegin=agev[mw[mi][i]][i]; /* Age at beginning of effective wave */
4580: ageend=agev[mw[mi][i]][i] + (dh[mi][i])*stepm/YEARM; /* Age at end of effective wave and at the end of transition */
4581: for(d=0; d<dh[mi][i]; d++){
4582: newm=savm;
4583: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
4584: cov[2]=agexact;
4585: if(nagesqr==1)
4586: cov[3]= agexact*agexact; /* Should be changed here */
4587: /* for (kk=1; kk<=cptcovage;kk++) { */
4588: /* if(!FixedV[Tvar[Tage[kk]]]) */
4589: /* cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; /\* Tage[kk] gives the data-covariate associated with age *\/ */
4590: /* else */
4591: /* cov[Tage[kk]+2+nagesqr]=cotvar[mw[mi][i]][Tvar[Tage[kk]]][i]*agexact; /\* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) *\/ */
4592: /* } */
4593: for(ncovva=1, iposold=0; ncovva <= ncovta ; ncovva++){ /* Time varying covariates with age including individual from products, product is computed dynamically */
4594: itv=TvarAVVA[ncovva]; /* TvarVV={3, 1, 3} gives the name of each varying covariate, exploding product Vn*Vm into Vn and then Vm */
4595: ipos=TvarAVVAind[ncovva]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
4596: if(FixedV[itv]!=0){ /* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv */
4597: cotvarv=cotvar[mw[mi][i]][TvarAVVA[ncovva]][i]; /* because cotvar starts now at first ncovcol+nqv+ntv+nqtv (1 to nqtv) */
4598: }else{ /* fixed covariate */
4599: cotvarv=covar[itv][i]; /* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model */
4600: }
4601: if(ipos!=iposold){ /* Not a product or first of a product */
4602: cotvarvold=cotvarv;
4603: }else{ /* A second product */
4604: cotvarv=cotvarv*cotvarvold;
4605: }
4606: iposold=ipos;
4607: cov[ioffset+ipos]=cotvarv*agexact;
4608: /* For products */
4609: }
4610:
4611: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
4612: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
4613: savm=oldm;
4614: oldm=newm;
4615: } /* end mult */
4616:
4617: /*lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ /* Original formula */
4618: /* But now since version 0.9 we anticipate for bias at large stepm.
4619: * If stepm is larger than one month (smallest stepm) and if the exact delay
4620: * (in months) between two waves is not a multiple of stepm, we rounded to
4621: * the nearest (and in case of equal distance, to the lowest) interval but now
4622: * we keep into memory the bias bh[mi][i] and also the previous matrix product
4623: * (i.e to dh[mi][i]-1) saved in 'savm'. Then we inter(extra)polate the
4624: * probability in order to take into account the bias as a fraction of the way
4625: * from savm to out if bh is negative or even beyond if bh is positive. bh varies
4626: * -stepm/2 to stepm/2 .
4627: * For stepm=1 the results are the same as for previous versions of Imach.
4628: * For stepm > 1 the results are less biased than in previous versions.
4629: */
4630: s1=s[mw[mi][i]][i];
4631: s2=s[mw[mi+1][i]][i];
4632: bbh=(double)bh[mi][i]/(double)stepm;
4633: /* bias bh is positive if real duration
4634: * is higher than the multiple of stepm and negative otherwise.
4635: */
4636: /* lli= (savm[s1][s2]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2]));*/
4637: if( s2 > nlstate){
4638: /* i.e. if s2 is a death state and if the date of death is known
4639: then the contribution to the likelihood is the probability to
4640: die between last step unit time and current step unit time,
4641: which is also equal to probability to die before dh
4642: minus probability to die before dh-stepm .
4643: In version up to 0.92 likelihood was computed
4644: as if date of death was unknown. Death was treated as any other
4645: health state: the date of the interview describes the actual state
4646: and not the date of a change in health state. The former idea was
4647: to consider that at each interview the state was recorded
4648: (healthy, disable or death) and IMaCh was corrected; but when we
4649: introduced the exact date of death then we should have modified
4650: the contribution of an exact death to the likelihood. This new
4651: contribution is smaller and very dependent of the step unit
4652: stepm. It is no more the probability to die between last interview
4653: and month of death but the probability to survive from last
4654: interview up to one month before death multiplied by the
4655: probability to die within a month. Thanks to Chris
4656: Jackson for correcting this bug. Former versions increased
4657: mortality artificially. The bad side is that we add another loop
4658: which slows down the processing. The difference can be up to 10%
4659: lower mortality.
4660: */
4661: /* If, at the beginning of the maximization mostly, the
4662: cumulative probability or probability to be dead is
4663: constant (ie = 1) over time d, the difference is equal to
4664: 0. out[s1][3] = savm[s1][3]: probability, being at state
4665: s1 at precedent wave, to be dead a month before current
4666: wave is equal to probability, being at state s1 at
4667: precedent wave, to be dead at mont of the current
4668: wave. Then the observed probability (that this person died)
4669: is null according to current estimated parameter. In fact,
4670: it should be very low but not zero otherwise the log go to
4671: infinity.
4672: */
4673: /* #ifdef INFINITYORIGINAL */
4674: /* lli=log(out[s1][s2] - savm[s1][s2]); */
4675: /* #else */
4676: /* if ((out[s1][s2] - savm[s1][s2]) < mytinydouble) */
4677: /* lli=log(mytinydouble); */
4678: /* else */
4679: /* lli=log(out[s1][s2] - savm[s1][s2]); */
4680: /* #endif */
4681: lli=log(out[s1][s2] - savm[s1][s2]);
4682:
4683: } else if ( s2==-1 ) { /* alive */
4684: for (j=1,survp=0. ; j<=nlstate; j++)
4685: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
4686: /*survp += out[s1][j]; */
4687: lli= log(survp);
4688: }
4689: /* else if (s2==-4) { */
4690: /* for (j=3,survp=0. ; j<=nlstate; j++) */
4691: /* survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j]; */
4692: /* lli= log(survp); */
4693: /* } */
4694: /* else if (s2==-5) { */
4695: /* for (j=1,survp=0. ; j<=2; j++) */
4696: /* survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j]; */
4697: /* lli= log(survp); */
4698: /* } */
4699: else{
4700: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
4701: /* 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 */
4702: }
4703: /*lli=(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]);*/
4704: /*if(lli ==000.0)*/
4705: /* printf("num[i], i=%d, 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); */
4706: ipmx +=1;
4707: sw += weight[i];
4708: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
4709: /* if (lli < log(mytinydouble)){ */
4710: /* 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); */
4711: /* 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]); */
4712: /* } */
4713: } /* end of wave */
4714: } /* end of individual */
4715: } else if(mle==2){
4716: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
4717: ioffset=2+nagesqr ;
4718: for (k=1; k<=ncovf;k++)
4719: cov[ioffset+TvarFind[k]]=covar[Tvar[TvarFind[k]]][i];
4720: for(mi=1; mi<= wav[i]-1; mi++){
4721: for(k=1; k <= ncovv ; k++){
4722: cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; /* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) */
4723: }
4724: for (ii=1;ii<=nlstate+ndeath;ii++)
4725: for (j=1;j<=nlstate+ndeath;j++){
4726: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
4727: savm[ii][j]=(ii==j ? 1.0 : 0.0);
4728: }
4729: for(d=0; d<=dh[mi][i]; d++){
4730: newm=savm;
4731: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
4732: cov[2]=agexact;
4733: if(nagesqr==1)
4734: cov[3]= agexact*agexact;
4735: for (kk=1; kk<=cptcovage;kk++) {
4736: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
4737: }
4738: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
4739: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
4740: savm=oldm;
4741: oldm=newm;
4742: } /* end mult */
4743:
4744: s1=s[mw[mi][i]][i];
4745: s2=s[mw[mi+1][i]][i];
4746: bbh=(double)bh[mi][i]/(double)stepm;
4747: 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 */
4748: ipmx +=1;
4749: sw += weight[i];
4750: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
4751: } /* end of wave */
4752: } /* end of individual */
4753: } else if(mle==3){ /* exponential inter-extrapolation */
4754: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
4755: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
4756: for(mi=1; mi<= wav[i]-1; mi++){
4757: for (ii=1;ii<=nlstate+ndeath;ii++)
4758: for (j=1;j<=nlstate+ndeath;j++){
4759: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
4760: savm[ii][j]=(ii==j ? 1.0 : 0.0);
4761: }
4762: for(d=0; d<dh[mi][i]; d++){
4763: newm=savm;
4764: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
4765: cov[2]=agexact;
4766: if(nagesqr==1)
4767: cov[3]= agexact*agexact;
4768: for (kk=1; kk<=cptcovage;kk++) {
4769: if(!FixedV[Tvar[Tage[kk]]])
4770: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; /* Tage[kk] gives the data-covariate associated with age */
4771: else
4772: cov[Tage[kk]+2+nagesqr]=cotvar[mw[mi][i]][Tvar[Tage[kk]]][i]*agexact; /* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) */
4773: }
4774: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
4775: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
4776: savm=oldm;
4777: oldm=newm;
4778: } /* end mult */
4779:
4780: s1=s[mw[mi][i]][i];
4781: s2=s[mw[mi+1][i]][i];
4782: bbh=(double)bh[mi][i]/(double)stepm;
4783: 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 */
4784: ipmx +=1;
4785: sw += weight[i];
4786: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
4787: } /* end of wave */
4788: } /* end of individual */
4789: }else if (mle==4){ /* ml=4 no inter-extrapolation */
4790: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
4791: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
4792: for(mi=1; mi<= wav[i]-1; mi++){
4793: for (ii=1;ii<=nlstate+ndeath;ii++)
4794: for (j=1;j<=nlstate+ndeath;j++){
4795: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
4796: savm[ii][j]=(ii==j ? 1.0 : 0.0);
4797: }
4798: for(d=0; d<dh[mi][i]; d++){
4799: newm=savm;
4800: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
4801: cov[2]=agexact;
4802: if(nagesqr==1)
4803: cov[3]= agexact*agexact;
4804: for (kk=1; kk<=cptcovage;kk++) {
4805: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
4806: }
4807:
4808: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
4809: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
4810: savm=oldm;
4811: oldm=newm;
4812: } /* end mult */
4813:
4814: s1=s[mw[mi][i]][i];
4815: s2=s[mw[mi+1][i]][i];
4816: if( s2 > nlstate){
4817: lli=log(out[s1][s2] - savm[s1][s2]);
4818: } else if ( s2==-1 ) { /* alive */
4819: for (j=1,survp=0. ; j<=nlstate; j++)
4820: survp += out[s1][j];
4821: lli= log(survp);
4822: }else{
4823: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
4824: }
4825: ipmx +=1;
4826: sw += weight[i];
4827: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
4828: /* printf("num[i]=%09ld, 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",num[i],i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],(s2==-1? -1: out[s1][s2]),(s2==-1? -1: savm[s1][s2])); */
4829: } /* end of wave */
4830: } /* end of individual */
4831: }else{ /* ml=5 no inter-extrapolation no jackson =0.8a */
4832: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
4833: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
4834: for(mi=1; mi<= wav[i]-1; mi++){
4835: for (ii=1;ii<=nlstate+ndeath;ii++)
4836: for (j=1;j<=nlstate+ndeath;j++){
4837: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
4838: savm[ii][j]=(ii==j ? 1.0 : 0.0);
4839: }
4840: for(d=0; d<dh[mi][i]; d++){
4841: newm=savm;
4842: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
4843: cov[2]=agexact;
4844: if(nagesqr==1)
4845: cov[3]= agexact*agexact;
4846: for (kk=1; kk<=cptcovage;kk++) {
4847: if(!FixedV[Tvar[Tage[kk]]])
4848: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; /* Tage[kk] gives the data-covariate associated with age */
4849: else
4850: cov[Tage[kk]+2+nagesqr]=cotvar[mw[mi][i]][Tvar[Tage[kk]]][i]*agexact; /* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) */
4851: }
4852:
4853: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
4854: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
4855: savm=oldm;
4856: oldm=newm;
4857: } /* end mult */
4858:
4859: s1=s[mw[mi][i]][i];
4860: s2=s[mw[mi+1][i]][i];
4861: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
4862: ipmx +=1;
4863: sw += weight[i];
4864: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
4865: /*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]);*/
4866: } /* end of wave */
4867: } /* end of individual */
4868: } /* End of if */
4869: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
4870: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
4871: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
4872: return -l;
4873: }
4874:
4875: /*************** log-likelihood *************/
4876: double funcone( double *x)
4877: {
4878: /* Same as func but slower because of a lot of printf and if */
4879: int i, ii, j, k, mi, d, kk, kv=0, kf=0;
4880: int ioffset=0;
4881: int ipos=0,iposold=0,ncovv=0;
4882:
4883: double cotvarv, cotvarvold;
4884: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
4885: double **out;
4886: double lli; /* Individual log likelihood */
4887: double llt;
4888: int s1, s2;
4889: int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate, quantitative time varying covariate */
4890:
4891: double bbh, survp;
4892: double agexact;
4893: double agebegin, ageend;
4894: /*extern weight */
4895: /* We are differentiating ll according to initial status */
4896: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
4897: /*for(i=1;i<imx;i++)
4898: printf(" %d\n",s[4][i]);
4899: */
4900: cov[1]=1.;
4901:
4902: for(k=1; k<=nlstate; k++) ll[k]=0.;
4903: ioffset=0;
4904: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
4905: /* Computes the values of the ncovmodel covariates of the model
4906: depending if the covariates are fixed or varying (age dependent) and stores them in cov[]
4907: Then computes with function pmij which return a matrix p[i][j] giving the elementary probability
4908: to be observed in j being in i according to the model.
4909: */
4910: /* ioffset=2+nagesqr+cptcovage; */
4911: ioffset=2+nagesqr;
4912: /* Fixed */
4913: /* for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i]; */
4914: /* for (k=1; k<=ncoveff;k++){ /\* Simple and product fixed Dummy covariates without age* products *\/ */
4915: for (kf=1; kf<=ncovf;kf++){ /* V2 + V3 + V4 Simple and product fixed covariates without age* products *//* Missing values are set to -1 but should be dropped */
4916: /* printf("Debug3 TvarFind[%d]=%d",kf, TvarFind[kf]); */
4917: /* printf(" Tvar[TvarFind[kf]]=%d", Tvar[TvarFind[kf]]); */
4918: /* printf(" i=%d covar[Tvar[TvarFind[kf]]][i]=%f\n",i,covar[Tvar[TvarFind[kf]]][i]); */
4919: cov[ioffset+TvarFind[kf]]=covar[Tvar[TvarFind[kf]]][i];/* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, only V1 is fixed (k=6)*/
4920: /* cov[ioffset+TvarFind[1]]=covar[Tvar[TvarFind[1]]][i]; */
4921: /* cov[2+6]=covar[Tvar[6]][i]; */
4922: /* cov[2+6]=covar[2][i]; V2 */
4923: /* cov[TvarFind[2]]=covar[Tvar[TvarFind[2]]][i]; */
4924: /* cov[2+7]=covar[Tvar[7]][i]; */
4925: /* cov[2+7]=covar[7][i]; V7=V1*V2 */
4926: /* cov[TvarFind[3]]=covar[Tvar[TvarFind[3]]][i]; */
4927: /* cov[2+9]=covar[Tvar[9]][i]; */
4928: /* cov[2+9]=covar[1][i]; V1 */
4929: }
4930: /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4]
4931: is 5, Tvar[3=age*V3] should not be computed because of age Tvar[4=V3*V2]=6
4932: has been calculated etc */
4933: /* For an individual i, wav[i] gives the number of effective waves */
4934: /* We compute the contribution to Likelihood of each effective transition
4935: mw[mi][i] is real wave of the mi th effectve wave */
4936: /* Then statuses are computed at each begin and end of an effective wave s1=s[ mw[mi][i] ][i];
4937: s2=s[mw[mi+1][i]][i];
4938: And the iv th varying covariate in the DATA is the cotvar[mw[mi+1][i]][ncovcol+nqv+iv][i]
4939: */
4940: /* This part may be useless now because everythin should be in covar */
4941: /* for (k=1; k<=nqfveff;k++){ /\* Simple and product fixed Quantitative covariates without age* products *\/ */
4942: /* 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?)*\/ */
4943: /* } */
4944: /* for(iqv=1; iqv <= nqfveff; iqv++){ /\* Quantitative fixed covariates *\/ */
4945: /* cov[++ioffset]=coqvar[Tvar[iqv]][i]; /\* Only V2 k=6 and V1*V2 7 *\/ */
4946: /* } */
4947:
4948:
4949: for(mi=1; mi<= wav[i]-1; mi++){ /* Varying with waves */
4950: /* Wave varying (but not age varying) *//* V1+V3+age*V1+age*V3+V1*V3 with V4 tv and V5 tvq k= 1 to 5 and extra at V(5+1)=6 for V1*V3 */
4951: /* for(k=1; k <= ncovv ; k++){ /\* Varying covariates (single and product but no age )*\/ */
4952: /* /\* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; *\/ */
4953: /* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]-ncovcol-nqv][i]; */
4954: /* } */
4955:
4956: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
4957: /* model V1+V3+age*V1+age*V3+V1*V3 */
4958: /* Tvar={1, 3, 1, 3, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
4959: /* TvarVV[1]=V3 (first time varying in the model equation, TvarVV[2]=V1 (in V1*V3) TvarVV[3]=3(V3) */
4960: /* We need the position of the time varying or product in the model */
4961: /* TvarVVind={2,5,5}, for V3 at position 2 and then the product V1*V3 is decomposed into V1 and V3 but at same position 5 */
4962: /* TvarVV gives the variable name */
4963: /* Other example V1 + V3 + V5 + age*V1 + age*V3 + age*V5 + V1*V3 + V3*V5 + V1*V5
4964: * k= 1 2 3 4 5 6 7 8 9
4965: * varying 1 2 3 4 5
4966: * ncovv 1 2 3 4 5 6 7 8
4967: * TvarVV[ncovv] V3 5 1 3 3 5 1 5
4968: * TvarVVind 2 3 7 7 8 8 9 9
4969: * TvarFind[k] 1 0 0 0 0 0 0 0 0
4970: */
4971: /* Other model ncovcol=5 nqv=0 ntv=3 nqtv=0 nlstate=3
4972: * V2 V3 V4 are fixed V6 V7 are timevarying so V8 and V5 are not in the model and product column will start at 9 Tvar[(v6*V2)6]=9
4973: * FixedV[ncovcol+qv+ntv+nqtv] V5
4974: * 3 V1 V2 V3 V4 V5 V6 V7 V8 V3*V2 V7*V2 V6*V3 V7*V3 V6*V4 V7*V4
4975: * 0 0 0 0 0 1 1 1 0, 0, 1,1, 1, 0, 1, 0, 1, 0, 1, 0}
4976: * 3 0 0 0 0 0 1 1 1 0, 1 1 1 1 1}
4977: * model= V2 + V3 + V4 + V6 + V7 + V6*V2 + V7*V2 + V6*V3 + V7*V3 + V6*V4 + V7*V4
4978: * +age*V2 +age*V3 +age*V4 +age*V6 + age*V7
4979: * +age*V6*V2 + age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4
4980: * model2= V2 + V3 + V4 + V6 + V7 + V3*V2 + V7*V2 + V6*V3 + V7*V3 + V6*V4 + V7*V4
4981: * +age*V2 +age*V3 +age*V4 +age*V6 + age*V7
4982: * +age*V3*V2 + age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4
4983: * model3= V2 + V3 + V4 + V6 + V7 + age*V3*V2 + V7*V2 + V6*V3 + V7*V3 + V6*V4 + V7*V4
4984: * +age*V2 +age*V3 +age*V4 +age*V6 + age*V7
4985: * +V3*V2 + age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4
4986: * kmodel 1 2 3 4 5 6 7 8 9 10 11
4987: * 12 13 14 15 16
4988: * 17 18 19 20 21
4989: * Tvar[kmodel] 2 3 4 6 7 9 10 11 12 13 14
4990: * 2 3 4 6 7
4991: * 9 11 12 13 14
4992: * cptcovage=5+5 total of covariates with age
4993: * Tage[cptcovage] age*V2=12 13 14 15 16
4994: *1 17 18 19 20 21 gives the position in model of covariates associated with age
4995: *3 Tage[cptcovage] age*V3*V2=6
4996: *3 age*V2=12 13 14 15 16
4997: *3 age*V6*V3=18 19 20 21
4998: * Tvar[Tage[cptcovage]] Tvar[12]=2 3 4 6 Tvar[16]=7(age*V7)
4999: * Tvar[17]age*V6*V2=9 Tvar[18]age*V6*V3=11 age*V7*V3=12 age*V6*V4=13 Tvar[21]age*V7*V4=14
5000: * 2 Tvar[17]age*V3*V2=9 Tvar[18]age*V6*V3=11 age*V7*V3=12 age*V6*V4=13 Tvar[21]age*V7*V4=14
5001: * 3 Tvar[Tage[cptcovage]] Tvar[6]=9 Tvar[12]=2 3 4 6 Tvar[16]=7(age*V7)
5002: * 3 Tvar[18]age*V6*V3=11 age*V7*V3=12 age*V6*V4=13 Tvar[21]age*V7*V4=14
5003: * 3 Tage[cptcovage] age*V3*V2=6 age*V2=12 age*V3 13 14 15 16
5004: * age*V6*V3=18 19 20 21 gives the position in model of covariates associated with age
5005: * 3 Tvar[17]age*V3*V2=9 Tvar[18]age*V6*V3=11 age*V7*V3=12 age*V6*V4=13 Tvar[21]age*V7*V4=14
5006: * Tvar= {2, 3, 4, 6, 7,
5007: * 9, 10, 11, 12, 13, 14,
5008: * Tvar[12]=2, 3, 4, 6, 7,
5009: * Tvar[17]=9, 11, 12, 13, 14}
5010: * Typevar[1]@21 = {0, 0, 0, 0, 0,
5011: * 2, 2, 2, 2, 2, 2,
5012: * 3 3, 2, 2, 2, 2, 2,
5013: * 1, 1, 1, 1, 1,
5014: * 3, 3, 3, 3, 3}
5015: * 3 2, 3, 3, 3, 3}
5016: * p Tposprod[1]@21 {0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0, 1, 3, 4, 5, 6} Id of the prod at position k in the model
5017: * p Tprod[1]@21 {6, 7, 8, 9, 10, 11, 0 <repeats 15 times>}
5018: * 3 Tposprod[1]@21 {0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0, 1, 3, 4, 5, 6}
5019: * 3 Tprod[1]@21 {17, 7, 8, 9, 10, 11, 0 <repeats 15 times>}
5020: * cptcovprod=11 (6+5)
5021: * FixedV[Tvar[Tage[cptcovage]]]] FixedV[2]=0 FixedV[3]=0 0 1 (age*V7)Tvar[16]=1 FixedV[absolute] not [kmodel]
5022: * FixedV[Tvar[17]=FixedV[age*V6*V2]=FixedV[9]=1 1 1 1 1
5023: * 3 FixedV[Tvar[17]=FixedV[age*V3*V2]=FixedV[9]=0 [11]=1 1 1 1
5024: * FixedV[] V1=0 V2=0 V3=0 v4=0 V5=0 V6=1 V7=1 v8=1 OK then model dependent
5025: * 9=1 [V7*V2]=[10]=1 11=1 12=1 13=1 14=1
5026: * 3 9=0 [V7*V2]=[10]=1 11=1 12=1 13=1 14=1
5027: * cptcovdageprod=5 for gnuplot printing
5028: * cptcovprodvage=6
5029: * ncova=15 1 2 3 4 5
5030: * 6 7 8 9 10 11 12 13 14 15
5031: * TvarA 2 3 4 6 7
5032: * 6 2 6 7 7 3 6 4 7 4
5033: * TvaAind 12 12 13 13 14 14 15 15 16 16
5034: * ncovf 1 2 3
5035: * V6 V7 V6*V2 V7*V2 V6*V3 V7*V3 V6*V4 V7*V4
5036: * ncovvt=14 1 2 3 4 5 6 7 8 9 10 11 12 13 14
5037: * TvarVV[1]@14 = itv {V6=6, 7, V6*V2=6, 2, 7, 2, 6, 3, 7, 3, 6, 4, 7, 4}
5038: * TvarVVind[1]@14= {4, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11}
5039: * 3 ncovvt=12 V6 V7 V7*V2 V6*V3 V7*V3 V6*V4 V7*V4
5040: * 3 TvarVV[1]@12 = itv {6, 7, V7*V2=7, 2, 6, 3, 7, 3, 6, 4, 7, 4}
5041: * 3 1 2 3 4 5 6 7 8 9 10 11 12
5042: * TvarVVind[1]@12= {V6 is in k=4, 5, 7,(4isV2)=7, 8, 8, 9, 9, 10,10, 11,11}TvarVVind[12]=k=11
5043: * TvarV 6, 7, 9, 10, 11, 12, 13, 14
5044: * 3 cptcovprodvage=6
5045: * 3 ncovta=15 +age*V3*V2+age*V2+agev3+ageV4 +age*V6 + age*V7 + age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4
5046: * 3 TvarAVVA[1]@15= itva 3 2 2 3 4 6 7 6 3 7 3 6 4 7 4
5047: * 3 ncovta 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
5048: *?TvarAVVAind[1]@15= V3 is in k=2 1 1 2 3 4 5 4,2 5,2, 4,3 5 3}TvarVVAind[]
5049: * TvarAVVAind[1]@15= V3 is in k=6 6 12 13 14 15 16 18 18 19,19, 20,20 21,21}TvarVVAind[]
5050: * 3 ncovvta=10 +age*V6 + age*V7 + age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4
5051: * 3 we want to compute =cotvar[mw[mi][i]][TvarVVA[ncovva]][i] at position TvarVVAind[ncovva]
5052: * 3 TvarVVA[1]@10= itva 6 7 6 3 7 3 6 4 7 4
5053: * 3 ncovva 1 2 3 4 5 6 7 8 9 10
5054: * TvarVVAind[1]@10= V6 is in k=4 5 8,8 9, 9, 10,10 11 11}TvarVVAind[]
5055: * TvarVVAind[1]@10= 15 16 18,18 19,19, 20,20 21 21}TvarVVAind[]
5056: * TvarVA V3*V2=6 6 , 1, 2, 11, 12, 13, 14
5057: * TvarFind[1]@14= {1, 2, 3, 0 <repeats 12 times>}
5058: * Tvar[1]@21= {2, 3, 4, 6, 7, 9, 10, 11, 12, 13, 14,
5059: * 2, 3, 4, 6, 7,
5060: * 6, 8, 9, 10, 11}
5061: * TvarFind[itv] 0 0 0
5062: * FixedV[itv] 1 1 1 0 1 0 1 0 1 0 0
5063: *? FixedV[itv] 1 1 1 0 1 0 1 0 1 0 1 0 1 0
5064: * Tvar[TvarFind[ncovf]]=[1]=2 [2]=3 [4]=4
5065: * Tvar[TvarFind[itv]] [0]=? ?ncovv 1 à ncovvt]
5066: * Not a fixed cotvar[mw][itv][i] 6 7 6 2 7, 2, 6, 3, 7, 3, 6, 4, 7, 4}
5067: * fixed covar[itv] [6] [7] [6][2]
5068: */
5069:
5070: for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){ /* V6 V7 V7*V2 V6*V3 V7*V3 V6*V4 V7*V4 Time varying covariates (single and extended product but no age) including individual from products, product is computed dynamically */
5071: itv=TvarVV[ncovv]; /* TvarVV={3, 1, 3} gives the name of each varying covariate, or fixed covariate of a varying product after exploding product Vn*Vm into Vn and then Vm */
5072: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
5073: /* if(TvarFind[itv]==0){ /\* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv *\/ */
5074: if(FixedV[itv]!=0){ /* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv */
5075: /* printf("DEBUG ncovv=%d, Varying TvarVV[ncovv]=%d\n",ncovv, TvarVV[ncovv]); */
5076: cotvarv=cotvar[mw[mi][i]][TvarVV[ncovv]][i]; /* because cotvar starts now at first ncovcol+nqv+ntv+nqtv (1 to nqtv) */
5077: /* printf("DEBUG Varying cov[ioffset+ipos=%d]=%g \n",ioffset+ipos,cotvarv); */
5078: }else{ /* fixed covariate */
5079: /* cotvarv=covar[Tvar[TvarFind[itv]]][i]; /\* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model *\/ */
5080: /* printf("DEBUG ncovv=%d, Fixed TvarVV[ncovv]=%d\n",ncovv, TvarVV[ncovv]); */
5081: cotvarv=covar[itv][i]; /* Good: In V6*V3, 3 is fixed at position of the data */
5082: /* printf("DEBUG Fixed cov[ioffset+ipos=%d]=%g \n",ioffset+ipos,cotvarv); */
5083: }
5084: if(ipos!=iposold){ /* Not a product or first of a product */
5085: cotvarvold=cotvarv;
5086: }else{ /* A second product */
5087: cotvarv=cotvarv*cotvarvold;
5088: }
5089: iposold=ipos;
5090: cov[ioffset+ipos]=cotvarv;
5091: /* printf("DEBUG Product cov[ioffset+ipos=%d] \n",ioffset+ipos); */
5092: /* For products */
5093: }
5094: /* for(itv=1; itv <= ntveff; itv++){ /\* Varying dummy covariates single *\/ */
5095: /* iv=TvarVDind[itv]; /\* iv, position in the model equation of time varying covariate itv *\/ */
5096: /* /\* "V1+V3+age*V1+age*V3+V1*V3" with V3 time varying *\/ */
5097: /* /\* 1 2 3 4 5 *\/ */
5098: /* /\*itv 1 *\/ */
5099: /* /\* TvarVInd[1]= 2 *\/ */
5100: /* /\* iv= Tvar[Tmodelind[itv]]-ncovcol-nqv; /\\* Counting the # varying covariate from 1 to ntveff *\\/ *\/ */
5101: /* /\* iv= Tvar[Tmodelind[ioffset-2-nagesqr-cptcovage+itv]]-ncovcol-nqv; *\/ */
5102: /* /\* cov[ioffset+iv]=cotvar[mw[mi][i]][iv][i]; *\/ */
5103: /* /\* k=ioffset-2-nagesqr-cptcovage+itv; /\\* position in simple model *\\/ *\/ */
5104: /* /\* cov[ioffset+iv]=cotvar[mw[mi][i]][TmodelInvind[itv]][i]; *\/ */
5105: /* cov[ioffset+iv]=cotvar[mw[mi][i]][itv][i]; */
5106: /* /\* printf(" i=%d,mi=%d,itv=%d,TmodelInvind[itv]=%d,cotvar[mw[mi][i]][itv][i]=%f\n", i, mi, itv, TvarVDind[itv],cotvar[mw[mi][i]][itv][i]); *\/ */
5107: /* } */
5108: /* for(iqtv=1; iqtv <= nqtveff; iqtv++){ /\* Varying quantitatives covariates *\/ */
5109: /* iv=TmodelInvQind[iqtv]; /\* Counting the # varying covariate from 1 to ntveff *\/ */
5110: /* /\* 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]); *\/ */
5111: /* cov[ioffset+ntveff+iqtv]=cotqvar[mw[mi][i]][TmodelInvQind[iqtv]][i]; */
5112: /* } */
5113: for (ii=1;ii<=nlstate+ndeath;ii++)
5114: for (j=1;j<=nlstate+ndeath;j++){
5115: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
5116: savm[ii][j]=(ii==j ? 1.0 : 0.0);
5117: }
5118:
5119: agebegin=agev[mw[mi][i]][i]; /* Age at beginning of effective wave */
5120: ageend=agev[mw[mi][i]][i] + (dh[mi][i])*stepm/YEARM; /* Age at end of effective wave and at the end of transition */
5121: for(d=0; d<dh[mi][i]; d++){ /* Delay between two effective waves */
5122: /* for(d=0; d<=0; d++){ /\* Delay between two effective waves Only one matrix to speed up*\/ */
5123: /*dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
5124: and mw[mi+1][i]. dh depends on stepm.*/
5125: newm=savm;
5126: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM; /* Here d is needed */
5127: cov[2]=agexact;
5128: if(nagesqr==1)
5129: cov[3]= agexact*agexact;
5130: for(ncovva=1, iposold=0; ncovva <= ncovta ; ncovva++){ /* Time varying covariates with age including individual from products, product is computed dynamically */
5131: itv=TvarAVVA[ncovva]; /* TvarVV={3, 1, 3} gives the name of each varying covariate, exploding product Vn*Vm into Vn and then Vm */
5132: ipos=TvarAVVAind[ncovva]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
5133: /* if(TvarFind[itv]==0){ /\* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv *\/ */
5134: if(FixedV[itv]!=0){ /* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv */
5135: /* printf("DEBUG ncovva=%d, Varying TvarAVVA[ncovva]=%d\n", ncovva, TvarAVVA[ncovva]); */
5136: cotvarv=cotvar[mw[mi][i]][TvarAVVA[ncovva]][i]; /* because cotvar starts now at first ncovcol+nqv+ntv+nqtv (1 to nqtv) */
5137: }else{ /* fixed covariate */
5138: /* cotvarv=covar[Tvar[TvarFind[itv]]][i]; /\* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model *\/ */
5139: /* printf("DEBUG ncovva=%d, Fixed TvarAVVA[ncovva]=%d\n", ncovva, TvarAVVA[ncovva]); */
5140: cotvarv=covar[itv][i]; /* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model */
5141: }
5142: if(ipos!=iposold){ /* Not a product or first of a product */
5143: cotvarvold=cotvarv;
5144: }else{ /* A second product */
5145: /* printf("DEBUG * \n"); */
5146: cotvarv=cotvarv*cotvarvold;
5147: }
5148: iposold=ipos;
5149: /* printf("DEBUG Product cov[ioffset+ipos=%d] \n",ioffset+ipos); */
5150: cov[ioffset+ipos]=cotvarv*agexact;
5151: /* For products */
5152: }
5153:
5154: /* printf("i=%d,mi=%d,d=%d,mw[mi][i]=%d\n",i, mi,d,mw[mi][i]); */
5155: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
5156: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
5157: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
5158: /* out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath, */
5159: /* 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate)); */
5160: savm=oldm;
5161: oldm=newm;
5162: } /* end mult */
5163: /*lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ /* Original formula */
5164: /* But now since version 0.9 we anticipate for bias at large stepm.
5165: * If stepm is larger than one month (smallest stepm) and if the exact delay
5166: * (in months) between two waves is not a multiple of stepm, we rounded to
5167: * the nearest (and in case of equal distance, to the lowest) interval but now
5168: * we keep into memory the bias bh[mi][i] and also the previous matrix product
5169: * (i.e to dh[mi][i]-1) saved in 'savm'. Then we inter(extra)polate the
5170: * probability in order to take into account the bias as a fraction of the way
5171: * from savm to out if bh is negative or even beyond if bh is positive. bh varies
5172: * -stepm/2 to stepm/2 .
5173: * For stepm=1 the results are the same as for previous versions of Imach.
5174: * For stepm > 1 the results are less biased than in previous versions.
5175: */
5176: s1=s[mw[mi][i]][i];
5177: s2=s[mw[mi+1][i]][i];
5178: /* if(s2==-1){ */
5179: /* printf(" ERROR s1=%d, s2=%d i=%d \n", s1, s2, i); */
5180: /* /\* exit(1); *\/ */
5181: /* } */
5182: bbh=(double)bh[mi][i]/(double)stepm;
5183: /* bias is positive if real duration
5184: * is higher than the multiple of stepm and negative otherwise.
5185: */
5186: if( s2 > nlstate && (mle <5) ){ /* Jackson */
5187: lli=log(out[s1][s2] - savm[s1][s2]);
5188: } else if ( s2==-1 ) { /* alive */
5189: for (j=1,survp=0. ; j<=nlstate; j++)
5190: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
5191: lli= log(survp);
5192: }else if (mle==1){
5193: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
5194: } else if(mle==2){
5195: 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 */
5196: } else if(mle==3){ /* exponential inter-extrapolation */
5197: 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 */
5198: } else if (mle==4){ /* mle=4 no inter-extrapolation */
5199: lli=log(out[s1][s2]); /* Original formula */
5200: } else{ /* mle=0 back to 1 */
5201: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
5202: /*lli=log(out[s1][s2]); */ /* Original formula */
5203: } /* End of if */
5204: ipmx +=1;
5205: sw += weight[i];
5206: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
5207: /* Printing covariates values for each contribution for checking */
5208: /* printf("num[i]=%09ld, 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",num[i],i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],(s2==-1? -1: out[s1][s2]),(s2==-1? -1: savm[s1][s2])); */
5209: if(globpr){
5210: fprintf(ficresilk,"%09ld %6.1f %6.1f %6d %2d %2d %2d %2d %3d %15.6f %8.4f %8.3f\
5211: %11.6f %11.6f %11.6f ", \
5212: num[i], agebegin, ageend, i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],weight[i]*gipmx/gsw,
5213: 2*weight[i]*lli,(s2==-1? -1: out[s1][s2]),(s2==-1? -1: savm[s1][s2]));
5214: /* printf("%09ld %6.1f %6.1f %6d %2d %2d %2d %2d %3d %15.6f %8.4f %8.3f\ */
5215: /* %11.6f %11.6f %11.6f ", \ */
5216: /* num[i], agebegin, ageend, i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],weight[i]*gipmx/gsw, */
5217: /* 2*weight[i]*lli,(s2==-1? -1: out[s1][s2]),(s2==-1? -1: savm[s1][s2])); */
5218: for(k=1,llt=0.,l=0.; k<=nlstate; k++){
5219: llt +=ll[k]*gipmx/gsw;
5220: fprintf(ficresilk," %10.6f",-ll[k]*gipmx/gsw);
5221: /* printf(" %10.6f",-ll[k]*gipmx/gsw); */
5222: }
5223: fprintf(ficresilk," %10.6f ", -llt);
5224: /* printf(" %10.6f\n", -llt); */
5225: /* if(debugILK){ /\* debugILK is set by a #d in a comment line *\/ */
5226: /* fprintf(ficresilk,"%09ld ", num[i]); */ /* not necessary */
5227: for (kf=1; kf<=ncovf;kf++){ /* Simple and product fixed covariates without age* products *//* Missing values are set to -1 but should be dropped */
5228: fprintf(ficresilk," %g",covar[Tvar[TvarFind[kf]]][i]);
5229: }
5230: for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){ /* Varying covariates (single and product but no age) including individual from products */
5231: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
5232: if(ipos!=iposold){ /* Not a product or first of a product */
5233: fprintf(ficresilk," %g",cov[ioffset+ipos]);
5234: /* printf(" %g",cov[ioffset+ipos]); */
5235: }else{
5236: fprintf(ficresilk,"*");
5237: /* printf("*"); */
5238: }
5239: iposold=ipos;
5240: }
5241: /* for (kk=1; kk<=cptcovage;kk++) { */
5242: /* if(!FixedV[Tvar[Tage[kk]]]){ */
5243: /* fprintf(ficresilk," %g*age",covar[Tvar[Tage[kk]]][i]); */
5244: /* /\* printf(" %g*age",covar[Tvar[Tage[kk]]][i]); *\/ */
5245: /* }else{ */
5246: /* fprintf(ficresilk," %g*age",cotvar[mw[mi][i]][Tvar[Tage[kk]]][i]);/\* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) *\/ */
5247: /* /\* printf(" %g*age",cotvar[mw[mi][i]][Tvar[Tage[kk]]][i]);/\\* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) *\\/ *\/ */
5248: /* } */
5249: /* } */
5250: for(ncovva=1, iposold=0; ncovva <= ncovta ; ncovva++){ /* Time varying covariates with age including individual from products, product is computed dynamically */
5251: itv=TvarAVVA[ncovva]; /* TvarVV={3, 1, 3} gives the name of each varying covariate, exploding product Vn*Vm into Vn and then Vm */
5252: ipos=TvarAVVAind[ncovva]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
5253: /* if(TvarFind[itv]==0){ /\* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv *\/ */
5254: if(FixedV[itv]!=0){ /* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv */
5255: /* printf("DEBUG ncovva=%d, Varying TvarAVVA[ncovva]=%d\n", ncovva, TvarAVVA[ncovva]); */
5256: cotvarv=cotvar[mw[mi][i]][TvarAVVA[ncovva]][i]; /* because cotvar starts now at first ncovcol+nqv+ntv+nqtv (1 to nqtv) */
5257: }else{ /* fixed covariate */
5258: /* cotvarv=covar[Tvar[TvarFind[itv]]][i]; /\* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model *\/ */
5259: /* printf("DEBUG ncovva=%d, Fixed TvarAVVA[ncovva]=%d\n", ncovva, TvarAVVA[ncovva]); */
5260: cotvarv=covar[itv][i]; /* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model */
5261: }
5262: if(ipos!=iposold){ /* Not a product or first of a product */
5263: cotvarvold=cotvarv;
5264: }else{ /* A second product */
5265: /* printf("DEBUG * \n"); */
5266: cotvarv=cotvarv*cotvarvold;
5267: }
5268: cotvarv=cotvarv*agexact;
5269: fprintf(ficresilk," %g*age",cotvarv);
5270: iposold=ipos;
5271: /* printf("DEBUG Product cov[ioffset+ipos=%d] \n",ioffset+ipos); */
5272: cov[ioffset+ipos]=cotvarv;
5273: /* For products */
5274: }
5275: /* printf("\n"); */
5276: /* } /\* End debugILK *\/ */
5277: fprintf(ficresilk,"\n");
5278: } /* End if globpr */
5279: } /* end of wave */
5280: } /* end of individual */
5281: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
5282: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
5283: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
5284: if(globpr==0){ /* First time we count the contributions and weights */
5285: gipmx=ipmx;
5286: gsw=sw;
5287: }
5288: return -l;
5289: }
5290:
5291:
5292: /*************** function likelione ***********/
5293: void likelione(FILE *ficres,double p[], int npar, int nlstate, int *globpri, long *ipmx, double *sw, double *fretone, double (*func)(double []))
5294: {
5295: /* This routine should help understanding what is done with
5296: the selection of individuals/waves and
5297: to check the exact contribution to the likelihood.
5298: Plotting could be done.
5299: */
5300: void pstamp(FILE *ficres);
5301: int k, kf, kk, kvar, ncovv, iposold, ipos;
5302:
5303: if(*globpri !=0){ /* Just counts and sums, no printings */
5304: strcpy(fileresilk,"ILK_");
5305: strcat(fileresilk,fileresu);
5306: if((ficresilk=fopen(fileresilk,"w"))==NULL) {
5307: printf("Problem with resultfile: %s\n", fileresilk);
5308: fprintf(ficlog,"Problem with resultfile: %s\n", fileresilk);
5309: }
5310: pstamp(ficresilk);fprintf(ficresilk,"# model=1+age+%s\n",model);
5311: 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");
5312: fprintf(ficresilk, "#num_i ageb agend i s1 s2 mi mw dh likeli weight %%weight 2wlli out sav ");
5313: /* i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],2*weight[i]*lli,out[s1][s2],savm[s1][s2]); */
5314: for(k=1; k<=nlstate; k++)
5315: fprintf(ficresilk," -2*gipw/gsw*weight*ll[%d]++",k);
5316: fprintf(ficresilk," -2*gipw/gsw*weight*ll(total) ");
5317:
5318: /* if(debugILK){ /\* debugILK is set by a #d in a comment line *\/ */
5319: for(kf=1;kf <= ncovf; kf++){
5320: fprintf(ficresilk,"V%d",Tvar[TvarFind[kf]]);
5321: /* printf("V%d",Tvar[TvarFind[kf]]); */
5322: }
5323: for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){
5324: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate */
5325: if(ipos!=iposold){ /* Not a product or first of a product */
5326: /* printf(" %d",ipos); */
5327: fprintf(ficresilk," V%d",TvarVV[ncovv]);
5328: }else{
5329: /* printf("*"); */
5330: fprintf(ficresilk,"*");
5331: }
5332: iposold=ipos;
5333: }
5334: for (kk=1; kk<=cptcovage;kk++) {
5335: if(!FixedV[Tvar[Tage[kk]]]){
5336: /* printf(" %d*age(Fixed)",Tvar[Tage[kk]]); */
5337: fprintf(ficresilk," %d*age(Fixed)",Tvar[Tage[kk]]);
5338: }else{
5339: fprintf(ficresilk," %d*age(Varying)",Tvar[Tage[kk]]);/* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) */
5340: /* printf(" %d*age(Varying)",Tvar[Tage[kk]]);/\* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) *\/ */
5341: }
5342: }
5343: /* } /\* End if debugILK *\/ */
5344: /* printf("\n"); */
5345: fprintf(ficresilk,"\n");
5346: } /* End glogpri */
5347:
5348: *fretone=(*func)(p);
5349: if(*globpri !=0){
5350: fclose(ficresilk);
5351: if (mle ==0)
5352: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with initial parameters and mle = %d.",mle);
5353: else if(mle >=1)
5354: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with optimized parameters mle = %d.",mle);
5355: 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));
5356: fprintf(fichtm,"\n<br>Equation of the model: <b>model=1+age+%s</b><br>\n",model);
5357:
5358: 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> \
5359: <img src=\"%s-ori.png\">\n",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
5360: fprintf(fichtm,"<br>- and by state of destination <a href=\"%s-dest.png\">%s-dest.png</a><br> \
5361: <img src=\"%s-dest.png\">\n",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
5362:
5363: for (k=1; k<= nlstate ; k++) {
5364: 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>\n \
5365: <img src=\"%s-p%dj.png\">\n",k,k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k);
5366: for(kf=1; kf <= ncovf; kf++){ /* For each simple dummy covariate of the model */
5367: kvar=Tvar[TvarFind[kf]]; /* variable */
5368: fprintf(fichtm,"<br>- Probability p<sub>%dj</sub> by origin %d and destination j with colored covariate V%d. Same dot size of all points but with a different color for transitions with dummy variable V%d=1 at beginning of transition (keeping former color for V%d=0): ",k,k,Tvar[TvarFind[kf]],Tvar[TvarFind[kf]],Tvar[TvarFind[kf]]);
5369: fprintf(fichtm,"<a href=\"%s-p%dj-%d.png\">%s-p%dj-%d.png</a><br>",subdirf2(optionfilefiname,"ILK_"),k,kvar,subdirf2(optionfilefiname,"ILK_"),k,kvar);
5370: fprintf(fichtm,"<img src=\"%s-p%dj-%d.png\">",subdirf2(optionfilefiname,"ILK_"),k,Tvar[TvarFind[kf]]);
5371: }
5372: for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){ /* Loop on the time varying extended covariates (with extension of Vn*Vm */
5373: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate */
5374: kvar=TvarVV[ncovv]; /* TvarVV={3, 1, 3} gives the name of each varying covariate */
5375: /* printf("DebugILK fichtm ncovv=%d, kvar=TvarVV[ncovv]=V%d, ipos=TvarVVind[ncovv]=%d, Dummy[ipos]=%d, Typevar[ipos]=%d\n", ncovv,kvar,ipos,Dummy[ipos],Typevar[ipos]); */
5376: if(ipos!=iposold){ /* Not a product or first of a product */
5377: /* fprintf(ficresilk," V%d",TvarVV[ncovv]); */
5378: /* printf(" DebugILK fichtm ipos=%d != iposold=%d\n", ipos, iposold); */
5379: if(Dummy[ipos]==0 && Typevar[ipos]==0){ /* Only if dummy time varying: Dummy(0, 1=quant singor prod without age,2 dummy*age, 3quant*age) Typevar (0 single, 1=*age,2=Vn*vm) */
5380: fprintf(fichtm,"<br>- Probability p<sub>%dj</sub> by origin %d and destination j with colored time varying dummy covariate V%d. Same dot size of all points but with a different color for transitions with dummy variable V%d=1 at beginning of transition (keeping former color for V%d=0): <a href=\"%s-p%dj.png\">%s-p%dj.png</a><br> \
5381: <img src=\"%s-p%dj-%d.png\">",k,k,kvar,kvar,kvar,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,kvar);
5382: } /* End only for dummies time varying (single?) */
5383: }else{ /* Useless product */
5384: /* printf("*"); */
5385: /* fprintf(ficresilk,"*"); */
5386: }
5387: iposold=ipos;
5388: } /* For each time varying covariate */
5389: } /* End loop on states */
5390:
5391: /* if(debugILK){ */
5392: /* for(kf=1; kf <= ncovf; kf++){ /\* For each simple dummy covariate of the model *\/ */
5393: /* /\* kvar=Tvar[TvarFind[kf]]; *\/ /\* variable *\/ */
5394: /* for (k=1; k<= nlstate ; k++) { */
5395: /* fprintf(fichtm,"<br>- Probability p<sub>%dj</sub> by origin %d and destination j with colored covariate V%. Same dot size of all points but with a different color for transitions with dummy variable V%d=1 at beginning of transition (keeping former color for V%d=0): <a href=\"%s-p%dj.png\">%s-p%dj.png</a><br> \ */
5396: /* <img src=\"%s-p%dj-%d.png\">",k,k,Tvar[TvarFind[kf]],Tvar[TvarFind[kf]],Tvar[TvarFind[kf]],subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,Tvar[TvarFind[kf]]); */
5397: /* } */
5398: /* } */
5399: /* for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){ /\* Loop on the time varying extended covariates (with extension of Vn*Vm *\/ */
5400: /* ipos=TvarVVind[ncovv]; /\* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate *\/ */
5401: /* kvar=TvarVV[ncovv]; /\* TvarVV={3, 1, 3} gives the name of each varying covariate *\/ */
5402: /* /\* printf("DebugILK fichtm ncovv=%d, kvar=TvarVV[ncovv]=V%d, ipos=TvarVVind[ncovv]=%d, Dummy[ipos]=%d, Typevar[ipos]=%d\n", ncovv,kvar,ipos,Dummy[ipos],Typevar[ipos]); *\/ */
5403: /* if(ipos!=iposold){ /\* Not a product or first of a product *\/ */
5404: /* /\* fprintf(ficresilk," V%d",TvarVV[ncovv]); *\/ */
5405: /* /\* printf(" DebugILK fichtm ipos=%d != iposold=%d\n", ipos, iposold); *\/ */
5406: /* if(Dummy[ipos]==0 && Typevar[ipos]==0){ /\* Only if dummy time varying: Dummy(0, 1=quant singor prod without age,2 dummy*age, 3quant*age) Typevar (0 single, 1=*age,2=Vn*vm) *\/ */
5407: /* for (k=1; k<= nlstate ; k++) { */
5408: /* fprintf(fichtm,"<br>- Probability p<sub>%dj</sub> by origin %d and destination j. Same dot size of all points but with a different color for transitions with dummy variable V%d=1 at beginning of transition (keeping former color for V%d=0): <a href=\"%s-p%dj.png\">%s-p%dj.png</a><br> \ */
5409: /* <img src=\"%s-p%dj-%d.png\">",k,k,kvar,kvar,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,kvar); */
5410: /* } /\* End state *\/ */
5411: /* } /\* End only for dummies time varying (single?) *\/ */
5412: /* }else{ /\* Useless product *\/ */
5413: /* /\* printf("*"); *\/ */
5414: /* /\* fprintf(ficresilk,"*"); *\/ */
5415: /* } */
5416: /* iposold=ipos; */
5417: /* } /\* For each time varying covariate *\/ */
5418: /* }/\* End debugILK *\/ */
5419: fflush(fichtm);
5420: }/* End globpri */
5421: return;
5422: }
5423:
5424:
5425: /*********** Maximum Likelihood Estimation ***************/
5426:
5427: void mlikeli(FILE *ficres,double p[], int npar, int ncovmodel, int nlstate, double ftol, double (*func)(double []))
5428: {
5429: int i,j,k, jk, jkk=0, iter=0;
5430: double **xi;
5431: double fret;
5432: double fretone; /* Only one call to likelihood */
5433: /* char filerespow[FILENAMELENGTH];*/
5434:
5435: double * p1; /* Shifted parameters from 0 instead of 1 */
5436: #ifdef NLOPT
5437: int creturn;
5438: nlopt_opt opt;
5439: /* double lb[9] = { -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL }; /\* lower bounds *\/ */
5440: double *lb;
5441: double minf; /* the minimum objective value, upon return */
5442:
5443: myfunc_data dinst, *d = &dinst;
5444: #endif
5445:
5446:
5447: xi=matrix(1,npar,1,npar);
5448: for (i=1;i<=npar;i++) /* Starting with canonical directions j=1,n xi[i=1,n][j] */
5449: for (j=1;j<=npar;j++)
5450: xi[i][j]=(i==j ? 1.0 : 0.0);
5451: printf("Powell-prax\n"); fprintf(ficlog,"Powell-prax\n");
5452: strcpy(filerespow,"POW_");
5453: strcat(filerespow,fileres);
5454: if((ficrespow=fopen(filerespow,"w"))==NULL) {
5455: printf("Problem with resultfile: %s\n", filerespow);
5456: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
5457: }
5458: fprintf(ficrespow,"# Powell\n# iter -2*LL");
5459: for (i=1;i<=nlstate;i++)
5460: for(j=1;j<=nlstate+ndeath;j++)
5461: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
5462: fprintf(ficrespow,"\n");
5463: #ifdef POWELL
5464: #ifdef LINMINORIGINAL
5465: #else /* LINMINORIGINAL */
5466:
5467: flatdir=ivector(1,npar);
5468: for (j=1;j<=npar;j++) flatdir[j]=0;
5469: #endif /*LINMINORIGINAL */
5470:
5471: #ifdef FLATSUP
5472: powell(p,xi,npar,ftol,&iter,&fret,flatdir,func);
5473: /* reorganizing p by suppressing flat directions */
5474: for(i=1, jk=1; i <=nlstate; i++){
5475: for(k=1; k <=(nlstate+ndeath); k++){
5476: if (k != i) {
5477: printf("%d%d flatdir[%d]=%d",i,k,jk, flatdir[jk]);
5478: if(flatdir[jk]==1){
5479: printf(" To be skipped %d%d flatdir[%d]=%d ",i,k,jk, flatdir[jk]);
5480: }
5481: for(j=1; j <=ncovmodel; j++){
5482: printf("%12.7f ",p[jk]);
5483: jk++;
5484: }
5485: printf("\n");
5486: }
5487: }
5488: }
5489: /* skipping */
5490: /* for(i=1, jk=1, jkk=1;(flatdir[jk]==0)&& (i <=nlstate); i++){ */
5491: for(i=1, jk=1, jkk=1;i <=nlstate; i++){
5492: for(k=1; k <=(nlstate+ndeath); k++){
5493: if (k != i) {
5494: printf("%d%d flatdir[%d]=%d",i,k,jk, flatdir[jk]);
5495: if(flatdir[jk]==1){
5496: printf(" To be skipped %d%d flatdir[%d]=%d jk=%d p[%d] ",i,k,jk, flatdir[jk],jk, jk);
5497: for(j=1; j <=ncovmodel; jk++,j++){
5498: printf(" p[%d]=%12.7f",jk, p[jk]);
5499: /*q[jjk]=p[jk];*/
5500: }
5501: }else{
5502: printf(" To be kept %d%d flatdir[%d]=%d jk=%d q[%d]=p[%d] ",i,k,jk, flatdir[jk],jk, jkk, jk);
5503: for(j=1; j <=ncovmodel; jk++,jkk++,j++){
5504: printf(" p[%d]=%12.7f=q[%d]",jk, p[jk],jkk);
5505: /*q[jjk]=p[jk];*/
5506: }
5507: }
5508: printf("\n");
5509: }
5510: fflush(stdout);
5511: }
5512: }
5513: powell(p,xi,npar,ftol,&iter,&fret,flatdir,func);
5514: #else /* FLATSUP */
5515: powell(p,xi,npar,ftol,&iter,&fret,func);
5516: /* praxis ( t0, h0, n, prin, x, beale_f ); */
5517: /* int prin=4; */
5518: /* double h0=0.25; */
5519: /* #include "praxis.h" */
5520: /* Be careful that praxis start at x[0] and powell start at p[1] */
5521: /* praxis ( ftol, h0, npar, prin, p, func ); */
5522: /* p1= (p+1); /\* p *(p+1)@8 and p *(p1)@8 are equal p1[0]=p[1] *\/ */
5523: /* printf("Praxis \n"); */
5524: /* fprintf(ficlog, "Praxis \n");fflush(ficlog); */
5525: /* praxis ( ftol, h0, npar, prin, p1, func ); */
5526: /* printf("End Praxis\n"); */
5527: #endif /* FLATSUP */
5528:
5529: #ifdef LINMINORIGINAL
5530: #else
5531: free_ivector(flatdir,1,npar);
5532: #endif /* LINMINORIGINAL*/
5533: #endif /* POWELL */
5534:
5535: #ifdef NLOPT
5536: #ifdef NEWUOA
5537: opt = nlopt_create(NLOPT_LN_NEWUOA,npar);
5538: #else
5539: opt = nlopt_create(NLOPT_LN_BOBYQA,npar);
5540: #endif
5541: lb=vector(0,npar-1);
5542: for (i=0;i<npar;i++) lb[i]= -HUGE_VAL;
5543: nlopt_set_lower_bounds(opt, lb);
5544: nlopt_set_initial_step1(opt, 0.1);
5545:
5546: p1= (p+1); /* p *(p+1)@8 and p *(p1)@8 are equal p1[0]=p[1] */
5547: d->function = func;
5548: printf(" Func %.12lf \n",myfunc(npar,p1,NULL,d));
5549: nlopt_set_min_objective(opt, myfunc, d);
5550: nlopt_set_xtol_rel(opt, ftol);
5551: if ((creturn=nlopt_optimize(opt, p1, &minf)) < 0) {
5552: printf("nlopt failed! %d\n",creturn);
5553: }
5554: else {
5555: printf("found minimum after %d evaluations (NLOPT=%d)\n", countcallfunc ,NLOPT);
5556: printf("found minimum at f(%g,%g) = %0.10g\n", p[0], p[1], minf);
5557: iter=1; /* not equal */
5558: }
5559: nlopt_destroy(opt);
5560: #endif
5561: #ifdef FLATSUP
5562: /* npared = npar -flatd/ncovmodel; */
5563: /* xired= matrix(1,npared,1,npared); */
5564: /* paramred= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel); */
5565: /* powell(pred,xired,npared,ftol,&iter,&fret,flatdir,func); */
5566: /* free_matrix(xire,1,npared,1,npared); */
5567: #else /* FLATSUP */
5568: #endif /* FLATSUP */
5569: free_matrix(xi,1,npar,1,npar);
5570: fclose(ficrespow);
5571: printf("\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
5572: fprintf(ficlog,"\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
5573: fprintf(ficres,"#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
5574:
5575: }
5576:
5577: /**** Computes Hessian and covariance matrix ***/
5578: void hesscov(double **matcov, double **hess, double p[], int npar, double delti[], double ftolhess, double (*func)(double []))
5579: {
5580: double **a,**y,*x,pd;
5581: /* double **hess; */
5582: int i, j;
5583: int *indx;
5584:
5585: double hessii(double p[], double delta, int theta, double delti[],double (*func)(double []),int npar);
5586: double hessij(double p[], double **hess, double delti[], int i, int j,double (*func)(double []),int npar);
5587: void lubksb(double **a, int npar, int *indx, double b[]) ;
5588: void ludcmp(double **a, int npar, int *indx, double *d) ;
5589: double gompertz(double p[]);
5590: /* hess=matrix(1,npar,1,npar); */
5591:
5592: printf("\nCalculation of the hessian matrix. Wait...\n");
5593: fprintf(ficlog,"\nCalculation of the hessian matrix. Wait...\n");
5594: for (i=1;i<=npar;i++){
5595: printf("%d-",i);fflush(stdout);
5596: fprintf(ficlog,"%d-",i);fflush(ficlog);
5597:
5598: hess[i][i]=hessii(p,ftolhess,i,delti,func,npar);
5599:
5600: /* printf(" %f ",p[i]);
5601: printf(" %lf %lf %lf",hess[i][i],ftolhess,delti[i]);*/
5602: }
5603:
5604: for (i=1;i<=npar;i++) {
5605: for (j=1;j<=npar;j++) {
5606: if (j>i) {
5607: printf(".%d-%d",i,j);fflush(stdout);
5608: fprintf(ficlog,".%d-%d",i,j);fflush(ficlog);
5609: hess[i][j]=hessij(p,hess, delti,i,j,func,npar);
5610:
5611: hess[j][i]=hess[i][j];
5612: /*printf(" %lf ",hess[i][j]);*/
5613: }
5614: }
5615: }
5616: printf("\n");
5617: fprintf(ficlog,"\n");
5618:
5619: printf("\nInverting the hessian to get the covariance matrix. Wait...\n");
5620: fprintf(ficlog,"\nInverting the hessian to get the covariance matrix. Wait...\n");
5621:
5622: a=matrix(1,npar,1,npar);
5623: y=matrix(1,npar,1,npar);
5624: x=vector(1,npar);
5625: indx=ivector(1,npar);
5626: for (i=1;i<=npar;i++)
5627: for (j=1;j<=npar;j++) a[i][j]=hess[i][j];
5628: ludcmp(a,npar,indx,&pd);
5629:
5630: for (j=1;j<=npar;j++) {
5631: for (i=1;i<=npar;i++) x[i]=0;
5632: x[j]=1;
5633: lubksb(a,npar,indx,x);
5634: for (i=1;i<=npar;i++){
5635: matcov[i][j]=x[i];
5636: }
5637: }
5638:
5639: printf("\n#Hessian matrix#\n");
5640: fprintf(ficlog,"\n#Hessian matrix#\n");
5641: for (i=1;i<=npar;i++) {
5642: for (j=1;j<=npar;j++) {
5643: printf("%.6e ",hess[i][j]);
5644: fprintf(ficlog,"%.6e ",hess[i][j]);
5645: }
5646: printf("\n");
5647: fprintf(ficlog,"\n");
5648: }
5649:
5650: /* printf("\n#Covariance matrix#\n"); */
5651: /* fprintf(ficlog,"\n#Covariance matrix#\n"); */
5652: /* for (i=1;i<=npar;i++) { */
5653: /* for (j=1;j<=npar;j++) { */
5654: /* printf("%.6e ",matcov[i][j]); */
5655: /* fprintf(ficlog,"%.6e ",matcov[i][j]); */
5656: /* } */
5657: /* printf("\n"); */
5658: /* fprintf(ficlog,"\n"); */
5659: /* } */
5660:
5661: /* Recompute Inverse */
5662: /* for (i=1;i<=npar;i++) */
5663: /* for (j=1;j<=npar;j++) a[i][j]=matcov[i][j]; */
5664: /* ludcmp(a,npar,indx,&pd); */
5665:
5666: /* printf("\n#Hessian matrix recomputed#\n"); */
5667:
5668: /* for (j=1;j<=npar;j++) { */
5669: /* for (i=1;i<=npar;i++) x[i]=0; */
5670: /* x[j]=1; */
5671: /* lubksb(a,npar,indx,x); */
5672: /* for (i=1;i<=npar;i++){ */
5673: /* y[i][j]=x[i]; */
5674: /* printf("%.3e ",y[i][j]); */
5675: /* fprintf(ficlog,"%.3e ",y[i][j]); */
5676: /* } */
5677: /* printf("\n"); */
5678: /* fprintf(ficlog,"\n"); */
5679: /* } */
5680:
5681: /* Verifying the inverse matrix */
5682: #ifdef DEBUGHESS
5683: y=matprod2(y,hess,1,npar,1,npar,1,npar,matcov);
5684:
5685: printf("\n#Verification: multiplying the matrix of covariance by the Hessian matrix, should be unity:#\n");
5686: fprintf(ficlog,"\n#Verification: multiplying the matrix of covariance by the Hessian matrix. Should be unity:#\n");
5687:
5688: for (j=1;j<=npar;j++) {
5689: for (i=1;i<=npar;i++){
5690: printf("%.2f ",y[i][j]);
5691: fprintf(ficlog,"%.2f ",y[i][j]);
5692: }
5693: printf("\n");
5694: fprintf(ficlog,"\n");
5695: }
5696: #endif
5697:
5698: free_matrix(a,1,npar,1,npar);
5699: free_matrix(y,1,npar,1,npar);
5700: free_vector(x,1,npar);
5701: free_ivector(indx,1,npar);
5702: /* free_matrix(hess,1,npar,1,npar); */
5703:
5704:
5705: }
5706:
5707: /*************** hessian matrix ****************/
5708: double hessii(double x[], double delta, int theta, double delti[], double (*func)(double []), int npar)
5709: { /* Around values of x, computes the function func and returns the scales delti and hessian */
5710: int i;
5711: int l=1, lmax=20;
5712: double k1,k2, res, fx;
5713: double p2[MAXPARM+1]; /* identical to x */
5714: double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4;
5715: int k=0,kmax=10;
5716: double l1;
5717:
5718: fx=func(x);
5719: for (i=1;i<=npar;i++) p2[i]=x[i];
5720: for(l=0 ; l <=lmax; l++){ /* Enlarging the zone around the Maximum */
5721: l1=pow(10,l);
5722: delts=delt;
5723: for(k=1 ; k <kmax; k=k+1){
5724: delt = delta*(l1*k);
5725: p2[theta]=x[theta] +delt;
5726: k1=func(p2)-fx; /* Might be negative if too close to the theoretical maximum */
5727: p2[theta]=x[theta]-delt;
5728: k2=func(p2)-fx;
5729: /*res= (k1-2.0*fx+k2)/delt/delt; */
5730: res= (k1+k2)/delt/delt/2.; /* Divided by 2 because L and not 2*L */
5731:
5732: #ifdef DEBUGHESSII
5733: 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);
5734: 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);
5735: #endif
5736: /*if(fabs(k1-2.0*fx+k2) <1.e-13){ */
5737: if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)){
5738: k=kmax;
5739: }
5740: else if((k1 >khi/nkhif) || (k2 >khi/nkhif)){ /* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. */
5741: k=kmax; l=lmax*10;
5742: }
5743: else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){
5744: delts=delt;
5745: }
5746: } /* End loop k */
5747: }
5748: delti[theta]=delts;
5749: return res;
5750:
5751: }
5752:
5753: double hessij( double x[], double **hess, double delti[], int thetai,int thetaj,double (*func)(double []),int npar)
5754: {
5755: int i;
5756: int l=1, lmax=20;
5757: double k1,k2,k3,k4,res,fx;
5758: double p2[MAXPARM+1];
5759: int k, kmax=1;
5760: double v1, v2, cv12, lc1, lc2;
5761:
5762: int firstime=0;
5763:
5764: fx=func(x);
5765: for (k=1; k<=kmax; k=k+10) {
5766: for (i=1;i<=npar;i++) p2[i]=x[i];
5767: p2[thetai]=x[thetai]+delti[thetai]*k;
5768: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
5769: k1=func(p2)-fx;
5770:
5771: p2[thetai]=x[thetai]+delti[thetai]*k;
5772: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
5773: k2=func(p2)-fx;
5774:
5775: p2[thetai]=x[thetai]-delti[thetai]*k;
5776: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
5777: k3=func(p2)-fx;
5778:
5779: p2[thetai]=x[thetai]-delti[thetai]*k;
5780: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
5781: k4=func(p2)-fx;
5782: res=(k1-k2-k3+k4)/4.0/delti[thetai]/k/delti[thetaj]/k/2.; /* Because of L not 2*L */
5783: if(k1*k2*k3*k4 <0.){
5784: firstime=1;
5785: kmax=kmax+10;
5786: }
5787: if(kmax >=10 || firstime ==1){
5788: /* What are the thetai and thetaj? thetai/ncovmodel thetai=(thetai-thetai%ncovmodel)/ncovmodel +thetai%ncovmodel=(line,pos) */
5789: printf("Warning: directions %d-%d, you are not estimating the Hessian at the exact maximum likelihood; you could increase ftol=%.2e\n",thetai,thetaj, ftol);
5790: fprintf(ficlog,"Warning: directions %d-%d, you are not estimating the Hessian at the exact maximum likelihood; you could increase ftol=%.2e\n",thetai,thetaj, ftol);
5791: 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);
5792: 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);
5793: }
5794: #ifdef DEBUGHESSIJ
5795: v1=hess[thetai][thetai];
5796: v2=hess[thetaj][thetaj];
5797: cv12=res;
5798: /* Computing eigen value of Hessian matrix */
5799: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
5800: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
5801: if ((lc2 <0) || (lc1 <0) ){
5802: printf("Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
5803: fprintf(ficlog, "Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
5804: 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);
5805: 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);
5806: }
5807: #endif
5808: }
5809: return res;
5810: }
5811:
5812: /* Not done yet: Was supposed to fix if not exactly at the maximum */
5813: /* double hessij( double x[], double delti[], int thetai,int thetaj,double (*func)(double []),int npar) */
5814: /* { */
5815: /* int i; */
5816: /* int l=1, lmax=20; */
5817: /* double k1,k2,k3,k4,res,fx; */
5818: /* double p2[MAXPARM+1]; */
5819: /* double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4; */
5820: /* int k=0,kmax=10; */
5821: /* double l1; */
5822:
5823: /* fx=func(x); */
5824: /* for(l=0 ; l <=lmax; l++){ /\* Enlarging the zone around the Maximum *\/ */
5825: /* l1=pow(10,l); */
5826: /* delts=delt; */
5827: /* for(k=1 ; k <kmax; k=k+1){ */
5828: /* delt = delti*(l1*k); */
5829: /* for (i=1;i<=npar;i++) p2[i]=x[i]; */
5830: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
5831: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
5832: /* k1=func(p2)-fx; */
5833:
5834: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
5835: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
5836: /* k2=func(p2)-fx; */
5837:
5838: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
5839: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
5840: /* k3=func(p2)-fx; */
5841:
5842: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
5843: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
5844: /* k4=func(p2)-fx; */
5845: /* res=(k1-k2-k3+k4)/4.0/delti[thetai]*k/delti[thetaj]*k/2.; /\* Because of L not 2*L *\/ */
5846: /* #ifdef DEBUGHESSIJ */
5847: /* 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); */
5848: /* 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); */
5849: /* #endif */
5850: /* if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)){ */
5851: /* k=kmax; */
5852: /* } */
5853: /* else if((k1 >khi/nkhif) || (k2 >khi/nkhif) || (k4 >khi/nkhif) || (k4 >khi/nkhif)){ /\* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. *\/ */
5854: /* k=kmax; l=lmax*10; */
5855: /* } */
5856: /* else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){ */
5857: /* delts=delt; */
5858: /* } */
5859: /* } /\* End loop k *\/ */
5860: /* } */
5861: /* delti[theta]=delts; */
5862: /* return res; */
5863: /* } */
5864:
5865:
5866: /************** Inverse of matrix **************/
5867: void ludcmp(double **a, int n, int *indx, double *d)
5868: {
5869: int i,imax,j,k;
5870: double big,dum,sum,temp;
5871: double *vv;
5872:
5873: vv=vector(1,n);
5874: *d=1.0;
5875: for (i=1;i<=n;i++) {
5876: big=0.0;
5877: for (j=1;j<=n;j++)
5878: if ((temp=fabs(a[i][j])) > big) big=temp;
5879: if (big == 0.0){
5880: printf(" Singular Hessian matrix at row %d:\n",i);
5881: for (j=1;j<=n;j++) {
5882: printf(" a[%d][%d]=%f,",i,j,a[i][j]);
5883: fprintf(ficlog," a[%d][%d]=%f,",i,j,a[i][j]);
5884: }
5885: fflush(ficlog);
5886: fclose(ficlog);
5887: nrerror("Singular matrix in routine ludcmp");
5888: }
5889: vv[i]=1.0/big;
5890: }
5891: for (j=1;j<=n;j++) {
5892: for (i=1;i<j;i++) {
5893: sum=a[i][j];
5894: for (k=1;k<i;k++) sum -= a[i][k]*a[k][j];
5895: a[i][j]=sum;
5896: }
5897: big=0.0;
5898: for (i=j;i<=n;i++) {
5899: sum=a[i][j];
5900: for (k=1;k<j;k++)
5901: sum -= a[i][k]*a[k][j];
5902: a[i][j]=sum;
5903: if ( (dum=vv[i]*fabs(sum)) >= big) {
5904: big=dum;
5905: imax=i;
5906: }
5907: }
5908: if (j != imax) {
5909: for (k=1;k<=n;k++) {
5910: dum=a[imax][k];
5911: a[imax][k]=a[j][k];
5912: a[j][k]=dum;
5913: }
5914: *d = -(*d);
5915: vv[imax]=vv[j];
5916: }
5917: indx[j]=imax;
5918: if (a[j][j] == 0.0) a[j][j]=TINY;
5919: if (j != n) {
5920: dum=1.0/(a[j][j]);
5921: for (i=j+1;i<=n;i++) a[i][j] *= dum;
5922: }
5923: }
5924: free_vector(vv,1,n); /* Doesn't work */
5925: ;
5926: }
5927:
5928: void lubksb(double **a, int n, int *indx, double b[])
5929: {
5930: int i,ii=0,ip,j;
5931: double sum;
5932:
5933: for (i=1;i<=n;i++) {
5934: ip=indx[i];
5935: sum=b[ip];
5936: b[ip]=b[i];
5937: if (ii)
5938: for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j];
5939: else if (sum) ii=i;
5940: b[i]=sum;
5941: }
5942: for (i=n;i>=1;i--) {
5943: sum=b[i];
5944: for (j=i+1;j<=n;j++) sum -= a[i][j]*b[j];
5945: b[i]=sum/a[i][i];
5946: }
5947: }
5948:
5949: void pstamp(FILE *fichier)
5950: {
5951: fprintf(fichier,"# %s.%s\n#IMaCh version %s, %s\n#%s\n# %s", optionfilefiname,optionfilext,version,copyright, fullversion, strstart);
5952: }
5953:
5954: void date2dmy(double date,double *day, double *month, double *year){
5955: double yp=0., yp1=0., yp2=0.;
5956:
5957: yp1=modf(date,&yp);/* extracts integral of date in yp and
5958: fractional in yp1 */
5959: *year=yp;
5960: yp2=modf((yp1*12),&yp);
5961: *month=yp;
5962: yp1=modf((yp2*30.5),&yp);
5963: *day=yp;
5964: if(*day==0) *day=1;
5965: if(*month==0) *month=1;
5966: }
5967:
5968:
5969:
5970: /************ Frequencies ********************/
5971: void freqsummary(char fileres[], double p[], double pstart[], int iagemin, int iagemax, int **s, double **agev, int nlstate, int imx, \
5972: int *Tvaraff, int *invalidvarcomb, int **nbcode, int *ncodemax,double **mint,double **anint, char strstart[], \
5973: int firstpass, int lastpass, int stepm, int weightopt, char model[])
5974: { /* Some frequencies as well as proposing some starting values */
5975: /* Frequencies of any combination of dummy covariate used in the model equation */
5976: int i, m, jk, j1, bool, z1,j, nj, nl, k, iv, jj=0, s1=1, s2=1;
5977: int iind=0, iage=0;
5978: int mi; /* Effective wave */
5979: int first;
5980: double ***freq; /* Frequencies */
5981: double *x, *y, a=0.,b=0.,r=1., sa=0., sb=0.; /* for regression, y=b+m*x and r is the correlation coefficient */
5982: int no=0, linreg(int ifi, int ila, int *no, const double x[], const double y[], double* a, double* b, double* r, double* sa, double * sb);
5983: double *meanq, *stdq, *idq;
5984: double **meanqt;
5985: double *pp, **prop, *posprop, *pospropt;
5986: double pos=0., posproptt=0., pospropta=0., k2, dateintsum=0,k2cpt=0;
5987: char fileresp[FILENAMELENGTH], fileresphtm[FILENAMELENGTH], fileresphtmfr[FILENAMELENGTH];
5988: double agebegin, ageend;
5989:
5990: pp=vector(1,nlstate);
5991: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
5992: posprop=vector(1,nlstate); /* Counting the number of transition starting from a live state per age */
5993: pospropt=vector(1,nlstate); /* Counting the number of transition starting from a live state */
5994: /* prop=matrix(1,nlstate,iagemin,iagemax+3); */
5995: meanq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
5996: stdq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
5997: idq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
5998: meanqt=matrix(1,lastpass,1,nqtveff);
5999: strcpy(fileresp,"P_");
6000: strcat(fileresp,fileresu);
6001: /*strcat(fileresphtm,fileresu);*/
6002: if((ficresp=fopen(fileresp,"w"))==NULL) {
6003: printf("Problem with prevalence resultfile: %s\n", fileresp);
6004: fprintf(ficlog,"Problem with prevalence resultfile: %s\n", fileresp);
6005: exit(0);
6006: }
6007:
6008: strcpy(fileresphtm,subdirfext(optionfilefiname,"PHTM_",".htm"));
6009: if((ficresphtm=fopen(fileresphtm,"w"))==NULL) {
6010: printf("Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
6011: fprintf(ficlog,"Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
6012: fflush(ficlog);
6013: exit(70);
6014: }
6015: else{
6016: fprintf(ficresphtm,"<html><head>\n<title>IMaCh PHTM_ %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
6017: <hr size=\"2\" color=\"#EC5E5E\"> \n \
6018: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
6019: fileresphtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
6020: }
6021: fprintf(ficresphtm,"Current page is file <a href=\"%s\">%s</a><br>\n\n<h4>Frequencies (weight=%d) and prevalence by age at begin of transition and dummy covariate value at beginning of transition</h4>\n",fileresphtm, fileresphtm, weightopt);
6022:
6023: strcpy(fileresphtmfr,subdirfext(optionfilefiname,"PHTMFR_",".htm"));
6024: if((ficresphtmfr=fopen(fileresphtmfr,"w"))==NULL) {
6025: printf("Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
6026: fprintf(ficlog,"Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
6027: fflush(ficlog);
6028: exit(70);
6029: } else{
6030: fprintf(ficresphtmfr,"<html><head>\n<title>IMaCh PHTM_Frequency table %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
6031: ,<hr size=\"2\" color=\"#EC5E5E\"> \n \
6032: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
6033: fileresphtmfr,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
6034: }
6035: fprintf(ficresphtmfr,"Current page is file <a href=\"%s\">%s</a><br>\n\n<h4>(weight=%d) frequencies of all effective transitions of the model, by age at begin of transition, and covariate value at the begin of transition (if the covariate is a varying covariate) </h4>Unknown status is -1<br/>\n",fileresphtmfr, fileresphtmfr,weightopt);
6036:
6037: y= vector(iagemin-AGEMARGE,iagemax+4+AGEMARGE);
6038: x= vector(iagemin-AGEMARGE,iagemax+4+AGEMARGE);
6039: freq= ma3x(-5,nlstate+ndeath,-5,nlstate+ndeath,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
6040: j1=0;
6041:
6042: /* j=ncoveff; /\* Only fixed dummy covariates *\/ */
6043: j=cptcoveff; /* Only simple dummy covariates used in the model */
6044: /* j=cptcovn; /\* Only dummy covariates of the model *\/ */
6045: if (cptcovn<1) {j=1;ncodemax[1]=1;}
6046:
6047:
6048: /* Detects if a combination j1 is empty: for a multinomial variable like 3 education levels:
6049: reference=low_education V1=0,V2=0
6050: med_educ V1=1 V2=0,
6051: high_educ V1=0 V2=1
6052: Then V1=1 and V2=1 is a noisy combination that we want to exclude for the list 2**cptcovn
6053: */
6054: dateintsum=0;
6055: k2cpt=0;
6056:
6057: if(cptcoveff == 0 )
6058: nl=1; /* Constant and age model only */
6059: else
6060: nl=2;
6061:
6062: /* if a constant only model, one pass to compute frequency tables and to write it on ficresp */
6063: /* Loop on nj=1 or 2 if dummy covariates j!=0
6064: * Loop on j1(1 to 2**cptcoveff) covariate combination
6065: * freq[s1][s2][iage] =0.
6066: * Loop on iind
6067: * ++freq[s1][s2][iage] weighted
6068: * end iind
6069: * if covariate and j!0
6070: * headers Variable on one line
6071: * endif cov j!=0
6072: * header of frequency table by age
6073: * Loop on age
6074: * pp[s1]+=freq[s1][s2][iage] weighted
6075: * pos+=freq[s1][s2][iage] weighted
6076: * Loop on s1 initial state
6077: * fprintf(ficresp
6078: * end s1
6079: * end age
6080: * if j!=0 computes starting values
6081: * end compute starting values
6082: * end j1
6083: * end nl
6084: */
6085: for (nj = 1; nj <= nl; nj++){ /* nj= 1 constant model, nl number of loops. */
6086: if(nj==1)
6087: j=0; /* First pass for the constant */
6088: else{
6089: j=cptcoveff; /* Other passes for the covariate values number of simple covariates in the model V2+V1 =2 (simple dummy fixed or time varying) */
6090: }
6091: first=1;
6092: for (j1 = 1; j1 <= (int) pow(2,j); j1++){ /* Loop on all dummy covariates combination of the model, ie excluding quantitatives, V4=0, V3=0 for example, fixed or varying covariates */
6093: posproptt=0.;
6094: /*printf("cptcovn=%d Tvaraff=%d", cptcovn,Tvaraff[1]);
6095: scanf("%d", i);*/
6096: for (i=-5; i<=nlstate+ndeath; i++)
6097: for (s2=-5; s2<=nlstate+ndeath; s2++)
6098: for(m=iagemin; m <= iagemax+3; m++)
6099: freq[i][s2][m]=0;
6100:
6101: for (i=1; i<=nlstate; i++) {
6102: for(m=iagemin; m <= iagemax+3; m++)
6103: prop[i][m]=0;
6104: posprop[i]=0;
6105: pospropt[i]=0;
6106: }
6107: for (z1=1; z1<= nqfveff; z1++) { /* zeroing for each combination j1 as well as for the total */
6108: idq[z1]=0.;
6109: meanq[z1]=0.;
6110: stdq[z1]=0.;
6111: }
6112: /* for (z1=1; z1<= nqtveff; z1++) { */
6113: /* for(m=1;m<=lastpass;m++){ */
6114: /* meanqt[m][z1]=0.; */
6115: /* } */
6116: /* } */
6117: /* dateintsum=0; */
6118: /* k2cpt=0; */
6119:
6120: /* For that combination of covariates j1 (V4=1 V3=0 for example), we count and print the frequencies in one pass */
6121: for (iind=1; iind<=imx; iind++) { /* For each individual iind */
6122: bool=1;
6123: if(j !=0){
6124: if(anyvaryingduminmodel==0){ /* If All fixed covariates */
6125: if (cptcoveff >0) { /* Filter is here: Must be looked at for model=V1+V2+V3+V4 */
6126: for (z1=1; z1<=cptcoveff; z1++) { /* loops on covariates in the model */
6127: /* if(Tvaraff[z1] ==-20){ */
6128: /* /\* sumnew+=cotvar[mw[mi][iind]][z1][iind]; *\/ */
6129: /* }else if(Tvaraff[z1] ==-10){ */
6130: /* /\* sumnew+=coqvar[z1][iind]; *\/ */
6131: /* }else */ /* TODO TODO codtabm(j1,z1) or codtabm(j1,Tvaraff[z1]]z1)*/
6132: /* if( iind >=imx-3) printf("Searching error iind=%d Tvaraff[z1]=%d covar[Tvaraff[z1]][iind]=%.f TnsdVar[Tvaraff[z1]]=%d, cptcoveff=%d, cptcovs=%d \n",iind, Tvaraff[z1], covar[Tvaraff[z1]][iind],TnsdVar[Tvaraff[z1]],cptcoveff, cptcovs); */
6133: if(Tvaraff[z1]<1 || Tvaraff[z1]>=NCOVMAX)
6134: printf("Error Tvaraff[z1]=%d<1 or >=%d, cptcoveff=%d model=1+age+%s\n",Tvaraff[z1],NCOVMAX, cptcoveff, model);
6135: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]){ /* for combination j1 of covariates */
6136: /* Tests if the value of the covariate z1 for this individual iind responded to combination j1 (V4=1 V3=0) */
6137: bool=0; /* bool should be equal to 1 to be selected, one covariate value failed */
6138: /* 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", */
6139: /* bool,i,z1, z1, Tvaraff[z1],i,covar[Tvaraff[z1]][i],j1,z1,codtabm(j1,z1),*/
6140: /* j1,z1,nbcode[Tvaraff[z1]][codtabm(j1,z1)],j1);*/
6141: /* For j1=7 in V1+V2+V3+V4 = 0 1 1 0 and codtabm(7,3)=1 and nbcde[3][?]=1*/
6142: } /* Onlyf fixed */
6143: } /* end z1 */
6144: } /* cptcoveff > 0 */
6145: } /* end any */
6146: }/* end j==0 */
6147: if (bool==1){ /* We selected an individual iind satisfying combination j1 (V4=1 V3=0) or all fixed covariates */
6148: /* for(m=firstpass; m<=lastpass; m++){ */
6149: for(mi=1; mi<wav[iind];mi++){ /* For each wave */
6150: m=mw[mi][iind];
6151: if(j!=0){
6152: if(anyvaryingduminmodel==1){ /* Some are varying covariates */
6153: for (z1=1; z1<=cptcoveff; z1++) {
6154: if( Fixed[Tmodelind[z1]]==1){
6155: /* iv= Tvar[Tmodelind[z1]]-ncovcol-nqv; /\* Good *\/ */
6156: iv= Tvar[Tmodelind[z1]]; /* Good *//* because cotvar starts now at first at ncovcol+nqv+ntv */
6157: if (cotvar[m][iv][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]) /* iv=1 to ntv, right modality. If covariate's
6158: value is -1, we don't select. It differs from the
6159: constant and age model which counts them. */
6160: bool=0; /* not selected */
6161: }else if( Fixed[Tmodelind[z1]]== 0) { /* fixed */
6162: /* i1=Tvaraff[z1]; */
6163: /* i2=TnsdVar[i1]; */
6164: /* i3=nbcode[i1][i2]; */
6165: /* i4=covar[i1][iind]; */
6166: /* if(i4 != i3){ */
6167: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]) { /* Bug valgrind */
6168: bool=0;
6169: }
6170: }
6171: }
6172: }/* Some are varying covariates, we tried to speed up if all fixed covariates in the model, avoiding waves loop */
6173: } /* end j==0 */
6174: /* bool =0 we keep that guy which corresponds to the combination of dummy values */
6175: if(bool==1){ /*Selected */
6176: /* dh[m][iind] or dh[mw[mi][iind]][iind] is the delay between two effective (mi) waves m=mw[mi][iind]
6177: and mw[mi+1][iind]. dh depends on stepm. */
6178: agebegin=agev[m][iind]; /* Age at beginning of wave before transition*/
6179: ageend=agev[m][iind]+(dh[m][iind])*stepm/YEARM; /* Age at end of wave and transition */
6180: if(m >=firstpass && m <=lastpass){
6181: k2=anint[m][iind]+(mint[m][iind]/12.);
6182: /*if ((k2>=dateprev1) && (k2<=dateprev2)) {*/
6183: if(agev[m][iind]==0) agev[m][iind]=iagemax+1; /* All ages equal to 0 are in iagemax+1 */
6184: if(agev[m][iind]==1) agev[m][iind]=iagemax+2; /* All ages equal to 1 are in iagemax+2 */
6185: if (s[m][iind]>0 && s[m][iind]<=nlstate) /* If status at wave m is known and a live state */
6186: prop[s[m][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */
6187: if (m<lastpass) {
6188: /* if(s[m][iind]==4 && s[m+1][iind]==4) */
6189: /* 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]); */
6190: if(s[m][iind]==-1)
6191: 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.));
6192: freq[s[m][iind]][s[m+1][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */
6193: for (z1=1; z1<= nqfveff; z1++) { /* Quantitative variables, calculating mean on known values only */
6194: if(!isnan(covar[ncovcol+z1][iind])){
6195: idq[z1]=idq[z1]+weight[iind];
6196: meanq[z1]+=covar[ncovcol+z1][iind]*weight[iind]; /* Computes mean of quantitative with selected filter */
6197: /* stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]*weight[iind]; *//*error*/
6198: stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]; /* *weight[iind];*/ /* Computes mean of quantitative with selected filter */
6199: }
6200: }
6201: /* if((int)agev[m][iind] == 55) */
6202: /* printf("j=%d, j1=%d Age %d, iind=%d, num=%09ld m=%d\n",j,j1,(int)agev[m][iind],iind, num[iind],m); */
6203: /* freq[s[m][iind]][s[m+1][iind]][(int)((agebegin+ageend)/2.)] += weight[iind]; */
6204: 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 */
6205: }
6206: } /* end if between passes */
6207: if ((agev[m][iind]>1) && (agev[m][iind]< (iagemax+3)) && (anint[m][iind]!=9999) && (mint[m][iind]!=99) && (j==0)) {
6208: dateintsum=dateintsum+k2; /* on all covariates ?*/
6209: k2cpt++;
6210: /* printf("iind=%ld dateintmean = %lf dateintsum=%lf k2cpt=%lf k2=%lf\n",iind, dateintsum/k2cpt, dateintsum,k2cpt, k2); */
6211: }
6212: }else{
6213: bool=1;
6214: }/* end bool 2 */
6215: } /* end m */
6216: /* for (z1=1; z1<= nqfveff; z1++) { /\* Quantitative variables, calculating mean *\/ */
6217: /* idq[z1]=idq[z1]+weight[iind]; */
6218: /* meanq[z1]+=covar[ncovcol+z1][iind]*weight[iind]; /\* Computes mean of quantitative with selected filter *\/ */
6219: /* stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]*weight[iind]; /\* *weight[iind];*\/ /\* Computes mean of quantitative with selected filter *\/ */
6220: /* } */
6221: } /* end bool */
6222: } /* end iind = 1 to imx */
6223: /* prop[s][age] is fed for any initial and valid live state as well as
6224: freq[s1][s2][age] at single age of beginning the transition, for a combination j1 */
6225:
6226:
6227: /* fprintf(ficresp, "#Count between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);*/
6228: if(cptcoveff==0 && nj==1) /* no covariate and first pass */
6229: pstamp(ficresp);
6230: if (cptcoveff>0 && j!=0){
6231: pstamp(ficresp);
6232: printf( "\n#********** Variable ");
6233: fprintf(ficresp, "\n#********** Variable ");
6234: fprintf(ficresphtm, "\n<br/><br/><h3>********** Variable ");
6235: fprintf(ficresphtmfr, "\n<br/><br/><h3>********** Variable ");
6236: fprintf(ficlog, "\n#********** Variable ");
6237: for (z1=1; z1<=cptcoveff; z1++){
6238: if(!FixedV[Tvaraff[z1]]){
6239: printf( "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
6240: fprintf(ficresp, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
6241: fprintf(ficresphtm, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
6242: fprintf(ficresphtmfr, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
6243: fprintf(ficlog, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
6244: }else{
6245: printf( "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
6246: fprintf(ficresp, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
6247: fprintf(ficresphtm, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
6248: fprintf(ficresphtmfr, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
6249: fprintf(ficlog, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
6250: }
6251: }
6252: printf( "**********\n#");
6253: fprintf(ficresp, "**********\n#");
6254: fprintf(ficresphtm, "**********</h3>\n");
6255: fprintf(ficresphtmfr, "**********</h3>\n");
6256: fprintf(ficlog, "**********\n");
6257: }
6258: /*
6259: Printing means of quantitative variables if any
6260: */
6261: for (z1=1; z1<= nqfveff; z1++) {
6262: fprintf(ficlog,"Mean of fixed quantitative variable V%d on %.3g (weighted) individuals sum=%f", ncovcol+z1, idq[z1], meanq[z1]);
6263: fprintf(ficlog,", mean=%.3g\n",meanq[z1]/idq[z1]);
6264: if(weightopt==1){
6265: printf(" Weighted mean and standard deviation of");
6266: fprintf(ficlog," Weighted mean and standard deviation of");
6267: fprintf(ficresphtmfr," Weighted mean and standard deviation of");
6268: }
6269: /* mu = \frac{w x}{\sum w}
6270: var = \frac{\sum w (x-mu)^2}{\sum w} = \frac{w x^2}{\sum w} - mu^2
6271: */
6272: printf(" fixed quantitative variable V%d on %.3g (weighted) representatives of the population : %8.5g (%8.5g)\n", ncovcol+z1, idq[z1],meanq[z1]/idq[z1], sqrt(stdq[z1]/idq[z1]-meanq[z1]*meanq[z1]/idq[z1]/idq[z1]));
6273: fprintf(ficlog," fixed quantitative variable V%d on %.3g (weighted) representatives of the population : %8.5g (%8.5g)\n", ncovcol+z1, idq[z1],meanq[z1]/idq[z1], sqrt(stdq[z1]/idq[z1]-meanq[z1]*meanq[z1]/idq[z1]/idq[z1]));
6274: fprintf(ficresphtmfr," fixed quantitative variable V%d on %.3g (weighted) representatives of the population : %8.5g (%8.5g)<p>\n", ncovcol+z1, idq[z1],meanq[z1]/idq[z1], sqrt(stdq[z1]/idq[z1]-meanq[z1]*meanq[z1]/idq[z1]/idq[z1]));
6275: }
6276: /* for (z1=1; z1<= nqtveff; z1++) { */
6277: /* for(m=1;m<=lastpass;m++){ */
6278: /* fprintf(ficresphtmfr,"V quantitative id %d, pass id=%d, mean=%f<p>\n", z1, m, meanqt[m][z1]); */
6279: /* } */
6280: /* } */
6281:
6282: fprintf(ficresphtm,"<table style=\"text-align:center; border: 1px solid\">");
6283: if((cptcoveff==0 && nj==1)|| nj==2 ) /* no covariate and first pass */
6284: fprintf(ficresp, " Age");
6285: if(nj==2) for (z1=1; z1<=cptcoveff; z1++) {
6286: printf(" V%d=%d, z1=%d, Tvaraff[z1]=%d, j1=%d, TnsdVar[Tvaraff[%d]]=%d |",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])], z1, Tvaraff[z1], j1,z1,TnsdVar[Tvaraff[z1]]);
6287: fprintf(ficresp, " V%d=%d",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
6288: }
6289: for(i=1; i<=nlstate;i++) {
6290: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp," Prev(%d) N(%d) N ",i,i);
6291: fprintf(ficresphtm, "<th>Age</th><th>Prev(%d)</th><th>N(%d)</th><th>N</th>",i,i);
6292: }
6293: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp, "\n");
6294: fprintf(ficresphtm, "\n");
6295:
6296: /* Header of frequency table by age */
6297: fprintf(ficresphtmfr,"<table style=\"text-align:center; border: 1px solid\">");
6298: fprintf(ficresphtmfr,"<th>Age</th> ");
6299: for(s2=-1; s2 <=nlstate+ndeath; s2++){
6300: for(m=-1; m <=nlstate+ndeath; m++){
6301: if(s2!=0 && m!=0)
6302: fprintf(ficresphtmfr,"<th>%d%d</th> ",s2,m);
6303: }
6304: }
6305: fprintf(ficresphtmfr, "\n");
6306:
6307: /* For each age */
6308: for(iage=iagemin; iage <= iagemax+3; iage++){
6309: fprintf(ficresphtm,"<tr>");
6310: if(iage==iagemax+1){
6311: fprintf(ficlog,"1");
6312: fprintf(ficresphtmfr,"<tr><th>0</th> ");
6313: }else if(iage==iagemax+2){
6314: fprintf(ficlog,"0");
6315: fprintf(ficresphtmfr,"<tr><th>Unknown</th> ");
6316: }else if(iage==iagemax+3){
6317: fprintf(ficlog,"Total");
6318: fprintf(ficresphtmfr,"<tr><th>Total</th> ");
6319: }else{
6320: if(first==1){
6321: first=0;
6322: printf("See log file for details...\n");
6323: }
6324: fprintf(ficresphtmfr,"<tr><th>%d</th> ",iage);
6325: fprintf(ficlog,"Age %d", iage);
6326: }
6327: for(s1=1; s1 <=nlstate ; s1++){
6328: for(m=-1, pp[s1]=0; m <=nlstate+ndeath ; m++)
6329: pp[s1] += freq[s1][m][iage];
6330: }
6331: for(s1=1; s1 <=nlstate ; s1++){
6332: for(m=-1, pos=0; m <=0 ; m++)
6333: pos += freq[s1][m][iage];
6334: if(pp[s1]>=1.e-10){
6335: if(first==1){
6336: printf(" %d.=%.0f loss[%d]=%.1f%%",s1,pp[s1],s1,100*pos/pp[s1]);
6337: }
6338: fprintf(ficlog," %d.=%.0f loss[%d]=%.1f%%",s1,pp[s1],s1,100*pos/pp[s1]);
6339: }else{
6340: if(first==1)
6341: printf(" %d.=%.0f loss[%d]=NaNQ%%",s1,pp[s1],s1);
6342: fprintf(ficlog," %d.=%.0f loss[%d]=NaNQ%%",s1,pp[s1],s1);
6343: }
6344: }
6345:
6346: for(s1=1; s1 <=nlstate ; s1++){
6347: /* posprop[s1]=0; */
6348: for(m=0, pp[s1]=0; m <=nlstate+ndeath; m++)/* Summing on all ages */
6349: pp[s1] += freq[s1][m][iage];
6350: } /* pp[s1] is the total number of transitions starting from state s1 and any ending status until this age */
6351:
6352: for(s1=1,pos=0, pospropta=0.; s1 <=nlstate ; s1++){
6353: pos += pp[s1]; /* pos is the total number of transitions until this age */
6354: posprop[s1] += prop[s1][iage]; /* prop is the number of transitions from a live state
6355: from s1 at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
6356: pospropta += prop[s1][iage]; /* prop is the number of transitions from a live state
6357: from s1 at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
6358: }
6359:
6360: /* Writing ficresp */
6361: if(cptcoveff==0 && nj==1){ /* no covariate and first pass */
6362: if( iage <= iagemax){
6363: fprintf(ficresp," %d",iage);
6364: }
6365: }else if( nj==2){
6366: if( iage <= iagemax){
6367: fprintf(ficresp," %d",iage);
6368: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresp, " %d %d",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
6369: }
6370: }
6371: for(s1=1; s1 <=nlstate ; s1++){
6372: if(pos>=1.e-5){
6373: if(first==1)
6374: printf(" %d.=%.0f prev[%d]=%.1f%%",s1,pp[s1],s1,100*pp[s1]/pos);
6375: fprintf(ficlog," %d.=%.0f prev[%d]=%.1f%%",s1,pp[s1],s1,100*pp[s1]/pos);
6376: }else{
6377: if(first==1)
6378: printf(" %d.=%.0f prev[%d]=NaNQ%%",s1,pp[s1],s1);
6379: fprintf(ficlog," %d.=%.0f prev[%d]=NaNQ%%",s1,pp[s1],s1);
6380: }
6381: if( iage <= iagemax){
6382: if(pos>=1.e-5){
6383: if(cptcoveff==0 && nj==1){ /* no covariate and first pass */
6384: fprintf(ficresp," %.5f %.0f %.0f",prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
6385: }else if( nj==2){
6386: fprintf(ficresp," %.5f %.0f %.0f",prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
6387: }
6388: fprintf(ficresphtm,"<th>%d</th><td>%.5f</td><td>%.0f</td><td>%.0f</td>",iage,prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
6389: /*probs[iage][s1][j1]= pp[s1]/pos;*/
6390: /*printf("\niage=%d s1=%d j1=%d %.5f %.0f %.0f %f",iage,s1,j1,pp[s1]/pos, pp[s1],pos,probs[iage][s1][j1]);*/
6391: } else{
6392: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp," NaNq %.0f %.0f",prop[s1][iage],pospropta);
6393: fprintf(ficresphtm,"<th>%d</th><td>NaNq</td><td>%.0f</td><td>%.0f</td>",iage, prop[s1][iage],pospropta);
6394: }
6395: }
6396: pospropt[s1] +=posprop[s1];
6397: } /* end loop s1 */
6398: /* pospropt=0.; */
6399: for(s1=-1; s1 <=nlstate+ndeath; s1++){
6400: for(m=-1; m <=nlstate+ndeath; m++){
6401: if(freq[s1][m][iage] !=0 ) { /* minimizing output */
6402: if(first==1){
6403: printf(" %d%d=%.0f",s1,m,freq[s1][m][iage]);
6404: }
6405: /* printf(" %d%d=%.0f",s1,m,freq[s1][m][iage]); */
6406: fprintf(ficlog," %d%d=%.0f",s1,m,freq[s1][m][iage]);
6407: }
6408: if(s1!=0 && m!=0)
6409: fprintf(ficresphtmfr,"<td>%.0f</td> ",freq[s1][m][iage]);
6410: }
6411: } /* end loop s1 */
6412: posproptt=0.;
6413: for(s1=1; s1 <=nlstate; s1++){
6414: posproptt += pospropt[s1];
6415: }
6416: fprintf(ficresphtmfr,"</tr>\n ");
6417: fprintf(ficresphtm,"</tr>\n");
6418: if((cptcoveff==0 && nj==1)|| nj==2 ) {
6419: if(iage <= iagemax)
6420: fprintf(ficresp,"\n");
6421: }
6422: if(first==1)
6423: printf("Others in log...\n");
6424: fprintf(ficlog,"\n");
6425: } /* end loop age iage */
6426:
6427: fprintf(ficresphtm,"<tr><th>Tot</th>");
6428: for(s1=1; s1 <=nlstate ; s1++){
6429: if(posproptt < 1.e-5){
6430: fprintf(ficresphtm,"<td>Nanq</td><td>%.0f</td><td>%.0f</td>",pospropt[s1],posproptt);
6431: }else{
6432: fprintf(ficresphtm,"<td>%.5f</td><td>%.0f</td><td>%.0f</td>",pospropt[s1]/posproptt,pospropt[s1],posproptt);
6433: }
6434: }
6435: fprintf(ficresphtm,"</tr>\n");
6436: fprintf(ficresphtm,"</table>\n");
6437: fprintf(ficresphtmfr,"</table>\n");
6438: if(posproptt < 1.e-5){
6439: fprintf(ficresphtm,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
6440: fprintf(ficresphtmfr,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
6441: fprintf(ficlog,"# This combination (%d) is not valid and no result will be produced\n",j1);
6442: printf("# This combination (%d) is not valid and no result will be produced\n",j1);
6443: invalidvarcomb[j1]=1;
6444: }else{
6445: fprintf(ficresphtm,"\n <p> This combination (%d) is valid and result will be produced (or no resultline).</p>",j1);
6446: invalidvarcomb[j1]=0;
6447: }
6448: fprintf(ficresphtmfr,"</table>\n");
6449: fprintf(ficlog,"\n");
6450: if(j!=0){
6451: printf("#Freqsummary: Starting values for combination j1=%d:\n", j1);
6452: for(i=1,s1=1; i <=nlstate; i++){
6453: for(k=1; k <=(nlstate+ndeath); k++){
6454: if (k != i) {
6455: for(jj=1; jj <=ncovmodel; jj++){ /* For counting s1 */
6456: if(jj==1){ /* Constant case (in fact cste + age) */
6457: if(j1==1){ /* All dummy covariates to zero */
6458: freq[i][k][iagemax+4]=freq[i][k][iagemax+3]; /* Stores case 0 0 0 */
6459: freq[i][i][iagemax+4]=freq[i][i][iagemax+3]; /* Stores case 0 0 0 */
6460: printf("%d%d ",i,k);
6461: fprintf(ficlog,"%d%d ",i,k);
6462: printf("%12.7f ln(%.0f/%.0f)= %f, OR=%f sd=%f \n",p[s1],freq[i][k][iagemax+3],freq[i][i][iagemax+3], log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]),freq[i][k][iagemax+3]/freq[i][i][iagemax+3], sqrt(1/freq[i][k][iagemax+3]+1/freq[i][i][iagemax+3]));
6463: fprintf(ficlog,"%12.7f ln(%.0f/%.0f)= %12.7f \n",p[s1],freq[i][k][iagemax+3],freq[i][i][iagemax+3], log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]));
6464: pstart[s1]= log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]);
6465: }
6466: }else if((j1==1) && (jj==2 || nagesqr==1)){ /* age or age*age parameter without covariate V4*age (to be done later) */
6467: for(iage=iagemin; iage <= iagemax+3; iage++){
6468: x[iage]= (double)iage;
6469: y[iage]= log(freq[i][k][iage]/freq[i][i][iage]);
6470: /* printf("i=%d, k=%d, s1=%d, j1=%d, jj=%d, y[%d]=%f\n",i,k,s1,j1,jj, iage, y[iage]); */
6471: }
6472: /* Some are not finite, but linreg will ignore these ages */
6473: no=0;
6474: linreg(iagemin,iagemax,&no,x,y,&a,&b,&r, &sa, &sb ); /* y= a+b*x with standard errors */
6475: pstart[s1]=b;
6476: pstart[s1-1]=a;
6477: }else if( j1!=1 && (j1==2 || (log(j1-1.)/log(2.)-(int)(log(j1-1.)/log(2.))) <0.010) && ( TvarsDind[(int)(log(j1-1.)/log(2.))+1]+2+nagesqr == jj) && Dummy[jj-2-nagesqr]==0){ /* We want only if the position, jj, in model corresponds to unique covariate equal to 1 in j1 combination */
6478: printf("j1=%d, jj=%d, (int)(log(j1-1.)/log(2.))+1=%d, TvarsDind[(int)(log(j1-1.)/log(2.))+1]=%d\n",j1, jj,(int)(log(j1-1.)/log(2.))+1,TvarsDind[(int)(log(j1-1.)/log(2.))+1]);
6479: printf("j1=%d, jj=%d, (log(j1-1.)/log(2.))+1=%f, TvarsDind[(int)(log(j1-1.)/log(2.))+1]=%d\n",j1, jj,(log(j1-1.)/log(2.))+1,TvarsDind[(int)(log(j1-1.)/log(2.))+1]);
6480: pstart[s1]= log((freq[i][k][iagemax+3]/freq[i][i][iagemax+3])/(freq[i][k][iagemax+4]/freq[i][i][iagemax+4]));
6481: printf("%d%d ",i,k);
6482: fprintf(ficlog,"%d%d ",i,k);
6483: printf("s1=%d,i=%d,k=%d,p[%d]=%12.7f ln((%.0f/%.0f)/(%.0f/%.0f))= %f, OR=%f sd=%f \n",s1,i,k,s1,p[s1],freq[i][k][iagemax+3],freq[i][i][iagemax+3],freq[i][k][iagemax+4],freq[i][i][iagemax+4], log((freq[i][k][iagemax+3]/freq[i][i][iagemax+3])/(freq[i][k][iagemax+4]/freq[i][i][iagemax+4])),(freq[i][k][iagemax+3]/freq[i][i][iagemax+3])/(freq[i][k][iagemax+4]/freq[i][i][iagemax+4]), sqrt(1/freq[i][k][iagemax+3]+1/freq[i][i][iagemax+3]+1/freq[i][k][iagemax+4]+1/freq[i][i][iagemax+4]));
6484: }else{ /* Other cases, like quantitative fixed or varying covariates */
6485: ;
6486: }
6487: /* printf("%12.7f )", param[i][jj][k]); */
6488: /* fprintf(ficlog,"%12.7f )", param[i][jj][k]); */
6489: s1++;
6490: } /* end jj */
6491: } /* end k!= i */
6492: } /* end k */
6493: } /* end i, s1 */
6494: } /* end j !=0 */
6495: } /* end selected combination of covariate j1 */
6496: if(j==0){ /* We can estimate starting values from the occurences in each case */
6497: printf("#Freqsummary: Starting values for the constants:\n");
6498: fprintf(ficlog,"\n");
6499: for(i=1,s1=1; i <=nlstate; i++){
6500: for(k=1; k <=(nlstate+ndeath); k++){
6501: if (k != i) {
6502: printf("%d%d ",i,k);
6503: fprintf(ficlog,"%d%d ",i,k);
6504: for(jj=1; jj <=ncovmodel; jj++){
6505: pstart[s1]=p[s1]; /* Setting pstart to p values by default */
6506: if(jj==1){ /* Age has to be done */
6507: pstart[s1]= log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]);
6508: printf("%12.7f ln(%.0f/%.0f)= %12.7f ",p[s1],freq[i][k][iagemax+3],freq[i][i][iagemax+3], log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]));
6509: fprintf(ficlog,"%12.7f ln(%.0f/%.0f)= %12.7f ",p[s1],freq[i][k][iagemax+3],freq[i][i][iagemax+3], log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]));
6510: }
6511: /* printf("%12.7f )", param[i][jj][k]); */
6512: /* fprintf(ficlog,"%12.7f )", param[i][jj][k]); */
6513: s1++;
6514: }
6515: printf("\n");
6516: fprintf(ficlog,"\n");
6517: }
6518: }
6519: } /* end of state i */
6520: printf("#Freqsummary\n");
6521: fprintf(ficlog,"\n");
6522: for(s1=-1; s1 <=nlstate+ndeath; s1++){
6523: for(s2=-1; s2 <=nlstate+ndeath; s2++){
6524: /* param[i]|j][k]= freq[s1][s2][iagemax+3] */
6525: printf(" %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]);
6526: fprintf(ficlog," %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]);
6527: /* if(freq[s1][s2][iage] !=0 ) { /\* minimizing output *\/ */
6528: /* printf(" %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]); */
6529: /* fprintf(ficlog," %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]); */
6530: /* } */
6531: }
6532: } /* end loop s1 */
6533:
6534: printf("\n");
6535: fprintf(ficlog,"\n");
6536: } /* end j=0 */
6537: } /* end j */
6538:
6539: if(mle == -2){ /* We want to use these values as starting values */
6540: for(i=1, jk=1; i <=nlstate; i++){
6541: for(j=1; j <=nlstate+ndeath; j++){
6542: if(j!=i){
6543: /*ca[0]= k+'a'-1;ca[1]='\0';*/
6544: printf("%1d%1d",i,j);
6545: fprintf(ficparo,"%1d%1d",i,j);
6546: for(k=1; k<=ncovmodel;k++){
6547: /* printf(" %lf",param[i][j][k]); */
6548: /* fprintf(ficparo," %lf",param[i][j][k]); */
6549: p[jk]=pstart[jk];
6550: printf(" %f ",pstart[jk]);
6551: fprintf(ficparo," %f ",pstart[jk]);
6552: jk++;
6553: }
6554: printf("\n");
6555: fprintf(ficparo,"\n");
6556: }
6557: }
6558: }
6559: } /* end mle=-2 */
6560: dateintmean=dateintsum/k2cpt;
6561: date2dmy(dateintmean,&jintmean,&mintmean,&aintmean);
6562:
6563: fclose(ficresp);
6564: fclose(ficresphtm);
6565: fclose(ficresphtmfr);
6566: free_vector(idq,1,nqfveff);
6567: free_vector(meanq,1,nqfveff);
6568: free_vector(stdq,1,nqfveff);
6569: free_matrix(meanqt,1,lastpass,1,nqtveff);
6570: free_vector(x, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
6571: free_vector(y, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
6572: free_ma3x(freq,-5,nlstate+ndeath,-5,nlstate+ndeath, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
6573: free_vector(pospropt,1,nlstate);
6574: free_vector(posprop,1,nlstate);
6575: free_matrix(prop,1,nlstate,iagemin-AGEMARGE, iagemax+4+AGEMARGE);
6576: free_vector(pp,1,nlstate);
6577: /* End of freqsummary */
6578: }
6579:
6580: /* Simple linear regression */
6581: int linreg(int ifi, int ila, int *no, const double x[], const double y[], double* a, double* b, double* r, double* sa, double * sb) {
6582:
6583: /* y=a+bx regression */
6584: double sumx = 0.0; /* sum of x */
6585: double sumx2 = 0.0; /* sum of x**2 */
6586: double sumxy = 0.0; /* sum of x * y */
6587: double sumy = 0.0; /* sum of y */
6588: double sumy2 = 0.0; /* sum of y**2 */
6589: double sume2 = 0.0; /* sum of square or residuals */
6590: double yhat;
6591:
6592: double denom=0;
6593: int i;
6594: int ne=*no;
6595:
6596: for ( i=ifi, ne=0;i<=ila;i++) {
6597: if(!isfinite(x[i]) || !isfinite(y[i])){
6598: /* printf(" x[%d]=%f, y[%d]=%f\n",i,x[i],i,y[i]); */
6599: continue;
6600: }
6601: ne=ne+1;
6602: sumx += x[i];
6603: sumx2 += x[i]*x[i];
6604: sumxy += x[i] * y[i];
6605: sumy += y[i];
6606: sumy2 += y[i]*y[i];
6607: denom = (ne * sumx2 - sumx*sumx);
6608: /* printf("ne=%d, i=%d,x[%d]=%f, y[%d]=%f sumx=%f, sumx2=%f, sumxy=%f, sumy=%f, sumy2=%f, denom=%f\n",ne,i,i,x[i],i,y[i], sumx, sumx2,sumxy, sumy, sumy2,denom); */
6609: }
6610:
6611: denom = (ne * sumx2 - sumx*sumx);
6612: if (denom == 0) {
6613: // vertical, slope m is infinity
6614: *b = INFINITY;
6615: *a = 0;
6616: if (r) *r = 0;
6617: return 1;
6618: }
6619:
6620: *b = (ne * sumxy - sumx * sumy) / denom;
6621: *a = (sumy * sumx2 - sumx * sumxy) / denom;
6622: if (r!=NULL) {
6623: *r = (sumxy - sumx * sumy / ne) / /* compute correlation coeff */
6624: sqrt((sumx2 - sumx*sumx/ne) *
6625: (sumy2 - sumy*sumy/ne));
6626: }
6627: *no=ne;
6628: for ( i=ifi, ne=0;i<=ila;i++) {
6629: if(!isfinite(x[i]) || !isfinite(y[i])){
6630: /* printf(" x[%d]=%f, y[%d]=%f\n",i,x[i],i,y[i]); */
6631: continue;
6632: }
6633: ne=ne+1;
6634: yhat = y[i] - *a -*b* x[i];
6635: sume2 += yhat * yhat ;
6636:
6637: denom = (ne * sumx2 - sumx*sumx);
6638: /* printf("ne=%d, i=%d,x[%d]=%f, y[%d]=%f sumx=%f, sumx2=%f, sumxy=%f, sumy=%f, sumy2=%f, denom=%f\n",ne,i,i,x[i],i,y[i], sumx, sumx2,sumxy, sumy, sumy2,denom); */
6639: }
6640: *sb = sqrt(sume2/(double)(ne-2)/(sumx2 - sumx * sumx /(double)ne));
6641: *sa= *sb * sqrt(sumx2/ne);
6642:
6643: return 0;
6644: }
6645:
6646: /************ Prevalence ********************/
6647: 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)
6648: {
6649: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
6650: in each health status at the date of interview (if between dateprev1 and dateprev2).
6651: We still use firstpass and lastpass as another selection.
6652: */
6653:
6654: int i, m, jk, j1, bool, z1,j, iv;
6655: int mi; /* Effective wave */
6656: int iage;
6657: double agebegin, ageend;
6658:
6659: double **prop;
6660: double posprop;
6661: double y2; /* in fractional years */
6662: int iagemin, iagemax;
6663: int first; /** to stop verbosity which is redirected to log file */
6664:
6665: iagemin= (int) agemin;
6666: iagemax= (int) agemax;
6667: /*pp=vector(1,nlstate);*/
6668: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
6669: /* freq=ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,iagemin,iagemax+3);*/
6670: j1=0;
6671:
6672: /*j=cptcoveff;*/
6673: if (cptcovn<1) {j=1;ncodemax[1]=1;}
6674:
6675: first=0;
6676: for(j1=1; j1<= (int) pow(2,cptcoveff);j1++){ /* For each combination of simple dummy covariates */
6677: for (i=1; i<=nlstate; i++)
6678: for(iage=iagemin-AGEMARGE; iage <= iagemax+4+AGEMARGE; iage++)
6679: prop[i][iage]=0.0;
6680: printf("Prevalence combination of varying and fixed dummies %d\n",j1);
6681: /* fprintf(ficlog," V%d=%d ",Tvaraff[j1],nbcode[Tvaraff[j1]][codtabm(k,j1)]); */
6682: fprintf(ficlog,"Prevalence combination of varying and fixed dummies %d\n",j1);
6683:
6684: for (i=1; i<=imx; i++) { /* Each individual */
6685: bool=1;
6686: /* for(m=firstpass; m<=lastpass; m++){/\* Other selection (we can limit to certain interviews*\/ */
6687: for(mi=1; mi<wav[i];mi++){ /* For this wave too look where individual can be counted V4=0 V3=0 */
6688: m=mw[mi][i];
6689: /* Tmodelind[z1]=k is the position of the varying covariate in the model, but which # within 1 to ntv? */
6690: /* Tvar[Tmodelind[z1]] is the n of Vn; n-ncovcol-nqv is the first time varying covariate or iv */
6691: for (z1=1; z1<=cptcoveff; z1++){
6692: if( Fixed[Tmodelind[z1]]==1){
6693: iv= Tvar[Tmodelind[z1]];/* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) */
6694: if (cotvar[m][iv][i]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]) /* iv=1 to ntv, right modality */
6695: bool=0;
6696: }else if( Fixed[Tmodelind[z1]]== 0) /* fixed */
6697: if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]) {
6698: bool=0;
6699: }
6700: }
6701: if(bool==1){ /* Otherwise we skip that wave/person */
6702: agebegin=agev[m][i]; /* Age at beginning of wave before transition*/
6703: /* ageend=agev[m][i]+(dh[m][i])*stepm/YEARM; /\* Age at end of wave and transition *\/ */
6704: if(m >=firstpass && m <=lastpass){
6705: y2=anint[m][i]+(mint[m][i]/12.); /* Fractional date in year */
6706: if ((y2>=dateprev1) && (y2<=dateprev2)) { /* Here is the main selection (fractional years) */
6707: if(agev[m][i]==0) agev[m][i]=iagemax+1;
6708: if(agev[m][i]==1) agev[m][i]=iagemax+2;
6709: if((int)agev[m][i] <iagemin-AGEMARGE || (int)agev[m][i] >iagemax+4+AGEMARGE){
6710: 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);
6711: exit(1);
6712: }
6713: if (s[m][i]>0 && s[m][i]<=nlstate) {
6714: /*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]]);*/
6715: prop[s[m][i]][(int)agev[m][i]] += weight[i];/* At age of beginning of transition, where status is known */
6716: prop[s[m][i]][iagemax+3] += weight[i];
6717: } /* end valid statuses */
6718: } /* end selection of dates */
6719: } /* end selection of waves */
6720: } /* end bool */
6721: } /* end wave */
6722: } /* end individual */
6723: for(i=iagemin; i <= iagemax+3; i++){
6724: for(jk=1,posprop=0; jk <=nlstate ; jk++) {
6725: posprop += prop[jk][i];
6726: }
6727:
6728: for(jk=1; jk <=nlstate ; jk++){
6729: if( i <= iagemax){
6730: if(posprop>=1.e-5){
6731: probs[i][jk][j1]= prop[jk][i]/posprop;
6732: } else{
6733: if(!first){
6734: first=1;
6735: printf("Warning Observed prevalence doesn't sum to 1 for state %d: probs[%d][%d][%d]=%lf because of lack of cases\nSee others in log file...\n",jk,i,jk, j1,probs[i][jk][j1]);
6736: }else{
6737: fprintf(ficlog,"Warning Observed prevalence doesn't sum to 1 for state %d: probs[%d][%d][%d]=%lf because of lack of cases.\n",jk,i,jk, j1,probs[i][jk][j1]);
6738: }
6739: }
6740: }
6741: }/* end jk */
6742: }/* end i */
6743: /*} *//* end i1 */
6744: } /* end j1 */
6745:
6746: /* free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath, iagemin, iagemax+3);*/
6747: /*free_vector(pp,1,nlstate);*/
6748: free_matrix(prop,1,nlstate, iagemin-AGEMARGE,iagemax+4+AGEMARGE);
6749: } /* End of prevalence */
6750:
6751: /************* Waves Concatenation ***************/
6752:
6753: 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)
6754: {
6755: /* Concatenates waves: wav[i] is the number of effective (useful waves in the sense that a non interview is useless) of individual i.
6756: Death is a valid wave (if date is known).
6757: mw[mi][i] is the mi (mi=1 to wav[i]) effective wave of individual i
6758: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
6759: and mw[mi+1][i]. dh depends on stepm. s[m][i] exists for any wave from firstpass to lastpass
6760: */
6761:
6762: int i=0, mi=0, m=0, mli=0;
6763: /* int j, k=0,jk, ju, jl,jmin=1e+5, jmax=-1;
6764: double sum=0., jmean=0.;*/
6765: int first=0, firstwo=0, firsthree=0, firstfour=0, firstfiv=0;
6766: int j, k=0,jk, ju, jl;
6767: double sum=0.;
6768: first=0;
6769: firstwo=0;
6770: firsthree=0;
6771: firstfour=0;
6772: jmin=100000;
6773: jmax=-1;
6774: jmean=0.;
6775:
6776: /* Treating live states */
6777: for(i=1; i<=imx; i++){ /* For simple cases and if state is death */
6778: mi=0; /* First valid wave */
6779: mli=0; /* Last valid wave */
6780: m=firstpass; /* Loop on waves */
6781: while(s[m][i] <= nlstate){ /* a live state or unknown state */
6782: 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 */
6783: mli=m-1;/* mw[++mi][i]=m-1; */
6784: }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 */
6785: mw[++mi][i]=m; /* Valid wave: incrementing mi and updating mi; mw[mi] is the wave number of mi_th valid transition */
6786: mli=m;
6787: } /* else might be a useless wave -1 and mi is not incremented and mw[mi] not updated */
6788: if(m < lastpass){ /* m < lastpass, standard case */
6789: m++; /* mi gives the "effective" current wave, m the current wave, go to next wave by incrementing m */
6790: }
6791: else{ /* m = lastpass, eventual special issue with warning */
6792: #ifdef UNKNOWNSTATUSNOTCONTRIBUTING
6793: break;
6794: #else
6795: if(s[m][i]==-1 && (int) andc[i] == 9999 && (int)anint[m][i] != 9999){ /* no death date and known date of interview, case -2 (vital status unknown is warned later */
6796: if(firsthree == 0){
6797: 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 1-p_{%d%d} .\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, s[m][i], nlstate+ndeath);
6798: firsthree=1;
6799: }else if(firsthree >=1 && firsthree < 10){
6800: 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 1-p_{%d%d} .\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, s[m][i], nlstate+ndeath);
6801: firsthree++;
6802: }else if(firsthree == 10){
6803: printf("Information, too many Information flags: no more reported to log either\n");
6804: fprintf(ficlog,"Information, too many Information flags: no more reported to log either\n");
6805: firsthree++;
6806: }else{
6807: firsthree++;
6808: }
6809: mw[++mi][i]=m; /* Valid transition with unknown status */
6810: mli=m;
6811: }
6812: if(s[m][i]==-2){ /* Vital status is really unknown */
6813: nbwarn++;
6814: if((int)anint[m][i] == 9999){ /* Has the vital status really been verified?not a transition */
6815: 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);
6816: 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);
6817: }
6818: break;
6819: }
6820: break;
6821: #endif
6822: }/* End m >= lastpass */
6823: }/* end while */
6824:
6825: /* mi is the last effective wave, m is lastpass, mw[j][i] gives the # of j-th effective wave for individual i */
6826: /* After last pass */
6827: /* Treating death states */
6828: if (s[m][i] > nlstate){ /* In a death state */
6829: /* if( mint[m][i]==mdc[m][i] && anint[m][i]==andc[m][i]){ /\* same date of death and date of interview *\/ */
6830: /* } */
6831: mi++; /* Death is another wave */
6832: /* if(mi==0) never been interviewed correctly before death */
6833: /* Only death is a correct wave */
6834: mw[mi][i]=m;
6835: } /* else not in a death state */
6836: #ifndef DISPATCHINGKNOWNDEATHAFTERLASTWAVE
6837: else if ((int) andc[i] != 9999) { /* Date of death is known */
6838: if ((int)anint[m][i]!= 9999) { /* date of last interview is known */
6839: if((andc[i]+moisdc[i]/12.) <=(anint[m][i]+mint[m][i]/12.)){ /* month of death occured before last wave month and status should have been death instead of -1 */
6840: nbwarn++;
6841: if(firstfiv==0){
6842: printf("Warning! Death for individual %ld line=%d occurred at %d/%d before last wave %d, interviewed on %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 );
6843: firstfiv=1;
6844: }else{
6845: fprintf(ficlog,"Warning! Death for individual %ld line=%d occurred at %d/%d before last wave %d, interviewed on %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 );
6846: }
6847: s[m][i]=nlstate+1; /* Fixing the status as death. Be careful if multiple death states */
6848: }else{ /* Month of Death occured afer last wave month, potential bias */
6849: nberr++;
6850: if(firstwo==0){
6851: printf("Error! Death for individual %ld line=%d occurred at %d/%d after last wave %d interviewed at %d/%d with status %d. Potential bias if other individuals are still alive on this date but ignored. This case (%d)/wave (%d) is skipped, no contribution to likelihood. Please add a new fictitious wave at the date of last vital status scan, with a dead status. See documentation\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 );
6852: firstwo=1;
6853: }
6854: fprintf(ficlog,"Error! Death for individual %ld line=%d occurred at %d/%d after last wave %d interviewed at %d/%d with status %d. Potential bias if other individuals are still alive on this date but ignored. This case (%d)/wave (%d) is skipped, no contribution to likelihood. Please add a new fictitious wave at the date of last vital status scan, with a dead status. See documentation\n\n",num[i],i,(int) moisdc[i], (int) andc[i], lastpass,(int)mint[m][i],(int)anint[m][i], s[m][i], i,m );
6855: }
6856: }else{ /* if date of interview is unknown */
6857: /* death is known but not confirmed by death status at any wave */
6858: if(firstfour==0){
6859: 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 with status %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], s[m][i], i,m );
6860: firstfour=1;
6861: }
6862: 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 with status %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], s[m][i], i,m );
6863: }
6864: } /* end if date of death is known */
6865: #endif
6866: wav[i]=mi; /* mi should be the last effective wave (or mli), */
6867: /* wav[i]=mw[mi][i]; */
6868: if(mi==0){
6869: nbwarn++;
6870: if(first==0){
6871: printf("Warning! No valid information for individual %ld line=%d (skipped) and may be others, see log file\n",num[i],i);
6872: first=1;
6873: }
6874: if(first==1){
6875: fprintf(ficlog,"Warning! No valid information for individual %ld line=%d (skipped)\n",num[i],i);
6876: }
6877: } /* end mi==0 */
6878: } /* End individuals */
6879: /* wav and mw are no more changed */
6880:
6881: printf("Information, you have to check %d informations which haven't been logged!\n",firsthree);
6882: fprintf(ficlog,"Information, you have to check %d informations which haven't been logged!\n",firsthree);
6883:
6884:
6885: for(i=1; i<=imx; i++){
6886: for(mi=1; mi<wav[i];mi++){
6887: if (stepm <=0)
6888: dh[mi][i]=1;
6889: else{
6890: if (s[mw[mi+1][i]][i] > nlstate) { /* A death, but what if date is unknown? */
6891: if (agedc[i] < 2*AGESUP) {
6892: j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12);
6893: if(j==0) j=1; /* Survives at least one month after exam */
6894: else if(j<0){
6895: nberr++;
6896: printf("Error! Negative delay (%d to death) between waves %d and %d of individual %ld (around 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]);
6897: j=1; /* Temporary Dangerous patch */
6898: 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);
6899: fprintf(ficlog,"Error! Negative delay (%d to death) between waves %d and %d of individual %ld (around 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]);
6900: 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);
6901: }
6902: k=k+1;
6903: if (j >= jmax){
6904: jmax=j;
6905: ijmax=i;
6906: }
6907: if (j <= jmin){
6908: jmin=j;
6909: ijmin=i;
6910: }
6911: sum=sum+j;
6912: /*if (j<0) printf("j=%d num=%d \n",j,i);*/
6913: /* printf("%d %d %d %d\n", s[mw[mi][i]][i] ,s[mw[mi+1][i]][i],j,i);*/
6914: }
6915: }
6916: else{
6917: j= rint( (agev[mw[mi+1][i]][i]*12 - agev[mw[mi][i]][i]*12));
6918: /* 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]); */
6919:
6920: k=k+1;
6921: if (j >= jmax) {
6922: jmax=j;
6923: ijmax=i;
6924: }
6925: else if (j <= jmin){
6926: jmin=j;
6927: ijmin=i;
6928: }
6929: /* if (j<10) printf("j=%d jmin=%d num=%d ",j,jmin,i); */
6930: /*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]);*/
6931: if(j<0){
6932: nberr++;
6933: printf("Error! Negative delay (%d) between waves %d and %d of individual %ld (around 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]);
6934: fprintf(ficlog,"Error! Negative delay (%d) between waves %d and %d of individual %ld (around 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]);
6935: }
6936: sum=sum+j;
6937: }
6938: jk= j/stepm;
6939: jl= j -jk*stepm;
6940: ju= j -(jk+1)*stepm;
6941: if(mle <=1){ /* only if we use a the linear-interpoloation pseudo-likelihood */
6942: if(jl==0){
6943: dh[mi][i]=jk;
6944: bh[mi][i]=0;
6945: }else{ /* We want a negative bias in order to only have interpolation ie
6946: * to avoid the price of an extra matrix product in likelihood */
6947: dh[mi][i]=jk+1;
6948: bh[mi][i]=ju;
6949: }
6950: }else{
6951: if(jl <= -ju){
6952: dh[mi][i]=jk;
6953: bh[mi][i]=jl; /* bias is positive if real duration
6954: * is higher than the multiple of stepm and negative otherwise.
6955: */
6956: }
6957: else{
6958: dh[mi][i]=jk+1;
6959: bh[mi][i]=ju;
6960: }
6961: if(dh[mi][i]==0){
6962: dh[mi][i]=1; /* At least one step */
6963: bh[mi][i]=ju; /* At least one step */
6964: /* 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);*/
6965: }
6966: } /* end if mle */
6967: }
6968: } /* end wave */
6969: }
6970: jmean=sum/k;
6971: 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);
6972: 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);
6973: }
6974:
6975: /*********** Tricode ****************************/
6976: void tricode(int *cptcov, int *Tvar, int **nbcode, int imx, int *Ndum)
6977: {
6978: /**< Uses cptcovn+2*cptcovprod as the number of covariates */
6979: /* Tvar[i]=atoi(stre); find 'n' in Vn and stores in Tvar. If model=V2+V1 Tvar[1]=2 and Tvar[2]=1
6980: * Boring subroutine which should only output nbcode[Tvar[j]][k]
6981: * Tvar[5] in V2+V1+V3*age+V2*V4 is 4 (V4) even it is a time varying or quantitative variable
6982: * nbcode[Tvar[5]][1]= nbcode[4][1]=0, nbcode[4][2]=1 (usually);
6983: */
6984:
6985: int ij=1, k=0, j=0, i=0, maxncov=NCOVMAX;
6986: int modmaxcovj=0; /* Modality max of covariates j */
6987: int cptcode=0; /* Modality max of covariates j */
6988: int modmincovj=0; /* Modality min of covariates j */
6989:
6990:
6991: /* cptcoveff=0; */
6992: /* *cptcov=0; */
6993:
6994: for (k=1; k <= maxncov; k++) ncodemax[k]=0; /* Horrible constant again replaced by NCOVMAX */
6995: for (k=1; k <= maxncov; k++)
6996: for(j=1; j<=2; j++)
6997: nbcode[k][j]=0; /* Valgrind */
6998:
6999: /* Loop on covariates without age and products and no quantitative variable */
7000: for (k=1; k<=cptcovt; k++) { /* cptcovt: total number of covariates of the model (2) nbocc(+)+1 = 8 excepting constant and age and age*age */
7001: for (j=-1; (j < maxncov); j++) Ndum[j]=0;
7002: /* printf("Testing k=%d, cptcovt=%d\n",k, cptcovt); */
7003: if(Dummy[k]==0 && Typevar[k] !=1 && Typevar[k] != 3 && Typevar[k] != 2){ /* Dummy covariate and not age product nor fixed product */
7004: switch(Fixed[k]) {
7005: case 0: /* Testing on fixed dummy covariate, simple or product of fixed */
7006: modmaxcovj=0;
7007: modmincovj=0;
7008: 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*/
7009: /* printf("Waiting for error tricode Tvar[%d]=%d i=%d (int)(covar[Tvar[k]][i]=%d\n",k,Tvar[k], i, (int)(covar[Tvar[k]][i])); */
7010: ij=(int)(covar[Tvar[k]][i]);
7011: /* ij=0 or 1 or -1. Value of the covariate Tvar[j] for individual i
7012: * If product of Vn*Vm, still boolean *:
7013: * If it was coded 1, 2, 3, 4 should be splitted into 3 boolean variables
7014: * 1 => 0 0 0, 2 => 0 0 1, 3 => 0 1 1, 4=1 0 0 */
7015: /* Finds for covariate j, n=Tvar[j] of Vn . ij is the
7016: modality of the nth covariate of individual i. */
7017: if (ij > modmaxcovj)
7018: modmaxcovj=ij;
7019: else if (ij < modmincovj)
7020: modmincovj=ij;
7021: if (ij <0 || ij >1 ){
7022: printf("ERROR, IMaCh doesn't treat covariate with missing values V%d=-1, individual %d will be skipped.\n",Tvar[k],i);
7023: fprintf(ficlog,"ERROR, currently IMaCh doesn't treat covariate with missing values V%d=-1, individual %d will be skipped.\n",Tvar[k],i);
7024: fflush(ficlog);
7025: exit(1);
7026: }
7027: if ((ij < -1) || (ij > NCOVMAX)){
7028: printf( "Error: minimal is less than -1 or maximal is bigger than %d. Exiting. \n", NCOVMAX );
7029: exit(1);
7030: }else
7031: Ndum[ij]++; /*counts and stores the occurence of this modality 0, 1, -1*/
7032: /* If coded 1, 2, 3 , counts the number of 1 Ndum[1], number of 2, Ndum[2], etc */
7033: /*printf("i=%d ij=%d Ndum[ij]=%d imx=%d",i,ij,Ndum[ij],imx);*/
7034: /* getting the maximum value of the modality of the covariate
7035: (should be 0 or 1 now) Tvar[j]. If V=sex and male is coded 0 and
7036: female ies 1, then modmaxcovj=1.
7037: */
7038: } /* end for loop on individuals i */
7039: printf(" Minimal and maximal values of %d th (fixed) covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj);
7040: fprintf(ficlog," Minimal and maximal values of %d th (fixed) covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj);
7041: cptcode=modmaxcovj;
7042: /* Ndum[0] = frequency of 0 for model-covariate j, Ndum[1] frequency of 1 etc. */
7043: /*for (i=0; i<=cptcode; i++) {*/
7044: for (j=modmincovj; j<=modmaxcovj; j++) { /* j=-1 ? 0 and 1*//* For each value j of the modality of model-cov k */
7045: printf("Frequencies of (fixed) covariate %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]);
7046: fprintf(ficlog, "Frequencies of (fixed) covariate %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]);
7047: if( Ndum[j] != 0 ){ /* Counts if nobody answered modality j ie empty modality, we skip it and reorder */
7048: if( j != -1){
7049: ncodemax[k]++; /* ncodemax[k]= Number of modalities of the k th
7050: covariate for which somebody answered excluding
7051: undefined. Usually 2: 0 and 1. */
7052: }
7053: ncodemaxwundef[k]++; /* ncodemax[j]= Number of modalities of the k th
7054: covariate for which somebody answered including
7055: undefined. Usually 3: -1, 0 and 1. */
7056: } /* In fact ncodemax[k]=2 (dichotom. variables only) but it could be more for
7057: * historical reasons: 3 if coded 1, 2, 3 and 4 and Ndum[2]=0 */
7058: } /* Ndum[-1] number of undefined modalities */
7059:
7060: /* j is a covariate, n=Tvar[j] of Vn; Fills nbcode */
7061: /* For covariate j, modalities could be 1, 2, 3, 4, 5, 6, 7. */
7062: /* If Ndum[1]=0, Ndum[2]=0, Ndum[3]= 635, Ndum[4]=0, Ndum[5]=0, Ndum[6]=27, Ndum[7]=125; */
7063: /* modmincovj=3; modmaxcovj = 7; */
7064: /* There are only 3 modalities non empty 3, 6, 7 (or 2 if 27 is too few) : ncodemax[j]=3; */
7065: /* which will be coded 0, 1, 2 which in binary on 2=3-1 digits are 0=00 1=01, 2=10; */
7066: /* defining two dummy variables: variables V1_1 and V1_2.*/
7067: /* nbcode[Tvar[j]][ij]=k; */
7068: /* nbcode[Tvar[j]][1]=0; */
7069: /* nbcode[Tvar[j]][2]=1; */
7070: /* nbcode[Tvar[j]][3]=2; */
7071: /* To be continued (not working yet). */
7072: ij=0; /* ij is similar to i but can jump over null modalities */
7073:
7074: /* 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*/
7075: /* Skipping the case of missing values by reducing nbcode to 0 and 1 and not -1, 0, 1 */
7076: /* model=V1+V2+V3, if V2=-1, 0 or 1, then nbcode[2][1]=0 and nbcode[2][2]=1 instead of
7077: * nbcode[2][1]=-1, nbcode[2][2]=0 and nbcode[2][3]=1 */
7078: /*, could be restored in the future */
7079: for (i=0; i<=1; i++) { /* i= 1 to 2 for dichotomous, or from 1 to 3 or from -1 or 0 to 1 currently*/
7080: if (Ndum[i] == 0) { /* If nobody responded to this modality k */
7081: break;
7082: }
7083: ij++;
7084: 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 . Could be -1*/
7085: cptcode = ij; /* New max modality for covar j */
7086: } /* end of loop on modality i=-1 to 1 or more */
7087: break;
7088: case 1: /* Testing on varying covariate, could be simple and
7089: * should look at waves or product of fixed *
7090: * varying. No time to test -1, assuming 0 and 1 only */
7091: ij=0;
7092: for(i=0; i<=1;i++){
7093: nbcode[Tvar[k]][++ij]=i;
7094: }
7095: break;
7096: default:
7097: break;
7098: } /* end switch */
7099: } /* end dummy test */
7100: if(Dummy[k]==1 && Typevar[k] !=1 && Typevar[k] !=3 && Fixed ==0){ /* Fixed Quantitative covariate and not age product */
7101: 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*/
7102: if(Tvar[k]<=0 || Tvar[k]>=NCOVMAX){
7103: printf("Error k=%d \n",k);
7104: exit(1);
7105: }
7106: if(isnan(covar[Tvar[k]][i])){
7107: printf("ERROR, IMaCh doesn't treat fixed quantitative covariate with missing values V%d=., individual %d will be skipped.\n",Tvar[k],i);
7108: fprintf(ficlog,"ERROR, currently IMaCh doesn't treat covariate with missing values V%d=., individual %d will be skipped.\n",Tvar[k],i);
7109: fflush(ficlog);
7110: exit(1);
7111: }
7112: }
7113: } /* end Quanti */
7114: } /* end of loop on model-covariate k. nbcode[Tvark][1]=-1, nbcode[Tvark][1]=0 and nbcode[Tvark][2]=1 sets the value of covariate k*/
7115:
7116: for (k=-1; k< maxncov; k++) Ndum[k]=0;
7117: /* Look at fixed dummy (single or product) covariates to check empty modalities */
7118: for (i=1; i<=ncovmodel-2-nagesqr; i++) { /* -2, cste and age and eventually age*age */
7119: /* Listing of all covariables in statement model to see if some covariates appear twice. For example, V1 appears twice in V1+V1*V2.*/
7120: 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 */
7121: 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 */
7122: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, {2, 1, 1, 1, 2, 1, 1, 0, 0} */
7123: } /* V4+V3+V5, Ndum[1]@5={0, 0, 1, 1, 1} */
7124:
7125: ij=0;
7126: /* for (i=0; i<= maxncov-1; i++) { /\* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) *\/ */
7127: for (k=1; k<= cptcovt; k++) { /* cptcovt: total number of covariates of the model (2) nbocc(+)+1 = 8 excepting constant and age and age*age */
7128: /* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) */
7129: /*printf("Ndum[%d]=%d\n",i, Ndum[i]);*/
7130: /* if((Ndum[i]!=0) && (i<=ncovcol)){ /\* Tvar[i] <= ncovmodel ? *\/ */
7131: if(Ndum[Tvar[k]]!=0 && Dummy[k] == 0 && Typevar[k]==0){ /* Only Dummy simple and non empty in the model */
7132: /* Typevar[k] =0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product */
7133: /* Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product*/
7134: /* If product not in single variable we don't print results */
7135: /*printf("diff Ndum[%d]=%d\n",i, Ndum[i]);*/
7136: ++ij;/* V5 + V4 + V3 + V4*V3 + V5*age + V2 + V1*V2 + V1*age + V1, *//* V5 quanti, V2 quanti, V4, V3, V1 dummies */
7137: /* k= 1 2 3 4 5 6 7 8 9 */
7138: /* Tvar[k]= 5 4 3 6 5 2 7 1 1 */
7139: /* ij 1 2 3 */
7140: /* Tvaraff[ij]= 4 3 1 */
7141: /* Tmodelind[ij]=2 3 9 */
7142: /* TmodelInvind[ij]=2 1 1 */
7143: 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*/
7144: Tmodelind[ij]=k; /* Tmodelind: index in model of dummies Tmodelind[1]=2 V4: pos=2; V3: pos=3, V1=9 {2, 3, 9, ?, ?,} */
7145: 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 */
7146: if(Fixed[k]!=0)
7147: anyvaryingduminmodel=1;
7148: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv)){ */
7149: /* Tvaraff[++ij]=-10; /\* Dont'n know how to treat quantitative variables yet *\/ */
7150: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv)){ */
7151: /* Tvaraff[++ij]=i; /\*For printing (unclear) *\/ */
7152: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv+nqtv)){ */
7153: /* Tvaraff[++ij]=-20; /\* Dont'n know how to treat quantitative variables yet *\/ */
7154: }
7155: } /* Tvaraff[1]@5 {3, 4, -20, 0, 0} Very strange */
7156: /* ij--; */
7157: /* cptcoveff=ij; /\*Number of total covariates*\/ */
7158: *cptcov=ij; /* cptcov= Number of total real effective simple dummies (fixed or time arying) effective (used as cptcoveff in other functions)
7159: * because they can be excluded from the model and real
7160: * if in the model but excluded because missing values, but how to get k from ij?*/
7161: for(j=ij+1; j<= cptcovt; j++){
7162: Tvaraff[j]=0;
7163: Tmodelind[j]=0;
7164: }
7165: for(j=ntveff+1; j<= cptcovt; j++){
7166: TmodelInvind[j]=0;
7167: }
7168: /* To be sorted */
7169: ;
7170: }
7171:
7172:
7173: /*********** Health Expectancies ****************/
7174:
7175: 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 )
7176:
7177: {
7178: /* Health expectancies, no variances */
7179: /* cij is the combination in the list of combination of dummy covariates */
7180: /* strstart is a string of time at start of computing */
7181: int i, j, nhstepm, hstepm, h, nstepm;
7182: int nhstepma, nstepma; /* Decreasing with age */
7183: double age, agelim, hf;
7184: double ***p3mat;
7185: double eip;
7186:
7187: /* pstamp(ficreseij); */
7188: fprintf(ficreseij,"# (a) Life expectancies by health status at initial age and (b) health expectancies by health status at initial age\n");
7189: fprintf(ficreseij,"# Age");
7190: for(i=1; i<=nlstate;i++){
7191: for(j=1; j<=nlstate;j++){
7192: fprintf(ficreseij," e%1d%1d ",i,j);
7193: }
7194: fprintf(ficreseij," e%1d. ",i);
7195: }
7196: fprintf(ficreseij,"\n");
7197:
7198:
7199: if(estepm < stepm){
7200: printf ("Problem %d lower than %d\n",estepm, stepm);
7201: }
7202: else hstepm=estepm;
7203: /* We compute the life expectancy from trapezoids spaced every estepm months
7204: * This is mainly to measure the difference between two models: for example
7205: * if stepm=24 months pijx are given only every 2 years and by summing them
7206: * we are calculating an estimate of the Life Expectancy assuming a linear
7207: * progression in between and thus overestimating or underestimating according
7208: * to the curvature of the survival function. If, for the same date, we
7209: * estimate the model with stepm=1 month, we can keep estepm to 24 months
7210: * to compare the new estimate of Life expectancy with the same linear
7211: * hypothesis. A more precise result, taking into account a more precise
7212: * curvature will be obtained if estepm is as small as stepm. */
7213:
7214: /* For example we decided to compute the life expectancy with the smallest unit */
7215: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
7216: nhstepm is the number of hstepm from age to agelim
7217: nstepm is the number of stepm from age to agelin.
7218: Look at hpijx to understand the reason which relies in memory size consideration
7219: and note for a fixed period like estepm months */
7220: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
7221: survival function given by stepm (the optimization length). Unfortunately it
7222: means that if the survival funtion is printed only each two years of age and if
7223: you sum them up and add 1 year (area under the trapezoids) you won't get the same
7224: results. So we changed our mind and took the option of the best precision.
7225: */
7226: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
7227:
7228: agelim=AGESUP;
7229: /* If stepm=6 months */
7230: /* Computed by stepm unit matrices, product of hstepm matrices, stored
7231: in an array of nhstepm length: nhstepm=10, hstepm=4, stepm=6 months */
7232:
7233: /* nhstepm age range expressed in number of stepm */
7234: nstepm=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
7235: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
7236: /* if (stepm >= YEARM) hstepm=1;*/
7237: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
7238: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
7239:
7240: for (age=bage; age<=fage; age ++){
7241: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
7242: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
7243: /* if (stepm >= YEARM) hstepm=1;*/
7244: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
7245:
7246: /* If stepm=6 months */
7247: /* Computed by stepm unit matrices, product of hstepma matrices, stored
7248: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
7249: /* printf("HELLO evsij Entering hpxij age=%d cij=%d hstepm=%d x[1]=%f nres=%d\n",(int) age, cij, hstepm, x[1], nres); */
7250: hpxij(p3mat,nhstepma,age,hstepm,x,nlstate,stepm,oldm, savm, cij, nres);
7251:
7252: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
7253:
7254: printf("%d|",(int)age);fflush(stdout);
7255: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
7256:
7257: /* Computing expectancies */
7258: for(i=1; i<=nlstate;i++)
7259: for(j=1; j<=nlstate;j++)
7260: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
7261: eij[i][j][(int)age] += (p3mat[i][j][h]+p3mat[i][j][h+1])/2.0*hf;
7262:
7263: /* 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]);*/
7264:
7265: }
7266:
7267: fprintf(ficreseij,"%3.0f",age );
7268: for(i=1; i<=nlstate;i++){
7269: eip=0;
7270: for(j=1; j<=nlstate;j++){
7271: eip +=eij[i][j][(int)age];
7272: fprintf(ficreseij,"%9.4f", eij[i][j][(int)age] );
7273: }
7274: fprintf(ficreseij,"%9.4f", eip );
7275: }
7276: fprintf(ficreseij,"\n");
7277:
7278: }
7279: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
7280: printf("\n");
7281: fprintf(ficlog,"\n");
7282:
7283: }
7284:
7285: 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 )
7286:
7287: {
7288: /* Covariances of health expectancies eij and of total life expectancies according
7289: to initial status i, ei. .
7290: */
7291: /* Very time consuming function, but already optimized with precov */
7292: int i, j, nhstepm, hstepm, h, nstepm, k, cptj, cptj2, i2, j2, ij, ji;
7293: int nhstepma, nstepma; /* Decreasing with age */
7294: double age, agelim, hf;
7295: double ***p3matp, ***p3matm, ***varhe;
7296: double **dnewm,**doldm;
7297: double *xp, *xm;
7298: double **gp, **gm;
7299: double ***gradg, ***trgradg;
7300: int theta;
7301:
7302: double eip, vip;
7303:
7304: varhe=ma3x(1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int) fage);
7305: xp=vector(1,npar);
7306: xm=vector(1,npar);
7307: dnewm=matrix(1,nlstate*nlstate,1,npar);
7308: doldm=matrix(1,nlstate*nlstate,1,nlstate*nlstate);
7309:
7310: pstamp(ficresstdeij);
7311: fprintf(ficresstdeij,"# Health expectancies with standard errors\n");
7312: fprintf(ficresstdeij,"# Age");
7313: for(i=1; i<=nlstate;i++){
7314: for(j=1; j<=nlstate;j++)
7315: fprintf(ficresstdeij," e%1d%1d (SE)",i,j);
7316: fprintf(ficresstdeij," e%1d. ",i);
7317: }
7318: fprintf(ficresstdeij,"\n");
7319:
7320: pstamp(ficrescveij);
7321: fprintf(ficrescveij,"# Subdiagonal matrix of covariances of health expectancies by age: cov(eij,ekl)\n");
7322: fprintf(ficrescveij,"# Age");
7323: for(i=1; i<=nlstate;i++)
7324: for(j=1; j<=nlstate;j++){
7325: cptj= (j-1)*nlstate+i;
7326: for(i2=1; i2<=nlstate;i2++)
7327: for(j2=1; j2<=nlstate;j2++){
7328: cptj2= (j2-1)*nlstate+i2;
7329: if(cptj2 <= cptj)
7330: fprintf(ficrescveij," %1d%1d,%1d%1d",i,j,i2,j2);
7331: }
7332: }
7333: fprintf(ficrescveij,"\n");
7334:
7335: if(estepm < stepm){
7336: printf ("Problem %d lower than %d\n",estepm, stepm);
7337: }
7338: else hstepm=estepm;
7339: /* We compute the life expectancy from trapezoids spaced every estepm months
7340: * This is mainly to measure the difference between two models: for example
7341: * if stepm=24 months pijx are given only every 2 years and by summing them
7342: * we are calculating an estimate of the Life Expectancy assuming a linear
7343: * progression in between and thus overestimating or underestimating according
7344: * to the curvature of the survival function. If, for the same date, we
7345: * estimate the model with stepm=1 month, we can keep estepm to 24 months
7346: * to compare the new estimate of Life expectancy with the same linear
7347: * hypothesis. A more precise result, taking into account a more precise
7348: * curvature will be obtained if estepm is as small as stepm. */
7349:
7350: /* For example we decided to compute the life expectancy with the smallest unit */
7351: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
7352: nhstepm is the number of hstepm from age to agelim
7353: nstepm is the number of stepm from age to agelin.
7354: Look at hpijx to understand the reason of that which relies in memory size
7355: and note for a fixed period like estepm months */
7356: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
7357: survival function given by stepm (the optimization length). Unfortunately it
7358: means that if the survival funtion is printed only each two years of age and if
7359: you sum them up and add 1 year (area under the trapezoids) you won't get the same
7360: results. So we changed our mind and took the option of the best precision.
7361: */
7362: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
7363:
7364: /* If stepm=6 months */
7365: /* nhstepm age range expressed in number of stepm */
7366: agelim=AGESUP;
7367: nstepm=(int) rint((agelim-bage)*YEARM/stepm);
7368: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
7369: /* if (stepm >= YEARM) hstepm=1;*/
7370: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
7371:
7372: p3matp=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
7373: p3matm=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
7374: gradg=ma3x(0,nhstepm,1,npar,1,nlstate*nlstate);
7375: trgradg =ma3x(0,nhstepm,1,nlstate*nlstate,1,npar);
7376: gp=matrix(0,nhstepm,1,nlstate*nlstate);
7377: gm=matrix(0,nhstepm,1,nlstate*nlstate);
7378:
7379: for (age=bage; age<=fage; age ++){
7380: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
7381: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
7382: /* if (stepm >= YEARM) hstepm=1;*/
7383: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
7384:
7385: /* If stepm=6 months */
7386: /* Computed by stepm unit matrices, product of hstepma matrices, stored
7387: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
7388:
7389: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
7390:
7391: /* Computing Variances of health expectancies */
7392: /* Gradient is computed with plus gp and minus gm. Code is duplicated in order to
7393: decrease memory allocation */
7394: for(theta=1; theta <=npar; theta++){
7395: for(i=1; i<=npar; i++){
7396: xp[i] = x[i] + (i==theta ?delti[theta]:0);
7397: xm[i] = x[i] - (i==theta ?delti[theta]:0);
7398: }
7399: hpxij(p3matp,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, cij, nres);
7400: hpxij(p3matm,nhstepm,age,hstepm,xm,nlstate,stepm,oldm,savm, cij, nres);
7401:
7402: for(j=1; j<= nlstate; j++){
7403: for(i=1; i<=nlstate; i++){
7404: for(h=0; h<=nhstepm-1; h++){
7405: gp[h][(j-1)*nlstate + i] = (p3matp[i][j][h]+p3matp[i][j][h+1])/2.;
7406: gm[h][(j-1)*nlstate + i] = (p3matm[i][j][h]+p3matm[i][j][h+1])/2.;
7407: }
7408: }
7409: }
7410:
7411: for(ij=1; ij<= nlstate*nlstate; ij++)
7412: for(h=0; h<=nhstepm-1; h++){
7413: gradg[h][theta][ij]= (gp[h][ij]-gm[h][ij])/2./delti[theta];
7414: }
7415: }/* End theta */
7416:
7417:
7418: for(h=0; h<=nhstepm-1; h++)
7419: for(j=1; j<=nlstate*nlstate;j++)
7420: for(theta=1; theta <=npar; theta++)
7421: trgradg[h][j][theta]=gradg[h][theta][j];
7422:
7423:
7424: for(ij=1;ij<=nlstate*nlstate;ij++)
7425: for(ji=1;ji<=nlstate*nlstate;ji++)
7426: varhe[ij][ji][(int)age] =0.;
7427:
7428: printf("%d|",(int)age);fflush(stdout);
7429: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
7430: for(h=0;h<=nhstepm-1;h++){
7431: for(k=0;k<=nhstepm-1;k++){
7432: matprod2(dnewm,trgradg[h],1,nlstate*nlstate,1,npar,1,npar,matcov);
7433: matprod2(doldm,dnewm,1,nlstate*nlstate,1,npar,1,nlstate*nlstate,gradg[k]);
7434: for(ij=1;ij<=nlstate*nlstate;ij++)
7435: for(ji=1;ji<=nlstate*nlstate;ji++)
7436: varhe[ij][ji][(int)age] += doldm[ij][ji]*hf*hf;
7437: }
7438: }
7439: /* if((int)age ==50){ */
7440: /* printf(" age=%d cij=%d nres=%d varhe[%d][%d]=%f ",(int)age, cij, nres, 1,2,varhe[1][2]); */
7441: /* } */
7442: /* Computing expectancies */
7443: hpxij(p3matm,nhstepm,age,hstepm,x,nlstate,stepm,oldm, savm, cij,nres);
7444: for(i=1; i<=nlstate;i++)
7445: for(j=1; j<=nlstate;j++)
7446: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
7447: eij[i][j][(int)age] += (p3matm[i][j][h]+p3matm[i][j][h+1])/2.0*hf;
7448:
7449: /* 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]);*/
7450:
7451: }
7452:
7453: /* Standard deviation of expectancies ij */
7454: fprintf(ficresstdeij,"%3.0f",age );
7455: for(i=1; i<=nlstate;i++){
7456: eip=0.;
7457: vip=0.;
7458: for(j=1; j<=nlstate;j++){
7459: eip += eij[i][j][(int)age];
7460: for(k=1; k<=nlstate;k++) /* Sum on j and k of cov(eij,eik) */
7461: vip += varhe[(j-1)*nlstate+i][(k-1)*nlstate+i][(int)age];
7462: fprintf(ficresstdeij," %9.4f (%.4f)", eij[i][j][(int)age], sqrt(varhe[(j-1)*nlstate+i][(j-1)*nlstate+i][(int)age]) );
7463: }
7464: fprintf(ficresstdeij," %9.4f (%.4f)", eip, sqrt(vip));
7465: }
7466: fprintf(ficresstdeij,"\n");
7467:
7468: /* Variance of expectancies ij */
7469: fprintf(ficrescveij,"%3.0f",age );
7470: for(i=1; i<=nlstate;i++)
7471: for(j=1; j<=nlstate;j++){
7472: cptj= (j-1)*nlstate+i;
7473: for(i2=1; i2<=nlstate;i2++)
7474: for(j2=1; j2<=nlstate;j2++){
7475: cptj2= (j2-1)*nlstate+i2;
7476: if(cptj2 <= cptj)
7477: fprintf(ficrescveij," %.4f", varhe[cptj][cptj2][(int)age]);
7478: }
7479: }
7480: fprintf(ficrescveij,"\n");
7481:
7482: }
7483: free_matrix(gm,0,nhstepm,1,nlstate*nlstate);
7484: free_matrix(gp,0,nhstepm,1,nlstate*nlstate);
7485: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate*nlstate);
7486: free_ma3x(trgradg,0,nhstepm,1,nlstate*nlstate,1,npar);
7487: free_ma3x(p3matm,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
7488: free_ma3x(p3matp,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
7489: printf("\n");
7490: fprintf(ficlog,"\n");
7491:
7492: free_vector(xm,1,npar);
7493: free_vector(xp,1,npar);
7494: free_matrix(dnewm,1,nlstate*nlstate,1,npar);
7495: free_matrix(doldm,1,nlstate*nlstate,1,nlstate*nlstate);
7496: free_ma3x(varhe,1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int)fage);
7497: }
7498:
7499: /************ Variance ******************/
7500: 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)
7501: {
7502: /** Variance of health expectancies
7503: * double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);
7504: * double **newm;
7505: * int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav)
7506: */
7507:
7508: /* int movingaverage(); */
7509: double **dnewm,**doldm;
7510: double **dnewmp,**doldmp;
7511: int i, j, nhstepm, hstepm, h, nstepm ;
7512: int first=0;
7513: int k;
7514: double *xp;
7515: double **gp, **gm; /**< for var eij */
7516: double ***gradg, ***trgradg; /**< for var eij */
7517: double **gradgp, **trgradgp; /**< for var p point j */
7518: double *gpp, *gmp; /**< for var p point j */
7519: double **varppt; /**< for var p point j nlstate to nlstate+ndeath */
7520: double ***p3mat;
7521: double age,agelim, hf;
7522: /* double ***mobaverage; */
7523: int theta;
7524: char digit[4];
7525: char digitp[25];
7526:
7527: char fileresprobmorprev[FILENAMELENGTH];
7528:
7529: if(popbased==1){
7530: if(mobilav!=0)
7531: strcpy(digitp,"-POPULBASED-MOBILAV_");
7532: else strcpy(digitp,"-POPULBASED-NOMOBIL_");
7533: }
7534: else
7535: strcpy(digitp,"-STABLBASED_");
7536:
7537: /* if (mobilav!=0) { */
7538: /* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
7539: /* if (movingaverage(probs, bage, fage, mobaverage,mobilav)!=0){ */
7540: /* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); */
7541: /* printf(" Error in movingaverage mobilav=%d\n",mobilav); */
7542: /* } */
7543: /* } */
7544:
7545: strcpy(fileresprobmorprev,"PRMORPREV-");
7546: sprintf(digit,"%-d",ij);
7547: /*printf("DIGIT=%s, ij=%d ijr=%-d|\n",digit, ij,ij);*/
7548: strcat(fileresprobmorprev,digit); /* Tvar to be done */
7549: strcat(fileresprobmorprev,digitp); /* Popbased or not, mobilav or not */
7550: strcat(fileresprobmorprev,fileresu);
7551: if((ficresprobmorprev=fopen(fileresprobmorprev,"w"))==NULL) {
7552: printf("Problem with resultfile: %s\n", fileresprobmorprev);
7553: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobmorprev);
7554: }
7555: printf("Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
7556: fprintf(ficlog,"Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
7557: pstamp(ficresprobmorprev);
7558: 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);
7559: fprintf(ficresprobmorprev,"# Selected quantitative variables and dummies");
7560:
7561: /* We use TinvDoQresult[nres][resultmodel[nres][j] we sort according to the equation model and the resultline: it is a choice */
7562: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ /\* To be done*\/ */
7563: /* fprintf(ficresprobmorprev," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
7564: /* } */
7565: for (j=1; j<= cptcovs; j++){ /* For each selected (single) quantitative value */ /* To be done*/
7566: /* fprintf(ficresprobmorprev," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]); */
7567: fprintf(ficresprobmorprev," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
7568: }
7569: /* for(j=1;j<=cptcoveff;j++) */
7570: /* fprintf(ficresprobmorprev," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(ij,TnsdVar[Tvaraff[j]])]); */
7571: fprintf(ficresprobmorprev,"\n");
7572:
7573: fprintf(ficresprobmorprev,"# Age cov=%-d",ij);
7574: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
7575: fprintf(ficresprobmorprev," p.%-d SE",j);
7576: for(i=1; i<=nlstate;i++)
7577: fprintf(ficresprobmorprev," w%1d p%-d%-d",i,i,j);
7578: }
7579: fprintf(ficresprobmorprev,"\n");
7580:
7581: fprintf(ficgp,"\n# Routine varevsij");
7582: fprintf(ficgp,"\nunset title \n");
7583: /* fprintf(fichtm, "#Local time at start: %s", strstart);*/
7584: 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");
7585: fprintf(fichtm,"\n<br>%s <br>\n",digitp);
7586:
7587: varppt = matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
7588: pstamp(ficresvij);
7589: fprintf(ficresvij,"# Variance and covariance of health expectancies e.j \n# (weighted average of eij where weights are ");
7590: if(popbased==1)
7591: 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);
7592: else
7593: fprintf(ficresvij,"the age specific period (stable) prevalences in each health state \n");
7594: fprintf(ficresvij,"# Age");
7595: for(i=1; i<=nlstate;i++)
7596: for(j=1; j<=nlstate;j++)
7597: fprintf(ficresvij," Cov(e.%1d, e.%1d)",i,j);
7598: fprintf(ficresvij,"\n");
7599:
7600: xp=vector(1,npar);
7601: dnewm=matrix(1,nlstate,1,npar);
7602: doldm=matrix(1,nlstate,1,nlstate);
7603: dnewmp= matrix(nlstate+1,nlstate+ndeath,1,npar);
7604: doldmp= matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
7605:
7606: gradgp=matrix(1,npar,nlstate+1,nlstate+ndeath);
7607: gpp=vector(nlstate+1,nlstate+ndeath);
7608: gmp=vector(nlstate+1,nlstate+ndeath);
7609: trgradgp =matrix(nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
7610:
7611: if(estepm < stepm){
7612: printf ("Problem %d lower than %d\n",estepm, stepm);
7613: }
7614: else hstepm=estepm;
7615: /* For example we decided to compute the life expectancy with the smallest unit */
7616: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
7617: nhstepm is the number of hstepm from age to agelim
7618: nstepm is the number of stepm from age to agelim.
7619: Look at function hpijx to understand why because of memory size limitations,
7620: we decided (b) to get a life expectancy respecting the most precise curvature of the
7621: survival function given by stepm (the optimization length). Unfortunately it
7622: means that if the survival funtion is printed every two years of age and if
7623: you sum them up and add 1 year (area under the trapezoids) you won't get the same
7624: results. So we changed our mind and took the option of the best precision.
7625: */
7626: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
7627: agelim = AGESUP;
7628: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
7629: nstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
7630: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
7631: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
7632: gradg=ma3x(0,nhstepm,1,npar,1,nlstate);
7633: gp=matrix(0,nhstepm,1,nlstate);
7634: gm=matrix(0,nhstepm,1,nlstate);
7635:
7636:
7637: for(theta=1; theta <=npar; theta++){
7638: for(i=1; i<=npar; i++){ /* Computes gradient x + delta*/
7639: xp[i] = x[i] + (i==theta ?delti[theta]:0);
7640: }
7641: /**< Computes the prevalence limit with parameter theta shifted of delta up to ftolpl precision and
7642: * returns into prlim .
7643: */
7644: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij, nres);
7645:
7646: /* If popbased = 1 we use crossection prevalences. Previous step is useless but prlim is created */
7647: if (popbased==1) {
7648: if(mobilav ==0){
7649: for(i=1; i<=nlstate;i++)
7650: prlim[i][i]=probs[(int)age][i][ij];
7651: }else{ /* mobilav */
7652: for(i=1; i<=nlstate;i++)
7653: prlim[i][i]=mobaverage[(int)age][i][ij];
7654: }
7655: }
7656: /**< Computes the shifted transition matrix \f$ {}{h}_p^{ij}x\f$ at horizon h.
7657: */
7658: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij,nres); /* Returns p3mat[i][j][h] for h=0 to nhstepm */
7659: /**< And for each alive state j, sums over i \f$ w^i_x {}{h}_p^{ij}x\f$, which are the probability
7660: * at horizon h in state j including mortality.
7661: */
7662: for(j=1; j<= nlstate; j++){
7663: for(h=0; h<=nhstepm; h++){
7664: for(i=1, gp[h][j]=0.;i<=nlstate;i++)
7665: gp[h][j] += prlim[i][i]*p3mat[i][j][h];
7666: }
7667: }
7668: /* Next for computing shifted+ probability of death (h=1 means
7669: computed over hstepm matrices product = hstepm*stepm months)
7670: as a weighted average of prlim(i) * p(i,j) p.3=w1*p13 + w2*p23 .
7671: */
7672: for(j=nlstate+1;j<=nlstate+ndeath;j++){
7673: for(i=1,gpp[j]=0.; i<= nlstate; i++)
7674: gpp[j] += prlim[i][i]*p3mat[i][j][1];
7675: }
7676:
7677: /* Again with minus shift */
7678:
7679: for(i=1; i<=npar; i++) /* Computes gradient x - delta */
7680: xp[i] = x[i] - (i==theta ?delti[theta]:0);
7681:
7682: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp, ij, nres);
7683:
7684: if (popbased==1) {
7685: if(mobilav ==0){
7686: for(i=1; i<=nlstate;i++)
7687: prlim[i][i]=probs[(int)age][i][ij];
7688: }else{ /* mobilav */
7689: for(i=1; i<=nlstate;i++)
7690: prlim[i][i]=mobaverage[(int)age][i][ij];
7691: }
7692: }
7693:
7694: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij,nres);
7695:
7696: for(j=1; j<= nlstate; j++){ /* Sum of wi * eij = e.j */
7697: for(h=0; h<=nhstepm; h++){
7698: for(i=1, gm[h][j]=0.;i<=nlstate;i++)
7699: gm[h][j] += prlim[i][i]*p3mat[i][j][h];
7700: }
7701: }
7702: /* This for computing probability of death (h=1 means
7703: computed over hstepm matrices product = hstepm*stepm months)
7704: as a weighted average of prlim.
7705: */
7706: for(j=nlstate+1;j<=nlstate+ndeath;j++){
7707: for(i=1,gmp[j]=0.; i<= nlstate; i++)
7708: gmp[j] += prlim[i][i]*p3mat[i][j][1];
7709: }
7710: /* end shifting computations */
7711:
7712: /**< Computing gradient matrix at horizon h
7713: */
7714: for(j=1; j<= nlstate; j++) /* vareij */
7715: for(h=0; h<=nhstepm; h++){
7716: gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta];
7717: }
7718: /**< Gradient of overall mortality p.3 (or p.j)
7719: */
7720: for(j=nlstate+1; j<= nlstate+ndeath; j++){ /* var mu mortality from j */
7721: gradgp[theta][j]= (gpp[j]-gmp[j])/2./delti[theta];
7722: }
7723:
7724: } /* End theta */
7725:
7726: /* We got the gradient matrix for each theta and state j */
7727: trgradg =ma3x(0,nhstepm,1,nlstate,1,npar); /* veij */
7728:
7729: for(h=0; h<=nhstepm; h++) /* veij */
7730: for(j=1; j<=nlstate;j++)
7731: for(theta=1; theta <=npar; theta++)
7732: trgradg[h][j][theta]=gradg[h][theta][j];
7733:
7734: for(j=nlstate+1; j<=nlstate+ndeath;j++) /* mu */
7735: for(theta=1; theta <=npar; theta++)
7736: trgradgp[j][theta]=gradgp[theta][j];
7737: /**< as well as its transposed matrix
7738: */
7739:
7740: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
7741: for(i=1;i<=nlstate;i++)
7742: for(j=1;j<=nlstate;j++)
7743: vareij[i][j][(int)age] =0.;
7744:
7745: /* Computing trgradg by matcov by gradg at age and summing over h
7746: * and k (nhstepm) formula 15 of article
7747: * Lievre-Brouard-Heathcote
7748: */
7749:
7750: for(h=0;h<=nhstepm;h++){
7751: for(k=0;k<=nhstepm;k++){
7752: matprod2(dnewm,trgradg[h],1,nlstate,1,npar,1,npar,matcov);
7753: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg[k]);
7754: for(i=1;i<=nlstate;i++)
7755: for(j=1;j<=nlstate;j++)
7756: vareij[i][j][(int)age] += doldm[i][j]*hf*hf;
7757: }
7758: }
7759:
7760: /* pptj is p.3 or p.j = trgradgp by cov by gradgp, variance of
7761: * p.j overall mortality formula 49 but computed directly because
7762: * we compute the grad (wix pijx) instead of grad (pijx),even if
7763: * wix is independent of theta.
7764: */
7765: matprod2(dnewmp,trgradgp,nlstate+1,nlstate+ndeath,1,npar,1,npar,matcov);
7766: matprod2(doldmp,dnewmp,nlstate+1,nlstate+ndeath,1,npar,nlstate+1,nlstate+ndeath,gradgp);
7767: for(j=nlstate+1;j<=nlstate+ndeath;j++)
7768: for(i=nlstate+1;i<=nlstate+ndeath;i++)
7769: varppt[j][i]=doldmp[j][i];
7770: /* end ppptj */
7771: /* x centered again */
7772:
7773: prevalim(prlim,nlstate,x,age,oldm,savm,ftolpl,ncvyearp,ij, nres);
7774:
7775: if (popbased==1) {
7776: if(mobilav ==0){
7777: for(i=1; i<=nlstate;i++)
7778: prlim[i][i]=probs[(int)age][i][ij];
7779: }else{ /* mobilav */
7780: for(i=1; i<=nlstate;i++)
7781: prlim[i][i]=mobaverage[(int)age][i][ij];
7782: }
7783: }
7784:
7785: /* This for computing probability of death (h=1 means
7786: computed over hstepm (estepm) matrices product = hstepm*stepm months)
7787: as a weighted average of prlim.
7788: */
7789: hpxij(p3mat,nhstepm,age,hstepm,x,nlstate,stepm,oldm,savm, ij, nres);
7790: for(j=nlstate+1;j<=nlstate+ndeath;j++){
7791: for(i=1,gmp[j]=0.;i<= nlstate; i++)
7792: gmp[j] += prlim[i][i]*p3mat[i][j][1];
7793: }
7794: /* end probability of death */
7795:
7796: fprintf(ficresprobmorprev,"%3d %d ",(int) age, ij);
7797: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
7798: fprintf(ficresprobmorprev," %11.3e %11.3e",gmp[j], sqrt(varppt[j][j]));
7799: for(i=1; i<=nlstate;i++){
7800: fprintf(ficresprobmorprev," %11.3e %11.3e ",prlim[i][i],p3mat[i][j][1]);
7801: }
7802: }
7803: fprintf(ficresprobmorprev,"\n");
7804:
7805: fprintf(ficresvij,"%.0f ",age );
7806: for(i=1; i<=nlstate;i++)
7807: for(j=1; j<=nlstate;j++){
7808: fprintf(ficresvij," %.4f", vareij[i][j][(int)age]);
7809: }
7810: fprintf(ficresvij,"\n");
7811: free_matrix(gp,0,nhstepm,1,nlstate);
7812: free_matrix(gm,0,nhstepm,1,nlstate);
7813: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate);
7814: free_ma3x(trgradg,0,nhstepm,1,nlstate,1,npar);
7815: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
7816: } /* End age */
7817: free_vector(gpp,nlstate+1,nlstate+ndeath);
7818: free_vector(gmp,nlstate+1,nlstate+ndeath);
7819: free_matrix(gradgp,1,npar,nlstate+1,nlstate+ndeath);
7820: free_matrix(trgradgp,nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
7821: /* fprintf(ficgp,"\nunset parametric;unset label; set ter png small size 320, 240"); */
7822: fprintf(ficgp,"\nunset parametric;unset label; set ter svg size 640, 480");
7823: /* for(j=nlstate+1; j<= nlstate+ndeath; j++){ *//* Only the first actually */
7824: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Force of mortality (year-1)\";");
7825: fprintf(ficgp,"\nset out \"%s%s.svg\";",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
7826: /* fprintf(ficgp,"\n plot \"%s\" u 1:($3*%6.3f) not w l 1 ",fileresprobmorprev,YEARM/estepm); */
7827: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)*%6.3f) t \"95\%% interval\" w l 2 ",fileresprobmorprev,YEARM/estepm); */
7828: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)*%6.3f) not w l 2 ",fileresprobmorprev,YEARM/estepm); */
7829: fprintf(ficgp,"\n plot \"%s\" u 1:($3) not w l lt 1 ",subdirf(fileresprobmorprev));
7830: fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)) t \"95%% interval\" w l lt 2 ",subdirf(fileresprobmorprev));
7831: fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)) not w l lt 2 ",subdirf(fileresprobmorprev));
7832: fprintf(fichtm,"\n<br> File (multiple files are possible if covariates are present): <A href=\"%s\">%s</a>\n",subdirf(fileresprobmorprev),subdirf(fileresprobmorprev));
7833: 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);
7834: /* 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);
7835: */
7836: /* fprintf(ficgp,"\nset out \"varmuptjgr%s%s%s.svg\";replot;",digitp,optionfilefiname,digit); */
7837: fprintf(ficgp,"\nset out;\nset out \"%s%s.svg\";replot;set out;\n",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
7838:
7839: free_vector(xp,1,npar);
7840: free_matrix(doldm,1,nlstate,1,nlstate);
7841: free_matrix(dnewm,1,nlstate,1,npar);
7842: free_matrix(doldmp,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
7843: free_matrix(dnewmp,nlstate+1,nlstate+ndeath,1,npar);
7844: free_matrix(varppt,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
7845: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
7846: fclose(ficresprobmorprev);
7847: fflush(ficgp);
7848: fflush(fichtm);
7849: } /* end varevsij */
7850:
7851: /************ Variance of prevlim ******************/
7852: void varprevlim(char fileresvpl[], FILE *ficresvpl, 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)
7853: {
7854: /* Variance of prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/
7855: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
7856:
7857: double **dnewmpar,**doldm;
7858: int i, j, nhstepm, hstepm;
7859: double *xp;
7860: double *gp, *gm;
7861: double **gradg, **trgradg;
7862: double **mgm, **mgp;
7863: double age,agelim;
7864: int theta;
7865:
7866: pstamp(ficresvpl);
7867: fprintf(ficresvpl,"# Standard deviation of period (forward stable) prevalences \n");
7868: fprintf(ficresvpl,"# Age ");
7869: if(nresult >=1)
7870: fprintf(ficresvpl," Result# ");
7871: for(i=1; i<=nlstate;i++)
7872: fprintf(ficresvpl," %1d-%1d",i,i);
7873: fprintf(ficresvpl,"\n");
7874:
7875: xp=vector(1,npar);
7876: dnewmpar=matrix(1,nlstate,1,npar);
7877: doldm=matrix(1,nlstate,1,nlstate);
7878:
7879: hstepm=1*YEARM; /* Every year of age */
7880: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
7881: agelim = AGESUP;
7882: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
7883: nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
7884: if (stepm >= YEARM) hstepm=1;
7885: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
7886: gradg=matrix(1,npar,1,nlstate);
7887: mgp=matrix(1,npar,1,nlstate);
7888: mgm=matrix(1,npar,1,nlstate);
7889: gp=vector(1,nlstate);
7890: gm=vector(1,nlstate);
7891:
7892: for(theta=1; theta <=npar; theta++){
7893: for(i=1; i<=npar; i++){ /* Computes gradient */
7894: xp[i] = x[i] + (i==theta ?delti[theta]:0);
7895: }
7896: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ) */
7897: /* prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres); */
7898: /* else */
7899: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
7900: for(i=1;i<=nlstate;i++){
7901: gp[i] = prlim[i][i];
7902: mgp[theta][i] = prlim[i][i];
7903: }
7904: for(i=1; i<=npar; i++) /* Computes gradient */
7905: xp[i] = x[i] - (i==theta ?delti[theta]:0);
7906: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ) */
7907: /* prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres); */
7908: /* else */
7909: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
7910: for(i=1;i<=nlstate;i++){
7911: gm[i] = prlim[i][i];
7912: mgm[theta][i] = prlim[i][i];
7913: }
7914: for(i=1;i<=nlstate;i++)
7915: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
7916: /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */
7917: } /* End theta */
7918:
7919: trgradg =matrix(1,nlstate,1,npar);
7920:
7921: for(j=1; j<=nlstate;j++)
7922: for(theta=1; theta <=npar; theta++)
7923: trgradg[j][theta]=gradg[theta][j];
7924: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
7925: /* printf("\nmgm mgp %d ",(int)age); */
7926: /* for(j=1; j<=nlstate;j++){ */
7927: /* printf(" %d ",j); */
7928: /* for(theta=1; theta <=npar; theta++) */
7929: /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */
7930: /* printf("\n "); */
7931: /* } */
7932: /* } */
7933: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
7934: /* printf("\n gradg %d ",(int)age); */
7935: /* for(j=1; j<=nlstate;j++){ */
7936: /* printf("%d ",j); */
7937: /* for(theta=1; theta <=npar; theta++) */
7938: /* printf("%d %lf ",theta,gradg[theta][j]); */
7939: /* printf("\n "); */
7940: /* } */
7941: /* } */
7942:
7943: for(i=1;i<=nlstate;i++)
7944: varpl[i][(int)age] =0.;
7945: if((int)age==79 ||(int)age== 80 ||(int)age== 81){
7946: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
7947: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
7948: }else{
7949: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
7950: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
7951: }
7952: for(i=1;i<=nlstate;i++)
7953: varpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
7954:
7955: fprintf(ficresvpl,"%.0f ",age );
7956: if(nresult >=1)
7957: fprintf(ficresvpl,"%d ",nres );
7958: for(i=1; i<=nlstate;i++){
7959: fprintf(ficresvpl," %.5f (%.5f)",prlim[i][i],sqrt(varpl[i][(int)age]));
7960: /* for(j=1;j<=nlstate;j++) */
7961: /* fprintf(ficresvpl," %d %.5f ",j,prlim[j][i]); */
7962: }
7963: fprintf(ficresvpl,"\n");
7964: free_vector(gp,1,nlstate);
7965: free_vector(gm,1,nlstate);
7966: free_matrix(mgm,1,npar,1,nlstate);
7967: free_matrix(mgp,1,npar,1,nlstate);
7968: free_matrix(gradg,1,npar,1,nlstate);
7969: free_matrix(trgradg,1,nlstate,1,npar);
7970: } /* End age */
7971:
7972: free_vector(xp,1,npar);
7973: free_matrix(doldm,1,nlstate,1,npar);
7974: free_matrix(dnewmpar,1,nlstate,1,nlstate);
7975:
7976: }
7977:
7978:
7979: /************ Variance of backprevalence limit ******************/
7980: void varbrevlim(char fileresvbl[], FILE *ficresvbl, double **varbpl, double **matcov, double x[], double delti[], int nlstate, int stepm, double bage, double fage, double **oldm, double **savm, double **bprlim, double ftolpl, int mobilavproj, int *ncvyearp, int ij, char strstart[], int nres)
7981: {
7982: /* Variance of backward prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/
7983: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
7984:
7985: double **dnewmpar,**doldm;
7986: int i, j, nhstepm, hstepm;
7987: double *xp;
7988: double *gp, *gm;
7989: double **gradg, **trgradg;
7990: double **mgm, **mgp;
7991: double age,agelim;
7992: int theta;
7993:
7994: pstamp(ficresvbl);
7995: fprintf(ficresvbl,"# Standard deviation of back (stable) prevalences \n");
7996: fprintf(ficresvbl,"# Age ");
7997: if(nresult >=1)
7998: fprintf(ficresvbl," Result# ");
7999: for(i=1; i<=nlstate;i++)
8000: fprintf(ficresvbl," %1d-%1d",i,i);
8001: fprintf(ficresvbl,"\n");
8002:
8003: xp=vector(1,npar);
8004: dnewmpar=matrix(1,nlstate,1,npar);
8005: doldm=matrix(1,nlstate,1,nlstate);
8006:
8007: hstepm=1*YEARM; /* Every year of age */
8008: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
8009: agelim = AGEINF;
8010: for (age=fage; age>=bage; age --){ /* If stepm=6 months */
8011: nhstepm=(int) rint((age-agelim)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
8012: if (stepm >= YEARM) hstepm=1;
8013: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
8014: gradg=matrix(1,npar,1,nlstate);
8015: mgp=matrix(1,npar,1,nlstate);
8016: mgm=matrix(1,npar,1,nlstate);
8017: gp=vector(1,nlstate);
8018: gm=vector(1,nlstate);
8019:
8020: for(theta=1; theta <=npar; theta++){
8021: for(i=1; i<=npar; i++){ /* Computes gradient */
8022: xp[i] = x[i] + (i==theta ?delti[theta]:0);
8023: }
8024: if(mobilavproj > 0 )
8025: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
8026: else
8027: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
8028: for(i=1;i<=nlstate;i++){
8029: gp[i] = bprlim[i][i];
8030: mgp[theta][i] = bprlim[i][i];
8031: }
8032: for(i=1; i<=npar; i++) /* Computes gradient */
8033: xp[i] = x[i] - (i==theta ?delti[theta]:0);
8034: if(mobilavproj > 0 )
8035: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
8036: else
8037: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
8038: for(i=1;i<=nlstate;i++){
8039: gm[i] = bprlim[i][i];
8040: mgm[theta][i] = bprlim[i][i];
8041: }
8042: for(i=1;i<=nlstate;i++)
8043: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
8044: /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */
8045: } /* End theta */
8046:
8047: trgradg =matrix(1,nlstate,1,npar);
8048:
8049: for(j=1; j<=nlstate;j++)
8050: for(theta=1; theta <=npar; theta++)
8051: trgradg[j][theta]=gradg[theta][j];
8052: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
8053: /* printf("\nmgm mgp %d ",(int)age); */
8054: /* for(j=1; j<=nlstate;j++){ */
8055: /* printf(" %d ",j); */
8056: /* for(theta=1; theta <=npar; theta++) */
8057: /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */
8058: /* printf("\n "); */
8059: /* } */
8060: /* } */
8061: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
8062: /* printf("\n gradg %d ",(int)age); */
8063: /* for(j=1; j<=nlstate;j++){ */
8064: /* printf("%d ",j); */
8065: /* for(theta=1; theta <=npar; theta++) */
8066: /* printf("%d %lf ",theta,gradg[theta][j]); */
8067: /* printf("\n "); */
8068: /* } */
8069: /* } */
8070:
8071: for(i=1;i<=nlstate;i++)
8072: varbpl[i][(int)age] =0.;
8073: if((int)age==79 ||(int)age== 80 ||(int)age== 81){
8074: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
8075: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
8076: }else{
8077: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
8078: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
8079: }
8080: for(i=1;i<=nlstate;i++)
8081: varbpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
8082:
8083: fprintf(ficresvbl,"%.0f ",age );
8084: if(nresult >=1)
8085: fprintf(ficresvbl,"%d ",nres );
8086: for(i=1; i<=nlstate;i++)
8087: fprintf(ficresvbl," %.5f (%.5f)",bprlim[i][i],sqrt(varbpl[i][(int)age]));
8088: fprintf(ficresvbl,"\n");
8089: free_vector(gp,1,nlstate);
8090: free_vector(gm,1,nlstate);
8091: free_matrix(mgm,1,npar,1,nlstate);
8092: free_matrix(mgp,1,npar,1,nlstate);
8093: free_matrix(gradg,1,npar,1,nlstate);
8094: free_matrix(trgradg,1,nlstate,1,npar);
8095: } /* End age */
8096:
8097: free_vector(xp,1,npar);
8098: free_matrix(doldm,1,nlstate,1,npar);
8099: free_matrix(dnewmpar,1,nlstate,1,nlstate);
8100:
8101: }
8102:
8103: /************ Variance of one-step probabilities ******************/
8104: 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[])
8105: {
8106: int i, j=0, k1, l1, tj;
8107: int k2, l2, j1, z1;
8108: int k=0, l;
8109: int first=1, first1, first2;
8110: int nres=0; /* New */
8111: double cv12, mu1, mu2, lc1, lc2, v12, v21, v11, v22,v1,v2, c12, tnalp;
8112: double **dnewm,**doldm;
8113: double *xp;
8114: double *gp, *gm;
8115: double **gradg, **trgradg;
8116: double **mu;
8117: double age, cov[NCOVMAX+1];
8118: double std=2.0; /* Number of standard deviation wide of confidence ellipsoids */
8119: int theta;
8120: char fileresprob[FILENAMELENGTH];
8121: char fileresprobcov[FILENAMELENGTH];
8122: char fileresprobcor[FILENAMELENGTH];
8123: double ***varpij;
8124:
8125: strcpy(fileresprob,"PROB_");
8126: strcat(fileresprob,fileresu);
8127: if((ficresprob=fopen(fileresprob,"w"))==NULL) {
8128: printf("Problem with resultfile: %s\n", fileresprob);
8129: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprob);
8130: }
8131: strcpy(fileresprobcov,"PROBCOV_");
8132: strcat(fileresprobcov,fileresu);
8133: if((ficresprobcov=fopen(fileresprobcov,"w"))==NULL) {
8134: printf("Problem with resultfile: %s\n", fileresprobcov);
8135: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcov);
8136: }
8137: strcpy(fileresprobcor,"PROBCOR_");
8138: strcat(fileresprobcor,fileresu);
8139: if((ficresprobcor=fopen(fileresprobcor,"w"))==NULL) {
8140: printf("Problem with resultfile: %s\n", fileresprobcor);
8141: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcor);
8142: }
8143: printf("Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
8144: fprintf(ficlog,"Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
8145: printf("Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
8146: fprintf(ficlog,"Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
8147: printf("and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
8148: fprintf(ficlog,"and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
8149: pstamp(ficresprob);
8150: fprintf(ficresprob,"#One-step probabilities and stand. devi in ()\n");
8151: fprintf(ficresprob,"# Age");
8152: pstamp(ficresprobcov);
8153: fprintf(ficresprobcov,"#One-step probabilities and covariance matrix\n");
8154: fprintf(ficresprobcov,"# Age");
8155: pstamp(ficresprobcor);
8156: fprintf(ficresprobcor,"#One-step probabilities and correlation matrix\n");
8157: fprintf(ficresprobcor,"# Age");
8158:
8159:
8160: for(i=1; i<=nlstate;i++)
8161: for(j=1; j<=(nlstate+ndeath);j++){
8162: fprintf(ficresprob," p%1d-%1d (SE)",i,j);
8163: fprintf(ficresprobcov," p%1d-%1d ",i,j);
8164: fprintf(ficresprobcor," p%1d-%1d ",i,j);
8165: }
8166: /* fprintf(ficresprob,"\n");
8167: fprintf(ficresprobcov,"\n");
8168: fprintf(ficresprobcor,"\n");
8169: */
8170: xp=vector(1,npar);
8171: dnewm=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
8172: doldm=matrix(1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
8173: mu=matrix(1,(nlstate)*(nlstate+ndeath), (int) bage, (int)fage);
8174: varpij=ma3x(1,nlstate*(nlstate+ndeath),1,nlstate*(nlstate+ndeath),(int) bage, (int) fage);
8175: first=1;
8176: fprintf(ficgp,"\n# Routine varprob");
8177: fprintf(fichtm,"\n<li><h4> Computing and drawing one step probabilities with their confidence intervals</h4></li>\n");
8178: fprintf(fichtm,"\n");
8179:
8180: 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. File %s</li>\n",optionfilehtmcov,optionfilehtmcov);
8181: 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);
8182: fprintf(fichtmcov,"\nEllipsoids of confidence centered on point (p<inf>ij</inf>, p<inf>kl</inf>) are estimated \
8183: and drawn. It helps understanding how is the covariance between two incidences.\
8184: They are expressed in year<sup>-1</sup> in order to be less dependent of stepm.<br>\n");
8185: 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. \
8186: It can be understood this way: if pij and pkl where uncorrelated the (2x2) matrix of covariance \
8187: would have been (1/(var pij), 0 , 0, 1/(var pkl)), and the confidence interval would be 2 \
8188: standard deviations wide on each axis. <br>\
8189: Now, if both incidences are correlated (usual case) we diagonalised the inverse of the covariance matrix\
8190: and made the appropriate rotation to look at the uncorrelated principal directions.<br>\
8191: To be simple, these graphs help to understand the significativity of each parameter in relation to a second other one.<br> \n");
8192:
8193: cov[1]=1;
8194: /* tj=cptcoveff; */
8195: tj = (int) pow(2,cptcoveff);
8196: if (cptcovn<1) {tj=1;ncodemax[1]=1;}
8197: j1=0;
8198:
8199: for(nres=1;nres <=nresult; nres++){ /* For each resultline */
8200: for(j1=1; j1<=tj;j1++){ /* For any combination of dummy covariates, fixed and varying */
8201: /* printf("Varprob TKresult[nres]=%d j1=%d, nres=%d, cptcovn=%d, cptcoveff=%d tj=%d cptcovs=%d\n", TKresult[nres], j1, nres, cptcovn, cptcoveff, tj, cptcovs); */
8202: if(tj != 1 && TKresult[nres]!= j1)
8203: continue;
8204:
8205: /* for(j1=1; j1<=tj;j1++){ /\* For each valid combination of covariates or only once*\/ */
8206: /* for(nres=1;nres <=1; nres++){ /\* For each resultline *\/ */
8207: /* /\* for(nres=1;nres <=nresult; nres++){ /\\* For each resultline *\\/ *\/ */
8208: if (cptcovn>0) {
8209: fprintf(ficresprob, "\n#********** Variable ");
8210: fprintf(ficresprobcov, "\n#********** Variable ");
8211: fprintf(ficgp, "\n#********** Variable ");
8212: fprintf(fichtmcov, "\n<hr size=\"2\" color=\"#EC5E5E\">********** Variable ");
8213: fprintf(ficresprobcor, "\n#********** Variable ");
8214:
8215: /* Including quantitative variables of the resultline to be done */
8216: for (z1=1; z1<=cptcovs; z1++){ /* Loop on each variable of this resultline */
8217: /* printf("Varprob modelresult[%d][%d]=%d model=1+age+%s \n",nres, z1, modelresult[nres][z1], model); */
8218: fprintf(ficlog,"Varprob modelresult[%d][%d]=%d model=1+age+%s \n",nres, z1, modelresult[nres][z1], model);
8219: /* fprintf(ficlog,"Varprob modelresult[%d][%d]=%d model=1+age+%s resultline[%d]=%s \n",nres, z1, modelresult[nres][z1], model, nres, resultline[nres]); */
8220: if(Dummy[modelresult[nres][z1]]==0){/* Dummy variable of the variable in position modelresult in the model corresponding to z1 in resultline */
8221: if(Fixed[modelresult[nres][z1]]==0){ /* Fixed referenced to model equation */
8222: fprintf(ficresprob,"V%d=%d ",Tvresult[nres][z1],Tresult[nres][z1]); /* Output of each value for the combination TKresult[nres], ordere by the covariate values in the resultline */
8223: fprintf(ficresprobcov,"V%d=%d ",Tvresult[nres][z1],Tresult[nres][z1]); /* Output of each value for the combination TKresult[nres], ordere by the covariate values in the resultline */
8224: fprintf(ficgp,"V%d=%d ",Tvresult[nres][z1],Tresult[nres][z1]); /* Output of each value for the combination TKresult[nres], ordere by the covariate values in the resultline */
8225: fprintf(fichtmcov,"V%d=%d ",Tvresult[nres][z1],Tresult[nres][z1]); /* Output of each value for the combination TKresult[nres], ordere by the covariate values in the resultline */
8226: fprintf(ficresprobcor,"V%d=%d ",Tvresult[nres][z1],Tresult[nres][z1]); /* Output of each value for the combination TKresult[nres], ordere by the covariate values in the resultline */
8227: fprintf(ficresprob,"fixed ");
8228: fprintf(ficresprobcov,"fixed ");
8229: fprintf(ficgp,"fixed ");
8230: fprintf(fichtmcov,"fixed ");
8231: fprintf(ficresprobcor,"fixed ");
8232: }else{
8233: fprintf(ficresprob,"varyi ");
8234: fprintf(ficresprobcov,"varyi ");
8235: fprintf(ficgp,"varyi ");
8236: fprintf(fichtmcov,"varyi ");
8237: fprintf(ficresprobcor,"varyi ");
8238: }
8239: }else if(Dummy[modelresult[nres][z1]]==1){ /* Quanti variable */
8240: /* For each selected (single) quantitative value */
8241: fprintf(ficresprob," V%d=%lg ",Tvqresult[nres][z1],Tqresult[nres][z1]);
8242: if(Fixed[modelresult[nres][z1]]==0){ /* Fixed */
8243: fprintf(ficresprob,"fixed ");
8244: fprintf(ficresprobcov,"fixed ");
8245: fprintf(ficgp,"fixed ");
8246: fprintf(fichtmcov,"fixed ");
8247: fprintf(ficresprobcor,"fixed ");
8248: }else{
8249: fprintf(ficresprob,"varyi ");
8250: fprintf(ficresprobcov,"varyi ");
8251: fprintf(ficgp,"varyi ");
8252: fprintf(fichtmcov,"varyi ");
8253: fprintf(ficresprobcor,"varyi ");
8254: }
8255: }else{
8256: printf("Error in varprob() Dummy[modelresult[%d][%d]]=%d, modelresult[%d][%d]=V%d cptcovs=%d, cptcoveff=%d \n", nres, z1, Dummy[modelresult[nres][z1]],nres,z1,modelresult[nres][z1],cptcovs, cptcoveff); /* end if dummy or quanti */
8257: fprintf(ficlog,"Error in varprob() Dummy[modelresult[%d][%d]]=%d, modelresult[%d][%d]=V%d cptcovs=%d, cptcoveff=%d \n", nres, z1, Dummy[modelresult[nres][z1]],nres,z1,modelresult[nres][z1],cptcovs, cptcoveff); /* end if dummy or quanti */
8258: exit(1);
8259: }
8260: } /* End loop on variable of this resultline */
8261: /* for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprob, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]); */
8262: fprintf(ficresprob, "**********\n#\n");
8263: fprintf(ficresprobcov, "**********\n#\n");
8264: fprintf(ficgp, "**********\n#\n");
8265: fprintf(fichtmcov, "**********\n<hr size=\"2\" color=\"#EC5E5E\">");
8266: fprintf(ficresprobcor, "**********\n#");
8267: if(invalidvarcomb[j1]){
8268: fprintf(ficgp,"\n#Combination (%d) ignored because no cases \n",j1);
8269: fprintf(fichtmcov,"\n<h3>Combination (%d) ignored because no cases </h3>\n",j1);
8270: continue;
8271: }
8272: }
8273: gradg=matrix(1,npar,1,(nlstate)*(nlstate+ndeath));
8274: trgradg=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
8275: gp=vector(1,(nlstate)*(nlstate+ndeath));
8276: gm=vector(1,(nlstate)*(nlstate+ndeath));
8277: for (age=bage; age<=fage; age ++){ /* Fo each age we feed the model equation with covariates, using precov as in hpxij() ? */
8278: cov[2]=age;
8279: if(nagesqr==1)
8280: cov[3]= age*age;
8281: /* New code end of combination but for each resultline */
8282: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
8283: if(Typevar[k1]==1 || Typevar[k1] ==3){ /* A product with age */
8284: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
8285: }else{
8286: cov[2+nagesqr+k1]=precov[nres][k1];
8287: }
8288: }/* End of loop on model equation */
8289: /* Old code */
8290: /* /\* for (k=1; k<=cptcovn;k++) { *\/ */
8291: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,k)]; *\/ */
8292: /* for (k=1; k<=nsd;k++) { /\* For single dummy covariates only *\/ */
8293: /* /\* Here comes the value of the covariate 'j1' after renumbering k with single dummy covariates *\/ */
8294: /* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(j1,TnsdVar[TvarsD[k]])]; */
8295: /* /\*cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,Tvar[k])];*\//\* j1 1 2 3 4 */
8296: /* * 1 1 1 1 1 */
8297: /* * 2 2 1 1 1 */
8298: /* * 3 1 2 1 1 */
8299: /* *\/ */
8300: /* /\* nbcode[1][1]=0 nbcode[1][2]=1;*\/ */
8301: /* } */
8302: /* /\* V2+V1+V4+V3*age Tvar[4]=3 ; V1+V2*age Tvar[2]=2; V1+V1*age Tvar[2]=1, Tage[1]=2 *\/ */
8303: /* /\* ) p nbcode[Tvar[Tage[k]]][(1 & (ij-1) >> (k-1))+1] *\/ */
8304: /* /\*for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; *\/ */
8305: /* for (k=1; k<=cptcovage;k++){ /\* For product with age *\/ */
8306: /* if(Dummy[Tage[k]]==2){ /\* dummy with age *\/ */
8307: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(j1,TnsdVar[Tvar[Tage[k]]])]*cov[2]; */
8308: /* /\* cov[++k1]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; *\/ */
8309: /* } else if(Dummy[Tage[k]]==3){ /\* quantitative with age *\/ */
8310: /* printf("Internal IMaCh error, don't know which value for quantitative covariate with age, Tage[k]%d, k=%d, Tvar[Tage[k]]=V%d, age=%d\n",Tage[k],k ,Tvar[Tage[k]], (int)cov[2]); */
8311: /* /\* cov[2+nagesqr+Tage[k]]=meanq[k]/idq[k]*cov[2];/\\* Using the mean of quantitative variable Tvar[Tage[k]] /\\* Tqresult[nres][k]; *\\/ *\/ */
8312: /* /\* exit(1); *\/ */
8313: /* /\* cov[++k1]=Tqresult[nres][k]; *\/ */
8314: /* } */
8315: /* /\* cov[2+Tage[k]+nagesqr]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; *\/ */
8316: /* } */
8317: /* for (k=1; k<=cptcovprod;k++){/\* For product without age *\/ */
8318: /* if(Dummy[Tvard[k][1]]==0){ */
8319: /* if(Dummy[Tvard[k][2]]==0){ */
8320: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(j1,TnsdVar[Tvard[k][1]])] * nbcode[Tvard[k][2]][codtabm(j1,TnsdVar[Tvard[k][2]])]; */
8321: /* /\* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
8322: /* }else{ /\* Should we use the mean of the quantitative variables? *\/ */
8323: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(j1,TnsdVar[Tvard[k][1]])] * Tqresult[nres][resultmodel[nres][k]]; */
8324: /* /\* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * Tqresult[nres][k]; *\/ */
8325: /* } */
8326: /* }else{ */
8327: /* if(Dummy[Tvard[k][2]]==0){ */
8328: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(j1,TnsdVar[Tvard[k][2]])] * Tqinvresult[nres][TnsdVar[Tvard[k][1]]]; */
8329: /* /\* cov[++k1]=nbcode[Tvard[k][2]][codtabm(ij,k)] * Tqinvresult[nres][Tvard[k][1]]; *\/ */
8330: /* }else{ */
8331: /* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][TnsdVar[Tvard[k][1]]]* Tqinvresult[nres][TnsdVar[Tvard[k][2]]]; */
8332: /* /\* cov[++k1]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; *\/ */
8333: /* } */
8334: /* } */
8335: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
8336: /* } */
8337: /* For each age and combination of dummy covariates we slightly move the parameters of delti in order to get the gradient*/
8338: for(theta=1; theta <=npar; theta++){
8339: for(i=1; i<=npar; i++)
8340: xp[i] = x[i] + (i==theta ?delti[theta]:(double)0);
8341:
8342: pmij(pmmij,cov,ncovmodel,xp,nlstate);
8343:
8344: k=0;
8345: for(i=1; i<= (nlstate); i++){
8346: for(j=1; j<=(nlstate+ndeath);j++){
8347: k=k+1;
8348: gp[k]=pmmij[i][j];
8349: }
8350: }
8351:
8352: for(i=1; i<=npar; i++)
8353: xp[i] = x[i] - (i==theta ?delti[theta]:(double)0);
8354:
8355: pmij(pmmij,cov,ncovmodel,xp,nlstate);
8356: k=0;
8357: for(i=1; i<=(nlstate); i++){
8358: for(j=1; j<=(nlstate+ndeath);j++){
8359: k=k+1;
8360: gm[k]=pmmij[i][j];
8361: }
8362: }
8363:
8364: for(i=1; i<= (nlstate)*(nlstate+ndeath); i++)
8365: gradg[theta][i]=(gp[i]-gm[i])/(double)2./delti[theta];
8366: }
8367:
8368: for(j=1; j<=(nlstate)*(nlstate+ndeath);j++)
8369: for(theta=1; theta <=npar; theta++)
8370: trgradg[j][theta]=gradg[theta][j];
8371:
8372: matprod2(dnewm,trgradg,1,(nlstate)*(nlstate+ndeath),1,npar,1,npar,matcov);
8373: matprod2(doldm,dnewm,1,(nlstate)*(nlstate+ndeath),1,npar,1,(nlstate)*(nlstate+ndeath),gradg);
8374:
8375: pmij(pmmij,cov,ncovmodel,x,nlstate);
8376:
8377: k=0;
8378: for(i=1; i<=(nlstate); i++){
8379: for(j=1; j<=(nlstate+ndeath);j++){
8380: k=k+1;
8381: mu[k][(int) age]=pmmij[i][j];
8382: }
8383: }
8384: for(i=1;i<=(nlstate)*(nlstate+ndeath);i++)
8385: for(j=1;j<=(nlstate)*(nlstate+ndeath);j++)
8386: varpij[i][j][(int)age] = doldm[i][j];
8387:
8388: /*printf("\n%d ",(int)age);
8389: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
8390: printf("%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
8391: fprintf(ficlog,"%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
8392: }*/
8393:
8394: fprintf(ficresprob,"\n%d ",(int)age);
8395: fprintf(ficresprobcov,"\n%d ",(int)age);
8396: fprintf(ficresprobcor,"\n%d ",(int)age);
8397:
8398: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++)
8399: fprintf(ficresprob,"%11.3e (%11.3e) ",mu[i][(int) age],sqrt(varpij[i][i][(int)age]));
8400: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
8401: fprintf(ficresprobcov,"%11.3e ",mu[i][(int) age]);
8402: fprintf(ficresprobcor,"%11.3e ",mu[i][(int) age]);
8403: }
8404: i=0;
8405: for (k=1; k<=(nlstate);k++){
8406: for (l=1; l<=(nlstate+ndeath);l++){
8407: i++;
8408: fprintf(ficresprobcov,"\n%d %d-%d",(int)age,k,l);
8409: fprintf(ficresprobcor,"\n%d %d-%d",(int)age,k,l);
8410: for (j=1; j<=i;j++){
8411: /* printf(" k=%d l=%d i=%d j=%d\n",k,l,i,j);fflush(stdout); */
8412: fprintf(ficresprobcov," %11.3e",varpij[i][j][(int)age]);
8413: fprintf(ficresprobcor," %11.3e",varpij[i][j][(int) age]/sqrt(varpij[i][i][(int) age])/sqrt(varpij[j][j][(int)age]));
8414: }
8415: }
8416: }/* end of loop for state */
8417: } /* end of loop for age */
8418: free_vector(gp,1,(nlstate+ndeath)*(nlstate+ndeath));
8419: free_vector(gm,1,(nlstate+ndeath)*(nlstate+ndeath));
8420: free_matrix(trgradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
8421: free_matrix(gradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
8422:
8423: /* Confidence intervalle of pij */
8424: /*
8425: fprintf(ficgp,"\nunset parametric;unset label");
8426: fprintf(ficgp,"\nset log y;unset log x; set xlabel \"Age\";set ylabel \"probability (year-1)\"");
8427: fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65");
8428: 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);
8429: fprintf(fichtm,"\n<br><img src=\"pijgr%s.png\"> ",optionfilefiname);
8430: fprintf(ficgp,"\nset out \"pijgr%s.png\"",optionfilefiname);
8431: fprintf(ficgp,"\nplot \"%s\" every :::%d::%d u 1:2 \"\%%lf",k1,k2,xfilevarprob);
8432: */
8433:
8434: /* Drawing ellipsoids of confidence of two variables p(k1-l1,k2-l2)*/
8435: first1=1;first2=2;
8436: for (k2=1; k2<=(nlstate);k2++){
8437: for (l2=1; l2<=(nlstate+ndeath);l2++){
8438: if(l2==k2) continue;
8439: j=(k2-1)*(nlstate+ndeath)+l2;
8440: for (k1=1; k1<=(nlstate);k1++){
8441: for (l1=1; l1<=(nlstate+ndeath);l1++){
8442: if(l1==k1) continue;
8443: i=(k1-1)*(nlstate+ndeath)+l1;
8444: if(i<=j) continue;
8445: for (age=bage; age<=fage; age ++){
8446: if ((int)age %5==0){
8447: v1=varpij[i][i][(int)age]/stepm*YEARM/stepm*YEARM;
8448: v2=varpij[j][j][(int)age]/stepm*YEARM/stepm*YEARM;
8449: cv12=varpij[i][j][(int)age]/stepm*YEARM/stepm*YEARM;
8450: mu1=mu[i][(int) age]/stepm*YEARM ;
8451: mu2=mu[j][(int) age]/stepm*YEARM;
8452: c12=cv12/sqrt(v1*v2);
8453: /* Computing eigen value of matrix of covariance */
8454: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
8455: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
8456: if ((lc2 <0) || (lc1 <0) ){
8457: if(first2==1){
8458: first1=0;
8459: 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);
8460: }
8461: 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);
8462: /* lc1=fabs(lc1); */ /* If we want to have them positive */
8463: /* lc2=fabs(lc2); */
8464: }
8465:
8466: /* Eigen vectors */
8467: if(1+(v1-lc1)*(v1-lc1)/cv12/cv12 <1.e-5){
8468: printf(" Error sqrt of a negative number: %lf\n",1+(v1-lc1)*(v1-lc1)/cv12/cv12);
8469: fprintf(ficlog," Error sqrt of a negative number: %lf\n",1+(v1-lc1)*(v1-lc1)/cv12/cv12);
8470: v11=(1./sqrt(fabs(1+(v1-lc1)*(v1-lc1)/cv12/cv12)));
8471: }else
8472: v11=(1./sqrt(1+(v1-lc1)*(v1-lc1)/cv12/cv12));
8473: /*v21=sqrt(1.-v11*v11); *//* error */
8474: v21=(lc1-v1)/cv12*v11;
8475: v12=-v21;
8476: v22=v11;
8477: tnalp=v21/v11;
8478: if(first1==1){
8479: first1=0;
8480: 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);
8481: }
8482: 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);
8483: /*printf(fignu*/
8484: /* mu1+ v11*lc1*cost + v12*lc2*sin(t) */
8485: /* mu2+ v21*lc1*cost + v22*lc2*sin(t) */
8486: if(first==1){
8487: first=0;
8488: fprintf(ficgp,"\n# Ellipsoids of confidence\n#\n");
8489: fprintf(ficgp,"\nset parametric;unset label");
8490: 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);
8491: fprintf(ficgp,"\nset ter svg size 640, 480");
8492: fprintf(fichtmcov,"\n<p><br>Ellipsoids of confidence cov(p%1d%1d,p%1d%1d) expressed in year<sup>-1</sup>\
8493: :<a href=\"%s_%d%1d%1d-%1d%1d.svg\"> \
8494: %s_%d%1d%1d-%1d%1d.svg</A>, ",k1,l1,k2,l2,\
8495: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2, \
8496: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
8497: fprintf(fichtmcov,"\n<br><img src=\"%s_%d%1d%1d-%1d%1d.svg\"> ",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
8498: fprintf(fichtmcov,"\n<br> Correlation at age %d (%.3f),",(int) age, c12);
8499: fprintf(ficgp,"\nset out \"%s_%d%1d%1d-%1d%1d.svg\"",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
8500: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
8501: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
8502: 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", \
8503: mu1,std,v11,sqrt(fabs(lc1)),v12,sqrt(fabs(lc2)), \
8504: mu2,std,v21,sqrt(fabs(lc1)),v22,sqrt(fabs(lc2))); /* For gnuplot only */
8505: }else{
8506: first=0;
8507: fprintf(fichtmcov," %d (%.3f),",(int) age, c12);
8508: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
8509: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
8510: 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", \
8511: mu1,std,v11,sqrt(lc1),v12,sqrt(fabs(lc2)), \
8512: mu2,std,v21,sqrt(lc1),v22,sqrt(fabs(lc2)));
8513: }/* if first */
8514: } /* age mod 5 */
8515: } /* end loop age */
8516: fprintf(ficgp,"\nset out;\nset out \"%s_%d%1d%1d-%1d%1d.svg\";replot;set out;",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
8517: first=1;
8518: } /*l12 */
8519: } /* k12 */
8520: } /*l1 */
8521: }/* k1 */
8522: } /* loop on combination of covariates j1 */
8523: } /* loop on nres */
8524: free_ma3x(varpij,1,nlstate,1,nlstate+ndeath,(int) bage, (int)fage);
8525: free_matrix(mu,1,(nlstate+ndeath)*(nlstate+ndeath),(int) bage, (int)fage);
8526: free_matrix(doldm,1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
8527: free_matrix(dnewm,1,(nlstate)*(nlstate+ndeath),1,npar);
8528: free_vector(xp,1,npar);
8529: fclose(ficresprob);
8530: fclose(ficresprobcov);
8531: fclose(ficresprobcor);
8532: fflush(ficgp);
8533: fflush(fichtmcov);
8534: }
8535:
8536:
8537: /******************* Printing html file ***********/
8538: void printinghtml(char fileresu[], char title[], char datafile[], int firstpass, \
8539: int lastpass, int stepm, int weightopt, char model[],\
8540: int imx,int jmin, int jmax, double jmeanint,char rfileres[],\
8541: int popforecast, int mobilav, int prevfcast, int mobilavproj, int prevbcast, int estepm , \
8542: double jprev1, double mprev1,double anprev1, double dateprev1, double dateprojd, double dateback1, \
8543: double jprev2, double mprev2,double anprev2, double dateprev2, double dateprojf, double dateback2){
8544: int jj1, k1, i1, cpt, k4, nres;
8545: /* In fact some results are already printed in fichtm which is open */
8546: fprintf(fichtm,"<ul><li><a href='#firstorder'>Result files (first order: no variance)</a>\n \
8547: <li><a href='#secondorder'>Result files (second order (variance)</a>\n \
8548: </ul>");
8549: /* fprintf(fichtm,"<ul><li> model=1+age+%s\n \ */
8550: /* </ul>", model); */
8551: fprintf(fichtm,"<ul><li><h4><a name='firstorder'>Result files (first order: no variance)</a></h4>\n");
8552: 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",
8553: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTMFR_",".htm"),subdirfext3(optionfilefiname,"PHTMFR_",".htm"));
8554: fprintf(fichtm,"<li> - Observed prevalence (cross-sectional prevalence) in each state (during the period defined between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf): <a href=\"%s\">%s</a> (html file) ",
8555: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTM_",".htm"),subdirfext3(optionfilefiname,"PHTM_",".htm"));
8556: fprintf(fichtm,", <a href=\"%s\">%s</a> (text file) <br>\n",subdirf2(fileresu,"P_"),subdirf2(fileresu,"P_"));
8557: fprintf(fichtm,"\
8558: - Estimated transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
8559: stepm,subdirf2(fileresu,"PIJ_"),subdirf2(fileresu,"PIJ_"));
8560: fprintf(fichtm,"\
8561: - Estimated back transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
8562: stepm,subdirf2(fileresu,"PIJB_"),subdirf2(fileresu,"PIJB_"));
8563: fprintf(fichtm,"\
8564: - Period (forward) prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
8565: subdirf2(fileresu,"PL_"),subdirf2(fileresu,"PL_"));
8566: fprintf(fichtm,"\
8567: - Backward prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
8568: subdirf2(fileresu,"PLB_"),subdirf2(fileresu,"PLB_"));
8569: fprintf(fichtm,"\
8570: - (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): \
8571: <a href=\"%s\">%s</a> <br>\n",
8572: estepm,subdirf2(fileresu,"E_"),subdirf2(fileresu,"E_"));
8573: if(prevfcast==1){
8574: fprintf(fichtm,"\
8575: - Prevalence projections by age and states: \
8576: <a href=\"%s\">%s</a> <br>\n</li>", subdirf2(fileresu,"F_"),subdirf2(fileresu,"F_"));
8577: }
8578:
8579:
8580: m=pow(2,cptcoveff);
8581: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
8582:
8583: fprintf(fichtm," \n<ul><li><b>Graphs (first order)</b></li><p>");
8584:
8585: jj1=0;
8586:
8587: fprintf(fichtm," \n<ul>");
8588: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
8589: /* k1=nres; */
8590: k1=TKresult[nres];
8591: if(TKresult[nres]==0)k1=1; /* To be checked for no result */
8592: /* for(k1=1; k1<=m;k1++){ /\* For each combination of covariate *\/ */
8593: /* if(m != 1 && TKresult[nres]!= k1) */
8594: /* continue; */
8595: jj1++;
8596: if (cptcovn > 0) {
8597: fprintf(fichtm,"\n<li><a size=\"1\" color=\"#EC5E5E\" href=\"#rescov");
8598: for (cpt=1; cpt<=cptcovs;cpt++){ /**< cptcovs number of SIMPLE covariates in the model V2+V1 =2 (dummy or quantit or time varying) */
8599: fprintf(fichtm,"_V%d=%lg_",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
8600: }
8601: /* for (cpt=1; cpt<=cptcoveff;cpt++){ */
8602: /* fprintf(fichtm,"_V%d=%d_",Tvresult[nres][cpt],(int)Tresult[nres][cpt]); */
8603: /* } */
8604: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
8605: /* fprintf(fichtm,"_V%d=%f_",Tvqresult[nres][k4],Tqresult[nres][k4]); */
8606: /* } */
8607: fprintf(fichtm,"\">");
8608:
8609: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
8610: fprintf(fichtm,"************ Results for covariates");
8611: for (cpt=1; cpt<=cptcovs;cpt++){
8612: fprintf(fichtm," V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
8613: }
8614: /* fprintf(fichtm,"************ Results for covariates"); */
8615: /* for (cpt=1; cpt<=cptcoveff;cpt++){ */
8616: /* fprintf(fichtm," V%d=%d ",Tvresult[nres][cpt],(int)Tresult[nres][cpt]); */
8617: /* } */
8618: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
8619: /* fprintf(fichtm," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
8620: /* } */
8621: if(invalidvarcomb[k1]){
8622: fprintf(fichtm," Warning Combination (%d) ignored because no cases ",k1);
8623: continue;
8624: }
8625: fprintf(fichtm,"</a></li>");
8626: } /* cptcovn >0 */
8627: }
8628: fprintf(fichtm," \n</ul>");
8629:
8630: jj1=0;
8631:
8632: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
8633: /* k1=nres; */
8634: k1=TKresult[nres];
8635: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
8636: /* for(k1=1; k1<=m;k1++){ /\* For each combination of covariate *\/ */
8637: /* if(m != 1 && TKresult[nres]!= k1) */
8638: /* continue; */
8639:
8640: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
8641: jj1++;
8642: if (cptcovn > 0) {
8643: fprintf(fichtm,"\n<p><a name=\"rescov");
8644: for (cpt=1; cpt<=cptcovs;cpt++){
8645: fprintf(fichtm,"_V%d=%lg_",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
8646: }
8647: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
8648: /* fprintf(fichtm,"_V%d=%f_",Tvqresult[nres][k4],Tqresult[nres][k4]); */
8649: /* } */
8650: fprintf(fichtm,"\"</a>");
8651:
8652: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
8653: for (cpt=1; cpt<=cptcovs;cpt++){
8654: fprintf(fichtm," V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
8655: printf(" V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
8656: /* fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]); */
8657: /* printf(" V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]);fflush(stdout); */
8658: }
8659: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
8660: fprintf(fichtm," (model=1+age+%s) ************\n<hr size=\"2\" color=\"#EC5E5E\">",model);
8661: if(invalidvarcomb[k1]){
8662: fprintf(fichtm,"\n<h3>Combination (%d) ignored because no cases </h3>\n",k1);
8663: printf("\nCombination (%d) ignored because no cases \n",k1);
8664: continue;
8665: }
8666: }
8667: /* aij, bij */
8668: fprintf(fichtm,"<br>- Logit model (yours is: logit(pij)=log(pij/pii)= aij+ bij age+%s) as a function of age: <a href=\"%s_%d-1-%d.svg\">%s_%d-1-%d.svg</a><br> \
8669: <img src=\"%s_%d-1-%d.svg\">",model,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres);
8670: /* Pij */
8671: 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-%d.svg\">%s_%d-2-%d.svg</a><br> \
8672: <img src=\"%s_%d-2-%d.svg\">",stepm,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres);
8673: /* Quasi-incidences */
8674: fprintf(fichtm,"<br>\n- I<sub>ij</sub> or Conditional probabilities to be observed in state j being in state i %d (stepm) months\
8675: before but expressed in per year i.e. quasi incidences if stepm is small and probabilities too, \
8676: incidence (rates) are the limit when h tends to zero of the ratio of the probability <sub>h</sub>P<sub>ij</sub> \
8677: divided by h: <sub>h</sub>P<sub>ij</sub>/h : <a href=\"%s_%d-3-%d.svg\">%s_%d-3-%d.svg</a><br> \
8678: <img src=\"%s_%d-3-%d.svg\">",stepm,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres);
8679: /* Survival functions (period) in state j */
8680: for(cpt=1; cpt<=nlstate;cpt++){
8681: fprintf(fichtm,"<br>\n- Survival functions in state %d. And probability to be observed in state %d being in state (1 to %d) at different ages. Mean times spent in state (or Life Expectancy or Health Expectancy etc.) are the areas under each curve. <a href=\"%s_%d-%d-%d.svg\">%s_%d-%d-%d.svg</a><br>", cpt, cpt, nlstate, subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres,subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres);
8682: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"PIJ_"),subdirf2(optionfilefiname,"PIJ_"));
8683: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres);
8684: }
8685: /* State specific survival functions (period) */
8686: for(cpt=1; cpt<=nlstate;cpt++){
8687: fprintf(fichtm,"<br>\n- Survival functions in state %d and in any other live state (total).\
8688: And probability to be observed in various states (up to %d) being in state %d at different ages. Mean times spent in state (or Life Expectancy or Health Expectancy etc.) are the areas under each curve. \
8689: <a href=\"%s_%d-%d-%d.svg\">%s_%d-%d-%d.svg</a><br> ", cpt, nlstate, cpt, subdirf2(optionfilefiname,"LIJT_"),cpt,k1,nres,subdirf2(optionfilefiname,"LIJT_"),cpt,k1,nres);
8690: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"PIJ_"),subdirf2(optionfilefiname,"PIJ_"));
8691: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",subdirf2(optionfilefiname,"LIJT_"),cpt,k1,nres);
8692: }
8693: /* Period (forward stable) prevalence in each health state */
8694: for(cpt=1; cpt<=nlstate;cpt++){
8695: fprintf(fichtm,"<br>\n- Convergence to period (stable) prevalence in state %d. Or probability for a person being in state (1 to %d) at different ages, to be alive in state %d some years after. <a href=\"%s_%d-%d-%d.svg\">%s_%d-%d-%d.svg</a><br>", cpt, nlstate, cpt, subdirf2(optionfilefiname,"P_"),cpt,k1,nres,subdirf2(optionfilefiname,"P_"),cpt,k1,nres);
8696: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"PIJ_"),subdirf2(optionfilefiname,"PIJ_"));
8697: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">" ,subdirf2(optionfilefiname,"P_"),cpt,k1,nres);
8698: }
8699: if(prevbcast==1){
8700: /* Backward prevalence in each health state */
8701: for(cpt=1; cpt<=nlstate;cpt++){
8702: fprintf(fichtm,"<br>\n- Convergence to mixed (stable) back prevalence in state %d. Or probability for a person to be in state %d at a younger age, knowing that she/he was in state (1 to %d) at different older ages. <a href=\"%s_%d-%d-%d.svg\">%s_%d-%d-%d.svg</a><br>", cpt, cpt, nlstate, subdirf2(optionfilefiname,"PB_"),cpt,k1,nres,subdirf2(optionfilefiname,"PB_"),cpt,k1,nres);
8703: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"PIJB_"),subdirf2(optionfilefiname,"PIJB_"));
8704: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">" ,subdirf2(optionfilefiname,"PB_"),cpt,k1,nres);
8705: }
8706: }
8707: if(prevfcast==1){
8708: /* Projection of prevalence up to period (forward stable) prevalence in each health state */
8709: for(cpt=1; cpt<=nlstate;cpt++){
8710: fprintf(fichtm,"<br>\n- Projection of cross-sectional prevalence (estimated with cases observed from %.1f to %.1f and mobil_average=%d), from year %.1f up to year %.1f tending to period (stable) forward prevalence in state %d. Or probability to be in state %d being in an observed weighted state (from 1 to %d). <a href=\"%s_%d-%d-%d.svg\">%s_%d-%d-%d.svg</a>", dateprev1, dateprev2, mobilavproj, dateprojd, dateprojf, cpt, cpt, nlstate, subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres,subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres);
8711: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"F_"),subdirf2(optionfilefiname,"F_"));
8712: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",
8713: subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres);
8714: }
8715: }
8716: if(prevbcast==1){
8717: /* Back projection of prevalence up to stable (mixed) back-prevalence in each health state */
8718: for(cpt=1; cpt<=nlstate;cpt++){
8719: fprintf(fichtm,"<br>\n- Back projection of cross-sectional prevalence (estimated with cases observed from %.1f to %.1f and mobil_average=%d), \
8720: from year %.1f up to year %.1f (probably close to stable [mixed] back prevalence in state %d). Randomness in cross-sectional prevalence is not taken into \
8721: account but can visually be appreciated. Or probability to have been in an state %d, knowing that the person was in either state (1 or %d) \
8722: with weights corresponding to observed prevalence at different ages. <a href=\"%s_%d-%d-%d.svg\">%s_%d-%d-%d.svg</a>", dateprev1, dateprev2, mobilavproj, dateback1, dateback2, cpt, cpt, nlstate, subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres,subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres);
8723: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"FB_"),subdirf2(optionfilefiname,"FB_"));
8724: fprintf(fichtm," <img src=\"%s_%d-%d-%d.svg\">", subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres);
8725: }
8726: }
8727:
8728: for(cpt=1; cpt<=nlstate;cpt++) {
8729: 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-%d.svg\">%s_%d-%d-%d.svg</a>",cpt,nlstate,subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres,subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres);
8730: fprintf(fichtm," (data from text file <a href=\"%s.txt\"> %s.txt</a>)\n<br>",subdirf2(optionfilefiname,"E_"),subdirf2(optionfilefiname,"E_"));
8731: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">", subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres );
8732: }
8733: /* } /\* end i1 *\/ */
8734: }/* End k1=nres */
8735: fprintf(fichtm,"</ul>");
8736:
8737: fprintf(fichtm,"\
8738: \n<br><li><h4> <a name='secondorder'>Result files (second order: variances)</a></h4>\n\
8739: - Parameter file with estimated parameters and covariance matrix: <a href=\"%s\">%s</a> <br> \
8740: - 95%% confidence intervals and Wald tests of the estimated parameters are in the log file if optimization has been done (mle != 0).<br> \
8741: But because parameters are usually highly correlated (a higher incidence of disability \
8742: and a higher incidence of recovery can give very close observed transition) it might \
8743: be very useful to look not only at linear confidence intervals estimated from the \
8744: variances but at the covariance matrix. And instead of looking at the estimated coefficients \
8745: (parameters) of the logistic regression, it might be more meaningful to visualize the \
8746: covariance matrix of the one-step probabilities. \
8747: See page 'Matrix of variance-covariance of one-step probabilities' below. \n", rfileres,rfileres);
8748:
8749: fprintf(fichtm," - Standard deviation of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
8750: subdirf2(fileresu,"PROB_"),subdirf2(fileresu,"PROB_"));
8751: fprintf(fichtm,"\
8752: - Variance-covariance of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
8753: subdirf2(fileresu,"PROBCOV_"),subdirf2(fileresu,"PROBCOV_"));
8754:
8755: fprintf(fichtm,"\
8756: - Correlation matrix of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
8757: subdirf2(fileresu,"PROBCOR_"),subdirf2(fileresu,"PROBCOR_"));
8758: fprintf(fichtm,"\
8759: - 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): \
8760: <a href=\"%s\">%s</a> <br>\n</li>",
8761: estepm,subdirf2(fileresu,"CVE_"),subdirf2(fileresu,"CVE_"));
8762: fprintf(fichtm,"\
8763: - (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): \
8764: <a href=\"%s\">%s</a> <br>\n</li>",
8765: estepm,subdirf2(fileresu,"STDE_"),subdirf2(fileresu,"STDE_"));
8766: fprintf(fichtm,"\
8767: - Variances and covariances of health expectancies by age. Status (i) based health expectancies (in state j), e<sup>ij</sup> are weighted by the forward (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",
8768: estepm, subdirf2(fileresu,"V_"),subdirf2(fileresu,"V_"));
8769: fprintf(fichtm,"\
8770: - 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",
8771: estepm, subdirf2(fileresu,"T_"),subdirf2(fileresu,"T_"));
8772: fprintf(fichtm,"\
8773: - Standard deviation of forward (period) prevalences: <a href=\"%s\">%s</a> <br>\n",\
8774: subdirf2(fileresu,"VPL_"),subdirf2(fileresu,"VPL_"));
8775:
8776: /* if(popforecast==1) fprintf(fichtm,"\n */
8777: /* - Prevalences forecasting: <a href=\"f%s\">f%s</a> <br>\n */
8778: /* - Population forecasting (if popforecast=1): <a href=\"pop%s\">pop%s</a> <br>\n */
8779: /* <br>",fileres,fileres,fileres,fileres); */
8780: /* else */
8781: /* fprintf(fichtm,"\n No population forecast: popforecast = %d (instead of 1) or stepm = %d (instead of 1) or model=1+age+%s (instead of .)<br><br></li>\n",popforecast, stepm, model); */
8782: fflush(fichtm);
8783:
8784: m=pow(2,cptcoveff);
8785: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
8786:
8787: fprintf(fichtm," <ul><li><b>Graphs (second order)</b></li><p>");
8788:
8789: jj1=0;
8790:
8791: fprintf(fichtm," \n<ul>");
8792: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
8793: /* k1=nres; */
8794: k1=TKresult[nres];
8795: /* for(k1=1; k1<=m;k1++){ /\* For each combination of covariate *\/ */
8796: /* if(m != 1 && TKresult[nres]!= k1) */
8797: /* continue; */
8798: jj1++;
8799: if (cptcovn > 0) {
8800: fprintf(fichtm,"\n<li><a size=\"1\" color=\"#EC5E5E\" href=\"#rescovsecond");
8801: for (cpt=1; cpt<=cptcovs;cpt++){
8802: fprintf(fichtm,"_V%d=%lg_",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
8803: }
8804: fprintf(fichtm,"\">");
8805:
8806: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
8807: fprintf(fichtm,"************ Results for covariates");
8808: for (cpt=1; cpt<=cptcovs;cpt++){
8809: fprintf(fichtm," V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
8810: }
8811: if(invalidvarcomb[k1]){
8812: fprintf(fichtm," Warning Combination (%d) ignored because no cases ",k1);
8813: continue;
8814: }
8815: fprintf(fichtm,"</a></li>");
8816: } /* cptcovn >0 */
8817: } /* End nres */
8818: fprintf(fichtm," \n</ul>");
8819:
8820: jj1=0;
8821:
8822: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
8823: /* k1=nres; */
8824: k1=TKresult[nres];
8825: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
8826: /* for(k1=1; k1<=m;k1++){ */
8827: /* if(m != 1 && TKresult[nres]!= k1) */
8828: /* continue; */
8829: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
8830: jj1++;
8831: if (cptcovn > 0) {
8832: fprintf(fichtm,"\n<p><a name=\"rescovsecond");
8833: for (cpt=1; cpt<=cptcovs;cpt++){
8834: fprintf(fichtm,"_V%d=%lg_",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
8835: }
8836: fprintf(fichtm,"\"</a>");
8837:
8838: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
8839: for (cpt=1; cpt<=cptcovs;cpt++){ /**< cptcoveff number of variables */
8840: fprintf(fichtm," V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
8841: printf(" V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
8842: /* fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]); */
8843: }
8844:
8845: fprintf(fichtm," (model=1+age+%s) ************\n<hr size=\"2\" color=\"#EC5E5E\">",model);
8846:
8847: if(invalidvarcomb[k1]){
8848: fprintf(fichtm,"\n<h4>Combination (%d) ignored because no cases </h4>\n",k1);
8849: continue;
8850: }
8851: } /* If cptcovn >0 */
8852: for(cpt=1; cpt<=nlstate;cpt++) {
8853: fprintf(fichtm,"\n<br>- Observed (cross-sectional with mov_average=%d) and period (incidence based) \
8854: prevalence (with 95%% confidence interval) in state (%d): <a href=\"%s_%d-%d-%d.svg\"> %s_%d-%d-%d.svg</a>",mobilav,cpt,subdirf2(optionfilefiname,"V_"),cpt,k1,nres,subdirf2(optionfilefiname,"V_"),cpt,k1,nres);
8855: fprintf(fichtm," (data from text file <a href=\"%s\">%s</a>)\n <br>",subdirf2(fileresu,"VPL_"),subdirf2(fileresu,"VPL_"));
8856: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",subdirf2(optionfilefiname,"V_"), cpt,k1,nres);
8857: }
8858: fprintf(fichtm,"\n<br>- Total life expectancy by age and \
8859: health expectancies in each live states (1 to %d). If popbased=1 the smooth (due to the model) \
8860: true period expectancies (those weighted with period prevalences are also\
8861: drawn in addition to the population based expectancies computed using\
8862: observed and cahotic prevalences: <a href=\"%s_%d-%d.svg\">%s_%d-%d.svg</a>",nlstate, subdirf2(optionfilefiname,"E_"),k1,nres,subdirf2(optionfilefiname,"E_"),k1,nres);
8863: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>) \n<br>",subdirf2(optionfilefiname,"T_"),subdirf2(optionfilefiname,"T_"));
8864: fprintf(fichtm,"<img src=\"%s_%d-%d.svg\">",subdirf2(optionfilefiname,"E_"),k1,nres);
8865: /* } /\* end i1 *\/ */
8866: }/* End nres */
8867: fprintf(fichtm,"</ul>");
8868: fflush(fichtm);
8869: }
8870:
8871: /******************* Gnuplot file **************/
8872: void printinggnuplot(char fileresu[], char optionfilefiname[], double ageminpar, double agemaxpar, double bage, double fage , int prevfcast, int prevbcast, char pathc[], double p[], int offyear, int offbyear){
8873:
8874: char dirfileres[256],optfileres[256];
8875: char gplotcondition[256], gplotlabel[256];
8876: int cpt=0,k1=0,i=0,k=0,j=0,jk=0,k2=0,k3=0,k4=0,kf=0,kvar=0,kk=0,ipos=0,iposold=0,ij=0, ijp=0, l=0;
8877: int lv=0, vlv=0, kl=0;
8878: int ng=0;
8879: int vpopbased;
8880: int ioffset; /* variable offset for columns */
8881: int iyearc=1; /* variable column for year of projection */
8882: int iagec=1; /* variable column for age of projection */
8883: int nres=0; /* Index of resultline */
8884: int istart=1; /* For starting graphs in projections */
8885:
8886: /* if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) { */
8887: /* printf("Problem with file %s",optionfilegnuplot); */
8888: /* fprintf(ficlog,"Problem with file %s",optionfilegnuplot); */
8889: /* } */
8890:
8891: /*#ifdef windows */
8892: fprintf(ficgp,"cd \"%s\" \n",pathc);
8893: /*#endif */
8894: m=pow(2,cptcoveff);
8895:
8896: /* diagram of the model */
8897: fprintf(ficgp,"\n#Diagram of the model \n");
8898: fprintf(ficgp,"\ndelta=0.03;delta2=0.07;unset arrow;\n");
8899: fprintf(ficgp,"yoff=(%d > 2? 0:1);\n",nlstate);
8900: fprintf(ficgp,"\n#Peripheral arrows\nset for [i=1:%d] for [j=1:%d] arrow i*10+j from cos(pi*((1-(%d/2)*2./%d)/2+(i-1)*2./%d))-(i!=j?(i-j)/abs(i-j)*delta:0), yoff +sin(pi*((1-(%d/2)*2./%d)/2+(i-1)*2./%d)) + (i!=j?(i-j)/abs(i-j)*delta:0) rto -0.95*(cos(pi*((1-(%d/2)*2./%d)/2+(i-1)*2./%d))+(i!=j?(i-j)/abs(i-j)*delta:0) - cos(pi*((1-(%d/2)*2./%d)/2+(j-1)*2./%d)) + (i!=j?(i-j)/abs(i-j)*delta2:0)), -0.95*(sin(pi*((1-(%d/2)*2./%d)/2+(i-1)*2./%d)) + (i!=j?(i-j)/abs(i-j)*delta:0) - sin(pi*((1-(%d/2)*2./%d)/2+(j-1)*2./%d))+( i!=j?(i-j)/abs(i-j)*delta2:0)) ls (i < j? 1:2)\n",nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate);
8901:
8902: fprintf(ficgp,"\n#Centripete arrows (turning in other direction (1-i) instead of (i-1)) \nset for [i=1:%d] for [j=1:%d] arrow (%d+1)*10+i from cos(pi*((1-(%d/2)*2./%d)/2+(1-i)*2./%d))-(i!=j?(i-j)/abs(i-j)*delta:0), yoff +sin(pi*((1-(%d/2)*2./%d)/2+(1-i)*2./%d)) + (i!=j?(i-j)/abs(i-j)*delta:0) rto -0.80*(cos(pi*((1-(%d/2)*2./%d)/2+(1-i)*2./%d))+(i!=j?(i-j)/abs(i-j)*delta:0) ), -0.80*(sin(pi*((1-(%d/2)*2./%d)/2+(1-i)*2./%d)) + (i!=j?(i-j)/abs(i-j)*delta:0) + yoff ) ls 4\n",nlstate, nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate);
8903: fprintf(ficgp,"\n#show arrow\nunset label\n");
8904: fprintf(ficgp,"\n#States labels, starting from 2 (2-i) instead of (1-i), was (i-1)\nset for [i=1:%d] label i sprintf(\"State %%d\",i) center at cos(pi*((1-(%d/2)*2./%d)/2+(2-i)*2./%d)), yoff+sin(pi*((1-(%d/2)*2./%d)/2+(2-i)*2./%d)) font \"helvetica, 16\" tc rgbcolor \"blue\"\n",nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate);
8905: fprintf(ficgp,"\nset label %d+1 sprintf(\"State %%d\",%d+1) center at 0.,0. font \"helvetica, 16\" tc rgbcolor \"red\"\n",nlstate,nlstate);
8906: fprintf(ficgp,"\n#show label\nunset border;unset xtics; unset ytics;\n");
8907: fprintf(ficgp,"\n\nset ter svg size 640, 480;set out \"%s_.svg\" \n",subdirf2(optionfilefiname,"D_"));
8908: fprintf(ficgp,"unset log y; plot [-1.2:1.2][yoff-1.2:1.2] 1/0 not; set out;reset;\n");
8909:
8910: /* Contribution to likelihood */
8911: /* Plot the probability implied in the likelihood */
8912: fprintf(ficgp,"\n# Contributions to the Likelihood, mle >=1. For mle=4 no interpolation, pure matrix products.\n#\n");
8913: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Likelihood (-2Log(L))\";");
8914: /* fprintf(ficgp,"\nset ter svg size 640, 480"); */ /* Too big for svg */
8915: fprintf(ficgp,"\nset ter pngcairo size 640, 480");
8916: /* nice for mle=4 plot by number of matrix products.
8917: replot "rrtest1/toto.txt" u 2:($4 == 1 && $5==2 ? $9 : 1/0):5 t "p12" with point lc 1 */
8918: /* replot exp(p1+p2*x)/(1+exp(p1+p2*x)+exp(p3+p4*x)+exp(p5+p6*x)) t "p12(x)" */
8919: /* fprintf(ficgp,"\nset out \"%s.svg\";",subdirf2(optionfilefiname,"ILK_")); */
8920: fprintf(ficgp,"\nset out \"%s-dest.png\";",subdirf2(optionfilefiname,"ILK_"));
8921: 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));
8922: fprintf(ficgp,"\nset out \"%s-ori.png\";",subdirf2(optionfilefiname,"ILK_"));
8923: 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));
8924: for (i=1; i<= nlstate ; i ++) {
8925: fprintf(ficgp,"\nset out \"%s-p%dj.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i);
8926: fprintf(ficgp,"unset log;\n# plot weighted, mean weight should have point size of 0.5\n plot \"%s\"",subdirf(fileresilk));
8927: 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);
8928: for (j=2; j<= nlstate+ndeath ; j ++) {
8929: 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);
8930: }
8931: fprintf(ficgp,";\nset out; unset ylabel;\n");
8932: }
8933: /* 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 */
8934: /* fprintf(ficgp,"\nset log y;plot \"%s\" u 2:(-$11):3 t \"All sample, all transitions\" with dots lc variable",subdirf(fileresilk)); */
8935: /* fprintf(ficgp,"\nreplot \"%s\" u 2:($3 <= 3 ? -$11 : 1/0):3 t \"First 3 individuals\" with line lc variable", subdirf(fileresilk)); */
8936: fprintf(ficgp,"\nset out;unset log\n");
8937: /* fprintf(ficgp,"\nset out \"%s.svg\"; replot; set out; # bug gnuplot",subdirf2(optionfilefiname,"ILK_")); */
8938:
8939: /* Plot the probability implied in the likelihood by covariate value */
8940: fprintf(ficgp,"\nset ter pngcairo size 640, 480");
8941: /* if(debugILK==1){ */
8942: for(kf=1; kf <= ncovf; kf++){ /* For each simple dummy covariate of the model */
8943: kvar=Tvar[TvarFind[kf]]; /* variable name */
8944: /* k=18+Tvar[TvarFind[kf]];/\*offset because there are 18 columns in the ILK_ file but could be placed else where *\/ */
8945: /* k=18+kf;/\*offset because there are 18 columns in the ILK_ file *\/ */
8946: /* k=19+kf;/\*offset because there are 19 columns in the ILK_ file *\/ */
8947: k=16+nlstate+kf;/*offset because there are 19 columns in the ILK_ file, first cov Vn on col 21 with 4 living states */
8948: for (i=1; i<= nlstate ; i ++) {
8949: fprintf(ficgp,"\nset out \"%s-p%dj-%d.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i,kvar);
8950: fprintf(ficgp,"unset log;\n# For each simple dummy covariate of the model \n plot \"%s\"",subdirf(fileresilk));
8951: if(gnuplotversion >=5.2){ /* Former gnuplot versions do not have variable pointsize!! */
8952: fprintf(ficgp," u 2:($5 == %d && $6==%d ? $10 : 1/0):($%d==0 ? 7 : 9):($%d==0 ? $6 : $6+4) t \"p%d%d V%d\" with points pt variable ps 0.4 lc variable \\\n",i,1,k,k,i,1,kvar);
8953: for (j=2; j<= nlstate+ndeath ; j ++) {
8954: fprintf(ficgp,",\\\n \"\" u 2:($5 == %d && $6==%d ? $10 : 1/0):($%d==0 ? 7 : 9):($%d==0 ? $6 : $6+4) t \"p%d%d V%d\" with points pt variable ps 0.4 lc variable ",i,j,k,k,i,j,kvar);
8955: }
8956: }else{
8957: fprintf(ficgp," u 2:($5 == %d && $6==%d ? $10 : 1/0):($%d==0 ? $6 : $6+4) t \"p%d%d V%d\" with points pt 7 ps 0.4 lc variable \\\n",i,1,k,i,1,kvar);
8958: for (j=2; j<= nlstate+ndeath ; j ++) {
8959: fprintf(ficgp,",\\\n \"\" u 2:($5 == %d && $6==%d ? $10 : 1/0):($%d==0 ? $6 : $6+4) t \"p%d%d V%d\" with points pt 7 ps 0.4 lc variable ",i,j,k,i,j,kvar);
8960: }
8961: }
8962: fprintf(ficgp,";\nset out; unset ylabel;\n");
8963: }
8964: } /* End of each covariate dummy */
8965: for(ncovv=1, iposold=0, kk=0; ncovv <= ncovvt ; ncovv++){
8966: /* Other example V1 + V3 + V5 + age*V1 + age*V3 + age*V5 + V1*V3 + V3*V5 + V1*V5
8967: * kmodel = 1 2 3 4 5 6 7 8 9
8968: * varying 1 2 3 4 5
8969: * ncovv 1 2 3 4 5 6 7 8
8970: * TvarVV[ncovv] V3 5 1 3 3 5 1 5
8971: * TvarVVind[ncovv]=kmodel 2 3 7 7 8 8 9 9
8972: * TvarFind[kmodel] 1 0 0 0 0 0 0 0 0
8973: * kdata ncovcol=[V1 V2] nqv=0 ntv=[V3 V4] nqtv=V5
8974: * Dummy[kmodel] 0 0 1 2 2 3 1 1 1
8975: */
8976: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate */
8977: kvar=TvarVV[ncovv]; /* TvarVV={3, 1, 3} gives the name of each varying covariate */
8978: /* printf("DebugILK ficgp ncovv=%d, kvar=TvarVV[ncovv]=%d, ipos=TvarVVind[ncovv]=%d, Dummy[ipos]=%d, Typevar[ipos]=%d\n", ncovv,kvar,ipos,Dummy[ipos],Typevar[ipos]); */
8979: if(ipos!=iposold){ /* Not a product or first of a product */
8980: /* printf(" %d",ipos); */
8981: /* fprintf(ficresilk," V%d",TvarVV[ncovv]); */
8982: /* printf(" DebugILK ficgp suite ipos=%d != iposold=%d\n", ipos, iposold); */
8983: kk++; /* Position of the ncovv column in ILK_ */
8984: k=18+ncovf+kk; /*offset because there are 18 columns in the ILK_ file plus ncovf fixed covariate */
8985: if(Dummy[ipos]==0 && Typevar[ipos]==0){ /* Only if dummy time varying: Dummy(0, 1=quant singor prod without age,2 dummy*age, 3quant*age) Typevar (0 single, 1=*age,2=Vn*vm) */
8986: for (i=1; i<= nlstate ; i ++) {
8987: fprintf(ficgp,"\nset out \"%s-p%dj-%d.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i,kvar);
8988: fprintf(ficgp,"unset log;\n# For each simple dummy covariate of the model \n plot \"%s\"",subdirf(fileresilk));
8989:
8990: /* printf("Before DebugILK gnuplotversion=%g >=5.2\n",gnuplotversion); */
8991: if(gnuplotversion >=5.2){ /* Former gnuplot versions do not have variable pointsize!! */
8992: /* printf("DebugILK gnuplotversion=%g >=5.2\n",gnuplotversion); */
8993: fprintf(ficgp," u 2:($5 == %d && $6==%d ? $10 : 1/0):($%d==0 ? 7 : 9):($%d==0 ? $6 : $6+4) t \"p%d%d V%d\" with points pt variable ps 0.4 lc variable \\\n",i,1,k,k,i,1,kvar);
8994: for (j=2; j<= nlstate+ndeath ; j ++) {
8995: fprintf(ficgp,",\\\n \"\" u 2:($5 == %d && $6==%d ? $10 : 1/0):($%d==0 ? 7 : 9):($%d==0 ? $6 : $6+4) t \"p%d%d V%d\" with points pt variable ps 0.4 lc variable ",i,j,k,k,i,j,kvar);
8996: }
8997: }else{
8998: /* printf("DebugILK gnuplotversion=%g <5.2\n",gnuplotversion); */
8999: fprintf(ficgp," u 2:($5 == %d && $6==%d ? $10 : 1/0):($%d==0 ? $6 : $6+4) t \"p%d%d V%d\" with points pt 7 ps 0.4 lc variable \\\n",i,1,k,i,1,kvar);
9000: for (j=2; j<= nlstate+ndeath ; j ++) {
9001: fprintf(ficgp,",\\\n \"\" u 2:($5 == %d && $6==%d ? $10 : 1/0):($%d==0 ? $6 : $6+4) t \"p%d%d V%d\" with points pt 7 ps 0.4 lc variable ",i,j,k,i,j,kvar);
9002: }
9003: }
9004: fprintf(ficgp,";\nset out; unset ylabel;\n");
9005: }
9006: }/* End if dummy varying */
9007: }else{ /*Product */
9008: /* printf("*"); */
9009: /* fprintf(ficresilk,"*"); */
9010: }
9011: iposold=ipos;
9012: } /* For each time varying covariate */
9013: /* } /\* debugILK==1 *\/ */
9014: /* 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 */
9015: /* fprintf(ficgp,"\nset log y;plot \"%s\" u 2:(-$11):3 t \"All sample, all transitions\" with dots lc variable",subdirf(fileresilk)); */
9016: /* fprintf(ficgp,"\nreplot \"%s\" u 2:($3 <= 3 ? -$11 : 1/0):3 t \"First 3 individuals\" with line lc variable", subdirf(fileresilk)); */
9017: fprintf(ficgp,"\nset out;unset log\n");
9018: /* fprintf(ficgp,"\nset out \"%s.svg\"; replot; set out; # bug gnuplot",subdirf2(optionfilefiname,"ILK_")); */
9019:
9020:
9021:
9022: strcpy(dirfileres,optionfilefiname);
9023: strcpy(optfileres,"vpl");
9024: /* 1eme*/
9025: for (cpt=1; cpt<= nlstate ; cpt ++){ /* For each live state */
9026: /* for (k1=1; k1<= m ; k1 ++){ /\* For each valid combination of covariate *\/ */
9027: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
9028: k1=TKresult[nres];
9029: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
9030: /* plot [100000000000000000000:-100000000000000000000] "mysbiaspar/vplrmysbiaspar.txt to check */
9031: /* if(m != 1 && TKresult[nres]!= k1) */
9032: /* continue; */
9033: /* We are interested in selected combination by the resultline */
9034: /* printf("\n# 1st: Period (stable) prevalence with CI: 'VPL_' files and live state =%d ", cpt); */
9035: fprintf(ficgp,"\n# 1st: Forward (stable period) prevalence with CI: 'VPL_' files and live state =%d ", cpt);
9036: strcpy(gplotlabel,"(");
9037: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
9038: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
9039: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
9040:
9041: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate k get corresponding value lv for combination k1 *\/ */
9042: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the value of the covariate corresponding to k1 combination *\\/ *\/ */
9043: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
9044: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
9045: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
9046: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
9047: /* vlv= nbcode[Tvaraff[k]][lv]; /\* vlv is the value of the covariate lv, 0 or 1 *\/ */
9048: /* /\* For each combination of covariate k1 (V1=1, V3=0), we printed the current covariate k and its value vlv *\/ */
9049: /* /\* printf(" V%d=%d ",Tvaraff[k],vlv); *\/ */
9050: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
9051: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
9052: /* } */
9053: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
9054: /* /\* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); *\/ */
9055: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
9056: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
9057: }
9058: strcpy(gplotlabel+strlen(gplotlabel),")");
9059: /* printf("\n#\n"); */
9060: fprintf(ficgp,"\n#\n");
9061: if(invalidvarcomb[k1]){
9062: /*k1=k1-1;*/ /* To be checked */
9063: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
9064: continue;
9065: }
9066:
9067: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"V_"),cpt,k1,nres);
9068: fprintf(ficgp,"\n#set out \"V_%s_%d-%d-%d.svg\" \n",optionfilefiname,cpt,k1,nres);
9069: /* fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel); */
9070: fprintf(ficgp,"set title \"Alive state %d %s model=1+age+%s\" font \"Helvetica,12\"\n",cpt,gplotlabel,model);
9071: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \nset ter svg size 640, 480\nplot [%.f:%.f] \"%s\" every :::%d::%d u 1:($2==%d ? $3:1/0) \"%%lf %%lf",ageminpar,fage,subdirf2(fileresu,"VPL_"),nres-1,nres-1,nres);
9072: /* fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \nset ter svg size 640, 480\nplot [%.f:%.f] \"%s\" every :::%d::%d u 1:($2==%d ? $3:1/0) \"%%lf %%lf",ageminpar,fage,subdirf2(fileresu,"VPL_"),k1-1,k1-1,nres); */
9073: /* k1-1 error should be nres-1*/
9074: for (i=1; i<= nlstate ; i ++) {
9075: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
9076: else fprintf(ficgp," %%*lf (%%*lf)");
9077: }
9078: fprintf(ficgp,"\" t\"Forward prevalence\" w l lt 0,\"%s\" every :::%d::%d u 1:($2==%d ? $3+1.96*$4 : 1/0) \"%%lf %%lf",subdirf2(fileresu,"VPL_"),nres-1,nres-1,nres);
9079: for (i=1; i<= nlstate ; i ++) {
9080: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
9081: else fprintf(ficgp," %%*lf (%%*lf)");
9082: }
9083: fprintf(ficgp,"\" t\"95%% CI\" w l lt 1,\"%s\" every :::%d::%d u 1:($2==%d ? $3-1.96*$4 : 1/0) \"%%lf %%lf",subdirf2(fileresu,"VPL_"),nres-1,nres-1,nres);
9084: for (i=1; i<= nlstate ; i ++) {
9085: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
9086: else fprintf(ficgp," %%*lf (%%*lf)");
9087: }
9088: /* 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)); */
9089:
9090: fprintf(ficgp,"\" t\"\" w l lt 1,\"%s\" u 1:((",subdirf2(fileresu,"P_"));
9091: if(cptcoveff ==0){
9092: fprintf(ficgp,"$%d)) t 'Observed prevalence in state %d' with line lt 3", 2+3*(cpt-1), cpt );
9093: }else{
9094: kl=0;
9095: for (k=1; k<=cptcoveff; k++){ /* For each combination of covariate */
9096: /* lv= decodtabm(k1,k,cptcoveff); /\* Should be the covariate value corresponding to k1 combination and kth covariate *\/ */
9097: lv=codtabm(k1,TnsdVar[Tvaraff[k]]);
9098: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
9099: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
9100: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
9101: vlv= nbcode[Tvaraff[k]][lv];
9102: kl++;
9103: /* 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 *\/ */
9104: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
9105: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
9106: /* '' 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*/
9107: if(k==cptcoveff){
9108: fprintf(ficgp,"$%d==%d && $%d==%d)? $%d : 1/0) t 'Observed prevalence in state %d' w l lt 2",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv], \
9109: 2+cptcoveff*2+3*(cpt-1), cpt ); /* 4 or 6 ?*/
9110: }else{
9111: fprintf(ficgp,"$%d==%d && $%d==%d && ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv]);
9112: kl++;
9113: }
9114: } /* end covariate */
9115: } /* end if no covariate */
9116:
9117: if(prevbcast==1){ /* We need to get the corresponding values of the covariates involved in this combination k1 */
9118: /* 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); */
9119: fprintf(ficgp,",\"%s\" u 1:((",subdirf2(fileresu,"PLB_")); /* Age is in 1, nres in 2 to be fixed */
9120: if(cptcoveff ==0){
9121: fprintf(ficgp,"$%d)) t 'Backward prevalence in state %d' with line lt 3", 2+(cpt-1), cpt );
9122: }else{
9123: kl=0;
9124: for (k=1; k<=cptcoveff; k++){ /* For each combination of covariate */
9125: /* lv= decodtabm(k1,k,cptcoveff); /\* Should be the covariate value corresponding to k1 combination and kth covariate *\/ */
9126: lv=codtabm(k1,TnsdVar[Tvaraff[k]]);
9127: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
9128: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
9129: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
9130: /* vlv= nbcode[Tvaraff[k]][lv]; */
9131: vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])];
9132: kl++;
9133: /* 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 *\/ */
9134: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
9135: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
9136: /* '' 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*/
9137: if(k==cptcoveff){
9138: fprintf(ficgp,"$%d==%d && $%d==%d)? $%d : 1/0) t 'Backward prevalence in state %d' w l lt 3",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv], \
9139: 2+cptcoveff*2+(cpt-1), cpt ); /* 4 or 6 ?*/
9140: }else{
9141: fprintf(ficgp,"$%d==%d && $%d==%d && ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]);
9142: kl++;
9143: }
9144: } /* end covariate */
9145: } /* end if no covariate */
9146: if(prevbcast == 1){
9147: fprintf(ficgp,", \"%s\" every :::%d::%d u 1:($2==%d ? $3:1/0) \"%%lf %%lf",subdirf2(fileresu,"VBL_"),nres-1,nres-1,nres);
9148: /* k1-1 error should be nres-1*/
9149: for (i=1; i<= nlstate ; i ++) {
9150: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
9151: else fprintf(ficgp," %%*lf (%%*lf)");
9152: }
9153: fprintf(ficgp,"\" t\"Backward (stable) prevalence\" w l lt 6 dt 3,\"%s\" every :::%d::%d u 1:($2==%d ? $3+1.96*$4 : 1/0) \"%%lf %%lf",subdirf2(fileresu,"VBL_"),nres-1,nres-1,nres);
9154: for (i=1; i<= nlstate ; i ++) {
9155: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
9156: else fprintf(ficgp," %%*lf (%%*lf)");
9157: }
9158: fprintf(ficgp,"\" t\"95%% CI\" w l lt 4,\"%s\" every :::%d::%d u 1:($2==%d ? $3-1.96*$4 : 1/0) \"%%lf %%lf",subdirf2(fileresu,"VBL_"),nres-1,nres-1,nres);
9159: for (i=1; i<= nlstate ; i ++) {
9160: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
9161: else fprintf(ficgp," %%*lf (%%*lf)");
9162: }
9163: fprintf(ficgp,"\" t\"\" w l lt 4");
9164: } /* end if backprojcast */
9165: } /* end if prevbcast */
9166: /* fprintf(ficgp,"\nset out ;unset label;\n"); */
9167: fprintf(ficgp,"\nset out ;unset title;\n");
9168: } /* nres */
9169: /* } /\* k1 *\/ */
9170: } /* cpt */
9171:
9172:
9173: /*2 eme*/
9174: /* for (k1=1; k1<= m ; k1 ++){ */
9175: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
9176: k1=TKresult[nres];
9177: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
9178: /* if(m != 1 && TKresult[nres]!= k1) */
9179: /* continue; */
9180: fprintf(ficgp,"\n# 2nd: Total life expectancy with CI: 't' files ");
9181: strcpy(gplotlabel,"(");
9182: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
9183: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
9184: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
9185: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
9186: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
9187: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
9188: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
9189: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
9190: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
9191: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
9192: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
9193: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
9194: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
9195: /* } */
9196: /* /\* for(k=1; k <= ncovds; k++){ *\/ */
9197: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
9198: /* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
9199: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
9200: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
9201: }
9202: strcpy(gplotlabel+strlen(gplotlabel),")");
9203: fprintf(ficgp,"\n#\n");
9204: if(invalidvarcomb[k1]){
9205: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
9206: continue;
9207: }
9208:
9209: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"E_"),k1,nres);
9210: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
9211: fprintf(ficgp,"\nset label \"popbased %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",vpopbased,gplotlabel);
9212: if(vpopbased==0){
9213: fprintf(ficgp,"set ylabel \"Years\" \nset ter svg size 640, 480\nplot [%.f:%.f] ",ageminpar,fage);
9214: }else
9215: fprintf(ficgp,"\nreplot ");
9216: for (i=1; i<= nlstate+1 ; i ++) {
9217: k=2*i;
9218: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2==%d && $4!=0 ?$4 : 1/0) \"%%lf %%lf %%lf",subdirf2(fileresu,"T_"),nres-1,nres-1, vpopbased);
9219: for (j=1; j<= nlstate+1 ; j ++) {
9220: if (j==i) fprintf(ficgp," %%lf (%%lf)");
9221: else fprintf(ficgp," %%*lf (%%*lf)");
9222: }
9223: if (i== 1) fprintf(ficgp,"\" t\"TLE\" w l lt %d, \\\n",i);
9224: else fprintf(ficgp,"\" t\"LE in state (%d)\" w l lt %d, \\\n",i-1,i+1);
9225: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2==%d && $4!=0 ? $4-$5*2 : 1/0) \"%%lf %%lf %%lf",subdirf2(fileresu,"T_"),nres-1,nres-1,vpopbased);
9226: for (j=1; j<= nlstate+1 ; j ++) {
9227: if (j==i) fprintf(ficgp," %%lf (%%lf)");
9228: else fprintf(ficgp," %%*lf (%%*lf)");
9229: }
9230: fprintf(ficgp,"\" t\"\" w l lt 0,");
9231: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2==%d && $4!=0 ? $4+$5*2 : 1/0) \"%%lf %%lf %%lf",subdirf2(fileresu,"T_"),nres-1,nres-1,vpopbased);
9232: for (j=1; j<= nlstate+1 ; j ++) {
9233: if (j==i) fprintf(ficgp," %%lf (%%lf)");
9234: else fprintf(ficgp," %%*lf (%%*lf)");
9235: }
9236: if (i== (nlstate+1)) fprintf(ficgp,"\" t\"\" w l lt 0");
9237: else fprintf(ficgp,"\" t\"\" w l lt 0,\\\n");
9238: } /* state */
9239: } /* vpopbased */
9240: fprintf(ficgp,"\nset out;set out \"%s_%d-%d.svg\"; replot; set out; unset label;\n",subdirf2(optionfilefiname,"E_"),k1,nres); /* Buggy gnuplot */
9241: } /* end nres */
9242: /* } /\* k1 end 2 eme*\/ */
9243:
9244:
9245: /*3eme*/
9246: /* for (k1=1; k1<= m ; k1 ++){ */
9247: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
9248: k1=TKresult[nres];
9249: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
9250: /* if(m != 1 && TKresult[nres]!= k1) */
9251: /* continue; */
9252:
9253: for (cpt=1; cpt<= nlstate ; cpt ++) { /* Fragile no verification of covariate values */
9254: fprintf(ficgp,"\n\n# 3d: Life expectancy with EXP_ files: combination=%d state=%d",k1, cpt);
9255: strcpy(gplotlabel,"(");
9256: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
9257: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
9258: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
9259: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
9260: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
9261: /* lv= codtabm(k1,TnsdVar[Tvaraff[k]]); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
9262: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
9263: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
9264: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
9265: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
9266: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
9267: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
9268: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
9269: /* } */
9270: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
9271: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][resultmodel[nres][k4]]); */
9272: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][resultmodel[nres][k4]]); */
9273: }
9274: strcpy(gplotlabel+strlen(gplotlabel),")");
9275: fprintf(ficgp,"\n#\n");
9276: if(invalidvarcomb[k1]){
9277: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
9278: continue;
9279: }
9280:
9281: /* k=2+nlstate*(2*cpt-2); */
9282: k=2+(nlstate+1)*(cpt-1);
9283: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres);
9284: fprintf(ficgp,"set label \"%s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",gplotlabel);
9285: fprintf(ficgp,"set ter svg size 640, 480\n\
9286: plot [%.f:%.f] \"%s\" every :::%d::%d u 1:%d t \"e%d1\" w l",ageminpar,fage,subdirf2(fileresu,"E_"),nres-1,nres-1,k,cpt);
9287: /*fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d-2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
9288: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
9289: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
9290: fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d+2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
9291: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
9292: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
9293:
9294: */
9295: for (i=1; i< nlstate ; i ++) {
9296: fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d%d\" w l",subdirf2(fileresu,"E_"),nres-1,nres-1,k+i,cpt,i+1);
9297: /* 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);*/
9298:
9299: }
9300: fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d.\" w l",subdirf2(fileresu,"E_"),nres-1,nres-1,k+nlstate,cpt);
9301: }
9302: fprintf(ficgp,"\nunset label;\n");
9303: } /* end nres */
9304: /* } /\* end kl 3eme *\/ */
9305:
9306: /* 4eme */
9307: /* Survival functions (period) from state i in state j by initial state i */
9308: /* for (k1=1; k1<=m; k1++){ /\* For each covariate and each value *\/ */
9309: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
9310: k1=TKresult[nres];
9311: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
9312: /* if(m != 1 && TKresult[nres]!= k1) */
9313: /* continue; */
9314: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state cpt*/
9315: strcpy(gplotlabel,"(");
9316: fprintf(ficgp,"\n#\n#\n# Survival functions in state %d : 'LIJ_' files, cov=%d state=%d", cpt, k1, cpt);
9317: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
9318: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
9319: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
9320: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
9321: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
9322: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
9323: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
9324: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
9325: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
9326: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
9327: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
9328: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
9329: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
9330: /* } */
9331: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
9332: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
9333: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
9334: }
9335: strcpy(gplotlabel+strlen(gplotlabel),")");
9336: fprintf(ficgp,"\n#\n");
9337: if(invalidvarcomb[k1]){
9338: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
9339: continue;
9340: }
9341:
9342: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres);
9343: fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
9344: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
9345: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
9346: k=3;
9347: for (i=1; i<= nlstate ; i ++){
9348: if(i==1){
9349: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
9350: }else{
9351: fprintf(ficgp,", '' ");
9352: }
9353: l=(nlstate+ndeath)*(i-1)+1;
9354: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
9355: for (j=2; j<= nlstate+ndeath ; j ++)
9356: fprintf(ficgp,"+$%d",k+l+j-1);
9357: fprintf(ficgp,")) t \"l(%d,%d)\" w l",i,cpt);
9358: } /* nlstate */
9359: fprintf(ficgp,"\nset out; unset label;\n");
9360: } /* end cpt state*/
9361: } /* end nres */
9362: /* } /\* end covariate k1 *\/ */
9363:
9364: /* 5eme */
9365: /* Survival functions (period) from state i in state j by final state j */
9366: /* for (k1=1; k1<= m ; k1++){ /\* For each covariate combination if any *\/ */
9367: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
9368: k1=TKresult[nres];
9369: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
9370: /* if(m != 1 && TKresult[nres]!= k1) */
9371: /* continue; */
9372: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each inital state */
9373: strcpy(gplotlabel,"(");
9374: 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);
9375: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
9376: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
9377: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
9378: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
9379: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
9380: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
9381: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
9382: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
9383: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
9384: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
9385: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
9386: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
9387: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
9388: /* } */
9389: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
9390: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
9391: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
9392: }
9393: strcpy(gplotlabel+strlen(gplotlabel),")");
9394: fprintf(ficgp,"\n#\n");
9395: if(invalidvarcomb[k1]){
9396: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
9397: continue;
9398: }
9399:
9400: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJT_"),cpt,k1,nres);
9401: fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
9402: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
9403: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
9404: k=3;
9405: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
9406: if(j==1)
9407: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
9408: else
9409: fprintf(ficgp,", '' ");
9410: l=(nlstate+ndeath)*(cpt-1) +j;
9411: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):($%d",k1,k+l);
9412: /* for (i=2; i<= nlstate+ndeath ; i ++) */
9413: /* fprintf(ficgp,"+$%d",k+l+i-1); */
9414: fprintf(ficgp,") t \"l(%d,%d)\" w l",cpt,j);
9415: } /* nlstate */
9416: fprintf(ficgp,", '' ");
9417: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):(",k1);
9418: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
9419: l=(nlstate+ndeath)*(cpt-1) +j;
9420: if(j < nlstate)
9421: fprintf(ficgp,"$%d +",k+l);
9422: else
9423: fprintf(ficgp,"$%d) t\"l(%d,.)\" w l",k+l,cpt);
9424: }
9425: fprintf(ficgp,"\nset out; unset label;\n");
9426: } /* end cpt state*/
9427: /* } /\* end covariate *\/ */
9428: } /* end nres */
9429:
9430: /* 6eme */
9431: /* CV preval stable (period) for each covariate */
9432: /* for (k1=1; k1<= m ; k1 ++) /\* For each covariate combination if any *\/ */
9433: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
9434: k1=TKresult[nres];
9435: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
9436: /* if(m != 1 && TKresult[nres]!= k1) */
9437: /* continue; */
9438: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state of arrival */
9439: strcpy(gplotlabel,"(");
9440: fprintf(ficgp,"\n#\n#\n#CV preval stable (forward): 'pij' files, covariatecombination#=%d state=%d",k1, cpt);
9441: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
9442: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
9443: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
9444: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
9445: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
9446: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
9447: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
9448: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
9449: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
9450: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
9451: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
9452: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
9453: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
9454: /* } */
9455: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
9456: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
9457: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
9458: }
9459: strcpy(gplotlabel+strlen(gplotlabel),")");
9460: fprintf(ficgp,"\n#\n");
9461: if(invalidvarcomb[k1]){
9462: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
9463: continue;
9464: }
9465:
9466: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"P_"),cpt,k1,nres);
9467: fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
9468: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
9469: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
9470: k=3; /* Offset */
9471: for (i=1; i<= nlstate ; i ++){ /* State of origin */
9472: if(i==1)
9473: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
9474: else
9475: fprintf(ficgp,", '' ");
9476: l=(nlstate+ndeath)*(i-1)+1; /* 1, 1+ nlstate+ndeath, 1+2*(nlstate+ndeath) */
9477: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
9478: for (j=2; j<= nlstate ; j ++)
9479: fprintf(ficgp,"+$%d",k+l+j-1);
9480: fprintf(ficgp,")) t \"prev(%d,%d)\" w l",i,cpt);
9481: } /* nlstate */
9482: fprintf(ficgp,"\nset out; unset label;\n");
9483: } /* end cpt state*/
9484: } /* end covariate */
9485:
9486:
9487: /* 7eme */
9488: if(prevbcast == 1){
9489: /* CV backward prevalence for each covariate */
9490: /* for (k1=1; k1<= m ; k1 ++) /\* For each covariate combination if any *\/ */
9491: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
9492: k1=TKresult[nres];
9493: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
9494: /* if(m != 1 && TKresult[nres]!= k1) */
9495: /* continue; */
9496: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life origin state */
9497: strcpy(gplotlabel,"(");
9498: fprintf(ficgp,"\n#\n#\n#CV Backward stable prevalence: 'pijb' files, covariatecombination#=%d state=%d",k1, cpt);
9499: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
9500: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
9501: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
9502: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
9503: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
9504: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
9505: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
9506: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
9507: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
9508: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
9509: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
9510: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
9511: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
9512: /* } */
9513: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
9514: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
9515: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
9516: }
9517: strcpy(gplotlabel+strlen(gplotlabel),")");
9518: fprintf(ficgp,"\n#\n");
9519: if(invalidvarcomb[k1]){
9520: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
9521: continue;
9522: }
9523:
9524: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PB_"),cpt,k1,nres);
9525: fprintf(ficgp,"set label \"Origin alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
9526: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
9527: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
9528: k=3; /* Offset */
9529: for (i=1; i<= nlstate ; i ++){ /* State of arrival */
9530: if(i==1)
9531: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJB_"));
9532: else
9533: fprintf(ficgp,", '' ");
9534: /* l=(nlstate+ndeath)*(i-1)+1; */
9535: l=(nlstate+ndeath)*(cpt-1)+1; /* fixed for i; cpt=1 1, cpt=2 1+ nlstate+ndeath, 1+2*(nlstate+ndeath) */
9536: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l); /\* a vérifier *\/ */
9537: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l+(cpt-1)+i-1); /\* a vérifier *\/ */
9538: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d",k1,k+l+i-1); /* To be verified */
9539: /* for (j=2; j<= nlstate ; j ++) */
9540: /* fprintf(ficgp,"+$%d",k+l+j-1); */
9541: /* /\* fprintf(ficgp,"+$%d",k+l+j-1); *\/ */
9542: fprintf(ficgp,") t \"bprev(%d,%d)\" w l",cpt,i);
9543: } /* nlstate */
9544: fprintf(ficgp,"\nset out; unset label;\n");
9545: } /* end cpt state*/
9546: } /* end covariate */
9547: } /* End if prevbcast */
9548:
9549: /* 8eme */
9550: if(prevfcast==1){
9551: /* Projection from cross-sectional to forward stable (period) prevalence for each covariate */
9552:
9553: /* for (k1=1; k1<= m ; k1 ++) /\* For each covariate combination if any *\/ */
9554: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
9555: k1=TKresult[nres];
9556: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
9557: /* if(m != 1 && TKresult[nres]!= k1) */
9558: /* continue; */
9559: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
9560: strcpy(gplotlabel,"(");
9561: fprintf(ficgp,"\n#\n#\n#Projection of prevalence to forward stable prevalence (period): 'PROJ_' files, covariatecombination#=%d state=%d",k1, cpt);
9562: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
9563: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
9564: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
9565: /* for (k=1; k<=cptcoveff; k++){ /\* For each correspondig covariate value *\/ */
9566: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate value corresponding to k1 combination and kth covariate *\\/ *\/ */
9567: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
9568: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
9569: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
9570: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
9571: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
9572: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
9573: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
9574: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
9575: /* } */
9576: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
9577: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
9578: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
9579: }
9580: strcpy(gplotlabel+strlen(gplotlabel),")");
9581: fprintf(ficgp,"\n#\n");
9582: if(invalidvarcomb[k1]){
9583: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
9584: continue;
9585: }
9586:
9587: fprintf(ficgp,"# hpijx=probability over h years, hp.jx is weighted by observed prev\n ");
9588: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres);
9589: fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
9590: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\
9591: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
9592:
9593: /* for (i=1; i<= nlstate+1 ; i ++){ /\* nlstate +1 p11 p21 p.1 *\/ */
9594: istart=nlstate+1; /* Could be one if by state, but nlstate+1 is w.i projection only */
9595: /*istart=1;*/ /* Could be one if by state, but nlstate+1 is w.i projection only */
9596: for (i=istart; i<= nlstate+1 ; i ++){ /* nlstate +1 p11 p21 p.1 */
9597: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
9598: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
9599: /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
9600: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
9601: if(i==istart){
9602: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"F_"));
9603: }else{
9604: fprintf(ficgp,",\\\n '' ");
9605: }
9606: if(cptcoveff ==0){ /* No covariate */
9607: ioffset=2; /* Age is in 2 */
9608: /*# yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
9609: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
9610: /*# V1 = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
9611: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
9612: fprintf(ficgp," u %d:(", ioffset);
9613: if(i==nlstate+1){
9614: fprintf(ficgp," $%d/(1.-$%d)):1 t 'pw.%d' with line lc variable ", \
9615: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
9616: fprintf(ficgp,",\\\n '' ");
9617: fprintf(ficgp," u %d:(",ioffset);
9618: fprintf(ficgp," (($1-$2) == %d ) ? $%d/(1.-$%d) : 1/0):1 with labels center not ", \
9619: offyear, \
9620: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate );
9621: }else
9622: fprintf(ficgp," $%d/(1.-$%d)) t 'p%d%d' with line ", \
9623: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,i,cpt );
9624: }else{ /* more than 2 covariates */
9625: ioffset=2*cptcoveff+2; /* Age is in 4 or 6 or etc.*/
9626: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
9627: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
9628: iyearc=ioffset-1;
9629: iagec=ioffset;
9630: fprintf(ficgp," u %d:(",ioffset);
9631: kl=0;
9632: strcpy(gplotcondition,"(");
9633: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate writing the chain of conditions *\/ */
9634: /* lv= decodtabm(k1,k,cptcoveff); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
9635: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
9636: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
9637: lv=Tvresult[nres][k];
9638: vlv=TinvDoQresult[nres][Tvresult[nres][k]];
9639: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
9640: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
9641: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
9642: /* vlv= nbcode[Tvaraff[k]][lv]; /\* Value of the modality of Tvaraff[k] *\/ */
9643: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
9644: kl++;
9645: /* sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,Tvaraff[k], kl+1, nbcode[Tvaraff[k]][lv]); */
9646: sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,lv, kl+1, vlv );
9647: kl++;
9648: if(k <cptcovs && cptcovs>1)
9649: sprintf(gplotcondition+strlen(gplotcondition)," && ");
9650: }
9651: strcpy(gplotcondition+strlen(gplotcondition),")");
9652: /* 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 *\/ */
9653: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
9654: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
9655: /* '' 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*/
9656: if(i==nlstate+1){
9657: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0):%d t 'p.%d' with line lc variable", gplotcondition, \
9658: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,iyearc, cpt );
9659: fprintf(ficgp,",\\\n '' ");
9660: fprintf(ficgp," u %d:(",iagec);
9661: fprintf(ficgp,"%s && (($%d-$%d) == %d ) ? $%d/(1.-$%d) : 1/0):%d with labels center not ", gplotcondition, \
9662: iyearc, iagec, offyear, \
9663: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate, iyearc );
9664: /* '' u 6:(($1==1 && $2==0 && $3==2 && $4==0) && (($5-$6) == 1947) ? $10/(1.-$22) : 1/0):5 with labels center boxed not*/
9665: }else{
9666: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \
9667: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset +1+(i-1)+(nlstate+1)*nlstate,i,cpt );
9668: }
9669: } /* end if covariate */
9670: } /* nlstate */
9671: fprintf(ficgp,"\nset out; unset label;\n");
9672: } /* end cpt state*/
9673: } /* end covariate */
9674: } /* End if prevfcast */
9675:
9676: if(prevbcast==1){
9677: /* Back projection from cross-sectional to stable (mixed) for each covariate */
9678:
9679: /* for (k1=1; k1<= m ; k1 ++) /\* For each covariate combination if any *\/ */
9680: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
9681: k1=TKresult[nres];
9682: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
9683: /* if(m != 1 && TKresult[nres]!= k1) */
9684: /* continue; */
9685: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
9686: strcpy(gplotlabel,"(");
9687: fprintf(ficgp,"\n#\n#\n#Back projection of prevalence to stable (mixed) back prevalence: 'BPROJ_' files, covariatecombination#=%d originstate=%d",k1, cpt);
9688: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
9689: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
9690: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
9691: /* for (k=1; k<=cptcoveff; k++){ /\* For each correspondig covariate value *\/ */
9692: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate value corresponding to k1 combination and kth covariate *\\/ *\/ */
9693: /* lv= codtabm(k1,TnsdVar[Tvaraff[k]]); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
9694: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
9695: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
9696: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
9697: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
9698: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
9699: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
9700: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
9701: /* } */
9702: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
9703: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
9704: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
9705: }
9706: strcpy(gplotlabel+strlen(gplotlabel),")");
9707: fprintf(ficgp,"\n#\n");
9708: if(invalidvarcomb[k1]){
9709: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
9710: continue;
9711: }
9712:
9713: fprintf(ficgp,"# hbijx=backprobability over h years, hb.jx is weighted by observed prev at destination state\n ");
9714: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres);
9715: fprintf(ficgp,"set label \"Origin alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
9716: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\
9717: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
9718:
9719: /* for (i=1; i<= nlstate+1 ; i ++){ /\* nlstate +1 p11 p21 p.1 *\/ */
9720: istart=nlstate+1; /* Could be one if by state, but nlstate+1 is w.i projection only */
9721: /*istart=1;*/ /* Could be one if by state, but nlstate+1 is w.i projection only */
9722: for (i=istart; i<= nlstate+1 ; i ++){ /* nlstate +1 p11 p21 p.1 */
9723: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
9724: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
9725: /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
9726: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
9727: if(i==istart){
9728: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"FB_"));
9729: }else{
9730: fprintf(ficgp,",\\\n '' ");
9731: }
9732: /* if(cptcoveff ==0){ /\* No covariate *\/ */
9733: if(cptcovs ==0){ /* No covariate */
9734: ioffset=2; /* Age is in 2 */
9735: /*# yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
9736: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
9737: /*# V1 = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
9738: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
9739: fprintf(ficgp," u %d:(", ioffset);
9740: if(i==nlstate+1){
9741: fprintf(ficgp," $%d/(1.-$%d)):1 t 'bw%d' with line lc variable ", \
9742: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
9743: fprintf(ficgp,",\\\n '' ");
9744: fprintf(ficgp," u %d:(",ioffset);
9745: fprintf(ficgp," (($1-$2) == %d ) ? $%d : 1/0):1 with labels center not ", \
9746: offbyear, \
9747: ioffset+(cpt-1)*(nlstate+1)+1+(i-1) );
9748: }else
9749: fprintf(ficgp," $%d/(1.-$%d)) t 'b%d%d' with line ", \
9750: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt,i );
9751: }else{ /* more than 2 covariates */
9752: ioffset=2*cptcoveff+2; /* Age is in 4 or 6 or etc.*/
9753: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
9754: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
9755: iyearc=ioffset-1;
9756: iagec=ioffset;
9757: fprintf(ficgp," u %d:(",ioffset);
9758: kl=0;
9759: strcpy(gplotcondition,"(");
9760: for (k=1; k<=cptcovs; k++){ /* For each covariate k of the resultline, get corresponding value lv for combination k1 */
9761: if(Dummy[modelresult[nres][k]]==0){ /* To be verified */
9762: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate writing the chain of conditions *\/ */
9763: /* lv= decodtabm(k1,k,cptcoveff); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
9764: /* lv= codtabm(k1,TnsdVar[Tvaraff[k]]); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
9765: lv=Tvresult[nres][k];
9766: vlv=TinvDoQresult[nres][Tvresult[nres][k]];
9767: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
9768: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
9769: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
9770: /* vlv= nbcode[Tvaraff[k]][lv]; /\* Value of the modality of Tvaraff[k] *\/ */
9771: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
9772: kl++;
9773: /* sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,Tvaraff[k], kl+1, nbcode[Tvaraff[k]][lv]); */
9774: sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%lg " ,kl,Tvresult[nres][k], kl+1,TinvDoQresult[nres][Tvresult[nres][k]]);
9775: kl++;
9776: if(k <cptcovs && cptcovs>1)
9777: sprintf(gplotcondition+strlen(gplotcondition)," && ");
9778: }
9779: }
9780: strcpy(gplotcondition+strlen(gplotcondition),")");
9781: /* 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 *\/ */
9782: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
9783: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
9784: /* '' 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*/
9785: if(i==nlstate+1){
9786: fprintf(ficgp,"%s ? $%d : 1/0):%d t 'bw%d' with line lc variable", gplotcondition, \
9787: ioffset+(cpt-1)*(nlstate+1)+1+(i-1),iyearc,cpt );
9788: fprintf(ficgp,",\\\n '' ");
9789: fprintf(ficgp," u %d:(",iagec);
9790: /* fprintf(ficgp,"%s && (($5-$6) == %d ) ? $%d/(1.-$%d) : 1/0):5 with labels center not ", gplotcondition, \ */
9791: fprintf(ficgp,"%s && (($%d-$%d) == %d ) ? $%d : 1/0):%d with labels center not ", gplotcondition, \
9792: iyearc,iagec,offbyear, \
9793: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), iyearc );
9794: /* '' u 6:(($1==1 && $2==0 && $3==2 && $4==0) && (($5-$6) == 1947) ? $10/(1.-$22) : 1/0):5 with labels center boxed not*/
9795: }else{
9796: /* fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \ */
9797: fprintf(ficgp,"%s ? $%d : 1/0) t 'b%d%d' with line ", gplotcondition, \
9798: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), cpt,i );
9799: }
9800: } /* end if covariate */
9801: } /* nlstate */
9802: fprintf(ficgp,"\nset out; unset label;\n");
9803: } /* end cpt state*/
9804: } /* end covariate */
9805: } /* End if prevbcast */
9806:
9807:
9808: /* 9eme writing MLE parameters */
9809: fprintf(ficgp,"\n##############\n#9eme MLE estimated parameters\n#############\n");
9810: for(i=1,jk=1; i <=nlstate; i++){
9811: fprintf(ficgp,"# initial state %d\n",i);
9812: for(k=1; k <=(nlstate+ndeath); k++){
9813: if (k != i) {
9814: fprintf(ficgp,"# current state %d\n",k);
9815: for(j=1; j <=ncovmodel; j++){
9816: fprintf(ficgp,"p%d=%f; ",jk,p[jk]);
9817: jk++;
9818: }
9819: fprintf(ficgp,"\n");
9820: }
9821: }
9822: }
9823: fprintf(ficgp,"##############\n#\n");
9824:
9825: /*goto avoid;*/
9826: /* 10eme Graphics of probabilities or incidences using written MLE parameters */
9827: fprintf(ficgp,"\n##############\n#10eme Graphics of probabilities or incidences\n#############\n");
9828: fprintf(ficgp,"# logi(p12/p11)=a12+b12*age+c12age*age+d12*V1+e12*V1*age\n");
9829: fprintf(ficgp,"# logi(p12/p11)=p1 +p2*age +p3*age*age+ p4*V1+ p5*V1*age\n");
9830: fprintf(ficgp,"# logi(p13/p11)=a13+b13*age+c13age*age+d13*V1+e13*V1*age\n");
9831: fprintf(ficgp,"# logi(p13/p11)=p6 +p7*age +p8*age*age+ p9*V1+ p10*V1*age\n");
9832: fprintf(ficgp,"# p12+p13+p14+p11=1=p11(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
9833: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
9834: fprintf(ficgp,"# p11=1/(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
9835: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
9836: fprintf(ficgp,"# p12=exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)/\n");
9837: fprintf(ficgp,"# (1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
9838: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age))\n");
9839: fprintf(ficgp,"# +exp(a14+b14*age+c14age*age+d14*V1+e14*V1*age)+...)\n");
9840: fprintf(ficgp,"#\n");
9841: for(ng=1; ng<=3;ng++){ /* Number of graphics: first is logit, 2nd is probabilities, third is incidences per year*/
9842: fprintf(ficgp,"#Number of graphics: first is logit, 2nd is probabilities, third is incidences per year\n");
9843: fprintf(ficgp,"#model=1+age+%s \n",model);
9844: fprintf(ficgp,"# Type of graphic ng=%d\n",ng);
9845: /* fprintf(ficgp,"# k1=1 to 2^%d=%d\n",cptcoveff,m);/\* to be checked *\/ */
9846: fprintf(ficgp,"# k1=1 to 2^%d=%d\n",cptcovs,m);/* to be checked */
9847: /* for(k1=1; k1 <=m; k1++) /\* For each combination of covariate *\/ */
9848: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
9849: /* k1=nres; */
9850: k1=TKresult[nres];
9851: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
9852: fprintf(ficgp,"\n\n# Resultline k1=%d ",k1);
9853: strcpy(gplotlabel,"(");
9854: /*sprintf(gplotlabel+strlen(gplotlabel)," Dummy combination %d ",k1);*/
9855: for (k=1; k<=cptcovs; k++){ /**< cptcovs number of SIMPLE covariates in the model V2+V1 =2 (dummy or quantit or time varying) */
9856: /* for each resultline nres, and position k, Tvresult[nres][k] gives the name of the variable and
9857: TinvDoQresult[nres][Tvresult[nres][k]] gives its value double or integer) */
9858: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
9859: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
9860: }
9861: /* if(m != 1 && TKresult[nres]!= k1) */
9862: /* continue; */
9863: /* fprintf(ficgp,"\n\n# Combination of dummy k1=%d which is ",k1); */
9864: /* strcpy(gplotlabel,"("); */
9865: /* /\*sprintf(gplotlabel+strlen(gplotlabel)," Dummy combination %d ",k1);*\/ */
9866: /* for (k=1; k<=cptcoveff; k++){ /\* For each correspondig covariate value *\/ */
9867: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate value corresponding to k1 combination and kth covariate *\\/ *\/ */
9868: /* lv= codtabm(k1,TnsdVar[Tvaraff[k]]); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
9869: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
9870: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
9871: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
9872: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
9873: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
9874: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
9875: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
9876: /* } */
9877: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
9878: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
9879: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
9880: /* } */
9881: strcpy(gplotlabel+strlen(gplotlabel),")");
9882: fprintf(ficgp,"\n#\n");
9883: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" ",subdirf2(optionfilefiname,"PE_"),k1,ng,nres);
9884: fprintf(ficgp,"\nset key outside ");
9885: /* fprintf(ficgp,"\nset label \"%s\" at graph 1.2,0.5 center rotate font \"Helvetica,12\"\n",gplotlabel); */
9886: fprintf(ficgp,"\nset title \"%s\" font \"Helvetica,12\"\n",gplotlabel);
9887: fprintf(ficgp,"\nset ter svg size 640, 480 ");
9888: if (ng==1){
9889: fprintf(ficgp,"\nset ylabel \"Value of the logit of the model\"\n"); /* exp(a12+b12*x) could be nice */
9890: fprintf(ficgp,"\nunset log y");
9891: }else if (ng==2){
9892: fprintf(ficgp,"\nset ylabel \"Probability\"\n");
9893: fprintf(ficgp,"\nset log y");
9894: }else if (ng==3){
9895: fprintf(ficgp,"\nset ylabel \"Quasi-incidence per year\"\n");
9896: fprintf(ficgp,"\nset log y");
9897: }else
9898: fprintf(ficgp,"\nunset title ");
9899: fprintf(ficgp,"\nplot [%.f:%.f] ",ageminpar,agemaxpar);
9900: i=1;
9901: for(k2=1; k2<=nlstate; k2++) {
9902: k3=i;
9903: for(k=1; k<=(nlstate+ndeath); k++) {
9904: if (k != k2){
9905: switch( ng) {
9906: case 1:
9907: if(nagesqr==0)
9908: fprintf(ficgp," p%d+p%d*x",i,i+1);
9909: else /* nagesqr =1 */
9910: fprintf(ficgp," p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
9911: break;
9912: case 2: /* ng=2 */
9913: if(nagesqr==0)
9914: fprintf(ficgp," exp(p%d+p%d*x",i,i+1);
9915: else /* nagesqr =1 */
9916: fprintf(ficgp," exp(p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
9917: break;
9918: case 3:
9919: if(nagesqr==0)
9920: fprintf(ficgp," %f*exp(p%d+p%d*x",YEARM/stepm,i,i+1);
9921: else /* nagesqr =1 */
9922: fprintf(ficgp," %f*exp(p%d+p%d*x+p%d*x*x",YEARM/stepm,i,i+1,i+1+nagesqr);
9923: break;
9924: }
9925: ij=1;/* To be checked else nbcode[0][0] wrong */
9926: ijp=1; /* product no age */
9927: /* for(j=3; j <=ncovmodel-nagesqr; j++) { */
9928: for(j=1; j <=cptcovt; j++) { /* For each covariate of the simplified model */
9929: /* printf("Tage[%d]=%d, j=%d\n", ij, Tage[ij], j); */
9930: switch(Typevar[j]){
9931: case 1:
9932: if(cptcovage >0){ /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, 2 V5 and V1 */
9933: if(j==Tage[ij]) { /* Product by age To be looked at!!*//* Bug valgrind */
9934: if(ij <=cptcovage) { /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, 2 V5 and V1 */
9935: if(DummyV[j]==0){/* Bug valgrind */
9936: fprintf(ficgp,"+p%d*%d*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]);;
9937: }else{ /* quantitative */
9938: fprintf(ficgp,"+p%d*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /* Tqinvresult in decoderesult */
9939: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
9940: }
9941: ij++;
9942: }
9943: }
9944: }
9945: break;
9946: case 2:
9947: if(cptcovprod >0){
9948: if(j==Tprod[ijp]) { /* */
9949: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
9950: if(ijp <=cptcovprod) { /* Product */
9951: if(DummyV[Tvard[ijp][1]]==0){/* Vn is dummy */
9952: if(DummyV[Tvard[ijp][2]]==0){/* Vn and Vm are dummy */
9953: /* fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],nbcode[Tvard[ijp][2]][codtabm(k1,j)]); */
9954: fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]);
9955: }else{ /* Vn is dummy and Vm is quanti */
9956: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],Tqinvresult[nres][Tvard[ijp][2]]); */
9957: fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
9958: }
9959: }else{ /* Vn*Vm Vn is quanti */
9960: if(DummyV[Tvard[ijp][2]]==0){
9961: fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]);
9962: }else{ /* Both quanti */
9963: fprintf(ficgp,"+p%d*%f*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
9964: }
9965: }
9966: ijp++;
9967: }
9968: } /* end Tprod */
9969: }
9970: break;
9971: case 3:
9972: if(cptcovdageprod >0){
9973: /* if(j==Tprod[ijp]) { */ /* not necessary */
9974: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
9975: if(ijp <=cptcovprod) { /* Product Vn*Vm and age*VN*Vm*/
9976: if(DummyV[Tvardk[ijp][1]]==0){/* Vn is dummy */
9977: if(DummyV[Tvardk[ijp][2]]==0){/* Vn and Vm are dummy */
9978: /* fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],nbcode[Tvard[ijp][2]][codtabm(k1,j)]); */
9979: fprintf(ficgp,"+p%d*%d*%d*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]);
9980: }else{ /* Vn is dummy and Vm is quanti */
9981: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],Tqinvresult[nres][Tvard[ijp][2]]); */
9982: fprintf(ficgp,"+p%d*%d*%f*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvardk[ijp][1]],Tqinvresult[nres][Tvardk[ijp][2]]);
9983: }
9984: }else{ /* age* Vn*Vm Vn is quanti HERE */
9985: if(DummyV[Tvard[ijp][2]]==0){
9986: fprintf(ficgp,"+p%d*%d*%f*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvardk[ijp][2]],Tqinvresult[nres][Tvardk[ijp][1]]);
9987: }else{ /* Both quanti */
9988: fprintf(ficgp,"+p%d*%f*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvardk[ijp][1]],Tqinvresult[nres][Tvardk[ijp][2]]);
9989: }
9990: }
9991: ijp++;
9992: }
9993: /* } */ /* end Tprod */
9994: }
9995: break;
9996: case 0:
9997: /* simple covariate */
9998: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,nbcode[Tvar[j]][codtabm(k1,j)]); /\* Valgrind bug nbcode *\/ */
9999: if(Dummy[j]==0){
10000: fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]); /* */
10001: }else{ /* quantitative */
10002: fprintf(ficgp,"+p%d*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /* */
10003: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
10004: }
10005: /* end simple */
10006: break;
10007: default:
10008: break;
10009: } /* end switch */
10010: } /* end j */
10011: }else{ /* k=k2 */
10012: if(ng !=1 ){ /* For logit formula of log p11 is more difficult to get */
10013: fprintf(ficgp," (1.");i=i-ncovmodel;
10014: }else
10015: i=i-ncovmodel;
10016: }
10017:
10018: if(ng != 1){
10019: fprintf(ficgp,")/(1");
10020:
10021: for(cpt=1; cpt <=nlstate; cpt++){
10022: if(nagesqr==0)
10023: fprintf(ficgp,"+exp(p%d+p%d*x",k3+(cpt-1)*ncovmodel,k3+(cpt-1)*ncovmodel+1);
10024: else /* nagesqr =1 */
10025: fprintf(ficgp,"+exp(p%d+p%d*x+p%d*x*x",k3+(cpt-1)*ncovmodel,k3+(cpt-1)*ncovmodel+1,k3+(cpt-1)*ncovmodel+1+nagesqr);
10026:
10027: ij=1;
10028: ijp=1;
10029: /* for(j=3; j <=ncovmodel-nagesqr; j++){ */
10030: for(j=1; j <=cptcovt; j++) { /* For each covariate of the simplified model */
10031: switch(Typevar[j]){
10032: case 1:
10033: if(cptcovage >0){
10034: if(j==Tage[ij]) { /* Bug valgrind */
10035: if(ij <=cptcovage) { /* Bug valgrind */
10036: if(DummyV[j]==0){/* Bug valgrind */
10037: /* fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,j-2)]); */
10038: /* fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j+nagesqr,nbcode[Tvar[j]][codtabm(k1,j)]); */
10039: fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvar[j]]);
10040: /* fprintf(ficgp,"+p%d*%d*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]);; */
10041: /* fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
10042: }else{ /* quantitative */
10043: /* fprintf(ficgp,"+p%d*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /\* Tqinvresult in decoderesult *\/ */
10044: fprintf(ficgp,"+p%d*%f*x",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tqinvresult[nres][Tvar[j]]); /* Tqinvresult in decoderesult */
10045: /* fprintf(ficgp,"+p%d*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /\* Tqinvresult in decoderesult *\/ */
10046: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
10047: }
10048: ij++;
10049: }
10050: }
10051: }
10052: break;
10053: case 2:
10054: if(cptcovprod >0){
10055: if(j==Tprod[ijp]) { /* */
10056: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
10057: if(ijp <=cptcovprod) { /* Product */
10058: if(DummyV[Tvard[ijp][1]]==0){/* Vn is dummy */
10059: if(DummyV[Tvard[ijp][2]]==0){/* Vn and Vm are dummy */
10060: /* fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],nbcode[Tvard[ijp][2]][codtabm(k1,j)]); */
10061: fprintf(ficgp,"+p%d*%d*%d",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]);
10062: /* fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]); */
10063: }else{ /* Vn is dummy and Vm is quanti */
10064: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],Tqinvresult[nres][Tvard[ijp][2]]); */
10065: fprintf(ficgp,"+p%d*%d*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
10066: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]); */
10067: }
10068: }else{ /* Vn*Vm Vn is quanti */
10069: if(DummyV[Tvard[ijp][2]]==0){
10070: fprintf(ficgp,"+p%d*%d*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]);
10071: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]); */
10072: }else{ /* Both quanti */
10073: fprintf(ficgp,"+p%d*%f*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
10074: /* fprintf(ficgp,"+p%d*%f*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]); */
10075: }
10076: }
10077: ijp++;
10078: }
10079: } /* end Tprod */
10080: } /* end if */
10081: break;
10082: case 3:
10083: if(cptcovdageprod >0){
10084: /* if(j==Tprod[ijp]) { /\* *\/ */
10085: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
10086: if(ijp <=cptcovprod) { /* Product */
10087: if(DummyV[Tvardk[ijp][1]]==0){/* Vn is dummy */
10088: if(DummyV[Tvardk[ijp][2]]==0){/* Vn and Vm are dummy */
10089: /* fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],nbcode[Tvard[ijp][2]][codtabm(k1,j)]); */
10090: fprintf(ficgp,"+p%d*%d*%d*x",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvardk[ijp][1]],Tinvresult[nres][Tvardk[ijp][2]]);
10091: /* fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]); */
10092: }else{ /* Vn is dummy and Vm is quanti */
10093: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],Tqinvresult[nres][Tvard[ijp][2]]); */
10094: fprintf(ficgp,"+p%d*%d*%f*x",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvardk[ijp][1]],Tqinvresult[nres][Tvardk[ijp][2]]);
10095: /* fprintf(ficgp,"+p%d*%d*%f*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]); */
10096: }
10097: }else{ /* Vn*Vm Vn is quanti */
10098: if(DummyV[Tvardk[ijp][2]]==0){
10099: fprintf(ficgp,"+p%d*%d*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvardk[ijp][2]],Tqinvresult[nres][Tvardk[ijp][1]]);
10100: /* fprintf(ficgp,"+p%d*%d*%f*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]); */
10101: }else{ /* Both quanti */
10102: fprintf(ficgp,"+p%d*%f*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tqinvresult[nres][Tvardk[ijp][1]],Tqinvresult[nres][Tvardk[ijp][2]]);
10103: /* fprintf(ficgp,"+p%d*%f*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]); */
10104: }
10105: }
10106: ijp++;
10107: }
10108: /* } /\* end Tprod *\/ */
10109: } /* end if */
10110: break;
10111: case 0:
10112: /* simple covariate */
10113: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,nbcode[Tvar[j]][codtabm(k1,j)]); /\* Valgrind bug nbcode *\/ */
10114: if(Dummy[j]==0){
10115: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]); /\* *\/ */
10116: fprintf(ficgp,"+p%d*%d",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvar[j]]); /* */
10117: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]); /\* *\/ */
10118: }else{ /* quantitative */
10119: fprintf(ficgp,"+p%d*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tqinvresult[nres][Tvar[j]]); /* */
10120: /* fprintf(ficgp,"+p%d*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /\* *\/ */
10121: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
10122: }
10123: /* end simple */
10124: /* fprintf(ficgp,"+p%d*%d",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,j-2)]);/\* Valgrind bug nbcode *\/ */
10125: break;
10126: default:
10127: break;
10128: } /* end switch */
10129: }
10130: fprintf(ficgp,")");
10131: }
10132: fprintf(ficgp,")");
10133: if(ng ==2)
10134: fprintf(ficgp," w l lw 2 lt (%d*%d+%d)%%%d+1 dt %d t \"p%d%d\" ", nlstate+ndeath, k2, k, nlstate+ndeath, k2, k2,k);
10135: else /* ng= 3 */
10136: fprintf(ficgp," w l lw 2 lt (%d*%d+%d)%%%d+1 dt %d t \"i%d%d\" ", nlstate+ndeath, k2, k, nlstate+ndeath, k2, k2,k);
10137: }else{ /* end ng <> 1 */
10138: if( k !=k2) /* logit p11 is hard to draw */
10139: fprintf(ficgp," w l lw 2 lt (%d*%d+%d)%%%d+1 dt %d t \"logit(p%d%d)\" ", nlstate+ndeath, k2, k, nlstate+ndeath, k2, k2,k);
10140: }
10141: if ((k+k2)!= (nlstate*2+ndeath) && ng != 1)
10142: fprintf(ficgp,",");
10143: if (ng == 1 && k!=k2 && (k+k2)!= (nlstate*2+ndeath))
10144: fprintf(ficgp,",");
10145: i=i+ncovmodel;
10146: } /* end k */
10147: } /* end k2 */
10148: /* fprintf(ficgp,"\n set out; unset label;set key default;\n"); */
10149: fprintf(ficgp,"\n set out; unset title;set key default;\n");
10150: } /* end resultline */
10151: } /* end ng */
10152: /* avoid: */
10153: fflush(ficgp);
10154: } /* end gnuplot */
10155:
10156:
10157: /*************** Moving average **************/
10158: /* int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav, double bageout, double fageout){ */
10159: int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav){
10160:
10161: int i, cpt, cptcod;
10162: int modcovmax =1;
10163: int mobilavrange, mob;
10164: int iage=0;
10165: int firstA1=0, firstA2=0;
10166:
10167: double sum=0., sumr=0.;
10168: double age;
10169: double *sumnewp, *sumnewm, *sumnewmr;
10170: double *agemingood, *agemaxgood;
10171: double *agemingoodr, *agemaxgoodr;
10172:
10173:
10174: /* modcovmax=2*cptcoveff; Max number of modalities. We suppose */
10175: /* a covariate has 2 modalities, should be equal to ncovcombmax */
10176:
10177: sumnewp = vector(1,ncovcombmax);
10178: sumnewm = vector(1,ncovcombmax);
10179: sumnewmr = vector(1,ncovcombmax);
10180: agemingood = vector(1,ncovcombmax);
10181: agemingoodr = vector(1,ncovcombmax);
10182: agemaxgood = vector(1,ncovcombmax);
10183: agemaxgoodr = vector(1,ncovcombmax);
10184:
10185: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
10186: sumnewm[cptcod]=0.; sumnewmr[cptcod]=0.;
10187: sumnewp[cptcod]=0.;
10188: agemingood[cptcod]=0, agemingoodr[cptcod]=0;
10189: agemaxgood[cptcod]=0, agemaxgoodr[cptcod]=0;
10190: }
10191: if (cptcovn<1) ncovcombmax=1; /* At least 1 pass */
10192:
10193: if(mobilav==-1 || mobilav==1||mobilav ==3 ||mobilav==5 ||mobilav== 7){
10194: if(mobilav==1 || mobilav==-1) mobilavrange=5; /* default */
10195: else mobilavrange=mobilav;
10196: for (age=bage; age<=fage; age++)
10197: for (i=1; i<=nlstate;i++)
10198: for (cptcod=1;cptcod<=ncovcombmax;cptcod++)
10199: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
10200: /* We keep the original values on the extreme ages bage, fage and for
10201: fage+1 and bage-1 we use a 3 terms moving average; for fage+2 bage+2
10202: we use a 5 terms etc. until the borders are no more concerned.
10203: */
10204: for (mob=3;mob <=mobilavrange;mob=mob+2){
10205: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){
10206: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
10207: sumnewm[cptcod]=0.;
10208: for (i=1; i<=nlstate;i++){
10209: mobaverage[(int)age][i][cptcod] =probs[(int)age][i][cptcod];
10210: for (cpt=1;cpt<=(mob-1)/2;cpt++){
10211: mobaverage[(int)age][i][cptcod] +=probs[(int)age-cpt][i][cptcod];
10212: mobaverage[(int)age][i][cptcod] +=probs[(int)age+cpt][i][cptcod];
10213: }
10214: mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/mob;
10215: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
10216: } /* end i */
10217: if(sumnewm[cptcod] >1.e-3) mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/sumnewm[cptcod]; /* Rescaling to sum one */
10218: } /* end cptcod */
10219: }/* end age */
10220: }/* end mob */
10221: }else{
10222: printf("Error internal in movingaverage, mobilav=%d.\n",mobilav);
10223: return -1;
10224: }
10225:
10226: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){ /* for each combination */
10227: /* for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ */
10228: if(invalidvarcomb[cptcod]){
10229: printf("\nCombination (%d) ignored because no cases \n",cptcod);
10230: continue;
10231: }
10232:
10233: for (age=fage-(mob-1)/2; age>=bage+(mob-1)/2; age--){ /*looking for the youngest and oldest good age */
10234: sumnewm[cptcod]=0.;
10235: sumnewmr[cptcod]=0.;
10236: for (i=1; i<=nlstate;i++){
10237: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
10238: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
10239: }
10240: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
10241: agemingoodr[cptcod]=age;
10242: }
10243: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
10244: agemingood[cptcod]=age;
10245: }
10246: } /* age */
10247: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ /*looking for the youngest and oldest good age */
10248: sumnewm[cptcod]=0.;
10249: sumnewmr[cptcod]=0.;
10250: for (i=1; i<=nlstate;i++){
10251: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
10252: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
10253: }
10254: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
10255: agemaxgoodr[cptcod]=age;
10256: }
10257: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
10258: agemaxgood[cptcod]=age;
10259: }
10260: } /* age */
10261: /* Thus we have agemingood and agemaxgood as well as goodr for raw (preobs) */
10262: /* but they will change */
10263: firstA1=0;firstA2=0;
10264: for (age=fage-(mob-1)/2; age>=bage; age--){/* From oldest to youngest, filling up to the youngest */
10265: sumnewm[cptcod]=0.;
10266: sumnewmr[cptcod]=0.;
10267: for (i=1; i<=nlstate;i++){
10268: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
10269: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
10270: }
10271: if(mobilav==-1){ /* Forcing raw ages if good else agemingood */
10272: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
10273: agemaxgoodr[cptcod]=age; /* age min */
10274: for (i=1; i<=nlstate;i++)
10275: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
10276: }else{ /* bad we change the value with the values of good ages */
10277: for (i=1; i<=nlstate;i++){
10278: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgoodr[cptcod]][i][cptcod];
10279: } /* i */
10280: } /* end bad */
10281: }else{
10282: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
10283: agemaxgood[cptcod]=age;
10284: }else{ /* bad we change the value with the values of good ages */
10285: for (i=1; i<=nlstate;i++){
10286: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
10287: } /* i */
10288: } /* end bad */
10289: }/* end else */
10290: sum=0.;sumr=0.;
10291: for (i=1; i<=nlstate;i++){
10292: sum+=mobaverage[(int)age][i][cptcod];
10293: sumr+=probs[(int)age][i][cptcod];
10294: }
10295: if(fabs(sum - 1.) > 1.e-3) { /* bad */
10296: if(!firstA1){
10297: firstA1=1;
10298: printf("Moving average A1: For this combination of covariate cptcod=%d, we can't get a smoothed prevalence which sums to one (%f) at any descending age! age=%d, could you increase bage=%d. Others in log file...\n",cptcod,sumr, (int)age, (int)bage);
10299: }
10300: fprintf(ficlog,"Moving average A1: For this combination of covariate cptcod=%d, we can't get a smoothed prevalence which sums to one (%f) at any descending age! age=%d, could you increase bage=%d\n",cptcod,sumr, (int)age, (int)bage);
10301: } /* end bad */
10302: /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */
10303: if(fabs(sumr - 1.) > 1.e-3) { /* bad */
10304: if(!firstA2){
10305: firstA2=1;
10306: printf("Moving average A2: For this combination of covariate cptcod=%d, the raw prevalence doesn't sums to one (%f) even with smoothed values at young ages! age=%d, could you increase bage=%d. Others in log file...\n",cptcod,sumr, (int)age, (int)bage);
10307: }
10308: fprintf(ficlog,"Moving average A2: For this combination of covariate cptcod=%d, the raw prevalence doesn't sums to one (%f) even with smoothed values at young ages! age=%d, could you increase bage=%d\n",cptcod,sumr, (int)age, (int)bage);
10309: } /* end bad */
10310: }/* age */
10311:
10312: for (age=bage+(mob-1)/2; age<=fage; age++){/* From youngest, finding the oldest wrong */
10313: sumnewm[cptcod]=0.;
10314: sumnewmr[cptcod]=0.;
10315: for (i=1; i<=nlstate;i++){
10316: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
10317: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
10318: }
10319: if(mobilav==-1){ /* Forcing raw ages if good else agemingood */
10320: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good */
10321: agemingoodr[cptcod]=age;
10322: for (i=1; i<=nlstate;i++)
10323: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
10324: }else{ /* bad we change the value with the values of good ages */
10325: for (i=1; i<=nlstate;i++){
10326: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingoodr[cptcod]][i][cptcod];
10327: } /* i */
10328: } /* end bad */
10329: }else{
10330: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
10331: agemingood[cptcod]=age;
10332: }else{ /* bad */
10333: for (i=1; i<=nlstate;i++){
10334: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod];
10335: } /* i */
10336: } /* end bad */
10337: }/* end else */
10338: sum=0.;sumr=0.;
10339: for (i=1; i<=nlstate;i++){
10340: sum+=mobaverage[(int)age][i][cptcod];
10341: sumr+=mobaverage[(int)age][i][cptcod];
10342: }
10343: if(fabs(sum - 1.) > 1.e-3) { /* bad */
10344: printf("Moving average B1: For this combination of covariate cptcod=%d, we can't get a smoothed prevalence which sums to one (%f) at any descending age! age=%d, could you decrease fage=%d?\n",cptcod, sum, (int) age, (int)fage);
10345: } /* end bad */
10346: /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */
10347: if(fabs(sumr - 1.) > 1.e-3) { /* bad */
10348: printf("Moving average B2: For this combination of covariate cptcod=%d, the raw prevalence doesn't sums to one (%f) even with smoothed values at young ages! age=%d, could you increase fage=%d\n",cptcod,sumr, (int)age, (int)fage);
10349: } /* end bad */
10350: }/* age */
10351:
10352:
10353: for (age=bage; age<=fage; age++){
10354: /* printf("%d %d ", cptcod, (int)age); */
10355: sumnewp[cptcod]=0.;
10356: sumnewm[cptcod]=0.;
10357: for (i=1; i<=nlstate;i++){
10358: sumnewp[cptcod]+=probs[(int)age][i][cptcod];
10359: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
10360: /* printf("%.4f %.4f ",probs[(int)age][i][cptcod], mobaverage[(int)age][i][cptcod]); */
10361: }
10362: /* printf("%.4f %.4f \n",sumnewp[cptcod], sumnewm[cptcod]); */
10363: }
10364: /* printf("\n"); */
10365: /* } */
10366:
10367: /* brutal averaging */
10368: /* for (i=1; i<=nlstate;i++){ */
10369: /* for (age=1; age<=bage; age++){ */
10370: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod]; */
10371: /* /\* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); *\/ */
10372: /* } */
10373: /* for (age=fage; age<=AGESUP; age++){ */
10374: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod]; */
10375: /* /\* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); *\/ */
10376: /* } */
10377: /* } /\* end i status *\/ */
10378: /* for (i=nlstate+1; i<=nlstate+ndeath;i++){ */
10379: /* for (age=1; age<=AGESUP; age++){ */
10380: /* /\*printf("i=%d, age=%d, cptcod=%d\n",i, (int)age, cptcod);*\/ */
10381: /* mobaverage[(int)age][i][cptcod]=0.; */
10382: /* } */
10383: /* } */
10384: }/* end cptcod */
10385: free_vector(agemaxgoodr,1, ncovcombmax);
10386: free_vector(agemaxgood,1, ncovcombmax);
10387: free_vector(agemingood,1, ncovcombmax);
10388: free_vector(agemingoodr,1, ncovcombmax);
10389: free_vector(sumnewmr,1, ncovcombmax);
10390: free_vector(sumnewm,1, ncovcombmax);
10391: free_vector(sumnewp,1, ncovcombmax);
10392: return 0;
10393: }/* End movingaverage */
10394:
10395:
10396:
10397: /************** Forecasting ******************/
10398: /* void prevforecast(char fileres[], double dateintmean, double anprojd, double mprojd, double jprojd, double ageminpar, double agemax, double dateprev1, double dateprev2, int mobilav, double ***prev, double bage, double fage, int firstpass, int lastpass, double anprojf, double p[], int cptcoveff)*/
10399: void prevforecast(char fileres[], double dateintmean, double dateprojd, double dateprojf, double ageminpar, double agemax, double dateprev1, double dateprev2, int mobilav, double ***prev, double bage, double fage, int firstpass, int lastpass, double p[], int cptcoveff){
10400: /* dateintemean, mean date of interviews
10401: dateprojd, year, month, day of starting projection
10402: dateprojf date of end of projection;year of end of projection (same day and month as proj1).
10403: agemin, agemax range of age
10404: dateprev1 dateprev2 range of dates during which prevalence is computed
10405: */
10406: /* double anprojd, mprojd, jprojd; */
10407: /* double anprojf, mprojf, jprojf; */
10408: int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1, k4, nres=0;
10409: double agec; /* generic age */
10410: double agelim, ppij, yp,yp1,yp2;
10411: double *popeffectif,*popcount;
10412: double ***p3mat;
10413: /* double ***mobaverage; */
10414: char fileresf[FILENAMELENGTH];
10415:
10416: agelim=AGESUP;
10417: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
10418: in each health status at the date of interview (if between dateprev1 and dateprev2).
10419: We still use firstpass and lastpass as another selection.
10420: */
10421: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ */
10422: /* firstpass, lastpass, stepm, weightopt, model); */
10423:
10424: strcpy(fileresf,"F_");
10425: strcat(fileresf,fileresu);
10426: if((ficresf=fopen(fileresf,"w"))==NULL) {
10427: printf("Problem with forecast resultfile: %s\n", fileresf);
10428: fprintf(ficlog,"Problem with forecast resultfile: %s\n", fileresf);
10429: }
10430: printf("\nComputing forecasting: result on file '%s', please wait... \n", fileresf);
10431: fprintf(ficlog,"\nComputing forecasting: result on file '%s', please wait... \n", fileresf);
10432:
10433: if (cptcoveff==0) ncodemax[cptcoveff]=1;
10434:
10435:
10436: stepsize=(int) (stepm+YEARM-1)/YEARM;
10437: if (stepm<=12) stepsize=1;
10438: if(estepm < stepm){
10439: printf ("Problem %d lower than %d\n",estepm, stepm);
10440: }
10441: else{
10442: hstepm=estepm;
10443: }
10444: if(estepm > stepm){ /* Yes every two year */
10445: stepsize=2;
10446: }
10447: hstepm=hstepm/stepm;
10448:
10449:
10450: /* yp1=modf(dateintmean,&yp);/\* extracts integral of datemean in yp and */
10451: /* fractional in yp1 *\/ */
10452: /* aintmean=yp; */
10453: /* yp2=modf((yp1*12),&yp); */
10454: /* mintmean=yp; */
10455: /* yp1=modf((yp2*30.5),&yp); */
10456: /* jintmean=yp; */
10457: /* if(jintmean==0) jintmean=1; */
10458: /* if(mintmean==0) mintmean=1; */
10459:
10460:
10461: /* date2dmy(dateintmean,&jintmean,&mintmean,&aintmean); */
10462: /* date2dmy(dateprojd,&jprojd, &mprojd, &anprojd); */
10463: /* date2dmy(dateprojf,&jprojf, &mprojf, &anprojf); */
10464: /* i1=pow(2,cptcoveff); */
10465: /* if (cptcovn < 1){i1=1;} */
10466:
10467: fprintf(ficresf,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
10468:
10469: fprintf(ficresf,"#****** Routine prevforecast **\n");
10470:
10471: /* if (h==(int)(YEARM*yearp)){ */
10472: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
10473: k=TKresult[nres];
10474: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
10475: /* for(k=1; k<=i1;k++){ /\* We want to find the combination k corresponding to the values of the dummies given in this resut line (to be cleaned one day) *\/ */
10476: /* if(i1 != 1 && TKresult[nres]!= k) */
10477: /* continue; */
10478: /* if(invalidvarcomb[k]){ */
10479: /* printf("\nCombination (%d) projection ignored because no cases \n",k); */
10480: /* continue; */
10481: /* } */
10482: fprintf(ficresf,"\n#****** hpijx=probability over h years, hp.jx is weighted by observed prev \n#");
10483: for(j=1;j<=cptcovs;j++){
10484: /* for(j=1;j<=cptcoveff;j++) { */
10485: /* /\* fprintf(ficresf," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,Tvaraff[j])]); *\/ */
10486: /* fprintf(ficresf," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
10487: /* } */
10488: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10489: /* fprintf(ficresf," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
10490: /* } */
10491: fprintf(ficresf," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
10492: }
10493:
10494: fprintf(ficresf," yearproj age");
10495: for(j=1; j<=nlstate+ndeath;j++){
10496: for(i=1; i<=nlstate;i++)
10497: fprintf(ficresf," p%d%d",i,j);
10498: fprintf(ficresf," wp.%d",j);
10499: }
10500: for (yearp=0; yearp<=(anprojf-anprojd);yearp +=stepsize) {
10501: fprintf(ficresf,"\n");
10502: fprintf(ficresf,"\n# Forecasting at date %.lf/%.lf/%.lf ",jprojd,mprojd,anprojd+yearp);
10503: /* for (agec=fage; agec>=(ageminpar-1); agec--){ */
10504: for (agec=fage; agec>=(bage); agec--){
10505: nhstepm=(int) rint((agelim-agec)*YEARM/stepm);
10506: nhstepm = nhstepm/hstepm;
10507: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
10508: oldm=oldms;savm=savms;
10509: /* We compute pii at age agec over nhstepm);*/
10510: hpxij(p3mat,nhstepm,agec,hstepm,p,nlstate,stepm,oldm,savm, k,nres);
10511: /* Then we print p3mat for h corresponding to the right agec+h*stepms=yearp */
10512: for (h=0; h<=nhstepm; h++){
10513: if (h*hstepm/YEARM*stepm ==yearp) {
10514: break;
10515: }
10516: }
10517: fprintf(ficresf,"\n");
10518: /* for(j=1;j<=cptcoveff;j++) */
10519: for(j=1;j<=cptcovs;j++)
10520: fprintf(ficresf,"%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
10521: /* fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,Tvaraff[j])]); /\* Tvaraff not correct *\/ */
10522: /* fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); /\* TnsdVar[Tvaraff] correct *\/ */
10523: fprintf(ficresf,"%.f %.f ",anprojd+yearp,agec+h*hstepm/YEARM*stepm);
10524:
10525: for(j=1; j<=nlstate+ndeath;j++) {
10526: ppij=0.;
10527: for(i=1; i<=nlstate;i++) {
10528: if (mobilav>=1)
10529: ppij=ppij+p3mat[i][j][h]*prev[(int)agec][i][k];
10530: else { /* even if mobilav==-1 we use mobaverage, probs may not sums to 1 */
10531: ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][k];
10532: }
10533: fprintf(ficresf," %.3f", p3mat[i][j][h]);
10534: } /* end i */
10535: fprintf(ficresf," %.3f", ppij);
10536: }/* end j */
10537: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
10538: } /* end agec */
10539: /* diffyear=(int) anproj1+yearp-ageminpar-1; */
10540: /*printf("Prevforecast %d+%d-%d=diffyear=%d\n",(int) anproj1, (int)yearp,(int)ageminpar,(int) anproj1-(int)ageminpar);*/
10541: } /* end yearp */
10542: } /* end k */
10543:
10544: fclose(ficresf);
10545: printf("End of Computing forecasting \n");
10546: fprintf(ficlog,"End of Computing forecasting\n");
10547:
10548: }
10549:
10550: /************** Back Forecasting ******************/
10551: /* void prevbackforecast(char fileres[], double ***prevacurrent, 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){ */
10552: void prevbackforecast(char fileres[], double ***prevacurrent, double dateintmean, double dateprojd, double dateprojf, double ageminpar, double agemax, double dateprev1, double dateprev2, int mobilav, double bage, double fage, int firstpass, int lastpass, double p[], int cptcoveff){
10553: /* back1, year, month, day of starting backprojection
10554: agemin, agemax range of age
10555: dateprev1 dateprev2 range of dates during which prevalence is computed
10556: anback2 year of end of backprojection (same day and month as back1).
10557: prevacurrent and prev are prevalences.
10558: */
10559: int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1, k4, nres=0;
10560: double agec; /* generic age */
10561: double agelim, ppij, ppi, yp,yp1,yp2; /* ,jintmean,mintmean,aintmean;*/
10562: double *popeffectif,*popcount;
10563: double ***p3mat;
10564: /* double ***mobaverage; */
10565: char fileresfb[FILENAMELENGTH];
10566:
10567: agelim=AGEINF;
10568: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
10569: in each health status at the date of interview (if between dateprev1 and dateprev2).
10570: We still use firstpass and lastpass as another selection.
10571: */
10572: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ */
10573: /* firstpass, lastpass, stepm, weightopt, model); */
10574:
10575: /*Do we need to compute prevalence again?*/
10576:
10577: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
10578:
10579: strcpy(fileresfb,"FB_");
10580: strcat(fileresfb,fileresu);
10581: if((ficresfb=fopen(fileresfb,"w"))==NULL) {
10582: printf("Problem with back forecast resultfile: %s\n", fileresfb);
10583: fprintf(ficlog,"Problem with back forecast resultfile: %s\n", fileresfb);
10584: }
10585: printf("\nComputing back forecasting: result on file '%s', please wait... \n", fileresfb);
10586: fprintf(ficlog,"\nComputing back forecasting: result on file '%s', please wait... \n", fileresfb);
10587:
10588: if (cptcoveff==0) ncodemax[cptcoveff]=1;
10589:
10590:
10591: stepsize=(int) (stepm+YEARM-1)/YEARM;
10592: if (stepm<=12) stepsize=1;
10593: if(estepm < stepm){
10594: printf ("Problem %d lower than %d\n",estepm, stepm);
10595: }
10596: else{
10597: hstepm=estepm;
10598: }
10599: if(estepm >= stepm){ /* Yes every two year */
10600: stepsize=2;
10601: }
10602:
10603: hstepm=hstepm/stepm;
10604: /* yp1=modf(dateintmean,&yp);/\* extracts integral of datemean in yp and */
10605: /* fractional in yp1 *\/ */
10606: /* aintmean=yp; */
10607: /* yp2=modf((yp1*12),&yp); */
10608: /* mintmean=yp; */
10609: /* yp1=modf((yp2*30.5),&yp); */
10610: /* jintmean=yp; */
10611: /* if(jintmean==0) jintmean=1; */
10612: /* if(mintmean==0) jintmean=1; */
10613:
10614: /* i1=pow(2,cptcoveff); */
10615: /* if (cptcovn < 1){i1=1;} */
10616:
10617: fprintf(ficresfb,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
10618: printf("# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
10619:
10620: fprintf(ficresfb,"#****** Routine prevbackforecast **\n");
10621:
10622: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
10623: k=TKresult[nres];
10624: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
10625: /* for(k=1; k<=i1;k++){ */
10626: /* if(i1 != 1 && TKresult[nres]!= k) */
10627: /* continue; */
10628: /* if(invalidvarcomb[k]){ */
10629: /* printf("\nCombination (%d) projection ignored because no cases \n",k); */
10630: /* continue; */
10631: /* } */
10632: fprintf(ficresfb,"\n#****** hbijx=probability over h years, hb.jx is weighted by observed prev \n#");
10633: for(j=1;j<=cptcovs;j++){
10634: /* for(j=1;j<=cptcoveff;j++) { */
10635: /* fprintf(ficresfb," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
10636: /* } */
10637: fprintf(ficresfb," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
10638: }
10639: /* fprintf(ficrespij,"******\n"); */
10640: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10641: /* fprintf(ficresfb," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
10642: /* } */
10643: fprintf(ficresfb," yearbproj age");
10644: for(j=1; j<=nlstate+ndeath;j++){
10645: for(i=1; i<=nlstate;i++)
10646: fprintf(ficresfb," b%d%d",i,j);
10647: fprintf(ficresfb," b.%d",j);
10648: }
10649: for (yearp=0; yearp>=(anbackf-anbackd);yearp -=stepsize) {
10650: /* for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) { */
10651: fprintf(ficresfb,"\n");
10652: fprintf(ficresfb,"\n# Back Forecasting at date %.lf/%.lf/%.lf ",jbackd,mbackd,anbackd+yearp);
10653: /* printf("\n# Back Forecasting at date %.lf/%.lf/%.lf ",jback1,mback1,anback1+yearp); */
10654: /* for (agec=bage; agec<=agemax-1; agec++){ /\* testing *\/ */
10655: for (agec=bage; agec<=fage; agec++){ /* testing */
10656: /* We compute bij at age agec over nhstepm, nhstepm decreases when agec increases because of agemax;*/
10657: nhstepm=(int) (agec-agelim) *YEARM/stepm;/* nhstepm=(int) rint((agec-agelim)*YEARM/stepm);*/
10658: nhstepm = nhstepm/hstepm;
10659: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
10660: oldm=oldms;savm=savms;
10661: /* computes hbxij at age agec over 1 to nhstepm */
10662: /* printf("####prevbackforecast debug agec=%.2f nhstepm=%d\n",agec, nhstepm);fflush(stdout); */
10663: hbxij(p3mat,nhstepm,agec,hstepm,p,prevacurrent,nlstate,stepm, k, nres);
10664: /* hpxij(p3mat,nhstepm,agec,hstepm,p, nlstate,stepm,oldm,savm, k,nres); */
10665: /* Then we print p3mat for h corresponding to the right agec+h*stepms=yearp */
10666: /* printf(" agec=%.2f\n",agec);fflush(stdout); */
10667: for (h=0; h<=nhstepm; h++){
10668: if (h*hstepm/YEARM*stepm ==-yearp) {
10669: break;
10670: }
10671: }
10672: fprintf(ficresfb,"\n");
10673: /* for(j=1;j<=cptcoveff;j++) */
10674: for(j=1;j<=cptcovs;j++)
10675: fprintf(ficresfb,"%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
10676: /* fprintf(ficresfb,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
10677: fprintf(ficresfb,"%.f %.f ",anbackd+yearp,agec-h*hstepm/YEARM*stepm);
10678: for(i=1; i<=nlstate+ndeath;i++) {
10679: ppij=0.;ppi=0.;
10680: for(j=1; j<=nlstate;j++) {
10681: /* if (mobilav==1) */
10682: ppij=ppij+p3mat[i][j][h]*prevacurrent[(int)agec][j][k];
10683: ppi=ppi+prevacurrent[(int)agec][j][k];
10684: /* ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][j][k]; */
10685: /* ppi=ppi+mobaverage[(int)agec][j][k]; */
10686: /* else { */
10687: /* ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][k]; */
10688: /* } */
10689: fprintf(ficresfb," %.3f", p3mat[i][j][h]);
10690: } /* end j */
10691: if(ppi <0.99){
10692: printf("Error in prevbackforecast, prevalence doesn't sum to 1 for state %d: %3f\n",i, ppi);
10693: fprintf(ficlog,"Error in prevbackforecast, prevalence doesn't sum to 1 for state %d: %3f\n",i, ppi);
10694: }
10695: fprintf(ficresfb," %.3f", ppij);
10696: }/* end j */
10697: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
10698: } /* end agec */
10699: } /* end yearp */
10700: } /* end k */
10701:
10702: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
10703:
10704: fclose(ficresfb);
10705: printf("End of Computing Back forecasting \n");
10706: fprintf(ficlog,"End of Computing Back forecasting\n");
10707:
10708: }
10709:
10710: /* Variance of prevalence limit: varprlim */
10711: void varprlim(char fileresu[], int nresult, double ***prevacurrent, int mobilavproj, double bage, double fage, double **prlim, int *ncvyearp, double ftolpl, double p[], double **matcov, double *delti, int stepm, int cptcoveff){
10712: /*------- Variance of forward period (stable) prevalence------*/
10713:
10714: char fileresvpl[FILENAMELENGTH];
10715: FILE *ficresvpl;
10716: double **oldm, **savm;
10717: double **varpl; /* Variances of prevalence limits by age */
10718: int i1, k, nres, j ;
10719:
10720: strcpy(fileresvpl,"VPL_");
10721: strcat(fileresvpl,fileresu);
10722: if((ficresvpl=fopen(fileresvpl,"w"))==NULL) {
10723: printf("Problem with variance of forward period (stable) prevalence resultfile: %s\n", fileresvpl);
10724: exit(0);
10725: }
10726: printf("Computing Variance-covariance of forward period (stable) prevalence: file '%s' ...", fileresvpl);fflush(stdout);
10727: fprintf(ficlog, "Computing Variance-covariance of forward period (stable) prevalence: file '%s' ...", fileresvpl);fflush(ficlog);
10728:
10729: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
10730: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
10731:
10732: i1=pow(2,cptcoveff);
10733: if (cptcovn < 1){i1=1;}
10734:
10735: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
10736: k=TKresult[nres];
10737: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
10738: /* for(k=1; k<=i1;k++){ /\* We find the combination equivalent to result line values of dummies *\/ */
10739: if(i1 != 1 && TKresult[nres]!= k)
10740: continue;
10741: fprintf(ficresvpl,"\n#****** ");
10742: printf("\n#****** ");
10743: fprintf(ficlog,"\n#****** ");
10744: for(j=1;j<=cptcovs;j++) {
10745: fprintf(ficresvpl,"V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
10746: fprintf(ficlog,"V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
10747: printf("V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
10748: /* fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
10749: /* printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
10750: }
10751: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
10752: /* printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
10753: /* fprintf(ficresvpl," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
10754: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
10755: /* } */
10756: fprintf(ficresvpl,"******\n");
10757: printf("******\n");
10758: fprintf(ficlog,"******\n");
10759:
10760: varpl=matrix(1,nlstate,(int) bage, (int) fage);
10761: oldm=oldms;savm=savms;
10762: varprevlim(fileresvpl, ficresvpl, varpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl, ncvyearp, k, strstart, nres);
10763: free_matrix(varpl,1,nlstate,(int) bage, (int)fage);
10764: /*}*/
10765: }
10766:
10767: fclose(ficresvpl);
10768: printf("done variance-covariance of forward period prevalence\n");fflush(stdout);
10769: fprintf(ficlog,"done variance-covariance of forward period prevalence\n");fflush(ficlog);
10770:
10771: }
10772: /* Variance of back prevalence: varbprlim */
10773: void varbprlim(char fileresu[], int nresult, double ***prevacurrent, int mobilavproj, double bage, double fage, double **bprlim, int *ncvyearp, double ftolpl, double p[], double **matcov, double *delti, int stepm, int cptcoveff){
10774: /*------- Variance of back (stable) prevalence------*/
10775:
10776: char fileresvbl[FILENAMELENGTH];
10777: FILE *ficresvbl;
10778:
10779: double **oldm, **savm;
10780: double **varbpl; /* Variances of back prevalence limits by age */
10781: int i1, k, nres, j ;
10782:
10783: strcpy(fileresvbl,"VBL_");
10784: strcat(fileresvbl,fileresu);
10785: if((ficresvbl=fopen(fileresvbl,"w"))==NULL) {
10786: printf("Problem with variance of back (stable) prevalence resultfile: %s\n", fileresvbl);
10787: exit(0);
10788: }
10789: printf("Computing Variance-covariance of back (stable) prevalence: file '%s' ...", fileresvbl);fflush(stdout);
10790: fprintf(ficlog, "Computing Variance-covariance of back (stable) prevalence: file '%s' ...", fileresvbl);fflush(ficlog);
10791:
10792:
10793: i1=pow(2,cptcoveff);
10794: if (cptcovn < 1){i1=1;}
10795:
10796: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
10797: k=TKresult[nres];
10798: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
10799: /* for(k=1; k<=i1;k++){ */
10800: /* if(i1 != 1 && TKresult[nres]!= k) */
10801: /* continue; */
10802: fprintf(ficresvbl,"\n#****** ");
10803: printf("\n#****** ");
10804: fprintf(ficlog,"\n#****** ");
10805: for (j=1; j<= cptcovs; j++){ /* For each selected (single) quantitative value */
10806: printf(" V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][resultmodel[nres][j]]);
10807: fprintf(ficresvbl," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][resultmodel[nres][j]]);
10808: fprintf(ficlog," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][resultmodel[nres][j]]);
10809: /* for(j=1;j<=cptcoveff;j++) { */
10810: /* fprintf(ficresvbl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
10811: /* fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
10812: /* printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
10813: /* } */
10814: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
10815: /* printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
10816: /* fprintf(ficresvbl," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
10817: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
10818: }
10819: fprintf(ficresvbl,"******\n");
10820: printf("******\n");
10821: fprintf(ficlog,"******\n");
10822:
10823: varbpl=matrix(1,nlstate,(int) bage, (int) fage);
10824: oldm=oldms;savm=savms;
10825:
10826: varbrevlim(fileresvbl, ficresvbl, varbpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, bprlim, ftolpl, mobilavproj, ncvyearp, k, strstart, nres);
10827: free_matrix(varbpl,1,nlstate,(int) bage, (int)fage);
10828: /*}*/
10829: }
10830:
10831: fclose(ficresvbl);
10832: printf("done variance-covariance of back prevalence\n");fflush(stdout);
10833: fprintf(ficlog,"done variance-covariance of back prevalence\n");fflush(ficlog);
10834:
10835: } /* End of varbprlim */
10836:
10837: /************** Forecasting *****not tested NB*************/
10838: /* 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){ */
10839:
10840: /* int cpt, stepsize, hstepm, nhstepm, j,k,c, cptcod, i,h; */
10841: /* int *popage; */
10842: /* double calagedatem, agelim, kk1, kk2; */
10843: /* double *popeffectif,*popcount; */
10844: /* double ***p3mat,***tabpop,***tabpopprev; */
10845: /* /\* double ***mobaverage; *\/ */
10846: /* char filerespop[FILENAMELENGTH]; */
10847:
10848: /* tabpop= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
10849: /* tabpopprev= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
10850: /* agelim=AGESUP; */
10851: /* calagedatem=(anpyram+mpyram/12.+jpyram/365.-dateintmean)*YEARM; */
10852:
10853: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
10854:
10855:
10856: /* strcpy(filerespop,"POP_"); */
10857: /* strcat(filerespop,fileresu); */
10858: /* if((ficrespop=fopen(filerespop,"w"))==NULL) { */
10859: /* printf("Problem with forecast resultfile: %s\n", filerespop); */
10860: /* fprintf(ficlog,"Problem with forecast resultfile: %s\n", filerespop); */
10861: /* } */
10862: /* printf("Computing forecasting: result on file '%s' \n", filerespop); */
10863: /* fprintf(ficlog,"Computing forecasting: result on file '%s' \n", filerespop); */
10864:
10865: /* if (cptcoveff==0) ncodemax[cptcoveff]=1; */
10866:
10867: /* /\* if (mobilav!=0) { *\/ */
10868: /* /\* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
10869: /* /\* if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){ *\/ */
10870: /* /\* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); *\/ */
10871: /* /\* printf(" Error in movingaverage mobilav=%d\n",mobilav); *\/ */
10872: /* /\* } *\/ */
10873: /* /\* } *\/ */
10874:
10875: /* stepsize=(int) (stepm+YEARM-1)/YEARM; */
10876: /* if (stepm<=12) stepsize=1; */
10877:
10878: /* agelim=AGESUP; */
10879:
10880: /* hstepm=1; */
10881: /* hstepm=hstepm/stepm; */
10882:
10883: /* if (popforecast==1) { */
10884: /* if((ficpop=fopen(popfile,"r"))==NULL) { */
10885: /* printf("Problem with population file : %s\n",popfile);exit(0); */
10886: /* fprintf(ficlog,"Problem with population file : %s\n",popfile);exit(0); */
10887: /* } */
10888: /* popage=ivector(0,AGESUP); */
10889: /* popeffectif=vector(0,AGESUP); */
10890: /* popcount=vector(0,AGESUP); */
10891:
10892: /* i=1; */
10893: /* while ((c=fscanf(ficpop,"%d %lf\n",&popage[i],&popcount[i])) != EOF) i=i+1; */
10894:
10895: /* imx=i; */
10896: /* for (i=1; i<imx;i++) popeffectif[popage[i]]=popcount[i]; */
10897: /* } */
10898:
10899: /* for(cptcov=1,k=0;cptcov<=i2;cptcov++){ */
10900: /* for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){ */
10901: /* k=k+1; */
10902: /* fprintf(ficrespop,"\n#******"); */
10903: /* for(j=1;j<=cptcoveff;j++) { */
10904: /* fprintf(ficrespop," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */
10905: /* } */
10906: /* fprintf(ficrespop,"******\n"); */
10907: /* fprintf(ficrespop,"# Age"); */
10908: /* for(j=1; j<=nlstate+ndeath;j++) fprintf(ficrespop," P.%d",j); */
10909: /* if (popforecast==1) fprintf(ficrespop," [Population]"); */
10910:
10911: /* for (cpt=0; cpt<=0;cpt++) { */
10912: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */
10913:
10914: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */
10915: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */
10916: /* nhstepm = nhstepm/hstepm; */
10917:
10918: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
10919: /* oldm=oldms;savm=savms; */
10920: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
10921:
10922: /* for (h=0; h<=nhstepm; h++){ */
10923: /* if (h==(int) (calagedatem+YEARM*cpt)) { */
10924: /* fprintf(ficrespop,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */
10925: /* } */
10926: /* for(j=1; j<=nlstate+ndeath;j++) { */
10927: /* kk1=0.;kk2=0; */
10928: /* for(i=1; i<=nlstate;i++) { */
10929: /* if (mobilav==1) */
10930: /* kk1=kk1+p3mat[i][j][h]*mobaverage[(int)agedeb+1][i][cptcod]; */
10931: /* else { */
10932: /* kk1=kk1+p3mat[i][j][h]*probs[(int)(agedeb+1)][i][cptcod]; */
10933: /* } */
10934: /* } */
10935: /* if (h==(int)(calagedatem+12*cpt)){ */
10936: /* tabpop[(int)(agedeb)][j][cptcod]=kk1; */
10937: /* /\*fprintf(ficrespop," %.3f", kk1); */
10938: /* if (popforecast==1) fprintf(ficrespop," [%.f]", kk1*popeffectif[(int)agedeb+1]);*\/ */
10939: /* } */
10940: /* } */
10941: /* for(i=1; i<=nlstate;i++){ */
10942: /* kk1=0.; */
10943: /* for(j=1; j<=nlstate;j++){ */
10944: /* kk1= kk1+tabpop[(int)(agedeb)][j][cptcod]; */
10945: /* } */
10946: /* tabpopprev[(int)(agedeb)][i][cptcod]=tabpop[(int)(agedeb)][i][cptcod]/kk1*popeffectif[(int)(agedeb+(calagedatem+12*cpt)*hstepm/YEARM*stepm-1)]; */
10947: /* } */
10948:
10949: /* if (h==(int)(calagedatem+12*cpt)) */
10950: /* for(j=1; j<=nlstate;j++) */
10951: /* fprintf(ficrespop," %15.2f",tabpopprev[(int)(agedeb+1)][j][cptcod]); */
10952: /* } */
10953: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
10954: /* } */
10955: /* } */
10956:
10957: /* /\******\/ */
10958:
10959: /* for (cpt=1; cpt<=(anpyram1-anpyram);cpt++) { */
10960: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */
10961: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */
10962: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */
10963: /* nhstepm = nhstepm/hstepm; */
10964:
10965: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
10966: /* oldm=oldms;savm=savms; */
10967: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
10968: /* for (h=0; h<=nhstepm; h++){ */
10969: /* if (h==(int) (calagedatem+YEARM*cpt)) { */
10970: /* fprintf(ficresf,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */
10971: /* } */
10972: /* for(j=1; j<=nlstate+ndeath;j++) { */
10973: /* kk1=0.;kk2=0; */
10974: /* for(i=1; i<=nlstate;i++) { */
10975: /* kk1=kk1+p3mat[i][j][h]*tabpopprev[(int)agedeb+1][i][cptcod]; */
10976: /* } */
10977: /* if (h==(int)(calagedatem+12*cpt)) fprintf(ficresf," %15.2f", kk1); */
10978: /* } */
10979: /* } */
10980: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
10981: /* } */
10982: /* } */
10983: /* } */
10984: /* } */
10985:
10986: /* /\* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
10987:
10988: /* if (popforecast==1) { */
10989: /* free_ivector(popage,0,AGESUP); */
10990: /* free_vector(popeffectif,0,AGESUP); */
10991: /* free_vector(popcount,0,AGESUP); */
10992: /* } */
10993: /* free_ma3x(tabpop,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
10994: /* free_ma3x(tabpopprev,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
10995: /* fclose(ficrespop); */
10996: /* } /\* End of popforecast *\/ */
10997:
10998: int fileappend(FILE *fichier, char *optionfich)
10999: {
11000: if((fichier=fopen(optionfich,"a"))==NULL) {
11001: printf("Problem with file: %s\n", optionfich);
11002: fprintf(ficlog,"Problem with file: %s\n", optionfich);
11003: return (0);
11004: }
11005: fflush(fichier);
11006: return (1);
11007: }
11008:
11009:
11010: /**************** function prwizard **********************/
11011: void prwizard(int ncovmodel, int nlstate, int ndeath, char model[], FILE *ficparo)
11012: {
11013:
11014: /* Wizard to print covariance matrix template */
11015:
11016: char ca[32], cb[32];
11017: int i,j, k, li, lj, lk, ll, jj, npar, itimes;
11018: int numlinepar;
11019:
11020: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
11021: fprintf(ficparo,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
11022: for(i=1; i <=nlstate; i++){
11023: jj=0;
11024: for(j=1; j <=nlstate+ndeath; j++){
11025: if(j==i) continue;
11026: jj++;
11027: /*ca[0]= k+'a'-1;ca[1]='\0';*/
11028: printf("%1d%1d",i,j);
11029: fprintf(ficparo,"%1d%1d",i,j);
11030: for(k=1; k<=ncovmodel;k++){
11031: /* printf(" %lf",param[i][j][k]); */
11032: /* fprintf(ficparo," %lf",param[i][j][k]); */
11033: printf(" 0.");
11034: fprintf(ficparo," 0.");
11035: }
11036: printf("\n");
11037: fprintf(ficparo,"\n");
11038: }
11039: }
11040: printf("# Scales (for hessian or gradient estimation)\n");
11041: fprintf(ficparo,"# Scales (for hessian or gradient estimation)\n");
11042: npar= (nlstate+ndeath-1)*nlstate*ncovmodel; /* Number of parameters*/
11043: for(i=1; i <=nlstate; i++){
11044: jj=0;
11045: for(j=1; j <=nlstate+ndeath; j++){
11046: if(j==i) continue;
11047: jj++;
11048: fprintf(ficparo,"%1d%1d",i,j);
11049: printf("%1d%1d",i,j);
11050: fflush(stdout);
11051: for(k=1; k<=ncovmodel;k++){
11052: /* printf(" %le",delti3[i][j][k]); */
11053: /* fprintf(ficparo," %le",delti3[i][j][k]); */
11054: printf(" 0.");
11055: fprintf(ficparo," 0.");
11056: }
11057: numlinepar++;
11058: printf("\n");
11059: fprintf(ficparo,"\n");
11060: }
11061: }
11062: printf("# Covariance matrix\n");
11063: /* # 121 Var(a12)\n\ */
11064: /* # 122 Cov(b12,a12) Var(b12)\n\ */
11065: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
11066: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
11067: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
11068: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
11069: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
11070: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
11071: fflush(stdout);
11072: fprintf(ficparo,"# Covariance matrix\n");
11073: /* # 121 Var(a12)\n\ */
11074: /* # 122 Cov(b12,a12) Var(b12)\n\ */
11075: /* # ...\n\ */
11076: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
11077:
11078: for(itimes=1;itimes<=2;itimes++){
11079: jj=0;
11080: for(i=1; i <=nlstate; i++){
11081: for(j=1; j <=nlstate+ndeath; j++){
11082: if(j==i) continue;
11083: for(k=1; k<=ncovmodel;k++){
11084: jj++;
11085: ca[0]= k+'a'-1;ca[1]='\0';
11086: if(itimes==1){
11087: printf("#%1d%1d%d",i,j,k);
11088: fprintf(ficparo,"#%1d%1d%d",i,j,k);
11089: }else{
11090: printf("%1d%1d%d",i,j,k);
11091: fprintf(ficparo,"%1d%1d%d",i,j,k);
11092: /* printf(" %.5le",matcov[i][j]); */
11093: }
11094: ll=0;
11095: for(li=1;li <=nlstate; li++){
11096: for(lj=1;lj <=nlstate+ndeath; lj++){
11097: if(lj==li) continue;
11098: for(lk=1;lk<=ncovmodel;lk++){
11099: ll++;
11100: if(ll<=jj){
11101: cb[0]= lk +'a'-1;cb[1]='\0';
11102: if(ll<jj){
11103: if(itimes==1){
11104: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
11105: fprintf(ficparo," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
11106: }else{
11107: printf(" 0.");
11108: fprintf(ficparo," 0.");
11109: }
11110: }else{
11111: if(itimes==1){
11112: printf(" Var(%s%1d%1d)",ca,i,j);
11113: fprintf(ficparo," Var(%s%1d%1d)",ca,i,j);
11114: }else{
11115: printf(" 0.");
11116: fprintf(ficparo," 0.");
11117: }
11118: }
11119: }
11120: } /* end lk */
11121: } /* end lj */
11122: } /* end li */
11123: printf("\n");
11124: fprintf(ficparo,"\n");
11125: numlinepar++;
11126: } /* end k*/
11127: } /*end j */
11128: } /* end i */
11129: } /* end itimes */
11130:
11131: } /* end of prwizard */
11132: /******************* Gompertz Likelihood ******************************/
11133: double gompertz(double x[])
11134: {
11135: double A=0.0,B=0.,L=0.0,sump=0.,num=0.;
11136: int i,n=0; /* n is the size of the sample */
11137:
11138: for (i=1;i<=imx ; i++) {
11139: sump=sump+weight[i];
11140: /* sump=sump+1;*/
11141: num=num+1;
11142: }
11143: L=0.0;
11144: /* agegomp=AGEGOMP; */
11145: /* for (i=0; i<=imx; i++)
11146: 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]);*/
11147:
11148: for (i=1;i<=imx ; i++) {
11149: /* mu(a)=mu(agecomp)*exp(teta*(age-agegomp))
11150: mu(a)=x[1]*exp(x[2]*(age-agegomp)); x[1] and x[2] are per year.
11151: * L= Product mu(agedeces)exp(-\int_ageexam^agedc mu(u) du ) for a death between agedc (in month)
11152: * and agedc +1 month, cens[i]=0: log(x[1]/YEARM)
11153: * +
11154: * exp(-\int_ageexam^agecens mu(u) du ) when censored, cens[i]=1
11155: */
11156: if (wav[i] > 1 || agedc[i] < AGESUP) {
11157: if (cens[i] == 1){
11158: A=-x[1]/(x[2])*(exp(x[2]*(agecens[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)));
11159: } else if (cens[i] == 0){
11160: A=-x[1]/(x[2])*(exp(x[2]*(agedc[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)))
11161: +log(x[1]/YEARM) +x[2]*(agedc[i]-agegomp)+log(YEARM);
11162: } else
11163: printf("Gompertz cens[%d] neither 1 nor 0\n",i);
11164: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
11165: L=L+A*weight[i];
11166: /* 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]);*/
11167: }
11168: }
11169:
11170: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
11171:
11172: return -2*L*num/sump;
11173: }
11174:
11175: #ifdef GSL
11176: /******************* Gompertz_f Likelihood ******************************/
11177: double gompertz_f(const gsl_vector *v, void *params)
11178: {
11179: double A=0.,B=0.,LL=0.0,sump=0.,num=0.;
11180: double *x= (double *) v->data;
11181: int i,n=0; /* n is the size of the sample */
11182:
11183: for (i=0;i<=imx-1 ; i++) {
11184: sump=sump+weight[i];
11185: /* sump=sump+1;*/
11186: num=num+1;
11187: }
11188:
11189:
11190: /* for (i=0; i<=imx; i++)
11191: 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]);*/
11192: printf("x[0]=%lf x[1]=%lf\n",x[0],x[1]);
11193: for (i=1;i<=imx ; i++)
11194: {
11195: if (cens[i] == 1 && wav[i]>1)
11196: A=-x[0]/(x[1])*(exp(x[1]*(agecens[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)));
11197:
11198: if (cens[i] == 0 && wav[i]>1)
11199: A=-x[0]/(x[1])*(exp(x[1]*(agedc[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)))
11200: +log(x[0]/YEARM)+x[1]*(agedc[i]-agegomp)+log(YEARM);
11201:
11202: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
11203: if (wav[i] > 1 ) { /* ??? */
11204: LL=LL+A*weight[i];
11205: /* 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]);*/
11206: }
11207: }
11208:
11209: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
11210: printf("x[0]=%lf x[1]=%lf -2*LL*num/sump=%lf\n",x[0],x[1],-2*LL*num/sump);
11211:
11212: return -2*LL*num/sump;
11213: }
11214: #endif
11215:
11216: /******************* Printing html file ***********/
11217: void printinghtmlmort(char fileresu[], char title[], char datafile[], int firstpass, \
11218: int lastpass, int stepm, int weightopt, char model[],\
11219: int imx, double p[],double **matcov,double agemortsup){
11220: int i,k;
11221:
11222: fprintf(fichtm,"<ul><li><h4>Result files </h4>\n Force of mortality. Parameters of the Gompertz fit (with confidence interval in brackets):<br>");
11223: fprintf(fichtm," mu(age) =%lf*exp(%lf*(age-%d)) per year<br><br>",p[1],p[2],agegomp);
11224: for (i=1;i<=2;i++)
11225: 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]));
11226: fprintf(fichtm,"<br><br><img src=\"graphmort.svg\">");
11227: fprintf(fichtm,"</ul>");
11228:
11229: fprintf(fichtm,"<ul><li><h4>Life table</h4>\n <br>");
11230:
11231: 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>");
11232:
11233: for (k=agegomp;k<(agemortsup-2);k++)
11234: 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]);
11235:
11236:
11237: fflush(fichtm);
11238: }
11239:
11240: /******************* Gnuplot file **************/
11241: void printinggnuplotmort(char fileresu[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){
11242:
11243: char dirfileres[132],optfileres[132];
11244:
11245: int ng;
11246:
11247:
11248: /*#ifdef windows */
11249: fprintf(ficgp,"cd \"%s\" \n",pathc);
11250: /*#endif */
11251:
11252:
11253: strcpy(dirfileres,optionfilefiname);
11254: strcpy(optfileres,"vpl");
11255: fprintf(ficgp,"set out \"graphmort.svg\"\n ");
11256: fprintf(ficgp,"set xlabel \"Age\"\n set ylabel \"Force of mortality (per year)\" \n ");
11257: fprintf(ficgp, "set ter svg size 640, 480\n set log y\n");
11258: /* fprintf(ficgp, "set size 0.65,0.65\n"); */
11259: fprintf(ficgp,"plot [%d:100] %lf*exp(%lf*(x-%d))",agegomp,p[1],p[2],agegomp);
11260:
11261: }
11262:
11263: int readdata(char datafile[], int firstobs, int lastobs, int *imax)
11264: {
11265:
11266: /*-------- data file ----------*/
11267: FILE *fic;
11268: char dummy[]=" ";
11269: int i=0, j=0, n=0, iv=0, v;
11270: int lstra;
11271: int linei, month, year,iout;
11272: int noffset=0; /* This is the offset if BOM data file */
11273: char line[MAXLINE], linetmp[MAXLINE];
11274: char stra[MAXLINE], strb[MAXLINE];
11275: char *stratrunc;
11276:
11277: /* DummyV=ivector(-1,NCOVMAX); /\* 1 to 3 *\/ */
11278: /* FixedV=ivector(-1,NCOVMAX); /\* 1 to 3 *\/ */
11279:
11280: ncovcolt=ncovcol+nqv+ntv+nqtv; /* total of covariates in the data, not in the model equation */
11281:
11282: if((fic=fopen(datafile,"r"))==NULL) {
11283: printf("Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(stdout);
11284: fprintf(ficlog,"Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(ficlog);return 1;
11285: }
11286:
11287: /* Is it a BOM UTF-8 Windows file? */
11288: /* First data line */
11289: linei=0;
11290: while(fgets(line, MAXLINE, fic)) {
11291: noffset=0;
11292: if( line[0] == (char)0xEF && line[1] == (char)0xBB) /* EF BB BF */
11293: {
11294: noffset=noffset+3;
11295: printf("# Data file '%s' is an UTF8 BOM file, please convert to UTF8 or ascii file and rerun.\n",datafile);fflush(stdout);
11296: fprintf(ficlog,"# Data file '%s' is an UTF8 BOM file, please convert to UTF8 or ascii file and rerun.\n",datafile);
11297: fflush(ficlog); return 1;
11298: }
11299: /* else if( line[0] == (char)0xFE && line[1] == (char)0xFF)*/
11300: else if( line[0] == (char)0xFF && line[1] == (char)0xFE)
11301: {
11302: noffset=noffset+2;
11303: printf("# Error Data file '%s' is a huge UTF16BE BOM file, please convert to UTF8 or ascii file (for example with dos2unix) and rerun.\n",datafile);fflush(stdout);
11304: fprintf(ficlog,"# Error Data file '%s' is a huge UTF16BE BOM file, please convert to UTF8 or ascii file (for example with dos2unix) and rerun.\n",datafile);
11305: fflush(ficlog); return 1;
11306: }
11307: else if( line[0] == 0 && line[1] == 0)
11308: {
11309: if( line[2] == (char)0xFE && line[3] == (char)0xFF){
11310: noffset=noffset+4;
11311: printf("# Error Data file '%s' is a huge UTF16BE BOM file, please convert to UTF8 or ascii file (for example with dos2unix) and rerun.\n",datafile);fflush(stdout);
11312: fprintf(ficlog,"# Error Data file '%s' is a huge UTF16BE BOM file, please convert to UTF8 or ascii file (for example with dos2unix) and rerun.\n",datafile);
11313: fflush(ficlog); return 1;
11314: }
11315: } else{
11316: ;/*printf(" Not a BOM file\n");*/
11317: }
11318: /* If line starts with a # it is a comment */
11319: if (line[noffset] == '#') {
11320: linei=linei+1;
11321: break;
11322: }else{
11323: break;
11324: }
11325: }
11326: fclose(fic);
11327: if((fic=fopen(datafile,"r"))==NULL) {
11328: printf("Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(stdout);
11329: fprintf(ficlog,"Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(ficlog);return 1;
11330: }
11331: /* Not a Bom file */
11332:
11333: i=1;
11334: while ((fgets(line, MAXLINE, fic) != NULL) &&((i >= firstobs) && (i <=lastobs))) {
11335: linei=linei+1;
11336: for(j=strlen(line); j>=0;j--){ /* Untabifies line */
11337: if(line[j] == '\t')
11338: line[j] = ' ';
11339: }
11340: for(j=strlen(line)-1; (line[j]==' ')||(line[j]==10)||(line[j]==13);j--){
11341: ;
11342: };
11343: line[j+1]=0; /* Trims blanks at end of line */
11344: if(line[0]=='#'){
11345: fprintf(ficlog,"Comment line\n%s\n",line);
11346: printf("Comment line\n%s\n",line);
11347: continue;
11348: }
11349: trimbb(linetmp,line); /* Trims multiple blanks in line */
11350: strcpy(line, linetmp);
11351:
11352: /* Loops on waves */
11353: for (j=maxwav;j>=1;j--){
11354: for (iv=nqtv;iv>=1;iv--){ /* Loop on time varying quantitative variables */
11355: cutv(stra, strb, line, ' ');
11356: if(strb[0]=='.') { /* Missing value */
11357: lval=-1;
11358: cotqvar[j][iv][i]=-1; /* 0.0/0.0 */
11359: cotvar[j][ncovcol+nqv+ntv+iv][i]=-1; /* For performance reasons */
11360: if(isalpha(strb[1])) { /* .m or .d Really Missing value */
11361: 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);
11362: 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);
11363: return 1;
11364: }
11365: }else{
11366: errno=0;
11367: /* what_kind_of_number(strb); */
11368: dval=strtod(strb,&endptr);
11369: /* if( strb[0]=='\0' || (*endptr != '\0')){ */
11370: /* if(strb != endptr && *endptr == '\0') */
11371: /* dval=dlval; */
11372: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
11373: if( strb[0]=='\0' || (*endptr != '\0')){
11374: 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);
11375: 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);
11376: return 1;
11377: }
11378: cotqvar[j][iv][i]=dval;
11379: cotvar[j][ncovcol+nqv+ntv+iv][i]=dval; /* because cotvar starts now at first ntv */
11380: }
11381: strcpy(line,stra);
11382: }/* end loop ntqv */
11383:
11384: for (iv=ntv;iv>=1;iv--){ /* Loop on time varying dummies */
11385: cutv(stra, strb, line, ' ');
11386: if(strb[0]=='.') { /* Missing value */
11387: lval=-1;
11388: }else{
11389: errno=0;
11390: lval=strtol(strb,&endptr,10);
11391: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
11392: if( strb[0]=='\0' || (*endptr != '\0')){
11393: 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);
11394: 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);
11395: return 1;
11396: }
11397: }
11398: if(lval <-1 || lval >1){
11399: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
11400: Should be a value of %d(nth) covariate of wave %d (0 should be the value for the reference and 1\n \
11401: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
11402: For example, for multinomial values like 1, 2 and 3,\n \
11403: build V1=0 V2=0 for the reference value (1),\n \
11404: V1=1 V2=0 for (2) \n \
11405: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
11406: output of IMaCh is often meaningless.\n \
11407: Exiting.\n",lval,linei, i,line,iv,j);
11408: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
11409: Should be a value of %d(nth) covariate of wave %d (0 should be the value for the reference and 1\n \
11410: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
11411: For example, for multinomial values like 1, 2 and 3,\n \
11412: build V1=0 V2=0 for the reference value (1),\n \
11413: V1=1 V2=0 for (2) \n \
11414: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
11415: output of IMaCh is often meaningless.\n \
11416: Exiting.\n",lval,linei, i,line,iv,j);fflush(ficlog);
11417: return 1;
11418: }
11419: cotvar[j][ncovcol+nqv+iv][i]=(double)(lval);
11420: strcpy(line,stra);
11421: }/* end loop ntv */
11422:
11423: /* Statuses at wave */
11424: cutv(stra, strb, line, ' ');
11425: if(strb[0]=='.') { /* Missing value */
11426: lval=-1;
11427: }else{
11428: errno=0;
11429: lval=strtol(strb,&endptr,10);
11430: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
11431: if( strb[0]=='\0' || (*endptr != '\0' )){
11432: 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);
11433: 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);
11434: return 1;
11435: }else if( lval==0 || lval > nlstate+ndeath){
11436: printf("Error in data around '%s' at line number %d for individual %d, '%s'\n Should be a state at wave %d. A state should be 1 to %d and not %ld.\n Fix your data file '%s'! Exiting.\n", strb, linei,i,line,j,nlstate+ndeath, lval, datafile);fflush(stdout);
11437: fprintf(ficlog,"Error in data around '%s' at line number %d for individual %d, '%s'\n Should be a state at wave %d. A state should be 1 to %d and not %ld.\n Fix your data file '%s'! Exiting.\n", strb, linei,i,line,j,nlstate+ndeath, lval, datafile); fflush(ficlog);
11438: return 1;
11439: }
11440: }
11441:
11442: s[j][i]=lval;
11443:
11444: /* Date of Interview */
11445: strcpy(line,stra);
11446: cutv(stra, strb,line,' ');
11447: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
11448: }
11449: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
11450: month=99;
11451: year=9999;
11452: }else{
11453: 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);
11454: 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);
11455: return 1;
11456: }
11457: anint[j][i]= (double) year;
11458: mint[j][i]= (double)month;
11459: /* if( (int)anint[j][i]+ (int)(mint[j][i])/12. < (int) (moisnais[i]/12.+annais[i])){ */
11460: /* printf("Warning reading data around '%s' at line number %d for individual %d, '%s'\nThe date of interview (%2d/%4d) at wave %d occurred before the date of birth (%2d/%4d).\n",strb, linei,i, line, mint[j][i],anint[j][i], moisnais[i],annais[i]); */
11461: /* fprintf(ficlog,"Warning reading data around '%s' at line number %d for individual %d, '%s'\nThe date of interview (%2d/%4d) at wave %d occurred before the date of birth (%2d/%4d).\n",strb, linei,i, line, mint[j][i],anint[j][i], moisnais[i],annais[i]); */
11462: /* } */
11463: strcpy(line,stra);
11464: } /* End loop on waves */
11465:
11466: /* Date of death */
11467: cutv(stra, strb,line,' ');
11468: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
11469: }
11470: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
11471: month=99;
11472: year=9999;
11473: }else{
11474: 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);
11475: 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);
11476: return 1;
11477: }
11478: andc[i]=(double) year;
11479: moisdc[i]=(double) month;
11480: strcpy(line,stra);
11481:
11482: /* Date of birth */
11483: cutv(stra, strb,line,' ');
11484: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
11485: }
11486: else if( (iout=sscanf(strb,"%s.", dummy)) != 0){
11487: month=99;
11488: year=9999;
11489: }else{
11490: 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);
11491: 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);
11492: return 1;
11493: }
11494: if (year==9999) {
11495: 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);
11496: 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);
11497: return 1;
11498:
11499: }
11500: annais[i]=(double)(year);
11501: moisnais[i]=(double)(month);
11502: for (j=1;j<=maxwav;j++){
11503: if( (int)anint[j][i]+ (int)(mint[j][i])/12. < (int) (moisnais[i]/12.+annais[i])){
11504: printf("Warning reading data around '%s' at line number %d for individual %d, '%s'\nThe date of interview (%2d/%4d) at wave %d occurred before the date of birth (%2d/%4d).\n",strb, linei,i, line, (int)mint[j][i],(int)anint[j][i], j,(int)moisnais[i],(int)annais[i]);
11505: fprintf(ficlog,"Warning reading data around '%s' at line number %d for individual %d, '%s'\nThe date of interview (%2d/%4d) at wave %d occurred before the date of birth (%2d/%4d).\n",strb, linei,i, line, (int)mint[j][i],(int)anint[j][i], j, (int)moisnais[i],(int)annais[i]);
11506: }
11507: }
11508:
11509: strcpy(line,stra);
11510:
11511: /* Sample weight */
11512: cutv(stra, strb,line,' ');
11513: errno=0;
11514: dval=strtod(strb,&endptr);
11515: if( strb[0]=='\0' || (*endptr != '\0')){
11516: printf("Error reading data around '%f' at line number %d, \"%s\" for individual %d\nShould be a weight. Exiting.\n",dval, i,line,linei);
11517: 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);
11518: fflush(ficlog);
11519: return 1;
11520: }
11521: weight[i]=dval;
11522: strcpy(line,stra);
11523:
11524: for (iv=nqv;iv>=1;iv--){ /* Loop on fixed quantitative variables */
11525: cutv(stra, strb, line, ' ');
11526: if(strb[0]=='.') { /* Missing value */
11527: lval=-1;
11528: coqvar[iv][i]=NAN;
11529: covar[ncovcol+iv][i]=NAN; /* including qvar in standard covar for performance reasons */
11530: }else{
11531: errno=0;
11532: /* what_kind_of_number(strb); */
11533: dval=strtod(strb,&endptr);
11534: /* if(strb != endptr && *endptr == '\0') */
11535: /* dval=dlval; */
11536: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
11537: if( strb[0]=='\0' || (*endptr != '\0')){
11538: 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);
11539: 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);
11540: return 1;
11541: }
11542: coqvar[iv][i]=dval;
11543: covar[ncovcol+iv][i]=dval; /* including qvar in standard covar for performance reasons */
11544: }
11545: strcpy(line,stra);
11546: }/* end loop nqv */
11547:
11548: /* Covariate values */
11549: for (j=ncovcol;j>=1;j--){
11550: cutv(stra, strb,line,' ');
11551: if(strb[0]=='.') { /* Missing covariate value */
11552: lval=-1;
11553: }else{
11554: errno=0;
11555: lval=strtol(strb,&endptr,10);
11556: if( strb[0]=='\0' || (*endptr != '\0')){
11557: 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);
11558: 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);
11559: return 1;
11560: }
11561: }
11562: if(lval <-1 || lval >1){
11563: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
11564: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
11565: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
11566: For example, for multinomial values like 1, 2 and 3,\n \
11567: build V1=0 V2=0 for the reference value (1),\n \
11568: V1=1 V2=0 for (2) \n \
11569: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
11570: output of IMaCh is often meaningless.\n \
11571: Exiting.\n",lval,linei, i,line,j);
11572: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
11573: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
11574: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
11575: For example, for multinomial values like 1, 2 and 3,\n \
11576: build V1=0 V2=0 for the reference value (1),\n \
11577: V1=1 V2=0 for (2) \n \
11578: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
11579: output of IMaCh is often meaningless.\n \
11580: Exiting.\n",lval,linei, i,line,j);fflush(ficlog);
11581: return 1;
11582: }
11583: covar[j][i]=(double)(lval);
11584: strcpy(line,stra);
11585: }
11586: lstra=strlen(stra);
11587:
11588: if(lstra > 9){ /* More than 2**32 or max of what printf can write with %ld */
11589: stratrunc = &(stra[lstra-9]);
11590: num[i]=atol(stratrunc);
11591: }
11592: else
11593: num[i]=atol(stra);
11594: /*if((s[2][i]==2) && (s[3][i]==-1)&&(s[4][i]==9)){
11595: 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;}*/
11596:
11597: i=i+1;
11598: } /* End loop reading data */
11599:
11600: *imax=i-1; /* Number of individuals */
11601: fclose(fic);
11602:
11603: return (0);
11604: /* endread: */
11605: printf("Exiting readdata: ");
11606: fclose(fic);
11607: return (1);
11608: }
11609:
11610: void removefirstspace(char **stri){/*, char stro[]) {*/
11611: char *p1 = *stri, *p2 = *stri;
11612: while (*p2 == ' ')
11613: p2++;
11614: /* while ((*p1++ = *p2++) !=0) */
11615: /* ; */
11616: /* do */
11617: /* while (*p2 == ' ') */
11618: /* p2++; */
11619: /* while (*p1++ == *p2++); */
11620: *stri=p2;
11621: }
11622:
11623: int decoderesult( char resultline[], int nres)
11624: /**< This routine decode one result line and returns the combination # of dummy covariates only **/
11625: {
11626: int j=0, k=0, k1=0, k2=0, k3=0, k4=0, match=0, k2q=0, k3q=0, k4q=0;
11627: char resultsav[MAXLINE];
11628: /* int resultmodel[MAXLINE]; */
11629: /* int modelresult[MAXLINE]; */
11630: char stra[80], strb[80], strc[80], strd[80],stre[80];
11631:
11632: removefirstspace(&resultline);
11633: printf("decoderesult:%s\n",resultline);
11634:
11635: strcpy(resultsav,resultline);
11636: /* printf("Decoderesult resultsav=\"%s\" resultline=\"%s\"\n", resultsav, resultline); */
11637: if (strlen(resultsav) >1){
11638: j=nbocc(resultsav,'='); /**< j=Number of covariate values'=' in this resultline */
11639: }
11640: if(j == 0 && cptcovs== 0){ /* Resultline but no = and no covariate in the model */
11641: TKresult[nres]=0; /* Combination for the nresult and the model */
11642: return (0);
11643: }
11644: if( j != cptcovs ){ /* Be careful if a variable is in a product but not single */
11645: fprintf(ficlog,"ERROR: the number of variables in the resultline which is %d, differs from the number %d of single variables used in the model line, 1+age+%s.\n",j, cptcovs, model);fflush(ficlog);
11646: printf("ERROR: the number of variables in the resultline which is %d, differs from the number %d of single variables used in the model line, 1+age+%s.\n",j, cptcovs, model);fflush(stdout);
11647: if(j==0)
11648: return 1;
11649: }
11650: for(k=1; k<=j;k++){ /* Loop on any covariate of the RESULT LINE */
11651: if(nbocc(resultsav,'=') >1){
11652: cutl(stra,strb,resultsav,' '); /* keeps in strb after the first ' ' (stra is the rest of the resultline to be analyzed in the next loop *//* resultsav= "V4=1 V5=25.1 V3=0" stra= "V5=25.1 V3=0" strb= "V4=1" */
11653: /* If resultsav= "V4= 1 V5=25.1 V3=0" with a blank then strb="V4=" and stra="1 V5=25.1 V3=0" */
11654: cutl(strc,strd,strb,'='); /* strb:"V4=1" strc="1" strd="V4" */
11655: /* If a blank, then strc="V4=" and strd='\0' */
11656: if(strc[0]=='\0'){
11657: printf("Error in resultline, probably a blank after the \"%s\", \"result:%s\", stra=\"%s\" resultsav=\"%s\"\n",strb,resultline, stra, resultsav);
11658: fprintf(ficlog,"Error in resultline, probably a blank after the \"V%s=\", resultline=%s\n",strb,resultline);
11659: return 1;
11660: }
11661: }else
11662: cutl(strc,strd,resultsav,'=');
11663: Tvalsel[k]=atof(strc); /* 1 */ /* Tvalsel of k is the float value of the kth covariate appearing in this result line */
11664:
11665: cutl(strc,stre,strd,'V'); /* strd='V4' strc=4 stre='V' */;
11666: Tvarsel[k]=atoi(strc); /* 4 */ /* Tvarsel is the id of the kth covariate in the result line Tvarsel[1] in "V4=1.." is 4.*/
11667: /* Typevarsel[k]=1; /\* 1 for age product *\/ */
11668: /* cptcovsel++; */
11669: if (nbocc(stra,'=') >0)
11670: strcpy(resultsav,stra); /* and analyzes it */
11671: }
11672: /* Checking for missing or useless values in comparison of current model needs */
11673: /* Feeds resultmodel[nres][k1]=k2 for k1th product covariate with age in the model equation fed by the index k2 of the resutline*/
11674: for(k1=1; k1<= cptcovt ;k1++){ /* Loop on MODEL LINE V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
11675: if(Typevar[k1]==0){ /* Single covariate in model */
11676: /* 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product */
11677: match=0;
11678: for(k2=1; k2 <=j;k2++){/* Loop on resultline. In result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
11679: if(Tvar[k1]==Tvarsel[k2]) {/* Tvar is coming from the model, Tvarsel from the result. Tvar[1]=5 == Tvarsel[2]=5 */
11680: modelresult[nres][k2]=k1;/* modelresult[2]=1 modelresult[1]=2 modelresult[3]=3 modelresult[6]=4 modelresult[9]=5 */
11681: match=1; /* modelresult of k2 variable of resultline is identical to k1 variable of the model good */
11682: break;
11683: }
11684: }
11685: if(match == 0){
11686: printf("Error in result line (Dummy single): V%d is missing in result: %s according to model=1+age+%s. Tvar[k1=%d]=%d is different from Tvarsel[k2=%d]=%d.\n",Tvar[k1], resultline, model,k1, Tvar[k1], k2, Tvarsel[k2]);
11687: fprintf(ficlog,"Error in result line (Dummy single): V%d is missing in result: %s according to model=1+age+%s\n",Tvar[k1], resultline, model);
11688: return 1;
11689: }
11690: }else if(Typevar[k1]==1){ /* Product with age We want to get the position k2 in the resultline of the product k1 in the model line*/
11691: /* We feed resultmodel[k1]=k2; */
11692: match=0;
11693: for(k2=1; k2 <=j;k2++){/* Loop on resultline. jth occurence of = signs in the result line. In result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
11694: if(Tvar[k1]==Tvarsel[k2]) {/* Tvar is coming from the model, Tvarsel from the result. Tvar[1]=5 == Tvarsel[2]=5 */
11695: modelresult[nres][k2]=k1;/* we found a Vn=1 corrresponding to Vn*age in the model modelresult[2]=1 modelresult[1]=2 modelresult[3]=3 modelresult[6]=4 modelresult[9]=5 */
11696: resultmodel[nres][k1]=k2; /* Added here */
11697: /* printf("Decoderesult first modelresult[k2=%d]=%d (k1) V%d*AGE\n",k2,k1,Tvar[k1]); */
11698: match=1; /* modelresult of k2 variable of resultline is identical to k1 variable of the model good */
11699: break;
11700: }
11701: }
11702: if(match == 0){
11703: printf("Error in result line (Product with age): V%d is missing in result: %s according to model=1+age+%s (Tvarsel[k2=%d]=%d)\n",Tvar[k1], resultline, model, k2, Tvarsel[k2]);
11704: fprintf(ficlog,"Error in result line (Product with age): V%d is missing in result: %s according to model=1+age+%s (Tvarsel[k2=%d]=%d)\n",Tvar[k1], resultline, model, k2, Tvarsel[k2]);
11705: return 1;
11706: }
11707: }else if(Typevar[k1]==2 || Typevar[k1]==3){ /* Product with or without age. We want to get the position in the resultline of the product in the model line*/
11708: /* resultmodel[nres][of such a Vn * Vm product k1] is not unique, so can't exist, we feed Tvard[k1][1] and [2] */
11709: match=0;
11710: /* printf("Decoderesult very first Product Tvardk[k1=%d][1]=%d Tvardk[k1=%d][2]=%d V%d * V%d\n",k1,Tvardk[k1][1],k1,Tvardk[k1][2],Tvardk[k1][1],Tvardk[k1][2]); */
11711: for(k2=1; k2 <=j;k2++){/* Loop on resultline. In result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
11712: if(Tvardk[k1][1]==Tvarsel[k2]) {/* Tvardk is coming from the model, Tvarsel from the result. Tvar[1]=5 == Tvarsel[2]=5 */
11713: /* modelresult[k2]=k1; */
11714: /* printf("Decoderesult first Product modelresult[k2=%d]=%d (k1) V%d * \n",k2,k1,Tvarsel[k2]); */
11715: match=1; /* modelresult of k2 variable of resultline is identical to k1 variable of the model good */
11716: }
11717: }
11718: if(match == 0){
11719: printf("Error in result line (Product without age first variable or double product with age): V%d is missing in result: %s according to model=1+age+%s\n",Tvardk[k1][1], resultline, model);
11720: fprintf(ficlog,"Error in result line (Product without age first variable or double product with age): V%d is missing in result: %s according to model=1+age+%s\n",Tvardk[k1][1], resultline, model);
11721: return 1;
11722: }
11723: match=0;
11724: for(k2=1; k2 <=j;k2++){/* Loop on resultline. In result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
11725: if(Tvardk[k1][2]==Tvarsel[k2]) {/* Tvardk is coming from the model, Tvarsel from the result. Tvar[1]=5 == Tvarsel[2]=5 */
11726: /* modelresult[k2]=k1;*/
11727: /* printf("Decoderesult second Product modelresult[k2=%d]=%d (k1) * V%d \n ",k2,k1,Tvarsel[k2]); */
11728: match=1; /* modelresult of k2 variable of resultline is identical to k1 variable of the model good */
11729: break;
11730: }
11731: }
11732: if(match == 0){
11733: printf("Error in result line (Product without age second variable or double product with age): V%d is missing in result: %s according to model=1+age+%s\n",Tvardk[k1][2], resultline, model);
11734: fprintf(ficlog,"Error in result line (Product without age second variable or double product with age): V%d is missing in result : %s according to model=1+age+%s\n",Tvardk[k1][2], resultline, model);
11735: return 1;
11736: }
11737: }/* End of testing */
11738: }/* End loop cptcovt */
11739: /* Checking for missing or useless values in comparison of current model needs */
11740: /* Feeds resultmodel[nres][k1]=k2 for single covariate (k1) in the model equation */
11741: for(k2=1; k2 <=j;k2++){ /* j or cptcovs is the number of single covariates used either in the model line as well as in the result line (dummy or quantitative)
11742: * Loop on resultline variables: result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
11743: match=0;
11744: for(k1=1; k1<= cptcovt ;k1++){ /* loop on model: model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
11745: if(Typevar[k1]==0){ /* Single only */
11746: if(Tvar[k1]==Tvarsel[k2]) { /* Tvar[2]=4 == Tvarsel[1]=4 What if a product? */
11747: resultmodel[nres][k1]=k2; /* k1th position in the model equation corresponds to k2th position in the result line. resultmodel[2]=1 resultmodel[1]=2 resultmodel[3]=3 resultmodel[6]=4 resultmodel[9]=5 */
11748: modelresult[nres][k2]=k1; /* k1th position in the model equation corresponds to k2th position in the result line. modelresult[1]=2 modelresult[2]=1 modelresult[3]=3 remodelresult[4]=6 modelresult[5]=9 */
11749: ++match;
11750: }
11751: }
11752: }
11753: if(match == 0){
11754: printf("Error in result line: variable V%d is missing in model; result: %s, model=1+age+%s\n",Tvarsel[k2], resultline, model);
11755: fprintf(ficlog,"Error in result line: variable V%d is missing in model; result: %s, model=1+age+%s\n",Tvarsel[k2], resultline, model);
11756: return 1;
11757: }else if(match > 1){
11758: printf("Error in result line: %d doubled; result: %s, model=1+age+%s\n",k2, resultline, model);
11759: fprintf(ficlog,"Error in result line: %d doubled; result: %s, model=1+age+%s\n",k2, resultline, model);
11760: return 1;
11761: }
11762: }
11763: /* cptcovres=j /\* Number of variables in the resultline is equal to cptcovs and thus useless *\/ */
11764: /* We need to deduce which combination number is chosen and save quantitative values */
11765: /* model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
11766: /* nres=1st result line: V4=1 V5=25.1 V3=0 V2=8 V1=1 */
11767: /* should correspond to the combination 6 of dummy: V4=1, V3=0, V1=1 => V4*2**(0) + V3*2**(1) + V1*2**(2) = 1*1 + 0*2 + 1*4 = 5 + (1offset) = 6*/
11768: /* nres=2nd result line: V4=1 V5=24.1 V3=1 V2=8 V1=0 */
11769: /* should give a combination of dummy V4=1, V3=1, V1=0 => V4*2**(0) + V3*2**(1) + V1*2**(2) = 3 + (1offset) = 4*/
11770: /* 1 0 0 0 */
11771: /* 2 1 0 0 */
11772: /* 3 0 1 0 */
11773: /* 4 1 1 0 */ /* V4=1, V3=1, V1=0 (nres=2)*/
11774: /* 5 0 0 1 */
11775: /* 6 1 0 1 */ /* V4=1, V3=0, V1=1 (nres=1)*/
11776: /* 7 0 1 1 */
11777: /* 8 1 1 1 */
11778: /* V(Tvresult)=Tresult V4=1 V3=0 V1=1 Tresult[nres=1][2]=0 */
11779: /* V(Tvqresult)=Tqresult V5=25.1 V2=8 Tqresult[nres=1][1]=25.1 */
11780: /* V5*age V5 known which value for nres? */
11781: /* Tqinvresult[2]=8 Tqinvresult[1]=25.1 */
11782: for(k1=1, k=0, k4=0, k4q=0; k1 <=cptcovt;k1++){ /* cptcovt number of covariates (excluding 1 and age or age*age) in the MODEL equation.
11783: * loop on position k1 in the MODEL LINE */
11784: /* k counting number of combination of single dummies in the equation model */
11785: /* k4 counting single dummies in the equation model */
11786: /* k4q counting single quantitatives in the equation model */
11787: if( Dummy[k1]==0 && Typevar[k1]==0 ){ /* Dummy and Single, fixed or timevarying, k1 is sorting according to MODEL, but k3 to resultline */
11788: /* k4+1= (not always if quant in model) position in the resultline V(Tvarsel)=Tvalsel=Tresult[nres][pos](value); V(Tvresult[nres][pos] (variable): V(variable)=value) */
11789: /* modelresult[k3]=k1: k3th position in the result line corresponds to the k1 position in the model line (doesn't work with products)*/
11790: /* Value in the (current nres) resultline of the variable at the k1th position in the model equation resultmodel[nres][k1]= k3 */
11791: /* resultmodel[nres][k1]=k3: k1th position in the model correspond to the k3 position in the resultline */
11792: /* k3 is the position in the nres result line of the k1th variable of the model equation */
11793: /* Tvarsel[k3]: Name of the variable at the k3th position in the result line. */
11794: /* Tvalsel[k3]: Value of the variable at the k3th position in the result line. */
11795: /* Tresult[nres][result_position]= value of the dummy variable at the result_position in the nres resultline */
11796: /* Tvresult[nres][result_position]= name of the dummy variable at the result_position in the nres resultline */
11797: /* Tinvresult[nres][Name of a dummy variable]= value of the variable in the result line */
11798: /* TinvDoQresult[nres][Name of a Dummy or Q variable]= value of the variable in the result line */
11799: k3= resultmodel[nres][k1]; /* From position k1 in model get position k3 in result line */
11800: /* nres=1 k1=2 resultmodel[2(V4)] = 1=k3 ; k1=3 resultmodel[3(V3)] = 2=k3*/
11801: k2=(int)Tvarsel[k3]; /* from position k3 in resultline get name k2: nres=1 k1=2=>k3=1 Tvarsel[resultmodel[2]]= Tvarsel[1] = 4=k2 (V4); k1=3=>k3=2 Tvarsel[2]=3 (V3)*/
11802: k+=Tvalsel[k3]*pow(2,k4); /* nres=1 k1=2 Tvalsel[1]=1 (V4=1); k1=3 k3=2 Tvalsel[2]=0 (V3=0) */
11803: TinvDoQresult[nres][(int)Tvarsel[k3]]=Tvalsel[k3]; /* TinvDoQresult[nres][Name]=Value; stores the value into the name of the variable. */
11804: /* Tinvresult[nres][4]=1 */
11805: /* Tresult[nres][k4+1]=Tvalsel[k3];/\* Tresult[nres=2][1]=1(V4=1) Tresult[nres=2][2]=0(V3=0) *\/ */
11806: Tresult[nres][k3]=Tvalsel[k3];/* Tresult[nres=2][1]=1(V4=1) Tresult[nres=2][2]=0(V3=0) */
11807: /* Tvresult[nres][k4+1]=(int)Tvarsel[k3];/\* Tvresult[nres][1]=4 Tvresult[nres][3]=1 *\/ */
11808: Tvresult[nres][k3]=(int)Tvarsel[k3];/* Tvresult[nres][1]=4 Tvresult[nres][3]=1 */
11809: Tinvresult[nres][(int)Tvarsel[k3]]=Tvalsel[k3]; /* Tinvresult[nres][4]=1 */
11810: precov[nres][k1]=Tvalsel[k3]; /* Value from resultline of the variable at the k1 position in the model */
11811: /* printf("Decoderesult Dummy k=%d, k1=%d precov[nres=%d][k1=%d]=%.f V(k2=V%d)= Tvalsel[%d]=%d, 2**(%d)\n",k, k1, nres, k1,precov[nres][k1], k2, k3, (int)Tvalsel[k3], k4); */
11812: k4++;;
11813: }else if( Dummy[k1]==1 && Typevar[k1]==0 ){ /* Quantitative and single */
11814: /* Tqresult[nres][result_position]= value of the variable at the result_position in the nres resultline */
11815: /* Tvqresult[nres][result_position]= id of the variable at the result_position in the nres resultline */
11816: /* Tqinvresult[nres][Name of a quantitative variable]= value of the variable in the result line */
11817: k3q= resultmodel[nres][k1]; /* resultmodel[1(V5)] = 5 =k3q */
11818: k2q=(int)Tvarsel[k3q]; /* Name of variable at k3q th position in the resultline */
11819: /* Tvarsel[resultmodel[1]]= Tvarsel[1] = 4=k2 */
11820: /* Tqresult[nres][k4q+1]=Tvalsel[k3q]; /\* Tqresult[nres][1]=25.1 *\/ */
11821: /* Tvresult[nres][k4q+1]=(int)Tvarsel[k3q];/\* Tvresult[nres][1]=4 Tvresult[nres][3]=1 *\/ */
11822: /* Tvqresult[nres][k4q+1]=(int)Tvarsel[k3q]; /\* Tvqresult[nres][1]=5 *\/ */
11823: Tqresult[nres][k3q]=Tvalsel[k3q]; /* Tqresult[nres][1]=25.1 */
11824: Tvresult[nres][k3q]=(int)Tvarsel[k3q];/* Tvresult[nres][1]=4 Tvresult[nres][3]=1 */
11825: Tvqresult[nres][k3q]=(int)Tvarsel[k3q]; /* Tvqresult[nres][1]=5 */
11826: Tqinvresult[nres][(int)Tvarsel[k3q]]=Tvalsel[k3q]; /* Tqinvresult[nres][5]=25.1 */
11827: TinvDoQresult[nres][(int)Tvarsel[k3q]]=Tvalsel[k3q]; /* Tqinvresult[nres][5]=25.1 */
11828: precov[nres][k1]=Tvalsel[k3q];
11829: /* printf("Decoderesult Quantitative nres=%d,precov[nres=%d][k1=%d]=%.f V(k2q=V%d)= Tvalsel[%d]=%d, Tvarsel[%d]=%f\n",nres, nres, k1,precov[nres][k1], k2q, k3q, Tvarsel[k3q], k3q, Tvalsel[k3q]); */
11830: k4q++;;
11831: }else if( Dummy[k1]==2 ){ /* For dummy with age product "V2+V3+V4+V6+V7+V6*V2+V7*V2+V6*V3+V7*V3+V6*V4+V7*V4+age*V2+age*V3+age*V4+age*V6+age*V7+age*V6*V2+age*V6*V3+age*V7*V3+age*V6*V4+age*V7*V4\r"*/
11832: /* Tvar[k1]; */ /* Age variable */ /* 17 age*V6*V2 ?*/
11833: /* Wrong we want the value of variable name Tvar[k1] */
11834: if(Typevar[k1]==2 || Typevar[k1]==3 ){ /* For product quant or dummy (with or without age) */
11835: precov[nres][k1]=TinvDoQresult[nres][Tvardk[k1][1]] * TinvDoQresult[nres][Tvardk[k1][2]];
11836: /* printf("Decoderesult Quantitative or Dummy (not with age) nres=%d k1=%d precov[nres=%d][k1=%d]=%.f V%d(=%.f) * V%d(=%.f) \n",nres, k1, nres, k1,precov[nres][k1], Tvardk[k1][1], TinvDoQresult[nres][Tvardk[k1][1]], Tvardk[k1][2], TinvDoQresult[nres][Tvardk[k1][2]]); */
11837: }else{
11838: k3= resultmodel[nres][k1]; /* nres=1 k1=2 resultmodel[2(V4)] = 1=k3 ; k1=3 resultmodel[3(V3)] = 2=k3*/
11839: k2=(int)Tvarsel[k3]; /* nres=1 k1=2=>k3=1 Tvarsel[resultmodel[2]]= Tvarsel[1] = 4=k2 (V4); k1=3=>k3=2 Tvarsel[2]=3 (V3)*/
11840: TinvDoQresult[nres][(int)Tvarsel[k3]]=Tvalsel[k3]; /* TinvDoQresult[nres][4]=1 */
11841: precov[nres][k1]=Tvalsel[k3];
11842: }
11843: /* printf("Decoderesult Dummy with age k=%d, k1=%d precov[nres=%d][k1=%d]=%.f Tvar[%d]=V%d k2=Tvarsel[%d]=%d Tvalsel[%d]=%d\n",k, k1, nres, k1,precov[nres][k1], k1, Tvar[k1], k3,(int)Tvarsel[k3], k3, (int)Tvalsel[k3]); */
11844: }else if( Dummy[k1]==3 ){ /* For quant with age product */
11845: if(Typevar[k1]==2 || Typevar[k1]==3 ){ /* For product quant or dummy (with or without age) */
11846: precov[nres][k1]=TinvDoQresult[nres][Tvardk[k1][1]] * TinvDoQresult[nres][Tvardk[k1][2]];
11847: /* printf("Decoderesult Quantitative or Dummy (not with age) nres=%d k1=%d precov[nres=%d][k1=%d]=%.f V%d(=%.f) * V%d(=%.f) \n",nres, k1, nres, k1,precov[nres][k1], Tvardk[k1][1], TinvDoQresult[nres][Tvardk[k1][1]], Tvardk[k1][2], TinvDoQresult[nres][Tvardk[k1][2]]); */
11848: }else{
11849: k3q= resultmodel[nres][k1]; /* resultmodel[1(V5)] = 25.1=k3q */
11850: k2q=(int)Tvarsel[k3q]; /* Tvarsel[resultmodel[1]]= Tvarsel[1] = 4=k2 */
11851: TinvDoQresult[nres][(int)Tvarsel[k3q]]=Tvalsel[k3q]; /* TinvDoQresult[nres][5]=25.1 */
11852: precov[nres][k1]=Tvalsel[k3q];
11853: }
11854: /* printf("Decoderesult Quantitative with age nres=%d, k1=%d, precov[nres=%d][k1=%d]=%f Tvar[%d]=V%d V(k2q=%d)= Tvarsel[%d]=%d, Tvalsel[%d]=%f\n",nres, k1, nres, k1,precov[nres][k1], k1, Tvar[k1], k2q, k3q, Tvarsel[k3q], k3q, Tvalsel[k3q]); */
11855: }else if(Typevar[k1]==2 || Typevar[k1]==3 ){ /* For product quant or dummy (with or without age) */
11856: precov[nres][k1]=TinvDoQresult[nres][Tvardk[k1][1]] * TinvDoQresult[nres][Tvardk[k1][2]];
11857: /* printf("Decoderesult Quantitative or Dummy (not with age) nres=%d k1=%d precov[nres=%d][k1=%d]=%.f V%d(=%.f) * V%d(=%.f) \n",nres, k1, nres, k1,precov[nres][k1], Tvardk[k1][1], TinvDoQresult[nres][Tvardk[k1][1]], Tvardk[k1][2], TinvDoQresult[nres][Tvardk[k1][2]]); */
11858: }else{
11859: printf("Error Decoderesult probably a product Dummy[%d]==%d && Typevar[%d]==%d\n", k1, Dummy[k1], k1, Typevar[k1]);
11860: fprintf(ficlog,"Error Decoderesult probably a product Dummy[%d]==%d && Typevar[%d]==%d\n", k1, Dummy[k1], k1, Typevar[k1]);
11861: }
11862: }
11863:
11864: TKresult[nres]=++k; /* Number of combinations of dummies for the nresult and the model =Tvalsel[k3]*pow(2,k4) + 1*/
11865: return (0);
11866: }
11867:
11868: int decodemodel( char model[], int lastobs)
11869: /**< This routine decodes the model and returns:
11870: * Model V1+V2+V3+V8+V7*V8+V5*V6+V8*age+V3*age+age*age
11871: * - nagesqr = 1 if age*age in the model, otherwise 0.
11872: * - cptcovt total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age
11873: * - cptcovn or number of covariates k of the models excluding age*products =6 and age*age
11874: * - cptcovage number of covariates with age*products =2
11875: * - cptcovs number of simple covariates
11876: * ncovcolt=ncovcol+nqv+ntv+nqtv total of covariates in the data, not in the model equation
11877: * - 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
11878: * which is a new column after the 9 (ncovcol+nqv+ntv+nqtv) variables.
11879: * - if k is a product Vn*Vm, covar[k][i] is filled with correct values for each individual
11880: * - Tprod[l] gives the kth covariates of the product Vn*Vm l=1 to cptcovprod-cptcovage
11881: * Tprod[1]@2 {5, 6}: position of first product V7*V8 is 5, and second V5*V6 is 6.
11882: * - Tvard[k] p Tvard[1][1]@4 {7, 8, 5, 6} for V7*V8 and V5*V6 .
11883: */
11884: /* V2+V1+V4+V3*age Tvar[4]=3 ; V1+V2*age Tvar[2]=2; V1+V1*age Tvar[2]=1, Tage[1]=2 */
11885: {
11886: int i, j, k, ks, v;
11887: int n,m;
11888: int j1, k1, k11, k12, k2, k3, k4;
11889: char modelsav[300];
11890: char stra[300], strb[300], strc[300], strd[300],stre[300],strf[300];
11891: char *strpt;
11892: int **existcomb;
11893:
11894: existcomb=imatrix(1,NCOVMAX,1,NCOVMAX);
11895: for(i=1;i<=NCOVMAX;i++)
11896: for(j=1;j<=NCOVMAX;j++)
11897: existcomb[i][j]=0;
11898:
11899: /*removespace(model);*/
11900: if (strlen(model) >1){ /* If there is at least 1 covariate */
11901: j=0, j1=0, k1=0, k12=0, k2=-1, ks=0, cptcovn=0;
11902: if (strstr(model,"AGE") !=0){
11903: printf("Error. AGE must be in lower case 'age' model=1+age+%s. ",model);
11904: fprintf(ficlog,"Error. AGE must be in lower case model=1+age+%s. ",model);fflush(ficlog);
11905: return 1;
11906: }
11907: if (strstr(model,"v") !=0){
11908: printf("Error. 'v' must be in upper case 'V' model=1+age+%s ",model);
11909: fprintf(ficlog,"Error. 'v' must be in upper case model=1+age+%s ",model);fflush(ficlog);
11910: return 1;
11911: }
11912: strcpy(modelsav,model);
11913: if ((strpt=strstr(model,"age*age")) !=0){
11914: printf(" strpt=%s, model=1+age+%s\n",strpt, model);
11915: if(strpt != model){
11916: printf("Error in model: 'model=1+age+%s'; 'age*age' should in first place before other covariates\n \
11917: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
11918: corresponding column of parameters.\n",model);
11919: fprintf(ficlog,"Error in model: 'model=1+age+%s'; 'age*age' should in first place before other covariates\n \
11920: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
11921: corresponding column of parameters.\n",model); fflush(ficlog);
11922: return 1;
11923: }
11924: nagesqr=1;
11925: if (strstr(model,"+age*age") !=0)
11926: substrchaine(modelsav, model, "+age*age");
11927: else if (strstr(model,"age*age+") !=0)
11928: substrchaine(modelsav, model, "age*age+");
11929: else
11930: substrchaine(modelsav, model, "age*age");
11931: }else
11932: nagesqr=0;
11933: if (strlen(modelsav) >1){ /* V2 +V3 +V4 +V6 +V7 +V6*V2 +V7*V2 +V6*V3 +V7*V3 +V6*V4 +V7*V4 +age*V2 +age*V3 +age*V4 +age*V6 +age*V7 +age*V6*V2 +V7*V2 +age*V6*V3 +age*V7*V3 +age*V6*V4 +age*V7*V4 */
11934: j=nbocc(modelsav,'+'); /**< j=Number of '+' */
11935: j1=nbocc(modelsav,'*'); /**< j1=Number of '*' */
11936: cptcovs=0; /**< Number of simple covariates V1 +V1*age +V3 +V3*V4 +age*age => V1 + V3 =4+1-3=2 Wrong */
11937: cptcovt= j+1; /* Number of total covariates in the model, not including
11938: * cst, age and age*age
11939: * V1+V1*age+ V3 + V3*V4+age*age=> 3+1=4*/
11940: /* including age products which are counted in cptcovage.
11941: * but the covariates which are products must be treated
11942: * separately: ncovn=4- 2=2 (V1+V3). */
11943: cptcovprod=0; /**< Number of products V1*V2 +v3*age = 2 */
11944: cptcovdageprod=0; /* Number of doouble products with age age*Vn*VM or Vn*age*Vm or Vn*Vm*age */
11945: cptcovprodnoage=0; /**< Number of covariate products without age: V3*V4 =1 */
11946: cptcovprodage=0;
11947: /* cptcovprodage=nboccstr(modelsav,"age");*/
11948:
11949: /* Design
11950: * V1 V2 V3 V4 V5 V6 V7 V8 V9 Weight
11951: * < ncovcol=8 >
11952: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8
11953: * k= 1 2 3 4 5 6 7 8
11954: * cptcovn number of covariates (not including constant and age ) = # of + plus 1 = 7+1=8
11955: * covar[k,i], are for fixed covariates, value of kth covariate if not including age for individual i:
11956: * covar[1][i]= (V1), covar[4][i]=(V4), covar[8][i]=(V8)
11957: * Tvar[k] # of the kth covariate: Tvar[1]=2 Tvar[2]=1 Tvar[4]=3 Tvar[8]=8
11958: * if multiplied by age: V3*age Tvar[3=V3*age]=3 (V3) Tvar[7]=8 and
11959: * Tage[++cptcovage]=k
11960: * if products, new covar are created after ncovcol + nqv (quanti fixed) with k1
11961: * Tvar[k]=ncovcol+k1; # of the kth covariate product: Tvar[5]=ncovcol+1=10 Tvar[6]=ncovcol+1=11
11962: * Tprod[k1]=k; Tprod[1]=5 Tprod[2]= 6; gives the position of the k1th product
11963: * 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
11964: * Tvar[cptcovn+k2]=Tvard[k1][1];Tvar[cptcovn+k2+1]=Tvard[k1][2];
11965: * Tvar[8+1]=5;Tvar[8+2]=6;Tvar[8+3]=7;Tvar[8+4]=8 inverted
11966: * V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11
11967: * < ncovcol=8 8 fixed covariate. Additional starts at 9 (V5*V6) and 10(V7*V8) >
11968: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8 d1 d1 d2 d2
11969: * k= 1 2 3 4 5 6 7 8 9 10 11 12
11970: * Tvard[k]= 2 1 3 3 10 11 8 8 5 6 7 8
11971: * p Tvar[1]@12={2, 1, 3, 3, 9, 10, 8, 8}
11972: * p Tprod[1]@2={ 6, 5}
11973: *p Tvard[1][1]@4= {7, 8, 5, 6}
11974: * covar[k][i]= V2 V1 ? V3 V5*V6? V7*V8? ? V8
11975: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
11976: *How to reorganize? Tvars(orted)
11977: * Model V1 + V2 + V3 + V8 + V5*V6 + V7*V8 + V3*age + V8*age
11978: * Tvars {2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
11979: * {2, 1, 4, 8, 5, 6, 3, 7}
11980: * Struct []
11981: */
11982:
11983: /* This loop fills the array Tvar from the string 'model'.*/
11984: /* j is the number of + signs in the model V1+V2+V3 j=2 i=3 to 1 */
11985: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4 */
11986: /* k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tage[cptcovage=1]=4 */
11987: /* k=3 V4 Tvar[k=3]= 4 (from V4) */
11988: /* k=2 V1 Tvar[k=2]= 1 (from V1) */
11989: /* k=1 Tvar[1]=2 (from V2) */
11990: /* k=5 Tvar[5] */
11991: /* for (k=1; k<=cptcovn;k++) { */
11992: /* cov[2+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
11993: /* } */
11994: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k])]]*cov[2]; */
11995: /*
11996: * Treating invertedly V2+V1+V3*age+V2*V4 is as if written V2*V4 +V3*age + V1 + V2 */
11997: for(k=cptcovt; k>=1;k--){ /**< Number of covariates not including constant and age, neither age*age*/
11998: Tvar[k]=0; Tprod[k]=0; Tposprod[k]=0;
11999: }
12000: cptcovage=0;
12001:
12002: /* First loop in order to calculate */
12003: /* for age*VN*Vm
12004: * Provides, Typevar[k], Tage[cptcovage], existcomb[n][m], FixedV[ncovcolt+k12]
12005: * Tprod[k1]=k Tposprod[k]=k1; Tvard[k1][1] =m;
12006: */
12007: /* Needs FixedV[Tvardk[k][1]] */
12008: /* For others:
12009: * Sets Typevar[k];
12010: * Tvar[k]=ncovcol+nqv+ntv+nqtv+k11;
12011: * Tposprod[k]=k11;
12012: * Tprod[k11]=k;
12013: * Tvardk[k][1] =m;
12014: * Needs FixedV[Tvardk[k][1]] == 0
12015: */
12016:
12017: for(k=1; k<=cptcovt;k++){ /* Loop on total covariates of the model line */
12018: cutl(stra,strb,modelsav,'+'); /* keeps in strb after the first '+' cutl from left to right
12019: modelsav==V2+V1+V5*age+V4+V3*age strb=V3*age stra=V2+V1V5*age+V4 */ /* <model> "V5+V4+V3+V4*V3+V5*age+V1*age+V1" strb="V5" stra="V4+V3+V4*V3+V5*age+V1*age+V1" */
12020: if (nbocc(modelsav,'+')==0)
12021: strcpy(strb,modelsav); /* and analyzes it */
12022: /* printf("i=%d a=%s b=%s sav=%s\n",i, stra,strb,modelsav);*/
12023: /*scanf("%d",i);*/
12024: if (strchr(strb,'*')) { /**< Model includes a product V2+V1+V5*age+ V4+V3*age strb=V3*age OR double product with age strb=age*V6*V2 or V6*V2*age or V6*age*V2 */
12025: cutl(strc,strd,strb,'*'); /**< k=1 strd*strc Vm*Vn: strb=V3*age(input) strc=age strd=V3 ; V3*V2 strc=V2, strd=V3 OR strb=age*V6*V2 strc=V6*V2 strd=age OR c=V2*age OR c=age*V2 */
12026: if(strchr(strc,'*')) { /**< Model with age and DOUBLE product: allowed since 0.99r44, strc=V6*V2 or V2*age or age*V2, strd=age or V6 or V6 */
12027: Typevar[k]=3; /* 3 for age and double product age*Vn*Vm varying of fixed */
12028: if(strstr(strc,"age")!=0) { /* It means that strc=V2*age or age*V2 and thus that strd=Vn */
12029: cutl(stre,strf,strc,'*') ; /* strf=age or Vm, stre=Vm or age. If strc=V6*V2 then strf=V6 and stre=V2 */
12030: strcpy(strc,strb); /* save strb(=age*Vn*Vm) into strc */
12031: /* We want strb=Vn*Vm */
12032: if(strcmp(strf,"age")==0){ /* strf is "age" so that stre=Vm =V2 . */
12033: strcpy(strb,strd);
12034: strcat(strb,"*");
12035: strcat(strb,stre);
12036: }else{ /* strf=Vm If strf=V6 then stre=V2 */
12037: strcpy(strb,strf);
12038: strcat(strb,"*");
12039: strcat(strb,stre);
12040: strcpy(strd,strb); /* in order for strd to not be "age" for next test (will be Vn*Vm */
12041: }
12042: /* printf("DEBUG FIXED k=%d, Tage[k]=%d, Tvar[Tage[k]=%d,FixedV[Tvar[Tage[k]]]=%d\n",k,Tage[k],Tvar[Tage[k]],FixedV[Tvar[Tage[k]]]); */
12043: /* FixedV[Tvar[Tage[k]]]=0; /\* HERY not sure if V7*V4*age Fixed might not exist yet*\/ */
12044: }else{ /* strc=Vn*Vm (and strd=age) and should be strb=Vn*Vm but want to keep original strb double product */
12045: strcpy(stre,strb); /* save full b in stre */
12046: strcpy(strb,strc); /* save short c in new short b for next block strb=Vn*Vm*/
12047: strcpy(strf,strc); /* save short c in new short f */
12048: cutl(strc,strd,strf,'*'); /* We get strd=Vn and strc=Vm for next block (strb=Vn*Vm)*/
12049: /* strcpy(strc,stre);*/ /* save full e in c for future */
12050: }
12051: cptcovdageprod++; /* double product with age Which product is it? */
12052: /* strcpy(strb,strc); /\* strb was age*V6*V2 or V6*V2*age or V6*age*V2 IS now V6*V2 or V2*age or age*V2 *\/ */
12053: /* cutl(strc,strd,strb,'*'); /\* strd= V6 or V2 or age and strc= V2 or age or V2 *\/ */
12054: cutl(stre,strb,strc,'V'); /* strc= Vn, stre is n; strb=V3*V2 stre=3 strc=*/
12055: n=atoi(stre);
12056: cutl(strc,strb,strd,'V'); /* strd was Vm, strc is m */
12057: m=atoi(strc);
12058: cptcovage++; /* Counts the number of covariates which include age as a product */
12059: Tage[cptcovage]=k; /* For age*V3*V2 gives the position in model of covariates associated with age Tage[1]=6 HERY too*/
12060: if(existcomb[n][m] == 0){
12061: /* r /home/brouard/Documents/Recherches/REVES/Zachary/Zach-2022/Feinuo_Sun/Feinuo-threeway/femV12V15_3wayintNBe.imach */
12062: printf("Warning in model combination V%d*V%d should exist in the model before adding V%d*V%d*age !\n",n,m,n,m);
12063: fprintf(ficlog,"Warning in model combination V%d*V%d should exist in the model before adding V%d*V%d*age !\n",n,m,n,m);
12064: fflush(ficlog);
12065: k1++; /* The combination Vn*Vm will be in the model so we create it at k1 */
12066: k12++;
12067: existcomb[n][m]=k1;
12068: existcomb[m][n]=k1;
12069: Tvar[k]=ncovcol+nqv+ntv+nqtv+k1;
12070: Tprod[k1]=k; /* Tprod[1]=3(=V1*V4) for V2+V1+V1*V4+age*V3+V3*V2+ age*V6*V3 Gives the k position of the k1 double product Vn*Vm or age*Vn*Vm*/
12071: Tposprod[k]=k1; /* Tposprod[3]=1, Tposprod[2]=5 Gives the k1 double product Vn*Vm or age*Vn*Vm at the k position */
12072: Tvard[k1][1] =m; /* m 1 for V1*/
12073: Tvardk[k][1] =m; /* m 1 for V1*/
12074: Tvard[k1][2] =n; /* n 4 for V4*/
12075: Tvardk[k][2] =n; /* n 4 for V4*/
12076: /* Tvar[Tage[cptcovage]]=k1;*/ /* Tvar[6=age*V3*V2]=9 (new fixed covariate) */ /* We don't know about Fixed yet HERE */
12077: if( FixedV[Tvardk[k][1]] == 0 && FixedV[Tvardk[k][2]] == 0){ /* If the product is a fixed covariate then we feed the new column with Vn*Vm */
12078: for (i=1; i<=lastobs;i++){/* For fixed product */
12079: /* Computes the new covariate which is a product of
12080: covar[n][i]* covar[m][i] and stores it at ncovol+k1 May not be defined */
12081: covar[ncovcolt+k1][i]=covar[atoi(stre)][i]*covar[atoi(strc)][i];
12082: }
12083: cptcovprodage++; /* Counting the number of fixed covariate with age */
12084: FixedV[ncovcolt+k12]=0; /* We expand Vn*Vm */
12085: k12++;
12086: FixedV[ncovcolt+k12]=0;
12087: }else{ /*End of FixedV */
12088: cptcovprodvage++; /* Counting the number of varying covariate with age */
12089: FixedV[ncovcolt+k12]=1; /* We expand Vn*Vm */
12090: k12++;
12091: FixedV[ncovcolt+k12]=1;
12092: }
12093: }else{ /* k1 Vn*Vm already exists */
12094: k11=existcomb[n][m];
12095: Tposprod[k]=k11; /* OK */
12096: Tvar[k]=Tvar[Tprod[k11]]; /* HERY */
12097: Tvardk[k][1]=m;
12098: Tvardk[k][2]=n;
12099: if( FixedV[Tvardk[k][1]] == 0 && FixedV[Tvardk[k][2]] == 0){ /* If the product is a fixed covariate then we feed the new column with Vn*Vm */
12100: /*cptcovage++;*/ /* Counts the number of covariates which include age as a product */
12101: cptcovprodage++; /* Counting the number of fixed covariate with age */
12102: /*Tage[cptcovage]=k;*/ /* For age*V3*V2 Tage[1]=V3*V3=9 HERY too*/
12103: Tvar[Tage[cptcovage]]=k1;
12104: FixedV[ncovcolt+k12]=0; /* We expand Vn*Vm */
12105: k12++;
12106: FixedV[ncovcolt+k12]=0;
12107: }else{ /* Already exists but time varying (and age) */
12108: /*cptcovage++;*/ /* Counts the number of covariates which include age as a product */
12109: /*Tage[cptcovage]=k;*/ /* For age*V3*V2 Tage[1]=V3*V3=9 HERY too*/
12110: /* Tvar[Tage[cptcovage]]=k1; */
12111: cptcovprodvage++;
12112: FixedV[ncovcolt+k12]=1; /* We expand Vn*Vm */
12113: k12++;
12114: FixedV[ncovcolt+k12]=1;
12115: }
12116: }
12117: /* Tage[cptcovage]=k; /\* V2+V1+V4+V3*age Tvar[4]=3, Tage[1] = 4 or V1+V1*age Tvar[2]=1, Tage[1]=2 *\/ */
12118: /* Tvar[k]=k11; /\* HERY *\/ */
12119: } else {/* simple product strb=age*Vn so that c=Vn and d=age, or strb=Vn*age so that c=age and d=Vn, or b=Vn*Vm so that c=Vm and d=Vn */
12120: cptcovprod++;
12121: if (strcmp(strc,"age")==0) { /**< Model includes age: strb= Vn*age c=age d=Vn*/
12122: /* covar is not filled and then is empty */
12123: cutl(stre,strb,strd,'V'); /* strd=V3(input): stre="3" */
12124: Tvar[k]=atoi(stre); /* V2+V1+V5*age+V4+V3*age Tvar[5]=3 ; V1+V2*age Tvar[2]=2; V1+V1*age Tvar[2]=1 */
12125: Typevar[k]=1; /* 1 for age product */
12126: cptcovage++; /* Counts the number of covariates which include age as a product */
12127: Tage[cptcovage]=k; /* V2+V1+V4+V3*age Tvar[4]=3, Tage[1] = 4 or V1+V1*age Tvar[2]=1, Tage[1]=2 */
12128: if( FixedV[Tvar[k]] == 0){
12129: cptcovprodage++; /* Counting the number of fixed covariate with age */
12130: }else{
12131: cptcovprodvage++; /* Counting the number of fixedvarying covariate with age */
12132: }
12133: /*printf("stre=%s ", stre);*/
12134: } else if (strcmp(strd,"age")==0) { /* strb= age*Vn c=Vn */
12135: cutl(stre,strb,strc,'V');
12136: Tvar[k]=atoi(stre);
12137: Typevar[k]=1; /* 1 for age product */
12138: cptcovage++;
12139: Tage[cptcovage]=k;
12140: if( FixedV[Tvar[k]] == 0){
12141: cptcovprodage++; /* Counting the number of fixed covariate with age */
12142: }else{
12143: cptcovprodvage++; /* Counting the number of fixedvarying covariate with age */
12144: }
12145: }else{ /* for product Vn*Vm */
12146: Typevar[k]=2; /* 2 for product Vn*Vm */
12147: cutl(stre,strb,strc,'V'); /* strc= Vn, stre is n; strb=V3*V2 stre=3 strc=*/
12148: n=atoi(stre);
12149: cutl(strc,strb,strd,'V'); /* strd was Vm, strc is m */
12150: m=atoi(strc);
12151: k1++;
12152: cptcovprodnoage++;
12153: if(existcomb[n][m] != 0 || existcomb[m][n] != 0){
12154: printf("Warning in model combination V%d*V%d already exists in the model in position k1=%d!\n",n,m,existcomb[n][m]);
12155: fprintf(ficlog,"Warning in model combination V%d*V%d already exists in the model in position k1=%d!\n",n,m,existcomb[n][m]);
12156: fflush(ficlog);
12157: k11=existcomb[n][m];
12158: Tvar[k]=ncovcol+nqv+ntv+nqtv+k11;
12159: Tposprod[k]=k11;
12160: Tprod[k11]=k;
12161: Tvardk[k][1] =m; /* m 1 for V1*/
12162: /* Tvard[k11][1] =m; /\* n 4 for V4*\/ */
12163: Tvardk[k][2] =n; /* n 4 for V4*/
12164: /* Tvard[k11][2] =n; /\* n 4 for V4*\/ */
12165: }else{ /* combination Vn*Vm doesn't exist we create it (no age)*/
12166: existcomb[n][m]=k1;
12167: existcomb[m][n]=k1;
12168: Tvar[k]=ncovcol+nqv+ntv+nqtv+k1; /* ncovcolt+k1; For model-covariate k tells which data-covariate to use but
12169: because this model-covariate is a construction we invent a new column
12170: which is after existing variables ncovcol+nqv+ntv+nqtv + k1
12171: If already ncovcol=4 and model= V2 + V1 + V1*V4 + age*V3 + V3*V2
12172: thus after V4 we invent V5 and V6 because age*V3 will be computed in 4
12173: Tvar[3=V1*V4]=4+1=5 Tvar[5=V3*V2]=4 + 2= 6, Tvar[4=age*V3]=3 etc */
12174: /* Please remark that the new variables are model dependent */
12175: /* If we have 4 variable but the model uses only 3, like in
12176: * model= V1 + age*V1 + V2 + V3 + age*V2 + age*V3 + V1*V2 + V1*V3
12177: * k= 1 2 3 4 5 6 7 8
12178: * Tvar[k]=1 1 2 3 2 3 (5 6) (and not 4 5 because of V4 missing)
12179: * Tage[kk] [1]= 2 [2]=5 [3]=6 kk=1 to cptcovage=3
12180: * Tvar[Tage[kk]][1]=2 [2]=2 [3]=3
12181: */
12182: /* We need to feed some variables like TvarVV, but later on next loop because of ncovv (k2) is not correct */
12183: Tprod[k1]=k; /* Tprod[1]=3(=V1*V4) for V2+V1+V1*V4+age*V3+V3*V2 +V6*V2*age */
12184: Tposprod[k]=k1; /* Tposprod[3]=1, Tposprod[2]=5 */
12185: Tvard[k1][1] =m; /* m 1 for V1*/
12186: Tvardk[k][1] =m; /* m 1 for V1*/
12187: Tvard[k1][2] =n; /* n 4 for V4*/
12188: Tvardk[k][2] =n; /* n 4 for V4*/
12189: k2=k2+2; /* k2 is initialize to -1, We want to store the n and m in Vn*Vm at the end of Tvar */
12190: /* Tvar[cptcovt+k2]=Tvard[k1][1]; /\* Tvar[(cptcovt=4+k2=1)=5]= 1 (V1) *\/ */
12191: /* Tvar[cptcovt+k2+1]=Tvard[k1][2]; /\* Tvar[(cptcovt=4+(k2=1)+1)=6]= 4 (V4) *\/ */
12192: /*ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2, Tvar[3]=5, Tvar[4]=6, cptcovt=5 */
12193: /* 1 2 3 4 5 | Tvar[5+1)=1, Tvar[7]=2 */
12194: if( FixedV[Tvardk[k][1]] == 0 && FixedV[Tvardk[k][2]] == 0){ /* If the product is a fixed covariate then we feed the new column with Vn*Vm */
12195: for (i=1; i<=lastobs;i++){/* For fixed product */
12196: /* Computes the new covariate which is a product of
12197: covar[n][i]* covar[m][i] and stores it at ncovol+k1 May not be defined */
12198: covar[ncovcolt+k1][i]=covar[atoi(stre)][i]*covar[atoi(strc)][i];
12199: }
12200: /* TvarVV[k2]=n; */
12201: /* FixedV[ncovcolt+k2]=0; /\* or FixedV[Tvar[k]]=0; FixedV[TvarVV[ncovv]]=0 HERE *\/ */
12202: /* TvarVV[k2+1]=m; */
12203: /* FixedV[ncovcolt+k2]=0; /\* or FixedV[Tvar[k]]=0; FixedV[TvarVV[ncovv]]=0 HERE *\/ */
12204: }else{ /* not FixedV */
12205: /* TvarVV[k2]=n; */
12206: /* FixedV[ncovcolt+k2]=0; /\* or FixedV[Tvar[k]]=0; FixedV[TvarVV[ncovv]]=0 HERE *\/ */
12207: /* TvarVV[k2+1]=m; */
12208: /* FixedV[ncovcolt+k2]=0; /\* or FixedV[Tvar[k]]=0; FixedV[TvarVV[ncovv]]=0 HERE *\/ */
12209: }
12210: } /* End of creation of Vn*Vm if not created by age*Vn*Vm earlier */
12211: } /* End of product Vn*Vm */
12212: } /* End of age*double product or simple product */
12213: }else { /* not a product */
12214: /*printf("d=%s c=%s b=%s\n", strd,strc,strb);*/
12215: /* scanf("%d",i);*/
12216: cutl(strd,strc,strb,'V');
12217: ks++; /**< Number of simple covariates dummy or quantitative, fixe or varying */
12218: cptcovn++; /** V4+V3+V5: V4 and V3 timevarying dummy covariates, V5 timevarying quantitative */
12219: Tvar[k]=atoi(strd);
12220: Typevar[k]=0; /* 0 for simple covariates */
12221: }
12222: strcpy(modelsav,stra); /* modelsav=V2+V1+V4 stra=V2+V1+V4 */
12223: /*printf("a=%s b=%s sav=%s\n", stra,strb,modelsav);
12224: scanf("%d",i);*/
12225: } /* end of loop + on total covariates */
12226:
12227:
12228: } /* end if strlen(modelsave == 0) age*age might exist */
12229: } /* end if strlen(model == 0) */
12230: cptcovs=cptcovt - cptcovdageprod - cptcovprod;/**< Number of simple covariates V1 +V1*age +V3 +V3*V4 +age*age + age*v4*V3=> V1 + V3 =4+1-3=2 */
12231:
12232: /*The number n of Vn is stored in Tvar. cptcovage =number of age covariate. Tage gives the position of age. cptcovprod= number of products.
12233: If model=V1+V1*age then Tvar[1]=1 Tvar[2]=1 cptcovage=1 Tage[1]=2 cptcovprod=0*/
12234:
12235: /* printf("tvar1=%d tvar2=%d tvar3=%d cptcovage=%d Tage=%d",Tvar[1],Tvar[2],Tvar[3],cptcovage,Tage[1]);
12236: printf("cptcovprod=%d ", cptcovprod);
12237: fprintf(ficlog,"cptcovprod=%d ", cptcovprod);
12238: scanf("%d ",i);*/
12239:
12240:
12241: /* Until here, decodemodel knows only the grammar (simple, product, age*) of the model but not what kind
12242: of variable (dummy vs quantitative, fixed vs time varying) is behind. But we know the # of each. */
12243: /* ncovcol= 1, nqv=1 | ntv=2, nqtv= 1 = 5 possible variables data: 2 fixed 3, varying
12244: model= V5 + V4 +V3 + V4*V3 + V5*age + V2 + V1*V2 + V1*age + V5*age, V1 is not used saving its place
12245: k = 1 2 3 4 5 6 7 8 9
12246: Tvar[k]= 5 4 3 1+1+2+1+1=6 5 2 7 1 5
12247: Typevar[k]= 0 0 0 2 1 0 2 1 0
12248: Fixed[k] 1 1 1 1 3 0 0 or 2 2 3
12249: Dummy[k] 1 0 0 0 3 1 1 2 3
12250: Tmodelind[combination of covar]=k;
12251: */
12252: /* Dispatching between quantitative and time varying covariates */
12253: /* If Tvar[k] >ncovcol it is a product */
12254: /* Tvar[k] is the value n of Vn with n varying for 1 to nvcol, or p Vp=Vn*Vm for product */
12255: /* Computing effective variables, ie used by the model, that is from the cptcovt variables */
12256: printf("Model=1+age+%s\n\
12257: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product, 3 for double product with age \n\
12258: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\
12259: 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);
12260: fprintf(ficlog,"Model=1+age+%s\n\
12261: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product, 3 for double product with age \n\
12262: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\
12263: 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);
12264: for(k=-1;k<=NCOVMAX; k++){ Fixed[k]=0; Dummy[k]=0;}
12265: for(k=1;k<=NCOVMAX; k++){TvarFind[k]=0; TvarVind[k]=0;}
12266:
12267:
12268: /* Second loop for calculating Fixed[k], Dummy[k]*/
12269:
12270:
12271: for(k=1, ncovf=0, nsd=0, nsq=0, ncovv=0,ncovva=0,ncovvta=0, ncova=0, ncoveff=0, nqfveff=0, ntveff=0, nqtveff=0, ncovvt=0;k<=cptcovt; k++){ /* or cptocvt loop on k from model */
12272: if (Tvar[k] <=ncovcol && Typevar[k]==0 ){ /* Simple fixed dummy (<=ncovcol) covariates */
12273: Fixed[k]= 0;
12274: Dummy[k]= 0;
12275: ncoveff++;
12276: ncovf++;
12277: nsd++;
12278: modell[k].maintype= FTYPE;
12279: TvarsD[nsd]=Tvar[k];
12280: TvarsDind[nsd]=k;
12281: TnsdVar[Tvar[k]]=nsd;
12282: TvarF[ncovf]=Tvar[k];
12283: TvarFind[ncovf]=k;
12284: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
12285: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
12286: /* }else if( Tvar[k] <=ncovcol && Typevar[k]==2){ /\* Product of fixed dummy (<=ncovcol) covariates For a fixed product k is higher than ncovcol *\/ */
12287: }else if( Tvar[k] <=ncovcol+nqv && Typevar[k]==0){/* Remind that product Vn*Vm are added in k Only simple fixed quantitative variable */
12288: Fixed[k]= 0;
12289: Dummy[k]= 1;
12290: nqfveff++;
12291: modell[k].maintype= FTYPE;
12292: modell[k].subtype= FQ;
12293: nsq++;
12294: TvarsQ[nsq]=Tvar[k]; /* Gives the variable name (extended to products) of first nsq simple quantitative covariates (fixed or time vary see below */
12295: TvarsQind[nsq]=k; /* Gives the position in the model equation of the first nsq simple quantitative covariates (fixed or time vary) */
12296: ncovf++;
12297: TvarF[ncovf]=Tvar[k];
12298: TvarFind[ncovf]=k;
12299: 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 */
12300: 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 */
12301: }else if( Tvar[k] <=ncovcol+nqv+ntv && Typevar[k]==0){/* Only simple time varying dummy variables */
12302: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
12303: /* model V1+V3+age*V1+age*V3+V1*V3 */
12304: /* Tvar={1, 3, 1, 3, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
12305: ncovvt++;
12306: TvarVV[ncovvt]=Tvar[k]; /* TvarVV[1]=V3 (first time varying in the model equation */
12307: TvarVVind[ncovvt]=k; /* TvarVVind[1]=2 (second position in the model equation */
12308:
12309: Fixed[k]= 1;
12310: Dummy[k]= 0;
12311: ntveff++; /* Only simple time varying dummy variable */
12312: modell[k].maintype= VTYPE;
12313: modell[k].subtype= VD;
12314: nsd++;
12315: TvarsD[nsd]=Tvar[k];
12316: TvarsDind[nsd]=k;
12317: TnsdVar[Tvar[k]]=nsd; /* To be verified */
12318: ncovv++; /* Only simple time varying variables */
12319: TvarV[ncovv]=Tvar[k];
12320: TvarVind[ncovv]=k; /* TvarVind[2]=2 TvarVind[3]=3 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Any time varying singele */
12321: 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 */
12322: 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 */
12323: 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);
12324: printf("Quasi TmodelInvind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv);
12325: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv && Typevar[k]==0){ /* Only simple time varying quantitative variable V5*/
12326: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
12327: /* model V1+V3+age*V1+age*V3+V1*V3 */
12328: /* Tvar={1, 3, 1, 3, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
12329: ncovvt++;
12330: TvarVV[ncovvt]=Tvar[k]; /* TvarVV[1]=V3 (first time varying in the model equation */
12331: TvarVVind[ncovvt]=k; /* TvarVV[1]=V3 (first time varying in the model equation */
12332:
12333: Fixed[k]= 1;
12334: Dummy[k]= 1;
12335: nqtveff++;
12336: modell[k].maintype= VTYPE;
12337: modell[k].subtype= VQ;
12338: ncovv++; /* Only simple time varying variables */
12339: nsq++;
12340: TvarsQ[nsq]=Tvar[k]; /* k=1 Tvar=5 nsq=1 TvarsQ[1]=5 */ /* Gives the variable name (extended to products) of first nsq simple quantitative covariates (fixed or time vary here) */
12341: TvarsQind[nsq]=k; /* For single quantitative covariate gives the model position of each single quantitative covariate *//* Gives the position in the model equation of the first nsq simple quantitative covariates (fixed or time vary) */
12342: TvarV[ncovv]=Tvar[k];
12343: TvarVind[ncovv]=k; /* TvarVind[1]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Any time varying singele */
12344: 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 */
12345: 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 */
12346: TmodelInvQind[nqtveff]=Tvar[k]- ncovcol-nqv-ntv;/* Only simple time varying quantitative variable */
12347: /* Tmodeliqind[k]=nqtveff;/\* Only simple time varying quantitative variable *\/ */
12348: /* printf("Quasi TmodelQind[%d]=%d,Tvar[TmodelQind[%d]]=V%d, ncovcol=%d, nqv=%d, ntv=%Ad,Tvar[k]- ncovcol-nqv-ntv=%d\n",nqtveff,k,nqtveff,Tvar[k], ncovcol, nqv, ntv, Tvar[k]- ncovcol-nqv-ntv); */
12349: /* printf("Quasi TmodelInvQind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv-ntv); */
12350: }else if (Typevar[k] == 1) { /* product with age */
12351: ncova++;
12352: TvarA[ncova]=Tvar[k];
12353: TvarAind[ncova]=k;
12354: /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
12355: /** Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product */
12356: if (Tvar[k] <=ncovcol ){ /* Product age with fixed dummy covariatee */
12357: Fixed[k]= 2;
12358: Dummy[k]= 2;
12359: modell[k].maintype= ATYPE;
12360: modell[k].subtype= APFD;
12361: ncovta++;
12362: TvarAVVA[ncovta]=Tvar[k]; /* (2)age*V3 */
12363: TvarAVVAind[ncovta]=k;
12364: /* ncoveff++; */
12365: }else if( Tvar[k] <=ncovcol+nqv) { /* Remind that product Vn*Vm are added in k*/
12366: Fixed[k]= 2;
12367: Dummy[k]= 3;
12368: modell[k].maintype= ATYPE;
12369: modell[k].subtype= APFQ; /* Product age * fixed quantitative */
12370: ncovta++;
12371: TvarAVVA[ncovta]=Tvar[k]; /* */
12372: TvarAVVAind[ncovta]=k;
12373: /* nqfveff++; /\* Only simple fixed quantitative variable *\/ */
12374: }else if( Tvar[k] <=ncovcol+nqv+ntv ){
12375: Fixed[k]= 3;
12376: Dummy[k]= 2;
12377: modell[k].maintype= ATYPE;
12378: modell[k].subtype= APVD; /* Product age * varying dummy */
12379: ncovva++;
12380: TvarVVA[ncovva]=Tvar[k]; /* (1)+age*V6 + (2)age*V7 */
12381: TvarVVAind[ncovva]=k;
12382: ncovta++;
12383: TvarAVVA[ncovta]=Tvar[k]; /* */
12384: TvarAVVAind[ncovta]=k;
12385: /* ntveff++; /\* Only simple time varying dummy variable *\/ */
12386: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv){
12387: Fixed[k]= 3;
12388: Dummy[k]= 3;
12389: modell[k].maintype= ATYPE;
12390: modell[k].subtype= APVQ; /* Product age * varying quantitative */
12391: ncovva++;
12392: TvarVVA[ncovva]=Tvar[k]; /* */
12393: TvarVVAind[ncovva]=k;
12394: ncovta++;
12395: TvarAVVA[ncovta]=Tvar[k]; /* (1)+age*V6 + (2)age*V7 */
12396: TvarAVVAind[ncovta]=k;
12397: /* nqtveff++;/\* Only simple time varying quantitative variable *\/ */
12398: }
12399: }else if( Tposprod[k]>0 && Typevar[k]==2){ /* Detects if fixed product no age Vm*Vn */
12400: printf("MEMORY ERRORR k=%d Tposprod[k]=%d, Typevar[k]=%d\n ",k, Tposprod[k], Typevar[k]);
12401: if(FixedV[Tvardk[k][1]] == 0 && FixedV[Tvardk[k][2]] == 0){ /* Needs a fixed product Product of fixed dummy (<=ncovcol) covariates For a fixed product k is higher than ncovcol V3*V2 */
12402: printf("MEMORY ERRORR k=%d Tvardk[k][1]=%d, Tvardk[k][2]=%d, FixedV[Tvardk[k][1]]=%d,FixedV[Tvardk[k][2]]=%d\n ",k,Tvardk[k][1],Tvardk[k][2],FixedV[Tvardk[k][1]],FixedV[Tvardk[k][2]]);
12403: Fixed[k]= 0;
12404: Dummy[k]= 0;
12405: ncoveff++;
12406: ncovf++;
12407: /* ncovv++; */
12408: /* TvarVV[ncovv]=Tvardk[k][1]; */
12409: /* FixedV[ncovcolt+ncovv]=0; /\* or FixedV[TvarVV[ncovv]]=0 HERE *\/ */
12410: /* ncovv++; */
12411: /* TvarVV[ncovv]=Tvardk[k][2]; */
12412: /* FixedV[ncovcolt+ncovv]=0; /\* or FixedV[TvarVV[ncovv]]=0 HERE *\/ */
12413: modell[k].maintype= FTYPE;
12414: TvarF[ncovf]=Tvar[k];
12415: /* TnsdVar[Tvar[k]]=nsd; */ /* To be done */
12416: TvarFind[ncovf]=k;
12417: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
12418: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
12419: }else{/* product varying Vn * Vm without age, V1+V3+age*V1+age*V3+V1*V3 looking at V1*V3, Typevar={0, 0, 1, 1, 2}, k=5, V1 is fixed, V3 is timevary, V5 is a product */
12420: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
12421: /* model V1+V3+age*V1+age*V3+V1*V3 + V1*V3*age*/
12422: /* Tvar={1, 3, 1, 3, 6, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
12423: k1=Tposprod[k]; /* Position in the products of product k, Tposprod={0, 0, 0, 0, 1, 1} k1=1 first product but second time varying because of V3 */
12424: ncovvt++;
12425: TvarVV[ncovvt]=Tvard[k1][1]; /* TvarVV[2]=V1 (because TvarVV[1] was V3, first time varying covariates */
12426: TvarVVind[ncovvt]=k; /* TvarVVind[2]=5 (because TvarVVind[2] was V1*V3 at position 5 */
12427: ncovvt++;
12428: TvarVV[ncovvt]=Tvard[k1][2]; /* TvarVV[3]=V3 */
12429: TvarVVind[ncovvt]=k; /* TvarVVind[2]=5 (because TvarVVind[2] was V1*V3 at position 5 */
12430:
12431: /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
12432: /** Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product */
12433:
12434: if(Tvard[k1][1] <=ncovcol){ /* Vn is dummy fixed, (Tvard[1][1]=V1), (Tvard[1][1]=V3 time varying) */
12435: if(Tvard[k1][2] <=ncovcol){ /* Vm is dummy fixed */
12436: Fixed[k]= 1;
12437: Dummy[k]= 0;
12438: modell[k].maintype= FTYPE;
12439: modell[k].subtype= FPDD; /* Product fixed dummy * fixed dummy */
12440: ncovf++; /* Fixed variables without age */
12441: TvarF[ncovf]=Tvar[k];
12442: TvarFind[ncovf]=k;
12443: }else if(Tvard[k1][2] <=ncovcol+nqv){ /* Vm is quanti fixed */
12444: Fixed[k]= 0; /* Fixed product */
12445: Dummy[k]= 1;
12446: modell[k].maintype= FTYPE;
12447: modell[k].subtype= FPDQ; /* Product fixed dummy * fixed quantitative */
12448: ncovf++; /* Varying variables without age */
12449: TvarF[ncovf]=Tvar[k];
12450: TvarFind[ncovf]=k;
12451: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){ /* Vm is a time varying dummy covariate */
12452: Fixed[k]= 1;
12453: Dummy[k]= 0;
12454: modell[k].maintype= VTYPE;
12455: modell[k].subtype= VPDD; /* Product fixed dummy * varying dummy */
12456: ncovv++; /* Varying variables without age */
12457: TvarV[ncovv]=Tvar[k]; /* TvarV[1]=Tvar[5]=5 because there is a V4 */
12458: TvarVind[ncovv]=k;/* TvarVind[1]=5 */
12459: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){ /* Vm is a time varying quantitative covariate */
12460: Fixed[k]= 1;
12461: Dummy[k]= 1;
12462: modell[k].maintype= VTYPE;
12463: modell[k].subtype= VPDQ; /* Product fixed dummy * varying quantitative */
12464: ncovv++; /* Varying variables without age */
12465: TvarV[ncovv]=Tvar[k];
12466: TvarVind[ncovv]=k;
12467: }
12468: }else if(Tvard[k1][1] <=ncovcol+nqv){ /* Vn is fixed quanti */
12469: if(Tvard[k1][2] <=ncovcol){ /* Vm is fixed dummy */
12470: Fixed[k]= 0; /* Fixed product */
12471: Dummy[k]= 1;
12472: modell[k].maintype= FTYPE;
12473: modell[k].subtype= FPDQ; /* Product fixed quantitative * fixed dummy */
12474: ncovf++; /* Fixed variables without age */
12475: TvarF[ncovf]=Tvar[k];
12476: TvarFind[ncovf]=k;
12477: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){ /* Vm is time varying */
12478: Fixed[k]= 1;
12479: Dummy[k]= 1;
12480: modell[k].maintype= VTYPE;
12481: modell[k].subtype= VPDQ; /* Product fixed quantitative * varying dummy */
12482: ncovv++; /* Varying variables without age */
12483: TvarV[ncovv]=Tvar[k];
12484: TvarVind[ncovv]=k;
12485: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){ /* Vm is time varying quanti */
12486: Fixed[k]= 1;
12487: Dummy[k]= 1;
12488: modell[k].maintype= VTYPE;
12489: modell[k].subtype= VPQQ; /* Product fixed quantitative * varying quantitative */
12490: ncovv++; /* Varying variables without age */
12491: TvarV[ncovv]=Tvar[k];
12492: TvarVind[ncovv]=k;
12493: ncovv++; /* Varying variables without age */
12494: TvarV[ncovv]=Tvar[k];
12495: TvarVind[ncovv]=k;
12496: }
12497: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv){ /* Vn is time varying dummy */
12498: if(Tvard[k1][2] <=ncovcol){
12499: Fixed[k]= 1;
12500: Dummy[k]= 1;
12501: modell[k].maintype= VTYPE;
12502: modell[k].subtype= VPDD; /* Product time varying dummy * fixed dummy */
12503: ncovv++; /* Varying variables without age */
12504: TvarV[ncovv]=Tvar[k];
12505: TvarVind[ncovv]=k;
12506: }else if(Tvard[k1][2] <=ncovcol+nqv){
12507: Fixed[k]= 1;
12508: Dummy[k]= 1;
12509: modell[k].maintype= VTYPE;
12510: modell[k].subtype= VPDQ; /* Product time varying dummy * fixed quantitative */
12511: ncovv++; /* Varying variables without age */
12512: TvarV[ncovv]=Tvar[k];
12513: TvarVind[ncovv]=k;
12514: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
12515: Fixed[k]= 1;
12516: Dummy[k]= 0;
12517: modell[k].maintype= VTYPE;
12518: modell[k].subtype= VPDD; /* Product time varying dummy * time varying dummy */
12519: ncovv++; /* Varying variables without age */
12520: TvarV[ncovv]=Tvar[k];
12521: TvarVind[ncovv]=k;
12522: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
12523: Fixed[k]= 1;
12524: Dummy[k]= 1;
12525: modell[k].maintype= VTYPE;
12526: modell[k].subtype= VPDQ; /* Product time varying dummy * time varying quantitative */
12527: ncovv++; /* Varying variables without age */
12528: TvarV[ncovv]=Tvar[k];
12529: TvarVind[ncovv]=k;
12530: }
12531: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv+nqtv){ /* Vn is time varying quanti */
12532: if(Tvard[k1][2] <=ncovcol){
12533: Fixed[k]= 1;
12534: Dummy[k]= 1;
12535: modell[k].maintype= VTYPE;
12536: modell[k].subtype= VPDQ; /* Product time varying quantitative * fixed dummy */
12537: ncovv++; /* Varying variables without age */
12538: TvarV[ncovv]=Tvar[k];
12539: TvarVind[ncovv]=k;
12540: }else if(Tvard[k1][2] <=ncovcol+nqv){
12541: Fixed[k]= 1;
12542: Dummy[k]= 1;
12543: modell[k].maintype= VTYPE;
12544: modell[k].subtype= VPQQ; /* Product time varying quantitative * fixed quantitative */
12545: ncovv++; /* Varying variables without age */
12546: TvarV[ncovv]=Tvar[k];
12547: TvarVind[ncovv]=k;
12548: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
12549: Fixed[k]= 1;
12550: Dummy[k]= 1;
12551: modell[k].maintype= VTYPE;
12552: modell[k].subtype= VPDQ; /* Product time varying quantitative * time varying dummy */
12553: ncovv++; /* Varying variables without age */
12554: TvarV[ncovv]=Tvar[k];
12555: TvarVind[ncovv]=k;
12556: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
12557: Fixed[k]= 1;
12558: Dummy[k]= 1;
12559: modell[k].maintype= VTYPE;
12560: modell[k].subtype= VPQQ; /* Product time varying quantitative * time varying quantitative */
12561: ncovv++; /* Varying variables without age */
12562: TvarV[ncovv]=Tvar[k];
12563: TvarVind[ncovv]=k;
12564: }
12565: }else{
12566: printf("Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
12567: fprintf(ficlog,"Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
12568: } /*end k1*/
12569: }
12570: }else if(Typevar[k] == 3){ /* product Vn * Vm with age, V1+V3+age*V1+age*V3+V1*V3 looking at V1*V3, Typevar={0, 0, 1, 1, 2}, k=5, V1 is fixed, V3 is timevary, V5 is a product */
12571: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
12572: /* model V1+V3+age*V1+age*V3+V1*V3 + V1*V3*age*/
12573: /* Tvar={1, 3, 1, 3, 6, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
12574: k1=Tposprod[k]; /* Position in the products of product k, Tposprod={0, 0, 0, 0, 1, 1} k1=1 first product but second time varying because of V3 */
12575: ncova++;
12576: TvarA[ncova]=Tvard[k1][1]; /* TvarVV[2]=V1 (because TvarVV[1] was V3, first time varying covariates */
12577: TvarAind[ncova]=k; /* TvarVVind[2]=5 (because TvarVVind[2] was V1*V3 at position 5 */
12578: ncova++;
12579: TvarA[ncova]=Tvard[k1][2]; /* TvarVV[3]=V3 */
12580: TvarAind[ncova]=k; /* TvarVVind[2]=5 (because TvarVVind[2] was V1*V3 at position 5 */
12581:
12582: /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
12583: /** Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product */
12584: if( FixedV[Tvardk[k][1]] == 0 && FixedV[Tvardk[k][2]] == 0){
12585: ncovta++;
12586: TvarAVVA[ncovta]=Tvard[k1][1]; /* age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 */
12587: TvarAVVAind[ncovta]=k;
12588: ncovta++;
12589: TvarAVVA[ncovta]=Tvard[k1][2]; /* age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 */
12590: TvarAVVAind[ncovta]=k;
12591: }else{
12592: ncovva++; /* HERY reached */
12593: TvarVVA[ncovva]=Tvard[k1][1]; /* age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 */
12594: TvarVVAind[ncovva]=k;
12595: ncovva++;
12596: TvarVVA[ncovva]=Tvard[k1][2]; /* */
12597: TvarVVAind[ncovva]=k;
12598: ncovta++;
12599: TvarAVVA[ncovta]=Tvard[k1][1]; /* age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 */
12600: TvarAVVAind[ncovta]=k;
12601: ncovta++;
12602: TvarAVVA[ncovta]=Tvard[k1][2]; /* age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 */
12603: TvarAVVAind[ncovta]=k;
12604: }
12605: if(Tvard[k1][1] <=ncovcol){ /* Vn is dummy fixed, (Tvard[1][1]=V1), (Tvard[1][1]=V3 time varying) */
12606: if(Tvard[k1][2] <=ncovcol){ /* Vm is dummy fixed */
12607: Fixed[k]= 2;
12608: Dummy[k]= 2;
12609: modell[k].maintype= FTYPE;
12610: modell[k].subtype= FPDD; /* Product fixed dummy * fixed dummy */
12611: /* TvarF[ncova]=Tvar[k]; /\* Problem to solve *\/ */
12612: /* TvarFind[ncova]=k; */
12613: }else if(Tvard[k1][2] <=ncovcol+nqv){ /* Vm is quanti fixed */
12614: Fixed[k]= 2; /* Fixed product */
12615: Dummy[k]= 3;
12616: modell[k].maintype= FTYPE;
12617: modell[k].subtype= FPDQ; /* Product fixed dummy * fixed quantitative */
12618: /* TvarF[ncova]=Tvar[k]; */
12619: /* TvarFind[ncova]=k; */
12620: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){ /* Vm is a time varying dummy covariate */
12621: Fixed[k]= 3;
12622: Dummy[k]= 2;
12623: modell[k].maintype= VTYPE;
12624: modell[k].subtype= VPDD; /* Product fixed dummy * varying dummy */
12625: TvarV[ncova]=Tvar[k]; /* TvarV[1]=Tvar[5]=5 because there is a V4 */
12626: TvarVind[ncova]=k;/* TvarVind[1]=5 */
12627: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){ /* Vm is a time varying quantitative covariate */
12628: Fixed[k]= 3;
12629: Dummy[k]= 3;
12630: modell[k].maintype= VTYPE;
12631: modell[k].subtype= VPDQ; /* Product fixed dummy * varying quantitative */
12632: /* ncovv++; /\* Varying variables without age *\/ */
12633: /* TvarV[ncovv]=Tvar[k]; */
12634: /* TvarVind[ncovv]=k; */
12635: }
12636: }else if(Tvard[k1][1] <=ncovcol+nqv){ /* Vn is fixed quanti */
12637: if(Tvard[k1][2] <=ncovcol){ /* Vm is fixed dummy */
12638: Fixed[k]= 2; /* Fixed product */
12639: Dummy[k]= 2;
12640: modell[k].maintype= FTYPE;
12641: modell[k].subtype= FPDQ; /* Product fixed quantitative * fixed dummy */
12642: /* ncova++; /\* Fixed variables with age *\/ */
12643: /* TvarF[ncovf]=Tvar[k]; */
12644: /* TvarFind[ncovf]=k; */
12645: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){ /* Vm is time varying */
12646: Fixed[k]= 2;
12647: Dummy[k]= 3;
12648: modell[k].maintype= VTYPE;
12649: modell[k].subtype= VPDQ; /* Product fixed quantitative * varying dummy */
12650: /* ncova++; /\* Varying variables with age *\/ */
12651: /* TvarV[ncova]=Tvar[k]; */
12652: /* TvarVind[ncova]=k; */
12653: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){ /* Vm is time varying quanti */
12654: Fixed[k]= 3;
12655: Dummy[k]= 2;
12656: modell[k].maintype= VTYPE;
12657: modell[k].subtype= VPQQ; /* Product fixed quantitative * varying quantitative */
12658: ncova++; /* Varying variables without age */
12659: TvarV[ncova]=Tvar[k];
12660: TvarVind[ncova]=k;
12661: /* ncova++; /\* Varying variables without age *\/ */
12662: /* TvarV[ncova]=Tvar[k]; */
12663: /* TvarVind[ncova]=k; */
12664: }
12665: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv){ /* Vn is time varying dummy */
12666: if(Tvard[k1][2] <=ncovcol){
12667: Fixed[k]= 2;
12668: Dummy[k]= 2;
12669: modell[k].maintype= VTYPE;
12670: modell[k].subtype= VPDD; /* Product time varying dummy * fixed dummy */
12671: /* ncova++; /\* Varying variables with age *\/ */
12672: /* TvarV[ncova]=Tvar[k]; */
12673: /* TvarVind[ncova]=k; */
12674: }else if(Tvard[k1][2] <=ncovcol+nqv){
12675: Fixed[k]= 2;
12676: Dummy[k]= 3;
12677: modell[k].maintype= VTYPE;
12678: modell[k].subtype= VPDQ; /* Product time varying dummy * fixed quantitative */
12679: /* ncova++; /\* Varying variables with age *\/ */
12680: /* TvarV[ncova]=Tvar[k]; */
12681: /* TvarVind[ncova]=k; */
12682: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
12683: Fixed[k]= 3;
12684: Dummy[k]= 2;
12685: modell[k].maintype= VTYPE;
12686: modell[k].subtype= VPDD; /* Product time varying dummy * time varying dummy */
12687: /* ncova++; /\* Varying variables with age *\/ */
12688: /* TvarV[ncova]=Tvar[k]; */
12689: /* TvarVind[ncova]=k; */
12690: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
12691: Fixed[k]= 3;
12692: Dummy[k]= 3;
12693: modell[k].maintype= VTYPE;
12694: modell[k].subtype= VPDQ; /* Product time varying dummy * time varying quantitative */
12695: /* ncova++; /\* Varying variables with age *\/ */
12696: /* TvarV[ncova]=Tvar[k]; */
12697: /* TvarVind[ncova]=k; */
12698: }
12699: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv+nqtv){ /* Vn is time varying quanti */
12700: if(Tvard[k1][2] <=ncovcol){
12701: Fixed[k]= 2;
12702: Dummy[k]= 2;
12703: modell[k].maintype= VTYPE;
12704: modell[k].subtype= VPDQ; /* Product time varying quantitative * fixed dummy */
12705: /* ncova++; /\* Varying variables with age *\/ */
12706: /* TvarV[ncova]=Tvar[k]; */
12707: /* TvarVind[ncova]=k; */
12708: }else if(Tvard[k1][2] <=ncovcol+nqv){
12709: Fixed[k]= 2;
12710: Dummy[k]= 3;
12711: modell[k].maintype= VTYPE;
12712: modell[k].subtype= VPQQ; /* Product time varying quantitative * fixed quantitative */
12713: /* ncova++; /\* Varying variables with age *\/ */
12714: /* TvarV[ncova]=Tvar[k]; */
12715: /* TvarVind[ncova]=k; */
12716: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
12717: Fixed[k]= 3;
12718: Dummy[k]= 2;
12719: modell[k].maintype= VTYPE;
12720: modell[k].subtype= VPDQ; /* Product time varying quantitative * time varying dummy */
12721: /* ncova++; /\* Varying variables with age *\/ */
12722: /* TvarV[ncova]=Tvar[k]; */
12723: /* TvarVind[ncova]=k; */
12724: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
12725: Fixed[k]= 3;
12726: Dummy[k]= 3;
12727: modell[k].maintype= VTYPE;
12728: modell[k].subtype= VPQQ; /* Product time varying quantitative * time varying quantitative */
12729: /* ncova++; /\* Varying variables with age *\/ */
12730: /* TvarV[ncova]=Tvar[k]; */
12731: /* TvarVind[ncova]=k; */
12732: }
12733: }else{
12734: printf("Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
12735: fprintf(ficlog,"Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
12736: } /*end k1*/
12737: } else{
12738: printf("Error, current version can't treat for performance reasons, Tvar[%d]=%d, Typevar[%d]=%d\n", k, Tvar[k], k, Typevar[k]);
12739: fprintf(ficlog,"Error, current version can't treat for performance reasons, Tvar[%d]=%d, Typevar[%d]=%d\n", k, Tvar[k], k, Typevar[k]);
12740: }
12741: /* 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]); */
12742: /* printf(" modell[%d].maintype=%d, modell[%d].subtype=%d\n",k,modell[k].maintype,k,modell[k].subtype); */
12743: 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]);
12744: }
12745: ncovvta=ncovva;
12746: /* Searching for doublons in the model */
12747: for(k1=1; k1<= cptcovt;k1++){
12748: for(k2=1; k2 <k1;k2++){
12749: /* if((Typevar[k1]==Typevar[k2]) && (Fixed[Tvar[k1]]==Fixed[Tvar[k2]]) && (Dummy[Tvar[k1]]==Dummy[Tvar[k2]] )){ */
12750: if((Typevar[k1]==Typevar[k2]) && (Fixed[k1]==Fixed[k2]) && (Dummy[k1]==Dummy[k2] )){
12751: if((Typevar[k1] == 0 || Typevar[k1] == 1)){ /* Simple or age product */
12752: if(Tvar[k1]==Tvar[k2]){
12753: printf("Error duplication in the model=1+age+%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[k1],Dummy[k1]);
12754: fprintf(ficlog,"Error duplication in the model=1+age+%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[k1],Dummy[k1]); fflush(ficlog);
12755: return(1);
12756: }
12757: }else if (Typevar[k1] ==2){
12758: k3=Tposprod[k1];
12759: k4=Tposprod[k2];
12760: 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])) ){
12761: printf("Error duplication in the model=1+age+%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]]);
12762: fprintf(ficlog,"Error duplication in the model=1+age+%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);
12763: return(1);
12764: }
12765: }
12766: }
12767: }
12768: }
12769: printf("ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
12770: fprintf(ficlog,"ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
12771: printf("ncovf=%d, ncovv=%d, ncova=%d, nsd=%d, nsq=%d\n",ncovf,ncovv,ncova,nsd,nsq);
12772: fprintf(ficlog,"ncovf=%d, ncovv=%d, ncova=%d, nsd=%d, nsq=%d\n",ncovf,ncovv,ncova,nsd, nsq);
12773:
12774: free_imatrix(existcomb,1,NCOVMAX,1,NCOVMAX);
12775: return (0); /* with covar[new additional covariate if product] and Tage if age */
12776: /*endread:*/
12777: printf("Exiting decodemodel: ");
12778: return (1);
12779: }
12780:
12781: int calandcheckages(int imx, int maxwav, double *agemin, double *agemax, int *nberr, int *nbwarn )
12782: {/* Check ages at death */
12783: int i, m;
12784: int firstone=0;
12785:
12786: for (i=1; i<=imx; i++) {
12787: for(m=2; (m<= maxwav); m++) {
12788: if (((int)mint[m][i]== 99) && (s[m][i] <= nlstate)){
12789: anint[m][i]=9999;
12790: if (s[m][i] != -2) /* Keeping initial status of unknown vital status */
12791: s[m][i]=-1;
12792: }
12793: if((int)moisdc[i]==99 && (int)andc[i]==9999 && s[m][i]>nlstate){
12794: *nberr = *nberr + 1;
12795: if(firstone == 0){
12796: firstone=1;
12797: printf("Warning (#%d)! Date of death (month %2d and year %4d) of individual %ld on line %d was unknown but status is a death state %d at wave %d. If you don't know the vital status, please enter -2. If he/she is still alive but don't know the state, please code with '-1 or '.'. Here, we do not believe in a death, skipped.\nOther similar cases in log file\n", *nberr,(int)moisdc[i],(int)andc[i],num[i],i,s[m][i],m);
12798: }
12799: fprintf(ficlog,"Warning (#%d)! Date of death (month %2d and year %4d) of individual %ld on line %d was unknown but status is a death state %d at wave %d. If you don't know the vital status, please enter -2. If he/she is still alive but don't know the state, please code with '-1 or '.'. Here, we do not believe in a death, skipped.\n", *nberr,(int)moisdc[i],(int)andc[i],num[i],i,s[m][i],m);
12800: s[m][i]=-1; /* Droping the death status */
12801: }
12802: if((int)moisdc[i]==99 && (int)andc[i]!=9999 && s[m][i]>nlstate){
12803: (*nberr)++;
12804: printf("Error (#%d)! Month of death of individual %ld on line %d was unknown (%2d) (year of death is %4d) and status is a death state %d at wave %d. Please impute an arbitrary (or not) month and rerun. Currently this transition to death will be skipped (status is set to -2).\nOther similar cases in log file\n", *nberr, num[i],i,(int)moisdc[i],(int)andc[i],s[m][i],m);
12805: fprintf(ficlog,"Error (#%d)! Month of death of individual %ld on line %d was unknown (%2d) (year of death is %4d) and status is a death state %d at wave %d. Please impute an arbitrary (or not) month and rerun. Currently this transition to death will be skipped (status is set to -2).\n", *nberr, num[i],i,(int)moisdc[i],(int)andc[i],s[m][i],m);
12806: s[m][i]=-2; /* We prefer to skip it (and to skip it in version 0.8a1 too */
12807: }
12808: }
12809: }
12810:
12811: for (i=1; i<=imx; i++) {
12812: agedc[i]=(moisdc[i]/12.+andc[i])-(moisnais[i]/12.+annais[i]);
12813: for(m=firstpass; (m<= lastpass); m++){
12814: 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 */
12815: if (s[m][i] >= nlstate+1) {
12816: if(agedc[i]>0){
12817: if((int)moisdc[i]!=99 && (int)andc[i]!=9999){
12818: agev[m][i]=agedc[i];
12819: /*if(moisdc[i]==99 && andc[i]==9999) s[m][i]=-1;*/
12820: }else {
12821: if ((int)andc[i]!=9999){
12822: nbwarn++;
12823: printf("Warning negative age at death: %ld line:%d\n",num[i],i);
12824: fprintf(ficlog,"Warning negative age at death: %ld line:%d\n",num[i],i);
12825: agev[m][i]=-1;
12826: }
12827: }
12828: } /* agedc > 0 */
12829: } /* end if */
12830: else if(s[m][i] !=9){ /* Standard case, age in fractional
12831: years but with the precision of a month */
12832: agev[m][i]=(mint[m][i]/12.+1./24.+anint[m][i])-(moisnais[i]/12.+1./24.+annais[i]);
12833: if((int)mint[m][i]==99 || (int)anint[m][i]==9999)
12834: agev[m][i]=1;
12835: else if(agev[m][i] < *agemin){
12836: *agemin=agev[m][i];
12837: printf(" Min anint[%d][%d]=%.2f annais[%d]=%.2f, agemin=%.2f\n",m,i,anint[m][i], i,annais[i], *agemin);
12838: }
12839: else if(agev[m][i] >*agemax){
12840: *agemax=agev[m][i];
12841: /* printf(" Max anint[%d][%d]=%.0f annais[%d]=%.0f, agemax=%.2f\n",m,i,anint[m][i], i,annais[i], *agemax);*/
12842: }
12843: /*agev[m][i]=anint[m][i]-annais[i];*/
12844: /* agev[m][i] = age[i]+2*m;*/
12845: } /* en if 9*/
12846: else { /* =9 */
12847: /* printf("Debug num[%d]=%ld s[%d][%d]=%d\n",i,num[i], m,i, s[m][i]); */
12848: agev[m][i]=1;
12849: s[m][i]=-1;
12850: }
12851: }
12852: else if(s[m][i]==0) /*= 0 Unknown */
12853: agev[m][i]=1;
12854: else{
12855: printf("Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
12856: fprintf(ficlog, "Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
12857: agev[m][i]=0;
12858: }
12859: } /* End for lastpass */
12860: }
12861:
12862: for (i=1; i<=imx; i++) {
12863: for(m=firstpass; (m<=lastpass); m++){
12864: if (s[m][i] > (nlstate+ndeath)) {
12865: (*nberr)++;
12866: 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);
12867: 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);
12868: return 1;
12869: }
12870: }
12871: }
12872:
12873: /*for (i=1; i<=imx; i++){
12874: for (m=firstpass; (m<lastpass); m++){
12875: printf("%ld %d %.lf %d %d\n", num[i],(covar[1][i]),agev[m][i],s[m][i],s[m+1][i]);
12876: }
12877:
12878: }*/
12879:
12880:
12881: printf("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
12882: fprintf(ficlog,"Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
12883:
12884: return (0);
12885: /* endread:*/
12886: printf("Exiting calandcheckages: ");
12887: return (1);
12888: }
12889:
12890: #if defined(_MSC_VER)
12891: /*printf("Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
12892: /*fprintf(ficlog, "Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
12893: //#include "stdafx.h"
12894: //#include <stdio.h>
12895: //#include <tchar.h>
12896: //#include <windows.h>
12897: //#include <iostream>
12898: typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
12899:
12900: LPFN_ISWOW64PROCESS fnIsWow64Process;
12901:
12902: BOOL IsWow64()
12903: {
12904: BOOL bIsWow64 = FALSE;
12905:
12906: //typedef BOOL (APIENTRY *LPFN_ISWOW64PROCESS)
12907: // (HANDLE, PBOOL);
12908:
12909: //LPFN_ISWOW64PROCESS fnIsWow64Process;
12910:
12911: HMODULE module = GetModuleHandle(_T("kernel32"));
12912: const char funcName[] = "IsWow64Process";
12913: fnIsWow64Process = (LPFN_ISWOW64PROCESS)
12914: GetProcAddress(module, funcName);
12915:
12916: if (NULL != fnIsWow64Process)
12917: {
12918: if (!fnIsWow64Process(GetCurrentProcess(),
12919: &bIsWow64))
12920: //throw std::exception("Unknown error");
12921: printf("Unknown error\n");
12922: }
12923: return bIsWow64 != FALSE;
12924: }
12925: #endif
12926:
12927: void syscompilerinfo(int logged)
12928: {
12929: #include <stdint.h>
12930:
12931: /* #include "syscompilerinfo.h"*/
12932: /* command line Intel compiler 32bit windows, XP compatible:*/
12933: /* /GS /W3 /Gy
12934: /Zc:wchar_t /Zi /O2 /Fd"Release\vc120.pdb" /D "WIN32" /D "NDEBUG" /D
12935: "_CONSOLE" /D "_LIB" /D "_USING_V110_SDK71_" /D "_UNICODE" /D
12936: "UNICODE" /Qipo /Zc:forScope /Gd /Oi /MT /Fa"Release\" /EHsc /nologo
12937: /Fo"Release\" /Qprof-dir "Release\" /Fp"Release\IMaCh.pch"
12938: */
12939: /* 64 bits */
12940: /*
12941: /GS /W3 /Gy
12942: /Zc:wchar_t /Zi /O2 /Fd"x64\Release\vc120.pdb" /D "WIN32" /D "NDEBUG"
12943: /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo /Zc:forScope
12944: /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Fo"x64\Release\" /Qprof-dir
12945: "x64\Release\" /Fp"x64\Release\IMaCh.pch" */
12946: /* Optimization are useless and O3 is slower than O2 */
12947: /*
12948: /GS /W3 /Gy /Zc:wchar_t /Zi /O3 /Fd"x64\Release\vc120.pdb" /D "WIN32"
12949: /D "NDEBUG" /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo
12950: /Zc:forScope /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Qparallel
12951: /Fo"x64\Release\" /Qprof-dir "x64\Release\" /Fp"x64\Release\IMaCh.pch"
12952: */
12953: /* Link is */ /* /OUT:"visual studio
12954: 2013\Projects\IMaCh\Release\IMaCh.exe" /MANIFEST /NXCOMPAT
12955: /PDB:"visual studio
12956: 2013\Projects\IMaCh\Release\IMaCh.pdb" /DYNAMICBASE
12957: "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib"
12958: "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib"
12959: "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib"
12960: /MACHINE:X86 /OPT:REF /SAFESEH /INCREMENTAL:NO
12961: /SUBSYSTEM:CONSOLE",5.01" /MANIFESTUAC:"level='asInvoker'
12962: uiAccess='false'"
12963: /ManifestFile:"Release\IMaCh.exe.intermediate.manifest" /OPT:ICF
12964: /NOLOGO /TLBID:1
12965: */
12966:
12967:
12968: #if defined __INTEL_COMPILER
12969: #if defined(__GNUC__)
12970: struct utsname sysInfo; /* For Intel on Linux and OS/X */
12971: #endif
12972: #elif defined(__GNUC__)
12973: #ifndef __APPLE__
12974: #include <gnu/libc-version.h> /* Only on gnu */
12975: #endif
12976: struct utsname sysInfo;
12977: int cross = CROSS;
12978: if (cross){
12979: printf("Cross-");
12980: if(logged) fprintf(ficlog, "Cross-");
12981: }
12982: #endif
12983:
12984: printf("Compiled with:");if(logged)fprintf(ficlog,"Compiled with:");
12985: #if defined(__clang__)
12986: printf(" Clang/LLVM");if(logged)fprintf(ficlog," Clang/LLVM"); /* Clang/LLVM. ---------------------------------------------- */
12987: #endif
12988: #if defined(__ICC) || defined(__INTEL_COMPILER)
12989: printf(" Intel ICC/ICPC");if(logged)fprintf(ficlog," Intel ICC/ICPC");/* Intel ICC/ICPC. ------------------------------------------ */
12990: #endif
12991: #if defined(__GNUC__) || defined(__GNUG__)
12992: printf(" GNU GCC/G++");if(logged)fprintf(ficlog," GNU GCC/G++");/* GNU GCC/G++. --------------------------------------------- */
12993: #endif
12994: #if defined(__HP_cc) || defined(__HP_aCC)
12995: printf(" Hewlett-Packard C/aC++");if(logged)fprintf(fcilog," Hewlett-Packard C/aC++"); /* Hewlett-Packard C/aC++. ---------------------------------- */
12996: #endif
12997: #if defined(__IBMC__) || defined(__IBMCPP__)
12998: printf(" IBM XL C/C++"); if(logged) fprintf(ficlog," IBM XL C/C++");/* IBM XL C/C++. -------------------------------------------- */
12999: #endif
13000: #if defined(_MSC_VER)
13001: printf(" Microsoft Visual Studio");if(logged)fprintf(ficlog," Microsoft Visual Studio");/* Microsoft Visual Studio. --------------------------------- */
13002: #endif
13003: #if defined(__PGI)
13004: printf(" Portland Group PGCC/PGCPP");if(logged) fprintf(ficlog," Portland Group PGCC/PGCPP");/* Portland Group PGCC/PGCPP. ------------------------------- */
13005: #endif
13006: #if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
13007: printf(" Oracle Solaris Studio");if(logged)fprintf(ficlog," Oracle Solaris Studio\n");/* Oracle Solaris Studio. ----------------------------------- */
13008: #endif
13009: printf(" for "); if (logged) fprintf(ficlog, " for ");
13010:
13011: // http://stackoverflow.com/questions/4605842/how-to-identify-platform-compiler-from-preprocessor-macros
13012: #ifdef _WIN32 // note the underscore: without it, it's not msdn official!
13013: // Windows (x64 and x86)
13014: printf("Windows (x64 and x86) ");if(logged) fprintf(ficlog,"Windows (x64 and x86) ");
13015: #elif __unix__ // all unices, not all compilers
13016: // Unix
13017: printf("Unix ");if(logged) fprintf(ficlog,"Unix ");
13018: #elif __linux__
13019: // linux
13020: printf("linux ");if(logged) fprintf(ficlog,"linux ");
13021: #elif __APPLE__
13022: // Mac OS, not sure if this is covered by __posix__ and/or __unix__ though..
13023: printf("Mac OS ");if(logged) fprintf(ficlog,"Mac OS ");
13024: #endif
13025:
13026: /* __MINGW32__ */
13027: /* __CYGWIN__ */
13028: /* __MINGW64__ */
13029: // http://msdn.microsoft.com/en-us/library/b0084kay.aspx
13030: /* _MSC_VER //the Visual C++ compiler is 17.00.51106.1, the _MSC_VER macro evaluates to 1700. Type cl /? */
13031: /* _MSC_FULL_VER //the Visual C++ compiler is 15.00.20706.01, the _MSC_FULL_VER macro evaluates to 150020706 */
13032: /* _WIN64 // Defined for applications for Win64. */
13033: /* _M_X64 // Defined for compilations that target x64 processors. */
13034: /* _DEBUG // Defined when you compile with /LDd, /MDd, and /MTd. */
13035:
13036: #if UINTPTR_MAX == 0xffffffff
13037: printf(" 32-bit"); if(logged) fprintf(ficlog," 32-bit");/* 32-bit */
13038: #elif UINTPTR_MAX == 0xffffffffffffffff
13039: printf(" 64-bit"); if(logged) fprintf(ficlog," 64-bit");/* 64-bit */
13040: #else
13041: printf(" wtf-bit"); if(logged) fprintf(ficlog," wtf-bit");/* wtf */
13042: #endif
13043:
13044: #if defined(__GNUC__)
13045: # if defined(__GNUC_PATCHLEVEL__)
13046: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
13047: + __GNUC_MINOR__ * 100 \
13048: + __GNUC_PATCHLEVEL__)
13049: # else
13050: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
13051: + __GNUC_MINOR__ * 100)
13052: # endif
13053: printf(" using GNU C version %d.\n", __GNUC_VERSION__);
13054: if(logged) fprintf(ficlog, " using GNU C version %d.\n", __GNUC_VERSION__);
13055:
13056: if (uname(&sysInfo) != -1) {
13057: printf("Running on: %s %s %s %s %s\n",sysInfo.sysname, sysInfo.nodename, sysInfo.release, sysInfo.version, sysInfo.machine);
13058: if(logged) fprintf(ficlog,"Running on: %s %s %s %s %s\n ",sysInfo.sysname, sysInfo.nodename, sysInfo.release, sysInfo.version, sysInfo.machine);
13059: }
13060: else
13061: perror("uname() error");
13062: //#ifndef __INTEL_COMPILER
13063: #if !defined (__INTEL_COMPILER) && !defined(__APPLE__)
13064: printf("GNU libc version: %s\n", gnu_get_libc_version());
13065: if(logged) fprintf(ficlog,"GNU libc version: %s\n", gnu_get_libc_version());
13066: #endif
13067: #endif
13068:
13069: // void main ()
13070: // {
13071: #if defined(_MSC_VER)
13072: if (IsWow64()){
13073: printf("\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
13074: if (logged) fprintf(ficlog, "\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
13075: }
13076: else{
13077: printf("\nThe program is not running under WOW64 (i.e probably on a 64bit Windows).\n");
13078: if (logged) fprintf(ficlog, "\nThe programm is not running under WOW64 (i.e probably on a 64bit Windows).\n");
13079: }
13080: // printf("\nPress Enter to continue...");
13081: // getchar();
13082: // }
13083:
13084: #endif
13085:
13086:
13087: }
13088:
13089: int prevalence_limit(double *p, double **prlim, double ageminpar, double agemaxpar, double ftolpl, int *ncvyearp){
13090: /*--------------- Prevalence limit (forward period or forward stable prevalence) --------------*/
13091: /* Computes the prevalence limit for each combination of the dummy covariates */
13092: int i, j, k, i1, k4=0, nres=0 ;
13093: /* double ftolpl = 1.e-10; */
13094: double age, agebase, agelim;
13095: double tot;
13096:
13097: strcpy(filerespl,"PL_");
13098: strcat(filerespl,fileresu);
13099: if((ficrespl=fopen(filerespl,"w"))==NULL) {
13100: printf("Problem with forward period (stable) prevalence resultfile: %s\n", filerespl);return 1;
13101: fprintf(ficlog,"Problem with forward period (stable) prevalence resultfile: %s\n", filerespl);return 1;
13102: }
13103: printf("\nComputing forward period (stable) prevalence: result on file '%s' \n", filerespl);
13104: fprintf(ficlog,"\nComputing forward period (stable) prevalence: result on file '%s' \n", filerespl);
13105: pstamp(ficrespl);
13106: fprintf(ficrespl,"# Forward period (stable) prevalence. Precision given by ftolpl=%g \n", ftolpl);
13107: fprintf(ficrespl,"#Age ");
13108: for(i=1; i<=nlstate;i++) fprintf(ficrespl,"%d-%d ",i,i);
13109: fprintf(ficrespl,"\n");
13110:
13111: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
13112:
13113: agebase=ageminpar;
13114: agelim=agemaxpar;
13115:
13116: /* i1=pow(2,ncoveff); */
13117: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
13118: if (cptcovn < 1){i1=1;}
13119:
13120: /* for(k=1; k<=i1;k++){ /\* For each combination k of dummy covariates in the model *\/ */
13121: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
13122: k=TKresult[nres];
13123: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
13124: /* if(i1 != 1 && TKresult[nres]!= k) /\* We found the combination k corresponding to the resultline value of dummies *\/ */
13125: /* continue; */
13126:
13127: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
13128: /* for(cptcov=1,k=0;cptcov<=1;cptcov++){ */
13129: //for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
13130: /* k=k+1; */
13131: /* to clean */
13132: /*printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));*/
13133: fprintf(ficrespl,"#******");
13134: printf("#******");
13135: fprintf(ficlog,"#******");
13136: for(j=1;j<=cptcovs ;j++) {/**< cptcovs number of SIMPLE covariates in the model or resultline V2+V1 =2 (dummy or quantit or time varying) */
13137: /* fprintf(ficrespl," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,Tvaraff[j])]); /\* Here problem for varying dummy*\/ */
13138: /* printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
13139: /* fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
13140: fprintf(ficrespl," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
13141: printf(" V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
13142: fprintf(ficlog," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
13143: }
13144: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
13145: /* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
13146: /* fprintf(ficrespl," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
13147: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
13148: /* } */
13149: fprintf(ficrespl,"******\n");
13150: printf("******\n");
13151: fprintf(ficlog,"******\n");
13152: if(invalidvarcomb[k]){
13153: printf("\nCombination (%d) ignored because no case \n",k);
13154: fprintf(ficrespl,"#Combination (%d) ignored because no case \n",k);
13155: fprintf(ficlog,"\nCombination (%d) ignored because no case \n",k);
13156: continue;
13157: }
13158:
13159: fprintf(ficrespl,"#Age ");
13160: /* for(j=1;j<=cptcoveff;j++) { */
13161: /* fprintf(ficrespl,"V%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
13162: /* } */
13163: for(j=1;j<=cptcovs;j++) { /* New the quanti variable is added */
13164: fprintf(ficrespl,"V%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
13165: }
13166: for(i=1; i<=nlstate;i++) fprintf(ficrespl," %d-%d ",i,i);
13167: fprintf(ficrespl,"Total Years_to_converge\n");
13168:
13169: for (age=agebase; age<=agelim; age++){
13170: /* for (age=agebase; age<=agebase; age++){ */
13171: /**< Computes the prevalence limit in each live state at age x and for covariate combination (k and) nres */
13172: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, ncvyearp, k, nres); /* Nicely done */
13173: fprintf(ficrespl,"%.0f ",age );
13174: /* for(j=1;j<=cptcoveff;j++) */
13175: /* fprintf(ficrespl,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
13176: for(j=1;j<=cptcovs;j++)
13177: fprintf(ficrespl,"%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
13178: tot=0.;
13179: for(i=1; i<=nlstate;i++){
13180: tot += prlim[i][i];
13181: fprintf(ficrespl," %.5f", prlim[i][i]);
13182: }
13183: fprintf(ficrespl," %.3f %d\n", tot, *ncvyearp);
13184: } /* Age */
13185: /* was end of cptcod */
13186: } /* nres */
13187: /* } /\* for each combination *\/ */
13188: return 0;
13189: }
13190:
13191: 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){
13192: /*--------------- Back Prevalence limit (backward stable prevalence) --------------*/
13193:
13194: /* Computes the back prevalence limit for any combination of covariate values
13195: * at any age between ageminpar and agemaxpar
13196: */
13197: int i, j, k, i1, nres=0 ;
13198: /* double ftolpl = 1.e-10; */
13199: double age, agebase, agelim;
13200: double tot;
13201: /* double ***mobaverage; */
13202: /* double **dnewm, **doldm, **dsavm; /\* for use *\/ */
13203:
13204: strcpy(fileresplb,"PLB_");
13205: strcat(fileresplb,fileresu);
13206: if((ficresplb=fopen(fileresplb,"w"))==NULL) {
13207: printf("Problem with backward prevalence resultfile: %s\n", fileresplb);return 1;
13208: fprintf(ficlog,"Problem with backward prevalence resultfile: %s\n", fileresplb);return 1;
13209: }
13210: printf("Computing backward prevalence: result on file '%s' \n", fileresplb);
13211: fprintf(ficlog,"Computing backward prevalence: result on file '%s' \n", fileresplb);
13212: pstamp(ficresplb);
13213: fprintf(ficresplb,"# Backward prevalence. Precision given by ftolpl=%g \n", ftolpl);
13214: fprintf(ficresplb,"#Age ");
13215: for(i=1; i<=nlstate;i++) fprintf(ficresplb,"%d-%d ",i,i);
13216: fprintf(ficresplb,"\n");
13217:
13218:
13219: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
13220:
13221: agebase=ageminpar;
13222: agelim=agemaxpar;
13223:
13224:
13225: i1=pow(2,cptcoveff);
13226: if (cptcovn < 1){i1=1;}
13227:
13228: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
13229: /* for(k=1; k<=i1;k++){ /\* For any combination of dummy covariates, fixed and varying *\/ */
13230: k=TKresult[nres];
13231: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
13232: /* if(i1 != 1 && TKresult[nres]!= k) */
13233: /* continue; */
13234: /* /\*printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));*\/ */
13235: fprintf(ficresplb,"#******");
13236: printf("#******");
13237: fprintf(ficlog,"#******");
13238: for(j=1;j<=cptcovs ;j++) {/**< cptcovs number of SIMPLE covariates in the model or resultline V2+V1 =2 (dummy or quantit or time varying) */
13239: printf(" V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
13240: fprintf(ficresplb," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
13241: fprintf(ficlog," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
13242: }
13243: /* for(j=1;j<=cptcoveff ;j++) {/\* all covariates *\/ */
13244: /* fprintf(ficresplb," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
13245: /* printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
13246: /* fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
13247: /* } */
13248: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
13249: /* printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
13250: /* fprintf(ficresplb," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
13251: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
13252: /* } */
13253: fprintf(ficresplb,"******\n");
13254: printf("******\n");
13255: fprintf(ficlog,"******\n");
13256: if(invalidvarcomb[k]){
13257: printf("\nCombination (%d) ignored because no cases \n",k);
13258: fprintf(ficresplb,"#Combination (%d) ignored because no cases \n",k);
13259: fprintf(ficlog,"\nCombination (%d) ignored because no cases \n",k);
13260: continue;
13261: }
13262:
13263: fprintf(ficresplb,"#Age ");
13264: for(j=1;j<=cptcovs;j++) {
13265: fprintf(ficresplb,"V%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
13266: }
13267: for(i=1; i<=nlstate;i++) fprintf(ficresplb," %d-%d ",i,i);
13268: fprintf(ficresplb,"Total Years_to_converge\n");
13269:
13270:
13271: for (age=agebase; age<=agelim; age++){
13272: /* for (age=agebase; age<=agebase; age++){ */
13273: if(mobilavproj > 0){
13274: /* bprevalim(bprlim, mobaverage, nlstate, p, age, ageminpar, agemaxpar, oldm, savm, doldm, dsavm, ftolpl, ncvyearp, k); */
13275: /* bprevalim(bprlim, mobaverage, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
13276: bprevalim(bprlim, mobaverage, nlstate, p, age, ftolpl, ncvyearp, k, nres);
13277: }else if (mobilavproj == 0){
13278: 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);
13279: 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);
13280: exit(1);
13281: }else{
13282: /* bprevalim(bprlim, probs, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
13283: bprevalim(bprlim, probs, nlstate, p, age, ftolpl, ncvyearp, k,nres);
13284: /* printf("TOTOT\n"); */
13285: /* exit(1); */
13286: }
13287: fprintf(ficresplb,"%.0f ",age );
13288: for(j=1;j<=cptcovs;j++)
13289: fprintf(ficresplb,"%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
13290: tot=0.;
13291: for(i=1; i<=nlstate;i++){
13292: tot += bprlim[i][i];
13293: fprintf(ficresplb," %.5f", bprlim[i][i]);
13294: }
13295: fprintf(ficresplb," %.3f %d\n", tot, *ncvyearp);
13296: } /* Age */
13297: /* was end of cptcod */
13298: /*fprintf(ficresplb,"\n");*/ /* Seems to be necessary for gnuplot only if two result lines and no covariate. */
13299: /* } /\* end of any combination *\/ */
13300: } /* end of nres */
13301: /* hBijx(p, bage, fage); */
13302: /* fclose(ficrespijb); */
13303:
13304: return 0;
13305: }
13306:
13307: int hPijx(double *p, int bage, int fage){
13308: /*------------- h Pij x at various ages ------------*/
13309: /* to be optimized with precov */
13310: int stepsize;
13311: int agelim;
13312: int hstepm;
13313: int nhstepm;
13314: int h, i, i1, j, k, k4, nres=0;
13315:
13316: double agedeb;
13317: double ***p3mat;
13318:
13319: strcpy(filerespij,"PIJ_"); strcat(filerespij,fileresu);
13320: if((ficrespij=fopen(filerespij,"w"))==NULL) {
13321: printf("Problem with Pij resultfile: %s\n", filerespij); return 1;
13322: fprintf(ficlog,"Problem with Pij resultfile: %s\n", filerespij); return 1;
13323: }
13324: printf("Computing pij: result on file '%s' \n", filerespij);
13325: fprintf(ficlog,"Computing pij: result on file '%s' \n", filerespij);
13326:
13327: stepsize=(int) (stepm+YEARM-1)/YEARM;
13328: /*if (stepm<=24) stepsize=2;*/
13329:
13330: agelim=AGESUP;
13331: hstepm=stepsize*YEARM; /* Every year of age */
13332: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
13333:
13334: /* hstepm=1; aff par mois*/
13335: pstamp(ficrespij);
13336: fprintf(ficrespij,"#****** h Pij x Probability to be in state j at age x+h being in i at x ");
13337: i1= pow(2,cptcoveff);
13338: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
13339: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
13340: /* k=k+1; */
13341: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
13342: k=TKresult[nres];
13343: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
13344: /* for(k=1; k<=i1;k++){ */
13345: /* if(i1 != 1 && TKresult[nres]!= k) */
13346: /* continue; */
13347: fprintf(ficrespij,"\n#****** ");
13348: for(j=1;j<=cptcovs;j++){
13349: fprintf(ficrespij," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
13350: /* fprintf(ficrespij,"@wV%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
13351: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
13352: /* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
13353: /* fprintf(ficrespij," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
13354: }
13355: fprintf(ficrespij,"******\n");
13356:
13357: for (agedeb=fage; agedeb>=bage; agedeb--){ /* If stepm=6 months */
13358: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
13359: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
13360:
13361: /* nhstepm=nhstepm*YEARM; aff par mois*/
13362:
13363: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
13364: oldm=oldms;savm=savms;
13365: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k, nres);
13366: fprintf(ficrespij,"# Cov Agex agex+h hpijx with i,j=");
13367: for(i=1; i<=nlstate;i++)
13368: for(j=1; j<=nlstate+ndeath;j++)
13369: fprintf(ficrespij," %1d-%1d",i,j);
13370: fprintf(ficrespij,"\n");
13371: for (h=0; h<=nhstepm; h++){
13372: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
13373: fprintf(ficrespij,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm );
13374: for(i=1; i<=nlstate;i++)
13375: for(j=1; j<=nlstate+ndeath;j++)
13376: fprintf(ficrespij," %.5f", p3mat[i][j][h]);
13377: fprintf(ficrespij,"\n");
13378: }
13379: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
13380: fprintf(ficrespij,"\n");
13381: }
13382: }
13383: /*}*/
13384: return 0;
13385: }
13386:
13387: int hBijx(double *p, int bage, int fage, double ***prevacurrent){
13388: /*------------- h Bij x at various ages ------------*/
13389: /* To be optimized with precov */
13390: int stepsize;
13391: /* int agelim; */
13392: int ageminl;
13393: int hstepm;
13394: int nhstepm;
13395: int h, i, i1, j, k, nres;
13396:
13397: double agedeb;
13398: double ***p3mat;
13399:
13400: strcpy(filerespijb,"PIJB_"); strcat(filerespijb,fileresu);
13401: if((ficrespijb=fopen(filerespijb,"w"))==NULL) {
13402: printf("Problem with Pij back resultfile: %s\n", filerespijb); return 1;
13403: fprintf(ficlog,"Problem with Pij back resultfile: %s\n", filerespijb); return 1;
13404: }
13405: printf("Computing pij back: result on file '%s' \n", filerespijb);
13406: fprintf(ficlog,"Computing pij back: result on file '%s' \n", filerespijb);
13407:
13408: stepsize=(int) (stepm+YEARM-1)/YEARM;
13409: /*if (stepm<=24) stepsize=2;*/
13410:
13411: /* agelim=AGESUP; */
13412: ageminl=AGEINF; /* was 30 */
13413: hstepm=stepsize*YEARM; /* Every year of age */
13414: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
13415:
13416: /* hstepm=1; aff par mois*/
13417: pstamp(ficrespijb);
13418: fprintf(ficrespijb,"#****** h Bij x Back probability to be in state i at age x-h being in j at x: B1j+B2j+...=1 ");
13419: i1= pow(2,cptcoveff);
13420: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
13421: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
13422: /* k=k+1; */
13423: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
13424: k=TKresult[nres];
13425: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
13426: /* for(k=1; k<=i1;k++){ /\* For any combination of dummy covariates, fixed and varying *\/ */
13427: /* if(i1 != 1 && TKresult[nres]!= k) */
13428: /* continue; */
13429: fprintf(ficrespijb,"\n#****** ");
13430: for(j=1;j<=cptcovs;j++){
13431: fprintf(ficrespijb," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
13432: /* for(j=1;j<=cptcoveff;j++) */
13433: /* fprintf(ficrespijb,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
13434: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
13435: /* fprintf(ficrespijb," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
13436: }
13437: fprintf(ficrespijb,"******\n");
13438: if(invalidvarcomb[k]){ /* Is it necessary here? */
13439: fprintf(ficrespijb,"\n#Combination (%d) ignored because no cases \n",k);
13440: continue;
13441: }
13442:
13443: /* for (agedeb=fage; agedeb>=bage; agedeb--){ /\* If stepm=6 months *\/ */
13444: for (agedeb=bage; agedeb<=fage; agedeb++){ /* If stepm=6 months and estepm=24 (2 years) */
13445: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /\* Typically 20 years = 20*12/6=40 *\/ */
13446: nhstepm=(int) rint((agedeb-ageminl)*YEARM/stepm+0.1)-1; /* Typically 20 years = 20*12/6=40 or 55*12/24=27.5-1.1=>27 */
13447: nhstepm = nhstepm/hstepm; /* Typically 40/4=10, because estepm=24 stepm=6 => hstepm=24/6=4 or 28*/
13448:
13449: /* nhstepm=nhstepm*YEARM; aff par mois*/
13450:
13451: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); /* We can't have it at an upper level because of nhstepm */
13452: /* and memory limitations if stepm is small */
13453:
13454: /* oldm=oldms;savm=savms; */
13455: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
13456: hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm, k, nres);/* Bug valgrind */
13457: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm,oldm,savm, dnewm, doldm, dsavm, k); */
13458: fprintf(ficrespijb,"# Cov Agex agex-h hbijx with i,j=");
13459: for(i=1; i<=nlstate;i++)
13460: for(j=1; j<=nlstate+ndeath;j++)
13461: fprintf(ficrespijb," %1d-%1d",i,j);
13462: fprintf(ficrespijb,"\n");
13463: for (h=0; h<=nhstepm; h++){
13464: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
13465: fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb - h*hstepm/YEARM*stepm );
13466: /* fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm ); */
13467: for(i=1; i<=nlstate;i++)
13468: for(j=1; j<=nlstate+ndeath;j++)
13469: fprintf(ficrespijb," %.5f", p3mat[i][j][h]);/* Bug valgrind */
13470: fprintf(ficrespijb,"\n");
13471: }
13472: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
13473: fprintf(ficrespijb,"\n");
13474: } /* end age deb */
13475: /* } /\* end combination *\/ */
13476: } /* end nres */
13477: return 0;
13478: } /* hBijx */
13479:
13480:
13481: /***********************************************/
13482: /**************** Main Program *****************/
13483: /***********************************************/
13484:
13485: int main(int argc, char *argv[])
13486: {
13487: #ifdef GSL
13488: const gsl_multimin_fminimizer_type *T;
13489: size_t iteri = 0, it;
13490: int rval = GSL_CONTINUE;
13491: int status = GSL_SUCCESS;
13492: double ssval;
13493: #endif
13494: int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav);
13495: int i,j, k, iter=0,m,size=100, cptcod; /* Suppressing because nobs */
13496: /* int i,j, k, n=MAXN,iter=0,m,size=100, cptcod; */
13497: int ncvyear=0; /* Number of years needed for the period prevalence to converge */
13498: int jj, ll, li, lj, lk;
13499: int numlinepar=0; /* Current linenumber of parameter file */
13500: int num_filled;
13501: int itimes;
13502: int NDIM=2;
13503: int vpopbased=0;
13504: int nres=0;
13505: int endishere=0;
13506: int noffset=0;
13507: int ncurrv=0; /* Temporary variable */
13508:
13509: char ca[32], cb[32];
13510: /* FILE *fichtm; *//* Html File */
13511: /* FILE *ficgp;*/ /*Gnuplot File */
13512: struct stat info;
13513: double agedeb=0.;
13514:
13515: double ageminpar=AGEOVERFLOW,agemin=AGEOVERFLOW, agemaxpar=-AGEOVERFLOW, agemax=-AGEOVERFLOW;
13516: double ageminout=-AGEOVERFLOW,agemaxout=AGEOVERFLOW; /* Smaller Age range redefined after movingaverage */
13517:
13518: double fret;
13519: double dum=0.; /* Dummy variable */
13520: double ***p3mat;
13521: /* double ***mobaverage; */
13522: double wald;
13523:
13524: char line[MAXLINE], linetmp[MAXLINE];
13525: char path[MAXLINE],pathc[MAXLINE],pathcd[MAXLINE],pathtot[MAXLINE];
13526:
13527: char modeltemp[MAXLINE];
13528: char resultline[MAXLINE], resultlineori[MAXLINE];
13529:
13530: char pathr[MAXLINE], pathimach[MAXLINE];
13531: char *tok, *val; /* pathtot */
13532: /* int firstobs=1, lastobs=10; /\* nobs = lastobs-firstobs declared globally ;*\/ */
13533: int c, h , cpt, c2;
13534: int jl=0;
13535: int i1, j1, jk, stepsize=0;
13536: int count=0;
13537:
13538: int *tab;
13539: int mobilavproj=0 , prevfcast=0 ; /* moving average of prev, If prevfcast=1 prevalence projection */
13540: /* double anprojd, mprojd, jprojd; /\* For eventual projections *\/ */
13541: /* double anprojf, mprojf, jprojf; */
13542: /* double jintmean,mintmean,aintmean; */
13543: int prvforecast = 0; /* Might be 1 (date of beginning of projection is a choice or 2 is the dateintmean */
13544: int prvbackcast = 0; /* Might be 1 (date of beginning of projection is a choice or 2 is the dateintmean */
13545: double yrfproj= 10.0; /* Number of years of forward projections */
13546: double yrbproj= 10.0; /* Number of years of backward projections */
13547: int prevbcast=0; /* defined as global for mlikeli and mle, replacing backcast */
13548: int mobilav=0,popforecast=0;
13549: int hstepm=0, nhstepm=0;
13550: int agemortsup;
13551: float sumlpop=0.;
13552: double jprev1=1, mprev1=1,anprev1=2000,jprev2=1, mprev2=1,anprev2=2000;
13553: double jpyram=1, mpyram=1,anpyram=2000,jpyram1=1, mpyram1=1,anpyram1=2000;
13554:
13555: double bage=0, fage=110., age, agelim=0., agebase=0.;
13556: double ftolpl=FTOL;
13557: double **prlim;
13558: double **bprlim;
13559: double ***param; /* Matrix of parameters, param[i][j][k] param=ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel)
13560: state of origin, state of destination including death, for each covariate: constante, age, and V1 V2 etc. */
13561: double ***paramstart; /* Matrix of starting parameter values */
13562: double *p, *pstart; /* p=param[1][1] pstart is for starting values guessed by freqsummary */
13563: double **matcov; /* Matrix of covariance */
13564: double **hess; /* Hessian matrix */
13565: double ***delti3; /* Scale */
13566: double *delti; /* Scale */
13567: double ***eij, ***vareij;
13568: double **varpl; /* Variances of prevalence limits by age */
13569:
13570: double *epj, vepp;
13571:
13572: double dateprev1, dateprev2;
13573: double jproj1=1,mproj1=1,anproj1=2000,jproj2=1,mproj2=1,anproj2=2000, dateproj1=0, dateproj2=0, dateprojd=0, dateprojf=0;
13574: double jback1=1,mback1=1,anback1=2000,jback2=1,mback2=1,anback2=2000, dateback1=0, dateback2=0, datebackd=0, datebackf=0;
13575:
13576:
13577: double **ximort;
13578: char *alph[]={"a","a","b","c","d","e"}, str[4]="1234";
13579: int *dcwave;
13580:
13581: char z[1]="c";
13582:
13583: /*char *strt;*/
13584: char strtend[80];
13585:
13586:
13587: /* setlocale (LC_ALL, ""); */
13588: /* bindtextdomain (PACKAGE, LOCALEDIR); */
13589: /* textdomain (PACKAGE); */
13590: /* setlocale (LC_CTYPE, ""); */
13591: /* setlocale (LC_MESSAGES, ""); */
13592:
13593: /* gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */
13594: rstart_time = time(NULL);
13595: /* (void) gettimeofday(&start_time,&tzp);*/
13596: start_time = *localtime(&rstart_time);
13597: curr_time=start_time;
13598: /*tml = *localtime(&start_time.tm_sec);*/
13599: /* strcpy(strstart,asctime(&tml)); */
13600: strcpy(strstart,asctime(&start_time));
13601:
13602: /* printf("Localtime (at start)=%s",strstart); */
13603: /* tp.tm_sec = tp.tm_sec +86400; */
13604: /* tm = *localtime(&start_time.tm_sec); */
13605: /* tmg.tm_year=tmg.tm_year +dsign*dyear; */
13606: /* tmg.tm_mon=tmg.tm_mon +dsign*dmonth; */
13607: /* tmg.tm_hour=tmg.tm_hour + 1; */
13608: /* tp.tm_sec = mktime(&tmg); */
13609: /* strt=asctime(&tmg); */
13610: /* printf("Time(after) =%s",strstart); */
13611: /* (void) time (&time_value);
13612: * printf("time=%d,t-=%d\n",time_value,time_value-86400);
13613: * tm = *localtime(&time_value);
13614: * strstart=asctime(&tm);
13615: * printf("tim_value=%d,asctime=%s\n",time_value,strstart);
13616: */
13617:
13618: nberr=0; /* Number of errors and warnings */
13619: nbwarn=0;
13620: #ifdef WIN32
13621: _getcwd(pathcd, size);
13622: #else
13623: getcwd(pathcd, size);
13624: #endif
13625: syscompilerinfo(0);
13626: printf("\nIMaCh prax version minfit %s, %s\n%s",version, copyright, fullversion);
13627: if(argc <=1){
13628: printf("\nEnter the parameter file name: ");
13629: if(!fgets(pathr,FILENAMELENGTH,stdin)){
13630: printf("ERROR Empty parameter file name\n");
13631: goto end;
13632: }
13633: i=strlen(pathr);
13634: if(pathr[i-1]=='\n')
13635: pathr[i-1]='\0';
13636: i=strlen(pathr);
13637: if(i >= 1 && pathr[i-1]==' ') {/* This may happen when dragging on oS/X! */
13638: pathr[i-1]='\0';
13639: }
13640: i=strlen(pathr);
13641: if( i==0 ){
13642: printf("ERROR Empty parameter file name\n");
13643: goto end;
13644: }
13645: for (tok = pathr; tok != NULL; ){
13646: printf("Pathr |%s|\n",pathr);
13647: while ((val = strsep(&tok, "\"" )) != NULL && *val == '\0');
13648: printf("val= |%s| pathr=%s\n",val,pathr);
13649: strcpy (pathtot, val);
13650: if(pathr[0] == '\0') break; /* Dirty */
13651: }
13652: }
13653: else if (argc<=2){
13654: strcpy(pathtot,argv[1]);
13655: }
13656: else{
13657: strcpy(pathtot,argv[1]);
13658: strcpy(z,argv[2]);
13659: printf("\nargv[2]=%s z=%c\n",argv[2],z[0]);
13660: }
13661: /*if(getcwd(pathcd, MAXLINE)!= NULL)printf ("Error pathcd\n");*/
13662: /*cygwin_split_path(pathtot,path,optionfile);
13663: printf("pathtot=%s, path=%s, optionfile=%s\n",pathtot,path,optionfile);*/
13664: /* cutv(path,optionfile,pathtot,'\\');*/
13665:
13666: /* Split argv[0], imach program to get pathimach */
13667: printf("\nargv[0]=%s argv[1]=%s, \n",argv[0],argv[1]);
13668: split(argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
13669: printf("\nargv[0]=%s pathimach=%s, \noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
13670: /* strcpy(pathimach,argv[0]); */
13671: /* Split argv[1]=pathtot, parameter file name to get path, optionfile, extension and name */
13672: split(pathtot,path,optionfile,optionfilext,optionfilefiname);
13673: printf("\npathtot=%s,\npath=%s,\noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname);
13674: #ifdef WIN32
13675: _chdir(path); /* Can be a relative path */
13676: if(_getcwd(pathcd,MAXLINE) > 0) /* So pathcd is the full path */
13677: #else
13678: chdir(path); /* Can be a relative path */
13679: if (getcwd(pathcd, MAXLINE) > 0) /* So pathcd is the full path */
13680: #endif
13681: printf("Current directory %s!\n",pathcd);
13682: strcpy(command,"mkdir ");
13683: strcat(command,optionfilefiname);
13684: if((outcmd=system(command)) != 0){
13685: printf("Directory already exists (or can't create it) %s%s, err=%d\n",path,optionfilefiname,outcmd);
13686: /* fprintf(ficlog,"Problem creating directory %s%s\n",path,optionfilefiname); */
13687: /* fclose(ficlog); */
13688: /* exit(1); */
13689: }
13690: /* if((imk=mkdir(optionfilefiname))<0){ */
13691: /* perror("mkdir"); */
13692: /* } */
13693:
13694: /*-------- arguments in the command line --------*/
13695:
13696: /* Main Log file */
13697: strcat(filelog, optionfilefiname);
13698: strcat(filelog,".log"); /* */
13699: if((ficlog=fopen(filelog,"w"))==NULL) {
13700: printf("Problem with logfile %s\n",filelog);
13701: goto end;
13702: }
13703: fprintf(ficlog,"Log filename:%s\n",filelog);
13704: fprintf(ficlog,"Version %s %s",version,fullversion);
13705: fprintf(ficlog,"\nEnter the parameter file name: \n");
13706: fprintf(ficlog,"pathimach=%s\npathtot=%s\n\
13707: path=%s \n\
13708: optionfile=%s\n\
13709: optionfilext=%s\n\
13710: optionfilefiname='%s'\n",pathimach,pathtot,path,optionfile,optionfilext,optionfilefiname);
13711:
13712: syscompilerinfo(1);
13713:
13714: printf("Local time (at start):%s",strstart);
13715: fprintf(ficlog,"Local time (at start): %s",strstart);
13716: fflush(ficlog);
13717: /* (void) gettimeofday(&curr_time,&tzp); */
13718: /* printf("Elapsed time %d\n", asc_diff_time(curr_time.tm_sec-start_time.tm_sec,tmpout)); */
13719:
13720: /* */
13721: strcpy(fileres,"r");
13722: strcat(fileres, optionfilefiname);
13723: strcat(fileresu, optionfilefiname); /* Without r in front */
13724: strcat(fileres,".txt"); /* Other files have txt extension */
13725: strcat(fileresu,".txt"); /* Other files have txt extension */
13726:
13727: /* Main ---------arguments file --------*/
13728:
13729: if((ficpar=fopen(optionfile,"r"))==NULL) {
13730: printf("Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
13731: fprintf(ficlog,"Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
13732: fflush(ficlog);
13733: /* goto end; */
13734: exit(70);
13735: }
13736:
13737: strcpy(filereso,"o");
13738: strcat(filereso,fileresu);
13739: if((ficparo=fopen(filereso,"w"))==NULL) { /* opened on subdirectory */
13740: printf("Problem with Output resultfile: %s\n", filereso);
13741: fprintf(ficlog,"Problem with Output resultfile: %s\n", filereso);
13742: fflush(ficlog);
13743: goto end;
13744: }
13745: /*-------- Rewriting parameter file ----------*/
13746: strcpy(rfileres,"r"); /* "Rparameterfile */
13747: strcat(rfileres,optionfilefiname); /* Parameter file first name */
13748: strcat(rfileres,"."); /* */
13749: strcat(rfileres,optionfilext); /* Other files have txt extension */
13750: if((ficres =fopen(rfileres,"w"))==NULL) {
13751: printf("Problem writing new parameter file: %s\n", rfileres);goto end;
13752: fprintf(ficlog,"Problem writing new parameter file: %s\n", rfileres);goto end;
13753: fflush(ficlog);
13754: goto end;
13755: }
13756: fprintf(ficres,"#IMaCh %s\n",version);
13757:
13758:
13759: /* Reads comments: lines beginning with '#' */
13760: numlinepar=0;
13761: /* Is it a BOM UTF-8 Windows file? */
13762: /* First parameter line */
13763: while(fgets(line, MAXLINE, ficpar)) {
13764: noffset=0;
13765: if( line[0] == (char)0xEF && line[1] == (char)0xBB) /* EF BB BF */
13766: {
13767: noffset=noffset+3;
13768: printf("# File is an UTF8 Bom.\n"); // 0xBF
13769: }
13770: /* else if( line[0] == (char)0xFE && line[1] == (char)0xFF)*/
13771: else if( line[0] == (char)0xFF && line[1] == (char)0xFE)
13772: {
13773: noffset=noffset+2;
13774: printf("# File is an UTF16BE BOM file\n");
13775: }
13776: else if( line[0] == 0 && line[1] == 0)
13777: {
13778: if( line[2] == (char)0xFE && line[3] == (char)0xFF){
13779: noffset=noffset+4;
13780: printf("# File is an UTF16BE BOM file\n");
13781: }
13782: } else{
13783: ;/*printf(" Not a BOM file\n");*/
13784: }
13785:
13786: /* If line starts with a # it is a comment */
13787: if (line[noffset] == '#') {
13788: numlinepar++;
13789: fputs(line,stdout);
13790: fputs(line,ficparo);
13791: fputs(line,ficres);
13792: fputs(line,ficlog);
13793: continue;
13794: }else
13795: break;
13796: }
13797: if((num_filled=sscanf(line,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", \
13798: title, datafile, &lastobs, &firstpass,&lastpass)) !=EOF){
13799: if (num_filled != 5) {
13800: printf("Should be 5 parameters\n");
13801: fprintf(ficlog,"Should be 5 parameters\n");
13802: }
13803: numlinepar++;
13804: printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
13805: fprintf(ficparo,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
13806: fprintf(ficres,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
13807: fprintf(ficlog,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
13808: }
13809: /* Second parameter line */
13810: while(fgets(line, MAXLINE, ficpar)) {
13811: /* while(fscanf(ficpar,"%[^\n]", line)) { */
13812: /* If line starts with a # it is a comment. Strangely fgets reads the EOL and fputs doesn't */
13813: if (line[0] == '#') {
13814: numlinepar++;
13815: printf("%s",line);
13816: fprintf(ficres,"%s",line);
13817: fprintf(ficparo,"%s",line);
13818: fprintf(ficlog,"%s",line);
13819: continue;
13820: }else
13821: break;
13822: }
13823: 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", \
13824: &ftol, &stepm, &ncovcol, &nqv, &ntv, &nqtv, &nlstate, &ndeath, &maxwav, &mle, &weightopt)) !=EOF){
13825: if (num_filled != 11) {
13826: 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");
13827: printf("but line=%s\n",line);
13828: fprintf(ficlog,"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");
13829: fprintf(ficlog,"but line=%s\n",line);
13830: }
13831: if( lastpass > maxwav){
13832: printf("Error (lastpass = %d) > (maxwav = %d)\n",lastpass, maxwav);
13833: fprintf(ficlog,"Error (lastpass = %d) > (maxwav = %d)\n",lastpass, maxwav);
13834: fflush(ficlog);
13835: goto end;
13836: }
13837: 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);
13838: fprintf(ficparo,"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);
13839: fprintf(ficres,"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, 0, weightopt);
13840: fprintf(ficlog,"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);
13841: }
13842: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
13843: /*ftolpl=6.e-4; *//* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
13844: /* Third parameter line */
13845: while(fgets(line, MAXLINE, ficpar)) {
13846: /* If line starts with a # it is a comment */
13847: if (line[0] == '#') {
13848: numlinepar++;
13849: printf("%s",line);
13850: fprintf(ficres,"%s",line);
13851: fprintf(ficparo,"%s",line);
13852: fprintf(ficlog,"%s",line);
13853: continue;
13854: }else
13855: break;
13856: }
13857: if((num_filled=sscanf(line,"model=%[^.\n]", model)) !=EOF){ /* Every character after model but dot and return */
13858: if (num_filled != 1){
13859: printf("ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line);
13860: fprintf(ficlog,"ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line);
13861: model[0]='\0';
13862: goto end;
13863: }else{
13864: trimbtab(linetmp,line); /* Trims multiple blanks in line */
13865: strcpy(line, linetmp);
13866: }
13867: }
13868: if((num_filled=sscanf(line,"model=1+age%[^.\n]", model)) !=EOF){ /* Every character after 1+age but dot and return */
13869: if (num_filled != 1){
13870: printf("ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line);
13871: fprintf(ficlog,"ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line);
13872: model[0]='\0';
13873: goto end;
13874: }
13875: else{
13876: if (model[0]=='+'){
13877: for(i=1; i<=strlen(model);i++)
13878: modeltemp[i-1]=model[i];
13879: strcpy(model,modeltemp);
13880: }
13881: }
13882: /* printf(" model=1+age%s modeltemp= %s, model=1+age+%s\n",model, modeltemp, model);fflush(stdout); */
13883: printf("model=1+age+%s\n",model);fflush(stdout);
13884: fprintf(ficparo,"model=1+age+%s\n",model);fflush(stdout);
13885: fprintf(ficres,"model=1+age+%s\n",model);fflush(stdout);
13886: fprintf(ficlog,"model=1+age+%s\n",model);fflush(stdout);
13887: }
13888: /* 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); */
13889: /* numlinepar=numlinepar+3; /\* In general *\/ */
13890: /* 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); */
13891: /* 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); */
13892: /* 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); */
13893: fflush(ficlog);
13894: /* if(model[0]=='#'|| model[0]== '\0'){ */
13895: if(model[0]=='#'){
13896: printf("Error in 'model' line: model should start with 'model=1+age+' and end without space \n \
13897: 'model=1+age+' or 'model=1+age+V1.' or 'model=1+age+age*age+V1+V1*age' or \n \
13898: 'model=1+age+V1+V2' or 'model=1+age+V1+V2+V1*V2' etc. \n"); \
13899: if(mle != -1){
13900: printf("Fix the model line and run imach with mle=-1 to get a correct template of the parameter vectors and subdiagonal covariance matrix.\n");
13901: exit(1);
13902: }
13903: }
13904: while((c=getc(ficpar))=='#' && c!= EOF){
13905: ungetc(c,ficpar);
13906: fgets(line, MAXLINE, ficpar);
13907: numlinepar++;
13908: if(line[1]=='q'){ /* This #q will quit imach (the answer is q) */
13909: z[0]=line[1];
13910: }else if(line[1]=='d'){ /* For debugging individual values of covariates in ficresilk */
13911: debugILK=1;printf("DebugILK\n");
13912: }
13913: /* printf("****line [1] = %c \n",line[1]); */
13914: fputs(line, stdout);
13915: //puts(line);
13916: fputs(line,ficparo);
13917: fputs(line,ficlog);
13918: }
13919: ungetc(c,ficpar);
13920:
13921:
13922: covar=matrix(0,NCOVMAX,firstobs,lastobs); /**< used in readdata */
13923: if(nqv>=1)coqvar=matrix(1,nqv,firstobs,lastobs); /**< Fixed quantitative covariate */
13924: if(nqtv>=1)cotqvar=ma3x(1,maxwav,1,nqtv,firstobs,lastobs); /**< Time varying quantitative covariate */
13925: /* if(ntv+nqtv>=1)cotvar=ma3x(1,maxwav,1,ntv+nqtv,firstobs,lastobs); /\**< Time varying covariate (dummy and quantitative)*\/ */
13926: if(ntv+nqtv>=1)cotvar=ma3x(1,maxwav,ncovcol+nqv+1,ncovcol+nqv+ntv+nqtv,firstobs,lastobs); /**< Might be better */
13927: cptcovn=0; /*Number of covariates, i.e. number of '+' in model statement plus one, indepently of n in Vn*/
13928: /* v1+v2+v3+v2*v4+v5*age makes cptcovn = 5
13929: v1+v2*age+v2*v3 makes cptcovn = 3
13930: */
13931: if (strlen(model)>1)
13932: 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*/
13933: else
13934: ncovmodel=2; /* Constant and age */
13935: nforce= (nlstate+ndeath-1)*nlstate; /* Number of forces ij from state i to j */
13936: npar= nforce*ncovmodel; /* Number of parameters like aij*/
13937: if(npar >MAXPARM || nlstate >NLSTATEMAX || ndeath >NDEATHMAX || ncovmodel>NCOVMAX){
13938: 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);
13939: 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);
13940: fflush(stdout);
13941: fclose (ficlog);
13942: goto end;
13943: }
13944: delti3= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
13945: delti=delti3[1][1];
13946: /*delti=vector(1,npar); *//* Scale of each paramater (output from hesscov)*/
13947: if(mle==-1){ /* Print a wizard for help writing covariance matrix */
13948: /* We could also provide initial parameters values giving by simple logistic regression
13949: * only one way, that is without matrix product. We will have nlstate maximizations */
13950: /* for(i=1;i<nlstate;i++){ */
13951: /* /\*reducing xi for 1 to npar to 1 to ncovmodel; *\/ */
13952: /* mlikeli(ficres,p, ncovmodel, ncovmodel, nlstate, ftol, funcnoprod); */
13953: /* } */
13954: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
13955: printf(" You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
13956: fprintf(ficlog," You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
13957: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
13958: fclose (ficparo);
13959: fclose (ficlog);
13960: goto end;
13961: exit(0);
13962: } else if(mle==-5) { /* Main Wizard */
13963: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
13964: printf(" You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
13965: fprintf(ficlog," You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
13966: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
13967: matcov=matrix(1,npar,1,npar);
13968: hess=matrix(1,npar,1,npar);
13969: } else{ /* Begin of mle != -1 or -5 */
13970: /* Read guessed parameters */
13971: /* Reads comments: lines beginning with '#' */
13972: while((c=getc(ficpar))=='#' && c!= EOF){
13973: ungetc(c,ficpar);
13974: fgets(line, MAXLINE, ficpar);
13975: numlinepar++;
13976: fputs(line,stdout);
13977: fputs(line,ficparo);
13978: fputs(line,ficlog);
13979: }
13980: ungetc(c,ficpar);
13981:
13982: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
13983: paramstart= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
13984: for(i=1; i <=nlstate; i++){
13985: j=0;
13986: for(jj=1; jj <=nlstate+ndeath; jj++){
13987: if(jj==i) continue;
13988: j++;
13989: while((c=getc(ficpar))=='#' && c!= EOF){
13990: ungetc(c,ficpar);
13991: fgets(line, MAXLINE, ficpar);
13992: numlinepar++;
13993: fputs(line,stdout);
13994: fputs(line,ficparo);
13995: fputs(line,ficlog);
13996: }
13997: ungetc(c,ficpar);
13998: fscanf(ficpar,"%1d%1d",&i1,&j1);
13999: if ((i1 != i) || (j1 != jj)){
14000: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n \
14001: It might be a problem of design; if ncovcol and the model are correct\n \
14002: run imach with mle=-1 to get a correct template of the parameter file.\n",numlinepar, i,j, i1, j1);
14003: exit(1);
14004: }
14005: fprintf(ficparo,"%1d%1d",i1,j1);
14006: if(mle==1)
14007: printf("%1d%1d",i,jj);
14008: fprintf(ficlog,"%1d%1d",i,jj);
14009: for(k=1; k<=ncovmodel;k++){
14010: fscanf(ficpar," %lf",¶m[i][j][k]);
14011: if(mle==1){
14012: printf(" %lf",param[i][j][k]);
14013: fprintf(ficlog," %lf",param[i][j][k]);
14014: }
14015: else
14016: fprintf(ficlog," %lf",param[i][j][k]);
14017: fprintf(ficparo," %lf",param[i][j][k]);
14018: }
14019: fscanf(ficpar,"\n");
14020: numlinepar++;
14021: if(mle==1)
14022: printf("\n");
14023: fprintf(ficlog,"\n");
14024: fprintf(ficparo,"\n");
14025: }
14026: }
14027: fflush(ficlog);
14028:
14029: /* Reads parameters values */
14030: p=param[1][1];
14031: pstart=paramstart[1][1];
14032:
14033: /* Reads comments: lines beginning with '#' */
14034: while((c=getc(ficpar))=='#' && c!= EOF){
14035: ungetc(c,ficpar);
14036: fgets(line, MAXLINE, ficpar);
14037: numlinepar++;
14038: fputs(line,stdout);
14039: fputs(line,ficparo);
14040: fputs(line,ficlog);
14041: }
14042: ungetc(c,ficpar);
14043:
14044: for(i=1; i <=nlstate; i++){
14045: for(j=1; j <=nlstate+ndeath-1; j++){
14046: fscanf(ficpar,"%1d%1d",&i1,&j1);
14047: if ( (i1-i) * (j1-j) != 0){
14048: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n",numlinepar, i,j, i1, j1);
14049: exit(1);
14050: }
14051: printf("%1d%1d",i,j);
14052: fprintf(ficparo,"%1d%1d",i1,j1);
14053: fprintf(ficlog,"%1d%1d",i1,j1);
14054: for(k=1; k<=ncovmodel;k++){
14055: fscanf(ficpar,"%le",&delti3[i][j][k]);
14056: printf(" %le",delti3[i][j][k]);
14057: fprintf(ficparo," %le",delti3[i][j][k]);
14058: fprintf(ficlog," %le",delti3[i][j][k]);
14059: }
14060: fscanf(ficpar,"\n");
14061: numlinepar++;
14062: printf("\n");
14063: fprintf(ficparo,"\n");
14064: fprintf(ficlog,"\n");
14065: }
14066: }
14067: fflush(ficlog);
14068:
14069: /* Reads covariance matrix */
14070: delti=delti3[1][1];
14071:
14072:
14073: /* free_ma3x(delti3,1,nlstate,1,nlstate+ndeath-1,1,ncovmodel); */ /* Hasn't to to freed here otherwise delti is no more allocated */
14074:
14075: /* Reads comments: lines beginning with '#' */
14076: while((c=getc(ficpar))=='#' && c!= EOF){
14077: ungetc(c,ficpar);
14078: fgets(line, MAXLINE, ficpar);
14079: numlinepar++;
14080: fputs(line,stdout);
14081: fputs(line,ficparo);
14082: fputs(line,ficlog);
14083: }
14084: ungetc(c,ficpar);
14085:
14086: matcov=matrix(1,npar,1,npar);
14087: hess=matrix(1,npar,1,npar);
14088: for(i=1; i <=npar; i++)
14089: for(j=1; j <=npar; j++) matcov[i][j]=0.;
14090:
14091: /* Scans npar lines */
14092: for(i=1; i <=npar; i++){
14093: count=fscanf(ficpar,"%1d%1d%d",&i1,&j1,&jk);
14094: if(count != 3){
14095: printf("Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
14096: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
14097: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
14098: fprintf(ficlog,"Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
14099: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
14100: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
14101: exit(1);
14102: }else{
14103: if(mle==1)
14104: printf("%1d%1d%d",i1,j1,jk);
14105: }
14106: fprintf(ficlog,"%1d%1d%d",i1,j1,jk);
14107: fprintf(ficparo,"%1d%1d%d",i1,j1,jk);
14108: for(j=1; j <=i; j++){
14109: fscanf(ficpar," %le",&matcov[i][j]);
14110: if(mle==1){
14111: printf(" %.5le",matcov[i][j]);
14112: }
14113: fprintf(ficlog," %.5le",matcov[i][j]);
14114: fprintf(ficparo," %.5le",matcov[i][j]);
14115: }
14116: fscanf(ficpar,"\n");
14117: numlinepar++;
14118: if(mle==1)
14119: printf("\n");
14120: fprintf(ficlog,"\n");
14121: fprintf(ficparo,"\n");
14122: }
14123: /* End of read covariance matrix npar lines */
14124: for(i=1; i <=npar; i++)
14125: for(j=i+1;j<=npar;j++)
14126: matcov[i][j]=matcov[j][i];
14127:
14128: if(mle==1)
14129: printf("\n");
14130: fprintf(ficlog,"\n");
14131:
14132: fflush(ficlog);
14133:
14134: } /* End of mle != -3 */
14135:
14136: /* Main data
14137: */
14138: nobs=lastobs-firstobs+1; /* was = lastobs;*/
14139: /* num=lvector(1,n); */
14140: /* moisnais=vector(1,n); */
14141: /* annais=vector(1,n); */
14142: /* moisdc=vector(1,n); */
14143: /* andc=vector(1,n); */
14144: /* weight=vector(1,n); */
14145: /* agedc=vector(1,n); */
14146: /* cod=ivector(1,n); */
14147: /* for(i=1;i<=n;i++){ */
14148: num=lvector(firstobs,lastobs);
14149: moisnais=vector(firstobs,lastobs);
14150: annais=vector(firstobs,lastobs);
14151: moisdc=vector(firstobs,lastobs);
14152: andc=vector(firstobs,lastobs);
14153: weight=vector(firstobs,lastobs);
14154: agedc=vector(firstobs,lastobs);
14155: cod=ivector(firstobs,lastobs);
14156: for(i=firstobs;i<=lastobs;i++){
14157: num[i]=0;
14158: moisnais[i]=0;
14159: annais[i]=0;
14160: moisdc[i]=0;
14161: andc[i]=0;
14162: agedc[i]=0;
14163: cod[i]=0;
14164: weight[i]=1.0; /* Equal weights, 1 by default */
14165: }
14166: mint=matrix(1,maxwav,firstobs,lastobs);
14167: anint=matrix(1,maxwav,firstobs,lastobs);
14168: s=imatrix(1,maxwav+1,firstobs,lastobs); /* s[i][j] health state for wave i and individual j */
14169: /* printf("BUG ncovmodel=%d NCOVMAX=%d 2**ncovmodel=%f BUG\n",ncovmodel,NCOVMAX,pow(2,ncovmodel)); */
14170: tab=ivector(1,NCOVMAX);
14171: ncodemax=ivector(1,NCOVMAX); /* Number of code per covariate; if O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */
14172: ncodemaxwundef=ivector(1,NCOVMAX); /* Number of code per covariate; if - 1 O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */
14173:
14174: /* Reads data from file datafile */
14175: if (readdata(datafile, firstobs, lastobs, &imx)==1)
14176: goto end;
14177:
14178: /* Calculation of the number of parameters from char model */
14179: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4
14180: k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tag[cptcovage=1]=4
14181: k=3 V4 Tvar[k=3]= 4 (from V4)
14182: k=2 V1 Tvar[k=2]= 1 (from V1)
14183: k=1 Tvar[1]=2 (from V2)
14184: */
14185:
14186: Tvar=ivector(1,NCOVMAX); /* Was 15 changed to NCOVMAX. */
14187: TvarsDind=ivector(1,NCOVMAX); /* */
14188: TnsdVar=ivector(1,NCOVMAX); /* */
14189: /* for(i=1; i<=NCOVMAX;i++) TnsdVar[i]=3; */
14190: TvarsD=ivector(1,NCOVMAX); /* */
14191: TvarsQind=ivector(1,NCOVMAX); /* */
14192: TvarsQ=ivector(1,NCOVMAX); /* */
14193: TvarF=ivector(1,NCOVMAX); /* */
14194: TvarFind=ivector(1,NCOVMAX); /* */
14195: TvarV=ivector(1,NCOVMAX); /* */
14196: TvarVind=ivector(1,NCOVMAX); /* */
14197: TvarA=ivector(1,NCOVMAX); /* */
14198: TvarAind=ivector(1,NCOVMAX); /* */
14199: TvarFD=ivector(1,NCOVMAX); /* */
14200: TvarFDind=ivector(1,NCOVMAX); /* */
14201: TvarFQ=ivector(1,NCOVMAX); /* */
14202: TvarFQind=ivector(1,NCOVMAX); /* */
14203: TvarVD=ivector(1,NCOVMAX); /* */
14204: TvarVDind=ivector(1,NCOVMAX); /* */
14205: TvarVQ=ivector(1,NCOVMAX); /* */
14206: TvarVQind=ivector(1,NCOVMAX); /* */
14207: TvarVV=ivector(1,NCOVMAX); /* */
14208: TvarVVind=ivector(1,NCOVMAX); /* */
14209: TvarVVA=ivector(1,NCOVMAX); /* */
14210: TvarVVAind=ivector(1,NCOVMAX); /* */
14211: TvarAVVA=ivector(1,NCOVMAX); /* */
14212: TvarAVVAind=ivector(1,NCOVMAX); /* */
14213:
14214: Tvalsel=vector(1,NCOVMAX); /* */
14215: Tvarsel=ivector(1,NCOVMAX); /* */
14216: Typevar=ivector(-1,NCOVMAX); /* -1 to 2 */
14217: Fixed=ivector(-1,NCOVMAX); /* -1 to 3 */
14218: Dummy=ivector(-1,NCOVMAX); /* -1 to 3 */
14219: DummyV=ivector(-1,NCOVMAX); /* 1 to 3 */
14220: FixedV=ivector(-1,NCOVMAX); /* 1 to 3 */
14221:
14222: /* V2+V1+V4+age*V3 is a model with 4 covariates (3 plus signs).
14223: For each model-covariate stores the data-covariate id. Tvar[1]=2, Tvar[2]=1, Tvar[3]=4,
14224: Tvar[4=age*V3] is 3 and 'age' is recorded in Tage.
14225: */
14226: /* For model-covariate k tells which data-covariate to use but
14227: because this model-covariate is a construction we invent a new column
14228: ncovcol + k1
14229: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3
14230: Tvar[3=V1*V4]=4+1 etc */
14231: Tprod=ivector(1,NCOVMAX); /* Gives the k position of the k1 product */
14232: Tposprod=ivector(1,NCOVMAX); /* Gives the k1 product from the k position */
14233: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3
14234: if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2)
14235: Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5]=2
14236: */
14237: Tvaraff=ivector(1,NCOVMAX); /* Unclear */
14238: 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
14239: * For V3*V2 (in V2+V1+V1*V4+age*V3+V3*V2), V3*V2 position is 2nd.
14240: * Tvard[k1=2][1]=3 (V3) Tvard[k1=2][2]=2(V2) */
14241: Tvardk=imatrix(0,NCOVMAX,1,2);
14242: Tage=ivector(1,NCOVMAX); /* Gives the covariate id of covariates associated with age: V2 + V1 + age*V4 + V3*age
14243: 4 covariates (3 plus signs)
14244: Tage[1=V3*age]= 4; Tage[2=age*V4] = 3
14245: */
14246: for(i=1;i<NCOVMAX;i++)
14247: Tage[i]=0;
14248: Tmodelind=ivector(1,NCOVMAX);/** gives the k model position of an
14249: * individual dummy, fixed or varying:
14250: * Tmodelind[Tvaraff[3]]=9,Tvaraff[1]@9={4,
14251: * 3, 1, 0, 0, 0, 0, 0, 0},
14252: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 ,
14253: * V1 df, V2 qf, V3 & V4 dv, V5 qv
14254: * Tmodelind[1]@9={9,0,3,2,}*/
14255: TmodelInvind=ivector(1,NCOVMAX); /* TmodelInvind=Tvar[k]- ncovcol-nqv={5-2-1=2,*/
14256: TmodelInvQind=ivector(1,NCOVMAX);/** gives the k model position of an
14257: * individual quantitative, fixed or varying:
14258: * Tmodelqind[1]=1,Tvaraff[1]@9={4,
14259: * 3, 1, 0, 0, 0, 0, 0, 0},
14260: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1*/
14261:
14262: /* Probably useless zeroes */
14263: for(i=1;i<NCOVMAX;i++){
14264: DummyV[i]=0;
14265: FixedV[i]=0;
14266: }
14267:
14268: for(i=1; i <=ncovcol;i++){
14269: DummyV[i]=0;
14270: FixedV[i]=0;
14271: }
14272: for(i=ncovcol+1; i <=ncovcol+nqv;i++){
14273: DummyV[i]=1;
14274: FixedV[i]=0;
14275: }
14276: for(i=ncovcol+nqv+1; i <=ncovcol+nqv+ntv;i++){
14277: DummyV[i]=0;
14278: FixedV[i]=1;
14279: }
14280: for(i=ncovcol+nqv+ntv+1; i <=ncovcol+nqv+ntv+nqtv;i++){
14281: DummyV[i]=1;
14282: FixedV[i]=1;
14283: }
14284: for(i=1; i <=ncovcol+nqv+ntv+nqtv;i++){
14285: printf("Covariate type in the data: V%d, DummyV(V%d)=%d, FixedV(V%d)=%d\n",i,i,DummyV[i],i,FixedV[i]);
14286: fprintf(ficlog,"Covariate type in the data: V%d, DummyV(V%d)=%d, FixedV(V%d)=%d\n",i,i,DummyV[i],i,FixedV[i]);
14287: }
14288:
14289:
14290:
14291: /* Main decodemodel */
14292:
14293:
14294: if(decodemodel(model, lastobs) == 1) /* In order to get Tvar[k] V4+V3+V5 p Tvar[1]@3 = {4, 3, 5}*/
14295: goto end;
14296:
14297: if((double)(lastobs-imx)/(double)imx > 1.10){
14298: nbwarn++;
14299: 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);
14300: 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);
14301: }
14302: /* if(mle==1){*/
14303: if (weightopt != 1) { /* Maximisation without weights. We can have weights different from 1 but want no weight*/
14304: for(i=1;i<=imx;i++) weight[i]=1.0; /* changed to imx */
14305: }
14306:
14307: /*-calculation of age at interview from date of interview and age at death -*/
14308: agev=matrix(1,maxwav,1,imx);
14309:
14310: if(calandcheckages(imx, maxwav, &agemin, &agemax, &nberr, &nbwarn) == 1)
14311: goto end;
14312:
14313:
14314: agegomp=(int)agemin;
14315: free_vector(moisnais,firstobs,lastobs);
14316: free_vector(annais,firstobs,lastobs);
14317: /* free_matrix(mint,1,maxwav,1,n);
14318: free_matrix(anint,1,maxwav,1,n);*/
14319: /* free_vector(moisdc,1,n); */
14320: /* free_vector(andc,1,n); */
14321: /* */
14322:
14323: wav=ivector(1,imx);
14324: /* dh=imatrix(1,lastpass-firstpass+1,1,imx); */
14325: /* bh=imatrix(1,lastpass-firstpass+1,1,imx); */
14326: /* mw=imatrix(1,lastpass-firstpass+1,1,imx); */
14327: 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.*/
14328: bh=imatrix(1,lastpass-firstpass+2,1,imx);
14329: mw=imatrix(1,lastpass-firstpass+2,1,imx);
14330:
14331: /* Concatenates waves */
14332: /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
14333: Death is a valid wave (if date is known).
14334: mw[mi][i] is the number of (mi=1 to wav[i]) effective wave out of mi of individual i
14335: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
14336: and mw[mi+1][i]. dh depends on stepm.
14337: */
14338:
14339: concatwav(wav, dh, bh, mw, s, agedc, agev, firstpass, lastpass, imx, nlstate, stepm);
14340: /* Concatenates waves */
14341:
14342: free_vector(moisdc,firstobs,lastobs);
14343: free_vector(andc,firstobs,lastobs);
14344:
14345: /* Routine tricode is to calculate cptcoveff (real number of unique covariates) and to associate covariable number and modality */
14346: nbcode=imatrix(0,NCOVMAX,0,NCOVMAX);
14347: ncodemax[1]=1;
14348: Ndum =ivector(-1,NCOVMAX);
14349: cptcoveff=0;
14350: if (ncovmodel-nagesqr > 2 ){ /* That is if covariate other than cst, age and age*age */
14351: tricode(&cptcoveff,Tvar,nbcode,imx, Ndum); /**< Fills nbcode[Tvar[j]][l]; as well as calculate cptcoveff or number of total effective dummy covariates*/
14352: }
14353:
14354: ncovcombmax=pow(2,cptcoveff);
14355: invalidvarcomb=ivector(0, ncovcombmax);
14356: for(i=0;i<ncovcombmax;i++)
14357: invalidvarcomb[i]=0;
14358:
14359: /* Nbcode gives the value of the lth modality (currently 1 to 2) of jth covariate, in
14360: V2+V1*age, there are 3 covariates Tvar[2]=1 (V1).*/
14361: /* 1 to ncodemax[j] which is the maximum value of this jth covariate */
14362:
14363: /* codtab=imatrix(1,100,1,10);*/ /* codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) */
14364: /*printf(" codtab[1,1],codtab[100,10]=%d,%d\n", codtab[1][1],codtabm(100,10));*/
14365: /* codtab gives the value 1 or 2 of the hth combination of k covariates (1 or 2).*/
14366: /* nbcode[Tvaraff[j]][codtabm(h,j)]) : if there are only 2 modalities for a covariate j,
14367: * codtabm(h,j) gives its value classified at position h and nbcode gives how it is coded
14368: * (currently 0 or 1) in the data.
14369: * In a loop on h=1 to 2**k, and a loop on j (=1 to k), we get the value of
14370: * corresponding modality (h,j).
14371: */
14372:
14373: h=0;
14374: /*if (cptcovn > 0) */
14375: m=pow(2,cptcoveff);
14376:
14377: /**< codtab(h,k) k = codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) + 1
14378: * For k=4 covariates, h goes from 1 to m=2**k
14379: * codtabm(h,k)= (1 & (h-1) >> (k-1)) + 1;
14380: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
14381: * h\k 1 2 3 4 * h-1\k-1 4 3 2 1
14382: *______________________________ *______________________
14383: * 1 i=1 1 i=1 1 i=1 1 i=1 1 * 0 0 0 0 0
14384: * 2 2 1 1 1 * 1 0 0 0 1
14385: * 3 i=2 1 2 1 1 * 2 0 0 1 0
14386: * 4 2 2 1 1 * 3 0 0 1 1
14387: * 5 i=3 1 i=2 1 2 1 * 4 0 1 0 0
14388: * 6 2 1 2 1 * 5 0 1 0 1
14389: * 7 i=4 1 2 2 1 * 6 0 1 1 0
14390: * 8 2 2 2 1 * 7 0 1 1 1
14391: * 9 i=5 1 i=3 1 i=2 1 2 * 8 1 0 0 0
14392: * 10 2 1 1 2 * 9 1 0 0 1
14393: * 11 i=6 1 2 1 2 * 10 1 0 1 0
14394: * 12 2 2 1 2 * 11 1 0 1 1
14395: * 13 i=7 1 i=4 1 2 2 * 12 1 1 0 0
14396: * 14 2 1 2 2 * 13 1 1 0 1
14397: * 15 i=8 1 2 2 2 * 14 1 1 1 0
14398: * 16 2 2 2 2 * 15 1 1 1 1
14399: */
14400: /* How to do the opposite? From combination h (=1 to 2**k) how to get the value on the covariates? */
14401: /* from h=5 and m, we get then number of covariates k=log(m)/log(2)=4
14402: * and the value of each covariate?
14403: * V1=1, V2=1, V3=2, V4=1 ?
14404: * h-1=4 and 4 is 0100 or reverse 0010, and +1 is 1121 ok.
14405: * h=6, 6-1=5, 5 is 0101, 1010, 2121, V1=2nd, V2=1st, V3=2nd, V4=1st.
14406: * In order to get the real value in the data, we use nbcode
14407: * nbcode[Tvar[3][2nd]]=1 and nbcode[Tvar[4][1]]=0
14408: * We are keeping this crazy system in order to be able (in the future?)
14409: * to have more than 2 values (0 or 1) for a covariate.
14410: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
14411: * h=6, k=2? h-1=5=0101, reverse 1010, +1=2121, k=2nd position: value is 1: codtabm(6,2)=1
14412: * bbbbbbbb
14413: * 76543210
14414: * h-1 00000101 (6-1=5)
14415: *(h-1)>>(k-1)= 00000010 >> (2-1) = 1 right shift
14416: * &
14417: * 1 00000001 (1)
14418: * 00000000 = 1 & ((h-1) >> (k-1))
14419: * +1= 00000001 =1
14420: *
14421: * h=14, k=3 => h'=h-1=13, k'=k-1=2
14422: * h' 1101 =2^3+2^2+0x2^1+2^0
14423: * >>k' 11
14424: * & 00000001
14425: * = 00000001
14426: * +1 = 00000010=2 = codtabm(14,3)
14427: * Reverse h=6 and m=16?
14428: * cptcoveff=log(16)/log(2)=4 covariate: 6-1=5=0101 reversed=1010 +1=2121 =>V1=2, V2=1, V3=2, V4=1.
14429: * for (j=1 to cptcoveff) Vj=decodtabm(j,h,cptcoveff)
14430: * decodtabm(h,j,cptcoveff)= (((h-1) >> (j-1)) & 1) +1
14431: * decodtabm(h,j,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (j-1)) & 1) +1 : -1)
14432: * V3=decodtabm(14,3,2**4)=2
14433: * h'=13 1101 =2^3+2^2+0x2^1+2^0
14434: *(h-1) >> (j-1) 0011 =13 >> 2
14435: * &1 000000001
14436: * = 000000001
14437: * +1= 000000010 =2
14438: * 2211
14439: * V1=1+1, V2=0+1, V3=1+1, V4=1+1
14440: * V3=2
14441: * codtabm and decodtabm are identical
14442: */
14443:
14444:
14445: free_ivector(Ndum,-1,NCOVMAX);
14446:
14447:
14448:
14449: /* Initialisation of ----------- gnuplot -------------*/
14450: strcpy(optionfilegnuplot,optionfilefiname);
14451: if(mle==-3)
14452: strcat(optionfilegnuplot,"-MORT_");
14453: strcat(optionfilegnuplot,".gp");
14454:
14455: if((ficgp=fopen(optionfilegnuplot,"w"))==NULL) {
14456: printf("Problem with file %s",optionfilegnuplot);
14457: }
14458: else{
14459: fprintf(ficgp,"\n# IMaCh-%s\n", version);
14460: fprintf(ficgp,"# %s\n", optionfilegnuplot);
14461: //fprintf(ficgp,"set missing 'NaNq'\n");
14462: fprintf(ficgp,"set datafile missing 'NaNq'\n");
14463: }
14464: /* fclose(ficgp);*/
14465:
14466:
14467: /* Initialisation of --------- index.htm --------*/
14468:
14469: strcpy(optionfilehtm,optionfilefiname); /* Main html file */
14470: if(mle==-3)
14471: strcat(optionfilehtm,"-MORT_");
14472: strcat(optionfilehtm,".htm");
14473: if((fichtm=fopen(optionfilehtm,"w"))==NULL) {
14474: printf("Problem with %s \n",optionfilehtm);
14475: exit(0);
14476: }
14477:
14478: strcpy(optionfilehtmcov,optionfilefiname); /* Only for matrix of covariance */
14479: strcat(optionfilehtmcov,"-cov.htm");
14480: if((fichtmcov=fopen(optionfilehtmcov,"w"))==NULL) {
14481: printf("Problem with %s \n",optionfilehtmcov), exit(0);
14482: }
14483: else{
14484: fprintf(fichtmcov,"<html><head>\n<title>IMaCh Cov %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
14485: <hr size=\"2\" color=\"#EC5E5E\"> \n\
14486: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
14487: optionfilehtmcov,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
14488: }
14489:
14490: fprintf(fichtm,"<html><head>\n<meta charset=\"utf-8\"/><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n\
14491: <title>IMaCh %s</title></head>\n\
14492: <body><font size=\"7\"><a href=http:/euroreves.ined.fr/imach>IMaCh for Interpolated Markov Chain</a> </font><br>\n\
14493: <font size=\"3\">Sponsored by Copyright (C) 2002-2015 <a href=http://www.ined.fr>INED</a>\
14494: -EUROREVES-Institut de longévité-2013-2022-Japan Society for the Promotion of Sciences 日本学術振興会 \
14495: (<a href=https://www.jsps.go.jp/english/e-grants/>Grant-in-Aid for Scientific Research 25293121</a>) - \
14496: <a href=https://software.intel.com/en-us>Intel Software 2015-2018</a></font><br> \n", optionfilehtm);
14497:
14498: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\"> \n\
14499: <font size=\"2\">IMaCh-%s <br> %s</font> \
14500: <hr size=\"2\" color=\"#EC5E5E\"> \n\
14501: This file: <a href=\"%s\">%s</a></br>Title=%s <br>Datafile=<a href=\"%s\">%s</a> Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n\
14502: \n\
14503: <hr size=\"2\" color=\"#EC5E5E\">\
14504: <ul><li><h4>Parameter files</h4>\n\
14505: - Parameter file: <a href=\"%s.%s\">%s.%s</a><br>\n\
14506: - Copy of the parameter file: <a href=\"o%s\">o%s</a><br>\n\
14507: - Log file of the run: <a href=\"%s\">%s</a><br>\n\
14508: - Gnuplot file name: <a href=\"%s\">%s</a><br>\n\
14509: - Date and time at start: %s</ul>\n",\
14510: version,fullversion,optionfilehtm,optionfilehtm,title,datafile,datafile,firstpass,lastpass,stepm, weightopt, model, \
14511: optionfilefiname,optionfilext,optionfilefiname,optionfilext,\
14512: fileres,fileres,\
14513: filelog,filelog,optionfilegnuplot,optionfilegnuplot,strstart);
14514: fflush(fichtm);
14515:
14516: strcpy(pathr,path);
14517: strcat(pathr,optionfilefiname);
14518: #ifdef WIN32
14519: _chdir(optionfilefiname); /* Move to directory named optionfile */
14520: #else
14521: chdir(optionfilefiname); /* Move to directory named optionfile */
14522: #endif
14523:
14524:
14525: /* Calculates basic frequencies. Computes observed prevalence at single age
14526: and for any valid combination of covariates
14527: and prints on file fileres'p'. */
14528: freqsummary(fileres, p, pstart, agemin, agemax, s, agev, nlstate, imx, Tvaraff, invalidvarcomb, nbcode, ncodemax,mint,anint,strstart, \
14529: firstpass, lastpass, stepm, weightopt, model);
14530:
14531: fprintf(fichtm,"\n");
14532: fprintf(fichtm,"<h4>Parameter line 2</h4><ul><li>Tolerance for the convergence of the likelihood: ftol=%g \n<li>Interval for the elementary matrix (in month): stepm=%d",\
14533: ftol, stepm);
14534: fprintf(fichtm,"\n<li>Number of fixed dummy covariates: ncovcol=%d ", ncovcol);
14535: ncurrv=1;
14536: for(i=ncurrv; i <=ncovcol; i++) fprintf(fichtm,"V%d ", i);
14537: fprintf(fichtm,"\n<li> Number of fixed quantitative variables: nqv=%d ", nqv);
14538: ncurrv=i;
14539: for(i=ncurrv; i <=ncurrv-1+nqv; i++) fprintf(fichtm,"V%d ", i);
14540: fprintf(fichtm,"\n<li> Number of time varying (wave varying) dummy covariates: ntv=%d ", ntv);
14541: ncurrv=i;
14542: for(i=ncurrv; i <=ncurrv-1+ntv; i++) fprintf(fichtm,"V%d ", i);
14543: fprintf(fichtm,"\n<li>Number of time varying quantitative covariates: nqtv=%d ", nqtv);
14544: ncurrv=i;
14545: for(i=ncurrv; i <=ncurrv-1+nqtv; i++) fprintf(fichtm,"V%d ", i);
14546: fprintf(fichtm,"\n<li>Weights column \n<br>Number of alive states: nlstate=%d <br>Number of death states (not really implemented): ndeath=%d \n<li>Number of waves: maxwav=%d \n<li>Parameter for maximization (1), using parameter values (0), for design of parameters and variance-covariance matrix: mle=%d \n<li>Does the weight column be taken into account (1), or not (0): weight=%d</ul>\n", \
14547: nlstate, ndeath, maxwav, mle, weightopt);
14548:
14549: fprintf(fichtm,"<h4> Diagram of states <a href=\"%s_.svg\">%s_.svg</a></h4> \n\
14550: <img src=\"%s_.svg\">", subdirf2(optionfilefiname,"D_"),subdirf2(optionfilefiname,"D_"),subdirf2(optionfilefiname,"D_"));
14551:
14552:
14553: fprintf(fichtm,"\n<h4>Some descriptive statistics </h4>\n<br>Number of (used) observations=%d <br>\n\
14554: Youngest age at first (selected) pass %.2f, oldest age %.2f<br>\n\
14555: Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n",\
14556: imx,agemin,agemax,jmin,jmax,jmean);
14557: pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
14558: oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
14559: newms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
14560: savms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
14561: oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */
14562:
14563: /* For Powell, parameters are in a vector p[] starting at p[1]
14564: so we point p on param[1][1] so that p[1] maps on param[1][1][1] */
14565: p=param[1][1]; /* *(*(*(param +1)+1)+0) */
14566:
14567: globpr=0; /* To get the number ipmx of contributions and the sum of weights*/
14568: /* For mortality only */
14569: if (mle==-3){
14570: ximort=matrix(1,NDIM,1,NDIM);
14571: for(i=1;i<=NDIM;i++)
14572: for(j=1;j<=NDIM;j++)
14573: ximort[i][j]=0.;
14574: /* ximort=gsl_matrix_alloc(1,NDIM,1,NDIM); */
14575: cens=ivector(firstobs,lastobs);
14576: ageexmed=vector(firstobs,lastobs);
14577: agecens=vector(firstobs,lastobs);
14578: dcwave=ivector(firstobs,lastobs);
14579:
14580: for (i=1; i<=imx; i++){
14581: dcwave[i]=-1;
14582: for (m=firstpass; m<=lastpass; m++)
14583: if (s[m][i]>nlstate) {
14584: dcwave[i]=m;
14585: /* printf("i=%d j=%d s=%d dcwave=%d\n",i,j, s[j][i],dcwave[i]);*/
14586: break;
14587: }
14588: }
14589:
14590: for (i=1; i<=imx; i++) {
14591: if (wav[i]>0){
14592: ageexmed[i]=agev[mw[1][i]][i];
14593: j=wav[i];
14594: agecens[i]=1.;
14595:
14596: if (ageexmed[i]> 1 && wav[i] > 0){
14597: agecens[i]=agev[mw[j][i]][i];
14598: cens[i]= 1;
14599: }else if (ageexmed[i]< 1)
14600: cens[i]= -1;
14601: if (agedc[i]< AGESUP && agedc[i]>1 && dcwave[i]>firstpass && dcwave[i]<=lastpass)
14602: cens[i]=0 ;
14603: }
14604: else cens[i]=-1;
14605: }
14606:
14607: for (i=1;i<=NDIM;i++) {
14608: for (j=1;j<=NDIM;j++)
14609: ximort[i][j]=(i == j ? 1.0 : 0.0);
14610: }
14611:
14612: p[1]=0.0268; p[NDIM]=0.083;
14613: /* printf("%lf %lf", p[1], p[2]); */
14614:
14615:
14616: #ifdef GSL
14617: printf("GSL optimization\n"); fprintf(ficlog,"Powell\n");
14618: #else
14619: printf("Powell-mort\n"); fprintf(ficlog,"Powell-mort\n");
14620: #endif
14621: strcpy(filerespow,"POW-MORT_");
14622: strcat(filerespow,fileresu);
14623: if((ficrespow=fopen(filerespow,"w"))==NULL) {
14624: printf("Problem with resultfile: %s\n", filerespow);
14625: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
14626: }
14627: #ifdef GSL
14628: fprintf(ficrespow,"# GSL optimization\n# iter -2*LL");
14629: #else
14630: fprintf(ficrespow,"# Powell\n# iter -2*LL");
14631: #endif
14632: /* for (i=1;i<=nlstate;i++)
14633: for(j=1;j<=nlstate+ndeath;j++)
14634: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
14635: */
14636: fprintf(ficrespow,"\n");
14637: #ifdef GSL
14638: /* gsl starts here */
14639: T = gsl_multimin_fminimizer_nmsimplex;
14640: gsl_multimin_fminimizer *sfm = NULL;
14641: gsl_vector *ss, *x;
14642: gsl_multimin_function minex_func;
14643:
14644: /* Initial vertex size vector */
14645: ss = gsl_vector_alloc (NDIM);
14646:
14647: if (ss == NULL){
14648: GSL_ERROR_VAL ("failed to allocate space for ss", GSL_ENOMEM, 0);
14649: }
14650: /* Set all step sizes to 1 */
14651: gsl_vector_set_all (ss, 0.001);
14652:
14653: /* Starting point */
14654:
14655: x = gsl_vector_alloc (NDIM);
14656:
14657: if (x == NULL){
14658: gsl_vector_free(ss);
14659: GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0);
14660: }
14661:
14662: /* Initialize method and iterate */
14663: /* p[1]=0.0268; p[NDIM]=0.083; */
14664: /* gsl_vector_set(x, 0, 0.0268); */
14665: /* gsl_vector_set(x, 1, 0.083); */
14666: gsl_vector_set(x, 0, p[1]);
14667: gsl_vector_set(x, 1, p[2]);
14668:
14669: minex_func.f = &gompertz_f;
14670: minex_func.n = NDIM;
14671: minex_func.params = (void *)&p; /* ??? */
14672:
14673: sfm = gsl_multimin_fminimizer_alloc (T, NDIM);
14674: gsl_multimin_fminimizer_set (sfm, &minex_func, x, ss);
14675:
14676: printf("Iterations beginning .....\n\n");
14677: printf("Iter. # Intercept Slope -Log Likelihood Simplex size\n");
14678:
14679: iteri=0;
14680: while (rval == GSL_CONTINUE){
14681: iteri++;
14682: status = gsl_multimin_fminimizer_iterate(sfm);
14683:
14684: if (status) printf("error: %s\n", gsl_strerror (status));
14685: fflush(0);
14686:
14687: if (status)
14688: break;
14689:
14690: rval = gsl_multimin_test_size (gsl_multimin_fminimizer_size (sfm), 1e-6);
14691: ssval = gsl_multimin_fminimizer_size (sfm);
14692:
14693: if (rval == GSL_SUCCESS)
14694: printf ("converged to a local maximum at\n");
14695:
14696: printf("%5d ", iteri);
14697: for (it = 0; it < NDIM; it++){
14698: printf ("%10.5f ", gsl_vector_get (sfm->x, it));
14699: }
14700: printf("f() = %-10.5f ssize = %.7f\n", sfm->fval, ssval);
14701: }
14702:
14703: printf("\n\n Please note: Program should be run many times with varying starting points to detemine global maximum\n\n");
14704:
14705: gsl_vector_free(x); /* initial values */
14706: gsl_vector_free(ss); /* inital step size */
14707: for (it=0; it<NDIM; it++){
14708: p[it+1]=gsl_vector_get(sfm->x,it);
14709: fprintf(ficrespow," %.12lf", p[it]);
14710: }
14711: gsl_multimin_fminimizer_free (sfm); /* p *(sfm.x.data) et p *(sfm.x.data+1) */
14712: #endif
14713: #ifdef POWELL
14714: powell(p,ximort,NDIM,ftol,&iter,&fret,gompertz);
14715: #endif
14716: fclose(ficrespow);
14717:
14718: hesscov(matcov, hess, p, NDIM, delti, 1e-4, gompertz);
14719:
14720: for(i=1; i <=NDIM; i++)
14721: for(j=i+1;j<=NDIM;j++)
14722: matcov[i][j]=matcov[j][i];
14723:
14724: printf("\nCovariance matrix\n ");
14725: fprintf(ficlog,"\nCovariance matrix\n ");
14726: for(i=1; i <=NDIM; i++) {
14727: for(j=1;j<=NDIM;j++){
14728: printf("%f ",matcov[i][j]);
14729: fprintf(ficlog,"%f ",matcov[i][j]);
14730: }
14731: printf("\n "); fprintf(ficlog,"\n ");
14732: }
14733:
14734: printf("iter=%d MLE=%f Eq=%lf*exp(%lf*(age-%d))\n",iter,-gompertz(p),p[1],p[2],agegomp);
14735: for (i=1;i<=NDIM;i++) {
14736: printf("%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
14737: fprintf(ficlog,"%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
14738: }
14739: lsurv=vector(agegomp,AGESUP);
14740: lpop=vector(agegomp,AGESUP);
14741: tpop=vector(agegomp,AGESUP);
14742: lsurv[agegomp]=100000;
14743:
14744: for (k=agegomp;k<=AGESUP;k++) {
14745: agemortsup=k;
14746: if (p[1]*exp(p[2]*(k-agegomp))>1) break;
14747: }
14748:
14749: for (k=agegomp;k<agemortsup;k++)
14750: lsurv[k+1]=lsurv[k]-lsurv[k]*(p[1]*exp(p[2]*(k-agegomp)));
14751:
14752: for (k=agegomp;k<agemortsup;k++){
14753: lpop[k]=(lsurv[k]+lsurv[k+1])/2.;
14754: sumlpop=sumlpop+lpop[k];
14755: }
14756:
14757: tpop[agegomp]=sumlpop;
14758: for (k=agegomp;k<(agemortsup-3);k++){
14759: /* tpop[k+1]=2;*/
14760: tpop[k+1]=tpop[k]-lpop[k];
14761: }
14762:
14763:
14764: printf("\nAge lx qx dx Lx Tx e(x)\n");
14765: for (k=agegomp;k<(agemortsup-2);k++)
14766: 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]);
14767:
14768:
14769: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
14770: ageminpar=50;
14771: agemaxpar=100;
14772: if(ageminpar == AGEOVERFLOW ||agemaxpar == AGEOVERFLOW){
14773: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
14774: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
14775: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
14776: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
14777: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
14778: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
14779: }else{
14780: printf("Warning! ageminpar %f and agemaxpar %f have been fixed because for simplification until it is fixed...\n\n",ageminpar,agemaxpar);
14781: fprintf(ficlog,"Warning! ageminpar %f and agemaxpar %f have been fixed because for simplification until it is fixed...\n\n",ageminpar,agemaxpar);
14782: printinggnuplotmort(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, pathc,p);
14783: }
14784: printinghtmlmort(fileresu,title,datafile, firstpass, lastpass, \
14785: stepm, weightopt,\
14786: model,imx,p,matcov,agemortsup);
14787:
14788: free_vector(lsurv,agegomp,AGESUP);
14789: free_vector(lpop,agegomp,AGESUP);
14790: free_vector(tpop,agegomp,AGESUP);
14791: free_matrix(ximort,1,NDIM,1,NDIM);
14792: free_ivector(dcwave,firstobs,lastobs);
14793: free_vector(agecens,firstobs,lastobs);
14794: free_vector(ageexmed,firstobs,lastobs);
14795: free_ivector(cens,firstobs,lastobs);
14796: #ifdef GSL
14797: #endif
14798: } /* Endof if mle==-3 mortality only */
14799: /* Standard */
14800: else{ /* For mle !=- 3, could be 0 or 1 or 4 etc. */
14801: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
14802: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
14803: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
14804: printf("First Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
14805: for (k=1; k<=npar;k++)
14806: printf(" %d %8.5f",k,p[k]);
14807: printf("\n");
14808: if(mle>=1){ /* Could be 1 or 2, Real Maximization */
14809: /* mlikeli uses func not funcone */
14810: /* for(i=1;i<nlstate;i++){ */
14811: /* /\*reducing xi for 1 to npar to 1 to ncovmodel; *\/ */
14812: /* mlikeli(ficres,p, ncovmodel, ncovmodel, nlstate, ftol, funcnoprod); */
14813: /* } */
14814: mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func);
14815: }
14816: if(mle==0) {/* No optimization, will print the likelihoods for the datafile */
14817: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
14818: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
14819: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
14820: }
14821: globpr=1; /* again, to print the individual contributions using computed gpimx and gsw */
14822: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
14823: printf("Second Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
14824: /* exit(0); */
14825: for (k=1; k<=npar;k++)
14826: printf(" %d %8.5f",k,p[k]);
14827: printf("\n");
14828:
14829: /*--------- results files --------------*/
14830: /* 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); */
14831:
14832:
14833: fprintf(ficres,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
14834: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n"); /* Printing model equation */
14835: fprintf(ficlog,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
14836:
14837: printf("#model= 1 + age ");
14838: fprintf(ficres,"#model= 1 + age ");
14839: fprintf(ficlog,"#model= 1 + age ");
14840: fprintf(fichtm,"\n<ul><li> model=1+age+%s\n \
14841: </ul>", model);
14842:
14843: fprintf(fichtm,"\n<table style=\"text-align:center; border: 1px solid\">\n");
14844: fprintf(fichtm, "<tr><th>Model=</th><th>1</th><th>+ age</th>");
14845: if(nagesqr==1){
14846: printf(" + age*age ");
14847: fprintf(ficres," + age*age ");
14848: fprintf(ficlog," + age*age ");
14849: fprintf(fichtm, "<th>+ age*age</th>");
14850: }
14851: for(j=1;j <=ncovmodel-2;j++){
14852: if(Typevar[j]==0) {
14853: printf(" + V%d ",Tvar[j]);
14854: fprintf(ficres," + V%d ",Tvar[j]);
14855: fprintf(ficlog," + V%d ",Tvar[j]);
14856: fprintf(fichtm, "<th>+ V%d</th>",Tvar[j]);
14857: }else if(Typevar[j]==1) {
14858: printf(" + V%d*age ",Tvar[j]);
14859: fprintf(ficres," + V%d*age ",Tvar[j]);
14860: fprintf(ficlog," + V%d*age ",Tvar[j]);
14861: fprintf(fichtm, "<th>+ V%d*age</th>",Tvar[j]);
14862: }else if(Typevar[j]==2) {
14863: printf(" + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
14864: fprintf(ficres," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
14865: fprintf(ficlog," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
14866: fprintf(fichtm, "<th>+ V%d*V%d</th>",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
14867: }else if(Typevar[j]==3) { /* TO VERIFY */
14868: printf(" + V%d*V%d*age ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
14869: fprintf(ficres," + V%d*V%d*age ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
14870: fprintf(ficlog," + V%d*V%d*age ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
14871: fprintf(fichtm, "<th>+ V%d*V%d*age</th>",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
14872: }
14873: }
14874: printf("\n");
14875: fprintf(ficres,"\n");
14876: fprintf(ficlog,"\n");
14877: fprintf(fichtm, "</tr>");
14878: fprintf(fichtm, "\n");
14879:
14880:
14881: for(i=1,jk=1; i <=nlstate; i++){
14882: for(k=1; k <=(nlstate+ndeath); k++){
14883: if (k != i) {
14884: fprintf(fichtm, "<tr>");
14885: printf("%d%d ",i,k);
14886: fprintf(ficlog,"%d%d ",i,k);
14887: fprintf(ficres,"%1d%1d ",i,k);
14888: fprintf(fichtm, "<td>%1d%1d</td>",i,k);
14889: for(j=1; j <=ncovmodel; j++){
14890: printf("%12.7f ",p[jk]);
14891: fprintf(ficlog,"%12.7f ",p[jk]);
14892: fprintf(ficres,"%12.7f ",p[jk]);
14893: fprintf(fichtm, "<td>%12.7f</td>",p[jk]);
14894: jk++;
14895: }
14896: printf("\n");
14897: fprintf(ficlog,"\n");
14898: fprintf(ficres,"\n");
14899: fprintf(fichtm, "</tr>\n");
14900: }
14901: }
14902: }
14903: /* fprintf(fichtm,"</tr>\n"); */
14904: fprintf(fichtm,"</table>\n");
14905: fprintf(fichtm, "\n");
14906:
14907: if(mle != 0){
14908: /* Computing hessian and covariance matrix only at a peak of the Likelihood, that is after optimization */
14909: ftolhess=ftol; /* Usually correct */
14910: hesscov(matcov, hess, p, npar, delti, ftolhess, func);
14911: 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");
14912: 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");
14913: fprintf(fichtm, "\n<p>The Wald test results are output only if the maximimzation of the Likelihood is performed (mle=1)\n</br>Parameters, Wald tests and Wald-based confidence intervals\n</br> W is simply the result of the division of the parameter by the square root of covariance of the parameter.\n</br> And Wald-based confidence intervals plus and minus 1.96 * W \n </br> It might be better to visualize the covariance matrix. See the page '<a href=\"%s\">Matrix of variance-covariance of one-step probabilities and its graphs</a>'.\n</br>",optionfilehtmcov);
14914: fprintf(fichtm,"\n<table style=\"text-align:center; border: 1px solid\">");
14915: fprintf(fichtm, "\n<tr><th>Model=</th><th>1</th><th>+ age</th>");
14916: if(nagesqr==1){
14917: printf(" + age*age ");
14918: fprintf(ficres," + age*age ");
14919: fprintf(ficlog," + age*age ");
14920: fprintf(fichtm, "<th>+ age*age</th>");
14921: }
14922: for(j=1;j <=ncovmodel-2;j++){
14923: if(Typevar[j]==0) {
14924: printf(" + V%d ",Tvar[j]);
14925: fprintf(fichtm, "<th>+ V%d</th>",Tvar[j]);
14926: }else if(Typevar[j]==1) {
14927: printf(" + V%d*age ",Tvar[j]);
14928: fprintf(fichtm, "<th>+ V%d*age</th>",Tvar[j]);
14929: }else if(Typevar[j]==2) {
14930: fprintf(fichtm, "<th>+ V%d*V%d</th>",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
14931: }else if(Typevar[j]==3) { /* TO VERIFY */
14932: fprintf(fichtm, "<th>+ V%d*V%d*age</th>",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
14933: }
14934: }
14935: fprintf(fichtm, "</tr>\n");
14936:
14937: for(i=1,jk=1; i <=nlstate; i++){
14938: for(k=1; k <=(nlstate+ndeath); k++){
14939: if (k != i) {
14940: fprintf(fichtm, "<tr valign=top>");
14941: printf("%d%d ",i,k);
14942: fprintf(ficlog,"%d%d ",i,k);
14943: fprintf(fichtm, "<td>%1d%1d</td>",i,k);
14944: for(j=1; j <=ncovmodel; j++){
14945: wald=p[jk]/sqrt(matcov[jk][jk]);
14946: printf("%12.7f(%12.7f) W=%8.3f CI=[%12.7f ; %12.7f] ",p[jk],sqrt(matcov[jk][jk]), p[jk]/sqrt(matcov[jk][jk]), p[jk]-1.96*sqrt(matcov[jk][jk]),p[jk]+1.96*sqrt(matcov[jk][jk]));
14947: fprintf(ficlog,"%12.7f(%12.7f) W=%8.3f CI=[%12.7f ; %12.7f] ",p[jk],sqrt(matcov[jk][jk]), p[jk]/sqrt(matcov[jk][jk]), p[jk]-1.96*sqrt(matcov[jk][jk]),p[jk]+1.96*sqrt(matcov[jk][jk]));
14948: if(fabs(wald) > 1.96){
14949: fprintf(fichtm, "<td><b>%12.7f</b></br> (%12.7f)</br>",p[jk],sqrt(matcov[jk][jk]));
14950: }else{
14951: fprintf(fichtm, "<td>%12.7f (%12.7f)</br>",p[jk],sqrt(matcov[jk][jk]));
14952: }
14953: fprintf(fichtm,"W=%8.3f</br>",wald);
14954: fprintf(fichtm,"[%12.7f;%12.7f]</br></td>", p[jk]-1.96*sqrt(matcov[jk][jk]),p[jk]+1.96*sqrt(matcov[jk][jk]));
14955: jk++;
14956: }
14957: printf("\n");
14958: fprintf(ficlog,"\n");
14959: fprintf(fichtm, "</tr>\n");
14960: }
14961: }
14962: }
14963: } /* end of hesscov and Wald tests */
14964: fprintf(fichtm,"</table>\n");
14965:
14966: /* */
14967: fprintf(ficres,"# Scales (for hessian or gradient estimation)\n");
14968: printf("# Scales (for hessian or gradient estimation)\n");
14969: fprintf(ficlog,"# Scales (for hessian or gradient estimation)\n");
14970: for(i=1,jk=1; i <=nlstate; i++){
14971: for(j=1; j <=nlstate+ndeath; j++){
14972: if (j!=i) {
14973: fprintf(ficres,"%1d%1d",i,j);
14974: printf("%1d%1d",i,j);
14975: fprintf(ficlog,"%1d%1d",i,j);
14976: for(k=1; k<=ncovmodel;k++){
14977: printf(" %.5e",delti[jk]);
14978: fprintf(ficlog," %.5e",delti[jk]);
14979: fprintf(ficres," %.5e",delti[jk]);
14980: jk++;
14981: }
14982: printf("\n");
14983: fprintf(ficlog,"\n");
14984: fprintf(ficres,"\n");
14985: }
14986: }
14987: }
14988:
14989: 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");
14990: if(mle >= 1) /* Too big for the screen */
14991: 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");
14992: 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");
14993: /* # 121 Var(a12)\n\ */
14994: /* # 122 Cov(b12,a12) Var(b12)\n\ */
14995: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
14996: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
14997: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
14998: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
14999: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
15000: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
15001:
15002:
15003: /* Just to have a covariance matrix which will be more understandable
15004: even is we still don't want to manage dictionary of variables
15005: */
15006: for(itimes=1;itimes<=2;itimes++){
15007: jj=0;
15008: for(i=1; i <=nlstate; i++){
15009: for(j=1; j <=nlstate+ndeath; j++){
15010: if(j==i) continue;
15011: for(k=1; k<=ncovmodel;k++){
15012: jj++;
15013: ca[0]= k+'a'-1;ca[1]='\0';
15014: if(itimes==1){
15015: if(mle>=1)
15016: printf("#%1d%1d%d",i,j,k);
15017: fprintf(ficlog,"#%1d%1d%d",i,j,k);
15018: fprintf(ficres,"#%1d%1d%d",i,j,k);
15019: }else{
15020: if(mle>=1)
15021: printf("%1d%1d%d",i,j,k);
15022: fprintf(ficlog,"%1d%1d%d",i,j,k);
15023: fprintf(ficres,"%1d%1d%d",i,j,k);
15024: }
15025: ll=0;
15026: for(li=1;li <=nlstate; li++){
15027: for(lj=1;lj <=nlstate+ndeath; lj++){
15028: if(lj==li) continue;
15029: for(lk=1;lk<=ncovmodel;lk++){
15030: ll++;
15031: if(ll<=jj){
15032: cb[0]= lk +'a'-1;cb[1]='\0';
15033: if(ll<jj){
15034: if(itimes==1){
15035: if(mle>=1)
15036: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
15037: fprintf(ficlog," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
15038: fprintf(ficres," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
15039: }else{
15040: if(mle>=1)
15041: printf(" %.5e",matcov[jj][ll]);
15042: fprintf(ficlog," %.5e",matcov[jj][ll]);
15043: fprintf(ficres," %.5e",matcov[jj][ll]);
15044: }
15045: }else{
15046: if(itimes==1){
15047: if(mle>=1)
15048: printf(" Var(%s%1d%1d)",ca,i,j);
15049: fprintf(ficlog," Var(%s%1d%1d)",ca,i,j);
15050: fprintf(ficres," Var(%s%1d%1d)",ca,i,j);
15051: }else{
15052: if(mle>=1)
15053: printf(" %.7e",matcov[jj][ll]);
15054: fprintf(ficlog," %.7e",matcov[jj][ll]);
15055: fprintf(ficres," %.7e",matcov[jj][ll]);
15056: }
15057: }
15058: }
15059: } /* end lk */
15060: } /* end lj */
15061: } /* end li */
15062: if(mle>=1)
15063: printf("\n");
15064: fprintf(ficlog,"\n");
15065: fprintf(ficres,"\n");
15066: numlinepar++;
15067: } /* end k*/
15068: } /*end j */
15069: } /* end i */
15070: } /* end itimes */
15071:
15072: fflush(ficlog);
15073: fflush(ficres);
15074: while(fgets(line, MAXLINE, ficpar)) {
15075: /* If line starts with a # it is a comment */
15076: if (line[0] == '#') {
15077: numlinepar++;
15078: fputs(line,stdout);
15079: fputs(line,ficparo);
15080: fputs(line,ficlog);
15081: fputs(line,ficres);
15082: continue;
15083: }else
15084: break;
15085: }
15086:
15087: /* while((c=getc(ficpar))=='#' && c!= EOF){ */
15088: /* ungetc(c,ficpar); */
15089: /* fgets(line, MAXLINE, ficpar); */
15090: /* fputs(line,stdout); */
15091: /* fputs(line,ficparo); */
15092: /* } */
15093: /* ungetc(c,ficpar); */
15094:
15095: estepm=0;
15096: 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){
15097:
15098: if (num_filled != 6) {
15099: 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);
15100: 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);
15101: goto end;
15102: }
15103: printf("agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%lf\n",ageminpar,agemaxpar, bage, fage, estepm, ftolpl);
15104: }
15105: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
15106: /*ftolpl=6.e-4;*/ /* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
15107:
15108: /* fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%\n",&ageminpar,&agemaxpar, &bage, &fage, &estepm); */
15109: if (estepm==0 || estepm < stepm) estepm=stepm;
15110: if (fage <= 2) {
15111: bage = ageminpar;
15112: fage = agemaxpar;
15113: }
15114:
15115: fprintf(ficres,"# agemin agemax for life expectancy, bage fage (if mle==0 ie no data nor Max likelihood).\n");
15116: fprintf(ficres,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
15117: fprintf(ficparo,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d, ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
15118:
15119: /* Other stuffs, more or less useful */
15120: while(fgets(line, MAXLINE, ficpar)) {
15121: /* If line starts with a # it is a comment */
15122: if (line[0] == '#') {
15123: numlinepar++;
15124: fputs(line,stdout);
15125: fputs(line,ficparo);
15126: fputs(line,ficlog);
15127: fputs(line,ficres);
15128: continue;
15129: }else
15130: break;
15131: }
15132:
15133: if((num_filled=sscanf(line,"begin-prev-date=%lf/%lf/%lf end-prev-date=%lf/%lf/%lf mov_average=%d\n",&jprev1, &mprev1,&anprev1,&jprev2, &mprev2,&anprev2,&mobilav)) !=EOF){
15134:
15135: if (num_filled != 7) {
15136: printf("Error: Not 7 (data)parameters in line but %d, for example:begin-prev-date=1/1/1990 end-prev-date=1/6/2004 mov_average=0\n, your line=%s . Probably you are running an older format.\n",num_filled,line);
15137: fprintf(ficlog,"Error: Not 7 (data)parameters in line but %d, for example:begin-prev-date=1/1/1990 end-prev-date=1/6/2004 mov_average=0\n, your line=%s . Probably you are running an older format.\n",num_filled,line);
15138: goto end;
15139: }
15140: printf("begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mov_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
15141: 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);
15142: 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);
15143: 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);
15144: }
15145:
15146: while(fgets(line, MAXLINE, ficpar)) {
15147: /* If line starts with a # it is a comment */
15148: if (line[0] == '#') {
15149: numlinepar++;
15150: fputs(line,stdout);
15151: fputs(line,ficparo);
15152: fputs(line,ficlog);
15153: fputs(line,ficres);
15154: continue;
15155: }else
15156: break;
15157: }
15158:
15159:
15160: dateprev1=anprev1+(mprev1-1)/12.+(jprev1-1)/365.;
15161: dateprev2=anprev2+(mprev2-1)/12.+(jprev2-1)/365.;
15162:
15163: if((num_filled=sscanf(line,"pop_based=%d\n",&popbased)) !=EOF){
15164: if (num_filled != 1) {
15165: printf("Error: Not 1 (data)parameters in line but %d, for example:pop_based=0\n, your line=%s . Probably you are running an older format.\n",num_filled,line);
15166: fprintf(ficlog,"Error: Not 1 (data)parameters in line but %d, for example: pop_based=1\n, your line=%s . Probably you are running an older format.\n",num_filled,line);
15167: goto end;
15168: }
15169: printf("pop_based=%d\n",popbased);
15170: fprintf(ficlog,"pop_based=%d\n",popbased);
15171: fprintf(ficparo,"pop_based=%d\n",popbased);
15172: fprintf(ficres,"pop_based=%d\n",popbased);
15173: }
15174:
15175: /* Results */
15176: /* Value of covariate in each resultine will be computed (if product) and sorted according to model rank */
15177: /* It is precov[] because we need the varying age in order to compute the real cov[] of the model equation */
15178: precov=matrix(1,MAXRESULTLINESPONE,1,NCOVMAX+1);
15179: endishere=0;
15180: nresult=0;
15181: parameterline=0;
15182: do{
15183: if(!fgets(line, MAXLINE, ficpar)){
15184: endishere=1;
15185: parameterline=15;
15186: }else if (line[0] == '#') {
15187: /* If line starts with a # it is a comment */
15188: numlinepar++;
15189: fputs(line,stdout);
15190: fputs(line,ficparo);
15191: fputs(line,ficlog);
15192: fputs(line,ficres);
15193: continue;
15194: }else if(sscanf(line,"prevforecast=%[^\n]\n",modeltemp))
15195: parameterline=11;
15196: else if(sscanf(line,"prevbackcast=%[^\n]\n",modeltemp))
15197: parameterline=12;
15198: else if(sscanf(line,"result:%[^\n]\n",modeltemp)){
15199: parameterline=13;
15200: }
15201: else{
15202: parameterline=14;
15203: }
15204: switch (parameterline){ /* =0 only if only comments */
15205: case 11:
15206: if((num_filled=sscanf(line,"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)) !=EOF && (num_filled == 8)){
15207: 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);
15208: 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);
15209: 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);
15210: 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);
15211: /* day and month of proj2 are not used but only year anproj2.*/
15212: dateproj1=anproj1+(mproj1-1)/12.+(jproj1-1)/365.;
15213: dateproj2=anproj2+(mproj2-1)/12.+(jproj2-1)/365.;
15214: prvforecast = 1;
15215: }
15216: else if((num_filled=sscanf(line,"prevforecast=%d yearsfproj=%lf mobil_average=%d\n",&prevfcast,&yrfproj,&mobilavproj)) !=EOF){/* && (num_filled == 3))*/
15217: printf("prevforecast=%d yearsfproj=%.2lf mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
15218: fprintf(ficlog,"prevforecast=%d yearsfproj=%.2lf mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
15219: fprintf(ficres,"prevforecast=%d yearsfproj=%.2lf mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
15220: prvforecast = 2;
15221: }
15222: else {
15223: printf("Error: Not 8 (data)parameters in line but %d, for example:prevforecast=1 starting-proj-date=1/1/1990 final-proj-date=1/1/2000 mobil_average=0\nnor 3 (data)parameters, for example:prevforecast=1 yearsfproj=10 mobil_average=0. Your line=%s . You are running probably an older format.\n, ",num_filled,line);
15224: fprintf(ficlog,"Error: Not 8 (data)parameters in line but %d, for example:prevforecast=1 starting-proj-date=1/1/1990 final-proj-date=1/1/2000 mobil_average=0\nnor 3 (data)parameters, for example:prevforecast=1 yearproj=10 mobil_average=0. Your line=%s . You are running probably an older format.\n, ",num_filled,line);
15225: goto end;
15226: }
15227: break;
15228: case 12:
15229: if((num_filled=sscanf(line,"prevbackcast=%d starting-back-date=%lf/%lf/%lf final-back-date=%lf/%lf/%lf mobil_average=%d\n",&prevbcast,&jback1,&mback1,&anback1,&jback2,&mback2,&anback2,&mobilavproj)) !=EOF && (num_filled == 8)){
15230: fprintf(ficparo,"prevbackcast=%d starting-back-date=%.lf/%.lf/%.lf final-back-date=%.lf/%.lf/%.lf mobil_average=%d\n",prevbcast,jback1,mback1,anback1,jback2,mback2,anback2,mobilavproj);
15231: printf("prevbackcast=%d starting-back-date=%.lf/%.lf/%.lf final-back-date=%.lf/%.lf/%.lf mobil_average=%d\n",prevbcast,jback1,mback1,anback1,jback2,mback2,anback2,mobilavproj);
15232: fprintf(ficlog,"prevbackcast=%d starting-back-date=%.lf/%.lf/%.lf final-back-date=%.lf/%.lf/%.lf mobil_average=%d\n",prevbcast,jback1,mback1,anback1,jback2,mback2,anback2,mobilavproj);
15233: fprintf(ficres,"prevbackcast=%d starting-back-date=%.lf/%.lf/%.lf final-back-date=%.lf/%.lf/%.lf mobil_average=%d\n",prevbcast,jback1,mback1,anback1,jback2,mback2,anback2,mobilavproj);
15234: /* day and month of back2 are not used but only year anback2.*/
15235: dateback1=anback1+(mback1-1)/12.+(jback1-1)/365.;
15236: dateback2=anback2+(mback2-1)/12.+(jback2-1)/365.;
15237: prvbackcast = 1;
15238: }
15239: else if((num_filled=sscanf(line,"prevbackcast=%d yearsbproj=%lf mobil_average=%d\n",&prevbcast,&yrbproj,&mobilavproj)) ==3){/* && (num_filled == 3))*/
15240: printf("prevbackcast=%d yearsbproj=%.2lf mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
15241: fprintf(ficlog,"prevbackcast=%d yearsbproj=%.2lf mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
15242: fprintf(ficres,"prevbackcast=%d yearsbproj=%.2lf mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
15243: prvbackcast = 2;
15244: }
15245: else {
15246: printf("Error: Not 8 (data)parameters in line but %d, for example:prevbackcast=1 starting-back-date=1/1/1990 final-back-date=1/1/2000 mobil_average=0\nnor 3 (data)parameters, for example:prevbackcast=1 yearsbproj=10 mobil_average=0. Your line=%s . You are running probably an older format.\n, ",num_filled,line);
15247: fprintf(ficlog,"Error: Not 8 (data)parameters in line but %d, for example:prevbackcast=1 starting-back-date=1/1/1990 final-back-date=1/1/2000 mobil_average=0\nnor 3 (data)parameters, for example:prevbackcast=1 yearbproj=10 mobil_average=0. Your line=%s . You are running probably an older format.\n, ",num_filled,line);
15248: goto end;
15249: }
15250: break;
15251: case 13:
15252: num_filled=sscanf(line,"result:%[^\n]\n",resultlineori);
15253: nresult++; /* Sum of resultlines */
15254: /* printf("Result %d: result:%s\n",nresult, resultlineori); */
15255: /* removefirstspace(&resultlineori); */
15256:
15257: if(strstr(resultlineori,"v") !=0){
15258: printf("Error. 'v' must be in upper case 'V' result: %s ",resultlineori);
15259: fprintf(ficlog,"Error. 'v' must be in upper case result: %s ",resultlineori);fflush(ficlog);
15260: return 1;
15261: }
15262: trimbb(resultline, resultlineori); /* Suppressing double blank in the resultline */
15263: /* printf("Decoderesult resultline=\"%s\" resultlineori=\"%s\"\n", resultline, resultlineori); */
15264: if(nresult > MAXRESULTLINESPONE-1){
15265: printf("ERROR: Current version of IMaCh limits the number of resultlines to %d, you used %d\nYou can use the 'r' parameter file '%s' which uses option mle=0 to get other results. ",MAXRESULTLINESPONE-1,nresult,rfileres);
15266: fprintf(ficlog,"ERROR: Current version of IMaCh limits the number of resultlines to %d, you used %d\nYou can use the 'r' parameter file '%s' which uses option mle=0 to get other results. ",MAXRESULTLINESPONE-1,nresult,rfileres);
15267: goto end;
15268: }
15269:
15270: if(!decoderesult(resultline, nresult)){ /* Fills TKresult[nresult] combination and Tresult[nresult][k4+1] combination values */
15271: fprintf(ficparo,"result: %s\n",resultline);
15272: fprintf(ficres,"result: %s\n",resultline);
15273: fprintf(ficlog,"result: %s\n",resultline);
15274: } else
15275: goto end;
15276: break;
15277: case 14:
15278: printf("Error: Unknown command '%s'\n",line);
15279: fprintf(ficlog,"Error: Unknown command '%s'\n",line);
15280: if(line[0] == ' ' || line[0] == '\n'){
15281: printf("It should not be an empty line '%s'\n",line);
15282: fprintf(ficlog,"It should not be an empty line '%s'\n",line);
15283: }
15284: if(ncovmodel >=2 && nresult==0 ){
15285: printf("ERROR: no result lines! It should be at minimum 'result: V2=0 V1=1 or result:.' %s\n",line);
15286: fprintf(ficlog,"ERROR: no result lines! It should be at minimum 'result: V2=0 V1=1 or result:.' %s\n",line);
15287: }
15288: /* goto end; */
15289: break;
15290: case 15:
15291: printf("End of resultlines.\n");
15292: fprintf(ficlog,"End of resultlines.\n");
15293: break;
15294: default: /* parameterline =0 */
15295: nresult=1;
15296: decoderesult(".",nresult ); /* No covariate */
15297: } /* End switch parameterline */
15298: }while(endishere==0); /* End do */
15299:
15300: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint); */
15301: /* ,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2); */
15302:
15303: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
15304: if(ageminpar == AGEOVERFLOW ||agemaxpar == -AGEOVERFLOW){
15305: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
15306: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
15307: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
15308: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
15309: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
15310: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
15311: }else{
15312: /* printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, prevfcast, backcast, pathc,p, (int)anproj1-(int)agemin, (int)anback1-(int)agemax+1); */
15313: /* It seems that anprojd which is computed from the mean year at interview which is known yet because of freqsummary */
15314: /* date2dmy(dateintmean,&jintmean,&mintmean,&aintmean); */ /* Done in freqsummary */
15315: if(prvforecast==1){
15316: dateprojd=(jproj1+12*mproj1+365*anproj1)/365;
15317: jprojd=jproj1;
15318: mprojd=mproj1;
15319: anprojd=anproj1;
15320: dateprojf=(jproj2+12*mproj2+365*anproj2)/365;
15321: jprojf=jproj2;
15322: mprojf=mproj2;
15323: anprojf=anproj2;
15324: } else if(prvforecast == 2){
15325: dateprojd=dateintmean;
15326: date2dmy(dateprojd,&jprojd, &mprojd, &anprojd);
15327: dateprojf=dateintmean+yrfproj;
15328: date2dmy(dateprojf,&jprojf, &mprojf, &anprojf);
15329: }
15330: if(prvbackcast==1){
15331: datebackd=(jback1+12*mback1+365*anback1)/365;
15332: jbackd=jback1;
15333: mbackd=mback1;
15334: anbackd=anback1;
15335: datebackf=(jback2+12*mback2+365*anback2)/365;
15336: jbackf=jback2;
15337: mbackf=mback2;
15338: anbackf=anback2;
15339: } else if(prvbackcast == 2){
15340: datebackd=dateintmean;
15341: date2dmy(datebackd,&jbackd, &mbackd, &anbackd);
15342: datebackf=dateintmean-yrbproj;
15343: date2dmy(datebackf,&jbackf, &mbackf, &anbackf);
15344: }
15345:
15346: printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,bage, fage, prevfcast, prevbcast, pathc,p, (int)anprojd-bage, (int)anbackd-fage);/* HERE valgrind Tvard*/
15347: }
15348: printinghtml(fileresu,title,datafile, firstpass, lastpass, stepm, weightopt, \
15349: model,imx,jmin,jmax,jmean,rfileres,popforecast,mobilav,prevfcast,mobilavproj,prevbcast, estepm, \
15350: jprev1,mprev1,anprev1,dateprev1, dateprojd, datebackd,jprev2,mprev2,anprev2,dateprev2,dateprojf, datebackf);
15351:
15352: /*------------ free_vector -------------*/
15353: /* chdir(path); */
15354:
15355: /* free_ivector(wav,1,imx); */ /* Moved after last prevalence call */
15356: /* free_imatrix(dh,1,lastpass-firstpass+2,1,imx); */
15357: /* free_imatrix(bh,1,lastpass-firstpass+2,1,imx); */
15358: /* free_imatrix(mw,1,lastpass-firstpass+2,1,imx); */
15359: free_lvector(num,firstobs,lastobs);
15360: free_vector(agedc,firstobs,lastobs);
15361: /*free_matrix(covar,0,NCOVMAX,1,n);*/
15362: /*free_matrix(covar,1,NCOVMAX,1,n);*/
15363: fclose(ficparo);
15364: fclose(ficres);
15365:
15366:
15367: /* Other results (useful)*/
15368:
15369:
15370: /*--------------- Prevalence limit (period or stable prevalence) --------------*/
15371: /*#include "prevlim.h"*/ /* Use ficrespl, ficlog */
15372: prlim=matrix(1,nlstate,1,nlstate);
15373: /* Computes the prevalence limit for each combination k of the dummy covariates by calling prevalim(k) */
15374: prevalence_limit(p, prlim, ageminpar, agemaxpar, ftolpl, &ncvyear);
15375: fclose(ficrespl);
15376:
15377: /*------------- h Pij x at various ages ------------*/
15378: /*#include "hpijx.h"*/
15379: /** h Pij x Probability to be in state j at age x+h being in i at x, for each combination k of dummies in the model line or to nres?*/
15380: /* calls hpxij with combination k */
15381: hPijx(p, bage, fage);
15382: fclose(ficrespij);
15383:
15384: /* ncovcombmax= pow(2,cptcoveff); */
15385: /*-------------- Variance of one-step probabilities for a combination ij or for nres ?---*/
15386: k=1;
15387: varprob(optionfilefiname, matcov, p, delti, nlstate, bage, fage,k,Tvar,nbcode, ncodemax,strstart);
15388:
15389: /* Prevalence for each covariate combination in probs[age][status][cov] */
15390: probs= ma3x(AGEINF,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
15391: for(i=AGEINF;i<=AGESUP;i++)
15392: for(j=1;j<=nlstate+ndeath;j++) /* ndeath is useless but a necessity to be compared with mobaverages */
15393: for(k=1;k<=ncovcombmax;k++)
15394: probs[i][j][k]=0.;
15395: prevalence(probs, ageminpar, agemaxpar, s, agev, nlstate, imx, Tvar, nbcode,
15396: ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
15397: if (mobilav!=0 ||mobilavproj !=0 ) {
15398: mobaverages= ma3x(AGEINF, AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
15399: for(i=AGEINF;i<=AGESUP;i++)
15400: for(j=1;j<=nlstate+ndeath;j++)
15401: for(k=1;k<=ncovcombmax;k++)
15402: mobaverages[i][j][k]=0.;
15403: mobaverage=mobaverages;
15404: if (mobilav!=0) {
15405: printf("Movingaveraging observed prevalence\n");
15406: fprintf(ficlog,"Movingaveraging observed prevalence\n");
15407: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilav)!=0){
15408: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
15409: printf(" Error in movingaverage mobilav=%d\n",mobilav);
15410: }
15411: } else if (mobilavproj !=0) {
15412: printf("Movingaveraging projected observed prevalence\n");
15413: fprintf(ficlog,"Movingaveraging projected observed prevalence\n");
15414: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilavproj)!=0){
15415: fprintf(ficlog," Error in movingaverage mobilavproj=%d\n",mobilavproj);
15416: printf(" Error in movingaverage mobilavproj=%d\n",mobilavproj);
15417: }
15418: }else{
15419: printf("Internal error moving average\n");
15420: fflush(stdout);
15421: exit(1);
15422: }
15423: }/* end if moving average */
15424:
15425: /*---------- Forecasting ------------------*/
15426: if(prevfcast==1){
15427: /* /\* if(stepm ==1){*\/ */
15428: /* /\* anproj1, mproj1, jproj1 either read explicitly or yrfproj *\/ */
15429: /*This done previously after freqsummary.*/
15430: /* dateprojd=(jproj1+12*mproj1+365*anproj1)/365; */
15431: /* dateprojf=(jproj2+12*mproj2+365*anproj2)/365; */
15432:
15433: /* } else if (prvforecast==2){ */
15434: /* /\* if(stepm ==1){*\/ */
15435: /* /\* anproj1, mproj1, jproj1 either read explicitly or yrfproj *\/ */
15436: /* } */
15437: /*prevforecast(fileresu, dateintmean, anproj1, mproj1, jproj1, agemin, agemax, dateprev1, dateprev2, mobilavproj, mobaverage, bage, fage, firstpass, lastpass, anproj2, p, cptcoveff);*/
15438: prevforecast(fileresu,dateintmean, dateprojd, dateprojf, agemin, agemax, dateprev1, dateprev2, mobilavproj, mobaverage, bage, fage, firstpass, lastpass, p, cptcoveff);
15439: }
15440:
15441: /* Prevbcasting */
15442: if(prevbcast==1){
15443: ddnewms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
15444: ddoldms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
15445: ddsavms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
15446:
15447: /*--------------- Back Prevalence limit (period or stable prevalence) --------------*/
15448:
15449: bprlim=matrix(1,nlstate,1,nlstate);
15450:
15451: back_prevalence_limit(p, bprlim, ageminpar, agemaxpar, ftolpl, &ncvyear, dateprev1, dateprev2, firstpass, lastpass, mobilavproj);
15452: fclose(ficresplb);
15453:
15454: hBijx(p, bage, fage, mobaverage);
15455: fclose(ficrespijb);
15456:
15457: /* /\* prevbackforecast(fileresu, mobaverage, anback1, mback1, jback1, agemin, agemax, dateprev1, dateprev2, *\/ */
15458: /* /\* mobilavproj, bage, fage, firstpass, lastpass, anback2, p, cptcoveff); *\/ */
15459: /* prevbackforecast(fileresu, mobaverage, anback1, mback1, jback1, agemin, agemax, dateprev1, dateprev2, */
15460: /* mobilavproj, bage, fage, firstpass, lastpass, anback2, p, cptcoveff); */
15461: prevbackforecast(fileresu, mobaverage, dateintmean, dateprojd, dateprojf, agemin, agemax, dateprev1, dateprev2,
15462: mobilavproj, bage, fage, firstpass, lastpass, p, cptcoveff);
15463:
15464:
15465: varbprlim(fileresu, nresult, mobaverage, mobilavproj, bage, fage, bprlim, &ncvyear, ftolpl, p, matcov, delti, stepm, cptcoveff);
15466:
15467:
15468: free_matrix(bprlim,1,nlstate,1,nlstate); /*here or after loop ? */
15469: free_matrix(ddnewms, 1, nlstate+ndeath, 1, nlstate+ndeath);
15470: free_matrix(ddsavms, 1, nlstate+ndeath, 1, nlstate+ndeath);
15471: free_matrix(ddoldms, 1, nlstate+ndeath, 1, nlstate+ndeath);
15472: } /* end Prevbcasting */
15473:
15474:
15475: /* ------ Other prevalence ratios------------ */
15476:
15477: free_ivector(wav,1,imx);
15478: free_imatrix(dh,1,lastpass-firstpass+2,1,imx);
15479: free_imatrix(bh,1,lastpass-firstpass+2,1,imx);
15480: free_imatrix(mw,1,lastpass-firstpass+2,1,imx);
15481:
15482:
15483: /*---------- Health expectancies, no variances ------------*/
15484:
15485: strcpy(filerese,"E_");
15486: strcat(filerese,fileresu);
15487: if((ficreseij=fopen(filerese,"w"))==NULL) {
15488: printf("Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
15489: fprintf(ficlog,"Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
15490: }
15491: printf("Computing Health Expectancies: result on file '%s' ...", filerese);fflush(stdout);
15492: fprintf(ficlog,"Computing Health Expectancies: result on file '%s' ...", filerese);fflush(ficlog);
15493:
15494: pstamp(ficreseij);
15495:
15496: /* i1=pow(2,cptcoveff); /\* Number of combination of dummy covariates *\/ */
15497: /* if (cptcovn < 1){i1=1;} */
15498:
15499: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
15500: /* for(k=1; k<=i1;k++){ /\* For any combination of dummy covariates, fixed and varying *\/ */
15501: /* if(i1 != 1 && TKresult[nres]!= k) */
15502: /* continue; */
15503: fprintf(ficreseij,"\n#****** ");
15504: printf("\n#****** ");
15505: for(j=1;j<=cptcovs;j++){
15506: /* for(j=1;j<=cptcoveff;j++) { */
15507: /* fprintf(ficreseij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
15508: fprintf(ficreseij," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
15509: printf(" V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
15510: /* printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
15511: }
15512: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
15513: printf(" V%d=%lg ",TvarsQ[j], TinvDoQresult[nres][TvarsQ[j]]); /* TvarsQ[j] gives the name of the jth quantitative (fixed or time v) */
15514: fprintf(ficreseij,"V%d=%lg ",TvarsQ[j], TinvDoQresult[nres][TvarsQ[j]]);
15515: }
15516: fprintf(ficreseij,"******\n");
15517: printf("******\n");
15518:
15519: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
15520: oldm=oldms;savm=savms;
15521: /* printf("HELLO Entering evsij bage=%d fage=%d k=%d estepm=%d nres=%d\n",(int) bage, (int)fage, k, estepm, nres); */
15522: evsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, strstart, nres);
15523:
15524: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
15525: }
15526: fclose(ficreseij);
15527: printf("done evsij\n");fflush(stdout);
15528: fprintf(ficlog,"done evsij\n");fflush(ficlog);
15529:
15530:
15531: /*---------- State-specific expectancies and variances ------------*/
15532: /* Should be moved in a function */
15533: strcpy(filerest,"T_");
15534: strcat(filerest,fileresu);
15535: if((ficrest=fopen(filerest,"w"))==NULL) {
15536: printf("Problem with total LE resultfile: %s\n", filerest);goto end;
15537: fprintf(ficlog,"Problem with total LE resultfile: %s\n", filerest);goto end;
15538: }
15539: printf("Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(stdout);
15540: fprintf(ficlog,"Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(ficlog);
15541: strcpy(fileresstde,"STDE_");
15542: strcat(fileresstde,fileresu);
15543: if((ficresstdeij=fopen(fileresstde,"w"))==NULL) {
15544: printf("Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
15545: fprintf(ficlog,"Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
15546: }
15547: printf(" Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde);
15548: fprintf(ficlog," Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde);
15549:
15550: strcpy(filerescve,"CVE_");
15551: strcat(filerescve,fileresu);
15552: if((ficrescveij=fopen(filerescve,"w"))==NULL) {
15553: printf("Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0);
15554: fprintf(ficlog,"Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0);
15555: }
15556: printf(" Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve);
15557: fprintf(ficlog," Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve);
15558:
15559: strcpy(fileresv,"V_");
15560: strcat(fileresv,fileresu);
15561: if((ficresvij=fopen(fileresv,"w"))==NULL) {
15562: printf("Problem with variance resultfile: %s\n", fileresv);exit(0);
15563: fprintf(ficlog,"Problem with variance resultfile: %s\n", fileresv);exit(0);
15564: }
15565: printf(" Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(stdout);
15566: fprintf(ficlog," Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(ficlog);
15567:
15568: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
15569: if (cptcovn < 1){i1=1;}
15570:
15571: for(nres=1; nres <= nresult; nres++) /* For each resultline, find the combination and output results according to the values of dummies and then quanti. */
15572: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying. For each nres and each value at position k
15573: * we know Tresult[nres][result_position]= value of the dummy variable at the result_position in the nres resultline
15574: * Tvqresult[nres][result_position]= id of the variable at the result_position in the nres resultline
15575: * and Tqresult[nres][result_position]= value of the variable at the result_position in the nres resultline */
15576: /* */
15577: if(i1 != 1 && TKresult[nres]!= k) /* TKresult[nres] is the combination of this nres resultline. All the i1 combinations are not output */
15578: continue;
15579: printf("\n# model=1+age+%s \n#****** Result for:", model); /* HERE model is empty */
15580: fprintf(ficrest,"\n# model=1+age+%s \n#****** Result for:", model);
15581: fprintf(ficlog,"\n# model=1+age+%s \n#****** Result for:", model);
15582: /* It might not be a good idea to mix dummies and quantitative */
15583: /* for(j=1;j<=cptcoveff;j++){ /\* j=resultpos. Could be a loop on cptcovs: number of single dummy covariate in the result line as well as in the model *\/ */
15584: for(j=1;j<=cptcovs;j++){ /* j=resultpos. Could be a loop on cptcovs: number of single covariate (dummy or quantitative) in the result line as well as in the model */
15585: /* printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); /\* Output by variables in the resultline *\/ */
15586: /* Tvaraff[j] is the name of the dummy variable in position j in the equation model:
15587: * Tvaraff[1]@9={4, 3, 0, 0, 0, 0, 0, 0, 0}, in model=V5+V4+V3+V4*V3+V5*age
15588: * (V5 is quanti) V4 and V3 are dummies
15589: * TnsdVar[4] is the position 1 and TnsdVar[3]=2 in codtabm(k,l)(V4 V3)=V4 V3
15590: * l=1 l=2
15591: * k=1 1 1 0 0
15592: * k=2 2 1 1 0
15593: * k=3 [1] [2] 0 1
15594: * k=4 2 2 1 1
15595: * If nres=1 result: V3=1 V4=0 then k=3 and outputs
15596: * If nres=2 result: V4=1 V3=0 then k=2 and outputs
15597: * nres=1 =>k=3 j=1 V4= nbcode[4][codtabm(3,1)=1)=0; j=2 V3= nbcode[3][codtabm(3,2)=2]=1
15598: * nres=2 =>k=2 j=1 V4= nbcode[4][codtabm(2,1)=2)=1; j=2 V3= nbcode[3][codtabm(2,2)=1]=0
15599: */
15600: /* Tvresult[nres][j] Name of the variable at position j in this resultline */
15601: /* Tresult[nres][j] Value of this variable at position j could be a float if quantitative */
15602: /* We give up with the combinations!! */
15603: /* if(debugILK) */
15604: /* printf("\n j=%d In computing T_ Dummy[modelresult[%d][%d]]=%d, modelresult[%d][%d]=%d cptcovs=%d, cptcoveff=%d Fixed[modelresult[nres][j]]=%d\n", j, nres, j, Dummy[modelresult[nres][j]],nres,j,modelresult[nres][j],cptcovs, cptcoveff,Fixed[modelresult[nres][j]]); /\* end if dummy or quanti *\/ */
15605:
15606: if(Dummy[modelresult[nres][j]]==0){/* Dummy variable of the variable in position modelresult in the model corresponding to j in resultline */
15607: /* printf("V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][j]); /\* Output of each value for the combination TKresult[nres], ordere by the covariate values in the resultline *\/ */ /* TinvDoQresult[nres][Name of the variable] */
15608: printf("V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]); /* Output of each value for the combination TKresult[nres], ordered by the covariate values in the resultline */
15609: fprintf(ficlog,"V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]); /* Output of each value for the combination TKresult[nres], ordere by the covariate values in the resultline */
15610: fprintf(ficrest,"V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]); /* Output of each value for the combination TKresult[nres], ordere by the covariate values in the resultline */
15611: if(Fixed[modelresult[nres][j]]==0){ /* Fixed */
15612: printf("fixed ");fprintf(ficlog,"fixed ");fprintf(ficrest,"fixed ");
15613: }else{
15614: printf("varyi ");fprintf(ficlog,"varyi ");fprintf(ficrest,"varyi ");
15615: }
15616: /* fprintf(ficrest,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
15617: /* fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
15618: }else if(Dummy[modelresult[nres][j]]==1){ /* Quanti variable */
15619: /* For each selected (single) quantitative value */
15620: printf(" V%d=%lg ",Tvqresult[nres][j],Tqresult[nres][j]);
15621: fprintf(ficlog," V%d=%lg ",Tvqresult[nres][j],Tqresult[nres][j]);
15622: fprintf(ficrest," V%d=%lg ",Tvqresult[nres][j],Tqresult[nres][j]);
15623: if(Fixed[modelresult[nres][j]]==0){ /* Fixed */
15624: printf("fixed ");fprintf(ficlog,"fixed ");fprintf(ficrest,"fixed ");
15625: }else{
15626: printf("varyi ");fprintf(ficlog,"varyi ");fprintf(ficrest,"varyi ");
15627: }
15628: }else{
15629: printf("Error in computing T_ Dummy[modelresult[%d][%d]]=%d, modelresult[%d][%d]=%d cptcovs=%d, cptcoveff=%d \n", nres, j, Dummy[modelresult[nres][j]],nres,j,modelresult[nres][j],cptcovs, cptcoveff); /* end if dummy or quanti */
15630: fprintf(ficlog,"Error in computing T_ Dummy[modelresult[%d][%d]]=%d, modelresult[%d][%d]=%d cptcovs=%d, cptcoveff=%d \n", nres, j, Dummy[modelresult[nres][j]],nres,j,modelresult[nres][j],cptcovs, cptcoveff); /* end if dummy or quanti */
15631: exit(1);
15632: }
15633: } /* End loop for each variable in the resultline */
15634: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
15635: /* printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); /\* Wrong j is not in the equation model *\/ */
15636: /* fprintf(ficrest," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
15637: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
15638: /* } */
15639: fprintf(ficrest,"******\n");
15640: fprintf(ficlog,"******\n");
15641: printf("******\n");
15642:
15643: fprintf(ficresstdeij,"\n#****** ");
15644: fprintf(ficrescveij,"\n#****** ");
15645: /* It could have been: for(j=1;j<=cptcoveff;j++) {printf("V=%d=%lg",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);} */
15646: /* But it won't be sorted and depends on how the resultline is ordered */
15647: for(j=1;j<=cptcoveff;j++) {
15648: fprintf(ficresstdeij,"V%d=%d ",Tvresult[nres][j],Tresult[nres][j]);
15649: /* fprintf(ficresstdeij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
15650: /* fprintf(ficrescveij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
15651: }
15652: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value, TvarsQind gives the position of a quantitative in model equation */
15653: fprintf(ficresstdeij," V%d=%lg ",Tvar[TvarsQind[j]],Tqresult[nres][resultmodel[nres][TvarsQind[j]]]);
15654: fprintf(ficrescveij," V%d=%lg ",Tvar[TvarsQind[j]],Tqresult[nres][resultmodel[nres][TvarsQind[j]]]);
15655: }
15656: fprintf(ficresstdeij,"******\n");
15657: fprintf(ficrescveij,"******\n");
15658:
15659: fprintf(ficresvij,"\n#****** ");
15660: /* pstamp(ficresvij); */
15661: for(j=1;j<=cptcoveff;j++)
15662: fprintf(ficresvij,"V%d=%d ",Tvresult[nres][j],Tresult[nres][j]);
15663: /* fprintf(ficresvij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[TnsdVar[Tvaraff[j]]])]); */
15664: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
15665: /* fprintf(ficresvij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]); /\* To solve *\/ */
15666: fprintf(ficresvij," V%d=%lg ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); /* Solved */
15667: }
15668: fprintf(ficresvij,"******\n");
15669:
15670: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
15671: oldm=oldms;savm=savms;
15672: printf(" cvevsij ");
15673: fprintf(ficlog, " cvevsij ");
15674: cvevsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, delti, matcov, strstart, nres);
15675: printf(" end cvevsij \n ");
15676: fprintf(ficlog, " end cvevsij \n ");
15677:
15678: /*
15679: */
15680: /* goto endfree; */
15681:
15682: vareij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
15683: pstamp(ficrest);
15684:
15685: epj=vector(1,nlstate+1);
15686: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
15687: oldm=oldms;savm=savms; /* ZZ Segmentation fault */
15688: cptcod= 0; /* To be deleted */
15689: printf("varevsij vpopbased=%d \n",vpopbased);
15690: fprintf(ficlog, "varevsij vpopbased=%d \n",vpopbased);
15691: 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 */
15692: 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 ");
15693: if(vpopbased==1)
15694: 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);
15695: else
15696: fprintf(ficrest,"the age specific forward period (stable) prevalences in each health state \n");
15697: fprintf(ficrest,"# Age popbased mobilav e.. (std) "); /* Adding covariate values? */
15698: for (i=1;i<=nlstate;i++) fprintf(ficrest,"e.%d (std) ",i);
15699: fprintf(ficrest,"\n");
15700: /* printf("Which p?\n"); for(i=1;i<=npar;i++)printf("p[i=%d]=%lf,",i,p[i]);printf("\n"); */
15701: printf("Computing age specific forward period (stable) prevalences in each health state \n");
15702: fprintf(ficlog,"Computing age specific forward period (stable) prevalences in each health state \n");
15703: for(age=bage; age <=fage ;age++){
15704: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, &ncvyear, k, nres); /*ZZ Is it the correct prevalim */
15705: if (vpopbased==1) {
15706: if(mobilav ==0){
15707: for(i=1; i<=nlstate;i++)
15708: prlim[i][i]=probs[(int)age][i][k];
15709: }else{ /* mobilav */
15710: for(i=1; i<=nlstate;i++)
15711: prlim[i][i]=mobaverage[(int)age][i][k];
15712: }
15713: }
15714:
15715: fprintf(ficrest," %4.0f %d %d",age, vpopbased, mobilav);
15716: /* fprintf(ficrest," %4.0f %d %d %d %d",age, vpopbased, mobilav,Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */ /* to be done */
15717: /* printf(" age %4.0f ",age); */
15718: for(j=1, epj[nlstate+1]=0.;j <=nlstate;j++){
15719: for(i=1, epj[j]=0.;i <=nlstate;i++) {
15720: epj[j] += prlim[i][i]*eij[i][j][(int)age];
15721: /*ZZZ printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]);*/
15722: /* printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]); */
15723: }
15724: epj[nlstate+1] +=epj[j];
15725: }
15726: /* printf(" age %4.0f \n",age); */
15727:
15728: for(i=1, vepp=0.;i <=nlstate;i++)
15729: for(j=1;j <=nlstate;j++)
15730: vepp += vareij[i][j][(int)age];
15731: fprintf(ficrest," %7.3f (%7.3f)", epj[nlstate+1],sqrt(vepp));
15732: for(j=1;j <=nlstate;j++){
15733: fprintf(ficrest," %7.3f (%7.3f)", epj[j],sqrt(vareij[j][j][(int)age]));
15734: }
15735: fprintf(ficrest,"\n");
15736: }
15737: } /* End vpopbased */
15738: free_vector(epj,1,nlstate+1);
15739: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
15740: free_ma3x(vareij,1,nlstate,1,nlstate,(int) bage, (int)fage);
15741: printf("done selection\n");fflush(stdout);
15742: fprintf(ficlog,"done selection\n");fflush(ficlog);
15743:
15744: } /* End k selection or end covariate selection for nres */
15745:
15746: printf("done State-specific expectancies\n");fflush(stdout);
15747: fprintf(ficlog,"done State-specific expectancies\n");fflush(ficlog);
15748:
15749: /* variance-covariance of forward period prevalence */
15750: varprlim(fileresu, nresult, mobaverage, mobilavproj, bage, fage, prlim, &ncvyear, ftolpl, p, matcov, delti, stepm, cptcoveff);
15751:
15752:
15753: free_vector(weight,firstobs,lastobs);
15754: free_imatrix(Tvardk,0,NCOVMAX,1,2);
15755: free_imatrix(Tvard,1,NCOVMAX,1,2);
15756: free_imatrix(s,1,maxwav+1,firstobs,lastobs);
15757: free_matrix(anint,1,maxwav,firstobs,lastobs);
15758: free_matrix(mint,1,maxwav,firstobs,lastobs);
15759: free_ivector(cod,firstobs,lastobs);
15760: free_ivector(tab,1,NCOVMAX);
15761: fclose(ficresstdeij);
15762: fclose(ficrescveij);
15763: fclose(ficresvij);
15764: fclose(ficrest);
15765: fclose(ficpar);
15766:
15767:
15768: /*---------- End : free ----------------*/
15769: if (mobilav!=0 ||mobilavproj !=0)
15770: free_ma3x(mobaverages,AGEINF, AGESUP,1,nlstate+ndeath, 1,ncovcombmax); /* We need to have a squared matrix with prevalence of the dead! */
15771: free_ma3x(probs,AGEINF,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
15772: free_matrix(prlim,1,nlstate,1,nlstate); /*here or after loop ? */
15773: free_matrix(pmmij,1,nlstate+ndeath,1,nlstate+ndeath);
15774: } /* mle==-3 arrives here for freeing */
15775: /* endfree:*/
15776: if(mle!=-3) free_matrix(precov, 1,MAXRESULTLINESPONE,1,NCOVMAX+1); /* Could be elsewhere ?*/
15777: free_matrix(oldms, 1,nlstate+ndeath,1,nlstate+ndeath);
15778: free_matrix(newms, 1,nlstate+ndeath,1,nlstate+ndeath);
15779: free_matrix(savms, 1,nlstate+ndeath,1,nlstate+ndeath);
15780: /* if(ntv+nqtv>=1)free_ma3x(cotvar,1,maxwav,1,ntv+nqtv,firstobs,lastobs); */
15781: if(ntv+nqtv>=1)free_ma3x(cotvar,1,maxwav,ncovcol+nqv+1,ncovcol+nqv+ntv+nqtv,firstobs,lastobs);
15782: if(nqtv>=1)free_ma3x(cotqvar,1,maxwav,1,nqtv,firstobs,lastobs);
15783: if(nqv>=1)free_matrix(coqvar,1,nqv,firstobs,lastobs);
15784: free_matrix(covar,0,NCOVMAX,firstobs,lastobs);
15785: free_matrix(matcov,1,npar,1,npar);
15786: free_matrix(hess,1,npar,1,npar);
15787: /*free_vector(delti,1,npar);*/
15788: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
15789: free_matrix(agev,1,maxwav,1,imx);
15790: free_ma3x(paramstart,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
15791: free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
15792:
15793: free_ivector(ncodemax,1,NCOVMAX);
15794: free_ivector(ncodemaxwundef,1,NCOVMAX);
15795: free_ivector(Dummy,-1,NCOVMAX);
15796: free_ivector(Fixed,-1,NCOVMAX);
15797: free_ivector(DummyV,-1,NCOVMAX);
15798: free_ivector(FixedV,-1,NCOVMAX);
15799: free_ivector(Typevar,-1,NCOVMAX);
15800: free_ivector(Tvar,1,NCOVMAX);
15801: free_ivector(TvarsQ,1,NCOVMAX);
15802: free_ivector(TvarsQind,1,NCOVMAX);
15803: free_ivector(TvarsD,1,NCOVMAX);
15804: free_ivector(TnsdVar,1,NCOVMAX);
15805: free_ivector(TvarsDind,1,NCOVMAX);
15806: free_ivector(TvarFD,1,NCOVMAX);
15807: free_ivector(TvarFDind,1,NCOVMAX);
15808: free_ivector(TvarF,1,NCOVMAX);
15809: free_ivector(TvarFind,1,NCOVMAX);
15810: free_ivector(TvarV,1,NCOVMAX);
15811: free_ivector(TvarVind,1,NCOVMAX);
15812: free_ivector(TvarA,1,NCOVMAX);
15813: free_ivector(TvarAind,1,NCOVMAX);
15814: free_ivector(TvarFQ,1,NCOVMAX);
15815: free_ivector(TvarFQind,1,NCOVMAX);
15816: free_ivector(TvarVD,1,NCOVMAX);
15817: free_ivector(TvarVDind,1,NCOVMAX);
15818: free_ivector(TvarVQ,1,NCOVMAX);
15819: free_ivector(TvarVQind,1,NCOVMAX);
15820: free_ivector(TvarAVVA,1,NCOVMAX);
15821: free_ivector(TvarAVVAind,1,NCOVMAX);
15822: free_ivector(TvarVVA,1,NCOVMAX);
15823: free_ivector(TvarVVAind,1,NCOVMAX);
15824: free_ivector(TvarVV,1,NCOVMAX);
15825: free_ivector(TvarVVind,1,NCOVMAX);
15826:
15827: free_ivector(Tvarsel,1,NCOVMAX);
15828: free_vector(Tvalsel,1,NCOVMAX);
15829: free_ivector(Tposprod,1,NCOVMAX);
15830: free_ivector(Tprod,1,NCOVMAX);
15831: free_ivector(Tvaraff,1,NCOVMAX);
15832: free_ivector(invalidvarcomb,0,ncovcombmax);
15833: free_ivector(Tage,1,NCOVMAX);
15834: free_ivector(Tmodelind,1,NCOVMAX);
15835: free_ivector(TmodelInvind,1,NCOVMAX);
15836: free_ivector(TmodelInvQind,1,NCOVMAX);
15837:
15838: free_matrix(precov, 1,MAXRESULTLINESPONE,1,NCOVMAX+1); /* Could be elsewhere ?*/
15839:
15840: free_imatrix(nbcode,0,NCOVMAX,0,NCOVMAX);
15841: /* free_imatrix(codtab,1,100,1,10); */
15842: fflush(fichtm);
15843: fflush(ficgp);
15844:
15845:
15846: if((nberr >0) || (nbwarn>0)){
15847: printf("End of Imach with %d errors and/or %d warnings. Please look at the log file for details.\n",nberr,nbwarn);
15848: fprintf(ficlog,"End of Imach with %d errors and/or warnings %d. Please look at the log file for details.\n",nberr,nbwarn);
15849: }else{
15850: printf("End of Imach\n");
15851: fprintf(ficlog,"End of Imach\n");
15852: }
15853: printf("See log file on %s\n",filelog);
15854: /* gettimeofday(&end_time, (struct timezone*)0);*/ /* after time */
15855: /*(void) gettimeofday(&end_time,&tzp);*/
15856: rend_time = time(NULL);
15857: end_time = *localtime(&rend_time);
15858: /* tml = *localtime(&end_time.tm_sec); */
15859: strcpy(strtend,asctime(&end_time));
15860: printf("Local time at start %s\nLocal time at end %s",strstart, strtend);
15861: fprintf(ficlog,"Local time at start %s\nLocal time at end %s\n",strstart, strtend);
15862: printf("Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
15863:
15864: printf("Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
15865: fprintf(ficlog,"Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
15866: fprintf(ficlog,"Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
15867: /* printf("Total time was %d uSec.\n", total_usecs);*/
15868: /* if(fileappend(fichtm,optionfilehtm)){ */
15869: fprintf(fichtm,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
15870: fclose(fichtm);
15871: fprintf(fichtmcov,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
15872: fclose(fichtmcov);
15873: fclose(ficgp);
15874: fclose(ficlog);
15875: /*------ End -----------*/
15876:
15877:
15878: /* Executes gnuplot */
15879:
15880: printf("Before Current directory %s!\n",pathcd);
15881: #ifdef WIN32
15882: if (_chdir(pathcd) != 0)
15883: printf("Can't move to directory %s!\n",path);
15884: if(_getcwd(pathcd,MAXLINE) > 0)
15885: #else
15886: if(chdir(pathcd) != 0)
15887: printf("Can't move to directory %s!\n", path);
15888: if (getcwd(pathcd, MAXLINE) > 0)
15889: #endif
15890: printf("Current directory %s!\n",pathcd);
15891: /*strcat(plotcmd,CHARSEPARATOR);*/
15892: sprintf(plotcmd,"gnuplot");
15893: #ifdef _WIN32
15894: sprintf(plotcmd,"\"%sgnuplot.exe\"",pathimach);
15895: #endif
15896: if(!stat(plotcmd,&info)){
15897: printf("Error or gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
15898: if(!stat(getenv("GNUPLOTBIN"),&info)){
15899: printf("Error or gnuplot program not found: '%s' Environment GNUPLOTBIN not set.\n",plotcmd);fflush(stdout);
15900: }else
15901: strcpy(pplotcmd,plotcmd);
15902: #ifdef __unix
15903: strcpy(plotcmd,GNUPLOTPROGRAM);
15904: if(!stat(plotcmd,&info)){
15905: printf("Error gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
15906: }else
15907: strcpy(pplotcmd,plotcmd);
15908: #endif
15909: }else
15910: strcpy(pplotcmd,plotcmd);
15911:
15912: sprintf(plotcmd,"%s %s",pplotcmd, optionfilegnuplot);
15913: printf("Starting graphs with: '%s'\n",plotcmd);fflush(stdout);
15914: strcpy(pplotcmd,plotcmd);
15915:
15916: if((outcmd=system(plotcmd)) != 0){
15917: printf("Error in gnuplot, command might not be in your path: '%s', err=%d\n", plotcmd, outcmd);
15918: printf("\n Trying if gnuplot resides on the same directory that IMaCh\n");
15919: sprintf(plotcmd,"%sgnuplot %s", pathimach, optionfilegnuplot);
15920: if((outcmd=system(plotcmd)) != 0){
15921: printf("\n Still a problem with gnuplot command %s, err=%d\n", plotcmd, outcmd);
15922: strcpy(plotcmd,pplotcmd);
15923: }
15924: }
15925: printf(" Successful, please wait...");
15926: while (z[0] != 'q') {
15927: /* chdir(path); */
15928: printf("\nType e to edit results with your browser, g to graph again and q for exit: ");
15929: scanf("%s",z);
15930: /* if (z[0] == 'c') system("./imach"); */
15931: if (z[0] == 'e') {
15932: #ifdef __APPLE__
15933: sprintf(pplotcmd, "open %s", optionfilehtm);
15934: #elif __linux
15935: sprintf(pplotcmd, "xdg-open %s", optionfilehtm);
15936: #else
15937: sprintf(pplotcmd, "%s", optionfilehtm);
15938: #endif
15939: printf("Starting browser with: %s",pplotcmd);fflush(stdout);
15940: system(pplotcmd);
15941: }
15942: else if (z[0] == 'g') system(plotcmd);
15943: else if (z[0] == 'q') exit(0);
15944: }
15945: end:
15946: while (z[0] != 'q') {
15947: printf("\nType q for exiting: "); fflush(stdout);
15948: scanf("%s",z);
15949: }
15950: printf("End\n");
15951: exit(0);
15952: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>