File:
[Local Repository] /
imach /
src /
imach.c
Revision
1.348:
download - view:
text,
annotated -
select for diffs
Tue Sep 20 00:01:38 2022 UTC (23 months ago) by
brouard
Branches:
MAIN
CVS tags:
HEAD
Summary: version 0.99r43
* imach.c (Module): Version 0.99r42 needed a newer version of
Gnuplot. But newer version 0.99r43 should run with the Gnuplot
version 5.0 or 5.1 distributed with IMaCh.
1: /* $Id: imach.c,v 1.348 2022/09/20 00:01:38 brouard Exp $
2: $State: Exp $
3: $Log: imach.c,v $
4: Revision 1.348 2022/09/20 00:01:38 brouard
5: Summary: version 0.99r43
6:
7: * imach.c (Module): Version 0.99r42 needed a newer version of
8: Gnuplot. But newer version 0.99r43 should run with the Gnuplot
9: version 5.0 or 5.1 distributed with IMaCh.
10:
11: Revision 1.347 2022/09/18 14:36:44 brouard
12: Summary: version 0.99r42
13:
14: Revision 1.346 2022/09/16 13:52:36 brouard
15: * src/imach.c (Module): 0.99r41 Was an error when product of timevarying and fixed. Using FixedV[of name] now. Thank you Feinuo
16:
17: Revision 1.345 2022/09/16 13:40:11 brouard
18: Summary: Version 0.99r41
19:
20: * imach.c (Module): 0.99r41 Was an error when product of timevarying and fixed. Using FixedV[of name] now. Thank you Feinuo
21:
22: Revision 1.344 2022/09/14 19:33:30 brouard
23: Summary: version 0.99r40
24:
25: * imach.c (Module): Fixing names of variables in T_ (thanks to Feinuo)
26:
27: Revision 1.343 2022/09/14 14:22:16 brouard
28: Summary: version 0.99r39
29:
30: * imach.c (Module): Version 0.99r39 with colored dummy covariates
31: (fixed or time varying), using new last columns of
32: ILK_parameter.txt file.
33:
34: Revision 1.342 2022/09/11 19:54:09 brouard
35: Summary: 0.99r38
36:
37: * imach.c (Module): Adding timevarying products of any kinds,
38: should work before shifting cotvar from ncovcol+nqv columns in
39: order to have a correspondance between the column of cotvar and
40: the id of column.
41: (Module): Some cleaning and adding covariates in ILK.txt
42:
43: Revision 1.341 2022/09/11 07:58:42 brouard
44: Summary: Version 0.99r38
45:
46: After adding change in cotvar.
47:
48: Revision 1.340 2022/09/11 07:53:11 brouard
49: Summary: Version imach 0.99r37
50:
51: * imach.c (Module): Adding timevarying products of any kinds,
52: should work before shifting cotvar from ncovcol+nqv columns in
53: order to have a correspondance between the column of cotvar and
54: the id of column.
55:
56: Revision 1.339 2022/09/09 17:55:22 brouard
57: Summary: version 0.99r37
58:
59: * imach.c (Module): Many improvements for fixing products of fixed
60: timevarying as well as fixed * fixed, and test with quantitative
61: covariate.
62:
63: Revision 1.338 2022/09/04 17:40:33 brouard
64: Summary: 0.99r36
65:
66: * imach.c (Module): Now the easy runs i.e. without result or
67: model=1+age only did not work. The defautl combination should be 1
68: and not 0 because everything hasn't been tranformed yet.
69:
70: Revision 1.337 2022/09/02 14:26:02 brouard
71: Summary: version 0.99r35
72:
73: * src/imach.c: Version 0.99r35 because it outputs same results with
74: 1+age+V1+V1*age for females and 1+age for females only
75: (education=1 noweight)
76:
77: Revision 1.336 2022/08/31 09:52:36 brouard
78: *** empty log message ***
79:
80: Revision 1.335 2022/08/31 08:23:16 brouard
81: Summary: improvements...
82:
83: Revision 1.334 2022/08/25 09:08:41 brouard
84: Summary: In progress for quantitative
85:
86: Revision 1.333 2022/08/21 09:10:30 brouard
87: * src/imach.c (Module): Version 0.99r33 A lot of changes in
88: reassigning covariates: my first idea was that people will always
89: use the first covariate V1 into the model but in fact they are
90: producing data with many covariates and can use an equation model
91: with some of the covariate; it means that in a model V2+V3 instead
92: of codtabm(k,Tvaraff[j]) which calculates for combination k, for
93: three covariates (V1, V2, V3) the value of Tvaraff[j], but in fact
94: the equation model is restricted to two variables only (V2, V3)
95: and the combination for V2 should be codtabm(k,1) instead of
96: (codtabm(k,2), and the code should be
97: codtabm(k,TnsdVar[Tvaraff[j]]. Many many changes have been
98: made. All of these should be simplified once a day like we did in
99: hpxij() for example by using precov[nres] which is computed in
100: decoderesult for each nres of each resultline. Loop should be done
101: on the equation model globally by distinguishing only product with
102: age (which are changing with age) and no more on type of
103: covariates, single dummies, single covariates.
104:
105: Revision 1.332 2022/08/21 09:06:25 brouard
106: Summary: Version 0.99r33
107:
108: * src/imach.c (Module): Version 0.99r33 A lot of changes in
109: reassigning covariates: my first idea was that people will always
110: use the first covariate V1 into the model but in fact they are
111: producing data with many covariates and can use an equation model
112: with some of the covariate; it means that in a model V2+V3 instead
113: of codtabm(k,Tvaraff[j]) which calculates for combination k, for
114: three covariates (V1, V2, V3) the value of Tvaraff[j], but in fact
115: the equation model is restricted to two variables only (V2, V3)
116: and the combination for V2 should be codtabm(k,1) instead of
117: (codtabm(k,2), and the code should be
118: codtabm(k,TnsdVar[Tvaraff[j]]. Many many changes have been
119: made. All of these should be simplified once a day like we did in
120: hpxij() for example by using precov[nres] which is computed in
121: decoderesult for each nres of each resultline. Loop should be done
122: on the equation model globally by distinguishing only product with
123: age (which are changing with age) and no more on type of
124: covariates, single dummies, single covariates.
125:
126: Revision 1.331 2022/08/07 05:40:09 brouard
127: *** empty log message ***
128:
129: Revision 1.330 2022/08/06 07:18:25 brouard
130: Summary: last 0.99r31
131:
132: * imach.c (Module): Version of imach using partly decoderesult to rebuild xpxij function
133:
134: Revision 1.329 2022/08/03 17:29:54 brouard
135: * imach.c (Module): Many errors in graphs fixed with Vn*age covariates.
136:
137: Revision 1.328 2022/07/27 17:40:48 brouard
138: Summary: valgrind bug fixed by initializing to zero DummyV as well as Tage
139:
140: Revision 1.327 2022/07/27 14:47:35 brouard
141: Summary: Still a problem for one-step probabilities in case of quantitative variables
142:
143: Revision 1.326 2022/07/26 17:33:55 brouard
144: Summary: some test with nres=1
145:
146: Revision 1.325 2022/07/25 14:27:23 brouard
147: Summary: r30
148:
149: * imach.c (Module): Error cptcovn instead of nsd in bmij (was
150: coredumped, revealed by Feiuno, thank you.
151:
152: Revision 1.324 2022/07/23 17:44:26 brouard
153: *** empty log message ***
154:
155: Revision 1.323 2022/07/22 12:30:08 brouard
156: * imach.c (Module): Output of Wald test in the htm file and not only in the log.
157:
158: Revision 1.322 2022/07/22 12:27:48 brouard
159: * imach.c (Module): Output of Wald test in the htm file and not only in the log.
160:
161: Revision 1.321 2022/07/22 12:04:24 brouard
162: Summary: r28
163:
164: * imach.c (Module): Output of Wald test in the htm file and not only in the log.
165:
166: Revision 1.320 2022/06/02 05:10:11 brouard
167: *** empty log message ***
168:
169: Revision 1.319 2022/06/02 04:45:11 brouard
170: * imach.c (Module): Adding the Wald tests from the log to the main
171: htm for better display of the maximum likelihood estimators.
172:
173: Revision 1.318 2022/05/24 08:10:59 brouard
174: * imach.c (Module): Some attempts to find a bug of wrong estimates
175: of confidencce intervals with product in the equation modelC
176:
177: Revision 1.317 2022/05/15 15:06:23 brouard
178: * imach.c (Module): Some minor improvements
179:
180: Revision 1.316 2022/05/11 15:11:31 brouard
181: Summary: r27
182:
183: Revision 1.315 2022/05/11 15:06:32 brouard
184: *** empty log message ***
185:
186: Revision 1.314 2022/04/13 17:43:09 brouard
187: * imach.c (Module): Adding link to text data files
188:
189: Revision 1.313 2022/04/11 15:57:42 brouard
190: * imach.c (Module): Error in rewriting the 'r' file with yearsfproj or yearsbproj fixed
191:
192: Revision 1.312 2022/04/05 21:24:39 brouard
193: *** empty log message ***
194:
195: Revision 1.311 2022/04/05 21:03:51 brouard
196: Summary: Fixed quantitative covariates
197:
198: Fixed covariates (dummy or quantitative)
199: with missing values have never been allowed but are ERRORS and
200: program quits. Standard deviations of fixed covariates were
201: wrongly computed. Mean and standard deviations of time varying
202: covariates are still not computed.
203:
204: Revision 1.310 2022/03/17 08:45:53 brouard
205: Summary: 99r25
206:
207: Improving detection of errors: result lines should be compatible with
208: the model.
209:
210: Revision 1.309 2021/05/20 12:39:14 brouard
211: Summary: Version 0.99r24
212:
213: Revision 1.308 2021/03/31 13:11:57 brouard
214: Summary: Version 0.99r23
215:
216:
217: * imach.c (Module): Still bugs in the result loop. Thank to Holly Benett
218:
219: Revision 1.307 2021/03/08 18:11:32 brouard
220: Summary: 0.99r22 fixed bug on result:
221:
222: Revision 1.306 2021/02/20 15:44:02 brouard
223: Summary: Version 0.99r21
224:
225: * imach.c (Module): Fix bug on quitting after result lines!
226: (Module): Version 0.99r21
227:
228: Revision 1.305 2021/02/20 15:28:30 brouard
229: * imach.c (Module): Fix bug on quitting after result lines!
230:
231: Revision 1.304 2021/02/12 11:34:20 brouard
232: * imach.c (Module): The use of a Windows BOM (huge) file is now an error
233:
234: Revision 1.303 2021/02/11 19:50:15 brouard
235: * (Module): imach.c Someone entered 'results:' instead of 'result:'. Now it is an error which is printed.
236:
237: Revision 1.302 2020/02/22 21:00:05 brouard
238: * (Module): imach.c Update mle=-3 (for computing Life expectancy
239: and life table from the data without any state)
240:
241: Revision 1.301 2019/06/04 13:51:20 brouard
242: Summary: Error in 'r'parameter file backcast yearsbproj instead of yearsfproj
243:
244: Revision 1.300 2019/05/22 19:09:45 brouard
245: Summary: version 0.99r19 of May 2019
246:
247: Revision 1.299 2019/05/22 18:37:08 brouard
248: Summary: Cleaned 0.99r19
249:
250: Revision 1.298 2019/05/22 18:19:56 brouard
251: *** empty log message ***
252:
253: Revision 1.297 2019/05/22 17:56:10 brouard
254: Summary: Fix bug by moving date2dmy and nhstepm which gaefin=-1
255:
256: Revision 1.296 2019/05/20 13:03:18 brouard
257: Summary: Projection syntax simplified
258:
259:
260: We can now start projections, forward or backward, from the mean date
261: of inteviews up to or down to a number of years of projection:
262: prevforecast=1 yearsfproj=15.3 mobil_average=0
263: or
264: prevforecast=1 starting-proj-date=1/1/2007 final-proj-date=12/31/2017 mobil_average=0
265: or
266: prevbackcast=1 yearsbproj=12.3 mobil_average=1
267: or
268: prevbackcast=1 starting-back-date=1/10/1999 final-back-date=1/1/1985 mobil_average=1
269:
270: Revision 1.295 2019/05/18 09:52:50 brouard
271: Summary: doxygen tex bug
272:
273: Revision 1.294 2019/05/16 14:54:33 brouard
274: Summary: There was some wrong lines added
275:
276: Revision 1.293 2019/05/09 15:17:34 brouard
277: *** empty log message ***
278:
279: Revision 1.292 2019/05/09 14:17:20 brouard
280: Summary: Some updates
281:
282: Revision 1.291 2019/05/09 13:44:18 brouard
283: Summary: Before ncovmax
284:
285: Revision 1.290 2019/05/09 13:39:37 brouard
286: Summary: 0.99r18 unlimited number of individuals
287:
288: 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.
289:
290: Revision 1.289 2018/12/13 09:16:26 brouard
291: Summary: Bug for young ages (<-30) will be in r17
292:
293: Revision 1.288 2018/05/02 20:58:27 brouard
294: Summary: Some bugs fixed
295:
296: Revision 1.287 2018/05/01 17:57:25 brouard
297: Summary: Bug fixed by providing frequencies only for non missing covariates
298:
299: Revision 1.286 2018/04/27 14:27:04 brouard
300: Summary: some minor bugs
301:
302: Revision 1.285 2018/04/21 21:02:16 brouard
303: Summary: Some bugs fixed, valgrind tested
304:
305: Revision 1.284 2018/04/20 05:22:13 brouard
306: Summary: Computing mean and stdeviation of fixed quantitative variables
307:
308: Revision 1.283 2018/04/19 14:49:16 brouard
309: Summary: Some minor bugs fixed
310:
311: Revision 1.282 2018/02/27 22:50:02 brouard
312: *** empty log message ***
313:
314: Revision 1.281 2018/02/27 19:25:23 brouard
315: Summary: Adding second argument for quitting
316:
317: Revision 1.280 2018/02/21 07:58:13 brouard
318: Summary: 0.99r15
319:
320: New Makefile with recent VirtualBox 5.26. Bug in sqrt negatve in imach.c
321:
322: Revision 1.279 2017/07/20 13:35:01 brouard
323: Summary: temporary working
324:
325: Revision 1.278 2017/07/19 14:09:02 brouard
326: Summary: Bug for mobil_average=0 and prevforecast fixed(?)
327:
328: Revision 1.277 2017/07/17 08:53:49 brouard
329: Summary: BOM files can be read now
330:
331: Revision 1.276 2017/06/30 15:48:31 brouard
332: Summary: Graphs improvements
333:
334: Revision 1.275 2017/06/30 13:39:33 brouard
335: Summary: Saito's color
336:
337: Revision 1.274 2017/06/29 09:47:08 brouard
338: Summary: Version 0.99r14
339:
340: Revision 1.273 2017/06/27 11:06:02 brouard
341: Summary: More documentation on projections
342:
343: Revision 1.272 2017/06/27 10:22:40 brouard
344: Summary: Color of backprojection changed from 6 to 5(yellow)
345:
346: Revision 1.271 2017/06/27 10:17:50 brouard
347: Summary: Some bug with rint
348:
349: Revision 1.270 2017/05/24 05:45:29 brouard
350: *** empty log message ***
351:
352: Revision 1.269 2017/05/23 08:39:25 brouard
353: Summary: Code into subroutine, cleanings
354:
355: Revision 1.268 2017/05/18 20:09:32 brouard
356: Summary: backprojection and confidence intervals of backprevalence
357:
358: Revision 1.267 2017/05/13 10:25:05 brouard
359: Summary: temporary save for backprojection
360:
361: Revision 1.266 2017/05/13 07:26:12 brouard
362: Summary: Version 0.99r13 (improvements and bugs fixed)
363:
364: Revision 1.265 2017/04/26 16:22:11 brouard
365: Summary: imach 0.99r13 Some bugs fixed
366:
367: Revision 1.264 2017/04/26 06:01:29 brouard
368: Summary: Labels in graphs
369:
370: Revision 1.263 2017/04/24 15:23:15 brouard
371: Summary: to save
372:
373: Revision 1.262 2017/04/18 16:48:12 brouard
374: *** empty log message ***
375:
376: Revision 1.261 2017/04/05 10:14:09 brouard
377: Summary: Bug in E_ as well as in T_ fixed nres-1 vs k1-1
378:
379: Revision 1.260 2017/04/04 17:46:59 brouard
380: Summary: Gnuplot indexations fixed (humm)
381:
382: Revision 1.259 2017/04/04 13:01:16 brouard
383: Summary: Some errors to warnings only if date of death is unknown but status is death we could set to pi3
384:
385: Revision 1.258 2017/04/03 10:17:47 brouard
386: Summary: Version 0.99r12
387:
388: Some cleanings, conformed with updated documentation.
389:
390: Revision 1.257 2017/03/29 16:53:30 brouard
391: Summary: Temp
392:
393: Revision 1.256 2017/03/27 05:50:23 brouard
394: Summary: Temporary
395:
396: Revision 1.255 2017/03/08 16:02:28 brouard
397: Summary: IMaCh version 0.99r10 bugs in gnuplot fixed
398:
399: Revision 1.254 2017/03/08 07:13:00 brouard
400: Summary: Fixing data parameter line
401:
402: Revision 1.253 2016/12/15 11:59:41 brouard
403: Summary: 0.99 in progress
404:
405: Revision 1.252 2016/09/15 21:15:37 brouard
406: *** empty log message ***
407:
408: Revision 1.251 2016/09/15 15:01:13 brouard
409: Summary: not working
410:
411: Revision 1.250 2016/09/08 16:07:27 brouard
412: Summary: continue
413:
414: Revision 1.249 2016/09/07 17:14:18 brouard
415: Summary: Starting values from frequencies
416:
417: Revision 1.248 2016/09/07 14:10:18 brouard
418: *** empty log message ***
419:
420: Revision 1.247 2016/09/02 11:11:21 brouard
421: *** empty log message ***
422:
423: Revision 1.246 2016/09/02 08:49:22 brouard
424: *** empty log message ***
425:
426: Revision 1.245 2016/09/02 07:25:01 brouard
427: *** empty log message ***
428:
429: Revision 1.244 2016/09/02 07:17:34 brouard
430: *** empty log message ***
431:
432: Revision 1.243 2016/09/02 06:45:35 brouard
433: *** empty log message ***
434:
435: Revision 1.242 2016/08/30 15:01:20 brouard
436: Summary: Fixing a lots
437:
438: Revision 1.241 2016/08/29 17:17:25 brouard
439: Summary: gnuplot problem in Back projection to fix
440:
441: Revision 1.240 2016/08/29 07:53:18 brouard
442: Summary: Better
443:
444: Revision 1.239 2016/08/26 15:51:03 brouard
445: Summary: Improvement in Powell output in order to copy and paste
446:
447: Author:
448:
449: Revision 1.238 2016/08/26 14:23:35 brouard
450: Summary: Starting tests of 0.99
451:
452: Revision 1.237 2016/08/26 09:20:19 brouard
453: Summary: to valgrind
454:
455: Revision 1.236 2016/08/25 10:50:18 brouard
456: *** empty log message ***
457:
458: Revision 1.235 2016/08/25 06:59:23 brouard
459: *** empty log message ***
460:
461: Revision 1.234 2016/08/23 16:51:20 brouard
462: *** empty log message ***
463:
464: Revision 1.233 2016/08/23 07:40:50 brouard
465: Summary: not working
466:
467: Revision 1.232 2016/08/22 14:20:21 brouard
468: Summary: not working
469:
470: Revision 1.231 2016/08/22 07:17:15 brouard
471: Summary: not working
472:
473: Revision 1.230 2016/08/22 06:55:53 brouard
474: Summary: Not working
475:
476: Revision 1.229 2016/07/23 09:45:53 brouard
477: Summary: Completing for func too
478:
479: Revision 1.228 2016/07/22 17:45:30 brouard
480: Summary: Fixing some arrays, still debugging
481:
482: Revision 1.226 2016/07/12 18:42:34 brouard
483: Summary: temp
484:
485: Revision 1.225 2016/07/12 08:40:03 brouard
486: Summary: saving but not running
487:
488: Revision 1.224 2016/07/01 13:16:01 brouard
489: Summary: Fixes
490:
491: Revision 1.223 2016/02/19 09:23:35 brouard
492: Summary: temporary
493:
494: Revision 1.222 2016/02/17 08:14:50 brouard
495: Summary: Probably last 0.98 stable version 0.98r6
496:
497: Revision 1.221 2016/02/15 23:35:36 brouard
498: Summary: minor bug
499:
500: Revision 1.219 2016/02/15 00:48:12 brouard
501: *** empty log message ***
502:
503: Revision 1.218 2016/02/12 11:29:23 brouard
504: Summary: 0.99 Back projections
505:
506: Revision 1.217 2015/12/23 17:18:31 brouard
507: Summary: Experimental backcast
508:
509: Revision 1.216 2015/12/18 17:32:11 brouard
510: Summary: 0.98r4 Warning and status=-2
511:
512: Version 0.98r4 is now:
513: - displaying an error when status is -1, date of interview unknown and date of death known;
514: - permitting a status -2 when the vital status is unknown at a known date of right truncation.
515: Older changes concerning s=-2, dating from 2005 have been supersed.
516:
517: Revision 1.215 2015/12/16 08:52:24 brouard
518: Summary: 0.98r4 working
519:
520: Revision 1.214 2015/12/16 06:57:54 brouard
521: Summary: temporary not working
522:
523: Revision 1.213 2015/12/11 18:22:17 brouard
524: Summary: 0.98r4
525:
526: Revision 1.212 2015/11/21 12:47:24 brouard
527: Summary: minor typo
528:
529: Revision 1.211 2015/11/21 12:41:11 brouard
530: Summary: 0.98r3 with some graph of projected cross-sectional
531:
532: Author: Nicolas Brouard
533:
534: Revision 1.210 2015/11/18 17:41:20 brouard
535: Summary: Start working on projected prevalences Revision 1.209 2015/11/17 22:12:03 brouard
536: Summary: Adding ftolpl parameter
537: Author: N Brouard
538:
539: We had difficulties to get smoothed confidence intervals. It was due
540: to the period prevalence which wasn't computed accurately. The inner
541: parameter ftolpl is now an outer parameter of the .imach parameter
542: file after estepm. If ftolpl is small 1.e-4 and estepm too,
543: computation are long.
544:
545: Revision 1.208 2015/11/17 14:31:57 brouard
546: Summary: temporary
547:
548: Revision 1.207 2015/10/27 17:36:57 brouard
549: *** empty log message ***
550:
551: Revision 1.206 2015/10/24 07:14:11 brouard
552: *** empty log message ***
553:
554: Revision 1.205 2015/10/23 15:50:53 brouard
555: Summary: 0.98r3 some clarification for graphs on likelihood contributions
556:
557: Revision 1.204 2015/10/01 16:20:26 brouard
558: Summary: Some new graphs of contribution to likelihood
559:
560: Revision 1.203 2015/09/30 17:45:14 brouard
561: Summary: looking at better estimation of the hessian
562:
563: Also a better criteria for convergence to the period prevalence And
564: therefore adding the number of years needed to converge. (The
565: prevalence in any alive state shold sum to one
566:
567: Revision 1.202 2015/09/22 19:45:16 brouard
568: Summary: Adding some overall graph on contribution to likelihood. Might change
569:
570: Revision 1.201 2015/09/15 17:34:58 brouard
571: Summary: 0.98r0
572:
573: - Some new graphs like suvival functions
574: - Some bugs fixed like model=1+age+V2.
575:
576: Revision 1.200 2015/09/09 16:53:55 brouard
577: Summary: Big bug thanks to Flavia
578:
579: Even model=1+age+V2. did not work anymore
580:
581: Revision 1.199 2015/09/07 14:09:23 brouard
582: Summary: 0.98q6 changing default small png format for graph to vectorized svg.
583:
584: Revision 1.198 2015/09/03 07:14:39 brouard
585: Summary: 0.98q5 Flavia
586:
587: Revision 1.197 2015/09/01 18:24:39 brouard
588: *** empty log message ***
589:
590: Revision 1.196 2015/08/18 23:17:52 brouard
591: Summary: 0.98q5
592:
593: Revision 1.195 2015/08/18 16:28:39 brouard
594: Summary: Adding a hack for testing purpose
595:
596: After reading the title, ftol and model lines, if the comment line has
597: a q, starting with #q, the answer at the end of the run is quit. It
598: permits to run test files in batch with ctest. The former workaround was
599: $ echo q | imach foo.imach
600:
601: Revision 1.194 2015/08/18 13:32:00 brouard
602: Summary: Adding error when the covariance matrix doesn't contain the exact number of lines required by the model line.
603:
604: Revision 1.193 2015/08/04 07:17:42 brouard
605: Summary: 0.98q4
606:
607: Revision 1.192 2015/07/16 16:49:02 brouard
608: Summary: Fixing some outputs
609:
610: Revision 1.191 2015/07/14 10:00:33 brouard
611: Summary: Some fixes
612:
613: Revision 1.190 2015/05/05 08:51:13 brouard
614: Summary: Adding digits in output parameters (7 digits instead of 6)
615:
616: Fix 1+age+.
617:
618: Revision 1.189 2015/04/30 14:45:16 brouard
619: Summary: 0.98q2
620:
621: Revision 1.188 2015/04/30 08:27:53 brouard
622: *** empty log message ***
623:
624: Revision 1.187 2015/04/29 09:11:15 brouard
625: *** empty log message ***
626:
627: Revision 1.186 2015/04/23 12:01:52 brouard
628: Summary: V1*age is working now, version 0.98q1
629:
630: Some codes had been disabled in order to simplify and Vn*age was
631: working in the optimization phase, ie, giving correct MLE parameters,
632: but, as usual, outputs were not correct and program core dumped.
633:
634: Revision 1.185 2015/03/11 13:26:42 brouard
635: Summary: Inclusion of compile and links command line for Intel Compiler
636:
637: Revision 1.184 2015/03/11 11:52:39 brouard
638: Summary: Back from Windows 8. Intel Compiler
639:
640: Revision 1.183 2015/03/10 20:34:32 brouard
641: Summary: 0.98q0, trying with directest, mnbrak fixed
642:
643: We use directest instead of original Powell test; probably no
644: incidence on the results, but better justifications;
645: We fixed Numerical Recipes mnbrak routine which was wrong and gave
646: wrong results.
647:
648: Revision 1.182 2015/02/12 08:19:57 brouard
649: Summary: Trying to keep directest which seems simpler and more general
650: Author: Nicolas Brouard
651:
652: Revision 1.181 2015/02/11 23:22:24 brouard
653: Summary: Comments on Powell added
654:
655: Author:
656:
657: Revision 1.180 2015/02/11 17:33:45 brouard
658: Summary: Finishing move from main to function (hpijx and prevalence_limit)
659:
660: Revision 1.179 2015/01/04 09:57:06 brouard
661: Summary: back to OS/X
662:
663: Revision 1.178 2015/01/04 09:35:48 brouard
664: *** empty log message ***
665:
666: Revision 1.177 2015/01/03 18:40:56 brouard
667: Summary: Still testing ilc32 on OSX
668:
669: Revision 1.176 2015/01/03 16:45:04 brouard
670: *** empty log message ***
671:
672: Revision 1.175 2015/01/03 16:33:42 brouard
673: *** empty log message ***
674:
675: Revision 1.174 2015/01/03 16:15:49 brouard
676: Summary: Still in cross-compilation
677:
678: Revision 1.173 2015/01/03 12:06:26 brouard
679: Summary: trying to detect cross-compilation
680:
681: Revision 1.172 2014/12/27 12:07:47 brouard
682: Summary: Back from Visual Studio and Intel, options for compiling for Windows XP
683:
684: Revision 1.171 2014/12/23 13:26:59 brouard
685: Summary: Back from Visual C
686:
687: Still problem with utsname.h on Windows
688:
689: Revision 1.170 2014/12/23 11:17:12 brouard
690: Summary: Cleaning some \%% back to %%
691:
692: The escape was mandatory for a specific compiler (which one?), but too many warnings.
693:
694: Revision 1.169 2014/12/22 23:08:31 brouard
695: Summary: 0.98p
696:
697: Outputs some informations on compiler used, OS etc. Testing on different platforms.
698:
699: Revision 1.168 2014/12/22 15:17:42 brouard
700: Summary: update
701:
702: Revision 1.167 2014/12/22 13:50:56 brouard
703: Summary: Testing uname and compiler version and if compiled 32 or 64
704:
705: Testing on Linux 64
706:
707: Revision 1.166 2014/12/22 11:40:47 brouard
708: *** empty log message ***
709:
710: Revision 1.165 2014/12/16 11:20:36 brouard
711: Summary: After compiling on Visual C
712:
713: * imach.c (Module): Merging 1.61 to 1.162
714:
715: Revision 1.164 2014/12/16 10:52:11 brouard
716: Summary: Merging with Visual C after suppressing some warnings for unused variables. Also fixing Saito's bug 0.98Xn
717:
718: * imach.c (Module): Merging 1.61 to 1.162
719:
720: Revision 1.163 2014/12/16 10:30:11 brouard
721: * imach.c (Module): Merging 1.61 to 1.162
722:
723: Revision 1.162 2014/09/25 11:43:39 brouard
724: Summary: temporary backup 0.99!
725:
726: Revision 1.1 2014/09/16 11:06:58 brouard
727: Summary: With some code (wrong) for nlopt
728:
729: Author:
730:
731: Revision 1.161 2014/09/15 20:41:41 brouard
732: Summary: Problem with macro SQR on Intel compiler
733:
734: Revision 1.160 2014/09/02 09:24:05 brouard
735: *** empty log message ***
736:
737: Revision 1.159 2014/09/01 10:34:10 brouard
738: Summary: WIN32
739: Author: Brouard
740:
741: Revision 1.158 2014/08/27 17:11:51 brouard
742: *** empty log message ***
743:
744: Revision 1.157 2014/08/27 16:26:55 brouard
745: Summary: Preparing windows Visual studio version
746: Author: Brouard
747:
748: In order to compile on Visual studio, time.h is now correct and time_t
749: and tm struct should be used. difftime should be used but sometimes I
750: just make the differences in raw time format (time(&now).
751: Trying to suppress #ifdef LINUX
752: Add xdg-open for __linux in order to open default browser.
753:
754: Revision 1.156 2014/08/25 20:10:10 brouard
755: *** empty log message ***
756:
757: Revision 1.155 2014/08/25 18:32:34 brouard
758: Summary: New compile, minor changes
759: Author: Brouard
760:
761: Revision 1.154 2014/06/20 17:32:08 brouard
762: Summary: Outputs now all graphs of convergence to period prevalence
763:
764: Revision 1.153 2014/06/20 16:45:46 brouard
765: Summary: If 3 live state, convergence to period prevalence on same graph
766: Author: Brouard
767:
768: Revision 1.152 2014/06/18 17:54:09 brouard
769: Summary: open browser, use gnuplot on same dir than imach if not found in the path
770:
771: Revision 1.151 2014/06/18 16:43:30 brouard
772: *** empty log message ***
773:
774: Revision 1.150 2014/06/18 16:42:35 brouard
775: Summary: If gnuplot is not in the path try on same directory than imach binary (OSX)
776: Author: brouard
777:
778: Revision 1.149 2014/06/18 15:51:14 brouard
779: Summary: Some fixes in parameter files errors
780: Author: Nicolas Brouard
781:
782: Revision 1.148 2014/06/17 17:38:48 brouard
783: Summary: Nothing new
784: Author: Brouard
785:
786: Just a new packaging for OS/X version 0.98nS
787:
788: Revision 1.147 2014/06/16 10:33:11 brouard
789: *** empty log message ***
790:
791: Revision 1.146 2014/06/16 10:20:28 brouard
792: Summary: Merge
793: Author: Brouard
794:
795: Merge, before building revised version.
796:
797: Revision 1.145 2014/06/10 21:23:15 brouard
798: Summary: Debugging with valgrind
799: Author: Nicolas Brouard
800:
801: Lot of changes in order to output the results with some covariates
802: After the Edimburgh REVES conference 2014, it seems mandatory to
803: improve the code.
804: No more memory valgrind error but a lot has to be done in order to
805: continue the work of splitting the code into subroutines.
806: Also, decodemodel has been improved. Tricode is still not
807: optimal. nbcode should be improved. Documentation has been added in
808: the source code.
809:
810: Revision 1.143 2014/01/26 09:45:38 brouard
811: Summary: Version 0.98nR (to be improved, but gives same optimization results as 0.98k. Nice, promising
812:
813: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
814: (Module): Version 0.98nR Running ok, but output format still only works for three covariates.
815:
816: Revision 1.142 2014/01/26 03:57:36 brouard
817: Summary: gnuplot changed plot w l 1 has to be changed to plot w l lt 2
818:
819: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
820:
821: Revision 1.141 2014/01/26 02:42:01 brouard
822: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
823:
824: Revision 1.140 2011/09/02 10:37:54 brouard
825: Summary: times.h is ok with mingw32 now.
826:
827: Revision 1.139 2010/06/14 07:50:17 brouard
828: After the theft of my laptop, I probably lost some lines of codes which were not uploaded to the CVS tree.
829: I remember having already fixed agemin agemax which are pointers now but not cvs saved.
830:
831: Revision 1.138 2010/04/30 18:19:40 brouard
832: *** empty log message ***
833:
834: Revision 1.137 2010/04/29 18:11:38 brouard
835: (Module): Checking covariates for more complex models
836: than V1+V2. A lot of change to be done. Unstable.
837:
838: Revision 1.136 2010/04/26 20:30:53 brouard
839: (Module): merging some libgsl code. Fixing computation
840: of likelione (using inter/intrapolation if mle = 0) in order to
841: get same likelihood as if mle=1.
842: Some cleaning of code and comments added.
843:
844: Revision 1.135 2009/10/29 15:33:14 brouard
845: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
846:
847: Revision 1.134 2009/10/29 13:18:53 brouard
848: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
849:
850: Revision 1.133 2009/07/06 10:21:25 brouard
851: just nforces
852:
853: Revision 1.132 2009/07/06 08:22:05 brouard
854: Many tings
855:
856: Revision 1.131 2009/06/20 16:22:47 brouard
857: Some dimensions resccaled
858:
859: Revision 1.130 2009/05/26 06:44:34 brouard
860: (Module): Max Covariate is now set to 20 instead of 8. A
861: lot of cleaning with variables initialized to 0. Trying to make
862: V2+V3*age+V1+V4 strb=V3*age+V1+V4 working better.
863:
864: Revision 1.129 2007/08/31 13:49:27 lievre
865: Modification of the way of exiting when the covariate is not binary in order to see on the window the error message before exiting
866:
867: Revision 1.128 2006/06/30 13:02:05 brouard
868: (Module): Clarifications on computing e.j
869:
870: Revision 1.127 2006/04/28 18:11:50 brouard
871: (Module): Yes the sum of survivors was wrong since
872: imach-114 because nhstepm was no more computed in the age
873: loop. Now we define nhstepma in the age loop.
874: (Module): In order to speed up (in case of numerous covariates) we
875: compute health expectancies (without variances) in a first step
876: and then all the health expectancies with variances or standard
877: deviation (needs data from the Hessian matrices) which slows the
878: computation.
879: In the future we should be able to stop the program is only health
880: expectancies and graph are needed without standard deviations.
881:
882: Revision 1.126 2006/04/28 17:23:28 brouard
883: (Module): Yes the sum of survivors was wrong since
884: imach-114 because nhstepm was no more computed in the age
885: loop. Now we define nhstepma in the age loop.
886: Version 0.98h
887:
888: Revision 1.125 2006/04/04 15:20:31 lievre
889: Errors in calculation of health expectancies. Age was not initialized.
890: Forecasting file added.
891:
892: Revision 1.124 2006/03/22 17:13:53 lievre
893: Parameters are printed with %lf instead of %f (more numbers after the comma).
894: The log-likelihood is printed in the log file
895:
896: Revision 1.123 2006/03/20 10:52:43 brouard
897: * imach.c (Module): <title> changed, corresponds to .htm file
898: name. <head> headers where missing.
899:
900: * imach.c (Module): Weights can have a decimal point as for
901: English (a comma might work with a correct LC_NUMERIC environment,
902: otherwise the weight is truncated).
903: Modification of warning when the covariates values are not 0 or
904: 1.
905: Version 0.98g
906:
907: Revision 1.122 2006/03/20 09:45:41 brouard
908: (Module): Weights can have a decimal point as for
909: English (a comma might work with a correct LC_NUMERIC environment,
910: otherwise the weight is truncated).
911: Modification of warning when the covariates values are not 0 or
912: 1.
913: Version 0.98g
914:
915: Revision 1.121 2006/03/16 17:45:01 lievre
916: * imach.c (Module): Comments concerning covariates added
917:
918: * imach.c (Module): refinements in the computation of lli if
919: status=-2 in order to have more reliable computation if stepm is
920: not 1 month. Version 0.98f
921:
922: Revision 1.120 2006/03/16 15:10:38 lievre
923: (Module): refinements in the computation of lli if
924: status=-2 in order to have more reliable computation if stepm is
925: not 1 month. Version 0.98f
926:
927: Revision 1.119 2006/03/15 17:42:26 brouard
928: (Module): Bug if status = -2, the loglikelihood was
929: computed as likelihood omitting the logarithm. Version O.98e
930:
931: Revision 1.118 2006/03/14 18:20:07 brouard
932: (Module): varevsij Comments added explaining the second
933: table of variances if popbased=1 .
934: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
935: (Module): Function pstamp added
936: (Module): Version 0.98d
937:
938: Revision 1.117 2006/03/14 17:16:22 brouard
939: (Module): varevsij Comments added explaining the second
940: table of variances if popbased=1 .
941: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
942: (Module): Function pstamp added
943: (Module): Version 0.98d
944:
945: Revision 1.116 2006/03/06 10:29:27 brouard
946: (Module): Variance-covariance wrong links and
947: varian-covariance of ej. is needed (Saito).
948:
949: Revision 1.115 2006/02/27 12:17:45 brouard
950: (Module): One freematrix added in mlikeli! 0.98c
951:
952: Revision 1.114 2006/02/26 12:57:58 brouard
953: (Module): Some improvements in processing parameter
954: filename with strsep.
955:
956: Revision 1.113 2006/02/24 14:20:24 brouard
957: (Module): Memory leaks checks with valgrind and:
958: datafile was not closed, some imatrix were not freed and on matrix
959: allocation too.
960:
961: Revision 1.112 2006/01/30 09:55:26 brouard
962: (Module): Back to gnuplot.exe instead of wgnuplot.exe
963:
964: Revision 1.111 2006/01/25 20:38:18 brouard
965: (Module): Lots of cleaning and bugs added (Gompertz)
966: (Module): Comments can be added in data file. Missing date values
967: can be a simple dot '.'.
968:
969: Revision 1.110 2006/01/25 00:51:50 brouard
970: (Module): Lots of cleaning and bugs added (Gompertz)
971:
972: Revision 1.109 2006/01/24 19:37:15 brouard
973: (Module): Comments (lines starting with a #) are allowed in data.
974:
975: Revision 1.108 2006/01/19 18:05:42 lievre
976: Gnuplot problem appeared...
977: To be fixed
978:
979: Revision 1.107 2006/01/19 16:20:37 brouard
980: Test existence of gnuplot in imach path
981:
982: Revision 1.106 2006/01/19 13:24:36 brouard
983: Some cleaning and links added in html output
984:
985: Revision 1.105 2006/01/05 20:23:19 lievre
986: *** empty log message ***
987:
988: Revision 1.104 2005/09/30 16:11:43 lievre
989: (Module): sump fixed, loop imx fixed, and simplifications.
990: (Module): If the status is missing at the last wave but we know
991: that the person is alive, then we can code his/her status as -2
992: (instead of missing=-1 in earlier versions) and his/her
993: contributions to the likelihood is 1 - Prob of dying from last
994: health status (= 1-p13= p11+p12 in the easiest case of somebody in
995: the healthy state at last known wave). Version is 0.98
996:
997: Revision 1.103 2005/09/30 15:54:49 lievre
998: (Module): sump fixed, loop imx fixed, and simplifications.
999:
1000: Revision 1.102 2004/09/15 17:31:30 brouard
1001: Add the possibility to read data file including tab characters.
1002:
1003: Revision 1.101 2004/09/15 10:38:38 brouard
1004: Fix on curr_time
1005:
1006: Revision 1.100 2004/07/12 18:29:06 brouard
1007: Add version for Mac OS X. Just define UNIX in Makefile
1008:
1009: Revision 1.99 2004/06/05 08:57:40 brouard
1010: *** empty log message ***
1011:
1012: Revision 1.98 2004/05/16 15:05:56 brouard
1013: New version 0.97 . First attempt to estimate force of mortality
1014: directly from the data i.e. without the need of knowing the health
1015: state at each age, but using a Gompertz model: log u =a + b*age .
1016: This is the basic analysis of mortality and should be done before any
1017: other analysis, in order to test if the mortality estimated from the
1018: cross-longitudinal survey is different from the mortality estimated
1019: from other sources like vital statistic data.
1020:
1021: The same imach parameter file can be used but the option for mle should be -3.
1022:
1023: Agnès, who wrote this part of the code, tried to keep most of the
1024: former routines in order to include the new code within the former code.
1025:
1026: The output is very simple: only an estimate of the intercept and of
1027: the slope with 95% confident intervals.
1028:
1029: Current limitations:
1030: A) Even if you enter covariates, i.e. with the
1031: model= V1+V2 equation for example, the programm does only estimate a unique global model without covariates.
1032: B) There is no computation of Life Expectancy nor Life Table.
1033:
1034: Revision 1.97 2004/02/20 13:25:42 lievre
1035: Version 0.96d. Population forecasting command line is (temporarily)
1036: suppressed.
1037:
1038: Revision 1.96 2003/07/15 15:38:55 brouard
1039: * imach.c (Repository): Errors in subdirf, 2, 3 while printing tmpout is
1040: rewritten within the same printf. Workaround: many printfs.
1041:
1042: Revision 1.95 2003/07/08 07:54:34 brouard
1043: * imach.c (Repository):
1044: (Repository): Using imachwizard code to output a more meaningful covariance
1045: matrix (cov(a12,c31) instead of numbers.
1046:
1047: Revision 1.94 2003/06/27 13:00:02 brouard
1048: Just cleaning
1049:
1050: Revision 1.93 2003/06/25 16:33:55 brouard
1051: (Module): On windows (cygwin) function asctime_r doesn't
1052: exist so I changed back to asctime which exists.
1053: (Module): Version 0.96b
1054:
1055: Revision 1.92 2003/06/25 16:30:45 brouard
1056: (Module): On windows (cygwin) function asctime_r doesn't
1057: exist so I changed back to asctime which exists.
1058:
1059: Revision 1.91 2003/06/25 15:30:29 brouard
1060: * imach.c (Repository): Duplicated warning errors corrected.
1061: (Repository): Elapsed time after each iteration is now output. It
1062: helps to forecast when convergence will be reached. Elapsed time
1063: is stamped in powell. We created a new html file for the graphs
1064: concerning matrix of covariance. It has extension -cov.htm.
1065:
1066: Revision 1.90 2003/06/24 12:34:15 brouard
1067: (Module): Some bugs corrected for windows. Also, when
1068: mle=-1 a template is output in file "or"mypar.txt with the design
1069: of the covariance matrix to be input.
1070:
1071: Revision 1.89 2003/06/24 12:30:52 brouard
1072: (Module): Some bugs corrected for windows. Also, when
1073: mle=-1 a template is output in file "or"mypar.txt with the design
1074: of the covariance matrix to be input.
1075:
1076: Revision 1.88 2003/06/23 17:54:56 brouard
1077: * 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.
1078:
1079: Revision 1.87 2003/06/18 12:26:01 brouard
1080: Version 0.96
1081:
1082: Revision 1.86 2003/06/17 20:04:08 brouard
1083: (Module): Change position of html and gnuplot routines and added
1084: routine fileappend.
1085:
1086: Revision 1.85 2003/06/17 13:12:43 brouard
1087: * imach.c (Repository): Check when date of death was earlier that
1088: current date of interview. It may happen when the death was just
1089: prior to the death. In this case, dh was negative and likelihood
1090: was wrong (infinity). We still send an "Error" but patch by
1091: assuming that the date of death was just one stepm after the
1092: interview.
1093: (Repository): Because some people have very long ID (first column)
1094: we changed int to long in num[] and we added a new lvector for
1095: memory allocation. But we also truncated to 8 characters (left
1096: truncation)
1097: (Repository): No more line truncation errors.
1098:
1099: Revision 1.84 2003/06/13 21:44:43 brouard
1100: * imach.c (Repository): Replace "freqsummary" at a correct
1101: place. It differs from routine "prevalence" which may be called
1102: many times. Probs is memory consuming and must be used with
1103: parcimony.
1104: Version 0.95a3 (should output exactly the same maximization than 0.8a2)
1105:
1106: Revision 1.83 2003/06/10 13:39:11 lievre
1107: *** empty log message ***
1108:
1109: Revision 1.82 2003/06/05 15:57:20 brouard
1110: Add log in imach.c and fullversion number is now printed.
1111:
1112: */
1113: /*
1114: Interpolated Markov Chain
1115:
1116: Short summary of the programme:
1117:
1118: This program computes Healthy Life Expectancies or State-specific
1119: (if states aren't health statuses) Expectancies from
1120: cross-longitudinal data. Cross-longitudinal data consist in:
1121:
1122: -1- a first survey ("cross") where individuals from different ages
1123: are interviewed on their health status or degree of disability (in
1124: the case of a health survey which is our main interest)
1125:
1126: -2- at least a second wave of interviews ("longitudinal") which
1127: measure each change (if any) in individual health status. Health
1128: expectancies are computed from the time spent in each health state
1129: according to a model. More health states you consider, more time is
1130: necessary to reach the Maximum Likelihood of the parameters involved
1131: in the model. The simplest model is the multinomial logistic model
1132: where pij is the probability to be observed in state j at the second
1133: wave conditional to be observed in state i at the first
1134: wave. Therefore the model is: log(pij/pii)= aij + bij*age+ cij*sex +
1135: etc , where 'age' is age and 'sex' is a covariate. If you want to
1136: have a more complex model than "constant and age", you should modify
1137: the program where the markup *Covariates have to be included here
1138: again* invites you to do it. More covariates you add, slower the
1139: convergence.
1140:
1141: The advantage of this computer programme, compared to a simple
1142: multinomial logistic model, is clear when the delay between waves is not
1143: identical for each individual. Also, if a individual missed an
1144: intermediate interview, the information is lost, but taken into
1145: account using an interpolation or extrapolation.
1146:
1147: hPijx is the probability to be observed in state i at age x+h
1148: conditional to the observed state i at age x. The delay 'h' can be
1149: split into an exact number (nh*stepm) of unobserved intermediate
1150: states. This elementary transition (by month, quarter,
1151: semester or year) is modelled as a multinomial logistic. The hPx
1152: matrix is simply the matrix product of nh*stepm elementary matrices
1153: and the contribution of each individual to the likelihood is simply
1154: hPijx.
1155:
1156: Also this programme outputs the covariance matrix of the parameters but also
1157: of the life expectancies. It also computes the period (stable) prevalence.
1158:
1159: Back prevalence and projections:
1160:
1161: - back_prevalence_limit(double *p, double **bprlim, double ageminpar,
1162: double agemaxpar, double ftolpl, int *ncvyearp, double
1163: dateprev1,double dateprev2, int firstpass, int lastpass, int
1164: mobilavproj)
1165:
1166: Computes the back prevalence limit for any combination of
1167: covariate values k at any age between ageminpar and agemaxpar and
1168: returns it in **bprlim. In the loops,
1169:
1170: - **bprevalim(**bprlim, ***mobaverage, nlstate, *p, age, **oldm,
1171: **savm, **dnewm, **doldm, **dsavm, ftolpl, ncvyearp, k);
1172:
1173: - hBijx Back Probability to be in state i at age x-h being in j at x
1174: Computes for any combination of covariates k and any age between bage and fage
1175: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
1176: oldm=oldms;savm=savms;
1177:
1178: - hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k, nres);
1179: Computes the transition matrix starting at age 'age' over
1180: 'nhstepm*hstepm*stepm' months (i.e. until
1181: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
1182: nhstepm*hstepm matrices.
1183:
1184: Returns p3mat[i][j][h] after calling
1185: p3mat[i][j][h]=matprod2(newm,
1186: bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm,
1187: dsavm,ij),\ 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
1188: oldm);
1189:
1190: Important routines
1191:
1192: - func (or funcone), computes logit (pij) distinguishing
1193: o fixed variables (single or product dummies or quantitative);
1194: o varying variables by:
1195: (1) wave (single, product dummies, quantitative),
1196: (2) by age (can be month) age (done), age*age (done), age*Vn where Vn can be:
1197: % fixed dummy (treated) or quantitative (not done because time-consuming);
1198: % varying dummy (not done) or quantitative (not done);
1199: - Tricode which tests the modality of dummy variables (in order to warn with wrong or empty modalities)
1200: and returns the number of efficient covariates cptcoveff and modalities nbcode[Tvar[k]][1]= 0 and nbcode[Tvar[k]][2]= 1 usually.
1201: - printinghtml which outputs results like life expectancy in and from a state for a combination of modalities of dummy variables
1202: o There are 2**cptcoveff combinations of (0,1) for cptcoveff variables. Outputting only combinations with people, éliminating 1 1 if
1203: race White (0 0), Black vs White (1 0), Hispanic (0 1) and 1 1 being meaningless.
1204:
1205:
1206:
1207: Authors: Nicolas Brouard (brouard@ined.fr) and Agnès Lièvre (lievre@ined.fr).
1208: Institut national d'études démographiques, Paris.
1209: This software have been partly granted by Euro-REVES, a concerted action
1210: from the European Union.
1211: It is copyrighted identically to a GNU software product, ie programme and
1212: software can be distributed freely for non commercial use. Latest version
1213: can be accessed at http://euroreves.ined.fr/imach .
1214:
1215: Help to debug: LD_PRELOAD=/usr/local/lib/libnjamd.so ./imach foo.imach
1216: or better on gdb : set env LD_PRELOAD=/usr/local/lib/libnjamd.so
1217:
1218: **********************************************************************/
1219: /*
1220: main
1221: read parameterfile
1222: read datafile
1223: concatwav
1224: freqsummary
1225: if (mle >= 1)
1226: mlikeli
1227: print results files
1228: if mle==1
1229: computes hessian
1230: read end of parameter file: agemin, agemax, bage, fage, estepm
1231: begin-prev-date,...
1232: open gnuplot file
1233: open html file
1234: period (stable) prevalence | pl_nom 1-1 2-2 etc by covariate
1235: for age prevalim() | #****** V1=0 V2=1 V3=1 V4=0 ******
1236: | 65 1 0 2 1 3 1 4 0 0.96326 0.03674
1237: freexexit2 possible for memory heap.
1238:
1239: h Pij x | pij_nom ficrestpij
1240: # Cov Agex agex+h hpijx with i,j= 1-1 1-2 1-3 2-1 2-2 2-3
1241: 1 85 85 1.00000 0.00000 0.00000 0.00000 1.00000 0.00000
1242: 1 85 86 0.68299 0.22291 0.09410 0.71093 0.00000 0.28907
1243:
1244: 1 65 99 0.00364 0.00322 0.99314 0.00350 0.00310 0.99340
1245: 1 65 100 0.00214 0.00204 0.99581 0.00206 0.00196 0.99597
1246: variance of p one-step probabilities varprob | prob_nom ficresprob #One-step probabilities and stand. devi in ()
1247: Standard deviation of one-step probabilities | probcor_nom ficresprobcor #One-step probabilities and correlation matrix
1248: Matrix of variance covariance of one-step probabilities | probcov_nom ficresprobcov #One-step probabilities and covariance matrix
1249:
1250: forecasting if prevfcast==1 prevforecast call prevalence()
1251: health expectancies
1252: Variance-covariance of DFLE
1253: prevalence()
1254: movingaverage()
1255: varevsij()
1256: if popbased==1 varevsij(,popbased)
1257: total life expectancies
1258: Variance of period (stable) prevalence
1259: end
1260: */
1261:
1262: /* #define DEBUG */
1263: /* #define DEBUGBRENT */
1264: /* #define DEBUGLINMIN */
1265: /* #define DEBUGHESS */
1266: #define DEBUGHESSIJ
1267: /* #define LINMINORIGINAL /\* Don't use loop on scale in linmin (accepting nan) *\/ */
1268: #define POWELL /* Instead of NLOPT */
1269: #define POWELLNOF3INFF1TEST /* Skip test */
1270: /* #define POWELLORIGINAL /\* Don't use Directest to decide new direction but original Powell test *\/ */
1271: /* #define MNBRAKORIGINAL /\* Don't use mnbrak fix *\/ */
1272: /* #define FLATSUP *//* Suppresses directions where likelihood is flat */
1273:
1274: #include <math.h>
1275: #include <stdio.h>
1276: #include <stdlib.h>
1277: #include <string.h>
1278: #include <ctype.h>
1279:
1280: #ifdef _WIN32
1281: #include <io.h>
1282: #include <windows.h>
1283: #include <tchar.h>
1284: #else
1285: #include <unistd.h>
1286: #endif
1287:
1288: #include <limits.h>
1289: #include <sys/types.h>
1290:
1291: #if defined(__GNUC__)
1292: #include <sys/utsname.h> /* Doesn't work on Windows */
1293: #endif
1294:
1295: #include <sys/stat.h>
1296: #include <errno.h>
1297: /* extern int errno; */
1298:
1299: /* #ifdef LINUX */
1300: /* #include <time.h> */
1301: /* #include "timeval.h" */
1302: /* #else */
1303: /* #include <sys/time.h> */
1304: /* #endif */
1305:
1306: #include <time.h>
1307:
1308: #ifdef GSL
1309: #include <gsl/gsl_errno.h>
1310: #include <gsl/gsl_multimin.h>
1311: #endif
1312:
1313:
1314: #ifdef NLOPT
1315: #include <nlopt.h>
1316: typedef struct {
1317: double (* function)(double [] );
1318: } myfunc_data ;
1319: #endif
1320:
1321: /* #include <libintl.h> */
1322: /* #define _(String) gettext (String) */
1323:
1324: #define MAXLINE 2048 /* Was 256 and 1024. Overflow with 312 with 2 states and 4 covariates. Should be ok */
1325:
1326: #define GNUPLOTPROGRAM "gnuplot"
1327: #define GNUPLOTVERSION 5.1
1328: double gnuplotversion=GNUPLOTVERSION;
1329: /*#define GNUPLOTPROGRAM "..\\gp37mgw\\wgnuplot"*/
1330: #define FILENAMELENGTH 256
1331:
1332: #define GLOCK_ERROR_NOPATH -1 /* empty path */
1333: #define GLOCK_ERROR_GETCWD -2 /* cannot get cwd */
1334:
1335: #define MAXPARM 128 /**< Maximum number of parameters for the optimization */
1336: #define NPARMAX 64 /**< (nlstate+ndeath-1)*nlstate*ncovmodel */
1337:
1338: #define NINTERVMAX 8
1339: #define NLSTATEMAX 8 /**< Maximum number of live states (for func) */
1340: #define NDEATHMAX 8 /**< Maximum number of dead states (for func) */
1341: #define NCOVMAX 30 /**< Maximum number of covariates used in the model, including generated covariates V1*V2 or V1*age */
1342: #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1343: /*#define decodtabm(h,k,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (k-1)) & 1) +1 : -1)*/
1344: #define decodtabm(h,k,cptcoveff) (((h-1) >> (k-1)) & 1) +1
1345: /*#define MAXN 20000 */ /* Should by replaced by nobs, real number of observations and unlimited */
1346: #define YEARM 12. /**< Number of months per year */
1347: /* #define AGESUP 130 */
1348: /* #define AGESUP 150 */
1349: #define AGESUP 200
1350: #define AGEINF 0
1351: #define AGEMARGE 25 /* Marge for agemin and agemax for(iage=agemin-AGEMARGE; iage <= agemax+3+AGEMARGE; iage++) */
1352: #define AGEBASE 40
1353: #define AGEOVERFLOW 1.e20
1354: #define AGEGOMP 10 /**< Minimal age for Gompertz adjustment */
1355: #ifdef _WIN32
1356: #define DIRSEPARATOR '\\'
1357: #define CHARSEPARATOR "\\"
1358: #define ODIRSEPARATOR '/'
1359: #else
1360: #define DIRSEPARATOR '/'
1361: #define CHARSEPARATOR "/"
1362: #define ODIRSEPARATOR '\\'
1363: #endif
1364:
1365: /* $Id: imach.c,v 1.348 2022/09/20 00:01:38 brouard Exp $ */
1366: /* $State: Exp $ */
1367: #include "version.h"
1368: char version[]=__IMACH_VERSION__;
1369: char copyright[]="September 2022,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";
1370: char fullversion[]="$Revision: 1.348 $ $Date: 2022/09/20 00:01:38 $";
1371: char strstart[80];
1372: char optionfilext[10], optionfilefiname[FILENAMELENGTH];
1373: int erreur=0, nberr=0, nbwarn=0; /* Error number, number of errors number of warnings */
1374: int debugILK=0; /* debugILK is set by a #d in a comment line */
1375: int nagesqr=0, nforce=0; /* nagesqr=1 if model is including age*age, number of forces */
1376: /* Number of covariates model (1)=V2+V1+ V3*age+V2*V4 */
1377: /* Model(2) V1 + V2 + V3 + V8 + V7*V8 + V5*V6 + V8*age + V3*age + age*age */
1378: int cptcovn=0; /**< cptcovn decodemodel: number of covariates k of the models excluding age*products =6 and age*age but including products */
1379: int cptcovt=0; /**< cptcovt: total number of covariates of the model (2) nbocc(+)+1 = 8 excepting constant and age and age*age */
1380: int cptcovs=0; /**< cptcovs number of SIMPLE covariates in the model V2+V1 =2 (dummy or quantit or time varying) */
1381: int cptcovsnq=0; /**< cptcovsnq number of SIMPLE covariates in the model but non quantitative V2+V1 =2 */
1382: int cptcovage=0; /**< Number of covariates with age: V3*age only =1 */
1383: int cptcovprodnoage=0; /**< Number of covariate products without age */
1384: 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) */
1385: int ncovf=0; /* Total number of effective fixed covariates (dummy or quantitative) in the model */
1386: int ncovv=0; /* Total number of effective (wave) varying covariates (dummy or quantitative) in the model */
1387: int ncovvt=0; /* Total number of effective (wave) varying covariates (dummy or quantitative or products [without age]) in the model */
1388: int ncova=0; /* Total number of effective (wave and stepm) varying with age covariates (dummy of quantitative) in the model */
1389: int nsd=0; /**< Total number of single dummy variables (output) */
1390: int nsq=0; /**< Total number of single quantitative variables (output) */
1391: int ncoveff=0; /* Total number of effective fixed dummy covariates in the model */
1392: int nqfveff=0; /**< nqfveff Number of Quantitative Fixed Variables Effective */
1393: int ntveff=0; /**< ntveff number of effective time varying variables */
1394: int nqtveff=0; /**< ntqveff number of effective time varying quantitative variables */
1395: int cptcov=0; /* Working variable */
1396: int firstobs=1, lastobs=10; /* nobs = lastobs-firstobs+1 declared globally ;*/
1397: int nobs=10; /* Number of observations in the data lastobs-firstobs */
1398: int ncovcombmax=NCOVMAX; /* Maximum calculated number of covariate combination = pow(2, cptcoveff) */
1399: int npar=NPARMAX; /* Number of parameters (nlstate+ndeath-1)*nlstate*ncovmodel; */
1400: int nlstate=2; /* Number of live states */
1401: int ndeath=1; /* Number of dead states */
1402: int ncovmodel=0, ncovcol=0; /* Total number of covariables including constant a12*1 +b12*x ncovmodel=2 */
1403: int nqv=0, ntv=0, nqtv=0; /* Total number of quantitative variables, time variable (dummy), quantitative and time variable*/
1404: int ncovcolt=0; /* ncovcolt=ncovcol+nqv+ntv+nqtv; total of covariates in the data, not in the model equation*/
1405: int popbased=0;
1406:
1407: int *wav; /* Number of waves for this individuual 0 is possible */
1408: int maxwav=0; /* Maxim number of waves */
1409: int jmin=0, jmax=0; /* min, max spacing between 2 waves */
1410: int ijmin=0, ijmax=0; /* Individuals having jmin and jmax */
1411: int gipmx=0, gsw=0; /* Global variables on the number of contributions
1412: to the likelihood and the sum of weights (done by funcone)*/
1413: int mle=1, weightopt=0;
1414: int **mw; /* mw[mi][i] is number of the mi wave for this individual */
1415: int **dh; /* dh[mi][i] is number of steps between mi,mi+1 for this individual */
1416: int **bh; /* bh[mi][i] is the bias (+ or -) for this individual if the delay between
1417: * wave mi and wave mi+1 is not an exact multiple of stepm. */
1418: int countcallfunc=0; /* Count the number of calls to func */
1419: int selected(int kvar); /* Is covariate kvar selected for printing results */
1420:
1421: double jmean=1; /* Mean space between 2 waves */
1422: double **matprod2(); /* test */
1423: double **oldm, **newm, **savm; /* Working pointers to matrices */
1424: double **oldms, **newms, **savms; /* Fixed working pointers to matrices */
1425: double **ddnewms, **ddoldms, **ddsavms; /* for freeing later */
1426:
1427: /*FILE *fic ; */ /* Used in readdata only */
1428: FILE *ficpar, *ficparo,*ficres, *ficresp, *ficresphtm, *ficresphtmfr, *ficrespl, *ficresplb,*ficrespij, *ficrespijb, *ficrest,*ficresf, *ficresfb,*ficrespop;
1429: FILE *ficlog, *ficrespow;
1430: int globpr=0; /* Global variable for printing or not */
1431: double fretone; /* Only one call to likelihood */
1432: long ipmx=0; /* Number of contributions */
1433: double sw; /* Sum of weights */
1434: char filerespow[FILENAMELENGTH];
1435: char fileresilk[FILENAMELENGTH]; /* File of individual contributions to the likelihood */
1436: FILE *ficresilk;
1437: FILE *ficgp,*ficresprob,*ficpop, *ficresprobcov, *ficresprobcor;
1438: FILE *ficresprobmorprev;
1439: FILE *fichtm, *fichtmcov; /* Html File */
1440: FILE *ficreseij;
1441: char filerese[FILENAMELENGTH];
1442: FILE *ficresstdeij;
1443: char fileresstde[FILENAMELENGTH];
1444: FILE *ficrescveij;
1445: char filerescve[FILENAMELENGTH];
1446: FILE *ficresvij;
1447: char fileresv[FILENAMELENGTH];
1448:
1449: char title[MAXLINE];
1450: char model[MAXLINE]; /**< The model line */
1451: char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH], filerespl[FILENAMELENGTH], fileresplb[FILENAMELENGTH];
1452: char plotcmd[FILENAMELENGTH], pplotcmd[FILENAMELENGTH];
1453: char tmpout[FILENAMELENGTH], tmpout2[FILENAMELENGTH];
1454: char command[FILENAMELENGTH];
1455: int outcmd=0;
1456:
1457: char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filerespijb[FILENAMELENGTH], filereso[FILENAMELENGTH], rfileres[FILENAMELENGTH];
1458: char fileresu[FILENAMELENGTH]; /* fileres without r in front */
1459: char filelog[FILENAMELENGTH]; /* Log file */
1460: char filerest[FILENAMELENGTH];
1461: char fileregp[FILENAMELENGTH];
1462: char popfile[FILENAMELENGTH];
1463:
1464: char optionfilegnuplot[FILENAMELENGTH], optionfilehtm[FILENAMELENGTH], optionfilehtmcov[FILENAMELENGTH] ;
1465:
1466: /* struct timeval start_time, end_time, curr_time, last_time, forecast_time; */
1467: /* struct timezone tzp; */
1468: /* extern int gettimeofday(); */
1469: struct tm tml, *gmtime(), *localtime();
1470:
1471: extern time_t time();
1472:
1473: struct tm start_time, end_time, curr_time, last_time, forecast_time;
1474: time_t rstart_time, rend_time, rcurr_time, rlast_time, rforecast_time; /* raw time */
1475: struct tm tm;
1476:
1477: char strcurr[80], strfor[80];
1478:
1479: char *endptr;
1480: long lval;
1481: double dval;
1482:
1483: #define NR_END 1
1484: #define FREE_ARG char*
1485: #define FTOL 1.0e-10
1486:
1487: #define NRANSI
1488: #define ITMAX 200
1489: #define ITPOWMAX 20 /* This is now multiplied by the number of parameters */
1490:
1491: #define TOL 2.0e-4
1492:
1493: #define CGOLD 0.3819660
1494: #define ZEPS 1.0e-10
1495: #define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d);
1496:
1497: #define GOLD 1.618034
1498: #define GLIMIT 100.0
1499: #define TINY 1.0e-20
1500:
1501: static double maxarg1,maxarg2;
1502: #define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)>(maxarg2)? (maxarg1):(maxarg2))
1503: #define FMIN(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)<(maxarg2)? (maxarg1):(maxarg2))
1504:
1505: #define SIGN(a,b) ((b)>0.0 ? fabs(a) : -fabs(a))
1506: #define rint(a) floor(a+0.5)
1507: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/myutils_8h-source.html */
1508: #define mytinydouble 1.0e-16
1509: /* #define DEQUAL(a,b) (fabs((a)-(b))<mytinydouble) */
1510: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/mynrutils_8h-source.html */
1511: /* static double dsqrarg; */
1512: /* #define DSQR(a) (DEQUAL((dsqrarg=(a)),0.0) ? 0.0 : dsqrarg*dsqrarg) */
1513: static double sqrarg;
1514: #define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 :sqrarg*sqrarg)
1515: #define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;}
1516: int agegomp= AGEGOMP;
1517:
1518: int imx;
1519: int stepm=1;
1520: /* Stepm, step in month: minimum step interpolation*/
1521:
1522: int estepm;
1523: /* Estepm, step in month to interpolate survival function in order to approximate Life Expectancy*/
1524:
1525: int m,nb;
1526: long *num;
1527: int firstpass=0, lastpass=4,*cod, *cens;
1528: int *ncodemax; /* ncodemax[j]= Number of modalities of the j th
1529: covariate for which somebody answered excluding
1530: undefined. Usually 2: 0 and 1. */
1531: int *ncodemaxwundef; /* ncodemax[j]= Number of modalities of the j th
1532: covariate for which somebody answered including
1533: undefined. Usually 3: -1, 0 and 1. */
1534: double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint;
1535: double **pmmij, ***probs; /* Global pointer */
1536: double ***mobaverage, ***mobaverages; /* New global variable */
1537: double **precov; /* New global variable to store for each resultline, values of model covariates given by the resultlines (in order to speed up) */
1538: double *ageexmed,*agecens;
1539: double dateintmean=0;
1540: double anprojd, mprojd, jprojd; /* For eventual projections */
1541: double anprojf, mprojf, jprojf;
1542:
1543: double anbackd, mbackd, jbackd; /* For eventual backprojections */
1544: double anbackf, mbackf, jbackf;
1545: double jintmean,mintmean,aintmean;
1546: double *weight;
1547: int **s; /* Status */
1548: double *agedc;
1549: double **covar; /**< covar[j,i], value of jth covariate for individual i,
1550: * covar=matrix(0,NCOVMAX,1,n);
1551: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*age; */
1552: double **coqvar; /* Fixed quantitative covariate nqv */
1553: double ***cotvar; /* Time varying covariate start at ncovcol + nqv + (1 to ntv) */
1554: double ***cotqvar; /* Time varying quantitative covariate itqv */
1555: double idx;
1556: int **nbcode, *Tvar; /**< model=V2 => Tvar[1]= 2 */
1557: /* Some documentation */
1558: /* Design original data
1559: * V1 V2 V3 V4 V5 V6 V7 V8 Weight ddb ddth d1st s1 V9 V10 V11 V12 s2 V9 V10 V11 V12
1560: * < ncovcol=6 > nqv=2 (V7 V8) dv dv dv qtv dv dv dvv qtv
1561: * ntv=3 nqtv=1
1562: * cptcovn number of covariates (not including constant and age or age*age) = number of plus sign + 1 = 10+1=11
1563: * For time varying covariate, quanti or dummies
1564: * cotqvar[wav][iv(1 to nqtv)][i]= [1][12][i]=(V12) quanti
1565: * cotvar[wav][ncovcol+nqv+ iv(1 to nqtv)][i]= [(1 to nqtv)][i]=(V12) quanti
1566: * cotvar[wav][iv(1 to ntv)][i]= [1][1][i]=(V9) dummies at wav 1
1567: * cotvar[wav][iv(1 to ntv)][i]= [1][2][i]=(V10) dummies at wav 1
1568: * covar[Vk,i], value of the Vkth fixed covariate dummy or quanti for individual i:
1569: * covar[1][i]= (V1), covar[4][i]=(V4), covar[8][i]=(V8)
1570: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8 + V9 + V9*age + V10
1571: * k= 1 2 3 4 5 6 7 8 9 10 11
1572: */
1573: /* According to the model, more columns can be added to covar by the product of covariates */
1574: /* 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
1575: # States 1=Coresidence, 2 Living alone, 3 Institution
1576: # V1=sex, V2=raedyrs Quant Fixed, State=livarnb4..livarnb11, V3=iadl4..iald11, V4=adlw4..adlw11, V5=r4bmi..r11bmi
1577: */
1578: /* V5+V4+ V3+V4*V3 +V5*age+V2 +V1*V2+V1*age+V1 */
1579: /* kmodel 1 2 3 4 5 6 7 8 9 */
1580: /*Typevar[k]= 0 0 0 2 1 0 2 1 0 *//*0 for simple covariate (dummy, quantitative,*/
1581: /* fixed or varying), 1 for age product, 2 for*/
1582: /* product */
1583: /*Dummy[k]= 1 0 0 1 3 1 1 2 0 *//*Dummy[k] 0=dummy (0 1), 1 quantitative */
1584: /*(single or product without age), 2 dummy*/
1585: /* with age product, 3 quant with age product*/
1586: /*Tvar[k]= 5 4 3 6 5 2 7 1 1 */
1587: /* nsd 1 2 3 */ /* Counting single dummies covar fixed or tv */
1588: /*TnsdVar[Tvar] 1 2 3 */
1589: /*Tvaraff[nsd] 4 3 1 */ /* ID of single dummy cova fixed or timevary*/
1590: /*TvarsD[nsd] 4 3 1 */ /* ID of single dummy cova fixed or timevary*/
1591: /*TvarsDind[nsd] 2 3 9 */ /* position K of single dummy cova */
1592: /* nsq 1 2 */ /* Counting single quantit tv */
1593: /* TvarsQ[k] 5 2 */ /* Number of single quantitative cova */
1594: /* TvarsQind 1 6 */ /* position K of single quantitative cova */
1595: /* Tprod[i]=k 1 2 */ /* Position in model of the ith prod without age */
1596: /* cptcovage 1 2 */ /* Counting cov*age in the model equation */
1597: /* Tage[cptcovage]=k 5 8 */ /* Position in the model of ith cov*age */
1598: /* Tvard[1][1]@4={4,3,1,2} V4*V3 V1*V2 */ /* Position in model of the ith prod without age */
1599: /* 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*/
1600: /* 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 */
1601: /* TvarFind; TvarFind[1]=6, TvarFind[2]=7, TvarFind[3]=9 *//* Inverse V2(6) is first fixed (single or prod) */
1602: /* Type */
1603: /* V 1 2 3 4 5 */
1604: /* F F V V V */
1605: /* D Q D D Q */
1606: /* */
1607: int *TvarsD;
1608: int *TnsdVar;
1609: int *TvarsDind;
1610: int *TvarsQ;
1611: int *TvarsQind;
1612:
1613: #define MAXRESULTLINESPONE 10+1
1614: int nresult=0;
1615: int parameterline=0; /* # of the parameter (type) line */
1616: int TKresult[MAXRESULTLINESPONE]; /* TKresult[nres]=k for each resultline nres give the corresponding combination of dummies */
1617: int resultmodel[MAXRESULTLINESPONE][NCOVMAX];/* resultmodel[k1]=k3: k1th position in the model corresponds to the k3 position in the resultline */
1618: int modelresult[MAXRESULTLINESPONE][NCOVMAX];/* modelresult[k3]=k1: k1th position in the model corresponds to the k3 position in the resultline */
1619: int Tresult[MAXRESULTLINESPONE][NCOVMAX];/* Tresult[nres][result_position]= value of the dummy variable at the result_position in the nres resultline */
1620: int Tinvresult[MAXRESULTLINESPONE][NCOVMAX];/* Tinvresult[nres][Name of a dummy variable]= value of the variable in the result line */
1621: double TinvDoQresult[MAXRESULTLINESPONE][NCOVMAX];/* TinvDoQresult[nres][Name of a Dummy or Q variable]= value of the variable in the result line */
1622: int Tvresult[MAXRESULTLINESPONE][NCOVMAX]; /* Tvresult[nres][result_position]= name of the dummy variable at the result_position in the nres resultline */
1623: double Tqresult[MAXRESULTLINESPONE][NCOVMAX]; /* Tqresult[nres][result_position]= value of the variable at the result_position in the nres resultline */
1624: double Tqinvresult[MAXRESULTLINESPONE][NCOVMAX]; /* For quantitative variable , value (output) */
1625: int Tvqresult[MAXRESULTLINESPONE][NCOVMAX]; /* Tvqresult[nres][result_position]= id of the variable at the result_position in the nres resultline */
1626:
1627: /* 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
1628: # States 1=Coresidence, 2 Living alone, 3 Institution
1629: # V1=sex, V2=raedyrs Quant Fixed, State=livarnb4..livarnb11, V3=iadl4..iald11, V4=adlw4..adlw11, V5=r4bmi..r11bmi
1630: */
1631: /* 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 *\/ */
1632: 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 */
1633: 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 */
1634: 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 */
1635: int *TvarVind; /**< TvarVind[1]=1, TvarVind[2]=2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1636: 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 */
1637: int *TvarAind; /**< TvarindA[1]=5, TvarAind[2]=8 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1638: int *TvarFD; /**< TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1639: int *TvarFDind; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1640: int *TvarFQ; /* TvarFQ[1]=V2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1641: int *TvarFQind; /* TvarFQind[1]=6 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1642: int *TvarVD; /* TvarVD[1]=V5 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1643: int *TvarVDind; /* TvarVDind[1]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1644: 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 */
1645: 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 */
1646: int *TvarVV; /* We count ncovvt time varying covariates (single or products without age) and put their name into TvarVV */
1647: int *TvarVVind; /* We count ncovvt time varying covariates (single or products without age) and put their name into TvarVV */
1648: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
1649: /* model V1+V3+age*V1+age*V3+V1*V3 */
1650: /* Tvar={1, 3, 1, 3, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
1651: /* TvarVV={3,1,3}, for V3 and then the product V1*V3 is decomposed into V1 and V3 */
1652: /* TvarVVind={2,5,5}, for V3 and then the product V1*V3 is decomposed into V1 and V3 */
1653: int *Tvarsel; /**< Selected covariates for output */
1654: double *Tvalsel; /**< Selected modality value of covariate for output */
1655: int *Typevar; /**< 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product */
1656: int *Fixed; /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
1657: 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 */
1658: int *DummyV; /** Dummy[v] 0=dummy (0 1), 1 quantitative */
1659: int *FixedV; /** FixedV[v] 0 fixed, 1 varying */
1660: int *Tage;
1661: int anyvaryingduminmodel=0; /**< Any varying dummy in Model=1 yes, 0 no, to avoid a loop on waves in freq */
1662: 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*/
1663: 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*/
1664: 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 */
1665: int *Ndum; /** Freq of modality (tricode */
1666: /* int **codtab;*/ /**< codtab=imatrix(1,100,1,10); */
1667: int **Tvard;
1668: int **Tvardk;
1669: int *Tprod;/**< Gives the k position of the k1 product */
1670: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3 */
1671: int *Tposprod; /**< Gives the k1 product from the k position */
1672: /* if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2) */
1673: /* Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5(V3*V2)]=2 (2nd product without age) */
1674: int cptcovprod, *Tvaraff, *invalidvarcomb;
1675: double *lsurv, *lpop, *tpop;
1676:
1677: #define FD 1; /* Fixed dummy covariate */
1678: #define FQ 2; /* Fixed quantitative covariate */
1679: #define FP 3; /* Fixed product covariate */
1680: #define FPDD 7; /* Fixed product dummy*dummy covariate */
1681: #define FPDQ 8; /* Fixed product dummy*quantitative covariate */
1682: #define FPQQ 9; /* Fixed product quantitative*quantitative covariate */
1683: #define VD 10; /* Varying dummy covariate */
1684: #define VQ 11; /* Varying quantitative covariate */
1685: #define VP 12; /* Varying product covariate */
1686: #define VPDD 13; /* Varying product dummy*dummy covariate */
1687: #define VPDQ 14; /* Varying product dummy*quantitative covariate */
1688: #define VPQQ 15; /* Varying product quantitative*quantitative covariate */
1689: #define APFD 16; /* Age product * fixed dummy covariate */
1690: #define APFQ 17; /* Age product * fixed quantitative covariate */
1691: #define APVD 18; /* Age product * varying dummy covariate */
1692: #define APVQ 19; /* Age product * varying quantitative covariate */
1693:
1694: #define FTYPE 1; /* Fixed covariate */
1695: #define VTYPE 2; /* Varying covariate (loop in wave) */
1696: #define ATYPE 2; /* Age product covariate (loop in dh within wave)*/
1697:
1698: struct kmodel{
1699: int maintype; /* main type */
1700: int subtype; /* subtype */
1701: };
1702: struct kmodel modell[NCOVMAX];
1703:
1704: double ftol=FTOL; /**< Tolerance for computing Max Likelihood */
1705: double ftolhess; /**< Tolerance for computing hessian */
1706:
1707: /**************** split *************************/
1708: static int split( char *path, char *dirc, char *name, char *ext, char *finame )
1709: {
1710: /* From a file name with (full) path (either Unix or Windows) we extract the directory (dirc)
1711: the name of the file (name), its extension only (ext) and its first part of the name (finame)
1712: */
1713: char *ss; /* pointer */
1714: int l1=0, l2=0; /* length counters */
1715:
1716: l1 = strlen(path ); /* length of path */
1717: if ( l1 == 0 ) return( GLOCK_ERROR_NOPATH );
1718: ss= strrchr( path, DIRSEPARATOR ); /* find last / */
1719: if ( ss == NULL ) { /* no directory, so determine current directory */
1720: strcpy( name, path ); /* we got the fullname name because no directory */
1721: /*if(strrchr(path, ODIRSEPARATOR )==NULL)
1722: printf("Warning you should use %s as a separator\n",DIRSEPARATOR);*/
1723: /* get current working directory */
1724: /* extern char* getcwd ( char *buf , int len);*/
1725: #ifdef WIN32
1726: if (_getcwd( dirc, FILENAME_MAX ) == NULL ) {
1727: #else
1728: if (getcwd(dirc, FILENAME_MAX) == NULL) {
1729: #endif
1730: return( GLOCK_ERROR_GETCWD );
1731: }
1732: /* got dirc from getcwd*/
1733: printf(" DIRC = %s \n",dirc);
1734: } else { /* strip directory from path */
1735: ss++; /* after this, the filename */
1736: l2 = strlen( ss ); /* length of filename */
1737: if ( l2 == 0 ) return( GLOCK_ERROR_NOPATH );
1738: strcpy( name, ss ); /* save file name */
1739: strncpy( dirc, path, l1 - l2 ); /* now the directory */
1740: dirc[l1-l2] = '\0'; /* add zero */
1741: printf(" DIRC2 = %s \n",dirc);
1742: }
1743: /* We add a separator at the end of dirc if not exists */
1744: l1 = strlen( dirc ); /* length of directory */
1745: if( dirc[l1-1] != DIRSEPARATOR ){
1746: dirc[l1] = DIRSEPARATOR;
1747: dirc[l1+1] = 0;
1748: printf(" DIRC3 = %s \n",dirc);
1749: }
1750: ss = strrchr( name, '.' ); /* find last / */
1751: if (ss >0){
1752: ss++;
1753: strcpy(ext,ss); /* save extension */
1754: l1= strlen( name);
1755: l2= strlen(ss)+1;
1756: strncpy( finame, name, l1-l2);
1757: finame[l1-l2]= 0;
1758: }
1759:
1760: return( 0 ); /* we're done */
1761: }
1762:
1763:
1764: /******************************************/
1765:
1766: void replace_back_to_slash(char *s, char*t)
1767: {
1768: int i;
1769: int lg=0;
1770: i=0;
1771: lg=strlen(t);
1772: for(i=0; i<= lg; i++) {
1773: (s[i] = t[i]);
1774: if (t[i]== '\\') s[i]='/';
1775: }
1776: }
1777:
1778: char *trimbb(char *out, char *in)
1779: { /* Trim multiple blanks in line but keeps first blanks if line starts with blanks */
1780: char *s;
1781: s=out;
1782: while (*in != '\0'){
1783: while( *in == ' ' && *(in+1) == ' '){ /* && *(in+1) != '\0'){*/
1784: in++;
1785: }
1786: *out++ = *in++;
1787: }
1788: *out='\0';
1789: return s;
1790: }
1791:
1792: /* char *substrchaine(char *out, char *in, char *chain) */
1793: /* { */
1794: /* /\* Substract chain 'chain' from 'in', return and output 'out' *\/ */
1795: /* char *s, *t; */
1796: /* t=in;s=out; */
1797: /* while ((*in != *chain) && (*in != '\0')){ */
1798: /* *out++ = *in++; */
1799: /* } */
1800:
1801: /* /\* *in matches *chain *\/ */
1802: /* while ((*in++ == *chain++) && (*in != '\0')){ */
1803: /* printf("*in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1804: /* } */
1805: /* in--; chain--; */
1806: /* while ( (*in != '\0')){ */
1807: /* printf("Bef *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1808: /* *out++ = *in++; */
1809: /* printf("Aft *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1810: /* } */
1811: /* *out='\0'; */
1812: /* out=s; */
1813: /* return out; */
1814: /* } */
1815: char *substrchaine(char *out, char *in, char *chain)
1816: {
1817: /* Substract chain 'chain' from 'in', return and output 'out' */
1818: /* in="V1+V1*age+age*age+V2", chain="age*age" */
1819:
1820: char *strloc;
1821:
1822: strcpy (out, in);
1823: strloc = strstr(out, chain); /* strloc points to out at age*age+V2 */
1824: printf("Bef strloc=%s chain=%s out=%s \n", strloc, chain, out);
1825: if(strloc != NULL){
1826: /* will affect out */ /* strloc+strlenc(chain)=+V2 */ /* Will also work in Unicode */
1827: memmove(strloc,strloc+strlen(chain), strlen(strloc+strlen(chain))+1);
1828: /* strcpy (strloc, strloc +strlen(chain));*/
1829: }
1830: printf("Aft strloc=%s chain=%s in=%s out=%s \n", strloc, chain, in, out);
1831: return out;
1832: }
1833:
1834:
1835: char *cutl(char *blocc, char *alocc, char *in, char occ)
1836: {
1837: /* cuts string in into blocc and alocc where blocc ends before FIRST occurence of char 'occ'
1838: and alocc starts after first occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
1839: gives alocc="abcdef" and blocc="ghi2j".
1840: If occ is not found blocc is null and alocc is equal to in. Returns blocc
1841: */
1842: char *s, *t;
1843: t=in;s=in;
1844: while ((*in != occ) && (*in != '\0')){
1845: *alocc++ = *in++;
1846: }
1847: if( *in == occ){
1848: *(alocc)='\0';
1849: s=++in;
1850: }
1851:
1852: if (s == t) {/* occ not found */
1853: *(alocc-(in-s))='\0';
1854: in=s;
1855: }
1856: while ( *in != '\0'){
1857: *blocc++ = *in++;
1858: }
1859:
1860: *blocc='\0';
1861: return t;
1862: }
1863: char *cutv(char *blocc, char *alocc, char *in, char occ)
1864: {
1865: /* cuts string in into blocc and alocc where blocc ends before LAST occurence of char 'occ'
1866: and alocc starts after last occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
1867: gives blocc="abcdef2ghi" and alocc="j".
1868: If occ is not found blocc is null and alocc is equal to in. Returns alocc
1869: */
1870: char *s, *t;
1871: t=in;s=in;
1872: while (*in != '\0'){
1873: while( *in == occ){
1874: *blocc++ = *in++;
1875: s=in;
1876: }
1877: *blocc++ = *in++;
1878: }
1879: if (s == t) /* occ not found */
1880: *(blocc-(in-s))='\0';
1881: else
1882: *(blocc-(in-s)-1)='\0';
1883: in=s;
1884: while ( *in != '\0'){
1885: *alocc++ = *in++;
1886: }
1887:
1888: *alocc='\0';
1889: return s;
1890: }
1891:
1892: int nbocc(char *s, char occ)
1893: {
1894: int i,j=0;
1895: int lg=20;
1896: i=0;
1897: lg=strlen(s);
1898: for(i=0; i<= lg; i++) {
1899: if (s[i] == occ ) j++;
1900: }
1901: return j;
1902: }
1903:
1904: /* void cutv(char *u,char *v, char*t, char occ) */
1905: /* { */
1906: /* /\* cuts string t into u and v where u ends before last occurence of char 'occ' */
1907: /* and v starts after last occurence of char 'occ' : ex cutv(u,v,"abcdef2ghi2j",'2') */
1908: /* gives u="abcdef2ghi" and v="j" *\/ */
1909: /* int i,lg,j,p=0; */
1910: /* i=0; */
1911: /* lg=strlen(t); */
1912: /* for(j=0; j<=lg-1; j++) { */
1913: /* if((t[j]!= occ) && (t[j+1]== occ)) p=j+1; */
1914: /* } */
1915:
1916: /* for(j=0; j<p; j++) { */
1917: /* (u[j] = t[j]); */
1918: /* } */
1919: /* u[p]='\0'; */
1920:
1921: /* for(j=0; j<= lg; j++) { */
1922: /* if (j>=(p+1))(v[j-p-1] = t[j]); */
1923: /* } */
1924: /* } */
1925:
1926: #ifdef _WIN32
1927: char * strsep(char **pp, const char *delim)
1928: {
1929: char *p, *q;
1930:
1931: if ((p = *pp) == NULL)
1932: return 0;
1933: if ((q = strpbrk (p, delim)) != NULL)
1934: {
1935: *pp = q + 1;
1936: *q = '\0';
1937: }
1938: else
1939: *pp = 0;
1940: return p;
1941: }
1942: #endif
1943:
1944: /********************** nrerror ********************/
1945:
1946: void nrerror(char error_text[])
1947: {
1948: fprintf(stderr,"ERREUR ...\n");
1949: fprintf(stderr,"%s\n",error_text);
1950: exit(EXIT_FAILURE);
1951: }
1952: /*********************** vector *******************/
1953: double *vector(int nl, int nh)
1954: {
1955: double *v;
1956: v=(double *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(double)));
1957: if (!v) nrerror("allocation failure in vector");
1958: return v-nl+NR_END;
1959: }
1960:
1961: /************************ free vector ******************/
1962: void free_vector(double*v, int nl, int nh)
1963: {
1964: free((FREE_ARG)(v+nl-NR_END));
1965: }
1966:
1967: /************************ivector *******************************/
1968: int *ivector(long nl,long nh)
1969: {
1970: int *v;
1971: v=(int *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(int)));
1972: if (!v) nrerror("allocation failure in ivector");
1973: return v-nl+NR_END;
1974: }
1975:
1976: /******************free ivector **************************/
1977: void free_ivector(int *v, long nl, long nh)
1978: {
1979: free((FREE_ARG)(v+nl-NR_END));
1980: }
1981:
1982: /************************lvector *******************************/
1983: long *lvector(long nl,long nh)
1984: {
1985: long *v;
1986: v=(long *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(long)));
1987: if (!v) nrerror("allocation failure in ivector");
1988: return v-nl+NR_END;
1989: }
1990:
1991: /******************free lvector **************************/
1992: void free_lvector(long *v, long nl, long nh)
1993: {
1994: free((FREE_ARG)(v+nl-NR_END));
1995: }
1996:
1997: /******************* imatrix *******************************/
1998: int **imatrix(long nrl, long nrh, long ncl, long nch)
1999: /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
2000: {
2001: long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
2002: int **m;
2003:
2004: /* allocate pointers to rows */
2005: m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
2006: if (!m) nrerror("allocation failure 1 in matrix()");
2007: m += NR_END;
2008: m -= nrl;
2009:
2010:
2011: /* allocate rows and set pointers to them */
2012: m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
2013: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
2014: m[nrl] += NR_END;
2015: m[nrl] -= ncl;
2016:
2017: for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
2018:
2019: /* return pointer to array of pointers to rows */
2020: return m;
2021: }
2022:
2023: /****************** free_imatrix *************************/
2024: void free_imatrix(m,nrl,nrh,ncl,nch)
2025: int **m;
2026: long nch,ncl,nrh,nrl;
2027: /* free an int matrix allocated by imatrix() */
2028: {
2029: free((FREE_ARG) (m[nrl]+ncl-NR_END));
2030: free((FREE_ARG) (m+nrl-NR_END));
2031: }
2032:
2033: /******************* matrix *******************************/
2034: double **matrix(long nrl, long nrh, long ncl, long nch)
2035: {
2036: long i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
2037: double **m;
2038:
2039: m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
2040: if (!m) nrerror("allocation failure 1 in matrix()");
2041: m += NR_END;
2042: m -= nrl;
2043:
2044: m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
2045: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
2046: m[nrl] += NR_END;
2047: m[nrl] -= ncl;
2048:
2049: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
2050: return m;
2051: /* print *(*(m+1)+70) or print m[1][70]; print m+1 or print &(m[1]) or &(m[1][0])
2052: m[i] = address of ith row of the table. &(m[i]) is its value which is another adress
2053: that of m[i][0]. In order to get the value p m[i][0] but it is unitialized.
2054: */
2055: }
2056:
2057: /*************************free matrix ************************/
2058: void free_matrix(double **m, long nrl, long nrh, long ncl, long nch)
2059: {
2060: free((FREE_ARG)(m[nrl]+ncl-NR_END));
2061: free((FREE_ARG)(m+nrl-NR_END));
2062: }
2063:
2064: /******************* ma3x *******************************/
2065: double ***ma3x(long nrl, long nrh, long ncl, long nch, long nll, long nlh)
2066: {
2067: long i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1, nlay=nlh-nll+1;
2068: double ***m;
2069:
2070: m=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
2071: if (!m) nrerror("allocation failure 1 in matrix()");
2072: m += NR_END;
2073: m -= nrl;
2074:
2075: m[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
2076: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
2077: m[nrl] += NR_END;
2078: m[nrl] -= ncl;
2079:
2080: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
2081:
2082: m[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*nlay+NR_END)*sizeof(double)));
2083: if (!m[nrl][ncl]) nrerror("allocation failure 3 in matrix()");
2084: m[nrl][ncl] += NR_END;
2085: m[nrl][ncl] -= nll;
2086: for (j=ncl+1; j<=nch; j++)
2087: m[nrl][j]=m[nrl][j-1]+nlay;
2088:
2089: for (i=nrl+1; i<=nrh; i++) {
2090: m[i][ncl]=m[i-1l][ncl]+ncol*nlay;
2091: for (j=ncl+1; j<=nch; j++)
2092: m[i][j]=m[i][j-1]+nlay;
2093: }
2094: return m;
2095: /* gdb: p *(m+1) <=> p m[1] and p (m+1) <=> p (m+1) <=> p &(m[1])
2096: &(m[i][j][k]) <=> *((*(m+i) + j)+k)
2097: */
2098: }
2099:
2100: /*************************free ma3x ************************/
2101: void free_ma3x(double ***m, long nrl, long nrh, long ncl, long nch,long nll, long nlh)
2102: {
2103: free((FREE_ARG)(m[nrl][ncl]+ nll-NR_END));
2104: free((FREE_ARG)(m[nrl]+ncl-NR_END));
2105: free((FREE_ARG)(m+nrl-NR_END));
2106: }
2107:
2108: /*************** function subdirf ***********/
2109: char *subdirf(char fileres[])
2110: {
2111: /* Caution optionfilefiname is hidden */
2112: strcpy(tmpout,optionfilefiname);
2113: strcat(tmpout,"/"); /* Add to the right */
2114: strcat(tmpout,fileres);
2115: return tmpout;
2116: }
2117:
2118: /*************** function subdirf2 ***********/
2119: char *subdirf2(char fileres[], char *preop)
2120: {
2121: /* Example subdirf2(optionfilefiname,"FB_") with optionfilefiname="texte", result="texte/FB_texte"
2122: Errors in subdirf, 2, 3 while printing tmpout is
2123: rewritten within the same printf. Workaround: many printfs */
2124: /* Caution optionfilefiname is hidden */
2125: strcpy(tmpout,optionfilefiname);
2126: strcat(tmpout,"/");
2127: strcat(tmpout,preop);
2128: strcat(tmpout,fileres);
2129: return tmpout;
2130: }
2131:
2132: /*************** function subdirf3 ***********/
2133: char *subdirf3(char fileres[], char *preop, char *preop2)
2134: {
2135:
2136: /* Caution optionfilefiname is hidden */
2137: strcpy(tmpout,optionfilefiname);
2138: strcat(tmpout,"/");
2139: strcat(tmpout,preop);
2140: strcat(tmpout,preop2);
2141: strcat(tmpout,fileres);
2142: return tmpout;
2143: }
2144:
2145: /*************** function subdirfext ***********/
2146: char *subdirfext(char fileres[], char *preop, char *postop)
2147: {
2148:
2149: strcpy(tmpout,preop);
2150: strcat(tmpout,fileres);
2151: strcat(tmpout,postop);
2152: return tmpout;
2153: }
2154:
2155: /*************** function subdirfext3 ***********/
2156: char *subdirfext3(char fileres[], char *preop, char *postop)
2157: {
2158:
2159: /* Caution optionfilefiname is hidden */
2160: strcpy(tmpout,optionfilefiname);
2161: strcat(tmpout,"/");
2162: strcat(tmpout,preop);
2163: strcat(tmpout,fileres);
2164: strcat(tmpout,postop);
2165: return tmpout;
2166: }
2167:
2168: char *asc_diff_time(long time_sec, char ascdiff[])
2169: {
2170: long sec_left, days, hours, minutes;
2171: days = (time_sec) / (60*60*24);
2172: sec_left = (time_sec) % (60*60*24);
2173: hours = (sec_left) / (60*60) ;
2174: sec_left = (sec_left) %(60*60);
2175: minutes = (sec_left) /60;
2176: sec_left = (sec_left) % (60);
2177: sprintf(ascdiff,"%ld day(s) %ld hour(s) %ld minute(s) %ld second(s)",days, hours, minutes, sec_left);
2178: return ascdiff;
2179: }
2180:
2181: /***************** f1dim *************************/
2182: extern int ncom;
2183: extern double *pcom,*xicom;
2184: extern double (*nrfunc)(double []);
2185:
2186: double f1dim(double x)
2187: {
2188: int j;
2189: double f;
2190: double *xt;
2191:
2192: xt=vector(1,ncom);
2193: for (j=1;j<=ncom;j++) xt[j]=pcom[j]+x*xicom[j];
2194: f=(*nrfunc)(xt);
2195: free_vector(xt,1,ncom);
2196: return f;
2197: }
2198:
2199: /*****************brent *************************/
2200: double brent(double ax, double bx, double cx, double (*f)(double), double tol, double *xmin)
2201: {
2202: /* Given a function f, and given a bracketing triplet of abscissas ax, bx, cx (such that bx is
2203: * between ax and cx, and f(bx) is less than both f(ax) and f(cx) ), this routine isolates
2204: * the minimum to a fractional precision of about tol using Brent’s method. The abscissa of
2205: * the minimum is returned as xmin, and the minimum function value is returned as brent , the
2206: * returned function value.
2207: */
2208: int iter;
2209: double a,b,d,etemp;
2210: double fu=0,fv,fw,fx;
2211: double ftemp=0.;
2212: double p,q,r,tol1,tol2,u,v,w,x,xm;
2213: double e=0.0;
2214:
2215: a=(ax < cx ? ax : cx);
2216: b=(ax > cx ? ax : cx);
2217: x=w=v=bx;
2218: fw=fv=fx=(*f)(x);
2219: for (iter=1;iter<=ITMAX;iter++) {
2220: xm=0.5*(a+b);
2221: tol2=2.0*(tol1=tol*fabs(x)+ZEPS);
2222: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret)))*/
2223: printf(".");fflush(stdout);
2224: fprintf(ficlog,".");fflush(ficlog);
2225: #ifdef DEBUGBRENT
2226: 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);
2227: 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);
2228: /* if ((fabs(x-xm) <= (tol2-0.5*(b-a)))||(2.0*fabs(fu-ftemp) <= ftol*1.e-2*(fabs(fu)+fabs(ftemp)))) { */
2229: #endif
2230: if (fabs(x-xm) <= (tol2-0.5*(b-a))){
2231: *xmin=x;
2232: return fx;
2233: }
2234: ftemp=fu;
2235: if (fabs(e) > tol1) {
2236: r=(x-w)*(fx-fv);
2237: q=(x-v)*(fx-fw);
2238: p=(x-v)*q-(x-w)*r;
2239: q=2.0*(q-r);
2240: if (q > 0.0) p = -p;
2241: q=fabs(q);
2242: etemp=e;
2243: e=d;
2244: if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x))
2245: d=CGOLD*(e=(x >= xm ? a-x : b-x));
2246: else {
2247: d=p/q;
2248: u=x+d;
2249: if (u-a < tol2 || b-u < tol2)
2250: d=SIGN(tol1,xm-x);
2251: }
2252: } else {
2253: d=CGOLD*(e=(x >= xm ? a-x : b-x));
2254: }
2255: u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d));
2256: fu=(*f)(u);
2257: if (fu <= fx) {
2258: if (u >= x) a=x; else b=x;
2259: SHFT(v,w,x,u)
2260: SHFT(fv,fw,fx,fu)
2261: } else {
2262: if (u < x) a=u; else b=u;
2263: if (fu <= fw || w == x) {
2264: v=w;
2265: w=u;
2266: fv=fw;
2267: fw=fu;
2268: } else if (fu <= fv || v == x || v == w) {
2269: v=u;
2270: fv=fu;
2271: }
2272: }
2273: }
2274: nrerror("Too many iterations in brent");
2275: *xmin=x;
2276: return fx;
2277: }
2278:
2279: /****************** mnbrak ***********************/
2280:
2281: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, double *fc,
2282: double (*func)(double))
2283: { /* Given a function func , and given distinct initial points ax and bx , this routine searches in
2284: the downhill direction (defined by the function as evaluated at the initial points) and returns
2285: new points ax , bx , cx that bracket a minimum of the function. Also returned are the function
2286: values at the three points, fa, fb , and fc such that fa > fb and fb < fc.
2287: */
2288: double ulim,u,r,q, dum;
2289: double fu;
2290:
2291: double scale=10.;
2292: int iterscale=0;
2293:
2294: *fa=(*func)(*ax); /* xta[j]=pcom[j]+(*ax)*xicom[j]; fa=f(xta[j])*/
2295: *fb=(*func)(*bx); /* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) */
2296:
2297:
2298: /* while(*fb != *fb){ /\* *ax should be ok, reducing distance to *ax *\/ */
2299: /* printf("Warning mnbrak *fb = %lf, *bx=%lf *ax=%lf *fa==%lf iter=%d\n",*fb, *bx, *ax, *fa, iterscale++); */
2300: /* *bx = *ax - (*ax - *bx)/scale; */
2301: /* *fb=(*func)(*bx); /\* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) *\/ */
2302: /* } */
2303:
2304: if (*fb > *fa) {
2305: SHFT(dum,*ax,*bx,dum)
2306: SHFT(dum,*fb,*fa,dum)
2307: }
2308: *cx=(*bx)+GOLD*(*bx-*ax);
2309: *fc=(*func)(*cx);
2310: #ifdef DEBUG
2311: printf("mnbrak0 a=%lf *fa=%lf, b=%lf *fb=%lf, c=%lf *fc=%lf\n",*ax,*fa,*bx,*fb,*cx, *fc);
2312: fprintf(ficlog,"mnbrak0 a=%lf *fa=%lf, b=%lf *fb=%lf, c=%lf *fc=%lf\n",*ax,*fa,*bx,*fb,*cx, *fc);
2313: #endif
2314: while (*fb > *fc) { /* Declining a,b,c with fa> fb > fc. If fc=inf it exits and if flat fb=fc it exits too.*/
2315: r=(*bx-*ax)*(*fb-*fc);
2316: q=(*bx-*cx)*(*fb-*fa); /* What if fa=inf */
2317: u=(*bx)-((*bx-*cx)*q-(*bx-*ax)*r)/
2318: (2.0*SIGN(FMAX(fabs(q-r),TINY),q-r)); /* Minimum abscissa of a parabolic estimated from (a,fa), (b,fb) and (c,fc). */
2319: ulim=(*bx)+GLIMIT*(*cx-*bx); /* Maximum abscissa where function should be evaluated */
2320: if ((*bx-u)*(u-*cx) > 0.0) { /* if u_p is between b and c */
2321: fu=(*func)(u);
2322: #ifdef DEBUG
2323: /* f(x)=A(x-u)**2+f(u) */
2324: double A, fparabu;
2325: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
2326: fparabu= *fa - A*(*ax-u)*(*ax-u);
2327: 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);
2328: 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);
2329: /* And thus,it can be that fu > *fc even if fparabu < *fc */
2330: /* mnbrak (*ax=7.666299858533, *fa=299039.693133272231), (*bx=8.595447774979, *fb=298976.598289369489),
2331: (*cx=10.098840694817, *fc=298946.631474258087), (*u=9.852501168332, fu=298948.773013752128, fparabu=298945.434711494134) */
2332: /* In that case, there is no bracket in the output! Routine is wrong with many consequences.*/
2333: #endif
2334: #ifdef MNBRAKORIGINAL
2335: #else
2336: /* if (fu > *fc) { */
2337: /* #ifdef DEBUG */
2338: /* printf("mnbrak4 fu > fc \n"); */
2339: /* fprintf(ficlog, "mnbrak4 fu > fc\n"); */
2340: /* #endif */
2341: /* /\* 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 *\\/ *\/ */
2342: /* /\* SHFT(*fa,*fc,fu,*fc) /\\* (b, u, c) is a bracket while test fb > fc will be fu > fc will exit *\\/ *\/ */
2343: /* dum=u; /\* Shifting c and u *\/ */
2344: /* u = *cx; */
2345: /* *cx = dum; */
2346: /* dum = fu; */
2347: /* fu = *fc; */
2348: /* *fc =dum; */
2349: /* } else { /\* end *\/ */
2350: /* #ifdef DEBUG */
2351: /* printf("mnbrak3 fu < fc \n"); */
2352: /* fprintf(ficlog, "mnbrak3 fu < fc\n"); */
2353: /* #endif */
2354: /* dum=u; /\* Shifting c and u *\/ */
2355: /* u = *cx; */
2356: /* *cx = dum; */
2357: /* dum = fu; */
2358: /* fu = *fc; */
2359: /* *fc =dum; */
2360: /* } */
2361: #ifdef DEBUGMNBRAK
2362: double A, fparabu;
2363: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
2364: fparabu= *fa - A*(*ax-u)*(*ax-u);
2365: 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);
2366: 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);
2367: #endif
2368: dum=u; /* Shifting c and u */
2369: u = *cx;
2370: *cx = dum;
2371: dum = fu;
2372: fu = *fc;
2373: *fc =dum;
2374: #endif
2375: } else if ((*cx-u)*(u-ulim) > 0.0) { /* u is after c but before ulim */
2376: #ifdef DEBUG
2377: printf("\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
2378: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
2379: #endif
2380: fu=(*func)(u);
2381: if (fu < *fc) {
2382: #ifdef DEBUG
2383: printf("\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
2384: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
2385: #endif
2386: SHFT(*bx,*cx,u,*cx+GOLD*(*cx-*bx))
2387: SHFT(*fb,*fc,fu,(*func)(u))
2388: #ifdef DEBUG
2389: printf("\nmnbrak2 shift GOLD c=%lf",*cx+GOLD*(*cx-*bx));
2390: #endif
2391: }
2392: } else if ((u-ulim)*(ulim-*cx) >= 0.0) { /* u outside ulim (verifying that ulim is beyond c) */
2393: #ifdef DEBUG
2394: printf("\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
2395: fprintf(ficlog,"\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
2396: #endif
2397: u=ulim;
2398: fu=(*func)(u);
2399: } else { /* u could be left to b (if r > q parabola has a maximum) */
2400: #ifdef DEBUG
2401: printf("\nmnbrak2 u=%lf could be left to b=%lf (if r=%lf > q=%lf parabola has a maximum)\n",u,*bx,r,q);
2402: 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);
2403: #endif
2404: u=(*cx)+GOLD*(*cx-*bx);
2405: fu=(*func)(u);
2406: #ifdef DEBUG
2407: printf("\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
2408: fprintf(ficlog,"\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
2409: #endif
2410: } /* end tests */
2411: SHFT(*ax,*bx,*cx,u)
2412: SHFT(*fa,*fb,*fc,fu)
2413: #ifdef DEBUG
2414: printf("\nmnbrak2 shift (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf)\n",*ax,*fa,*bx,*fb,*cx,*fc);
2415: fprintf(ficlog, "\nmnbrak2 shift (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf)\n",*ax,*fa,*bx,*fb,*cx,*fc);
2416: #endif
2417: } /* end while; ie return (a, b, c, fa, fb, fc) such that a < b < c with f(a) > f(b) and fb < f(c) */
2418: }
2419:
2420: /*************** linmin ************************/
2421: /* Given an n -dimensional point p[1..n] and an n -dimensional direction xi[1..n] , moves and
2422: resets p to where the function func(p) takes on a minimum along the direction xi from p ,
2423: and replaces xi by the actual vector displacement that p was moved. Also returns as fret
2424: the value of func at the returned location p . This is actually all accomplished by calling the
2425: routines mnbrak and brent .*/
2426: int ncom;
2427: double *pcom,*xicom;
2428: double (*nrfunc)(double []);
2429:
2430: #ifdef LINMINORIGINAL
2431: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []))
2432: #else
2433: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []), int *flat)
2434: #endif
2435: {
2436: double brent(double ax, double bx, double cx,
2437: double (*f)(double), double tol, double *xmin);
2438: double f1dim(double x);
2439: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb,
2440: double *fc, double (*func)(double));
2441: int j;
2442: double xx,xmin,bx,ax;
2443: double fx,fb,fa;
2444:
2445: #ifdef LINMINORIGINAL
2446: #else
2447: double scale=10., axs, xxs; /* Scale added for infinity */
2448: #endif
2449:
2450: ncom=n;
2451: pcom=vector(1,n);
2452: xicom=vector(1,n);
2453: nrfunc=func;
2454: for (j=1;j<=n;j++) {
2455: pcom[j]=p[j];
2456: xicom[j]=xi[j]; /* Former scale xi[j] of currrent direction i */
2457: }
2458:
2459: #ifdef LINMINORIGINAL
2460: xx=1.;
2461: #else
2462: axs=0.0;
2463: xxs=1.;
2464: do{
2465: xx= xxs;
2466: #endif
2467: ax=0.;
2468: mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim); /* Outputs: xtx[j]=pcom[j]+(*xx)*xicom[j]; fx=f(xtx[j]) */
2469: /* brackets with inputs ax=0 and xx=1, but points, pcom=p, and directions values, xicom=xi, are sent via f1dim(x) */
2470: /* 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)) */
2471: /* Outputs: fa=f(p(j)) and fx=f(p(j) + xxs * xi(j) ) and f(bx)= f(p(j)+ bx* xi(j)) */
2472: /* Given input ax=axs and xx=xxs, xx might be too far from ax to get a finite f(xx) */
2473: /* Searches on line, outputs (ax, xx, bx) such that fx < min(fa and fb) */
2474: /* 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]]*/
2475: #ifdef LINMINORIGINAL
2476: #else
2477: if (fx != fx){
2478: xxs=xxs/scale; /* Trying a smaller xx, closer to initial ax=0 */
2479: printf("|");
2480: fprintf(ficlog,"|");
2481: #ifdef DEBUGLINMIN
2482: 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);
2483: #endif
2484: }
2485: }while(fx != fx && xxs > 1.e-5);
2486: #endif
2487:
2488: #ifdef DEBUGLINMIN
2489: 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);
2490: 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);
2491: #endif
2492: #ifdef LINMINORIGINAL
2493: #else
2494: if(fb == fx){ /* Flat function in the direction */
2495: xmin=xx;
2496: *flat=1;
2497: }else{
2498: *flat=0;
2499: #endif
2500: /*Flat mnbrak2 shift (*ax=0.000000000000, *fa=51626.272983130431), (*bx=-1.618034000000, *fb=51590.149499362531), (*cx=-4.236068025156, *fc=51590.149499362531) */
2501: *fret=brent(ax,xx,bx,f1dim,TOL,&xmin); /* Giving a bracketting triplet (ax, xx, bx), find a minimum, xmin, according to f1dim, *fret(xmin),*/
2502: /* fa = f(p[j] + ax * xi[j]), fx = f(p[j] + xx * xi[j]), fb = f(p[j] + bx * xi[j]) */
2503: /* fmin = f(p[j] + xmin * xi[j]) */
2504: /* P+lambda n in that direction (lambdamin), with TOL between abscisses */
2505: /* f1dim(xmin): for (j=1;j<=ncom;j++) xt[j]=pcom[j]+xmin*xicom[j]; */
2506: #ifdef DEBUG
2507: 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);
2508: 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);
2509: #endif
2510: #ifdef LINMINORIGINAL
2511: #else
2512: }
2513: #endif
2514: #ifdef DEBUGLINMIN
2515: printf("linmin end ");
2516: fprintf(ficlog,"linmin end ");
2517: #endif
2518: for (j=1;j<=n;j++) {
2519: #ifdef LINMINORIGINAL
2520: xi[j] *= xmin;
2521: #else
2522: #ifdef DEBUGLINMIN
2523: if(xxs <1.0)
2524: printf(" before xi[%d]=%12.8f", j,xi[j]);
2525: #endif
2526: 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) */
2527: #ifdef DEBUGLINMIN
2528: if(xxs <1.0)
2529: 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 );
2530: #endif
2531: #endif
2532: p[j] += xi[j]; /* Parameters values are updated accordingly */
2533: }
2534: #ifdef DEBUGLINMIN
2535: printf("\n");
2536: printf("Comparing last *frec(xmin=%12.8f)=%12.8f from Brent and frec(0.)=%12.8f \n", xmin, *fret, (*func)(p));
2537: fprintf(ficlog,"Comparing last *frec(xmin=%12.8f)=%12.8f from Brent and frec(0.)=%12.8f \n", xmin, *fret, (*func)(p));
2538: for (j=1;j<=n;j++) {
2539: printf(" xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
2540: fprintf(ficlog," xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
2541: if(j % ncovmodel == 0){
2542: printf("\n");
2543: fprintf(ficlog,"\n");
2544: }
2545: }
2546: #else
2547: #endif
2548: free_vector(xicom,1,n);
2549: free_vector(pcom,1,n);
2550: }
2551:
2552:
2553: /*************** powell ************************/
2554: /*
2555: Minimization of a function func of n variables. Input consists in an initial starting point
2556: p[1..n] ; an initial matrix xi[1..n][1..n] whose columns contain the initial set of di-
2557: rections (usually the n unit vectors); and ftol, the fractional tolerance in the function value
2558: such that failure to decrease by more than this amount in one iteration signals doneness. On
2559: output, p is set to the best point found, xi is the then-current direction set, fret is the returned
2560: function value at p , and iter is the number of iterations taken. The routine linmin is used.
2561: */
2562: #ifdef LINMINORIGINAL
2563: #else
2564: int *flatdir; /* Function is vanishing in that direction */
2565: int flat=0, flatd=0; /* Function is vanishing in that direction */
2566: #endif
2567: void powell(double p[], double **xi, int n, double ftol, int *iter, double *fret,
2568: double (*func)(double []))
2569: {
2570: #ifdef LINMINORIGINAL
2571: void linmin(double p[], double xi[], int n, double *fret,
2572: double (*func)(double []));
2573: #else
2574: void linmin(double p[], double xi[], int n, double *fret,
2575: double (*func)(double []),int *flat);
2576: #endif
2577: int i,ibig,j,jk,k;
2578: double del,t,*pt,*ptt,*xit;
2579: double directest;
2580: double fp,fptt;
2581: double *xits;
2582: int niterf, itmp;
2583:
2584: pt=vector(1,n);
2585: ptt=vector(1,n);
2586: xit=vector(1,n);
2587: xits=vector(1,n);
2588: *fret=(*func)(p);
2589: for (j=1;j<=n;j++) pt[j]=p[j];
2590: rcurr_time = time(NULL);
2591: fp=(*fret); /* Initialisation */
2592: for (*iter=1;;++(*iter)) {
2593: ibig=0;
2594: del=0.0;
2595: rlast_time=rcurr_time;
2596: /* (void) gettimeofday(&curr_time,&tzp); */
2597: rcurr_time = time(NULL);
2598: curr_time = *localtime(&rcurr_time);
2599: /* 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); */
2600: /* 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); */
2601: printf("\nPowell iter=%d -2*LL=%.12f gain=%.3lg %ld sec. %ld sec.",*iter,*fret,fp-*fret, rcurr_time-rlast_time, rcurr_time-rstart_time);fflush(stdout);
2602: fprintf(ficlog,"\nPowell iter=%d -2*LL=%.12f gain=%.3lg %ld sec. %ld sec.",*iter,*fret,fp-*fret,rcurr_time-rlast_time, rcurr_time-rstart_time); fflush(ficlog);
2603: /* fprintf(ficrespow,"%d %.12f %ld",*iter,*fret,curr_time.tm_sec-start_time.tm_sec); */
2604: fp=(*fret); /* From former iteration or initial value */
2605: for (i=1;i<=n;i++) {
2606: fprintf(ficrespow," %.12lf", p[i]);
2607: }
2608: fprintf(ficrespow,"\n");fflush(ficrespow);
2609: printf("\n#model= 1 + age ");
2610: fprintf(ficlog,"\n#model= 1 + age ");
2611: if(nagesqr==1){
2612: printf(" + age*age ");
2613: fprintf(ficlog," + age*age ");
2614: }
2615: for(j=1;j <=ncovmodel-2;j++){
2616: if(Typevar[j]==0) {
2617: printf(" + V%d ",Tvar[j]);
2618: fprintf(ficlog," + V%d ",Tvar[j]);
2619: }else if(Typevar[j]==1) {
2620: printf(" + V%d*age ",Tvar[j]);
2621: fprintf(ficlog," + V%d*age ",Tvar[j]);
2622: }else if(Typevar[j]==2) {
2623: printf(" + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
2624: fprintf(ficlog," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
2625: }
2626: }
2627: printf("\n");
2628: /* printf("12 47.0114589 0.0154322 33.2424412 0.3279905 2.3731903 */
2629: /* 13 -21.5392400 0.1118147 1.2680506 1.2973408 -1.0663662 */
2630: fprintf(ficlog,"\n");
2631: for(i=1,jk=1; i <=nlstate; i++){
2632: for(k=1; k <=(nlstate+ndeath); k++){
2633: if (k != i) {
2634: printf("%d%d ",i,k);
2635: fprintf(ficlog,"%d%d ",i,k);
2636: for(j=1; j <=ncovmodel; j++){
2637: printf("%12.7f ",p[jk]);
2638: fprintf(ficlog,"%12.7f ",p[jk]);
2639: jk++;
2640: }
2641: printf("\n");
2642: fprintf(ficlog,"\n");
2643: }
2644: }
2645: }
2646: if(*iter <=3 && *iter >1){
2647: tml = *localtime(&rcurr_time);
2648: strcpy(strcurr,asctime(&tml));
2649: rforecast_time=rcurr_time;
2650: itmp = strlen(strcurr);
2651: if(strcurr[itmp-1]=='\n') /* Windows outputs with a new line */
2652: strcurr[itmp-1]='\0';
2653: printf("\nConsidering the time needed for the last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
2654: fprintf(ficlog,"\nConsidering the time needed for this last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
2655: for(niterf=10;niterf<=30;niterf+=10){
2656: rforecast_time=rcurr_time+(niterf-*iter)*(rcurr_time-rlast_time);
2657: forecast_time = *localtime(&rforecast_time);
2658: strcpy(strfor,asctime(&forecast_time));
2659: itmp = strlen(strfor);
2660: if(strfor[itmp-1]=='\n')
2661: strfor[itmp-1]='\0';
2662: printf(" - if your program needs %d iterations to converge, convergence will be \n reached in %s i.e.\n on %s (current time is %s);\n",niterf, asc_diff_time(rforecast_time-rcurr_time,tmpout),strfor,strcurr);
2663: fprintf(ficlog," - if your program needs %d iterations to converge, convergence will be \n reached in %s i.e.\n on %s (current time is %s);\n",niterf, asc_diff_time(rforecast_time-rcurr_time,tmpout),strfor,strcurr);
2664: }
2665: }
2666: for (i=1;i<=n;i++) { /* For each direction i */
2667: for (j=1;j<=n;j++) xit[j]=xi[j][i]; /* Directions stored from previous iteration with previous scales */
2668: fptt=(*fret);
2669: #ifdef DEBUG
2670: printf("fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
2671: fprintf(ficlog, "fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
2672: #endif
2673: printf("%d",i);fflush(stdout); /* print direction (parameter) i */
2674: fprintf(ficlog,"%d",i);fflush(ficlog);
2675: #ifdef LINMINORIGINAL
2676: linmin(p,xit,n,fret,func); /* Point p[n]. xit[n] has been loaded for direction i as input.*/
2677: #else
2678: linmin(p,xit,n,fret,func,&flat); /* Point p[n]. xit[n] has been loaded for direction i as input.*/
2679: flatdir[i]=flat; /* Function is vanishing in that direction i */
2680: #endif
2681: /* Outputs are fret(new point p) p is updated and xit rescaled */
2682: if (fabs(fptt-(*fret)) > del) { /* We are keeping the max gain on each of the n directions */
2683: /* because that direction will be replaced unless the gain del is small */
2684: /* in comparison with the 'probable' gain, mu^2, with the last average direction. */
2685: /* Unless the n directions are conjugate some gain in the determinant may be obtained */
2686: /* with the new direction. */
2687: del=fabs(fptt-(*fret));
2688: ibig=i;
2689: }
2690: #ifdef DEBUG
2691: printf("%d %.12e",i,(*fret));
2692: fprintf(ficlog,"%d %.12e",i,(*fret));
2693: for (j=1;j<=n;j++) {
2694: xits[j]=FMAX(fabs(p[j]-pt[j]),1.e-5);
2695: printf(" x(%d)=%.12e",j,xit[j]);
2696: fprintf(ficlog," x(%d)=%.12e",j,xit[j]);
2697: }
2698: for(j=1;j<=n;j++) {
2699: printf(" p(%d)=%.12e",j,p[j]);
2700: fprintf(ficlog," p(%d)=%.12e",j,p[j]);
2701: }
2702: printf("\n");
2703: fprintf(ficlog,"\n");
2704: #endif
2705: } /* end loop on each direction i */
2706: /* Convergence test will use last linmin estimation (fret) and compare former iteration (fp) */
2707: /* But p and xit have been updated at the end of linmin, *fret corresponds to new p, xit */
2708: /* New value of last point Pn is not computed, P(n-1) */
2709: for(j=1;j<=n;j++) {
2710: if(flatdir[j] >0){
2711: printf(" p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
2712: fprintf(ficlog," p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
2713: }
2714: /* printf("\n"); */
2715: /* fprintf(ficlog,"\n"); */
2716: }
2717: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret))) { /\* Did we reach enough precision? *\/ */
2718: if (2.0*fabs(fp-(*fret)) <= ftol) { /* Did we reach enough precision? */
2719: /* We could compare with a chi^2. chisquare(0.95,ddl=1)=3.84 */
2720: /* By adding age*age in a model, the new -2LL should be lower and the difference follows a */
2721: /* a chisquare statistics with 1 degree. To be significant at the 95% level, it should have */
2722: /* decreased of more than 3.84 */
2723: /* By adding age*age and V1*age the gain (-2LL) should be more than 5.99 (ddl=2) */
2724: /* By using V1+V2+V3, the gain should be 7.82, compared with basic 1+age. */
2725: /* By adding 10 parameters more the gain should be 18.31 */
2726:
2727: /* Starting the program with initial values given by a former maximization will simply change */
2728: /* the scales of the directions and the directions, because the are reset to canonical directions */
2729: /* Thus the first calls to linmin will give new points and better maximizations until fp-(*fret) is */
2730: /* under the tolerance value. If the tolerance is very small 1.e-9, it could last long. */
2731: #ifdef DEBUG
2732: int k[2],l;
2733: k[0]=1;
2734: k[1]=-1;
2735: printf("Max: %.12e",(*func)(p));
2736: fprintf(ficlog,"Max: %.12e",(*func)(p));
2737: for (j=1;j<=n;j++) {
2738: printf(" %.12e",p[j]);
2739: fprintf(ficlog," %.12e",p[j]);
2740: }
2741: printf("\n");
2742: fprintf(ficlog,"\n");
2743: for(l=0;l<=1;l++) {
2744: for (j=1;j<=n;j++) {
2745: ptt[j]=p[j]+(p[j]-pt[j])*k[l];
2746: printf("l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
2747: fprintf(ficlog,"l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
2748: }
2749: printf("func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
2750: fprintf(ficlog,"func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
2751: }
2752: #endif
2753:
2754: free_vector(xit,1,n);
2755: free_vector(xits,1,n);
2756: free_vector(ptt,1,n);
2757: free_vector(pt,1,n);
2758: return;
2759: } /* enough precision */
2760: if (*iter == ITMAX*n) nrerror("powell exceeding maximum iterations.");
2761: for (j=1;j<=n;j++) { /* Computes the extrapolated point P_0 + 2 (P_n-P_0) */
2762: ptt[j]=2.0*p[j]-pt[j];
2763: xit[j]=p[j]-pt[j];
2764: pt[j]=p[j];
2765: }
2766: fptt=(*func)(ptt); /* f_3 */
2767: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */
2768: if (*iter <=4) {
2769: #else
2770: #endif
2771: #ifdef POWELLNOF3INFF1TEST /* skips test F3 <F1 */
2772: #else
2773: if (fptt < fp) { /* If extrapolated point is better, decide if we keep that new direction or not */
2774: #endif
2775: /* (x1 f1=fp), (x2 f2=*fret), (x3 f3=fptt), (xm fm) */
2776: /* From x1 (P0) distance of x2 is at h and x3 is 2h */
2777: /* Let f"(x2) be the 2nd derivative equal everywhere. */
2778: /* Then the parabolic through (x1,f1), (x2,f2) and (x3,f3) */
2779: /* will reach at f3 = fm + h^2/2 f"m ; f" = (f1 -2f2 +f3 ) / h**2 */
2780: /* Conditional for using this new direction is that mu^2 = (f1-2f2+f3)^2 /2 < del or directest <0 */
2781: /* also lamda^2=(f1-f2)^2/mu² is a parasite solution of powell */
2782: /* For powell, inclusion of this average direction is only if t(del)<0 or del inbetween mu^2 and lambda^2 */
2783: /* t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)-del*SQR(fp-fptt); */
2784: /* Even if f3 <f1, directest can be negative and t >0 */
2785: /* mu² and del² are equal when f3=f1 */
2786: /* f3 < f1 : mu² < del <= lambda^2 both test are equivalent */
2787: /* f3 < f1 : mu² < lambda^2 < del then directtest is negative and powell t is positive */
2788: /* f3 > f1 : lambda² < mu^2 < del then t is negative and directest >0 */
2789: /* f3 > f1 : lambda² < del < mu^2 then t is positive and directest >0 */
2790: #ifdef NRCORIGINAL
2791: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)- del*SQR(fp-fptt); /* Original Numerical Recipes in C*/
2792: #else
2793: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del); /* Intel compiler doesn't work on one line; bug reported */
2794: t= t- del*SQR(fp-fptt);
2795: #endif
2796: directest = fp-2.0*(*fret)+fptt - 2.0 * del; /* If delta was big enough we change it for a new direction */
2797: #ifdef DEBUG
2798: printf("t1= %.12lf, t2= %.12lf, t=%.12lf directest=%.12lf\n", 2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del),del*SQR(fp-fptt),t,directest);
2799: fprintf(ficlog,"t1= %.12lf, t2= %.12lf, t=%.12lf directest=%.12lf\n", 2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del),del*SQR(fp-fptt),t,directest);
2800: printf("t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
2801: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
2802: fprintf(ficlog,"t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
2803: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
2804: printf("tt= %.12lf, t=%.12lf\n",2.0*(fp-2.0*(*fret)+fptt)*(fp-(*fret)-del)*(fp-(*fret)-del)-del*(fp-fptt)*(fp-fptt),t);
2805: fprintf(ficlog, "tt= %.12lf, t=%.12lf\n",2.0*(fp-2.0*(*fret)+fptt)*(fp-(*fret)-del)*(fp-(*fret)-del)-del*(fp-fptt)*(fp-fptt),t);
2806: #endif
2807: #ifdef POWELLORIGINAL
2808: if (t < 0.0) { /* Then we use it for new direction */
2809: #else
2810: if (directest*t < 0.0) { /* Contradiction between both tests */
2811: 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);
2812: printf("f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt);
2813: 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);
2814: fprintf(ficlog,"f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt);
2815: }
2816: if (directest < 0.0) { /* Then we use it for new direction */
2817: #endif
2818: #ifdef DEBUGLINMIN
2819: printf("Before linmin in direction P%d-P0\n",n);
2820: for (j=1;j<=n;j++) {
2821: printf(" Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2822: fprintf(ficlog," Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2823: if(j % ncovmodel == 0){
2824: printf("\n");
2825: fprintf(ficlog,"\n");
2826: }
2827: }
2828: #endif
2829: #ifdef LINMINORIGINAL
2830: linmin(p,xit,n,fret,func); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
2831: #else
2832: linmin(p,xit,n,fret,func,&flat); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
2833: flatdir[i]=flat; /* Function is vanishing in that direction i */
2834: #endif
2835:
2836: #ifdef DEBUGLINMIN
2837: for (j=1;j<=n;j++) {
2838: printf("After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2839: fprintf(ficlog,"After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2840: if(j % ncovmodel == 0){
2841: printf("\n");
2842: fprintf(ficlog,"\n");
2843: }
2844: }
2845: #endif
2846: for (j=1;j<=n;j++) {
2847: xi[j][ibig]=xi[j][n]; /* Replace direction with biggest decrease by last direction n */
2848: xi[j][n]=xit[j]; /* and this nth direction by the by the average p_0 p_n */
2849: }
2850: #ifdef LINMINORIGINAL
2851: #else
2852: for (j=1, flatd=0;j<=n;j++) {
2853: if(flatdir[j]>0)
2854: flatd++;
2855: }
2856: if(flatd >0){
2857: printf("%d flat directions: ",flatd);
2858: fprintf(ficlog,"%d flat directions :",flatd);
2859: for (j=1;j<=n;j++) {
2860: if(flatdir[j]>0){
2861: printf("%d ",j);
2862: fprintf(ficlog,"%d ",j);
2863: }
2864: }
2865: printf("\n");
2866: fprintf(ficlog,"\n");
2867: #ifdef FLATSUP
2868: free_vector(xit,1,n);
2869: free_vector(xits,1,n);
2870: free_vector(ptt,1,n);
2871: free_vector(pt,1,n);
2872: return;
2873: #endif
2874: }
2875: #endif
2876: printf("Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
2877: fprintf(ficlog,"Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
2878:
2879: #ifdef DEBUG
2880: printf("Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
2881: fprintf(ficlog,"Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
2882: for(j=1;j<=n;j++){
2883: printf(" %lf",xit[j]);
2884: fprintf(ficlog," %lf",xit[j]);
2885: }
2886: printf("\n");
2887: fprintf(ficlog,"\n");
2888: #endif
2889: } /* end of t or directest negative */
2890: #ifdef POWELLNOF3INFF1TEST
2891: #else
2892: } /* end if (fptt < fp) */
2893: #endif
2894: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */
2895: } /*NODIRECTIONCHANGEDUNTILNITER No change in drections until some iterations are done */
2896: #else
2897: #endif
2898: } /* loop iteration */
2899: }
2900:
2901: /**** Prevalence limit (stable or period prevalence) ****************/
2902:
2903: double **prevalim(double **prlim, int nlstate, double x[], double age, double **oldm, double **savm, double ftolpl, int *ncvyear, int ij, int nres)
2904: {
2905: /**< Computes the prevalence limit in each live state at age x and for covariate combination ij . Nicely done
2906: * (and selected quantitative values in nres)
2907: * by left multiplying the unit
2908: * matrix by transitions matrix until convergence is reached with precision ftolpl
2909: * Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I
2910: * Wx is row vector: population in state 1, population in state 2, population dead
2911: * or prevalence in state 1, prevalence in state 2, 0
2912: * newm is the matrix after multiplications, its rows are identical at a factor.
2913: * Inputs are the parameter, age, a tolerance for the prevalence limit ftolpl.
2914: * Output is prlim.
2915: * Initial matrix pimij
2916: */
2917: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
2918: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
2919: /* 0, 0 , 1} */
2920: /*
2921: * and after some iteration: */
2922: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
2923: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
2924: /* 0, 0 , 1} */
2925: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
2926: /* {0.51571254859325999, 0.4842874514067399, */
2927: /* 0.51326036147820708, 0.48673963852179264} */
2928: /* If we start from prlim again, prlim tends to a constant matrix */
2929:
2930: int i, ii,j,k, k1;
2931: double *min, *max, *meandiff, maxmax,sumnew=0.;
2932: /* double **matprod2(); */ /* test */
2933: double **out, cov[NCOVMAX+1], **pmij(); /* **pmmij is a global variable feeded with oldms etc */
2934: double **newm;
2935: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
2936: int ncvloop=0;
2937: int first=0;
2938:
2939: min=vector(1,nlstate);
2940: max=vector(1,nlstate);
2941: meandiff=vector(1,nlstate);
2942:
2943: /* Starting with matrix unity */
2944: for (ii=1;ii<=nlstate+ndeath;ii++)
2945: for (j=1;j<=nlstate+ndeath;j++){
2946: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
2947: }
2948:
2949: cov[1]=1.;
2950:
2951: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
2952: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
2953: for(agefin=age-stepm/YEARM; agefin>=age-delaymax; agefin=agefin-stepm/YEARM){
2954: ncvloop++;
2955: newm=savm;
2956: /* Covariates have to be included here again */
2957: cov[2]=agefin;
2958: if(nagesqr==1){
2959: cov[3]= agefin*agefin;
2960: }
2961: /* Model(2) V1 + V2 + V3 + V8 + V7*V8 + V5*V6 + V8*age + V3*age + age*age */
2962: /* total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age */
2963: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
2964: if(Typevar[k1]==1){ /* A product with age */
2965: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
2966: }else{
2967: cov[2+nagesqr+k1]=precov[nres][k1];
2968: }
2969: }/* End of loop on model equation */
2970:
2971: /* Start of old code (replaced by a loop on position in the model equation */
2972: /* for (k=1; k<=nsd;k++) { /\* For single dummy covariates only of the model *\/ */
2973: /* /\* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates *\/ */
2974: /* /\* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,TvarsD[k])]; *\/ */
2975: /* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,TnsdVar[TvarsD[k]])]; */
2976: /* /\* model = 1 +age + V1*V3 + age*V1 + V2 + V1 + age*V2 + V3 + V3*age + V1*V2 */
2977: /* * k 1 2 3 4 5 6 7 8 */
2978: /* *cov[] 1 2 3 4 5 6 7 8 9 10 */
2979: /* *TypeVar[k] 2 1 0 0 1 0 1 2 */
2980: /* *Dummy[k] 0 2 0 0 2 0 2 0 */
2981: /* *Tvar[k] 4 1 2 1 2 3 3 5 */
2982: /* *nsd=3 (1) (2) (3) */
2983: /* *TvarsD[nsd] [1]=2 1 3 */
2984: /* *TnsdVar [2]=2 [1]=1 [3]=3 */
2985: /* *TvarsDind[nsd](=k) [1]=3 [2]=4 [3]=6 */
2986: /* *Tage[] [1]=1 [2]=2 [3]=3 */
2987: /* *Tvard[] [1][1]=1 [2][1]=1 */
2988: /* * [1][2]=3 [2][2]=2 */
2989: /* *Tprod[](=k) [1]=1 [2]=8 */
2990: /* *TvarsDp(=Tvar) [1]=1 [2]=2 [3]=3 [4]=5 */
2991: /* *TvarD (=k) [1]=1 [2]=3 [3]=4 [3]=6 [4]=6 */
2992: /* *TvarsDpType */
2993: /* *si model= 1 + age + V3 + V2*age + V2 + V3*age */
2994: /* * nsd=1 (1) (2) */
2995: /* *TvarsD[nsd] 3 2 */
2996: /* *TnsdVar (3)=1 (2)=2 */
2997: /* *TvarsDind[nsd](=k) [1]=1 [2]=3 */
2998: /* *Tage[] [1]=2 [2]= 3 */
2999: /* *\/ */
3000: /* /\* cov[++k1]=nbcode[TvarsD[k]][codtabm(ij,k)]; *\/ */
3001: /* /\* 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)); *\/ */
3002: /* } */
3003: /* for (k=1; k<=nsq;k++) { /\* For single quantitative varying covariates only of the model *\/ */
3004: /* /\* Here comes the value of quantitative after renumbering k with single quantitative covariates *\/ */
3005: /* /\* Tqresult[nres][result_position]= value of the variable at the result_position in the nres resultline *\/ */
3006: /* /\* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; *\/ */
3007: /* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][resultmodel[nres][k1]] */
3008: /* /\* cov[++k1]=Tqresult[nres][k]; *\/ */
3009: /* /\* 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]); *\/ */
3010: /* } */
3011: /* for (k=1; k<=cptcovage;k++){ /\* For product with age *\/ */
3012: /* if(Dummy[Tage[k]]==2){ /\* dummy with age *\/ */
3013: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
3014: /* /\* cov[++k1]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; *\/ */
3015: /* } else if(Dummy[Tage[k]]==3){ /\* quantitative with age *\/ */
3016: /* cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; */
3017: /* /\* cov[++k1]=Tqresult[nres][k]; *\/ */
3018: /* } */
3019: /* /\* 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]); *\/ */
3020: /* } */
3021: /* for (k=1; k<=cptcovprod;k++){ /\* For product without age *\/ */
3022: /* /\* 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]); *\/ */
3023: /* if(Dummy[Tvard[k][1]]==0){ */
3024: /* if(Dummy[Tvard[k][2]]==0){ */
3025: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
3026: /* /\* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
3027: /* }else{ */
3028: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * Tqresult[nres][k]; */
3029: /* /\* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * Tqresult[nres][k]; *\/ */
3030: /* } */
3031: /* }else{ */
3032: /* if(Dummy[Tvard[k][2]]==0){ */
3033: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])] * Tqinvresult[nres][Tvard[k][1]]; */
3034: /* /\* cov[++k1]=nbcode[Tvard[k][2]][codtabm(ij,k)] * Tqinvresult[nres][Tvard[k][1]]; *\/ */
3035: /* }else{ */
3036: /* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; */
3037: /* /\* cov[++k1]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; *\/ */
3038: /* } */
3039: /* } */
3040: /* } /\* End product without age *\/ */
3041: /* ENd of old code */
3042: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
3043: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
3044: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
3045: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
3046: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
3047: /* age and covariate values of ij are in 'cov' */
3048: out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /* Bug Valgrind */
3049:
3050: savm=oldm;
3051: oldm=newm;
3052:
3053: for(j=1; j<=nlstate; j++){
3054: max[j]=0.;
3055: min[j]=1.;
3056: }
3057: for(i=1;i<=nlstate;i++){
3058: sumnew=0;
3059: for(k=1; k<=ndeath; k++) sumnew+=newm[i][nlstate+k];
3060: for(j=1; j<=nlstate; j++){
3061: prlim[i][j]= newm[i][j]/(1-sumnew);
3062: max[j]=FMAX(max[j],prlim[i][j]);
3063: min[j]=FMIN(min[j],prlim[i][j]);
3064: }
3065: }
3066:
3067: maxmax=0.;
3068: for(j=1; j<=nlstate; j++){
3069: meandiff[j]=(max[j]-min[j])/(max[j]+min[j])*2.; /* mean difference for each column */
3070: maxmax=FMAX(maxmax,meandiff[j]);
3071: /* 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); */
3072: } /* j loop */
3073: *ncvyear= (int)age- (int)agefin;
3074: /* printf("maxmax=%lf maxmin=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, maxmin, ncvloop, (int)age, (int)agefin, *ncvyear); */
3075: if(maxmax < ftolpl){
3076: /* printf("maxmax=%lf ncvloop=%ld, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
3077: free_vector(min,1,nlstate);
3078: free_vector(max,1,nlstate);
3079: free_vector(meandiff,1,nlstate);
3080: return prlim;
3081: }
3082: } /* agefin loop */
3083: /* After some age loop it doesn't converge */
3084: if(!first){
3085: first=1;
3086: 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);
3087: 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);
3088: }else if (first >=1 && first <10){
3089: 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);
3090: first++;
3091: }else if (first ==10){
3092: 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);
3093: 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");
3094: fprintf(ficlog,"Warning: the stable prevalence no convergence; too many cases, giving up noticing, even in log file\n");
3095: first++;
3096: }
3097:
3098: /* 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); */
3099: free_vector(min,1,nlstate);
3100: free_vector(max,1,nlstate);
3101: free_vector(meandiff,1,nlstate);
3102:
3103: return prlim; /* should not reach here */
3104: }
3105:
3106:
3107: /**** Back Prevalence limit (stable or period prevalence) ****************/
3108:
3109: /* 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) */
3110: /* 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) */
3111: double **bprevalim(double **bprlim, double ***prevacurrent, int nlstate, double x[], double age, double ftolpl, int *ncvyear, int ij, int nres)
3112: {
3113: /* Computes the prevalence limit in each live state at age x and for covariate combination ij (<=2**cptcoveff) by left multiplying the unit
3114: matrix by transitions matrix until convergence is reached with precision ftolpl */
3115: /* Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I */
3116: /* Wx is row vector: population in state 1, population in state 2, population dead */
3117: /* or prevalence in state 1, prevalence in state 2, 0 */
3118: /* newm is the matrix after multiplications, its rows are identical at a factor */
3119: /* Initial matrix pimij */
3120: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
3121: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
3122: /* 0, 0 , 1} */
3123: /*
3124: * and after some iteration: */
3125: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
3126: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
3127: /* 0, 0 , 1} */
3128: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
3129: /* {0.51571254859325999, 0.4842874514067399, */
3130: /* 0.51326036147820708, 0.48673963852179264} */
3131: /* If we start from prlim again, prlim tends to a constant matrix */
3132:
3133: int i, ii,j,k, k1;
3134: int first=0;
3135: double *min, *max, *meandiff, maxmax,sumnew=0.;
3136: /* double **matprod2(); */ /* test */
3137: double **out, cov[NCOVMAX+1], **bmij();
3138: double **newm;
3139: double **dnewm, **doldm, **dsavm; /* for use */
3140: double **oldm, **savm; /* for use */
3141:
3142: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
3143: int ncvloop=0;
3144:
3145: min=vector(1,nlstate);
3146: max=vector(1,nlstate);
3147: meandiff=vector(1,nlstate);
3148:
3149: dnewm=ddnewms; doldm=ddoldms; dsavm=ddsavms;
3150: oldm=oldms; savm=savms;
3151:
3152: /* Starting with matrix unity */
3153: for (ii=1;ii<=nlstate+ndeath;ii++)
3154: for (j=1;j<=nlstate+ndeath;j++){
3155: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3156: }
3157:
3158: cov[1]=1.;
3159:
3160: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
3161: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
3162: /* for(agefin=age+stepm/YEARM; agefin<=age+delaymax; agefin=agefin+stepm/YEARM){ /\* A changer en age *\/ */
3163: /* for(agefin=age; agefin<AGESUP; agefin=agefin+stepm/YEARM){ /\* A changer en age *\/ */
3164: for(agefin=age; agefin<FMIN(AGESUP,age+delaymax); agefin=agefin+stepm/YEARM){ /* A changer en age */
3165: ncvloop++;
3166: newm=savm; /* oldm should be kept from previous iteration or unity at start */
3167: /* newm points to the allocated table savm passed by the function it can be written, savm could be reallocated */
3168: /* Covariates have to be included here again */
3169: cov[2]=agefin;
3170: if(nagesqr==1){
3171: cov[3]= agefin*agefin;;
3172: }
3173: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
3174: if(Typevar[k1]==1){ /* A product with age */
3175: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
3176: }else{
3177: cov[2+nagesqr+k1]=precov[nres][k1];
3178: }
3179: }/* End of loop on model equation */
3180:
3181: /* Old code */
3182:
3183: /* for (k=1; k<=nsd;k++) { /\* For single dummy covariates only *\/ */
3184: /* /\* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates *\/ */
3185: /* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,TvarsD[k])]; */
3186: /* /\* 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)); *\/ */
3187: /* } */
3188: /* /\* for (k=1; k<=cptcovn;k++) { *\/ */
3189: /* /\* /\\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; *\\/ *\/ */
3190: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; *\/ */
3191: /* /\* /\\* 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])]); *\\/ *\/ */
3192: /* /\* } *\/ */
3193: /* for (k=1; k<=nsq;k++) { /\* For single varying covariates only *\/ */
3194: /* /\* Here comes the value of quantitative after renumbering k with single quantitative covariates *\/ */
3195: /* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; */
3196: /* /\* 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]); *\/ */
3197: /* } */
3198: /* /\* for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,k)]*cov[2]; *\/ */
3199: /* /\* for (k=1; k<=cptcovprod;k++) /\\* Useless *\\/ *\/ */
3200: /* /\* /\\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; *\\/ *\/ */
3201: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
3202: /* for (k=1; k<=cptcovage;k++){ /\* For product with age *\/ */
3203: /* /\* if(Dummy[Tvar[Tage[k]]]== 2){ /\\* dummy with age *\\/ ERROR ???*\/ */
3204: /* if(Dummy[Tage[k]]== 2){ /\* dummy with age *\/ */
3205: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
3206: /* } else if(Dummy[Tage[k]]== 3){ /\* quantitative with age *\/ */
3207: /* cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; */
3208: /* } */
3209: /* /\* 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]); *\/ */
3210: /* } */
3211: /* for (k=1; k<=cptcovprod;k++){ /\* For product without age *\/ */
3212: /* /\* 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]); *\/ */
3213: /* if(Dummy[Tvard[k][1]]==0){ */
3214: /* if(Dummy[Tvard[k][2]]==0){ */
3215: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
3216: /* }else{ */
3217: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * Tqresult[nres][k]; */
3218: /* } */
3219: /* }else{ */
3220: /* if(Dummy[Tvard[k][2]]==0){ */
3221: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])] * Tqinvresult[nres][Tvard[k][1]]; */
3222: /* }else{ */
3223: /* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; */
3224: /* } */
3225: /* } */
3226: /* } */
3227:
3228: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
3229: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
3230: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
3231: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
3232: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
3233: /* ij should be linked to the correct index of cov */
3234: /* age and covariate values ij are in 'cov', but we need to pass
3235: * ij for the observed prevalence at age and status and covariate
3236: * number: prevacurrent[(int)agefin][ii][ij]
3237: */
3238: /* 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 *\/ */
3239: /* 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 *\/ */
3240: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij)); /* Bug Valgrind */
3241: /* if((int)age == 86 || (int)age == 87){ */
3242: /* printf(" Backward prevalim age=%d agefin=%d \n", (int) age, (int) agefin); */
3243: /* for(i=1; i<=nlstate+ndeath; i++) { */
3244: /* printf("%d newm= ",i); */
3245: /* for(j=1;j<=nlstate+ndeath;j++) { */
3246: /* printf("%f ",newm[i][j]); */
3247: /* } */
3248: /* printf("oldm * "); */
3249: /* for(j=1;j<=nlstate+ndeath;j++) { */
3250: /* printf("%f ",oldm[i][j]); */
3251: /* } */
3252: /* printf(" bmmij "); */
3253: /* for(j=1;j<=nlstate+ndeath;j++) { */
3254: /* printf("%f ",pmmij[i][j]); */
3255: /* } */
3256: /* printf("\n"); */
3257: /* } */
3258: /* } */
3259: savm=oldm;
3260: oldm=newm;
3261:
3262: for(j=1; j<=nlstate; j++){
3263: max[j]=0.;
3264: min[j]=1.;
3265: }
3266: for(j=1; j<=nlstate; j++){
3267: for(i=1;i<=nlstate;i++){
3268: /* bprlim[i][j]= newm[i][j]/(1-sumnew); */
3269: bprlim[i][j]= newm[i][j];
3270: max[i]=FMAX(max[i],bprlim[i][j]); /* Max in line */
3271: min[i]=FMIN(min[i],bprlim[i][j]);
3272: }
3273: }
3274:
3275: maxmax=0.;
3276: for(i=1; i<=nlstate; i++){
3277: meandiff[i]=(max[i]-min[i])/(max[i]+min[i])*2.; /* mean difference for each column, could be nan! */
3278: maxmax=FMAX(maxmax,meandiff[i]);
3279: /* 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); */
3280: } /* i loop */
3281: *ncvyear= -( (int)age- (int)agefin);
3282: /* printf("Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
3283: if(maxmax < ftolpl){
3284: /* printf("OK Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
3285: free_vector(min,1,nlstate);
3286: free_vector(max,1,nlstate);
3287: free_vector(meandiff,1,nlstate);
3288: return bprlim;
3289: }
3290: } /* agefin loop */
3291: /* After some age loop it doesn't converge */
3292: if(!first){
3293: first=1;
3294: 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\
3295: 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);
3296: }
3297: 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\
3298: 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);
3299: /* 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); */
3300: free_vector(min,1,nlstate);
3301: free_vector(max,1,nlstate);
3302: free_vector(meandiff,1,nlstate);
3303:
3304: return bprlim; /* should not reach here */
3305: }
3306:
3307: /*************** transition probabilities ***************/
3308:
3309: double **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
3310: {
3311: /* According to parameters values stored in x and the covariate's values stored in cov,
3312: computes the probability to be observed in state j (after stepm years) being in state i by appying the
3313: model to the ncovmodel covariates (including constant and age).
3314: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
3315: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
3316: ncth covariate in the global vector x is given by the formula:
3317: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
3318: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
3319: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
3320: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
3321: Outputs ps[i][j] or probability to be observed in j being in i according to
3322: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
3323: Sum on j ps[i][j] should equal to 1.
3324: */
3325: double s1, lnpijopii;
3326: /*double t34;*/
3327: int i,j, nc, ii, jj;
3328:
3329: for(i=1; i<= nlstate; i++){
3330: for(j=1; j<i;j++){
3331: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
3332: /*lnpijopii += param[i][j][nc]*cov[nc];*/
3333: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
3334: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
3335: }
3336: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
3337: /* printf("Debug pmij() i=%d j=%d nc=%d s1=%.17f, lnpijopii=%.17f\n",i,j,nc, s1,lnpijopii); */
3338: }
3339: for(j=i+1; j<=nlstate+ndeath;j++){
3340: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
3341: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
3342: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
3343: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
3344: }
3345: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
3346: /* printf("Debug pmij() i=%d j=%d nc=%d s1=%.17f, lnpijopii=%.17f\n",i,j,nc, s1,lnpijopii); */
3347: }
3348: }
3349:
3350: for(i=1; i<= nlstate; i++){
3351: s1=0;
3352: for(j=1; j<i; j++){
3353: /* printf("debug1 %d %d ps=%lf exp(ps)=%lf \n",i,j,ps[i][j],exp(ps[i][j])); */
3354: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
3355: }
3356: for(j=i+1; j<=nlstate+ndeath; j++){
3357: /* printf("debug2 %d %d ps=%lf exp(ps)=%lf \n",i,j,ps[i][j],exp(ps[i][j])); */
3358: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
3359: }
3360: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
3361: ps[i][i]=1./(s1+1.);
3362: /* Computing other pijs */
3363: for(j=1; j<i; j++)
3364: ps[i][j]= exp(ps[i][j])*ps[i][i];/* Bug valgrind */
3365: for(j=i+1; j<=nlstate+ndeath; j++)
3366: ps[i][j]= exp(ps[i][j])*ps[i][i];
3367: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
3368: } /* end i */
3369:
3370: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
3371: for(jj=1; jj<= nlstate+ndeath; jj++){
3372: ps[ii][jj]=0;
3373: ps[ii][ii]=1;
3374: }
3375: }
3376:
3377:
3378: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
3379: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
3380: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
3381: /* } */
3382: /* printf("\n "); */
3383: /* } */
3384: /* printf("\n ");printf("%lf ",cov[2]);*/
3385: /*
3386: for(i=1; i<= npar; i++) printf("%f ",x[i]);
3387: goto end;*/
3388: return ps; /* Pointer is unchanged since its call */
3389: }
3390:
3391: /*************** backward transition probabilities ***************/
3392:
3393: /* 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 ) */
3394: /* double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, double ***dnewm, double **doldm, double **dsavm, int ij ) */
3395: double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, int ij )
3396: {
3397: /* Computes the backward probability at age agefin, cov[2], and covariate combination 'ij'. In fact cov is already filled and x too.
3398: * Call to pmij(cov and x), call to cross prevalence, sums and inverses, left multiply, and returns in **ps as well as **bmij.
3399: */
3400: int i, ii, j,k;
3401:
3402: double **out, **pmij();
3403: double sumnew=0.;
3404: double agefin;
3405: double k3=0.; /* constant of the w_x diagonal matrix (in order for B to sum to 1 even for death state) */
3406: double **dnewm, **dsavm, **doldm;
3407: double **bbmij;
3408:
3409: doldm=ddoldms; /* global pointers */
3410: dnewm=ddnewms;
3411: dsavm=ddsavms;
3412:
3413: /* Debug */
3414: /* printf("Bmij ij=%d, cov[2}=%f\n", ij, cov[2]); */
3415: agefin=cov[2];
3416: /* Bx = Diag(w_x) P_x Diag(Sum_i w^i_x p^ij_x */
3417: /* bmij *//* age is cov[2], ij is included in cov, but we need for
3418: the observed prevalence (with this covariate ij) at beginning of transition */
3419: /* dsavm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
3420:
3421: /* P_x */
3422: pmmij=pmij(pmmij,cov,ncovmodel,x,nlstate); /*This is forward probability from agefin to agefin + stepm *//* Bug valgrind */
3423: /* outputs pmmij which is a stochastic matrix in row */
3424:
3425: /* Diag(w_x) */
3426: /* Rescaling the cross-sectional prevalence: Problem with prevacurrent which can be zero */
3427: sumnew=0.;
3428: /*for (ii=1;ii<=nlstate+ndeath;ii++){*/
3429: for (ii=1;ii<=nlstate;ii++){ /* Only on live states */
3430: /* printf(" agefin=%d, ii=%d, ij=%d, prev=%f\n",(int)agefin,ii, ij, prevacurrent[(int)agefin][ii][ij]); */
3431: sumnew+=prevacurrent[(int)agefin][ii][ij];
3432: }
3433: if(sumnew >0.01){ /* At least some value in the prevalence */
3434: for (ii=1;ii<=nlstate+ndeath;ii++){
3435: for (j=1;j<=nlstate+ndeath;j++)
3436: doldm[ii][j]=(ii==j ? prevacurrent[(int)agefin][ii][ij]/sumnew : 0.0);
3437: }
3438: }else{
3439: for (ii=1;ii<=nlstate+ndeath;ii++){
3440: for (j=1;j<=nlstate+ndeath;j++)
3441: doldm[ii][j]=(ii==j ? 1./nlstate : 0.0);
3442: }
3443: /* if(sumnew <0.9){ */
3444: /* printf("Problem internal bmij B: sum on i wi <0.9: j=%d, sum_i wi=%lf,agefin=%d\n",j,sumnew, (int)agefin); */
3445: /* } */
3446: }
3447: k3=0.0; /* We put the last diagonal to 0 */
3448: for (ii=nlstate+1;ii<=nlstate+ndeath;ii++){
3449: doldm[ii][ii]= k3;
3450: }
3451: /* End doldm, At the end doldm is diag[(w_i)] */
3452:
3453: /* Left product of this diag matrix by pmmij=Px (dnewm=dsavm*doldm): diag[(w_i)*Px */
3454: bbmij=matprod2(dnewm, doldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, pmmij); /* was a Bug Valgrind */
3455:
3456: /* Diag(Sum_i w^i_x p^ij_x, should be the prevalence at age x+stepm */
3457: /* w1 p11 + w2 p21 only on live states N1./N..*N11/N1. + N2./N..*N21/N2.=(N11+N21)/N..=N.1/N.. */
3458: for (j=1;j<=nlstate+ndeath;j++){
3459: sumnew=0.;
3460: for (ii=1;ii<=nlstate;ii++){
3461: /* sumnew+=dsavm[ii][j]*prevacurrent[(int)agefin][ii][ij]; */
3462: sumnew+=pmmij[ii][j]*doldm[ii][ii]; /* Yes prevalence at beginning of transition */
3463: } /* sumnew is (N11+N21)/N..= N.1/N.. = sum on i of w_i pij */
3464: for (ii=1;ii<=nlstate+ndeath;ii++){
3465: /* if(agefin >= agemaxpar && agefin <= agemaxpar+stepm/YEARM){ */
3466: /* dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
3467: /* }else if(agefin >= agemaxpar+stepm/YEARM){ */
3468: /* dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
3469: /* }else */
3470: dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0);
3471: } /*End ii */
3472: } /* 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 */
3473:
3474: ps=matprod2(ps, dnewm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, dsavm); /* was a Bug Valgrind */
3475: /* ps is now diag[w_i] * Px * diag [1/(w_1p1i+w_2 p2i)] */
3476: /* end bmij */
3477: return ps; /*pointer is unchanged */
3478: }
3479: /*************** transition probabilities ***************/
3480:
3481: double **bpmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
3482: {
3483: /* According to parameters values stored in x and the covariate's values stored in cov,
3484: computes the probability to be observed in state j being in state i by appying the
3485: model to the ncovmodel covariates (including constant and age).
3486: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
3487: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
3488: ncth covariate in the global vector x is given by the formula:
3489: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
3490: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
3491: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
3492: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
3493: Outputs ps[i][j] the probability to be observed in j being in j according to
3494: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
3495: */
3496: double s1, lnpijopii;
3497: /*double t34;*/
3498: int i,j, nc, ii, jj;
3499:
3500: for(i=1; i<= nlstate; i++){
3501: for(j=1; j<i;j++){
3502: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
3503: /*lnpijopii += param[i][j][nc]*cov[nc];*/
3504: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
3505: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
3506: }
3507: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
3508: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
3509: }
3510: for(j=i+1; j<=nlstate+ndeath;j++){
3511: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
3512: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
3513: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
3514: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
3515: }
3516: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
3517: }
3518: }
3519:
3520: for(i=1; i<= nlstate; i++){
3521: s1=0;
3522: for(j=1; j<i; j++){
3523: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
3524: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
3525: }
3526: for(j=i+1; j<=nlstate+ndeath; j++){
3527: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
3528: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
3529: }
3530: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
3531: ps[i][i]=1./(s1+1.);
3532: /* Computing other pijs */
3533: for(j=1; j<i; j++)
3534: ps[i][j]= exp(ps[i][j])*ps[i][i];
3535: for(j=i+1; j<=nlstate+ndeath; j++)
3536: ps[i][j]= exp(ps[i][j])*ps[i][i];
3537: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
3538: } /* end i */
3539:
3540: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
3541: for(jj=1; jj<= nlstate+ndeath; jj++){
3542: ps[ii][jj]=0;
3543: ps[ii][ii]=1;
3544: }
3545: }
3546: /* Added for prevbcast */ /* Transposed matrix too */
3547: for(jj=1; jj<= nlstate+ndeath; jj++){
3548: s1=0.;
3549: for(ii=1; ii<= nlstate+ndeath; ii++){
3550: s1+=ps[ii][jj];
3551: }
3552: for(ii=1; ii<= nlstate; ii++){
3553: ps[ii][jj]=ps[ii][jj]/s1;
3554: }
3555: }
3556: /* Transposition */
3557: for(jj=1; jj<= nlstate+ndeath; jj++){
3558: for(ii=jj; ii<= nlstate+ndeath; ii++){
3559: s1=ps[ii][jj];
3560: ps[ii][jj]=ps[jj][ii];
3561: ps[jj][ii]=s1;
3562: }
3563: }
3564: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
3565: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
3566: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
3567: /* } */
3568: /* printf("\n "); */
3569: /* } */
3570: /* printf("\n ");printf("%lf ",cov[2]);*/
3571: /*
3572: for(i=1; i<= npar; i++) printf("%f ",x[i]);
3573: goto end;*/
3574: return ps;
3575: }
3576:
3577:
3578: /**************** Product of 2 matrices ******************/
3579:
3580: double **matprod2(double **out, double **in,int nrl, int nrh, int ncl, int nch, int ncolol, int ncoloh, double **b)
3581: {
3582: /* Computes the matrix product of in(1,nrh-nrl+1)(1,nch-ncl+1) times
3583: b(1,nch-ncl+1)(1,ncoloh-ncolol+1) into out(...) */
3584: /* in, b, out are matrice of pointers which should have been initialized
3585: before: only the contents of out is modified. The function returns
3586: a pointer to pointers identical to out */
3587: int i, j, k;
3588: for(i=nrl; i<= nrh; i++)
3589: for(k=ncolol; k<=ncoloh; k++){
3590: out[i][k]=0.;
3591: for(j=ncl; j<=nch; j++)
3592: out[i][k] +=in[i][j]*b[j][k];
3593: }
3594: return out;
3595: }
3596:
3597:
3598: /************* Higher Matrix Product ***************/
3599:
3600: double ***hpxij(double ***po, int nhstepm, double age, int hstepm, double *x, int nlstate, int stepm, double **oldm, double **savm, int ij, int nres )
3601: {
3602: /* Already optimized with precov.
3603: Computes the transition matrix starting at age 'age' and dummies values in each resultline (loop on ij to find the corresponding combination) to over
3604: 'nhstepm*hstepm*stepm' months (i.e. until
3605: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
3606: nhstepm*hstepm matrices.
3607: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
3608: (typically every 2 years instead of every month which is too big
3609: for the memory).
3610: Model is determined by parameters x and covariates have to be
3611: included manually here.
3612:
3613: */
3614:
3615: int i, j, d, h, k, k1;
3616: double **out, cov[NCOVMAX+1];
3617: double **newm;
3618: double agexact;
3619: double agebegin, ageend;
3620:
3621: /* Hstepm could be zero and should return the unit matrix */
3622: for (i=1;i<=nlstate+ndeath;i++)
3623: for (j=1;j<=nlstate+ndeath;j++){
3624: oldm[i][j]=(i==j ? 1.0 : 0.0);
3625: po[i][j][0]=(i==j ? 1.0 : 0.0);
3626: }
3627: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
3628: for(h=1; h <=nhstepm; h++){
3629: for(d=1; d <=hstepm; d++){
3630: newm=savm;
3631: /* Covariates have to be included here again */
3632: cov[1]=1.;
3633: agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /* age just before transition */
3634: cov[2]=agexact;
3635: if(nagesqr==1){
3636: cov[3]= agexact*agexact;
3637: }
3638: /* Model(2) V1 + V2 + V3 + V8 + V7*V8 + V5*V6 + V8*age + V3*age + age*age */
3639: /* total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age */
3640: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
3641: if(Typevar[k1]==1){ /* A product with age */
3642: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
3643: }else{
3644: cov[2+nagesqr+k1]=precov[nres][k1];
3645: }
3646: }/* End of loop on model equation */
3647: /* Old code */
3648: /* if( Dummy[k1]==0 && Typevar[k1]==0 ){ /\* Single dummy *\/ */
3649: /* /\* V(Tvarsel)=Tvalsel=Tresult[nres][pos](value); V(Tvresult[nres][pos] (variable): V(variable)=value) *\/ */
3650: /* /\* for (k=1; k<=nsd;k++) { /\\* For single dummy covariates only *\\/ *\/ */
3651: /* /\* /\\* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates *\\/ *\/ */
3652: /* /\* codtabm(ij,k) (1 & (ij-1) >> (k-1))+1 *\/ */
3653: /* /\* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 *\/ */
3654: /* /\* k 1 2 3 4 5 6 7 8 9 *\/ */
3655: /* /\*Tvar[k]= 5 4 3 6 5 2 7 1 1 *\/ */
3656: /* /\* nsd 1 2 3 *\/ /\* Counting single dummies covar fixed or tv *\/ */
3657: /* /\*TvarsD[nsd] 4 3 1 *\/ /\* ID of single dummy cova fixed or timevary*\/ */
3658: /* /\*TvarsDind[k] 2 3 9 *\/ /\* position K of single dummy cova *\/ */
3659: /* /\* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)];or [codtabm(ij,TnsdVar[TvarsD[k]] *\/ */
3660: /* cov[2+nagesqr+k1]=Tresult[nres][resultmodel[nres][k1]]; */
3661: /* /\* 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]])); *\/ */
3662: /* 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); */
3663: /* printf("hpxij new Dummy precov[nres=%d][k1=%d]=%.4f\n", nres, k1, precov[nres][k1]); */
3664: /* }else if( Dummy[k1]==1 && Typevar[k1]==0 ){ /\* Single quantitative variables *\/ */
3665: /* /\* resultmodel[nres][k1]=k3: k1th position in the model correspond to the k3 position in the resultline *\/ */
3666: /* cov[2+nagesqr+k1]=Tqresult[nres][resultmodel[nres][k1]]; */
3667: /* /\* for (k=1; k<=nsq;k++) { /\\* For single varying covariates only *\\/ *\/ */
3668: /* /\* /\\* Here comes the value of quantitative after renumbering k with single quantitative covariates *\\/ *\/ */
3669: /* /\* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; *\/ */
3670: /* 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]]); */
3671: /* printf("hpxij new Quanti precov[nres=%d][k1=%d]=%.4f\n", nres, k1, precov[nres][k1]); */
3672: /* }else if( Dummy[k1]==2 ){ /\* For dummy with age product *\/ */
3673: /* /\* Tvar[k1] Variable in the age product age*V1 is 1 *\/ */
3674: /* /\* [Tinvresult[nres][V1] is its value in the resultline nres *\/ */
3675: /* cov[2+nagesqr+k1]=TinvDoQresult[nres][Tvar[k1]]*cov[2]; */
3676: /* 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]); */
3677: /* printf("hpxij new Dummy with age product precov[nres=%d][k1=%d]=%.4f * age=%.2f\n", nres, k1, precov[nres][k1], cov[2]); */
3678:
3679: /* /\* cov[2+nagesqr+k1]=Tresult[nres][resultmodel[nres][k1]]; *\/ */
3680: /* /\* for (k=1; k<=cptcovage;k++){ /\\* For product with age V1+V1*age +V4 +age*V3 *\\/ *\/ */
3681: /* /\* 1+2 Tage[1]=2 TVar[2]=1 Dummy[2]=2, Tage[2]=4 TVar[4]=3 Dummy[4]=3 quant*\/ */
3682: /* /\* *\/ */
3683: /* /\* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 *\/ */
3684: /* /\* k 1 2 3 4 5 6 7 8 9 *\/ */
3685: /* /\*Tvar[k]= 5 4 3 6 5 2 7 1 1 *\/ */
3686: /* /\*cptcovage=2 1 2 *\/ */
3687: /* /\*Tage[k]= 5 8 *\/ */
3688: /* }else if( Dummy[k1]==3 ){ /\* For quant with age product *\/ */
3689: /* cov[2+nagesqr+k1]=Tresult[nres][resultmodel[nres][k1]]; */
3690: /* 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]]); */
3691: /* printf("hpxij new Quanti with age product precov[nres=%d][k1=%d] * age=%.2f\n", nres, k1, precov[nres][k1], cov[2]); */
3692: /* /\* if(Dummy[Tage[k]]== 2){ /\\* dummy with age *\\/ *\/ */
3693: /* /\* /\\* if(Dummy[Tvar[Tage[k]]]== 2){ /\\\* dummy with age *\\\/ *\\/ *\/ */
3694: /* /\* /\\* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; *\\/ *\/ */
3695: /* /\* /\\* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,TnsdVar[TvarsD[Tvar[Tage[k]]]])]*cov[2]; *\\/ *\/ */
3696: /* /\* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,TnsdVar[TvarsD[Tvar[Tage[k]]]])]*cov[2]; *\/ */
3697: /* /\* 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); *\/ */
3698: /* /\* } else if(Dummy[Tage[k]]== 3){ /\\* quantitative with age *\\/ *\/ */
3699: /* /\* cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; *\/ */
3700: /* /\* } *\/ */
3701: /* /\* 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]); *\/ */
3702: /* }else if(Typevar[k1]==2 ){ /\* For product (not with age) *\/ */
3703: /* /\* for (k=1; k<=cptcovprod;k++){ /\\* For product without age *\\/ *\/ */
3704: /* /\* /\\* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 *\\/ *\/ */
3705: /* /\* /\\* k 1 2 3 4 5 6 7 8 9 *\\/ *\/ */
3706: /* /\* /\\*Tvar[k]= 5 4 3 6 5 2 7 1 1 *\\/ *\/ */
3707: /* /\* /\\*cptcovprod=1 1 2 *\\/ *\/ */
3708: /* /\* /\\*Tprod[]= 4 7 *\\/ *\/ */
3709: /* /\* /\\*Tvard[][1] 4 1 *\\/ *\/ */
3710: /* /\* /\\*Tvard[][2] 3 2 *\\/ *\/ */
3711:
3712: /* /\* 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])]); *\/ */
3713: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
3714: /* cov[2+nagesqr+k1]=TinvDoQresult[nres][Tvardk[k1][1]] * TinvDoQresult[nres][Tvardk[k1][2]]; */
3715: /* 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]]); */
3716: /* printf("hpxij new Product no age product precov[nres=%d][k1=%d]=%.4f\n", nres, k1, precov[nres][k1]); */
3717:
3718: /* /\* if(Dummy[Tvardk[k1][1]]==0){ *\/ */
3719: /* /\* if(Dummy[Tvardk[k1][2]]==0){ /\\* Product of dummies *\\/ *\/ */
3720: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
3721: /* /\* cov[2+nagesqr+k1]=Tinvresult[nres][Tvardk[k1][1]] * Tinvresult[nres][Tvardk[k1][2]]; *\/ */
3722: /* /\* 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]])]; *\/ */
3723: /* /\* }else{ /\\* Product of dummy by quantitative *\\/ *\/ */
3724: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,TnsdVar[Tvard[k][1]])] * Tqresult[nres][k]; *\/ */
3725: /* /\* cov[2+nagesqr+k1]=Tresult[nres][Tinvresult[nres][Tvardk[k1][1]]] * Tqresult[nres][Tinvresult[nres][Tvardk[k1][2]]]; *\/ */
3726: /* /\* } *\/ */
3727: /* /\* }else{ /\\* Product of quantitative by...*\\/ *\/ */
3728: /* /\* if(Dummy[Tvard[k][2]]==0){ /\\* quant by dummy *\\/ *\/ */
3729: /* /\* /\\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,TnsdVar[Tvard[k][2]])] * Tqinvresult[nres][Tvard[k][1]]; *\\/ *\/ */
3730: /* /\* cov[2+nagesqr+k1]=Tqresult[nres][Tinvresult[nres][Tvardk[k1][1]]] * Tresult[nres][Tinvresult[nres][Tvardk[k1][2]]] ; *\/ */
3731: /* /\* }else{ /\\* Product of two quant *\\/ *\/ */
3732: /* /\* /\\* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; *\\/ *\/ */
3733: /* /\* cov[2+nagesqr+k1]=Tqresult[nres][Tinvresult[nres][Tvardk[k1][1]]] * Tqresult[nres][Tinvresult[nres][Tvardk[k1][2]]] ; *\/ */
3734: /* /\* } *\/ */
3735: /* /\* }/\\*end of products quantitative *\\/ *\/ */
3736: /* }/\*end of products *\/ */
3737: /* } /\* End of loop on model equation *\/ */
3738: /* for (k=1; k<=cptcovn;k++) */
3739: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; */
3740: /* for (k=1; k<=cptcovage;k++) /\* Should start at cptcovn+1 *\/ */
3741: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; */
3742: /* for (k=1; k<=cptcovprod;k++) /\* Useless because included in cptcovn *\/ */
3743: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)]; */
3744:
3745:
3746: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
3747: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
3748: /* right multiplication of oldm by the current matrix */
3749: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
3750: pmij(pmmij,cov,ncovmodel,x,nlstate));
3751: /* if((int)age == 70){ */
3752: /* printf(" Forward hpxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
3753: /* for(i=1; i<=nlstate+ndeath; i++) { */
3754: /* printf("%d pmmij ",i); */
3755: /* for(j=1;j<=nlstate+ndeath;j++) { */
3756: /* printf("%f ",pmmij[i][j]); */
3757: /* } */
3758: /* printf(" oldm "); */
3759: /* for(j=1;j<=nlstate+ndeath;j++) { */
3760: /* printf("%f ",oldm[i][j]); */
3761: /* } */
3762: /* printf("\n"); */
3763: /* } */
3764: /* } */
3765: savm=oldm;
3766: oldm=newm;
3767: }
3768: for(i=1; i<=nlstate+ndeath; i++)
3769: for(j=1;j<=nlstate+ndeath;j++) {
3770: po[i][j][h]=newm[i][j];
3771: /*if(h==nhstepm) printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]);*/
3772: }
3773: /*printf("h=%d ",h);*/
3774: } /* end h */
3775: /* printf("\n H=%d \n",h); */
3776: return po;
3777: }
3778:
3779: /************* Higher Back Matrix Product ***************/
3780: /* 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 ) */
3781: double ***hbxij(double ***po, int nhstepm, double age, int hstepm, double *x, double ***prevacurrent, int nlstate, int stepm, int ij, int nres )
3782: {
3783: /* For dummy covariates given in each resultline (for historical, computes the corresponding combination ij),
3784: computes the transition matrix starting at age 'age' over
3785: 'nhstepm*hstepm*stepm' months (i.e. until
3786: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
3787: nhstepm*hstepm matrices.
3788: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
3789: (typically every 2 years instead of every month which is too big
3790: for the memory).
3791: Model is determined by parameters x and covariates have to be
3792: included manually here. Then we use a call to bmij(x and cov)
3793: The addresss of po (p3mat allocated to the dimension of nhstepm) should be stored for output
3794: */
3795:
3796: int i, j, d, h, k, k1;
3797: double **out, cov[NCOVMAX+1], **bmij();
3798: double **newm, ***newmm;
3799: double agexact;
3800: double agebegin, ageend;
3801: double **oldm, **savm;
3802:
3803: newmm=po; /* To be saved */
3804: oldm=oldms;savm=savms; /* Global pointers */
3805: /* Hstepm could be zero and should return the unit matrix */
3806: for (i=1;i<=nlstate+ndeath;i++)
3807: for (j=1;j<=nlstate+ndeath;j++){
3808: oldm[i][j]=(i==j ? 1.0 : 0.0);
3809: po[i][j][0]=(i==j ? 1.0 : 0.0);
3810: }
3811: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
3812: for(h=1; h <=nhstepm; h++){
3813: for(d=1; d <=hstepm; d++){
3814: newm=savm;
3815: /* Covariates have to be included here again */
3816: cov[1]=1.;
3817: agexact=age-( (h-1)*hstepm + (d) )*stepm/YEARM; /* age just before transition, d or d-1? */
3818: /* agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /\* age just before transition *\/ */
3819: /* Debug */
3820: /* printf("hBxij age=%lf, agexact=%lf\n", age, agexact); */
3821: cov[2]=agexact;
3822: if(nagesqr==1){
3823: cov[3]= agexact*agexact;
3824: }
3825: /** New code */
3826: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
3827: if(Typevar[k1]==1){ /* A product with age */
3828: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
3829: }else{
3830: cov[2+nagesqr+k1]=precov[nres][k1];
3831: }
3832: }/* End of loop on model equation */
3833: /** End of new code */
3834: /** This was old code */
3835: /* for (k=1; k<=nsd;k++){ /\* For single dummy covariates only *\//\* cptcovn error *\/ */
3836: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; *\/ */
3837: /* /\* /\\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; *\\/ *\/ */
3838: /* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,TvarsD[k])];/\* Bug valgrind *\/ */
3839: /* /\* 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)); *\/ */
3840: /* } */
3841: /* for (k=1; k<=nsq;k++) { /\* For single varying covariates only *\/ */
3842: /* /\* Here comes the value of quantitative after renumbering k with single quantitative covariates *\/ */
3843: /* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; */
3844: /* /\* 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]); *\/ */
3845: /* } */
3846: /* for (k=1; k<=cptcovage;k++){ /\* Should start at cptcovn+1 *\//\* For product with age *\/ */
3847: /* /\* if(Dummy[Tvar[Tage[k]]]== 2){ /\\* dummy with age error!!!*\\/ *\/ */
3848: /* if(Dummy[Tage[k]]== 2){ /\* dummy with age *\/ */
3849: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
3850: /* } else if(Dummy[Tage[k]]== 3){ /\* quantitative with age *\/ */
3851: /* cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; */
3852: /* } */
3853: /* /\* 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]); *\/ */
3854: /* } */
3855: /* for (k=1; k<=cptcovprod;k++){ /\* Useless because included in cptcovn *\/ */
3856: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])]*nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
3857: /* if(Dummy[Tvard[k][1]]==0){ */
3858: /* if(Dummy[Tvard[k][2]]==0){ */
3859: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][1])]; */
3860: /* }else{ */
3861: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * Tqresult[nres][k]; */
3862: /* } */
3863: /* }else{ */
3864: /* if(Dummy[Tvard[k][2]]==0){ */
3865: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])] * Tqinvresult[nres][Tvard[k][1]]; */
3866: /* }else{ */
3867: /* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; */
3868: /* } */
3869: /* } */
3870: /* } */
3871: /* /\*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*\/ */
3872: /* /\*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*\/ */
3873: /** End of old code */
3874:
3875: /* Careful transposed matrix */
3876: /* age is in cov[2], prevacurrent at beginning of transition. */
3877: /* out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm, dsavm,ij),\ */
3878: /* 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); */
3879: out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij),\
3880: 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm);/* Bug valgrind */
3881: /* if((int)age == 70){ */
3882: /* printf(" Backward hbxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
3883: /* for(i=1; i<=nlstate+ndeath; i++) { */
3884: /* printf("%d pmmij ",i); */
3885: /* for(j=1;j<=nlstate+ndeath;j++) { */
3886: /* printf("%f ",pmmij[i][j]); */
3887: /* } */
3888: /* printf(" oldm "); */
3889: /* for(j=1;j<=nlstate+ndeath;j++) { */
3890: /* printf("%f ",oldm[i][j]); */
3891: /* } */
3892: /* printf("\n"); */
3893: /* } */
3894: /* } */
3895: savm=oldm;
3896: oldm=newm;
3897: }
3898: for(i=1; i<=nlstate+ndeath; i++)
3899: for(j=1;j<=nlstate+ndeath;j++) {
3900: po[i][j][h]=newm[i][j];
3901: /* if(h==nhstepm) */
3902: /* printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]); */
3903: }
3904: /* printf("h=%d %.1f ",h, agexact); */
3905: } /* end h */
3906: /* printf("\n H=%d nhs=%d \n",h, nhstepm); */
3907: return po;
3908: }
3909:
3910:
3911: #ifdef NLOPT
3912: double myfunc(unsigned n, const double *p1, double *grad, void *pd){
3913: double fret;
3914: double *xt;
3915: int j;
3916: myfunc_data *d2 = (myfunc_data *) pd;
3917: /* xt = (p1-1); */
3918: xt=vector(1,n);
3919: for (j=1;j<=n;j++) xt[j]=p1[j-1]; /* xt[1]=p1[0] */
3920:
3921: fret=(d2->function)(xt); /* p xt[1]@8 is fine */
3922: /* fret=(*func)(xt); /\* p xt[1]@8 is fine *\/ */
3923: printf("Function = %.12lf ",fret);
3924: for (j=1;j<=n;j++) printf(" %d %.8lf", j, xt[j]);
3925: printf("\n");
3926: free_vector(xt,1,n);
3927: return fret;
3928: }
3929: #endif
3930:
3931: /*************** log-likelihood *************/
3932: double func( double *x)
3933: {
3934: int i, ii, j, k, mi, d, kk, kf=0;
3935: int ioffset=0;
3936: int ipos=0,iposold=0,ncovv=0;
3937:
3938: double cotvarv, cotvarvold;
3939: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
3940: double **out;
3941: double lli; /* Individual log likelihood */
3942: int s1, s2;
3943: int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate, quantitative time varying covariate */
3944:
3945: double bbh, survp;
3946: double agexact;
3947: double agebegin, ageend;
3948: /*extern weight */
3949: /* We are differentiating ll according to initial status */
3950: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
3951: /*for(i=1;i<imx;i++)
3952: printf(" %d\n",s[4][i]);
3953: */
3954:
3955: ++countcallfunc;
3956:
3957: cov[1]=1.;
3958:
3959: for(k=1; k<=nlstate; k++) ll[k]=0.;
3960: ioffset=0;
3961: if(mle==1){
3962: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3963: /* Computes the values of the ncovmodel covariates of the model
3964: depending if the covariates are fixed or varying (age dependent) and stores them in cov[]
3965: Then computes with function pmij which return a matrix p[i][j] giving the elementary probability
3966: to be observed in j being in i according to the model.
3967: */
3968: ioffset=2+nagesqr ;
3969: /* Fixed */
3970: for (kf=1; kf<=ncovf;kf++){ /* For each fixed covariate dummy or quant or prod */
3971: /* # V1=sex, V2=raedyrs Quant Fixed, State=livarnb4..livarnb11, V3=iadl4..iald11, V4=adlw4..adlw11, V5=r4bmi..r11bmi */
3972: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
3973: /* 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 */
3974: /* TvarFind; TvarFind[1]=6, TvarFind[2]=7, TvarFind[3]=9 *//* Inverse V2(6) is first fixed (single or prod) */
3975: 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)*/
3976: /* V1*V2 (7) TvarFind[2]=7, TvarFind[3]=9 */
3977: }
3978: /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4]
3979: is 5, Tvar[3=age*V3] should not be computed because of age Tvar[4=V3*V2]=6
3980: has been calculated etc */
3981: /* For an individual i, wav[i] gives the number of effective waves */
3982: /* We compute the contribution to Likelihood of each effective transition
3983: mw[mi][i] is real wave of the mi th effectve wave */
3984: /* Then statuses are computed at each begin and end of an effective wave s1=s[ mw[mi][i] ][i];
3985: s2=s[mw[mi+1][i]][i];
3986: And the iv th varying covariate is the cotvar[mw[mi+1][i]][iv][i] because now is moved after nvocol+nqv
3987: But if the variable is not in the model TTvar[iv] is the real variable effective in the model:
3988: meaning that decodemodel should be used cotvar[mw[mi+1][i]][TTvar[iv]][i]
3989: */
3990: for(mi=1; mi<= wav[i]-1; mi++){ /* Varying with waves */
3991: /* Wave varying (but not age varying) */
3992: /* 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*\/ */
3993: /* /\* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; but where is the crossproduct? *\/ */
3994: /* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]-ncovcol-nqv][i]; */
3995: /* } */
3996: for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){ /* Varying covariates (single and product but no age )*/
3997: itv=TvarVV[ncovv]; /* TvarVV={3, 1, 3} gives the name of each varying covariate */
3998: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
3999: if(FixedV[itv]!=0){ /* Not a fixed covariate */
4000: cotvarv=cotvar[mw[mi][i]][TvarVV[ncovv]][i]; /* cotvar[wav][ncovcol+nqv+iv][i] */
4001: }else{ /* fixed covariate */
4002: cotvarv=covar[itv][i]; /* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model */
4003: }
4004: if(ipos!=iposold){ /* Not a product or first of a product */
4005: cotvarvold=cotvarv;
4006: }else{ /* A second product */
4007: cotvarv=cotvarv*cotvarvold;
4008: }
4009: iposold=ipos;
4010: cov[ioffset+ipos]=cotvarv;
4011: }
4012: /* for(itv=1; itv <= ntveff; itv++){ /\* Varying dummy covariates (single??)*\/ */
4013: /* iv= Tvar[Tmodelind[ioffset-2-nagesqr-cptcovage+itv]]-ncovcol-nqv; /\* Counting the # varying covariate from 1 to ntveff *\/ */
4014: /* cov[ioffset+iv]=cotvar[mw[mi][i]][iv][i]; */
4015: /* k=ioffset-2-nagesqr-cptcovage+itv; /\* position in simple model *\/ */
4016: /* cov[ioffset+itv]=cotvar[mw[mi][i]][TmodelInvind[itv]][i]; */
4017: /* printf(" i=%d,mi=%d,itv=%d,TmodelInvind[itv]=%d,cotvar[mw[mi][i]][TmodelInvind[itv]][i]=%f\n", i, mi, itv, TmodelInvind[itv],cotvar[mw[mi][i]][TmodelInvind[itv]][i]); */
4018: /* } */
4019: /* for(iqtv=1; iqtv <= nqtveff; iqtv++){ /\* Varying quantitatives covariates *\/ */
4020: /* iv=TmodelInvQind[iqtv]; /\* Counting the # varying covariate from 1 to ntveff *\/ */
4021: /* /\* 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]); *\/ */
4022: /* cov[ioffset+ntveff+iqtv]=cotqvar[mw[mi][i]][TmodelInvQind[iqtv]][i]; */
4023: /* } */
4024: /* for products of time varying to be done */
4025: for (ii=1;ii<=nlstate+ndeath;ii++)
4026: for (j=1;j<=nlstate+ndeath;j++){
4027: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
4028: savm[ii][j]=(ii==j ? 1.0 : 0.0);
4029: }
4030:
4031: agebegin=agev[mw[mi][i]][i]; /* Age at beginning of effective wave */
4032: ageend=agev[mw[mi][i]][i] + (dh[mi][i])*stepm/YEARM; /* Age at end of effective wave and at the end of transition */
4033: for(d=0; d<dh[mi][i]; d++){
4034: newm=savm;
4035: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
4036: cov[2]=agexact;
4037: if(nagesqr==1)
4038: cov[3]= agexact*agexact; /* Should be changed here */
4039: for (kk=1; kk<=cptcovage;kk++) {
4040: if(!FixedV[Tvar[Tage[kk]]])
4041: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; /* Tage[kk] gives the data-covariate associated with age */
4042: else
4043: 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) */
4044: }
4045: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
4046: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
4047: savm=oldm;
4048: oldm=newm;
4049: } /* end mult */
4050:
4051: /*lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ /* Original formula */
4052: /* But now since version 0.9 we anticipate for bias at large stepm.
4053: * If stepm is larger than one month (smallest stepm) and if the exact delay
4054: * (in months) between two waves is not a multiple of stepm, we rounded to
4055: * the nearest (and in case of equal distance, to the lowest) interval but now
4056: * we keep into memory the bias bh[mi][i] and also the previous matrix product
4057: * (i.e to dh[mi][i]-1) saved in 'savm'. Then we inter(extra)polate the
4058: * probability in order to take into account the bias as a fraction of the way
4059: * from savm to out if bh is negative or even beyond if bh is positive. bh varies
4060: * -stepm/2 to stepm/2 .
4061: * For stepm=1 the results are the same as for previous versions of Imach.
4062: * For stepm > 1 the results are less biased than in previous versions.
4063: */
4064: s1=s[mw[mi][i]][i];
4065: s2=s[mw[mi+1][i]][i];
4066: bbh=(double)bh[mi][i]/(double)stepm;
4067: /* bias bh is positive if real duration
4068: * is higher than the multiple of stepm and negative otherwise.
4069: */
4070: /* lli= (savm[s1][s2]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2]));*/
4071: if( s2 > nlstate){
4072: /* i.e. if s2 is a death state and if the date of death is known
4073: then the contribution to the likelihood is the probability to
4074: die between last step unit time and current step unit time,
4075: which is also equal to probability to die before dh
4076: minus probability to die before dh-stepm .
4077: In version up to 0.92 likelihood was computed
4078: as if date of death was unknown. Death was treated as any other
4079: health state: the date of the interview describes the actual state
4080: and not the date of a change in health state. The former idea was
4081: to consider that at each interview the state was recorded
4082: (healthy, disable or death) and IMaCh was corrected; but when we
4083: introduced the exact date of death then we should have modified
4084: the contribution of an exact death to the likelihood. This new
4085: contribution is smaller and very dependent of the step unit
4086: stepm. It is no more the probability to die between last interview
4087: and month of death but the probability to survive from last
4088: interview up to one month before death multiplied by the
4089: probability to die within a month. Thanks to Chris
4090: Jackson for correcting this bug. Former versions increased
4091: mortality artificially. The bad side is that we add another loop
4092: which slows down the processing. The difference can be up to 10%
4093: lower mortality.
4094: */
4095: /* If, at the beginning of the maximization mostly, the
4096: cumulative probability or probability to be dead is
4097: constant (ie = 1) over time d, the difference is equal to
4098: 0. out[s1][3] = savm[s1][3]: probability, being at state
4099: s1 at precedent wave, to be dead a month before current
4100: wave is equal to probability, being at state s1 at
4101: precedent wave, to be dead at mont of the current
4102: wave. Then the observed probability (that this person died)
4103: is null according to current estimated parameter. In fact,
4104: it should be very low but not zero otherwise the log go to
4105: infinity.
4106: */
4107: /* #ifdef INFINITYORIGINAL */
4108: /* lli=log(out[s1][s2] - savm[s1][s2]); */
4109: /* #else */
4110: /* if ((out[s1][s2] - savm[s1][s2]) < mytinydouble) */
4111: /* lli=log(mytinydouble); */
4112: /* else */
4113: /* lli=log(out[s1][s2] - savm[s1][s2]); */
4114: /* #endif */
4115: lli=log(out[s1][s2] - savm[s1][s2]);
4116:
4117: } else if ( s2==-1 ) { /* alive */
4118: for (j=1,survp=0. ; j<=nlstate; j++)
4119: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
4120: /*survp += out[s1][j]; */
4121: lli= log(survp);
4122: }
4123: /* else if (s2==-4) { */
4124: /* for (j=3,survp=0. ; j<=nlstate; j++) */
4125: /* survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j]; */
4126: /* lli= log(survp); */
4127: /* } */
4128: /* else if (s2==-5) { */
4129: /* for (j=1,survp=0. ; j<=2; j++) */
4130: /* survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j]; */
4131: /* lli= log(survp); */
4132: /* } */
4133: else{
4134: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
4135: /* 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 */
4136: }
4137: /*lli=(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]);*/
4138: /*if(lli ==000.0)*/
4139: /* 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); */
4140: ipmx +=1;
4141: sw += weight[i];
4142: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
4143: /* if (lli < log(mytinydouble)){ */
4144: /* 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); */
4145: /* 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]); */
4146: /* } */
4147: } /* end of wave */
4148: } /* end of individual */
4149: } else if(mle==2){
4150: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
4151: ioffset=2+nagesqr ;
4152: for (k=1; k<=ncovf;k++)
4153: cov[ioffset+TvarFind[k]]=covar[Tvar[TvarFind[k]]][i];
4154: for(mi=1; mi<= wav[i]-1; mi++){
4155: for(k=1; k <= ncovv ; k++){
4156: cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; /* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) */
4157: }
4158: for (ii=1;ii<=nlstate+ndeath;ii++)
4159: for (j=1;j<=nlstate+ndeath;j++){
4160: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
4161: savm[ii][j]=(ii==j ? 1.0 : 0.0);
4162: }
4163: for(d=0; d<=dh[mi][i]; d++){
4164: newm=savm;
4165: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
4166: cov[2]=agexact;
4167: if(nagesqr==1)
4168: cov[3]= agexact*agexact;
4169: for (kk=1; kk<=cptcovage;kk++) {
4170: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
4171: }
4172: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
4173: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
4174: savm=oldm;
4175: oldm=newm;
4176: } /* end mult */
4177:
4178: s1=s[mw[mi][i]][i];
4179: s2=s[mw[mi+1][i]][i];
4180: bbh=(double)bh[mi][i]/(double)stepm;
4181: 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 */
4182: ipmx +=1;
4183: sw += weight[i];
4184: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
4185: } /* end of wave */
4186: } /* end of individual */
4187: } else if(mle==3){ /* exponential inter-extrapolation */
4188: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
4189: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
4190: for(mi=1; mi<= wav[i]-1; mi++){
4191: for (ii=1;ii<=nlstate+ndeath;ii++)
4192: for (j=1;j<=nlstate+ndeath;j++){
4193: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
4194: savm[ii][j]=(ii==j ? 1.0 : 0.0);
4195: }
4196: for(d=0; d<dh[mi][i]; d++){
4197: newm=savm;
4198: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
4199: cov[2]=agexact;
4200: if(nagesqr==1)
4201: cov[3]= agexact*agexact;
4202: for (kk=1; kk<=cptcovage;kk++) {
4203: if(!FixedV[Tvar[Tage[kk]]])
4204: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; /* Tage[kk] gives the data-covariate associated with age */
4205: else
4206: 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) */
4207: }
4208: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
4209: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
4210: savm=oldm;
4211: oldm=newm;
4212: } /* end mult */
4213:
4214: s1=s[mw[mi][i]][i];
4215: s2=s[mw[mi+1][i]][i];
4216: bbh=(double)bh[mi][i]/(double)stepm;
4217: 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 */
4218: ipmx +=1;
4219: sw += weight[i];
4220: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
4221: } /* end of wave */
4222: } /* end of individual */
4223: }else if (mle==4){ /* ml=4 no inter-extrapolation */
4224: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
4225: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
4226: for(mi=1; mi<= wav[i]-1; mi++){
4227: for (ii=1;ii<=nlstate+ndeath;ii++)
4228: for (j=1;j<=nlstate+ndeath;j++){
4229: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
4230: savm[ii][j]=(ii==j ? 1.0 : 0.0);
4231: }
4232: for(d=0; d<dh[mi][i]; d++){
4233: newm=savm;
4234: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
4235: cov[2]=agexact;
4236: if(nagesqr==1)
4237: cov[3]= agexact*agexact;
4238: for (kk=1; kk<=cptcovage;kk++) {
4239: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
4240: }
4241:
4242: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
4243: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
4244: savm=oldm;
4245: oldm=newm;
4246: } /* end mult */
4247:
4248: s1=s[mw[mi][i]][i];
4249: s2=s[mw[mi+1][i]][i];
4250: if( s2 > nlstate){
4251: lli=log(out[s1][s2] - savm[s1][s2]);
4252: } else if ( s2==-1 ) { /* alive */
4253: for (j=1,survp=0. ; j<=nlstate; j++)
4254: survp += out[s1][j];
4255: lli= log(survp);
4256: }else{
4257: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
4258: }
4259: ipmx +=1;
4260: sw += weight[i];
4261: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
4262: /* 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])); */
4263: } /* end of wave */
4264: } /* end of individual */
4265: }else{ /* ml=5 no inter-extrapolation no jackson =0.8a */
4266: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
4267: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
4268: for(mi=1; mi<= wav[i]-1; mi++){
4269: for (ii=1;ii<=nlstate+ndeath;ii++)
4270: for (j=1;j<=nlstate+ndeath;j++){
4271: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
4272: savm[ii][j]=(ii==j ? 1.0 : 0.0);
4273: }
4274: for(d=0; d<dh[mi][i]; d++){
4275: newm=savm;
4276: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
4277: cov[2]=agexact;
4278: if(nagesqr==1)
4279: cov[3]= agexact*agexact;
4280: for (kk=1; kk<=cptcovage;kk++) {
4281: if(!FixedV[Tvar[Tage[kk]]])
4282: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; /* Tage[kk] gives the data-covariate associated with age */
4283: else
4284: 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) */
4285: }
4286:
4287: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
4288: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
4289: savm=oldm;
4290: oldm=newm;
4291: } /* end mult */
4292:
4293: s1=s[mw[mi][i]][i];
4294: s2=s[mw[mi+1][i]][i];
4295: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
4296: ipmx +=1;
4297: sw += weight[i];
4298: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
4299: /*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]);*/
4300: } /* end of wave */
4301: } /* end of individual */
4302: } /* End of if */
4303: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
4304: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
4305: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
4306: return -l;
4307: }
4308:
4309: /*************** log-likelihood *************/
4310: double funcone( double *x)
4311: {
4312: /* Same as func but slower because of a lot of printf and if */
4313: int i, ii, j, k, mi, d, kk, kf=0;
4314: int ioffset=0;
4315: int ipos=0,iposold=0,ncovv=0;
4316:
4317: double cotvarv, cotvarvold;
4318: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
4319: double **out;
4320: double lli; /* Individual log likelihood */
4321: double llt;
4322: int s1, s2;
4323: int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate, quantitative time varying covariate */
4324:
4325: double bbh, survp;
4326: double agexact;
4327: double agebegin, ageend;
4328: /*extern weight */
4329: /* We are differentiating ll according to initial status */
4330: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
4331: /*for(i=1;i<imx;i++)
4332: printf(" %d\n",s[4][i]);
4333: */
4334: cov[1]=1.;
4335:
4336: for(k=1; k<=nlstate; k++) ll[k]=0.;
4337: ioffset=0;
4338: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
4339: /* Computes the values of the ncovmodel covariates of the model
4340: depending if the covariates are fixed or varying (age dependent) and stores them in cov[]
4341: Then computes with function pmij which return a matrix p[i][j] giving the elementary probability
4342: to be observed in j being in i according to the model.
4343: */
4344: /* ioffset=2+nagesqr+cptcovage; */
4345: ioffset=2+nagesqr;
4346: /* Fixed */
4347: /* for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i]; */
4348: /* for (k=1; k<=ncoveff;k++){ /\* Simple and product fixed Dummy covariates without age* products *\/ */
4349: for (kf=1; kf<=ncovf;kf++){ /* Simple and product fixed covariates without age* products *//* Missing values are set to -1 but should be dropped */
4350: /* printf("Debug3 TvarFind[%d]=%d",kf, TvarFind[kf]); */
4351: /* printf(" Tvar[TvarFind[kf]]=%d", Tvar[TvarFind[kf]]); */
4352: /* printf(" i=%d covar[Tvar[TvarFind[kf]]][i]=%f\n",i,covar[Tvar[TvarFind[kf]]][i]); */
4353: 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)*/
4354: /* cov[ioffset+TvarFind[1]]=covar[Tvar[TvarFind[1]]][i]; */
4355: /* cov[2+6]=covar[Tvar[6]][i]; */
4356: /* cov[2+6]=covar[2][i]; V2 */
4357: /* cov[TvarFind[2]]=covar[Tvar[TvarFind[2]]][i]; */
4358: /* cov[2+7]=covar[Tvar[7]][i]; */
4359: /* cov[2+7]=covar[7][i]; V7=V1*V2 */
4360: /* cov[TvarFind[3]]=covar[Tvar[TvarFind[3]]][i]; */
4361: /* cov[2+9]=covar[Tvar[9]][i]; */
4362: /* cov[2+9]=covar[1][i]; V1 */
4363: }
4364: /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4]
4365: is 5, Tvar[3=age*V3] should not be computed because of age Tvar[4=V3*V2]=6
4366: has been calculated etc */
4367: /* For an individual i, wav[i] gives the number of effective waves */
4368: /* We compute the contribution to Likelihood of each effective transition
4369: mw[mi][i] is real wave of the mi th effectve wave */
4370: /* Then statuses are computed at each begin and end of an effective wave s1=s[ mw[mi][i] ][i];
4371: s2=s[mw[mi+1][i]][i];
4372: And the iv th varying covariate in the DATA is the cotvar[mw[mi+1][i]][ncovcol+nqv+iv][i]
4373: */
4374: /* This part may be useless now because everythin should be in covar */
4375: /* for (k=1; k<=nqfveff;k++){ /\* Simple and product fixed Quantitative covariates without age* products *\/ */
4376: /* 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?)*\/ */
4377: /* } */
4378: /* for(iqv=1; iqv <= nqfveff; iqv++){ /\* Quantitative fixed covariates *\/ */
4379: /* cov[++ioffset]=coqvar[Tvar[iqv]][i]; /\* Only V2 k=6 and V1*V2 7 *\/ */
4380: /* } */
4381:
4382:
4383: for(mi=1; mi<= wav[i]-1; mi++){ /* Varying with waves */
4384: /* 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 */
4385: /* for(k=1; k <= ncovv ; k++){ /\* Varying covariates (single and product but no age )*\/ */
4386: /* /\* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; *\/ */
4387: /* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]-ncovcol-nqv][i]; */
4388: /* } */
4389:
4390: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
4391: /* model V1+V3+age*V1+age*V3+V1*V3 */
4392: /* Tvar={1, 3, 1, 3, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
4393: /* TvarVV[1]=V3 (first time varying in the model equation, TvarVV[2]=V1 (in V1*V3) TvarVV[3]=3(V3) */
4394: /* We need the position of the time varying or product in the model */
4395: /* 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 */
4396: /* TvarVV gives the variable name */
4397: /* Other example V1 + V3 + V5 + age*V1 + age*V3 + age*V5 + V1*V3 + V3*V5 + V1*V5
4398: * k= 1 2 3 4 5 6 7 8 9
4399: * varying 1 2 3 4 5
4400: * ncovv 1 2 3 4 5 6 7 8
4401: * TvarVV[ncovv] V3 5 1 3 3 5 1 5
4402: * TvarVVind 2 3 7 7 8 8 9 9
4403: * TvarFind[k] 1 0 0 0 0 0 0 0 0
4404: */
4405: /* Other model ncovcol=5 nqv=0 ntv=3 nqtv=0 nlstate=3
4406: * 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[4]=6
4407: * FixedV[ncovcol+qv+ntv+nqtv] V5
4408: * V1 V2 V3 V4 V5 V6 V7 V8
4409: * 0 0 0 0 0 1 1 1
4410: * model= V2 + V3 + V4 + V6 + V7 + V6*V2 + V7*V2 + V6*V3 + V7*V3 + V6*V4 + V7*V4
4411: * kmodel 1 2 3 4 5 6 7 8 9 10 11
4412: * ncovf 1 2 3
4413: * ncovvt=14 1 2 3 4 5 6 7 8 9 10 11 12 13 14
4414: * TvarVV[1]@14 = itv {6, 7, 6, 2, 7, 2, 6, 3, 7, 3, 6, 4, 7, 4}
4415: * TvarVVind[1]@14= {4, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11}
4416: * TvarFind[1]@14= {1, 2, 3, 0 <repeats 12 times>}
4417: * Tvar[1]@20= {2, 3, 4, 6, 7, 9, 10, 11, 12, 13, 14}
4418: * TvarFind[itv] 0 0 0
4419: * FixedV[itv] 1 1 1 0 1 0 1 0 1 0 0
4420: * Tvar[TvarFind[ncovf]]=[1]=2 [2]=3 [4]=4
4421: * Tvar[TvarFind[itv]] [0]=? ?ncovv 1 à ncovvt]
4422: * Not a fixed cotvar[mw][itv][i] 6 7 6 2 7, 2, 6, 3, 7, 3, 6, 4, 7, 4}
4423: * fixed covar[itv] [6] [7] [6][2]
4424: */
4425:
4426: for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){ /* Varying covariates (single and product but no age) including individual from products */
4427: itv=TvarVV[ncovv]; /* TvarVV={3, 1, 3} gives the name of each varying covariate, exploding product */
4428: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
4429: /* if(TvarFind[itv]==0){ /\* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv *\/ */
4430: if(FixedV[itv]!=0){ /* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv */
4431: cotvarv=cotvar[mw[mi][i]][TvarVV[ncovv]][i]; /* because cotvar starts now at first ncovcol+nqv+ntv+nqtv (1 to nqtv) */
4432: }else{ /* fixed covariate */
4433: /* cotvarv=covar[Tvar[TvarFind[itv]]][i]; /\* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model *\/ */
4434: cotvarv=covar[itv][i]; /* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model */
4435: }
4436: if(ipos!=iposold){ /* Not a product or first of a product */
4437: cotvarvold=cotvarv;
4438: }else{ /* A second product */
4439: cotvarv=cotvarv*cotvarvold;
4440: }
4441: iposold=ipos;
4442: cov[ioffset+ipos]=cotvarv;
4443: /* For products */
4444: }
4445: /* for(itv=1; itv <= ntveff; itv++){ /\* Varying dummy covariates single *\/ */
4446: /* iv=TvarVDind[itv]; /\* iv, position in the model equation of time varying covariate itv *\/ */
4447: /* /\* "V1+V3+age*V1+age*V3+V1*V3" with V3 time varying *\/ */
4448: /* /\* 1 2 3 4 5 *\/ */
4449: /* /\*itv 1 *\/ */
4450: /* /\* TvarVInd[1]= 2 *\/ */
4451: /* /\* iv= Tvar[Tmodelind[itv]]-ncovcol-nqv; /\\* Counting the # varying covariate from 1 to ntveff *\\/ *\/ */
4452: /* /\* iv= Tvar[Tmodelind[ioffset-2-nagesqr-cptcovage+itv]]-ncovcol-nqv; *\/ */
4453: /* /\* cov[ioffset+iv]=cotvar[mw[mi][i]][iv][i]; *\/ */
4454: /* /\* k=ioffset-2-nagesqr-cptcovage+itv; /\\* position in simple model *\\/ *\/ */
4455: /* /\* cov[ioffset+iv]=cotvar[mw[mi][i]][TmodelInvind[itv]][i]; *\/ */
4456: /* cov[ioffset+iv]=cotvar[mw[mi][i]][itv][i]; */
4457: /* /\* 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]); *\/ */
4458: /* } */
4459: /* for(iqtv=1; iqtv <= nqtveff; iqtv++){ /\* Varying quantitatives covariates *\/ */
4460: /* iv=TmodelInvQind[iqtv]; /\* Counting the # varying covariate from 1 to ntveff *\/ */
4461: /* /\* 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]); *\/ */
4462: /* cov[ioffset+ntveff+iqtv]=cotqvar[mw[mi][i]][TmodelInvQind[iqtv]][i]; */
4463: /* } */
4464: for (ii=1;ii<=nlstate+ndeath;ii++)
4465: for (j=1;j<=nlstate+ndeath;j++){
4466: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
4467: savm[ii][j]=(ii==j ? 1.0 : 0.0);
4468: }
4469:
4470: agebegin=agev[mw[mi][i]][i]; /* Age at beginning of effective wave */
4471: ageend=agev[mw[mi][i]][i] + (dh[mi][i])*stepm/YEARM; /* Age at end of effective wave and at the end of transition */
4472: for(d=0; d<dh[mi][i]; d++){ /* Delay between two effective waves */
4473: /* for(d=0; d<=0; d++){ /\* Delay between two effective waves Only one matrix to speed up*\/ */
4474: /*dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
4475: and mw[mi+1][i]. dh depends on stepm.*/
4476: newm=savm;
4477: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM; /* Here d is needed */
4478: cov[2]=agexact;
4479: if(nagesqr==1)
4480: cov[3]= agexact*agexact;
4481: for (kk=1; kk<=cptcovage;kk++) {
4482: if(!FixedV[Tvar[Tage[kk]]])
4483: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
4484: else
4485: 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) */
4486: }
4487: /* printf("i=%d,mi=%d,d=%d,mw[mi][i]=%d\n",i, mi,d,mw[mi][i]); */
4488: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
4489: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
4490: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
4491: /* out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath, */
4492: /* 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate)); */
4493: savm=oldm;
4494: oldm=newm;
4495: } /* end mult */
4496: /*lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ /* Original formula */
4497: /* But now since version 0.9 we anticipate for bias at large stepm.
4498: * If stepm is larger than one month (smallest stepm) and if the exact delay
4499: * (in months) between two waves is not a multiple of stepm, we rounded to
4500: * the nearest (and in case of equal distance, to the lowest) interval but now
4501: * we keep into memory the bias bh[mi][i] and also the previous matrix product
4502: * (i.e to dh[mi][i]-1) saved in 'savm'. Then we inter(extra)polate the
4503: * probability in order to take into account the bias as a fraction of the way
4504: * from savm to out if bh is negative or even beyond if bh is positive. bh varies
4505: * -stepm/2 to stepm/2 .
4506: * For stepm=1 the results are the same as for previous versions of Imach.
4507: * For stepm > 1 the results are less biased than in previous versions.
4508: */
4509: s1=s[mw[mi][i]][i];
4510: s2=s[mw[mi+1][i]][i];
4511: /* if(s2==-1){ */
4512: /* printf(" ERROR s1=%d, s2=%d i=%d \n", s1, s2, i); */
4513: /* /\* exit(1); *\/ */
4514: /* } */
4515: bbh=(double)bh[mi][i]/(double)stepm;
4516: /* bias is positive if real duration
4517: * is higher than the multiple of stepm and negative otherwise.
4518: */
4519: if( s2 > nlstate && (mle <5) ){ /* Jackson */
4520: lli=log(out[s1][s2] - savm[s1][s2]);
4521: } else if ( s2==-1 ) { /* alive */
4522: for (j=1,survp=0. ; j<=nlstate; j++)
4523: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
4524: lli= log(survp);
4525: }else if (mle==1){
4526: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
4527: } else if(mle==2){
4528: 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 */
4529: } else if(mle==3){ /* exponential inter-extrapolation */
4530: 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 */
4531: } else if (mle==4){ /* mle=4 no inter-extrapolation */
4532: lli=log(out[s1][s2]); /* Original formula */
4533: } else{ /* mle=0 back to 1 */
4534: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
4535: /*lli=log(out[s1][s2]); */ /* Original formula */
4536: } /* End of if */
4537: ipmx +=1;
4538: sw += weight[i];
4539: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
4540: /* Printing covariates values for each contribution for checking */
4541: /* 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])); */
4542: if(globpr){
4543: fprintf(ficresilk,"%09ld %6.1f %6.1f %6d %2d %2d %2d %2d %3d %15.6f %8.4f %8.3f\
4544: %11.6f %11.6f %11.6f ", \
4545: num[i], agebegin, ageend, i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],weight[i]*gipmx/gsw,
4546: 2*weight[i]*lli,(s2==-1? -1: out[s1][s2]),(s2==-1? -1: savm[s1][s2]));
4547: /* printf("%09ld %6.1f %6.1f %6d %2d %2d %2d %2d %3d %15.6f %8.4f %8.3f\ */
4548: /* %11.6f %11.6f %11.6f ", \ */
4549: /* num[i], agebegin, ageend, i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],weight[i]*gipmx/gsw, */
4550: /* 2*weight[i]*lli,(s2==-1? -1: out[s1][s2]),(s2==-1? -1: savm[s1][s2])); */
4551: for(k=1,llt=0.,l=0.; k<=nlstate; k++){
4552: llt +=ll[k]*gipmx/gsw;
4553: fprintf(ficresilk," %10.6f",-ll[k]*gipmx/gsw);
4554: /* printf(" %10.6f",-ll[k]*gipmx/gsw); */
4555: }
4556: fprintf(ficresilk," %10.6f ", -llt);
4557: /* printf(" %10.6f\n", -llt); */
4558: /* if(debugILK){ /\* debugILK is set by a #d in a comment line *\/ */
4559: /* fprintf(ficresilk,"%09ld ", num[i]); */ /* not necessary */
4560: for (kf=1; kf<=ncovf;kf++){ /* Simple and product fixed covariates without age* products *//* Missing values are set to -1 but should be dropped */
4561: fprintf(ficresilk," %g",covar[Tvar[TvarFind[kf]]][i]);
4562: }
4563: for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){ /* Varying covariates (single and product but no age) including individual from products */
4564: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
4565: if(ipos!=iposold){ /* Not a product or first of a product */
4566: fprintf(ficresilk," %g",cov[ioffset+ipos]);
4567: /* printf(" %g",cov[ioffset+ipos]); */
4568: }else{
4569: fprintf(ficresilk,"*");
4570: /* printf("*"); */
4571: }
4572: iposold=ipos;
4573: }
4574: for (kk=1; kk<=cptcovage;kk++) {
4575: if(!FixedV[Tvar[Tage[kk]]]){
4576: fprintf(ficresilk," %g*age",covar[Tvar[Tage[kk]]][i]);
4577: /* printf(" %g*age",covar[Tvar[Tage[kk]]][i]); */
4578: }else{
4579: fprintf(ficresilk," %g*age",cotvar[mw[mi][i]][Tvar[Tage[kk]]][i]);/* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) */
4580: /* printf(" %g*age",cotvar[mw[mi][i]][Tvar[Tage[kk]]][i]);/\* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) *\/ */
4581: }
4582: }
4583: /* printf("\n"); */
4584: /* } /\* End debugILK *\/ */
4585: fprintf(ficresilk,"\n");
4586: } /* End if globpr */
4587: } /* end of wave */
4588: } /* end of individual */
4589: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
4590: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
4591: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
4592: if(globpr==0){ /* First time we count the contributions and weights */
4593: gipmx=ipmx;
4594: gsw=sw;
4595: }
4596: return -l;
4597: }
4598:
4599:
4600: /*************** function likelione ***********/
4601: void likelione(FILE *ficres,double p[], int npar, int nlstate, int *globpri, long *ipmx, double *sw, double *fretone, double (*func)(double []))
4602: {
4603: /* This routine should help understanding what is done with
4604: the selection of individuals/waves and
4605: to check the exact contribution to the likelihood.
4606: Plotting could be done.
4607: */
4608: void pstamp(FILE *ficres);
4609: int k, kf, kk, kvar, ncovv, iposold, ipos;
4610:
4611: if(*globpri !=0){ /* Just counts and sums, no printings */
4612: strcpy(fileresilk,"ILK_");
4613: strcat(fileresilk,fileresu);
4614: if((ficresilk=fopen(fileresilk,"w"))==NULL) {
4615: printf("Problem with resultfile: %s\n", fileresilk);
4616: fprintf(ficlog,"Problem with resultfile: %s\n", fileresilk);
4617: }
4618: pstamp(ficresilk);fprintf(ficresilk,"# model=1+age+%s\n",model);
4619: 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");
4620: fprintf(ficresilk, "#num_i ageb agend i s1 s2 mi mw dh likeli weight %%weight 2wlli out sav ");
4621: /* i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],2*weight[i]*lli,out[s1][s2],savm[s1][s2]); */
4622: for(k=1; k<=nlstate; k++)
4623: fprintf(ficresilk," -2*gipw/gsw*weight*ll[%d]++",k);
4624: fprintf(ficresilk," -2*gipw/gsw*weight*ll(total) ");
4625:
4626: /* if(debugILK){ /\* debugILK is set by a #d in a comment line *\/ */
4627: for(kf=1;kf <= ncovf; kf++){
4628: fprintf(ficresilk,"V%d",Tvar[TvarFind[kf]]);
4629: /* printf("V%d",Tvar[TvarFind[kf]]); */
4630: }
4631: for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){
4632: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate */
4633: if(ipos!=iposold){ /* Not a product or first of a product */
4634: /* printf(" %d",ipos); */
4635: fprintf(ficresilk," V%d",TvarVV[ncovv]);
4636: }else{
4637: /* printf("*"); */
4638: fprintf(ficresilk,"*");
4639: }
4640: iposold=ipos;
4641: }
4642: for (kk=1; kk<=cptcovage;kk++) {
4643: if(!FixedV[Tvar[Tage[kk]]]){
4644: /* printf(" %d*age(Fixed)",Tvar[Tage[kk]]); */
4645: fprintf(ficresilk," %d*age(Fixed)",Tvar[Tage[kk]]);
4646: }else{
4647: fprintf(ficresilk," %d*age(Varying)",Tvar[Tage[kk]]);/* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) */
4648: /* printf(" %d*age(Varying)",Tvar[Tage[kk]]);/\* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) *\/ */
4649: }
4650: }
4651: /* } /\* End if debugILK *\/ */
4652: /* printf("\n"); */
4653: fprintf(ficresilk,"\n");
4654: } /* End glogpri */
4655:
4656: *fretone=(*func)(p);
4657: if(*globpri !=0){
4658: fclose(ficresilk);
4659: if (mle ==0)
4660: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with initial parameters and mle = %d.",mle);
4661: else if(mle >=1)
4662: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with optimized parameters mle = %d.",mle);
4663: 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));
4664: fprintf(fichtm,"\n<br>Equation of the model: <b>model=1+age+%s</b><br>\n",model);
4665:
4666: 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> \
4667: <img src=\"%s-ori.png\">\n",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
4668: fprintf(fichtm,"<br>- and by state of destination <a href=\"%s-dest.png\">%s-dest.png</a><br> \
4669: <img src=\"%s-dest.png\">\n",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
4670:
4671: for (k=1; k<= nlstate ; k++) {
4672: 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 \
4673: <img src=\"%s-p%dj.png\">\n",k,k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k);
4674: for(kf=1; kf <= ncovf; kf++){ /* For each simple dummy covariate of the model */
4675: /* kvar=Tvar[TvarFind[kf]]; */ /* variable */
4676: 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): <a href=\"%s-p%dj.png\">%s-p%dj.png</a><br> \
4677: <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]]);
4678: }
4679: for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){ /* Loop on the time varying extended covariates (with extension of Vn*Vm */
4680: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate */
4681: kvar=TvarVV[ncovv]; /* TvarVV={3, 1, 3} gives the name of each varying covariate */
4682: /* 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]); */
4683: if(ipos!=iposold){ /* Not a product or first of a product */
4684: /* fprintf(ficresilk," V%d",TvarVV[ncovv]); */
4685: /* printf(" DebugILK fichtm ipos=%d != iposold=%d\n", ipos, iposold); */
4686: 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) */
4687: 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> \
4688: <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);
4689: } /* End only for dummies time varying (single?) */
4690: }else{ /* Useless product */
4691: /* printf("*"); */
4692: /* fprintf(ficresilk,"*"); */
4693: }
4694: iposold=ipos;
4695: } /* For each time varying covariate */
4696: } /* End loop on states */
4697:
4698: /* if(debugILK){ */
4699: /* for(kf=1; kf <= ncovf; kf++){ /\* For each simple dummy covariate of the model *\/ */
4700: /* /\* kvar=Tvar[TvarFind[kf]]; *\/ /\* variable *\/ */
4701: /* for (k=1; k<= nlstate ; k++) { */
4702: /* 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> \ */
4703: /* <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]]); */
4704: /* } */
4705: /* } */
4706: /* for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){ /\* Loop on the time varying extended covariates (with extension of Vn*Vm *\/ */
4707: /* ipos=TvarVVind[ncovv]; /\* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate *\/ */
4708: /* kvar=TvarVV[ncovv]; /\* TvarVV={3, 1, 3} gives the name of each varying covariate *\/ */
4709: /* /\* 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]); *\/ */
4710: /* if(ipos!=iposold){ /\* Not a product or first of a product *\/ */
4711: /* /\* fprintf(ficresilk," V%d",TvarVV[ncovv]); *\/ */
4712: /* /\* printf(" DebugILK fichtm ipos=%d != iposold=%d\n", ipos, iposold); *\/ */
4713: /* 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) *\/ */
4714: /* for (k=1; k<= nlstate ; k++) { */
4715: /* 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> \ */
4716: /* <img src=\"%s-p%dj-%d.png\">",k,k,kvar,kvar,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,kvar); */
4717: /* } /\* End state *\/ */
4718: /* } /\* End only for dummies time varying (single?) *\/ */
4719: /* }else{ /\* Useless product *\/ */
4720: /* /\* printf("*"); *\/ */
4721: /* /\* fprintf(ficresilk,"*"); *\/ */
4722: /* } */
4723: /* iposold=ipos; */
4724: /* } /\* For each time varying covariate *\/ */
4725: /* }/\* End debugILK *\/ */
4726: fflush(fichtm);
4727: }/* End globpri */
4728: return;
4729: }
4730:
4731:
4732: /*********** Maximum Likelihood Estimation ***************/
4733:
4734: void mlikeli(FILE *ficres,double p[], int npar, int ncovmodel, int nlstate, double ftol, double (*func)(double []))
4735: {
4736: int i,j,k, jk, jkk=0, iter=0;
4737: double **xi;
4738: double fret;
4739: double fretone; /* Only one call to likelihood */
4740: /* char filerespow[FILENAMELENGTH];*/
4741:
4742: #ifdef NLOPT
4743: int creturn;
4744: nlopt_opt opt;
4745: /* double lb[9] = { -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL }; /\* lower bounds *\/ */
4746: double *lb;
4747: double minf; /* the minimum objective value, upon return */
4748: double * p1; /* Shifted parameters from 0 instead of 1 */
4749: myfunc_data dinst, *d = &dinst;
4750: #endif
4751:
4752:
4753: xi=matrix(1,npar,1,npar);
4754: for (i=1;i<=npar;i++)
4755: for (j=1;j<=npar;j++)
4756: xi[i][j]=(i==j ? 1.0 : 0.0);
4757: printf("Powell\n"); fprintf(ficlog,"Powell\n");
4758: strcpy(filerespow,"POW_");
4759: strcat(filerespow,fileres);
4760: if((ficrespow=fopen(filerespow,"w"))==NULL) {
4761: printf("Problem with resultfile: %s\n", filerespow);
4762: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
4763: }
4764: fprintf(ficrespow,"# Powell\n# iter -2*LL");
4765: for (i=1;i<=nlstate;i++)
4766: for(j=1;j<=nlstate+ndeath;j++)
4767: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
4768: fprintf(ficrespow,"\n");
4769: #ifdef POWELL
4770: #ifdef LINMINORIGINAL
4771: #else /* LINMINORIGINAL */
4772:
4773: flatdir=ivector(1,npar);
4774: for (j=1;j<=npar;j++) flatdir[j]=0;
4775: #endif /*LINMINORIGINAL */
4776:
4777: #ifdef FLATSUP
4778: powell(p,xi,npar,ftol,&iter,&fret,flatdir,func);
4779: /* reorganizing p by suppressing flat directions */
4780: for(i=1, jk=1; i <=nlstate; i++){
4781: for(k=1; k <=(nlstate+ndeath); k++){
4782: if (k != i) {
4783: printf("%d%d flatdir[%d]=%d",i,k,jk, flatdir[jk]);
4784: if(flatdir[jk]==1){
4785: printf(" To be skipped %d%d flatdir[%d]=%d ",i,k,jk, flatdir[jk]);
4786: }
4787: for(j=1; j <=ncovmodel; j++){
4788: printf("%12.7f ",p[jk]);
4789: jk++;
4790: }
4791: printf("\n");
4792: }
4793: }
4794: }
4795: /* skipping */
4796: /* for(i=1, jk=1, jkk=1;(flatdir[jk]==0)&& (i <=nlstate); i++){ */
4797: for(i=1, jk=1, jkk=1;i <=nlstate; i++){
4798: for(k=1; k <=(nlstate+ndeath); k++){
4799: if (k != i) {
4800: printf("%d%d flatdir[%d]=%d",i,k,jk, flatdir[jk]);
4801: if(flatdir[jk]==1){
4802: printf(" To be skipped %d%d flatdir[%d]=%d jk=%d p[%d] ",i,k,jk, flatdir[jk],jk, jk);
4803: for(j=1; j <=ncovmodel; jk++,j++){
4804: printf(" p[%d]=%12.7f",jk, p[jk]);
4805: /*q[jjk]=p[jk];*/
4806: }
4807: }else{
4808: printf(" To be kept %d%d flatdir[%d]=%d jk=%d q[%d]=p[%d] ",i,k,jk, flatdir[jk],jk, jkk, jk);
4809: for(j=1; j <=ncovmodel; jk++,jkk++,j++){
4810: printf(" p[%d]=%12.7f=q[%d]",jk, p[jk],jkk);
4811: /*q[jjk]=p[jk];*/
4812: }
4813: }
4814: printf("\n");
4815: }
4816: fflush(stdout);
4817: }
4818: }
4819: powell(p,xi,npar,ftol,&iter,&fret,flatdir,func);
4820: #else /* FLATSUP */
4821: powell(p,xi,npar,ftol,&iter,&fret,func);
4822: #endif /* FLATSUP */
4823:
4824: #ifdef LINMINORIGINAL
4825: #else
4826: free_ivector(flatdir,1,npar);
4827: #endif /* LINMINORIGINAL*/
4828: #endif /* POWELL */
4829:
4830: #ifdef NLOPT
4831: #ifdef NEWUOA
4832: opt = nlopt_create(NLOPT_LN_NEWUOA,npar);
4833: #else
4834: opt = nlopt_create(NLOPT_LN_BOBYQA,npar);
4835: #endif
4836: lb=vector(0,npar-1);
4837: for (i=0;i<npar;i++) lb[i]= -HUGE_VAL;
4838: nlopt_set_lower_bounds(opt, lb);
4839: nlopt_set_initial_step1(opt, 0.1);
4840:
4841: p1= (p+1); /* p *(p+1)@8 and p *(p1)@8 are equal p1[0]=p[1] */
4842: d->function = func;
4843: printf(" Func %.12lf \n",myfunc(npar,p1,NULL,d));
4844: nlopt_set_min_objective(opt, myfunc, d);
4845: nlopt_set_xtol_rel(opt, ftol);
4846: if ((creturn=nlopt_optimize(opt, p1, &minf)) < 0) {
4847: printf("nlopt failed! %d\n",creturn);
4848: }
4849: else {
4850: printf("found minimum after %d evaluations (NLOPT=%d)\n", countcallfunc ,NLOPT);
4851: printf("found minimum at f(%g,%g) = %0.10g\n", p[0], p[1], minf);
4852: iter=1; /* not equal */
4853: }
4854: nlopt_destroy(opt);
4855: #endif
4856: #ifdef FLATSUP
4857: /* npared = npar -flatd/ncovmodel; */
4858: /* xired= matrix(1,npared,1,npared); */
4859: /* paramred= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel); */
4860: /* powell(pred,xired,npared,ftol,&iter,&fret,flatdir,func); */
4861: /* free_matrix(xire,1,npared,1,npared); */
4862: #else /* FLATSUP */
4863: #endif /* FLATSUP */
4864: free_matrix(xi,1,npar,1,npar);
4865: fclose(ficrespow);
4866: printf("\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
4867: fprintf(ficlog,"\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
4868: fprintf(ficres,"#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
4869:
4870: }
4871:
4872: /**** Computes Hessian and covariance matrix ***/
4873: void hesscov(double **matcov, double **hess, double p[], int npar, double delti[], double ftolhess, double (*func)(double []))
4874: {
4875: double **a,**y,*x,pd;
4876: /* double **hess; */
4877: int i, j;
4878: int *indx;
4879:
4880: double hessii(double p[], double delta, int theta, double delti[],double (*func)(double []),int npar);
4881: double hessij(double p[], double **hess, double delti[], int i, int j,double (*func)(double []),int npar);
4882: void lubksb(double **a, int npar, int *indx, double b[]) ;
4883: void ludcmp(double **a, int npar, int *indx, double *d) ;
4884: double gompertz(double p[]);
4885: /* hess=matrix(1,npar,1,npar); */
4886:
4887: printf("\nCalculation of the hessian matrix. Wait...\n");
4888: fprintf(ficlog,"\nCalculation of the hessian matrix. Wait...\n");
4889: for (i=1;i<=npar;i++){
4890: printf("%d-",i);fflush(stdout);
4891: fprintf(ficlog,"%d-",i);fflush(ficlog);
4892:
4893: hess[i][i]=hessii(p,ftolhess,i,delti,func,npar);
4894:
4895: /* printf(" %f ",p[i]);
4896: printf(" %lf %lf %lf",hess[i][i],ftolhess,delti[i]);*/
4897: }
4898:
4899: for (i=1;i<=npar;i++) {
4900: for (j=1;j<=npar;j++) {
4901: if (j>i) {
4902: printf(".%d-%d",i,j);fflush(stdout);
4903: fprintf(ficlog,".%d-%d",i,j);fflush(ficlog);
4904: hess[i][j]=hessij(p,hess, delti,i,j,func,npar);
4905:
4906: hess[j][i]=hess[i][j];
4907: /*printf(" %lf ",hess[i][j]);*/
4908: }
4909: }
4910: }
4911: printf("\n");
4912: fprintf(ficlog,"\n");
4913:
4914: printf("\nInverting the hessian to get the covariance matrix. Wait...\n");
4915: fprintf(ficlog,"\nInverting the hessian to get the covariance matrix. Wait...\n");
4916:
4917: a=matrix(1,npar,1,npar);
4918: y=matrix(1,npar,1,npar);
4919: x=vector(1,npar);
4920: indx=ivector(1,npar);
4921: for (i=1;i<=npar;i++)
4922: for (j=1;j<=npar;j++) a[i][j]=hess[i][j];
4923: ludcmp(a,npar,indx,&pd);
4924:
4925: for (j=1;j<=npar;j++) {
4926: for (i=1;i<=npar;i++) x[i]=0;
4927: x[j]=1;
4928: lubksb(a,npar,indx,x);
4929: for (i=1;i<=npar;i++){
4930: matcov[i][j]=x[i];
4931: }
4932: }
4933:
4934: printf("\n#Hessian matrix#\n");
4935: fprintf(ficlog,"\n#Hessian matrix#\n");
4936: for (i=1;i<=npar;i++) {
4937: for (j=1;j<=npar;j++) {
4938: printf("%.6e ",hess[i][j]);
4939: fprintf(ficlog,"%.6e ",hess[i][j]);
4940: }
4941: printf("\n");
4942: fprintf(ficlog,"\n");
4943: }
4944:
4945: /* printf("\n#Covariance matrix#\n"); */
4946: /* fprintf(ficlog,"\n#Covariance matrix#\n"); */
4947: /* for (i=1;i<=npar;i++) { */
4948: /* for (j=1;j<=npar;j++) { */
4949: /* printf("%.6e ",matcov[i][j]); */
4950: /* fprintf(ficlog,"%.6e ",matcov[i][j]); */
4951: /* } */
4952: /* printf("\n"); */
4953: /* fprintf(ficlog,"\n"); */
4954: /* } */
4955:
4956: /* Recompute Inverse */
4957: /* for (i=1;i<=npar;i++) */
4958: /* for (j=1;j<=npar;j++) a[i][j]=matcov[i][j]; */
4959: /* ludcmp(a,npar,indx,&pd); */
4960:
4961: /* printf("\n#Hessian matrix recomputed#\n"); */
4962:
4963: /* for (j=1;j<=npar;j++) { */
4964: /* for (i=1;i<=npar;i++) x[i]=0; */
4965: /* x[j]=1; */
4966: /* lubksb(a,npar,indx,x); */
4967: /* for (i=1;i<=npar;i++){ */
4968: /* y[i][j]=x[i]; */
4969: /* printf("%.3e ",y[i][j]); */
4970: /* fprintf(ficlog,"%.3e ",y[i][j]); */
4971: /* } */
4972: /* printf("\n"); */
4973: /* fprintf(ficlog,"\n"); */
4974: /* } */
4975:
4976: /* Verifying the inverse matrix */
4977: #ifdef DEBUGHESS
4978: y=matprod2(y,hess,1,npar,1,npar,1,npar,matcov);
4979:
4980: printf("\n#Verification: multiplying the matrix of covariance by the Hessian matrix, should be unity:#\n");
4981: fprintf(ficlog,"\n#Verification: multiplying the matrix of covariance by the Hessian matrix. Should be unity:#\n");
4982:
4983: for (j=1;j<=npar;j++) {
4984: for (i=1;i<=npar;i++){
4985: printf("%.2f ",y[i][j]);
4986: fprintf(ficlog,"%.2f ",y[i][j]);
4987: }
4988: printf("\n");
4989: fprintf(ficlog,"\n");
4990: }
4991: #endif
4992:
4993: free_matrix(a,1,npar,1,npar);
4994: free_matrix(y,1,npar,1,npar);
4995: free_vector(x,1,npar);
4996: free_ivector(indx,1,npar);
4997: /* free_matrix(hess,1,npar,1,npar); */
4998:
4999:
5000: }
5001:
5002: /*************** hessian matrix ****************/
5003: double hessii(double x[], double delta, int theta, double delti[], double (*func)(double []), int npar)
5004: { /* Around values of x, computes the function func and returns the scales delti and hessian */
5005: int i;
5006: int l=1, lmax=20;
5007: double k1,k2, res, fx;
5008: double p2[MAXPARM+1]; /* identical to x */
5009: double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4;
5010: int k=0,kmax=10;
5011: double l1;
5012:
5013: fx=func(x);
5014: for (i=1;i<=npar;i++) p2[i]=x[i];
5015: for(l=0 ; l <=lmax; l++){ /* Enlarging the zone around the Maximum */
5016: l1=pow(10,l);
5017: delts=delt;
5018: for(k=1 ; k <kmax; k=k+1){
5019: delt = delta*(l1*k);
5020: p2[theta]=x[theta] +delt;
5021: k1=func(p2)-fx; /* Might be negative if too close to the theoretical maximum */
5022: p2[theta]=x[theta]-delt;
5023: k2=func(p2)-fx;
5024: /*res= (k1-2.0*fx+k2)/delt/delt; */
5025: res= (k1+k2)/delt/delt/2.; /* Divided by 2 because L and not 2*L */
5026:
5027: #ifdef DEBUGHESSII
5028: 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);
5029: 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);
5030: #endif
5031: /*if(fabs(k1-2.0*fx+k2) <1.e-13){ */
5032: if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)){
5033: k=kmax;
5034: }
5035: else if((k1 >khi/nkhif) || (k2 >khi/nkhif)){ /* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. */
5036: k=kmax; l=lmax*10;
5037: }
5038: else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){
5039: delts=delt;
5040: }
5041: } /* End loop k */
5042: }
5043: delti[theta]=delts;
5044: return res;
5045:
5046: }
5047:
5048: double hessij( double x[], double **hess, double delti[], int thetai,int thetaj,double (*func)(double []),int npar)
5049: {
5050: int i;
5051: int l=1, lmax=20;
5052: double k1,k2,k3,k4,res,fx;
5053: double p2[MAXPARM+1];
5054: int k, kmax=1;
5055: double v1, v2, cv12, lc1, lc2;
5056:
5057: int firstime=0;
5058:
5059: fx=func(x);
5060: for (k=1; k<=kmax; k=k+10) {
5061: for (i=1;i<=npar;i++) p2[i]=x[i];
5062: p2[thetai]=x[thetai]+delti[thetai]*k;
5063: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
5064: k1=func(p2)-fx;
5065:
5066: p2[thetai]=x[thetai]+delti[thetai]*k;
5067: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
5068: k2=func(p2)-fx;
5069:
5070: p2[thetai]=x[thetai]-delti[thetai]*k;
5071: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
5072: k3=func(p2)-fx;
5073:
5074: p2[thetai]=x[thetai]-delti[thetai]*k;
5075: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
5076: k4=func(p2)-fx;
5077: res=(k1-k2-k3+k4)/4.0/delti[thetai]/k/delti[thetaj]/k/2.; /* Because of L not 2*L */
5078: if(k1*k2*k3*k4 <0.){
5079: firstime=1;
5080: kmax=kmax+10;
5081: }
5082: if(kmax >=10 || firstime ==1){
5083: 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);
5084: 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);
5085: 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);
5086: 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);
5087: }
5088: #ifdef DEBUGHESSIJ
5089: v1=hess[thetai][thetai];
5090: v2=hess[thetaj][thetaj];
5091: cv12=res;
5092: /* Computing eigen value of Hessian matrix */
5093: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
5094: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
5095: if ((lc2 <0) || (lc1 <0) ){
5096: printf("Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
5097: fprintf(ficlog, "Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
5098: 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);
5099: 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);
5100: }
5101: #endif
5102: }
5103: return res;
5104: }
5105:
5106: /* Not done yet: Was supposed to fix if not exactly at the maximum */
5107: /* double hessij( double x[], double delti[], int thetai,int thetaj,double (*func)(double []),int npar) */
5108: /* { */
5109: /* int i; */
5110: /* int l=1, lmax=20; */
5111: /* double k1,k2,k3,k4,res,fx; */
5112: /* double p2[MAXPARM+1]; */
5113: /* double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4; */
5114: /* int k=0,kmax=10; */
5115: /* double l1; */
5116:
5117: /* fx=func(x); */
5118: /* for(l=0 ; l <=lmax; l++){ /\* Enlarging the zone around the Maximum *\/ */
5119: /* l1=pow(10,l); */
5120: /* delts=delt; */
5121: /* for(k=1 ; k <kmax; k=k+1){ */
5122: /* delt = delti*(l1*k); */
5123: /* for (i=1;i<=npar;i++) p2[i]=x[i]; */
5124: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
5125: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
5126: /* k1=func(p2)-fx; */
5127:
5128: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
5129: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
5130: /* k2=func(p2)-fx; */
5131:
5132: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
5133: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
5134: /* k3=func(p2)-fx; */
5135:
5136: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
5137: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
5138: /* k4=func(p2)-fx; */
5139: /* res=(k1-k2-k3+k4)/4.0/delti[thetai]*k/delti[thetaj]*k/2.; /\* Because of L not 2*L *\/ */
5140: /* #ifdef DEBUGHESSIJ */
5141: /* 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); */
5142: /* 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); */
5143: /* #endif */
5144: /* if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)){ */
5145: /* k=kmax; */
5146: /* } */
5147: /* else if((k1 >khi/nkhif) || (k2 >khi/nkhif) || (k4 >khi/nkhif) || (k4 >khi/nkhif)){ /\* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. *\/ */
5148: /* k=kmax; l=lmax*10; */
5149: /* } */
5150: /* else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){ */
5151: /* delts=delt; */
5152: /* } */
5153: /* } /\* End loop k *\/ */
5154: /* } */
5155: /* delti[theta]=delts; */
5156: /* return res; */
5157: /* } */
5158:
5159:
5160: /************** Inverse of matrix **************/
5161: void ludcmp(double **a, int n, int *indx, double *d)
5162: {
5163: int i,imax,j,k;
5164: double big,dum,sum,temp;
5165: double *vv;
5166:
5167: vv=vector(1,n);
5168: *d=1.0;
5169: for (i=1;i<=n;i++) {
5170: big=0.0;
5171: for (j=1;j<=n;j++)
5172: if ((temp=fabs(a[i][j])) > big) big=temp;
5173: if (big == 0.0){
5174: printf(" Singular Hessian matrix at row %d:\n",i);
5175: for (j=1;j<=n;j++) {
5176: printf(" a[%d][%d]=%f,",i,j,a[i][j]);
5177: fprintf(ficlog," a[%d][%d]=%f,",i,j,a[i][j]);
5178: }
5179: fflush(ficlog);
5180: fclose(ficlog);
5181: nrerror("Singular matrix in routine ludcmp");
5182: }
5183: vv[i]=1.0/big;
5184: }
5185: for (j=1;j<=n;j++) {
5186: for (i=1;i<j;i++) {
5187: sum=a[i][j];
5188: for (k=1;k<i;k++) sum -= a[i][k]*a[k][j];
5189: a[i][j]=sum;
5190: }
5191: big=0.0;
5192: for (i=j;i<=n;i++) {
5193: sum=a[i][j];
5194: for (k=1;k<j;k++)
5195: sum -= a[i][k]*a[k][j];
5196: a[i][j]=sum;
5197: if ( (dum=vv[i]*fabs(sum)) >= big) {
5198: big=dum;
5199: imax=i;
5200: }
5201: }
5202: if (j != imax) {
5203: for (k=1;k<=n;k++) {
5204: dum=a[imax][k];
5205: a[imax][k]=a[j][k];
5206: a[j][k]=dum;
5207: }
5208: *d = -(*d);
5209: vv[imax]=vv[j];
5210: }
5211: indx[j]=imax;
5212: if (a[j][j] == 0.0) a[j][j]=TINY;
5213: if (j != n) {
5214: dum=1.0/(a[j][j]);
5215: for (i=j+1;i<=n;i++) a[i][j] *= dum;
5216: }
5217: }
5218: free_vector(vv,1,n); /* Doesn't work */
5219: ;
5220: }
5221:
5222: void lubksb(double **a, int n, int *indx, double b[])
5223: {
5224: int i,ii=0,ip,j;
5225: double sum;
5226:
5227: for (i=1;i<=n;i++) {
5228: ip=indx[i];
5229: sum=b[ip];
5230: b[ip]=b[i];
5231: if (ii)
5232: for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j];
5233: else if (sum) ii=i;
5234: b[i]=sum;
5235: }
5236: for (i=n;i>=1;i--) {
5237: sum=b[i];
5238: for (j=i+1;j<=n;j++) sum -= a[i][j]*b[j];
5239: b[i]=sum/a[i][i];
5240: }
5241: }
5242:
5243: void pstamp(FILE *fichier)
5244: {
5245: fprintf(fichier,"# %s.%s\n#IMaCh version %s, %s\n#%s\n# %s", optionfilefiname,optionfilext,version,copyright, fullversion, strstart);
5246: }
5247:
5248: void date2dmy(double date,double *day, double *month, double *year){
5249: double yp=0., yp1=0., yp2=0.;
5250:
5251: yp1=modf(date,&yp);/* extracts integral of date in yp and
5252: fractional in yp1 */
5253: *year=yp;
5254: yp2=modf((yp1*12),&yp);
5255: *month=yp;
5256: yp1=modf((yp2*30.5),&yp);
5257: *day=yp;
5258: if(*day==0) *day=1;
5259: if(*month==0) *month=1;
5260: }
5261:
5262:
5263:
5264: /************ Frequencies ********************/
5265: void freqsummary(char fileres[], double p[], double pstart[], int iagemin, int iagemax, int **s, double **agev, int nlstate, int imx, \
5266: int *Tvaraff, int *invalidvarcomb, int **nbcode, int *ncodemax,double **mint,double **anint, char strstart[], \
5267: int firstpass, int lastpass, int stepm, int weightopt, char model[])
5268: { /* Some frequencies as well as proposing some starting values */
5269: /* Frequencies of any combination of dummy covariate used in the model equation */
5270: int i, m, jk, j1, bool, z1,j, nj, nl, k, iv, jj=0, s1=1, s2=1;
5271: int iind=0, iage=0;
5272: int mi; /* Effective wave */
5273: int first;
5274: double ***freq; /* Frequencies */
5275: 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 */
5276: 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);
5277: double *meanq, *stdq, *idq;
5278: double **meanqt;
5279: double *pp, **prop, *posprop, *pospropt;
5280: double pos=0., posproptt=0., pospropta=0., k2, dateintsum=0,k2cpt=0;
5281: char fileresp[FILENAMELENGTH], fileresphtm[FILENAMELENGTH], fileresphtmfr[FILENAMELENGTH];
5282: double agebegin, ageend;
5283:
5284: pp=vector(1,nlstate);
5285: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
5286: posprop=vector(1,nlstate); /* Counting the number of transition starting from a live state per age */
5287: pospropt=vector(1,nlstate); /* Counting the number of transition starting from a live state */
5288: /* prop=matrix(1,nlstate,iagemin,iagemax+3); */
5289: meanq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
5290: stdq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
5291: idq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
5292: meanqt=matrix(1,lastpass,1,nqtveff);
5293: strcpy(fileresp,"P_");
5294: strcat(fileresp,fileresu);
5295: /*strcat(fileresphtm,fileresu);*/
5296: if((ficresp=fopen(fileresp,"w"))==NULL) {
5297: printf("Problem with prevalence resultfile: %s\n", fileresp);
5298: fprintf(ficlog,"Problem with prevalence resultfile: %s\n", fileresp);
5299: exit(0);
5300: }
5301:
5302: strcpy(fileresphtm,subdirfext(optionfilefiname,"PHTM_",".htm"));
5303: if((ficresphtm=fopen(fileresphtm,"w"))==NULL) {
5304: printf("Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
5305: fprintf(ficlog,"Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
5306: fflush(ficlog);
5307: exit(70);
5308: }
5309: else{
5310: fprintf(ficresphtm,"<html><head>\n<title>IMaCh PHTM_ %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
5311: <hr size=\"2\" color=\"#EC5E5E\"> \n \
5312: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
5313: fileresphtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
5314: }
5315: 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);
5316:
5317: strcpy(fileresphtmfr,subdirfext(optionfilefiname,"PHTMFR_",".htm"));
5318: if((ficresphtmfr=fopen(fileresphtmfr,"w"))==NULL) {
5319: printf("Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
5320: fprintf(ficlog,"Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
5321: fflush(ficlog);
5322: exit(70);
5323: } else{
5324: fprintf(ficresphtmfr,"<html><head>\n<title>IMaCh PHTM_Frequency table %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
5325: ,<hr size=\"2\" color=\"#EC5E5E\"> \n \
5326: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
5327: fileresphtmfr,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
5328: }
5329: 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);
5330:
5331: y= vector(iagemin-AGEMARGE,iagemax+4+AGEMARGE);
5332: x= vector(iagemin-AGEMARGE,iagemax+4+AGEMARGE);
5333: freq= ma3x(-5,nlstate+ndeath,-5,nlstate+ndeath,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
5334: j1=0;
5335:
5336: /* j=ncoveff; /\* Only fixed dummy covariates *\/ */
5337: j=cptcoveff; /* Only simple dummy covariates used in the model */
5338: /* j=cptcovn; /\* Only dummy covariates of the model *\/ */
5339: if (cptcovn<1) {j=1;ncodemax[1]=1;}
5340:
5341:
5342: /* Detects if a combination j1 is empty: for a multinomial variable like 3 education levels:
5343: reference=low_education V1=0,V2=0
5344: med_educ V1=1 V2=0,
5345: high_educ V1=0 V2=1
5346: Then V1=1 and V2=1 is a noisy combination that we want to exclude for the list 2**cptcovn
5347: */
5348: dateintsum=0;
5349: k2cpt=0;
5350:
5351: if(cptcoveff == 0 )
5352: nl=1; /* Constant and age model only */
5353: else
5354: nl=2;
5355:
5356: /* if a constant only model, one pass to compute frequency tables and to write it on ficresp */
5357: /* Loop on nj=1 or 2 if dummy covariates j!=0
5358: * Loop on j1(1 to 2**cptcoveff) covariate combination
5359: * freq[s1][s2][iage] =0.
5360: * Loop on iind
5361: * ++freq[s1][s2][iage] weighted
5362: * end iind
5363: * if covariate and j!0
5364: * headers Variable on one line
5365: * endif cov j!=0
5366: * header of frequency table by age
5367: * Loop on age
5368: * pp[s1]+=freq[s1][s2][iage] weighted
5369: * pos+=freq[s1][s2][iage] weighted
5370: * Loop on s1 initial state
5371: * fprintf(ficresp
5372: * end s1
5373: * end age
5374: * if j!=0 computes starting values
5375: * end compute starting values
5376: * end j1
5377: * end nl
5378: */
5379: for (nj = 1; nj <= nl; nj++){ /* nj= 1 constant model, nl number of loops. */
5380: if(nj==1)
5381: j=0; /* First pass for the constant */
5382: else{
5383: j=cptcoveff; /* Other passes for the covariate values number of simple covariates in the model V2+V1 =2 (simple dummy fixed or time varying) */
5384: }
5385: first=1;
5386: 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 */
5387: posproptt=0.;
5388: /*printf("cptcovn=%d Tvaraff=%d", cptcovn,Tvaraff[1]);
5389: scanf("%d", i);*/
5390: for (i=-5; i<=nlstate+ndeath; i++)
5391: for (s2=-5; s2<=nlstate+ndeath; s2++)
5392: for(m=iagemin; m <= iagemax+3; m++)
5393: freq[i][s2][m]=0;
5394:
5395: for (i=1; i<=nlstate; i++) {
5396: for(m=iagemin; m <= iagemax+3; m++)
5397: prop[i][m]=0;
5398: posprop[i]=0;
5399: pospropt[i]=0;
5400: }
5401: for (z1=1; z1<= nqfveff; z1++) { /* zeroing for each combination j1 as well as for the total */
5402: idq[z1]=0.;
5403: meanq[z1]=0.;
5404: stdq[z1]=0.;
5405: }
5406: /* for (z1=1; z1<= nqtveff; z1++) { */
5407: /* for(m=1;m<=lastpass;m++){ */
5408: /* meanqt[m][z1]=0.; */
5409: /* } */
5410: /* } */
5411: /* dateintsum=0; */
5412: /* k2cpt=0; */
5413:
5414: /* For that combination of covariates j1 (V4=1 V3=0 for example), we count and print the frequencies in one pass */
5415: for (iind=1; iind<=imx; iind++) { /* For each individual iind */
5416: bool=1;
5417: if(j !=0){
5418: if(anyvaryingduminmodel==0){ /* If All fixed covariates */
5419: if (cptcoveff >0) { /* Filter is here: Must be looked at for model=V1+V2+V3+V4 */
5420: for (z1=1; z1<=cptcoveff; z1++) { /* loops on covariates in the model */
5421: /* if(Tvaraff[z1] ==-20){ */
5422: /* /\* sumnew+=cotvar[mw[mi][iind]][z1][iind]; *\/ */
5423: /* }else if(Tvaraff[z1] ==-10){ */
5424: /* /\* sumnew+=coqvar[z1][iind]; *\/ */
5425: /* }else */ /* TODO TODO codtabm(j1,z1) or codtabm(j1,Tvaraff[z1]]z1)*/
5426: /* 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); */
5427: if(Tvaraff[z1]<1 || Tvaraff[z1]>=NCOVMAX)
5428: printf("Error Tvaraff[z1]=%d<1 or >=%d, cptcoveff=%d model=1+age+%s\n",Tvaraff[z1],NCOVMAX, cptcoveff, model);
5429: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]){ /* for combination j1 of covariates */
5430: /* Tests if the value of the covariate z1 for this individual iind responded to combination j1 (V4=1 V3=0) */
5431: bool=0; /* bool should be equal to 1 to be selected, one covariate value failed */
5432: /* 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", */
5433: /* bool,i,z1, z1, Tvaraff[z1],i,covar[Tvaraff[z1]][i],j1,z1,codtabm(j1,z1),*/
5434: /* j1,z1,nbcode[Tvaraff[z1]][codtabm(j1,z1)],j1);*/
5435: /* For j1=7 in V1+V2+V3+V4 = 0 1 1 0 and codtabm(7,3)=1 and nbcde[3][?]=1*/
5436: } /* Onlyf fixed */
5437: } /* end z1 */
5438: } /* cptcoveff > 0 */
5439: } /* end any */
5440: }/* end j==0 */
5441: if (bool==1){ /* We selected an individual iind satisfying combination j1 (V4=1 V3=0) or all fixed covariates */
5442: /* for(m=firstpass; m<=lastpass; m++){ */
5443: for(mi=1; mi<wav[iind];mi++){ /* For each wave */
5444: m=mw[mi][iind];
5445: if(j!=0){
5446: if(anyvaryingduminmodel==1){ /* Some are varying covariates */
5447: for (z1=1; z1<=cptcoveff; z1++) {
5448: if( Fixed[Tmodelind[z1]]==1){
5449: /* iv= Tvar[Tmodelind[z1]]-ncovcol-nqv; /\* Good *\/ */
5450: iv= Tvar[Tmodelind[z1]]; /* Good *//* because cotvar starts now at first at ncovcol+nqv+ntv */
5451: if (cotvar[m][iv][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]) /* iv=1 to ntv, right modality. If covariate's
5452: value is -1, we don't select. It differs from the
5453: constant and age model which counts them. */
5454: bool=0; /* not selected */
5455: }else if( Fixed[Tmodelind[z1]]== 0) { /* fixed */
5456: /* i1=Tvaraff[z1]; */
5457: /* i2=TnsdVar[i1]; */
5458: /* i3=nbcode[i1][i2]; */
5459: /* i4=covar[i1][iind]; */
5460: /* if(i4 != i3){ */
5461: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]) { /* Bug valgrind */
5462: bool=0;
5463: }
5464: }
5465: }
5466: }/* Some are varying covariates, we tried to speed up if all fixed covariates in the model, avoiding waves loop */
5467: } /* end j==0 */
5468: /* bool =0 we keep that guy which corresponds to the combination of dummy values */
5469: if(bool==1){ /*Selected */
5470: /* dh[m][iind] or dh[mw[mi][iind]][iind] is the delay between two effective (mi) waves m=mw[mi][iind]
5471: and mw[mi+1][iind]. dh depends on stepm. */
5472: agebegin=agev[m][iind]; /* Age at beginning of wave before transition*/
5473: ageend=agev[m][iind]+(dh[m][iind])*stepm/YEARM; /* Age at end of wave and transition */
5474: if(m >=firstpass && m <=lastpass){
5475: k2=anint[m][iind]+(mint[m][iind]/12.);
5476: /*if ((k2>=dateprev1) && (k2<=dateprev2)) {*/
5477: if(agev[m][iind]==0) agev[m][iind]=iagemax+1; /* All ages equal to 0 are in iagemax+1 */
5478: if(agev[m][iind]==1) agev[m][iind]=iagemax+2; /* All ages equal to 1 are in iagemax+2 */
5479: if (s[m][iind]>0 && s[m][iind]<=nlstate) /* If status at wave m is known and a live state */
5480: prop[s[m][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */
5481: if (m<lastpass) {
5482: /* if(s[m][iind]==4 && s[m+1][iind]==4) */
5483: /* 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]); */
5484: if(s[m][iind]==-1)
5485: 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.));
5486: freq[s[m][iind]][s[m+1][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */
5487: for (z1=1; z1<= nqfveff; z1++) { /* Quantitative variables, calculating mean on known values only */
5488: if(!isnan(covar[ncovcol+z1][iind])){
5489: idq[z1]=idq[z1]+weight[iind];
5490: meanq[z1]+=covar[ncovcol+z1][iind]*weight[iind]; /* Computes mean of quantitative with selected filter */
5491: /* stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]*weight[iind]; *//*error*/
5492: stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]; /* *weight[iind];*/ /* Computes mean of quantitative with selected filter */
5493: }
5494: }
5495: /* if((int)agev[m][iind] == 55) */
5496: /* printf("j=%d, j1=%d Age %d, iind=%d, num=%09ld m=%d\n",j,j1,(int)agev[m][iind],iind, num[iind],m); */
5497: /* freq[s[m][iind]][s[m+1][iind]][(int)((agebegin+ageend)/2.)] += weight[iind]; */
5498: 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 */
5499: }
5500: } /* end if between passes */
5501: if ((agev[m][iind]>1) && (agev[m][iind]< (iagemax+3)) && (anint[m][iind]!=9999) && (mint[m][iind]!=99) && (j==0)) {
5502: dateintsum=dateintsum+k2; /* on all covariates ?*/
5503: k2cpt++;
5504: /* printf("iind=%ld dateintmean = %lf dateintsum=%lf k2cpt=%lf k2=%lf\n",iind, dateintsum/k2cpt, dateintsum,k2cpt, k2); */
5505: }
5506: }else{
5507: bool=1;
5508: }/* end bool 2 */
5509: } /* end m */
5510: /* for (z1=1; z1<= nqfveff; z1++) { /\* Quantitative variables, calculating mean *\/ */
5511: /* idq[z1]=idq[z1]+weight[iind]; */
5512: /* meanq[z1]+=covar[ncovcol+z1][iind]*weight[iind]; /\* Computes mean of quantitative with selected filter *\/ */
5513: /* stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]*weight[iind]; /\* *weight[iind];*\/ /\* Computes mean of quantitative with selected filter *\/ */
5514: /* } */
5515: } /* end bool */
5516: } /* end iind = 1 to imx */
5517: /* prop[s][age] is fed for any initial and valid live state as well as
5518: freq[s1][s2][age] at single age of beginning the transition, for a combination j1 */
5519:
5520:
5521: /* fprintf(ficresp, "#Count between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);*/
5522: if(cptcoveff==0 && nj==1) /* no covariate and first pass */
5523: pstamp(ficresp);
5524: if (cptcoveff>0 && j!=0){
5525: pstamp(ficresp);
5526: printf( "\n#********** Variable ");
5527: fprintf(ficresp, "\n#********** Variable ");
5528: fprintf(ficresphtm, "\n<br/><br/><h3>********** Variable ");
5529: fprintf(ficresphtmfr, "\n<br/><br/><h3>********** Variable ");
5530: fprintf(ficlog, "\n#********** Variable ");
5531: for (z1=1; z1<=cptcoveff; z1++){
5532: if(!FixedV[Tvaraff[z1]]){
5533: printf( "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
5534: fprintf(ficresp, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
5535: fprintf(ficresphtm, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
5536: fprintf(ficresphtmfr, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
5537: fprintf(ficlog, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
5538: }else{
5539: printf( "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
5540: fprintf(ficresp, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
5541: fprintf(ficresphtm, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
5542: fprintf(ficresphtmfr, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
5543: fprintf(ficlog, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
5544: }
5545: }
5546: printf( "**********\n#");
5547: fprintf(ficresp, "**********\n#");
5548: fprintf(ficresphtm, "**********</h3>\n");
5549: fprintf(ficresphtmfr, "**********</h3>\n");
5550: fprintf(ficlog, "**********\n");
5551: }
5552: /*
5553: Printing means of quantitative variables if any
5554: */
5555: for (z1=1; z1<= nqfveff; z1++) {
5556: fprintf(ficlog,"Mean of fixed quantitative variable V%d on %.3g (weighted) individuals sum=%f", ncovcol+z1, idq[z1], meanq[z1]);
5557: fprintf(ficlog,", mean=%.3g\n",meanq[z1]/idq[z1]);
5558: if(weightopt==1){
5559: printf(" Weighted mean and standard deviation of");
5560: fprintf(ficlog," Weighted mean and standard deviation of");
5561: fprintf(ficresphtmfr," Weighted mean and standard deviation of");
5562: }
5563: /* mu = \frac{w x}{\sum w}
5564: var = \frac{\sum w (x-mu)^2}{\sum w} = \frac{w x^2}{\sum w} - mu^2
5565: */
5566: 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]));
5567: 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]));
5568: 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]));
5569: }
5570: /* for (z1=1; z1<= nqtveff; z1++) { */
5571: /* for(m=1;m<=lastpass;m++){ */
5572: /* fprintf(ficresphtmfr,"V quantitative id %d, pass id=%d, mean=%f<p>\n", z1, m, meanqt[m][z1]); */
5573: /* } */
5574: /* } */
5575:
5576: fprintf(ficresphtm,"<table style=\"text-align:center; border: 1px solid\">");
5577: if((cptcoveff==0 && nj==1)|| nj==2 ) /* no covariate and first pass */
5578: fprintf(ficresp, " Age");
5579: if(nj==2) for (z1=1; z1<=cptcoveff; z1++) {
5580: 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]]);
5581: fprintf(ficresp, " V%d=%d",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
5582: }
5583: for(i=1; i<=nlstate;i++) {
5584: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp," Prev(%d) N(%d) N ",i,i);
5585: fprintf(ficresphtm, "<th>Age</th><th>Prev(%d)</th><th>N(%d)</th><th>N</th>",i,i);
5586: }
5587: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp, "\n");
5588: fprintf(ficresphtm, "\n");
5589:
5590: /* Header of frequency table by age */
5591: fprintf(ficresphtmfr,"<table style=\"text-align:center; border: 1px solid\">");
5592: fprintf(ficresphtmfr,"<th>Age</th> ");
5593: for(s2=-1; s2 <=nlstate+ndeath; s2++){
5594: for(m=-1; m <=nlstate+ndeath; m++){
5595: if(s2!=0 && m!=0)
5596: fprintf(ficresphtmfr,"<th>%d%d</th> ",s2,m);
5597: }
5598: }
5599: fprintf(ficresphtmfr, "\n");
5600:
5601: /* For each age */
5602: for(iage=iagemin; iage <= iagemax+3; iage++){
5603: fprintf(ficresphtm,"<tr>");
5604: if(iage==iagemax+1){
5605: fprintf(ficlog,"1");
5606: fprintf(ficresphtmfr,"<tr><th>0</th> ");
5607: }else if(iage==iagemax+2){
5608: fprintf(ficlog,"0");
5609: fprintf(ficresphtmfr,"<tr><th>Unknown</th> ");
5610: }else if(iage==iagemax+3){
5611: fprintf(ficlog,"Total");
5612: fprintf(ficresphtmfr,"<tr><th>Total</th> ");
5613: }else{
5614: if(first==1){
5615: first=0;
5616: printf("See log file for details...\n");
5617: }
5618: fprintf(ficresphtmfr,"<tr><th>%d</th> ",iage);
5619: fprintf(ficlog,"Age %d", iage);
5620: }
5621: for(s1=1; s1 <=nlstate ; s1++){
5622: for(m=-1, pp[s1]=0; m <=nlstate+ndeath ; m++)
5623: pp[s1] += freq[s1][m][iage];
5624: }
5625: for(s1=1; s1 <=nlstate ; s1++){
5626: for(m=-1, pos=0; m <=0 ; m++)
5627: pos += freq[s1][m][iage];
5628: if(pp[s1]>=1.e-10){
5629: if(first==1){
5630: printf(" %d.=%.0f loss[%d]=%.1f%%",s1,pp[s1],s1,100*pos/pp[s1]);
5631: }
5632: fprintf(ficlog," %d.=%.0f loss[%d]=%.1f%%",s1,pp[s1],s1,100*pos/pp[s1]);
5633: }else{
5634: if(first==1)
5635: printf(" %d.=%.0f loss[%d]=NaNQ%%",s1,pp[s1],s1);
5636: fprintf(ficlog," %d.=%.0f loss[%d]=NaNQ%%",s1,pp[s1],s1);
5637: }
5638: }
5639:
5640: for(s1=1; s1 <=nlstate ; s1++){
5641: /* posprop[s1]=0; */
5642: for(m=0, pp[s1]=0; m <=nlstate+ndeath; m++)/* Summing on all ages */
5643: pp[s1] += freq[s1][m][iage];
5644: } /* pp[s1] is the total number of transitions starting from state s1 and any ending status until this age */
5645:
5646: for(s1=1,pos=0, pospropta=0.; s1 <=nlstate ; s1++){
5647: pos += pp[s1]; /* pos is the total number of transitions until this age */
5648: posprop[s1] += prop[s1][iage]; /* prop is the number of transitions from a live state
5649: from s1 at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
5650: pospropta += prop[s1][iage]; /* prop is the number of transitions from a live state
5651: from s1 at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
5652: }
5653:
5654: /* Writing ficresp */
5655: if(cptcoveff==0 && nj==1){ /* no covariate and first pass */
5656: if( iage <= iagemax){
5657: fprintf(ficresp," %d",iage);
5658: }
5659: }else if( nj==2){
5660: if( iage <= iagemax){
5661: fprintf(ficresp," %d",iage);
5662: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresp, " %d %d",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
5663: }
5664: }
5665: for(s1=1; s1 <=nlstate ; s1++){
5666: if(pos>=1.e-5){
5667: if(first==1)
5668: printf(" %d.=%.0f prev[%d]=%.1f%%",s1,pp[s1],s1,100*pp[s1]/pos);
5669: fprintf(ficlog," %d.=%.0f prev[%d]=%.1f%%",s1,pp[s1],s1,100*pp[s1]/pos);
5670: }else{
5671: if(first==1)
5672: printf(" %d.=%.0f prev[%d]=NaNQ%%",s1,pp[s1],s1);
5673: fprintf(ficlog," %d.=%.0f prev[%d]=NaNQ%%",s1,pp[s1],s1);
5674: }
5675: if( iage <= iagemax){
5676: if(pos>=1.e-5){
5677: if(cptcoveff==0 && nj==1){ /* no covariate and first pass */
5678: fprintf(ficresp," %.5f %.0f %.0f",prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
5679: }else if( nj==2){
5680: fprintf(ficresp," %.5f %.0f %.0f",prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
5681: }
5682: fprintf(ficresphtm,"<th>%d</th><td>%.5f</td><td>%.0f</td><td>%.0f</td>",iage,prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
5683: /*probs[iage][s1][j1]= pp[s1]/pos;*/
5684: /*printf("\niage=%d s1=%d j1=%d %.5f %.0f %.0f %f",iage,s1,j1,pp[s1]/pos, pp[s1],pos,probs[iage][s1][j1]);*/
5685: } else{
5686: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp," NaNq %.0f %.0f",prop[s1][iage],pospropta);
5687: fprintf(ficresphtm,"<th>%d</th><td>NaNq</td><td>%.0f</td><td>%.0f</td>",iage, prop[s1][iage],pospropta);
5688: }
5689: }
5690: pospropt[s1] +=posprop[s1];
5691: } /* end loop s1 */
5692: /* pospropt=0.; */
5693: for(s1=-1; s1 <=nlstate+ndeath; s1++){
5694: for(m=-1; m <=nlstate+ndeath; m++){
5695: if(freq[s1][m][iage] !=0 ) { /* minimizing output */
5696: if(first==1){
5697: printf(" %d%d=%.0f",s1,m,freq[s1][m][iage]);
5698: }
5699: /* printf(" %d%d=%.0f",s1,m,freq[s1][m][iage]); */
5700: fprintf(ficlog," %d%d=%.0f",s1,m,freq[s1][m][iage]);
5701: }
5702: if(s1!=0 && m!=0)
5703: fprintf(ficresphtmfr,"<td>%.0f</td> ",freq[s1][m][iage]);
5704: }
5705: } /* end loop s1 */
5706: posproptt=0.;
5707: for(s1=1; s1 <=nlstate; s1++){
5708: posproptt += pospropt[s1];
5709: }
5710: fprintf(ficresphtmfr,"</tr>\n ");
5711: fprintf(ficresphtm,"</tr>\n");
5712: if((cptcoveff==0 && nj==1)|| nj==2 ) {
5713: if(iage <= iagemax)
5714: fprintf(ficresp,"\n");
5715: }
5716: if(first==1)
5717: printf("Others in log...\n");
5718: fprintf(ficlog,"\n");
5719: } /* end loop age iage */
5720:
5721: fprintf(ficresphtm,"<tr><th>Tot</th>");
5722: for(s1=1; s1 <=nlstate ; s1++){
5723: if(posproptt < 1.e-5){
5724: fprintf(ficresphtm,"<td>Nanq</td><td>%.0f</td><td>%.0f</td>",pospropt[s1],posproptt);
5725: }else{
5726: fprintf(ficresphtm,"<td>%.5f</td><td>%.0f</td><td>%.0f</td>",pospropt[s1]/posproptt,pospropt[s1],posproptt);
5727: }
5728: }
5729: fprintf(ficresphtm,"</tr>\n");
5730: fprintf(ficresphtm,"</table>\n");
5731: fprintf(ficresphtmfr,"</table>\n");
5732: if(posproptt < 1.e-5){
5733: fprintf(ficresphtm,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
5734: fprintf(ficresphtmfr,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
5735: fprintf(ficlog,"# This combination (%d) is not valid and no result will be produced\n",j1);
5736: printf("# This combination (%d) is not valid and no result will be produced\n",j1);
5737: invalidvarcomb[j1]=1;
5738: }else{
5739: fprintf(ficresphtm,"\n <p> This combination (%d) is valid and result will be produced (or no resultline).</p>",j1);
5740: invalidvarcomb[j1]=0;
5741: }
5742: fprintf(ficresphtmfr,"</table>\n");
5743: fprintf(ficlog,"\n");
5744: if(j!=0){
5745: printf("#Freqsummary: Starting values for combination j1=%d:\n", j1);
5746: for(i=1,s1=1; i <=nlstate; i++){
5747: for(k=1; k <=(nlstate+ndeath); k++){
5748: if (k != i) {
5749: for(jj=1; jj <=ncovmodel; jj++){ /* For counting s1 */
5750: if(jj==1){ /* Constant case (in fact cste + age) */
5751: if(j1==1){ /* All dummy covariates to zero */
5752: freq[i][k][iagemax+4]=freq[i][k][iagemax+3]; /* Stores case 0 0 0 */
5753: freq[i][i][iagemax+4]=freq[i][i][iagemax+3]; /* Stores case 0 0 0 */
5754: printf("%d%d ",i,k);
5755: fprintf(ficlog,"%d%d ",i,k);
5756: 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]));
5757: 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]));
5758: pstart[s1]= log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]);
5759: }
5760: }else if((j1==1) && (jj==2 || nagesqr==1)){ /* age or age*age parameter without covariate V4*age (to be done later) */
5761: for(iage=iagemin; iage <= iagemax+3; iage++){
5762: x[iage]= (double)iage;
5763: y[iage]= log(freq[i][k][iage]/freq[i][i][iage]);
5764: /* printf("i=%d, k=%d, s1=%d, j1=%d, jj=%d, y[%d]=%f\n",i,k,s1,j1,jj, iage, y[iage]); */
5765: }
5766: /* Some are not finite, but linreg will ignore these ages */
5767: no=0;
5768: linreg(iagemin,iagemax,&no,x,y,&a,&b,&r, &sa, &sb ); /* y= a+b*x with standard errors */
5769: pstart[s1]=b;
5770: pstart[s1-1]=a;
5771: }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 */
5772: 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]);
5773: 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]);
5774: pstart[s1]= log((freq[i][k][iagemax+3]/freq[i][i][iagemax+3])/(freq[i][k][iagemax+4]/freq[i][i][iagemax+4]));
5775: printf("%d%d ",i,k);
5776: fprintf(ficlog,"%d%d ",i,k);
5777: 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]));
5778: }else{ /* Other cases, like quantitative fixed or varying covariates */
5779: ;
5780: }
5781: /* printf("%12.7f )", param[i][jj][k]); */
5782: /* fprintf(ficlog,"%12.7f )", param[i][jj][k]); */
5783: s1++;
5784: } /* end jj */
5785: } /* end k!= i */
5786: } /* end k */
5787: } /* end i, s1 */
5788: } /* end j !=0 */
5789: } /* end selected combination of covariate j1 */
5790: if(j==0){ /* We can estimate starting values from the occurences in each case */
5791: printf("#Freqsummary: Starting values for the constants:\n");
5792: fprintf(ficlog,"\n");
5793: for(i=1,s1=1; i <=nlstate; i++){
5794: for(k=1; k <=(nlstate+ndeath); k++){
5795: if (k != i) {
5796: printf("%d%d ",i,k);
5797: fprintf(ficlog,"%d%d ",i,k);
5798: for(jj=1; jj <=ncovmodel; jj++){
5799: pstart[s1]=p[s1]; /* Setting pstart to p values by default */
5800: if(jj==1){ /* Age has to be done */
5801: pstart[s1]= log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]);
5802: 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]));
5803: 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]));
5804: }
5805: /* printf("%12.7f )", param[i][jj][k]); */
5806: /* fprintf(ficlog,"%12.7f )", param[i][jj][k]); */
5807: s1++;
5808: }
5809: printf("\n");
5810: fprintf(ficlog,"\n");
5811: }
5812: }
5813: } /* end of state i */
5814: printf("#Freqsummary\n");
5815: fprintf(ficlog,"\n");
5816: for(s1=-1; s1 <=nlstate+ndeath; s1++){
5817: for(s2=-1; s2 <=nlstate+ndeath; s2++){
5818: /* param[i]|j][k]= freq[s1][s2][iagemax+3] */
5819: printf(" %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]);
5820: fprintf(ficlog," %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]);
5821: /* if(freq[s1][s2][iage] !=0 ) { /\* minimizing output *\/ */
5822: /* printf(" %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]); */
5823: /* fprintf(ficlog," %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]); */
5824: /* } */
5825: }
5826: } /* end loop s1 */
5827:
5828: printf("\n");
5829: fprintf(ficlog,"\n");
5830: } /* end j=0 */
5831: } /* end j */
5832:
5833: if(mle == -2){ /* We want to use these values as starting values */
5834: for(i=1, jk=1; i <=nlstate; i++){
5835: for(j=1; j <=nlstate+ndeath; j++){
5836: if(j!=i){
5837: /*ca[0]= k+'a'-1;ca[1]='\0';*/
5838: printf("%1d%1d",i,j);
5839: fprintf(ficparo,"%1d%1d",i,j);
5840: for(k=1; k<=ncovmodel;k++){
5841: /* printf(" %lf",param[i][j][k]); */
5842: /* fprintf(ficparo," %lf",param[i][j][k]); */
5843: p[jk]=pstart[jk];
5844: printf(" %f ",pstart[jk]);
5845: fprintf(ficparo," %f ",pstart[jk]);
5846: jk++;
5847: }
5848: printf("\n");
5849: fprintf(ficparo,"\n");
5850: }
5851: }
5852: }
5853: } /* end mle=-2 */
5854: dateintmean=dateintsum/k2cpt;
5855: date2dmy(dateintmean,&jintmean,&mintmean,&aintmean);
5856:
5857: fclose(ficresp);
5858: fclose(ficresphtm);
5859: fclose(ficresphtmfr);
5860: free_vector(idq,1,nqfveff);
5861: free_vector(meanq,1,nqfveff);
5862: free_vector(stdq,1,nqfveff);
5863: free_matrix(meanqt,1,lastpass,1,nqtveff);
5864: free_vector(x, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
5865: free_vector(y, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
5866: free_ma3x(freq,-5,nlstate+ndeath,-5,nlstate+ndeath, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
5867: free_vector(pospropt,1,nlstate);
5868: free_vector(posprop,1,nlstate);
5869: free_matrix(prop,1,nlstate,iagemin-AGEMARGE, iagemax+4+AGEMARGE);
5870: free_vector(pp,1,nlstate);
5871: /* End of freqsummary */
5872: }
5873:
5874: /* Simple linear regression */
5875: int linreg(int ifi, int ila, int *no, const double x[], const double y[], double* a, double* b, double* r, double* sa, double * sb) {
5876:
5877: /* y=a+bx regression */
5878: double sumx = 0.0; /* sum of x */
5879: double sumx2 = 0.0; /* sum of x**2 */
5880: double sumxy = 0.0; /* sum of x * y */
5881: double sumy = 0.0; /* sum of y */
5882: double sumy2 = 0.0; /* sum of y**2 */
5883: double sume2 = 0.0; /* sum of square or residuals */
5884: double yhat;
5885:
5886: double denom=0;
5887: int i;
5888: int ne=*no;
5889:
5890: for ( i=ifi, ne=0;i<=ila;i++) {
5891: if(!isfinite(x[i]) || !isfinite(y[i])){
5892: /* printf(" x[%d]=%f, y[%d]=%f\n",i,x[i],i,y[i]); */
5893: continue;
5894: }
5895: ne=ne+1;
5896: sumx += x[i];
5897: sumx2 += x[i]*x[i];
5898: sumxy += x[i] * y[i];
5899: sumy += y[i];
5900: sumy2 += y[i]*y[i];
5901: denom = (ne * sumx2 - sumx*sumx);
5902: /* 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); */
5903: }
5904:
5905: denom = (ne * sumx2 - sumx*sumx);
5906: if (denom == 0) {
5907: // vertical, slope m is infinity
5908: *b = INFINITY;
5909: *a = 0;
5910: if (r) *r = 0;
5911: return 1;
5912: }
5913:
5914: *b = (ne * sumxy - sumx * sumy) / denom;
5915: *a = (sumy * sumx2 - sumx * sumxy) / denom;
5916: if (r!=NULL) {
5917: *r = (sumxy - sumx * sumy / ne) / /* compute correlation coeff */
5918: sqrt((sumx2 - sumx*sumx/ne) *
5919: (sumy2 - sumy*sumy/ne));
5920: }
5921: *no=ne;
5922: for ( i=ifi, ne=0;i<=ila;i++) {
5923: if(!isfinite(x[i]) || !isfinite(y[i])){
5924: /* printf(" x[%d]=%f, y[%d]=%f\n",i,x[i],i,y[i]); */
5925: continue;
5926: }
5927: ne=ne+1;
5928: yhat = y[i] - *a -*b* x[i];
5929: sume2 += yhat * yhat ;
5930:
5931: denom = (ne * sumx2 - sumx*sumx);
5932: /* 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); */
5933: }
5934: *sb = sqrt(sume2/(double)(ne-2)/(sumx2 - sumx * sumx /(double)ne));
5935: *sa= *sb * sqrt(sumx2/ne);
5936:
5937: return 0;
5938: }
5939:
5940: /************ Prevalence ********************/
5941: 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)
5942: {
5943: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
5944: in each health status at the date of interview (if between dateprev1 and dateprev2).
5945: We still use firstpass and lastpass as another selection.
5946: */
5947:
5948: int i, m, jk, j1, bool, z1,j, iv;
5949: int mi; /* Effective wave */
5950: int iage;
5951: double agebegin, ageend;
5952:
5953: double **prop;
5954: double posprop;
5955: double y2; /* in fractional years */
5956: int iagemin, iagemax;
5957: int first; /** to stop verbosity which is redirected to log file */
5958:
5959: iagemin= (int) agemin;
5960: iagemax= (int) agemax;
5961: /*pp=vector(1,nlstate);*/
5962: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
5963: /* freq=ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,iagemin,iagemax+3);*/
5964: j1=0;
5965:
5966: /*j=cptcoveff;*/
5967: if (cptcovn<1) {j=1;ncodemax[1]=1;}
5968:
5969: first=0;
5970: for(j1=1; j1<= (int) pow(2,cptcoveff);j1++){ /* For each combination of simple dummy covariates */
5971: for (i=1; i<=nlstate; i++)
5972: for(iage=iagemin-AGEMARGE; iage <= iagemax+4+AGEMARGE; iage++)
5973: prop[i][iage]=0.0;
5974: printf("Prevalence combination of varying and fixed dummies %d\n",j1);
5975: /* fprintf(ficlog," V%d=%d ",Tvaraff[j1],nbcode[Tvaraff[j1]][codtabm(k,j1)]); */
5976: fprintf(ficlog,"Prevalence combination of varying and fixed dummies %d\n",j1);
5977:
5978: for (i=1; i<=imx; i++) { /* Each individual */
5979: bool=1;
5980: /* for(m=firstpass; m<=lastpass; m++){/\* Other selection (we can limit to certain interviews*\/ */
5981: for(mi=1; mi<wav[i];mi++){ /* For this wave too look where individual can be counted V4=0 V3=0 */
5982: m=mw[mi][i];
5983: /* Tmodelind[z1]=k is the position of the varying covariate in the model, but which # within 1 to ntv? */
5984: /* Tvar[Tmodelind[z1]] is the n of Vn; n-ncovcol-nqv is the first time varying covariate or iv */
5985: for (z1=1; z1<=cptcoveff; z1++){
5986: if( Fixed[Tmodelind[z1]]==1){
5987: iv= Tvar[Tmodelind[z1]];/* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) */
5988: if (cotvar[m][iv][i]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]) /* iv=1 to ntv, right modality */
5989: bool=0;
5990: }else if( Fixed[Tmodelind[z1]]== 0) /* fixed */
5991: if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]) {
5992: bool=0;
5993: }
5994: }
5995: if(bool==1){ /* Otherwise we skip that wave/person */
5996: agebegin=agev[m][i]; /* Age at beginning of wave before transition*/
5997: /* ageend=agev[m][i]+(dh[m][i])*stepm/YEARM; /\* Age at end of wave and transition *\/ */
5998: if(m >=firstpass && m <=lastpass){
5999: y2=anint[m][i]+(mint[m][i]/12.); /* Fractional date in year */
6000: if ((y2>=dateprev1) && (y2<=dateprev2)) { /* Here is the main selection (fractional years) */
6001: if(agev[m][i]==0) agev[m][i]=iagemax+1;
6002: if(agev[m][i]==1) agev[m][i]=iagemax+2;
6003: if((int)agev[m][i] <iagemin-AGEMARGE || (int)agev[m][i] >iagemax+4+AGEMARGE){
6004: 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);
6005: exit(1);
6006: }
6007: if (s[m][i]>0 && s[m][i]<=nlstate) {
6008: /*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]]);*/
6009: prop[s[m][i]][(int)agev[m][i]] += weight[i];/* At age of beginning of transition, where status is known */
6010: prop[s[m][i]][iagemax+3] += weight[i];
6011: } /* end valid statuses */
6012: } /* end selection of dates */
6013: } /* end selection of waves */
6014: } /* end bool */
6015: } /* end wave */
6016: } /* end individual */
6017: for(i=iagemin; i <= iagemax+3; i++){
6018: for(jk=1,posprop=0; jk <=nlstate ; jk++) {
6019: posprop += prop[jk][i];
6020: }
6021:
6022: for(jk=1; jk <=nlstate ; jk++){
6023: if( i <= iagemax){
6024: if(posprop>=1.e-5){
6025: probs[i][jk][j1]= prop[jk][i]/posprop;
6026: } else{
6027: if(!first){
6028: first=1;
6029: 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]);
6030: }else{
6031: 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]);
6032: }
6033: }
6034: }
6035: }/* end jk */
6036: }/* end i */
6037: /*} *//* end i1 */
6038: } /* end j1 */
6039:
6040: /* free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath, iagemin, iagemax+3);*/
6041: /*free_vector(pp,1,nlstate);*/
6042: free_matrix(prop,1,nlstate, iagemin-AGEMARGE,iagemax+4+AGEMARGE);
6043: } /* End of prevalence */
6044:
6045: /************* Waves Concatenation ***************/
6046:
6047: 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)
6048: {
6049: /* Concatenates waves: wav[i] is the number of effective (useful waves in the sense that a non interview is useless) of individual i.
6050: Death is a valid wave (if date is known).
6051: mw[mi][i] is the mi (mi=1 to wav[i]) effective wave of individual i
6052: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
6053: and mw[mi+1][i]. dh depends on stepm. s[m][i] exists for any wave from firstpass to lastpass
6054: */
6055:
6056: int i=0, mi=0, m=0, mli=0;
6057: /* int j, k=0,jk, ju, jl,jmin=1e+5, jmax=-1;
6058: double sum=0., jmean=0.;*/
6059: int first=0, firstwo=0, firsthree=0, firstfour=0, firstfiv=0;
6060: int j, k=0,jk, ju, jl;
6061: double sum=0.;
6062: first=0;
6063: firstwo=0;
6064: firsthree=0;
6065: firstfour=0;
6066: jmin=100000;
6067: jmax=-1;
6068: jmean=0.;
6069:
6070: /* Treating live states */
6071: for(i=1; i<=imx; i++){ /* For simple cases and if state is death */
6072: mi=0; /* First valid wave */
6073: mli=0; /* Last valid wave */
6074: m=firstpass; /* Loop on waves */
6075: while(s[m][i] <= nlstate){ /* a live state or unknown state */
6076: 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 */
6077: mli=m-1;/* mw[++mi][i]=m-1; */
6078: }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 */
6079: mw[++mi][i]=m; /* Valid wave: incrementing mi and updating mi; mw[mi] is the wave number of mi_th valid transition */
6080: mli=m;
6081: } /* else might be a useless wave -1 and mi is not incremented and mw[mi] not updated */
6082: if(m < lastpass){ /* m < lastpass, standard case */
6083: m++; /* mi gives the "effective" current wave, m the current wave, go to next wave by incrementing m */
6084: }
6085: else{ /* m = lastpass, eventual special issue with warning */
6086: #ifdef UNKNOWNSTATUSNOTCONTRIBUTING
6087: break;
6088: #else
6089: 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 */
6090: if(firsthree == 0){
6091: 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);
6092: firsthree=1;
6093: }else if(firsthree >=1 && firsthree < 10){
6094: 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);
6095: firsthree++;
6096: }else if(firsthree == 10){
6097: printf("Information, too many Information flags: no more reported to log either\n");
6098: fprintf(ficlog,"Information, too many Information flags: no more reported to log either\n");
6099: firsthree++;
6100: }else{
6101: firsthree++;
6102: }
6103: mw[++mi][i]=m; /* Valid transition with unknown status */
6104: mli=m;
6105: }
6106: if(s[m][i]==-2){ /* Vital status is really unknown */
6107: nbwarn++;
6108: if((int)anint[m][i] == 9999){ /* Has the vital status really been verified?not a transition */
6109: 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);
6110: 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);
6111: }
6112: break;
6113: }
6114: break;
6115: #endif
6116: }/* End m >= lastpass */
6117: }/* end while */
6118:
6119: /* mi is the last effective wave, m is lastpass, mw[j][i] gives the # of j-th effective wave for individual i */
6120: /* After last pass */
6121: /* Treating death states */
6122: if (s[m][i] > nlstate){ /* In a death state */
6123: /* if( mint[m][i]==mdc[m][i] && anint[m][i]==andc[m][i]){ /\* same date of death and date of interview *\/ */
6124: /* } */
6125: mi++; /* Death is another wave */
6126: /* if(mi==0) never been interviewed correctly before death */
6127: /* Only death is a correct wave */
6128: mw[mi][i]=m;
6129: } /* else not in a death state */
6130: #ifndef DISPATCHINGKNOWNDEATHAFTERLASTWAVE
6131: else if ((int) andc[i] != 9999) { /* Date of death is known */
6132: if ((int)anint[m][i]!= 9999) { /* date of last interview is known */
6133: 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 */
6134: nbwarn++;
6135: if(firstfiv==0){
6136: 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 );
6137: firstfiv=1;
6138: }else{
6139: 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 );
6140: }
6141: s[m][i]=nlstate+1; /* Fixing the status as death. Be careful if multiple death states */
6142: }else{ /* Month of Death occured afer last wave month, potential bias */
6143: nberr++;
6144: if(firstwo==0){
6145: 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 );
6146: firstwo=1;
6147: }
6148: 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 );
6149: }
6150: }else{ /* if date of interview is unknown */
6151: /* death is known but not confirmed by death status at any wave */
6152: if(firstfour==0){
6153: 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 );
6154: firstfour=1;
6155: }
6156: 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 );
6157: }
6158: } /* end if date of death is known */
6159: #endif
6160: wav[i]=mi; /* mi should be the last effective wave (or mli), */
6161: /* wav[i]=mw[mi][i]; */
6162: if(mi==0){
6163: nbwarn++;
6164: if(first==0){
6165: printf("Warning! No valid information for individual %ld line=%d (skipped) and may be others, see log file\n",num[i],i);
6166: first=1;
6167: }
6168: if(first==1){
6169: fprintf(ficlog,"Warning! No valid information for individual %ld line=%d (skipped)\n",num[i],i);
6170: }
6171: } /* end mi==0 */
6172: } /* End individuals */
6173: /* wav and mw are no more changed */
6174:
6175: printf("Information, you have to check %d informations which haven't been logged!\n",firsthree);
6176: fprintf(ficlog,"Information, you have to check %d informations which haven't been logged!\n",firsthree);
6177:
6178:
6179: for(i=1; i<=imx; i++){
6180: for(mi=1; mi<wav[i];mi++){
6181: if (stepm <=0)
6182: dh[mi][i]=1;
6183: else{
6184: if (s[mw[mi+1][i]][i] > nlstate) { /* A death, but what if date is unknown? */
6185: if (agedc[i] < 2*AGESUP) {
6186: j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12);
6187: if(j==0) j=1; /* Survives at least one month after exam */
6188: else if(j<0){
6189: nberr++;
6190: printf("Error! Negative delay (%d to death) between waves %d and %d of individual %ld at line %d who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);
6191: j=1; /* Temporary Dangerous patch */
6192: 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);
6193: fprintf(ficlog,"Error! Negative delay (%d to death) between waves %d and %d of individual %ld at line %d who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);
6194: 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);
6195: }
6196: k=k+1;
6197: if (j >= jmax){
6198: jmax=j;
6199: ijmax=i;
6200: }
6201: if (j <= jmin){
6202: jmin=j;
6203: ijmin=i;
6204: }
6205: sum=sum+j;
6206: /*if (j<0) printf("j=%d num=%d \n",j,i);*/
6207: /* printf("%d %d %d %d\n", s[mw[mi][i]][i] ,s[mw[mi+1][i]][i],j,i);*/
6208: }
6209: }
6210: else{
6211: j= rint( (agev[mw[mi+1][i]][i]*12 - agev[mw[mi][i]][i]*12));
6212: /* 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]); */
6213:
6214: k=k+1;
6215: if (j >= jmax) {
6216: jmax=j;
6217: ijmax=i;
6218: }
6219: else if (j <= jmin){
6220: jmin=j;
6221: ijmin=i;
6222: }
6223: /* if (j<10) printf("j=%d jmin=%d num=%d ",j,jmin,i); */
6224: /*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]);*/
6225: if(j<0){
6226: nberr++;
6227: printf("Error! Negative delay (%d) between waves %d and %d of individual %ld at line %d who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);
6228: fprintf(ficlog,"Error! Negative delay (%d) between waves %d and %d of individual %ld at line %d who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);
6229: }
6230: sum=sum+j;
6231: }
6232: jk= j/stepm;
6233: jl= j -jk*stepm;
6234: ju= j -(jk+1)*stepm;
6235: if(mle <=1){ /* only if we use a the linear-interpoloation pseudo-likelihood */
6236: if(jl==0){
6237: dh[mi][i]=jk;
6238: bh[mi][i]=0;
6239: }else{ /* We want a negative bias in order to only have interpolation ie
6240: * to avoid the price of an extra matrix product in likelihood */
6241: dh[mi][i]=jk+1;
6242: bh[mi][i]=ju;
6243: }
6244: }else{
6245: if(jl <= -ju){
6246: dh[mi][i]=jk;
6247: bh[mi][i]=jl; /* bias is positive if real duration
6248: * is higher than the multiple of stepm and negative otherwise.
6249: */
6250: }
6251: else{
6252: dh[mi][i]=jk+1;
6253: bh[mi][i]=ju;
6254: }
6255: if(dh[mi][i]==0){
6256: dh[mi][i]=1; /* At least one step */
6257: bh[mi][i]=ju; /* At least one step */
6258: /* 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);*/
6259: }
6260: } /* end if mle */
6261: }
6262: } /* end wave */
6263: }
6264: jmean=sum/k;
6265: 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);
6266: 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);
6267: }
6268:
6269: /*********** Tricode ****************************/
6270: void tricode(int *cptcov, int *Tvar, int **nbcode, int imx, int *Ndum)
6271: {
6272: /**< Uses cptcovn+2*cptcovprod as the number of covariates */
6273: /* Tvar[i]=atoi(stre); find 'n' in Vn and stores in Tvar. If model=V2+V1 Tvar[1]=2 and Tvar[2]=1
6274: * Boring subroutine which should only output nbcode[Tvar[j]][k]
6275: * Tvar[5] in V2+V1+V3*age+V2*V4 is 4 (V4) even it is a time varying or quantitative variable
6276: * nbcode[Tvar[5]][1]= nbcode[4][1]=0, nbcode[4][2]=1 (usually);
6277: */
6278:
6279: int ij=1, k=0, j=0, i=0, maxncov=NCOVMAX;
6280: int modmaxcovj=0; /* Modality max of covariates j */
6281: int cptcode=0; /* Modality max of covariates j */
6282: int modmincovj=0; /* Modality min of covariates j */
6283:
6284:
6285: /* cptcoveff=0; */
6286: /* *cptcov=0; */
6287:
6288: for (k=1; k <= maxncov; k++) ncodemax[k]=0; /* Horrible constant again replaced by NCOVMAX */
6289: for (k=1; k <= maxncov; k++)
6290: for(j=1; j<=2; j++)
6291: nbcode[k][j]=0; /* Valgrind */
6292:
6293: /* Loop on covariates without age and products and no quantitative variable */
6294: 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 */
6295: for (j=-1; (j < maxncov); j++) Ndum[j]=0;
6296: /* printf("Testing k=%d, cptcovt=%d\n",k, cptcovt); */
6297: if(Dummy[k]==0 && Typevar[k] !=1 && Typevar[k] != 2){ /* Dummy covariate and not age product nor fixed product */
6298: switch(Fixed[k]) {
6299: case 0: /* Testing on fixed dummy covariate, simple or product of fixed */
6300: modmaxcovj=0;
6301: modmincovj=0;
6302: 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*/
6303: /* 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])); */
6304: ij=(int)(covar[Tvar[k]][i]);
6305: /* ij=0 or 1 or -1. Value of the covariate Tvar[j] for individual i
6306: * If product of Vn*Vm, still boolean *:
6307: * If it was coded 1, 2, 3, 4 should be splitted into 3 boolean variables
6308: * 1 => 0 0 0, 2 => 0 0 1, 3 => 0 1 1, 4=1 0 0 */
6309: /* Finds for covariate j, n=Tvar[j] of Vn . ij is the
6310: modality of the nth covariate of individual i. */
6311: if (ij > modmaxcovj)
6312: modmaxcovj=ij;
6313: else if (ij < modmincovj)
6314: modmincovj=ij;
6315: if (ij <0 || ij >1 ){
6316: printf("ERROR, IMaCh doesn't treat covariate with missing values V%d=-1, individual %d will be skipped.\n",Tvar[k],i);
6317: fprintf(ficlog,"ERROR, currently IMaCh doesn't treat covariate with missing values V%d=-1, individual %d will be skipped.\n",Tvar[k],i);
6318: fflush(ficlog);
6319: exit(1);
6320: }
6321: if ((ij < -1) || (ij > NCOVMAX)){
6322: printf( "Error: minimal is less than -1 or maximal is bigger than %d. Exiting. \n", NCOVMAX );
6323: exit(1);
6324: }else
6325: Ndum[ij]++; /*counts and stores the occurence of this modality 0, 1, -1*/
6326: /* If coded 1, 2, 3 , counts the number of 1 Ndum[1], number of 2, Ndum[2], etc */
6327: /*printf("i=%d ij=%d Ndum[ij]=%d imx=%d",i,ij,Ndum[ij],imx);*/
6328: /* getting the maximum value of the modality of the covariate
6329: (should be 0 or 1 now) Tvar[j]. If V=sex and male is coded 0 and
6330: female ies 1, then modmaxcovj=1.
6331: */
6332: } /* end for loop on individuals i */
6333: printf(" Minimal and maximal values of %d th (fixed) covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj);
6334: fprintf(ficlog," Minimal and maximal values of %d th (fixed) covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj);
6335: cptcode=modmaxcovj;
6336: /* Ndum[0] = frequency of 0 for model-covariate j, Ndum[1] frequency of 1 etc. */
6337: /*for (i=0; i<=cptcode; i++) {*/
6338: for (j=modmincovj; j<=modmaxcovj; j++) { /* j=-1 ? 0 and 1*//* For each value j of the modality of model-cov k */
6339: printf("Frequencies of (fixed) covariate %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]);
6340: fprintf(ficlog, "Frequencies of (fixed) covariate %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]);
6341: if( Ndum[j] != 0 ){ /* Counts if nobody answered modality j ie empty modality, we skip it and reorder */
6342: if( j != -1){
6343: ncodemax[k]++; /* ncodemax[k]= Number of modalities of the k th
6344: covariate for which somebody answered excluding
6345: undefined. Usually 2: 0 and 1. */
6346: }
6347: ncodemaxwundef[k]++; /* ncodemax[j]= Number of modalities of the k th
6348: covariate for which somebody answered including
6349: undefined. Usually 3: -1, 0 and 1. */
6350: } /* In fact ncodemax[k]=2 (dichotom. variables only) but it could be more for
6351: * historical reasons: 3 if coded 1, 2, 3 and 4 and Ndum[2]=0 */
6352: } /* Ndum[-1] number of undefined modalities */
6353:
6354: /* j is a covariate, n=Tvar[j] of Vn; Fills nbcode */
6355: /* For covariate j, modalities could be 1, 2, 3, 4, 5, 6, 7. */
6356: /* If Ndum[1]=0, Ndum[2]=0, Ndum[3]= 635, Ndum[4]=0, Ndum[5]=0, Ndum[6]=27, Ndum[7]=125; */
6357: /* modmincovj=3; modmaxcovj = 7; */
6358: /* There are only 3 modalities non empty 3, 6, 7 (or 2 if 27 is too few) : ncodemax[j]=3; */
6359: /* which will be coded 0, 1, 2 which in binary on 2=3-1 digits are 0=00 1=01, 2=10; */
6360: /* defining two dummy variables: variables V1_1 and V1_2.*/
6361: /* nbcode[Tvar[j]][ij]=k; */
6362: /* nbcode[Tvar[j]][1]=0; */
6363: /* nbcode[Tvar[j]][2]=1; */
6364: /* nbcode[Tvar[j]][3]=2; */
6365: /* To be continued (not working yet). */
6366: ij=0; /* ij is similar to i but can jump over null modalities */
6367:
6368: /* 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*/
6369: /* Skipping the case of missing values by reducing nbcode to 0 and 1 and not -1, 0, 1 */
6370: /* model=V1+V2+V3, if V2=-1, 0 or 1, then nbcode[2][1]=0 and nbcode[2][2]=1 instead of
6371: * nbcode[2][1]=-1, nbcode[2][2]=0 and nbcode[2][3]=1 */
6372: /*, could be restored in the future */
6373: 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*/
6374: if (Ndum[i] == 0) { /* If nobody responded to this modality k */
6375: break;
6376: }
6377: ij++;
6378: 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*/
6379: cptcode = ij; /* New max modality for covar j */
6380: } /* end of loop on modality i=-1 to 1 or more */
6381: break;
6382: case 1: /* Testing on varying covariate, could be simple and
6383: * should look at waves or product of fixed *
6384: * varying. No time to test -1, assuming 0 and 1 only */
6385: ij=0;
6386: for(i=0; i<=1;i++){
6387: nbcode[Tvar[k]][++ij]=i;
6388: }
6389: break;
6390: default:
6391: break;
6392: } /* end switch */
6393: } /* end dummy test */
6394: if(Dummy[k]==1 && Typevar[k] !=1 && Fixed ==0){ /* Fixed Quantitative covariate and not age product */
6395: 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*/
6396: if(Tvar[k]<=0 || Tvar[k]>=NCOVMAX){
6397: printf("Error k=%d \n",k);
6398: exit(1);
6399: }
6400: if(isnan(covar[Tvar[k]][i])){
6401: printf("ERROR, IMaCh doesn't treat fixed quantitative covariate with missing values V%d=., individual %d will be skipped.\n",Tvar[k],i);
6402: fprintf(ficlog,"ERROR, currently IMaCh doesn't treat covariate with missing values V%d=., individual %d will be skipped.\n",Tvar[k],i);
6403: fflush(ficlog);
6404: exit(1);
6405: }
6406: }
6407: } /* end Quanti */
6408: } /* 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*/
6409:
6410: for (k=-1; k< maxncov; k++) Ndum[k]=0;
6411: /* Look at fixed dummy (single or product) covariates to check empty modalities */
6412: for (i=1; i<=ncovmodel-2-nagesqr; i++) { /* -2, cste and age and eventually age*age */
6413: /* Listing of all covariables in statement model to see if some covariates appear twice. For example, V1 appears twice in V1+V1*V2.*/
6414: 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 */
6415: 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 */
6416: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, {2, 1, 1, 1, 2, 1, 1, 0, 0} */
6417: } /* V4+V3+V5, Ndum[1]@5={0, 0, 1, 1, 1} */
6418:
6419: ij=0;
6420: /* for (i=0; i<= maxncov-1; i++) { /\* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) *\/ */
6421: 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 */
6422: /* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) */
6423: /*printf("Ndum[%d]=%d\n",i, Ndum[i]);*/
6424: /* if((Ndum[i]!=0) && (i<=ncovcol)){ /\* Tvar[i] <= ncovmodel ? *\/ */
6425: if(Ndum[Tvar[k]]!=0 && Dummy[k] == 0 && Typevar[k]==0){ /* Only Dummy simple and non empty in the model */
6426: /* Typevar[k] =0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product */
6427: /* Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product*/
6428: /* If product not in single variable we don't print results */
6429: /*printf("diff Ndum[%d]=%d\n",i, Ndum[i]);*/
6430: ++ij;/* V5 + V4 + V3 + V4*V3 + V5*age + V2 + V1*V2 + V1*age + V1, *//* V5 quanti, V2 quanti, V4, V3, V1 dummies */
6431: /* k= 1 2 3 4 5 6 7 8 9 */
6432: /* Tvar[k]= 5 4 3 6 5 2 7 1 1 */
6433: /* ij 1 2 3 */
6434: /* Tvaraff[ij]= 4 3 1 */
6435: /* Tmodelind[ij]=2 3 9 */
6436: /* TmodelInvind[ij]=2 1 1 */
6437: 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*/
6438: Tmodelind[ij]=k; /* Tmodelind: index in model of dummies Tmodelind[1]=2 V4: pos=2; V3: pos=3, V1=9 {2, 3, 9, ?, ?,} */
6439: 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 */
6440: if(Fixed[k]!=0)
6441: anyvaryingduminmodel=1;
6442: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv)){ */
6443: /* Tvaraff[++ij]=-10; /\* Dont'n know how to treat quantitative variables yet *\/ */
6444: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv)){ */
6445: /* Tvaraff[++ij]=i; /\*For printing (unclear) *\/ */
6446: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv+nqtv)){ */
6447: /* Tvaraff[++ij]=-20; /\* Dont'n know how to treat quantitative variables yet *\/ */
6448: }
6449: } /* Tvaraff[1]@5 {3, 4, -20, 0, 0} Very strange */
6450: /* ij--; */
6451: /* cptcoveff=ij; /\*Number of total covariates*\/ */
6452: *cptcov=ij; /* cptcov= Number of total real effective simple dummies (fixed or time arying) effective (used as cptcoveff in other functions)
6453: * because they can be excluded from the model and real
6454: * if in the model but excluded because missing values, but how to get k from ij?*/
6455: for(j=ij+1; j<= cptcovt; j++){
6456: Tvaraff[j]=0;
6457: Tmodelind[j]=0;
6458: }
6459: for(j=ntveff+1; j<= cptcovt; j++){
6460: TmodelInvind[j]=0;
6461: }
6462: /* To be sorted */
6463: ;
6464: }
6465:
6466:
6467: /*********** Health Expectancies ****************/
6468:
6469: 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 )
6470:
6471: {
6472: /* Health expectancies, no variances */
6473: /* cij is the combination in the list of combination of dummy covariates */
6474: /* strstart is a string of time at start of computing */
6475: int i, j, nhstepm, hstepm, h, nstepm;
6476: int nhstepma, nstepma; /* Decreasing with age */
6477: double age, agelim, hf;
6478: double ***p3mat;
6479: double eip;
6480:
6481: /* pstamp(ficreseij); */
6482: fprintf(ficreseij,"# (a) Life expectancies by health status at initial age and (b) health expectancies by health status at initial age\n");
6483: fprintf(ficreseij,"# Age");
6484: for(i=1; i<=nlstate;i++){
6485: for(j=1; j<=nlstate;j++){
6486: fprintf(ficreseij," e%1d%1d ",i,j);
6487: }
6488: fprintf(ficreseij," e%1d. ",i);
6489: }
6490: fprintf(ficreseij,"\n");
6491:
6492:
6493: if(estepm < stepm){
6494: printf ("Problem %d lower than %d\n",estepm, stepm);
6495: }
6496: else hstepm=estepm;
6497: /* We compute the life expectancy from trapezoids spaced every estepm months
6498: * This is mainly to measure the difference between two models: for example
6499: * if stepm=24 months pijx are given only every 2 years and by summing them
6500: * we are calculating an estimate of the Life Expectancy assuming a linear
6501: * progression in between and thus overestimating or underestimating according
6502: * to the curvature of the survival function. If, for the same date, we
6503: * estimate the model with stepm=1 month, we can keep estepm to 24 months
6504: * to compare the new estimate of Life expectancy with the same linear
6505: * hypothesis. A more precise result, taking into account a more precise
6506: * curvature will be obtained if estepm is as small as stepm. */
6507:
6508: /* For example we decided to compute the life expectancy with the smallest unit */
6509: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
6510: nhstepm is the number of hstepm from age to agelim
6511: nstepm is the number of stepm from age to agelin.
6512: Look at hpijx to understand the reason which relies in memory size consideration
6513: and note for a fixed period like estepm months */
6514: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
6515: survival function given by stepm (the optimization length). Unfortunately it
6516: means that if the survival funtion is printed only each two years of age and if
6517: you sum them up and add 1 year (area under the trapezoids) you won't get the same
6518: results. So we changed our mind and took the option of the best precision.
6519: */
6520: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
6521:
6522: agelim=AGESUP;
6523: /* If stepm=6 months */
6524: /* Computed by stepm unit matrices, product of hstepm matrices, stored
6525: in an array of nhstepm length: nhstepm=10, hstepm=4, stepm=6 months */
6526:
6527: /* nhstepm age range expressed in number of stepm */
6528: nstepm=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
6529: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
6530: /* if (stepm >= YEARM) hstepm=1;*/
6531: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
6532: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6533:
6534: for (age=bage; age<=fage; age ++){
6535: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
6536: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
6537: /* if (stepm >= YEARM) hstepm=1;*/
6538: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
6539:
6540: /* If stepm=6 months */
6541: /* Computed by stepm unit matrices, product of hstepma matrices, stored
6542: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
6543: /* printf("HELLO evsij Entering hpxij age=%d cij=%d hstepm=%d x[1]=%f nres=%d\n",(int) age, cij, hstepm, x[1], nres); */
6544: hpxij(p3mat,nhstepma,age,hstepm,x,nlstate,stepm,oldm, savm, cij, nres);
6545:
6546: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
6547:
6548: printf("%d|",(int)age);fflush(stdout);
6549: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
6550:
6551: /* Computing expectancies */
6552: for(i=1; i<=nlstate;i++)
6553: for(j=1; j<=nlstate;j++)
6554: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
6555: eij[i][j][(int)age] += (p3mat[i][j][h]+p3mat[i][j][h+1])/2.0*hf;
6556:
6557: /* 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]);*/
6558:
6559: }
6560:
6561: fprintf(ficreseij,"%3.0f",age );
6562: for(i=1; i<=nlstate;i++){
6563: eip=0;
6564: for(j=1; j<=nlstate;j++){
6565: eip +=eij[i][j][(int)age];
6566: fprintf(ficreseij,"%9.4f", eij[i][j][(int)age] );
6567: }
6568: fprintf(ficreseij,"%9.4f", eip );
6569: }
6570: fprintf(ficreseij,"\n");
6571:
6572: }
6573: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6574: printf("\n");
6575: fprintf(ficlog,"\n");
6576:
6577: }
6578:
6579: 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 )
6580:
6581: {
6582: /* Covariances of health expectancies eij and of total life expectancies according
6583: to initial status i, ei. .
6584: */
6585: /* Very time consuming function, but already optimized with precov */
6586: int i, j, nhstepm, hstepm, h, nstepm, k, cptj, cptj2, i2, j2, ij, ji;
6587: int nhstepma, nstepma; /* Decreasing with age */
6588: double age, agelim, hf;
6589: double ***p3matp, ***p3matm, ***varhe;
6590: double **dnewm,**doldm;
6591: double *xp, *xm;
6592: double **gp, **gm;
6593: double ***gradg, ***trgradg;
6594: int theta;
6595:
6596: double eip, vip;
6597:
6598: varhe=ma3x(1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int) fage);
6599: xp=vector(1,npar);
6600: xm=vector(1,npar);
6601: dnewm=matrix(1,nlstate*nlstate,1,npar);
6602: doldm=matrix(1,nlstate*nlstate,1,nlstate*nlstate);
6603:
6604: pstamp(ficresstdeij);
6605: fprintf(ficresstdeij,"# Health expectancies with standard errors\n");
6606: fprintf(ficresstdeij,"# Age");
6607: for(i=1; i<=nlstate;i++){
6608: for(j=1; j<=nlstate;j++)
6609: fprintf(ficresstdeij," e%1d%1d (SE)",i,j);
6610: fprintf(ficresstdeij," e%1d. ",i);
6611: }
6612: fprintf(ficresstdeij,"\n");
6613:
6614: pstamp(ficrescveij);
6615: fprintf(ficrescveij,"# Subdiagonal matrix of covariances of health expectancies by age: cov(eij,ekl)\n");
6616: fprintf(ficrescveij,"# Age");
6617: for(i=1; i<=nlstate;i++)
6618: for(j=1; j<=nlstate;j++){
6619: cptj= (j-1)*nlstate+i;
6620: for(i2=1; i2<=nlstate;i2++)
6621: for(j2=1; j2<=nlstate;j2++){
6622: cptj2= (j2-1)*nlstate+i2;
6623: if(cptj2 <= cptj)
6624: fprintf(ficrescveij," %1d%1d,%1d%1d",i,j,i2,j2);
6625: }
6626: }
6627: fprintf(ficrescveij,"\n");
6628:
6629: if(estepm < stepm){
6630: printf ("Problem %d lower than %d\n",estepm, stepm);
6631: }
6632: else hstepm=estepm;
6633: /* We compute the life expectancy from trapezoids spaced every estepm months
6634: * This is mainly to measure the difference between two models: for example
6635: * if stepm=24 months pijx are given only every 2 years and by summing them
6636: * we are calculating an estimate of the Life Expectancy assuming a linear
6637: * progression in between and thus overestimating or underestimating according
6638: * to the curvature of the survival function. If, for the same date, we
6639: * estimate the model with stepm=1 month, we can keep estepm to 24 months
6640: * to compare the new estimate of Life expectancy with the same linear
6641: * hypothesis. A more precise result, taking into account a more precise
6642: * curvature will be obtained if estepm is as small as stepm. */
6643:
6644: /* For example we decided to compute the life expectancy with the smallest unit */
6645: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
6646: nhstepm is the number of hstepm from age to agelim
6647: nstepm is the number of stepm from age to agelin.
6648: Look at hpijx to understand the reason of that which relies in memory size
6649: and note for a fixed period like estepm months */
6650: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
6651: survival function given by stepm (the optimization length). Unfortunately it
6652: means that if the survival funtion is printed only each two years of age and if
6653: you sum them up and add 1 year (area under the trapezoids) you won't get the same
6654: results. So we changed our mind and took the option of the best precision.
6655: */
6656: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
6657:
6658: /* If stepm=6 months */
6659: /* nhstepm age range expressed in number of stepm */
6660: agelim=AGESUP;
6661: nstepm=(int) rint((agelim-bage)*YEARM/stepm);
6662: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
6663: /* if (stepm >= YEARM) hstepm=1;*/
6664: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
6665:
6666: p3matp=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6667: p3matm=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6668: gradg=ma3x(0,nhstepm,1,npar,1,nlstate*nlstate);
6669: trgradg =ma3x(0,nhstepm,1,nlstate*nlstate,1,npar);
6670: gp=matrix(0,nhstepm,1,nlstate*nlstate);
6671: gm=matrix(0,nhstepm,1,nlstate*nlstate);
6672:
6673: for (age=bage; age<=fage; age ++){
6674: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
6675: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
6676: /* if (stepm >= YEARM) hstepm=1;*/
6677: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
6678:
6679: /* If stepm=6 months */
6680: /* Computed by stepm unit matrices, product of hstepma matrices, stored
6681: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
6682:
6683: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
6684:
6685: /* Computing Variances of health expectancies */
6686: /* Gradient is computed with plus gp and minus gm. Code is duplicated in order to
6687: decrease memory allocation */
6688: for(theta=1; theta <=npar; theta++){
6689: for(i=1; i<=npar; i++){
6690: xp[i] = x[i] + (i==theta ?delti[theta]:0);
6691: xm[i] = x[i] - (i==theta ?delti[theta]:0);
6692: }
6693: hpxij(p3matp,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, cij, nres);
6694: hpxij(p3matm,nhstepm,age,hstepm,xm,nlstate,stepm,oldm,savm, cij, nres);
6695:
6696: for(j=1; j<= nlstate; j++){
6697: for(i=1; i<=nlstate; i++){
6698: for(h=0; h<=nhstepm-1; h++){
6699: gp[h][(j-1)*nlstate + i] = (p3matp[i][j][h]+p3matp[i][j][h+1])/2.;
6700: gm[h][(j-1)*nlstate + i] = (p3matm[i][j][h]+p3matm[i][j][h+1])/2.;
6701: }
6702: }
6703: }
6704:
6705: for(ij=1; ij<= nlstate*nlstate; ij++)
6706: for(h=0; h<=nhstepm-1; h++){
6707: gradg[h][theta][ij]= (gp[h][ij]-gm[h][ij])/2./delti[theta];
6708: }
6709: }/* End theta */
6710:
6711:
6712: for(h=0; h<=nhstepm-1; h++)
6713: for(j=1; j<=nlstate*nlstate;j++)
6714: for(theta=1; theta <=npar; theta++)
6715: trgradg[h][j][theta]=gradg[h][theta][j];
6716:
6717:
6718: for(ij=1;ij<=nlstate*nlstate;ij++)
6719: for(ji=1;ji<=nlstate*nlstate;ji++)
6720: varhe[ij][ji][(int)age] =0.;
6721:
6722: printf("%d|",(int)age);fflush(stdout);
6723: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
6724: for(h=0;h<=nhstepm-1;h++){
6725: for(k=0;k<=nhstepm-1;k++){
6726: matprod2(dnewm,trgradg[h],1,nlstate*nlstate,1,npar,1,npar,matcov);
6727: matprod2(doldm,dnewm,1,nlstate*nlstate,1,npar,1,nlstate*nlstate,gradg[k]);
6728: for(ij=1;ij<=nlstate*nlstate;ij++)
6729: for(ji=1;ji<=nlstate*nlstate;ji++)
6730: varhe[ij][ji][(int)age] += doldm[ij][ji]*hf*hf;
6731: }
6732: }
6733: /* if((int)age ==50){ */
6734: /* printf(" age=%d cij=%d nres=%d varhe[%d][%d]=%f ",(int)age, cij, nres, 1,2,varhe[1][2]); */
6735: /* } */
6736: /* Computing expectancies */
6737: hpxij(p3matm,nhstepm,age,hstepm,x,nlstate,stepm,oldm, savm, cij,nres);
6738: for(i=1; i<=nlstate;i++)
6739: for(j=1; j<=nlstate;j++)
6740: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
6741: eij[i][j][(int)age] += (p3matm[i][j][h]+p3matm[i][j][h+1])/2.0*hf;
6742:
6743: /* 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]);*/
6744:
6745: }
6746:
6747: /* Standard deviation of expectancies ij */
6748: fprintf(ficresstdeij,"%3.0f",age );
6749: for(i=1; i<=nlstate;i++){
6750: eip=0.;
6751: vip=0.;
6752: for(j=1; j<=nlstate;j++){
6753: eip += eij[i][j][(int)age];
6754: for(k=1; k<=nlstate;k++) /* Sum on j and k of cov(eij,eik) */
6755: vip += varhe[(j-1)*nlstate+i][(k-1)*nlstate+i][(int)age];
6756: fprintf(ficresstdeij," %9.4f (%.4f)", eij[i][j][(int)age], sqrt(varhe[(j-1)*nlstate+i][(j-1)*nlstate+i][(int)age]) );
6757: }
6758: fprintf(ficresstdeij," %9.4f (%.4f)", eip, sqrt(vip));
6759: }
6760: fprintf(ficresstdeij,"\n");
6761:
6762: /* Variance of expectancies ij */
6763: fprintf(ficrescveij,"%3.0f",age );
6764: for(i=1; i<=nlstate;i++)
6765: for(j=1; j<=nlstate;j++){
6766: cptj= (j-1)*nlstate+i;
6767: for(i2=1; i2<=nlstate;i2++)
6768: for(j2=1; j2<=nlstate;j2++){
6769: cptj2= (j2-1)*nlstate+i2;
6770: if(cptj2 <= cptj)
6771: fprintf(ficrescveij," %.4f", varhe[cptj][cptj2][(int)age]);
6772: }
6773: }
6774: fprintf(ficrescveij,"\n");
6775:
6776: }
6777: free_matrix(gm,0,nhstepm,1,nlstate*nlstate);
6778: free_matrix(gp,0,nhstepm,1,nlstate*nlstate);
6779: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate*nlstate);
6780: free_ma3x(trgradg,0,nhstepm,1,nlstate*nlstate,1,npar);
6781: free_ma3x(p3matm,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6782: free_ma3x(p3matp,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6783: printf("\n");
6784: fprintf(ficlog,"\n");
6785:
6786: free_vector(xm,1,npar);
6787: free_vector(xp,1,npar);
6788: free_matrix(dnewm,1,nlstate*nlstate,1,npar);
6789: free_matrix(doldm,1,nlstate*nlstate,1,nlstate*nlstate);
6790: free_ma3x(varhe,1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int)fage);
6791: }
6792:
6793: /************ Variance ******************/
6794: 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)
6795: {
6796: /** Variance of health expectancies
6797: * double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);
6798: * double **newm;
6799: * int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav)
6800: */
6801:
6802: /* int movingaverage(); */
6803: double **dnewm,**doldm;
6804: double **dnewmp,**doldmp;
6805: int i, j, nhstepm, hstepm, h, nstepm ;
6806: int first=0;
6807: int k;
6808: double *xp;
6809: double **gp, **gm; /**< for var eij */
6810: double ***gradg, ***trgradg; /**< for var eij */
6811: double **gradgp, **trgradgp; /**< for var p point j */
6812: double *gpp, *gmp; /**< for var p point j */
6813: double **varppt; /**< for var p point j nlstate to nlstate+ndeath */
6814: double ***p3mat;
6815: double age,agelim, hf;
6816: /* double ***mobaverage; */
6817: int theta;
6818: char digit[4];
6819: char digitp[25];
6820:
6821: char fileresprobmorprev[FILENAMELENGTH];
6822:
6823: if(popbased==1){
6824: if(mobilav!=0)
6825: strcpy(digitp,"-POPULBASED-MOBILAV_");
6826: else strcpy(digitp,"-POPULBASED-NOMOBIL_");
6827: }
6828: else
6829: strcpy(digitp,"-STABLBASED_");
6830:
6831: /* if (mobilav!=0) { */
6832: /* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
6833: /* if (movingaverage(probs, bage, fage, mobaverage,mobilav)!=0){ */
6834: /* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); */
6835: /* printf(" Error in movingaverage mobilav=%d\n",mobilav); */
6836: /* } */
6837: /* } */
6838:
6839: strcpy(fileresprobmorprev,"PRMORPREV-");
6840: sprintf(digit,"%-d",ij);
6841: /*printf("DIGIT=%s, ij=%d ijr=%-d|\n",digit, ij,ij);*/
6842: strcat(fileresprobmorprev,digit); /* Tvar to be done */
6843: strcat(fileresprobmorprev,digitp); /* Popbased or not, mobilav or not */
6844: strcat(fileresprobmorprev,fileresu);
6845: if((ficresprobmorprev=fopen(fileresprobmorprev,"w"))==NULL) {
6846: printf("Problem with resultfile: %s\n", fileresprobmorprev);
6847: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobmorprev);
6848: }
6849: printf("Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
6850: fprintf(ficlog,"Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
6851: pstamp(ficresprobmorprev);
6852: 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);
6853: fprintf(ficresprobmorprev,"# Selected quantitative variables and dummies");
6854:
6855: /* We use TinvDoQresult[nres][resultmodel[nres][j] we sort according to the equation model and the resultline: it is a choice */
6856: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ /\* To be done*\/ */
6857: /* fprintf(ficresprobmorprev," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
6858: /* } */
6859: for (j=1; j<= cptcovs; j++){ /* For each selected (single) quantitative value */ /* To be done*/
6860: /* fprintf(ficresprobmorprev," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]); */
6861: fprintf(ficresprobmorprev," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
6862: }
6863: /* for(j=1;j<=cptcoveff;j++) */
6864: /* fprintf(ficresprobmorprev," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(ij,TnsdVar[Tvaraff[j]])]); */
6865: fprintf(ficresprobmorprev,"\n");
6866:
6867: fprintf(ficresprobmorprev,"# Age cov=%-d",ij);
6868: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
6869: fprintf(ficresprobmorprev," p.%-d SE",j);
6870: for(i=1; i<=nlstate;i++)
6871: fprintf(ficresprobmorprev," w%1d p%-d%-d",i,i,j);
6872: }
6873: fprintf(ficresprobmorprev,"\n");
6874:
6875: fprintf(ficgp,"\n# Routine varevsij");
6876: fprintf(ficgp,"\nunset title \n");
6877: /* fprintf(fichtm, "#Local time at start: %s", strstart);*/
6878: 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");
6879: fprintf(fichtm,"\n<br>%s <br>\n",digitp);
6880:
6881: varppt = matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
6882: pstamp(ficresvij);
6883: fprintf(ficresvij,"# Variance and covariance of health expectancies e.j \n# (weighted average of eij where weights are ");
6884: if(popbased==1)
6885: 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);
6886: else
6887: fprintf(ficresvij,"the age specific period (stable) prevalences in each health state \n");
6888: fprintf(ficresvij,"# Age");
6889: for(i=1; i<=nlstate;i++)
6890: for(j=1; j<=nlstate;j++)
6891: fprintf(ficresvij," Cov(e.%1d, e.%1d)",i,j);
6892: fprintf(ficresvij,"\n");
6893:
6894: xp=vector(1,npar);
6895: dnewm=matrix(1,nlstate,1,npar);
6896: doldm=matrix(1,nlstate,1,nlstate);
6897: dnewmp= matrix(nlstate+1,nlstate+ndeath,1,npar);
6898: doldmp= matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
6899:
6900: gradgp=matrix(1,npar,nlstate+1,nlstate+ndeath);
6901: gpp=vector(nlstate+1,nlstate+ndeath);
6902: gmp=vector(nlstate+1,nlstate+ndeath);
6903: trgradgp =matrix(nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
6904:
6905: if(estepm < stepm){
6906: printf ("Problem %d lower than %d\n",estepm, stepm);
6907: }
6908: else hstepm=estepm;
6909: /* For example we decided to compute the life expectancy with the smallest unit */
6910: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
6911: nhstepm is the number of hstepm from age to agelim
6912: nstepm is the number of stepm from age to agelim.
6913: Look at function hpijx to understand why because of memory size limitations,
6914: we decided (b) to get a life expectancy respecting the most precise curvature of the
6915: survival function given by stepm (the optimization length). Unfortunately it
6916: means that if the survival funtion is printed every two years of age and if
6917: you sum them up and add 1 year (area under the trapezoids) you won't get the same
6918: results. So we changed our mind and took the option of the best precision.
6919: */
6920: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
6921: agelim = AGESUP;
6922: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
6923: nstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
6924: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
6925: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6926: gradg=ma3x(0,nhstepm,1,npar,1,nlstate);
6927: gp=matrix(0,nhstepm,1,nlstate);
6928: gm=matrix(0,nhstepm,1,nlstate);
6929:
6930:
6931: for(theta=1; theta <=npar; theta++){
6932: for(i=1; i<=npar; i++){ /* Computes gradient x + delta*/
6933: xp[i] = x[i] + (i==theta ?delti[theta]:0);
6934: }
6935: /**< Computes the prevalence limit with parameter theta shifted of delta up to ftolpl precision and
6936: * returns into prlim .
6937: */
6938: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij, nres);
6939:
6940: /* If popbased = 1 we use crossection prevalences. Previous step is useless but prlim is created */
6941: if (popbased==1) {
6942: if(mobilav ==0){
6943: for(i=1; i<=nlstate;i++)
6944: prlim[i][i]=probs[(int)age][i][ij];
6945: }else{ /* mobilav */
6946: for(i=1; i<=nlstate;i++)
6947: prlim[i][i]=mobaverage[(int)age][i][ij];
6948: }
6949: }
6950: /**< Computes the shifted transition matrix \f$ {}{h}_p^{ij}x\f$ at horizon h.
6951: */
6952: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij,nres); /* Returns p3mat[i][j][h] for h=0 to nhstepm */
6953: /**< And for each alive state j, sums over i \f$ w^i_x {}{h}_p^{ij}x\f$, which are the probability
6954: * at horizon h in state j including mortality.
6955: */
6956: for(j=1; j<= nlstate; j++){
6957: for(h=0; h<=nhstepm; h++){
6958: for(i=1, gp[h][j]=0.;i<=nlstate;i++)
6959: gp[h][j] += prlim[i][i]*p3mat[i][j][h];
6960: }
6961: }
6962: /* Next for computing shifted+ probability of death (h=1 means
6963: computed over hstepm matrices product = hstepm*stepm months)
6964: as a weighted average of prlim(i) * p(i,j) p.3=w1*p13 + w2*p23 .
6965: */
6966: for(j=nlstate+1;j<=nlstate+ndeath;j++){
6967: for(i=1,gpp[j]=0.; i<= nlstate; i++)
6968: gpp[j] += prlim[i][i]*p3mat[i][j][1];
6969: }
6970:
6971: /* Again with minus shift */
6972:
6973: for(i=1; i<=npar; i++) /* Computes gradient x - delta */
6974: xp[i] = x[i] - (i==theta ?delti[theta]:0);
6975:
6976: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp, ij, nres);
6977:
6978: if (popbased==1) {
6979: if(mobilav ==0){
6980: for(i=1; i<=nlstate;i++)
6981: prlim[i][i]=probs[(int)age][i][ij];
6982: }else{ /* mobilav */
6983: for(i=1; i<=nlstate;i++)
6984: prlim[i][i]=mobaverage[(int)age][i][ij];
6985: }
6986: }
6987:
6988: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij,nres);
6989:
6990: for(j=1; j<= nlstate; j++){ /* Sum of wi * eij = e.j */
6991: for(h=0; h<=nhstepm; h++){
6992: for(i=1, gm[h][j]=0.;i<=nlstate;i++)
6993: gm[h][j] += prlim[i][i]*p3mat[i][j][h];
6994: }
6995: }
6996: /* This for computing probability of death (h=1 means
6997: computed over hstepm matrices product = hstepm*stepm months)
6998: as a weighted average of prlim.
6999: */
7000: for(j=nlstate+1;j<=nlstate+ndeath;j++){
7001: for(i=1,gmp[j]=0.; i<= nlstate; i++)
7002: gmp[j] += prlim[i][i]*p3mat[i][j][1];
7003: }
7004: /* end shifting computations */
7005:
7006: /**< Computing gradient matrix at horizon h
7007: */
7008: for(j=1; j<= nlstate; j++) /* vareij */
7009: for(h=0; h<=nhstepm; h++){
7010: gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta];
7011: }
7012: /**< Gradient of overall mortality p.3 (or p.j)
7013: */
7014: for(j=nlstate+1; j<= nlstate+ndeath; j++){ /* var mu mortality from j */
7015: gradgp[theta][j]= (gpp[j]-gmp[j])/2./delti[theta];
7016: }
7017:
7018: } /* End theta */
7019:
7020: /* We got the gradient matrix for each theta and state j */
7021: trgradg =ma3x(0,nhstepm,1,nlstate,1,npar); /* veij */
7022:
7023: for(h=0; h<=nhstepm; h++) /* veij */
7024: for(j=1; j<=nlstate;j++)
7025: for(theta=1; theta <=npar; theta++)
7026: trgradg[h][j][theta]=gradg[h][theta][j];
7027:
7028: for(j=nlstate+1; j<=nlstate+ndeath;j++) /* mu */
7029: for(theta=1; theta <=npar; theta++)
7030: trgradgp[j][theta]=gradgp[theta][j];
7031: /**< as well as its transposed matrix
7032: */
7033:
7034: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
7035: for(i=1;i<=nlstate;i++)
7036: for(j=1;j<=nlstate;j++)
7037: vareij[i][j][(int)age] =0.;
7038:
7039: /* Computing trgradg by matcov by gradg at age and summing over h
7040: * and k (nhstepm) formula 15 of article
7041: * Lievre-Brouard-Heathcote
7042: */
7043:
7044: for(h=0;h<=nhstepm;h++){
7045: for(k=0;k<=nhstepm;k++){
7046: matprod2(dnewm,trgradg[h],1,nlstate,1,npar,1,npar,matcov);
7047: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg[k]);
7048: for(i=1;i<=nlstate;i++)
7049: for(j=1;j<=nlstate;j++)
7050: vareij[i][j][(int)age] += doldm[i][j]*hf*hf;
7051: }
7052: }
7053:
7054: /* pptj is p.3 or p.j = trgradgp by cov by gradgp, variance of
7055: * p.j overall mortality formula 49 but computed directly because
7056: * we compute the grad (wix pijx) instead of grad (pijx),even if
7057: * wix is independent of theta.
7058: */
7059: matprod2(dnewmp,trgradgp,nlstate+1,nlstate+ndeath,1,npar,1,npar,matcov);
7060: matprod2(doldmp,dnewmp,nlstate+1,nlstate+ndeath,1,npar,nlstate+1,nlstate+ndeath,gradgp);
7061: for(j=nlstate+1;j<=nlstate+ndeath;j++)
7062: for(i=nlstate+1;i<=nlstate+ndeath;i++)
7063: varppt[j][i]=doldmp[j][i];
7064: /* end ppptj */
7065: /* x centered again */
7066:
7067: prevalim(prlim,nlstate,x,age,oldm,savm,ftolpl,ncvyearp,ij, nres);
7068:
7069: if (popbased==1) {
7070: if(mobilav ==0){
7071: for(i=1; i<=nlstate;i++)
7072: prlim[i][i]=probs[(int)age][i][ij];
7073: }else{ /* mobilav */
7074: for(i=1; i<=nlstate;i++)
7075: prlim[i][i]=mobaverage[(int)age][i][ij];
7076: }
7077: }
7078:
7079: /* This for computing probability of death (h=1 means
7080: computed over hstepm (estepm) matrices product = hstepm*stepm months)
7081: as a weighted average of prlim.
7082: */
7083: hpxij(p3mat,nhstepm,age,hstepm,x,nlstate,stepm,oldm,savm, ij, nres);
7084: for(j=nlstate+1;j<=nlstate+ndeath;j++){
7085: for(i=1,gmp[j]=0.;i<= nlstate; i++)
7086: gmp[j] += prlim[i][i]*p3mat[i][j][1];
7087: }
7088: /* end probability of death */
7089:
7090: fprintf(ficresprobmorprev,"%3d %d ",(int) age, ij);
7091: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
7092: fprintf(ficresprobmorprev," %11.3e %11.3e",gmp[j], sqrt(varppt[j][j]));
7093: for(i=1; i<=nlstate;i++){
7094: fprintf(ficresprobmorprev," %11.3e %11.3e ",prlim[i][i],p3mat[i][j][1]);
7095: }
7096: }
7097: fprintf(ficresprobmorprev,"\n");
7098:
7099: fprintf(ficresvij,"%.0f ",age );
7100: for(i=1; i<=nlstate;i++)
7101: for(j=1; j<=nlstate;j++){
7102: fprintf(ficresvij," %.4f", vareij[i][j][(int)age]);
7103: }
7104: fprintf(ficresvij,"\n");
7105: free_matrix(gp,0,nhstepm,1,nlstate);
7106: free_matrix(gm,0,nhstepm,1,nlstate);
7107: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate);
7108: free_ma3x(trgradg,0,nhstepm,1,nlstate,1,npar);
7109: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
7110: } /* End age */
7111: free_vector(gpp,nlstate+1,nlstate+ndeath);
7112: free_vector(gmp,nlstate+1,nlstate+ndeath);
7113: free_matrix(gradgp,1,npar,nlstate+1,nlstate+ndeath);
7114: free_matrix(trgradgp,nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
7115: /* fprintf(ficgp,"\nunset parametric;unset label; set ter png small size 320, 240"); */
7116: fprintf(ficgp,"\nunset parametric;unset label; set ter svg size 640, 480");
7117: /* for(j=nlstate+1; j<= nlstate+ndeath; j++){ *//* Only the first actually */
7118: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Force of mortality (year-1)\";");
7119: fprintf(ficgp,"\nset out \"%s%s.svg\";",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
7120: /* fprintf(ficgp,"\n plot \"%s\" u 1:($3*%6.3f) not w l 1 ",fileresprobmorprev,YEARM/estepm); */
7121: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)*%6.3f) t \"95\%% interval\" w l 2 ",fileresprobmorprev,YEARM/estepm); */
7122: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)*%6.3f) not w l 2 ",fileresprobmorprev,YEARM/estepm); */
7123: fprintf(ficgp,"\n plot \"%s\" u 1:($3) not w l lt 1 ",subdirf(fileresprobmorprev));
7124: fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)) t \"95%% interval\" w l lt 2 ",subdirf(fileresprobmorprev));
7125: fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)) not w l lt 2 ",subdirf(fileresprobmorprev));
7126: fprintf(fichtm,"\n<br> File (multiple files are possible if covariates are present): <A href=\"%s\">%s</a>\n",subdirf(fileresprobmorprev),subdirf(fileresprobmorprev));
7127: 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);
7128: /* 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);
7129: */
7130: /* fprintf(ficgp,"\nset out \"varmuptjgr%s%s%s.svg\";replot;",digitp,optionfilefiname,digit); */
7131: fprintf(ficgp,"\nset out;\nset out \"%s%s.svg\";replot;set out;\n",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
7132:
7133: free_vector(xp,1,npar);
7134: free_matrix(doldm,1,nlstate,1,nlstate);
7135: free_matrix(dnewm,1,nlstate,1,npar);
7136: free_matrix(doldmp,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
7137: free_matrix(dnewmp,nlstate+1,nlstate+ndeath,1,npar);
7138: free_matrix(varppt,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
7139: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
7140: fclose(ficresprobmorprev);
7141: fflush(ficgp);
7142: fflush(fichtm);
7143: } /* end varevsij */
7144:
7145: /************ Variance of prevlim ******************/
7146: 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)
7147: {
7148: /* Variance of prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/
7149: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
7150:
7151: double **dnewmpar,**doldm;
7152: int i, j, nhstepm, hstepm;
7153: double *xp;
7154: double *gp, *gm;
7155: double **gradg, **trgradg;
7156: double **mgm, **mgp;
7157: double age,agelim;
7158: int theta;
7159:
7160: pstamp(ficresvpl);
7161: fprintf(ficresvpl,"# Standard deviation of period (forward stable) prevalences \n");
7162: fprintf(ficresvpl,"# Age ");
7163: if(nresult >=1)
7164: fprintf(ficresvpl," Result# ");
7165: for(i=1; i<=nlstate;i++)
7166: fprintf(ficresvpl," %1d-%1d",i,i);
7167: fprintf(ficresvpl,"\n");
7168:
7169: xp=vector(1,npar);
7170: dnewmpar=matrix(1,nlstate,1,npar);
7171: doldm=matrix(1,nlstate,1,nlstate);
7172:
7173: hstepm=1*YEARM; /* Every year of age */
7174: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
7175: agelim = AGESUP;
7176: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
7177: nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
7178: if (stepm >= YEARM) hstepm=1;
7179: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
7180: gradg=matrix(1,npar,1,nlstate);
7181: mgp=matrix(1,npar,1,nlstate);
7182: mgm=matrix(1,npar,1,nlstate);
7183: gp=vector(1,nlstate);
7184: gm=vector(1,nlstate);
7185:
7186: for(theta=1; theta <=npar; theta++){
7187: for(i=1; i<=npar; i++){ /* Computes gradient */
7188: xp[i] = x[i] + (i==theta ?delti[theta]:0);
7189: }
7190: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ) */
7191: /* prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres); */
7192: /* else */
7193: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
7194: for(i=1;i<=nlstate;i++){
7195: gp[i] = prlim[i][i];
7196: mgp[theta][i] = prlim[i][i];
7197: }
7198: for(i=1; i<=npar; i++) /* Computes gradient */
7199: xp[i] = x[i] - (i==theta ?delti[theta]:0);
7200: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ) */
7201: /* prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres); */
7202: /* else */
7203: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
7204: for(i=1;i<=nlstate;i++){
7205: gm[i] = prlim[i][i];
7206: mgm[theta][i] = prlim[i][i];
7207: }
7208: for(i=1;i<=nlstate;i++)
7209: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
7210: /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */
7211: } /* End theta */
7212:
7213: trgradg =matrix(1,nlstate,1,npar);
7214:
7215: for(j=1; j<=nlstate;j++)
7216: for(theta=1; theta <=npar; theta++)
7217: trgradg[j][theta]=gradg[theta][j];
7218: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
7219: /* printf("\nmgm mgp %d ",(int)age); */
7220: /* for(j=1; j<=nlstate;j++){ */
7221: /* printf(" %d ",j); */
7222: /* for(theta=1; theta <=npar; theta++) */
7223: /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */
7224: /* printf("\n "); */
7225: /* } */
7226: /* } */
7227: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
7228: /* printf("\n gradg %d ",(int)age); */
7229: /* for(j=1; j<=nlstate;j++){ */
7230: /* printf("%d ",j); */
7231: /* for(theta=1; theta <=npar; theta++) */
7232: /* printf("%d %lf ",theta,gradg[theta][j]); */
7233: /* printf("\n "); */
7234: /* } */
7235: /* } */
7236:
7237: for(i=1;i<=nlstate;i++)
7238: varpl[i][(int)age] =0.;
7239: if((int)age==79 ||(int)age== 80 ||(int)age== 81){
7240: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
7241: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
7242: }else{
7243: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
7244: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
7245: }
7246: for(i=1;i<=nlstate;i++)
7247: varpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
7248:
7249: fprintf(ficresvpl,"%.0f ",age );
7250: if(nresult >=1)
7251: fprintf(ficresvpl,"%d ",nres );
7252: for(i=1; i<=nlstate;i++){
7253: fprintf(ficresvpl," %.5f (%.5f)",prlim[i][i],sqrt(varpl[i][(int)age]));
7254: /* for(j=1;j<=nlstate;j++) */
7255: /* fprintf(ficresvpl," %d %.5f ",j,prlim[j][i]); */
7256: }
7257: fprintf(ficresvpl,"\n");
7258: free_vector(gp,1,nlstate);
7259: free_vector(gm,1,nlstate);
7260: free_matrix(mgm,1,npar,1,nlstate);
7261: free_matrix(mgp,1,npar,1,nlstate);
7262: free_matrix(gradg,1,npar,1,nlstate);
7263: free_matrix(trgradg,1,nlstate,1,npar);
7264: } /* End age */
7265:
7266: free_vector(xp,1,npar);
7267: free_matrix(doldm,1,nlstate,1,npar);
7268: free_matrix(dnewmpar,1,nlstate,1,nlstate);
7269:
7270: }
7271:
7272:
7273: /************ Variance of backprevalence limit ******************/
7274: 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)
7275: {
7276: /* Variance of backward prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/
7277: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
7278:
7279: double **dnewmpar,**doldm;
7280: int i, j, nhstepm, hstepm;
7281: double *xp;
7282: double *gp, *gm;
7283: double **gradg, **trgradg;
7284: double **mgm, **mgp;
7285: double age,agelim;
7286: int theta;
7287:
7288: pstamp(ficresvbl);
7289: fprintf(ficresvbl,"# Standard deviation of back (stable) prevalences \n");
7290: fprintf(ficresvbl,"# Age ");
7291: if(nresult >=1)
7292: fprintf(ficresvbl," Result# ");
7293: for(i=1; i<=nlstate;i++)
7294: fprintf(ficresvbl," %1d-%1d",i,i);
7295: fprintf(ficresvbl,"\n");
7296:
7297: xp=vector(1,npar);
7298: dnewmpar=matrix(1,nlstate,1,npar);
7299: doldm=matrix(1,nlstate,1,nlstate);
7300:
7301: hstepm=1*YEARM; /* Every year of age */
7302: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
7303: agelim = AGEINF;
7304: for (age=fage; age>=bage; age --){ /* If stepm=6 months */
7305: nhstepm=(int) rint((age-agelim)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
7306: if (stepm >= YEARM) hstepm=1;
7307: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
7308: gradg=matrix(1,npar,1,nlstate);
7309: mgp=matrix(1,npar,1,nlstate);
7310: mgm=matrix(1,npar,1,nlstate);
7311: gp=vector(1,nlstate);
7312: gm=vector(1,nlstate);
7313:
7314: for(theta=1; theta <=npar; theta++){
7315: for(i=1; i<=npar; i++){ /* Computes gradient */
7316: xp[i] = x[i] + (i==theta ?delti[theta]:0);
7317: }
7318: if(mobilavproj > 0 )
7319: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
7320: else
7321: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
7322: for(i=1;i<=nlstate;i++){
7323: gp[i] = bprlim[i][i];
7324: mgp[theta][i] = bprlim[i][i];
7325: }
7326: for(i=1; i<=npar; i++) /* Computes gradient */
7327: xp[i] = x[i] - (i==theta ?delti[theta]:0);
7328: if(mobilavproj > 0 )
7329: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
7330: else
7331: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
7332: for(i=1;i<=nlstate;i++){
7333: gm[i] = bprlim[i][i];
7334: mgm[theta][i] = bprlim[i][i];
7335: }
7336: for(i=1;i<=nlstate;i++)
7337: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
7338: /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */
7339: } /* End theta */
7340:
7341: trgradg =matrix(1,nlstate,1,npar);
7342:
7343: for(j=1; j<=nlstate;j++)
7344: for(theta=1; theta <=npar; theta++)
7345: trgradg[j][theta]=gradg[theta][j];
7346: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
7347: /* printf("\nmgm mgp %d ",(int)age); */
7348: /* for(j=1; j<=nlstate;j++){ */
7349: /* printf(" %d ",j); */
7350: /* for(theta=1; theta <=npar; theta++) */
7351: /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */
7352: /* printf("\n "); */
7353: /* } */
7354: /* } */
7355: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
7356: /* printf("\n gradg %d ",(int)age); */
7357: /* for(j=1; j<=nlstate;j++){ */
7358: /* printf("%d ",j); */
7359: /* for(theta=1; theta <=npar; theta++) */
7360: /* printf("%d %lf ",theta,gradg[theta][j]); */
7361: /* printf("\n "); */
7362: /* } */
7363: /* } */
7364:
7365: for(i=1;i<=nlstate;i++)
7366: varbpl[i][(int)age] =0.;
7367: if((int)age==79 ||(int)age== 80 ||(int)age== 81){
7368: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
7369: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
7370: }else{
7371: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
7372: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
7373: }
7374: for(i=1;i<=nlstate;i++)
7375: varbpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
7376:
7377: fprintf(ficresvbl,"%.0f ",age );
7378: if(nresult >=1)
7379: fprintf(ficresvbl,"%d ",nres );
7380: for(i=1; i<=nlstate;i++)
7381: fprintf(ficresvbl," %.5f (%.5f)",bprlim[i][i],sqrt(varbpl[i][(int)age]));
7382: fprintf(ficresvbl,"\n");
7383: free_vector(gp,1,nlstate);
7384: free_vector(gm,1,nlstate);
7385: free_matrix(mgm,1,npar,1,nlstate);
7386: free_matrix(mgp,1,npar,1,nlstate);
7387: free_matrix(gradg,1,npar,1,nlstate);
7388: free_matrix(trgradg,1,nlstate,1,npar);
7389: } /* End age */
7390:
7391: free_vector(xp,1,npar);
7392: free_matrix(doldm,1,nlstate,1,npar);
7393: free_matrix(dnewmpar,1,nlstate,1,nlstate);
7394:
7395: }
7396:
7397: /************ Variance of one-step probabilities ******************/
7398: 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[])
7399: {
7400: int i, j=0, k1, l1, tj;
7401: int k2, l2, j1, z1;
7402: int k=0, l;
7403: int first=1, first1, first2;
7404: int nres=0; /* New */
7405: double cv12, mu1, mu2, lc1, lc2, v12, v21, v11, v22,v1,v2, c12, tnalp;
7406: double **dnewm,**doldm;
7407: double *xp;
7408: double *gp, *gm;
7409: double **gradg, **trgradg;
7410: double **mu;
7411: double age, cov[NCOVMAX+1];
7412: double std=2.0; /* Number of standard deviation wide of confidence ellipsoids */
7413: int theta;
7414: char fileresprob[FILENAMELENGTH];
7415: char fileresprobcov[FILENAMELENGTH];
7416: char fileresprobcor[FILENAMELENGTH];
7417: double ***varpij;
7418:
7419: strcpy(fileresprob,"PROB_");
7420: strcat(fileresprob,fileres);
7421: if((ficresprob=fopen(fileresprob,"w"))==NULL) {
7422: printf("Problem with resultfile: %s\n", fileresprob);
7423: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprob);
7424: }
7425: strcpy(fileresprobcov,"PROBCOV_");
7426: strcat(fileresprobcov,fileresu);
7427: if((ficresprobcov=fopen(fileresprobcov,"w"))==NULL) {
7428: printf("Problem with resultfile: %s\n", fileresprobcov);
7429: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcov);
7430: }
7431: strcpy(fileresprobcor,"PROBCOR_");
7432: strcat(fileresprobcor,fileresu);
7433: if((ficresprobcor=fopen(fileresprobcor,"w"))==NULL) {
7434: printf("Problem with resultfile: %s\n", fileresprobcor);
7435: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcor);
7436: }
7437: printf("Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
7438: fprintf(ficlog,"Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
7439: printf("Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
7440: fprintf(ficlog,"Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
7441: printf("and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
7442: fprintf(ficlog,"and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
7443: pstamp(ficresprob);
7444: fprintf(ficresprob,"#One-step probabilities and stand. devi in ()\n");
7445: fprintf(ficresprob,"# Age");
7446: pstamp(ficresprobcov);
7447: fprintf(ficresprobcov,"#One-step probabilities and covariance matrix\n");
7448: fprintf(ficresprobcov,"# Age");
7449: pstamp(ficresprobcor);
7450: fprintf(ficresprobcor,"#One-step probabilities and correlation matrix\n");
7451: fprintf(ficresprobcor,"# Age");
7452:
7453:
7454: for(i=1; i<=nlstate;i++)
7455: for(j=1; j<=(nlstate+ndeath);j++){
7456: fprintf(ficresprob," p%1d-%1d (SE)",i,j);
7457: fprintf(ficresprobcov," p%1d-%1d ",i,j);
7458: fprintf(ficresprobcor," p%1d-%1d ",i,j);
7459: }
7460: /* fprintf(ficresprob,"\n");
7461: fprintf(ficresprobcov,"\n");
7462: fprintf(ficresprobcor,"\n");
7463: */
7464: xp=vector(1,npar);
7465: dnewm=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
7466: doldm=matrix(1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
7467: mu=matrix(1,(nlstate)*(nlstate+ndeath), (int) bage, (int)fage);
7468: varpij=ma3x(1,nlstate*(nlstate+ndeath),1,nlstate*(nlstate+ndeath),(int) bage, (int) fage);
7469: first=1;
7470: fprintf(ficgp,"\n# Routine varprob");
7471: fprintf(fichtm,"\n<li><h4> Computing and drawing one step probabilities with their confidence intervals</h4></li>\n");
7472: fprintf(fichtm,"\n");
7473:
7474: 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);
7475: 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);
7476: fprintf(fichtmcov,"\nEllipsoids of confidence centered on point (p<inf>ij</inf>, p<inf>kl</inf>) are estimated \
7477: and drawn. It helps understanding how is the covariance between two incidences.\
7478: They are expressed in year<sup>-1</sup> in order to be less dependent of stepm.<br>\n");
7479: 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. \
7480: It can be understood this way: if pij and pkl where uncorrelated the (2x2) matrix of covariance \
7481: would have been (1/(var pij), 0 , 0, 1/(var pkl)), and the confidence interval would be 2 \
7482: standard deviations wide on each axis. <br>\
7483: Now, if both incidences are correlated (usual case) we diagonalised the inverse of the covariance matrix\
7484: and made the appropriate rotation to look at the uncorrelated principal directions.<br>\
7485: To be simple, these graphs help to understand the significativity of each parameter in relation to a second other one.<br> \n");
7486:
7487: cov[1]=1;
7488: /* tj=cptcoveff; */
7489: tj = (int) pow(2,cptcoveff);
7490: if (cptcovn<1) {tj=1;ncodemax[1]=1;}
7491: j1=0;
7492:
7493: for(nres=1;nres <=nresult; nres++){ /* For each resultline */
7494: for(j1=1; j1<=tj;j1++){ /* For any combination of dummy covariates, fixed and varying */
7495: /* 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); */
7496: if(tj != 1 && TKresult[nres]!= j1)
7497: continue;
7498:
7499: /* for(j1=1; j1<=tj;j1++){ /\* For each valid combination of covariates or only once*\/ */
7500: /* for(nres=1;nres <=1; nres++){ /\* For each resultline *\/ */
7501: /* /\* for(nres=1;nres <=nresult; nres++){ /\\* For each resultline *\\/ *\/ */
7502: if (cptcovn>0) {
7503: fprintf(ficresprob, "\n#********** Variable ");
7504: fprintf(ficresprobcov, "\n#********** Variable ");
7505: fprintf(ficgp, "\n#********** Variable ");
7506: fprintf(fichtmcov, "\n<hr size=\"2\" color=\"#EC5E5E\">********** Variable ");
7507: fprintf(ficresprobcor, "\n#********** Variable ");
7508:
7509: /* Including quantitative variables of the resultline to be done */
7510: for (z1=1; z1<=cptcovs; z1++){ /* Loop on each variable of this resultline */
7511: /* printf("Varprob modelresult[%d][%d]=%d model=1+age+%s \n",nres, z1, modelresult[nres][z1], model); */
7512: fprintf(ficlog,"Varprob modelresult[%d][%d]=%d model=1+age+%s \n",nres, z1, modelresult[nres][z1], model);
7513: /* fprintf(ficlog,"Varprob modelresult[%d][%d]=%d model=1+age+%s resultline[%d]=%s \n",nres, z1, modelresult[nres][z1], model, nres, resultline[nres]); */
7514: if(Dummy[modelresult[nres][z1]]==0){/* Dummy variable of the variable in position modelresult in the model corresponding to z1 in resultline */
7515: if(Fixed[modelresult[nres][z1]]==0){ /* Fixed referenced to model equation */
7516: 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 */
7517: 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 */
7518: 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 */
7519: 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 */
7520: 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 */
7521: fprintf(ficresprob,"fixed ");
7522: fprintf(ficresprobcov,"fixed ");
7523: fprintf(ficgp,"fixed ");
7524: fprintf(fichtmcov,"fixed ");
7525: fprintf(ficresprobcor,"fixed ");
7526: }else{
7527: fprintf(ficresprob,"varyi ");
7528: fprintf(ficresprobcov,"varyi ");
7529: fprintf(ficgp,"varyi ");
7530: fprintf(fichtmcov,"varyi ");
7531: fprintf(ficresprobcor,"varyi ");
7532: }
7533: }else if(Dummy[modelresult[nres][z1]]==1){ /* Quanti variable */
7534: /* For each selected (single) quantitative value */
7535: fprintf(ficresprob," V%d=%lg ",Tvqresult[nres][z1],Tqresult[nres][z1]);
7536: if(Fixed[modelresult[nres][z1]]==0){ /* Fixed */
7537: fprintf(ficresprob,"fixed ");
7538: fprintf(ficresprobcov,"fixed ");
7539: fprintf(ficgp,"fixed ");
7540: fprintf(fichtmcov,"fixed ");
7541: fprintf(ficresprobcor,"fixed ");
7542: }else{
7543: fprintf(ficresprob,"varyi ");
7544: fprintf(ficresprobcov,"varyi ");
7545: fprintf(ficgp,"varyi ");
7546: fprintf(fichtmcov,"varyi ");
7547: fprintf(ficresprobcor,"varyi ");
7548: }
7549: }else{
7550: 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 */
7551: 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 */
7552: exit(1);
7553: }
7554: } /* End loop on variable of this resultline */
7555: /* for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprob, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]); */
7556: fprintf(ficresprob, "**********\n#\n");
7557: fprintf(ficresprobcov, "**********\n#\n");
7558: fprintf(ficgp, "**********\n#\n");
7559: fprintf(fichtmcov, "**********\n<hr size=\"2\" color=\"#EC5E5E\">");
7560: fprintf(ficresprobcor, "**********\n#");
7561: if(invalidvarcomb[j1]){
7562: fprintf(ficgp,"\n#Combination (%d) ignored because no cases \n",j1);
7563: fprintf(fichtmcov,"\n<h3>Combination (%d) ignored because no cases </h3>\n",j1);
7564: continue;
7565: }
7566: }
7567: gradg=matrix(1,npar,1,(nlstate)*(nlstate+ndeath));
7568: trgradg=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
7569: gp=vector(1,(nlstate)*(nlstate+ndeath));
7570: gm=vector(1,(nlstate)*(nlstate+ndeath));
7571: for (age=bage; age<=fage; age ++){ /* Fo each age we feed the model equation with covariates, using precov as in hpxij() ? */
7572: cov[2]=age;
7573: if(nagesqr==1)
7574: cov[3]= age*age;
7575: /* New code end of combination but for each resultline */
7576: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
7577: if(Typevar[k1]==1){ /* A product with age */
7578: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
7579: }else{
7580: cov[2+nagesqr+k1]=precov[nres][k1];
7581: }
7582: }/* End of loop on model equation */
7583: /* Old code */
7584: /* /\* for (k=1; k<=cptcovn;k++) { *\/ */
7585: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,k)]; *\/ */
7586: /* for (k=1; k<=nsd;k++) { /\* For single dummy covariates only *\/ */
7587: /* /\* Here comes the value of the covariate 'j1' after renumbering k with single dummy covariates *\/ */
7588: /* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(j1,TnsdVar[TvarsD[k]])]; */
7589: /* /\*cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,Tvar[k])];*\//\* j1 1 2 3 4 */
7590: /* * 1 1 1 1 1 */
7591: /* * 2 2 1 1 1 */
7592: /* * 3 1 2 1 1 */
7593: /* *\/ */
7594: /* /\* nbcode[1][1]=0 nbcode[1][2]=1;*\/ */
7595: /* } */
7596: /* /\* V2+V1+V4+V3*age Tvar[4]=3 ; V1+V2*age Tvar[2]=2; V1+V1*age Tvar[2]=1, Tage[1]=2 *\/ */
7597: /* /\* ) p nbcode[Tvar[Tage[k]]][(1 & (ij-1) >> (k-1))+1] *\/ */
7598: /* /\*for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; *\/ */
7599: /* for (k=1; k<=cptcovage;k++){ /\* For product with age *\/ */
7600: /* if(Dummy[Tage[k]]==2){ /\* dummy with age *\/ */
7601: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(j1,TnsdVar[Tvar[Tage[k]]])]*cov[2]; */
7602: /* /\* cov[++k1]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; *\/ */
7603: /* } else if(Dummy[Tage[k]]==3){ /\* quantitative with age *\/ */
7604: /* 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]); */
7605: /* /\* cov[2+nagesqr+Tage[k]]=meanq[k]/idq[k]*cov[2];/\\* Using the mean of quantitative variable Tvar[Tage[k]] /\\* Tqresult[nres][k]; *\\/ *\/ */
7606: /* /\* exit(1); *\/ */
7607: /* /\* cov[++k1]=Tqresult[nres][k]; *\/ */
7608: /* } */
7609: /* /\* cov[2+Tage[k]+nagesqr]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; *\/ */
7610: /* } */
7611: /* for (k=1; k<=cptcovprod;k++){/\* For product without age *\/ */
7612: /* if(Dummy[Tvard[k][1]]==0){ */
7613: /* if(Dummy[Tvard[k][2]]==0){ */
7614: /* 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]])]; */
7615: /* /\* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
7616: /* }else{ /\* Should we use the mean of the quantitative variables? *\/ */
7617: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(j1,TnsdVar[Tvard[k][1]])] * Tqresult[nres][resultmodel[nres][k]]; */
7618: /* /\* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * Tqresult[nres][k]; *\/ */
7619: /* } */
7620: /* }else{ */
7621: /* if(Dummy[Tvard[k][2]]==0){ */
7622: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(j1,TnsdVar[Tvard[k][2]])] * Tqinvresult[nres][TnsdVar[Tvard[k][1]]]; */
7623: /* /\* cov[++k1]=nbcode[Tvard[k][2]][codtabm(ij,k)] * Tqinvresult[nres][Tvard[k][1]]; *\/ */
7624: /* }else{ */
7625: /* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][TnsdVar[Tvard[k][1]]]* Tqinvresult[nres][TnsdVar[Tvard[k][2]]]; */
7626: /* /\* cov[++k1]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; *\/ */
7627: /* } */
7628: /* } */
7629: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
7630: /* } */
7631: /* For each age and combination of dummy covariates we slightly move the parameters of delti in order to get the gradient*/
7632: for(theta=1; theta <=npar; theta++){
7633: for(i=1; i<=npar; i++)
7634: xp[i] = x[i] + (i==theta ?delti[theta]:(double)0);
7635:
7636: pmij(pmmij,cov,ncovmodel,xp,nlstate);
7637:
7638: k=0;
7639: for(i=1; i<= (nlstate); i++){
7640: for(j=1; j<=(nlstate+ndeath);j++){
7641: k=k+1;
7642: gp[k]=pmmij[i][j];
7643: }
7644: }
7645:
7646: for(i=1; i<=npar; i++)
7647: xp[i] = x[i] - (i==theta ?delti[theta]:(double)0);
7648:
7649: pmij(pmmij,cov,ncovmodel,xp,nlstate);
7650: k=0;
7651: for(i=1; i<=(nlstate); i++){
7652: for(j=1; j<=(nlstate+ndeath);j++){
7653: k=k+1;
7654: gm[k]=pmmij[i][j];
7655: }
7656: }
7657:
7658: for(i=1; i<= (nlstate)*(nlstate+ndeath); i++)
7659: gradg[theta][i]=(gp[i]-gm[i])/(double)2./delti[theta];
7660: }
7661:
7662: for(j=1; j<=(nlstate)*(nlstate+ndeath);j++)
7663: for(theta=1; theta <=npar; theta++)
7664: trgradg[j][theta]=gradg[theta][j];
7665:
7666: matprod2(dnewm,trgradg,1,(nlstate)*(nlstate+ndeath),1,npar,1,npar,matcov);
7667: matprod2(doldm,dnewm,1,(nlstate)*(nlstate+ndeath),1,npar,1,(nlstate)*(nlstate+ndeath),gradg);
7668:
7669: pmij(pmmij,cov,ncovmodel,x,nlstate);
7670:
7671: k=0;
7672: for(i=1; i<=(nlstate); i++){
7673: for(j=1; j<=(nlstate+ndeath);j++){
7674: k=k+1;
7675: mu[k][(int) age]=pmmij[i][j];
7676: }
7677: }
7678: for(i=1;i<=(nlstate)*(nlstate+ndeath);i++)
7679: for(j=1;j<=(nlstate)*(nlstate+ndeath);j++)
7680: varpij[i][j][(int)age] = doldm[i][j];
7681:
7682: /*printf("\n%d ",(int)age);
7683: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
7684: printf("%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
7685: fprintf(ficlog,"%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
7686: }*/
7687:
7688: fprintf(ficresprob,"\n%d ",(int)age);
7689: fprintf(ficresprobcov,"\n%d ",(int)age);
7690: fprintf(ficresprobcor,"\n%d ",(int)age);
7691:
7692: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++)
7693: fprintf(ficresprob,"%11.3e (%11.3e) ",mu[i][(int) age],sqrt(varpij[i][i][(int)age]));
7694: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
7695: fprintf(ficresprobcov,"%11.3e ",mu[i][(int) age]);
7696: fprintf(ficresprobcor,"%11.3e ",mu[i][(int) age]);
7697: }
7698: i=0;
7699: for (k=1; k<=(nlstate);k++){
7700: for (l=1; l<=(nlstate+ndeath);l++){
7701: i++;
7702: fprintf(ficresprobcov,"\n%d %d-%d",(int)age,k,l);
7703: fprintf(ficresprobcor,"\n%d %d-%d",(int)age,k,l);
7704: for (j=1; j<=i;j++){
7705: /* printf(" k=%d l=%d i=%d j=%d\n",k,l,i,j);fflush(stdout); */
7706: fprintf(ficresprobcov," %11.3e",varpij[i][j][(int)age]);
7707: fprintf(ficresprobcor," %11.3e",varpij[i][j][(int) age]/sqrt(varpij[i][i][(int) age])/sqrt(varpij[j][j][(int)age]));
7708: }
7709: }
7710: }/* end of loop for state */
7711: } /* end of loop for age */
7712: free_vector(gp,1,(nlstate+ndeath)*(nlstate+ndeath));
7713: free_vector(gm,1,(nlstate+ndeath)*(nlstate+ndeath));
7714: free_matrix(trgradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
7715: free_matrix(gradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
7716:
7717: /* Confidence intervalle of pij */
7718: /*
7719: fprintf(ficgp,"\nunset parametric;unset label");
7720: fprintf(ficgp,"\nset log y;unset log x; set xlabel \"Age\";set ylabel \"probability (year-1)\"");
7721: fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65");
7722: 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);
7723: fprintf(fichtm,"\n<br><img src=\"pijgr%s.png\"> ",optionfilefiname);
7724: fprintf(ficgp,"\nset out \"pijgr%s.png\"",optionfilefiname);
7725: fprintf(ficgp,"\nplot \"%s\" every :::%d::%d u 1:2 \"\%%lf",k1,k2,xfilevarprob);
7726: */
7727:
7728: /* Drawing ellipsoids of confidence of two variables p(k1-l1,k2-l2)*/
7729: first1=1;first2=2;
7730: for (k2=1; k2<=(nlstate);k2++){
7731: for (l2=1; l2<=(nlstate+ndeath);l2++){
7732: if(l2==k2) continue;
7733: j=(k2-1)*(nlstate+ndeath)+l2;
7734: for (k1=1; k1<=(nlstate);k1++){
7735: for (l1=1; l1<=(nlstate+ndeath);l1++){
7736: if(l1==k1) continue;
7737: i=(k1-1)*(nlstate+ndeath)+l1;
7738: if(i<=j) continue;
7739: for (age=bage; age<=fage; age ++){
7740: if ((int)age %5==0){
7741: v1=varpij[i][i][(int)age]/stepm*YEARM/stepm*YEARM;
7742: v2=varpij[j][j][(int)age]/stepm*YEARM/stepm*YEARM;
7743: cv12=varpij[i][j][(int)age]/stepm*YEARM/stepm*YEARM;
7744: mu1=mu[i][(int) age]/stepm*YEARM ;
7745: mu2=mu[j][(int) age]/stepm*YEARM;
7746: c12=cv12/sqrt(v1*v2);
7747: /* Computing eigen value of matrix of covariance */
7748: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
7749: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
7750: if ((lc2 <0) || (lc1 <0) ){
7751: if(first2==1){
7752: first1=0;
7753: 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);
7754: }
7755: 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);
7756: /* lc1=fabs(lc1); */ /* If we want to have them positive */
7757: /* lc2=fabs(lc2); */
7758: }
7759:
7760: /* Eigen vectors */
7761: if(1+(v1-lc1)*(v1-lc1)/cv12/cv12 <1.e-5){
7762: printf(" Error sqrt of a negative number: %lf\n",1+(v1-lc1)*(v1-lc1)/cv12/cv12);
7763: fprintf(ficlog," Error sqrt of a negative number: %lf\n",1+(v1-lc1)*(v1-lc1)/cv12/cv12);
7764: v11=(1./sqrt(fabs(1+(v1-lc1)*(v1-lc1)/cv12/cv12)));
7765: }else
7766: v11=(1./sqrt(1+(v1-lc1)*(v1-lc1)/cv12/cv12));
7767: /*v21=sqrt(1.-v11*v11); *//* error */
7768: v21=(lc1-v1)/cv12*v11;
7769: v12=-v21;
7770: v22=v11;
7771: tnalp=v21/v11;
7772: if(first1==1){
7773: first1=0;
7774: 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);
7775: }
7776: 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);
7777: /*printf(fignu*/
7778: /* mu1+ v11*lc1*cost + v12*lc2*sin(t) */
7779: /* mu2+ v21*lc1*cost + v22*lc2*sin(t) */
7780: if(first==1){
7781: first=0;
7782: fprintf(ficgp,"\n# Ellipsoids of confidence\n#\n");
7783: fprintf(ficgp,"\nset parametric;unset label");
7784: 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);
7785: fprintf(ficgp,"\nset ter svg size 640, 480");
7786: fprintf(fichtmcov,"\n<p><br>Ellipsoids of confidence cov(p%1d%1d,p%1d%1d) expressed in year<sup>-1</sup>\
7787: :<a href=\"%s_%d%1d%1d-%1d%1d.svg\"> \
7788: %s_%d%1d%1d-%1d%1d.svg</A>, ",k1,l1,k2,l2,\
7789: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2, \
7790: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
7791: fprintf(fichtmcov,"\n<br><img src=\"%s_%d%1d%1d-%1d%1d.svg\"> ",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
7792: fprintf(fichtmcov,"\n<br> Correlation at age %d (%.3f),",(int) age, c12);
7793: fprintf(ficgp,"\nset out \"%s_%d%1d%1d-%1d%1d.svg\"",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
7794: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
7795: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
7796: 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", \
7797: mu1,std,v11,sqrt(fabs(lc1)),v12,sqrt(fabs(lc2)), \
7798: mu2,std,v21,sqrt(fabs(lc1)),v22,sqrt(fabs(lc2))); /* For gnuplot only */
7799: }else{
7800: first=0;
7801: fprintf(fichtmcov," %d (%.3f),",(int) age, c12);
7802: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
7803: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
7804: 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", \
7805: mu1,std,v11,sqrt(lc1),v12,sqrt(fabs(lc2)), \
7806: mu2,std,v21,sqrt(lc1),v22,sqrt(fabs(lc2)));
7807: }/* if first */
7808: } /* age mod 5 */
7809: } /* end loop age */
7810: fprintf(ficgp,"\nset out;\nset out \"%s_%d%1d%1d-%1d%1d.svg\";replot;set out;",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
7811: first=1;
7812: } /*l12 */
7813: } /* k12 */
7814: } /*l1 */
7815: }/* k1 */
7816: } /* loop on combination of covariates j1 */
7817: } /* loop on nres */
7818: free_ma3x(varpij,1,nlstate,1,nlstate+ndeath,(int) bage, (int)fage);
7819: free_matrix(mu,1,(nlstate+ndeath)*(nlstate+ndeath),(int) bage, (int)fage);
7820: free_matrix(doldm,1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
7821: free_matrix(dnewm,1,(nlstate)*(nlstate+ndeath),1,npar);
7822: free_vector(xp,1,npar);
7823: fclose(ficresprob);
7824: fclose(ficresprobcov);
7825: fclose(ficresprobcor);
7826: fflush(ficgp);
7827: fflush(fichtmcov);
7828: }
7829:
7830:
7831: /******************* Printing html file ***********/
7832: void printinghtml(char fileresu[], char title[], char datafile[], int firstpass, \
7833: int lastpass, int stepm, int weightopt, char model[],\
7834: int imx,int jmin, int jmax, double jmeanint,char rfileres[],\
7835: int popforecast, int mobilav, int prevfcast, int mobilavproj, int prevbcast, int estepm , \
7836: double jprev1, double mprev1,double anprev1, double dateprev1, double dateprojd, double dateback1, \
7837: double jprev2, double mprev2,double anprev2, double dateprev2, double dateprojf, double dateback2){
7838: int jj1, k1, i1, cpt, k4, nres;
7839: /* In fact some results are already printed in fichtm which is open */
7840: fprintf(fichtm,"<ul><li><a href='#firstorder'>Result files (first order: no variance)</a>\n \
7841: <li><a href='#secondorder'>Result files (second order (variance)</a>\n \
7842: </ul>");
7843: /* fprintf(fichtm,"<ul><li> model=1+age+%s\n \ */
7844: /* </ul>", model); */
7845: fprintf(fichtm,"<ul><li><h4><a name='firstorder'>Result files (first order: no variance)</a></h4>\n");
7846: 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",
7847: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTMFR_",".htm"),subdirfext3(optionfilefiname,"PHTMFR_",".htm"));
7848: 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) ",
7849: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTM_",".htm"),subdirfext3(optionfilefiname,"PHTM_",".htm"));
7850: fprintf(fichtm,", <a href=\"%s\">%s</a> (text file) <br>\n",subdirf2(fileresu,"P_"),subdirf2(fileresu,"P_"));
7851: fprintf(fichtm,"\
7852: - Estimated transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
7853: stepm,subdirf2(fileresu,"PIJ_"),subdirf2(fileresu,"PIJ_"));
7854: fprintf(fichtm,"\
7855: - Estimated back transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
7856: stepm,subdirf2(fileresu,"PIJB_"),subdirf2(fileresu,"PIJB_"));
7857: fprintf(fichtm,"\
7858: - Period (forward) prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
7859: subdirf2(fileresu,"PL_"),subdirf2(fileresu,"PL_"));
7860: fprintf(fichtm,"\
7861: - Backward prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
7862: subdirf2(fileresu,"PLB_"),subdirf2(fileresu,"PLB_"));
7863: fprintf(fichtm,"\
7864: - (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): \
7865: <a href=\"%s\">%s</a> <br>\n",
7866: estepm,subdirf2(fileresu,"E_"),subdirf2(fileresu,"E_"));
7867: if(prevfcast==1){
7868: fprintf(fichtm,"\
7869: - Prevalence projections by age and states: \
7870: <a href=\"%s\">%s</a> <br>\n</li>", subdirf2(fileresu,"F_"),subdirf2(fileresu,"F_"));
7871: }
7872:
7873:
7874: m=pow(2,cptcoveff);
7875: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
7876:
7877: fprintf(fichtm," \n<ul><li><b>Graphs (first order)</b></li><p>");
7878:
7879: jj1=0;
7880:
7881: fprintf(fichtm," \n<ul>");
7882: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
7883: /* k1=nres; */
7884: k1=TKresult[nres];
7885: if(TKresult[nres]==0)k1=1; /* To be checked for no result */
7886: /* for(k1=1; k1<=m;k1++){ /\* For each combination of covariate *\/ */
7887: /* if(m != 1 && TKresult[nres]!= k1) */
7888: /* continue; */
7889: jj1++;
7890: if (cptcovn > 0) {
7891: fprintf(fichtm,"\n<li><a size=\"1\" color=\"#EC5E5E\" href=\"#rescov");
7892: for (cpt=1; cpt<=cptcovs;cpt++){ /**< cptcovs number of SIMPLE covariates in the model V2+V1 =2 (dummy or quantit or time varying) */
7893: fprintf(fichtm,"_V%d=%lg_",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
7894: }
7895: /* for (cpt=1; cpt<=cptcoveff;cpt++){ */
7896: /* fprintf(fichtm,"_V%d=%d_",Tvresult[nres][cpt],(int)Tresult[nres][cpt]); */
7897: /* } */
7898: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
7899: /* fprintf(fichtm,"_V%d=%f_",Tvqresult[nres][k4],Tqresult[nres][k4]); */
7900: /* } */
7901: fprintf(fichtm,"\">");
7902:
7903: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
7904: fprintf(fichtm,"************ Results for covariates");
7905: for (cpt=1; cpt<=cptcovs;cpt++){
7906: fprintf(fichtm," V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
7907: }
7908: /* fprintf(fichtm,"************ Results for covariates"); */
7909: /* for (cpt=1; cpt<=cptcoveff;cpt++){ */
7910: /* fprintf(fichtm," V%d=%d ",Tvresult[nres][cpt],(int)Tresult[nres][cpt]); */
7911: /* } */
7912: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
7913: /* fprintf(fichtm," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
7914: /* } */
7915: if(invalidvarcomb[k1]){
7916: fprintf(fichtm," Warning Combination (%d) ignored because no cases ",k1);
7917: continue;
7918: }
7919: fprintf(fichtm,"</a></li>");
7920: } /* cptcovn >0 */
7921: }
7922: fprintf(fichtm," \n</ul>");
7923:
7924: jj1=0;
7925:
7926: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
7927: /* k1=nres; */
7928: k1=TKresult[nres];
7929: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
7930: /* for(k1=1; k1<=m;k1++){ /\* For each combination of covariate *\/ */
7931: /* if(m != 1 && TKresult[nres]!= k1) */
7932: /* continue; */
7933:
7934: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
7935: jj1++;
7936: if (cptcovn > 0) {
7937: fprintf(fichtm,"\n<p><a name=\"rescov");
7938: for (cpt=1; cpt<=cptcovs;cpt++){
7939: fprintf(fichtm,"_V%d=%lg_",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
7940: }
7941: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
7942: /* fprintf(fichtm,"_V%d=%f_",Tvqresult[nres][k4],Tqresult[nres][k4]); */
7943: /* } */
7944: fprintf(fichtm,"\"</a>");
7945:
7946: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
7947: for (cpt=1; cpt<=cptcovs;cpt++){
7948: fprintf(fichtm," V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
7949: printf(" V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
7950: /* fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]); */
7951: /* printf(" V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]);fflush(stdout); */
7952: }
7953: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
7954: fprintf(fichtm," (model=1+age+%s) ************\n<hr size=\"2\" color=\"#EC5E5E\">",model);
7955: if(invalidvarcomb[k1]){
7956: fprintf(fichtm,"\n<h3>Combination (%d) ignored because no cases </h3>\n",k1);
7957: printf("\nCombination (%d) ignored because no cases \n",k1);
7958: continue;
7959: }
7960: }
7961: /* aij, bij */
7962: 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> \
7963: <img src=\"%s_%d-1-%d.svg\">",model,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres);
7964: /* Pij */
7965: 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> \
7966: <img src=\"%s_%d-2-%d.svg\">",stepm,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres);
7967: /* Quasi-incidences */
7968: fprintf(fichtm,"<br>\n- I<sub>ij</sub> or Conditional probabilities to be observed in state j being in state i %d (stepm) months\
7969: before but expressed in per year i.e. quasi incidences if stepm is small and probabilities too, \
7970: incidence (rates) are the limit when h tends to zero of the ratio of the probability <sub>h</sub>P<sub>ij</sub> \
7971: 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> \
7972: <img src=\"%s_%d-3-%d.svg\">",stepm,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres);
7973: /* Survival functions (period) in state j */
7974: for(cpt=1; cpt<=nlstate;cpt++){
7975: 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. <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);
7976: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"PIJ_"),subdirf2(optionfilefiname,"PIJ_"));
7977: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres);
7978: }
7979: /* State specific survival functions (period) */
7980: for(cpt=1; cpt<=nlstate;cpt++){
7981: fprintf(fichtm,"<br>\n- Survival functions in state %d and in any other live state (total).\
7982: And probability to be observed in various states (up to %d) being in state %d at different ages. \
7983: <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);
7984: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"PIJ_"),subdirf2(optionfilefiname,"PIJ_"));
7985: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",subdirf2(optionfilefiname,"LIJT_"),cpt,k1,nres);
7986: }
7987: /* Period (forward stable) prevalence in each health state */
7988: for(cpt=1; cpt<=nlstate;cpt++){
7989: 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 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);
7990: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"PIJ_"),subdirf2(optionfilefiname,"PIJ_"));
7991: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">" ,subdirf2(optionfilefiname,"P_"),cpt,k1,nres);
7992: }
7993: if(prevbcast==1){
7994: /* Backward prevalence in each health state */
7995: for(cpt=1; cpt<=nlstate;cpt++){
7996: 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);
7997: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"PIJB_"),subdirf2(optionfilefiname,"PIJB_"));
7998: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">" ,subdirf2(optionfilefiname,"PB_"),cpt,k1,nres);
7999: }
8000: }
8001: if(prevfcast==1){
8002: /* Projection of prevalence up to period (forward stable) prevalence in each health state */
8003: for(cpt=1; cpt<=nlstate;cpt++){
8004: 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);
8005: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"F_"),subdirf2(optionfilefiname,"F_"));
8006: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",
8007: subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres);
8008: }
8009: }
8010: if(prevbcast==1){
8011: /* Back projection of prevalence up to stable (mixed) back-prevalence in each health state */
8012: for(cpt=1; cpt<=nlstate;cpt++){
8013: fprintf(fichtm,"<br>\n- Back projection of cross-sectional prevalence (estimated with cases observed from %.1f to %.1f and mobil_average=%d), \
8014: 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 \
8015: 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) \
8016: 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);
8017: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"FB_"),subdirf2(optionfilefiname,"FB_"));
8018: fprintf(fichtm," <img src=\"%s_%d-%d-%d.svg\">", subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres);
8019: }
8020: }
8021:
8022: for(cpt=1; cpt<=nlstate;cpt++) {
8023: 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);
8024: fprintf(fichtm," (data from text file <a href=\"%s.txt\"> %s.txt</a>)\n<br>",subdirf2(optionfilefiname,"E_"),subdirf2(optionfilefiname,"E_"));
8025: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">", subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres );
8026: }
8027: /* } /\* end i1 *\/ */
8028: }/* End k1=nres */
8029: fprintf(fichtm,"</ul>");
8030:
8031: fprintf(fichtm,"\
8032: \n<br><li><h4> <a name='secondorder'>Result files (second order: variances)</a></h4>\n\
8033: - Parameter file with estimated parameters and covariance matrix: <a href=\"%s\">%s</a> <br> \
8034: - 95%% confidence intervals and Wald tests of the estimated parameters are in the log file if optimization has been done (mle != 0).<br> \
8035: But because parameters are usually highly correlated (a higher incidence of disability \
8036: and a higher incidence of recovery can give very close observed transition) it might \
8037: be very useful to look not only at linear confidence intervals estimated from the \
8038: variances but at the covariance matrix. And instead of looking at the estimated coefficients \
8039: (parameters) of the logistic regression, it might be more meaningful to visualize the \
8040: covariance matrix of the one-step probabilities. \
8041: See page 'Matrix of variance-covariance of one-step probabilities' below. \n", rfileres,rfileres);
8042:
8043: fprintf(fichtm," - Standard deviation of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
8044: subdirf2(fileresu,"PROB_"),subdirf2(fileresu,"PROB_"));
8045: fprintf(fichtm,"\
8046: - Variance-covariance of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
8047: subdirf2(fileresu,"PROBCOV_"),subdirf2(fileresu,"PROBCOV_"));
8048:
8049: fprintf(fichtm,"\
8050: - Correlation matrix of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
8051: subdirf2(fileresu,"PROBCOR_"),subdirf2(fileresu,"PROBCOR_"));
8052: fprintf(fichtm,"\
8053: - 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): \
8054: <a href=\"%s\">%s</a> <br>\n</li>",
8055: estepm,subdirf2(fileresu,"CVE_"),subdirf2(fileresu,"CVE_"));
8056: fprintf(fichtm,"\
8057: - (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): \
8058: <a href=\"%s\">%s</a> <br>\n</li>",
8059: estepm,subdirf2(fileresu,"STDE_"),subdirf2(fileresu,"STDE_"));
8060: fprintf(fichtm,"\
8061: - 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",
8062: estepm, subdirf2(fileresu,"V_"),subdirf2(fileresu,"V_"));
8063: fprintf(fichtm,"\
8064: - 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",
8065: estepm, subdirf2(fileresu,"T_"),subdirf2(fileresu,"T_"));
8066: fprintf(fichtm,"\
8067: - Standard deviation of forward (period) prevalences: <a href=\"%s\">%s</a> <br>\n",\
8068: subdirf2(fileresu,"VPL_"),subdirf2(fileresu,"VPL_"));
8069:
8070: /* if(popforecast==1) fprintf(fichtm,"\n */
8071: /* - Prevalences forecasting: <a href=\"f%s\">f%s</a> <br>\n */
8072: /* - Population forecasting (if popforecast=1): <a href=\"pop%s\">pop%s</a> <br>\n */
8073: /* <br>",fileres,fileres,fileres,fileres); */
8074: /* else */
8075: /* 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); */
8076: fflush(fichtm);
8077:
8078: m=pow(2,cptcoveff);
8079: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
8080:
8081: fprintf(fichtm," <ul><li><b>Graphs (second order)</b></li><p>");
8082:
8083: jj1=0;
8084:
8085: fprintf(fichtm," \n<ul>");
8086: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
8087: /* k1=nres; */
8088: k1=TKresult[nres];
8089: /* for(k1=1; k1<=m;k1++){ /\* For each combination of covariate *\/ */
8090: /* if(m != 1 && TKresult[nres]!= k1) */
8091: /* continue; */
8092: jj1++;
8093: if (cptcovn > 0) {
8094: fprintf(fichtm,"\n<li><a size=\"1\" color=\"#EC5E5E\" href=\"#rescovsecond");
8095: for (cpt=1; cpt<=cptcovs;cpt++){
8096: fprintf(fichtm,"_V%d=%lg_",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
8097: }
8098: fprintf(fichtm,"\">");
8099:
8100: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
8101: fprintf(fichtm,"************ Results for covariates");
8102: for (cpt=1; cpt<=cptcovs;cpt++){
8103: fprintf(fichtm," V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
8104: }
8105: if(invalidvarcomb[k1]){
8106: fprintf(fichtm," Warning Combination (%d) ignored because no cases ",k1);
8107: continue;
8108: }
8109: fprintf(fichtm,"</a></li>");
8110: } /* cptcovn >0 */
8111: } /* End nres */
8112: fprintf(fichtm," \n</ul>");
8113:
8114: jj1=0;
8115:
8116: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
8117: /* k1=nres; */
8118: k1=TKresult[nres];
8119: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
8120: /* for(k1=1; k1<=m;k1++){ */
8121: /* if(m != 1 && TKresult[nres]!= k1) */
8122: /* continue; */
8123: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
8124: jj1++;
8125: if (cptcovn > 0) {
8126: fprintf(fichtm,"\n<p><a name=\"rescovsecond");
8127: for (cpt=1; cpt<=cptcovs;cpt++){
8128: fprintf(fichtm,"_V%d=%lg_",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
8129: }
8130: fprintf(fichtm,"\"</a>");
8131:
8132: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
8133: for (cpt=1; cpt<=cptcovs;cpt++){ /**< cptcoveff number of variables */
8134: fprintf(fichtm," V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
8135: printf(" V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
8136: /* fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]); */
8137: }
8138:
8139: fprintf(fichtm," (model=1+age+%s) ************\n<hr size=\"2\" color=\"#EC5E5E\">",model);
8140:
8141: if(invalidvarcomb[k1]){
8142: fprintf(fichtm,"\n<h4>Combination (%d) ignored because no cases </h4>\n",k1);
8143: continue;
8144: }
8145: } /* If cptcovn >0 */
8146: for(cpt=1; cpt<=nlstate;cpt++) {
8147: fprintf(fichtm,"\n<br>- Observed (cross-sectional with mov_average=%d) and period (incidence based) \
8148: 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);
8149: fprintf(fichtm," (data from text file <a href=\"%s\">%s</a>)\n <br>",subdirf2(fileresu,"VPL_"),subdirf2(fileresu,"VPL_"));
8150: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",subdirf2(optionfilefiname,"V_"), cpt,k1,nres);
8151: }
8152: fprintf(fichtm,"\n<br>- Total life expectancy by age and \
8153: health expectancies in each live states (1 to %d). If popbased=1 the smooth (due to the model) \
8154: true period expectancies (those weighted with period prevalences are also\
8155: drawn in addition to the population based expectancies computed using\
8156: 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);
8157: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>) \n<br>",subdirf2(optionfilefiname,"T_"),subdirf2(optionfilefiname,"T_"));
8158: fprintf(fichtm,"<img src=\"%s_%d-%d.svg\">",subdirf2(optionfilefiname,"E_"),k1,nres);
8159: /* } /\* end i1 *\/ */
8160: }/* End nres */
8161: fprintf(fichtm,"</ul>");
8162: fflush(fichtm);
8163: }
8164:
8165: /******************* Gnuplot file **************/
8166: 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){
8167:
8168: char dirfileres[132],optfileres[132];
8169: char gplotcondition[132], gplotlabel[132];
8170: 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;
8171: int lv=0, vlv=0, kl=0;
8172: int ng=0;
8173: int vpopbased;
8174: int ioffset; /* variable offset for columns */
8175: int iyearc=1; /* variable column for year of projection */
8176: int iagec=1; /* variable column for age of projection */
8177: int nres=0; /* Index of resultline */
8178: int istart=1; /* For starting graphs in projections */
8179:
8180: /* if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) { */
8181: /* printf("Problem with file %s",optionfilegnuplot); */
8182: /* fprintf(ficlog,"Problem with file %s",optionfilegnuplot); */
8183: /* } */
8184:
8185: /*#ifdef windows */
8186: fprintf(ficgp,"cd \"%s\" \n",pathc);
8187: /*#endif */
8188: m=pow(2,cptcoveff);
8189:
8190: /* diagram of the model */
8191: fprintf(ficgp,"\n#Diagram of the model \n");
8192: fprintf(ficgp,"\ndelta=0.03;delta2=0.07;unset arrow;\n");
8193: fprintf(ficgp,"yoff=(%d > 2? 0:1);\n",nlstate);
8194: 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);
8195:
8196: 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);
8197: fprintf(ficgp,"\n#show arrow\nunset label\n");
8198: 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);
8199: fprintf(ficgp,"\nset label %d+1 sprintf(\"State %%d\",%d+1) center at 0.,0. font \"helvetica, 16\" tc rgbcolor \"red\"\n",nlstate,nlstate);
8200: fprintf(ficgp,"\n#show label\nunset border;unset xtics; unset ytics;\n");
8201: fprintf(ficgp,"\n\nset ter svg size 640, 480;set out \"%s_.svg\" \n",subdirf2(optionfilefiname,"D_"));
8202: fprintf(ficgp,"unset log y; plot [-1.2:1.2][yoff-1.2:1.2] 1/0 not; set out;reset;\n");
8203:
8204: /* Contribution to likelihood */
8205: /* Plot the probability implied in the likelihood */
8206: fprintf(ficgp,"\n# Contributions to the Likelihood, mle >=1. For mle=4 no interpolation, pure matrix products.\n#\n");
8207: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Likelihood (-2Log(L))\";");
8208: /* fprintf(ficgp,"\nset ter svg size 640, 480"); */ /* Too big for svg */
8209: fprintf(ficgp,"\nset ter pngcairo size 640, 480");
8210: /* nice for mle=4 plot by number of matrix products.
8211: replot "rrtest1/toto.txt" u 2:($4 == 1 && $5==2 ? $9 : 1/0):5 t "p12" with point lc 1 */
8212: /* replot exp(p1+p2*x)/(1+exp(p1+p2*x)+exp(p3+p4*x)+exp(p5+p6*x)) t "p12(x)" */
8213: /* fprintf(ficgp,"\nset out \"%s.svg\";",subdirf2(optionfilefiname,"ILK_")); */
8214: fprintf(ficgp,"\nset out \"%s-dest.png\";",subdirf2(optionfilefiname,"ILK_"));
8215: 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));
8216: fprintf(ficgp,"\nset out \"%s-ori.png\";",subdirf2(optionfilefiname,"ILK_"));
8217: 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));
8218: for (i=1; i<= nlstate ; i ++) {
8219: fprintf(ficgp,"\nset out \"%s-p%dj.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i);
8220: fprintf(ficgp,"unset log;\n# plot weighted, mean weight should have point size of 0.5\n plot \"%s\"",subdirf(fileresilk));
8221: 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);
8222: for (j=2; j<= nlstate+ndeath ; j ++) {
8223: 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);
8224: }
8225: fprintf(ficgp,";\nset out; unset ylabel;\n");
8226: }
8227: /* 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 */
8228: /* fprintf(ficgp,"\nset log y;plot \"%s\" u 2:(-$11):3 t \"All sample, all transitions\" with dots lc variable",subdirf(fileresilk)); */
8229: /* fprintf(ficgp,"\nreplot \"%s\" u 2:($3 <= 3 ? -$11 : 1/0):3 t \"First 3 individuals\" with line lc variable", subdirf(fileresilk)); */
8230: fprintf(ficgp,"\nset out;unset log\n");
8231: /* fprintf(ficgp,"\nset out \"%s.svg\"; replot; set out; # bug gnuplot",subdirf2(optionfilefiname,"ILK_")); */
8232:
8233: /* Plot the probability implied in the likelihood by covariate value */
8234: fprintf(ficgp,"\nset ter pngcairo size 640, 480");
8235: /* if(debugILK==1){ */
8236: for(kf=1; kf <= ncovf; kf++){ /* For each simple dummy covariate of the model */
8237: kvar=Tvar[TvarFind[kf]]; /* variable name */
8238: /* k=18+Tvar[TvarFind[kf]];/\*offset because there are 18 columns in the ILK_ file but could be placed else where *\/ */
8239: k=18+kf;/*offset because there are 18 columns in the ILK_ file */
8240: for (i=1; i<= nlstate ; i ++) {
8241: fprintf(ficgp,"\nset out \"%s-p%dj-%d.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i,kvar);
8242: fprintf(ficgp,"unset log;\n# For each simple dummy covariate of the model \n plot \"%s\"",subdirf(fileresilk));
8243: if(gnuplotversion >=5.2){ /* Former gnuplot versions do not have variable pointsize!! */
8244: 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);
8245: for (j=2; j<= nlstate+ndeath ; j ++) {
8246: 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);
8247: }
8248: }else{
8249: 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);
8250: for (j=2; j<= nlstate+ndeath ; j ++) {
8251: 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);
8252: }
8253: }
8254: fprintf(ficgp,";\nset out; unset ylabel;\n");
8255: }
8256: } /* End of each covariate dummy */
8257: for(ncovv=1, iposold=0, kk=0; ncovv <= ncovvt ; ncovv++){
8258: /* Other example V1 + V3 + V5 + age*V1 + age*V3 + age*V5 + V1*V3 + V3*V5 + V1*V5
8259: * kmodel = 1 2 3 4 5 6 7 8 9
8260: * varying 1 2 3 4 5
8261: * ncovv 1 2 3 4 5 6 7 8
8262: * TvarVV[ncovv] V3 5 1 3 3 5 1 5
8263: * TvarVVind[ncovv]=kmodel 2 3 7 7 8 8 9 9
8264: * TvarFind[kmodel] 1 0 0 0 0 0 0 0 0
8265: * kdata ncovcol=[V1 V2] nqv=0 ntv=[V3 V4] nqtv=V5
8266: * Dummy[kmodel] 0 0 1 2 2 3 1 1 1
8267: */
8268: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate */
8269: kvar=TvarVV[ncovv]; /* TvarVV={3, 1, 3} gives the name of each varying covariate */
8270: /* 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]); */
8271: if(ipos!=iposold){ /* Not a product or first of a product */
8272: /* printf(" %d",ipos); */
8273: /* fprintf(ficresilk," V%d",TvarVV[ncovv]); */
8274: /* printf(" DebugILK ficgp suite ipos=%d != iposold=%d\n", ipos, iposold); */
8275: kk++; /* Position of the ncovv column in ILK_ */
8276: k=18+ncovf+kk; /*offset because there are 18 columns in the ILK_ file plus ncovf fixed covariate */
8277: 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) */
8278: for (i=1; i<= nlstate ; i ++) {
8279: fprintf(ficgp,"\nset out \"%s-p%dj-%d.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i,kvar);
8280: fprintf(ficgp,"unset log;\n# For each simple dummy covariate of the model \n plot \"%s\"",subdirf(fileresilk));
8281:
8282: /* printf("Before DebugILK gnuplotversion=%g >=5.2\n",gnuplotversion); */
8283: if(gnuplotversion >=5.2){ /* Former gnuplot versions do not have variable pointsize!! */
8284: /* printf("DebugILK gnuplotversion=%g >=5.2\n",gnuplotversion); */
8285: 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);
8286: for (j=2; j<= nlstate+ndeath ; j ++) {
8287: 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);
8288: }
8289: }else{
8290: /* printf("DebugILK gnuplotversion=%g <5.2\n",gnuplotversion); */
8291: 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);
8292: for (j=2; j<= nlstate+ndeath ; j ++) {
8293: 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);
8294: }
8295: }
8296: fprintf(ficgp,";\nset out; unset ylabel;\n");
8297: }
8298: }/* End if dummy varying */
8299: }else{ /*Product */
8300: /* printf("*"); */
8301: /* fprintf(ficresilk,"*"); */
8302: }
8303: iposold=ipos;
8304: } /* For each time varying covariate */
8305: /* } /\* debugILK==1 *\/ */
8306: /* 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 */
8307: /* fprintf(ficgp,"\nset log y;plot \"%s\" u 2:(-$11):3 t \"All sample, all transitions\" with dots lc variable",subdirf(fileresilk)); */
8308: /* fprintf(ficgp,"\nreplot \"%s\" u 2:($3 <= 3 ? -$11 : 1/0):3 t \"First 3 individuals\" with line lc variable", subdirf(fileresilk)); */
8309: fprintf(ficgp,"\nset out;unset log\n");
8310: /* fprintf(ficgp,"\nset out \"%s.svg\"; replot; set out; # bug gnuplot",subdirf2(optionfilefiname,"ILK_")); */
8311:
8312:
8313:
8314: strcpy(dirfileres,optionfilefiname);
8315: strcpy(optfileres,"vpl");
8316: /* 1eme*/
8317: for (cpt=1; cpt<= nlstate ; cpt ++){ /* For each live state */
8318: /* for (k1=1; k1<= m ; k1 ++){ /\* For each valid combination of covariate *\/ */
8319: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
8320: k1=TKresult[nres];
8321: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
8322: /* plot [100000000000000000000:-100000000000000000000] "mysbiaspar/vplrmysbiaspar.txt to check */
8323: /* if(m != 1 && TKresult[nres]!= k1) */
8324: /* continue; */
8325: /* We are interested in selected combination by the resultline */
8326: /* printf("\n# 1st: Period (stable) prevalence with CI: 'VPL_' files and live state =%d ", cpt); */
8327: fprintf(ficgp,"\n# 1st: Forward (stable period) prevalence with CI: 'VPL_' files and live state =%d ", cpt);
8328: strcpy(gplotlabel,"(");
8329: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
8330: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8331: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8332:
8333: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate k get corresponding value lv for combination k1 *\/ */
8334: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the value of the covariate corresponding to k1 combination *\\/ *\/ */
8335: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
8336: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
8337: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
8338: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
8339: /* vlv= nbcode[Tvaraff[k]][lv]; /\* vlv is the value of the covariate lv, 0 or 1 *\/ */
8340: /* /\* For each combination of covariate k1 (V1=1, V3=0), we printed the current covariate k and its value vlv *\/ */
8341: /* /\* printf(" V%d=%d ",Tvaraff[k],vlv); *\/ */
8342: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
8343: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
8344: /* } */
8345: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
8346: /* /\* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); *\/ */
8347: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
8348: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
8349: }
8350: strcpy(gplotlabel+strlen(gplotlabel),")");
8351: /* printf("\n#\n"); */
8352: fprintf(ficgp,"\n#\n");
8353: if(invalidvarcomb[k1]){
8354: /*k1=k1-1;*/ /* To be checked */
8355: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
8356: continue;
8357: }
8358:
8359: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"V_"),cpt,k1,nres);
8360: fprintf(ficgp,"\n#set out \"V_%s_%d-%d-%d.svg\" \n",optionfilefiname,cpt,k1,nres);
8361: /* fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel); */
8362: fprintf(ficgp,"set title \"Alive state %d %s model=1+age+%s\" font \"Helvetica,12\"\n",cpt,gplotlabel,model);
8363: 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);
8364: /* 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); */
8365: /* k1-1 error should be nres-1*/
8366: for (i=1; i<= nlstate ; i ++) {
8367: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
8368: else fprintf(ficgp," %%*lf (%%*lf)");
8369: }
8370: 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);
8371: for (i=1; i<= nlstate ; i ++) {
8372: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
8373: else fprintf(ficgp," %%*lf (%%*lf)");
8374: }
8375: 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);
8376: for (i=1; i<= nlstate ; i ++) {
8377: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
8378: else fprintf(ficgp," %%*lf (%%*lf)");
8379: }
8380: /* 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)); */
8381:
8382: fprintf(ficgp,"\" t\"\" w l lt 1,\"%s\" u 1:((",subdirf2(fileresu,"P_"));
8383: if(cptcoveff ==0){
8384: fprintf(ficgp,"$%d)) t 'Observed prevalence in state %d' with line lt 3", 2+3*(cpt-1), cpt );
8385: }else{
8386: kl=0;
8387: for (k=1; k<=cptcoveff; k++){ /* For each combination of covariate */
8388: /* lv= decodtabm(k1,k,cptcoveff); /\* Should be the covariate value corresponding to k1 combination and kth covariate *\/ */
8389: lv=codtabm(k1,TnsdVar[Tvaraff[k]]);
8390: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
8391: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
8392: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
8393: vlv= nbcode[Tvaraff[k]][lv];
8394: kl++;
8395: /* 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 *\/ */
8396: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
8397: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
8398: /* '' 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*/
8399: if(k==cptcoveff){
8400: 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], \
8401: 2+cptcoveff*2+3*(cpt-1), cpt ); /* 4 or 6 ?*/
8402: }else{
8403: fprintf(ficgp,"$%d==%d && $%d==%d && ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv]);
8404: kl++;
8405: }
8406: } /* end covariate */
8407: } /* end if no covariate */
8408:
8409: if(prevbcast==1){ /* We need to get the corresponding values of the covariates involved in this combination k1 */
8410: /* 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); */
8411: fprintf(ficgp,",\"%s\" u 1:((",subdirf2(fileresu,"PLB_")); /* Age is in 1, nres in 2 to be fixed */
8412: if(cptcoveff ==0){
8413: fprintf(ficgp,"$%d)) t 'Backward prevalence in state %d' with line lt 3", 2+(cpt-1), cpt );
8414: }else{
8415: kl=0;
8416: for (k=1; k<=cptcoveff; k++){ /* For each combination of covariate */
8417: /* lv= decodtabm(k1,k,cptcoveff); /\* Should be the covariate value corresponding to k1 combination and kth covariate *\/ */
8418: lv=codtabm(k1,TnsdVar[Tvaraff[k]]);
8419: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
8420: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
8421: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
8422: /* vlv= nbcode[Tvaraff[k]][lv]; */
8423: vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])];
8424: kl++;
8425: /* 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 *\/ */
8426: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
8427: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
8428: /* '' 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*/
8429: if(k==cptcoveff){
8430: 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], \
8431: 2+cptcoveff*2+(cpt-1), cpt ); /* 4 or 6 ?*/
8432: }else{
8433: fprintf(ficgp,"$%d==%d && $%d==%d && ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]);
8434: kl++;
8435: }
8436: } /* end covariate */
8437: } /* end if no covariate */
8438: if(prevbcast == 1){
8439: fprintf(ficgp,", \"%s\" every :::%d::%d u 1:($2==%d ? $3:1/0) \"%%lf %%lf",subdirf2(fileresu,"VBL_"),nres-1,nres-1,nres);
8440: /* k1-1 error should be nres-1*/
8441: for (i=1; i<= nlstate ; i ++) {
8442: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
8443: else fprintf(ficgp," %%*lf (%%*lf)");
8444: }
8445: 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);
8446: for (i=1; i<= nlstate ; i ++) {
8447: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
8448: else fprintf(ficgp," %%*lf (%%*lf)");
8449: }
8450: 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);
8451: for (i=1; i<= nlstate ; i ++) {
8452: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
8453: else fprintf(ficgp," %%*lf (%%*lf)");
8454: }
8455: fprintf(ficgp,"\" t\"\" w l lt 4");
8456: } /* end if backprojcast */
8457: } /* end if prevbcast */
8458: /* fprintf(ficgp,"\nset out ;unset label;\n"); */
8459: fprintf(ficgp,"\nset out ;unset title;\n");
8460: } /* nres */
8461: /* } /\* k1 *\/ */
8462: } /* cpt */
8463:
8464:
8465: /*2 eme*/
8466: /* for (k1=1; k1<= m ; k1 ++){ */
8467: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
8468: k1=TKresult[nres];
8469: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
8470: /* if(m != 1 && TKresult[nres]!= k1) */
8471: /* continue; */
8472: fprintf(ficgp,"\n# 2nd: Total life expectancy with CI: 't' files ");
8473: strcpy(gplotlabel,"(");
8474: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
8475: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8476: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8477: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
8478: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
8479: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
8480: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
8481: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
8482: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
8483: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
8484: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
8485: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
8486: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
8487: /* } */
8488: /* /\* for(k=1; k <= ncovds; k++){ *\/ */
8489: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
8490: /* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
8491: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
8492: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
8493: }
8494: strcpy(gplotlabel+strlen(gplotlabel),")");
8495: fprintf(ficgp,"\n#\n");
8496: if(invalidvarcomb[k1]){
8497: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
8498: continue;
8499: }
8500:
8501: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"E_"),k1,nres);
8502: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
8503: fprintf(ficgp,"\nset label \"popbased %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",vpopbased,gplotlabel);
8504: if(vpopbased==0){
8505: fprintf(ficgp,"set ylabel \"Years\" \nset ter svg size 640, 480\nplot [%.f:%.f] ",ageminpar,fage);
8506: }else
8507: fprintf(ficgp,"\nreplot ");
8508: for (i=1; i<= nlstate+1 ; i ++) {
8509: k=2*i;
8510: 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);
8511: for (j=1; j<= nlstate+1 ; j ++) {
8512: if (j==i) fprintf(ficgp," %%lf (%%lf)");
8513: else fprintf(ficgp," %%*lf (%%*lf)");
8514: }
8515: if (i== 1) fprintf(ficgp,"\" t\"TLE\" w l lt %d, \\\n",i);
8516: else fprintf(ficgp,"\" t\"LE in state (%d)\" w l lt %d, \\\n",i-1,i+1);
8517: 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);
8518: for (j=1; j<= nlstate+1 ; j ++) {
8519: if (j==i) fprintf(ficgp," %%lf (%%lf)");
8520: else fprintf(ficgp," %%*lf (%%*lf)");
8521: }
8522: fprintf(ficgp,"\" t\"\" w l lt 0,");
8523: 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);
8524: for (j=1; j<= nlstate+1 ; j ++) {
8525: if (j==i) fprintf(ficgp," %%lf (%%lf)");
8526: else fprintf(ficgp," %%*lf (%%*lf)");
8527: }
8528: if (i== (nlstate+1)) fprintf(ficgp,"\" t\"\" w l lt 0");
8529: else fprintf(ficgp,"\" t\"\" w l lt 0,\\\n");
8530: } /* state */
8531: } /* vpopbased */
8532: fprintf(ficgp,"\nset out;set out \"%s_%d-%d.svg\"; replot; set out; unset label;\n",subdirf2(optionfilefiname,"E_"),k1,nres); /* Buggy gnuplot */
8533: } /* end nres */
8534: /* } /\* k1 end 2 eme*\/ */
8535:
8536:
8537: /*3eme*/
8538: /* for (k1=1; k1<= m ; k1 ++){ */
8539: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
8540: k1=TKresult[nres];
8541: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
8542: /* if(m != 1 && TKresult[nres]!= k1) */
8543: /* continue; */
8544:
8545: for (cpt=1; cpt<= nlstate ; cpt ++) { /* Fragile no verification of covariate values */
8546: fprintf(ficgp,"\n\n# 3d: Life expectancy with EXP_ files: combination=%d state=%d",k1, cpt);
8547: strcpy(gplotlabel,"(");
8548: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
8549: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8550: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8551: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
8552: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
8553: /* lv= codtabm(k1,TnsdVar[Tvaraff[k]]); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
8554: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
8555: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
8556: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
8557: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
8558: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
8559: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
8560: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
8561: /* } */
8562: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
8563: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][resultmodel[nres][k4]]); */
8564: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][resultmodel[nres][k4]]); */
8565: }
8566: strcpy(gplotlabel+strlen(gplotlabel),")");
8567: fprintf(ficgp,"\n#\n");
8568: if(invalidvarcomb[k1]){
8569: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
8570: continue;
8571: }
8572:
8573: /* k=2+nlstate*(2*cpt-2); */
8574: k=2+(nlstate+1)*(cpt-1);
8575: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres);
8576: fprintf(ficgp,"set label \"%s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",gplotlabel);
8577: fprintf(ficgp,"set ter svg size 640, 480\n\
8578: 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);
8579: /*fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d-2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
8580: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
8581: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
8582: fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d+2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
8583: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
8584: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
8585:
8586: */
8587: for (i=1; i< nlstate ; i ++) {
8588: 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);
8589: /* 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);*/
8590:
8591: }
8592: fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d.\" w l",subdirf2(fileresu,"E_"),nres-1,nres-1,k+nlstate,cpt);
8593: }
8594: fprintf(ficgp,"\nunset label;\n");
8595: } /* end nres */
8596: /* } /\* end kl 3eme *\/ */
8597:
8598: /* 4eme */
8599: /* Survival functions (period) from state i in state j by initial state i */
8600: /* for (k1=1; k1<=m; k1++){ /\* For each covariate and each value *\/ */
8601: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
8602: k1=TKresult[nres];
8603: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
8604: /* if(m != 1 && TKresult[nres]!= k1) */
8605: /* continue; */
8606: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state cpt*/
8607: strcpy(gplotlabel,"(");
8608: fprintf(ficgp,"\n#\n#\n# Survival functions in state %d : 'LIJ_' files, cov=%d state=%d", cpt, k1, cpt);
8609: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
8610: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8611: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8612: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
8613: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
8614: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
8615: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
8616: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
8617: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
8618: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
8619: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
8620: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
8621: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
8622: /* } */
8623: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
8624: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
8625: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
8626: }
8627: strcpy(gplotlabel+strlen(gplotlabel),")");
8628: fprintf(ficgp,"\n#\n");
8629: if(invalidvarcomb[k1]){
8630: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
8631: continue;
8632: }
8633:
8634: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres);
8635: fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
8636: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
8637: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
8638: k=3;
8639: for (i=1; i<= nlstate ; i ++){
8640: if(i==1){
8641: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
8642: }else{
8643: fprintf(ficgp,", '' ");
8644: }
8645: l=(nlstate+ndeath)*(i-1)+1;
8646: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
8647: for (j=2; j<= nlstate+ndeath ; j ++)
8648: fprintf(ficgp,"+$%d",k+l+j-1);
8649: fprintf(ficgp,")) t \"l(%d,%d)\" w l",i,cpt);
8650: } /* nlstate */
8651: fprintf(ficgp,"\nset out; unset label;\n");
8652: } /* end cpt state*/
8653: } /* end nres */
8654: /* } /\* end covariate k1 *\/ */
8655:
8656: /* 5eme */
8657: /* Survival functions (period) from state i in state j by final state j */
8658: /* for (k1=1; k1<= m ; k1++){ /\* For each covariate combination if any *\/ */
8659: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
8660: k1=TKresult[nres];
8661: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
8662: /* if(m != 1 && TKresult[nres]!= k1) */
8663: /* continue; */
8664: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each inital state */
8665: strcpy(gplotlabel,"(");
8666: 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);
8667: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
8668: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8669: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8670: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
8671: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
8672: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
8673: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
8674: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
8675: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
8676: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
8677: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
8678: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
8679: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
8680: /* } */
8681: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
8682: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
8683: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
8684: }
8685: strcpy(gplotlabel+strlen(gplotlabel),")");
8686: fprintf(ficgp,"\n#\n");
8687: if(invalidvarcomb[k1]){
8688: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
8689: continue;
8690: }
8691:
8692: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJT_"),cpt,k1,nres);
8693: fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
8694: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
8695: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
8696: k=3;
8697: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
8698: if(j==1)
8699: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
8700: else
8701: fprintf(ficgp,", '' ");
8702: l=(nlstate+ndeath)*(cpt-1) +j;
8703: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):($%d",k1,k+l);
8704: /* for (i=2; i<= nlstate+ndeath ; i ++) */
8705: /* fprintf(ficgp,"+$%d",k+l+i-1); */
8706: fprintf(ficgp,") t \"l(%d,%d)\" w l",cpt,j);
8707: } /* nlstate */
8708: fprintf(ficgp,", '' ");
8709: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):(",k1);
8710: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
8711: l=(nlstate+ndeath)*(cpt-1) +j;
8712: if(j < nlstate)
8713: fprintf(ficgp,"$%d +",k+l);
8714: else
8715: fprintf(ficgp,"$%d) t\"l(%d,.)\" w l",k+l,cpt);
8716: }
8717: fprintf(ficgp,"\nset out; unset label;\n");
8718: } /* end cpt state*/
8719: /* } /\* end covariate *\/ */
8720: } /* end nres */
8721:
8722: /* 6eme */
8723: /* CV preval stable (period) for each covariate */
8724: /* for (k1=1; k1<= m ; k1 ++) /\* For each covariate combination if any *\/ */
8725: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
8726: k1=TKresult[nres];
8727: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
8728: /* if(m != 1 && TKresult[nres]!= k1) */
8729: /* continue; */
8730: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state of arrival */
8731: strcpy(gplotlabel,"(");
8732: fprintf(ficgp,"\n#\n#\n#CV preval stable (forward): 'pij' files, covariatecombination#=%d state=%d",k1, cpt);
8733: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
8734: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8735: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8736: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
8737: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
8738: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
8739: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
8740: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
8741: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
8742: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
8743: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
8744: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
8745: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
8746: /* } */
8747: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
8748: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
8749: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
8750: }
8751: strcpy(gplotlabel+strlen(gplotlabel),")");
8752: fprintf(ficgp,"\n#\n");
8753: if(invalidvarcomb[k1]){
8754: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
8755: continue;
8756: }
8757:
8758: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"P_"),cpt,k1,nres);
8759: fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
8760: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
8761: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
8762: k=3; /* Offset */
8763: for (i=1; i<= nlstate ; i ++){ /* State of origin */
8764: if(i==1)
8765: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
8766: else
8767: fprintf(ficgp,", '' ");
8768: l=(nlstate+ndeath)*(i-1)+1; /* 1, 1+ nlstate+ndeath, 1+2*(nlstate+ndeath) */
8769: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
8770: for (j=2; j<= nlstate ; j ++)
8771: fprintf(ficgp,"+$%d",k+l+j-1);
8772: fprintf(ficgp,")) t \"prev(%d,%d)\" w l",i,cpt);
8773: } /* nlstate */
8774: fprintf(ficgp,"\nset out; unset label;\n");
8775: } /* end cpt state*/
8776: } /* end covariate */
8777:
8778:
8779: /* 7eme */
8780: if(prevbcast == 1){
8781: /* CV backward prevalence for each covariate */
8782: /* for (k1=1; k1<= m ; k1 ++) /\* For each covariate combination if any *\/ */
8783: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
8784: k1=TKresult[nres];
8785: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
8786: /* if(m != 1 && TKresult[nres]!= k1) */
8787: /* continue; */
8788: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life origin state */
8789: strcpy(gplotlabel,"(");
8790: fprintf(ficgp,"\n#\n#\n#CV Backward stable prevalence: 'pijb' files, covariatecombination#=%d state=%d",k1, cpt);
8791: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
8792: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8793: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8794: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
8795: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
8796: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
8797: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
8798: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
8799: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
8800: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
8801: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
8802: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
8803: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
8804: /* } */
8805: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
8806: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
8807: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
8808: }
8809: strcpy(gplotlabel+strlen(gplotlabel),")");
8810: fprintf(ficgp,"\n#\n");
8811: if(invalidvarcomb[k1]){
8812: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
8813: continue;
8814: }
8815:
8816: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PB_"),cpt,k1,nres);
8817: fprintf(ficgp,"set label \"Origin alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
8818: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
8819: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
8820: k=3; /* Offset */
8821: for (i=1; i<= nlstate ; i ++){ /* State of arrival */
8822: if(i==1)
8823: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJB_"));
8824: else
8825: fprintf(ficgp,", '' ");
8826: /* l=(nlstate+ndeath)*(i-1)+1; */
8827: l=(nlstate+ndeath)*(cpt-1)+1; /* fixed for i; cpt=1 1, cpt=2 1+ nlstate+ndeath, 1+2*(nlstate+ndeath) */
8828: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l); /\* a vérifier *\/ */
8829: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l+(cpt-1)+i-1); /\* a vérifier *\/ */
8830: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d",k1,k+l+i-1); /* To be verified */
8831: /* for (j=2; j<= nlstate ; j ++) */
8832: /* fprintf(ficgp,"+$%d",k+l+j-1); */
8833: /* /\* fprintf(ficgp,"+$%d",k+l+j-1); *\/ */
8834: fprintf(ficgp,") t \"bprev(%d,%d)\" w l",cpt,i);
8835: } /* nlstate */
8836: fprintf(ficgp,"\nset out; unset label;\n");
8837: } /* end cpt state*/
8838: } /* end covariate */
8839: } /* End if prevbcast */
8840:
8841: /* 8eme */
8842: if(prevfcast==1){
8843: /* Projection from cross-sectional to forward stable (period) prevalence for each covariate */
8844:
8845: /* for (k1=1; k1<= m ; k1 ++) /\* For each covariate combination if any *\/ */
8846: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
8847: k1=TKresult[nres];
8848: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
8849: /* if(m != 1 && TKresult[nres]!= k1) */
8850: /* continue; */
8851: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
8852: strcpy(gplotlabel,"(");
8853: fprintf(ficgp,"\n#\n#\n#Projection of prevalence to forward stable prevalence (period): 'PROJ_' files, covariatecombination#=%d state=%d",k1, cpt);
8854: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
8855: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8856: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8857: /* for (k=1; k<=cptcoveff; k++){ /\* For each correspondig covariate value *\/ */
8858: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate value corresponding to k1 combination and kth covariate *\\/ *\/ */
8859: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
8860: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
8861: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
8862: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
8863: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
8864: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
8865: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
8866: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
8867: /* } */
8868: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
8869: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
8870: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
8871: }
8872: strcpy(gplotlabel+strlen(gplotlabel),")");
8873: fprintf(ficgp,"\n#\n");
8874: if(invalidvarcomb[k1]){
8875: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
8876: continue;
8877: }
8878:
8879: fprintf(ficgp,"# hpijx=probability over h years, hp.jx is weighted by observed prev\n ");
8880: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres);
8881: fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
8882: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\
8883: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
8884:
8885: /* for (i=1; i<= nlstate+1 ; i ++){ /\* nlstate +1 p11 p21 p.1 *\/ */
8886: istart=nlstate+1; /* Could be one if by state, but nlstate+1 is w.i projection only */
8887: /*istart=1;*/ /* Could be one if by state, but nlstate+1 is w.i projection only */
8888: for (i=istart; i<= nlstate+1 ; i ++){ /* nlstate +1 p11 p21 p.1 */
8889: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
8890: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
8891: /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
8892: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
8893: if(i==istart){
8894: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"F_"));
8895: }else{
8896: fprintf(ficgp,",\\\n '' ");
8897: }
8898: if(cptcoveff ==0){ /* No covariate */
8899: ioffset=2; /* Age is in 2 */
8900: /*# yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
8901: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
8902: /*# V1 = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
8903: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
8904: fprintf(ficgp," u %d:(", ioffset);
8905: if(i==nlstate+1){
8906: fprintf(ficgp," $%d/(1.-$%d)):1 t 'pw.%d' with line lc variable ", \
8907: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
8908: fprintf(ficgp,",\\\n '' ");
8909: fprintf(ficgp," u %d:(",ioffset);
8910: fprintf(ficgp," (($1-$2) == %d ) ? $%d/(1.-$%d) : 1/0):1 with labels center not ", \
8911: offyear, \
8912: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate );
8913: }else
8914: fprintf(ficgp," $%d/(1.-$%d)) t 'p%d%d' with line ", \
8915: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,i,cpt );
8916: }else{ /* more than 2 covariates */
8917: ioffset=2*cptcoveff+2; /* Age is in 4 or 6 or etc.*/
8918: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
8919: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
8920: iyearc=ioffset-1;
8921: iagec=ioffset;
8922: fprintf(ficgp," u %d:(",ioffset);
8923: kl=0;
8924: strcpy(gplotcondition,"(");
8925: for (k=1; k<=cptcoveff; k++){ /* For each covariate writing the chain of conditions */
8926: /* lv= decodtabm(k1,k,cptcoveff); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
8927: lv=codtabm(k1,TnsdVar[Tvaraff[k]]);
8928: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
8929: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
8930: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
8931: /* vlv= nbcode[Tvaraff[k]][lv]; /\* Value of the modality of Tvaraff[k] *\/ */
8932: vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])];
8933: kl++;
8934: sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,Tvaraff[k], kl+1, nbcode[Tvaraff[k]][lv]);
8935: kl++;
8936: if(k <cptcoveff && cptcoveff>1)
8937: sprintf(gplotcondition+strlen(gplotcondition)," && ");
8938: }
8939: strcpy(gplotcondition+strlen(gplotcondition),")");
8940: /* 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 *\/ */
8941: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
8942: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
8943: /* '' 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*/
8944: if(i==nlstate+1){
8945: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0):%d t 'p.%d' with line lc variable", gplotcondition, \
8946: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,iyearc, cpt );
8947: fprintf(ficgp,",\\\n '' ");
8948: fprintf(ficgp," u %d:(",iagec);
8949: fprintf(ficgp,"%s && (($%d-$%d) == %d ) ? $%d/(1.-$%d) : 1/0):%d with labels center not ", gplotcondition, \
8950: iyearc, iagec, offyear, \
8951: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate, iyearc );
8952: /* '' u 6:(($1==1 && $2==0 && $3==2 && $4==0) && (($5-$6) == 1947) ? $10/(1.-$22) : 1/0):5 with labels center boxed not*/
8953: }else{
8954: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \
8955: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset +1+(i-1)+(nlstate+1)*nlstate,i,cpt );
8956: }
8957: } /* end if covariate */
8958: } /* nlstate */
8959: fprintf(ficgp,"\nset out; unset label;\n");
8960: } /* end cpt state*/
8961: } /* end covariate */
8962: } /* End if prevfcast */
8963:
8964: if(prevbcast==1){
8965: /* Back projection from cross-sectional to stable (mixed) for each covariate */
8966:
8967: /* for (k1=1; k1<= m ; k1 ++) /\* For each covariate combination if any *\/ */
8968: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
8969: k1=TKresult[nres];
8970: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
8971: /* if(m != 1 && TKresult[nres]!= k1) */
8972: /* continue; */
8973: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
8974: strcpy(gplotlabel,"(");
8975: fprintf(ficgp,"\n#\n#\n#Back projection of prevalence to stable (mixed) back prevalence: 'BPROJ_' files, covariatecombination#=%d originstate=%d",k1, cpt);
8976: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
8977: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8978: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
8979: /* for (k=1; k<=cptcoveff; k++){ /\* For each correspondig covariate value *\/ */
8980: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate value corresponding to k1 combination and kth covariate *\\/ *\/ */
8981: /* lv= codtabm(k1,TnsdVar[Tvaraff[k]]); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
8982: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
8983: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
8984: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
8985: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
8986: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
8987: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
8988: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
8989: /* } */
8990: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
8991: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
8992: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
8993: }
8994: strcpy(gplotlabel+strlen(gplotlabel),")");
8995: fprintf(ficgp,"\n#\n");
8996: if(invalidvarcomb[k1]){
8997: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
8998: continue;
8999: }
9000:
9001: fprintf(ficgp,"# hbijx=backprobability over h years, hb.jx is weighted by observed prev at destination state\n ");
9002: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres);
9003: fprintf(ficgp,"set label \"Origin alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
9004: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\
9005: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
9006:
9007: /* for (i=1; i<= nlstate+1 ; i ++){ /\* nlstate +1 p11 p21 p.1 *\/ */
9008: istart=nlstate+1; /* Could be one if by state, but nlstate+1 is w.i projection only */
9009: /*istart=1;*/ /* Could be one if by state, but nlstate+1 is w.i projection only */
9010: for (i=istart; i<= nlstate+1 ; i ++){ /* nlstate +1 p11 p21 p.1 */
9011: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
9012: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
9013: /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
9014: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
9015: if(i==istart){
9016: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"FB_"));
9017: }else{
9018: fprintf(ficgp,",\\\n '' ");
9019: }
9020: if(cptcoveff ==0){ /* No covariate */
9021: ioffset=2; /* Age is in 2 */
9022: /*# yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
9023: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
9024: /*# V1 = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
9025: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
9026: fprintf(ficgp," u %d:(", ioffset);
9027: if(i==nlstate+1){
9028: fprintf(ficgp," $%d/(1.-$%d)):1 t 'bw%d' with line lc variable ", \
9029: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
9030: fprintf(ficgp,",\\\n '' ");
9031: fprintf(ficgp," u %d:(",ioffset);
9032: fprintf(ficgp," (($1-$2) == %d ) ? $%d : 1/0):1 with labels center not ", \
9033: offbyear, \
9034: ioffset+(cpt-1)*(nlstate+1)+1+(i-1) );
9035: }else
9036: fprintf(ficgp," $%d/(1.-$%d)) t 'b%d%d' with line ", \
9037: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt,i );
9038: }else{ /* more than 2 covariates */
9039: ioffset=2*cptcoveff+2; /* Age is in 4 or 6 or etc.*/
9040: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
9041: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
9042: iyearc=ioffset-1;
9043: iagec=ioffset;
9044: fprintf(ficgp," u %d:(",ioffset);
9045: kl=0;
9046: strcpy(gplotcondition,"(");
9047: for (k=1; k<=cptcovs; k++){ /* For each covariate k of the resultline, get corresponding value lv for combination k1 */
9048: if(Dummy[modelresult[nres][k]]==0){ /* To be verified */
9049: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate writing the chain of conditions *\/ */
9050: /* lv= decodtabm(k1,k,cptcoveff); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
9051: /* lv= codtabm(k1,TnsdVar[Tvaraff[k]]); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
9052: lv=Tvresult[nres][k];
9053: vlv=TinvDoQresult[nres][Tvresult[nres][k]];
9054: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
9055: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
9056: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
9057: /* vlv= nbcode[Tvaraff[k]][lv]; /\* Value of the modality of Tvaraff[k] *\/ */
9058: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
9059: kl++;
9060: /* sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,Tvaraff[k], kl+1, nbcode[Tvaraff[k]][lv]); */
9061: sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%lg " ,kl,Tvresult[nres][k], kl+1,TinvDoQresult[nres][Tvresult[nres][k]]);
9062: kl++;
9063: if(k <cptcovs && cptcovs>1)
9064: sprintf(gplotcondition+strlen(gplotcondition)," && ");
9065: }
9066: }
9067: strcpy(gplotcondition+strlen(gplotcondition),")");
9068: /* 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 *\/ */
9069: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
9070: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
9071: /* '' 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*/
9072: if(i==nlstate+1){
9073: fprintf(ficgp,"%s ? $%d : 1/0):%d t 'bw%d' with line lc variable", gplotcondition, \
9074: ioffset+(cpt-1)*(nlstate+1)+1+(i-1),iyearc,cpt );
9075: fprintf(ficgp,",\\\n '' ");
9076: fprintf(ficgp," u %d:(",iagec);
9077: /* fprintf(ficgp,"%s && (($5-$6) == %d ) ? $%d/(1.-$%d) : 1/0):5 with labels center not ", gplotcondition, \ */
9078: fprintf(ficgp,"%s && (($%d-$%d) == %d ) ? $%d : 1/0):%d with labels center not ", gplotcondition, \
9079: iyearc,iagec,offbyear, \
9080: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), iyearc );
9081: /* '' u 6:(($1==1 && $2==0 && $3==2 && $4==0) && (($5-$6) == 1947) ? $10/(1.-$22) : 1/0):5 with labels center boxed not*/
9082: }else{
9083: /* fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \ */
9084: fprintf(ficgp,"%s ? $%d : 1/0) t 'b%d%d' with line ", gplotcondition, \
9085: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), cpt,i );
9086: }
9087: } /* end if covariate */
9088: } /* nlstate */
9089: fprintf(ficgp,"\nset out; unset label;\n");
9090: } /* end cpt state*/
9091: } /* end covariate */
9092: } /* End if prevbcast */
9093:
9094:
9095: /* 9eme writing MLE parameters */
9096: fprintf(ficgp,"\n##############\n#9eme MLE estimated parameters\n#############\n");
9097: for(i=1,jk=1; i <=nlstate; i++){
9098: fprintf(ficgp,"# initial state %d\n",i);
9099: for(k=1; k <=(nlstate+ndeath); k++){
9100: if (k != i) {
9101: fprintf(ficgp,"# current state %d\n",k);
9102: for(j=1; j <=ncovmodel; j++){
9103: fprintf(ficgp,"p%d=%f; ",jk,p[jk]);
9104: jk++;
9105: }
9106: fprintf(ficgp,"\n");
9107: }
9108: }
9109: }
9110: fprintf(ficgp,"##############\n#\n");
9111:
9112: /*goto avoid;*/
9113: /* 10eme Graphics of probabilities or incidences using written MLE parameters */
9114: fprintf(ficgp,"\n##############\n#10eme Graphics of probabilities or incidences\n#############\n");
9115: fprintf(ficgp,"# logi(p12/p11)=a12+b12*age+c12age*age+d12*V1+e12*V1*age\n");
9116: fprintf(ficgp,"# logi(p12/p11)=p1 +p2*age +p3*age*age+ p4*V1+ p5*V1*age\n");
9117: fprintf(ficgp,"# logi(p13/p11)=a13+b13*age+c13age*age+d13*V1+e13*V1*age\n");
9118: fprintf(ficgp,"# logi(p13/p11)=p6 +p7*age +p8*age*age+ p9*V1+ p10*V1*age\n");
9119: fprintf(ficgp,"# p12+p13+p14+p11=1=p11(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
9120: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
9121: fprintf(ficgp,"# p11=1/(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
9122: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
9123: fprintf(ficgp,"# p12=exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)/\n");
9124: fprintf(ficgp,"# (1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
9125: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age))\n");
9126: fprintf(ficgp,"# +exp(a14+b14*age+c14age*age+d14*V1+e14*V1*age)+...)\n");
9127: fprintf(ficgp,"#\n");
9128: for(ng=1; ng<=3;ng++){ /* Number of graphics: first is logit, 2nd is probabilities, third is incidences per year*/
9129: fprintf(ficgp,"#Number of graphics: first is logit, 2nd is probabilities, third is incidences per year\n");
9130: fprintf(ficgp,"#model=1+age+%s \n",model);
9131: fprintf(ficgp,"# Type of graphic ng=%d\n",ng);
9132: fprintf(ficgp,"# k1=1 to 2^%d=%d\n",cptcoveff,m);/* to be checked */
9133: /* for(k1=1; k1 <=m; k1++) /\* For each combination of covariate *\/ */
9134: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
9135: /* k1=nres; */
9136: k1=TKresult[nres];
9137: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
9138: fprintf(ficgp,"\n\n# Resultline k1=%d ",k1);
9139: strcpy(gplotlabel,"(");
9140: /*sprintf(gplotlabel+strlen(gplotlabel)," Dummy combination %d ",k1);*/
9141: for (k=1; k<=cptcovs; k++){ /**< cptcovs number of SIMPLE covariates in the model V2+V1 =2 (dummy or quantit or time varying) */
9142: /* for each resultline nres, and position k, Tvresult[nres][k] gives the name of the variable and
9143: TinvDoQresult[nres][Tvresult[nres][k]] gives its value double or integer) */
9144: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
9145: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
9146: }
9147: /* if(m != 1 && TKresult[nres]!= k1) */
9148: /* continue; */
9149: /* fprintf(ficgp,"\n\n# Combination of dummy k1=%d which is ",k1); */
9150: /* strcpy(gplotlabel,"("); */
9151: /* /\*sprintf(gplotlabel+strlen(gplotlabel)," Dummy combination %d ",k1);*\/ */
9152: /* for (k=1; k<=cptcoveff; k++){ /\* For each correspondig covariate value *\/ */
9153: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate value corresponding to k1 combination and kth covariate *\\/ *\/ */
9154: /* lv= codtabm(k1,TnsdVar[Tvaraff[k]]); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
9155: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
9156: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
9157: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
9158: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
9159: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
9160: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
9161: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
9162: /* } */
9163: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
9164: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
9165: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
9166: /* } */
9167: strcpy(gplotlabel+strlen(gplotlabel),")");
9168: fprintf(ficgp,"\n#\n");
9169: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" ",subdirf2(optionfilefiname,"PE_"),k1,ng,nres);
9170: fprintf(ficgp,"\nset key outside ");
9171: /* fprintf(ficgp,"\nset label \"%s\" at graph 1.2,0.5 center rotate font \"Helvetica,12\"\n",gplotlabel); */
9172: fprintf(ficgp,"\nset title \"%s\" font \"Helvetica,12\"\n",gplotlabel);
9173: fprintf(ficgp,"\nset ter svg size 640, 480 ");
9174: if (ng==1){
9175: fprintf(ficgp,"\nset ylabel \"Value of the logit of the model\"\n"); /* exp(a12+b12*x) could be nice */
9176: fprintf(ficgp,"\nunset log y");
9177: }else if (ng==2){
9178: fprintf(ficgp,"\nset ylabel \"Probability\"\n");
9179: fprintf(ficgp,"\nset log y");
9180: }else if (ng==3){
9181: fprintf(ficgp,"\nset ylabel \"Quasi-incidence per year\"\n");
9182: fprintf(ficgp,"\nset log y");
9183: }else
9184: fprintf(ficgp,"\nunset title ");
9185: fprintf(ficgp,"\nplot [%.f:%.f] ",ageminpar,agemaxpar);
9186: i=1;
9187: for(k2=1; k2<=nlstate; k2++) {
9188: k3=i;
9189: for(k=1; k<=(nlstate+ndeath); k++) {
9190: if (k != k2){
9191: switch( ng) {
9192: case 1:
9193: if(nagesqr==0)
9194: fprintf(ficgp," p%d+p%d*x",i,i+1);
9195: else /* nagesqr =1 */
9196: fprintf(ficgp," p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
9197: break;
9198: case 2: /* ng=2 */
9199: if(nagesqr==0)
9200: fprintf(ficgp," exp(p%d+p%d*x",i,i+1);
9201: else /* nagesqr =1 */
9202: fprintf(ficgp," exp(p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
9203: break;
9204: case 3:
9205: if(nagesqr==0)
9206: fprintf(ficgp," %f*exp(p%d+p%d*x",YEARM/stepm,i,i+1);
9207: else /* nagesqr =1 */
9208: fprintf(ficgp," %f*exp(p%d+p%d*x+p%d*x*x",YEARM/stepm,i,i+1,i+1+nagesqr);
9209: break;
9210: }
9211: ij=1;/* To be checked else nbcode[0][0] wrong */
9212: ijp=1; /* product no age */
9213: /* for(j=3; j <=ncovmodel-nagesqr; j++) { */
9214: for(j=1; j <=cptcovt; j++) { /* For each covariate of the simplified model */
9215: /* printf("Tage[%d]=%d, j=%d\n", ij, Tage[ij], j); */
9216: switch(Typevar[j]){
9217: case 1:
9218: if(cptcovage >0){ /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, 2 V5 and V1 */
9219: if(j==Tage[ij]) { /* Product by age To be looked at!!*//* Bug valgrind */
9220: if(ij <=cptcovage) { /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, 2 V5 and V1 */
9221: if(DummyV[j]==0){/* Bug valgrind */
9222: fprintf(ficgp,"+p%d*%d*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]);;
9223: }else{ /* quantitative */
9224: fprintf(ficgp,"+p%d*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /* Tqinvresult in decoderesult */
9225: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
9226: }
9227: ij++;
9228: }
9229: }
9230: }
9231: break;
9232: case 2:
9233: if(cptcovprod >0){
9234: if(j==Tprod[ijp]) { /* */
9235: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
9236: if(ijp <=cptcovprod) { /* Product */
9237: if(DummyV[Tvard[ijp][1]]==0){/* Vn is dummy */
9238: if(DummyV[Tvard[ijp][2]]==0){/* Vn and Vm are dummy */
9239: /* 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)]); */
9240: fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]);
9241: }else{ /* Vn is dummy and Vm is quanti */
9242: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],Tqinvresult[nres][Tvard[ijp][2]]); */
9243: fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
9244: }
9245: }else{ /* Vn*Vm Vn is quanti */
9246: if(DummyV[Tvard[ijp][2]]==0){
9247: fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]);
9248: }else{ /* Both quanti */
9249: fprintf(ficgp,"+p%d*%f*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
9250: }
9251: }
9252: ijp++;
9253: }
9254: } /* end Tprod */
9255: }
9256: break;
9257: case 0:
9258: /* simple covariate */
9259: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,nbcode[Tvar[j]][codtabm(k1,j)]); /\* Valgrind bug nbcode *\/ */
9260: if(Dummy[j]==0){
9261: fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]); /* */
9262: }else{ /* quantitative */
9263: fprintf(ficgp,"+p%d*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /* */
9264: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
9265: }
9266: /* end simple */
9267: break;
9268: default:
9269: break;
9270: } /* end switch */
9271: } /* end j */
9272: }else{ /* k=k2 */
9273: if(ng !=1 ){ /* For logit formula of log p11 is more difficult to get */
9274: fprintf(ficgp," (1.");i=i-ncovmodel;
9275: }else
9276: i=i-ncovmodel;
9277: }
9278:
9279: if(ng != 1){
9280: fprintf(ficgp,")/(1");
9281:
9282: for(cpt=1; cpt <=nlstate; cpt++){
9283: if(nagesqr==0)
9284: fprintf(ficgp,"+exp(p%d+p%d*x",k3+(cpt-1)*ncovmodel,k3+(cpt-1)*ncovmodel+1);
9285: else /* nagesqr =1 */
9286: 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);
9287:
9288: ij=1;
9289: ijp=1;
9290: /* for(j=3; j <=ncovmodel-nagesqr; j++){ */
9291: for(j=1; j <=cptcovt; j++) { /* For each covariate of the simplified model */
9292: switch(Typevar[j]){
9293: case 1:
9294: if(cptcovage >0){
9295: if(j==Tage[ij]) { /* Bug valgrind */
9296: if(ij <=cptcovage) { /* Bug valgrind */
9297: if(DummyV[j]==0){/* Bug valgrind */
9298: /* fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,j-2)]); */
9299: /* fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j+nagesqr,nbcode[Tvar[j]][codtabm(k1,j)]); */
9300: fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvar[j]]);
9301: /* fprintf(ficgp,"+p%d*%d*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]);; */
9302: /* fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
9303: }else{ /* quantitative */
9304: /* fprintf(ficgp,"+p%d*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /\* Tqinvresult in decoderesult *\/ */
9305: fprintf(ficgp,"+p%d*%f*x",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tqinvresult[nres][Tvar[j]]); /* Tqinvresult in decoderesult */
9306: /* fprintf(ficgp,"+p%d*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /\* Tqinvresult in decoderesult *\/ */
9307: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
9308: }
9309: ij++;
9310: }
9311: }
9312: }
9313: break;
9314: case 2:
9315: if(cptcovprod >0){
9316: if(j==Tprod[ijp]) { /* */
9317: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
9318: if(ijp <=cptcovprod) { /* Product */
9319: if(DummyV[Tvard[ijp][1]]==0){/* Vn is dummy */
9320: if(DummyV[Tvard[ijp][2]]==0){/* Vn and Vm are dummy */
9321: /* 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)]); */
9322: fprintf(ficgp,"+p%d*%d*%d",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]);
9323: /* fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]); */
9324: }else{ /* Vn is dummy and Vm is quanti */
9325: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],Tqinvresult[nres][Tvard[ijp][2]]); */
9326: fprintf(ficgp,"+p%d*%d*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
9327: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]); */
9328: }
9329: }else{ /* Vn*Vm Vn is quanti */
9330: if(DummyV[Tvard[ijp][2]]==0){
9331: fprintf(ficgp,"+p%d*%d*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]);
9332: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]); */
9333: }else{ /* Both quanti */
9334: fprintf(ficgp,"+p%d*%f*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
9335: /* fprintf(ficgp,"+p%d*%f*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]); */
9336: }
9337: }
9338: ijp++;
9339: }
9340: } /* end Tprod */
9341: } /* end if */
9342: break;
9343: case 0:
9344: /* simple covariate */
9345: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,nbcode[Tvar[j]][codtabm(k1,j)]); /\* Valgrind bug nbcode *\/ */
9346: if(Dummy[j]==0){
9347: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]); /\* *\/ */
9348: fprintf(ficgp,"+p%d*%d",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvar[j]]); /* */
9349: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]); /\* *\/ */
9350: }else{ /* quantitative */
9351: fprintf(ficgp,"+p%d*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tqinvresult[nres][Tvar[j]]); /* */
9352: /* fprintf(ficgp,"+p%d*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /\* *\/ */
9353: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
9354: }
9355: /* end simple */
9356: /* fprintf(ficgp,"+p%d*%d",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,j-2)]);/\* Valgrind bug nbcode *\/ */
9357: break;
9358: default:
9359: break;
9360: } /* end switch */
9361: }
9362: fprintf(ficgp,")");
9363: }
9364: fprintf(ficgp,")");
9365: if(ng ==2)
9366: 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);
9367: else /* ng= 3 */
9368: 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);
9369: }else{ /* end ng <> 1 */
9370: if( k !=k2) /* logit p11 is hard to draw */
9371: 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);
9372: }
9373: if ((k+k2)!= (nlstate*2+ndeath) && ng != 1)
9374: fprintf(ficgp,",");
9375: if (ng == 1 && k!=k2 && (k+k2)!= (nlstate*2+ndeath))
9376: fprintf(ficgp,",");
9377: i=i+ncovmodel;
9378: } /* end k */
9379: } /* end k2 */
9380: /* fprintf(ficgp,"\n set out; unset label;set key default;\n"); */
9381: fprintf(ficgp,"\n set out; unset title;set key default;\n");
9382: } /* end resultline */
9383: } /* end ng */
9384: /* avoid: */
9385: fflush(ficgp);
9386: } /* end gnuplot */
9387:
9388:
9389: /*************** Moving average **************/
9390: /* int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav, double bageout, double fageout){ */
9391: int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav){
9392:
9393: int i, cpt, cptcod;
9394: int modcovmax =1;
9395: int mobilavrange, mob;
9396: int iage=0;
9397: int firstA1=0, firstA2=0;
9398:
9399: double sum=0., sumr=0.;
9400: double age;
9401: double *sumnewp, *sumnewm, *sumnewmr;
9402: double *agemingood, *agemaxgood;
9403: double *agemingoodr, *agemaxgoodr;
9404:
9405:
9406: /* modcovmax=2*cptcoveff; Max number of modalities. We suppose */
9407: /* a covariate has 2 modalities, should be equal to ncovcombmax */
9408:
9409: sumnewp = vector(1,ncovcombmax);
9410: sumnewm = vector(1,ncovcombmax);
9411: sumnewmr = vector(1,ncovcombmax);
9412: agemingood = vector(1,ncovcombmax);
9413: agemingoodr = vector(1,ncovcombmax);
9414: agemaxgood = vector(1,ncovcombmax);
9415: agemaxgoodr = vector(1,ncovcombmax);
9416:
9417: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
9418: sumnewm[cptcod]=0.; sumnewmr[cptcod]=0.;
9419: sumnewp[cptcod]=0.;
9420: agemingood[cptcod]=0, agemingoodr[cptcod]=0;
9421: agemaxgood[cptcod]=0, agemaxgoodr[cptcod]=0;
9422: }
9423: if (cptcovn<1) ncovcombmax=1; /* At least 1 pass */
9424:
9425: if(mobilav==-1 || mobilav==1||mobilav ==3 ||mobilav==5 ||mobilav== 7){
9426: if(mobilav==1 || mobilav==-1) mobilavrange=5; /* default */
9427: else mobilavrange=mobilav;
9428: for (age=bage; age<=fage; age++)
9429: for (i=1; i<=nlstate;i++)
9430: for (cptcod=1;cptcod<=ncovcombmax;cptcod++)
9431: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
9432: /* We keep the original values on the extreme ages bage, fage and for
9433: fage+1 and bage-1 we use a 3 terms moving average; for fage+2 bage+2
9434: we use a 5 terms etc. until the borders are no more concerned.
9435: */
9436: for (mob=3;mob <=mobilavrange;mob=mob+2){
9437: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){
9438: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
9439: sumnewm[cptcod]=0.;
9440: for (i=1; i<=nlstate;i++){
9441: mobaverage[(int)age][i][cptcod] =probs[(int)age][i][cptcod];
9442: for (cpt=1;cpt<=(mob-1)/2;cpt++){
9443: mobaverage[(int)age][i][cptcod] +=probs[(int)age-cpt][i][cptcod];
9444: mobaverage[(int)age][i][cptcod] +=probs[(int)age+cpt][i][cptcod];
9445: }
9446: mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/mob;
9447: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
9448: } /* end i */
9449: if(sumnewm[cptcod] >1.e-3) mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/sumnewm[cptcod]; /* Rescaling to sum one */
9450: } /* end cptcod */
9451: }/* end age */
9452: }/* end mob */
9453: }else{
9454: printf("Error internal in movingaverage, mobilav=%d.\n",mobilav);
9455: return -1;
9456: }
9457:
9458: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){ /* for each combination */
9459: /* for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ */
9460: if(invalidvarcomb[cptcod]){
9461: printf("\nCombination (%d) ignored because no cases \n",cptcod);
9462: continue;
9463: }
9464:
9465: for (age=fage-(mob-1)/2; age>=bage+(mob-1)/2; age--){ /*looking for the youngest and oldest good age */
9466: sumnewm[cptcod]=0.;
9467: sumnewmr[cptcod]=0.;
9468: for (i=1; i<=nlstate;i++){
9469: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
9470: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
9471: }
9472: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
9473: agemingoodr[cptcod]=age;
9474: }
9475: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
9476: agemingood[cptcod]=age;
9477: }
9478: } /* age */
9479: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ /*looking for the youngest and oldest good age */
9480: sumnewm[cptcod]=0.;
9481: sumnewmr[cptcod]=0.;
9482: for (i=1; i<=nlstate;i++){
9483: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
9484: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
9485: }
9486: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
9487: agemaxgoodr[cptcod]=age;
9488: }
9489: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
9490: agemaxgood[cptcod]=age;
9491: }
9492: } /* age */
9493: /* Thus we have agemingood and agemaxgood as well as goodr for raw (preobs) */
9494: /* but they will change */
9495: firstA1=0;firstA2=0;
9496: for (age=fage-(mob-1)/2; age>=bage; age--){/* From oldest to youngest, filling up to the youngest */
9497: sumnewm[cptcod]=0.;
9498: sumnewmr[cptcod]=0.;
9499: for (i=1; i<=nlstate;i++){
9500: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
9501: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
9502: }
9503: if(mobilav==-1){ /* Forcing raw ages if good else agemingood */
9504: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
9505: agemaxgoodr[cptcod]=age; /* age min */
9506: for (i=1; i<=nlstate;i++)
9507: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
9508: }else{ /* bad we change the value with the values of good ages */
9509: for (i=1; i<=nlstate;i++){
9510: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgoodr[cptcod]][i][cptcod];
9511: } /* i */
9512: } /* end bad */
9513: }else{
9514: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
9515: agemaxgood[cptcod]=age;
9516: }else{ /* bad we change the value with the values of good ages */
9517: for (i=1; i<=nlstate;i++){
9518: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
9519: } /* i */
9520: } /* end bad */
9521: }/* end else */
9522: sum=0.;sumr=0.;
9523: for (i=1; i<=nlstate;i++){
9524: sum+=mobaverage[(int)age][i][cptcod];
9525: sumr+=probs[(int)age][i][cptcod];
9526: }
9527: if(fabs(sum - 1.) > 1.e-3) { /* bad */
9528: if(!firstA1){
9529: firstA1=1;
9530: 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);
9531: }
9532: 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);
9533: } /* end bad */
9534: /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */
9535: if(fabs(sumr - 1.) > 1.e-3) { /* bad */
9536: if(!firstA2){
9537: firstA2=1;
9538: 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);
9539: }
9540: 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);
9541: } /* end bad */
9542: }/* age */
9543:
9544: for (age=bage+(mob-1)/2; age<=fage; age++){/* From youngest, finding the oldest wrong */
9545: sumnewm[cptcod]=0.;
9546: sumnewmr[cptcod]=0.;
9547: for (i=1; i<=nlstate;i++){
9548: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
9549: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
9550: }
9551: if(mobilav==-1){ /* Forcing raw ages if good else agemingood */
9552: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good */
9553: agemingoodr[cptcod]=age;
9554: for (i=1; i<=nlstate;i++)
9555: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
9556: }else{ /* bad we change the value with the values of good ages */
9557: for (i=1; i<=nlstate;i++){
9558: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingoodr[cptcod]][i][cptcod];
9559: } /* i */
9560: } /* end bad */
9561: }else{
9562: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
9563: agemingood[cptcod]=age;
9564: }else{ /* bad */
9565: for (i=1; i<=nlstate;i++){
9566: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod];
9567: } /* i */
9568: } /* end bad */
9569: }/* end else */
9570: sum=0.;sumr=0.;
9571: for (i=1; i<=nlstate;i++){
9572: sum+=mobaverage[(int)age][i][cptcod];
9573: sumr+=mobaverage[(int)age][i][cptcod];
9574: }
9575: if(fabs(sum - 1.) > 1.e-3) { /* bad */
9576: 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);
9577: } /* end bad */
9578: /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */
9579: if(fabs(sumr - 1.) > 1.e-3) { /* bad */
9580: 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);
9581: } /* end bad */
9582: }/* age */
9583:
9584:
9585: for (age=bage; age<=fage; age++){
9586: /* printf("%d %d ", cptcod, (int)age); */
9587: sumnewp[cptcod]=0.;
9588: sumnewm[cptcod]=0.;
9589: for (i=1; i<=nlstate;i++){
9590: sumnewp[cptcod]+=probs[(int)age][i][cptcod];
9591: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
9592: /* printf("%.4f %.4f ",probs[(int)age][i][cptcod], mobaverage[(int)age][i][cptcod]); */
9593: }
9594: /* printf("%.4f %.4f \n",sumnewp[cptcod], sumnewm[cptcod]); */
9595: }
9596: /* printf("\n"); */
9597: /* } */
9598:
9599: /* brutal averaging */
9600: /* for (i=1; i<=nlstate;i++){ */
9601: /* for (age=1; age<=bage; age++){ */
9602: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod]; */
9603: /* /\* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); *\/ */
9604: /* } */
9605: /* for (age=fage; age<=AGESUP; age++){ */
9606: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod]; */
9607: /* /\* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); *\/ */
9608: /* } */
9609: /* } /\* end i status *\/ */
9610: /* for (i=nlstate+1; i<=nlstate+ndeath;i++){ */
9611: /* for (age=1; age<=AGESUP; age++){ */
9612: /* /\*printf("i=%d, age=%d, cptcod=%d\n",i, (int)age, cptcod);*\/ */
9613: /* mobaverage[(int)age][i][cptcod]=0.; */
9614: /* } */
9615: /* } */
9616: }/* end cptcod */
9617: free_vector(agemaxgoodr,1, ncovcombmax);
9618: free_vector(agemaxgood,1, ncovcombmax);
9619: free_vector(agemingood,1, ncovcombmax);
9620: free_vector(agemingoodr,1, ncovcombmax);
9621: free_vector(sumnewmr,1, ncovcombmax);
9622: free_vector(sumnewm,1, ncovcombmax);
9623: free_vector(sumnewp,1, ncovcombmax);
9624: return 0;
9625: }/* End movingaverage */
9626:
9627:
9628:
9629: /************** Forecasting ******************/
9630: /* 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)*/
9631: 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){
9632: /* dateintemean, mean date of interviews
9633: dateprojd, year, month, day of starting projection
9634: dateprojf date of end of projection;year of end of projection (same day and month as proj1).
9635: agemin, agemax range of age
9636: dateprev1 dateprev2 range of dates during which prevalence is computed
9637: */
9638: /* double anprojd, mprojd, jprojd; */
9639: /* double anprojf, mprojf, jprojf; */
9640: int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1, k4, nres=0;
9641: double agec; /* generic age */
9642: double agelim, ppij, yp,yp1,yp2;
9643: double *popeffectif,*popcount;
9644: double ***p3mat;
9645: /* double ***mobaverage; */
9646: char fileresf[FILENAMELENGTH];
9647:
9648: agelim=AGESUP;
9649: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
9650: in each health status at the date of interview (if between dateprev1 and dateprev2).
9651: We still use firstpass and lastpass as another selection.
9652: */
9653: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ */
9654: /* firstpass, lastpass, stepm, weightopt, model); */
9655:
9656: strcpy(fileresf,"F_");
9657: strcat(fileresf,fileresu);
9658: if((ficresf=fopen(fileresf,"w"))==NULL) {
9659: printf("Problem with forecast resultfile: %s\n", fileresf);
9660: fprintf(ficlog,"Problem with forecast resultfile: %s\n", fileresf);
9661: }
9662: printf("\nComputing forecasting: result on file '%s', please wait... \n", fileresf);
9663: fprintf(ficlog,"\nComputing forecasting: result on file '%s', please wait... \n", fileresf);
9664:
9665: if (cptcoveff==0) ncodemax[cptcoveff]=1;
9666:
9667:
9668: stepsize=(int) (stepm+YEARM-1)/YEARM;
9669: if (stepm<=12) stepsize=1;
9670: if(estepm < stepm){
9671: printf ("Problem %d lower than %d\n",estepm, stepm);
9672: }
9673: else{
9674: hstepm=estepm;
9675: }
9676: if(estepm > stepm){ /* Yes every two year */
9677: stepsize=2;
9678: }
9679: hstepm=hstepm/stepm;
9680:
9681:
9682: /* yp1=modf(dateintmean,&yp);/\* extracts integral of datemean in yp and */
9683: /* fractional in yp1 *\/ */
9684: /* aintmean=yp; */
9685: /* yp2=modf((yp1*12),&yp); */
9686: /* mintmean=yp; */
9687: /* yp1=modf((yp2*30.5),&yp); */
9688: /* jintmean=yp; */
9689: /* if(jintmean==0) jintmean=1; */
9690: /* if(mintmean==0) mintmean=1; */
9691:
9692:
9693: /* date2dmy(dateintmean,&jintmean,&mintmean,&aintmean); */
9694: /* date2dmy(dateprojd,&jprojd, &mprojd, &anprojd); */
9695: /* date2dmy(dateprojf,&jprojf, &mprojf, &anprojf); */
9696: i1=pow(2,cptcoveff);
9697: if (cptcovn < 1){i1=1;}
9698:
9699: fprintf(ficresf,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
9700:
9701: fprintf(ficresf,"#****** Routine prevforecast **\n");
9702:
9703: /* if (h==(int)(YEARM*yearp)){ */
9704: for(nres=1; nres <= nresult; nres++) /* For each resultline */
9705: 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) */
9706: if(i1 != 1 && TKresult[nres]!= k)
9707: continue;
9708: if(invalidvarcomb[k]){
9709: printf("\nCombination (%d) projection ignored because no cases \n",k);
9710: continue;
9711: }
9712: fprintf(ficresf,"\n#****** hpijx=probability over h years, hp.jx is weighted by observed prev \n#");
9713: for(j=1;j<=cptcoveff;j++) {
9714: /* fprintf(ficresf," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,Tvaraff[j])]); */
9715: fprintf(ficresf," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]);
9716: }
9717: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
9718: fprintf(ficresf," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
9719: }
9720: fprintf(ficresf," yearproj age");
9721: for(j=1; j<=nlstate+ndeath;j++){
9722: for(i=1; i<=nlstate;i++)
9723: fprintf(ficresf," p%d%d",i,j);
9724: fprintf(ficresf," wp.%d",j);
9725: }
9726: for (yearp=0; yearp<=(anprojf-anprojd);yearp +=stepsize) {
9727: fprintf(ficresf,"\n");
9728: fprintf(ficresf,"\n# Forecasting at date %.lf/%.lf/%.lf ",jprojd,mprojd,anprojd+yearp);
9729: /* for (agec=fage; agec>=(ageminpar-1); agec--){ */
9730: for (agec=fage; agec>=(bage); agec--){
9731: nhstepm=(int) rint((agelim-agec)*YEARM/stepm);
9732: nhstepm = nhstepm/hstepm;
9733: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
9734: oldm=oldms;savm=savms;
9735: /* We compute pii at age agec over nhstepm);*/
9736: hpxij(p3mat,nhstepm,agec,hstepm,p,nlstate,stepm,oldm,savm, k,nres);
9737: /* Then we print p3mat for h corresponding to the right agec+h*stepms=yearp */
9738: for (h=0; h<=nhstepm; h++){
9739: if (h*hstepm/YEARM*stepm ==yearp) {
9740: break;
9741: }
9742: }
9743: fprintf(ficresf,"\n");
9744: for(j=1;j<=cptcoveff;j++)
9745: /* fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,Tvaraff[j])]); /\* Tvaraff not correct *\/ */
9746: fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); /* TnsdVar[Tvaraff] correct */
9747: fprintf(ficresf,"%.f %.f ",anprojd+yearp,agec+h*hstepm/YEARM*stepm);
9748:
9749: for(j=1; j<=nlstate+ndeath;j++) {
9750: ppij=0.;
9751: for(i=1; i<=nlstate;i++) {
9752: if (mobilav>=1)
9753: ppij=ppij+p3mat[i][j][h]*prev[(int)agec][i][k];
9754: else { /* even if mobilav==-1 we use mobaverage, probs may not sums to 1 */
9755: ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][k];
9756: }
9757: fprintf(ficresf," %.3f", p3mat[i][j][h]);
9758: } /* end i */
9759: fprintf(ficresf," %.3f", ppij);
9760: }/* end j */
9761: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
9762: } /* end agec */
9763: /* diffyear=(int) anproj1+yearp-ageminpar-1; */
9764: /*printf("Prevforecast %d+%d-%d=diffyear=%d\n",(int) anproj1, (int)yearp,(int)ageminpar,(int) anproj1-(int)ageminpar);*/
9765: } /* end yearp */
9766: } /* end k */
9767:
9768: fclose(ficresf);
9769: printf("End of Computing forecasting \n");
9770: fprintf(ficlog,"End of Computing forecasting\n");
9771:
9772: }
9773:
9774: /************** Back Forecasting ******************/
9775: /* 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){ */
9776: 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){
9777: /* back1, year, month, day of starting backprojection
9778: agemin, agemax range of age
9779: dateprev1 dateprev2 range of dates during which prevalence is computed
9780: anback2 year of end of backprojection (same day and month as back1).
9781: prevacurrent and prev are prevalences.
9782: */
9783: int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1, k4, nres=0;
9784: double agec; /* generic age */
9785: double agelim, ppij, ppi, yp,yp1,yp2; /* ,jintmean,mintmean,aintmean;*/
9786: double *popeffectif,*popcount;
9787: double ***p3mat;
9788: /* double ***mobaverage; */
9789: char fileresfb[FILENAMELENGTH];
9790:
9791: agelim=AGEINF;
9792: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
9793: in each health status at the date of interview (if between dateprev1 and dateprev2).
9794: We still use firstpass and lastpass as another selection.
9795: */
9796: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ */
9797: /* firstpass, lastpass, stepm, weightopt, model); */
9798:
9799: /*Do we need to compute prevalence again?*/
9800:
9801: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
9802:
9803: strcpy(fileresfb,"FB_");
9804: strcat(fileresfb,fileresu);
9805: if((ficresfb=fopen(fileresfb,"w"))==NULL) {
9806: printf("Problem with back forecast resultfile: %s\n", fileresfb);
9807: fprintf(ficlog,"Problem with back forecast resultfile: %s\n", fileresfb);
9808: }
9809: printf("\nComputing back forecasting: result on file '%s', please wait... \n", fileresfb);
9810: fprintf(ficlog,"\nComputing back forecasting: result on file '%s', please wait... \n", fileresfb);
9811:
9812: if (cptcoveff==0) ncodemax[cptcoveff]=1;
9813:
9814:
9815: stepsize=(int) (stepm+YEARM-1)/YEARM;
9816: if (stepm<=12) stepsize=1;
9817: if(estepm < stepm){
9818: printf ("Problem %d lower than %d\n",estepm, stepm);
9819: }
9820: else{
9821: hstepm=estepm;
9822: }
9823: if(estepm >= stepm){ /* Yes every two year */
9824: stepsize=2;
9825: }
9826:
9827: hstepm=hstepm/stepm;
9828: /* yp1=modf(dateintmean,&yp);/\* extracts integral of datemean in yp and */
9829: /* fractional in yp1 *\/ */
9830: /* aintmean=yp; */
9831: /* yp2=modf((yp1*12),&yp); */
9832: /* mintmean=yp; */
9833: /* yp1=modf((yp2*30.5),&yp); */
9834: /* jintmean=yp; */
9835: /* if(jintmean==0) jintmean=1; */
9836: /* if(mintmean==0) jintmean=1; */
9837:
9838: i1=pow(2,cptcoveff);
9839: if (cptcovn < 1){i1=1;}
9840:
9841: fprintf(ficresfb,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
9842: printf("# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
9843:
9844: fprintf(ficresfb,"#****** Routine prevbackforecast **\n");
9845:
9846: for(nres=1; nres <= nresult; nres++) /* For each resultline */
9847: for(k=1; k<=i1;k++){
9848: if(i1 != 1 && TKresult[nres]!= k)
9849: continue;
9850: if(invalidvarcomb[k]){
9851: printf("\nCombination (%d) projection ignored because no cases \n",k);
9852: continue;
9853: }
9854: fprintf(ficresfb,"\n#****** hbijx=probability over h years, hb.jx is weighted by observed prev \n#");
9855: for(j=1;j<=cptcoveff;j++) {
9856: fprintf(ficresfb," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]);
9857: }
9858: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
9859: fprintf(ficresf," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
9860: }
9861: fprintf(ficresfb," yearbproj age");
9862: for(j=1; j<=nlstate+ndeath;j++){
9863: for(i=1; i<=nlstate;i++)
9864: fprintf(ficresfb," b%d%d",i,j);
9865: fprintf(ficresfb," b.%d",j);
9866: }
9867: for (yearp=0; yearp>=(anbackf-anbackd);yearp -=stepsize) {
9868: /* for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) { */
9869: fprintf(ficresfb,"\n");
9870: fprintf(ficresfb,"\n# Back Forecasting at date %.lf/%.lf/%.lf ",jbackd,mbackd,anbackd+yearp);
9871: /* printf("\n# Back Forecasting at date %.lf/%.lf/%.lf ",jback1,mback1,anback1+yearp); */
9872: /* for (agec=bage; agec<=agemax-1; agec++){ /\* testing *\/ */
9873: for (agec=bage; agec<=fage; agec++){ /* testing */
9874: /* We compute bij at age agec over nhstepm, nhstepm decreases when agec increases because of agemax;*/
9875: nhstepm=(int) (agec-agelim) *YEARM/stepm;/* nhstepm=(int) rint((agec-agelim)*YEARM/stepm);*/
9876: nhstepm = nhstepm/hstepm;
9877: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
9878: oldm=oldms;savm=savms;
9879: /* computes hbxij at age agec over 1 to nhstepm */
9880: /* printf("####prevbackforecast debug agec=%.2f nhstepm=%d\n",agec, nhstepm);fflush(stdout); */
9881: hbxij(p3mat,nhstepm,agec,hstepm,p,prevacurrent,nlstate,stepm, k, nres);
9882: /* hpxij(p3mat,nhstepm,agec,hstepm,p, nlstate,stepm,oldm,savm, k,nres); */
9883: /* Then we print p3mat for h corresponding to the right agec+h*stepms=yearp */
9884: /* printf(" agec=%.2f\n",agec);fflush(stdout); */
9885: for (h=0; h<=nhstepm; h++){
9886: if (h*hstepm/YEARM*stepm ==-yearp) {
9887: break;
9888: }
9889: }
9890: fprintf(ficresfb,"\n");
9891: for(j=1;j<=cptcoveff;j++)
9892: fprintf(ficresfb,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]);
9893: fprintf(ficresfb,"%.f %.f ",anbackd+yearp,agec-h*hstepm/YEARM*stepm);
9894: for(i=1; i<=nlstate+ndeath;i++) {
9895: ppij=0.;ppi=0.;
9896: for(j=1; j<=nlstate;j++) {
9897: /* if (mobilav==1) */
9898: ppij=ppij+p3mat[i][j][h]*prevacurrent[(int)agec][j][k];
9899: ppi=ppi+prevacurrent[(int)agec][j][k];
9900: /* ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][j][k]; */
9901: /* ppi=ppi+mobaverage[(int)agec][j][k]; */
9902: /* else { */
9903: /* ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][k]; */
9904: /* } */
9905: fprintf(ficresfb," %.3f", p3mat[i][j][h]);
9906: } /* end j */
9907: if(ppi <0.99){
9908: printf("Error in prevbackforecast, prevalence doesn't sum to 1 for state %d: %3f\n",i, ppi);
9909: fprintf(ficlog,"Error in prevbackforecast, prevalence doesn't sum to 1 for state %d: %3f\n",i, ppi);
9910: }
9911: fprintf(ficresfb," %.3f", ppij);
9912: }/* end j */
9913: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
9914: } /* end agec */
9915: } /* end yearp */
9916: } /* end k */
9917:
9918: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
9919:
9920: fclose(ficresfb);
9921: printf("End of Computing Back forecasting \n");
9922: fprintf(ficlog,"End of Computing Back forecasting\n");
9923:
9924: }
9925:
9926: /* Variance of prevalence limit: varprlim */
9927: 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){
9928: /*------- Variance of forward period (stable) prevalence------*/
9929:
9930: char fileresvpl[FILENAMELENGTH];
9931: FILE *ficresvpl;
9932: double **oldm, **savm;
9933: double **varpl; /* Variances of prevalence limits by age */
9934: int i1, k, nres, j ;
9935:
9936: strcpy(fileresvpl,"VPL_");
9937: strcat(fileresvpl,fileresu);
9938: if((ficresvpl=fopen(fileresvpl,"w"))==NULL) {
9939: printf("Problem with variance of forward period (stable) prevalence resultfile: %s\n", fileresvpl);
9940: exit(0);
9941: }
9942: printf("Computing Variance-covariance of forward period (stable) prevalence: file '%s' ...", fileresvpl);fflush(stdout);
9943: fprintf(ficlog, "Computing Variance-covariance of forward period (stable) prevalence: file '%s' ...", fileresvpl);fflush(ficlog);
9944:
9945: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
9946: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
9947:
9948: i1=pow(2,cptcoveff);
9949: if (cptcovn < 1){i1=1;}
9950:
9951: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
9952: k=TKresult[nres];
9953: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
9954: /* for(k=1; k<=i1;k++){ /\* We find the combination equivalent to result line values of dummies *\/ */
9955: if(i1 != 1 && TKresult[nres]!= k)
9956: continue;
9957: fprintf(ficresvpl,"\n#****** ");
9958: printf("\n#****** ");
9959: fprintf(ficlog,"\n#****** ");
9960: for(j=1;j<=cptcovs;j++) {
9961: fprintf(ficresvpl,"V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
9962: fprintf(ficlog,"V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
9963: printf("V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
9964: /* fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
9965: /* printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
9966: }
9967: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
9968: /* printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
9969: /* fprintf(ficresvpl," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
9970: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
9971: /* } */
9972: fprintf(ficresvpl,"******\n");
9973: printf("******\n");
9974: fprintf(ficlog,"******\n");
9975:
9976: varpl=matrix(1,nlstate,(int) bage, (int) fage);
9977: oldm=oldms;savm=savms;
9978: varprevlim(fileresvpl, ficresvpl, varpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl, ncvyearp, k, strstart, nres);
9979: free_matrix(varpl,1,nlstate,(int) bage, (int)fage);
9980: /*}*/
9981: }
9982:
9983: fclose(ficresvpl);
9984: printf("done variance-covariance of forward period prevalence\n");fflush(stdout);
9985: fprintf(ficlog,"done variance-covariance of forward period prevalence\n");fflush(ficlog);
9986:
9987: }
9988: /* Variance of back prevalence: varbprlim */
9989: 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){
9990: /*------- Variance of back (stable) prevalence------*/
9991:
9992: char fileresvbl[FILENAMELENGTH];
9993: FILE *ficresvbl;
9994:
9995: double **oldm, **savm;
9996: double **varbpl; /* Variances of back prevalence limits by age */
9997: int i1, k, nres, j ;
9998:
9999: strcpy(fileresvbl,"VBL_");
10000: strcat(fileresvbl,fileresu);
10001: if((ficresvbl=fopen(fileresvbl,"w"))==NULL) {
10002: printf("Problem with variance of back (stable) prevalence resultfile: %s\n", fileresvbl);
10003: exit(0);
10004: }
10005: printf("Computing Variance-covariance of back (stable) prevalence: file '%s' ...", fileresvbl);fflush(stdout);
10006: fprintf(ficlog, "Computing Variance-covariance of back (stable) prevalence: file '%s' ...", fileresvbl);fflush(ficlog);
10007:
10008:
10009: i1=pow(2,cptcoveff);
10010: if (cptcovn < 1){i1=1;}
10011:
10012: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
10013: k=TKresult[nres];
10014: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
10015: /* for(k=1; k<=i1;k++){ */
10016: /* if(i1 != 1 && TKresult[nres]!= k) */
10017: /* continue; */
10018: fprintf(ficresvbl,"\n#****** ");
10019: printf("\n#****** ");
10020: fprintf(ficlog,"\n#****** ");
10021: for (j=1; j<= cptcovs; j++){ /* For each selected (single) quantitative value */
10022: printf(" V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][resultmodel[nres][j]]);
10023: fprintf(ficresvbl," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][resultmodel[nres][j]]);
10024: fprintf(ficlog," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][resultmodel[nres][j]]);
10025: /* for(j=1;j<=cptcoveff;j++) { */
10026: /* fprintf(ficresvbl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
10027: /* fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
10028: /* printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
10029: /* } */
10030: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
10031: /* printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
10032: /* fprintf(ficresvbl," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
10033: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
10034: }
10035: fprintf(ficresvbl,"******\n");
10036: printf("******\n");
10037: fprintf(ficlog,"******\n");
10038:
10039: varbpl=matrix(1,nlstate,(int) bage, (int) fage);
10040: oldm=oldms;savm=savms;
10041:
10042: varbrevlim(fileresvbl, ficresvbl, varbpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, bprlim, ftolpl, mobilavproj, ncvyearp, k, strstart, nres);
10043: free_matrix(varbpl,1,nlstate,(int) bage, (int)fage);
10044: /*}*/
10045: }
10046:
10047: fclose(ficresvbl);
10048: printf("done variance-covariance of back prevalence\n");fflush(stdout);
10049: fprintf(ficlog,"done variance-covariance of back prevalence\n");fflush(ficlog);
10050:
10051: } /* End of varbprlim */
10052:
10053: /************** Forecasting *****not tested NB*************/
10054: /* 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){ */
10055:
10056: /* int cpt, stepsize, hstepm, nhstepm, j,k,c, cptcod, i,h; */
10057: /* int *popage; */
10058: /* double calagedatem, agelim, kk1, kk2; */
10059: /* double *popeffectif,*popcount; */
10060: /* double ***p3mat,***tabpop,***tabpopprev; */
10061: /* /\* double ***mobaverage; *\/ */
10062: /* char filerespop[FILENAMELENGTH]; */
10063:
10064: /* tabpop= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
10065: /* tabpopprev= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
10066: /* agelim=AGESUP; */
10067: /* calagedatem=(anpyram+mpyram/12.+jpyram/365.-dateintmean)*YEARM; */
10068:
10069: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
10070:
10071:
10072: /* strcpy(filerespop,"POP_"); */
10073: /* strcat(filerespop,fileresu); */
10074: /* if((ficrespop=fopen(filerespop,"w"))==NULL) { */
10075: /* printf("Problem with forecast resultfile: %s\n", filerespop); */
10076: /* fprintf(ficlog,"Problem with forecast resultfile: %s\n", filerespop); */
10077: /* } */
10078: /* printf("Computing forecasting: result on file '%s' \n", filerespop); */
10079: /* fprintf(ficlog,"Computing forecasting: result on file '%s' \n", filerespop); */
10080:
10081: /* if (cptcoveff==0) ncodemax[cptcoveff]=1; */
10082:
10083: /* /\* if (mobilav!=0) { *\/ */
10084: /* /\* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
10085: /* /\* if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){ *\/ */
10086: /* /\* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); *\/ */
10087: /* /\* printf(" Error in movingaverage mobilav=%d\n",mobilav); *\/ */
10088: /* /\* } *\/ */
10089: /* /\* } *\/ */
10090:
10091: /* stepsize=(int) (stepm+YEARM-1)/YEARM; */
10092: /* if (stepm<=12) stepsize=1; */
10093:
10094: /* agelim=AGESUP; */
10095:
10096: /* hstepm=1; */
10097: /* hstepm=hstepm/stepm; */
10098:
10099: /* if (popforecast==1) { */
10100: /* if((ficpop=fopen(popfile,"r"))==NULL) { */
10101: /* printf("Problem with population file : %s\n",popfile);exit(0); */
10102: /* fprintf(ficlog,"Problem with population file : %s\n",popfile);exit(0); */
10103: /* } */
10104: /* popage=ivector(0,AGESUP); */
10105: /* popeffectif=vector(0,AGESUP); */
10106: /* popcount=vector(0,AGESUP); */
10107:
10108: /* i=1; */
10109: /* while ((c=fscanf(ficpop,"%d %lf\n",&popage[i],&popcount[i])) != EOF) i=i+1; */
10110:
10111: /* imx=i; */
10112: /* for (i=1; i<imx;i++) popeffectif[popage[i]]=popcount[i]; */
10113: /* } */
10114:
10115: /* for(cptcov=1,k=0;cptcov<=i2;cptcov++){ */
10116: /* for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){ */
10117: /* k=k+1; */
10118: /* fprintf(ficrespop,"\n#******"); */
10119: /* for(j=1;j<=cptcoveff;j++) { */
10120: /* fprintf(ficrespop," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */
10121: /* } */
10122: /* fprintf(ficrespop,"******\n"); */
10123: /* fprintf(ficrespop,"# Age"); */
10124: /* for(j=1; j<=nlstate+ndeath;j++) fprintf(ficrespop," P.%d",j); */
10125: /* if (popforecast==1) fprintf(ficrespop," [Population]"); */
10126:
10127: /* for (cpt=0; cpt<=0;cpt++) { */
10128: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */
10129:
10130: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */
10131: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */
10132: /* nhstepm = nhstepm/hstepm; */
10133:
10134: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
10135: /* oldm=oldms;savm=savms; */
10136: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
10137:
10138: /* for (h=0; h<=nhstepm; h++){ */
10139: /* if (h==(int) (calagedatem+YEARM*cpt)) { */
10140: /* fprintf(ficrespop,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */
10141: /* } */
10142: /* for(j=1; j<=nlstate+ndeath;j++) { */
10143: /* kk1=0.;kk2=0; */
10144: /* for(i=1; i<=nlstate;i++) { */
10145: /* if (mobilav==1) */
10146: /* kk1=kk1+p3mat[i][j][h]*mobaverage[(int)agedeb+1][i][cptcod]; */
10147: /* else { */
10148: /* kk1=kk1+p3mat[i][j][h]*probs[(int)(agedeb+1)][i][cptcod]; */
10149: /* } */
10150: /* } */
10151: /* if (h==(int)(calagedatem+12*cpt)){ */
10152: /* tabpop[(int)(agedeb)][j][cptcod]=kk1; */
10153: /* /\*fprintf(ficrespop," %.3f", kk1); */
10154: /* if (popforecast==1) fprintf(ficrespop," [%.f]", kk1*popeffectif[(int)agedeb+1]);*\/ */
10155: /* } */
10156: /* } */
10157: /* for(i=1; i<=nlstate;i++){ */
10158: /* kk1=0.; */
10159: /* for(j=1; j<=nlstate;j++){ */
10160: /* kk1= kk1+tabpop[(int)(agedeb)][j][cptcod]; */
10161: /* } */
10162: /* tabpopprev[(int)(agedeb)][i][cptcod]=tabpop[(int)(agedeb)][i][cptcod]/kk1*popeffectif[(int)(agedeb+(calagedatem+12*cpt)*hstepm/YEARM*stepm-1)]; */
10163: /* } */
10164:
10165: /* if (h==(int)(calagedatem+12*cpt)) */
10166: /* for(j=1; j<=nlstate;j++) */
10167: /* fprintf(ficrespop," %15.2f",tabpopprev[(int)(agedeb+1)][j][cptcod]); */
10168: /* } */
10169: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
10170: /* } */
10171: /* } */
10172:
10173: /* /\******\/ */
10174:
10175: /* for (cpt=1; cpt<=(anpyram1-anpyram);cpt++) { */
10176: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */
10177: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */
10178: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */
10179: /* nhstepm = nhstepm/hstepm; */
10180:
10181: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
10182: /* oldm=oldms;savm=savms; */
10183: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
10184: /* for (h=0; h<=nhstepm; h++){ */
10185: /* if (h==(int) (calagedatem+YEARM*cpt)) { */
10186: /* fprintf(ficresf,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */
10187: /* } */
10188: /* for(j=1; j<=nlstate+ndeath;j++) { */
10189: /* kk1=0.;kk2=0; */
10190: /* for(i=1; i<=nlstate;i++) { */
10191: /* kk1=kk1+p3mat[i][j][h]*tabpopprev[(int)agedeb+1][i][cptcod]; */
10192: /* } */
10193: /* if (h==(int)(calagedatem+12*cpt)) fprintf(ficresf," %15.2f", kk1); */
10194: /* } */
10195: /* } */
10196: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
10197: /* } */
10198: /* } */
10199: /* } */
10200: /* } */
10201:
10202: /* /\* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
10203:
10204: /* if (popforecast==1) { */
10205: /* free_ivector(popage,0,AGESUP); */
10206: /* free_vector(popeffectif,0,AGESUP); */
10207: /* free_vector(popcount,0,AGESUP); */
10208: /* } */
10209: /* free_ma3x(tabpop,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
10210: /* free_ma3x(tabpopprev,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
10211: /* fclose(ficrespop); */
10212: /* } /\* End of popforecast *\/ */
10213:
10214: int fileappend(FILE *fichier, char *optionfich)
10215: {
10216: if((fichier=fopen(optionfich,"a"))==NULL) {
10217: printf("Problem with file: %s\n", optionfich);
10218: fprintf(ficlog,"Problem with file: %s\n", optionfich);
10219: return (0);
10220: }
10221: fflush(fichier);
10222: return (1);
10223: }
10224:
10225:
10226: /**************** function prwizard **********************/
10227: void prwizard(int ncovmodel, int nlstate, int ndeath, char model[], FILE *ficparo)
10228: {
10229:
10230: /* Wizard to print covariance matrix template */
10231:
10232: char ca[32], cb[32];
10233: int i,j, k, li, lj, lk, ll, jj, npar, itimes;
10234: int numlinepar;
10235:
10236: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
10237: fprintf(ficparo,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
10238: for(i=1; i <=nlstate; i++){
10239: jj=0;
10240: for(j=1; j <=nlstate+ndeath; j++){
10241: if(j==i) continue;
10242: jj++;
10243: /*ca[0]= k+'a'-1;ca[1]='\0';*/
10244: printf("%1d%1d",i,j);
10245: fprintf(ficparo,"%1d%1d",i,j);
10246: for(k=1; k<=ncovmodel;k++){
10247: /* printf(" %lf",param[i][j][k]); */
10248: /* fprintf(ficparo," %lf",param[i][j][k]); */
10249: printf(" 0.");
10250: fprintf(ficparo," 0.");
10251: }
10252: printf("\n");
10253: fprintf(ficparo,"\n");
10254: }
10255: }
10256: printf("# Scales (for hessian or gradient estimation)\n");
10257: fprintf(ficparo,"# Scales (for hessian or gradient estimation)\n");
10258: npar= (nlstate+ndeath-1)*nlstate*ncovmodel; /* Number of parameters*/
10259: for(i=1; i <=nlstate; i++){
10260: jj=0;
10261: for(j=1; j <=nlstate+ndeath; j++){
10262: if(j==i) continue;
10263: jj++;
10264: fprintf(ficparo,"%1d%1d",i,j);
10265: printf("%1d%1d",i,j);
10266: fflush(stdout);
10267: for(k=1; k<=ncovmodel;k++){
10268: /* printf(" %le",delti3[i][j][k]); */
10269: /* fprintf(ficparo," %le",delti3[i][j][k]); */
10270: printf(" 0.");
10271: fprintf(ficparo," 0.");
10272: }
10273: numlinepar++;
10274: printf("\n");
10275: fprintf(ficparo,"\n");
10276: }
10277: }
10278: printf("# Covariance matrix\n");
10279: /* # 121 Var(a12)\n\ */
10280: /* # 122 Cov(b12,a12) Var(b12)\n\ */
10281: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
10282: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
10283: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
10284: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
10285: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
10286: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
10287: fflush(stdout);
10288: fprintf(ficparo,"# Covariance matrix\n");
10289: /* # 121 Var(a12)\n\ */
10290: /* # 122 Cov(b12,a12) Var(b12)\n\ */
10291: /* # ...\n\ */
10292: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
10293:
10294: for(itimes=1;itimes<=2;itimes++){
10295: jj=0;
10296: for(i=1; i <=nlstate; i++){
10297: for(j=1; j <=nlstate+ndeath; j++){
10298: if(j==i) continue;
10299: for(k=1; k<=ncovmodel;k++){
10300: jj++;
10301: ca[0]= k+'a'-1;ca[1]='\0';
10302: if(itimes==1){
10303: printf("#%1d%1d%d",i,j,k);
10304: fprintf(ficparo,"#%1d%1d%d",i,j,k);
10305: }else{
10306: printf("%1d%1d%d",i,j,k);
10307: fprintf(ficparo,"%1d%1d%d",i,j,k);
10308: /* printf(" %.5le",matcov[i][j]); */
10309: }
10310: ll=0;
10311: for(li=1;li <=nlstate; li++){
10312: for(lj=1;lj <=nlstate+ndeath; lj++){
10313: if(lj==li) continue;
10314: for(lk=1;lk<=ncovmodel;lk++){
10315: ll++;
10316: if(ll<=jj){
10317: cb[0]= lk +'a'-1;cb[1]='\0';
10318: if(ll<jj){
10319: if(itimes==1){
10320: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
10321: fprintf(ficparo," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
10322: }else{
10323: printf(" 0.");
10324: fprintf(ficparo," 0.");
10325: }
10326: }else{
10327: if(itimes==1){
10328: printf(" Var(%s%1d%1d)",ca,i,j);
10329: fprintf(ficparo," Var(%s%1d%1d)",ca,i,j);
10330: }else{
10331: printf(" 0.");
10332: fprintf(ficparo," 0.");
10333: }
10334: }
10335: }
10336: } /* end lk */
10337: } /* end lj */
10338: } /* end li */
10339: printf("\n");
10340: fprintf(ficparo,"\n");
10341: numlinepar++;
10342: } /* end k*/
10343: } /*end j */
10344: } /* end i */
10345: } /* end itimes */
10346:
10347: } /* end of prwizard */
10348: /******************* Gompertz Likelihood ******************************/
10349: double gompertz(double x[])
10350: {
10351: double A=0.0,B=0.,L=0.0,sump=0.,num=0.;
10352: int i,n=0; /* n is the size of the sample */
10353:
10354: for (i=1;i<=imx ; i++) {
10355: sump=sump+weight[i];
10356: /* sump=sump+1;*/
10357: num=num+1;
10358: }
10359: L=0.0;
10360: /* agegomp=AGEGOMP; */
10361: /* for (i=0; i<=imx; i++)
10362: 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]);*/
10363:
10364: for (i=1;i<=imx ; i++) {
10365: /* mu(a)=mu(agecomp)*exp(teta*(age-agegomp))
10366: mu(a)=x[1]*exp(x[2]*(age-agegomp)); x[1] and x[2] are per year.
10367: * L= Product mu(agedeces)exp(-\int_ageexam^agedc mu(u) du ) for a death between agedc (in month)
10368: * and agedc +1 month, cens[i]=0: log(x[1]/YEARM)
10369: * +
10370: * exp(-\int_ageexam^agecens mu(u) du ) when censored, cens[i]=1
10371: */
10372: if (wav[i] > 1 || agedc[i] < AGESUP) {
10373: if (cens[i] == 1){
10374: A=-x[1]/(x[2])*(exp(x[2]*(agecens[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)));
10375: } else if (cens[i] == 0){
10376: A=-x[1]/(x[2])*(exp(x[2]*(agedc[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)))
10377: +log(x[1]/YEARM) +x[2]*(agedc[i]-agegomp)+log(YEARM);
10378: } else
10379: printf("Gompertz cens[%d] neither 1 nor 0\n",i);
10380: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
10381: L=L+A*weight[i];
10382: /* 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]);*/
10383: }
10384: }
10385:
10386: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
10387:
10388: return -2*L*num/sump;
10389: }
10390:
10391: #ifdef GSL
10392: /******************* Gompertz_f Likelihood ******************************/
10393: double gompertz_f(const gsl_vector *v, void *params)
10394: {
10395: double A=0.,B=0.,LL=0.0,sump=0.,num=0.;
10396: double *x= (double *) v->data;
10397: int i,n=0; /* n is the size of the sample */
10398:
10399: for (i=0;i<=imx-1 ; i++) {
10400: sump=sump+weight[i];
10401: /* sump=sump+1;*/
10402: num=num+1;
10403: }
10404:
10405:
10406: /* for (i=0; i<=imx; i++)
10407: 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]);*/
10408: printf("x[0]=%lf x[1]=%lf\n",x[0],x[1]);
10409: for (i=1;i<=imx ; i++)
10410: {
10411: if (cens[i] == 1 && wav[i]>1)
10412: A=-x[0]/(x[1])*(exp(x[1]*(agecens[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)));
10413:
10414: if (cens[i] == 0 && wav[i]>1)
10415: A=-x[0]/(x[1])*(exp(x[1]*(agedc[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)))
10416: +log(x[0]/YEARM)+x[1]*(agedc[i]-agegomp)+log(YEARM);
10417:
10418: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
10419: if (wav[i] > 1 ) { /* ??? */
10420: LL=LL+A*weight[i];
10421: /* 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]);*/
10422: }
10423: }
10424:
10425: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
10426: printf("x[0]=%lf x[1]=%lf -2*LL*num/sump=%lf\n",x[0],x[1],-2*LL*num/sump);
10427:
10428: return -2*LL*num/sump;
10429: }
10430: #endif
10431:
10432: /******************* Printing html file ***********/
10433: void printinghtmlmort(char fileresu[], char title[], char datafile[], int firstpass, \
10434: int lastpass, int stepm, int weightopt, char model[],\
10435: int imx, double p[],double **matcov,double agemortsup){
10436: int i,k;
10437:
10438: fprintf(fichtm,"<ul><li><h4>Result files </h4>\n Force of mortality. Parameters of the Gompertz fit (with confidence interval in brackets):<br>");
10439: fprintf(fichtm," mu(age) =%lf*exp(%lf*(age-%d)) per year<br><br>",p[1],p[2],agegomp);
10440: for (i=1;i<=2;i++)
10441: 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]));
10442: fprintf(fichtm,"<br><br><img src=\"graphmort.svg\">");
10443: fprintf(fichtm,"</ul>");
10444:
10445: fprintf(fichtm,"<ul><li><h4>Life table</h4>\n <br>");
10446:
10447: 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>");
10448:
10449: for (k=agegomp;k<(agemortsup-2);k++)
10450: 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]);
10451:
10452:
10453: fflush(fichtm);
10454: }
10455:
10456: /******************* Gnuplot file **************/
10457: void printinggnuplotmort(char fileresu[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){
10458:
10459: char dirfileres[132],optfileres[132];
10460:
10461: int ng;
10462:
10463:
10464: /*#ifdef windows */
10465: fprintf(ficgp,"cd \"%s\" \n",pathc);
10466: /*#endif */
10467:
10468:
10469: strcpy(dirfileres,optionfilefiname);
10470: strcpy(optfileres,"vpl");
10471: fprintf(ficgp,"set out \"graphmort.svg\"\n ");
10472: fprintf(ficgp,"set xlabel \"Age\"\n set ylabel \"Force of mortality (per year)\" \n ");
10473: fprintf(ficgp, "set ter svg size 640, 480\n set log y\n");
10474: /* fprintf(ficgp, "set size 0.65,0.65\n"); */
10475: fprintf(ficgp,"plot [%d:100] %lf*exp(%lf*(x-%d))",agegomp,p[1],p[2],agegomp);
10476:
10477: }
10478:
10479: int readdata(char datafile[], int firstobs, int lastobs, int *imax)
10480: {
10481:
10482: /*-------- data file ----------*/
10483: FILE *fic;
10484: char dummy[]=" ";
10485: int i=0, j=0, n=0, iv=0, v;
10486: int lstra;
10487: int linei, month, year,iout;
10488: int noffset=0; /* This is the offset if BOM data file */
10489: char line[MAXLINE], linetmp[MAXLINE];
10490: char stra[MAXLINE], strb[MAXLINE];
10491: char *stratrunc;
10492:
10493: DummyV=ivector(1,NCOVMAX); /* 1 to 3 */
10494: FixedV=ivector(1,NCOVMAX); /* 1 to 3 */
10495: for(v=1;v<NCOVMAX;v++){
10496: DummyV[v]=0;
10497: FixedV[v]=0;
10498: }
10499:
10500: for(v=1; v <=ncovcol;v++){
10501: DummyV[v]=0;
10502: FixedV[v]=0;
10503: }
10504: for(v=ncovcol+1; v <=ncovcol+nqv;v++){
10505: DummyV[v]=1;
10506: FixedV[v]=0;
10507: }
10508: for(v=ncovcol+nqv+1; v <=ncovcol+nqv+ntv;v++){
10509: DummyV[v]=0;
10510: FixedV[v]=1;
10511: }
10512: for(v=ncovcol+nqv+ntv+1; v <=ncovcol+nqv+ntv+nqtv;v++){
10513: DummyV[v]=1;
10514: FixedV[v]=1;
10515: }
10516: for(v=1; v <=ncovcol+nqv+ntv+nqtv;v++){
10517: printf("Covariate type in the data: V%d, DummyV(V%d)=%d, FixedV(V%d)=%d\n",v,v,DummyV[v],v,FixedV[v]);
10518: fprintf(ficlog,"Covariate type in the data: V%d, DummyV(V%d)=%d, FixedV(V%d)=%d\n",v,v,DummyV[v],v,FixedV[v]);
10519: }
10520:
10521: ncovcolt=ncovcol+nqv+ntv+nqtv; /* total of covariates in the data, not in the model equation */
10522:
10523: if((fic=fopen(datafile,"r"))==NULL) {
10524: printf("Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(stdout);
10525: fprintf(ficlog,"Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(ficlog);return 1;
10526: }
10527:
10528: /* Is it a BOM UTF-8 Windows file? */
10529: /* First data line */
10530: linei=0;
10531: while(fgets(line, MAXLINE, fic)) {
10532: noffset=0;
10533: if( line[0] == (char)0xEF && line[1] == (char)0xBB) /* EF BB BF */
10534: {
10535: noffset=noffset+3;
10536: printf("# Data file '%s' is an UTF8 BOM file, please convert to UTF8 or ascii file and rerun.\n",datafile);fflush(stdout);
10537: fprintf(ficlog,"# Data file '%s' is an UTF8 BOM file, please convert to UTF8 or ascii file and rerun.\n",datafile);
10538: fflush(ficlog); return 1;
10539: }
10540: /* else if( line[0] == (char)0xFE && line[1] == (char)0xFF)*/
10541: else if( line[0] == (char)0xFF && line[1] == (char)0xFE)
10542: {
10543: noffset=noffset+2;
10544: 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);
10545: 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);
10546: fflush(ficlog); return 1;
10547: }
10548: else if( line[0] == 0 && line[1] == 0)
10549: {
10550: if( line[2] == (char)0xFE && line[3] == (char)0xFF){
10551: noffset=noffset+4;
10552: 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);
10553: 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);
10554: fflush(ficlog); return 1;
10555: }
10556: } else{
10557: ;/*printf(" Not a BOM file\n");*/
10558: }
10559: /* If line starts with a # it is a comment */
10560: if (line[noffset] == '#') {
10561: linei=linei+1;
10562: break;
10563: }else{
10564: break;
10565: }
10566: }
10567: fclose(fic);
10568: if((fic=fopen(datafile,"r"))==NULL) {
10569: printf("Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(stdout);
10570: fprintf(ficlog,"Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(ficlog);return 1;
10571: }
10572: /* Not a Bom file */
10573:
10574: i=1;
10575: while ((fgets(line, MAXLINE, fic) != NULL) &&((i >= firstobs) && (i <=lastobs))) {
10576: linei=linei+1;
10577: for(j=strlen(line); j>=0;j--){ /* Untabifies line */
10578: if(line[j] == '\t')
10579: line[j] = ' ';
10580: }
10581: for(j=strlen(line)-1; (line[j]==' ')||(line[j]==10)||(line[j]==13);j--){
10582: ;
10583: };
10584: line[j+1]=0; /* Trims blanks at end of line */
10585: if(line[0]=='#'){
10586: fprintf(ficlog,"Comment line\n%s\n",line);
10587: printf("Comment line\n%s\n",line);
10588: continue;
10589: }
10590: trimbb(linetmp,line); /* Trims multiple blanks in line */
10591: strcpy(line, linetmp);
10592:
10593: /* Loops on waves */
10594: for (j=maxwav;j>=1;j--){
10595: for (iv=nqtv;iv>=1;iv--){ /* Loop on time varying quantitative variables */
10596: cutv(stra, strb, line, ' ');
10597: if(strb[0]=='.') { /* Missing value */
10598: lval=-1;
10599: cotqvar[j][iv][i]=-1; /* 0.0/0.0 */
10600: cotvar[j][ncovcol+nqv+ntv+iv][i]=-1; /* For performance reasons */
10601: if(isalpha(strb[1])) { /* .m or .d Really Missing value */
10602: 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);
10603: 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);
10604: return 1;
10605: }
10606: }else{
10607: errno=0;
10608: /* what_kind_of_number(strb); */
10609: dval=strtod(strb,&endptr);
10610: /* if( strb[0]=='\0' || (*endptr != '\0')){ */
10611: /* if(strb != endptr && *endptr == '\0') */
10612: /* dval=dlval; */
10613: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
10614: if( strb[0]=='\0' || (*endptr != '\0')){
10615: 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);
10616: 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);
10617: return 1;
10618: }
10619: cotqvar[j][iv][i]=dval;
10620: cotvar[j][ncovcol+nqv+ntv+iv][i]=dval; /* because cotvar starts now at first ntv */
10621: }
10622: strcpy(line,stra);
10623: }/* end loop ntqv */
10624:
10625: for (iv=ntv;iv>=1;iv--){ /* Loop on time varying dummies */
10626: cutv(stra, strb, line, ' ');
10627: if(strb[0]=='.') { /* Missing value */
10628: lval=-1;
10629: }else{
10630: errno=0;
10631: lval=strtol(strb,&endptr,10);
10632: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
10633: if( strb[0]=='\0' || (*endptr != '\0')){
10634: 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);
10635: 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);
10636: return 1;
10637: }
10638: }
10639: if(lval <-1 || lval >1){
10640: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
10641: Should be a value of %d(nth) covariate of wave %d (0 should be the value for the reference and 1\n \
10642: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
10643: For example, for multinomial values like 1, 2 and 3,\n \
10644: build V1=0 V2=0 for the reference value (1),\n \
10645: V1=1 V2=0 for (2) \n \
10646: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
10647: output of IMaCh is often meaningless.\n \
10648: Exiting.\n",lval,linei, i,line,iv,j);
10649: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
10650: Should be a value of %d(nth) covariate of wave %d (0 should be the value for the reference and 1\n \
10651: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
10652: For example, for multinomial values like 1, 2 and 3,\n \
10653: build V1=0 V2=0 for the reference value (1),\n \
10654: V1=1 V2=0 for (2) \n \
10655: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
10656: output of IMaCh is often meaningless.\n \
10657: Exiting.\n",lval,linei, i,line,iv,j);fflush(ficlog);
10658: return 1;
10659: }
10660: cotvar[j][ncovcol+nqv+iv][i]=(double)(lval);
10661: strcpy(line,stra);
10662: }/* end loop ntv */
10663:
10664: /* Statuses at wave */
10665: cutv(stra, strb, line, ' ');
10666: if(strb[0]=='.') { /* Missing value */
10667: lval=-1;
10668: }else{
10669: errno=0;
10670: lval=strtol(strb,&endptr,10);
10671: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
10672: if( strb[0]=='\0' || (*endptr != '\0' )){
10673: 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);
10674: 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);
10675: return 1;
10676: }else if( lval==0 || lval > nlstate+ndeath){
10677: 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);
10678: 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);
10679: return 1;
10680: }
10681: }
10682:
10683: s[j][i]=lval;
10684:
10685: /* Date of Interview */
10686: strcpy(line,stra);
10687: cutv(stra, strb,line,' ');
10688: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
10689: }
10690: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
10691: month=99;
10692: year=9999;
10693: }else{
10694: 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);
10695: 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);
10696: return 1;
10697: }
10698: anint[j][i]= (double) year;
10699: mint[j][i]= (double)month;
10700: /* if( (int)anint[j][i]+ (int)(mint[j][i])/12. < (int) (moisnais[i]/12.+annais[i])){ */
10701: /* 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]); */
10702: /* 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]); */
10703: /* } */
10704: strcpy(line,stra);
10705: } /* End loop on waves */
10706:
10707: /* Date of death */
10708: cutv(stra, strb,line,' ');
10709: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
10710: }
10711: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
10712: month=99;
10713: year=9999;
10714: }else{
10715: 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);
10716: 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);
10717: return 1;
10718: }
10719: andc[i]=(double) year;
10720: moisdc[i]=(double) month;
10721: strcpy(line,stra);
10722:
10723: /* Date of birth */
10724: cutv(stra, strb,line,' ');
10725: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
10726: }
10727: else if( (iout=sscanf(strb,"%s.", dummy)) != 0){
10728: month=99;
10729: year=9999;
10730: }else{
10731: 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);
10732: 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);
10733: return 1;
10734: }
10735: if (year==9999) {
10736: 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);
10737: 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);
10738: return 1;
10739:
10740: }
10741: annais[i]=(double)(year);
10742: moisnais[i]=(double)(month);
10743: for (j=1;j<=maxwav;j++){
10744: if( (int)anint[j][i]+ (int)(mint[j][i])/12. < (int) (moisnais[i]/12.+annais[i])){
10745: 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]);
10746: 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]);
10747: }
10748: }
10749:
10750: strcpy(line,stra);
10751:
10752: /* Sample weight */
10753: cutv(stra, strb,line,' ');
10754: errno=0;
10755: dval=strtod(strb,&endptr);
10756: if( strb[0]=='\0' || (*endptr != '\0')){
10757: printf("Error reading data around '%f' at line number %d, \"%s\" for individual %d\nShould be a weight. Exiting.\n",dval, i,line,linei);
10758: 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);
10759: fflush(ficlog);
10760: return 1;
10761: }
10762: weight[i]=dval;
10763: strcpy(line,stra);
10764:
10765: for (iv=nqv;iv>=1;iv--){ /* Loop on fixed quantitative variables */
10766: cutv(stra, strb, line, ' ');
10767: if(strb[0]=='.') { /* Missing value */
10768: lval=-1;
10769: coqvar[iv][i]=NAN;
10770: covar[ncovcol+iv][i]=NAN; /* including qvar in standard covar for performance reasons */
10771: }else{
10772: errno=0;
10773: /* what_kind_of_number(strb); */
10774: dval=strtod(strb,&endptr);
10775: /* if(strb != endptr && *endptr == '\0') */
10776: /* dval=dlval; */
10777: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
10778: if( strb[0]=='\0' || (*endptr != '\0')){
10779: 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);
10780: 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);
10781: return 1;
10782: }
10783: coqvar[iv][i]=dval;
10784: covar[ncovcol+iv][i]=dval; /* including qvar in standard covar for performance reasons */
10785: }
10786: strcpy(line,stra);
10787: }/* end loop nqv */
10788:
10789: /* Covariate values */
10790: for (j=ncovcol;j>=1;j--){
10791: cutv(stra, strb,line,' ');
10792: if(strb[0]=='.') { /* Missing covariate value */
10793: lval=-1;
10794: }else{
10795: errno=0;
10796: lval=strtol(strb,&endptr,10);
10797: if( strb[0]=='\0' || (*endptr != '\0')){
10798: 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);
10799: 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);
10800: return 1;
10801: }
10802: }
10803: if(lval <-1 || lval >1){
10804: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
10805: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
10806: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
10807: For example, for multinomial values like 1, 2 and 3,\n \
10808: build V1=0 V2=0 for the reference value (1),\n \
10809: V1=1 V2=0 for (2) \n \
10810: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
10811: output of IMaCh is often meaningless.\n \
10812: Exiting.\n",lval,linei, i,line,j);
10813: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
10814: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
10815: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
10816: For example, for multinomial values like 1, 2 and 3,\n \
10817: build V1=0 V2=0 for the reference value (1),\n \
10818: V1=1 V2=0 for (2) \n \
10819: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
10820: output of IMaCh is often meaningless.\n \
10821: Exiting.\n",lval,linei, i,line,j);fflush(ficlog);
10822: return 1;
10823: }
10824: covar[j][i]=(double)(lval);
10825: strcpy(line,stra);
10826: }
10827: lstra=strlen(stra);
10828:
10829: if(lstra > 9){ /* More than 2**32 or max of what printf can write with %ld */
10830: stratrunc = &(stra[lstra-9]);
10831: num[i]=atol(stratrunc);
10832: }
10833: else
10834: num[i]=atol(stra);
10835: /*if((s[2][i]==2) && (s[3][i]==-1)&&(s[4][i]==9)){
10836: 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;}*/
10837:
10838: i=i+1;
10839: } /* End loop reading data */
10840:
10841: *imax=i-1; /* Number of individuals */
10842: fclose(fic);
10843:
10844: return (0);
10845: /* endread: */
10846: printf("Exiting readdata: ");
10847: fclose(fic);
10848: return (1);
10849: }
10850:
10851: void removefirstspace(char **stri){/*, char stro[]) {*/
10852: char *p1 = *stri, *p2 = *stri;
10853: while (*p2 == ' ')
10854: p2++;
10855: /* while ((*p1++ = *p2++) !=0) */
10856: /* ; */
10857: /* do */
10858: /* while (*p2 == ' ') */
10859: /* p2++; */
10860: /* while (*p1++ == *p2++); */
10861: *stri=p2;
10862: }
10863:
10864: int decoderesult( char resultline[], int nres)
10865: /**< This routine decode one result line and returns the combination # of dummy covariates only **/
10866: {
10867: int j=0, k=0, k1=0, k2=0, k3=0, k4=0, match=0, k2q=0, k3q=0, k4q=0;
10868: char resultsav[MAXLINE];
10869: /* int resultmodel[MAXLINE]; */
10870: /* int modelresult[MAXLINE]; */
10871: char stra[80], strb[80], strc[80], strd[80],stre[80];
10872:
10873: removefirstspace(&resultline);
10874: printf("decoderesult:%s\n",resultline);
10875:
10876: strcpy(resultsav,resultline);
10877: /* printf("Decoderesult resultsav=\"%s\" resultline=\"%s\"\n", resultsav, resultline); */
10878: if (strlen(resultsav) >1){
10879: j=nbocc(resultsav,'='); /**< j=Number of covariate values'=' in this resultline */
10880: }
10881: if(j == 0){ /* Resultline but no = */
10882: TKresult[nres]=0; /* Combination for the nresult and the model */
10883: return (0);
10884: }
10885: if( j != cptcovs ){ /* Be careful if a variable is in a product but not single */
10886: 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, %s.\n",j, cptcovs, model);
10887: 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, %s.\n",j, cptcovs, model);
10888: /* return 1;*/
10889: }
10890: for(k=1; k<=j;k++){ /* Loop on any covariate of the RESULT LINE */
10891: if(nbocc(resultsav,'=') >1){
10892: 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" */
10893: /* If resultsav= "V4= 1 V5=25.1 V3=0" with a blank then strb="V4=" and stra="1 V5=25.1 V3=0" */
10894: cutl(strc,strd,strb,'='); /* strb:"V4=1" strc="1" strd="V4" */
10895: /* If a blank, then strc="V4=" and strd='\0' */
10896: if(strc[0]=='\0'){
10897: printf("Error in resultline, probably a blank after the \"%s\", \"result:%s\", stra=\"%s\" resultsav=\"%s\"\n",strb,resultline, stra, resultsav);
10898: fprintf(ficlog,"Error in resultline, probably a blank after the \"V%s=\", resultline=%s\n",strb,resultline);
10899: return 1;
10900: }
10901: }else
10902: cutl(strc,strd,resultsav,'=');
10903: Tvalsel[k]=atof(strc); /* 1 */ /* Tvalsel of k is the float value of the kth covariate appearing in this result line */
10904:
10905: cutl(strc,stre,strd,'V'); /* strd='V4' strc=4 stre='V' */;
10906: Tvarsel[k]=atoi(strc); /* 4 */ /* Tvarsel is the id of the kth covariate in the result line Tvarsel[1] in "V4=1.." is 4.*/
10907: /* Typevarsel[k]=1; /\* 1 for age product *\/ */
10908: /* cptcovsel++; */
10909: if (nbocc(stra,'=') >0)
10910: strcpy(resultsav,stra); /* and analyzes it */
10911: }
10912: /* Checking for missing or useless values in comparison of current model needs */
10913: /* Feeds resultmodel[nres][k1]=k2 for k1th product covariate with age in the model equation fed by the index k2 of the resutline*/
10914: for(k1=1; k1<= cptcovt ;k1++){ /* Loop on MODEL LINE V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
10915: if(Typevar[k1]==0){ /* Single covariate in model */
10916: /* 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product */
10917: match=0;
10918: for(k2=1; k2 <=j;k2++){/* Loop on resultline. In result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
10919: if(Tvar[k1]==Tvarsel[k2]) {/* Tvar is coming from the model, Tvarsel from the result. Tvar[1]=5 == Tvarsel[2]=5 */
10920: modelresult[nres][k2]=k1;/* modelresult[2]=1 modelresult[1]=2 modelresult[3]=3 modelresult[6]=4 modelresult[9]=5 */
10921: match=1; /* modelresult of k2 variable of resultline is identical to k1 variable of the model good */
10922: break;
10923: }
10924: }
10925: if(match == 0){
10926: 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]);
10927: 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);
10928: return 1;
10929: }
10930: }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*/
10931: /* We feed resultmodel[k1]=k2; */
10932: match=0;
10933: 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 */
10934: if(Tvar[k1]==Tvarsel[k2]) {/* Tvar is coming from the model, Tvarsel from the result. Tvar[1]=5 == Tvarsel[2]=5 */
10935: 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 */
10936: resultmodel[nres][k1]=k2; /* Added here */
10937: /* printf("Decoderesult first modelresult[k2=%d]=%d (k1) V%d*AGE\n",k2,k1,Tvar[k1]); */
10938: match=1; /* modelresult of k2 variable of resultline is identical to k1 variable of the model good */
10939: break;
10940: }
10941: }
10942: if(match == 0){
10943: 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]);
10944: 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]);
10945: return 1;
10946: }
10947: }else if(Typevar[k1]==2){ /* Product No age We want to get the position in the resultline of the product in the model line*/
10948: /* resultmodel[nres][of such a Vn * Vm product k1] is not unique, so can't exist, we feed Tvard[k1][1] and [2] */
10949: match=0;
10950: /* 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]); */
10951: for(k2=1; k2 <=j;k2++){/* Loop on resultline. In result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
10952: if(Tvardk[k1][1]==Tvarsel[k2]) {/* Tvardk is coming from the model, Tvarsel from the result. Tvar[1]=5 == Tvarsel[2]=5 */
10953: /* modelresult[k2]=k1; */
10954: /* printf("Decoderesult first Product modelresult[k2=%d]=%d (k1) V%d * \n",k2,k1,Tvarsel[k2]); */
10955: match=1; /* modelresult of k2 variable of resultline is identical to k1 variable of the model good */
10956: }
10957: }
10958: if(match == 0){
10959: printf("Error in result line (Product without age first variable): V%d is missing in result: %s according to model=1+age+%s\n",Tvardk[k1][1], resultline, model);
10960: fprintf(ficlog,"Error in result line (Product without age first variable): V%d is missing in result: %s according to model=1+age+%s\n",Tvardk[k1][1], resultline, model);
10961: return 1;
10962: }
10963: match=0;
10964: for(k2=1; k2 <=j;k2++){/* Loop on resultline. In result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
10965: if(Tvardk[k1][2]==Tvarsel[k2]) {/* Tvardk is coming from the model, Tvarsel from the result. Tvar[1]=5 == Tvarsel[2]=5 */
10966: /* modelresult[k2]=k1;*/
10967: /* printf("Decoderesult second Product modelresult[k2=%d]=%d (k1) * V%d \n ",k2,k1,Tvarsel[k2]); */
10968: match=1; /* modelresult of k2 variable of resultline is identical to k1 variable of the model good */
10969: break;
10970: }
10971: }
10972: if(match == 0){
10973: printf("Error in result line (Product without age second variable): V%d is missing in result: %s according to model=1+age+%s\n",Tvardk[k1][2], resultline, model);
10974: fprintf(ficlog,"Error in result line (Product without age second variable): V%d is missing in result : %s according to model=1+age+%s\n",Tvardk[k1][2], resultline, model);
10975: return 1;
10976: }
10977: }/* End of testing */
10978: }/* End loop cptcovt */
10979: /* Checking for missing or useless values in comparison of current model needs */
10980: /* Feeds resultmodel[nres][k1]=k2 for single covariate (k1) in the model equation */
10981: 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)
10982: * Loop on resultline variables: result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
10983: match=0;
10984: for(k1=1; k1<= cptcovt ;k1++){ /* loop on model: model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
10985: if(Typevar[k1]==0){ /* Single only */
10986: if(Tvar[k1]==Tvarsel[k2]) { /* Tvar[2]=4 == Tvarsel[1]=4 */
10987: 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 */
10988: 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 */
10989: ++match;
10990: }
10991: }
10992: }
10993: if(match == 0){
10994: printf("Error in result line: variable V%d is missing in model; result: %s, model=1+age+%s\n",Tvarsel[k2], resultline, model);
10995: fprintf(ficlog,"Error in result line: variable V%d is missing in model; result: %s, model=1+age+%s\n",Tvarsel[k2], resultline, model);
10996: return 1;
10997: }else if(match > 1){
10998: printf("Error in result line: %d doubled; result: %s, model=1+age+%s\n",k2, resultline, model);
10999: fprintf(ficlog,"Error in result line: %d doubled; result: %s, model=1+age+%s\n",k2, resultline, model);
11000: return 1;
11001: }
11002: }
11003: /* cptcovres=j /\* Number of variables in the resultline is equal to cptcovs and thus useless *\/ */
11004: /* We need to deduce which combination number is chosen and save quantitative values */
11005: /* model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
11006: /* nres=1st result line: V4=1 V5=25.1 V3=0 V2=8 V1=1 */
11007: /* 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*/
11008: /* nres=2nd result line: V4=1 V5=24.1 V3=1 V2=8 V1=0 */
11009: /* should give a combination of dummy V4=1, V3=1, V1=0 => V4*2**(0) + V3*2**(1) + V1*2**(2) = 3 + (1offset) = 4*/
11010: /* 1 0 0 0 */
11011: /* 2 1 0 0 */
11012: /* 3 0 1 0 */
11013: /* 4 1 1 0 */ /* V4=1, V3=1, V1=0 (nres=2)*/
11014: /* 5 0 0 1 */
11015: /* 6 1 0 1 */ /* V4=1, V3=0, V1=1 (nres=1)*/
11016: /* 7 0 1 1 */
11017: /* 8 1 1 1 */
11018: /* V(Tvresult)=Tresult V4=1 V3=0 V1=1 Tresult[nres=1][2]=0 */
11019: /* V(Tvqresult)=Tqresult V5=25.1 V2=8 Tqresult[nres=1][1]=25.1 */
11020: /* V5*age V5 known which value for nres? */
11021: /* Tqinvresult[2]=8 Tqinvresult[1]=25.1 */
11022: 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.
11023: * loop on position k1 in the MODEL LINE */
11024: /* k counting number of combination of single dummies in the equation model */
11025: /* k4 counting single dummies in the equation model */
11026: /* k4q counting single quantitatives in the equation model */
11027: if( Dummy[k1]==0 && Typevar[k1]==0 ){ /* Dummy and Single, fixed or timevarying, k1 is sorting according to MODEL, but k3 to resultline */
11028: /* 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) */
11029: /* modelresult[k3]=k1: k3th position in the result line corresponds to the k1 position in the model line (doesn't work with products)*/
11030: /* Value in the (current nres) resultline of the variable at the k1th position in the model equation resultmodel[nres][k1]= k3 */
11031: /* resultmodel[nres][k1]=k3: k1th position in the model correspond to the k3 position in the resultline */
11032: /* k3 is the position in the nres result line of the k1th variable of the model equation */
11033: /* Tvarsel[k3]: Name of the variable at the k3th position in the result line. */
11034: /* Tvalsel[k3]: Value of the variable at the k3th position in the result line. */
11035: /* Tresult[nres][result_position]= value of the dummy variable at the result_position in the nres resultline */
11036: /* Tvresult[nres][result_position]= name of the dummy variable at the result_position in the nres resultline */
11037: /* Tinvresult[nres][Name of a dummy variable]= value of the variable in the result line */
11038: /* TinvDoQresult[nres][Name of a Dummy or Q variable]= value of the variable in the result line */
11039: k3= resultmodel[nres][k1]; /* From position k1 in model get position k3 in result line */
11040: /* nres=1 k1=2 resultmodel[2(V4)] = 1=k3 ; k1=3 resultmodel[3(V3)] = 2=k3*/
11041: 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)*/
11042: k+=Tvalsel[k3]*pow(2,k4); /* nres=1 k1=2 Tvalsel[1]=1 (V4=1); k1=3 k3=2 Tvalsel[2]=0 (V3=0) */
11043: TinvDoQresult[nres][(int)Tvarsel[k3]]=Tvalsel[k3]; /* TinvDoQresult[nres][Name]=Value; stores the value into the name of the variable. */
11044: /* Tinvresult[nres][4]=1 */
11045: /* Tresult[nres][k4+1]=Tvalsel[k3];/\* Tresult[nres=2][1]=1(V4=1) Tresult[nres=2][2]=0(V3=0) *\/ */
11046: Tresult[nres][k3]=Tvalsel[k3];/* Tresult[nres=2][1]=1(V4=1) Tresult[nres=2][2]=0(V3=0) */
11047: /* Tvresult[nres][k4+1]=(int)Tvarsel[k3];/\* Tvresult[nres][1]=4 Tvresult[nres][3]=1 *\/ */
11048: Tvresult[nres][k3]=(int)Tvarsel[k3];/* Tvresult[nres][1]=4 Tvresult[nres][3]=1 */
11049: Tinvresult[nres][(int)Tvarsel[k3]]=Tvalsel[k3]; /* Tinvresult[nres][4]=1 */
11050: precov[nres][k1]=Tvalsel[k3]; /* Value from resultline of the variable at the k1 position in the model */
11051: /* 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); */
11052: k4++;;
11053: }else if( Dummy[k1]==1 && Typevar[k1]==0 ){ /* Quantitative and single */
11054: /* Tqresult[nres][result_position]= value of the variable at the result_position in the nres resultline */
11055: /* Tvqresult[nres][result_position]= id of the variable at the result_position in the nres resultline */
11056: /* Tqinvresult[nres][Name of a quantitative variable]= value of the variable in the result line */
11057: k3q= resultmodel[nres][k1]; /* resultmodel[1(V5)] = 5 =k3q */
11058: k2q=(int)Tvarsel[k3q]; /* Name of variable at k3q th position in the resultline */
11059: /* Tvarsel[resultmodel[1]]= Tvarsel[1] = 4=k2 */
11060: /* Tqresult[nres][k4q+1]=Tvalsel[k3q]; /\* Tqresult[nres][1]=25.1 *\/ */
11061: /* Tvresult[nres][k4q+1]=(int)Tvarsel[k3q];/\* Tvresult[nres][1]=4 Tvresult[nres][3]=1 *\/ */
11062: /* Tvqresult[nres][k4q+1]=(int)Tvarsel[k3q]; /\* Tvqresult[nres][1]=5 *\/ */
11063: Tqresult[nres][k3q]=Tvalsel[k3q]; /* Tqresult[nres][1]=25.1 */
11064: Tvresult[nres][k3q]=(int)Tvarsel[k3q];/* Tvresult[nres][1]=4 Tvresult[nres][3]=1 */
11065: Tvqresult[nres][k3q]=(int)Tvarsel[k3q]; /* Tvqresult[nres][1]=5 */
11066: Tqinvresult[nres][(int)Tvarsel[k3q]]=Tvalsel[k3q]; /* Tqinvresult[nres][5]=25.1 */
11067: TinvDoQresult[nres][(int)Tvarsel[k3q]]=Tvalsel[k3q]; /* Tqinvresult[nres][5]=25.1 */
11068: precov[nres][k1]=Tvalsel[k3q];
11069: /* 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]); */
11070: k4q++;;
11071: }else if( Dummy[k1]==2 ){ /* For dummy with age product */
11072: /* Tvar[k1]; */ /* Age variable */
11073: /* Wrong we want the value of variable name Tvar[k1] */
11074:
11075: k3= resultmodel[nres][k1]; /* nres=1 k1=2 resultmodel[2(V4)] = 1=k3 ; k1=3 resultmodel[3(V3)] = 2=k3*/
11076: 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)*/
11077: TinvDoQresult[nres][(int)Tvarsel[k3]]=Tvalsel[k3]; /* TinvDoQresult[nres][4]=1 */
11078: precov[nres][k1]=Tvalsel[k3];
11079: /* 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]); */
11080: }else if( Dummy[k1]==3 ){ /* For quant with age product */
11081: k3q= resultmodel[nres][k1]; /* resultmodel[1(V5)] = 25.1=k3q */
11082: k2q=(int)Tvarsel[k3q]; /* Tvarsel[resultmodel[1]]= Tvarsel[1] = 4=k2 */
11083: TinvDoQresult[nres][(int)Tvarsel[k3q]]=Tvalsel[k3q]; /* TinvDoQresult[nres][5]=25.1 */
11084: precov[nres][k1]=Tvalsel[k3q];
11085: /* 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]); */
11086: }else if(Typevar[k1]==2 ){ /* For product quant or dummy (not with age) */
11087: precov[nres][k1]=TinvDoQresult[nres][Tvardk[k1][1]] * TinvDoQresult[nres][Tvardk[k1][2]];
11088: /* 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]]); */
11089: }else{
11090: printf("Error Decoderesult probably a product Dummy[%d]==%d && Typevar[%d]==%d\n", k1, Dummy[k1], k1, Typevar[k1]);
11091: fprintf(ficlog,"Error Decoderesult probably a product Dummy[%d]==%d && Typevar[%d]==%d\n", k1, Dummy[k1], k1, Typevar[k1]);
11092: }
11093: }
11094:
11095: TKresult[nres]=++k; /* Number of combinations of dummies for the nresult and the model =Tvalsel[k3]*pow(2,k4) + 1*/
11096: return (0);
11097: }
11098:
11099: int decodemodel( char model[], int lastobs)
11100: /**< This routine decodes the model and returns:
11101: * Model V1+V2+V3+V8+V7*V8+V5*V6+V8*age+V3*age+age*age
11102: * - nagesqr = 1 if age*age in the model, otherwise 0.
11103: * - cptcovt total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age
11104: * - cptcovn or number of covariates k of the models excluding age*products =6 and age*age
11105: * - cptcovage number of covariates with age*products =2
11106: * - cptcovs number of simple covariates
11107: * ncovcolt=ncovcol+nqv+ntv+nqtv total of covariates in the data, not in the model equation
11108: * - 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
11109: * which is a new column after the 9 (ncovcol+nqv+ntv+nqtv) variables.
11110: * - if k is a product Vn*Vm, covar[k][i] is filled with correct values for each individual
11111: * - Tprod[l] gives the kth covariates of the product Vn*Vm l=1 to cptcovprod-cptcovage
11112: * Tprod[1]@2 {5, 6}: position of first product V7*V8 is 5, and second V5*V6 is 6.
11113: * - Tvard[k] p Tvard[1][1]@4 {7, 8, 5, 6} for V7*V8 and V5*V6 .
11114: */
11115: /* V2+V1+V4+V3*age Tvar[4]=3 ; V1+V2*age Tvar[2]=2; V1+V1*age Tvar[2]=1, Tage[1]=2 */
11116: {
11117: int i, j, k, ks, v;
11118: int j1, k1, k2, k3, k4;
11119: char modelsav[80];
11120: char stra[80], strb[80], strc[80], strd[80],stre[80];
11121: char *strpt;
11122:
11123: /*removespace(model);*/
11124: if (strlen(model) >1){ /* If there is at least 1 covariate */
11125: j=0, j1=0, k1=0, k2=-1, ks=0, cptcovn=0;
11126: if (strstr(model,"AGE") !=0){
11127: printf("Error. AGE must be in lower case 'age' model=1+age+%s. ",model);
11128: fprintf(ficlog,"Error. AGE must be in lower case model=1+age+%s. ",model);fflush(ficlog);
11129: return 1;
11130: }
11131: if (strstr(model,"v") !=0){
11132: printf("Error. 'v' must be in upper case 'V' model=1+age+%s ",model);
11133: fprintf(ficlog,"Error. 'v' must be in upper case model=1+age+%s ",model);fflush(ficlog);
11134: return 1;
11135: }
11136: strcpy(modelsav,model);
11137: if ((strpt=strstr(model,"age*age")) !=0){
11138: printf(" strpt=%s, model=1+age+%s\n",strpt, model);
11139: if(strpt != model){
11140: printf("Error in model: 'model=1+age+%s'; 'age*age' should in first place before other covariates\n \
11141: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
11142: corresponding column of parameters.\n",model);
11143: fprintf(ficlog,"Error in model: 'model=1+age+%s'; 'age*age' should in first place before other covariates\n \
11144: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
11145: corresponding column of parameters.\n",model); fflush(ficlog);
11146: return 1;
11147: }
11148: nagesqr=1;
11149: if (strstr(model,"+age*age") !=0)
11150: substrchaine(modelsav, model, "+age*age");
11151: else if (strstr(model,"age*age+") !=0)
11152: substrchaine(modelsav, model, "age*age+");
11153: else
11154: substrchaine(modelsav, model, "age*age");
11155: }else
11156: nagesqr=0;
11157: if (strlen(modelsav) >1){
11158: j=nbocc(modelsav,'+'); /**< j=Number of '+' */
11159: j1=nbocc(modelsav,'*'); /**< j1=Number of '*' */
11160: cptcovs=j+1-j1; /**< Number of simple covariates V1+V1*age+V3 +V3*V4+age*age=> V1 + V3 =5-3=2 */
11161: cptcovt= j+1; /* Number of total covariates in the model, not including
11162: * cst, age and age*age
11163: * V1+V1*age+ V3 + V3*V4+age*age=> 3+1=4*/
11164: /* including age products which are counted in cptcovage.
11165: * but the covariates which are products must be treated
11166: * separately: ncovn=4- 2=2 (V1+V3). */
11167: cptcovprod=j1; /**< Number of products V1*V2 +v3*age = 2 */
11168: cptcovprodnoage=0; /**< Number of covariate products without age: V3*V4 =1 */
11169:
11170:
11171: /* Design
11172: * V1 V2 V3 V4 V5 V6 V7 V8 V9 Weight
11173: * < ncovcol=8 >
11174: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8
11175: * k= 1 2 3 4 5 6 7 8
11176: * cptcovn number of covariates (not including constant and age ) = # of + plus 1 = 7+1=8
11177: * covar[k,i], are for fixed covariates, value of kth covariate if not including age for individual i:
11178: * covar[1][i]= (V1), covar[4][i]=(V4), covar[8][i]=(V8)
11179: * Tvar[k] # of the kth covariate: Tvar[1]=2 Tvar[2]=1 Tvar[4]=3 Tvar[8]=8
11180: * if multiplied by age: V3*age Tvar[3=V3*age]=3 (V3) Tvar[7]=8 and
11181: * Tage[++cptcovage]=k
11182: * if products, new covar are created after ncovcol + nqv (quanti fixed) with k1
11183: * Tvar[k]=ncovcol+k1; # of the kth covariate product: Tvar[5]=ncovcol+1=10 Tvar[6]=ncovcol+1=11
11184: * Tprod[k1]=k; Tprod[1]=5 Tprod[2]= 6; gives the position of the k1th product
11185: * 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
11186: * Tvar[cptcovn+k2]=Tvard[k1][1];Tvar[cptcovn+k2+1]=Tvard[k1][2];
11187: * Tvar[8+1]=5;Tvar[8+2]=6;Tvar[8+3]=7;Tvar[8+4]=8 inverted
11188: * V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11
11189: * < ncovcol=8 8 fixed covariate. Additional starts at 9 (V5*V6) and 10(V7*V8) >
11190: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8 d1 d1 d2 d2
11191: * k= 1 2 3 4 5 6 7 8 9 10 11 12
11192: * Tvard[k]= 2 1 3 3 10 11 8 8 5 6 7 8
11193: * p Tvar[1]@12={2, 1, 3, 3, 9, 10, 8, 8}
11194: * p Tprod[1]@2={ 6, 5}
11195: *p Tvard[1][1]@4= {7, 8, 5, 6}
11196: * covar[k][i]= V2 V1 ? V3 V5*V6? V7*V8? ? V8
11197: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
11198: *How to reorganize? Tvars(orted)
11199: * Model V1 + V2 + V3 + V8 + V5*V6 + V7*V8 + V3*age + V8*age
11200: * Tvars {2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
11201: * {2, 1, 4, 8, 5, 6, 3, 7}
11202: * Struct []
11203: */
11204:
11205: /* This loop fills the array Tvar from the string 'model'.*/
11206: /* j is the number of + signs in the model V1+V2+V3 j=2 i=3 to 1 */
11207: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4 */
11208: /* k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tage[cptcovage=1]=4 */
11209: /* k=3 V4 Tvar[k=3]= 4 (from V4) */
11210: /* k=2 V1 Tvar[k=2]= 1 (from V1) */
11211: /* k=1 Tvar[1]=2 (from V2) */
11212: /* k=5 Tvar[5] */
11213: /* for (k=1; k<=cptcovn;k++) { */
11214: /* cov[2+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
11215: /* } */
11216: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k])]]*cov[2]; */
11217: /*
11218: * Treating invertedly V2+V1+V3*age+V2*V4 is as if written V2*V4 +V3*age + V1 + V2 */
11219: for(k=cptcovt; k>=1;k--){ /**< Number of covariates not including constant and age, neither age*age*/
11220: Tvar[k]=0; Tprod[k]=0; Tposprod[k]=0;
11221: }
11222: cptcovage=0;
11223: for(k=1; k<=cptcovt;k++){ /* Loop on total covariates of the model line */
11224: cutl(stra,strb,modelsav,'+'); /* keeps in strb after the first '+' cutl from left to right
11225: 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" */
11226: if (nbocc(modelsav,'+')==0)
11227: strcpy(strb,modelsav); /* and analyzes it */
11228: /* printf("i=%d a=%s b=%s sav=%s\n",i, stra,strb,modelsav);*/
11229: /*scanf("%d",i);*/
11230: if (strchr(strb,'*')) { /**< Model includes a product V2+V1+V5*age+ V4+V3*age strb=V3*age */
11231: cutl(strc,strd,strb,'*'); /**< k=1 strd*strc Vm*Vn: strb=V3*age(input) strc=age strd=V3 ; V3*V2 strc=V2, strd=V3 */
11232: if (strcmp(strc,"age")==0) { /**< Model includes age: Vn*age */
11233: /* covar is not filled and then is empty */
11234: cptcovprod--;
11235: cutl(stre,strb,strd,'V'); /* strd=V3(input): stre="3" */
11236: 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 */
11237: Typevar[k]=1; /* 1 for age product */
11238: cptcovage++; /* Counts the number of covariates which include age as a product */
11239: Tage[cptcovage]=k; /* V2+V1+V4+V3*age Tvar[4]=3, Tage[1] = 4 or V1+V1*age Tvar[2]=1, Tage[1]=2 */
11240: /*printf("stre=%s ", stre);*/
11241: } else if (strcmp(strd,"age")==0) { /* or age*Vn */
11242: cptcovprod--;
11243: cutl(stre,strb,strc,'V');
11244: Tvar[k]=atoi(stre);
11245: Typevar[k]=1; /* 1 for age product */
11246: cptcovage++;
11247: Tage[cptcovage]=k;
11248: } else { /* Age is not in the model product V2+V1+V1*V4+V3*age+V3*V2 strb=V3*V2*/
11249: /* loops on k1=1 (V3*V2) and k1=2 V4*V3 */
11250: cptcovn++;
11251: cptcovprodnoage++;k1++;
11252: cutl(stre,strb,strc,'V'); /* strc= Vn, stre is n; strb=V3*V2 stre=3 strc=*/
11253: Tvar[k]=ncovcol+nqv+ntv+nqtv+k1; /* ncovcolt+k1; For model-covariate k tells which data-covariate to use but
11254: because this model-covariate is a construction we invent a new column
11255: which is after existing variables ncovcol+nqv+ntv+nqtv + k1
11256: If already ncovcol=4 and model= V2 + V1 + V1*V4 + age*V3 + V3*V2
11257: thus after V4 we invent V5 and V6 because age*V3 will be computed in 4
11258: Tvar[3=V1*V4]=4+1=5 Tvar[5=V3*V2]=4 + 2= 6, Tvar[4=age*V3]=3 etc */
11259: /* Please remark that the new variables are model dependent */
11260: /* If we have 4 variable but the model uses only 3, like in
11261: * model= V1 + age*V1 + V2 + V3 + age*V2 + age*V3 + V1*V2 + V1*V3
11262: * k= 1 2 3 4 5 6 7 8
11263: * Tvar[k]=1 1 2 3 2 3 (5 6) (and not 4 5 because of V4 missing)
11264: * Tage[kk] [1]= 2 [2]=5 [3]=6 kk=1 to cptcovage=3
11265: * Tvar[Tage[kk]][1]=2 [2]=2 [3]=3
11266: */
11267: Typevar[k]=2; /* 2 for product */
11268: cutl(strc,strb,strd,'V'); /* strd was Vm, strc is m */
11269: Tprod[k1]=k; /* Tprod[1]=3(=V1*V4) for V2+V1+V1*V4+age*V3+V3*V2 */
11270: Tposprod[k]=k1; /* Tposprod[3]=1, Tposprod[2]=5 */
11271: Tvard[k1][1] =atoi(strc); /* m 1 for V1*/
11272: Tvardk[k][1] =atoi(strc); /* m 1 for V1*/
11273: Tvard[k1][2] =atoi(stre); /* n 4 for V4*/
11274: Tvardk[k][2] =atoi(stre); /* n 4 for V4*/
11275: k2=k2+2; /* k2 is initialize to -1, We want to store the n and m in Vn*Vm at the end of Tvar */
11276: /* Tvar[cptcovt+k2]=Tvard[k1][1]; /\* Tvar[(cptcovt=4+k2=1)=5]= 1 (V1) *\/ */
11277: /* Tvar[cptcovt+k2+1]=Tvard[k1][2]; /\* Tvar[(cptcovt=4+(k2=1)+1)=6]= 4 (V4) *\/ */
11278: /*ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2, Tvar[3]=5, Tvar[4]=6, cptcovt=5 */
11279: /* 1 2 3 4 5 | Tvar[5+1)=1, Tvar[7]=2 */
11280: 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 */
11281: for (i=1; i<=lastobs;i++){/* For fixed product */
11282: /* Computes the new covariate which is a product of
11283: covar[n][i]* covar[m][i] and stores it at ncovol+k1 May not be defined */
11284: covar[ncovcolt+k1][i]=covar[atoi(stre)][i]*covar[atoi(strc)][i];
11285: }
11286: } /*End of FixedV */
11287: } /* End age is not in the model */
11288: } /* End if model includes a product */
11289: else { /* not a product */
11290: /*printf("d=%s c=%s b=%s\n", strd,strc,strb);*/
11291: /* scanf("%d",i);*/
11292: cutl(strd,strc,strb,'V');
11293: ks++; /**< Number of simple covariates dummy or quantitative, fixe or varying */
11294: cptcovn++; /** V4+V3+V5: V4 and V3 timevarying dummy covariates, V5 timevarying quantitative */
11295: Tvar[k]=atoi(strd);
11296: Typevar[k]=0; /* 0 for simple covariates */
11297: }
11298: strcpy(modelsav,stra); /* modelsav=V2+V1+V4 stra=V2+V1+V4 */
11299: /*printf("a=%s b=%s sav=%s\n", stra,strb,modelsav);
11300: scanf("%d",i);*/
11301: } /* end of loop + on total covariates */
11302: } /* end if strlen(modelsave == 0) age*age might exist */
11303: } /* end if strlen(model == 0) */
11304:
11305: /*The number n of Vn is stored in Tvar. cptcovage =number of age covariate. Tage gives the position of age. cptcovprod= number of products.
11306: If model=V1+V1*age then Tvar[1]=1 Tvar[2]=1 cptcovage=1 Tage[1]=2 cptcovprod=0*/
11307:
11308: /* printf("tvar1=%d tvar2=%d tvar3=%d cptcovage=%d Tage=%d",Tvar[1],Tvar[2],Tvar[3],cptcovage,Tage[1]);
11309: printf("cptcovprod=%d ", cptcovprod);
11310: fprintf(ficlog,"cptcovprod=%d ", cptcovprod);
11311: scanf("%d ",i);*/
11312:
11313:
11314: /* Until here, decodemodel knows only the grammar (simple, product, age*) of the model but not what kind
11315: of variable (dummy vs quantitative, fixed vs time varying) is behind. But we know the # of each. */
11316: /* ncovcol= 1, nqv=1 | ntv=2, nqtv= 1 = 5 possible variables data: 2 fixed 3, varying
11317: model= V5 + V4 +V3 + V4*V3 + V5*age + V2 + V1*V2 + V1*age + V5*age, V1 is not used saving its place
11318: k = 1 2 3 4 5 6 7 8 9
11319: Tvar[k]= 5 4 3 1+1+2+1+1=6 5 2 7 1 5
11320: Typevar[k]= 0 0 0 2 1 0 2 1 0
11321: Fixed[k] 1 1 1 1 3 0 0 or 2 2 3
11322: Dummy[k] 1 0 0 0 3 1 1 2 3
11323: Tmodelind[combination of covar]=k;
11324: */
11325: /* Dispatching between quantitative and time varying covariates */
11326: /* If Tvar[k] >ncovcol it is a product */
11327: /* Tvar[k] is the value n of Vn with n varying for 1 to nvcol, or p Vp=Vn*Vm for product */
11328: /* Computing effective variables, ie used by the model, that is from the cptcovt variables */
11329: printf("Model=1+age+%s\n\
11330: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product \n\
11331: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\
11332: 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);
11333: fprintf(ficlog,"Model=1+age+%s\n\
11334: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product \n\
11335: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\
11336: 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);
11337: for(k=-1;k<=NCOVMAX; k++){ Fixed[k]=0; Dummy[k]=0;}
11338: for(k=1;k<=NCOVMAX; k++){TvarFind[k]=0; TvarVind[k]=0;}
11339: for(k=1, ncovf=0, nsd=0, nsq=0, ncovv=0, ncova=0, ncoveff=0, nqfveff=0, ntveff=0, nqtveff=0, ncovvt=0;k<=cptcovt; k++){ /* or cptocvt loop on k from model */
11340: if (Tvar[k] <=ncovcol && Typevar[k]==0 ){ /* Simple fixed dummy (<=ncovcol) covariates */
11341: Fixed[k]= 0;
11342: Dummy[k]= 0;
11343: ncoveff++;
11344: ncovf++;
11345: nsd++;
11346: modell[k].maintype= FTYPE;
11347: TvarsD[nsd]=Tvar[k];
11348: TvarsDind[nsd]=k;
11349: TnsdVar[Tvar[k]]=nsd;
11350: TvarF[ncovf]=Tvar[k];
11351: TvarFind[ncovf]=k;
11352: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
11353: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
11354: /* }else if( Tvar[k] <=ncovcol && Typevar[k]==2){ /\* Product of fixed dummy (<=ncovcol) covariates For a fixed product k is higher than ncovcol *\/ */
11355: }else if( Tposprod[k]>0 && Typevar[k]==2 && 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 */
11356: Fixed[k]= 0;
11357: Dummy[k]= 0;
11358: ncoveff++;
11359: ncovf++;
11360: modell[k].maintype= FTYPE;
11361: TvarF[ncovf]=Tvar[k];
11362: /* TnsdVar[Tvar[k]]=nsd; */ /* To be done */
11363: TvarFind[ncovf]=k;
11364: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
11365: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
11366: }else if( Tvar[k] <=ncovcol+nqv && Typevar[k]==0){/* Remind that product Vn*Vm are added in k Only simple fixed quantitative variable */
11367: Fixed[k]= 0;
11368: Dummy[k]= 1;
11369: nqfveff++;
11370: modell[k].maintype= FTYPE;
11371: modell[k].subtype= FQ;
11372: nsq++;
11373: TvarsQ[nsq]=Tvar[k]; /* Gives the variable name (extended to products) of first nsq simple quantitative covariates (fixed or time vary see below */
11374: TvarsQind[nsq]=k; /* Gives the position in the model equation of the first nsq simple quantitative covariates (fixed or time vary) */
11375: ncovf++;
11376: TvarF[ncovf]=Tvar[k];
11377: TvarFind[ncovf]=k;
11378: 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 */
11379: 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 */
11380: }else if( Tvar[k] <=ncovcol+nqv+ntv && Typevar[k]==0){/* Only simple time varying dummy variables */
11381: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
11382: /* model V1+V3+age*V1+age*V3+V1*V3 */
11383: /* Tvar={1, 3, 1, 3, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
11384: ncovvt++;
11385: TvarVV[ncovvt]=Tvar[k]; /* TvarVV[1]=V3 (first time varying in the model equation */
11386: TvarVVind[ncovvt]=k; /* TvarVVind[1]=2 (second position in the model equation */
11387:
11388: Fixed[k]= 1;
11389: Dummy[k]= 0;
11390: ntveff++; /* Only simple time varying dummy variable */
11391: modell[k].maintype= VTYPE;
11392: modell[k].subtype= VD;
11393: nsd++;
11394: TvarsD[nsd]=Tvar[k];
11395: TvarsDind[nsd]=k;
11396: TnsdVar[Tvar[k]]=nsd; /* To be verified */
11397: ncovv++; /* Only simple time varying variables */
11398: TvarV[ncovv]=Tvar[k];
11399: 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 */
11400: 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 */
11401: 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 */
11402: 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);
11403: printf("Quasi TmodelInvind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv);
11404: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv && Typevar[k]==0){ /* Only simple time varying quantitative variable V5*/
11405: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
11406: /* model V1+V3+age*V1+age*V3+V1*V3 */
11407: /* Tvar={1, 3, 1, 3, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
11408: ncovvt++;
11409: TvarVV[ncovvt]=Tvar[k]; /* TvarVV[1]=V3 (first time varying in the model equation */
11410: TvarVVind[ncovvt]=k; /* TvarVV[1]=V3 (first time varying in the model equation */
11411:
11412: Fixed[k]= 1;
11413: Dummy[k]= 1;
11414: nqtveff++;
11415: modell[k].maintype= VTYPE;
11416: modell[k].subtype= VQ;
11417: ncovv++; /* Only simple time varying variables */
11418: nsq++;
11419: 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) */
11420: 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) */
11421: TvarV[ncovv]=Tvar[k];
11422: TvarVind[ncovv]=k; /* TvarVind[1]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Any time varying singele */
11423: 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 */
11424: 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 */
11425: TmodelInvQind[nqtveff]=Tvar[k]- ncovcol-nqv-ntv;/* Only simple time varying quantitative variable */
11426: /* Tmodeliqind[k]=nqtveff;/\* Only simple time varying quantitative variable *\/ */
11427: /* printf("Quasi TmodelQind[%d]=%d,Tvar[TmodelQind[%d]]=V%d, ncovcol=%d, nqv=%d, ntv=%d,Tvar[k]- ncovcol-nqv-ntv=%d\n",nqtveff,k,nqtveff,Tvar[k], ncovcol, nqv, ntv, Tvar[k]- ncovcol-nqv-ntv); */
11428: /* printf("Quasi TmodelInvQind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv-ntv); */
11429: }else if (Typevar[k] == 1) { /* product with age */
11430: ncova++;
11431: TvarA[ncova]=Tvar[k];
11432: TvarAind[ncova]=k;
11433: if (Tvar[k] <=ncovcol ){ /* Product age with fixed dummy covariatee */
11434: Fixed[k]= 2;
11435: Dummy[k]= 2;
11436: modell[k].maintype= ATYPE;
11437: modell[k].subtype= APFD;
11438: /* ncoveff++; */
11439: }else if( Tvar[k] <=ncovcol+nqv) { /* Remind that product Vn*Vm are added in k*/
11440: Fixed[k]= 2;
11441: Dummy[k]= 3;
11442: modell[k].maintype= ATYPE;
11443: modell[k].subtype= APFQ; /* Product age * fixed quantitative */
11444: /* nqfveff++; /\* Only simple fixed quantitative variable *\/ */
11445: }else if( Tvar[k] <=ncovcol+nqv+ntv ){
11446: Fixed[k]= 3;
11447: Dummy[k]= 2;
11448: modell[k].maintype= ATYPE;
11449: modell[k].subtype= APVD; /* Product age * varying dummy */
11450: /* ntveff++; /\* Only simple time varying dummy variable *\/ */
11451: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv){
11452: Fixed[k]= 3;
11453: Dummy[k]= 3;
11454: modell[k].maintype= ATYPE;
11455: modell[k].subtype= APVQ; /* Product age * varying quantitative */
11456: /* nqtveff++;/\* Only simple time varying quantitative variable *\/ */
11457: }
11458: }else if (Typevar[k] == 2) { /* product 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 */
11459: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
11460: /* model V1+V3+age*V1+age*V3+V1*V3 */
11461: /* Tvar={1, 3, 1, 3, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
11462: k1=Tposprod[k]; /* Position in the products of product k, Tposprod={0, 0, 0, 0, 1} k1=1 first product but second time varying because of V3 */
11463: ncovvt++;
11464: TvarVV[ncovvt]=Tvard[k1][1]; /* TvarVV[2]=V1 (because TvarVV[1] was V3, first time varying covariates */
11465: TvarVVind[ncovvt]=k; /* TvarVVind[2]=5 (because TvarVVind[2] was V1*V3 at position 5 */
11466: ncovvt++;
11467: TvarVV[ncovvt]=Tvard[k1][2]; /* TvarVV[3]=V3 */
11468: TvarVVind[ncovvt]=k; /* TvarVVind[2]=5 (because TvarVVind[2] was V1*V3 at position 5 */
11469:
11470:
11471: if(Tvard[k1][1] <=ncovcol){ /* Vn is dummy fixed, (Tvard[1][1]=V1), (Tvard[1][1]=V3 time varying) */
11472: if(Tvard[k1][2] <=ncovcol){ /* Vm is dummy fixed */
11473: Fixed[k]= 1;
11474: Dummy[k]= 0;
11475: modell[k].maintype= FTYPE;
11476: modell[k].subtype= FPDD; /* Product fixed dummy * fixed dummy */
11477: ncovf++; /* Fixed variables without age */
11478: TvarF[ncovf]=Tvar[k];
11479: TvarFind[ncovf]=k;
11480: }else if(Tvard[k1][2] <=ncovcol+nqv){ /* Vm is quanti fixed */
11481: Fixed[k]= 0; /* Fixed product */
11482: Dummy[k]= 1;
11483: modell[k].maintype= FTYPE;
11484: modell[k].subtype= FPDQ; /* Product fixed dummy * fixed quantitative */
11485: ncovf++; /* Varying variables without age */
11486: TvarF[ncovf]=Tvar[k];
11487: TvarFind[ncovf]=k;
11488: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){ /* Vm is a time varying dummy covariate */
11489: Fixed[k]= 1;
11490: Dummy[k]= 0;
11491: modell[k].maintype= VTYPE;
11492: modell[k].subtype= VPDD; /* Product fixed dummy * varying dummy */
11493: ncovv++; /* Varying variables without age */
11494: TvarV[ncovv]=Tvar[k]; /* TvarV[1]=Tvar[5]=5 because there is a V4 */
11495: TvarVind[ncovv]=k;/* TvarVind[1]=5 */
11496: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){ /* Vm is a time varying quantitative covariate */
11497: Fixed[k]= 1;
11498: Dummy[k]= 1;
11499: modell[k].maintype= VTYPE;
11500: modell[k].subtype= VPDQ; /* Product fixed dummy * varying quantitative */
11501: ncovv++; /* Varying variables without age */
11502: TvarV[ncovv]=Tvar[k];
11503: TvarVind[ncovv]=k;
11504: }
11505: }else if(Tvard[k1][1] <=ncovcol+nqv){ /* Vn is fixed quanti */
11506: if(Tvard[k1][2] <=ncovcol){ /* Vm is fixed dummy */
11507: Fixed[k]= 0; /* Fixed product */
11508: Dummy[k]= 1;
11509: modell[k].maintype= FTYPE;
11510: modell[k].subtype= FPDQ; /* Product fixed quantitative * fixed dummy */
11511: ncovf++; /* Fixed variables without age */
11512: TvarF[ncovf]=Tvar[k];
11513: TvarFind[ncovf]=k;
11514: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){ /* Vm is time varying */
11515: Fixed[k]= 1;
11516: Dummy[k]= 1;
11517: modell[k].maintype= VTYPE;
11518: modell[k].subtype= VPDQ; /* Product fixed quantitative * varying dummy */
11519: ncovv++; /* Varying variables without age */
11520: TvarV[ncovv]=Tvar[k];
11521: TvarVind[ncovv]=k;
11522: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){ /* Vm is time varying quanti */
11523: Fixed[k]= 1;
11524: Dummy[k]= 1;
11525: modell[k].maintype= VTYPE;
11526: modell[k].subtype= VPQQ; /* Product fixed quantitative * varying quantitative */
11527: ncovv++; /* Varying variables without age */
11528: TvarV[ncovv]=Tvar[k];
11529: TvarVind[ncovv]=k;
11530: ncovv++; /* Varying variables without age */
11531: TvarV[ncovv]=Tvar[k];
11532: TvarVind[ncovv]=k;
11533: }
11534: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv){ /* Vn is time varying dummy */
11535: if(Tvard[k1][2] <=ncovcol){
11536: Fixed[k]= 1;
11537: Dummy[k]= 1;
11538: modell[k].maintype= VTYPE;
11539: modell[k].subtype= VPDD; /* Product time varying dummy * fixed dummy */
11540: ncovv++; /* Varying variables without age */
11541: TvarV[ncovv]=Tvar[k];
11542: TvarVind[ncovv]=k;
11543: }else if(Tvard[k1][2] <=ncovcol+nqv){
11544: Fixed[k]= 1;
11545: Dummy[k]= 1;
11546: modell[k].maintype= VTYPE;
11547: modell[k].subtype= VPDQ; /* Product time varying dummy * fixed quantitative */
11548: ncovv++; /* Varying variables without age */
11549: TvarV[ncovv]=Tvar[k];
11550: TvarVind[ncovv]=k;
11551: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
11552: Fixed[k]= 1;
11553: Dummy[k]= 0;
11554: modell[k].maintype= VTYPE;
11555: modell[k].subtype= VPDD; /* Product time varying dummy * time varying dummy */
11556: ncovv++; /* Varying variables without age */
11557: TvarV[ncovv]=Tvar[k];
11558: TvarVind[ncovv]=k;
11559: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
11560: Fixed[k]= 1;
11561: Dummy[k]= 1;
11562: modell[k].maintype= VTYPE;
11563: modell[k].subtype= VPDQ; /* Product time varying dummy * time varying quantitative */
11564: ncovv++; /* Varying variables without age */
11565: TvarV[ncovv]=Tvar[k];
11566: TvarVind[ncovv]=k;
11567: }
11568: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv+nqtv){ /* Vn is time varying quanti */
11569: if(Tvard[k1][2] <=ncovcol){
11570: Fixed[k]= 1;
11571: Dummy[k]= 1;
11572: modell[k].maintype= VTYPE;
11573: modell[k].subtype= VPDQ; /* Product time varying quantitative * fixed dummy */
11574: ncovv++; /* Varying variables without age */
11575: TvarV[ncovv]=Tvar[k];
11576: TvarVind[ncovv]=k;
11577: }else if(Tvard[k1][2] <=ncovcol+nqv){
11578: Fixed[k]= 1;
11579: Dummy[k]= 1;
11580: modell[k].maintype= VTYPE;
11581: modell[k].subtype= VPQQ; /* Product time varying quantitative * fixed quantitative */
11582: ncovv++; /* Varying variables without age */
11583: TvarV[ncovv]=Tvar[k];
11584: TvarVind[ncovv]=k;
11585: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
11586: Fixed[k]= 1;
11587: Dummy[k]= 1;
11588: modell[k].maintype= VTYPE;
11589: modell[k].subtype= VPDQ; /* Product time varying quantitative * time varying dummy */
11590: ncovv++; /* Varying variables without age */
11591: TvarV[ncovv]=Tvar[k];
11592: TvarVind[ncovv]=k;
11593: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
11594: Fixed[k]= 1;
11595: Dummy[k]= 1;
11596: modell[k].maintype= VTYPE;
11597: modell[k].subtype= VPQQ; /* Product time varying quantitative * time varying quantitative */
11598: ncovv++; /* Varying variables without age */
11599: TvarV[ncovv]=Tvar[k];
11600: TvarVind[ncovv]=k;
11601: }
11602: }else{
11603: printf("Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
11604: fprintf(ficlog,"Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
11605: } /*end k1*/
11606: }else{
11607: printf("Error, current version can't treat for performance reasons, Tvar[%d]=%d, Typevar[%d]=%d\n", k, Tvar[k], k, Typevar[k]);
11608: fprintf(ficlog,"Error, current version can't treat for performance reasons, Tvar[%d]=%d, Typevar[%d]=%d\n", k, Tvar[k], k, Typevar[k]);
11609: }
11610: /* 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]); */
11611: /* printf(" modell[%d].maintype=%d, modell[%d].subtype=%d\n",k,modell[k].maintype,k,modell[k].subtype); */
11612: 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]);
11613: }
11614: /* Searching for doublons in the model */
11615: for(k1=1; k1<= cptcovt;k1++){
11616: for(k2=1; k2 <k1;k2++){
11617: /* if((Typevar[k1]==Typevar[k2]) && (Fixed[Tvar[k1]]==Fixed[Tvar[k2]]) && (Dummy[Tvar[k1]]==Dummy[Tvar[k2]] )){ */
11618: if((Typevar[k1]==Typevar[k2]) && (Fixed[k1]==Fixed[k2]) && (Dummy[k1]==Dummy[k2] )){
11619: if((Typevar[k1] == 0 || Typevar[k1] == 1)){ /* Simple or age product */
11620: if(Tvar[k1]==Tvar[k2]){
11621: 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]);
11622: 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);
11623: return(1);
11624: }
11625: }else if (Typevar[k1] ==2){
11626: k3=Tposprod[k1];
11627: k4=Tposprod[k2];
11628: 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])) ){
11629: 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]]);
11630: 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);
11631: return(1);
11632: }
11633: }
11634: }
11635: }
11636: }
11637: printf("ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
11638: fprintf(ficlog,"ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
11639: printf("ncovf=%d, ncovv=%d, ncova=%d, nsd=%d, nsq=%d\n",ncovf,ncovv,ncova,nsd,nsq);
11640: fprintf(ficlog,"ncovf=%d, ncovv=%d, ncova=%d, nsd=%d, nsq=%d\n",ncovf,ncovv,ncova,nsd, nsq);
11641: return (0); /* with covar[new additional covariate if product] and Tage if age */
11642: /*endread:*/
11643: printf("Exiting decodemodel: ");
11644: return (1);
11645: }
11646:
11647: int calandcheckages(int imx, int maxwav, double *agemin, double *agemax, int *nberr, int *nbwarn )
11648: {/* Check ages at death */
11649: int i, m;
11650: int firstone=0;
11651:
11652: for (i=1; i<=imx; i++) {
11653: for(m=2; (m<= maxwav); m++) {
11654: if (((int)mint[m][i]== 99) && (s[m][i] <= nlstate)){
11655: anint[m][i]=9999;
11656: if (s[m][i] != -2) /* Keeping initial status of unknown vital status */
11657: s[m][i]=-1;
11658: }
11659: if((int)moisdc[i]==99 && (int)andc[i]==9999 && s[m][i]>nlstate){
11660: *nberr = *nberr + 1;
11661: if(firstone == 0){
11662: firstone=1;
11663: 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);
11664: }
11665: 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);
11666: s[m][i]=-1; /* Droping the death status */
11667: }
11668: if((int)moisdc[i]==99 && (int)andc[i]!=9999 && s[m][i]>nlstate){
11669: (*nberr)++;
11670: 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);
11671: 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);
11672: s[m][i]=-2; /* We prefer to skip it (and to skip it in version 0.8a1 too */
11673: }
11674: }
11675: }
11676:
11677: for (i=1; i<=imx; i++) {
11678: agedc[i]=(moisdc[i]/12.+andc[i])-(moisnais[i]/12.+annais[i]);
11679: for(m=firstpass; (m<= lastpass); m++){
11680: 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 */
11681: if (s[m][i] >= nlstate+1) {
11682: if(agedc[i]>0){
11683: if((int)moisdc[i]!=99 && (int)andc[i]!=9999){
11684: agev[m][i]=agedc[i];
11685: /*if(moisdc[i]==99 && andc[i]==9999) s[m][i]=-1;*/
11686: }else {
11687: if ((int)andc[i]!=9999){
11688: nbwarn++;
11689: printf("Warning negative age at death: %ld line:%d\n",num[i],i);
11690: fprintf(ficlog,"Warning negative age at death: %ld line:%d\n",num[i],i);
11691: agev[m][i]=-1;
11692: }
11693: }
11694: } /* agedc > 0 */
11695: } /* end if */
11696: else if(s[m][i] !=9){ /* Standard case, age in fractional
11697: years but with the precision of a month */
11698: agev[m][i]=(mint[m][i]/12.+1./24.+anint[m][i])-(moisnais[i]/12.+1./24.+annais[i]);
11699: if((int)mint[m][i]==99 || (int)anint[m][i]==9999)
11700: agev[m][i]=1;
11701: else if(agev[m][i] < *agemin){
11702: *agemin=agev[m][i];
11703: printf(" Min anint[%d][%d]=%.2f annais[%d]=%.2f, agemin=%.2f\n",m,i,anint[m][i], i,annais[i], *agemin);
11704: }
11705: else if(agev[m][i] >*agemax){
11706: *agemax=agev[m][i];
11707: /* printf(" Max anint[%d][%d]=%.0f annais[%d]=%.0f, agemax=%.2f\n",m,i,anint[m][i], i,annais[i], *agemax);*/
11708: }
11709: /*agev[m][i]=anint[m][i]-annais[i];*/
11710: /* agev[m][i] = age[i]+2*m;*/
11711: } /* en if 9*/
11712: else { /* =9 */
11713: /* printf("Debug num[%d]=%ld s[%d][%d]=%d\n",i,num[i], m,i, s[m][i]); */
11714: agev[m][i]=1;
11715: s[m][i]=-1;
11716: }
11717: }
11718: else if(s[m][i]==0) /*= 0 Unknown */
11719: agev[m][i]=1;
11720: else{
11721: printf("Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
11722: fprintf(ficlog, "Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
11723: agev[m][i]=0;
11724: }
11725: } /* End for lastpass */
11726: }
11727:
11728: for (i=1; i<=imx; i++) {
11729: for(m=firstpass; (m<=lastpass); m++){
11730: if (s[m][i] > (nlstate+ndeath)) {
11731: (*nberr)++;
11732: 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);
11733: 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);
11734: return 1;
11735: }
11736: }
11737: }
11738:
11739: /*for (i=1; i<=imx; i++){
11740: for (m=firstpass; (m<lastpass); m++){
11741: printf("%ld %d %.lf %d %d\n", num[i],(covar[1][i]),agev[m][i],s[m][i],s[m+1][i]);
11742: }
11743:
11744: }*/
11745:
11746:
11747: printf("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
11748: fprintf(ficlog,"Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
11749:
11750: return (0);
11751: /* endread:*/
11752: printf("Exiting calandcheckages: ");
11753: return (1);
11754: }
11755:
11756: #if defined(_MSC_VER)
11757: /*printf("Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
11758: /*fprintf(ficlog, "Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
11759: //#include "stdafx.h"
11760: //#include <stdio.h>
11761: //#include <tchar.h>
11762: //#include <windows.h>
11763: //#include <iostream>
11764: typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
11765:
11766: LPFN_ISWOW64PROCESS fnIsWow64Process;
11767:
11768: BOOL IsWow64()
11769: {
11770: BOOL bIsWow64 = FALSE;
11771:
11772: //typedef BOOL (APIENTRY *LPFN_ISWOW64PROCESS)
11773: // (HANDLE, PBOOL);
11774:
11775: //LPFN_ISWOW64PROCESS fnIsWow64Process;
11776:
11777: HMODULE module = GetModuleHandle(_T("kernel32"));
11778: const char funcName[] = "IsWow64Process";
11779: fnIsWow64Process = (LPFN_ISWOW64PROCESS)
11780: GetProcAddress(module, funcName);
11781:
11782: if (NULL != fnIsWow64Process)
11783: {
11784: if (!fnIsWow64Process(GetCurrentProcess(),
11785: &bIsWow64))
11786: //throw std::exception("Unknown error");
11787: printf("Unknown error\n");
11788: }
11789: return bIsWow64 != FALSE;
11790: }
11791: #endif
11792:
11793: void syscompilerinfo(int logged)
11794: {
11795: #include <stdint.h>
11796:
11797: /* #include "syscompilerinfo.h"*/
11798: /* command line Intel compiler 32bit windows, XP compatible:*/
11799: /* /GS /W3 /Gy
11800: /Zc:wchar_t /Zi /O2 /Fd"Release\vc120.pdb" /D "WIN32" /D "NDEBUG" /D
11801: "_CONSOLE" /D "_LIB" /D "_USING_V110_SDK71_" /D "_UNICODE" /D
11802: "UNICODE" /Qipo /Zc:forScope /Gd /Oi /MT /Fa"Release\" /EHsc /nologo
11803: /Fo"Release\" /Qprof-dir "Release\" /Fp"Release\IMaCh.pch"
11804: */
11805: /* 64 bits */
11806: /*
11807: /GS /W3 /Gy
11808: /Zc:wchar_t /Zi /O2 /Fd"x64\Release\vc120.pdb" /D "WIN32" /D "NDEBUG"
11809: /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo /Zc:forScope
11810: /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Fo"x64\Release\" /Qprof-dir
11811: "x64\Release\" /Fp"x64\Release\IMaCh.pch" */
11812: /* Optimization are useless and O3 is slower than O2 */
11813: /*
11814: /GS /W3 /Gy /Zc:wchar_t /Zi /O3 /Fd"x64\Release\vc120.pdb" /D "WIN32"
11815: /D "NDEBUG" /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo
11816: /Zc:forScope /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Qparallel
11817: /Fo"x64\Release\" /Qprof-dir "x64\Release\" /Fp"x64\Release\IMaCh.pch"
11818: */
11819: /* Link is */ /* /OUT:"visual studio
11820: 2013\Projects\IMaCh\Release\IMaCh.exe" /MANIFEST /NXCOMPAT
11821: /PDB:"visual studio
11822: 2013\Projects\IMaCh\Release\IMaCh.pdb" /DYNAMICBASE
11823: "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib"
11824: "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib"
11825: "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib"
11826: /MACHINE:X86 /OPT:REF /SAFESEH /INCREMENTAL:NO
11827: /SUBSYSTEM:CONSOLE",5.01" /MANIFESTUAC:"level='asInvoker'
11828: uiAccess='false'"
11829: /ManifestFile:"Release\IMaCh.exe.intermediate.manifest" /OPT:ICF
11830: /NOLOGO /TLBID:1
11831: */
11832:
11833:
11834: #if defined __INTEL_COMPILER
11835: #if defined(__GNUC__)
11836: struct utsname sysInfo; /* For Intel on Linux and OS/X */
11837: #endif
11838: #elif defined(__GNUC__)
11839: #ifndef __APPLE__
11840: #include <gnu/libc-version.h> /* Only on gnu */
11841: #endif
11842: struct utsname sysInfo;
11843: int cross = CROSS;
11844: if (cross){
11845: printf("Cross-");
11846: if(logged) fprintf(ficlog, "Cross-");
11847: }
11848: #endif
11849:
11850: printf("Compiled with:");if(logged)fprintf(ficlog,"Compiled with:");
11851: #if defined(__clang__)
11852: printf(" Clang/LLVM");if(logged)fprintf(ficlog," Clang/LLVM"); /* Clang/LLVM. ---------------------------------------------- */
11853: #endif
11854: #if defined(__ICC) || defined(__INTEL_COMPILER)
11855: printf(" Intel ICC/ICPC");if(logged)fprintf(ficlog," Intel ICC/ICPC");/* Intel ICC/ICPC. ------------------------------------------ */
11856: #endif
11857: #if defined(__GNUC__) || defined(__GNUG__)
11858: printf(" GNU GCC/G++");if(logged)fprintf(ficlog," GNU GCC/G++");/* GNU GCC/G++. --------------------------------------------- */
11859: #endif
11860: #if defined(__HP_cc) || defined(__HP_aCC)
11861: printf(" Hewlett-Packard C/aC++");if(logged)fprintf(fcilog," Hewlett-Packard C/aC++"); /* Hewlett-Packard C/aC++. ---------------------------------- */
11862: #endif
11863: #if defined(__IBMC__) || defined(__IBMCPP__)
11864: printf(" IBM XL C/C++"); if(logged) fprintf(ficlog," IBM XL C/C++");/* IBM XL C/C++. -------------------------------------------- */
11865: #endif
11866: #if defined(_MSC_VER)
11867: printf(" Microsoft Visual Studio");if(logged)fprintf(ficlog," Microsoft Visual Studio");/* Microsoft Visual Studio. --------------------------------- */
11868: #endif
11869: #if defined(__PGI)
11870: printf(" Portland Group PGCC/PGCPP");if(logged) fprintf(ficlog," Portland Group PGCC/PGCPP");/* Portland Group PGCC/PGCPP. ------------------------------- */
11871: #endif
11872: #if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
11873: printf(" Oracle Solaris Studio");if(logged)fprintf(ficlog," Oracle Solaris Studio\n");/* Oracle Solaris Studio. ----------------------------------- */
11874: #endif
11875: printf(" for "); if (logged) fprintf(ficlog, " for ");
11876:
11877: // http://stackoverflow.com/questions/4605842/how-to-identify-platform-compiler-from-preprocessor-macros
11878: #ifdef _WIN32 // note the underscore: without it, it's not msdn official!
11879: // Windows (x64 and x86)
11880: printf("Windows (x64 and x86) ");if(logged) fprintf(ficlog,"Windows (x64 and x86) ");
11881: #elif __unix__ // all unices, not all compilers
11882: // Unix
11883: printf("Unix ");if(logged) fprintf(ficlog,"Unix ");
11884: #elif __linux__
11885: // linux
11886: printf("linux ");if(logged) fprintf(ficlog,"linux ");
11887: #elif __APPLE__
11888: // Mac OS, not sure if this is covered by __posix__ and/or __unix__ though..
11889: printf("Mac OS ");if(logged) fprintf(ficlog,"Mac OS ");
11890: #endif
11891:
11892: /* __MINGW32__ */
11893: /* __CYGWIN__ */
11894: /* __MINGW64__ */
11895: // http://msdn.microsoft.com/en-us/library/b0084kay.aspx
11896: /* _MSC_VER //the Visual C++ compiler is 17.00.51106.1, the _MSC_VER macro evaluates to 1700. Type cl /? */
11897: /* _MSC_FULL_VER //the Visual C++ compiler is 15.00.20706.01, the _MSC_FULL_VER macro evaluates to 150020706 */
11898: /* _WIN64 // Defined for applications for Win64. */
11899: /* _M_X64 // Defined for compilations that target x64 processors. */
11900: /* _DEBUG // Defined when you compile with /LDd, /MDd, and /MTd. */
11901:
11902: #if UINTPTR_MAX == 0xffffffff
11903: printf(" 32-bit"); if(logged) fprintf(ficlog," 32-bit");/* 32-bit */
11904: #elif UINTPTR_MAX == 0xffffffffffffffff
11905: printf(" 64-bit"); if(logged) fprintf(ficlog," 64-bit");/* 64-bit */
11906: #else
11907: printf(" wtf-bit"); if(logged) fprintf(ficlog," wtf-bit");/* wtf */
11908: #endif
11909:
11910: #if defined(__GNUC__)
11911: # if defined(__GNUC_PATCHLEVEL__)
11912: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
11913: + __GNUC_MINOR__ * 100 \
11914: + __GNUC_PATCHLEVEL__)
11915: # else
11916: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
11917: + __GNUC_MINOR__ * 100)
11918: # endif
11919: printf(" using GNU C version %d.\n", __GNUC_VERSION__);
11920: if(logged) fprintf(ficlog, " using GNU C version %d.\n", __GNUC_VERSION__);
11921:
11922: if (uname(&sysInfo) != -1) {
11923: printf("Running on: %s %s %s %s %s\n",sysInfo.sysname, sysInfo.nodename, sysInfo.release, sysInfo.version, sysInfo.machine);
11924: if(logged) fprintf(ficlog,"Running on: %s %s %s %s %s\n ",sysInfo.sysname, sysInfo.nodename, sysInfo.release, sysInfo.version, sysInfo.machine);
11925: }
11926: else
11927: perror("uname() error");
11928: //#ifndef __INTEL_COMPILER
11929: #if !defined (__INTEL_COMPILER) && !defined(__APPLE__)
11930: printf("GNU libc version: %s\n", gnu_get_libc_version());
11931: if(logged) fprintf(ficlog,"GNU libc version: %s\n", gnu_get_libc_version());
11932: #endif
11933: #endif
11934:
11935: // void main ()
11936: // {
11937: #if defined(_MSC_VER)
11938: if (IsWow64()){
11939: printf("\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
11940: if (logged) fprintf(ficlog, "\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
11941: }
11942: else{
11943: printf("\nThe program is not running under WOW64 (i.e probably on a 64bit Windows).\n");
11944: if (logged) fprintf(ficlog, "\nThe programm is not running under WOW64 (i.e probably on a 64bit Windows).\n");
11945: }
11946: // printf("\nPress Enter to continue...");
11947: // getchar();
11948: // }
11949:
11950: #endif
11951:
11952:
11953: }
11954:
11955: int prevalence_limit(double *p, double **prlim, double ageminpar, double agemaxpar, double ftolpl, int *ncvyearp){
11956: /*--------------- Prevalence limit (forward period or forward stable prevalence) --------------*/
11957: /* Computes the prevalence limit for each combination of the dummy covariates */
11958: int i, j, k, i1, k4=0, nres=0 ;
11959: /* double ftolpl = 1.e-10; */
11960: double age, agebase, agelim;
11961: double tot;
11962:
11963: strcpy(filerespl,"PL_");
11964: strcat(filerespl,fileresu);
11965: if((ficrespl=fopen(filerespl,"w"))==NULL) {
11966: printf("Problem with forward period (stable) prevalence resultfile: %s\n", filerespl);return 1;
11967: fprintf(ficlog,"Problem with forward period (stable) prevalence resultfile: %s\n", filerespl);return 1;
11968: }
11969: printf("\nComputing forward period (stable) prevalence: result on file '%s' \n", filerespl);
11970: fprintf(ficlog,"\nComputing forward period (stable) prevalence: result on file '%s' \n", filerespl);
11971: pstamp(ficrespl);
11972: fprintf(ficrespl,"# Forward period (stable) prevalence. Precision given by ftolpl=%g \n", ftolpl);
11973: fprintf(ficrespl,"#Age ");
11974: for(i=1; i<=nlstate;i++) fprintf(ficrespl,"%d-%d ",i,i);
11975: fprintf(ficrespl,"\n");
11976:
11977: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
11978:
11979: agebase=ageminpar;
11980: agelim=agemaxpar;
11981:
11982: /* i1=pow(2,ncoveff); */
11983: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
11984: if (cptcovn < 1){i1=1;}
11985:
11986: /* for(k=1; k<=i1;k++){ /\* For each combination k of dummy covariates in the model *\/ */
11987: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
11988: k=TKresult[nres];
11989: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
11990: /* if(i1 != 1 && TKresult[nres]!= k) /\* We found the combination k corresponding to the resultline value of dummies *\/ */
11991: /* continue; */
11992:
11993: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
11994: /* for(cptcov=1,k=0;cptcov<=1;cptcov++){ */
11995: //for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
11996: /* k=k+1; */
11997: /* to clean */
11998: /*printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));*/
11999: fprintf(ficrespl,"#******");
12000: printf("#******");
12001: fprintf(ficlog,"#******");
12002: 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) */
12003: /* fprintf(ficrespl," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,Tvaraff[j])]); /\* Here problem for varying dummy*\/ */
12004: /* printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
12005: /* fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
12006: fprintf(ficrespl," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
12007: printf(" V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
12008: fprintf(ficlog," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
12009: }
12010: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
12011: /* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
12012: /* fprintf(ficrespl," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
12013: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
12014: /* } */
12015: fprintf(ficrespl,"******\n");
12016: printf("******\n");
12017: fprintf(ficlog,"******\n");
12018: if(invalidvarcomb[k]){
12019: printf("\nCombination (%d) ignored because no case \n",k);
12020: fprintf(ficrespl,"#Combination (%d) ignored because no case \n",k);
12021: fprintf(ficlog,"\nCombination (%d) ignored because no case \n",k);
12022: continue;
12023: }
12024:
12025: fprintf(ficrespl,"#Age ");
12026: /* for(j=1;j<=cptcoveff;j++) { */
12027: /* fprintf(ficrespl,"V%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
12028: /* } */
12029: for(j=1;j<=cptcovs;j++) { /* New the quanti variable is added */
12030: fprintf(ficrespl,"V%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
12031: }
12032: for(i=1; i<=nlstate;i++) fprintf(ficrespl," %d-%d ",i,i);
12033: fprintf(ficrespl,"Total Years_to_converge\n");
12034:
12035: for (age=agebase; age<=agelim; age++){
12036: /* for (age=agebase; age<=agebase; age++){ */
12037: /**< Computes the prevalence limit in each live state at age x and for covariate combination (k and) nres */
12038: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, ncvyearp, k, nres); /* Nicely done */
12039: fprintf(ficrespl,"%.0f ",age );
12040: /* for(j=1;j<=cptcoveff;j++) */
12041: /* fprintf(ficrespl,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
12042: for(j=1;j<=cptcovs;j++)
12043: fprintf(ficrespl,"%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
12044: tot=0.;
12045: for(i=1; i<=nlstate;i++){
12046: tot += prlim[i][i];
12047: fprintf(ficrespl," %.5f", prlim[i][i]);
12048: }
12049: fprintf(ficrespl," %.3f %d\n", tot, *ncvyearp);
12050: } /* Age */
12051: /* was end of cptcod */
12052: } /* nres */
12053: /* } /\* for each combination *\/ */
12054: return 0;
12055: }
12056:
12057: 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){
12058: /*--------------- Back Prevalence limit (backward stable prevalence) --------------*/
12059:
12060: /* Computes the back prevalence limit for any combination of covariate values
12061: * at any age between ageminpar and agemaxpar
12062: */
12063: int i, j, k, i1, nres=0 ;
12064: /* double ftolpl = 1.e-10; */
12065: double age, agebase, agelim;
12066: double tot;
12067: /* double ***mobaverage; */
12068: /* double **dnewm, **doldm, **dsavm; /\* for use *\/ */
12069:
12070: strcpy(fileresplb,"PLB_");
12071: strcat(fileresplb,fileresu);
12072: if((ficresplb=fopen(fileresplb,"w"))==NULL) {
12073: printf("Problem with backward prevalence resultfile: %s\n", fileresplb);return 1;
12074: fprintf(ficlog,"Problem with backward prevalence resultfile: %s\n", fileresplb);return 1;
12075: }
12076: printf("Computing backward prevalence: result on file '%s' \n", fileresplb);
12077: fprintf(ficlog,"Computing backward prevalence: result on file '%s' \n", fileresplb);
12078: pstamp(ficresplb);
12079: fprintf(ficresplb,"# Backward prevalence. Precision given by ftolpl=%g \n", ftolpl);
12080: fprintf(ficresplb,"#Age ");
12081: for(i=1; i<=nlstate;i++) fprintf(ficresplb,"%d-%d ",i,i);
12082: fprintf(ficresplb,"\n");
12083:
12084:
12085: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
12086:
12087: agebase=ageminpar;
12088: agelim=agemaxpar;
12089:
12090:
12091: i1=pow(2,cptcoveff);
12092: if (cptcovn < 1){i1=1;}
12093:
12094: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
12095: /* for(k=1; k<=i1;k++){ /\* For any combination of dummy covariates, fixed and varying *\/ */
12096: k=TKresult[nres];
12097: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
12098: /* if(i1 != 1 && TKresult[nres]!= k) */
12099: /* continue; */
12100: /* /\*printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));*\/ */
12101: fprintf(ficresplb,"#******");
12102: printf("#******");
12103: fprintf(ficlog,"#******");
12104: 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) */
12105: printf(" V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
12106: fprintf(ficresplb," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
12107: fprintf(ficlog," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
12108: }
12109: /* for(j=1;j<=cptcoveff ;j++) {/\* all covariates *\/ */
12110: /* fprintf(ficresplb," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
12111: /* printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
12112: /* fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
12113: /* } */
12114: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
12115: /* printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
12116: /* fprintf(ficresplb," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
12117: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
12118: /* } */
12119: fprintf(ficresplb,"******\n");
12120: printf("******\n");
12121: fprintf(ficlog,"******\n");
12122: if(invalidvarcomb[k]){
12123: printf("\nCombination (%d) ignored because no cases \n",k);
12124: fprintf(ficresplb,"#Combination (%d) ignored because no cases \n",k);
12125: fprintf(ficlog,"\nCombination (%d) ignored because no cases \n",k);
12126: continue;
12127: }
12128:
12129: fprintf(ficresplb,"#Age ");
12130: for(j=1;j<=cptcovs;j++) {
12131: fprintf(ficresplb,"V%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
12132: }
12133: for(i=1; i<=nlstate;i++) fprintf(ficresplb," %d-%d ",i,i);
12134: fprintf(ficresplb,"Total Years_to_converge\n");
12135:
12136:
12137: for (age=agebase; age<=agelim; age++){
12138: /* for (age=agebase; age<=agebase; age++){ */
12139: if(mobilavproj > 0){
12140: /* bprevalim(bprlim, mobaverage, nlstate, p, age, ageminpar, agemaxpar, oldm, savm, doldm, dsavm, ftolpl, ncvyearp, k); */
12141: /* bprevalim(bprlim, mobaverage, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
12142: bprevalim(bprlim, mobaverage, nlstate, p, age, ftolpl, ncvyearp, k, nres);
12143: }else if (mobilavproj == 0){
12144: 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);
12145: 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);
12146: exit(1);
12147: }else{
12148: /* bprevalim(bprlim, probs, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
12149: bprevalim(bprlim, probs, nlstate, p, age, ftolpl, ncvyearp, k,nres);
12150: /* printf("TOTOT\n"); */
12151: /* exit(1); */
12152: }
12153: fprintf(ficresplb,"%.0f ",age );
12154: for(j=1;j<=cptcovs;j++)
12155: fprintf(ficresplb,"%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
12156: tot=0.;
12157: for(i=1; i<=nlstate;i++){
12158: tot += bprlim[i][i];
12159: fprintf(ficresplb," %.5f", bprlim[i][i]);
12160: }
12161: fprintf(ficresplb," %.3f %d\n", tot, *ncvyearp);
12162: } /* Age */
12163: /* was end of cptcod */
12164: /*fprintf(ficresplb,"\n");*/ /* Seems to be necessary for gnuplot only if two result lines and no covariate. */
12165: /* } /\* end of any combination *\/ */
12166: } /* end of nres */
12167: /* hBijx(p, bage, fage); */
12168: /* fclose(ficrespijb); */
12169:
12170: return 0;
12171: }
12172:
12173: int hPijx(double *p, int bage, int fage){
12174: /*------------- h Pij x at various ages ------------*/
12175: /* to be optimized with precov */
12176: int stepsize;
12177: int agelim;
12178: int hstepm;
12179: int nhstepm;
12180: int h, i, i1, j, k, k4, nres=0;
12181:
12182: double agedeb;
12183: double ***p3mat;
12184:
12185: strcpy(filerespij,"PIJ_"); strcat(filerespij,fileresu);
12186: if((ficrespij=fopen(filerespij,"w"))==NULL) {
12187: printf("Problem with Pij resultfile: %s\n", filerespij); return 1;
12188: fprintf(ficlog,"Problem with Pij resultfile: %s\n", filerespij); return 1;
12189: }
12190: printf("Computing pij: result on file '%s' \n", filerespij);
12191: fprintf(ficlog,"Computing pij: result on file '%s' \n", filerespij);
12192:
12193: stepsize=(int) (stepm+YEARM-1)/YEARM;
12194: /*if (stepm<=24) stepsize=2;*/
12195:
12196: agelim=AGESUP;
12197: hstepm=stepsize*YEARM; /* Every year of age */
12198: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
12199:
12200: /* hstepm=1; aff par mois*/
12201: pstamp(ficrespij);
12202: fprintf(ficrespij,"#****** h Pij x Probability to be in state j at age x+h being in i at x ");
12203: i1= pow(2,cptcoveff);
12204: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
12205: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
12206: /* k=k+1; */
12207: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
12208: k=TKresult[nres];
12209: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
12210: /* for(k=1; k<=i1;k++){ */
12211: /* if(i1 != 1 && TKresult[nres]!= k) */
12212: /* continue; */
12213: fprintf(ficrespij,"\n#****** ");
12214: for(j=1;j<=cptcovs;j++){
12215: fprintf(ficrespij," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
12216: /* fprintf(ficrespij,"@wV%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
12217: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
12218: /* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
12219: /* fprintf(ficrespij," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
12220: }
12221: fprintf(ficrespij,"******\n");
12222:
12223: for (agedeb=fage; agedeb>=bage; agedeb--){ /* If stepm=6 months */
12224: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
12225: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
12226:
12227: /* nhstepm=nhstepm*YEARM; aff par mois*/
12228:
12229: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
12230: oldm=oldms;savm=savms;
12231: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k, nres);
12232: fprintf(ficrespij,"# Cov Agex agex+h hpijx with i,j=");
12233: for(i=1; i<=nlstate;i++)
12234: for(j=1; j<=nlstate+ndeath;j++)
12235: fprintf(ficrespij," %1d-%1d",i,j);
12236: fprintf(ficrespij,"\n");
12237: for (h=0; h<=nhstepm; h++){
12238: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
12239: fprintf(ficrespij,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm );
12240: for(i=1; i<=nlstate;i++)
12241: for(j=1; j<=nlstate+ndeath;j++)
12242: fprintf(ficrespij," %.5f", p3mat[i][j][h]);
12243: fprintf(ficrespij,"\n");
12244: }
12245: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
12246: fprintf(ficrespij,"\n");
12247: }
12248: }
12249: /*}*/
12250: return 0;
12251: }
12252:
12253: int hBijx(double *p, int bage, int fage, double ***prevacurrent){
12254: /*------------- h Bij x at various ages ------------*/
12255: /* To be optimized with precov */
12256: int stepsize;
12257: /* int agelim; */
12258: int ageminl;
12259: int hstepm;
12260: int nhstepm;
12261: int h, i, i1, j, k, nres;
12262:
12263: double agedeb;
12264: double ***p3mat;
12265:
12266: strcpy(filerespijb,"PIJB_"); strcat(filerespijb,fileresu);
12267: if((ficrespijb=fopen(filerespijb,"w"))==NULL) {
12268: printf("Problem with Pij back resultfile: %s\n", filerespijb); return 1;
12269: fprintf(ficlog,"Problem with Pij back resultfile: %s\n", filerespijb); return 1;
12270: }
12271: printf("Computing pij back: result on file '%s' \n", filerespijb);
12272: fprintf(ficlog,"Computing pij back: result on file '%s' \n", filerespijb);
12273:
12274: stepsize=(int) (stepm+YEARM-1)/YEARM;
12275: /*if (stepm<=24) stepsize=2;*/
12276:
12277: /* agelim=AGESUP; */
12278: ageminl=AGEINF; /* was 30 */
12279: hstepm=stepsize*YEARM; /* Every year of age */
12280: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
12281:
12282: /* hstepm=1; aff par mois*/
12283: pstamp(ficrespijb);
12284: fprintf(ficrespijb,"#****** h Bij x Back probability to be in state i at age x-h being in j at x: B1j+B2j+...=1 ");
12285: i1= pow(2,cptcoveff);
12286: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
12287: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
12288: /* k=k+1; */
12289: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
12290: k=TKresult[nres];
12291: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
12292: /* for(k=1; k<=i1;k++){ /\* For any combination of dummy covariates, fixed and varying *\/ */
12293: /* if(i1 != 1 && TKresult[nres]!= k) */
12294: /* continue; */
12295: fprintf(ficrespijb,"\n#****** ");
12296: for(j=1;j<=cptcovs;j++){
12297: fprintf(ficrespijb," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
12298: /* for(j=1;j<=cptcoveff;j++) */
12299: /* fprintf(ficrespijb,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
12300: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
12301: /* fprintf(ficrespijb," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
12302: }
12303: fprintf(ficrespijb,"******\n");
12304: if(invalidvarcomb[k]){ /* Is it necessary here? */
12305: fprintf(ficrespijb,"\n#Combination (%d) ignored because no cases \n",k);
12306: continue;
12307: }
12308:
12309: /* for (agedeb=fage; agedeb>=bage; agedeb--){ /\* If stepm=6 months *\/ */
12310: for (agedeb=bage; agedeb<=fage; agedeb++){ /* If stepm=6 months and estepm=24 (2 years) */
12311: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /\* Typically 20 years = 20*12/6=40 *\/ */
12312: 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 */
12313: nhstepm = nhstepm/hstepm; /* Typically 40/4=10, because estepm=24 stepm=6 => hstepm=24/6=4 or 28*/
12314:
12315: /* nhstepm=nhstepm*YEARM; aff par mois*/
12316:
12317: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); /* We can't have it at an upper level because of nhstepm */
12318: /* and memory limitations if stepm is small */
12319:
12320: /* oldm=oldms;savm=savms; */
12321: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
12322: hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm, k, nres);/* Bug valgrind */
12323: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm,oldm,savm, dnewm, doldm, dsavm, k); */
12324: fprintf(ficrespijb,"# Cov Agex agex-h hbijx with i,j=");
12325: for(i=1; i<=nlstate;i++)
12326: for(j=1; j<=nlstate+ndeath;j++)
12327: fprintf(ficrespijb," %1d-%1d",i,j);
12328: fprintf(ficrespijb,"\n");
12329: for (h=0; h<=nhstepm; h++){
12330: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
12331: fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb - h*hstepm/YEARM*stepm );
12332: /* fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm ); */
12333: for(i=1; i<=nlstate;i++)
12334: for(j=1; j<=nlstate+ndeath;j++)
12335: fprintf(ficrespijb," %.5f", p3mat[i][j][h]);/* Bug valgrind */
12336: fprintf(ficrespijb,"\n");
12337: }
12338: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
12339: fprintf(ficrespijb,"\n");
12340: } /* end age deb */
12341: /* } /\* end combination *\/ */
12342: } /* end nres */
12343: return 0;
12344: } /* hBijx */
12345:
12346:
12347: /***********************************************/
12348: /**************** Main Program *****************/
12349: /***********************************************/
12350:
12351: int main(int argc, char *argv[])
12352: {
12353: #ifdef GSL
12354: const gsl_multimin_fminimizer_type *T;
12355: size_t iteri = 0, it;
12356: int rval = GSL_CONTINUE;
12357: int status = GSL_SUCCESS;
12358: double ssval;
12359: #endif
12360: int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav);
12361: int i,j, k, iter=0,m,size=100, cptcod; /* Suppressing because nobs */
12362: /* int i,j, k, n=MAXN,iter=0,m,size=100, cptcod; */
12363: int ncvyear=0; /* Number of years needed for the period prevalence to converge */
12364: int jj, ll, li, lj, lk;
12365: int numlinepar=0; /* Current linenumber of parameter file */
12366: int num_filled;
12367: int itimes;
12368: int NDIM=2;
12369: int vpopbased=0;
12370: int nres=0;
12371: int endishere=0;
12372: int noffset=0;
12373: int ncurrv=0; /* Temporary variable */
12374:
12375: char ca[32], cb[32];
12376: /* FILE *fichtm; *//* Html File */
12377: /* FILE *ficgp;*/ /*Gnuplot File */
12378: struct stat info;
12379: double agedeb=0.;
12380:
12381: double ageminpar=AGEOVERFLOW,agemin=AGEOVERFLOW, agemaxpar=-AGEOVERFLOW, agemax=-AGEOVERFLOW;
12382: double ageminout=-AGEOVERFLOW,agemaxout=AGEOVERFLOW; /* Smaller Age range redefined after movingaverage */
12383:
12384: double fret;
12385: double dum=0.; /* Dummy variable */
12386: double ***p3mat;
12387: /* double ***mobaverage; */
12388: double wald;
12389:
12390: char line[MAXLINE];
12391: char path[MAXLINE],pathc[MAXLINE],pathcd[MAXLINE],pathtot[MAXLINE];
12392:
12393: char modeltemp[MAXLINE];
12394: char resultline[MAXLINE], resultlineori[MAXLINE];
12395:
12396: char pathr[MAXLINE], pathimach[MAXLINE];
12397: char *tok, *val; /* pathtot */
12398: /* int firstobs=1, lastobs=10; /\* nobs = lastobs-firstobs declared globally ;*\/ */
12399: int c, h , cpt, c2;
12400: int jl=0;
12401: int i1, j1, jk, stepsize=0;
12402: int count=0;
12403:
12404: int *tab;
12405: int mobilavproj=0 , prevfcast=0 ; /* moving average of prev, If prevfcast=1 prevalence projection */
12406: /* double anprojd, mprojd, jprojd; /\* For eventual projections *\/ */
12407: /* double anprojf, mprojf, jprojf; */
12408: /* double jintmean,mintmean,aintmean; */
12409: int prvforecast = 0; /* Might be 1 (date of beginning of projection is a choice or 2 is the dateintmean */
12410: int prvbackcast = 0; /* Might be 1 (date of beginning of projection is a choice or 2 is the dateintmean */
12411: double yrfproj= 10.0; /* Number of years of forward projections */
12412: double yrbproj= 10.0; /* Number of years of backward projections */
12413: int prevbcast=0; /* defined as global for mlikeli and mle, replacing backcast */
12414: int mobilav=0,popforecast=0;
12415: int hstepm=0, nhstepm=0;
12416: int agemortsup;
12417: float sumlpop=0.;
12418: double jprev1=1, mprev1=1,anprev1=2000,jprev2=1, mprev2=1,anprev2=2000;
12419: double jpyram=1, mpyram=1,anpyram=2000,jpyram1=1, mpyram1=1,anpyram1=2000;
12420:
12421: double bage=0, fage=110., age, agelim=0., agebase=0.;
12422: double ftolpl=FTOL;
12423: double **prlim;
12424: double **bprlim;
12425: double ***param; /* Matrix of parameters, param[i][j][k] param=ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel)
12426: state of origin, state of destination including death, for each covariate: constante, age, and V1 V2 etc. */
12427: double ***paramstart; /* Matrix of starting parameter values */
12428: double *p, *pstart; /* p=param[1][1] pstart is for starting values guessed by freqsummary */
12429: double **matcov; /* Matrix of covariance */
12430: double **hess; /* Hessian matrix */
12431: double ***delti3; /* Scale */
12432: double *delti; /* Scale */
12433: double ***eij, ***vareij;
12434: double **varpl; /* Variances of prevalence limits by age */
12435:
12436: double *epj, vepp;
12437:
12438: double dateprev1, dateprev2;
12439: double jproj1=1,mproj1=1,anproj1=2000,jproj2=1,mproj2=1,anproj2=2000, dateproj1=0, dateproj2=0, dateprojd=0, dateprojf=0;
12440: double jback1=1,mback1=1,anback1=2000,jback2=1,mback2=1,anback2=2000, dateback1=0, dateback2=0, datebackd=0, datebackf=0;
12441:
12442:
12443: double **ximort;
12444: char *alph[]={"a","a","b","c","d","e"}, str[4]="1234";
12445: int *dcwave;
12446:
12447: char z[1]="c";
12448:
12449: /*char *strt;*/
12450: char strtend[80];
12451:
12452:
12453: /* setlocale (LC_ALL, ""); */
12454: /* bindtextdomain (PACKAGE, LOCALEDIR); */
12455: /* textdomain (PACKAGE); */
12456: /* setlocale (LC_CTYPE, ""); */
12457: /* setlocale (LC_MESSAGES, ""); */
12458:
12459: /* gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */
12460: rstart_time = time(NULL);
12461: /* (void) gettimeofday(&start_time,&tzp);*/
12462: start_time = *localtime(&rstart_time);
12463: curr_time=start_time;
12464: /*tml = *localtime(&start_time.tm_sec);*/
12465: /* strcpy(strstart,asctime(&tml)); */
12466: strcpy(strstart,asctime(&start_time));
12467:
12468: /* printf("Localtime (at start)=%s",strstart); */
12469: /* tp.tm_sec = tp.tm_sec +86400; */
12470: /* tm = *localtime(&start_time.tm_sec); */
12471: /* tmg.tm_year=tmg.tm_year +dsign*dyear; */
12472: /* tmg.tm_mon=tmg.tm_mon +dsign*dmonth; */
12473: /* tmg.tm_hour=tmg.tm_hour + 1; */
12474: /* tp.tm_sec = mktime(&tmg); */
12475: /* strt=asctime(&tmg); */
12476: /* printf("Time(after) =%s",strstart); */
12477: /* (void) time (&time_value);
12478: * printf("time=%d,t-=%d\n",time_value,time_value-86400);
12479: * tm = *localtime(&time_value);
12480: * strstart=asctime(&tm);
12481: * printf("tim_value=%d,asctime=%s\n",time_value,strstart);
12482: */
12483:
12484: nberr=0; /* Number of errors and warnings */
12485: nbwarn=0;
12486: #ifdef WIN32
12487: _getcwd(pathcd, size);
12488: #else
12489: getcwd(pathcd, size);
12490: #endif
12491: syscompilerinfo(0);
12492: printf("\nIMaCh version %s, %s\n%s",version, copyright, fullversion);
12493: if(argc <=1){
12494: printf("\nEnter the parameter file name: ");
12495: if(!fgets(pathr,FILENAMELENGTH,stdin)){
12496: printf("ERROR Empty parameter file name\n");
12497: goto end;
12498: }
12499: i=strlen(pathr);
12500: if(pathr[i-1]=='\n')
12501: pathr[i-1]='\0';
12502: i=strlen(pathr);
12503: if(i >= 1 && pathr[i-1]==' ') {/* This may happen when dragging on oS/X! */
12504: pathr[i-1]='\0';
12505: }
12506: i=strlen(pathr);
12507: if( i==0 ){
12508: printf("ERROR Empty parameter file name\n");
12509: goto end;
12510: }
12511: for (tok = pathr; tok != NULL; ){
12512: printf("Pathr |%s|\n",pathr);
12513: while ((val = strsep(&tok, "\"" )) != NULL && *val == '\0');
12514: printf("val= |%s| pathr=%s\n",val,pathr);
12515: strcpy (pathtot, val);
12516: if(pathr[0] == '\0') break; /* Dirty */
12517: }
12518: }
12519: else if (argc<=2){
12520: strcpy(pathtot,argv[1]);
12521: }
12522: else{
12523: strcpy(pathtot,argv[1]);
12524: strcpy(z,argv[2]);
12525: printf("\nargv[2]=%s z=%c\n",argv[2],z[0]);
12526: }
12527: /*if(getcwd(pathcd, MAXLINE)!= NULL)printf ("Error pathcd\n");*/
12528: /*cygwin_split_path(pathtot,path,optionfile);
12529: printf("pathtot=%s, path=%s, optionfile=%s\n",pathtot,path,optionfile);*/
12530: /* cutv(path,optionfile,pathtot,'\\');*/
12531:
12532: /* Split argv[0], imach program to get pathimach */
12533: printf("\nargv[0]=%s argv[1]=%s, \n",argv[0],argv[1]);
12534: split(argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
12535: printf("\nargv[0]=%s pathimach=%s, \noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
12536: /* strcpy(pathimach,argv[0]); */
12537: /* Split argv[1]=pathtot, parameter file name to get path, optionfile, extension and name */
12538: split(pathtot,path,optionfile,optionfilext,optionfilefiname);
12539: printf("\npathtot=%s,\npath=%s,\noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname);
12540: #ifdef WIN32
12541: _chdir(path); /* Can be a relative path */
12542: if(_getcwd(pathcd,MAXLINE) > 0) /* So pathcd is the full path */
12543: #else
12544: chdir(path); /* Can be a relative path */
12545: if (getcwd(pathcd, MAXLINE) > 0) /* So pathcd is the full path */
12546: #endif
12547: printf("Current directory %s!\n",pathcd);
12548: strcpy(command,"mkdir ");
12549: strcat(command,optionfilefiname);
12550: if((outcmd=system(command)) != 0){
12551: printf("Directory already exists (or can't create it) %s%s, err=%d\n",path,optionfilefiname,outcmd);
12552: /* fprintf(ficlog,"Problem creating directory %s%s\n",path,optionfilefiname); */
12553: /* fclose(ficlog); */
12554: /* exit(1); */
12555: }
12556: /* if((imk=mkdir(optionfilefiname))<0){ */
12557: /* perror("mkdir"); */
12558: /* } */
12559:
12560: /*-------- arguments in the command line --------*/
12561:
12562: /* Main Log file */
12563: strcat(filelog, optionfilefiname);
12564: strcat(filelog,".log"); /* */
12565: if((ficlog=fopen(filelog,"w"))==NULL) {
12566: printf("Problem with logfile %s\n",filelog);
12567: goto end;
12568: }
12569: fprintf(ficlog,"Log filename:%s\n",filelog);
12570: fprintf(ficlog,"Version %s %s",version,fullversion);
12571: fprintf(ficlog,"\nEnter the parameter file name: \n");
12572: fprintf(ficlog,"pathimach=%s\npathtot=%s\n\
12573: path=%s \n\
12574: optionfile=%s\n\
12575: optionfilext=%s\n\
12576: optionfilefiname='%s'\n",pathimach,pathtot,path,optionfile,optionfilext,optionfilefiname);
12577:
12578: syscompilerinfo(1);
12579:
12580: printf("Local time (at start):%s",strstart);
12581: fprintf(ficlog,"Local time (at start): %s",strstart);
12582: fflush(ficlog);
12583: /* (void) gettimeofday(&curr_time,&tzp); */
12584: /* printf("Elapsed time %d\n", asc_diff_time(curr_time.tm_sec-start_time.tm_sec,tmpout)); */
12585:
12586: /* */
12587: strcpy(fileres,"r");
12588: strcat(fileres, optionfilefiname);
12589: strcat(fileresu, optionfilefiname); /* Without r in front */
12590: strcat(fileres,".txt"); /* Other files have txt extension */
12591: strcat(fileresu,".txt"); /* Other files have txt extension */
12592:
12593: /* Main ---------arguments file --------*/
12594:
12595: if((ficpar=fopen(optionfile,"r"))==NULL) {
12596: printf("Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
12597: fprintf(ficlog,"Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
12598: fflush(ficlog);
12599: /* goto end; */
12600: exit(70);
12601: }
12602:
12603: strcpy(filereso,"o");
12604: strcat(filereso,fileresu);
12605: if((ficparo=fopen(filereso,"w"))==NULL) { /* opened on subdirectory */
12606: printf("Problem with Output resultfile: %s\n", filereso);
12607: fprintf(ficlog,"Problem with Output resultfile: %s\n", filereso);
12608: fflush(ficlog);
12609: goto end;
12610: }
12611: /*-------- Rewriting parameter file ----------*/
12612: strcpy(rfileres,"r"); /* "Rparameterfile */
12613: strcat(rfileres,optionfilefiname); /* Parameter file first name */
12614: strcat(rfileres,"."); /* */
12615: strcat(rfileres,optionfilext); /* Other files have txt extension */
12616: if((ficres =fopen(rfileres,"w"))==NULL) {
12617: printf("Problem writing new parameter file: %s\n", rfileres);goto end;
12618: fprintf(ficlog,"Problem writing new parameter file: %s\n", rfileres);goto end;
12619: fflush(ficlog);
12620: goto end;
12621: }
12622: fprintf(ficres,"#IMaCh %s\n",version);
12623:
12624:
12625: /* Reads comments: lines beginning with '#' */
12626: numlinepar=0;
12627: /* Is it a BOM UTF-8 Windows file? */
12628: /* First parameter line */
12629: while(fgets(line, MAXLINE, ficpar)) {
12630: noffset=0;
12631: if( line[0] == (char)0xEF && line[1] == (char)0xBB) /* EF BB BF */
12632: {
12633: noffset=noffset+3;
12634: printf("# File is an UTF8 Bom.\n"); // 0xBF
12635: }
12636: /* else if( line[0] == (char)0xFE && line[1] == (char)0xFF)*/
12637: else if( line[0] == (char)0xFF && line[1] == (char)0xFE)
12638: {
12639: noffset=noffset+2;
12640: printf("# File is an UTF16BE BOM file\n");
12641: }
12642: else if( line[0] == 0 && line[1] == 0)
12643: {
12644: if( line[2] == (char)0xFE && line[3] == (char)0xFF){
12645: noffset=noffset+4;
12646: printf("# File is an UTF16BE BOM file\n");
12647: }
12648: } else{
12649: ;/*printf(" Not a BOM file\n");*/
12650: }
12651:
12652: /* If line starts with a # it is a comment */
12653: if (line[noffset] == '#') {
12654: numlinepar++;
12655: fputs(line,stdout);
12656: fputs(line,ficparo);
12657: fputs(line,ficres);
12658: fputs(line,ficlog);
12659: continue;
12660: }else
12661: break;
12662: }
12663: if((num_filled=sscanf(line,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", \
12664: title, datafile, &lastobs, &firstpass,&lastpass)) !=EOF){
12665: if (num_filled != 5) {
12666: printf("Should be 5 parameters\n");
12667: fprintf(ficlog,"Should be 5 parameters\n");
12668: }
12669: numlinepar++;
12670: printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
12671: fprintf(ficparo,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
12672: fprintf(ficres,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
12673: fprintf(ficlog,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
12674: }
12675: /* Second parameter line */
12676: while(fgets(line, MAXLINE, ficpar)) {
12677: /* while(fscanf(ficpar,"%[^\n]", line)) { */
12678: /* If line starts with a # it is a comment. Strangely fgets reads the EOL and fputs doesn't */
12679: if (line[0] == '#') {
12680: numlinepar++;
12681: printf("%s",line);
12682: fprintf(ficres,"%s",line);
12683: fprintf(ficparo,"%s",line);
12684: fprintf(ficlog,"%s",line);
12685: continue;
12686: }else
12687: break;
12688: }
12689: 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", \
12690: &ftol, &stepm, &ncovcol, &nqv, &ntv, &nqtv, &nlstate, &ndeath, &maxwav, &mle, &weightopt)) !=EOF){
12691: if (num_filled != 11) {
12692: 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");
12693: printf("but line=%s\n",line);
12694: 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");
12695: fprintf(ficlog,"but line=%s\n",line);
12696: }
12697: if( lastpass > maxwav){
12698: printf("Error (lastpass = %d) > (maxwav = %d)\n",lastpass, maxwav);
12699: fprintf(ficlog,"Error (lastpass = %d) > (maxwav = %d)\n",lastpass, maxwav);
12700: fflush(ficlog);
12701: goto end;
12702: }
12703: 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);
12704: 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);
12705: 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);
12706: 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);
12707: }
12708: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
12709: /*ftolpl=6.e-4; *//* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
12710: /* Third parameter line */
12711: while(fgets(line, MAXLINE, ficpar)) {
12712: /* If line starts with a # it is a comment */
12713: if (line[0] == '#') {
12714: numlinepar++;
12715: printf("%s",line);
12716: fprintf(ficres,"%s",line);
12717: fprintf(ficparo,"%s",line);
12718: fprintf(ficlog,"%s",line);
12719: continue;
12720: }else
12721: break;
12722: }
12723: if((num_filled=sscanf(line,"model=1+age%[^.\n]", model)) !=EOF){
12724: if (num_filled != 1){
12725: printf("ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line);
12726: fprintf(ficlog,"ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line);
12727: model[0]='\0';
12728: goto end;
12729: }
12730: else{
12731: if (model[0]=='+'){
12732: for(i=1; i<=strlen(model);i++)
12733: modeltemp[i-1]=model[i];
12734: strcpy(model,modeltemp);
12735: }
12736: }
12737: /* printf(" model=1+age%s modeltemp= %s, model=1+age+%s\n",model, modeltemp, model);fflush(stdout); */
12738: printf("model=1+age+%s\n",model);fflush(stdout);
12739: fprintf(ficparo,"model=1+age+%s\n",model);fflush(stdout);
12740: fprintf(ficres,"model=1+age+%s\n",model);fflush(stdout);
12741: fprintf(ficlog,"model=1+age+%s\n",model);fflush(stdout);
12742: }
12743: /* 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); */
12744: /* numlinepar=numlinepar+3; /\* In general *\/ */
12745: /* 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); */
12746: /* 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); */
12747: /* 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); */
12748: fflush(ficlog);
12749: /* if(model[0]=='#'|| model[0]== '\0'){ */
12750: if(model[0]=='#'){
12751: printf("Error in 'model' line: model should start with 'model=1+age+' and end without space \n \
12752: 'model=1+age+' or 'model=1+age+V1.' or 'model=1+age+age*age+V1+V1*age' or \n \
12753: 'model=1+age+V1+V2' or 'model=1+age+V1+V2+V1*V2' etc. \n"); \
12754: if(mle != -1){
12755: 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");
12756: exit(1);
12757: }
12758: }
12759: while((c=getc(ficpar))=='#' && c!= EOF){
12760: ungetc(c,ficpar);
12761: fgets(line, MAXLINE, ficpar);
12762: numlinepar++;
12763: if(line[1]=='q'){ /* This #q will quit imach (the answer is q) */
12764: z[0]=line[1];
12765: }else if(line[1]=='d'){ /* For debugging individual values of covariates in ficresilk */
12766: debugILK=1;printf("DebugILK\n");
12767: }
12768: /* printf("****line [1] = %c \n",line[1]); */
12769: fputs(line, stdout);
12770: //puts(line);
12771: fputs(line,ficparo);
12772: fputs(line,ficlog);
12773: }
12774: ungetc(c,ficpar);
12775:
12776:
12777: covar=matrix(0,NCOVMAX,firstobs,lastobs); /**< used in readdata */
12778: if(nqv>=1)coqvar=matrix(1,nqv,firstobs,lastobs); /**< Fixed quantitative covariate */
12779: if(nqtv>=1)cotqvar=ma3x(1,maxwav,1,nqtv,firstobs,lastobs); /**< Time varying quantitative covariate */
12780: /* if(ntv+nqtv>=1)cotvar=ma3x(1,maxwav,1,ntv+nqtv,firstobs,lastobs); /\**< Time varying covariate (dummy and quantitative)*\/ */
12781: if(ntv+nqtv>=1)cotvar=ma3x(1,maxwav,ncovcol+nqv+1,ncovcol+nqv+ntv+nqtv,firstobs,lastobs); /**< Might be better */
12782: cptcovn=0; /*Number of covariates, i.e. number of '+' in model statement plus one, indepently of n in Vn*/
12783: /* v1+v2+v3+v2*v4+v5*age makes cptcovn = 5
12784: v1+v2*age+v2*v3 makes cptcovn = 3
12785: */
12786: if (strlen(model)>1)
12787: 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*/
12788: else
12789: ncovmodel=2; /* Constant and age */
12790: nforce= (nlstate+ndeath-1)*nlstate; /* Number of forces ij from state i to j */
12791: npar= nforce*ncovmodel; /* Number of parameters like aij*/
12792: if(npar >MAXPARM || nlstate >NLSTATEMAX || ndeath >NDEATHMAX || ncovmodel>NCOVMAX){
12793: 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);
12794: 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);
12795: fflush(stdout);
12796: fclose (ficlog);
12797: goto end;
12798: }
12799: delti3= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
12800: delti=delti3[1][1];
12801: /*delti=vector(1,npar); *//* Scale of each paramater (output from hesscov)*/
12802: if(mle==-1){ /* Print a wizard for help writing covariance matrix */
12803: /* We could also provide initial parameters values giving by simple logistic regression
12804: * only one way, that is without matrix product. We will have nlstate maximizations */
12805: /* for(i=1;i<nlstate;i++){ */
12806: /* /\*reducing xi for 1 to npar to 1 to ncovmodel; *\/ */
12807: /* mlikeli(ficres,p, ncovmodel, ncovmodel, nlstate, ftol, funcnoprod); */
12808: /* } */
12809: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
12810: printf(" You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
12811: fprintf(ficlog," You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
12812: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
12813: fclose (ficparo);
12814: fclose (ficlog);
12815: goto end;
12816: exit(0);
12817: } else if(mle==-5) { /* Main Wizard */
12818: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
12819: printf(" You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
12820: fprintf(ficlog," You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
12821: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
12822: matcov=matrix(1,npar,1,npar);
12823: hess=matrix(1,npar,1,npar);
12824: } else{ /* Begin of mle != -1 or -5 */
12825: /* Read guessed parameters */
12826: /* Reads comments: lines beginning with '#' */
12827: while((c=getc(ficpar))=='#' && c!= EOF){
12828: ungetc(c,ficpar);
12829: fgets(line, MAXLINE, ficpar);
12830: numlinepar++;
12831: fputs(line,stdout);
12832: fputs(line,ficparo);
12833: fputs(line,ficlog);
12834: }
12835: ungetc(c,ficpar);
12836:
12837: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
12838: paramstart= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
12839: for(i=1; i <=nlstate; i++){
12840: j=0;
12841: for(jj=1; jj <=nlstate+ndeath; jj++){
12842: if(jj==i) continue;
12843: j++;
12844: while((c=getc(ficpar))=='#' && c!= EOF){
12845: ungetc(c,ficpar);
12846: fgets(line, MAXLINE, ficpar);
12847: numlinepar++;
12848: fputs(line,stdout);
12849: fputs(line,ficparo);
12850: fputs(line,ficlog);
12851: }
12852: ungetc(c,ficpar);
12853: fscanf(ficpar,"%1d%1d",&i1,&j1);
12854: if ((i1 != i) || (j1 != jj)){
12855: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n \
12856: It might be a problem of design; if ncovcol and the model are correct\n \
12857: run imach with mle=-1 to get a correct template of the parameter file.\n",numlinepar, i,j, i1, j1);
12858: exit(1);
12859: }
12860: fprintf(ficparo,"%1d%1d",i1,j1);
12861: if(mle==1)
12862: printf("%1d%1d",i,jj);
12863: fprintf(ficlog,"%1d%1d",i,jj);
12864: for(k=1; k<=ncovmodel;k++){
12865: fscanf(ficpar," %lf",¶m[i][j][k]);
12866: if(mle==1){
12867: printf(" %lf",param[i][j][k]);
12868: fprintf(ficlog," %lf",param[i][j][k]);
12869: }
12870: else
12871: fprintf(ficlog," %lf",param[i][j][k]);
12872: fprintf(ficparo," %lf",param[i][j][k]);
12873: }
12874: fscanf(ficpar,"\n");
12875: numlinepar++;
12876: if(mle==1)
12877: printf("\n");
12878: fprintf(ficlog,"\n");
12879: fprintf(ficparo,"\n");
12880: }
12881: }
12882: fflush(ficlog);
12883:
12884: /* Reads parameters values */
12885: p=param[1][1];
12886: pstart=paramstart[1][1];
12887:
12888: /* Reads comments: lines beginning with '#' */
12889: while((c=getc(ficpar))=='#' && c!= EOF){
12890: ungetc(c,ficpar);
12891: fgets(line, MAXLINE, ficpar);
12892: numlinepar++;
12893: fputs(line,stdout);
12894: fputs(line,ficparo);
12895: fputs(line,ficlog);
12896: }
12897: ungetc(c,ficpar);
12898:
12899: for(i=1; i <=nlstate; i++){
12900: for(j=1; j <=nlstate+ndeath-1; j++){
12901: fscanf(ficpar,"%1d%1d",&i1,&j1);
12902: if ( (i1-i) * (j1-j) != 0){
12903: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n",numlinepar, i,j, i1, j1);
12904: exit(1);
12905: }
12906: printf("%1d%1d",i,j);
12907: fprintf(ficparo,"%1d%1d",i1,j1);
12908: fprintf(ficlog,"%1d%1d",i1,j1);
12909: for(k=1; k<=ncovmodel;k++){
12910: fscanf(ficpar,"%le",&delti3[i][j][k]);
12911: printf(" %le",delti3[i][j][k]);
12912: fprintf(ficparo," %le",delti3[i][j][k]);
12913: fprintf(ficlog," %le",delti3[i][j][k]);
12914: }
12915: fscanf(ficpar,"\n");
12916: numlinepar++;
12917: printf("\n");
12918: fprintf(ficparo,"\n");
12919: fprintf(ficlog,"\n");
12920: }
12921: }
12922: fflush(ficlog);
12923:
12924: /* Reads covariance matrix */
12925: delti=delti3[1][1];
12926:
12927:
12928: /* free_ma3x(delti3,1,nlstate,1,nlstate+ndeath-1,1,ncovmodel); */ /* Hasn't to to freed here otherwise delti is no more allocated */
12929:
12930: /* Reads comments: lines beginning with '#' */
12931: while((c=getc(ficpar))=='#' && c!= EOF){
12932: ungetc(c,ficpar);
12933: fgets(line, MAXLINE, ficpar);
12934: numlinepar++;
12935: fputs(line,stdout);
12936: fputs(line,ficparo);
12937: fputs(line,ficlog);
12938: }
12939: ungetc(c,ficpar);
12940:
12941: matcov=matrix(1,npar,1,npar);
12942: hess=matrix(1,npar,1,npar);
12943: for(i=1; i <=npar; i++)
12944: for(j=1; j <=npar; j++) matcov[i][j]=0.;
12945:
12946: /* Scans npar lines */
12947: for(i=1; i <=npar; i++){
12948: count=fscanf(ficpar,"%1d%1d%d",&i1,&j1,&jk);
12949: if(count != 3){
12950: printf("Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
12951: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
12952: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
12953: fprintf(ficlog,"Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
12954: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
12955: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
12956: exit(1);
12957: }else{
12958: if(mle==1)
12959: printf("%1d%1d%d",i1,j1,jk);
12960: }
12961: fprintf(ficlog,"%1d%1d%d",i1,j1,jk);
12962: fprintf(ficparo,"%1d%1d%d",i1,j1,jk);
12963: for(j=1; j <=i; j++){
12964: fscanf(ficpar," %le",&matcov[i][j]);
12965: if(mle==1){
12966: printf(" %.5le",matcov[i][j]);
12967: }
12968: fprintf(ficlog," %.5le",matcov[i][j]);
12969: fprintf(ficparo," %.5le",matcov[i][j]);
12970: }
12971: fscanf(ficpar,"\n");
12972: numlinepar++;
12973: if(mle==1)
12974: printf("\n");
12975: fprintf(ficlog,"\n");
12976: fprintf(ficparo,"\n");
12977: }
12978: /* End of read covariance matrix npar lines */
12979: for(i=1; i <=npar; i++)
12980: for(j=i+1;j<=npar;j++)
12981: matcov[i][j]=matcov[j][i];
12982:
12983: if(mle==1)
12984: printf("\n");
12985: fprintf(ficlog,"\n");
12986:
12987: fflush(ficlog);
12988:
12989: } /* End of mle != -3 */
12990:
12991: /* Main data
12992: */
12993: nobs=lastobs-firstobs+1; /* was = lastobs;*/
12994: /* num=lvector(1,n); */
12995: /* moisnais=vector(1,n); */
12996: /* annais=vector(1,n); */
12997: /* moisdc=vector(1,n); */
12998: /* andc=vector(1,n); */
12999: /* weight=vector(1,n); */
13000: /* agedc=vector(1,n); */
13001: /* cod=ivector(1,n); */
13002: /* for(i=1;i<=n;i++){ */
13003: num=lvector(firstobs,lastobs);
13004: moisnais=vector(firstobs,lastobs);
13005: annais=vector(firstobs,lastobs);
13006: moisdc=vector(firstobs,lastobs);
13007: andc=vector(firstobs,lastobs);
13008: weight=vector(firstobs,lastobs);
13009: agedc=vector(firstobs,lastobs);
13010: cod=ivector(firstobs,lastobs);
13011: for(i=firstobs;i<=lastobs;i++){
13012: num[i]=0;
13013: moisnais[i]=0;
13014: annais[i]=0;
13015: moisdc[i]=0;
13016: andc[i]=0;
13017: agedc[i]=0;
13018: cod[i]=0;
13019: weight[i]=1.0; /* Equal weights, 1 by default */
13020: }
13021: mint=matrix(1,maxwav,firstobs,lastobs);
13022: anint=matrix(1,maxwav,firstobs,lastobs);
13023: s=imatrix(1,maxwav+1,firstobs,lastobs); /* s[i][j] health state for wave i and individual j */
13024: /* printf("BUG ncovmodel=%d NCOVMAX=%d 2**ncovmodel=%f BUG\n",ncovmodel,NCOVMAX,pow(2,ncovmodel)); */
13025: tab=ivector(1,NCOVMAX);
13026: ncodemax=ivector(1,NCOVMAX); /* Number of code per covariate; if O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */
13027: ncodemaxwundef=ivector(1,NCOVMAX); /* Number of code per covariate; if - 1 O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */
13028:
13029: /* Reads data from file datafile */
13030: if (readdata(datafile, firstobs, lastobs, &imx)==1)
13031: goto end;
13032:
13033: /* Calculation of the number of parameters from char model */
13034: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4
13035: k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tag[cptcovage=1]=4
13036: k=3 V4 Tvar[k=3]= 4 (from V4)
13037: k=2 V1 Tvar[k=2]= 1 (from V1)
13038: k=1 Tvar[1]=2 (from V2)
13039: */
13040:
13041: Tvar=ivector(1,NCOVMAX); /* Was 15 changed to NCOVMAX. */
13042: TvarsDind=ivector(1,NCOVMAX); /* */
13043: TnsdVar=ivector(1,NCOVMAX); /* */
13044: /* for(i=1; i<=NCOVMAX;i++) TnsdVar[i]=3; */
13045: TvarsD=ivector(1,NCOVMAX); /* */
13046: TvarsQind=ivector(1,NCOVMAX); /* */
13047: TvarsQ=ivector(1,NCOVMAX); /* */
13048: TvarF=ivector(1,NCOVMAX); /* */
13049: TvarFind=ivector(1,NCOVMAX); /* */
13050: TvarV=ivector(1,NCOVMAX); /* */
13051: TvarVind=ivector(1,NCOVMAX); /* */
13052: TvarA=ivector(1,NCOVMAX); /* */
13053: TvarAind=ivector(1,NCOVMAX); /* */
13054: TvarFD=ivector(1,NCOVMAX); /* */
13055: TvarFDind=ivector(1,NCOVMAX); /* */
13056: TvarFQ=ivector(1,NCOVMAX); /* */
13057: TvarFQind=ivector(1,NCOVMAX); /* */
13058: TvarVD=ivector(1,NCOVMAX); /* */
13059: TvarVDind=ivector(1,NCOVMAX); /* */
13060: TvarVQ=ivector(1,NCOVMAX); /* */
13061: TvarVQind=ivector(1,NCOVMAX); /* */
13062: TvarVV=ivector(1,NCOVMAX); /* */
13063: TvarVVind=ivector(1,NCOVMAX); /* */
13064:
13065: Tvalsel=vector(1,NCOVMAX); /* */
13066: Tvarsel=ivector(1,NCOVMAX); /* */
13067: Typevar=ivector(-1,NCOVMAX); /* -1 to 2 */
13068: Fixed=ivector(-1,NCOVMAX); /* -1 to 3 */
13069: Dummy=ivector(-1,NCOVMAX); /* -1 to 3 */
13070: /* V2+V1+V4+age*V3 is a model with 4 covariates (3 plus signs).
13071: For each model-covariate stores the data-covariate id. Tvar[1]=2, Tvar[2]=1, Tvar[3]=4,
13072: Tvar[4=age*V3] is 3 and 'age' is recorded in Tage.
13073: */
13074: /* For model-covariate k tells which data-covariate to use but
13075: because this model-covariate is a construction we invent a new column
13076: ncovcol + k1
13077: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3
13078: Tvar[3=V1*V4]=4+1 etc */
13079: Tprod=ivector(1,NCOVMAX); /* Gives the k position of the k1 product */
13080: Tposprod=ivector(1,NCOVMAX); /* Gives the k1 product from the k position */
13081: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3
13082: if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2)
13083: Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5]=2
13084: */
13085: Tvaraff=ivector(1,NCOVMAX); /* Unclear */
13086: 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
13087: * For V3*V2 (in V2+V1+V1*V4+age*V3+V3*V2), V3*V2 position is 2nd.
13088: * Tvard[k1=2][1]=3 (V3) Tvard[k1=2][2]=2(V2) */
13089: Tvardk=imatrix(1,NCOVMAX,1,2);
13090: Tage=ivector(1,NCOVMAX); /* Gives the covariate id of covariates associated with age: V2 + V1 + age*V4 + V3*age
13091: 4 covariates (3 plus signs)
13092: Tage[1=V3*age]= 4; Tage[2=age*V4] = 3
13093: */
13094: for(i=1;i<NCOVMAX;i++)
13095: Tage[i]=0;
13096: Tmodelind=ivector(1,NCOVMAX);/** gives the k model position of an
13097: * individual dummy, fixed or varying:
13098: * Tmodelind[Tvaraff[3]]=9,Tvaraff[1]@9={4,
13099: * 3, 1, 0, 0, 0, 0, 0, 0},
13100: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 ,
13101: * V1 df, V2 qf, V3 & V4 dv, V5 qv
13102: * Tmodelind[1]@9={9,0,3,2,}*/
13103: TmodelInvind=ivector(1,NCOVMAX); /* TmodelInvind=Tvar[k]- ncovcol-nqv={5-2-1=2,*/
13104: TmodelInvQind=ivector(1,NCOVMAX);/** gives the k model position of an
13105: * individual quantitative, fixed or varying:
13106: * Tmodelqind[1]=1,Tvaraff[1]@9={4,
13107: * 3, 1, 0, 0, 0, 0, 0, 0},
13108: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1*/
13109: /* Main decodemodel */
13110:
13111:
13112: if(decodemodel(model, lastobs) == 1) /* In order to get Tvar[k] V4+V3+V5 p Tvar[1]@3 = {4, 3, 5}*/
13113: goto end;
13114:
13115: if((double)(lastobs-imx)/(double)imx > 1.10){
13116: nbwarn++;
13117: 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);
13118: 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);
13119: }
13120: /* if(mle==1){*/
13121: if (weightopt != 1) { /* Maximisation without weights. We can have weights different from 1 but want no weight*/
13122: for(i=1;i<=imx;i++) weight[i]=1.0; /* changed to imx */
13123: }
13124:
13125: /*-calculation of age at interview from date of interview and age at death -*/
13126: agev=matrix(1,maxwav,1,imx);
13127:
13128: if(calandcheckages(imx, maxwav, &agemin, &agemax, &nberr, &nbwarn) == 1)
13129: goto end;
13130:
13131:
13132: agegomp=(int)agemin;
13133: free_vector(moisnais,firstobs,lastobs);
13134: free_vector(annais,firstobs,lastobs);
13135: /* free_matrix(mint,1,maxwav,1,n);
13136: free_matrix(anint,1,maxwav,1,n);*/
13137: /* free_vector(moisdc,1,n); */
13138: /* free_vector(andc,1,n); */
13139: /* */
13140:
13141: wav=ivector(1,imx);
13142: /* dh=imatrix(1,lastpass-firstpass+1,1,imx); */
13143: /* bh=imatrix(1,lastpass-firstpass+1,1,imx); */
13144: /* mw=imatrix(1,lastpass-firstpass+1,1,imx); */
13145: 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.*/
13146: bh=imatrix(1,lastpass-firstpass+2,1,imx);
13147: mw=imatrix(1,lastpass-firstpass+2,1,imx);
13148:
13149: /* Concatenates waves */
13150: /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
13151: Death is a valid wave (if date is known).
13152: mw[mi][i] is the number of (mi=1 to wav[i]) effective wave out of mi of individual i
13153: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
13154: and mw[mi+1][i]. dh depends on stepm.
13155: */
13156:
13157: concatwav(wav, dh, bh, mw, s, agedc, agev, firstpass, lastpass, imx, nlstate, stepm);
13158: /* Concatenates waves */
13159:
13160: free_vector(moisdc,firstobs,lastobs);
13161: free_vector(andc,firstobs,lastobs);
13162:
13163: /* Routine tricode is to calculate cptcoveff (real number of unique covariates) and to associate covariable number and modality */
13164: nbcode=imatrix(0,NCOVMAX,0,NCOVMAX);
13165: ncodemax[1]=1;
13166: Ndum =ivector(-1,NCOVMAX);
13167: cptcoveff=0;
13168: if (ncovmodel-nagesqr > 2 ){ /* That is if covariate other than cst, age and age*age */
13169: tricode(&cptcoveff,Tvar,nbcode,imx, Ndum); /**< Fills nbcode[Tvar[j]][l]; as well as calculate cptcoveff or number of total effective dummy covariates*/
13170: }
13171:
13172: ncovcombmax=pow(2,cptcoveff);
13173: invalidvarcomb=ivector(0, ncovcombmax);
13174: for(i=0;i<ncovcombmax;i++)
13175: invalidvarcomb[i]=0;
13176:
13177: /* Nbcode gives the value of the lth modality (currently 1 to 2) of jth covariate, in
13178: V2+V1*age, there are 3 covariates Tvar[2]=1 (V1).*/
13179: /* 1 to ncodemax[j] which is the maximum value of this jth covariate */
13180:
13181: /* codtab=imatrix(1,100,1,10);*/ /* codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) */
13182: /*printf(" codtab[1,1],codtab[100,10]=%d,%d\n", codtab[1][1],codtabm(100,10));*/
13183: /* codtab gives the value 1 or 2 of the hth combination of k covariates (1 or 2).*/
13184: /* nbcode[Tvaraff[j]][codtabm(h,j)]) : if there are only 2 modalities for a covariate j,
13185: * codtabm(h,j) gives its value classified at position h and nbcode gives how it is coded
13186: * (currently 0 or 1) in the data.
13187: * In a loop on h=1 to 2**k, and a loop on j (=1 to k), we get the value of
13188: * corresponding modality (h,j).
13189: */
13190:
13191: h=0;
13192: /*if (cptcovn > 0) */
13193: m=pow(2,cptcoveff);
13194:
13195: /**< codtab(h,k) k = codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) + 1
13196: * For k=4 covariates, h goes from 1 to m=2**k
13197: * codtabm(h,k)= (1 & (h-1) >> (k-1)) + 1;
13198: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
13199: * h\k 1 2 3 4 * h-1\k-1 4 3 2 1
13200: *______________________________ *______________________
13201: * 1 i=1 1 i=1 1 i=1 1 i=1 1 * 0 0 0 0 0
13202: * 2 2 1 1 1 * 1 0 0 0 1
13203: * 3 i=2 1 2 1 1 * 2 0 0 1 0
13204: * 4 2 2 1 1 * 3 0 0 1 1
13205: * 5 i=3 1 i=2 1 2 1 * 4 0 1 0 0
13206: * 6 2 1 2 1 * 5 0 1 0 1
13207: * 7 i=4 1 2 2 1 * 6 0 1 1 0
13208: * 8 2 2 2 1 * 7 0 1 1 1
13209: * 9 i=5 1 i=3 1 i=2 1 2 * 8 1 0 0 0
13210: * 10 2 1 1 2 * 9 1 0 0 1
13211: * 11 i=6 1 2 1 2 * 10 1 0 1 0
13212: * 12 2 2 1 2 * 11 1 0 1 1
13213: * 13 i=7 1 i=4 1 2 2 * 12 1 1 0 0
13214: * 14 2 1 2 2 * 13 1 1 0 1
13215: * 15 i=8 1 2 2 2 * 14 1 1 1 0
13216: * 16 2 2 2 2 * 15 1 1 1 1
13217: */
13218: /* How to do the opposite? From combination h (=1 to 2**k) how to get the value on the covariates? */
13219: /* from h=5 and m, we get then number of covariates k=log(m)/log(2)=4
13220: * and the value of each covariate?
13221: * V1=1, V2=1, V3=2, V4=1 ?
13222: * h-1=4 and 4 is 0100 or reverse 0010, and +1 is 1121 ok.
13223: * h=6, 6-1=5, 5 is 0101, 1010, 2121, V1=2nd, V2=1st, V3=2nd, V4=1st.
13224: * In order to get the real value in the data, we use nbcode
13225: * nbcode[Tvar[3][2nd]]=1 and nbcode[Tvar[4][1]]=0
13226: * We are keeping this crazy system in order to be able (in the future?)
13227: * to have more than 2 values (0 or 1) for a covariate.
13228: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
13229: * h=6, k=2? h-1=5=0101, reverse 1010, +1=2121, k=2nd position: value is 1: codtabm(6,2)=1
13230: * bbbbbbbb
13231: * 76543210
13232: * h-1 00000101 (6-1=5)
13233: *(h-1)>>(k-1)= 00000010 >> (2-1) = 1 right shift
13234: * &
13235: * 1 00000001 (1)
13236: * 00000000 = 1 & ((h-1) >> (k-1))
13237: * +1= 00000001 =1
13238: *
13239: * h=14, k=3 => h'=h-1=13, k'=k-1=2
13240: * h' 1101 =2^3+2^2+0x2^1+2^0
13241: * >>k' 11
13242: * & 00000001
13243: * = 00000001
13244: * +1 = 00000010=2 = codtabm(14,3)
13245: * Reverse h=6 and m=16?
13246: * cptcoveff=log(16)/log(2)=4 covariate: 6-1=5=0101 reversed=1010 +1=2121 =>V1=2, V2=1, V3=2, V4=1.
13247: * for (j=1 to cptcoveff) Vj=decodtabm(j,h,cptcoveff)
13248: * decodtabm(h,j,cptcoveff)= (((h-1) >> (j-1)) & 1) +1
13249: * decodtabm(h,j,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (j-1)) & 1) +1 : -1)
13250: * V3=decodtabm(14,3,2**4)=2
13251: * h'=13 1101 =2^3+2^2+0x2^1+2^0
13252: *(h-1) >> (j-1) 0011 =13 >> 2
13253: * &1 000000001
13254: * = 000000001
13255: * +1= 000000010 =2
13256: * 2211
13257: * V1=1+1, V2=0+1, V3=1+1, V4=1+1
13258: * V3=2
13259: * codtabm and decodtabm are identical
13260: */
13261:
13262:
13263: free_ivector(Ndum,-1,NCOVMAX);
13264:
13265:
13266:
13267: /* Initialisation of ----------- gnuplot -------------*/
13268: strcpy(optionfilegnuplot,optionfilefiname);
13269: if(mle==-3)
13270: strcat(optionfilegnuplot,"-MORT_");
13271: strcat(optionfilegnuplot,".gp");
13272:
13273: if((ficgp=fopen(optionfilegnuplot,"w"))==NULL) {
13274: printf("Problem with file %s",optionfilegnuplot);
13275: }
13276: else{
13277: fprintf(ficgp,"\n# IMaCh-%s\n", version);
13278: fprintf(ficgp,"# %s\n", optionfilegnuplot);
13279: //fprintf(ficgp,"set missing 'NaNq'\n");
13280: fprintf(ficgp,"set datafile missing 'NaNq'\n");
13281: }
13282: /* fclose(ficgp);*/
13283:
13284:
13285: /* Initialisation of --------- index.htm --------*/
13286:
13287: strcpy(optionfilehtm,optionfilefiname); /* Main html file */
13288: if(mle==-3)
13289: strcat(optionfilehtm,"-MORT_");
13290: strcat(optionfilehtm,".htm");
13291: if((fichtm=fopen(optionfilehtm,"w"))==NULL) {
13292: printf("Problem with %s \n",optionfilehtm);
13293: exit(0);
13294: }
13295:
13296: strcpy(optionfilehtmcov,optionfilefiname); /* Only for matrix of covariance */
13297: strcat(optionfilehtmcov,"-cov.htm");
13298: if((fichtmcov=fopen(optionfilehtmcov,"w"))==NULL) {
13299: printf("Problem with %s \n",optionfilehtmcov), exit(0);
13300: }
13301: else{
13302: fprintf(fichtmcov,"<html><head>\n<title>IMaCh Cov %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
13303: <hr size=\"2\" color=\"#EC5E5E\"> \n\
13304: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
13305: optionfilehtmcov,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
13306: }
13307:
13308: fprintf(fichtm,"<html><head>\n<meta charset=\"utf-8\"/><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n\
13309: <title>IMaCh %s</title></head>\n\
13310: <body><font size=\"7\"><a href=http:/euroreves.ined.fr/imach>IMaCh for Interpolated Markov Chain</a> </font><br>\n\
13311: <font size=\"3\">Sponsored by Copyright (C) 2002-2015 <a href=http://www.ined.fr>INED</a>\
13312: -EUROREVES-Institut de longévité-2013-2022-Japan Society for the Promotion of Sciences 日本学術振興会 \
13313: (<a href=https://www.jsps.go.jp/english/e-grants/>Grant-in-Aid for Scientific Research 25293121</a>) - \
13314: <a href=https://software.intel.com/en-us>Intel Software 2015-2018</a></font><br> \n", optionfilehtm);
13315:
13316: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\"> \n\
13317: <font size=\"2\">IMaCh-%s <br> %s</font> \
13318: <hr size=\"2\" color=\"#EC5E5E\"> \n\
13319: 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\
13320: \n\
13321: <hr size=\"2\" color=\"#EC5E5E\">\
13322: <ul><li><h4>Parameter files</h4>\n\
13323: - Parameter file: <a href=\"%s.%s\">%s.%s</a><br>\n\
13324: - Copy of the parameter file: <a href=\"o%s\">o%s</a><br>\n\
13325: - Log file of the run: <a href=\"%s\">%s</a><br>\n\
13326: - Gnuplot file name: <a href=\"%s\">%s</a><br>\n\
13327: - Date and time at start: %s</ul>\n",\
13328: version,fullversion,optionfilehtm,optionfilehtm,title,datafile,datafile,firstpass,lastpass,stepm, weightopt, model, \
13329: optionfilefiname,optionfilext,optionfilefiname,optionfilext,\
13330: fileres,fileres,\
13331: filelog,filelog,optionfilegnuplot,optionfilegnuplot,strstart);
13332: fflush(fichtm);
13333:
13334: strcpy(pathr,path);
13335: strcat(pathr,optionfilefiname);
13336: #ifdef WIN32
13337: _chdir(optionfilefiname); /* Move to directory named optionfile */
13338: #else
13339: chdir(optionfilefiname); /* Move to directory named optionfile */
13340: #endif
13341:
13342:
13343: /* Calculates basic frequencies. Computes observed prevalence at single age
13344: and for any valid combination of covariates
13345: and prints on file fileres'p'. */
13346: freqsummary(fileres, p, pstart, agemin, agemax, s, agev, nlstate, imx, Tvaraff, invalidvarcomb, nbcode, ncodemax,mint,anint,strstart, \
13347: firstpass, lastpass, stepm, weightopt, model);
13348:
13349: fprintf(fichtm,"\n");
13350: 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",\
13351: ftol, stepm);
13352: fprintf(fichtm,"\n<li>Number of fixed dummy covariates: ncovcol=%d ", ncovcol);
13353: ncurrv=1;
13354: for(i=ncurrv; i <=ncovcol; i++) fprintf(fichtm,"V%d ", i);
13355: fprintf(fichtm,"\n<li> Number of fixed quantitative variables: nqv=%d ", nqv);
13356: ncurrv=i;
13357: for(i=ncurrv; i <=ncurrv-1+nqv; i++) fprintf(fichtm,"V%d ", i);
13358: fprintf(fichtm,"\n<li> Number of time varying (wave varying) dummy covariates: ntv=%d ", ntv);
13359: ncurrv=i;
13360: for(i=ncurrv; i <=ncurrv-1+ntv; i++) fprintf(fichtm,"V%d ", i);
13361: fprintf(fichtm,"\n<li>Number of time varying quantitative covariates: nqtv=%d ", nqtv);
13362: ncurrv=i;
13363: for(i=ncurrv; i <=ncurrv-1+nqtv; i++) fprintf(fichtm,"V%d ", i);
13364: 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", \
13365: nlstate, ndeath, maxwav, mle, weightopt);
13366:
13367: fprintf(fichtm,"<h4> Diagram of states <a href=\"%s_.svg\">%s_.svg</a></h4> \n\
13368: <img src=\"%s_.svg\">", subdirf2(optionfilefiname,"D_"),subdirf2(optionfilefiname,"D_"),subdirf2(optionfilefiname,"D_"));
13369:
13370:
13371: fprintf(fichtm,"\n<h4>Some descriptive statistics </h4>\n<br>Number of (used) observations=%d <br>\n\
13372: Youngest age at first (selected) pass %.2f, oldest age %.2f<br>\n\
13373: Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n",\
13374: imx,agemin,agemax,jmin,jmax,jmean);
13375: pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
13376: oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
13377: newms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
13378: savms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
13379: oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */
13380:
13381: /* For Powell, parameters are in a vector p[] starting at p[1]
13382: so we point p on param[1][1] so that p[1] maps on param[1][1][1] */
13383: p=param[1][1]; /* *(*(*(param +1)+1)+0) */
13384:
13385: globpr=0; /* To get the number ipmx of contributions and the sum of weights*/
13386: /* For mortality only */
13387: if (mle==-3){
13388: ximort=matrix(1,NDIM,1,NDIM);
13389: for(i=1;i<=NDIM;i++)
13390: for(j=1;j<=NDIM;j++)
13391: ximort[i][j]=0.;
13392: /* ximort=gsl_matrix_alloc(1,NDIM,1,NDIM); */
13393: cens=ivector(firstobs,lastobs);
13394: ageexmed=vector(firstobs,lastobs);
13395: agecens=vector(firstobs,lastobs);
13396: dcwave=ivector(firstobs,lastobs);
13397:
13398: for (i=1; i<=imx; i++){
13399: dcwave[i]=-1;
13400: for (m=firstpass; m<=lastpass; m++)
13401: if (s[m][i]>nlstate) {
13402: dcwave[i]=m;
13403: /* printf("i=%d j=%d s=%d dcwave=%d\n",i,j, s[j][i],dcwave[i]);*/
13404: break;
13405: }
13406: }
13407:
13408: for (i=1; i<=imx; i++) {
13409: if (wav[i]>0){
13410: ageexmed[i]=agev[mw[1][i]][i];
13411: j=wav[i];
13412: agecens[i]=1.;
13413:
13414: if (ageexmed[i]> 1 && wav[i] > 0){
13415: agecens[i]=agev[mw[j][i]][i];
13416: cens[i]= 1;
13417: }else if (ageexmed[i]< 1)
13418: cens[i]= -1;
13419: if (agedc[i]< AGESUP && agedc[i]>1 && dcwave[i]>firstpass && dcwave[i]<=lastpass)
13420: cens[i]=0 ;
13421: }
13422: else cens[i]=-1;
13423: }
13424:
13425: for (i=1;i<=NDIM;i++) {
13426: for (j=1;j<=NDIM;j++)
13427: ximort[i][j]=(i == j ? 1.0 : 0.0);
13428: }
13429:
13430: p[1]=0.0268; p[NDIM]=0.083;
13431: /* printf("%lf %lf", p[1], p[2]); */
13432:
13433:
13434: #ifdef GSL
13435: printf("GSL optimization\n"); fprintf(ficlog,"Powell\n");
13436: #else
13437: printf("Powell\n"); fprintf(ficlog,"Powell\n");
13438: #endif
13439: strcpy(filerespow,"POW-MORT_");
13440: strcat(filerespow,fileresu);
13441: if((ficrespow=fopen(filerespow,"w"))==NULL) {
13442: printf("Problem with resultfile: %s\n", filerespow);
13443: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
13444: }
13445: #ifdef GSL
13446: fprintf(ficrespow,"# GSL optimization\n# iter -2*LL");
13447: #else
13448: fprintf(ficrespow,"# Powell\n# iter -2*LL");
13449: #endif
13450: /* for (i=1;i<=nlstate;i++)
13451: for(j=1;j<=nlstate+ndeath;j++)
13452: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
13453: */
13454: fprintf(ficrespow,"\n");
13455: #ifdef GSL
13456: /* gsl starts here */
13457: T = gsl_multimin_fminimizer_nmsimplex;
13458: gsl_multimin_fminimizer *sfm = NULL;
13459: gsl_vector *ss, *x;
13460: gsl_multimin_function minex_func;
13461:
13462: /* Initial vertex size vector */
13463: ss = gsl_vector_alloc (NDIM);
13464:
13465: if (ss == NULL){
13466: GSL_ERROR_VAL ("failed to allocate space for ss", GSL_ENOMEM, 0);
13467: }
13468: /* Set all step sizes to 1 */
13469: gsl_vector_set_all (ss, 0.001);
13470:
13471: /* Starting point */
13472:
13473: x = gsl_vector_alloc (NDIM);
13474:
13475: if (x == NULL){
13476: gsl_vector_free(ss);
13477: GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0);
13478: }
13479:
13480: /* Initialize method and iterate */
13481: /* p[1]=0.0268; p[NDIM]=0.083; */
13482: /* gsl_vector_set(x, 0, 0.0268); */
13483: /* gsl_vector_set(x, 1, 0.083); */
13484: gsl_vector_set(x, 0, p[1]);
13485: gsl_vector_set(x, 1, p[2]);
13486:
13487: minex_func.f = &gompertz_f;
13488: minex_func.n = NDIM;
13489: minex_func.params = (void *)&p; /* ??? */
13490:
13491: sfm = gsl_multimin_fminimizer_alloc (T, NDIM);
13492: gsl_multimin_fminimizer_set (sfm, &minex_func, x, ss);
13493:
13494: printf("Iterations beginning .....\n\n");
13495: printf("Iter. # Intercept Slope -Log Likelihood Simplex size\n");
13496:
13497: iteri=0;
13498: while (rval == GSL_CONTINUE){
13499: iteri++;
13500: status = gsl_multimin_fminimizer_iterate(sfm);
13501:
13502: if (status) printf("error: %s\n", gsl_strerror (status));
13503: fflush(0);
13504:
13505: if (status)
13506: break;
13507:
13508: rval = gsl_multimin_test_size (gsl_multimin_fminimizer_size (sfm), 1e-6);
13509: ssval = gsl_multimin_fminimizer_size (sfm);
13510:
13511: if (rval == GSL_SUCCESS)
13512: printf ("converged to a local maximum at\n");
13513:
13514: printf("%5d ", iteri);
13515: for (it = 0; it < NDIM; it++){
13516: printf ("%10.5f ", gsl_vector_get (sfm->x, it));
13517: }
13518: printf("f() = %-10.5f ssize = %.7f\n", sfm->fval, ssval);
13519: }
13520:
13521: printf("\n\n Please note: Program should be run many times with varying starting points to detemine global maximum\n\n");
13522:
13523: gsl_vector_free(x); /* initial values */
13524: gsl_vector_free(ss); /* inital step size */
13525: for (it=0; it<NDIM; it++){
13526: p[it+1]=gsl_vector_get(sfm->x,it);
13527: fprintf(ficrespow," %.12lf", p[it]);
13528: }
13529: gsl_multimin_fminimizer_free (sfm); /* p *(sfm.x.data) et p *(sfm.x.data+1) */
13530: #endif
13531: #ifdef POWELL
13532: powell(p,ximort,NDIM,ftol,&iter,&fret,gompertz);
13533: #endif
13534: fclose(ficrespow);
13535:
13536: hesscov(matcov, hess, p, NDIM, delti, 1e-4, gompertz);
13537:
13538: for(i=1; i <=NDIM; i++)
13539: for(j=i+1;j<=NDIM;j++)
13540: matcov[i][j]=matcov[j][i];
13541:
13542: printf("\nCovariance matrix\n ");
13543: fprintf(ficlog,"\nCovariance matrix\n ");
13544: for(i=1; i <=NDIM; i++) {
13545: for(j=1;j<=NDIM;j++){
13546: printf("%f ",matcov[i][j]);
13547: fprintf(ficlog,"%f ",matcov[i][j]);
13548: }
13549: printf("\n "); fprintf(ficlog,"\n ");
13550: }
13551:
13552: printf("iter=%d MLE=%f Eq=%lf*exp(%lf*(age-%d))\n",iter,-gompertz(p),p[1],p[2],agegomp);
13553: for (i=1;i<=NDIM;i++) {
13554: printf("%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
13555: fprintf(ficlog,"%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
13556: }
13557: lsurv=vector(agegomp,AGESUP);
13558: lpop=vector(agegomp,AGESUP);
13559: tpop=vector(agegomp,AGESUP);
13560: lsurv[agegomp]=100000;
13561:
13562: for (k=agegomp;k<=AGESUP;k++) {
13563: agemortsup=k;
13564: if (p[1]*exp(p[2]*(k-agegomp))>1) break;
13565: }
13566:
13567: for (k=agegomp;k<agemortsup;k++)
13568: lsurv[k+1]=lsurv[k]-lsurv[k]*(p[1]*exp(p[2]*(k-agegomp)));
13569:
13570: for (k=agegomp;k<agemortsup;k++){
13571: lpop[k]=(lsurv[k]+lsurv[k+1])/2.;
13572: sumlpop=sumlpop+lpop[k];
13573: }
13574:
13575: tpop[agegomp]=sumlpop;
13576: for (k=agegomp;k<(agemortsup-3);k++){
13577: /* tpop[k+1]=2;*/
13578: tpop[k+1]=tpop[k]-lpop[k];
13579: }
13580:
13581:
13582: printf("\nAge lx qx dx Lx Tx e(x)\n");
13583: for (k=agegomp;k<(agemortsup-2);k++)
13584: 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]);
13585:
13586:
13587: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
13588: ageminpar=50;
13589: agemaxpar=100;
13590: if(ageminpar == AGEOVERFLOW ||agemaxpar == AGEOVERFLOW){
13591: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
13592: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
13593: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
13594: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
13595: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
13596: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
13597: }else{
13598: printf("Warning! ageminpar %f and agemaxpar %f have been fixed because for simplification until it is fixed...\n\n",ageminpar,agemaxpar);
13599: fprintf(ficlog,"Warning! ageminpar %f and agemaxpar %f have been fixed because for simplification until it is fixed...\n\n",ageminpar,agemaxpar);
13600: printinggnuplotmort(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, pathc,p);
13601: }
13602: printinghtmlmort(fileresu,title,datafile, firstpass, lastpass, \
13603: stepm, weightopt,\
13604: model,imx,p,matcov,agemortsup);
13605:
13606: free_vector(lsurv,agegomp,AGESUP);
13607: free_vector(lpop,agegomp,AGESUP);
13608: free_vector(tpop,agegomp,AGESUP);
13609: free_matrix(ximort,1,NDIM,1,NDIM);
13610: free_ivector(dcwave,firstobs,lastobs);
13611: free_vector(agecens,firstobs,lastobs);
13612: free_vector(ageexmed,firstobs,lastobs);
13613: free_ivector(cens,firstobs,lastobs);
13614: #ifdef GSL
13615: #endif
13616: } /* Endof if mle==-3 mortality only */
13617: /* Standard */
13618: else{ /* For mle !=- 3, could be 0 or 1 or 4 etc. */
13619: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
13620: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
13621: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
13622: printf("First Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
13623: for (k=1; k<=npar;k++)
13624: printf(" %d %8.5f",k,p[k]);
13625: printf("\n");
13626: if(mle>=1){ /* Could be 1 or 2, Real Maximization */
13627: /* mlikeli uses func not funcone */
13628: /* for(i=1;i<nlstate;i++){ */
13629: /* /\*reducing xi for 1 to npar to 1 to ncovmodel; *\/ */
13630: /* mlikeli(ficres,p, ncovmodel, ncovmodel, nlstate, ftol, funcnoprod); */
13631: /* } */
13632: mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func);
13633: }
13634: if(mle==0) {/* No optimization, will print the likelihoods for the datafile */
13635: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
13636: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
13637: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
13638: }
13639: globpr=1; /* again, to print the individual contributions using computed gpimx and gsw */
13640: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
13641: printf("Second Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
13642: /* exit(0); */
13643: for (k=1; k<=npar;k++)
13644: printf(" %d %8.5f",k,p[k]);
13645: printf("\n");
13646:
13647: /*--------- results files --------------*/
13648: /* 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); */
13649:
13650:
13651: fprintf(ficres,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
13652: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n"); /* Printing model equation */
13653: fprintf(ficlog,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
13654:
13655: printf("#model= 1 + age ");
13656: fprintf(ficres,"#model= 1 + age ");
13657: fprintf(ficlog,"#model= 1 + age ");
13658: fprintf(fichtm,"\n<ul><li> model=1+age+%s\n \
13659: </ul>", model);
13660:
13661: fprintf(fichtm,"\n<table style=\"text-align:center; border: 1px solid\">\n");
13662: fprintf(fichtm, "<tr><th>Model=</th><th>1</th><th>+ age</th>");
13663: if(nagesqr==1){
13664: printf(" + age*age ");
13665: fprintf(ficres," + age*age ");
13666: fprintf(ficlog," + age*age ");
13667: fprintf(fichtm, "<th>+ age*age</th>");
13668: }
13669: for(j=1;j <=ncovmodel-2;j++){
13670: if(Typevar[j]==0) {
13671: printf(" + V%d ",Tvar[j]);
13672: fprintf(ficres," + V%d ",Tvar[j]);
13673: fprintf(ficlog," + V%d ",Tvar[j]);
13674: fprintf(fichtm, "<th>+ V%d</th>",Tvar[j]);
13675: }else if(Typevar[j]==1) {
13676: printf(" + V%d*age ",Tvar[j]);
13677: fprintf(ficres," + V%d*age ",Tvar[j]);
13678: fprintf(ficlog," + V%d*age ",Tvar[j]);
13679: fprintf(fichtm, "<th>+ V%d*age</th>",Tvar[j]);
13680: }else if(Typevar[j]==2) {
13681: printf(" + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
13682: fprintf(ficres," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
13683: fprintf(ficlog," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
13684: fprintf(fichtm, "<th>+ V%d*V%d</th>",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
13685: }
13686: }
13687: printf("\n");
13688: fprintf(ficres,"\n");
13689: fprintf(ficlog,"\n");
13690: fprintf(fichtm, "</tr>");
13691: fprintf(fichtm, "\n");
13692:
13693:
13694: for(i=1,jk=1; i <=nlstate; i++){
13695: for(k=1; k <=(nlstate+ndeath); k++){
13696: if (k != i) {
13697: fprintf(fichtm, "<tr>");
13698: printf("%d%d ",i,k);
13699: fprintf(ficlog,"%d%d ",i,k);
13700: fprintf(ficres,"%1d%1d ",i,k);
13701: fprintf(fichtm, "<td>%1d%1d</td>",i,k);
13702: for(j=1; j <=ncovmodel; j++){
13703: printf("%12.7f ",p[jk]);
13704: fprintf(ficlog,"%12.7f ",p[jk]);
13705: fprintf(ficres,"%12.7f ",p[jk]);
13706: fprintf(fichtm, "<td>%12.7f</td>",p[jk]);
13707: jk++;
13708: }
13709: printf("\n");
13710: fprintf(ficlog,"\n");
13711: fprintf(ficres,"\n");
13712: fprintf(fichtm, "</tr>\n");
13713: }
13714: }
13715: }
13716: /* fprintf(fichtm,"</tr>\n"); */
13717: fprintf(fichtm,"</table>\n");
13718: fprintf(fichtm, "\n");
13719:
13720: if(mle != 0){
13721: /* Computing hessian and covariance matrix only at a peak of the Likelihood, that is after optimization */
13722: ftolhess=ftol; /* Usually correct */
13723: hesscov(matcov, hess, p, npar, delti, ftolhess, func);
13724: 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");
13725: 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");
13726: 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);
13727: fprintf(fichtm,"\n<table style=\"text-align:center; border: 1px solid\">");
13728: fprintf(fichtm, "\n<tr><th>Model=</th><th>1</th><th>+ age</th>");
13729: if(nagesqr==1){
13730: printf(" + age*age ");
13731: fprintf(ficres," + age*age ");
13732: fprintf(ficlog," + age*age ");
13733: fprintf(fichtm, "<th>+ age*age</th>");
13734: }
13735: for(j=1;j <=ncovmodel-2;j++){
13736: if(Typevar[j]==0) {
13737: printf(" + V%d ",Tvar[j]);
13738: fprintf(fichtm, "<th>+ V%d</th>",Tvar[j]);
13739: }else if(Typevar[j]==1) {
13740: printf(" + V%d*age ",Tvar[j]);
13741: fprintf(fichtm, "<th>+ V%d*age</th>",Tvar[j]);
13742: }else if(Typevar[j]==2) {
13743: fprintf(fichtm, "<th>+ V%d*V%d</th>",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
13744: }
13745: }
13746: fprintf(fichtm, "</tr>\n");
13747:
13748: for(i=1,jk=1; i <=nlstate; i++){
13749: for(k=1; k <=(nlstate+ndeath); k++){
13750: if (k != i) {
13751: fprintf(fichtm, "<tr valign=top>");
13752: printf("%d%d ",i,k);
13753: fprintf(ficlog,"%d%d ",i,k);
13754: fprintf(fichtm, "<td>%1d%1d</td>",i,k);
13755: for(j=1; j <=ncovmodel; j++){
13756: wald=p[jk]/sqrt(matcov[jk][jk]);
13757: 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]));
13758: 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]));
13759: if(fabs(wald) > 1.96){
13760: fprintf(fichtm, "<td><b>%12.7f</b></br> (%12.7f)</br>",p[jk],sqrt(matcov[jk][jk]));
13761: }else{
13762: fprintf(fichtm, "<td>%12.7f (%12.7f)</br>",p[jk],sqrt(matcov[jk][jk]));
13763: }
13764: fprintf(fichtm,"W=%8.3f</br>",wald);
13765: fprintf(fichtm,"[%12.7f;%12.7f]</br></td>", p[jk]-1.96*sqrt(matcov[jk][jk]),p[jk]+1.96*sqrt(matcov[jk][jk]));
13766: jk++;
13767: }
13768: printf("\n");
13769: fprintf(ficlog,"\n");
13770: fprintf(fichtm, "</tr>\n");
13771: }
13772: }
13773: }
13774: } /* end of hesscov and Wald tests */
13775: fprintf(fichtm,"</table>\n");
13776:
13777: /* */
13778: fprintf(ficres,"# Scales (for hessian or gradient estimation)\n");
13779: printf("# Scales (for hessian or gradient estimation)\n");
13780: fprintf(ficlog,"# Scales (for hessian or gradient estimation)\n");
13781: for(i=1,jk=1; i <=nlstate; i++){
13782: for(j=1; j <=nlstate+ndeath; j++){
13783: if (j!=i) {
13784: fprintf(ficres,"%1d%1d",i,j);
13785: printf("%1d%1d",i,j);
13786: fprintf(ficlog,"%1d%1d",i,j);
13787: for(k=1; k<=ncovmodel;k++){
13788: printf(" %.5e",delti[jk]);
13789: fprintf(ficlog," %.5e",delti[jk]);
13790: fprintf(ficres," %.5e",delti[jk]);
13791: jk++;
13792: }
13793: printf("\n");
13794: fprintf(ficlog,"\n");
13795: fprintf(ficres,"\n");
13796: }
13797: }
13798: }
13799:
13800: 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");
13801: if(mle >= 1) /* To big for the screen */
13802: 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");
13803: 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");
13804: /* # 121 Var(a12)\n\ */
13805: /* # 122 Cov(b12,a12) Var(b12)\n\ */
13806: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
13807: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
13808: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
13809: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
13810: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
13811: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
13812:
13813:
13814: /* Just to have a covariance matrix which will be more understandable
13815: even is we still don't want to manage dictionary of variables
13816: */
13817: for(itimes=1;itimes<=2;itimes++){
13818: jj=0;
13819: for(i=1; i <=nlstate; i++){
13820: for(j=1; j <=nlstate+ndeath; j++){
13821: if(j==i) continue;
13822: for(k=1; k<=ncovmodel;k++){
13823: jj++;
13824: ca[0]= k+'a'-1;ca[1]='\0';
13825: if(itimes==1){
13826: if(mle>=1)
13827: printf("#%1d%1d%d",i,j,k);
13828: fprintf(ficlog,"#%1d%1d%d",i,j,k);
13829: fprintf(ficres,"#%1d%1d%d",i,j,k);
13830: }else{
13831: if(mle>=1)
13832: printf("%1d%1d%d",i,j,k);
13833: fprintf(ficlog,"%1d%1d%d",i,j,k);
13834: fprintf(ficres,"%1d%1d%d",i,j,k);
13835: }
13836: ll=0;
13837: for(li=1;li <=nlstate; li++){
13838: for(lj=1;lj <=nlstate+ndeath; lj++){
13839: if(lj==li) continue;
13840: for(lk=1;lk<=ncovmodel;lk++){
13841: ll++;
13842: if(ll<=jj){
13843: cb[0]= lk +'a'-1;cb[1]='\0';
13844: if(ll<jj){
13845: if(itimes==1){
13846: if(mle>=1)
13847: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
13848: fprintf(ficlog," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
13849: fprintf(ficres," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
13850: }else{
13851: if(mle>=1)
13852: printf(" %.5e",matcov[jj][ll]);
13853: fprintf(ficlog," %.5e",matcov[jj][ll]);
13854: fprintf(ficres," %.5e",matcov[jj][ll]);
13855: }
13856: }else{
13857: if(itimes==1){
13858: if(mle>=1)
13859: printf(" Var(%s%1d%1d)",ca,i,j);
13860: fprintf(ficlog," Var(%s%1d%1d)",ca,i,j);
13861: fprintf(ficres," Var(%s%1d%1d)",ca,i,j);
13862: }else{
13863: if(mle>=1)
13864: printf(" %.7e",matcov[jj][ll]);
13865: fprintf(ficlog," %.7e",matcov[jj][ll]);
13866: fprintf(ficres," %.7e",matcov[jj][ll]);
13867: }
13868: }
13869: }
13870: } /* end lk */
13871: } /* end lj */
13872: } /* end li */
13873: if(mle>=1)
13874: printf("\n");
13875: fprintf(ficlog,"\n");
13876: fprintf(ficres,"\n");
13877: numlinepar++;
13878: } /* end k*/
13879: } /*end j */
13880: } /* end i */
13881: } /* end itimes */
13882:
13883: fflush(ficlog);
13884: fflush(ficres);
13885: while(fgets(line, MAXLINE, ficpar)) {
13886: /* If line starts with a # it is a comment */
13887: if (line[0] == '#') {
13888: numlinepar++;
13889: fputs(line,stdout);
13890: fputs(line,ficparo);
13891: fputs(line,ficlog);
13892: fputs(line,ficres);
13893: continue;
13894: }else
13895: break;
13896: }
13897:
13898: /* while((c=getc(ficpar))=='#' && c!= EOF){ */
13899: /* ungetc(c,ficpar); */
13900: /* fgets(line, MAXLINE, ficpar); */
13901: /* fputs(line,stdout); */
13902: /* fputs(line,ficparo); */
13903: /* } */
13904: /* ungetc(c,ficpar); */
13905:
13906: estepm=0;
13907: 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){
13908:
13909: if (num_filled != 6) {
13910: 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);
13911: 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);
13912: goto end;
13913: }
13914: printf("agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%lf\n",ageminpar,agemaxpar, bage, fage, estepm, ftolpl);
13915: }
13916: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
13917: /*ftolpl=6.e-4;*/ /* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
13918:
13919: /* fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%\n",&ageminpar,&agemaxpar, &bage, &fage, &estepm); */
13920: if (estepm==0 || estepm < stepm) estepm=stepm;
13921: if (fage <= 2) {
13922: bage = ageminpar;
13923: fage = agemaxpar;
13924: }
13925:
13926: fprintf(ficres,"# agemin agemax for life expectancy, bage fage (if mle==0 ie no data nor Max likelihood).\n");
13927: fprintf(ficres,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
13928: fprintf(ficparo,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d, ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
13929:
13930: /* Other stuffs, more or less useful */
13931: while(fgets(line, MAXLINE, ficpar)) {
13932: /* If line starts with a # it is a comment */
13933: if (line[0] == '#') {
13934: numlinepar++;
13935: fputs(line,stdout);
13936: fputs(line,ficparo);
13937: fputs(line,ficlog);
13938: fputs(line,ficres);
13939: continue;
13940: }else
13941: break;
13942: }
13943:
13944: 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){
13945:
13946: if (num_filled != 7) {
13947: 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);
13948: 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);
13949: goto end;
13950: }
13951: printf("begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mov_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
13952: 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);
13953: 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);
13954: 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);
13955: }
13956:
13957: while(fgets(line, MAXLINE, ficpar)) {
13958: /* If line starts with a # it is a comment */
13959: if (line[0] == '#') {
13960: numlinepar++;
13961: fputs(line,stdout);
13962: fputs(line,ficparo);
13963: fputs(line,ficlog);
13964: fputs(line,ficres);
13965: continue;
13966: }else
13967: break;
13968: }
13969:
13970:
13971: dateprev1=anprev1+(mprev1-1)/12.+(jprev1-1)/365.;
13972: dateprev2=anprev2+(mprev2-1)/12.+(jprev2-1)/365.;
13973:
13974: if((num_filled=sscanf(line,"pop_based=%d\n",&popbased)) !=EOF){
13975: if (num_filled != 1) {
13976: 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);
13977: 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);
13978: goto end;
13979: }
13980: printf("pop_based=%d\n",popbased);
13981: fprintf(ficlog,"pop_based=%d\n",popbased);
13982: fprintf(ficparo,"pop_based=%d\n",popbased);
13983: fprintf(ficres,"pop_based=%d\n",popbased);
13984: }
13985:
13986: /* Results */
13987: /* Value of covariate in each resultine will be compututed (if product) and sorted according to model rank */
13988: /* It is precov[] because we need the varying age in order to compute the real cov[] of the model equation */
13989: precov=matrix(1,MAXRESULTLINESPONE,1,NCOVMAX+1);
13990: endishere=0;
13991: nresult=0;
13992: parameterline=0;
13993: do{
13994: if(!fgets(line, MAXLINE, ficpar)){
13995: endishere=1;
13996: parameterline=15;
13997: }else if (line[0] == '#') {
13998: /* If line starts with a # it is a comment */
13999: numlinepar++;
14000: fputs(line,stdout);
14001: fputs(line,ficparo);
14002: fputs(line,ficlog);
14003: fputs(line,ficres);
14004: continue;
14005: }else if(sscanf(line,"prevforecast=%[^\n]\n",modeltemp))
14006: parameterline=11;
14007: else if(sscanf(line,"prevbackcast=%[^\n]\n",modeltemp))
14008: parameterline=12;
14009: else if(sscanf(line,"result:%[^\n]\n",modeltemp)){
14010: parameterline=13;
14011: }
14012: else{
14013: parameterline=14;
14014: }
14015: switch (parameterline){ /* =0 only if only comments */
14016: case 11:
14017: 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)){
14018: 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);
14019: 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);
14020: 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);
14021: 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);
14022: /* day and month of proj2 are not used but only year anproj2.*/
14023: dateproj1=anproj1+(mproj1-1)/12.+(jproj1-1)/365.;
14024: dateproj2=anproj2+(mproj2-1)/12.+(jproj2-1)/365.;
14025: prvforecast = 1;
14026: }
14027: else if((num_filled=sscanf(line,"prevforecast=%d yearsfproj=%lf mobil_average=%d\n",&prevfcast,&yrfproj,&mobilavproj)) !=EOF){/* && (num_filled == 3))*/
14028: printf("prevforecast=%d yearsfproj=%.2lf mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
14029: fprintf(ficlog,"prevforecast=%d yearsfproj=%.2lf mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
14030: fprintf(ficres,"prevforecast=%d yearsfproj=%.2lf mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
14031: prvforecast = 2;
14032: }
14033: else {
14034: 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);
14035: 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);
14036: goto end;
14037: }
14038: break;
14039: case 12:
14040: 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)){
14041: 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);
14042: 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);
14043: 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);
14044: 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);
14045: /* day and month of back2 are not used but only year anback2.*/
14046: dateback1=anback1+(mback1-1)/12.+(jback1-1)/365.;
14047: dateback2=anback2+(mback2-1)/12.+(jback2-1)/365.;
14048: prvbackcast = 1;
14049: }
14050: else if((num_filled=sscanf(line,"prevbackcast=%d yearsbproj=%lf mobil_average=%d\n",&prevbcast,&yrbproj,&mobilavproj)) ==3){/* && (num_filled == 3))*/
14051: printf("prevbackcast=%d yearsbproj=%.2lf mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
14052: fprintf(ficlog,"prevbackcast=%d yearsbproj=%.2lf mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
14053: fprintf(ficres,"prevbackcast=%d yearsbproj=%.2lf mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
14054: prvbackcast = 2;
14055: }
14056: else {
14057: 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);
14058: 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);
14059: goto end;
14060: }
14061: break;
14062: case 13:
14063: num_filled=sscanf(line,"result:%[^\n]\n",resultlineori);
14064: nresult++; /* Sum of resultlines */
14065: /* printf("Result %d: result:%s\n",nresult, resultlineori); */
14066: /* removefirstspace(&resultlineori); */
14067:
14068: if(strstr(resultlineori,"v") !=0){
14069: printf("Error. 'v' must be in upper case 'V' result: %s ",resultlineori);
14070: fprintf(ficlog,"Error. 'v' must be in upper case result: %s ",resultlineori);fflush(ficlog);
14071: return 1;
14072: }
14073: trimbb(resultline, resultlineori); /* Suppressing double blank in the resultline */
14074: /* printf("Decoderesult resultline=\"%s\" resultlineori=\"%s\"\n", resultline, resultlineori); */
14075: if(nresult > MAXRESULTLINESPONE-1){
14076: 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);
14077: 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);
14078: goto end;
14079: }
14080:
14081: if(!decoderesult(resultline, nresult)){ /* Fills TKresult[nresult] combination and Tresult[nresult][k4+1] combination values */
14082: fprintf(ficparo,"result: %s\n",resultline);
14083: fprintf(ficres,"result: %s\n",resultline);
14084: fprintf(ficlog,"result: %s\n",resultline);
14085: } else
14086: goto end;
14087: break;
14088: case 14:
14089: printf("Error: Unknown command '%s'\n",line);
14090: fprintf(ficlog,"Error: Unknown command '%s'\n",line);
14091: if(line[0] == ' ' || line[0] == '\n'){
14092: printf("It should not be an empty line '%s'\n",line);
14093: fprintf(ficlog,"It should not be an empty line '%s'\n",line);
14094: }
14095: if(ncovmodel >=2 && nresult==0 ){
14096: printf("ERROR: no result lines! It should be at minimum 'result: V2=0 V1=1 or result:.' %s\n",line);
14097: fprintf(ficlog,"ERROR: no result lines! It should be at minimum 'result: V2=0 V1=1 or result:.' %s\n",line);
14098: }
14099: /* goto end; */
14100: break;
14101: case 15:
14102: printf("End of resultlines.\n");
14103: fprintf(ficlog,"End of resultlines.\n");
14104: break;
14105: default: /* parameterline =0 */
14106: nresult=1;
14107: decoderesult(".",nresult ); /* No covariate */
14108: } /* End switch parameterline */
14109: }while(endishere==0); /* End do */
14110:
14111: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint); */
14112: /* ,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2); */
14113:
14114: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
14115: if(ageminpar == AGEOVERFLOW ||agemaxpar == -AGEOVERFLOW){
14116: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
14117: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
14118: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
14119: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
14120: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
14121: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
14122: }else{
14123: /* printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, prevfcast, backcast, pathc,p, (int)anproj1-(int)agemin, (int)anback1-(int)agemax+1); */
14124: /* It seems that anprojd which is computed from the mean year at interview which is known yet because of freqsummary */
14125: /* date2dmy(dateintmean,&jintmean,&mintmean,&aintmean); */ /* Done in freqsummary */
14126: if(prvforecast==1){
14127: dateprojd=(jproj1+12*mproj1+365*anproj1)/365;
14128: jprojd=jproj1;
14129: mprojd=mproj1;
14130: anprojd=anproj1;
14131: dateprojf=(jproj2+12*mproj2+365*anproj2)/365;
14132: jprojf=jproj2;
14133: mprojf=mproj2;
14134: anprojf=anproj2;
14135: } else if(prvforecast == 2){
14136: dateprojd=dateintmean;
14137: date2dmy(dateprojd,&jprojd, &mprojd, &anprojd);
14138: dateprojf=dateintmean+yrfproj;
14139: date2dmy(dateprojf,&jprojf, &mprojf, &anprojf);
14140: }
14141: if(prvbackcast==1){
14142: datebackd=(jback1+12*mback1+365*anback1)/365;
14143: jbackd=jback1;
14144: mbackd=mback1;
14145: anbackd=anback1;
14146: datebackf=(jback2+12*mback2+365*anback2)/365;
14147: jbackf=jback2;
14148: mbackf=mback2;
14149: anbackf=anback2;
14150: } else if(prvbackcast == 2){
14151: datebackd=dateintmean;
14152: date2dmy(datebackd,&jbackd, &mbackd, &anbackd);
14153: datebackf=dateintmean-yrbproj;
14154: date2dmy(datebackf,&jbackf, &mbackf, &anbackf);
14155: }
14156:
14157: printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,bage, fage, prevfcast, prevbcast, pathc,p, (int)anprojd-bage, (int)anbackd-fage);
14158: }
14159: printinghtml(fileresu,title,datafile, firstpass, lastpass, stepm, weightopt, \
14160: model,imx,jmin,jmax,jmean,rfileres,popforecast,mobilav,prevfcast,mobilavproj,prevbcast, estepm, \
14161: jprev1,mprev1,anprev1,dateprev1, dateprojd, datebackd,jprev2,mprev2,anprev2,dateprev2,dateprojf, datebackf);
14162:
14163: /*------------ free_vector -------------*/
14164: /* chdir(path); */
14165:
14166: /* free_ivector(wav,1,imx); */ /* Moved after last prevalence call */
14167: /* free_imatrix(dh,1,lastpass-firstpass+2,1,imx); */
14168: /* free_imatrix(bh,1,lastpass-firstpass+2,1,imx); */
14169: /* free_imatrix(mw,1,lastpass-firstpass+2,1,imx); */
14170: free_lvector(num,firstobs,lastobs);
14171: free_vector(agedc,firstobs,lastobs);
14172: /*free_matrix(covar,0,NCOVMAX,1,n);*/
14173: /*free_matrix(covar,1,NCOVMAX,1,n);*/
14174: fclose(ficparo);
14175: fclose(ficres);
14176:
14177:
14178: /* Other results (useful)*/
14179:
14180:
14181: /*--------------- Prevalence limit (period or stable prevalence) --------------*/
14182: /*#include "prevlim.h"*/ /* Use ficrespl, ficlog */
14183: prlim=matrix(1,nlstate,1,nlstate);
14184: /* Computes the prevalence limit for each combination k of the dummy covariates by calling prevalim(k) */
14185: prevalence_limit(p, prlim, ageminpar, agemaxpar, ftolpl, &ncvyear);
14186: fclose(ficrespl);
14187:
14188: /*------------- h Pij x at various ages ------------*/
14189: /*#include "hpijx.h"*/
14190: /** 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?*/
14191: /* calls hpxij with combination k */
14192: hPijx(p, bage, fage);
14193: fclose(ficrespij);
14194:
14195: /* ncovcombmax= pow(2,cptcoveff); */
14196: /*-------------- Variance of one-step probabilities for a combination ij or for nres ?---*/
14197: k=1;
14198: varprob(optionfilefiname, matcov, p, delti, nlstate, bage, fage,k,Tvar,nbcode, ncodemax,strstart);
14199:
14200: /* Prevalence for each covariate combination in probs[age][status][cov] */
14201: probs= ma3x(AGEINF,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
14202: for(i=AGEINF;i<=AGESUP;i++)
14203: for(j=1;j<=nlstate+ndeath;j++) /* ndeath is useless but a necessity to be compared with mobaverages */
14204: for(k=1;k<=ncovcombmax;k++)
14205: probs[i][j][k]=0.;
14206: prevalence(probs, ageminpar, agemaxpar, s, agev, nlstate, imx, Tvar, nbcode,
14207: ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
14208: if (mobilav!=0 ||mobilavproj !=0 ) {
14209: mobaverages= ma3x(AGEINF, AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
14210: for(i=AGEINF;i<=AGESUP;i++)
14211: for(j=1;j<=nlstate+ndeath;j++)
14212: for(k=1;k<=ncovcombmax;k++)
14213: mobaverages[i][j][k]=0.;
14214: mobaverage=mobaverages;
14215: if (mobilav!=0) {
14216: printf("Movingaveraging observed prevalence\n");
14217: fprintf(ficlog,"Movingaveraging observed prevalence\n");
14218: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilav)!=0){
14219: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
14220: printf(" Error in movingaverage mobilav=%d\n",mobilav);
14221: }
14222: } else if (mobilavproj !=0) {
14223: printf("Movingaveraging projected observed prevalence\n");
14224: fprintf(ficlog,"Movingaveraging projected observed prevalence\n");
14225: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilavproj)!=0){
14226: fprintf(ficlog," Error in movingaverage mobilavproj=%d\n",mobilavproj);
14227: printf(" Error in movingaverage mobilavproj=%d\n",mobilavproj);
14228: }
14229: }else{
14230: printf("Internal error moving average\n");
14231: fflush(stdout);
14232: exit(1);
14233: }
14234: }/* end if moving average */
14235:
14236: /*---------- Forecasting ------------------*/
14237: if(prevfcast==1){
14238: /* /\* if(stepm ==1){*\/ */
14239: /* /\* anproj1, mproj1, jproj1 either read explicitly or yrfproj *\/ */
14240: /*This done previously after freqsummary.*/
14241: /* dateprojd=(jproj1+12*mproj1+365*anproj1)/365; */
14242: /* dateprojf=(jproj2+12*mproj2+365*anproj2)/365; */
14243:
14244: /* } else if (prvforecast==2){ */
14245: /* /\* if(stepm ==1){*\/ */
14246: /* /\* anproj1, mproj1, jproj1 either read explicitly or yrfproj *\/ */
14247: /* } */
14248: /*prevforecast(fileresu, dateintmean, anproj1, mproj1, jproj1, agemin, agemax, dateprev1, dateprev2, mobilavproj, mobaverage, bage, fage, firstpass, lastpass, anproj2, p, cptcoveff);*/
14249: prevforecast(fileresu,dateintmean, dateprojd, dateprojf, agemin, agemax, dateprev1, dateprev2, mobilavproj, mobaverage, bage, fage, firstpass, lastpass, p, cptcoveff);
14250: }
14251:
14252: /* Prevbcasting */
14253: if(prevbcast==1){
14254: ddnewms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
14255: ddoldms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
14256: ddsavms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
14257:
14258: /*--------------- Back Prevalence limit (period or stable prevalence) --------------*/
14259:
14260: bprlim=matrix(1,nlstate,1,nlstate);
14261:
14262: back_prevalence_limit(p, bprlim, ageminpar, agemaxpar, ftolpl, &ncvyear, dateprev1, dateprev2, firstpass, lastpass, mobilavproj);
14263: fclose(ficresplb);
14264:
14265: hBijx(p, bage, fage, mobaverage);
14266: fclose(ficrespijb);
14267:
14268: /* /\* prevbackforecast(fileresu, mobaverage, anback1, mback1, jback1, agemin, agemax, dateprev1, dateprev2, *\/ */
14269: /* /\* mobilavproj, bage, fage, firstpass, lastpass, anback2, p, cptcoveff); *\/ */
14270: /* prevbackforecast(fileresu, mobaverage, anback1, mback1, jback1, agemin, agemax, dateprev1, dateprev2, */
14271: /* mobilavproj, bage, fage, firstpass, lastpass, anback2, p, cptcoveff); */
14272: prevbackforecast(fileresu, mobaverage, dateintmean, dateprojd, dateprojf, agemin, agemax, dateprev1, dateprev2,
14273: mobilavproj, bage, fage, firstpass, lastpass, p, cptcoveff);
14274:
14275:
14276: varbprlim(fileresu, nresult, mobaverage, mobilavproj, bage, fage, bprlim, &ncvyear, ftolpl, p, matcov, delti, stepm, cptcoveff);
14277:
14278:
14279: free_matrix(bprlim,1,nlstate,1,nlstate); /*here or after loop ? */
14280: free_matrix(ddnewms, 1, nlstate+ndeath, 1, nlstate+ndeath);
14281: free_matrix(ddsavms, 1, nlstate+ndeath, 1, nlstate+ndeath);
14282: free_matrix(ddoldms, 1, nlstate+ndeath, 1, nlstate+ndeath);
14283: } /* end Prevbcasting */
14284:
14285:
14286: /* ------ Other prevalence ratios------------ */
14287:
14288: free_ivector(wav,1,imx);
14289: free_imatrix(dh,1,lastpass-firstpass+2,1,imx);
14290: free_imatrix(bh,1,lastpass-firstpass+2,1,imx);
14291: free_imatrix(mw,1,lastpass-firstpass+2,1,imx);
14292:
14293:
14294: /*---------- Health expectancies, no variances ------------*/
14295:
14296: strcpy(filerese,"E_");
14297: strcat(filerese,fileresu);
14298: if((ficreseij=fopen(filerese,"w"))==NULL) {
14299: printf("Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
14300: fprintf(ficlog,"Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
14301: }
14302: printf("Computing Health Expectancies: result on file '%s' ...", filerese);fflush(stdout);
14303: fprintf(ficlog,"Computing Health Expectancies: result on file '%s' ...", filerese);fflush(ficlog);
14304:
14305: pstamp(ficreseij);
14306:
14307: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
14308: if (cptcovn < 1){i1=1;}
14309:
14310: for(nres=1; nres <= nresult; nres++) /* For each resultline */
14311: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying */
14312: if(i1 != 1 && TKresult[nres]!= k)
14313: continue;
14314: fprintf(ficreseij,"\n#****** ");
14315: printf("\n#****** ");
14316: for(j=1;j<=cptcoveff;j++) {
14317: fprintf(ficreseij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]);
14318: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]);
14319: }
14320: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
14321: printf(" V%d=%lg ",TvarsQ[j], TinvDoQresult[nres][TvarsQ[j]]); /* TvarsQ[j] gives the name of the jth quantitative (fixed or time v) */
14322: fprintf(ficreseij,"V%d=%lg ",TvarsQ[j], TinvDoQresult[nres][TvarsQ[j]]);
14323: }
14324: fprintf(ficreseij,"******\n");
14325: printf("******\n");
14326:
14327: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
14328: oldm=oldms;savm=savms;
14329: /* printf("HELLO Entering evsij bage=%d fage=%d k=%d estepm=%d nres=%d\n",(int) bage, (int)fage, k, estepm, nres); */
14330: evsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, strstart, nres);
14331:
14332: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
14333: }
14334: fclose(ficreseij);
14335: printf("done evsij\n");fflush(stdout);
14336: fprintf(ficlog,"done evsij\n");fflush(ficlog);
14337:
14338:
14339: /*---------- State-specific expectancies and variances ------------*/
14340: /* Should be moved in a function */
14341: strcpy(filerest,"T_");
14342: strcat(filerest,fileresu);
14343: if((ficrest=fopen(filerest,"w"))==NULL) {
14344: printf("Problem with total LE resultfile: %s\n", filerest);goto end;
14345: fprintf(ficlog,"Problem with total LE resultfile: %s\n", filerest);goto end;
14346: }
14347: printf("Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(stdout);
14348: fprintf(ficlog,"Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(ficlog);
14349: strcpy(fileresstde,"STDE_");
14350: strcat(fileresstde,fileresu);
14351: if((ficresstdeij=fopen(fileresstde,"w"))==NULL) {
14352: printf("Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
14353: fprintf(ficlog,"Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
14354: }
14355: printf(" Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde);
14356: fprintf(ficlog," Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde);
14357:
14358: strcpy(filerescve,"CVE_");
14359: strcat(filerescve,fileresu);
14360: if((ficrescveij=fopen(filerescve,"w"))==NULL) {
14361: printf("Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0);
14362: fprintf(ficlog,"Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0);
14363: }
14364: printf(" Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve);
14365: fprintf(ficlog," Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve);
14366:
14367: strcpy(fileresv,"V_");
14368: strcat(fileresv,fileresu);
14369: if((ficresvij=fopen(fileresv,"w"))==NULL) {
14370: printf("Problem with variance resultfile: %s\n", fileresv);exit(0);
14371: fprintf(ficlog,"Problem with variance resultfile: %s\n", fileresv);exit(0);
14372: }
14373: printf(" Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(stdout);
14374: fprintf(ficlog," Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(ficlog);
14375:
14376: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
14377: if (cptcovn < 1){i1=1;}
14378:
14379: for(nres=1; nres <= nresult; nres++) /* For each resultline, find the combination and output results according to the values of dummies and then quanti. */
14380: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying. For each nres and each value at position k
14381: * we know Tresult[nres][result_position]= value of the dummy variable at the result_position in the nres resultline
14382: * Tvqresult[nres][result_position]= id of the variable at the result_position in the nres resultline
14383: * and Tqresult[nres][result_position]= value of the variable at the result_position in the nres resultline */
14384: /* */
14385: if(i1 != 1 && TKresult[nres]!= k) /* TKresult[nres] is the combination of this nres resultline. All the i1 combinations are not output */
14386: continue;
14387: printf("\n# model %s \n#****** Result for:", model);
14388: fprintf(ficrest,"\n# model %s \n#****** Result for:", model);
14389: fprintf(ficlog,"\n# model %s \n#****** Result for:", model);
14390: /* It might not be a good idea to mix dummies and quantitative */
14391: /* 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 *\/ */
14392: 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 */
14393: /* printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); /\* Output by variables in the resultline *\/ */
14394: /* Tvaraff[j] is the name of the dummy variable in position j in the equation model:
14395: * Tvaraff[1]@9={4, 3, 0, 0, 0, 0, 0, 0, 0}, in model=V5+V4+V3+V4*V3+V5*age
14396: * (V5 is quanti) V4 and V3 are dummies
14397: * TnsdVar[4] is the position 1 and TnsdVar[3]=2 in codtabm(k,l)(V4 V3)=V4 V3
14398: * l=1 l=2
14399: * k=1 1 1 0 0
14400: * k=2 2 1 1 0
14401: * k=3 [1] [2] 0 1
14402: * k=4 2 2 1 1
14403: * If nres=1 result: V3=1 V4=0 then k=3 and outputs
14404: * If nres=2 result: V4=1 V3=0 then k=2 and outputs
14405: * nres=1 =>k=3 j=1 V4= nbcode[4][codtabm(3,1)=1)=0; j=2 V3= nbcode[3][codtabm(3,2)=2]=1
14406: * nres=2 =>k=2 j=1 V4= nbcode[4][codtabm(2,1)=2)=1; j=2 V3= nbcode[3][codtabm(2,2)=1]=0
14407: */
14408: /* Tvresult[nres][j] Name of the variable at position j in this resultline */
14409: /* Tresult[nres][j] Value of this variable at position j could be a float if quantitative */
14410: /* We give up with the combinations!! */
14411: /* if(debugILK) */
14412: /* 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 *\/ */
14413:
14414: if(Dummy[modelresult[nres][j]]==0){/* Dummy variable of the variable in position modelresult in the model corresponding to j in resultline */
14415: /* 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] */
14416: 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 */
14417: 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 */
14418: 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 */
14419: if(Fixed[modelresult[nres][j]]==0){ /* Fixed */
14420: printf("fixed ");fprintf(ficlog,"fixed ");fprintf(ficrest,"fixed ");
14421: }else{
14422: printf("varyi ");fprintf(ficlog,"varyi ");fprintf(ficrest,"varyi ");
14423: }
14424: /* fprintf(ficrest,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14425: /* fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14426: }else if(Dummy[modelresult[nres][j]]==1){ /* Quanti variable */
14427: /* For each selected (single) quantitative value */
14428: printf(" V%d=%lg ",Tvqresult[nres][j],Tqresult[nres][j]);
14429: fprintf(ficlog," V%d=%lg ",Tvqresult[nres][j],Tqresult[nres][j]);
14430: fprintf(ficrest," V%d=%lg ",Tvqresult[nres][j],Tqresult[nres][j]);
14431: if(Fixed[modelresult[nres][j]]==0){ /* Fixed */
14432: printf("fixed ");fprintf(ficlog,"fixed ");fprintf(ficrest,"fixed ");
14433: }else{
14434: printf("varyi ");fprintf(ficlog,"varyi ");fprintf(ficrest,"varyi ");
14435: }
14436: }else{
14437: 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 */
14438: 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 */
14439: exit(1);
14440: }
14441: } /* End loop for each variable in the resultline */
14442: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
14443: /* printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); /\* Wrong j is not in the equation model *\/ */
14444: /* fprintf(ficrest," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
14445: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
14446: /* } */
14447: fprintf(ficrest,"******\n");
14448: fprintf(ficlog,"******\n");
14449: printf("******\n");
14450:
14451: fprintf(ficresstdeij,"\n#****** ");
14452: fprintf(ficrescveij,"\n#****** ");
14453: /* It could have been: for(j=1;j<=cptcoveff;j++) {printf("V=%d=%lg",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);} */
14454: /* But it won't be sorted and depends on how the resultline is ordered */
14455: for(j=1;j<=cptcoveff;j++) {
14456: fprintf(ficresstdeij,"V%d=%d ",Tvresult[nres][j],Tresult[nres][j]);
14457: /* fprintf(ficresstdeij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14458: /* fprintf(ficrescveij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14459: }
14460: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value, TvarsQind gives the position of a quantitative in model equation */
14461: fprintf(ficresstdeij," V%d=%lg ",Tvar[TvarsQind[j]],Tqresult[nres][resultmodel[nres][TvarsQind[j]]]);
14462: fprintf(ficrescveij," V%d=%lg ",Tvar[TvarsQind[j]],Tqresult[nres][resultmodel[nres][TvarsQind[j]]]);
14463: }
14464: fprintf(ficresstdeij,"******\n");
14465: fprintf(ficrescveij,"******\n");
14466:
14467: fprintf(ficresvij,"\n#****** ");
14468: /* pstamp(ficresvij); */
14469: for(j=1;j<=cptcoveff;j++)
14470: fprintf(ficresvij,"V%d=%d ",Tvresult[nres][j],Tresult[nres][j]);
14471: /* fprintf(ficresvij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[TnsdVar[Tvaraff[j]]])]); */
14472: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
14473: /* fprintf(ficresvij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]); /\* To solve *\/ */
14474: fprintf(ficresvij," V%d=%lg ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); /* Solved */
14475: }
14476: fprintf(ficresvij,"******\n");
14477:
14478: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
14479: oldm=oldms;savm=savms;
14480: printf(" cvevsij ");
14481: fprintf(ficlog, " cvevsij ");
14482: cvevsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, delti, matcov, strstart, nres);
14483: printf(" end cvevsij \n ");
14484: fprintf(ficlog, " end cvevsij \n ");
14485:
14486: /*
14487: */
14488: /* goto endfree; */
14489:
14490: vareij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
14491: pstamp(ficrest);
14492:
14493: epj=vector(1,nlstate+1);
14494: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
14495: oldm=oldms;savm=savms; /* ZZ Segmentation fault */
14496: cptcod= 0; /* To be deleted */
14497: printf("varevsij vpopbased=%d \n",vpopbased);
14498: fprintf(ficlog, "varevsij vpopbased=%d \n",vpopbased);
14499: 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 */
14500: 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 ");
14501: if(vpopbased==1)
14502: 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);
14503: else
14504: fprintf(ficrest,"the age specific forward period (stable) prevalences in each health state \n");
14505: fprintf(ficrest,"# Age popbased mobilav e.. (std) "); /* Adding covariate values? */
14506: for (i=1;i<=nlstate;i++) fprintf(ficrest,"e.%d (std) ",i);
14507: fprintf(ficrest,"\n");
14508: /* printf("Which p?\n"); for(i=1;i<=npar;i++)printf("p[i=%d]=%lf,",i,p[i]);printf("\n"); */
14509: printf("Computing age specific forward period (stable) prevalences in each health state \n");
14510: fprintf(ficlog,"Computing age specific forward period (stable) prevalences in each health state \n");
14511: for(age=bage; age <=fage ;age++){
14512: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, &ncvyear, k, nres); /*ZZ Is it the correct prevalim */
14513: if (vpopbased==1) {
14514: if(mobilav ==0){
14515: for(i=1; i<=nlstate;i++)
14516: prlim[i][i]=probs[(int)age][i][k];
14517: }else{ /* mobilav */
14518: for(i=1; i<=nlstate;i++)
14519: prlim[i][i]=mobaverage[(int)age][i][k];
14520: }
14521: }
14522:
14523: fprintf(ficrest," %4.0f %d %d",age, vpopbased, mobilav);
14524: /* fprintf(ficrest," %4.0f %d %d %d %d",age, vpopbased, mobilav,Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */ /* to be done */
14525: /* printf(" age %4.0f ",age); */
14526: for(j=1, epj[nlstate+1]=0.;j <=nlstate;j++){
14527: for(i=1, epj[j]=0.;i <=nlstate;i++) {
14528: epj[j] += prlim[i][i]*eij[i][j][(int)age];
14529: /*ZZZ printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]);*/
14530: /* printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]); */
14531: }
14532: epj[nlstate+1] +=epj[j];
14533: }
14534: /* printf(" age %4.0f \n",age); */
14535:
14536: for(i=1, vepp=0.;i <=nlstate;i++)
14537: for(j=1;j <=nlstate;j++)
14538: vepp += vareij[i][j][(int)age];
14539: fprintf(ficrest," %7.3f (%7.3f)", epj[nlstate+1],sqrt(vepp));
14540: for(j=1;j <=nlstate;j++){
14541: fprintf(ficrest," %7.3f (%7.3f)", epj[j],sqrt(vareij[j][j][(int)age]));
14542: }
14543: fprintf(ficrest,"\n");
14544: }
14545: } /* End vpopbased */
14546: free_vector(epj,1,nlstate+1);
14547: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
14548: free_ma3x(vareij,1,nlstate,1,nlstate,(int) bage, (int)fage);
14549: printf("done selection\n");fflush(stdout);
14550: fprintf(ficlog,"done selection\n");fflush(ficlog);
14551:
14552: } /* End k selection or end covariate selection for nres */
14553:
14554: printf("done State-specific expectancies\n");fflush(stdout);
14555: fprintf(ficlog,"done State-specific expectancies\n");fflush(ficlog);
14556:
14557: /* variance-covariance of forward period prevalence */
14558: varprlim(fileresu, nresult, mobaverage, mobilavproj, bage, fage, prlim, &ncvyear, ftolpl, p, matcov, delti, stepm, cptcoveff);
14559:
14560:
14561: free_vector(weight,firstobs,lastobs);
14562: free_imatrix(Tvardk,1,NCOVMAX,1,2);
14563: free_imatrix(Tvard,1,NCOVMAX,1,2);
14564: free_imatrix(s,1,maxwav+1,firstobs,lastobs);
14565: free_matrix(anint,1,maxwav,firstobs,lastobs);
14566: free_matrix(mint,1,maxwav,firstobs,lastobs);
14567: free_ivector(cod,firstobs,lastobs);
14568: free_ivector(tab,1,NCOVMAX);
14569: fclose(ficresstdeij);
14570: fclose(ficrescveij);
14571: fclose(ficresvij);
14572: fclose(ficrest);
14573: fclose(ficpar);
14574:
14575:
14576: /*---------- End : free ----------------*/
14577: if (mobilav!=0 ||mobilavproj !=0)
14578: free_ma3x(mobaverages,AGEINF, AGESUP,1,nlstate+ndeath, 1,ncovcombmax); /* We need to have a squared matrix with prevalence of the dead! */
14579: free_ma3x(probs,AGEINF,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
14580: free_matrix(prlim,1,nlstate,1,nlstate); /*here or after loop ? */
14581: free_matrix(pmmij,1,nlstate+ndeath,1,nlstate+ndeath);
14582: } /* mle==-3 arrives here for freeing */
14583: /* endfree:*/
14584: free_matrix(oldms, 1,nlstate+ndeath,1,nlstate+ndeath);
14585: free_matrix(newms, 1,nlstate+ndeath,1,nlstate+ndeath);
14586: free_matrix(savms, 1,nlstate+ndeath,1,nlstate+ndeath);
14587: /* if(ntv+nqtv>=1)free_ma3x(cotvar,1,maxwav,1,ntv+nqtv,firstobs,lastobs); */
14588: if(ntv+nqtv>=1)free_ma3x(cotvar,1,maxwav,ncovcol+nqv+1,ncovcol+nqv+ntv+nqtv,firstobs,lastobs);
14589: if(nqtv>=1)free_ma3x(cotqvar,1,maxwav,1,nqtv,firstobs,lastobs);
14590: if(nqv>=1)free_matrix(coqvar,1,nqv,firstobs,lastobs);
14591: free_matrix(covar,0,NCOVMAX,firstobs,lastobs);
14592: free_matrix(matcov,1,npar,1,npar);
14593: free_matrix(hess,1,npar,1,npar);
14594: /*free_vector(delti,1,npar);*/
14595: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
14596: free_matrix(agev,1,maxwav,1,imx);
14597: free_ma3x(paramstart,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
14598: free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
14599:
14600: free_ivector(ncodemax,1,NCOVMAX);
14601: free_ivector(ncodemaxwundef,1,NCOVMAX);
14602: free_ivector(Dummy,-1,NCOVMAX);
14603: free_ivector(Fixed,-1,NCOVMAX);
14604: free_ivector(DummyV,1,NCOVMAX);
14605: free_ivector(FixedV,1,NCOVMAX);
14606: free_ivector(Typevar,-1,NCOVMAX);
14607: free_ivector(Tvar,1,NCOVMAX);
14608: free_ivector(TvarsQ,1,NCOVMAX);
14609: free_ivector(TvarsQind,1,NCOVMAX);
14610: free_ivector(TvarsD,1,NCOVMAX);
14611: free_ivector(TnsdVar,1,NCOVMAX);
14612: free_ivector(TvarsDind,1,NCOVMAX);
14613: free_ivector(TvarFD,1,NCOVMAX);
14614: free_ivector(TvarFDind,1,NCOVMAX);
14615: free_ivector(TvarF,1,NCOVMAX);
14616: free_ivector(TvarFind,1,NCOVMAX);
14617: free_ivector(TvarV,1,NCOVMAX);
14618: free_ivector(TvarVind,1,NCOVMAX);
14619: free_ivector(TvarA,1,NCOVMAX);
14620: free_ivector(TvarAind,1,NCOVMAX);
14621: free_ivector(TvarFQ,1,NCOVMAX);
14622: free_ivector(TvarFQind,1,NCOVMAX);
14623: free_ivector(TvarVD,1,NCOVMAX);
14624: free_ivector(TvarVDind,1,NCOVMAX);
14625: free_ivector(TvarVQ,1,NCOVMAX);
14626: free_ivector(TvarVQind,1,NCOVMAX);
14627: free_ivector(TvarVV,1,NCOVMAX);
14628: free_ivector(TvarVVind,1,NCOVMAX);
14629:
14630: free_ivector(Tvarsel,1,NCOVMAX);
14631: free_vector(Tvalsel,1,NCOVMAX);
14632: free_ivector(Tposprod,1,NCOVMAX);
14633: free_ivector(Tprod,1,NCOVMAX);
14634: free_ivector(Tvaraff,1,NCOVMAX);
14635: free_ivector(invalidvarcomb,0,ncovcombmax);
14636: free_ivector(Tage,1,NCOVMAX);
14637: free_ivector(Tmodelind,1,NCOVMAX);
14638: free_ivector(TmodelInvind,1,NCOVMAX);
14639: free_ivector(TmodelInvQind,1,NCOVMAX);
14640:
14641: free_matrix(precov, 1,MAXRESULTLINESPONE,1,NCOVMAX+1); /* Could be elsewhere ?*/
14642:
14643: free_imatrix(nbcode,0,NCOVMAX,0,NCOVMAX);
14644: /* free_imatrix(codtab,1,100,1,10); */
14645: fflush(fichtm);
14646: fflush(ficgp);
14647:
14648:
14649: if((nberr >0) || (nbwarn>0)){
14650: printf("End of Imach with %d errors and/or %d warnings. Please look at the log file for details.\n",nberr,nbwarn);
14651: fprintf(ficlog,"End of Imach with %d errors and/or warnings %d. Please look at the log file for details.\n",nberr,nbwarn);
14652: }else{
14653: printf("End of Imach\n");
14654: fprintf(ficlog,"End of Imach\n");
14655: }
14656: printf("See log file on %s\n",filelog);
14657: /* gettimeofday(&end_time, (struct timezone*)0);*/ /* after time */
14658: /*(void) gettimeofday(&end_time,&tzp);*/
14659: rend_time = time(NULL);
14660: end_time = *localtime(&rend_time);
14661: /* tml = *localtime(&end_time.tm_sec); */
14662: strcpy(strtend,asctime(&end_time));
14663: printf("Local time at start %s\nLocal time at end %s",strstart, strtend);
14664: fprintf(ficlog,"Local time at start %s\nLocal time at end %s\n",strstart, strtend);
14665: printf("Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
14666:
14667: printf("Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
14668: fprintf(ficlog,"Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
14669: fprintf(ficlog,"Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
14670: /* printf("Total time was %d uSec.\n", total_usecs);*/
14671: /* if(fileappend(fichtm,optionfilehtm)){ */
14672: fprintf(fichtm,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
14673: fclose(fichtm);
14674: fprintf(fichtmcov,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
14675: fclose(fichtmcov);
14676: fclose(ficgp);
14677: fclose(ficlog);
14678: /*------ End -----------*/
14679:
14680:
14681: /* Executes gnuplot */
14682:
14683: printf("Before Current directory %s!\n",pathcd);
14684: #ifdef WIN32
14685: if (_chdir(pathcd) != 0)
14686: printf("Can't move to directory %s!\n",path);
14687: if(_getcwd(pathcd,MAXLINE) > 0)
14688: #else
14689: if(chdir(pathcd) != 0)
14690: printf("Can't move to directory %s!\n", path);
14691: if (getcwd(pathcd, MAXLINE) > 0)
14692: #endif
14693: printf("Current directory %s!\n",pathcd);
14694: /*strcat(plotcmd,CHARSEPARATOR);*/
14695: sprintf(plotcmd,"gnuplot");
14696: #ifdef _WIN32
14697: sprintf(plotcmd,"\"%sgnuplot.exe\"",pathimach);
14698: #endif
14699: if(!stat(plotcmd,&info)){
14700: printf("Error or gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
14701: if(!stat(getenv("GNUPLOTBIN"),&info)){
14702: printf("Error or gnuplot program not found: '%s' Environment GNUPLOTBIN not set.\n",plotcmd);fflush(stdout);
14703: }else
14704: strcpy(pplotcmd,plotcmd);
14705: #ifdef __unix
14706: strcpy(plotcmd,GNUPLOTPROGRAM);
14707: if(!stat(plotcmd,&info)){
14708: printf("Error gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
14709: }else
14710: strcpy(pplotcmd,plotcmd);
14711: #endif
14712: }else
14713: strcpy(pplotcmd,plotcmd);
14714:
14715: sprintf(plotcmd,"%s %s",pplotcmd, optionfilegnuplot);
14716: printf("Starting graphs with: '%s'\n",plotcmd);fflush(stdout);
14717: strcpy(pplotcmd,plotcmd);
14718:
14719: if((outcmd=system(plotcmd)) != 0){
14720: printf("Error in gnuplot, command might not be in your path: '%s', err=%d\n", plotcmd, outcmd);
14721: printf("\n Trying if gnuplot resides on the same directory that IMaCh\n");
14722: sprintf(plotcmd,"%sgnuplot %s", pathimach, optionfilegnuplot);
14723: if((outcmd=system(plotcmd)) != 0){
14724: printf("\n Still a problem with gnuplot command %s, err=%d\n", plotcmd, outcmd);
14725: strcpy(plotcmd,pplotcmd);
14726: }
14727: }
14728: printf(" Successful, please wait...");
14729: while (z[0] != 'q') {
14730: /* chdir(path); */
14731: printf("\nType e to edit results with your browser, g to graph again and q for exit: ");
14732: scanf("%s",z);
14733: /* if (z[0] == 'c') system("./imach"); */
14734: if (z[0] == 'e') {
14735: #ifdef __APPLE__
14736: sprintf(pplotcmd, "open %s", optionfilehtm);
14737: #elif __linux
14738: sprintf(pplotcmd, "xdg-open %s", optionfilehtm);
14739: #else
14740: sprintf(pplotcmd, "%s", optionfilehtm);
14741: #endif
14742: printf("Starting browser with: %s",pplotcmd);fflush(stdout);
14743: system(pplotcmd);
14744: }
14745: else if (z[0] == 'g') system(plotcmd);
14746: else if (z[0] == 'q') exit(0);
14747: }
14748: end:
14749: while (z[0] != 'q') {
14750: printf("\nType q for exiting: "); fflush(stdout);
14751: scanf("%s",z);
14752: }
14753: printf("End\n");
14754: exit(0);
14755: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>