1: /* $Id: imachprax.c,v 1.1 2023/01/31 09:24:19 brouard Exp $
2: $State: Exp $
3: $Log: imachprax.c,v $
4: Revision 1.1 2023/01/31 09:24:19 brouard
5: Summary: version s1 with praxis instead of Powell for large models with age and difficulties to converge
6:
7: Revision 1.347 2022/09/18 14:36:44 brouard
8: Summary: version 0.99r42
9:
10: Revision 1.346 2022/09/16 13:52:36 brouard
11: * src/imach.c (Module): 0.99r41 Was an error when product of timevarying and fixed. Using FixedV[of name] now. Thank you Feinuo
12:
13: Revision 1.345 2022/09/16 13:40:11 brouard
14: Summary: Version 0.99r41
15:
16: * imach.c (Module): 0.99r41 Was an error when product of timevarying and fixed. Using FixedV[of name] now. Thank you Feinuo
17:
18: Revision 1.344 2022/09/14 19:33:30 brouard
19: Summary: version 0.99r40
20:
21: * imach.c (Module): Fixing names of variables in T_ (thanks to Feinuo)
22:
23: Revision 1.343 2022/09/14 14:22:16 brouard
24: Summary: version 0.99r39
25:
26: * imach.c (Module): Version 0.99r39 with colored dummy covariates
27: (fixed or time varying), using new last columns of
28: ILK_parameter.txt file.
29:
30: Revision 1.342 2022/09/11 19:54:09 brouard
31: Summary: 0.99r38
32:
33: * imach.c (Module): Adding timevarying products of any kinds,
34: should work before shifting cotvar from ncovcol+nqv columns in
35: order to have a correspondance between the column of cotvar and
36: the id of column.
37: (Module): Some cleaning and adding covariates in ILK.txt
38:
39: Revision 1.341 2022/09/11 07:58:42 brouard
40: Summary: Version 0.99r38
41:
42: After adding change in cotvar.
43:
44: Revision 1.340 2022/09/11 07:53:11 brouard
45: Summary: Version imach 0.99r37
46:
47: * imach.c (Module): Adding timevarying products of any kinds,
48: should work before shifting cotvar from ncovcol+nqv columns in
49: order to have a correspondance between the column of cotvar and
50: the id of column.
51:
52: Revision 1.339 2022/09/09 17:55:22 brouard
53: Summary: version 0.99r37
54:
55: * imach.c (Module): Many improvements for fixing products of fixed
56: timevarying as well as fixed * fixed, and test with quantitative
57: covariate.
58:
59: Revision 1.338 2022/09/04 17:40:33 brouard
60: Summary: 0.99r36
61:
62: * imach.c (Module): Now the easy runs i.e. without result or
63: model=1+age only did not work. The defautl combination should be 1
64: and not 0 because everything hasn't been tranformed yet.
65:
66: Revision 1.337 2022/09/02 14:26:02 brouard
67: Summary: version 0.99r35
68:
69: * src/imach.c: Version 0.99r35 because it outputs same results with
70: 1+age+V1+V1*age for females and 1+age for females only
71: (education=1 noweight)
72:
73: Revision 1.336 2022/08/31 09:52:36 brouard
74: *** empty log message ***
75:
76: Revision 1.335 2022/08/31 08:23:16 brouard
77: Summary: improvements...
78:
79: Revision 1.334 2022/08/25 09:08:41 brouard
80: Summary: In progress for quantitative
81:
82: Revision 1.333 2022/08/21 09:10:30 brouard
83: * src/imach.c (Module): Version 0.99r33 A lot of changes in
84: reassigning covariates: my first idea was that people will always
85: use the first covariate V1 into the model but in fact they are
86: producing data with many covariates and can use an equation model
87: with some of the covariate; it means that in a model V2+V3 instead
88: of codtabm(k,Tvaraff[j]) which calculates for combination k, for
89: three covariates (V1, V2, V3) the value of Tvaraff[j], but in fact
90: the equation model is restricted to two variables only (V2, V3)
91: and the combination for V2 should be codtabm(k,1) instead of
92: (codtabm(k,2), and the code should be
93: codtabm(k,TnsdVar[Tvaraff[j]]. Many many changes have been
94: made. All of these should be simplified once a day like we did in
95: hpxij() for example by using precov[nres] which is computed in
96: decoderesult for each nres of each resultline. Loop should be done
97: on the equation model globally by distinguishing only product with
98: age (which are changing with age) and no more on type of
99: covariates, single dummies, single covariates.
100:
101: Revision 1.332 2022/08/21 09:06:25 brouard
102: Summary: Version 0.99r33
103:
104: * src/imach.c (Module): Version 0.99r33 A lot of changes in
105: reassigning covariates: my first idea was that people will always
106: use the first covariate V1 into the model but in fact they are
107: producing data with many covariates and can use an equation model
108: with some of the covariate; it means that in a model V2+V3 instead
109: of codtabm(k,Tvaraff[j]) which calculates for combination k, for
110: three covariates (V1, V2, V3) the value of Tvaraff[j], but in fact
111: the equation model is restricted to two variables only (V2, V3)
112: and the combination for V2 should be codtabm(k,1) instead of
113: (codtabm(k,2), and the code should be
114: codtabm(k,TnsdVar[Tvaraff[j]]. Many many changes have been
115: made. All of these should be simplified once a day like we did in
116: hpxij() for example by using precov[nres] which is computed in
117: decoderesult for each nres of each resultline. Loop should be done
118: on the equation model globally by distinguishing only product with
119: age (which are changing with age) and no more on type of
120: covariates, single dummies, single covariates.
121:
122: Revision 1.331 2022/08/07 05:40:09 brouard
123: *** empty log message ***
124:
125: Revision 1.330 2022/08/06 07:18:25 brouard
126: Summary: last 0.99r31
127:
128: * imach.c (Module): Version of imach using partly decoderesult to rebuild xpxij function
129:
130: Revision 1.329 2022/08/03 17:29:54 brouard
131: * imach.c (Module): Many errors in graphs fixed with Vn*age covariates.
132:
133: Revision 1.328 2022/07/27 17:40:48 brouard
134: Summary: valgrind bug fixed by initializing to zero DummyV as well as Tage
135:
136: Revision 1.327 2022/07/27 14:47:35 brouard
137: Summary: Still a problem for one-step probabilities in case of quantitative variables
138:
139: Revision 1.326 2022/07/26 17:33:55 brouard
140: Summary: some test with nres=1
141:
142: Revision 1.325 2022/07/25 14:27:23 brouard
143: Summary: r30
144:
145: * imach.c (Module): Error cptcovn instead of nsd in bmij (was
146: coredumped, revealed by Feiuno, thank you.
147:
148: Revision 1.324 2022/07/23 17:44:26 brouard
149: *** empty log message ***
150:
151: Revision 1.323 2022/07/22 12:30:08 brouard
152: * imach.c (Module): Output of Wald test in the htm file and not only in the log.
153:
154: Revision 1.322 2022/07/22 12:27:48 brouard
155: * imach.c (Module): Output of Wald test in the htm file and not only in the log.
156:
157: Revision 1.321 2022/07/22 12:04:24 brouard
158: Summary: r28
159:
160: * imach.c (Module): Output of Wald test in the htm file and not only in the log.
161:
162: Revision 1.320 2022/06/02 05:10:11 brouard
163: *** empty log message ***
164:
165: Revision 1.319 2022/06/02 04:45:11 brouard
166: * imach.c (Module): Adding the Wald tests from the log to the main
167: htm for better display of the maximum likelihood estimators.
168:
169: Revision 1.318 2022/05/24 08:10:59 brouard
170: * imach.c (Module): Some attempts to find a bug of wrong estimates
171: of confidencce intervals with product in the equation modelC
172:
173: Revision 1.317 2022/05/15 15:06:23 brouard
174: * imach.c (Module): Some minor improvements
175:
176: Revision 1.316 2022/05/11 15:11:31 brouard
177: Summary: r27
178:
179: Revision 1.315 2022/05/11 15:06:32 brouard
180: *** empty log message ***
181:
182: Revision 1.314 2022/04/13 17:43:09 brouard
183: * imach.c (Module): Adding link to text data files
184:
185: Revision 1.313 2022/04/11 15:57:42 brouard
186: * imach.c (Module): Error in rewriting the 'r' file with yearsfproj or yearsbproj fixed
187:
188: Revision 1.312 2022/04/05 21:24:39 brouard
189: *** empty log message ***
190:
191: Revision 1.311 2022/04/05 21:03:51 brouard
192: Summary: Fixed quantitative covariates
193:
194: Fixed covariates (dummy or quantitative)
195: with missing values have never been allowed but are ERRORS and
196: program quits. Standard deviations of fixed covariates were
197: wrongly computed. Mean and standard deviations of time varying
198: covariates are still not computed.
199:
200: Revision 1.310 2022/03/17 08:45:53 brouard
201: Summary: 99r25
202:
203: Improving detection of errors: result lines should be compatible with
204: the model.
205:
206: Revision 1.309 2021/05/20 12:39:14 brouard
207: Summary: Version 0.99r24
208:
209: Revision 1.308 2021/03/31 13:11:57 brouard
210: Summary: Version 0.99r23
211:
212:
213: * imach.c (Module): Still bugs in the result loop. Thank to Holly Benett
214:
215: Revision 1.307 2021/03/08 18:11:32 brouard
216: Summary: 0.99r22 fixed bug on result:
217:
218: Revision 1.306 2021/02/20 15:44:02 brouard
219: Summary: Version 0.99r21
220:
221: * imach.c (Module): Fix bug on quitting after result lines!
222: (Module): Version 0.99r21
223:
224: Revision 1.305 2021/02/20 15:28:30 brouard
225: * imach.c (Module): Fix bug on quitting after result lines!
226:
227: Revision 1.304 2021/02/12 11:34:20 brouard
228: * imach.c (Module): The use of a Windows BOM (huge) file is now an error
229:
230: Revision 1.303 2021/02/11 19:50:15 brouard
231: * (Module): imach.c Someone entered 'results:' instead of 'result:'. Now it is an error which is printed.
232:
233: Revision 1.302 2020/02/22 21:00:05 brouard
234: * (Module): imach.c Update mle=-3 (for computing Life expectancy
235: and life table from the data without any state)
236:
237: Revision 1.301 2019/06/04 13:51:20 brouard
238: Summary: Error in 'r'parameter file backcast yearsbproj instead of yearsfproj
239:
240: Revision 1.300 2019/05/22 19:09:45 brouard
241: Summary: version 0.99r19 of May 2019
242:
243: Revision 1.299 2019/05/22 18:37:08 brouard
244: Summary: Cleaned 0.99r19
245:
246: Revision 1.298 2019/05/22 18:19:56 brouard
247: *** empty log message ***
248:
249: Revision 1.297 2019/05/22 17:56:10 brouard
250: Summary: Fix bug by moving date2dmy and nhstepm which gaefin=-1
251:
252: Revision 1.296 2019/05/20 13:03:18 brouard
253: Summary: Projection syntax simplified
254:
255:
256: We can now start projections, forward or backward, from the mean date
257: of inteviews up to or down to a number of years of projection:
258: prevforecast=1 yearsfproj=15.3 mobil_average=0
259: or
260: prevforecast=1 starting-proj-date=1/1/2007 final-proj-date=12/31/2017 mobil_average=0
261: or
262: prevbackcast=1 yearsbproj=12.3 mobil_average=1
263: or
264: prevbackcast=1 starting-back-date=1/10/1999 final-back-date=1/1/1985 mobil_average=1
265:
266: Revision 1.295 2019/05/18 09:52:50 brouard
267: Summary: doxygen tex bug
268:
269: Revision 1.294 2019/05/16 14:54:33 brouard
270: Summary: There was some wrong lines added
271:
272: Revision 1.293 2019/05/09 15:17:34 brouard
273: *** empty log message ***
274:
275: Revision 1.292 2019/05/09 14:17:20 brouard
276: Summary: Some updates
277:
278: Revision 1.291 2019/05/09 13:44:18 brouard
279: Summary: Before ncovmax
280:
281: Revision 1.290 2019/05/09 13:39:37 brouard
282: Summary: 0.99r18 unlimited number of individuals
283:
284: 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.
285:
286: Revision 1.289 2018/12/13 09:16:26 brouard
287: Summary: Bug for young ages (<-30) will be in r17
288:
289: Revision 1.288 2018/05/02 20:58:27 brouard
290: Summary: Some bugs fixed
291:
292: Revision 1.287 2018/05/01 17:57:25 brouard
293: Summary: Bug fixed by providing frequencies only for non missing covariates
294:
295: Revision 1.286 2018/04/27 14:27:04 brouard
296: Summary: some minor bugs
297:
298: Revision 1.285 2018/04/21 21:02:16 brouard
299: Summary: Some bugs fixed, valgrind tested
300:
301: Revision 1.284 2018/04/20 05:22:13 brouard
302: Summary: Computing mean and stdeviation of fixed quantitative variables
303:
304: Revision 1.283 2018/04/19 14:49:16 brouard
305: Summary: Some minor bugs fixed
306:
307: Revision 1.282 2018/02/27 22:50:02 brouard
308: *** empty log message ***
309:
310: Revision 1.281 2018/02/27 19:25:23 brouard
311: Summary: Adding second argument for quitting
312:
313: Revision 1.280 2018/02/21 07:58:13 brouard
314: Summary: 0.99r15
315:
316: New Makefile with recent VirtualBox 5.26. Bug in sqrt negatve in imach.c
317:
318: Revision 1.279 2017/07/20 13:35:01 brouard
319: Summary: temporary working
320:
321: Revision 1.278 2017/07/19 14:09:02 brouard
322: Summary: Bug for mobil_average=0 and prevforecast fixed(?)
323:
324: Revision 1.277 2017/07/17 08:53:49 brouard
325: Summary: BOM files can be read now
326:
327: Revision 1.276 2017/06/30 15:48:31 brouard
328: Summary: Graphs improvements
329:
330: Revision 1.275 2017/06/30 13:39:33 brouard
331: Summary: Saito's color
332:
333: Revision 1.274 2017/06/29 09:47:08 brouard
334: Summary: Version 0.99r14
335:
336: Revision 1.273 2017/06/27 11:06:02 brouard
337: Summary: More documentation on projections
338:
339: Revision 1.272 2017/06/27 10:22:40 brouard
340: Summary: Color of backprojection changed from 6 to 5(yellow)
341:
342: Revision 1.271 2017/06/27 10:17:50 brouard
343: Summary: Some bug with rint
344:
345: Revision 1.270 2017/05/24 05:45:29 brouard
346: *** empty log message ***
347:
348: Revision 1.269 2017/05/23 08:39:25 brouard
349: Summary: Code into subroutine, cleanings
350:
351: Revision 1.268 2017/05/18 20:09:32 brouard
352: Summary: backprojection and confidence intervals of backprevalence
353:
354: Revision 1.267 2017/05/13 10:25:05 brouard
355: Summary: temporary save for backprojection
356:
357: Revision 1.266 2017/05/13 07:26:12 brouard
358: Summary: Version 0.99r13 (improvements and bugs fixed)
359:
360: Revision 1.265 2017/04/26 16:22:11 brouard
361: Summary: imach 0.99r13 Some bugs fixed
362:
363: Revision 1.264 2017/04/26 06:01:29 brouard
364: Summary: Labels in graphs
365:
366: Revision 1.263 2017/04/24 15:23:15 brouard
367: Summary: to save
368:
369: Revision 1.262 2017/04/18 16:48:12 brouard
370: *** empty log message ***
371:
372: Revision 1.261 2017/04/05 10:14:09 brouard
373: Summary: Bug in E_ as well as in T_ fixed nres-1 vs k1-1
374:
375: Revision 1.260 2017/04/04 17:46:59 brouard
376: Summary: Gnuplot indexations fixed (humm)
377:
378: Revision 1.259 2017/04/04 13:01:16 brouard
379: Summary: Some errors to warnings only if date of death is unknown but status is death we could set to pi3
380:
381: Revision 1.258 2017/04/03 10:17:47 brouard
382: Summary: Version 0.99r12
383:
384: Some cleanings, conformed with updated documentation.
385:
386: Revision 1.257 2017/03/29 16:53:30 brouard
387: Summary: Temp
388:
389: Revision 1.256 2017/03/27 05:50:23 brouard
390: Summary: Temporary
391:
392: Revision 1.255 2017/03/08 16:02:28 brouard
393: Summary: IMaCh version 0.99r10 bugs in gnuplot fixed
394:
395: Revision 1.254 2017/03/08 07:13:00 brouard
396: Summary: Fixing data parameter line
397:
398: Revision 1.253 2016/12/15 11:59:41 brouard
399: Summary: 0.99 in progress
400:
401: Revision 1.252 2016/09/15 21:15:37 brouard
402: *** empty log message ***
403:
404: Revision 1.251 2016/09/15 15:01:13 brouard
405: Summary: not working
406:
407: Revision 1.250 2016/09/08 16:07:27 brouard
408: Summary: continue
409:
410: Revision 1.249 2016/09/07 17:14:18 brouard
411: Summary: Starting values from frequencies
412:
413: Revision 1.248 2016/09/07 14:10:18 brouard
414: *** empty log message ***
415:
416: Revision 1.247 2016/09/02 11:11:21 brouard
417: *** empty log message ***
418:
419: Revision 1.246 2016/09/02 08:49:22 brouard
420: *** empty log message ***
421:
422: Revision 1.245 2016/09/02 07:25:01 brouard
423: *** empty log message ***
424:
425: Revision 1.244 2016/09/02 07:17:34 brouard
426: *** empty log message ***
427:
428: Revision 1.243 2016/09/02 06:45:35 brouard
429: *** empty log message ***
430:
431: Revision 1.242 2016/08/30 15:01:20 brouard
432: Summary: Fixing a lots
433:
434: Revision 1.241 2016/08/29 17:17:25 brouard
435: Summary: gnuplot problem in Back projection to fix
436:
437: Revision 1.240 2016/08/29 07:53:18 brouard
438: Summary: Better
439:
440: Revision 1.239 2016/08/26 15:51:03 brouard
441: Summary: Improvement in Powell output in order to copy and paste
442:
443: Author:
444:
445: Revision 1.238 2016/08/26 14:23:35 brouard
446: Summary: Starting tests of 0.99
447:
448: Revision 1.237 2016/08/26 09:20:19 brouard
449: Summary: to valgrind
450:
451: Revision 1.236 2016/08/25 10:50:18 brouard
452: *** empty log message ***
453:
454: Revision 1.235 2016/08/25 06:59:23 brouard
455: *** empty log message ***
456:
457: Revision 1.234 2016/08/23 16:51:20 brouard
458: *** empty log message ***
459:
460: Revision 1.233 2016/08/23 07:40:50 brouard
461: Summary: not working
462:
463: Revision 1.232 2016/08/22 14:20:21 brouard
464: Summary: not working
465:
466: Revision 1.231 2016/08/22 07:17:15 brouard
467: Summary: not working
468:
469: Revision 1.230 2016/08/22 06:55:53 brouard
470: Summary: Not working
471:
472: Revision 1.229 2016/07/23 09:45:53 brouard
473: Summary: Completing for func too
474:
475: Revision 1.228 2016/07/22 17:45:30 brouard
476: Summary: Fixing some arrays, still debugging
477:
478: Revision 1.226 2016/07/12 18:42:34 brouard
479: Summary: temp
480:
481: Revision 1.225 2016/07/12 08:40:03 brouard
482: Summary: saving but not running
483:
484: Revision 1.224 2016/07/01 13:16:01 brouard
485: Summary: Fixes
486:
487: Revision 1.223 2016/02/19 09:23:35 brouard
488: Summary: temporary
489:
490: Revision 1.222 2016/02/17 08:14:50 brouard
491: Summary: Probably last 0.98 stable version 0.98r6
492:
493: Revision 1.221 2016/02/15 23:35:36 brouard
494: Summary: minor bug
495:
496: Revision 1.219 2016/02/15 00:48:12 brouard
497: *** empty log message ***
498:
499: Revision 1.218 2016/02/12 11:29:23 brouard
500: Summary: 0.99 Back projections
501:
502: Revision 1.217 2015/12/23 17:18:31 brouard
503: Summary: Experimental backcast
504:
505: Revision 1.216 2015/12/18 17:32:11 brouard
506: Summary: 0.98r4 Warning and status=-2
507:
508: Version 0.98r4 is now:
509: - displaying an error when status is -1, date of interview unknown and date of death known;
510: - permitting a status -2 when the vital status is unknown at a known date of right truncation.
511: Older changes concerning s=-2, dating from 2005 have been supersed.
512:
513: Revision 1.215 2015/12/16 08:52:24 brouard
514: Summary: 0.98r4 working
515:
516: Revision 1.214 2015/12/16 06:57:54 brouard
517: Summary: temporary not working
518:
519: Revision 1.213 2015/12/11 18:22:17 brouard
520: Summary: 0.98r4
521:
522: Revision 1.212 2015/11/21 12:47:24 brouard
523: Summary: minor typo
524:
525: Revision 1.211 2015/11/21 12:41:11 brouard
526: Summary: 0.98r3 with some graph of projected cross-sectional
527:
528: Author: Nicolas Brouard
529:
530: Revision 1.210 2015/11/18 17:41:20 brouard
531: Summary: Start working on projected prevalences Revision 1.209 2015/11/17 22:12:03 brouard
532: Summary: Adding ftolpl parameter
533: Author: N Brouard
534:
535: We had difficulties to get smoothed confidence intervals. It was due
536: to the period prevalence which wasn't computed accurately. The inner
537: parameter ftolpl is now an outer parameter of the .imach parameter
538: file after estepm. If ftolpl is small 1.e-4 and estepm too,
539: computation are long.
540:
541: Revision 1.208 2015/11/17 14:31:57 brouard
542: Summary: temporary
543:
544: Revision 1.207 2015/10/27 17:36:57 brouard
545: *** empty log message ***
546:
547: Revision 1.206 2015/10/24 07:14:11 brouard
548: *** empty log message ***
549:
550: Revision 1.205 2015/10/23 15:50:53 brouard
551: Summary: 0.98r3 some clarification for graphs on likelihood contributions
552:
553: Revision 1.204 2015/10/01 16:20:26 brouard
554: Summary: Some new graphs of contribution to likelihood
555:
556: Revision 1.203 2015/09/30 17:45:14 brouard
557: Summary: looking at better estimation of the hessian
558:
559: Also a better criteria for convergence to the period prevalence And
560: therefore adding the number of years needed to converge. (The
561: prevalence in any alive state shold sum to one
562:
563: Revision 1.202 2015/09/22 19:45:16 brouard
564: Summary: Adding some overall graph on contribution to likelihood. Might change
565:
566: Revision 1.201 2015/09/15 17:34:58 brouard
567: Summary: 0.98r0
568:
569: - Some new graphs like suvival functions
570: - Some bugs fixed like model=1+age+V2.
571:
572: Revision 1.200 2015/09/09 16:53:55 brouard
573: Summary: Big bug thanks to Flavia
574:
575: Even model=1+age+V2. did not work anymore
576:
577: Revision 1.199 2015/09/07 14:09:23 brouard
578: Summary: 0.98q6 changing default small png format for graph to vectorized svg.
579:
580: Revision 1.198 2015/09/03 07:14:39 brouard
581: Summary: 0.98q5 Flavia
582:
583: Revision 1.197 2015/09/01 18:24:39 brouard
584: *** empty log message ***
585:
586: Revision 1.196 2015/08/18 23:17:52 brouard
587: Summary: 0.98q5
588:
589: Revision 1.195 2015/08/18 16:28:39 brouard
590: Summary: Adding a hack for testing purpose
591:
592: After reading the title, ftol and model lines, if the comment line has
593: a q, starting with #q, the answer at the end of the run is quit. It
594: permits to run test files in batch with ctest. The former workaround was
595: $ echo q | imach foo.imach
596:
597: Revision 1.194 2015/08/18 13:32:00 brouard
598: Summary: Adding error when the covariance matrix doesn't contain the exact number of lines required by the model line.
599:
600: Revision 1.193 2015/08/04 07:17:42 brouard
601: Summary: 0.98q4
602:
603: Revision 1.192 2015/07/16 16:49:02 brouard
604: Summary: Fixing some outputs
605:
606: Revision 1.191 2015/07/14 10:00:33 brouard
607: Summary: Some fixes
608:
609: Revision 1.190 2015/05/05 08:51:13 brouard
610: Summary: Adding digits in output parameters (7 digits instead of 6)
611:
612: Fix 1+age+.
613:
614: Revision 1.189 2015/04/30 14:45:16 brouard
615: Summary: 0.98q2
616:
617: Revision 1.188 2015/04/30 08:27:53 brouard
618: *** empty log message ***
619:
620: Revision 1.187 2015/04/29 09:11:15 brouard
621: *** empty log message ***
622:
623: Revision 1.186 2015/04/23 12:01:52 brouard
624: Summary: V1*age is working now, version 0.98q1
625:
626: Some codes had been disabled in order to simplify and Vn*age was
627: working in the optimization phase, ie, giving correct MLE parameters,
628: but, as usual, outputs were not correct and program core dumped.
629:
630: Revision 1.185 2015/03/11 13:26:42 brouard
631: Summary: Inclusion of compile and links command line for Intel Compiler
632:
633: Revision 1.184 2015/03/11 11:52:39 brouard
634: Summary: Back from Windows 8. Intel Compiler
635:
636: Revision 1.183 2015/03/10 20:34:32 brouard
637: Summary: 0.98q0, trying with directest, mnbrak fixed
638:
639: We use directest instead of original Powell test; probably no
640: incidence on the results, but better justifications;
641: We fixed Numerical Recipes mnbrak routine which was wrong and gave
642: wrong results.
643:
644: Revision 1.182 2015/02/12 08:19:57 brouard
645: Summary: Trying to keep directest which seems simpler and more general
646: Author: Nicolas Brouard
647:
648: Revision 1.181 2015/02/11 23:22:24 brouard
649: Summary: Comments on Powell added
650:
651: Author:
652:
653: Revision 1.180 2015/02/11 17:33:45 brouard
654: Summary: Finishing move from main to function (hpijx and prevalence_limit)
655:
656: Revision 1.179 2015/01/04 09:57:06 brouard
657: Summary: back to OS/X
658:
659: Revision 1.178 2015/01/04 09:35:48 brouard
660: *** empty log message ***
661:
662: Revision 1.177 2015/01/03 18:40:56 brouard
663: Summary: Still testing ilc32 on OSX
664:
665: Revision 1.176 2015/01/03 16:45:04 brouard
666: *** empty log message ***
667:
668: Revision 1.175 2015/01/03 16:33:42 brouard
669: *** empty log message ***
670:
671: Revision 1.174 2015/01/03 16:15:49 brouard
672: Summary: Still in cross-compilation
673:
674: Revision 1.173 2015/01/03 12:06:26 brouard
675: Summary: trying to detect cross-compilation
676:
677: Revision 1.172 2014/12/27 12:07:47 brouard
678: Summary: Back from Visual Studio and Intel, options for compiling for Windows XP
679:
680: Revision 1.171 2014/12/23 13:26:59 brouard
681: Summary: Back from Visual C
682:
683: Still problem with utsname.h on Windows
684:
685: Revision 1.170 2014/12/23 11:17:12 brouard
686: Summary: Cleaning some \%% back to %%
687:
688: The escape was mandatory for a specific compiler (which one?), but too many warnings.
689:
690: Revision 1.169 2014/12/22 23:08:31 brouard
691: Summary: 0.98p
692:
693: Outputs some informations on compiler used, OS etc. Testing on different platforms.
694:
695: Revision 1.168 2014/12/22 15:17:42 brouard
696: Summary: update
697:
698: Revision 1.167 2014/12/22 13:50:56 brouard
699: Summary: Testing uname and compiler version and if compiled 32 or 64
700:
701: Testing on Linux 64
702:
703: Revision 1.166 2014/12/22 11:40:47 brouard
704: *** empty log message ***
705:
706: Revision 1.165 2014/12/16 11:20:36 brouard
707: Summary: After compiling on Visual C
708:
709: * imach.c (Module): Merging 1.61 to 1.162
710:
711: Revision 1.164 2014/12/16 10:52:11 brouard
712: Summary: Merging with Visual C after suppressing some warnings for unused variables. Also fixing Saito's bug 0.98Xn
713:
714: * imach.c (Module): Merging 1.61 to 1.162
715:
716: Revision 1.163 2014/12/16 10:30:11 brouard
717: * imach.c (Module): Merging 1.61 to 1.162
718:
719: Revision 1.162 2014/09/25 11:43:39 brouard
720: Summary: temporary backup 0.99!
721:
722: Revision 1.1 2014/09/16 11:06:58 brouard
723: Summary: With some code (wrong) for nlopt
724:
725: Author:
726:
727: Revision 1.161 2014/09/15 20:41:41 brouard
728: Summary: Problem with macro SQR on Intel compiler
729:
730: Revision 1.160 2014/09/02 09:24:05 brouard
731: *** empty log message ***
732:
733: Revision 1.159 2014/09/01 10:34:10 brouard
734: Summary: WIN32
735: Author: Brouard
736:
737: Revision 1.158 2014/08/27 17:11:51 brouard
738: *** empty log message ***
739:
740: Revision 1.157 2014/08/27 16:26:55 brouard
741: Summary: Preparing windows Visual studio version
742: Author: Brouard
743:
744: In order to compile on Visual studio, time.h is now correct and time_t
745: and tm struct should be used. difftime should be used but sometimes I
746: just make the differences in raw time format (time(&now).
747: Trying to suppress #ifdef LINUX
748: Add xdg-open for __linux in order to open default browser.
749:
750: Revision 1.156 2014/08/25 20:10:10 brouard
751: *** empty log message ***
752:
753: Revision 1.155 2014/08/25 18:32:34 brouard
754: Summary: New compile, minor changes
755: Author: Brouard
756:
757: Revision 1.154 2014/06/20 17:32:08 brouard
758: Summary: Outputs now all graphs of convergence to period prevalence
759:
760: Revision 1.153 2014/06/20 16:45:46 brouard
761: Summary: If 3 live state, convergence to period prevalence on same graph
762: Author: Brouard
763:
764: Revision 1.152 2014/06/18 17:54:09 brouard
765: Summary: open browser, use gnuplot on same dir than imach if not found in the path
766:
767: Revision 1.151 2014/06/18 16:43:30 brouard
768: *** empty log message ***
769:
770: Revision 1.150 2014/06/18 16:42:35 brouard
771: Summary: If gnuplot is not in the path try on same directory than imach binary (OSX)
772: Author: brouard
773:
774: Revision 1.149 2014/06/18 15:51:14 brouard
775: Summary: Some fixes in parameter files errors
776: Author: Nicolas Brouard
777:
778: Revision 1.148 2014/06/17 17:38:48 brouard
779: Summary: Nothing new
780: Author: Brouard
781:
782: Just a new packaging for OS/X version 0.98nS
783:
784: Revision 1.147 2014/06/16 10:33:11 brouard
785: *** empty log message ***
786:
787: Revision 1.146 2014/06/16 10:20:28 brouard
788: Summary: Merge
789: Author: Brouard
790:
791: Merge, before building revised version.
792:
793: Revision 1.145 2014/06/10 21:23:15 brouard
794: Summary: Debugging with valgrind
795: Author: Nicolas Brouard
796:
797: Lot of changes in order to output the results with some covariates
798: After the Edimburgh REVES conference 2014, it seems mandatory to
799: improve the code.
800: No more memory valgrind error but a lot has to be done in order to
801: continue the work of splitting the code into subroutines.
802: Also, decodemodel has been improved. Tricode is still not
803: optimal. nbcode should be improved. Documentation has been added in
804: the source code.
805:
806: Revision 1.143 2014/01/26 09:45:38 brouard
807: Summary: Version 0.98nR (to be improved, but gives same optimization results as 0.98k. Nice, promising
808:
809: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
810: (Module): Version 0.98nR Running ok, but output format still only works for three covariates.
811:
812: Revision 1.142 2014/01/26 03:57:36 brouard
813: Summary: gnuplot changed plot w l 1 has to be changed to plot w l lt 2
814:
815: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
816:
817: Revision 1.141 2014/01/26 02:42:01 brouard
818: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
819:
820: Revision 1.140 2011/09/02 10:37:54 brouard
821: Summary: times.h is ok with mingw32 now.
822:
823: Revision 1.139 2010/06/14 07:50:17 brouard
824: After the theft of my laptop, I probably lost some lines of codes which were not uploaded to the CVS tree.
825: I remember having already fixed agemin agemax which are pointers now but not cvs saved.
826:
827: Revision 1.138 2010/04/30 18:19:40 brouard
828: *** empty log message ***
829:
830: Revision 1.137 2010/04/29 18:11:38 brouard
831: (Module): Checking covariates for more complex models
832: than V1+V2. A lot of change to be done. Unstable.
833:
834: Revision 1.136 2010/04/26 20:30:53 brouard
835: (Module): merging some libgsl code. Fixing computation
836: of likelione (using inter/intrapolation if mle = 0) in order to
837: get same likelihood as if mle=1.
838: Some cleaning of code and comments added.
839:
840: Revision 1.135 2009/10/29 15:33:14 brouard
841: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
842:
843: Revision 1.134 2009/10/29 13:18:53 brouard
844: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
845:
846: Revision 1.133 2009/07/06 10:21:25 brouard
847: just nforces
848:
849: Revision 1.132 2009/07/06 08:22:05 brouard
850: Many tings
851:
852: Revision 1.131 2009/06/20 16:22:47 brouard
853: Some dimensions resccaled
854:
855: Revision 1.130 2009/05/26 06:44:34 brouard
856: (Module): Max Covariate is now set to 20 instead of 8. A
857: lot of cleaning with variables initialized to 0. Trying to make
858: V2+V3*age+V1+V4 strb=V3*age+V1+V4 working better.
859:
860: Revision 1.129 2007/08/31 13:49:27 lievre
861: Modification of the way of exiting when the covariate is not binary in order to see on the window the error message before exiting
862:
863: Revision 1.128 2006/06/30 13:02:05 brouard
864: (Module): Clarifications on computing e.j
865:
866: Revision 1.127 2006/04/28 18:11:50 brouard
867: (Module): Yes the sum of survivors was wrong since
868: imach-114 because nhstepm was no more computed in the age
869: loop. Now we define nhstepma in the age loop.
870: (Module): In order to speed up (in case of numerous covariates) we
871: compute health expectancies (without variances) in a first step
872: and then all the health expectancies with variances or standard
873: deviation (needs data from the Hessian matrices) which slows the
874: computation.
875: In the future we should be able to stop the program is only health
876: expectancies and graph are needed without standard deviations.
877:
878: Revision 1.126 2006/04/28 17:23:28 brouard
879: (Module): Yes the sum of survivors was wrong since
880: imach-114 because nhstepm was no more computed in the age
881: loop. Now we define nhstepma in the age loop.
882: Version 0.98h
883:
884: Revision 1.125 2006/04/04 15:20:31 lievre
885: Errors in calculation of health expectancies. Age was not initialized.
886: Forecasting file added.
887:
888: Revision 1.124 2006/03/22 17:13:53 lievre
889: Parameters are printed with %lf instead of %f (more numbers after the comma).
890: The log-likelihood is printed in the log file
891:
892: Revision 1.123 2006/03/20 10:52:43 brouard
893: * imach.c (Module): <title> changed, corresponds to .htm file
894: name. <head> headers where missing.
895:
896: * imach.c (Module): Weights can have a decimal point as for
897: English (a comma might work with a correct LC_NUMERIC environment,
898: otherwise the weight is truncated).
899: Modification of warning when the covariates values are not 0 or
900: 1.
901: Version 0.98g
902:
903: Revision 1.122 2006/03/20 09:45:41 brouard
904: (Module): Weights can have a decimal point as for
905: English (a comma might work with a correct LC_NUMERIC environment,
906: otherwise the weight is truncated).
907: Modification of warning when the covariates values are not 0 or
908: 1.
909: Version 0.98g
910:
911: Revision 1.121 2006/03/16 17:45:01 lievre
912: * imach.c (Module): Comments concerning covariates added
913:
914: * imach.c (Module): refinements in the computation of lli if
915: status=-2 in order to have more reliable computation if stepm is
916: not 1 month. Version 0.98f
917:
918: Revision 1.120 2006/03/16 15:10:38 lievre
919: (Module): refinements in the computation of lli if
920: status=-2 in order to have more reliable computation if stepm is
921: not 1 month. Version 0.98f
922:
923: Revision 1.119 2006/03/15 17:42:26 brouard
924: (Module): Bug if status = -2, the loglikelihood was
925: computed as likelihood omitting the logarithm. Version O.98e
926:
927: Revision 1.118 2006/03/14 18:20:07 brouard
928: (Module): varevsij Comments added explaining the second
929: table of variances if popbased=1 .
930: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
931: (Module): Function pstamp added
932: (Module): Version 0.98d
933:
934: Revision 1.117 2006/03/14 17:16:22 brouard
935: (Module): varevsij Comments added explaining the second
936: table of variances if popbased=1 .
937: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
938: (Module): Function pstamp added
939: (Module): Version 0.98d
940:
941: Revision 1.116 2006/03/06 10:29:27 brouard
942: (Module): Variance-covariance wrong links and
943: varian-covariance of ej. is needed (Saito).
944:
945: Revision 1.115 2006/02/27 12:17:45 brouard
946: (Module): One freematrix added in mlikeli! 0.98c
947:
948: Revision 1.114 2006/02/26 12:57:58 brouard
949: (Module): Some improvements in processing parameter
950: filename with strsep.
951:
952: Revision 1.113 2006/02/24 14:20:24 brouard
953: (Module): Memory leaks checks with valgrind and:
954: datafile was not closed, some imatrix were not freed and on matrix
955: allocation too.
956:
957: Revision 1.112 2006/01/30 09:55:26 brouard
958: (Module): Back to gnuplot.exe instead of wgnuplot.exe
959:
960: Revision 1.111 2006/01/25 20:38:18 brouard
961: (Module): Lots of cleaning and bugs added (Gompertz)
962: (Module): Comments can be added in data file. Missing date values
963: can be a simple dot '.'.
964:
965: Revision 1.110 2006/01/25 00:51:50 brouard
966: (Module): Lots of cleaning and bugs added (Gompertz)
967:
968: Revision 1.109 2006/01/24 19:37:15 brouard
969: (Module): Comments (lines starting with a #) are allowed in data.
970:
971: Revision 1.108 2006/01/19 18:05:42 lievre
972: Gnuplot problem appeared...
973: To be fixed
974:
975: Revision 1.107 2006/01/19 16:20:37 brouard
976: Test existence of gnuplot in imach path
977:
978: Revision 1.106 2006/01/19 13:24:36 brouard
979: Some cleaning and links added in html output
980:
981: Revision 1.105 2006/01/05 20:23:19 lievre
982: *** empty log message ***
983:
984: Revision 1.104 2005/09/30 16:11:43 lievre
985: (Module): sump fixed, loop imx fixed, and simplifications.
986: (Module): If the status is missing at the last wave but we know
987: that the person is alive, then we can code his/her status as -2
988: (instead of missing=-1 in earlier versions) and his/her
989: contributions to the likelihood is 1 - Prob of dying from last
990: health status (= 1-p13= p11+p12 in the easiest case of somebody in
991: the healthy state at last known wave). Version is 0.98
992:
993: Revision 1.103 2005/09/30 15:54:49 lievre
994: (Module): sump fixed, loop imx fixed, and simplifications.
995:
996: Revision 1.102 2004/09/15 17:31:30 brouard
997: Add the possibility to read data file including tab characters.
998:
999: Revision 1.101 2004/09/15 10:38:38 brouard
1000: Fix on curr_time
1001:
1002: Revision 1.100 2004/07/12 18:29:06 brouard
1003: Add version for Mac OS X. Just define UNIX in Makefile
1004:
1005: Revision 1.99 2004/06/05 08:57:40 brouard
1006: *** empty log message ***
1007:
1008: Revision 1.98 2004/05/16 15:05:56 brouard
1009: New version 0.97 . First attempt to estimate force of mortality
1010: directly from the data i.e. without the need of knowing the health
1011: state at each age, but using a Gompertz model: log u =a + b*age .
1012: This is the basic analysis of mortality and should be done before any
1013: other analysis, in order to test if the mortality estimated from the
1014: cross-longitudinal survey is different from the mortality estimated
1015: from other sources like vital statistic data.
1016:
1017: The same imach parameter file can be used but the option for mle should be -3.
1018:
1019: Agnès, who wrote this part of the code, tried to keep most of the
1020: former routines in order to include the new code within the former code.
1021:
1022: The output is very simple: only an estimate of the intercept and of
1023: the slope with 95% confident intervals.
1024:
1025: Current limitations:
1026: A) Even if you enter covariates, i.e. with the
1027: model= V1+V2 equation for example, the programm does only estimate a unique global model without covariates.
1028: B) There is no computation of Life Expectancy nor Life Table.
1029:
1030: Revision 1.97 2004/02/20 13:25:42 lievre
1031: Version 0.96d. Population forecasting command line is (temporarily)
1032: suppressed.
1033:
1034: Revision 1.96 2003/07/15 15:38:55 brouard
1035: * imach.c (Repository): Errors in subdirf, 2, 3 while printing tmpout is
1036: rewritten within the same printf. Workaround: many printfs.
1037:
1038: Revision 1.95 2003/07/08 07:54:34 brouard
1039: * imach.c (Repository):
1040: (Repository): Using imachwizard code to output a more meaningful covariance
1041: matrix (cov(a12,c31) instead of numbers.
1042:
1043: Revision 1.94 2003/06/27 13:00:02 brouard
1044: Just cleaning
1045:
1046: Revision 1.93 2003/06/25 16:33:55 brouard
1047: (Module): On windows (cygwin) function asctime_r doesn't
1048: exist so I changed back to asctime which exists.
1049: (Module): Version 0.96b
1050:
1051: Revision 1.92 2003/06/25 16:30:45 brouard
1052: (Module): On windows (cygwin) function asctime_r doesn't
1053: exist so I changed back to asctime which exists.
1054:
1055: Revision 1.91 2003/06/25 15:30:29 brouard
1056: * imach.c (Repository): Duplicated warning errors corrected.
1057: (Repository): Elapsed time after each iteration is now output. It
1058: helps to forecast when convergence will be reached. Elapsed time
1059: is stamped in powell. We created a new html file for the graphs
1060: concerning matrix of covariance. It has extension -cov.htm.
1061:
1062: Revision 1.90 2003/06/24 12:34:15 brouard
1063: (Module): Some bugs corrected for windows. Also, when
1064: mle=-1 a template is output in file "or"mypar.txt with the design
1065: of the covariance matrix to be input.
1066:
1067: Revision 1.89 2003/06/24 12:30:52 brouard
1068: (Module): Some bugs corrected for windows. Also, when
1069: mle=-1 a template is output in file "or"mypar.txt with the design
1070: of the covariance matrix to be input.
1071:
1072: Revision 1.88 2003/06/23 17:54:56 brouard
1073: * 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.
1074:
1075: Revision 1.87 2003/06/18 12:26:01 brouard
1076: Version 0.96
1077:
1078: Revision 1.86 2003/06/17 20:04:08 brouard
1079: (Module): Change position of html and gnuplot routines and added
1080: routine fileappend.
1081:
1082: Revision 1.85 2003/06/17 13:12:43 brouard
1083: * imach.c (Repository): Check when date of death was earlier that
1084: current date of interview. It may happen when the death was just
1085: prior to the death. In this case, dh was negative and likelihood
1086: was wrong (infinity). We still send an "Error" but patch by
1087: assuming that the date of death was just one stepm after the
1088: interview.
1089: (Repository): Because some people have very long ID (first column)
1090: we changed int to long in num[] and we added a new lvector for
1091: memory allocation. But we also truncated to 8 characters (left
1092: truncation)
1093: (Repository): No more line truncation errors.
1094:
1095: Revision 1.84 2003/06/13 21:44:43 brouard
1096: * imach.c (Repository): Replace "freqsummary" at a correct
1097: place. It differs from routine "prevalence" which may be called
1098: many times. Probs is memory consuming and must be used with
1099: parcimony.
1100: Version 0.95a3 (should output exactly the same maximization than 0.8a2)
1101:
1102: Revision 1.83 2003/06/10 13:39:11 lievre
1103: *** empty log message ***
1104:
1105: Revision 1.82 2003/06/05 15:57:20 brouard
1106: Add log in imach.c and fullversion number is now printed.
1107:
1108: */
1109: /*
1110: Interpolated Markov Chain
1111:
1112: Short summary of the programme:
1113:
1114: This program computes Healthy Life Expectancies or State-specific
1115: (if states aren't health statuses) Expectancies from
1116: cross-longitudinal data. Cross-longitudinal data consist in:
1117:
1118: -1- a first survey ("cross") where individuals from different ages
1119: are interviewed on their health status or degree of disability (in
1120: the case of a health survey which is our main interest)
1121:
1122: -2- at least a second wave of interviews ("longitudinal") which
1123: measure each change (if any) in individual health status. Health
1124: expectancies are computed from the time spent in each health state
1125: according to a model. More health states you consider, more time is
1126: necessary to reach the Maximum Likelihood of the parameters involved
1127: in the model. The simplest model is the multinomial logistic model
1128: where pij is the probability to be observed in state j at the second
1129: wave conditional to be observed in state i at the first
1130: wave. Therefore the model is: log(pij/pii)= aij + bij*age+ cij*sex +
1131: etc , where 'age' is age and 'sex' is a covariate. If you want to
1132: have a more complex model than "constant and age", you should modify
1133: the program where the markup *Covariates have to be included here
1134: again* invites you to do it. More covariates you add, slower the
1135: convergence.
1136:
1137: The advantage of this computer programme, compared to a simple
1138: multinomial logistic model, is clear when the delay between waves is not
1139: identical for each individual. Also, if a individual missed an
1140: intermediate interview, the information is lost, but taken into
1141: account using an interpolation or extrapolation.
1142:
1143: hPijx is the probability to be observed in state i at age x+h
1144: conditional to the observed state i at age x. The delay 'h' can be
1145: split into an exact number (nh*stepm) of unobserved intermediate
1146: states. This elementary transition (by month, quarter,
1147: semester or year) is modelled as a multinomial logistic. The hPx
1148: matrix is simply the matrix product of nh*stepm elementary matrices
1149: and the contribution of each individual to the likelihood is simply
1150: hPijx.
1151:
1152: Also this programme outputs the covariance matrix of the parameters but also
1153: of the life expectancies. It also computes the period (stable) prevalence.
1154:
1155: Back prevalence and projections:
1156:
1157: - back_prevalence_limit(double *p, double **bprlim, double ageminpar,
1158: double agemaxpar, double ftolpl, int *ncvyearp, double
1159: dateprev1,double dateprev2, int firstpass, int lastpass, int
1160: mobilavproj)
1161:
1162: Computes the back prevalence limit for any combination of
1163: covariate values k at any age between ageminpar and agemaxpar and
1164: returns it in **bprlim. In the loops,
1165:
1166: - **bprevalim(**bprlim, ***mobaverage, nlstate, *p, age, **oldm,
1167: **savm, **dnewm, **doldm, **dsavm, ftolpl, ncvyearp, k);
1168:
1169: - hBijx Back Probability to be in state i at age x-h being in j at x
1170: Computes for any combination of covariates k and any age between bage and fage
1171: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
1172: oldm=oldms;savm=savms;
1173:
1174: - hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k, nres);
1175: Computes the transition matrix starting at age 'age' over
1176: 'nhstepm*hstepm*stepm' months (i.e. until
1177: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
1178: nhstepm*hstepm matrices.
1179:
1180: Returns p3mat[i][j][h] after calling
1181: p3mat[i][j][h]=matprod2(newm,
1182: bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm,
1183: dsavm,ij),\ 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
1184: oldm);
1185:
1186: Important routines
1187:
1188: - func (or funcone), computes logit (pij) distinguishing
1189: o fixed variables (single or product dummies or quantitative);
1190: o varying variables by:
1191: (1) wave (single, product dummies, quantitative),
1192: (2) by age (can be month) age (done), age*age (done), age*Vn where Vn can be:
1193: % fixed dummy (treated) or quantitative (not done because time-consuming);
1194: % varying dummy (not done) or quantitative (not done);
1195: - Tricode which tests the modality of dummy variables (in order to warn with wrong or empty modalities)
1196: and returns the number of efficient covariates cptcoveff and modalities nbcode[Tvar[k]][1]= 0 and nbcode[Tvar[k]][2]= 1 usually.
1197: - printinghtml which outputs results like life expectancy in and from a state for a combination of modalities of dummy variables
1198: o There are 2**cptcoveff combinations of (0,1) for cptcoveff variables. Outputting only combinations with people, éliminating 1 1 if
1199: race White (0 0), Black vs White (1 0), Hispanic (0 1) and 1 1 being meaningless.
1200:
1201:
1202:
1203: Authors: Nicolas Brouard (brouard@ined.fr) and Agnès Lièvre (lievre@ined.fr).
1204: Institut national d'études démographiques, Paris.
1205: This software have been partly granted by Euro-REVES, a concerted action
1206: from the European Union.
1207: It is copyrighted identically to a GNU software product, ie programme and
1208: software can be distributed freely for non commercial use. Latest version
1209: can be accessed at http://euroreves.ined.fr/imach .
1210:
1211: Help to debug: LD_PRELOAD=/usr/local/lib/libnjamd.so ./imach foo.imach
1212: or better on gdb : set env LD_PRELOAD=/usr/local/lib/libnjamd.so
1213:
1214: **********************************************************************/
1215: /*
1216: main
1217: read parameterfile
1218: read datafile
1219: concatwav
1220: freqsummary
1221: if (mle >= 1)
1222: mlikeli
1223: print results files
1224: if mle==1
1225: computes hessian
1226: read end of parameter file: agemin, agemax, bage, fage, estepm
1227: begin-prev-date,...
1228: open gnuplot file
1229: open html file
1230: period (stable) prevalence | pl_nom 1-1 2-2 etc by covariate
1231: for age prevalim() | #****** V1=0 V2=1 V3=1 V4=0 ******
1232: | 65 1 0 2 1 3 1 4 0 0.96326 0.03674
1233: freexexit2 possible for memory heap.
1234:
1235: h Pij x | pij_nom ficrestpij
1236: # Cov Agex agex+h hpijx with i,j= 1-1 1-2 1-3 2-1 2-2 2-3
1237: 1 85 85 1.00000 0.00000 0.00000 0.00000 1.00000 0.00000
1238: 1 85 86 0.68299 0.22291 0.09410 0.71093 0.00000 0.28907
1239:
1240: 1 65 99 0.00364 0.00322 0.99314 0.00350 0.00310 0.99340
1241: 1 65 100 0.00214 0.00204 0.99581 0.00206 0.00196 0.99597
1242: variance of p one-step probabilities varprob | prob_nom ficresprob #One-step probabilities and stand. devi in ()
1243: Standard deviation of one-step probabilities | probcor_nom ficresprobcor #One-step probabilities and correlation matrix
1244: Matrix of variance covariance of one-step probabilities | probcov_nom ficresprobcov #One-step probabilities and covariance matrix
1245:
1246: forecasting if prevfcast==1 prevforecast call prevalence()
1247: health expectancies
1248: Variance-covariance of DFLE
1249: prevalence()
1250: movingaverage()
1251: varevsij()
1252: if popbased==1 varevsij(,popbased)
1253: total life expectancies
1254: Variance of period (stable) prevalence
1255: end
1256: */
1257:
1258: /* #define DEBUG */
1259: /* #define DEBUGBRENT */
1260: /* #define DEBUGLINMIN */
1261: /* #define DEBUGHESS */
1262: #define DEBUGHESSIJ
1263: /* #define LINMINORIGINAL /\* Don't use loop on scale in linmin (accepting nan) *\/ */
1264: #define POWELL /* Instead of NLOPT */
1265: #define POWELLNOF3INFF1TEST /* Skip test */
1266: /* #define POWELLORIGINAL /\* Don't use Directest to decide new direction but original Powell test *\/ */
1267: /* #define MNBRAKORIGINAL /\* Don't use mnbrak fix *\/ */
1268: /* #define FLATSUP *//* Suppresses directions where likelihood is flat */
1269:
1270: #include <math.h>
1271: #include <stdio.h>
1272: #include <stdlib.h>
1273: #include <string.h>
1274: #include <ctype.h>
1275:
1276: #ifdef _WIN32
1277: #include <io.h>
1278: #include <windows.h>
1279: #include <tchar.h>
1280: #else
1281: #include <unistd.h>
1282: #endif
1283:
1284: #include <limits.h>
1285: #include <sys/types.h>
1286:
1287: #if defined(__GNUC__)
1288: #include <sys/utsname.h> /* Doesn't work on Windows */
1289: #endif
1290:
1291: #include <sys/stat.h>
1292: #include <errno.h>
1293: /* extern int errno; */
1294:
1295: /* #ifdef LINUX */
1296: /* #include <time.h> */
1297: /* #include "timeval.h" */
1298: /* #else */
1299: /* #include <sys/time.h> */
1300: /* #endif */
1301:
1302: #include <time.h>
1303:
1304: #ifdef GSL
1305: #include <gsl/gsl_errno.h>
1306: #include <gsl/gsl_multimin.h>
1307: #endif
1308:
1309:
1310: #ifdef NLOPT
1311: #include <nlopt.h>
1312: typedef struct {
1313: double (* function)(double [] );
1314: } myfunc_data ;
1315: #endif
1316:
1317: /* #include <libintl.h> */
1318: /* #define _(String) gettext (String) */
1319:
1320: #define MAXLINE 16384 /* Was 256 and 1024 and 2048. Overflow with 312 with 2 states and 4 covariates. Should be ok */
1321:
1322: #define GNUPLOTPROGRAM "gnuplot"
1323: #define GNUPLOTVERSION 5.1
1324: double gnuplotversion=GNUPLOTVERSION;
1325: /*#define GNUPLOTPROGRAM "..\\gp37mgw\\wgnuplot"*/
1326: #define FILENAMELENGTH 256
1327:
1328: #define GLOCK_ERROR_NOPATH -1 /* empty path */
1329: #define GLOCK_ERROR_GETCWD -2 /* cannot get cwd */
1330:
1331: #define MAXPARM 216 /**< Maximum number of parameters for the optimization was 128 */
1332: #define NPARMAX 64 /**< (nlstate+ndeath-1)*nlstate*ncovmodel */
1333:
1334: #define NINTERVMAX 8
1335: #define NLSTATEMAX 8 /**< Maximum number of live states (for func) */
1336: #define NDEATHMAX 8 /**< Maximum number of dead states (for func) */
1337: #define NCOVMAX 30 /**< Maximum number of covariates used in the model, including generated covariates V1*V2 or V1*age */
1338: #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1339: /*#define decodtabm(h,k,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (k-1)) & 1) +1 : -1)*/
1340: #define decodtabm(h,k,cptcoveff) (((h-1) >> (k-1)) & 1) +1
1341: /*#define MAXN 20000 */ /* Should by replaced by nobs, real number of observations and unlimited */
1342: #define YEARM 12. /**< Number of months per year */
1343: /* #define AGESUP 130 */
1344: /* #define AGESUP 150 */
1345: #define AGESUP 200
1346: #define AGEINF 0
1347: #define AGEMARGE 25 /* Marge for agemin and agemax for(iage=agemin-AGEMARGE; iage <= agemax+3+AGEMARGE; iage++) */
1348: #define AGEBASE 40
1349: #define AGEOVERFLOW 1.e20
1350: #define AGEGOMP 10 /**< Minimal age for Gompertz adjustment */
1351: #ifdef _WIN32
1352: #define DIRSEPARATOR '\\'
1353: #define CHARSEPARATOR "\\"
1354: #define ODIRSEPARATOR '/'
1355: #else
1356: #define DIRSEPARATOR '/'
1357: #define CHARSEPARATOR "/"
1358: #define ODIRSEPARATOR '\\'
1359: #endif
1360:
1361: /* $Id: imachprax.c,v 1.1 2023/01/31 09:24:19 brouard Exp $ */
1362: /* $State: Exp $ */
1363: #include "version.h"
1364: char version[]=__IMACH_VERSION__;
1365: char copyright[]="January 2023,INED-EUROREVES-Institut de longevite-Japan Society for the Promotion of Science (Grant-in-Aid for Scientific Research 25293121), Intel Software 2015-2020, Nihon University 2021-202, INED 2000-2022";
1366: char fullversion[]="$Revision: 1.1 $ $Date: 2023/01/31 09:24:19 $";
1367: char strstart[80];
1368: char optionfilext[10], optionfilefiname[FILENAMELENGTH];
1369: int erreur=0, nberr=0, nbwarn=0; /* Error number, number of errors number of warnings */
1370: int debugILK=0; /* debugILK is set by a #d in a comment line */
1371: int nagesqr=0, nforce=0; /* nagesqr=1 if model is including age*age, number of forces */
1372: /* Number of covariates model (1)=V2+V1+ V3*age+V2*V4 */
1373: /* Model(2) V1 + V2 + V3 + V8 + V7*V8 + V5*V6 + V8*age + V3*age + age*age */
1374: int cptcovn=0; /**< cptcovn decodemodel: number of covariates k of the models excluding age*products =6 and age*age but including products */
1375: int cptcovt=0; /**< cptcovt: total number of covariates of the model (2) nbocc(+)+1 = 8 excepting constant and age and age*age */
1376: int cptcovs=0; /**< cptcovs number of SIMPLE covariates in the model V2+V1 =2 (dummy or quantit or time varying) */
1377: int cptcovsnq=0; /**< cptcovsnq number of SIMPLE covariates in the model but non quantitative V2+V1 =2 */
1378: int cptcovage=0; /**< Number of covariates with age: V3*age only =1 */
1379: int cptcovprodage=0; /**< Number of fixed covariates with age: V3*age or V2*V3*age 1 */
1380: int cptcovprodvage=0; /**< Number of varying covariates with age: V7*age or V7*V6*age */
1381: int cptcovdageprod=0; /**< Number of doubleproducts with age, since 0.99r44 only: age*Vn*Vm for gnuplot printing*/
1382: int cptcovprodnoage=0; /**< Number of covariate products without age */
1383: 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) */
1384: int ncovf=0; /* Total number of effective fixed covariates (dummy or quantitative) in the model */
1385: int ncovv=0; /* Total number of effective (wave) varying covariates (dummy or quantitative) in the model */
1386: int ncovvt=0; /* Total number of effective (wave) varying covariates (dummy or quantitative or products [without age]) in the model */
1387: int ncovvta=0; /* +age*V6 + age*V7+ age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 Total number of expandend products [with age]) in the model */
1388: int ncovta=0; /*age*V3*V2 +age*V2+agev3+ageV4 +age*V6 + age*V7+ age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 Total number of expandend products [with age]) in the model */
1389: int ncova=0; /* Total number of effective (wave and stepm) varying with age covariates (single or product, dummy or quantitative) in the model */
1390: int ncovva=0; /* +age*V6 + age*V7+ge*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 Total number of effective (wave and stepm) varying with age covariates (single or product, dummy or quantitative) in the model */
1391: int nsd=0; /**< Total number of single dummy variables (output) */
1392: int nsq=0; /**< Total number of single quantitative variables (output) */
1393: int ncoveff=0; /* Total number of effective fixed dummy covariates in the model */
1394: int nqfveff=0; /**< nqfveff Number of Quantitative Fixed Variables Effective */
1395: int ntveff=0; /**< ntveff number of effective time varying variables */
1396: int nqtveff=0; /**< ntqveff number of effective time varying quantitative variables */
1397: int cptcov=0; /* Working variable */
1398: int firstobs=1, lastobs=10; /* nobs = lastobs-firstobs+1 declared globally ;*/
1399: int nobs=10; /* Number of observations in the data lastobs-firstobs */
1400: int ncovcombmax=NCOVMAX; /* Maximum calculated number of covariate combination = pow(2, cptcoveff) */
1401: int npar=NPARMAX; /* Number of parameters (nlstate+ndeath-1)*nlstate*ncovmodel; */
1402: int nlstate=2; /* Number of live states */
1403: int ndeath=1; /* Number of dead states */
1404: int ncovmodel=0, ncovcol=0; /* Total number of covariables including constant a12*1 +b12*x ncovmodel=2 */
1405: int nqv=0, ntv=0, nqtv=0; /* Total number of quantitative variables, time variable (dummy), quantitative and time variable*/
1406: int ncovcolt=0; /* ncovcolt=ncovcol+nqv+ntv+nqtv; total of covariates in the data, not in the model equation*/
1407: int popbased=0;
1408:
1409: int *wav; /* Number of waves for this individuual 0 is possible */
1410: int maxwav=0; /* Maxim number of waves */
1411: int jmin=0, jmax=0; /* min, max spacing between 2 waves */
1412: int ijmin=0, ijmax=0; /* Individuals having jmin and jmax */
1413: int gipmx=0, gsw=0; /* Global variables on the number of contributions
1414: to the likelihood and the sum of weights (done by funcone)*/
1415: int mle=1, weightopt=0;
1416: int **mw; /* mw[mi][i] is number of the mi wave for this individual */
1417: int **dh; /* dh[mi][i] is number of steps between mi,mi+1 for this individual */
1418: int **bh; /* bh[mi][i] is the bias (+ or -) for this individual if the delay between
1419: * wave mi and wave mi+1 is not an exact multiple of stepm. */
1420: int countcallfunc=0; /* Count the number of calls to func */
1421: int selected(int kvar); /* Is covariate kvar selected for printing results */
1422:
1423: double jmean=1; /* Mean space between 2 waves */
1424: double **matprod2(); /* test */
1425: double **oldm, **newm, **savm; /* Working pointers to matrices */
1426: double **oldms, **newms, **savms; /* Fixed working pointers to matrices */
1427: double **ddnewms, **ddoldms, **ddsavms; /* for freeing later */
1428:
1429: /*FILE *fic ; */ /* Used in readdata only */
1430: FILE *ficpar, *ficparo,*ficres, *ficresp, *ficresphtm, *ficresphtmfr, *ficrespl, *ficresplb,*ficrespij, *ficrespijb, *ficrest,*ficresf, *ficresfb,*ficrespop;
1431: FILE *ficlog, *ficrespow;
1432: int globpr=0; /* Global variable for printing or not */
1433: double fretone; /* Only one call to likelihood */
1434: long ipmx=0; /* Number of contributions */
1435: double sw; /* Sum of weights */
1436: char filerespow[FILENAMELENGTH];
1437: char fileresilk[FILENAMELENGTH]; /* File of individual contributions to the likelihood */
1438: FILE *ficresilk;
1439: FILE *ficgp,*ficresprob,*ficpop, *ficresprobcov, *ficresprobcor;
1440: FILE *ficresprobmorprev;
1441: FILE *fichtm, *fichtmcov; /* Html File */
1442: FILE *ficreseij;
1443: char filerese[FILENAMELENGTH];
1444: FILE *ficresstdeij;
1445: char fileresstde[FILENAMELENGTH];
1446: FILE *ficrescveij;
1447: char filerescve[FILENAMELENGTH];
1448: FILE *ficresvij;
1449: char fileresv[FILENAMELENGTH];
1450:
1451: char title[MAXLINE];
1452: char model[MAXLINE]; /**< The model line */
1453: char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH], filerespl[FILENAMELENGTH], fileresplb[FILENAMELENGTH];
1454: char plotcmd[FILENAMELENGTH], pplotcmd[FILENAMELENGTH];
1455: char tmpout[FILENAMELENGTH], tmpout2[FILENAMELENGTH];
1456: char command[FILENAMELENGTH];
1457: int outcmd=0;
1458:
1459: char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filerespijb[FILENAMELENGTH], filereso[FILENAMELENGTH], rfileres[FILENAMELENGTH];
1460: char fileresu[FILENAMELENGTH]; /* fileres without r in front */
1461: char filelog[FILENAMELENGTH]; /* Log file */
1462: char filerest[FILENAMELENGTH];
1463: char fileregp[FILENAMELENGTH];
1464: char popfile[FILENAMELENGTH];
1465:
1466: char optionfilegnuplot[FILENAMELENGTH], optionfilehtm[FILENAMELENGTH], optionfilehtmcov[FILENAMELENGTH] ;
1467:
1468: /* struct timeval start_time, end_time, curr_time, last_time, forecast_time; */
1469: /* struct timezone tzp; */
1470: /* extern int gettimeofday(); */
1471: struct tm tml, *gmtime(), *localtime();
1472:
1473: extern time_t time();
1474:
1475: struct tm start_time, end_time, curr_time, last_time, forecast_time;
1476: time_t rstart_time, rend_time, rcurr_time, rlast_time, rforecast_time; /* raw time */
1477: struct tm tm;
1478:
1479: char strcurr[80], strfor[80];
1480:
1481: char *endptr;
1482: long lval;
1483: double dval;
1484:
1485: #define NR_END 1
1486: #define FREE_ARG char*
1487: #define FTOL 1.0e-10
1488:
1489: #define NRANSI
1490: #define ITMAX 200
1491: #define ITPOWMAX 20 /* This is now multiplied by the number of parameters */
1492:
1493: #define TOL 2.0e-4
1494:
1495: #define CGOLD 0.3819660
1496: #define ZEPS 1.0e-10
1497: #define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d);
1498:
1499: #define GOLD 1.618034
1500: #define GLIMIT 100.0
1501: #define TINY 1.0e-20
1502:
1503: static double maxarg1,maxarg2;
1504: #define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)>(maxarg2)? (maxarg1):(maxarg2))
1505: #define FMIN(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)<(maxarg2)? (maxarg1):(maxarg2))
1506:
1507: #define SIGN(a,b) ((b)>0.0 ? fabs(a) : -fabs(a))
1508: #define rint(a) floor(a+0.5)
1509: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/myutils_8h-source.html */
1510: #define mytinydouble 1.0e-16
1511: /* #define DEQUAL(a,b) (fabs((a)-(b))<mytinydouble) */
1512: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/mynrutils_8h-source.html */
1513: /* static double dsqrarg; */
1514: /* #define DSQR(a) (DEQUAL((dsqrarg=(a)),0.0) ? 0.0 : dsqrarg*dsqrarg) */
1515: static double sqrarg;
1516: #define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 :sqrarg*sqrarg)
1517: #define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;}
1518: int agegomp= AGEGOMP;
1519:
1520: int imx;
1521: int stepm=1;
1522: /* Stepm, step in month: minimum step interpolation*/
1523:
1524: int estepm;
1525: /* Estepm, step in month to interpolate survival function in order to approximate Life Expectancy*/
1526:
1527: int m,nb;
1528: long *num;
1529: int firstpass=0, lastpass=4,*cod, *cens;
1530: int *ncodemax; /* ncodemax[j]= Number of modalities of the j th
1531: covariate for which somebody answered excluding
1532: undefined. Usually 2: 0 and 1. */
1533: int *ncodemaxwundef; /* ncodemax[j]= Number of modalities of the j th
1534: covariate for which somebody answered including
1535: undefined. Usually 3: -1, 0 and 1. */
1536: double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint;
1537: double **pmmij, ***probs; /* Global pointer */
1538: double ***mobaverage, ***mobaverages; /* New global variable */
1539: double **precov; /* New global variable to store for each resultline, values of model covariates given by the resultlines (in order to speed up) */
1540: double *ageexmed,*agecens;
1541: double dateintmean=0;
1542: double anprojd, mprojd, jprojd; /* For eventual projections */
1543: double anprojf, mprojf, jprojf;
1544:
1545: double anbackd, mbackd, jbackd; /* For eventual backprojections */
1546: double anbackf, mbackf, jbackf;
1547: double jintmean,mintmean,aintmean;
1548: double *weight;
1549: int **s; /* Status */
1550: double *agedc;
1551: double **covar; /**< covar[j,i], value of jth covariate for individual i,
1552: * covar=matrix(0,NCOVMAX,1,n);
1553: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*age; */
1554: double **coqvar; /* Fixed quantitative covariate nqv */
1555: double ***cotvar; /* Time varying covariate start at ncovcol + nqv + (1 to ntv) */
1556: double ***cotqvar; /* Time varying quantitative covariate itqv */
1557: double idx;
1558: int **nbcode, *Tvar; /**< model=V2 => Tvar[1]= 2 */
1559: /* Some documentation */
1560: /* Design original data
1561: * V1 V2 V3 V4 V5 V6 V7 V8 Weight ddb ddth d1st s1 V9 V10 V11 V12 s2 V9 V10 V11 V12
1562: * < ncovcol=6 > nqv=2 (V7 V8) dv dv dv qtv dv dv dvv qtv
1563: * ntv=3 nqtv=1
1564: * cptcovn number of covariates (not including constant and age or age*age) = number of plus sign + 1 = 10+1=11
1565: * For time varying covariate, quanti or dummies
1566: * cotqvar[wav][iv(1 to nqtv)][i]= [1][12][i]=(V12) quanti
1567: * cotvar[wav][ncovcol+nqv+ iv(1 to nqtv)][i]= [(1 to nqtv)][i]=(V12) quanti
1568: * cotvar[wav][iv(1 to ntv)][i]= [1][1][i]=(V9) dummies at wav 1
1569: * cotvar[wav][iv(1 to ntv)][i]= [1][2][i]=(V10) dummies at wav 1
1570: * covar[Vk,i], value of the Vkth fixed covariate dummy or quanti for individual i:
1571: * covar[1][i]= (V1), covar[4][i]=(V4), covar[8][i]=(V8)
1572: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8 + V9 + V9*age + V10
1573: * k= 1 2 3 4 5 6 7 8 9 10 11
1574: */
1575: /* According to the model, more columns can be added to covar by the product of covariates */
1576: /* 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
1577: # States 1=Coresidence, 2 Living alone, 3 Institution
1578: # V1=sex, V2=raedyrs Quant Fixed, State=livarnb4..livarnb11, V3=iadl4..iald11, V4=adlw4..adlw11, V5=r4bmi..r11bmi
1579: */
1580: /* V5+V4+ V3+V4*V3 +V5*age+V2 +V1*V2+V1*age+V1+V4*V3*age */
1581: /* kmodel 1 2 3 4 5 6 7 8 9 10 */
1582: /*Typevar[k]= 0 0 0 2 1 0 2 1 0 3 *//*0 for simple covariate (dummy, quantitative,*/
1583: /* fixed or varying), 1 for age product, 2 for*/
1584: /* product without age, 3 for age and double product */
1585: /*Dummy[k]= 1 0 0 1 3 1 1 2 0 3 *//*Dummy[k] 0=dummy (0 1), 1 quantitative */
1586: /*(single or product without age), 2 dummy*/
1587: /* with age product, 3 quant with age product*/
1588: /*Tvar[k]= 5 4 3 6 5 2 7 1 1 6 */
1589: /* nsd 1 2 3 */ /* Counting single dummies covar fixed or tv */
1590: /*TnsdVar[Tvar] 1 2 3 */
1591: /*Tvaraff[nsd] 4 3 1 */ /* ID of single dummy cova fixed or timevary*/
1592: /*TvarsD[nsd] 4 3 1 */ /* ID of single dummy cova fixed or timevary*/
1593: /*TvarsDind[nsd] 2 3 9 */ /* position K of single dummy cova */
1594: /* nsq 1 2 */ /* Counting single quantit tv */
1595: /* TvarsQ[k] 5 2 */ /* Number of single quantitative cova */
1596: /* TvarsQind 1 6 */ /* position K of single quantitative cova */
1597: /* Tprod[i]=k 1 2 */ /* Position in model of the ith prod without age */
1598: /* cptcovage 1 2 3 */ /* Counting cov*age in the model equation */
1599: /* Tage[cptcovage]=k 5 8 10 */ /* Position in the model of ith cov*age */
1600: /* Tvard[1][1]@4={4,3,1,2} V4*V3 V1*V2 */ /* Position in model of the ith prod without age */
1601: /* 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*/
1602: /* 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 */
1603: /* TvarFind; TvarFind[1]=6, TvarFind[2]=7, TvarFind[3]=9 *//* Inverse V2(6) is first fixed (single or prod) */
1604: /* Type */
1605: /* V 1 2 3 4 5 */
1606: /* F F V V V */
1607: /* D Q D D Q */
1608: /* */
1609: int *TvarsD;
1610: int *TnsdVar;
1611: int *TvarsDind;
1612: int *TvarsQ;
1613: int *TvarsQind;
1614:
1615: #define MAXRESULTLINESPONE 10+1
1616: int nresult=0;
1617: int parameterline=0; /* # of the parameter (type) line */
1618: int TKresult[MAXRESULTLINESPONE]; /* TKresult[nres]=k for each resultline nres give the corresponding combination of dummies */
1619: int resultmodel[MAXRESULTLINESPONE][NCOVMAX];/* resultmodel[k1]=k3: k1th position in the model corresponds to the k3 position in the resultline */
1620: int modelresult[MAXRESULTLINESPONE][NCOVMAX];/* modelresult[k3]=k1: k1th position in the model corresponds to the k3 position in the resultline */
1621: int Tresult[MAXRESULTLINESPONE][NCOVMAX];/* Tresult[nres][result_position]= value of the dummy variable at the result_position in the nres resultline */
1622: int Tinvresult[MAXRESULTLINESPONE][NCOVMAX];/* Tinvresult[nres][Name of a dummy variable]= value of the variable in the result line */
1623: double TinvDoQresult[MAXRESULTLINESPONE][NCOVMAX];/* TinvDoQresult[nres][Name of a Dummy or Q variable]= value of the variable in the result line */
1624: int Tvresult[MAXRESULTLINESPONE][NCOVMAX]; /* Tvresult[nres][result_position]= name of the dummy variable at the result_position in the nres resultline */
1625: double Tqresult[MAXRESULTLINESPONE][NCOVMAX]; /* Tqresult[nres][result_position]= value of the variable at the result_position in the nres resultline */
1626: double Tqinvresult[MAXRESULTLINESPONE][NCOVMAX]; /* For quantitative variable , value (output) */
1627: int Tvqresult[MAXRESULTLINESPONE][NCOVMAX]; /* Tvqresult[nres][result_position]= id of the variable at the result_position in the nres resultline */
1628:
1629: /* 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
1630: # States 1=Coresidence, 2 Living alone, 3 Institution
1631: # V1=sex, V2=raedyrs Quant Fixed, State=livarnb4..livarnb11, V3=iadl4..iald11, V4=adlw4..adlw11, V5=r4bmi..r11bmi
1632: */
1633: /* 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 *\/ */
1634: 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 */
1635: 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 */
1636: 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 */
1637: int *TvarVind; /**< TvarVind[1]=1, TvarVind[2]=2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1638: 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 */
1639: int *TvarAind; /**< TvarindA[1]=5, TvarAind[2]=8 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1640: int *TvarFD; /**< TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1641: int *TvarFDind; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1642: int *TvarFQ; /* TvarFQ[1]=V2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1643: int *TvarFQind; /* TvarFQind[1]=6 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1644: int *TvarVD; /* TvarVD[1]=V5 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1645: int *TvarVDind; /* TvarVDind[1]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1646: 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 */
1647: 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 */
1648: int *TvarVV; /* We count ncovvt time varying covariates (single or products without age) and put their name into TvarVV */
1649: int *TvarVVind; /* We count ncovvt time varying covariates (single or products without age) and put their name into TvarVV */
1650: int *TvarVVA; /* We count ncovvt time varying covariates (single or products with age) and put their name into TvarVVA */
1651: int *TvarVVAind; /* We count ncovvt time varying covariates (single or products without age) and put their name into TvarVV */
1652: int *TvarAVVA; /* We count ALL ncovta time varying covariates (single or products with age) and put their name into TvarVVA */
1653: int *TvarAVVAind; /* We count ALL ncovta time varying covariates (single or products without age) and put their name into TvarVV */
1654: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
1655: /* model V1+V3+age*V1+age*V3+V1*V3 + V1*V3*age */
1656: /* Tvar={1, 3, 1, 3, 6, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
1657: /* TvarVV={3,1,3,1,3}, for V3 and then the product V1*V3 is decomposed into V1 and V3 */
1658: /* TvarVVind={2,5,5,6,6}, for V3 and then the product V1*V3 is decomposed into V1 and V3 and V1*V3*age into 6,6 */
1659: int *Tvarsel; /**< Selected covariates for output */
1660: double *Tvalsel; /**< Selected modality value of covariate for output */
1661: int *Typevar; /**< 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product, 3 age*Vn*Vm */
1662: int *Fixed; /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
1663: 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 */
1664: int *DummyV; /** Dummy[v] 0=dummy (0 1), 1 quantitative */
1665: int *FixedV; /** FixedV[v] 0 fixed, 1 varying */
1666: int *Tage;
1667: int anyvaryingduminmodel=0; /**< Any varying dummy in Model=1 yes, 0 no, to avoid a loop on waves in freq */
1668: 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*/
1669: 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*/
1670: 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 */
1671: int *Ndum; /** Freq of modality (tricode */
1672: /* int **codtab;*/ /**< codtab=imatrix(1,100,1,10); */
1673: int **Tvard;
1674: int **Tvardk;
1675: int *Tprod;/**< Gives the k position of the k1 product */
1676: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3 */
1677: int *Tposprod; /**< Gives the k1 product from the k position */
1678: /* if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2) */
1679: /* Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5(V3*V2)]=2 (2nd product without age) */
1680: int cptcovprod, *Tvaraff, *invalidvarcomb;
1681: double *lsurv, *lpop, *tpop;
1682:
1683: #define FD 1; /* Fixed dummy covariate */
1684: #define FQ 2; /* Fixed quantitative covariate */
1685: #define FP 3; /* Fixed product covariate */
1686: #define FPDD 7; /* Fixed product dummy*dummy covariate */
1687: #define FPDQ 8; /* Fixed product dummy*quantitative covariate */
1688: #define FPQQ 9; /* Fixed product quantitative*quantitative covariate */
1689: #define VD 10; /* Varying dummy covariate */
1690: #define VQ 11; /* Varying quantitative covariate */
1691: #define VP 12; /* Varying product covariate */
1692: #define VPDD 13; /* Varying product dummy*dummy covariate */
1693: #define VPDQ 14; /* Varying product dummy*quantitative covariate */
1694: #define VPQQ 15; /* Varying product quantitative*quantitative covariate */
1695: #define APFD 16; /* Age product * fixed dummy covariate */
1696: #define APFQ 17; /* Age product * fixed quantitative covariate */
1697: #define APVD 18; /* Age product * varying dummy covariate */
1698: #define APVQ 19; /* Age product * varying quantitative covariate */
1699:
1700: #define FTYPE 1; /* Fixed covariate */
1701: #define VTYPE 2; /* Varying covariate (loop in wave) */
1702: #define ATYPE 2; /* Age product covariate (loop in dh within wave)*/
1703:
1704: struct kmodel{
1705: int maintype; /* main type */
1706: int subtype; /* subtype */
1707: };
1708: struct kmodel modell[NCOVMAX];
1709:
1710: double ftol=FTOL; /**< Tolerance for computing Max Likelihood */
1711: double ftolhess; /**< Tolerance for computing hessian */
1712:
1713: /**************** split *************************/
1714: static int split( char *path, char *dirc, char *name, char *ext, char *finame )
1715: {
1716: /* From a file name with (full) path (either Unix or Windows) we extract the directory (dirc)
1717: the name of the file (name), its extension only (ext) and its first part of the name (finame)
1718: */
1719: char *ss; /* pointer */
1720: int l1=0, l2=0; /* length counters */
1721:
1722: l1 = strlen(path ); /* length of path */
1723: if ( l1 == 0 ) return( GLOCK_ERROR_NOPATH );
1724: ss= strrchr( path, DIRSEPARATOR ); /* find last / */
1725: if ( ss == NULL ) { /* no directory, so determine current directory */
1726: strcpy( name, path ); /* we got the fullname name because no directory */
1727: /*if(strrchr(path, ODIRSEPARATOR )==NULL)
1728: printf("Warning you should use %s as a separator\n",DIRSEPARATOR);*/
1729: /* get current working directory */
1730: /* extern char* getcwd ( char *buf , int len);*/
1731: #ifdef WIN32
1732: if (_getcwd( dirc, FILENAME_MAX ) == NULL ) {
1733: #else
1734: if (getcwd(dirc, FILENAME_MAX) == NULL) {
1735: #endif
1736: return( GLOCK_ERROR_GETCWD );
1737: }
1738: /* got dirc from getcwd*/
1739: printf(" DIRC = %s \n",dirc);
1740: } else { /* strip directory from path */
1741: ss++; /* after this, the filename */
1742: l2 = strlen( ss ); /* length of filename */
1743: if ( l2 == 0 ) return( GLOCK_ERROR_NOPATH );
1744: strcpy( name, ss ); /* save file name */
1745: strncpy( dirc, path, l1 - l2 ); /* now the directory */
1746: dirc[l1-l2] = '\0'; /* add zero */
1747: printf(" DIRC2 = %s \n",dirc);
1748: }
1749: /* We add a separator at the end of dirc if not exists */
1750: l1 = strlen( dirc ); /* length of directory */
1751: if( dirc[l1-1] != DIRSEPARATOR ){
1752: dirc[l1] = DIRSEPARATOR;
1753: dirc[l1+1] = 0;
1754: printf(" DIRC3 = %s \n",dirc);
1755: }
1756: ss = strrchr( name, '.' ); /* find last / */
1757: if (ss >0){
1758: ss++;
1759: strcpy(ext,ss); /* save extension */
1760: l1= strlen( name);
1761: l2= strlen(ss)+1;
1762: strncpy( finame, name, l1-l2);
1763: finame[l1-l2]= 0;
1764: }
1765:
1766: return( 0 ); /* we're done */
1767: }
1768:
1769:
1770: /******************************************/
1771:
1772: void replace_back_to_slash(char *s, char*t)
1773: {
1774: int i;
1775: int lg=0;
1776: i=0;
1777: lg=strlen(t);
1778: for(i=0; i<= lg; i++) {
1779: (s[i] = t[i]);
1780: if (t[i]== '\\') s[i]='/';
1781: }
1782: }
1783:
1784: char *trimbb(char *out, char *in)
1785: { /* Trim multiple blanks in line but keeps first blanks if line starts with blanks */
1786: char *s;
1787: s=out;
1788: while (*in != '\0'){
1789: while( *in == ' ' && *(in+1) == ' '){ /* && *(in+1) != '\0'){*/
1790: in++;
1791: }
1792: *out++ = *in++;
1793: }
1794: *out='\0';
1795: return s;
1796: }
1797:
1798: /* char *substrchaine(char *out, char *in, char *chain) */
1799: /* { */
1800: /* /\* Substract chain 'chain' from 'in', return and output 'out' *\/ */
1801: /* char *s, *t; */
1802: /* t=in;s=out; */
1803: /* while ((*in != *chain) && (*in != '\0')){ */
1804: /* *out++ = *in++; */
1805: /* } */
1806:
1807: /* /\* *in matches *chain *\/ */
1808: /* while ((*in++ == *chain++) && (*in != '\0')){ */
1809: /* printf("*in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1810: /* } */
1811: /* in--; chain--; */
1812: /* while ( (*in != '\0')){ */
1813: /* printf("Bef *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1814: /* *out++ = *in++; */
1815: /* printf("Aft *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1816: /* } */
1817: /* *out='\0'; */
1818: /* out=s; */
1819: /* return out; */
1820: /* } */
1821: char *substrchaine(char *out, char *in, char *chain)
1822: {
1823: /* Substract chain 'chain' from 'in', return and output 'out' */
1824: /* in="V1+V1*age+age*age+V2", chain="+age*age" out="V1+V1*age+V2" */
1825:
1826: char *strloc;
1827:
1828: strcpy (out, in); /* out="V1+V1*age+age*age+V2" */
1829: strloc = strstr(out, chain); /* strloc points to out at "+age*age+V2" */
1830: printf("Bef strloc=%s chain=%s out=%s \n", strloc, chain, out); /* strloc=+age*age+V2 chain="+age*age", out="V1+V1*age+age*age+V2" */
1831: if(strloc != NULL){
1832: /* will affect out */ /* strloc+strlen(chain)=|+V2 = "V1+V1*age+age*age|+V2" */ /* Will also work in Unicodek */
1833: memmove(strloc,strloc+strlen(chain), strlen(strloc+strlen(chain))+1); /* move number of bytes corresponding to the length of "+V2" which is 3, plus one is 4 (including the null)*/
1834: /* equivalent to strcpy (strloc, strloc +strlen(chain)) if no overlap; Copies from "+V2" to V1+V1*age+ */
1835: }
1836: printf("Aft strloc=%s chain=%s in=%s out=%s \n", strloc, chain, in, out); /* strloc=+V2 chain="+age*age", in="V1+V1*age+age*age+V2", out="V1+V1*age+V2" */
1837: return out;
1838: }
1839:
1840:
1841: char *cutl(char *blocc, char *alocc, char *in, char occ)
1842: {
1843: /* cuts string in into blocc and alocc where blocc ends before FIRST occurence of char 'occ'
1844: and alocc starts after first occurence of char 'occ' : ex cutl(blocc,alocc,"abcdef2ghi2j",'2')
1845: gives alocc="abcdef" and blocc="ghi2j".
1846: If occ is not found blocc is null and alocc is equal to in. Returns blocc
1847: */
1848: char *s, *t;
1849: t=in;s=in;
1850: while ((*in != occ) && (*in != '\0')){
1851: *alocc++ = *in++;
1852: }
1853: if( *in == occ){
1854: *(alocc)='\0';
1855: s=++in;
1856: }
1857:
1858: if (s == t) {/* occ not found */
1859: *(alocc-(in-s))='\0';
1860: in=s;
1861: }
1862: while ( *in != '\0'){
1863: *blocc++ = *in++;
1864: }
1865:
1866: *blocc='\0';
1867: return t;
1868: }
1869: char *cutv(char *blocc, char *alocc, char *in, char occ)
1870: {
1871: /* cuts string in into blocc and alocc where blocc ends before LAST occurence of char 'occ'
1872: and alocc starts after last occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
1873: gives blocc="abcdef2ghi" and alocc="j".
1874: If occ is not found blocc is null and alocc is equal to in. Returns alocc
1875: */
1876: char *s, *t;
1877: t=in;s=in;
1878: while (*in != '\0'){
1879: while( *in == occ){
1880: *blocc++ = *in++;
1881: s=in;
1882: }
1883: *blocc++ = *in++;
1884: }
1885: if (s == t) /* occ not found */
1886: *(blocc-(in-s))='\0';
1887: else
1888: *(blocc-(in-s)-1)='\0';
1889: in=s;
1890: while ( *in != '\0'){
1891: *alocc++ = *in++;
1892: }
1893:
1894: *alocc='\0';
1895: return s;
1896: }
1897:
1898: int nbocc(char *s, char occ)
1899: {
1900: int i,j=0;
1901: int lg=20;
1902: i=0;
1903: lg=strlen(s);
1904: for(i=0; i<= lg; i++) {
1905: if (s[i] == occ ) j++;
1906: }
1907: return j;
1908: }
1909:
1910: int nboccstr(char *textin, char *chain)
1911: {
1912: /* Counts the number of occurence of "chain" in string textin */
1913: /* in="+V7*V4+age*V2+age*V3+age*V4" chain="age" */
1914: char *strloc;
1915:
1916: int i,j=0;
1917:
1918: i=0;
1919:
1920: strloc=textin; /* strloc points to "^+V7*V4+age+..." in textin */
1921: for(;;) {
1922: strloc= strstr(strloc,chain); /* strloc points to first character of chain in textin if found. Example strloc points^ to "+V7*V4+^age" in textin */
1923: if(strloc != NULL){
1924: strloc = strloc+strlen(chain); /* strloc points to "+V7*V4+age^" in textin */
1925: j++;
1926: }else
1927: break;
1928: }
1929: return j;
1930:
1931: }
1932: /* void cutv(char *u,char *v, char*t, char occ) */
1933: /* { */
1934: /* /\* cuts string t into u and v where u ends before last occurence of char 'occ' */
1935: /* and v starts after last occurence of char 'occ' : ex cutv(u,v,"abcdef2ghi2j",'2') */
1936: /* gives u="abcdef2ghi" and v="j" *\/ */
1937: /* int i,lg,j,p=0; */
1938: /* i=0; */
1939: /* lg=strlen(t); */
1940: /* for(j=0; j<=lg-1; j++) { */
1941: /* if((t[j]!= occ) && (t[j+1]== occ)) p=j+1; */
1942: /* } */
1943:
1944: /* for(j=0; j<p; j++) { */
1945: /* (u[j] = t[j]); */
1946: /* } */
1947: /* u[p]='\0'; */
1948:
1949: /* for(j=0; j<= lg; j++) { */
1950: /* if (j>=(p+1))(v[j-p-1] = t[j]); */
1951: /* } */
1952: /* } */
1953:
1954: #ifdef _WIN32
1955: char * strsep(char **pp, const char *delim)
1956: {
1957: char *p, *q;
1958:
1959: if ((p = *pp) == NULL)
1960: return 0;
1961: if ((q = strpbrk (p, delim)) != NULL)
1962: {
1963: *pp = q + 1;
1964: *q = '\0';
1965: }
1966: else
1967: *pp = 0;
1968: return p;
1969: }
1970: #endif
1971:
1972: /********************** nrerror ********************/
1973:
1974: void nrerror(char error_text[])
1975: {
1976: fprintf(stderr,"ERREUR ...\n");
1977: fprintf(stderr,"%s\n",error_text);
1978: exit(EXIT_FAILURE);
1979: }
1980: /*********************** vector *******************/
1981: double *vector(int nl, int nh)
1982: {
1983: double *v;
1984: v=(double *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(double)));
1985: if (!v) nrerror("allocation failure in vector");
1986: return v-nl+NR_END;
1987: }
1988:
1989: /************************ free vector ******************/
1990: void free_vector(double*v, int nl, int nh)
1991: {
1992: free((FREE_ARG)(v+nl-NR_END));
1993: }
1994:
1995: /************************ivector *******************************/
1996: int *ivector(long nl,long nh)
1997: {
1998: int *v;
1999: v=(int *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(int)));
2000: if (!v) nrerror("allocation failure in ivector");
2001: return v-nl+NR_END;
2002: }
2003:
2004: /******************free ivector **************************/
2005: void free_ivector(int *v, long nl, long nh)
2006: {
2007: free((FREE_ARG)(v+nl-NR_END));
2008: }
2009:
2010: /************************lvector *******************************/
2011: long *lvector(long nl,long nh)
2012: {
2013: long *v;
2014: v=(long *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(long)));
2015: if (!v) nrerror("allocation failure in ivector");
2016: return v-nl+NR_END;
2017: }
2018:
2019: /******************free lvector **************************/
2020: void free_lvector(long *v, long nl, long nh)
2021: {
2022: free((FREE_ARG)(v+nl-NR_END));
2023: }
2024:
2025: /******************* imatrix *******************************/
2026: int **imatrix(long nrl, long nrh, long ncl, long nch)
2027: /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
2028: {
2029: long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
2030: int **m;
2031:
2032: /* allocate pointers to rows */
2033: m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
2034: if (!m) nrerror("allocation failure 1 in matrix()");
2035: m += NR_END;
2036: m -= nrl;
2037:
2038:
2039: /* allocate rows and set pointers to them */
2040: m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
2041: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
2042: m[nrl] += NR_END;
2043: m[nrl] -= ncl;
2044:
2045: for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
2046:
2047: /* return pointer to array of pointers to rows */
2048: return m;
2049: }
2050:
2051: /****************** free_imatrix *************************/
2052: void free_imatrix(m,nrl,nrh,ncl,nch)
2053: int **m;
2054: long nch,ncl,nrh,nrl;
2055: /* free an int matrix allocated by imatrix() */
2056: {
2057: free((FREE_ARG) (m[nrl]+ncl-NR_END));
2058: free((FREE_ARG) (m+nrl-NR_END));
2059: }
2060:
2061: /******************* matrix *******************************/
2062: double **matrix(long nrl, long nrh, long ncl, long nch)
2063: {
2064: long i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
2065: double **m;
2066:
2067: m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
2068: if (!m) nrerror("allocation failure 1 in matrix()");
2069: m += NR_END;
2070: m -= nrl;
2071:
2072: m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
2073: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
2074: m[nrl] += NR_END;
2075: m[nrl] -= ncl;
2076:
2077: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
2078: return m;
2079: /* print *(*(m+1)+70) or print m[1][70]; print m+1 or print &(m[1]) or &(m[1][0])
2080: m[i] = address of ith row of the table. &(m[i]) is its value which is another adress
2081: that of m[i][0]. In order to get the value p m[i][0] but it is unitialized.
2082: */
2083: }
2084:
2085: /*************************free matrix ************************/
2086: void free_matrix(double **m, long nrl, long nrh, long ncl, long nch)
2087: {
2088: free((FREE_ARG)(m[nrl]+ncl-NR_END));
2089: free((FREE_ARG)(m+nrl-NR_END));
2090: }
2091:
2092: /******************* ma3x *******************************/
2093: double ***ma3x(long nrl, long nrh, long ncl, long nch, long nll, long nlh)
2094: {
2095: long i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1, nlay=nlh-nll+1;
2096: double ***m;
2097:
2098: m=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
2099: if (!m) nrerror("allocation failure 1 in matrix()");
2100: m += NR_END;
2101: m -= nrl;
2102:
2103: m[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
2104: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
2105: m[nrl] += NR_END;
2106: m[nrl] -= ncl;
2107:
2108: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
2109:
2110: m[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*nlay+NR_END)*sizeof(double)));
2111: if (!m[nrl][ncl]) nrerror("allocation failure 3 in matrix()");
2112: m[nrl][ncl] += NR_END;
2113: m[nrl][ncl] -= nll;
2114: for (j=ncl+1; j<=nch; j++)
2115: m[nrl][j]=m[nrl][j-1]+nlay;
2116:
2117: for (i=nrl+1; i<=nrh; i++) {
2118: m[i][ncl]=m[i-1l][ncl]+ncol*nlay;
2119: for (j=ncl+1; j<=nch; j++)
2120: m[i][j]=m[i][j-1]+nlay;
2121: }
2122: return m;
2123: /* gdb: p *(m+1) <=> p m[1] and p (m+1) <=> p (m+1) <=> p &(m[1])
2124: &(m[i][j][k]) <=> *((*(m+i) + j)+k)
2125: */
2126: }
2127:
2128: /*************************free ma3x ************************/
2129: void free_ma3x(double ***m, long nrl, long nrh, long ncl, long nch,long nll, long nlh)
2130: {
2131: free((FREE_ARG)(m[nrl][ncl]+ nll-NR_END));
2132: free((FREE_ARG)(m[nrl]+ncl-NR_END));
2133: free((FREE_ARG)(m+nrl-NR_END));
2134: }
2135:
2136: /*************** function subdirf ***********/
2137: char *subdirf(char fileres[])
2138: {
2139: /* Caution optionfilefiname is hidden */
2140: strcpy(tmpout,optionfilefiname);
2141: strcat(tmpout,"/"); /* Add to the right */
2142: strcat(tmpout,fileres);
2143: return tmpout;
2144: }
2145:
2146: /*************** function subdirf2 ***********/
2147: char *subdirf2(char fileres[], char *preop)
2148: {
2149: /* Example subdirf2(optionfilefiname,"FB_") with optionfilefiname="texte", result="texte/FB_texte"
2150: Errors in subdirf, 2, 3 while printing tmpout is
2151: rewritten within the same printf. Workaround: many printfs */
2152: /* Caution optionfilefiname is hidden */
2153: strcpy(tmpout,optionfilefiname);
2154: strcat(tmpout,"/");
2155: strcat(tmpout,preop);
2156: strcat(tmpout,fileres);
2157: return tmpout;
2158: }
2159:
2160: /*************** function subdirf3 ***********/
2161: char *subdirf3(char fileres[], char *preop, char *preop2)
2162: {
2163:
2164: /* Caution optionfilefiname is hidden */
2165: strcpy(tmpout,optionfilefiname);
2166: strcat(tmpout,"/");
2167: strcat(tmpout,preop);
2168: strcat(tmpout,preop2);
2169: strcat(tmpout,fileres);
2170: return tmpout;
2171: }
2172:
2173: /*************** function subdirfext ***********/
2174: char *subdirfext(char fileres[], char *preop, char *postop)
2175: {
2176:
2177: strcpy(tmpout,preop);
2178: strcat(tmpout,fileres);
2179: strcat(tmpout,postop);
2180: return tmpout;
2181: }
2182:
2183: /*************** function subdirfext3 ***********/
2184: char *subdirfext3(char fileres[], char *preop, char *postop)
2185: {
2186:
2187: /* Caution optionfilefiname is hidden */
2188: strcpy(tmpout,optionfilefiname);
2189: strcat(tmpout,"/");
2190: strcat(tmpout,preop);
2191: strcat(tmpout,fileres);
2192: strcat(tmpout,postop);
2193: return tmpout;
2194: }
2195:
2196: char *asc_diff_time(long time_sec, char ascdiff[])
2197: {
2198: long sec_left, days, hours, minutes;
2199: days = (time_sec) / (60*60*24);
2200: sec_left = (time_sec) % (60*60*24);
2201: hours = (sec_left) / (60*60) ;
2202: sec_left = (sec_left) %(60*60);
2203: minutes = (sec_left) /60;
2204: sec_left = (sec_left) % (60);
2205: sprintf(ascdiff,"%ld day(s) %ld hour(s) %ld minute(s) %ld second(s)",days, hours, minutes, sec_left);
2206: return ascdiff;
2207: }
2208:
2209: /***************** f1dim *************************/
2210: extern int ncom;
2211: extern double *pcom,*xicom;
2212: extern double (*nrfunc)(double []);
2213:
2214: double f1dim(double x)
2215: {
2216: int j;
2217: double f;
2218: double *xt;
2219:
2220: xt=vector(1,ncom);
2221: for (j=1;j<=ncom;j++) xt[j]=pcom[j]+x*xicom[j];
2222: f=(*nrfunc)(xt);
2223: free_vector(xt,1,ncom);
2224: return f;
2225: }
2226:
2227: /*****************brent *************************/
2228: double brent(double ax, double bx, double cx, double (*f)(double), double tol, double *xmin)
2229: {
2230: /* Given a function f, and given a bracketing triplet of abscissas ax, bx, cx (such that bx is
2231: * between ax and cx, and f(bx) is less than both f(ax) and f(cx) ), this routine isolates
2232: * the minimum to a fractional precision of about tol using Brent’s method. The abscissa of
2233: * the minimum is returned as xmin, and the minimum function value is returned as brent , the
2234: * returned function value.
2235: */
2236: int iter;
2237: double a,b,d,etemp;
2238: double fu=0,fv,fw,fx;
2239: double ftemp=0.;
2240: double p,q,r,tol1,tol2,u,v,w,x,xm;
2241: double e=0.0;
2242:
2243: a=(ax < cx ? ax : cx);
2244: b=(ax > cx ? ax : cx);
2245: x=w=v=bx;
2246: fw=fv=fx=(*f)(x);
2247: for (iter=1;iter<=ITMAX;iter++) {
2248: xm=0.5*(a+b);
2249: tol2=2.0*(tol1=tol*fabs(x)+ZEPS);
2250: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret)))*/
2251: printf(".");fflush(stdout);
2252: fprintf(ficlog,".");fflush(ficlog);
2253: #ifdef DEBUGBRENT
2254: 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);
2255: 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);
2256: /* if ((fabs(x-xm) <= (tol2-0.5*(b-a)))||(2.0*fabs(fu-ftemp) <= ftol*1.e-2*(fabs(fu)+fabs(ftemp)))) { */
2257: #endif
2258: if (fabs(x-xm) <= (tol2-0.5*(b-a))){
2259: *xmin=x;
2260: return fx;
2261: }
2262: ftemp=fu;
2263: if (fabs(e) > tol1) {
2264: r=(x-w)*(fx-fv);
2265: q=(x-v)*(fx-fw);
2266: p=(x-v)*q-(x-w)*r;
2267: q=2.0*(q-r);
2268: if (q > 0.0) p = -p;
2269: q=fabs(q);
2270: etemp=e;
2271: e=d;
2272: if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x))
2273: d=CGOLD*(e=(x >= xm ? a-x : b-x));
2274: else {
2275: d=p/q;
2276: u=x+d;
2277: if (u-a < tol2 || b-u < tol2)
2278: d=SIGN(tol1,xm-x);
2279: }
2280: } else {
2281: d=CGOLD*(e=(x >= xm ? a-x : b-x));
2282: }
2283: u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d));
2284: fu=(*f)(u);
2285: if (fu <= fx) {
2286: if (u >= x) a=x; else b=x;
2287: SHFT(v,w,x,u)
2288: SHFT(fv,fw,fx,fu)
2289: } else {
2290: if (u < x) a=u; else b=u;
2291: if (fu <= fw || w == x) {
2292: v=w;
2293: w=u;
2294: fv=fw;
2295: fw=fu;
2296: } else if (fu <= fv || v == x || v == w) {
2297: v=u;
2298: fv=fu;
2299: }
2300: }
2301: }
2302: nrerror("Too many iterations in brent");
2303: *xmin=x;
2304: return fx;
2305: }
2306:
2307: /****************** mnbrak ***********************/
2308:
2309: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, double *fc,
2310: double (*func)(double))
2311: { /* Given a function func , and given distinct initial points ax and bx , this routine searches in
2312: the downhill direction (defined by the function as evaluated at the initial points) and returns
2313: new points ax , bx , cx that bracket a minimum of the function. Also returned are the function
2314: values at the three points, fa, fb , and fc such that fa > fb and fb < fc.
2315: */
2316: double ulim,u,r,q, dum;
2317: double fu;
2318:
2319: double scale=10.;
2320: int iterscale=0;
2321:
2322: *fa=(*func)(*ax); /* xta[j]=pcom[j]+(*ax)*xicom[j]; fa=f(xta[j])*/
2323: *fb=(*func)(*bx); /* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) */
2324:
2325:
2326: /* while(*fb != *fb){ /\* *ax should be ok, reducing distance to *ax *\/ */
2327: /* printf("Warning mnbrak *fb = %lf, *bx=%lf *ax=%lf *fa==%lf iter=%d\n",*fb, *bx, *ax, *fa, iterscale++); */
2328: /* *bx = *ax - (*ax - *bx)/scale; */
2329: /* *fb=(*func)(*bx); /\* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) *\/ */
2330: /* } */
2331:
2332: if (*fb > *fa) {
2333: SHFT(dum,*ax,*bx,dum)
2334: SHFT(dum,*fb,*fa,dum)
2335: }
2336: *cx=(*bx)+GOLD*(*bx-*ax);
2337: *fc=(*func)(*cx);
2338: #ifdef DEBUG
2339: printf("mnbrak0 a=%lf *fa=%lf, b=%lf *fb=%lf, c=%lf *fc=%lf\n",*ax,*fa,*bx,*fb,*cx, *fc);
2340: fprintf(ficlog,"mnbrak0 a=%lf *fa=%lf, b=%lf *fb=%lf, c=%lf *fc=%lf\n",*ax,*fa,*bx,*fb,*cx, *fc);
2341: #endif
2342: while (*fb > *fc) { /* Declining a,b,c with fa> fb > fc. If fc=inf it exits and if flat fb=fc it exits too.*/
2343: r=(*bx-*ax)*(*fb-*fc);
2344: q=(*bx-*cx)*(*fb-*fa); /* What if fa=inf */
2345: u=(*bx)-((*bx-*cx)*q-(*bx-*ax)*r)/
2346: (2.0*SIGN(FMAX(fabs(q-r),TINY),q-r)); /* Minimum abscissa of a parabolic estimated from (a,fa), (b,fb) and (c,fc). */
2347: ulim=(*bx)+GLIMIT*(*cx-*bx); /* Maximum abscissa where function should be evaluated */
2348: if ((*bx-u)*(u-*cx) > 0.0) { /* if u_p is between b and c */
2349: fu=(*func)(u);
2350: #ifdef DEBUG
2351: /* f(x)=A(x-u)**2+f(u) */
2352: double A, fparabu;
2353: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
2354: fparabu= *fa - A*(*ax-u)*(*ax-u);
2355: 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);
2356: 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);
2357: /* And thus,it can be that fu > *fc even if fparabu < *fc */
2358: /* mnbrak (*ax=7.666299858533, *fa=299039.693133272231), (*bx=8.595447774979, *fb=298976.598289369489),
2359: (*cx=10.098840694817, *fc=298946.631474258087), (*u=9.852501168332, fu=298948.773013752128, fparabu=298945.434711494134) */
2360: /* In that case, there is no bracket in the output! Routine is wrong with many consequences.*/
2361: #endif
2362: #ifdef MNBRAKORIGINAL
2363: #else
2364: /* if (fu > *fc) { */
2365: /* #ifdef DEBUG */
2366: /* printf("mnbrak4 fu > fc \n"); */
2367: /* fprintf(ficlog, "mnbrak4 fu > fc\n"); */
2368: /* #endif */
2369: /* /\* 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 *\\/ *\/ */
2370: /* /\* SHFT(*fa,*fc,fu,*fc) /\\* (b, u, c) is a bracket while test fb > fc will be fu > fc will exit *\\/ *\/ */
2371: /* dum=u; /\* Shifting c and u *\/ */
2372: /* u = *cx; */
2373: /* *cx = dum; */
2374: /* dum = fu; */
2375: /* fu = *fc; */
2376: /* *fc =dum; */
2377: /* } else { /\* end *\/ */
2378: /* #ifdef DEBUG */
2379: /* printf("mnbrak3 fu < fc \n"); */
2380: /* fprintf(ficlog, "mnbrak3 fu < fc\n"); */
2381: /* #endif */
2382: /* dum=u; /\* Shifting c and u *\/ */
2383: /* u = *cx; */
2384: /* *cx = dum; */
2385: /* dum = fu; */
2386: /* fu = *fc; */
2387: /* *fc =dum; */
2388: /* } */
2389: #ifdef DEBUGMNBRAK
2390: double A, fparabu;
2391: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
2392: fparabu= *fa - A*(*ax-u)*(*ax-u);
2393: 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);
2394: 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);
2395: #endif
2396: dum=u; /* Shifting c and u */
2397: u = *cx;
2398: *cx = dum;
2399: dum = fu;
2400: fu = *fc;
2401: *fc =dum;
2402: #endif
2403: } else if ((*cx-u)*(u-ulim) > 0.0) { /* u is after c but before ulim */
2404: #ifdef DEBUG
2405: printf("\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
2406: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
2407: #endif
2408: fu=(*func)(u);
2409: if (fu < *fc) {
2410: #ifdef DEBUG
2411: printf("\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
2412: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
2413: #endif
2414: SHFT(*bx,*cx,u,*cx+GOLD*(*cx-*bx))
2415: SHFT(*fb,*fc,fu,(*func)(u))
2416: #ifdef DEBUG
2417: printf("\nmnbrak2 shift GOLD c=%lf",*cx+GOLD*(*cx-*bx));
2418: #endif
2419: }
2420: } else if ((u-ulim)*(ulim-*cx) >= 0.0) { /* u outside ulim (verifying that ulim is beyond c) */
2421: #ifdef DEBUG
2422: printf("\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
2423: fprintf(ficlog,"\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
2424: #endif
2425: u=ulim;
2426: fu=(*func)(u);
2427: } else { /* u could be left to b (if r > q parabola has a maximum) */
2428: #ifdef DEBUG
2429: printf("\nmnbrak2 u=%lf could be left to b=%lf (if r=%lf > q=%lf parabola has a maximum)\n",u,*bx,r,q);
2430: 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);
2431: #endif
2432: u=(*cx)+GOLD*(*cx-*bx);
2433: fu=(*func)(u);
2434: #ifdef DEBUG
2435: printf("\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
2436: fprintf(ficlog,"\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
2437: #endif
2438: } /* end tests */
2439: SHFT(*ax,*bx,*cx,u)
2440: SHFT(*fa,*fb,*fc,fu)
2441: #ifdef DEBUG
2442: printf("\nmnbrak2 shift (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf)\n",*ax,*fa,*bx,*fb,*cx,*fc);
2443: fprintf(ficlog, "\nmnbrak2 shift (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf)\n",*ax,*fa,*bx,*fb,*cx,*fc);
2444: #endif
2445: } /* end while; ie return (a, b, c, fa, fb, fc) such that a < b < c with f(a) > f(b) and fb < f(c) */
2446: }
2447:
2448: /*************** linmin ************************/
2449: /* Given an n -dimensional point p[1..n] and an n -dimensional direction xi[1..n] , moves and
2450: resets p to where the function func(p) takes on a minimum along the direction xi from p ,
2451: and replaces xi by the actual vector displacement that p was moved. Also returns as fret
2452: the value of func at the returned location p . This is actually all accomplished by calling the
2453: routines mnbrak and brent .*/
2454: int ncom;
2455: double *pcom,*xicom;
2456: double (*nrfunc)(double []);
2457:
2458: #ifdef LINMINORIGINAL
2459: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []))
2460: #else
2461: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []), int *flat)
2462: #endif
2463: {
2464: double brent(double ax, double bx, double cx,
2465: double (*f)(double), double tol, double *xmin);
2466: double f1dim(double x);
2467: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb,
2468: double *fc, double (*func)(double));
2469: int j;
2470: double xx,xmin,bx,ax;
2471: double fx,fb,fa;
2472:
2473: #ifdef LINMINORIGINAL
2474: #else
2475: double scale=10., axs, xxs; /* Scale added for infinity */
2476: #endif
2477:
2478: ncom=n;
2479: pcom=vector(1,n);
2480: xicom=vector(1,n);
2481: nrfunc=func;
2482: for (j=1;j<=n;j++) {
2483: pcom[j]=p[j];
2484: xicom[j]=xi[j]; /* Former scale xi[j] of currrent direction i */
2485: }
2486:
2487: #ifdef LINMINORIGINAL
2488: xx=1.;
2489: #else
2490: axs=0.0;
2491: xxs=1.;
2492: do{
2493: xx= xxs;
2494: #endif
2495: ax=0.;
2496: mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim); /* Outputs: xtx[j]=pcom[j]+(*xx)*xicom[j]; fx=f(xtx[j]) */
2497: /* brackets with inputs ax=0 and xx=1, but points, pcom=p, and directions values, xicom=xi, are sent via f1dim(x) */
2498: /* 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)) */
2499: /* Outputs: fa=f(p(j)) and fx=f(p(j) + xxs * xi(j) ) and f(bx)= f(p(j)+ bx* xi(j)) */
2500: /* Given input ax=axs and xx=xxs, xx might be too far from ax to get a finite f(xx) */
2501: /* Searches on line, outputs (ax, xx, bx) such that fx < min(fa and fb) */
2502: /* 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]]*/
2503: #ifdef LINMINORIGINAL
2504: #else
2505: if (fx != fx){
2506: xxs=xxs/scale; /* Trying a smaller xx, closer to initial ax=0 */
2507: printf("|");
2508: fprintf(ficlog,"|");
2509: #ifdef DEBUGLINMIN
2510: 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);
2511: #endif
2512: }
2513: }while(fx != fx && xxs > 1.e-5);
2514: #endif
2515:
2516: #ifdef DEBUGLINMIN
2517: 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);
2518: 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);
2519: #endif
2520: #ifdef LINMINORIGINAL
2521: #else
2522: if(fb == fx){ /* Flat function in the direction */
2523: xmin=xx;
2524: *flat=1;
2525: }else{
2526: *flat=0;
2527: #endif
2528: /*Flat mnbrak2 shift (*ax=0.000000000000, *fa=51626.272983130431), (*bx=-1.618034000000, *fb=51590.149499362531), (*cx=-4.236068025156, *fc=51590.149499362531) */
2529: *fret=brent(ax,xx,bx,f1dim,TOL,&xmin); /* Giving a bracketting triplet (ax, xx, bx), find a minimum, xmin, according to f1dim, *fret(xmin),*/
2530: /* fa = f(p[j] + ax * xi[j]), fx = f(p[j] + xx * xi[j]), fb = f(p[j] + bx * xi[j]) */
2531: /* fmin = f(p[j] + xmin * xi[j]) */
2532: /* P+lambda n in that direction (lambdamin), with TOL between abscisses */
2533: /* f1dim(xmin): for (j=1;j<=ncom;j++) xt[j]=pcom[j]+xmin*xicom[j]; */
2534: #ifdef DEBUG
2535: 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);
2536: 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);
2537: #endif
2538: #ifdef LINMINORIGINAL
2539: #else
2540: }
2541: #endif
2542: #ifdef DEBUGLINMIN
2543: printf("linmin end ");
2544: fprintf(ficlog,"linmin end ");
2545: #endif
2546: for (j=1;j<=n;j++) {
2547: #ifdef LINMINORIGINAL
2548: xi[j] *= xmin;
2549: #else
2550: #ifdef DEBUGLINMIN
2551: if(xxs <1.0)
2552: printf(" before xi[%d]=%12.8f", j,xi[j]);
2553: #endif
2554: 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) */
2555: #ifdef DEBUGLINMIN
2556: if(xxs <1.0)
2557: 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 );
2558: #endif
2559: #endif
2560: p[j] += xi[j]; /* Parameters values are updated accordingly */
2561: }
2562: #ifdef DEBUGLINMIN
2563: printf("\n");
2564: printf("Comparing last *frec(xmin=%12.8f)=%12.8f from Brent and frec(0.)=%12.8f \n", xmin, *fret, (*func)(p));
2565: fprintf(ficlog,"Comparing last *frec(xmin=%12.8f)=%12.8f from Brent and frec(0.)=%12.8f \n", xmin, *fret, (*func)(p));
2566: for (j=1;j<=n;j++) {
2567: printf(" xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
2568: fprintf(ficlog," xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
2569: if(j % ncovmodel == 0){
2570: printf("\n");
2571: fprintf(ficlog,"\n");
2572: }
2573: }
2574: #else
2575: #endif
2576: free_vector(xicom,1,n);
2577: free_vector(pcom,1,n);
2578: }
2579:
2580: /**** praxis ****/
2581: # include <float.h>
2582: /* # include <math.h> */
2583: /* # include <stdio.h> */
2584: /* # include <stdlib.h> */
2585: /* # include <string.h> */
2586: /* # include <time.h> */
2587:
2588: # include "praxis.h"
2589:
2590: /******************************************************************************/
2591:
2592: double flin ( int n, int jsearch, double l, double (*func) ( double [] ),
2593: double x[], int *nf, double v[], double q0[], double q1[], double *qd0,
2594: double *qd1, double *qa, double *qb, double *qc )
2595: /* double flin ( int n, int jsearch, double l, double f ( double x[], int n ), */
2596: /* double x[], int *nf, double v[], double q0[], double q1[], double *qd0, */
2597: /* double *qd1, double *qa, double *qb, double *qc ) */
2598:
2599: /******************************************************************************/
2600: /*
2601: Purpose:
2602:
2603: FLIN is the function of one variable to be minimized by MINNY.
2604:
2605: Discussion:
2606:
2607: F(X) is a scalar function of a vector argument X.
2608:
2609: A minimizer of F(X) is sought along a line or parabola.
2610:
2611: Licensing:
2612:
2613: This code is distributed under the GNU LGPL license.
2614:
2615: Modified:
2616:
2617: 28 July 2016
2618:
2619: Author:
2620:
2621: Original FORTRAN77 version by Richard Brent.
2622: C version by John Burkardt.
2623:
2624: Reference:
2625:
2626: Richard Brent,
2627: Algorithms for Minimization with Derivatives,
2628: Prentice Hall, 1973,
2629: Reprinted by Dover, 2002.
2630:
2631: Parameters:
2632:
2633: Input, int N, the number of variables.
2634:
2635: Input, int JSEARCH, indicates the kind of search.
2636: If JSEARCH is a legal column index, linear search along V(*,JSEARCH).
2637: If JSEARCH is -1, then the search is parabolic, based on X, Q0 and Q1.
2638:
2639: Input, double L, is the parameter determining the particular
2640: point at which F is to be evaluated.
2641: For a linear search, L is the step size.
2642: For a quadratic search, L is a parameter which specifies
2643: a point in the plane of X, Q0 and Q1.
2644:
2645: Input, double F ( double X[], int N ), the function to be minimized.
2646:
2647: Input, double X[N], the base point of the search.
2648:
2649: Input/output, int *NF, the function evaluation counter.
2650:
2651: Input, double V[N,N], a matrix whose columns constitute
2652: search directions.
2653:
2654: Input, double Q0[N], Q1[N], two auxiliary points used to
2655: determine the plane when a quadratic search is performed.
2656:
2657: Input, double *QD0, *QD1, values needed to compute the
2658: coefficients QA, QB, QC.
2659:
2660: Output, double *QA, *QB, *QC, coefficients used to combine
2661: Q0, X, and A1 if a quadratic search is used.
2662:
2663: Output, double FLIN, the value of the function at the
2664: minimizing point.
2665: */
2666: {
2667: int i;
2668: double *t;
2669: double value;
2670:
2671: t = ( double * ) malloc ( n * sizeof ( double ) );
2672: /*
2673: The search is linear.
2674: */
2675: if ( 0 <= jsearch )
2676: {
2677: for ( i = 0; i < n; i++ )
2678: {
2679: t[i] = x[i] + l * v[i+jsearch*n];
2680: }
2681: }
2682: /*
2683: The search is along a parabolic space curve.
2684: */
2685: else
2686: {
2687: *qa = l * ( l - *qd1 ) / ( *qd0 + *qd1 ) / *qd0;
2688: *qb = - ( l + *qd0 ) * ( l - *qd1 ) / *qd1 / *qd0;
2689: *qc = ( l + *qd0 ) * l / *qd1 / ( *qd0 + *qd1 );
2690:
2691: for ( i = 0; i < n; i++ )
2692: {
2693: t[i] = *qa * q0[i] + *qb * x[i] + *qc * q1[i];
2694: }
2695: }
2696: /*
2697: The function evaluation counter NF is incremented.
2698: */
2699: *nf = *nf + 1;
2700: /*
2701: Evaluate the function.
2702: */
2703: value = (*func) ( (t-1) );/* This for func which is computed from x[1] and not from x[0] xm1=(x-1)*/
2704: /* value = f ( t, n ); */
2705:
2706: free ( t );
2707:
2708: return value;
2709: }
2710: /******************************************************************************/
2711:
2712: void minfit ( int n, double tol, double a[], double q[] )
2713:
2714: /******************************************************************************/
2715: /*
2716: Purpose:
2717:
2718: MINFIT computes the singular value decomposition of an N by N array.
2719:
2720: Discussion:
2721:
2722: This is an improved version of the EISPACK routine MINFIT
2723: restricted to the case M = N and P = 0.
2724:
2725: The singular values of the array A are returned in Q. A is
2726: overwritten with the orthogonal matrix V such that U * diag(Q) = A * V,
2727: where U is another orthogonal matrix.
2728:
2729: Licensing:
2730:
2731: This code is distributed under the GNU LGPL license.
2732:
2733: Modified:
2734:
2735: 30 July 2016
2736:
2737: Author:
2738:
2739: Original FORTRAN77 version by Richard Brent.
2740: C version by John Burkardt.
2741:
2742: Reference:
2743:
2744: Richard Brent,
2745: Algorithms for Minimization with Derivatives,
2746: Prentice Hall, 1973,
2747: Reprinted by Dover, 2002.
2748:
2749: James Wilkinson, Christian Reinsch,
2750: Handbook for Automatic Computation,
2751: Volume II, Linear Algebra, Part 2,
2752: Springer Verlag, 1971.
2753:
2754: Brian Smith, James Boyle, Jack Dongarra, Burton Garbow, Yasuhiko Ikebe,
2755: Virginia Klema, Cleve Moler,
2756: Matrix Eigensystem Routines, EISPACK Guide,
2757: Lecture Notes in Computer Science, Volume 6,
2758: Springer Verlag, 1976,
2759: ISBN13: 978-3540075462,
2760: LC: QA193.M37.
2761:
2762: Parameters:
2763:
2764: Input, int N, the order of the matrix A.
2765:
2766: Input, double TOL, a tolerance which determines when a vector
2767: (a column or part of a column of the matrix) may be considered
2768: "essentially" equal to zero.
2769:
2770: Input/output, double A[N,N]. On input, an N by N array whose
2771: singular value decomposition is desired. On output, the
2772: SVD orthogonal matrix factor V.
2773:
2774: Input/output, double Q[N], the singular values.
2775: */
2776: {
2777: double c;
2778: double *e;
2779: double eps;
2780: double f;
2781: double g;
2782: double h;
2783: int i;
2784: int ii;
2785: int j;
2786: int jj;
2787: int k;
2788: int kt;
2789: const int kt_max = 30;
2790: int l;
2791: int l2;
2792: double s;
2793: int skip;
2794: double temp;
2795: double x;
2796: double y;
2797: double z;
2798: /*
2799: Householder's reduction to bidiagonal form.
2800: */
2801: if ( n == 1 )
2802: {
2803: q[0] = a[0+0*n];
2804: a[0+0*n] = 1.0;
2805: return;
2806: }
2807:
2808: e = ( double * ) malloc ( n * sizeof ( double ) );
2809:
2810: eps = DBL_EPSILON;
2811: g = 0.0;
2812: x = 0.0;
2813:
2814: for ( i = 1; i <= n; i++ )
2815: {
2816: e[i-1] = g;
2817: l = i + 1;
2818:
2819: s = 0.0;
2820: for ( ii = i; ii <= n; ii++ )
2821: {
2822: s = s + a[ii-1+(i-1)*n] * a[ii-1+(i-1)*n];
2823: }
2824:
2825: g = 0.0;
2826:
2827: if ( tol <= s )
2828: {
2829: f = a[i-1+(i-1)*n];
2830:
2831: g = sqrt ( s );
2832:
2833: if ( 0.0 <= f )
2834: {
2835: g = - g;
2836: }
2837:
2838: h = f * g - s;
2839: a[i-1+(i-1)*n] = f - g;
2840:
2841: for ( j = l; j <= n; j++ )
2842: {
2843: f = 0.0;
2844: for ( ii = i; ii <= n; ii++ )
2845: {
2846: f = f + a[ii-1+(i-1)*n] * a[ii-1+(j-1)*n];
2847: }
2848: f = f / h;
2849:
2850: for ( ii = i; ii <= n; ii++ )
2851: {
2852: a[ii-1+(j-1)*n] = a[ii-1+(j-1)*n] + f * a[ii-1+(i-1)*n];
2853: }
2854: }
2855: }
2856:
2857: q[i-1] = g;
2858:
2859: s = 0.0;
2860: for ( j = l; j <= n; j++ )
2861: {
2862: s = s + a[i-1+(j-1)*n] * a[i-1+(j-1)*n];
2863: }
2864:
2865: g = 0.0;
2866:
2867: if ( tol <= s )
2868: {
2869: if ( i < n )
2870: {
2871: f = a[i-1+i*n];
2872: }
2873:
2874: g = sqrt ( s );
2875:
2876: if ( 0.0 <= f )
2877: {
2878: g = - g;
2879: }
2880:
2881: h = f * g - s;
2882:
2883: if ( i < n )
2884: {
2885: a[i-1+i*n] = f - g;
2886: for ( jj = l; jj <= n; jj++ )
2887: {
2888: e[jj-1] = a[i-1+(jj-1)*n] / h;
2889: }
2890:
2891: for ( j = l; j <= n; j++ )
2892: {
2893: s = 0.0;
2894: for ( jj = l; jj <= n; jj++ )
2895: {
2896: s = s + a[j-1+(jj-1)*n] * a[i-1+(jj-1)*n];
2897: }
2898: for ( jj = l; jj <= n; jj++ )
2899: {
2900: a[j-1+(jj-1)*n] = a[j-1+(jj-1)*n] + s * e[jj-1];
2901: }
2902: }
2903: }
2904: }
2905:
2906: y = fabs ( q[i-1] ) + fabs ( e[i-1] );
2907:
2908: x = fmax ( x, y );
2909: }
2910: /*
2911: Accumulation of right-hand transformations.
2912: */
2913: a[n-1+(n-1)*n] = 1.0;
2914: g = e[n-1];
2915: l = n;
2916:
2917: for ( i = n - 1; 1 <= i; i-- )
2918: {
2919: if ( g != 0.0 )
2920: {
2921: h = a[i-1+i*n] * g;
2922:
2923: for ( ii = l; ii <= n; ii++ )
2924: {
2925: a[ii-1+(i-1)*n] = a[i-1+(ii-1)*n] / h;
2926: }
2927:
2928: for ( j = l; j <= n; j++ )
2929: {
2930: s = 0.0;
2931: for ( jj = l; jj <= n; jj++ )
2932: {
2933: s = s + a[i-1+(jj-1)*n] * a[jj-1+(j-1)*n];
2934: }
2935:
2936: for ( ii = l; ii <= n; ii++ )
2937: {
2938: a[ii-1+(j-1)*n] = a[ii-1+(j-1)*n] + s * a[ii-1+(i-1)*n];
2939: }
2940: }
2941: }
2942:
2943: for ( jj = l; jj <= n; jj++ )
2944: {
2945: a[i-1+(jj-1)*n] = 0.0;
2946: }
2947:
2948: for ( ii = l; ii <= n; ii++ )
2949: {
2950: a[ii-1+(i-1)*n] = 0.0;
2951: }
2952:
2953: a[i-1+(i-1)*n] = 1.0;
2954:
2955: g = e[i-1];
2956:
2957: l = i;
2958: }
2959: /*
2960: Diagonalization of the bidiagonal form.
2961: */
2962: eps = eps * x;
2963:
2964: for ( k = n; 1 <= k; k-- )
2965: {
2966: kt = 0;
2967:
2968: for ( ; ; )
2969: {
2970: kt = kt + 1;
2971:
2972: if ( kt_max < kt )
2973: {
2974: e[k-1] = 0.0;
2975: fprintf ( stderr, "\n" );
2976: fprintf ( stderr, "MINFIT - Fatal error!\n" );
2977: fprintf ( stderr, " The QR algorithm failed to converge.\n" );
2978: exit ( 1 );
2979: }
2980:
2981: skip = 0;
2982:
2983: for ( l2 = k; 1 <= l2; l2-- )
2984: {
2985: l = l2;
2986:
2987: if ( fabs ( e[l-1] ) <= eps )
2988: {
2989: skip = 1;
2990: break;
2991: }
2992:
2993: if ( 1 < l )
2994: {
2995: if ( fabs ( q[l-2] ) <= eps )
2996: {
2997: break;
2998: }
2999: }
3000: }
3001: /*
3002: Cancellation of E(L) if 1 < L.
3003: */
3004: if ( ! skip )
3005: {
3006: c = 0.0;
3007: s = 1.0;
3008:
3009: for ( i = l; i <= k; i++ )
3010: {
3011: f = s * e[i-1];
3012: e[i-1] = c * e[i-1];
3013: if ( fabs ( f ) <= eps )
3014: {
3015: break;
3016: }
3017: g = q[i-1];
3018: /*
3019: q(i) = h = sqrt(g*g + f*f).
3020: */
3021: h = r8_hypot ( f, g );
3022:
3023: q[i-1] = h;
3024:
3025: if ( h == 0.0 )
3026: {
3027: g = 1.0;
3028: h = 1.0;
3029: }
3030:
3031: c = g / h;
3032: s = - f / h;
3033: }
3034: }
3035: /*
3036: Test for convergence for this index K.
3037: */
3038: z = q[k-1];
3039:
3040: if ( l == k )
3041: {
3042: if ( z < 0.0 )
3043: {
3044: q[k-1] = - z;
3045: for ( i = 1; i <= n; i++ )
3046: {
3047: a[i-1+(k-1)*n] = - a[i-1+(k-1)*n];
3048: }
3049: }
3050: break;
3051: }
3052: /*
3053: Shift from bottom 2*2 minor.
3054: */
3055: x = q[l-1];
3056: y = q[k-2];
3057: g = e[k-2];
3058: h = e[k-1];
3059: f = ( ( y - z ) * ( y + z ) + ( g - h ) * ( g + h ) ) / ( 2.0 * h * y );
3060:
3061: g = r8_hypot ( f, 1.0 );
3062:
3063: if ( f < 0.0 )
3064: {
3065: temp = f - g;
3066: }
3067: else
3068: {
3069: temp = f + g;
3070: }
3071:
3072: f = ( ( x - z ) * ( x + z ) + h * ( y / temp - h ) ) / x;
3073: /*
3074: Next QR transformation.
3075: */
3076: c = 1.0;
3077: s = 1.0;
3078:
3079: for ( i = l + 1; i <= k; i++ )
3080: {
3081: g = e[i-1];
3082: y = q[i-1];
3083: h = s * g;
3084: g = g * c;
3085:
3086: z = r8_hypot ( f, h );
3087:
3088: e[i-2] = z;
3089:
3090: if ( z == 0.0 )
3091: {
3092: f = 1.0;
3093: z = 1.0;
3094: }
3095:
3096: c = f / z;
3097: s = h / z;
3098: f = x * c + g * s;
3099: g = - x * s + g * c;
3100: h = y * s;
3101: y = y * c;
3102:
3103: for ( j = 1; j <= n; j++ )
3104: {
3105: x = a[j-1+(i-2)*n];
3106: z = a[j-1+(i-1)*n];
3107: a[j-1+(i-2)*n] = x * c + z * s;
3108: a[j-1+(i-1)*n] = - x * s + z * c;
3109: }
3110:
3111: z = r8_hypot ( f, h );
3112:
3113: q[i-2] = z;
3114:
3115: if ( z == 0.0 )
3116: {
3117: f = 1.0;
3118: z = 1.0;
3119: }
3120:
3121: c = f / z;
3122: s = h / z;
3123: f = c * g + s * y;
3124: x = - s * g + c * y;
3125: }
3126:
3127: e[l-1] = 0.0;
3128: e[k-1] = f;
3129: q[k-1] = x;
3130: }
3131: }
3132:
3133: free ( e );
3134:
3135: return;
3136: }
3137: /******************************************************************************/
3138:
3139: void minny ( int n, int jsearch, int nits, double *d2, double *x1, double *f1,
3140: int fk, double (*func) ( double []), double x[], double t, double h,
3141: double v[], double q0[], double q1[], int *nl, int *nf, double dmin,
3142: double ldt, double *fx, double *qa, double *qb, double *qc, double *qd0,
3143: double *qd1 )
3144: /* void minny ( int n, int jsearch, int nits, double *d2, double *x1, double *f1, */
3145: /* int fk, double f ( double x[], int n ), double x[], double t, double h, */
3146: /* double v[], double q0[], double q1[], int *nl, int *nf, double dmin, */
3147: /* double ldt, double *fx, double *qa, double *qb, double *qc, double *qd0, */
3148: /* double *qd1 ) */
3149:
3150: /******************************************************************************/
3151: /*
3152: Purpose:
3153:
3154: MINNY minimizes a scalar function of N variables along a line.
3155:
3156: Discussion:
3157:
3158: MINNY minimizes F along the line from X in the direction V(*,JSEARCH)
3159: or else using a quadratic search in the plane defined by Q0, Q1 and X.
3160:
3161: If FK = true, then F1 is FLIN(X1). Otherwise X1 and F1 are ignored
3162: on entry unless final FX is greater than F1.
3163:
3164: Licensing:
3165:
3166: This code is distributed under the GNU LGPL license.
3167:
3168: Modified:
3169:
3170: 03 August 2016
3171:
3172: Author:
3173:
3174: Original FORTRAN77 version by Richard Brent.
3175: C version by John Burkardt.
3176:
3177: Reference:
3178:
3179: Richard Brent,
3180: Algorithms for Minimization with Derivatives,
3181: Prentice Hall, 1973,
3182: Reprinted by Dover, 2002.
3183:
3184: Parameters:
3185:
3186: Input, int N, the number of variables.
3187:
3188: Input, int JSEARCH, indicates the kind of search.
3189: If J is a legal columnindex, linear search in the direction of V(*,JSEARCH).
3190: Otherwise, the search is parabolic, based on X, Q0 and Q1.
3191:
3192: Input, int NITS, the maximum number of times the interval
3193: may be halved to retry the calculation.
3194:
3195: Input/output, double *D2, is either zero, or an approximation to
3196: the value of (1/2) times the second derivative of F.
3197:
3198: Input/output, double *X1, on entry, an estimate of the
3199: distance from X to the minimum along V(*,JSEARCH), or a curve.
3200: On output, the distance between X and the minimizer that was found.
3201:
3202: Input/output, double *F1, ?
3203:
3204: Input, int FK; if FK is TRUE, then on input F1 contains
3205: the value FLIN(X1).
3206:
3207: Input, double F ( double X[], int N ), is the name of the function to
3208: be minimized.
3209:
3210: Input/output, double X[N], ?
3211:
3212: Input, double T, ?
3213:
3214: Input, double H, ?
3215:
3216: Input, double V[N,N], a matrix whose columns are direction
3217: vectors along which the function may be minimized.
3218:
3219: ?, double Q0[N], ?
3220:
3221: ?, double Q1[N], ?
3222:
3223: Input/output, int *NL, the number of linear searches.
3224:
3225: Input/output, int *NF, the number of function evaluations.
3226:
3227: Input, double DMIN, an estimate for the smallest eigenvalue.
3228:
3229: Input, double LDT, the length of the step.
3230:
3231: Input/output, double *FX, the value of F(X,N).
3232:
3233: Input/output, double *QA, *QB, *QC;
3234:
3235: Input/output, double *QD0, *QD1, ?.
3236: */
3237: {
3238: double d1;
3239: int dz;
3240: double f0;
3241: double f2;
3242: double fm;
3243: int i;
3244: int k;
3245: double m2;
3246: double m4;
3247: double machep;
3248: int ok;
3249: double s;
3250: double sf1;
3251: double small;
3252: double sx1;
3253: double t2;
3254: double temp;
3255: double x2;
3256: double xm;
3257:
3258: machep = DBL_EPSILON;
3259: small = machep * machep;
3260: m2 = sqrt ( machep );
3261: m4 = sqrt ( m2 );
3262: sf1 = *f1;
3263: sx1 = *x1;
3264: k = 0;
3265: xm = 0.0;
3266: fm = *fx;
3267: f0 = *fx;
3268: dz = ( *d2 < machep );
3269: /*
3270: Find the step size.
3271: */
3272: s = r8vec_norm ( n, x );
3273:
3274: if ( dz )
3275: {
3276: temp = dmin;
3277: }
3278: else
3279: {
3280: temp = *d2;
3281: }
3282:
3283: t2 = m4 * sqrt ( fabs ( *fx ) / temp + s * ldt ) + m2 * ldt;
3284: s = m4 * s + t;
3285: if ( dz && s < t2 )
3286: {
3287: t2 = s;
3288: }
3289:
3290: t2 = fmax ( t2, small );
3291: t2 = fmin ( t2, 0.01 * h );
3292:
3293: if ( fk && *f1 <= fm )
3294: {
3295: xm = *x1;
3296: fm = *f1;
3297: }
3298:
3299: if ( ( ! fk ) || fabs ( *x1 ) < t2 )
3300: {
3301: if ( 0.0 <= *x1 )
3302: {
3303: temp = 1.0;
3304: }
3305: else
3306: {
3307: temp = - 1.0;
3308: }
3309:
3310: *x1 = temp * t2;
3311: *f1 = flin ( n, jsearch, *x1, func, x, nf, v, q0, q1, qd0, qd1, qa, qb, qc );
3312: /* *f1 = flin ( n, jsearch, *x1, f, x, nf, v, q0, q1, qd0, qd1, qa, qb, qc ); */
3313: }
3314:
3315: if ( *f1 <= fm )
3316: {
3317: xm = *x1;
3318: fm = *f1;
3319: }
3320: /*
3321: Evaluate FLIN at another point and estimate the second derivative.
3322: */
3323: for ( ; ; )
3324: {
3325: if ( dz )
3326: {
3327: if ( *f1 <= f0 )
3328: {
3329: x2 = 2.0 * *x1;
3330: }
3331: else
3332: {
3333: x2 = - *x1;
3334: }
3335:
3336: f2 = flin ( n, jsearch, x2, func, x, nf, v, q0, q1, qd0, qd1, qa, qb, qc );
3337: /* f2 = flin ( n, jsearch, x2, f, x, nf, v, q0, q1, qd0, qd1, qa, qb, qc ); */
3338:
3339: if ( f2 <= fm )
3340: {
3341: xm = x2;
3342: fm = f2;
3343: }
3344:
3345: *d2 = ( x2 * ( *f1 - f0 ) - *x1 * ( f2 - f0 ) )
3346: / ( ( *x1 * x2 ) * ( *x1 - x2 ) );
3347: }
3348: /*
3349: Estimate the first derivative at 0.
3350: */
3351: d1 = ( *f1 - f0 ) / *x1 - *x1 * *d2;
3352: dz = 1;
3353: /*
3354: Predict the minimum.
3355: */
3356: if ( *d2 <= small )
3357: {
3358: if ( 0.0 <= d1 )
3359: {
3360: x2 = - h;
3361: }
3362: else
3363: {
3364: x2 = h;
3365: }
3366: }
3367: else
3368: {
3369: x2 = ( - 0.5 * d1 ) / *d2;
3370: }
3371:
3372: if ( h < fabs ( x2 ) )
3373: {
3374: if ( x2 <= 0.0 )
3375: {
3376: x2 = - h;
3377: }
3378: else
3379: {
3380: x2 = h;
3381: }
3382: }
3383: /*
3384: Evaluate F at the predicted minimum.
3385: */
3386: ok = 1;
3387:
3388: for ( ; ; )
3389: {
3390: f2 = flin ( n, jsearch, x2, func, x, nf, v, q0, q1, qd0, qd1, qa, qb, qc );
3391: /* f2 = flin ( n, jsearch, x2, f, x, nf, v, q0, q1, qd0, qd1, qa, qb, qc ); */
3392:
3393: if ( nits <= k || f2 <= f0 )
3394: {
3395: break;
3396: }
3397:
3398: k = k + 1;
3399:
3400: if ( f0 < *f1 && 0.0 < *x1 * x2 )
3401: {
3402: ok = 0;
3403: break;
3404: }
3405: x2 = 0.5 * x2;
3406: }
3407:
3408: if ( ok )
3409: {
3410: break;
3411: }
3412: }
3413: /*
3414: Increment the one-dimensional search counter.
3415: */
3416: *nl = *nl + 1;
3417:
3418: if ( fm < f2 )
3419: {
3420: x2 = xm;
3421: }
3422: else
3423: {
3424: fm = f2;
3425: }
3426: /*
3427: Get a new estimate of the second derivative.
3428: */
3429: if ( small < fabs ( x2 * ( x2 - *x1 ) ) )
3430: {
3431: *d2 = ( x2 * ( *f1 - f0 ) - *x1 * ( fm - f0 ) )
3432: / ( ( *x1 * x2 ) * ( *x1 - x2 ) );
3433: }
3434: else
3435: {
3436: if ( 0 < k )
3437: {
3438: *d2 = 0.0;
3439: }
3440: }
3441:
3442: *d2 = fmax ( *d2, small );
3443:
3444: *x1 = x2;
3445: *fx = fm;
3446:
3447: if ( sf1 < *fx )
3448: {
3449: *fx = sf1;
3450: *x1 = sx1;
3451: }
3452: /*
3453: Update X for linear search.
3454: */
3455: if ( 0 <= jsearch )
3456: {
3457: for ( i = 0; i < n; i++ )
3458: {
3459: x[i] = x[i] + *x1 * v[i+jsearch*n];
3460: }
3461: }
3462:
3463: return;
3464: }
3465: /******************************************************************************/
3466:
3467: /* double praxis ( double t0, double h0, int n, int prin, double x[], */
3468: /* double f ( double x[], int n ) ) */
3469: double praxis ( double t0, double h0, int n, int prin, double x[],
3470: double (*func) ( double [] ))
3471:
3472: /******************************************************************************/
3473: /*
3474: Purpose:
3475:
3476: PRAXIS seeks an N-dimensional minimizer X of a scalar function F(X).
3477:
3478: Discussion:
3479:
3480: PRAXIS returns the minimum of the function F(X,N) of N variables
3481: using the principal axis method. The gradient of the function is
3482: not required.
3483:
3484: The approximating quadratic form is
3485:
3486: Q(x") = F(x,n) + (1/2) * (x"-x)" * A * (x"-x)
3487:
3488: where X is the best estimate of the minimum and
3489:
3490: A = inverse(V") * D * inverse(V)
3491:
3492: V(*,*) is the matrix of search directions;
3493: D(*) is the array of second differences.
3494:
3495: If F(X) has continuous second derivatives near X0, then A will tend
3496: to the hessian of F at X0 as X approaches X0.
3497:
3498: Licensing:
3499:
3500: This code is distributed under the GNU LGPL license.
3501:
3502: Modified:
3503:
3504: 03 August 2016
3505:
3506: Author:
3507:
3508: Original FORTRAN77 version by Richard Brent.
3509: C version by John Burkardt.
3510:
3511: Reference:
3512:
3513: Richard Brent,
3514: Algorithms for Minimization with Derivatives,
3515: Prentice Hall, 1973,
3516: Reprinted by Dover, 2002.
3517:
3518: Parameters:
3519:
3520: Input, double T0, is a tolerance. PRAXIS attempts to return
3521: praxis = f(x) such that if X0 is the true local minimum near X, then
3522: norm ( x - x0 ) < T0 + sqrt ( EPSILON ) * norm ( X ),
3523: where EPSILON is the machine precision.
3524:
3525: Input, double H0, is the maximum step size. H0 should be
3526: set to about the maximum distance from the initial guess to the minimum.
3527: If H0 is set too large or too small, the initial rate of
3528: convergence may be slow.
3529:
3530: Input, int N, the number of variables.
3531:
3532: Input, int PRIN, controls printing intermediate results.
3533: 0, nothing is printed.
3534: 1, F is printed after every n+1 or n+2 linear minimizations.
3535: final X is printed, but intermediate X is printed only
3536: if N is at most 4.
3537: 2, the scale factors and the principal values of the approximating
3538: quadratic form are also printed.
3539: 3, X is also printed after every few linear minimizations.
3540: 4, the principal vectors of the approximating quadratic form are
3541: also printed.
3542:
3543: Input/output, double X[N], is an array containing on entry a
3544: guess of the point of minimum, on return the estimated point of minimum.
3545:
3546: Input, double F ( double X[], int N ), is the name of the function to be
3547: minimized.
3548:
3549: Output, double PRAXIS, the function value at the minimizer.
3550:
3551: Local parameters:
3552:
3553: Local, double DMIN, an estimate for the smallest eigenvalue.
3554:
3555: Local, double FX, the value of F(X,N).
3556:
3557: Local, int ILLC, is TRUE if the system is ill-conditioned.
3558:
3559: Local, double LDT, the length of the step.
3560:
3561: Local, int NF, the number of function evaluations.
3562:
3563: Local, int NL, the number of linear searches.
3564: */
3565: {
3566: int biter=0; /* Added to count the loops */
3567: double *d;
3568: double d2;
3569: double df;
3570: double dmin;
3571: double dn;
3572: double dni;
3573: double f1;
3574: int fk;
3575: double fx;
3576: double h;
3577: int i;
3578: int illc;
3579: int j;
3580: int jsearch;
3581: int k;
3582: int k2;
3583: int kl;
3584: int kt;
3585: int ktm;
3586: double large;
3587: double ldfac;
3588: double lds;
3589: double ldt;
3590: double m2;
3591: double m4;
3592: double machep;
3593: int nits;
3594: int nl;
3595: int nf;
3596: double *q0;
3597: double *q1;
3598: double qa;
3599: double qb;
3600: double qc;
3601: double qd0;
3602: double qd1;
3603: double qf1;
3604: double r;
3605: double s;
3606: double scbd;
3607: int seed;
3608: double sf;
3609: double sl;
3610: double small;
3611: double t;
3612: double temp;
3613: double t2;
3614: double *v;
3615: double value;
3616: double vlarge;
3617: double vsmall;
3618: double *y;
3619: double *z;
3620: /*
3621: Allocation.
3622: */
3623: d = ( double * ) malloc ( n * sizeof ( double ) );
3624: q0 = ( double * ) malloc ( n * sizeof ( double ) );
3625: q1 = ( double * ) malloc ( n * sizeof ( double ) );
3626: v = ( double * ) malloc ( n * n * sizeof ( double ) );
3627: y = ( double * ) malloc ( n * sizeof ( double ) );
3628: z = ( double * ) malloc ( n * sizeof ( double ) );
3629: /*
3630: Initialization.
3631: */
3632: machep = DBL_EPSILON;
3633: small = machep * machep;
3634: vsmall = small * small;
3635: large = 1.0 / small;
3636: vlarge = 1.0 / vsmall;
3637: m2 = sqrt ( machep );
3638: m4 = sqrt ( m2 );
3639: seed = 123456789;
3640: /*
3641: Heuristic numbers:
3642:
3643: If the axes may be badly scaled (which is to be avoided if
3644: possible), then set SCBD = 10. Otherwise set SCBD = 1.
3645:
3646: If the problem is known to be ill-conditioned, initialize ILLC = true.
3647:
3648: KTM is the number of iterations without improvement before the
3649: algorithm terminates. KTM = 4 is very cautious; usually KTM = 1
3650: is satisfactory.
3651: */
3652: scbd = 1.0;
3653: illc = 0;
3654: ktm = 1;
3655:
3656: if ( illc )
3657: {
3658: ldfac = 0.1;
3659: }
3660: else
3661: {
3662: ldfac = 0.01;
3663: }
3664:
3665: kt = 0;
3666: nl = 0;
3667: nf = 1;
3668: /* fx = f ( x, n ); */
3669: fx = (*func) ( (x-1) );/* This for func which is computed from x[1] and not from x[0] xm1=(x-1)*/
3670: qf1 = fx;
3671: t = small + fabs ( t0 );
3672: t2 = t;
3673: dmin = small;
3674: h = h0;
3675: h = fmax ( h, 100.0 * t );
3676: ldt = h;
3677: /*
3678: The initial set of search directions V is the identity matrix.
3679: */
3680: for ( j = 0; j < n; j++ )
3681: {
3682: for ( i = 0; i < n; i++ )
3683: {
3684: v[i+j*n] = 0.0;
3685: }
3686: v[j+j*n] = 1.0;
3687: }
3688:
3689: for ( i = 0; i < n; i++ )
3690: {
3691: d[i] = 0.0;
3692: }
3693: qa = 0.0;
3694: qb = 0.0;
3695: qc = 0.0;
3696: qd0 = 0.0;
3697: qd1 = 0.0;
3698: r8vec_copy ( n, x, q0 );
3699: r8vec_copy ( n, x, q1 );
3700:
3701: if ( 0 < prin )
3702: {
3703: print2 ( n, x, prin, fx, nf, nl );
3704: }
3705: /*
3706: The main loop starts here.
3707: */
3708: for ( ; ; )
3709: {
3710: biter++; /* Added to count the loops */
3711: printf("\n Big iteration %d \n",biter);
3712: sf = d[0];
3713: d[0] = 0.0;
3714: /*
3715: Minimize along the first direction V(*,1).
3716: */
3717: jsearch = 0;
3718: nits = 2;
3719: d2 = d[0];
3720: s = 0.0;
3721: value = fx;
3722: fk = 0;
3723:
3724: minny ( n, jsearch, nits, &d2, &s, &value, fk, func, x, t,
3725: h, v, q0, q1, &nl, &nf, dmin, ldt, &fx, &qa, &qb, &qc, &qd0, &qd1 );
3726: /* minny ( n, jsearch, nits, &d2, &s, &value, fk, func, x, t, */
3727: /* h, v, q0, q1, &nl, &nf, dmin, ldt, &fx, &qa, &qb, &qc, &qd0, &qd1 ); */
3728:
3729: d[0] = d2;
3730:
3731: if ( s <= 0.0 )
3732: {
3733: for ( i = 0; i < n; i++ )
3734: {
3735: v[i+0*n] = - v[i+0*n];
3736: }
3737: }
3738:
3739: if ( sf <= 0.9 * d[0] || d[0] <= 0.9 * sf )
3740: {
3741: for ( i = 1; i < n; i++ )
3742: {
3743: d[i] = 0.0;
3744: }
3745: }
3746: /*
3747: The inner loop starts here.
3748: */
3749: for ( k = 2; k <= n; k++ )
3750: {
3751: r8vec_copy ( n, x, y );
3752:
3753: sf = fx;
3754:
3755: if ( 0 < kt )
3756: {
3757: illc = 1;
3758: }
3759:
3760: for ( ; ; )
3761: {
3762: kl = k;
3763: df = 0.0;
3764: /*
3765: A random step follows, to avoid resolution valleys.
3766: */
3767: if ( illc )
3768: {
3769: for ( j = 0; j < n; j++ )
3770: {
3771: r = r8_uniform_01 ( &seed );
3772: s = ( 0.1 * ldt + t2 * pow ( 10.0, kt ) ) * ( r - 0.5 );
3773: z[j] = s;
3774: for ( i = 0; i < n; i++ )
3775: {
3776: x[i] = x[i] + s * v[i+j*n];
3777: }
3778: }
3779:
3780: fx = (*func) ( (x-1) );/* This for func which is computed from x[1] and not from x[0] xm1=(x-1)*/
3781: /* fx = f ( x, n ); */
3782: nf = nf + 1;
3783: }
3784: /*
3785: Minimize along the "non-conjugate" directions V(*,K),...,V(*,N).
3786: */
3787: for ( k2 = k; k2 <= n; k2++ )
3788: {
3789: sl = fx;
3790:
3791: jsearch = k2 - 1;
3792: nits = 2;
3793: d2 = d[k2-1];
3794: s = 0.0;
3795: value = fx;
3796: fk = 0;
3797:
3798: minny ( n, jsearch, nits, &d2, &s, &value, fk, func, x, t,
3799: h, v, q0, q1, &nl, &nf, dmin, ldt, &fx, &qa, &qb, &qc, &qd0, &qd1 );
3800: /* minny ( n, jsearch, nits, &d2, &s, &value, fk, f, x, t, */
3801: /* h, v, q0, q1, &nl, &nf, dmin, ldt, &fx, &qa, &qb, &qc, &qd0, &qd1 ); */
3802:
3803: d[k2-1] = d2;
3804:
3805: if ( illc )
3806: {
3807: s = d[k2-1] * pow ( s + z[k2-1], 2 );
3808: }
3809: else
3810: {
3811: s = sl - fx;
3812: }
3813:
3814: if ( df <= s )
3815: {
3816: df = s;
3817: kl = k2;
3818: }
3819: }
3820: /*
3821: If there was not much improvement on the first try, set
3822: ILLC = true and start the inner loop again.
3823: */
3824: if ( illc )
3825: {
3826: break;
3827: }
3828: printf("\n fabs( 100.0 * machep(=%.12lf) * fx(=%.12lf) ) <=? df(=%.12lf)\n", machep, fx, df);
3829: if ( fabs ( 100.0 * machep * fx ) <= df )
3830: {
3831: break;
3832: }
3833: illc = 1;
3834: }
3835:
3836: if ( k == 2 && 1 < prin )
3837: {
3838: r8vec_print ( n, d, " The second difference array:" );
3839: }
3840: /*
3841: Minimize along the "conjugate" directions V(*,1),...,V(*,K-1).
3842: */
3843: for ( k2 = 1; k2 < k; k2++ )
3844: {
3845: jsearch = k2 - 1;
3846: nits = 2;
3847: d2 = d[k2-1];
3848: s = 0.0;
3849: value = fx;
3850: fk = 0;
3851:
3852: minny ( n, jsearch, nits, &d2, &s, &value, fk, func, x, t,
3853: h, v, q0, q1, &nl, &nf, dmin, ldt, &fx, &qa, &qb, &qc, &qd0, &qd1 );
3854: /* minny ( n, jsearch, nits, &d2, &s, &value, fk, f, x, t, */
3855: /* h, v, q0, q1, &nl, &nf, dmin, ldt, &fx, &qa, &qb, &qc, &qd0, &qd1 ); */
3856:
3857: d[k2-1] = d2;
3858: }
3859:
3860: f1 = fx;
3861: fx = sf;
3862:
3863: for ( i = 0; i < n; i++ )
3864: {
3865: temp = x[i];
3866: x[i] = y[i];
3867: y[i] = temp - y[i];
3868: }
3869:
3870: lds = r8vec_norm ( n, y );
3871: /*
3872: Discard direction V(*,kl).
3873:
3874: If no random step was taken, V(*,KL) is the "non-conjugate"
3875: direction along which the greatest improvement was made.
3876: */
3877: if ( small < lds )
3878: {
3879: for ( j = kl - 1; k <= j; j-- )
3880: {
3881: for ( i = 1; i <= n; i++ )
3882: {
3883: v[i-1+j*n] = v[i-1+(j-1)*n];
3884: }
3885: d[j] = d[j-1];
3886: }
3887:
3888: d[k-1] = 0.0;
3889:
3890: for ( i = 1; i <= n; i++ )
3891: {
3892: v[i-1+(k-1)*n] = y[i-1] / lds;
3893: }
3894: /*
3895: Minimize along the new "conjugate" direction V(*,k), which is
3896: the normalized vector: (new x) - (old x).
3897: */
3898: jsearch = k - 1;
3899: nits = 4;
3900: d2 = d[k-1];
3901: value = f1;
3902: fk = 1;
3903:
3904: minny ( n, jsearch, nits, &d2, &lds, &value, fk, func, x, t,
3905: h, v, q0, q1, &nl, &nf, dmin, ldt, &fx, &qa, &qb, &qc, &qd0, &qd1 );
3906: /* minny ( n, jsearch, nits, &d2, &lds, &value, fk, f, x, t, */
3907: /* h, v, q0, q1, &nl, &nf, dmin, ldt, &fx, &qa, &qb, &qc, &qd0, &qd1 ); */
3908:
3909: d[k-1] = d2;
3910:
3911: if ( lds <= 0.0 )
3912: {
3913: lds = - lds;
3914: for ( i = 1; i <= n; i++ )
3915: {
3916: v[i-1+(k-1)*n] = - v[i-1+(k-1)*n];
3917: }
3918: }
3919: }
3920:
3921: ldt = ldfac * ldt;
3922: ldt = fmax ( ldt, lds );
3923:
3924: if ( 0 < prin )
3925: {
3926: printf(" k=%d",k);
3927: print2 ( n, x, prin, fx, nf, nl );
3928: }
3929:
3930: t2 = r8vec_norm ( n, x );
3931:
3932: t2 = m2 * t2 + t;
3933: /*
3934: See whether the length of the step taken since starting the
3935: inner loop exceeds half the tolerance.
3936: */
3937: if ( 0.5 * t2 < ldt )
3938: {
3939: kt = - 1;
3940: }
3941:
3942: kt = kt + 1;
3943:
3944: if ( ktm < kt )
3945: {
3946: if ( 0 < prin )
3947: {
3948: r8vec_print ( n, x, " X:" );
3949: }
3950:
3951: free ( d );
3952: free ( q0 );
3953: free ( q1 );
3954: free ( v );
3955: free ( y );
3956: free ( z );
3957:
3958: return fx;
3959: }
3960: }
3961: /*
3962: The inner loop ends here.
3963:
3964: Try quadratic extrapolation in case we are in a curved valley.
3965: */
3966: quad ( n, func, x, t, h, v, q0, q1, &nl, &nf, dmin, ldt, &fx, &qf1,
3967: &qa, &qb, &qc, &qd0, &qd1 );
3968: /* quad ( n, f, x, t, h, v, q0, q1, &nl, &nf, dmin, ldt, &fx, &qf1, */
3969: /* &qa, &qb, &qc, &qd0, &qd1 ); */
3970:
3971: for ( j = 0; j < n; j++ )
3972: {
3973: d[j] = 1.0 / sqrt ( d[j] );
3974: }
3975:
3976: dn = r8vec_max ( n, d );
3977:
3978: if ( 3 < prin )
3979: {
3980: r8mat_print ( n, n, v, " The new direction vectors:" );
3981: }
3982:
3983: for ( j = 0; j < n; j++ )
3984: {
3985: for ( i = 0; i < n; i++ )
3986: {
3987: v[i+j*n] = ( d[j] / dn ) * v[i+j*n];
3988: }
3989: }
3990: /*
3991: Scale the axes to try to reduce the condition number.
3992: */
3993: if ( 1.0 < scbd )
3994: {
3995: for ( i = 0; i < n; i++ )
3996: {
3997: s = 0.0;
3998: for ( j = 0; j < n; j++ )
3999: {
4000: s = s + v[i+j*n] * v[i+j*n];
4001: }
4002: s = sqrt ( s );
4003: z[i] = fmax ( m4, s );
4004: }
4005:
4006: s = r8vec_min ( n, z );
4007:
4008: for ( i = 0; i < n; i++ )
4009: {
4010: sl = s / z[i];
4011: z[i] = 1.0 / sl;
4012:
4013: if ( scbd < z[i] )
4014: {
4015: sl = 1.0 / scbd;
4016: z[i] = scbd;
4017: }
4018: for ( j = 0; j < n; j++ )
4019: {
4020: v[i+j*n] = sl * v[i+j*n];
4021: }
4022: }
4023: }
4024: /*
4025: Calculate a new set of orthogonal directions before repeating
4026: the main loop.
4027:
4028: Transpose V for MINFIT:
4029: */
4030: printf(" Calculate a new set of orthogonal directions before repeating the main loop.\n Transpose V for MINFIT:...\n");
4031: r8mat_transpose_in_place ( n, v );
4032: /*
4033: MINFIT finds the singular value decomposition of V.
4034:
4035: This gives the principal values and principal directions of the
4036: approximating quadratic form without squaring the condition number.
4037: */
4038: printf(" MINFIT finds the singular value decomposition of V. \n This gives the principal values and principal directions of the\n approximating quadratic form without squaring the condition number...\n");
4039: minfit ( n, vsmall, v, d );
4040: /*
4041: Unscale the axes.
4042: */
4043: printf(" Unscale the axes.\n");
4044: if ( 1.0 < scbd )
4045: {
4046: for ( i = 0; i < n; i++ )
4047: {
4048: for ( j = 0; j < n; j++ )
4049: {
4050: v[i+j*n] = z[i] * v[i+j*n];
4051: }
4052: }
4053:
4054: for ( j = 0; j < n; j++ )
4055: {
4056: s = 0.0;
4057: for ( i = 0; i < n; i++ )
4058: {
4059: s = s + v[i+j*n] * v[i+j*n];
4060: }
4061: s = sqrt ( s );
4062:
4063: d[j] = s * d[j];
4064: for ( i = 0; i < n; i++ )
4065: {
4066: v[i+j*n] = v[i+j*n] / s;
4067: }
4068: }
4069: }
4070:
4071: for ( i = 0; i < n; i++ )
4072: {
4073: dni = dn * d[i];
4074:
4075: if ( large < dni )
4076: {
4077: d[i] = vsmall;
4078: }
4079: else if ( dni < small )
4080: {
4081: d[i] = vlarge;
4082: }
4083: else
4084: {
4085: d[i] = 1.0 / dni / dni;
4086: }
4087: }
4088: /*
4089: Sort the eigenvalues and eigenvectors.
4090: */
4091: printf(" Sort the eigenvalues and eigenvectors....\n");
4092: svsort ( n, d, v );
4093: /*
4094: Determine the smallest eigenvalue.
4095: */
4096: printf(" Determine the smallest eigenvalue.\n");
4097: dmin = fmax ( d[n-1], small );
4098: /*
4099: The ratio of the smallest to largest eigenvalue determines whether
4100: the system is ill conditioned.
4101: */
4102:
4103: if ( dmin < m2 * d[0] )
4104: {
4105: illc = 1;
4106: }
4107: else
4108: {
4109: illc = 0;
4110: }
4111: printf(" The ratio of the smallest to largest eigenvalue determines whether\n the system is ill conditioned=%d . dmin=%.12lf < m2=%.12lf * d[0]=%.12lf \n",illc, dmin,m2, d[0]);
4112:
4113: if ( 1 < prin )
4114: {
4115: if ( 1.0 < scbd )
4116: {
4117: r8vec_print ( n, z, " The scale factors:" );
4118: }
4119: r8vec_print ( n, d, " Principal values of the quadratic form:" );
4120: }
4121:
4122: if ( 3 < prin )
4123: {
4124: r8mat_print ( n, n, v, " The principal axes:" );
4125: }
4126: /*
4127: The main loop ends here.
4128: */
4129: }
4130:
4131: if ( 0 < prin )
4132: {
4133: r8vec_print ( n, x, " X:" );
4134: }
4135: /*
4136: Free memory.
4137: */
4138: free ( d );
4139: free ( q0 );
4140: free ( q1 );
4141: free ( v );
4142: free ( y );
4143: free ( z );
4144:
4145: return fx;
4146: }
4147: /******************************************************************************/
4148:
4149: void print2 ( int n, double x[], int prin, double fx, int nf, int nl )
4150:
4151: /******************************************************************************/
4152: /*
4153: Purpose:
4154:
4155: PRINT2 prints certain data about the progress of the iteration.
4156:
4157: Licensing:
4158:
4159: This code is distributed under the GNU LGPL license.
4160:
4161: Modified:
4162:
4163: 28 July 2016
4164:
4165: Author:
4166:
4167: Original FORTRAN77 version by Richard Brent.
4168: C version by John Burkardt.
4169:
4170: Reference:
4171:
4172: Richard Brent,
4173: Algorithms for Minimization with Derivatives,
4174: Prentice Hall, 1973,
4175: Reprinted by Dover, 2002.
4176:
4177: Parameters:
4178:
4179: Input, int N, the number of variables.
4180:
4181: Input, double X[N], the current estimate of the minimizer.
4182:
4183: Input, int PRIN, the user-specifed print level.
4184: 0, nothing is printed.
4185: 1, F is printed after every n+1 or n+2 linear minimizations.
4186: final X is printed, but intermediate X is printed only
4187: if N is at most 4.
4188: 2, the scale factors and the principal values of the approximating
4189: quadratic form are also printed.
4190: 3, X is also printed after every few linear minimizations.
4191: 4, the principal vectors of the approximating quadratic form are
4192: also printed.
4193:
4194: Input, double FX, the smallest value of F(X) found so far.
4195:
4196: Input, int NF, the number of function evaluations.
4197:
4198: Input, int NL, the number of linear searches.
4199: */
4200: {
4201: printf ( "\n" );
4202: printf ( " Linear searches %d", nl );
4203: /* printf ( " Linear searches %d\n", nl ); */
4204: /* printf ( " Function evaluations %d\n", nf ); */
4205: /* printf ( " Function value FX = %g\n", fx ); */
4206: printf ( " Function evaluations %d", nf );
4207: printf ( " Function value FX = %.12lf\n", fx );
4208:
4209: if ( n <= 4 || 2 < prin )
4210: {
4211: r8vec_print ( n, x, " X:" );
4212: }
4213:
4214: return;
4215: }
4216: /******************************************************************************/
4217:
4218: void quad ( int n, double (*func) ( double [] ), double x[], double t,
4219: double h, double v[], double q0[], double q1[], int *nl, int *nf, double dmin,
4220: double ldt, double *fx, double *qf1, double *qa, double *qb, double *qc,
4221: double *qd0, double *qd1 )
4222: /* void quad ( int n, double f ( double x[], int n ), double x[], double t, */
4223: /* double h, double v[], double q0[], double q1[], int *nl, int *nf, double dmin, */
4224: /* double ldt, double *fx, double *qf1, double *qa, double *qb, double *qc, */
4225: /* double *qd0, double *qd1 ) */
4226:
4227: /******************************************************************************/
4228: /*
4229: Purpose:
4230:
4231: QUAD seeks to minimize the scalar function F along a particular curve.
4232:
4233: Discussion:
4234:
4235: The minimizer to be sought is required to lie on a curve defined
4236: by Q0, Q1 and X.
4237:
4238: Licensing:
4239:
4240: This code is distributed under the GNU LGPL license.
4241:
4242: Modified:
4243:
4244: 30 July 2016
4245:
4246: Author:
4247:
4248: Original FORTRAN77 version by Richard Brent.
4249: C version by John Burkardt.
4250:
4251: Reference:
4252:
4253: Richard Brent,
4254: Algorithms for Minimization with Derivatives,
4255: Prentice Hall, 1973,
4256: Reprinted by Dover, 2002.
4257:
4258: Parameters:
4259:
4260: Input, int N, the number of variables.
4261:
4262: Input, double F ( double X[], int N ), the name of the function to
4263: be minimized.
4264:
4265: Input/output, double X[N], ?
4266:
4267: Input, double T, ?
4268:
4269: Input, double H, ?
4270:
4271: Input, double V[N,N], the matrix of search directions.
4272:
4273: Input/output, double Q0[N], Q1[N], two auxiliary points used to define
4274: a curve through X.
4275:
4276: Input/output, int *NL, the number of linear searches.
4277:
4278: Input/output, int *NF, the number of function evaluations.
4279:
4280: Input, double DMIN, an estimate for the smallest eigenvalue.
4281:
4282: Input, double LDT, the length of the step.
4283:
4284: Input/output, double *FX, the value of F(X,N).
4285:
4286: Input/output, double *QF1, *QA, *QB, *QC, *QD0, *QD1 ?
4287: */
4288: {
4289: int fk;
4290: int i;
4291: int jsearch;
4292: double l;
4293: int nits;
4294: double s;
4295: double temp;
4296: double value;
4297:
4298: temp = *fx;
4299: *fx = *qf1;
4300: *qf1 = temp;
4301:
4302: for ( i = 0; i < n; i++ )
4303: {
4304: temp = x[i];
4305: x[i] = q1[i];
4306: q1[i] = temp;
4307: }
4308:
4309: *qd1 = 0.0;
4310: for ( i = 0; i < n; i++ )
4311: {
4312: *qd1 = *qd1 + ( x[i] - q1[i] ) * ( x[i] - q1[i] );
4313: }
4314: *qd1 = sqrt ( *qd1 );
4315:
4316: if ( *qd0 <= 0.0 || *qd1 <= 0.0 || *nl < 3 * n * n )
4317: {
4318: *fx = *qf1;
4319: *qa = 0.0;
4320: *qb = 0.0;
4321: *qc = 1.0;
4322: s = 0.0;
4323: }
4324: else
4325: {
4326: jsearch = - 1;
4327: nits = 2;
4328: s = 0.0;
4329: l = *qd1;
4330: value = *qf1;
4331: fk = 1;
4332:
4333: minny ( n, jsearch, nits, &s, &l, &value, fk, func, x, t,
4334: h, v, q0, q1, nl, nf, dmin, ldt, fx, qa, qb, qc, qd0, qd1 );
4335: /* minny ( n, jsearch, nits, &s, &l, &value, fk, f, x, t, */
4336: /* h, v, q0, q1, nl, nf, dmin, ldt, fx, qa, qb, qc, qd0, qd1 ); */
4337:
4338: *qa = l * ( l - *qd1 ) / ( *qd0 + *qd1 ) / *qd0;
4339: *qb = - ( l + *qd0 ) * ( l - *qd1 ) / *qd1 / *qd0;
4340: *qc = ( l + *qd0 ) * l / *qd1 / ( *qd0 + *qd1 );
4341: }
4342:
4343: *qd0 = *qd1;
4344:
4345: for ( i = 0; i < n; i++ )
4346: {
4347: s = q0[i];
4348: q0[i] = x[i];
4349: x[i] = *qa * s + *qb * x[i] + *qc * q1[i];
4350: }
4351:
4352: return;
4353: }
4354: /******************************************************************************/
4355:
4356: double r8_hypot ( double x, double y )
4357:
4358: /******************************************************************************/
4359: /*
4360: Purpose:
4361:
4362: R8_HYPOT returns the value of sqrt ( X^2 + Y^2 ).
4363:
4364: Licensing:
4365:
4366: This code is distributed under the GNU LGPL license.
4367:
4368: Modified:
4369:
4370: 26 March 2012
4371:
4372: Author:
4373:
4374: John Burkardt
4375:
4376: Parameters:
4377:
4378: Input, double X, Y, the arguments.
4379:
4380: Output, double R8_HYPOT, the value of sqrt ( X^2 + Y^2 ).
4381: */
4382: {
4383: double a;
4384: double b;
4385: double value;
4386:
4387: if ( fabs ( x ) < fabs ( y ) )
4388: {
4389: a = fabs ( y );
4390: b = fabs ( x );
4391: }
4392: else
4393: {
4394: a = fabs ( x );
4395: b = fabs ( y );
4396: }
4397: /*
4398: A contains the larger value.
4399: */
4400: if ( a == 0.0 )
4401: {
4402: value = 0.0;
4403: }
4404: else
4405: {
4406: value = a * sqrt ( 1.0 + ( b / a ) * ( b / a ) );
4407: }
4408:
4409: return value;
4410: }
4411: /******************************************************************************/
4412:
4413: double r8_uniform_01 ( int *seed )
4414:
4415: /******************************************************************************/
4416: /*
4417: Purpose:
4418:
4419: R8_UNIFORM_01 returns a pseudorandom R8 scaled to [0,1].
4420:
4421: Discussion:
4422:
4423: This routine implements the recursion
4424:
4425: seed = 16807 * seed mod ( 2^31 - 1 )
4426: r8_uniform_01 = seed / ( 2^31 - 1 )
4427:
4428: The integer arithmetic never requires more than 32 bits,
4429: including a sign bit.
4430:
4431: If the initial seed is 12345, then the first three computations are
4432:
4433: Input Output R8_UNIFORM_01
4434: SEED SEED
4435:
4436: 12345 207482415 0.096616
4437: 207482415 1790989824 0.833995
4438: 1790989824 2035175616 0.947702
4439:
4440: Licensing:
4441:
4442: This code is distributed under the GNU LGPL license.
4443:
4444: Modified:
4445:
4446: 11 August 2004
4447:
4448: Author:
4449:
4450: John Burkardt
4451:
4452: Reference:
4453:
4454: Paul Bratley, Bennett Fox, Linus Schrage,
4455: A Guide to Simulation,
4456: Springer Verlag, pages 201-202, 1983.
4457:
4458: Pierre L'Ecuyer,
4459: Random Number Generation,
4460: in Handbook of Simulation
4461: edited by Jerry Banks,
4462: Wiley Interscience, page 95, 1998.
4463:
4464: Bennett Fox,
4465: Algorithm 647:
4466: Implementation and Relative Efficiency of Quasirandom
4467: Sequence Generators,
4468: ACM Transactions on Mathematical Software,
4469: Volume 12, Number 4, pages 362-376, 1986.
4470:
4471: P A Lewis, A S Goodman, J M Miller,
4472: A Pseudo-Random Number Generator for the System/360,
4473: IBM Systems Journal,
4474: Volume 8, pages 136-143, 1969.
4475:
4476: Parameters:
4477:
4478: Input/output, int *SEED, the "seed" value. Normally, this
4479: value should not be 0. On output, SEED has been updated.
4480:
4481: Output, double R8_UNIFORM_01, a new pseudorandom variate, strictly between
4482: 0 and 1.
4483: */
4484: {
4485: const int i4_huge = 2147483647;
4486: int k;
4487: double r;
4488:
4489: if ( *seed == 0 )
4490: {
4491: fprintf ( stderr, "\n" );
4492: fprintf ( stderr, "R8_UNIFORM_01 - Fatal error!\n" );
4493: fprintf ( stderr, " Input value of SEED = 0\n" );
4494: exit ( 1 );
4495: }
4496:
4497: k = *seed / 127773;
4498:
4499: *seed = 16807 * ( *seed - k * 127773 ) - k * 2836;
4500:
4501: if ( *seed < 0 )
4502: {
4503: *seed = *seed + i4_huge;
4504: }
4505:
4506: r = ( ( double ) ( *seed ) ) * 4.656612875E-10;
4507:
4508: return r;
4509: }
4510: /******************************************************************************/
4511:
4512: void r8mat_print ( int m, int n, double a[], char *title )
4513:
4514: /******************************************************************************/
4515: /*
4516: Purpose:
4517:
4518: R8MAT_PRINT prints an R8MAT.
4519:
4520: Discussion:
4521:
4522: An R8MAT is a doubly dimensioned array of R8 values, stored as a vector
4523: in column-major order.
4524:
4525: Entry A(I,J) is stored as A[I+J*M]
4526:
4527: Licensing:
4528:
4529: This code is distributed under the GNU LGPL license.
4530:
4531: Modified:
4532:
4533: 28 May 2008
4534:
4535: Author:
4536:
4537: John Burkardt
4538:
4539: Parameters:
4540:
4541: Input, int M, the number of rows in A.
4542:
4543: Input, int N, the number of columns in A.
4544:
4545: Input, double A[M*N], the M by N matrix.
4546:
4547: Input, char *TITLE, a title.
4548: */
4549: {
4550: r8mat_print_some ( m, n, a, 1, 1, m, n, title );
4551:
4552: return;
4553: }
4554: /******************************************************************************/
4555:
4556: void r8mat_print_some ( int m, int n, double a[], int ilo, int jlo, int ihi,
4557: int jhi, char *title )
4558:
4559: /******************************************************************************/
4560: /*
4561: Purpose:
4562:
4563: R8MAT_PRINT_SOME prints some of an R8MAT.
4564:
4565: Discussion:
4566:
4567: An R8MAT is a doubly dimensioned array of R8 values, stored as a vector
4568: in column-major order.
4569:
4570: Licensing:
4571:
4572: This code is distributed under the GNU LGPL license.
4573:
4574: Modified:
4575:
4576: 26 June 2013
4577:
4578: Author:
4579:
4580: John Burkardt
4581:
4582: Parameters:
4583:
4584: Input, int M, the number of rows of the matrix.
4585: M must be positive.
4586:
4587: Input, int N, the number of columns of the matrix.
4588: N must be positive.
4589:
4590: Input, double A[M*N], the matrix.
4591:
4592: Input, int ILO, JLO, IHI, JHI, designate the first row and
4593: column, and the last row and column to be printed.
4594:
4595: Input, char *TITLE, a title.
4596: */
4597: {
4598: # define INCX 5
4599:
4600: int i;
4601: int i2hi;
4602: int i2lo;
4603: int j;
4604: int j2hi;
4605: int j2lo;
4606:
4607: fprintf ( stdout, "\n" );
4608: fprintf ( stdout, "%s\n", title );
4609:
4610: if ( m <= 0 || n <= 0 )
4611: {
4612: fprintf ( stdout, "\n" );
4613: fprintf ( stdout, " (None)\n" );
4614: return;
4615: }
4616: /*
4617: Print the columns of the matrix, in strips of 5.
4618: */
4619: for ( j2lo = jlo; j2lo <= jhi; j2lo = j2lo + INCX )
4620: {
4621: j2hi = j2lo + INCX - 1;
4622: if ( n < j2hi )
4623: {
4624: j2hi = n;
4625: }
4626: if ( jhi < j2hi )
4627: {
4628: j2hi = jhi;
4629: }
4630:
4631: fprintf ( stdout, "\n" );
4632: /*
4633: For each column J in the current range...
4634:
4635: Write the header.
4636: */
4637: fprintf ( stdout, " Col: ");
4638: for ( j = j2lo; j <= j2hi; j++ )
4639: {
4640: fprintf ( stdout, " %7d ", j - 1 );
4641: }
4642: fprintf ( stdout, "\n" );
4643: fprintf ( stdout, " Row\n" );
4644: fprintf ( stdout, "\n" );
4645: /*
4646: Determine the range of the rows in this strip.
4647: */
4648: if ( 1 < ilo )
4649: {
4650: i2lo = ilo;
4651: }
4652: else
4653: {
4654: i2lo = 1;
4655: }
4656: if ( m < ihi )
4657: {
4658: i2hi = m;
4659: }
4660: else
4661: {
4662: i2hi = ihi;
4663: }
4664:
4665: for ( i = i2lo; i <= i2hi; i++ )
4666: {
4667: /*
4668: Print out (up to) 5 entries in row I, that lie in the current strip.
4669: */
4670: fprintf ( stdout, "%5d:", i - 1 );
4671: for ( j = j2lo; j <= j2hi; j++ )
4672: {
4673: fprintf ( stdout, " %14g", a[i-1+(j-1)*m] );
4674: }
4675: fprintf ( stdout, "\n" );
4676: }
4677: }
4678:
4679: return;
4680: # undef INCX
4681: }
4682: /******************************************************************************/
4683:
4684: void r8mat_transpose_in_place ( int n, double a[] )
4685:
4686: /******************************************************************************/
4687: /*
4688: Purpose:
4689:
4690: R8MAT_TRANSPOSE_IN_PLACE transposes a square matrix in place.
4691:
4692: Discussion:
4693:
4694: An R8MAT is a doubly dimensioned array of R8 values, stored as a vector
4695: in column-major order.
4696:
4697: Licensing:
4698:
4699: This code is distributed under the GNU LGPL license.
4700:
4701: Modified:
4702:
4703: 26 June 2008
4704:
4705: Author:
4706:
4707: John Burkardt
4708:
4709: Parameters:
4710:
4711: Input, int N, the number of rows and columns of the matrix A.
4712:
4713: Input/output, double A[N*N], the matrix to be transposed.
4714: */
4715: {
4716: int i;
4717: int j;
4718: double t;
4719:
4720: for ( j = 0; j < n; j++ )
4721: {
4722: for ( i = 0; i < j; i++ )
4723: {
4724: t = a[i+j*n];
4725: a[i+j*n] = a[j+i*n];
4726: a[j+i*n] = t;
4727: }
4728: }
4729: return;
4730: }
4731: /******************************************************************************/
4732:
4733: void r8vec_copy ( int n, double a1[], double a2[] )
4734:
4735: /******************************************************************************/
4736: /*
4737: Purpose:
4738:
4739: R8VEC_COPY copies an R8VEC.
4740:
4741: Discussion:
4742:
4743: An R8VEC is a vector of R8's.
4744:
4745: Licensing:
4746:
4747: This code is distributed under the GNU LGPL license.
4748:
4749: Modified:
4750:
4751: 03 July 2005
4752:
4753: Author:
4754:
4755: John Burkardt
4756:
4757: Parameters:
4758:
4759: Input, int N, the number of entries in the vectors.
4760:
4761: Input, double A1[N], the vector to be copied.
4762:
4763: Input, double A2[N], the copy of A1.
4764: */
4765: {
4766: int i;
4767:
4768: for ( i = 0; i < n; i++ )
4769: {
4770: a2[i] = a1[i];
4771: }
4772: return;
4773: }
4774: /******************************************************************************/
4775:
4776: double r8vec_max ( int n, double r8vec[] )
4777:
4778: /******************************************************************************/
4779: /*
4780: Purpose:
4781:
4782: R8VEC_MAX returns the value of the maximum element in a R8VEC.
4783:
4784: Licensing:
4785:
4786: This code is distributed under the GNU LGPL license.
4787:
4788: Modified:
4789:
4790: 05 May 2006
4791:
4792: Author:
4793:
4794: John Burkardt
4795:
4796: Parameters:
4797:
4798: Input, int N, the number of entries in the array.
4799:
4800: Input, double R8VEC[N], a pointer to the first entry of the array.
4801:
4802: Output, double R8VEC_MAX, the value of the maximum element. This
4803: is set to 0.0 if N <= 0.
4804: */
4805: {
4806: int i;
4807: double value;
4808:
4809: if ( n <= 0 )
4810: {
4811: value = 0.0;
4812: return value;
4813: }
4814:
4815: value = r8vec[0];
4816:
4817: for ( i = 1; i < n; i++ )
4818: {
4819: if ( value < r8vec[i] )
4820: {
4821: value = r8vec[i];
4822: }
4823: }
4824: return value;
4825: }
4826: /******************************************************************************/
4827:
4828: double r8vec_min ( int n, double r8vec[] )
4829:
4830: /******************************************************************************/
4831: /*
4832: Purpose:
4833:
4834: R8VEC_MIN returns the value of the minimum element in a R8VEC.
4835:
4836: Licensing:
4837:
4838: This code is distributed under the GNU LGPL license.
4839:
4840: Modified:
4841:
4842: 05 May 2006
4843:
4844: Author:
4845:
4846: John Burkardt
4847:
4848: Parameters:
4849:
4850: Input, int N, the number of entries in the array.
4851:
4852: Input, double R8VEC[N], the array to be checked.
4853:
4854: Output, double R8VEC_MIN, the value of the minimum element.
4855: */
4856: {
4857: int i;
4858: double value;
4859:
4860: value = r8vec[0];
4861:
4862: for ( i = 1; i < n; i++ )
4863: {
4864: if ( r8vec[i] < value )
4865: {
4866: value = r8vec[i];
4867: }
4868: }
4869: return value;
4870: }
4871: /******************************************************************************/
4872:
4873: double r8vec_norm ( int n, double a[] )
4874:
4875: /******************************************************************************/
4876: /*
4877: Purpose:
4878:
4879: R8VEC_NORM returns the L2 norm of an R8VEC.
4880:
4881: Discussion:
4882:
4883: The vector L2 norm is defined as:
4884:
4885: R8VEC_NORM = sqrt ( sum ( 1 <= I <= N ) A(I)^2 ).
4886:
4887: Licensing:
4888:
4889: This code is distributed under the GNU LGPL license.
4890:
4891: Modified:
4892:
4893: 01 March 2003
4894:
4895: Author:
4896:
4897: John Burkardt
4898:
4899: Parameters:
4900:
4901: Input, int N, the number of entries in A.
4902:
4903: Input, double A[N], the vector whose L2 norm is desired.
4904:
4905: Output, double R8VEC_NORM, the L2 norm of A.
4906: */
4907: {
4908: int i;
4909: double v;
4910:
4911: v = 0.0;
4912:
4913: for ( i = 0; i < n; i++ )
4914: {
4915: v = v + a[i] * a[i];
4916: }
4917: v = sqrt ( v );
4918:
4919: return v;
4920: }
4921: /******************************************************************************/
4922:
4923: void r8vec_print ( int n, double a[], char *title )
4924:
4925: /******************************************************************************/
4926: /*
4927: Purpose:
4928:
4929: R8VEC_PRINT prints an R8VEC.
4930:
4931: Discussion:
4932:
4933: An R8VEC is a vector of R8's.
4934:
4935: Licensing:
4936:
4937: This code is distributed under the GNU LGPL license.
4938:
4939: Modified:
4940:
4941: 08 April 2009
4942:
4943: Author:
4944:
4945: John Burkardt
4946:
4947: Parameters:
4948:
4949: Input, int N, the number of components of the vector.
4950:
4951: Input, double A[N], the vector to be printed.
4952:
4953: Input, char *TITLE, a title.
4954: */
4955: {
4956: int i,j, jk, k;
4957:
4958: double *p;
4959:
4960: p=(a-1); /* So that a[0]=p[1] */
4961: /* for (i=1;i<=n;i++) { */
4962: /* fprintf(ficrespow," %.12lf", p[i]); */
4963: /* } */
4964: /* fprintf(ficrespow,"\n");fflush(ficrespow); */
4965: printf("\n#model= 1 + age ");
4966: fprintf(ficlog,"\n#model= 1 + age ");
4967: if(nagesqr==1){
4968: printf(" + age*age ");
4969: fprintf(ficlog," + age*age ");
4970: }
4971: for(j=1;j <=ncovmodel-2;j++){
4972: if(Typevar[j]==0) {
4973: printf(" + V%d ",Tvar[j]);
4974: fprintf(ficlog," + V%d ",Tvar[j]);
4975: }else if(Typevar[j]==1) {
4976: printf(" + V%d*age ",Tvar[j]);
4977: fprintf(ficlog," + V%d*age ",Tvar[j]);
4978: }else if(Typevar[j]==2) {
4979: printf(" + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
4980: fprintf(ficlog," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
4981: }else if(Typevar[j]==3) {
4982: printf(" + V%d*V%d*age ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
4983: fprintf(ficlog," + V%d*V%d*age ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
4984: }
4985: }
4986: printf("\n");
4987: /* printf("12 47.0114589 0.0154322 33.2424412 0.3279905 2.3731903 */
4988: /* 13 -21.5392400 0.1118147 1.2680506 1.2973408 -1.0663662 */
4989: fprintf(ficlog,"\n");
4990: for(i=1,jk=1; i <=nlstate; i++){
4991: for(k=1; k <=(nlstate+ndeath); k++){
4992: if (k != i) {
4993: printf("%d%d ",i,k);
4994: fprintf(ficlog,"%d%d ",i,k);
4995: for(j=1; j <=ncovmodel; j++){
4996: printf("%12.7f ",p[jk]);
4997: fprintf(ficlog,"%12.7f ",p[jk]);
4998: jk++;
4999: }
5000: printf("\n");
5001: fprintf(ficlog,"\n");
5002: }
5003: }
5004: }
5005: /* fprintf ( stdout, "\n" ); */
5006: /* fprintf ( stdout, " %s\n", title ); */
5007: fprintf ( stdout, " %s", title );
5008: /* fprintf ( stdout, "\n" ); */
5009: for ( i = 0; i < n; i++ )
5010: {
5011: /* fprintf ( stdout, " %8d: %14g", i+1, a[i] ); */
5012: fprintf ( stdout, " %.12lf", a[i] );
5013: }
5014: fprintf ( stdout, "\n" );
5015: /* for ( i = 0; i < n; i++ ) */
5016: /* { */
5017: /* fprintf ( stdout, " %8d: %14g\n", i, a[i] ); */
5018: /* } */
5019:
5020: return;
5021: }
5022: /******************************************************************************/
5023:
5024: void svsort ( int n, double d[], double v[] )
5025:
5026: /******************************************************************************/
5027: /*
5028: Purpose:
5029:
5030: SVSORT descending sorts D and adjusts the corresponding columns of V.
5031:
5032: Discussion:
5033:
5034: A simple bubble sort is used on D.
5035:
5036: In our application, D contains singular values, and the columns of V are
5037: the corresponding right singular vectors.
5038:
5039: Licensing:
5040:
5041: This code is distributed under the GNU LGPL license.
5042:
5043: Modified:
5044:
5045: 28 July 2016
5046:
5047: Author:
5048:
5049: Original FORTRAN77 version by Richard Brent.
5050: C version by John Burkardt.
5051:
5052: Reference:
5053:
5054: Richard Brent,
5055: Algorithms for Minimization with Derivatives,
5056: Prentice Hall, 1973,
5057: Reprinted by Dover, 2002.
5058:
5059: Parameters:
5060:
5061: Input, int N, the length of D, and the order of V.
5062:
5063: Input/output, double D[N], the vector to be sorted.
5064: On output, the entries of D are in descending order.
5065:
5066: Input/output, double V[N,N], an N by N array to be adjusted
5067: as D is sorted. In particular, if the value that was in D(I) on input is
5068: moved to D(J) on output, then the input column V(*,I) is moved to
5069: the output column V(*,J).
5070: */
5071: {
5072: int i;
5073: int j1;
5074: int j2;
5075: int j3;
5076: double t;
5077:
5078: for ( j1 = 0; j1 < n - 1; j1++ )
5079: {
5080: /*
5081: Find J3, the index of the largest entry in D[J1:N-1].
5082: MAXLOC apparently requires its output to be an array.
5083: */
5084: j3 = j1;
5085: for ( j2 = j1 + 1; j2 < n; j2++ )
5086: {
5087: if ( d[j3] < d[j2] )
5088: {
5089: j3 = j2;
5090: }
5091: }
5092: /*
5093: If J1 != J3, swap D[J1] and D[J3], and columns J1 and J3 of V.
5094: */
5095: if ( j1 != j3 )
5096: {
5097: t = d[j1];
5098: d[j1] = d[j3];
5099: d[j3] = t;
5100: for ( i = 0; i < n; i++ )
5101: {
5102: t = v[i+j1*n];
5103: v[i+j1*n] = v[i+j3*n];
5104: v[i+j3*n] = t;
5105: }
5106: }
5107: }
5108:
5109: return;
5110: }
5111: /******************************************************************************/
5112:
5113: void timestamp ( )
5114:
5115: /******************************************************************************/
5116: /*
5117: Purpose:
5118:
5119: TIMESTAMP prints the current YMDHMS date as a time stamp.
5120:
5121: Example:
5122:
5123: 31 May 2001 09:45:54 AM
5124:
5125: Licensing:
5126:
5127: This code is distributed under the GNU LGPL license.
5128:
5129: Modified:
5130:
5131: 24 September 2003
5132:
5133: Author:
5134:
5135: John Burkardt
5136:
5137: Parameters:
5138:
5139: None
5140: */
5141: {
5142: # define TIME_SIZE 40
5143:
5144: static char time_buffer[TIME_SIZE];
5145: const struct tm *tm;
5146: time_t now;
5147:
5148: now = time ( NULL );
5149: tm = localtime ( &now );
5150:
5151: strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm );
5152:
5153: fprintf ( stdout, "%s\n", time_buffer );
5154:
5155: return;
5156: # undef TIME_SIZE
5157: }
5158: /* end praxis */
5159:
5160: /*************** powell ************************/
5161: /*
5162: Minimization of a function func of n variables. Input consists in an initial starting point
5163: p[1..n] ; an initial matrix xi[1..n][1..n] whose columns contain the initial set of di-
5164: rections (usually the n unit vectors); and ftol, the fractional tolerance in the function value
5165: such that failure to decrease by more than this amount in one iteration signals doneness. On
5166: output, p is set to the best point found, xi is the then-current direction set, fret is the returned
5167: function value at p , and iter is the number of iterations taken. The routine linmin is used.
5168: */
5169: #ifdef LINMINORIGINAL
5170: #else
5171: int *flatdir; /* Function is vanishing in that direction */
5172: int flat=0, flatd=0; /* Function is vanishing in that direction */
5173: #endif
5174: void powell(double p[], double **xi, int n, double ftol, int *iter, double *fret,
5175: double (*func)(double []))
5176: {
5177: #ifdef LINMINORIGINAL
5178: void linmin(double p[], double xi[], int n, double *fret,
5179: double (*func)(double []));
5180: #else
5181: void linmin(double p[], double xi[], int n, double *fret,
5182: double (*func)(double []),int *flat);
5183: #endif
5184: int i,ibig,j,jk,k;
5185: double del,t,*pt,*ptt,*xit;
5186: double directest;
5187: double fp,fptt;
5188: double *xits;
5189: int niterf, itmp;
5190:
5191: pt=vector(1,n);
5192: ptt=vector(1,n);
5193: xit=vector(1,n);
5194: xits=vector(1,n);
5195: *fret=(*func)(p);
5196: for (j=1;j<=n;j++) pt[j]=p[j];
5197: rcurr_time = time(NULL);
5198: fp=(*fret); /* Initialisation */
5199: for (*iter=1;;++(*iter)) {
5200: ibig=0;
5201: del=0.0;
5202: rlast_time=rcurr_time;
5203: /* (void) gettimeofday(&curr_time,&tzp); */
5204: rcurr_time = time(NULL);
5205: curr_time = *localtime(&rcurr_time);
5206: /* 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); */
5207: /* 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); */
5208: 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);
5209: 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);
5210: /* fprintf(ficrespow,"%d %.12f %ld",*iter,*fret,curr_time.tm_sec-start_time.tm_sec); */
5211: fp=(*fret); /* From former iteration or initial value */
5212: for (i=1;i<=n;i++) {
5213: fprintf(ficrespow," %.12lf", p[i]);
5214: }
5215: fprintf(ficrespow,"\n");fflush(ficrespow);
5216: printf("\n#model= 1 + age ");
5217: fprintf(ficlog,"\n#model= 1 + age ");
5218: if(nagesqr==1){
5219: printf(" + age*age ");
5220: fprintf(ficlog," + age*age ");
5221: }
5222: for(j=1;j <=ncovmodel-2;j++){
5223: if(Typevar[j]==0) {
5224: printf(" + V%d ",Tvar[j]);
5225: fprintf(ficlog," + V%d ",Tvar[j]);
5226: }else if(Typevar[j]==1) {
5227: printf(" + V%d*age ",Tvar[j]);
5228: fprintf(ficlog," + V%d*age ",Tvar[j]);
5229: }else if(Typevar[j]==2) {
5230: printf(" + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
5231: fprintf(ficlog," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
5232: }else if(Typevar[j]==3) {
5233: printf(" + V%d*V%d*age ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
5234: fprintf(ficlog," + V%d*V%d*age ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
5235: }
5236: }
5237: printf("\n");
5238: /* printf("12 47.0114589 0.0154322 33.2424412 0.3279905 2.3731903 */
5239: /* 13 -21.5392400 0.1118147 1.2680506 1.2973408 -1.0663662 */
5240: fprintf(ficlog,"\n");
5241: for(i=1,jk=1; i <=nlstate; i++){
5242: for(k=1; k <=(nlstate+ndeath); k++){
5243: if (k != i) {
5244: printf("%d%d ",i,k);
5245: fprintf(ficlog,"%d%d ",i,k);
5246: for(j=1; j <=ncovmodel; j++){
5247: printf("%12.7f ",p[jk]);
5248: fprintf(ficlog,"%12.7f ",p[jk]);
5249: jk++;
5250: }
5251: printf("\n");
5252: fprintf(ficlog,"\n");
5253: }
5254: }
5255: }
5256: if(*iter <=3 && *iter >1){
5257: tml = *localtime(&rcurr_time);
5258: strcpy(strcurr,asctime(&tml));
5259: rforecast_time=rcurr_time;
5260: itmp = strlen(strcurr);
5261: if(strcurr[itmp-1]=='\n') /* Windows outputs with a new line */
5262: strcurr[itmp-1]='\0';
5263: printf("\nConsidering the time needed for the last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
5264: fprintf(ficlog,"\nConsidering the time needed for this last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
5265: for(niterf=10;niterf<=30;niterf+=10){
5266: rforecast_time=rcurr_time+(niterf-*iter)*(rcurr_time-rlast_time);
5267: forecast_time = *localtime(&rforecast_time);
5268: strcpy(strfor,asctime(&forecast_time));
5269: itmp = strlen(strfor);
5270: if(strfor[itmp-1]=='\n')
5271: strfor[itmp-1]='\0';
5272: 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);
5273: 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);
5274: }
5275: }
5276: for (i=1;i<=n;i++) { /* For each direction i */
5277: for (j=1;j<=n;j++) xit[j]=xi[j][i]; /* Directions stored from previous iteration with previous scales */
5278: fptt=(*fret);
5279: #ifdef DEBUG
5280: printf("fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
5281: fprintf(ficlog, "fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
5282: #endif
5283: printf("%d",i);fflush(stdout); /* print direction (parameter) i */
5284: fprintf(ficlog,"%d",i);fflush(ficlog);
5285: #ifdef LINMINORIGINAL
5286: linmin(p,xit,n,fret,func); /* Point p[n]. xit[n] has been loaded for direction i as input.*/
5287: #else
5288: linmin(p,xit,n,fret,func,&flat); /* Point p[n]. xit[n] has been loaded for direction i as input.*/
5289: flatdir[i]=flat; /* Function is vanishing in that direction i */
5290: #endif
5291: /* Outputs are fret(new point p) p is updated and xit rescaled */
5292: if (fabs(fptt-(*fret)) > del) { /* We are keeping the max gain on each of the n directions */
5293: /* because that direction will be replaced unless the gain del is small */
5294: /* in comparison with the 'probable' gain, mu^2, with the last average direction. */
5295: /* Unless the n directions are conjugate some gain in the determinant may be obtained */
5296: /* with the new direction. */
5297: del=fabs(fptt-(*fret));
5298: ibig=i;
5299: }
5300: #ifdef DEBUG
5301: printf("%d %.12e",i,(*fret));
5302: fprintf(ficlog,"%d %.12e",i,(*fret));
5303: for (j=1;j<=n;j++) {
5304: xits[j]=FMAX(fabs(p[j]-pt[j]),1.e-5);
5305: printf(" x(%d)=%.12e",j,xit[j]);
5306: fprintf(ficlog," x(%d)=%.12e",j,xit[j]);
5307: }
5308: for(j=1;j<=n;j++) {
5309: printf(" p(%d)=%.12e",j,p[j]);
5310: fprintf(ficlog," p(%d)=%.12e",j,p[j]);
5311: }
5312: printf("\n");
5313: fprintf(ficlog,"\n");
5314: #endif
5315: } /* end loop on each direction i */
5316: /* Convergence test will use last linmin estimation (fret) and compare former iteration (fp) */
5317: /* But p and xit have been updated at the end of linmin, *fret corresponds to new p, xit */
5318: /* New value of last point Pn is not computed, P(n-1) */
5319: for(j=1;j<=n;j++) {
5320: if(flatdir[j] >0){
5321: printf(" p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
5322: fprintf(ficlog," p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
5323: }
5324: /* printf("\n"); */
5325: /* fprintf(ficlog,"\n"); */
5326: }
5327: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret))) { /\* Did we reach enough precision? *\/ */
5328: if (2.0*fabs(fp-(*fret)) <= ftol) { /* Did we reach enough precision? */
5329: /* We could compare with a chi^2. chisquare(0.95,ddl=1)=3.84 */
5330: /* By adding age*age in a model, the new -2LL should be lower and the difference follows a */
5331: /* a chisquare statistics with 1 degree. To be significant at the 95% level, it should have */
5332: /* decreased of more than 3.84 */
5333: /* By adding age*age and V1*age the gain (-2LL) should be more than 5.99 (ddl=2) */
5334: /* By using V1+V2+V3, the gain should be 7.82, compared with basic 1+age. */
5335: /* By adding 10 parameters more the gain should be 18.31 */
5336:
5337: /* Starting the program with initial values given by a former maximization will simply change */
5338: /* the scales of the directions and the directions, because the are reset to canonical directions */
5339: /* Thus the first calls to linmin will give new points and better maximizations until fp-(*fret) is */
5340: /* under the tolerance value. If the tolerance is very small 1.e-9, it could last long. */
5341: #ifdef DEBUG
5342: int k[2],l;
5343: k[0]=1;
5344: k[1]=-1;
5345: printf("Max: %.12e",(*func)(p));
5346: fprintf(ficlog,"Max: %.12e",(*func)(p));
5347: for (j=1;j<=n;j++) {
5348: printf(" %.12e",p[j]);
5349: fprintf(ficlog," %.12e",p[j]);
5350: }
5351: printf("\n");
5352: fprintf(ficlog,"\n");
5353: for(l=0;l<=1;l++) {
5354: for (j=1;j<=n;j++) {
5355: ptt[j]=p[j]+(p[j]-pt[j])*k[l];
5356: printf("l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
5357: fprintf(ficlog,"l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
5358: }
5359: printf("func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
5360: fprintf(ficlog,"func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
5361: }
5362: #endif
5363:
5364: free_vector(xit,1,n);
5365: free_vector(xits,1,n);
5366: free_vector(ptt,1,n);
5367: free_vector(pt,1,n);
5368: return;
5369: } /* enough precision */
5370: if (*iter == ITMAX*n) nrerror("powell exceeding maximum iterations.");
5371: for (j=1;j<=n;j++) { /* Computes the extrapolated point P_0 + 2 (P_n-P_0) */
5372: ptt[j]=2.0*p[j]-pt[j];
5373: xit[j]=p[j]-pt[j];
5374: pt[j]=p[j];
5375: }
5376: fptt=(*func)(ptt); /* f_3 */
5377: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */
5378: if (*iter <=4) {
5379: #else
5380: #endif
5381: #ifdef POWELLNOF3INFF1TEST /* skips test F3 <F1 */
5382: #else
5383: if (fptt < fp) { /* If extrapolated point is better, decide if we keep that new direction or not */
5384: #endif
5385: /* (x1 f1=fp), (x2 f2=*fret), (x3 f3=fptt), (xm fm) */
5386: /* From x1 (P0) distance of x2 is at h and x3 is 2h */
5387: /* Let f"(x2) be the 2nd derivative equal everywhere. */
5388: /* Then the parabolic through (x1,f1), (x2,f2) and (x3,f3) */
5389: /* will reach at f3 = fm + h^2/2 f"m ; f" = (f1 -2f2 +f3 ) / h**2 */
5390: /* Conditional for using this new direction is that mu^2 = (f1-2f2+f3)^2 /2 < del or directest <0 */
5391: /* also lamda^2=(f1-f2)^2/mu² is a parasite solution of powell */
5392: /* For powell, inclusion of this average direction is only if t(del)<0 or del inbetween mu^2 and lambda^2 */
5393: /* t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)-del*SQR(fp-fptt); */
5394: /* Even if f3 <f1, directest can be negative and t >0 */
5395: /* mu² and del² are equal when f3=f1 */
5396: /* f3 < f1 : mu² < del <= lambda^2 both test are equivalent */
5397: /* f3 < f1 : mu² < lambda^2 < del then directtest is negative and powell t is positive */
5398: /* f3 > f1 : lambda² < mu^2 < del then t is negative and directest >0 */
5399: /* f3 > f1 : lambda² < del < mu^2 then t is positive and directest >0 */
5400: #ifdef NRCORIGINAL
5401: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)- del*SQR(fp-fptt); /* Original Numerical Recipes in C*/
5402: #else
5403: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del); /* Intel compiler doesn't work on one line; bug reported */
5404: t= t- del*SQR(fp-fptt);
5405: #endif
5406: directest = fp-2.0*(*fret)+fptt - 2.0 * del; /* If delta was big enough we change it for a new direction */
5407: #ifdef DEBUG
5408: 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);
5409: 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);
5410: printf("t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
5411: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
5412: fprintf(ficlog,"t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
5413: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
5414: 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);
5415: 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);
5416: #endif
5417: #ifdef POWELLORIGINAL
5418: if (t < 0.0) { /* Then we use it for new direction */
5419: #else
5420: if (directest*t < 0.0) { /* Contradiction between both tests */
5421: 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);
5422: printf("f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt);
5423: 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);
5424: fprintf(ficlog,"f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt);
5425: }
5426: if (directest < 0.0) { /* Then we use it for new direction */
5427: #endif
5428: #ifdef DEBUGLINMIN
5429: printf("Before linmin in direction P%d-P0\n",n);
5430: for (j=1;j<=n;j++) {
5431: printf(" Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
5432: fprintf(ficlog," Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
5433: if(j % ncovmodel == 0){
5434: printf("\n");
5435: fprintf(ficlog,"\n");
5436: }
5437: }
5438: #endif
5439: #ifdef LINMINORIGINAL
5440: linmin(p,xit,n,fret,func); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
5441: #else
5442: linmin(p,xit,n,fret,func,&flat); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
5443: flatdir[i]=flat; /* Function is vanishing in that direction i */
5444: #endif
5445:
5446: #ifdef DEBUGLINMIN
5447: for (j=1;j<=n;j++) {
5448: printf("After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
5449: fprintf(ficlog,"After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
5450: if(j % ncovmodel == 0){
5451: printf("\n");
5452: fprintf(ficlog,"\n");
5453: }
5454: }
5455: #endif
5456: for (j=1;j<=n;j++) {
5457: xi[j][ibig]=xi[j][n]; /* Replace direction with biggest decrease by last direction n */
5458: xi[j][n]=xit[j]; /* and this nth direction by the by the average p_0 p_n */
5459: }
5460: #ifdef LINMINORIGINAL
5461: #else
5462: for (j=1, flatd=0;j<=n;j++) {
5463: if(flatdir[j]>0)
5464: flatd++;
5465: }
5466: if(flatd >0){
5467: printf("%d flat directions: ",flatd);
5468: fprintf(ficlog,"%d flat directions :",flatd);
5469: for (j=1;j<=n;j++) {
5470: if(flatdir[j]>0){
5471: printf("%d ",j);
5472: fprintf(ficlog,"%d ",j);
5473: }
5474: }
5475: printf("\n");
5476: fprintf(ficlog,"\n");
5477: #ifdef FLATSUP
5478: free_vector(xit,1,n);
5479: free_vector(xits,1,n);
5480: free_vector(ptt,1,n);
5481: free_vector(pt,1,n);
5482: return;
5483: #endif
5484: }
5485: #endif
5486: printf("Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
5487: fprintf(ficlog,"Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
5488:
5489: #ifdef DEBUG
5490: printf("Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
5491: fprintf(ficlog,"Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
5492: for(j=1;j<=n;j++){
5493: printf(" %lf",xit[j]);
5494: fprintf(ficlog," %lf",xit[j]);
5495: }
5496: printf("\n");
5497: fprintf(ficlog,"\n");
5498: #endif
5499: } /* end of t or directest negative */
5500: #ifdef POWELLNOF3INFF1TEST
5501: #else
5502: } /* end if (fptt < fp) */
5503: #endif
5504: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */
5505: } /*NODIRECTIONCHANGEDUNTILNITER No change in drections until some iterations are done */
5506: #else
5507: #endif
5508: } /* loop iteration */
5509: }
5510:
5511: /**** Prevalence limit (stable or period prevalence) ****************/
5512:
5513: double **prevalim(double **prlim, int nlstate, double x[], double age, double **oldm, double **savm, double ftolpl, int *ncvyear, int ij, int nres)
5514: {
5515: /**< Computes the prevalence limit in each live state at age x and for covariate combination ij . Nicely done
5516: * (and selected quantitative values in nres)
5517: * by left multiplying the unit
5518: * matrix by transitions matrix until convergence is reached with precision ftolpl
5519: * Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I
5520: * Wx is row vector: population in state 1, population in state 2, population dead
5521: * or prevalence in state 1, prevalence in state 2, 0
5522: * newm is the matrix after multiplications, its rows are identical at a factor.
5523: * Inputs are the parameter, age, a tolerance for the prevalence limit ftolpl.
5524: * Output is prlim.
5525: * Initial matrix pimij
5526: */
5527: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
5528: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
5529: /* 0, 0 , 1} */
5530: /*
5531: * and after some iteration: */
5532: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
5533: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
5534: /* 0, 0 , 1} */
5535: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
5536: /* {0.51571254859325999, 0.4842874514067399, */
5537: /* 0.51326036147820708, 0.48673963852179264} */
5538: /* If we start from prlim again, prlim tends to a constant matrix */
5539:
5540: int i, ii,j,k, k1;
5541: double *min, *max, *meandiff, maxmax,sumnew=0.;
5542: /* double **matprod2(); */ /* test */
5543: double **out, cov[NCOVMAX+1], **pmij(); /* **pmmij is a global variable feeded with oldms etc */
5544: double **newm;
5545: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
5546: int ncvloop=0;
5547: int first=0;
5548:
5549: min=vector(1,nlstate);
5550: max=vector(1,nlstate);
5551: meandiff=vector(1,nlstate);
5552:
5553: /* Starting with matrix unity */
5554: for (ii=1;ii<=nlstate+ndeath;ii++)
5555: for (j=1;j<=nlstate+ndeath;j++){
5556: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
5557: }
5558:
5559: cov[1]=1.;
5560:
5561: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
5562: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
5563: for(agefin=age-stepm/YEARM; agefin>=age-delaymax; agefin=agefin-stepm/YEARM){
5564: ncvloop++;
5565: newm=savm;
5566: /* Covariates have to be included here again */
5567: cov[2]=agefin;
5568: if(nagesqr==1){
5569: cov[3]= agefin*agefin;
5570: }
5571: /* Model(2) V1 + V2 + V3 + V8 + V7*V8 + V5*V6 + V8*age + V3*age + age*age */
5572: /* total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age */
5573: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
5574: if(Typevar[k1]==1 || Typevar[k1]==3){ /* A product with age */
5575: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
5576: }else{
5577: cov[2+nagesqr+k1]=precov[nres][k1];
5578: }
5579: }/* End of loop on model equation */
5580:
5581: /* Start of old code (replaced by a loop on position in the model equation */
5582: /* for (k=1; k<=nsd;k++) { /\* For single dummy covariates only of the model *\/ */
5583: /* /\* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates *\/ */
5584: /* /\* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,TvarsD[k])]; *\/ */
5585: /* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,TnsdVar[TvarsD[k]])]; */
5586: /* /\* model = 1 +age + V1*V3 + age*V1 + V2 + V1 + age*V2 + V3 + V3*age + V1*V2 */
5587: /* * k 1 2 3 4 5 6 7 8 */
5588: /* *cov[] 1 2 3 4 5 6 7 8 9 10 */
5589: /* *TypeVar[k] 2 1 0 0 1 0 1 2 */
5590: /* *Dummy[k] 0 2 0 0 2 0 2 0 */
5591: /* *Tvar[k] 4 1 2 1 2 3 3 5 */
5592: /* *nsd=3 (1) (2) (3) */
5593: /* *TvarsD[nsd] [1]=2 1 3 */
5594: /* *TnsdVar [2]=2 [1]=1 [3]=3 */
5595: /* *TvarsDind[nsd](=k) [1]=3 [2]=4 [3]=6 */
5596: /* *Tage[] [1]=1 [2]=2 [3]=3 */
5597: /* *Tvard[] [1][1]=1 [2][1]=1 */
5598: /* * [1][2]=3 [2][2]=2 */
5599: /* *Tprod[](=k) [1]=1 [2]=8 */
5600: /* *TvarsDp(=Tvar) [1]=1 [2]=2 [3]=3 [4]=5 */
5601: /* *TvarD (=k) [1]=1 [2]=3 [3]=4 [3]=6 [4]=6 */
5602: /* *TvarsDpType */
5603: /* *si model= 1 + age + V3 + V2*age + V2 + V3*age */
5604: /* * nsd=1 (1) (2) */
5605: /* *TvarsD[nsd] 3 2 */
5606: /* *TnsdVar (3)=1 (2)=2 */
5607: /* *TvarsDind[nsd](=k) [1]=1 [2]=3 */
5608: /* *Tage[] [1]=2 [2]= 3 */
5609: /* *\/ */
5610: /* /\* cov[++k1]=nbcode[TvarsD[k]][codtabm(ij,k)]; *\/ */
5611: /* /\* 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)); *\/ */
5612: /* } */
5613: /* for (k=1; k<=nsq;k++) { /\* For single quantitative varying covariates only of the model *\/ */
5614: /* /\* Here comes the value of quantitative after renumbering k with single quantitative covariates *\/ */
5615: /* /\* Tqresult[nres][result_position]= value of the variable at the result_position in the nres resultline *\/ */
5616: /* /\* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; *\/ */
5617: /* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][resultmodel[nres][k1]] */
5618: /* /\* cov[++k1]=Tqresult[nres][k]; *\/ */
5619: /* /\* 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]); *\/ */
5620: /* } */
5621: /* for (k=1; k<=cptcovage;k++){ /\* For product with age *\/ */
5622: /* if(Dummy[Tage[k]]==2){ /\* dummy with age *\/ */
5623: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
5624: /* /\* cov[++k1]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; *\/ */
5625: /* } else if(Dummy[Tage[k]]==3){ /\* quantitative with age *\/ */
5626: /* cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; */
5627: /* /\* cov[++k1]=Tqresult[nres][k]; *\/ */
5628: /* } */
5629: /* /\* 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]); *\/ */
5630: /* } */
5631: /* for (k=1; k<=cptcovprod;k++){ /\* For product without age *\/ */
5632: /* /\* 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]); *\/ */
5633: /* if(Dummy[Tvard[k][1]]==0){ */
5634: /* if(Dummy[Tvard[k][2]]==0){ */
5635: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
5636: /* /\* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
5637: /* }else{ */
5638: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * Tqresult[nres][k]; */
5639: /* /\* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * Tqresult[nres][k]; *\/ */
5640: /* } */
5641: /* }else{ */
5642: /* if(Dummy[Tvard[k][2]]==0){ */
5643: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])] * Tqinvresult[nres][Tvard[k][1]]; */
5644: /* /\* cov[++k1]=nbcode[Tvard[k][2]][codtabm(ij,k)] * Tqinvresult[nres][Tvard[k][1]]; *\/ */
5645: /* }else{ */
5646: /* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; */
5647: /* /\* cov[++k1]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; *\/ */
5648: /* } */
5649: /* } */
5650: /* } /\* End product without age *\/ */
5651: /* ENd of old code */
5652: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
5653: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
5654: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
5655: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
5656: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
5657: /* age and covariate values of ij are in 'cov' */
5658: out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /* Bug Valgrind */
5659:
5660: savm=oldm;
5661: oldm=newm;
5662:
5663: for(j=1; j<=nlstate; j++){
5664: max[j]=0.;
5665: min[j]=1.;
5666: }
5667: for(i=1;i<=nlstate;i++){
5668: sumnew=0;
5669: for(k=1; k<=ndeath; k++) sumnew+=newm[i][nlstate+k];
5670: for(j=1; j<=nlstate; j++){
5671: prlim[i][j]= newm[i][j]/(1-sumnew);
5672: max[j]=FMAX(max[j],prlim[i][j]);
5673: min[j]=FMIN(min[j],prlim[i][j]);
5674: }
5675: }
5676:
5677: maxmax=0.;
5678: for(j=1; j<=nlstate; j++){
5679: meandiff[j]=(max[j]-min[j])/(max[j]+min[j])*2.; /* mean difference for each column */
5680: maxmax=FMAX(maxmax,meandiff[j]);
5681: /* 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); */
5682: } /* j loop */
5683: *ncvyear= (int)age- (int)agefin;
5684: /* printf("maxmax=%lf maxmin=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, maxmin, ncvloop, (int)age, (int)agefin, *ncvyear); */
5685: if(maxmax < ftolpl){
5686: /* printf("maxmax=%lf ncvloop=%ld, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
5687: free_vector(min,1,nlstate);
5688: free_vector(max,1,nlstate);
5689: free_vector(meandiff,1,nlstate);
5690: return prlim;
5691: }
5692: } /* agefin loop */
5693: /* After some age loop it doesn't converge */
5694: if(!first){
5695: first=1;
5696: 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);
5697: 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);
5698: }else if (first >=1 && first <10){
5699: 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);
5700: first++;
5701: }else if (first ==10){
5702: 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);
5703: 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");
5704: fprintf(ficlog,"Warning: the stable prevalence no convergence; too many cases, giving up noticing, even in log file\n");
5705: first++;
5706: }
5707:
5708: /* 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); */
5709: free_vector(min,1,nlstate);
5710: free_vector(max,1,nlstate);
5711: free_vector(meandiff,1,nlstate);
5712:
5713: return prlim; /* should not reach here */
5714: }
5715:
5716:
5717: /**** Back Prevalence limit (stable or period prevalence) ****************/
5718:
5719: /* 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) */
5720: /* 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) */
5721: double **bprevalim(double **bprlim, double ***prevacurrent, int nlstate, double x[], double age, double ftolpl, int *ncvyear, int ij, int nres)
5722: {
5723: /* Computes the prevalence limit in each live state at age x and for covariate combination ij (<=2**cptcoveff) by left multiplying the unit
5724: matrix by transitions matrix until convergence is reached with precision ftolpl */
5725: /* Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I */
5726: /* Wx is row vector: population in state 1, population in state 2, population dead */
5727: /* or prevalence in state 1, prevalence in state 2, 0 */
5728: /* newm is the matrix after multiplications, its rows are identical at a factor */
5729: /* Initial matrix pimij */
5730: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
5731: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
5732: /* 0, 0 , 1} */
5733: /*
5734: * and after some iteration: */
5735: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
5736: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
5737: /* 0, 0 , 1} */
5738: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
5739: /* {0.51571254859325999, 0.4842874514067399, */
5740: /* 0.51326036147820708, 0.48673963852179264} */
5741: /* If we start from prlim again, prlim tends to a constant matrix */
5742:
5743: int i, ii,j,k, k1;
5744: int first=0;
5745: double *min, *max, *meandiff, maxmax,sumnew=0.;
5746: /* double **matprod2(); */ /* test */
5747: double **out, cov[NCOVMAX+1], **bmij();
5748: double **newm;
5749: double **dnewm, **doldm, **dsavm; /* for use */
5750: double **oldm, **savm; /* for use */
5751:
5752: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
5753: int ncvloop=0;
5754:
5755: min=vector(1,nlstate);
5756: max=vector(1,nlstate);
5757: meandiff=vector(1,nlstate);
5758:
5759: dnewm=ddnewms; doldm=ddoldms; dsavm=ddsavms;
5760: oldm=oldms; savm=savms;
5761:
5762: /* Starting with matrix unity */
5763: for (ii=1;ii<=nlstate+ndeath;ii++)
5764: for (j=1;j<=nlstate+ndeath;j++){
5765: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
5766: }
5767:
5768: cov[1]=1.;
5769:
5770: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
5771: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
5772: /* for(agefin=age+stepm/YEARM; agefin<=age+delaymax; agefin=agefin+stepm/YEARM){ /\* A changer en age *\/ */
5773: /* for(agefin=age; agefin<AGESUP; agefin=agefin+stepm/YEARM){ /\* A changer en age *\/ */
5774: for(agefin=age; agefin<FMIN(AGESUP,age+delaymax); agefin=agefin+stepm/YEARM){ /* A changer en age */
5775: ncvloop++;
5776: newm=savm; /* oldm should be kept from previous iteration or unity at start */
5777: /* newm points to the allocated table savm passed by the function it can be written, savm could be reallocated */
5778: /* Covariates have to be included here again */
5779: cov[2]=agefin;
5780: if(nagesqr==1){
5781: cov[3]= agefin*agefin;;
5782: }
5783: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
5784: if(Typevar[k1]==1 || Typevar[k1]==3){ /* A product with age */
5785: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
5786: }else{
5787: cov[2+nagesqr+k1]=precov[nres][k1];
5788: }
5789: }/* End of loop on model equation */
5790:
5791: /* Old code */
5792:
5793: /* for (k=1; k<=nsd;k++) { /\* For single dummy covariates only *\/ */
5794: /* /\* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates *\/ */
5795: /* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,TvarsD[k])]; */
5796: /* /\* 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)); *\/ */
5797: /* } */
5798: /* /\* for (k=1; k<=cptcovn;k++) { *\/ */
5799: /* /\* /\\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; *\\/ *\/ */
5800: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; *\/ */
5801: /* /\* /\\* 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])]); *\\/ *\/ */
5802: /* /\* } *\/ */
5803: /* for (k=1; k<=nsq;k++) { /\* For single varying covariates only *\/ */
5804: /* /\* Here comes the value of quantitative after renumbering k with single quantitative covariates *\/ */
5805: /* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; */
5806: /* /\* 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]); *\/ */
5807: /* } */
5808: /* /\* for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,k)]*cov[2]; *\/ */
5809: /* /\* for (k=1; k<=cptcovprod;k++) /\\* Useless *\\/ *\/ */
5810: /* /\* /\\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; *\\/ *\/ */
5811: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
5812: /* for (k=1; k<=cptcovage;k++){ /\* For product with age *\/ */
5813: /* /\* if(Dummy[Tvar[Tage[k]]]== 2){ /\\* dummy with age *\\/ ERROR ???*\/ */
5814: /* if(Dummy[Tage[k]]== 2){ /\* dummy with age *\/ */
5815: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
5816: /* } else if(Dummy[Tage[k]]== 3){ /\* quantitative with age *\/ */
5817: /* cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; */
5818: /* } */
5819: /* /\* 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]); *\/ */
5820: /* } */
5821: /* for (k=1; k<=cptcovprod;k++){ /\* For product without age *\/ */
5822: /* /\* 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]); *\/ */
5823: /* if(Dummy[Tvard[k][1]]==0){ */
5824: /* if(Dummy[Tvard[k][2]]==0){ */
5825: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
5826: /* }else{ */
5827: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * Tqresult[nres][k]; */
5828: /* } */
5829: /* }else{ */
5830: /* if(Dummy[Tvard[k][2]]==0){ */
5831: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])] * Tqinvresult[nres][Tvard[k][1]]; */
5832: /* }else{ */
5833: /* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; */
5834: /* } */
5835: /* } */
5836: /* } */
5837:
5838: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
5839: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
5840: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
5841: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
5842: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
5843: /* ij should be linked to the correct index of cov */
5844: /* age and covariate values ij are in 'cov', but we need to pass
5845: * ij for the observed prevalence at age and status and covariate
5846: * number: prevacurrent[(int)agefin][ii][ij]
5847: */
5848: /* 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 *\/ */
5849: /* 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 *\/ */
5850: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij)); /* Bug Valgrind */
5851: /* if((int)age == 86 || (int)age == 87){ */
5852: /* printf(" Backward prevalim age=%d agefin=%d \n", (int) age, (int) agefin); */
5853: /* for(i=1; i<=nlstate+ndeath; i++) { */
5854: /* printf("%d newm= ",i); */
5855: /* for(j=1;j<=nlstate+ndeath;j++) { */
5856: /* printf("%f ",newm[i][j]); */
5857: /* } */
5858: /* printf("oldm * "); */
5859: /* for(j=1;j<=nlstate+ndeath;j++) { */
5860: /* printf("%f ",oldm[i][j]); */
5861: /* } */
5862: /* printf(" bmmij "); */
5863: /* for(j=1;j<=nlstate+ndeath;j++) { */
5864: /* printf("%f ",pmmij[i][j]); */
5865: /* } */
5866: /* printf("\n"); */
5867: /* } */
5868: /* } */
5869: savm=oldm;
5870: oldm=newm;
5871:
5872: for(j=1; j<=nlstate; j++){
5873: max[j]=0.;
5874: min[j]=1.;
5875: }
5876: for(j=1; j<=nlstate; j++){
5877: for(i=1;i<=nlstate;i++){
5878: /* bprlim[i][j]= newm[i][j]/(1-sumnew); */
5879: bprlim[i][j]= newm[i][j];
5880: max[i]=FMAX(max[i],bprlim[i][j]); /* Max in line */
5881: min[i]=FMIN(min[i],bprlim[i][j]);
5882: }
5883: }
5884:
5885: maxmax=0.;
5886: for(i=1; i<=nlstate; i++){
5887: meandiff[i]=(max[i]-min[i])/(max[i]+min[i])*2.; /* mean difference for each column, could be nan! */
5888: maxmax=FMAX(maxmax,meandiff[i]);
5889: /* 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); */
5890: } /* i loop */
5891: *ncvyear= -( (int)age- (int)agefin);
5892: /* printf("Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
5893: if(maxmax < ftolpl){
5894: /* printf("OK Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
5895: free_vector(min,1,nlstate);
5896: free_vector(max,1,nlstate);
5897: free_vector(meandiff,1,nlstate);
5898: return bprlim;
5899: }
5900: } /* agefin loop */
5901: /* After some age loop it doesn't converge */
5902: if(!first){
5903: first=1;
5904: 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\
5905: 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);
5906: }
5907: 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\
5908: 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);
5909: /* 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); */
5910: free_vector(min,1,nlstate);
5911: free_vector(max,1,nlstate);
5912: free_vector(meandiff,1,nlstate);
5913:
5914: return bprlim; /* should not reach here */
5915: }
5916:
5917: /*************** transition probabilities ***************/
5918:
5919: double **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
5920: {
5921: /* According to parameters values stored in x and the covariate's values stored in cov,
5922: computes the probability to be observed in state j (after stepm years) being in state i by appying the
5923: model to the ncovmodel covariates (including constant and age).
5924: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
5925: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
5926: ncth covariate in the global vector x is given by the formula:
5927: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
5928: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
5929: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
5930: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
5931: Outputs ps[i][j] or probability to be observed in j being in i according to
5932: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
5933: Sum on j ps[i][j] should equal to 1.
5934: */
5935: double s1, lnpijopii;
5936: /*double t34;*/
5937: int i,j, nc, ii, jj;
5938:
5939: for(i=1; i<= nlstate; i++){
5940: for(j=1; j<i;j++){
5941: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
5942: /*lnpijopii += param[i][j][nc]*cov[nc];*/
5943: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
5944: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
5945: }
5946: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
5947: /* printf("Debug pmij() i=%d j=%d nc=%d s1=%.17f, lnpijopii=%.17f\n",i,j,nc, s1,lnpijopii); */
5948: }
5949: for(j=i+1; j<=nlstate+ndeath;j++){
5950: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
5951: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
5952: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
5953: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
5954: }
5955: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
5956: /* printf("Debug pmij() i=%d j=%d nc=%d s1=%.17f, lnpijopii=%.17f\n",i,j,nc, s1,lnpijopii); */
5957: }
5958: }
5959:
5960: for(i=1; i<= nlstate; i++){
5961: s1=0;
5962: for(j=1; j<i; j++){
5963: /* printf("debug1 %d %d ps=%lf exp(ps)=%lf \n",i,j,ps[i][j],exp(ps[i][j])); */
5964: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
5965: }
5966: for(j=i+1; j<=nlstate+ndeath; j++){
5967: /* printf("debug2 %d %d ps=%lf exp(ps)=%lf \n",i,j,ps[i][j],exp(ps[i][j])); */
5968: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
5969: }
5970: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
5971: ps[i][i]=1./(s1+1.);
5972: /* Computing other pijs */
5973: for(j=1; j<i; j++)
5974: ps[i][j]= exp(ps[i][j])*ps[i][i];/* Bug valgrind */
5975: for(j=i+1; j<=nlstate+ndeath; j++)
5976: ps[i][j]= exp(ps[i][j])*ps[i][i];
5977: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
5978: } /* end i */
5979:
5980: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
5981: for(jj=1; jj<= nlstate+ndeath; jj++){
5982: ps[ii][jj]=0;
5983: ps[ii][ii]=1;
5984: }
5985: }
5986:
5987:
5988: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
5989: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
5990: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
5991: /* } */
5992: /* printf("\n "); */
5993: /* } */
5994: /* printf("\n ");printf("%lf ",cov[2]);*/
5995: /*
5996: for(i=1; i<= npar; i++) printf("%f ",x[i]);
5997: goto end;*/
5998: return ps; /* Pointer is unchanged since its call */
5999: }
6000:
6001: /*************** backward transition probabilities ***************/
6002:
6003: /* 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 ) */
6004: /* double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, double ***dnewm, double **doldm, double **dsavm, int ij ) */
6005: double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, int ij )
6006: {
6007: /* Computes the backward probability at age agefin, cov[2], and covariate combination 'ij'. In fact cov is already filled and x too.
6008: * Call to pmij(cov and x), call to cross prevalence, sums and inverses, left multiply, and returns in **ps as well as **bmij.
6009: */
6010: int i, ii, j,k;
6011:
6012: double **out, **pmij();
6013: double sumnew=0.;
6014: double agefin;
6015: double k3=0.; /* constant of the w_x diagonal matrix (in order for B to sum to 1 even for death state) */
6016: double **dnewm, **dsavm, **doldm;
6017: double **bbmij;
6018:
6019: doldm=ddoldms; /* global pointers */
6020: dnewm=ddnewms;
6021: dsavm=ddsavms;
6022:
6023: /* Debug */
6024: /* printf("Bmij ij=%d, cov[2}=%f\n", ij, cov[2]); */
6025: agefin=cov[2];
6026: /* Bx = Diag(w_x) P_x Diag(Sum_i w^i_x p^ij_x */
6027: /* bmij *//* age is cov[2], ij is included in cov, but we need for
6028: the observed prevalence (with this covariate ij) at beginning of transition */
6029: /* dsavm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
6030:
6031: /* P_x */
6032: pmmij=pmij(pmmij,cov,ncovmodel,x,nlstate); /*This is forward probability from agefin to agefin + stepm *//* Bug valgrind */
6033: /* outputs pmmij which is a stochastic matrix in row */
6034:
6035: /* Diag(w_x) */
6036: /* Rescaling the cross-sectional prevalence: Problem with prevacurrent which can be zero */
6037: sumnew=0.;
6038: /*for (ii=1;ii<=nlstate+ndeath;ii++){*/
6039: for (ii=1;ii<=nlstate;ii++){ /* Only on live states */
6040: /* printf(" agefin=%d, ii=%d, ij=%d, prev=%f\n",(int)agefin,ii, ij, prevacurrent[(int)agefin][ii][ij]); */
6041: sumnew+=prevacurrent[(int)agefin][ii][ij];
6042: }
6043: if(sumnew >0.01){ /* At least some value in the prevalence */
6044: for (ii=1;ii<=nlstate+ndeath;ii++){
6045: for (j=1;j<=nlstate+ndeath;j++)
6046: doldm[ii][j]=(ii==j ? prevacurrent[(int)agefin][ii][ij]/sumnew : 0.0);
6047: }
6048: }else{
6049: for (ii=1;ii<=nlstate+ndeath;ii++){
6050: for (j=1;j<=nlstate+ndeath;j++)
6051: doldm[ii][j]=(ii==j ? 1./nlstate : 0.0);
6052: }
6053: /* if(sumnew <0.9){ */
6054: /* printf("Problem internal bmij B: sum on i wi <0.9: j=%d, sum_i wi=%lf,agefin=%d\n",j,sumnew, (int)agefin); */
6055: /* } */
6056: }
6057: k3=0.0; /* We put the last diagonal to 0 */
6058: for (ii=nlstate+1;ii<=nlstate+ndeath;ii++){
6059: doldm[ii][ii]= k3;
6060: }
6061: /* End doldm, At the end doldm is diag[(w_i)] */
6062:
6063: /* Left product of this diag matrix by pmmij=Px (dnewm=dsavm*doldm): diag[(w_i)*Px */
6064: bbmij=matprod2(dnewm, doldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, pmmij); /* was a Bug Valgrind */
6065:
6066: /* Diag(Sum_i w^i_x p^ij_x, should be the prevalence at age x+stepm */
6067: /* w1 p11 + w2 p21 only on live states N1./N..*N11/N1. + N2./N..*N21/N2.=(N11+N21)/N..=N.1/N.. */
6068: for (j=1;j<=nlstate+ndeath;j++){
6069: sumnew=0.;
6070: for (ii=1;ii<=nlstate;ii++){
6071: /* sumnew+=dsavm[ii][j]*prevacurrent[(int)agefin][ii][ij]; */
6072: sumnew+=pmmij[ii][j]*doldm[ii][ii]; /* Yes prevalence at beginning of transition */
6073: } /* sumnew is (N11+N21)/N..= N.1/N.. = sum on i of w_i pij */
6074: for (ii=1;ii<=nlstate+ndeath;ii++){
6075: /* if(agefin >= agemaxpar && agefin <= agemaxpar+stepm/YEARM){ */
6076: /* dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
6077: /* }else if(agefin >= agemaxpar+stepm/YEARM){ */
6078: /* dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
6079: /* }else */
6080: dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0);
6081: } /*End ii */
6082: } /* 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 */
6083:
6084: ps=matprod2(ps, dnewm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, dsavm); /* was a Bug Valgrind */
6085: /* ps is now diag[w_i] * Px * diag [1/(w_1p1i+w_2 p2i)] */
6086: /* end bmij */
6087: return ps; /*pointer is unchanged */
6088: }
6089: /*************** transition probabilities ***************/
6090:
6091: double **bpmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
6092: {
6093: /* According to parameters values stored in x and the covariate's values stored in cov,
6094: computes the probability to be observed in state j being in state i by appying the
6095: model to the ncovmodel covariates (including constant and age).
6096: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
6097: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
6098: ncth covariate in the global vector x is given by the formula:
6099: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
6100: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
6101: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
6102: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
6103: Outputs ps[i][j] the probability to be observed in j being in j according to
6104: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
6105: */
6106: double s1, lnpijopii;
6107: /*double t34;*/
6108: int i,j, nc, ii, jj;
6109:
6110: for(i=1; i<= nlstate; i++){
6111: for(j=1; j<i;j++){
6112: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
6113: /*lnpijopii += param[i][j][nc]*cov[nc];*/
6114: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
6115: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
6116: }
6117: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
6118: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
6119: }
6120: for(j=i+1; j<=nlstate+ndeath;j++){
6121: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
6122: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
6123: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
6124: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
6125: }
6126: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
6127: }
6128: }
6129:
6130: for(i=1; i<= nlstate; i++){
6131: s1=0;
6132: for(j=1; j<i; j++){
6133: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
6134: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
6135: }
6136: for(j=i+1; j<=nlstate+ndeath; j++){
6137: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
6138: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
6139: }
6140: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
6141: ps[i][i]=1./(s1+1.);
6142: /* Computing other pijs */
6143: for(j=1; j<i; j++)
6144: ps[i][j]= exp(ps[i][j])*ps[i][i];
6145: for(j=i+1; j<=nlstate+ndeath; j++)
6146: ps[i][j]= exp(ps[i][j])*ps[i][i];
6147: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
6148: } /* end i */
6149:
6150: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
6151: for(jj=1; jj<= nlstate+ndeath; jj++){
6152: ps[ii][jj]=0;
6153: ps[ii][ii]=1;
6154: }
6155: }
6156: /* Added for prevbcast */ /* Transposed matrix too */
6157: for(jj=1; jj<= nlstate+ndeath; jj++){
6158: s1=0.;
6159: for(ii=1; ii<= nlstate+ndeath; ii++){
6160: s1+=ps[ii][jj];
6161: }
6162: for(ii=1; ii<= nlstate; ii++){
6163: ps[ii][jj]=ps[ii][jj]/s1;
6164: }
6165: }
6166: /* Transposition */
6167: for(jj=1; jj<= nlstate+ndeath; jj++){
6168: for(ii=jj; ii<= nlstate+ndeath; ii++){
6169: s1=ps[ii][jj];
6170: ps[ii][jj]=ps[jj][ii];
6171: ps[jj][ii]=s1;
6172: }
6173: }
6174: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
6175: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
6176: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
6177: /* } */
6178: /* printf("\n "); */
6179: /* } */
6180: /* printf("\n ");printf("%lf ",cov[2]);*/
6181: /*
6182: for(i=1; i<= npar; i++) printf("%f ",x[i]);
6183: goto end;*/
6184: return ps;
6185: }
6186:
6187:
6188: /**************** Product of 2 matrices ******************/
6189:
6190: double **matprod2(double **out, double **in,int nrl, int nrh, int ncl, int nch, int ncolol, int ncoloh, double **b)
6191: {
6192: /* Computes the matrix product of in(1,nrh-nrl+1)(1,nch-ncl+1) times
6193: b(1,nch-ncl+1)(1,ncoloh-ncolol+1) into out(...) */
6194: /* in, b, out are matrice of pointers which should have been initialized
6195: before: only the contents of out is modified. The function returns
6196: a pointer to pointers identical to out */
6197: int i, j, k;
6198: for(i=nrl; i<= nrh; i++)
6199: for(k=ncolol; k<=ncoloh; k++){
6200: out[i][k]=0.;
6201: for(j=ncl; j<=nch; j++)
6202: out[i][k] +=in[i][j]*b[j][k];
6203: }
6204: return out;
6205: }
6206:
6207:
6208: /************* Higher Matrix Product ***************/
6209:
6210: double ***hpxij(double ***po, int nhstepm, double age, int hstepm, double *x, int nlstate, int stepm, double **oldm, double **savm, int ij, int nres )
6211: {
6212: /* Already optimized with precov.
6213: Computes the transition matrix starting at age 'age' and dummies values in each resultline (loop on ij to find the corresponding combination) to over
6214: 'nhstepm*hstepm*stepm' months (i.e. until
6215: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
6216: nhstepm*hstepm matrices.
6217: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
6218: (typically every 2 years instead of every month which is too big
6219: for the memory).
6220: Model is determined by parameters x and covariates have to be
6221: included manually here.
6222:
6223: */
6224:
6225: int i, j, d, h, k, k1;
6226: double **out, cov[NCOVMAX+1];
6227: double **newm;
6228: double agexact;
6229: double agebegin, ageend;
6230:
6231: /* Hstepm could be zero and should return the unit matrix */
6232: for (i=1;i<=nlstate+ndeath;i++)
6233: for (j=1;j<=nlstate+ndeath;j++){
6234: oldm[i][j]=(i==j ? 1.0 : 0.0);
6235: po[i][j][0]=(i==j ? 1.0 : 0.0);
6236: }
6237: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
6238: for(h=1; h <=nhstepm; h++){
6239: for(d=1; d <=hstepm; d++){
6240: newm=savm;
6241: /* Covariates have to be included here again */
6242: cov[1]=1.;
6243: agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /* age just before transition */
6244: cov[2]=agexact;
6245: if(nagesqr==1){
6246: cov[3]= agexact*agexact;
6247: }
6248: /* Model(2) V1 + V2 + V3 + V8 + V7*V8 + V5*V6 + V8*age + V3*age + age*age */
6249: /* total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age */
6250: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
6251: if(Typevar[k1]==1 || Typevar[k1]==3){ /* A product with age */
6252: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
6253: }else{
6254: cov[2+nagesqr+k1]=precov[nres][k1];
6255: }
6256: }/* End of loop on model equation */
6257: /* Old code */
6258: /* if( Dummy[k1]==0 && Typevar[k1]==0 ){ /\* Single dummy *\/ */
6259: /* /\* V(Tvarsel)=Tvalsel=Tresult[nres][pos](value); V(Tvresult[nres][pos] (variable): V(variable)=value) *\/ */
6260: /* /\* for (k=1; k<=nsd;k++) { /\\* For single dummy covariates only *\\/ *\/ */
6261: /* /\* /\\* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates *\\/ *\/ */
6262: /* /\* codtabm(ij,k) (1 & (ij-1) >> (k-1))+1 *\/ */
6263: /* /\* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 *\/ */
6264: /* /\* k 1 2 3 4 5 6 7 8 9 *\/ */
6265: /* /\*Tvar[k]= 5 4 3 6 5 2 7 1 1 *\/ */
6266: /* /\* nsd 1 2 3 *\/ /\* Counting single dummies covar fixed or tv *\/ */
6267: /* /\*TvarsD[nsd] 4 3 1 *\/ /\* ID of single dummy cova fixed or timevary*\/ */
6268: /* /\*TvarsDind[k] 2 3 9 *\/ /\* position K of single dummy cova *\/ */
6269: /* /\* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)];or [codtabm(ij,TnsdVar[TvarsD[k]] *\/ */
6270: /* cov[2+nagesqr+k1]=Tresult[nres][resultmodel[nres][k1]]; */
6271: /* /\* 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]])); *\/ */
6272: /* 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); */
6273: /* printf("hpxij new Dummy precov[nres=%d][k1=%d]=%.4f\n", nres, k1, precov[nres][k1]); */
6274: /* }else if( Dummy[k1]==1 && Typevar[k1]==0 ){ /\* Single quantitative variables *\/ */
6275: /* /\* resultmodel[nres][k1]=k3: k1th position in the model correspond to the k3 position in the resultline *\/ */
6276: /* cov[2+nagesqr+k1]=Tqresult[nres][resultmodel[nres][k1]]; */
6277: /* /\* for (k=1; k<=nsq;k++) { /\\* For single varying covariates only *\\/ *\/ */
6278: /* /\* /\\* Here comes the value of quantitative after renumbering k with single quantitative covariates *\\/ *\/ */
6279: /* /\* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; *\/ */
6280: /* 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]]); */
6281: /* printf("hpxij new Quanti precov[nres=%d][k1=%d]=%.4f\n", nres, k1, precov[nres][k1]); */
6282: /* }else if( Dummy[k1]==2 ){ /\* For dummy with age product *\/ */
6283: /* /\* Tvar[k1] Variable in the age product age*V1 is 1 *\/ */
6284: /* /\* [Tinvresult[nres][V1] is its value in the resultline nres *\/ */
6285: /* cov[2+nagesqr+k1]=TinvDoQresult[nres][Tvar[k1]]*cov[2]; */
6286: /* 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]); */
6287: /* printf("hpxij new Dummy with age product precov[nres=%d][k1=%d]=%.4f * age=%.2f\n", nres, k1, precov[nres][k1], cov[2]); */
6288:
6289: /* /\* cov[2+nagesqr+k1]=Tresult[nres][resultmodel[nres][k1]]; *\/ */
6290: /* /\* for (k=1; k<=cptcovage;k++){ /\\* For product with age V1+V1*age +V4 +age*V3 *\\/ *\/ */
6291: /* /\* 1+2 Tage[1]=2 TVar[2]=1 Dummy[2]=2, Tage[2]=4 TVar[4]=3 Dummy[4]=3 quant*\/ */
6292: /* /\* *\/ */
6293: /* /\* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 *\/ */
6294: /* /\* k 1 2 3 4 5 6 7 8 9 *\/ */
6295: /* /\*Tvar[k]= 5 4 3 6 5 2 7 1 1 *\/ */
6296: /* /\*cptcovage=2 1 2 *\/ */
6297: /* /\*Tage[k]= 5 8 *\/ */
6298: /* }else if( Dummy[k1]==3 ){ /\* For quant with age product *\/ */
6299: /* cov[2+nagesqr+k1]=Tresult[nres][resultmodel[nres][k1]]; */
6300: /* 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]]); */
6301: /* printf("hpxij new Quanti with age product precov[nres=%d][k1=%d] * age=%.2f\n", nres, k1, precov[nres][k1], cov[2]); */
6302: /* /\* if(Dummy[Tage[k]]== 2){ /\\* dummy with age *\\/ *\/ */
6303: /* /\* /\\* if(Dummy[Tvar[Tage[k]]]== 2){ /\\\* dummy with age *\\\/ *\\/ *\/ */
6304: /* /\* /\\* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; *\\/ *\/ */
6305: /* /\* /\\* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,TnsdVar[TvarsD[Tvar[Tage[k]]]])]*cov[2]; *\\/ *\/ */
6306: /* /\* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,TnsdVar[TvarsD[Tvar[Tage[k]]]])]*cov[2]; *\/ */
6307: /* /\* 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); *\/ */
6308: /* /\* } else if(Dummy[Tage[k]]== 3){ /\\* quantitative with age *\\/ *\/ */
6309: /* /\* cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; *\/ */
6310: /* /\* } *\/ */
6311: /* /\* 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]); *\/ */
6312: /* }else if(Typevar[k1]==2 ){ /\* For product (not with age) *\/ */
6313: /* /\* for (k=1; k<=cptcovprod;k++){ /\\* For product without age *\\/ *\/ */
6314: /* /\* /\\* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 *\\/ *\/ */
6315: /* /\* /\\* k 1 2 3 4 5 6 7 8 9 *\\/ *\/ */
6316: /* /\* /\\*Tvar[k]= 5 4 3 6 5 2 7 1 1 *\\/ *\/ */
6317: /* /\* /\\*cptcovprod=1 1 2 *\\/ *\/ */
6318: /* /\* /\\*Tprod[]= 4 7 *\\/ *\/ */
6319: /* /\* /\\*Tvard[][1] 4 1 *\\/ *\/ */
6320: /* /\* /\\*Tvard[][2] 3 2 *\\/ *\/ */
6321:
6322: /* /\* 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])]); *\/ */
6323: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
6324: /* cov[2+nagesqr+k1]=TinvDoQresult[nres][Tvardk[k1][1]] * TinvDoQresult[nres][Tvardk[k1][2]]; */
6325: /* 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]]); */
6326: /* printf("hpxij new Product no age product precov[nres=%d][k1=%d]=%.4f\n", nres, k1, precov[nres][k1]); */
6327:
6328: /* /\* if(Dummy[Tvardk[k1][1]]==0){ *\/ */
6329: /* /\* if(Dummy[Tvardk[k1][2]]==0){ /\\* Product of dummies *\\/ *\/ */
6330: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
6331: /* /\* cov[2+nagesqr+k1]=Tinvresult[nres][Tvardk[k1][1]] * Tinvresult[nres][Tvardk[k1][2]]; *\/ */
6332: /* /\* 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]])]; *\/ */
6333: /* /\* }else{ /\\* Product of dummy by quantitative *\\/ *\/ */
6334: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,TnsdVar[Tvard[k][1]])] * Tqresult[nres][k]; *\/ */
6335: /* /\* cov[2+nagesqr+k1]=Tresult[nres][Tinvresult[nres][Tvardk[k1][1]]] * Tqresult[nres][Tinvresult[nres][Tvardk[k1][2]]]; *\/ */
6336: /* /\* } *\/ */
6337: /* /\* }else{ /\\* Product of quantitative by...*\\/ *\/ */
6338: /* /\* if(Dummy[Tvard[k][2]]==0){ /\\* quant by dummy *\\/ *\/ */
6339: /* /\* /\\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,TnsdVar[Tvard[k][2]])] * Tqinvresult[nres][Tvard[k][1]]; *\\/ *\/ */
6340: /* /\* cov[2+nagesqr+k1]=Tqresult[nres][Tinvresult[nres][Tvardk[k1][1]]] * Tresult[nres][Tinvresult[nres][Tvardk[k1][2]]] ; *\/ */
6341: /* /\* }else{ /\\* Product of two quant *\\/ *\/ */
6342: /* /\* /\\* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; *\\/ *\/ */
6343: /* /\* cov[2+nagesqr+k1]=Tqresult[nres][Tinvresult[nres][Tvardk[k1][1]]] * Tqresult[nres][Tinvresult[nres][Tvardk[k1][2]]] ; *\/ */
6344: /* /\* } *\/ */
6345: /* /\* }/\\*end of products quantitative *\\/ *\/ */
6346: /* }/\*end of products *\/ */
6347: /* } /\* End of loop on model equation *\/ */
6348: /* for (k=1; k<=cptcovn;k++) */
6349: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; */
6350: /* for (k=1; k<=cptcovage;k++) /\* Should start at cptcovn+1 *\/ */
6351: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; */
6352: /* for (k=1; k<=cptcovprod;k++) /\* Useless because included in cptcovn *\/ */
6353: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)]; */
6354:
6355:
6356: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
6357: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
6358: /* right multiplication of oldm by the current matrix */
6359: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
6360: pmij(pmmij,cov,ncovmodel,x,nlstate));
6361: /* if((int)age == 70){ */
6362: /* printf(" Forward hpxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
6363: /* for(i=1; i<=nlstate+ndeath; i++) { */
6364: /* printf("%d pmmij ",i); */
6365: /* for(j=1;j<=nlstate+ndeath;j++) { */
6366: /* printf("%f ",pmmij[i][j]); */
6367: /* } */
6368: /* printf(" oldm "); */
6369: /* for(j=1;j<=nlstate+ndeath;j++) { */
6370: /* printf("%f ",oldm[i][j]); */
6371: /* } */
6372: /* printf("\n"); */
6373: /* } */
6374: /* } */
6375: savm=oldm;
6376: oldm=newm;
6377: }
6378: for(i=1; i<=nlstate+ndeath; i++)
6379: for(j=1;j<=nlstate+ndeath;j++) {
6380: po[i][j][h]=newm[i][j];
6381: /*if(h==nhstepm) printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]);*/
6382: }
6383: /*printf("h=%d ",h);*/
6384: } /* end h */
6385: /* printf("\n H=%d \n",h); */
6386: return po;
6387: }
6388:
6389: /************* Higher Back Matrix Product ***************/
6390: /* 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 ) */
6391: double ***hbxij(double ***po, int nhstepm, double age, int hstepm, double *x, double ***prevacurrent, int nlstate, int stepm, int ij, int nres )
6392: {
6393: /* For dummy covariates given in each resultline (for historical, computes the corresponding combination ij),
6394: computes the transition matrix starting at age 'age' over
6395: 'nhstepm*hstepm*stepm' months (i.e. until
6396: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
6397: nhstepm*hstepm matrices.
6398: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
6399: (typically every 2 years instead of every month which is too big
6400: for the memory).
6401: Model is determined by parameters x and covariates have to be
6402: included manually here. Then we use a call to bmij(x and cov)
6403: The addresss of po (p3mat allocated to the dimension of nhstepm) should be stored for output
6404: */
6405:
6406: int i, j, d, h, k, k1;
6407: double **out, cov[NCOVMAX+1], **bmij();
6408: double **newm, ***newmm;
6409: double agexact;
6410: double agebegin, ageend;
6411: double **oldm, **savm;
6412:
6413: newmm=po; /* To be saved */
6414: oldm=oldms;savm=savms; /* Global pointers */
6415: /* Hstepm could be zero and should return the unit matrix */
6416: for (i=1;i<=nlstate+ndeath;i++)
6417: for (j=1;j<=nlstate+ndeath;j++){
6418: oldm[i][j]=(i==j ? 1.0 : 0.0);
6419: po[i][j][0]=(i==j ? 1.0 : 0.0);
6420: }
6421: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
6422: for(h=1; h <=nhstepm; h++){
6423: for(d=1; d <=hstepm; d++){
6424: newm=savm;
6425: /* Covariates have to be included here again */
6426: cov[1]=1.;
6427: agexact=age-( (h-1)*hstepm + (d) )*stepm/YEARM; /* age just before transition, d or d-1? */
6428: /* agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /\* age just before transition *\/ */
6429: /* Debug */
6430: /* printf("hBxij age=%lf, agexact=%lf\n", age, agexact); */
6431: cov[2]=agexact;
6432: if(nagesqr==1){
6433: cov[3]= agexact*agexact;
6434: }
6435: /** New code */
6436: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
6437: if(Typevar[k1]==1 || Typevar[k1]==3){ /* A product with age */
6438: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
6439: }else{
6440: cov[2+nagesqr+k1]=precov[nres][k1];
6441: }
6442: }/* End of loop on model equation */
6443: /** End of new code */
6444: /** This was old code */
6445: /* for (k=1; k<=nsd;k++){ /\* For single dummy covariates only *\//\* cptcovn error *\/ */
6446: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; *\/ */
6447: /* /\* /\\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; *\\/ *\/ */
6448: /* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,TvarsD[k])];/\* Bug valgrind *\/ */
6449: /* /\* 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)); *\/ */
6450: /* } */
6451: /* for (k=1; k<=nsq;k++) { /\* For single varying covariates only *\/ */
6452: /* /\* Here comes the value of quantitative after renumbering k with single quantitative covariates *\/ */
6453: /* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; */
6454: /* /\* 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]); *\/ */
6455: /* } */
6456: /* for (k=1; k<=cptcovage;k++){ /\* Should start at cptcovn+1 *\//\* For product with age *\/ */
6457: /* /\* if(Dummy[Tvar[Tage[k]]]== 2){ /\\* dummy with age error!!!*\\/ *\/ */
6458: /* if(Dummy[Tage[k]]== 2){ /\* dummy with age *\/ */
6459: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
6460: /* } else if(Dummy[Tage[k]]== 3){ /\* quantitative with age *\/ */
6461: /* cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; */
6462: /* } */
6463: /* /\* 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]); *\/ */
6464: /* } */
6465: /* for (k=1; k<=cptcovprod;k++){ /\* Useless because included in cptcovn *\/ */
6466: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])]*nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
6467: /* if(Dummy[Tvard[k][1]]==0){ */
6468: /* if(Dummy[Tvard[k][2]]==0){ */
6469: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][1])]; */
6470: /* }else{ */
6471: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * Tqresult[nres][k]; */
6472: /* } */
6473: /* }else{ */
6474: /* if(Dummy[Tvard[k][2]]==0){ */
6475: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])] * Tqinvresult[nres][Tvard[k][1]]; */
6476: /* }else{ */
6477: /* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; */
6478: /* } */
6479: /* } */
6480: /* } */
6481: /* /\*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*\/ */
6482: /* /\*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*\/ */
6483: /** End of old code */
6484:
6485: /* Careful transposed matrix */
6486: /* age is in cov[2], prevacurrent at beginning of transition. */
6487: /* out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm, dsavm,ij),\ */
6488: /* 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); */
6489: out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij),\
6490: 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm);/* Bug valgrind */
6491: /* if((int)age == 70){ */
6492: /* printf(" Backward hbxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
6493: /* for(i=1; i<=nlstate+ndeath; i++) { */
6494: /* printf("%d pmmij ",i); */
6495: /* for(j=1;j<=nlstate+ndeath;j++) { */
6496: /* printf("%f ",pmmij[i][j]); */
6497: /* } */
6498: /* printf(" oldm "); */
6499: /* for(j=1;j<=nlstate+ndeath;j++) { */
6500: /* printf("%f ",oldm[i][j]); */
6501: /* } */
6502: /* printf("\n"); */
6503: /* } */
6504: /* } */
6505: savm=oldm;
6506: oldm=newm;
6507: }
6508: for(i=1; i<=nlstate+ndeath; i++)
6509: for(j=1;j<=nlstate+ndeath;j++) {
6510: po[i][j][h]=newm[i][j];
6511: /* if(h==nhstepm) */
6512: /* printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]); */
6513: }
6514: /* printf("h=%d %.1f ",h, agexact); */
6515: } /* end h */
6516: /* printf("\n H=%d nhs=%d \n",h, nhstepm); */
6517: return po;
6518: }
6519:
6520:
6521: #ifdef NLOPT
6522: double myfunc(unsigned n, const double *p1, double *grad, void *pd){
6523: double fret;
6524: double *xt;
6525: int j;
6526: myfunc_data *d2 = (myfunc_data *) pd;
6527: /* xt = (p1-1); */
6528: xt=vector(1,n);
6529: for (j=1;j<=n;j++) xt[j]=p1[j-1]; /* xt[1]=p1[0] */
6530:
6531: fret=(d2->function)(xt); /* p xt[1]@8 is fine */
6532: /* fret=(*func)(xt); /\* p xt[1]@8 is fine *\/ */
6533: printf("Function = %.12lf ",fret);
6534: for (j=1;j<=n;j++) printf(" %d %.8lf", j, xt[j]);
6535: printf("\n");
6536: free_vector(xt,1,n);
6537: return fret;
6538: }
6539: #endif
6540:
6541: /*************** log-likelihood *************/
6542: double func( double *x)
6543: {
6544: int i, ii, j, k, mi, d, kk, kf=0;
6545: int ioffset=0;
6546: int ipos=0,iposold=0,ncovv=0;
6547:
6548: double cotvarv, cotvarvold;
6549: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
6550: double **out;
6551: double lli; /* Individual log likelihood */
6552: int s1, s2;
6553: int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate, quantitative time varying covariate */
6554:
6555: double bbh, survp;
6556: double agexact;
6557: double agebegin, ageend;
6558: /*extern weight */
6559: /* We are differentiating ll according to initial status */
6560: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
6561: /*for(i=1;i<imx;i++)
6562: printf(" %d\n",s[4][i]);
6563: */
6564:
6565: ++countcallfunc;
6566:
6567: cov[1]=1.;
6568:
6569: for(k=1; k<=nlstate; k++) ll[k]=0.;
6570: ioffset=0;
6571: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
6572: /* Computes the values of the ncovmodel covariates of the model
6573: depending if the covariates are fixed or varying (age dependent) and stores them in cov[]
6574: Then computes with function pmij which return a matrix p[i][j] giving the elementary probability
6575: to be observed in j being in i according to the model.
6576: */
6577: ioffset=2+nagesqr ;
6578: /* Fixed */
6579: for (kf=1; kf<=ncovf;kf++){ /* For each fixed covariate dummy or quant or prod */
6580: /* # V1=sex, V2=raedyrs Quant Fixed, State=livarnb4..livarnb11, V3=iadl4..iald11, V4=adlw4..adlw11, V5=r4bmi..r11bmi */
6581: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
6582: /* 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 */
6583: /* TvarFind; TvarFind[1]=6, TvarFind[2]=7, TvarFind[3]=9 *//* Inverse V2(6) is first fixed (single or prod) */
6584: 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)*/
6585: /* V1*V2 (7) TvarFind[2]=7, TvarFind[3]=9 */
6586: }
6587: /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4]
6588: is 5, Tvar[3=age*V3] should not be computed because of age Tvar[4=V3*V2]=6
6589: has been calculated etc */
6590: /* For an individual i, wav[i] gives the number of effective waves */
6591: /* We compute the contribution to Likelihood of each effective transition
6592: mw[mi][i] is real wave of the mi th effectve wave */
6593: /* Then statuses are computed at each begin and end of an effective wave s1=s[ mw[mi][i] ][i];
6594: s2=s[mw[mi+1][i]][i];
6595: And the iv th varying covariate is the cotvar[mw[mi+1][i]][iv][i] because now is moved after nvocol+nqv
6596: But if the variable is not in the model TTvar[iv] is the real variable effective in the model:
6597: meaning that decodemodel should be used cotvar[mw[mi+1][i]][TTvar[iv]][i]
6598: */
6599: for(mi=1; mi<= wav[i]-1; mi++){ /* Varying with waves */
6600: /* Wave varying (but not age varying) */
6601: /* 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*\/ */
6602: /* /\* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; but where is the crossproduct? *\/ */
6603: /* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]-ncovcol-nqv][i]; */
6604: /* } */
6605: for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){ /* Varying covariates (single and product but no age )*/
6606: itv=TvarVV[ncovv]; /* TvarVV={3, 1, 3} gives the name of each varying covariate */
6607: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
6608: if(FixedV[itv]!=0){ /* Not a fixed covariate */
6609: cotvarv=cotvar[mw[mi][i]][TvarVV[ncovv]][i]; /* cotvar[wav][ncovcol+nqv+iv][i] */
6610: }else{ /* fixed covariate */
6611: cotvarv=covar[itv][i]; /* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model */
6612: }
6613: if(ipos!=iposold){ /* Not a product or first of a product */
6614: cotvarvold=cotvarv;
6615: }else{ /* A second product */
6616: cotvarv=cotvarv*cotvarvold;
6617: }
6618: iposold=ipos;
6619: cov[ioffset+ipos]=cotvarv;
6620: }
6621: /* for(itv=1; itv <= ntveff; itv++){ /\* Varying dummy covariates (single??)*\/ */
6622: /* iv= Tvar[Tmodelind[ioffset-2-nagesqr-cptcovage+itv]]-ncovcol-nqv; /\* Counting the # varying covariate from 1 to ntveff *\/ */
6623: /* cov[ioffset+iv]=cotvar[mw[mi][i]][iv][i]; */
6624: /* k=ioffset-2-nagesqr-cptcovage+itv; /\* position in simple model *\/ */
6625: /* cov[ioffset+itv]=cotvar[mw[mi][i]][TmodelInvind[itv]][i]; */
6626: /* 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]); */
6627: /* } */
6628: /* for(iqtv=1; iqtv <= nqtveff; iqtv++){ /\* Varying quantitatives covariates *\/ */
6629: /* iv=TmodelInvQind[iqtv]; /\* Counting the # varying covariate from 1 to ntveff *\/ */
6630: /* /\* 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]); *\/ */
6631: /* cov[ioffset+ntveff+iqtv]=cotqvar[mw[mi][i]][TmodelInvQind[iqtv]][i]; */
6632: /* } */
6633: /* for products of time varying to be done */
6634: for (ii=1;ii<=nlstate+ndeath;ii++)
6635: for (j=1;j<=nlstate+ndeath;j++){
6636: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
6637: savm[ii][j]=(ii==j ? 1.0 : 0.0);
6638: }
6639:
6640: agebegin=agev[mw[mi][i]][i]; /* Age at beginning of effective wave */
6641: ageend=agev[mw[mi][i]][i] + (dh[mi][i])*stepm/YEARM; /* Age at end of effective wave and at the end of transition */
6642: for(d=0; d<dh[mi][i]; d++){
6643: newm=savm;
6644: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
6645: cov[2]=agexact;
6646: if(nagesqr==1)
6647: cov[3]= agexact*agexact; /* Should be changed here */
6648: for (kk=1; kk<=cptcovprodage;kk++) {/* + age*V3*V2 +age*V2 +age*V3 +age*V4 For age product with simple covariates or product of fixed covariates */
6649: /* if(!FixedV[Tvar[Tage[kk]]]) */
6650: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; /* Tage[kk] gives the data-covariate associated with age */
6651: /* else*/
6652: /*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) */
6653: }
6654: for(ncovva=1, iposold=0; ncovva <= ncovvta ; ncovva++){ /* Time varying covariates with age including individual from products, product is computed dynamically */
6655: itv=TvarVVA[ncovva]; /* TvarVV={3, 1, 3} gives the name of each varying covariate, exploding product Vn*Vm into Vn and then Vm */
6656: ipos=TvarVVAind[ncovva]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
6657: if(FixedV[itv]!=0){ /* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv */
6658: cotvarv=cotvar[mw[mi][i]][TvarVVA[ncovva]][i]; /* because cotvar starts now at first ncovcol+nqv+ntv+nqtv (1 to nqtv) */
6659: }else{ /* fixed covariate */
6660: cotvarv=covar[itv][i]; /* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model */
6661: }
6662: if(ipos!=iposold){ /* Not a product or first of a product */
6663: cotvarvold=cotvarv;
6664: }else{ /* A second product */
6665: cotvarv=cotvarv*cotvarvold;
6666: }
6667: iposold=ipos;
6668: cov[ioffset+ipos]=cotvarv*agexact;
6669: /* For products */
6670: }
6671:
6672: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
6673: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
6674: savm=oldm;
6675: oldm=newm;
6676: } /* end mult */
6677:
6678: /*lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ /* Original formula */
6679: /* But now since version 0.9 we anticipate for bias at large stepm.
6680: * If stepm is larger than one month (smallest stepm) and if the exact delay
6681: * (in months) between two waves is not a multiple of stepm, we rounded to
6682: * the nearest (and in case of equal distance, to the lowest) interval but now
6683: * we keep into memory the bias bh[mi][i] and also the previous matrix product
6684: * (i.e to dh[mi][i]-1) saved in 'savm'. Then we inter(extra)polate the
6685: * probability in order to take into account the bias as a fraction of the way
6686: * from savm to out if bh is negative or even beyond if bh is positive. bh varies
6687: * -stepm/2 to stepm/2 .
6688: * For stepm=1 the results are the same as for previous versions of Imach.
6689: * For stepm > 1 the results are less biased than in previous versions.
6690: */
6691: s1=s[mw[mi][i]][i];
6692: s2=s[mw[mi+1][i]][i];
6693: bbh=(double)bh[mi][i]/(double)stepm;
6694: /* bias bh is positive if real duration
6695: * is higher than the multiple of stepm and negative otherwise.
6696: */
6697: /* lli= (savm[s1][s2]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2]));*/
6698: if( s2 > nlstate){
6699: /* i.e. if s2 is a death state and if the date of death is known
6700: then the contribution to the likelihood is the probability to
6701: die between last step unit time and current step unit time,
6702: which is also equal to probability to die before dh
6703: minus probability to die before dh-stepm .
6704: In version up to 0.92 likelihood was computed
6705: as if date of death was unknown. Death was treated as any other
6706: health state: the date of the interview describes the actual state
6707: and not the date of a change in health state. The former idea was
6708: to consider that at each interview the state was recorded
6709: (healthy, disable or death) and IMaCh was corrected; but when we
6710: introduced the exact date of death then we should have modified
6711: the contribution of an exact death to the likelihood. This new
6712: contribution is smaller and very dependent of the step unit
6713: stepm. It is no more the probability to die between last interview
6714: and month of death but the probability to survive from last
6715: interview up to one month before death multiplied by the
6716: probability to die within a month. Thanks to Chris
6717: Jackson for correcting this bug. Former versions increased
6718: mortality artificially. The bad side is that we add another loop
6719: which slows down the processing. The difference can be up to 10%
6720: lower mortality.
6721: */
6722: /* If, at the beginning of the maximization mostly, the
6723: cumulative probability or probability to be dead is
6724: constant (ie = 1) over time d, the difference is equal to
6725: 0. out[s1][3] = savm[s1][3]: probability, being at state
6726: s1 at precedent wave, to be dead a month before current
6727: wave is equal to probability, being at state s1 at
6728: precedent wave, to be dead at mont of the current
6729: wave. Then the observed probability (that this person died)
6730: is null according to current estimated parameter. In fact,
6731: it should be very low but not zero otherwise the log go to
6732: infinity.
6733: */
6734: /* #ifdef INFINITYORIGINAL */
6735: /* lli=log(out[s1][s2] - savm[s1][s2]); */
6736: /* #else */
6737: /* if ((out[s1][s2] - savm[s1][s2]) < mytinydouble) */
6738: /* lli=log(mytinydouble); */
6739: /* else */
6740: /* lli=log(out[s1][s2] - savm[s1][s2]); */
6741: /* #endif */
6742: lli=log(out[s1][s2] - savm[s1][s2]);
6743:
6744: } else if ( s2==-1 ) { /* alive */
6745: for (j=1,survp=0. ; j<=nlstate; j++)
6746: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
6747: /*survp += out[s1][j]; */
6748: lli= log(survp);
6749: }
6750: /* else if (s2==-4) { */
6751: /* for (j=3,survp=0. ; j<=nlstate; j++) */
6752: /* survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j]; */
6753: /* lli= log(survp); */
6754: /* } */
6755: /* else if (s2==-5) { */
6756: /* for (j=1,survp=0. ; j<=2; j++) */
6757: /* survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j]; */
6758: /* lli= log(survp); */
6759: /* } */
6760: else if (mle==1){
6761: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
6762: /* 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 */
6763: } else if(mle==2){
6764: 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 */
6765: } else if(mle==3){ /* exponential inter-extrapolation */
6766: 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 */
6767: } else if (mle==4){ /* mle=4 no inter-extrapolation */
6768: lli=log(out[s1][s2]); /* Original formula */
6769: } else{ /* mle=0 back to 1 */
6770: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
6771: /*lli=log(out[s1][s2]); */ /* Original formula */
6772: }
6773: /*lli=(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]);*/
6774: /*if(lli ==000.0)*/
6775: /* 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); */
6776: ipmx +=1;
6777: sw += weight[i];
6778: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
6779: /* if (lli < log(mytinydouble)){ */
6780: /* 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); */
6781: /* 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]); */
6782: /* } */
6783: } /* end of wave */
6784: } /* end of individual */
6785: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
6786: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
6787: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
6788: return -l;
6789: }
6790:
6791: /*************** log-likelihood *************/
6792: double funcone( double *x)
6793: {
6794: /* Same as func but slower because of a lot of printf and if */
6795: int i, ii, j, k, mi, d, kk, kv=0, kf=0;
6796: int ioffset=0;
6797: int ipos=0,iposold=0,ncovv=0;
6798:
6799: char labficresilk[NCOVMAX+1];
6800: double cotvarv, cotvarvold;
6801: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
6802: double **out;
6803: double lli; /* Individual log likelihood */
6804: double llt;
6805: int s1, s2;
6806: int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate, quantitative time varying covariate */
6807:
6808: double bbh, survp;
6809: double agexact;
6810: double agebegin, ageend;
6811: /*extern weight */
6812: /* We are differentiating ll according to initial status */
6813: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
6814: /*for(i=1;i<imx;i++)
6815: printf(" %d\n",s[4][i]);
6816: */
6817: cov[1]=1.;
6818:
6819: for(k=1; k<=nlstate; k++) ll[k]=0.;
6820: ioffset=0;
6821: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
6822: /* Computes the values of the ncovmodel covariates of the model
6823: depending if the covariates are fixed or varying (age dependent) and stores them in cov[]
6824: Then computes with function pmij which return a matrix p[i][j] giving the elementary probability
6825: to be observed in j being in i according to the model.
6826: */
6827: /* ioffset=2+nagesqr+cptcovage; */
6828: ioffset=2+nagesqr;
6829: /* Fixed */
6830: /* for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i]; */
6831: /* for (k=1; k<=ncoveff;k++){ /\* Simple and product fixed Dummy covariates without age* products *\/ */
6832: /*strcpy(labficresilk," "); */ /* A string fed with values of covariates and print in ficresilk at the end of the line to produce graphics with covariate values and also in order to verify the process of calculating the likelihood */
6833: for (kf=1; kf<=ncovf;kf++){ /* V2 + V3 + V4 Simple and product fixed covariates without age* products *//* Missing values are set to -1 but should be dropped */
6834: /* printf("Debug3 TvarFind[%d]=%d",kf, TvarFind[kf]); */
6835: /* printf(" Tvar[TvarFind[kf]]=%d", Tvar[TvarFind[kf]]); */
6836: /* printf(" i=%d covar[Tvar[TvarFind[kf]]][i]=%f\n",i,covar[Tvar[TvarFind[kf]]][i]); */
6837: ipos=TvarFind[kf];
6838: 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)*/
6839: /* if(globpr) */
6840: /* sprintf(labficresilk+strlen(labficresilk)," %g",cov[ioffset+ipos]); */
6841: /* cov[ioffset+TvarFind[1]]=covar[Tvar[TvarFind[1]]][i]; */
6842: /* cov[2+6]=covar[Tvar[6]][i]; */
6843: /* cov[2+6]=covar[2][i]; V2 */
6844: /* cov[TvarFind[2]]=covar[Tvar[TvarFind[2]]][i]; */
6845: /* cov[2+7]=covar[Tvar[7]][i]; */
6846: /* cov[2+7]=covar[7][i]; V7=V1*V2 */
6847: /* cov[TvarFind[3]]=covar[Tvar[TvarFind[3]]][i]; */
6848: /* cov[2+9]=covar[Tvar[9]][i]; */
6849: /* cov[2+9]=covar[1][i]; V1 */
6850: }
6851: /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4]
6852: is 5, Tvar[3=age*V3] should not be computed because of age Tvar[4=V3*V2]=6
6853: has been calculated etc */
6854: /* For an individual i, wav[i] gives the number of effective waves */
6855: /* We compute the contribution to Likelihood of each effective transition
6856: mw[mi][i] is real wave of the mi th effectve wave */
6857: /* Then statuses are computed at each begin and end of an effective wave s1=s[ mw[mi][i] ][i];
6858: s2=s[mw[mi+1][i]][i];
6859: And the iv th varying covariate in the DATA is the cotvar[mw[mi+1][i]][ncovcol+nqv+iv][i]
6860: */
6861: /* This part may be useless now because everythin should be in covar */
6862: /* for (k=1; k<=nqfveff;k++){ /\* Simple and product fixed Quantitative covariates without age* products *\/ */
6863: /* 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?)*\/ */
6864: /* } */
6865: /* for(iqv=1; iqv <= nqfveff; iqv++){ /\* Quantitative fixed covariates *\/ */
6866: /* cov[++ioffset]=coqvar[Tvar[iqv]][i]; /\* Only V2 k=6 and V1*V2 7 *\/ */
6867: /* } */
6868:
6869:
6870: for(mi=1; mi<= wav[i]-1; mi++){ /* Varying with waves */
6871: /* 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 */
6872: /* for(k=1; k <= ncovv ; k++){ /\* Varying covariates (single and product but no age )*\/ */
6873: /* /\* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; *\/ */
6874: /* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]-ncovcol-nqv][i]; */
6875: /* } */
6876:
6877: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
6878: /* model V1+V3+age*V1+age*V3+V1*V3 */
6879: /* Tvar={1, 3, 1, 3, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
6880: /* TvarVV[1]=V3 (first time varying in the model equation, TvarVV[2]=V1 (in V1*V3) TvarVV[3]=3(V3) */
6881: /* We need the position of the time varying or product in the model */
6882: /* 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 */
6883: /* TvarVV gives the variable name */
6884: /* Other example V1 + V3 + V5 + age*V1 + age*V3 + age*V5 + V1*V3 + V3*V5 + V1*V5
6885: * k= 1 2 3 4 5 6 7 8 9
6886: * varying 1 2 3 4 5
6887: * ncovv 1 2 3 4 5 6 7 8
6888: * TvarVV[ncovv] V3 5 1 3 3 5 1 5
6889: * TvarVVind 2 3 7 7 8 8 9 9
6890: * TvarFind[k] 1 0 0 0 0 0 0 0 0
6891: */
6892: /* Other model ncovcol=5 nqv=0 ntv=3 nqtv=0 nlstate=3
6893: * V2 V3 V4 are fixed V6 V7 are timevarying so V8 and V5 are not in the model and product column will start at 9 Tvar[(v6*V2)6]=9
6894: * FixedV[ncovcol+qv+ntv+nqtv] V5
6895: * 3 V1 V2 V3 V4 V5 V6 V7 V8 V3*V2 V7*V2 V6*V3 V7*V3 V6*V4 V7*V4
6896: * 0 0 0 0 0 1 1 1 0, 0, 1,1, 1, 0, 1, 0, 1, 0, 1, 0}
6897: * 3 0 0 0 0 0 1 1 1 0, 1 1 1 1 1}
6898: * model= V2 + V3 + V4 + V6 + V7 + V6*V2 + V7*V2 + V6*V3 + V7*V3 + V6*V4 + V7*V4
6899: * +age*V2 +age*V3 +age*V4 +age*V6 + age*V7
6900: * +age*V6*V2 + age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4
6901: * model2= V2 + V3 + V4 + V6 + V7 + V3*V2 + V7*V2 + V6*V3 + V7*V3 + V6*V4 + V7*V4
6902: * +age*V2 +age*V3 +age*V4 +age*V6 + age*V7
6903: * +age*V3*V2 + age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4
6904: * model3= V2 + V3 + V4 + V6 + V7 + age*V3*V2 + V7*V2 + V6*V3 + V7*V3 + V6*V4 + V7*V4
6905: * +age*V2 +age*V3 +age*V4 +age*V6 + age*V7
6906: * +V3*V2 + age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4
6907: * kmodel 1 2 3 4 5 6 7 8 9 10 11
6908: * 12 13 14 15 16
6909: * 17 18 19 20 21
6910: * Tvar[kmodel] 2 3 4 6 7 9 10 11 12 13 14
6911: * 2 3 4 6 7
6912: * 9 11 12 13 14
6913: * cptcovage=5+5 total of covariates with age
6914: * Tage[cptcovage] age*V2=12 13 14 15 16
6915: *1 17 18 19 20 21 gives the position in model of covariates associated with age
6916: *3 Tage[cptcovage] age*V3*V2=6
6917: *3 age*V2=12 13 14 15 16
6918: *3 age*V6*V3=18 19 20 21
6919: * Tvar[Tage[cptcovage]] Tvar[12]=2 3 4 6 Tvar[16]=7(age*V7)
6920: * Tvar[17]age*V6*V2=9 Tvar[18]age*V6*V3=11 age*V7*V3=12 age*V6*V4=13 Tvar[21]age*V7*V4=14
6921: * 2 Tvar[17]age*V3*V2=9 Tvar[18]age*V6*V3=11 age*V7*V3=12 age*V6*V4=13 Tvar[21]age*V7*V4=14
6922: * 3 Tvar[Tage[cptcovage]] Tvar[6]=9 Tvar[12]=2 3 4 6 Tvar[16]=7(age*V7)
6923: * 3 Tvar[18]age*V6*V3=11 age*V7*V3=12 age*V6*V4=13 Tvar[21]age*V7*V4=14
6924: * 3 Tage[cptcovage] age*V3*V2=6 age*V2=12 age*V3 13 14 15 16
6925: * age*V6*V3=18 19 20 21 gives the position in model of covariates associated with age
6926: * 3 Tvar[17]age*V3*V2=9 Tvar[18]age*V6*V3=11 age*V7*V3=12 age*V6*V4=13 Tvar[21]age*V7*V4=14
6927: * Tvar= {2, 3, 4, 6, 7,
6928: * 9, 10, 11, 12, 13, 14,
6929: * Tvar[12]=2, 3, 4, 6, 7,
6930: * Tvar[17]=9, 11, 12, 13, 14}
6931: * Typevar[1]@21 = {0, 0, 0, 0, 0,
6932: * 2, 2, 2, 2, 2, 2,
6933: * 3 3, 2, 2, 2, 2, 2,
6934: * 1, 1, 1, 1, 1,
6935: * 3, 3, 3, 3, 3}
6936: * 3 2, 3, 3, 3, 3}
6937: * p Tposprod[1]@21 {0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0, 1, 3, 4, 5, 6} Id of the prod at position k in the model
6938: * p Tprod[1]@21 {6, 7, 8, 9, 10, 11, 0 <repeats 15 times>}
6939: * 3 Tposprod[1]@21 {0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0, 1, 3, 4, 5, 6}
6940: * 3 Tprod[1]@21 {17, 7, 8, 9, 10, 11, 0 <repeats 15 times>}
6941: * cptcovprod=11 (6+5)
6942: * FixedV[Tvar[Tage[cptcovage]]]] FixedV[2]=0 FixedV[3]=0 0 1 (age*V7)Tvar[16]=1 FixedV[absolute] not [kmodel]
6943: * FixedV[Tvar[17]=FixedV[age*V6*V2]=FixedV[9]=1 1 1 1 1
6944: * 3 FixedV[Tvar[17]=FixedV[age*V3*V2]=FixedV[9]=0 [11]=1 1 1 1
6945: * FixedV[] V1=0 V2=0 V3=0 v4=0 V5=0 V6=1 V7=1 v8=1 OK then model dependent
6946: * 9=1 [V7*V2]=[10]=1 11=1 12=1 13=1 14=1
6947: * 3 9=0 [V7*V2]=[10]=1 11=1 12=1 13=1 14=1
6948: * cptcovdageprod=5 for gnuplot printing
6949: * cptcovprodvage=6
6950: * ncova=15 1 2 3 4 5
6951: * 6 7 8 9 10 11 12 13 14 15
6952: * TvarA 2 3 4 6 7
6953: * 6 2 6 7 7 3 6 4 7 4
6954: * TvaAind 12 12 13 13 14 14 15 15 16 16
6955: * ncovf 1 2 3
6956: * V6 V7 V6*V2 V7*V2 V6*V3 V7*V3 V6*V4 V7*V4
6957: * ncovvt=14 1 2 3 4 5 6 7 8 9 10 11 12 13 14
6958: * TvarVV[1]@14 = itv {V6=6, 7, V6*V2=6, 2, 7, 2, 6, 3, 7, 3, 6, 4, 7, 4}
6959: * TvarVVind[1]@14= {4, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11}
6960: * 3 ncovvt=12 V6 V7 V7*V2 V6*V3 V7*V3 V6*V4 V7*V4
6961: * 3 TvarVV[1]@12 = itv {6, 7, V7*V2=7, 2, 6, 3, 7, 3, 6, 4, 7, 4}
6962: * 3 1 2 3 4 5 6 7 8 9 10 11 12
6963: * TvarVVind[1]@12= {V6 is in k=4, 5, 7,(4isV2)=7, 8, 8, 9, 9, 10,10, 11,11}TvarVVind[12]=k=11
6964: * TvarV 6, 7, 9, 10, 11, 12, 13, 14
6965: * 3 cptcovprodvage=6
6966: * 3 ncovta=15 +age*V3*V2+age*V2+agev3+ageV4 +age*V6 + age*V7 + age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4
6967: * 3 TvarAVVA[1]@15= itva 3 2 2 3 4 6 7 6 3 7 3 6 4 7 4
6968: * 3 ncovta 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
6969: *
6970: * TvarAVVAind[1]@15= V3 is in k=6 6 12 13 14 15 16 18 18 19,19, 20,20 21,21}TvarVVAind[]
6971: * 3 ncovvta=10 +age*V6 + age*V7 + age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4
6972: * 3 we want to compute =cotvar[mw[mi][i]][TvarVVA[ncovva]][i] at position TvarVVAind[ncovva]
6973: * 3 TvarVVA[1]@10= itva 6 7 6 3 7 3 6 4 7 4
6974: * 3 ncovva 1 2 3 4 5 6 7 8 9 10
6975: * TvarVVAind[1]@10= V6 is in k=4 5 8,8 9, 9, 10,10 11 11}TvarVVAind[]
6976: * TvarVVAind[1]@10= 15 16 18,18 19,19, 20,20 21 21}TvarVVAind[]
6977: * TvarVA V3*V2=6 6 , 1, 2, 11, 12, 13, 14
6978: * TvarFind[1]@14= {1, 2, 3, 0 <repeats 12 times>}
6979: * Tvar[1]@21= {2, 3, 4, 6, 7, 9, 10, 11, 12, 13, 14,
6980: * 2, 3, 4, 6, 7,
6981: * 6, 8, 9, 10, 11}
6982: * TvarFind[itv] 0 0 0
6983: * FixedV[itv] 1 1 1 0 1 0 1 0 1 0 1 0 1 0
6984: * Tvar[TvarFind[ncovf]]=[1]=2 [2]=3 [4]=4
6985: * Tvar[TvarFind[itv]] [0]=? ?ncovv 1 à ncovvt]
6986: * Not a fixed cotvar[mw][itv][i] 6 7 6 2 7, 2, 6, 3, 7, 3, 6, 4, 7, 4}
6987: * fixed covar[itv] [6] [7] [6][2]
6988: */
6989:
6990: for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){ /* V6 V7 V7*V2 V6*V3 V7*V3 V6*V4 V7*V4 Time varying covariates (single and extended product but no age) including individual from products, product is computed dynamically */
6991: itv=TvarVV[ncovv]; /* TvarVV={3, 1, 3} gives the name of each varying covariate, or fixed covariate of a varying product after exploding product Vn*Vm into Vn and then Vm */
6992: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
6993: /* if(TvarFind[itv]==0){ /\* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv *\/ */
6994: if(FixedV[itv]!=0){ /* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv */
6995: /* printf("DEBUG ncovv=%d, Varying TvarVV[ncovv]=%d\n",ncovv, TvarVV[ncovv]); */
6996: cotvarv=cotvar[mw[mi][i]][TvarVV[ncovv]][i]; /* because cotvar starts now at first ncovcol+nqv+ntv+nqtv (1 to nqtv) */
6997: /* printf("DEBUG Varying cov[ioffset+ipos=%d]=%g \n",ioffset+ipos,cotvarv); */
6998: }else{ /* fixed covariate */
6999: /* cotvarv=covar[Tvar[TvarFind[itv]]][i]; /\* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model *\/ */
7000: /* printf("DEBUG ncovv=%d, Fixed TvarVV[ncovv]=%d\n",ncovv, TvarVV[ncovv]); */
7001: cotvarv=covar[itv][i]; /* Good: In V6*V3, 3 is fixed at position of the data */
7002: /* printf("DEBUG Fixed cov[ioffset+ipos=%d]=%g \n",ioffset+ipos,cotvarv); */
7003: }
7004: /* if(globpr) */
7005: /* sprintf(labficresilk+strlen(labficresilk)," %g",cotvarv); */
7006: if(ipos!=iposold){ /* Not a product or first of a product */
7007: cotvarvold=cotvarv;
7008: }else{ /* A second product */
7009: cotvarv=cotvarv*cotvarvold;
7010: /* if(globpr) */
7011: /* sprintf(labficresilk+strlen(labficresilk)," *"); */
7012: /* printf("DEBUG * \n"); */
7013: }
7014: iposold=ipos;
7015: cov[ioffset+ipos]=cotvarv;
7016: /* printf("DEBUG Product cov[ioffset+ipos=%d] \n",ioffset+ipos); */
7017: /* For products */
7018: }
7019: /* for(itv=1; itv <= ntveff; itv++){ /\* Varying dummy covariates single *\/ */
7020: /* iv=TvarVDind[itv]; /\* iv, position in the model equation of time varying covariate itv *\/ */
7021: /* /\* "V1+V3+age*V1+age*V3+V1*V3" with V3 time varying *\/ */
7022: /* /\* 1 2 3 4 5 *\/ */
7023: /* /\*itv 1 *\/ */
7024: /* /\* TvarVInd[1]= 2 *\/ */
7025: /* /\* iv= Tvar[Tmodelind[itv]]-ncovcol-nqv; /\\* Counting the # varying covariate from 1 to ntveff *\\/ *\/ */
7026: /* /\* iv= Tvar[Tmodelind[ioffset-2-nagesqr-cptcovage+itv]]-ncovcol-nqv; *\/ */
7027: /* /\* cov[ioffset+iv]=cotvar[mw[mi][i]][iv][i]; *\/ */
7028: /* /\* k=ioffset-2-nagesqr-cptcovage+itv; /\\* position in simple model *\\/ *\/ */
7029: /* /\* cov[ioffset+iv]=cotvar[mw[mi][i]][TmodelInvind[itv]][i]; *\/ */
7030: /* cov[ioffset+iv]=cotvar[mw[mi][i]][itv][i]; */
7031: /* /\* 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]); *\/ */
7032: /* } */
7033: /* for(iqtv=1; iqtv <= nqtveff; iqtv++){ /\* Varying quantitatives covariates *\/ */
7034: /* iv=TmodelInvQind[iqtv]; /\* Counting the # varying covariate from 1 to ntveff *\/ */
7035: /* /\* 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]); *\/ */
7036: /* cov[ioffset+ntveff+iqtv]=cotqvar[mw[mi][i]][TmodelInvQind[iqtv]][i]; */
7037: /* } */
7038: for (ii=1;ii<=nlstate+ndeath;ii++)
7039: for (j=1;j<=nlstate+ndeath;j++){
7040: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
7041: savm[ii][j]=(ii==j ? 1.0 : 0.0);
7042: }
7043:
7044: agebegin=agev[mw[mi][i]][i]; /* Age at beginning of effective wave */
7045: ageend=agev[mw[mi][i]][i] + (dh[mi][i])*stepm/YEARM; /* Age at end of effective wave and at the end of transition */
7046: for(d=0; d<dh[mi][i]; d++){ /* Delay between two effective waves */
7047: /* for(d=0; d<=0; d++){ /\* Delay between two effective waves Only one matrix to speed up*\/ */
7048: /*dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
7049: and mw[mi+1][i]. dh depends on stepm.*/
7050: newm=savm;
7051: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM; /* Here d is needed */
7052: cov[2]=agexact;
7053: if(nagesqr==1)
7054: cov[3]= agexact*agexact;
7055: /* for (kk=1; kk<=cptcovage;kk++) { /\* + age*V3*V2 +age*V2 +age*V3 +age*V4 For age product with simple covariates or product of fixed covariates *\/ */
7056: for (kk=1; kk<=cptcovprodage;kk++) { /* + age*V3*V2 +age*V2 +age*V3 +age*V4 For age product with simple covariates or product of fixed covariates */
7057: ipos=Tage[kk];
7058: if(!FixedV[Tvar[Tage[kk]]]){ /* age*V3*V2 +age*V2 +age*V3 +age*V4 Fixed covariate with age age*V3 or age*V2*V3 Tvar[Tage[kk]] has its own already calculated column */
7059: /* printf("DEBUG kk=%d, Fixed Tvar[Tage[kk]]=%d agexact=%g\n",kk, Tvar[Tage[kk]], agexact); */
7060: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
7061: /* printf("DEBUG Fixed cov[Tage[kk]+2+nagesqr=%d]=%g agexact=%g \n",Tage[kk]+2+nagesqr,cov[Tage[kk]+2+nagesqr], agexact); */
7062: }else{ /* +age*V6 + age*V7 + age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 time varying covariates with age age*V7 or age*V7*V3 is Tvar[Tage[kk]] defined, yes*/
7063: /* printf("DEBUG kk=%d, Varyingd Tvar[Tage[kk]]=%d\n",kk, Tvar[Tage[kk]]); */
7064: cov[Tage[kk]+2+nagesqr]=cotvar[mw[mi][i]][Tvar[Tage[kk]]][i]*agexact; /* Are you sure? because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) */
7065: /* printf("DEBUG Varying cov[Tage[kk]+2+nagesqr=%d]=%g agexact=%g \n",Tage[kk]+2+nagesqr,cov[Tage[kk]+2+nagesqr], agexact); */
7066: }
7067: /* if(globpr) */
7068: /* sprintf(labficresilk+strlen(labficresilk)," %g*ageF",cov[Tage[kk]+2+nagesqr]); */
7069: }
7070: /* For product with age age*Vn*Vm where Vn*Vm is time varying */
7071: /* for(kv=1; kv<=cptcovprodvage;kv++){ /\*HERY? +age*V6 + age*V7 +age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 Number of time varying covariates with age *\/ */
7072: for(ncovva=1, iposold=0; ncovva <= ncovvta ; ncovva++){ /* +age*V6 + age*V7 +age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 Time varying covariates with age including individual from products, product is computed dynamically */
7073: itv=TvarVVA[ncovva]; /* TvarVV={3, 1, 3} gives the name of each varying covariate, exploding product Vn*Vm into Vn and then Vm */
7074: ipos=TvarVVAind[ncovva]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
7075: /* if(TvarFind[itv]==0){ /\* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv *\/ */
7076: if(FixedV[itv]!=0){ /* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv */
7077: /* printf("DEBUG ncovva=%d, Varying TvarVVA[ncovva]=%d agexact=%g\n", ncovva, TvarVVA[ncovva], agexact); */
7078: cotvarv=cotvar[mw[mi][i]][TvarVVA[ncovva]][i]; /* because cotvar starts now at first ncovcol+nqv+ntv+nqtv (1 to nqtv) */
7079: }else{ /* fixed covariate */
7080: /* cotvarv=covar[Tvar[TvarFind[itv]]][i]; /\* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model *\/ */
7081: /* printf("DEBUG ncovva=%d, Fixed TvarVVA[ncovva]=%d agexact=%g\n", ncovva, TvarVVA[ncovva], agexact); */
7082: cotvarv=covar[itv][i]; /* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model */
7083: }
7084: if(ipos!=iposold){ /* Not a product or first of a product */
7085: cotvarvold=cotvarv;
7086: }else{ /* A second product */
7087: /* printf("DEBUG * \n"); */
7088: cotvarv=cotvarv*cotvarvold;
7089: }
7090: iposold=ipos;
7091: cov[ioffset+ipos]=cotvarv*agexact;
7092: /* printf("DEBUG Product cov[ioffset+ipos=%d]=%g, agexact=%g.3 \n",ioffset+ipos,cov[ioffset+ipos], agexact); */
7093: /* if(globpr) */
7094: /* sprintf(labficresilk+strlen(labficresilk)," %g*age",cov[ioffset+ipos]); */
7095:
7096: /* For products */
7097: }
7098:
7099: /* printf("i=%d,mi=%d,d=%d,mw[mi][i]=%d\n",i, mi,d,mw[mi][i]); */
7100: /* for(kk=1;kk<=ncovmodel;kk++){ */
7101: /* printf(" %d=%11.6f",kk,cov[kk]); */
7102: /* } */
7103: /* printf("\n"); */
7104: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
7105: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
7106: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
7107: /* out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath, */
7108: /* 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate)); */
7109: savm=oldm;
7110: oldm=newm;
7111: } /* end mult */
7112: /*lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ /* Original formula */
7113: /* But now since version 0.9 we anticipate for bias at large stepm.
7114: * If stepm is larger than one month (smallest stepm) and if the exact delay
7115: * (in months) between two waves is not a multiple of stepm, we rounded to
7116: * the nearest (and in case of equal distance, to the lowest) interval but now
7117: * we keep into memory the bias bh[mi][i] and also the previous matrix product
7118: * (i.e to dh[mi][i]-1) saved in 'savm'. Then we inter(extra)polate the
7119: * probability in order to take into account the bias as a fraction of the way
7120: * from savm to out if bh is negative or even beyond if bh is positive. bh varies
7121: * -stepm/2 to stepm/2 .
7122: * For stepm=1 the results are the same as for previous versions of Imach.
7123: * For stepm > 1 the results are less biased than in previous versions.
7124: */
7125: s1=s[mw[mi][i]][i];
7126: s2=s[mw[mi+1][i]][i];
7127: /* if(s2==-1){ */
7128: /* printf(" ERROR s1=%d, s2=%d i=%d \n", s1, s2, i); */
7129: /* /\* exit(1); *\/ */
7130: /* } */
7131: bbh=(double)bh[mi][i]/(double)stepm;
7132: /* bias is positive if real duration
7133: * is higher than the multiple of stepm and negative otherwise.
7134: */
7135: if( s2 > nlstate && (mle <5) ){ /* Jackson */
7136: lli=log(out[s1][s2] - savm[s1][s2]);
7137: } else if ( s2==-1 ) { /* alive */
7138: for (j=1,survp=0. ; j<=nlstate; j++)
7139: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
7140: lli= log(survp);
7141: }else if (mle==1){
7142: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
7143: } else if(mle==2){
7144: 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 */
7145: } else if(mle==3){ /* exponential inter-extrapolation */
7146: 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 */
7147: } else if (mle==4){ /* mle=4 no inter-extrapolation */
7148: lli=log(out[s1][s2]); /* Original formula */
7149: } else{ /* mle=0 back to 1 */
7150: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
7151: /*lli=log(out[s1][s2]); */ /* Original formula */
7152: } /* End of if */
7153: ipmx +=1;
7154: sw += weight[i];
7155: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
7156: /* Printing covariates values for each contribution for checking */
7157: /* 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])); */
7158: if(globpr){
7159: fprintf(ficresilk,"%09ld %6.1f %6.1f %6d %2d %2d %2d %2d %3d %15.6f %8.4f %8.3f\
7160: %11.6f %11.6f %11.6f ", \
7161: num[i], agebegin, ageend, i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],weight[i]*gipmx/gsw,
7162: 2*weight[i]*lli,(s2==-1? -1: out[s1][s2]),(s2==-1? -1: savm[s1][s2]));
7163:
7164: /* printf("%09ld %6.1f %6.1f %6d %2d %2d %2d %2d %3d %15.6f %8.4f %8.3f\ */
7165: /* %11.6f %11.6f %11.6f ", \ */
7166: /* num[i], agebegin, ageend, i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],weight[i]*gipmx/gsw, */
7167: /* 2*weight[i]*lli,(s2==-1? -1: out[s1][s2]),(s2==-1? -1: savm[s1][s2])); */
7168: for(k=1,llt=0.,l=0.; k<=nlstate; k++){
7169: llt +=ll[k]*gipmx/gsw;
7170: fprintf(ficresilk," %10.6f",-ll[k]*gipmx/gsw);
7171: /* printf(" %10.6f",-ll[k]*gipmx/gsw); */
7172: }
7173: fprintf(ficresilk," %10.6f ", -llt);
7174: /* fprintf(ficresilk,"%s", labficresilk); */
7175: /* printf(" %10.6f\n", -llt); */
7176: /* if(debugILK){ /\* debugILK is set by a #d in a comment line *\/ */
7177: /* fprintf(ficresilk,"%09ld ", num[i]); */ /* not necessary */
7178: for (kf=1; kf<=ncovf;kf++){ /* Simple and product fixed covariates without age* products *//* Missing values are set to -1 but should be dropped */
7179: fprintf(ficresilk," %g",covar[Tvar[TvarFind[kf]]][i]);
7180: }
7181: for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){ /* Varying covariates (single and product but no age) including individual from products */
7182: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
7183: if(ipos!=iposold){ /* Not a product or first of a product */
7184: fprintf(ficresilk," %g",cov[ioffset+ipos]);
7185: /* printf(" %g",cov[ioffset+ipos]); */
7186: }else{
7187: fprintf(ficresilk,"*");
7188: /* printf("*"); */
7189: }
7190: iposold=ipos;
7191: }
7192: for(ncovva=1, iposold=0; ncovva <= ncovvta ; ncovva++){ /* Time varying covariates with age including individual from products, product is computed dynamically */
7193: itv=TvarVVA[ncovva]; /* TvarVV={3, 1, 3} gives the name of each varying covariate, exploding product Vn*Vm into Vn and then Vm */
7194: ipos=TvarVVAind[ncovva]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
7195: fprintf(ficresilk," %g*age",cov[ioffset+ipos]);
7196: }
7197: /* if(FixedV[itv]!=0){ /\* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv *\/ */
7198: /* cotvarv=cotvar[mw[mi][i]][TvarVVA[ncovva]][i]; /\* because cotvar starts now at first ncovcol+nqv+ntv+nqtv (1 to nqtv) *\/ */
7199: /* }else{ /\* fixed covariate *\/ */
7200: /* cotvarv=covar[itv][i]; /\* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model *\/ */
7201: /* } */
7202: /* if(ipos!=iposold){ /\* Not a product or first of a product *\/ */
7203: /* cotvarvold=cotvarv; */
7204: /* }else{ /\* A second product *\/ */
7205: /* cotvarv=cotvarv*cotvarvold; */
7206: /* } */
7207: /* iposold=ipos; */
7208: /* cov[ioffset+ipos]=cotvarv; */
7209: /* /\* For products *\/ */
7210: /* fprintf(ficresilk," %g*age",cotvarv);/\* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) *\/ */
7211: /* } */
7212: /* for (kk=1; kk<=cptcovage;kk++) { */
7213: /* if(!FixedV[Tvar[Tage[kk]]]){ */
7214: /* fprintf(ficresilk," %g*age",covar[Tvar[Tage[kk]]][i]); */
7215: /* /\* printf(" %g*age",covar[Tvar[Tage[kk]]][i]); *\/ */
7216: /* }else{ */
7217: /* fprintf(ficresilk," %g*age",cotvar[mw[mi][i]][Tvar[Tage[kk]]][i]);/\* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) *\/ */
7218: /* /\* printf(" %g*age",cotvar[mw[mi][i]][Tvar[Tage[kk]]][i]);/\\* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) *\\/ *\/ */
7219: /* } */
7220: /* } */
7221: /* printf("\n"); */
7222: /* } /\* End debugILK *\/ */
7223: fprintf(ficresilk,"\n");
7224: /* printf("\n"); */
7225: } /* End if globpr */
7226: } /* end of wave */
7227: } /* end of individual */
7228: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
7229: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
7230: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
7231: if(globpr==0){ /* First time we count the contributions and weights */
7232: gipmx=ipmx;
7233: gsw=sw;
7234: }
7235: return -l;
7236: }
7237:
7238:
7239: /*************** function likelione ***********/
7240: void likelione(FILE *ficres,double p[], int npar, int nlstate, int *globpri, long *ipmx, double *sw, double *fretone, double (*func)(double []))
7241: {
7242: /* This routine should help understanding what is done with
7243: the selection of individuals/waves and
7244: to check the exact contribution to the likelihood.
7245: Plotting could be done.
7246: */
7247: void pstamp(FILE *ficres);
7248: int k, kf, kk, kvar, kvarold, ncovv, iposold, ipos, itv;
7249:
7250: if(*globpri !=0){ /* Just counts and sums, no printings */
7251: strcpy(fileresilk,"ILK_");
7252: strcat(fileresilk,fileresu);
7253: if((ficresilk=fopen(fileresilk,"w"))==NULL) {
7254: printf("Problem with resultfile: %s\n", fileresilk);
7255: fprintf(ficlog,"Problem with resultfile: %s\n", fileresilk);
7256: }
7257: pstamp(ficresilk);fprintf(ficresilk,"# model=1+age+%s\n",model);
7258: 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");
7259: fprintf(ficresilk, "#num_i ageb agend i s1 s2 mi mw dh likeli weight %%weight 2wlli out sav ");
7260: /* i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],2*weight[i]*lli,out[s1][s2],savm[s1][s2]); */
7261: for(k=1; k<=nlstate; k++)
7262: fprintf(ficresilk," -2*gipw/gsw*weight*ll[%d]++",k);
7263: fprintf(ficresilk," -2*gipw/gsw*weight*ll(total) ");
7264:
7265: /* if(debugILK){ /\* debugILK is set by a #d in a comment line *\/ */
7266: for(kf=1;kf <= ncovf; kf++){ /* Fixed covariates */
7267: fprintf(ficresilk,"V%d",Tvar[TvarFind[kf]]);
7268: /* printf("V%d",Tvar[TvarFind[kf]]); */
7269: }
7270: for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){ /* Time varying covariates V6 V7 V7*V2 V6*V3 V7*V3 V6*V4 V7*V4 Time varying covariates (single and extended product but no age) including individual from products, product is computed dynamically */
7271: itv=TvarVV[ncovv]; /* TvarVV={3, 1, 3} gives the name of each varying covariate, or fixed covariate of a varying product after exploding product Vn*Vm into Vn and then Vm */
7272: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate */
7273: if(FixedV[itv]!=0){ /* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv */
7274: kvar=TvarVV[ncovv];
7275: }else{
7276: kvar=itv;
7277: }
7278: if(ipos!=iposold){ /* Not a product or first of a product */
7279: kvarold=kvar;
7280: /* printf(" %d",ipos); */
7281: fprintf(ficresilk," V%d",kvarold);
7282: }else{ /* a second product */
7283: /* printf("*"); */
7284: fprintf(ficresilk,"*");
7285: fprintf(ficresilk," V%d",kvar);
7286: }
7287: iposold=ipos;
7288: }
7289: for (kk=1; kk<=cptcovprodage;kk++) { /* Fixed Covariates with age + age*V3*V2 +age*V2 +age*V3 +age*V4 For age product with simple covariates or product of fixed covariates */
7290: ipos=Tage[kk];
7291: if(!FixedV[Tvar[Tage[kk]]]){/* age*V3*V2 +age*V2 +age*V3 +age*V4 Fixed covariate with age age*V3 or age*V2*V3 Tvar[Tage[kk]] has its own already calculated column */
7292: /* printf(" %d*age(Fixed)",Tvar[Tage[kk]]); */
7293: fprintf(ficresilk," %d*age(Fixed)",Tvar[Tage[kk]]);
7294: }else{
7295: fprintf(ficresilk," %d*age(Varying)",Tvar[Tage[kk]]);/* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) */
7296: /* printf(" %d*age(Varying)",Tvar[Tage[kk]]);/\* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) *\/ */
7297: }
7298: }
7299: for(ncovva=1, iposold=0; ncovva <= ncovvta ; ncovva++){ /* +age*V6 + age*V7 +age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 Time varying covariates with age including individual from products, product is computed dynamically */
7300: itv=TvarVVA[ncovva]; /* TvarVV={3, 1, 3} gives the name of each varying covariate, exploding product Vn*Vm into Vn and then Vm */
7301: ipos=TvarVVAind[ncovva]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
7302: /* if(TvarFind[itv]==0){ /\* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv *\/ */
7303: if(FixedV[itv]!=0){ /* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv */
7304: /* printf("DEBUG ncovva=%d, Varying TvarVVA[ncovva]=%d agexact=%g\n", ncovva, TvarVVA[ncovva], agexact); */
7305: kvar=TvarVVA[ncovva];
7306: /* cotvarv=cotvar[mw[mi][i]][TvarVVA[ncovva]][i]; /\* because cotvar starts now at first ncovcol+nqv+ntv+nqtv (1 to nqtv) *\/ */
7307: }else{ /* fixed covariate */
7308: /* cotvarv=covar[Tvar[TvarFind[itv]]][i]; /\* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model *\/ */
7309: /* printf("DEBUG ncovva=%d, Fixed TvarVVA[ncovva]=%d agexact=%g\n", ncovva, TvarVVA[ncovva], agexact); */
7310: kvar=itv;
7311: /* cotvarv=covar[itv][i]; /\* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model *\/ */
7312: }
7313: if(ipos!=iposold){ /* Not a product or first of a product */
7314: kvarold=kvar;
7315: fprintf(ficresilk," age*V%d",kvarold);
7316: }else{ /* a second product */
7317: /* printf("*"); */
7318: fprintf(ficresilk," *V%d",kvar);
7319: /* printf("DEBUG * \n"); */
7320: }
7321: iposold=ipos;
7322: /* printf("DEBUG Product cov[ioffset+ipos=%d]=%g, agexact=%g.3 \n",ioffset+ipos,cov[ioffset+ipos], agexact); */
7323: /* if(globpr) */
7324: /* sprintf(labficresilk+strlen(labficresilk)," %g*age",cov[ioffset+ipos]); */
7325:
7326: /* For products */
7327: }
7328: /* } /\* End if debugILK *\/ */
7329: /* printf("\n"); */
7330: fprintf(ficresilk,"\n");
7331: } /* End glogpri */
7332:
7333: *fretone=(*func)(p);
7334: if(*globpri !=0){
7335: fclose(ficresilk);
7336: if (mle ==0)
7337: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with initial parameters and mle = %d.",mle);
7338: else if(mle >=1)
7339: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with optimized parameters mle = %d.",mle);
7340: 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));
7341: fprintf(fichtm,"\n<br>Equation of the model: <b>model=1+age+%s</b><br>\n",model);
7342:
7343: 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> \
7344: <img src=\"%s-ori.png\">\n",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
7345: fprintf(fichtm,"<br>- and by state of destination <a href=\"%s-dest.png\">%s-dest.png</a><br> \
7346: <img src=\"%s-dest.png\">\n",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
7347:
7348: for (k=1; k<= nlstate ; k++) {
7349: 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 \
7350: <img src=\"%s-p%dj.png\">\n",k,k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k);
7351: for(kf=1; kf <= ncovf; kf++){ /* For each simple dummy covariate of the model */
7352: /* kvar=Tvar[TvarFind[kf]]; */ /* variable */
7353: 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> \
7354: <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]]);
7355: }
7356: for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){ /* Loop on the time varying extended covariates (with extension of Vn*Vm */
7357: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate */
7358: kvar=TvarVV[ncovv]; /* TvarVV={3, 1, 3} gives the name of each varying covariate */
7359: /* 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]); */
7360: if(ipos!=iposold){ /* Not a product or first of a product */
7361: /* fprintf(ficresilk," V%d",TvarVV[ncovv]); */
7362: /* printf(" DebugILK fichtm ipos=%d != iposold=%d\n", ipos, iposold); */
7363: 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) */
7364: 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> \
7365: <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);
7366: } /* End only for dummies time varying (single?) */
7367: }else{ /* Useless product */
7368: /* printf("*"); */
7369: /* fprintf(ficresilk,"*"); */
7370: }
7371: iposold=ipos;
7372: } /* For each time varying covariate */
7373: } /* End loop on states */
7374:
7375: /* if(debugILK){ */
7376: /* for(kf=1; kf <= ncovf; kf++){ /\* For each simple dummy covariate of the model *\/ */
7377: /* /\* kvar=Tvar[TvarFind[kf]]; *\/ /\* variable *\/ */
7378: /* for (k=1; k<= nlstate ; k++) { */
7379: /* 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> \ */
7380: /* <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]]); */
7381: /* } */
7382: /* } */
7383: /* for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){ /\* Loop on the time varying extended covariates (with extension of Vn*Vm *\/ */
7384: /* ipos=TvarVVind[ncovv]; /\* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate *\/ */
7385: /* kvar=TvarVV[ncovv]; /\* TvarVV={3, 1, 3} gives the name of each varying covariate *\/ */
7386: /* /\* 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]); *\/ */
7387: /* if(ipos!=iposold){ /\* Not a product or first of a product *\/ */
7388: /* /\* fprintf(ficresilk," V%d",TvarVV[ncovv]); *\/ */
7389: /* /\* printf(" DebugILK fichtm ipos=%d != iposold=%d\n", ipos, iposold); *\/ */
7390: /* 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) *\/ */
7391: /* for (k=1; k<= nlstate ; k++) { */
7392: /* 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> \ */
7393: /* <img src=\"%s-p%dj-%d.png\">",k,k,kvar,kvar,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,kvar); */
7394: /* } /\* End state *\/ */
7395: /* } /\* End only for dummies time varying (single?) *\/ */
7396: /* }else{ /\* Useless product *\/ */
7397: /* /\* printf("*"); *\/ */
7398: /* /\* fprintf(ficresilk,"*"); *\/ */
7399: /* } */
7400: /* iposold=ipos; */
7401: /* } /\* For each time varying covariate *\/ */
7402: /* }/\* End debugILK *\/ */
7403: fflush(fichtm);
7404: }/* End globpri */
7405: return;
7406: }
7407:
7408:
7409: /*********** Maximum Likelihood Estimation ***************/
7410:
7411: void mlikeli(FILE *ficres,double p[], int npar, int ncovmodel, int nlstate, double ftol, double (*func)(double []))
7412: {
7413: int i,j,k, jk, jkk=0, iter=0;
7414: double **xi;
7415: double fret;
7416: double fretone; /* Only one call to likelihood */
7417: /* char filerespow[FILENAMELENGTH];*/
7418:
7419: double * p1; /* Shifted parameters from 0 instead of 1 */
7420: #ifdef NLOPT
7421: int creturn;
7422: nlopt_opt opt;
7423: /* double lb[9] = { -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL }; /\* lower bounds *\/ */
7424: double *lb;
7425: double minf; /* the minimum objective value, upon return */
7426:
7427: myfunc_data dinst, *d = &dinst;
7428: #endif
7429:
7430:
7431: xi=matrix(1,npar,1,npar);
7432: for (i=1;i<=npar;i++)
7433: for (j=1;j<=npar;j++)
7434: xi[i][j]=(i==j ? 1.0 : 0.0);
7435: printf("Powell\n"); fprintf(ficlog,"Powell\n");
7436: strcpy(filerespow,"POW_");
7437: strcat(filerespow,fileres);
7438: if((ficrespow=fopen(filerespow,"w"))==NULL) {
7439: printf("Problem with resultfile: %s\n", filerespow);
7440: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
7441: }
7442: fprintf(ficrespow,"# Powell\n# iter -2*LL");
7443: for (i=1;i<=nlstate;i++)
7444: for(j=1;j<=nlstate+ndeath;j++)
7445: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
7446: fprintf(ficrespow,"\n");
7447: #ifdef POWELL
7448: #ifdef LINMINORIGINAL
7449: #else /* LINMINORIGINAL */
7450:
7451: flatdir=ivector(1,npar);
7452: for (j=1;j<=npar;j++) flatdir[j]=0;
7453: #endif /*LINMINORIGINAL */
7454:
7455: #ifdef FLATSUP
7456: powell(p,xi,npar,ftol,&iter,&fret,flatdir,func);
7457: /* reorganizing p by suppressing flat directions */
7458: for(i=1, jk=1; i <=nlstate; i++){
7459: for(k=1; k <=(nlstate+ndeath); k++){
7460: if (k != i) {
7461: printf("%d%d flatdir[%d]=%d",i,k,jk, flatdir[jk]);
7462: if(flatdir[jk]==1){
7463: printf(" To be skipped %d%d flatdir[%d]=%d ",i,k,jk, flatdir[jk]);
7464: }
7465: for(j=1; j <=ncovmodel; j++){
7466: printf("%12.7f ",p[jk]);
7467: jk++;
7468: }
7469: printf("\n");
7470: }
7471: }
7472: }
7473: /* skipping */
7474: /* for(i=1, jk=1, jkk=1;(flatdir[jk]==0)&& (i <=nlstate); i++){ */
7475: for(i=1, jk=1, jkk=1;i <=nlstate; i++){
7476: for(k=1; k <=(nlstate+ndeath); k++){
7477: if (k != i) {
7478: printf("%d%d flatdir[%d]=%d",i,k,jk, flatdir[jk]);
7479: if(flatdir[jk]==1){
7480: printf(" To be skipped %d%d flatdir[%d]=%d jk=%d p[%d] ",i,k,jk, flatdir[jk],jk, jk);
7481: for(j=1; j <=ncovmodel; jk++,j++){
7482: printf(" p[%d]=%12.7f",jk, p[jk]);
7483: /*q[jjk]=p[jk];*/
7484: }
7485: }else{
7486: printf(" To be kept %d%d flatdir[%d]=%d jk=%d q[%d]=p[%d] ",i,k,jk, flatdir[jk],jk, jkk, jk);
7487: for(j=1; j <=ncovmodel; jk++,jkk++,j++){
7488: printf(" p[%d]=%12.7f=q[%d]",jk, p[jk],jkk);
7489: /*q[jjk]=p[jk];*/
7490: }
7491: }
7492: printf("\n");
7493: }
7494: fflush(stdout);
7495: }
7496: }
7497: powell(p,xi,npar,ftol,&iter,&fret,flatdir,func);
7498: #else /* FLATSUP */
7499: /* powell(p,xi,npar,ftol,&iter,&fret,func);*/
7500: /* praxis ( t0, h0, n, prin, x, beale_f ); */
7501: int prin=4;
7502: double h0=0.25;
7503: #include "praxis.h"
7504: /* Be careful that praxis start at x[0] and powell start at p[1] */
7505: /* praxis ( ftol, h0, npar, prin, p, func ); */
7506: p1= (p+1); /* p *(p+1)@8 and p *(p1)@8 are equal p1[0]=p[1] */
7507: printf("Praxis \n");
7508: fprintf(ficlog, "Praxis \n");fflush(ficlog);
7509: praxis ( ftol, h0, npar, prin, p1, func );
7510: printf("End Praxis\n");
7511: #endif /* FLATSUP */
7512:
7513: #ifdef LINMINORIGINAL
7514: #else
7515: free_ivector(flatdir,1,npar);
7516: #endif /* LINMINORIGINAL*/
7517: #endif /* POWELL */
7518:
7519: #ifdef NLOPT
7520: #ifdef NEWUOA
7521: opt = nlopt_create(NLOPT_LN_NEWUOA,npar);
7522: #else
7523: opt = nlopt_create(NLOPT_LN_BOBYQA,npar);
7524: #endif
7525: lb=vector(0,npar-1);
7526: for (i=0;i<npar;i++) lb[i]= -HUGE_VAL;
7527: nlopt_set_lower_bounds(opt, lb);
7528: nlopt_set_initial_step1(opt, 0.1);
7529:
7530: p1= (p+1); /* p *(p+1)@8 and p *(p1)@8 are equal p1[0]=p[1] */
7531: d->function = func;
7532: printf(" Func %.12lf \n",myfunc(npar,p1,NULL,d));
7533: nlopt_set_min_objective(opt, myfunc, d);
7534: nlopt_set_xtol_rel(opt, ftol);
7535: if ((creturn=nlopt_optimize(opt, p1, &minf)) < 0) {
7536: printf("nlopt failed! %d\n",creturn);
7537: }
7538: else {
7539: printf("found minimum after %d evaluations (NLOPT=%d)\n", countcallfunc ,NLOPT);
7540: printf("found minimum at f(%g,%g) = %0.10g\n", p[0], p[1], minf);
7541: iter=1; /* not equal */
7542: }
7543: nlopt_destroy(opt);
7544: #endif
7545: #ifdef FLATSUP
7546: /* npared = npar -flatd/ncovmodel; */
7547: /* xired= matrix(1,npared,1,npared); */
7548: /* paramred= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel); */
7549: /* powell(pred,xired,npared,ftol,&iter,&fret,flatdir,func); */
7550: /* free_matrix(xire,1,npared,1,npared); */
7551: #else /* FLATSUP */
7552: #endif /* FLATSUP */
7553: free_matrix(xi,1,npar,1,npar);
7554: fclose(ficrespow);
7555: printf("\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
7556: fprintf(ficlog,"\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
7557: fprintf(ficres,"#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
7558:
7559: }
7560:
7561: /**** Computes Hessian and covariance matrix ***/
7562: void hesscov(double **matcov, double **hess, double p[], int npar, double delti[], double ftolhess, double (*func)(double []))
7563: {
7564: double **a,**y,*x,pd;
7565: /* double **hess; */
7566: int i, j;
7567: int *indx;
7568:
7569: double hessii(double p[], double delta, int theta, double delti[],double (*func)(double []),int npar);
7570: double hessij(double p[], double **hess, double delti[], int i, int j,double (*func)(double []),int npar);
7571: void lubksb(double **a, int npar, int *indx, double b[]) ;
7572: void ludcmp(double **a, int npar, int *indx, double *d) ;
7573: double gompertz(double p[]);
7574: /* hess=matrix(1,npar,1,npar); */
7575:
7576: printf("\nCalculation of the hessian matrix. Wait...\n");
7577: fprintf(ficlog,"\nCalculation of the hessian matrix. Wait...\n");
7578: for (i=1;i<=npar;i++){
7579: printf("%d-",i);fflush(stdout);
7580: fprintf(ficlog,"%d-",i);fflush(ficlog);
7581:
7582: hess[i][i]=hessii(p,ftolhess,i,delti,func,npar);
7583:
7584: /* printf(" %f ",p[i]);
7585: printf(" %lf %lf %lf",hess[i][i],ftolhess,delti[i]);*/
7586: }
7587:
7588: for (i=1;i<=npar;i++) {
7589: for (j=1;j<=npar;j++) {
7590: if (j>i) {
7591: printf(".%d-%d",i,j);fflush(stdout);
7592: fprintf(ficlog,".%d-%d",i,j);fflush(ficlog);
7593: hess[i][j]=hessij(p,hess, delti,i,j,func,npar);
7594:
7595: hess[j][i]=hess[i][j];
7596: /*printf(" %lf ",hess[i][j]);*/
7597: }
7598: }
7599: }
7600: printf("\n");
7601: fprintf(ficlog,"\n");
7602:
7603: printf("\nInverting the hessian to get the covariance matrix. Wait...\n");
7604: fprintf(ficlog,"\nInverting the hessian to get the covariance matrix. Wait...\n");
7605:
7606: a=matrix(1,npar,1,npar);
7607: y=matrix(1,npar,1,npar);
7608: x=vector(1,npar);
7609: indx=ivector(1,npar);
7610: for (i=1;i<=npar;i++)
7611: for (j=1;j<=npar;j++) a[i][j]=hess[i][j];
7612: ludcmp(a,npar,indx,&pd);
7613:
7614: for (j=1;j<=npar;j++) {
7615: for (i=1;i<=npar;i++) x[i]=0;
7616: x[j]=1;
7617: lubksb(a,npar,indx,x);
7618: for (i=1;i<=npar;i++){
7619: matcov[i][j]=x[i];
7620: }
7621: }
7622:
7623: printf("\n#Hessian matrix#\n");
7624: fprintf(ficlog,"\n#Hessian matrix#\n");
7625: for (i=1;i<=npar;i++) {
7626: for (j=1;j<=npar;j++) {
7627: printf("%.6e ",hess[i][j]);
7628: fprintf(ficlog,"%.6e ",hess[i][j]);
7629: }
7630: printf("\n");
7631: fprintf(ficlog,"\n");
7632: }
7633:
7634: /* printf("\n#Covariance matrix#\n"); */
7635: /* fprintf(ficlog,"\n#Covariance matrix#\n"); */
7636: /* for (i=1;i<=npar;i++) { */
7637: /* for (j=1;j<=npar;j++) { */
7638: /* printf("%.6e ",matcov[i][j]); */
7639: /* fprintf(ficlog,"%.6e ",matcov[i][j]); */
7640: /* } */
7641: /* printf("\n"); */
7642: /* fprintf(ficlog,"\n"); */
7643: /* } */
7644:
7645: /* Recompute Inverse */
7646: /* for (i=1;i<=npar;i++) */
7647: /* for (j=1;j<=npar;j++) a[i][j]=matcov[i][j]; */
7648: /* ludcmp(a,npar,indx,&pd); */
7649:
7650: /* printf("\n#Hessian matrix recomputed#\n"); */
7651:
7652: /* for (j=1;j<=npar;j++) { */
7653: /* for (i=1;i<=npar;i++) x[i]=0; */
7654: /* x[j]=1; */
7655: /* lubksb(a,npar,indx,x); */
7656: /* for (i=1;i<=npar;i++){ */
7657: /* y[i][j]=x[i]; */
7658: /* printf("%.3e ",y[i][j]); */
7659: /* fprintf(ficlog,"%.3e ",y[i][j]); */
7660: /* } */
7661: /* printf("\n"); */
7662: /* fprintf(ficlog,"\n"); */
7663: /* } */
7664:
7665: /* Verifying the inverse matrix */
7666: #ifdef DEBUGHESS
7667: y=matprod2(y,hess,1,npar,1,npar,1,npar,matcov);
7668:
7669: printf("\n#Verification: multiplying the matrix of covariance by the Hessian matrix, should be unity:#\n");
7670: fprintf(ficlog,"\n#Verification: multiplying the matrix of covariance by the Hessian matrix. Should be unity:#\n");
7671:
7672: for (j=1;j<=npar;j++) {
7673: for (i=1;i<=npar;i++){
7674: printf("%.2f ",y[i][j]);
7675: fprintf(ficlog,"%.2f ",y[i][j]);
7676: }
7677: printf("\n");
7678: fprintf(ficlog,"\n");
7679: }
7680: #endif
7681:
7682: free_matrix(a,1,npar,1,npar);
7683: free_matrix(y,1,npar,1,npar);
7684: free_vector(x,1,npar);
7685: free_ivector(indx,1,npar);
7686: /* free_matrix(hess,1,npar,1,npar); */
7687:
7688:
7689: }
7690:
7691: /*************** hessian matrix ****************/
7692: double hessii(double x[], double delta, int theta, double delti[], double (*func)(double []), int npar)
7693: { /* Around values of x, computes the function func and returns the scales delti and hessian */
7694: int i;
7695: int l=1, lmax=20;
7696: double k1,k2, res, fx;
7697: double p2[MAXPARM+1]; /* identical to x */
7698: double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4;
7699: int k=0,kmax=10;
7700: double l1;
7701:
7702: fx=func(x);
7703: for (i=1;i<=npar;i++) p2[i]=x[i];
7704: for(l=0 ; l <=lmax; l++){ /* Enlarging the zone around the Maximum */
7705: l1=pow(10,l);
7706: delts=delt;
7707: for(k=1 ; k <kmax; k=k+1){
7708: delt = delta*(l1*k);
7709: p2[theta]=x[theta] +delt;
7710: k1=func(p2)-fx; /* Might be negative if too close to the theoretical maximum */
7711: p2[theta]=x[theta]-delt;
7712: k2=func(p2)-fx;
7713: /*res= (k1-2.0*fx+k2)/delt/delt; */
7714: res= (k1+k2)/delt/delt/2.; /* Divided by 2 because L and not 2*L */
7715:
7716: #ifdef DEBUGHESSII
7717: 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);
7718: 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);
7719: #endif
7720: /*if(fabs(k1-2.0*fx+k2) <1.e-13){ */
7721: if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)){
7722: k=kmax;
7723: }
7724: else if((k1 >khi/nkhif) || (k2 >khi/nkhif)){ /* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. */
7725: k=kmax; l=lmax*10;
7726: }
7727: else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){
7728: delts=delt;
7729: }
7730: } /* End loop k */
7731: }
7732: delti[theta]=delts;
7733: return res;
7734:
7735: }
7736:
7737: double hessij( double x[], double **hess, double delti[], int thetai,int thetaj,double (*func)(double []),int npar)
7738: {
7739: int i;
7740: int l=1, lmax=20;
7741: double k1,k2,k3,k4,res,fx;
7742: double p2[MAXPARM+1];
7743: int k, kmax=1;
7744: double v1, v2, cv12, lc1, lc2;
7745:
7746: int firstime=0;
7747:
7748: fx=func(x);
7749: for (k=1; k<=kmax; k=k+10) {
7750: for (i=1;i<=npar;i++) p2[i]=x[i];
7751: p2[thetai]=x[thetai]+delti[thetai]*k;
7752: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
7753: k1=func(p2)-fx;
7754:
7755: p2[thetai]=x[thetai]+delti[thetai]*k;
7756: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
7757: k2=func(p2)-fx;
7758:
7759: p2[thetai]=x[thetai]-delti[thetai]*k;
7760: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
7761: k3=func(p2)-fx;
7762:
7763: p2[thetai]=x[thetai]-delti[thetai]*k;
7764: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
7765: k4=func(p2)-fx;
7766: res=(k1-k2-k3+k4)/4.0/delti[thetai]/k/delti[thetaj]/k/2.; /* Because of L not 2*L */
7767: if(k1*k2*k3*k4 <0.){
7768: firstime=1;
7769: kmax=kmax+10;
7770: }
7771: if(kmax >=10 || firstime ==1){
7772: /* What are the thetai and thetaj? thetai/ncovmodel thetai=(thetai-thetai%ncovmodel)/ncovmodel +thetai%ncovmodel=(line,pos) */
7773: 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);
7774: 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);
7775: 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);
7776: 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);
7777: }
7778: #ifdef DEBUGHESSIJ
7779: v1=hess[thetai][thetai];
7780: v2=hess[thetaj][thetaj];
7781: cv12=res;
7782: /* Computing eigen value of Hessian matrix */
7783: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
7784: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
7785: if ((lc2 <0) || (lc1 <0) ){
7786: printf("Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
7787: fprintf(ficlog, "Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
7788: 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);
7789: 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);
7790: }
7791: #endif
7792: }
7793: return res;
7794: }
7795:
7796: /* Not done yet: Was supposed to fix if not exactly at the maximum */
7797: /* double hessij( double x[], double delti[], int thetai,int thetaj,double (*func)(double []),int npar) */
7798: /* { */
7799: /* int i; */
7800: /* int l=1, lmax=20; */
7801: /* double k1,k2,k3,k4,res,fx; */
7802: /* double p2[MAXPARM+1]; */
7803: /* double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4; */
7804: /* int k=0,kmax=10; */
7805: /* double l1; */
7806:
7807: /* fx=func(x); */
7808: /* for(l=0 ; l <=lmax; l++){ /\* Enlarging the zone around the Maximum *\/ */
7809: /* l1=pow(10,l); */
7810: /* delts=delt; */
7811: /* for(k=1 ; k <kmax; k=k+1){ */
7812: /* delt = delti*(l1*k); */
7813: /* for (i=1;i<=npar;i++) p2[i]=x[i]; */
7814: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
7815: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
7816: /* k1=func(p2)-fx; */
7817:
7818: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
7819: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
7820: /* k2=func(p2)-fx; */
7821:
7822: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
7823: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
7824: /* k3=func(p2)-fx; */
7825:
7826: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
7827: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
7828: /* k4=func(p2)-fx; */
7829: /* res=(k1-k2-k3+k4)/4.0/delti[thetai]*k/delti[thetaj]*k/2.; /\* Because of L not 2*L *\/ */
7830: /* #ifdef DEBUGHESSIJ */
7831: /* 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); */
7832: /* 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); */
7833: /* #endif */
7834: /* if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)){ */
7835: /* k=kmax; */
7836: /* } */
7837: /* else if((k1 >khi/nkhif) || (k2 >khi/nkhif) || (k4 >khi/nkhif) || (k4 >khi/nkhif)){ /\* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. *\/ */
7838: /* k=kmax; l=lmax*10; */
7839: /* } */
7840: /* else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){ */
7841: /* delts=delt; */
7842: /* } */
7843: /* } /\* End loop k *\/ */
7844: /* } */
7845: /* delti[theta]=delts; */
7846: /* return res; */
7847: /* } */
7848:
7849:
7850: /************** Inverse of matrix **************/
7851: void ludcmp(double **a, int n, int *indx, double *d)
7852: {
7853: int i,imax,j,k;
7854: double big,dum,sum,temp;
7855: double *vv;
7856:
7857: vv=vector(1,n);
7858: *d=1.0;
7859: for (i=1;i<=n;i++) {
7860: big=0.0;
7861: for (j=1;j<=n;j++)
7862: if ((temp=fabs(a[i][j])) > big) big=temp;
7863: if (big == 0.0){
7864: printf(" Singular Hessian matrix at row %d:\n",i);
7865: for (j=1;j<=n;j++) {
7866: printf(" a[%d][%d]=%f,",i,j,a[i][j]);
7867: fprintf(ficlog," a[%d][%d]=%f,",i,j,a[i][j]);
7868: }
7869: fflush(ficlog);
7870: fclose(ficlog);
7871: nrerror("Singular matrix in routine ludcmp");
7872: }
7873: vv[i]=1.0/big;
7874: }
7875: for (j=1;j<=n;j++) {
7876: for (i=1;i<j;i++) {
7877: sum=a[i][j];
7878: for (k=1;k<i;k++) sum -= a[i][k]*a[k][j];
7879: a[i][j]=sum;
7880: }
7881: big=0.0;
7882: for (i=j;i<=n;i++) {
7883: sum=a[i][j];
7884: for (k=1;k<j;k++)
7885: sum -= a[i][k]*a[k][j];
7886: a[i][j]=sum;
7887: if ( (dum=vv[i]*fabs(sum)) >= big) {
7888: big=dum;
7889: imax=i;
7890: }
7891: }
7892: if (j != imax) {
7893: for (k=1;k<=n;k++) {
7894: dum=a[imax][k];
7895: a[imax][k]=a[j][k];
7896: a[j][k]=dum;
7897: }
7898: *d = -(*d);
7899: vv[imax]=vv[j];
7900: }
7901: indx[j]=imax;
7902: if (a[j][j] == 0.0) a[j][j]=TINY;
7903: if (j != n) {
7904: dum=1.0/(a[j][j]);
7905: for (i=j+1;i<=n;i++) a[i][j] *= dum;
7906: }
7907: }
7908: free_vector(vv,1,n); /* Doesn't work */
7909: ;
7910: }
7911:
7912: void lubksb(double **a, int n, int *indx, double b[])
7913: {
7914: int i,ii=0,ip,j;
7915: double sum;
7916:
7917: for (i=1;i<=n;i++) {
7918: ip=indx[i];
7919: sum=b[ip];
7920: b[ip]=b[i];
7921: if (ii)
7922: for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j];
7923: else if (sum) ii=i;
7924: b[i]=sum;
7925: }
7926: for (i=n;i>=1;i--) {
7927: sum=b[i];
7928: for (j=i+1;j<=n;j++) sum -= a[i][j]*b[j];
7929: b[i]=sum/a[i][i];
7930: }
7931: }
7932:
7933: void pstamp(FILE *fichier)
7934: {
7935: fprintf(fichier,"# %s.%s\n#IMaCh version %s, %s\n#%s\n# %s", optionfilefiname,optionfilext,version,copyright, fullversion, strstart);
7936: }
7937:
7938: void date2dmy(double date,double *day, double *month, double *year){
7939: double yp=0., yp1=0., yp2=0.;
7940:
7941: yp1=modf(date,&yp);/* extracts integral of date in yp and
7942: fractional in yp1 */
7943: *year=yp;
7944: yp2=modf((yp1*12),&yp);
7945: *month=yp;
7946: yp1=modf((yp2*30.5),&yp);
7947: *day=yp;
7948: if(*day==0) *day=1;
7949: if(*month==0) *month=1;
7950: }
7951:
7952:
7953:
7954: /************ Frequencies ********************/
7955: void freqsummary(char fileres[], double p[], double pstart[], int iagemin, int iagemax, int **s, double **agev, int nlstate, int imx, \
7956: int *Tvaraff, int *invalidvarcomb, int **nbcode, int *ncodemax,double **mint,double **anint, char strstart[], \
7957: int firstpass, int lastpass, int stepm, int weightopt, char model[])
7958: { /* Some frequencies as well as proposing some starting values */
7959: /* Frequencies of any combination of dummy covariate used in the model equation */
7960: int i, m, jk, j1, bool, z1,j, nj, nl, k, iv, jj=0, s1=1, s2=1;
7961: int iind=0, iage=0;
7962: int mi; /* Effective wave */
7963: int first;
7964: double ***freq; /* Frequencies */
7965: 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 */
7966: 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);
7967: double *meanq, *stdq, *idq;
7968: double **meanqt;
7969: double *pp, **prop, *posprop, *pospropt;
7970: double pos=0., posproptt=0., pospropta=0., k2, dateintsum=0,k2cpt=0;
7971: char fileresp[FILENAMELENGTH], fileresphtm[FILENAMELENGTH], fileresphtmfr[FILENAMELENGTH];
7972: double agebegin, ageend;
7973:
7974: pp=vector(1,nlstate);
7975: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
7976: posprop=vector(1,nlstate); /* Counting the number of transition starting from a live state per age */
7977: pospropt=vector(1,nlstate); /* Counting the number of transition starting from a live state */
7978: /* prop=matrix(1,nlstate,iagemin,iagemax+3); */
7979: meanq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
7980: stdq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
7981: idq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
7982: meanqt=matrix(1,lastpass,1,nqtveff);
7983: strcpy(fileresp,"P_");
7984: strcat(fileresp,fileresu);
7985: /*strcat(fileresphtm,fileresu);*/
7986: if((ficresp=fopen(fileresp,"w"))==NULL) {
7987: printf("Problem with prevalence resultfile: %s\n", fileresp);
7988: fprintf(ficlog,"Problem with prevalence resultfile: %s\n", fileresp);
7989: exit(0);
7990: }
7991:
7992: strcpy(fileresphtm,subdirfext(optionfilefiname,"PHTM_",".htm"));
7993: if((ficresphtm=fopen(fileresphtm,"w"))==NULL) {
7994: printf("Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
7995: fprintf(ficlog,"Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
7996: fflush(ficlog);
7997: exit(70);
7998: }
7999: else{
8000: fprintf(ficresphtm,"<html><head>\n<title>IMaCh PHTM_ %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
8001: <hr size=\"2\" color=\"#EC5E5E\"> \n \
8002: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
8003: fileresphtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
8004: }
8005: 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);
8006:
8007: strcpy(fileresphtmfr,subdirfext(optionfilefiname,"PHTMFR_",".htm"));
8008: if((ficresphtmfr=fopen(fileresphtmfr,"w"))==NULL) {
8009: printf("Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
8010: fprintf(ficlog,"Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
8011: fflush(ficlog);
8012: exit(70);
8013: } else{
8014: fprintf(ficresphtmfr,"<html><head>\n<title>IMaCh PHTM_Frequency table %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
8015: ,<hr size=\"2\" color=\"#EC5E5E\"> \n \
8016: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
8017: fileresphtmfr,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
8018: }
8019: 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);
8020:
8021: y= vector(iagemin-AGEMARGE,iagemax+4+AGEMARGE);
8022: x= vector(iagemin-AGEMARGE,iagemax+4+AGEMARGE);
8023: freq= ma3x(-5,nlstate+ndeath,-5,nlstate+ndeath,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
8024: j1=0;
8025:
8026: /* j=ncoveff; /\* Only fixed dummy covariates *\/ */
8027: j=cptcoveff; /* Only simple dummy covariates used in the model */
8028: /* j=cptcovn; /\* Only dummy covariates of the model *\/ */
8029: if (cptcovn<1) {j=1;ncodemax[1]=1;}
8030:
8031:
8032: /* Detects if a combination j1 is empty: for a multinomial variable like 3 education levels:
8033: reference=low_education V1=0,V2=0
8034: med_educ V1=1 V2=0,
8035: high_educ V1=0 V2=1
8036: Then V1=1 and V2=1 is a noisy combination that we want to exclude for the list 2**cptcovn
8037: */
8038: dateintsum=0;
8039: k2cpt=0;
8040:
8041: if(cptcoveff == 0 )
8042: nl=1; /* Constant and age model only */
8043: else
8044: nl=2;
8045:
8046: /* if a constant only model, one pass to compute frequency tables and to write it on ficresp */
8047: /* Loop on nj=1 or 2 if dummy covariates j!=0
8048: * Loop on j1(1 to 2**cptcoveff) covariate combination
8049: * freq[s1][s2][iage] =0.
8050: * Loop on iind
8051: * ++freq[s1][s2][iage] weighted
8052: * end iind
8053: * if covariate and j!0
8054: * headers Variable on one line
8055: * endif cov j!=0
8056: * header of frequency table by age
8057: * Loop on age
8058: * pp[s1]+=freq[s1][s2][iage] weighted
8059: * pos+=freq[s1][s2][iage] weighted
8060: * Loop on s1 initial state
8061: * fprintf(ficresp
8062: * end s1
8063: * end age
8064: * if j!=0 computes starting values
8065: * end compute starting values
8066: * end j1
8067: * end nl
8068: */
8069: for (nj = 1; nj <= nl; nj++){ /* nj= 1 constant model, nl number of loops. */
8070: if(nj==1)
8071: j=0; /* First pass for the constant */
8072: else{
8073: j=cptcoveff; /* Other passes for the covariate values number of simple covariates in the model V2+V1 =2 (simple dummy fixed or time varying) */
8074: }
8075: first=1;
8076: 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 */
8077: posproptt=0.;
8078: /*printf("cptcovn=%d Tvaraff=%d", cptcovn,Tvaraff[1]);
8079: scanf("%d", i);*/
8080: for (i=-5; i<=nlstate+ndeath; i++)
8081: for (s2=-5; s2<=nlstate+ndeath; s2++)
8082: for(m=iagemin; m <= iagemax+3; m++)
8083: freq[i][s2][m]=0;
8084:
8085: for (i=1; i<=nlstate; i++) {
8086: for(m=iagemin; m <= iagemax+3; m++)
8087: prop[i][m]=0;
8088: posprop[i]=0;
8089: pospropt[i]=0;
8090: }
8091: for (z1=1; z1<= nqfveff; z1++) { /* zeroing for each combination j1 as well as for the total */
8092: idq[z1]=0.;
8093: meanq[z1]=0.;
8094: stdq[z1]=0.;
8095: }
8096: /* for (z1=1; z1<= nqtveff; z1++) { */
8097: /* for(m=1;m<=lastpass;m++){ */
8098: /* meanqt[m][z1]=0.; */
8099: /* } */
8100: /* } */
8101: /* dateintsum=0; */
8102: /* k2cpt=0; */
8103:
8104: /* For that combination of covariates j1 (V4=1 V3=0 for example), we count and print the frequencies in one pass */
8105: for (iind=1; iind<=imx; iind++) { /* For each individual iind */
8106: bool=1;
8107: if(j !=0){
8108: if(anyvaryingduminmodel==0){ /* If All fixed covariates */
8109: if (cptcoveff >0) { /* Filter is here: Must be looked at for model=V1+V2+V3+V4 */
8110: for (z1=1; z1<=cptcoveff; z1++) { /* loops on covariates in the model */
8111: /* if(Tvaraff[z1] ==-20){ */
8112: /* /\* sumnew+=cotvar[mw[mi][iind]][z1][iind]; *\/ */
8113: /* }else if(Tvaraff[z1] ==-10){ */
8114: /* /\* sumnew+=coqvar[z1][iind]; *\/ */
8115: /* }else */ /* TODO TODO codtabm(j1,z1) or codtabm(j1,Tvaraff[z1]]z1)*/
8116: /* 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); */
8117: if(Tvaraff[z1]<1 || Tvaraff[z1]>=NCOVMAX)
8118: printf("Error Tvaraff[z1]=%d<1 or >=%d, cptcoveff=%d model=1+age+%s\n",Tvaraff[z1],NCOVMAX, cptcoveff, model);
8119: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]){ /* for combination j1 of covariates */
8120: /* Tests if the value of the covariate z1 for this individual iind responded to combination j1 (V4=1 V3=0) */
8121: bool=0; /* bool should be equal to 1 to be selected, one covariate value failed */
8122: /* 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", */
8123: /* bool,i,z1, z1, Tvaraff[z1],i,covar[Tvaraff[z1]][i],j1,z1,codtabm(j1,z1),*/
8124: /* j1,z1,nbcode[Tvaraff[z1]][codtabm(j1,z1)],j1);*/
8125: /* For j1=7 in V1+V2+V3+V4 = 0 1 1 0 and codtabm(7,3)=1 and nbcde[3][?]=1*/
8126: } /* Onlyf fixed */
8127: } /* end z1 */
8128: } /* cptcoveff > 0 */
8129: } /* end any */
8130: }/* end j==0 */
8131: if (bool==1){ /* We selected an individual iind satisfying combination j1 (V4=1 V3=0) or all fixed covariates */
8132: /* for(m=firstpass; m<=lastpass; m++){ */
8133: for(mi=1; mi<wav[iind];mi++){ /* For each wave */
8134: m=mw[mi][iind];
8135: if(j!=0){
8136: if(anyvaryingduminmodel==1){ /* Some are varying covariates */
8137: for (z1=1; z1<=cptcoveff; z1++) {
8138: if( Fixed[Tmodelind[z1]]==1){
8139: /* iv= Tvar[Tmodelind[z1]]-ncovcol-nqv; /\* Good *\/ */
8140: iv= Tvar[Tmodelind[z1]]; /* Good *//* because cotvar starts now at first at ncovcol+nqv+ntv */
8141: if (cotvar[m][iv][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]) /* iv=1 to ntv, right modality. If covariate's
8142: value is -1, we don't select. It differs from the
8143: constant and age model which counts them. */
8144: bool=0; /* not selected */
8145: }else if( Fixed[Tmodelind[z1]]== 0) { /* fixed */
8146: /* i1=Tvaraff[z1]; */
8147: /* i2=TnsdVar[i1]; */
8148: /* i3=nbcode[i1][i2]; */
8149: /* i4=covar[i1][iind]; */
8150: /* if(i4 != i3){ */
8151: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]) { /* Bug valgrind */
8152: bool=0;
8153: }
8154: }
8155: }
8156: }/* Some are varying covariates, we tried to speed up if all fixed covariates in the model, avoiding waves loop */
8157: } /* end j==0 */
8158: /* bool =0 we keep that guy which corresponds to the combination of dummy values */
8159: if(bool==1){ /*Selected */
8160: /* dh[m][iind] or dh[mw[mi][iind]][iind] is the delay between two effective (mi) waves m=mw[mi][iind]
8161: and mw[mi+1][iind]. dh depends on stepm. */
8162: agebegin=agev[m][iind]; /* Age at beginning of wave before transition*/
8163: ageend=agev[m][iind]+(dh[m][iind])*stepm/YEARM; /* Age at end of wave and transition */
8164: if(m >=firstpass && m <=lastpass){
8165: k2=anint[m][iind]+(mint[m][iind]/12.);
8166: /*if ((k2>=dateprev1) && (k2<=dateprev2)) {*/
8167: if(agev[m][iind]==0) agev[m][iind]=iagemax+1; /* All ages equal to 0 are in iagemax+1 */
8168: if(agev[m][iind]==1) agev[m][iind]=iagemax+2; /* All ages equal to 1 are in iagemax+2 */
8169: if (s[m][iind]>0 && s[m][iind]<=nlstate) /* If status at wave m is known and a live state */
8170: prop[s[m][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */
8171: if (m<lastpass) {
8172: /* if(s[m][iind]==4 && s[m+1][iind]==4) */
8173: /* 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]); */
8174: if(s[m][iind]==-1)
8175: 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.));
8176: freq[s[m][iind]][s[m+1][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */
8177: for (z1=1; z1<= nqfveff; z1++) { /* Quantitative variables, calculating mean on known values only */
8178: if(!isnan(covar[ncovcol+z1][iind])){
8179: idq[z1]=idq[z1]+weight[iind];
8180: meanq[z1]+=covar[ncovcol+z1][iind]*weight[iind]; /* Computes mean of quantitative with selected filter */
8181: /* stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]*weight[iind]; *//*error*/
8182: stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]; /* *weight[iind];*/ /* Computes mean of quantitative with selected filter */
8183: }
8184: }
8185: /* if((int)agev[m][iind] == 55) */
8186: /* printf("j=%d, j1=%d Age %d, iind=%d, num=%09ld m=%d\n",j,j1,(int)agev[m][iind],iind, num[iind],m); */
8187: /* freq[s[m][iind]][s[m+1][iind]][(int)((agebegin+ageend)/2.)] += weight[iind]; */
8188: 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 */
8189: }
8190: } /* end if between passes */
8191: if ((agev[m][iind]>1) && (agev[m][iind]< (iagemax+3)) && (anint[m][iind]!=9999) && (mint[m][iind]!=99) && (j==0)) {
8192: dateintsum=dateintsum+k2; /* on all covariates ?*/
8193: k2cpt++;
8194: /* printf("iind=%ld dateintmean = %lf dateintsum=%lf k2cpt=%lf k2=%lf\n",iind, dateintsum/k2cpt, dateintsum,k2cpt, k2); */
8195: }
8196: }else{
8197: bool=1;
8198: }/* end bool 2 */
8199: } /* end m */
8200: /* for (z1=1; z1<= nqfveff; z1++) { /\* Quantitative variables, calculating mean *\/ */
8201: /* idq[z1]=idq[z1]+weight[iind]; */
8202: /* meanq[z1]+=covar[ncovcol+z1][iind]*weight[iind]; /\* Computes mean of quantitative with selected filter *\/ */
8203: /* stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]*weight[iind]; /\* *weight[iind];*\/ /\* Computes mean of quantitative with selected filter *\/ */
8204: /* } */
8205: } /* end bool */
8206: } /* end iind = 1 to imx */
8207: /* prop[s][age] is fed for any initial and valid live state as well as
8208: freq[s1][s2][age] at single age of beginning the transition, for a combination j1 */
8209:
8210:
8211: /* fprintf(ficresp, "#Count between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);*/
8212: if(cptcoveff==0 && nj==1) /* no covariate and first pass */
8213: pstamp(ficresp);
8214: if (cptcoveff>0 && j!=0){
8215: pstamp(ficresp);
8216: printf( "\n#********** Variable ");
8217: fprintf(ficresp, "\n#********** Variable ");
8218: fprintf(ficresphtm, "\n<br/><br/><h3>********** Variable ");
8219: fprintf(ficresphtmfr, "\n<br/><br/><h3>********** Variable ");
8220: fprintf(ficlog, "\n#********** Variable ");
8221: for (z1=1; z1<=cptcoveff; z1++){
8222: if(!FixedV[Tvaraff[z1]]){
8223: printf( "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
8224: fprintf(ficresp, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
8225: fprintf(ficresphtm, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
8226: fprintf(ficresphtmfr, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
8227: fprintf(ficlog, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
8228: }else{
8229: printf( "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
8230: fprintf(ficresp, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
8231: fprintf(ficresphtm, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
8232: fprintf(ficresphtmfr, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
8233: fprintf(ficlog, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
8234: }
8235: }
8236: printf( "**********\n#");
8237: fprintf(ficresp, "**********\n#");
8238: fprintf(ficresphtm, "**********</h3>\n");
8239: fprintf(ficresphtmfr, "**********</h3>\n");
8240: fprintf(ficlog, "**********\n");
8241: }
8242: /*
8243: Printing means of quantitative variables if any
8244: */
8245: for (z1=1; z1<= nqfveff; z1++) {
8246: fprintf(ficlog,"Mean of fixed quantitative variable V%d on %.3g (weighted) individuals sum=%f", ncovcol+z1, idq[z1], meanq[z1]);
8247: fprintf(ficlog,", mean=%.3g\n",meanq[z1]/idq[z1]);
8248: if(weightopt==1){
8249: printf(" Weighted mean and standard deviation of");
8250: fprintf(ficlog," Weighted mean and standard deviation of");
8251: fprintf(ficresphtmfr," Weighted mean and standard deviation of");
8252: }
8253: /* mu = \frac{w x}{\sum w}
8254: var = \frac{\sum w (x-mu)^2}{\sum w} = \frac{w x^2}{\sum w} - mu^2
8255: */
8256: 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]));
8257: 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]));
8258: 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]));
8259: }
8260: /* for (z1=1; z1<= nqtveff; z1++) { */
8261: /* for(m=1;m<=lastpass;m++){ */
8262: /* fprintf(ficresphtmfr,"V quantitative id %d, pass id=%d, mean=%f<p>\n", z1, m, meanqt[m][z1]); */
8263: /* } */
8264: /* } */
8265:
8266: fprintf(ficresphtm,"<table style=\"text-align:center; border: 1px solid\">");
8267: if((cptcoveff==0 && nj==1)|| nj==2 ) /* no covariate and first pass */
8268: fprintf(ficresp, " Age");
8269: if(nj==2) for (z1=1; z1<=cptcoveff; z1++) {
8270: 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]]);
8271: fprintf(ficresp, " V%d=%d",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
8272: }
8273: for(i=1; i<=nlstate;i++) {
8274: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp," Prev(%d) N(%d) N ",i,i);
8275: fprintf(ficresphtm, "<th>Age</th><th>Prev(%d)</th><th>N(%d)</th><th>N</th>",i,i);
8276: }
8277: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp, "\n");
8278: fprintf(ficresphtm, "\n");
8279:
8280: /* Header of frequency table by age */
8281: fprintf(ficresphtmfr,"<table style=\"text-align:center; border: 1px solid\">");
8282: fprintf(ficresphtmfr,"<th>Age</th> ");
8283: for(s2=-1; s2 <=nlstate+ndeath; s2++){
8284: for(m=-1; m <=nlstate+ndeath; m++){
8285: if(s2!=0 && m!=0)
8286: fprintf(ficresphtmfr,"<th>%d%d</th> ",s2,m);
8287: }
8288: }
8289: fprintf(ficresphtmfr, "\n");
8290:
8291: /* For each age */
8292: for(iage=iagemin; iage <= iagemax+3; iage++){
8293: fprintf(ficresphtm,"<tr>");
8294: if(iage==iagemax+1){
8295: fprintf(ficlog,"1");
8296: fprintf(ficresphtmfr,"<tr><th>0</th> ");
8297: }else if(iage==iagemax+2){
8298: fprintf(ficlog,"0");
8299: fprintf(ficresphtmfr,"<tr><th>Unknown</th> ");
8300: }else if(iage==iagemax+3){
8301: fprintf(ficlog,"Total");
8302: fprintf(ficresphtmfr,"<tr><th>Total</th> ");
8303: }else{
8304: if(first==1){
8305: first=0;
8306: printf("See log file for details...\n");
8307: }
8308: fprintf(ficresphtmfr,"<tr><th>%d</th> ",iage);
8309: fprintf(ficlog,"Age %d", iage);
8310: }
8311: for(s1=1; s1 <=nlstate ; s1++){
8312: for(m=-1, pp[s1]=0; m <=nlstate+ndeath ; m++)
8313: pp[s1] += freq[s1][m][iage];
8314: }
8315: for(s1=1; s1 <=nlstate ; s1++){
8316: for(m=-1, pos=0; m <=0 ; m++)
8317: pos += freq[s1][m][iage];
8318: if(pp[s1]>=1.e-10){
8319: if(first==1){
8320: printf(" %d.=%.0f loss[%d]=%.1f%%",s1,pp[s1],s1,100*pos/pp[s1]);
8321: }
8322: fprintf(ficlog," %d.=%.0f loss[%d]=%.1f%%",s1,pp[s1],s1,100*pos/pp[s1]);
8323: }else{
8324: if(first==1)
8325: printf(" %d.=%.0f loss[%d]=NaNQ%%",s1,pp[s1],s1);
8326: fprintf(ficlog," %d.=%.0f loss[%d]=NaNQ%%",s1,pp[s1],s1);
8327: }
8328: }
8329:
8330: for(s1=1; s1 <=nlstate ; s1++){
8331: /* posprop[s1]=0; */
8332: for(m=0, pp[s1]=0; m <=nlstate+ndeath; m++)/* Summing on all ages */
8333: pp[s1] += freq[s1][m][iage];
8334: } /* pp[s1] is the total number of transitions starting from state s1 and any ending status until this age */
8335:
8336: for(s1=1,pos=0, pospropta=0.; s1 <=nlstate ; s1++){
8337: pos += pp[s1]; /* pos is the total number of transitions until this age */
8338: posprop[s1] += prop[s1][iage]; /* prop is the number of transitions from a live state
8339: from s1 at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
8340: pospropta += prop[s1][iage]; /* prop is the number of transitions from a live state
8341: from s1 at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
8342: }
8343:
8344: /* Writing ficresp */
8345: if(cptcoveff==0 && nj==1){ /* no covariate and first pass */
8346: if( iage <= iagemax){
8347: fprintf(ficresp," %d",iage);
8348: }
8349: }else if( nj==2){
8350: if( iage <= iagemax){
8351: fprintf(ficresp," %d",iage);
8352: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresp, " %d %d",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
8353: }
8354: }
8355: for(s1=1; s1 <=nlstate ; s1++){
8356: if(pos>=1.e-5){
8357: if(first==1)
8358: printf(" %d.=%.0f prev[%d]=%.1f%%",s1,pp[s1],s1,100*pp[s1]/pos);
8359: fprintf(ficlog," %d.=%.0f prev[%d]=%.1f%%",s1,pp[s1],s1,100*pp[s1]/pos);
8360: }else{
8361: if(first==1)
8362: printf(" %d.=%.0f prev[%d]=NaNQ%%",s1,pp[s1],s1);
8363: fprintf(ficlog," %d.=%.0f prev[%d]=NaNQ%%",s1,pp[s1],s1);
8364: }
8365: if( iage <= iagemax){
8366: if(pos>=1.e-5){
8367: if(cptcoveff==0 && nj==1){ /* no covariate and first pass */
8368: fprintf(ficresp," %.5f %.0f %.0f",prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
8369: }else if( nj==2){
8370: fprintf(ficresp," %.5f %.0f %.0f",prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
8371: }
8372: fprintf(ficresphtm,"<th>%d</th><td>%.5f</td><td>%.0f</td><td>%.0f</td>",iage,prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
8373: /*probs[iage][s1][j1]= pp[s1]/pos;*/
8374: /*printf("\niage=%d s1=%d j1=%d %.5f %.0f %.0f %f",iage,s1,j1,pp[s1]/pos, pp[s1],pos,probs[iage][s1][j1]);*/
8375: } else{
8376: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp," NaNq %.0f %.0f",prop[s1][iage],pospropta);
8377: fprintf(ficresphtm,"<th>%d</th><td>NaNq</td><td>%.0f</td><td>%.0f</td>",iage, prop[s1][iage],pospropta);
8378: }
8379: }
8380: pospropt[s1] +=posprop[s1];
8381: } /* end loop s1 */
8382: /* pospropt=0.; */
8383: for(s1=-1; s1 <=nlstate+ndeath; s1++){
8384: for(m=-1; m <=nlstate+ndeath; m++){
8385: if(freq[s1][m][iage] !=0 ) { /* minimizing output */
8386: if(first==1){
8387: printf(" %d%d=%.0f",s1,m,freq[s1][m][iage]);
8388: }
8389: /* printf(" %d%d=%.0f",s1,m,freq[s1][m][iage]); */
8390: fprintf(ficlog," %d%d=%.0f",s1,m,freq[s1][m][iage]);
8391: }
8392: if(s1!=0 && m!=0)
8393: fprintf(ficresphtmfr,"<td>%.0f</td> ",freq[s1][m][iage]);
8394: }
8395: } /* end loop s1 */
8396: posproptt=0.;
8397: for(s1=1; s1 <=nlstate; s1++){
8398: posproptt += pospropt[s1];
8399: }
8400: fprintf(ficresphtmfr,"</tr>\n ");
8401: fprintf(ficresphtm,"</tr>\n");
8402: if((cptcoveff==0 && nj==1)|| nj==2 ) {
8403: if(iage <= iagemax)
8404: fprintf(ficresp,"\n");
8405: }
8406: if(first==1)
8407: printf("Others in log...\n");
8408: fprintf(ficlog,"\n");
8409: } /* end loop age iage */
8410:
8411: fprintf(ficresphtm,"<tr><th>Tot</th>");
8412: for(s1=1; s1 <=nlstate ; s1++){
8413: if(posproptt < 1.e-5){
8414: fprintf(ficresphtm,"<td>Nanq</td><td>%.0f</td><td>%.0f</td>",pospropt[s1],posproptt);
8415: }else{
8416: fprintf(ficresphtm,"<td>%.5f</td><td>%.0f</td><td>%.0f</td>",pospropt[s1]/posproptt,pospropt[s1],posproptt);
8417: }
8418: }
8419: fprintf(ficresphtm,"</tr>\n");
8420: fprintf(ficresphtm,"</table>\n");
8421: fprintf(ficresphtmfr,"</table>\n");
8422: if(posproptt < 1.e-5){
8423: fprintf(ficresphtm,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
8424: fprintf(ficresphtmfr,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
8425: fprintf(ficlog,"# This combination (%d) is not valid and no result will be produced\n",j1);
8426: printf("# This combination (%d) is not valid and no result will be produced\n",j1);
8427: invalidvarcomb[j1]=1;
8428: }else{
8429: fprintf(ficresphtm,"\n <p> This combination (%d) is valid and result will be produced (or no resultline).</p>",j1);
8430: invalidvarcomb[j1]=0;
8431: }
8432: fprintf(ficresphtmfr,"</table>\n");
8433: fprintf(ficlog,"\n");
8434: if(j!=0){
8435: printf("#Freqsummary: Starting values for combination j1=%d:\n", j1);
8436: for(i=1,s1=1; i <=nlstate; i++){
8437: for(k=1; k <=(nlstate+ndeath); k++){
8438: if (k != i) {
8439: for(jj=1; jj <=ncovmodel; jj++){ /* For counting s1 */
8440: if(jj==1){ /* Constant case (in fact cste + age) */
8441: if(j1==1){ /* All dummy covariates to zero */
8442: freq[i][k][iagemax+4]=freq[i][k][iagemax+3]; /* Stores case 0 0 0 */
8443: freq[i][i][iagemax+4]=freq[i][i][iagemax+3]; /* Stores case 0 0 0 */
8444: printf("%d%d ",i,k);
8445: fprintf(ficlog,"%d%d ",i,k);
8446: 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]));
8447: 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]));
8448: pstart[s1]= log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]);
8449: }
8450: }else if((j1==1) && (jj==2 || nagesqr==1)){ /* age or age*age parameter without covariate V4*age (to be done later) */
8451: for(iage=iagemin; iage <= iagemax+3; iage++){
8452: x[iage]= (double)iage;
8453: y[iage]= log(freq[i][k][iage]/freq[i][i][iage]);
8454: /* printf("i=%d, k=%d, s1=%d, j1=%d, jj=%d, y[%d]=%f\n",i,k,s1,j1,jj, iage, y[iage]); */
8455: }
8456: /* Some are not finite, but linreg will ignore these ages */
8457: no=0;
8458: linreg(iagemin,iagemax,&no,x,y,&a,&b,&r, &sa, &sb ); /* y= a+b*x with standard errors */
8459: pstart[s1]=b;
8460: pstart[s1-1]=a;
8461: }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 */
8462: 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]);
8463: 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]);
8464: pstart[s1]= log((freq[i][k][iagemax+3]/freq[i][i][iagemax+3])/(freq[i][k][iagemax+4]/freq[i][i][iagemax+4]));
8465: printf("%d%d ",i,k);
8466: fprintf(ficlog,"%d%d ",i,k);
8467: 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]));
8468: }else{ /* Other cases, like quantitative fixed or varying covariates */
8469: ;
8470: }
8471: /* printf("%12.7f )", param[i][jj][k]); */
8472: /* fprintf(ficlog,"%12.7f )", param[i][jj][k]); */
8473: s1++;
8474: } /* end jj */
8475: } /* end k!= i */
8476: } /* end k */
8477: } /* end i, s1 */
8478: } /* end j !=0 */
8479: } /* end selected combination of covariate j1 */
8480: if(j==0){ /* We can estimate starting values from the occurences in each case */
8481: printf("#Freqsummary: Starting values for the constants:\n");
8482: fprintf(ficlog,"\n");
8483: for(i=1,s1=1; i <=nlstate; i++){
8484: for(k=1; k <=(nlstate+ndeath); k++){
8485: if (k != i) {
8486: printf("%d%d ",i,k);
8487: fprintf(ficlog,"%d%d ",i,k);
8488: for(jj=1; jj <=ncovmodel; jj++){
8489: pstart[s1]=p[s1]; /* Setting pstart to p values by default */
8490: if(jj==1){ /* Age has to be done */
8491: pstart[s1]= log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]);
8492: 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]));
8493: 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]));
8494: }
8495: /* printf("%12.7f )", param[i][jj][k]); */
8496: /* fprintf(ficlog,"%12.7f )", param[i][jj][k]); */
8497: s1++;
8498: }
8499: printf("\n");
8500: fprintf(ficlog,"\n");
8501: }
8502: }
8503: } /* end of state i */
8504: printf("#Freqsummary\n");
8505: fprintf(ficlog,"\n");
8506: for(s1=-1; s1 <=nlstate+ndeath; s1++){
8507: for(s2=-1; s2 <=nlstate+ndeath; s2++){
8508: /* param[i]|j][k]= freq[s1][s2][iagemax+3] */
8509: printf(" %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]);
8510: fprintf(ficlog," %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]);
8511: /* if(freq[s1][s2][iage] !=0 ) { /\* minimizing output *\/ */
8512: /* printf(" %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]); */
8513: /* fprintf(ficlog," %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]); */
8514: /* } */
8515: }
8516: } /* end loop s1 */
8517:
8518: printf("\n");
8519: fprintf(ficlog,"\n");
8520: } /* end j=0 */
8521: } /* end j */
8522:
8523: if(mle == -2){ /* We want to use these values as starting values */
8524: for(i=1, jk=1; i <=nlstate; i++){
8525: for(j=1; j <=nlstate+ndeath; j++){
8526: if(j!=i){
8527: /*ca[0]= k+'a'-1;ca[1]='\0';*/
8528: printf("%1d%1d",i,j);
8529: fprintf(ficparo,"%1d%1d",i,j);
8530: for(k=1; k<=ncovmodel;k++){
8531: /* printf(" %lf",param[i][j][k]); */
8532: /* fprintf(ficparo," %lf",param[i][j][k]); */
8533: p[jk]=pstart[jk];
8534: printf(" %f ",pstart[jk]);
8535: fprintf(ficparo," %f ",pstart[jk]);
8536: jk++;
8537: }
8538: printf("\n");
8539: fprintf(ficparo,"\n");
8540: }
8541: }
8542: }
8543: } /* end mle=-2 */
8544: dateintmean=dateintsum/k2cpt;
8545: date2dmy(dateintmean,&jintmean,&mintmean,&aintmean);
8546:
8547: fclose(ficresp);
8548: fclose(ficresphtm);
8549: fclose(ficresphtmfr);
8550: free_vector(idq,1,nqfveff);
8551: free_vector(meanq,1,nqfveff);
8552: free_vector(stdq,1,nqfveff);
8553: free_matrix(meanqt,1,lastpass,1,nqtveff);
8554: free_vector(x, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
8555: free_vector(y, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
8556: free_ma3x(freq,-5,nlstate+ndeath,-5,nlstate+ndeath, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
8557: free_vector(pospropt,1,nlstate);
8558: free_vector(posprop,1,nlstate);
8559: free_matrix(prop,1,nlstate,iagemin-AGEMARGE, iagemax+4+AGEMARGE);
8560: free_vector(pp,1,nlstate);
8561: /* End of freqsummary */
8562: }
8563:
8564: /* Simple linear regression */
8565: int linreg(int ifi, int ila, int *no, const double x[], const double y[], double* a, double* b, double* r, double* sa, double * sb) {
8566:
8567: /* y=a+bx regression */
8568: double sumx = 0.0; /* sum of x */
8569: double sumx2 = 0.0; /* sum of x**2 */
8570: double sumxy = 0.0; /* sum of x * y */
8571: double sumy = 0.0; /* sum of y */
8572: double sumy2 = 0.0; /* sum of y**2 */
8573: double sume2 = 0.0; /* sum of square or residuals */
8574: double yhat;
8575:
8576: double denom=0;
8577: int i;
8578: int ne=*no;
8579:
8580: for ( i=ifi, ne=0;i<=ila;i++) {
8581: if(!isfinite(x[i]) || !isfinite(y[i])){
8582: /* printf(" x[%d]=%f, y[%d]=%f\n",i,x[i],i,y[i]); */
8583: continue;
8584: }
8585: ne=ne+1;
8586: sumx += x[i];
8587: sumx2 += x[i]*x[i];
8588: sumxy += x[i] * y[i];
8589: sumy += y[i];
8590: sumy2 += y[i]*y[i];
8591: denom = (ne * sumx2 - sumx*sumx);
8592: /* 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); */
8593: }
8594:
8595: denom = (ne * sumx2 - sumx*sumx);
8596: if (denom == 0) {
8597: // vertical, slope m is infinity
8598: *b = INFINITY;
8599: *a = 0;
8600: if (r) *r = 0;
8601: return 1;
8602: }
8603:
8604: *b = (ne * sumxy - sumx * sumy) / denom;
8605: *a = (sumy * sumx2 - sumx * sumxy) / denom;
8606: if (r!=NULL) {
8607: *r = (sumxy - sumx * sumy / ne) / /* compute correlation coeff */
8608: sqrt((sumx2 - sumx*sumx/ne) *
8609: (sumy2 - sumy*sumy/ne));
8610: }
8611: *no=ne;
8612: for ( i=ifi, ne=0;i<=ila;i++) {
8613: if(!isfinite(x[i]) || !isfinite(y[i])){
8614: /* printf(" x[%d]=%f, y[%d]=%f\n",i,x[i],i,y[i]); */
8615: continue;
8616: }
8617: ne=ne+1;
8618: yhat = y[i] - *a -*b* x[i];
8619: sume2 += yhat * yhat ;
8620:
8621: denom = (ne * sumx2 - sumx*sumx);
8622: /* 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); */
8623: }
8624: *sb = sqrt(sume2/(double)(ne-2)/(sumx2 - sumx * sumx /(double)ne));
8625: *sa= *sb * sqrt(sumx2/ne);
8626:
8627: return 0;
8628: }
8629:
8630: /************ Prevalence ********************/
8631: 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)
8632: {
8633: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
8634: in each health status at the date of interview (if between dateprev1 and dateprev2).
8635: We still use firstpass and lastpass as another selection.
8636: */
8637:
8638: int i, m, jk, j1, bool, z1,j, iv;
8639: int mi; /* Effective wave */
8640: int iage;
8641: double agebegin, ageend;
8642:
8643: double **prop;
8644: double posprop;
8645: double y2; /* in fractional years */
8646: int iagemin, iagemax;
8647: int first; /** to stop verbosity which is redirected to log file */
8648:
8649: iagemin= (int) agemin;
8650: iagemax= (int) agemax;
8651: /*pp=vector(1,nlstate);*/
8652: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
8653: /* freq=ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,iagemin,iagemax+3);*/
8654: j1=0;
8655:
8656: /*j=cptcoveff;*/
8657: if (cptcovn<1) {j=1;ncodemax[1]=1;}
8658:
8659: first=0;
8660: for(j1=1; j1<= (int) pow(2,cptcoveff);j1++){ /* For each combination of simple dummy covariates */
8661: for (i=1; i<=nlstate; i++)
8662: for(iage=iagemin-AGEMARGE; iage <= iagemax+4+AGEMARGE; iage++)
8663: prop[i][iage]=0.0;
8664: printf("Prevalence combination of varying and fixed dummies %d\n",j1);
8665: /* fprintf(ficlog," V%d=%d ",Tvaraff[j1],nbcode[Tvaraff[j1]][codtabm(k,j1)]); */
8666: fprintf(ficlog,"Prevalence combination of varying and fixed dummies %d\n",j1);
8667:
8668: for (i=1; i<=imx; i++) { /* Each individual */
8669: bool=1;
8670: /* for(m=firstpass; m<=lastpass; m++){/\* Other selection (we can limit to certain interviews*\/ */
8671: for(mi=1; mi<wav[i];mi++){ /* For this wave too look where individual can be counted V4=0 V3=0 */
8672: m=mw[mi][i];
8673: /* Tmodelind[z1]=k is the position of the varying covariate in the model, but which # within 1 to ntv? */
8674: /* Tvar[Tmodelind[z1]] is the n of Vn; n-ncovcol-nqv is the first time varying covariate or iv */
8675: for (z1=1; z1<=cptcoveff; z1++){
8676: if( Fixed[Tmodelind[z1]]==1){
8677: iv= Tvar[Tmodelind[z1]];/* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) */
8678: if (cotvar[m][iv][i]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]) /* iv=1 to ntv, right modality */
8679: bool=0;
8680: }else if( Fixed[Tmodelind[z1]]== 0) /* fixed */
8681: if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]) {
8682: bool=0;
8683: }
8684: }
8685: if(bool==1){ /* Otherwise we skip that wave/person */
8686: agebegin=agev[m][i]; /* Age at beginning of wave before transition*/
8687: /* ageend=agev[m][i]+(dh[m][i])*stepm/YEARM; /\* Age at end of wave and transition *\/ */
8688: if(m >=firstpass && m <=lastpass){
8689: y2=anint[m][i]+(mint[m][i]/12.); /* Fractional date in year */
8690: if ((y2>=dateprev1) && (y2<=dateprev2)) { /* Here is the main selection (fractional years) */
8691: if(agev[m][i]==0) agev[m][i]=iagemax+1;
8692: if(agev[m][i]==1) agev[m][i]=iagemax+2;
8693: if((int)agev[m][i] <iagemin-AGEMARGE || (int)agev[m][i] >iagemax+4+AGEMARGE){
8694: 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);
8695: exit(1);
8696: }
8697: if (s[m][i]>0 && s[m][i]<=nlstate) {
8698: /*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]]);*/
8699: prop[s[m][i]][(int)agev[m][i]] += weight[i];/* At age of beginning of transition, where status is known */
8700: prop[s[m][i]][iagemax+3] += weight[i];
8701: } /* end valid statuses */
8702: } /* end selection of dates */
8703: } /* end selection of waves */
8704: } /* end bool */
8705: } /* end wave */
8706: } /* end individual */
8707: for(i=iagemin; i <= iagemax+3; i++){
8708: for(jk=1,posprop=0; jk <=nlstate ; jk++) {
8709: posprop += prop[jk][i];
8710: }
8711:
8712: for(jk=1; jk <=nlstate ; jk++){
8713: if( i <= iagemax){
8714: if(posprop>=1.e-5){
8715: probs[i][jk][j1]= prop[jk][i]/posprop;
8716: } else{
8717: if(!first){
8718: first=1;
8719: 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]);
8720: }else{
8721: 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]);
8722: }
8723: }
8724: }
8725: }/* end jk */
8726: }/* end i */
8727: /*} *//* end i1 */
8728: } /* end j1 */
8729:
8730: /* free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath, iagemin, iagemax+3);*/
8731: /*free_vector(pp,1,nlstate);*/
8732: free_matrix(prop,1,nlstate, iagemin-AGEMARGE,iagemax+4+AGEMARGE);
8733: } /* End of prevalence */
8734:
8735: /************* Waves Concatenation ***************/
8736:
8737: 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)
8738: {
8739: /* Concatenates waves: wav[i] is the number of effective (useful waves in the sense that a non interview is useless) of individual i.
8740: Death is a valid wave (if date is known).
8741: mw[mi][i] is the mi (mi=1 to wav[i]) effective wave of individual i
8742: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
8743: and mw[mi+1][i]. dh depends on stepm. s[m][i] exists for any wave from firstpass to lastpass
8744: */
8745:
8746: int i=0, mi=0, m=0, mli=0;
8747: /* int j, k=0,jk, ju, jl,jmin=1e+5, jmax=-1;
8748: double sum=0., jmean=0.;*/
8749: int first=0, firstwo=0, firsthree=0, firstfour=0, firstfiv=0;
8750: int j, k=0,jk, ju, jl;
8751: double sum=0.;
8752: first=0;
8753: firstwo=0;
8754: firsthree=0;
8755: firstfour=0;
8756: jmin=100000;
8757: jmax=-1;
8758: jmean=0.;
8759:
8760: /* Treating live states */
8761: for(i=1; i<=imx; i++){ /* For simple cases and if state is death */
8762: mi=0; /* First valid wave */
8763: mli=0; /* Last valid wave */
8764: m=firstpass; /* Loop on waves */
8765: while(s[m][i] <= nlstate){ /* a live state or unknown state */
8766: 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 */
8767: mli=m-1;/* mw[++mi][i]=m-1; */
8768: }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 */
8769: mw[++mi][i]=m; /* Valid wave: incrementing mi and updating mi; mw[mi] is the wave number of mi_th valid transition */
8770: mli=m;
8771: } /* else might be a useless wave -1 and mi is not incremented and mw[mi] not updated */
8772: if(m < lastpass){ /* m < lastpass, standard case */
8773: m++; /* mi gives the "effective" current wave, m the current wave, go to next wave by incrementing m */
8774: }
8775: else{ /* m = lastpass, eventual special issue with warning */
8776: #ifdef UNKNOWNSTATUSNOTCONTRIBUTING
8777: break;
8778: #else
8779: 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 */
8780: if(firsthree == 0){
8781: 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);
8782: firsthree=1;
8783: }else if(firsthree >=1 && firsthree < 10){
8784: 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);
8785: firsthree++;
8786: }else if(firsthree == 10){
8787: printf("Information, too many Information flags: no more reported to log either\n");
8788: fprintf(ficlog,"Information, too many Information flags: no more reported to log either\n");
8789: firsthree++;
8790: }else{
8791: firsthree++;
8792: }
8793: mw[++mi][i]=m; /* Valid transition with unknown status */
8794: mli=m;
8795: }
8796: if(s[m][i]==-2){ /* Vital status is really unknown */
8797: nbwarn++;
8798: if((int)anint[m][i] == 9999){ /* Has the vital status really been verified?not a transition */
8799: 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);
8800: 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);
8801: }
8802: break;
8803: }
8804: break;
8805: #endif
8806: }/* End m >= lastpass */
8807: }/* end while */
8808:
8809: /* mi is the last effective wave, m is lastpass, mw[j][i] gives the # of j-th effective wave for individual i */
8810: /* After last pass */
8811: /* Treating death states */
8812: if (s[m][i] > nlstate){ /* In a death state */
8813: /* if( mint[m][i]==mdc[m][i] && anint[m][i]==andc[m][i]){ /\* same date of death and date of interview *\/ */
8814: /* } */
8815: mi++; /* Death is another wave */
8816: /* if(mi==0) never been interviewed correctly before death */
8817: /* Only death is a correct wave */
8818: mw[mi][i]=m;
8819: } /* else not in a death state */
8820: #ifndef DISPATCHINGKNOWNDEATHAFTERLASTWAVE
8821: else if ((int) andc[i] != 9999) { /* Date of death is known */
8822: if ((int)anint[m][i]!= 9999) { /* date of last interview is known */
8823: 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 */
8824: nbwarn++;
8825: if(firstfiv==0){
8826: 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 );
8827: firstfiv=1;
8828: }else{
8829: 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 );
8830: }
8831: s[m][i]=nlstate+1; /* Fixing the status as death. Be careful if multiple death states */
8832: }else{ /* Month of Death occured afer last wave month, potential bias */
8833: nberr++;
8834: if(firstwo==0){
8835: 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 );
8836: firstwo=1;
8837: }
8838: 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 );
8839: }
8840: }else{ /* if date of interview is unknown */
8841: /* death is known but not confirmed by death status at any wave */
8842: if(firstfour==0){
8843: 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 );
8844: firstfour=1;
8845: }
8846: 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 );
8847: }
8848: } /* end if date of death is known */
8849: #endif
8850: wav[i]=mi; /* mi should be the last effective wave (or mli), */
8851: /* wav[i]=mw[mi][i]; */
8852: if(mi==0){
8853: nbwarn++;
8854: if(first==0){
8855: printf("Warning! No valid information for individual %ld line=%d (skipped) and may be others, see log file\n",num[i],i);
8856: first=1;
8857: }
8858: if(first==1){
8859: fprintf(ficlog,"Warning! No valid information for individual %ld line=%d (skipped)\n",num[i],i);
8860: }
8861: } /* end mi==0 */
8862: } /* End individuals */
8863: /* wav and mw are no more changed */
8864:
8865: printf("Information, you have to check %d informations which haven't been logged!\n",firsthree);
8866: fprintf(ficlog,"Information, you have to check %d informations which haven't been logged!\n",firsthree);
8867:
8868:
8869: for(i=1; i<=imx; i++){
8870: for(mi=1; mi<wav[i];mi++){
8871: if (stepm <=0)
8872: dh[mi][i]=1;
8873: else{
8874: if (s[mw[mi+1][i]][i] > nlstate) { /* A death, but what if date is unknown? */
8875: if (agedc[i] < 2*AGESUP) {
8876: j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12);
8877: if(j==0) j=1; /* Survives at least one month after exam */
8878: else if(j<0){
8879: nberr++;
8880: 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]);
8881: j=1; /* Temporary Dangerous patch */
8882: 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);
8883: 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]);
8884: 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);
8885: }
8886: k=k+1;
8887: if (j >= jmax){
8888: jmax=j;
8889: ijmax=i;
8890: }
8891: if (j <= jmin){
8892: jmin=j;
8893: ijmin=i;
8894: }
8895: sum=sum+j;
8896: /*if (j<0) printf("j=%d num=%d \n",j,i);*/
8897: /* printf("%d %d %d %d\n", s[mw[mi][i]][i] ,s[mw[mi+1][i]][i],j,i);*/
8898: }
8899: }
8900: else{
8901: j= rint( (agev[mw[mi+1][i]][i]*12 - agev[mw[mi][i]][i]*12));
8902: /* 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]); */
8903:
8904: k=k+1;
8905: if (j >= jmax) {
8906: jmax=j;
8907: ijmax=i;
8908: }
8909: else if (j <= jmin){
8910: jmin=j;
8911: ijmin=i;
8912: }
8913: /* if (j<10) printf("j=%d jmin=%d num=%d ",j,jmin,i); */
8914: /*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]);*/
8915: if(j<0){
8916: nberr++;
8917: 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]);
8918: 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]);
8919: }
8920: sum=sum+j;
8921: }
8922: jk= j/stepm;
8923: jl= j -jk*stepm;
8924: ju= j -(jk+1)*stepm;
8925: if(mle <=1){ /* only if we use a the linear-interpoloation pseudo-likelihood */
8926: if(jl==0){
8927: dh[mi][i]=jk;
8928: bh[mi][i]=0;
8929: }else{ /* We want a negative bias in order to only have interpolation ie
8930: * to avoid the price of an extra matrix product in likelihood */
8931: dh[mi][i]=jk+1;
8932: bh[mi][i]=ju;
8933: }
8934: }else{
8935: if(jl <= -ju){
8936: dh[mi][i]=jk;
8937: bh[mi][i]=jl; /* bias is positive if real duration
8938: * is higher than the multiple of stepm and negative otherwise.
8939: */
8940: }
8941: else{
8942: dh[mi][i]=jk+1;
8943: bh[mi][i]=ju;
8944: }
8945: if(dh[mi][i]==0){
8946: dh[mi][i]=1; /* At least one step */
8947: bh[mi][i]=ju; /* At least one step */
8948: /* 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);*/
8949: }
8950: } /* end if mle */
8951: }
8952: } /* end wave */
8953: }
8954: jmean=sum/k;
8955: 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);
8956: 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);
8957: }
8958:
8959: /*********** Tricode ****************************/
8960: void tricode(int *cptcov, int *Tvar, int **nbcode, int imx, int *Ndum)
8961: {
8962: /**< Uses cptcovn+2*cptcovprod as the number of covariates */
8963: /* Tvar[i]=atoi(stre); find 'n' in Vn and stores in Tvar. If model=V2+V1 Tvar[1]=2 and Tvar[2]=1
8964: * Boring subroutine which should only output nbcode[Tvar[j]][k]
8965: * Tvar[5] in V2+V1+V3*age+V2*V4 is 4 (V4) even it is a time varying or quantitative variable
8966: * nbcode[Tvar[5]][1]= nbcode[4][1]=0, nbcode[4][2]=1 (usually);
8967: */
8968:
8969: int ij=1, k=0, j=0, i=0, maxncov=NCOVMAX;
8970: int modmaxcovj=0; /* Modality max of covariates j */
8971: int cptcode=0; /* Modality max of covariates j */
8972: int modmincovj=0; /* Modality min of covariates j */
8973:
8974:
8975: /* cptcoveff=0; */
8976: /* *cptcov=0; */
8977:
8978: for (k=1; k <= maxncov; k++) ncodemax[k]=0; /* Horrible constant again replaced by NCOVMAX */
8979: for (k=1; k <= maxncov; k++)
8980: for(j=1; j<=2; j++)
8981: nbcode[k][j]=0; /* Valgrind */
8982:
8983: /* Loop on covariates without age and products and no quantitative variable */
8984: 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 */
8985: for (j=-1; (j < maxncov); j++) Ndum[j]=0;
8986: /* printf("Testing k=%d, cptcovt=%d\n",k, cptcovt); */
8987: if(Dummy[k]==0 && Typevar[k] !=1 && Typevar[k] != 3 && Typevar[k] != 2){ /* Dummy covariate and not age product nor fixed product */
8988: switch(Fixed[k]) {
8989: case 0: /* Testing on fixed dummy covariate, simple or product of fixed */
8990: modmaxcovj=0;
8991: modmincovj=0;
8992: 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*/
8993: /* 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])); */
8994: ij=(int)(covar[Tvar[k]][i]);
8995: /* ij=0 or 1 or -1. Value of the covariate Tvar[j] for individual i
8996: * If product of Vn*Vm, still boolean *:
8997: * If it was coded 1, 2, 3, 4 should be splitted into 3 boolean variables
8998: * 1 => 0 0 0, 2 => 0 0 1, 3 => 0 1 1, 4=1 0 0 */
8999: /* Finds for covariate j, n=Tvar[j] of Vn . ij is the
9000: modality of the nth covariate of individual i. */
9001: if (ij > modmaxcovj)
9002: modmaxcovj=ij;
9003: else if (ij < modmincovj)
9004: modmincovj=ij;
9005: if (ij <0 || ij >1 ){
9006: printf("ERROR, IMaCh doesn't treat covariate with missing values V%d=-1, individual %d will be skipped.\n",Tvar[k],i);
9007: fprintf(ficlog,"ERROR, currently IMaCh doesn't treat covariate with missing values V%d=-1, individual %d will be skipped.\n",Tvar[k],i);
9008: fflush(ficlog);
9009: exit(1);
9010: }
9011: if ((ij < -1) || (ij > NCOVMAX)){
9012: printf( "Error: minimal is less than -1 or maximal is bigger than %d. Exiting. \n", NCOVMAX );
9013: exit(1);
9014: }else
9015: Ndum[ij]++; /*counts and stores the occurence of this modality 0, 1, -1*/
9016: /* If coded 1, 2, 3 , counts the number of 1 Ndum[1], number of 2, Ndum[2], etc */
9017: /*printf("i=%d ij=%d Ndum[ij]=%d imx=%d",i,ij,Ndum[ij],imx);*/
9018: /* getting the maximum value of the modality of the covariate
9019: (should be 0 or 1 now) Tvar[j]. If V=sex and male is coded 0 and
9020: female ies 1, then modmaxcovj=1.
9021: */
9022: } /* end for loop on individuals i */
9023: printf(" Minimal and maximal values of %d th (fixed) covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj);
9024: fprintf(ficlog," Minimal and maximal values of %d th (fixed) covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj);
9025: cptcode=modmaxcovj;
9026: /* Ndum[0] = frequency of 0 for model-covariate j, Ndum[1] frequency of 1 etc. */
9027: /*for (i=0; i<=cptcode; i++) {*/
9028: for (j=modmincovj; j<=modmaxcovj; j++) { /* j=-1 ? 0 and 1*//* For each value j of the modality of model-cov k */
9029: printf("Frequencies of (fixed) covariate %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]);
9030: fprintf(ficlog, "Frequencies of (fixed) covariate %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]);
9031: if( Ndum[j] != 0 ){ /* Counts if nobody answered modality j ie empty modality, we skip it and reorder */
9032: if( j != -1){
9033: ncodemax[k]++; /* ncodemax[k]= Number of modalities of the k th
9034: covariate for which somebody answered excluding
9035: undefined. Usually 2: 0 and 1. */
9036: }
9037: ncodemaxwundef[k]++; /* ncodemax[j]= Number of modalities of the k th
9038: covariate for which somebody answered including
9039: undefined. Usually 3: -1, 0 and 1. */
9040: } /* In fact ncodemax[k]=2 (dichotom. variables only) but it could be more for
9041: * historical reasons: 3 if coded 1, 2, 3 and 4 and Ndum[2]=0 */
9042: } /* Ndum[-1] number of undefined modalities */
9043:
9044: /* j is a covariate, n=Tvar[j] of Vn; Fills nbcode */
9045: /* For covariate j, modalities could be 1, 2, 3, 4, 5, 6, 7. */
9046: /* If Ndum[1]=0, Ndum[2]=0, Ndum[3]= 635, Ndum[4]=0, Ndum[5]=0, Ndum[6]=27, Ndum[7]=125; */
9047: /* modmincovj=3; modmaxcovj = 7; */
9048: /* There are only 3 modalities non empty 3, 6, 7 (or 2 if 27 is too few) : ncodemax[j]=3; */
9049: /* which will be coded 0, 1, 2 which in binary on 2=3-1 digits are 0=00 1=01, 2=10; */
9050: /* defining two dummy variables: variables V1_1 and V1_2.*/
9051: /* nbcode[Tvar[j]][ij]=k; */
9052: /* nbcode[Tvar[j]][1]=0; */
9053: /* nbcode[Tvar[j]][2]=1; */
9054: /* nbcode[Tvar[j]][3]=2; */
9055: /* To be continued (not working yet). */
9056: ij=0; /* ij is similar to i but can jump over null modalities */
9057:
9058: /* 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*/
9059: /* Skipping the case of missing values by reducing nbcode to 0 and 1 and not -1, 0, 1 */
9060: /* model=V1+V2+V3, if V2=-1, 0 or 1, then nbcode[2][1]=0 and nbcode[2][2]=1 instead of
9061: * nbcode[2][1]=-1, nbcode[2][2]=0 and nbcode[2][3]=1 */
9062: /*, could be restored in the future */
9063: 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*/
9064: if (Ndum[i] == 0) { /* If nobody responded to this modality k */
9065: break;
9066: }
9067: ij++;
9068: 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*/
9069: cptcode = ij; /* New max modality for covar j */
9070: } /* end of loop on modality i=-1 to 1 or more */
9071: break;
9072: case 1: /* Testing on varying covariate, could be simple and
9073: * should look at waves or product of fixed *
9074: * varying. No time to test -1, assuming 0 and 1 only */
9075: ij=0;
9076: for(i=0; i<=1;i++){
9077: nbcode[Tvar[k]][++ij]=i;
9078: }
9079: break;
9080: default:
9081: break;
9082: } /* end switch */
9083: } /* end dummy test */
9084: if(Dummy[k]==1 && Typevar[k] !=1 && Typevar[k] !=3 && Fixed ==0){ /* Fixed Quantitative covariate and not age product */
9085: 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*/
9086: if(Tvar[k]<=0 || Tvar[k]>=NCOVMAX){
9087: printf("Error k=%d \n",k);
9088: exit(1);
9089: }
9090: if(isnan(covar[Tvar[k]][i])){
9091: printf("ERROR, IMaCh doesn't treat fixed quantitative covariate with missing values V%d=., individual %d will be skipped.\n",Tvar[k],i);
9092: fprintf(ficlog,"ERROR, currently IMaCh doesn't treat covariate with missing values V%d=., individual %d will be skipped.\n",Tvar[k],i);
9093: fflush(ficlog);
9094: exit(1);
9095: }
9096: }
9097: } /* end Quanti */
9098: } /* 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*/
9099:
9100: for (k=-1; k< maxncov; k++) Ndum[k]=0;
9101: /* Look at fixed dummy (single or product) covariates to check empty modalities */
9102: for (i=1; i<=ncovmodel-2-nagesqr; i++) { /* -2, cste and age and eventually age*age */
9103: /* Listing of all covariables in statement model to see if some covariates appear twice. For example, V1 appears twice in V1+V1*V2.*/
9104: 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 */
9105: 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 */
9106: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, {2, 1, 1, 1, 2, 1, 1, 0, 0} */
9107: } /* V4+V3+V5, Ndum[1]@5={0, 0, 1, 1, 1} */
9108:
9109: ij=0;
9110: /* for (i=0; i<= maxncov-1; i++) { /\* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) *\/ */
9111: 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 */
9112: /* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) */
9113: /*printf("Ndum[%d]=%d\n",i, Ndum[i]);*/
9114: /* if((Ndum[i]!=0) && (i<=ncovcol)){ /\* Tvar[i] <= ncovmodel ? *\/ */
9115: if(Ndum[Tvar[k]]!=0 && Dummy[k] == 0 && Typevar[k]==0){ /* Only Dummy simple and non empty in the model */
9116: /* Typevar[k] =0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product */
9117: /* Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product*/
9118: /* If product not in single variable we don't print results */
9119: /*printf("diff Ndum[%d]=%d\n",i, Ndum[i]);*/
9120: ++ij;/* V5 + V4 + V3 + V4*V3 + V5*age + V2 + V1*V2 + V1*age + V1, *//* V5 quanti, V2 quanti, V4, V3, V1 dummies */
9121: /* k= 1 2 3 4 5 6 7 8 9 */
9122: /* Tvar[k]= 5 4 3 6 5 2 7 1 1 */
9123: /* ij 1 2 3 */
9124: /* Tvaraff[ij]= 4 3 1 */
9125: /* Tmodelind[ij]=2 3 9 */
9126: /* TmodelInvind[ij]=2 1 1 */
9127: 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*/
9128: Tmodelind[ij]=k; /* Tmodelind: index in model of dummies Tmodelind[1]=2 V4: pos=2; V3: pos=3, V1=9 {2, 3, 9, ?, ?,} */
9129: 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 */
9130: if(Fixed[k]!=0)
9131: anyvaryingduminmodel=1;
9132: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv)){ */
9133: /* Tvaraff[++ij]=-10; /\* Dont'n know how to treat quantitative variables yet *\/ */
9134: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv)){ */
9135: /* Tvaraff[++ij]=i; /\*For printing (unclear) *\/ */
9136: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv+nqtv)){ */
9137: /* Tvaraff[++ij]=-20; /\* Dont'n know how to treat quantitative variables yet *\/ */
9138: }
9139: } /* Tvaraff[1]@5 {3, 4, -20, 0, 0} Very strange */
9140: /* ij--; */
9141: /* cptcoveff=ij; /\*Number of total covariates*\/ */
9142: *cptcov=ij; /* cptcov= Number of total real effective simple dummies (fixed or time arying) effective (used as cptcoveff in other functions)
9143: * because they can be excluded from the model and real
9144: * if in the model but excluded because missing values, but how to get k from ij?*/
9145: for(j=ij+1; j<= cptcovt; j++){
9146: Tvaraff[j]=0;
9147: Tmodelind[j]=0;
9148: }
9149: for(j=ntveff+1; j<= cptcovt; j++){
9150: TmodelInvind[j]=0;
9151: }
9152: /* To be sorted */
9153: ;
9154: }
9155:
9156:
9157: /*********** Health Expectancies ****************/
9158:
9159: 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 )
9160:
9161: {
9162: /* Health expectancies, no variances */
9163: /* cij is the combination in the list of combination of dummy covariates */
9164: /* strstart is a string of time at start of computing */
9165: int i, j, nhstepm, hstepm, h, nstepm;
9166: int nhstepma, nstepma; /* Decreasing with age */
9167: double age, agelim, hf;
9168: double ***p3mat;
9169: double eip;
9170:
9171: /* pstamp(ficreseij); */
9172: fprintf(ficreseij,"# (a) Life expectancies by health status at initial age and (b) health expectancies by health status at initial age\n");
9173: fprintf(ficreseij,"# Age");
9174: for(i=1; i<=nlstate;i++){
9175: for(j=1; j<=nlstate;j++){
9176: fprintf(ficreseij," e%1d%1d ",i,j);
9177: }
9178: fprintf(ficreseij," e%1d. ",i);
9179: }
9180: fprintf(ficreseij,"\n");
9181:
9182:
9183: if(estepm < stepm){
9184: printf ("Problem %d lower than %d\n",estepm, stepm);
9185: }
9186: else hstepm=estepm;
9187: /* We compute the life expectancy from trapezoids spaced every estepm months
9188: * This is mainly to measure the difference between two models: for example
9189: * if stepm=24 months pijx are given only every 2 years and by summing them
9190: * we are calculating an estimate of the Life Expectancy assuming a linear
9191: * progression in between and thus overestimating or underestimating according
9192: * to the curvature of the survival function. If, for the same date, we
9193: * estimate the model with stepm=1 month, we can keep estepm to 24 months
9194: * to compare the new estimate of Life expectancy with the same linear
9195: * hypothesis. A more precise result, taking into account a more precise
9196: * curvature will be obtained if estepm is as small as stepm. */
9197:
9198: /* For example we decided to compute the life expectancy with the smallest unit */
9199: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
9200: nhstepm is the number of hstepm from age to agelim
9201: nstepm is the number of stepm from age to agelin.
9202: Look at hpijx to understand the reason which relies in memory size consideration
9203: and note for a fixed period like estepm months */
9204: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
9205: survival function given by stepm (the optimization length). Unfortunately it
9206: means that if the survival funtion is printed only each two years of age and if
9207: you sum them up and add 1 year (area under the trapezoids) you won't get the same
9208: results. So we changed our mind and took the option of the best precision.
9209: */
9210: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
9211:
9212: agelim=AGESUP;
9213: /* If stepm=6 months */
9214: /* Computed by stepm unit matrices, product of hstepm matrices, stored
9215: in an array of nhstepm length: nhstepm=10, hstepm=4, stepm=6 months */
9216:
9217: /* nhstepm age range expressed in number of stepm */
9218: nstepm=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
9219: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
9220: /* if (stepm >= YEARM) hstepm=1;*/
9221: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
9222: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
9223:
9224: for (age=bage; age<=fage; age ++){
9225: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
9226: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
9227: /* if (stepm >= YEARM) hstepm=1;*/
9228: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
9229:
9230: /* If stepm=6 months */
9231: /* Computed by stepm unit matrices, product of hstepma matrices, stored
9232: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
9233: /* printf("HELLO evsij Entering hpxij age=%d cij=%d hstepm=%d x[1]=%f nres=%d\n",(int) age, cij, hstepm, x[1], nres); */
9234: hpxij(p3mat,nhstepma,age,hstepm,x,nlstate,stepm,oldm, savm, cij, nres);
9235:
9236: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
9237:
9238: printf("%d|",(int)age);fflush(stdout);
9239: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
9240:
9241: /* Computing expectancies */
9242: for(i=1; i<=nlstate;i++)
9243: for(j=1; j<=nlstate;j++)
9244: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
9245: eij[i][j][(int)age] += (p3mat[i][j][h]+p3mat[i][j][h+1])/2.0*hf;
9246:
9247: /* 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]);*/
9248:
9249: }
9250:
9251: fprintf(ficreseij,"%3.0f",age );
9252: for(i=1; i<=nlstate;i++){
9253: eip=0;
9254: for(j=1; j<=nlstate;j++){
9255: eip +=eij[i][j][(int)age];
9256: fprintf(ficreseij,"%9.4f", eij[i][j][(int)age] );
9257: }
9258: fprintf(ficreseij,"%9.4f", eip );
9259: }
9260: fprintf(ficreseij,"\n");
9261:
9262: }
9263: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
9264: printf("\n");
9265: fprintf(ficlog,"\n");
9266:
9267: }
9268:
9269: 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 )
9270:
9271: {
9272: /* Covariances of health expectancies eij and of total life expectancies according
9273: to initial status i, ei. .
9274: */
9275: /* Very time consuming function, but already optimized with precov */
9276: int i, j, nhstepm, hstepm, h, nstepm, k, cptj, cptj2, i2, j2, ij, ji;
9277: int nhstepma, nstepma; /* Decreasing with age */
9278: double age, agelim, hf;
9279: double ***p3matp, ***p3matm, ***varhe;
9280: double **dnewm,**doldm;
9281: double *xp, *xm;
9282: double **gp, **gm;
9283: double ***gradg, ***trgradg;
9284: int theta;
9285:
9286: double eip, vip;
9287:
9288: varhe=ma3x(1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int) fage);
9289: xp=vector(1,npar);
9290: xm=vector(1,npar);
9291: dnewm=matrix(1,nlstate*nlstate,1,npar);
9292: doldm=matrix(1,nlstate*nlstate,1,nlstate*nlstate);
9293:
9294: pstamp(ficresstdeij);
9295: fprintf(ficresstdeij,"# Health expectancies with standard errors\n");
9296: fprintf(ficresstdeij,"# Age");
9297: for(i=1; i<=nlstate;i++){
9298: for(j=1; j<=nlstate;j++)
9299: fprintf(ficresstdeij," e%1d%1d (SE)",i,j);
9300: fprintf(ficresstdeij," e%1d. ",i);
9301: }
9302: fprintf(ficresstdeij,"\n");
9303:
9304: pstamp(ficrescveij);
9305: fprintf(ficrescveij,"# Subdiagonal matrix of covariances of health expectancies by age: cov(eij,ekl)\n");
9306: fprintf(ficrescveij,"# Age");
9307: for(i=1; i<=nlstate;i++)
9308: for(j=1; j<=nlstate;j++){
9309: cptj= (j-1)*nlstate+i;
9310: for(i2=1; i2<=nlstate;i2++)
9311: for(j2=1; j2<=nlstate;j2++){
9312: cptj2= (j2-1)*nlstate+i2;
9313: if(cptj2 <= cptj)
9314: fprintf(ficrescveij," %1d%1d,%1d%1d",i,j,i2,j2);
9315: }
9316: }
9317: fprintf(ficrescveij,"\n");
9318:
9319: if(estepm < stepm){
9320: printf ("Problem %d lower than %d\n",estepm, stepm);
9321: }
9322: else hstepm=estepm;
9323: /* We compute the life expectancy from trapezoids spaced every estepm months
9324: * This is mainly to measure the difference between two models: for example
9325: * if stepm=24 months pijx are given only every 2 years and by summing them
9326: * we are calculating an estimate of the Life Expectancy assuming a linear
9327: * progression in between and thus overestimating or underestimating according
9328: * to the curvature of the survival function. If, for the same date, we
9329: * estimate the model with stepm=1 month, we can keep estepm to 24 months
9330: * to compare the new estimate of Life expectancy with the same linear
9331: * hypothesis. A more precise result, taking into account a more precise
9332: * curvature will be obtained if estepm is as small as stepm. */
9333:
9334: /* For example we decided to compute the life expectancy with the smallest unit */
9335: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
9336: nhstepm is the number of hstepm from age to agelim
9337: nstepm is the number of stepm from age to agelin.
9338: Look at hpijx to understand the reason of that which relies in memory size
9339: and note for a fixed period like estepm months */
9340: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
9341: survival function given by stepm (the optimization length). Unfortunately it
9342: means that if the survival funtion is printed only each two years of age and if
9343: you sum them up and add 1 year (area under the trapezoids) you won't get the same
9344: results. So we changed our mind and took the option of the best precision.
9345: */
9346: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
9347:
9348: /* If stepm=6 months */
9349: /* nhstepm age range expressed in number of stepm */
9350: agelim=AGESUP;
9351: nstepm=(int) rint((agelim-bage)*YEARM/stepm);
9352: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
9353: /* if (stepm >= YEARM) hstepm=1;*/
9354: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
9355:
9356: p3matp=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
9357: p3matm=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
9358: gradg=ma3x(0,nhstepm,1,npar,1,nlstate*nlstate);
9359: trgradg =ma3x(0,nhstepm,1,nlstate*nlstate,1,npar);
9360: gp=matrix(0,nhstepm,1,nlstate*nlstate);
9361: gm=matrix(0,nhstepm,1,nlstate*nlstate);
9362:
9363: for (age=bage; age<=fage; age ++){
9364: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
9365: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
9366: /* if (stepm >= YEARM) hstepm=1;*/
9367: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
9368:
9369: /* If stepm=6 months */
9370: /* Computed by stepm unit matrices, product of hstepma matrices, stored
9371: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
9372:
9373: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
9374:
9375: /* Computing Variances of health expectancies */
9376: /* Gradient is computed with plus gp and minus gm. Code is duplicated in order to
9377: decrease memory allocation */
9378: for(theta=1; theta <=npar; theta++){
9379: for(i=1; i<=npar; i++){
9380: xp[i] = x[i] + (i==theta ?delti[theta]:0);
9381: xm[i] = x[i] - (i==theta ?delti[theta]:0);
9382: }
9383: hpxij(p3matp,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, cij, nres);
9384: hpxij(p3matm,nhstepm,age,hstepm,xm,nlstate,stepm,oldm,savm, cij, nres);
9385:
9386: for(j=1; j<= nlstate; j++){
9387: for(i=1; i<=nlstate; i++){
9388: for(h=0; h<=nhstepm-1; h++){
9389: gp[h][(j-1)*nlstate + i] = (p3matp[i][j][h]+p3matp[i][j][h+1])/2.;
9390: gm[h][(j-1)*nlstate + i] = (p3matm[i][j][h]+p3matm[i][j][h+1])/2.;
9391: }
9392: }
9393: }
9394:
9395: for(ij=1; ij<= nlstate*nlstate; ij++)
9396: for(h=0; h<=nhstepm-1; h++){
9397: gradg[h][theta][ij]= (gp[h][ij]-gm[h][ij])/2./delti[theta];
9398: }
9399: }/* End theta */
9400:
9401:
9402: for(h=0; h<=nhstepm-1; h++)
9403: for(j=1; j<=nlstate*nlstate;j++)
9404: for(theta=1; theta <=npar; theta++)
9405: trgradg[h][j][theta]=gradg[h][theta][j];
9406:
9407:
9408: for(ij=1;ij<=nlstate*nlstate;ij++)
9409: for(ji=1;ji<=nlstate*nlstate;ji++)
9410: varhe[ij][ji][(int)age] =0.;
9411:
9412: printf("%d|",(int)age);fflush(stdout);
9413: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
9414: for(h=0;h<=nhstepm-1;h++){
9415: for(k=0;k<=nhstepm-1;k++){
9416: matprod2(dnewm,trgradg[h],1,nlstate*nlstate,1,npar,1,npar,matcov);
9417: matprod2(doldm,dnewm,1,nlstate*nlstate,1,npar,1,nlstate*nlstate,gradg[k]);
9418: for(ij=1;ij<=nlstate*nlstate;ij++)
9419: for(ji=1;ji<=nlstate*nlstate;ji++)
9420: varhe[ij][ji][(int)age] += doldm[ij][ji]*hf*hf;
9421: }
9422: }
9423: /* if((int)age ==50){ */
9424: /* printf(" age=%d cij=%d nres=%d varhe[%d][%d]=%f ",(int)age, cij, nres, 1,2,varhe[1][2]); */
9425: /* } */
9426: /* Computing expectancies */
9427: hpxij(p3matm,nhstepm,age,hstepm,x,nlstate,stepm,oldm, savm, cij,nres);
9428: for(i=1; i<=nlstate;i++)
9429: for(j=1; j<=nlstate;j++)
9430: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
9431: eij[i][j][(int)age] += (p3matm[i][j][h]+p3matm[i][j][h+1])/2.0*hf;
9432:
9433: /* 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]);*/
9434:
9435: }
9436:
9437: /* Standard deviation of expectancies ij */
9438: fprintf(ficresstdeij,"%3.0f",age );
9439: for(i=1; i<=nlstate;i++){
9440: eip=0.;
9441: vip=0.;
9442: for(j=1; j<=nlstate;j++){
9443: eip += eij[i][j][(int)age];
9444: for(k=1; k<=nlstate;k++) /* Sum on j and k of cov(eij,eik) */
9445: vip += varhe[(j-1)*nlstate+i][(k-1)*nlstate+i][(int)age];
9446: fprintf(ficresstdeij," %9.4f (%.4f)", eij[i][j][(int)age], sqrt(varhe[(j-1)*nlstate+i][(j-1)*nlstate+i][(int)age]) );
9447: }
9448: fprintf(ficresstdeij," %9.4f (%.4f)", eip, sqrt(vip));
9449: }
9450: fprintf(ficresstdeij,"\n");
9451:
9452: /* Variance of expectancies ij */
9453: fprintf(ficrescveij,"%3.0f",age );
9454: for(i=1; i<=nlstate;i++)
9455: for(j=1; j<=nlstate;j++){
9456: cptj= (j-1)*nlstate+i;
9457: for(i2=1; i2<=nlstate;i2++)
9458: for(j2=1; j2<=nlstate;j2++){
9459: cptj2= (j2-1)*nlstate+i2;
9460: if(cptj2 <= cptj)
9461: fprintf(ficrescveij," %.4f", varhe[cptj][cptj2][(int)age]);
9462: }
9463: }
9464: fprintf(ficrescveij,"\n");
9465:
9466: }
9467: free_matrix(gm,0,nhstepm,1,nlstate*nlstate);
9468: free_matrix(gp,0,nhstepm,1,nlstate*nlstate);
9469: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate*nlstate);
9470: free_ma3x(trgradg,0,nhstepm,1,nlstate*nlstate,1,npar);
9471: free_ma3x(p3matm,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
9472: free_ma3x(p3matp,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
9473: printf("\n");
9474: fprintf(ficlog,"\n");
9475:
9476: free_vector(xm,1,npar);
9477: free_vector(xp,1,npar);
9478: free_matrix(dnewm,1,nlstate*nlstate,1,npar);
9479: free_matrix(doldm,1,nlstate*nlstate,1,nlstate*nlstate);
9480: free_ma3x(varhe,1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int)fage);
9481: }
9482:
9483: /************ Variance ******************/
9484: 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)
9485: {
9486: /** Variance of health expectancies
9487: * double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);
9488: * double **newm;
9489: * int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav)
9490: */
9491:
9492: /* int movingaverage(); */
9493: double **dnewm,**doldm;
9494: double **dnewmp,**doldmp;
9495: int i, j, nhstepm, hstepm, h, nstepm ;
9496: int first=0;
9497: int k;
9498: double *xp;
9499: double **gp, **gm; /**< for var eij */
9500: double ***gradg, ***trgradg; /**< for var eij */
9501: double **gradgp, **trgradgp; /**< for var p point j */
9502: double *gpp, *gmp; /**< for var p point j */
9503: double **varppt; /**< for var p point j nlstate to nlstate+ndeath */
9504: double ***p3mat;
9505: double age,agelim, hf;
9506: /* double ***mobaverage; */
9507: int theta;
9508: char digit[4];
9509: char digitp[25];
9510:
9511: char fileresprobmorprev[FILENAMELENGTH];
9512:
9513: if(popbased==1){
9514: if(mobilav!=0)
9515: strcpy(digitp,"-POPULBASED-MOBILAV_");
9516: else strcpy(digitp,"-POPULBASED-NOMOBIL_");
9517: }
9518: else
9519: strcpy(digitp,"-STABLBASED_");
9520:
9521: /* if (mobilav!=0) { */
9522: /* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
9523: /* if (movingaverage(probs, bage, fage, mobaverage,mobilav)!=0){ */
9524: /* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); */
9525: /* printf(" Error in movingaverage mobilav=%d\n",mobilav); */
9526: /* } */
9527: /* } */
9528:
9529: strcpy(fileresprobmorprev,"PRMORPREV-");
9530: sprintf(digit,"%-d",ij);
9531: /*printf("DIGIT=%s, ij=%d ijr=%-d|\n",digit, ij,ij);*/
9532: strcat(fileresprobmorprev,digit); /* Tvar to be done */
9533: strcat(fileresprobmorprev,digitp); /* Popbased or not, mobilav or not */
9534: strcat(fileresprobmorprev,fileresu);
9535: if((ficresprobmorprev=fopen(fileresprobmorprev,"w"))==NULL) {
9536: printf("Problem with resultfile: %s\n", fileresprobmorprev);
9537: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobmorprev);
9538: }
9539: printf("Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
9540: fprintf(ficlog,"Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
9541: pstamp(ficresprobmorprev);
9542: 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);
9543: fprintf(ficresprobmorprev,"# Selected quantitative variables and dummies");
9544:
9545: /* We use TinvDoQresult[nres][resultmodel[nres][j] we sort according to the equation model and the resultline: it is a choice */
9546: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ /\* To be done*\/ */
9547: /* fprintf(ficresprobmorprev," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
9548: /* } */
9549: for (j=1; j<= cptcovs; j++){ /* For each selected (single) quantitative value */ /* To be done*/
9550: /* fprintf(ficresprobmorprev," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]); */
9551: fprintf(ficresprobmorprev," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
9552: }
9553: /* for(j=1;j<=cptcoveff;j++) */
9554: /* fprintf(ficresprobmorprev," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(ij,TnsdVar[Tvaraff[j]])]); */
9555: fprintf(ficresprobmorprev,"\n");
9556:
9557: fprintf(ficresprobmorprev,"# Age cov=%-d",ij);
9558: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
9559: fprintf(ficresprobmorprev," p.%-d SE",j);
9560: for(i=1; i<=nlstate;i++)
9561: fprintf(ficresprobmorprev," w%1d p%-d%-d",i,i,j);
9562: }
9563: fprintf(ficresprobmorprev,"\n");
9564:
9565: fprintf(ficgp,"\n# Routine varevsij");
9566: fprintf(ficgp,"\nunset title \n");
9567: /* fprintf(fichtm, "#Local time at start: %s", strstart);*/
9568: 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");
9569: fprintf(fichtm,"\n<br>%s <br>\n",digitp);
9570:
9571: varppt = matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
9572: pstamp(ficresvij);
9573: fprintf(ficresvij,"# Variance and covariance of health expectancies e.j \n# (weighted average of eij where weights are ");
9574: if(popbased==1)
9575: 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);
9576: else
9577: fprintf(ficresvij,"the age specific period (stable) prevalences in each health state \n");
9578: fprintf(ficresvij,"# Age");
9579: for(i=1; i<=nlstate;i++)
9580: for(j=1; j<=nlstate;j++)
9581: fprintf(ficresvij," Cov(e.%1d, e.%1d)",i,j);
9582: fprintf(ficresvij,"\n");
9583:
9584: xp=vector(1,npar);
9585: dnewm=matrix(1,nlstate,1,npar);
9586: doldm=matrix(1,nlstate,1,nlstate);
9587: dnewmp= matrix(nlstate+1,nlstate+ndeath,1,npar);
9588: doldmp= matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
9589:
9590: gradgp=matrix(1,npar,nlstate+1,nlstate+ndeath);
9591: gpp=vector(nlstate+1,nlstate+ndeath);
9592: gmp=vector(nlstate+1,nlstate+ndeath);
9593: trgradgp =matrix(nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
9594:
9595: if(estepm < stepm){
9596: printf ("Problem %d lower than %d\n",estepm, stepm);
9597: }
9598: else hstepm=estepm;
9599: /* For example we decided to compute the life expectancy with the smallest unit */
9600: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
9601: nhstepm is the number of hstepm from age to agelim
9602: nstepm is the number of stepm from age to agelim.
9603: Look at function hpijx to understand why because of memory size limitations,
9604: we decided (b) to get a life expectancy respecting the most precise curvature of the
9605: survival function given by stepm (the optimization length). Unfortunately it
9606: means that if the survival funtion is printed every two years of age and if
9607: you sum them up and add 1 year (area under the trapezoids) you won't get the same
9608: results. So we changed our mind and took the option of the best precision.
9609: */
9610: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
9611: agelim = AGESUP;
9612: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
9613: nstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
9614: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
9615: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
9616: gradg=ma3x(0,nhstepm,1,npar,1,nlstate);
9617: gp=matrix(0,nhstepm,1,nlstate);
9618: gm=matrix(0,nhstepm,1,nlstate);
9619:
9620:
9621: for(theta=1; theta <=npar; theta++){
9622: for(i=1; i<=npar; i++){ /* Computes gradient x + delta*/
9623: xp[i] = x[i] + (i==theta ?delti[theta]:0);
9624: }
9625: /**< Computes the prevalence limit with parameter theta shifted of delta up to ftolpl precision and
9626: * returns into prlim .
9627: */
9628: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij, nres);
9629:
9630: /* If popbased = 1 we use crossection prevalences. Previous step is useless but prlim is created */
9631: if (popbased==1) {
9632: if(mobilav ==0){
9633: for(i=1; i<=nlstate;i++)
9634: prlim[i][i]=probs[(int)age][i][ij];
9635: }else{ /* mobilav */
9636: for(i=1; i<=nlstate;i++)
9637: prlim[i][i]=mobaverage[(int)age][i][ij];
9638: }
9639: }
9640: /**< Computes the shifted transition matrix \f$ {}{h}_p^{ij}x\f$ at horizon h.
9641: */
9642: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij,nres); /* Returns p3mat[i][j][h] for h=0 to nhstepm */
9643: /**< And for each alive state j, sums over i \f$ w^i_x {}{h}_p^{ij}x\f$, which are the probability
9644: * at horizon h in state j including mortality.
9645: */
9646: for(j=1; j<= nlstate; j++){
9647: for(h=0; h<=nhstepm; h++){
9648: for(i=1, gp[h][j]=0.;i<=nlstate;i++)
9649: gp[h][j] += prlim[i][i]*p3mat[i][j][h];
9650: }
9651: }
9652: /* Next for computing shifted+ probability of death (h=1 means
9653: computed over hstepm matrices product = hstepm*stepm months)
9654: as a weighted average of prlim(i) * p(i,j) p.3=w1*p13 + w2*p23 .
9655: */
9656: for(j=nlstate+1;j<=nlstate+ndeath;j++){
9657: for(i=1,gpp[j]=0.; i<= nlstate; i++)
9658: gpp[j] += prlim[i][i]*p3mat[i][j][1];
9659: }
9660:
9661: /* Again with minus shift */
9662:
9663: for(i=1; i<=npar; i++) /* Computes gradient x - delta */
9664: xp[i] = x[i] - (i==theta ?delti[theta]:0);
9665:
9666: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp, ij, nres);
9667:
9668: if (popbased==1) {
9669: if(mobilav ==0){
9670: for(i=1; i<=nlstate;i++)
9671: prlim[i][i]=probs[(int)age][i][ij];
9672: }else{ /* mobilav */
9673: for(i=1; i<=nlstate;i++)
9674: prlim[i][i]=mobaverage[(int)age][i][ij];
9675: }
9676: }
9677:
9678: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij,nres);
9679:
9680: for(j=1; j<= nlstate; j++){ /* Sum of wi * eij = e.j */
9681: for(h=0; h<=nhstepm; h++){
9682: for(i=1, gm[h][j]=0.;i<=nlstate;i++)
9683: gm[h][j] += prlim[i][i]*p3mat[i][j][h];
9684: }
9685: }
9686: /* This for computing probability of death (h=1 means
9687: computed over hstepm matrices product = hstepm*stepm months)
9688: as a weighted average of prlim.
9689: */
9690: for(j=nlstate+1;j<=nlstate+ndeath;j++){
9691: for(i=1,gmp[j]=0.; i<= nlstate; i++)
9692: gmp[j] += prlim[i][i]*p3mat[i][j][1];
9693: }
9694: /* end shifting computations */
9695:
9696: /**< Computing gradient matrix at horizon h
9697: */
9698: for(j=1; j<= nlstate; j++) /* vareij */
9699: for(h=0; h<=nhstepm; h++){
9700: gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta];
9701: }
9702: /**< Gradient of overall mortality p.3 (or p.j)
9703: */
9704: for(j=nlstate+1; j<= nlstate+ndeath; j++){ /* var mu mortality from j */
9705: gradgp[theta][j]= (gpp[j]-gmp[j])/2./delti[theta];
9706: }
9707:
9708: } /* End theta */
9709:
9710: /* We got the gradient matrix for each theta and state j */
9711: trgradg =ma3x(0,nhstepm,1,nlstate,1,npar); /* veij */
9712:
9713: for(h=0; h<=nhstepm; h++) /* veij */
9714: for(j=1; j<=nlstate;j++)
9715: for(theta=1; theta <=npar; theta++)
9716: trgradg[h][j][theta]=gradg[h][theta][j];
9717:
9718: for(j=nlstate+1; j<=nlstate+ndeath;j++) /* mu */
9719: for(theta=1; theta <=npar; theta++)
9720: trgradgp[j][theta]=gradgp[theta][j];
9721: /**< as well as its transposed matrix
9722: */
9723:
9724: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
9725: for(i=1;i<=nlstate;i++)
9726: for(j=1;j<=nlstate;j++)
9727: vareij[i][j][(int)age] =0.;
9728:
9729: /* Computing trgradg by matcov by gradg at age and summing over h
9730: * and k (nhstepm) formula 15 of article
9731: * Lievre-Brouard-Heathcote
9732: */
9733:
9734: for(h=0;h<=nhstepm;h++){
9735: for(k=0;k<=nhstepm;k++){
9736: matprod2(dnewm,trgradg[h],1,nlstate,1,npar,1,npar,matcov);
9737: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg[k]);
9738: for(i=1;i<=nlstate;i++)
9739: for(j=1;j<=nlstate;j++)
9740: vareij[i][j][(int)age] += doldm[i][j]*hf*hf;
9741: }
9742: }
9743:
9744: /* pptj is p.3 or p.j = trgradgp by cov by gradgp, variance of
9745: * p.j overall mortality formula 49 but computed directly because
9746: * we compute the grad (wix pijx) instead of grad (pijx),even if
9747: * wix is independent of theta.
9748: */
9749: matprod2(dnewmp,trgradgp,nlstate+1,nlstate+ndeath,1,npar,1,npar,matcov);
9750: matprod2(doldmp,dnewmp,nlstate+1,nlstate+ndeath,1,npar,nlstate+1,nlstate+ndeath,gradgp);
9751: for(j=nlstate+1;j<=nlstate+ndeath;j++)
9752: for(i=nlstate+1;i<=nlstate+ndeath;i++)
9753: varppt[j][i]=doldmp[j][i];
9754: /* end ppptj */
9755: /* x centered again */
9756:
9757: prevalim(prlim,nlstate,x,age,oldm,savm,ftolpl,ncvyearp,ij, nres);
9758:
9759: if (popbased==1) {
9760: if(mobilav ==0){
9761: for(i=1; i<=nlstate;i++)
9762: prlim[i][i]=probs[(int)age][i][ij];
9763: }else{ /* mobilav */
9764: for(i=1; i<=nlstate;i++)
9765: prlim[i][i]=mobaverage[(int)age][i][ij];
9766: }
9767: }
9768:
9769: /* This for computing probability of death (h=1 means
9770: computed over hstepm (estepm) matrices product = hstepm*stepm months)
9771: as a weighted average of prlim.
9772: */
9773: hpxij(p3mat,nhstepm,age,hstepm,x,nlstate,stepm,oldm,savm, ij, nres);
9774: for(j=nlstate+1;j<=nlstate+ndeath;j++){
9775: for(i=1,gmp[j]=0.;i<= nlstate; i++)
9776: gmp[j] += prlim[i][i]*p3mat[i][j][1];
9777: }
9778: /* end probability of death */
9779:
9780: fprintf(ficresprobmorprev,"%3d %d ",(int) age, ij);
9781: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
9782: fprintf(ficresprobmorprev," %11.3e %11.3e",gmp[j], sqrt(varppt[j][j]));
9783: for(i=1; i<=nlstate;i++){
9784: fprintf(ficresprobmorprev," %11.3e %11.3e ",prlim[i][i],p3mat[i][j][1]);
9785: }
9786: }
9787: fprintf(ficresprobmorprev,"\n");
9788:
9789: fprintf(ficresvij,"%.0f ",age );
9790: for(i=1; i<=nlstate;i++)
9791: for(j=1; j<=nlstate;j++){
9792: fprintf(ficresvij," %.4f", vareij[i][j][(int)age]);
9793: }
9794: fprintf(ficresvij,"\n");
9795: free_matrix(gp,0,nhstepm,1,nlstate);
9796: free_matrix(gm,0,nhstepm,1,nlstate);
9797: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate);
9798: free_ma3x(trgradg,0,nhstepm,1,nlstate,1,npar);
9799: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
9800: } /* End age */
9801: free_vector(gpp,nlstate+1,nlstate+ndeath);
9802: free_vector(gmp,nlstate+1,nlstate+ndeath);
9803: free_matrix(gradgp,1,npar,nlstate+1,nlstate+ndeath);
9804: free_matrix(trgradgp,nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
9805: /* fprintf(ficgp,"\nunset parametric;unset label; set ter png small size 320, 240"); */
9806: fprintf(ficgp,"\nunset parametric;unset label; set ter svg size 640, 480");
9807: /* for(j=nlstate+1; j<= nlstate+ndeath; j++){ *//* Only the first actually */
9808: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Force of mortality (year-1)\";");
9809: fprintf(ficgp,"\nset out \"%s%s.svg\";",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
9810: /* fprintf(ficgp,"\n plot \"%s\" u 1:($3*%6.3f) not w l 1 ",fileresprobmorprev,YEARM/estepm); */
9811: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)*%6.3f) t \"95\%% interval\" w l 2 ",fileresprobmorprev,YEARM/estepm); */
9812: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)*%6.3f) not w l 2 ",fileresprobmorprev,YEARM/estepm); */
9813: fprintf(ficgp,"\n plot \"%s\" u 1:($3) not w l lt 1 ",subdirf(fileresprobmorprev));
9814: fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)) t \"95%% interval\" w l lt 2 ",subdirf(fileresprobmorprev));
9815: fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)) not w l lt 2 ",subdirf(fileresprobmorprev));
9816: fprintf(fichtm,"\n<br> File (multiple files are possible if covariates are present): <A href=\"%s\">%s</a>\n",subdirf(fileresprobmorprev),subdirf(fileresprobmorprev));
9817: 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);
9818: /* 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);
9819: */
9820: /* fprintf(ficgp,"\nset out \"varmuptjgr%s%s%s.svg\";replot;",digitp,optionfilefiname,digit); */
9821: fprintf(ficgp,"\nset out;\nset out \"%s%s.svg\";replot;set out;\n",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
9822:
9823: free_vector(xp,1,npar);
9824: free_matrix(doldm,1,nlstate,1,nlstate);
9825: free_matrix(dnewm,1,nlstate,1,npar);
9826: free_matrix(doldmp,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
9827: free_matrix(dnewmp,nlstate+1,nlstate+ndeath,1,npar);
9828: free_matrix(varppt,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
9829: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
9830: fclose(ficresprobmorprev);
9831: fflush(ficgp);
9832: fflush(fichtm);
9833: } /* end varevsij */
9834:
9835: /************ Variance of prevlim ******************/
9836: 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)
9837: {
9838: /* Variance of prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/
9839: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
9840:
9841: double **dnewmpar,**doldm;
9842: int i, j, nhstepm, hstepm;
9843: double *xp;
9844: double *gp, *gm;
9845: double **gradg, **trgradg;
9846: double **mgm, **mgp;
9847: double age,agelim;
9848: int theta;
9849:
9850: pstamp(ficresvpl);
9851: fprintf(ficresvpl,"# Standard deviation of period (forward stable) prevalences \n");
9852: fprintf(ficresvpl,"# Age ");
9853: if(nresult >=1)
9854: fprintf(ficresvpl," Result# ");
9855: for(i=1; i<=nlstate;i++)
9856: fprintf(ficresvpl," %1d-%1d",i,i);
9857: fprintf(ficresvpl,"\n");
9858:
9859: xp=vector(1,npar);
9860: dnewmpar=matrix(1,nlstate,1,npar);
9861: doldm=matrix(1,nlstate,1,nlstate);
9862:
9863: hstepm=1*YEARM; /* Every year of age */
9864: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
9865: agelim = AGESUP;
9866: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
9867: nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
9868: if (stepm >= YEARM) hstepm=1;
9869: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
9870: gradg=matrix(1,npar,1,nlstate);
9871: mgp=matrix(1,npar,1,nlstate);
9872: mgm=matrix(1,npar,1,nlstate);
9873: gp=vector(1,nlstate);
9874: gm=vector(1,nlstate);
9875:
9876: for(theta=1; theta <=npar; theta++){
9877: for(i=1; i<=npar; i++){ /* Computes gradient */
9878: xp[i] = x[i] + (i==theta ?delti[theta]:0);
9879: }
9880: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ) */
9881: /* prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres); */
9882: /* else */
9883: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
9884: for(i=1;i<=nlstate;i++){
9885: gp[i] = prlim[i][i];
9886: mgp[theta][i] = prlim[i][i];
9887: }
9888: for(i=1; i<=npar; i++) /* Computes gradient */
9889: xp[i] = x[i] - (i==theta ?delti[theta]:0);
9890: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ) */
9891: /* prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres); */
9892: /* else */
9893: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
9894: for(i=1;i<=nlstate;i++){
9895: gm[i] = prlim[i][i];
9896: mgm[theta][i] = prlim[i][i];
9897: }
9898: for(i=1;i<=nlstate;i++)
9899: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
9900: /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */
9901: } /* End theta */
9902:
9903: trgradg =matrix(1,nlstate,1,npar);
9904:
9905: for(j=1; j<=nlstate;j++)
9906: for(theta=1; theta <=npar; theta++)
9907: trgradg[j][theta]=gradg[theta][j];
9908: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
9909: /* printf("\nmgm mgp %d ",(int)age); */
9910: /* for(j=1; j<=nlstate;j++){ */
9911: /* printf(" %d ",j); */
9912: /* for(theta=1; theta <=npar; theta++) */
9913: /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */
9914: /* printf("\n "); */
9915: /* } */
9916: /* } */
9917: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
9918: /* printf("\n gradg %d ",(int)age); */
9919: /* for(j=1; j<=nlstate;j++){ */
9920: /* printf("%d ",j); */
9921: /* for(theta=1; theta <=npar; theta++) */
9922: /* printf("%d %lf ",theta,gradg[theta][j]); */
9923: /* printf("\n "); */
9924: /* } */
9925: /* } */
9926:
9927: for(i=1;i<=nlstate;i++)
9928: varpl[i][(int)age] =0.;
9929: if((int)age==79 ||(int)age== 80 ||(int)age== 81){
9930: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
9931: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
9932: }else{
9933: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
9934: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
9935: }
9936: for(i=1;i<=nlstate;i++)
9937: varpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
9938:
9939: fprintf(ficresvpl,"%.0f ",age );
9940: if(nresult >=1)
9941: fprintf(ficresvpl,"%d ",nres );
9942: for(i=1; i<=nlstate;i++){
9943: fprintf(ficresvpl," %.5f (%.5f)",prlim[i][i],sqrt(varpl[i][(int)age]));
9944: /* for(j=1;j<=nlstate;j++) */
9945: /* fprintf(ficresvpl," %d %.5f ",j,prlim[j][i]); */
9946: }
9947: fprintf(ficresvpl,"\n");
9948: free_vector(gp,1,nlstate);
9949: free_vector(gm,1,nlstate);
9950: free_matrix(mgm,1,npar,1,nlstate);
9951: free_matrix(mgp,1,npar,1,nlstate);
9952: free_matrix(gradg,1,npar,1,nlstate);
9953: free_matrix(trgradg,1,nlstate,1,npar);
9954: } /* End age */
9955:
9956: free_vector(xp,1,npar);
9957: free_matrix(doldm,1,nlstate,1,npar);
9958: free_matrix(dnewmpar,1,nlstate,1,nlstate);
9959:
9960: }
9961:
9962:
9963: /************ Variance of backprevalence limit ******************/
9964: 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)
9965: {
9966: /* Variance of backward prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/
9967: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
9968:
9969: double **dnewmpar,**doldm;
9970: int i, j, nhstepm, hstepm;
9971: double *xp;
9972: double *gp, *gm;
9973: double **gradg, **trgradg;
9974: double **mgm, **mgp;
9975: double age,agelim;
9976: int theta;
9977:
9978: pstamp(ficresvbl);
9979: fprintf(ficresvbl,"# Standard deviation of back (stable) prevalences \n");
9980: fprintf(ficresvbl,"# Age ");
9981: if(nresult >=1)
9982: fprintf(ficresvbl," Result# ");
9983: for(i=1; i<=nlstate;i++)
9984: fprintf(ficresvbl," %1d-%1d",i,i);
9985: fprintf(ficresvbl,"\n");
9986:
9987: xp=vector(1,npar);
9988: dnewmpar=matrix(1,nlstate,1,npar);
9989: doldm=matrix(1,nlstate,1,nlstate);
9990:
9991: hstepm=1*YEARM; /* Every year of age */
9992: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
9993: agelim = AGEINF;
9994: for (age=fage; age>=bage; age --){ /* If stepm=6 months */
9995: nhstepm=(int) rint((age-agelim)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
9996: if (stepm >= YEARM) hstepm=1;
9997: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
9998: gradg=matrix(1,npar,1,nlstate);
9999: mgp=matrix(1,npar,1,nlstate);
10000: mgm=matrix(1,npar,1,nlstate);
10001: gp=vector(1,nlstate);
10002: gm=vector(1,nlstate);
10003:
10004: for(theta=1; theta <=npar; theta++){
10005: for(i=1; i<=npar; i++){ /* Computes gradient */
10006: xp[i] = x[i] + (i==theta ?delti[theta]:0);
10007: }
10008: if(mobilavproj > 0 )
10009: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
10010: else
10011: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
10012: for(i=1;i<=nlstate;i++){
10013: gp[i] = bprlim[i][i];
10014: mgp[theta][i] = bprlim[i][i];
10015: }
10016: for(i=1; i<=npar; i++) /* Computes gradient */
10017: xp[i] = x[i] - (i==theta ?delti[theta]:0);
10018: if(mobilavproj > 0 )
10019: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
10020: else
10021: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
10022: for(i=1;i<=nlstate;i++){
10023: gm[i] = bprlim[i][i];
10024: mgm[theta][i] = bprlim[i][i];
10025: }
10026: for(i=1;i<=nlstate;i++)
10027: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
10028: /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */
10029: } /* End theta */
10030:
10031: trgradg =matrix(1,nlstate,1,npar);
10032:
10033: for(j=1; j<=nlstate;j++)
10034: for(theta=1; theta <=npar; theta++)
10035: trgradg[j][theta]=gradg[theta][j];
10036: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
10037: /* printf("\nmgm mgp %d ",(int)age); */
10038: /* for(j=1; j<=nlstate;j++){ */
10039: /* printf(" %d ",j); */
10040: /* for(theta=1; theta <=npar; theta++) */
10041: /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */
10042: /* printf("\n "); */
10043: /* } */
10044: /* } */
10045: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
10046: /* printf("\n gradg %d ",(int)age); */
10047: /* for(j=1; j<=nlstate;j++){ */
10048: /* printf("%d ",j); */
10049: /* for(theta=1; theta <=npar; theta++) */
10050: /* printf("%d %lf ",theta,gradg[theta][j]); */
10051: /* printf("\n "); */
10052: /* } */
10053: /* } */
10054:
10055: for(i=1;i<=nlstate;i++)
10056: varbpl[i][(int)age] =0.;
10057: if((int)age==79 ||(int)age== 80 ||(int)age== 81){
10058: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
10059: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
10060: }else{
10061: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
10062: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
10063: }
10064: for(i=1;i<=nlstate;i++)
10065: varbpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
10066:
10067: fprintf(ficresvbl,"%.0f ",age );
10068: if(nresult >=1)
10069: fprintf(ficresvbl,"%d ",nres );
10070: for(i=1; i<=nlstate;i++)
10071: fprintf(ficresvbl," %.5f (%.5f)",bprlim[i][i],sqrt(varbpl[i][(int)age]));
10072: fprintf(ficresvbl,"\n");
10073: free_vector(gp,1,nlstate);
10074: free_vector(gm,1,nlstate);
10075: free_matrix(mgm,1,npar,1,nlstate);
10076: free_matrix(mgp,1,npar,1,nlstate);
10077: free_matrix(gradg,1,npar,1,nlstate);
10078: free_matrix(trgradg,1,nlstate,1,npar);
10079: } /* End age */
10080:
10081: free_vector(xp,1,npar);
10082: free_matrix(doldm,1,nlstate,1,npar);
10083: free_matrix(dnewmpar,1,nlstate,1,nlstate);
10084:
10085: }
10086:
10087: /************ Variance of one-step probabilities ******************/
10088: 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[])
10089: {
10090: int i, j=0, k1, l1, tj;
10091: int k2, l2, j1, z1;
10092: int k=0, l;
10093: int first=1, first1, first2;
10094: int nres=0; /* New */
10095: double cv12, mu1, mu2, lc1, lc2, v12, v21, v11, v22,v1,v2, c12, tnalp;
10096: double **dnewm,**doldm;
10097: double *xp;
10098: double *gp, *gm;
10099: double **gradg, **trgradg;
10100: double **mu;
10101: double age, cov[NCOVMAX+1];
10102: double std=2.0; /* Number of standard deviation wide of confidence ellipsoids */
10103: int theta;
10104: char fileresprob[FILENAMELENGTH];
10105: char fileresprobcov[FILENAMELENGTH];
10106: char fileresprobcor[FILENAMELENGTH];
10107: double ***varpij;
10108:
10109: strcpy(fileresprob,"PROB_");
10110: strcat(fileresprob,fileres);
10111: if((ficresprob=fopen(fileresprob,"w"))==NULL) {
10112: printf("Problem with resultfile: %s\n", fileresprob);
10113: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprob);
10114: }
10115: strcpy(fileresprobcov,"PROBCOV_");
10116: strcat(fileresprobcov,fileresu);
10117: if((ficresprobcov=fopen(fileresprobcov,"w"))==NULL) {
10118: printf("Problem with resultfile: %s\n", fileresprobcov);
10119: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcov);
10120: }
10121: strcpy(fileresprobcor,"PROBCOR_");
10122: strcat(fileresprobcor,fileresu);
10123: if((ficresprobcor=fopen(fileresprobcor,"w"))==NULL) {
10124: printf("Problem with resultfile: %s\n", fileresprobcor);
10125: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcor);
10126: }
10127: printf("Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
10128: fprintf(ficlog,"Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
10129: printf("Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
10130: fprintf(ficlog,"Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
10131: printf("and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
10132: fprintf(ficlog,"and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
10133: pstamp(ficresprob);
10134: fprintf(ficresprob,"#One-step probabilities and stand. devi in ()\n");
10135: fprintf(ficresprob,"# Age");
10136: pstamp(ficresprobcov);
10137: fprintf(ficresprobcov,"#One-step probabilities and covariance matrix\n");
10138: fprintf(ficresprobcov,"# Age");
10139: pstamp(ficresprobcor);
10140: fprintf(ficresprobcor,"#One-step probabilities and correlation matrix\n");
10141: fprintf(ficresprobcor,"# Age");
10142:
10143:
10144: for(i=1; i<=nlstate;i++)
10145: for(j=1; j<=(nlstate+ndeath);j++){
10146: fprintf(ficresprob," p%1d-%1d (SE)",i,j);
10147: fprintf(ficresprobcov," p%1d-%1d ",i,j);
10148: fprintf(ficresprobcor," p%1d-%1d ",i,j);
10149: }
10150: /* fprintf(ficresprob,"\n");
10151: fprintf(ficresprobcov,"\n");
10152: fprintf(ficresprobcor,"\n");
10153: */
10154: xp=vector(1,npar);
10155: dnewm=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
10156: doldm=matrix(1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
10157: mu=matrix(1,(nlstate)*(nlstate+ndeath), (int) bage, (int)fage);
10158: varpij=ma3x(1,nlstate*(nlstate+ndeath),1,nlstate*(nlstate+ndeath),(int) bage, (int) fage);
10159: first=1;
10160: fprintf(ficgp,"\n# Routine varprob");
10161: fprintf(fichtm,"\n<li><h4> Computing and drawing one step probabilities with their confidence intervals</h4></li>\n");
10162: fprintf(fichtm,"\n");
10163:
10164: 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);
10165: 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);
10166: fprintf(fichtmcov,"\nEllipsoids of confidence centered on point (p<inf>ij</inf>, p<inf>kl</inf>) are estimated \
10167: and drawn. It helps understanding how is the covariance between two incidences.\
10168: They are expressed in year<sup>-1</sup> in order to be less dependent of stepm.<br>\n");
10169: 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. \
10170: It can be understood this way: if pij and pkl where uncorrelated the (2x2) matrix of covariance \
10171: would have been (1/(var pij), 0 , 0, 1/(var pkl)), and the confidence interval would be 2 \
10172: standard deviations wide on each axis. <br>\
10173: Now, if both incidences are correlated (usual case) we diagonalised the inverse of the covariance matrix\
10174: and made the appropriate rotation to look at the uncorrelated principal directions.<br>\
10175: To be simple, these graphs help to understand the significativity of each parameter in relation to a second other one.<br> \n");
10176:
10177: cov[1]=1;
10178: /* tj=cptcoveff; */
10179: tj = (int) pow(2,cptcoveff);
10180: if (cptcovn<1) {tj=1;ncodemax[1]=1;}
10181: j1=0;
10182:
10183: for(nres=1;nres <=nresult; nres++){ /* For each resultline */
10184: for(j1=1; j1<=tj;j1++){ /* For any combination of dummy covariates, fixed and varying */
10185: /* 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); */
10186: if(tj != 1 && TKresult[nres]!= j1)
10187: continue;
10188:
10189: /* for(j1=1; j1<=tj;j1++){ /\* For each valid combination of covariates or only once*\/ */
10190: /* for(nres=1;nres <=1; nres++){ /\* For each resultline *\/ */
10191: /* /\* for(nres=1;nres <=nresult; nres++){ /\\* For each resultline *\\/ *\/ */
10192: if (cptcovn>0) {
10193: fprintf(ficresprob, "\n#********** Variable ");
10194: fprintf(ficresprobcov, "\n#********** Variable ");
10195: fprintf(ficgp, "\n#********** Variable ");
10196: fprintf(fichtmcov, "\n<hr size=\"2\" color=\"#EC5E5E\">********** Variable ");
10197: fprintf(ficresprobcor, "\n#********** Variable ");
10198:
10199: /* Including quantitative variables of the resultline to be done */
10200: for (z1=1; z1<=cptcovs; z1++){ /* Loop on each variable of this resultline */
10201: /* printf("Varprob modelresult[%d][%d]=%d model=1+age+%s \n",nres, z1, modelresult[nres][z1], model); */
10202: fprintf(ficlog,"Varprob modelresult[%d][%d]=%d model=1+age+%s \n",nres, z1, modelresult[nres][z1], model);
10203: /* fprintf(ficlog,"Varprob modelresult[%d][%d]=%d model=1+age+%s resultline[%d]=%s \n",nres, z1, modelresult[nres][z1], model, nres, resultline[nres]); */
10204: if(Dummy[modelresult[nres][z1]]==0){/* Dummy variable of the variable in position modelresult in the model corresponding to z1 in resultline */
10205: if(Fixed[modelresult[nres][z1]]==0){ /* Fixed referenced to model equation */
10206: 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 */
10207: 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 */
10208: 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 */
10209: 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 */
10210: 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 */
10211: fprintf(ficresprob,"fixed ");
10212: fprintf(ficresprobcov,"fixed ");
10213: fprintf(ficgp,"fixed ");
10214: fprintf(fichtmcov,"fixed ");
10215: fprintf(ficresprobcor,"fixed ");
10216: }else{
10217: fprintf(ficresprob,"varyi ");
10218: fprintf(ficresprobcov,"varyi ");
10219: fprintf(ficgp,"varyi ");
10220: fprintf(fichtmcov,"varyi ");
10221: fprintf(ficresprobcor,"varyi ");
10222: }
10223: }else if(Dummy[modelresult[nres][z1]]==1){ /* Quanti variable */
10224: /* For each selected (single) quantitative value */
10225: fprintf(ficresprob," V%d=%lg ",Tvqresult[nres][z1],Tqresult[nres][z1]);
10226: if(Fixed[modelresult[nres][z1]]==0){ /* Fixed */
10227: fprintf(ficresprob,"fixed ");
10228: fprintf(ficresprobcov,"fixed ");
10229: fprintf(ficgp,"fixed ");
10230: fprintf(fichtmcov,"fixed ");
10231: fprintf(ficresprobcor,"fixed ");
10232: }else{
10233: fprintf(ficresprob,"varyi ");
10234: fprintf(ficresprobcov,"varyi ");
10235: fprintf(ficgp,"varyi ");
10236: fprintf(fichtmcov,"varyi ");
10237: fprintf(ficresprobcor,"varyi ");
10238: }
10239: }else{
10240: 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 */
10241: 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 */
10242: exit(1);
10243: }
10244: } /* End loop on variable of this resultline */
10245: /* for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprob, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]); */
10246: fprintf(ficresprob, "**********\n#\n");
10247: fprintf(ficresprobcov, "**********\n#\n");
10248: fprintf(ficgp, "**********\n#\n");
10249: fprintf(fichtmcov, "**********\n<hr size=\"2\" color=\"#EC5E5E\">");
10250: fprintf(ficresprobcor, "**********\n#");
10251: if(invalidvarcomb[j1]){
10252: fprintf(ficgp,"\n#Combination (%d) ignored because no cases \n",j1);
10253: fprintf(fichtmcov,"\n<h3>Combination (%d) ignored because no cases </h3>\n",j1);
10254: continue;
10255: }
10256: }
10257: gradg=matrix(1,npar,1,(nlstate)*(nlstate+ndeath));
10258: trgradg=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
10259: gp=vector(1,(nlstate)*(nlstate+ndeath));
10260: gm=vector(1,(nlstate)*(nlstate+ndeath));
10261: for (age=bage; age<=fage; age ++){ /* Fo each age we feed the model equation with covariates, using precov as in hpxij() ? */
10262: cov[2]=age;
10263: if(nagesqr==1)
10264: cov[3]= age*age;
10265: /* New code end of combination but for each resultline */
10266: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
10267: if(Typevar[k1]==1 || Typevar[k1] ==3){ /* A product with age */
10268: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
10269: }else{
10270: cov[2+nagesqr+k1]=precov[nres][k1];
10271: }
10272: }/* End of loop on model equation */
10273: /* Old code */
10274: /* /\* for (k=1; k<=cptcovn;k++) { *\/ */
10275: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,k)]; *\/ */
10276: /* for (k=1; k<=nsd;k++) { /\* For single dummy covariates only *\/ */
10277: /* /\* Here comes the value of the covariate 'j1' after renumbering k with single dummy covariates *\/ */
10278: /* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(j1,TnsdVar[TvarsD[k]])]; */
10279: /* /\*cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,Tvar[k])];*\//\* j1 1 2 3 4 */
10280: /* * 1 1 1 1 1 */
10281: /* * 2 2 1 1 1 */
10282: /* * 3 1 2 1 1 */
10283: /* *\/ */
10284: /* /\* nbcode[1][1]=0 nbcode[1][2]=1;*\/ */
10285: /* } */
10286: /* /\* V2+V1+V4+V3*age Tvar[4]=3 ; V1+V2*age Tvar[2]=2; V1+V1*age Tvar[2]=1, Tage[1]=2 *\/ */
10287: /* /\* ) p nbcode[Tvar[Tage[k]]][(1 & (ij-1) >> (k-1))+1] *\/ */
10288: /* /\*for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; *\/ */
10289: /* for (k=1; k<=cptcovage;k++){ /\* For product with age *\/ */
10290: /* if(Dummy[Tage[k]]==2){ /\* dummy with age *\/ */
10291: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(j1,TnsdVar[Tvar[Tage[k]]])]*cov[2]; */
10292: /* /\* cov[++k1]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; *\/ */
10293: /* } else if(Dummy[Tage[k]]==3){ /\* quantitative with age *\/ */
10294: /* 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]); */
10295: /* /\* cov[2+nagesqr+Tage[k]]=meanq[k]/idq[k]*cov[2];/\\* Using the mean of quantitative variable Tvar[Tage[k]] /\\* Tqresult[nres][k]; *\\/ *\/ */
10296: /* /\* exit(1); *\/ */
10297: /* /\* cov[++k1]=Tqresult[nres][k]; *\/ */
10298: /* } */
10299: /* /\* cov[2+Tage[k]+nagesqr]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; *\/ */
10300: /* } */
10301: /* for (k=1; k<=cptcovprod;k++){/\* For product without age *\/ */
10302: /* if(Dummy[Tvard[k][1]]==0){ */
10303: /* if(Dummy[Tvard[k][2]]==0){ */
10304: /* 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]])]; */
10305: /* /\* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
10306: /* }else{ /\* Should we use the mean of the quantitative variables? *\/ */
10307: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(j1,TnsdVar[Tvard[k][1]])] * Tqresult[nres][resultmodel[nres][k]]; */
10308: /* /\* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * Tqresult[nres][k]; *\/ */
10309: /* } */
10310: /* }else{ */
10311: /* if(Dummy[Tvard[k][2]]==0){ */
10312: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(j1,TnsdVar[Tvard[k][2]])] * Tqinvresult[nres][TnsdVar[Tvard[k][1]]]; */
10313: /* /\* cov[++k1]=nbcode[Tvard[k][2]][codtabm(ij,k)] * Tqinvresult[nres][Tvard[k][1]]; *\/ */
10314: /* }else{ */
10315: /* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][TnsdVar[Tvard[k][1]]]* Tqinvresult[nres][TnsdVar[Tvard[k][2]]]; */
10316: /* /\* cov[++k1]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; *\/ */
10317: /* } */
10318: /* } */
10319: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
10320: /* } */
10321: /* For each age and combination of dummy covariates we slightly move the parameters of delti in order to get the gradient*/
10322: for(theta=1; theta <=npar; theta++){
10323: for(i=1; i<=npar; i++)
10324: xp[i] = x[i] + (i==theta ?delti[theta]:(double)0);
10325:
10326: pmij(pmmij,cov,ncovmodel,xp,nlstate);
10327:
10328: k=0;
10329: for(i=1; i<= (nlstate); i++){
10330: for(j=1; j<=(nlstate+ndeath);j++){
10331: k=k+1;
10332: gp[k]=pmmij[i][j];
10333: }
10334: }
10335:
10336: for(i=1; i<=npar; i++)
10337: xp[i] = x[i] - (i==theta ?delti[theta]:(double)0);
10338:
10339: pmij(pmmij,cov,ncovmodel,xp,nlstate);
10340: k=0;
10341: for(i=1; i<=(nlstate); i++){
10342: for(j=1; j<=(nlstate+ndeath);j++){
10343: k=k+1;
10344: gm[k]=pmmij[i][j];
10345: }
10346: }
10347:
10348: for(i=1; i<= (nlstate)*(nlstate+ndeath); i++)
10349: gradg[theta][i]=(gp[i]-gm[i])/(double)2./delti[theta];
10350: }
10351:
10352: for(j=1; j<=(nlstate)*(nlstate+ndeath);j++)
10353: for(theta=1; theta <=npar; theta++)
10354: trgradg[j][theta]=gradg[theta][j];
10355:
10356: matprod2(dnewm,trgradg,1,(nlstate)*(nlstate+ndeath),1,npar,1,npar,matcov);
10357: matprod2(doldm,dnewm,1,(nlstate)*(nlstate+ndeath),1,npar,1,(nlstate)*(nlstate+ndeath),gradg);
10358:
10359: pmij(pmmij,cov,ncovmodel,x,nlstate);
10360:
10361: k=0;
10362: for(i=1; i<=(nlstate); i++){
10363: for(j=1; j<=(nlstate+ndeath);j++){
10364: k=k+1;
10365: mu[k][(int) age]=pmmij[i][j];
10366: }
10367: }
10368: for(i=1;i<=(nlstate)*(nlstate+ndeath);i++)
10369: for(j=1;j<=(nlstate)*(nlstate+ndeath);j++)
10370: varpij[i][j][(int)age] = doldm[i][j];
10371:
10372: /*printf("\n%d ",(int)age);
10373: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
10374: printf("%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
10375: fprintf(ficlog,"%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
10376: }*/
10377:
10378: fprintf(ficresprob,"\n%d ",(int)age);
10379: fprintf(ficresprobcov,"\n%d ",(int)age);
10380: fprintf(ficresprobcor,"\n%d ",(int)age);
10381:
10382: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++)
10383: fprintf(ficresprob,"%11.3e (%11.3e) ",mu[i][(int) age],sqrt(varpij[i][i][(int)age]));
10384: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
10385: fprintf(ficresprobcov,"%11.3e ",mu[i][(int) age]);
10386: fprintf(ficresprobcor,"%11.3e ",mu[i][(int) age]);
10387: }
10388: i=0;
10389: for (k=1; k<=(nlstate);k++){
10390: for (l=1; l<=(nlstate+ndeath);l++){
10391: i++;
10392: fprintf(ficresprobcov,"\n%d %d-%d",(int)age,k,l);
10393: fprintf(ficresprobcor,"\n%d %d-%d",(int)age,k,l);
10394: for (j=1; j<=i;j++){
10395: /* printf(" k=%d l=%d i=%d j=%d\n",k,l,i,j);fflush(stdout); */
10396: fprintf(ficresprobcov," %11.3e",varpij[i][j][(int)age]);
10397: fprintf(ficresprobcor," %11.3e",varpij[i][j][(int) age]/sqrt(varpij[i][i][(int) age])/sqrt(varpij[j][j][(int)age]));
10398: }
10399: }
10400: }/* end of loop for state */
10401: } /* end of loop for age */
10402: free_vector(gp,1,(nlstate+ndeath)*(nlstate+ndeath));
10403: free_vector(gm,1,(nlstate+ndeath)*(nlstate+ndeath));
10404: free_matrix(trgradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
10405: free_matrix(gradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
10406:
10407: /* Confidence intervalle of pij */
10408: /*
10409: fprintf(ficgp,"\nunset parametric;unset label");
10410: fprintf(ficgp,"\nset log y;unset log x; set xlabel \"Age\";set ylabel \"probability (year-1)\"");
10411: fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65");
10412: 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);
10413: fprintf(fichtm,"\n<br><img src=\"pijgr%s.png\"> ",optionfilefiname);
10414: fprintf(ficgp,"\nset out \"pijgr%s.png\"",optionfilefiname);
10415: fprintf(ficgp,"\nplot \"%s\" every :::%d::%d u 1:2 \"\%%lf",k1,k2,xfilevarprob);
10416: */
10417:
10418: /* Drawing ellipsoids of confidence of two variables p(k1-l1,k2-l2)*/
10419: first1=1;first2=2;
10420: for (k2=1; k2<=(nlstate);k2++){
10421: for (l2=1; l2<=(nlstate+ndeath);l2++){
10422: if(l2==k2) continue;
10423: j=(k2-1)*(nlstate+ndeath)+l2;
10424: for (k1=1; k1<=(nlstate);k1++){
10425: for (l1=1; l1<=(nlstate+ndeath);l1++){
10426: if(l1==k1) continue;
10427: i=(k1-1)*(nlstate+ndeath)+l1;
10428: if(i<=j) continue;
10429: for (age=bage; age<=fage; age ++){
10430: if ((int)age %5==0){
10431: v1=varpij[i][i][(int)age]/stepm*YEARM/stepm*YEARM;
10432: v2=varpij[j][j][(int)age]/stepm*YEARM/stepm*YEARM;
10433: cv12=varpij[i][j][(int)age]/stepm*YEARM/stepm*YEARM;
10434: mu1=mu[i][(int) age]/stepm*YEARM ;
10435: mu2=mu[j][(int) age]/stepm*YEARM;
10436: c12=cv12/sqrt(v1*v2);
10437: /* Computing eigen value of matrix of covariance */
10438: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
10439: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
10440: if ((lc2 <0) || (lc1 <0) ){
10441: if(first2==1){
10442: first1=0;
10443: 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);
10444: }
10445: 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);
10446: /* lc1=fabs(lc1); */ /* If we want to have them positive */
10447: /* lc2=fabs(lc2); */
10448: }
10449:
10450: /* Eigen vectors */
10451: if(1+(v1-lc1)*(v1-lc1)/cv12/cv12 <1.e-5){
10452: printf(" Error sqrt of a negative number: %lf\n",1+(v1-lc1)*(v1-lc1)/cv12/cv12);
10453: fprintf(ficlog," Error sqrt of a negative number: %lf\n",1+(v1-lc1)*(v1-lc1)/cv12/cv12);
10454: v11=(1./sqrt(fabs(1+(v1-lc1)*(v1-lc1)/cv12/cv12)));
10455: }else
10456: v11=(1./sqrt(1+(v1-lc1)*(v1-lc1)/cv12/cv12));
10457: /*v21=sqrt(1.-v11*v11); *//* error */
10458: v21=(lc1-v1)/cv12*v11;
10459: v12=-v21;
10460: v22=v11;
10461: tnalp=v21/v11;
10462: if(first1==1){
10463: first1=0;
10464: 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);
10465: }
10466: 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);
10467: /*printf(fignu*/
10468: /* mu1+ v11*lc1*cost + v12*lc2*sin(t) */
10469: /* mu2+ v21*lc1*cost + v22*lc2*sin(t) */
10470: if(first==1){
10471: first=0;
10472: fprintf(ficgp,"\n# Ellipsoids of confidence\n#\n");
10473: fprintf(ficgp,"\nset parametric;unset label");
10474: 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);
10475: fprintf(ficgp,"\nset ter svg size 640, 480");
10476: fprintf(fichtmcov,"\n<p><br>Ellipsoids of confidence cov(p%1d%1d,p%1d%1d) expressed in year<sup>-1</sup>\
10477: :<a href=\"%s_%d%1d%1d-%1d%1d.svg\"> \
10478: %s_%d%1d%1d-%1d%1d.svg</A>, ",k1,l1,k2,l2,\
10479: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2, \
10480: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
10481: fprintf(fichtmcov,"\n<br><img src=\"%s_%d%1d%1d-%1d%1d.svg\"> ",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
10482: fprintf(fichtmcov,"\n<br> Correlation at age %d (%.3f),",(int) age, c12);
10483: fprintf(ficgp,"\nset out \"%s_%d%1d%1d-%1d%1d.svg\"",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
10484: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
10485: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
10486: 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", \
10487: mu1,std,v11,sqrt(fabs(lc1)),v12,sqrt(fabs(lc2)), \
10488: mu2,std,v21,sqrt(fabs(lc1)),v22,sqrt(fabs(lc2))); /* For gnuplot only */
10489: }else{
10490: first=0;
10491: fprintf(fichtmcov," %d (%.3f),",(int) age, c12);
10492: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
10493: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
10494: 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", \
10495: mu1,std,v11,sqrt(lc1),v12,sqrt(fabs(lc2)), \
10496: mu2,std,v21,sqrt(lc1),v22,sqrt(fabs(lc2)));
10497: }/* if first */
10498: } /* age mod 5 */
10499: } /* end loop age */
10500: fprintf(ficgp,"\nset out;\nset out \"%s_%d%1d%1d-%1d%1d.svg\";replot;set out;",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
10501: first=1;
10502: } /*l12 */
10503: } /* k12 */
10504: } /*l1 */
10505: }/* k1 */
10506: } /* loop on combination of covariates j1 */
10507: } /* loop on nres */
10508: free_ma3x(varpij,1,nlstate,1,nlstate+ndeath,(int) bage, (int)fage);
10509: free_matrix(mu,1,(nlstate+ndeath)*(nlstate+ndeath),(int) bage, (int)fage);
10510: free_matrix(doldm,1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
10511: free_matrix(dnewm,1,(nlstate)*(nlstate+ndeath),1,npar);
10512: free_vector(xp,1,npar);
10513: fclose(ficresprob);
10514: fclose(ficresprobcov);
10515: fclose(ficresprobcor);
10516: fflush(ficgp);
10517: fflush(fichtmcov);
10518: }
10519:
10520:
10521: /******************* Printing html file ***********/
10522: void printinghtml(char fileresu[], char title[], char datafile[], int firstpass, \
10523: int lastpass, int stepm, int weightopt, char model[],\
10524: int imx,int jmin, int jmax, double jmeanint,char rfileres[],\
10525: int popforecast, int mobilav, int prevfcast, int mobilavproj, int prevbcast, int estepm , \
10526: double jprev1, double mprev1,double anprev1, double dateprev1, double dateprojd, double dateback1, \
10527: double jprev2, double mprev2,double anprev2, double dateprev2, double dateprojf, double dateback2){
10528: int jj1, k1, i1, cpt, k4, nres;
10529: /* In fact some results are already printed in fichtm which is open */
10530: fprintf(fichtm,"<ul><li><a href='#firstorder'>Result files (first order: no variance)</a>\n \
10531: <li><a href='#secondorder'>Result files (second order (variance)</a>\n \
10532: </ul>");
10533: /* fprintf(fichtm,"<ul><li> model=1+age+%s\n \ */
10534: /* </ul>", model); */
10535: fprintf(fichtm,"<ul><li><h4><a name='firstorder'>Result files (first order: no variance)</a></h4>\n");
10536: 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",
10537: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTMFR_",".htm"),subdirfext3(optionfilefiname,"PHTMFR_",".htm"));
10538: 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) ",
10539: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTM_",".htm"),subdirfext3(optionfilefiname,"PHTM_",".htm"));
10540: fprintf(fichtm,", <a href=\"%s\">%s</a> (text file) <br>\n",subdirf2(fileresu,"P_"),subdirf2(fileresu,"P_"));
10541: fprintf(fichtm,"\
10542: - Estimated transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
10543: stepm,subdirf2(fileresu,"PIJ_"),subdirf2(fileresu,"PIJ_"));
10544: fprintf(fichtm,"\
10545: - Estimated back transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
10546: stepm,subdirf2(fileresu,"PIJB_"),subdirf2(fileresu,"PIJB_"));
10547: fprintf(fichtm,"\
10548: - Period (forward) prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
10549: subdirf2(fileresu,"PL_"),subdirf2(fileresu,"PL_"));
10550: fprintf(fichtm,"\
10551: - Backward prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
10552: subdirf2(fileresu,"PLB_"),subdirf2(fileresu,"PLB_"));
10553: fprintf(fichtm,"\
10554: - (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): \
10555: <a href=\"%s\">%s</a> <br>\n",
10556: estepm,subdirf2(fileresu,"E_"),subdirf2(fileresu,"E_"));
10557: if(prevfcast==1){
10558: fprintf(fichtm,"\
10559: - Prevalence projections by age and states: \
10560: <a href=\"%s\">%s</a> <br>\n</li>", subdirf2(fileresu,"F_"),subdirf2(fileresu,"F_"));
10561: }
10562:
10563:
10564: m=pow(2,cptcoveff);
10565: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
10566:
10567: fprintf(fichtm," \n<ul><li><b>Graphs (first order)</b></li><p>");
10568:
10569: jj1=0;
10570:
10571: fprintf(fichtm," \n<ul>");
10572: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
10573: /* k1=nres; */
10574: k1=TKresult[nres];
10575: if(TKresult[nres]==0)k1=1; /* To be checked for no result */
10576: /* for(k1=1; k1<=m;k1++){ /\* For each combination of covariate *\/ */
10577: /* if(m != 1 && TKresult[nres]!= k1) */
10578: /* continue; */
10579: jj1++;
10580: if (cptcovn > 0) {
10581: fprintf(fichtm,"\n<li><a size=\"1\" color=\"#EC5E5E\" href=\"#rescov");
10582: for (cpt=1; cpt<=cptcovs;cpt++){ /**< cptcovs number of SIMPLE covariates in the model V2+V1 =2 (dummy or quantit or time varying) */
10583: fprintf(fichtm,"_V%d=%lg_",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
10584: }
10585: /* for (cpt=1; cpt<=cptcoveff;cpt++){ */
10586: /* fprintf(fichtm,"_V%d=%d_",Tvresult[nres][cpt],(int)Tresult[nres][cpt]); */
10587: /* } */
10588: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10589: /* fprintf(fichtm,"_V%d=%f_",Tvqresult[nres][k4],Tqresult[nres][k4]); */
10590: /* } */
10591: fprintf(fichtm,"\">");
10592:
10593: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
10594: fprintf(fichtm,"************ Results for covariates");
10595: for (cpt=1; cpt<=cptcovs;cpt++){
10596: fprintf(fichtm," V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
10597: }
10598: /* fprintf(fichtm,"************ Results for covariates"); */
10599: /* for (cpt=1; cpt<=cptcoveff;cpt++){ */
10600: /* fprintf(fichtm," V%d=%d ",Tvresult[nres][cpt],(int)Tresult[nres][cpt]); */
10601: /* } */
10602: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10603: /* fprintf(fichtm," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
10604: /* } */
10605: if(invalidvarcomb[k1]){
10606: fprintf(fichtm," Warning Combination (%d) ignored because no cases ",k1);
10607: continue;
10608: }
10609: fprintf(fichtm,"</a></li>");
10610: } /* cptcovn >0 */
10611: }
10612: fprintf(fichtm," \n</ul>");
10613:
10614: jj1=0;
10615:
10616: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
10617: /* k1=nres; */
10618: k1=TKresult[nres];
10619: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
10620: /* for(k1=1; k1<=m;k1++){ /\* For each combination of covariate *\/ */
10621: /* if(m != 1 && TKresult[nres]!= k1) */
10622: /* continue; */
10623:
10624: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
10625: jj1++;
10626: if (cptcovn > 0) {
10627: fprintf(fichtm,"\n<p><a name=\"rescov");
10628: for (cpt=1; cpt<=cptcovs;cpt++){
10629: fprintf(fichtm,"_V%d=%lg_",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
10630: }
10631: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10632: /* fprintf(fichtm,"_V%d=%f_",Tvqresult[nres][k4],Tqresult[nres][k4]); */
10633: /* } */
10634: fprintf(fichtm,"\"</a>");
10635:
10636: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
10637: for (cpt=1; cpt<=cptcovs;cpt++){
10638: fprintf(fichtm," V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
10639: printf(" V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
10640: /* fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]); */
10641: /* printf(" V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]);fflush(stdout); */
10642: }
10643: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
10644: fprintf(fichtm," (model=1+age+%s) ************\n<hr size=\"2\" color=\"#EC5E5E\">",model);
10645: if(invalidvarcomb[k1]){
10646: fprintf(fichtm,"\n<h3>Combination (%d) ignored because no cases </h3>\n",k1);
10647: printf("\nCombination (%d) ignored because no cases \n",k1);
10648: continue;
10649: }
10650: }
10651: /* aij, bij */
10652: 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> \
10653: <img src=\"%s_%d-1-%d.svg\">",model,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres);
10654: /* Pij */
10655: 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> \
10656: <img src=\"%s_%d-2-%d.svg\">",stepm,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres);
10657: /* Quasi-incidences */
10658: fprintf(fichtm,"<br>\n- I<sub>ij</sub> or Conditional probabilities to be observed in state j being in state i %d (stepm) months\
10659: before but expressed in per year i.e. quasi incidences if stepm is small and probabilities too, \
10660: incidence (rates) are the limit when h tends to zero of the ratio of the probability <sub>h</sub>P<sub>ij</sub> \
10661: 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> \
10662: <img src=\"%s_%d-3-%d.svg\">",stepm,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres);
10663: /* Survival functions (period) in state j */
10664: for(cpt=1; cpt<=nlstate;cpt++){
10665: 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);
10666: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"PIJ_"),subdirf2(optionfilefiname,"PIJ_"));
10667: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres);
10668: }
10669: /* State specific survival functions (period) */
10670: for(cpt=1; cpt<=nlstate;cpt++){
10671: fprintf(fichtm,"<br>\n- Survival functions in state %d and in any other live state (total).\
10672: And probability to be observed in various states (up to %d) being in state %d at different ages. \
10673: <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);
10674: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"PIJ_"),subdirf2(optionfilefiname,"PIJ_"));
10675: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",subdirf2(optionfilefiname,"LIJT_"),cpt,k1,nres);
10676: }
10677: /* Period (forward stable) prevalence in each health state */
10678: for(cpt=1; cpt<=nlstate;cpt++){
10679: 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);
10680: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"PIJ_"),subdirf2(optionfilefiname,"PIJ_"));
10681: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">" ,subdirf2(optionfilefiname,"P_"),cpt,k1,nres);
10682: }
10683: if(prevbcast==1){
10684: /* Backward prevalence in each health state */
10685: for(cpt=1; cpt<=nlstate;cpt++){
10686: 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);
10687: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"PIJB_"),subdirf2(optionfilefiname,"PIJB_"));
10688: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">" ,subdirf2(optionfilefiname,"PB_"),cpt,k1,nres);
10689: }
10690: }
10691: if(prevfcast==1){
10692: /* Projection of prevalence up to period (forward stable) prevalence in each health state */
10693: for(cpt=1; cpt<=nlstate;cpt++){
10694: 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);
10695: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"F_"),subdirf2(optionfilefiname,"F_"));
10696: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",
10697: subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres);
10698: }
10699: }
10700: if(prevbcast==1){
10701: /* Back projection of prevalence up to stable (mixed) back-prevalence in each health state */
10702: for(cpt=1; cpt<=nlstate;cpt++){
10703: fprintf(fichtm,"<br>\n- Back projection of cross-sectional prevalence (estimated with cases observed from %.1f to %.1f and mobil_average=%d), \
10704: 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 \
10705: 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) \
10706: 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);
10707: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"FB_"),subdirf2(optionfilefiname,"FB_"));
10708: fprintf(fichtm," <img src=\"%s_%d-%d-%d.svg\">", subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres);
10709: }
10710: }
10711:
10712: for(cpt=1; cpt<=nlstate;cpt++) {
10713: 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);
10714: fprintf(fichtm," (data from text file <a href=\"%s.txt\"> %s.txt</a>)\n<br>",subdirf2(optionfilefiname,"E_"),subdirf2(optionfilefiname,"E_"));
10715: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">", subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres );
10716: }
10717: /* } /\* end i1 *\/ */
10718: }/* End k1=nres */
10719: fprintf(fichtm,"</ul>");
10720:
10721: fprintf(fichtm,"\
10722: \n<br><li><h4> <a name='secondorder'>Result files (second order: variances)</a></h4>\n\
10723: - Parameter file with estimated parameters and covariance matrix: <a href=\"%s\">%s</a> <br> \
10724: - 95%% confidence intervals and Wald tests of the estimated parameters are in the log file if optimization has been done (mle != 0).<br> \
10725: But because parameters are usually highly correlated (a higher incidence of disability \
10726: and a higher incidence of recovery can give very close observed transition) it might \
10727: be very useful to look not only at linear confidence intervals estimated from the \
10728: variances but at the covariance matrix. And instead of looking at the estimated coefficients \
10729: (parameters) of the logistic regression, it might be more meaningful to visualize the \
10730: covariance matrix of the one-step probabilities. \
10731: See page 'Matrix of variance-covariance of one-step probabilities' below. \n", rfileres,rfileres);
10732:
10733: fprintf(fichtm," - Standard deviation of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
10734: subdirf2(fileresu,"PROB_"),subdirf2(fileresu,"PROB_"));
10735: fprintf(fichtm,"\
10736: - Variance-covariance of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
10737: subdirf2(fileresu,"PROBCOV_"),subdirf2(fileresu,"PROBCOV_"));
10738:
10739: fprintf(fichtm,"\
10740: - Correlation matrix of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
10741: subdirf2(fileresu,"PROBCOR_"),subdirf2(fileresu,"PROBCOR_"));
10742: fprintf(fichtm,"\
10743: - 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): \
10744: <a href=\"%s\">%s</a> <br>\n</li>",
10745: estepm,subdirf2(fileresu,"CVE_"),subdirf2(fileresu,"CVE_"));
10746: fprintf(fichtm,"\
10747: - (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): \
10748: <a href=\"%s\">%s</a> <br>\n</li>",
10749: estepm,subdirf2(fileresu,"STDE_"),subdirf2(fileresu,"STDE_"));
10750: fprintf(fichtm,"\
10751: - 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",
10752: estepm, subdirf2(fileresu,"V_"),subdirf2(fileresu,"V_"));
10753: fprintf(fichtm,"\
10754: - 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",
10755: estepm, subdirf2(fileresu,"T_"),subdirf2(fileresu,"T_"));
10756: fprintf(fichtm,"\
10757: - Standard deviation of forward (period) prevalences: <a href=\"%s\">%s</a> <br>\n",\
10758: subdirf2(fileresu,"VPL_"),subdirf2(fileresu,"VPL_"));
10759:
10760: /* if(popforecast==1) fprintf(fichtm,"\n */
10761: /* - Prevalences forecasting: <a href=\"f%s\">f%s</a> <br>\n */
10762: /* - Population forecasting (if popforecast=1): <a href=\"pop%s\">pop%s</a> <br>\n */
10763: /* <br>",fileres,fileres,fileres,fileres); */
10764: /* else */
10765: /* 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); */
10766: fflush(fichtm);
10767:
10768: m=pow(2,cptcoveff);
10769: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
10770:
10771: fprintf(fichtm," <ul><li><b>Graphs (second order)</b></li><p>");
10772:
10773: jj1=0;
10774:
10775: fprintf(fichtm," \n<ul>");
10776: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
10777: /* k1=nres; */
10778: k1=TKresult[nres];
10779: /* for(k1=1; k1<=m;k1++){ /\* For each combination of covariate *\/ */
10780: /* if(m != 1 && TKresult[nres]!= k1) */
10781: /* continue; */
10782: jj1++;
10783: if (cptcovn > 0) {
10784: fprintf(fichtm,"\n<li><a size=\"1\" color=\"#EC5E5E\" href=\"#rescovsecond");
10785: for (cpt=1; cpt<=cptcovs;cpt++){
10786: fprintf(fichtm,"_V%d=%lg_",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
10787: }
10788: fprintf(fichtm,"\">");
10789:
10790: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
10791: fprintf(fichtm,"************ Results for covariates");
10792: for (cpt=1; cpt<=cptcovs;cpt++){
10793: fprintf(fichtm," V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
10794: }
10795: if(invalidvarcomb[k1]){
10796: fprintf(fichtm," Warning Combination (%d) ignored because no cases ",k1);
10797: continue;
10798: }
10799: fprintf(fichtm,"</a></li>");
10800: } /* cptcovn >0 */
10801: } /* End nres */
10802: fprintf(fichtm," \n</ul>");
10803:
10804: jj1=0;
10805:
10806: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
10807: /* k1=nres; */
10808: k1=TKresult[nres];
10809: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
10810: /* for(k1=1; k1<=m;k1++){ */
10811: /* if(m != 1 && TKresult[nres]!= k1) */
10812: /* continue; */
10813: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
10814: jj1++;
10815: if (cptcovn > 0) {
10816: fprintf(fichtm,"\n<p><a name=\"rescovsecond");
10817: for (cpt=1; cpt<=cptcovs;cpt++){
10818: fprintf(fichtm,"_V%d=%lg_",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
10819: }
10820: fprintf(fichtm,"\"</a>");
10821:
10822: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
10823: for (cpt=1; cpt<=cptcovs;cpt++){ /**< cptcoveff number of variables */
10824: fprintf(fichtm," V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
10825: printf(" V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
10826: /* fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]); */
10827: }
10828:
10829: fprintf(fichtm," (model=1+age+%s) ************\n<hr size=\"2\" color=\"#EC5E5E\">",model);
10830:
10831: if(invalidvarcomb[k1]){
10832: fprintf(fichtm,"\n<h4>Combination (%d) ignored because no cases </h4>\n",k1);
10833: continue;
10834: }
10835: } /* If cptcovn >0 */
10836: for(cpt=1; cpt<=nlstate;cpt++) {
10837: fprintf(fichtm,"\n<br>- Observed (cross-sectional with mov_average=%d) and period (incidence based) \
10838: 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);
10839: fprintf(fichtm," (data from text file <a href=\"%s\">%s</a>)\n <br>",subdirf2(fileresu,"VPL_"),subdirf2(fileresu,"VPL_"));
10840: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",subdirf2(optionfilefiname,"V_"), cpt,k1,nres);
10841: }
10842: fprintf(fichtm,"\n<br>- Total life expectancy by age and \
10843: health expectancies in each live states (1 to %d). If popbased=1 the smooth (due to the model) \
10844: true period expectancies (those weighted with period prevalences are also\
10845: drawn in addition to the population based expectancies computed using\
10846: 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);
10847: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>) \n<br>",subdirf2(optionfilefiname,"T_"),subdirf2(optionfilefiname,"T_"));
10848: fprintf(fichtm,"<img src=\"%s_%d-%d.svg\">",subdirf2(optionfilefiname,"E_"),k1,nres);
10849: /* } /\* end i1 *\/ */
10850: }/* End nres */
10851: fprintf(fichtm,"</ul>");
10852: fflush(fichtm);
10853: }
10854:
10855: /******************* Gnuplot file **************/
10856: 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){
10857:
10858: char dirfileres[256],optfileres[256];
10859: char gplotcondition[256], gplotlabel[256];
10860: 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;
10861: int lv=0, vlv=0, kl=0;
10862: int ng=0;
10863: int vpopbased;
10864: int ioffset; /* variable offset for columns */
10865: int iyearc=1; /* variable column for year of projection */
10866: int iagec=1; /* variable column for age of projection */
10867: int nres=0; /* Index of resultline */
10868: int istart=1; /* For starting graphs in projections */
10869:
10870: /* if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) { */
10871: /* printf("Problem with file %s",optionfilegnuplot); */
10872: /* fprintf(ficlog,"Problem with file %s",optionfilegnuplot); */
10873: /* } */
10874:
10875: /*#ifdef windows */
10876: fprintf(ficgp,"cd \"%s\" \n",pathc);
10877: /*#endif */
10878: m=pow(2,cptcoveff);
10879:
10880: /* diagram of the model */
10881: fprintf(ficgp,"\n#Diagram of the model \n");
10882: fprintf(ficgp,"\ndelta=0.03;delta2=0.07;unset arrow;\n");
10883: fprintf(ficgp,"yoff=(%d > 2? 0:1);\n",nlstate);
10884: 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);
10885:
10886: 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);
10887: fprintf(ficgp,"\n#show arrow\nunset label\n");
10888: 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);
10889: fprintf(ficgp,"\nset label %d+1 sprintf(\"State %%d\",%d+1) center at 0.,0. font \"helvetica, 16\" tc rgbcolor \"red\"\n",nlstate,nlstate);
10890: fprintf(ficgp,"\n#show label\nunset border;unset xtics; unset ytics;\n");
10891: fprintf(ficgp,"\n\nset ter svg size 640, 480;set out \"%s_.svg\" \n",subdirf2(optionfilefiname,"D_"));
10892: fprintf(ficgp,"unset log y; plot [-1.2:1.2][yoff-1.2:1.2] 1/0 not; set out;reset;\n");
10893:
10894: /* Contribution to likelihood */
10895: /* Plot the probability implied in the likelihood */
10896: fprintf(ficgp,"\n# Contributions to the Likelihood, mle >=1. For mle=4 no interpolation, pure matrix products.\n#\n");
10897: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Likelihood (-2Log(L))\";");
10898: /* fprintf(ficgp,"\nset ter svg size 640, 480"); */ /* Too big for svg */
10899: fprintf(ficgp,"\nset ter pngcairo size 640, 480");
10900: /* nice for mle=4 plot by number of matrix products.
10901: replot "rrtest1/toto.txt" u 2:($4 == 1 && $5==2 ? $9 : 1/0):5 t "p12" with point lc 1 */
10902: /* replot exp(p1+p2*x)/(1+exp(p1+p2*x)+exp(p3+p4*x)+exp(p5+p6*x)) t "p12(x)" */
10903: /* fprintf(ficgp,"\nset out \"%s.svg\";",subdirf2(optionfilefiname,"ILK_")); */
10904: fprintf(ficgp,"\nset out \"%s-dest.png\";",subdirf2(optionfilefiname,"ILK_"));
10905: 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));
10906: fprintf(ficgp,"\nset out \"%s-ori.png\";",subdirf2(optionfilefiname,"ILK_"));
10907: 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));
10908: for (i=1; i<= nlstate ; i ++) {
10909: fprintf(ficgp,"\nset out \"%s-p%dj.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i);
10910: fprintf(ficgp,"unset log;\n# plot weighted, mean weight should have point size of 0.5\n plot \"%s\"",subdirf(fileresilk));
10911: 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);
10912: for (j=2; j<= nlstate+ndeath ; j ++) {
10913: 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);
10914: }
10915: fprintf(ficgp,";\nset out; unset ylabel;\n");
10916: }
10917: /* 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 */
10918: /* fprintf(ficgp,"\nset log y;plot \"%s\" u 2:(-$11):3 t \"All sample, all transitions\" with dots lc variable",subdirf(fileresilk)); */
10919: /* fprintf(ficgp,"\nreplot \"%s\" u 2:($3 <= 3 ? -$11 : 1/0):3 t \"First 3 individuals\" with line lc variable", subdirf(fileresilk)); */
10920: fprintf(ficgp,"\nset out;unset log\n");
10921: /* fprintf(ficgp,"\nset out \"%s.svg\"; replot; set out; # bug gnuplot",subdirf2(optionfilefiname,"ILK_")); */
10922:
10923: /* Plot the probability implied in the likelihood by covariate value */
10924: fprintf(ficgp,"\nset ter pngcairo size 640, 480");
10925: /* if(debugILK==1){ */
10926: for(kf=1; kf <= ncovf; kf++){ /* For each simple dummy covariate of the model */
10927: kvar=Tvar[TvarFind[kf]]; /* variable name */
10928: /* k=18+Tvar[TvarFind[kf]];/\*offset because there are 18 columns in the ILK_ file but could be placed else where *\/ */
10929: k=18+kf;/*offset because there are 18 columns in the ILK_ file */
10930: for (i=1; i<= nlstate ; i ++) {
10931: fprintf(ficgp,"\nset out \"%s-p%dj-%d.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i,kvar);
10932: fprintf(ficgp,"unset log;\n# For each simple dummy covariate of the model \n plot \"%s\"",subdirf(fileresilk));
10933: if(gnuplotversion >=5.2){ /* Former gnuplot versions do not have variable pointsize!! */
10934: 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);
10935: for (j=2; j<= nlstate+ndeath ; j ++) {
10936: 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);
10937: }
10938: }else{
10939: 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);
10940: for (j=2; j<= nlstate+ndeath ; j ++) {
10941: 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);
10942: }
10943: }
10944: fprintf(ficgp,";\nset out; unset ylabel;\n");
10945: }
10946: } /* End of each covariate dummy */
10947: for(ncovv=1, iposold=0, kk=0; ncovv <= ncovvt ; ncovv++){
10948: /* Other example V1 + V3 + V5 + age*V1 + age*V3 + age*V5 + V1*V3 + V3*V5 + V1*V5
10949: * kmodel = 1 2 3 4 5 6 7 8 9
10950: * varying 1 2 3 4 5
10951: * ncovv 1 2 3 4 5 6 7 8
10952: * TvarVV[ncovv] V3 5 1 3 3 5 1 5
10953: * TvarVVind[ncovv]=kmodel 2 3 7 7 8 8 9 9
10954: * TvarFind[kmodel] 1 0 0 0 0 0 0 0 0
10955: * kdata ncovcol=[V1 V2] nqv=0 ntv=[V3 V4] nqtv=V5
10956: * Dummy[kmodel] 0 0 1 2 2 3 1 1 1
10957: */
10958: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate */
10959: kvar=TvarVV[ncovv]; /* TvarVV={3, 1, 3} gives the name of each varying covariate */
10960: /* 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]); */
10961: if(ipos!=iposold){ /* Not a product or first of a product */
10962: /* printf(" %d",ipos); */
10963: /* fprintf(ficresilk," V%d",TvarVV[ncovv]); */
10964: /* printf(" DebugILK ficgp suite ipos=%d != iposold=%d\n", ipos, iposold); */
10965: kk++; /* Position of the ncovv column in ILK_ */
10966: k=18+ncovf+kk; /*offset because there are 18 columns in the ILK_ file plus ncovf fixed covariate */
10967: 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) */
10968: for (i=1; i<= nlstate ; i ++) {
10969: fprintf(ficgp,"\nset out \"%s-p%dj-%d.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i,kvar);
10970: fprintf(ficgp,"unset log;\n# For each simple dummy covariate of the model \n plot \"%s\"",subdirf(fileresilk));
10971:
10972: /* printf("Before DebugILK gnuplotversion=%g >=5.2\n",gnuplotversion); */
10973: if(gnuplotversion >=5.2){ /* Former gnuplot versions do not have variable pointsize!! */
10974: /* printf("DebugILK gnuplotversion=%g >=5.2\n",gnuplotversion); */
10975: 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);
10976: for (j=2; j<= nlstate+ndeath ; j ++) {
10977: 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);
10978: }
10979: }else{
10980: /* printf("DebugILK gnuplotversion=%g <5.2\n",gnuplotversion); */
10981: 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);
10982: for (j=2; j<= nlstate+ndeath ; j ++) {
10983: 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);
10984: }
10985: }
10986: fprintf(ficgp,";\nset out; unset ylabel;\n");
10987: }
10988: }/* End if dummy varying */
10989: }else{ /*Product */
10990: /* printf("*"); */
10991: /* fprintf(ficresilk,"*"); */
10992: }
10993: iposold=ipos;
10994: } /* For each time varying covariate */
10995: /* } /\* debugILK==1 *\/ */
10996: /* 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 */
10997: /* fprintf(ficgp,"\nset log y;plot \"%s\" u 2:(-$11):3 t \"All sample, all transitions\" with dots lc variable",subdirf(fileresilk)); */
10998: /* fprintf(ficgp,"\nreplot \"%s\" u 2:($3 <= 3 ? -$11 : 1/0):3 t \"First 3 individuals\" with line lc variable", subdirf(fileresilk)); */
10999: fprintf(ficgp,"\nset out;unset log\n");
11000: /* fprintf(ficgp,"\nset out \"%s.svg\"; replot; set out; # bug gnuplot",subdirf2(optionfilefiname,"ILK_")); */
11001:
11002:
11003:
11004: strcpy(dirfileres,optionfilefiname);
11005: strcpy(optfileres,"vpl");
11006: /* 1eme*/
11007: for (cpt=1; cpt<= nlstate ; cpt ++){ /* For each live state */
11008: /* for (k1=1; k1<= m ; k1 ++){ /\* For each valid combination of covariate *\/ */
11009: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
11010: k1=TKresult[nres];
11011: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
11012: /* plot [100000000000000000000:-100000000000000000000] "mysbiaspar/vplrmysbiaspar.txt to check */
11013: /* if(m != 1 && TKresult[nres]!= k1) */
11014: /* continue; */
11015: /* We are interested in selected combination by the resultline */
11016: /* printf("\n# 1st: Period (stable) prevalence with CI: 'VPL_' files and live state =%d ", cpt); */
11017: fprintf(ficgp,"\n# 1st: Forward (stable period) prevalence with CI: 'VPL_' files and live state =%d ", cpt);
11018: strcpy(gplotlabel,"(");
11019: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
11020: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
11021: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
11022:
11023: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate k get corresponding value lv for combination k1 *\/ */
11024: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the value of the covariate corresponding to k1 combination *\\/ *\/ */
11025: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
11026: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
11027: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
11028: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
11029: /* vlv= nbcode[Tvaraff[k]][lv]; /\* vlv is the value of the covariate lv, 0 or 1 *\/ */
11030: /* /\* For each combination of covariate k1 (V1=1, V3=0), we printed the current covariate k and its value vlv *\/ */
11031: /* /\* printf(" V%d=%d ",Tvaraff[k],vlv); *\/ */
11032: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
11033: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
11034: /* } */
11035: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
11036: /* /\* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); *\/ */
11037: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
11038: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
11039: }
11040: strcpy(gplotlabel+strlen(gplotlabel),")");
11041: /* printf("\n#\n"); */
11042: fprintf(ficgp,"\n#\n");
11043: if(invalidvarcomb[k1]){
11044: /*k1=k1-1;*/ /* To be checked */
11045: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
11046: continue;
11047: }
11048:
11049: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"V_"),cpt,k1,nres);
11050: fprintf(ficgp,"\n#set out \"V_%s_%d-%d-%d.svg\" \n",optionfilefiname,cpt,k1,nres);
11051: /* fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel); */
11052: fprintf(ficgp,"set title \"Alive state %d %s model=1+age+%s\" font \"Helvetica,12\"\n",cpt,gplotlabel,model);
11053: 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);
11054: /* 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); */
11055: /* k1-1 error should be nres-1*/
11056: for (i=1; i<= nlstate ; i ++) {
11057: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
11058: else fprintf(ficgp," %%*lf (%%*lf)");
11059: }
11060: 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);
11061: for (i=1; i<= nlstate ; i ++) {
11062: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
11063: else fprintf(ficgp," %%*lf (%%*lf)");
11064: }
11065: 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);
11066: for (i=1; i<= nlstate ; i ++) {
11067: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
11068: else fprintf(ficgp," %%*lf (%%*lf)");
11069: }
11070: /* 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)); */
11071:
11072: fprintf(ficgp,"\" t\"\" w l lt 1,\"%s\" u 1:((",subdirf2(fileresu,"P_"));
11073: if(cptcoveff ==0){
11074: fprintf(ficgp,"$%d)) t 'Observed prevalence in state %d' with line lt 3", 2+3*(cpt-1), cpt );
11075: }else{
11076: kl=0;
11077: for (k=1; k<=cptcoveff; k++){ /* For each combination of covariate */
11078: /* lv= decodtabm(k1,k,cptcoveff); /\* Should be the covariate value corresponding to k1 combination and kth covariate *\/ */
11079: lv=codtabm(k1,TnsdVar[Tvaraff[k]]);
11080: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
11081: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
11082: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
11083: vlv= nbcode[Tvaraff[k]][lv];
11084: kl++;
11085: /* 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 *\/ */
11086: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
11087: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
11088: /* '' 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*/
11089: if(k==cptcoveff){
11090: 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], \
11091: 2+cptcoveff*2+3*(cpt-1), cpt ); /* 4 or 6 ?*/
11092: }else{
11093: fprintf(ficgp,"$%d==%d && $%d==%d && ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv]);
11094: kl++;
11095: }
11096: } /* end covariate */
11097: } /* end if no covariate */
11098:
11099: if(prevbcast==1){ /* We need to get the corresponding values of the covariates involved in this combination k1 */
11100: /* 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); */
11101: fprintf(ficgp,",\"%s\" u 1:((",subdirf2(fileresu,"PLB_")); /* Age is in 1, nres in 2 to be fixed */
11102: if(cptcoveff ==0){
11103: fprintf(ficgp,"$%d)) t 'Backward prevalence in state %d' with line lt 3", 2+(cpt-1), cpt );
11104: }else{
11105: kl=0;
11106: for (k=1; k<=cptcoveff; k++){ /* For each combination of covariate */
11107: /* lv= decodtabm(k1,k,cptcoveff); /\* Should be the covariate value corresponding to k1 combination and kth covariate *\/ */
11108: lv=codtabm(k1,TnsdVar[Tvaraff[k]]);
11109: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
11110: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
11111: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
11112: /* vlv= nbcode[Tvaraff[k]][lv]; */
11113: vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])];
11114: kl++;
11115: /* 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 *\/ */
11116: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
11117: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
11118: /* '' 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*/
11119: if(k==cptcoveff){
11120: 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], \
11121: 2+cptcoveff*2+(cpt-1), cpt ); /* 4 or 6 ?*/
11122: }else{
11123: fprintf(ficgp,"$%d==%d && $%d==%d && ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]);
11124: kl++;
11125: }
11126: } /* end covariate */
11127: } /* end if no covariate */
11128: if(prevbcast == 1){
11129: fprintf(ficgp,", \"%s\" every :::%d::%d u 1:($2==%d ? $3:1/0) \"%%lf %%lf",subdirf2(fileresu,"VBL_"),nres-1,nres-1,nres);
11130: /* k1-1 error should be nres-1*/
11131: for (i=1; i<= nlstate ; i ++) {
11132: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
11133: else fprintf(ficgp," %%*lf (%%*lf)");
11134: }
11135: 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);
11136: for (i=1; i<= nlstate ; i ++) {
11137: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
11138: else fprintf(ficgp," %%*lf (%%*lf)");
11139: }
11140: 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);
11141: for (i=1; i<= nlstate ; i ++) {
11142: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
11143: else fprintf(ficgp," %%*lf (%%*lf)");
11144: }
11145: fprintf(ficgp,"\" t\"\" w l lt 4");
11146: } /* end if backprojcast */
11147: } /* end if prevbcast */
11148: /* fprintf(ficgp,"\nset out ;unset label;\n"); */
11149: fprintf(ficgp,"\nset out ;unset title;\n");
11150: } /* nres */
11151: /* } /\* k1 *\/ */
11152: } /* cpt */
11153:
11154:
11155: /*2 eme*/
11156: /* for (k1=1; k1<= m ; k1 ++){ */
11157: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
11158: k1=TKresult[nres];
11159: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
11160: /* if(m != 1 && TKresult[nres]!= k1) */
11161: /* continue; */
11162: fprintf(ficgp,"\n# 2nd: Total life expectancy with CI: 't' files ");
11163: strcpy(gplotlabel,"(");
11164: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
11165: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
11166: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
11167: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
11168: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
11169: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
11170: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
11171: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
11172: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
11173: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
11174: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
11175: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
11176: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
11177: /* } */
11178: /* /\* for(k=1; k <= ncovds; k++){ *\/ */
11179: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
11180: /* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
11181: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
11182: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
11183: }
11184: strcpy(gplotlabel+strlen(gplotlabel),")");
11185: fprintf(ficgp,"\n#\n");
11186: if(invalidvarcomb[k1]){
11187: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
11188: continue;
11189: }
11190:
11191: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"E_"),k1,nres);
11192: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
11193: fprintf(ficgp,"\nset label \"popbased %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",vpopbased,gplotlabel);
11194: if(vpopbased==0){
11195: fprintf(ficgp,"set ylabel \"Years\" \nset ter svg size 640, 480\nplot [%.f:%.f] ",ageminpar,fage);
11196: }else
11197: fprintf(ficgp,"\nreplot ");
11198: for (i=1; i<= nlstate+1 ; i ++) {
11199: k=2*i;
11200: 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);
11201: for (j=1; j<= nlstate+1 ; j ++) {
11202: if (j==i) fprintf(ficgp," %%lf (%%lf)");
11203: else fprintf(ficgp," %%*lf (%%*lf)");
11204: }
11205: if (i== 1) fprintf(ficgp,"\" t\"TLE\" w l lt %d, \\\n",i);
11206: else fprintf(ficgp,"\" t\"LE in state (%d)\" w l lt %d, \\\n",i-1,i+1);
11207: 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);
11208: for (j=1; j<= nlstate+1 ; j ++) {
11209: if (j==i) fprintf(ficgp," %%lf (%%lf)");
11210: else fprintf(ficgp," %%*lf (%%*lf)");
11211: }
11212: fprintf(ficgp,"\" t\"\" w l lt 0,");
11213: 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);
11214: for (j=1; j<= nlstate+1 ; j ++) {
11215: if (j==i) fprintf(ficgp," %%lf (%%lf)");
11216: else fprintf(ficgp," %%*lf (%%*lf)");
11217: }
11218: if (i== (nlstate+1)) fprintf(ficgp,"\" t\"\" w l lt 0");
11219: else fprintf(ficgp,"\" t\"\" w l lt 0,\\\n");
11220: } /* state */
11221: } /* vpopbased */
11222: fprintf(ficgp,"\nset out;set out \"%s_%d-%d.svg\"; replot; set out; unset label;\n",subdirf2(optionfilefiname,"E_"),k1,nres); /* Buggy gnuplot */
11223: } /* end nres */
11224: /* } /\* k1 end 2 eme*\/ */
11225:
11226:
11227: /*3eme*/
11228: /* for (k1=1; k1<= m ; k1 ++){ */
11229: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
11230: k1=TKresult[nres];
11231: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
11232: /* if(m != 1 && TKresult[nres]!= k1) */
11233: /* continue; */
11234:
11235: for (cpt=1; cpt<= nlstate ; cpt ++) { /* Fragile no verification of covariate values */
11236: fprintf(ficgp,"\n\n# 3d: Life expectancy with EXP_ files: combination=%d state=%d",k1, cpt);
11237: strcpy(gplotlabel,"(");
11238: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
11239: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
11240: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
11241: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
11242: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
11243: /* lv= codtabm(k1,TnsdVar[Tvaraff[k]]); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
11244: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
11245: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
11246: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
11247: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
11248: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
11249: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
11250: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
11251: /* } */
11252: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
11253: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][resultmodel[nres][k4]]); */
11254: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][resultmodel[nres][k4]]); */
11255: }
11256: strcpy(gplotlabel+strlen(gplotlabel),")");
11257: fprintf(ficgp,"\n#\n");
11258: if(invalidvarcomb[k1]){
11259: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
11260: continue;
11261: }
11262:
11263: /* k=2+nlstate*(2*cpt-2); */
11264: k=2+(nlstate+1)*(cpt-1);
11265: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres);
11266: fprintf(ficgp,"set label \"%s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",gplotlabel);
11267: fprintf(ficgp,"set ter svg size 640, 480\n\
11268: 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);
11269: /*fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d-2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
11270: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
11271: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
11272: fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d+2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
11273: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
11274: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
11275:
11276: */
11277: for (i=1; i< nlstate ; i ++) {
11278: 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);
11279: /* 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);*/
11280:
11281: }
11282: fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d.\" w l",subdirf2(fileresu,"E_"),nres-1,nres-1,k+nlstate,cpt);
11283: }
11284: fprintf(ficgp,"\nunset label;\n");
11285: } /* end nres */
11286: /* } /\* end kl 3eme *\/ */
11287:
11288: /* 4eme */
11289: /* Survival functions (period) from state i in state j by initial state i */
11290: /* for (k1=1; k1<=m; k1++){ /\* For each covariate and each value *\/ */
11291: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
11292: k1=TKresult[nres];
11293: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
11294: /* if(m != 1 && TKresult[nres]!= k1) */
11295: /* continue; */
11296: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state cpt*/
11297: strcpy(gplotlabel,"(");
11298: fprintf(ficgp,"\n#\n#\n# Survival functions in state %d : 'LIJ_' files, cov=%d state=%d", cpt, k1, cpt);
11299: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
11300: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
11301: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
11302: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
11303: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
11304: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
11305: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
11306: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
11307: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
11308: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
11309: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
11310: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
11311: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
11312: /* } */
11313: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
11314: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
11315: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
11316: }
11317: strcpy(gplotlabel+strlen(gplotlabel),")");
11318: fprintf(ficgp,"\n#\n");
11319: if(invalidvarcomb[k1]){
11320: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
11321: continue;
11322: }
11323:
11324: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres);
11325: fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
11326: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
11327: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
11328: k=3;
11329: for (i=1; i<= nlstate ; i ++){
11330: if(i==1){
11331: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
11332: }else{
11333: fprintf(ficgp,", '' ");
11334: }
11335: l=(nlstate+ndeath)*(i-1)+1;
11336: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
11337: for (j=2; j<= nlstate+ndeath ; j ++)
11338: fprintf(ficgp,"+$%d",k+l+j-1);
11339: fprintf(ficgp,")) t \"l(%d,%d)\" w l",i,cpt);
11340: } /* nlstate */
11341: fprintf(ficgp,"\nset out; unset label;\n");
11342: } /* end cpt state*/
11343: } /* end nres */
11344: /* } /\* end covariate k1 *\/ */
11345:
11346: /* 5eme */
11347: /* Survival functions (period) from state i in state j by final state j */
11348: /* for (k1=1; k1<= m ; k1++){ /\* For each covariate combination if any *\/ */
11349: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
11350: k1=TKresult[nres];
11351: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
11352: /* if(m != 1 && TKresult[nres]!= k1) */
11353: /* continue; */
11354: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each inital state */
11355: strcpy(gplotlabel,"(");
11356: 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);
11357: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
11358: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
11359: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
11360: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
11361: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
11362: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
11363: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
11364: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
11365: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
11366: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
11367: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
11368: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
11369: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
11370: /* } */
11371: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
11372: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
11373: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
11374: }
11375: strcpy(gplotlabel+strlen(gplotlabel),")");
11376: fprintf(ficgp,"\n#\n");
11377: if(invalidvarcomb[k1]){
11378: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
11379: continue;
11380: }
11381:
11382: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJT_"),cpt,k1,nres);
11383: fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
11384: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
11385: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
11386: k=3;
11387: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
11388: if(j==1)
11389: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
11390: else
11391: fprintf(ficgp,", '' ");
11392: l=(nlstate+ndeath)*(cpt-1) +j;
11393: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):($%d",k1,k+l);
11394: /* for (i=2; i<= nlstate+ndeath ; i ++) */
11395: /* fprintf(ficgp,"+$%d",k+l+i-1); */
11396: fprintf(ficgp,") t \"l(%d,%d)\" w l",cpt,j);
11397: } /* nlstate */
11398: fprintf(ficgp,", '' ");
11399: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):(",k1);
11400: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
11401: l=(nlstate+ndeath)*(cpt-1) +j;
11402: if(j < nlstate)
11403: fprintf(ficgp,"$%d +",k+l);
11404: else
11405: fprintf(ficgp,"$%d) t\"l(%d,.)\" w l",k+l,cpt);
11406: }
11407: fprintf(ficgp,"\nset out; unset label;\n");
11408: } /* end cpt state*/
11409: /* } /\* end covariate *\/ */
11410: } /* end nres */
11411:
11412: /* 6eme */
11413: /* CV preval stable (period) for each covariate */
11414: /* for (k1=1; k1<= m ; k1 ++) /\* For each covariate combination if any *\/ */
11415: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
11416: k1=TKresult[nres];
11417: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
11418: /* if(m != 1 && TKresult[nres]!= k1) */
11419: /* continue; */
11420: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state of arrival */
11421: strcpy(gplotlabel,"(");
11422: fprintf(ficgp,"\n#\n#\n#CV preval stable (forward): 'pij' files, covariatecombination#=%d state=%d",k1, cpt);
11423: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
11424: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
11425: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
11426: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
11427: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
11428: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
11429: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
11430: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
11431: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
11432: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
11433: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
11434: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
11435: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
11436: /* } */
11437: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
11438: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
11439: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
11440: }
11441: strcpy(gplotlabel+strlen(gplotlabel),")");
11442: fprintf(ficgp,"\n#\n");
11443: if(invalidvarcomb[k1]){
11444: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
11445: continue;
11446: }
11447:
11448: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"P_"),cpt,k1,nres);
11449: fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
11450: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
11451: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
11452: k=3; /* Offset */
11453: for (i=1; i<= nlstate ; i ++){ /* State of origin */
11454: if(i==1)
11455: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
11456: else
11457: fprintf(ficgp,", '' ");
11458: l=(nlstate+ndeath)*(i-1)+1; /* 1, 1+ nlstate+ndeath, 1+2*(nlstate+ndeath) */
11459: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
11460: for (j=2; j<= nlstate ; j ++)
11461: fprintf(ficgp,"+$%d",k+l+j-1);
11462: fprintf(ficgp,")) t \"prev(%d,%d)\" w l",i,cpt);
11463: } /* nlstate */
11464: fprintf(ficgp,"\nset out; unset label;\n");
11465: } /* end cpt state*/
11466: } /* end covariate */
11467:
11468:
11469: /* 7eme */
11470: if(prevbcast == 1){
11471: /* CV backward prevalence for each covariate */
11472: /* for (k1=1; k1<= m ; k1 ++) /\* For each covariate combination if any *\/ */
11473: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
11474: k1=TKresult[nres];
11475: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
11476: /* if(m != 1 && TKresult[nres]!= k1) */
11477: /* continue; */
11478: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life origin state */
11479: strcpy(gplotlabel,"(");
11480: fprintf(ficgp,"\n#\n#\n#CV Backward stable prevalence: 'pijb' files, covariatecombination#=%d state=%d",k1, cpt);
11481: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
11482: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
11483: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
11484: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
11485: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
11486: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
11487: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
11488: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
11489: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
11490: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
11491: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
11492: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
11493: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
11494: /* } */
11495: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
11496: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
11497: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
11498: }
11499: strcpy(gplotlabel+strlen(gplotlabel),")");
11500: fprintf(ficgp,"\n#\n");
11501: if(invalidvarcomb[k1]){
11502: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
11503: continue;
11504: }
11505:
11506: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PB_"),cpt,k1,nres);
11507: fprintf(ficgp,"set label \"Origin alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
11508: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
11509: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
11510: k=3; /* Offset */
11511: for (i=1; i<= nlstate ; i ++){ /* State of arrival */
11512: if(i==1)
11513: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJB_"));
11514: else
11515: fprintf(ficgp,", '' ");
11516: /* l=(nlstate+ndeath)*(i-1)+1; */
11517: l=(nlstate+ndeath)*(cpt-1)+1; /* fixed for i; cpt=1 1, cpt=2 1+ nlstate+ndeath, 1+2*(nlstate+ndeath) */
11518: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l); /\* a vérifier *\/ */
11519: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l+(cpt-1)+i-1); /\* a vérifier *\/ */
11520: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d",k1,k+l+i-1); /* To be verified */
11521: /* for (j=2; j<= nlstate ; j ++) */
11522: /* fprintf(ficgp,"+$%d",k+l+j-1); */
11523: /* /\* fprintf(ficgp,"+$%d",k+l+j-1); *\/ */
11524: fprintf(ficgp,") t \"bprev(%d,%d)\" w l",cpt,i);
11525: } /* nlstate */
11526: fprintf(ficgp,"\nset out; unset label;\n");
11527: } /* end cpt state*/
11528: } /* end covariate */
11529: } /* End if prevbcast */
11530:
11531: /* 8eme */
11532: if(prevfcast==1){
11533: /* Projection from cross-sectional to forward stable (period) prevalence for each covariate */
11534:
11535: /* for (k1=1; k1<= m ; k1 ++) /\* For each covariate combination if any *\/ */
11536: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
11537: k1=TKresult[nres];
11538: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
11539: /* if(m != 1 && TKresult[nres]!= k1) */
11540: /* continue; */
11541: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
11542: strcpy(gplotlabel,"(");
11543: fprintf(ficgp,"\n#\n#\n#Projection of prevalence to forward stable prevalence (period): 'PROJ_' files, covariatecombination#=%d state=%d",k1, cpt);
11544: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
11545: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
11546: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
11547: /* for (k=1; k<=cptcoveff; k++){ /\* For each correspondig covariate value *\/ */
11548: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate value corresponding to k1 combination and kth covariate *\\/ *\/ */
11549: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
11550: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
11551: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
11552: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
11553: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
11554: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
11555: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
11556: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
11557: /* } */
11558: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
11559: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
11560: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
11561: }
11562: strcpy(gplotlabel+strlen(gplotlabel),")");
11563: fprintf(ficgp,"\n#\n");
11564: if(invalidvarcomb[k1]){
11565: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
11566: continue;
11567: }
11568:
11569: fprintf(ficgp,"# hpijx=probability over h years, hp.jx is weighted by observed prev\n ");
11570: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres);
11571: fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
11572: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\
11573: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
11574:
11575: /* for (i=1; i<= nlstate+1 ; i ++){ /\* nlstate +1 p11 p21 p.1 *\/ */
11576: istart=nlstate+1; /* Could be one if by state, but nlstate+1 is w.i projection only */
11577: /*istart=1;*/ /* Could be one if by state, but nlstate+1 is w.i projection only */
11578: for (i=istart; i<= nlstate+1 ; i ++){ /* nlstate +1 p11 p21 p.1 */
11579: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
11580: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
11581: /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
11582: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
11583: if(i==istart){
11584: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"F_"));
11585: }else{
11586: fprintf(ficgp,",\\\n '' ");
11587: }
11588: if(cptcoveff ==0){ /* No covariate */
11589: ioffset=2; /* Age is in 2 */
11590: /*# yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
11591: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
11592: /*# V1 = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
11593: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
11594: fprintf(ficgp," u %d:(", ioffset);
11595: if(i==nlstate+1){
11596: fprintf(ficgp," $%d/(1.-$%d)):1 t 'pw.%d' with line lc variable ", \
11597: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
11598: fprintf(ficgp,",\\\n '' ");
11599: fprintf(ficgp," u %d:(",ioffset);
11600: fprintf(ficgp," (($1-$2) == %d ) ? $%d/(1.-$%d) : 1/0):1 with labels center not ", \
11601: offyear, \
11602: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate );
11603: }else
11604: fprintf(ficgp," $%d/(1.-$%d)) t 'p%d%d' with line ", \
11605: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,i,cpt );
11606: }else{ /* more than 2 covariates */
11607: ioffset=2*cptcoveff+2; /* Age is in 4 or 6 or etc.*/
11608: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
11609: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
11610: iyearc=ioffset-1;
11611: iagec=ioffset;
11612: fprintf(ficgp," u %d:(",ioffset);
11613: kl=0;
11614: strcpy(gplotcondition,"(");
11615: for (k=1; k<=cptcoveff; k++){ /* For each covariate writing the chain of conditions */
11616: /* lv= decodtabm(k1,k,cptcoveff); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
11617: lv=codtabm(k1,TnsdVar[Tvaraff[k]]);
11618: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
11619: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
11620: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
11621: /* vlv= nbcode[Tvaraff[k]][lv]; /\* Value of the modality of Tvaraff[k] *\/ */
11622: vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])];
11623: kl++;
11624: sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,Tvaraff[k], kl+1, nbcode[Tvaraff[k]][lv]);
11625: kl++;
11626: if(k <cptcoveff && cptcoveff>1)
11627: sprintf(gplotcondition+strlen(gplotcondition)," && ");
11628: }
11629: strcpy(gplotcondition+strlen(gplotcondition),")");
11630: /* 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 *\/ */
11631: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
11632: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
11633: /* '' 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*/
11634: if(i==nlstate+1){
11635: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0):%d t 'p.%d' with line lc variable", gplotcondition, \
11636: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,iyearc, cpt );
11637: fprintf(ficgp,",\\\n '' ");
11638: fprintf(ficgp," u %d:(",iagec);
11639: fprintf(ficgp,"%s && (($%d-$%d) == %d ) ? $%d/(1.-$%d) : 1/0):%d with labels center not ", gplotcondition, \
11640: iyearc, iagec, offyear, \
11641: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate, iyearc );
11642: /* '' u 6:(($1==1 && $2==0 && $3==2 && $4==0) && (($5-$6) == 1947) ? $10/(1.-$22) : 1/0):5 with labels center boxed not*/
11643: }else{
11644: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \
11645: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset +1+(i-1)+(nlstate+1)*nlstate,i,cpt );
11646: }
11647: } /* end if covariate */
11648: } /* nlstate */
11649: fprintf(ficgp,"\nset out; unset label;\n");
11650: } /* end cpt state*/
11651: } /* end covariate */
11652: } /* End if prevfcast */
11653:
11654: if(prevbcast==1){
11655: /* Back projection from cross-sectional to stable (mixed) for each covariate */
11656:
11657: /* for (k1=1; k1<= m ; k1 ++) /\* For each covariate combination if any *\/ */
11658: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
11659: k1=TKresult[nres];
11660: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
11661: /* if(m != 1 && TKresult[nres]!= k1) */
11662: /* continue; */
11663: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
11664: strcpy(gplotlabel,"(");
11665: fprintf(ficgp,"\n#\n#\n#Back projection of prevalence to stable (mixed) back prevalence: 'BPROJ_' files, covariatecombination#=%d originstate=%d",k1, cpt);
11666: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
11667: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
11668: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
11669: /* for (k=1; k<=cptcoveff; k++){ /\* For each correspondig covariate value *\/ */
11670: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate value corresponding to k1 combination and kth covariate *\\/ *\/ */
11671: /* lv= codtabm(k1,TnsdVar[Tvaraff[k]]); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
11672: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
11673: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
11674: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
11675: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
11676: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
11677: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
11678: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
11679: /* } */
11680: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
11681: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
11682: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
11683: }
11684: strcpy(gplotlabel+strlen(gplotlabel),")");
11685: fprintf(ficgp,"\n#\n");
11686: if(invalidvarcomb[k1]){
11687: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
11688: continue;
11689: }
11690:
11691: fprintf(ficgp,"# hbijx=backprobability over h years, hb.jx is weighted by observed prev at destination state\n ");
11692: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres);
11693: fprintf(ficgp,"set label \"Origin alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
11694: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\
11695: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
11696:
11697: /* for (i=1; i<= nlstate+1 ; i ++){ /\* nlstate +1 p11 p21 p.1 *\/ */
11698: istart=nlstate+1; /* Could be one if by state, but nlstate+1 is w.i projection only */
11699: /*istart=1;*/ /* Could be one if by state, but nlstate+1 is w.i projection only */
11700: for (i=istart; i<= nlstate+1 ; i ++){ /* nlstate +1 p11 p21 p.1 */
11701: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
11702: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
11703: /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
11704: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
11705: if(i==istart){
11706: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"FB_"));
11707: }else{
11708: fprintf(ficgp,",\\\n '' ");
11709: }
11710: if(cptcoveff ==0){ /* No covariate */
11711: ioffset=2; /* Age is in 2 */
11712: /*# yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
11713: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
11714: /*# V1 = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
11715: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
11716: fprintf(ficgp," u %d:(", ioffset);
11717: if(i==nlstate+1){
11718: fprintf(ficgp," $%d/(1.-$%d)):1 t 'bw%d' with line lc variable ", \
11719: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
11720: fprintf(ficgp,",\\\n '' ");
11721: fprintf(ficgp," u %d:(",ioffset);
11722: fprintf(ficgp," (($1-$2) == %d ) ? $%d : 1/0):1 with labels center not ", \
11723: offbyear, \
11724: ioffset+(cpt-1)*(nlstate+1)+1+(i-1) );
11725: }else
11726: fprintf(ficgp," $%d/(1.-$%d)) t 'b%d%d' with line ", \
11727: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt,i );
11728: }else{ /* more than 2 covariates */
11729: ioffset=2*cptcoveff+2; /* Age is in 4 or 6 or etc.*/
11730: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
11731: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
11732: iyearc=ioffset-1;
11733: iagec=ioffset;
11734: fprintf(ficgp," u %d:(",ioffset); /* PROBLEM HERE VERIFY */
11735: kl=0;
11736: strcpy(gplotcondition,"(");
11737: for (k=1; k<=cptcovs; k++){ /* For each covariate k of the resultline, get corresponding value lv for combination k1 */
11738: if(Dummy[modelresult[nres][k]]==0){ /* To be verified */
11739: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate writing the chain of conditions *\/ */
11740: /* lv= decodtabm(k1,k,cptcoveff); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
11741: /* lv= codtabm(k1,TnsdVar[Tvaraff[k]]); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
11742: lv=Tvresult[nres][k];
11743: vlv=TinvDoQresult[nres][Tvresult[nres][k]];
11744: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
11745: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
11746: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
11747: /* vlv= nbcode[Tvaraff[k]][lv]; /\* Value of the modality of Tvaraff[k] *\/ */
11748: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
11749: kl++;
11750: /* sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,Tvaraff[k], kl+1, nbcode[Tvaraff[k]][lv]); */
11751: sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%lg " ,kl,Tvresult[nres][k], kl+1,TinvDoQresult[nres][Tvresult[nres][k]]);
11752: kl++;
11753: if(k <cptcovs && cptcovs>1)
11754: sprintf(gplotcondition+strlen(gplotcondition)," && ");
11755: }
11756: }
11757: strcpy(gplotcondition+strlen(gplotcondition),")");
11758: /* 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 *\/ */
11759: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
11760: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
11761: /* '' 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*/
11762: if(i==nlstate+1){
11763: fprintf(ficgp,"%s ? $%d : 1/0):%d t 'bw%d' with line lc variable", gplotcondition, \
11764: ioffset+(cpt-1)*(nlstate+1)+1+(i-1),iyearc,cpt );
11765: fprintf(ficgp,",\\\n '' ");
11766: fprintf(ficgp," u %d:(",iagec);
11767: /* fprintf(ficgp,"%s && (($5-$6) == %d ) ? $%d/(1.-$%d) : 1/0):5 with labels center not ", gplotcondition, \ */
11768: fprintf(ficgp,"%s && (($%d-$%d) == %d ) ? $%d : 1/0):%d with labels center not ", gplotcondition, \
11769: iyearc,iagec,offbyear, \
11770: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), iyearc );
11771: /* '' u 6:(($1==1 && $2==0 && $3==2 && $4==0) && (($5-$6) == 1947) ? $10/(1.-$22) : 1/0):5 with labels center boxed not*/
11772: }else{
11773: /* fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \ */
11774: fprintf(ficgp,"%s ? $%d : 1/0) t 'b%d%d' with line ", gplotcondition, \
11775: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), cpt,i );
11776: }
11777: } /* end if covariate */
11778: } /* nlstate */
11779: fprintf(ficgp,"\nset out; unset label;\n");
11780: } /* end cpt state*/
11781: } /* end covariate */
11782: } /* End if prevbcast */
11783:
11784:
11785: /* 9eme writing MLE parameters */
11786: fprintf(ficgp,"\n##############\n#9eme MLE estimated parameters\n#############\n");
11787: for(i=1,jk=1; i <=nlstate; i++){
11788: fprintf(ficgp,"# initial state %d\n",i);
11789: for(k=1; k <=(nlstate+ndeath); k++){
11790: if (k != i) {
11791: fprintf(ficgp,"# current state %d\n",k);
11792: for(j=1; j <=ncovmodel; j++){
11793: fprintf(ficgp,"p%d=%f; ",jk,p[jk]);
11794: jk++;
11795: }
11796: fprintf(ficgp,"\n");
11797: }
11798: }
11799: }
11800: fprintf(ficgp,"##############\n#\n");
11801:
11802: /*goto avoid;*/
11803: /* 10eme Graphics of probabilities or incidences using written MLE parameters */
11804: fprintf(ficgp,"\n##############\n#10eme Graphics of probabilities or incidences\n#############\n");
11805: fprintf(ficgp,"# logi(p12/p11)=a12+b12*age+c12age*age+d12*V1+e12*V1*age\n");
11806: fprintf(ficgp,"# logi(p12/p11)=p1 +p2*age +p3*age*age+ p4*V1+ p5*V1*age\n");
11807: fprintf(ficgp,"# logi(p13/p11)=a13+b13*age+c13age*age+d13*V1+e13*V1*age\n");
11808: fprintf(ficgp,"# logi(p13/p11)=p6 +p7*age +p8*age*age+ p9*V1+ p10*V1*age\n");
11809: fprintf(ficgp,"# p12+p13+p14+p11=1=p11(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
11810: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
11811: fprintf(ficgp,"# p11=1/(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
11812: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
11813: fprintf(ficgp,"# p12=exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)/\n");
11814: fprintf(ficgp,"# (1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
11815: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age))\n");
11816: fprintf(ficgp,"# +exp(a14+b14*age+c14age*age+d14*V1+e14*V1*age)+...)\n");
11817: fprintf(ficgp,"#\n");
11818: for(ng=1; ng<=3;ng++){ /* Number of graphics: first is logit, 2nd is probabilities, third is incidences per year*/
11819: fprintf(ficgp,"#Number of graphics: first is logit, 2nd is probabilities, third is incidences per year\n");
11820: fprintf(ficgp,"#model=1+age+%s \n",model);
11821: fprintf(ficgp,"# Type of graphic ng=%d\n",ng);
11822: fprintf(ficgp,"# k1=1 to 2^%d=%d\n",cptcoveff,m);/* to be checked */
11823: /* for(k1=1; k1 <=m; k1++) /\* For each combination of covariate *\/ */
11824: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
11825: /* k1=nres; */
11826: k1=TKresult[nres];
11827: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
11828: fprintf(ficgp,"\n\n# Resultline k1=%d ",k1);
11829: strcpy(gplotlabel,"(");
11830: /*sprintf(gplotlabel+strlen(gplotlabel)," Dummy combination %d ",k1);*/
11831: for (k=1; k<=cptcovs; k++){ /**< cptcovs number of SIMPLE covariates in the model V2+V1 =2 (dummy or quantit or time varying) */
11832: /* for each resultline nres, and position k, Tvresult[nres][k] gives the name of the variable and
11833: TinvDoQresult[nres][Tvresult[nres][k]] gives its value double or integer) */
11834: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
11835: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
11836: }
11837: /* if(m != 1 && TKresult[nres]!= k1) */
11838: /* continue; */
11839: /* fprintf(ficgp,"\n\n# Combination of dummy k1=%d which is ",k1); */
11840: /* strcpy(gplotlabel,"("); */
11841: /* /\*sprintf(gplotlabel+strlen(gplotlabel)," Dummy combination %d ",k1);*\/ */
11842: /* for (k=1; k<=cptcoveff; k++){ /\* For each correspondig covariate value *\/ */
11843: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate value corresponding to k1 combination and kth covariate *\\/ *\/ */
11844: /* lv= codtabm(k1,TnsdVar[Tvaraff[k]]); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
11845: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
11846: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
11847: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
11848: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
11849: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
11850: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
11851: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
11852: /* } */
11853: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
11854: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
11855: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
11856: /* } */
11857: strcpy(gplotlabel+strlen(gplotlabel),")");
11858: fprintf(ficgp,"\n#\n");
11859: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" ",subdirf2(optionfilefiname,"PE_"),k1,ng,nres);
11860: fprintf(ficgp,"\nset key outside ");
11861: /* fprintf(ficgp,"\nset label \"%s\" at graph 1.2,0.5 center rotate font \"Helvetica,12\"\n",gplotlabel); */
11862: fprintf(ficgp,"\nset title \"%s\" font \"Helvetica,12\"\n",gplotlabel);
11863: fprintf(ficgp,"\nset ter svg size 640, 480 ");
11864: if (ng==1){
11865: fprintf(ficgp,"\nset ylabel \"Value of the logit of the model\"\n"); /* exp(a12+b12*x) could be nice */
11866: fprintf(ficgp,"\nunset log y");
11867: }else if (ng==2){
11868: fprintf(ficgp,"\nset ylabel \"Probability\"\n");
11869: fprintf(ficgp,"\nset log y");
11870: }else if (ng==3){
11871: fprintf(ficgp,"\nset ylabel \"Quasi-incidence per year\"\n");
11872: fprintf(ficgp,"\nset log y");
11873: }else
11874: fprintf(ficgp,"\nunset title ");
11875: fprintf(ficgp,"\nplot [%.f:%.f] ",ageminpar,agemaxpar);
11876: i=1;
11877: for(k2=1; k2<=nlstate; k2++) {
11878: k3=i;
11879: for(k=1; k<=(nlstate+ndeath); k++) {
11880: if (k != k2){
11881: switch( ng) {
11882: case 1:
11883: if(nagesqr==0)
11884: fprintf(ficgp," p%d+p%d*x",i,i+1);
11885: else /* nagesqr =1 */
11886: fprintf(ficgp," p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
11887: break;
11888: case 2: /* ng=2 */
11889: if(nagesqr==0)
11890: fprintf(ficgp," exp(p%d+p%d*x",i,i+1);
11891: else /* nagesqr =1 */
11892: fprintf(ficgp," exp(p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
11893: break;
11894: case 3:
11895: if(nagesqr==0)
11896: fprintf(ficgp," %f*exp(p%d+p%d*x",YEARM/stepm,i,i+1);
11897: else /* nagesqr =1 */
11898: fprintf(ficgp," %f*exp(p%d+p%d*x+p%d*x*x",YEARM/stepm,i,i+1,i+1+nagesqr);
11899: break;
11900: }
11901: ij=1;/* To be checked else nbcode[0][0] wrong */
11902: ijp=1; /* product no age */
11903: /* for(j=3; j <=ncovmodel-nagesqr; j++) { */
11904: for(j=1; j <=cptcovt; j++) { /* For each covariate of the simplified model */
11905: /* printf("Tage[%d]=%d, j=%d\n", ij, Tage[ij], j); */
11906: switch(Typevar[j]){
11907: case 1:
11908: if(cptcovage >0){ /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, 2 V5 and V1 */
11909: if(j==Tage[ij]) { /* Product by age To be looked at!!*//* Bug valgrind */
11910: if(ij <=cptcovage) { /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, 2 V5 and V1 */
11911: if(DummyV[j]==0){/* Bug valgrind */
11912: fprintf(ficgp,"+p%d*%d*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]);;
11913: }else{ /* quantitative */
11914: fprintf(ficgp,"+p%d*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /* Tqinvresult in decoderesult */
11915: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
11916: }
11917: ij++;
11918: }
11919: }
11920: }
11921: break;
11922: case 2:
11923: if(cptcovprod >0){
11924: if(j==Tprod[ijp]) { /* */
11925: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
11926: if(ijp <=cptcovprod) { /* Product */
11927: if(DummyV[Tvard[ijp][1]]==0){/* Vn is dummy */
11928: if(DummyV[Tvard[ijp][2]]==0){/* Vn and Vm are dummy */
11929: /* 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)]); */
11930: fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]);
11931: }else{ /* Vn is dummy and Vm is quanti */
11932: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],Tqinvresult[nres][Tvard[ijp][2]]); */
11933: fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
11934: }
11935: }else{ /* Vn*Vm Vn is quanti */
11936: if(DummyV[Tvard[ijp][2]]==0){
11937: fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]);
11938: }else{ /* Both quanti */
11939: fprintf(ficgp,"+p%d*%f*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
11940: }
11941: }
11942: ijp++;
11943: }
11944: } /* end Tprod */
11945: }
11946: break;
11947: case 3:
11948: if(cptcovdageprod >0){
11949: /* if(j==Tprod[ijp]) { */ /* not necessary */
11950: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
11951: if(ijp <=cptcovprod) { /* Product */
11952: if(DummyV[Tvard[ijp][1]]==0){/* Vn is dummy */
11953: if(DummyV[Tvard[ijp][2]]==0){/* Vn and Vm are dummy */
11954: /* 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)]); */
11955: fprintf(ficgp,"+p%d*%d*%d*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]);
11956: }else{ /* Vn is dummy and Vm is quanti */
11957: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],Tqinvresult[nres][Tvard[ijp][2]]); */
11958: fprintf(ficgp,"+p%d*%d*%f*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
11959: }
11960: }else{ /* Vn*Vm Vn is quanti */
11961: if(DummyV[Tvard[ijp][2]]==0){
11962: fprintf(ficgp,"+p%d*%d*%f*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]);
11963: }else{ /* Both quanti */
11964: fprintf(ficgp,"+p%d*%f*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
11965: }
11966: }
11967: ijp++;
11968: }
11969: /* } */ /* end Tprod */
11970: }
11971: break;
11972: case 0:
11973: /* simple covariate */
11974: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,nbcode[Tvar[j]][codtabm(k1,j)]); /\* Valgrind bug nbcode *\/ */
11975: if(Dummy[j]==0){
11976: fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]); /* */
11977: }else{ /* quantitative */
11978: fprintf(ficgp,"+p%d*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /* */
11979: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
11980: }
11981: /* end simple */
11982: break;
11983: default:
11984: break;
11985: } /* end switch */
11986: } /* end j */
11987: }else{ /* k=k2 */
11988: if(ng !=1 ){ /* For logit formula of log p11 is more difficult to get */
11989: fprintf(ficgp," (1.");i=i-ncovmodel;
11990: }else
11991: i=i-ncovmodel;
11992: }
11993:
11994: if(ng != 1){
11995: fprintf(ficgp,")/(1");
11996:
11997: for(cpt=1; cpt <=nlstate; cpt++){
11998: if(nagesqr==0)
11999: fprintf(ficgp,"+exp(p%d+p%d*x",k3+(cpt-1)*ncovmodel,k3+(cpt-1)*ncovmodel+1);
12000: else /* nagesqr =1 */
12001: 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);
12002:
12003: ij=1;
12004: ijp=1;
12005: /* for(j=3; j <=ncovmodel-nagesqr; j++){ */
12006: for(j=1; j <=cptcovt; j++) { /* For each covariate of the simplified model */
12007: switch(Typevar[j]){
12008: case 1:
12009: if(cptcovage >0){
12010: if(j==Tage[ij]) { /* Bug valgrind */
12011: if(ij <=cptcovage) { /* Bug valgrind */
12012: if(DummyV[j]==0){/* Bug valgrind */
12013: /* fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,j-2)]); */
12014: /* fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j+nagesqr,nbcode[Tvar[j]][codtabm(k1,j)]); */
12015: fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvar[j]]);
12016: /* fprintf(ficgp,"+p%d*%d*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]);; */
12017: /* fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
12018: }else{ /* quantitative */
12019: /* fprintf(ficgp,"+p%d*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /\* Tqinvresult in decoderesult *\/ */
12020: fprintf(ficgp,"+p%d*%f*x",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tqinvresult[nres][Tvar[j]]); /* Tqinvresult in decoderesult */
12021: /* fprintf(ficgp,"+p%d*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /\* Tqinvresult in decoderesult *\/ */
12022: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
12023: }
12024: ij++;
12025: }
12026: }
12027: }
12028: break;
12029: case 2:
12030: if(cptcovprod >0){
12031: if(j==Tprod[ijp]) { /* */
12032: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
12033: if(ijp <=cptcovprod) { /* Product */
12034: if(DummyV[Tvard[ijp][1]]==0){/* Vn is dummy */
12035: if(DummyV[Tvard[ijp][2]]==0){/* Vn and Vm are dummy */
12036: /* 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)]); */
12037: fprintf(ficgp,"+p%d*%d*%d",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]);
12038: /* fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]); */
12039: }else{ /* Vn is dummy and Vm is quanti */
12040: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],Tqinvresult[nres][Tvard[ijp][2]]); */
12041: fprintf(ficgp,"+p%d*%d*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
12042: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]); */
12043: }
12044: }else{ /* Vn*Vm Vn is quanti */
12045: if(DummyV[Tvard[ijp][2]]==0){
12046: fprintf(ficgp,"+p%d*%d*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]);
12047: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]); */
12048: }else{ /* Both quanti */
12049: fprintf(ficgp,"+p%d*%f*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
12050: /* fprintf(ficgp,"+p%d*%f*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]); */
12051: }
12052: }
12053: ijp++;
12054: }
12055: } /* end Tprod */
12056: } /* end if */
12057: break;
12058: case 3:
12059: if(cptcovdageprod >0){
12060: /* if(j==Tprod[ijp]) { /\* *\/ */
12061: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
12062: if(ijp <=cptcovprod) { /* Product */
12063: if(DummyV[Tvard[ijp][1]]==0){/* Vn is dummy */
12064: if(DummyV[Tvard[ijp][2]]==0){/* Vn and Vm are dummy */
12065: /* 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)]); */
12066: fprintf(ficgp,"+p%d*%d*%d*x",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]);
12067: /* fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]); */
12068: }else{ /* Vn is dummy and Vm is quanti */
12069: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],Tqinvresult[nres][Tvard[ijp][2]]); */
12070: fprintf(ficgp,"+p%d*%d*%f*x",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
12071: /* fprintf(ficgp,"+p%d*%d*%f*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]); */
12072: }
12073: }else{ /* Vn*Vm Vn is quanti */
12074: if(DummyV[Tvard[ijp][2]]==0){
12075: fprintf(ficgp,"+p%d*%d*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]);
12076: /* fprintf(ficgp,"+p%d*%d*%f*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]); */
12077: }else{ /* Both quanti */
12078: fprintf(ficgp,"+p%d*%f*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
12079: /* fprintf(ficgp,"+p%d*%f*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]); */
12080: }
12081: }
12082: ijp++;
12083: }
12084: /* } /\* end Tprod *\/ */
12085: } /* end if */
12086: break;
12087: case 0:
12088: /* simple covariate */
12089: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,nbcode[Tvar[j]][codtabm(k1,j)]); /\* Valgrind bug nbcode *\/ */
12090: if(Dummy[j]==0){
12091: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]); /\* *\/ */
12092: fprintf(ficgp,"+p%d*%d",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvar[j]]); /* */
12093: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]); /\* *\/ */
12094: }else{ /* quantitative */
12095: fprintf(ficgp,"+p%d*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tqinvresult[nres][Tvar[j]]); /* */
12096: /* fprintf(ficgp,"+p%d*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /\* *\/ */
12097: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
12098: }
12099: /* end simple */
12100: /* fprintf(ficgp,"+p%d*%d",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,j-2)]);/\* Valgrind bug nbcode *\/ */
12101: break;
12102: default:
12103: break;
12104: } /* end switch */
12105: }
12106: fprintf(ficgp,")");
12107: }
12108: fprintf(ficgp,")");
12109: if(ng ==2)
12110: 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);
12111: else /* ng= 3 */
12112: 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);
12113: }else{ /* end ng <> 1 */
12114: if( k !=k2) /* logit p11 is hard to draw */
12115: 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);
12116: }
12117: if ((k+k2)!= (nlstate*2+ndeath) && ng != 1)
12118: fprintf(ficgp,",");
12119: if (ng == 1 && k!=k2 && (k+k2)!= (nlstate*2+ndeath))
12120: fprintf(ficgp,",");
12121: i=i+ncovmodel;
12122: } /* end k */
12123: } /* end k2 */
12124: /* fprintf(ficgp,"\n set out; unset label;set key default;\n"); */
12125: fprintf(ficgp,"\n set out; unset title;set key default;\n");
12126: } /* end resultline */
12127: } /* end ng */
12128: /* avoid: */
12129: fflush(ficgp);
12130: } /* end gnuplot */
12131:
12132:
12133: /*************** Moving average **************/
12134: /* int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav, double bageout, double fageout){ */
12135: int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav){
12136:
12137: int i, cpt, cptcod;
12138: int modcovmax =1;
12139: int mobilavrange, mob;
12140: int iage=0;
12141: int firstA1=0, firstA2=0;
12142:
12143: double sum=0., sumr=0.;
12144: double age;
12145: double *sumnewp, *sumnewm, *sumnewmr;
12146: double *agemingood, *agemaxgood;
12147: double *agemingoodr, *agemaxgoodr;
12148:
12149:
12150: /* modcovmax=2*cptcoveff; Max number of modalities. We suppose */
12151: /* a covariate has 2 modalities, should be equal to ncovcombmax */
12152:
12153: sumnewp = vector(1,ncovcombmax);
12154: sumnewm = vector(1,ncovcombmax);
12155: sumnewmr = vector(1,ncovcombmax);
12156: agemingood = vector(1,ncovcombmax);
12157: agemingoodr = vector(1,ncovcombmax);
12158: agemaxgood = vector(1,ncovcombmax);
12159: agemaxgoodr = vector(1,ncovcombmax);
12160:
12161: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
12162: sumnewm[cptcod]=0.; sumnewmr[cptcod]=0.;
12163: sumnewp[cptcod]=0.;
12164: agemingood[cptcod]=0, agemingoodr[cptcod]=0;
12165: agemaxgood[cptcod]=0, agemaxgoodr[cptcod]=0;
12166: }
12167: if (cptcovn<1) ncovcombmax=1; /* At least 1 pass */
12168:
12169: if(mobilav==-1 || mobilav==1||mobilav ==3 ||mobilav==5 ||mobilav== 7){
12170: if(mobilav==1 || mobilav==-1) mobilavrange=5; /* default */
12171: else mobilavrange=mobilav;
12172: for (age=bage; age<=fage; age++)
12173: for (i=1; i<=nlstate;i++)
12174: for (cptcod=1;cptcod<=ncovcombmax;cptcod++)
12175: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
12176: /* We keep the original values on the extreme ages bage, fage and for
12177: fage+1 and bage-1 we use a 3 terms moving average; for fage+2 bage+2
12178: we use a 5 terms etc. until the borders are no more concerned.
12179: */
12180: for (mob=3;mob <=mobilavrange;mob=mob+2){
12181: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){
12182: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
12183: sumnewm[cptcod]=0.;
12184: for (i=1; i<=nlstate;i++){
12185: mobaverage[(int)age][i][cptcod] =probs[(int)age][i][cptcod];
12186: for (cpt=1;cpt<=(mob-1)/2;cpt++){
12187: mobaverage[(int)age][i][cptcod] +=probs[(int)age-cpt][i][cptcod];
12188: mobaverage[(int)age][i][cptcod] +=probs[(int)age+cpt][i][cptcod];
12189: }
12190: mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/mob;
12191: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
12192: } /* end i */
12193: if(sumnewm[cptcod] >1.e-3) mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/sumnewm[cptcod]; /* Rescaling to sum one */
12194: } /* end cptcod */
12195: }/* end age */
12196: }/* end mob */
12197: }else{
12198: printf("Error internal in movingaverage, mobilav=%d.\n",mobilav);
12199: return -1;
12200: }
12201:
12202: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){ /* for each combination */
12203: /* for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ */
12204: if(invalidvarcomb[cptcod]){
12205: printf("\nCombination (%d) ignored because no cases \n",cptcod);
12206: continue;
12207: }
12208:
12209: for (age=fage-(mob-1)/2; age>=bage+(mob-1)/2; age--){ /*looking for the youngest and oldest good age */
12210: sumnewm[cptcod]=0.;
12211: sumnewmr[cptcod]=0.;
12212: for (i=1; i<=nlstate;i++){
12213: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
12214: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
12215: }
12216: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
12217: agemingoodr[cptcod]=age;
12218: }
12219: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
12220: agemingood[cptcod]=age;
12221: }
12222: } /* age */
12223: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ /*looking for the youngest and oldest good age */
12224: sumnewm[cptcod]=0.;
12225: sumnewmr[cptcod]=0.;
12226: for (i=1; i<=nlstate;i++){
12227: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
12228: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
12229: }
12230: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
12231: agemaxgoodr[cptcod]=age;
12232: }
12233: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
12234: agemaxgood[cptcod]=age;
12235: }
12236: } /* age */
12237: /* Thus we have agemingood and agemaxgood as well as goodr for raw (preobs) */
12238: /* but they will change */
12239: firstA1=0;firstA2=0;
12240: for (age=fage-(mob-1)/2; age>=bage; age--){/* From oldest to youngest, filling up to the youngest */
12241: sumnewm[cptcod]=0.;
12242: sumnewmr[cptcod]=0.;
12243: for (i=1; i<=nlstate;i++){
12244: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
12245: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
12246: }
12247: if(mobilav==-1){ /* Forcing raw ages if good else agemingood */
12248: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
12249: agemaxgoodr[cptcod]=age; /* age min */
12250: for (i=1; i<=nlstate;i++)
12251: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
12252: }else{ /* bad we change the value with the values of good ages */
12253: for (i=1; i<=nlstate;i++){
12254: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgoodr[cptcod]][i][cptcod];
12255: } /* i */
12256: } /* end bad */
12257: }else{
12258: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
12259: agemaxgood[cptcod]=age;
12260: }else{ /* bad we change the value with the values of good ages */
12261: for (i=1; i<=nlstate;i++){
12262: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
12263: } /* i */
12264: } /* end bad */
12265: }/* end else */
12266: sum=0.;sumr=0.;
12267: for (i=1; i<=nlstate;i++){
12268: sum+=mobaverage[(int)age][i][cptcod];
12269: sumr+=probs[(int)age][i][cptcod];
12270: }
12271: if(fabs(sum - 1.) > 1.e-3) { /* bad */
12272: if(!firstA1){
12273: firstA1=1;
12274: 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);
12275: }
12276: 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);
12277: } /* end bad */
12278: /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */
12279: if(fabs(sumr - 1.) > 1.e-3) { /* bad */
12280: if(!firstA2){
12281: firstA2=1;
12282: 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);
12283: }
12284: 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);
12285: } /* end bad */
12286: }/* age */
12287:
12288: for (age=bage+(mob-1)/2; age<=fage; age++){/* From youngest, finding the oldest wrong */
12289: sumnewm[cptcod]=0.;
12290: sumnewmr[cptcod]=0.;
12291: for (i=1; i<=nlstate;i++){
12292: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
12293: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
12294: }
12295: if(mobilav==-1){ /* Forcing raw ages if good else agemingood */
12296: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good */
12297: agemingoodr[cptcod]=age;
12298: for (i=1; i<=nlstate;i++)
12299: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
12300: }else{ /* bad we change the value with the values of good ages */
12301: for (i=1; i<=nlstate;i++){
12302: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingoodr[cptcod]][i][cptcod];
12303: } /* i */
12304: } /* end bad */
12305: }else{
12306: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
12307: agemingood[cptcod]=age;
12308: }else{ /* bad */
12309: for (i=1; i<=nlstate;i++){
12310: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod];
12311: } /* i */
12312: } /* end bad */
12313: }/* end else */
12314: sum=0.;sumr=0.;
12315: for (i=1; i<=nlstate;i++){
12316: sum+=mobaverage[(int)age][i][cptcod];
12317: sumr+=mobaverage[(int)age][i][cptcod];
12318: }
12319: if(fabs(sum - 1.) > 1.e-3) { /* bad */
12320: 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);
12321: } /* end bad */
12322: /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */
12323: if(fabs(sumr - 1.) > 1.e-3) { /* bad */
12324: 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);
12325: } /* end bad */
12326: }/* age */
12327:
12328:
12329: for (age=bage; age<=fage; age++){
12330: /* printf("%d %d ", cptcod, (int)age); */
12331: sumnewp[cptcod]=0.;
12332: sumnewm[cptcod]=0.;
12333: for (i=1; i<=nlstate;i++){
12334: sumnewp[cptcod]+=probs[(int)age][i][cptcod];
12335: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
12336: /* printf("%.4f %.4f ",probs[(int)age][i][cptcod], mobaverage[(int)age][i][cptcod]); */
12337: }
12338: /* printf("%.4f %.4f \n",sumnewp[cptcod], sumnewm[cptcod]); */
12339: }
12340: /* printf("\n"); */
12341: /* } */
12342:
12343: /* brutal averaging */
12344: /* for (i=1; i<=nlstate;i++){ */
12345: /* for (age=1; age<=bage; age++){ */
12346: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod]; */
12347: /* /\* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); *\/ */
12348: /* } */
12349: /* for (age=fage; age<=AGESUP; age++){ */
12350: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod]; */
12351: /* /\* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); *\/ */
12352: /* } */
12353: /* } /\* end i status *\/ */
12354: /* for (i=nlstate+1; i<=nlstate+ndeath;i++){ */
12355: /* for (age=1; age<=AGESUP; age++){ */
12356: /* /\*printf("i=%d, age=%d, cptcod=%d\n",i, (int)age, cptcod);*\/ */
12357: /* mobaverage[(int)age][i][cptcod]=0.; */
12358: /* } */
12359: /* } */
12360: }/* end cptcod */
12361: free_vector(agemaxgoodr,1, ncovcombmax);
12362: free_vector(agemaxgood,1, ncovcombmax);
12363: free_vector(agemingood,1, ncovcombmax);
12364: free_vector(agemingoodr,1, ncovcombmax);
12365: free_vector(sumnewmr,1, ncovcombmax);
12366: free_vector(sumnewm,1, ncovcombmax);
12367: free_vector(sumnewp,1, ncovcombmax);
12368: return 0;
12369: }/* End movingaverage */
12370:
12371:
12372:
12373: /************** Forecasting ******************/
12374: /* 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)*/
12375: 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){
12376: /* dateintemean, mean date of interviews
12377: dateprojd, year, month, day of starting projection
12378: dateprojf date of end of projection;year of end of projection (same day and month as proj1).
12379: agemin, agemax range of age
12380: dateprev1 dateprev2 range of dates during which prevalence is computed
12381: */
12382: /* double anprojd, mprojd, jprojd; */
12383: /* double anprojf, mprojf, jprojf; */
12384: int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1, k4, nres=0;
12385: double agec; /* generic age */
12386: double agelim, ppij, yp,yp1,yp2;
12387: double *popeffectif,*popcount;
12388: double ***p3mat;
12389: /* double ***mobaverage; */
12390: char fileresf[FILENAMELENGTH];
12391:
12392: agelim=AGESUP;
12393: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
12394: in each health status at the date of interview (if between dateprev1 and dateprev2).
12395: We still use firstpass and lastpass as another selection.
12396: */
12397: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ */
12398: /* firstpass, lastpass, stepm, weightopt, model); */
12399:
12400: strcpy(fileresf,"F_");
12401: strcat(fileresf,fileresu);
12402: if((ficresf=fopen(fileresf,"w"))==NULL) {
12403: printf("Problem with forecast resultfile: %s\n", fileresf);
12404: fprintf(ficlog,"Problem with forecast resultfile: %s\n", fileresf);
12405: }
12406: printf("\nComputing forecasting: result on file '%s', please wait... \n", fileresf);
12407: fprintf(ficlog,"\nComputing forecasting: result on file '%s', please wait... \n", fileresf);
12408:
12409: if (cptcoveff==0) ncodemax[cptcoveff]=1;
12410:
12411:
12412: stepsize=(int) (stepm+YEARM-1)/YEARM;
12413: if (stepm<=12) stepsize=1;
12414: if(estepm < stepm){
12415: printf ("Problem %d lower than %d\n",estepm, stepm);
12416: }
12417: else{
12418: hstepm=estepm;
12419: }
12420: if(estepm > stepm){ /* Yes every two year */
12421: stepsize=2;
12422: }
12423: hstepm=hstepm/stepm;
12424:
12425:
12426: /* yp1=modf(dateintmean,&yp);/\* extracts integral of datemean in yp and */
12427: /* fractional in yp1 *\/ */
12428: /* aintmean=yp; */
12429: /* yp2=modf((yp1*12),&yp); */
12430: /* mintmean=yp; */
12431: /* yp1=modf((yp2*30.5),&yp); */
12432: /* jintmean=yp; */
12433: /* if(jintmean==0) jintmean=1; */
12434: /* if(mintmean==0) mintmean=1; */
12435:
12436:
12437: /* date2dmy(dateintmean,&jintmean,&mintmean,&aintmean); */
12438: /* date2dmy(dateprojd,&jprojd, &mprojd, &anprojd); */
12439: /* date2dmy(dateprojf,&jprojf, &mprojf, &anprojf); */
12440: i1=pow(2,cptcoveff);
12441: if (cptcovn < 1){i1=1;}
12442:
12443: fprintf(ficresf,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
12444:
12445: fprintf(ficresf,"#****** Routine prevforecast **\n");
12446:
12447: /* if (h==(int)(YEARM*yearp)){ */
12448: for(nres=1; nres <= nresult; nres++) /* For each resultline */
12449: 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) */
12450: if(i1 != 1 && TKresult[nres]!= k)
12451: continue;
12452: if(invalidvarcomb[k]){
12453: printf("\nCombination (%d) projection ignored because no cases \n",k);
12454: continue;
12455: }
12456: fprintf(ficresf,"\n#****** hpijx=probability over h years, hp.jx is weighted by observed prev \n#");
12457: for(j=1;j<=cptcoveff;j++) {
12458: /* fprintf(ficresf," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,Tvaraff[j])]); */
12459: fprintf(ficresf," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]);
12460: }
12461: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
12462: fprintf(ficresf," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
12463: }
12464: fprintf(ficresf," yearproj age");
12465: for(j=1; j<=nlstate+ndeath;j++){
12466: for(i=1; i<=nlstate;i++)
12467: fprintf(ficresf," p%d%d",i,j);
12468: fprintf(ficresf," wp.%d",j);
12469: }
12470: for (yearp=0; yearp<=(anprojf-anprojd);yearp +=stepsize) {
12471: fprintf(ficresf,"\n");
12472: fprintf(ficresf,"\n# Forecasting at date %.lf/%.lf/%.lf ",jprojd,mprojd,anprojd+yearp);
12473: /* for (agec=fage; agec>=(ageminpar-1); agec--){ */
12474: for (agec=fage; agec>=(bage); agec--){
12475: nhstepm=(int) rint((agelim-agec)*YEARM/stepm);
12476: nhstepm = nhstepm/hstepm;
12477: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
12478: oldm=oldms;savm=savms;
12479: /* We compute pii at age agec over nhstepm);*/
12480: hpxij(p3mat,nhstepm,agec,hstepm,p,nlstate,stepm,oldm,savm, k,nres);
12481: /* Then we print p3mat for h corresponding to the right agec+h*stepms=yearp */
12482: for (h=0; h<=nhstepm; h++){
12483: if (h*hstepm/YEARM*stepm ==yearp) {
12484: break;
12485: }
12486: }
12487: fprintf(ficresf,"\n");
12488: for(j=1;j<=cptcoveff;j++)
12489: /* fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,Tvaraff[j])]); /\* Tvaraff not correct *\/ */
12490: fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); /* TnsdVar[Tvaraff] correct */
12491: fprintf(ficresf,"%.f %.f ",anprojd+yearp,agec+h*hstepm/YEARM*stepm);
12492:
12493: for(j=1; j<=nlstate+ndeath;j++) {
12494: ppij=0.;
12495: for(i=1; i<=nlstate;i++) {
12496: if (mobilav>=1)
12497: ppij=ppij+p3mat[i][j][h]*prev[(int)agec][i][k];
12498: else { /* even if mobilav==-1 we use mobaverage, probs may not sums to 1 */
12499: ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][k];
12500: }
12501: fprintf(ficresf," %.3f", p3mat[i][j][h]);
12502: } /* end i */
12503: fprintf(ficresf," %.3f", ppij);
12504: }/* end j */
12505: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
12506: } /* end agec */
12507: /* diffyear=(int) anproj1+yearp-ageminpar-1; */
12508: /*printf("Prevforecast %d+%d-%d=diffyear=%d\n",(int) anproj1, (int)yearp,(int)ageminpar,(int) anproj1-(int)ageminpar);*/
12509: } /* end yearp */
12510: } /* end k */
12511:
12512: fclose(ficresf);
12513: printf("End of Computing forecasting \n");
12514: fprintf(ficlog,"End of Computing forecasting\n");
12515:
12516: }
12517:
12518: /************** Back Forecasting ******************/
12519: /* 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){ */
12520: 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){
12521: /* back1, year, month, day of starting backprojection
12522: agemin, agemax range of age
12523: dateprev1 dateprev2 range of dates during which prevalence is computed
12524: anback2 year of end of backprojection (same day and month as back1).
12525: prevacurrent and prev are prevalences.
12526: */
12527: int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1, k4, nres=0;
12528: double agec; /* generic age */
12529: double agelim, ppij, ppi, yp,yp1,yp2; /* ,jintmean,mintmean,aintmean;*/
12530: double *popeffectif,*popcount;
12531: double ***p3mat;
12532: /* double ***mobaverage; */
12533: char fileresfb[FILENAMELENGTH];
12534:
12535: agelim=AGEINF;
12536: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
12537: in each health status at the date of interview (if between dateprev1 and dateprev2).
12538: We still use firstpass and lastpass as another selection.
12539: */
12540: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ */
12541: /* firstpass, lastpass, stepm, weightopt, model); */
12542:
12543: /*Do we need to compute prevalence again?*/
12544:
12545: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
12546:
12547: strcpy(fileresfb,"FB_");
12548: strcat(fileresfb,fileresu);
12549: if((ficresfb=fopen(fileresfb,"w"))==NULL) {
12550: printf("Problem with back forecast resultfile: %s\n", fileresfb);
12551: fprintf(ficlog,"Problem with back forecast resultfile: %s\n", fileresfb);
12552: }
12553: printf("\nComputing back forecasting: result on file '%s', please wait... \n", fileresfb);
12554: fprintf(ficlog,"\nComputing back forecasting: result on file '%s', please wait... \n", fileresfb);
12555:
12556: if (cptcoveff==0) ncodemax[cptcoveff]=1;
12557:
12558:
12559: stepsize=(int) (stepm+YEARM-1)/YEARM;
12560: if (stepm<=12) stepsize=1;
12561: if(estepm < stepm){
12562: printf ("Problem %d lower than %d\n",estepm, stepm);
12563: }
12564: else{
12565: hstepm=estepm;
12566: }
12567: if(estepm >= stepm){ /* Yes every two year */
12568: stepsize=2;
12569: }
12570:
12571: hstepm=hstepm/stepm;
12572: /* yp1=modf(dateintmean,&yp);/\* extracts integral of datemean in yp and */
12573: /* fractional in yp1 *\/ */
12574: /* aintmean=yp; */
12575: /* yp2=modf((yp1*12),&yp); */
12576: /* mintmean=yp; */
12577: /* yp1=modf((yp2*30.5),&yp); */
12578: /* jintmean=yp; */
12579: /* if(jintmean==0) jintmean=1; */
12580: /* if(mintmean==0) jintmean=1; */
12581:
12582: i1=pow(2,cptcoveff);
12583: if (cptcovn < 1){i1=1;}
12584:
12585: fprintf(ficresfb,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
12586: printf("# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
12587:
12588: fprintf(ficresfb,"#****** Routine prevbackforecast **\n");
12589:
12590: for(nres=1; nres <= nresult; nres++) /* For each resultline */
12591: for(k=1; k<=i1;k++){
12592: if(i1 != 1 && TKresult[nres]!= k)
12593: continue;
12594: if(invalidvarcomb[k]){
12595: printf("\nCombination (%d) projection ignored because no cases \n",k);
12596: continue;
12597: }
12598: fprintf(ficresfb,"\n#****** hbijx=probability over h years, hb.jx is weighted by observed prev \n#");
12599: for(j=1;j<=cptcoveff;j++) {
12600: fprintf(ficresfb," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]);
12601: }
12602: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
12603: fprintf(ficresf," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
12604: }
12605: fprintf(ficresfb," yearbproj age");
12606: for(j=1; j<=nlstate+ndeath;j++){
12607: for(i=1; i<=nlstate;i++)
12608: fprintf(ficresfb," b%d%d",i,j);
12609: fprintf(ficresfb," b.%d",j);
12610: }
12611: for (yearp=0; yearp>=(anbackf-anbackd);yearp -=stepsize) {
12612: /* for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) { */
12613: fprintf(ficresfb,"\n");
12614: fprintf(ficresfb,"\n# Back Forecasting at date %.lf/%.lf/%.lf ",jbackd,mbackd,anbackd+yearp);
12615: /* printf("\n# Back Forecasting at date %.lf/%.lf/%.lf ",jback1,mback1,anback1+yearp); */
12616: /* for (agec=bage; agec<=agemax-1; agec++){ /\* testing *\/ */
12617: for (agec=bage; agec<=fage; agec++){ /* testing */
12618: /* We compute bij at age agec over nhstepm, nhstepm decreases when agec increases because of agemax;*/
12619: nhstepm=(int) (agec-agelim) *YEARM/stepm;/* nhstepm=(int) rint((agec-agelim)*YEARM/stepm);*/
12620: nhstepm = nhstepm/hstepm;
12621: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
12622: oldm=oldms;savm=savms;
12623: /* computes hbxij at age agec over 1 to nhstepm */
12624: /* printf("####prevbackforecast debug agec=%.2f nhstepm=%d\n",agec, nhstepm);fflush(stdout); */
12625: hbxij(p3mat,nhstepm,agec,hstepm,p,prevacurrent,nlstate,stepm, k, nres);
12626: /* hpxij(p3mat,nhstepm,agec,hstepm,p, nlstate,stepm,oldm,savm, k,nres); */
12627: /* Then we print p3mat for h corresponding to the right agec+h*stepms=yearp */
12628: /* printf(" agec=%.2f\n",agec);fflush(stdout); */
12629: for (h=0; h<=nhstepm; h++){
12630: if (h*hstepm/YEARM*stepm ==-yearp) {
12631: break;
12632: }
12633: }
12634: fprintf(ficresfb,"\n");
12635: for(j=1;j<=cptcoveff;j++)
12636: fprintf(ficresfb,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]);
12637: fprintf(ficresfb,"%.f %.f ",anbackd+yearp,agec-h*hstepm/YEARM*stepm);
12638: for(i=1; i<=nlstate+ndeath;i++) {
12639: ppij=0.;ppi=0.;
12640: for(j=1; j<=nlstate;j++) {
12641: /* if (mobilav==1) */
12642: ppij=ppij+p3mat[i][j][h]*prevacurrent[(int)agec][j][k];
12643: ppi=ppi+prevacurrent[(int)agec][j][k];
12644: /* ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][j][k]; */
12645: /* ppi=ppi+mobaverage[(int)agec][j][k]; */
12646: /* else { */
12647: /* ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][k]; */
12648: /* } */
12649: fprintf(ficresfb," %.3f", p3mat[i][j][h]);
12650: } /* end j */
12651: if(ppi <0.99){
12652: printf("Error in prevbackforecast, prevalence doesn't sum to 1 for state %d: %3f\n",i, ppi);
12653: fprintf(ficlog,"Error in prevbackforecast, prevalence doesn't sum to 1 for state %d: %3f\n",i, ppi);
12654: }
12655: fprintf(ficresfb," %.3f", ppij);
12656: }/* end j */
12657: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
12658: } /* end agec */
12659: } /* end yearp */
12660: } /* end k */
12661:
12662: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
12663:
12664: fclose(ficresfb);
12665: printf("End of Computing Back forecasting \n");
12666: fprintf(ficlog,"End of Computing Back forecasting\n");
12667:
12668: }
12669:
12670: /* Variance of prevalence limit: varprlim */
12671: 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){
12672: /*------- Variance of forward period (stable) prevalence------*/
12673:
12674: char fileresvpl[FILENAMELENGTH];
12675: FILE *ficresvpl;
12676: double **oldm, **savm;
12677: double **varpl; /* Variances of prevalence limits by age */
12678: int i1, k, nres, j ;
12679:
12680: strcpy(fileresvpl,"VPL_");
12681: strcat(fileresvpl,fileresu);
12682: if((ficresvpl=fopen(fileresvpl,"w"))==NULL) {
12683: printf("Problem with variance of forward period (stable) prevalence resultfile: %s\n", fileresvpl);
12684: exit(0);
12685: }
12686: printf("Computing Variance-covariance of forward period (stable) prevalence: file '%s' ...", fileresvpl);fflush(stdout);
12687: fprintf(ficlog, "Computing Variance-covariance of forward period (stable) prevalence: file '%s' ...", fileresvpl);fflush(ficlog);
12688:
12689: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
12690: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
12691:
12692: i1=pow(2,cptcoveff);
12693: if (cptcovn < 1){i1=1;}
12694:
12695: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
12696: k=TKresult[nres];
12697: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
12698: /* for(k=1; k<=i1;k++){ /\* We find the combination equivalent to result line values of dummies *\/ */
12699: if(i1 != 1 && TKresult[nres]!= k)
12700: continue;
12701: fprintf(ficresvpl,"\n#****** ");
12702: printf("\n#****** ");
12703: fprintf(ficlog,"\n#****** ");
12704: for(j=1;j<=cptcovs;j++) {
12705: fprintf(ficresvpl,"V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
12706: fprintf(ficlog,"V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
12707: printf("V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
12708: /* fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
12709: /* printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
12710: }
12711: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
12712: /* printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
12713: /* fprintf(ficresvpl," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
12714: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
12715: /* } */
12716: fprintf(ficresvpl,"******\n");
12717: printf("******\n");
12718: fprintf(ficlog,"******\n");
12719:
12720: varpl=matrix(1,nlstate,(int) bage, (int) fage);
12721: oldm=oldms;savm=savms;
12722: varprevlim(fileresvpl, ficresvpl, varpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl, ncvyearp, k, strstart, nres);
12723: free_matrix(varpl,1,nlstate,(int) bage, (int)fage);
12724: /*}*/
12725: }
12726:
12727: fclose(ficresvpl);
12728: printf("done variance-covariance of forward period prevalence\n");fflush(stdout);
12729: fprintf(ficlog,"done variance-covariance of forward period prevalence\n");fflush(ficlog);
12730:
12731: }
12732: /* Variance of back prevalence: varbprlim */
12733: 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){
12734: /*------- Variance of back (stable) prevalence------*/
12735:
12736: char fileresvbl[FILENAMELENGTH];
12737: FILE *ficresvbl;
12738:
12739: double **oldm, **savm;
12740: double **varbpl; /* Variances of back prevalence limits by age */
12741: int i1, k, nres, j ;
12742:
12743: strcpy(fileresvbl,"VBL_");
12744: strcat(fileresvbl,fileresu);
12745: if((ficresvbl=fopen(fileresvbl,"w"))==NULL) {
12746: printf("Problem with variance of back (stable) prevalence resultfile: %s\n", fileresvbl);
12747: exit(0);
12748: }
12749: printf("Computing Variance-covariance of back (stable) prevalence: file '%s' ...", fileresvbl);fflush(stdout);
12750: fprintf(ficlog, "Computing Variance-covariance of back (stable) prevalence: file '%s' ...", fileresvbl);fflush(ficlog);
12751:
12752:
12753: i1=pow(2,cptcoveff);
12754: if (cptcovn < 1){i1=1;}
12755:
12756: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
12757: k=TKresult[nres];
12758: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
12759: /* for(k=1; k<=i1;k++){ */
12760: /* if(i1 != 1 && TKresult[nres]!= k) */
12761: /* continue; */
12762: fprintf(ficresvbl,"\n#****** ");
12763: printf("\n#****** ");
12764: fprintf(ficlog,"\n#****** ");
12765: for (j=1; j<= cptcovs; j++){ /* For each selected (single) quantitative value */
12766: printf(" V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][resultmodel[nres][j]]);
12767: fprintf(ficresvbl," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][resultmodel[nres][j]]);
12768: fprintf(ficlog," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][resultmodel[nres][j]]);
12769: /* for(j=1;j<=cptcoveff;j++) { */
12770: /* fprintf(ficresvbl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
12771: /* fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
12772: /* printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
12773: /* } */
12774: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
12775: /* printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
12776: /* fprintf(ficresvbl," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
12777: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
12778: }
12779: fprintf(ficresvbl,"******\n");
12780: printf("******\n");
12781: fprintf(ficlog,"******\n");
12782:
12783: varbpl=matrix(1,nlstate,(int) bage, (int) fage);
12784: oldm=oldms;savm=savms;
12785:
12786: varbrevlim(fileresvbl, ficresvbl, varbpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, bprlim, ftolpl, mobilavproj, ncvyearp, k, strstart, nres);
12787: free_matrix(varbpl,1,nlstate,(int) bage, (int)fage);
12788: /*}*/
12789: }
12790:
12791: fclose(ficresvbl);
12792: printf("done variance-covariance of back prevalence\n");fflush(stdout);
12793: fprintf(ficlog,"done variance-covariance of back prevalence\n");fflush(ficlog);
12794:
12795: } /* End of varbprlim */
12796:
12797: /************** Forecasting *****not tested NB*************/
12798: /* 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){ */
12799:
12800: /* int cpt, stepsize, hstepm, nhstepm, j,k,c, cptcod, i,h; */
12801: /* int *popage; */
12802: /* double calagedatem, agelim, kk1, kk2; */
12803: /* double *popeffectif,*popcount; */
12804: /* double ***p3mat,***tabpop,***tabpopprev; */
12805: /* /\* double ***mobaverage; *\/ */
12806: /* char filerespop[FILENAMELENGTH]; */
12807:
12808: /* tabpop= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
12809: /* tabpopprev= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
12810: /* agelim=AGESUP; */
12811: /* calagedatem=(anpyram+mpyram/12.+jpyram/365.-dateintmean)*YEARM; */
12812:
12813: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
12814:
12815:
12816: /* strcpy(filerespop,"POP_"); */
12817: /* strcat(filerespop,fileresu); */
12818: /* if((ficrespop=fopen(filerespop,"w"))==NULL) { */
12819: /* printf("Problem with forecast resultfile: %s\n", filerespop); */
12820: /* fprintf(ficlog,"Problem with forecast resultfile: %s\n", filerespop); */
12821: /* } */
12822: /* printf("Computing forecasting: result on file '%s' \n", filerespop); */
12823: /* fprintf(ficlog,"Computing forecasting: result on file '%s' \n", filerespop); */
12824:
12825: /* if (cptcoveff==0) ncodemax[cptcoveff]=1; */
12826:
12827: /* /\* if (mobilav!=0) { *\/ */
12828: /* /\* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
12829: /* /\* if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){ *\/ */
12830: /* /\* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); *\/ */
12831: /* /\* printf(" Error in movingaverage mobilav=%d\n",mobilav); *\/ */
12832: /* /\* } *\/ */
12833: /* /\* } *\/ */
12834:
12835: /* stepsize=(int) (stepm+YEARM-1)/YEARM; */
12836: /* if (stepm<=12) stepsize=1; */
12837:
12838: /* agelim=AGESUP; */
12839:
12840: /* hstepm=1; */
12841: /* hstepm=hstepm/stepm; */
12842:
12843: /* if (popforecast==1) { */
12844: /* if((ficpop=fopen(popfile,"r"))==NULL) { */
12845: /* printf("Problem with population file : %s\n",popfile);exit(0); */
12846: /* fprintf(ficlog,"Problem with population file : %s\n",popfile);exit(0); */
12847: /* } */
12848: /* popage=ivector(0,AGESUP); */
12849: /* popeffectif=vector(0,AGESUP); */
12850: /* popcount=vector(0,AGESUP); */
12851:
12852: /* i=1; */
12853: /* while ((c=fscanf(ficpop,"%d %lf\n",&popage[i],&popcount[i])) != EOF) i=i+1; */
12854:
12855: /* imx=i; */
12856: /* for (i=1; i<imx;i++) popeffectif[popage[i]]=popcount[i]; */
12857: /* } */
12858:
12859: /* for(cptcov=1,k=0;cptcov<=i2;cptcov++){ */
12860: /* for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){ */
12861: /* k=k+1; */
12862: /* fprintf(ficrespop,"\n#******"); */
12863: /* for(j=1;j<=cptcoveff;j++) { */
12864: /* fprintf(ficrespop," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */
12865: /* } */
12866: /* fprintf(ficrespop,"******\n"); */
12867: /* fprintf(ficrespop,"# Age"); */
12868: /* for(j=1; j<=nlstate+ndeath;j++) fprintf(ficrespop," P.%d",j); */
12869: /* if (popforecast==1) fprintf(ficrespop," [Population]"); */
12870:
12871: /* for (cpt=0; cpt<=0;cpt++) { */
12872: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */
12873:
12874: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */
12875: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */
12876: /* nhstepm = nhstepm/hstepm; */
12877:
12878: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
12879: /* oldm=oldms;savm=savms; */
12880: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
12881:
12882: /* for (h=0; h<=nhstepm; h++){ */
12883: /* if (h==(int) (calagedatem+YEARM*cpt)) { */
12884: /* fprintf(ficrespop,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */
12885: /* } */
12886: /* for(j=1; j<=nlstate+ndeath;j++) { */
12887: /* kk1=0.;kk2=0; */
12888: /* for(i=1; i<=nlstate;i++) { */
12889: /* if (mobilav==1) */
12890: /* kk1=kk1+p3mat[i][j][h]*mobaverage[(int)agedeb+1][i][cptcod]; */
12891: /* else { */
12892: /* kk1=kk1+p3mat[i][j][h]*probs[(int)(agedeb+1)][i][cptcod]; */
12893: /* } */
12894: /* } */
12895: /* if (h==(int)(calagedatem+12*cpt)){ */
12896: /* tabpop[(int)(agedeb)][j][cptcod]=kk1; */
12897: /* /\*fprintf(ficrespop," %.3f", kk1); */
12898: /* if (popforecast==1) fprintf(ficrespop," [%.f]", kk1*popeffectif[(int)agedeb+1]);*\/ */
12899: /* } */
12900: /* } */
12901: /* for(i=1; i<=nlstate;i++){ */
12902: /* kk1=0.; */
12903: /* for(j=1; j<=nlstate;j++){ */
12904: /* kk1= kk1+tabpop[(int)(agedeb)][j][cptcod]; */
12905: /* } */
12906: /* tabpopprev[(int)(agedeb)][i][cptcod]=tabpop[(int)(agedeb)][i][cptcod]/kk1*popeffectif[(int)(agedeb+(calagedatem+12*cpt)*hstepm/YEARM*stepm-1)]; */
12907: /* } */
12908:
12909: /* if (h==(int)(calagedatem+12*cpt)) */
12910: /* for(j=1; j<=nlstate;j++) */
12911: /* fprintf(ficrespop," %15.2f",tabpopprev[(int)(agedeb+1)][j][cptcod]); */
12912: /* } */
12913: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
12914: /* } */
12915: /* } */
12916:
12917: /* /\******\/ */
12918:
12919: /* for (cpt=1; cpt<=(anpyram1-anpyram);cpt++) { */
12920: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */
12921: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */
12922: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */
12923: /* nhstepm = nhstepm/hstepm; */
12924:
12925: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
12926: /* oldm=oldms;savm=savms; */
12927: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
12928: /* for (h=0; h<=nhstepm; h++){ */
12929: /* if (h==(int) (calagedatem+YEARM*cpt)) { */
12930: /* fprintf(ficresf,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */
12931: /* } */
12932: /* for(j=1; j<=nlstate+ndeath;j++) { */
12933: /* kk1=0.;kk2=0; */
12934: /* for(i=1; i<=nlstate;i++) { */
12935: /* kk1=kk1+p3mat[i][j][h]*tabpopprev[(int)agedeb+1][i][cptcod]; */
12936: /* } */
12937: /* if (h==(int)(calagedatem+12*cpt)) fprintf(ficresf," %15.2f", kk1); */
12938: /* } */
12939: /* } */
12940: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
12941: /* } */
12942: /* } */
12943: /* } */
12944: /* } */
12945:
12946: /* /\* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
12947:
12948: /* if (popforecast==1) { */
12949: /* free_ivector(popage,0,AGESUP); */
12950: /* free_vector(popeffectif,0,AGESUP); */
12951: /* free_vector(popcount,0,AGESUP); */
12952: /* } */
12953: /* free_ma3x(tabpop,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
12954: /* free_ma3x(tabpopprev,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
12955: /* fclose(ficrespop); */
12956: /* } /\* End of popforecast *\/ */
12957:
12958: int fileappend(FILE *fichier, char *optionfich)
12959: {
12960: if((fichier=fopen(optionfich,"a"))==NULL) {
12961: printf("Problem with file: %s\n", optionfich);
12962: fprintf(ficlog,"Problem with file: %s\n", optionfich);
12963: return (0);
12964: }
12965: fflush(fichier);
12966: return (1);
12967: }
12968:
12969:
12970: /**************** function prwizard **********************/
12971: void prwizard(int ncovmodel, int nlstate, int ndeath, char model[], FILE *ficparo)
12972: {
12973:
12974: /* Wizard to print covariance matrix template */
12975:
12976: char ca[32], cb[32];
12977: int i,j, k, li, lj, lk, ll, jj, npar, itimes;
12978: int numlinepar;
12979:
12980: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
12981: fprintf(ficparo,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
12982: for(i=1; i <=nlstate; i++){
12983: jj=0;
12984: for(j=1; j <=nlstate+ndeath; j++){
12985: if(j==i) continue;
12986: jj++;
12987: /*ca[0]= k+'a'-1;ca[1]='\0';*/
12988: printf("%1d%1d",i,j);
12989: fprintf(ficparo,"%1d%1d",i,j);
12990: for(k=1; k<=ncovmodel;k++){
12991: /* printf(" %lf",param[i][j][k]); */
12992: /* fprintf(ficparo," %lf",param[i][j][k]); */
12993: printf(" 0.");
12994: fprintf(ficparo," 0.");
12995: }
12996: printf("\n");
12997: fprintf(ficparo,"\n");
12998: }
12999: }
13000: printf("# Scales (for hessian or gradient estimation)\n");
13001: fprintf(ficparo,"# Scales (for hessian or gradient estimation)\n");
13002: npar= (nlstate+ndeath-1)*nlstate*ncovmodel; /* Number of parameters*/
13003: for(i=1; i <=nlstate; i++){
13004: jj=0;
13005: for(j=1; j <=nlstate+ndeath; j++){
13006: if(j==i) continue;
13007: jj++;
13008: fprintf(ficparo,"%1d%1d",i,j);
13009: printf("%1d%1d",i,j);
13010: fflush(stdout);
13011: for(k=1; k<=ncovmodel;k++){
13012: /* printf(" %le",delti3[i][j][k]); */
13013: /* fprintf(ficparo," %le",delti3[i][j][k]); */
13014: printf(" 0.");
13015: fprintf(ficparo," 0.");
13016: }
13017: numlinepar++;
13018: printf("\n");
13019: fprintf(ficparo,"\n");
13020: }
13021: }
13022: printf("# Covariance matrix\n");
13023: /* # 121 Var(a12)\n\ */
13024: /* # 122 Cov(b12,a12) Var(b12)\n\ */
13025: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
13026: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
13027: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
13028: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
13029: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
13030: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
13031: fflush(stdout);
13032: fprintf(ficparo,"# Covariance matrix\n");
13033: /* # 121 Var(a12)\n\ */
13034: /* # 122 Cov(b12,a12) Var(b12)\n\ */
13035: /* # ...\n\ */
13036: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
13037:
13038: for(itimes=1;itimes<=2;itimes++){
13039: jj=0;
13040: for(i=1; i <=nlstate; i++){
13041: for(j=1; j <=nlstate+ndeath; j++){
13042: if(j==i) continue;
13043: for(k=1; k<=ncovmodel;k++){
13044: jj++;
13045: ca[0]= k+'a'-1;ca[1]='\0';
13046: if(itimes==1){
13047: printf("#%1d%1d%d",i,j,k);
13048: fprintf(ficparo,"#%1d%1d%d",i,j,k);
13049: }else{
13050: printf("%1d%1d%d",i,j,k);
13051: fprintf(ficparo,"%1d%1d%d",i,j,k);
13052: /* printf(" %.5le",matcov[i][j]); */
13053: }
13054: ll=0;
13055: for(li=1;li <=nlstate; li++){
13056: for(lj=1;lj <=nlstate+ndeath; lj++){
13057: if(lj==li) continue;
13058: for(lk=1;lk<=ncovmodel;lk++){
13059: ll++;
13060: if(ll<=jj){
13061: cb[0]= lk +'a'-1;cb[1]='\0';
13062: if(ll<jj){
13063: if(itimes==1){
13064: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
13065: fprintf(ficparo," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
13066: }else{
13067: printf(" 0.");
13068: fprintf(ficparo," 0.");
13069: }
13070: }else{
13071: if(itimes==1){
13072: printf(" Var(%s%1d%1d)",ca,i,j);
13073: fprintf(ficparo," Var(%s%1d%1d)",ca,i,j);
13074: }else{
13075: printf(" 0.");
13076: fprintf(ficparo," 0.");
13077: }
13078: }
13079: }
13080: } /* end lk */
13081: } /* end lj */
13082: } /* end li */
13083: printf("\n");
13084: fprintf(ficparo,"\n");
13085: numlinepar++;
13086: } /* end k*/
13087: } /*end j */
13088: } /* end i */
13089: } /* end itimes */
13090:
13091: } /* end of prwizard */
13092: /******************* Gompertz Likelihood ******************************/
13093: double gompertz(double x[])
13094: {
13095: double A=0.0,B=0.,L=0.0,sump=0.,num=0.;
13096: int i,n=0; /* n is the size of the sample */
13097:
13098: for (i=1;i<=imx ; i++) {
13099: sump=sump+weight[i];
13100: /* sump=sump+1;*/
13101: num=num+1;
13102: }
13103: L=0.0;
13104: /* agegomp=AGEGOMP; */
13105: /* for (i=0; i<=imx; i++)
13106: 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]);*/
13107:
13108: for (i=1;i<=imx ; i++) {
13109: /* mu(a)=mu(agecomp)*exp(teta*(age-agegomp))
13110: mu(a)=x[1]*exp(x[2]*(age-agegomp)); x[1] and x[2] are per year.
13111: * L= Product mu(agedeces)exp(-\int_ageexam^agedc mu(u) du ) for a death between agedc (in month)
13112: * and agedc +1 month, cens[i]=0: log(x[1]/YEARM)
13113: * +
13114: * exp(-\int_ageexam^agecens mu(u) du ) when censored, cens[i]=1
13115: */
13116: if (wav[i] > 1 || agedc[i] < AGESUP) {
13117: if (cens[i] == 1){
13118: A=-x[1]/(x[2])*(exp(x[2]*(agecens[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)));
13119: } else if (cens[i] == 0){
13120: A=-x[1]/(x[2])*(exp(x[2]*(agedc[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)))
13121: +log(x[1]/YEARM) +x[2]*(agedc[i]-agegomp)+log(YEARM);
13122: } else
13123: printf("Gompertz cens[%d] neither 1 nor 0\n",i);
13124: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
13125: L=L+A*weight[i];
13126: /* 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]);*/
13127: }
13128: }
13129:
13130: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
13131:
13132: return -2*L*num/sump;
13133: }
13134:
13135: #ifdef GSL
13136: /******************* Gompertz_f Likelihood ******************************/
13137: double gompertz_f(const gsl_vector *v, void *params)
13138: {
13139: double A=0.,B=0.,LL=0.0,sump=0.,num=0.;
13140: double *x= (double *) v->data;
13141: int i,n=0; /* n is the size of the sample */
13142:
13143: for (i=0;i<=imx-1 ; i++) {
13144: sump=sump+weight[i];
13145: /* sump=sump+1;*/
13146: num=num+1;
13147: }
13148:
13149:
13150: /* for (i=0; i<=imx; i++)
13151: 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]);*/
13152: printf("x[0]=%lf x[1]=%lf\n",x[0],x[1]);
13153: for (i=1;i<=imx ; i++)
13154: {
13155: if (cens[i] == 1 && wav[i]>1)
13156: A=-x[0]/(x[1])*(exp(x[1]*(agecens[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)));
13157:
13158: if (cens[i] == 0 && wav[i]>1)
13159: A=-x[0]/(x[1])*(exp(x[1]*(agedc[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)))
13160: +log(x[0]/YEARM)+x[1]*(agedc[i]-agegomp)+log(YEARM);
13161:
13162: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
13163: if (wav[i] > 1 ) { /* ??? */
13164: LL=LL+A*weight[i];
13165: /* 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]);*/
13166: }
13167: }
13168:
13169: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
13170: printf("x[0]=%lf x[1]=%lf -2*LL*num/sump=%lf\n",x[0],x[1],-2*LL*num/sump);
13171:
13172: return -2*LL*num/sump;
13173: }
13174: #endif
13175:
13176: /******************* Printing html file ***********/
13177: void printinghtmlmort(char fileresu[], char title[], char datafile[], int firstpass, \
13178: int lastpass, int stepm, int weightopt, char model[],\
13179: int imx, double p[],double **matcov,double agemortsup){
13180: int i,k;
13181:
13182: fprintf(fichtm,"<ul><li><h4>Result files </h4>\n Force of mortality. Parameters of the Gompertz fit (with confidence interval in brackets):<br>");
13183: fprintf(fichtm," mu(age) =%lf*exp(%lf*(age-%d)) per year<br><br>",p[1],p[2],agegomp);
13184: for (i=1;i<=2;i++)
13185: 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]));
13186: fprintf(fichtm,"<br><br><img src=\"graphmort.svg\">");
13187: fprintf(fichtm,"</ul>");
13188:
13189: fprintf(fichtm,"<ul><li><h4>Life table</h4>\n <br>");
13190:
13191: 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>");
13192:
13193: for (k=agegomp;k<(agemortsup-2);k++)
13194: 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]);
13195:
13196:
13197: fflush(fichtm);
13198: }
13199:
13200: /******************* Gnuplot file **************/
13201: void printinggnuplotmort(char fileresu[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){
13202:
13203: char dirfileres[132],optfileres[132];
13204:
13205: int ng;
13206:
13207:
13208: /*#ifdef windows */
13209: fprintf(ficgp,"cd \"%s\" \n",pathc);
13210: /*#endif */
13211:
13212:
13213: strcpy(dirfileres,optionfilefiname);
13214: strcpy(optfileres,"vpl");
13215: fprintf(ficgp,"set out \"graphmort.svg\"\n ");
13216: fprintf(ficgp,"set xlabel \"Age\"\n set ylabel \"Force of mortality (per year)\" \n ");
13217: fprintf(ficgp, "set ter svg size 640, 480\n set log y\n");
13218: /* fprintf(ficgp, "set size 0.65,0.65\n"); */
13219: fprintf(ficgp,"plot [%d:100] %lf*exp(%lf*(x-%d))",agegomp,p[1],p[2],agegomp);
13220:
13221: }
13222:
13223: int readdata(char datafile[], int firstobs, int lastobs, int *imax)
13224: {
13225:
13226: /*-------- data file ----------*/
13227: FILE *fic;
13228: char dummy[]=" ";
13229: int i=0, j=0, n=0, iv=0, v;
13230: int lstra;
13231: int linei, month, year,iout;
13232: int noffset=0; /* This is the offset if BOM data file */
13233: char line[MAXLINE], linetmp[MAXLINE];
13234: char stra[MAXLINE], strb[MAXLINE];
13235: char *stratrunc;
13236:
13237: /* DummyV=ivector(-1,NCOVMAX); /\* 1 to 3 *\/ */
13238: /* FixedV=ivector(-1,NCOVMAX); /\* 1 to 3 *\/ */
13239:
13240: ncovcolt=ncovcol+nqv+ntv+nqtv; /* total of covariates in the data, not in the model equation */
13241:
13242: if((fic=fopen(datafile,"r"))==NULL) {
13243: printf("Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(stdout);
13244: fprintf(ficlog,"Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(ficlog);return 1;
13245: }
13246:
13247: /* Is it a BOM UTF-8 Windows file? */
13248: /* First data line */
13249: linei=0;
13250: while(fgets(line, MAXLINE, fic)) {
13251: noffset=0;
13252: if( line[0] == (char)0xEF && line[1] == (char)0xBB) /* EF BB BF */
13253: {
13254: noffset=noffset+3;
13255: printf("# Data file '%s' is an UTF8 BOM file, please convert to UTF8 or ascii file and rerun.\n",datafile);fflush(stdout);
13256: fprintf(ficlog,"# Data file '%s' is an UTF8 BOM file, please convert to UTF8 or ascii file and rerun.\n",datafile);
13257: fflush(ficlog); return 1;
13258: }
13259: /* else if( line[0] == (char)0xFE && line[1] == (char)0xFF)*/
13260: else if( line[0] == (char)0xFF && line[1] == (char)0xFE)
13261: {
13262: noffset=noffset+2;
13263: 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);
13264: 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);
13265: fflush(ficlog); return 1;
13266: }
13267: else if( line[0] == 0 && line[1] == 0)
13268: {
13269: if( line[2] == (char)0xFE && line[3] == (char)0xFF){
13270: noffset=noffset+4;
13271: 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);
13272: 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);
13273: fflush(ficlog); return 1;
13274: }
13275: } else{
13276: ;/*printf(" Not a BOM file\n");*/
13277: }
13278: /* If line starts with a # it is a comment */
13279: if (line[noffset] == '#') {
13280: linei=linei+1;
13281: break;
13282: }else{
13283: break;
13284: }
13285: }
13286: fclose(fic);
13287: if((fic=fopen(datafile,"r"))==NULL) {
13288: printf("Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(stdout);
13289: fprintf(ficlog,"Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(ficlog);return 1;
13290: }
13291: /* Not a Bom file */
13292:
13293: i=1;
13294: while ((fgets(line, MAXLINE, fic) != NULL) &&((i >= firstobs) && (i <=lastobs))) {
13295: linei=linei+1;
13296: for(j=strlen(line); j>=0;j--){ /* Untabifies line */
13297: if(line[j] == '\t')
13298: line[j] = ' ';
13299: }
13300: for(j=strlen(line)-1; (line[j]==' ')||(line[j]==10)||(line[j]==13);j--){
13301: ;
13302: };
13303: line[j+1]=0; /* Trims blanks at end of line */
13304: if(line[0]=='#'){
13305: fprintf(ficlog,"Comment line\n%s\n",line);
13306: printf("Comment line\n%s\n",line);
13307: continue;
13308: }
13309: trimbb(linetmp,line); /* Trims multiple blanks in line */
13310: strcpy(line, linetmp);
13311:
13312: /* Loops on waves */
13313: for (j=maxwav;j>=1;j--){
13314: for (iv=nqtv;iv>=1;iv--){ /* Loop on time varying quantitative variables */
13315: cutv(stra, strb, line, ' ');
13316: if(strb[0]=='.') { /* Missing value */
13317: lval=-1;
13318: cotqvar[j][iv][i]=-1; /* 0.0/0.0 */
13319: cotvar[j][ncovcol+nqv+ntv+iv][i]=-1; /* For performance reasons */
13320: if(isalpha(strb[1])) { /* .m or .d Really Missing value */
13321: 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);
13322: 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);
13323: return 1;
13324: }
13325: }else{
13326: errno=0;
13327: /* what_kind_of_number(strb); */
13328: dval=strtod(strb,&endptr);
13329: /* if( strb[0]=='\0' || (*endptr != '\0')){ */
13330: /* if(strb != endptr && *endptr == '\0') */
13331: /* dval=dlval; */
13332: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
13333: if( strb[0]=='\0' || (*endptr != '\0')){
13334: 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);
13335: 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);
13336: return 1;
13337: }
13338: cotqvar[j][iv][i]=dval;
13339: cotvar[j][ncovcol+nqv+ntv+iv][i]=dval; /* because cotvar starts now at first ntv */
13340: }
13341: strcpy(line,stra);
13342: }/* end loop ntqv */
13343:
13344: for (iv=ntv;iv>=1;iv--){ /* Loop on time varying dummies */
13345: cutv(stra, strb, line, ' ');
13346: if(strb[0]=='.') { /* Missing value */
13347: lval=-1;
13348: }else{
13349: errno=0;
13350: lval=strtol(strb,&endptr,10);
13351: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
13352: if( strb[0]=='\0' || (*endptr != '\0')){
13353: 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);
13354: 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);
13355: return 1;
13356: }
13357: }
13358: if(lval <-1 || lval >1){
13359: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
13360: Should be a value of %d(nth) covariate of wave %d (0 should be the value for the reference and 1\n \
13361: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
13362: For example, for multinomial values like 1, 2 and 3,\n \
13363: build V1=0 V2=0 for the reference value (1),\n \
13364: V1=1 V2=0 for (2) \n \
13365: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
13366: output of IMaCh is often meaningless.\n \
13367: Exiting.\n",lval,linei, i,line,iv,j);
13368: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
13369: Should be a value of %d(nth) covariate of wave %d (0 should be the value for the reference and 1\n \
13370: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
13371: For example, for multinomial values like 1, 2 and 3,\n \
13372: build V1=0 V2=0 for the reference value (1),\n \
13373: V1=1 V2=0 for (2) \n \
13374: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
13375: output of IMaCh is often meaningless.\n \
13376: Exiting.\n",lval,linei, i,line,iv,j);fflush(ficlog);
13377: return 1;
13378: }
13379: cotvar[j][ncovcol+nqv+iv][i]=(double)(lval);
13380: strcpy(line,stra);
13381: }/* end loop ntv */
13382:
13383: /* Statuses at wave */
13384: cutv(stra, strb, line, ' ');
13385: if(strb[0]=='.') { /* Missing value */
13386: lval=-1;
13387: }else{
13388: errno=0;
13389: lval=strtol(strb,&endptr,10);
13390: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
13391: if( strb[0]=='\0' || (*endptr != '\0' )){
13392: 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);
13393: 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);
13394: return 1;
13395: }else if( lval==0 || lval > nlstate+ndeath){
13396: 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);
13397: 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);
13398: return 1;
13399: }
13400: }
13401:
13402: s[j][i]=lval;
13403:
13404: /* Date of Interview */
13405: strcpy(line,stra);
13406: cutv(stra, strb,line,' ');
13407: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
13408: }
13409: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
13410: month=99;
13411: year=9999;
13412: }else{
13413: 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);
13414: 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);
13415: return 1;
13416: }
13417: anint[j][i]= (double) year;
13418: mint[j][i]= (double)month;
13419: /* if( (int)anint[j][i]+ (int)(mint[j][i])/12. < (int) (moisnais[i]/12.+annais[i])){ */
13420: /* 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]); */
13421: /* 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]); */
13422: /* } */
13423: strcpy(line,stra);
13424: } /* End loop on waves */
13425:
13426: /* Date of death */
13427: cutv(stra, strb,line,' ');
13428: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
13429: }
13430: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
13431: month=99;
13432: year=9999;
13433: }else{
13434: 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);
13435: 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);
13436: return 1;
13437: }
13438: andc[i]=(double) year;
13439: moisdc[i]=(double) month;
13440: strcpy(line,stra);
13441:
13442: /* Date of birth */
13443: cutv(stra, strb,line,' ');
13444: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
13445: }
13446: else if( (iout=sscanf(strb,"%s.", dummy)) != 0){
13447: month=99;
13448: year=9999;
13449: }else{
13450: 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);
13451: 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);
13452: return 1;
13453: }
13454: if (year==9999) {
13455: 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);
13456: 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);
13457: return 1;
13458:
13459: }
13460: annais[i]=(double)(year);
13461: moisnais[i]=(double)(month);
13462: for (j=1;j<=maxwav;j++){
13463: if( (int)anint[j][i]+ (int)(mint[j][i])/12. < (int) (moisnais[i]/12.+annais[i])){
13464: 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]);
13465: 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]);
13466: }
13467: }
13468:
13469: strcpy(line,stra);
13470:
13471: /* Sample weight */
13472: cutv(stra, strb,line,' ');
13473: errno=0;
13474: dval=strtod(strb,&endptr);
13475: if( strb[0]=='\0' || (*endptr != '\0')){
13476: printf("Error reading data around '%f' at line number %d, \"%s\" for individual %d\nShould be a weight. Exiting.\n",dval, i,line,linei);
13477: 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);
13478: fflush(ficlog);
13479: return 1;
13480: }
13481: weight[i]=dval;
13482: strcpy(line,stra);
13483:
13484: for (iv=nqv;iv>=1;iv--){ /* Loop on fixed quantitative variables */
13485: cutv(stra, strb, line, ' ');
13486: if(strb[0]=='.') { /* Missing value */
13487: lval=-1;
13488: coqvar[iv][i]=NAN;
13489: covar[ncovcol+iv][i]=NAN; /* including qvar in standard covar for performance reasons */
13490: }else{
13491: errno=0;
13492: /* what_kind_of_number(strb); */
13493: dval=strtod(strb,&endptr);
13494: /* if(strb != endptr && *endptr == '\0') */
13495: /* dval=dlval; */
13496: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
13497: if( strb[0]=='\0' || (*endptr != '\0')){
13498: 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);
13499: 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);
13500: return 1;
13501: }
13502: coqvar[iv][i]=dval;
13503: covar[ncovcol+iv][i]=dval; /* including qvar in standard covar for performance reasons */
13504: }
13505: strcpy(line,stra);
13506: }/* end loop nqv */
13507:
13508: /* Covariate values */
13509: for (j=ncovcol;j>=1;j--){
13510: cutv(stra, strb,line,' ');
13511: if(strb[0]=='.') { /* Missing covariate value */
13512: lval=-1;
13513: }else{
13514: errno=0;
13515: lval=strtol(strb,&endptr,10);
13516: if( strb[0]=='\0' || (*endptr != '\0')){
13517: 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);
13518: 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);
13519: return 1;
13520: }
13521: }
13522: if(lval <-1 || lval >1){
13523: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
13524: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
13525: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
13526: For example, for multinomial values like 1, 2 and 3,\n \
13527: build V1=0 V2=0 for the reference value (1),\n \
13528: V1=1 V2=0 for (2) \n \
13529: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
13530: output of IMaCh is often meaningless.\n \
13531: Exiting.\n",lval,linei, i,line,j);
13532: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
13533: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
13534: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
13535: For example, for multinomial values like 1, 2 and 3,\n \
13536: build V1=0 V2=0 for the reference value (1),\n \
13537: V1=1 V2=0 for (2) \n \
13538: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
13539: output of IMaCh is often meaningless.\n \
13540: Exiting.\n",lval,linei, i,line,j);fflush(ficlog);
13541: return 1;
13542: }
13543: covar[j][i]=(double)(lval);
13544: strcpy(line,stra);
13545: }
13546: lstra=strlen(stra);
13547:
13548: if(lstra > 9){ /* More than 2**32 or max of what printf can write with %ld */
13549: stratrunc = &(stra[lstra-9]);
13550: num[i]=atol(stratrunc);
13551: }
13552: else
13553: num[i]=atol(stra);
13554: /*if((s[2][i]==2) && (s[3][i]==-1)&&(s[4][i]==9)){
13555: 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;}*/
13556:
13557: i=i+1;
13558: } /* End loop reading data */
13559:
13560: *imax=i-1; /* Number of individuals */
13561: fclose(fic);
13562:
13563: return (0);
13564: /* endread: */
13565: printf("Exiting readdata: ");
13566: fclose(fic);
13567: return (1);
13568: }
13569:
13570: void removefirstspace(char **stri){/*, char stro[]) {*/
13571: char *p1 = *stri, *p2 = *stri;
13572: while (*p2 == ' ')
13573: p2++;
13574: /* while ((*p1++ = *p2++) !=0) */
13575: /* ; */
13576: /* do */
13577: /* while (*p2 == ' ') */
13578: /* p2++; */
13579: /* while (*p1++ == *p2++); */
13580: *stri=p2;
13581: }
13582:
13583: int decoderesult( char resultline[], int nres)
13584: /**< This routine decode one result line and returns the combination # of dummy covariates only **/
13585: {
13586: int j=0, k=0, k1=0, k2=0, k3=0, k4=0, match=0, k2q=0, k3q=0, k4q=0;
13587: char resultsav[MAXLINE];
13588: /* int resultmodel[MAXLINE]; */
13589: /* int modelresult[MAXLINE]; */
13590: char stra[80], strb[80], strc[80], strd[80],stre[80];
13591:
13592: removefirstspace(&resultline);
13593: printf("decoderesult:%s\n",resultline);
13594:
13595: strcpy(resultsav,resultline);
13596: /* printf("Decoderesult resultsav=\"%s\" resultline=\"%s\"\n", resultsav, resultline); */
13597: if (strlen(resultsav) >1){
13598: j=nbocc(resultsav,'='); /**< j=Number of covariate values'=' in this resultline */
13599: }
13600: if(j == 0){ /* Resultline but no = */
13601: TKresult[nres]=0; /* Combination for the nresult and the model */
13602: return (0);
13603: }
13604: if( j != cptcovs ){ /* Be careful if a variable is in a product but not single */
13605: 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);
13606: 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);
13607: /* return 1;*/
13608: }
13609: for(k=1; k<=j;k++){ /* Loop on any covariate of the RESULT LINE */
13610: if(nbocc(resultsav,'=') >1){
13611: 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" */
13612: /* If resultsav= "V4= 1 V5=25.1 V3=0" with a blank then strb="V4=" and stra="1 V5=25.1 V3=0" */
13613: cutl(strc,strd,strb,'='); /* strb:"V4=1" strc="1" strd="V4" */
13614: /* If a blank, then strc="V4=" and strd='\0' */
13615: if(strc[0]=='\0'){
13616: printf("Error in resultline, probably a blank after the \"%s\", \"result:%s\", stra=\"%s\" resultsav=\"%s\"\n",strb,resultline, stra, resultsav);
13617: fprintf(ficlog,"Error in resultline, probably a blank after the \"V%s=\", resultline=%s\n",strb,resultline);
13618: return 1;
13619: }
13620: }else
13621: cutl(strc,strd,resultsav,'=');
13622: Tvalsel[k]=atof(strc); /* 1 */ /* Tvalsel of k is the float value of the kth covariate appearing in this result line */
13623:
13624: cutl(strc,stre,strd,'V'); /* strd='V4' strc=4 stre='V' */;
13625: Tvarsel[k]=atoi(strc); /* 4 */ /* Tvarsel is the id of the kth covariate in the result line Tvarsel[1] in "V4=1.." is 4.*/
13626: /* Typevarsel[k]=1; /\* 1 for age product *\/ */
13627: /* cptcovsel++; */
13628: if (nbocc(stra,'=') >0)
13629: strcpy(resultsav,stra); /* and analyzes it */
13630: }
13631: /* Checking for missing or useless values in comparison of current model needs */
13632: /* Feeds resultmodel[nres][k1]=k2 for k1th product covariate with age in the model equation fed by the index k2 of the resutline*/
13633: for(k1=1; k1<= cptcovt ;k1++){ /* Loop on MODEL LINE V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
13634: if(Typevar[k1]==0){ /* Single covariate in model */
13635: /* 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product */
13636: match=0;
13637: for(k2=1; k2 <=j;k2++){/* Loop on resultline. In result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
13638: if(Tvar[k1]==Tvarsel[k2]) {/* Tvar is coming from the model, Tvarsel from the result. Tvar[1]=5 == Tvarsel[2]=5 */
13639: modelresult[nres][k2]=k1;/* modelresult[2]=1 modelresult[1]=2 modelresult[3]=3 modelresult[6]=4 modelresult[9]=5 */
13640: match=1; /* modelresult of k2 variable of resultline is identical to k1 variable of the model good */
13641: break;
13642: }
13643: }
13644: if(match == 0){
13645: 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]);
13646: 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);
13647: return 1;
13648: }
13649: }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*/
13650: /* We feed resultmodel[k1]=k2; */
13651: match=0;
13652: 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 */
13653: if(Tvar[k1]==Tvarsel[k2]) {/* Tvar is coming from the model, Tvarsel from the result. Tvar[1]=5 == Tvarsel[2]=5 */
13654: 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 */
13655: resultmodel[nres][k1]=k2; /* Added here */
13656: /* printf("Decoderesult first modelresult[k2=%d]=%d (k1) V%d*AGE\n",k2,k1,Tvar[k1]); */
13657: match=1; /* modelresult of k2 variable of resultline is identical to k1 variable of the model good */
13658: break;
13659: }
13660: }
13661: if(match == 0){
13662: 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]);
13663: 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]);
13664: return 1;
13665: }
13666: }else if(Typevar[k1]==2 || Typevar[k1]==3){ /* Product with or without age. We want to get the position in the resultline of the product in the model line*/
13667: /* resultmodel[nres][of such a Vn * Vm product k1] is not unique, so can't exist, we feed Tvard[k1][1] and [2] */
13668: match=0;
13669: /* 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]); */
13670: for(k2=1; k2 <=j;k2++){/* Loop on resultline. In result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
13671: if(Tvardk[k1][1]==Tvarsel[k2]) {/* Tvardk is coming from the model, Tvarsel from the result. Tvar[1]=5 == Tvarsel[2]=5 */
13672: /* modelresult[k2]=k1; */
13673: /* printf("Decoderesult first Product modelresult[k2=%d]=%d (k1) V%d * \n",k2,k1,Tvarsel[k2]); */
13674: match=1; /* modelresult of k2 variable of resultline is identical to k1 variable of the model good */
13675: }
13676: }
13677: if(match == 0){
13678: printf("Error in result line (Product without age first variable or double product with age): V%d is missing in result: %s according to model=1+age+%s\n",Tvardk[k1][1], resultline, model);
13679: fprintf(ficlog,"Error in result line (Product without age first variable or double product with age): V%d is missing in result: %s according to model=1+age+%s\n",Tvardk[k1][1], resultline, model);
13680: return 1;
13681: }
13682: match=0;
13683: for(k2=1; k2 <=j;k2++){/* Loop on resultline. In result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
13684: if(Tvardk[k1][2]==Tvarsel[k2]) {/* Tvardk is coming from the model, Tvarsel from the result. Tvar[1]=5 == Tvarsel[2]=5 */
13685: /* modelresult[k2]=k1;*/
13686: /* printf("Decoderesult second Product modelresult[k2=%d]=%d (k1) * V%d \n ",k2,k1,Tvarsel[k2]); */
13687: match=1; /* modelresult of k2 variable of resultline is identical to k1 variable of the model good */
13688: break;
13689: }
13690: }
13691: if(match == 0){
13692: printf("Error in result line (Product without age second variable or double product with age): V%d is missing in result: %s according to model=1+age+%s\n",Tvardk[k1][2], resultline, model);
13693: fprintf(ficlog,"Error in result line (Product without age second variable or double product with age): V%d is missing in result : %s according to model=1+age+%s\n",Tvardk[k1][2], resultline, model);
13694: return 1;
13695: }
13696: }/* End of testing */
13697: }/* End loop cptcovt */
13698: /* Checking for missing or useless values in comparison of current model needs */
13699: /* Feeds resultmodel[nres][k1]=k2 for single covariate (k1) in the model equation */
13700: 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)
13701: * Loop on resultline variables: result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
13702: match=0;
13703: for(k1=1; k1<= cptcovt ;k1++){ /* loop on model: model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
13704: if(Typevar[k1]==0){ /* Single only */
13705: if(Tvar[k1]==Tvarsel[k2]) { /* Tvar[2]=4 == Tvarsel[1]=4 What if a product? */
13706: 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 */
13707: 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 */
13708: ++match;
13709: }
13710: }
13711: }
13712: if(match == 0){
13713: printf("Error in result line: variable V%d is missing in model; result: %s, model=1+age+%s\n",Tvarsel[k2], resultline, model);
13714: fprintf(ficlog,"Error in result line: variable V%d is missing in model; result: %s, model=1+age+%s\n",Tvarsel[k2], resultline, model);
13715: return 1;
13716: }else if(match > 1){
13717: printf("Error in result line: %d doubled; result: %s, model=1+age+%s\n",k2, resultline, model);
13718: fprintf(ficlog,"Error in result line: %d doubled; result: %s, model=1+age+%s\n",k2, resultline, model);
13719: return 1;
13720: }
13721: }
13722: /* cptcovres=j /\* Number of variables in the resultline is equal to cptcovs and thus useless *\/ */
13723: /* We need to deduce which combination number is chosen and save quantitative values */
13724: /* model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
13725: /* nres=1st result line: V4=1 V5=25.1 V3=0 V2=8 V1=1 */
13726: /* 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*/
13727: /* nres=2nd result line: V4=1 V5=24.1 V3=1 V2=8 V1=0 */
13728: /* should give a combination of dummy V4=1, V3=1, V1=0 => V4*2**(0) + V3*2**(1) + V1*2**(2) = 3 + (1offset) = 4*/
13729: /* 1 0 0 0 */
13730: /* 2 1 0 0 */
13731: /* 3 0 1 0 */
13732: /* 4 1 1 0 */ /* V4=1, V3=1, V1=0 (nres=2)*/
13733: /* 5 0 0 1 */
13734: /* 6 1 0 1 */ /* V4=1, V3=0, V1=1 (nres=1)*/
13735: /* 7 0 1 1 */
13736: /* 8 1 1 1 */
13737: /* V(Tvresult)=Tresult V4=1 V3=0 V1=1 Tresult[nres=1][2]=0 */
13738: /* V(Tvqresult)=Tqresult V5=25.1 V2=8 Tqresult[nres=1][1]=25.1 */
13739: /* V5*age V5 known which value for nres? */
13740: /* Tqinvresult[2]=8 Tqinvresult[1]=25.1 */
13741: 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.
13742: * loop on position k1 in the MODEL LINE */
13743: /* k counting number of combination of single dummies in the equation model */
13744: /* k4 counting single dummies in the equation model */
13745: /* k4q counting single quantitatives in the equation model */
13746: if( Dummy[k1]==0 && Typevar[k1]==0 ){ /* Dummy and Single, fixed or timevarying, k1 is sorting according to MODEL, but k3 to resultline */
13747: /* 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) */
13748: /* modelresult[k3]=k1: k3th position in the result line corresponds to the k1 position in the model line (doesn't work with products)*/
13749: /* Value in the (current nres) resultline of the variable at the k1th position in the model equation resultmodel[nres][k1]= k3 */
13750: /* resultmodel[nres][k1]=k3: k1th position in the model correspond to the k3 position in the resultline */
13751: /* k3 is the position in the nres result line of the k1th variable of the model equation */
13752: /* Tvarsel[k3]: Name of the variable at the k3th position in the result line. */
13753: /* Tvalsel[k3]: Value of the variable at the k3th position in the result line. */
13754: /* Tresult[nres][result_position]= value of the dummy variable at the result_position in the nres resultline */
13755: /* Tvresult[nres][result_position]= name of the dummy variable at the result_position in the nres resultline */
13756: /* Tinvresult[nres][Name of a dummy variable]= value of the variable in the result line */
13757: /* TinvDoQresult[nres][Name of a Dummy or Q variable]= value of the variable in the result line */
13758: k3= resultmodel[nres][k1]; /* From position k1 in model get position k3 in result line */
13759: /* nres=1 k1=2 resultmodel[2(V4)] = 1=k3 ; k1=3 resultmodel[3(V3)] = 2=k3*/
13760: 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)*/
13761: k+=Tvalsel[k3]*pow(2,k4); /* nres=1 k1=2 Tvalsel[1]=1 (V4=1); k1=3 k3=2 Tvalsel[2]=0 (V3=0) */
13762: TinvDoQresult[nres][(int)Tvarsel[k3]]=Tvalsel[k3]; /* TinvDoQresult[nres][Name]=Value; stores the value into the name of the variable. */
13763: /* Tinvresult[nres][4]=1 */
13764: /* Tresult[nres][k4+1]=Tvalsel[k3];/\* Tresult[nres=2][1]=1(V4=1) Tresult[nres=2][2]=0(V3=0) *\/ */
13765: Tresult[nres][k3]=Tvalsel[k3];/* Tresult[nres=2][1]=1(V4=1) Tresult[nres=2][2]=0(V3=0) */
13766: /* Tvresult[nres][k4+1]=(int)Tvarsel[k3];/\* Tvresult[nres][1]=4 Tvresult[nres][3]=1 *\/ */
13767: Tvresult[nres][k3]=(int)Tvarsel[k3];/* Tvresult[nres][1]=4 Tvresult[nres][3]=1 */
13768: Tinvresult[nres][(int)Tvarsel[k3]]=Tvalsel[k3]; /* Tinvresult[nres][4]=1 */
13769: precov[nres][k1]=Tvalsel[k3]; /* Value from resultline of the variable at the k1 position in the model */
13770: /* 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); */
13771: k4++;;
13772: }else if( Dummy[k1]==1 && Typevar[k1]==0 ){ /* Quantitative and single */
13773: /* Tqresult[nres][result_position]= value of the variable at the result_position in the nres resultline */
13774: /* Tvqresult[nres][result_position]= id of the variable at the result_position in the nres resultline */
13775: /* Tqinvresult[nres][Name of a quantitative variable]= value of the variable in the result line */
13776: k3q= resultmodel[nres][k1]; /* resultmodel[1(V5)] = 5 =k3q */
13777: k2q=(int)Tvarsel[k3q]; /* Name of variable at k3q th position in the resultline */
13778: /* Tvarsel[resultmodel[1]]= Tvarsel[1] = 4=k2 */
13779: /* Tqresult[nres][k4q+1]=Tvalsel[k3q]; /\* Tqresult[nres][1]=25.1 *\/ */
13780: /* Tvresult[nres][k4q+1]=(int)Tvarsel[k3q];/\* Tvresult[nres][1]=4 Tvresult[nres][3]=1 *\/ */
13781: /* Tvqresult[nres][k4q+1]=(int)Tvarsel[k3q]; /\* Tvqresult[nres][1]=5 *\/ */
13782: Tqresult[nres][k3q]=Tvalsel[k3q]; /* Tqresult[nres][1]=25.1 */
13783: Tvresult[nres][k3q]=(int)Tvarsel[k3q];/* Tvresult[nres][1]=4 Tvresult[nres][3]=1 */
13784: Tvqresult[nres][k3q]=(int)Tvarsel[k3q]; /* Tvqresult[nres][1]=5 */
13785: Tqinvresult[nres][(int)Tvarsel[k3q]]=Tvalsel[k3q]; /* Tqinvresult[nres][5]=25.1 */
13786: TinvDoQresult[nres][(int)Tvarsel[k3q]]=Tvalsel[k3q]; /* Tqinvresult[nres][5]=25.1 */
13787: precov[nres][k1]=Tvalsel[k3q];
13788: /* 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]); */
13789: k4q++;;
13790: }else if( Dummy[k1]==2 ){ /* For dummy with age product */
13791: /* Tvar[k1]; */ /* Age variable */
13792: /* Wrong we want the value of variable name Tvar[k1] */
13793:
13794: k3= resultmodel[nres][k1]; /* nres=1 k1=2 resultmodel[2(V4)] = 1=k3 ; k1=3 resultmodel[3(V3)] = 2=k3*/
13795: 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)*/
13796: TinvDoQresult[nres][(int)Tvarsel[k3]]=Tvalsel[k3]; /* TinvDoQresult[nres][4]=1 */
13797: precov[nres][k1]=Tvalsel[k3];
13798: /* 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]); */
13799: }else if( Dummy[k1]==3 ){ /* For quant with age product */
13800: k3q= resultmodel[nres][k1]; /* resultmodel[1(V5)] = 25.1=k3q */
13801: k2q=(int)Tvarsel[k3q]; /* Tvarsel[resultmodel[1]]= Tvarsel[1] = 4=k2 */
13802: TinvDoQresult[nres][(int)Tvarsel[k3q]]=Tvalsel[k3q]; /* TinvDoQresult[nres][5]=25.1 */
13803: precov[nres][k1]=Tvalsel[k3q];
13804: /* 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]); */
13805: }else if(Typevar[k1]==2 || Typevar[k1]==3 ){ /* For product quant or dummy (with or without age) */
13806: precov[nres][k1]=TinvDoQresult[nres][Tvardk[k1][1]] * TinvDoQresult[nres][Tvardk[k1][2]];
13807: /* 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]]); */
13808: }else{
13809: printf("Error Decoderesult probably a product Dummy[%d]==%d && Typevar[%d]==%d\n", k1, Dummy[k1], k1, Typevar[k1]);
13810: fprintf(ficlog,"Error Decoderesult probably a product Dummy[%d]==%d && Typevar[%d]==%d\n", k1, Dummy[k1], k1, Typevar[k1]);
13811: }
13812: }
13813:
13814: TKresult[nres]=++k; /* Number of combinations of dummies for the nresult and the model =Tvalsel[k3]*pow(2,k4) + 1*/
13815: return (0);
13816: }
13817:
13818: int decodemodel( char model[], int lastobs)
13819: /**< This routine decodes the model and returns:
13820: * Model V1+V2+V3+V8+V7*V8+V5*V6+V8*age+V3*age+age*age
13821: * - nagesqr = 1 if age*age in the model, otherwise 0.
13822: * - cptcovt total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age
13823: * - cptcovn or number of covariates k of the models excluding age*products =6 and age*age
13824: * - cptcovage number of covariates with age*products =2
13825: * - cptcovs number of simple covariates
13826: * ncovcolt=ncovcol+nqv+ntv+nqtv total of covariates in the data, not in the model equation
13827: * - 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
13828: * which is a new column after the 9 (ncovcol+nqv+ntv+nqtv) variables.
13829: * - if k is a product Vn*Vm, covar[k][i] is filled with correct values for each individual
13830: * - Tprod[l] gives the kth covariates of the product Vn*Vm l=1 to cptcovprod-cptcovage
13831: * Tprod[1]@2 {5, 6}: position of first product V7*V8 is 5, and second V5*V6 is 6.
13832: * - Tvard[k] p Tvard[1][1]@4 {7, 8, 5, 6} for V7*V8 and V5*V6 .
13833: */
13834: /* V2+V1+V4+V3*age Tvar[4]=3 ; V1+V2*age Tvar[2]=2; V1+V1*age Tvar[2]=1, Tage[1]=2 */
13835: {
13836: int i, j, k, ks, v;
13837: int n,m;
13838: int j1, k1, k11, k12, k2, k3, k4;
13839: char modelsav[300];
13840: char stra[300], strb[300], strc[300], strd[300],stre[300],strf[300];
13841: char *strpt;
13842: int **existcomb;
13843:
13844: existcomb=imatrix(1,NCOVMAX,1,NCOVMAX);
13845: for(i=1;i<=NCOVMAX;i++)
13846: for(j=1;j<=NCOVMAX;j++)
13847: existcomb[i][j]=0;
13848:
13849: /*removespace(model);*/
13850: if (strlen(model) >1){ /* If there is at least 1 covariate */
13851: j=0, j1=0, k1=0, k12=0, k2=-1, ks=0, cptcovn=0;
13852: if (strstr(model,"AGE") !=0){
13853: printf("Error. AGE must be in lower case 'age' model=1+age+%s. ",model);
13854: fprintf(ficlog,"Error. AGE must be in lower case model=1+age+%s. ",model);fflush(ficlog);
13855: return 1;
13856: }
13857: if (strstr(model,"v") !=0){
13858: printf("Error. 'v' must be in upper case 'V' model=1+age+%s ",model);
13859: fprintf(ficlog,"Error. 'v' must be in upper case model=1+age+%s ",model);fflush(ficlog);
13860: return 1;
13861: }
13862: strcpy(modelsav,model);
13863: if ((strpt=strstr(model,"age*age")) !=0){
13864: printf(" strpt=%s, model=1+age+%s\n",strpt, model);
13865: if(strpt != model){
13866: printf("Error in model: 'model=1+age+%s'; 'age*age' should in first place before other covariates\n \
13867: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
13868: corresponding column of parameters.\n",model);
13869: fprintf(ficlog,"Error in model: 'model=1+age+%s'; 'age*age' should in first place before other covariates\n \
13870: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
13871: corresponding column of parameters.\n",model); fflush(ficlog);
13872: return 1;
13873: }
13874: nagesqr=1;
13875: if (strstr(model,"+age*age") !=0)
13876: substrchaine(modelsav, model, "+age*age");
13877: else if (strstr(model,"age*age+") !=0)
13878: substrchaine(modelsav, model, "age*age+");
13879: else
13880: substrchaine(modelsav, model, "age*age");
13881: }else
13882: nagesqr=0;
13883: if (strlen(modelsav) >1){ /* V2 +V3 +V4 +V6 +V7 +V6*V2 +V7*V2 +V6*V3 +V7*V3 +V6*V4 +V7*V4 +age*V2 +age*V3 +age*V4 +age*V6 +age*V7 +age*V6*V2 +V7*V2 +age*V6*V3 +age*V7*V3 +age*V6*V4 +age*V7*V4 */
13884: j=nbocc(modelsav,'+'); /**< j=Number of '+' */
13885: j1=nbocc(modelsav,'*'); /**< j1=Number of '*' */
13886: /* cptcovs=j+1-j1; */ /* is wrong , see after */
13887: cptcovt= j+1; /* Number of total covariates in the model, not including
13888: * cst, age and age*age
13889: * V1+V1*age+ V3 + V3*V4+age*age=> 3+1=4*/
13890: /* including age products which are counted in cptcovage.
13891: * but the covariates which are products must be treated
13892: * separately: ncovn=4- 2=2 (V1+V3). */
13893: cptcovprod=0; /**< Number of products and single product with age V1*V2 +v3*age = 2 */
13894: cptcovdageprod=0; /* Number of double products with age age*Vn*VM or Vn*age*Vm or Vn*Vm*age */
13895: cptcovprodnoage=0; /**< Number of covariate products without age: V3*V4 =1 */
13896: cptcovprodage=0; /**< Number of varying covariate products with age: age*V6(v)*V3(f) =1 */
13897: /* cptcovprodage=nboccstr(modelsav,"age");*/
13898:
13899: /* Design
13900: * V1 V2 V3 V4 V5 V6 V7 V8 V9 Weight
13901: * < ncovcol=8 >
13902: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8
13903: * k= 1 2 3 4 5 6 7 8
13904: * cptcovn number of covariates (not including constant and age ) = # of + plus 1 = 7+1=8
13905: * covar[k,i], are for fixed covariates, value of kth covariate if not including age for individual i:
13906: * covar[1][i]= (V1), covar[4][i]=(V4), covar[8][i]=(V8)
13907: * Tvar[k] # of the kth covariate: Tvar[1]=2 Tvar[2]=1 Tvar[4]=3 Tvar[8]=8
13908: * if multiplied by age: V3*age Tvar[3=V3*age]=3 (V3) Tvar[7]=8 and
13909: * Tage[++cptcovage]=k
13910: * if products, new covar are created after ncovcol + nqv (quanti fixed) with k1
13911: * Tvar[k]=ncovcol+k1; # of the kth covariate product: Tvar[5]=ncovcol+1=10 Tvar[6]=ncovcol+1=11
13912: * Tprod[k1]=k; Tprod[1]=5 Tprod[2]= 6; gives the position of the k1th product
13913: * 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
13914: * Tvar[cptcovn+k2]=Tvard[k1][1];Tvar[cptcovn+k2+1]=Tvard[k1][2];
13915: * Tvar[8+1]=5;Tvar[8+2]=6;Tvar[8+3]=7;Tvar[8+4]=8 inverted
13916: * V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11
13917: * < ncovcol=8 8 fixed covariate. Additional starts at 9 (V5*V6) and 10(V7*V8) >
13918: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8 d1 d1 d2 d2
13919: * k= 1 2 3 4 5 6 7 8 9 10 11 12
13920: * Tvard[k]= 2 1 3 3 10 11 8 8 5 6 7 8
13921: * p Tvar[1]@12={2, 1, 3, 3, 9, 10, 8, 8}
13922: * p Tprod[1]@2={ 6, 5}
13923: *p Tvard[1][1]@4= {7, 8, 5, 6}
13924: * covar[k][i]= V2 V1 ? V3 V5*V6? V7*V8? ? V8
13925: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
13926: *How to reorganize? Tvars(orted)
13927: * Model V1 + V2 + V3 + V8 + V5*V6 + V7*V8 + V3*age + V8*age
13928: * Tvars {2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
13929: * {2, 1, 4, 8, 5, 6, 3, 7}
13930: * Struct []
13931: */
13932:
13933: /* This loop fills the array Tvar from the string 'model'.*/
13934: /* j is the number of + signs in the model V1+V2+V3 j=2 i=3 to 1 */
13935: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4 */
13936: /* k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tage[cptcovage=1]=4 */
13937: /* k=3 V4 Tvar[k=3]= 4 (from V4) */
13938: /* k=2 V1 Tvar[k=2]= 1 (from V1) */
13939: /* k=1 Tvar[1]=2 (from V2) */
13940: /* k=5 Tvar[5] */
13941: /* for (k=1; k<=cptcovn;k++) { */
13942: /* cov[2+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
13943: /* } */
13944: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k])]]*cov[2]; */
13945: /*
13946: * Treating invertedly V2+V1+V3*age+V2*V4 is as if written V2*V4 +V3*age + V1 + V2 */
13947: for(k=cptcovt; k>=1;k--){ /**< Number of covariates not including constant and age, neither age*age*/
13948: Tvar[k]=0; Tprod[k]=0; Tposprod[k]=0;
13949: }
13950: cptcovage=0;
13951: for(k=1; k<=cptcovt;k++){ /* Loop on total covariates of the model line */
13952: cutl(stra,strb,modelsav,'+'); /* keeps in strb after the first '+' cutl from left to right
13953: 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" */
13954: if (nbocc(modelsav,'+')==0)
13955: strcpy(strb,modelsav); /* and analyzes it */
13956: /* printf("i=%d a=%s b=%s sav=%s\n",i, stra,strb,modelsav);*/
13957: /*scanf("%d",i);*/
13958: if (strchr(strb,'*')) { /**< Model includes a product V2+V1+V5*age+ V4+V3*age strb=V3*age OR double product with age strb=age*V6*V2 or V6*V2*age or V6*age*V2 */
13959: cutl(strc,strd,strb,'*'); /**< k=1 strd*strc Vm*Vn: strb=V3*age(input) strc=age strd=V3 ; V3*V2 strc=V2, strd=V3 OR strb=age*V6*V2 strc=V6*V2 strd=age OR strb=V6*age*V2 c=age*V2 d=V6 OR b=V6*V2*age c=V2*age d=V6 */
13960: if(strchr(strc,'*')) { /**< Model with age and DOUBLE product: allowed since 0.99r44, strc=V6*V2 or V2*age or age*V2, strd=age or V6 or V6 OR (strb=age*V6*V2 or V6*V2*age or V6*age*V2) */
13961: Typevar[k]=3; /* 3 for age and double product age*Vn*Vm varying of fixed */
13962: if(strstr(strc,"age")!=0) { /* It means that strc=V2*age=Vm*age or age*V2 (not V6*V2) and thus that strd=Vn and strb=V6*V2*age or V6*age*V2 (but not age*V6*V2) */
13963: cutl(stre,strf,strc,'*') ; /* if strc=age*Vm then stre=Vm and strf=age, if strc=Vm*age then stre=age and strf=Vm. */
13964: strcpy(strc,strb); /* save strb(=age*Vn*Vm) into strc , strd=Vn */
13965: /* We want strb=Vn*Vm */
13966: if(strcmp(strf,"age")==0){ /* strf is "age" so stre=Vm =V2 (strc=age*Vm and strb Vn*age*Vm) . */
13967: strcpy(strb,strd); /* strd=Vn */
13968: strcat(strb,"*");
13969: strcat(strb,stre);/* strb=Vn*Vm */
13970: }else{ /* strf=Vm so stre=age. strd=Vn If strf=V6 then stre=V2 */
13971: strcpy(strb,strf);
13972: strcat(strb,"*");
13973: strcat(strb,strd); /* strb=Vm*Vn */
13974: strcpy(strd,strb); /* in order for strd to not be "age" for next test (will be strd=Vn*Vm */
13975: }
13976: printf("DEBUG FIXED k=%d, Tage[k]=%d, Tvar[Tage[k]=%d,FixedV[Tvar[Tage[k]]]=%d\n",k,Tage[k],Tvar[Tage[k]],FixedV[Tvar[Tage[k]]]);
13977: /* FixedV[Tvar[Tage[k]]]=0;*/ /* HERY not sure */
13978: }else{ /* strb=age*Vn*Vm strc=Vn*Vm (and strd=age) and should be strb=Vn*Vm but want to keep original strb double product */
13979: strcpy(stre,strb); /* save full b in stre */
13980: strcpy(strb,strc); /* save short c in new short b for next block strb=Vn*Vm*/
13981: strcpy(strf,strc); /* save short c in new short f */
13982: cutl(strc,strd,strf,'*'); /* We get strd=Vn and strc=Vm for next block (strb=Vn*Vm)*/
13983: /* strcpy(strc,stre);*/ /* save full e in c for future */
13984: }
13985: cptcovdageprod++; /* double product with age Which product is it? */
13986: /* strcpy(strb,strc); /\* strb was age*V6*V2 or V6*V2*age or V6*age*V2 IS now V6*V2 or V2*age or age*V2 *\/ */
13987: /* cutl(strc,strd,strb,'*'); /\* strd= V6 or V2 or age and strc= V2 or age or V2 *\/ */
13988: cutl(stre,strb,strc,'V'); /* strc= Vn, stre is n; strb=V3*V2 stre=3 strc=*/
13989: n=atoi(stre);
13990: cutl(strc,strb,strd,'V'); /* strd was Vm, strc is m */
13991: m=atoi(strc);
13992: cptcovage++; /* Counts the number of covariates which include age as a product */
13993: Tage[cptcovage]=k; /* For age*V3*V2 gives the position in model of covariates associated with age Tage[1]=6 HERY too*/
13994: if(existcomb[n][m] == 0){
13995: /* r /home/brouard/Documents/Recherches/REVES/Zachary/Zach-2022/Feinuo_Sun/Feinuo-threeway/femV12V15_3wayintNBe.imach */
13996: printf("Warning in model combination V%d*V%d should exist in the model before adding V%d*V%d*age !\n",n,m,n,m);
13997: fprintf(ficlog,"Warning in model combination V%d*V%d should exist in the model before adding V%d*V%d*age !\n",n,m,n,m);
13998: fflush(ficlog);
13999: k1++; /* The combination Vn*Vm will be in the model so we create it at k1 */
14000: k12++;
14001: existcomb[n][m]=k1;
14002: existcomb[m][n]=k1;
14003: Tvar[k]=ncovcol+nqv+ntv+nqtv+k1;
14004: Tprod[k1]=k; /* Tprod[1]=3(=V1*V4) for V2+V1+V1*V4+age*V3+V3*V2+ age*V6*V3 Gives the k position of the k1 double product Vn*Vm or age*Vn*Vm*/
14005: Tposprod[k]=k1; /* Tposprod[3]=1, Tposprod[2]=5 Gives the k1 double product Vn*Vm or age*Vn*Vm at the k position */
14006: Tvard[k1][1] =m; /* m 1 for V1*/
14007: Tvardk[k][1] =m; /* m 1 for V1*/
14008: Tvard[k1][2] =n; /* n 4 for V4*/
14009: Tvardk[k][2] =n; /* n 4 for V4*/
14010: /* Tvar[Tage[cptcovage]]=k1;*/ /* Tvar[6=age*V3*V2]=9 (new fixed covariate) */
14011: 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 */
14012: for (i=1; i<=lastobs;i++){/* For fixed product */
14013: /* Computes the new covariate which is a product of
14014: covar[n][i]* covar[m][i] and stores it at ncovol+k1 May not be defined */
14015: covar[ncovcolt+k1][i]=covar[atoi(stre)][i]*covar[atoi(strc)][i];
14016: }
14017: cptcovprodage++; /* Counting the number of fixed covariate with age */
14018: FixedV[ncovcolt+k12]=0; /* We expand Vn*Vm */
14019: k12++;
14020: FixedV[ncovcolt+k12]=0;
14021: }else{ /*End of FixedV */
14022: cptcovprodvage++; /* Counting the number of varying covariate with age */
14023: FixedV[ncovcolt+k12]=1; /* We expand Vn*Vm */
14024: k12++;
14025: FixedV[ncovcolt+k12]=1;
14026: }
14027: }else{ /* k1 Vn*Vm already exists */
14028: k11=existcomb[n][m];
14029: Tposprod[k]=k11; /* OK */
14030: Tvar[k]=Tvar[Tprod[k11]]; /* HERY */
14031: Tvardk[k][1]=m;
14032: Tvardk[k][2]=n;
14033: 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 */
14034: /*cptcovage++;*/ /* Counts the number of covariates which include age as a product */
14035: cptcovprodage++; /* Counting the number of fixed covariate with age */
14036: /*Tage[cptcovage]=k;*/ /* For age*V3*V2 Tage[1]=V3*V3=9 HERY too*/
14037: Tvar[Tage[cptcovage]]=k1;
14038: FixedV[ncovcolt+k12]=0; /* We expand Vn*Vm */
14039: k12++;
14040: FixedV[ncovcolt+k12]=0;
14041: }else{ /* Already exists but time varying (and age) */
14042: /*cptcovage++;*/ /* Counts the number of covariates which include age as a product */
14043: /*Tage[cptcovage]=k;*/ /* For age*V3*V2 Tage[1]=V3*V3=9 HERY too*/
14044: /* Tvar[Tage[cptcovage]]=k1; */
14045: cptcovprodvage++;
14046: k12=2*k11-1;
14047: FixedV[ncovcolt+k12]=1; /* We expand Vn*Vm */
14048: FixedV[ncovcolt+k12+1]=1;
14049: }
14050: }
14051: /* Tage[cptcovage]=k; /\* V2+V1+V4+V3*age Tvar[4]=3, Tage[1] = 4 or V1+V1*age Tvar[2]=1, Tage[1]=2 *\/ */
14052: /* Tvar[k]=k11; /\* HERY *\/ */
14053: } else {/* simple product strb=age*Vn so that c=Vn and d=age, or strb=Vn*age so that c=age and d=Vn, or b=Vn*Vm so that c=Vm and d=Vn */
14054: cptcovprod++;
14055: if (strcmp(strc,"age")==0) { /**< Model includes age: strb= Vn*age c=age d=Vn*/
14056: /* covar is not filled and then is empty */
14057: cutl(stre,strb,strd,'V'); /* strd=V3(input): stre="3" */
14058: 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 */
14059: Typevar[k]=1; /* 1 for age product */
14060: cptcovage++; /* Counts the number of covariates which include age as a product */
14061: Tage[cptcovage]=k; /* V2+V1+V4+V3*age Tvar[4]=3, Tage[1] = 4 or V1+V1*age Tvar[2]=1, Tage[1]=2 */
14062: if( FixedV[Tvar[k]] == 0){
14063: cptcovprodage++; /* Counting the number of fixed covariate with age */
14064: }else{
14065: cptcovprodvage++; /* Counting the number of fixedvarying covariate with age */
14066: }
14067: /*printf("stre=%s ", stre);*/
14068: } else if (strcmp(strd,"age")==0) { /* strb= age*Vn c=Vn */
14069: cutl(stre,strb,strc,'V');
14070: Tvar[k]=atoi(stre);
14071: Typevar[k]=1; /* 1 for age product */
14072: cptcovage++;
14073: Tage[cptcovage]=k;
14074: if( FixedV[Tvar[k]] == 0){
14075: cptcovprodage++; /* Counting the number of fixed covariate with age */
14076: }else{
14077: cptcovprodvage++; /* Counting the number of fixedvarying covariate with age */
14078: }
14079: }else{ /* for product Vn*Vm */
14080: Typevar[k]=2; /* 2 for product Vn*Vm */
14081: cutl(stre,strb,strc,'V'); /* strc= Vn, stre is n; strb=V3*V2 stre=3 strc=*/
14082: n=atoi(stre);
14083: cutl(strc,strb,strd,'V'); /* strd was Vm, strc is m */
14084: m=atoi(strc);
14085: k1++;
14086: cptcovprodnoage++;
14087: if(existcomb[n][m] != 0 || existcomb[m][n] != 0){
14088: printf("Warning in model combination V%d*V%d already exists in the model in position k1=%d!\n",n,m,existcomb[n][m]);
14089: fprintf(ficlog,"Warning in model combination V%d*V%d already exists in the model in position k1=%d!\n",n,m,existcomb[n][m]);
14090: fflush(ficlog);
14091: k11=existcomb[n][m];
14092: Tvar[k]=ncovcol+nqv+ntv+nqtv+k11;
14093: Tposprod[k]=k11;
14094: Tprod[k11]=k;
14095: Tvardk[k][1] =m; /* m 1 for V1*/
14096: /* Tvard[k11][1] =m; /\* n 4 for V4*\/ */
14097: Tvardk[k][2] =n; /* n 4 for V4*/
14098: /* Tvard[k11][2] =n; /\* n 4 for V4*\/ */
14099: }else{ /* combination Vn*Vm doesn't exist we create it (no age)*/
14100: existcomb[n][m]=k1;
14101: existcomb[m][n]=k1;
14102: Tvar[k]=ncovcol+nqv+ntv+nqtv+k1; /* ncovcolt+k1; For model-covariate k tells which data-covariate to use but
14103: because this model-covariate is a construction we invent a new column
14104: which is after existing variables ncovcol+nqv+ntv+nqtv + k1
14105: If already ncovcol=4 and model= V2 + V1 + V1*V4 + age*V3 + V3*V2
14106: thus after V4 we invent V5 and V6 because age*V3 will be computed in 4
14107: Tvar[3=V1*V4]=4+1=5 Tvar[5=V3*V2]=4 + 2= 6, Tvar[4=age*V3]=3 etc */
14108: /* Please remark that the new variables are model dependent */
14109: /* If we have 4 variable but the model uses only 3, like in
14110: * model= V1 + age*V1 + V2 + V3 + age*V2 + age*V3 + V1*V2 + V1*V3
14111: * k= 1 2 3 4 5 6 7 8
14112: * Tvar[k]=1 1 2 3 2 3 (5 6) (and not 4 5 because of V4 missing)
14113: * Tage[kk] [1]= 2 [2]=5 [3]=6 kk=1 to cptcovage=3
14114: * Tvar[Tage[kk]][1]=2 [2]=2 [3]=3
14115: */
14116: /* We need to feed some variables like TvarVV, but later on next loop because of ncovv (k2) is not correct */
14117: Tprod[k1]=k; /* Tprod[1]=3(=V1*V4) for V2+V1+V1*V4+age*V3+V3*V2 +V6*V2*age */
14118: Tposprod[k]=k1; /* Tposprod[3]=1, Tposprod[2]=5 */
14119: Tvard[k1][1] =m; /* m 1 for V1*/
14120: Tvardk[k][1] =m; /* m 1 for V1*/
14121: Tvard[k1][2] =n; /* n 4 for V4*/
14122: Tvardk[k][2] =n; /* n 4 for V4*/
14123: k2=k2+2; /* k2 is initialize to -1, We want to store the n and m in Vn*Vm at the end of Tvar */
14124: /* Tvar[cptcovt+k2]=Tvard[k1][1]; /\* Tvar[(cptcovt=4+k2=1)=5]= 1 (V1) *\/ */
14125: /* Tvar[cptcovt+k2+1]=Tvard[k1][2]; /\* Tvar[(cptcovt=4+(k2=1)+1)=6]= 4 (V4) *\/ */
14126: /*ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2, Tvar[3]=5, Tvar[4]=6, cptcovt=5 */
14127: /* 1 2 3 4 5 | Tvar[5+1)=1, Tvar[7]=2 */
14128: 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 */
14129: for (i=1; i<=lastobs;i++){/* For fixed product */
14130: /* Computes the new covariate which is a product of
14131: covar[n][i]* covar[m][i] and stores it at ncovol+k1 May not be defined */
14132: covar[ncovcolt+k1][i]=covar[atoi(stre)][i]*covar[atoi(strc)][i];
14133: }
14134: /* TvarVV[k2]=n; */
14135: /* FixedV[ncovcolt+k2]=0; /\* or FixedV[Tvar[k]]=0; FixedV[TvarVV[ncovv]]=0 HERE *\/ */
14136: /* TvarVV[k2+1]=m; */
14137: /* FixedV[ncovcolt+k2]=0; /\* or FixedV[Tvar[k]]=0; FixedV[TvarVV[ncovv]]=0 HERE *\/ */
14138: }else{ /* not FixedV */
14139: /* TvarVV[k2]=n; */
14140: /* FixedV[ncovcolt+k2]=0; /\* or FixedV[Tvar[k]]=0; FixedV[TvarVV[ncovv]]=0 HERE *\/ */
14141: /* TvarVV[k2+1]=m; */
14142: /* FixedV[ncovcolt+k2]=0; /\* or FixedV[Tvar[k]]=0; FixedV[TvarVV[ncovv]]=0 HERE *\/ */
14143: }
14144: } /* End of creation of Vn*Vm if not created by age*Vn*Vm earlier */
14145: } /* End of product Vn*Vm */
14146: } /* End of age*double product or simple product */
14147: }else { /* not a product */
14148: /*printf("d=%s c=%s b=%s\n", strd,strc,strb);*/
14149: /* scanf("%d",i);*/
14150: cutl(strd,strc,strb,'V');
14151: ks++; /**< Number of simple covariates dummy or quantitative, fixe or varying */
14152: cptcovn++; /** V4+V3+V5: V4 and V3 timevarying dummy covariates, V5 timevarying quantitative */
14153: Tvar[k]=atoi(strd);
14154: Typevar[k]=0; /* 0 for simple covariates */
14155: }
14156: strcpy(modelsav,stra); /* modelsav=V2+V1+V4 stra=V2+V1+V4 */
14157: /*printf("a=%s b=%s sav=%s\n", stra,strb,modelsav);
14158: scanf("%d",i);*/
14159: } /* end of loop + on total covariates */
14160: } /* end if strlen(modelsave == 0) age*age might exist */
14161: } /* end if strlen(model == 0) */
14162: cptcovs=cptcovt - cptcovdageprod - cptcovprod;/**< Number of simple covariates V1 +V1*age +V3 +V3*V4 +age*age + age*v4*V3=> V1 + V3 =4+1-3=2 */
14163:
14164: /*The number n of Vn is stored in Tvar. cptcovage =number of age covariate. Tage gives the position of age. cptcovprod= number of products.
14165: If model=V1+V1*age then Tvar[1]=1 Tvar[2]=1 cptcovage=1 Tage[1]=2 cptcovprod=0*/
14166:
14167: /* printf("tvar1=%d tvar2=%d tvar3=%d cptcovage=%d Tage=%d",Tvar[1],Tvar[2],Tvar[3],cptcovage,Tage[1]);
14168: printf("cptcovprod=%d ", cptcovprod);
14169: fprintf(ficlog,"cptcovprod=%d ", cptcovprod);
14170: scanf("%d ",i);*/
14171:
14172:
14173: /* Until here, decodemodel knows only the grammar (simple, product, age*) of the model but not what kind
14174: of variable (dummy vs quantitative, fixed vs time varying) is behind. But we know the # of each. */
14175: /* ncovcol= 1, nqv=1 | ntv=2, nqtv= 1 = 5 possible variables data: 2 fixed 3, varying
14176: model= V5 + V4 +V3 + V4*V3 + V5*age + V2 + V1*V2 + V1*age + V5*age, V1 is not used saving its place
14177: k = 1 2 3 4 5 6 7 8 9
14178: Tvar[k]= 5 4 3 1+1+2+1+1=6 5 2 7 1 5
14179: Typevar[k]= 0 0 0 2 1 0 2 1 0
14180: Fixed[k] 1 1 1 1 3 0 0 or 2 2 3
14181: Dummy[k] 1 0 0 0 3 1 1 2 3
14182: Tmodelind[combination of covar]=k;
14183: */
14184: /* Dispatching between quantitative and time varying covariates */
14185: /* If Tvar[k] >ncovcol it is a product */
14186: /* Tvar[k] is the value n of Vn with n varying for 1 to nvcol, or p Vp=Vn*Vm for product */
14187: /* Computing effective variables, ie used by the model, that is from the cptcovt variables */
14188: printf("Model=1+age+%s\n\
14189: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product, 3 for double product with age \n\
14190: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\
14191: 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);
14192: fprintf(ficlog,"Model=1+age+%s\n\
14193: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product, 3 for double product with age \n\
14194: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\
14195: 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);
14196: for(k=-1;k<=NCOVMAX; k++){ Fixed[k]=0; Dummy[k]=0;}
14197: for(k=1;k<=NCOVMAX; k++){TvarFind[k]=0; TvarVind[k]=0;}
14198: for(k=1, ncovf=0, nsd=0, nsq=0, ncovv=0,ncovva=0,ncovvta=0, ncova=0, ncoveff=0, nqfveff=0, ntveff=0, nqtveff=0, ncovvt=0;k<=cptcovt; k++){ /* or cptocvt loop on k from model */
14199: if (Tvar[k] <=ncovcol && Typevar[k]==0 ){ /* Simple fixed dummy (<=ncovcol) covariates */
14200: Fixed[k]= 0;
14201: Dummy[k]= 0;
14202: ncoveff++;
14203: ncovf++;
14204: nsd++;
14205: modell[k].maintype= FTYPE;
14206: TvarsD[nsd]=Tvar[k];
14207: TvarsDind[nsd]=k;
14208: TnsdVar[Tvar[k]]=nsd;
14209: TvarF[ncovf]=Tvar[k];
14210: TvarFind[ncovf]=k;
14211: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
14212: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
14213: /* }else if( Tvar[k] <=ncovcol && Typevar[k]==2){ /\* Product of fixed dummy (<=ncovcol) covariates For a fixed product k is higher than ncovcol *\/ */
14214: }else if( Tvar[k] <=ncovcol+nqv && Typevar[k]==0){/* Remind that product Vn*Vm are added in k Only simple fixed quantitative variable */
14215: Fixed[k]= 0;
14216: Dummy[k]= 1;
14217: nqfveff++;
14218: modell[k].maintype= FTYPE;
14219: modell[k].subtype= FQ;
14220: nsq++;
14221: TvarsQ[nsq]=Tvar[k]; /* Gives the variable name (extended to products) of first nsq simple quantitative covariates (fixed or time vary see below */
14222: TvarsQind[nsq]=k; /* Gives the position in the model equation of the first nsq simple quantitative covariates (fixed or time vary) */
14223: ncovf++;
14224: TvarF[ncovf]=Tvar[k];
14225: TvarFind[ncovf]=k;
14226: 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 */
14227: 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 */
14228: }else if( Tvar[k] <=ncovcol+nqv+ntv && Typevar[k]==0){/* Only simple time varying dummy variables */
14229: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
14230: /* model V1+V3+age*V1+age*V3+V1*V3 */
14231: /* Tvar={1, 3, 1, 3, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
14232: ncovvt++;
14233: TvarVV[ncovvt]=Tvar[k]; /* TvarVV[1]=V3 (first time varying in the model equation */
14234: TvarVVind[ncovvt]=k; /* TvarVVind[1]=2 (second position in the model equation */
14235:
14236: Fixed[k]= 1;
14237: Dummy[k]= 0;
14238: ntveff++; /* Only simple time varying dummy variable */
14239: modell[k].maintype= VTYPE;
14240: modell[k].subtype= VD;
14241: nsd++;
14242: TvarsD[nsd]=Tvar[k];
14243: TvarsDind[nsd]=k;
14244: TnsdVar[Tvar[k]]=nsd; /* To be verified */
14245: ncovv++; /* Only simple time varying variables */
14246: TvarV[ncovv]=Tvar[k];
14247: 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 */
14248: 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 */
14249: 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 */
14250: 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);
14251: printf("Quasi TmodelInvind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv);
14252: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv && Typevar[k]==0){ /* Only simple time varying quantitative variable V5*/
14253: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
14254: /* model V1+V3+age*V1+age*V3+V1*V3 */
14255: /* Tvar={1, 3, 1, 3, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
14256: ncovvt++;
14257: TvarVV[ncovvt]=Tvar[k]; /* TvarVV[1]=V3 (first time varying in the model equation */
14258: TvarVVind[ncovvt]=k; /* TvarVV[1]=V3 (first time varying in the model equation */
14259:
14260: Fixed[k]= 1;
14261: Dummy[k]= 1;
14262: nqtveff++;
14263: modell[k].maintype= VTYPE;
14264: modell[k].subtype= VQ;
14265: ncovv++; /* Only simple time varying variables */
14266: nsq++;
14267: 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) */
14268: 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) */
14269: TvarV[ncovv]=Tvar[k];
14270: TvarVind[ncovv]=k; /* TvarVind[1]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Any time varying singele */
14271: 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 */
14272: 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 */
14273: TmodelInvQind[nqtveff]=Tvar[k]- ncovcol-nqv-ntv;/* Only simple time varying quantitative variable */
14274: /* Tmodeliqind[k]=nqtveff;/\* Only simple time varying quantitative variable *\/ */
14275: /* printf("Quasi TmodelQind[%d]=%d,Tvar[TmodelQind[%d]]=V%d, ncovcol=%d, nqv=%d, ntv=%Ad,Tvar[k]- ncovcol-nqv-ntv=%d\n",nqtveff,k,nqtveff,Tvar[k], ncovcol, nqv, ntv, Tvar[k]- ncovcol-nqv-ntv); */
14276: /* printf("Quasi TmodelInvQind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv-ntv); */
14277: }else if (Typevar[k] == 1) { /* product with age */
14278: ncova++;
14279: TvarA[ncova]=Tvar[k];
14280: TvarAind[ncova]=k;
14281: /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
14282: /** Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product */
14283: if (Tvar[k] <=ncovcol ){ /* Product age with fixed dummy covariatee */
14284: Fixed[k]= 2;
14285: Dummy[k]= 2;
14286: modell[k].maintype= ATYPE;
14287: modell[k].subtype= APFD;
14288: ncovta++;
14289: TvarAVVA[ncovta]=Tvar[k]; /* (2)age*V3 */
14290: TvarAVVAind[ncovta]=k;
14291: /* ncoveff++; */
14292: }else if( Tvar[k] <=ncovcol+nqv) { /* Remind that product Vn*Vm are added in k*/
14293: Fixed[k]= 2;
14294: Dummy[k]= 3;
14295: modell[k].maintype= ATYPE;
14296: modell[k].subtype= APFQ; /* Product age * fixed quantitative */
14297: ncovta++;
14298: TvarAVVA[ncovta]=Tvar[k]; /* */
14299: TvarAVVAind[ncovta]=k;
14300: /* nqfveff++; /\* Only simple fixed quantitative variable *\/ */
14301: }else if( Tvar[k] <=ncovcol+nqv+ntv ){
14302: Fixed[k]= 3;
14303: Dummy[k]= 2;
14304: modell[k].maintype= ATYPE;
14305: modell[k].subtype= APVD; /* Product age * varying dummy */
14306: ncovva++;
14307: TvarVVA[ncovva]=Tvar[k]; /* (1)+age*V6 + (2)age*V7 */
14308: TvarVVAind[ncovva]=k;
14309: ncovta++;
14310: TvarAVVA[ncovta]=Tvar[k]; /* */
14311: TvarAVVAind[ncovta]=k;
14312: /* ntveff++; /\* Only simple time varying dummy variable *\/ */
14313: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv){
14314: Fixed[k]= 3;
14315: Dummy[k]= 3;
14316: modell[k].maintype= ATYPE;
14317: modell[k].subtype= APVQ; /* Product age * varying quantitative */
14318: ncovva++;
14319: TvarVVA[ncovva]=Tvar[k]; /* */
14320: TvarVVAind[ncovva]=k;
14321: ncovta++;
14322: TvarAVVA[ncovta]=Tvar[k]; /* (1)+age*V6 + (2)age*V7 */
14323: TvarAVVAind[ncovta]=k;
14324: /* nqtveff++;/\* Only simple time varying quantitative variable *\/ */
14325: }
14326: }else if( Tposprod[k]>0 && Typevar[k]==2){ /* Detects if fixed product no age Vm*Vn */
14327: printf("MEMORY ERRORR k=%d Tposprod[k]=%d, Typevar[k]=%d\n ",k, Tposprod[k], Typevar[k]);
14328: if(FixedV[Tvardk[k][1]] == 0 && FixedV[Tvardk[k][2]] == 0){ /* Needs a fixed product Product of fixed dummy (<=ncovcol) covariates For a fixed product k is higher than ncovcol V3*V2 */
14329: printf("MEMORY ERRORR k=%d Tvardk[k][1]=%d, Tvardk[k][2]=%d, FixedV[Tvardk[k][1]]=%d,FixedV[Tvardk[k][2]]=%d\n ",k,Tvardk[k][1],Tvardk[k][2],FixedV[Tvardk[k][1]],FixedV[Tvardk[k][2]]);
14330: Fixed[k]= 0;
14331: Dummy[k]= 0;
14332: ncoveff++;
14333: ncovf++;
14334: /* ncovv++; */
14335: /* TvarVV[ncovv]=Tvardk[k][1]; */
14336: /* FixedV[ncovcolt+ncovv]=0; /\* or FixedV[TvarVV[ncovv]]=0 HERE *\/ */
14337: /* ncovv++; */
14338: /* TvarVV[ncovv]=Tvardk[k][2]; */
14339: /* FixedV[ncovcolt+ncovv]=0; /\* or FixedV[TvarVV[ncovv]]=0 HERE *\/ */
14340: modell[k].maintype= FTYPE;
14341: TvarF[ncovf]=Tvar[k];
14342: /* TnsdVar[Tvar[k]]=nsd; */ /* To be done */
14343: TvarFind[ncovf]=k;
14344: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
14345: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
14346: }else{/* product varying Vn * Vm without age, V1+V3+age*V1+age*V3+V1*V3 looking at V1*V3, Typevar={0, 0, 1, 1, 2}, k=5, V1 is fixed, V3 is timevary, V5 is a product */
14347: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
14348: /* model V1+V3+age*V1+age*V3+V1*V3 + V1*V3*age*/
14349: /* Tvar={1, 3, 1, 3, 6, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
14350: k1=Tposprod[k]; /* Position in the products of product k, Tposprod={0, 0, 0, 0, 1, 1} k1=1 first product but second time varying because of V3 */
14351: ncovvt++;
14352: TvarVV[ncovvt]=Tvard[k1][1]; /* TvarVV[2]=V1 (because TvarVV[1] was V3, first time varying covariates */
14353: TvarVVind[ncovvt]=k; /* TvarVVind[2]=5 (because TvarVVind[2] was V1*V3 at position 5 */
14354: ncovvt++;
14355: TvarVV[ncovvt]=Tvard[k1][2]; /* TvarVV[3]=V3 */
14356: TvarVVind[ncovvt]=k; /* TvarVVind[2]=5 (because TvarVVind[2] was V1*V3 at position 5 */
14357:
14358: /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
14359: /** Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product */
14360:
14361: if(Tvard[k1][1] <=ncovcol){ /* Vn is dummy fixed, (Tvard[1][1]=V1), (Tvard[1][1]=V3 time varying) */
14362: if(Tvard[k1][2] <=ncovcol){ /* Vm is dummy fixed */
14363: Fixed[k]= 1;
14364: Dummy[k]= 0;
14365: modell[k].maintype= FTYPE;
14366: modell[k].subtype= FPDD; /* Product fixed dummy * fixed dummy */
14367: ncovf++; /* Fixed variables without age */
14368: TvarF[ncovf]=Tvar[k];
14369: TvarFind[ncovf]=k;
14370: }else if(Tvard[k1][2] <=ncovcol+nqv){ /* Vm is quanti fixed */
14371: Fixed[k]= 0; /* Fixed product */
14372: Dummy[k]= 1;
14373: modell[k].maintype= FTYPE;
14374: modell[k].subtype= FPDQ; /* Product fixed dummy * fixed quantitative */
14375: ncovf++; /* Varying variables without age */
14376: TvarF[ncovf]=Tvar[k];
14377: TvarFind[ncovf]=k;
14378: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){ /* Vm is a time varying dummy covariate */
14379: Fixed[k]= 1;
14380: Dummy[k]= 0;
14381: modell[k].maintype= VTYPE;
14382: modell[k].subtype= VPDD; /* Product fixed dummy * varying dummy */
14383: ncovv++; /* Varying variables without age */
14384: TvarV[ncovv]=Tvar[k]; /* TvarV[1]=Tvar[5]=5 because there is a V4 */
14385: TvarVind[ncovv]=k;/* TvarVind[1]=5 */
14386: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){ /* Vm is a time varying quantitative covariate */
14387: Fixed[k]= 1;
14388: Dummy[k]= 1;
14389: modell[k].maintype= VTYPE;
14390: modell[k].subtype= VPDQ; /* Product fixed dummy * varying quantitative */
14391: ncovv++; /* Varying variables without age */
14392: TvarV[ncovv]=Tvar[k];
14393: TvarVind[ncovv]=k;
14394: }
14395: }else if(Tvard[k1][1] <=ncovcol+nqv){ /* Vn is fixed quanti */
14396: if(Tvard[k1][2] <=ncovcol){ /* Vm is fixed dummy */
14397: Fixed[k]= 0; /* Fixed product */
14398: Dummy[k]= 1;
14399: modell[k].maintype= FTYPE;
14400: modell[k].subtype= FPDQ; /* Product fixed quantitative * fixed dummy */
14401: ncovf++; /* Fixed variables without age */
14402: TvarF[ncovf]=Tvar[k];
14403: TvarFind[ncovf]=k;
14404: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){ /* Vm is time varying */
14405: Fixed[k]= 1;
14406: Dummy[k]= 1;
14407: modell[k].maintype= VTYPE;
14408: modell[k].subtype= VPDQ; /* Product fixed quantitative * varying dummy */
14409: ncovv++; /* Varying variables without age */
14410: TvarV[ncovv]=Tvar[k];
14411: TvarVind[ncovv]=k;
14412: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){ /* Vm is time varying quanti */
14413: Fixed[k]= 1;
14414: Dummy[k]= 1;
14415: modell[k].maintype= VTYPE;
14416: modell[k].subtype= VPQQ; /* Product fixed quantitative * varying quantitative */
14417: ncovv++; /* Varying variables without age */
14418: TvarV[ncovv]=Tvar[k];
14419: TvarVind[ncovv]=k;
14420: ncovv++; /* Varying variables without age */
14421: TvarV[ncovv]=Tvar[k];
14422: TvarVind[ncovv]=k;
14423: }
14424: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv){ /* Vn is time varying dummy */
14425: if(Tvard[k1][2] <=ncovcol){
14426: Fixed[k]= 1;
14427: Dummy[k]= 1;
14428: modell[k].maintype= VTYPE;
14429: modell[k].subtype= VPDD; /* Product time varying dummy * fixed dummy */
14430: ncovv++; /* Varying variables without age */
14431: TvarV[ncovv]=Tvar[k];
14432: TvarVind[ncovv]=k;
14433: }else if(Tvard[k1][2] <=ncovcol+nqv){
14434: Fixed[k]= 1;
14435: Dummy[k]= 1;
14436: modell[k].maintype= VTYPE;
14437: modell[k].subtype= VPDQ; /* Product time varying dummy * fixed quantitative */
14438: ncovv++; /* Varying variables without age */
14439: TvarV[ncovv]=Tvar[k];
14440: TvarVind[ncovv]=k;
14441: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
14442: Fixed[k]= 1;
14443: Dummy[k]= 0;
14444: modell[k].maintype= VTYPE;
14445: modell[k].subtype= VPDD; /* Product time varying dummy * time varying dummy */
14446: ncovv++; /* Varying variables without age */
14447: TvarV[ncovv]=Tvar[k];
14448: TvarVind[ncovv]=k;
14449: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
14450: Fixed[k]= 1;
14451: Dummy[k]= 1;
14452: modell[k].maintype= VTYPE;
14453: modell[k].subtype= VPDQ; /* Product time varying dummy * time varying quantitative */
14454: ncovv++; /* Varying variables without age */
14455: TvarV[ncovv]=Tvar[k];
14456: TvarVind[ncovv]=k;
14457: }
14458: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv+nqtv){ /* Vn is time varying quanti */
14459: if(Tvard[k1][2] <=ncovcol){
14460: Fixed[k]= 1;
14461: Dummy[k]= 1;
14462: modell[k].maintype= VTYPE;
14463: modell[k].subtype= VPDQ; /* Product time varying quantitative * fixed dummy */
14464: ncovv++; /* Varying variables without age */
14465: TvarV[ncovv]=Tvar[k];
14466: TvarVind[ncovv]=k;
14467: }else if(Tvard[k1][2] <=ncovcol+nqv){
14468: Fixed[k]= 1;
14469: Dummy[k]= 1;
14470: modell[k].maintype= VTYPE;
14471: modell[k].subtype= VPQQ; /* Product time varying quantitative * fixed quantitative */
14472: ncovv++; /* Varying variables without age */
14473: TvarV[ncovv]=Tvar[k];
14474: TvarVind[ncovv]=k;
14475: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
14476: Fixed[k]= 1;
14477: Dummy[k]= 1;
14478: modell[k].maintype= VTYPE;
14479: modell[k].subtype= VPDQ; /* Product time varying quantitative * time varying dummy */
14480: ncovv++; /* Varying variables without age */
14481: TvarV[ncovv]=Tvar[k];
14482: TvarVind[ncovv]=k;
14483: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
14484: Fixed[k]= 1;
14485: Dummy[k]= 1;
14486: modell[k].maintype= VTYPE;
14487: modell[k].subtype= VPQQ; /* Product time varying quantitative * time varying quantitative */
14488: ncovv++; /* Varying variables without age */
14489: TvarV[ncovv]=Tvar[k];
14490: TvarVind[ncovv]=k;
14491: }
14492: }else{
14493: printf("Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
14494: fprintf(ficlog,"Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
14495: } /*end k1*/
14496: }
14497: }else if(Typevar[k] == 3){ /* product Vn * Vm with age, V1+V3+age*V1+age*V3+V1*V3 looking at V1*V3, Typevar={0, 0, 1, 1, 2}, k=5, V1 is fixed, V3 is timevary, V5 is a product */
14498: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
14499: /* model V1+V3+age*V1+age*V3+V1*V3 + V1*V3*age*/
14500: /* Tvar={1, 3, 1, 3, 6, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
14501: k1=Tposprod[k]; /* Position in the products of product k, Tposprod={0, 0, 0, 0, 1, 1} k1=1 first product but second time varying because of V3 */
14502: ncova++;
14503: TvarA[ncova]=Tvard[k1][1]; /* TvarVV[2]=V1 (because TvarVV[1] was V3, first time varying covariates */
14504: TvarAind[ncova]=k; /* TvarVVind[2]=5 (because TvarVVind[2] was V1*V3 at position 5 */
14505: ncova++;
14506: TvarA[ncova]=Tvard[k1][2]; /* TvarVV[3]=V3 */
14507: TvarAind[ncova]=k; /* TvarVVind[2]=5 (because TvarVVind[2] was V1*V3 at position 5 */
14508:
14509: /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
14510: /** Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product */
14511: if( FixedV[Tvardk[k][1]] == 0 && FixedV[Tvardk[k][2]] == 0){
14512: ncovta++;
14513: TvarAVVA[ncovta]=Tvard[k1][1]; /* age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 */
14514: TvarAVVAind[ncovta]=k;
14515: ncovta++;
14516: TvarAVVA[ncovta]=Tvard[k1][2]; /* age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 */
14517: TvarAVVAind[ncovta]=k;
14518: }else{
14519: ncovva++; /* HERY reached */
14520: TvarVVA[ncovva]=Tvard[k1][1]; /* age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 */
14521: TvarVVAind[ncovva]=k;
14522: ncovva++;
14523: TvarVVA[ncovva]=Tvard[k1][2]; /* */
14524: TvarVVAind[ncovva]=k;
14525: ncovta++;
14526: TvarAVVA[ncovta]=Tvard[k1][1]; /* age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 */
14527: TvarAVVAind[ncovta]=k;
14528: ncovta++;
14529: TvarAVVA[ncovta]=Tvard[k1][2]; /* age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 */
14530: TvarAVVAind[ncovta]=k;
14531: }
14532: if(Tvard[k1][1] <=ncovcol){ /* Vn is dummy fixed, (Tvard[1][1]=V1), (Tvard[1][1]=V3 time varying) */
14533: if(Tvard[k1][2] <=ncovcol){ /* Vm is dummy fixed */
14534: Fixed[k]= 2;
14535: Dummy[k]= 2;
14536: modell[k].maintype= FTYPE;
14537: modell[k].subtype= FPDD; /* Product fixed dummy * fixed dummy */
14538: /* TvarF[ncova]=Tvar[k]; /\* Problem to solve *\/ */
14539: /* TvarFind[ncova]=k; */
14540: }else if(Tvard[k1][2] <=ncovcol+nqv){ /* Vm is quanti fixed */
14541: Fixed[k]= 2; /* Fixed product */
14542: Dummy[k]= 3;
14543: modell[k].maintype= FTYPE;
14544: modell[k].subtype= FPDQ; /* Product fixed dummy * fixed quantitative */
14545: /* TvarF[ncova]=Tvar[k]; */
14546: /* TvarFind[ncova]=k; */
14547: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){ /* Vm is a time varying dummy covariate */
14548: Fixed[k]= 3;
14549: Dummy[k]= 2;
14550: modell[k].maintype= VTYPE;
14551: modell[k].subtype= VPDD; /* Product fixed dummy * varying dummy */
14552: TvarV[ncova]=Tvar[k]; /* TvarV[1]=Tvar[5]=5 because there is a V4 */
14553: TvarVind[ncova]=k;/* TvarVind[1]=5 */
14554: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){ /* Vm is a time varying quantitative covariate */
14555: Fixed[k]= 3;
14556: Dummy[k]= 3;
14557: modell[k].maintype= VTYPE;
14558: modell[k].subtype= VPDQ; /* Product fixed dummy * varying quantitative */
14559: /* ncovv++; /\* Varying variables without age *\/ */
14560: /* TvarV[ncovv]=Tvar[k]; */
14561: /* TvarVind[ncovv]=k; */
14562: }
14563: }else if(Tvard[k1][1] <=ncovcol+nqv){ /* Vn is fixed quanti */
14564: if(Tvard[k1][2] <=ncovcol){ /* Vm is fixed dummy */
14565: Fixed[k]= 2; /* Fixed product */
14566: Dummy[k]= 2;
14567: modell[k].maintype= FTYPE;
14568: modell[k].subtype= FPDQ; /* Product fixed quantitative * fixed dummy */
14569: /* ncova++; /\* Fixed variables with age *\/ */
14570: /* TvarF[ncovf]=Tvar[k]; */
14571: /* TvarFind[ncovf]=k; */
14572: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){ /* Vm is time varying */
14573: Fixed[k]= 2;
14574: Dummy[k]= 3;
14575: modell[k].maintype= VTYPE;
14576: modell[k].subtype= VPDQ; /* Product fixed quantitative * varying dummy */
14577: /* ncova++; /\* Varying variables with age *\/ */
14578: /* TvarV[ncova]=Tvar[k]; */
14579: /* TvarVind[ncova]=k; */
14580: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){ /* Vm is time varying quanti */
14581: Fixed[k]= 3;
14582: Dummy[k]= 2;
14583: modell[k].maintype= VTYPE;
14584: modell[k].subtype= VPQQ; /* Product fixed quantitative * varying quantitative */
14585: ncova++; /* Varying variables without age */
14586: TvarV[ncova]=Tvar[k];
14587: TvarVind[ncova]=k;
14588: /* ncova++; /\* Varying variables without age *\/ */
14589: /* TvarV[ncova]=Tvar[k]; */
14590: /* TvarVind[ncova]=k; */
14591: }
14592: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv){ /* Vn is time varying dummy */
14593: if(Tvard[k1][2] <=ncovcol){
14594: Fixed[k]= 2;
14595: Dummy[k]= 2;
14596: modell[k].maintype= VTYPE;
14597: modell[k].subtype= VPDD; /* Product time varying dummy * fixed dummy */
14598: /* ncova++; /\* Varying variables with age *\/ */
14599: /* TvarV[ncova]=Tvar[k]; */
14600: /* TvarVind[ncova]=k; */
14601: }else if(Tvard[k1][2] <=ncovcol+nqv){
14602: Fixed[k]= 2;
14603: Dummy[k]= 3;
14604: modell[k].maintype= VTYPE;
14605: modell[k].subtype= VPDQ; /* Product time varying dummy * fixed quantitative */
14606: /* ncova++; /\* Varying variables with age *\/ */
14607: /* TvarV[ncova]=Tvar[k]; */
14608: /* TvarVind[ncova]=k; */
14609: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
14610: Fixed[k]= 3;
14611: Dummy[k]= 2;
14612: modell[k].maintype= VTYPE;
14613: modell[k].subtype= VPDD; /* Product time varying dummy * time varying dummy */
14614: /* ncova++; /\* Varying variables with age *\/ */
14615: /* TvarV[ncova]=Tvar[k]; */
14616: /* TvarVind[ncova]=k; */
14617: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
14618: Fixed[k]= 3;
14619: Dummy[k]= 3;
14620: modell[k].maintype= VTYPE;
14621: modell[k].subtype= VPDQ; /* Product time varying dummy * time varying quantitative */
14622: /* ncova++; /\* Varying variables with age *\/ */
14623: /* TvarV[ncova]=Tvar[k]; */
14624: /* TvarVind[ncova]=k; */
14625: }
14626: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv+nqtv){ /* Vn is time varying quanti */
14627: if(Tvard[k1][2] <=ncovcol){
14628: Fixed[k]= 2;
14629: Dummy[k]= 2;
14630: modell[k].maintype= VTYPE;
14631: modell[k].subtype= VPDQ; /* Product time varying quantitative * fixed dummy */
14632: /* ncova++; /\* Varying variables with age *\/ */
14633: /* TvarV[ncova]=Tvar[k]; */
14634: /* TvarVind[ncova]=k; */
14635: }else if(Tvard[k1][2] <=ncovcol+nqv){
14636: Fixed[k]= 2;
14637: Dummy[k]= 3;
14638: modell[k].maintype= VTYPE;
14639: modell[k].subtype= VPQQ; /* Product time varying quantitative * fixed quantitative */
14640: /* ncova++; /\* Varying variables with age *\/ */
14641: /* TvarV[ncova]=Tvar[k]; */
14642: /* TvarVind[ncova]=k; */
14643: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
14644: Fixed[k]= 3;
14645: Dummy[k]= 2;
14646: modell[k].maintype= VTYPE;
14647: modell[k].subtype= VPDQ; /* Product time varying quantitative * time varying dummy */
14648: /* ncova++; /\* Varying variables with age *\/ */
14649: /* TvarV[ncova]=Tvar[k]; */
14650: /* TvarVind[ncova]=k; */
14651: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
14652: Fixed[k]= 3;
14653: Dummy[k]= 3;
14654: modell[k].maintype= VTYPE;
14655: modell[k].subtype= VPQQ; /* Product time varying quantitative * time varying quantitative */
14656: /* ncova++; /\* Varying variables with age *\/ */
14657: /* TvarV[ncova]=Tvar[k]; */
14658: /* TvarVind[ncova]=k; */
14659: }
14660: }else{
14661: printf("Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
14662: fprintf(ficlog,"Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
14663: } /*end k1*/
14664: } else{
14665: printf("Error, current version can't treat for performance reasons, Tvar[%d]=%d, Typevar[%d]=%d\n", k, Tvar[k], k, Typevar[k]);
14666: fprintf(ficlog,"Error, current version can't treat for performance reasons, Tvar[%d]=%d, Typevar[%d]=%d\n", k, Tvar[k], k, Typevar[k]);
14667: }
14668: /* 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]); */
14669: /* printf(" modell[%d].maintype=%d, modell[%d].subtype=%d\n",k,modell[k].maintype,k,modell[k].subtype); */
14670: 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]);
14671: }
14672: ncovvta=ncovva;
14673: /* Searching for doublons in the model */
14674: for(k1=1; k1<= cptcovt;k1++){
14675: for(k2=1; k2 <k1;k2++){
14676: /* if((Typevar[k1]==Typevar[k2]) && (Fixed[Tvar[k1]]==Fixed[Tvar[k2]]) && (Dummy[Tvar[k1]]==Dummy[Tvar[k2]] )){ */
14677: if((Typevar[k1]==Typevar[k2]) && (Fixed[k1]==Fixed[k2]) && (Dummy[k1]==Dummy[k2] )){
14678: if((Typevar[k1] == 0 || Typevar[k1] == 1)){ /* Simple or age product */
14679: if(Tvar[k1]==Tvar[k2]){
14680: 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]);
14681: 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);
14682: return(1);
14683: }
14684: }else if (Typevar[k1] ==2){
14685: k3=Tposprod[k1];
14686: k4=Tposprod[k2];
14687: 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])) ){
14688: 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]]);
14689: 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);
14690: return(1);
14691: }
14692: }
14693: }
14694: }
14695: }
14696: printf("ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
14697: fprintf(ficlog,"ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
14698: printf("ncovf=%d, ncovv=%d, ncova=%d, nsd=%d, nsq=%d\n",ncovf,ncovv,ncova,nsd,nsq);
14699: fprintf(ficlog,"ncovf=%d, ncovv=%d, ncova=%d, nsd=%d, nsq=%d\n",ncovf,ncovv,ncova,nsd, nsq);
14700:
14701: free_imatrix(existcomb,1,NCOVMAX,1,NCOVMAX);
14702: return (0); /* with covar[new additional covariate if product] and Tage if age */
14703: /*endread:*/
14704: printf("Exiting decodemodel: ");
14705: return (1);
14706: }
14707:
14708: int calandcheckages(int imx, int maxwav, double *agemin, double *agemax, int *nberr, int *nbwarn )
14709: {/* Check ages at death */
14710: int i, m;
14711: int firstone=0;
14712:
14713: for (i=1; i<=imx; i++) {
14714: for(m=2; (m<= maxwav); m++) {
14715: if (((int)mint[m][i]== 99) && (s[m][i] <= nlstate)){
14716: anint[m][i]=9999;
14717: if (s[m][i] != -2) /* Keeping initial status of unknown vital status */
14718: s[m][i]=-1;
14719: }
14720: if((int)moisdc[i]==99 && (int)andc[i]==9999 && s[m][i]>nlstate){
14721: *nberr = *nberr + 1;
14722: if(firstone == 0){
14723: firstone=1;
14724: 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);
14725: }
14726: 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);
14727: s[m][i]=-1; /* Droping the death status */
14728: }
14729: if((int)moisdc[i]==99 && (int)andc[i]!=9999 && s[m][i]>nlstate){
14730: (*nberr)++;
14731: 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);
14732: 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);
14733: s[m][i]=-2; /* We prefer to skip it (and to skip it in version 0.8a1 too */
14734: }
14735: }
14736: }
14737:
14738: for (i=1; i<=imx; i++) {
14739: agedc[i]=(moisdc[i]/12.+andc[i])-(moisnais[i]/12.+annais[i]);
14740: for(m=firstpass; (m<= lastpass); m++){
14741: 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 */
14742: if (s[m][i] >= nlstate+1) {
14743: if(agedc[i]>0){
14744: if((int)moisdc[i]!=99 && (int)andc[i]!=9999){
14745: agev[m][i]=agedc[i];
14746: /*if(moisdc[i]==99 && andc[i]==9999) s[m][i]=-1;*/
14747: }else {
14748: if ((int)andc[i]!=9999){
14749: nbwarn++;
14750: printf("Warning negative age at death: %ld line:%d\n",num[i],i);
14751: fprintf(ficlog,"Warning negative age at death: %ld line:%d\n",num[i],i);
14752: agev[m][i]=-1;
14753: }
14754: }
14755: } /* agedc > 0 */
14756: } /* end if */
14757: else if(s[m][i] !=9){ /* Standard case, age in fractional
14758: years but with the precision of a month */
14759: agev[m][i]=(mint[m][i]/12.+1./24.+anint[m][i])-(moisnais[i]/12.+1./24.+annais[i]);
14760: if((int)mint[m][i]==99 || (int)anint[m][i]==9999)
14761: agev[m][i]=1;
14762: else if(agev[m][i] < *agemin){
14763: *agemin=agev[m][i];
14764: printf(" Min anint[%d][%d]=%.2f annais[%d]=%.2f, agemin=%.2f\n",m,i,anint[m][i], i,annais[i], *agemin);
14765: }
14766: else if(agev[m][i] >*agemax){
14767: *agemax=agev[m][i];
14768: /* printf(" Max anint[%d][%d]=%.0f annais[%d]=%.0f, agemax=%.2f\n",m,i,anint[m][i], i,annais[i], *agemax);*/
14769: }
14770: /*agev[m][i]=anint[m][i]-annais[i];*/
14771: /* agev[m][i] = age[i]+2*m;*/
14772: } /* en if 9*/
14773: else { /* =9 */
14774: /* printf("Debug num[%d]=%ld s[%d][%d]=%d\n",i,num[i], m,i, s[m][i]); */
14775: agev[m][i]=1;
14776: s[m][i]=-1;
14777: }
14778: }
14779: else if(s[m][i]==0) /*= 0 Unknown */
14780: agev[m][i]=1;
14781: else{
14782: printf("Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
14783: fprintf(ficlog, "Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
14784: agev[m][i]=0;
14785: }
14786: } /* End for lastpass */
14787: }
14788:
14789: for (i=1; i<=imx; i++) {
14790: for(m=firstpass; (m<=lastpass); m++){
14791: if (s[m][i] > (nlstate+ndeath)) {
14792: (*nberr)++;
14793: 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);
14794: 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);
14795: return 1;
14796: }
14797: }
14798: }
14799:
14800: /*for (i=1; i<=imx; i++){
14801: for (m=firstpass; (m<lastpass); m++){
14802: printf("%ld %d %.lf %d %d\n", num[i],(covar[1][i]),agev[m][i],s[m][i],s[m+1][i]);
14803: }
14804:
14805: }*/
14806:
14807:
14808: printf("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
14809: fprintf(ficlog,"Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
14810:
14811: return (0);
14812: /* endread:*/
14813: printf("Exiting calandcheckages: ");
14814: return (1);
14815: }
14816:
14817: #if defined(_MSC_VER)
14818: /*printf("Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
14819: /*fprintf(ficlog, "Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
14820: //#include "stdafx.h"
14821: //#include <stdio.h>
14822: //#include <tchar.h>
14823: //#include <windows.h>
14824: //#include <iostream>
14825: typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
14826:
14827: LPFN_ISWOW64PROCESS fnIsWow64Process;
14828:
14829: BOOL IsWow64()
14830: {
14831: BOOL bIsWow64 = FALSE;
14832:
14833: //typedef BOOL (APIENTRY *LPFN_ISWOW64PROCESS)
14834: // (HANDLE, PBOOL);
14835:
14836: //LPFN_ISWOW64PROCESS fnIsWow64Process;
14837:
14838: HMODULE module = GetModuleHandle(_T("kernel32"));
14839: const char funcName[] = "IsWow64Process";
14840: fnIsWow64Process = (LPFN_ISWOW64PROCESS)
14841: GetProcAddress(module, funcName);
14842:
14843: if (NULL != fnIsWow64Process)
14844: {
14845: if (!fnIsWow64Process(GetCurrentProcess(),
14846: &bIsWow64))
14847: //throw std::exception("Unknown error");
14848: printf("Unknown error\n");
14849: }
14850: return bIsWow64 != FALSE;
14851: }
14852: #endif
14853:
14854: void syscompilerinfo(int logged)
14855: {
14856: #include <stdint.h>
14857:
14858: /* #include "syscompilerinfo.h"*/
14859: /* command line Intel compiler 32bit windows, XP compatible:*/
14860: /* /GS /W3 /Gy
14861: /Zc:wchar_t /Zi /O2 /Fd"Release\vc120.pdb" /D "WIN32" /D "NDEBUG" /D
14862: "_CONSOLE" /D "_LIB" /D "_USING_V110_SDK71_" /D "_UNICODE" /D
14863: "UNICODE" /Qipo /Zc:forScope /Gd /Oi /MT /Fa"Release\" /EHsc /nologo
14864: /Fo"Release\" /Qprof-dir "Release\" /Fp"Release\IMaCh.pch"
14865: */
14866: /* 64 bits */
14867: /*
14868: /GS /W3 /Gy
14869: /Zc:wchar_t /Zi /O2 /Fd"x64\Release\vc120.pdb" /D "WIN32" /D "NDEBUG"
14870: /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo /Zc:forScope
14871: /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Fo"x64\Release\" /Qprof-dir
14872: "x64\Release\" /Fp"x64\Release\IMaCh.pch" */
14873: /* Optimization are useless and O3 is slower than O2 */
14874: /*
14875: /GS /W3 /Gy /Zc:wchar_t /Zi /O3 /Fd"x64\Release\vc120.pdb" /D "WIN32"
14876: /D "NDEBUG" /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo
14877: /Zc:forScope /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Qparallel
14878: /Fo"x64\Release\" /Qprof-dir "x64\Release\" /Fp"x64\Release\IMaCh.pch"
14879: */
14880: /* Link is */ /* /OUT:"visual studio
14881: 2013\Projects\IMaCh\Release\IMaCh.exe" /MANIFEST /NXCOMPAT
14882: /PDB:"visual studio
14883: 2013\Projects\IMaCh\Release\IMaCh.pdb" /DYNAMICBASE
14884: "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib"
14885: "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib"
14886: "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib"
14887: /MACHINE:X86 /OPT:REF /SAFESEH /INCREMENTAL:NO
14888: /SUBSYSTEM:CONSOLE",5.01" /MANIFESTUAC:"level='asInvoker'
14889: uiAccess='false'"
14890: /ManifestFile:"Release\IMaCh.exe.intermediate.manifest" /OPT:ICF
14891: /NOLOGO /TLBID:1
14892: */
14893:
14894:
14895: #if defined __INTEL_COMPILER
14896: #if defined(__GNUC__)
14897: struct utsname sysInfo; /* For Intel on Linux and OS/X */
14898: #endif
14899: #elif defined(__GNUC__)
14900: #ifndef __APPLE__
14901: #include <gnu/libc-version.h> /* Only on gnu */
14902: #endif
14903: struct utsname sysInfo;
14904: int cross = CROSS;
14905: if (cross){
14906: printf("Cross-");
14907: if(logged) fprintf(ficlog, "Cross-");
14908: }
14909: #endif
14910:
14911: printf("Compiled with:");if(logged)fprintf(ficlog,"Compiled with:");
14912: #if defined(__clang__)
14913: printf(" Clang/LLVM");if(logged)fprintf(ficlog," Clang/LLVM"); /* Clang/LLVM. ---------------------------------------------- */
14914: #endif
14915: #if defined(__ICC) || defined(__INTEL_COMPILER)
14916: printf(" Intel ICC/ICPC");if(logged)fprintf(ficlog," Intel ICC/ICPC");/* Intel ICC/ICPC. ------------------------------------------ */
14917: #endif
14918: #if defined(__GNUC__) || defined(__GNUG__)
14919: printf(" GNU GCC/G++");if(logged)fprintf(ficlog," GNU GCC/G++");/* GNU GCC/G++. --------------------------------------------- */
14920: #endif
14921: #if defined(__HP_cc) || defined(__HP_aCC)
14922: printf(" Hewlett-Packard C/aC++");if(logged)fprintf(fcilog," Hewlett-Packard C/aC++"); /* Hewlett-Packard C/aC++. ---------------------------------- */
14923: #endif
14924: #if defined(__IBMC__) || defined(__IBMCPP__)
14925: printf(" IBM XL C/C++"); if(logged) fprintf(ficlog," IBM XL C/C++");/* IBM XL C/C++. -------------------------------------------- */
14926: #endif
14927: #if defined(_MSC_VER)
14928: printf(" Microsoft Visual Studio");if(logged)fprintf(ficlog," Microsoft Visual Studio");/* Microsoft Visual Studio. --------------------------------- */
14929: #endif
14930: #if defined(__PGI)
14931: printf(" Portland Group PGCC/PGCPP");if(logged) fprintf(ficlog," Portland Group PGCC/PGCPP");/* Portland Group PGCC/PGCPP. ------------------------------- */
14932: #endif
14933: #if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
14934: printf(" Oracle Solaris Studio");if(logged)fprintf(ficlog," Oracle Solaris Studio\n");/* Oracle Solaris Studio. ----------------------------------- */
14935: #endif
14936: printf(" for "); if (logged) fprintf(ficlog, " for ");
14937:
14938: // http://stackoverflow.com/questions/4605842/how-to-identify-platform-compiler-from-preprocessor-macros
14939: #ifdef _WIN32 // note the underscore: without it, it's not msdn official!
14940: // Windows (x64 and x86)
14941: printf("Windows (x64 and x86) ");if(logged) fprintf(ficlog,"Windows (x64 and x86) ");
14942: #elif __unix__ // all unices, not all compilers
14943: // Unix
14944: printf("Unix ");if(logged) fprintf(ficlog,"Unix ");
14945: #elif __linux__
14946: // linux
14947: printf("linux ");if(logged) fprintf(ficlog,"linux ");
14948: #elif __APPLE__
14949: // Mac OS, not sure if this is covered by __posix__ and/or __unix__ though..
14950: printf("Mac OS ");if(logged) fprintf(ficlog,"Mac OS ");
14951: #endif
14952:
14953: /* __MINGW32__ */
14954: /* __CYGWIN__ */
14955: /* __MINGW64__ */
14956: // http://msdn.microsoft.com/en-us/library/b0084kay.aspx
14957: /* _MSC_VER //the Visual C++ compiler is 17.00.51106.1, the _MSC_VER macro evaluates to 1700. Type cl /? */
14958: /* _MSC_FULL_VER //the Visual C++ compiler is 15.00.20706.01, the _MSC_FULL_VER macro evaluates to 150020706 */
14959: /* _WIN64 // Defined for applications for Win64. */
14960: /* _M_X64 // Defined for compilations that target x64 processors. */
14961: /* _DEBUG // Defined when you compile with /LDd, /MDd, and /MTd. */
14962:
14963: #if UINTPTR_MAX == 0xffffffff
14964: printf(" 32-bit"); if(logged) fprintf(ficlog," 32-bit");/* 32-bit */
14965: #elif UINTPTR_MAX == 0xffffffffffffffff
14966: printf(" 64-bit"); if(logged) fprintf(ficlog," 64-bit");/* 64-bit */
14967: #else
14968: printf(" wtf-bit"); if(logged) fprintf(ficlog," wtf-bit");/* wtf */
14969: #endif
14970:
14971: #if defined(__GNUC__)
14972: # if defined(__GNUC_PATCHLEVEL__)
14973: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
14974: + __GNUC_MINOR__ * 100 \
14975: + __GNUC_PATCHLEVEL__)
14976: # else
14977: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
14978: + __GNUC_MINOR__ * 100)
14979: # endif
14980: printf(" using GNU C version %d.\n", __GNUC_VERSION__);
14981: if(logged) fprintf(ficlog, " using GNU C version %d.\n", __GNUC_VERSION__);
14982:
14983: if (uname(&sysInfo) != -1) {
14984: printf("Running on: %s %s %s %s %s\n",sysInfo.sysname, sysInfo.nodename, sysInfo.release, sysInfo.version, sysInfo.machine);
14985: if(logged) fprintf(ficlog,"Running on: %s %s %s %s %s\n ",sysInfo.sysname, sysInfo.nodename, sysInfo.release, sysInfo.version, sysInfo.machine);
14986: }
14987: else
14988: perror("uname() error");
14989: //#ifndef __INTEL_COMPILER
14990: #if !defined (__INTEL_COMPILER) && !defined(__APPLE__)
14991: printf("GNU libc version: %s\n", gnu_get_libc_version());
14992: if(logged) fprintf(ficlog,"GNU libc version: %s\n", gnu_get_libc_version());
14993: #endif
14994: #endif
14995:
14996: // void main ()
14997: // {
14998: #if defined(_MSC_VER)
14999: if (IsWow64()){
15000: printf("\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
15001: if (logged) fprintf(ficlog, "\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
15002: }
15003: else{
15004: printf("\nThe program is not running under WOW64 (i.e probably on a 64bit Windows).\n");
15005: if (logged) fprintf(ficlog, "\nThe programm is not running under WOW64 (i.e probably on a 64bit Windows).\n");
15006: }
15007: // printf("\nPress Enter to continue...");
15008: // getchar();
15009: // }
15010:
15011: #endif
15012:
15013:
15014: }
15015:
15016: int prevalence_limit(double *p, double **prlim, double ageminpar, double agemaxpar, double ftolpl, int *ncvyearp){
15017: /*--------------- Prevalence limit (forward period or forward stable prevalence) --------------*/
15018: /* Computes the prevalence limit for each combination of the dummy covariates */
15019: int i, j, k, i1, k4=0, nres=0 ;
15020: /* double ftolpl = 1.e-10; */
15021: double age, agebase, agelim;
15022: double tot;
15023:
15024: strcpy(filerespl,"PL_");
15025: strcat(filerespl,fileresu);
15026: if((ficrespl=fopen(filerespl,"w"))==NULL) {
15027: printf("Problem with forward period (stable) prevalence resultfile: %s\n", filerespl);return 1;
15028: fprintf(ficlog,"Problem with forward period (stable) prevalence resultfile: %s\n", filerespl);return 1;
15029: }
15030: printf("\nComputing forward period (stable) prevalence: result on file '%s' \n", filerespl);
15031: fprintf(ficlog,"\nComputing forward period (stable) prevalence: result on file '%s' \n", filerespl);
15032: pstamp(ficrespl);
15033: fprintf(ficrespl,"# Forward period (stable) prevalence. Precision given by ftolpl=%g \n", ftolpl);
15034: fprintf(ficrespl,"#Age ");
15035: for(i=1; i<=nlstate;i++) fprintf(ficrespl,"%d-%d ",i,i);
15036: fprintf(ficrespl,"\n");
15037:
15038: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
15039:
15040: agebase=ageminpar;
15041: agelim=agemaxpar;
15042:
15043: /* i1=pow(2,ncoveff); */
15044: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
15045: if (cptcovn < 1){i1=1;}
15046:
15047: /* for(k=1; k<=i1;k++){ /\* For each combination k of dummy covariates in the model *\/ */
15048: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
15049: k=TKresult[nres];
15050: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
15051: /* if(i1 != 1 && TKresult[nres]!= k) /\* We found the combination k corresponding to the resultline value of dummies *\/ */
15052: /* continue; */
15053:
15054: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
15055: /* for(cptcov=1,k=0;cptcov<=1;cptcov++){ */
15056: //for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
15057: /* k=k+1; */
15058: /* to clean */
15059: /*printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));*/
15060: fprintf(ficrespl,"#******");
15061: printf("#******");
15062: fprintf(ficlog,"#******");
15063: 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) */
15064: /* fprintf(ficrespl," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,Tvaraff[j])]); /\* Here problem for varying dummy*\/ */
15065: /* printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
15066: /* fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
15067: fprintf(ficrespl," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
15068: printf(" V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
15069: fprintf(ficlog," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
15070: }
15071: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
15072: /* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
15073: /* fprintf(ficrespl," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
15074: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
15075: /* } */
15076: fprintf(ficrespl,"******\n");
15077: printf("******\n");
15078: fprintf(ficlog,"******\n");
15079: if(invalidvarcomb[k]){
15080: printf("\nCombination (%d) ignored because no case \n",k);
15081: fprintf(ficrespl,"#Combination (%d) ignored because no case \n",k);
15082: fprintf(ficlog,"\nCombination (%d) ignored because no case \n",k);
15083: continue;
15084: }
15085:
15086: fprintf(ficrespl,"#Age ");
15087: /* for(j=1;j<=cptcoveff;j++) { */
15088: /* fprintf(ficrespl,"V%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
15089: /* } */
15090: for(j=1;j<=cptcovs;j++) { /* New the quanti variable is added */
15091: fprintf(ficrespl,"V%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
15092: }
15093: for(i=1; i<=nlstate;i++) fprintf(ficrespl," %d-%d ",i,i);
15094: fprintf(ficrespl,"Total Years_to_converge\n");
15095:
15096: for (age=agebase; age<=agelim; age++){
15097: /* for (age=agebase; age<=agebase; age++){ */
15098: /**< Computes the prevalence limit in each live state at age x and for covariate combination (k and) nres */
15099: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, ncvyearp, k, nres); /* Nicely done */
15100: fprintf(ficrespl,"%.0f ",age );
15101: /* for(j=1;j<=cptcoveff;j++) */
15102: /* fprintf(ficrespl,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
15103: for(j=1;j<=cptcovs;j++)
15104: fprintf(ficrespl,"%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
15105: tot=0.;
15106: for(i=1; i<=nlstate;i++){
15107: tot += prlim[i][i];
15108: fprintf(ficrespl," %.5f", prlim[i][i]);
15109: }
15110: fprintf(ficrespl," %.3f %d\n", tot, *ncvyearp);
15111: } /* Age */
15112: /* was end of cptcod */
15113: } /* nres */
15114: /* } /\* for each combination *\/ */
15115: return 0;
15116: }
15117:
15118: 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){
15119: /*--------------- Back Prevalence limit (backward stable prevalence) --------------*/
15120:
15121: /* Computes the back prevalence limit for any combination of covariate values
15122: * at any age between ageminpar and agemaxpar
15123: */
15124: int i, j, k, i1, nres=0 ;
15125: /* double ftolpl = 1.e-10; */
15126: double age, agebase, agelim;
15127: double tot;
15128: /* double ***mobaverage; */
15129: /* double **dnewm, **doldm, **dsavm; /\* for use *\/ */
15130:
15131: strcpy(fileresplb,"PLB_");
15132: strcat(fileresplb,fileresu);
15133: if((ficresplb=fopen(fileresplb,"w"))==NULL) {
15134: printf("Problem with backward prevalence resultfile: %s\n", fileresplb);return 1;
15135: fprintf(ficlog,"Problem with backward prevalence resultfile: %s\n", fileresplb);return 1;
15136: }
15137: printf("Computing backward prevalence: result on file '%s' \n", fileresplb);
15138: fprintf(ficlog,"Computing backward prevalence: result on file '%s' \n", fileresplb);
15139: pstamp(ficresplb);
15140: fprintf(ficresplb,"# Backward prevalence. Precision given by ftolpl=%g \n", ftolpl);
15141: fprintf(ficresplb,"#Age ");
15142: for(i=1; i<=nlstate;i++) fprintf(ficresplb,"%d-%d ",i,i);
15143: fprintf(ficresplb,"\n");
15144:
15145:
15146: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
15147:
15148: agebase=ageminpar;
15149: agelim=agemaxpar;
15150:
15151:
15152: i1=pow(2,cptcoveff);
15153: if (cptcovn < 1){i1=1;}
15154:
15155: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
15156: /* for(k=1; k<=i1;k++){ /\* For any combination of dummy covariates, fixed and varying *\/ */
15157: k=TKresult[nres];
15158: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
15159: /* if(i1 != 1 && TKresult[nres]!= k) */
15160: /* continue; */
15161: /* /\*printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));*\/ */
15162: fprintf(ficresplb,"#******");
15163: printf("#******");
15164: fprintf(ficlog,"#******");
15165: 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) */
15166: printf(" V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
15167: fprintf(ficresplb," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
15168: fprintf(ficlog," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
15169: }
15170: /* for(j=1;j<=cptcoveff ;j++) {/\* all covariates *\/ */
15171: /* fprintf(ficresplb," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
15172: /* printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
15173: /* fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
15174: /* } */
15175: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
15176: /* printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
15177: /* fprintf(ficresplb," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
15178: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
15179: /* } */
15180: fprintf(ficresplb,"******\n");
15181: printf("******\n");
15182: fprintf(ficlog,"******\n");
15183: if(invalidvarcomb[k]){
15184: printf("\nCombination (%d) ignored because no cases \n",k);
15185: fprintf(ficresplb,"#Combination (%d) ignored because no cases \n",k);
15186: fprintf(ficlog,"\nCombination (%d) ignored because no cases \n",k);
15187: continue;
15188: }
15189:
15190: fprintf(ficresplb,"#Age ");
15191: for(j=1;j<=cptcovs;j++) {
15192: fprintf(ficresplb,"V%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
15193: }
15194: for(i=1; i<=nlstate;i++) fprintf(ficresplb," %d-%d ",i,i);
15195: fprintf(ficresplb,"Total Years_to_converge\n");
15196:
15197:
15198: for (age=agebase; age<=agelim; age++){
15199: /* for (age=agebase; age<=agebase; age++){ */
15200: if(mobilavproj > 0){
15201: /* bprevalim(bprlim, mobaverage, nlstate, p, age, ageminpar, agemaxpar, oldm, savm, doldm, dsavm, ftolpl, ncvyearp, k); */
15202: /* bprevalim(bprlim, mobaverage, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
15203: bprevalim(bprlim, mobaverage, nlstate, p, age, ftolpl, ncvyearp, k, nres);
15204: }else if (mobilavproj == 0){
15205: 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);
15206: 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);
15207: exit(1);
15208: }else{
15209: /* bprevalim(bprlim, probs, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
15210: bprevalim(bprlim, probs, nlstate, p, age, ftolpl, ncvyearp, k,nres);
15211: /* printf("TOTOT\n"); */
15212: /* exit(1); */
15213: }
15214: fprintf(ficresplb,"%.0f ",age );
15215: for(j=1;j<=cptcovs;j++)
15216: fprintf(ficresplb,"%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
15217: tot=0.;
15218: for(i=1; i<=nlstate;i++){
15219: tot += bprlim[i][i];
15220: fprintf(ficresplb," %.5f", bprlim[i][i]);
15221: }
15222: fprintf(ficresplb," %.3f %d\n", tot, *ncvyearp);
15223: } /* Age */
15224: /* was end of cptcod */
15225: /*fprintf(ficresplb,"\n");*/ /* Seems to be necessary for gnuplot only if two result lines and no covariate. */
15226: /* } /\* end of any combination *\/ */
15227: } /* end of nres */
15228: /* hBijx(p, bage, fage); */
15229: /* fclose(ficrespijb); */
15230:
15231: return 0;
15232: }
15233:
15234: int hPijx(double *p, int bage, int fage){
15235: /*------------- h Pij x at various ages ------------*/
15236: /* to be optimized with precov */
15237: int stepsize;
15238: int agelim;
15239: int hstepm;
15240: int nhstepm;
15241: int h, i, i1, j, k, k4, nres=0;
15242:
15243: double agedeb;
15244: double ***p3mat;
15245:
15246: strcpy(filerespij,"PIJ_"); strcat(filerespij,fileresu);
15247: if((ficrespij=fopen(filerespij,"w"))==NULL) {
15248: printf("Problem with Pij resultfile: %s\n", filerespij); return 1;
15249: fprintf(ficlog,"Problem with Pij resultfile: %s\n", filerespij); return 1;
15250: }
15251: printf("Computing pij: result on file '%s' \n", filerespij);
15252: fprintf(ficlog,"Computing pij: result on file '%s' \n", filerespij);
15253:
15254: stepsize=(int) (stepm+YEARM-1)/YEARM;
15255: /*if (stepm<=24) stepsize=2;*/
15256:
15257: agelim=AGESUP;
15258: hstepm=stepsize*YEARM; /* Every year of age */
15259: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
15260:
15261: /* hstepm=1; aff par mois*/
15262: pstamp(ficrespij);
15263: fprintf(ficrespij,"#****** h Pij x Probability to be in state j at age x+h being in i at x ");
15264: i1= pow(2,cptcoveff);
15265: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
15266: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
15267: /* k=k+1; */
15268: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
15269: k=TKresult[nres];
15270: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
15271: /* for(k=1; k<=i1;k++){ */
15272: /* if(i1 != 1 && TKresult[nres]!= k) */
15273: /* continue; */
15274: fprintf(ficrespij,"\n#****** ");
15275: for(j=1;j<=cptcovs;j++){
15276: fprintf(ficrespij," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
15277: /* fprintf(ficrespij,"@wV%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
15278: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
15279: /* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
15280: /* fprintf(ficrespij," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
15281: }
15282: fprintf(ficrespij,"******\n");
15283:
15284: for (agedeb=fage; agedeb>=bage; agedeb--){ /* If stepm=6 months */
15285: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
15286: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
15287:
15288: /* nhstepm=nhstepm*YEARM; aff par mois*/
15289:
15290: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
15291: oldm=oldms;savm=savms;
15292: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k, nres);
15293: fprintf(ficrespij,"# Cov Agex agex+h hpijx with i,j=");
15294: for(i=1; i<=nlstate;i++)
15295: for(j=1; j<=nlstate+ndeath;j++)
15296: fprintf(ficrespij," %1d-%1d",i,j);
15297: fprintf(ficrespij,"\n");
15298: for (h=0; h<=nhstepm; h++){
15299: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
15300: fprintf(ficrespij,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm );
15301: for(i=1; i<=nlstate;i++)
15302: for(j=1; j<=nlstate+ndeath;j++)
15303: fprintf(ficrespij," %.5f", p3mat[i][j][h]);
15304: fprintf(ficrespij,"\n");
15305: }
15306: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
15307: fprintf(ficrespij,"\n");
15308: }
15309: }
15310: /*}*/
15311: return 0;
15312: }
15313:
15314: int hBijx(double *p, int bage, int fage, double ***prevacurrent){
15315: /*------------- h Bij x at various ages ------------*/
15316: /* To be optimized with precov */
15317: int stepsize;
15318: /* int agelim; */
15319: int ageminl;
15320: int hstepm;
15321: int nhstepm;
15322: int h, i, i1, j, k, nres;
15323:
15324: double agedeb;
15325: double ***p3mat;
15326:
15327: strcpy(filerespijb,"PIJB_"); strcat(filerespijb,fileresu);
15328: if((ficrespijb=fopen(filerespijb,"w"))==NULL) {
15329: printf("Problem with Pij back resultfile: %s\n", filerespijb); return 1;
15330: fprintf(ficlog,"Problem with Pij back resultfile: %s\n", filerespijb); return 1;
15331: }
15332: printf("Computing pij back: result on file '%s' \n", filerespijb);
15333: fprintf(ficlog,"Computing pij back: result on file '%s' \n", filerespijb);
15334:
15335: stepsize=(int) (stepm+YEARM-1)/YEARM;
15336: /*if (stepm<=24) stepsize=2;*/
15337:
15338: /* agelim=AGESUP; */
15339: ageminl=AGEINF; /* was 30 */
15340: hstepm=stepsize*YEARM; /* Every year of age */
15341: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
15342:
15343: /* hstepm=1; aff par mois*/
15344: pstamp(ficrespijb);
15345: fprintf(ficrespijb,"#****** h Bij x Back probability to be in state i at age x-h being in j at x: B1j+B2j+...=1 ");
15346: i1= pow(2,cptcoveff);
15347: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
15348: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
15349: /* k=k+1; */
15350: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
15351: k=TKresult[nres];
15352: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
15353: /* for(k=1; k<=i1;k++){ /\* For any combination of dummy covariates, fixed and varying *\/ */
15354: /* if(i1 != 1 && TKresult[nres]!= k) */
15355: /* continue; */
15356: fprintf(ficrespijb,"\n#****** ");
15357: for(j=1;j<=cptcovs;j++){
15358: fprintf(ficrespijb," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
15359: /* for(j=1;j<=cptcoveff;j++) */
15360: /* fprintf(ficrespijb,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
15361: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
15362: /* fprintf(ficrespijb," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
15363: }
15364: fprintf(ficrespijb,"******\n");
15365: if(invalidvarcomb[k]){ /* Is it necessary here? */
15366: fprintf(ficrespijb,"\n#Combination (%d) ignored because no cases \n",k);
15367: continue;
15368: }
15369:
15370: /* for (agedeb=fage; agedeb>=bage; agedeb--){ /\* If stepm=6 months *\/ */
15371: for (agedeb=bage; agedeb<=fage; agedeb++){ /* If stepm=6 months and estepm=24 (2 years) */
15372: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /\* Typically 20 years = 20*12/6=40 *\/ */
15373: 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 */
15374: nhstepm = nhstepm/hstepm; /* Typically 40/4=10, because estepm=24 stepm=6 => hstepm=24/6=4 or 28*/
15375:
15376: /* nhstepm=nhstepm*YEARM; aff par mois*/
15377:
15378: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); /* We can't have it at an upper level because of nhstepm */
15379: /* and memory limitations if stepm is small */
15380:
15381: /* oldm=oldms;savm=savms; */
15382: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
15383: hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm, k, nres);/* Bug valgrind */
15384: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm,oldm,savm, dnewm, doldm, dsavm, k); */
15385: fprintf(ficrespijb,"# Cov Agex agex-h hbijx with i,j=");
15386: for(i=1; i<=nlstate;i++)
15387: for(j=1; j<=nlstate+ndeath;j++)
15388: fprintf(ficrespijb," %1d-%1d",i,j);
15389: fprintf(ficrespijb,"\n");
15390: for (h=0; h<=nhstepm; h++){
15391: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
15392: fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb - h*hstepm/YEARM*stepm );
15393: /* fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm ); */
15394: for(i=1; i<=nlstate;i++)
15395: for(j=1; j<=nlstate+ndeath;j++)
15396: fprintf(ficrespijb," %.5f", p3mat[i][j][h]);/* Bug valgrind */
15397: fprintf(ficrespijb,"\n");
15398: }
15399: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
15400: fprintf(ficrespijb,"\n");
15401: } /* end age deb */
15402: /* } /\* end combination *\/ */
15403: } /* end nres */
15404: return 0;
15405: } /* hBijx */
15406:
15407:
15408: /***********************************************/
15409: /**************** Main Program *****************/
15410: /***********************************************/
15411:
15412: int main(int argc, char *argv[])
15413: {
15414: #ifdef GSL
15415: const gsl_multimin_fminimizer_type *T;
15416: size_t iteri = 0, it;
15417: int rval = GSL_CONTINUE;
15418: int status = GSL_SUCCESS;
15419: double ssval;
15420: #endif
15421: int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav);
15422: int i,j, k, iter=0,m,size=100, cptcod; /* Suppressing because nobs */
15423: /* int i,j, k, n=MAXN,iter=0,m,size=100, cptcod; */
15424: int ncvyear=0; /* Number of years needed for the period prevalence to converge */
15425: int jj, ll, li, lj, lk;
15426: int numlinepar=0; /* Current linenumber of parameter file */
15427: int num_filled;
15428: int itimes;
15429: int NDIM=2;
15430: int vpopbased=0;
15431: int nres=0;
15432: int endishere=0;
15433: int noffset=0;
15434: int ncurrv=0; /* Temporary variable */
15435:
15436: char ca[32], cb[32];
15437: /* FILE *fichtm; *//* Html File */
15438: /* FILE *ficgp;*/ /*Gnuplot File */
15439: struct stat info;
15440: double agedeb=0.;
15441:
15442: double ageminpar=AGEOVERFLOW,agemin=AGEOVERFLOW, agemaxpar=-AGEOVERFLOW, agemax=-AGEOVERFLOW;
15443: double ageminout=-AGEOVERFLOW,agemaxout=AGEOVERFLOW; /* Smaller Age range redefined after movingaverage */
15444:
15445: double fret;
15446: double dum=0.; /* Dummy variable */
15447: double ***p3mat;
15448: /* double ***mobaverage; */
15449: double wald;
15450:
15451: char line[MAXLINE];
15452: char path[MAXLINE],pathc[MAXLINE],pathcd[MAXLINE],pathtot[MAXLINE];
15453:
15454: char modeltemp[MAXLINE];
15455: char resultline[MAXLINE], resultlineori[MAXLINE];
15456:
15457: char pathr[MAXLINE], pathimach[MAXLINE];
15458: char *tok, *val; /* pathtot */
15459: /* int firstobs=1, lastobs=10; /\* nobs = lastobs-firstobs declared globally ;*\/ */
15460: int c, h , cpt, c2;
15461: int jl=0;
15462: int i1, j1, jk, stepsize=0;
15463: int count=0;
15464:
15465: int *tab;
15466: int mobilavproj=0 , prevfcast=0 ; /* moving average of prev, If prevfcast=1 prevalence projection */
15467: /* double anprojd, mprojd, jprojd; /\* For eventual projections *\/ */
15468: /* double anprojf, mprojf, jprojf; */
15469: /* double jintmean,mintmean,aintmean; */
15470: int prvforecast = 0; /* Might be 1 (date of beginning of projection is a choice or 2 is the dateintmean */
15471: int prvbackcast = 0; /* Might be 1 (date of beginning of projection is a choice or 2 is the dateintmean */
15472: double yrfproj= 10.0; /* Number of years of forward projections */
15473: double yrbproj= 10.0; /* Number of years of backward projections */
15474: int prevbcast=0; /* defined as global for mlikeli and mle, replacing backcast */
15475: int mobilav=0,popforecast=0;
15476: int hstepm=0, nhstepm=0;
15477: int agemortsup;
15478: float sumlpop=0.;
15479: double jprev1=1, mprev1=1,anprev1=2000,jprev2=1, mprev2=1,anprev2=2000;
15480: double jpyram=1, mpyram=1,anpyram=2000,jpyram1=1, mpyram1=1,anpyram1=2000;
15481:
15482: double bage=0, fage=110., age, agelim=0., agebase=0.;
15483: double ftolpl=FTOL;
15484: double **prlim;
15485: double **bprlim;
15486: double ***param; /* Matrix of parameters, param[i][j][k] param=ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel)
15487: state of origin, state of destination including death, for each covariate: constante, age, and V1 V2 etc. */
15488: double ***paramstart; /* Matrix of starting parameter values */
15489: double *p, *pstart; /* p=param[1][1] pstart is for starting values guessed by freqsummary */
15490: double **matcov; /* Matrix of covariance */
15491: double **hess; /* Hessian matrix */
15492: double ***delti3; /* Scale */
15493: double *delti; /* Scale */
15494: double ***eij, ***vareij;
15495: double **varpl; /* Variances of prevalence limits by age */
15496:
15497: double *epj, vepp;
15498:
15499: double dateprev1, dateprev2;
15500: double jproj1=1,mproj1=1,anproj1=2000,jproj2=1,mproj2=1,anproj2=2000, dateproj1=0, dateproj2=0, dateprojd=0, dateprojf=0;
15501: double jback1=1,mback1=1,anback1=2000,jback2=1,mback2=1,anback2=2000, dateback1=0, dateback2=0, datebackd=0, datebackf=0;
15502:
15503:
15504: double **ximort;
15505: char *alph[]={"a","a","b","c","d","e"}, str[4]="1234";
15506: int *dcwave;
15507:
15508: char z[1]="c";
15509:
15510: /*char *strt;*/
15511: char strtend[80];
15512:
15513:
15514: /* setlocale (LC_ALL, ""); */
15515: /* bindtextdomain (PACKAGE, LOCALEDIR); */
15516: /* textdomain (PACKAGE); */
15517: /* setlocale (LC_CTYPE, ""); */
15518: /* setlocale (LC_MESSAGES, ""); */
15519:
15520: /* gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */
15521: rstart_time = time(NULL);
15522: /* (void) gettimeofday(&start_time,&tzp);*/
15523: start_time = *localtime(&rstart_time);
15524: curr_time=start_time;
15525: /*tml = *localtime(&start_time.tm_sec);*/
15526: /* strcpy(strstart,asctime(&tml)); */
15527: strcpy(strstart,asctime(&start_time));
15528:
15529: /* printf("Localtime (at start)=%s",strstart); */
15530: /* tp.tm_sec = tp.tm_sec +86400; */
15531: /* tm = *localtime(&start_time.tm_sec); */
15532: /* tmg.tm_year=tmg.tm_year +dsign*dyear; */
15533: /* tmg.tm_mon=tmg.tm_mon +dsign*dmonth; */
15534: /* tmg.tm_hour=tmg.tm_hour + 1; */
15535: /* tp.tm_sec = mktime(&tmg); */
15536: /* strt=asctime(&tmg); */
15537: /* printf("Time(after) =%s",strstart); */
15538: /* (void) time (&time_value);
15539: * printf("time=%d,t-=%d\n",time_value,time_value-86400);
15540: * tm = *localtime(&time_value);
15541: * strstart=asctime(&tm);
15542: * printf("tim_value=%d,asctime=%s\n",time_value,strstart);
15543: */
15544:
15545: nberr=0; /* Number of errors and warnings */
15546: nbwarn=0;
15547: #ifdef WIN32
15548: _getcwd(pathcd, size);
15549: #else
15550: getcwd(pathcd, size);
15551: #endif
15552: syscompilerinfo(0);
15553: printf("\nIMaCh version %s, %s\n%s",version, copyright, fullversion);
15554: if(argc <=1){
15555: printf("\nEnter the parameter file name: ");
15556: if(!fgets(pathr,FILENAMELENGTH,stdin)){
15557: printf("ERROR Empty parameter file name\n");
15558: goto end;
15559: }
15560: i=strlen(pathr);
15561: if(pathr[i-1]=='\n')
15562: pathr[i-1]='\0';
15563: i=strlen(pathr);
15564: if(i >= 1 && pathr[i-1]==' ') {/* This may happen when dragging on oS/X! */
15565: pathr[i-1]='\0';
15566: }
15567: i=strlen(pathr);
15568: if( i==0 ){
15569: printf("ERROR Empty parameter file name\n");
15570: goto end;
15571: }
15572: for (tok = pathr; tok != NULL; ){
15573: printf("Pathr |%s|\n",pathr);
15574: while ((val = strsep(&tok, "\"" )) != NULL && *val == '\0');
15575: printf("val= |%s| pathr=%s\n",val,pathr);
15576: strcpy (pathtot, val);
15577: if(pathr[0] == '\0') break; /* Dirty */
15578: }
15579: }
15580: else if (argc<=2){
15581: strcpy(pathtot,argv[1]);
15582: }
15583: else{
15584: strcpy(pathtot,argv[1]);
15585: strcpy(z,argv[2]);
15586: printf("\nargv[2]=%s z=%c\n",argv[2],z[0]);
15587: }
15588: /*if(getcwd(pathcd, MAXLINE)!= NULL)printf ("Error pathcd\n");*/
15589: /*cygwin_split_path(pathtot,path,optionfile);
15590: printf("pathtot=%s, path=%s, optionfile=%s\n",pathtot,path,optionfile);*/
15591: /* cutv(path,optionfile,pathtot,'\\');*/
15592:
15593: /* Split argv[0], imach program to get pathimach */
15594: printf("\nargv[0]=%s argv[1]=%s, \n",argv[0],argv[1]);
15595: split(argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
15596: printf("\nargv[0]=%s pathimach=%s, \noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
15597: /* strcpy(pathimach,argv[0]); */
15598: /* Split argv[1]=pathtot, parameter file name to get path, optionfile, extension and name */
15599: split(pathtot,path,optionfile,optionfilext,optionfilefiname);
15600: printf("\npathtot=%s,\npath=%s,\noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname);
15601: #ifdef WIN32
15602: _chdir(path); /* Can be a relative path */
15603: if(_getcwd(pathcd,MAXLINE) > 0) /* So pathcd is the full path */
15604: #else
15605: chdir(path); /* Can be a relative path */
15606: if (getcwd(pathcd, MAXLINE) > 0) /* So pathcd is the full path */
15607: #endif
15608: printf("Current directory %s!\n",pathcd);
15609: strcpy(command,"mkdir ");
15610: strcat(command,optionfilefiname);
15611: if((outcmd=system(command)) != 0){
15612: printf("Directory already exists (or can't create it) %s%s, err=%d\n",path,optionfilefiname,outcmd);
15613: /* fprintf(ficlog,"Problem creating directory %s%s\n",path,optionfilefiname); */
15614: /* fclose(ficlog); */
15615: /* exit(1); */
15616: }
15617: /* if((imk=mkdir(optionfilefiname))<0){ */
15618: /* perror("mkdir"); */
15619: /* } */
15620:
15621: /*-------- arguments in the command line --------*/
15622:
15623: /* Main Log file */
15624: strcat(filelog, optionfilefiname);
15625: strcat(filelog,".log"); /* */
15626: if((ficlog=fopen(filelog,"w"))==NULL) {
15627: printf("Problem with logfile %s\n",filelog);
15628: goto end;
15629: }
15630: fprintf(ficlog,"Log filename:%s\n",filelog);
15631: fprintf(ficlog,"Version %s %s",version,fullversion);
15632: fprintf(ficlog,"\nEnter the parameter file name: \n");
15633: fprintf(ficlog,"pathimach=%s\npathtot=%s\n\
15634: path=%s \n\
15635: optionfile=%s\n\
15636: optionfilext=%s\n\
15637: optionfilefiname='%s'\n",pathimach,pathtot,path,optionfile,optionfilext,optionfilefiname);
15638:
15639: syscompilerinfo(1);
15640:
15641: printf("Local time (at start):%s",strstart);
15642: fprintf(ficlog,"Local time (at start): %s",strstart);
15643: fflush(ficlog);
15644: /* (void) gettimeofday(&curr_time,&tzp); */
15645: /* printf("Elapsed time %d\n", asc_diff_time(curr_time.tm_sec-start_time.tm_sec,tmpout)); */
15646:
15647: /* */
15648: strcpy(fileres,"r");
15649: strcat(fileres, optionfilefiname);
15650: strcat(fileresu, optionfilefiname); /* Without r in front */
15651: strcat(fileres,".txt"); /* Other files have txt extension */
15652: strcat(fileresu,".txt"); /* Other files have txt extension */
15653:
15654: /* Main ---------arguments file --------*/
15655:
15656: if((ficpar=fopen(optionfile,"r"))==NULL) {
15657: printf("Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
15658: fprintf(ficlog,"Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
15659: fflush(ficlog);
15660: /* goto end; */
15661: exit(70);
15662: }
15663:
15664: strcpy(filereso,"o");
15665: strcat(filereso,fileresu);
15666: if((ficparo=fopen(filereso,"w"))==NULL) { /* opened on subdirectory */
15667: printf("Problem with Output resultfile: %s\n", filereso);
15668: fprintf(ficlog,"Problem with Output resultfile: %s\n", filereso);
15669: fflush(ficlog);
15670: goto end;
15671: }
15672: /*-------- Rewriting parameter file ----------*/
15673: strcpy(rfileres,"r"); /* "Rparameterfile */
15674: strcat(rfileres,optionfilefiname); /* Parameter file first name */
15675: strcat(rfileres,"."); /* */
15676: strcat(rfileres,optionfilext); /* Other files have txt extension */
15677: if((ficres =fopen(rfileres,"w"))==NULL) {
15678: printf("Problem writing new parameter file: %s\n", rfileres);goto end;
15679: fprintf(ficlog,"Problem writing new parameter file: %s\n", rfileres);goto end;
15680: fflush(ficlog);
15681: goto end;
15682: }
15683: fprintf(ficres,"#IMaCh %s\n",version);
15684:
15685:
15686: /* Reads comments: lines beginning with '#' */
15687: numlinepar=0;
15688: /* Is it a BOM UTF-8 Windows file? */
15689: /* First parameter line */
15690: while(fgets(line, MAXLINE, ficpar)) {
15691: noffset=0;
15692: if( line[0] == (char)0xEF && line[1] == (char)0xBB) /* EF BB BF */
15693: {
15694: noffset=noffset+3;
15695: printf("# File is an UTF8 Bom.\n"); // 0xBF
15696: }
15697: /* else if( line[0] == (char)0xFE && line[1] == (char)0xFF)*/
15698: else if( line[0] == (char)0xFF && line[1] == (char)0xFE)
15699: {
15700: noffset=noffset+2;
15701: printf("# File is an UTF16BE BOM file\n");
15702: }
15703: else if( line[0] == 0 && line[1] == 0)
15704: {
15705: if( line[2] == (char)0xFE && line[3] == (char)0xFF){
15706: noffset=noffset+4;
15707: printf("# File is an UTF16BE BOM file\n");
15708: }
15709: } else{
15710: ;/*printf(" Not a BOM file\n");*/
15711: }
15712:
15713: /* If line starts with a # it is a comment */
15714: if (line[noffset] == '#') {
15715: numlinepar++;
15716: fputs(line,stdout);
15717: fputs(line,ficparo);
15718: fputs(line,ficres);
15719: fputs(line,ficlog);
15720: continue;
15721: }else
15722: break;
15723: }
15724: if((num_filled=sscanf(line,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", \
15725: title, datafile, &lastobs, &firstpass,&lastpass)) !=EOF){
15726: if (num_filled != 5) {
15727: printf("Should be 5 parameters\n");
15728: fprintf(ficlog,"Should be 5 parameters\n");
15729: }
15730: numlinepar++;
15731: printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
15732: fprintf(ficparo,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
15733: fprintf(ficres,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
15734: fprintf(ficlog,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
15735: }
15736: /* Second parameter line */
15737: while(fgets(line, MAXLINE, ficpar)) {
15738: /* while(fscanf(ficpar,"%[^\n]", line)) { */
15739: /* If line starts with a # it is a comment. Strangely fgets reads the EOL and fputs doesn't */
15740: if (line[0] == '#') {
15741: numlinepar++;
15742: printf("%s",line);
15743: fprintf(ficres,"%s",line);
15744: fprintf(ficparo,"%s",line);
15745: fprintf(ficlog,"%s",line);
15746: continue;
15747: }else
15748: break;
15749: }
15750: 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", \
15751: &ftol, &stepm, &ncovcol, &nqv, &ntv, &nqtv, &nlstate, &ndeath, &maxwav, &mle, &weightopt)) !=EOF){
15752: if (num_filled != 11) {
15753: 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");
15754: printf("but line=%s\n",line);
15755: 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");
15756: fprintf(ficlog,"but line=%s\n",line);
15757: }
15758: if( lastpass > maxwav){
15759: printf("Error (lastpass = %d) > (maxwav = %d)\n",lastpass, maxwav);
15760: fprintf(ficlog,"Error (lastpass = %d) > (maxwav = %d)\n",lastpass, maxwav);
15761: fflush(ficlog);
15762: goto end;
15763: }
15764: 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);
15765: 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);
15766: 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);
15767: 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);
15768: }
15769: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
15770: /*ftolpl=6.e-4; *//* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
15771: /* Third parameter line */
15772: while(fgets(line, MAXLINE, ficpar)) {
15773: /* If line starts with a # it is a comment */
15774: if (line[0] == '#') {
15775: numlinepar++;
15776: printf("%s",line);
15777: fprintf(ficres,"%s",line);
15778: fprintf(ficparo,"%s",line);
15779: fprintf(ficlog,"%s",line);
15780: continue;
15781: }else
15782: break;
15783: }
15784: if((num_filled=sscanf(line,"model=1+age%[^.\n]", model)) !=EOF){
15785: if (num_filled != 1){
15786: printf("ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line);
15787: fprintf(ficlog,"ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line);
15788: model[0]='\0';
15789: goto end;
15790: }
15791: else{
15792: if (model[0]=='+'){
15793: for(i=1; i<=strlen(model);i++)
15794: modeltemp[i-1]=model[i];
15795: strcpy(model,modeltemp);
15796: }
15797: }
15798: /* printf(" model=1+age%s modeltemp= %s, model=1+age+%s\n",model, modeltemp, model);fflush(stdout); */
15799: printf("model=1+age+%s\n",model);fflush(stdout);
15800: fprintf(ficparo,"model=1+age+%s\n",model);fflush(stdout);
15801: fprintf(ficres,"model=1+age+%s\n",model);fflush(stdout);
15802: fprintf(ficlog,"model=1+age+%s\n",model);fflush(stdout);
15803: }
15804: /* 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); */
15805: /* numlinepar=numlinepar+3; /\* In general *\/ */
15806: /* 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); */
15807: /* 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); */
15808: /* 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); */
15809: fflush(ficlog);
15810: /* if(model[0]=='#'|| model[0]== '\0'){ */
15811: if(model[0]=='#'){
15812: printf("Error in 'model' line: model should start with 'model=1+age+' and end without space \n \
15813: 'model=1+age+' or 'model=1+age+V1.' or 'model=1+age+age*age+V1+V1*age' or \n \
15814: 'model=1+age+V1+V2' or 'model=1+age+V1+V2+V1*V2' etc. \n"); \
15815: if(mle != -1){
15816: 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");
15817: exit(1);
15818: }
15819: }
15820: while((c=getc(ficpar))=='#' && c!= EOF){
15821: ungetc(c,ficpar);
15822: fgets(line, MAXLINE, ficpar);
15823: numlinepar++;
15824: if(line[1]=='q'){ /* This #q will quit imach (the answer is q) */
15825: z[0]=line[1];
15826: }else if(line[1]=='d'){ /* For debugging individual values of covariates in ficresilk */
15827: debugILK=1;printf("DebugILK\n");
15828: }
15829: /* printf("****line [1] = %c \n",line[1]); */
15830: fputs(line, stdout);
15831: //puts(line);
15832: fputs(line,ficparo);
15833: fputs(line,ficlog);
15834: }
15835: ungetc(c,ficpar);
15836:
15837:
15838: covar=matrix(0,NCOVMAX,firstobs,lastobs); /**< used in readdata */
15839: if(nqv>=1)coqvar=matrix(1,nqv,firstobs,lastobs); /**< Fixed quantitative covariate */
15840: if(nqtv>=1)cotqvar=ma3x(1,maxwav,1,nqtv,firstobs,lastobs); /**< Time varying quantitative covariate */
15841: /* if(ntv+nqtv>=1)cotvar=ma3x(1,maxwav,1,ntv+nqtv,firstobs,lastobs); /\**< Time varying covariate (dummy and quantitative)*\/ */
15842: if(ntv+nqtv>=1)cotvar=ma3x(1,maxwav,ncovcol+nqv+1,ncovcol+nqv+ntv+nqtv,firstobs,lastobs); /**< Might be better */
15843: cptcovn=0; /*Number of covariates, i.e. number of '+' in model statement plus one, indepently of n in Vn*/
15844: /* v1+v2+v3+v2*v4+v5*age makes cptcovn = 5
15845: v1+v2*age+v2*v3 makes cptcovn = 3
15846: */
15847: if (strlen(model)>1)
15848: 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*/
15849: else
15850: ncovmodel=2; /* Constant and age */
15851: nforce= (nlstate+ndeath-1)*nlstate; /* Number of forces ij from state i to j */
15852: npar= nforce*ncovmodel; /* Number of parameters like aij*/
15853: if(npar >MAXPARM || nlstate >NLSTATEMAX || ndeath >NDEATHMAX || ncovmodel>NCOVMAX){
15854: 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);
15855: 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);
15856: fflush(stdout);
15857: fclose (ficlog);
15858: goto end;
15859: }
15860: delti3= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
15861: delti=delti3[1][1];
15862: /*delti=vector(1,npar); *//* Scale of each paramater (output from hesscov)*/
15863: if(mle==-1){ /* Print a wizard for help writing covariance matrix */
15864: /* We could also provide initial parameters values giving by simple logistic regression
15865: * only one way, that is without matrix product. We will have nlstate maximizations */
15866: /* for(i=1;i<nlstate;i++){ */
15867: /* /\*reducing xi for 1 to npar to 1 to ncovmodel; *\/ */
15868: /* mlikeli(ficres,p, ncovmodel, ncovmodel, nlstate, ftol, funcnoprod); */
15869: /* } */
15870: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
15871: printf(" You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
15872: fprintf(ficlog," You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
15873: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
15874: fclose (ficparo);
15875: fclose (ficlog);
15876: goto end;
15877: exit(0);
15878: } else if(mle==-5) { /* Main Wizard */
15879: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
15880: printf(" You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
15881: fprintf(ficlog," You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
15882: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
15883: matcov=matrix(1,npar,1,npar);
15884: hess=matrix(1,npar,1,npar);
15885: } else{ /* Begin of mle != -1 or -5 */
15886: /* Read guessed parameters */
15887: /* Reads comments: lines beginning with '#' */
15888: while((c=getc(ficpar))=='#' && c!= EOF){
15889: ungetc(c,ficpar);
15890: fgets(line, MAXLINE, ficpar);
15891: numlinepar++;
15892: fputs(line,stdout);
15893: fputs(line,ficparo);
15894: fputs(line,ficlog);
15895: }
15896: ungetc(c,ficpar);
15897:
15898: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
15899: paramstart= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
15900: for(i=1; i <=nlstate; i++){
15901: j=0;
15902: for(jj=1; jj <=nlstate+ndeath; jj++){
15903: if(jj==i) continue;
15904: j++;
15905: while((c=getc(ficpar))=='#' && c!= EOF){
15906: ungetc(c,ficpar);
15907: fgets(line, MAXLINE, ficpar);
15908: numlinepar++;
15909: fputs(line,stdout);
15910: fputs(line,ficparo);
15911: fputs(line,ficlog);
15912: }
15913: ungetc(c,ficpar);
15914: fscanf(ficpar,"%1d%1d",&i1,&j1);
15915: if ((i1 != i) || (j1 != jj)){
15916: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n \
15917: It might be a problem of design; if ncovcol and the model are correct\n \
15918: run imach with mle=-1 to get a correct template of the parameter file.\n",numlinepar, i,j, i1, j1);
15919: exit(1);
15920: }
15921: fprintf(ficparo,"%1d%1d",i1,j1);
15922: if(mle==1)
15923: printf("%1d%1d",i,jj);
15924: fprintf(ficlog,"%1d%1d",i,jj);
15925: for(k=1; k<=ncovmodel;k++){
15926: fscanf(ficpar," %lf",¶m[i][j][k]);
15927: if(mle==1){
15928: printf(" %lf",param[i][j][k]);
15929: fprintf(ficlog," %lf",param[i][j][k]);
15930: }
15931: else
15932: fprintf(ficlog," %lf",param[i][j][k]);
15933: fprintf(ficparo," %lf",param[i][j][k]);
15934: }
15935: fscanf(ficpar,"\n");
15936: numlinepar++;
15937: if(mle==1)
15938: printf("\n");
15939: fprintf(ficlog,"\n");
15940: fprintf(ficparo,"\n");
15941: }
15942: }
15943: fflush(ficlog);
15944:
15945: /* Reads parameters values */
15946: p=param[1][1];
15947: pstart=paramstart[1][1];
15948:
15949: /* Reads comments: lines beginning with '#' */
15950: while((c=getc(ficpar))=='#' && c!= EOF){
15951: ungetc(c,ficpar);
15952: fgets(line, MAXLINE, ficpar);
15953: numlinepar++;
15954: fputs(line,stdout);
15955: fputs(line,ficparo);
15956: fputs(line,ficlog);
15957: }
15958: ungetc(c,ficpar);
15959:
15960: for(i=1; i <=nlstate; i++){
15961: for(j=1; j <=nlstate+ndeath-1; j++){
15962: fscanf(ficpar,"%1d%1d",&i1,&j1);
15963: if ( (i1-i) * (j1-j) != 0){
15964: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n",numlinepar, i,j, i1, j1);
15965: exit(1);
15966: }
15967: printf("%1d%1d",i,j);
15968: fprintf(ficparo,"%1d%1d",i1,j1);
15969: fprintf(ficlog,"%1d%1d",i1,j1);
15970: for(k=1; k<=ncovmodel;k++){
15971: fscanf(ficpar,"%le",&delti3[i][j][k]);
15972: printf(" %le",delti3[i][j][k]);
15973: fprintf(ficparo," %le",delti3[i][j][k]);
15974: fprintf(ficlog," %le",delti3[i][j][k]);
15975: }
15976: fscanf(ficpar,"\n");
15977: numlinepar++;
15978: printf("\n");
15979: fprintf(ficparo,"\n");
15980: fprintf(ficlog,"\n");
15981: }
15982: }
15983: fflush(ficlog);
15984:
15985: /* Reads covariance matrix */
15986: delti=delti3[1][1];
15987:
15988:
15989: /* free_ma3x(delti3,1,nlstate,1,nlstate+ndeath-1,1,ncovmodel); */ /* Hasn't to to freed here otherwise delti is no more allocated */
15990:
15991: /* Reads comments: lines beginning with '#' */
15992: while((c=getc(ficpar))=='#' && c!= EOF){
15993: ungetc(c,ficpar);
15994: fgets(line, MAXLINE, ficpar);
15995: numlinepar++;
15996: fputs(line,stdout);
15997: fputs(line,ficparo);
15998: fputs(line,ficlog);
15999: }
16000: ungetc(c,ficpar);
16001:
16002: matcov=matrix(1,npar,1,npar);
16003: hess=matrix(1,npar,1,npar);
16004: for(i=1; i <=npar; i++)
16005: for(j=1; j <=npar; j++) matcov[i][j]=0.;
16006:
16007: /* Scans npar lines */
16008: for(i=1; i <=npar; i++){
16009: count=fscanf(ficpar,"%1d%1d%d",&i1,&j1,&jk);
16010: if(count != 3){
16011: printf("Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
16012: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
16013: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
16014: fprintf(ficlog,"Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
16015: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
16016: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
16017: exit(1);
16018: }else{
16019: if(mle==1)
16020: printf("%1d%1d%d",i1,j1,jk);
16021: }
16022: fprintf(ficlog,"%1d%1d%d",i1,j1,jk);
16023: fprintf(ficparo,"%1d%1d%d",i1,j1,jk);
16024: for(j=1; j <=i; j++){
16025: fscanf(ficpar," %le",&matcov[i][j]);
16026: if(mle==1){
16027: printf(" %.5le",matcov[i][j]);
16028: }
16029: fprintf(ficlog," %.5le",matcov[i][j]);
16030: fprintf(ficparo," %.5le",matcov[i][j]);
16031: }
16032: fscanf(ficpar,"\n");
16033: numlinepar++;
16034: if(mle==1)
16035: printf("\n");
16036: fprintf(ficlog,"\n");
16037: fprintf(ficparo,"\n");
16038: }
16039: /* End of read covariance matrix npar lines */
16040: for(i=1; i <=npar; i++)
16041: for(j=i+1;j<=npar;j++)
16042: matcov[i][j]=matcov[j][i];
16043:
16044: if(mle==1)
16045: printf("\n");
16046: fprintf(ficlog,"\n");
16047:
16048: fflush(ficlog);
16049:
16050: } /* End of mle != -3 */
16051:
16052: /* Main data
16053: */
16054: nobs=lastobs-firstobs+1; /* was = lastobs;*/
16055: /* num=lvector(1,n); */
16056: /* moisnais=vector(1,n); */
16057: /* annais=vector(1,n); */
16058: /* moisdc=vector(1,n); */
16059: /* andc=vector(1,n); */
16060: /* weight=vector(1,n); */
16061: /* agedc=vector(1,n); */
16062: /* cod=ivector(1,n); */
16063: /* for(i=1;i<=n;i++){ */
16064: num=lvector(firstobs,lastobs);
16065: moisnais=vector(firstobs,lastobs);
16066: annais=vector(firstobs,lastobs);
16067: moisdc=vector(firstobs,lastobs);
16068: andc=vector(firstobs,lastobs);
16069: weight=vector(firstobs,lastobs);
16070: agedc=vector(firstobs,lastobs);
16071: cod=ivector(firstobs,lastobs);
16072: for(i=firstobs;i<=lastobs;i++){
16073: num[i]=0;
16074: moisnais[i]=0;
16075: annais[i]=0;
16076: moisdc[i]=0;
16077: andc[i]=0;
16078: agedc[i]=0;
16079: cod[i]=0;
16080: weight[i]=1.0; /* Equal weights, 1 by default */
16081: }
16082: mint=matrix(1,maxwav,firstobs,lastobs);
16083: anint=matrix(1,maxwav,firstobs,lastobs);
16084: s=imatrix(1,maxwav+1,firstobs,lastobs); /* s[i][j] health state for wave i and individual j */
16085: /* printf("BUG ncovmodel=%d NCOVMAX=%d 2**ncovmodel=%f BUG\n",ncovmodel,NCOVMAX,pow(2,ncovmodel)); */
16086: tab=ivector(1,NCOVMAX);
16087: ncodemax=ivector(1,NCOVMAX); /* Number of code per covariate; if O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */
16088: ncodemaxwundef=ivector(1,NCOVMAX); /* Number of code per covariate; if - 1 O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */
16089:
16090: /* Reads data from file datafile */
16091: if (readdata(datafile, firstobs, lastobs, &imx)==1)
16092: goto end;
16093:
16094: /* Calculation of the number of parameters from char model */
16095: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4
16096: k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tag[cptcovage=1]=4
16097: k=3 V4 Tvar[k=3]= 4 (from V4)
16098: k=2 V1 Tvar[k=2]= 1 (from V1)
16099: k=1 Tvar[1]=2 (from V2)
16100: */
16101:
16102: Tvar=ivector(1,NCOVMAX); /* Was 15 changed to NCOVMAX. */
16103: TvarsDind=ivector(1,NCOVMAX); /* */
16104: TnsdVar=ivector(1,NCOVMAX); /* */
16105: /* for(i=1; i<=NCOVMAX;i++) TnsdVar[i]=3; */
16106: TvarsD=ivector(1,NCOVMAX); /* */
16107: TvarsQind=ivector(1,NCOVMAX); /* */
16108: TvarsQ=ivector(1,NCOVMAX); /* */
16109: TvarF=ivector(1,NCOVMAX); /* */
16110: TvarFind=ivector(1,NCOVMAX); /* */
16111: TvarV=ivector(1,NCOVMAX); /* */
16112: TvarVind=ivector(1,NCOVMAX); /* */
16113: TvarA=ivector(1,NCOVMAX); /* */
16114: TvarAind=ivector(1,NCOVMAX); /* */
16115: TvarFD=ivector(1,NCOVMAX); /* */
16116: TvarFDind=ivector(1,NCOVMAX); /* */
16117: TvarFQ=ivector(1,NCOVMAX); /* */
16118: TvarFQind=ivector(1,NCOVMAX); /* */
16119: TvarVD=ivector(1,NCOVMAX); /* */
16120: TvarVDind=ivector(1,NCOVMAX); /* */
16121: TvarVQ=ivector(1,NCOVMAX); /* */
16122: TvarVQind=ivector(1,NCOVMAX); /* */
16123: TvarVV=ivector(1,NCOVMAX); /* */
16124: TvarVVind=ivector(1,NCOVMAX); /* */
16125: TvarVVA=ivector(1,NCOVMAX); /* */
16126: TvarVVAind=ivector(1,NCOVMAX); /* */
16127: TvarAVVA=ivector(1,NCOVMAX); /* */
16128: TvarAVVAind=ivector(1,NCOVMAX); /* */
16129:
16130: Tvalsel=vector(1,NCOVMAX); /* */
16131: Tvarsel=ivector(1,NCOVMAX); /* */
16132: Typevar=ivector(-1,NCOVMAX); /* -1 to 2 */
16133: Fixed=ivector(-1,NCOVMAX); /* -1 to 3 */
16134: Dummy=ivector(-1,NCOVMAX); /* -1 to 3 */
16135: DummyV=ivector(-1,NCOVMAX); /* 1 to 3 */
16136: FixedV=ivector(-1,NCOVMAX); /* 1 to 3 */
16137:
16138: /* V2+V1+V4+age*V3 is a model with 4 covariates (3 plus signs).
16139: For each model-covariate stores the data-covariate id. Tvar[1]=2, Tvar[2]=1, Tvar[3]=4,
16140: Tvar[4=age*V3] is 3 and 'age' is recorded in Tage.
16141: */
16142: /* For model-covariate k tells which data-covariate to use but
16143: because this model-covariate is a construction we invent a new column
16144: ncovcol + k1
16145: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3
16146: Tvar[3=V1*V4]=4+1 etc */
16147: Tprod=ivector(1,NCOVMAX); /* Gives the k position of the k1 product */
16148: Tposprod=ivector(1,NCOVMAX); /* Gives the k1 product from the k position */
16149: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3
16150: if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2)
16151: Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5]=2
16152: */
16153: Tvaraff=ivector(1,NCOVMAX); /* Unclear */
16154: 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
16155: * For V3*V2 (in V2+V1+V1*V4+age*V3+V3*V2), V3*V2 position is 2nd.
16156: * Tvard[k1=2][1]=3 (V3) Tvard[k1=2][2]=2(V2) */
16157: Tvardk=imatrix(-1,NCOVMAX,1,2);
16158: Tage=ivector(1,NCOVMAX); /* Gives the covariate id of covariates associated with age: V2 + V1 + age*V4 + V3*age
16159: 4 covariates (3 plus signs)
16160: Tage[1=V3*age]= 4; Tage[2=age*V4] = 3
16161: */
16162: for(i=1;i<NCOVMAX;i++)
16163: Tage[i]=0;
16164: Tmodelind=ivector(1,NCOVMAX);/** gives the k model position of an
16165: * individual dummy, fixed or varying:
16166: * Tmodelind[Tvaraff[3]]=9,Tvaraff[1]@9={4,
16167: * 3, 1, 0, 0, 0, 0, 0, 0},
16168: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 ,
16169: * V1 df, V2 qf, V3 & V4 dv, V5 qv
16170: * Tmodelind[1]@9={9,0,3,2,}*/
16171: TmodelInvind=ivector(1,NCOVMAX); /* TmodelInvind=Tvar[k]- ncovcol-nqv={5-2-1=2,*/
16172: TmodelInvQind=ivector(1,NCOVMAX);/** gives the k model position of an
16173: * individual quantitative, fixed or varying:
16174: * Tmodelqind[1]=1,Tvaraff[1]@9={4,
16175: * 3, 1, 0, 0, 0, 0, 0, 0},
16176: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1*/
16177:
16178: /* Probably useless zeroes */
16179: for(i=1;i<NCOVMAX;i++){
16180: DummyV[i]=0;
16181: FixedV[i]=0;
16182: }
16183:
16184: for(i=1; i <=ncovcol;i++){
16185: DummyV[i]=0;
16186: FixedV[i]=0;
16187: }
16188: for(i=ncovcol+1; i <=ncovcol+nqv;i++){
16189: DummyV[i]=1;
16190: FixedV[i]=0;
16191: }
16192: for(i=ncovcol+nqv+1; i <=ncovcol+nqv+ntv;i++){
16193: DummyV[i]=0;
16194: FixedV[i]=1;
16195: }
16196: for(i=ncovcol+nqv+ntv+1; i <=ncovcol+nqv+ntv+nqtv;i++){
16197: DummyV[i]=1;
16198: FixedV[i]=1;
16199: }
16200: for(i=1; i <=ncovcol+nqv+ntv+nqtv;i++){
16201: printf("Covariate type in the data: V%d, DummyV(V%d)=%d, FixedV(V%d)=%d\n",i,i,DummyV[i],i,FixedV[i]);
16202: fprintf(ficlog,"Covariate type in the data: V%d, DummyV(V%d)=%d, FixedV(V%d)=%d\n",i,i,DummyV[i],i,FixedV[i]);
16203: }
16204:
16205:
16206:
16207: /* Main decodemodel */
16208:
16209:
16210: if(decodemodel(model, lastobs) == 1) /* In order to get Tvar[k] V4+V3+V5 p Tvar[1]@3 = {4, 3, 5}*/
16211: goto end;
16212:
16213: if((double)(lastobs-imx)/(double)imx > 1.10){
16214: nbwarn++;
16215: 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);
16216: 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);
16217: }
16218: /* if(mle==1){*/
16219: if (weightopt != 1) { /* Maximisation without weights. We can have weights different from 1 but want no weight*/
16220: for(i=1;i<=imx;i++) weight[i]=1.0; /* changed to imx */
16221: }
16222:
16223: /*-calculation of age at interview from date of interview and age at death -*/
16224: agev=matrix(1,maxwav,1,imx);
16225:
16226: if(calandcheckages(imx, maxwav, &agemin, &agemax, &nberr, &nbwarn) == 1)
16227: goto end;
16228:
16229:
16230: agegomp=(int)agemin;
16231: free_vector(moisnais,firstobs,lastobs);
16232: free_vector(annais,firstobs,lastobs);
16233: /* free_matrix(mint,1,maxwav,1,n);
16234: free_matrix(anint,1,maxwav,1,n);*/
16235: /* free_vector(moisdc,1,n); */
16236: /* free_vector(andc,1,n); */
16237: /* */
16238:
16239: wav=ivector(1,imx);
16240: /* dh=imatrix(1,lastpass-firstpass+1,1,imx); */
16241: /* bh=imatrix(1,lastpass-firstpass+1,1,imx); */
16242: /* mw=imatrix(1,lastpass-firstpass+1,1,imx); */
16243: 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.*/
16244: bh=imatrix(1,lastpass-firstpass+2,1,imx);
16245: mw=imatrix(1,lastpass-firstpass+2,1,imx);
16246:
16247: /* Concatenates waves */
16248: /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
16249: Death is a valid wave (if date is known).
16250: mw[mi][i] is the number of (mi=1 to wav[i]) effective wave out of mi of individual i
16251: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
16252: and mw[mi+1][i]. dh depends on stepm.
16253: */
16254:
16255: concatwav(wav, dh, bh, mw, s, agedc, agev, firstpass, lastpass, imx, nlstate, stepm);
16256: /* Concatenates waves */
16257:
16258: free_vector(moisdc,firstobs,lastobs);
16259: free_vector(andc,firstobs,lastobs);
16260:
16261: /* Routine tricode is to calculate cptcoveff (real number of unique covariates) and to associate covariable number and modality */
16262: nbcode=imatrix(0,NCOVMAX,0,NCOVMAX);
16263: ncodemax[1]=1;
16264: Ndum =ivector(-1,NCOVMAX);
16265: cptcoveff=0;
16266: if (ncovmodel-nagesqr > 2 ){ /* That is if covariate other than cst, age and age*age */
16267: tricode(&cptcoveff,Tvar,nbcode,imx, Ndum); /**< Fills nbcode[Tvar[j]][l]; as well as calculate cptcoveff or number of total effective dummy covariates*/
16268: }
16269:
16270: ncovcombmax=pow(2,cptcoveff);
16271: invalidvarcomb=ivector(0, ncovcombmax);
16272: for(i=0;i<ncovcombmax;i++)
16273: invalidvarcomb[i]=0;
16274:
16275: /* Nbcode gives the value of the lth modality (currently 1 to 2) of jth covariate, in
16276: V2+V1*age, there are 3 covariates Tvar[2]=1 (V1).*/
16277: /* 1 to ncodemax[j] which is the maximum value of this jth covariate */
16278:
16279: /* codtab=imatrix(1,100,1,10);*/ /* codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) */
16280: /*printf(" codtab[1,1],codtab[100,10]=%d,%d\n", codtab[1][1],codtabm(100,10));*/
16281: /* codtab gives the value 1 or 2 of the hth combination of k covariates (1 or 2).*/
16282: /* nbcode[Tvaraff[j]][codtabm(h,j)]) : if there are only 2 modalities for a covariate j,
16283: * codtabm(h,j) gives its value classified at position h and nbcode gives how it is coded
16284: * (currently 0 or 1) in the data.
16285: * In a loop on h=1 to 2**k, and a loop on j (=1 to k), we get the value of
16286: * corresponding modality (h,j).
16287: */
16288:
16289: h=0;
16290: /*if (cptcovn > 0) */
16291: m=pow(2,cptcoveff);
16292:
16293: /**< codtab(h,k) k = codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) + 1
16294: * For k=4 covariates, h goes from 1 to m=2**k
16295: * codtabm(h,k)= (1 & (h-1) >> (k-1)) + 1;
16296: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
16297: * h\k 1 2 3 4 * h-1\k-1 4 3 2 1
16298: *______________________________ *______________________
16299: * 1 i=1 1 i=1 1 i=1 1 i=1 1 * 0 0 0 0 0
16300: * 2 2 1 1 1 * 1 0 0 0 1
16301: * 3 i=2 1 2 1 1 * 2 0 0 1 0
16302: * 4 2 2 1 1 * 3 0 0 1 1
16303: * 5 i=3 1 i=2 1 2 1 * 4 0 1 0 0
16304: * 6 2 1 2 1 * 5 0 1 0 1
16305: * 7 i=4 1 2 2 1 * 6 0 1 1 0
16306: * 8 2 2 2 1 * 7 0 1 1 1
16307: * 9 i=5 1 i=3 1 i=2 1 2 * 8 1 0 0 0
16308: * 10 2 1 1 2 * 9 1 0 0 1
16309: * 11 i=6 1 2 1 2 * 10 1 0 1 0
16310: * 12 2 2 1 2 * 11 1 0 1 1
16311: * 13 i=7 1 i=4 1 2 2 * 12 1 1 0 0
16312: * 14 2 1 2 2 * 13 1 1 0 1
16313: * 15 i=8 1 2 2 2 * 14 1 1 1 0
16314: * 16 2 2 2 2 * 15 1 1 1 1
16315: */
16316: /* How to do the opposite? From combination h (=1 to 2**k) how to get the value on the covariates? */
16317: /* from h=5 and m, we get then number of covariates k=log(m)/log(2)=4
16318: * and the value of each covariate?
16319: * V1=1, V2=1, V3=2, V4=1 ?
16320: * h-1=4 and 4 is 0100 or reverse 0010, and +1 is 1121 ok.
16321: * h=6, 6-1=5, 5 is 0101, 1010, 2121, V1=2nd, V2=1st, V3=2nd, V4=1st.
16322: * In order to get the real value in the data, we use nbcode
16323: * nbcode[Tvar[3][2nd]]=1 and nbcode[Tvar[4][1]]=0
16324: * We are keeping this crazy system in order to be able (in the future?)
16325: * to have more than 2 values (0 or 1) for a covariate.
16326: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
16327: * h=6, k=2? h-1=5=0101, reverse 1010, +1=2121, k=2nd position: value is 1: codtabm(6,2)=1
16328: * bbbbbbbb
16329: * 76543210
16330: * h-1 00000101 (6-1=5)
16331: *(h-1)>>(k-1)= 00000010 >> (2-1) = 1 right shift
16332: * &
16333: * 1 00000001 (1)
16334: * 00000000 = 1 & ((h-1) >> (k-1))
16335: * +1= 00000001 =1
16336: *
16337: * h=14, k=3 => h'=h-1=13, k'=k-1=2
16338: * h' 1101 =2^3+2^2+0x2^1+2^0
16339: * >>k' 11
16340: * & 00000001
16341: * = 00000001
16342: * +1 = 00000010=2 = codtabm(14,3)
16343: * Reverse h=6 and m=16?
16344: * cptcoveff=log(16)/log(2)=4 covariate: 6-1=5=0101 reversed=1010 +1=2121 =>V1=2, V2=1, V3=2, V4=1.
16345: * for (j=1 to cptcoveff) Vj=decodtabm(j,h,cptcoveff)
16346: * decodtabm(h,j,cptcoveff)= (((h-1) >> (j-1)) & 1) +1
16347: * decodtabm(h,j,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (j-1)) & 1) +1 : -1)
16348: * V3=decodtabm(14,3,2**4)=2
16349: * h'=13 1101 =2^3+2^2+0x2^1+2^0
16350: *(h-1) >> (j-1) 0011 =13 >> 2
16351: * &1 000000001
16352: * = 000000001
16353: * +1= 000000010 =2
16354: * 2211
16355: * V1=1+1, V2=0+1, V3=1+1, V4=1+1
16356: * V3=2
16357: * codtabm and decodtabm are identical
16358: */
16359:
16360:
16361: free_ivector(Ndum,-1,NCOVMAX);
16362:
16363:
16364:
16365: /* Initialisation of ----------- gnuplot -------------*/
16366: strcpy(optionfilegnuplot,optionfilefiname);
16367: if(mle==-3)
16368: strcat(optionfilegnuplot,"-MORT_");
16369: strcat(optionfilegnuplot,".gp");
16370:
16371: if((ficgp=fopen(optionfilegnuplot,"w"))==NULL) {
16372: printf("Problem with file %s",optionfilegnuplot);
16373: }
16374: else{
16375: fprintf(ficgp,"\n# IMaCh-%s\n", version);
16376: fprintf(ficgp,"# %s\n", optionfilegnuplot);
16377: //fprintf(ficgp,"set missing 'NaNq'\n");
16378: fprintf(ficgp,"set datafile missing 'NaNq'\n");
16379: }
16380: /* fclose(ficgp);*/
16381:
16382:
16383: /* Initialisation of --------- index.htm --------*/
16384:
16385: strcpy(optionfilehtm,optionfilefiname); /* Main html file */
16386: if(mle==-3)
16387: strcat(optionfilehtm,"-MORT_");
16388: strcat(optionfilehtm,".htm");
16389: if((fichtm=fopen(optionfilehtm,"w"))==NULL) {
16390: printf("Problem with %s \n",optionfilehtm);
16391: exit(0);
16392: }
16393:
16394: strcpy(optionfilehtmcov,optionfilefiname); /* Only for matrix of covariance */
16395: strcat(optionfilehtmcov,"-cov.htm");
16396: if((fichtmcov=fopen(optionfilehtmcov,"w"))==NULL) {
16397: printf("Problem with %s \n",optionfilehtmcov), exit(0);
16398: }
16399: else{
16400: fprintf(fichtmcov,"<html><head>\n<title>IMaCh Cov %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
16401: <hr size=\"2\" color=\"#EC5E5E\"> \n\
16402: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
16403: optionfilehtmcov,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
16404: }
16405:
16406: fprintf(fichtm,"<html><head>\n<meta charset=\"utf-8\"/><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n\
16407: <title>IMaCh %s</title></head>\n\
16408: <body><font size=\"7\"><a href=http:/euroreves.ined.fr/imach>IMaCh for Interpolated Markov Chain</a> </font><br>\n\
16409: <font size=\"3\">Sponsored by Copyright (C) 2002-2015 <a href=http://www.ined.fr>INED</a>\
16410: -EUROREVES-Institut de longévité-2013-2022-Japan Society for the Promotion of Sciences 日本学術振興会 \
16411: (<a href=https://www.jsps.go.jp/english/e-grants/>Grant-in-Aid for Scientific Research 25293121</a>) - \
16412: <a href=https://software.intel.com/en-us>Intel Software 2015-2018</a></font><br> \n", optionfilehtm);
16413:
16414: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\"> \n\
16415: <font size=\"2\">IMaCh-%s <br> %s</font> \
16416: <hr size=\"2\" color=\"#EC5E5E\"> \n\
16417: 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\
16418: \n\
16419: <hr size=\"2\" color=\"#EC5E5E\">\
16420: <ul><li><h4>Parameter files</h4>\n\
16421: - Parameter file: <a href=\"%s.%s\">%s.%s</a><br>\n\
16422: - Copy of the parameter file: <a href=\"o%s\">o%s</a><br>\n\
16423: - Log file of the run: <a href=\"%s\">%s</a><br>\n\
16424: - Gnuplot file name: <a href=\"%s\">%s</a><br>\n\
16425: - Date and time at start: %s</ul>\n",\
16426: version,fullversion,optionfilehtm,optionfilehtm,title,datafile,datafile,firstpass,lastpass,stepm, weightopt, model, \
16427: optionfilefiname,optionfilext,optionfilefiname,optionfilext,\
16428: fileres,fileres,\
16429: filelog,filelog,optionfilegnuplot,optionfilegnuplot,strstart);
16430: fflush(fichtm);
16431:
16432: strcpy(pathr,path);
16433: strcat(pathr,optionfilefiname);
16434: #ifdef WIN32
16435: _chdir(optionfilefiname); /* Move to directory named optionfile */
16436: #else
16437: chdir(optionfilefiname); /* Move to directory named optionfile */
16438: #endif
16439:
16440:
16441: /* Calculates basic frequencies. Computes observed prevalence at single age
16442: and for any valid combination of covariates
16443: and prints on file fileres'p'. */
16444: freqsummary(fileres, p, pstart, agemin, agemax, s, agev, nlstate, imx, Tvaraff, invalidvarcomb, nbcode, ncodemax,mint,anint,strstart, \
16445: firstpass, lastpass, stepm, weightopt, model);
16446:
16447: fprintf(fichtm,"\n");
16448: 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",\
16449: ftol, stepm);
16450: fprintf(fichtm,"\n<li>Number of fixed dummy covariates: ncovcol=%d ", ncovcol);
16451: ncurrv=1;
16452: for(i=ncurrv; i <=ncovcol; i++) fprintf(fichtm,"V%d ", i);
16453: fprintf(fichtm,"\n<li> Number of fixed quantitative variables: nqv=%d ", nqv);
16454: ncurrv=i;
16455: for(i=ncurrv; i <=ncurrv-1+nqv; i++) fprintf(fichtm,"V%d ", i);
16456: fprintf(fichtm,"\n<li> Number of time varying (wave varying) dummy covariates: ntv=%d ", ntv);
16457: ncurrv=i;
16458: for(i=ncurrv; i <=ncurrv-1+ntv; i++) fprintf(fichtm,"V%d ", i);
16459: fprintf(fichtm,"\n<li>Number of time varying quantitative covariates: nqtv=%d ", nqtv);
16460: ncurrv=i;
16461: for(i=ncurrv; i <=ncurrv-1+nqtv; i++) fprintf(fichtm,"V%d ", i);
16462: 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", \
16463: nlstate, ndeath, maxwav, mle, weightopt);
16464:
16465: fprintf(fichtm,"<h4> Diagram of states <a href=\"%s_.svg\">%s_.svg</a></h4> \n\
16466: <img src=\"%s_.svg\">", subdirf2(optionfilefiname,"D_"),subdirf2(optionfilefiname,"D_"),subdirf2(optionfilefiname,"D_"));
16467:
16468:
16469: fprintf(fichtm,"\n<h4>Some descriptive statistics </h4>\n<br>Number of (used) observations=%d <br>\n\
16470: Youngest age at first (selected) pass %.2f, oldest age %.2f<br>\n\
16471: Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n",\
16472: imx,agemin,agemax,jmin,jmax,jmean);
16473: pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
16474: oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
16475: newms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
16476: savms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
16477: oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */
16478:
16479: /* For Powell, parameters are in a vector p[] starting at p[1]
16480: so we point p on param[1][1] so that p[1] maps on param[1][1][1] */
16481: p=param[1][1]; /* *(*(*(param +1)+1)+0) */
16482:
16483: globpr=0; /* To get the number ipmx of contributions and the sum of weights*/
16484: /* For mortality only */
16485: if (mle==-3){
16486: ximort=matrix(1,NDIM,1,NDIM);
16487: for(i=1;i<=NDIM;i++)
16488: for(j=1;j<=NDIM;j++)
16489: ximort[i][j]=0.;
16490: /* ximort=gsl_matrix_alloc(1,NDIM,1,NDIM); */
16491: cens=ivector(firstobs,lastobs);
16492: ageexmed=vector(firstobs,lastobs);
16493: agecens=vector(firstobs,lastobs);
16494: dcwave=ivector(firstobs,lastobs);
16495:
16496: for (i=1; i<=imx; i++){
16497: dcwave[i]=-1;
16498: for (m=firstpass; m<=lastpass; m++)
16499: if (s[m][i]>nlstate) {
16500: dcwave[i]=m;
16501: /* printf("i=%d j=%d s=%d dcwave=%d\n",i,j, s[j][i],dcwave[i]);*/
16502: break;
16503: }
16504: }
16505:
16506: for (i=1; i<=imx; i++) {
16507: if (wav[i]>0){
16508: ageexmed[i]=agev[mw[1][i]][i];
16509: j=wav[i];
16510: agecens[i]=1.;
16511:
16512: if (ageexmed[i]> 1 && wav[i] > 0){
16513: agecens[i]=agev[mw[j][i]][i];
16514: cens[i]= 1;
16515: }else if (ageexmed[i]< 1)
16516: cens[i]= -1;
16517: if (agedc[i]< AGESUP && agedc[i]>1 && dcwave[i]>firstpass && dcwave[i]<=lastpass)
16518: cens[i]=0 ;
16519: }
16520: else cens[i]=-1;
16521: }
16522:
16523: for (i=1;i<=NDIM;i++) {
16524: for (j=1;j<=NDIM;j++)
16525: ximort[i][j]=(i == j ? 1.0 : 0.0);
16526: }
16527:
16528: p[1]=0.0268; p[NDIM]=0.083;
16529: /* printf("%lf %lf", p[1], p[2]); */
16530:
16531:
16532: #ifdef GSL
16533: printf("GSL optimization\n"); fprintf(ficlog,"Powell\n");
16534: #else
16535: printf("Powell\n"); fprintf(ficlog,"Powell\n");
16536: #endif
16537: strcpy(filerespow,"POW-MORT_");
16538: strcat(filerespow,fileresu);
16539: if((ficrespow=fopen(filerespow,"w"))==NULL) {
16540: printf("Problem with resultfile: %s\n", filerespow);
16541: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
16542: }
16543: #ifdef GSL
16544: fprintf(ficrespow,"# GSL optimization\n# iter -2*LL");
16545: #else
16546: fprintf(ficrespow,"# Powell\n# iter -2*LL");
16547: #endif
16548: /* for (i=1;i<=nlstate;i++)
16549: for(j=1;j<=nlstate+ndeath;j++)
16550: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
16551: */
16552: fprintf(ficrespow,"\n");
16553: #ifdef GSL
16554: /* gsl starts here */
16555: T = gsl_multimin_fminimizer_nmsimplex;
16556: gsl_multimin_fminimizer *sfm = NULL;
16557: gsl_vector *ss, *x;
16558: gsl_multimin_function minex_func;
16559:
16560: /* Initial vertex size vector */
16561: ss = gsl_vector_alloc (NDIM);
16562:
16563: if (ss == NULL){
16564: GSL_ERROR_VAL ("failed to allocate space for ss", GSL_ENOMEM, 0);
16565: }
16566: /* Set all step sizes to 1 */
16567: gsl_vector_set_all (ss, 0.001);
16568:
16569: /* Starting point */
16570:
16571: x = gsl_vector_alloc (NDIM);
16572:
16573: if (x == NULL){
16574: gsl_vector_free(ss);
16575: GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0);
16576: }
16577:
16578: /* Initialize method and iterate */
16579: /* p[1]=0.0268; p[NDIM]=0.083; */
16580: /* gsl_vector_set(x, 0, 0.0268); */
16581: /* gsl_vector_set(x, 1, 0.083); */
16582: gsl_vector_set(x, 0, p[1]);
16583: gsl_vector_set(x, 1, p[2]);
16584:
16585: minex_func.f = &gompertz_f;
16586: minex_func.n = NDIM;
16587: minex_func.params = (void *)&p; /* ??? */
16588:
16589: sfm = gsl_multimin_fminimizer_alloc (T, NDIM);
16590: gsl_multimin_fminimizer_set (sfm, &minex_func, x, ss);
16591:
16592: printf("Iterations beginning .....\n\n");
16593: printf("Iter. # Intercept Slope -Log Likelihood Simplex size\n");
16594:
16595: iteri=0;
16596: while (rval == GSL_CONTINUE){
16597: iteri++;
16598: status = gsl_multimin_fminimizer_iterate(sfm);
16599:
16600: if (status) printf("error: %s\n", gsl_strerror (status));
16601: fflush(0);
16602:
16603: if (status)
16604: break;
16605:
16606: rval = gsl_multimin_test_size (gsl_multimin_fminimizer_size (sfm), 1e-6);
16607: ssval = gsl_multimin_fminimizer_size (sfm);
16608:
16609: if (rval == GSL_SUCCESS)
16610: printf ("converged to a local maximum at\n");
16611:
16612: printf("%5d ", iteri);
16613: for (it = 0; it < NDIM; it++){
16614: printf ("%10.5f ", gsl_vector_get (sfm->x, it));
16615: }
16616: printf("f() = %-10.5f ssize = %.7f\n", sfm->fval, ssval);
16617: }
16618:
16619: printf("\n\n Please note: Program should be run many times with varying starting points to detemine global maximum\n\n");
16620:
16621: gsl_vector_free(x); /* initial values */
16622: gsl_vector_free(ss); /* inital step size */
16623: for (it=0; it<NDIM; it++){
16624: p[it+1]=gsl_vector_get(sfm->x,it);
16625: fprintf(ficrespow," %.12lf", p[it]);
16626: }
16627: gsl_multimin_fminimizer_free (sfm); /* p *(sfm.x.data) et p *(sfm.x.data+1) */
16628: #endif
16629: #ifdef POWELL
16630: powell(p,ximort,NDIM,ftol,&iter,&fret,gompertz);
16631: #endif
16632: fclose(ficrespow);
16633:
16634: hesscov(matcov, hess, p, NDIM, delti, 1e-4, gompertz);
16635:
16636: for(i=1; i <=NDIM; i++)
16637: for(j=i+1;j<=NDIM;j++)
16638: matcov[i][j]=matcov[j][i];
16639:
16640: printf("\nCovariance matrix\n ");
16641: fprintf(ficlog,"\nCovariance matrix\n ");
16642: for(i=1; i <=NDIM; i++) {
16643: for(j=1;j<=NDIM;j++){
16644: printf("%f ",matcov[i][j]);
16645: fprintf(ficlog,"%f ",matcov[i][j]);
16646: }
16647: printf("\n "); fprintf(ficlog,"\n ");
16648: }
16649:
16650: printf("iter=%d MLE=%f Eq=%lf*exp(%lf*(age-%d))\n",iter,-gompertz(p),p[1],p[2],agegomp);
16651: for (i=1;i<=NDIM;i++) {
16652: printf("%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
16653: fprintf(ficlog,"%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
16654: }
16655: lsurv=vector(agegomp,AGESUP);
16656: lpop=vector(agegomp,AGESUP);
16657: tpop=vector(agegomp,AGESUP);
16658: lsurv[agegomp]=100000;
16659:
16660: for (k=agegomp;k<=AGESUP;k++) {
16661: agemortsup=k;
16662: if (p[1]*exp(p[2]*(k-agegomp))>1) break;
16663: }
16664:
16665: for (k=agegomp;k<agemortsup;k++)
16666: lsurv[k+1]=lsurv[k]-lsurv[k]*(p[1]*exp(p[2]*(k-agegomp)));
16667:
16668: for (k=agegomp;k<agemortsup;k++){
16669: lpop[k]=(lsurv[k]+lsurv[k+1])/2.;
16670: sumlpop=sumlpop+lpop[k];
16671: }
16672:
16673: tpop[agegomp]=sumlpop;
16674: for (k=agegomp;k<(agemortsup-3);k++){
16675: /* tpop[k+1]=2;*/
16676: tpop[k+1]=tpop[k]-lpop[k];
16677: }
16678:
16679:
16680: printf("\nAge lx qx dx Lx Tx e(x)\n");
16681: for (k=agegomp;k<(agemortsup-2);k++)
16682: 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]);
16683:
16684:
16685: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
16686: ageminpar=50;
16687: agemaxpar=100;
16688: if(ageminpar == AGEOVERFLOW ||agemaxpar == AGEOVERFLOW){
16689: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
16690: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
16691: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
16692: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
16693: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
16694: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
16695: }else{
16696: printf("Warning! ageminpar %f and agemaxpar %f have been fixed because for simplification until it is fixed...\n\n",ageminpar,agemaxpar);
16697: fprintf(ficlog,"Warning! ageminpar %f and agemaxpar %f have been fixed because for simplification until it is fixed...\n\n",ageminpar,agemaxpar);
16698: printinggnuplotmort(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, pathc,p);
16699: }
16700: printinghtmlmort(fileresu,title,datafile, firstpass, lastpass, \
16701: stepm, weightopt,\
16702: model,imx,p,matcov,agemortsup);
16703:
16704: free_vector(lsurv,agegomp,AGESUP);
16705: free_vector(lpop,agegomp,AGESUP);
16706: free_vector(tpop,agegomp,AGESUP);
16707: free_matrix(ximort,1,NDIM,1,NDIM);
16708: free_ivector(dcwave,firstobs,lastobs);
16709: free_vector(agecens,firstobs,lastobs);
16710: free_vector(ageexmed,firstobs,lastobs);
16711: free_ivector(cens,firstobs,lastobs);
16712: #ifdef GSL
16713: #endif
16714: } /* Endof if mle==-3 mortality only */
16715: /* Standard */
16716: else{ /* For mle !=- 3, could be 0 or 1 or 4 etc. */
16717: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
16718: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
16719: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
16720: printf("First Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
16721: for (k=1; k<=npar;k++)
16722: printf(" %d %8.5f",k,p[k]);
16723: printf("\n");
16724: if(mle>=1){ /* Could be 1 or 2, Real Maximization */
16725: /* mlikeli uses func not funcone */
16726: /* for(i=1;i<nlstate;i++){ */
16727: /* /\*reducing xi for 1 to npar to 1 to ncovmodel; *\/ */
16728: /* mlikeli(ficres,p, ncovmodel, ncovmodel, nlstate, ftol, funcnoprod); */
16729: /* } */
16730: mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func);
16731: }
16732: if(mle==0) {/* No optimization, will print the likelihoods for the datafile */
16733: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
16734: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
16735: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
16736: }
16737: globpr=1; /* again, to print the individual contributions using computed gpimx and gsw */
16738: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
16739: printf("Second Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
16740: /* exit(0); */
16741: for (k=1; k<=npar;k++)
16742: printf(" %d %8.5f",k,p[k]);
16743: printf("\n");
16744:
16745: /*--------- results files --------------*/
16746: /* 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); */
16747:
16748:
16749: fprintf(ficres,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
16750: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n"); /* Printing model equation */
16751: fprintf(ficlog,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
16752:
16753: printf("#model= 1 + age ");
16754: fprintf(ficres,"#model= 1 + age ");
16755: fprintf(ficlog,"#model= 1 + age ");
16756: fprintf(fichtm,"\n<ul><li> model=1+age+%s\n \
16757: </ul>", model);
16758:
16759: fprintf(fichtm,"\n<table style=\"text-align:center; border: 1px solid\">\n");
16760: fprintf(fichtm, "<tr><th>Model=</th><th>1</th><th>+ age</th>");
16761: if(nagesqr==1){
16762: printf(" + age*age ");
16763: fprintf(ficres," + age*age ");
16764: fprintf(ficlog," + age*age ");
16765: fprintf(fichtm, "<th>+ age*age</th>");
16766: }
16767: for(j=1;j <=ncovmodel-2;j++){
16768: if(Typevar[j]==0) {
16769: printf(" + V%d ",Tvar[j]);
16770: fprintf(ficres," + V%d ",Tvar[j]);
16771: fprintf(ficlog," + V%d ",Tvar[j]);
16772: fprintf(fichtm, "<th>+ V%d</th>",Tvar[j]);
16773: }else if(Typevar[j]==1) {
16774: printf(" + V%d*age ",Tvar[j]);
16775: fprintf(ficres," + V%d*age ",Tvar[j]);
16776: fprintf(ficlog," + V%d*age ",Tvar[j]);
16777: fprintf(fichtm, "<th>+ V%d*age</th>",Tvar[j]);
16778: }else if(Typevar[j]==2) {
16779: printf(" + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
16780: fprintf(ficres," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
16781: fprintf(ficlog," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
16782: fprintf(fichtm, "<th>+ V%d*V%d</th>",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
16783: }else if(Typevar[j]==3) { /* TO VERIFY */
16784: printf(" + V%d*V%d*age ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
16785: fprintf(ficres," + V%d*V%d*age ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
16786: fprintf(ficlog," + V%d*V%d*age ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
16787: fprintf(fichtm, "<th>+ V%d*V%d*age</th>",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
16788: }
16789: }
16790: printf("\n");
16791: fprintf(ficres,"\n");
16792: fprintf(ficlog,"\n");
16793: fprintf(fichtm, "</tr>");
16794: fprintf(fichtm, "\n");
16795:
16796:
16797: for(i=1,jk=1; i <=nlstate; i++){
16798: for(k=1; k <=(nlstate+ndeath); k++){
16799: if (k != i) {
16800: fprintf(fichtm, "<tr>");
16801: printf("%d%d ",i,k);
16802: fprintf(ficlog,"%d%d ",i,k);
16803: fprintf(ficres,"%1d%1d ",i,k);
16804: fprintf(fichtm, "<td>%1d%1d</td>",i,k);
16805: for(j=1; j <=ncovmodel; j++){
16806: printf("%12.7f ",p[jk]);
16807: fprintf(ficlog,"%12.7f ",p[jk]);
16808: fprintf(ficres,"%12.7f ",p[jk]);
16809: fprintf(fichtm, "<td>%12.7f</td>",p[jk]);
16810: jk++;
16811: }
16812: printf("\n");
16813: fprintf(ficlog,"\n");
16814: fprintf(ficres,"\n");
16815: fprintf(fichtm, "</tr>\n");
16816: }
16817: }
16818: }
16819: /* fprintf(fichtm,"</tr>\n"); */
16820: fprintf(fichtm,"</table>\n");
16821: fprintf(fichtm, "\n");
16822:
16823: if(mle != 0){
16824: /* Computing hessian and covariance matrix only at a peak of the Likelihood, that is after optimization */
16825: ftolhess=ftol; /* Usually correct */
16826: hesscov(matcov, hess, p, npar, delti, ftolhess, func);
16827: 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");
16828: 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");
16829: 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);
16830: fprintf(fichtm,"\n<table style=\"text-align:center; border: 1px solid\">");
16831: fprintf(fichtm, "\n<tr><th>Model=</th><th>1</th><th>+ age</th>");
16832: if(nagesqr==1){
16833: printf(" + age*age ");
16834: fprintf(ficres," + age*age ");
16835: fprintf(ficlog," + age*age ");
16836: fprintf(fichtm, "<th>+ age*age</th>");
16837: }
16838: for(j=1;j <=ncovmodel-2;j++){
16839: if(Typevar[j]==0) {
16840: printf(" + V%d ",Tvar[j]);
16841: fprintf(fichtm, "<th>+ V%d</th>",Tvar[j]);
16842: }else if(Typevar[j]==1) {
16843: printf(" + V%d*age ",Tvar[j]);
16844: fprintf(fichtm, "<th>+ V%d*age</th>",Tvar[j]);
16845: }else if(Typevar[j]==2) {
16846: fprintf(fichtm, "<th>+ V%d*V%d</th>",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
16847: }else if(Typevar[j]==3) { /* TO VERIFY */
16848: fprintf(fichtm, "<th>+ V%d*V%d*age</th>",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
16849: }
16850: }
16851: fprintf(fichtm, "</tr>\n");
16852:
16853: for(i=1,jk=1; i <=nlstate; i++){
16854: for(k=1; k <=(nlstate+ndeath); k++){
16855: if (k != i) {
16856: fprintf(fichtm, "<tr valign=top>");
16857: printf("%d%d ",i,k);
16858: fprintf(ficlog,"%d%d ",i,k);
16859: fprintf(fichtm, "<td>%1d%1d</td>",i,k);
16860: for(j=1; j <=ncovmodel; j++){
16861: wald=p[jk]/sqrt(matcov[jk][jk]);
16862: 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]));
16863: 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]));
16864: if(fabs(wald) > 1.96){
16865: fprintf(fichtm, "<td><b>%12.7f</b></br> (%12.7f)</br>",p[jk],sqrt(matcov[jk][jk]));
16866: }else{
16867: fprintf(fichtm, "<td>%12.7f (%12.7f)</br>",p[jk],sqrt(matcov[jk][jk]));
16868: }
16869: fprintf(fichtm,"W=%8.3f</br>",wald);
16870: fprintf(fichtm,"[%12.7f;%12.7f]</br></td>", p[jk]-1.96*sqrt(matcov[jk][jk]),p[jk]+1.96*sqrt(matcov[jk][jk]));
16871: jk++;
16872: }
16873: printf("\n");
16874: fprintf(ficlog,"\n");
16875: fprintf(fichtm, "</tr>\n");
16876: }
16877: }
16878: }
16879: } /* end of hesscov and Wald tests */
16880: fprintf(fichtm,"</table>\n");
16881:
16882: /* */
16883: fprintf(ficres,"# Scales (for hessian or gradient estimation)\n");
16884: printf("# Scales (for hessian or gradient estimation)\n");
16885: fprintf(ficlog,"# Scales (for hessian or gradient estimation)\n");
16886: for(i=1,jk=1; i <=nlstate; i++){
16887: for(j=1; j <=nlstate+ndeath; j++){
16888: if (j!=i) {
16889: fprintf(ficres,"%1d%1d",i,j);
16890: printf("%1d%1d",i,j);
16891: fprintf(ficlog,"%1d%1d",i,j);
16892: for(k=1; k<=ncovmodel;k++){
16893: printf(" %.5e",delti[jk]);
16894: fprintf(ficlog," %.5e",delti[jk]);
16895: fprintf(ficres," %.5e",delti[jk]);
16896: jk++;
16897: }
16898: printf("\n");
16899: fprintf(ficlog,"\n");
16900: fprintf(ficres,"\n");
16901: }
16902: }
16903: }
16904:
16905: 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");
16906: if(mle >= 1) /* Too big for the screen */
16907: 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");
16908: 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");
16909: /* # 121 Var(a12)\n\ */
16910: /* # 122 Cov(b12,a12) Var(b12)\n\ */
16911: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
16912: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
16913: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
16914: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
16915: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
16916: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
16917:
16918:
16919: /* Just to have a covariance matrix which will be more understandable
16920: even is we still don't want to manage dictionary of variables
16921: */
16922: for(itimes=1;itimes<=2;itimes++){
16923: jj=0;
16924: for(i=1; i <=nlstate; i++){
16925: for(j=1; j <=nlstate+ndeath; j++){
16926: if(j==i) continue;
16927: for(k=1; k<=ncovmodel;k++){
16928: jj++;
16929: ca[0]= k+'a'-1;ca[1]='\0';
16930: if(itimes==1){
16931: if(mle>=1)
16932: printf("#%1d%1d%d",i,j,k);
16933: fprintf(ficlog,"#%1d%1d%d",i,j,k);
16934: fprintf(ficres,"#%1d%1d%d",i,j,k);
16935: }else{
16936: if(mle>=1)
16937: printf("%1d%1d%d",i,j,k);
16938: fprintf(ficlog,"%1d%1d%d",i,j,k);
16939: fprintf(ficres,"%1d%1d%d",i,j,k);
16940: }
16941: ll=0;
16942: for(li=1;li <=nlstate; li++){
16943: for(lj=1;lj <=nlstate+ndeath; lj++){
16944: if(lj==li) continue;
16945: for(lk=1;lk<=ncovmodel;lk++){
16946: ll++;
16947: if(ll<=jj){
16948: cb[0]= lk +'a'-1;cb[1]='\0';
16949: if(ll<jj){
16950: if(itimes==1){
16951: if(mle>=1)
16952: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
16953: fprintf(ficlog," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
16954: fprintf(ficres," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
16955: }else{
16956: if(mle>=1)
16957: printf(" %.5e",matcov[jj][ll]);
16958: fprintf(ficlog," %.5e",matcov[jj][ll]);
16959: fprintf(ficres," %.5e",matcov[jj][ll]);
16960: }
16961: }else{
16962: if(itimes==1){
16963: if(mle>=1)
16964: printf(" Var(%s%1d%1d)",ca,i,j);
16965: fprintf(ficlog," Var(%s%1d%1d)",ca,i,j);
16966: fprintf(ficres," Var(%s%1d%1d)",ca,i,j);
16967: }else{
16968: if(mle>=1)
16969: printf(" %.7e",matcov[jj][ll]);
16970: fprintf(ficlog," %.7e",matcov[jj][ll]);
16971: fprintf(ficres," %.7e",matcov[jj][ll]);
16972: }
16973: }
16974: }
16975: } /* end lk */
16976: } /* end lj */
16977: } /* end li */
16978: if(mle>=1)
16979: printf("\n");
16980: fprintf(ficlog,"\n");
16981: fprintf(ficres,"\n");
16982: numlinepar++;
16983: } /* end k*/
16984: } /*end j */
16985: } /* end i */
16986: } /* end itimes */
16987:
16988: fflush(ficlog);
16989: fflush(ficres);
16990: while(fgets(line, MAXLINE, ficpar)) {
16991: /* If line starts with a # it is a comment */
16992: if (line[0] == '#') {
16993: numlinepar++;
16994: fputs(line,stdout);
16995: fputs(line,ficparo);
16996: fputs(line,ficlog);
16997: fputs(line,ficres);
16998: continue;
16999: }else
17000: break;
17001: }
17002:
17003: /* while((c=getc(ficpar))=='#' && c!= EOF){ */
17004: /* ungetc(c,ficpar); */
17005: /* fgets(line, MAXLINE, ficpar); */
17006: /* fputs(line,stdout); */
17007: /* fputs(line,ficparo); */
17008: /* } */
17009: /* ungetc(c,ficpar); */
17010:
17011: estepm=0;
17012: 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){
17013:
17014: if (num_filled != 6) {
17015: 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);
17016: 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);
17017: goto end;
17018: }
17019: printf("agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%lf\n",ageminpar,agemaxpar, bage, fage, estepm, ftolpl);
17020: }
17021: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
17022: /*ftolpl=6.e-4;*/ /* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
17023:
17024: /* fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%\n",&ageminpar,&agemaxpar, &bage, &fage, &estepm); */
17025: if (estepm==0 || estepm < stepm) estepm=stepm;
17026: if (fage <= 2) {
17027: bage = ageminpar;
17028: fage = agemaxpar;
17029: }
17030:
17031: fprintf(ficres,"# agemin agemax for life expectancy, bage fage (if mle==0 ie no data nor Max likelihood).\n");
17032: fprintf(ficres,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
17033: fprintf(ficparo,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d, ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
17034:
17035: /* Other stuffs, more or less useful */
17036: while(fgets(line, MAXLINE, ficpar)) {
17037: /* If line starts with a # it is a comment */
17038: if (line[0] == '#') {
17039: numlinepar++;
17040: fputs(line,stdout);
17041: fputs(line,ficparo);
17042: fputs(line,ficlog);
17043: fputs(line,ficres);
17044: continue;
17045: }else
17046: break;
17047: }
17048:
17049: 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){
17050:
17051: if (num_filled != 7) {
17052: 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);
17053: 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);
17054: goto end;
17055: }
17056: printf("begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mov_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
17057: 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);
17058: 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);
17059: 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);
17060: }
17061:
17062: while(fgets(line, MAXLINE, ficpar)) {
17063: /* If line starts with a # it is a comment */
17064: if (line[0] == '#') {
17065: numlinepar++;
17066: fputs(line,stdout);
17067: fputs(line,ficparo);
17068: fputs(line,ficlog);
17069: fputs(line,ficres);
17070: continue;
17071: }else
17072: break;
17073: }
17074:
17075:
17076: dateprev1=anprev1+(mprev1-1)/12.+(jprev1-1)/365.;
17077: dateprev2=anprev2+(mprev2-1)/12.+(jprev2-1)/365.;
17078:
17079: if((num_filled=sscanf(line,"pop_based=%d\n",&popbased)) !=EOF){
17080: if (num_filled != 1) {
17081: 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);
17082: 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);
17083: goto end;
17084: }
17085: printf("pop_based=%d\n",popbased);
17086: fprintf(ficlog,"pop_based=%d\n",popbased);
17087: fprintf(ficparo,"pop_based=%d\n",popbased);
17088: fprintf(ficres,"pop_based=%d\n",popbased);
17089: }
17090:
17091: /* Results */
17092: /* Value of covariate in each resultine will be compututed (if product) and sorted according to model rank */
17093: /* It is precov[] because we need the varying age in order to compute the real cov[] of the model equation */
17094: precov=matrix(1,MAXRESULTLINESPONE,1,NCOVMAX+1);
17095: endishere=0;
17096: nresult=0;
17097: parameterline=0;
17098: do{
17099: if(!fgets(line, MAXLINE, ficpar)){
17100: endishere=1;
17101: parameterline=15;
17102: }else if (line[0] == '#') {
17103: /* If line starts with a # it is a comment */
17104: numlinepar++;
17105: fputs(line,stdout);
17106: fputs(line,ficparo);
17107: fputs(line,ficlog);
17108: fputs(line,ficres);
17109: continue;
17110: }else if(sscanf(line,"prevforecast=%[^\n]\n",modeltemp))
17111: parameterline=11;
17112: else if(sscanf(line,"prevbackcast=%[^\n]\n",modeltemp))
17113: parameterline=12;
17114: else if(sscanf(line,"result:%[^\n]\n",modeltemp)){
17115: parameterline=13;
17116: }
17117: else{
17118: parameterline=14;
17119: }
17120: switch (parameterline){ /* =0 only if only comments */
17121: case 11:
17122: 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)){
17123: 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);
17124: 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);
17125: 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);
17126: 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);
17127: /* day and month of proj2 are not used but only year anproj2.*/
17128: dateproj1=anproj1+(mproj1-1)/12.+(jproj1-1)/365.;
17129: dateproj2=anproj2+(mproj2-1)/12.+(jproj2-1)/365.;
17130: prvforecast = 1;
17131: }
17132: else if((num_filled=sscanf(line,"prevforecast=%d yearsfproj=%lf mobil_average=%d\n",&prevfcast,&yrfproj,&mobilavproj)) !=EOF){/* && (num_filled == 3))*/
17133: printf("prevforecast=%d yearsfproj=%.2lf mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
17134: fprintf(ficlog,"prevforecast=%d yearsfproj=%.2lf mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
17135: fprintf(ficres,"prevforecast=%d yearsfproj=%.2lf mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
17136: prvforecast = 2;
17137: }
17138: else {
17139: 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);
17140: 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);
17141: goto end;
17142: }
17143: break;
17144: case 12:
17145: 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)){
17146: 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);
17147: 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);
17148: 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);
17149: 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);
17150: /* day and month of back2 are not used but only year anback2.*/
17151: dateback1=anback1+(mback1-1)/12.+(jback1-1)/365.;
17152: dateback2=anback2+(mback2-1)/12.+(jback2-1)/365.;
17153: prvbackcast = 1;
17154: }
17155: else if((num_filled=sscanf(line,"prevbackcast=%d yearsbproj=%lf mobil_average=%d\n",&prevbcast,&yrbproj,&mobilavproj)) ==3){/* && (num_filled == 3))*/
17156: printf("prevbackcast=%d yearsbproj=%.2lf mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
17157: fprintf(ficlog,"prevbackcast=%d yearsbproj=%.2lf mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
17158: fprintf(ficres,"prevbackcast=%d yearsbproj=%.2lf mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
17159: prvbackcast = 2;
17160: }
17161: else {
17162: 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);
17163: 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);
17164: goto end;
17165: }
17166: break;
17167: case 13:
17168: num_filled=sscanf(line,"result:%[^\n]\n",resultlineori);
17169: nresult++; /* Sum of resultlines */
17170: /* printf("Result %d: result:%s\n",nresult, resultlineori); */
17171: /* removefirstspace(&resultlineori); */
17172:
17173: if(strstr(resultlineori,"v") !=0){
17174: printf("Error. 'v' must be in upper case 'V' result: %s ",resultlineori);
17175: fprintf(ficlog,"Error. 'v' must be in upper case result: %s ",resultlineori);fflush(ficlog);
17176: return 1;
17177: }
17178: trimbb(resultline, resultlineori); /* Suppressing double blank in the resultline */
17179: /* printf("Decoderesult resultline=\"%s\" resultlineori=\"%s\"\n", resultline, resultlineori); */
17180: if(nresult > MAXRESULTLINESPONE-1){
17181: 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);
17182: 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);
17183: goto end;
17184: }
17185:
17186: if(!decoderesult(resultline, nresult)){ /* Fills TKresult[nresult] combination and Tresult[nresult][k4+1] combination values */
17187: fprintf(ficparo,"result: %s\n",resultline);
17188: fprintf(ficres,"result: %s\n",resultline);
17189: fprintf(ficlog,"result: %s\n",resultline);
17190: } else
17191: goto end;
17192: break;
17193: case 14:
17194: printf("Error: Unknown command '%s'\n",line);
17195: fprintf(ficlog,"Error: Unknown command '%s'\n",line);
17196: if(line[0] == ' ' || line[0] == '\n'){
17197: printf("It should not be an empty line '%s'\n",line);
17198: fprintf(ficlog,"It should not be an empty line '%s'\n",line);
17199: }
17200: if(ncovmodel >=2 && nresult==0 ){
17201: printf("ERROR: no result lines! It should be at minimum 'result: V2=0 V1=1 or result:.' %s\n",line);
17202: fprintf(ficlog,"ERROR: no result lines! It should be at minimum 'result: V2=0 V1=1 or result:.' %s\n",line);
17203: }
17204: /* goto end; */
17205: break;
17206: case 15:
17207: printf("End of resultlines.\n");
17208: fprintf(ficlog,"End of resultlines.\n");
17209: break;
17210: default: /* parameterline =0 */
17211: nresult=1;
17212: decoderesult(".",nresult ); /* No covariate */
17213: } /* End switch parameterline */
17214: }while(endishere==0); /* End do */
17215:
17216: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint); */
17217: /* ,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2); */
17218:
17219: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
17220: if(ageminpar == AGEOVERFLOW ||agemaxpar == -AGEOVERFLOW){
17221: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
17222: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
17223: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
17224: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
17225: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
17226: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
17227: }else{
17228: /* printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, prevfcast, backcast, pathc,p, (int)anproj1-(int)agemin, (int)anback1-(int)agemax+1); */
17229: /* It seems that anprojd which is computed from the mean year at interview which is known yet because of freqsummary */
17230: /* date2dmy(dateintmean,&jintmean,&mintmean,&aintmean); */ /* Done in freqsummary */
17231: if(prvforecast==1){
17232: dateprojd=(jproj1+12*mproj1+365*anproj1)/365;
17233: jprojd=jproj1;
17234: mprojd=mproj1;
17235: anprojd=anproj1;
17236: dateprojf=(jproj2+12*mproj2+365*anproj2)/365;
17237: jprojf=jproj2;
17238: mprojf=mproj2;
17239: anprojf=anproj2;
17240: } else if(prvforecast == 2){
17241: dateprojd=dateintmean;
17242: date2dmy(dateprojd,&jprojd, &mprojd, &anprojd);
17243: dateprojf=dateintmean+yrfproj;
17244: date2dmy(dateprojf,&jprojf, &mprojf, &anprojf);
17245: }
17246: if(prvbackcast==1){
17247: datebackd=(jback1+12*mback1+365*anback1)/365;
17248: jbackd=jback1;
17249: mbackd=mback1;
17250: anbackd=anback1;
17251: datebackf=(jback2+12*mback2+365*anback2)/365;
17252: jbackf=jback2;
17253: mbackf=mback2;
17254: anbackf=anback2;
17255: } else if(prvbackcast == 2){
17256: datebackd=dateintmean;
17257: date2dmy(datebackd,&jbackd, &mbackd, &anbackd);
17258: datebackf=dateintmean-yrbproj;
17259: date2dmy(datebackf,&jbackf, &mbackf, &anbackf);
17260: }
17261:
17262: printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,bage, fage, prevfcast, prevbcast, pathc,p, (int)anprojd-bage, (int)anbackd-fage);
17263: }
17264: printinghtml(fileresu,title,datafile, firstpass, lastpass, stepm, weightopt, \
17265: model,imx,jmin,jmax,jmean,rfileres,popforecast,mobilav,prevfcast,mobilavproj,prevbcast, estepm, \
17266: jprev1,mprev1,anprev1,dateprev1, dateprojd, datebackd,jprev2,mprev2,anprev2,dateprev2,dateprojf, datebackf);
17267:
17268: /*------------ free_vector -------------*/
17269: /* chdir(path); */
17270:
17271: /* free_ivector(wav,1,imx); */ /* Moved after last prevalence call */
17272: /* free_imatrix(dh,1,lastpass-firstpass+2,1,imx); */
17273: /* free_imatrix(bh,1,lastpass-firstpass+2,1,imx); */
17274: /* free_imatrix(mw,1,lastpass-firstpass+2,1,imx); */
17275: free_lvector(num,firstobs,lastobs);
17276: free_vector(agedc,firstobs,lastobs);
17277: /*free_matrix(covar,0,NCOVMAX,1,n);*/
17278: /*free_matrix(covar,1,NCOVMAX,1,n);*/
17279: fclose(ficparo);
17280: fclose(ficres);
17281:
17282:
17283: /* Other results (useful)*/
17284:
17285:
17286: /*--------------- Prevalence limit (period or stable prevalence) --------------*/
17287: /*#include "prevlim.h"*/ /* Use ficrespl, ficlog */
17288: prlim=matrix(1,nlstate,1,nlstate);
17289: /* Computes the prevalence limit for each combination k of the dummy covariates by calling prevalim(k) */
17290: prevalence_limit(p, prlim, ageminpar, agemaxpar, ftolpl, &ncvyear);
17291: fclose(ficrespl);
17292:
17293: /*------------- h Pij x at various ages ------------*/
17294: /*#include "hpijx.h"*/
17295: /** 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?*/
17296: /* calls hpxij with combination k */
17297: hPijx(p, bage, fage);
17298: fclose(ficrespij);
17299:
17300: /* ncovcombmax= pow(2,cptcoveff); */
17301: /*-------------- Variance of one-step probabilities for a combination ij or for nres ?---*/
17302: k=1;
17303: varprob(optionfilefiname, matcov, p, delti, nlstate, bage, fage,k,Tvar,nbcode, ncodemax,strstart);
17304:
17305: /* Prevalence for each covariate combination in probs[age][status][cov] */
17306: probs= ma3x(AGEINF,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
17307: for(i=AGEINF;i<=AGESUP;i++)
17308: for(j=1;j<=nlstate+ndeath;j++) /* ndeath is useless but a necessity to be compared with mobaverages */
17309: for(k=1;k<=ncovcombmax;k++)
17310: probs[i][j][k]=0.;
17311: prevalence(probs, ageminpar, agemaxpar, s, agev, nlstate, imx, Tvar, nbcode,
17312: ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
17313: if (mobilav!=0 ||mobilavproj !=0 ) {
17314: mobaverages= ma3x(AGEINF, AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
17315: for(i=AGEINF;i<=AGESUP;i++)
17316: for(j=1;j<=nlstate+ndeath;j++)
17317: for(k=1;k<=ncovcombmax;k++)
17318: mobaverages[i][j][k]=0.;
17319: mobaverage=mobaverages;
17320: if (mobilav!=0) {
17321: printf("Movingaveraging observed prevalence\n");
17322: fprintf(ficlog,"Movingaveraging observed prevalence\n");
17323: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilav)!=0){
17324: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
17325: printf(" Error in movingaverage mobilav=%d\n",mobilav);
17326: }
17327: } else if (mobilavproj !=0) {
17328: printf("Movingaveraging projected observed prevalence\n");
17329: fprintf(ficlog,"Movingaveraging projected observed prevalence\n");
17330: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilavproj)!=0){
17331: fprintf(ficlog," Error in movingaverage mobilavproj=%d\n",mobilavproj);
17332: printf(" Error in movingaverage mobilavproj=%d\n",mobilavproj);
17333: }
17334: }else{
17335: printf("Internal error moving average\n");
17336: fflush(stdout);
17337: exit(1);
17338: }
17339: }/* end if moving average */
17340:
17341: /*---------- Forecasting ------------------*/
17342: if(prevfcast==1){
17343: /* /\* if(stepm ==1){*\/ */
17344: /* /\* anproj1, mproj1, jproj1 either read explicitly or yrfproj *\/ */
17345: /*This done previously after freqsummary.*/
17346: /* dateprojd=(jproj1+12*mproj1+365*anproj1)/365; */
17347: /* dateprojf=(jproj2+12*mproj2+365*anproj2)/365; */
17348:
17349: /* } else if (prvforecast==2){ */
17350: /* /\* if(stepm ==1){*\/ */
17351: /* /\* anproj1, mproj1, jproj1 either read explicitly or yrfproj *\/ */
17352: /* } */
17353: /*prevforecast(fileresu, dateintmean, anproj1, mproj1, jproj1, agemin, agemax, dateprev1, dateprev2, mobilavproj, mobaverage, bage, fage, firstpass, lastpass, anproj2, p, cptcoveff);*/
17354: prevforecast(fileresu,dateintmean, dateprojd, dateprojf, agemin, agemax, dateprev1, dateprev2, mobilavproj, mobaverage, bage, fage, firstpass, lastpass, p, cptcoveff);
17355: }
17356:
17357: /* Prevbcasting */
17358: if(prevbcast==1){
17359: ddnewms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
17360: ddoldms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
17361: ddsavms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
17362:
17363: /*--------------- Back Prevalence limit (period or stable prevalence) --------------*/
17364:
17365: bprlim=matrix(1,nlstate,1,nlstate);
17366:
17367: back_prevalence_limit(p, bprlim, ageminpar, agemaxpar, ftolpl, &ncvyear, dateprev1, dateprev2, firstpass, lastpass, mobilavproj);
17368: fclose(ficresplb);
17369:
17370: hBijx(p, bage, fage, mobaverage);
17371: fclose(ficrespijb);
17372:
17373: /* /\* prevbackforecast(fileresu, mobaverage, anback1, mback1, jback1, agemin, agemax, dateprev1, dateprev2, *\/ */
17374: /* /\* mobilavproj, bage, fage, firstpass, lastpass, anback2, p, cptcoveff); *\/ */
17375: /* prevbackforecast(fileresu, mobaverage, anback1, mback1, jback1, agemin, agemax, dateprev1, dateprev2, */
17376: /* mobilavproj, bage, fage, firstpass, lastpass, anback2, p, cptcoveff); */
17377: prevbackforecast(fileresu, mobaverage, dateintmean, dateprojd, dateprojf, agemin, agemax, dateprev1, dateprev2,
17378: mobilavproj, bage, fage, firstpass, lastpass, p, cptcoveff);
17379:
17380:
17381: varbprlim(fileresu, nresult, mobaverage, mobilavproj, bage, fage, bprlim, &ncvyear, ftolpl, p, matcov, delti, stepm, cptcoveff);
17382:
17383:
17384: free_matrix(bprlim,1,nlstate,1,nlstate); /*here or after loop ? */
17385: free_matrix(ddnewms, 1, nlstate+ndeath, 1, nlstate+ndeath);
17386: free_matrix(ddsavms, 1, nlstate+ndeath, 1, nlstate+ndeath);
17387: free_matrix(ddoldms, 1, nlstate+ndeath, 1, nlstate+ndeath);
17388: } /* end Prevbcasting */
17389:
17390:
17391: /* ------ Other prevalence ratios------------ */
17392:
17393: free_ivector(wav,1,imx);
17394: free_imatrix(dh,1,lastpass-firstpass+2,1,imx);
17395: free_imatrix(bh,1,lastpass-firstpass+2,1,imx);
17396: free_imatrix(mw,1,lastpass-firstpass+2,1,imx);
17397:
17398:
17399: /*---------- Health expectancies, no variances ------------*/
17400:
17401: strcpy(filerese,"E_");
17402: strcat(filerese,fileresu);
17403: if((ficreseij=fopen(filerese,"w"))==NULL) {
17404: printf("Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
17405: fprintf(ficlog,"Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
17406: }
17407: printf("Computing Health Expectancies: result on file '%s' ...", filerese);fflush(stdout);
17408: fprintf(ficlog,"Computing Health Expectancies: result on file '%s' ...", filerese);fflush(ficlog);
17409:
17410: pstamp(ficreseij);
17411:
17412: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
17413: if (cptcovn < 1){i1=1;}
17414:
17415: for(nres=1; nres <= nresult; nres++) /* For each resultline */
17416: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying */
17417: if(i1 != 1 && TKresult[nres]!= k)
17418: continue;
17419: fprintf(ficreseij,"\n#****** ");
17420: printf("\n#****** ");
17421: for(j=1;j<=cptcoveff;j++) {
17422: fprintf(ficreseij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]);
17423: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]);
17424: }
17425: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
17426: printf(" V%d=%lg ",TvarsQ[j], TinvDoQresult[nres][TvarsQ[j]]); /* TvarsQ[j] gives the name of the jth quantitative (fixed or time v) */
17427: fprintf(ficreseij,"V%d=%lg ",TvarsQ[j], TinvDoQresult[nres][TvarsQ[j]]);
17428: }
17429: fprintf(ficreseij,"******\n");
17430: printf("******\n");
17431:
17432: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
17433: oldm=oldms;savm=savms;
17434: /* printf("HELLO Entering evsij bage=%d fage=%d k=%d estepm=%d nres=%d\n",(int) bage, (int)fage, k, estepm, nres); */
17435: evsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, strstart, nres);
17436:
17437: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
17438: }
17439: fclose(ficreseij);
17440: printf("done evsij\n");fflush(stdout);
17441: fprintf(ficlog,"done evsij\n");fflush(ficlog);
17442:
17443:
17444: /*---------- State-specific expectancies and variances ------------*/
17445: /* Should be moved in a function */
17446: strcpy(filerest,"T_");
17447: strcat(filerest,fileresu);
17448: if((ficrest=fopen(filerest,"w"))==NULL) {
17449: printf("Problem with total LE resultfile: %s\n", filerest);goto end;
17450: fprintf(ficlog,"Problem with total LE resultfile: %s\n", filerest);goto end;
17451: }
17452: printf("Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(stdout);
17453: fprintf(ficlog,"Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(ficlog);
17454: strcpy(fileresstde,"STDE_");
17455: strcat(fileresstde,fileresu);
17456: if((ficresstdeij=fopen(fileresstde,"w"))==NULL) {
17457: printf("Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
17458: fprintf(ficlog,"Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
17459: }
17460: printf(" Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde);
17461: fprintf(ficlog," Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde);
17462:
17463: strcpy(filerescve,"CVE_");
17464: strcat(filerescve,fileresu);
17465: if((ficrescveij=fopen(filerescve,"w"))==NULL) {
17466: printf("Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0);
17467: fprintf(ficlog,"Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0);
17468: }
17469: printf(" Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve);
17470: fprintf(ficlog," Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve);
17471:
17472: strcpy(fileresv,"V_");
17473: strcat(fileresv,fileresu);
17474: if((ficresvij=fopen(fileresv,"w"))==NULL) {
17475: printf("Problem with variance resultfile: %s\n", fileresv);exit(0);
17476: fprintf(ficlog,"Problem with variance resultfile: %s\n", fileresv);exit(0);
17477: }
17478: printf(" Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(stdout);
17479: fprintf(ficlog," Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(ficlog);
17480:
17481: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
17482: if (cptcovn < 1){i1=1;}
17483:
17484: for(nres=1; nres <= nresult; nres++) /* For each resultline, find the combination and output results according to the values of dummies and then quanti. */
17485: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying. For each nres and each value at position k
17486: * we know Tresult[nres][result_position]= value of the dummy variable at the result_position in the nres resultline
17487: * Tvqresult[nres][result_position]= id of the variable at the result_position in the nres resultline
17488: * and Tqresult[nres][result_position]= value of the variable at the result_position in the nres resultline */
17489: /* */
17490: if(i1 != 1 && TKresult[nres]!= k) /* TKresult[nres] is the combination of this nres resultline. All the i1 combinations are not output */
17491: continue;
17492: printf("\n# model %s \n#****** Result for:", model);
17493: fprintf(ficrest,"\n# model %s \n#****** Result for:", model);
17494: fprintf(ficlog,"\n# model %s \n#****** Result for:", model);
17495: /* It might not be a good idea to mix dummies and quantitative */
17496: /* 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 *\/ */
17497: 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 */
17498: /* printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); /\* Output by variables in the resultline *\/ */
17499: /* Tvaraff[j] is the name of the dummy variable in position j in the equation model:
17500: * Tvaraff[1]@9={4, 3, 0, 0, 0, 0, 0, 0, 0}, in model=V5+V4+V3+V4*V3+V5*age
17501: * (V5 is quanti) V4 and V3 are dummies
17502: * TnsdVar[4] is the position 1 and TnsdVar[3]=2 in codtabm(k,l)(V4 V3)=V4 V3
17503: * l=1 l=2
17504: * k=1 1 1 0 0
17505: * k=2 2 1 1 0
17506: * k=3 [1] [2] 0 1
17507: * k=4 2 2 1 1
17508: * If nres=1 result: V3=1 V4=0 then k=3 and outputs
17509: * If nres=2 result: V4=1 V3=0 then k=2 and outputs
17510: * nres=1 =>k=3 j=1 V4= nbcode[4][codtabm(3,1)=1)=0; j=2 V3= nbcode[3][codtabm(3,2)=2]=1
17511: * nres=2 =>k=2 j=1 V4= nbcode[4][codtabm(2,1)=2)=1; j=2 V3= nbcode[3][codtabm(2,2)=1]=0
17512: */
17513: /* Tvresult[nres][j] Name of the variable at position j in this resultline */
17514: /* Tresult[nres][j] Value of this variable at position j could be a float if quantitative */
17515: /* We give up with the combinations!! */
17516: /* if(debugILK) */
17517: /* 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 *\/ */
17518:
17519: if(Dummy[modelresult[nres][j]]==0){/* Dummy variable of the variable in position modelresult in the model corresponding to j in resultline */
17520: /* 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] */
17521: 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 */
17522: 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 */
17523: 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 */
17524: if(Fixed[modelresult[nres][j]]==0){ /* Fixed */
17525: printf("fixed ");fprintf(ficlog,"fixed ");fprintf(ficrest,"fixed ");
17526: }else{
17527: printf("varyi ");fprintf(ficlog,"varyi ");fprintf(ficrest,"varyi ");
17528: }
17529: /* fprintf(ficrest,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
17530: /* fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
17531: }else if(Dummy[modelresult[nres][j]]==1){ /* Quanti variable */
17532: /* For each selected (single) quantitative value */
17533: printf(" V%d=%lg ",Tvqresult[nres][j],Tqresult[nres][j]);
17534: fprintf(ficlog," V%d=%lg ",Tvqresult[nres][j],Tqresult[nres][j]);
17535: fprintf(ficrest," V%d=%lg ",Tvqresult[nres][j],Tqresult[nres][j]);
17536: if(Fixed[modelresult[nres][j]]==0){ /* Fixed */
17537: printf("fixed ");fprintf(ficlog,"fixed ");fprintf(ficrest,"fixed ");
17538: }else{
17539: printf("varyi ");fprintf(ficlog,"varyi ");fprintf(ficrest,"varyi ");
17540: }
17541: }else{
17542: 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 */
17543: 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 */
17544: exit(1);
17545: }
17546: } /* End loop for each variable in the resultline */
17547: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
17548: /* printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); /\* Wrong j is not in the equation model *\/ */
17549: /* fprintf(ficrest," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
17550: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
17551: /* } */
17552: fprintf(ficrest,"******\n");
17553: fprintf(ficlog,"******\n");
17554: printf("******\n");
17555:
17556: fprintf(ficresstdeij,"\n#****** ");
17557: fprintf(ficrescveij,"\n#****** ");
17558: /* It could have been: for(j=1;j<=cptcoveff;j++) {printf("V=%d=%lg",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);} */
17559: /* But it won't be sorted and depends on how the resultline is ordered */
17560: for(j=1;j<=cptcoveff;j++) {
17561: fprintf(ficresstdeij,"V%d=%d ",Tvresult[nres][j],Tresult[nres][j]);
17562: /* fprintf(ficresstdeij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
17563: /* fprintf(ficrescveij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
17564: }
17565: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value, TvarsQind gives the position of a quantitative in model equation */
17566: fprintf(ficresstdeij," V%d=%lg ",Tvar[TvarsQind[j]],Tqresult[nres][resultmodel[nres][TvarsQind[j]]]);
17567: fprintf(ficrescveij," V%d=%lg ",Tvar[TvarsQind[j]],Tqresult[nres][resultmodel[nres][TvarsQind[j]]]);
17568: }
17569: fprintf(ficresstdeij,"******\n");
17570: fprintf(ficrescveij,"******\n");
17571:
17572: fprintf(ficresvij,"\n#****** ");
17573: /* pstamp(ficresvij); */
17574: for(j=1;j<=cptcoveff;j++)
17575: fprintf(ficresvij,"V%d=%d ",Tvresult[nres][j],Tresult[nres][j]);
17576: /* fprintf(ficresvij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[TnsdVar[Tvaraff[j]]])]); */
17577: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
17578: /* fprintf(ficresvij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]); /\* To solve *\/ */
17579: fprintf(ficresvij," V%d=%lg ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); /* Solved */
17580: }
17581: fprintf(ficresvij,"******\n");
17582:
17583: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
17584: oldm=oldms;savm=savms;
17585: printf(" cvevsij ");
17586: fprintf(ficlog, " cvevsij ");
17587: cvevsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, delti, matcov, strstart, nres);
17588: printf(" end cvevsij \n ");
17589: fprintf(ficlog, " end cvevsij \n ");
17590:
17591: /*
17592: */
17593: /* goto endfree; */
17594:
17595: vareij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
17596: pstamp(ficrest);
17597:
17598: epj=vector(1,nlstate+1);
17599: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
17600: oldm=oldms;savm=savms; /* ZZ Segmentation fault */
17601: cptcod= 0; /* To be deleted */
17602: printf("varevsij vpopbased=%d \n",vpopbased);
17603: fprintf(ficlog, "varevsij vpopbased=%d \n",vpopbased);
17604: 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 */
17605: 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 ");
17606: if(vpopbased==1)
17607: 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);
17608: else
17609: fprintf(ficrest,"the age specific forward period (stable) prevalences in each health state \n");
17610: fprintf(ficrest,"# Age popbased mobilav e.. (std) "); /* Adding covariate values? */
17611: for (i=1;i<=nlstate;i++) fprintf(ficrest,"e.%d (std) ",i);
17612: fprintf(ficrest,"\n");
17613: /* printf("Which p?\n"); for(i=1;i<=npar;i++)printf("p[i=%d]=%lf,",i,p[i]);printf("\n"); */
17614: printf("Computing age specific forward period (stable) prevalences in each health state \n");
17615: fprintf(ficlog,"Computing age specific forward period (stable) prevalences in each health state \n");
17616: for(age=bage; age <=fage ;age++){
17617: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, &ncvyear, k, nres); /*ZZ Is it the correct prevalim */
17618: if (vpopbased==1) {
17619: if(mobilav ==0){
17620: for(i=1; i<=nlstate;i++)
17621: prlim[i][i]=probs[(int)age][i][k];
17622: }else{ /* mobilav */
17623: for(i=1; i<=nlstate;i++)
17624: prlim[i][i]=mobaverage[(int)age][i][k];
17625: }
17626: }
17627:
17628: fprintf(ficrest," %4.0f %d %d",age, vpopbased, mobilav);
17629: /* fprintf(ficrest," %4.0f %d %d %d %d",age, vpopbased, mobilav,Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */ /* to be done */
17630: /* printf(" age %4.0f ",age); */
17631: for(j=1, epj[nlstate+1]=0.;j <=nlstate;j++){
17632: for(i=1, epj[j]=0.;i <=nlstate;i++) {
17633: epj[j] += prlim[i][i]*eij[i][j][(int)age];
17634: /*ZZZ printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]);*/
17635: /* printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]); */
17636: }
17637: epj[nlstate+1] +=epj[j];
17638: }
17639: /* printf(" age %4.0f \n",age); */
17640:
17641: for(i=1, vepp=0.;i <=nlstate;i++)
17642: for(j=1;j <=nlstate;j++)
17643: vepp += vareij[i][j][(int)age];
17644: fprintf(ficrest," %7.3f (%7.3f)", epj[nlstate+1],sqrt(vepp));
17645: for(j=1;j <=nlstate;j++){
17646: fprintf(ficrest," %7.3f (%7.3f)", epj[j],sqrt(vareij[j][j][(int)age]));
17647: }
17648: fprintf(ficrest,"\n");
17649: }
17650: } /* End vpopbased */
17651: free_vector(epj,1,nlstate+1);
17652: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
17653: free_ma3x(vareij,1,nlstate,1,nlstate,(int) bage, (int)fage);
17654: printf("done selection\n");fflush(stdout);
17655: fprintf(ficlog,"done selection\n");fflush(ficlog);
17656:
17657: } /* End k selection or end covariate selection for nres */
17658:
17659: printf("done State-specific expectancies\n");fflush(stdout);
17660: fprintf(ficlog,"done State-specific expectancies\n");fflush(ficlog);
17661:
17662: /* variance-covariance of forward period prevalence */
17663: varprlim(fileresu, nresult, mobaverage, mobilavproj, bage, fage, prlim, &ncvyear, ftolpl, p, matcov, delti, stepm, cptcoveff);
17664:
17665:
17666: free_vector(weight,firstobs,lastobs);
17667: free_imatrix(Tvardk,-1,NCOVMAX,1,2);
17668: free_imatrix(Tvard,1,NCOVMAX,1,2);
17669: free_imatrix(s,1,maxwav+1,firstobs,lastobs);
17670: free_matrix(anint,1,maxwav,firstobs,lastobs);
17671: free_matrix(mint,1,maxwav,firstobs,lastobs);
17672: free_ivector(cod,firstobs,lastobs);
17673: free_ivector(tab,1,NCOVMAX);
17674: fclose(ficresstdeij);
17675: fclose(ficrescveij);
17676: fclose(ficresvij);
17677: fclose(ficrest);
17678: fclose(ficpar);
17679:
17680:
17681: /*---------- End : free ----------------*/
17682: if (mobilav!=0 ||mobilavproj !=0)
17683: free_ma3x(mobaverages,AGEINF, AGESUP,1,nlstate+ndeath, 1,ncovcombmax); /* We need to have a squared matrix with prevalence of the dead! */
17684: free_ma3x(probs,AGEINF,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
17685: free_matrix(prlim,1,nlstate,1,nlstate); /*here or after loop ? */
17686: free_matrix(pmmij,1,nlstate+ndeath,1,nlstate+ndeath);
17687: } /* mle==-3 arrives here for freeing */
17688: /* endfree:*/
17689: free_matrix(oldms, 1,nlstate+ndeath,1,nlstate+ndeath);
17690: free_matrix(newms, 1,nlstate+ndeath,1,nlstate+ndeath);
17691: free_matrix(savms, 1,nlstate+ndeath,1,nlstate+ndeath);
17692: /* if(ntv+nqtv>=1)free_ma3x(cotvar,1,maxwav,1,ntv+nqtv,firstobs,lastobs); */
17693: if(ntv+nqtv>=1)free_ma3x(cotvar,1,maxwav,ncovcol+nqv+1,ncovcol+nqv+ntv+nqtv,firstobs,lastobs);
17694: if(nqtv>=1)free_ma3x(cotqvar,1,maxwav,1,nqtv,firstobs,lastobs);
17695: if(nqv>=1)free_matrix(coqvar,1,nqv,firstobs,lastobs);
17696: free_matrix(covar,0,NCOVMAX,firstobs,lastobs);
17697: free_matrix(matcov,1,npar,1,npar);
17698: free_matrix(hess,1,npar,1,npar);
17699: /*free_vector(delti,1,npar);*/
17700: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
17701: free_matrix(agev,1,maxwav,1,imx);
17702: free_ma3x(paramstart,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
17703: free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
17704:
17705: free_ivector(ncodemax,1,NCOVMAX);
17706: free_ivector(ncodemaxwundef,1,NCOVMAX);
17707: free_ivector(Dummy,-1,NCOVMAX);
17708: free_ivector(Fixed,-1,NCOVMAX);
17709: free_ivector(DummyV,-1,NCOVMAX);
17710: free_ivector(FixedV,-1,NCOVMAX);
17711: free_ivector(Typevar,-1,NCOVMAX);
17712: free_ivector(Tvar,1,NCOVMAX);
17713: free_ivector(TvarsQ,1,NCOVMAX);
17714: free_ivector(TvarsQind,1,NCOVMAX);
17715: free_ivector(TvarsD,1,NCOVMAX);
17716: free_ivector(TnsdVar,1,NCOVMAX);
17717: free_ivector(TvarsDind,1,NCOVMAX);
17718: free_ivector(TvarFD,1,NCOVMAX);
17719: free_ivector(TvarFDind,1,NCOVMAX);
17720: free_ivector(TvarF,1,NCOVMAX);
17721: free_ivector(TvarFind,1,NCOVMAX);
17722: free_ivector(TvarV,1,NCOVMAX);
17723: free_ivector(TvarVind,1,NCOVMAX);
17724: free_ivector(TvarA,1,NCOVMAX);
17725: free_ivector(TvarAind,1,NCOVMAX);
17726: free_ivector(TvarFQ,1,NCOVMAX);
17727: free_ivector(TvarFQind,1,NCOVMAX);
17728: free_ivector(TvarVD,1,NCOVMAX);
17729: free_ivector(TvarVDind,1,NCOVMAX);
17730: free_ivector(TvarVQ,1,NCOVMAX);
17731: free_ivector(TvarVQind,1,NCOVMAX);
17732: free_ivector(TvarAVVA,1,NCOVMAX);
17733: free_ivector(TvarAVVAind,1,NCOVMAX);
17734: free_ivector(TvarVVA,1,NCOVMAX);
17735: free_ivector(TvarVVAind,1,NCOVMAX);
17736: free_ivector(TvarVV,1,NCOVMAX);
17737: free_ivector(TvarVVind,1,NCOVMAX);
17738:
17739: free_ivector(Tvarsel,1,NCOVMAX);
17740: free_vector(Tvalsel,1,NCOVMAX);
17741: free_ivector(Tposprod,1,NCOVMAX);
17742: free_ivector(Tprod,1,NCOVMAX);
17743: free_ivector(Tvaraff,1,NCOVMAX);
17744: free_ivector(invalidvarcomb,0,ncovcombmax);
17745: free_ivector(Tage,1,NCOVMAX);
17746: free_ivector(Tmodelind,1,NCOVMAX);
17747: free_ivector(TmodelInvind,1,NCOVMAX);
17748: free_ivector(TmodelInvQind,1,NCOVMAX);
17749:
17750: free_matrix(precov, 1,MAXRESULTLINESPONE,1,NCOVMAX+1); /* Could be elsewhere ?*/
17751:
17752: free_imatrix(nbcode,0,NCOVMAX,0,NCOVMAX);
17753: /* free_imatrix(codtab,1,100,1,10); */
17754: fflush(fichtm);
17755: fflush(ficgp);
17756:
17757:
17758: if((nberr >0) || (nbwarn>0)){
17759: printf("End of Imach with %d errors and/or %d warnings. Please look at the log file for details.\n",nberr,nbwarn);
17760: fprintf(ficlog,"End of Imach with %d errors and/or warnings %d. Please look at the log file for details.\n",nberr,nbwarn);
17761: }else{
17762: printf("End of Imach\n");
17763: fprintf(ficlog,"End of Imach\n");
17764: }
17765: printf("See log file on %s\n",filelog);
17766: /* gettimeofday(&end_time, (struct timezone*)0);*/ /* after time */
17767: /*(void) gettimeofday(&end_time,&tzp);*/
17768: rend_time = time(NULL);
17769: end_time = *localtime(&rend_time);
17770: /* tml = *localtime(&end_time.tm_sec); */
17771: strcpy(strtend,asctime(&end_time));
17772: printf("Local time at start %s\nLocal time at end %s",strstart, strtend);
17773: fprintf(ficlog,"Local time at start %s\nLocal time at end %s\n",strstart, strtend);
17774: printf("Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
17775:
17776: printf("Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
17777: fprintf(ficlog,"Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
17778: fprintf(ficlog,"Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
17779: /* printf("Total time was %d uSec.\n", total_usecs);*/
17780: /* if(fileappend(fichtm,optionfilehtm)){ */
17781: fprintf(fichtm,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
17782: fclose(fichtm);
17783: fprintf(fichtmcov,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
17784: fclose(fichtmcov);
17785: fclose(ficgp);
17786: fclose(ficlog);
17787: /*------ End -----------*/
17788:
17789:
17790: /* Executes gnuplot */
17791:
17792: printf("Before Current directory %s!\n",pathcd);
17793: #ifdef WIN32
17794: if (_chdir(pathcd) != 0)
17795: printf("Can't move to directory %s!\n",path);
17796: if(_getcwd(pathcd,MAXLINE) > 0)
17797: #else
17798: if(chdir(pathcd) != 0)
17799: printf("Can't move to directory %s!\n", path);
17800: if (getcwd(pathcd, MAXLINE) > 0)
17801: #endif
17802: printf("Current directory %s!\n",pathcd);
17803: /*strcat(plotcmd,CHARSEPARATOR);*/
17804: sprintf(plotcmd,"gnuplot");
17805: #ifdef _WIN32
17806: sprintf(plotcmd,"\"%sgnuplot.exe\"",pathimach);
17807: #endif
17808: if(!stat(plotcmd,&info)){
17809: printf("Error or gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
17810: if(!stat(getenv("GNUPLOTBIN"),&info)){
17811: printf("Error or gnuplot program not found: '%s' Environment GNUPLOTBIN not set.\n",plotcmd);fflush(stdout);
17812: }else
17813: strcpy(pplotcmd,plotcmd);
17814: #ifdef __unix
17815: strcpy(plotcmd,GNUPLOTPROGRAM);
17816: if(!stat(plotcmd,&info)){
17817: printf("Error gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
17818: }else
17819: strcpy(pplotcmd,plotcmd);
17820: #endif
17821: }else
17822: strcpy(pplotcmd,plotcmd);
17823:
17824: sprintf(plotcmd,"%s %s",pplotcmd, optionfilegnuplot);
17825: printf("Starting graphs with: '%s'\n",plotcmd);fflush(stdout);
17826: strcpy(pplotcmd,plotcmd);
17827:
17828: if((outcmd=system(plotcmd)) != 0){
17829: printf("Error in gnuplot, command might not be in your path: '%s', err=%d\n", plotcmd, outcmd);
17830: printf("\n Trying if gnuplot resides on the same directory that IMaCh\n");
17831: sprintf(plotcmd,"%sgnuplot %s", pathimach, optionfilegnuplot);
17832: if((outcmd=system(plotcmd)) != 0){
17833: printf("\n Still a problem with gnuplot command %s, err=%d\n", plotcmd, outcmd);
17834: strcpy(plotcmd,pplotcmd);
17835: }
17836: }
17837: printf(" Successful, please wait...");
17838: while (z[0] != 'q') {
17839: /* chdir(path); */
17840: printf("\nType e to edit results with your browser, g to graph again and q for exit: ");
17841: scanf("%s",z);
17842: /* if (z[0] == 'c') system("./imach"); */
17843: if (z[0] == 'e') {
17844: #ifdef __APPLE__
17845: sprintf(pplotcmd, "open %s", optionfilehtm);
17846: #elif __linux
17847: sprintf(pplotcmd, "xdg-open %s", optionfilehtm);
17848: #else
17849: sprintf(pplotcmd, "%s", optionfilehtm);
17850: #endif
17851: printf("Starting browser with: %s",pplotcmd);fflush(stdout);
17852: system(pplotcmd);
17853: }
17854: else if (z[0] == 'g') system(plotcmd);
17855: else if (z[0] == 'q') exit(0);
17856: }
17857: end:
17858: while (z[0] != 'q') {
17859: printf("\nType q for exiting: "); fflush(stdout);
17860: scanf("%s",z);
17861: }
17862: printf("End\n");
17863: exit(0);
17864: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>