Annotation of imach/src/imach.c, revision 1.365
1.365 ! brouard 1: /* $Id: imach.c,v 1.364 2024/06/28 12:27:05 brouard Exp $
1.126 brouard 2: $State: Exp $
1.360 brouard 3: $Log: imach.c,v $
1.365 ! brouard 4: Revision 1.364 2024/06/28 12:27:05 brouard
! 5: * imach.c (Module): fixing some bugs in gnuplot and quantitative variables, but not completely solved
! 6:
1.364 brouard 7: Revision 1.363 2024/06/28 09:31:55 brouard
8: Summary: Adding log lines too
9:
1.363 brouard 10: Revision 1.362 2024/06/28 08:00:31 brouard
11: Summary: 0.99s6
12:
13: * imach.c (Module): s6 errors with age*age (harmless).
14:
1.362 brouard 15: Revision 1.361 2024/05/12 20:29:32 brouard
16: Summary: Version 0.99s5
17:
18: * src/imach.c Version 0.99s5 In fact, the covariance of total life
19: expectancy e.. with a partial life expectancy e.j is high,
20: therefore the complete matrix of variance covariance has to be
21: included in the formula of the standard error of the proportion of
22: total life expectancy spent in a specific state:
23: var(X/Y)=mu_x^2/mu_y^2*(sigma_x^2/mu_x^2 -2
24: sigma_xy/mu_x/mu_y+sigma^2/mu_y^2). Also an error with mle=-3
25: made the program core dump. It is fixed in this version.
26:
1.361 brouard 27: Revision 1.360 2024/04/30 10:59:22 brouard
28: Summary: Version 0.99s4 and estimation of std of e.j/e..
29:
1.360 brouard 30: Revision 1.359 2024/04/24 21:21:17 brouard
31: Summary: First IMaCh version using Brent Praxis software based on Buckhardt and Gegenfürtner C codes
32:
1.359 brouard 33: Revision 1.6 2024/04/24 21:10:29 brouard
34: Summary: First IMaCh version using Brent Praxis software based on Buckhardt and Gegenfürtner C codes
1.358 brouard 35:
1.359 brouard 36: Revision 1.5 2023/10/09 09:10:01 brouard
37: Summary: trying to reconsider
1.357 brouard 38:
1.359 brouard 39: Revision 1.4 2023/06/22 12:50:51 brouard
40: Summary: stil on going
1.357 brouard 41:
1.359 brouard 42: Revision 1.3 2023/06/22 11:28:07 brouard
43: *** empty log message ***
1.356 brouard 44:
1.359 brouard 45: Revision 1.2 2023/06/22 11:22:40 brouard
46: Summary: with svd but not working yet
1.355 brouard 47:
1.354 brouard 48: Revision 1.353 2023/05/08 18:48:22 brouard
49: *** empty log message ***
50:
1.353 brouard 51: Revision 1.352 2023/04/29 10:46:21 brouard
52: *** empty log message ***
53:
1.352 brouard 54: Revision 1.351 2023/04/29 10:43:47 brouard
55: Summary: 099r45
56:
1.351 brouard 57: Revision 1.350 2023/04/24 11:38:06 brouard
58: *** empty log message ***
59:
1.350 brouard 60: Revision 1.349 2023/01/31 09:19:37 brouard
61: Summary: Improvements in models with age*Vn*Vm
62:
1.348 brouard 63: Revision 1.347 2022/09/18 14:36:44 brouard
64: Summary: version 0.99r42
65:
1.347 brouard 66: Revision 1.346 2022/09/16 13:52:36 brouard
67: * src/imach.c (Module): 0.99r41 Was an error when product of timevarying and fixed. Using FixedV[of name] now. Thank you Feinuo
68:
1.346 brouard 69: Revision 1.345 2022/09/16 13:40:11 brouard
70: Summary: Version 0.99r41
71:
72: * imach.c (Module): 0.99r41 Was an error when product of timevarying and fixed. Using FixedV[of name] now. Thank you Feinuo
73:
1.345 brouard 74: Revision 1.344 2022/09/14 19:33:30 brouard
75: Summary: version 0.99r40
76:
77: * imach.c (Module): Fixing names of variables in T_ (thanks to Feinuo)
78:
1.344 brouard 79: Revision 1.343 2022/09/14 14:22:16 brouard
80: Summary: version 0.99r39
81:
82: * imach.c (Module): Version 0.99r39 with colored dummy covariates
83: (fixed or time varying), using new last columns of
84: ILK_parameter.txt file.
85:
1.343 brouard 86: Revision 1.342 2022/09/11 19:54:09 brouard
87: Summary: 0.99r38
88:
89: * imach.c (Module): Adding timevarying products of any kinds,
90: should work before shifting cotvar from ncovcol+nqv columns in
91: order to have a correspondance between the column of cotvar and
92: the id of column.
93: (Module): Some cleaning and adding covariates in ILK.txt
94:
1.342 brouard 95: Revision 1.341 2022/09/11 07:58:42 brouard
96: Summary: Version 0.99r38
97:
98: After adding change in cotvar.
99:
1.341 brouard 100: Revision 1.340 2022/09/11 07:53:11 brouard
101: Summary: Version imach 0.99r37
102:
103: * imach.c (Module): Adding timevarying products of any kinds,
104: should work before shifting cotvar from ncovcol+nqv columns in
105: order to have a correspondance between the column of cotvar and
106: the id of column.
107:
1.340 brouard 108: Revision 1.339 2022/09/09 17:55:22 brouard
109: Summary: version 0.99r37
110:
111: * imach.c (Module): Many improvements for fixing products of fixed
112: timevarying as well as fixed * fixed, and test with quantitative
113: covariate.
114:
1.339 brouard 115: Revision 1.338 2022/09/04 17:40:33 brouard
116: Summary: 0.99r36
117:
118: * imach.c (Module): Now the easy runs i.e. without result or
119: model=1+age only did not work. The defautl combination should be 1
120: and not 0 because everything hasn't been tranformed yet.
121:
1.338 brouard 122: Revision 1.337 2022/09/02 14:26:02 brouard
123: Summary: version 0.99r35
124:
125: * src/imach.c: Version 0.99r35 because it outputs same results with
126: 1+age+V1+V1*age for females and 1+age for females only
127: (education=1 noweight)
128:
1.337 brouard 129: Revision 1.336 2022/08/31 09:52:36 brouard
130: *** empty log message ***
131:
1.336 brouard 132: Revision 1.335 2022/08/31 08:23:16 brouard
133: Summary: improvements...
134:
1.335 brouard 135: Revision 1.334 2022/08/25 09:08:41 brouard
136: Summary: In progress for quantitative
137:
1.334 brouard 138: Revision 1.333 2022/08/21 09:10:30 brouard
139: * src/imach.c (Module): Version 0.99r33 A lot of changes in
140: reassigning covariates: my first idea was that people will always
141: use the first covariate V1 into the model but in fact they are
142: producing data with many covariates and can use an equation model
143: with some of the covariate; it means that in a model V2+V3 instead
144: of codtabm(k,Tvaraff[j]) which calculates for combination k, for
145: three covariates (V1, V2, V3) the value of Tvaraff[j], but in fact
146: the equation model is restricted to two variables only (V2, V3)
147: and the combination for V2 should be codtabm(k,1) instead of
148: (codtabm(k,2), and the code should be
149: codtabm(k,TnsdVar[Tvaraff[j]]. Many many changes have been
150: made. All of these should be simplified once a day like we did in
151: hpxij() for example by using precov[nres] which is computed in
152: decoderesult for each nres of each resultline. Loop should be done
153: on the equation model globally by distinguishing only product with
154: age (which are changing with age) and no more on type of
155: covariates, single dummies, single covariates.
156:
1.333 brouard 157: Revision 1.332 2022/08/21 09:06:25 brouard
158: Summary: Version 0.99r33
159:
160: * src/imach.c (Module): Version 0.99r33 A lot of changes in
161: reassigning covariates: my first idea was that people will always
162: use the first covariate V1 into the model but in fact they are
163: producing data with many covariates and can use an equation model
164: with some of the covariate; it means that in a model V2+V3 instead
165: of codtabm(k,Tvaraff[j]) which calculates for combination k, for
166: three covariates (V1, V2, V3) the value of Tvaraff[j], but in fact
167: the equation model is restricted to two variables only (V2, V3)
168: and the combination for V2 should be codtabm(k,1) instead of
169: (codtabm(k,2), and the code should be
170: codtabm(k,TnsdVar[Tvaraff[j]]. Many many changes have been
171: made. All of these should be simplified once a day like we did in
172: hpxij() for example by using precov[nres] which is computed in
173: decoderesult for each nres of each resultline. Loop should be done
174: on the equation model globally by distinguishing only product with
175: age (which are changing with age) and no more on type of
176: covariates, single dummies, single covariates.
177:
1.332 brouard 178: Revision 1.331 2022/08/07 05:40:09 brouard
179: *** empty log message ***
180:
1.331 brouard 181: Revision 1.330 2022/08/06 07:18:25 brouard
182: Summary: last 0.99r31
183:
184: * imach.c (Module): Version of imach using partly decoderesult to rebuild xpxij function
185:
1.330 brouard 186: Revision 1.329 2022/08/03 17:29:54 brouard
187: * imach.c (Module): Many errors in graphs fixed with Vn*age covariates.
188:
1.329 brouard 189: Revision 1.328 2022/07/27 17:40:48 brouard
190: Summary: valgrind bug fixed by initializing to zero DummyV as well as Tage
191:
1.328 brouard 192: Revision 1.327 2022/07/27 14:47:35 brouard
193: Summary: Still a problem for one-step probabilities in case of quantitative variables
194:
1.327 brouard 195: Revision 1.326 2022/07/26 17:33:55 brouard
196: Summary: some test with nres=1
197:
1.326 brouard 198: Revision 1.325 2022/07/25 14:27:23 brouard
199: Summary: r30
200:
201: * imach.c (Module): Error cptcovn instead of nsd in bmij (was
202: coredumped, revealed by Feiuno, thank you.
203:
1.325 brouard 204: Revision 1.324 2022/07/23 17:44:26 brouard
205: *** empty log message ***
206:
1.324 brouard 207: Revision 1.323 2022/07/22 12:30:08 brouard
208: * imach.c (Module): Output of Wald test in the htm file and not only in the log.
209:
1.323 brouard 210: Revision 1.322 2022/07/22 12:27:48 brouard
211: * imach.c (Module): Output of Wald test in the htm file and not only in the log.
212:
1.322 brouard 213: Revision 1.321 2022/07/22 12:04:24 brouard
214: Summary: r28
215:
216: * imach.c (Module): Output of Wald test in the htm file and not only in the log.
217:
1.321 brouard 218: Revision 1.320 2022/06/02 05:10:11 brouard
219: *** empty log message ***
220:
1.320 brouard 221: Revision 1.319 2022/06/02 04:45:11 brouard
222: * imach.c (Module): Adding the Wald tests from the log to the main
223: htm for better display of the maximum likelihood estimators.
224:
1.319 brouard 225: Revision 1.318 2022/05/24 08:10:59 brouard
226: * imach.c (Module): Some attempts to find a bug of wrong estimates
227: of confidencce intervals with product in the equation modelC
228:
1.318 brouard 229: Revision 1.317 2022/05/15 15:06:23 brouard
230: * imach.c (Module): Some minor improvements
231:
1.317 brouard 232: Revision 1.316 2022/05/11 15:11:31 brouard
233: Summary: r27
234:
1.316 brouard 235: Revision 1.315 2022/05/11 15:06:32 brouard
236: *** empty log message ***
237:
1.315 brouard 238: Revision 1.314 2022/04/13 17:43:09 brouard
239: * imach.c (Module): Adding link to text data files
240:
1.314 brouard 241: Revision 1.313 2022/04/11 15:57:42 brouard
242: * imach.c (Module): Error in rewriting the 'r' file with yearsfproj or yearsbproj fixed
243:
1.313 brouard 244: Revision 1.312 2022/04/05 21:24:39 brouard
245: *** empty log message ***
246:
1.312 brouard 247: Revision 1.311 2022/04/05 21:03:51 brouard
248: Summary: Fixed quantitative covariates
249:
250: Fixed covariates (dummy or quantitative)
251: with missing values have never been allowed but are ERRORS and
252: program quits. Standard deviations of fixed covariates were
253: wrongly computed. Mean and standard deviations of time varying
254: covariates are still not computed.
255:
1.311 brouard 256: Revision 1.310 2022/03/17 08:45:53 brouard
257: Summary: 99r25
258:
259: Improving detection of errors: result lines should be compatible with
260: the model.
261:
1.310 brouard 262: Revision 1.309 2021/05/20 12:39:14 brouard
263: Summary: Version 0.99r24
264:
1.309 brouard 265: Revision 1.308 2021/03/31 13:11:57 brouard
266: Summary: Version 0.99r23
267:
268:
269: * imach.c (Module): Still bugs in the result loop. Thank to Holly Benett
270:
1.308 brouard 271: Revision 1.307 2021/03/08 18:11:32 brouard
272: Summary: 0.99r22 fixed bug on result:
273:
1.307 brouard 274: Revision 1.306 2021/02/20 15:44:02 brouard
275: Summary: Version 0.99r21
276:
277: * imach.c (Module): Fix bug on quitting after result lines!
278: (Module): Version 0.99r21
279:
1.306 brouard 280: Revision 1.305 2021/02/20 15:28:30 brouard
281: * imach.c (Module): Fix bug on quitting after result lines!
282:
1.305 brouard 283: Revision 1.304 2021/02/12 11:34:20 brouard
284: * imach.c (Module): The use of a Windows BOM (huge) file is now an error
285:
1.304 brouard 286: Revision 1.303 2021/02/11 19:50:15 brouard
287: * (Module): imach.c Someone entered 'results:' instead of 'result:'. Now it is an error which is printed.
288:
1.303 brouard 289: Revision 1.302 2020/02/22 21:00:05 brouard
290: * (Module): imach.c Update mle=-3 (for computing Life expectancy
291: and life table from the data without any state)
292:
1.302 brouard 293: Revision 1.301 2019/06/04 13:51:20 brouard
294: Summary: Error in 'r'parameter file backcast yearsbproj instead of yearsfproj
295:
1.301 brouard 296: Revision 1.300 2019/05/22 19:09:45 brouard
297: Summary: version 0.99r19 of May 2019
298:
1.300 brouard 299: Revision 1.299 2019/05/22 18:37:08 brouard
300: Summary: Cleaned 0.99r19
301:
1.299 brouard 302: Revision 1.298 2019/05/22 18:19:56 brouard
303: *** empty log message ***
304:
1.298 brouard 305: Revision 1.297 2019/05/22 17:56:10 brouard
306: Summary: Fix bug by moving date2dmy and nhstepm which gaefin=-1
307:
1.297 brouard 308: Revision 1.296 2019/05/20 13:03:18 brouard
309: Summary: Projection syntax simplified
310:
311:
312: We can now start projections, forward or backward, from the mean date
313: of inteviews up to or down to a number of years of projection:
314: prevforecast=1 yearsfproj=15.3 mobil_average=0
315: or
316: prevforecast=1 starting-proj-date=1/1/2007 final-proj-date=12/31/2017 mobil_average=0
317: or
318: prevbackcast=1 yearsbproj=12.3 mobil_average=1
319: or
320: prevbackcast=1 starting-back-date=1/10/1999 final-back-date=1/1/1985 mobil_average=1
321:
1.296 brouard 322: Revision 1.295 2019/05/18 09:52:50 brouard
323: Summary: doxygen tex bug
324:
1.295 brouard 325: Revision 1.294 2019/05/16 14:54:33 brouard
326: Summary: There was some wrong lines added
327:
1.294 brouard 328: Revision 1.293 2019/05/09 15:17:34 brouard
329: *** empty log message ***
330:
1.293 brouard 331: Revision 1.292 2019/05/09 14:17:20 brouard
332: Summary: Some updates
333:
1.292 brouard 334: Revision 1.291 2019/05/09 13:44:18 brouard
335: Summary: Before ncovmax
336:
1.291 brouard 337: Revision 1.290 2019/05/09 13:39:37 brouard
338: Summary: 0.99r18 unlimited number of individuals
339:
340: 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.
341:
1.290 brouard 342: Revision 1.289 2018/12/13 09:16:26 brouard
343: Summary: Bug for young ages (<-30) will be in r17
344:
1.289 brouard 345: Revision 1.288 2018/05/02 20:58:27 brouard
346: Summary: Some bugs fixed
347:
1.288 brouard 348: Revision 1.287 2018/05/01 17:57:25 brouard
349: Summary: Bug fixed by providing frequencies only for non missing covariates
350:
1.287 brouard 351: Revision 1.286 2018/04/27 14:27:04 brouard
352: Summary: some minor bugs
353:
1.286 brouard 354: Revision 1.285 2018/04/21 21:02:16 brouard
355: Summary: Some bugs fixed, valgrind tested
356:
1.285 brouard 357: Revision 1.284 2018/04/20 05:22:13 brouard
358: Summary: Computing mean and stdeviation of fixed quantitative variables
359:
1.284 brouard 360: Revision 1.283 2018/04/19 14:49:16 brouard
361: Summary: Some minor bugs fixed
362:
1.283 brouard 363: Revision 1.282 2018/02/27 22:50:02 brouard
364: *** empty log message ***
365:
1.282 brouard 366: Revision 1.281 2018/02/27 19:25:23 brouard
367: Summary: Adding second argument for quitting
368:
1.281 brouard 369: Revision 1.280 2018/02/21 07:58:13 brouard
370: Summary: 0.99r15
371:
372: New Makefile with recent VirtualBox 5.26. Bug in sqrt negatve in imach.c
373:
1.280 brouard 374: Revision 1.279 2017/07/20 13:35:01 brouard
375: Summary: temporary working
376:
1.279 brouard 377: Revision 1.278 2017/07/19 14:09:02 brouard
378: Summary: Bug for mobil_average=0 and prevforecast fixed(?)
379:
1.278 brouard 380: Revision 1.277 2017/07/17 08:53:49 brouard
381: Summary: BOM files can be read now
382:
1.277 brouard 383: Revision 1.276 2017/06/30 15:48:31 brouard
384: Summary: Graphs improvements
385:
1.276 brouard 386: Revision 1.275 2017/06/30 13:39:33 brouard
387: Summary: Saito's color
388:
1.275 brouard 389: Revision 1.274 2017/06/29 09:47:08 brouard
390: Summary: Version 0.99r14
391:
1.274 brouard 392: Revision 1.273 2017/06/27 11:06:02 brouard
393: Summary: More documentation on projections
394:
1.273 brouard 395: Revision 1.272 2017/06/27 10:22:40 brouard
396: Summary: Color of backprojection changed from 6 to 5(yellow)
397:
1.272 brouard 398: Revision 1.271 2017/06/27 10:17:50 brouard
399: Summary: Some bug with rint
400:
1.271 brouard 401: Revision 1.270 2017/05/24 05:45:29 brouard
402: *** empty log message ***
403:
1.270 brouard 404: Revision 1.269 2017/05/23 08:39:25 brouard
405: Summary: Code into subroutine, cleanings
406:
1.269 brouard 407: Revision 1.268 2017/05/18 20:09:32 brouard
408: Summary: backprojection and confidence intervals of backprevalence
409:
1.268 brouard 410: Revision 1.267 2017/05/13 10:25:05 brouard
411: Summary: temporary save for backprojection
412:
1.267 brouard 413: Revision 1.266 2017/05/13 07:26:12 brouard
414: Summary: Version 0.99r13 (improvements and bugs fixed)
415:
1.266 brouard 416: Revision 1.265 2017/04/26 16:22:11 brouard
417: Summary: imach 0.99r13 Some bugs fixed
418:
1.265 brouard 419: Revision 1.264 2017/04/26 06:01:29 brouard
420: Summary: Labels in graphs
421:
1.264 brouard 422: Revision 1.263 2017/04/24 15:23:15 brouard
423: Summary: to save
424:
1.263 brouard 425: Revision 1.262 2017/04/18 16:48:12 brouard
426: *** empty log message ***
427:
1.262 brouard 428: Revision 1.261 2017/04/05 10:14:09 brouard
429: Summary: Bug in E_ as well as in T_ fixed nres-1 vs k1-1
430:
1.261 brouard 431: Revision 1.260 2017/04/04 17:46:59 brouard
432: Summary: Gnuplot indexations fixed (humm)
433:
1.260 brouard 434: Revision 1.259 2017/04/04 13:01:16 brouard
435: Summary: Some errors to warnings only if date of death is unknown but status is death we could set to pi3
436:
1.259 brouard 437: Revision 1.258 2017/04/03 10:17:47 brouard
438: Summary: Version 0.99r12
439:
440: Some cleanings, conformed with updated documentation.
441:
1.258 brouard 442: Revision 1.257 2017/03/29 16:53:30 brouard
443: Summary: Temp
444:
1.257 brouard 445: Revision 1.256 2017/03/27 05:50:23 brouard
446: Summary: Temporary
447:
1.256 brouard 448: Revision 1.255 2017/03/08 16:02:28 brouard
449: Summary: IMaCh version 0.99r10 bugs in gnuplot fixed
450:
1.255 brouard 451: Revision 1.254 2017/03/08 07:13:00 brouard
452: Summary: Fixing data parameter line
453:
1.254 brouard 454: Revision 1.253 2016/12/15 11:59:41 brouard
455: Summary: 0.99 in progress
456:
1.253 brouard 457: Revision 1.252 2016/09/15 21:15:37 brouard
458: *** empty log message ***
459:
1.252 brouard 460: Revision 1.251 2016/09/15 15:01:13 brouard
461: Summary: not working
462:
1.251 brouard 463: Revision 1.250 2016/09/08 16:07:27 brouard
464: Summary: continue
465:
1.250 brouard 466: Revision 1.249 2016/09/07 17:14:18 brouard
467: Summary: Starting values from frequencies
468:
1.249 brouard 469: Revision 1.248 2016/09/07 14:10:18 brouard
470: *** empty log message ***
471:
1.248 brouard 472: Revision 1.247 2016/09/02 11:11:21 brouard
473: *** empty log message ***
474:
1.247 brouard 475: Revision 1.246 2016/09/02 08:49:22 brouard
476: *** empty log message ***
477:
1.246 brouard 478: Revision 1.245 2016/09/02 07:25:01 brouard
479: *** empty log message ***
480:
1.245 brouard 481: Revision 1.244 2016/09/02 07:17:34 brouard
482: *** empty log message ***
483:
1.244 brouard 484: Revision 1.243 2016/09/02 06:45:35 brouard
485: *** empty log message ***
486:
1.243 brouard 487: Revision 1.242 2016/08/30 15:01:20 brouard
488: Summary: Fixing a lots
489:
1.242 brouard 490: Revision 1.241 2016/08/29 17:17:25 brouard
491: Summary: gnuplot problem in Back projection to fix
492:
1.241 brouard 493: Revision 1.240 2016/08/29 07:53:18 brouard
494: Summary: Better
495:
1.240 brouard 496: Revision 1.239 2016/08/26 15:51:03 brouard
497: Summary: Improvement in Powell output in order to copy and paste
498:
499: Author:
500:
1.239 brouard 501: Revision 1.238 2016/08/26 14:23:35 brouard
502: Summary: Starting tests of 0.99
503:
1.238 brouard 504: Revision 1.237 2016/08/26 09:20:19 brouard
505: Summary: to valgrind
506:
1.237 brouard 507: Revision 1.236 2016/08/25 10:50:18 brouard
508: *** empty log message ***
509:
1.236 brouard 510: Revision 1.235 2016/08/25 06:59:23 brouard
511: *** empty log message ***
512:
1.235 brouard 513: Revision 1.234 2016/08/23 16:51:20 brouard
514: *** empty log message ***
515:
1.234 brouard 516: Revision 1.233 2016/08/23 07:40:50 brouard
517: Summary: not working
518:
1.233 brouard 519: Revision 1.232 2016/08/22 14:20:21 brouard
520: Summary: not working
521:
1.232 brouard 522: Revision 1.231 2016/08/22 07:17:15 brouard
523: Summary: not working
524:
1.231 brouard 525: Revision 1.230 2016/08/22 06:55:53 brouard
526: Summary: Not working
527:
1.230 brouard 528: Revision 1.229 2016/07/23 09:45:53 brouard
529: Summary: Completing for func too
530:
1.229 brouard 531: Revision 1.228 2016/07/22 17:45:30 brouard
532: Summary: Fixing some arrays, still debugging
533:
1.227 brouard 534: Revision 1.226 2016/07/12 18:42:34 brouard
535: Summary: temp
536:
1.226 brouard 537: Revision 1.225 2016/07/12 08:40:03 brouard
538: Summary: saving but not running
539:
1.225 brouard 540: Revision 1.224 2016/07/01 13:16:01 brouard
541: Summary: Fixes
542:
1.224 brouard 543: Revision 1.223 2016/02/19 09:23:35 brouard
544: Summary: temporary
545:
1.223 brouard 546: Revision 1.222 2016/02/17 08:14:50 brouard
547: Summary: Probably last 0.98 stable version 0.98r6
548:
1.222 brouard 549: Revision 1.221 2016/02/15 23:35:36 brouard
550: Summary: minor bug
551:
1.220 brouard 552: Revision 1.219 2016/02/15 00:48:12 brouard
553: *** empty log message ***
554:
1.219 brouard 555: Revision 1.218 2016/02/12 11:29:23 brouard
556: Summary: 0.99 Back projections
557:
1.218 brouard 558: Revision 1.217 2015/12/23 17:18:31 brouard
559: Summary: Experimental backcast
560:
1.217 brouard 561: Revision 1.216 2015/12/18 17:32:11 brouard
562: Summary: 0.98r4 Warning and status=-2
563:
564: Version 0.98r4 is now:
565: - displaying an error when status is -1, date of interview unknown and date of death known;
566: - permitting a status -2 when the vital status is unknown at a known date of right truncation.
567: Older changes concerning s=-2, dating from 2005 have been supersed.
568:
1.216 brouard 569: Revision 1.215 2015/12/16 08:52:24 brouard
570: Summary: 0.98r4 working
571:
1.215 brouard 572: Revision 1.214 2015/12/16 06:57:54 brouard
573: Summary: temporary not working
574:
1.214 brouard 575: Revision 1.213 2015/12/11 18:22:17 brouard
576: Summary: 0.98r4
577:
1.213 brouard 578: Revision 1.212 2015/11/21 12:47:24 brouard
579: Summary: minor typo
580:
1.212 brouard 581: Revision 1.211 2015/11/21 12:41:11 brouard
582: Summary: 0.98r3 with some graph of projected cross-sectional
583:
584: Author: Nicolas Brouard
585:
1.211 brouard 586: Revision 1.210 2015/11/18 17:41:20 brouard
1.252 brouard 587: Summary: Start working on projected prevalences Revision 1.209 2015/11/17 22:12:03 brouard
1.210 brouard 588: Summary: Adding ftolpl parameter
589: Author: N Brouard
590:
591: We had difficulties to get smoothed confidence intervals. It was due
592: to the period prevalence which wasn't computed accurately. The inner
593: parameter ftolpl is now an outer parameter of the .imach parameter
594: file after estepm. If ftolpl is small 1.e-4 and estepm too,
595: computation are long.
596:
1.209 brouard 597: Revision 1.208 2015/11/17 14:31:57 brouard
598: Summary: temporary
599:
1.208 brouard 600: Revision 1.207 2015/10/27 17:36:57 brouard
601: *** empty log message ***
602:
1.207 brouard 603: Revision 1.206 2015/10/24 07:14:11 brouard
604: *** empty log message ***
605:
1.206 brouard 606: Revision 1.205 2015/10/23 15:50:53 brouard
607: Summary: 0.98r3 some clarification for graphs on likelihood contributions
608:
1.205 brouard 609: Revision 1.204 2015/10/01 16:20:26 brouard
610: Summary: Some new graphs of contribution to likelihood
611:
1.204 brouard 612: Revision 1.203 2015/09/30 17:45:14 brouard
613: Summary: looking at better estimation of the hessian
614:
615: Also a better criteria for convergence to the period prevalence And
616: therefore adding the number of years needed to converge. (The
617: prevalence in any alive state shold sum to one
618:
1.203 brouard 619: Revision 1.202 2015/09/22 19:45:16 brouard
620: Summary: Adding some overall graph on contribution to likelihood. Might change
621:
1.202 brouard 622: Revision 1.201 2015/09/15 17:34:58 brouard
623: Summary: 0.98r0
624:
625: - Some new graphs like suvival functions
626: - Some bugs fixed like model=1+age+V2.
627:
1.201 brouard 628: Revision 1.200 2015/09/09 16:53:55 brouard
629: Summary: Big bug thanks to Flavia
630:
631: Even model=1+age+V2. did not work anymore
632:
1.200 brouard 633: Revision 1.199 2015/09/07 14:09:23 brouard
634: Summary: 0.98q6 changing default small png format for graph to vectorized svg.
635:
1.199 brouard 636: Revision 1.198 2015/09/03 07:14:39 brouard
637: Summary: 0.98q5 Flavia
638:
1.198 brouard 639: Revision 1.197 2015/09/01 18:24:39 brouard
640: *** empty log message ***
641:
1.197 brouard 642: Revision 1.196 2015/08/18 23:17:52 brouard
643: Summary: 0.98q5
644:
1.196 brouard 645: Revision 1.195 2015/08/18 16:28:39 brouard
646: Summary: Adding a hack for testing purpose
647:
648: After reading the title, ftol and model lines, if the comment line has
649: a q, starting with #q, the answer at the end of the run is quit. It
650: permits to run test files in batch with ctest. The former workaround was
651: $ echo q | imach foo.imach
652:
1.195 brouard 653: Revision 1.194 2015/08/18 13:32:00 brouard
654: Summary: Adding error when the covariance matrix doesn't contain the exact number of lines required by the model line.
655:
1.194 brouard 656: Revision 1.193 2015/08/04 07:17:42 brouard
657: Summary: 0.98q4
658:
1.193 brouard 659: Revision 1.192 2015/07/16 16:49:02 brouard
660: Summary: Fixing some outputs
661:
1.192 brouard 662: Revision 1.191 2015/07/14 10:00:33 brouard
663: Summary: Some fixes
664:
1.191 brouard 665: Revision 1.190 2015/05/05 08:51:13 brouard
666: Summary: Adding digits in output parameters (7 digits instead of 6)
667:
668: Fix 1+age+.
669:
1.190 brouard 670: Revision 1.189 2015/04/30 14:45:16 brouard
671: Summary: 0.98q2
672:
1.189 brouard 673: Revision 1.188 2015/04/30 08:27:53 brouard
674: *** empty log message ***
675:
1.188 brouard 676: Revision 1.187 2015/04/29 09:11:15 brouard
677: *** empty log message ***
678:
1.187 brouard 679: Revision 1.186 2015/04/23 12:01:52 brouard
680: Summary: V1*age is working now, version 0.98q1
681:
682: Some codes had been disabled in order to simplify and Vn*age was
683: working in the optimization phase, ie, giving correct MLE parameters,
684: but, as usual, outputs were not correct and program core dumped.
685:
1.186 brouard 686: Revision 1.185 2015/03/11 13:26:42 brouard
687: Summary: Inclusion of compile and links command line for Intel Compiler
688:
1.185 brouard 689: Revision 1.184 2015/03/11 11:52:39 brouard
690: Summary: Back from Windows 8. Intel Compiler
691:
1.184 brouard 692: Revision 1.183 2015/03/10 20:34:32 brouard
693: Summary: 0.98q0, trying with directest, mnbrak fixed
694:
695: We use directest instead of original Powell test; probably no
696: incidence on the results, but better justifications;
697: We fixed Numerical Recipes mnbrak routine which was wrong and gave
698: wrong results.
699:
1.183 brouard 700: Revision 1.182 2015/02/12 08:19:57 brouard
701: Summary: Trying to keep directest which seems simpler and more general
702: Author: Nicolas Brouard
703:
1.182 brouard 704: Revision 1.181 2015/02/11 23:22:24 brouard
705: Summary: Comments on Powell added
706:
707: Author:
708:
1.181 brouard 709: Revision 1.180 2015/02/11 17:33:45 brouard
710: Summary: Finishing move from main to function (hpijx and prevalence_limit)
711:
1.180 brouard 712: Revision 1.179 2015/01/04 09:57:06 brouard
713: Summary: back to OS/X
714:
1.179 brouard 715: Revision 1.178 2015/01/04 09:35:48 brouard
716: *** empty log message ***
717:
1.178 brouard 718: Revision 1.177 2015/01/03 18:40:56 brouard
719: Summary: Still testing ilc32 on OSX
720:
1.177 brouard 721: Revision 1.176 2015/01/03 16:45:04 brouard
722: *** empty log message ***
723:
1.176 brouard 724: Revision 1.175 2015/01/03 16:33:42 brouard
725: *** empty log message ***
726:
1.175 brouard 727: Revision 1.174 2015/01/03 16:15:49 brouard
728: Summary: Still in cross-compilation
729:
1.174 brouard 730: Revision 1.173 2015/01/03 12:06:26 brouard
731: Summary: trying to detect cross-compilation
732:
1.173 brouard 733: Revision 1.172 2014/12/27 12:07:47 brouard
734: Summary: Back from Visual Studio and Intel, options for compiling for Windows XP
735:
1.172 brouard 736: Revision 1.171 2014/12/23 13:26:59 brouard
737: Summary: Back from Visual C
738:
739: Still problem with utsname.h on Windows
740:
1.171 brouard 741: Revision 1.170 2014/12/23 11:17:12 brouard
742: Summary: Cleaning some \%% back to %%
743:
744: The escape was mandatory for a specific compiler (which one?), but too many warnings.
745:
1.170 brouard 746: Revision 1.169 2014/12/22 23:08:31 brouard
747: Summary: 0.98p
748:
749: Outputs some informations on compiler used, OS etc. Testing on different platforms.
750:
1.169 brouard 751: Revision 1.168 2014/12/22 15:17:42 brouard
1.170 brouard 752: Summary: update
1.169 brouard 753:
1.168 brouard 754: Revision 1.167 2014/12/22 13:50:56 brouard
755: Summary: Testing uname and compiler version and if compiled 32 or 64
756:
757: Testing on Linux 64
758:
1.167 brouard 759: Revision 1.166 2014/12/22 11:40:47 brouard
760: *** empty log message ***
761:
1.166 brouard 762: Revision 1.165 2014/12/16 11:20:36 brouard
763: Summary: After compiling on Visual C
764:
765: * imach.c (Module): Merging 1.61 to 1.162
766:
1.165 brouard 767: Revision 1.164 2014/12/16 10:52:11 brouard
768: Summary: Merging with Visual C after suppressing some warnings for unused variables. Also fixing Saito's bug 0.98Xn
769:
770: * imach.c (Module): Merging 1.61 to 1.162
771:
1.164 brouard 772: Revision 1.163 2014/12/16 10:30:11 brouard
773: * imach.c (Module): Merging 1.61 to 1.162
774:
1.163 brouard 775: Revision 1.162 2014/09/25 11:43:39 brouard
776: Summary: temporary backup 0.99!
777:
1.162 brouard 778: Revision 1.1 2014/09/16 11:06:58 brouard
779: Summary: With some code (wrong) for nlopt
780:
781: Author:
782:
783: Revision 1.161 2014/09/15 20:41:41 brouard
784: Summary: Problem with macro SQR on Intel compiler
785:
1.161 brouard 786: Revision 1.160 2014/09/02 09:24:05 brouard
787: *** empty log message ***
788:
1.160 brouard 789: Revision 1.159 2014/09/01 10:34:10 brouard
790: Summary: WIN32
791: Author: Brouard
792:
1.159 brouard 793: Revision 1.158 2014/08/27 17:11:51 brouard
794: *** empty log message ***
795:
1.158 brouard 796: Revision 1.157 2014/08/27 16:26:55 brouard
797: Summary: Preparing windows Visual studio version
798: Author: Brouard
799:
800: In order to compile on Visual studio, time.h is now correct and time_t
801: and tm struct should be used. difftime should be used but sometimes I
802: just make the differences in raw time format (time(&now).
803: Trying to suppress #ifdef LINUX
804: Add xdg-open for __linux in order to open default browser.
805:
1.157 brouard 806: Revision 1.156 2014/08/25 20:10:10 brouard
807: *** empty log message ***
808:
1.156 brouard 809: Revision 1.155 2014/08/25 18:32:34 brouard
810: Summary: New compile, minor changes
811: Author: Brouard
812:
1.155 brouard 813: Revision 1.154 2014/06/20 17:32:08 brouard
814: Summary: Outputs now all graphs of convergence to period prevalence
815:
1.154 brouard 816: Revision 1.153 2014/06/20 16:45:46 brouard
817: Summary: If 3 live state, convergence to period prevalence on same graph
818: Author: Brouard
819:
1.153 brouard 820: Revision 1.152 2014/06/18 17:54:09 brouard
821: Summary: open browser, use gnuplot on same dir than imach if not found in the path
822:
1.152 brouard 823: Revision 1.151 2014/06/18 16:43:30 brouard
824: *** empty log message ***
825:
1.151 brouard 826: Revision 1.150 2014/06/18 16:42:35 brouard
827: Summary: If gnuplot is not in the path try on same directory than imach binary (OSX)
828: Author: brouard
829:
1.150 brouard 830: Revision 1.149 2014/06/18 15:51:14 brouard
831: Summary: Some fixes in parameter files errors
832: Author: Nicolas Brouard
833:
1.149 brouard 834: Revision 1.148 2014/06/17 17:38:48 brouard
835: Summary: Nothing new
836: Author: Brouard
837:
838: Just a new packaging for OS/X version 0.98nS
839:
1.148 brouard 840: Revision 1.147 2014/06/16 10:33:11 brouard
841: *** empty log message ***
842:
1.147 brouard 843: Revision 1.146 2014/06/16 10:20:28 brouard
844: Summary: Merge
845: Author: Brouard
846:
847: Merge, before building revised version.
848:
1.146 brouard 849: Revision 1.145 2014/06/10 21:23:15 brouard
850: Summary: Debugging with valgrind
851: Author: Nicolas Brouard
852:
853: Lot of changes in order to output the results with some covariates
854: After the Edimburgh REVES conference 2014, it seems mandatory to
855: improve the code.
856: No more memory valgrind error but a lot has to be done in order to
857: continue the work of splitting the code into subroutines.
858: Also, decodemodel has been improved. Tricode is still not
859: optimal. nbcode should be improved. Documentation has been added in
860: the source code.
861:
1.144 brouard 862: Revision 1.143 2014/01/26 09:45:38 brouard
863: Summary: Version 0.98nR (to be improved, but gives same optimization results as 0.98k. Nice, promising
864:
865: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
866: (Module): Version 0.98nR Running ok, but output format still only works for three covariates.
867:
1.143 brouard 868: Revision 1.142 2014/01/26 03:57:36 brouard
869: Summary: gnuplot changed plot w l 1 has to be changed to plot w l lt 2
870:
871: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
872:
1.142 brouard 873: Revision 1.141 2014/01/26 02:42:01 brouard
874: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
875:
1.141 brouard 876: Revision 1.140 2011/09/02 10:37:54 brouard
877: Summary: times.h is ok with mingw32 now.
878:
1.140 brouard 879: Revision 1.139 2010/06/14 07:50:17 brouard
880: After the theft of my laptop, I probably lost some lines of codes which were not uploaded to the CVS tree.
881: I remember having already fixed agemin agemax which are pointers now but not cvs saved.
882:
1.139 brouard 883: Revision 1.138 2010/04/30 18:19:40 brouard
884: *** empty log message ***
885:
1.138 brouard 886: Revision 1.137 2010/04/29 18:11:38 brouard
887: (Module): Checking covariates for more complex models
888: than V1+V2. A lot of change to be done. Unstable.
889:
1.137 brouard 890: Revision 1.136 2010/04/26 20:30:53 brouard
891: (Module): merging some libgsl code. Fixing computation
892: of likelione (using inter/intrapolation if mle = 0) in order to
893: get same likelihood as if mle=1.
894: Some cleaning of code and comments added.
895:
1.136 brouard 896: Revision 1.135 2009/10/29 15:33:14 brouard
897: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
898:
1.135 brouard 899: Revision 1.134 2009/10/29 13:18:53 brouard
900: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
901:
1.134 brouard 902: Revision 1.133 2009/07/06 10:21:25 brouard
903: just nforces
904:
1.133 brouard 905: Revision 1.132 2009/07/06 08:22:05 brouard
906: Many tings
907:
1.132 brouard 908: Revision 1.131 2009/06/20 16:22:47 brouard
909: Some dimensions resccaled
910:
1.131 brouard 911: Revision 1.130 2009/05/26 06:44:34 brouard
912: (Module): Max Covariate is now set to 20 instead of 8. A
913: lot of cleaning with variables initialized to 0. Trying to make
914: V2+V3*age+V1+V4 strb=V3*age+V1+V4 working better.
915:
1.130 brouard 916: Revision 1.129 2007/08/31 13:49:27 lievre
917: Modification of the way of exiting when the covariate is not binary in order to see on the window the error message before exiting
918:
1.129 lievre 919: Revision 1.128 2006/06/30 13:02:05 brouard
920: (Module): Clarifications on computing e.j
921:
1.128 brouard 922: Revision 1.127 2006/04/28 18:11:50 brouard
923: (Module): Yes the sum of survivors was wrong since
924: imach-114 because nhstepm was no more computed in the age
925: loop. Now we define nhstepma in the age loop.
926: (Module): In order to speed up (in case of numerous covariates) we
927: compute health expectancies (without variances) in a first step
928: and then all the health expectancies with variances or standard
929: deviation (needs data from the Hessian matrices) which slows the
930: computation.
931: In the future we should be able to stop the program is only health
932: expectancies and graph are needed without standard deviations.
933:
1.127 brouard 934: Revision 1.126 2006/04/28 17:23:28 brouard
935: (Module): Yes the sum of survivors was wrong since
936: imach-114 because nhstepm was no more computed in the age
937: loop. Now we define nhstepma in the age loop.
938: Version 0.98h
939:
1.126 brouard 940: Revision 1.125 2006/04/04 15:20:31 lievre
941: Errors in calculation of health expectancies. Age was not initialized.
942: Forecasting file added.
943:
944: Revision 1.124 2006/03/22 17:13:53 lievre
945: Parameters are printed with %lf instead of %f (more numbers after the comma).
946: The log-likelihood is printed in the log file
947:
948: Revision 1.123 2006/03/20 10:52:43 brouard
949: * imach.c (Module): <title> changed, corresponds to .htm file
950: name. <head> headers where missing.
951:
952: * imach.c (Module): Weights can have a decimal point as for
953: English (a comma might work with a correct LC_NUMERIC environment,
954: otherwise the weight is truncated).
955: Modification of warning when the covariates values are not 0 or
956: 1.
957: Version 0.98g
958:
959: Revision 1.122 2006/03/20 09:45:41 brouard
960: (Module): Weights can have a decimal point as for
961: English (a comma might work with a correct LC_NUMERIC environment,
962: otherwise the weight is truncated).
963: Modification of warning when the covariates values are not 0 or
964: 1.
965: Version 0.98g
966:
967: Revision 1.121 2006/03/16 17:45:01 lievre
968: * imach.c (Module): Comments concerning covariates added
969:
970: * imach.c (Module): refinements in the computation of lli if
971: status=-2 in order to have more reliable computation if stepm is
972: not 1 month. Version 0.98f
973:
974: Revision 1.120 2006/03/16 15:10:38 lievre
975: (Module): refinements in the computation of lli if
976: status=-2 in order to have more reliable computation if stepm is
977: not 1 month. Version 0.98f
978:
979: Revision 1.119 2006/03/15 17:42:26 brouard
980: (Module): Bug if status = -2, the loglikelihood was
981: computed as likelihood omitting the logarithm. Version O.98e
982:
983: Revision 1.118 2006/03/14 18:20:07 brouard
984: (Module): varevsij Comments added explaining the second
985: table of variances if popbased=1 .
986: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
987: (Module): Function pstamp added
988: (Module): Version 0.98d
989:
990: Revision 1.117 2006/03/14 17:16:22 brouard
991: (Module): varevsij Comments added explaining the second
992: table of variances if popbased=1 .
993: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
994: (Module): Function pstamp added
995: (Module): Version 0.98d
996:
997: Revision 1.116 2006/03/06 10:29:27 brouard
998: (Module): Variance-covariance wrong links and
999: varian-covariance of ej. is needed (Saito).
1000:
1001: Revision 1.115 2006/02/27 12:17:45 brouard
1002: (Module): One freematrix added in mlikeli! 0.98c
1003:
1004: Revision 1.114 2006/02/26 12:57:58 brouard
1005: (Module): Some improvements in processing parameter
1006: filename with strsep.
1007:
1008: Revision 1.113 2006/02/24 14:20:24 brouard
1009: (Module): Memory leaks checks with valgrind and:
1010: datafile was not closed, some imatrix were not freed and on matrix
1011: allocation too.
1012:
1013: Revision 1.112 2006/01/30 09:55:26 brouard
1014: (Module): Back to gnuplot.exe instead of wgnuplot.exe
1015:
1016: Revision 1.111 2006/01/25 20:38:18 brouard
1017: (Module): Lots of cleaning and bugs added (Gompertz)
1018: (Module): Comments can be added in data file. Missing date values
1019: can be a simple dot '.'.
1020:
1021: Revision 1.110 2006/01/25 00:51:50 brouard
1022: (Module): Lots of cleaning and bugs added (Gompertz)
1023:
1024: Revision 1.109 2006/01/24 19:37:15 brouard
1025: (Module): Comments (lines starting with a #) are allowed in data.
1026:
1027: Revision 1.108 2006/01/19 18:05:42 lievre
1028: Gnuplot problem appeared...
1029: To be fixed
1030:
1031: Revision 1.107 2006/01/19 16:20:37 brouard
1032: Test existence of gnuplot in imach path
1033:
1034: Revision 1.106 2006/01/19 13:24:36 brouard
1035: Some cleaning and links added in html output
1036:
1037: Revision 1.105 2006/01/05 20:23:19 lievre
1038: *** empty log message ***
1039:
1040: Revision 1.104 2005/09/30 16:11:43 lievre
1041: (Module): sump fixed, loop imx fixed, and simplifications.
1042: (Module): If the status is missing at the last wave but we know
1043: that the person is alive, then we can code his/her status as -2
1044: (instead of missing=-1 in earlier versions) and his/her
1045: contributions to the likelihood is 1 - Prob of dying from last
1046: health status (= 1-p13= p11+p12 in the easiest case of somebody in
1047: the healthy state at last known wave). Version is 0.98
1048:
1049: Revision 1.103 2005/09/30 15:54:49 lievre
1050: (Module): sump fixed, loop imx fixed, and simplifications.
1051:
1052: Revision 1.102 2004/09/15 17:31:30 brouard
1053: Add the possibility to read data file including tab characters.
1054:
1055: Revision 1.101 2004/09/15 10:38:38 brouard
1056: Fix on curr_time
1057:
1058: Revision 1.100 2004/07/12 18:29:06 brouard
1059: Add version for Mac OS X. Just define UNIX in Makefile
1060:
1061: Revision 1.99 2004/06/05 08:57:40 brouard
1062: *** empty log message ***
1063:
1064: Revision 1.98 2004/05/16 15:05:56 brouard
1065: New version 0.97 . First attempt to estimate force of mortality
1066: directly from the data i.e. without the need of knowing the health
1067: state at each age, but using a Gompertz model: log u =a + b*age .
1068: This is the basic analysis of mortality and should be done before any
1069: other analysis, in order to test if the mortality estimated from the
1070: cross-longitudinal survey is different from the mortality estimated
1071: from other sources like vital statistic data.
1072:
1073: The same imach parameter file can be used but the option for mle should be -3.
1074:
1.324 brouard 1075: Agnès, who wrote this part of the code, tried to keep most of the
1.126 brouard 1076: former routines in order to include the new code within the former code.
1077:
1078: The output is very simple: only an estimate of the intercept and of
1079: the slope with 95% confident intervals.
1080:
1081: Current limitations:
1082: A) Even if you enter covariates, i.e. with the
1083: model= V1+V2 equation for example, the programm does only estimate a unique global model without covariates.
1084: B) There is no computation of Life Expectancy nor Life Table.
1085:
1086: Revision 1.97 2004/02/20 13:25:42 lievre
1087: Version 0.96d. Population forecasting command line is (temporarily)
1088: suppressed.
1089:
1090: Revision 1.96 2003/07/15 15:38:55 brouard
1091: * imach.c (Repository): Errors in subdirf, 2, 3 while printing tmpout is
1092: rewritten within the same printf. Workaround: many printfs.
1093:
1094: Revision 1.95 2003/07/08 07:54:34 brouard
1095: * imach.c (Repository):
1096: (Repository): Using imachwizard code to output a more meaningful covariance
1097: matrix (cov(a12,c31) instead of numbers.
1098:
1099: Revision 1.94 2003/06/27 13:00:02 brouard
1100: Just cleaning
1101:
1102: Revision 1.93 2003/06/25 16:33:55 brouard
1103: (Module): On windows (cygwin) function asctime_r doesn't
1104: exist so I changed back to asctime which exists.
1105: (Module): Version 0.96b
1106:
1107: Revision 1.92 2003/06/25 16:30:45 brouard
1108: (Module): On windows (cygwin) function asctime_r doesn't
1109: exist so I changed back to asctime which exists.
1110:
1111: Revision 1.91 2003/06/25 15:30:29 brouard
1112: * imach.c (Repository): Duplicated warning errors corrected.
1113: (Repository): Elapsed time after each iteration is now output. It
1114: helps to forecast when convergence will be reached. Elapsed time
1115: is stamped in powell. We created a new html file for the graphs
1116: concerning matrix of covariance. It has extension -cov.htm.
1117:
1118: Revision 1.90 2003/06/24 12:34:15 brouard
1119: (Module): Some bugs corrected for windows. Also, when
1120: mle=-1 a template is output in file "or"mypar.txt with the design
1121: of the covariance matrix to be input.
1122:
1123: Revision 1.89 2003/06/24 12:30:52 brouard
1124: (Module): Some bugs corrected for windows. Also, when
1125: mle=-1 a template is output in file "or"mypar.txt with the design
1126: of the covariance matrix to be input.
1127:
1128: Revision 1.88 2003/06/23 17:54:56 brouard
1129: * 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.
1130:
1131: Revision 1.87 2003/06/18 12:26:01 brouard
1132: Version 0.96
1133:
1134: Revision 1.86 2003/06/17 20:04:08 brouard
1135: (Module): Change position of html and gnuplot routines and added
1136: routine fileappend.
1137:
1138: Revision 1.85 2003/06/17 13:12:43 brouard
1139: * imach.c (Repository): Check when date of death was earlier that
1140: current date of interview. It may happen when the death was just
1141: prior to the death. In this case, dh was negative and likelihood
1142: was wrong (infinity). We still send an "Error" but patch by
1143: assuming that the date of death was just one stepm after the
1144: interview.
1145: (Repository): Because some people have very long ID (first column)
1146: we changed int to long in num[] and we added a new lvector for
1147: memory allocation. But we also truncated to 8 characters (left
1148: truncation)
1149: (Repository): No more line truncation errors.
1150:
1151: Revision 1.84 2003/06/13 21:44:43 brouard
1152: * imach.c (Repository): Replace "freqsummary" at a correct
1153: place. It differs from routine "prevalence" which may be called
1154: many times. Probs is memory consuming and must be used with
1155: parcimony.
1156: Version 0.95a3 (should output exactly the same maximization than 0.8a2)
1157:
1158: Revision 1.83 2003/06/10 13:39:11 lievre
1159: *** empty log message ***
1160:
1161: Revision 1.82 2003/06/05 15:57:20 brouard
1162: Add log in imach.c and fullversion number is now printed.
1163:
1164: */
1165: /*
1166: Interpolated Markov Chain
1167:
1168: Short summary of the programme:
1169:
1.227 brouard 1170: This program computes Healthy Life Expectancies or State-specific
1171: (if states aren't health statuses) Expectancies from
1172: cross-longitudinal data. Cross-longitudinal data consist in:
1173:
1174: -1- a first survey ("cross") where individuals from different ages
1175: are interviewed on their health status or degree of disability (in
1176: the case of a health survey which is our main interest)
1177:
1178: -2- at least a second wave of interviews ("longitudinal") which
1179: measure each change (if any) in individual health status. Health
1180: expectancies are computed from the time spent in each health state
1181: according to a model. More health states you consider, more time is
1182: necessary to reach the Maximum Likelihood of the parameters involved
1183: in the model. The simplest model is the multinomial logistic model
1184: where pij is the probability to be observed in state j at the second
1185: wave conditional to be observed in state i at the first
1186: wave. Therefore the model is: log(pij/pii)= aij + bij*age+ cij*sex +
1187: etc , where 'age' is age and 'sex' is a covariate. If you want to
1188: have a more complex model than "constant and age", you should modify
1189: the program where the markup *Covariates have to be included here
1190: again* invites you to do it. More covariates you add, slower the
1.126 brouard 1191: convergence.
1192:
1193: The advantage of this computer programme, compared to a simple
1194: multinomial logistic model, is clear when the delay between waves is not
1195: identical for each individual. Also, if a individual missed an
1196: intermediate interview, the information is lost, but taken into
1197: account using an interpolation or extrapolation.
1198:
1199: hPijx is the probability to be observed in state i at age x+h
1200: conditional to the observed state i at age x. The delay 'h' can be
1201: split into an exact number (nh*stepm) of unobserved intermediate
1202: states. This elementary transition (by month, quarter,
1203: semester or year) is modelled as a multinomial logistic. The hPx
1204: matrix is simply the matrix product of nh*stepm elementary matrices
1205: and the contribution of each individual to the likelihood is simply
1206: hPijx.
1207:
1208: Also this programme outputs the covariance matrix of the parameters but also
1.218 brouard 1209: of the life expectancies. It also computes the period (stable) prevalence.
1210:
1211: Back prevalence and projections:
1.227 brouard 1212:
1213: - back_prevalence_limit(double *p, double **bprlim, double ageminpar,
1214: double agemaxpar, double ftolpl, int *ncvyearp, double
1215: dateprev1,double dateprev2, int firstpass, int lastpass, int
1216: mobilavproj)
1217:
1218: Computes the back prevalence limit for any combination of
1219: covariate values k at any age between ageminpar and agemaxpar and
1220: returns it in **bprlim. In the loops,
1221:
1222: - **bprevalim(**bprlim, ***mobaverage, nlstate, *p, age, **oldm,
1223: **savm, **dnewm, **doldm, **dsavm, ftolpl, ncvyearp, k);
1224:
1225: - hBijx Back Probability to be in state i at age x-h being in j at x
1.218 brouard 1226: Computes for any combination of covariates k and any age between bage and fage
1227: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
1228: oldm=oldms;savm=savms;
1.227 brouard 1229:
1.267 brouard 1230: - hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k, nres);
1.218 brouard 1231: Computes the transition matrix starting at age 'age' over
1232: 'nhstepm*hstepm*stepm' months (i.e. until
1233: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
1.227 brouard 1234: nhstepm*hstepm matrices.
1235:
1236: Returns p3mat[i][j][h] after calling
1237: p3mat[i][j][h]=matprod2(newm,
1238: bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm,
1239: dsavm,ij),\ 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
1240: oldm);
1.226 brouard 1241:
1242: Important routines
1243:
1244: - func (or funcone), computes logit (pij) distinguishing
1245: o fixed variables (single or product dummies or quantitative);
1246: o varying variables by:
1247: (1) wave (single, product dummies, quantitative),
1248: (2) by age (can be month) age (done), age*age (done), age*Vn where Vn can be:
1249: % fixed dummy (treated) or quantitative (not done because time-consuming);
1250: % varying dummy (not done) or quantitative (not done);
1251: - Tricode which tests the modality of dummy variables (in order to warn with wrong or empty modalities)
1252: and returns the number of efficient covariates cptcoveff and modalities nbcode[Tvar[k]][1]= 0 and nbcode[Tvar[k]][2]= 1 usually.
1253: - printinghtml which outputs results like life expectancy in and from a state for a combination of modalities of dummy variables
1.364 brouard 1254: o There are 2**cptcoveff combinations of (0,1) for cptcoveff variables. Outputting only combinations with people, eliminating 1 1 if
1.226 brouard 1255: race White (0 0), Black vs White (1 0), Hispanic (0 1) and 1 1 being meaningless.
1.218 brouard 1256:
1.226 brouard 1257:
1258:
1.324 brouard 1259: Authors: Nicolas Brouard (brouard@ined.fr) and Agnès Lièvre (lievre@ined.fr).
1260: Institut national d'études démographiques, Paris.
1.126 brouard 1261: This software have been partly granted by Euro-REVES, a concerted action
1262: from the European Union.
1263: It is copyrighted identically to a GNU software product, ie programme and
1264: software can be distributed freely for non commercial use. Latest version
1265: can be accessed at http://euroreves.ined.fr/imach .
1266:
1267: Help to debug: LD_PRELOAD=/usr/local/lib/libnjamd.so ./imach foo.imach
1268: or better on gdb : set env LD_PRELOAD=/usr/local/lib/libnjamd.so
1269:
1270: **********************************************************************/
1271: /*
1272: main
1273: read parameterfile
1274: read datafile
1275: concatwav
1276: freqsummary
1277: if (mle >= 1)
1278: mlikeli
1279: print results files
1280: if mle==1
1281: computes hessian
1282: read end of parameter file: agemin, agemax, bage, fage, estepm
1283: begin-prev-date,...
1284: open gnuplot file
1285: open html file
1.145 brouard 1286: period (stable) prevalence | pl_nom 1-1 2-2 etc by covariate
1287: for age prevalim() | #****** V1=0 V2=1 V3=1 V4=0 ******
1288: | 65 1 0 2 1 3 1 4 0 0.96326 0.03674
1289: freexexit2 possible for memory heap.
1290:
1291: h Pij x | pij_nom ficrestpij
1292: # Cov Agex agex+h hpijx with i,j= 1-1 1-2 1-3 2-1 2-2 2-3
1293: 1 85 85 1.00000 0.00000 0.00000 0.00000 1.00000 0.00000
1294: 1 85 86 0.68299 0.22291 0.09410 0.71093 0.00000 0.28907
1295:
1296: 1 65 99 0.00364 0.00322 0.99314 0.00350 0.00310 0.99340
1297: 1 65 100 0.00214 0.00204 0.99581 0.00206 0.00196 0.99597
1298: variance of p one-step probabilities varprob | prob_nom ficresprob #One-step probabilities and stand. devi in ()
1299: Standard deviation of one-step probabilities | probcor_nom ficresprobcor #One-step probabilities and correlation matrix
1300: Matrix of variance covariance of one-step probabilities | probcov_nom ficresprobcov #One-step probabilities and covariance matrix
1301:
1.126 brouard 1302: forecasting if prevfcast==1 prevforecast call prevalence()
1303: health expectancies
1304: Variance-covariance of DFLE
1305: prevalence()
1306: movingaverage()
1307: varevsij()
1308: if popbased==1 varevsij(,popbased)
1309: total life expectancies
1310: Variance of period (stable) prevalence
1311: end
1312: */
1313:
1.187 brouard 1314: /* #define DEBUG */
1315: /* #define DEBUGBRENT */
1.203 brouard 1316: /* #define DEBUGLINMIN */
1317: /* #define DEBUGHESS */
1318: #define DEBUGHESSIJ
1.224 brouard 1319: /* #define LINMINORIGINAL /\* Don't use loop on scale in linmin (accepting nan) *\/ */
1.165 brouard 1320: #define POWELL /* Instead of NLOPT */
1.224 brouard 1321: #define POWELLNOF3INFF1TEST /* Skip test */
1.186 brouard 1322: /* #define POWELLORIGINAL /\* Don't use Directest to decide new direction but original Powell test *\/ */
1323: /* #define MNBRAKORIGINAL /\* Don't use mnbrak fix *\/ */
1.319 brouard 1324: /* #define FLATSUP *//* Suppresses directions where likelihood is flat */
1.359 brouard 1325: /* #define POWELLORIGINCONJUGATE /\* Don't use conjugate but biggest decrease if valuable *\/ */
1326: /* #define NOTMINFIT */
1.126 brouard 1327:
1328: #include <math.h>
1329: #include <stdio.h>
1330: #include <stdlib.h>
1331: #include <string.h>
1.226 brouard 1332: #include <ctype.h>
1.159 brouard 1333:
1334: #ifdef _WIN32
1335: #include <io.h>
1.172 brouard 1336: #include <windows.h>
1337: #include <tchar.h>
1.159 brouard 1338: #else
1.126 brouard 1339: #include <unistd.h>
1.159 brouard 1340: #endif
1.126 brouard 1341:
1342: #include <limits.h>
1343: #include <sys/types.h>
1.171 brouard 1344:
1345: #if defined(__GNUC__)
1346: #include <sys/utsname.h> /* Doesn't work on Windows */
1347: #endif
1348:
1.126 brouard 1349: #include <sys/stat.h>
1350: #include <errno.h>
1.159 brouard 1351: /* extern int errno; */
1.126 brouard 1352:
1.157 brouard 1353: /* #ifdef LINUX */
1354: /* #include <time.h> */
1355: /* #include "timeval.h" */
1356: /* #else */
1357: /* #include <sys/time.h> */
1358: /* #endif */
1359:
1.126 brouard 1360: #include <time.h>
1361:
1.136 brouard 1362: #ifdef GSL
1363: #include <gsl/gsl_errno.h>
1364: #include <gsl/gsl_multimin.h>
1365: #endif
1366:
1.167 brouard 1367:
1.162 brouard 1368: #ifdef NLOPT
1369: #include <nlopt.h>
1370: typedef struct {
1371: double (* function)(double [] );
1372: } myfunc_data ;
1373: #endif
1374:
1.126 brouard 1375: /* #include <libintl.h> */
1376: /* #define _(String) gettext (String) */
1377:
1.349 brouard 1378: #define MAXLINE 16384 /* Was 256 and 1024 and 2048. Overflow with 312 with 2 states and 4 covariates. Should be ok */
1.126 brouard 1379:
1380: #define GNUPLOTPROGRAM "gnuplot"
1.343 brouard 1381: #define GNUPLOTVERSION 5.1
1382: double gnuplotversion=GNUPLOTVERSION;
1.126 brouard 1383: /*#define GNUPLOTPROGRAM "..\\gp37mgw\\wgnuplot"*/
1.329 brouard 1384: #define FILENAMELENGTH 256
1.126 brouard 1385:
1386: #define GLOCK_ERROR_NOPATH -1 /* empty path */
1387: #define GLOCK_ERROR_GETCWD -2 /* cannot get cwd */
1388:
1.349 brouard 1389: #define MAXPARM 216 /**< Maximum number of parameters for the optimization was 128 */
1.144 brouard 1390: #define NPARMAX 64 /**< (nlstate+ndeath-1)*nlstate*ncovmodel */
1.126 brouard 1391:
1392: #define NINTERVMAX 8
1.144 brouard 1393: #define NLSTATEMAX 8 /**< Maximum number of live states (for func) */
1394: #define NDEATHMAX 8 /**< Maximum number of dead states (for func) */
1.325 brouard 1395: #define NCOVMAX 30 /**< Maximum number of covariates used in the model, including generated covariates V1*V2 or V1*age */
1.197 brouard 1396: #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1.211 brouard 1397: /*#define decodtabm(h,k,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (k-1)) & 1) +1 : -1)*/
1398: #define decodtabm(h,k,cptcoveff) (((h-1) >> (k-1)) & 1) +1
1.290 brouard 1399: /*#define MAXN 20000 */ /* Should by replaced by nobs, real number of observations and unlimited */
1.144 brouard 1400: #define YEARM 12. /**< Number of months per year */
1.218 brouard 1401: /* #define AGESUP 130 */
1.288 brouard 1402: /* #define AGESUP 150 */
1403: #define AGESUP 200
1.268 brouard 1404: #define AGEINF 0
1.218 brouard 1405: #define AGEMARGE 25 /* Marge for agemin and agemax for(iage=agemin-AGEMARGE; iage <= agemax+3+AGEMARGE; iage++) */
1.126 brouard 1406: #define AGEBASE 40
1.194 brouard 1407: #define AGEOVERFLOW 1.e20
1.164 brouard 1408: #define AGEGOMP 10 /**< Minimal age for Gompertz adjustment */
1.157 brouard 1409: #ifdef _WIN32
1410: #define DIRSEPARATOR '\\'
1411: #define CHARSEPARATOR "\\"
1412: #define ODIRSEPARATOR '/'
1413: #else
1.126 brouard 1414: #define DIRSEPARATOR '/'
1415: #define CHARSEPARATOR "/"
1416: #define ODIRSEPARATOR '\\'
1417: #endif
1418:
1.365 ! brouard 1419: /* $Id: imach.c,v 1.364 2024/06/28 12:27:05 brouard Exp $ */
1.126 brouard 1420: /* $State: Exp $ */
1.196 brouard 1421: #include "version.h"
1422: char version[]=__IMACH_VERSION__;
1.360 brouard 1423: char copyright[]="April 2024,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-2024";
1.365 ! brouard 1424: char fullversion[]="$Revision: 1.364 $ $Date: 2024/06/28 12:27:05 $";
1.126 brouard 1425: char strstart[80];
1426: char optionfilext[10], optionfilefiname[FILENAMELENGTH];
1.130 brouard 1427: int erreur=0, nberr=0, nbwarn=0; /* Error number, number of errors number of warnings */
1.342 brouard 1428: int debugILK=0; /* debugILK is set by a #d in a comment line */
1.187 brouard 1429: int nagesqr=0, nforce=0; /* nagesqr=1 if model is including age*age, number of forces */
1.330 brouard 1430: /* Number of covariates model (1)=V2+V1+ V3*age+V2*V4 */
1431: /* Model(2) V1 + V2 + V3 + V8 + V7*V8 + V5*V6 + V8*age + V3*age + age*age */
1.335 brouard 1432: int cptcovn=0; /**< cptcovn decodemodel: number of covariates k of the models excluding age*products =6 and age*age but including products */
1.330 brouard 1433: int cptcovt=0; /**< cptcovt: total number of covariates of the model (2) nbocc(+)+1 = 8 excepting constant and age and age*age */
1.335 brouard 1434: int cptcovs=0; /**< cptcovs number of SIMPLE covariates in the model V2+V1 =2 (dummy or quantit or time varying) */
1435: int cptcovsnq=0; /**< cptcovsnq number of SIMPLE covariates in the model but non quantitative V2+V1 =2 */
1.145 brouard 1436: int cptcovage=0; /**< Number of covariates with age: V3*age only =1 */
1.349 brouard 1437: int cptcovprodage=0; /**< Number of fixed covariates with age: V3*age or V2*V3*age 1 */
1438: int cptcovprodvage=0; /**< Number of varying covariates with age: V7*age or V7*V6*age */
1439: int cptcovdageprod=0; /**< Number of doubleproducts with age, since 0.99r44 only: age*Vn*Vm for gnuplot printing*/
1.145 brouard 1440: int cptcovprodnoage=0; /**< Number of covariate products without age */
1.335 brouard 1441: 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) */
1.233 brouard 1442: int ncovf=0; /* Total number of effective fixed covariates (dummy or quantitative) in the model */
1443: int ncovv=0; /* Total number of effective (wave) varying covariates (dummy or quantitative) in the model */
1.339 brouard 1444: int ncovvt=0; /* Total number of effective (wave) varying covariates (dummy or quantitative or products [without age]) in the model */
1.349 brouard 1445: 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 */
1446: 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 */
1447: int ncova=0; /* Total number of effective (wave and stepm) varying with age covariates (single or product, dummy or quantitative) in the model */
1448: 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 */
1.234 brouard 1449: int nsd=0; /**< Total number of single dummy variables (output) */
1450: int nsq=0; /**< Total number of single quantitative variables (output) */
1.232 brouard 1451: int ncoveff=0; /* Total number of effective fixed dummy covariates in the model */
1.225 brouard 1452: int nqfveff=0; /**< nqfveff Number of Quantitative Fixed Variables Effective */
1.224 brouard 1453: int ntveff=0; /**< ntveff number of effective time varying variables */
1454: int nqtveff=0; /**< ntqveff number of effective time varying quantitative variables */
1.145 brouard 1455: int cptcov=0; /* Working variable */
1.334 brouard 1456: int firstobs=1, lastobs=10; /* nobs = lastobs-firstobs+1 declared globally ;*/
1.290 brouard 1457: int nobs=10; /* Number of observations in the data lastobs-firstobs */
1.218 brouard 1458: int ncovcombmax=NCOVMAX; /* Maximum calculated number of covariate combination = pow(2, cptcoveff) */
1.302 brouard 1459: int npar=NPARMAX; /* Number of parameters (nlstate+ndeath-1)*nlstate*ncovmodel; */
1.126 brouard 1460: int nlstate=2; /* Number of live states */
1461: int ndeath=1; /* Number of dead states */
1.130 brouard 1462: int ncovmodel=0, ncovcol=0; /* Total number of covariables including constant a12*1 +b12*x ncovmodel=2 */
1.339 brouard 1463: int nqv=0, ntv=0, nqtv=0; /* Total number of quantitative variables, time variable (dummy), quantitative and time variable*/
1464: int ncovcolt=0; /* ncovcolt=ncovcol+nqv+ntv+nqtv; total of covariates in the data, not in the model equation*/
1.126 brouard 1465: int popbased=0;
1466:
1467: int *wav; /* Number of waves for this individuual 0 is possible */
1.130 brouard 1468: int maxwav=0; /* Maxim number of waves */
1469: int jmin=0, jmax=0; /* min, max spacing between 2 waves */
1470: int ijmin=0, ijmax=0; /* Individuals having jmin and jmax */
1.359 brouard 1471: int gipmx = 0;
1472: double gsw = 0; /* Global variables on the number of contributions
1.126 brouard 1473: to the likelihood and the sum of weights (done by funcone)*/
1.130 brouard 1474: int mle=1, weightopt=0;
1.126 brouard 1475: int **mw; /* mw[mi][i] is number of the mi wave for this individual */
1476: int **dh; /* dh[mi][i] is number of steps between mi,mi+1 for this individual */
1477: int **bh; /* bh[mi][i] is the bias (+ or -) for this individual if the delay between
1478: * wave mi and wave mi+1 is not an exact multiple of stepm. */
1.162 brouard 1479: int countcallfunc=0; /* Count the number of calls to func */
1.230 brouard 1480: int selected(int kvar); /* Is covariate kvar selected for printing results */
1481:
1.130 brouard 1482: double jmean=1; /* Mean space between 2 waves */
1.145 brouard 1483: double **matprod2(); /* test */
1.126 brouard 1484: double **oldm, **newm, **savm; /* Working pointers to matrices */
1485: double **oldms, **newms, **savms; /* Fixed working pointers to matrices */
1.218 brouard 1486: double **ddnewms, **ddoldms, **ddsavms; /* for freeing later */
1487:
1.136 brouard 1488: /*FILE *fic ; */ /* Used in readdata only */
1.217 brouard 1489: FILE *ficpar, *ficparo,*ficres, *ficresp, *ficresphtm, *ficresphtmfr, *ficrespl, *ficresplb,*ficrespij, *ficrespijb, *ficrest,*ficresf, *ficresfb,*ficrespop;
1.126 brouard 1490: FILE *ficlog, *ficrespow;
1.130 brouard 1491: int globpr=0; /* Global variable for printing or not */
1.126 brouard 1492: double fretone; /* Only one call to likelihood */
1.130 brouard 1493: long ipmx=0; /* Number of contributions */
1.126 brouard 1494: double sw; /* Sum of weights */
1495: char filerespow[FILENAMELENGTH];
1496: char fileresilk[FILENAMELENGTH]; /* File of individual contributions to the likelihood */
1497: FILE *ficresilk;
1498: FILE *ficgp,*ficresprob,*ficpop, *ficresprobcov, *ficresprobcor;
1499: FILE *ficresprobmorprev;
1500: FILE *fichtm, *fichtmcov; /* Html File */
1501: FILE *ficreseij;
1502: char filerese[FILENAMELENGTH];
1503: FILE *ficresstdeij;
1504: char fileresstde[FILENAMELENGTH];
1505: FILE *ficrescveij;
1506: char filerescve[FILENAMELENGTH];
1507: FILE *ficresvij;
1508: char fileresv[FILENAMELENGTH];
1.269 brouard 1509:
1.126 brouard 1510: char title[MAXLINE];
1.234 brouard 1511: char model[MAXLINE]; /**< The model line */
1.217 brouard 1512: char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH], filerespl[FILENAMELENGTH], fileresplb[FILENAMELENGTH];
1.126 brouard 1513: char plotcmd[FILENAMELENGTH], pplotcmd[FILENAMELENGTH];
1514: char tmpout[FILENAMELENGTH], tmpout2[FILENAMELENGTH];
1515: char command[FILENAMELENGTH];
1516: int outcmd=0;
1517:
1.217 brouard 1518: char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filerespijb[FILENAMELENGTH], filereso[FILENAMELENGTH], rfileres[FILENAMELENGTH];
1.202 brouard 1519: char fileresu[FILENAMELENGTH]; /* fileres without r in front */
1.126 brouard 1520: char filelog[FILENAMELENGTH]; /* Log file */
1521: char filerest[FILENAMELENGTH];
1522: char fileregp[FILENAMELENGTH];
1523: char popfile[FILENAMELENGTH];
1524:
1525: char optionfilegnuplot[FILENAMELENGTH], optionfilehtm[FILENAMELENGTH], optionfilehtmcov[FILENAMELENGTH] ;
1526:
1.157 brouard 1527: /* struct timeval start_time, end_time, curr_time, last_time, forecast_time; */
1528: /* struct timezone tzp; */
1529: /* extern int gettimeofday(); */
1530: struct tm tml, *gmtime(), *localtime();
1531:
1532: extern time_t time();
1533:
1534: struct tm start_time, end_time, curr_time, last_time, forecast_time;
1535: time_t rstart_time, rend_time, rcurr_time, rlast_time, rforecast_time; /* raw time */
1.349 brouard 1536: time_t rlast_btime; /* raw time */
1.157 brouard 1537: struct tm tm;
1538:
1.126 brouard 1539: char strcurr[80], strfor[80];
1540:
1541: char *endptr;
1542: long lval;
1543: double dval;
1544:
1.362 brouard 1545: /* This for praxis gegen */
1546: /* int prin=1; */
1547: double h0=0.25;
1548: double macheps;
1549: double ffmin;
1550:
1.126 brouard 1551: #define NR_END 1
1552: #define FREE_ARG char*
1553: #define FTOL 1.0e-10
1554:
1555: #define NRANSI
1.240 brouard 1556: #define ITMAX 200
1557: #define ITPOWMAX 20 /* This is now multiplied by the number of parameters */
1.126 brouard 1558:
1559: #define TOL 2.0e-4
1560:
1561: #define CGOLD 0.3819660
1562: #define ZEPS 1.0e-10
1563: #define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d);
1564:
1565: #define GOLD 1.618034
1566: #define GLIMIT 100.0
1567: #define TINY 1.0e-20
1568:
1569: static double maxarg1,maxarg2;
1570: #define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)>(maxarg2)? (maxarg1):(maxarg2))
1571: #define FMIN(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)<(maxarg2)? (maxarg1):(maxarg2))
1572:
1573: #define SIGN(a,b) ((b)>0.0 ? fabs(a) : -fabs(a))
1574: #define rint(a) floor(a+0.5)
1.166 brouard 1575: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/myutils_8h-source.html */
1.183 brouard 1576: #define mytinydouble 1.0e-16
1.166 brouard 1577: /* #define DEQUAL(a,b) (fabs((a)-(b))<mytinydouble) */
1578: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/mynrutils_8h-source.html */
1579: /* static double dsqrarg; */
1580: /* #define DSQR(a) (DEQUAL((dsqrarg=(a)),0.0) ? 0.0 : dsqrarg*dsqrarg) */
1.126 brouard 1581: static double sqrarg;
1582: #define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 :sqrarg*sqrarg)
1583: #define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;}
1584: int agegomp= AGEGOMP;
1585:
1586: int imx;
1587: int stepm=1;
1588: /* Stepm, step in month: minimum step interpolation*/
1589:
1590: int estepm;
1591: /* Estepm, step in month to interpolate survival function in order to approximate Life Expectancy*/
1592:
1593: int m,nb;
1594: long *num;
1.197 brouard 1595: int firstpass=0, lastpass=4,*cod, *cens;
1.192 brouard 1596: int *ncodemax; /* ncodemax[j]= Number of modalities of the j th
1597: covariate for which somebody answered excluding
1598: undefined. Usually 2: 0 and 1. */
1599: int *ncodemaxwundef; /* ncodemax[j]= Number of modalities of the j th
1600: covariate for which somebody answered including
1601: undefined. Usually 3: -1, 0 and 1. */
1.126 brouard 1602: double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint;
1.218 brouard 1603: double **pmmij, ***probs; /* Global pointer */
1.219 brouard 1604: double ***mobaverage, ***mobaverages; /* New global variable */
1.332 brouard 1605: double **precov; /* New global variable to store for each resultline, values of model covariates given by the resultlines (in order to speed up) */
1.126 brouard 1606: double *ageexmed,*agecens;
1607: double dateintmean=0;
1.296 brouard 1608: double anprojd, mprojd, jprojd; /* For eventual projections */
1609: double anprojf, mprojf, jprojf;
1.126 brouard 1610:
1.296 brouard 1611: double anbackd, mbackd, jbackd; /* For eventual backprojections */
1612: double anbackf, mbackf, jbackf;
1613: double jintmean,mintmean,aintmean;
1.126 brouard 1614: double *weight;
1615: int **s; /* Status */
1.141 brouard 1616: double *agedc;
1.145 brouard 1617: double **covar; /**< covar[j,i], value of jth covariate for individual i,
1.141 brouard 1618: * covar=matrix(0,NCOVMAX,1,n);
1.187 brouard 1619: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*age; */
1.268 brouard 1620: double **coqvar; /* Fixed quantitative covariate nqv */
1.341 brouard 1621: double ***cotvar; /* Time varying covariate start at ncovcol + nqv + (1 to ntv) */
1.225 brouard 1622: double ***cotqvar; /* Time varying quantitative covariate itqv */
1.141 brouard 1623: double idx;
1624: int **nbcode, *Tvar; /**< model=V2 => Tvar[1]= 2 */
1.319 brouard 1625: /* Some documentation */
1626: /* Design original data
1627: * V1 V2 V3 V4 V5 V6 V7 V8 Weight ddb ddth d1st s1 V9 V10 V11 V12 s2 V9 V10 V11 V12
1628: * < ncovcol=6 > nqv=2 (V7 V8) dv dv dv qtv dv dv dvv qtv
1629: * ntv=3 nqtv=1
1.330 brouard 1630: * cptcovn number of covariates (not including constant and age or age*age) = number of plus sign + 1 = 10+1=11
1.319 brouard 1631: * For time varying covariate, quanti or dummies
1632: * cotqvar[wav][iv(1 to nqtv)][i]= [1][12][i]=(V12) quanti
1.341 brouard 1633: * cotvar[wav][ncovcol+nqv+ iv(1 to nqtv)][i]= [(1 to nqtv)][i]=(V12) quanti
1.319 brouard 1634: * cotvar[wav][iv(1 to ntv)][i]= [1][1][i]=(V9) dummies at wav 1
1635: * cotvar[wav][iv(1 to ntv)][i]= [1][2][i]=(V10) dummies at wav 1
1.332 brouard 1636: * covar[Vk,i], value of the Vkth fixed covariate dummy or quanti for individual i:
1.319 brouard 1637: * covar[1][i]= (V1), covar[4][i]=(V4), covar[8][i]=(V8)
1638: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8 + V9 + V9*age + V10
1639: * k= 1 2 3 4 5 6 7 8 9 10 11
1640: */
1641: /* According to the model, more columns can be added to covar by the product of covariates */
1.318 brouard 1642: /* 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
1643: # States 1=Coresidence, 2 Living alone, 3 Institution
1644: # V1=sex, V2=raedyrs Quant Fixed, State=livarnb4..livarnb11, V3=iadl4..iald11, V4=adlw4..adlw11, V5=r4bmi..r11bmi
1645: */
1.349 brouard 1646: /* V5+V4+ V3+V4*V3 +V5*age+V2 +V1*V2+V1*age+V1+V4*V3*age */
1647: /* kmodel 1 2 3 4 5 6 7 8 9 10 */
1648: /*Typevar[k]= 0 0 0 2 1 0 2 1 0 3 *//*0 for simple covariate (dummy, quantitative,*/
1649: /* fixed or varying), 1 for age product, 2 for*/
1650: /* product without age, 3 for age and double product */
1651: /*Dummy[k]= 1 0 0 1 3 1 1 2 0 3 *//*Dummy[k] 0=dummy (0 1), 1 quantitative */
1652: /*(single or product without age), 2 dummy*/
1653: /* with age product, 3 quant with age product*/
1654: /*Tvar[k]= 5 4 3 6 5 2 7 1 1 6 */
1655: /* nsd 1 2 3 */ /* Counting single dummies covar fixed or tv */
1656: /*TnsdVar[Tvar] 1 2 3 */
1657: /*Tvaraff[nsd] 4 3 1 */ /* ID of single dummy cova fixed or timevary*/
1658: /*TvarsD[nsd] 4 3 1 */ /* ID of single dummy cova fixed or timevary*/
1659: /*TvarsDind[nsd] 2 3 9 */ /* position K of single dummy cova */
1660: /* nsq 1 2 */ /* Counting single quantit tv */
1661: /* TvarsQ[k] 5 2 */ /* Number of single quantitative cova */
1662: /* TvarsQind 1 6 */ /* position K of single quantitative cova */
1663: /* Tprod[i]=k 1 2 */ /* Position in model of the ith prod without age */
1664: /* cptcovage 1 2 3 */ /* Counting cov*age in the model equation */
1665: /* Tage[cptcovage]=k 5 8 10 */ /* Position in the model of ith cov*age */
1.350 brouard 1666: /* model="V2+V3+V4+V6+V7+V6*V2+V7*V2+V6*V3+V7*V3+V6*V4+V7*V4+age*V2+age*V3+age*V4+age*V6+age*V7+age*V6*V2+age*V6*V3+age*V7*V3+age*V6*V4+age*V7*V4\r"*/
1667: /* p Tvard[1][1]@21 = {6, 2, 7, 2, 6, 3, 7, 3, 6, 4, 7, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0}*/
1.354 brouard 1668: /* p Tvard[2][1]@21 = {7, 2, 6, 3, 7, 3, 6, 4, 7, 4, 0 <repeats 11 times>} */
1.350 brouard 1669: /* p Tvardk[1][1]@24 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 2, 7, 2, 6, 3, 7, 3, 6, 4, 7, 4, 0, 0}*/
1670: /* p Tvardk[1][1]@22 = {0, 0, 0, 0, 0, 0, 0, 0, 6, 2, 7, 2, 6, 3, 7, 3, 6, 4, 7, 4, 0, 0} */
1.349 brouard 1671: /* Tvard[1][1]@4={4,3,1,2} V4*V3 V1*V2 */ /* Position in model of the ith prod without age */
1.330 brouard 1672: /* 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*/
1.319 brouard 1673: /* 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 */
1.320 brouard 1674: /* TvarFind; TvarFind[1]=6, TvarFind[2]=7, TvarFind[3]=9 *//* Inverse V2(6) is first fixed (single or prod) */
1.234 brouard 1675: /* Type */
1676: /* V 1 2 3 4 5 */
1677: /* F F V V V */
1678: /* D Q D D Q */
1679: /* */
1680: int *TvarsD;
1.330 brouard 1681: int *TnsdVar;
1.234 brouard 1682: int *TvarsDind;
1683: int *TvarsQ;
1684: int *TvarsQind;
1685:
1.318 brouard 1686: #define MAXRESULTLINESPONE 10+1
1.235 brouard 1687: int nresult=0;
1.258 brouard 1688: int parameterline=0; /* # of the parameter (type) line */
1.334 brouard 1689: int TKresult[MAXRESULTLINESPONE]; /* TKresult[nres]=k for each resultline nres give the corresponding combination of dummies */
1690: int resultmodel[MAXRESULTLINESPONE][NCOVMAX];/* resultmodel[k1]=k3: k1th position in the model corresponds to the k3 position in the resultline */
1691: int modelresult[MAXRESULTLINESPONE][NCOVMAX];/* modelresult[k3]=k1: k1th position in the model corresponds to the k3 position in the resultline */
1692: int Tresult[MAXRESULTLINESPONE][NCOVMAX];/* Tresult[nres][result_position]= value of the dummy variable at the result_position in the nres resultline */
1.332 brouard 1693: int Tinvresult[MAXRESULTLINESPONE][NCOVMAX];/* Tinvresult[nres][Name of a dummy variable]= value of the variable in the result line */
1694: double TinvDoQresult[MAXRESULTLINESPONE][NCOVMAX];/* TinvDoQresult[nres][Name of a Dummy or Q variable]= value of the variable in the result line */
1.334 brouard 1695: int Tvresult[MAXRESULTLINESPONE][NCOVMAX]; /* Tvresult[nres][result_position]= name of the dummy variable at the result_position in the nres resultline */
1.332 brouard 1696: double Tqresult[MAXRESULTLINESPONE][NCOVMAX]; /* Tqresult[nres][result_position]= value of the variable at the result_position in the nres resultline */
1.318 brouard 1697: double Tqinvresult[MAXRESULTLINESPONE][NCOVMAX]; /* For quantitative variable , value (output) */
1.332 brouard 1698: int Tvqresult[MAXRESULTLINESPONE][NCOVMAX]; /* Tvqresult[nres][result_position]= id of the variable at the result_position in the nres resultline */
1.318 brouard 1699:
1700: /* 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
1701: # States 1=Coresidence, 2 Living alone, 3 Institution
1702: # V1=sex, V2=raedyrs Quant Fixed, State=livarnb4..livarnb11, V3=iadl4..iald11, V4=adlw4..adlw11, V5=r4bmi..r11bmi
1703: */
1.234 brouard 1704: /* int *TDvar; /\**< TDvar[1]=4, TDvarF[2]=3, TDvar[3]=6 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 *\/ */
1.232 brouard 1705: 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 */
1706: 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 */
1707: 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 */
1708: int *TvarVind; /**< TvarVind[1]=1, TvarVind[2]=2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1709: 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 */
1710: int *TvarAind; /**< TvarindA[1]=5, TvarAind[2]=8 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1.231 brouard 1711: int *TvarFD; /**< TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1712: int *TvarFDind; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1713: int *TvarFQ; /* TvarFQ[1]=V2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1714: int *TvarFQind; /* TvarFQind[1]=6 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1715: int *TvarVD; /* TvarVD[1]=V5 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1716: int *TvarVDind; /* TvarVDind[1]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1717: 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 */
1718: 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 */
1.339 brouard 1719: int *TvarVV; /* We count ncovvt time varying covariates (single or products without age) and put their name into TvarVV */
1720: int *TvarVVind; /* We count ncovvt time varying covariates (single or products without age) and put their name into TvarVV */
1.349 brouard 1721: int *TvarVVA; /* We count ncovvt time varying covariates (single or products with age) and put their name into TvarVVA */
1722: int *TvarVVAind; /* We count ncovvt time varying covariates (single or products without age) and put their name into TvarVV */
1723: int *TvarAVVA; /* We count ALL ncovta time varying covariates (single or products with age) and put their name into TvarVVA */
1724: int *TvarAVVAind; /* We count ALL ncovta time varying covariates (single or products without age) and put their name into TvarVV */
1.339 brouard 1725: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
1.349 brouard 1726: /* model V1+V3+age*V1+age*V3+V1*V3 + V1*V3*age */
1727: /* Tvar={1, 3, 1, 3, 6, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
1728: /* TvarVV={3,1,3,1,3}, for V3 and then the product V1*V3 is decomposed into V1 and V3 */
1729: /* 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 */
1.230 brouard 1730: int *Tvarsel; /**< Selected covariates for output */
1731: double *Tvalsel; /**< Selected modality value of covariate for output */
1.349 brouard 1732: int *Typevar; /**< 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product, 3 age*Vn*Vm */
1.227 brouard 1733: int *Fixed; /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
1734: int *Dummy; /** Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product */
1.238 brouard 1735: int *DummyV; /** Dummy[v] 0=dummy (0 1), 1 quantitative */
1736: int *FixedV; /** FixedV[v] 0 fixed, 1 varying */
1.197 brouard 1737: int *Tage;
1.227 brouard 1738: int anyvaryingduminmodel=0; /**< Any varying dummy in Model=1 yes, 0 no, to avoid a loop on waves in freq */
1.228 brouard 1739: int *Tmodelind; /** Tmodelind[Tvaraff[3]]=9 for V1 position,Tvaraff[1]@9={4, 3, 1, 0, 0, 0, 0, 0, 0}, model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1*/
1.230 brouard 1740: 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*/
1741: int *TmodelInvQind; /** Tmodelqind[1]=1 for V5(quantitative varying) position,Tvaraff[1]@9={4, 3, 1, 0, 0, 0, 0, 0, 0}, model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1.145 brouard 1742: int *Ndum; /** Freq of modality (tricode */
1.200 brouard 1743: /* int **codtab;*/ /**< codtab=imatrix(1,100,1,10); */
1.227 brouard 1744: int **Tvard;
1.330 brouard 1745: int **Tvardk;
1.227 brouard 1746: int *Tprod;/**< Gives the k position of the k1 product */
1.238 brouard 1747: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3 */
1.227 brouard 1748: int *Tposprod; /**< Gives the k1 product from the k position */
1.238 brouard 1749: /* if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2) */
1750: /* Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5(V3*V2)]=2 (2nd product without age) */
1.227 brouard 1751: int cptcovprod, *Tvaraff, *invalidvarcomb;
1.126 brouard 1752: double *lsurv, *lpop, *tpop;
1753:
1.231 brouard 1754: #define FD 1; /* Fixed dummy covariate */
1755: #define FQ 2; /* Fixed quantitative covariate */
1756: #define FP 3; /* Fixed product covariate */
1757: #define FPDD 7; /* Fixed product dummy*dummy covariate */
1758: #define FPDQ 8; /* Fixed product dummy*quantitative covariate */
1759: #define FPQQ 9; /* Fixed product quantitative*quantitative covariate */
1760: #define VD 10; /* Varying dummy covariate */
1761: #define VQ 11; /* Varying quantitative covariate */
1762: #define VP 12; /* Varying product covariate */
1763: #define VPDD 13; /* Varying product dummy*dummy covariate */
1764: #define VPDQ 14; /* Varying product dummy*quantitative covariate */
1765: #define VPQQ 15; /* Varying product quantitative*quantitative covariate */
1766: #define APFD 16; /* Age product * fixed dummy covariate */
1767: #define APFQ 17; /* Age product * fixed quantitative covariate */
1768: #define APVD 18; /* Age product * varying dummy covariate */
1769: #define APVQ 19; /* Age product * varying quantitative covariate */
1770:
1771: #define FTYPE 1; /* Fixed covariate */
1772: #define VTYPE 2; /* Varying covariate (loop in wave) */
1773: #define ATYPE 2; /* Age product covariate (loop in dh within wave)*/
1774:
1775: struct kmodel{
1776: int maintype; /* main type */
1777: int subtype; /* subtype */
1778: };
1779: struct kmodel modell[NCOVMAX];
1780:
1.143 brouard 1781: double ftol=FTOL; /**< Tolerance for computing Max Likelihood */
1782: double ftolhess; /**< Tolerance for computing hessian */
1.126 brouard 1783:
1784: /**************** split *************************/
1785: static int split( char *path, char *dirc, char *name, char *ext, char *finame )
1786: {
1787: /* From a file name with (full) path (either Unix or Windows) we extract the directory (dirc)
1788: the name of the file (name), its extension only (ext) and its first part of the name (finame)
1789: */
1790: char *ss; /* pointer */
1.186 brouard 1791: int l1=0, l2=0; /* length counters */
1.126 brouard 1792:
1793: l1 = strlen(path ); /* length of path */
1794: if ( l1 == 0 ) return( GLOCK_ERROR_NOPATH );
1795: ss= strrchr( path, DIRSEPARATOR ); /* find last / */
1796: if ( ss == NULL ) { /* no directory, so determine current directory */
1797: strcpy( name, path ); /* we got the fullname name because no directory */
1798: /*if(strrchr(path, ODIRSEPARATOR )==NULL)
1799: printf("Warning you should use %s as a separator\n",DIRSEPARATOR);*/
1800: /* get current working directory */
1801: /* extern char* getcwd ( char *buf , int len);*/
1.184 brouard 1802: #ifdef WIN32
1803: if (_getcwd( dirc, FILENAME_MAX ) == NULL ) {
1804: #else
1805: if (getcwd(dirc, FILENAME_MAX) == NULL) {
1806: #endif
1.126 brouard 1807: return( GLOCK_ERROR_GETCWD );
1808: }
1809: /* got dirc from getcwd*/
1810: printf(" DIRC = %s \n",dirc);
1.205 brouard 1811: } else { /* strip directory from path */
1.126 brouard 1812: ss++; /* after this, the filename */
1813: l2 = strlen( ss ); /* length of filename */
1814: if ( l2 == 0 ) return( GLOCK_ERROR_NOPATH );
1815: strcpy( name, ss ); /* save file name */
1816: strncpy( dirc, path, l1 - l2 ); /* now the directory */
1.186 brouard 1817: dirc[l1-l2] = '\0'; /* add zero */
1.126 brouard 1818: printf(" DIRC2 = %s \n",dirc);
1819: }
1820: /* We add a separator at the end of dirc if not exists */
1821: l1 = strlen( dirc ); /* length of directory */
1822: if( dirc[l1-1] != DIRSEPARATOR ){
1823: dirc[l1] = DIRSEPARATOR;
1824: dirc[l1+1] = 0;
1825: printf(" DIRC3 = %s \n",dirc);
1826: }
1827: ss = strrchr( name, '.' ); /* find last / */
1828: if (ss >0){
1829: ss++;
1830: strcpy(ext,ss); /* save extension */
1831: l1= strlen( name);
1832: l2= strlen(ss)+1;
1833: strncpy( finame, name, l1-l2);
1834: finame[l1-l2]= 0;
1835: }
1836:
1837: return( 0 ); /* we're done */
1838: }
1839:
1840:
1841: /******************************************/
1842:
1843: void replace_back_to_slash(char *s, char*t)
1844: {
1845: int i;
1846: int lg=0;
1847: i=0;
1848: lg=strlen(t);
1849: for(i=0; i<= lg; i++) {
1850: (s[i] = t[i]);
1851: if (t[i]== '\\') s[i]='/';
1852: }
1853: }
1854:
1.132 brouard 1855: char *trimbb(char *out, char *in)
1.137 brouard 1856: { /* Trim multiple blanks in line but keeps first blanks if line starts with blanks */
1.132 brouard 1857: char *s;
1858: s=out;
1859: while (*in != '\0'){
1.137 brouard 1860: while( *in == ' ' && *(in+1) == ' '){ /* && *(in+1) != '\0'){*/
1.132 brouard 1861: in++;
1862: }
1863: *out++ = *in++;
1864: }
1865: *out='\0';
1866: return s;
1867: }
1868:
1.351 brouard 1869: char *trimbtab(char *out, char *in)
1870: { /* Trim blanks or tabs in line but keeps first blanks if line starts with blanks */
1871: char *s;
1872: s=out;
1873: while (*in != '\0'){
1874: while( (*in == ' ' || *in == '\t')){ /* && *(in+1) != '\0'){*/
1875: in++;
1876: }
1877: *out++ = *in++;
1878: }
1879: *out='\0';
1880: return s;
1881: }
1882:
1.187 brouard 1883: /* char *substrchaine(char *out, char *in, char *chain) */
1884: /* { */
1885: /* /\* Substract chain 'chain' from 'in', return and output 'out' *\/ */
1886: /* char *s, *t; */
1887: /* t=in;s=out; */
1888: /* while ((*in != *chain) && (*in != '\0')){ */
1889: /* *out++ = *in++; */
1890: /* } */
1891:
1892: /* /\* *in matches *chain *\/ */
1893: /* while ((*in++ == *chain++) && (*in != '\0')){ */
1894: /* printf("*in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1895: /* } */
1896: /* in--; chain--; */
1897: /* while ( (*in != '\0')){ */
1898: /* printf("Bef *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1899: /* *out++ = *in++; */
1900: /* printf("Aft *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1901: /* } */
1902: /* *out='\0'; */
1903: /* out=s; */
1904: /* return out; */
1905: /* } */
1906: char *substrchaine(char *out, char *in, char *chain)
1907: {
1908: /* Substract chain 'chain' from 'in', return and output 'out' */
1.349 brouard 1909: /* in="V1+V1*age+age*age+V2", chain="+age*age" out="V1+V1*age+V2" */
1.187 brouard 1910:
1911: char *strloc;
1912:
1.349 brouard 1913: strcpy (out, in); /* out="V1+V1*age+age*age+V2" */
1914: strloc = strstr(out, chain); /* strloc points to out at "+age*age+V2" */
1915: 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" */
1.187 brouard 1916: if(strloc != NULL){
1.349 brouard 1917: /* will affect out */ /* strloc+strlen(chain)=|+V2 = "V1+V1*age+age*age|+V2" */ /* Will also work in Unicodek */
1918: 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)*/
1919: /* equivalent to strcpy (strloc, strloc +strlen(chain)) if no overlap; Copies from "+V2" to V1+V1*age+ */
1.187 brouard 1920: }
1.349 brouard 1921: 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" */
1.187 brouard 1922: return out;
1923: }
1924:
1925:
1.145 brouard 1926: char *cutl(char *blocc, char *alocc, char *in, char occ)
1927: {
1.187 brouard 1928: /* cuts string in into blocc and alocc where blocc ends before FIRST occurence of char 'occ'
1.349 brouard 1929: and alocc starts after first occurence of char 'occ' : ex cutl(blocc,alocc,"abcdef2ghi2j",'2')
1.310 brouard 1930: gives alocc="abcdef" and blocc="ghi2j".
1.145 brouard 1931: If occ is not found blocc is null and alocc is equal to in. Returns blocc
1932: */
1.160 brouard 1933: char *s, *t;
1.145 brouard 1934: t=in;s=in;
1935: while ((*in != occ) && (*in != '\0')){
1936: *alocc++ = *in++;
1937: }
1938: if( *in == occ){
1939: *(alocc)='\0';
1940: s=++in;
1941: }
1942:
1943: if (s == t) {/* occ not found */
1944: *(alocc-(in-s))='\0';
1945: in=s;
1946: }
1947: while ( *in != '\0'){
1948: *blocc++ = *in++;
1949: }
1950:
1951: *blocc='\0';
1952: return t;
1953: }
1.137 brouard 1954: char *cutv(char *blocc, char *alocc, char *in, char occ)
1955: {
1.187 brouard 1956: /* cuts string in into blocc and alocc where blocc ends before LAST occurence of char 'occ'
1.137 brouard 1957: and alocc starts after last occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
1958: gives blocc="abcdef2ghi" and alocc="j".
1959: If occ is not found blocc is null and alocc is equal to in. Returns alocc
1960: */
1961: char *s, *t;
1962: t=in;s=in;
1963: while (*in != '\0'){
1964: while( *in == occ){
1965: *blocc++ = *in++;
1966: s=in;
1967: }
1968: *blocc++ = *in++;
1969: }
1970: if (s == t) /* occ not found */
1971: *(blocc-(in-s))='\0';
1972: else
1973: *(blocc-(in-s)-1)='\0';
1974: in=s;
1975: while ( *in != '\0'){
1976: *alocc++ = *in++;
1977: }
1978:
1979: *alocc='\0';
1980: return s;
1981: }
1982:
1.126 brouard 1983: int nbocc(char *s, char occ)
1984: {
1985: int i,j=0;
1986: int lg=20;
1987: i=0;
1988: lg=strlen(s);
1989: for(i=0; i<= lg; i++) {
1.234 brouard 1990: if (s[i] == occ ) j++;
1.126 brouard 1991: }
1992: return j;
1993: }
1994:
1.349 brouard 1995: int nboccstr(char *textin, char *chain)
1996: {
1997: /* Counts the number of occurence of "chain" in string textin */
1998: /* in="+V7*V4+age*V2+age*V3+age*V4" chain="age" */
1999: char *strloc;
2000:
2001: int i,j=0;
2002:
2003: i=0;
2004:
2005: strloc=textin; /* strloc points to "^+V7*V4+age+..." in textin */
2006: for(;;) {
2007: strloc= strstr(strloc,chain); /* strloc points to first character of chain in textin if found. Example strloc points^ to "+V7*V4+^age" in textin */
2008: if(strloc != NULL){
2009: strloc = strloc+strlen(chain); /* strloc points to "+V7*V4+age^" in textin */
2010: j++;
2011: }else
2012: break;
2013: }
2014: return j;
2015:
2016: }
1.137 brouard 2017: /* void cutv(char *u,char *v, char*t, char occ) */
2018: /* { */
2019: /* /\* cuts string t into u and v where u ends before last occurence of char 'occ' */
2020: /* and v starts after last occurence of char 'occ' : ex cutv(u,v,"abcdef2ghi2j",'2') */
2021: /* gives u="abcdef2ghi" and v="j" *\/ */
2022: /* int i,lg,j,p=0; */
2023: /* i=0; */
2024: /* lg=strlen(t); */
2025: /* for(j=0; j<=lg-1; j++) { */
2026: /* if((t[j]!= occ) && (t[j+1]== occ)) p=j+1; */
2027: /* } */
1.126 brouard 2028:
1.137 brouard 2029: /* for(j=0; j<p; j++) { */
2030: /* (u[j] = t[j]); */
2031: /* } */
2032: /* u[p]='\0'; */
1.126 brouard 2033:
1.137 brouard 2034: /* for(j=0; j<= lg; j++) { */
2035: /* if (j>=(p+1))(v[j-p-1] = t[j]); */
2036: /* } */
2037: /* } */
1.126 brouard 2038:
1.160 brouard 2039: #ifdef _WIN32
2040: char * strsep(char **pp, const char *delim)
2041: {
2042: char *p, *q;
2043:
2044: if ((p = *pp) == NULL)
2045: return 0;
2046: if ((q = strpbrk (p, delim)) != NULL)
2047: {
2048: *pp = q + 1;
2049: *q = '\0';
2050: }
2051: else
2052: *pp = 0;
2053: return p;
2054: }
2055: #endif
2056:
1.126 brouard 2057: /********************** nrerror ********************/
2058:
2059: void nrerror(char error_text[])
2060: {
2061: fprintf(stderr,"ERREUR ...\n");
2062: fprintf(stderr,"%s\n",error_text);
2063: exit(EXIT_FAILURE);
2064: }
2065: /*********************** vector *******************/
2066: double *vector(int nl, int nh)
2067: {
2068: double *v;
2069: v=(double *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(double)));
2070: if (!v) nrerror("allocation failure in vector");
2071: return v-nl+NR_END;
2072: }
2073:
2074: /************************ free vector ******************/
2075: void free_vector(double*v, int nl, int nh)
2076: {
2077: free((FREE_ARG)(v+nl-NR_END));
2078: }
2079:
2080: /************************ivector *******************************/
2081: int *ivector(long nl,long nh)
2082: {
2083: int *v;
2084: v=(int *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(int)));
2085: if (!v) nrerror("allocation failure in ivector");
2086: return v-nl+NR_END;
2087: }
2088:
2089: /******************free ivector **************************/
2090: void free_ivector(int *v, long nl, long nh)
2091: {
2092: free((FREE_ARG)(v+nl-NR_END));
2093: }
2094:
2095: /************************lvector *******************************/
2096: long *lvector(long nl,long nh)
2097: {
2098: long *v;
2099: v=(long *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(long)));
2100: if (!v) nrerror("allocation failure in ivector");
2101: return v-nl+NR_END;
2102: }
2103:
2104: /******************free lvector **************************/
2105: void free_lvector(long *v, long nl, long nh)
2106: {
2107: free((FREE_ARG)(v+nl-NR_END));
2108: }
2109:
2110: /******************* imatrix *******************************/
2111: int **imatrix(long nrl, long nrh, long ncl, long nch)
2112: /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
2113: {
2114: long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
2115: int **m;
2116:
2117: /* allocate pointers to rows */
2118: m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
2119: if (!m) nrerror("allocation failure 1 in matrix()");
2120: m += NR_END;
2121: m -= nrl;
2122:
2123:
2124: /* allocate rows and set pointers to them */
2125: m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
2126: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
2127: m[nrl] += NR_END;
2128: m[nrl] -= ncl;
2129:
2130: for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
2131:
2132: /* return pointer to array of pointers to rows */
2133: return m;
2134: }
2135:
2136: /****************** free_imatrix *************************/
2137: void free_imatrix(m,nrl,nrh,ncl,nch)
2138: int **m;
2139: long nch,ncl,nrh,nrl;
2140: /* free an int matrix allocated by imatrix() */
2141: {
2142: free((FREE_ARG) (m[nrl]+ncl-NR_END));
2143: free((FREE_ARG) (m+nrl-NR_END));
2144: }
2145:
2146: /******************* matrix *******************************/
2147: double **matrix(long nrl, long nrh, long ncl, long nch)
2148: {
2149: long i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
2150: double **m;
2151:
2152: m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
2153: if (!m) nrerror("allocation failure 1 in matrix()");
2154: m += NR_END;
2155: m -= nrl;
2156:
2157: m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
2158: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
2159: m[nrl] += NR_END;
2160: m[nrl] -= ncl;
2161:
2162: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
2163: return m;
1.145 brouard 2164: /* print *(*(m+1)+70) or print m[1][70]; print m+1 or print &(m[1]) or &(m[1][0])
2165: m[i] = address of ith row of the table. &(m[i]) is its value which is another adress
2166: that of m[i][0]. In order to get the value p m[i][0] but it is unitialized.
1.126 brouard 2167: */
2168: }
2169:
2170: /*************************free matrix ************************/
2171: void free_matrix(double **m, long nrl, long nrh, long ncl, long nch)
2172: {
2173: free((FREE_ARG)(m[nrl]+ncl-NR_END));
2174: free((FREE_ARG)(m+nrl-NR_END));
2175: }
2176:
2177: /******************* ma3x *******************************/
2178: double ***ma3x(long nrl, long nrh, long ncl, long nch, long nll, long nlh)
2179: {
2180: long i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1, nlay=nlh-nll+1;
2181: double ***m;
2182:
2183: m=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
2184: if (!m) nrerror("allocation failure 1 in matrix()");
2185: m += NR_END;
2186: m -= nrl;
2187:
2188: m[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
2189: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
2190: m[nrl] += NR_END;
2191: m[nrl] -= ncl;
2192:
2193: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
2194:
2195: m[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*nlay+NR_END)*sizeof(double)));
2196: if (!m[nrl][ncl]) nrerror("allocation failure 3 in matrix()");
2197: m[nrl][ncl] += NR_END;
2198: m[nrl][ncl] -= nll;
2199: for (j=ncl+1; j<=nch; j++)
2200: m[nrl][j]=m[nrl][j-1]+nlay;
2201:
2202: for (i=nrl+1; i<=nrh; i++) {
2203: m[i][ncl]=m[i-1l][ncl]+ncol*nlay;
2204: for (j=ncl+1; j<=nch; j++)
2205: m[i][j]=m[i][j-1]+nlay;
2206: }
2207: return m;
2208: /* gdb: p *(m+1) <=> p m[1] and p (m+1) <=> p (m+1) <=> p &(m[1])
2209: &(m[i][j][k]) <=> *((*(m+i) + j)+k)
2210: */
2211: }
2212:
2213: /*************************free ma3x ************************/
2214: void free_ma3x(double ***m, long nrl, long nrh, long ncl, long nch,long nll, long nlh)
2215: {
2216: free((FREE_ARG)(m[nrl][ncl]+ nll-NR_END));
2217: free((FREE_ARG)(m[nrl]+ncl-NR_END));
2218: free((FREE_ARG)(m+nrl-NR_END));
2219: }
2220:
2221: /*************** function subdirf ***********/
2222: char *subdirf(char fileres[])
2223: {
2224: /* Caution optionfilefiname is hidden */
2225: strcpy(tmpout,optionfilefiname);
2226: strcat(tmpout,"/"); /* Add to the right */
2227: strcat(tmpout,fileres);
2228: return tmpout;
2229: }
2230:
2231: /*************** function subdirf2 ***********/
2232: char *subdirf2(char fileres[], char *preop)
2233: {
1.314 brouard 2234: /* Example subdirf2(optionfilefiname,"FB_") with optionfilefiname="texte", result="texte/FB_texte"
2235: Errors in subdirf, 2, 3 while printing tmpout is
1.315 brouard 2236: rewritten within the same printf. Workaround: many printfs */
1.126 brouard 2237: /* Caution optionfilefiname is hidden */
2238: strcpy(tmpout,optionfilefiname);
2239: strcat(tmpout,"/");
2240: strcat(tmpout,preop);
2241: strcat(tmpout,fileres);
2242: return tmpout;
2243: }
2244:
2245: /*************** function subdirf3 ***********/
2246: char *subdirf3(char fileres[], char *preop, char *preop2)
2247: {
2248:
2249: /* Caution optionfilefiname is hidden */
2250: strcpy(tmpout,optionfilefiname);
2251: strcat(tmpout,"/");
2252: strcat(tmpout,preop);
2253: strcat(tmpout,preop2);
2254: strcat(tmpout,fileres);
2255: return tmpout;
2256: }
1.213 brouard 2257:
2258: /*************** function subdirfext ***********/
2259: char *subdirfext(char fileres[], char *preop, char *postop)
2260: {
2261:
2262: strcpy(tmpout,preop);
2263: strcat(tmpout,fileres);
2264: strcat(tmpout,postop);
2265: return tmpout;
2266: }
1.126 brouard 2267:
1.213 brouard 2268: /*************** function subdirfext3 ***********/
2269: char *subdirfext3(char fileres[], char *preop, char *postop)
2270: {
2271:
2272: /* Caution optionfilefiname is hidden */
2273: strcpy(tmpout,optionfilefiname);
2274: strcat(tmpout,"/");
2275: strcat(tmpout,preop);
2276: strcat(tmpout,fileres);
2277: strcat(tmpout,postop);
2278: return tmpout;
2279: }
2280:
1.162 brouard 2281: char *asc_diff_time(long time_sec, char ascdiff[])
2282: {
2283: long sec_left, days, hours, minutes;
2284: days = (time_sec) / (60*60*24);
2285: sec_left = (time_sec) % (60*60*24);
2286: hours = (sec_left) / (60*60) ;
2287: sec_left = (sec_left) %(60*60);
2288: minutes = (sec_left) /60;
2289: sec_left = (sec_left) % (60);
2290: sprintf(ascdiff,"%ld day(s) %ld hour(s) %ld minute(s) %ld second(s)",days, hours, minutes, sec_left);
2291: return ascdiff;
2292: }
2293:
1.126 brouard 2294: /***************** f1dim *************************/
2295: extern int ncom;
2296: extern double *pcom,*xicom;
2297: extern double (*nrfunc)(double []);
2298:
2299: double f1dim(double x)
2300: {
2301: int j;
2302: double f;
2303: double *xt;
2304:
2305: xt=vector(1,ncom);
2306: for (j=1;j<=ncom;j++) xt[j]=pcom[j]+x*xicom[j];
2307: f=(*nrfunc)(xt);
2308: free_vector(xt,1,ncom);
2309: return f;
2310: }
2311:
2312: /*****************brent *************************/
2313: double brent(double ax, double bx, double cx, double (*f)(double), double tol, double *xmin)
1.187 brouard 2314: {
2315: /* Given a function f, and given a bracketing triplet of abscissas ax, bx, cx (such that bx is
2316: * between ax and cx, and f(bx) is less than both f(ax) and f(cx) ), this routine isolates
2317: * the minimum to a fractional precision of about tol using Brent’s method. The abscissa of
2318: * the minimum is returned as xmin, and the minimum function value is returned as brent , the
2319: * returned function value.
2320: */
1.126 brouard 2321: int iter;
2322: double a,b,d,etemp;
1.159 brouard 2323: double fu=0,fv,fw,fx;
1.164 brouard 2324: double ftemp=0.;
1.126 brouard 2325: double p,q,r,tol1,tol2,u,v,w,x,xm;
2326: double e=0.0;
2327:
2328: a=(ax < cx ? ax : cx);
2329: b=(ax > cx ? ax : cx);
2330: x=w=v=bx;
2331: fw=fv=fx=(*f)(x);
2332: for (iter=1;iter<=ITMAX;iter++) {
2333: xm=0.5*(a+b);
2334: tol2=2.0*(tol1=tol*fabs(x)+ZEPS);
2335: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret)))*/
2336: printf(".");fflush(stdout);
2337: fprintf(ficlog,".");fflush(ficlog);
1.162 brouard 2338: #ifdef DEBUGBRENT
1.126 brouard 2339: 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);
2340: 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);
2341: /* if ((fabs(x-xm) <= (tol2-0.5*(b-a)))||(2.0*fabs(fu-ftemp) <= ftol*1.e-2*(fabs(fu)+fabs(ftemp)))) { */
2342: #endif
2343: if (fabs(x-xm) <= (tol2-0.5*(b-a))){
2344: *xmin=x;
2345: return fx;
2346: }
2347: ftemp=fu;
2348: if (fabs(e) > tol1) {
2349: r=(x-w)*(fx-fv);
2350: q=(x-v)*(fx-fw);
2351: p=(x-v)*q-(x-w)*r;
2352: q=2.0*(q-r);
2353: if (q > 0.0) p = -p;
2354: q=fabs(q);
2355: etemp=e;
2356: e=d;
2357: if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x))
1.224 brouard 2358: d=CGOLD*(e=(x >= xm ? a-x : b-x));
1.126 brouard 2359: else {
1.224 brouard 2360: d=p/q;
2361: u=x+d;
2362: if (u-a < tol2 || b-u < tol2)
2363: d=SIGN(tol1,xm-x);
1.126 brouard 2364: }
2365: } else {
2366: d=CGOLD*(e=(x >= xm ? a-x : b-x));
2367: }
2368: u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d));
2369: fu=(*f)(u);
2370: if (fu <= fx) {
2371: if (u >= x) a=x; else b=x;
2372: SHFT(v,w,x,u)
1.183 brouard 2373: SHFT(fv,fw,fx,fu)
2374: } else {
2375: if (u < x) a=u; else b=u;
2376: if (fu <= fw || w == x) {
1.224 brouard 2377: v=w;
2378: w=u;
2379: fv=fw;
2380: fw=fu;
1.183 brouard 2381: } else if (fu <= fv || v == x || v == w) {
1.224 brouard 2382: v=u;
2383: fv=fu;
1.183 brouard 2384: }
2385: }
1.126 brouard 2386: }
2387: nrerror("Too many iterations in brent");
2388: *xmin=x;
2389: return fx;
2390: }
2391:
2392: /****************** mnbrak ***********************/
2393:
2394: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, double *fc,
2395: double (*func)(double))
1.183 brouard 2396: { /* Given a function func , and given distinct initial points ax and bx , this routine searches in
2397: the downhill direction (defined by the function as evaluated at the initial points) and returns
2398: new points ax , bx , cx that bracket a minimum of the function. Also returned are the function
2399: values at the three points, fa, fb , and fc such that fa > fb and fb < fc.
2400: */
1.126 brouard 2401: double ulim,u,r,q, dum;
2402: double fu;
1.187 brouard 2403:
2404: double scale=10.;
2405: int iterscale=0;
2406:
2407: *fa=(*func)(*ax); /* xta[j]=pcom[j]+(*ax)*xicom[j]; fa=f(xta[j])*/
2408: *fb=(*func)(*bx); /* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) */
2409:
2410:
2411: /* while(*fb != *fb){ /\* *ax should be ok, reducing distance to *ax *\/ */
2412: /* printf("Warning mnbrak *fb = %lf, *bx=%lf *ax=%lf *fa==%lf iter=%d\n",*fb, *bx, *ax, *fa, iterscale++); */
2413: /* *bx = *ax - (*ax - *bx)/scale; */
2414: /* *fb=(*func)(*bx); /\* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) *\/ */
2415: /* } */
2416:
1.126 brouard 2417: if (*fb > *fa) {
2418: SHFT(dum,*ax,*bx,dum)
1.183 brouard 2419: SHFT(dum,*fb,*fa,dum)
2420: }
1.126 brouard 2421: *cx=(*bx)+GOLD*(*bx-*ax);
2422: *fc=(*func)(*cx);
1.183 brouard 2423: #ifdef DEBUG
1.224 brouard 2424: printf("mnbrak0 a=%lf *fa=%lf, b=%lf *fb=%lf, c=%lf *fc=%lf\n",*ax,*fa,*bx,*fb,*cx, *fc);
2425: fprintf(ficlog,"mnbrak0 a=%lf *fa=%lf, b=%lf *fb=%lf, c=%lf *fc=%lf\n",*ax,*fa,*bx,*fb,*cx, *fc);
1.183 brouard 2426: #endif
1.224 brouard 2427: while (*fb > *fc) { /* Declining a,b,c with fa> fb > fc. If fc=inf it exits and if flat fb=fc it exits too.*/
1.126 brouard 2428: r=(*bx-*ax)*(*fb-*fc);
1.224 brouard 2429: q=(*bx-*cx)*(*fb-*fa); /* What if fa=inf */
1.126 brouard 2430: u=(*bx)-((*bx-*cx)*q-(*bx-*ax)*r)/
1.183 brouard 2431: (2.0*SIGN(FMAX(fabs(q-r),TINY),q-r)); /* Minimum abscissa of a parabolic estimated from (a,fa), (b,fb) and (c,fc). */
2432: ulim=(*bx)+GLIMIT*(*cx-*bx); /* Maximum abscissa where function should be evaluated */
2433: if ((*bx-u)*(u-*cx) > 0.0) { /* if u_p is between b and c */
1.126 brouard 2434: fu=(*func)(u);
1.163 brouard 2435: #ifdef DEBUG
2436: /* f(x)=A(x-u)**2+f(u) */
2437: double A, fparabu;
2438: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
2439: fparabu= *fa - A*(*ax-u)*(*ax-u);
1.224 brouard 2440: 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);
2441: fprintf(ficlog,"\nmnbrak (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf), (*u=%.12f, fu=%.12lf, fparabu=%.12f, q=%lf < %lf=r)\n",*ax,*fa,*bx,*fb,*cx,*fc,u,fu, fparabu,q,r);
1.183 brouard 2442: /* And thus,it can be that fu > *fc even if fparabu < *fc */
2443: /* mnbrak (*ax=7.666299858533, *fa=299039.693133272231), (*bx=8.595447774979, *fb=298976.598289369489),
2444: (*cx=10.098840694817, *fc=298946.631474258087), (*u=9.852501168332, fu=298948.773013752128, fparabu=298945.434711494134) */
2445: /* In that case, there is no bracket in the output! Routine is wrong with many consequences.*/
1.163 brouard 2446: #endif
1.184 brouard 2447: #ifdef MNBRAKORIGINAL
1.183 brouard 2448: #else
1.191 brouard 2449: /* if (fu > *fc) { */
2450: /* #ifdef DEBUG */
2451: /* printf("mnbrak4 fu > fc \n"); */
2452: /* fprintf(ficlog, "mnbrak4 fu > fc\n"); */
2453: /* #endif */
2454: /* /\* 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 *\\/ *\/ */
2455: /* /\* SHFT(*fa,*fc,fu,*fc) /\\* (b, u, c) is a bracket while test fb > fc will be fu > fc will exit *\\/ *\/ */
2456: /* dum=u; /\* Shifting c and u *\/ */
2457: /* u = *cx; */
2458: /* *cx = dum; */
2459: /* dum = fu; */
2460: /* fu = *fc; */
2461: /* *fc =dum; */
2462: /* } else { /\* end *\/ */
2463: /* #ifdef DEBUG */
2464: /* printf("mnbrak3 fu < fc \n"); */
2465: /* fprintf(ficlog, "mnbrak3 fu < fc\n"); */
2466: /* #endif */
2467: /* dum=u; /\* Shifting c and u *\/ */
2468: /* u = *cx; */
2469: /* *cx = dum; */
2470: /* dum = fu; */
2471: /* fu = *fc; */
2472: /* *fc =dum; */
2473: /* } */
1.224 brouard 2474: #ifdef DEBUGMNBRAK
2475: double A, fparabu;
2476: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
2477: fparabu= *fa - A*(*ax-u)*(*ax-u);
2478: 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);
2479: fprintf(ficlog,"\nmnbrak35 ax=%lf fa=%lf bx=%lf fb=%lf, u=%lf fp=%lf fu=%lf < or >= fc=%lf cx=%lf, q=%lf < %lf=r \n",*ax, *fa, *bx,*fb,u,fparabu,fu,*fc,*cx,q,r);
1.183 brouard 2480: #endif
1.191 brouard 2481: dum=u; /* Shifting c and u */
2482: u = *cx;
2483: *cx = dum;
2484: dum = fu;
2485: fu = *fc;
2486: *fc =dum;
1.183 brouard 2487: #endif
1.162 brouard 2488: } else if ((*cx-u)*(u-ulim) > 0.0) { /* u is after c but before ulim */
1.183 brouard 2489: #ifdef DEBUG
1.224 brouard 2490: printf("\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
2491: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
1.183 brouard 2492: #endif
1.126 brouard 2493: fu=(*func)(u);
2494: if (fu < *fc) {
1.183 brouard 2495: #ifdef DEBUG
1.224 brouard 2496: printf("\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
2497: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
2498: #endif
2499: SHFT(*bx,*cx,u,*cx+GOLD*(*cx-*bx))
2500: SHFT(*fb,*fc,fu,(*func)(u))
2501: #ifdef DEBUG
2502: printf("\nmnbrak2 shift GOLD c=%lf",*cx+GOLD*(*cx-*bx));
1.183 brouard 2503: #endif
2504: }
1.162 brouard 2505: } else if ((u-ulim)*(ulim-*cx) >= 0.0) { /* u outside ulim (verifying that ulim is beyond c) */
1.183 brouard 2506: #ifdef DEBUG
1.224 brouard 2507: printf("\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
2508: fprintf(ficlog,"\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
1.183 brouard 2509: #endif
1.126 brouard 2510: u=ulim;
2511: fu=(*func)(u);
1.183 brouard 2512: } else { /* u could be left to b (if r > q parabola has a maximum) */
2513: #ifdef DEBUG
1.224 brouard 2514: printf("\nmnbrak2 u=%lf could be left to b=%lf (if r=%lf > q=%lf parabola has a maximum)\n",u,*bx,r,q);
2515: fprintf(ficlog,"\nmnbrak2 u=%lf could be left to b=%lf (if r=%lf > q=%lf parabola has a maximum)\n",u,*bx,r,q);
1.183 brouard 2516: #endif
1.126 brouard 2517: u=(*cx)+GOLD*(*cx-*bx);
2518: fu=(*func)(u);
1.224 brouard 2519: #ifdef DEBUG
2520: printf("\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
2521: fprintf(ficlog,"\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
2522: #endif
1.183 brouard 2523: } /* end tests */
1.126 brouard 2524: SHFT(*ax,*bx,*cx,u)
1.183 brouard 2525: SHFT(*fa,*fb,*fc,fu)
2526: #ifdef DEBUG
1.224 brouard 2527: printf("\nmnbrak2 shift (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf)\n",*ax,*fa,*bx,*fb,*cx,*fc);
2528: fprintf(ficlog, "\nmnbrak2 shift (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf)\n",*ax,*fa,*bx,*fb,*cx,*fc);
1.183 brouard 2529: #endif
2530: } /* end while; ie return (a, b, c, fa, fb, fc) such that a < b < c with f(a) > f(b) and fb < f(c) */
1.126 brouard 2531: }
2532:
2533: /*************** linmin ************************/
1.162 brouard 2534: /* Given an n -dimensional point p[1..n] and an n -dimensional direction xi[1..n] , moves and
2535: resets p to where the function func(p) takes on a minimum along the direction xi from p ,
2536: and replaces xi by the actual vector displacement that p was moved. Also returns as fret
2537: the value of func at the returned location p . This is actually all accomplished by calling the
2538: routines mnbrak and brent .*/
1.126 brouard 2539: int ncom;
2540: double *pcom,*xicom;
2541: double (*nrfunc)(double []);
2542:
1.224 brouard 2543: #ifdef LINMINORIGINAL
1.126 brouard 2544: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []))
1.224 brouard 2545: #else
2546: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []), int *flat)
2547: #endif
1.126 brouard 2548: {
2549: double brent(double ax, double bx, double cx,
2550: double (*f)(double), double tol, double *xmin);
2551: double f1dim(double x);
2552: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb,
2553: double *fc, double (*func)(double));
2554: int j;
2555: double xx,xmin,bx,ax;
2556: double fx,fb,fa;
1.187 brouard 2557:
1.203 brouard 2558: #ifdef LINMINORIGINAL
2559: #else
2560: double scale=10., axs, xxs; /* Scale added for infinity */
2561: #endif
2562:
1.126 brouard 2563: ncom=n;
2564: pcom=vector(1,n);
2565: xicom=vector(1,n);
2566: nrfunc=func;
2567: for (j=1;j<=n;j++) {
2568: pcom[j]=p[j];
1.202 brouard 2569: xicom[j]=xi[j]; /* Former scale xi[j] of currrent direction i */
1.126 brouard 2570: }
1.187 brouard 2571:
1.203 brouard 2572: #ifdef LINMINORIGINAL
2573: xx=1.;
2574: #else
2575: axs=0.0;
2576: xxs=1.;
2577: do{
2578: xx= xxs;
2579: #endif
1.187 brouard 2580: ax=0.;
2581: mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim); /* Outputs: xtx[j]=pcom[j]+(*xx)*xicom[j]; fx=f(xtx[j]) */
2582: /* brackets with inputs ax=0 and xx=1, but points, pcom=p, and directions values, xicom=xi, are sent via f1dim(x) */
2583: /* 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)) */
2584: /* Outputs: fa=f(p(j)) and fx=f(p(j) + xxs * xi(j) ) and f(bx)= f(p(j)+ bx* xi(j)) */
2585: /* Given input ax=axs and xx=xxs, xx might be too far from ax to get a finite f(xx) */
2586: /* Searches on line, outputs (ax, xx, bx) such that fx < min(fa and fb) */
2587: /* Find a bracket a,x,b in direction n=xi ie xicom, order may change. Scale is [0:xxs*xi[j]] et non plus [0:xi[j]]*/
1.203 brouard 2588: #ifdef LINMINORIGINAL
2589: #else
2590: if (fx != fx){
1.224 brouard 2591: xxs=xxs/scale; /* Trying a smaller xx, closer to initial ax=0 */
2592: printf("|");
2593: fprintf(ficlog,"|");
1.203 brouard 2594: #ifdef DEBUGLINMIN
1.224 brouard 2595: printf("\nLinmin NAN : input [axs=%lf:xxs=%lf], mnbrak outputs fx=%lf <(fb=%lf and fa=%lf) with xx=%lf in [ax=%lf:bx=%lf] \n", axs, xxs, fx,fb, fa, xx, ax, bx);
1.203 brouard 2596: #endif
2597: }
1.224 brouard 2598: }while(fx != fx && xxs > 1.e-5);
1.203 brouard 2599: #endif
2600:
1.191 brouard 2601: #ifdef DEBUGLINMIN
2602: printf("\nLinmin after mnbrak: ax=%12.7f xx=%12.7f bx=%12.7f fa=%12.2f fx=%12.2f fb=%12.2f\n", ax,xx,bx,fa,fx,fb);
1.202 brouard 2603: fprintf(ficlog,"\nLinmin after mnbrak: ax=%12.7f xx=%12.7f bx=%12.7f fa=%12.2f fx=%12.2f fb=%12.2f\n", ax,xx,bx,fa,fx,fb);
1.191 brouard 2604: #endif
1.224 brouard 2605: #ifdef LINMINORIGINAL
2606: #else
1.317 brouard 2607: if(fb == fx){ /* Flat function in the direction */
2608: xmin=xx;
1.224 brouard 2609: *flat=1;
1.317 brouard 2610: }else{
1.224 brouard 2611: *flat=0;
2612: #endif
2613: /*Flat mnbrak2 shift (*ax=0.000000000000, *fa=51626.272983130431), (*bx=-1.618034000000, *fb=51590.149499362531), (*cx=-4.236068025156, *fc=51590.149499362531) */
1.187 brouard 2614: *fret=brent(ax,xx,bx,f1dim,TOL,&xmin); /* Giving a bracketting triplet (ax, xx, bx), find a minimum, xmin, according to f1dim, *fret(xmin),*/
2615: /* fa = f(p[j] + ax * xi[j]), fx = f(p[j] + xx * xi[j]), fb = f(p[j] + bx * xi[j]) */
2616: /* fmin = f(p[j] + xmin * xi[j]) */
2617: /* P+lambda n in that direction (lambdamin), with TOL between abscisses */
2618: /* f1dim(xmin): for (j=1;j<=ncom;j++) xt[j]=pcom[j]+xmin*xicom[j]; */
1.126 brouard 2619: #ifdef DEBUG
1.224 brouard 2620: 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);
2621: 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);
2622: #endif
2623: #ifdef LINMINORIGINAL
2624: #else
2625: }
1.126 brouard 2626: #endif
1.191 brouard 2627: #ifdef DEBUGLINMIN
2628: printf("linmin end ");
1.202 brouard 2629: fprintf(ficlog,"linmin end ");
1.191 brouard 2630: #endif
1.126 brouard 2631: for (j=1;j<=n;j++) {
1.203 brouard 2632: #ifdef LINMINORIGINAL
2633: xi[j] *= xmin;
2634: #else
2635: #ifdef DEBUGLINMIN
2636: if(xxs <1.0)
2637: printf(" before xi[%d]=%12.8f", j,xi[j]);
2638: #endif
2639: 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) */
2640: #ifdef DEBUGLINMIN
2641: if(xxs <1.0)
2642: 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 );
2643: #endif
2644: #endif
1.187 brouard 2645: p[j] += xi[j]; /* Parameters values are updated accordingly */
1.126 brouard 2646: }
1.191 brouard 2647: #ifdef DEBUGLINMIN
1.203 brouard 2648: printf("\n");
1.191 brouard 2649: printf("Comparing last *frec(xmin=%12.8f)=%12.8f from Brent and frec(0.)=%12.8f \n", xmin, *fret, (*func)(p));
1.202 brouard 2650: fprintf(ficlog,"Comparing last *frec(xmin=%12.8f)=%12.8f from Brent and frec(0.)=%12.8f \n", xmin, *fret, (*func)(p));
1.191 brouard 2651: for (j=1;j<=n;j++) {
1.202 brouard 2652: printf(" xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
2653: fprintf(ficlog," xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
2654: if(j % ncovmodel == 0){
1.191 brouard 2655: printf("\n");
1.202 brouard 2656: fprintf(ficlog,"\n");
2657: }
1.191 brouard 2658: }
1.203 brouard 2659: #else
1.191 brouard 2660: #endif
1.126 brouard 2661: free_vector(xicom,1,n);
2662: free_vector(pcom,1,n);
2663: }
2664:
1.359 brouard 2665: /**** praxis gegen ****/
2666:
2667: /* This has been tested by Visual C from Microsoft and works */
2668: /* meaning tha valgrind could be wrong */
2669: /*********************************************************************/
2670: /* f u n c t i o n p r a x i s */
2671: /* */
2672: /* praxis is a general purpose routine for the minimization of a */
2673: /* function in several variables. the algorithm used is a modifi- */
2674: /* cation of conjugate gradient search method by powell. the changes */
2675: /* are due to r.p. brent, who gives an algol-w program, which served */
2676: /* as a basis for this function. */
2677: /* */
2678: /* references: */
2679: /* - powell, m.j.d., 1964. an efficient method for finding */
2680: /* the minimum of a function in several variables without */
2681: /* calculating derivatives, computer journal, 7, 155-162 */
2682: /* - brent, r.p., 1973. algorithms for minimization without */
2683: /* derivatives, prentice hall, englewood cliffs. */
2684: /* */
2685: /* problems, suggestions or improvements are always wellcome */
2686: /* karl gegenfurtner 07/08/87 */
2687: /* c - version */
2688: /*********************************************************************/
2689: /* */
2690: /* usage: min = praxis(tol, macheps, h, n, prin, x, func) */
2691: /* macheps has been suppressed because it is replaced by DBL_EPSILON */
2692: /* and if it was an argument of praxis (as it is in original brent) */
2693: /* it should be declared external */
2694: /* usage: min = praxis(tol, h, n, prin, x, func) */
2695: /* was min = praxis(fun, x, n); */
2696: /* */
2697: /* fun the function to be minimized. fun is called from */
2698: /* praxis with x and n as arguments */
2699: /* x a double array containing the initial guesses for */
2700: /* the minimum, which will contain the solution on */
2701: /* return */
2702: /* n an integer specifying the number of unknown */
2703: /* parameters */
2704: /* min praxis returns the least calculated value of fun */
2705: /* */
2706: /* some additional global variables control some more aspects of */
2707: /* the inner workings of praxis. setting them is optional, they */
2708: /* are all set to some reasonable default values given below. */
2709: /* */
2710: /* prin controls the printed output from the routine. */
2711: /* 0 -> no output */
2712: /* 1 -> print only starting and final values */
2713: /* 2 -> detailed map of the minimization process */
2714: /* 3 -> print also eigenvalues and vectors of the */
2715: /* search directions */
2716: /* the default value is 1 */
2717: /* tol is the tolerance allowed for the precision of the */
2718: /* solution. praxis returns if the criterion */
2719: /* 2 * ||x[k]-x[k-1]|| <= sqrt(macheps) * ||x[k]|| + tol */
2720: /* is fulfilled more than ktm times. */
2721: /* the default value depends on the machine precision */
2722: /* ktm see just above. default is 1, and a value of 4 leads */
2723: /* to a very(!) cautious stopping criterion. */
2724: /* h0 or step is a steplength parameter and should be set equal */
2725: /* to the expected distance from the solution. */
2726: /* exceptionally small or large values of step lead to */
2727: /* slower convergence on the first few iterations */
2728: /* the default value for step is 1.0 */
2729: /* scbd is a scaling parameter. 1.0 is the default and */
2730: /* indicates no scaling. if the scales for the different */
2731: /* parameters are very different, scbd should be set to */
2732: /* a value of about 10.0. */
2733: /* illc should be set to true (1) if the problem is known to */
2734: /* be ill-conditioned. the default is false (0). this */
2735: /* variable is automatically set, when praxis finds */
2736: /* the problem to be ill-conditioned during iterations. */
2737: /* maxfun is the maximum number of calls to fun allowed. praxis */
2738: /* will return after maxfun calls to fun even when the */
2739: /* minimum is not yet found. the default value of 0 */
2740: /* indicates no limit on the number of calls. */
2741: /* this return condition is only checked every n */
2742: /* iterations. */
2743: /* */
2744: /*********************************************************************/
2745:
2746: #include <math.h>
2747: #include <stdio.h>
2748: #include <stdlib.h>
2749: #include <float.h> /* for DBL_EPSILON */
2750: /* #include "machine.h" */
2751:
2752:
2753: /* extern void minfit(int n, double eps, double tol, double **ab, double q[]); */
2754: /* extern void minfit(int n, double eps, double tol, double ab[N][N], double q[]); */
2755: /* control parameters */
2756: /* control parameters */
2757: #define SQREPSILON 1.0e-19
2758: /* #define EPSILON 1.0e-8 */ /* in main */
2759:
2760: double tol = SQREPSILON,
2761: scbd = 1.0,
2762: step = 1.0;
2763: int ktm = 1,
2764: /* prin = 2, */
2765: maxfun = 0,
2766: illc = 0;
2767:
2768: /* some global variables */
2769: static int i, j, k, k2, nl, nf, kl, kt;
2770: /* static double s; */
2771: double sl, dn, dmin,
2772: fx, f1, lds, ldt, sf, df,
2773: qf1, qd0, qd1, qa, qb, qc,
2774: m2, m4, small_windows, vsmall, large,
2775: vlarge, ldfac, t2;
2776: /* static double d[N], y[N], z[N], */
2777: /* q0[N], q1[N], v[N][N]; */
2778:
2779: static double *d, *y, *z;
2780: static double *q0, *q1, **v;
2781: double *tflin; /* used in flin: return (*fun)(tflin, n); */
2782: double *e; /* used in minfit, don't konw how to free memory and thus made global */
2783: /* static double s, sl, dn, dmin, */
2784: /* fx, f1, lds, ldt, sf, df, */
2785: /* qf1, qd0, qd1, qa, qb, qc, */
2786: /* m2, m4, small, vsmall, large, */
2787: /* vlarge, ldfac, t2; */
2788: /* static double d[N], y[N], z[N], */
2789: /* q0[N], q1[N], v[N][N]; */
2790:
2791: /* these will be set by praxis to point to it's arguments */
2792: static int prin; /* added */
2793: static int n;
2794: static double *x;
2795: static double (*fun)();
2796: /* static double (*fun)(double *x, int n); */
2797:
2798: /* these will be set by praxis to the global control parameters */
2799: /* static double h, macheps, t; */
2800: extern double macheps;
2801: static double h;
2802: static double t;
2803:
2804: static double
2805: drandom() /* return random no between 0 and 1 */
2806: {
2807: return (double)(rand()%(8192*2))/(double)(8192*2);
2808: }
2809:
2810: static void sort() /* d and v in descending order */
2811: {
2812: int k, i, j;
2813: double s;
2814:
2815: for (i=1; i<=n-1; i++) {
2816: k = i; s = d[i];
2817: for (j=i+1; j<=n; j++) {
2818: if (d[j] > s) {
2819: k = j;
2820: s = d[j];
2821: }
2822: }
2823: if (k > i) {
2824: d[k] = d[i];
2825: d[i] = s;
2826: for (j=1; j<=n; j++) {
2827: s = v[j][i];
2828: v[j][i] = v[j][k];
2829: v[j][k] = s;
2830: }
2831: }
2832: }
2833: }
2834:
2835: double randbrent ( int *naught )
2836: {
2837: double ran1, ran3[127], half;
2838: int ran2, q, r, i, j;
2839: int init=0; /* false */
2840: double rr;
2841: /* REAL*8 RAN1,RAN3(127),HALF */
2842:
2843: /* INTEGER RAN2,Q,R */
2844: /* LOGICAL INIT */
2845: /* DATA INIT/.FALSE./ */
2846: /* IF (INIT) GO TO 3 */
2847: if(!init){
2848: /* R = MOD(NAUGHT,8190) + 1 *//* 1804289383 rand () */
2849: r = *naught % 8190 + 1;/* printf(" naught r %d %d",*naught,r); */
2850: ran2=127;
2851: for(i=ran2; i>0; i--){
2852: /* RAN2 = 128 */
2853: /* DO 2 I=1,127 */
2854: ran2 = ran2-1;
2855: /* RAN2 = RAN2 - 1 */
2856: ran1 = -pow(2.0,55);
2857: /* RAN1 = -2.D0**55 */
2858: /* DO 1 J=1,7 */
2859: for(j=1; j<=7;j++){
2860: /* R = MOD(1756*R,8191) */
2861: r = (1756*r) % 8191;/* printf(" i=%d (1756*r)%8191=%d",j,r); */
2862: q=r/32;
2863: /* Q = R/32 */
2864: /* 1 RAN1 = (RAN1 + Q)*(1.0D0/256) */
2865: ran1 =(ran1+q)*(1.0/256);
2866: }
2867: /* 2 RAN3(RAN2) = RAN1 */
2868: ran3[ran2] = ran1; /* printf(" ran2=%d ran1=%.7g \n",ran2,ran1); */
2869: }
2870: /* INIT = .TRUE. */
2871: init=1;
2872: /* 3 IF (RAN2.EQ.1) RAN2 = 128 */
2873: }
2874: if(ran2 == 0) ran2 = 126;
2875: else ran2 = ran2 -1;
2876: /* RAN2 = RAN2 - 1 */
2877: /* RAN1 = RAN1 + RAN3(RAN2) */
2878: ran1 = ran1 + ran3[ran2];/* printf("BIS ran2=%d ran1=%.7g \n",ran2,ran1); */
2879: half= 0.5;
2880: /* HALF = .5D0 */
2881: /* IF (RAN1.GE.0.D0) HALF = -HALF */
2882: if(ran1 >= 0.) half =-half;
2883: ran1 = ran1 +half;
2884: ran3[ran2] = ran1;
2885: rr= ran1+0.5;
2886: /* RAN1 = RAN1 + HALF */
2887: /* RAN3(RAN2) = RAN1 */
2888: /* RANDOM = RAN1 + .5D0 */
2889: /* r = ( ( double ) ( *seed ) ) * 4.656612875E-10; */
2890: return rr;
2891: }
2892: static void matprint(char *s, double **v, int m, int n)
2893: /* char *s; */
2894: /* double v[N][N]; */
2895: {
2896: #define INCX 8
2897: int i;
2898:
2899: int i2hi;
2900: int ihi;
2901: int ilo;
2902: int i2lo;
2903: int jlo=1;
2904: int j;
2905: int j2hi;
2906: int jhi;
2907: int j2lo;
2908: ilo=1;
2909: ihi=n;
2910: jlo=1;
2911: jhi=n;
2912:
2913: printf ("\n" );
2914: printf ("%s\n", s );
2915: for ( j2lo = jlo; j2lo <= jhi; j2lo = j2lo + INCX )
2916: {
2917: j2hi = j2lo + INCX - 1;
2918: if ( n < j2hi )
2919: {
2920: j2hi = n;
2921: }
2922: if ( jhi < j2hi )
2923: {
2924: j2hi = jhi;
2925: }
2926:
2927: /* fprintf ( ficlog, "\n" ); */
2928: printf ("\n" );
2929: /*
2930: For each column J in the current range...
2931:
2932: Write the header.
2933: */
2934: /* fprintf ( ficlog, " Col: "); */
2935: printf ("Col:");
2936: for ( j = j2lo; j <= j2hi; j++ )
2937: {
2938: /* fprintf ( ficlog, " %7d ", j - 1 ); */
2939: /* printf (" %9d ", j - 1 ); */
2940: printf (" %9d ", j );
2941: }
2942: /* fprintf ( ficlog, "\n" ); */
2943: /* fprintf ( ficlog, " Row\n" ); */
2944: /* fprintf ( ficlog, "\n" ); */
2945: printf ("\n" );
2946: printf (" Row\n" );
2947: printf ("\n" );
2948: /*
2949: Determine the range of the rows in this strip.
2950: */
2951: if ( 1 < ilo ){
2952: i2lo = ilo;
2953: }else{
2954: i2lo = 1;
2955: }
2956: if ( m < ihi ){
2957: i2hi = m;
2958: }else{
2959: i2hi = ihi;
2960: }
2961:
2962: for ( i = i2lo; i <= i2hi; i++ ){
2963: /*
2964: Print out (up to) 5 entries in row I, that lie in the current strip.
2965: */
2966: /* fprintf ( ficlog, "%5d:", i - 1 ); */
2967: /* printf ("%5d:", i - 1 ); */
2968: printf ("%5d:", i );
2969: for ( j = j2lo; j <= j2hi; j++ )
2970: {
2971: /* fprintf ( ficlog, " %14g", a[i-1+(j-1)*m] ); */
2972: /* printf ("%14.7g ", a[i-1+(j-1)*m] ); */
2973: /* printf("%14.7f ", v[i-1][j-1]); */
2974: printf("%14.7f ", v[i][j]);
2975: /* fprintf ( stdout, " %14g", a[i-1+(j-1)*m] ); */
2976: }
2977: /* fprintf ( ficlog, "\n" ); */
2978: printf ("\n" );
2979: }
2980: }
2981:
2982: /* printf("%s\n", s); */
2983: /* for (k=0; k<n; k++) { */
2984: /* for (i=0; i<n; i++) { */
2985: /* /\* printf("%20.10e ", v[k][i]); *\/ */
2986: /* } */
2987: /* printf("\n"); */
2988: /* } */
2989: #undef INCX
2990: }
2991:
2992: void vecprint(char *s, double *x, int n)
2993: /* char *s; */
2994: /* double x[N]; */
2995: {
2996: int i=0;
2997:
2998: printf(" %s", s);
2999: /* for (i=0; i<n; i++) */
3000: for (i=1; i<=n; i++)
3001: printf (" %14.7g", x[i] );
3002: /* printf(" %8d: %14g\n", i, x[i]); */
3003: printf ("\n" );
3004: }
3005:
3006: static void print() /* print a line of traces */
3007: {
3008:
3009:
3010: printf("\n");
3011: /* printf("... chi square reduced to ... %20.10e\n", fx); */
3012: /* printf("... after %u function calls ...\n", nf); */
3013: /* printf("... including %u linear searches ...\n", nl); */
3014: printf("%10d %10d%14.7g",nl, nf, fx);
3015: vecprint("... current values of x ...", x, n);
3016: }
3017: /* static void print2(int n, double *x, int prin, double fx, int nf, int nl) */ /* print a line of traces */
3018: static void print2() /* print a line of traces */
3019: {
3020: int i; double fmin=0.;
3021:
3022: /* printf("\n"); */
3023: /* printf("... chi square reduced to ... %20.10e\n", fx); */
3024: /* printf("... after %u function calls ...\n", nf); */
3025: /* printf("... including %u linear searches ...\n", nl); */
3026: /* printf("%10d %10d%14.7g",nl, nf, fx); */
1.363 brouard 3027: /* printf ( "\n" ); */
1.359 brouard 3028: printf ( " Linear searches %d", nl );
1.364 brouard 3029: fprintf (ficlog, " Linear searches %d", nl );
1.359 brouard 3030: /* printf ( " Linear searches %d\n", nl ); */
3031: /* printf ( " Function evaluations %d\n", nf ); */
3032: /* printf ( " Function value FX = %g\n", fx ); */
3033: printf ( " Function evaluations %d", nf );
3034: printf ( " Function value FX = %.12lf\n", fx );
1.363 brouard 3035: fprintf (ficlog, " Function evaluations %d", nf );
3036: fprintf (ficlog, " Function value FX = %.12lf\n", fx );
1.359 brouard 3037: #ifdef DEBUGPRAX
3038: printf("n=%d prin=%d\n",n,prin);
3039: #endif
1.363 brouard 3040: /* if(fx <= fmin) printf(" UNDEFINED "); else printf("%14.7g",log(fx-fmin)); */
1.359 brouard 3041: if ( n <= 4 || 2 < prin )
3042: {
3043: /* for(i=1;i<=n;i++)printf("%14.7g",x[i-1]); */
1.363 brouard 3044: for(i=1;i<=n;i++){
1.364 brouard 3045: printf(" %14.7g",x[i]);
3046: fprintf(ficlog," %14.7g",x[i]);
1.363 brouard 3047: }
1.359 brouard 3048: /* r8vec_print ( n, x, " X:" ); */
3049: }
3050: printf("\n");
1.363 brouard 3051: fprintf(ficlog,"\n");
1.359 brouard 3052: }
3053:
3054:
3055: /* #ifdef MSDOS */
3056: /* static double tflin[N]; */
3057: /* #endif */
3058:
3059: static double flin(double l, int j)
3060: /* double l; */
3061: {
3062: int i;
3063: /* #ifndef MSDOS */
3064: /* double tflin[N]; */
3065: /* #endif */
3066: /* double *tflin; */ /* Be careful to put tflin on a vector n */
3067:
3068: /* j is used from 0 to n-1 and can be -1 for parabolic search */
3069:
3070: /* if (j != -1) { /\* linear search *\/ */
3071: if (j > 0) { /* linear search */
3072: /* for (i=0; i<n; i++){ */
3073: for (i=1; i<=n; i++){
3074: tflin[i] = x[i] + l *v[i][j];
3075: #ifdef DEBUGPRAX
3076: /* printf(" flin i=%14d t=%14.7f x=%14.7f l=%14.7f v[%d,%d]=%14.7f nf=%14d\n",i+1, tflin[i],x[i],l,i,j,v[i][j],nf); */
3077: printf(" flin i=%14d t=%14.7f x=%14.7f l=%14.7f v[%d,%d]=%14.7f nf=%14d\n",i, tflin[i],x[i],l,i,j,v[i][j],nf);
3078: #endif
3079: }
3080: }
3081: else { /* search along parabolic space curve */
3082: qa = l*(l-qd1)/(qd0*(qd0+qd1));
3083: qb = (l+qd0)*(qd1-l)/(qd0*qd1);
3084: qc = l*(l+qd0)/(qd1*(qd0+qd1));
3085: #ifdef DEBUGPRAX
3086: printf(" search along a parabolic space curve. j=%14d nf=%14d l=%14.7f qd0=%14.7f qd1=%14.7f\n",j,nf,l,qd0,qd1);
3087: #endif
3088: /* for (i=0; i<n; i++){ */
3089: for (i=1; i<=n; i++){
3090: tflin[i] = qa*q0[i]+qb*x[i]+qc*q1[i];
3091: #ifdef DEBUGPRAX
3092: /* printf(" parabole i=%14d t(i)=%14.7f q0=%14.7f x=%14.7f q1=%14.7f\n",i+1,tflin[i],q0[i],x[i],q1[i]); */
3093: printf(" parabole i=%14d t(i)=%14.7e q0=%14.7e x=%14.7e q1=%14.7e\n",i,tflin[i],q0[i],x[i],q1[i]);
3094: #endif
3095: }
3096: }
3097: nf++;
3098:
3099: #ifdef NR_SHIFT
3100: return (*fun)((tflin-1), n);
3101: #else
3102: /* return (*fun)(tflin, n);*/
3103: return (*fun)(tflin);
3104: #endif
3105: }
3106:
3107: void minny(int j, int nits, double *d2, double *x1, double f1, int fk)
3108: /* double *d2, *x1, f1; */
3109: {
3110: /* here j is from 0 to n-1 and can be -1 for parabolic search */
3111: /* MINIMIZES F FROM X IN THE DIRECTION V(*,J) */
3112: /* UNLESS J<1, WHEN A QUADRATIC SEARCH IS DONE */
3113: /* IN THE PLANE DEFINED BY Q0, Q1 AND X. */
3114: /* D2 AN APPROXIMATION TO HALF F'' (OR ZERO), */
3115: /* X1 AN ESTIMATE OF DISTANCE TO MINIMUM, */
3116: /* RETURNED AS THE DISTANCE FOUND. */
3117: /* IF FK = TRUE THEN F1 IS FLIN(X1), OTHERWISE */
3118: /* X1 AND F1 ARE IGNORED ON ENTRY UNLESS FINAL */
3119: /* FX > F1. NITS CONTROLS THE NUMBER OF TIMES */
3120: /* AN ATTEMPT IS MADE TO HALVE THE INTERVAL. */
3121: /* SIDE EFFECTS: USES AND ALTERS X, FX, NF, NL. */
3122: /* IF J < 1 USES VARIABLES Q... . */
3123: /* USES H, N, T, M2, M4, LDT, DMIN, MACHEPS; */
3124: int k, i, dz;
3125: double x2, xm, f0, f2, fm, d1, t2, sf1, sx1;
3126: double s;
3127: double macheps;
3128: macheps=pow(16.0,-13.0);
3129: sf1 = f1; sx1 = *x1;
3130: k = 0; xm = 0.0; fm = f0 = fx; dz = *d2 < macheps;
3131: /* h=1.0;*/ /* To be revised */
3132: #ifdef DEBUGPRAX
3133: /* printf("min macheps=%14g h=%14g step=%14g t=%14g fx=%14g\n",macheps,h, step,t, fx); */
3134: /* Where is fx coming from */
3135: printf(" min macheps=%14g h=%14g t=%14g fx=%.9lf dirj=%d\n",macheps, h, t, fx, j);
3136: matprint(" min vectors:",v,n,n);
3137: #endif
3138: /* find step size */
3139: s = 0.;
3140: /* for (i=0; i<n; i++) s += x[i]*x[i]; */
3141: for (i=1; i<=n; i++) s += x[i]*x[i];
3142: s = sqrt(s);
3143: if (dz)
3144: t2 = m4*sqrt(fabs(fx)/dmin + s*ldt) + m2*ldt;
3145: else
3146: t2 = m4*sqrt(fabs(fx)/(*d2) + s*ldt) + m2*ldt;
3147: s = s*m4 + t;
3148: if (dz && t2 > s) t2 = s;
3149: if (t2 < small_windows) t2 = small_windows;
3150: if (t2 > 0.01*h) t2 = 0.01 * h;
3151: if (fk && f1 <= fm) {
3152: xm = *x1;
3153: fm = f1;
3154: }
3155: #ifdef DEBUGPRAX
3156: printf(" additional flin X1=%14.7f t2=%14.7f *f1=%14.7f fm=%14.7f fk=%d\n",*x1,t2,f1,fm,fk);
3157: #endif
3158: if (!fk || fabs(*x1) < t2) {
3159: *x1 = (*x1 >= 0 ? t2 : -t2);
3160: /* *x1 = (*x1 > 0 ? t2 : -t2); */ /* kind of error */
3161: #ifdef DEBUGPRAX
3162: printf(" additional flin X1=%16.10e dirj=%d fk=%d\n",*x1, j, fk);
3163: #endif
3164: f1 = flin(*x1, j);
3165: #ifdef DEBUGPRAX
3166: printf(" after flin f1=%18.12e dirj=%d fk=%d\n",f1, j,fk);
3167: #endif
3168: }
3169: if (f1 <= fm) {
3170: xm = *x1;
3171: fm = f1;
3172: }
3173: L0: /*L0 loop or next */
3174: /*
3175: Evaluate FLIN at another point and estimate the second derivative.
3176: */
3177: if (dz) {
3178: x2 = (f0 < f1 ? -(*x1) : 2*(*x1));
3179: #ifdef DEBUGPRAX
3180: printf(" additional second flin x2=%14.8e x1=%14.8e f0=%14.8e f1=%18.12e dirj=%d\n",x2,*x1,f0,f1,j);
3181: #endif
3182: f2 = flin(x2, j);
3183: #ifdef DEBUGPRAX
3184: printf(" additional second flin x2=%16.10e x1=%16.10e f1=%18.12e f0=%18.10e f2=%18.10e fm=%18.10e\n",x2, *x1, f1,f0,f2,fm);
3185: #endif
3186: if (f2 <= fm) {
3187: xm = x2;
3188: fm = f2;
3189: }
3190: /* d2 is the curvature or double difference f1 doesn't seem to be accurately computed */
3191: *d2 = (x2*(f1-f0) - (*x1)*(f2-f0))/((*x1)*x2*((*x1)-x2));
3192: #ifdef DEBUGPRAX
3193: double d11,d12;
3194: d11=(f1-f0)/(*x1);d12=(f2-f0)/x2;
3195: printf(" d11=%18.12e d12=%18.12e d11-d12=%18.12e x1-x2=%18.12e (d11-d12)/(x2-(*x1))=%18.12e\n", d11 ,d12, d11-d12, x2-(*x1), (d11-d12)/(x2-(*x1)));
3196: printf(" original computing f1=%18.12e *d2=%16.10e f0=%18.12e f1-f0=%16.10e f2-f0=%16.10e\n",f1,*d2,f0,f1-f0, f2-f0);
3197: double ff1=7.783920622852e+04;
3198: double f1mf0=9.0344736236e-05;
3199: *d2 = (f1mf0)/ (*x1)/((*x1)-x2) - (f2-f0)/x2/((*x1)-x2);
3200: /* *d2 = (ff1-f0)/ (*x1)/((*x1)-x2) - (f2-f0)/x2/((*x1)-x2); */
3201: printf(" simpliff computing *d2=%16.10e f1mf0=%18.12e,f1=f0+f1mf0=%18.12e\n",*d2,f1mf0,f0+f1mf0);
3202: *d2 = ((f1-f0)/ (*x1) - (f2-f0)/x2)/((*x1)-x2);
3203: printf(" overlifi computing *d2=%16.10e\n",*d2);
3204: #endif
3205: *d2 = ((f1-f0)/ (*x1) - (f2-f0)/x2)/((*x1)-x2);
3206: }
3207: #ifdef DEBUGPRAX
3208: printf(" additional second flin xm=%14.8e fm=%14.8e *d2=%14.8e\n",xm, fm,*d2);
3209: #endif
3210: /*
3211: Estimate the first derivative at 0.
3212: */
3213: d1 = (f1-f0)/(*x1) - *x1**d2; dz = 1;
3214: /*
3215: Predict the minimum.
3216: */
3217: if (*d2 <= small_windows) {
3218: x2 = (d1 < 0 ? h : -h);
3219: }
3220: else {
3221: x2 = - 0.5*d1/(*d2);
3222: }
3223: #ifdef DEBUGPRAX
3224: printf(" AT d1=%14.8e d2=%14.8e small=%14.8e dz=%d x1=%14.8e x2=%14.8e\n",d1,*d2,small_windows,dz,*x1,x2);
3225: #endif
3226: if (fabs(x2) > h)
3227: x2 = (x2 > 0 ? h : -h);
3228: L1: /* L1 or try loop */
3229: #ifdef DEBUGPRAX
3230: printf(" AT predicted minimum flin x2=%14.8e x1=%14.8e K=%14d NITS=%14d dirj=%d\n",x2,*x1,k,nits,j);
3231: #endif
3232: f2 = flin(x2, j); /* x[i]+x2*v[i][j] */
3233: #ifdef DEBUGPRAX
3234: printf(" after flin f0=%14.8e f1=%14.8e f2=%14.8e fm=%14.8e\n",f0,f1,f2, fm);
3235: #endif
3236: if ((k < nits) && (f2 > f0)) {
3237: #ifdef DEBUGPRAX
3238: printf(" NO SUCCESS SO TRY AGAIN;\n");
3239: #endif
3240: k++;
3241: if ((f0 < f1) && (*x1*x2 > 0.0))
3242: goto L0; /* or next */
3243: x2 *= 0.5;
3244: goto L1;
3245: }
3246: nl++;
3247: #ifdef DEBUGPRAX
3248: printf(" bebeBE end of min x1=%14.8e x2=%14.8e f1=%14.8e f2=%14.8e f0=%14.8e fm=%14.8e d2=%14.8e\n",*x1, x2, f1, f2, f0, fm, *d2);
3249: #endif
3250: if (f2 > fm) x2 = xm; else fm = f2;
3251: if (fabs(x2*(x2-*x1)) > small_windows) {
3252: *d2 = (x2*(f1-f0) - *x1*(fm-f0))/(*x1*x2*(*x1-x2));
3253: }
3254: else {
3255: if (k > 0) *d2 = 0;
3256: }
3257: #ifdef DEBUGPRAX
1.362 brouard 3258: printf(" bebe end of min x1 might be very wrong x1=%14.8e fx=%14.8e d2=%14.8e\n",*x1, fx, *d2);
1.359 brouard 3259: #endif
3260: if (*d2 <= small_windows) *d2 = small_windows;
3261: *x1 = x2; fx = fm;
3262: if (sf1 < fx) {
3263: fx = sf1;
3264: *x1 = sx1;
3265: }
3266: /*
3267: Update X for linear search.
3268: */
3269: #ifdef DEBUGPRAX
3270: printf(" end of min x1=%14.8e fx=%14.8e d2=%14.8e\n",*x1, fx, *d2);
3271: #endif
3272:
3273: /* if (j != -1) */
3274: /* for (i=0; i<n; i++) */
3275: /* x[i] += (*x1)*v[i][j]; */
3276: if (j > 0)
3277: for (i=1; i<=n; i++)
3278: x[i] += (*x1)*v[i][j];
3279: }
3280:
3281: void quad() /* look for a minimum along the curve q0, q1, q2 */
3282: {
3283: int i;
3284: double l, s;
3285:
3286: s = fx; fx = qf1; qf1 = s; qd1 = 0.0;
3287: /* for (i=0; i<n; i++) { */
3288: for (i=1; i<=n; i++) {
3289: s = x[i]; l = q1[i]; x[i] = l; q1[i] = s;
3290: qd1 = qd1 + (s-l)*(s-l);
3291: }
3292: s = 0.0; qd1 = sqrt(qd1); l = qd1;
3293: #ifdef DEBUGPRAX
3294: printf(" QUAD after sqrt qd1=%14.8e \n",qd1);
3295: #endif
3296:
3297: if (qd0>0.0 && qd1>0.0 &&nl>=3*n*n) {
3298: #ifdef DEBUGPRAX
3299: printf(" QUAD before min value=%14.8e \n",qf1);
3300: #endif
3301: /* min(-1, 2, &s, &l, qf1, 1); */
3302: minny(0, 2, &s, &l, qf1, 1);
3303: qa = l*(l-qd1)/(qd0*(qd0+qd1));
3304: qb = (l+qd0)*(qd1-l)/(qd0*qd1);
3305: qc = l*(l+qd0)/(qd1*(qd0+qd1));
3306: }
3307: else {
3308: fx = qf1; qa = qb = 0.0; qc = 1.0;
3309: }
3310: #ifdef DEBUGPRAX
3311: printf("after eventual min qd0=%14.8e qd1=%14.8e nl=%d\n",qd0, qd1,nl);
3312: #endif
3313: qd0 = qd1;
3314: /* for (i=0; i<n; i++) { */
3315: for (i=1; i<=n; i++) {
3316: s = q0[i]; q0[i] = x[i];
3317: x[i] = qa*s + qb*x[i] + qc*q1[i];
3318: }
3319: #ifdef DEBUGQUAD
3320: vecprint ( " X after QUAD:" , x, n );
3321: #endif
3322: }
3323:
3324: /* void minfit(int n, double eps, double tol, double ab[N][N], double q[]) */
3325: void minfit(int n, double eps, double tol, double **ab, double q[])
3326: /* int n; */
3327: /* double eps, tol, ab[N][N], q[N]; */
3328: {
3329: int l, kt, l2, i, j, k;
3330: double c, f, g, h, s, x, y, z;
3331: /* double eps; */
3332: /* #ifndef MSDOS */
3333: /* double e[N]; /\* plenty of stack on a vax *\/ */
3334: /* #endif */
3335: /* double *e; */
3336: /* e=vector(0,n-1); /\* should be freed somewhere but gotos *\/ */
3337:
3338: /* householder's reduction to bidiagonal form */
3339:
3340: if(n==1){
3341: /* q[1-1]=ab[1-1][1-1]; */
3342: /* ab[1-1][1-1]=1.0; */
3343: q[1]=ab[1][1];
3344: ab[1][1]=1.0;
3345: return; /* added from hardt */
3346: }
3347: /* eps=macheps; */ /* added */
3348: x = g = 0.0;
3349: #ifdef DEBUGPRAX
3350: matprint (" HOUSE holder:", ab, n, n);
3351: #endif
3352:
3353: /* for (i=0; i<n; i++) { /\* FOR I := 1 UNTIL N DO *\/ */
3354: for (i=1; i<=n; i++) { /* FOR I := 1 UNTIL N DO */
3355: e[i] = g; s = 0.0; l = i+1;
3356: /* for (j=i; j<n; j++) /\* FOR J := I UNTIL N DO S := S*AB(J,I)**2; *\/ /\* not correct *\/ */
3357: for (j=i; j<=n; j++) /* FOR J := I UNTIL N DO S := S*AB(J,I)**2; */ /* not correct */
3358: s += ab[j][i] * ab[j][i];
3359: #ifdef DEBUGPRAXFIN
3360: printf("i=%d s=%d %.7g tol=%.7g",i,s,tol);
3361: #endif
3362: if (s < tol) {
3363: g = 0.0;
3364: }
3365: else {
3366: /* f = ab[i][i]; */
3367: f = ab[i][i];
3368: if (f < 0.0)
3369: g = sqrt(s);
3370: else
3371: g = -sqrt(s);
3372: /* h = f*g - s; ab[i][i] = f - g; */
3373: h = f*g - s; ab[i][i] = f - g;
3374: /* for (j=l; j<n; j++) { */ /* FOR J := L UNTIL N DO */ /* wrong */
3375: for (j=l; j<=n; j++) {
3376: f = 0.0;
3377: /* for (k=i; k<n; k++) /\* FOR K := I UNTIL N DO *\/ /\* wrong *\/ */
3378: for (k=i; k<=n; k++) /* FOR K := I UNTIL N DO */
3379: /* f += ab[k][i] * ab[k][j]; */
3380: f += ab[k][i] * ab[k][j];
3381: f /= h;
3382: for (k=i; k<=n; k++) /* FOR K := I UNTIL N DO */
3383: /* for (k=i; k<n; k++)/\* FOR K := I UNTIL N DO *\/ /\* wrong *\/ */
3384: ab[k][j] += f * ab[k][i];
3385: /* ab[k][j] += f * ab[k][i]; */
3386: #ifdef DEBUGPRAX
3387: printf("Holder J=%d F=%.7g",j,f);
3388: #endif
3389: }
3390: } /* end s */
3391: /* q[i] = g; s = 0.0; */
3392: q[i] = g; s = 0.0;
3393: #ifdef DEBUGPRAX
3394: printf(" I Q=%d %.7g",i,q[i]);
3395: #endif
3396:
3397: /* if (i < n) */
3398: /* if (i <= n) /\* I is always lower or equal to n wasn't in golub reinsch*\/ */
3399: /* for (j=l; j<n; j++) */
3400: for (j=l; j<=n; j++)
3401: s += ab[i][j] * ab[i][j];
3402: /* s += ab[i][j] * ab[i][j]; */
3403: if (s < tol) {
3404: g = 0.0;
3405: }
3406: else {
3407: if(i<n)
3408: /* f = ab[i][i+1]; */ /* Brent golub overflow */
3409: f = ab[i][i+1];
3410: if (f < 0.0)
3411: g = sqrt(s);
3412: else
3413: g = - sqrt(s);
3414: h = f*g - s;
3415: /* h = f*g - s; ab[i][i+1] = f - g; */ /* Overflow for i=n Error in Golub too but not Burkardt*/
3416: /* for (j=l; j<n; j++) */
3417: /* e[j] = ab[i][j]/h; */
3418: if(i<n){
3419: ab[i][i+1] = f - g;
3420: for (j=l; j<=n; j++)
3421: e[j] = ab[i][j]/h;
3422: /* for (j=l; j<n; j++) { */
3423: for (j=l; j<=n; j++) {
3424: s = 0.0;
3425: /* for (k=l; k<n; k++) s += ab[j][k]*ab[i][k]; */
3426: for (k=l; k<=n; k++) s += ab[j][k]*ab[i][k];
3427: /* for (k=l; k<n; k++) ab[j][k] += s * e[k]; */
3428: for (k=l; k<=n; k++) ab[j][k] += s * e[k];
3429: } /* END J */
3430: } /* END i <n */
3431: } /* end s */
3432: /* y = fabs(q[i]) + fabs(e[i]); */
3433: y = fabs(q[i]) + fabs(e[i]);
3434: if (y > x) x = y;
3435: #ifdef DEBUGPRAX
3436: printf(" I Y=%d %.7g",i,y);
3437: #endif
3438: #ifdef DEBUGPRAX
3439: printf(" i=%d e(i) %.7g",i,e[i]);
3440: #endif
3441: } /* end i */
3442: /*
3443: Accumulation of right hand transformations */
3444: /* for (i=n-1; i >= 0; i--) { */ /* FOR I := N STEP -1 UNTIL 1 DO */
3445: /* We should avoid the overflow in Golub */
3446: /* ab[n-1][n-1] = 1.0; */
3447: /* g = e[n-1]; */
3448: ab[n][n] = 1.0;
3449: g = e[n];
3450: l = n;
3451:
3452: /* for (i=n; i >= 1; i--) { */
3453: for (i=n-1; i >= 1; i--) { /* n-1 loops, different from brent and golub*/
3454: if (g != 0.0) {
3455: /* h = ab[i-1][i]*g; */
3456: h = ab[i][i+1]*g;
3457: for (j=l; j<=n; j++) ab[j][i] = ab[i][j] / h;
3458: for (j=l; j<=n; j++) {
3459: /* h = ab[i][i+1]*g; */
3460: /* for (j=l; j<n; j++) ab[j][i] = ab[i][j] / h; */
3461: /* for (j=l; j<n; j++) { */
3462: s = 0.0;
3463: /* for (k=l; k<n; k++) s += ab[i][k] * ab[k][j]; */
3464: /* for (k=l; k<n; k++) ab[k][j] += s * ab[k][i]; */
3465: for (k=l; k<=n; k++) s += ab[i][k] * ab[k][j];
3466: for (k=l; k<=n; k++) ab[k][j] += s * ab[k][i];
3467: }/* END J */
3468: }/* END G */
3469: /* for (j=l; j<n; j++) */
3470: /* ab[i][j] = ab[j][i] = 0.0; */
3471: /* ab[i][i] = 1.0; g = e[i]; l = i; */
3472: for (j=l; j<=n; j++)
3473: ab[i][j] = ab[j][i] = 0.0;
3474: ab[i][i] = 1.0; g = e[i]; l = i;
3475: }/* END I */
3476: #ifdef DEBUGPRAX
3477: matprint (" HOUSE accumulation:",ab,n, n );
3478: #endif
3479:
3480: /* diagonalization to bidiagonal form */
3481: eps *= x;
3482: /* for (k=n-1; k>= 0; k--) { */
3483: for (k=n; k>= 1; k--) {
3484: kt = 0;
3485: TestFsplitting:
3486: #ifdef DEBUGPRAX
3487: printf(" TestFsplitting: k=%d kt=%d\n",k,kt);
3488: /* for(i=1;i<=n;i++)printf(" e(%d)=%.14f",i,e[i]);printf("\n"); */
3489: #endif
3490: kt = kt+1;
3491: /* TestFsplitting: */
3492: /* if (++kt > 30) { */
3493: if (kt > 30) {
3494: e[k] = 0.0;
3495: fprintf(stderr, "\n+++ MINFIT - Fatal error\n");
3496: fprintf ( stderr, " The QR algorithm failed to converge.\n" );
3497: }
3498: /* for (l2=k; l2>=0; l2--) { */
3499: for (l2=k; l2>=1; l2--) {
3500: l = l2;
3501: #ifdef DEBUGPRAX
3502: printf(" l e(l)< eps %d %.7g %.7g ",l,e[l], eps);
3503: #endif
3504: /* if (fabs(e[l]) <= eps) */
3505: if (fabs(e[l]) <= eps)
3506: goto TestFconvergence;
3507: /* if (fabs(q[l-1]) <= eps)*/ /* missing if ( 1 < l ){ *//* printf(" q(l-1)< eps %d %.7g %.7g ",l-1,q[l-2], eps); */
3508: if (fabs(q[l-1]) <= eps)
3509: break; /* goto Cancellation; */
3510: }
3511: Cancellation:
3512: #ifdef DEBUGPRAX
3513: printf(" Cancellation:\n");
3514: #endif
3515: c = 0.0; s = 1.0;
3516: for (i=l; i<=k; i++) {
3517: f = s * e[i]; e[i] *= c;
3518: /* f = s * e[i]; e[i] *= c; */
3519: if (fabs(f) <= eps)
3520: goto TestFconvergence;
3521: /* g = q[i]; */
3522: g = q[i];
3523: if (fabs(f) < fabs(g)) {
3524: double fg = f/g;
3525: h = fabs(g)*sqrt(1.0+fg*fg);
3526: }
3527: else {
3528: double gf = g/f;
3529: h = (f!=0.0 ? fabs(f)*sqrt(1.0+gf*gf) : 0.0);
3530: }
3531: /* COMMENT: THE ABOVE REPLACES Q(I):=H:=LONGSQRT(G*G+F*F) */
3532: /* WHICH MAY GIVE INCORRECT RESULTS IF THE */
3533: /* SQUARES UNDERFLOW OR IF F = G = 0; */
3534:
3535: /* q[i] = h; */
3536: q[i] = h;
3537: if (h == 0.0) { h = 1.0; g = 1.0; }
3538: c = g/h; s = -f/h;
3539: }
3540: TestFconvergence:
3541: #ifdef DEBUGPRAX
3542: printf(" TestFconvergence: l=%d k=%d\n",l,k);
3543: #endif
3544: /* z = q[k]; */
3545: z = q[k];
3546: if (l == k)
3547: goto Convergence;
3548: /* shift from bottom 2x2 minor */
3549: /* x = q[l]; y = q[k-l]; g = e[k-1]; h = e[k]; */ /* Error */
3550: x = q[l]; y = q[k-1]; g = e[k-1]; h = e[k];
3551: f = ((y-z)*(y+z) + (g-h)*(g+h)) / (2.0*h*y);
3552: g = sqrt(f*f+1.0);
3553: if (f <= 0.0)
3554: f = ((x-z)*(x+z) + h*(y/(f-g)-h))/x;
3555: else
3556: f = ((x-z)*(x+z) + h*(y/(f+g)-h))/x;
3557: /* next qr transformation */
3558: s = c = 1.0;
3559: for (i=l+1; i<=k; i++) {
3560: #ifdef DEBUGPRAXQR
3561: printf(" Before Mid TestFconvergence: l+1=%d i=%d k=%d h=%.6e e(i)=%14.8f e(i-1)=%14.8f\n",l+1,i,k, h, e[i],e[i-1]);
3562: #endif
3563: /* g = e[i]; y = q[i]; h = s*g; g *= c; */
3564: g = e[i]; y = q[i]; h = s*g; g *= c;
3565: if (fabs(f) < fabs(h)) {
3566: double fh = f/h;
3567: z = fabs(h) * sqrt(1.0 + fh*fh);
3568: }
3569: else {
3570: double hf = h/f;
3571: z = (f!=0.0 ? fabs(f)*sqrt(1.0+hf*hf) : 0.0);
3572: }
3573: /* e[i-1] = z; */
3574: e[i-1] = z;
3575: #ifdef DEBUGPRAXQR
3576: printf(" Mid TestFconvergence: l+1=%d i=%d k=%d h=%.6e e(i)=%14.8f e(i-1)=%14.8f\n",l+1,i,k, h, e[i],e[i-1]);
3577: #endif
3578: if (z == 0.0)
3579: f = z = 1.0;
3580: c = f/z; s = h/z;
3581: f = x*c + g*s; g = - x*s + g*c; h = y*s;
3582: y *= c;
3583: /* for (j=0; j<n; j++) { */
3584: /* x = ab[j][i-1]; z = ab[j][i]; */
3585: /* ab[j][i-1] = x*c + z*s; */
3586: /* ab[j][i] = - x*s + z*c; */
3587: /* } */
3588: for (j=1; j<=n; j++) {
3589: x = ab[j][i-1]; z = ab[j][i];
3590: ab[j][i-1] = x*c + z*s;
3591: ab[j][i] = - x*s + z*c;
3592: }
3593: if (fabs(f) < fabs(h)) {
3594: double fh = f/h;
3595: z = fabs(h) * sqrt(1.0 + fh*fh);
3596: }
3597: else {
3598: double hf = h/f;
3599: z = (f!=0.0 ? fabs(f)*sqrt(1.0+hf*hf) : 0.0);
3600: }
3601: #ifdef DEBUGPRAXQR
3602: printf(" qr transformation z f h=%.7g %.7g %.7g i=%d k=%d\n",z,f,h, i, k);
3603: #endif
3604: q[i-1] = z;
3605: if (z == 0.0)
3606: z = f = 1.0;
3607: c = f/z; s = h/z;
3608: f = c*g + s*y; /* f can be very small */
3609: x = - s*g + c*y;
3610: }
3611: /* e[l] = 0.0; e[k] = f; q[k] = x; */
3612: e[l] = 0.0; e[k] = f; q[k] = x;
3613: #ifdef DEBUGPRAXQR
3614: printf(" aftermid loop l=%d k=%d e(l)=%7g e(k)=%.7g q(k)=%.7g x=%.7g\n",l,k,e[l],e[k],q[k],x);
3615: #endif
3616: goto TestFsplitting;
3617: Convergence:
3618: #ifdef DEBUGPRAX
3619: printf(" Convergence:\n");
3620: #endif
3621: if (z < 0.0) {
3622: /* q[k] = - z; */
3623: /* for (j=0; j<n; j++) ab[j][k] = - ab[j][k]; */
3624: q[k] = - z;
3625: for (j=1; j<=n; j++) ab[j][k] = - ab[j][k];
3626: }/* END Z */
3627: }/* END K */
3628: } /* END MINFIT */
3629:
3630:
3631: double praxis(double tol, double macheps, double h0, int _n, int _prin, double *_x, double (*_fun)(double *_x))
3632: /* double praxis(double tol, double macheps, double h0, int _n, int _prin, double *_x, double (*_fun)(double *_x, int _n)) */
3633: /* double praxis(double (*_fun)(), double _x[], int _n) */
3634: /* double (*_fun)(); */
3635: /* double _x[N]; */
3636: /* double (*_fun)(); */
3637: /* double _x[N]; */
3638: {
3639: /* init global extern variables and parameters */
3640: /* double *d, *y, *z, */
3641: /* *q0, *q1, **v; */
3642: /* double *tflin; /\* used in flin: return (*fun)(tflin, n); *\/ */
3643: /* double *e; /\* used in minfit, don't konw how to free memory and thus made global *\/ */
3644:
3645:
3646: int seed; /* added */
3647: int biter=0;
3648: double r;
3649: double randbrent( int (*));
3650: double s, sf;
3651:
3652: h = h0; /* step; */
3653: t = tol;
3654: scbd = 1.0;
3655: illc = 0;
3656: ktm = 1;
3657:
3658: macheps = DBL_EPSILON;
3659: /* prin=4; */
3660: #ifdef DEBUGPRAX
3661: printf("Praxis macheps=%14g h=%14g step=%14g tol=%14g\n",macheps,h, h0,tol);
3662: #endif
3663: n = _n;
3664: x = _x;
3665: prin = _prin;
3666: fun = _fun;
3667: d=vector(1, n);
3668: y=vector(1, n);
3669: z=vector(1, n);
3670: q0=vector(1, n);
3671: q1=vector(1, n);
3672: e=vector(1, n);
3673: tflin=vector(1, n);
3674: v=matrix(1, n, 1, n);
3675: for(i=1;i<=n;i++){d[i]=y[i]=z[i]=q0[0]=e[i]=tflin[i]=0.;}
3676: small_windows = (macheps) * (macheps); vsmall = small_windows*small_windows;
3677: large = 1.0/small_windows; vlarge = 1.0/vsmall;
3678: m2 = sqrt(macheps); m4 = sqrt(m2);
3679: seed = 123456789; /* added */
3680: ldfac = (illc ? 0.1 : 0.01);
3681: for(i=1;i<=n;i++) z[i]=0.; /* Was missing in Gegenfurtner as well as Brent's algol or fortran */
3682: nl = kt = 0; nf = 1;
3683: #ifdef NR_SHIFT
3684: fx = (*fun)((x-1), n);
3685: #else
3686: fx = (*fun)(x);
3687: #endif
3688: qf1 = fx;
3689: t2 = small_windows + fabs(t); t = t2; dmin = small_windows;
3690: #ifdef DEBUGPRAX
3691: printf("praxis2 macheps=%14g h=%14g step=%14g small=%14g t=%14g\n",macheps,h, h0,small_windows, t);
3692: #endif
3693: if (h < 100.0*t) h = 100.0*t;
3694: #ifdef DEBUGPRAX
3695: printf("praxis3 macheps=%14g h=%14g step=%14g small=%14g t=%14g\n",macheps,h, h0,small_windows, t);
3696: #endif
3697: ldt = h;
3698: /* for (i=0; i<n; i++) for (j=0; j<n; j++) */
3699: for (i=1; i<=n; i++) for (j=1; j<=n; j++)
3700: v[i][j] = (i == j ? 1.0 : 0.0);
3701: d[1] = 0.0; qd0 = 0.0;
3702: /* for (i=0; i<n; i++) q1[i] = x[i]; */
3703: for (i=1; i<=n; i++) q1[i] = x[i];
3704: if (prin > 1) {
3705: printf("\n------------- enter function praxis -----------\n");
3706: printf("... current parameter settings ...\n");
3707: printf("... scaling ... %20.10e\n", scbd);
3708: printf("... tol ... %20.10e\n", t);
3709: printf("... maxstep ... %20.10e\n", h);
3710: printf("... illc ... %20u\n", illc);
3711: printf("... ktm ... %20u\n", ktm);
3712: printf("... maxfun ... %20u\n", maxfun);
3713: }
3714: if (prin) print2();
3715:
3716: mloop:
3717: biter++; /* Added to count the loops */
3718: /* sf = d[0]; */
3719: /* s = d[0] = 0.0; */
3720: printf("\n Big iteration %d \n",biter);
3721: fprintf(ficlog,"\n Big iteration %d \n",biter);
3722: sf = d[1];
3723: s = d[1] = 0.0;
3724:
3725: /* minimize along first direction V(*,1) */
3726: #ifdef DEBUGPRAX
3727: printf(" Minimize along the first direction V(*,1). illc=%d\n",illc);
3728: /* fprintf(ficlog," Minimize along the first direction V(*,1).\n"); */
3729: #endif
3730: #ifdef DEBUGPRAX2
3731: printf("praxis4 macheps=%14g h=%14g step=%14g small=%14g t=%14g\n",macheps,h, h0,small_windows, t);
3732: #endif
3733: /* min(0, 2, &d[0], &s, fx, 0); /\* mac heps not global *\/ */
1.362 brouard 3734: minny(1, 2, &d[1], &s, fx, 0); /* mac heps not global it seems that fx doesn't correspond to f(s=*x1) */
1.359 brouard 3735: #ifdef DEBUGPRAX
3736: printf("praxis5 macheps=%14g h=%14g looks at sign of s=%14g fx=%14g\n",macheps,h, s,fx);
3737: #endif
3738: if (s <= 0.0)
3739: /* for (i=0; i < n; i++) */
3740: for (i=1; i <= n; i++)
3741: v[i][1] = -v[i][1];
3742: /* if ((sf <= (0.9 * d[0])) || ((0.9 * sf) >= d[0])) */
3743: if ((sf <= (0.9 * d[1])) || ((0.9 * sf) >= d[1]))
3744: /* for (i=1; i<n; i++) */
3745: for (i=2; i<=n; i++)
3746: d[i] = 0.0;
3747: /* for (k=1; k<n; k++) { */
3748: for (k=2; k<=n; k++) {
3749: /*
3750: The inner loop starts here.
3751: */
3752: #ifdef DEBUGPRAX
3753: printf(" The inner loop here from k=%d to n=%d.\n",k,n);
3754: /* fprintf(ficlog," The inner loop here from k=%d to n=%d.\n",k,n); */
3755: #endif
3756: /* for (i=0; i<n; i++) */
3757: for (i=1; i<=n; i++)
3758: y[i] = x[i];
3759: sf = fx;
3760: #ifdef DEBUGPRAX
3761: printf(" illc=%d and kt=%d and ktm=%d\n", illc, kt, ktm);
3762: #endif
3763: illc = illc || (kt > 0);
3764: next:
3765: kl = k;
3766: df = 0.0;
3767: if (illc) { /* random step to get off resolution valley */
3768: #ifdef DEBUGPRAX
3769: printf(" A random step follows, to avoid resolution valleys.\n");
3770: matprint(" before rand, vectors:",v,n,n);
3771: #endif
3772: for (i=1; i<=n; i++) {
3773: #ifdef NOBRENTRAND
3774: r = drandom();
3775: #else
3776: seed=i;
3777: /* seed=i+1; */
3778: #ifdef DEBUGRAND
3779: printf(" Random seed=%d, brent i=%d",seed,i); /* YYYY i=5 j=1 vji= -0.0001170073 */
3780: #endif
3781: r = randbrent ( &seed );
3782: #endif
3783: #ifdef DEBUGRAND
3784: printf(" Random r=%.7g \n",r);
3785: #endif
3786: z[i] = (0.1 * ldt + t2 * pow(10.0,(double)kt)) * (r - 0.5);
3787: /* z[i] = (0.1 * ldt + t2 * pow(10.0,(double)kt)) * (drandom() - 0.5); */
3788:
3789: s = z[i];
3790: for (j=1; j <= n; j++)
3791: x[j] += s * v[j][i];
3792: }
3793: #ifdef DEBUGRAND
3794: matprint(" after rand, vectors:",v,n,n);
3795: #endif
3796: #ifdef NR_SHIFT
3797: fx = (*fun)((x-1), n);
3798: #else
3799: fx = (*fun)(x, n);
3800: #endif
3801: /* fx = (*func) ( (x-1) ); *//* This for func which is computed from x[1] and not from x[0] xm1=(x-1)*/
3802: nf++;
3803: }
3804: /* minimize along non-conjugate directions */
3805: #ifdef DEBUGPRAX
3806: printf(" Minimize along the 'non-conjugate' directions (dots printed) V(*,%d),...,V(*,%d).\n",k,n);
3807: /* fprintf(ficlog," Minimize along the 'non-conjugate' directions (dots printed) V(*,%d),...,V(*,%d).\n",k,n); */
3808: #endif
3809: /* for (k2=k; k2<n; k2++) { /\* Be careful here k2 <=n ? *\/ */
3810: for (k2=k; k2<=n; k2++) { /* Be careful here k2 <=n ? */
3811: sl = fx;
3812: s = 0.0;
3813: #ifdef DEBUGPRAX
3814: printf(" Minimize along the 'NON-CONJUGATE' true direction k2=%14d fx=%14.7f\n",k2, fx);
3815: matprint(" before min vectors:",v,n,n);
3816: #endif
3817: /* min(k2, 2, &d[k2], &s, fx, 0); */
3818: /* jsearch=k2-1; */
3819: /* min(jsearch, 2, &d[jsearch], &s, fx, 0); */
3820: minny(k2, 2, &d[k2], &s, fx, 0);
3821: #ifdef DEBUGPRAX
3822: printf(" . D(%d)=%14.7f d[k2]=%14.7f z[k2]=%14.7f illc=%14d fx=%14.7f\n",k2,d[k2],d[k2],z[k2],illc,fx);
3823: #endif
3824: if (illc) {
3825: /* double szk = s + z[k2]; */
3826: /* s = d[k2] * szk*szk; */
3827: double szk = s + z[k2];
3828: s = d[k2] * szk*szk;
3829: }
3830: else
3831: s = sl - fx;
3832: /* if (df < s) { */
3833: if (df <= s) {
3834: df = s;
3835: kl = k2;
3836: #ifdef DEBUGPRAX
3837: printf(" df=%.7g and choose kl=%d \n",df,kl); /* UUUU */
3838: #endif
3839: }
3840: } /* end loop k2 */
3841: /*
3842: If there was not much improvement on the first try, set
3843: ILLC = true and start the inner loop again.
3844: */
3845: #ifdef DEBUGPRAX
3846: printf(" If there was not much improvement on the first try, set ILLC = true and start the inner loop again. illc=%d\n",illc);
3847: /* fprintf(ficlog," If there was not much improvement on the first try, set ILLC = true and start the inner loop again.\n"); */
3848: #endif
3849: if (!illc && (df < fabs(100.0 * (macheps) * fx))) {
3850: #ifdef DEBUGPRAX
3851: printf("\n NO SUCCESS because DF is small, starts inner loop with same K(=%d), fabs( 100.0 * machep(=%.10e) * fx(=%.9e) )=%.9e > df(=%.9e) break illc=%d\n", k, macheps, fx, fabs ( 100.0 * macheps * fx ), df, illc);
3852: #endif
3853: illc = 1;
3854: goto next;
3855: }
3856: #ifdef DEBUGPRAX
3857: printf("\n SUCCESS, BREAKS inner loop K(=%d) because DF is big, fabs( 100.0 * machep(=%.10e) * fx(=%.9e) )=%.9e <= df(=%.9e) break illc=%d\n", k, macheps, fx, fabs ( 100.0 * macheps * fx ), df, illc);
3858: #endif
3859:
3860: /* if ((k == 1) && (prin > 1)){ /\* be careful k=2 *\/ */
3861: if ((k == 2) && (prin > 1)){ /* be careful k=2 */
3862: #ifdef DEBUGPRAX
3863: printf(" NEW D The second difference array d:\n" );
3864: /* fprintf(ficlog, " NEW D The second difference array d:\n" ); */
3865: #endif
3866: vecprint(" NEW D The second difference array d:",d,n);
3867: }
3868: /* minimize along conjugate directions */
3869: /*
3870: Minimize along the "conjugate" directions V(*,1),...,V(*,K-1).
3871: */
3872: #ifdef DEBUGPRAX
3873: printf("Minimize along the 'conjugate' directions V(*,1),...,V(*,K-1=%d).\n",k-1);
3874: /* fprintf(ficlog,"Minimize along the 'conjugate' directions V(*,1),...,V(*,K-1=%d).\n",k-1); */
3875: #endif
3876: /* for (k2=0; k2<=k-1; k2++) { */
3877: for (k2=1; k2<=k-1; k2++) {
3878: s = 0.0;
3879: /* min(k2-1, 2, &d[k2-1], &s, fx, 0); */
3880: minny(k2, 2, &d[k2], &s, fx, 0);
3881: }
3882: f1 = fx;
3883: fx = sf;
3884: lds = 0.0;
3885: /* for (i=0; i<n; i++) { */
3886: for (i=1; i<=n; i++) {
3887: sl = x[i];
3888: x[i] = y[i];
3889: y[i] = sl - y[i];
3890: sl = y[i];
3891: lds = lds + sl*sl;
3892: }
3893: lds = sqrt(lds);
3894: #ifdef DEBUGPRAX
3895: printf("Minimization done 'conjugate', shifted all points, computed lds=%.8f\n",lds);
3896: #endif
3897: /*
3898: Discard direction V(*,kl).
3899:
3900: If no random step was taken, V(*,KL) is the "non-conjugate"
3901: direction along which the greatest improvement was made.
3902: */
3903: if (lds > small_windows) {
3904: #ifdef DEBUGPRAX
3905: printf("lds big enough to throw direction V(*,kl=%d). If no random step was taken, V(*,KL) is the 'non-conjugate' direction along which the greatest improvement was made.\n",kl);
3906: matprint(" before shift new conjugate vectors:",v,n,n);
3907: #endif
3908: for (i=kl-1; i>=k; i--) {
3909: /* for (j=0; j < n; j++) */
3910: for (j=1; j <= n; j++)
3911: /* v[j][i+1] = v[j][i]; */ /* This is v[j][i+1]=v[j][i] i=kl-1 to k */
3912: v[j][i+1] = v[j][i]; /* This is v[j][i+1]=v[j][i] i=kl-1 to k */
3913: /* v[j][i+1] = v[j][i]; */
3914: /* d[i+1] = d[i];*/ /* last is d[k+1]= d[k] */
3915: d[i+1] = d[i]; /* last is d[k]= d[k-1] */
3916: }
3917: #ifdef DEBUGPRAX
3918: matprint(" after shift new conjugate vectors:",v,n,n);
3919: #endif /* d[k] = 0.0; */
3920: d[k] = 0.0;
3921: for (i=1; i <= n; i++)
3922: v[i][k] = y[i] / lds;
3923: /* v[i][k] = y[i] / lds; */
3924: #ifdef DEBUGPRAX
3925: printf("Minimize along the new 'conjugate' direction V(*,k=%d), which is the normalized vector: (new x) - (old x). d2=%14.7g lds=%.10f\n",k,d[k],lds);
3926: /* fprintf(ficlog,"Minimize along the new 'conjugate' direction V(*,k=%d), which is the normalized vector: (new x) - (old x).\n",k); */
3927: matprint(" before min new conjugate vectors:",v,n,n);
3928: #endif
3929: /* min(k-1, 4, &d[k-1], &lds, f1, 1); */
3930: minny(k, 4, &d[k], &lds, f1, 1);
3931: #ifdef DEBUGPRAX
3932: printf(" after min d(k)=%d %.7g lds=%14f\n",k,d[k],lds);
3933: matprint(" after min vectors:",v,n,n);
3934: #endif
3935: if (lds <= 0.0) {
3936: lds = -lds;
3937: #ifdef DEBUGPRAX
3938: printf(" lds changed sign lds=%.14f k=%d\n",lds,k);
3939: #endif
3940: /* for (i=0; i<n; i++) */
3941: /* v[i][k] = -v[i][k]; */
3942: for (i=1; i<=n; i++)
3943: v[i][k] = -v[i][k];
3944: }
3945: }
3946: ldt = ldfac * ldt;
3947: if (ldt < lds)
3948: ldt = lds;
3949: if (prin > 0){
3950: #ifdef DEBUGPRAX
3951: printf(" k=%d",k);
3952: /* fprintf(ficlog," k=%d",k); */
3953: #endif
3954: print2();/* n, x, prin, fx, nf, nl ); */
3955: }
3956: t2 = 0.0;
3957: /* for (i=0; i<n; i++) */
3958: for (i=1; i<=n; i++)
3959: t2 += x[i]*x[i];
3960: t2 = m2 * sqrt(t2) + t;
3961: /*
3962: See whether the length of the step taken since starting the
3963: inner loop exceeds half the tolerance.
3964: */
3965: #ifdef DEBUGPRAX
3966: printf("See if step length exceeds half the tolerance.\n"); /* ZZZZZ */
3967: /* fprintf(ficlog,"See if step length exceeds half the tolerance.\n"); */
3968: #endif
3969: if (ldt > (0.5 * t2))
3970: kt = 0;
3971: else
3972: kt++;
3973: #ifdef DEBUGPRAX
3974: printf("if kt=%d >? ktm=%d gotoL2 loop\n",kt,ktm);
3975: #endif
3976: if (kt > ktm){
3977: if ( 0 < prin ){
3978: /* printf("\nr8vec_print\n X:\n"); */
3979: /* fprintf(ficlog,"\nr8vec_print\n X:\n"); */
3980: vecprint ("END X:", x, n );
3981: }
3982: goto fret;
3983: }
3984: #ifdef DEBUGPRAX
3985: matprint(" end of L2 loop vectors:",v,n,n);
3986: #endif
3987:
3988: }
3989: /* printf("The inner loop ends here.\n"); */
3990: /* fprintf(ficlog,"The inner loop ends here.\n"); */
3991: /*
3992: The inner loop ends here.
3993:
3994: Try quadratic extrapolation in case we are in a curved valley.
3995: */
3996: #ifdef DEBUGPRAX
3997: printf("Try QUAD ratic extrapolation in case we are in a curved valley.\n");
3998: #endif
3999: /* try quadratic extrapolation in case */
4000: /* we are stuck in a curved valley */
4001: quad();
4002: dn = 0.0;
4003: /* for (i=0; i<n; i++) { */
4004: for (i=1; i<=n; i++) {
4005: d[i] = 1.0 / sqrt(d[i]);
4006: if (dn < d[i])
4007: dn = d[i];
4008: }
4009: if (prin > 2)
4010: matprint(" NEW DIRECTIONS vectors:",v,n,n);
4011: /* for (j=0; j<n; j++) { */
4012: for (j=1; j<=n; j++) {
4013: s = d[j] / dn;
4014: /* for (i=0; i < n; i++) */
4015: for (i=1; i <= n; i++)
4016: v[i][j] *= s;
4017: }
4018:
4019: if (scbd > 1.0) { /* scale axis to reduce condition number */
4020: #ifdef DEBUGPRAX
4021: printf("Scale the axes to try to reduce the condition number.\n");
4022: #endif
4023: /* fprintf(ficlog,"Scale the axes to try to reduce the condition number.\n"); */
4024: s = vlarge;
4025: /* for (i=0; i<n; i++) { */
4026: for (i=1; i<=n; i++) {
4027: sl = 0.0;
4028: /* for (j=0; j < n; j++) */
4029: for (j=1; j <= n; j++)
4030: sl += v[i][j]*v[i][j];
4031: z[i] = sqrt(sl);
4032: if (z[i] < m4)
4033: z[i] = m4;
4034: if (s > z[i])
4035: s = z[i];
4036: }
4037: /* for (i=0; i<n; i++) { */
4038: for (i=1; i<=n; i++) {
4039: sl = s / z[i];
4040: z[i] = 1.0 / sl;
4041: if (z[i] > scbd) {
4042: sl = 1.0 / scbd;
4043: z[i] = scbd;
4044: }
4045: }
4046: }
4047: for (i=1; i<=n; i++)
4048: /* for (j=0; j<=i-1; j++) { */
4049: /* for (j=1; j<=i; j++) { */
4050: for (j=1; j<=i-1; j++) {
4051: s = v[i][j];
4052: v[i][j] = v[j][i];
4053: v[j][i] = s;
4054: }
4055: #ifdef DEBUGPRAX
4056: printf(" Calculate a new set of orthogonal directions before repeating the main loop.\n Transpose V for MINFIT:...\n");
4057: #endif
4058: /*
4059: MINFIT finds the singular value decomposition of V.
4060:
4061: This gives the principal values and principal directions of the
4062: approximating quadratic form without squaring the condition number.
4063: */
4064: #ifdef DEBUGPRAX
4065: 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");
4066: #endif
4067:
4068: minfit(n, macheps, vsmall, v, d);
4069: /* for(i=0; i<n;i++)printf(" %14.7g",d[i]); */
4070: /* v is overwritten with R. */
4071: /*
4072: Unscale the axes.
4073: */
4074: if (scbd > 1.0) {
4075: #ifdef DEBUGPRAX
4076: printf(" Unscale the axes.\n");
4077: #endif
4078: /* for (i=0; i<n; i++) { */
4079: for (i=1; i<=n; i++) {
4080: s = z[i];
4081: /* for (j=0; j<n; j++) */
4082: for (j=1; j<=n; j++)
4083: v[i][j] *= s;
4084: }
4085: /* for (i=0; i<n; i++) { */
4086: for (i=1; i<=n; i++) {
4087: s = 0.0;
4088: /* for (j=0; j<n; j++) */
4089: for (j=1; j<=n; j++)
4090: s += v[j][i]*v[j][i];
4091: s = sqrt(s);
4092: d[i] *= s;
4093: s = 1.0 / s;
4094: /* for (j=0; j<n; j++) */
4095: for (j=1; j<=n; j++)
4096: v[j][i] *= s;
4097: }
4098: }
4099: /* for (i=0; i<n; i++) { */
4100: double dni; /* added for compatibility with buckhardt but not brent */
4101: for (i=1; i<=n; i++) {
4102: dni=dn*d[i]; /* added for compatibility with buckhardt but not brent */
4103: if ((dn * d[i]) > large)
4104: d[i] = vsmall;
4105: else if ((dn * d[i]) < small_windows)
4106: d[i] = vlarge;
4107: else
4108: d[i] = 1.0 / dni / dni; /* added for compatibility with buckhardt but not brent */
4109: /* d[i] = pow(dn * d[i],-2.0); */
4110: }
4111: #ifdef DEBUGPRAX
4112: vecprint ("\n Before sort Eigenvalues of a:",d,n );
4113: #endif
4114:
4115: sort(); /* the new eigenvalues and eigenvectors */
4116: #ifdef DEBUGPRAX
4117: vecprint( " After sort the eigenvalues ....\n", d, n);
4118: matprint( " After sort the eigenvectors....\n", v, n,n);
4119: #endif
4120: #ifdef DEBUGPRAX
4121: printf(" Determine the smallest eigenvalue.\n");
4122: #endif
4123: /* dmin = d[n-1]; */
4124: dmin = d[n];
4125: if (dmin < small_windows)
4126: dmin = small_windows;
4127: /*
4128: The ratio of the smallest to largest eigenvalue determines whether
4129: the system is ill conditioned.
4130: */
4131:
4132: /* illc = (m2 * d[0]) > dmin; */
4133: illc = (m2 * d[1]) > dmin;
4134: #ifdef DEBUGPRAX
4135: printf(" The ratio of the smallest to largest eigenvalue determines whether\n the system is ill conditioned=%d . dmin=%.10lf < m2=%.10lf * d[1]=%.10lf \n",illc, dmin,m2, d[1]);
4136: #endif
4137:
4138: if ((prin > 2) && (scbd > 1.0))
4139: vecprint("\n The scale factors:",z,n);
4140: if (prin > 2)
4141: vecprint(" Principal values (EIGEN VALUES OF A) of the quadratic form:",d,n);
4142: if (prin > 2)
4143: matprint(" The principal axes (EIGEN VECTORS OF A:",v,n, n);
4144:
4145: if ((maxfun > 0) && (nf > maxfun)) {
4146: if (prin)
4147: printf("\n... maximum number of function calls reached ...\n");
4148: goto fret;
4149: }
4150: #ifdef DEBUGPRAX
4151: printf("Goto main loop\n");
4152: #endif
4153: goto mloop; /* back to main loop */
4154:
4155: fret:
4156: if (prin > 0) {
4157: vecprint("\n X:", x, n);
4158: /* printf("\n... ChiSq reduced to %20.10e ...\n", fx); */
4159: /* printf("... after %20u function calls.\n", nf); */
4160: }
4161: free_vector(d, 1, n);
4162: free_vector(y, 1, n);
4163: free_vector(z, 1, n);
4164: free_vector(q0, 1, n);
4165: free_vector(q1, 1, n);
4166: free_matrix(v, 1, n, 1, n);
4167: /* double *d, *y, *z, */
4168: /* *q0, *q1, **v; */
4169: free_vector(tflin, 1, n);
4170: /* double *tflin; /\* used in flin: return (*fun)(tflin, n); *\/ */
4171: free_vector(e, 1, n);
4172: /* double *e; /\* used in minfit, don't konw how to free memory and thus made global *\/ */
4173:
4174: return(fx);
4175: }
4176:
4177: /* end praxis gegen */
1.126 brouard 4178:
4179: /*************** powell ************************/
1.162 brouard 4180: /*
1.317 brouard 4181: Minimization of a function func of n variables. Input consists in an initial starting point
4182: p[1..n] ; an initial matrix xi[1..n][1..n] whose columns contain the initial set of di-
4183: rections (usually the n unit vectors); and ftol, the fractional tolerance in the function value
4184: such that failure to decrease by more than this amount in one iteration signals doneness. On
1.162 brouard 4185: output, p is set to the best point found, xi is the then-current direction set, fret is the returned
4186: function value at p , and iter is the number of iterations taken. The routine linmin is used.
4187: */
1.224 brouard 4188: #ifdef LINMINORIGINAL
4189: #else
4190: int *flatdir; /* Function is vanishing in that direction */
1.225 brouard 4191: int flat=0, flatd=0; /* Function is vanishing in that direction */
1.224 brouard 4192: #endif
1.126 brouard 4193: void powell(double p[], double **xi, int n, double ftol, int *iter, double *fret,
4194: double (*func)(double []))
4195: {
1.224 brouard 4196: #ifdef LINMINORIGINAL
4197: void linmin(double p[], double xi[], int n, double *fret,
1.126 brouard 4198: double (*func)(double []));
1.224 brouard 4199: #else
1.241 brouard 4200: void linmin(double p[], double xi[], int n, double *fret,
4201: double (*func)(double []),int *flat);
1.224 brouard 4202: #endif
1.239 brouard 4203: int i,ibig,j,jk,k;
1.126 brouard 4204: double del,t,*pt,*ptt,*xit;
1.181 brouard 4205: double directest;
1.126 brouard 4206: double fp,fptt;
4207: double *xits;
4208: int niterf, itmp;
1.349 brouard 4209: int Bigter=0, nBigterf=1;
4210:
1.126 brouard 4211: pt=vector(1,n);
4212: ptt=vector(1,n);
4213: xit=vector(1,n);
4214: xits=vector(1,n);
4215: *fret=(*func)(p);
4216: for (j=1;j<=n;j++) pt[j]=p[j];
1.338 brouard 4217: rcurr_time = time(NULL);
4218: fp=(*fret); /* Initialisation */
1.126 brouard 4219: for (*iter=1;;++(*iter)) {
4220: ibig=0;
4221: del=0.0;
1.157 brouard 4222: rlast_time=rcurr_time;
1.349 brouard 4223: rlast_btime=rcurr_time;
1.157 brouard 4224: /* (void) gettimeofday(&curr_time,&tzp); */
4225: rcurr_time = time(NULL);
4226: curr_time = *localtime(&rcurr_time);
1.337 brouard 4227: /* 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); */
4228: /* 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); */
1.359 brouard 4229: /* Bigter=(*iter - *iter % ncovmodel)/ncovmodel +1; /\* Big iteration, i.e on ncovmodel cycle *\/ */
4230: Bigter=(*iter - (*iter-1) % n)/n +1; /* Big iteration, i.e on ncovmodel cycle */
1.349 brouard 4231: printf("\nPowell iter=%d Big Iter=%d -2*LL=%.12f gain=%.3lg %ld sec. %ld sec.",*iter,Bigter,*fret,fp-*fret, rcurr_time-rlast_time, rcurr_time-rstart_time);fflush(stdout);
4232: fprintf(ficlog,"\nPowell iter=%d Big Iter=%d -2*LL=%.12f gain=%.3lg %ld sec. %ld sec.",*iter,Bigter,*fret,fp-*fret,rcurr_time-rlast_time, rcurr_time-rstart_time); fflush(ficlog);
4233: fprintf(ficrespow,"%d %d %.12f %d",*iter,Bigter, *fret,curr_time.tm_sec-start_time.tm_sec);
1.324 brouard 4234: fp=(*fret); /* From former iteration or initial value */
1.192 brouard 4235: for (i=1;i<=n;i++) {
1.126 brouard 4236: fprintf(ficrespow," %.12lf", p[i]);
4237: }
1.239 brouard 4238: fprintf(ficrespow,"\n");fflush(ficrespow);
4239: printf("\n#model= 1 + age ");
4240: fprintf(ficlog,"\n#model= 1 + age ");
4241: if(nagesqr==1){
1.241 brouard 4242: printf(" + age*age ");
4243: fprintf(ficlog," + age*age ");
1.239 brouard 4244: }
1.362 brouard 4245: for(j=1;j <=ncovmodel-2-nagesqr;j++){
1.239 brouard 4246: if(Typevar[j]==0) {
4247: printf(" + V%d ",Tvar[j]);
4248: fprintf(ficlog," + V%d ",Tvar[j]);
4249: }else if(Typevar[j]==1) {
4250: printf(" + V%d*age ",Tvar[j]);
4251: fprintf(ficlog," + V%d*age ",Tvar[j]);
4252: }else if(Typevar[j]==2) {
4253: printf(" + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
4254: fprintf(ficlog," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
1.349 brouard 4255: }else if(Typevar[j]==3) {
4256: printf(" + V%d*V%d*age ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
4257: fprintf(ficlog," + V%d*V%d*age ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
1.239 brouard 4258: }
4259: }
1.126 brouard 4260: printf("\n");
1.239 brouard 4261: /* printf("12 47.0114589 0.0154322 33.2424412 0.3279905 2.3731903 */
4262: /* 13 -21.5392400 0.1118147 1.2680506 1.2973408 -1.0663662 */
1.126 brouard 4263: fprintf(ficlog,"\n");
1.239 brouard 4264: for(i=1,jk=1; i <=nlstate; i++){
4265: for(k=1; k <=(nlstate+ndeath); k++){
4266: if (k != i) {
4267: printf("%d%d ",i,k);
4268: fprintf(ficlog,"%d%d ",i,k);
4269: for(j=1; j <=ncovmodel; j++){
4270: printf("%12.7f ",p[jk]);
4271: fprintf(ficlog,"%12.7f ",p[jk]);
4272: jk++;
4273: }
4274: printf("\n");
4275: fprintf(ficlog,"\n");
4276: }
4277: }
4278: }
1.241 brouard 4279: if(*iter <=3 && *iter >1){
1.157 brouard 4280: tml = *localtime(&rcurr_time);
4281: strcpy(strcurr,asctime(&tml));
4282: rforecast_time=rcurr_time;
1.126 brouard 4283: itmp = strlen(strcurr);
4284: if(strcurr[itmp-1]=='\n') /* Windows outputs with a new line */
1.241 brouard 4285: strcurr[itmp-1]='\0';
1.162 brouard 4286: printf("\nConsidering the time needed for the last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
1.157 brouard 4287: fprintf(ficlog,"\nConsidering the time needed for this last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
1.349 brouard 4288: for(nBigterf=1;nBigterf<=31;nBigterf+=10){
4289: niterf=nBigterf*ncovmodel;
4290: /* rforecast_time=rcurr_time+(niterf-*iter)*(rcurr_time-rlast_time); */
1.241 brouard 4291: rforecast_time=rcurr_time+(niterf-*iter)*(rcurr_time-rlast_time);
4292: forecast_time = *localtime(&rforecast_time);
4293: strcpy(strfor,asctime(&forecast_time));
4294: itmp = strlen(strfor);
4295: if(strfor[itmp-1]=='\n')
4296: strfor[itmp-1]='\0';
1.349 brouard 4297: printf(" - if your program needs %d BIG iterations (%d iterations) to converge, convergence will be \n reached in %s i.e.\n on %s (current time is %s);\n",nBigterf, niterf, asc_diff_time(rforecast_time-rcurr_time,tmpout),strfor,strcurr);
4298: fprintf(ficlog," - if your program needs %d BIG iterations (%d iterations) to converge, convergence will be \n reached in %s i.e.\n on %s (current time is %s);\n",nBigterf, niterf, asc_diff_time(rforecast_time-rcurr_time,tmpout),strfor,strcurr);
1.126 brouard 4299: }
4300: }
1.359 brouard 4301: for (i=1;i<=n;i++) { /* For each direction i, maximisation after loading directions */
4302: for (j=1;j<=n;j++) xit[j]=xi[j][i]; /* Directions stored from previous iteration with previous scales. xi is not changed but one dim xit */
4303:
4304: fptt=(*fret); /* Computes likelihood for parameters xit */
1.126 brouard 4305: #ifdef DEBUG
1.203 brouard 4306: printf("fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
4307: fprintf(ficlog, "fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
1.126 brouard 4308: #endif
1.203 brouard 4309: printf("%d",i);fflush(stdout); /* print direction (parameter) i */
1.126 brouard 4310: fprintf(ficlog,"%d",i);fflush(ficlog);
1.224 brouard 4311: #ifdef LINMINORIGINAL
1.359 brouard 4312: linmin(p,xit,n,fret,func); /* New point i minimizing in direction xit, i has coordinates p[j].*/
1.357 brouard 4313: /* xit[j] gives the n coordinates of direction i as input.*/
4314: /* *fret gives the maximum value on direction xit */
1.224 brouard 4315: #else
4316: linmin(p,xit,n,fret,func,&flat); /* Point p[n]. xit[n] has been loaded for direction i as input.*/
1.359 brouard 4317: flatdir[i]=flat; /* Function is vanishing in that direction i */
1.224 brouard 4318: #endif
1.359 brouard 4319: /* Outputs are fret(new point p) p is updated and xit rescaled */
1.188 brouard 4320: if (fabs(fptt-(*fret)) > del) { /* We are keeping the max gain on each of the n directions */
1.359 brouard 4321: /* because that direction will be replaced unless the gain del is small */
4322: /* in comparison with the 'probable' gain, mu^2, with the last average direction. */
4323: /* Unless the n directions are conjugate some gain in the determinant may be obtained */
4324: /* with the new direction. */
4325: del=fabs(fptt-(*fret));
4326: ibig=i;
1.126 brouard 4327: }
4328: #ifdef DEBUG
4329: printf("%d %.12e",i,(*fret));
4330: fprintf(ficlog,"%d %.12e",i,(*fret));
4331: for (j=1;j<=n;j++) {
1.359 brouard 4332: xits[j]=FMAX(fabs(p[j]-pt[j]),1.e-5);
4333: printf(" x(%d)=%.12e",j,xit[j]);
4334: fprintf(ficlog," x(%d)=%.12e",j,xit[j]);
1.126 brouard 4335: }
4336: for(j=1;j<=n;j++) {
1.359 brouard 4337: printf(" p(%d)=%.12e",j,p[j]);
4338: fprintf(ficlog," p(%d)=%.12e",j,p[j]);
1.126 brouard 4339: }
4340: printf("\n");
4341: fprintf(ficlog,"\n");
4342: #endif
1.187 brouard 4343: } /* end loop on each direction i */
1.357 brouard 4344: /* Convergence test will use last linmin estimation (fret) and compare to former iteration (fp) */
1.188 brouard 4345: /* But p and xit have been updated at the end of linmin, *fret corresponds to new p, xit */
1.359 brouard 4346: /* New value of last point Pn is not computed, P(n-1) */
1.319 brouard 4347: for(j=1;j<=n;j++) {
4348: if(flatdir[j] >0){
4349: printf(" p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
4350: fprintf(ficlog," p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
1.302 brouard 4351: }
1.319 brouard 4352: /* printf("\n"); */
4353: /* fprintf(ficlog,"\n"); */
4354: }
1.243 brouard 4355: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret))) { /\* Did we reach enough precision? *\/ */
4356: if (2.0*fabs(fp-(*fret)) <= ftol) { /* Did we reach enough precision? */
1.188 brouard 4357: /* We could compare with a chi^2. chisquare(0.95,ddl=1)=3.84 */
4358: /* By adding age*age in a model, the new -2LL should be lower and the difference follows a */
4359: /* a chisquare statistics with 1 degree. To be significant at the 95% level, it should have */
4360: /* decreased of more than 3.84 */
4361: /* By adding age*age and V1*age the gain (-2LL) should be more than 5.99 (ddl=2) */
4362: /* By using V1+V2+V3, the gain should be 7.82, compared with basic 1+age. */
4363: /* By adding 10 parameters more the gain should be 18.31 */
1.224 brouard 4364:
1.188 brouard 4365: /* Starting the program with initial values given by a former maximization will simply change */
4366: /* the scales of the directions and the directions, because the are reset to canonical directions */
4367: /* Thus the first calls to linmin will give new points and better maximizations until fp-(*fret) is */
4368: /* under the tolerance value. If the tolerance is very small 1.e-9, it could last long. */
1.126 brouard 4369: #ifdef DEBUG
4370: int k[2],l;
4371: k[0]=1;
4372: k[1]=-1;
4373: printf("Max: %.12e",(*func)(p));
4374: fprintf(ficlog,"Max: %.12e",(*func)(p));
4375: for (j=1;j<=n;j++) {
4376: printf(" %.12e",p[j]);
4377: fprintf(ficlog," %.12e",p[j]);
4378: }
4379: printf("\n");
4380: fprintf(ficlog,"\n");
4381: for(l=0;l<=1;l++) {
4382: for (j=1;j<=n;j++) {
4383: ptt[j]=p[j]+(p[j]-pt[j])*k[l];
4384: printf("l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
4385: fprintf(ficlog,"l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
4386: }
4387: printf("func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
4388: fprintf(ficlog,"func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
4389: }
4390: #endif
4391:
4392: free_vector(xit,1,n);
4393: free_vector(xits,1,n);
4394: free_vector(ptt,1,n);
4395: free_vector(pt,1,n);
4396: return;
1.192 brouard 4397: } /* enough precision */
1.240 brouard 4398: if (*iter == ITMAX*n) nrerror("powell exceeding maximum iterations.");
1.359 brouard 4399: for (j=1;j<=n;j++) { /* Computes the extrapolated point and value f3, P_0 + 2 (P_n-P_0)=2Pn-P0 and xit is direction Pn-P0 */
1.126 brouard 4400: ptt[j]=2.0*p[j]-pt[j];
1.359 brouard 4401: xit[j]=p[j]-pt[j]; /* Coordinate j of last direction xi_n=P_n-P_0 */
4402: #ifdef DEBUG
4403: printf("\n %d xit=%12.7g p=%12.7g pt=%12.7g ",j,xit[j],p[j],pt[j]);
4404: #endif
4405: pt[j]=p[j]; /* New P0 is Pn */
4406: }
4407: #ifdef DEBUG
4408: printf("\n");
4409: #endif
1.181 brouard 4410: fptt=(*func)(ptt); /* f_3 */
1.359 brouard 4411: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in directions until some iterations are done */
1.224 brouard 4412: if (*iter <=4) {
1.225 brouard 4413: #else
4414: #endif
1.224 brouard 4415: #ifdef POWELLNOF3INFF1TEST /* skips test F3 <F1 */
1.192 brouard 4416: #else
1.161 brouard 4417: if (fptt < fp) { /* If extrapolated point is better, decide if we keep that new direction or not */
1.192 brouard 4418: #endif
1.162 brouard 4419: /* (x1 f1=fp), (x2 f2=*fret), (x3 f3=fptt), (xm fm) */
1.161 brouard 4420: /* From x1 (P0) distance of x2 is at h and x3 is 2h */
1.162 brouard 4421: /* Let f"(x2) be the 2nd derivative equal everywhere. */
4422: /* Then the parabolic through (x1,f1), (x2,f2) and (x3,f3) */
4423: /* will reach at f3 = fm + h^2/2 f"m ; f" = (f1 -2f2 +f3 ) / h**2 */
1.224 brouard 4424: /* Conditional for using this new direction is that mu^2 = (f1-2f2+f3)^2 /2 < del or directest <0 */
4425: /* also lamda^2=(f1-f2)^2/mu² is a parasite solution of powell */
4426: /* For powell, inclusion of this average direction is only if t(del)<0 or del inbetween mu^2 and lambda^2 */
1.161 brouard 4427: /* t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)-del*SQR(fp-fptt); */
1.224 brouard 4428: /* Even if f3 <f1, directest can be negative and t >0 */
4429: /* mu² and del² are equal when f3=f1 */
1.359 brouard 4430: /* f3 < f1 : mu² < del <= lambda^2 both test are equivalent */
4431: /* f3 < f1 : mu² < lambda^2 < del then directtest is negative and powell t is positive */
4432: /* f3 > f1 : lambda² < mu^2 < del then t is negative and directest >0 */
4433: /* f3 > f1 : lambda² < del < mu^2 then t is positive and directest >0 */
1.183 brouard 4434: #ifdef NRCORIGINAL
4435: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)- del*SQR(fp-fptt); /* Original Numerical Recipes in C*/
4436: #else
4437: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del); /* Intel compiler doesn't work on one line; bug reported */
1.161 brouard 4438: t= t- del*SQR(fp-fptt);
1.183 brouard 4439: #endif
1.202 brouard 4440: directest = fp-2.0*(*fret)+fptt - 2.0 * del; /* If delta was big enough we change it for a new direction */
1.161 brouard 4441: #ifdef DEBUG
1.181 brouard 4442: 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);
4443: fprintf(ficlog,"t1= %.12lf, t2= %.12lf, t=%.12lf directest=%.12lf\n", 2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del),del*SQR(fp-fptt),t,directest);
1.161 brouard 4444: printf("t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
4445: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
4446: fprintf(ficlog,"t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
4447: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
4448: 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);
4449: 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);
4450: #endif
1.183 brouard 4451: #ifdef POWELLORIGINAL
4452: if (t < 0.0) { /* Then we use it for new direction */
1.361 brouard 4453: #else /* Not POWELLOriginal but Brouard's */
1.182 brouard 4454: if (directest*t < 0.0) { /* Contradiction between both tests */
1.359 brouard 4455: printf("directest= %.12lf (if <0 we include P0 Pn as new direction), t= %.12lf, f1= %.12lf,f2= %.12lf,f3= %.12lf, del= %.12lf\n",directest, t, fp,(*fret),fptt,del);
1.192 brouard 4456: printf("f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt);
1.224 brouard 4457: fprintf(ficlog,"directest= %.12lf (if directest<0 or t<0 we include P0 Pn as new direction), t= %.12lf, f1= %.12lf,f2= %.12lf,f3= %.12lf, del= %.12lf\n",directest, t, fp,(*fret),fptt, del);
1.192 brouard 4458: fprintf(ficlog,"f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt);
4459: }
1.361 brouard 4460: if (directest < 0.0) { /* Then we use (P0, Pn) for new direction Xi_n or Xi_iBig */
1.181 brouard 4461: #endif
1.191 brouard 4462: #ifdef DEBUGLINMIN
1.234 brouard 4463: printf("Before linmin in direction P%d-P0\n",n);
4464: for (j=1;j<=n;j++) {
4465: printf(" Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
4466: fprintf(ficlog," Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
4467: if(j % ncovmodel == 0){
4468: printf("\n");
4469: fprintf(ficlog,"\n");
4470: }
4471: }
1.224 brouard 4472: #endif
4473: #ifdef LINMINORIGINAL
1.234 brouard 4474: linmin(p,xit,n,fret,func); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
1.224 brouard 4475: #else
1.234 brouard 4476: linmin(p,xit,n,fret,func,&flat); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
4477: flatdir[i]=flat; /* Function is vanishing in that direction i */
1.191 brouard 4478: #endif
1.234 brouard 4479:
1.191 brouard 4480: #ifdef DEBUGLINMIN
1.234 brouard 4481: for (j=1;j<=n;j++) {
4482: printf("After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
4483: fprintf(ficlog,"After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
4484: if(j % ncovmodel == 0){
4485: printf("\n");
4486: fprintf(ficlog,"\n");
4487: }
4488: }
1.224 brouard 4489: #endif
1.234 brouard 4490: for (j=1;j<=n;j++) {
4491: xi[j][ibig]=xi[j][n]; /* Replace direction with biggest decrease by last direction n */
4492: xi[j][n]=xit[j]; /* and this nth direction by the by the average p_0 p_n */
4493: }
1.361 brouard 4494:
4495: /* #else */
4496: /* for (i=1;i<=n-1;i++) { */
4497: /* for (j=1;j<=n;j++) { */
4498: /* xi[j][i]=xi[j][i+1]; /\* Standard method of conjugate directions, not Powell who changes the nth direction by p0 pn . *\/ */
4499: /* } */
4500: /* } */
4501: /* for (j=1;j<=n;j++) { */
4502: /* xi[j][n]=xit[j]; /\* and this nth direction by the by the average p_0 p_n *\/ */
4503: /* } */
4504: /* /\* for (j=1;j<=n-1;j++) { *\/ */
4505: /* /\* xi[j][1]=xi[j][j+1]; /\\* Standard method of conjugate directions *\\/ *\/ */
4506: /* /\* xi[j][n]=xit[j]; /\\* and this nth direction by the by the average p_0 p_n *\\/ *\/ */
4507: /* /\* } *\/ */
4508: /* #endif */
1.224 brouard 4509: #ifdef LINMINORIGINAL
4510: #else
1.234 brouard 4511: for (j=1, flatd=0;j<=n;j++) {
4512: if(flatdir[j]>0)
4513: flatd++;
4514: }
4515: if(flatd >0){
1.255 brouard 4516: printf("%d flat directions: ",flatd);
4517: fprintf(ficlog,"%d flat directions :",flatd);
1.234 brouard 4518: for (j=1;j<=n;j++) {
4519: if(flatdir[j]>0){
4520: printf("%d ",j);
4521: fprintf(ficlog,"%d ",j);
4522: }
4523: }
4524: printf("\n");
4525: fprintf(ficlog,"\n");
1.319 brouard 4526: #ifdef FLATSUP
4527: free_vector(xit,1,n);
4528: free_vector(xits,1,n);
4529: free_vector(ptt,1,n);
4530: free_vector(pt,1,n);
4531: return;
4532: #endif
1.361 brouard 4533: } /* endif(flatd >0) */
4534: #endif /* LINMINORIGINAL */
1.234 brouard 4535: printf("Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
4536: fprintf(ficlog,"Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
4537:
1.126 brouard 4538: #ifdef DEBUG
1.234 brouard 4539: printf("Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
4540: fprintf(ficlog,"Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
4541: for(j=1;j<=n;j++){
4542: printf(" %lf",xit[j]);
4543: fprintf(ficlog," %lf",xit[j]);
4544: }
4545: printf("\n");
4546: fprintf(ficlog,"\n");
1.126 brouard 4547: #endif
1.192 brouard 4548: } /* end of t or directest negative */
1.359 brouard 4549: printf(" Directest is positive, P_n-P_0 does not increase the conjugacy. n=%d\n",n);
4550: fprintf(ficlog," Directest is positive, P_n-P_0 does not increase the conjugacy. n=%d\n",n);
1.224 brouard 4551: #ifdef POWELLNOF3INFF1TEST
1.192 brouard 4552: #else
1.234 brouard 4553: } /* end if (fptt < fp) */
1.192 brouard 4554: #endif
1.225 brouard 4555: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */
1.234 brouard 4556: } /*NODIRECTIONCHANGEDUNTILNITER No change in drections until some iterations are done */
1.225 brouard 4557: #else
1.224 brouard 4558: #endif
1.234 brouard 4559: } /* loop iteration */
1.126 brouard 4560: }
1.234 brouard 4561:
1.126 brouard 4562: /**** Prevalence limit (stable or period prevalence) ****************/
1.234 brouard 4563:
1.235 brouard 4564: double **prevalim(double **prlim, int nlstate, double x[], double age, double **oldm, double **savm, double ftolpl, int *ncvyear, int ij, int nres)
1.234 brouard 4565: {
1.338 brouard 4566: /**< Computes the prevalence limit in each live state at age x and for covariate combination ij . Nicely done
1.279 brouard 4567: * (and selected quantitative values in nres)
4568: * by left multiplying the unit
4569: * matrix by transitions matrix until convergence is reached with precision ftolpl
4570: * Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I
4571: * Wx is row vector: population in state 1, population in state 2, population dead
4572: * or prevalence in state 1, prevalence in state 2, 0
4573: * newm is the matrix after multiplications, its rows are identical at a factor.
4574: * Inputs are the parameter, age, a tolerance for the prevalence limit ftolpl.
4575: * Output is prlim.
4576: * Initial matrix pimij
4577: */
1.206 brouard 4578: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
4579: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
4580: /* 0, 0 , 1} */
4581: /*
4582: * and after some iteration: */
4583: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
4584: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
4585: /* 0, 0 , 1} */
4586: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
4587: /* {0.51571254859325999, 0.4842874514067399, */
4588: /* 0.51326036147820708, 0.48673963852179264} */
4589: /* If we start from prlim again, prlim tends to a constant matrix */
1.234 brouard 4590:
1.332 brouard 4591: int i, ii,j,k, k1;
1.209 brouard 4592: double *min, *max, *meandiff, maxmax,sumnew=0.;
1.145 brouard 4593: /* double **matprod2(); */ /* test */
1.218 brouard 4594: double **out, cov[NCOVMAX+1], **pmij(); /* **pmmij is a global variable feeded with oldms etc */
1.126 brouard 4595: double **newm;
1.209 brouard 4596: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
1.203 brouard 4597: int ncvloop=0;
1.288 brouard 4598: int first=0;
1.169 brouard 4599:
1.209 brouard 4600: min=vector(1,nlstate);
4601: max=vector(1,nlstate);
4602: meandiff=vector(1,nlstate);
4603:
1.218 brouard 4604: /* Starting with matrix unity */
1.126 brouard 4605: for (ii=1;ii<=nlstate+ndeath;ii++)
4606: for (j=1;j<=nlstate+ndeath;j++){
4607: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
4608: }
1.169 brouard 4609:
4610: cov[1]=1.;
4611:
4612: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
1.202 brouard 4613: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
1.126 brouard 4614: for(agefin=age-stepm/YEARM; agefin>=age-delaymax; agefin=agefin-stepm/YEARM){
1.202 brouard 4615: ncvloop++;
1.126 brouard 4616: newm=savm;
4617: /* Covariates have to be included here again */
1.138 brouard 4618: cov[2]=agefin;
1.319 brouard 4619: if(nagesqr==1){
4620: cov[3]= agefin*agefin;
4621: }
1.332 brouard 4622: /* Model(2) V1 + V2 + V3 + V8 + V7*V8 + V5*V6 + V8*age + V3*age + age*age */
4623: /* total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age */
4624: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
1.349 brouard 4625: if(Typevar[k1]==1 || Typevar[k1]==3){ /* A product with age */
1.332 brouard 4626: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
4627: }else{
4628: cov[2+nagesqr+k1]=precov[nres][k1];
4629: }
4630: }/* End of loop on model equation */
4631:
4632: /* Start of old code (replaced by a loop on position in the model equation */
4633: /* for (k=1; k<=nsd;k++) { /\* For single dummy covariates only of the model *\/ */
4634: /* /\* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates *\/ */
4635: /* /\* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,TvarsD[k])]; *\/ */
4636: /* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,TnsdVar[TvarsD[k]])]; */
4637: /* /\* model = 1 +age + V1*V3 + age*V1 + V2 + V1 + age*V2 + V3 + V3*age + V1*V2 */
4638: /* * k 1 2 3 4 5 6 7 8 */
4639: /* *cov[] 1 2 3 4 5 6 7 8 9 10 */
4640: /* *TypeVar[k] 2 1 0 0 1 0 1 2 */
4641: /* *Dummy[k] 0 2 0 0 2 0 2 0 */
4642: /* *Tvar[k] 4 1 2 1 2 3 3 5 */
4643: /* *nsd=3 (1) (2) (3) */
4644: /* *TvarsD[nsd] [1]=2 1 3 */
4645: /* *TnsdVar [2]=2 [1]=1 [3]=3 */
4646: /* *TvarsDind[nsd](=k) [1]=3 [2]=4 [3]=6 */
4647: /* *Tage[] [1]=1 [2]=2 [3]=3 */
4648: /* *Tvard[] [1][1]=1 [2][1]=1 */
4649: /* * [1][2]=3 [2][2]=2 */
4650: /* *Tprod[](=k) [1]=1 [2]=8 */
4651: /* *TvarsDp(=Tvar) [1]=1 [2]=2 [3]=3 [4]=5 */
4652: /* *TvarD (=k) [1]=1 [2]=3 [3]=4 [3]=6 [4]=6 */
4653: /* *TvarsDpType */
4654: /* *si model= 1 + age + V3 + V2*age + V2 + V3*age */
4655: /* * nsd=1 (1) (2) */
4656: /* *TvarsD[nsd] 3 2 */
4657: /* *TnsdVar (3)=1 (2)=2 */
4658: /* *TvarsDind[nsd](=k) [1]=1 [2]=3 */
4659: /* *Tage[] [1]=2 [2]= 3 */
4660: /* *\/ */
4661: /* /\* cov[++k1]=nbcode[TvarsD[k]][codtabm(ij,k)]; *\/ */
4662: /* /\* 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)); *\/ */
4663: /* } */
4664: /* for (k=1; k<=nsq;k++) { /\* For single quantitative varying covariates only of the model *\/ */
4665: /* /\* Here comes the value of quantitative after renumbering k with single quantitative covariates *\/ */
4666: /* /\* Tqresult[nres][result_position]= value of the variable at the result_position in the nres resultline *\/ */
4667: /* /\* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; *\/ */
4668: /* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][resultmodel[nres][k1]] */
4669: /* /\* cov[++k1]=Tqresult[nres][k]; *\/ */
4670: /* /\* 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]); *\/ */
4671: /* } */
4672: /* for (k=1; k<=cptcovage;k++){ /\* For product with age *\/ */
4673: /* if(Dummy[Tage[k]]==2){ /\* dummy with age *\/ */
4674: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
4675: /* /\* cov[++k1]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; *\/ */
4676: /* } else if(Dummy[Tage[k]]==3){ /\* quantitative with age *\/ */
4677: /* cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; */
4678: /* /\* cov[++k1]=Tqresult[nres][k]; *\/ */
4679: /* } */
4680: /* /\* 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]); *\/ */
4681: /* } */
4682: /* for (k=1; k<=cptcovprod;k++){ /\* For product without age *\/ */
4683: /* /\* 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]); *\/ */
4684: /* if(Dummy[Tvard[k][1]]==0){ */
4685: /* if(Dummy[Tvard[k][2]]==0){ */
4686: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
4687: /* /\* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
4688: /* }else{ */
4689: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * Tqresult[nres][k]; */
4690: /* /\* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * Tqresult[nres][k]; *\/ */
4691: /* } */
4692: /* }else{ */
4693: /* if(Dummy[Tvard[k][2]]==0){ */
4694: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])] * Tqinvresult[nres][Tvard[k][1]]; */
4695: /* /\* cov[++k1]=nbcode[Tvard[k][2]][codtabm(ij,k)] * Tqinvresult[nres][Tvard[k][1]]; *\/ */
4696: /* }else{ */
4697: /* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; */
4698: /* /\* cov[++k1]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; *\/ */
4699: /* } */
4700: /* } */
4701: /* } /\* End product without age *\/ */
4702: /* ENd of old code */
1.138 brouard 4703: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
4704: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
4705: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
1.145 brouard 4706: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
4707: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
1.319 brouard 4708: /* age and covariate values of ij are in 'cov' */
1.142 brouard 4709: out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /* Bug Valgrind */
1.138 brouard 4710:
1.126 brouard 4711: savm=oldm;
4712: oldm=newm;
1.209 brouard 4713:
4714: for(j=1; j<=nlstate; j++){
4715: max[j]=0.;
4716: min[j]=1.;
4717: }
4718: for(i=1;i<=nlstate;i++){
4719: sumnew=0;
4720: for(k=1; k<=ndeath; k++) sumnew+=newm[i][nlstate+k];
4721: for(j=1; j<=nlstate; j++){
4722: prlim[i][j]= newm[i][j]/(1-sumnew);
4723: max[j]=FMAX(max[j],prlim[i][j]);
4724: min[j]=FMIN(min[j],prlim[i][j]);
4725: }
4726: }
4727:
1.126 brouard 4728: maxmax=0.;
1.209 brouard 4729: for(j=1; j<=nlstate; j++){
4730: meandiff[j]=(max[j]-min[j])/(max[j]+min[j])*2.; /* mean difference for each column */
4731: maxmax=FMAX(maxmax,meandiff[j]);
4732: /* printf(" age= %d meandiff[%d]=%f, agefin=%d max[%d]=%f min[%d]=%f maxmax=%f\n", (int)age, j, meandiff[j],(int)agefin, j, max[j], j, min[j],maxmax); */
1.169 brouard 4733: } /* j loop */
1.203 brouard 4734: *ncvyear= (int)age- (int)agefin;
1.208 brouard 4735: /* printf("maxmax=%lf maxmin=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, maxmin, ncvloop, (int)age, (int)agefin, *ncvyear); */
1.126 brouard 4736: if(maxmax < ftolpl){
1.209 brouard 4737: /* printf("maxmax=%lf ncvloop=%ld, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
4738: free_vector(min,1,nlstate);
4739: free_vector(max,1,nlstate);
4740: free_vector(meandiff,1,nlstate);
1.126 brouard 4741: return prlim;
4742: }
1.288 brouard 4743: } /* agefin loop */
1.208 brouard 4744: /* After some age loop it doesn't converge */
1.288 brouard 4745: if(!first){
4746: first=1;
4747: 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);
1.317 brouard 4748: 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);
4749: }else if (first >=1 && first <10){
4750: 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);
4751: first++;
4752: }else if (first ==10){
4753: 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);
4754: 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");
4755: fprintf(ficlog,"Warning: the stable prevalence no convergence; too many cases, giving up noticing, even in log file\n");
4756: first++;
1.288 brouard 4757: }
4758:
1.359 brouard 4759: /* Try to lower 'ftol', for example from 1.e-8 to 6.e-9.\n", ftolpl,
4760: * (int)age, (int)delaymax, (int)agefin, ncvloop,
4761: * (int)age-(int)agefin); */
1.209 brouard 4762: free_vector(min,1,nlstate);
4763: free_vector(max,1,nlstate);
4764: free_vector(meandiff,1,nlstate);
1.208 brouard 4765:
1.169 brouard 4766: return prlim; /* should not reach here */
1.126 brouard 4767: }
4768:
1.217 brouard 4769:
4770: /**** Back Prevalence limit (stable or period prevalence) ****************/
4771:
1.218 brouard 4772: /* 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) */
4773: /* 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) */
1.242 brouard 4774: double **bprevalim(double **bprlim, double ***prevacurrent, int nlstate, double x[], double age, double ftolpl, int *ncvyear, int ij, int nres)
1.217 brouard 4775: {
1.264 brouard 4776: /* Computes the prevalence limit in each live state at age x and for covariate combination ij (<=2**cptcoveff) by left multiplying the unit
1.217 brouard 4777: matrix by transitions matrix until convergence is reached with precision ftolpl */
4778: /* Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I */
4779: /* Wx is row vector: population in state 1, population in state 2, population dead */
4780: /* or prevalence in state 1, prevalence in state 2, 0 */
4781: /* newm is the matrix after multiplications, its rows are identical at a factor */
4782: /* Initial matrix pimij */
4783: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
4784: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
4785: /* 0, 0 , 1} */
4786: /*
4787: * and after some iteration: */
4788: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
4789: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
4790: /* 0, 0 , 1} */
4791: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
4792: /* {0.51571254859325999, 0.4842874514067399, */
4793: /* 0.51326036147820708, 0.48673963852179264} */
4794: /* If we start from prlim again, prlim tends to a constant matrix */
4795:
1.359 brouard 4796: int i, ii,j, k1;
1.247 brouard 4797: int first=0;
1.217 brouard 4798: double *min, *max, *meandiff, maxmax,sumnew=0.;
4799: /* double **matprod2(); */ /* test */
4800: double **out, cov[NCOVMAX+1], **bmij();
4801: double **newm;
1.218 brouard 4802: double **dnewm, **doldm, **dsavm; /* for use */
4803: double **oldm, **savm; /* for use */
4804:
1.217 brouard 4805: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
4806: int ncvloop=0;
4807:
4808: min=vector(1,nlstate);
4809: max=vector(1,nlstate);
4810: meandiff=vector(1,nlstate);
4811:
1.266 brouard 4812: dnewm=ddnewms; doldm=ddoldms; dsavm=ddsavms;
4813: oldm=oldms; savm=savms;
4814:
4815: /* Starting with matrix unity */
4816: for (ii=1;ii<=nlstate+ndeath;ii++)
4817: for (j=1;j<=nlstate+ndeath;j++){
1.217 brouard 4818: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
4819: }
4820:
4821: cov[1]=1.;
4822:
4823: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
4824: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
1.218 brouard 4825: /* for(agefin=age+stepm/YEARM; agefin<=age+delaymax; agefin=agefin+stepm/YEARM){ /\* A changer en age *\/ */
1.288 brouard 4826: /* for(agefin=age; agefin<AGESUP; agefin=agefin+stepm/YEARM){ /\* A changer en age *\/ */
4827: for(agefin=age; agefin<FMIN(AGESUP,age+delaymax); agefin=agefin+stepm/YEARM){ /* A changer en age */
1.217 brouard 4828: ncvloop++;
1.218 brouard 4829: newm=savm; /* oldm should be kept from previous iteration or unity at start */
4830: /* newm points to the allocated table savm passed by the function it can be written, savm could be reallocated */
1.217 brouard 4831: /* Covariates have to be included here again */
4832: cov[2]=agefin;
1.319 brouard 4833: if(nagesqr==1){
1.217 brouard 4834: cov[3]= agefin*agefin;;
1.319 brouard 4835: }
1.332 brouard 4836: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
1.349 brouard 4837: if(Typevar[k1]==1 || Typevar[k1]==3){ /* A product with age */
1.332 brouard 4838: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
1.242 brouard 4839: }else{
1.332 brouard 4840: cov[2+nagesqr+k1]=precov[nres][k1];
1.242 brouard 4841: }
1.332 brouard 4842: }/* End of loop on model equation */
4843:
4844: /* Old code */
4845:
4846: /* for (k=1; k<=nsd;k++) { /\* For single dummy covariates only *\/ */
4847: /* /\* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates *\/ */
4848: /* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,TvarsD[k])]; */
4849: /* /\* 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)); *\/ */
4850: /* } */
4851: /* /\* for (k=1; k<=cptcovn;k++) { *\/ */
4852: /* /\* /\\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; *\\/ *\/ */
4853: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; *\/ */
4854: /* /\* /\\* 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])]); *\\/ *\/ */
4855: /* /\* } *\/ */
4856: /* for (k=1; k<=nsq;k++) { /\* For single varying covariates only *\/ */
4857: /* /\* Here comes the value of quantitative after renumbering k with single quantitative covariates *\/ */
4858: /* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; */
4859: /* /\* 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]); *\/ */
4860: /* } */
4861: /* /\* for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,k)]*cov[2]; *\/ */
4862: /* /\* for (k=1; k<=cptcovprod;k++) /\\* Useless *\\/ *\/ */
4863: /* /\* /\\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; *\\/ *\/ */
4864: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
4865: /* for (k=1; k<=cptcovage;k++){ /\* For product with age *\/ */
4866: /* /\* if(Dummy[Tvar[Tage[k]]]== 2){ /\\* dummy with age *\\/ ERROR ???*\/ */
4867: /* if(Dummy[Tage[k]]== 2){ /\* dummy with age *\/ */
4868: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
4869: /* } else if(Dummy[Tage[k]]== 3){ /\* quantitative with age *\/ */
4870: /* cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; */
4871: /* } */
4872: /* /\* 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]); *\/ */
4873: /* } */
4874: /* for (k=1; k<=cptcovprod;k++){ /\* For product without age *\/ */
4875: /* /\* 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]); *\/ */
4876: /* if(Dummy[Tvard[k][1]]==0){ */
4877: /* if(Dummy[Tvard[k][2]]==0){ */
4878: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
4879: /* }else{ */
4880: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * Tqresult[nres][k]; */
4881: /* } */
4882: /* }else{ */
4883: /* if(Dummy[Tvard[k][2]]==0){ */
4884: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])] * Tqinvresult[nres][Tvard[k][1]]; */
4885: /* }else{ */
4886: /* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; */
4887: /* } */
4888: /* } */
4889: /* } */
1.217 brouard 4890:
4891: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
4892: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
4893: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
4894: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
4895: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
1.218 brouard 4896: /* ij should be linked to the correct index of cov */
4897: /* age and covariate values ij are in 'cov', but we need to pass
4898: * ij for the observed prevalence at age and status and covariate
4899: * number: prevacurrent[(int)agefin][ii][ij]
4900: */
4901: /* 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 *\/ */
4902: /* 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 *\/ */
4903: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij)); /* Bug Valgrind */
1.268 brouard 4904: /* if((int)age == 86 || (int)age == 87){ */
1.266 brouard 4905: /* printf(" Backward prevalim age=%d agefin=%d \n", (int) age, (int) agefin); */
4906: /* for(i=1; i<=nlstate+ndeath; i++) { */
4907: /* printf("%d newm= ",i); */
4908: /* for(j=1;j<=nlstate+ndeath;j++) { */
4909: /* printf("%f ",newm[i][j]); */
4910: /* } */
4911: /* printf("oldm * "); */
4912: /* for(j=1;j<=nlstate+ndeath;j++) { */
4913: /* printf("%f ",oldm[i][j]); */
4914: /* } */
1.268 brouard 4915: /* printf(" bmmij "); */
1.266 brouard 4916: /* for(j=1;j<=nlstate+ndeath;j++) { */
4917: /* printf("%f ",pmmij[i][j]); */
4918: /* } */
4919: /* printf("\n"); */
4920: /* } */
4921: /* } */
1.217 brouard 4922: savm=oldm;
4923: oldm=newm;
1.266 brouard 4924:
1.217 brouard 4925: for(j=1; j<=nlstate; j++){
4926: max[j]=0.;
4927: min[j]=1.;
4928: }
4929: for(j=1; j<=nlstate; j++){
4930: for(i=1;i<=nlstate;i++){
1.234 brouard 4931: /* bprlim[i][j]= newm[i][j]/(1-sumnew); */
4932: bprlim[i][j]= newm[i][j];
4933: max[i]=FMAX(max[i],bprlim[i][j]); /* Max in line */
4934: min[i]=FMIN(min[i],bprlim[i][j]);
1.217 brouard 4935: }
4936: }
1.218 brouard 4937:
1.217 brouard 4938: maxmax=0.;
4939: for(i=1; i<=nlstate; i++){
1.318 brouard 4940: meandiff[i]=(max[i]-min[i])/(max[i]+min[i])*2.; /* mean difference for each column, could be nan! */
1.217 brouard 4941: maxmax=FMAX(maxmax,meandiff[i]);
4942: /* 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); */
1.268 brouard 4943: } /* i loop */
1.217 brouard 4944: *ncvyear= -( (int)age- (int)agefin);
1.268 brouard 4945: /* printf("Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
1.217 brouard 4946: if(maxmax < ftolpl){
1.220 brouard 4947: /* printf("OK Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
1.217 brouard 4948: free_vector(min,1,nlstate);
4949: free_vector(max,1,nlstate);
4950: free_vector(meandiff,1,nlstate);
4951: return bprlim;
4952: }
1.288 brouard 4953: } /* agefin loop */
1.217 brouard 4954: /* After some age loop it doesn't converge */
1.288 brouard 4955: if(!first){
1.247 brouard 4956: first=1;
4957: 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\
4958: 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);
4959: }
4960: 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\
1.217 brouard 4961: 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);
4962: /* 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); */
4963: free_vector(min,1,nlstate);
4964: free_vector(max,1,nlstate);
4965: free_vector(meandiff,1,nlstate);
4966:
4967: return bprlim; /* should not reach here */
4968: }
4969:
1.126 brouard 4970: /*************** transition probabilities ***************/
4971:
4972: double **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
4973: {
1.138 brouard 4974: /* According to parameters values stored in x and the covariate's values stored in cov,
1.266 brouard 4975: computes the probability to be observed in state j (after stepm years) being in state i by appying the
1.138 brouard 4976: model to the ncovmodel covariates (including constant and age).
4977: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
4978: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
4979: ncth covariate in the global vector x is given by the formula:
4980: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
4981: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
4982: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
4983: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
1.266 brouard 4984: Outputs ps[i][j] or probability to be observed in j being in i according to
1.138 brouard 4985: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
1.266 brouard 4986: Sum on j ps[i][j] should equal to 1.
1.138 brouard 4987: */
4988: double s1, lnpijopii;
1.126 brouard 4989: /*double t34;*/
1.164 brouard 4990: int i,j, nc, ii, jj;
1.126 brouard 4991:
1.223 brouard 4992: for(i=1; i<= nlstate; i++){
4993: for(j=1; j<i;j++){
4994: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
4995: /*lnpijopii += param[i][j][nc]*cov[nc];*/
4996: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
4997: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
4998: }
4999: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
1.330 brouard 5000: /* printf("Debug pmij() i=%d j=%d nc=%d s1=%.17f, lnpijopii=%.17f\n",i,j,nc, s1,lnpijopii); */
1.223 brouard 5001: }
5002: for(j=i+1; j<=nlstate+ndeath;j++){
5003: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
5004: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
5005: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
5006: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
5007: }
5008: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
1.330 brouard 5009: /* printf("Debug pmij() i=%d j=%d nc=%d s1=%.17f, lnpijopii=%.17f\n",i,j,nc, s1,lnpijopii); */
1.223 brouard 5010: }
5011: }
1.218 brouard 5012:
1.223 brouard 5013: for(i=1; i<= nlstate; i++){
5014: s1=0;
5015: for(j=1; j<i; j++){
1.339 brouard 5016: /* printf("debug1 %d %d ps=%lf exp(ps)=%lf \n",i,j,ps[i][j],exp(ps[i][j])); */
1.223 brouard 5017: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
5018: }
5019: for(j=i+1; j<=nlstate+ndeath; j++){
1.339 brouard 5020: /* printf("debug2 %d %d ps=%lf exp(ps)=%lf \n",i,j,ps[i][j],exp(ps[i][j])); */
1.223 brouard 5021: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
5022: }
5023: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
5024: ps[i][i]=1./(s1+1.);
5025: /* Computing other pijs */
5026: for(j=1; j<i; j++)
1.325 brouard 5027: ps[i][j]= exp(ps[i][j])*ps[i][i];/* Bug valgrind */
1.223 brouard 5028: for(j=i+1; j<=nlstate+ndeath; j++)
5029: ps[i][j]= exp(ps[i][j])*ps[i][i];
5030: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
5031: } /* end i */
1.218 brouard 5032:
1.223 brouard 5033: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
5034: for(jj=1; jj<= nlstate+ndeath; jj++){
5035: ps[ii][jj]=0;
5036: ps[ii][ii]=1;
5037: }
5038: }
1.294 brouard 5039:
5040:
1.223 brouard 5041: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
5042: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
5043: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
5044: /* } */
5045: /* printf("\n "); */
5046: /* } */
5047: /* printf("\n ");printf("%lf ",cov[2]);*/
5048: /*
5049: for(i=1; i<= npar; i++) printf("%f ",x[i]);
1.218 brouard 5050: goto end;*/
1.266 brouard 5051: return ps; /* Pointer is unchanged since its call */
1.126 brouard 5052: }
5053:
1.218 brouard 5054: /*************** backward transition probabilities ***************/
5055:
5056: /* 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 ) */
5057: /* double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, double ***dnewm, double **doldm, double **dsavm, int ij ) */
5058: double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, int ij )
5059: {
1.302 brouard 5060: /* Computes the backward probability at age agefin, cov[2], and covariate combination 'ij'. In fact cov is already filled and x too.
1.266 brouard 5061: * Call to pmij(cov and x), call to cross prevalence, sums and inverses, left multiply, and returns in **ps as well as **bmij.
1.222 brouard 5062: */
1.359 brouard 5063: int ii, j;
1.222 brouard 5064:
1.359 brouard 5065: double **pmij();
1.222 brouard 5066: double sumnew=0.;
1.218 brouard 5067: double agefin;
1.292 brouard 5068: double k3=0.; /* constant of the w_x diagonal matrix (in order for B to sum to 1 even for death state) */
1.222 brouard 5069: double **dnewm, **dsavm, **doldm;
5070: double **bbmij;
5071:
1.218 brouard 5072: doldm=ddoldms; /* global pointers */
1.222 brouard 5073: dnewm=ddnewms;
5074: dsavm=ddsavms;
1.318 brouard 5075:
5076: /* Debug */
5077: /* printf("Bmij ij=%d, cov[2}=%f\n", ij, cov[2]); */
1.222 brouard 5078: agefin=cov[2];
1.268 brouard 5079: /* Bx = Diag(w_x) P_x Diag(Sum_i w^i_x p^ij_x */
1.222 brouard 5080: /* bmij *//* age is cov[2], ij is included in cov, but we need for
1.266 brouard 5081: the observed prevalence (with this covariate ij) at beginning of transition */
5082: /* dsavm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
1.268 brouard 5083:
5084: /* P_x */
1.325 brouard 5085: pmmij=pmij(pmmij,cov,ncovmodel,x,nlstate); /*This is forward probability from agefin to agefin + stepm *//* Bug valgrind */
1.268 brouard 5086: /* outputs pmmij which is a stochastic matrix in row */
5087:
5088: /* Diag(w_x) */
1.292 brouard 5089: /* Rescaling the cross-sectional prevalence: Problem with prevacurrent which can be zero */
1.268 brouard 5090: sumnew=0.;
1.269 brouard 5091: /*for (ii=1;ii<=nlstate+ndeath;ii++){*/
1.268 brouard 5092: for (ii=1;ii<=nlstate;ii++){ /* Only on live states */
1.297 brouard 5093: /* printf(" agefin=%d, ii=%d, ij=%d, prev=%f\n",(int)agefin,ii, ij, prevacurrent[(int)agefin][ii][ij]); */
1.268 brouard 5094: sumnew+=prevacurrent[(int)agefin][ii][ij];
5095: }
5096: if(sumnew >0.01){ /* At least some value in the prevalence */
5097: for (ii=1;ii<=nlstate+ndeath;ii++){
5098: for (j=1;j<=nlstate+ndeath;j++)
1.269 brouard 5099: doldm[ii][j]=(ii==j ? prevacurrent[(int)agefin][ii][ij]/sumnew : 0.0);
1.268 brouard 5100: }
5101: }else{
5102: for (ii=1;ii<=nlstate+ndeath;ii++){
5103: for (j=1;j<=nlstate+ndeath;j++)
5104: doldm[ii][j]=(ii==j ? 1./nlstate : 0.0);
5105: }
5106: /* if(sumnew <0.9){ */
5107: /* printf("Problem internal bmij B: sum on i wi <0.9: j=%d, sum_i wi=%lf,agefin=%d\n",j,sumnew, (int)agefin); */
5108: /* } */
5109: }
5110: k3=0.0; /* We put the last diagonal to 0 */
5111: for (ii=nlstate+1;ii<=nlstate+ndeath;ii++){
5112: doldm[ii][ii]= k3;
5113: }
5114: /* End doldm, At the end doldm is diag[(w_i)] */
5115:
1.292 brouard 5116: /* Left product of this diag matrix by pmmij=Px (dnewm=dsavm*doldm): diag[(w_i)*Px */
5117: bbmij=matprod2(dnewm, doldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, pmmij); /* was a Bug Valgrind */
1.268 brouard 5118:
1.292 brouard 5119: /* Diag(Sum_i w^i_x p^ij_x, should be the prevalence at age x+stepm */
1.268 brouard 5120: /* w1 p11 + w2 p21 only on live states N1./N..*N11/N1. + N2./N..*N21/N2.=(N11+N21)/N..=N.1/N.. */
1.222 brouard 5121: for (j=1;j<=nlstate+ndeath;j++){
1.268 brouard 5122: sumnew=0.;
1.222 brouard 5123: for (ii=1;ii<=nlstate;ii++){
1.266 brouard 5124: /* sumnew+=dsavm[ii][j]*prevacurrent[(int)agefin][ii][ij]; */
1.268 brouard 5125: sumnew+=pmmij[ii][j]*doldm[ii][ii]; /* Yes prevalence at beginning of transition */
1.222 brouard 5126: } /* sumnew is (N11+N21)/N..= N.1/N.. = sum on i of w_i pij */
1.268 brouard 5127: for (ii=1;ii<=nlstate+ndeath;ii++){
1.222 brouard 5128: /* if(agefin >= agemaxpar && agefin <= agemaxpar+stepm/YEARM){ */
1.268 brouard 5129: /* dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
1.222 brouard 5130: /* }else if(agefin >= agemaxpar+stepm/YEARM){ */
1.268 brouard 5131: /* dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
1.222 brouard 5132: /* }else */
1.268 brouard 5133: dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0);
5134: } /*End ii */
5135: } /* 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 */
5136:
1.292 brouard 5137: ps=matprod2(ps, dnewm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, dsavm); /* was a Bug Valgrind */
1.268 brouard 5138: /* ps is now diag[w_i] * Px * diag [1/(w_1p1i+w_2 p2i)] */
1.222 brouard 5139: /* end bmij */
1.266 brouard 5140: return ps; /*pointer is unchanged */
1.218 brouard 5141: }
1.217 brouard 5142: /*************** transition probabilities ***************/
5143:
1.218 brouard 5144: double **bpmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
1.217 brouard 5145: {
5146: /* According to parameters values stored in x and the covariate's values stored in cov,
5147: computes the probability to be observed in state j being in state i by appying the
5148: model to the ncovmodel covariates (including constant and age).
5149: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
5150: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
5151: ncth covariate in the global vector x is given by the formula:
5152: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
5153: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
5154: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
5155: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
5156: Outputs ps[i][j] the probability to be observed in j being in j according to
5157: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
5158: */
5159: double s1, lnpijopii;
5160: /*double t34;*/
5161: int i,j, nc, ii, jj;
5162:
1.234 brouard 5163: for(i=1; i<= nlstate; i++){
5164: for(j=1; j<i;j++){
5165: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
5166: /*lnpijopii += param[i][j][nc]*cov[nc];*/
5167: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
5168: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
5169: }
5170: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
5171: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
5172: }
5173: for(j=i+1; j<=nlstate+ndeath;j++){
5174: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
5175: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
5176: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
5177: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
5178: }
5179: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
5180: }
5181: }
5182:
5183: for(i=1; i<= nlstate; i++){
5184: s1=0;
5185: for(j=1; j<i; j++){
5186: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
5187: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
5188: }
5189: for(j=i+1; j<=nlstate+ndeath; j++){
5190: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
5191: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
5192: }
5193: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
5194: ps[i][i]=1./(s1+1.);
5195: /* Computing other pijs */
5196: for(j=1; j<i; j++)
5197: ps[i][j]= exp(ps[i][j])*ps[i][i];
5198: for(j=i+1; j<=nlstate+ndeath; j++)
5199: ps[i][j]= exp(ps[i][j])*ps[i][i];
5200: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
5201: } /* end i */
5202:
5203: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
5204: for(jj=1; jj<= nlstate+ndeath; jj++){
5205: ps[ii][jj]=0;
5206: ps[ii][ii]=1;
5207: }
5208: }
1.296 brouard 5209: /* Added for prevbcast */ /* Transposed matrix too */
1.234 brouard 5210: for(jj=1; jj<= nlstate+ndeath; jj++){
5211: s1=0.;
5212: for(ii=1; ii<= nlstate+ndeath; ii++){
5213: s1+=ps[ii][jj];
5214: }
5215: for(ii=1; ii<= nlstate; ii++){
5216: ps[ii][jj]=ps[ii][jj]/s1;
5217: }
5218: }
5219: /* Transposition */
5220: for(jj=1; jj<= nlstate+ndeath; jj++){
5221: for(ii=jj; ii<= nlstate+ndeath; ii++){
5222: s1=ps[ii][jj];
5223: ps[ii][jj]=ps[jj][ii];
5224: ps[jj][ii]=s1;
5225: }
5226: }
5227: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
5228: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
5229: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
5230: /* } */
5231: /* printf("\n "); */
5232: /* } */
5233: /* printf("\n ");printf("%lf ",cov[2]);*/
5234: /*
5235: for(i=1; i<= npar; i++) printf("%f ",x[i]);
5236: goto end;*/
5237: return ps;
1.217 brouard 5238: }
5239:
5240:
1.126 brouard 5241: /**************** Product of 2 matrices ******************/
5242:
1.145 brouard 5243: double **matprod2(double **out, double **in,int nrl, int nrh, int ncl, int nch, int ncolol, int ncoloh, double **b)
1.126 brouard 5244: {
5245: /* Computes the matrix product of in(1,nrh-nrl+1)(1,nch-ncl+1) times
5246: b(1,nch-ncl+1)(1,ncoloh-ncolol+1) into out(...) */
5247: /* in, b, out are matrice of pointers which should have been initialized
5248: before: only the contents of out is modified. The function returns
5249: a pointer to pointers identical to out */
1.145 brouard 5250: int i, j, k;
1.126 brouard 5251: for(i=nrl; i<= nrh; i++)
1.145 brouard 5252: for(k=ncolol; k<=ncoloh; k++){
5253: out[i][k]=0.;
5254: for(j=ncl; j<=nch; j++)
5255: out[i][k] +=in[i][j]*b[j][k];
5256: }
1.126 brouard 5257: return out;
5258: }
5259:
5260:
5261: /************* Higher Matrix Product ***************/
5262:
1.235 brouard 5263: double ***hpxij(double ***po, int nhstepm, double age, int hstepm, double *x, int nlstate, int stepm, double **oldm, double **savm, int ij, int nres )
1.126 brouard 5264: {
1.336 brouard 5265: /* Already optimized with precov.
5266: Computes the transition matrix starting at age 'age' and dummies values in each resultline (loop on ij to find the corresponding combination) to over
1.126 brouard 5267: 'nhstepm*hstepm*stepm' months (i.e. until
5268: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
5269: nhstepm*hstepm matrices.
5270: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
5271: (typically every 2 years instead of every month which is too big
5272: for the memory).
5273: Model is determined by parameters x and covariates have to be
5274: included manually here.
5275:
5276: */
5277:
1.359 brouard 5278: int i, j, d, h, k1;
1.131 brouard 5279: double **out, cov[NCOVMAX+1];
1.126 brouard 5280: double **newm;
1.187 brouard 5281: double agexact;
1.359 brouard 5282: /*double agebegin, ageend;*/
1.126 brouard 5283:
5284: /* Hstepm could be zero and should return the unit matrix */
5285: for (i=1;i<=nlstate+ndeath;i++)
5286: for (j=1;j<=nlstate+ndeath;j++){
5287: oldm[i][j]=(i==j ? 1.0 : 0.0);
5288: po[i][j][0]=(i==j ? 1.0 : 0.0);
5289: }
5290: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
5291: for(h=1; h <=nhstepm; h++){
5292: for(d=1; d <=hstepm; d++){
5293: newm=savm;
5294: /* Covariates have to be included here again */
5295: cov[1]=1.;
1.214 brouard 5296: agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /* age just before transition */
1.187 brouard 5297: cov[2]=agexact;
1.319 brouard 5298: if(nagesqr==1){
1.227 brouard 5299: cov[3]= agexact*agexact;
1.319 brouard 5300: }
1.330 brouard 5301: /* Model(2) V1 + V2 + V3 + V8 + V7*V8 + V5*V6 + V8*age + V3*age + age*age */
5302: /* total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age */
5303: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
1.349 brouard 5304: if(Typevar[k1]==1 || Typevar[k1]==3){ /* A product with age */
1.332 brouard 5305: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
5306: }else{
5307: cov[2+nagesqr+k1]=precov[nres][k1];
5308: }
5309: }/* End of loop on model equation */
5310: /* Old code */
5311: /* if( Dummy[k1]==0 && Typevar[k1]==0 ){ /\* Single dummy *\/ */
5312: /* /\* V(Tvarsel)=Tvalsel=Tresult[nres][pos](value); V(Tvresult[nres][pos] (variable): V(variable)=value) *\/ */
5313: /* /\* for (k=1; k<=nsd;k++) { /\\* For single dummy covariates only *\\/ *\/ */
5314: /* /\* /\\* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates *\\/ *\/ */
5315: /* /\* codtabm(ij,k) (1 & (ij-1) >> (k-1))+1 *\/ */
5316: /* /\* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 *\/ */
5317: /* /\* k 1 2 3 4 5 6 7 8 9 *\/ */
5318: /* /\*Tvar[k]= 5 4 3 6 5 2 7 1 1 *\/ */
5319: /* /\* nsd 1 2 3 *\/ /\* Counting single dummies covar fixed or tv *\/ */
5320: /* /\*TvarsD[nsd] 4 3 1 *\/ /\* ID of single dummy cova fixed or timevary*\/ */
5321: /* /\*TvarsDind[k] 2 3 9 *\/ /\* position K of single dummy cova *\/ */
5322: /* /\* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)];or [codtabm(ij,TnsdVar[TvarsD[k]] *\/ */
5323: /* cov[2+nagesqr+k1]=Tresult[nres][resultmodel[nres][k1]]; */
5324: /* /\* 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]])); *\/ */
5325: /* 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); */
5326: /* printf("hpxij new Dummy precov[nres=%d][k1=%d]=%.4f\n", nres, k1, precov[nres][k1]); */
5327: /* }else if( Dummy[k1]==1 && Typevar[k1]==0 ){ /\* Single quantitative variables *\/ */
5328: /* /\* resultmodel[nres][k1]=k3: k1th position in the model correspond to the k3 position in the resultline *\/ */
5329: /* cov[2+nagesqr+k1]=Tqresult[nres][resultmodel[nres][k1]]; */
5330: /* /\* for (k=1; k<=nsq;k++) { /\\* For single varying covariates only *\\/ *\/ */
5331: /* /\* /\\* Here comes the value of quantitative after renumbering k with single quantitative covariates *\\/ *\/ */
5332: /* /\* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; *\/ */
5333: /* 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]]); */
5334: /* printf("hpxij new Quanti precov[nres=%d][k1=%d]=%.4f\n", nres, k1, precov[nres][k1]); */
5335: /* }else if( Dummy[k1]==2 ){ /\* For dummy with age product *\/ */
5336: /* /\* Tvar[k1] Variable in the age product age*V1 is 1 *\/ */
5337: /* /\* [Tinvresult[nres][V1] is its value in the resultline nres *\/ */
5338: /* cov[2+nagesqr+k1]=TinvDoQresult[nres][Tvar[k1]]*cov[2]; */
5339: /* 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]); */
5340: /* printf("hpxij new Dummy with age product precov[nres=%d][k1=%d]=%.4f * age=%.2f\n", nres, k1, precov[nres][k1], cov[2]); */
5341:
5342: /* /\* cov[2+nagesqr+k1]=Tresult[nres][resultmodel[nres][k1]]; *\/ */
5343: /* /\* for (k=1; k<=cptcovage;k++){ /\\* For product with age V1+V1*age +V4 +age*V3 *\\/ *\/ */
5344: /* /\* 1+2 Tage[1]=2 TVar[2]=1 Dummy[2]=2, Tage[2]=4 TVar[4]=3 Dummy[4]=3 quant*\/ */
5345: /* /\* *\/ */
1.330 brouard 5346: /* /\* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 *\/ */
5347: /* /\* k 1 2 3 4 5 6 7 8 9 *\/ */
5348: /* /\*Tvar[k]= 5 4 3 6 5 2 7 1 1 *\/ */
1.332 brouard 5349: /* /\*cptcovage=2 1 2 *\/ */
5350: /* /\*Tage[k]= 5 8 *\/ */
5351: /* }else if( Dummy[k1]==3 ){ /\* For quant with age product *\/ */
5352: /* cov[2+nagesqr+k1]=Tresult[nres][resultmodel[nres][k1]]; */
5353: /* 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]]); */
5354: /* printf("hpxij new Quanti with age product precov[nres=%d][k1=%d] * age=%.2f\n", nres, k1, precov[nres][k1], cov[2]); */
5355: /* /\* if(Dummy[Tage[k]]== 2){ /\\* dummy with age *\\/ *\/ */
5356: /* /\* /\\* if(Dummy[Tvar[Tage[k]]]== 2){ /\\\* dummy with age *\\\/ *\\/ *\/ */
5357: /* /\* /\\* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; *\\/ *\/ */
5358: /* /\* /\\* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,TnsdVar[TvarsD[Tvar[Tage[k]]]])]*cov[2]; *\\/ *\/ */
5359: /* /\* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,TnsdVar[TvarsD[Tvar[Tage[k]]]])]*cov[2]; *\/ */
5360: /* /\* 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); *\/ */
5361: /* /\* } else if(Dummy[Tage[k]]== 3){ /\\* quantitative with age *\\/ *\/ */
5362: /* /\* cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; *\/ */
5363: /* /\* } *\/ */
5364: /* /\* 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]); *\/ */
5365: /* }else if(Typevar[k1]==2 ){ /\* For product (not with age) *\/ */
5366: /* /\* for (k=1; k<=cptcovprod;k++){ /\\* For product without age *\\/ *\/ */
5367: /* /\* /\\* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 *\\/ *\/ */
5368: /* /\* /\\* k 1 2 3 4 5 6 7 8 9 *\\/ *\/ */
5369: /* /\* /\\*Tvar[k]= 5 4 3 6 5 2 7 1 1 *\\/ *\/ */
5370: /* /\* /\\*cptcovprod=1 1 2 *\\/ *\/ */
5371: /* /\* /\\*Tprod[]= 4 7 *\\/ *\/ */
5372: /* /\* /\\*Tvard[][1] 4 1 *\\/ *\/ */
5373: /* /\* /\\*Tvard[][2] 3 2 *\\/ *\/ */
1.330 brouard 5374:
1.332 brouard 5375: /* /\* 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])]); *\/ */
5376: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
5377: /* cov[2+nagesqr+k1]=TinvDoQresult[nres][Tvardk[k1][1]] * TinvDoQresult[nres][Tvardk[k1][2]]; */
5378: /* 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]]); */
5379: /* printf("hpxij new Product no age product precov[nres=%d][k1=%d]=%.4f\n", nres, k1, precov[nres][k1]); */
5380:
5381: /* /\* if(Dummy[Tvardk[k1][1]]==0){ *\/ */
5382: /* /\* if(Dummy[Tvardk[k1][2]]==0){ /\\* Product of dummies *\\/ *\/ */
5383: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
5384: /* /\* cov[2+nagesqr+k1]=Tinvresult[nres][Tvardk[k1][1]] * Tinvresult[nres][Tvardk[k1][2]]; *\/ */
5385: /* /\* 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]])]; *\/ */
5386: /* /\* }else{ /\\* Product of dummy by quantitative *\\/ *\/ */
5387: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,TnsdVar[Tvard[k][1]])] * Tqresult[nres][k]; *\/ */
5388: /* /\* cov[2+nagesqr+k1]=Tresult[nres][Tinvresult[nres][Tvardk[k1][1]]] * Tqresult[nres][Tinvresult[nres][Tvardk[k1][2]]]; *\/ */
5389: /* /\* } *\/ */
5390: /* /\* }else{ /\\* Product of quantitative by...*\\/ *\/ */
5391: /* /\* if(Dummy[Tvard[k][2]]==0){ /\\* quant by dummy *\\/ *\/ */
5392: /* /\* /\\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,TnsdVar[Tvard[k][2]])] * Tqinvresult[nres][Tvard[k][1]]; *\\/ *\/ */
5393: /* /\* cov[2+nagesqr+k1]=Tqresult[nres][Tinvresult[nres][Tvardk[k1][1]]] * Tresult[nres][Tinvresult[nres][Tvardk[k1][2]]] ; *\/ */
5394: /* /\* }else{ /\\* Product of two quant *\\/ *\/ */
5395: /* /\* /\\* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; *\\/ *\/ */
5396: /* /\* cov[2+nagesqr+k1]=Tqresult[nres][Tinvresult[nres][Tvardk[k1][1]]] * Tqresult[nres][Tinvresult[nres][Tvardk[k1][2]]] ; *\/ */
5397: /* /\* } *\/ */
5398: /* /\* }/\\*end of products quantitative *\\/ *\/ */
5399: /* }/\*end of products *\/ */
5400: /* } /\* End of loop on model equation *\/ */
1.235 brouard 5401: /* for (k=1; k<=cptcovn;k++) */
5402: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; */
5403: /* for (k=1; k<=cptcovage;k++) /\* Should start at cptcovn+1 *\/ */
5404: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; */
5405: /* for (k=1; k<=cptcovprod;k++) /\* Useless because included in cptcovn *\/ */
5406: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)]; */
1.227 brouard 5407:
5408:
1.126 brouard 5409: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
5410: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
1.319 brouard 5411: /* right multiplication of oldm by the current matrix */
1.126 brouard 5412: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
5413: pmij(pmmij,cov,ncovmodel,x,nlstate));
1.217 brouard 5414: /* if((int)age == 70){ */
5415: /* printf(" Forward hpxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
5416: /* for(i=1; i<=nlstate+ndeath; i++) { */
5417: /* printf("%d pmmij ",i); */
5418: /* for(j=1;j<=nlstate+ndeath;j++) { */
5419: /* printf("%f ",pmmij[i][j]); */
5420: /* } */
5421: /* printf(" oldm "); */
5422: /* for(j=1;j<=nlstate+ndeath;j++) { */
5423: /* printf("%f ",oldm[i][j]); */
5424: /* } */
5425: /* printf("\n"); */
5426: /* } */
5427: /* } */
1.126 brouard 5428: savm=oldm;
5429: oldm=newm;
5430: }
5431: for(i=1; i<=nlstate+ndeath; i++)
5432: for(j=1;j<=nlstate+ndeath;j++) {
1.267 brouard 5433: po[i][j][h]=newm[i][j];
5434: /*if(h==nhstepm) printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]);*/
1.126 brouard 5435: }
1.128 brouard 5436: /*printf("h=%d ",h);*/
1.126 brouard 5437: } /* end h */
1.267 brouard 5438: /* printf("\n H=%d \n",h); */
1.126 brouard 5439: return po;
5440: }
5441:
1.217 brouard 5442: /************* Higher Back Matrix Product ***************/
1.218 brouard 5443: /* double ***hbxij(double ***po, int nhstepm, double age, int hstepm, double *x, double ***prevacurrent, int nlstate, int stepm, double **oldm, double **savm, double **dnewm, double **doldm, double **dsavm, int ij ) */
1.267 brouard 5444: double ***hbxij(double ***po, int nhstepm, double age, int hstepm, double *x, double ***prevacurrent, int nlstate, int stepm, int ij, int nres )
1.217 brouard 5445: {
1.332 brouard 5446: /* For dummy covariates given in each resultline (for historical, computes the corresponding combination ij),
5447: computes the transition matrix starting at age 'age' over
1.217 brouard 5448: 'nhstepm*hstepm*stepm' months (i.e. until
1.218 brouard 5449: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
5450: nhstepm*hstepm matrices.
5451: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
5452: (typically every 2 years instead of every month which is too big
1.217 brouard 5453: for the memory).
1.218 brouard 5454: Model is determined by parameters x and covariates have to be
1.266 brouard 5455: included manually here. Then we use a call to bmij(x and cov)
5456: The addresss of po (p3mat allocated to the dimension of nhstepm) should be stored for output
1.222 brouard 5457: */
1.217 brouard 5458:
1.359 brouard 5459: int i, j, d, h, k1;
1.266 brouard 5460: double **out, cov[NCOVMAX+1], **bmij();
5461: double **newm, ***newmm;
1.217 brouard 5462: double agexact;
1.359 brouard 5463: /*double agebegin, ageend;*/
1.222 brouard 5464: double **oldm, **savm;
1.217 brouard 5465:
1.266 brouard 5466: newmm=po; /* To be saved */
5467: oldm=oldms;savm=savms; /* Global pointers */
1.217 brouard 5468: /* Hstepm could be zero and should return the unit matrix */
5469: for (i=1;i<=nlstate+ndeath;i++)
5470: for (j=1;j<=nlstate+ndeath;j++){
5471: oldm[i][j]=(i==j ? 1.0 : 0.0);
5472: po[i][j][0]=(i==j ? 1.0 : 0.0);
5473: }
5474: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
5475: for(h=1; h <=nhstepm; h++){
5476: for(d=1; d <=hstepm; d++){
5477: newm=savm;
5478: /* Covariates have to be included here again */
5479: cov[1]=1.;
1.271 brouard 5480: agexact=age-( (h-1)*hstepm + (d) )*stepm/YEARM; /* age just before transition, d or d-1? */
1.217 brouard 5481: /* agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /\* age just before transition *\/ */
1.318 brouard 5482: /* Debug */
5483: /* printf("hBxij age=%lf, agexact=%lf\n", age, agexact); */
1.217 brouard 5484: cov[2]=agexact;
1.332 brouard 5485: if(nagesqr==1){
1.222 brouard 5486: cov[3]= agexact*agexact;
1.332 brouard 5487: }
5488: /** New code */
5489: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
1.349 brouard 5490: if(Typevar[k1]==1 || Typevar[k1]==3){ /* A product with age */
1.332 brouard 5491: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
1.325 brouard 5492: }else{
1.332 brouard 5493: cov[2+nagesqr+k1]=precov[nres][k1];
1.325 brouard 5494: }
1.332 brouard 5495: }/* End of loop on model equation */
5496: /** End of new code */
5497: /** This was old code */
5498: /* for (k=1; k<=nsd;k++){ /\* For single dummy covariates only *\//\* cptcovn error *\/ */
5499: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; *\/ */
5500: /* /\* /\\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; *\\/ *\/ */
5501: /* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,TvarsD[k])];/\* Bug valgrind *\/ */
5502: /* /\* 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)); *\/ */
5503: /* } */
5504: /* for (k=1; k<=nsq;k++) { /\* For single varying covariates only *\/ */
5505: /* /\* Here comes the value of quantitative after renumbering k with single quantitative covariates *\/ */
5506: /* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; */
5507: /* /\* 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]); *\/ */
5508: /* } */
5509: /* for (k=1; k<=cptcovage;k++){ /\* Should start at cptcovn+1 *\//\* For product with age *\/ */
5510: /* /\* if(Dummy[Tvar[Tage[k]]]== 2){ /\\* dummy with age error!!!*\\/ *\/ */
5511: /* if(Dummy[Tage[k]]== 2){ /\* dummy with age *\/ */
5512: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
5513: /* } else if(Dummy[Tage[k]]== 3){ /\* quantitative with age *\/ */
5514: /* cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; */
5515: /* } */
5516: /* /\* 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]); *\/ */
5517: /* } */
5518: /* for (k=1; k<=cptcovprod;k++){ /\* Useless because included in cptcovn *\/ */
5519: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])]*nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
5520: /* if(Dummy[Tvard[k][1]]==0){ */
5521: /* if(Dummy[Tvard[k][2]]==0){ */
5522: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][1])]; */
5523: /* }else{ */
5524: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * Tqresult[nres][k]; */
5525: /* } */
5526: /* }else{ */
5527: /* if(Dummy[Tvard[k][2]]==0){ */
5528: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])] * Tqinvresult[nres][Tvard[k][1]]; */
5529: /* }else{ */
5530: /* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; */
5531: /* } */
5532: /* } */
5533: /* } */
5534: /* /\*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*\/ */
5535: /* /\*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*\/ */
5536: /** End of old code */
5537:
1.218 brouard 5538: /* Careful transposed matrix */
1.266 brouard 5539: /* age is in cov[2], prevacurrent at beginning of transition. */
1.218 brouard 5540: /* out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm, dsavm,ij),\ */
1.222 brouard 5541: /* 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); */
1.218 brouard 5542: out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij),\
1.325 brouard 5543: 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm);/* Bug valgrind */
1.217 brouard 5544: /* if((int)age == 70){ */
5545: /* printf(" Backward hbxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
5546: /* for(i=1; i<=nlstate+ndeath; i++) { */
5547: /* printf("%d pmmij ",i); */
5548: /* for(j=1;j<=nlstate+ndeath;j++) { */
5549: /* printf("%f ",pmmij[i][j]); */
5550: /* } */
5551: /* printf(" oldm "); */
5552: /* for(j=1;j<=nlstate+ndeath;j++) { */
5553: /* printf("%f ",oldm[i][j]); */
5554: /* } */
5555: /* printf("\n"); */
5556: /* } */
5557: /* } */
5558: savm=oldm;
5559: oldm=newm;
5560: }
5561: for(i=1; i<=nlstate+ndeath; i++)
5562: for(j=1;j<=nlstate+ndeath;j++) {
1.222 brouard 5563: po[i][j][h]=newm[i][j];
1.268 brouard 5564: /* if(h==nhstepm) */
5565: /* printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]); */
1.217 brouard 5566: }
1.268 brouard 5567: /* printf("h=%d %.1f ",h, agexact); */
1.217 brouard 5568: } /* end h */
1.268 brouard 5569: /* printf("\n H=%d nhs=%d \n",h, nhstepm); */
1.217 brouard 5570: return po;
5571: }
5572:
5573:
1.162 brouard 5574: #ifdef NLOPT
5575: double myfunc(unsigned n, const double *p1, double *grad, void *pd){
5576: double fret;
5577: double *xt;
5578: int j;
5579: myfunc_data *d2 = (myfunc_data *) pd;
5580: /* xt = (p1-1); */
5581: xt=vector(1,n);
5582: for (j=1;j<=n;j++) xt[j]=p1[j-1]; /* xt[1]=p1[0] */
5583:
5584: fret=(d2->function)(xt); /* p xt[1]@8 is fine */
5585: /* fret=(*func)(xt); /\* p xt[1]@8 is fine *\/ */
5586: printf("Function = %.12lf ",fret);
5587: for (j=1;j<=n;j++) printf(" %d %.8lf", j, xt[j]);
5588: printf("\n");
5589: free_vector(xt,1,n);
5590: return fret;
5591: }
5592: #endif
1.126 brouard 5593:
5594: /*************** log-likelihood *************/
5595: double func( double *x)
5596: {
1.336 brouard 5597: int i, ii, j, k, mi, d, kk, kf=0;
1.226 brouard 5598: int ioffset=0;
1.339 brouard 5599: int ipos=0,iposold=0,ncovv=0;
5600:
1.340 brouard 5601: double cotvarv, cotvarvold;
1.226 brouard 5602: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
5603: double **out;
5604: double lli; /* Individual log likelihood */
5605: int s1, s2;
1.228 brouard 5606: int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate, quantitative time varying covariate */
1.336 brouard 5607:
1.226 brouard 5608: double bbh, survp;
5609: double agexact;
1.336 brouard 5610: double agebegin, ageend;
1.226 brouard 5611: /*extern weight */
5612: /* We are differentiating ll according to initial status */
5613: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
5614: /*for(i=1;i<imx;i++)
5615: printf(" %d\n",s[4][i]);
5616: */
1.162 brouard 5617:
1.226 brouard 5618: ++countcallfunc;
1.162 brouard 5619:
1.226 brouard 5620: cov[1]=1.;
1.126 brouard 5621:
1.226 brouard 5622: for(k=1; k<=nlstate; k++) ll[k]=0.;
1.224 brouard 5623: ioffset=0;
1.226 brouard 5624: if(mle==1){
5625: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
5626: /* Computes the values of the ncovmodel covariates of the model
5627: depending if the covariates are fixed or varying (age dependent) and stores them in cov[]
5628: Then computes with function pmij which return a matrix p[i][j] giving the elementary probability
5629: to be observed in j being in i according to the model.
5630: */
1.243 brouard 5631: ioffset=2+nagesqr ;
1.233 brouard 5632: /* Fixed */
1.345 brouard 5633: for (kf=1; kf<=ncovf;kf++){ /* For each fixed covariate dummy or quant or prod */
1.319 brouard 5634: /* # V1=sex, V2=raedyrs Quant Fixed, State=livarnb4..livarnb11, V3=iadl4..iald11, V4=adlw4..adlw11, V5=r4bmi..r11bmi */
5635: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
5636: /* 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 */
1.320 brouard 5637: /* TvarFind; TvarFind[1]=6, TvarFind[2]=7, TvarFind[3]=9 *//* Inverse V2(6) is first fixed (single or prod) */
1.336 brouard 5638: 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)*/
1.319 brouard 5639: /* V1*V2 (7) TvarFind[2]=7, TvarFind[3]=9 */
1.234 brouard 5640: }
1.226 brouard 5641: /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4]
1.319 brouard 5642: is 5, Tvar[3=age*V3] should not be computed because of age Tvar[4=V3*V2]=6
1.226 brouard 5643: has been calculated etc */
5644: /* For an individual i, wav[i] gives the number of effective waves */
5645: /* We compute the contribution to Likelihood of each effective transition
5646: mw[mi][i] is real wave of the mi th effectve wave */
5647: /* Then statuses are computed at each begin and end of an effective wave s1=s[ mw[mi][i] ][i];
5648: s2=s[mw[mi+1][i]][i];
1.341 brouard 5649: And the iv th varying covariate is the cotvar[mw[mi+1][i]][iv][i] because now is moved after nvocol+nqv
1.226 brouard 5650: But if the variable is not in the model TTvar[iv] is the real variable effective in the model:
5651: meaning that decodemodel should be used cotvar[mw[mi+1][i]][TTvar[iv]][i]
5652: */
1.336 brouard 5653: for(mi=1; mi<= wav[i]-1; mi++){ /* Varying with waves */
5654: /* Wave varying (but not age varying) */
1.339 brouard 5655: /* 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*\/ */
5656: /* /\* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; but where is the crossproduct? *\/ */
5657: /* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]-ncovcol-nqv][i]; */
5658: /* } */
1.340 brouard 5659: for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){ /* Varying covariates (single and product but no age )*/
5660: itv=TvarVV[ncovv]; /* TvarVV={3, 1, 3} gives the name of each varying covariate */
5661: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
1.345 brouard 5662: if(FixedV[itv]!=0){ /* Not a fixed covariate */
1.341 brouard 5663: cotvarv=cotvar[mw[mi][i]][TvarVV[ncovv]][i]; /* cotvar[wav][ncovcol+nqv+iv][i] */
1.340 brouard 5664: }else{ /* fixed covariate */
1.345 brouard 5665: cotvarv=covar[itv][i]; /* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model */
1.340 brouard 5666: }
1.339 brouard 5667: if(ipos!=iposold){ /* Not a product or first of a product */
1.340 brouard 5668: cotvarvold=cotvarv;
5669: }else{ /* A second product */
5670: cotvarv=cotvarv*cotvarvold;
1.339 brouard 5671: }
5672: iposold=ipos;
1.340 brouard 5673: cov[ioffset+ipos]=cotvarv;
1.234 brouard 5674: }
1.339 brouard 5675: /* for products of time varying to be done */
1.234 brouard 5676: for (ii=1;ii<=nlstate+ndeath;ii++)
5677: for (j=1;j<=nlstate+ndeath;j++){
5678: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
5679: savm[ii][j]=(ii==j ? 1.0 : 0.0);
5680: }
1.336 brouard 5681:
5682: agebegin=agev[mw[mi][i]][i]; /* Age at beginning of effective wave */
5683: ageend=agev[mw[mi][i]][i] + (dh[mi][i])*stepm/YEARM; /* Age at end of effective wave and at the end of transition */
1.234 brouard 5684: for(d=0; d<dh[mi][i]; d++){
5685: newm=savm;
5686: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
5687: cov[2]=agexact;
5688: if(nagesqr==1)
5689: cov[3]= agexact*agexact; /* Should be changed here */
1.349 brouard 5690: /* for (kk=1; kk<=cptcovage;kk++) { */
5691: /* if(!FixedV[Tvar[Tage[kk]]]) */
5692: /* cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; /\* Tage[kk] gives the data-covariate associated with age *\/ */
5693: /* else */
5694: /* 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) *\/ */
5695: /* } */
5696: for(ncovva=1, iposold=0; ncovva <= ncovta ; ncovva++){ /* Time varying covariates with age including individual from products, product is computed dynamically */
5697: itv=TvarAVVA[ncovva]; /* TvarVV={3, 1, 3} gives the name of each varying covariate, exploding product Vn*Vm into Vn and then Vm */
5698: ipos=TvarAVVAind[ncovva]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
5699: if(FixedV[itv]!=0){ /* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv */
5700: cotvarv=cotvar[mw[mi][i]][TvarAVVA[ncovva]][i]; /* because cotvar starts now at first ncovcol+nqv+ntv+nqtv (1 to nqtv) */
5701: }else{ /* fixed covariate */
5702: cotvarv=covar[itv][i]; /* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model */
5703: }
5704: if(ipos!=iposold){ /* Not a product or first of a product */
5705: cotvarvold=cotvarv;
5706: }else{ /* A second product */
5707: cotvarv=cotvarv*cotvarvold;
5708: }
5709: iposold=ipos;
5710: cov[ioffset+ipos]=cotvarv*agexact;
5711: /* For products */
1.234 brouard 5712: }
1.349 brouard 5713:
1.234 brouard 5714: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
5715: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
5716: savm=oldm;
5717: oldm=newm;
5718: } /* end mult */
5719:
5720: /*lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ /* Original formula */
5721: /* But now since version 0.9 we anticipate for bias at large stepm.
5722: * If stepm is larger than one month (smallest stepm) and if the exact delay
5723: * (in months) between two waves is not a multiple of stepm, we rounded to
5724: * the nearest (and in case of equal distance, to the lowest) interval but now
5725: * we keep into memory the bias bh[mi][i] and also the previous matrix product
5726: * (i.e to dh[mi][i]-1) saved in 'savm'. Then we inter(extra)polate the
5727: * probability in order to take into account the bias as a fraction of the way
1.231 brouard 5728: * from savm to out if bh is negative or even beyond if bh is positive. bh varies
5729: * -stepm/2 to stepm/2 .
5730: * For stepm=1 the results are the same as for previous versions of Imach.
5731: * For stepm > 1 the results are less biased than in previous versions.
5732: */
1.234 brouard 5733: s1=s[mw[mi][i]][i];
5734: s2=s[mw[mi+1][i]][i];
5735: bbh=(double)bh[mi][i]/(double)stepm;
5736: /* bias bh is positive if real duration
5737: * is higher than the multiple of stepm and negative otherwise.
5738: */
5739: /* lli= (savm[s1][s2]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2]));*/
5740: if( s2 > nlstate){
5741: /* i.e. if s2 is a death state and if the date of death is known
5742: then the contribution to the likelihood is the probability to
5743: die between last step unit time and current step unit time,
5744: which is also equal to probability to die before dh
5745: minus probability to die before dh-stepm .
5746: In version up to 0.92 likelihood was computed
5747: as if date of death was unknown. Death was treated as any other
5748: health state: the date of the interview describes the actual state
5749: and not the date of a change in health state. The former idea was
5750: to consider that at each interview the state was recorded
5751: (healthy, disable or death) and IMaCh was corrected; but when we
5752: introduced the exact date of death then we should have modified
5753: the contribution of an exact death to the likelihood. This new
5754: contribution is smaller and very dependent of the step unit
5755: stepm. It is no more the probability to die between last interview
5756: and month of death but the probability to survive from last
5757: interview up to one month before death multiplied by the
5758: probability to die within a month. Thanks to Chris
5759: Jackson for correcting this bug. Former versions increased
5760: mortality artificially. The bad side is that we add another loop
5761: which slows down the processing. The difference can be up to 10%
5762: lower mortality.
5763: */
5764: /* If, at the beginning of the maximization mostly, the
5765: cumulative probability or probability to be dead is
5766: constant (ie = 1) over time d, the difference is equal to
5767: 0. out[s1][3] = savm[s1][3]: probability, being at state
5768: s1 at precedent wave, to be dead a month before current
5769: wave is equal to probability, being at state s1 at
5770: precedent wave, to be dead at mont of the current
5771: wave. Then the observed probability (that this person died)
5772: is null according to current estimated parameter. In fact,
5773: it should be very low but not zero otherwise the log go to
5774: infinity.
5775: */
1.183 brouard 5776: /* #ifdef INFINITYORIGINAL */
5777: /* lli=log(out[s1][s2] - savm[s1][s2]); */
5778: /* #else */
5779: /* if ((out[s1][s2] - savm[s1][s2]) < mytinydouble) */
5780: /* lli=log(mytinydouble); */
5781: /* else */
5782: /* lli=log(out[s1][s2] - savm[s1][s2]); */
5783: /* #endif */
1.226 brouard 5784: lli=log(out[s1][s2] - savm[s1][s2]);
1.216 brouard 5785:
1.226 brouard 5786: } else if ( s2==-1 ) { /* alive */
5787: for (j=1,survp=0. ; j<=nlstate; j++)
5788: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
5789: /*survp += out[s1][j]; */
5790: lli= log(survp);
5791: }
1.336 brouard 5792: /* else if (s2==-4) { */
5793: /* for (j=3,survp=0. ; j<=nlstate; j++) */
5794: /* survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j]; */
5795: /* lli= log(survp); */
5796: /* } */
5797: /* else if (s2==-5) { */
5798: /* for (j=1,survp=0. ; j<=2; j++) */
5799: /* survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j]; */
5800: /* lli= log(survp); */
5801: /* } */
1.226 brouard 5802: else{
5803: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
5804: /* 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 */
5805: }
5806: /*lli=(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]);*/
5807: /*if(lli ==000.0)*/
1.340 brouard 5808: /* 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); */
1.226 brouard 5809: ipmx +=1;
5810: sw += weight[i];
5811: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
5812: /* if (lli < log(mytinydouble)){ */
5813: /* 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); */
5814: /* 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]); */
5815: /* } */
5816: } /* end of wave */
5817: } /* end of individual */
5818: } else if(mle==2){
5819: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1.319 brouard 5820: ioffset=2+nagesqr ;
5821: for (k=1; k<=ncovf;k++)
5822: cov[ioffset+TvarFind[k]]=covar[Tvar[TvarFind[k]]][i];
1.226 brouard 5823: for(mi=1; mi<= wav[i]-1; mi++){
1.319 brouard 5824: for(k=1; k <= ncovv ; k++){
1.341 brouard 5825: cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; /* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) */
1.319 brouard 5826: }
1.226 brouard 5827: for (ii=1;ii<=nlstate+ndeath;ii++)
5828: for (j=1;j<=nlstate+ndeath;j++){
5829: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
5830: savm[ii][j]=(ii==j ? 1.0 : 0.0);
5831: }
5832: for(d=0; d<=dh[mi][i]; d++){
5833: newm=savm;
5834: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
5835: cov[2]=agexact;
5836: if(nagesqr==1)
5837: cov[3]= agexact*agexact;
5838: for (kk=1; kk<=cptcovage;kk++) {
5839: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
5840: }
5841: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
5842: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
5843: savm=oldm;
5844: oldm=newm;
5845: } /* end mult */
5846:
5847: s1=s[mw[mi][i]][i];
5848: s2=s[mw[mi+1][i]][i];
5849: bbh=(double)bh[mi][i]/(double)stepm;
5850: 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 */
5851: ipmx +=1;
5852: sw += weight[i];
5853: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
5854: } /* end of wave */
5855: } /* end of individual */
5856: } else if(mle==3){ /* exponential inter-extrapolation */
5857: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
5858: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
5859: for(mi=1; mi<= wav[i]-1; mi++){
5860: for (ii=1;ii<=nlstate+ndeath;ii++)
5861: for (j=1;j<=nlstate+ndeath;j++){
5862: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
5863: savm[ii][j]=(ii==j ? 1.0 : 0.0);
5864: }
5865: for(d=0; d<dh[mi][i]; d++){
5866: newm=savm;
5867: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
5868: cov[2]=agexact;
5869: if(nagesqr==1)
5870: cov[3]= agexact*agexact;
5871: for (kk=1; kk<=cptcovage;kk++) {
1.340 brouard 5872: if(!FixedV[Tvar[Tage[kk]]])
5873: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; /* Tage[kk] gives the data-covariate associated with age */
5874: else
1.341 brouard 5875: 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) */
1.226 brouard 5876: }
5877: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
5878: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
5879: savm=oldm;
5880: oldm=newm;
5881: } /* end mult */
5882:
5883: s1=s[mw[mi][i]][i];
5884: s2=s[mw[mi+1][i]][i];
5885: bbh=(double)bh[mi][i]/(double)stepm;
5886: lli= (savm[s1][s2]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2])); /* exponential inter-extrapolation */
5887: ipmx +=1;
5888: sw += weight[i];
5889: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
5890: } /* end of wave */
5891: } /* end of individual */
5892: }else if (mle==4){ /* ml=4 no inter-extrapolation */
5893: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
5894: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
5895: for(mi=1; mi<= wav[i]-1; mi++){
5896: for (ii=1;ii<=nlstate+ndeath;ii++)
5897: for (j=1;j<=nlstate+ndeath;j++){
5898: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
5899: savm[ii][j]=(ii==j ? 1.0 : 0.0);
5900: }
5901: for(d=0; d<dh[mi][i]; d++){
5902: newm=savm;
5903: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
5904: cov[2]=agexact;
5905: if(nagesqr==1)
5906: cov[3]= agexact*agexact;
5907: for (kk=1; kk<=cptcovage;kk++) {
5908: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
5909: }
1.126 brouard 5910:
1.226 brouard 5911: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
5912: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
5913: savm=oldm;
5914: oldm=newm;
5915: } /* end mult */
5916:
5917: s1=s[mw[mi][i]][i];
5918: s2=s[mw[mi+1][i]][i];
5919: if( s2 > nlstate){
5920: lli=log(out[s1][s2] - savm[s1][s2]);
5921: } else if ( s2==-1 ) { /* alive */
5922: for (j=1,survp=0. ; j<=nlstate; j++)
5923: survp += out[s1][j];
5924: lli= log(survp);
5925: }else{
5926: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
5927: }
5928: ipmx +=1;
5929: sw += weight[i];
5930: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1.343 brouard 5931: /* 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])); */
1.226 brouard 5932: } /* end of wave */
5933: } /* end of individual */
5934: }else{ /* ml=5 no inter-extrapolation no jackson =0.8a */
5935: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
5936: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
5937: for(mi=1; mi<= wav[i]-1; mi++){
5938: for (ii=1;ii<=nlstate+ndeath;ii++)
5939: for (j=1;j<=nlstate+ndeath;j++){
5940: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
5941: savm[ii][j]=(ii==j ? 1.0 : 0.0);
5942: }
5943: for(d=0; d<dh[mi][i]; d++){
5944: newm=savm;
5945: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
5946: cov[2]=agexact;
5947: if(nagesqr==1)
5948: cov[3]= agexact*agexact;
5949: for (kk=1; kk<=cptcovage;kk++) {
1.340 brouard 5950: if(!FixedV[Tvar[Tage[kk]]])
5951: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; /* Tage[kk] gives the data-covariate associated with age */
5952: else
1.341 brouard 5953: 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) */
1.226 brouard 5954: }
1.126 brouard 5955:
1.226 brouard 5956: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
5957: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
5958: savm=oldm;
5959: oldm=newm;
5960: } /* end mult */
5961:
5962: s1=s[mw[mi][i]][i];
5963: s2=s[mw[mi+1][i]][i];
5964: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
5965: ipmx +=1;
5966: sw += weight[i];
5967: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
5968: /*printf("i=%6d s1=%1d s2=%1d mi=%1d mw=%1d dh=%3d prob=%10.6f w=%6.4f out=%10.6f sav=%10.6f\n",i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],out[s1][s2],savm[s1][s2]);*/
5969: } /* end of wave */
5970: } /* end of individual */
5971: } /* End of if */
5972: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
5973: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
5974: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
5975: return -l;
1.126 brouard 5976: }
5977:
5978: /*************** log-likelihood *************/
5979: double funcone( double *x)
5980: {
1.228 brouard 5981: /* Same as func but slower because of a lot of printf and if */
1.359 brouard 5982: int i, ii, j, k, mi, d, kv=0, kf=0;
1.228 brouard 5983: int ioffset=0;
1.339 brouard 5984: int ipos=0,iposold=0,ncovv=0;
5985:
1.340 brouard 5986: double cotvarv, cotvarvold;
1.131 brouard 5987: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
1.126 brouard 5988: double **out;
5989: double lli; /* Individual log likelihood */
5990: double llt;
5991: int s1, s2;
1.228 brouard 5992: int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate, quantitative time varying covariate */
5993:
1.126 brouard 5994: double bbh, survp;
1.187 brouard 5995: double agexact;
1.214 brouard 5996: double agebegin, ageend;
1.126 brouard 5997: /*extern weight */
5998: /* We are differentiating ll according to initial status */
5999: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
6000: /*for(i=1;i<imx;i++)
6001: printf(" %d\n",s[4][i]);
6002: */
6003: cov[1]=1.;
6004:
6005: for(k=1; k<=nlstate; k++) ll[k]=0.;
1.224 brouard 6006: ioffset=0;
6007: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1.336 brouard 6008: /* Computes the values of the ncovmodel covariates of the model
6009: depending if the covariates are fixed or varying (age dependent) and stores them in cov[]
6010: Then computes with function pmij which return a matrix p[i][j] giving the elementary probability
6011: to be observed in j being in i according to the model.
6012: */
1.243 brouard 6013: /* ioffset=2+nagesqr+cptcovage; */
6014: ioffset=2+nagesqr;
1.232 brouard 6015: /* Fixed */
1.224 brouard 6016: /* for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i]; */
1.232 brouard 6017: /* for (k=1; k<=ncoveff;k++){ /\* Simple and product fixed Dummy covariates without age* products *\/ */
1.349 brouard 6018: 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 */
1.339 brouard 6019: /* printf("Debug3 TvarFind[%d]=%d",kf, TvarFind[kf]); */
6020: /* printf(" Tvar[TvarFind[kf]]=%d", Tvar[TvarFind[kf]]); */
6021: /* printf(" i=%d covar[Tvar[TvarFind[kf]]][i]=%f\n",i,covar[Tvar[TvarFind[kf]]][i]); */
1.335 brouard 6022: 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)*/
1.232 brouard 6023: /* cov[ioffset+TvarFind[1]]=covar[Tvar[TvarFind[1]]][i]; */
6024: /* cov[2+6]=covar[Tvar[6]][i]; */
6025: /* cov[2+6]=covar[2][i]; V2 */
6026: /* cov[TvarFind[2]]=covar[Tvar[TvarFind[2]]][i]; */
6027: /* cov[2+7]=covar[Tvar[7]][i]; */
6028: /* cov[2+7]=covar[7][i]; V7=V1*V2 */
6029: /* cov[TvarFind[3]]=covar[Tvar[TvarFind[3]]][i]; */
6030: /* cov[2+9]=covar[Tvar[9]][i]; */
6031: /* cov[2+9]=covar[1][i]; V1 */
1.225 brouard 6032: }
1.336 brouard 6033: /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4]
6034: is 5, Tvar[3=age*V3] should not be computed because of age Tvar[4=V3*V2]=6
6035: has been calculated etc */
6036: /* For an individual i, wav[i] gives the number of effective waves */
6037: /* We compute the contribution to Likelihood of each effective transition
6038: mw[mi][i] is real wave of the mi th effectve wave */
6039: /* Then statuses are computed at each begin and end of an effective wave s1=s[ mw[mi][i] ][i];
6040: s2=s[mw[mi+1][i]][i];
1.341 brouard 6041: And the iv th varying covariate in the DATA is the cotvar[mw[mi+1][i]][ncovcol+nqv+iv][i]
1.336 brouard 6042: */
6043: /* This part may be useless now because everythin should be in covar */
1.232 brouard 6044: /* for (k=1; k<=nqfveff;k++){ /\* Simple and product fixed Quantitative covariates without age* products *\/ */
6045: /* 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?)*\/ */
6046: /* } */
1.231 brouard 6047: /* for(iqv=1; iqv <= nqfveff; iqv++){ /\* Quantitative fixed covariates *\/ */
6048: /* cov[++ioffset]=coqvar[Tvar[iqv]][i]; /\* Only V2 k=6 and V1*V2 7 *\/ */
6049: /* } */
1.225 brouard 6050:
1.233 brouard 6051:
6052: for(mi=1; mi<= wav[i]-1; mi++){ /* Varying with waves */
1.339 brouard 6053: /* 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 */
6054: /* for(k=1; k <= ncovv ; k++){ /\* Varying covariates (single and product but no age )*\/ */
6055: /* /\* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; *\/ */
6056: /* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]-ncovcol-nqv][i]; */
6057: /* } */
6058:
6059: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
6060: /* model V1+V3+age*V1+age*V3+V1*V3 */
6061: /* Tvar={1, 3, 1, 3, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
6062: /* TvarVV[1]=V3 (first time varying in the model equation, TvarVV[2]=V1 (in V1*V3) TvarVV[3]=3(V3) */
6063: /* We need the position of the time varying or product in the model */
6064: /* 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 */
6065: /* TvarVV gives the variable name */
1.340 brouard 6066: /* Other example V1 + V3 + V5 + age*V1 + age*V3 + age*V5 + V1*V3 + V3*V5 + V1*V5
6067: * k= 1 2 3 4 5 6 7 8 9
6068: * varying 1 2 3 4 5
6069: * ncovv 1 2 3 4 5 6 7 8
1.343 brouard 6070: * TvarVV[ncovv] V3 5 1 3 3 5 1 5
1.340 brouard 6071: * TvarVVind 2 3 7 7 8 8 9 9
6072: * TvarFind[k] 1 0 0 0 0 0 0 0 0
6073: */
1.345 brouard 6074: /* Other model ncovcol=5 nqv=0 ntv=3 nqtv=0 nlstate=3
1.349 brouard 6075: * 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
1.345 brouard 6076: * FixedV[ncovcol+qv+ntv+nqtv] V5
1.349 brouard 6077: * 3 V1 V2 V3 V4 V5 V6 V7 V8 V3*V2 V7*V2 V6*V3 V7*V3 V6*V4 V7*V4
6078: * 0 0 0 0 0 1 1 1 0, 0, 1,1, 1, 0, 1, 0, 1, 0, 1, 0}
6079: * 3 0 0 0 0 0 1 1 1 0, 1 1 1 1 1}
6080: * model= V2 + V3 + V4 + V6 + V7 + V6*V2 + V7*V2 + V6*V3 + V7*V3 + V6*V4 + V7*V4
6081: * +age*V2 +age*V3 +age*V4 +age*V6 + age*V7
6082: * +age*V6*V2 + age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4
6083: * model2= V2 + V3 + V4 + V6 + V7 + V3*V2 + V7*V2 + V6*V3 + V7*V3 + V6*V4 + V7*V4
6084: * +age*V2 +age*V3 +age*V4 +age*V6 + age*V7
6085: * +age*V3*V2 + age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4
6086: * model3= V2 + V3 + V4 + V6 + V7 + age*V3*V2 + V7*V2 + V6*V3 + V7*V3 + V6*V4 + V7*V4
6087: * +age*V2 +age*V3 +age*V4 +age*V6 + age*V7
6088: * +V3*V2 + age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4
6089: * kmodel 1 2 3 4 5 6 7 8 9 10 11
6090: * 12 13 14 15 16
6091: * 17 18 19 20 21
6092: * Tvar[kmodel] 2 3 4 6 7 9 10 11 12 13 14
6093: * 2 3 4 6 7
6094: * 9 11 12 13 14
6095: * cptcovage=5+5 total of covariates with age
6096: * Tage[cptcovage] age*V2=12 13 14 15 16
6097: *1 17 18 19 20 21 gives the position in model of covariates associated with age
6098: *3 Tage[cptcovage] age*V3*V2=6
6099: *3 age*V2=12 13 14 15 16
6100: *3 age*V6*V3=18 19 20 21
6101: * Tvar[Tage[cptcovage]] Tvar[12]=2 3 4 6 Tvar[16]=7(age*V7)
6102: * 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
6103: * 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
6104: * 3 Tvar[Tage[cptcovage]] Tvar[6]=9 Tvar[12]=2 3 4 6 Tvar[16]=7(age*V7)
6105: * 3 Tvar[18]age*V6*V3=11 age*V7*V3=12 age*V6*V4=13 Tvar[21]age*V7*V4=14
6106: * 3 Tage[cptcovage] age*V3*V2=6 age*V2=12 age*V3 13 14 15 16
6107: * age*V6*V3=18 19 20 21 gives the position in model of covariates associated with age
6108: * 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
6109: * Tvar= {2, 3, 4, 6, 7,
6110: * 9, 10, 11, 12, 13, 14,
6111: * Tvar[12]=2, 3, 4, 6, 7,
6112: * Tvar[17]=9, 11, 12, 13, 14}
6113: * Typevar[1]@21 = {0, 0, 0, 0, 0,
6114: * 2, 2, 2, 2, 2, 2,
6115: * 3 3, 2, 2, 2, 2, 2,
6116: * 1, 1, 1, 1, 1,
6117: * 3, 3, 3, 3, 3}
6118: * 3 2, 3, 3, 3, 3}
6119: * 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
6120: * p Tprod[1]@21 {6, 7, 8, 9, 10, 11, 0 <repeats 15 times>}
6121: * 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}
6122: * 3 Tprod[1]@21 {17, 7, 8, 9, 10, 11, 0 <repeats 15 times>}
6123: * cptcovprod=11 (6+5)
6124: * FixedV[Tvar[Tage[cptcovage]]]] FixedV[2]=0 FixedV[3]=0 0 1 (age*V7)Tvar[16]=1 FixedV[absolute] not [kmodel]
6125: * FixedV[Tvar[17]=FixedV[age*V6*V2]=FixedV[9]=1 1 1 1 1
6126: * 3 FixedV[Tvar[17]=FixedV[age*V3*V2]=FixedV[9]=0 [11]=1 1 1 1
6127: * FixedV[] V1=0 V2=0 V3=0 v4=0 V5=0 V6=1 V7=1 v8=1 OK then model dependent
6128: * 9=1 [V7*V2]=[10]=1 11=1 12=1 13=1 14=1
6129: * 3 9=0 [V7*V2]=[10]=1 11=1 12=1 13=1 14=1
6130: * cptcovdageprod=5 for gnuplot printing
6131: * cptcovprodvage=6
6132: * ncova=15 1 2 3 4 5
6133: * 6 7 8 9 10 11 12 13 14 15
6134: * TvarA 2 3 4 6 7
6135: * 6 2 6 7 7 3 6 4 7 4
6136: * TvaAind 12 12 13 13 14 14 15 15 16 16
1.345 brouard 6137: * ncovf 1 2 3
1.349 brouard 6138: * V6 V7 V6*V2 V7*V2 V6*V3 V7*V3 V6*V4 V7*V4
6139: * ncovvt=14 1 2 3 4 5 6 7 8 9 10 11 12 13 14
6140: * TvarVV[1]@14 = itv {V6=6, 7, V6*V2=6, 2, 7, 2, 6, 3, 7, 3, 6, 4, 7, 4}
6141: * TvarVVind[1]@14= {4, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11}
6142: * 3 ncovvt=12 V6 V7 V7*V2 V6*V3 V7*V3 V6*V4 V7*V4
6143: * 3 TvarVV[1]@12 = itv {6, 7, V7*V2=7, 2, 6, 3, 7, 3, 6, 4, 7, 4}
6144: * 3 1 2 3 4 5 6 7 8 9 10 11 12
6145: * TvarVVind[1]@12= {V6 is in k=4, 5, 7,(4isV2)=7, 8, 8, 9, 9, 10,10, 11,11}TvarVVind[12]=k=11
6146: * TvarV 6, 7, 9, 10, 11, 12, 13, 14
6147: * 3 cptcovprodvage=6
6148: * 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
6149: * 3 TvarAVVA[1]@15= itva 3 2 2 3 4 6 7 6 3 7 3 6 4 7 4
6150: * 3 ncovta 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1.354 brouard 6151: *?TvarAVVAind[1]@15= V3 is in k=2 1 1 2 3 4 5 4,2 5,2, 4,3 5 3}TvarVVAind[]
1.349 brouard 6152: * TvarAVVAind[1]@15= V3 is in k=6 6 12 13 14 15 16 18 18 19,19, 20,20 21,21}TvarVVAind[]
6153: * 3 ncovvta=10 +age*V6 + age*V7 + age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4
6154: * 3 we want to compute =cotvar[mw[mi][i]][TvarVVA[ncovva]][i] at position TvarVVAind[ncovva]
6155: * 3 TvarVVA[1]@10= itva 6 7 6 3 7 3 6 4 7 4
6156: * 3 ncovva 1 2 3 4 5 6 7 8 9 10
6157: * TvarVVAind[1]@10= V6 is in k=4 5 8,8 9, 9, 10,10 11 11}TvarVVAind[]
6158: * TvarVVAind[1]@10= 15 16 18,18 19,19, 20,20 21 21}TvarVVAind[]
6159: * TvarVA V3*V2=6 6 , 1, 2, 11, 12, 13, 14
1.345 brouard 6160: * TvarFind[1]@14= {1, 2, 3, 0 <repeats 12 times>}
1.349 brouard 6161: * Tvar[1]@21= {2, 3, 4, 6, 7, 9, 10, 11, 12, 13, 14,
6162: * 2, 3, 4, 6, 7,
6163: * 6, 8, 9, 10, 11}
1.345 brouard 6164: * TvarFind[itv] 0 0 0
6165: * FixedV[itv] 1 1 1 0 1 0 1 0 1 0 0
1.354 brouard 6166: *? FixedV[itv] 1 1 1 0 1 0 1 0 1 0 1 0 1 0
1.345 brouard 6167: * Tvar[TvarFind[ncovf]]=[1]=2 [2]=3 [4]=4
6168: * Tvar[TvarFind[itv]] [0]=? ?ncovv 1 à ncovvt]
6169: * Not a fixed cotvar[mw][itv][i] 6 7 6 2 7, 2, 6, 3, 7, 3, 6, 4, 7, 4}
1.349 brouard 6170: * fixed covar[itv] [6] [7] [6][2]
1.345 brouard 6171: */
6172:
1.349 brouard 6173: 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 */
6174: 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 */
1.340 brouard 6175: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
1.345 brouard 6176: /* if(TvarFind[itv]==0){ /\* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv *\/ */
6177: if(FixedV[itv]!=0){ /* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv */
1.354 brouard 6178: /* printf("DEBUG ncovv=%d, Varying TvarVV[ncovv]=%d\n",ncovv, TvarVV[ncovv]); */
1.345 brouard 6179: cotvarv=cotvar[mw[mi][i]][TvarVV[ncovv]][i]; /* because cotvar starts now at first ncovcol+nqv+ntv+nqtv (1 to nqtv) */
1.354 brouard 6180: /* printf("DEBUG Varying cov[ioffset+ipos=%d]=%g \n",ioffset+ipos,cotvarv); */
1.340 brouard 6181: }else{ /* fixed covariate */
1.345 brouard 6182: /* cotvarv=covar[Tvar[TvarFind[itv]]][i]; /\* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model *\/ */
1.354 brouard 6183: /* printf("DEBUG ncovv=%d, Fixed TvarVV[ncovv]=%d\n",ncovv, TvarVV[ncovv]); */
1.349 brouard 6184: cotvarv=covar[itv][i]; /* Good: In V6*V3, 3 is fixed at position of the data */
1.354 brouard 6185: /* printf("DEBUG Fixed cov[ioffset+ipos=%d]=%g \n",ioffset+ipos,cotvarv); */
1.340 brouard 6186: }
1.339 brouard 6187: if(ipos!=iposold){ /* Not a product or first of a product */
1.340 brouard 6188: cotvarvold=cotvarv;
6189: }else{ /* A second product */
6190: cotvarv=cotvarv*cotvarvold;
1.339 brouard 6191: }
6192: iposold=ipos;
1.340 brouard 6193: cov[ioffset+ipos]=cotvarv;
1.354 brouard 6194: /* printf("DEBUG Product cov[ioffset+ipos=%d] \n",ioffset+ipos); */
1.339 brouard 6195: /* For products */
6196: }
6197: /* for(itv=1; itv <= ntveff; itv++){ /\* Varying dummy covariates single *\/ */
6198: /* iv=TvarVDind[itv]; /\* iv, position in the model equation of time varying covariate itv *\/ */
6199: /* /\* "V1+V3+age*V1+age*V3+V1*V3" with V3 time varying *\/ */
6200: /* /\* 1 2 3 4 5 *\/ */
6201: /* /\*itv 1 *\/ */
6202: /* /\* TvarVInd[1]= 2 *\/ */
6203: /* /\* iv= Tvar[Tmodelind[itv]]-ncovcol-nqv; /\\* Counting the # varying covariate from 1 to ntveff *\\/ *\/ */
6204: /* /\* iv= Tvar[Tmodelind[ioffset-2-nagesqr-cptcovage+itv]]-ncovcol-nqv; *\/ */
6205: /* /\* cov[ioffset+iv]=cotvar[mw[mi][i]][iv][i]; *\/ */
6206: /* /\* k=ioffset-2-nagesqr-cptcovage+itv; /\\* position in simple model *\\/ *\/ */
6207: /* /\* cov[ioffset+iv]=cotvar[mw[mi][i]][TmodelInvind[itv]][i]; *\/ */
6208: /* cov[ioffset+iv]=cotvar[mw[mi][i]][itv][i]; */
6209: /* /\* 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]); *\/ */
6210: /* } */
1.232 brouard 6211: /* for(iqtv=1; iqtv <= nqtveff; iqtv++){ /\* Varying quantitatives covariates *\/ */
1.242 brouard 6212: /* iv=TmodelInvQind[iqtv]; /\* Counting the # varying covariate from 1 to ntveff *\/ */
6213: /* /\* 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]); *\/ */
6214: /* cov[ioffset+ntveff+iqtv]=cotqvar[mw[mi][i]][TmodelInvQind[iqtv]][i]; */
1.232 brouard 6215: /* } */
1.126 brouard 6216: for (ii=1;ii<=nlstate+ndeath;ii++)
1.242 brouard 6217: for (j=1;j<=nlstate+ndeath;j++){
6218: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
6219: savm[ii][j]=(ii==j ? 1.0 : 0.0);
6220: }
1.214 brouard 6221:
6222: agebegin=agev[mw[mi][i]][i]; /* Age at beginning of effective wave */
6223: ageend=agev[mw[mi][i]][i] + (dh[mi][i])*stepm/YEARM; /* Age at end of effective wave and at the end of transition */
6224: for(d=0; d<dh[mi][i]; d++){ /* Delay between two effective waves */
1.247 brouard 6225: /* for(d=0; d<=0; d++){ /\* Delay between two effective waves Only one matrix to speed up*\/ */
1.242 brouard 6226: /*dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
6227: and mw[mi+1][i]. dh depends on stepm.*/
6228: newm=savm;
1.247 brouard 6229: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM; /* Here d is needed */
1.242 brouard 6230: cov[2]=agexact;
6231: if(nagesqr==1)
6232: cov[3]= agexact*agexact;
1.349 brouard 6233: for(ncovva=1, iposold=0; ncovva <= ncovta ; ncovva++){ /* Time varying covariates with age including individual from products, product is computed dynamically */
6234: itv=TvarAVVA[ncovva]; /* TvarVV={3, 1, 3} gives the name of each varying covariate, exploding product Vn*Vm into Vn and then Vm */
6235: ipos=TvarAVVAind[ncovva]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
6236: /* if(TvarFind[itv]==0){ /\* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv *\/ */
6237: if(FixedV[itv]!=0){ /* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv */
6238: /* printf("DEBUG ncovva=%d, Varying TvarAVVA[ncovva]=%d\n", ncovva, TvarAVVA[ncovva]); */
6239: cotvarv=cotvar[mw[mi][i]][TvarAVVA[ncovva]][i]; /* because cotvar starts now at first ncovcol+nqv+ntv+nqtv (1 to nqtv) */
6240: }else{ /* fixed covariate */
6241: /* cotvarv=covar[Tvar[TvarFind[itv]]][i]; /\* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model *\/ */
6242: /* printf("DEBUG ncovva=%d, Fixed TvarAVVA[ncovva]=%d\n", ncovva, TvarAVVA[ncovva]); */
6243: cotvarv=covar[itv][i]; /* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model */
6244: }
6245: if(ipos!=iposold){ /* Not a product or first of a product */
6246: cotvarvold=cotvarv;
6247: }else{ /* A second product */
6248: /* printf("DEBUG * \n"); */
6249: cotvarv=cotvarv*cotvarvold;
6250: }
6251: iposold=ipos;
6252: /* printf("DEBUG Product cov[ioffset+ipos=%d] \n",ioffset+ipos); */
6253: cov[ioffset+ipos]=cotvarv*agexact;
6254: /* For products */
1.242 brouard 6255: }
1.349 brouard 6256:
1.242 brouard 6257: /* printf("i=%d,mi=%d,d=%d,mw[mi][i]=%d\n",i, mi,d,mw[mi][i]); */
6258: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
6259: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
6260: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
6261: /* out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath, */
6262: /* 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate)); */
6263: savm=oldm;
6264: oldm=newm;
1.126 brouard 6265: } /* end mult */
1.336 brouard 6266: /*lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ /* Original formula */
6267: /* But now since version 0.9 we anticipate for bias at large stepm.
6268: * If stepm is larger than one month (smallest stepm) and if the exact delay
6269: * (in months) between two waves is not a multiple of stepm, we rounded to
6270: * the nearest (and in case of equal distance, to the lowest) interval but now
6271: * we keep into memory the bias bh[mi][i] and also the previous matrix product
6272: * (i.e to dh[mi][i]-1) saved in 'savm'. Then we inter(extra)polate the
6273: * probability in order to take into account the bias as a fraction of the way
6274: * from savm to out if bh is negative or even beyond if bh is positive. bh varies
6275: * -stepm/2 to stepm/2 .
6276: * For stepm=1 the results are the same as for previous versions of Imach.
6277: * For stepm > 1 the results are less biased than in previous versions.
6278: */
1.126 brouard 6279: s1=s[mw[mi][i]][i];
6280: s2=s[mw[mi+1][i]][i];
1.217 brouard 6281: /* if(s2==-1){ */
1.268 brouard 6282: /* printf(" ERROR s1=%d, s2=%d i=%d \n", s1, s2, i); */
1.217 brouard 6283: /* /\* exit(1); *\/ */
6284: /* } */
1.126 brouard 6285: bbh=(double)bh[mi][i]/(double)stepm;
6286: /* bias is positive if real duration
6287: * is higher than the multiple of stepm and negative otherwise.
6288: */
6289: if( s2 > nlstate && (mle <5) ){ /* Jackson */
1.242 brouard 6290: lli=log(out[s1][s2] - savm[s1][s2]);
1.216 brouard 6291: } else if ( s2==-1 ) { /* alive */
1.242 brouard 6292: for (j=1,survp=0. ; j<=nlstate; j++)
6293: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
6294: lli= log(survp);
1.126 brouard 6295: }else if (mle==1){
1.242 brouard 6296: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
1.126 brouard 6297: } else if(mle==2){
1.242 brouard 6298: lli= (savm[s1][s2]>(double)1.e-8 ?log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]):log((1.+bbh)*out[s1][s2])); /* linear interpolation */
1.126 brouard 6299: } else if(mle==3){ /* exponential inter-extrapolation */
1.242 brouard 6300: lli= (savm[s1][s2]>(double)1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2])); /* exponential inter-extrapolation */
1.126 brouard 6301: } else if (mle==4){ /* mle=4 no inter-extrapolation */
1.242 brouard 6302: lli=log(out[s1][s2]); /* Original formula */
1.136 brouard 6303: } else{ /* mle=0 back to 1 */
1.242 brouard 6304: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
6305: /*lli=log(out[s1][s2]); */ /* Original formula */
1.126 brouard 6306: } /* End of if */
6307: ipmx +=1;
6308: sw += weight[i];
6309: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1.342 brouard 6310: /* Printing covariates values for each contribution for checking */
1.343 brouard 6311: /* 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])); */
1.126 brouard 6312: if(globpr){
1.246 brouard 6313: fprintf(ficresilk,"%09ld %6.1f %6.1f %6d %2d %2d %2d %2d %3d %15.6f %8.4f %8.3f\
1.126 brouard 6314: %11.6f %11.6f %11.6f ", \
1.242 brouard 6315: num[i], agebegin, ageend, i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],weight[i]*gipmx/gsw,
1.268 brouard 6316: 2*weight[i]*lli,(s2==-1? -1: out[s1][s2]),(s2==-1? -1: savm[s1][s2]));
1.343 brouard 6317: /* printf("%09ld %6.1f %6.1f %6d %2d %2d %2d %2d %3d %15.6f %8.4f %8.3f\ */
6318: /* %11.6f %11.6f %11.6f ", \ */
6319: /* num[i], agebegin, ageend, i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],weight[i]*gipmx/gsw, */
6320: /* 2*weight[i]*lli,(s2==-1? -1: out[s1][s2]),(s2==-1? -1: savm[s1][s2])); */
1.242 brouard 6321: for(k=1,llt=0.,l=0.; k<=nlstate; k++){
6322: llt +=ll[k]*gipmx/gsw;
6323: fprintf(ficresilk," %10.6f",-ll[k]*gipmx/gsw);
1.335 brouard 6324: /* printf(" %10.6f",-ll[k]*gipmx/gsw); */
1.242 brouard 6325: }
1.343 brouard 6326: fprintf(ficresilk," %10.6f ", -llt);
1.335 brouard 6327: /* printf(" %10.6f\n", -llt); */
1.342 brouard 6328: /* if(debugILK){ /\* debugILK is set by a #d in a comment line *\/ */
1.343 brouard 6329: /* fprintf(ficresilk,"%09ld ", num[i]); */ /* not necessary */
6330: for (kf=1; kf<=ncovf;kf++){ /* Simple and product fixed covariates without age* products *//* Missing values are set to -1 but should be dropped */
6331: fprintf(ficresilk," %g",covar[Tvar[TvarFind[kf]]][i]);
6332: }
6333: for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){ /* Varying covariates (single and product but no age) including individual from products */
6334: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
6335: if(ipos!=iposold){ /* Not a product or first of a product */
6336: fprintf(ficresilk," %g",cov[ioffset+ipos]);
6337: /* printf(" %g",cov[ioffset+ipos]); */
6338: }else{
6339: fprintf(ficresilk,"*");
6340: /* printf("*"); */
1.342 brouard 6341: }
1.343 brouard 6342: iposold=ipos;
6343: }
1.349 brouard 6344: /* for (kk=1; kk<=cptcovage;kk++) { */
6345: /* if(!FixedV[Tvar[Tage[kk]]]){ */
6346: /* fprintf(ficresilk," %g*age",covar[Tvar[Tage[kk]]][i]); */
6347: /* /\* printf(" %g*age",covar[Tvar[Tage[kk]]][i]); *\/ */
6348: /* }else{ */
6349: /* fprintf(ficresilk," %g*age",cotvar[mw[mi][i]][Tvar[Tage[kk]]][i]);/\* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) *\/ */
6350: /* /\* printf(" %g*age",cotvar[mw[mi][i]][Tvar[Tage[kk]]][i]);/\\* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) *\\/ *\/ */
6351: /* } */
6352: /* } */
6353: for(ncovva=1, iposold=0; ncovva <= ncovta ; ncovva++){ /* Time varying covariates with age including individual from products, product is computed dynamically */
6354: itv=TvarAVVA[ncovva]; /* TvarVV={3, 1, 3} gives the name of each varying covariate, exploding product Vn*Vm into Vn and then Vm */
6355: ipos=TvarAVVAind[ncovva]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
6356: /* if(TvarFind[itv]==0){ /\* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv *\/ */
6357: if(FixedV[itv]!=0){ /* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv */
6358: /* printf("DEBUG ncovva=%d, Varying TvarAVVA[ncovva]=%d\n", ncovva, TvarAVVA[ncovva]); */
6359: cotvarv=cotvar[mw[mi][i]][TvarAVVA[ncovva]][i]; /* because cotvar starts now at first ncovcol+nqv+ntv+nqtv (1 to nqtv) */
6360: }else{ /* fixed covariate */
6361: /* cotvarv=covar[Tvar[TvarFind[itv]]][i]; /\* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model *\/ */
6362: /* printf("DEBUG ncovva=%d, Fixed TvarAVVA[ncovva]=%d\n", ncovva, TvarAVVA[ncovva]); */
6363: cotvarv=covar[itv][i]; /* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model */
6364: }
6365: if(ipos!=iposold){ /* Not a product or first of a product */
6366: cotvarvold=cotvarv;
6367: }else{ /* A second product */
6368: /* printf("DEBUG * \n"); */
6369: cotvarv=cotvarv*cotvarvold;
1.342 brouard 6370: }
1.349 brouard 6371: cotvarv=cotvarv*agexact;
6372: fprintf(ficresilk," %g*age",cotvarv);
6373: iposold=ipos;
6374: /* printf("DEBUG Product cov[ioffset+ipos=%d] \n",ioffset+ipos); */
6375: cov[ioffset+ipos]=cotvarv;
6376: /* For products */
1.343 brouard 6377: }
6378: /* printf("\n"); */
1.342 brouard 6379: /* } /\* End debugILK *\/ */
6380: fprintf(ficresilk,"\n");
6381: } /* End if globpr */
1.335 brouard 6382: } /* end of wave */
6383: } /* end of individual */
6384: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
1.232 brouard 6385: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
1.335 brouard 6386: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
6387: if(globpr==0){ /* First time we count the contributions and weights */
6388: gipmx=ipmx;
6389: gsw=sw;
6390: }
1.343 brouard 6391: return -l;
1.126 brouard 6392: }
6393:
6394:
6395: /*************** function likelione ***********/
1.292 brouard 6396: void likelione(FILE *ficres,double p[], int npar, int nlstate, int *globpri, long *ipmx, double *sw, double *fretone, double (*func)(double []))
1.126 brouard 6397: {
6398: /* This routine should help understanding what is done with
6399: the selection of individuals/waves and
6400: to check the exact contribution to the likelihood.
6401: Plotting could be done.
1.342 brouard 6402: */
6403: void pstamp(FILE *ficres);
1.343 brouard 6404: int k, kf, kk, kvar, ncovv, iposold, ipos;
1.126 brouard 6405:
6406: if(*globpri !=0){ /* Just counts and sums, no printings */
1.201 brouard 6407: strcpy(fileresilk,"ILK_");
1.202 brouard 6408: strcat(fileresilk,fileresu);
1.126 brouard 6409: if((ficresilk=fopen(fileresilk,"w"))==NULL) {
6410: printf("Problem with resultfile: %s\n", fileresilk);
6411: fprintf(ficlog,"Problem with resultfile: %s\n", fileresilk);
6412: }
1.342 brouard 6413: pstamp(ficresilk);fprintf(ficresilk,"# model=1+age+%s\n",model);
1.214 brouard 6414: 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");
6415: fprintf(ficresilk, "#num_i ageb agend i s1 s2 mi mw dh likeli weight %%weight 2wlli out sav ");
1.126 brouard 6416: /* i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],2*weight[i]*lli,out[s1][s2],savm[s1][s2]); */
6417: for(k=1; k<=nlstate; k++)
6418: fprintf(ficresilk," -2*gipw/gsw*weight*ll[%d]++",k);
1.342 brouard 6419: fprintf(ficresilk," -2*gipw/gsw*weight*ll(total) ");
6420:
6421: /* if(debugILK){ /\* debugILK is set by a #d in a comment line *\/ */
6422: for(kf=1;kf <= ncovf; kf++){
6423: fprintf(ficresilk,"V%d",Tvar[TvarFind[kf]]);
6424: /* printf("V%d",Tvar[TvarFind[kf]]); */
6425: }
6426: for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){
1.343 brouard 6427: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate */
1.342 brouard 6428: if(ipos!=iposold){ /* Not a product or first of a product */
6429: /* printf(" %d",ipos); */
6430: fprintf(ficresilk," V%d",TvarVV[ncovv]);
6431: }else{
6432: /* printf("*"); */
6433: fprintf(ficresilk,"*");
1.343 brouard 6434: }
1.342 brouard 6435: iposold=ipos;
6436: }
6437: for (kk=1; kk<=cptcovage;kk++) {
6438: if(!FixedV[Tvar[Tage[kk]]]){
6439: /* printf(" %d*age(Fixed)",Tvar[Tage[kk]]); */
6440: fprintf(ficresilk," %d*age(Fixed)",Tvar[Tage[kk]]);
6441: }else{
6442: fprintf(ficresilk," %d*age(Varying)",Tvar[Tage[kk]]);/* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) */
6443: /* printf(" %d*age(Varying)",Tvar[Tage[kk]]);/\* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) *\/ */
6444: }
6445: }
6446: /* } /\* End if debugILK *\/ */
6447: /* printf("\n"); */
6448: fprintf(ficresilk,"\n");
6449: } /* End glogpri */
1.126 brouard 6450:
1.292 brouard 6451: *fretone=(*func)(p);
1.126 brouard 6452: if(*globpri !=0){
6453: fclose(ficresilk);
1.205 brouard 6454: if (mle ==0)
6455: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with initial parameters and mle = %d.",mle);
6456: else if(mle >=1)
6457: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with optimized parameters mle = %d.",mle);
6458: fprintf(fichtm," You should at least run with mle >= 1 to get starting values corresponding to the optimized parameters in order to visualize the real contribution of each individual/wave: <a href=\"%s\">%s</a><br>\n",subdirf(fileresilk),subdirf(fileresilk));
1.274 brouard 6459: fprintf(fichtm,"\n<br>Equation of the model: <b>model=1+age+%s</b><br>\n",model);
1.208 brouard 6460:
1.207 brouard 6461: fprintf(fichtm,"<br>- The function drawn is -2Log(L) in Log scale: by state of origin <a href=\"%s-ori.png\">%s-ori.png</a><br> \
1.343 brouard 6462: <img src=\"%s-ori.png\">\n",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
1.207 brouard 6463: fprintf(fichtm,"<br>- and by state of destination <a href=\"%s-dest.png\">%s-dest.png</a><br> \
1.343 brouard 6464: <img src=\"%s-dest.png\">\n",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
6465:
6466: for (k=1; k<= nlstate ; k++) {
6467: 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 \
6468: <img src=\"%s-p%dj.png\">\n",k,k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k);
6469: for(kf=1; kf <= ncovf; kf++){ /* For each simple dummy covariate of the model */
1.350 brouard 6470: kvar=Tvar[TvarFind[kf]]; /* variable */
6471: fprintf(fichtm,"<br>- Probability p<sub>%dj</sub> by origin %d and destination j with colored covariate V%d. Same dot size of all points but with a different color for transitions with dummy variable V%d=1 at beginning of transition (keeping former color for V%d=0): ",k,k,Tvar[TvarFind[kf]],Tvar[TvarFind[kf]],Tvar[TvarFind[kf]]);
6472: fprintf(fichtm,"<a href=\"%s-p%dj-%d.png\">%s-p%dj-%d.png</a><br>",subdirf2(optionfilefiname,"ILK_"),k,kvar,subdirf2(optionfilefiname,"ILK_"),k,kvar);
6473: fprintf(fichtm,"<img src=\"%s-p%dj-%d.png\">",subdirf2(optionfilefiname,"ILK_"),k,Tvar[TvarFind[kf]]);
1.343 brouard 6474: }
6475: for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){ /* Loop on the time varying extended covariates (with extension of Vn*Vm */
6476: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate */
6477: kvar=TvarVV[ncovv]; /* TvarVV={3, 1, 3} gives the name of each varying covariate */
6478: /* 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]); */
6479: if(ipos!=iposold){ /* Not a product or first of a product */
6480: /* fprintf(ficresilk," V%d",TvarVV[ncovv]); */
6481: /* printf(" DebugILK fichtm ipos=%d != iposold=%d\n", ipos, iposold); */
6482: 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) */
6483: 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> \
6484: <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);
6485: } /* End only for dummies time varying (single?) */
6486: }else{ /* Useless product */
6487: /* printf("*"); */
6488: /* fprintf(ficresilk,"*"); */
6489: }
6490: iposold=ipos;
6491: } /* For each time varying covariate */
6492: } /* End loop on states */
6493:
6494: /* if(debugILK){ */
6495: /* for(kf=1; kf <= ncovf; kf++){ /\* For each simple dummy covariate of the model *\/ */
6496: /* /\* kvar=Tvar[TvarFind[kf]]; *\/ /\* variable *\/ */
6497: /* for (k=1; k<= nlstate ; k++) { */
6498: /* 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> \ */
6499: /* <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]]); */
6500: /* } */
6501: /* } */
6502: /* for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){ /\* Loop on the time varying extended covariates (with extension of Vn*Vm *\/ */
6503: /* ipos=TvarVVind[ncovv]; /\* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate *\/ */
6504: /* kvar=TvarVV[ncovv]; /\* TvarVV={3, 1, 3} gives the name of each varying covariate *\/ */
6505: /* /\* 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]); *\/ */
6506: /* if(ipos!=iposold){ /\* Not a product or first of a product *\/ */
6507: /* /\* fprintf(ficresilk," V%d",TvarVV[ncovv]); *\/ */
6508: /* /\* printf(" DebugILK fichtm ipos=%d != iposold=%d\n", ipos, iposold); *\/ */
6509: /* 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) *\/ */
6510: /* for (k=1; k<= nlstate ; k++) { */
6511: /* 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> \ */
6512: /* <img src=\"%s-p%dj-%d.png\">",k,k,kvar,kvar,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,kvar); */
6513: /* } /\* End state *\/ */
6514: /* } /\* End only for dummies time varying (single?) *\/ */
6515: /* }else{ /\* Useless product *\/ */
6516: /* /\* printf("*"); *\/ */
6517: /* /\* fprintf(ficresilk,"*"); *\/ */
6518: /* } */
6519: /* iposold=ipos; */
6520: /* } /\* For each time varying covariate *\/ */
6521: /* }/\* End debugILK *\/ */
1.207 brouard 6522: fflush(fichtm);
1.343 brouard 6523: }/* End globpri */
1.126 brouard 6524: return;
6525: }
6526:
6527:
6528: /*********** Maximum Likelihood Estimation ***************/
6529:
6530: void mlikeli(FILE *ficres,double p[], int npar, int ncovmodel, int nlstate, double ftol, double (*func)(double []))
6531: {
1.359 brouard 6532: int i,j, jkk=0, iter=0;
1.126 brouard 6533: double **xi;
1.359 brouard 6534: /*double fret;*/
6535: /*double fretone;*/ /* Only one call to likelihood */
1.126 brouard 6536: /* char filerespow[FILENAMELENGTH];*/
1.354 brouard 6537:
1.359 brouard 6538: /*double * p1;*/ /* Shifted parameters from 0 instead of 1 */
1.162 brouard 6539: #ifdef NLOPT
6540: int creturn;
6541: nlopt_opt opt;
6542: /* double lb[9] = { -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL }; /\* lower bounds *\/ */
6543: double *lb;
6544: double minf; /* the minimum objective value, upon return */
1.354 brouard 6545:
1.162 brouard 6546: myfunc_data dinst, *d = &dinst;
6547: #endif
6548:
6549:
1.126 brouard 6550: xi=matrix(1,npar,1,npar);
1.357 brouard 6551: for (i=1;i<=npar;i++) /* Starting with canonical directions j=1,n xi[i=1,n][j] */
1.126 brouard 6552: for (j=1;j<=npar;j++)
6553: xi[i][j]=(i==j ? 1.0 : 0.0);
1.359 brouard 6554: printf("Powell-prax\n"); fprintf(ficlog,"Powell-prax\n");
1.201 brouard 6555: strcpy(filerespow,"POW_");
1.126 brouard 6556: strcat(filerespow,fileres);
6557: if((ficrespow=fopen(filerespow,"w"))==NULL) {
6558: printf("Problem with resultfile: %s\n", filerespow);
6559: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
6560: }
6561: fprintf(ficrespow,"# Powell\n# iter -2*LL");
6562: for (i=1;i<=nlstate;i++)
6563: for(j=1;j<=nlstate+ndeath;j++)
6564: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
6565: fprintf(ficrespow,"\n");
1.162 brouard 6566: #ifdef POWELL
1.319 brouard 6567: #ifdef LINMINORIGINAL
6568: #else /* LINMINORIGINAL */
6569:
6570: flatdir=ivector(1,npar);
6571: for (j=1;j<=npar;j++) flatdir[j]=0;
6572: #endif /*LINMINORIGINAL */
6573:
6574: #ifdef FLATSUP
6575: powell(p,xi,npar,ftol,&iter,&fret,flatdir,func);
6576: /* reorganizing p by suppressing flat directions */
6577: for(i=1, jk=1; i <=nlstate; i++){
6578: for(k=1; k <=(nlstate+ndeath); k++){
6579: if (k != i) {
6580: printf("%d%d flatdir[%d]=%d",i,k,jk, flatdir[jk]);
6581: if(flatdir[jk]==1){
6582: printf(" To be skipped %d%d flatdir[%d]=%d ",i,k,jk, flatdir[jk]);
6583: }
6584: for(j=1; j <=ncovmodel; j++){
6585: printf("%12.7f ",p[jk]);
6586: jk++;
6587: }
6588: printf("\n");
6589: }
6590: }
6591: }
6592: /* skipping */
6593: /* for(i=1, jk=1, jkk=1;(flatdir[jk]==0)&& (i <=nlstate); i++){ */
6594: for(i=1, jk=1, jkk=1;i <=nlstate; i++){
6595: for(k=1; k <=(nlstate+ndeath); k++){
6596: if (k != i) {
6597: printf("%d%d flatdir[%d]=%d",i,k,jk, flatdir[jk]);
6598: if(flatdir[jk]==1){
6599: printf(" To be skipped %d%d flatdir[%d]=%d jk=%d p[%d] ",i,k,jk, flatdir[jk],jk, jk);
6600: for(j=1; j <=ncovmodel; jk++,j++){
6601: printf(" p[%d]=%12.7f",jk, p[jk]);
6602: /*q[jjk]=p[jk];*/
6603: }
6604: }else{
6605: printf(" To be kept %d%d flatdir[%d]=%d jk=%d q[%d]=p[%d] ",i,k,jk, flatdir[jk],jk, jkk, jk);
6606: for(j=1; j <=ncovmodel; jk++,jkk++,j++){
6607: printf(" p[%d]=%12.7f=q[%d]",jk, p[jk],jkk);
6608: /*q[jjk]=p[jk];*/
6609: }
6610: }
6611: printf("\n");
6612: }
6613: fflush(stdout);
6614: }
6615: }
6616: powell(p,xi,npar,ftol,&iter,&fret,flatdir,func);
6617: #else /* FLATSUP */
1.359 brouard 6618: /* powell(p,xi,npar,ftol,&iter,&fret,func);*/
6619: /* praxis ( t0, h0, n, prin, x, beale_f ); */
1.364 brouard 6620: int prin=4;
1.362 brouard 6621: /* double h0=0.25; */
6622: /* double macheps; */
6623: /* double fmin; */
1.359 brouard 6624: macheps=pow(16.0,-13.0);
6625: /* #include "praxis.h" */
6626: /* Be careful that praxis start at x[0] and powell start at p[1] */
6627: /* praxis ( ftol, h0, npar, prin, p, func ); */
6628: /* p1= (p+1); */ /* p *(p+1)@8 and p *(p1)@8 are equal p1[0]=p[1] */
6629: printf("Praxis Gegenfurtner \n");
6630: fprintf(ficlog, "Praxis Gegenfurtner\n");fflush(ficlog);
6631: /* praxis ( ftol, h0, npar, prin, p1, func ); */
6632: /* fmin = praxis(1.e-5,macheps, h, n, prin, x, func); */
1.362 brouard 6633: ffmin = praxis(ftol,macheps, h0, npar, prin, p, func);
1.359 brouard 6634: printf("End Praxis\n");
1.319 brouard 6635: #endif /* FLATSUP */
6636:
6637: #ifdef LINMINORIGINAL
6638: #else
6639: free_ivector(flatdir,1,npar);
6640: #endif /* LINMINORIGINAL*/
6641: #endif /* POWELL */
1.126 brouard 6642:
1.162 brouard 6643: #ifdef NLOPT
6644: #ifdef NEWUOA
6645: opt = nlopt_create(NLOPT_LN_NEWUOA,npar);
6646: #else
6647: opt = nlopt_create(NLOPT_LN_BOBYQA,npar);
6648: #endif
6649: lb=vector(0,npar-1);
6650: for (i=0;i<npar;i++) lb[i]= -HUGE_VAL;
6651: nlopt_set_lower_bounds(opt, lb);
6652: nlopt_set_initial_step1(opt, 0.1);
6653:
6654: p1= (p+1); /* p *(p+1)@8 and p *(p1)@8 are equal p1[0]=p[1] */
6655: d->function = func;
6656: printf(" Func %.12lf \n",myfunc(npar,p1,NULL,d));
6657: nlopt_set_min_objective(opt, myfunc, d);
6658: nlopt_set_xtol_rel(opt, ftol);
6659: if ((creturn=nlopt_optimize(opt, p1, &minf)) < 0) {
6660: printf("nlopt failed! %d\n",creturn);
6661: }
6662: else {
6663: printf("found minimum after %d evaluations (NLOPT=%d)\n", countcallfunc ,NLOPT);
6664: printf("found minimum at f(%g,%g) = %0.10g\n", p[0], p[1], minf);
6665: iter=1; /* not equal */
6666: }
6667: nlopt_destroy(opt);
6668: #endif
1.319 brouard 6669: #ifdef FLATSUP
6670: /* npared = npar -flatd/ncovmodel; */
6671: /* xired= matrix(1,npared,1,npared); */
6672: /* paramred= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel); */
6673: /* powell(pred,xired,npared,ftol,&iter,&fret,flatdir,func); */
6674: /* free_matrix(xire,1,npared,1,npared); */
6675: #else /* FLATSUP */
6676: #endif /* FLATSUP */
1.126 brouard 6677: free_matrix(xi,1,npar,1,npar);
6678: fclose(ficrespow);
1.203 brouard 6679: printf("\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
6680: fprintf(ficlog,"\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
1.180 brouard 6681: fprintf(ficres,"#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
1.126 brouard 6682:
6683: }
6684:
6685: /**** Computes Hessian and covariance matrix ***/
1.203 brouard 6686: void hesscov(double **matcov, double **hess, double p[], int npar, double delti[], double ftolhess, double (*func)(double []))
1.126 brouard 6687: {
6688: double **a,**y,*x,pd;
1.203 brouard 6689: /* double **hess; */
1.164 brouard 6690: int i, j;
1.126 brouard 6691: int *indx;
6692:
6693: double hessii(double p[], double delta, int theta, double delti[],double (*func)(double []),int npar);
1.203 brouard 6694: double hessij(double p[], double **hess, double delti[], int i, int j,double (*func)(double []),int npar);
1.126 brouard 6695: void lubksb(double **a, int npar, int *indx, double b[]) ;
6696: void ludcmp(double **a, int npar, int *indx, double *d) ;
6697: double gompertz(double p[]);
1.203 brouard 6698: /* hess=matrix(1,npar,1,npar); */
1.126 brouard 6699:
6700: printf("\nCalculation of the hessian matrix. Wait...\n");
6701: fprintf(ficlog,"\nCalculation of the hessian matrix. Wait...\n");
6702: for (i=1;i<=npar;i++){
1.203 brouard 6703: printf("%d-",i);fflush(stdout);
6704: fprintf(ficlog,"%d-",i);fflush(ficlog);
1.126 brouard 6705:
6706: hess[i][i]=hessii(p,ftolhess,i,delti,func,npar);
6707:
6708: /* printf(" %f ",p[i]);
6709: printf(" %lf %lf %lf",hess[i][i],ftolhess,delti[i]);*/
6710: }
6711:
6712: for (i=1;i<=npar;i++) {
6713: for (j=1;j<=npar;j++) {
6714: if (j>i) {
1.203 brouard 6715: printf(".%d-%d",i,j);fflush(stdout);
6716: fprintf(ficlog,".%d-%d",i,j);fflush(ficlog);
6717: hess[i][j]=hessij(p,hess, delti,i,j,func,npar);
1.126 brouard 6718:
6719: hess[j][i]=hess[i][j];
6720: /*printf(" %lf ",hess[i][j]);*/
6721: }
6722: }
6723: }
6724: printf("\n");
6725: fprintf(ficlog,"\n");
6726:
6727: printf("\nInverting the hessian to get the covariance matrix. Wait...\n");
6728: fprintf(ficlog,"\nInverting the hessian to get the covariance matrix. Wait...\n");
6729:
6730: a=matrix(1,npar,1,npar);
6731: y=matrix(1,npar,1,npar);
6732: x=vector(1,npar);
6733: indx=ivector(1,npar);
6734: for (i=1;i<=npar;i++)
6735: for (j=1;j<=npar;j++) a[i][j]=hess[i][j];
6736: ludcmp(a,npar,indx,&pd);
6737:
6738: for (j=1;j<=npar;j++) {
6739: for (i=1;i<=npar;i++) x[i]=0;
6740: x[j]=1;
6741: lubksb(a,npar,indx,x);
6742: for (i=1;i<=npar;i++){
6743: matcov[i][j]=x[i];
6744: }
6745: }
6746:
6747: printf("\n#Hessian matrix#\n");
6748: fprintf(ficlog,"\n#Hessian matrix#\n");
6749: for (i=1;i<=npar;i++) {
6750: for (j=1;j<=npar;j++) {
1.203 brouard 6751: printf("%.6e ",hess[i][j]);
6752: fprintf(ficlog,"%.6e ",hess[i][j]);
1.126 brouard 6753: }
6754: printf("\n");
6755: fprintf(ficlog,"\n");
6756: }
6757:
1.203 brouard 6758: /* printf("\n#Covariance matrix#\n"); */
6759: /* fprintf(ficlog,"\n#Covariance matrix#\n"); */
6760: /* for (i=1;i<=npar;i++) { */
6761: /* for (j=1;j<=npar;j++) { */
6762: /* printf("%.6e ",matcov[i][j]); */
6763: /* fprintf(ficlog,"%.6e ",matcov[i][j]); */
6764: /* } */
6765: /* printf("\n"); */
6766: /* fprintf(ficlog,"\n"); */
6767: /* } */
6768:
1.126 brouard 6769: /* Recompute Inverse */
1.203 brouard 6770: /* for (i=1;i<=npar;i++) */
6771: /* for (j=1;j<=npar;j++) a[i][j]=matcov[i][j]; */
6772: /* ludcmp(a,npar,indx,&pd); */
6773:
6774: /* printf("\n#Hessian matrix recomputed#\n"); */
6775:
6776: /* for (j=1;j<=npar;j++) { */
6777: /* for (i=1;i<=npar;i++) x[i]=0; */
6778: /* x[j]=1; */
6779: /* lubksb(a,npar,indx,x); */
6780: /* for (i=1;i<=npar;i++){ */
6781: /* y[i][j]=x[i]; */
6782: /* printf("%.3e ",y[i][j]); */
6783: /* fprintf(ficlog,"%.3e ",y[i][j]); */
6784: /* } */
6785: /* printf("\n"); */
6786: /* fprintf(ficlog,"\n"); */
6787: /* } */
6788:
6789: /* Verifying the inverse matrix */
6790: #ifdef DEBUGHESS
6791: y=matprod2(y,hess,1,npar,1,npar,1,npar,matcov);
1.126 brouard 6792:
1.203 brouard 6793: printf("\n#Verification: multiplying the matrix of covariance by the Hessian matrix, should be unity:#\n");
6794: fprintf(ficlog,"\n#Verification: multiplying the matrix of covariance by the Hessian matrix. Should be unity:#\n");
1.126 brouard 6795:
6796: for (j=1;j<=npar;j++) {
6797: for (i=1;i<=npar;i++){
1.203 brouard 6798: printf("%.2f ",y[i][j]);
6799: fprintf(ficlog,"%.2f ",y[i][j]);
1.126 brouard 6800: }
6801: printf("\n");
6802: fprintf(ficlog,"\n");
6803: }
1.203 brouard 6804: #endif
1.126 brouard 6805:
6806: free_matrix(a,1,npar,1,npar);
6807: free_matrix(y,1,npar,1,npar);
6808: free_vector(x,1,npar);
6809: free_ivector(indx,1,npar);
1.203 brouard 6810: /* free_matrix(hess,1,npar,1,npar); */
1.126 brouard 6811:
6812:
6813: }
6814:
6815: /*************** hessian matrix ****************/
6816: double hessii(double x[], double delta, int theta, double delti[], double (*func)(double []), int npar)
1.203 brouard 6817: { /* Around values of x, computes the function func and returns the scales delti and hessian */
1.126 brouard 6818: int i;
6819: int l=1, lmax=20;
1.203 brouard 6820: double k1,k2, res, fx;
1.132 brouard 6821: double p2[MAXPARM+1]; /* identical to x */
1.126 brouard 6822: double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4;
6823: int k=0,kmax=10;
6824: double l1;
6825:
6826: fx=func(x);
6827: for (i=1;i<=npar;i++) p2[i]=x[i];
1.145 brouard 6828: for(l=0 ; l <=lmax; l++){ /* Enlarging the zone around the Maximum */
1.126 brouard 6829: l1=pow(10,l);
6830: delts=delt;
6831: for(k=1 ; k <kmax; k=k+1){
6832: delt = delta*(l1*k);
6833: p2[theta]=x[theta] +delt;
1.145 brouard 6834: k1=func(p2)-fx; /* Might be negative if too close to the theoretical maximum */
1.126 brouard 6835: p2[theta]=x[theta]-delt;
6836: k2=func(p2)-fx;
6837: /*res= (k1-2.0*fx+k2)/delt/delt; */
1.203 brouard 6838: res= (k1+k2)/delt/delt/2.; /* Divided by 2 because L and not 2*L */
1.126 brouard 6839:
1.203 brouard 6840: #ifdef DEBUGHESSII
1.126 brouard 6841: 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);
6842: 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);
6843: #endif
6844: /*if(fabs(k1-2.0*fx+k2) <1.e-13){ */
6845: if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)){
6846: k=kmax;
6847: }
6848: else if((k1 >khi/nkhif) || (k2 >khi/nkhif)){ /* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. */
1.164 brouard 6849: k=kmax; l=lmax*10;
1.126 brouard 6850: }
6851: else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){
6852: delts=delt;
6853: }
1.203 brouard 6854: } /* End loop k */
1.126 brouard 6855: }
6856: delti[theta]=delts;
6857: return res;
6858:
6859: }
6860:
1.203 brouard 6861: double hessij( double x[], double **hess, double delti[], int thetai,int thetaj,double (*func)(double []),int npar)
1.126 brouard 6862: {
6863: int i;
1.164 brouard 6864: int l=1, lmax=20;
1.126 brouard 6865: double k1,k2,k3,k4,res,fx;
1.132 brouard 6866: double p2[MAXPARM+1];
1.203 brouard 6867: int k, kmax=1;
6868: double v1, v2, cv12, lc1, lc2;
1.208 brouard 6869:
6870: int firstime=0;
1.203 brouard 6871:
1.126 brouard 6872: fx=func(x);
1.203 brouard 6873: for (k=1; k<=kmax; k=k+10) {
1.126 brouard 6874: for (i=1;i<=npar;i++) p2[i]=x[i];
1.203 brouard 6875: p2[thetai]=x[thetai]+delti[thetai]*k;
6876: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
1.126 brouard 6877: k1=func(p2)-fx;
6878:
1.203 brouard 6879: p2[thetai]=x[thetai]+delti[thetai]*k;
6880: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
1.126 brouard 6881: k2=func(p2)-fx;
6882:
1.203 brouard 6883: p2[thetai]=x[thetai]-delti[thetai]*k;
6884: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
1.126 brouard 6885: k3=func(p2)-fx;
6886:
1.203 brouard 6887: p2[thetai]=x[thetai]-delti[thetai]*k;
6888: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
1.126 brouard 6889: k4=func(p2)-fx;
1.203 brouard 6890: res=(k1-k2-k3+k4)/4.0/delti[thetai]/k/delti[thetaj]/k/2.; /* Because of L not 2*L */
6891: if(k1*k2*k3*k4 <0.){
1.208 brouard 6892: firstime=1;
1.203 brouard 6893: kmax=kmax+10;
1.208 brouard 6894: }
6895: if(kmax >=10 || firstime ==1){
1.354 brouard 6896: /* What are the thetai and thetaj? thetai/ncovmodel thetai=(thetai-thetai%ncovmodel)/ncovmodel +thetai%ncovmodel=(line,pos) */
1.246 brouard 6897: 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);
6898: 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);
1.203 brouard 6899: 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);
6900: 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);
6901: }
6902: #ifdef DEBUGHESSIJ
6903: v1=hess[thetai][thetai];
6904: v2=hess[thetaj][thetaj];
6905: cv12=res;
6906: /* Computing eigen value of Hessian matrix */
6907: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
6908: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
6909: if ((lc2 <0) || (lc1 <0) ){
6910: printf("Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
6911: fprintf(ficlog, "Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
6912: 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);
6913: 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);
6914: }
1.126 brouard 6915: #endif
6916: }
6917: return res;
6918: }
6919:
1.203 brouard 6920: /* Not done yet: Was supposed to fix if not exactly at the maximum */
6921: /* double hessij( double x[], double delti[], int thetai,int thetaj,double (*func)(double []),int npar) */
6922: /* { */
6923: /* int i; */
6924: /* int l=1, lmax=20; */
6925: /* double k1,k2,k3,k4,res,fx; */
6926: /* double p2[MAXPARM+1]; */
6927: /* double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4; */
6928: /* int k=0,kmax=10; */
6929: /* double l1; */
6930:
6931: /* fx=func(x); */
6932: /* for(l=0 ; l <=lmax; l++){ /\* Enlarging the zone around the Maximum *\/ */
6933: /* l1=pow(10,l); */
6934: /* delts=delt; */
6935: /* for(k=1 ; k <kmax; k=k+1){ */
6936: /* delt = delti*(l1*k); */
6937: /* for (i=1;i<=npar;i++) p2[i]=x[i]; */
6938: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
6939: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
6940: /* k1=func(p2)-fx; */
6941:
6942: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
6943: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
6944: /* k2=func(p2)-fx; */
6945:
6946: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
6947: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
6948: /* k3=func(p2)-fx; */
6949:
6950: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
6951: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
6952: /* k4=func(p2)-fx; */
6953: /* res=(k1-k2-k3+k4)/4.0/delti[thetai]*k/delti[thetaj]*k/2.; /\* Because of L not 2*L *\/ */
6954: /* #ifdef DEBUGHESSIJ */
6955: /* 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); */
6956: /* 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); */
6957: /* #endif */
6958: /* if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)){ */
6959: /* k=kmax; */
6960: /* } */
6961: /* else if((k1 >khi/nkhif) || (k2 >khi/nkhif) || (k4 >khi/nkhif) || (k4 >khi/nkhif)){ /\* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. *\/ */
6962: /* k=kmax; l=lmax*10; */
6963: /* } */
6964: /* else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){ */
6965: /* delts=delt; */
6966: /* } */
6967: /* } /\* End loop k *\/ */
6968: /* } */
6969: /* delti[theta]=delts; */
6970: /* return res; */
6971: /* } */
6972:
6973:
1.126 brouard 6974: /************** Inverse of matrix **************/
6975: void ludcmp(double **a, int n, int *indx, double *d)
6976: {
6977: int i,imax,j,k;
6978: double big,dum,sum,temp;
6979: double *vv;
6980:
6981: vv=vector(1,n);
6982: *d=1.0;
6983: for (i=1;i<=n;i++) {
6984: big=0.0;
6985: for (j=1;j<=n;j++)
6986: if ((temp=fabs(a[i][j])) > big) big=temp;
1.256 brouard 6987: if (big == 0.0){
6988: printf(" Singular Hessian matrix at row %d:\n",i);
6989: for (j=1;j<=n;j++) {
6990: printf(" a[%d][%d]=%f,",i,j,a[i][j]);
6991: fprintf(ficlog," a[%d][%d]=%f,",i,j,a[i][j]);
6992: }
6993: fflush(ficlog);
6994: fclose(ficlog);
6995: nrerror("Singular matrix in routine ludcmp");
6996: }
1.126 brouard 6997: vv[i]=1.0/big;
6998: }
6999: for (j=1;j<=n;j++) {
7000: for (i=1;i<j;i++) {
7001: sum=a[i][j];
7002: for (k=1;k<i;k++) sum -= a[i][k]*a[k][j];
7003: a[i][j]=sum;
7004: }
7005: big=0.0;
7006: for (i=j;i<=n;i++) {
7007: sum=a[i][j];
7008: for (k=1;k<j;k++)
7009: sum -= a[i][k]*a[k][j];
7010: a[i][j]=sum;
7011: if ( (dum=vv[i]*fabs(sum)) >= big) {
7012: big=dum;
7013: imax=i;
7014: }
7015: }
7016: if (j != imax) {
7017: for (k=1;k<=n;k++) {
7018: dum=a[imax][k];
7019: a[imax][k]=a[j][k];
7020: a[j][k]=dum;
7021: }
7022: *d = -(*d);
7023: vv[imax]=vv[j];
7024: }
7025: indx[j]=imax;
7026: if (a[j][j] == 0.0) a[j][j]=TINY;
7027: if (j != n) {
7028: dum=1.0/(a[j][j]);
7029: for (i=j+1;i<=n;i++) a[i][j] *= dum;
7030: }
7031: }
7032: free_vector(vv,1,n); /* Doesn't work */
7033: ;
7034: }
7035:
7036: void lubksb(double **a, int n, int *indx, double b[])
7037: {
7038: int i,ii=0,ip,j;
7039: double sum;
7040:
7041: for (i=1;i<=n;i++) {
7042: ip=indx[i];
7043: sum=b[ip];
7044: b[ip]=b[i];
7045: if (ii)
7046: for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j];
7047: else if (sum) ii=i;
7048: b[i]=sum;
7049: }
7050: for (i=n;i>=1;i--) {
7051: sum=b[i];
7052: for (j=i+1;j<=n;j++) sum -= a[i][j]*b[j];
7053: b[i]=sum/a[i][i];
7054: }
7055: }
7056:
7057: void pstamp(FILE *fichier)
7058: {
1.196 brouard 7059: fprintf(fichier,"# %s.%s\n#IMaCh version %s, %s\n#%s\n# %s", optionfilefiname,optionfilext,version,copyright, fullversion, strstart);
1.126 brouard 7060: }
7061:
1.297 brouard 7062: void date2dmy(double date,double *day, double *month, double *year){
7063: double yp=0., yp1=0., yp2=0.;
7064:
7065: yp1=modf(date,&yp);/* extracts integral of date in yp and
7066: fractional in yp1 */
7067: *year=yp;
7068: yp2=modf((yp1*12),&yp);
7069: *month=yp;
7070: yp1=modf((yp2*30.5),&yp);
7071: *day=yp;
7072: if(*day==0) *day=1;
7073: if(*month==0) *month=1;
7074: }
7075:
1.253 brouard 7076:
7077:
1.126 brouard 7078: /************ Frequencies ********************/
1.251 brouard 7079: void freqsummary(char fileres[], double p[], double pstart[], int iagemin, int iagemax, int **s, double **agev, int nlstate, int imx, \
1.226 brouard 7080: int *Tvaraff, int *invalidvarcomb, int **nbcode, int *ncodemax,double **mint,double **anint, char strstart[], \
7081: int firstpass, int lastpass, int stepm, int weightopt, char model[])
1.250 brouard 7082: { /* Some frequencies as well as proposing some starting values */
1.332 brouard 7083: /* Frequencies of any combination of dummy covariate used in the model equation */
1.265 brouard 7084: int i, m, jk, j1, bool, z1,j, nj, nl, k, iv, jj=0, s1=1, s2=1;
1.226 brouard 7085: int iind=0, iage=0;
7086: int mi; /* Effective wave */
7087: int first;
7088: double ***freq; /* Frequencies */
1.268 brouard 7089: 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 */
7090: 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);
1.284 brouard 7091: double *meanq, *stdq, *idq;
1.226 brouard 7092: double **meanqt;
7093: double *pp, **prop, *posprop, *pospropt;
7094: double pos=0., posproptt=0., pospropta=0., k2, dateintsum=0,k2cpt=0;
7095: char fileresp[FILENAMELENGTH], fileresphtm[FILENAMELENGTH], fileresphtmfr[FILENAMELENGTH];
7096: double agebegin, ageend;
7097:
7098: pp=vector(1,nlstate);
1.251 brouard 7099: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.226 brouard 7100: posprop=vector(1,nlstate); /* Counting the number of transition starting from a live state per age */
7101: pospropt=vector(1,nlstate); /* Counting the number of transition starting from a live state */
7102: /* prop=matrix(1,nlstate,iagemin,iagemax+3); */
7103: meanq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
1.284 brouard 7104: stdq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
1.283 brouard 7105: idq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
1.226 brouard 7106: meanqt=matrix(1,lastpass,1,nqtveff);
7107: strcpy(fileresp,"P_");
7108: strcat(fileresp,fileresu);
7109: /*strcat(fileresphtm,fileresu);*/
7110: if((ficresp=fopen(fileresp,"w"))==NULL) {
7111: printf("Problem with prevalence resultfile: %s\n", fileresp);
7112: fprintf(ficlog,"Problem with prevalence resultfile: %s\n", fileresp);
7113: exit(0);
7114: }
1.240 brouard 7115:
1.226 brouard 7116: strcpy(fileresphtm,subdirfext(optionfilefiname,"PHTM_",".htm"));
7117: if((ficresphtm=fopen(fileresphtm,"w"))==NULL) {
7118: printf("Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
7119: fprintf(ficlog,"Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
7120: fflush(ficlog);
7121: exit(70);
7122: }
7123: else{
7124: fprintf(ficresphtm,"<html><head>\n<title>IMaCh PHTM_ %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
1.240 brouard 7125: <hr size=\"2\" color=\"#EC5E5E\"> \n \
1.214 brouard 7126: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.226 brouard 7127: fileresphtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
7128: }
1.319 brouard 7129: 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);
1.240 brouard 7130:
1.226 brouard 7131: strcpy(fileresphtmfr,subdirfext(optionfilefiname,"PHTMFR_",".htm"));
7132: if((ficresphtmfr=fopen(fileresphtmfr,"w"))==NULL) {
7133: printf("Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
7134: fprintf(ficlog,"Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
7135: fflush(ficlog);
7136: exit(70);
1.240 brouard 7137: } else{
1.226 brouard 7138: fprintf(ficresphtmfr,"<html><head>\n<title>IMaCh PHTM_Frequency table %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
1.319 brouard 7139: ,<hr size=\"2\" color=\"#EC5E5E\"> \n \
1.214 brouard 7140: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.226 brouard 7141: fileresphtmfr,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
7142: }
1.319 brouard 7143: 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);
1.240 brouard 7144:
1.253 brouard 7145: y= vector(iagemin-AGEMARGE,iagemax+4+AGEMARGE);
7146: x= vector(iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.251 brouard 7147: freq= ma3x(-5,nlstate+ndeath,-5,nlstate+ndeath,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.226 brouard 7148: j1=0;
1.126 brouard 7149:
1.227 brouard 7150: /* j=ncoveff; /\* Only fixed dummy covariates *\/ */
1.335 brouard 7151: j=cptcoveff; /* Only simple dummy covariates used in the model */
1.330 brouard 7152: /* j=cptcovn; /\* Only dummy covariates of the model *\/ */
1.226 brouard 7153: if (cptcovn<1) {j=1;ncodemax[1]=1;}
1.240 brouard 7154:
7155:
1.226 brouard 7156: /* Detects if a combination j1 is empty: for a multinomial variable like 3 education levels:
7157: reference=low_education V1=0,V2=0
7158: med_educ V1=1 V2=0,
7159: high_educ V1=0 V2=1
1.330 brouard 7160: Then V1=1 and V2=1 is a noisy combination that we want to exclude for the list 2**cptcovn
1.226 brouard 7161: */
1.249 brouard 7162: dateintsum=0;
7163: k2cpt=0;
7164:
1.253 brouard 7165: if(cptcoveff == 0 )
1.265 brouard 7166: nl=1; /* Constant and age model only */
1.253 brouard 7167: else
7168: nl=2;
1.265 brouard 7169:
7170: /* if a constant only model, one pass to compute frequency tables and to write it on ficresp */
7171: /* Loop on nj=1 or 2 if dummy covariates j!=0
1.335 brouard 7172: * Loop on j1(1 to 2**cptcoveff) covariate combination
1.265 brouard 7173: * freq[s1][s2][iage] =0.
7174: * Loop on iind
7175: * ++freq[s1][s2][iage] weighted
7176: * end iind
7177: * if covariate and j!0
7178: * headers Variable on one line
7179: * endif cov j!=0
7180: * header of frequency table by age
7181: * Loop on age
7182: * pp[s1]+=freq[s1][s2][iage] weighted
7183: * pos+=freq[s1][s2][iage] weighted
7184: * Loop on s1 initial state
7185: * fprintf(ficresp
7186: * end s1
7187: * end age
7188: * if j!=0 computes starting values
7189: * end compute starting values
7190: * end j1
7191: * end nl
7192: */
1.253 brouard 7193: for (nj = 1; nj <= nl; nj++){ /* nj= 1 constant model, nl number of loops. */
7194: if(nj==1)
7195: j=0; /* First pass for the constant */
1.265 brouard 7196: else{
1.335 brouard 7197: j=cptcoveff; /* Other passes for the covariate values number of simple covariates in the model V2+V1 =2 (simple dummy fixed or time varying) */
1.265 brouard 7198: }
1.251 brouard 7199: first=1;
1.332 brouard 7200: 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 */
1.251 brouard 7201: posproptt=0.;
1.330 brouard 7202: /*printf("cptcovn=%d Tvaraff=%d", cptcovn,Tvaraff[1]);
1.251 brouard 7203: scanf("%d", i);*/
7204: for (i=-5; i<=nlstate+ndeath; i++)
1.265 brouard 7205: for (s2=-5; s2<=nlstate+ndeath; s2++)
1.251 brouard 7206: for(m=iagemin; m <= iagemax+3; m++)
1.265 brouard 7207: freq[i][s2][m]=0;
1.251 brouard 7208:
7209: for (i=1; i<=nlstate; i++) {
1.240 brouard 7210: for(m=iagemin; m <= iagemax+3; m++)
1.251 brouard 7211: prop[i][m]=0;
7212: posprop[i]=0;
7213: pospropt[i]=0;
7214: }
1.283 brouard 7215: for (z1=1; z1<= nqfveff; z1++) { /* zeroing for each combination j1 as well as for the total */
1.284 brouard 7216: idq[z1]=0.;
7217: meanq[z1]=0.;
7218: stdq[z1]=0.;
1.283 brouard 7219: }
7220: /* for (z1=1; z1<= nqtveff; z1++) { */
1.251 brouard 7221: /* for(m=1;m<=lastpass;m++){ */
1.283 brouard 7222: /* meanqt[m][z1]=0.; */
7223: /* } */
7224: /* } */
1.251 brouard 7225: /* dateintsum=0; */
7226: /* k2cpt=0; */
7227:
1.265 brouard 7228: /* For that combination of covariates j1 (V4=1 V3=0 for example), we count and print the frequencies in one pass */
1.251 brouard 7229: for (iind=1; iind<=imx; iind++) { /* For each individual iind */
7230: bool=1;
7231: if(j !=0){
7232: if(anyvaryingduminmodel==0){ /* If All fixed covariates */
1.335 brouard 7233: if (cptcoveff >0) { /* Filter is here: Must be looked at for model=V1+V2+V3+V4 */
7234: for (z1=1; z1<=cptcoveff; z1++) { /* loops on covariates in the model */
1.251 brouard 7235: /* if(Tvaraff[z1] ==-20){ */
7236: /* /\* sumnew+=cotvar[mw[mi][iind]][z1][iind]; *\/ */
7237: /* }else if(Tvaraff[z1] ==-10){ */
7238: /* /\* sumnew+=coqvar[z1][iind]; *\/ */
1.330 brouard 7239: /* }else */ /* TODO TODO codtabm(j1,z1) or codtabm(j1,Tvaraff[z1]]z1)*/
1.335 brouard 7240: /* 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); */
7241: if(Tvaraff[z1]<1 || Tvaraff[z1]>=NCOVMAX)
1.338 brouard 7242: printf("Error Tvaraff[z1]=%d<1 or >=%d, cptcoveff=%d model=1+age+%s\n",Tvaraff[z1],NCOVMAX, cptcoveff, model);
1.332 brouard 7243: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]){ /* for combination j1 of covariates */
1.265 brouard 7244: /* Tests if the value of the covariate z1 for this individual iind responded to combination j1 (V4=1 V3=0) */
1.251 brouard 7245: bool=0; /* bool should be equal to 1 to be selected, one covariate value failed */
1.332 brouard 7246: /* 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", */
7247: /* bool,i,z1, z1, Tvaraff[z1],i,covar[Tvaraff[z1]][i],j1,z1,codtabm(j1,z1),*/
7248: /* j1,z1,nbcode[Tvaraff[z1]][codtabm(j1,z1)],j1);*/
1.251 brouard 7249: /* For j1=7 in V1+V2+V3+V4 = 0 1 1 0 and codtabm(7,3)=1 and nbcde[3][?]=1*/
7250: } /* Onlyf fixed */
7251: } /* end z1 */
1.335 brouard 7252: } /* cptcoveff > 0 */
1.251 brouard 7253: } /* end any */
7254: }/* end j==0 */
1.265 brouard 7255: if (bool==1){ /* We selected an individual iind satisfying combination j1 (V4=1 V3=0) or all fixed covariates */
1.251 brouard 7256: /* for(m=firstpass; m<=lastpass; m++){ */
1.284 brouard 7257: for(mi=1; mi<wav[iind];mi++){ /* For each wave */
1.251 brouard 7258: m=mw[mi][iind];
7259: if(j!=0){
7260: if(anyvaryingduminmodel==1){ /* Some are varying covariates */
1.335 brouard 7261: for (z1=1; z1<=cptcoveff; z1++) {
1.251 brouard 7262: if( Fixed[Tmodelind[z1]]==1){
1.341 brouard 7263: /* iv= Tvar[Tmodelind[z1]]-ncovcol-nqv; /\* Good *\/ */
7264: iv= Tvar[Tmodelind[z1]]; /* Good *//* because cotvar starts now at first at ncovcol+nqv+ntv */
1.332 brouard 7265: if (cotvar[m][iv][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]) /* iv=1 to ntv, right modality. If covariate's
1.251 brouard 7266: value is -1, we don't select. It differs from the
7267: constant and age model which counts them. */
7268: bool=0; /* not selected */
7269: }else if( Fixed[Tmodelind[z1]]== 0) { /* fixed */
1.334 brouard 7270: /* i1=Tvaraff[z1]; */
7271: /* i2=TnsdVar[i1]; */
7272: /* i3=nbcode[i1][i2]; */
7273: /* i4=covar[i1][iind]; */
7274: /* if(i4 != i3){ */
7275: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]) { /* Bug valgrind */
1.251 brouard 7276: bool=0;
7277: }
7278: }
7279: }
7280: }/* Some are varying covariates, we tried to speed up if all fixed covariates in the model, avoiding waves loop */
7281: } /* end j==0 */
7282: /* bool =0 we keep that guy which corresponds to the combination of dummy values */
1.284 brouard 7283: if(bool==1){ /*Selected */
1.251 brouard 7284: /* dh[m][iind] or dh[mw[mi][iind]][iind] is the delay between two effective (mi) waves m=mw[mi][iind]
7285: and mw[mi+1][iind]. dh depends on stepm. */
7286: agebegin=agev[m][iind]; /* Age at beginning of wave before transition*/
7287: ageend=agev[m][iind]+(dh[m][iind])*stepm/YEARM; /* Age at end of wave and transition */
7288: if(m >=firstpass && m <=lastpass){
7289: k2=anint[m][iind]+(mint[m][iind]/12.);
7290: /*if ((k2>=dateprev1) && (k2<=dateprev2)) {*/
7291: if(agev[m][iind]==0) agev[m][iind]=iagemax+1; /* All ages equal to 0 are in iagemax+1 */
7292: if(agev[m][iind]==1) agev[m][iind]=iagemax+2; /* All ages equal to 1 are in iagemax+2 */
7293: if (s[m][iind]>0 && s[m][iind]<=nlstate) /* If status at wave m is known and a live state */
7294: prop[s[m][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */
7295: if (m<lastpass) {
7296: /* if(s[m][iind]==4 && s[m+1][iind]==4) */
7297: /* 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]); */
7298: if(s[m][iind]==-1)
7299: 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.));
7300: freq[s[m][iind]][s[m+1][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */
1.311 brouard 7301: for (z1=1; z1<= nqfveff; z1++) { /* Quantitative variables, calculating mean on known values only */
7302: if(!isnan(covar[ncovcol+z1][iind])){
1.332 brouard 7303: idq[z1]=idq[z1]+weight[iind];
7304: meanq[z1]+=covar[ncovcol+z1][iind]*weight[iind]; /* Computes mean of quantitative with selected filter */
7305: /* stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]*weight[iind]; *//*error*/
7306: stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]; /* *weight[iind];*/ /* Computes mean of quantitative with selected filter */
1.311 brouard 7307: }
1.284 brouard 7308: }
1.251 brouard 7309: /* if((int)agev[m][iind] == 55) */
7310: /* printf("j=%d, j1=%d Age %d, iind=%d, num=%09ld m=%d\n",j,j1,(int)agev[m][iind],iind, num[iind],m); */
7311: /* freq[s[m][iind]][s[m+1][iind]][(int)((agebegin+ageend)/2.)] += weight[iind]; */
7312: 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 */
1.234 brouard 7313: }
1.251 brouard 7314: } /* end if between passes */
7315: if ((agev[m][iind]>1) && (agev[m][iind]< (iagemax+3)) && (anint[m][iind]!=9999) && (mint[m][iind]!=99) && (j==0)) {
7316: dateintsum=dateintsum+k2; /* on all covariates ?*/
7317: k2cpt++;
7318: /* printf("iind=%ld dateintmean = %lf dateintsum=%lf k2cpt=%lf k2=%lf\n",iind, dateintsum/k2cpt, dateintsum,k2cpt, k2); */
1.234 brouard 7319: }
1.251 brouard 7320: }else{
7321: bool=1;
7322: }/* end bool 2 */
7323: } /* end m */
1.284 brouard 7324: /* for (z1=1; z1<= nqfveff; z1++) { /\* Quantitative variables, calculating mean *\/ */
7325: /* idq[z1]=idq[z1]+weight[iind]; */
7326: /* meanq[z1]+=covar[ncovcol+z1][iind]*weight[iind]; /\* Computes mean of quantitative with selected filter *\/ */
7327: /* stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]*weight[iind]; /\* *weight[iind];*\/ /\* Computes mean of quantitative with selected filter *\/ */
7328: /* } */
1.251 brouard 7329: } /* end bool */
7330: } /* end iind = 1 to imx */
1.319 brouard 7331: /* prop[s][age] is fed for any initial and valid live state as well as
1.251 brouard 7332: freq[s1][s2][age] at single age of beginning the transition, for a combination j1 */
7333:
7334:
7335: /* fprintf(ficresp, "#Count between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);*/
1.335 brouard 7336: if(cptcoveff==0 && nj==1) /* no covariate and first pass */
1.265 brouard 7337: pstamp(ficresp);
1.335 brouard 7338: if (cptcoveff>0 && j!=0){
1.265 brouard 7339: pstamp(ficresp);
1.251 brouard 7340: printf( "\n#********** Variable ");
7341: fprintf(ficresp, "\n#********** Variable ");
7342: fprintf(ficresphtm, "\n<br/><br/><h3>********** Variable ");
7343: fprintf(ficresphtmfr, "\n<br/><br/><h3>********** Variable ");
7344: fprintf(ficlog, "\n#********** Variable ");
1.340 brouard 7345: for (z1=1; z1<=cptcoveff; z1++){
1.251 brouard 7346: if(!FixedV[Tvaraff[z1]]){
1.330 brouard 7347: printf( "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7348: fprintf(ficresp, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7349: fprintf(ficresphtm, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7350: fprintf(ficresphtmfr, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7351: fprintf(ficlog, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
1.250 brouard 7352: }else{
1.330 brouard 7353: printf( "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7354: fprintf(ficresp, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7355: fprintf(ficresphtm, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7356: fprintf(ficresphtmfr, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7357: fprintf(ficlog, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
1.251 brouard 7358: }
7359: }
7360: printf( "**********\n#");
7361: fprintf(ficresp, "**********\n#");
7362: fprintf(ficresphtm, "**********</h3>\n");
7363: fprintf(ficresphtmfr, "**********</h3>\n");
7364: fprintf(ficlog, "**********\n");
7365: }
1.284 brouard 7366: /*
7367: Printing means of quantitative variables if any
7368: */
7369: for (z1=1; z1<= nqfveff; z1++) {
1.311 brouard 7370: fprintf(ficlog,"Mean of fixed quantitative variable V%d on %.3g (weighted) individuals sum=%f", ncovcol+z1, idq[z1], meanq[z1]);
1.312 brouard 7371: fprintf(ficlog,", mean=%.3g\n",meanq[z1]/idq[z1]);
1.284 brouard 7372: if(weightopt==1){
7373: printf(" Weighted mean and standard deviation of");
7374: fprintf(ficlog," Weighted mean and standard deviation of");
7375: fprintf(ficresphtmfr," Weighted mean and standard deviation of");
7376: }
1.311 brouard 7377: /* mu = \frac{w x}{\sum w}
7378: var = \frac{\sum w (x-mu)^2}{\sum w} = \frac{w x^2}{\sum w} - mu^2
7379: */
7380: 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]));
7381: 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]));
7382: 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]));
1.284 brouard 7383: }
7384: /* for (z1=1; z1<= nqtveff; z1++) { */
7385: /* for(m=1;m<=lastpass;m++){ */
7386: /* fprintf(ficresphtmfr,"V quantitative id %d, pass id=%d, mean=%f<p>\n", z1, m, meanqt[m][z1]); */
7387: /* } */
7388: /* } */
1.283 brouard 7389:
1.251 brouard 7390: fprintf(ficresphtm,"<table style=\"text-align:center; border: 1px solid\">");
1.335 brouard 7391: if((cptcoveff==0 && nj==1)|| nj==2 ) /* no covariate and first pass */
1.265 brouard 7392: fprintf(ficresp, " Age");
1.335 brouard 7393: if(nj==2) for (z1=1; z1<=cptcoveff; z1++) {
7394: 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]]);
7395: fprintf(ficresp, " V%d=%d",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7396: }
1.251 brouard 7397: for(i=1; i<=nlstate;i++) {
1.335 brouard 7398: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp," Prev(%d) N(%d) N ",i,i);
1.251 brouard 7399: fprintf(ficresphtm, "<th>Age</th><th>Prev(%d)</th><th>N(%d)</th><th>N</th>",i,i);
7400: }
1.335 brouard 7401: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp, "\n");
1.251 brouard 7402: fprintf(ficresphtm, "\n");
7403:
7404: /* Header of frequency table by age */
7405: fprintf(ficresphtmfr,"<table style=\"text-align:center; border: 1px solid\">");
7406: fprintf(ficresphtmfr,"<th>Age</th> ");
1.265 brouard 7407: for(s2=-1; s2 <=nlstate+ndeath; s2++){
1.251 brouard 7408: for(m=-1; m <=nlstate+ndeath; m++){
1.265 brouard 7409: if(s2!=0 && m!=0)
7410: fprintf(ficresphtmfr,"<th>%d%d</th> ",s2,m);
1.240 brouard 7411: }
1.226 brouard 7412: }
1.251 brouard 7413: fprintf(ficresphtmfr, "\n");
7414:
7415: /* For each age */
7416: for(iage=iagemin; iage <= iagemax+3; iage++){
7417: fprintf(ficresphtm,"<tr>");
7418: if(iage==iagemax+1){
7419: fprintf(ficlog,"1");
7420: fprintf(ficresphtmfr,"<tr><th>0</th> ");
7421: }else if(iage==iagemax+2){
7422: fprintf(ficlog,"0");
7423: fprintf(ficresphtmfr,"<tr><th>Unknown</th> ");
7424: }else if(iage==iagemax+3){
7425: fprintf(ficlog,"Total");
7426: fprintf(ficresphtmfr,"<tr><th>Total</th> ");
7427: }else{
1.240 brouard 7428: if(first==1){
1.251 brouard 7429: first=0;
7430: printf("See log file for details...\n");
7431: }
7432: fprintf(ficresphtmfr,"<tr><th>%d</th> ",iage);
7433: fprintf(ficlog,"Age %d", iage);
7434: }
1.265 brouard 7435: for(s1=1; s1 <=nlstate ; s1++){
7436: for(m=-1, pp[s1]=0; m <=nlstate+ndeath ; m++)
7437: pp[s1] += freq[s1][m][iage];
1.251 brouard 7438: }
1.265 brouard 7439: for(s1=1; s1 <=nlstate ; s1++){
1.251 brouard 7440: for(m=-1, pos=0; m <=0 ; m++)
1.265 brouard 7441: pos += freq[s1][m][iage];
7442: if(pp[s1]>=1.e-10){
1.251 brouard 7443: if(first==1){
1.265 brouard 7444: printf(" %d.=%.0f loss[%d]=%.1f%%",s1,pp[s1],s1,100*pos/pp[s1]);
1.251 brouard 7445: }
1.265 brouard 7446: fprintf(ficlog," %d.=%.0f loss[%d]=%.1f%%",s1,pp[s1],s1,100*pos/pp[s1]);
1.251 brouard 7447: }else{
7448: if(first==1)
1.265 brouard 7449: printf(" %d.=%.0f loss[%d]=NaNQ%%",s1,pp[s1],s1);
7450: fprintf(ficlog," %d.=%.0f loss[%d]=NaNQ%%",s1,pp[s1],s1);
1.240 brouard 7451: }
7452: }
7453:
1.265 brouard 7454: for(s1=1; s1 <=nlstate ; s1++){
7455: /* posprop[s1]=0; */
7456: for(m=0, pp[s1]=0; m <=nlstate+ndeath; m++)/* Summing on all ages */
7457: pp[s1] += freq[s1][m][iage];
7458: } /* pp[s1] is the total number of transitions starting from state s1 and any ending status until this age */
7459:
7460: for(s1=1,pos=0, pospropta=0.; s1 <=nlstate ; s1++){
7461: pos += pp[s1]; /* pos is the total number of transitions until this age */
7462: posprop[s1] += prop[s1][iage]; /* prop is the number of transitions from a live state
7463: from s1 at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
7464: pospropta += prop[s1][iage]; /* prop is the number of transitions from a live state
7465: from s1 at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
7466: }
7467:
7468: /* Writing ficresp */
1.335 brouard 7469: if(cptcoveff==0 && nj==1){ /* no covariate and first pass */
1.265 brouard 7470: if( iage <= iagemax){
7471: fprintf(ficresp," %d",iage);
7472: }
7473: }else if( nj==2){
7474: if( iage <= iagemax){
7475: fprintf(ficresp," %d",iage);
1.335 brouard 7476: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresp, " %d %d",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
1.265 brouard 7477: }
1.240 brouard 7478: }
1.265 brouard 7479: for(s1=1; s1 <=nlstate ; s1++){
1.240 brouard 7480: if(pos>=1.e-5){
1.251 brouard 7481: if(first==1)
1.265 brouard 7482: printf(" %d.=%.0f prev[%d]=%.1f%%",s1,pp[s1],s1,100*pp[s1]/pos);
7483: fprintf(ficlog," %d.=%.0f prev[%d]=%.1f%%",s1,pp[s1],s1,100*pp[s1]/pos);
1.251 brouard 7484: }else{
7485: if(first==1)
1.265 brouard 7486: printf(" %d.=%.0f prev[%d]=NaNQ%%",s1,pp[s1],s1);
7487: fprintf(ficlog," %d.=%.0f prev[%d]=NaNQ%%",s1,pp[s1],s1);
1.251 brouard 7488: }
7489: if( iage <= iagemax){
7490: if(pos>=1.e-5){
1.335 brouard 7491: if(cptcoveff==0 && nj==1){ /* no covariate and first pass */
1.265 brouard 7492: fprintf(ficresp," %.5f %.0f %.0f",prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
7493: }else if( nj==2){
7494: fprintf(ficresp," %.5f %.0f %.0f",prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
7495: }
7496: fprintf(ficresphtm,"<th>%d</th><td>%.5f</td><td>%.0f</td><td>%.0f</td>",iage,prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
7497: /*probs[iage][s1][j1]= pp[s1]/pos;*/
7498: /*printf("\niage=%d s1=%d j1=%d %.5f %.0f %.0f %f",iage,s1,j1,pp[s1]/pos, pp[s1],pos,probs[iage][s1][j1]);*/
7499: } else{
1.335 brouard 7500: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp," NaNq %.0f %.0f",prop[s1][iage],pospropta);
1.265 brouard 7501: fprintf(ficresphtm,"<th>%d</th><td>NaNq</td><td>%.0f</td><td>%.0f</td>",iage, prop[s1][iage],pospropta);
1.251 brouard 7502: }
1.240 brouard 7503: }
1.265 brouard 7504: pospropt[s1] +=posprop[s1];
7505: } /* end loop s1 */
1.251 brouard 7506: /* pospropt=0.; */
1.265 brouard 7507: for(s1=-1; s1 <=nlstate+ndeath; s1++){
1.251 brouard 7508: for(m=-1; m <=nlstate+ndeath; m++){
1.265 brouard 7509: if(freq[s1][m][iage] !=0 ) { /* minimizing output */
1.251 brouard 7510: if(first==1){
1.265 brouard 7511: printf(" %d%d=%.0f",s1,m,freq[s1][m][iage]);
1.251 brouard 7512: }
1.265 brouard 7513: /* printf(" %d%d=%.0f",s1,m,freq[s1][m][iage]); */
7514: fprintf(ficlog," %d%d=%.0f",s1,m,freq[s1][m][iage]);
1.251 brouard 7515: }
1.265 brouard 7516: if(s1!=0 && m!=0)
7517: fprintf(ficresphtmfr,"<td>%.0f</td> ",freq[s1][m][iage]);
1.240 brouard 7518: }
1.265 brouard 7519: } /* end loop s1 */
1.251 brouard 7520: posproptt=0.;
1.265 brouard 7521: for(s1=1; s1 <=nlstate; s1++){
7522: posproptt += pospropt[s1];
1.251 brouard 7523: }
7524: fprintf(ficresphtmfr,"</tr>\n ");
1.265 brouard 7525: fprintf(ficresphtm,"</tr>\n");
1.335 brouard 7526: if((cptcoveff==0 && nj==1)|| nj==2 ) {
1.265 brouard 7527: if(iage <= iagemax)
7528: fprintf(ficresp,"\n");
1.240 brouard 7529: }
1.251 brouard 7530: if(first==1)
7531: printf("Others in log...\n");
7532: fprintf(ficlog,"\n");
7533: } /* end loop age iage */
1.265 brouard 7534:
1.251 brouard 7535: fprintf(ficresphtm,"<tr><th>Tot</th>");
1.265 brouard 7536: for(s1=1; s1 <=nlstate ; s1++){
1.251 brouard 7537: if(posproptt < 1.e-5){
1.265 brouard 7538: fprintf(ficresphtm,"<td>Nanq</td><td>%.0f</td><td>%.0f</td>",pospropt[s1],posproptt);
1.251 brouard 7539: }else{
1.265 brouard 7540: fprintf(ficresphtm,"<td>%.5f</td><td>%.0f</td><td>%.0f</td>",pospropt[s1]/posproptt,pospropt[s1],posproptt);
1.240 brouard 7541: }
1.226 brouard 7542: }
1.251 brouard 7543: fprintf(ficresphtm,"</tr>\n");
7544: fprintf(ficresphtm,"</table>\n");
7545: fprintf(ficresphtmfr,"</table>\n");
1.226 brouard 7546: if(posproptt < 1.e-5){
1.251 brouard 7547: fprintf(ficresphtm,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
7548: fprintf(ficresphtmfr,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
1.260 brouard 7549: fprintf(ficlog,"# This combination (%d) is not valid and no result will be produced\n",j1);
7550: printf("# This combination (%d) is not valid and no result will be produced\n",j1);
1.251 brouard 7551: invalidvarcomb[j1]=1;
1.226 brouard 7552: }else{
1.338 brouard 7553: fprintf(ficresphtm,"\n <p> This combination (%d) is valid and result will be produced (or no resultline).</p>",j1);
1.251 brouard 7554: invalidvarcomb[j1]=0;
1.226 brouard 7555: }
1.251 brouard 7556: fprintf(ficresphtmfr,"</table>\n");
7557: fprintf(ficlog,"\n");
7558: if(j!=0){
7559: printf("#Freqsummary: Starting values for combination j1=%d:\n", j1);
1.265 brouard 7560: for(i=1,s1=1; i <=nlstate; i++){
1.251 brouard 7561: for(k=1; k <=(nlstate+ndeath); k++){
7562: if (k != i) {
1.265 brouard 7563: for(jj=1; jj <=ncovmodel; jj++){ /* For counting s1 */
1.253 brouard 7564: if(jj==1){ /* Constant case (in fact cste + age) */
1.251 brouard 7565: if(j1==1){ /* All dummy covariates to zero */
7566: freq[i][k][iagemax+4]=freq[i][k][iagemax+3]; /* Stores case 0 0 0 */
7567: freq[i][i][iagemax+4]=freq[i][i][iagemax+3]; /* Stores case 0 0 0 */
1.252 brouard 7568: printf("%d%d ",i,k);
7569: fprintf(ficlog,"%d%d ",i,k);
1.265 brouard 7570: 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]));
7571: 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]));
7572: pstart[s1]= log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]);
1.251 brouard 7573: }
1.253 brouard 7574: }else if((j1==1) && (jj==2 || nagesqr==1)){ /* age or age*age parameter without covariate V4*age (to be done later) */
7575: for(iage=iagemin; iage <= iagemax+3; iage++){
7576: x[iage]= (double)iage;
7577: y[iage]= log(freq[i][k][iage]/freq[i][i][iage]);
1.265 brouard 7578: /* printf("i=%d, k=%d, s1=%d, j1=%d, jj=%d, y[%d]=%f\n",i,k,s1,j1,jj, iage, y[iage]); */
1.253 brouard 7579: }
1.268 brouard 7580: /* Some are not finite, but linreg will ignore these ages */
7581: no=0;
1.253 brouard 7582: linreg(iagemin,iagemax,&no,x,y,&a,&b,&r, &sa, &sb ); /* y= a+b*x with standard errors */
1.265 brouard 7583: pstart[s1]=b;
7584: pstart[s1-1]=a;
1.252 brouard 7585: }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 */
7586: 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]);
7587: 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]);
1.265 brouard 7588: pstart[s1]= log((freq[i][k][iagemax+3]/freq[i][i][iagemax+3])/(freq[i][k][iagemax+4]/freq[i][i][iagemax+4]));
1.252 brouard 7589: printf("%d%d ",i,k);
7590: fprintf(ficlog,"%d%d ",i,k);
1.265 brouard 7591: 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]));
1.251 brouard 7592: }else{ /* Other cases, like quantitative fixed or varying covariates */
7593: ;
7594: }
7595: /* printf("%12.7f )", param[i][jj][k]); */
7596: /* fprintf(ficlog,"%12.7f )", param[i][jj][k]); */
1.265 brouard 7597: s1++;
1.251 brouard 7598: } /* end jj */
7599: } /* end k!= i */
7600: } /* end k */
1.265 brouard 7601: } /* end i, s1 */
1.251 brouard 7602: } /* end j !=0 */
7603: } /* end selected combination of covariate j1 */
7604: if(j==0){ /* We can estimate starting values from the occurences in each case */
7605: printf("#Freqsummary: Starting values for the constants:\n");
7606: fprintf(ficlog,"\n");
1.265 brouard 7607: for(i=1,s1=1; i <=nlstate; i++){
1.251 brouard 7608: for(k=1; k <=(nlstate+ndeath); k++){
7609: if (k != i) {
7610: printf("%d%d ",i,k);
7611: fprintf(ficlog,"%d%d ",i,k);
7612: for(jj=1; jj <=ncovmodel; jj++){
1.265 brouard 7613: pstart[s1]=p[s1]; /* Setting pstart to p values by default */
1.253 brouard 7614: if(jj==1){ /* Age has to be done */
1.265 brouard 7615: pstart[s1]= log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]);
7616: 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]));
7617: 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]));
1.251 brouard 7618: }
7619: /* printf("%12.7f )", param[i][jj][k]); */
7620: /* fprintf(ficlog,"%12.7f )", param[i][jj][k]); */
1.265 brouard 7621: s1++;
1.250 brouard 7622: }
1.251 brouard 7623: printf("\n");
7624: fprintf(ficlog,"\n");
1.250 brouard 7625: }
7626: }
1.284 brouard 7627: } /* end of state i */
1.251 brouard 7628: printf("#Freqsummary\n");
7629: fprintf(ficlog,"\n");
1.265 brouard 7630: for(s1=-1; s1 <=nlstate+ndeath; s1++){
7631: for(s2=-1; s2 <=nlstate+ndeath; s2++){
7632: /* param[i]|j][k]= freq[s1][s2][iagemax+3] */
7633: printf(" %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]);
7634: fprintf(ficlog," %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]);
7635: /* if(freq[s1][s2][iage] !=0 ) { /\* minimizing output *\/ */
7636: /* printf(" %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]); */
7637: /* fprintf(ficlog," %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]); */
1.251 brouard 7638: /* } */
7639: }
1.265 brouard 7640: } /* end loop s1 */
1.251 brouard 7641:
7642: printf("\n");
7643: fprintf(ficlog,"\n");
7644: } /* end j=0 */
1.249 brouard 7645: } /* end j */
1.252 brouard 7646:
1.253 brouard 7647: if(mle == -2){ /* We want to use these values as starting values */
1.252 brouard 7648: for(i=1, jk=1; i <=nlstate; i++){
7649: for(j=1; j <=nlstate+ndeath; j++){
7650: if(j!=i){
7651: /*ca[0]= k+'a'-1;ca[1]='\0';*/
7652: printf("%1d%1d",i,j);
7653: fprintf(ficparo,"%1d%1d",i,j);
7654: for(k=1; k<=ncovmodel;k++){
7655: /* printf(" %lf",param[i][j][k]); */
7656: /* fprintf(ficparo," %lf",param[i][j][k]); */
7657: p[jk]=pstart[jk];
7658: printf(" %f ",pstart[jk]);
7659: fprintf(ficparo," %f ",pstart[jk]);
7660: jk++;
7661: }
7662: printf("\n");
7663: fprintf(ficparo,"\n");
7664: }
7665: }
7666: }
7667: } /* end mle=-2 */
1.226 brouard 7668: dateintmean=dateintsum/k2cpt;
1.296 brouard 7669: date2dmy(dateintmean,&jintmean,&mintmean,&aintmean);
1.240 brouard 7670:
1.226 brouard 7671: fclose(ficresp);
7672: fclose(ficresphtm);
7673: fclose(ficresphtmfr);
1.283 brouard 7674: free_vector(idq,1,nqfveff);
1.226 brouard 7675: free_vector(meanq,1,nqfveff);
1.284 brouard 7676: free_vector(stdq,1,nqfveff);
1.226 brouard 7677: free_matrix(meanqt,1,lastpass,1,nqtveff);
1.253 brouard 7678: free_vector(x, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
7679: free_vector(y, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
1.251 brouard 7680: free_ma3x(freq,-5,nlstate+ndeath,-5,nlstate+ndeath, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
1.226 brouard 7681: free_vector(pospropt,1,nlstate);
7682: free_vector(posprop,1,nlstate);
1.251 brouard 7683: free_matrix(prop,1,nlstate,iagemin-AGEMARGE, iagemax+4+AGEMARGE);
1.226 brouard 7684: free_vector(pp,1,nlstate);
7685: /* End of freqsummary */
7686: }
1.126 brouard 7687:
1.268 brouard 7688: /* Simple linear regression */
7689: int linreg(int ifi, int ila, int *no, const double x[], const double y[], double* a, double* b, double* r, double* sa, double * sb) {
7690:
7691: /* y=a+bx regression */
7692: double sumx = 0.0; /* sum of x */
7693: double sumx2 = 0.0; /* sum of x**2 */
7694: double sumxy = 0.0; /* sum of x * y */
7695: double sumy = 0.0; /* sum of y */
7696: double sumy2 = 0.0; /* sum of y**2 */
7697: double sume2 = 0.0; /* sum of square or residuals */
7698: double yhat;
7699:
7700: double denom=0;
7701: int i;
7702: int ne=*no;
7703:
7704: for ( i=ifi, ne=0;i<=ila;i++) {
7705: if(!isfinite(x[i]) || !isfinite(y[i])){
7706: /* printf(" x[%d]=%f, y[%d]=%f\n",i,x[i],i,y[i]); */
7707: continue;
7708: }
7709: ne=ne+1;
7710: sumx += x[i];
7711: sumx2 += x[i]*x[i];
7712: sumxy += x[i] * y[i];
7713: sumy += y[i];
7714: sumy2 += y[i]*y[i];
7715: denom = (ne * sumx2 - sumx*sumx);
7716: /* 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); */
7717: }
7718:
7719: denom = (ne * sumx2 - sumx*sumx);
7720: if (denom == 0) {
7721: // vertical, slope m is infinity
7722: *b = INFINITY;
7723: *a = 0;
7724: if (r) *r = 0;
7725: return 1;
7726: }
7727:
7728: *b = (ne * sumxy - sumx * sumy) / denom;
7729: *a = (sumy * sumx2 - sumx * sumxy) / denom;
7730: if (r!=NULL) {
7731: *r = (sumxy - sumx * sumy / ne) / /* compute correlation coeff */
7732: sqrt((sumx2 - sumx*sumx/ne) *
7733: (sumy2 - sumy*sumy/ne));
7734: }
7735: *no=ne;
7736: for ( i=ifi, ne=0;i<=ila;i++) {
7737: if(!isfinite(x[i]) || !isfinite(y[i])){
7738: /* printf(" x[%d]=%f, y[%d]=%f\n",i,x[i],i,y[i]); */
7739: continue;
7740: }
7741: ne=ne+1;
7742: yhat = y[i] - *a -*b* x[i];
7743: sume2 += yhat * yhat ;
7744:
7745: denom = (ne * sumx2 - sumx*sumx);
7746: /* 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); */
7747: }
7748: *sb = sqrt(sume2/(double)(ne-2)/(sumx2 - sumx * sumx /(double)ne));
7749: *sa= *sb * sqrt(sumx2/ne);
7750:
7751: return 0;
7752: }
7753:
1.126 brouard 7754: /************ Prevalence ********************/
1.227 brouard 7755: 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)
7756: {
7757: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
7758: in each health status at the date of interview (if between dateprev1 and dateprev2).
7759: We still use firstpass and lastpass as another selection.
7760: */
1.126 brouard 7761:
1.227 brouard 7762: int i, m, jk, j1, bool, z1,j, iv;
7763: int mi; /* Effective wave */
7764: int iage;
1.359 brouard 7765: double agebegin; /*, ageend;*/
1.227 brouard 7766:
7767: double **prop;
7768: double posprop;
7769: double y2; /* in fractional years */
7770: int iagemin, iagemax;
7771: int first; /** to stop verbosity which is redirected to log file */
7772:
7773: iagemin= (int) agemin;
7774: iagemax= (int) agemax;
7775: /*pp=vector(1,nlstate);*/
1.251 brouard 7776: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.227 brouard 7777: /* freq=ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,iagemin,iagemax+3);*/
7778: j1=0;
1.222 brouard 7779:
1.227 brouard 7780: /*j=cptcoveff;*/
7781: if (cptcovn<1) {j=1;ncodemax[1]=1;}
1.222 brouard 7782:
1.288 brouard 7783: first=0;
1.335 brouard 7784: for(j1=1; j1<= (int) pow(2,cptcoveff);j1++){ /* For each combination of simple dummy covariates */
1.227 brouard 7785: for (i=1; i<=nlstate; i++)
1.251 brouard 7786: for(iage=iagemin-AGEMARGE; iage <= iagemax+4+AGEMARGE; iage++)
1.227 brouard 7787: prop[i][iage]=0.0;
7788: printf("Prevalence combination of varying and fixed dummies %d\n",j1);
7789: /* fprintf(ficlog," V%d=%d ",Tvaraff[j1],nbcode[Tvaraff[j1]][codtabm(k,j1)]); */
7790: fprintf(ficlog,"Prevalence combination of varying and fixed dummies %d\n",j1);
7791:
7792: for (i=1; i<=imx; i++) { /* Each individual */
7793: bool=1;
7794: /* for(m=firstpass; m<=lastpass; m++){/\* Other selection (we can limit to certain interviews*\/ */
7795: for(mi=1; mi<wav[i];mi++){ /* For this wave too look where individual can be counted V4=0 V3=0 */
7796: m=mw[mi][i];
7797: /* Tmodelind[z1]=k is the position of the varying covariate in the model, but which # within 1 to ntv? */
7798: /* Tvar[Tmodelind[z1]] is the n of Vn; n-ncovcol-nqv is the first time varying covariate or iv */
7799: for (z1=1; z1<=cptcoveff; z1++){
7800: if( Fixed[Tmodelind[z1]]==1){
1.341 brouard 7801: iv= Tvar[Tmodelind[z1]];/* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) */
1.332 brouard 7802: if (cotvar[m][iv][i]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]) /* iv=1 to ntv, right modality */
1.227 brouard 7803: bool=0;
7804: }else if( Fixed[Tmodelind[z1]]== 0) /* fixed */
1.332 brouard 7805: if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]) {
1.227 brouard 7806: bool=0;
7807: }
7808: }
7809: if(bool==1){ /* Otherwise we skip that wave/person */
7810: agebegin=agev[m][i]; /* Age at beginning of wave before transition*/
7811: /* ageend=agev[m][i]+(dh[m][i])*stepm/YEARM; /\* Age at end of wave and transition *\/ */
7812: if(m >=firstpass && m <=lastpass){
7813: y2=anint[m][i]+(mint[m][i]/12.); /* Fractional date in year */
7814: if ((y2>=dateprev1) && (y2<=dateprev2)) { /* Here is the main selection (fractional years) */
7815: if(agev[m][i]==0) agev[m][i]=iagemax+1;
7816: if(agev[m][i]==1) agev[m][i]=iagemax+2;
1.251 brouard 7817: if((int)agev[m][i] <iagemin-AGEMARGE || (int)agev[m][i] >iagemax+4+AGEMARGE){
1.227 brouard 7818: 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);
7819: exit(1);
7820: }
7821: if (s[m][i]>0 && s[m][i]<=nlstate) {
7822: /*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]]);*/
7823: prop[s[m][i]][(int)agev[m][i]] += weight[i];/* At age of beginning of transition, where status is known */
7824: prop[s[m][i]][iagemax+3] += weight[i];
7825: } /* end valid statuses */
7826: } /* end selection of dates */
7827: } /* end selection of waves */
7828: } /* end bool */
7829: } /* end wave */
7830: } /* end individual */
7831: for(i=iagemin; i <= iagemax+3; i++){
7832: for(jk=1,posprop=0; jk <=nlstate ; jk++) {
7833: posprop += prop[jk][i];
7834: }
7835:
7836: for(jk=1; jk <=nlstate ; jk++){
7837: if( i <= iagemax){
7838: if(posprop>=1.e-5){
7839: probs[i][jk][j1]= prop[jk][i]/posprop;
7840: } else{
1.288 brouard 7841: if(!first){
7842: first=1;
1.266 brouard 7843: 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]);
7844: }else{
1.288 brouard 7845: 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]);
1.227 brouard 7846: }
7847: }
7848: }
7849: }/* end jk */
7850: }/* end i */
1.222 brouard 7851: /*} *//* end i1 */
1.227 brouard 7852: } /* end j1 */
1.222 brouard 7853:
1.227 brouard 7854: /* free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath, iagemin, iagemax+3);*/
7855: /*free_vector(pp,1,nlstate);*/
1.251 brouard 7856: free_matrix(prop,1,nlstate, iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.227 brouard 7857: } /* End of prevalence */
1.126 brouard 7858:
7859: /************* Waves Concatenation ***************/
7860:
7861: 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)
7862: {
1.298 brouard 7863: /* Concatenates waves: wav[i] is the number of effective (useful waves in the sense that a non interview is useless) of individual i.
1.126 brouard 7864: Death is a valid wave (if date is known).
7865: mw[mi][i] is the mi (mi=1 to wav[i]) effective wave of individual i
7866: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
1.298 brouard 7867: and mw[mi+1][i]. dh depends on stepm. s[m][i] exists for any wave from firstpass to lastpass
1.227 brouard 7868: */
1.126 brouard 7869:
1.224 brouard 7870: int i=0, mi=0, m=0, mli=0;
1.126 brouard 7871: /* int j, k=0,jk, ju, jl,jmin=1e+5, jmax=-1;
7872: double sum=0., jmean=0.;*/
1.224 brouard 7873: int first=0, firstwo=0, firsthree=0, firstfour=0, firstfiv=0;
1.126 brouard 7874: int j, k=0,jk, ju, jl;
7875: double sum=0.;
7876: first=0;
1.214 brouard 7877: firstwo=0;
1.217 brouard 7878: firsthree=0;
1.218 brouard 7879: firstfour=0;
1.164 brouard 7880: jmin=100000;
1.126 brouard 7881: jmax=-1;
7882: jmean=0.;
1.224 brouard 7883:
7884: /* Treating live states */
1.214 brouard 7885: for(i=1; i<=imx; i++){ /* For simple cases and if state is death */
1.224 brouard 7886: mi=0; /* First valid wave */
1.227 brouard 7887: mli=0; /* Last valid wave */
1.309 brouard 7888: m=firstpass; /* Loop on waves */
7889: while(s[m][i] <= nlstate){ /* a live state or unknown state */
1.227 brouard 7890: 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 */
7891: mli=m-1;/* mw[++mi][i]=m-1; */
7892: }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 */
1.309 brouard 7893: mw[++mi][i]=m; /* Valid wave: incrementing mi and updating mi; mw[mi] is the wave number of mi_th valid transition */
1.227 brouard 7894: mli=m;
1.224 brouard 7895: } /* else might be a useless wave -1 and mi is not incremented and mw[mi] not updated */
7896: if(m < lastpass){ /* m < lastpass, standard case */
1.227 brouard 7897: m++; /* mi gives the "effective" current wave, m the current wave, go to next wave by incrementing m */
1.216 brouard 7898: }
1.309 brouard 7899: else{ /* m = lastpass, eventual special issue with warning */
1.224 brouard 7900: #ifdef UNKNOWNSTATUSNOTCONTRIBUTING
1.227 brouard 7901: break;
1.224 brouard 7902: #else
1.317 brouard 7903: 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 */
1.227 brouard 7904: if(firsthree == 0){
1.302 brouard 7905: 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);
1.227 brouard 7906: firsthree=1;
1.317 brouard 7907: }else if(firsthree >=1 && firsthree < 10){
7908: 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);
7909: firsthree++;
7910: }else if(firsthree == 10){
7911: printf("Information, too many Information flags: no more reported to log either\n");
7912: fprintf(ficlog,"Information, too many Information flags: no more reported to log either\n");
7913: firsthree++;
7914: }else{
7915: firsthree++;
1.227 brouard 7916: }
1.309 brouard 7917: mw[++mi][i]=m; /* Valid transition with unknown status */
1.227 brouard 7918: mli=m;
7919: }
7920: if(s[m][i]==-2){ /* Vital status is really unknown */
7921: nbwarn++;
1.309 brouard 7922: if((int)anint[m][i] == 9999){ /* Has the vital status really been verified?not a transition */
1.227 brouard 7923: 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);
7924: 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);
7925: }
7926: break;
7927: }
7928: break;
1.224 brouard 7929: #endif
1.227 brouard 7930: }/* End m >= lastpass */
1.126 brouard 7931: }/* end while */
1.224 brouard 7932:
1.227 brouard 7933: /* mi is the last effective wave, m is lastpass, mw[j][i] gives the # of j-th effective wave for individual i */
1.216 brouard 7934: /* After last pass */
1.224 brouard 7935: /* Treating death states */
1.214 brouard 7936: if (s[m][i] > nlstate){ /* In a death state */
1.227 brouard 7937: /* if( mint[m][i]==mdc[m][i] && anint[m][i]==andc[m][i]){ /\* same date of death and date of interview *\/ */
7938: /* } */
1.126 brouard 7939: mi++; /* Death is another wave */
7940: /* if(mi==0) never been interviewed correctly before death */
1.227 brouard 7941: /* Only death is a correct wave */
1.126 brouard 7942: mw[mi][i]=m;
1.257 brouard 7943: } /* else not in a death state */
1.224 brouard 7944: #ifndef DISPATCHINGKNOWNDEATHAFTERLASTWAVE
1.257 brouard 7945: else if ((int) andc[i] != 9999) { /* Date of death is known */
1.218 brouard 7946: if ((int)anint[m][i]!= 9999) { /* date of last interview is known */
1.309 brouard 7947: 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 */
1.227 brouard 7948: nbwarn++;
7949: if(firstfiv==0){
1.309 brouard 7950: 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 );
1.227 brouard 7951: firstfiv=1;
7952: }else{
1.309 brouard 7953: 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 );
1.227 brouard 7954: }
1.309 brouard 7955: s[m][i]=nlstate+1; /* Fixing the status as death. Be careful if multiple death states */
7956: }else{ /* Month of Death occured afer last wave month, potential bias */
1.227 brouard 7957: nberr++;
7958: if(firstwo==0){
1.309 brouard 7959: 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 );
1.227 brouard 7960: firstwo=1;
7961: }
1.309 brouard 7962: 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 );
1.227 brouard 7963: }
1.257 brouard 7964: }else{ /* if date of interview is unknown */
1.227 brouard 7965: /* death is known but not confirmed by death status at any wave */
7966: if(firstfour==0){
1.309 brouard 7967: 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 );
1.227 brouard 7968: firstfour=1;
7969: }
1.309 brouard 7970: 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 );
1.214 brouard 7971: }
1.224 brouard 7972: } /* end if date of death is known */
7973: #endif
1.309 brouard 7974: wav[i]=mi; /* mi should be the last effective wave (or mli), */
7975: /* wav[i]=mw[mi][i]; */
1.126 brouard 7976: if(mi==0){
7977: nbwarn++;
7978: if(first==0){
1.227 brouard 7979: printf("Warning! No valid information for individual %ld line=%d (skipped) and may be others, see log file\n",num[i],i);
7980: first=1;
1.126 brouard 7981: }
7982: if(first==1){
1.227 brouard 7983: fprintf(ficlog,"Warning! No valid information for individual %ld line=%d (skipped)\n",num[i],i);
1.126 brouard 7984: }
7985: } /* end mi==0 */
7986: } /* End individuals */
1.214 brouard 7987: /* wav and mw are no more changed */
1.223 brouard 7988:
1.317 brouard 7989: printf("Information, you have to check %d informations which haven't been logged!\n",firsthree);
7990: fprintf(ficlog,"Information, you have to check %d informations which haven't been logged!\n",firsthree);
7991:
7992:
1.126 brouard 7993: for(i=1; i<=imx; i++){
7994: for(mi=1; mi<wav[i];mi++){
7995: if (stepm <=0)
1.227 brouard 7996: dh[mi][i]=1;
1.126 brouard 7997: else{
1.260 brouard 7998: if (s[mw[mi+1][i]][i] > nlstate) { /* A death, but what if date is unknown? */
1.227 brouard 7999: if (agedc[i] < 2*AGESUP) {
8000: j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12);
8001: if(j==0) j=1; /* Survives at least one month after exam */
8002: else if(j<0){
8003: nberr++;
1.359 brouard 8004: printf("Error! Negative delay (%d to death) between waves %d and %d of individual %ld (around line %d) who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);
1.227 brouard 8005: j=1; /* Temporary Dangerous patch */
8006: 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);
1.359 brouard 8007: fprintf(ficlog,"Error! Negative delay (%d to death) between waves %d and %d of individual %ld (around line %d) who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);
1.227 brouard 8008: 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);
8009: }
8010: k=k+1;
8011: if (j >= jmax){
8012: jmax=j;
8013: ijmax=i;
8014: }
8015: if (j <= jmin){
8016: jmin=j;
8017: ijmin=i;
8018: }
8019: sum=sum+j;
8020: /*if (j<0) printf("j=%d num=%d \n",j,i);*/
8021: /* printf("%d %d %d %d\n", s[mw[mi][i]][i] ,s[mw[mi+1][i]][i],j,i);*/
8022: }
8023: }
8024: else{
8025: j= rint( (agev[mw[mi+1][i]][i]*12 - agev[mw[mi][i]][i]*12));
1.126 brouard 8026: /* if (j<0) printf("%d %lf %lf %d %d %d\n", i,agev[mw[mi+1][i]][i], agev[mw[mi][i]][i],j,s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]); */
1.223 brouard 8027:
1.227 brouard 8028: k=k+1;
8029: if (j >= jmax) {
8030: jmax=j;
8031: ijmax=i;
8032: }
8033: else if (j <= jmin){
8034: jmin=j;
8035: ijmin=i;
8036: }
8037: /* if (j<10) printf("j=%d jmin=%d num=%d ",j,jmin,i); */
8038: /*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]);*/
8039: if(j<0){
8040: nberr++;
1.359 brouard 8041: printf("Error! Negative delay (%d) between waves %d and %d of individual %ld (around line %d) who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);
8042: fprintf(ficlog,"Error! Negative delay (%d) between waves %d and %d of individual %ld (around line %d) who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);
1.227 brouard 8043: }
8044: sum=sum+j;
8045: }
8046: jk= j/stepm;
8047: jl= j -jk*stepm;
8048: ju= j -(jk+1)*stepm;
8049: if(mle <=1){ /* only if we use a the linear-interpoloation pseudo-likelihood */
8050: if(jl==0){
8051: dh[mi][i]=jk;
8052: bh[mi][i]=0;
8053: }else{ /* We want a negative bias in order to only have interpolation ie
8054: * to avoid the price of an extra matrix product in likelihood */
8055: dh[mi][i]=jk+1;
8056: bh[mi][i]=ju;
8057: }
8058: }else{
8059: if(jl <= -ju){
8060: dh[mi][i]=jk;
8061: bh[mi][i]=jl; /* bias is positive if real duration
8062: * is higher than the multiple of stepm and negative otherwise.
8063: */
8064: }
8065: else{
8066: dh[mi][i]=jk+1;
8067: bh[mi][i]=ju;
8068: }
8069: if(dh[mi][i]==0){
8070: dh[mi][i]=1; /* At least one step */
8071: bh[mi][i]=ju; /* At least one step */
8072: /* 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);*/
8073: }
8074: } /* end if mle */
1.126 brouard 8075: }
8076: } /* end wave */
8077: }
8078: jmean=sum/k;
8079: printf("Delay (in months) between two waves Min=%d (for indiviudal %ld) Max=%d (%ld) Mean=%f\n\n ",jmin, num[ijmin], jmax, num[ijmax], jmean);
1.141 brouard 8080: fprintf(ficlog,"Delay (in months) between two waves Min=%d (for indiviudal %d) Max=%d (%d) Mean=%f\n\n ",jmin, ijmin, jmax, ijmax, jmean);
1.227 brouard 8081: }
1.126 brouard 8082:
8083: /*********** Tricode ****************************/
1.220 brouard 8084: void tricode(int *cptcov, int *Tvar, int **nbcode, int imx, int *Ndum)
1.242 brouard 8085: {
8086: /**< Uses cptcovn+2*cptcovprod as the number of covariates */
8087: /* Tvar[i]=atoi(stre); find 'n' in Vn and stores in Tvar. If model=V2+V1 Tvar[1]=2 and Tvar[2]=1
8088: * Boring subroutine which should only output nbcode[Tvar[j]][k]
8089: * Tvar[5] in V2+V1+V3*age+V2*V4 is 4 (V4) even it is a time varying or quantitative variable
8090: * nbcode[Tvar[5]][1]= nbcode[4][1]=0, nbcode[4][2]=1 (usually);
8091: */
1.130 brouard 8092:
1.242 brouard 8093: int ij=1, k=0, j=0, i=0, maxncov=NCOVMAX;
8094: int modmaxcovj=0; /* Modality max of covariates j */
8095: int cptcode=0; /* Modality max of covariates j */
8096: int modmincovj=0; /* Modality min of covariates j */
1.145 brouard 8097:
8098:
1.242 brouard 8099: /* cptcoveff=0; */
8100: /* *cptcov=0; */
1.126 brouard 8101:
1.242 brouard 8102: for (k=1; k <= maxncov; k++) ncodemax[k]=0; /* Horrible constant again replaced by NCOVMAX */
1.285 brouard 8103: for (k=1; k <= maxncov; k++)
8104: for(j=1; j<=2; j++)
8105: nbcode[k][j]=0; /* Valgrind */
1.126 brouard 8106:
1.242 brouard 8107: /* Loop on covariates without age and products and no quantitative variable */
1.335 brouard 8108: 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 */
1.242 brouard 8109: for (j=-1; (j < maxncov); j++) Ndum[j]=0;
1.343 brouard 8110: /* printf("Testing k=%d, cptcovt=%d\n",k, cptcovt); */
1.349 brouard 8111: if(Dummy[k]==0 && Typevar[k] !=1 && Typevar[k] != 3 && Typevar[k] != 2){ /* Dummy covariate and not age product nor fixed product */
1.242 brouard 8112: switch(Fixed[k]) {
8113: case 0: /* Testing on fixed dummy covariate, simple or product of fixed */
1.311 brouard 8114: modmaxcovj=0;
8115: modmincovj=0;
1.242 brouard 8116: 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*/
1.339 brouard 8117: /* 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])); */
1.242 brouard 8118: ij=(int)(covar[Tvar[k]][i]);
8119: /* ij=0 or 1 or -1. Value of the covariate Tvar[j] for individual i
8120: * If product of Vn*Vm, still boolean *:
8121: * If it was coded 1, 2, 3, 4 should be splitted into 3 boolean variables
8122: * 1 => 0 0 0, 2 => 0 0 1, 3 => 0 1 1, 4=1 0 0 */
8123: /* Finds for covariate j, n=Tvar[j] of Vn . ij is the
8124: modality of the nth covariate of individual i. */
8125: if (ij > modmaxcovj)
8126: modmaxcovj=ij;
8127: else if (ij < modmincovj)
8128: modmincovj=ij;
1.287 brouard 8129: if (ij <0 || ij >1 ){
1.311 brouard 8130: printf("ERROR, IMaCh doesn't treat covariate with missing values V%d=-1, individual %d will be skipped.\n",Tvar[k],i);
8131: fprintf(ficlog,"ERROR, currently IMaCh doesn't treat covariate with missing values V%d=-1, individual %d will be skipped.\n",Tvar[k],i);
8132: fflush(ficlog);
8133: exit(1);
1.287 brouard 8134: }
8135: if ((ij < -1) || (ij > NCOVMAX)){
1.242 brouard 8136: printf( "Error: minimal is less than -1 or maximal is bigger than %d. Exiting. \n", NCOVMAX );
8137: exit(1);
8138: }else
8139: Ndum[ij]++; /*counts and stores the occurence of this modality 0, 1, -1*/
8140: /* If coded 1, 2, 3 , counts the number of 1 Ndum[1], number of 2, Ndum[2], etc */
8141: /*printf("i=%d ij=%d Ndum[ij]=%d imx=%d",i,ij,Ndum[ij],imx);*/
8142: /* getting the maximum value of the modality of the covariate
8143: (should be 0 or 1 now) Tvar[j]. If V=sex and male is coded 0 and
8144: female ies 1, then modmaxcovj=1.
8145: */
8146: } /* end for loop on individuals i */
8147: printf(" Minimal and maximal values of %d th (fixed) covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj);
8148: fprintf(ficlog," Minimal and maximal values of %d th (fixed) covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj);
8149: cptcode=modmaxcovj;
8150: /* Ndum[0] = frequency of 0 for model-covariate j, Ndum[1] frequency of 1 etc. */
8151: /*for (i=0; i<=cptcode; i++) {*/
8152: for (j=modmincovj; j<=modmaxcovj; j++) { /* j=-1 ? 0 and 1*//* For each value j of the modality of model-cov k */
8153: printf("Frequencies of (fixed) covariate %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]);
8154: fprintf(ficlog, "Frequencies of (fixed) covariate %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]);
8155: if( Ndum[j] != 0 ){ /* Counts if nobody answered modality j ie empty modality, we skip it and reorder */
8156: if( j != -1){
8157: ncodemax[k]++; /* ncodemax[k]= Number of modalities of the k th
8158: covariate for which somebody answered excluding
8159: undefined. Usually 2: 0 and 1. */
8160: }
8161: ncodemaxwundef[k]++; /* ncodemax[j]= Number of modalities of the k th
8162: covariate for which somebody answered including
8163: undefined. Usually 3: -1, 0 and 1. */
8164: } /* In fact ncodemax[k]=2 (dichotom. variables only) but it could be more for
8165: * historical reasons: 3 if coded 1, 2, 3 and 4 and Ndum[2]=0 */
8166: } /* Ndum[-1] number of undefined modalities */
1.231 brouard 8167:
1.242 brouard 8168: /* j is a covariate, n=Tvar[j] of Vn; Fills nbcode */
8169: /* For covariate j, modalities could be 1, 2, 3, 4, 5, 6, 7. */
8170: /* If Ndum[1]=0, Ndum[2]=0, Ndum[3]= 635, Ndum[4]=0, Ndum[5]=0, Ndum[6]=27, Ndum[7]=125; */
8171: /* modmincovj=3; modmaxcovj = 7; */
8172: /* There are only 3 modalities non empty 3, 6, 7 (or 2 if 27 is too few) : ncodemax[j]=3; */
8173: /* which will be coded 0, 1, 2 which in binary on 2=3-1 digits are 0=00 1=01, 2=10; */
8174: /* defining two dummy variables: variables V1_1 and V1_2.*/
8175: /* nbcode[Tvar[j]][ij]=k; */
8176: /* nbcode[Tvar[j]][1]=0; */
8177: /* nbcode[Tvar[j]][2]=1; */
8178: /* nbcode[Tvar[j]][3]=2; */
8179: /* To be continued (not working yet). */
8180: ij=0; /* ij is similar to i but can jump over null modalities */
1.287 brouard 8181:
8182: /* 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*/
8183: /* Skipping the case of missing values by reducing nbcode to 0 and 1 and not -1, 0, 1 */
8184: /* model=V1+V2+V3, if V2=-1, 0 or 1, then nbcode[2][1]=0 and nbcode[2][2]=1 instead of
8185: * nbcode[2][1]=-1, nbcode[2][2]=0 and nbcode[2][3]=1 */
8186: /*, could be restored in the future */
8187: 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*/
1.242 brouard 8188: if (Ndum[i] == 0) { /* If nobody responded to this modality k */
8189: break;
8190: }
8191: ij++;
1.287 brouard 8192: 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*/
1.242 brouard 8193: cptcode = ij; /* New max modality for covar j */
8194: } /* end of loop on modality i=-1 to 1 or more */
8195: break;
8196: case 1: /* Testing on varying covariate, could be simple and
8197: * should look at waves or product of fixed *
8198: * varying. No time to test -1, assuming 0 and 1 only */
8199: ij=0;
8200: for(i=0; i<=1;i++){
8201: nbcode[Tvar[k]][++ij]=i;
8202: }
8203: break;
8204: default:
8205: break;
8206: } /* end switch */
8207: } /* end dummy test */
1.349 brouard 8208: if(Dummy[k]==1 && Typevar[k] !=1 && Typevar[k] !=3 && Fixed ==0){ /* Fixed Quantitative covariate and not age product */
1.311 brouard 8209: 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*/
1.335 brouard 8210: if(Tvar[k]<=0 || Tvar[k]>=NCOVMAX){
8211: printf("Error k=%d \n",k);
8212: exit(1);
8213: }
1.311 brouard 8214: if(isnan(covar[Tvar[k]][i])){
8215: printf("ERROR, IMaCh doesn't treat fixed quantitative covariate with missing values V%d=., individual %d will be skipped.\n",Tvar[k],i);
8216: fprintf(ficlog,"ERROR, currently IMaCh doesn't treat covariate with missing values V%d=., individual %d will be skipped.\n",Tvar[k],i);
8217: fflush(ficlog);
8218: exit(1);
8219: }
8220: }
1.335 brouard 8221: } /* end Quanti */
1.287 brouard 8222: } /* 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*/
1.242 brouard 8223:
8224: for (k=-1; k< maxncov; k++) Ndum[k]=0;
8225: /* Look at fixed dummy (single or product) covariates to check empty modalities */
8226: for (i=1; i<=ncovmodel-2-nagesqr; i++) { /* -2, cste and age and eventually age*age */
8227: /* Listing of all covariables in statement model to see if some covariates appear twice. For example, V1 appears twice in V1+V1*V2.*/
8228: 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 */
8229: 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 */
8230: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, {2, 1, 1, 1, 2, 1, 1, 0, 0} */
8231: } /* V4+V3+V5, Ndum[1]@5={0, 0, 1, 1, 1} */
8232:
8233: ij=0;
8234: /* for (i=0; i<= maxncov-1; i++) { /\* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) *\/ */
1.335 brouard 8235: 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 */
8236: /* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) */
1.242 brouard 8237: /*printf("Ndum[%d]=%d\n",i, Ndum[i]);*/
8238: /* if((Ndum[i]!=0) && (i<=ncovcol)){ /\* Tvar[i] <= ncovmodel ? *\/ */
1.335 brouard 8239: if(Ndum[Tvar[k]]!=0 && Dummy[k] == 0 && Typevar[k]==0){ /* Only Dummy simple and non empty in the model */
8240: /* Typevar[k] =0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product */
8241: /* Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product*/
1.242 brouard 8242: /* If product not in single variable we don't print results */
8243: /*printf("diff Ndum[%d]=%d\n",i, Ndum[i]);*/
1.335 brouard 8244: ++ij;/* V5 + V4 + V3 + V4*V3 + V5*age + V2 + V1*V2 + V1*age + V1, *//* V5 quanti, V2 quanti, V4, V3, V1 dummies */
8245: /* k= 1 2 3 4 5 6 7 8 9 */
8246: /* Tvar[k]= 5 4 3 6 5 2 7 1 1 */
8247: /* ij 1 2 3 */
8248: /* Tvaraff[ij]= 4 3 1 */
8249: /* Tmodelind[ij]=2 3 9 */
8250: /* TmodelInvind[ij]=2 1 1 */
1.242 brouard 8251: 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*/
8252: Tmodelind[ij]=k; /* Tmodelind: index in model of dummies Tmodelind[1]=2 V4: pos=2; V3: pos=3, V1=9 {2, 3, 9, ?, ?,} */
8253: 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 */
8254: if(Fixed[k]!=0)
8255: anyvaryingduminmodel=1;
8256: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv)){ */
8257: /* Tvaraff[++ij]=-10; /\* Dont'n know how to treat quantitative variables yet *\/ */
8258: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv)){ */
8259: /* Tvaraff[++ij]=i; /\*For printing (unclear) *\/ */
8260: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv+nqtv)){ */
8261: /* Tvaraff[++ij]=-20; /\* Dont'n know how to treat quantitative variables yet *\/ */
8262: }
8263: } /* Tvaraff[1]@5 {3, 4, -20, 0, 0} Very strange */
8264: /* ij--; */
8265: /* cptcoveff=ij; /\*Number of total covariates*\/ */
1.335 brouard 8266: *cptcov=ij; /* cptcov= Number of total real effective simple dummies (fixed or time arying) effective (used as cptcoveff in other functions)
1.242 brouard 8267: * because they can be excluded from the model and real
8268: * if in the model but excluded because missing values, but how to get k from ij?*/
8269: for(j=ij+1; j<= cptcovt; j++){
8270: Tvaraff[j]=0;
8271: Tmodelind[j]=0;
8272: }
8273: for(j=ntveff+1; j<= cptcovt; j++){
8274: TmodelInvind[j]=0;
8275: }
8276: /* To be sorted */
8277: ;
8278: }
1.126 brouard 8279:
1.145 brouard 8280:
1.126 brouard 8281: /*********** Health Expectancies ****************/
8282:
1.235 brouard 8283: void evsij(double ***eij, double x[], int nlstate, int stepm, int bage, int fage, double **oldm, double **savm, int cij, int estepm,char strstart[], int nres )
1.126 brouard 8284:
8285: {
8286: /* Health expectancies, no variances */
1.329 brouard 8287: /* cij is the combination in the list of combination of dummy covariates */
8288: /* strstart is a string of time at start of computing */
1.164 brouard 8289: int i, j, nhstepm, hstepm, h, nstepm;
1.126 brouard 8290: int nhstepma, nstepma; /* Decreasing with age */
8291: double age, agelim, hf;
8292: double ***p3mat;
8293: double eip;
8294:
1.238 brouard 8295: /* pstamp(ficreseij); */
1.126 brouard 8296: fprintf(ficreseij,"# (a) Life expectancies by health status at initial age and (b) health expectancies by health status at initial age\n");
8297: fprintf(ficreseij,"# Age");
8298: for(i=1; i<=nlstate;i++){
8299: for(j=1; j<=nlstate;j++){
8300: fprintf(ficreseij," e%1d%1d ",i,j);
8301: }
8302: fprintf(ficreseij," e%1d. ",i);
8303: }
8304: fprintf(ficreseij,"\n");
8305:
8306:
8307: if(estepm < stepm){
8308: printf ("Problem %d lower than %d\n",estepm, stepm);
8309: }
8310: else hstepm=estepm;
8311: /* We compute the life expectancy from trapezoids spaced every estepm months
8312: * This is mainly to measure the difference between two models: for example
8313: * if stepm=24 months pijx are given only every 2 years and by summing them
8314: * we are calculating an estimate of the Life Expectancy assuming a linear
8315: * progression in between and thus overestimating or underestimating according
8316: * to the curvature of the survival function. If, for the same date, we
8317: * estimate the model with stepm=1 month, we can keep estepm to 24 months
8318: * to compare the new estimate of Life expectancy with the same linear
8319: * hypothesis. A more precise result, taking into account a more precise
8320: * curvature will be obtained if estepm is as small as stepm. */
8321:
8322: /* For example we decided to compute the life expectancy with the smallest unit */
8323: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
8324: nhstepm is the number of hstepm from age to agelim
8325: nstepm is the number of stepm from age to agelin.
1.270 brouard 8326: Look at hpijx to understand the reason which relies in memory size consideration
1.126 brouard 8327: and note for a fixed period like estepm months */
8328: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
8329: survival function given by stepm (the optimization length). Unfortunately it
8330: means that if the survival funtion is printed only each two years of age and if
8331: you sum them up and add 1 year (area under the trapezoids) you won't get the same
8332: results. So we changed our mind and took the option of the best precision.
8333: */
8334: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
8335:
8336: agelim=AGESUP;
8337: /* If stepm=6 months */
8338: /* Computed by stepm unit matrices, product of hstepm matrices, stored
8339: in an array of nhstepm length: nhstepm=10, hstepm=4, stepm=6 months */
8340:
8341: /* nhstepm age range expressed in number of stepm */
8342: nstepm=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
8343: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
8344: /* if (stepm >= YEARM) hstepm=1;*/
8345: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
8346: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8347:
8348: for (age=bage; age<=fage; age ++){
8349: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
8350: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
8351: /* if (stepm >= YEARM) hstepm=1;*/
8352: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
8353:
8354: /* If stepm=6 months */
8355: /* Computed by stepm unit matrices, product of hstepma matrices, stored
8356: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
1.330 brouard 8357: /* printf("HELLO evsij Entering hpxij age=%d cij=%d hstepm=%d x[1]=%f nres=%d\n",(int) age, cij, hstepm, x[1], nres); */
1.235 brouard 8358: hpxij(p3mat,nhstepma,age,hstepm,x,nlstate,stepm,oldm, savm, cij, nres);
1.126 brouard 8359:
8360: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
8361:
8362: printf("%d|",(int)age);fflush(stdout);
8363: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
8364:
8365: /* Computing expectancies */
8366: for(i=1; i<=nlstate;i++)
8367: for(j=1; j<=nlstate;j++)
8368: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
8369: eij[i][j][(int)age] += (p3mat[i][j][h]+p3mat[i][j][h+1])/2.0*hf;
8370:
8371: /* 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]);*/
8372:
8373: }
8374:
8375: fprintf(ficreseij,"%3.0f",age );
8376: for(i=1; i<=nlstate;i++){
8377: eip=0;
8378: for(j=1; j<=nlstate;j++){
8379: eip +=eij[i][j][(int)age];
8380: fprintf(ficreseij,"%9.4f", eij[i][j][(int)age] );
8381: }
8382: fprintf(ficreseij,"%9.4f", eip );
8383: }
8384: fprintf(ficreseij,"\n");
8385:
8386: }
8387: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8388: printf("\n");
8389: fprintf(ficlog,"\n");
8390:
8391: }
8392:
1.235 brouard 8393: void cvevsij(double ***eij, double x[], int nlstate, int stepm, int bage, int fage, double **oldm, double **savm, int cij, int estepm,double delti[],double **matcov,char strstart[], int nres )
1.126 brouard 8394:
8395: {
8396: /* Covariances of health expectancies eij and of total life expectancies according
1.222 brouard 8397: to initial status i, ei. .
1.126 brouard 8398: */
1.336 brouard 8399: /* Very time consuming function, but already optimized with precov */
1.126 brouard 8400: int i, j, nhstepm, hstepm, h, nstepm, k, cptj, cptj2, i2, j2, ij, ji;
8401: int nhstepma, nstepma; /* Decreasing with age */
8402: double age, agelim, hf;
8403: double ***p3matp, ***p3matm, ***varhe;
8404: double **dnewm,**doldm;
8405: double *xp, *xm;
8406: double **gp, **gm;
8407: double ***gradg, ***trgradg;
8408: int theta;
8409:
8410: double eip, vip;
8411:
8412: varhe=ma3x(1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int) fage);
8413: xp=vector(1,npar);
8414: xm=vector(1,npar);
8415: dnewm=matrix(1,nlstate*nlstate,1,npar);
8416: doldm=matrix(1,nlstate*nlstate,1,nlstate*nlstate);
8417:
8418: pstamp(ficresstdeij);
8419: fprintf(ficresstdeij,"# Health expectancies with standard errors\n");
8420: fprintf(ficresstdeij,"# Age");
8421: for(i=1; i<=nlstate;i++){
8422: for(j=1; j<=nlstate;j++)
8423: fprintf(ficresstdeij," e%1d%1d (SE)",i,j);
8424: fprintf(ficresstdeij," e%1d. ",i);
8425: }
8426: fprintf(ficresstdeij,"\n");
8427:
8428: pstamp(ficrescveij);
8429: fprintf(ficrescveij,"# Subdiagonal matrix of covariances of health expectancies by age: cov(eij,ekl)\n");
8430: fprintf(ficrescveij,"# Age");
8431: for(i=1; i<=nlstate;i++)
8432: for(j=1; j<=nlstate;j++){
8433: cptj= (j-1)*nlstate+i;
8434: for(i2=1; i2<=nlstate;i2++)
8435: for(j2=1; j2<=nlstate;j2++){
8436: cptj2= (j2-1)*nlstate+i2;
8437: if(cptj2 <= cptj)
8438: fprintf(ficrescveij," %1d%1d,%1d%1d",i,j,i2,j2);
8439: }
8440: }
8441: fprintf(ficrescveij,"\n");
8442:
8443: if(estepm < stepm){
8444: printf ("Problem %d lower than %d\n",estepm, stepm);
8445: }
8446: else hstepm=estepm;
8447: /* We compute the life expectancy from trapezoids spaced every estepm months
8448: * This is mainly to measure the difference between two models: for example
8449: * if stepm=24 months pijx are given only every 2 years and by summing them
8450: * we are calculating an estimate of the Life Expectancy assuming a linear
8451: * progression in between and thus overestimating or underestimating according
8452: * to the curvature of the survival function. If, for the same date, we
8453: * estimate the model with stepm=1 month, we can keep estepm to 24 months
8454: * to compare the new estimate of Life expectancy with the same linear
8455: * hypothesis. A more precise result, taking into account a more precise
8456: * curvature will be obtained if estepm is as small as stepm. */
8457:
8458: /* For example we decided to compute the life expectancy with the smallest unit */
8459: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
8460: nhstepm is the number of hstepm from age to agelim
8461: nstepm is the number of stepm from age to agelin.
8462: Look at hpijx to understand the reason of that which relies in memory size
8463: and note for a fixed period like estepm months */
8464: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
8465: survival function given by stepm (the optimization length). Unfortunately it
8466: means that if the survival funtion is printed only each two years of age and if
8467: you sum them up and add 1 year (area under the trapezoids) you won't get the same
8468: results. So we changed our mind and took the option of the best precision.
8469: */
8470: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
8471:
8472: /* If stepm=6 months */
8473: /* nhstepm age range expressed in number of stepm */
8474: agelim=AGESUP;
8475: nstepm=(int) rint((agelim-bage)*YEARM/stepm);
8476: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
8477: /* if (stepm >= YEARM) hstepm=1;*/
8478: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
8479:
8480: p3matp=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8481: p3matm=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8482: gradg=ma3x(0,nhstepm,1,npar,1,nlstate*nlstate);
8483: trgradg =ma3x(0,nhstepm,1,nlstate*nlstate,1,npar);
8484: gp=matrix(0,nhstepm,1,nlstate*nlstate);
8485: gm=matrix(0,nhstepm,1,nlstate*nlstate);
8486:
8487: for (age=bage; age<=fage; age ++){
8488: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
8489: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
8490: /* if (stepm >= YEARM) hstepm=1;*/
8491: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
1.218 brouard 8492:
1.126 brouard 8493: /* If stepm=6 months */
8494: /* Computed by stepm unit matrices, product of hstepma matrices, stored
8495: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
8496:
8497: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
1.218 brouard 8498:
1.126 brouard 8499: /* Computing Variances of health expectancies */
8500: /* Gradient is computed with plus gp and minus gm. Code is duplicated in order to
8501: decrease memory allocation */
8502: for(theta=1; theta <=npar; theta++){
8503: for(i=1; i<=npar; i++){
1.222 brouard 8504: xp[i] = x[i] + (i==theta ?delti[theta]:0);
8505: xm[i] = x[i] - (i==theta ?delti[theta]:0);
1.126 brouard 8506: }
1.235 brouard 8507: hpxij(p3matp,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, cij, nres);
8508: hpxij(p3matm,nhstepm,age,hstepm,xm,nlstate,stepm,oldm,savm, cij, nres);
1.218 brouard 8509:
1.126 brouard 8510: for(j=1; j<= nlstate; j++){
1.222 brouard 8511: for(i=1; i<=nlstate; i++){
8512: for(h=0; h<=nhstepm-1; h++){
8513: gp[h][(j-1)*nlstate + i] = (p3matp[i][j][h]+p3matp[i][j][h+1])/2.;
8514: gm[h][(j-1)*nlstate + i] = (p3matm[i][j][h]+p3matm[i][j][h+1])/2.;
8515: }
8516: }
1.126 brouard 8517: }
1.218 brouard 8518:
1.126 brouard 8519: for(ij=1; ij<= nlstate*nlstate; ij++)
1.222 brouard 8520: for(h=0; h<=nhstepm-1; h++){
8521: gradg[h][theta][ij]= (gp[h][ij]-gm[h][ij])/2./delti[theta];
8522: }
1.126 brouard 8523: }/* End theta */
8524:
8525:
8526: for(h=0; h<=nhstepm-1; h++)
8527: for(j=1; j<=nlstate*nlstate;j++)
1.222 brouard 8528: for(theta=1; theta <=npar; theta++)
8529: trgradg[h][j][theta]=gradg[h][theta][j];
1.126 brouard 8530:
1.218 brouard 8531:
1.222 brouard 8532: for(ij=1;ij<=nlstate*nlstate;ij++)
1.126 brouard 8533: for(ji=1;ji<=nlstate*nlstate;ji++)
1.222 brouard 8534: varhe[ij][ji][(int)age] =0.;
1.218 brouard 8535:
1.222 brouard 8536: printf("%d|",(int)age);fflush(stdout);
8537: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
8538: for(h=0;h<=nhstepm-1;h++){
1.126 brouard 8539: for(k=0;k<=nhstepm-1;k++){
1.222 brouard 8540: matprod2(dnewm,trgradg[h],1,nlstate*nlstate,1,npar,1,npar,matcov);
8541: matprod2(doldm,dnewm,1,nlstate*nlstate,1,npar,1,nlstate*nlstate,gradg[k]);
8542: for(ij=1;ij<=nlstate*nlstate;ij++)
8543: for(ji=1;ji<=nlstate*nlstate;ji++)
8544: varhe[ij][ji][(int)age] += doldm[ij][ji]*hf*hf;
1.126 brouard 8545: }
8546: }
1.320 brouard 8547: /* if((int)age ==50){ */
8548: /* printf(" age=%d cij=%d nres=%d varhe[%d][%d]=%f ",(int)age, cij, nres, 1,2,varhe[1][2]); */
8549: /* } */
1.126 brouard 8550: /* Computing expectancies */
1.235 brouard 8551: hpxij(p3matm,nhstepm,age,hstepm,x,nlstate,stepm,oldm, savm, cij,nres);
1.126 brouard 8552: for(i=1; i<=nlstate;i++)
8553: for(j=1; j<=nlstate;j++)
1.222 brouard 8554: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
8555: eij[i][j][(int)age] += (p3matm[i][j][h]+p3matm[i][j][h+1])/2.0*hf;
1.218 brouard 8556:
1.222 brouard 8557: /* if((int)age==70)printf("i=%2d,j=%2d,h=%2d,age=%3d,%9.4f,%9.4f,%9.4f\n",i,j,h,(int)age,p3mat[i][j][h],hf,eij[i][j][(int)age]);*/
1.218 brouard 8558:
1.222 brouard 8559: }
1.269 brouard 8560:
8561: /* Standard deviation of expectancies ij */
1.126 brouard 8562: fprintf(ficresstdeij,"%3.0f",age );
8563: for(i=1; i<=nlstate;i++){
8564: eip=0.;
8565: vip=0.;
8566: for(j=1; j<=nlstate;j++){
1.222 brouard 8567: eip += eij[i][j][(int)age];
8568: for(k=1; k<=nlstate;k++) /* Sum on j and k of cov(eij,eik) */
8569: vip += varhe[(j-1)*nlstate+i][(k-1)*nlstate+i][(int)age];
8570: fprintf(ficresstdeij," %9.4f (%.4f)", eij[i][j][(int)age], sqrt(varhe[(j-1)*nlstate+i][(j-1)*nlstate+i][(int)age]) );
1.126 brouard 8571: }
8572: fprintf(ficresstdeij," %9.4f (%.4f)", eip, sqrt(vip));
8573: }
8574: fprintf(ficresstdeij,"\n");
1.218 brouard 8575:
1.269 brouard 8576: /* Variance of expectancies ij */
1.126 brouard 8577: fprintf(ficrescveij,"%3.0f",age );
8578: for(i=1; i<=nlstate;i++)
8579: for(j=1; j<=nlstate;j++){
1.222 brouard 8580: cptj= (j-1)*nlstate+i;
8581: for(i2=1; i2<=nlstate;i2++)
8582: for(j2=1; j2<=nlstate;j2++){
8583: cptj2= (j2-1)*nlstate+i2;
8584: if(cptj2 <= cptj)
8585: fprintf(ficrescveij," %.4f", varhe[cptj][cptj2][(int)age]);
8586: }
1.126 brouard 8587: }
8588: fprintf(ficrescveij,"\n");
1.218 brouard 8589:
1.126 brouard 8590: }
8591: free_matrix(gm,0,nhstepm,1,nlstate*nlstate);
8592: free_matrix(gp,0,nhstepm,1,nlstate*nlstate);
8593: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate*nlstate);
8594: free_ma3x(trgradg,0,nhstepm,1,nlstate*nlstate,1,npar);
8595: free_ma3x(p3matm,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8596: free_ma3x(p3matp,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8597: printf("\n");
8598: fprintf(ficlog,"\n");
1.218 brouard 8599:
1.126 brouard 8600: free_vector(xm,1,npar);
8601: free_vector(xp,1,npar);
8602: free_matrix(dnewm,1,nlstate*nlstate,1,npar);
8603: free_matrix(doldm,1,nlstate*nlstate,1,nlstate*nlstate);
8604: free_ma3x(varhe,1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int)fage);
8605: }
1.218 brouard 8606:
1.126 brouard 8607: /************ Variance ******************/
1.235 brouard 8608: void varevsij(char optionfilefiname[], double ***vareij, double **matcov, double x[], double delti[], int nlstate, int stepm, double bage, double fage, double **oldm, double **savm, double **prlim, double ftolpl, int *ncvyearp, int ij, int estepm, int cptcov, int cptcod, int popbased, int mobilav, char strstart[], int nres)
1.218 brouard 8609: {
1.361 brouard 8610: /** Computes the matrix of variance covariance of health expectancies e.j= sum_i w_i e_ij where w_i depends of popbased,
8611: * either cross-sectional or implied.
8612: * return vareij[i][j][(int)age]=cov(e.i,e.j)=sum_h sum_k trgrad(h_p.i) V(theta) grad(k_p.k) Equation 20
1.279 brouard 8613: * double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);
8614: * double **newm;
8615: * int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav)
8616: */
1.218 brouard 8617:
8618: /* int movingaverage(); */
8619: double **dnewm,**doldm;
8620: double **dnewmp,**doldmp;
8621: int i, j, nhstepm, hstepm, h, nstepm ;
1.288 brouard 8622: int first=0;
1.218 brouard 8623: int k;
8624: double *xp;
1.279 brouard 8625: double **gp, **gm; /**< for var eij */
8626: double ***gradg, ***trgradg; /**< for var eij */
8627: double **gradgp, **trgradgp; /**< for var p point j */
8628: double *gpp, *gmp; /**< for var p point j */
1.362 brouard 8629: double **varppt; /**< for var p.3 p.death nlstate+1 to nlstate+ndeath */
1.218 brouard 8630: double ***p3mat;
8631: double age,agelim, hf;
8632: /* double ***mobaverage; */
8633: int theta;
8634: char digit[4];
8635: char digitp[25];
8636:
8637: char fileresprobmorprev[FILENAMELENGTH];
8638:
8639: if(popbased==1){
8640: if(mobilav!=0)
8641: strcpy(digitp,"-POPULBASED-MOBILAV_");
8642: else strcpy(digitp,"-POPULBASED-NOMOBIL_");
8643: }
8644: else
8645: strcpy(digitp,"-STABLBASED_");
1.126 brouard 8646:
1.218 brouard 8647: /* if (mobilav!=0) { */
8648: /* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
8649: /* if (movingaverage(probs, bage, fage, mobaverage,mobilav)!=0){ */
8650: /* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); */
8651: /* printf(" Error in movingaverage mobilav=%d\n",mobilav); */
8652: /* } */
8653: /* } */
8654:
8655: strcpy(fileresprobmorprev,"PRMORPREV-");
8656: sprintf(digit,"%-d",ij);
8657: /*printf("DIGIT=%s, ij=%d ijr=%-d|\n",digit, ij,ij);*/
8658: strcat(fileresprobmorprev,digit); /* Tvar to be done */
8659: strcat(fileresprobmorprev,digitp); /* Popbased or not, mobilav or not */
8660: strcat(fileresprobmorprev,fileresu);
8661: if((ficresprobmorprev=fopen(fileresprobmorprev,"w"))==NULL) {
8662: printf("Problem with resultfile: %s\n", fileresprobmorprev);
8663: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobmorprev);
8664: }
8665: printf("Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
8666: fprintf(ficlog,"Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
8667: pstamp(ficresprobmorprev);
8668: 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);
1.238 brouard 8669: fprintf(ficresprobmorprev,"# Selected quantitative variables and dummies");
1.337 brouard 8670:
8671: /* We use TinvDoQresult[nres][resultmodel[nres][j] we sort according to the equation model and the resultline: it is a choice */
8672: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ /\* To be done*\/ */
8673: /* fprintf(ficresprobmorprev," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
8674: /* } */
8675: for (j=1; j<= cptcovs; j++){ /* For each selected (single) quantitative value */ /* To be done*/
1.344 brouard 8676: /* fprintf(ficresprobmorprev," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]); */
1.337 brouard 8677: fprintf(ficresprobmorprev," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
1.238 brouard 8678: }
1.337 brouard 8679: /* for(j=1;j<=cptcoveff;j++) */
8680: /* fprintf(ficresprobmorprev," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(ij,TnsdVar[Tvaraff[j]])]); */
1.238 brouard 8681: fprintf(ficresprobmorprev,"\n");
8682:
1.218 brouard 8683: fprintf(ficresprobmorprev,"# Age cov=%-d",ij);
8684: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
8685: fprintf(ficresprobmorprev," p.%-d SE",j);
8686: for(i=1; i<=nlstate;i++)
8687: fprintf(ficresprobmorprev," w%1d p%-d%-d",i,i,j);
8688: }
8689: fprintf(ficresprobmorprev,"\n");
8690:
8691: fprintf(ficgp,"\n# Routine varevsij");
8692: fprintf(ficgp,"\nunset title \n");
8693: /* fprintf(fichtm, "#Local time at start: %s", strstart);*/
8694: 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");
8695: fprintf(fichtm,"\n<br>%s <br>\n",digitp);
1.279 brouard 8696:
1.361 brouard 8697: varppt = matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath); /* In fact, currently a double */
1.218 brouard 8698: pstamp(ficresvij);
8699: fprintf(ficresvij,"# Variance and covariance of health expectancies e.j \n# (weighted average of eij where weights are ");
8700: if(popbased==1)
8701: 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);
8702: else
8703: fprintf(ficresvij,"the age specific period (stable) prevalences in each health state \n");
8704: fprintf(ficresvij,"# Age");
8705: for(i=1; i<=nlstate;i++)
8706: for(j=1; j<=nlstate;j++)
8707: fprintf(ficresvij," Cov(e.%1d, e.%1d)",i,j);
8708: fprintf(ficresvij,"\n");
8709:
8710: xp=vector(1,npar);
8711: dnewm=matrix(1,nlstate,1,npar);
8712: doldm=matrix(1,nlstate,1,nlstate);
8713: dnewmp= matrix(nlstate+1,nlstate+ndeath,1,npar);
8714: doldmp= matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
8715:
8716: gradgp=matrix(1,npar,nlstate+1,nlstate+ndeath);
8717: gpp=vector(nlstate+1,nlstate+ndeath);
8718: gmp=vector(nlstate+1,nlstate+ndeath);
8719: trgradgp =matrix(nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
1.126 brouard 8720:
1.218 brouard 8721: if(estepm < stepm){
8722: printf ("Problem %d lower than %d\n",estepm, stepm);
8723: }
8724: else hstepm=estepm;
8725: /* For example we decided to compute the life expectancy with the smallest unit */
8726: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
8727: nhstepm is the number of hstepm from age to agelim
8728: nstepm is the number of stepm from age to agelim.
8729: Look at function hpijx to understand why because of memory size limitations,
8730: we decided (b) to get a life expectancy respecting the most precise curvature of the
8731: survival function given by stepm (the optimization length). Unfortunately it
8732: means that if the survival funtion is printed every two years of age and if
8733: you sum them up and add 1 year (area under the trapezoids) you won't get the same
8734: results. So we changed our mind and took the option of the best precision.
8735: */
8736: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
8737: agelim = AGESUP;
8738: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
8739: nstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
8740: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
8741: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8742: gradg=ma3x(0,nhstepm,1,npar,1,nlstate);
8743: gp=matrix(0,nhstepm,1,nlstate);
8744: gm=matrix(0,nhstepm,1,nlstate);
8745:
8746:
8747: for(theta=1; theta <=npar; theta++){
8748: for(i=1; i<=npar; i++){ /* Computes gradient x + delta*/
8749: xp[i] = x[i] + (i==theta ?delti[theta]:0);
8750: }
1.279 brouard 8751: /**< Computes the prevalence limit with parameter theta shifted of delta up to ftolpl precision and
8752: * returns into prlim .
1.288 brouard 8753: */
1.242 brouard 8754: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij, nres);
1.279 brouard 8755:
8756: /* If popbased = 1 we use crossection prevalences. Previous step is useless but prlim is created */
1.218 brouard 8757: if (popbased==1) {
8758: if(mobilav ==0){
8759: for(i=1; i<=nlstate;i++)
8760: prlim[i][i]=probs[(int)age][i][ij];
8761: }else{ /* mobilav */
8762: for(i=1; i<=nlstate;i++)
8763: prlim[i][i]=mobaverage[(int)age][i][ij];
8764: }
8765: }
1.361 brouard 8766: /**< Computes the shifted plus (gp) transition matrix \f$ {}{h}_p^{ij}x\f$ at horizon h.
1.279 brouard 8767: */
8768: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij,nres); /* Returns p3mat[i][j][h] for h=0 to nhstepm */
1.292 brouard 8769: /**< And for each alive state j, sums over i \f$ w^i_x {}{h}_p^{ij}x\f$, which are the probability
1.279 brouard 8770: * at horizon h in state j including mortality.
8771: */
1.218 brouard 8772: for(j=1; j<= nlstate; j++){
8773: for(h=0; h<=nhstepm; h++){
8774: for(i=1, gp[h][j]=0.;i<=nlstate;i++)
1.361 brouard 8775: gp[h][j] += prlim[i][i]*p3mat[i][j][h]; /* gp[h][j]= w_i h_pij */
1.218 brouard 8776: }
8777: }
1.279 brouard 8778: /* Next for computing shifted+ probability of death (h=1 means
1.218 brouard 8779: computed over hstepm matrices product = hstepm*stepm months)
1.279 brouard 8780: as a weighted average of prlim(i) * p(i,j) p.3=w1*p13 + w2*p23 .
1.218 brouard 8781: */
1.361 brouard 8782: for(j=nlstate+1;j<=nlstate+ndeath;j++){ /* Currently only once for theta plus p.3(age) Sum_i wi pi3*/
1.218 brouard 8783: for(i=1,gpp[j]=0.; i<= nlstate; i++)
8784: gpp[j] += prlim[i][i]*p3mat[i][j][1];
1.279 brouard 8785: }
8786:
8787: /* Again with minus shift */
1.218 brouard 8788:
8789: for(i=1; i<=npar; i++) /* Computes gradient x - delta */
8790: xp[i] = x[i] - (i==theta ?delti[theta]:0);
1.288 brouard 8791:
1.242 brouard 8792: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp, ij, nres);
1.218 brouard 8793:
8794: if (popbased==1) {
8795: if(mobilav ==0){
8796: for(i=1; i<=nlstate;i++)
8797: prlim[i][i]=probs[(int)age][i][ij];
8798: }else{ /* mobilav */
8799: for(i=1; i<=nlstate;i++)
8800: prlim[i][i]=mobaverage[(int)age][i][ij];
8801: }
8802: }
8803:
1.361 brouard 8804: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij,nres); /* Still minus */
1.218 brouard 8805:
1.361 brouard 8806: for(j=1; j<= nlstate; j++){ /* gm[h][j]= Sum_i of wi * pij = h_p.j */
1.218 brouard 8807: for(h=0; h<=nhstepm; h++){
8808: for(i=1, gm[h][j]=0.;i<=nlstate;i++)
8809: gm[h][j] += prlim[i][i]*p3mat[i][j][h];
8810: }
8811: }
8812: /* This for computing probability of death (h=1 means
8813: computed over hstepm matrices product = hstepm*stepm months)
1.361 brouard 8814: as a weighted average of prlim. j is death. gmp[3]=sum_i w_i*p_i3=p.3 minus theta
1.218 brouard 8815: */
1.361 brouard 8816: for(j=nlstate+1;j<=nlstate+ndeath;j++){ /* Currently only once theta_minus p.3=Sum_i wi pi3*/
1.218 brouard 8817: for(i=1,gmp[j]=0.; i<= nlstate; i++)
8818: gmp[j] += prlim[i][i]*p3mat[i][j][1];
8819: }
1.279 brouard 8820: /* end shifting computations */
8821:
1.361 brouard 8822: /**< Computing gradient of p.j matrix at horizon h and still for one parameter of vector theta
8823: * equation 31 and 32
1.279 brouard 8824: */
1.361 brouard 8825: for(j=1; j<= nlstate; j++) /* computes grad p.j(x, over each h) where p.j is Sum_i w_i*pij(x over h)
8826: * equation 24 */
1.218 brouard 8827: for(h=0; h<=nhstepm; h++){
8828: gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta];
8829: }
1.361 brouard 8830: /**< Gradient of overall mortality p.3 (or p.death)
1.279 brouard 8831: */
1.361 brouard 8832: for(j=nlstate+1; j<= nlstate+ndeath; j++){ /* computes grad of p.3 from wi+pi3 grad p.3 (theta) */
1.218 brouard 8833: gradgp[theta][j]= (gpp[j]-gmp[j])/2./delti[theta];
8834: }
8835:
8836: } /* End theta */
1.279 brouard 8837:
1.361 brouard 8838: /* We got the gradient matrix for each theta and each state j of gradg(h]theta][j)=grad(_hp.j(theta) */
8839: trgradg =ma3x(0,nhstepm,1,nlstate,1,npar);
1.218 brouard 8840:
1.361 brouard 8841: for(h=0; h<=nhstepm; h++) /* veij */ /* computes the transposed of grad (_hp.j(theta)*/
1.218 brouard 8842: for(j=1; j<=nlstate;j++)
8843: for(theta=1; theta <=npar; theta++)
8844: trgradg[h][j][theta]=gradg[h][theta][j];
8845:
1.361 brouard 8846: for(j=nlstate+1; j<=nlstate+ndeath;j++) /* computes transposed of grad p.3 (theta)*/
1.218 brouard 8847: for(theta=1; theta <=npar; theta++)
8848: trgradgp[j][theta]=gradgp[theta][j];
1.279 brouard 8849: /**< as well as its transposed matrix
8850: */
1.218 brouard 8851:
8852: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
8853: for(i=1;i<=nlstate;i++)
8854: for(j=1;j<=nlstate;j++)
8855: vareij[i][j][(int)age] =0.;
1.279 brouard 8856:
8857: /* Computing trgradg by matcov by gradg at age and summing over h
1.361 brouard 8858: * and k (nhstepm) formula 32 of article
8859: * Lievre-Brouard-Heathcote so that for each j, computes the cov(e.j,e.k) (formula 31).
8860: * for given h and k computes trgradg[h](i,j) matcov (theta) gradg(k)(i,j) into vareij[i][j] which is
8861: cov(e.i,e.j) and sums on h and k
8862: * including the covariances.
1.279 brouard 8863: */
8864:
1.218 brouard 8865: for(h=0;h<=nhstepm;h++){
8866: for(k=0;k<=nhstepm;k++){
8867: matprod2(dnewm,trgradg[h],1,nlstate,1,npar,1,npar,matcov);
8868: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg[k]);
8869: for(i=1;i<=nlstate;i++)
8870: for(j=1;j<=nlstate;j++)
1.361 brouard 8871: vareij[i][j][(int)age] += doldm[i][j]*hf*hf; /* This is vareij=sum_h sum_k trgrad(h_pij) V(theta) grad(k_pij)
8872: including the covariances of e.j */
1.218 brouard 8873: }
8874: }
8875:
1.361 brouard 8876: /* Mortality: pptj is p.3 or p.death = trgradgp by cov by gradgp, variance of
8877: * p.3=1-p..=1-sum i p.i overall mortality computed directly because
1.279 brouard 8878: * we compute the grad (wix pijx) instead of grad (pijx),even if
1.361 brouard 8879: * wix is independent of theta.
1.279 brouard 8880: */
1.218 brouard 8881: matprod2(dnewmp,trgradgp,nlstate+1,nlstate+ndeath,1,npar,1,npar,matcov);
8882: matprod2(doldmp,dnewmp,nlstate+1,nlstate+ndeath,1,npar,nlstate+1,nlstate+ndeath,gradgp);
8883: for(j=nlstate+1;j<=nlstate+ndeath;j++)
8884: for(i=nlstate+1;i<=nlstate+ndeath;i++)
1.361 brouard 8885: varppt[j][i]=doldmp[j][i]; /* This is the variance of p.3 */
1.218 brouard 8886: /* end ppptj */
8887: /* x centered again */
8888:
1.242 brouard 8889: prevalim(prlim,nlstate,x,age,oldm,savm,ftolpl,ncvyearp,ij, nres);
1.218 brouard 8890:
8891: if (popbased==1) {
8892: if(mobilav ==0){
8893: for(i=1; i<=nlstate;i++)
8894: prlim[i][i]=probs[(int)age][i][ij];
8895: }else{ /* mobilav */
8896: for(i=1; i<=nlstate;i++)
8897: prlim[i][i]=mobaverage[(int)age][i][ij];
8898: }
8899: }
8900:
8901: /* This for computing probability of death (h=1 means
8902: computed over hstepm (estepm) matrices product = hstepm*stepm months)
8903: as a weighted average of prlim.
8904: */
1.235 brouard 8905: hpxij(p3mat,nhstepm,age,hstepm,x,nlstate,stepm,oldm,savm, ij, nres);
1.218 brouard 8906: for(j=nlstate+1;j<=nlstate+ndeath;j++){
8907: for(i=1,gmp[j]=0.;i<= nlstate; i++)
1.361 brouard 8908: gmp[j] += prlim[i][i]*p3mat[i][j][1]; /* gmp[j] is p.3 */
1.218 brouard 8909: }
8910: /* end probability of death */
8911:
8912: fprintf(ficresprobmorprev,"%3d %d ",(int) age, ij);
8913: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
1.361 brouard 8914: fprintf(ficresprobmorprev," %11.3e %11.3e",gmp[j], sqrt(varppt[j][j]));/* p.3 (STD p.3) */
1.218 brouard 8915: for(i=1; i<=nlstate;i++){
1.361 brouard 8916: fprintf(ficresprobmorprev," %11.3e %11.3e ",prlim[i][i],p3mat[i][j][1]); /* wi, pi3 */
1.218 brouard 8917: }
8918: }
8919: fprintf(ficresprobmorprev,"\n");
8920:
8921: fprintf(ficresvij,"%.0f ",age );
8922: for(i=1; i<=nlstate;i++)
8923: for(j=1; j<=nlstate;j++){
8924: fprintf(ficresvij," %.4f", vareij[i][j][(int)age]);
8925: }
8926: fprintf(ficresvij,"\n");
8927: free_matrix(gp,0,nhstepm,1,nlstate);
8928: free_matrix(gm,0,nhstepm,1,nlstate);
8929: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate);
8930: free_ma3x(trgradg,0,nhstepm,1,nlstate,1,npar);
8931: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8932: } /* End age */
8933: free_vector(gpp,nlstate+1,nlstate+ndeath);
8934: free_vector(gmp,nlstate+1,nlstate+ndeath);
8935: free_matrix(gradgp,1,npar,nlstate+1,nlstate+ndeath);
8936: free_matrix(trgradgp,nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
8937: /* fprintf(ficgp,"\nunset parametric;unset label; set ter png small size 320, 240"); */
8938: fprintf(ficgp,"\nunset parametric;unset label; set ter svg size 640, 480");
8939: /* for(j=nlstate+1; j<= nlstate+ndeath; j++){ *//* Only the first actually */
8940: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Force of mortality (year-1)\";");
8941: fprintf(ficgp,"\nset out \"%s%s.svg\";",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
8942: /* fprintf(ficgp,"\n plot \"%s\" u 1:($3*%6.3f) not w l 1 ",fileresprobmorprev,YEARM/estepm); */
8943: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)*%6.3f) t \"95\%% interval\" w l 2 ",fileresprobmorprev,YEARM/estepm); */
8944: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)*%6.3f) not w l 2 ",fileresprobmorprev,YEARM/estepm); */
8945: fprintf(ficgp,"\n plot \"%s\" u 1:($3) not w l lt 1 ",subdirf(fileresprobmorprev));
8946: fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)) t \"95%% interval\" w l lt 2 ",subdirf(fileresprobmorprev));
8947: fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)) not w l lt 2 ",subdirf(fileresprobmorprev));
8948: fprintf(fichtm,"\n<br> File (multiple files are possible if covariates are present): <A href=\"%s\">%s</a>\n",subdirf(fileresprobmorprev),subdirf(fileresprobmorprev));
8949: 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);
8950: /* fprintf(fichtm,"\n<br> Probability is computed over estepm=%d months and then divided by estepm and multiplied by %.0f in order to have the probability to die over a year <br> <img src=\"varmuptjgr%s%s.svg\"> <br>\n", stepm,YEARM,digitp,digit);
1.126 brouard 8951: */
1.218 brouard 8952: /* fprintf(ficgp,"\nset out \"varmuptjgr%s%s%s.svg\";replot;",digitp,optionfilefiname,digit); */
8953: fprintf(ficgp,"\nset out;\nset out \"%s%s.svg\";replot;set out;\n",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
1.126 brouard 8954:
1.218 brouard 8955: free_vector(xp,1,npar);
8956: free_matrix(doldm,1,nlstate,1,nlstate);
8957: free_matrix(dnewm,1,nlstate,1,npar);
8958: free_matrix(doldmp,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
8959: free_matrix(dnewmp,nlstate+1,nlstate+ndeath,1,npar);
8960: free_matrix(varppt,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
8961: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
8962: fclose(ficresprobmorprev);
8963: fflush(ficgp);
8964: fflush(fichtm);
8965: } /* end varevsij */
1.126 brouard 8966:
8967: /************ Variance of prevlim ******************/
1.269 brouard 8968: 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)
1.126 brouard 8969: {
1.205 brouard 8970: /* Variance of prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/
1.126 brouard 8971: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
1.164 brouard 8972:
1.268 brouard 8973: double **dnewmpar,**doldm;
1.126 brouard 8974: int i, j, nhstepm, hstepm;
8975: double *xp;
8976: double *gp, *gm;
8977: double **gradg, **trgradg;
1.208 brouard 8978: double **mgm, **mgp;
1.126 brouard 8979: double age,agelim;
8980: int theta;
8981:
8982: pstamp(ficresvpl);
1.288 brouard 8983: fprintf(ficresvpl,"# Standard deviation of period (forward stable) prevalences \n");
1.241 brouard 8984: fprintf(ficresvpl,"# Age ");
8985: if(nresult >=1)
8986: fprintf(ficresvpl," Result# ");
1.126 brouard 8987: for(i=1; i<=nlstate;i++)
8988: fprintf(ficresvpl," %1d-%1d",i,i);
8989: fprintf(ficresvpl,"\n");
8990:
8991: xp=vector(1,npar);
1.268 brouard 8992: dnewmpar=matrix(1,nlstate,1,npar);
1.126 brouard 8993: doldm=matrix(1,nlstate,1,nlstate);
8994:
8995: hstepm=1*YEARM; /* Every year of age */
8996: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
8997: agelim = AGESUP;
8998: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
8999: nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
9000: if (stepm >= YEARM) hstepm=1;
9001: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
9002: gradg=matrix(1,npar,1,nlstate);
1.208 brouard 9003: mgp=matrix(1,npar,1,nlstate);
9004: mgm=matrix(1,npar,1,nlstate);
1.126 brouard 9005: gp=vector(1,nlstate);
9006: gm=vector(1,nlstate);
9007:
9008: for(theta=1; theta <=npar; theta++){
9009: for(i=1; i<=npar; i++){ /* Computes gradient */
9010: xp[i] = x[i] + (i==theta ?delti[theta]:0);
9011: }
1.288 brouard 9012: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ) */
9013: /* prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres); */
9014: /* else */
9015: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
1.208 brouard 9016: for(i=1;i<=nlstate;i++){
1.126 brouard 9017: gp[i] = prlim[i][i];
1.208 brouard 9018: mgp[theta][i] = prlim[i][i];
9019: }
1.126 brouard 9020: for(i=1; i<=npar; i++) /* Computes gradient */
9021: xp[i] = x[i] - (i==theta ?delti[theta]:0);
1.288 brouard 9022: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ) */
9023: /* prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres); */
9024: /* else */
9025: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
1.208 brouard 9026: for(i=1;i<=nlstate;i++){
1.126 brouard 9027: gm[i] = prlim[i][i];
1.208 brouard 9028: mgm[theta][i] = prlim[i][i];
9029: }
1.126 brouard 9030: for(i=1;i<=nlstate;i++)
9031: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
1.209 brouard 9032: /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */
1.126 brouard 9033: } /* End theta */
9034:
9035: trgradg =matrix(1,nlstate,1,npar);
9036:
9037: for(j=1; j<=nlstate;j++)
9038: for(theta=1; theta <=npar; theta++)
9039: trgradg[j][theta]=gradg[theta][j];
1.209 brouard 9040: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
9041: /* printf("\nmgm mgp %d ",(int)age); */
9042: /* for(j=1; j<=nlstate;j++){ */
9043: /* printf(" %d ",j); */
9044: /* for(theta=1; theta <=npar; theta++) */
9045: /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */
9046: /* printf("\n "); */
9047: /* } */
9048: /* } */
9049: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
9050: /* printf("\n gradg %d ",(int)age); */
9051: /* for(j=1; j<=nlstate;j++){ */
9052: /* printf("%d ",j); */
9053: /* for(theta=1; theta <=npar; theta++) */
9054: /* printf("%d %lf ",theta,gradg[theta][j]); */
9055: /* printf("\n "); */
9056: /* } */
9057: /* } */
1.126 brouard 9058:
9059: for(i=1;i<=nlstate;i++)
9060: varpl[i][(int)age] =0.;
1.209 brouard 9061: if((int)age==79 ||(int)age== 80 ||(int)age== 81){
1.268 brouard 9062: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
9063: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
1.205 brouard 9064: }else{
1.268 brouard 9065: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
9066: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
1.205 brouard 9067: }
1.126 brouard 9068: for(i=1;i<=nlstate;i++)
9069: varpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
9070:
9071: fprintf(ficresvpl,"%.0f ",age );
1.241 brouard 9072: if(nresult >=1)
9073: fprintf(ficresvpl,"%d ",nres );
1.288 brouard 9074: for(i=1; i<=nlstate;i++){
1.126 brouard 9075: fprintf(ficresvpl," %.5f (%.5f)",prlim[i][i],sqrt(varpl[i][(int)age]));
1.288 brouard 9076: /* for(j=1;j<=nlstate;j++) */
9077: /* fprintf(ficresvpl," %d %.5f ",j,prlim[j][i]); */
9078: }
1.126 brouard 9079: fprintf(ficresvpl,"\n");
9080: free_vector(gp,1,nlstate);
9081: free_vector(gm,1,nlstate);
1.208 brouard 9082: free_matrix(mgm,1,npar,1,nlstate);
9083: free_matrix(mgp,1,npar,1,nlstate);
1.126 brouard 9084: free_matrix(gradg,1,npar,1,nlstate);
9085: free_matrix(trgradg,1,nlstate,1,npar);
9086: } /* End age */
9087:
9088: free_vector(xp,1,npar);
9089: free_matrix(doldm,1,nlstate,1,npar);
1.268 brouard 9090: free_matrix(dnewmpar,1,nlstate,1,nlstate);
9091:
9092: }
9093:
9094:
9095: /************ Variance of backprevalence limit ******************/
1.269 brouard 9096: 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)
1.268 brouard 9097: {
9098: /* Variance of backward prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/
9099: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
9100:
9101: double **dnewmpar,**doldm;
9102: int i, j, nhstepm, hstepm;
9103: double *xp;
9104: double *gp, *gm;
9105: double **gradg, **trgradg;
9106: double **mgm, **mgp;
9107: double age,agelim;
9108: int theta;
9109:
9110: pstamp(ficresvbl);
9111: fprintf(ficresvbl,"# Standard deviation of back (stable) prevalences \n");
9112: fprintf(ficresvbl,"# Age ");
9113: if(nresult >=1)
9114: fprintf(ficresvbl," Result# ");
9115: for(i=1; i<=nlstate;i++)
9116: fprintf(ficresvbl," %1d-%1d",i,i);
9117: fprintf(ficresvbl,"\n");
9118:
9119: xp=vector(1,npar);
9120: dnewmpar=matrix(1,nlstate,1,npar);
9121: doldm=matrix(1,nlstate,1,nlstate);
9122:
9123: hstepm=1*YEARM; /* Every year of age */
9124: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
9125: agelim = AGEINF;
9126: for (age=fage; age>=bage; age --){ /* If stepm=6 months */
9127: nhstepm=(int) rint((age-agelim)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
9128: if (stepm >= YEARM) hstepm=1;
9129: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
9130: gradg=matrix(1,npar,1,nlstate);
9131: mgp=matrix(1,npar,1,nlstate);
9132: mgm=matrix(1,npar,1,nlstate);
9133: gp=vector(1,nlstate);
9134: gm=vector(1,nlstate);
9135:
9136: for(theta=1; theta <=npar; theta++){
9137: for(i=1; i<=npar; i++){ /* Computes gradient */
9138: xp[i] = x[i] + (i==theta ?delti[theta]:0);
9139: }
9140: if(mobilavproj > 0 )
9141: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
9142: else
9143: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
9144: for(i=1;i<=nlstate;i++){
9145: gp[i] = bprlim[i][i];
9146: mgp[theta][i] = bprlim[i][i];
9147: }
9148: for(i=1; i<=npar; i++) /* Computes gradient */
9149: xp[i] = x[i] - (i==theta ?delti[theta]:0);
9150: if(mobilavproj > 0 )
9151: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
9152: else
9153: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
9154: for(i=1;i<=nlstate;i++){
9155: gm[i] = bprlim[i][i];
9156: mgm[theta][i] = bprlim[i][i];
9157: }
9158: for(i=1;i<=nlstate;i++)
9159: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
9160: /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */
9161: } /* End theta */
9162:
9163: trgradg =matrix(1,nlstate,1,npar);
9164:
9165: for(j=1; j<=nlstate;j++)
9166: for(theta=1; theta <=npar; theta++)
9167: trgradg[j][theta]=gradg[theta][j];
9168: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
9169: /* printf("\nmgm mgp %d ",(int)age); */
9170: /* for(j=1; j<=nlstate;j++){ */
9171: /* printf(" %d ",j); */
9172: /* for(theta=1; theta <=npar; theta++) */
9173: /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */
9174: /* printf("\n "); */
9175: /* } */
9176: /* } */
9177: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
9178: /* printf("\n gradg %d ",(int)age); */
9179: /* for(j=1; j<=nlstate;j++){ */
9180: /* printf("%d ",j); */
9181: /* for(theta=1; theta <=npar; theta++) */
9182: /* printf("%d %lf ",theta,gradg[theta][j]); */
9183: /* printf("\n "); */
9184: /* } */
9185: /* } */
9186:
9187: for(i=1;i<=nlstate;i++)
9188: varbpl[i][(int)age] =0.;
9189: if((int)age==79 ||(int)age== 80 ||(int)age== 81){
9190: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
9191: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
9192: }else{
9193: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
9194: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
9195: }
9196: for(i=1;i<=nlstate;i++)
9197: varbpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
9198:
9199: fprintf(ficresvbl,"%.0f ",age );
9200: if(nresult >=1)
9201: fprintf(ficresvbl,"%d ",nres );
9202: for(i=1; i<=nlstate;i++)
9203: fprintf(ficresvbl," %.5f (%.5f)",bprlim[i][i],sqrt(varbpl[i][(int)age]));
9204: fprintf(ficresvbl,"\n");
9205: free_vector(gp,1,nlstate);
9206: free_vector(gm,1,nlstate);
9207: free_matrix(mgm,1,npar,1,nlstate);
9208: free_matrix(mgp,1,npar,1,nlstate);
9209: free_matrix(gradg,1,npar,1,nlstate);
9210: free_matrix(trgradg,1,nlstate,1,npar);
9211: } /* End age */
9212:
9213: free_vector(xp,1,npar);
9214: free_matrix(doldm,1,nlstate,1,npar);
9215: free_matrix(dnewmpar,1,nlstate,1,nlstate);
1.126 brouard 9216:
9217: }
9218:
9219: /************ Variance of one-step probabilities ******************/
9220: void varprob(char optionfilefiname[], double **matcov, double x[], double delti[], int nlstate, double bage, double fage, int ij, int *Tvar, int **nbcode, int *ncodemax, char strstart[])
1.222 brouard 9221: {
9222: int i, j=0, k1, l1, tj;
9223: int k2, l2, j1, z1;
9224: int k=0, l;
9225: int first=1, first1, first2;
1.326 brouard 9226: int nres=0; /* New */
1.222 brouard 9227: double cv12, mu1, mu2, lc1, lc2, v12, v21, v11, v22,v1,v2, c12, tnalp;
9228: double **dnewm,**doldm;
9229: double *xp;
9230: double *gp, *gm;
9231: double **gradg, **trgradg;
9232: double **mu;
9233: double age, cov[NCOVMAX+1];
9234: double std=2.0; /* Number of standard deviation wide of confidence ellipsoids */
9235: int theta;
9236: char fileresprob[FILENAMELENGTH];
9237: char fileresprobcov[FILENAMELENGTH];
9238: char fileresprobcor[FILENAMELENGTH];
9239: double ***varpij;
9240:
9241: strcpy(fileresprob,"PROB_");
1.356 brouard 9242: strcat(fileresprob,fileresu);
1.222 brouard 9243: if((ficresprob=fopen(fileresprob,"w"))==NULL) {
9244: printf("Problem with resultfile: %s\n", fileresprob);
9245: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprob);
9246: }
9247: strcpy(fileresprobcov,"PROBCOV_");
9248: strcat(fileresprobcov,fileresu);
9249: if((ficresprobcov=fopen(fileresprobcov,"w"))==NULL) {
9250: printf("Problem with resultfile: %s\n", fileresprobcov);
9251: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcov);
9252: }
9253: strcpy(fileresprobcor,"PROBCOR_");
9254: strcat(fileresprobcor,fileresu);
9255: if((ficresprobcor=fopen(fileresprobcor,"w"))==NULL) {
9256: printf("Problem with resultfile: %s\n", fileresprobcor);
9257: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcor);
9258: }
9259: printf("Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
9260: fprintf(ficlog,"Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
9261: printf("Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
9262: fprintf(ficlog,"Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
9263: printf("and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
9264: fprintf(ficlog,"and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
9265: pstamp(ficresprob);
9266: fprintf(ficresprob,"#One-step probabilities and stand. devi in ()\n");
9267: fprintf(ficresprob,"# Age");
9268: pstamp(ficresprobcov);
9269: fprintf(ficresprobcov,"#One-step probabilities and covariance matrix\n");
9270: fprintf(ficresprobcov,"# Age");
9271: pstamp(ficresprobcor);
9272: fprintf(ficresprobcor,"#One-step probabilities and correlation matrix\n");
9273: fprintf(ficresprobcor,"# Age");
1.126 brouard 9274:
9275:
1.222 brouard 9276: for(i=1; i<=nlstate;i++)
9277: for(j=1; j<=(nlstate+ndeath);j++){
9278: fprintf(ficresprob," p%1d-%1d (SE)",i,j);
9279: fprintf(ficresprobcov," p%1d-%1d ",i,j);
9280: fprintf(ficresprobcor," p%1d-%1d ",i,j);
9281: }
9282: /* fprintf(ficresprob,"\n");
9283: fprintf(ficresprobcov,"\n");
9284: fprintf(ficresprobcor,"\n");
9285: */
9286: xp=vector(1,npar);
9287: dnewm=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
9288: doldm=matrix(1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
9289: mu=matrix(1,(nlstate)*(nlstate+ndeath), (int) bage, (int)fage);
9290: varpij=ma3x(1,nlstate*(nlstate+ndeath),1,nlstate*(nlstate+ndeath),(int) bage, (int) fage);
9291: first=1;
9292: fprintf(ficgp,"\n# Routine varprob");
9293: fprintf(fichtm,"\n<li><h4> Computing and drawing one step probabilities with their confidence intervals</h4></li>\n");
9294: fprintf(fichtm,"\n");
9295:
1.288 brouard 9296: 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);
1.222 brouard 9297: 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);
9298: fprintf(fichtmcov,"\nEllipsoids of confidence centered on point (p<inf>ij</inf>, p<inf>kl</inf>) are estimated \
1.126 brouard 9299: and drawn. It helps understanding how is the covariance between two incidences.\
9300: They are expressed in year<sup>-1</sup> in order to be less dependent of stepm.<br>\n");
1.222 brouard 9301: fprintf(fichtmcov,"\n<br> Contour plot corresponding to x'cov<sup>-1</sup>x = 4 (where x is the column vector (pij,pkl)) are drawn. \
1.126 brouard 9302: It can be understood this way: if pij and pkl where uncorrelated the (2x2) matrix of covariance \
9303: would have been (1/(var pij), 0 , 0, 1/(var pkl)), and the confidence interval would be 2 \
9304: standard deviations wide on each axis. <br>\
9305: Now, if both incidences are correlated (usual case) we diagonalised the inverse of the covariance matrix\
9306: and made the appropriate rotation to look at the uncorrelated principal directions.<br>\
9307: To be simple, these graphs help to understand the significativity of each parameter in relation to a second other one.<br> \n");
9308:
1.222 brouard 9309: cov[1]=1;
9310: /* tj=cptcoveff; */
1.225 brouard 9311: tj = (int) pow(2,cptcoveff);
1.222 brouard 9312: if (cptcovn<1) {tj=1;ncodemax[1]=1;}
9313: j1=0;
1.332 brouard 9314:
9315: for(nres=1;nres <=nresult; nres++){ /* For each resultline */
9316: for(j1=1; j1<=tj;j1++){ /* For any combination of dummy covariates, fixed and varying */
1.342 brouard 9317: /* 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); */
1.332 brouard 9318: if(tj != 1 && TKresult[nres]!= j1)
9319: continue;
9320:
9321: /* for(j1=1; j1<=tj;j1++){ /\* For each valid combination of covariates or only once*\/ */
9322: /* for(nres=1;nres <=1; nres++){ /\* For each resultline *\/ */
9323: /* /\* for(nres=1;nres <=nresult; nres++){ /\\* For each resultline *\\/ *\/ */
1.222 brouard 9324: if (cptcovn>0) {
1.334 brouard 9325: fprintf(ficresprob, "\n#********** Variable ");
9326: fprintf(ficresprobcov, "\n#********** Variable ");
9327: fprintf(ficgp, "\n#********** Variable ");
9328: fprintf(fichtmcov, "\n<hr size=\"2\" color=\"#EC5E5E\">********** Variable ");
9329: fprintf(ficresprobcor, "\n#********** Variable ");
9330:
9331: /* Including quantitative variables of the resultline to be done */
9332: for (z1=1; z1<=cptcovs; z1++){ /* Loop on each variable of this resultline */
1.343 brouard 9333: /* printf("Varprob modelresult[%d][%d]=%d model=1+age+%s \n",nres, z1, modelresult[nres][z1], model); */
1.338 brouard 9334: fprintf(ficlog,"Varprob modelresult[%d][%d]=%d model=1+age+%s \n",nres, z1, modelresult[nres][z1], model);
9335: /* fprintf(ficlog,"Varprob modelresult[%d][%d]=%d model=1+age+%s resultline[%d]=%s \n",nres, z1, modelresult[nres][z1], model, nres, resultline[nres]); */
1.334 brouard 9336: if(Dummy[modelresult[nres][z1]]==0){/* Dummy variable of the variable in position modelresult in the model corresponding to z1 in resultline */
9337: if(Fixed[modelresult[nres][z1]]==0){ /* Fixed referenced to model equation */
9338: 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 */
9339: 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 */
9340: 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 */
9341: 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 */
9342: 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 */
9343: fprintf(ficresprob,"fixed ");
9344: fprintf(ficresprobcov,"fixed ");
9345: fprintf(ficgp,"fixed ");
9346: fprintf(fichtmcov,"fixed ");
9347: fprintf(ficresprobcor,"fixed ");
9348: }else{
9349: fprintf(ficresprob,"varyi ");
9350: fprintf(ficresprobcov,"varyi ");
9351: fprintf(ficgp,"varyi ");
9352: fprintf(fichtmcov,"varyi ");
9353: fprintf(ficresprobcor,"varyi ");
9354: }
9355: }else if(Dummy[modelresult[nres][z1]]==1){ /* Quanti variable */
9356: /* For each selected (single) quantitative value */
1.337 brouard 9357: fprintf(ficresprob," V%d=%lg ",Tvqresult[nres][z1],Tqresult[nres][z1]);
1.334 brouard 9358: if(Fixed[modelresult[nres][z1]]==0){ /* Fixed */
9359: fprintf(ficresprob,"fixed ");
9360: fprintf(ficresprobcov,"fixed ");
9361: fprintf(ficgp,"fixed ");
9362: fprintf(fichtmcov,"fixed ");
9363: fprintf(ficresprobcor,"fixed ");
9364: }else{
9365: fprintf(ficresprob,"varyi ");
9366: fprintf(ficresprobcov,"varyi ");
9367: fprintf(ficgp,"varyi ");
9368: fprintf(fichtmcov,"varyi ");
9369: fprintf(ficresprobcor,"varyi ");
9370: }
9371: }else{
9372: 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 */
9373: 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 */
9374: exit(1);
9375: }
9376: } /* End loop on variable of this resultline */
9377: /* for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprob, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]); */
1.222 brouard 9378: fprintf(ficresprob, "**********\n#\n");
9379: fprintf(ficresprobcov, "**********\n#\n");
9380: fprintf(ficgp, "**********\n#\n");
9381: fprintf(fichtmcov, "**********\n<hr size=\"2\" color=\"#EC5E5E\">");
9382: fprintf(ficresprobcor, "**********\n#");
9383: if(invalidvarcomb[j1]){
9384: fprintf(ficgp,"\n#Combination (%d) ignored because no cases \n",j1);
9385: fprintf(fichtmcov,"\n<h3>Combination (%d) ignored because no cases </h3>\n",j1);
9386: continue;
9387: }
9388: }
9389: gradg=matrix(1,npar,1,(nlstate)*(nlstate+ndeath));
9390: trgradg=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
9391: gp=vector(1,(nlstate)*(nlstate+ndeath));
9392: gm=vector(1,(nlstate)*(nlstate+ndeath));
1.334 brouard 9393: for (age=bage; age<=fage; age ++){ /* Fo each age we feed the model equation with covariates, using precov as in hpxij() ? */
1.222 brouard 9394: cov[2]=age;
9395: if(nagesqr==1)
9396: cov[3]= age*age;
1.334 brouard 9397: /* New code end of combination but for each resultline */
9398: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
1.349 brouard 9399: if(Typevar[k1]==1 || Typevar[k1] ==3){ /* A product with age */
1.334 brouard 9400: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
1.326 brouard 9401: }else{
1.334 brouard 9402: cov[2+nagesqr+k1]=precov[nres][k1];
1.326 brouard 9403: }
1.334 brouard 9404: }/* End of loop on model equation */
9405: /* Old code */
9406: /* /\* for (k=1; k<=cptcovn;k++) { *\/ */
9407: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,k)]; *\/ */
9408: /* for (k=1; k<=nsd;k++) { /\* For single dummy covariates only *\/ */
9409: /* /\* Here comes the value of the covariate 'j1' after renumbering k with single dummy covariates *\/ */
9410: /* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(j1,TnsdVar[TvarsD[k]])]; */
9411: /* /\*cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,Tvar[k])];*\//\* j1 1 2 3 4 */
9412: /* * 1 1 1 1 1 */
9413: /* * 2 2 1 1 1 */
9414: /* * 3 1 2 1 1 */
9415: /* *\/ */
9416: /* /\* nbcode[1][1]=0 nbcode[1][2]=1;*\/ */
9417: /* } */
9418: /* /\* V2+V1+V4+V3*age Tvar[4]=3 ; V1+V2*age Tvar[2]=2; V1+V1*age Tvar[2]=1, Tage[1]=2 *\/ */
9419: /* /\* ) p nbcode[Tvar[Tage[k]]][(1 & (ij-1) >> (k-1))+1] *\/ */
9420: /* /\*for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; *\/ */
9421: /* for (k=1; k<=cptcovage;k++){ /\* For product with age *\/ */
9422: /* if(Dummy[Tage[k]]==2){ /\* dummy with age *\/ */
9423: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(j1,TnsdVar[Tvar[Tage[k]]])]*cov[2]; */
9424: /* /\* cov[++k1]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; *\/ */
9425: /* } else if(Dummy[Tage[k]]==3){ /\* quantitative with age *\/ */
9426: /* 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]); */
9427: /* /\* cov[2+nagesqr+Tage[k]]=meanq[k]/idq[k]*cov[2];/\\* Using the mean of quantitative variable Tvar[Tage[k]] /\\* Tqresult[nres][k]; *\\/ *\/ */
9428: /* /\* exit(1); *\/ */
9429: /* /\* cov[++k1]=Tqresult[nres][k]; *\/ */
9430: /* } */
9431: /* /\* cov[2+Tage[k]+nagesqr]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; *\/ */
9432: /* } */
9433: /* for (k=1; k<=cptcovprod;k++){/\* For product without age *\/ */
9434: /* if(Dummy[Tvard[k][1]]==0){ */
9435: /* if(Dummy[Tvard[k][2]]==0){ */
9436: /* 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]])]; */
9437: /* /\* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
9438: /* }else{ /\* Should we use the mean of the quantitative variables? *\/ */
9439: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(j1,TnsdVar[Tvard[k][1]])] * Tqresult[nres][resultmodel[nres][k]]; */
9440: /* /\* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * Tqresult[nres][k]; *\/ */
9441: /* } */
9442: /* }else{ */
9443: /* if(Dummy[Tvard[k][2]]==0){ */
9444: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(j1,TnsdVar[Tvard[k][2]])] * Tqinvresult[nres][TnsdVar[Tvard[k][1]]]; */
9445: /* /\* cov[++k1]=nbcode[Tvard[k][2]][codtabm(ij,k)] * Tqinvresult[nres][Tvard[k][1]]; *\/ */
9446: /* }else{ */
9447: /* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][TnsdVar[Tvard[k][1]]]* Tqinvresult[nres][TnsdVar[Tvard[k][2]]]; */
9448: /* /\* cov[++k1]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; *\/ */
9449: /* } */
9450: /* } */
9451: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
9452: /* } */
1.326 brouard 9453: /* For each age and combination of dummy covariates we slightly move the parameters of delti in order to get the gradient*/
1.222 brouard 9454: for(theta=1; theta <=npar; theta++){
9455: for(i=1; i<=npar; i++)
9456: xp[i] = x[i] + (i==theta ?delti[theta]:(double)0);
1.220 brouard 9457:
1.222 brouard 9458: pmij(pmmij,cov,ncovmodel,xp,nlstate);
1.220 brouard 9459:
1.222 brouard 9460: k=0;
9461: for(i=1; i<= (nlstate); i++){
9462: for(j=1; j<=(nlstate+ndeath);j++){
9463: k=k+1;
9464: gp[k]=pmmij[i][j];
9465: }
9466: }
1.220 brouard 9467:
1.222 brouard 9468: for(i=1; i<=npar; i++)
9469: xp[i] = x[i] - (i==theta ?delti[theta]:(double)0);
1.220 brouard 9470:
1.222 brouard 9471: pmij(pmmij,cov,ncovmodel,xp,nlstate);
9472: k=0;
9473: for(i=1; i<=(nlstate); i++){
9474: for(j=1; j<=(nlstate+ndeath);j++){
9475: k=k+1;
9476: gm[k]=pmmij[i][j];
9477: }
9478: }
1.220 brouard 9479:
1.222 brouard 9480: for(i=1; i<= (nlstate)*(nlstate+ndeath); i++)
9481: gradg[theta][i]=(gp[i]-gm[i])/(double)2./delti[theta];
9482: }
1.126 brouard 9483:
1.222 brouard 9484: for(j=1; j<=(nlstate)*(nlstate+ndeath);j++)
9485: for(theta=1; theta <=npar; theta++)
9486: trgradg[j][theta]=gradg[theta][j];
1.220 brouard 9487:
1.222 brouard 9488: matprod2(dnewm,trgradg,1,(nlstate)*(nlstate+ndeath),1,npar,1,npar,matcov);
9489: matprod2(doldm,dnewm,1,(nlstate)*(nlstate+ndeath),1,npar,1,(nlstate)*(nlstate+ndeath),gradg);
1.220 brouard 9490:
1.222 brouard 9491: pmij(pmmij,cov,ncovmodel,x,nlstate);
1.220 brouard 9492:
1.222 brouard 9493: k=0;
9494: for(i=1; i<=(nlstate); i++){
9495: for(j=1; j<=(nlstate+ndeath);j++){
9496: k=k+1;
9497: mu[k][(int) age]=pmmij[i][j];
9498: }
9499: }
9500: for(i=1;i<=(nlstate)*(nlstate+ndeath);i++)
9501: for(j=1;j<=(nlstate)*(nlstate+ndeath);j++)
9502: varpij[i][j][(int)age] = doldm[i][j];
1.220 brouard 9503:
1.222 brouard 9504: /*printf("\n%d ",(int)age);
9505: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
9506: printf("%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
9507: fprintf(ficlog,"%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
9508: }*/
1.220 brouard 9509:
1.222 brouard 9510: fprintf(ficresprob,"\n%d ",(int)age);
9511: fprintf(ficresprobcov,"\n%d ",(int)age);
9512: fprintf(ficresprobcor,"\n%d ",(int)age);
1.220 brouard 9513:
1.222 brouard 9514: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++)
9515: fprintf(ficresprob,"%11.3e (%11.3e) ",mu[i][(int) age],sqrt(varpij[i][i][(int)age]));
9516: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
9517: fprintf(ficresprobcov,"%11.3e ",mu[i][(int) age]);
9518: fprintf(ficresprobcor,"%11.3e ",mu[i][(int) age]);
9519: }
9520: i=0;
9521: for (k=1; k<=(nlstate);k++){
9522: for (l=1; l<=(nlstate+ndeath);l++){
9523: i++;
9524: fprintf(ficresprobcov,"\n%d %d-%d",(int)age,k,l);
9525: fprintf(ficresprobcor,"\n%d %d-%d",(int)age,k,l);
9526: for (j=1; j<=i;j++){
9527: /* printf(" k=%d l=%d i=%d j=%d\n",k,l,i,j);fflush(stdout); */
9528: fprintf(ficresprobcov," %11.3e",varpij[i][j][(int)age]);
9529: fprintf(ficresprobcor," %11.3e",varpij[i][j][(int) age]/sqrt(varpij[i][i][(int) age])/sqrt(varpij[j][j][(int)age]));
9530: }
9531: }
9532: }/* end of loop for state */
9533: } /* end of loop for age */
9534: free_vector(gp,1,(nlstate+ndeath)*(nlstate+ndeath));
9535: free_vector(gm,1,(nlstate+ndeath)*(nlstate+ndeath));
9536: free_matrix(trgradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
9537: free_matrix(gradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
9538:
9539: /* Confidence intervalle of pij */
9540: /*
9541: fprintf(ficgp,"\nunset parametric;unset label");
9542: fprintf(ficgp,"\nset log y;unset log x; set xlabel \"Age\";set ylabel \"probability (year-1)\"");
9543: fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65");
9544: 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);
9545: fprintf(fichtm,"\n<br><img src=\"pijgr%s.png\"> ",optionfilefiname);
9546: fprintf(ficgp,"\nset out \"pijgr%s.png\"",optionfilefiname);
9547: fprintf(ficgp,"\nplot \"%s\" every :::%d::%d u 1:2 \"\%%lf",k1,k2,xfilevarprob);
9548: */
9549:
9550: /* Drawing ellipsoids of confidence of two variables p(k1-l1,k2-l2)*/
9551: first1=1;first2=2;
9552: for (k2=1; k2<=(nlstate);k2++){
9553: for (l2=1; l2<=(nlstate+ndeath);l2++){
9554: if(l2==k2) continue;
9555: j=(k2-1)*(nlstate+ndeath)+l2;
9556: for (k1=1; k1<=(nlstate);k1++){
9557: for (l1=1; l1<=(nlstate+ndeath);l1++){
9558: if(l1==k1) continue;
9559: i=(k1-1)*(nlstate+ndeath)+l1;
9560: if(i<=j) continue;
9561: for (age=bage; age<=fage; age ++){
9562: if ((int)age %5==0){
9563: v1=varpij[i][i][(int)age]/stepm*YEARM/stepm*YEARM;
9564: v2=varpij[j][j][(int)age]/stepm*YEARM/stepm*YEARM;
9565: cv12=varpij[i][j][(int)age]/stepm*YEARM/stepm*YEARM;
9566: mu1=mu[i][(int) age]/stepm*YEARM ;
9567: mu2=mu[j][(int) age]/stepm*YEARM;
9568: c12=cv12/sqrt(v1*v2);
9569: /* Computing eigen value of matrix of covariance */
9570: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
9571: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
9572: if ((lc2 <0) || (lc1 <0) ){
9573: if(first2==1){
9574: first1=0;
9575: 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);
9576: }
9577: 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);
9578: /* lc1=fabs(lc1); */ /* If we want to have them positive */
9579: /* lc2=fabs(lc2); */
9580: }
1.220 brouard 9581:
1.222 brouard 9582: /* Eigen vectors */
1.280 brouard 9583: if(1+(v1-lc1)*(v1-lc1)/cv12/cv12 <1.e-5){
9584: printf(" Error sqrt of a negative number: %lf\n",1+(v1-lc1)*(v1-lc1)/cv12/cv12);
9585: fprintf(ficlog," Error sqrt of a negative number: %lf\n",1+(v1-lc1)*(v1-lc1)/cv12/cv12);
9586: v11=(1./sqrt(fabs(1+(v1-lc1)*(v1-lc1)/cv12/cv12)));
9587: }else
9588: v11=(1./sqrt(1+(v1-lc1)*(v1-lc1)/cv12/cv12));
1.222 brouard 9589: /*v21=sqrt(1.-v11*v11); *//* error */
9590: v21=(lc1-v1)/cv12*v11;
9591: v12=-v21;
9592: v22=v11;
9593: tnalp=v21/v11;
9594: if(first1==1){
9595: first1=0;
9596: 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);
9597: }
9598: 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);
9599: /*printf(fignu*/
9600: /* mu1+ v11*lc1*cost + v12*lc2*sin(t) */
9601: /* mu2+ v21*lc1*cost + v22*lc2*sin(t) */
9602: if(first==1){
9603: first=0;
9604: fprintf(ficgp,"\n# Ellipsoids of confidence\n#\n");
9605: fprintf(ficgp,"\nset parametric;unset label");
9606: 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);
9607: fprintf(ficgp,"\nset ter svg size 640, 480");
1.266 brouard 9608: fprintf(fichtmcov,"\n<p><br>Ellipsoids of confidence cov(p%1d%1d,p%1d%1d) expressed in year<sup>-1</sup>\
1.220 brouard 9609: :<a href=\"%s_%d%1d%1d-%1d%1d.svg\"> \
1.201 brouard 9610: %s_%d%1d%1d-%1d%1d.svg</A>, ",k1,l1,k2,l2,\
1.222 brouard 9611: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2, \
9612: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
9613: fprintf(fichtmcov,"\n<br><img src=\"%s_%d%1d%1d-%1d%1d.svg\"> ",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
9614: fprintf(fichtmcov,"\n<br> Correlation at age %d (%.3f),",(int) age, c12);
9615: fprintf(ficgp,"\nset out \"%s_%d%1d%1d-%1d%1d.svg\"",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
9616: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
9617: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
9618: 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", \
1.280 brouard 9619: mu1,std,v11,sqrt(fabs(lc1)),v12,sqrt(fabs(lc2)), \
9620: mu2,std,v21,sqrt(fabs(lc1)),v22,sqrt(fabs(lc2))); /* For gnuplot only */
1.222 brouard 9621: }else{
9622: first=0;
9623: fprintf(fichtmcov," %d (%.3f),",(int) age, c12);
9624: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
9625: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
9626: 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", \
1.266 brouard 9627: mu1,std,v11,sqrt(lc1),v12,sqrt(fabs(lc2)), \
9628: mu2,std,v21,sqrt(lc1),v22,sqrt(fabs(lc2)));
1.222 brouard 9629: }/* if first */
9630: } /* age mod 5 */
9631: } /* end loop age */
9632: fprintf(ficgp,"\nset out;\nset out \"%s_%d%1d%1d-%1d%1d.svg\";replot;set out;",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
9633: first=1;
9634: } /*l12 */
9635: } /* k12 */
9636: } /*l1 */
9637: }/* k1 */
1.332 brouard 9638: } /* loop on combination of covariates j1 */
1.326 brouard 9639: } /* loop on nres */
1.222 brouard 9640: free_ma3x(varpij,1,nlstate,1,nlstate+ndeath,(int) bage, (int)fage);
9641: free_matrix(mu,1,(nlstate+ndeath)*(nlstate+ndeath),(int) bage, (int)fage);
9642: free_matrix(doldm,1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
9643: free_matrix(dnewm,1,(nlstate)*(nlstate+ndeath),1,npar);
9644: free_vector(xp,1,npar);
9645: fclose(ficresprob);
9646: fclose(ficresprobcov);
9647: fclose(ficresprobcor);
9648: fflush(ficgp);
9649: fflush(fichtmcov);
9650: }
1.126 brouard 9651:
9652:
9653: /******************* Printing html file ***********/
1.201 brouard 9654: void printinghtml(char fileresu[], char title[], char datafile[], int firstpass, \
1.126 brouard 9655: int lastpass, int stepm, int weightopt, char model[],\
9656: int imx,int jmin, int jmax, double jmeanint,char rfileres[],\
1.296 brouard 9657: int popforecast, int mobilav, int prevfcast, int mobilavproj, int prevbcast, int estepm , \
9658: double jprev1, double mprev1,double anprev1, double dateprev1, double dateprojd, double dateback1, \
9659: double jprev2, double mprev2,double anprev2, double dateprev2, double dateprojf, double dateback2){
1.359 brouard 9660: int jj1, k1, cpt, nres;
1.319 brouard 9661: /* In fact some results are already printed in fichtm which is open */
1.126 brouard 9662: fprintf(fichtm,"<ul><li><a href='#firstorder'>Result files (first order: no variance)</a>\n \
9663: <li><a href='#secondorder'>Result files (second order (variance)</a>\n \
9664: </ul>");
1.319 brouard 9665: /* fprintf(fichtm,"<ul><li> model=1+age+%s\n \ */
9666: /* </ul>", model); */
1.214 brouard 9667: fprintf(fichtm,"<ul><li><h4><a name='firstorder'>Result files (first order: no variance)</a></h4>\n");
9668: 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",
9669: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTMFR_",".htm"),subdirfext3(optionfilefiname,"PHTMFR_",".htm"));
1.332 brouard 9670: 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) ",
1.213 brouard 9671: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTM_",".htm"),subdirfext3(optionfilefiname,"PHTM_",".htm"));
9672: fprintf(fichtm,", <a href=\"%s\">%s</a> (text file) <br>\n",subdirf2(fileresu,"P_"),subdirf2(fileresu,"P_"));
1.126 brouard 9673: fprintf(fichtm,"\
9674: - Estimated transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
1.201 brouard 9675: stepm,subdirf2(fileresu,"PIJ_"),subdirf2(fileresu,"PIJ_"));
1.126 brouard 9676: fprintf(fichtm,"\
1.217 brouard 9677: - Estimated back transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
9678: stepm,subdirf2(fileresu,"PIJB_"),subdirf2(fileresu,"PIJB_"));
9679: fprintf(fichtm,"\
1.288 brouard 9680: - Period (forward) prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
1.201 brouard 9681: subdirf2(fileresu,"PL_"),subdirf2(fileresu,"PL_"));
1.126 brouard 9682: fprintf(fichtm,"\
1.288 brouard 9683: - Backward prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
1.217 brouard 9684: subdirf2(fileresu,"PLB_"),subdirf2(fileresu,"PLB_"));
9685: fprintf(fichtm,"\
1.211 brouard 9686: - (a) Life expectancies by health status at initial age, e<sub>i.</sub> (b) health expectancies by health status at initial age, e<sub>ij</sub> . If one or more covariates are included, specific tables for each value of the covariate are output in sequences within the same file (estepm=%2d months): \
1.126 brouard 9687: <a href=\"%s\">%s</a> <br>\n",
1.201 brouard 9688: estepm,subdirf2(fileresu,"E_"),subdirf2(fileresu,"E_"));
1.211 brouard 9689: if(prevfcast==1){
9690: fprintf(fichtm,"\
9691: - Prevalence projections by age and states: \
1.201 brouard 9692: <a href=\"%s\">%s</a> <br>\n</li>", subdirf2(fileresu,"F_"),subdirf2(fileresu,"F_"));
1.211 brouard 9693: }
1.126 brouard 9694:
9695:
1.225 brouard 9696: m=pow(2,cptcoveff);
1.222 brouard 9697: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
1.126 brouard 9698:
1.317 brouard 9699: fprintf(fichtm," \n<ul><li><b>Graphs (first order)</b></li><p>");
1.264 brouard 9700:
9701: jj1=0;
9702:
9703: fprintf(fichtm," \n<ul>");
1.337 brouard 9704: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
9705: /* k1=nres; */
1.338 brouard 9706: k1=TKresult[nres];
9707: if(TKresult[nres]==0)k1=1; /* To be checked for no result */
1.337 brouard 9708: /* for(k1=1; k1<=m;k1++){ /\* For each combination of covariate *\/ */
9709: /* if(m != 1 && TKresult[nres]!= k1) */
9710: /* continue; */
1.264 brouard 9711: jj1++;
9712: if (cptcovn > 0) {
9713: fprintf(fichtm,"\n<li><a size=\"1\" color=\"#EC5E5E\" href=\"#rescov");
1.337 brouard 9714: for (cpt=1; cpt<=cptcovs;cpt++){ /**< cptcovs number of SIMPLE covariates in the model V2+V1 =2 (dummy or quantit or time varying) */
9715: fprintf(fichtm,"_V%d=%lg_",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
1.264 brouard 9716: }
1.337 brouard 9717: /* for (cpt=1; cpt<=cptcoveff;cpt++){ */
9718: /* fprintf(fichtm,"_V%d=%d_",Tvresult[nres][cpt],(int)Tresult[nres][cpt]); */
9719: /* } */
9720: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
9721: /* fprintf(fichtm,"_V%d=%f_",Tvqresult[nres][k4],Tqresult[nres][k4]); */
9722: /* } */
1.264 brouard 9723: fprintf(fichtm,"\">");
9724:
9725: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
9726: fprintf(fichtm,"************ Results for covariates");
1.337 brouard 9727: for (cpt=1; cpt<=cptcovs;cpt++){
9728: fprintf(fichtm," V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
1.264 brouard 9729: }
1.337 brouard 9730: /* fprintf(fichtm,"************ Results for covariates"); */
9731: /* for (cpt=1; cpt<=cptcoveff;cpt++){ */
9732: /* fprintf(fichtm," V%d=%d ",Tvresult[nres][cpt],(int)Tresult[nres][cpt]); */
9733: /* } */
9734: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
9735: /* fprintf(fichtm," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
9736: /* } */
1.264 brouard 9737: if(invalidvarcomb[k1]){
9738: fprintf(fichtm," Warning Combination (%d) ignored because no cases ",k1);
9739: continue;
9740: }
9741: fprintf(fichtm,"</a></li>");
9742: } /* cptcovn >0 */
9743: }
1.317 brouard 9744: fprintf(fichtm," \n</ul>");
1.264 brouard 9745:
1.222 brouard 9746: jj1=0;
1.237 brouard 9747:
1.337 brouard 9748: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
9749: /* k1=nres; */
1.338 brouard 9750: k1=TKresult[nres];
9751: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.337 brouard 9752: /* for(k1=1; k1<=m;k1++){ /\* For each combination of covariate *\/ */
9753: /* if(m != 1 && TKresult[nres]!= k1) */
9754: /* continue; */
1.220 brouard 9755:
1.222 brouard 9756: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
9757: jj1++;
9758: if (cptcovn > 0) {
1.264 brouard 9759: fprintf(fichtm,"\n<p><a name=\"rescov");
1.337 brouard 9760: for (cpt=1; cpt<=cptcovs;cpt++){
9761: fprintf(fichtm,"_V%d=%lg_",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
1.264 brouard 9762: }
1.337 brouard 9763: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
9764: /* fprintf(fichtm,"_V%d=%f_",Tvqresult[nres][k4],Tqresult[nres][k4]); */
9765: /* } */
1.264 brouard 9766: fprintf(fichtm,"\"</a>");
9767:
1.222 brouard 9768: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
1.337 brouard 9769: for (cpt=1; cpt<=cptcovs;cpt++){
9770: fprintf(fichtm," V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
9771: printf(" V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
1.237 brouard 9772: /* fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]); */
9773: /* printf(" V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]);fflush(stdout); */
1.222 brouard 9774: }
1.230 brouard 9775: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
1.338 brouard 9776: fprintf(fichtm," (model=1+age+%s) ************\n<hr size=\"2\" color=\"#EC5E5E\">",model);
1.222 brouard 9777: if(invalidvarcomb[k1]){
9778: fprintf(fichtm,"\n<h3>Combination (%d) ignored because no cases </h3>\n",k1);
9779: printf("\nCombination (%d) ignored because no cases \n",k1);
9780: continue;
9781: }
9782: }
9783: /* aij, bij */
1.259 brouard 9784: 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> \
1.241 brouard 9785: <img src=\"%s_%d-1-%d.svg\">",model,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres);
1.222 brouard 9786: /* Pij */
1.241 brouard 9787: 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> \
9788: <img src=\"%s_%d-2-%d.svg\">",stepm,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres);
1.222 brouard 9789: /* Quasi-incidences */
9790: fprintf(fichtm,"<br>\n- I<sub>ij</sub> or Conditional probabilities to be observed in state j being in state i %d (stepm) months\
1.220 brouard 9791: before but expressed in per year i.e. quasi incidences if stepm is small and probabilities too, \
1.211 brouard 9792: incidence (rates) are the limit when h tends to zero of the ratio of the probability <sub>h</sub>P<sub>ij</sub> \
1.241 brouard 9793: 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> \
9794: <img src=\"%s_%d-3-%d.svg\">",stepm,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres);
1.222 brouard 9795: /* Survival functions (period) in state j */
9796: for(cpt=1; cpt<=nlstate;cpt++){
1.359 brouard 9797: fprintf(fichtm,"<br>\n- Survival functions in state %d. And probability to be observed in state %d being in state (1 to %d) at different ages. Mean times spent in state (or Life Expectancy or Health Expectancy etc.) are the areas under each curve. <a href=\"%s_%d-%d-%d.svg\">%s_%d-%d-%d.svg</a><br>", cpt, cpt, nlstate, subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres,subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres);
1.329 brouard 9798: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"PIJ_"),subdirf2(optionfilefiname,"PIJ_"));
9799: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres);
1.222 brouard 9800: }
9801: /* State specific survival functions (period) */
9802: for(cpt=1; cpt<=nlstate;cpt++){
1.292 brouard 9803: fprintf(fichtm,"<br>\n- Survival functions in state %d and in any other live state (total).\
1.359 brouard 9804: And probability to be observed in various states (up to %d) being in state %d at different ages. Mean times spent in state (or Life Expectancy or Health Expectancy etc.) are the areas under each curve. \
1.329 brouard 9805: <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);
9806: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"PIJ_"),subdirf2(optionfilefiname,"PIJ_"));
9807: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",subdirf2(optionfilefiname,"LIJT_"),cpt,k1,nres);
1.222 brouard 9808: }
1.288 brouard 9809: /* Period (forward stable) prevalence in each health state */
1.222 brouard 9810: for(cpt=1; cpt<=nlstate;cpt++){
1.359 brouard 9811: fprintf(fichtm,"<br>\n- Convergence to period (stable) prevalence in state %d. Or probability for a person being in state (1 to %d) at different ages, to be alive in state %d some years after. <a href=\"%s_%d-%d-%d.svg\">%s_%d-%d-%d.svg</a><br>", cpt, nlstate, cpt, subdirf2(optionfilefiname,"P_"),cpt,k1,nres,subdirf2(optionfilefiname,"P_"),cpt,k1,nres);
1.338 brouard 9812: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"PIJ_"),subdirf2(optionfilefiname,"PIJ_"));
1.329 brouard 9813: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">" ,subdirf2(optionfilefiname,"P_"),cpt,k1,nres);
1.222 brouard 9814: }
1.296 brouard 9815: if(prevbcast==1){
1.288 brouard 9816: /* Backward prevalence in each health state */
1.222 brouard 9817: for(cpt=1; cpt<=nlstate;cpt++){
1.338 brouard 9818: 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);
9819: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"PIJB_"),subdirf2(optionfilefiname,"PIJB_"));
9820: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">" ,subdirf2(optionfilefiname,"PB_"),cpt,k1,nres);
1.222 brouard 9821: }
1.217 brouard 9822: }
1.222 brouard 9823: if(prevfcast==1){
1.288 brouard 9824: /* Projection of prevalence up to period (forward stable) prevalence in each health state */
1.222 brouard 9825: for(cpt=1; cpt<=nlstate;cpt++){
1.314 brouard 9826: 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);
9827: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"F_"),subdirf2(optionfilefiname,"F_"));
9828: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",
9829: subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres);
1.222 brouard 9830: }
9831: }
1.296 brouard 9832: if(prevbcast==1){
1.268 brouard 9833: /* Back projection of prevalence up to stable (mixed) back-prevalence in each health state */
9834: for(cpt=1; cpt<=nlstate;cpt++){
1.273 brouard 9835: fprintf(fichtm,"<br>\n- Back projection of cross-sectional prevalence (estimated with cases observed from %.1f to %.1f and mobil_average=%d), \
1.359 brouard 9836: 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 \
9837: 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) \
1.314 brouard 9838: 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);
9839: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"FB_"),subdirf2(optionfilefiname,"FB_"));
9840: fprintf(fichtm," <img src=\"%s_%d-%d-%d.svg\">", subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres);
1.268 brouard 9841: }
9842: }
1.220 brouard 9843:
1.222 brouard 9844: for(cpt=1; cpt<=nlstate;cpt++) {
1.314 brouard 9845: 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);
9846: fprintf(fichtm," (data from text file <a href=\"%s.txt\"> %s.txt</a>)\n<br>",subdirf2(optionfilefiname,"E_"),subdirf2(optionfilefiname,"E_"));
9847: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">", subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres );
1.222 brouard 9848: }
9849: /* } /\* end i1 *\/ */
1.337 brouard 9850: }/* End k1=nres */
1.222 brouard 9851: fprintf(fichtm,"</ul>");
1.126 brouard 9852:
1.222 brouard 9853: fprintf(fichtm,"\
1.126 brouard 9854: \n<br><li><h4> <a name='secondorder'>Result files (second order: variances)</a></h4>\n\
1.193 brouard 9855: - Parameter file with estimated parameters and covariance matrix: <a href=\"%s\">%s</a> <br> \
1.203 brouard 9856: - 95%% confidence intervals and Wald tests of the estimated parameters are in the log file if optimization has been done (mle != 0).<br> \
1.197 brouard 9857: But because parameters are usually highly correlated (a higher incidence of disability \
9858: and a higher incidence of recovery can give very close observed transition) it might \
9859: be very useful to look not only at linear confidence intervals estimated from the \
9860: variances but at the covariance matrix. And instead of looking at the estimated coefficients \
9861: (parameters) of the logistic regression, it might be more meaningful to visualize the \
9862: covariance matrix of the one-step probabilities. \
9863: See page 'Matrix of variance-covariance of one-step probabilities' below. \n", rfileres,rfileres);
1.126 brouard 9864:
1.222 brouard 9865: fprintf(fichtm," - Standard deviation of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
9866: subdirf2(fileresu,"PROB_"),subdirf2(fileresu,"PROB_"));
9867: fprintf(fichtm,"\
1.126 brouard 9868: - Variance-covariance of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
1.222 brouard 9869: subdirf2(fileresu,"PROBCOV_"),subdirf2(fileresu,"PROBCOV_"));
1.126 brouard 9870:
1.222 brouard 9871: fprintf(fichtm,"\
1.126 brouard 9872: - Correlation matrix of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
1.222 brouard 9873: subdirf2(fileresu,"PROBCOR_"),subdirf2(fileresu,"PROBCOR_"));
9874: fprintf(fichtm,"\
1.126 brouard 9875: - 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): \
9876: <a href=\"%s\">%s</a> <br>\n</li>",
1.201 brouard 9877: estepm,subdirf2(fileresu,"CVE_"),subdirf2(fileresu,"CVE_"));
1.222 brouard 9878: fprintf(fichtm,"\
1.126 brouard 9879: - (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): \
9880: <a href=\"%s\">%s</a> <br>\n</li>",
1.201 brouard 9881: estepm,subdirf2(fileresu,"STDE_"),subdirf2(fileresu,"STDE_"));
1.222 brouard 9882: fprintf(fichtm,"\
1.288 brouard 9883: - 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",
1.222 brouard 9884: estepm, subdirf2(fileresu,"V_"),subdirf2(fileresu,"V_"));
9885: fprintf(fichtm,"\
1.128 brouard 9886: - Total life expectancy and total health expectancies to be spent in each health state e<sup>.j</sup> with their standard errors (if popbased=1, an additional computation is done using the cross-sectional prevalences, i.e population based) (estepm=%d months): <a href=\"%s\">%s</a> <br>\n",
1.222 brouard 9887: estepm, subdirf2(fileresu,"T_"),subdirf2(fileresu,"T_"));
9888: fprintf(fichtm,"\
1.288 brouard 9889: - Standard deviation of forward (period) prevalences: <a href=\"%s\">%s</a> <br>\n",\
1.222 brouard 9890: subdirf2(fileresu,"VPL_"),subdirf2(fileresu,"VPL_"));
1.126 brouard 9891:
9892: /* if(popforecast==1) fprintf(fichtm,"\n */
9893: /* - Prevalences forecasting: <a href=\"f%s\">f%s</a> <br>\n */
9894: /* - Population forecasting (if popforecast=1): <a href=\"pop%s\">pop%s</a> <br>\n */
9895: /* <br>",fileres,fileres,fileres,fileres); */
9896: /* else */
1.338 brouard 9897: /* 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); */
1.222 brouard 9898: fflush(fichtm);
1.126 brouard 9899:
1.225 brouard 9900: m=pow(2,cptcoveff);
1.222 brouard 9901: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
1.126 brouard 9902:
1.317 brouard 9903: fprintf(fichtm," <ul><li><b>Graphs (second order)</b></li><p>");
9904:
9905: jj1=0;
9906:
9907: fprintf(fichtm," \n<ul>");
1.337 brouard 9908: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
9909: /* k1=nres; */
1.338 brouard 9910: k1=TKresult[nres];
1.337 brouard 9911: /* for(k1=1; k1<=m;k1++){ /\* For each combination of covariate *\/ */
9912: /* if(m != 1 && TKresult[nres]!= k1) */
9913: /* continue; */
1.317 brouard 9914: jj1++;
9915: if (cptcovn > 0) {
9916: fprintf(fichtm,"\n<li><a size=\"1\" color=\"#EC5E5E\" href=\"#rescovsecond");
1.337 brouard 9917: for (cpt=1; cpt<=cptcovs;cpt++){
9918: fprintf(fichtm,"_V%d=%lg_",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
1.317 brouard 9919: }
9920: fprintf(fichtm,"\">");
9921:
9922: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
9923: fprintf(fichtm,"************ Results for covariates");
1.337 brouard 9924: for (cpt=1; cpt<=cptcovs;cpt++){
9925: fprintf(fichtm," V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
1.317 brouard 9926: }
9927: if(invalidvarcomb[k1]){
9928: fprintf(fichtm," Warning Combination (%d) ignored because no cases ",k1);
9929: continue;
9930: }
9931: fprintf(fichtm,"</a></li>");
9932: } /* cptcovn >0 */
1.337 brouard 9933: } /* End nres */
1.317 brouard 9934: fprintf(fichtm," \n</ul>");
9935:
1.222 brouard 9936: jj1=0;
1.237 brouard 9937:
1.241 brouard 9938: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 9939: /* k1=nres; */
1.338 brouard 9940: k1=TKresult[nres];
9941: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.337 brouard 9942: /* for(k1=1; k1<=m;k1++){ */
9943: /* if(m != 1 && TKresult[nres]!= k1) */
9944: /* continue; */
1.222 brouard 9945: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
9946: jj1++;
1.126 brouard 9947: if (cptcovn > 0) {
1.317 brouard 9948: fprintf(fichtm,"\n<p><a name=\"rescovsecond");
1.337 brouard 9949: for (cpt=1; cpt<=cptcovs;cpt++){
9950: fprintf(fichtm,"_V%d=%lg_",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
1.317 brouard 9951: }
9952: fprintf(fichtm,"\"</a>");
9953:
1.126 brouard 9954: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
1.337 brouard 9955: for (cpt=1; cpt<=cptcovs;cpt++){ /**< cptcoveff number of variables */
9956: fprintf(fichtm," V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
9957: printf(" V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
1.237 brouard 9958: /* fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]); */
1.317 brouard 9959: }
1.237 brouard 9960:
1.338 brouard 9961: fprintf(fichtm," (model=1+age+%s) ************\n<hr size=\"2\" color=\"#EC5E5E\">",model);
1.220 brouard 9962:
1.222 brouard 9963: if(invalidvarcomb[k1]){
9964: fprintf(fichtm,"\n<h4>Combination (%d) ignored because no cases </h4>\n",k1);
9965: continue;
9966: }
1.337 brouard 9967: } /* If cptcovn >0 */
1.126 brouard 9968: for(cpt=1; cpt<=nlstate;cpt++) {
1.258 brouard 9969: fprintf(fichtm,"\n<br>- Observed (cross-sectional with mov_average=%d) and period (incidence based) \
1.314 brouard 9970: 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);
9971: fprintf(fichtm," (data from text file <a href=\"%s\">%s</a>)\n <br>",subdirf2(fileresu,"VPL_"),subdirf2(fileresu,"VPL_"));
9972: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",subdirf2(optionfilefiname,"V_"), cpt,k1,nres);
1.126 brouard 9973: }
9974: fprintf(fichtm,"\n<br>- Total life expectancy by age and \
1.360 brouard 9975: health expectancies in each live state (1 to %d) with confidence intervals \
9976: on left y-scale as well as proportions of time spent in each live state \
9977: (with confidence intervals) on right y-scale 0 to 100%%.\
9978: If popbased=1 the smooth (due to the model) \
1.128 brouard 9979: true period expectancies (those weighted with period prevalences are also\
9980: drawn in addition to the population based expectancies computed using\
1.314 brouard 9981: 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);
9982: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>) \n<br>",subdirf2(optionfilefiname,"T_"),subdirf2(optionfilefiname,"T_"));
9983: fprintf(fichtm,"<img src=\"%s_%d-%d.svg\">",subdirf2(optionfilefiname,"E_"),k1,nres);
1.222 brouard 9984: /* } /\* end i1 *\/ */
1.241 brouard 9985: }/* End nres */
1.222 brouard 9986: fprintf(fichtm,"</ul>");
9987: fflush(fichtm);
1.126 brouard 9988: }
9989:
9990: /******************* Gnuplot file **************/
1.296 brouard 9991: 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){
1.126 brouard 9992:
1.354 brouard 9993: char dirfileres[256],optfileres[256];
9994: char gplotcondition[256], gplotlabel[256];
1.343 brouard 9995: 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;
1.365 ! brouard 9996: /* int lv=0, vlv=0, kl=0; */
! 9997: int lv=0, kl=0;
! 9998: double vlv=0;
1.130 brouard 9999: int ng=0;
1.201 brouard 10000: int vpopbased;
1.223 brouard 10001: int ioffset; /* variable offset for columns */
1.270 brouard 10002: int iyearc=1; /* variable column for year of projection */
10003: int iagec=1; /* variable column for age of projection */
1.235 brouard 10004: int nres=0; /* Index of resultline */
1.266 brouard 10005: int istart=1; /* For starting graphs in projections */
1.219 brouard 10006:
1.126 brouard 10007: /* if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) { */
10008: /* printf("Problem with file %s",optionfilegnuplot); */
10009: /* fprintf(ficlog,"Problem with file %s",optionfilegnuplot); */
10010: /* } */
10011:
10012: /*#ifdef windows */
10013: fprintf(ficgp,"cd \"%s\" \n",pathc);
1.223 brouard 10014: /*#endif */
1.225 brouard 10015: m=pow(2,cptcoveff);
1.126 brouard 10016:
1.274 brouard 10017: /* diagram of the model */
10018: fprintf(ficgp,"\n#Diagram of the model \n");
10019: fprintf(ficgp,"\ndelta=0.03;delta2=0.07;unset arrow;\n");
10020: fprintf(ficgp,"yoff=(%d > 2? 0:1);\n",nlstate);
10021: 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);
10022:
1.343 brouard 10023: 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);
1.274 brouard 10024: fprintf(ficgp,"\n#show arrow\nunset label\n");
10025: 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);
10026: fprintf(ficgp,"\nset label %d+1 sprintf(\"State %%d\",%d+1) center at 0.,0. font \"helvetica, 16\" tc rgbcolor \"red\"\n",nlstate,nlstate);
10027: fprintf(ficgp,"\n#show label\nunset border;unset xtics; unset ytics;\n");
10028: fprintf(ficgp,"\n\nset ter svg size 640, 480;set out \"%s_.svg\" \n",subdirf2(optionfilefiname,"D_"));
10029: fprintf(ficgp,"unset log y; plot [-1.2:1.2][yoff-1.2:1.2] 1/0 not; set out;reset;\n");
10030:
1.202 brouard 10031: /* Contribution to likelihood */
10032: /* Plot the probability implied in the likelihood */
1.223 brouard 10033: fprintf(ficgp,"\n# Contributions to the Likelihood, mle >=1. For mle=4 no interpolation, pure matrix products.\n#\n");
10034: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Likelihood (-2Log(L))\";");
10035: /* fprintf(ficgp,"\nset ter svg size 640, 480"); */ /* Too big for svg */
10036: fprintf(ficgp,"\nset ter pngcairo size 640, 480");
1.204 brouard 10037: /* nice for mle=4 plot by number of matrix products.
1.202 brouard 10038: replot "rrtest1/toto.txt" u 2:($4 == 1 && $5==2 ? $9 : 1/0):5 t "p12" with point lc 1 */
10039: /* replot exp(p1+p2*x)/(1+exp(p1+p2*x)+exp(p3+p4*x)+exp(p5+p6*x)) t "p12(x)" */
1.223 brouard 10040: /* fprintf(ficgp,"\nset out \"%s.svg\";",subdirf2(optionfilefiname,"ILK_")); */
10041: fprintf(ficgp,"\nset out \"%s-dest.png\";",subdirf2(optionfilefiname,"ILK_"));
10042: 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));
10043: fprintf(ficgp,"\nset out \"%s-ori.png\";",subdirf2(optionfilefiname,"ILK_"));
10044: 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));
10045: for (i=1; i<= nlstate ; i ++) {
10046: fprintf(ficgp,"\nset out \"%s-p%dj.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i);
10047: fprintf(ficgp,"unset log;\n# plot weighted, mean weight should have point size of 0.5\n plot \"%s\"",subdirf(fileresilk));
10048: 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);
10049: for (j=2; j<= nlstate+ndeath ; j ++) {
10050: 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);
10051: }
10052: fprintf(ficgp,";\nset out; unset ylabel;\n");
10053: }
10054: /* 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 */
10055: /* fprintf(ficgp,"\nset log y;plot \"%s\" u 2:(-$11):3 t \"All sample, all transitions\" with dots lc variable",subdirf(fileresilk)); */
10056: /* fprintf(ficgp,"\nreplot \"%s\" u 2:($3 <= 3 ? -$11 : 1/0):3 t \"First 3 individuals\" with line lc variable", subdirf(fileresilk)); */
10057: fprintf(ficgp,"\nset out;unset log\n");
10058: /* fprintf(ficgp,"\nset out \"%s.svg\"; replot; set out; # bug gnuplot",subdirf2(optionfilefiname,"ILK_")); */
1.202 brouard 10059:
1.343 brouard 10060: /* Plot the probability implied in the likelihood by covariate value */
10061: fprintf(ficgp,"\nset ter pngcairo size 640, 480");
10062: /* if(debugILK==1){ */
10063: for(kf=1; kf <= ncovf; kf++){ /* For each simple dummy covariate of the model */
1.347 brouard 10064: kvar=Tvar[TvarFind[kf]]; /* variable name */
10065: /* k=18+Tvar[TvarFind[kf]];/\*offset because there are 18 columns in the ILK_ file but could be placed else where *\/ */
1.350 brouard 10066: /* k=18+kf;/\*offset because there are 18 columns in the ILK_ file *\/ */
1.356 brouard 10067: /* k=19+kf;/\*offset because there are 19 columns in the ILK_ file *\/ */
1.355 brouard 10068: k=16+nlstate+kf;/*offset because there are 19 columns in the ILK_ file, first cov Vn on col 21 with 4 living states */
1.343 brouard 10069: for (i=1; i<= nlstate ; i ++) {
10070: fprintf(ficgp,"\nset out \"%s-p%dj-%d.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i,kvar);
10071: fprintf(ficgp,"unset log;\n# For each simple dummy covariate of the model \n plot \"%s\"",subdirf(fileresilk));
1.348 brouard 10072: if(gnuplotversion >=5.2){ /* Former gnuplot versions do not have variable pointsize!! */
10073: 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);
10074: for (j=2; j<= nlstate+ndeath ; j ++) {
10075: 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);
10076: }
10077: }else{
10078: 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);
10079: for (j=2; j<= nlstate+ndeath ; j ++) {
10080: 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);
10081: }
1.343 brouard 10082: }
10083: fprintf(ficgp,";\nset out; unset ylabel;\n");
10084: }
10085: } /* End of each covariate dummy */
10086: for(ncovv=1, iposold=0, kk=0; ncovv <= ncovvt ; ncovv++){
10087: /* Other example V1 + V3 + V5 + age*V1 + age*V3 + age*V5 + V1*V3 + V3*V5 + V1*V5
10088: * kmodel = 1 2 3 4 5 6 7 8 9
10089: * varying 1 2 3 4 5
10090: * ncovv 1 2 3 4 5 6 7 8
10091: * TvarVV[ncovv] V3 5 1 3 3 5 1 5
10092: * TvarVVind[ncovv]=kmodel 2 3 7 7 8 8 9 9
10093: * TvarFind[kmodel] 1 0 0 0 0 0 0 0 0
10094: * kdata ncovcol=[V1 V2] nqv=0 ntv=[V3 V4] nqtv=V5
10095: * Dummy[kmodel] 0 0 1 2 2 3 1 1 1
10096: */
10097: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate */
10098: kvar=TvarVV[ncovv]; /* TvarVV={3, 1, 3} gives the name of each varying covariate */
10099: /* 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]); */
10100: if(ipos!=iposold){ /* Not a product or first of a product */
10101: /* printf(" %d",ipos); */
10102: /* fprintf(ficresilk," V%d",TvarVV[ncovv]); */
10103: /* printf(" DebugILK ficgp suite ipos=%d != iposold=%d\n", ipos, iposold); */
10104: kk++; /* Position of the ncovv column in ILK_ */
10105: k=18+ncovf+kk; /*offset because there are 18 columns in the ILK_ file plus ncovf fixed covariate */
10106: 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) */
10107: for (i=1; i<= nlstate ; i ++) {
10108: fprintf(ficgp,"\nset out \"%s-p%dj-%d.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i,kvar);
10109: fprintf(ficgp,"unset log;\n# For each simple dummy covariate of the model \n plot \"%s\"",subdirf(fileresilk));
10110:
1.348 brouard 10111: /* printf("Before DebugILK gnuplotversion=%g >=5.2\n",gnuplotversion); */
1.343 brouard 10112: if(gnuplotversion >=5.2){ /* Former gnuplot versions do not have variable pointsize!! */
10113: /* printf("DebugILK gnuplotversion=%g >=5.2\n",gnuplotversion); */
10114: 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);
10115: for (j=2; j<= nlstate+ndeath ; j ++) {
10116: 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);
10117: }
10118: }else{
10119: /* printf("DebugILK gnuplotversion=%g <5.2\n",gnuplotversion); */
10120: 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);
10121: for (j=2; j<= nlstate+ndeath ; j ++) {
10122: 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);
10123: }
10124: }
10125: fprintf(ficgp,";\nset out; unset ylabel;\n");
10126: }
10127: }/* End if dummy varying */
10128: }else{ /*Product */
10129: /* printf("*"); */
10130: /* fprintf(ficresilk,"*"); */
10131: }
10132: iposold=ipos;
10133: } /* For each time varying covariate */
10134: /* } /\* debugILK==1 *\/ */
10135: /* 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 */
10136: /* fprintf(ficgp,"\nset log y;plot \"%s\" u 2:(-$11):3 t \"All sample, all transitions\" with dots lc variable",subdirf(fileresilk)); */
10137: /* fprintf(ficgp,"\nreplot \"%s\" u 2:($3 <= 3 ? -$11 : 1/0):3 t \"First 3 individuals\" with line lc variable", subdirf(fileresilk)); */
10138: fprintf(ficgp,"\nset out;unset log\n");
10139: /* fprintf(ficgp,"\nset out \"%s.svg\"; replot; set out; # bug gnuplot",subdirf2(optionfilefiname,"ILK_")); */
10140:
10141:
10142:
1.126 brouard 10143: strcpy(dirfileres,optionfilefiname);
10144: strcpy(optfileres,"vpl");
1.223 brouard 10145: /* 1eme*/
1.238 brouard 10146: for (cpt=1; cpt<= nlstate ; cpt ++){ /* For each live state */
1.337 brouard 10147: /* for (k1=1; k1<= m ; k1 ++){ /\* For each valid combination of covariate *\/ */
1.236 brouard 10148: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 10149: k1=TKresult[nres];
1.338 brouard 10150: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.238 brouard 10151: /* plot [100000000000000000000:-100000000000000000000] "mysbiaspar/vplrmysbiaspar.txt to check */
1.337 brouard 10152: /* if(m != 1 && TKresult[nres]!= k1) */
10153: /* continue; */
1.238 brouard 10154: /* We are interested in selected combination by the resultline */
1.246 brouard 10155: /* printf("\n# 1st: Period (stable) prevalence with CI: 'VPL_' files and live state =%d ", cpt); */
1.288 brouard 10156: fprintf(ficgp,"\n# 1st: Forward (stable period) prevalence with CI: 'VPL_' files and live state =%d ", cpt);
1.264 brouard 10157: strcpy(gplotlabel,"(");
1.337 brouard 10158: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10159: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10160: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10161:
10162: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate k get corresponding value lv for combination k1 *\/ */
10163: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the value of the covariate corresponding to k1 combination *\\/ *\/ */
10164: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
10165: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10166: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10167: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10168: /* vlv= nbcode[Tvaraff[k]][lv]; /\* vlv is the value of the covariate lv, 0 or 1 *\/ */
10169: /* /\* For each combination of covariate k1 (V1=1, V3=0), we printed the current covariate k and its value vlv *\/ */
10170: /* /\* printf(" V%d=%d ",Tvaraff[k],vlv); *\/ */
10171: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10172: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10173: /* } */
10174: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10175: /* /\* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); *\/ */
10176: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
10177: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
1.264 brouard 10178: }
10179: strcpy(gplotlabel+strlen(gplotlabel),")");
1.246 brouard 10180: /* printf("\n#\n"); */
1.238 brouard 10181: fprintf(ficgp,"\n#\n");
10182: if(invalidvarcomb[k1]){
1.260 brouard 10183: /*k1=k1-1;*/ /* To be checked */
1.238 brouard 10184: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10185: continue;
10186: }
1.235 brouard 10187:
1.241 brouard 10188: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"V_"),cpt,k1,nres);
10189: fprintf(ficgp,"\n#set out \"V_%s_%d-%d-%d.svg\" \n",optionfilefiname,cpt,k1,nres);
1.276 brouard 10190: /* fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel); */
1.338 brouard 10191: fprintf(ficgp,"set title \"Alive state %d %s model=1+age+%s\" font \"Helvetica,12\"\n",cpt,gplotlabel,model);
1.260 brouard 10192: 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);
10193: /* 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); */
10194: /* k1-1 error should be nres-1*/
1.238 brouard 10195: for (i=1; i<= nlstate ; i ++) {
10196: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
10197: else fprintf(ficgp," %%*lf (%%*lf)");
10198: }
1.288 brouard 10199: 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);
1.238 brouard 10200: for (i=1; i<= nlstate ; i ++) {
10201: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
10202: else fprintf(ficgp," %%*lf (%%*lf)");
10203: }
1.260 brouard 10204: 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);
1.238 brouard 10205: for (i=1; i<= nlstate ; i ++) {
10206: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
10207: else fprintf(ficgp," %%*lf (%%*lf)");
10208: }
1.265 brouard 10209: /* 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)); */
10210:
10211: fprintf(ficgp,"\" t\"\" w l lt 1,\"%s\" u 1:((",subdirf2(fileresu,"P_"));
10212: if(cptcoveff ==0){
1.271 brouard 10213: fprintf(ficgp,"$%d)) t 'Observed prevalence in state %d' with line lt 3", 2+3*(cpt-1), cpt );
1.265 brouard 10214: }else{
10215: kl=0;
10216: for (k=1; k<=cptcoveff; k++){ /* For each combination of covariate */
1.332 brouard 10217: /* lv= decodtabm(k1,k,cptcoveff); /\* Should be the covariate value corresponding to k1 combination and kth covariate *\/ */
10218: lv=codtabm(k1,TnsdVar[Tvaraff[k]]);
1.265 brouard 10219: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
10220: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
10221: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
10222: vlv= nbcode[Tvaraff[k]][lv];
10223: kl++;
10224: /* 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 *\/ */
10225: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
10226: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
10227: /* '' 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*/
10228: if(k==cptcoveff){
10229: 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], \
10230: 2+cptcoveff*2+3*(cpt-1), cpt ); /* 4 or 6 ?*/
10231: }else{
10232: fprintf(ficgp,"$%d==%d && $%d==%d && ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv]);
10233: kl++;
10234: }
10235: } /* end covariate */
10236: } /* end if no covariate */
10237:
1.296 brouard 10238: if(prevbcast==1){ /* We need to get the corresponding values of the covariates involved in this combination k1 */
1.238 brouard 10239: /* 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); */
1.242 brouard 10240: fprintf(ficgp,",\"%s\" u 1:((",subdirf2(fileresu,"PLB_")); /* Age is in 1, nres in 2 to be fixed */
1.238 brouard 10241: if(cptcoveff ==0){
1.245 brouard 10242: fprintf(ficgp,"$%d)) t 'Backward prevalence in state %d' with line lt 3", 2+(cpt-1), cpt );
1.238 brouard 10243: }else{
10244: kl=0;
10245: for (k=1; k<=cptcoveff; k++){ /* For each combination of covariate */
1.332 brouard 10246: /* lv= decodtabm(k1,k,cptcoveff); /\* Should be the covariate value corresponding to k1 combination and kth covariate *\/ */
10247: lv=codtabm(k1,TnsdVar[Tvaraff[k]]);
1.238 brouard 10248: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
10249: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
10250: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
1.332 brouard 10251: /* vlv= nbcode[Tvaraff[k]][lv]; */
10252: vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])];
1.223 brouard 10253: kl++;
1.238 brouard 10254: /* 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 *\/ */
10255: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
10256: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
10257: /* '' 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*/
10258: if(k==cptcoveff){
1.245 brouard 10259: 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], \
1.242 brouard 10260: 2+cptcoveff*2+(cpt-1), cpt ); /* 4 or 6 ?*/
1.238 brouard 10261: }else{
1.332 brouard 10262: fprintf(ficgp,"$%d==%d && $%d==%d && ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]);
1.238 brouard 10263: kl++;
10264: }
10265: } /* end covariate */
10266: } /* end if no covariate */
1.296 brouard 10267: if(prevbcast == 1){
1.268 brouard 10268: fprintf(ficgp,", \"%s\" every :::%d::%d u 1:($2==%d ? $3:1/0) \"%%lf %%lf",subdirf2(fileresu,"VBL_"),nres-1,nres-1,nres);
10269: /* k1-1 error should be nres-1*/
10270: for (i=1; i<= nlstate ; i ++) {
10271: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
10272: else fprintf(ficgp," %%*lf (%%*lf)");
10273: }
1.271 brouard 10274: 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);
1.268 brouard 10275: for (i=1; i<= nlstate ; i ++) {
10276: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
10277: else fprintf(ficgp," %%*lf (%%*lf)");
10278: }
1.276 brouard 10279: 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);
1.268 brouard 10280: for (i=1; i<= nlstate ; i ++) {
10281: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
10282: else fprintf(ficgp," %%*lf (%%*lf)");
10283: }
1.274 brouard 10284: fprintf(ficgp,"\" t\"\" w l lt 4");
1.268 brouard 10285: } /* end if backprojcast */
1.296 brouard 10286: } /* end if prevbcast */
1.276 brouard 10287: /* fprintf(ficgp,"\nset out ;unset label;\n"); */
10288: fprintf(ficgp,"\nset out ;unset title;\n");
1.238 brouard 10289: } /* nres */
1.337 brouard 10290: /* } /\* k1 *\/ */
1.201 brouard 10291: } /* cpt */
1.235 brouard 10292:
10293:
1.126 brouard 10294: /*2 eme*/
1.337 brouard 10295: /* for (k1=1; k1<= m ; k1 ++){ */
1.238 brouard 10296: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 10297: k1=TKresult[nres];
1.338 brouard 10298: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.337 brouard 10299: /* if(m != 1 && TKresult[nres]!= k1) */
10300: /* continue; */
1.238 brouard 10301: fprintf(ficgp,"\n# 2nd: Total life expectancy with CI: 't' files ");
1.264 brouard 10302: strcpy(gplotlabel,"(");
1.337 brouard 10303: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10304: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10305: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10306: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
10307: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
10308: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
10309: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10310: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10311: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10312: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
10313: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10314: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10315: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10316: /* } */
10317: /* /\* for(k=1; k <= ncovds; k++){ *\/ */
10318: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10319: /* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
10320: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
10321: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
1.238 brouard 10322: }
1.264 brouard 10323: strcpy(gplotlabel+strlen(gplotlabel),")");
1.211 brouard 10324: fprintf(ficgp,"\n#\n");
1.223 brouard 10325: if(invalidvarcomb[k1]){
10326: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10327: continue;
10328: }
1.219 brouard 10329:
1.241 brouard 10330: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"E_"),k1,nres);
1.238 brouard 10331: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
1.264 brouard 10332: fprintf(ficgp,"\nset label \"popbased %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",vpopbased,gplotlabel);
10333: if(vpopbased==0){
1.360 brouard 10334: fprintf(ficgp,"set ylabel \"Years\" \nset ter svg size 640, 480\nunset ytics; unset y2tics; set ytics nomirror; set y2tics 0,10,100;set y2range [0:100];\nplot [%.f:%.f] ",ageminpar,fage);
1.264 brouard 10335: }else
1.238 brouard 10336: fprintf(ficgp,"\nreplot ");
1.360 brouard 10337: for (i=1; i<= nlstate+1 ; i ++) { /* For state i-1=0 is LE, while i-1=1 to nlstate are origin state */
1.238 brouard 10338: k=2*i;
1.360 brouard 10339: 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); /* for fixed variables age, popbased, mobilav */
10340: for (j=1; j<= nlstate+1 ; j ++) { /* e.. e.1 e.2 again j-1 is the state of end, wlim_i eij*/
10341: if (j==i) fprintf(ficgp," %%lf (%%lf)"); /* We want to read e.. i=1,j=1, e.1 i=2,j=2, e.2 i=3,j=3 */
10342: else fprintf(ficgp," %%*lf (%%*lf)"); /* skipping that field with a star */
1.238 brouard 10343: }
10344: if (i== 1) fprintf(ficgp,"\" t\"TLE\" w l lt %d, \\\n",i);
1.360 brouard 10345: else fprintf(ficgp,"\" t\"LE in state (%d)\" w l lt %d, \\\n",i-1,i+1); /* state=i-1=1 to nlstate */
1.261 brouard 10346: 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);
1.238 brouard 10347: for (j=1; j<= nlstate+1 ; j ++) {
10348: if (j==i) fprintf(ficgp," %%lf (%%lf)");
10349: else fprintf(ficgp," %%*lf (%%*lf)");
10350: }
10351: fprintf(ficgp,"\" t\"\" w l lt 0,");
1.261 brouard 10352: 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);
1.238 brouard 10353: for (j=1; j<= nlstate+1 ; j ++) {
10354: if (j==i) fprintf(ficgp," %%lf (%%lf)");
10355: else fprintf(ficgp," %%*lf (%%*lf)");
10356: }
1.360 brouard 10357: if (i== (nlstate+1)) fprintf(ficgp,"\" t\"\" w l lt 0,\\\n"); /* ,\\\n added for th percentage graphs */
1.238 brouard 10358: else fprintf(ficgp,"\" t\"\" w l lt 0,\\\n");
10359: } /* state */
1.360 brouard 10360: /* again for the percentag spent in state i-1=1 to i-1=nlstate */
10361: for (i=2; i<= nlstate+1 ; i ++) { /* For state i-1=0 is LE, while i-1=1 to nlstate are origin state */
10362: k=2*i;
10363: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2==%d && ($4)<=1 && ($4)>=0 ?($4)*100. : 1/0) \"%%lf %%lf %%lf",subdirf2(fileresu,"T_"),nres-1,nres-1, vpopbased); /* for fixed variables age, popbased, mobilav */
10364: for (j=1; j<= nlstate ; j ++)
10365: fprintf(ficgp," %%*lf (%%*lf)"); /* Skipping TLE and LE to read %LE only */
10366: for (j=1; j<= nlstate+1 ; j ++) { /* e.. e.1 e.2 again j-1 is the state of end, wlim_i eij*/
10367: if (j==i) fprintf(ficgp," %%lf (%%lf)"); /* We want to read e.. i=1,j=1, e.1 i=2,j=2, e.2 i=3,j=3 */
10368: else fprintf(ficgp," %%*lf (%%*lf)"); /* skipping that field with a star */
10369: }
10370: if (i== 1) fprintf(ficgp,"\" t\"%%TLE\" w l lt %d axis x1y2, \\\n",i); /* Not used */
10371: else fprintf(ficgp,"\" t\"%%LE in state (%d)\" w l lw 2 lt %d axis x1y2, \\\n",i-1,i+1); /* state=i-1=1 to nlstate */
10372: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2==%d && ($4-$5*2)<=1 && ($4-$5*2)>=0? ($4-$5*2)*100. : 1/0) \"%%lf %%lf %%lf",subdirf2(fileresu,"T_"),nres-1,nres-1,vpopbased);
10373: for (j=1; j<= nlstate ; j ++)
10374: fprintf(ficgp," %%*lf (%%*lf)"); /* Skipping TLE and LE to read %LE only */
10375: for (j=1; j<= nlstate+1 ; j ++) {
10376: if (j==i) fprintf(ficgp," %%lf (%%lf)");
10377: else fprintf(ficgp," %%*lf (%%*lf)");
10378: }
10379: fprintf(ficgp,"\" t\"\" w l lt 0 axis x1y2,");
10380: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2==%d && ($4+$5*2)<=1 && ($4+$5*2)>=0 ? ($4+$5*2)*100. : 1/0) \"%%lf %%lf %%lf",subdirf2(fileresu,"T_"),nres-1,nres-1,vpopbased);
10381: for (j=1; j<= nlstate ; j ++)
10382: fprintf(ficgp," %%*lf (%%*lf)"); /* Skipping TLE and LE to read %LE only */
10383: for (j=1; j<= nlstate+1 ; j ++) {
10384: if (j==i) fprintf(ficgp," %%lf (%%lf)");
10385: else fprintf(ficgp," %%*lf (%%*lf)");
10386: }
10387: if (i== (nlstate+1)) fprintf(ficgp,"\" t\"\" w l lt 0 axis x1y2");
10388: else fprintf(ficgp,"\" t\"\" w l lt 0 axis x1y2,\\\n");
10389: } /* state for percent */
1.238 brouard 10390: } /* vpopbased */
1.264 brouard 10391: fprintf(ficgp,"\nset out;set out \"%s_%d-%d.svg\"; replot; set out; unset label;\n",subdirf2(optionfilefiname,"E_"),k1,nres); /* Buggy gnuplot */
1.238 brouard 10392: } /* end nres */
1.337 brouard 10393: /* } /\* k1 end 2 eme*\/ */
1.238 brouard 10394:
10395:
10396: /*3eme*/
1.337 brouard 10397: /* for (k1=1; k1<= m ; k1 ++){ */
1.238 brouard 10398: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 10399: k1=TKresult[nres];
1.338 brouard 10400: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.337 brouard 10401: /* if(m != 1 && TKresult[nres]!= k1) */
10402: /* continue; */
1.238 brouard 10403:
1.332 brouard 10404: for (cpt=1; cpt<= nlstate ; cpt ++) { /* Fragile no verification of covariate values */
1.261 brouard 10405: fprintf(ficgp,"\n\n# 3d: Life expectancy with EXP_ files: combination=%d state=%d",k1, cpt);
1.264 brouard 10406: strcpy(gplotlabel,"(");
1.337 brouard 10407: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10408: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10409: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10410: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
10411: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
10412: /* lv= codtabm(k1,TnsdVar[Tvaraff[k]]); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
10413: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10414: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10415: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10416: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
10417: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10418: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10419: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10420: /* } */
10421: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10422: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][resultmodel[nres][k4]]); */
10423: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][resultmodel[nres][k4]]); */
10424: }
1.264 brouard 10425: strcpy(gplotlabel+strlen(gplotlabel),")");
1.238 brouard 10426: fprintf(ficgp,"\n#\n");
10427: if(invalidvarcomb[k1]){
10428: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10429: continue;
10430: }
10431:
10432: /* k=2+nlstate*(2*cpt-2); */
10433: k=2+(nlstate+1)*(cpt-1);
1.241 brouard 10434: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres);
1.264 brouard 10435: fprintf(ficgp,"set label \"%s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",gplotlabel);
1.238 brouard 10436: fprintf(ficgp,"set ter svg size 640, 480\n\
1.261 brouard 10437: 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);
1.238 brouard 10438: /*fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d-2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
10439: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
10440: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
10441: fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d+2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
10442: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
10443: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
1.219 brouard 10444:
1.238 brouard 10445: */
10446: for (i=1; i< nlstate ; i ++) {
1.261 brouard 10447: 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);
1.238 brouard 10448: /* fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d%d\" w l",subdirf2(fileres,"e"),k1-1,k1-1,k+2*i,cpt,i+1);*/
1.219 brouard 10449:
1.238 brouard 10450: }
1.261 brouard 10451: fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d.\" w l",subdirf2(fileresu,"E_"),nres-1,nres-1,k+nlstate,cpt);
1.238 brouard 10452: }
1.264 brouard 10453: fprintf(ficgp,"\nunset label;\n");
1.238 brouard 10454: } /* end nres */
1.337 brouard 10455: /* } /\* end kl 3eme *\/ */
1.126 brouard 10456:
1.223 brouard 10457: /* 4eme */
1.201 brouard 10458: /* Survival functions (period) from state i in state j by initial state i */
1.337 brouard 10459: /* for (k1=1; k1<=m; k1++){ /\* For each covariate and each value *\/ */
1.238 brouard 10460: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 10461: k1=TKresult[nres];
1.338 brouard 10462: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.337 brouard 10463: /* if(m != 1 && TKresult[nres]!= k1) */
10464: /* continue; */
1.238 brouard 10465: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state cpt*/
1.264 brouard 10466: strcpy(gplotlabel,"(");
1.337 brouard 10467: fprintf(ficgp,"\n#\n#\n# Survival functions in state %d : 'LIJ_' files, cov=%d state=%d", cpt, k1, cpt);
10468: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10469: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10470: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10471: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
10472: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
10473: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
10474: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10475: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10476: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10477: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
10478: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10479: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10480: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10481: /* } */
10482: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10483: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
10484: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
1.238 brouard 10485: }
1.264 brouard 10486: strcpy(gplotlabel+strlen(gplotlabel),")");
1.238 brouard 10487: fprintf(ficgp,"\n#\n");
10488: if(invalidvarcomb[k1]){
10489: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10490: continue;
1.223 brouard 10491: }
1.238 brouard 10492:
1.241 brouard 10493: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres);
1.264 brouard 10494: fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
1.238 brouard 10495: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
10496: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
10497: k=3;
10498: for (i=1; i<= nlstate ; i ++){
10499: if(i==1){
10500: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
10501: }else{
10502: fprintf(ficgp,", '' ");
10503: }
10504: l=(nlstate+ndeath)*(i-1)+1;
10505: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
10506: for (j=2; j<= nlstate+ndeath ; j ++)
10507: fprintf(ficgp,"+$%d",k+l+j-1);
10508: fprintf(ficgp,")) t \"l(%d,%d)\" w l",i,cpt);
10509: } /* nlstate */
1.264 brouard 10510: fprintf(ficgp,"\nset out; unset label;\n");
1.238 brouard 10511: } /* end cpt state*/
10512: } /* end nres */
1.337 brouard 10513: /* } /\* end covariate k1 *\/ */
1.238 brouard 10514:
1.220 brouard 10515: /* 5eme */
1.201 brouard 10516: /* Survival functions (period) from state i in state j by final state j */
1.337 brouard 10517: /* for (k1=1; k1<= m ; k1++){ /\* For each covariate combination if any *\/ */
1.238 brouard 10518: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 10519: k1=TKresult[nres];
1.338 brouard 10520: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.337 brouard 10521: /* if(m != 1 && TKresult[nres]!= k1) */
10522: /* continue; */
1.238 brouard 10523: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each inital state */
1.264 brouard 10524: strcpy(gplotlabel,"(");
1.238 brouard 10525: fprintf(ficgp,"\n#\n#\n# Survival functions in state j and all livestates from state i by final state j: 'lij' files, cov=%d state=%d",k1, cpt);
1.337 brouard 10526: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10527: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10528: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10529: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
10530: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
10531: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
10532: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10533: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10534: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10535: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
10536: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10537: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10538: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10539: /* } */
10540: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10541: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
10542: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
1.238 brouard 10543: }
1.264 brouard 10544: strcpy(gplotlabel+strlen(gplotlabel),")");
1.238 brouard 10545: fprintf(ficgp,"\n#\n");
10546: if(invalidvarcomb[k1]){
10547: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10548: continue;
10549: }
1.227 brouard 10550:
1.241 brouard 10551: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJT_"),cpt,k1,nres);
1.264 brouard 10552: fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
1.238 brouard 10553: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
10554: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
10555: k=3;
10556: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
10557: if(j==1)
10558: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
10559: else
10560: fprintf(ficgp,", '' ");
10561: l=(nlstate+ndeath)*(cpt-1) +j;
10562: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):($%d",k1,k+l);
10563: /* for (i=2; i<= nlstate+ndeath ; i ++) */
10564: /* fprintf(ficgp,"+$%d",k+l+i-1); */
10565: fprintf(ficgp,") t \"l(%d,%d)\" w l",cpt,j);
10566: } /* nlstate */
10567: fprintf(ficgp,", '' ");
10568: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):(",k1);
10569: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
10570: l=(nlstate+ndeath)*(cpt-1) +j;
10571: if(j < nlstate)
10572: fprintf(ficgp,"$%d +",k+l);
10573: else
10574: fprintf(ficgp,"$%d) t\"l(%d,.)\" w l",k+l,cpt);
10575: }
1.264 brouard 10576: fprintf(ficgp,"\nset out; unset label;\n");
1.238 brouard 10577: } /* end cpt state*/
1.337 brouard 10578: /* } /\* end covariate *\/ */
1.238 brouard 10579: } /* end nres */
1.227 brouard 10580:
1.220 brouard 10581: /* 6eme */
1.202 brouard 10582: /* CV preval stable (period) for each covariate */
1.337 brouard 10583: /* for (k1=1; k1<= m ; k1 ++) /\* For each covariate combination if any *\/ */
1.237 brouard 10584: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 10585: k1=TKresult[nres];
1.338 brouard 10586: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.337 brouard 10587: /* if(m != 1 && TKresult[nres]!= k1) */
10588: /* continue; */
1.255 brouard 10589: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state of arrival */
1.264 brouard 10590: strcpy(gplotlabel,"(");
1.288 brouard 10591: fprintf(ficgp,"\n#\n#\n#CV preval stable (forward): 'pij' files, covariatecombination#=%d state=%d",k1, cpt);
1.337 brouard 10592: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10593: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10594: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10595: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
10596: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
10597: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
10598: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10599: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10600: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10601: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
10602: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10603: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10604: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10605: /* } */
10606: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10607: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
10608: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
1.237 brouard 10609: }
1.264 brouard 10610: strcpy(gplotlabel+strlen(gplotlabel),")");
1.211 brouard 10611: fprintf(ficgp,"\n#\n");
1.223 brouard 10612: if(invalidvarcomb[k1]){
1.227 brouard 10613: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10614: continue;
1.223 brouard 10615: }
1.227 brouard 10616:
1.241 brouard 10617: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"P_"),cpt,k1,nres);
1.264 brouard 10618: fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
1.126 brouard 10619: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
1.238 brouard 10620: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
1.211 brouard 10621: k=3; /* Offset */
1.255 brouard 10622: for (i=1; i<= nlstate ; i ++){ /* State of origin */
1.227 brouard 10623: if(i==1)
10624: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
10625: else
10626: fprintf(ficgp,", '' ");
1.255 brouard 10627: l=(nlstate+ndeath)*(i-1)+1; /* 1, 1+ nlstate+ndeath, 1+2*(nlstate+ndeath) */
1.227 brouard 10628: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
10629: for (j=2; j<= nlstate ; j ++)
10630: fprintf(ficgp,"+$%d",k+l+j-1);
10631: fprintf(ficgp,")) t \"prev(%d,%d)\" w l",i,cpt);
1.153 brouard 10632: } /* nlstate */
1.264 brouard 10633: fprintf(ficgp,"\nset out; unset label;\n");
1.153 brouard 10634: } /* end cpt state*/
10635: } /* end covariate */
1.227 brouard 10636:
10637:
1.220 brouard 10638: /* 7eme */
1.296 brouard 10639: if(prevbcast == 1){
1.288 brouard 10640: /* CV backward prevalence for each covariate */
1.337 brouard 10641: /* for (k1=1; k1<= m ; k1 ++) /\* For each covariate combination if any *\/ */
1.237 brouard 10642: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 10643: k1=TKresult[nres];
1.338 brouard 10644: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.337 brouard 10645: /* if(m != 1 && TKresult[nres]!= k1) */
10646: /* continue; */
1.268 brouard 10647: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life origin state */
1.264 brouard 10648: strcpy(gplotlabel,"(");
1.288 brouard 10649: fprintf(ficgp,"\n#\n#\n#CV Backward stable prevalence: 'pijb' files, covariatecombination#=%d state=%d",k1, cpt);
1.337 brouard 10650: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10651: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10652: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10653: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
10654: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
10655: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
10656: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10657: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10658: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10659: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
10660: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10661: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10662: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10663: /* } */
10664: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10665: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
10666: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
1.237 brouard 10667: }
1.264 brouard 10668: strcpy(gplotlabel+strlen(gplotlabel),")");
1.227 brouard 10669: fprintf(ficgp,"\n#\n");
10670: if(invalidvarcomb[k1]){
10671: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10672: continue;
10673: }
10674:
1.241 brouard 10675: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PB_"),cpt,k1,nres);
1.268 brouard 10676: fprintf(ficgp,"set label \"Origin alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
1.227 brouard 10677: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
1.238 brouard 10678: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
1.227 brouard 10679: k=3; /* Offset */
1.268 brouard 10680: for (i=1; i<= nlstate ; i ++){ /* State of arrival */
1.227 brouard 10681: if(i==1)
10682: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJB_"));
10683: else
10684: fprintf(ficgp,", '' ");
10685: /* l=(nlstate+ndeath)*(i-1)+1; */
1.255 brouard 10686: l=(nlstate+ndeath)*(cpt-1)+1; /* fixed for i; cpt=1 1, cpt=2 1+ nlstate+ndeath, 1+2*(nlstate+ndeath) */
1.324 brouard 10687: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l); /\* a vérifier *\/ */
10688: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l+(cpt-1)+i-1); /\* a vérifier *\/ */
1.255 brouard 10689: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d",k1,k+l+i-1); /* To be verified */
1.227 brouard 10690: /* for (j=2; j<= nlstate ; j ++) */
10691: /* fprintf(ficgp,"+$%d",k+l+j-1); */
10692: /* /\* fprintf(ficgp,"+$%d",k+l+j-1); *\/ */
1.268 brouard 10693: fprintf(ficgp,") t \"bprev(%d,%d)\" w l",cpt,i);
1.227 brouard 10694: } /* nlstate */
1.264 brouard 10695: fprintf(ficgp,"\nset out; unset label;\n");
1.218 brouard 10696: } /* end cpt state*/
10697: } /* end covariate */
1.296 brouard 10698: } /* End if prevbcast */
1.218 brouard 10699:
1.223 brouard 10700: /* 8eme */
1.218 brouard 10701: if(prevfcast==1){
1.288 brouard 10702: /* Projection from cross-sectional to forward stable (period) prevalence for each covariate */
1.218 brouard 10703:
1.337 brouard 10704: /* for (k1=1; k1<= m ; k1 ++) /\* For each covariate combination if any *\/ */
1.237 brouard 10705: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 10706: k1=TKresult[nres];
1.338 brouard 10707: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.337 brouard 10708: /* if(m != 1 && TKresult[nres]!= k1) */
10709: /* continue; */
1.211 brouard 10710: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.264 brouard 10711: strcpy(gplotlabel,"(");
1.288 brouard 10712: fprintf(ficgp,"\n#\n#\n#Projection of prevalence to forward stable prevalence (period): 'PROJ_' files, covariatecombination#=%d state=%d",k1, cpt);
1.337 brouard 10713: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10714: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10715: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10716: /* for (k=1; k<=cptcoveff; k++){ /\* For each correspondig covariate value *\/ */
10717: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate value corresponding to k1 combination and kth covariate *\\/ *\/ */
10718: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
10719: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10720: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10721: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10722: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
10723: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10724: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10725: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10726: /* } */
10727: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10728: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
10729: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
1.237 brouard 10730: }
1.264 brouard 10731: strcpy(gplotlabel+strlen(gplotlabel),")");
1.227 brouard 10732: fprintf(ficgp,"\n#\n");
10733: if(invalidvarcomb[k1]){
10734: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10735: continue;
10736: }
10737:
10738: fprintf(ficgp,"# hpijx=probability over h years, hp.jx is weighted by observed prev\n ");
1.241 brouard 10739: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres);
1.264 brouard 10740: fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
1.227 brouard 10741: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\
1.238 brouard 10742: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
1.266 brouard 10743:
10744: /* for (i=1; i<= nlstate+1 ; i ++){ /\* nlstate +1 p11 p21 p.1 *\/ */
10745: istart=nlstate+1; /* Could be one if by state, but nlstate+1 is w.i projection only */
10746: /*istart=1;*/ /* Could be one if by state, but nlstate+1 is w.i projection only */
10747: for (i=istart; i<= nlstate+1 ; i ++){ /* nlstate +1 p11 p21 p.1 */
1.227 brouard 10748: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
10749: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
10750: /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
10751: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1.266 brouard 10752: if(i==istart){
1.227 brouard 10753: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"F_"));
10754: }else{
10755: fprintf(ficgp,",\\\n '' ");
10756: }
10757: if(cptcoveff ==0){ /* No covariate */
10758: ioffset=2; /* Age is in 2 */
10759: /*# yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
10760: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
10761: /*# V1 = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
10762: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
10763: fprintf(ficgp," u %d:(", ioffset);
1.266 brouard 10764: if(i==nlstate+1){
1.270 brouard 10765: fprintf(ficgp," $%d/(1.-$%d)):1 t 'pw.%d' with line lc variable ", \
1.266 brouard 10766: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
10767: fprintf(ficgp,",\\\n '' ");
10768: fprintf(ficgp," u %d:(",ioffset);
1.270 brouard 10769: fprintf(ficgp," (($1-$2) == %d ) ? $%d/(1.-$%d) : 1/0):1 with labels center not ", \
1.266 brouard 10770: offyear, \
1.268 brouard 10771: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate );
1.266 brouard 10772: }else
1.227 brouard 10773: fprintf(ficgp," $%d/(1.-$%d)) t 'p%d%d' with line ", \
10774: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,i,cpt );
10775: }else{ /* more than 2 covariates */
1.270 brouard 10776: ioffset=2*cptcoveff+2; /* Age is in 4 or 6 or etc.*/
10777: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
10778: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
10779: iyearc=ioffset-1;
10780: iagec=ioffset;
1.227 brouard 10781: fprintf(ficgp," u %d:(",ioffset);
10782: kl=0;
10783: strcpy(gplotcondition,"(");
1.351 brouard 10784: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate writing the chain of conditions *\/ */
1.332 brouard 10785: /* lv= decodtabm(k1,k,cptcoveff); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
1.351 brouard 10786: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10787: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
10788: lv=Tvresult[nres][k];
10789: vlv=TinvDoQresult[nres][Tvresult[nres][k]];
1.227 brouard 10790: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
10791: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
10792: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
1.332 brouard 10793: /* vlv= nbcode[Tvaraff[k]][lv]; /\* Value of the modality of Tvaraff[k] *\/ */
1.351 brouard 10794: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
1.227 brouard 10795: kl++;
1.364 brouard 10796: /* Problem with quantitative variables TinvDoQresult[nres] */
1.351 brouard 10797: /* sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,Tvaraff[k], kl+1, nbcode[Tvaraff[k]][lv]); */
1.364 brouard 10798: sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%lg " ,kl,lv, kl+1, vlv );/* Solved but quantitative must be shifted */
1.227 brouard 10799: kl++;
1.351 brouard 10800: if(k <cptcovs && cptcovs>1)
1.227 brouard 10801: sprintf(gplotcondition+strlen(gplotcondition)," && ");
10802: }
10803: strcpy(gplotcondition+strlen(gplotcondition),")");
10804: /* 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 *\/ */
10805: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
10806: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
10807: /* '' 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*/
10808: if(i==nlstate+1){
1.270 brouard 10809: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0):%d t 'p.%d' with line lc variable", gplotcondition, \
10810: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,iyearc, cpt );
1.266 brouard 10811: fprintf(ficgp,",\\\n '' ");
1.364 brouard 10812: fprintf(ficgp," u %d:(",iagec); /* Below iyearc should be increades if quantitative variable in the reult line */
10813: /* $7==6 && $8==2.47 ) && (($9-$10) == 1953 ) ? $12/(1.-$24) : 1/0):7 with labels center not */
10814: /* but was && $7==6 && $8==2 ) && (($7-$8) == 1953 ) ? $12/(1.-$24) : 1/0):7 with labels center not */
1.270 brouard 10815: fprintf(ficgp,"%s && (($%d-$%d) == %d ) ? $%d/(1.-$%d) : 1/0):%d with labels center not ", gplotcondition, \
10816: iyearc, iagec, offyear, \
10817: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate, iyearc );
1.266 brouard 10818: /* '' u 6:(($1==1 && $2==0 && $3==2 && $4==0) && (($5-$6) == 1947) ? $10/(1.-$22) : 1/0):5 with labels center boxed not*/
1.227 brouard 10819: }else{
10820: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \
10821: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset +1+(i-1)+(nlstate+1)*nlstate,i,cpt );
10822: }
10823: } /* end if covariate */
10824: } /* nlstate */
1.264 brouard 10825: fprintf(ficgp,"\nset out; unset label;\n");
1.223 brouard 10826: } /* end cpt state*/
10827: } /* end covariate */
10828: } /* End if prevfcast */
1.227 brouard 10829:
1.296 brouard 10830: if(prevbcast==1){
1.268 brouard 10831: /* Back projection from cross-sectional to stable (mixed) for each covariate */
10832:
1.337 brouard 10833: /* for (k1=1; k1<= m ; k1 ++) /\* For each covariate combination if any *\/ */
1.268 brouard 10834: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 10835: k1=TKresult[nres];
1.338 brouard 10836: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.337 brouard 10837: /* if(m != 1 && TKresult[nres]!= k1) */
10838: /* continue; */
1.268 brouard 10839: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
10840: strcpy(gplotlabel,"(");
10841: fprintf(ficgp,"\n#\n#\n#Back projection of prevalence to stable (mixed) back prevalence: 'BPROJ_' files, covariatecombination#=%d originstate=%d",k1, cpt);
1.337 brouard 10842: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10843: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10844: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10845: /* for (k=1; k<=cptcoveff; k++){ /\* For each correspondig covariate value *\/ */
10846: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate value corresponding to k1 combination and kth covariate *\\/ *\/ */
10847: /* lv= codtabm(k1,TnsdVar[Tvaraff[k]]); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
10848: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10849: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10850: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10851: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
10852: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10853: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10854: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10855: /* } */
10856: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10857: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
10858: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
1.268 brouard 10859: }
10860: strcpy(gplotlabel+strlen(gplotlabel),")");
10861: fprintf(ficgp,"\n#\n");
10862: if(invalidvarcomb[k1]){
10863: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10864: continue;
10865: }
10866:
10867: fprintf(ficgp,"# hbijx=backprobability over h years, hb.jx is weighted by observed prev at destination state\n ");
10868: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres);
10869: fprintf(ficgp,"set label \"Origin alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
10870: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\
10871: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
10872:
10873: /* for (i=1; i<= nlstate+1 ; i ++){ /\* nlstate +1 p11 p21 p.1 *\/ */
10874: istart=nlstate+1; /* Could be one if by state, but nlstate+1 is w.i projection only */
10875: /*istart=1;*/ /* Could be one if by state, but nlstate+1 is w.i projection only */
10876: for (i=istart; i<= nlstate+1 ; i ++){ /* nlstate +1 p11 p21 p.1 */
10877: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
10878: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
10879: /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
10880: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
10881: if(i==istart){
10882: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"FB_"));
10883: }else{
10884: fprintf(ficgp,",\\\n '' ");
10885: }
1.351 brouard 10886: /* if(cptcoveff ==0){ /\* No covariate *\/ */
10887: if(cptcovs ==0){ /* No covariate */
1.268 brouard 10888: ioffset=2; /* Age is in 2 */
10889: /*# yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
10890: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
10891: /*# V1 = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
10892: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
10893: fprintf(ficgp," u %d:(", ioffset);
10894: if(i==nlstate+1){
1.270 brouard 10895: fprintf(ficgp," $%d/(1.-$%d)):1 t 'bw%d' with line lc variable ", \
1.268 brouard 10896: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
10897: fprintf(ficgp,",\\\n '' ");
10898: fprintf(ficgp," u %d:(",ioffset);
1.270 brouard 10899: fprintf(ficgp," (($1-$2) == %d ) ? $%d : 1/0):1 with labels center not ", \
1.268 brouard 10900: offbyear, \
10901: ioffset+(cpt-1)*(nlstate+1)+1+(i-1) );
10902: }else
10903: fprintf(ficgp," $%d/(1.-$%d)) t 'b%d%d' with line ", \
10904: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt,i );
10905: }else{ /* more than 2 covariates */
1.270 brouard 10906: ioffset=2*cptcoveff+2; /* Age is in 4 or 6 or etc.*/
10907: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
10908: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
10909: iyearc=ioffset-1;
10910: iagec=ioffset;
1.268 brouard 10911: fprintf(ficgp," u %d:(",ioffset);
10912: kl=0;
10913: strcpy(gplotcondition,"(");
1.337 brouard 10914: for (k=1; k<=cptcovs; k++){ /* For each covariate k of the resultline, get corresponding value lv for combination k1 */
1.338 brouard 10915: if(Dummy[modelresult[nres][k]]==0){ /* To be verified */
1.337 brouard 10916: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate writing the chain of conditions *\/ */
10917: /* lv= decodtabm(k1,k,cptcoveff); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
10918: /* lv= codtabm(k1,TnsdVar[Tvaraff[k]]); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
10919: lv=Tvresult[nres][k];
10920: vlv=TinvDoQresult[nres][Tvresult[nres][k]];
10921: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
10922: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
10923: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
10924: /* vlv= nbcode[Tvaraff[k]][lv]; /\* Value of the modality of Tvaraff[k] *\/ */
10925: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10926: kl++;
10927: /* sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,Tvaraff[k], kl+1, nbcode[Tvaraff[k]][lv]); */
10928: sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%lg " ,kl,Tvresult[nres][k], kl+1,TinvDoQresult[nres][Tvresult[nres][k]]);
10929: kl++;
1.338 brouard 10930: if(k <cptcovs && cptcovs>1)
1.337 brouard 10931: sprintf(gplotcondition+strlen(gplotcondition)," && ");
10932: }
1.268 brouard 10933: }
10934: strcpy(gplotcondition+strlen(gplotcondition),")");
10935: /* 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 *\/ */
10936: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
10937: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
10938: /* '' 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*/
10939: if(i==nlstate+1){
1.270 brouard 10940: fprintf(ficgp,"%s ? $%d : 1/0):%d t 'bw%d' with line lc variable", gplotcondition, \
10941: ioffset+(cpt-1)*(nlstate+1)+1+(i-1),iyearc,cpt );
1.268 brouard 10942: fprintf(ficgp,",\\\n '' ");
1.270 brouard 10943: fprintf(ficgp," u %d:(",iagec);
1.268 brouard 10944: /* fprintf(ficgp,"%s && (($5-$6) == %d ) ? $%d/(1.-$%d) : 1/0):5 with labels center not ", gplotcondition, \ */
1.270 brouard 10945: fprintf(ficgp,"%s && (($%d-$%d) == %d ) ? $%d : 1/0):%d with labels center not ", gplotcondition, \
10946: iyearc,iagec,offbyear, \
10947: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), iyearc );
1.268 brouard 10948: /* '' u 6:(($1==1 && $2==0 && $3==2 && $4==0) && (($5-$6) == 1947) ? $10/(1.-$22) : 1/0):5 with labels center boxed not*/
10949: }else{
10950: /* fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \ */
10951: fprintf(ficgp,"%s ? $%d : 1/0) t 'b%d%d' with line ", gplotcondition, \
10952: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), cpt,i );
10953: }
10954: } /* end if covariate */
10955: } /* nlstate */
10956: fprintf(ficgp,"\nset out; unset label;\n");
10957: } /* end cpt state*/
10958: } /* end covariate */
1.296 brouard 10959: } /* End if prevbcast */
1.268 brouard 10960:
1.227 brouard 10961:
1.238 brouard 10962: /* 9eme writing MLE parameters */
10963: fprintf(ficgp,"\n##############\n#9eme MLE estimated parameters\n#############\n");
1.126 brouard 10964: for(i=1,jk=1; i <=nlstate; i++){
1.187 brouard 10965: fprintf(ficgp,"# initial state %d\n",i);
1.126 brouard 10966: for(k=1; k <=(nlstate+ndeath); k++){
10967: if (k != i) {
1.227 brouard 10968: fprintf(ficgp,"# current state %d\n",k);
10969: for(j=1; j <=ncovmodel; j++){
10970: fprintf(ficgp,"p%d=%f; ",jk,p[jk]);
10971: jk++;
10972: }
10973: fprintf(ficgp,"\n");
1.126 brouard 10974: }
10975: }
1.223 brouard 10976: }
1.187 brouard 10977: fprintf(ficgp,"##############\n#\n");
1.227 brouard 10978:
1.145 brouard 10979: /*goto avoid;*/
1.238 brouard 10980: /* 10eme Graphics of probabilities or incidences using written MLE parameters */
10981: fprintf(ficgp,"\n##############\n#10eme Graphics of probabilities or incidences\n#############\n");
1.187 brouard 10982: fprintf(ficgp,"# logi(p12/p11)=a12+b12*age+c12age*age+d12*V1+e12*V1*age\n");
10983: fprintf(ficgp,"# logi(p12/p11)=p1 +p2*age +p3*age*age+ p4*V1+ p5*V1*age\n");
10984: fprintf(ficgp,"# logi(p13/p11)=a13+b13*age+c13age*age+d13*V1+e13*V1*age\n");
10985: fprintf(ficgp,"# logi(p13/p11)=p6 +p7*age +p8*age*age+ p9*V1+ p10*V1*age\n");
10986: fprintf(ficgp,"# p12+p13+p14+p11=1=p11(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
10987: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
10988: fprintf(ficgp,"# p11=1/(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
10989: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
10990: fprintf(ficgp,"# p12=exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)/\n");
10991: fprintf(ficgp,"# (1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
10992: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age))\n");
10993: fprintf(ficgp,"# +exp(a14+b14*age+c14age*age+d14*V1+e14*V1*age)+...)\n");
10994: fprintf(ficgp,"#\n");
1.223 brouard 10995: for(ng=1; ng<=3;ng++){ /* Number of graphics: first is logit, 2nd is probabilities, third is incidences per year*/
1.238 brouard 10996: fprintf(ficgp,"#Number of graphics: first is logit, 2nd is probabilities, third is incidences per year\n");
1.338 brouard 10997: fprintf(ficgp,"#model=1+age+%s \n",model);
1.238 brouard 10998: fprintf(ficgp,"# Type of graphic ng=%d\n",ng);
1.351 brouard 10999: /* fprintf(ficgp,"# k1=1 to 2^%d=%d\n",cptcoveff,m);/\* to be checked *\/ */
11000: fprintf(ficgp,"# k1=1 to 2^%d=%d\n",cptcovs,m);/* to be checked */
1.337 brouard 11001: /* for(k1=1; k1 <=m; k1++) /\* For each combination of covariate *\/ */
1.237 brouard 11002: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 11003: /* k1=nres; */
1.338 brouard 11004: k1=TKresult[nres];
11005: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.337 brouard 11006: fprintf(ficgp,"\n\n# Resultline k1=%d ",k1);
1.264 brouard 11007: strcpy(gplotlabel,"(");
1.276 brouard 11008: /*sprintf(gplotlabel+strlen(gplotlabel)," Dummy combination %d ",k1);*/
1.337 brouard 11009: for (k=1; k<=cptcovs; k++){ /**< cptcovs number of SIMPLE covariates in the model V2+V1 =2 (dummy or quantit or time varying) */
11010: /* for each resultline nres, and position k, Tvresult[nres][k] gives the name of the variable and
11011: TinvDoQresult[nres][Tvresult[nres][k]] gives its value double or integer) */
11012: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
11013: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
11014: }
11015: /* if(m != 1 && TKresult[nres]!= k1) */
11016: /* continue; */
11017: /* fprintf(ficgp,"\n\n# Combination of dummy k1=%d which is ",k1); */
11018: /* strcpy(gplotlabel,"("); */
11019: /* /\*sprintf(gplotlabel+strlen(gplotlabel)," Dummy combination %d ",k1);*\/ */
11020: /* for (k=1; k<=cptcoveff; k++){ /\* For each correspondig covariate value *\/ */
11021: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate value corresponding to k1 combination and kth covariate *\\/ *\/ */
11022: /* lv= codtabm(k1,TnsdVar[Tvaraff[k]]); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
11023: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
11024: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
11025: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
11026: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
11027: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
11028: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
11029: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
11030: /* } */
11031: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
11032: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
11033: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
11034: /* } */
1.264 brouard 11035: strcpy(gplotlabel+strlen(gplotlabel),")");
1.237 brouard 11036: fprintf(ficgp,"\n#\n");
1.264 brouard 11037: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" ",subdirf2(optionfilefiname,"PE_"),k1,ng,nres);
1.276 brouard 11038: fprintf(ficgp,"\nset key outside ");
11039: /* fprintf(ficgp,"\nset label \"%s\" at graph 1.2,0.5 center rotate font \"Helvetica,12\"\n",gplotlabel); */
11040: fprintf(ficgp,"\nset title \"%s\" font \"Helvetica,12\"\n",gplotlabel);
1.223 brouard 11041: fprintf(ficgp,"\nset ter svg size 640, 480 ");
11042: if (ng==1){
11043: fprintf(ficgp,"\nset ylabel \"Value of the logit of the model\"\n"); /* exp(a12+b12*x) could be nice */
11044: fprintf(ficgp,"\nunset log y");
11045: }else if (ng==2){
11046: fprintf(ficgp,"\nset ylabel \"Probability\"\n");
11047: fprintf(ficgp,"\nset log y");
11048: }else if (ng==3){
11049: fprintf(ficgp,"\nset ylabel \"Quasi-incidence per year\"\n");
11050: fprintf(ficgp,"\nset log y");
11051: }else
11052: fprintf(ficgp,"\nunset title ");
11053: fprintf(ficgp,"\nplot [%.f:%.f] ",ageminpar,agemaxpar);
11054: i=1;
11055: for(k2=1; k2<=nlstate; k2++) {
11056: k3=i;
11057: for(k=1; k<=(nlstate+ndeath); k++) {
11058: if (k != k2){
11059: switch( ng) {
11060: case 1:
11061: if(nagesqr==0)
11062: fprintf(ficgp," p%d+p%d*x",i,i+1);
11063: else /* nagesqr =1 */
11064: fprintf(ficgp," p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
11065: break;
11066: case 2: /* ng=2 */
11067: if(nagesqr==0)
11068: fprintf(ficgp," exp(p%d+p%d*x",i,i+1);
11069: else /* nagesqr =1 */
11070: fprintf(ficgp," exp(p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
11071: break;
11072: case 3:
11073: if(nagesqr==0)
11074: fprintf(ficgp," %f*exp(p%d+p%d*x",YEARM/stepm,i,i+1);
11075: else /* nagesqr =1 */
11076: fprintf(ficgp," %f*exp(p%d+p%d*x+p%d*x*x",YEARM/stepm,i,i+1,i+1+nagesqr);
11077: break;
11078: }
11079: ij=1;/* To be checked else nbcode[0][0] wrong */
1.237 brouard 11080: ijp=1; /* product no age */
11081: /* for(j=3; j <=ncovmodel-nagesqr; j++) { */
11082: for(j=1; j <=cptcovt; j++) { /* For each covariate of the simplified model */
1.223 brouard 11083: /* printf("Tage[%d]=%d, j=%d\n", ij, Tage[ij], j); */
1.329 brouard 11084: switch(Typevar[j]){
11085: case 1:
11086: if(cptcovage >0){ /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, 2 V5 and V1 */
11087: if(j==Tage[ij]) { /* Product by age To be looked at!!*//* Bug valgrind */
11088: if(ij <=cptcovage) { /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, 2 V5 and V1 */
11089: if(DummyV[j]==0){/* Bug valgrind */
11090: fprintf(ficgp,"+p%d*%d*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]);;
11091: }else{ /* quantitative */
11092: fprintf(ficgp,"+p%d*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /* Tqinvresult in decoderesult */
11093: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
11094: }
11095: ij++;
1.268 brouard 11096: }
1.237 brouard 11097: }
1.329 brouard 11098: }
11099: break;
11100: case 2:
11101: if(cptcovprod >0){
11102: if(j==Tprod[ijp]) { /* */
11103: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
11104: if(ijp <=cptcovprod) { /* Product */
11105: if(DummyV[Tvard[ijp][1]]==0){/* Vn is dummy */
11106: if(DummyV[Tvard[ijp][2]]==0){/* Vn and Vm are dummy */
11107: /* 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)]); */
11108: fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]);
11109: }else{ /* Vn is dummy and Vm is quanti */
11110: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],Tqinvresult[nres][Tvard[ijp][2]]); */
11111: fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
11112: }
11113: }else{ /* Vn*Vm Vn is quanti */
11114: if(DummyV[Tvard[ijp][2]]==0){
11115: fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]);
11116: }else{ /* Both quanti */
11117: fprintf(ficgp,"+p%d*%f*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
11118: }
1.268 brouard 11119: }
1.329 brouard 11120: ijp++;
1.237 brouard 11121: }
1.329 brouard 11122: } /* end Tprod */
11123: }
11124: break;
1.349 brouard 11125: case 3:
11126: if(cptcovdageprod >0){
11127: /* if(j==Tprod[ijp]) { */ /* not necessary */
11128: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
1.350 brouard 11129: if(ijp <=cptcovprod) { /* Product Vn*Vm and age*VN*Vm*/
11130: if(DummyV[Tvardk[ijp][1]]==0){/* Vn is dummy */
11131: if(DummyV[Tvardk[ijp][2]]==0){/* Vn and Vm are dummy */
1.349 brouard 11132: /* 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)]); */
11133: fprintf(ficgp,"+p%d*%d*%d*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]);
11134: }else{ /* Vn is dummy and Vm is quanti */
11135: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],Tqinvresult[nres][Tvard[ijp][2]]); */
1.350 brouard 11136: fprintf(ficgp,"+p%d*%d*%f*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvardk[ijp][1]],Tqinvresult[nres][Tvardk[ijp][2]]);
1.349 brouard 11137: }
1.350 brouard 11138: }else{ /* age* Vn*Vm Vn is quanti HERE */
1.349 brouard 11139: if(DummyV[Tvard[ijp][2]]==0){
1.350 brouard 11140: fprintf(ficgp,"+p%d*%d*%f*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvardk[ijp][2]],Tqinvresult[nres][Tvardk[ijp][1]]);
1.349 brouard 11141: }else{ /* Both quanti */
1.350 brouard 11142: fprintf(ficgp,"+p%d*%f*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvardk[ijp][1]],Tqinvresult[nres][Tvardk[ijp][2]]);
1.349 brouard 11143: }
11144: }
11145: ijp++;
11146: }
11147: /* } */ /* end Tprod */
11148: }
11149: break;
1.329 brouard 11150: case 0:
11151: /* simple covariate */
1.264 brouard 11152: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,nbcode[Tvar[j]][codtabm(k1,j)]); /\* Valgrind bug nbcode *\/ */
1.237 brouard 11153: if(Dummy[j]==0){
11154: fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]); /* */
11155: }else{ /* quantitative */
11156: fprintf(ficgp,"+p%d*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /* */
1.264 brouard 11157: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
1.223 brouard 11158: }
1.329 brouard 11159: /* end simple */
11160: break;
11161: default:
11162: break;
11163: } /* end switch */
1.237 brouard 11164: } /* end j */
1.329 brouard 11165: }else{ /* k=k2 */
11166: if(ng !=1 ){ /* For logit formula of log p11 is more difficult to get */
11167: fprintf(ficgp," (1.");i=i-ncovmodel;
11168: }else
11169: i=i-ncovmodel;
1.223 brouard 11170: }
1.227 brouard 11171:
1.223 brouard 11172: if(ng != 1){
11173: fprintf(ficgp,")/(1");
1.227 brouard 11174:
1.264 brouard 11175: for(cpt=1; cpt <=nlstate; cpt++){
1.223 brouard 11176: if(nagesqr==0)
1.264 brouard 11177: fprintf(ficgp,"+exp(p%d+p%d*x",k3+(cpt-1)*ncovmodel,k3+(cpt-1)*ncovmodel+1);
1.223 brouard 11178: else /* nagesqr =1 */
1.264 brouard 11179: 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);
1.217 brouard 11180:
1.223 brouard 11181: ij=1;
1.329 brouard 11182: ijp=1;
11183: /* for(j=3; j <=ncovmodel-nagesqr; j++){ */
11184: for(j=1; j <=cptcovt; j++) { /* For each covariate of the simplified model */
11185: switch(Typevar[j]){
11186: case 1:
11187: if(cptcovage >0){
11188: if(j==Tage[ij]) { /* Bug valgrind */
11189: if(ij <=cptcovage) { /* Bug valgrind */
11190: if(DummyV[j]==0){/* Bug valgrind */
11191: /* fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,j-2)]); */
11192: /* fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j+nagesqr,nbcode[Tvar[j]][codtabm(k1,j)]); */
11193: fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvar[j]]);
11194: /* fprintf(ficgp,"+p%d*%d*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]);; */
11195: /* fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
11196: }else{ /* quantitative */
11197: /* fprintf(ficgp,"+p%d*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /\* Tqinvresult in decoderesult *\/ */
11198: fprintf(ficgp,"+p%d*%f*x",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tqinvresult[nres][Tvar[j]]); /* Tqinvresult in decoderesult */
11199: /* fprintf(ficgp,"+p%d*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /\* Tqinvresult in decoderesult *\/ */
11200: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
11201: }
11202: ij++;
11203: }
11204: }
11205: }
11206: break;
11207: case 2:
11208: if(cptcovprod >0){
11209: if(j==Tprod[ijp]) { /* */
11210: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
11211: if(ijp <=cptcovprod) { /* Product */
11212: if(DummyV[Tvard[ijp][1]]==0){/* Vn is dummy */
11213: if(DummyV[Tvard[ijp][2]]==0){/* Vn and Vm are dummy */
11214: /* 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)]); */
11215: fprintf(ficgp,"+p%d*%d*%d",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]);
11216: /* fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]); */
11217: }else{ /* Vn is dummy and Vm is quanti */
11218: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],Tqinvresult[nres][Tvard[ijp][2]]); */
11219: fprintf(ficgp,"+p%d*%d*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
11220: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]); */
11221: }
11222: }else{ /* Vn*Vm Vn is quanti */
11223: if(DummyV[Tvard[ijp][2]]==0){
11224: fprintf(ficgp,"+p%d*%d*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]);
11225: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]); */
11226: }else{ /* Both quanti */
11227: fprintf(ficgp,"+p%d*%f*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
11228: /* fprintf(ficgp,"+p%d*%f*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]); */
11229: }
11230: }
11231: ijp++;
11232: }
11233: } /* end Tprod */
11234: } /* end if */
11235: break;
1.349 brouard 11236: case 3:
11237: if(cptcovdageprod >0){
11238: /* if(j==Tprod[ijp]) { /\* *\/ */
11239: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
11240: if(ijp <=cptcovprod) { /* Product */
1.350 brouard 11241: if(DummyV[Tvardk[ijp][1]]==0){/* Vn is dummy */
11242: if(DummyV[Tvardk[ijp][2]]==0){/* Vn and Vm are dummy */
1.349 brouard 11243: /* 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)]); */
1.350 brouard 11244: fprintf(ficgp,"+p%d*%d*%d*x",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvardk[ijp][1]],Tinvresult[nres][Tvardk[ijp][2]]);
1.349 brouard 11245: /* fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]); */
11246: }else{ /* Vn is dummy and Vm is quanti */
11247: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],Tqinvresult[nres][Tvard[ijp][2]]); */
1.350 brouard 11248: fprintf(ficgp,"+p%d*%d*%f*x",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvardk[ijp][1]],Tqinvresult[nres][Tvardk[ijp][2]]);
1.349 brouard 11249: /* fprintf(ficgp,"+p%d*%d*%f*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]); */
11250: }
11251: }else{ /* Vn*Vm Vn is quanti */
1.350 brouard 11252: if(DummyV[Tvardk[ijp][2]]==0){
11253: fprintf(ficgp,"+p%d*%d*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvardk[ijp][2]],Tqinvresult[nres][Tvardk[ijp][1]]);
1.349 brouard 11254: /* fprintf(ficgp,"+p%d*%d*%f*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]); */
11255: }else{ /* Both quanti */
1.350 brouard 11256: fprintf(ficgp,"+p%d*%f*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tqinvresult[nres][Tvardk[ijp][1]],Tqinvresult[nres][Tvardk[ijp][2]]);
1.349 brouard 11257: /* fprintf(ficgp,"+p%d*%f*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]); */
11258: }
11259: }
11260: ijp++;
11261: }
11262: /* } /\* end Tprod *\/ */
11263: } /* end if */
11264: break;
1.329 brouard 11265: case 0:
11266: /* simple covariate */
11267: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,nbcode[Tvar[j]][codtabm(k1,j)]); /\* Valgrind bug nbcode *\/ */
11268: if(Dummy[j]==0){
11269: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]); /\* *\/ */
11270: fprintf(ficgp,"+p%d*%d",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvar[j]]); /* */
11271: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]); /\* *\/ */
11272: }else{ /* quantitative */
11273: fprintf(ficgp,"+p%d*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tqinvresult[nres][Tvar[j]]); /* */
11274: /* fprintf(ficgp,"+p%d*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /\* *\/ */
11275: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
11276: }
11277: /* end simple */
11278: /* fprintf(ficgp,"+p%d*%d",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,j-2)]);/\* Valgrind bug nbcode *\/ */
11279: break;
11280: default:
11281: break;
11282: } /* end switch */
1.223 brouard 11283: }
11284: fprintf(ficgp,")");
11285: }
11286: fprintf(ficgp,")");
11287: if(ng ==2)
1.276 brouard 11288: 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);
1.223 brouard 11289: else /* ng= 3 */
1.276 brouard 11290: 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);
1.329 brouard 11291: }else{ /* end ng <> 1 */
1.223 brouard 11292: if( k !=k2) /* logit p11 is hard to draw */
1.276 brouard 11293: 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);
1.223 brouard 11294: }
11295: if ((k+k2)!= (nlstate*2+ndeath) && ng != 1)
11296: fprintf(ficgp,",");
11297: if (ng == 1 && k!=k2 && (k+k2)!= (nlstate*2+ndeath))
11298: fprintf(ficgp,",");
11299: i=i+ncovmodel;
11300: } /* end k */
11301: } /* end k2 */
1.276 brouard 11302: /* fprintf(ficgp,"\n set out; unset label;set key default;\n"); */
11303: fprintf(ficgp,"\n set out; unset title;set key default;\n");
1.337 brouard 11304: } /* end resultline */
1.223 brouard 11305: } /* end ng */
11306: /* avoid: */
11307: fflush(ficgp);
1.126 brouard 11308: } /* end gnuplot */
11309:
11310:
11311: /*************** Moving average **************/
1.219 brouard 11312: /* int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav, double bageout, double fageout){ */
1.222 brouard 11313: int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav){
1.218 brouard 11314:
1.222 brouard 11315: int i, cpt, cptcod;
11316: int modcovmax =1;
11317: int mobilavrange, mob;
11318: int iage=0;
1.288 brouard 11319: int firstA1=0, firstA2=0;
1.222 brouard 11320:
1.266 brouard 11321: double sum=0., sumr=0.;
1.222 brouard 11322: double age;
1.266 brouard 11323: double *sumnewp, *sumnewm, *sumnewmr;
11324: double *agemingood, *agemaxgood;
11325: double *agemingoodr, *agemaxgoodr;
1.222 brouard 11326:
11327:
1.278 brouard 11328: /* modcovmax=2*cptcoveff; Max number of modalities. We suppose */
11329: /* a covariate has 2 modalities, should be equal to ncovcombmax */
1.222 brouard 11330:
11331: sumnewp = vector(1,ncovcombmax);
11332: sumnewm = vector(1,ncovcombmax);
1.266 brouard 11333: sumnewmr = vector(1,ncovcombmax);
1.222 brouard 11334: agemingood = vector(1,ncovcombmax);
1.266 brouard 11335: agemingoodr = vector(1,ncovcombmax);
1.222 brouard 11336: agemaxgood = vector(1,ncovcombmax);
1.266 brouard 11337: agemaxgoodr = vector(1,ncovcombmax);
1.222 brouard 11338:
11339: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
1.266 brouard 11340: sumnewm[cptcod]=0.; sumnewmr[cptcod]=0.;
1.222 brouard 11341: sumnewp[cptcod]=0.;
1.266 brouard 11342: agemingood[cptcod]=0, agemingoodr[cptcod]=0;
11343: agemaxgood[cptcod]=0, agemaxgoodr[cptcod]=0;
1.222 brouard 11344: }
11345: if (cptcovn<1) ncovcombmax=1; /* At least 1 pass */
11346:
1.266 brouard 11347: if(mobilav==-1 || mobilav==1||mobilav ==3 ||mobilav==5 ||mobilav== 7){
11348: if(mobilav==1 || mobilav==-1) mobilavrange=5; /* default */
1.222 brouard 11349: else mobilavrange=mobilav;
11350: for (age=bage; age<=fage; age++)
11351: for (i=1; i<=nlstate;i++)
11352: for (cptcod=1;cptcod<=ncovcombmax;cptcod++)
11353: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
11354: /* We keep the original values on the extreme ages bage, fage and for
11355: fage+1 and bage-1 we use a 3 terms moving average; for fage+2 bage+2
11356: we use a 5 terms etc. until the borders are no more concerned.
11357: */
11358: for (mob=3;mob <=mobilavrange;mob=mob+2){
11359: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){
1.266 brouard 11360: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
11361: sumnewm[cptcod]=0.;
11362: for (i=1; i<=nlstate;i++){
1.222 brouard 11363: mobaverage[(int)age][i][cptcod] =probs[(int)age][i][cptcod];
11364: for (cpt=1;cpt<=(mob-1)/2;cpt++){
11365: mobaverage[(int)age][i][cptcod] +=probs[(int)age-cpt][i][cptcod];
11366: mobaverage[(int)age][i][cptcod] +=probs[(int)age+cpt][i][cptcod];
11367: }
11368: mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/mob;
1.266 brouard 11369: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
11370: } /* end i */
11371: if(sumnewm[cptcod] >1.e-3) mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/sumnewm[cptcod]; /* Rescaling to sum one */
11372: } /* end cptcod */
1.222 brouard 11373: }/* end age */
11374: }/* end mob */
1.266 brouard 11375: }else{
11376: printf("Error internal in movingaverage, mobilav=%d.\n",mobilav);
1.222 brouard 11377: return -1;
1.266 brouard 11378: }
11379:
11380: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){ /* for each combination */
1.222 brouard 11381: /* for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ */
11382: if(invalidvarcomb[cptcod]){
11383: printf("\nCombination (%d) ignored because no cases \n",cptcod);
11384: continue;
11385: }
1.219 brouard 11386:
1.266 brouard 11387: for (age=fage-(mob-1)/2; age>=bage+(mob-1)/2; age--){ /*looking for the youngest and oldest good age */
11388: sumnewm[cptcod]=0.;
11389: sumnewmr[cptcod]=0.;
11390: for (i=1; i<=nlstate;i++){
11391: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
11392: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
11393: }
11394: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
11395: agemingoodr[cptcod]=age;
11396: }
11397: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
11398: agemingood[cptcod]=age;
11399: }
11400: } /* age */
11401: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ /*looking for the youngest and oldest good age */
1.222 brouard 11402: sumnewm[cptcod]=0.;
1.266 brouard 11403: sumnewmr[cptcod]=0.;
1.222 brouard 11404: for (i=1; i<=nlstate;i++){
11405: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
1.266 brouard 11406: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
11407: }
11408: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
11409: agemaxgoodr[cptcod]=age;
1.222 brouard 11410: }
11411: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
1.266 brouard 11412: agemaxgood[cptcod]=age;
11413: }
11414: } /* age */
11415: /* Thus we have agemingood and agemaxgood as well as goodr for raw (preobs) */
11416: /* but they will change */
1.288 brouard 11417: firstA1=0;firstA2=0;
1.266 brouard 11418: for (age=fage-(mob-1)/2; age>=bage; age--){/* From oldest to youngest, filling up to the youngest */
11419: sumnewm[cptcod]=0.;
11420: sumnewmr[cptcod]=0.;
11421: for (i=1; i<=nlstate;i++){
11422: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
11423: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
11424: }
11425: if(mobilav==-1){ /* Forcing raw ages if good else agemingood */
11426: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
11427: agemaxgoodr[cptcod]=age; /* age min */
11428: for (i=1; i<=nlstate;i++)
11429: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
11430: }else{ /* bad we change the value with the values of good ages */
11431: for (i=1; i<=nlstate;i++){
11432: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgoodr[cptcod]][i][cptcod];
11433: } /* i */
11434: } /* end bad */
11435: }else{
11436: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
11437: agemaxgood[cptcod]=age;
11438: }else{ /* bad we change the value with the values of good ages */
11439: for (i=1; i<=nlstate;i++){
11440: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
11441: } /* i */
11442: } /* end bad */
11443: }/* end else */
11444: sum=0.;sumr=0.;
11445: for (i=1; i<=nlstate;i++){
11446: sum+=mobaverage[(int)age][i][cptcod];
11447: sumr+=probs[(int)age][i][cptcod];
11448: }
11449: if(fabs(sum - 1.) > 1.e-3) { /* bad */
1.288 brouard 11450: if(!firstA1){
11451: firstA1=1;
11452: 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);
11453: }
11454: 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);
1.266 brouard 11455: } /* end bad */
11456: /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */
11457: if(fabs(sumr - 1.) > 1.e-3) { /* bad */
1.288 brouard 11458: if(!firstA2){
11459: firstA2=1;
11460: 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);
11461: }
11462: 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);
1.222 brouard 11463: } /* end bad */
11464: }/* age */
1.266 brouard 11465:
11466: for (age=bage+(mob-1)/2; age<=fage; age++){/* From youngest, finding the oldest wrong */
1.222 brouard 11467: sumnewm[cptcod]=0.;
1.266 brouard 11468: sumnewmr[cptcod]=0.;
1.222 brouard 11469: for (i=1; i<=nlstate;i++){
11470: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
1.266 brouard 11471: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
11472: }
11473: if(mobilav==-1){ /* Forcing raw ages if good else agemingood */
11474: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good */
11475: agemingoodr[cptcod]=age;
11476: for (i=1; i<=nlstate;i++)
11477: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
11478: }else{ /* bad we change the value with the values of good ages */
11479: for (i=1; i<=nlstate;i++){
11480: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingoodr[cptcod]][i][cptcod];
11481: } /* i */
11482: } /* end bad */
11483: }else{
11484: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
11485: agemingood[cptcod]=age;
11486: }else{ /* bad */
11487: for (i=1; i<=nlstate;i++){
11488: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod];
11489: } /* i */
11490: } /* end bad */
11491: }/* end else */
11492: sum=0.;sumr=0.;
11493: for (i=1; i<=nlstate;i++){
11494: sum+=mobaverage[(int)age][i][cptcod];
11495: sumr+=mobaverage[(int)age][i][cptcod];
1.222 brouard 11496: }
1.266 brouard 11497: if(fabs(sum - 1.) > 1.e-3) { /* bad */
1.268 brouard 11498: 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);
1.266 brouard 11499: } /* end bad */
11500: /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */
11501: if(fabs(sumr - 1.) > 1.e-3) { /* bad */
1.268 brouard 11502: 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);
1.222 brouard 11503: } /* end bad */
11504: }/* age */
1.266 brouard 11505:
1.222 brouard 11506:
11507: for (age=bage; age<=fage; age++){
1.235 brouard 11508: /* printf("%d %d ", cptcod, (int)age); */
1.222 brouard 11509: sumnewp[cptcod]=0.;
11510: sumnewm[cptcod]=0.;
11511: for (i=1; i<=nlstate;i++){
11512: sumnewp[cptcod]+=probs[(int)age][i][cptcod];
11513: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
11514: /* printf("%.4f %.4f ",probs[(int)age][i][cptcod], mobaverage[(int)age][i][cptcod]); */
11515: }
11516: /* printf("%.4f %.4f \n",sumnewp[cptcod], sumnewm[cptcod]); */
11517: }
11518: /* printf("\n"); */
11519: /* } */
1.266 brouard 11520:
1.222 brouard 11521: /* brutal averaging */
1.266 brouard 11522: /* for (i=1; i<=nlstate;i++){ */
11523: /* for (age=1; age<=bage; age++){ */
11524: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod]; */
11525: /* /\* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); *\/ */
11526: /* } */
11527: /* for (age=fage; age<=AGESUP; age++){ */
11528: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod]; */
11529: /* /\* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); *\/ */
11530: /* } */
11531: /* } /\* end i status *\/ */
11532: /* for (i=nlstate+1; i<=nlstate+ndeath;i++){ */
11533: /* for (age=1; age<=AGESUP; age++){ */
11534: /* /\*printf("i=%d, age=%d, cptcod=%d\n",i, (int)age, cptcod);*\/ */
11535: /* mobaverage[(int)age][i][cptcod]=0.; */
11536: /* } */
11537: /* } */
1.222 brouard 11538: }/* end cptcod */
1.266 brouard 11539: free_vector(agemaxgoodr,1, ncovcombmax);
11540: free_vector(agemaxgood,1, ncovcombmax);
11541: free_vector(agemingood,1, ncovcombmax);
11542: free_vector(agemingoodr,1, ncovcombmax);
11543: free_vector(sumnewmr,1, ncovcombmax);
1.222 brouard 11544: free_vector(sumnewm,1, ncovcombmax);
11545: free_vector(sumnewp,1, ncovcombmax);
11546: return 0;
11547: }/* End movingaverage */
1.218 brouard 11548:
1.126 brouard 11549:
1.296 brouard 11550:
1.126 brouard 11551: /************** Forecasting ******************/
1.296 brouard 11552: /* 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)*/
11553: 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){
11554: /* dateintemean, mean date of interviews
11555: dateprojd, year, month, day of starting projection
11556: dateprojf date of end of projection;year of end of projection (same day and month as proj1).
1.126 brouard 11557: agemin, agemax range of age
11558: dateprev1 dateprev2 range of dates during which prevalence is computed
11559: */
1.296 brouard 11560: /* double anprojd, mprojd, jprojd; */
11561: /* double anprojf, mprojf, jprojf; */
1.359 brouard 11562: int yearp, stepsize, hstepm, nhstepm, j, k, i, h, nres=0;
1.126 brouard 11563: double agec; /* generic age */
1.359 brouard 11564: double agelim, ppij;
11565: /*double *popcount;*/
1.126 brouard 11566: double ***p3mat;
1.218 brouard 11567: /* double ***mobaverage; */
1.126 brouard 11568: char fileresf[FILENAMELENGTH];
11569:
11570: agelim=AGESUP;
1.211 brouard 11571: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
11572: in each health status at the date of interview (if between dateprev1 and dateprev2).
11573: We still use firstpass and lastpass as another selection.
11574: */
1.214 brouard 11575: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ */
11576: /* firstpass, lastpass, stepm, weightopt, model); */
1.126 brouard 11577:
1.201 brouard 11578: strcpy(fileresf,"F_");
11579: strcat(fileresf,fileresu);
1.126 brouard 11580: if((ficresf=fopen(fileresf,"w"))==NULL) {
11581: printf("Problem with forecast resultfile: %s\n", fileresf);
11582: fprintf(ficlog,"Problem with forecast resultfile: %s\n", fileresf);
11583: }
1.235 brouard 11584: printf("\nComputing forecasting: result on file '%s', please wait... \n", fileresf);
11585: fprintf(ficlog,"\nComputing forecasting: result on file '%s', please wait... \n", fileresf);
1.126 brouard 11586:
1.225 brouard 11587: if (cptcoveff==0) ncodemax[cptcoveff]=1;
1.126 brouard 11588:
11589:
11590: stepsize=(int) (stepm+YEARM-1)/YEARM;
11591: if (stepm<=12) stepsize=1;
11592: if(estepm < stepm){
11593: printf ("Problem %d lower than %d\n",estepm, stepm);
11594: }
1.270 brouard 11595: else{
11596: hstepm=estepm;
11597: }
11598: if(estepm > stepm){ /* Yes every two year */
11599: stepsize=2;
11600: }
1.296 brouard 11601: hstepm=hstepm/stepm;
1.126 brouard 11602:
1.296 brouard 11603:
11604: /* yp1=modf(dateintmean,&yp);/\* extracts integral of datemean in yp and */
11605: /* fractional in yp1 *\/ */
11606: /* aintmean=yp; */
11607: /* yp2=modf((yp1*12),&yp); */
11608: /* mintmean=yp; */
11609: /* yp1=modf((yp2*30.5),&yp); */
11610: /* jintmean=yp; */
11611: /* if(jintmean==0) jintmean=1; */
11612: /* if(mintmean==0) mintmean=1; */
1.126 brouard 11613:
1.296 brouard 11614:
11615: /* date2dmy(dateintmean,&jintmean,&mintmean,&aintmean); */
11616: /* date2dmy(dateprojd,&jprojd, &mprojd, &anprojd); */
11617: /* date2dmy(dateprojf,&jprojf, &mprojf, &anprojf); */
1.351 brouard 11618: /* i1=pow(2,cptcoveff); */
11619: /* if (cptcovn < 1){i1=1;} */
1.126 brouard 11620:
1.296 brouard 11621: fprintf(ficresf,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
1.126 brouard 11622:
11623: fprintf(ficresf,"#****** Routine prevforecast **\n");
1.227 brouard 11624:
1.126 brouard 11625: /* if (h==(int)(YEARM*yearp)){ */
1.351 brouard 11626: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
11627: k=TKresult[nres];
11628: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
11629: /* 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) *\/ */
11630: /* if(i1 != 1 && TKresult[nres]!= k) */
11631: /* continue; */
11632: /* if(invalidvarcomb[k]){ */
11633: /* printf("\nCombination (%d) projection ignored because no cases \n",k); */
11634: /* continue; */
11635: /* } */
1.227 brouard 11636: fprintf(ficresf,"\n#****** hpijx=probability over h years, hp.jx is weighted by observed prev \n#");
1.351 brouard 11637: for(j=1;j<=cptcovs;j++){
11638: /* for(j=1;j<=cptcoveff;j++) { */
11639: /* /\* fprintf(ficresf," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,Tvaraff[j])]); *\/ */
11640: /* fprintf(ficresf," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11641: /* } */
11642: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
11643: /* fprintf(ficresf," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
11644: /* } */
11645: fprintf(ficresf," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
1.235 brouard 11646: }
1.351 brouard 11647:
1.227 brouard 11648: fprintf(ficresf," yearproj age");
11649: for(j=1; j<=nlstate+ndeath;j++){
11650: for(i=1; i<=nlstate;i++)
11651: fprintf(ficresf," p%d%d",i,j);
11652: fprintf(ficresf," wp.%d",j);
11653: }
1.296 brouard 11654: for (yearp=0; yearp<=(anprojf-anprojd);yearp +=stepsize) {
1.227 brouard 11655: fprintf(ficresf,"\n");
1.296 brouard 11656: fprintf(ficresf,"\n# Forecasting at date %.lf/%.lf/%.lf ",jprojd,mprojd,anprojd+yearp);
1.270 brouard 11657: /* for (agec=fage; agec>=(ageminpar-1); agec--){ */
11658: for (agec=fage; agec>=(bage); agec--){
1.227 brouard 11659: nhstepm=(int) rint((agelim-agec)*YEARM/stepm);
11660: nhstepm = nhstepm/hstepm;
11661: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
11662: oldm=oldms;savm=savms;
1.268 brouard 11663: /* We compute pii at age agec over nhstepm);*/
1.235 brouard 11664: hpxij(p3mat,nhstepm,agec,hstepm,p,nlstate,stepm,oldm,savm, k,nres);
1.268 brouard 11665: /* Then we print p3mat for h corresponding to the right agec+h*stepms=yearp */
1.227 brouard 11666: for (h=0; h<=nhstepm; h++){
11667: if (h*hstepm/YEARM*stepm ==yearp) {
1.268 brouard 11668: break;
11669: }
11670: }
11671: fprintf(ficresf,"\n");
1.351 brouard 11672: /* for(j=1;j<=cptcoveff;j++) */
11673: for(j=1;j<=cptcovs;j++)
11674: fprintf(ficresf,"%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
1.332 brouard 11675: /* fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,Tvaraff[j])]); /\* Tvaraff not correct *\/ */
1.351 brouard 11676: /* fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); /\* TnsdVar[Tvaraff] correct *\/ */
1.296 brouard 11677: fprintf(ficresf,"%.f %.f ",anprojd+yearp,agec+h*hstepm/YEARM*stepm);
1.268 brouard 11678:
11679: for(j=1; j<=nlstate+ndeath;j++) {
11680: ppij=0.;
11681: for(i=1; i<=nlstate;i++) {
1.278 brouard 11682: if (mobilav>=1)
11683: ppij=ppij+p3mat[i][j][h]*prev[(int)agec][i][k];
11684: else { /* even if mobilav==-1 we use mobaverage, probs may not sums to 1 */
11685: ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][k];
11686: }
1.268 brouard 11687: fprintf(ficresf," %.3f", p3mat[i][j][h]);
11688: } /* end i */
11689: fprintf(ficresf," %.3f", ppij);
11690: }/* end j */
1.227 brouard 11691: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
11692: } /* end agec */
1.266 brouard 11693: /* diffyear=(int) anproj1+yearp-ageminpar-1; */
11694: /*printf("Prevforecast %d+%d-%d=diffyear=%d\n",(int) anproj1, (int)yearp,(int)ageminpar,(int) anproj1-(int)ageminpar);*/
1.227 brouard 11695: } /* end yearp */
11696: } /* end k */
1.219 brouard 11697:
1.126 brouard 11698: fclose(ficresf);
1.215 brouard 11699: printf("End of Computing forecasting \n");
11700: fprintf(ficlog,"End of Computing forecasting\n");
11701:
1.126 brouard 11702: }
11703:
1.269 brouard 11704: /************** Back Forecasting ******************/
1.296 brouard 11705: /* 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){ */
11706: 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){
11707: /* back1, year, month, day of starting backprojection
1.267 brouard 11708: agemin, agemax range of age
11709: dateprev1 dateprev2 range of dates during which prevalence is computed
1.269 brouard 11710: anback2 year of end of backprojection (same day and month as back1).
11711: prevacurrent and prev are prevalences.
1.267 brouard 11712: */
1.359 brouard 11713: int yearp, stepsize, hstepm, nhstepm, j, k, i, h, nres=0;
1.267 brouard 11714: double agec; /* generic age */
1.359 brouard 11715: double agelim, ppij, ppi; /* ,jintmean,mintmean,aintmean;*/
11716: /*double *popcount;*/
1.267 brouard 11717: double ***p3mat;
11718: /* double ***mobaverage; */
11719: char fileresfb[FILENAMELENGTH];
11720:
1.268 brouard 11721: agelim=AGEINF;
1.267 brouard 11722: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
11723: in each health status at the date of interview (if between dateprev1 and dateprev2).
11724: We still use firstpass and lastpass as another selection.
11725: */
11726: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ */
11727: /* firstpass, lastpass, stepm, weightopt, model); */
11728:
11729: /*Do we need to compute prevalence again?*/
11730:
11731: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
11732:
11733: strcpy(fileresfb,"FB_");
11734: strcat(fileresfb,fileresu);
11735: if((ficresfb=fopen(fileresfb,"w"))==NULL) {
11736: printf("Problem with back forecast resultfile: %s\n", fileresfb);
11737: fprintf(ficlog,"Problem with back forecast resultfile: %s\n", fileresfb);
11738: }
11739: printf("\nComputing back forecasting: result on file '%s', please wait... \n", fileresfb);
11740: fprintf(ficlog,"\nComputing back forecasting: result on file '%s', please wait... \n", fileresfb);
11741:
11742: if (cptcoveff==0) ncodemax[cptcoveff]=1;
11743:
11744:
11745: stepsize=(int) (stepm+YEARM-1)/YEARM;
11746: if (stepm<=12) stepsize=1;
11747: if(estepm < stepm){
11748: printf ("Problem %d lower than %d\n",estepm, stepm);
11749: }
1.270 brouard 11750: else{
11751: hstepm=estepm;
11752: }
11753: if(estepm >= stepm){ /* Yes every two year */
11754: stepsize=2;
11755: }
1.267 brouard 11756:
11757: hstepm=hstepm/stepm;
1.296 brouard 11758: /* yp1=modf(dateintmean,&yp);/\* extracts integral of datemean in yp and */
11759: /* fractional in yp1 *\/ */
11760: /* aintmean=yp; */
11761: /* yp2=modf((yp1*12),&yp); */
11762: /* mintmean=yp; */
11763: /* yp1=modf((yp2*30.5),&yp); */
11764: /* jintmean=yp; */
11765: /* if(jintmean==0) jintmean=1; */
11766: /* if(mintmean==0) jintmean=1; */
1.267 brouard 11767:
1.351 brouard 11768: /* i1=pow(2,cptcoveff); */
11769: /* if (cptcovn < 1){i1=1;} */
1.267 brouard 11770:
1.296 brouard 11771: fprintf(ficresfb,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
11772: printf("# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
1.267 brouard 11773:
11774: fprintf(ficresfb,"#****** Routine prevbackforecast **\n");
11775:
1.351 brouard 11776: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
11777: k=TKresult[nres];
11778: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
11779: /* for(k=1; k<=i1;k++){ */
11780: /* if(i1 != 1 && TKresult[nres]!= k) */
11781: /* continue; */
11782: /* if(invalidvarcomb[k]){ */
11783: /* printf("\nCombination (%d) projection ignored because no cases \n",k); */
11784: /* continue; */
11785: /* } */
1.268 brouard 11786: fprintf(ficresfb,"\n#****** hbijx=probability over h years, hb.jx is weighted by observed prev \n#");
1.351 brouard 11787: for(j=1;j<=cptcovs;j++){
11788: /* for(j=1;j<=cptcoveff;j++) { */
11789: /* fprintf(ficresfb," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11790: /* } */
11791: fprintf(ficresfb," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
1.267 brouard 11792: }
1.351 brouard 11793: /* fprintf(ficrespij,"******\n"); */
11794: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
11795: /* fprintf(ficresfb," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
11796: /* } */
1.267 brouard 11797: fprintf(ficresfb," yearbproj age");
11798: for(j=1; j<=nlstate+ndeath;j++){
11799: for(i=1; i<=nlstate;i++)
1.268 brouard 11800: fprintf(ficresfb," b%d%d",i,j);
11801: fprintf(ficresfb," b.%d",j);
1.267 brouard 11802: }
1.296 brouard 11803: for (yearp=0; yearp>=(anbackf-anbackd);yearp -=stepsize) {
1.267 brouard 11804: /* for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) { */
11805: fprintf(ficresfb,"\n");
1.296 brouard 11806: fprintf(ficresfb,"\n# Back Forecasting at date %.lf/%.lf/%.lf ",jbackd,mbackd,anbackd+yearp);
1.273 brouard 11807: /* printf("\n# Back Forecasting at date %.lf/%.lf/%.lf ",jback1,mback1,anback1+yearp); */
1.270 brouard 11808: /* for (agec=bage; agec<=agemax-1; agec++){ /\* testing *\/ */
11809: for (agec=bage; agec<=fage; agec++){ /* testing */
1.268 brouard 11810: /* We compute bij at age agec over nhstepm, nhstepm decreases when agec increases because of agemax;*/
1.271 brouard 11811: nhstepm=(int) (agec-agelim) *YEARM/stepm;/* nhstepm=(int) rint((agec-agelim)*YEARM/stepm);*/
1.267 brouard 11812: nhstepm = nhstepm/hstepm;
11813: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
11814: oldm=oldms;savm=savms;
1.268 brouard 11815: /* computes hbxij at age agec over 1 to nhstepm */
1.271 brouard 11816: /* printf("####prevbackforecast debug agec=%.2f nhstepm=%d\n",agec, nhstepm);fflush(stdout); */
1.267 brouard 11817: hbxij(p3mat,nhstepm,agec,hstepm,p,prevacurrent,nlstate,stepm, k, nres);
1.268 brouard 11818: /* hpxij(p3mat,nhstepm,agec,hstepm,p, nlstate,stepm,oldm,savm, k,nres); */
11819: /* Then we print p3mat for h corresponding to the right agec+h*stepms=yearp */
11820: /* printf(" agec=%.2f\n",agec);fflush(stdout); */
1.267 brouard 11821: for (h=0; h<=nhstepm; h++){
1.268 brouard 11822: if (h*hstepm/YEARM*stepm ==-yearp) {
11823: break;
11824: }
11825: }
11826: fprintf(ficresfb,"\n");
1.351 brouard 11827: /* for(j=1;j<=cptcoveff;j++) */
11828: for(j=1;j<=cptcovs;j++)
11829: fprintf(ficresfb,"%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
11830: /* fprintf(ficresfb,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
1.296 brouard 11831: fprintf(ficresfb,"%.f %.f ",anbackd+yearp,agec-h*hstepm/YEARM*stepm);
1.268 brouard 11832: for(i=1; i<=nlstate+ndeath;i++) {
11833: ppij=0.;ppi=0.;
11834: for(j=1; j<=nlstate;j++) {
11835: /* if (mobilav==1) */
1.269 brouard 11836: ppij=ppij+p3mat[i][j][h]*prevacurrent[(int)agec][j][k];
11837: ppi=ppi+prevacurrent[(int)agec][j][k];
11838: /* ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][j][k]; */
11839: /* ppi=ppi+mobaverage[(int)agec][j][k]; */
1.267 brouard 11840: /* else { */
11841: /* ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][k]; */
11842: /* } */
1.268 brouard 11843: fprintf(ficresfb," %.3f", p3mat[i][j][h]);
11844: } /* end j */
11845: if(ppi <0.99){
11846: printf("Error in prevbackforecast, prevalence doesn't sum to 1 for state %d: %3f\n",i, ppi);
11847: fprintf(ficlog,"Error in prevbackforecast, prevalence doesn't sum to 1 for state %d: %3f\n",i, ppi);
11848: }
11849: fprintf(ficresfb," %.3f", ppij);
11850: }/* end j */
1.267 brouard 11851: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
11852: } /* end agec */
11853: } /* end yearp */
11854: } /* end k */
1.217 brouard 11855:
1.267 brouard 11856: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
1.217 brouard 11857:
1.267 brouard 11858: fclose(ficresfb);
11859: printf("End of Computing Back forecasting \n");
11860: fprintf(ficlog,"End of Computing Back forecasting\n");
1.218 brouard 11861:
1.267 brouard 11862: }
1.217 brouard 11863:
1.269 brouard 11864: /* Variance of prevalence limit: varprlim */
11865: 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){
1.288 brouard 11866: /*------- Variance of forward period (stable) prevalence------*/
1.269 brouard 11867:
11868: char fileresvpl[FILENAMELENGTH];
11869: FILE *ficresvpl;
11870: double **oldm, **savm;
11871: double **varpl; /* Variances of prevalence limits by age */
11872: int i1, k, nres, j ;
11873:
11874: strcpy(fileresvpl,"VPL_");
11875: strcat(fileresvpl,fileresu);
11876: if((ficresvpl=fopen(fileresvpl,"w"))==NULL) {
1.288 brouard 11877: printf("Problem with variance of forward period (stable) prevalence resultfile: %s\n", fileresvpl);
1.269 brouard 11878: exit(0);
11879: }
1.288 brouard 11880: printf("Computing Variance-covariance of forward period (stable) prevalence: file '%s' ...", fileresvpl);fflush(stdout);
11881: fprintf(ficlog, "Computing Variance-covariance of forward period (stable) prevalence: file '%s' ...", fileresvpl);fflush(ficlog);
1.269 brouard 11882:
11883: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
11884: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
11885:
11886: i1=pow(2,cptcoveff);
11887: if (cptcovn < 1){i1=1;}
11888:
1.337 brouard 11889: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
11890: k=TKresult[nres];
1.338 brouard 11891: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
1.337 brouard 11892: /* for(k=1; k<=i1;k++){ /\* We find the combination equivalent to result line values of dummies *\/ */
1.269 brouard 11893: if(i1 != 1 && TKresult[nres]!= k)
11894: continue;
11895: fprintf(ficresvpl,"\n#****** ");
11896: printf("\n#****** ");
11897: fprintf(ficlog,"\n#****** ");
1.337 brouard 11898: for(j=1;j<=cptcovs;j++) {
11899: fprintf(ficresvpl,"V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
11900: fprintf(ficlog,"V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
11901: printf("V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
11902: /* fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11903: /* printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
1.269 brouard 11904: }
1.337 brouard 11905: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
11906: /* printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
11907: /* fprintf(ficresvpl," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
11908: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
11909: /* } */
1.269 brouard 11910: fprintf(ficresvpl,"******\n");
11911: printf("******\n");
11912: fprintf(ficlog,"******\n");
11913:
11914: varpl=matrix(1,nlstate,(int) bage, (int) fage);
11915: oldm=oldms;savm=savms;
11916: varprevlim(fileresvpl, ficresvpl, varpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl, ncvyearp, k, strstart, nres);
11917: free_matrix(varpl,1,nlstate,(int) bage, (int)fage);
11918: /*}*/
11919: }
11920:
11921: fclose(ficresvpl);
1.288 brouard 11922: printf("done variance-covariance of forward period prevalence\n");fflush(stdout);
11923: fprintf(ficlog,"done variance-covariance of forward period prevalence\n");fflush(ficlog);
1.269 brouard 11924:
11925: }
11926: /* Variance of back prevalence: varbprlim */
11927: 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){
11928: /*------- Variance of back (stable) prevalence------*/
11929:
11930: char fileresvbl[FILENAMELENGTH];
11931: FILE *ficresvbl;
11932:
11933: double **oldm, **savm;
11934: double **varbpl; /* Variances of back prevalence limits by age */
11935: int i1, k, nres, j ;
11936:
11937: strcpy(fileresvbl,"VBL_");
11938: strcat(fileresvbl,fileresu);
11939: if((ficresvbl=fopen(fileresvbl,"w"))==NULL) {
11940: printf("Problem with variance of back (stable) prevalence resultfile: %s\n", fileresvbl);
11941: exit(0);
11942: }
11943: printf("Computing Variance-covariance of back (stable) prevalence: file '%s' ...", fileresvbl);fflush(stdout);
11944: fprintf(ficlog, "Computing Variance-covariance of back (stable) prevalence: file '%s' ...", fileresvbl);fflush(ficlog);
11945:
11946:
11947: i1=pow(2,cptcoveff);
11948: if (cptcovn < 1){i1=1;}
11949:
1.337 brouard 11950: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
11951: k=TKresult[nres];
1.338 brouard 11952: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
1.337 brouard 11953: /* for(k=1; k<=i1;k++){ */
11954: /* if(i1 != 1 && TKresult[nres]!= k) */
11955: /* continue; */
1.269 brouard 11956: fprintf(ficresvbl,"\n#****** ");
11957: printf("\n#****** ");
11958: fprintf(ficlog,"\n#****** ");
1.337 brouard 11959: for (j=1; j<= cptcovs; j++){ /* For each selected (single) quantitative value */
1.338 brouard 11960: printf(" V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][resultmodel[nres][j]]);
11961: fprintf(ficresvbl," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][resultmodel[nres][j]]);
11962: fprintf(ficlog," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][resultmodel[nres][j]]);
1.337 brouard 11963: /* for(j=1;j<=cptcoveff;j++) { */
11964: /* fprintf(ficresvbl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11965: /* fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11966: /* printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11967: /* } */
11968: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
11969: /* printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
11970: /* fprintf(ficresvbl," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
11971: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
1.269 brouard 11972: }
11973: fprintf(ficresvbl,"******\n");
11974: printf("******\n");
11975: fprintf(ficlog,"******\n");
11976:
11977: varbpl=matrix(1,nlstate,(int) bage, (int) fage);
11978: oldm=oldms;savm=savms;
11979:
11980: varbrevlim(fileresvbl, ficresvbl, varbpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, bprlim, ftolpl, mobilavproj, ncvyearp, k, strstart, nres);
11981: free_matrix(varbpl,1,nlstate,(int) bage, (int)fage);
11982: /*}*/
11983: }
11984:
11985: fclose(ficresvbl);
11986: printf("done variance-covariance of back prevalence\n");fflush(stdout);
11987: fprintf(ficlog,"done variance-covariance of back prevalence\n");fflush(ficlog);
11988:
11989: } /* End of varbprlim */
11990:
1.126 brouard 11991: /************** Forecasting *****not tested NB*************/
1.227 brouard 11992: /* void populforecast(char fileres[], double anpyram,double mpyram,double jpyram,double ageminpar, double agemax,double dateprev1, double dateprev2s, int mobilav, double agedeb, double fage, int popforecast, char popfile[], double anpyram1,double p[], int i2){ */
1.126 brouard 11993:
1.227 brouard 11994: /* int cpt, stepsize, hstepm, nhstepm, j,k,c, cptcod, i,h; */
11995: /* int *popage; */
11996: /* double calagedatem, agelim, kk1, kk2; */
11997: /* double *popeffectif,*popcount; */
11998: /* double ***p3mat,***tabpop,***tabpopprev; */
11999: /* /\* double ***mobaverage; *\/ */
12000: /* char filerespop[FILENAMELENGTH]; */
1.126 brouard 12001:
1.227 brouard 12002: /* tabpop= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
12003: /* tabpopprev= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
12004: /* agelim=AGESUP; */
12005: /* calagedatem=(anpyram+mpyram/12.+jpyram/365.-dateintmean)*YEARM; */
1.126 brouard 12006:
1.227 brouard 12007: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
1.126 brouard 12008:
12009:
1.227 brouard 12010: /* strcpy(filerespop,"POP_"); */
12011: /* strcat(filerespop,fileresu); */
12012: /* if((ficrespop=fopen(filerespop,"w"))==NULL) { */
12013: /* printf("Problem with forecast resultfile: %s\n", filerespop); */
12014: /* fprintf(ficlog,"Problem with forecast resultfile: %s\n", filerespop); */
12015: /* } */
12016: /* printf("Computing forecasting: result on file '%s' \n", filerespop); */
12017: /* fprintf(ficlog,"Computing forecasting: result on file '%s' \n", filerespop); */
1.126 brouard 12018:
1.227 brouard 12019: /* if (cptcoveff==0) ncodemax[cptcoveff]=1; */
1.126 brouard 12020:
1.227 brouard 12021: /* /\* if (mobilav!=0) { *\/ */
12022: /* /\* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
12023: /* /\* if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){ *\/ */
12024: /* /\* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); *\/ */
12025: /* /\* printf(" Error in movingaverage mobilav=%d\n",mobilav); *\/ */
12026: /* /\* } *\/ */
12027: /* /\* } *\/ */
1.126 brouard 12028:
1.227 brouard 12029: /* stepsize=(int) (stepm+YEARM-1)/YEARM; */
12030: /* if (stepm<=12) stepsize=1; */
1.126 brouard 12031:
1.227 brouard 12032: /* agelim=AGESUP; */
1.126 brouard 12033:
1.227 brouard 12034: /* hstepm=1; */
12035: /* hstepm=hstepm/stepm; */
1.218 brouard 12036:
1.227 brouard 12037: /* if (popforecast==1) { */
12038: /* if((ficpop=fopen(popfile,"r"))==NULL) { */
12039: /* printf("Problem with population file : %s\n",popfile);exit(0); */
12040: /* fprintf(ficlog,"Problem with population file : %s\n",popfile);exit(0); */
12041: /* } */
12042: /* popage=ivector(0,AGESUP); */
12043: /* popeffectif=vector(0,AGESUP); */
12044: /* popcount=vector(0,AGESUP); */
1.126 brouard 12045:
1.227 brouard 12046: /* i=1; */
12047: /* while ((c=fscanf(ficpop,"%d %lf\n",&popage[i],&popcount[i])) != EOF) i=i+1; */
1.218 brouard 12048:
1.227 brouard 12049: /* imx=i; */
12050: /* for (i=1; i<imx;i++) popeffectif[popage[i]]=popcount[i]; */
12051: /* } */
1.218 brouard 12052:
1.227 brouard 12053: /* for(cptcov=1,k=0;cptcov<=i2;cptcov++){ */
12054: /* for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){ */
12055: /* k=k+1; */
12056: /* fprintf(ficrespop,"\n#******"); */
12057: /* for(j=1;j<=cptcoveff;j++) { */
12058: /* fprintf(ficrespop," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */
12059: /* } */
12060: /* fprintf(ficrespop,"******\n"); */
12061: /* fprintf(ficrespop,"# Age"); */
12062: /* for(j=1; j<=nlstate+ndeath;j++) fprintf(ficrespop," P.%d",j); */
12063: /* if (popforecast==1) fprintf(ficrespop," [Population]"); */
1.126 brouard 12064:
1.227 brouard 12065: /* for (cpt=0; cpt<=0;cpt++) { */
12066: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */
1.126 brouard 12067:
1.227 brouard 12068: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */
12069: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */
12070: /* nhstepm = nhstepm/hstepm; */
1.126 brouard 12071:
1.227 brouard 12072: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
12073: /* oldm=oldms;savm=savms; */
12074: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
1.218 brouard 12075:
1.227 brouard 12076: /* for (h=0; h<=nhstepm; h++){ */
12077: /* if (h==(int) (calagedatem+YEARM*cpt)) { */
12078: /* fprintf(ficrespop,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */
12079: /* } */
12080: /* for(j=1; j<=nlstate+ndeath;j++) { */
12081: /* kk1=0.;kk2=0; */
12082: /* for(i=1; i<=nlstate;i++) { */
12083: /* if (mobilav==1) */
12084: /* kk1=kk1+p3mat[i][j][h]*mobaverage[(int)agedeb+1][i][cptcod]; */
12085: /* else { */
12086: /* kk1=kk1+p3mat[i][j][h]*probs[(int)(agedeb+1)][i][cptcod]; */
12087: /* } */
12088: /* } */
12089: /* if (h==(int)(calagedatem+12*cpt)){ */
12090: /* tabpop[(int)(agedeb)][j][cptcod]=kk1; */
12091: /* /\*fprintf(ficrespop," %.3f", kk1); */
12092: /* if (popforecast==1) fprintf(ficrespop," [%.f]", kk1*popeffectif[(int)agedeb+1]);*\/ */
12093: /* } */
12094: /* } */
12095: /* for(i=1; i<=nlstate;i++){ */
12096: /* kk1=0.; */
12097: /* for(j=1; j<=nlstate;j++){ */
12098: /* kk1= kk1+tabpop[(int)(agedeb)][j][cptcod]; */
12099: /* } */
12100: /* tabpopprev[(int)(agedeb)][i][cptcod]=tabpop[(int)(agedeb)][i][cptcod]/kk1*popeffectif[(int)(agedeb+(calagedatem+12*cpt)*hstepm/YEARM*stepm-1)]; */
12101: /* } */
1.218 brouard 12102:
1.227 brouard 12103: /* if (h==(int)(calagedatem+12*cpt)) */
12104: /* for(j=1; j<=nlstate;j++) */
12105: /* fprintf(ficrespop," %15.2f",tabpopprev[(int)(agedeb+1)][j][cptcod]); */
12106: /* } */
12107: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
12108: /* } */
12109: /* } */
1.218 brouard 12110:
1.227 brouard 12111: /* /\******\/ */
1.218 brouard 12112:
1.227 brouard 12113: /* for (cpt=1; cpt<=(anpyram1-anpyram);cpt++) { */
12114: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */
12115: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */
12116: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */
12117: /* nhstepm = nhstepm/hstepm; */
1.126 brouard 12118:
1.227 brouard 12119: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
12120: /* oldm=oldms;savm=savms; */
12121: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
12122: /* for (h=0; h<=nhstepm; h++){ */
12123: /* if (h==(int) (calagedatem+YEARM*cpt)) { */
12124: /* fprintf(ficresf,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */
12125: /* } */
12126: /* for(j=1; j<=nlstate+ndeath;j++) { */
12127: /* kk1=0.;kk2=0; */
12128: /* for(i=1; i<=nlstate;i++) { */
12129: /* kk1=kk1+p3mat[i][j][h]*tabpopprev[(int)agedeb+1][i][cptcod]; */
12130: /* } */
12131: /* if (h==(int)(calagedatem+12*cpt)) fprintf(ficresf," %15.2f", kk1); */
12132: /* } */
12133: /* } */
12134: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
12135: /* } */
12136: /* } */
12137: /* } */
12138: /* } */
1.218 brouard 12139:
1.227 brouard 12140: /* /\* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
1.218 brouard 12141:
1.227 brouard 12142: /* if (popforecast==1) { */
12143: /* free_ivector(popage,0,AGESUP); */
12144: /* free_vector(popeffectif,0,AGESUP); */
12145: /* free_vector(popcount,0,AGESUP); */
12146: /* } */
12147: /* free_ma3x(tabpop,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
12148: /* free_ma3x(tabpopprev,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
12149: /* fclose(ficrespop); */
12150: /* } /\* End of popforecast *\/ */
1.218 brouard 12151:
1.126 brouard 12152: int fileappend(FILE *fichier, char *optionfich)
12153: {
12154: if((fichier=fopen(optionfich,"a"))==NULL) {
12155: printf("Problem with file: %s\n", optionfich);
12156: fprintf(ficlog,"Problem with file: %s\n", optionfich);
12157: return (0);
12158: }
12159: fflush(fichier);
12160: return (1);
12161: }
12162:
12163:
12164: /**************** function prwizard **********************/
12165: void prwizard(int ncovmodel, int nlstate, int ndeath, char model[], FILE *ficparo)
12166: {
12167:
12168: /* Wizard to print covariance matrix template */
12169:
1.164 brouard 12170: char ca[32], cb[32];
12171: int i,j, k, li, lj, lk, ll, jj, npar, itimes;
1.126 brouard 12172: int numlinepar;
12173:
12174: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
12175: fprintf(ficparo,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
12176: for(i=1; i <=nlstate; i++){
12177: jj=0;
12178: for(j=1; j <=nlstate+ndeath; j++){
12179: if(j==i) continue;
12180: jj++;
12181: /*ca[0]= k+'a'-1;ca[1]='\0';*/
12182: printf("%1d%1d",i,j);
12183: fprintf(ficparo,"%1d%1d",i,j);
12184: for(k=1; k<=ncovmodel;k++){
12185: /* printf(" %lf",param[i][j][k]); */
12186: /* fprintf(ficparo," %lf",param[i][j][k]); */
12187: printf(" 0.");
12188: fprintf(ficparo," 0.");
12189: }
12190: printf("\n");
12191: fprintf(ficparo,"\n");
12192: }
12193: }
12194: printf("# Scales (for hessian or gradient estimation)\n");
12195: fprintf(ficparo,"# Scales (for hessian or gradient estimation)\n");
12196: npar= (nlstate+ndeath-1)*nlstate*ncovmodel; /* Number of parameters*/
12197: for(i=1; i <=nlstate; i++){
12198: jj=0;
12199: for(j=1; j <=nlstate+ndeath; j++){
12200: if(j==i) continue;
12201: jj++;
12202: fprintf(ficparo,"%1d%1d",i,j);
12203: printf("%1d%1d",i,j);
12204: fflush(stdout);
12205: for(k=1; k<=ncovmodel;k++){
12206: /* printf(" %le",delti3[i][j][k]); */
12207: /* fprintf(ficparo," %le",delti3[i][j][k]); */
12208: printf(" 0.");
12209: fprintf(ficparo," 0.");
12210: }
12211: numlinepar++;
12212: printf("\n");
12213: fprintf(ficparo,"\n");
12214: }
12215: }
12216: printf("# Covariance matrix\n");
12217: /* # 121 Var(a12)\n\ */
12218: /* # 122 Cov(b12,a12) Var(b12)\n\ */
12219: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
12220: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
12221: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
12222: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
12223: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
12224: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
12225: fflush(stdout);
12226: fprintf(ficparo,"# Covariance matrix\n");
12227: /* # 121 Var(a12)\n\ */
12228: /* # 122 Cov(b12,a12) Var(b12)\n\ */
12229: /* # ...\n\ */
12230: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
12231:
12232: for(itimes=1;itimes<=2;itimes++){
12233: jj=0;
12234: for(i=1; i <=nlstate; i++){
12235: for(j=1; j <=nlstate+ndeath; j++){
12236: if(j==i) continue;
12237: for(k=1; k<=ncovmodel;k++){
12238: jj++;
12239: ca[0]= k+'a'-1;ca[1]='\0';
12240: if(itimes==1){
12241: printf("#%1d%1d%d",i,j,k);
12242: fprintf(ficparo,"#%1d%1d%d",i,j,k);
12243: }else{
12244: printf("%1d%1d%d",i,j,k);
12245: fprintf(ficparo,"%1d%1d%d",i,j,k);
12246: /* printf(" %.5le",matcov[i][j]); */
12247: }
12248: ll=0;
12249: for(li=1;li <=nlstate; li++){
12250: for(lj=1;lj <=nlstate+ndeath; lj++){
12251: if(lj==li) continue;
12252: for(lk=1;lk<=ncovmodel;lk++){
12253: ll++;
12254: if(ll<=jj){
12255: cb[0]= lk +'a'-1;cb[1]='\0';
12256: if(ll<jj){
12257: if(itimes==1){
12258: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
12259: fprintf(ficparo," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
12260: }else{
12261: printf(" 0.");
12262: fprintf(ficparo," 0.");
12263: }
12264: }else{
12265: if(itimes==1){
12266: printf(" Var(%s%1d%1d)",ca,i,j);
12267: fprintf(ficparo," Var(%s%1d%1d)",ca,i,j);
12268: }else{
12269: printf(" 0.");
12270: fprintf(ficparo," 0.");
12271: }
12272: }
12273: }
12274: } /* end lk */
12275: } /* end lj */
12276: } /* end li */
12277: printf("\n");
12278: fprintf(ficparo,"\n");
12279: numlinepar++;
12280: } /* end k*/
12281: } /*end j */
12282: } /* end i */
12283: } /* end itimes */
12284:
12285: } /* end of prwizard */
12286: /******************* Gompertz Likelihood ******************************/
12287: double gompertz(double x[])
12288: {
1.302 brouard 12289: double A=0.0,B=0.,L=0.0,sump=0.,num=0.;
1.126 brouard 12290: int i,n=0; /* n is the size of the sample */
12291:
1.220 brouard 12292: for (i=1;i<=imx ; i++) {
1.126 brouard 12293: sump=sump+weight[i];
12294: /* sump=sump+1;*/
12295: num=num+1;
12296: }
1.302 brouard 12297: L=0.0;
12298: /* agegomp=AGEGOMP; */
1.126 brouard 12299: /* for (i=0; i<=imx; i++)
12300: 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]);*/
12301:
1.302 brouard 12302: for (i=1;i<=imx ; i++) {
12303: /* mu(a)=mu(agecomp)*exp(teta*(age-agegomp))
12304: mu(a)=x[1]*exp(x[2]*(age-agegomp)); x[1] and x[2] are per year.
12305: * L= Product mu(agedeces)exp(-\int_ageexam^agedc mu(u) du ) for a death between agedc (in month)
12306: * and agedc +1 month, cens[i]=0: log(x[1]/YEARM)
12307: * +
12308: * exp(-\int_ageexam^agecens mu(u) du ) when censored, cens[i]=1
12309: */
12310: if (wav[i] > 1 || agedc[i] < AGESUP) {
12311: if (cens[i] == 1){
12312: A=-x[1]/(x[2])*(exp(x[2]*(agecens[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)));
12313: } else if (cens[i] == 0){
1.126 brouard 12314: A=-x[1]/(x[2])*(exp(x[2]*(agedc[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)))
1.362 brouard 12315: +log(fabs(x[1])/YEARM) +x[2]*(agedc[i]-agegomp)+log(YEARM);
12316: /* +log(x[1]/YEARM) +x[2]*(agedc[i]-agegomp)+log(YEARM); */ /* To be seen */
1.302 brouard 12317: } else
12318: printf("Gompertz cens[%d] neither 1 nor 0\n",i);
1.126 brouard 12319: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
1.302 brouard 12320: L=L+A*weight[i];
1.126 brouard 12321: /* 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]);*/
1.302 brouard 12322: }
12323: }
1.126 brouard 12324:
1.302 brouard 12325: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
1.126 brouard 12326:
12327: return -2*L*num/sump;
12328: }
12329:
1.136 brouard 12330: #ifdef GSL
12331: /******************* Gompertz_f Likelihood ******************************/
12332: double gompertz_f(const gsl_vector *v, void *params)
12333: {
1.302 brouard 12334: double A=0.,B=0.,LL=0.0,sump=0.,num=0.;
1.136 brouard 12335: double *x= (double *) v->data;
12336: int i,n=0; /* n is the size of the sample */
12337:
12338: for (i=0;i<=imx-1 ; i++) {
12339: sump=sump+weight[i];
12340: /* sump=sump+1;*/
12341: num=num+1;
12342: }
12343:
12344:
12345: /* for (i=0; i<=imx; i++)
12346: 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]);*/
12347: printf("x[0]=%lf x[1]=%lf\n",x[0],x[1]);
12348: for (i=1;i<=imx ; i++)
12349: {
12350: if (cens[i] == 1 && wav[i]>1)
12351: A=-x[0]/(x[1])*(exp(x[1]*(agecens[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)));
12352:
12353: if (cens[i] == 0 && wav[i]>1)
12354: A=-x[0]/(x[1])*(exp(x[1]*(agedc[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)))
12355: +log(x[0]/YEARM)+x[1]*(agedc[i]-agegomp)+log(YEARM);
12356:
12357: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
12358: if (wav[i] > 1 ) { /* ??? */
12359: LL=LL+A*weight[i];
12360: /* 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]);*/
12361: }
12362: }
12363:
12364: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
12365: printf("x[0]=%lf x[1]=%lf -2*LL*num/sump=%lf\n",x[0],x[1],-2*LL*num/sump);
12366:
12367: return -2*LL*num/sump;
12368: }
12369: #endif
12370:
1.126 brouard 12371: /******************* Printing html file ***********/
1.201 brouard 12372: void printinghtmlmort(char fileresu[], char title[], char datafile[], int firstpass, \
1.126 brouard 12373: int lastpass, int stepm, int weightopt, char model[],\
12374: int imx, double p[],double **matcov,double agemortsup){
12375: int i,k;
12376:
12377: fprintf(fichtm,"<ul><li><h4>Result files </h4>\n Force of mortality. Parameters of the Gompertz fit (with confidence interval in brackets):<br>");
12378: fprintf(fichtm," mu(age) =%lf*exp(%lf*(age-%d)) per year<br><br>",p[1],p[2],agegomp);
12379: for (i=1;i<=2;i++)
12380: fprintf(fichtm," p[%d] = %lf [%f ; %f]<br>\n",i,p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
1.199 brouard 12381: fprintf(fichtm,"<br><br><img src=\"graphmort.svg\">");
1.126 brouard 12382: fprintf(fichtm,"</ul>");
12383:
12384: fprintf(fichtm,"<ul><li><h4>Life table</h4>\n <br>");
12385:
12386: 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>");
12387:
12388: for (k=agegomp;k<(agemortsup-2);k++)
12389: 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]);
12390:
12391:
12392: fflush(fichtm);
12393: }
12394:
12395: /******************* Gnuplot file **************/
1.201 brouard 12396: void printinggnuplotmort(char fileresu[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){
1.126 brouard 12397:
12398: char dirfileres[132],optfileres[132];
1.164 brouard 12399:
1.359 brouard 12400: /*int ng;*/
1.126 brouard 12401:
12402:
12403: /*#ifdef windows */
12404: fprintf(ficgp,"cd \"%s\" \n",pathc);
12405: /*#endif */
12406:
12407:
12408: strcpy(dirfileres,optionfilefiname);
12409: strcpy(optfileres,"vpl");
1.199 brouard 12410: fprintf(ficgp,"set out \"graphmort.svg\"\n ");
1.126 brouard 12411: fprintf(ficgp,"set xlabel \"Age\"\n set ylabel \"Force of mortality (per year)\" \n ");
1.199 brouard 12412: fprintf(ficgp, "set ter svg size 640, 480\n set log y\n");
1.145 brouard 12413: /* fprintf(ficgp, "set size 0.65,0.65\n"); */
1.126 brouard 12414: fprintf(ficgp,"plot [%d:100] %lf*exp(%lf*(x-%d))",agegomp,p[1],p[2],agegomp);
12415:
12416: }
12417:
1.136 brouard 12418: int readdata(char datafile[], int firstobs, int lastobs, int *imax)
12419: {
1.126 brouard 12420:
1.136 brouard 12421: /*-------- data file ----------*/
12422: FILE *fic;
12423: char dummy[]=" ";
1.359 brouard 12424: int i = 0, j = 0, n = 0, iv = 0;/* , v;*/
1.223 brouard 12425: int lstra;
1.136 brouard 12426: int linei, month, year,iout;
1.302 brouard 12427: int noffset=0; /* This is the offset if BOM data file */
1.136 brouard 12428: char line[MAXLINE], linetmp[MAXLINE];
1.164 brouard 12429: char stra[MAXLINE], strb[MAXLINE];
1.136 brouard 12430: char *stratrunc;
1.223 brouard 12431:
1.349 brouard 12432: /* DummyV=ivector(-1,NCOVMAX); /\* 1 to 3 *\/ */
12433: /* FixedV=ivector(-1,NCOVMAX); /\* 1 to 3 *\/ */
1.339 brouard 12434:
12435: ncovcolt=ncovcol+nqv+ntv+nqtv; /* total of covariates in the data, not in the model equation */
12436:
1.136 brouard 12437: if((fic=fopen(datafile,"r"))==NULL) {
1.218 brouard 12438: printf("Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(stdout);
12439: fprintf(ficlog,"Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(ficlog);return 1;
1.136 brouard 12440: }
1.126 brouard 12441:
1.302 brouard 12442: /* Is it a BOM UTF-8 Windows file? */
12443: /* First data line */
12444: linei=0;
12445: while(fgets(line, MAXLINE, fic)) {
12446: noffset=0;
12447: if( line[0] == (char)0xEF && line[1] == (char)0xBB) /* EF BB BF */
12448: {
12449: noffset=noffset+3;
12450: printf("# Data file '%s' is an UTF8 BOM file, please convert to UTF8 or ascii file and rerun.\n",datafile);fflush(stdout);
12451: fprintf(ficlog,"# Data file '%s' is an UTF8 BOM file, please convert to UTF8 or ascii file and rerun.\n",datafile);
12452: fflush(ficlog); return 1;
12453: }
12454: /* else if( line[0] == (char)0xFE && line[1] == (char)0xFF)*/
12455: else if( line[0] == (char)0xFF && line[1] == (char)0xFE)
12456: {
12457: noffset=noffset+2;
1.304 brouard 12458: 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);
12459: 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);
1.302 brouard 12460: fflush(ficlog); return 1;
12461: }
12462: else if( line[0] == 0 && line[1] == 0)
12463: {
12464: if( line[2] == (char)0xFE && line[3] == (char)0xFF){
12465: noffset=noffset+4;
1.304 brouard 12466: 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);
12467: 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);
1.302 brouard 12468: fflush(ficlog); return 1;
12469: }
12470: } else{
12471: ;/*printf(" Not a BOM file\n");*/
12472: }
12473: /* If line starts with a # it is a comment */
12474: if (line[noffset] == '#') {
12475: linei=linei+1;
12476: break;
12477: }else{
12478: break;
12479: }
12480: }
12481: fclose(fic);
12482: if((fic=fopen(datafile,"r"))==NULL) {
12483: printf("Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(stdout);
12484: fprintf(ficlog,"Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(ficlog);return 1;
12485: }
12486: /* Not a Bom file */
12487:
1.136 brouard 12488: i=1;
12489: while ((fgets(line, MAXLINE, fic) != NULL) &&((i >= firstobs) && (i <=lastobs))) {
12490: linei=linei+1;
12491: for(j=strlen(line); j>=0;j--){ /* Untabifies line */
12492: if(line[j] == '\t')
12493: line[j] = ' ';
12494: }
12495: for(j=strlen(line)-1; (line[j]==' ')||(line[j]==10)||(line[j]==13);j--){
12496: ;
12497: };
12498: line[j+1]=0; /* Trims blanks at end of line */
12499: if(line[0]=='#'){
12500: fprintf(ficlog,"Comment line\n%s\n",line);
12501: printf("Comment line\n%s\n",line);
12502: continue;
12503: }
12504: trimbb(linetmp,line); /* Trims multiple blanks in line */
1.164 brouard 12505: strcpy(line, linetmp);
1.223 brouard 12506:
12507: /* Loops on waves */
12508: for (j=maxwav;j>=1;j--){
12509: for (iv=nqtv;iv>=1;iv--){ /* Loop on time varying quantitative variables */
1.238 brouard 12510: cutv(stra, strb, line, ' ');
12511: if(strb[0]=='.') { /* Missing value */
12512: lval=-1;
12513: cotqvar[j][iv][i]=-1; /* 0.0/0.0 */
1.341 brouard 12514: cotvar[j][ncovcol+nqv+ntv+iv][i]=-1; /* For performance reasons */
1.238 brouard 12515: if(isalpha(strb[1])) { /* .m or .d Really Missing value */
12516: 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);
12517: 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);
12518: return 1;
12519: }
12520: }else{
12521: errno=0;
12522: /* what_kind_of_number(strb); */
12523: dval=strtod(strb,&endptr);
12524: /* if( strb[0]=='\0' || (*endptr != '\0')){ */
12525: /* if(strb != endptr && *endptr == '\0') */
12526: /* dval=dlval; */
12527: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
12528: if( strb[0]=='\0' || (*endptr != '\0')){
12529: 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);
12530: 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);
12531: return 1;
12532: }
12533: cotqvar[j][iv][i]=dval;
1.341 brouard 12534: cotvar[j][ncovcol+nqv+ntv+iv][i]=dval; /* because cotvar starts now at first ntv */
1.238 brouard 12535: }
12536: strcpy(line,stra);
1.223 brouard 12537: }/* end loop ntqv */
1.225 brouard 12538:
1.223 brouard 12539: for (iv=ntv;iv>=1;iv--){ /* Loop on time varying dummies */
1.238 brouard 12540: cutv(stra, strb, line, ' ');
12541: if(strb[0]=='.') { /* Missing value */
12542: lval=-1;
12543: }else{
12544: errno=0;
12545: lval=strtol(strb,&endptr,10);
12546: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
12547: if( strb[0]=='\0' || (*endptr != '\0')){
12548: 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);
12549: 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);
12550: return 1;
12551: }
12552: }
12553: if(lval <-1 || lval >1){
12554: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.319 brouard 12555: Should be a value of %d(nth) covariate of wave %d (0 should be the value for the reference and 1\n \
1.223 brouard 12556: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.238 brouard 12557: For example, for multinomial values like 1, 2 and 3,\n \
12558: build V1=0 V2=0 for the reference value (1),\n \
12559: V1=1 V2=0 for (2) \n \
1.223 brouard 12560: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.238 brouard 12561: output of IMaCh is often meaningless.\n \
1.319 brouard 12562: Exiting.\n",lval,linei, i,line,iv,j);
1.238 brouard 12563: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.319 brouard 12564: Should be a value of %d(nth) covariate of wave %d (0 should be the value for the reference and 1\n \
1.223 brouard 12565: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.238 brouard 12566: For example, for multinomial values like 1, 2 and 3,\n \
12567: build V1=0 V2=0 for the reference value (1),\n \
12568: V1=1 V2=0 for (2) \n \
1.223 brouard 12569: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.238 brouard 12570: output of IMaCh is often meaningless.\n \
1.319 brouard 12571: Exiting.\n",lval,linei, i,line,iv,j);fflush(ficlog);
1.238 brouard 12572: return 1;
12573: }
1.341 brouard 12574: cotvar[j][ncovcol+nqv+iv][i]=(double)(lval);
1.238 brouard 12575: strcpy(line,stra);
1.223 brouard 12576: }/* end loop ntv */
1.225 brouard 12577:
1.223 brouard 12578: /* Statuses at wave */
1.137 brouard 12579: cutv(stra, strb, line, ' ');
1.223 brouard 12580: if(strb[0]=='.') { /* Missing value */
1.238 brouard 12581: lval=-1;
1.136 brouard 12582: }else{
1.238 brouard 12583: errno=0;
12584: lval=strtol(strb,&endptr,10);
12585: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
1.347 brouard 12586: if( strb[0]=='\0' || (*endptr != '\0' )){
12587: 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);
12588: 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);
12589: return 1;
12590: }else if( lval==0 || lval > nlstate+ndeath){
1.348 brouard 12591: 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);
12592: 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);
1.238 brouard 12593: return 1;
12594: }
1.136 brouard 12595: }
1.225 brouard 12596:
1.136 brouard 12597: s[j][i]=lval;
1.225 brouard 12598:
1.223 brouard 12599: /* Date of Interview */
1.136 brouard 12600: strcpy(line,stra);
12601: cutv(stra, strb,line,' ');
1.169 brouard 12602: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 12603: }
1.169 brouard 12604: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
1.225 brouard 12605: month=99;
12606: year=9999;
1.136 brouard 12607: }else{
1.225 brouard 12608: 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);
12609: 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);
12610: return 1;
1.136 brouard 12611: }
12612: anint[j][i]= (double) year;
1.302 brouard 12613: mint[j][i]= (double)month;
12614: /* if( (int)anint[j][i]+ (int)(mint[j][i])/12. < (int) (moisnais[i]/12.+annais[i])){ */
12615: /* 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]); */
12616: /* 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]); */
12617: /* } */
1.136 brouard 12618: strcpy(line,stra);
1.223 brouard 12619: } /* End loop on waves */
1.225 brouard 12620:
1.223 brouard 12621: /* Date of death */
1.136 brouard 12622: cutv(stra, strb,line,' ');
1.169 brouard 12623: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 12624: }
1.169 brouard 12625: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
1.136 brouard 12626: month=99;
12627: year=9999;
12628: }else{
1.141 brouard 12629: printf("Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be a date of death (mm/yyyy or .). Exiting.\n",strb, linei,i,line);
1.225 brouard 12630: 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);
12631: return 1;
1.136 brouard 12632: }
12633: andc[i]=(double) year;
12634: moisdc[i]=(double) month;
12635: strcpy(line,stra);
12636:
1.223 brouard 12637: /* Date of birth */
1.136 brouard 12638: cutv(stra, strb,line,' ');
1.169 brouard 12639: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 12640: }
1.169 brouard 12641: else if( (iout=sscanf(strb,"%s.", dummy)) != 0){
1.136 brouard 12642: month=99;
12643: year=9999;
12644: }else{
1.141 brouard 12645: 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);
12646: fprintf(ficlog,"Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be a date of birth (mm/yyyy or .). Exiting.\n",strb, linei,i,line);fflush(ficlog);
1.225 brouard 12647: return 1;
1.136 brouard 12648: }
12649: if (year==9999) {
1.141 brouard 12650: 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);
12651: fprintf(ficlog,"Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be a date of birth (mm/yyyy) but at least the year of birth should be given. Exiting.\n",strb, linei,i,line);fflush(ficlog);
1.225 brouard 12652: return 1;
12653:
1.136 brouard 12654: }
12655: annais[i]=(double)(year);
1.302 brouard 12656: moisnais[i]=(double)(month);
12657: for (j=1;j<=maxwav;j++){
12658: if( (int)anint[j][i]+ (int)(mint[j][i])/12. < (int) (moisnais[i]/12.+annais[i])){
12659: 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]);
12660: 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]);
12661: }
12662: }
12663:
1.136 brouard 12664: strcpy(line,stra);
1.225 brouard 12665:
1.223 brouard 12666: /* Sample weight */
1.136 brouard 12667: cutv(stra, strb,line,' ');
12668: errno=0;
12669: dval=strtod(strb,&endptr);
12670: if( strb[0]=='\0' || (*endptr != '\0')){
1.141 brouard 12671: printf("Error reading data around '%f' at line number %d, \"%s\" for individual %d\nShould be a weight. Exiting.\n",dval, i,line,linei);
12672: fprintf(ficlog,"Error reading data around '%f' at line number %d, \"%s\" for individual %d\nShould be a weight. Exiting.\n",dval, i,line,linei);
1.136 brouard 12673: fflush(ficlog);
12674: return 1;
12675: }
12676: weight[i]=dval;
12677: strcpy(line,stra);
1.225 brouard 12678:
1.223 brouard 12679: for (iv=nqv;iv>=1;iv--){ /* Loop on fixed quantitative variables */
12680: cutv(stra, strb, line, ' ');
12681: if(strb[0]=='.') { /* Missing value */
1.225 brouard 12682: lval=-1;
1.311 brouard 12683: coqvar[iv][i]=NAN;
12684: covar[ncovcol+iv][i]=NAN; /* including qvar in standard covar for performance reasons */
1.223 brouard 12685: }else{
1.225 brouard 12686: errno=0;
12687: /* what_kind_of_number(strb); */
12688: dval=strtod(strb,&endptr);
12689: /* if(strb != endptr && *endptr == '\0') */
12690: /* dval=dlval; */
12691: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
12692: if( strb[0]=='\0' || (*endptr != '\0')){
12693: 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);
12694: 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);
12695: return 1;
12696: }
12697: coqvar[iv][i]=dval;
1.226 brouard 12698: covar[ncovcol+iv][i]=dval; /* including qvar in standard covar for performance reasons */
1.223 brouard 12699: }
12700: strcpy(line,stra);
12701: }/* end loop nqv */
1.136 brouard 12702:
1.223 brouard 12703: /* Covariate values */
1.136 brouard 12704: for (j=ncovcol;j>=1;j--){
12705: cutv(stra, strb,line,' ');
1.223 brouard 12706: if(strb[0]=='.') { /* Missing covariate value */
1.225 brouard 12707: lval=-1;
1.136 brouard 12708: }else{
1.225 brouard 12709: errno=0;
12710: lval=strtol(strb,&endptr,10);
12711: if( strb[0]=='\0' || (*endptr != '\0')){
12712: 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);
12713: 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);
12714: return 1;
12715: }
1.136 brouard 12716: }
12717: if(lval <-1 || lval >1){
1.225 brouard 12718: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.136 brouard 12719: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
12720: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.225 brouard 12721: For example, for multinomial values like 1, 2 and 3,\n \
12722: build V1=0 V2=0 for the reference value (1),\n \
12723: V1=1 V2=0 for (2) \n \
1.136 brouard 12724: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.225 brouard 12725: output of IMaCh is often meaningless.\n \
1.136 brouard 12726: Exiting.\n",lval,linei, i,line,j);
1.225 brouard 12727: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.136 brouard 12728: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
12729: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.225 brouard 12730: For example, for multinomial values like 1, 2 and 3,\n \
12731: build V1=0 V2=0 for the reference value (1),\n \
12732: V1=1 V2=0 for (2) \n \
1.136 brouard 12733: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.225 brouard 12734: output of IMaCh is often meaningless.\n \
1.136 brouard 12735: Exiting.\n",lval,linei, i,line,j);fflush(ficlog);
1.225 brouard 12736: return 1;
1.136 brouard 12737: }
12738: covar[j][i]=(double)(lval);
12739: strcpy(line,stra);
12740: }
12741: lstra=strlen(stra);
1.225 brouard 12742:
1.136 brouard 12743: if(lstra > 9){ /* More than 2**32 or max of what printf can write with %ld */
12744: stratrunc = &(stra[lstra-9]);
12745: num[i]=atol(stratrunc);
12746: }
12747: else
12748: num[i]=atol(stra);
12749: /*if((s[2][i]==2) && (s[3][i]==-1)&&(s[4][i]==9)){
12750: 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;}*/
12751:
12752: i=i+1;
12753: } /* End loop reading data */
1.225 brouard 12754:
1.136 brouard 12755: *imax=i-1; /* Number of individuals */
12756: fclose(fic);
1.225 brouard 12757:
1.136 brouard 12758: return (0);
1.164 brouard 12759: /* endread: */
1.225 brouard 12760: printf("Exiting readdata: ");
12761: fclose(fic);
12762: return (1);
1.223 brouard 12763: }
1.126 brouard 12764:
1.234 brouard 12765: void removefirstspace(char **stri){/*, char stro[]) {*/
1.230 brouard 12766: char *p1 = *stri, *p2 = *stri;
1.235 brouard 12767: while (*p2 == ' ')
1.234 brouard 12768: p2++;
12769: /* while ((*p1++ = *p2++) !=0) */
12770: /* ; */
12771: /* do */
12772: /* while (*p2 == ' ') */
12773: /* p2++; */
12774: /* while (*p1++ == *p2++); */
12775: *stri=p2;
1.145 brouard 12776: }
12777:
1.330 brouard 12778: int decoderesult( char resultline[], int nres)
1.230 brouard 12779: /**< This routine decode one result line and returns the combination # of dummy covariates only **/
12780: {
1.235 brouard 12781: int j=0, k=0, k1=0, k2=0, k3=0, k4=0, match=0, k2q=0, k3q=0, k4q=0;
1.230 brouard 12782: char resultsav[MAXLINE];
1.330 brouard 12783: /* int resultmodel[MAXLINE]; */
1.334 brouard 12784: /* int modelresult[MAXLINE]; */
1.230 brouard 12785: char stra[80], strb[80], strc[80], strd[80],stre[80];
12786:
1.234 brouard 12787: removefirstspace(&resultline);
1.332 brouard 12788: printf("decoderesult:%s\n",resultline);
1.230 brouard 12789:
1.332 brouard 12790: strcpy(resultsav,resultline);
1.342 brouard 12791: /* printf("Decoderesult resultsav=\"%s\" resultline=\"%s\"\n", resultsav, resultline); */
1.230 brouard 12792: if (strlen(resultsav) >1){
1.334 brouard 12793: j=nbocc(resultsav,'='); /**< j=Number of covariate values'=' in this resultline */
1.230 brouard 12794: }
1.353 brouard 12795: if(j == 0 && cptcovs== 0){ /* Resultline but no = and no covariate in the model */
1.253 brouard 12796: TKresult[nres]=0; /* Combination for the nresult and the model */
12797: return (0);
12798: }
1.234 brouard 12799: if( j != cptcovs ){ /* Be careful if a variable is in a product but not single */
1.353 brouard 12800: fprintf(ficlog,"ERROR: the number of variables in the resultline which is %d, differs from the number %d of single variables used in the model line, 1+age+%s.\n",j, cptcovs, model);fflush(ficlog);
12801: printf("ERROR: the number of variables in the resultline which is %d, differs from the number %d of single variables used in the model line, 1+age+%s.\n",j, cptcovs, model);fflush(stdout);
12802: if(j==0)
12803: return 1;
1.234 brouard 12804: }
1.334 brouard 12805: for(k=1; k<=j;k++){ /* Loop on any covariate of the RESULT LINE */
1.234 brouard 12806: if(nbocc(resultsav,'=') >1){
1.318 brouard 12807: 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" */
1.332 brouard 12808: /* If resultsav= "V4= 1 V5=25.1 V3=0" with a blank then strb="V4=" and stra="1 V5=25.1 V3=0" */
1.318 brouard 12809: cutl(strc,strd,strb,'='); /* strb:"V4=1" strc="1" strd="V4" */
1.332 brouard 12810: /* If a blank, then strc="V4=" and strd='\0' */
12811: if(strc[0]=='\0'){
12812: printf("Error in resultline, probably a blank after the \"%s\", \"result:%s\", stra=\"%s\" resultsav=\"%s\"\n",strb,resultline, stra, resultsav);
12813: fprintf(ficlog,"Error in resultline, probably a blank after the \"V%s=\", resultline=%s\n",strb,resultline);
12814: return 1;
12815: }
1.234 brouard 12816: }else
12817: cutl(strc,strd,resultsav,'=');
1.318 brouard 12818: Tvalsel[k]=atof(strc); /* 1 */ /* Tvalsel of k is the float value of the kth covariate appearing in this result line */
1.234 brouard 12819:
1.230 brouard 12820: cutl(strc,stre,strd,'V'); /* strd='V4' strc=4 stre='V' */;
1.318 brouard 12821: Tvarsel[k]=atoi(strc); /* 4 */ /* Tvarsel is the id of the kth covariate in the result line Tvarsel[1] in "V4=1.." is 4.*/
1.230 brouard 12822: /* Typevarsel[k]=1; /\* 1 for age product *\/ */
12823: /* cptcovsel++; */
12824: if (nbocc(stra,'=') >0)
12825: strcpy(resultsav,stra); /* and analyzes it */
12826: }
1.235 brouard 12827: /* Checking for missing or useless values in comparison of current model needs */
1.332 brouard 12828: /* Feeds resultmodel[nres][k1]=k2 for k1th product covariate with age in the model equation fed by the index k2 of the resutline*/
1.334 brouard 12829: for(k1=1; k1<= cptcovt ;k1++){ /* Loop on MODEL LINE V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1.332 brouard 12830: if(Typevar[k1]==0){ /* Single covariate in model */
12831: /* 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product */
1.234 brouard 12832: match=0;
1.318 brouard 12833: for(k2=1; k2 <=j;k2++){/* Loop on resultline. In result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
12834: if(Tvar[k1]==Tvarsel[k2]) {/* Tvar is coming from the model, Tvarsel from the result. Tvar[1]=5 == Tvarsel[2]=5 */
1.334 brouard 12835: modelresult[nres][k2]=k1;/* modelresult[2]=1 modelresult[1]=2 modelresult[3]=3 modelresult[6]=4 modelresult[9]=5 */
1.318 brouard 12836: match=1; /* modelresult of k2 variable of resultline is identical to k1 variable of the model good */
1.234 brouard 12837: break;
12838: }
12839: }
12840: if(match == 0){
1.338 brouard 12841: 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]);
12842: 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);
1.310 brouard 12843: return 1;
1.234 brouard 12844: }
1.332 brouard 12845: }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*/
12846: /* We feed resultmodel[k1]=k2; */
12847: match=0;
12848: 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 */
12849: if(Tvar[k1]==Tvarsel[k2]) {/* Tvar is coming from the model, Tvarsel from the result. Tvar[1]=5 == Tvarsel[2]=5 */
1.334 brouard 12850: 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 */
1.332 brouard 12851: resultmodel[nres][k1]=k2; /* Added here */
1.342 brouard 12852: /* printf("Decoderesult first modelresult[k2=%d]=%d (k1) V%d*AGE\n",k2,k1,Tvar[k1]); */
1.332 brouard 12853: match=1; /* modelresult of k2 variable of resultline is identical to k1 variable of the model good */
12854: break;
12855: }
12856: }
12857: if(match == 0){
1.338 brouard 12858: 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]);
12859: 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]);
1.332 brouard 12860: return 1;
12861: }
1.349 brouard 12862: }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*/
1.332 brouard 12863: /* resultmodel[nres][of such a Vn * Vm product k1] is not unique, so can't exist, we feed Tvard[k1][1] and [2] */
12864: match=0;
1.342 brouard 12865: /* 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]); */
1.332 brouard 12866: for(k2=1; k2 <=j;k2++){/* Loop on resultline. In result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
12867: if(Tvardk[k1][1]==Tvarsel[k2]) {/* Tvardk is coming from the model, Tvarsel from the result. Tvar[1]=5 == Tvarsel[2]=5 */
12868: /* modelresult[k2]=k1; */
1.342 brouard 12869: /* printf("Decoderesult first Product modelresult[k2=%d]=%d (k1) V%d * \n",k2,k1,Tvarsel[k2]); */
1.332 brouard 12870: match=1; /* modelresult of k2 variable of resultline is identical to k1 variable of the model good */
12871: }
12872: }
12873: if(match == 0){
1.349 brouard 12874: 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);
12875: 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);
1.332 brouard 12876: return 1;
12877: }
12878: match=0;
12879: for(k2=1; k2 <=j;k2++){/* Loop on resultline. In result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
12880: if(Tvardk[k1][2]==Tvarsel[k2]) {/* Tvardk is coming from the model, Tvarsel from the result. Tvar[1]=5 == Tvarsel[2]=5 */
12881: /* modelresult[k2]=k1;*/
1.342 brouard 12882: /* printf("Decoderesult second Product modelresult[k2=%d]=%d (k1) * V%d \n ",k2,k1,Tvarsel[k2]); */
1.332 brouard 12883: match=1; /* modelresult of k2 variable of resultline is identical to k1 variable of the model good */
12884: break;
12885: }
12886: }
12887: if(match == 0){
1.349 brouard 12888: 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);
12889: 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);
1.332 brouard 12890: return 1;
12891: }
12892: }/* End of testing */
1.333 brouard 12893: }/* End loop cptcovt */
1.235 brouard 12894: /* Checking for missing or useless values in comparison of current model needs */
1.332 brouard 12895: /* Feeds resultmodel[nres][k1]=k2 for single covariate (k1) in the model equation */
1.334 brouard 12896: 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)
12897: * Loop on resultline variables: result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
1.234 brouard 12898: match=0;
1.318 brouard 12899: for(k1=1; k1<= cptcovt ;k1++){ /* loop on model: model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1.332 brouard 12900: if(Typevar[k1]==0){ /* Single only */
1.349 brouard 12901: if(Tvar[k1]==Tvarsel[k2]) { /* Tvar[2]=4 == Tvarsel[1]=4 What if a product? */
1.330 brouard 12902: 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 */
1.334 brouard 12903: 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 */
1.234 brouard 12904: ++match;
12905: }
12906: }
12907: }
12908: if(match == 0){
1.338 brouard 12909: printf("Error in result line: variable V%d is missing in model; result: %s, model=1+age+%s\n",Tvarsel[k2], resultline, model);
12910: fprintf(ficlog,"Error in result line: variable V%d is missing in model; result: %s, model=1+age+%s\n",Tvarsel[k2], resultline, model);
1.310 brouard 12911: return 1;
1.234 brouard 12912: }else if(match > 1){
1.338 brouard 12913: printf("Error in result line: %d doubled; result: %s, model=1+age+%s\n",k2, resultline, model);
12914: fprintf(ficlog,"Error in result line: %d doubled; result: %s, model=1+age+%s\n",k2, resultline, model);
1.310 brouard 12915: return 1;
1.234 brouard 12916: }
12917: }
1.334 brouard 12918: /* cptcovres=j /\* Number of variables in the resultline is equal to cptcovs and thus useless *\/ */
1.234 brouard 12919: /* We need to deduce which combination number is chosen and save quantitative values */
1.235 brouard 12920: /* model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1.330 brouard 12921: /* nres=1st result line: V4=1 V5=25.1 V3=0 V2=8 V1=1 */
12922: /* 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*/
12923: /* nres=2nd result line: V4=1 V5=24.1 V3=1 V2=8 V1=0 */
1.235 brouard 12924: /* should give a combination of dummy V4=1, V3=1, V1=0 => V4*2**(0) + V3*2**(1) + V1*2**(2) = 3 + (1offset) = 4*/
12925: /* 1 0 0 0 */
12926: /* 2 1 0 0 */
12927: /* 3 0 1 0 */
1.330 brouard 12928: /* 4 1 1 0 */ /* V4=1, V3=1, V1=0 (nres=2)*/
1.235 brouard 12929: /* 5 0 0 1 */
1.330 brouard 12930: /* 6 1 0 1 */ /* V4=1, V3=0, V1=1 (nres=1)*/
1.235 brouard 12931: /* 7 0 1 1 */
12932: /* 8 1 1 1 */
1.237 brouard 12933: /* V(Tvresult)=Tresult V4=1 V3=0 V1=1 Tresult[nres=1][2]=0 */
12934: /* V(Tvqresult)=Tqresult V5=25.1 V2=8 Tqresult[nres=1][1]=25.1 */
12935: /* V5*age V5 known which value for nres? */
12936: /* Tqinvresult[2]=8 Tqinvresult[1]=25.1 */
1.334 brouard 12937: 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.
12938: * loop on position k1 in the MODEL LINE */
1.331 brouard 12939: /* k counting number of combination of single dummies in the equation model */
12940: /* k4 counting single dummies in the equation model */
12941: /* k4q counting single quantitatives in the equation model */
1.344 brouard 12942: if( Dummy[k1]==0 && Typevar[k1]==0 ){ /* Dummy and Single, fixed or timevarying, k1 is sorting according to MODEL, but k3 to resultline */
1.334 brouard 12943: /* 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) */
1.332 brouard 12944: /* modelresult[k3]=k1: k3th position in the result line corresponds to the k1 position in the model line (doesn't work with products)*/
1.330 brouard 12945: /* Value in the (current nres) resultline of the variable at the k1th position in the model equation resultmodel[nres][k1]= k3 */
1.332 brouard 12946: /* resultmodel[nres][k1]=k3: k1th position in the model correspond to the k3 position in the resultline */
12947: /* k3 is the position in the nres result line of the k1th variable of the model equation */
12948: /* Tvarsel[k3]: Name of the variable at the k3th position in the result line. */
12949: /* Tvalsel[k3]: Value of the variable at the k3th position in the result line. */
12950: /* Tresult[nres][result_position]= value of the dummy variable at the result_position in the nres resultline */
1.334 brouard 12951: /* Tvresult[nres][result_position]= name of the dummy variable at the result_position in the nres resultline */
1.332 brouard 12952: /* Tinvresult[nres][Name of a dummy variable]= value of the variable in the result line */
1.330 brouard 12953: /* TinvDoQresult[nres][Name of a Dummy or Q variable]= value of the variable in the result line */
1.332 brouard 12954: k3= resultmodel[nres][k1]; /* From position k1 in model get position k3 in result line */
12955: /* nres=1 k1=2 resultmodel[2(V4)] = 1=k3 ; k1=3 resultmodel[3(V3)] = 2=k3*/
12956: 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)*/
1.330 brouard 12957: k+=Tvalsel[k3]*pow(2,k4); /* nres=1 k1=2 Tvalsel[1]=1 (V4=1); k1=3 k3=2 Tvalsel[2]=0 (V3=0) */
1.334 brouard 12958: TinvDoQresult[nres][(int)Tvarsel[k3]]=Tvalsel[k3]; /* TinvDoQresult[nres][Name]=Value; stores the value into the name of the variable. */
1.332 brouard 12959: /* Tinvresult[nres][4]=1 */
1.334 brouard 12960: /* Tresult[nres][k4+1]=Tvalsel[k3];/\* Tresult[nres=2][1]=1(V4=1) Tresult[nres=2][2]=0(V3=0) *\/ */
12961: Tresult[nres][k3]=Tvalsel[k3];/* Tresult[nres=2][1]=1(V4=1) Tresult[nres=2][2]=0(V3=0) */
12962: /* Tvresult[nres][k4+1]=(int)Tvarsel[k3];/\* Tvresult[nres][1]=4 Tvresult[nres][3]=1 *\/ */
12963: Tvresult[nres][k3]=(int)Tvarsel[k3];/* Tvresult[nres][1]=4 Tvresult[nres][3]=1 */
1.237 brouard 12964: Tinvresult[nres][(int)Tvarsel[k3]]=Tvalsel[k3]; /* Tinvresult[nres][4]=1 */
1.334 brouard 12965: precov[nres][k1]=Tvalsel[k3]; /* Value from resultline of the variable at the k1 position in the model */
1.342 brouard 12966: /* 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); */
1.235 brouard 12967: k4++;;
1.331 brouard 12968: }else if( Dummy[k1]==1 && Typevar[k1]==0 ){ /* Quantitative and single */
1.330 brouard 12969: /* Tqresult[nres][result_position]= value of the variable at the result_position in the nres resultline */
1.332 brouard 12970: /* Tvqresult[nres][result_position]= id of the variable at the result_position in the nres resultline */
1.330 brouard 12971: /* Tqinvresult[nres][Name of a quantitative variable]= value of the variable in the result line */
1.332 brouard 12972: k3q= resultmodel[nres][k1]; /* resultmodel[1(V5)] = 5 =k3q */
12973: k2q=(int)Tvarsel[k3q]; /* Name of variable at k3q th position in the resultline */
12974: /* Tvarsel[resultmodel[1]]= Tvarsel[1] = 4=k2 */
1.334 brouard 12975: /* Tqresult[nres][k4q+1]=Tvalsel[k3q]; /\* Tqresult[nres][1]=25.1 *\/ */
12976: /* Tvresult[nres][k4q+1]=(int)Tvarsel[k3q];/\* Tvresult[nres][1]=4 Tvresult[nres][3]=1 *\/ */
12977: /* Tvqresult[nres][k4q+1]=(int)Tvarsel[k3q]; /\* Tvqresult[nres][1]=5 *\/ */
12978: Tqresult[nres][k3q]=Tvalsel[k3q]; /* Tqresult[nres][1]=25.1 */
12979: Tvresult[nres][k3q]=(int)Tvarsel[k3q];/* Tvresult[nres][1]=4 Tvresult[nres][3]=1 */
12980: Tvqresult[nres][k3q]=(int)Tvarsel[k3q]; /* Tvqresult[nres][1]=5 */
1.237 brouard 12981: Tqinvresult[nres][(int)Tvarsel[k3q]]=Tvalsel[k3q]; /* Tqinvresult[nres][5]=25.1 */
1.330 brouard 12982: TinvDoQresult[nres][(int)Tvarsel[k3q]]=Tvalsel[k3q]; /* Tqinvresult[nres][5]=25.1 */
1.332 brouard 12983: precov[nres][k1]=Tvalsel[k3q];
1.342 brouard 12984: /* 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]); */
1.235 brouard 12985: k4q++;;
1.350 brouard 12986: }else if( Dummy[k1]==2 ){ /* For dummy with age product "V2+V3+V4+V6+V7+V6*V2+V7*V2+V6*V3+V7*V3+V6*V4+V7*V4+age*V2+age*V3+age*V4+age*V6+age*V7+age*V6*V2+age*V6*V3+age*V7*V3+age*V6*V4+age*V7*V4\r"*/
12987: /* Tvar[k1]; */ /* Age variable */ /* 17 age*V6*V2 ?*/
1.332 brouard 12988: /* Wrong we want the value of variable name Tvar[k1] */
1.350 brouard 12989: if(Typevar[k1]==2 || Typevar[k1]==3 ){ /* For product quant or dummy (with or without age) */
12990: precov[nres][k1]=TinvDoQresult[nres][Tvardk[k1][1]] * TinvDoQresult[nres][Tvardk[k1][2]];
12991: /* 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]]); */
12992: }else{
12993: k3= resultmodel[nres][k1]; /* nres=1 k1=2 resultmodel[2(V4)] = 1=k3 ; k1=3 resultmodel[3(V3)] = 2=k3*/
12994: 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)*/
12995: TinvDoQresult[nres][(int)Tvarsel[k3]]=Tvalsel[k3]; /* TinvDoQresult[nres][4]=1 */
12996: precov[nres][k1]=Tvalsel[k3];
12997: }
1.342 brouard 12998: /* 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]); */
1.331 brouard 12999: }else if( Dummy[k1]==3 ){ /* For quant with age product */
1.350 brouard 13000: if(Typevar[k1]==2 || Typevar[k1]==3 ){ /* For product quant or dummy (with or without age) */
13001: precov[nres][k1]=TinvDoQresult[nres][Tvardk[k1][1]] * TinvDoQresult[nres][Tvardk[k1][2]];
13002: /* 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]]); */
13003: }else{
13004: k3q= resultmodel[nres][k1]; /* resultmodel[1(V5)] = 25.1=k3q */
13005: k2q=(int)Tvarsel[k3q]; /* Tvarsel[resultmodel[1]]= Tvarsel[1] = 4=k2 */
13006: TinvDoQresult[nres][(int)Tvarsel[k3q]]=Tvalsel[k3q]; /* TinvDoQresult[nres][5]=25.1 */
13007: precov[nres][k1]=Tvalsel[k3q];
13008: }
1.342 brouard 13009: /* 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]); */
1.349 brouard 13010: }else if(Typevar[k1]==2 || Typevar[k1]==3 ){ /* For product quant or dummy (with or without age) */
1.332 brouard 13011: precov[nres][k1]=TinvDoQresult[nres][Tvardk[k1][1]] * TinvDoQresult[nres][Tvardk[k1][2]];
1.342 brouard 13012: /* 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]]); */
1.330 brouard 13013: }else{
1.332 brouard 13014: printf("Error Decoderesult probably a product Dummy[%d]==%d && Typevar[%d]==%d\n", k1, Dummy[k1], k1, Typevar[k1]);
13015: fprintf(ficlog,"Error Decoderesult probably a product Dummy[%d]==%d && Typevar[%d]==%d\n", k1, Dummy[k1], k1, Typevar[k1]);
1.235 brouard 13016: }
13017: }
1.234 brouard 13018:
1.334 brouard 13019: TKresult[nres]=++k; /* Number of combinations of dummies for the nresult and the model =Tvalsel[k3]*pow(2,k4) + 1*/
1.230 brouard 13020: return (0);
13021: }
1.235 brouard 13022:
1.230 brouard 13023: int decodemodel( char model[], int lastobs)
13024: /**< This routine decodes the model and returns:
1.224 brouard 13025: * Model V1+V2+V3+V8+V7*V8+V5*V6+V8*age+V3*age+age*age
13026: * - nagesqr = 1 if age*age in the model, otherwise 0.
13027: * - cptcovt total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age
13028: * - cptcovn or number of covariates k of the models excluding age*products =6 and age*age
13029: * - cptcovage number of covariates with age*products =2
13030: * - cptcovs number of simple covariates
1.339 brouard 13031: * ncovcolt=ncovcol+nqv+ntv+nqtv total of covariates in the data, not in the model equation
1.224 brouard 13032: * - 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
1.339 brouard 13033: * which is a new column after the 9 (ncovcol+nqv+ntv+nqtv) variables.
1.319 brouard 13034: * - if k is a product Vn*Vm, covar[k][i] is filled with correct values for each individual
1.224 brouard 13035: * - Tprod[l] gives the kth covariates of the product Vn*Vm l=1 to cptcovprod-cptcovage
13036: * Tprod[1]@2 {5, 6}: position of first product V7*V8 is 5, and second V5*V6 is 6.
13037: * - Tvard[k] p Tvard[1][1]@4 {7, 8, 5, 6} for V7*V8 and V5*V6 .
13038: */
1.319 brouard 13039: /* V2+V1+V4+V3*age Tvar[4]=3 ; V1+V2*age Tvar[2]=2; V1+V1*age Tvar[2]=1, Tage[1]=2 */
1.136 brouard 13040: {
1.359 brouard 13041: int i, j, k, ks;/* , v;*/
1.349 brouard 13042: int n,m;
13043: int j1, k1, k11, k12, k2, k3, k4;
13044: char modelsav[300];
13045: char stra[300], strb[300], strc[300], strd[300],stre[300],strf[300];
1.187 brouard 13046: char *strpt;
1.349 brouard 13047: int **existcomb;
13048:
13049: existcomb=imatrix(1,NCOVMAX,1,NCOVMAX);
13050: for(i=1;i<=NCOVMAX;i++)
13051: for(j=1;j<=NCOVMAX;j++)
13052: existcomb[i][j]=0;
13053:
1.145 brouard 13054: /*removespace(model);*/
1.136 brouard 13055: if (strlen(model) >1){ /* If there is at least 1 covariate */
1.349 brouard 13056: j=0, j1=0, k1=0, k12=0, k2=-1, ks=0, cptcovn=0;
1.137 brouard 13057: if (strstr(model,"AGE") !=0){
1.192 brouard 13058: printf("Error. AGE must be in lower case 'age' model=1+age+%s. ",model);
13059: fprintf(ficlog,"Error. AGE must be in lower case model=1+age+%s. ",model);fflush(ficlog);
1.136 brouard 13060: return 1;
13061: }
1.141 brouard 13062: if (strstr(model,"v") !=0){
1.338 brouard 13063: printf("Error. 'v' must be in upper case 'V' model=1+age+%s ",model);
13064: fprintf(ficlog,"Error. 'v' must be in upper case model=1+age+%s ",model);fflush(ficlog);
1.141 brouard 13065: return 1;
13066: }
1.187 brouard 13067: strcpy(modelsav,model);
13068: if ((strpt=strstr(model,"age*age")) !=0){
1.338 brouard 13069: printf(" strpt=%s, model=1+age+%s\n",strpt, model);
1.187 brouard 13070: if(strpt != model){
1.338 brouard 13071: printf("Error in model: 'model=1+age+%s'; 'age*age' should in first place before other covariates\n \
1.192 brouard 13072: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
1.187 brouard 13073: corresponding column of parameters.\n",model);
1.338 brouard 13074: fprintf(ficlog,"Error in model: 'model=1+age+%s'; 'age*age' should in first place before other covariates\n \
1.192 brouard 13075: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
1.187 brouard 13076: corresponding column of parameters.\n",model); fflush(ficlog);
1.234 brouard 13077: return 1;
1.225 brouard 13078: }
1.187 brouard 13079: nagesqr=1;
13080: if (strstr(model,"+age*age") !=0)
1.234 brouard 13081: substrchaine(modelsav, model, "+age*age");
1.187 brouard 13082: else if (strstr(model,"age*age+") !=0)
1.234 brouard 13083: substrchaine(modelsav, model, "age*age+");
1.187 brouard 13084: else
1.234 brouard 13085: substrchaine(modelsav, model, "age*age");
1.187 brouard 13086: }else
13087: nagesqr=0;
1.349 brouard 13088: 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 */
1.187 brouard 13089: j=nbocc(modelsav,'+'); /**< j=Number of '+' */
13090: j1=nbocc(modelsav,'*'); /**< j1=Number of '*' */
1.351 brouard 13091: cptcovs=0; /**< Number of simple covariates V1 +V1*age +V3 +V3*V4 +age*age => V1 + V3 =4+1-3=2 Wrong */
1.187 brouard 13092: cptcovt= j+1; /* Number of total covariates in the model, not including
1.225 brouard 13093: * cst, age and age*age
13094: * V1+V1*age+ V3 + V3*V4+age*age=> 3+1=4*/
13095: /* including age products which are counted in cptcovage.
13096: * but the covariates which are products must be treated
13097: * separately: ncovn=4- 2=2 (V1+V3). */
1.349 brouard 13098: cptcovprod=0; /**< Number of products V1*V2 +v3*age = 2 */
13099: cptcovdageprod=0; /* Number of doouble products with age age*Vn*VM or Vn*age*Vm or Vn*Vm*age */
1.187 brouard 13100: cptcovprodnoage=0; /**< Number of covariate products without age: V3*V4 =1 */
1.349 brouard 13101: cptcovprodage=0;
13102: /* cptcovprodage=nboccstr(modelsav,"age");*/
1.225 brouard 13103:
1.187 brouard 13104: /* Design
13105: * V1 V2 V3 V4 V5 V6 V7 V8 V9 Weight
13106: * < ncovcol=8 >
13107: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8
13108: * k= 1 2 3 4 5 6 7 8
13109: * cptcovn number of covariates (not including constant and age ) = # of + plus 1 = 7+1=8
1.345 brouard 13110: * covar[k,i], are for fixed covariates, value of kth covariate if not including age for individual i:
1.224 brouard 13111: * covar[1][i]= (V1), covar[4][i]=(V4), covar[8][i]=(V8)
13112: * Tvar[k] # of the kth covariate: Tvar[1]=2 Tvar[2]=1 Tvar[4]=3 Tvar[8]=8
1.187 brouard 13113: * if multiplied by age: V3*age Tvar[3=V3*age]=3 (V3) Tvar[7]=8 and
13114: * Tage[++cptcovage]=k
1.345 brouard 13115: * if products, new covar are created after ncovcol + nqv (quanti fixed) with k1
1.187 brouard 13116: * Tvar[k]=ncovcol+k1; # of the kth covariate product: Tvar[5]=ncovcol+1=10 Tvar[6]=ncovcol+1=11
13117: * Tprod[k1]=k; Tprod[1]=5 Tprod[2]= 6; gives the position of the k1th product
13118: * 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
13119: * Tvar[cptcovn+k2]=Tvard[k1][1];Tvar[cptcovn+k2+1]=Tvard[k1][2];
13120: * Tvar[8+1]=5;Tvar[8+2]=6;Tvar[8+3]=7;Tvar[8+4]=8 inverted
13121: * V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11
1.345 brouard 13122: * < ncovcol=8 8 fixed covariate. Additional starts at 9 (V5*V6) and 10(V7*V8) >
1.187 brouard 13123: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8 d1 d1 d2 d2
13124: * k= 1 2 3 4 5 6 7 8 9 10 11 12
1.345 brouard 13125: * Tvard[k]= 2 1 3 3 10 11 8 8 5 6 7 8
13126: * p Tvar[1]@12={2, 1, 3, 3, 9, 10, 8, 8}
1.187 brouard 13127: * p Tprod[1]@2={ 6, 5}
13128: *p Tvard[1][1]@4= {7, 8, 5, 6}
13129: * covar[k][i]= V2 V1 ? V3 V5*V6? V7*V8? ? V8
13130: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
1.319 brouard 13131: *How to reorganize? Tvars(orted)
1.187 brouard 13132: * Model V1 + V2 + V3 + V8 + V5*V6 + V7*V8 + V3*age + V8*age
13133: * Tvars {2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
13134: * {2, 1, 4, 8, 5, 6, 3, 7}
13135: * Struct []
13136: */
1.225 brouard 13137:
1.187 brouard 13138: /* This loop fills the array Tvar from the string 'model'.*/
13139: /* j is the number of + signs in the model V1+V2+V3 j=2 i=3 to 1 */
13140: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4 */
13141: /* k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tage[cptcovage=1]=4 */
13142: /* k=3 V4 Tvar[k=3]= 4 (from V4) */
13143: /* k=2 V1 Tvar[k=2]= 1 (from V1) */
13144: /* k=1 Tvar[1]=2 (from V2) */
13145: /* k=5 Tvar[5] */
13146: /* for (k=1; k<=cptcovn;k++) { */
1.198 brouard 13147: /* cov[2+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
1.187 brouard 13148: /* } */
1.198 brouard 13149: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k])]]*cov[2]; */
1.187 brouard 13150: /*
13151: * Treating invertedly V2+V1+V3*age+V2*V4 is as if written V2*V4 +V3*age + V1 + V2 */
1.227 brouard 13152: for(k=cptcovt; k>=1;k--){ /**< Number of covariates not including constant and age, neither age*age*/
13153: Tvar[k]=0; Tprod[k]=0; Tposprod[k]=0;
13154: }
1.187 brouard 13155: cptcovage=0;
1.351 brouard 13156:
13157: /* First loop in order to calculate */
13158: /* for age*VN*Vm
13159: * Provides, Typevar[k], Tage[cptcovage], existcomb[n][m], FixedV[ncovcolt+k12]
13160: * Tprod[k1]=k Tposprod[k]=k1; Tvard[k1][1] =m;
13161: */
13162: /* Needs FixedV[Tvardk[k][1]] */
13163: /* For others:
13164: * Sets Typevar[k];
13165: * Tvar[k]=ncovcol+nqv+ntv+nqtv+k11;
13166: * Tposprod[k]=k11;
13167: * Tprod[k11]=k;
13168: * Tvardk[k][1] =m;
13169: * Needs FixedV[Tvardk[k][1]] == 0
13170: */
13171:
1.319 brouard 13172: for(k=1; k<=cptcovt;k++){ /* Loop on total covariates of the model line */
13173: cutl(stra,strb,modelsav,'+'); /* keeps in strb after the first '+' cutl from left to right
13174: 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" */
13175: if (nbocc(modelsav,'+')==0)
13176: strcpy(strb,modelsav); /* and analyzes it */
1.234 brouard 13177: /* printf("i=%d a=%s b=%s sav=%s\n",i, stra,strb,modelsav);*/
13178: /*scanf("%d",i);*/
1.349 brouard 13179: 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 */
13180: cutl(strc,strd,strb,'*'); /**< k=1 strd*strc Vm*Vn: strb=V3*age(input) strc=age strd=V3 ; V3*V2 strc=V2, strd=V3 OR strb=age*V6*V2 strc=V6*V2 strd=age OR c=V2*age OR c=age*V2 */
13181: 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 */
13182: Typevar[k]=3; /* 3 for age and double product age*Vn*Vm varying of fixed */
13183: if(strstr(strc,"age")!=0) { /* It means that strc=V2*age or age*V2 and thus that strd=Vn */
13184: cutl(stre,strf,strc,'*') ; /* strf=age or Vm, stre=Vm or age. If strc=V6*V2 then strf=V6 and stre=V2 */
13185: strcpy(strc,strb); /* save strb(=age*Vn*Vm) into strc */
13186: /* We want strb=Vn*Vm */
13187: if(strcmp(strf,"age")==0){ /* strf is "age" so that stre=Vm =V2 . */
13188: strcpy(strb,strd);
13189: strcat(strb,"*");
13190: strcat(strb,stre);
13191: }else{ /* strf=Vm If strf=V6 then stre=V2 */
13192: strcpy(strb,strf);
13193: strcat(strb,"*");
13194: strcat(strb,stre);
13195: strcpy(strd,strb); /* in order for strd to not be "age" for next test (will be Vn*Vm */
13196: }
1.351 brouard 13197: /* 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]]]); */
13198: /* FixedV[Tvar[Tage[k]]]=0; /\* HERY not sure if V7*V4*age Fixed might not exist yet*\/ */
1.349 brouard 13199: }else{ /* strc=Vn*Vm (and strd=age) and should be strb=Vn*Vm but want to keep original strb double product */
13200: strcpy(stre,strb); /* save full b in stre */
13201: strcpy(strb,strc); /* save short c in new short b for next block strb=Vn*Vm*/
13202: strcpy(strf,strc); /* save short c in new short f */
13203: cutl(strc,strd,strf,'*'); /* We get strd=Vn and strc=Vm for next block (strb=Vn*Vm)*/
13204: /* strcpy(strc,stre);*/ /* save full e in c for future */
13205: }
13206: cptcovdageprod++; /* double product with age Which product is it? */
13207: /* 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 *\/ */
13208: /* cutl(strc,strd,strb,'*'); /\* strd= V6 or V2 or age and strc= V2 or age or V2 *\/ */
1.234 brouard 13209: cutl(stre,strb,strc,'V'); /* strc= Vn, stre is n; strb=V3*V2 stre=3 strc=*/
1.349 brouard 13210: n=atoi(stre);
1.234 brouard 13211: cutl(strc,strb,strd,'V'); /* strd was Vm, strc is m */
1.349 brouard 13212: m=atoi(strc);
13213: cptcovage++; /* Counts the number of covariates which include age as a product */
13214: Tage[cptcovage]=k; /* For age*V3*V2 gives the position in model of covariates associated with age Tage[1]=6 HERY too*/
13215: if(existcomb[n][m] == 0){
13216: /* r /home/brouard/Documents/Recherches/REVES/Zachary/Zach-2022/Feinuo_Sun/Feinuo-threeway/femV12V15_3wayintNBe.imach */
13217: 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);
13218: 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);
13219: fflush(ficlog);
13220: k1++; /* The combination Vn*Vm will be in the model so we create it at k1 */
13221: k12++;
13222: existcomb[n][m]=k1;
13223: existcomb[m][n]=k1;
13224: Tvar[k]=ncovcol+nqv+ntv+nqtv+k1;
13225: 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*/
13226: Tposprod[k]=k1; /* Tposprod[3]=1, Tposprod[2]=5 Gives the k1 double product Vn*Vm or age*Vn*Vm at the k position */
13227: Tvard[k1][1] =m; /* m 1 for V1*/
13228: Tvardk[k][1] =m; /* m 1 for V1*/
13229: Tvard[k1][2] =n; /* n 4 for V4*/
13230: Tvardk[k][2] =n; /* n 4 for V4*/
1.351 brouard 13231: /* Tvar[Tage[cptcovage]]=k1;*/ /* Tvar[6=age*V3*V2]=9 (new fixed covariate) */ /* We don't know about Fixed yet HERE */
1.349 brouard 13232: 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 */
13233: for (i=1; i<=lastobs;i++){/* For fixed product */
13234: /* Computes the new covariate which is a product of
13235: covar[n][i]* covar[m][i] and stores it at ncovol+k1 May not be defined */
13236: covar[ncovcolt+k1][i]=covar[atoi(stre)][i]*covar[atoi(strc)][i];
13237: }
13238: cptcovprodage++; /* Counting the number of fixed covariate with age */
13239: FixedV[ncovcolt+k12]=0; /* We expand Vn*Vm */
13240: k12++;
13241: FixedV[ncovcolt+k12]=0;
13242: }else{ /*End of FixedV */
13243: cptcovprodvage++; /* Counting the number of varying covariate with age */
13244: FixedV[ncovcolt+k12]=1; /* We expand Vn*Vm */
13245: k12++;
13246: FixedV[ncovcolt+k12]=1;
13247: }
13248: }else{ /* k1 Vn*Vm already exists */
13249: k11=existcomb[n][m];
13250: Tposprod[k]=k11; /* OK */
13251: Tvar[k]=Tvar[Tprod[k11]]; /* HERY */
13252: Tvardk[k][1]=m;
13253: Tvardk[k][2]=n;
13254: 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 */
13255: /*cptcovage++;*/ /* Counts the number of covariates which include age as a product */
13256: cptcovprodage++; /* Counting the number of fixed covariate with age */
13257: /*Tage[cptcovage]=k;*/ /* For age*V3*V2 Tage[1]=V3*V3=9 HERY too*/
13258: Tvar[Tage[cptcovage]]=k1;
13259: FixedV[ncovcolt+k12]=0; /* We expand Vn*Vm */
13260: k12++;
13261: FixedV[ncovcolt+k12]=0;
13262: }else{ /* Already exists but time varying (and age) */
13263: /*cptcovage++;*/ /* Counts the number of covariates which include age as a product */
13264: /*Tage[cptcovage]=k;*/ /* For age*V3*V2 Tage[1]=V3*V3=9 HERY too*/
13265: /* Tvar[Tage[cptcovage]]=k1; */
13266: cptcovprodvage++;
13267: FixedV[ncovcolt+k12]=1; /* We expand Vn*Vm */
13268: k12++;
13269: FixedV[ncovcolt+k12]=1;
13270: }
13271: }
13272: /* Tage[cptcovage]=k; /\* V2+V1+V4+V3*age Tvar[4]=3, Tage[1] = 4 or V1+V1*age Tvar[2]=1, Tage[1]=2 *\/ */
13273: /* Tvar[k]=k11; /\* HERY *\/ */
13274: } 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 */
13275: cptcovprod++;
13276: if (strcmp(strc,"age")==0) { /**< Model includes age: strb= Vn*age c=age d=Vn*/
13277: /* covar is not filled and then is empty */
13278: cutl(stre,strb,strd,'V'); /* strd=V3(input): stre="3" */
13279: 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 */
13280: Typevar[k]=1; /* 1 for age product */
13281: cptcovage++; /* Counts the number of covariates which include age as a product */
13282: Tage[cptcovage]=k; /* V2+V1+V4+V3*age Tvar[4]=3, Tage[1] = 4 or V1+V1*age Tvar[2]=1, Tage[1]=2 */
13283: if( FixedV[Tvar[k]] == 0){
13284: cptcovprodage++; /* Counting the number of fixed covariate with age */
13285: }else{
13286: cptcovprodvage++; /* Counting the number of fixedvarying covariate with age */
13287: }
13288: /*printf("stre=%s ", stre);*/
13289: } else if (strcmp(strd,"age")==0) { /* strb= age*Vn c=Vn */
13290: cutl(stre,strb,strc,'V');
13291: Tvar[k]=atoi(stre);
13292: Typevar[k]=1; /* 1 for age product */
13293: cptcovage++;
13294: Tage[cptcovage]=k;
13295: if( FixedV[Tvar[k]] == 0){
13296: cptcovprodage++; /* Counting the number of fixed covariate with age */
13297: }else{
13298: cptcovprodvage++; /* Counting the number of fixedvarying covariate with age */
1.339 brouard 13299: }
1.349 brouard 13300: }else{ /* for product Vn*Vm */
13301: Typevar[k]=2; /* 2 for product Vn*Vm */
13302: cutl(stre,strb,strc,'V'); /* strc= Vn, stre is n; strb=V3*V2 stre=3 strc=*/
13303: n=atoi(stre);
13304: cutl(strc,strb,strd,'V'); /* strd was Vm, strc is m */
13305: m=atoi(strc);
13306: k1++;
13307: cptcovprodnoage++;
13308: if(existcomb[n][m] != 0 || existcomb[m][n] != 0){
13309: printf("Warning in model combination V%d*V%d already exists in the model in position k1=%d!\n",n,m,existcomb[n][m]);
13310: 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]);
13311: fflush(ficlog);
13312: k11=existcomb[n][m];
13313: Tvar[k]=ncovcol+nqv+ntv+nqtv+k11;
13314: Tposprod[k]=k11;
13315: Tprod[k11]=k;
13316: Tvardk[k][1] =m; /* m 1 for V1*/
13317: /* Tvard[k11][1] =m; /\* n 4 for V4*\/ */
13318: Tvardk[k][2] =n; /* n 4 for V4*/
13319: /* Tvard[k11][2] =n; /\* n 4 for V4*\/ */
13320: }else{ /* combination Vn*Vm doesn't exist we create it (no age)*/
13321: existcomb[n][m]=k1;
13322: existcomb[m][n]=k1;
13323: Tvar[k]=ncovcol+nqv+ntv+nqtv+k1; /* ncovcolt+k1; For model-covariate k tells which data-covariate to use but
13324: because this model-covariate is a construction we invent a new column
13325: which is after existing variables ncovcol+nqv+ntv+nqtv + k1
13326: If already ncovcol=4 and model= V2 + V1 + V1*V4 + age*V3 + V3*V2
13327: thus after V4 we invent V5 and V6 because age*V3 will be computed in 4
13328: Tvar[3=V1*V4]=4+1=5 Tvar[5=V3*V2]=4 + 2= 6, Tvar[4=age*V3]=3 etc */
13329: /* Please remark that the new variables are model dependent */
13330: /* If we have 4 variable but the model uses only 3, like in
13331: * model= V1 + age*V1 + V2 + V3 + age*V2 + age*V3 + V1*V2 + V1*V3
13332: * k= 1 2 3 4 5 6 7 8
13333: * Tvar[k]=1 1 2 3 2 3 (5 6) (and not 4 5 because of V4 missing)
13334: * Tage[kk] [1]= 2 [2]=5 [3]=6 kk=1 to cptcovage=3
13335: * Tvar[Tage[kk]][1]=2 [2]=2 [3]=3
13336: */
13337: /* We need to feed some variables like TvarVV, but later on next loop because of ncovv (k2) is not correct */
13338: Tprod[k1]=k; /* Tprod[1]=3(=V1*V4) for V2+V1+V1*V4+age*V3+V3*V2 +V6*V2*age */
13339: Tposprod[k]=k1; /* Tposprod[3]=1, Tposprod[2]=5 */
13340: Tvard[k1][1] =m; /* m 1 for V1*/
13341: Tvardk[k][1] =m; /* m 1 for V1*/
13342: Tvard[k1][2] =n; /* n 4 for V4*/
13343: Tvardk[k][2] =n; /* n 4 for V4*/
13344: k2=k2+2; /* k2 is initialize to -1, We want to store the n and m in Vn*Vm at the end of Tvar */
13345: /* Tvar[cptcovt+k2]=Tvard[k1][1]; /\* Tvar[(cptcovt=4+k2=1)=5]= 1 (V1) *\/ */
13346: /* Tvar[cptcovt+k2+1]=Tvard[k1][2]; /\* Tvar[(cptcovt=4+(k2=1)+1)=6]= 4 (V4) *\/ */
13347: /*ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2, Tvar[3]=5, Tvar[4]=6, cptcovt=5 */
13348: /* 1 2 3 4 5 | Tvar[5+1)=1, Tvar[7]=2 */
13349: 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 */
13350: for (i=1; i<=lastobs;i++){/* For fixed product */
13351: /* Computes the new covariate which is a product of
13352: covar[n][i]* covar[m][i] and stores it at ncovol+k1 May not be defined */
13353: covar[ncovcolt+k1][i]=covar[atoi(stre)][i]*covar[atoi(strc)][i];
13354: }
13355: /* TvarVV[k2]=n; */
13356: /* FixedV[ncovcolt+k2]=0; /\* or FixedV[Tvar[k]]=0; FixedV[TvarVV[ncovv]]=0 HERE *\/ */
13357: /* TvarVV[k2+1]=m; */
13358: /* FixedV[ncovcolt+k2]=0; /\* or FixedV[Tvar[k]]=0; FixedV[TvarVV[ncovv]]=0 HERE *\/ */
13359: }else{ /* not FixedV */
13360: /* TvarVV[k2]=n; */
13361: /* FixedV[ncovcolt+k2]=0; /\* or FixedV[Tvar[k]]=0; FixedV[TvarVV[ncovv]]=0 HERE *\/ */
13362: /* TvarVV[k2+1]=m; */
13363: /* FixedV[ncovcolt+k2]=0; /\* or FixedV[Tvar[k]]=0; FixedV[TvarVV[ncovv]]=0 HERE *\/ */
13364: }
13365: } /* End of creation of Vn*Vm if not created by age*Vn*Vm earlier */
13366: } /* End of product Vn*Vm */
13367: } /* End of age*double product or simple product */
13368: }else { /* not a product */
1.234 brouard 13369: /*printf("d=%s c=%s b=%s\n", strd,strc,strb);*/
13370: /* scanf("%d",i);*/
13371: cutl(strd,strc,strb,'V');
13372: ks++; /**< Number of simple covariates dummy or quantitative, fixe or varying */
13373: cptcovn++; /** V4+V3+V5: V4 and V3 timevarying dummy covariates, V5 timevarying quantitative */
13374: Tvar[k]=atoi(strd);
13375: Typevar[k]=0; /* 0 for simple covariates */
13376: }
13377: strcpy(modelsav,stra); /* modelsav=V2+V1+V4 stra=V2+V1+V4 */
1.223 brouard 13378: /*printf("a=%s b=%s sav=%s\n", stra,strb,modelsav);
1.225 brouard 13379: scanf("%d",i);*/
1.187 brouard 13380: } /* end of loop + on total covariates */
1.351 brouard 13381:
13382:
1.187 brouard 13383: } /* end if strlen(modelsave == 0) age*age might exist */
13384: } /* end if strlen(model == 0) */
1.349 brouard 13385: 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 */
13386:
1.136 brouard 13387: /*The number n of Vn is stored in Tvar. cptcovage =number of age covariate. Tage gives the position of age. cptcovprod= number of products.
13388: If model=V1+V1*age then Tvar[1]=1 Tvar[2]=1 cptcovage=1 Tage[1]=2 cptcovprod=0*/
1.225 brouard 13389:
1.136 brouard 13390: /* printf("tvar1=%d tvar2=%d tvar3=%d cptcovage=%d Tage=%d",Tvar[1],Tvar[2],Tvar[3],cptcovage,Tage[1]);
1.225 brouard 13391: printf("cptcovprod=%d ", cptcovprod);
13392: fprintf(ficlog,"cptcovprod=%d ", cptcovprod);
13393: scanf("%d ",i);*/
13394:
13395:
1.230 brouard 13396: /* Until here, decodemodel knows only the grammar (simple, product, age*) of the model but not what kind
13397: of variable (dummy vs quantitative, fixed vs time varying) is behind. But we know the # of each. */
1.226 brouard 13398: /* ncovcol= 1, nqv=1 | ntv=2, nqtv= 1 = 5 possible variables data: 2 fixed 3, varying
13399: model= V5 + V4 +V3 + V4*V3 + V5*age + V2 + V1*V2 + V1*age + V5*age, V1 is not used saving its place
13400: k = 1 2 3 4 5 6 7 8 9
13401: Tvar[k]= 5 4 3 1+1+2+1+1=6 5 2 7 1 5
1.319 brouard 13402: Typevar[k]= 0 0 0 2 1 0 2 1 0
1.227 brouard 13403: Fixed[k] 1 1 1 1 3 0 0 or 2 2 3
13404: Dummy[k] 1 0 0 0 3 1 1 2 3
13405: Tmodelind[combination of covar]=k;
1.225 brouard 13406: */
13407: /* Dispatching between quantitative and time varying covariates */
1.226 brouard 13408: /* If Tvar[k] >ncovcol it is a product */
1.225 brouard 13409: /* Tvar[k] is the value n of Vn with n varying for 1 to nvcol, or p Vp=Vn*Vm for product */
1.226 brouard 13410: /* Computing effective variables, ie used by the model, that is from the cptcovt variables */
1.318 brouard 13411: printf("Model=1+age+%s\n\
1.349 brouard 13412: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product, 3 for double product with age \n\
1.227 brouard 13413: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\
13414: 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);
1.318 brouard 13415: fprintf(ficlog,"Model=1+age+%s\n\
1.349 brouard 13416: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product, 3 for double product with age \n\
1.227 brouard 13417: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\
13418: 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);
1.342 brouard 13419: for(k=-1;k<=NCOVMAX; k++){ Fixed[k]=0; Dummy[k]=0;}
13420: for(k=1;k<=NCOVMAX; k++){TvarFind[k]=0; TvarVind[k]=0;}
1.351 brouard 13421:
13422:
13423: /* Second loop for calculating Fixed[k], Dummy[k]*/
13424:
13425:
1.349 brouard 13426: 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 */
1.234 brouard 13427: if (Tvar[k] <=ncovcol && Typevar[k]==0 ){ /* Simple fixed dummy (<=ncovcol) covariates */
1.227 brouard 13428: Fixed[k]= 0;
13429: Dummy[k]= 0;
1.225 brouard 13430: ncoveff++;
1.232 brouard 13431: ncovf++;
1.234 brouard 13432: nsd++;
13433: modell[k].maintype= FTYPE;
13434: TvarsD[nsd]=Tvar[k];
13435: TvarsDind[nsd]=k;
1.330 brouard 13436: TnsdVar[Tvar[k]]=nsd;
1.234 brouard 13437: TvarF[ncovf]=Tvar[k];
13438: TvarFind[ncovf]=k;
13439: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
13440: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1.339 brouard 13441: /* }else if( Tvar[k] <=ncovcol && Typevar[k]==2){ /\* Product of fixed dummy (<=ncovcol) covariates For a fixed product k is higher than ncovcol *\/ */
1.240 brouard 13442: }else if( Tvar[k] <=ncovcol+nqv && Typevar[k]==0){/* Remind that product Vn*Vm are added in k Only simple fixed quantitative variable */
1.227 brouard 13443: Fixed[k]= 0;
13444: Dummy[k]= 1;
1.230 brouard 13445: nqfveff++;
1.234 brouard 13446: modell[k].maintype= FTYPE;
13447: modell[k].subtype= FQ;
13448: nsq++;
1.334 brouard 13449: TvarsQ[nsq]=Tvar[k]; /* Gives the variable name (extended to products) of first nsq simple quantitative covariates (fixed or time vary see below */
13450: TvarsQind[nsq]=k; /* Gives the position in the model equation of the first nsq simple quantitative covariates (fixed or time vary) */
1.232 brouard 13451: ncovf++;
1.234 brouard 13452: TvarF[ncovf]=Tvar[k];
13453: TvarFind[ncovf]=k;
1.231 brouard 13454: TvarFQ[nqfveff]=Tvar[k]-ncovcol; /* TvarFQ[1]=V2-1=1st in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1.230 brouard 13455: TvarFQind[nqfveff]=k; /* TvarFQind[1]=6 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1.242 brouard 13456: }else if( Tvar[k] <=ncovcol+nqv+ntv && Typevar[k]==0){/* Only simple time varying dummy variables */
1.339 brouard 13457: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
13458: /* model V1+V3+age*V1+age*V3+V1*V3 */
13459: /* Tvar={1, 3, 1, 3, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
13460: ncovvt++;
13461: TvarVV[ncovvt]=Tvar[k]; /* TvarVV[1]=V3 (first time varying in the model equation */
13462: TvarVVind[ncovvt]=k; /* TvarVVind[1]=2 (second position in the model equation */
13463:
1.227 brouard 13464: Fixed[k]= 1;
13465: Dummy[k]= 0;
1.225 brouard 13466: ntveff++; /* Only simple time varying dummy variable */
1.234 brouard 13467: modell[k].maintype= VTYPE;
13468: modell[k].subtype= VD;
13469: nsd++;
13470: TvarsD[nsd]=Tvar[k];
13471: TvarsDind[nsd]=k;
1.330 brouard 13472: TnsdVar[Tvar[k]]=nsd; /* To be verified */
1.234 brouard 13473: ncovv++; /* Only simple time varying variables */
13474: TvarV[ncovv]=Tvar[k];
1.242 brouard 13475: 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 */
1.231 brouard 13476: 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 */
13477: TvarVDind[ntveff]=k; /* TvarVDind[1]=2 TvarVDind[2]=3 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple time varying dummy variable */
1.228 brouard 13478: 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);
13479: printf("Quasi TmodelInvind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv);
1.231 brouard 13480: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv && Typevar[k]==0){ /* Only simple time varying quantitative variable V5*/
1.339 brouard 13481: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
13482: /* model V1+V3+age*V1+age*V3+V1*V3 */
13483: /* Tvar={1, 3, 1, 3, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
13484: ncovvt++;
13485: TvarVV[ncovvt]=Tvar[k]; /* TvarVV[1]=V3 (first time varying in the model equation */
13486: TvarVVind[ncovvt]=k; /* TvarVV[1]=V3 (first time varying in the model equation */
13487:
1.234 brouard 13488: Fixed[k]= 1;
13489: Dummy[k]= 1;
13490: nqtveff++;
13491: modell[k].maintype= VTYPE;
13492: modell[k].subtype= VQ;
13493: ncovv++; /* Only simple time varying variables */
13494: nsq++;
1.334 brouard 13495: 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) */
13496: 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) */
1.234 brouard 13497: TvarV[ncovv]=Tvar[k];
1.242 brouard 13498: TvarVind[ncovv]=k; /* TvarVind[1]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Any time varying singele */
1.231 brouard 13499: 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 */
13500: TvarVQind[nqtveff]=k; /* TvarVQind[1]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple time varying quantitative variable */
1.234 brouard 13501: TmodelInvQind[nqtveff]=Tvar[k]- ncovcol-nqv-ntv;/* Only simple time varying quantitative variable */
13502: /* Tmodeliqind[k]=nqtveff;/\* Only simple time varying quantitative variable *\/ */
1.349 brouard 13503: /* 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); */
1.342 brouard 13504: /* printf("Quasi TmodelInvQind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv-ntv); */
1.227 brouard 13505: }else if (Typevar[k] == 1) { /* product with age */
1.234 brouard 13506: ncova++;
13507: TvarA[ncova]=Tvar[k];
13508: TvarAind[ncova]=k;
1.349 brouard 13509: /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
13510: /** Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product */
1.231 brouard 13511: if (Tvar[k] <=ncovcol ){ /* Product age with fixed dummy covariatee */
1.240 brouard 13512: Fixed[k]= 2;
13513: Dummy[k]= 2;
13514: modell[k].maintype= ATYPE;
13515: modell[k].subtype= APFD;
1.349 brouard 13516: ncovta++;
13517: TvarAVVA[ncovta]=Tvar[k]; /* (2)age*V3 */
13518: TvarAVVAind[ncovta]=k;
1.240 brouard 13519: /* ncoveff++; */
1.227 brouard 13520: }else if( Tvar[k] <=ncovcol+nqv) { /* Remind that product Vn*Vm are added in k*/
1.240 brouard 13521: Fixed[k]= 2;
13522: Dummy[k]= 3;
13523: modell[k].maintype= ATYPE;
13524: modell[k].subtype= APFQ; /* Product age * fixed quantitative */
1.349 brouard 13525: ncovta++;
13526: TvarAVVA[ncovta]=Tvar[k]; /* */
13527: TvarAVVAind[ncovta]=k;
1.240 brouard 13528: /* nqfveff++; /\* Only simple fixed quantitative variable *\/ */
1.227 brouard 13529: }else if( Tvar[k] <=ncovcol+nqv+ntv ){
1.240 brouard 13530: Fixed[k]= 3;
13531: Dummy[k]= 2;
13532: modell[k].maintype= ATYPE;
13533: modell[k].subtype= APVD; /* Product age * varying dummy */
1.349 brouard 13534: ncovva++;
13535: TvarVVA[ncovva]=Tvar[k]; /* (1)+age*V6 + (2)age*V7 */
13536: TvarVVAind[ncovva]=k;
13537: ncovta++;
13538: TvarAVVA[ncovta]=Tvar[k]; /* */
13539: TvarAVVAind[ncovta]=k;
1.240 brouard 13540: /* ntveff++; /\* Only simple time varying dummy variable *\/ */
1.227 brouard 13541: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv){
1.240 brouard 13542: Fixed[k]= 3;
13543: Dummy[k]= 3;
13544: modell[k].maintype= ATYPE;
13545: modell[k].subtype= APVQ; /* Product age * varying quantitative */
1.349 brouard 13546: ncovva++;
13547: TvarVVA[ncovva]=Tvar[k]; /* */
13548: TvarVVAind[ncovva]=k;
13549: ncovta++;
13550: TvarAVVA[ncovta]=Tvar[k]; /* (1)+age*V6 + (2)age*V7 */
13551: TvarAVVAind[ncovta]=k;
1.240 brouard 13552: /* nqtveff++;/\* Only simple time varying quantitative variable *\/ */
1.227 brouard 13553: }
1.349 brouard 13554: }else if( Tposprod[k]>0 && Typevar[k]==2){ /* Detects if fixed product no age Vm*Vn */
13555: printf("MEMORY ERRORR k=%d Tposprod[k]=%d, Typevar[k]=%d\n ",k, Tposprod[k], Typevar[k]);
13556: 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 */
13557: 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]]);
13558: Fixed[k]= 0;
13559: Dummy[k]= 0;
13560: ncoveff++;
13561: ncovf++;
13562: /* ncovv++; */
13563: /* TvarVV[ncovv]=Tvardk[k][1]; */
13564: /* FixedV[ncovcolt+ncovv]=0; /\* or FixedV[TvarVV[ncovv]]=0 HERE *\/ */
13565: /* ncovv++; */
13566: /* TvarVV[ncovv]=Tvardk[k][2]; */
13567: /* FixedV[ncovcolt+ncovv]=0; /\* or FixedV[TvarVV[ncovv]]=0 HERE *\/ */
13568: modell[k].maintype= FTYPE;
13569: TvarF[ncovf]=Tvar[k];
13570: /* TnsdVar[Tvar[k]]=nsd; */ /* To be done */
13571: TvarFind[ncovf]=k;
13572: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
13573: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
13574: }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 */
13575: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
13576: /* model V1+V3+age*V1+age*V3+V1*V3 + V1*V3*age*/
13577: /* Tvar={1, 3, 1, 3, 6, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
13578: 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 */
13579: ncovvt++;
13580: TvarVV[ncovvt]=Tvard[k1][1]; /* TvarVV[2]=V1 (because TvarVV[1] was V3, first time varying covariates */
13581: TvarVVind[ncovvt]=k; /* TvarVVind[2]=5 (because TvarVVind[2] was V1*V3 at position 5 */
13582: ncovvt++;
13583: TvarVV[ncovvt]=Tvard[k1][2]; /* TvarVV[3]=V3 */
13584: TvarVVind[ncovvt]=k; /* TvarVVind[2]=5 (because TvarVVind[2] was V1*V3 at position 5 */
13585:
13586: /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
13587: /** Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product */
13588:
13589: if(Tvard[k1][1] <=ncovcol){ /* Vn is dummy fixed, (Tvard[1][1]=V1), (Tvard[1][1]=V3 time varying) */
13590: if(Tvard[k1][2] <=ncovcol){ /* Vm is dummy fixed */
13591: Fixed[k]= 1;
13592: Dummy[k]= 0;
13593: modell[k].maintype= FTYPE;
13594: modell[k].subtype= FPDD; /* Product fixed dummy * fixed dummy */
13595: ncovf++; /* Fixed variables without age */
13596: TvarF[ncovf]=Tvar[k];
13597: TvarFind[ncovf]=k;
13598: }else if(Tvard[k1][2] <=ncovcol+nqv){ /* Vm is quanti fixed */
13599: Fixed[k]= 0; /* Fixed product */
13600: Dummy[k]= 1;
13601: modell[k].maintype= FTYPE;
13602: modell[k].subtype= FPDQ; /* Product fixed dummy * fixed quantitative */
13603: ncovf++; /* Varying variables without age */
13604: TvarF[ncovf]=Tvar[k];
13605: TvarFind[ncovf]=k;
13606: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){ /* Vm is a time varying dummy covariate */
13607: Fixed[k]= 1;
13608: Dummy[k]= 0;
13609: modell[k].maintype= VTYPE;
13610: modell[k].subtype= VPDD; /* Product fixed dummy * varying dummy */
13611: ncovv++; /* Varying variables without age */
13612: TvarV[ncovv]=Tvar[k]; /* TvarV[1]=Tvar[5]=5 because there is a V4 */
13613: TvarVind[ncovv]=k;/* TvarVind[1]=5 */
13614: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){ /* Vm is a time varying quantitative covariate */
13615: Fixed[k]= 1;
13616: Dummy[k]= 1;
13617: modell[k].maintype= VTYPE;
13618: modell[k].subtype= VPDQ; /* Product fixed dummy * varying quantitative */
13619: ncovv++; /* Varying variables without age */
13620: TvarV[ncovv]=Tvar[k];
13621: TvarVind[ncovv]=k;
13622: }
13623: }else if(Tvard[k1][1] <=ncovcol+nqv){ /* Vn is fixed quanti */
13624: if(Tvard[k1][2] <=ncovcol){ /* Vm is fixed dummy */
13625: Fixed[k]= 0; /* Fixed product */
13626: Dummy[k]= 1;
13627: modell[k].maintype= FTYPE;
13628: modell[k].subtype= FPDQ; /* Product fixed quantitative * fixed dummy */
13629: ncovf++; /* Fixed variables without age */
13630: TvarF[ncovf]=Tvar[k];
13631: TvarFind[ncovf]=k;
13632: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){ /* Vm is time varying */
13633: Fixed[k]= 1;
13634: Dummy[k]= 1;
13635: modell[k].maintype= VTYPE;
13636: modell[k].subtype= VPDQ; /* Product fixed quantitative * varying dummy */
13637: ncovv++; /* Varying variables without age */
13638: TvarV[ncovv]=Tvar[k];
13639: TvarVind[ncovv]=k;
13640: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){ /* Vm is time varying quanti */
13641: Fixed[k]= 1;
13642: Dummy[k]= 1;
13643: modell[k].maintype= VTYPE;
13644: modell[k].subtype= VPQQ; /* Product fixed quantitative * varying quantitative */
13645: ncovv++; /* Varying variables without age */
13646: TvarV[ncovv]=Tvar[k];
13647: TvarVind[ncovv]=k;
13648: ncovv++; /* Varying variables without age */
13649: TvarV[ncovv]=Tvar[k];
13650: TvarVind[ncovv]=k;
13651: }
13652: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv){ /* Vn is time varying dummy */
13653: if(Tvard[k1][2] <=ncovcol){
13654: Fixed[k]= 1;
13655: Dummy[k]= 1;
13656: modell[k].maintype= VTYPE;
13657: modell[k].subtype= VPDD; /* Product time varying dummy * fixed dummy */
13658: ncovv++; /* Varying variables without age */
13659: TvarV[ncovv]=Tvar[k];
13660: TvarVind[ncovv]=k;
13661: }else if(Tvard[k1][2] <=ncovcol+nqv){
13662: Fixed[k]= 1;
13663: Dummy[k]= 1;
13664: modell[k].maintype= VTYPE;
13665: modell[k].subtype= VPDQ; /* Product time varying dummy * fixed quantitative */
13666: ncovv++; /* Varying variables without age */
13667: TvarV[ncovv]=Tvar[k];
13668: TvarVind[ncovv]=k;
13669: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
13670: Fixed[k]= 1;
13671: Dummy[k]= 0;
13672: modell[k].maintype= VTYPE;
13673: modell[k].subtype= VPDD; /* Product time varying dummy * time varying dummy */
13674: ncovv++; /* Varying variables without age */
13675: TvarV[ncovv]=Tvar[k];
13676: TvarVind[ncovv]=k;
13677: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
13678: Fixed[k]= 1;
13679: Dummy[k]= 1;
13680: modell[k].maintype= VTYPE;
13681: modell[k].subtype= VPDQ; /* Product time varying dummy * time varying quantitative */
13682: ncovv++; /* Varying variables without age */
13683: TvarV[ncovv]=Tvar[k];
13684: TvarVind[ncovv]=k;
13685: }
13686: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv+nqtv){ /* Vn is time varying quanti */
13687: if(Tvard[k1][2] <=ncovcol){
13688: Fixed[k]= 1;
13689: Dummy[k]= 1;
13690: modell[k].maintype= VTYPE;
13691: modell[k].subtype= VPDQ; /* Product time varying quantitative * fixed dummy */
13692: ncovv++; /* Varying variables without age */
13693: TvarV[ncovv]=Tvar[k];
13694: TvarVind[ncovv]=k;
13695: }else if(Tvard[k1][2] <=ncovcol+nqv){
13696: Fixed[k]= 1;
13697: Dummy[k]= 1;
13698: modell[k].maintype= VTYPE;
13699: modell[k].subtype= VPQQ; /* Product time varying quantitative * fixed quantitative */
13700: ncovv++; /* Varying variables without age */
13701: TvarV[ncovv]=Tvar[k];
13702: TvarVind[ncovv]=k;
13703: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
13704: Fixed[k]= 1;
13705: Dummy[k]= 1;
13706: modell[k].maintype= VTYPE;
13707: modell[k].subtype= VPDQ; /* Product time varying quantitative * time varying dummy */
13708: ncovv++; /* Varying variables without age */
13709: TvarV[ncovv]=Tvar[k];
13710: TvarVind[ncovv]=k;
13711: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
13712: Fixed[k]= 1;
13713: Dummy[k]= 1;
13714: modell[k].maintype= VTYPE;
13715: modell[k].subtype= VPQQ; /* Product time varying quantitative * time varying quantitative */
13716: ncovv++; /* Varying variables without age */
13717: TvarV[ncovv]=Tvar[k];
13718: TvarVind[ncovv]=k;
13719: }
13720: }else{
13721: printf("Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
13722: fprintf(ficlog,"Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
13723: } /*end k1*/
13724: }
13725: }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 */
1.339 brouard 13726: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
1.349 brouard 13727: /* model V1+V3+age*V1+age*V3+V1*V3 + V1*V3*age*/
13728: /* Tvar={1, 3, 1, 3, 6, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
13729: 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 */
13730: ncova++;
13731: TvarA[ncova]=Tvard[k1][1]; /* TvarVV[2]=V1 (because TvarVV[1] was V3, first time varying covariates */
13732: TvarAind[ncova]=k; /* TvarVVind[2]=5 (because TvarVVind[2] was V1*V3 at position 5 */
13733: ncova++;
13734: TvarA[ncova]=Tvard[k1][2]; /* TvarVV[3]=V3 */
13735: TvarAind[ncova]=k; /* TvarVVind[2]=5 (because TvarVVind[2] was V1*V3 at position 5 */
1.339 brouard 13736:
1.349 brouard 13737: /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
13738: /** Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product */
13739: if( FixedV[Tvardk[k][1]] == 0 && FixedV[Tvardk[k][2]] == 0){
13740: ncovta++;
13741: TvarAVVA[ncovta]=Tvard[k1][1]; /* age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 */
13742: TvarAVVAind[ncovta]=k;
13743: ncovta++;
13744: TvarAVVA[ncovta]=Tvard[k1][2]; /* age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 */
13745: TvarAVVAind[ncovta]=k;
13746: }else{
13747: ncovva++; /* HERY reached */
13748: TvarVVA[ncovva]=Tvard[k1][1]; /* age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 */
13749: TvarVVAind[ncovva]=k;
13750: ncovva++;
13751: TvarVVA[ncovva]=Tvard[k1][2]; /* */
13752: TvarVVAind[ncovva]=k;
13753: ncovta++;
13754: TvarAVVA[ncovta]=Tvard[k1][1]; /* age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 */
13755: TvarAVVAind[ncovta]=k;
13756: ncovta++;
13757: TvarAVVA[ncovta]=Tvard[k1][2]; /* age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 */
13758: TvarAVVAind[ncovta]=k;
13759: }
1.339 brouard 13760: if(Tvard[k1][1] <=ncovcol){ /* Vn is dummy fixed, (Tvard[1][1]=V1), (Tvard[1][1]=V3 time varying) */
13761: if(Tvard[k1][2] <=ncovcol){ /* Vm is dummy fixed */
1.349 brouard 13762: Fixed[k]= 2;
13763: Dummy[k]= 2;
1.240 brouard 13764: modell[k].maintype= FTYPE;
13765: modell[k].subtype= FPDD; /* Product fixed dummy * fixed dummy */
1.349 brouard 13766: /* TvarF[ncova]=Tvar[k]; /\* Problem to solve *\/ */
13767: /* TvarFind[ncova]=k; */
1.339 brouard 13768: }else if(Tvard[k1][2] <=ncovcol+nqv){ /* Vm is quanti fixed */
1.349 brouard 13769: Fixed[k]= 2; /* Fixed product */
13770: Dummy[k]= 3;
1.240 brouard 13771: modell[k].maintype= FTYPE;
13772: modell[k].subtype= FPDQ; /* Product fixed dummy * fixed quantitative */
1.349 brouard 13773: /* TvarF[ncova]=Tvar[k]; */
13774: /* TvarFind[ncova]=k; */
1.339 brouard 13775: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){ /* Vm is a time varying dummy covariate */
1.349 brouard 13776: Fixed[k]= 3;
13777: Dummy[k]= 2;
1.240 brouard 13778: modell[k].maintype= VTYPE;
13779: modell[k].subtype= VPDD; /* Product fixed dummy * varying dummy */
1.349 brouard 13780: TvarV[ncova]=Tvar[k]; /* TvarV[1]=Tvar[5]=5 because there is a V4 */
13781: TvarVind[ncova]=k;/* TvarVind[1]=5 */
1.339 brouard 13782: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){ /* Vm is a time varying quantitative covariate */
1.349 brouard 13783: Fixed[k]= 3;
13784: Dummy[k]= 3;
1.240 brouard 13785: modell[k].maintype= VTYPE;
13786: modell[k].subtype= VPDQ; /* Product fixed dummy * varying quantitative */
1.349 brouard 13787: /* ncovv++; /\* Varying variables without age *\/ */
13788: /* TvarV[ncovv]=Tvar[k]; */
13789: /* TvarVind[ncovv]=k; */
1.240 brouard 13790: }
1.339 brouard 13791: }else if(Tvard[k1][1] <=ncovcol+nqv){ /* Vn is fixed quanti */
13792: if(Tvard[k1][2] <=ncovcol){ /* Vm is fixed dummy */
1.349 brouard 13793: Fixed[k]= 2; /* Fixed product */
13794: Dummy[k]= 2;
1.240 brouard 13795: modell[k].maintype= FTYPE;
13796: modell[k].subtype= FPDQ; /* Product fixed quantitative * fixed dummy */
1.349 brouard 13797: /* ncova++; /\* Fixed variables with age *\/ */
13798: /* TvarF[ncovf]=Tvar[k]; */
13799: /* TvarFind[ncovf]=k; */
1.339 brouard 13800: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){ /* Vm is time varying */
1.349 brouard 13801: Fixed[k]= 2;
13802: Dummy[k]= 3;
1.240 brouard 13803: modell[k].maintype= VTYPE;
13804: modell[k].subtype= VPDQ; /* Product fixed quantitative * varying dummy */
1.349 brouard 13805: /* ncova++; /\* Varying variables with age *\/ */
13806: /* TvarV[ncova]=Tvar[k]; */
13807: /* TvarVind[ncova]=k; */
1.339 brouard 13808: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){ /* Vm is time varying quanti */
1.349 brouard 13809: Fixed[k]= 3;
13810: Dummy[k]= 2;
1.240 brouard 13811: modell[k].maintype= VTYPE;
13812: modell[k].subtype= VPQQ; /* Product fixed quantitative * varying quantitative */
1.349 brouard 13813: ncova++; /* Varying variables without age */
13814: TvarV[ncova]=Tvar[k];
13815: TvarVind[ncova]=k;
13816: /* ncova++; /\* Varying variables without age *\/ */
13817: /* TvarV[ncova]=Tvar[k]; */
13818: /* TvarVind[ncova]=k; */
1.240 brouard 13819: }
1.339 brouard 13820: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv){ /* Vn is time varying dummy */
1.240 brouard 13821: if(Tvard[k1][2] <=ncovcol){
1.349 brouard 13822: Fixed[k]= 2;
13823: Dummy[k]= 2;
1.240 brouard 13824: modell[k].maintype= VTYPE;
13825: modell[k].subtype= VPDD; /* Product time varying dummy * fixed dummy */
1.349 brouard 13826: /* ncova++; /\* Varying variables with age *\/ */
13827: /* TvarV[ncova]=Tvar[k]; */
13828: /* TvarVind[ncova]=k; */
1.240 brouard 13829: }else if(Tvard[k1][2] <=ncovcol+nqv){
1.349 brouard 13830: Fixed[k]= 2;
13831: Dummy[k]= 3;
1.240 brouard 13832: modell[k].maintype= VTYPE;
13833: modell[k].subtype= VPDQ; /* Product time varying dummy * fixed quantitative */
1.349 brouard 13834: /* ncova++; /\* Varying variables with age *\/ */
13835: /* TvarV[ncova]=Tvar[k]; */
13836: /* TvarVind[ncova]=k; */
1.240 brouard 13837: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
1.349 brouard 13838: Fixed[k]= 3;
13839: Dummy[k]= 2;
1.240 brouard 13840: modell[k].maintype= VTYPE;
13841: modell[k].subtype= VPDD; /* Product time varying dummy * time varying dummy */
1.349 brouard 13842: /* ncova++; /\* Varying variables with age *\/ */
13843: /* TvarV[ncova]=Tvar[k]; */
13844: /* TvarVind[ncova]=k; */
1.240 brouard 13845: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
1.349 brouard 13846: Fixed[k]= 3;
13847: Dummy[k]= 3;
1.240 brouard 13848: modell[k].maintype= VTYPE;
13849: modell[k].subtype= VPDQ; /* Product time varying dummy * time varying quantitative */
1.349 brouard 13850: /* ncova++; /\* Varying variables with age *\/ */
13851: /* TvarV[ncova]=Tvar[k]; */
13852: /* TvarVind[ncova]=k; */
1.240 brouard 13853: }
1.339 brouard 13854: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv+nqtv){ /* Vn is time varying quanti */
1.240 brouard 13855: if(Tvard[k1][2] <=ncovcol){
1.349 brouard 13856: Fixed[k]= 2;
13857: Dummy[k]= 2;
1.240 brouard 13858: modell[k].maintype= VTYPE;
13859: modell[k].subtype= VPDQ; /* Product time varying quantitative * fixed dummy */
1.349 brouard 13860: /* ncova++; /\* Varying variables with age *\/ */
13861: /* TvarV[ncova]=Tvar[k]; */
13862: /* TvarVind[ncova]=k; */
1.240 brouard 13863: }else if(Tvard[k1][2] <=ncovcol+nqv){
1.349 brouard 13864: Fixed[k]= 2;
13865: Dummy[k]= 3;
1.240 brouard 13866: modell[k].maintype= VTYPE;
13867: modell[k].subtype= VPQQ; /* Product time varying quantitative * fixed quantitative */
1.349 brouard 13868: /* ncova++; /\* Varying variables with age *\/ */
13869: /* TvarV[ncova]=Tvar[k]; */
13870: /* TvarVind[ncova]=k; */
1.240 brouard 13871: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
1.349 brouard 13872: Fixed[k]= 3;
13873: Dummy[k]= 2;
1.240 brouard 13874: modell[k].maintype= VTYPE;
13875: modell[k].subtype= VPDQ; /* Product time varying quantitative * time varying dummy */
1.349 brouard 13876: /* ncova++; /\* Varying variables with age *\/ */
13877: /* TvarV[ncova]=Tvar[k]; */
13878: /* TvarVind[ncova]=k; */
1.240 brouard 13879: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
1.349 brouard 13880: Fixed[k]= 3;
13881: Dummy[k]= 3;
1.240 brouard 13882: modell[k].maintype= VTYPE;
13883: modell[k].subtype= VPQQ; /* Product time varying quantitative * time varying quantitative */
1.349 brouard 13884: /* ncova++; /\* Varying variables with age *\/ */
13885: /* TvarV[ncova]=Tvar[k]; */
13886: /* TvarVind[ncova]=k; */
1.240 brouard 13887: }
1.227 brouard 13888: }else{
1.240 brouard 13889: printf("Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
13890: fprintf(ficlog,"Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
13891: } /*end k1*/
1.349 brouard 13892: } else{
1.226 brouard 13893: printf("Error, current version can't treat for performance reasons, Tvar[%d]=%d, Typevar[%d]=%d\n", k, Tvar[k], k, Typevar[k]);
13894: fprintf(ficlog,"Error, current version can't treat for performance reasons, Tvar[%d]=%d, Typevar[%d]=%d\n", k, Tvar[k], k, Typevar[k]);
1.225 brouard 13895: }
1.342 brouard 13896: /* 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]); */
13897: /* printf(" modell[%d].maintype=%d, modell[%d].subtype=%d\n",k,modell[k].maintype,k,modell[k].subtype); */
1.227 brouard 13898: 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]);
13899: }
1.349 brouard 13900: ncovvta=ncovva;
1.227 brouard 13901: /* Searching for doublons in the model */
13902: for(k1=1; k1<= cptcovt;k1++){
13903: for(k2=1; k2 <k1;k2++){
1.285 brouard 13904: /* if((Typevar[k1]==Typevar[k2]) && (Fixed[Tvar[k1]]==Fixed[Tvar[k2]]) && (Dummy[Tvar[k1]]==Dummy[Tvar[k2]] )){ */
13905: if((Typevar[k1]==Typevar[k2]) && (Fixed[k1]==Fixed[k2]) && (Dummy[k1]==Dummy[k2] )){
1.234 brouard 13906: if((Typevar[k1] == 0 || Typevar[k1] == 1)){ /* Simple or age product */
13907: if(Tvar[k1]==Tvar[k2]){
1.338 brouard 13908: 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]);
13909: 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);
1.234 brouard 13910: return(1);
13911: }
13912: }else if (Typevar[k1] ==2){
13913: k3=Tposprod[k1];
13914: k4=Tposprod[k2];
13915: 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])) ){
1.338 brouard 13916: 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]]);
13917: 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);
1.234 brouard 13918: return(1);
13919: }
13920: }
1.227 brouard 13921: }
13922: }
1.225 brouard 13923: }
13924: printf("ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
13925: fprintf(ficlog,"ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
1.234 brouard 13926: printf("ncovf=%d, ncovv=%d, ncova=%d, nsd=%d, nsq=%d\n",ncovf,ncovv,ncova,nsd,nsq);
13927: fprintf(ficlog,"ncovf=%d, ncovv=%d, ncova=%d, nsd=%d, nsq=%d\n",ncovf,ncovv,ncova,nsd, nsq);
1.349 brouard 13928:
13929: free_imatrix(existcomb,1,NCOVMAX,1,NCOVMAX);
1.137 brouard 13930: return (0); /* with covar[new additional covariate if product] and Tage if age */
1.164 brouard 13931: /*endread:*/
1.225 brouard 13932: printf("Exiting decodemodel: ");
13933: return (1);
1.136 brouard 13934: }
13935:
1.169 brouard 13936: int calandcheckages(int imx, int maxwav, double *agemin, double *agemax, int *nberr, int *nbwarn )
1.248 brouard 13937: {/* Check ages at death */
1.136 brouard 13938: int i, m;
1.218 brouard 13939: int firstone=0;
13940:
1.136 brouard 13941: for (i=1; i<=imx; i++) {
13942: for(m=2; (m<= maxwav); m++) {
13943: if (((int)mint[m][i]== 99) && (s[m][i] <= nlstate)){
13944: anint[m][i]=9999;
1.216 brouard 13945: if (s[m][i] != -2) /* Keeping initial status of unknown vital status */
13946: s[m][i]=-1;
1.136 brouard 13947: }
13948: if((int)moisdc[i]==99 && (int)andc[i]==9999 && s[m][i]>nlstate){
1.260 brouard 13949: *nberr = *nberr + 1;
1.218 brouard 13950: if(firstone == 0){
13951: firstone=1;
1.260 brouard 13952: 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);
1.218 brouard 13953: }
1.262 brouard 13954: 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);
1.260 brouard 13955: s[m][i]=-1; /* Droping the death status */
1.136 brouard 13956: }
13957: if((int)moisdc[i]==99 && (int)andc[i]!=9999 && s[m][i]>nlstate){
1.169 brouard 13958: (*nberr)++;
1.259 brouard 13959: 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);
1.262 brouard 13960: 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);
1.259 brouard 13961: s[m][i]=-2; /* We prefer to skip it (and to skip it in version 0.8a1 too */
1.136 brouard 13962: }
13963: }
13964: }
13965:
13966: for (i=1; i<=imx; i++) {
13967: agedc[i]=(moisdc[i]/12.+andc[i])-(moisnais[i]/12.+annais[i]);
13968: for(m=firstpass; (m<= lastpass); m++){
1.214 brouard 13969: if(s[m][i] >0 || s[m][i]==-1 || s[m][i]==-2 || s[m][i]==-4 || s[m][i]==-5){ /* What if s[m][i]=-1 */
1.136 brouard 13970: if (s[m][i] >= nlstate+1) {
1.169 brouard 13971: if(agedc[i]>0){
13972: if((int)moisdc[i]!=99 && (int)andc[i]!=9999){
1.136 brouard 13973: agev[m][i]=agedc[i];
1.214 brouard 13974: /*if(moisdc[i]==99 && andc[i]==9999) s[m][i]=-1;*/
1.169 brouard 13975: }else {
1.136 brouard 13976: if ((int)andc[i]!=9999){
13977: nbwarn++;
13978: printf("Warning negative age at death: %ld line:%d\n",num[i],i);
13979: fprintf(ficlog,"Warning negative age at death: %ld line:%d\n",num[i],i);
13980: agev[m][i]=-1;
13981: }
13982: }
1.169 brouard 13983: } /* agedc > 0 */
1.214 brouard 13984: } /* end if */
1.136 brouard 13985: else if(s[m][i] !=9){ /* Standard case, age in fractional
13986: years but with the precision of a month */
13987: agev[m][i]=(mint[m][i]/12.+1./24.+anint[m][i])-(moisnais[i]/12.+1./24.+annais[i]);
13988: if((int)mint[m][i]==99 || (int)anint[m][i]==9999)
13989: agev[m][i]=1;
13990: else if(agev[m][i] < *agemin){
13991: *agemin=agev[m][i];
13992: printf(" Min anint[%d][%d]=%.2f annais[%d]=%.2f, agemin=%.2f\n",m,i,anint[m][i], i,annais[i], *agemin);
13993: }
13994: else if(agev[m][i] >*agemax){
13995: *agemax=agev[m][i];
1.156 brouard 13996: /* printf(" Max anint[%d][%d]=%.0f annais[%d]=%.0f, agemax=%.2f\n",m,i,anint[m][i], i,annais[i], *agemax);*/
1.136 brouard 13997: }
13998: /*agev[m][i]=anint[m][i]-annais[i];*/
13999: /* agev[m][i] = age[i]+2*m;*/
1.214 brouard 14000: } /* en if 9*/
1.136 brouard 14001: else { /* =9 */
1.214 brouard 14002: /* printf("Debug num[%d]=%ld s[%d][%d]=%d\n",i,num[i], m,i, s[m][i]); */
1.136 brouard 14003: agev[m][i]=1;
14004: s[m][i]=-1;
14005: }
14006: }
1.214 brouard 14007: else if(s[m][i]==0) /*= 0 Unknown */
1.136 brouard 14008: agev[m][i]=1;
1.214 brouard 14009: else{
14010: printf("Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
14011: fprintf(ficlog, "Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
14012: agev[m][i]=0;
14013: }
14014: } /* End for lastpass */
14015: }
1.136 brouard 14016:
14017: for (i=1; i<=imx; i++) {
14018: for(m=firstpass; (m<=lastpass); m++){
14019: if (s[m][i] > (nlstate+ndeath)) {
1.169 brouard 14020: (*nberr)++;
1.136 brouard 14021: 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);
14022: 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);
14023: return 1;
14024: }
14025: }
14026: }
14027:
14028: /*for (i=1; i<=imx; i++){
14029: for (m=firstpass; (m<lastpass); m++){
14030: printf("%ld %d %.lf %d %d\n", num[i],(covar[1][i]),agev[m][i],s[m][i],s[m+1][i]);
14031: }
14032:
14033: }*/
14034:
14035:
1.139 brouard 14036: printf("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
14037: fprintf(ficlog,"Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
1.136 brouard 14038:
14039: return (0);
1.164 brouard 14040: /* endread:*/
1.136 brouard 14041: printf("Exiting calandcheckages: ");
14042: return (1);
14043: }
14044:
1.172 brouard 14045: #if defined(_MSC_VER)
14046: /*printf("Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
14047: /*fprintf(ficlog, "Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
14048: //#include "stdafx.h"
14049: //#include <stdio.h>
14050: //#include <tchar.h>
14051: //#include <windows.h>
14052: //#include <iostream>
14053: typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
14054:
14055: LPFN_ISWOW64PROCESS fnIsWow64Process;
14056:
14057: BOOL IsWow64()
14058: {
14059: BOOL bIsWow64 = FALSE;
14060:
14061: //typedef BOOL (APIENTRY *LPFN_ISWOW64PROCESS)
14062: // (HANDLE, PBOOL);
14063:
14064: //LPFN_ISWOW64PROCESS fnIsWow64Process;
14065:
14066: HMODULE module = GetModuleHandle(_T("kernel32"));
14067: const char funcName[] = "IsWow64Process";
14068: fnIsWow64Process = (LPFN_ISWOW64PROCESS)
14069: GetProcAddress(module, funcName);
14070:
14071: if (NULL != fnIsWow64Process)
14072: {
14073: if (!fnIsWow64Process(GetCurrentProcess(),
14074: &bIsWow64))
14075: //throw std::exception("Unknown error");
14076: printf("Unknown error\n");
14077: }
14078: return bIsWow64 != FALSE;
14079: }
14080: #endif
1.177 brouard 14081:
1.191 brouard 14082: void syscompilerinfo(int logged)
1.292 brouard 14083: {
14084: #include <stdint.h>
14085:
14086: /* #include "syscompilerinfo.h"*/
1.185 brouard 14087: /* command line Intel compiler 32bit windows, XP compatible:*/
14088: /* /GS /W3 /Gy
14089: /Zc:wchar_t /Zi /O2 /Fd"Release\vc120.pdb" /D "WIN32" /D "NDEBUG" /D
14090: "_CONSOLE" /D "_LIB" /D "_USING_V110_SDK71_" /D "_UNICODE" /D
14091: "UNICODE" /Qipo /Zc:forScope /Gd /Oi /MT /Fa"Release\" /EHsc /nologo
1.186 brouard 14092: /Fo"Release\" /Qprof-dir "Release\" /Fp"Release\IMaCh.pch"
14093: */
14094: /* 64 bits */
1.185 brouard 14095: /*
14096: /GS /W3 /Gy
14097: /Zc:wchar_t /Zi /O2 /Fd"x64\Release\vc120.pdb" /D "WIN32" /D "NDEBUG"
14098: /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo /Zc:forScope
14099: /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Fo"x64\Release\" /Qprof-dir
14100: "x64\Release\" /Fp"x64\Release\IMaCh.pch" */
14101: /* Optimization are useless and O3 is slower than O2 */
14102: /*
14103: /GS /W3 /Gy /Zc:wchar_t /Zi /O3 /Fd"x64\Release\vc120.pdb" /D "WIN32"
14104: /D "NDEBUG" /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo
14105: /Zc:forScope /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Qparallel
14106: /Fo"x64\Release\" /Qprof-dir "x64\Release\" /Fp"x64\Release\IMaCh.pch"
14107: */
1.186 brouard 14108: /* Link is */ /* /OUT:"visual studio
1.185 brouard 14109: 2013\Projects\IMaCh\Release\IMaCh.exe" /MANIFEST /NXCOMPAT
14110: /PDB:"visual studio
14111: 2013\Projects\IMaCh\Release\IMaCh.pdb" /DYNAMICBASE
14112: "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib"
14113: "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib"
14114: "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib"
14115: /MACHINE:X86 /OPT:REF /SAFESEH /INCREMENTAL:NO
14116: /SUBSYSTEM:CONSOLE",5.01" /MANIFESTUAC:"level='asInvoker'
14117: uiAccess='false'"
14118: /ManifestFile:"Release\IMaCh.exe.intermediate.manifest" /OPT:ICF
14119: /NOLOGO /TLBID:1
14120: */
1.292 brouard 14121:
14122:
1.177 brouard 14123: #if defined __INTEL_COMPILER
1.178 brouard 14124: #if defined(__GNUC__)
14125: struct utsname sysInfo; /* For Intel on Linux and OS/X */
14126: #endif
1.177 brouard 14127: #elif defined(__GNUC__)
1.179 brouard 14128: #ifndef __APPLE__
1.174 brouard 14129: #include <gnu/libc-version.h> /* Only on gnu */
1.179 brouard 14130: #endif
1.177 brouard 14131: struct utsname sysInfo;
1.178 brouard 14132: int cross = CROSS;
14133: if (cross){
14134: printf("Cross-");
1.191 brouard 14135: if(logged) fprintf(ficlog, "Cross-");
1.178 brouard 14136: }
1.174 brouard 14137: #endif
14138:
1.191 brouard 14139: printf("Compiled with:");if(logged)fprintf(ficlog,"Compiled with:");
1.169 brouard 14140: #if defined(__clang__)
1.191 brouard 14141: printf(" Clang/LLVM");if(logged)fprintf(ficlog," Clang/LLVM"); /* Clang/LLVM. ---------------------------------------------- */
1.169 brouard 14142: #endif
14143: #if defined(__ICC) || defined(__INTEL_COMPILER)
1.191 brouard 14144: printf(" Intel ICC/ICPC");if(logged)fprintf(ficlog," Intel ICC/ICPC");/* Intel ICC/ICPC. ------------------------------------------ */
1.169 brouard 14145: #endif
14146: #if defined(__GNUC__) || defined(__GNUG__)
1.191 brouard 14147: printf(" GNU GCC/G++");if(logged)fprintf(ficlog," GNU GCC/G++");/* GNU GCC/G++. --------------------------------------------- */
1.169 brouard 14148: #endif
14149: #if defined(__HP_cc) || defined(__HP_aCC)
1.191 brouard 14150: printf(" Hewlett-Packard C/aC++");if(logged)fprintf(fcilog," Hewlett-Packard C/aC++"); /* Hewlett-Packard C/aC++. ---------------------------------- */
1.169 brouard 14151: #endif
14152: #if defined(__IBMC__) || defined(__IBMCPP__)
1.191 brouard 14153: printf(" IBM XL C/C++"); if(logged) fprintf(ficlog," IBM XL C/C++");/* IBM XL C/C++. -------------------------------------------- */
1.169 brouard 14154: #endif
14155: #if defined(_MSC_VER)
1.191 brouard 14156: printf(" Microsoft Visual Studio");if(logged)fprintf(ficlog," Microsoft Visual Studio");/* Microsoft Visual Studio. --------------------------------- */
1.169 brouard 14157: #endif
14158: #if defined(__PGI)
1.191 brouard 14159: printf(" Portland Group PGCC/PGCPP");if(logged) fprintf(ficlog," Portland Group PGCC/PGCPP");/* Portland Group PGCC/PGCPP. ------------------------------- */
1.169 brouard 14160: #endif
14161: #if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
1.191 brouard 14162: printf(" Oracle Solaris Studio");if(logged)fprintf(ficlog," Oracle Solaris Studio\n");/* Oracle Solaris Studio. ----------------------------------- */
1.167 brouard 14163: #endif
1.191 brouard 14164: printf(" for "); if (logged) fprintf(ficlog, " for ");
1.169 brouard 14165:
1.167 brouard 14166: // http://stackoverflow.com/questions/4605842/how-to-identify-platform-compiler-from-preprocessor-macros
14167: #ifdef _WIN32 // note the underscore: without it, it's not msdn official!
14168: // Windows (x64 and x86)
1.191 brouard 14169: printf("Windows (x64 and x86) ");if(logged) fprintf(ficlog,"Windows (x64 and x86) ");
1.167 brouard 14170: #elif __unix__ // all unices, not all compilers
14171: // Unix
1.191 brouard 14172: printf("Unix ");if(logged) fprintf(ficlog,"Unix ");
1.167 brouard 14173: #elif __linux__
14174: // linux
1.191 brouard 14175: printf("linux ");if(logged) fprintf(ficlog,"linux ");
1.167 brouard 14176: #elif __APPLE__
1.174 brouard 14177: // Mac OS, not sure if this is covered by __posix__ and/or __unix__ though..
1.191 brouard 14178: printf("Mac OS ");if(logged) fprintf(ficlog,"Mac OS ");
1.167 brouard 14179: #endif
14180:
14181: /* __MINGW32__ */
14182: /* __CYGWIN__ */
14183: /* __MINGW64__ */
14184: // http://msdn.microsoft.com/en-us/library/b0084kay.aspx
14185: /* _MSC_VER //the Visual C++ compiler is 17.00.51106.1, the _MSC_VER macro evaluates to 1700. Type cl /? */
14186: /* _MSC_FULL_VER //the Visual C++ compiler is 15.00.20706.01, the _MSC_FULL_VER macro evaluates to 150020706 */
14187: /* _WIN64 // Defined for applications for Win64. */
14188: /* _M_X64 // Defined for compilations that target x64 processors. */
14189: /* _DEBUG // Defined when you compile with /LDd, /MDd, and /MTd. */
1.171 brouard 14190:
1.167 brouard 14191: #if UINTPTR_MAX == 0xffffffff
1.191 brouard 14192: printf(" 32-bit"); if(logged) fprintf(ficlog," 32-bit");/* 32-bit */
1.167 brouard 14193: #elif UINTPTR_MAX == 0xffffffffffffffff
1.191 brouard 14194: printf(" 64-bit"); if(logged) fprintf(ficlog," 64-bit");/* 64-bit */
1.167 brouard 14195: #else
1.191 brouard 14196: printf(" wtf-bit"); if(logged) fprintf(ficlog," wtf-bit");/* wtf */
1.167 brouard 14197: #endif
14198:
1.169 brouard 14199: #if defined(__GNUC__)
14200: # if defined(__GNUC_PATCHLEVEL__)
14201: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
14202: + __GNUC_MINOR__ * 100 \
14203: + __GNUC_PATCHLEVEL__)
14204: # else
14205: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
14206: + __GNUC_MINOR__ * 100)
14207: # endif
1.174 brouard 14208: printf(" using GNU C version %d.\n", __GNUC_VERSION__);
1.191 brouard 14209: if(logged) fprintf(ficlog, " using GNU C version %d.\n", __GNUC_VERSION__);
1.176 brouard 14210:
14211: if (uname(&sysInfo) != -1) {
14212: printf("Running on: %s %s %s %s %s\n",sysInfo.sysname, sysInfo.nodename, sysInfo.release, sysInfo.version, sysInfo.machine);
1.191 brouard 14213: if(logged) fprintf(ficlog,"Running on: %s %s %s %s %s\n ",sysInfo.sysname, sysInfo.nodename, sysInfo.release, sysInfo.version, sysInfo.machine);
1.176 brouard 14214: }
14215: else
14216: perror("uname() error");
1.179 brouard 14217: //#ifndef __INTEL_COMPILER
14218: #if !defined (__INTEL_COMPILER) && !defined(__APPLE__)
1.174 brouard 14219: printf("GNU libc version: %s\n", gnu_get_libc_version());
1.191 brouard 14220: if(logged) fprintf(ficlog,"GNU libc version: %s\n", gnu_get_libc_version());
1.177 brouard 14221: #endif
1.169 brouard 14222: #endif
1.172 brouard 14223:
1.286 brouard 14224: // void main ()
1.172 brouard 14225: // {
1.169 brouard 14226: #if defined(_MSC_VER)
1.174 brouard 14227: if (IsWow64()){
1.191 brouard 14228: printf("\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
14229: if (logged) fprintf(ficlog, "\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
1.174 brouard 14230: }
14231: else{
1.191 brouard 14232: printf("\nThe program is not running under WOW64 (i.e probably on a 64bit Windows).\n");
14233: if (logged) fprintf(ficlog, "\nThe programm is not running under WOW64 (i.e probably on a 64bit Windows).\n");
1.174 brouard 14234: }
1.172 brouard 14235: // printf("\nPress Enter to continue...");
14236: // getchar();
14237: // }
14238:
1.169 brouard 14239: #endif
14240:
1.167 brouard 14241:
1.219 brouard 14242: }
1.136 brouard 14243:
1.219 brouard 14244: int prevalence_limit(double *p, double **prlim, double ageminpar, double agemaxpar, double ftolpl, int *ncvyearp){
1.288 brouard 14245: /*--------------- Prevalence limit (forward period or forward stable prevalence) --------------*/
1.332 brouard 14246: /* Computes the prevalence limit for each combination of the dummy covariates */
1.235 brouard 14247: int i, j, k, i1, k4=0, nres=0 ;
1.202 brouard 14248: /* double ftolpl = 1.e-10; */
1.180 brouard 14249: double age, agebase, agelim;
1.203 brouard 14250: double tot;
1.180 brouard 14251:
1.202 brouard 14252: strcpy(filerespl,"PL_");
14253: strcat(filerespl,fileresu);
14254: if((ficrespl=fopen(filerespl,"w"))==NULL) {
1.288 brouard 14255: printf("Problem with forward period (stable) prevalence resultfile: %s\n", filerespl);return 1;
14256: fprintf(ficlog,"Problem with forward period (stable) prevalence resultfile: %s\n", filerespl);return 1;
1.202 brouard 14257: }
1.288 brouard 14258: printf("\nComputing forward period (stable) prevalence: result on file '%s' \n", filerespl);
14259: fprintf(ficlog,"\nComputing forward period (stable) prevalence: result on file '%s' \n", filerespl);
1.202 brouard 14260: pstamp(ficrespl);
1.288 brouard 14261: fprintf(ficrespl,"# Forward period (stable) prevalence. Precision given by ftolpl=%g \n", ftolpl);
1.202 brouard 14262: fprintf(ficrespl,"#Age ");
14263: for(i=1; i<=nlstate;i++) fprintf(ficrespl,"%d-%d ",i,i);
14264: fprintf(ficrespl,"\n");
1.180 brouard 14265:
1.219 brouard 14266: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
1.180 brouard 14267:
1.219 brouard 14268: agebase=ageminpar;
14269: agelim=agemaxpar;
1.180 brouard 14270:
1.227 brouard 14271: /* i1=pow(2,ncoveff); */
1.234 brouard 14272: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
1.219 brouard 14273: if (cptcovn < 1){i1=1;}
1.180 brouard 14274:
1.337 brouard 14275: /* for(k=1; k<=i1;k++){ /\* For each combination k of dummy covariates in the model *\/ */
1.238 brouard 14276: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 14277: k=TKresult[nres];
1.338 brouard 14278: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
1.337 brouard 14279: /* if(i1 != 1 && TKresult[nres]!= k) /\* We found the combination k corresponding to the resultline value of dummies *\/ */
14280: /* continue; */
1.235 brouard 14281:
1.238 brouard 14282: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
14283: /* for(cptcov=1,k=0;cptcov<=1;cptcov++){ */
14284: //for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
14285: /* k=k+1; */
14286: /* to clean */
1.332 brouard 14287: /*printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));*/
1.238 brouard 14288: fprintf(ficrespl,"#******");
14289: printf("#******");
14290: fprintf(ficlog,"#******");
1.337 brouard 14291: 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) */
1.332 brouard 14292: /* fprintf(ficrespl," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,Tvaraff[j])]); /\* Here problem for varying dummy*\/ */
1.337 brouard 14293: /* printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14294: /* fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14295: fprintf(ficrespl," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
14296: printf(" V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
14297: fprintf(ficlog," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
14298: }
14299: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
14300: /* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
14301: /* fprintf(ficrespl," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
14302: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
14303: /* } */
1.238 brouard 14304: fprintf(ficrespl,"******\n");
14305: printf("******\n");
14306: fprintf(ficlog,"******\n");
14307: if(invalidvarcomb[k]){
14308: printf("\nCombination (%d) ignored because no case \n",k);
14309: fprintf(ficrespl,"#Combination (%d) ignored because no case \n",k);
14310: fprintf(ficlog,"\nCombination (%d) ignored because no case \n",k);
14311: continue;
14312: }
1.219 brouard 14313:
1.238 brouard 14314: fprintf(ficrespl,"#Age ");
1.337 brouard 14315: /* for(j=1;j<=cptcoveff;j++) { */
14316: /* fprintf(ficrespl,"V%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14317: /* } */
14318: for(j=1;j<=cptcovs;j++) { /* New the quanti variable is added */
14319: fprintf(ficrespl,"V%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
1.238 brouard 14320: }
14321: for(i=1; i<=nlstate;i++) fprintf(ficrespl," %d-%d ",i,i);
14322: fprintf(ficrespl,"Total Years_to_converge\n");
1.227 brouard 14323:
1.238 brouard 14324: for (age=agebase; age<=agelim; age++){
14325: /* for (age=agebase; age<=agebase; age++){ */
1.337 brouard 14326: /**< Computes the prevalence limit in each live state at age x and for covariate combination (k and) nres */
14327: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, ncvyearp, k, nres); /* Nicely done */
1.238 brouard 14328: fprintf(ficrespl,"%.0f ",age );
1.337 brouard 14329: /* for(j=1;j<=cptcoveff;j++) */
14330: /* fprintf(ficrespl,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14331: for(j=1;j<=cptcovs;j++)
14332: fprintf(ficrespl,"%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
1.238 brouard 14333: tot=0.;
14334: for(i=1; i<=nlstate;i++){
14335: tot += prlim[i][i];
14336: fprintf(ficrespl," %.5f", prlim[i][i]);
14337: }
14338: fprintf(ficrespl," %.3f %d\n", tot, *ncvyearp);
14339: } /* Age */
14340: /* was end of cptcod */
1.337 brouard 14341: } /* nres */
14342: /* } /\* for each combination *\/ */
1.219 brouard 14343: return 0;
1.180 brouard 14344: }
14345:
1.218 brouard 14346: 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){
1.288 brouard 14347: /*--------------- Back Prevalence limit (backward stable prevalence) --------------*/
1.218 brouard 14348:
14349: /* Computes the back prevalence limit for any combination of covariate values
14350: * at any age between ageminpar and agemaxpar
14351: */
1.235 brouard 14352: int i, j, k, i1, nres=0 ;
1.217 brouard 14353: /* double ftolpl = 1.e-10; */
14354: double age, agebase, agelim;
14355: double tot;
1.218 brouard 14356: /* double ***mobaverage; */
14357: /* double **dnewm, **doldm, **dsavm; /\* for use *\/ */
1.217 brouard 14358:
14359: strcpy(fileresplb,"PLB_");
14360: strcat(fileresplb,fileresu);
14361: if((ficresplb=fopen(fileresplb,"w"))==NULL) {
1.288 brouard 14362: printf("Problem with backward prevalence resultfile: %s\n", fileresplb);return 1;
14363: fprintf(ficlog,"Problem with backward prevalence resultfile: %s\n", fileresplb);return 1;
1.217 brouard 14364: }
1.288 brouard 14365: printf("Computing backward prevalence: result on file '%s' \n", fileresplb);
14366: fprintf(ficlog,"Computing backward prevalence: result on file '%s' \n", fileresplb);
1.217 brouard 14367: pstamp(ficresplb);
1.288 brouard 14368: fprintf(ficresplb,"# Backward prevalence. Precision given by ftolpl=%g \n", ftolpl);
1.217 brouard 14369: fprintf(ficresplb,"#Age ");
14370: for(i=1; i<=nlstate;i++) fprintf(ficresplb,"%d-%d ",i,i);
14371: fprintf(ficresplb,"\n");
14372:
1.218 brouard 14373:
14374: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
14375:
14376: agebase=ageminpar;
14377: agelim=agemaxpar;
14378:
14379:
1.227 brouard 14380: i1=pow(2,cptcoveff);
1.218 brouard 14381: if (cptcovn < 1){i1=1;}
1.227 brouard 14382:
1.238 brouard 14383: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.338 brouard 14384: /* for(k=1; k<=i1;k++){ /\* For any combination of dummy covariates, fixed and varying *\/ */
14385: k=TKresult[nres];
14386: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
14387: /* if(i1 != 1 && TKresult[nres]!= k) */
14388: /* continue; */
14389: /* /\*printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));*\/ */
1.238 brouard 14390: fprintf(ficresplb,"#******");
14391: printf("#******");
14392: fprintf(ficlog,"#******");
1.338 brouard 14393: 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) */
14394: printf(" V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
14395: fprintf(ficresplb," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
14396: fprintf(ficlog," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
1.238 brouard 14397: }
1.338 brouard 14398: /* for(j=1;j<=cptcoveff ;j++) {/\* all covariates *\/ */
14399: /* fprintf(ficresplb," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14400: /* printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14401: /* fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14402: /* } */
14403: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
14404: /* printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
14405: /* fprintf(ficresplb," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
14406: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
14407: /* } */
1.238 brouard 14408: fprintf(ficresplb,"******\n");
14409: printf("******\n");
14410: fprintf(ficlog,"******\n");
14411: if(invalidvarcomb[k]){
14412: printf("\nCombination (%d) ignored because no cases \n",k);
14413: fprintf(ficresplb,"#Combination (%d) ignored because no cases \n",k);
14414: fprintf(ficlog,"\nCombination (%d) ignored because no cases \n",k);
14415: continue;
14416: }
1.218 brouard 14417:
1.238 brouard 14418: fprintf(ficresplb,"#Age ");
1.338 brouard 14419: for(j=1;j<=cptcovs;j++) {
14420: fprintf(ficresplb,"V%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
1.238 brouard 14421: }
14422: for(i=1; i<=nlstate;i++) fprintf(ficresplb," %d-%d ",i,i);
14423: fprintf(ficresplb,"Total Years_to_converge\n");
1.218 brouard 14424:
14425:
1.238 brouard 14426: for (age=agebase; age<=agelim; age++){
14427: /* for (age=agebase; age<=agebase; age++){ */
14428: if(mobilavproj > 0){
14429: /* bprevalim(bprlim, mobaverage, nlstate, p, age, ageminpar, agemaxpar, oldm, savm, doldm, dsavm, ftolpl, ncvyearp, k); */
14430: /* bprevalim(bprlim, mobaverage, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
1.242 brouard 14431: bprevalim(bprlim, mobaverage, nlstate, p, age, ftolpl, ncvyearp, k, nres);
1.238 brouard 14432: }else if (mobilavproj == 0){
14433: 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);
14434: 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);
14435: exit(1);
14436: }else{
14437: /* bprevalim(bprlim, probs, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
1.242 brouard 14438: bprevalim(bprlim, probs, nlstate, p, age, ftolpl, ncvyearp, k,nres);
1.266 brouard 14439: /* printf("TOTOT\n"); */
14440: /* exit(1); */
1.238 brouard 14441: }
14442: fprintf(ficresplb,"%.0f ",age );
1.338 brouard 14443: for(j=1;j<=cptcovs;j++)
14444: fprintf(ficresplb,"%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
1.238 brouard 14445: tot=0.;
14446: for(i=1; i<=nlstate;i++){
14447: tot += bprlim[i][i];
14448: fprintf(ficresplb," %.5f", bprlim[i][i]);
14449: }
14450: fprintf(ficresplb," %.3f %d\n", tot, *ncvyearp);
14451: } /* Age */
14452: /* was end of cptcod */
1.255 brouard 14453: /*fprintf(ficresplb,"\n");*/ /* Seems to be necessary for gnuplot only if two result lines and no covariate. */
1.338 brouard 14454: /* } /\* end of any combination *\/ */
1.238 brouard 14455: } /* end of nres */
1.218 brouard 14456: /* hBijx(p, bage, fage); */
14457: /* fclose(ficrespijb); */
14458:
14459: return 0;
1.217 brouard 14460: }
1.218 brouard 14461:
1.180 brouard 14462: int hPijx(double *p, int bage, int fage){
14463: /*------------- h Pij x at various ages ------------*/
1.336 brouard 14464: /* to be optimized with precov */
1.180 brouard 14465: int stepsize;
14466: int agelim;
14467: int hstepm;
14468: int nhstepm;
1.359 brouard 14469: int h, i, i1, j, k, nres=0;
1.180 brouard 14470:
14471: double agedeb;
14472: double ***p3mat;
14473:
1.337 brouard 14474: strcpy(filerespij,"PIJ_"); strcat(filerespij,fileresu);
14475: if((ficrespij=fopen(filerespij,"w"))==NULL) {
14476: printf("Problem with Pij resultfile: %s\n", filerespij); return 1;
14477: fprintf(ficlog,"Problem with Pij resultfile: %s\n", filerespij); return 1;
14478: }
14479: printf("Computing pij: result on file '%s' \n", filerespij);
14480: fprintf(ficlog,"Computing pij: result on file '%s' \n", filerespij);
14481:
14482: stepsize=(int) (stepm+YEARM-1)/YEARM;
14483: /*if (stepm<=24) stepsize=2;*/
14484:
14485: agelim=AGESUP;
14486: hstepm=stepsize*YEARM; /* Every year of age */
14487: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
14488:
14489: /* hstepm=1; aff par mois*/
14490: pstamp(ficrespij);
14491: fprintf(ficrespij,"#****** h Pij x Probability to be in state j at age x+h being in i at x ");
14492: i1= pow(2,cptcoveff);
14493: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
14494: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
14495: /* k=k+1; */
14496: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
14497: k=TKresult[nres];
1.338 brouard 14498: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
1.337 brouard 14499: /* for(k=1; k<=i1;k++){ */
14500: /* if(i1 != 1 && TKresult[nres]!= k) */
14501: /* continue; */
14502: fprintf(ficrespij,"\n#****** ");
14503: for(j=1;j<=cptcovs;j++){
14504: fprintf(ficrespij," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
14505: /* fprintf(ficrespij,"@wV%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14506: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
14507: /* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
14508: /* fprintf(ficrespij," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
14509: }
14510: fprintf(ficrespij,"******\n");
14511:
14512: for (agedeb=fage; agedeb>=bage; agedeb--){ /* If stepm=6 months */
14513: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
14514: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
14515:
14516: /* nhstepm=nhstepm*YEARM; aff par mois*/
14517:
14518: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
14519: oldm=oldms;savm=savms;
14520: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k, nres);
14521: fprintf(ficrespij,"# Cov Agex agex+h hpijx with i,j=");
14522: for(i=1; i<=nlstate;i++)
14523: for(j=1; j<=nlstate+ndeath;j++)
14524: fprintf(ficrespij," %1d-%1d",i,j);
14525: fprintf(ficrespij,"\n");
14526: for (h=0; h<=nhstepm; h++){
14527: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
14528: fprintf(ficrespij,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm );
1.183 brouard 14529: for(i=1; i<=nlstate;i++)
14530: for(j=1; j<=nlstate+ndeath;j++)
1.337 brouard 14531: fprintf(ficrespij," %.5f", p3mat[i][j][h]);
1.183 brouard 14532: fprintf(ficrespij,"\n");
14533: }
1.337 brouard 14534: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
14535: fprintf(ficrespij,"\n");
1.180 brouard 14536: }
1.337 brouard 14537: }
14538: /*}*/
14539: return 0;
1.180 brouard 14540: }
1.218 brouard 14541:
14542: int hBijx(double *p, int bage, int fage, double ***prevacurrent){
1.217 brouard 14543: /*------------- h Bij x at various ages ------------*/
1.336 brouard 14544: /* To be optimized with precov */
1.217 brouard 14545: int stepsize;
1.218 brouard 14546: /* int agelim; */
14547: int ageminl;
1.217 brouard 14548: int hstepm;
14549: int nhstepm;
1.238 brouard 14550: int h, i, i1, j, k, nres;
1.218 brouard 14551:
1.217 brouard 14552: double agedeb;
14553: double ***p3mat;
1.218 brouard 14554:
14555: strcpy(filerespijb,"PIJB_"); strcat(filerespijb,fileresu);
14556: if((ficrespijb=fopen(filerespijb,"w"))==NULL) {
14557: printf("Problem with Pij back resultfile: %s\n", filerespijb); return 1;
14558: fprintf(ficlog,"Problem with Pij back resultfile: %s\n", filerespijb); return 1;
14559: }
14560: printf("Computing pij back: result on file '%s' \n", filerespijb);
14561: fprintf(ficlog,"Computing pij back: result on file '%s' \n", filerespijb);
14562:
14563: stepsize=(int) (stepm+YEARM-1)/YEARM;
14564: /*if (stepm<=24) stepsize=2;*/
1.217 brouard 14565:
1.218 brouard 14566: /* agelim=AGESUP; */
1.289 brouard 14567: ageminl=AGEINF; /* was 30 */
1.218 brouard 14568: hstepm=stepsize*YEARM; /* Every year of age */
14569: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
14570:
14571: /* hstepm=1; aff par mois*/
14572: pstamp(ficrespijb);
1.255 brouard 14573: fprintf(ficrespijb,"#****** h Bij x Back probability to be in state i at age x-h being in j at x: B1j+B2j+...=1 ");
1.227 brouard 14574: i1= pow(2,cptcoveff);
1.218 brouard 14575: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
14576: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
14577: /* k=k+1; */
1.238 brouard 14578: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 14579: k=TKresult[nres];
1.338 brouard 14580: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
1.337 brouard 14581: /* for(k=1; k<=i1;k++){ /\* For any combination of dummy covariates, fixed and varying *\/ */
14582: /* if(i1 != 1 && TKresult[nres]!= k) */
14583: /* continue; */
14584: fprintf(ficrespijb,"\n#****** ");
14585: for(j=1;j<=cptcovs;j++){
1.338 brouard 14586: fprintf(ficrespijb," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
1.337 brouard 14587: /* for(j=1;j<=cptcoveff;j++) */
14588: /* fprintf(ficrespijb,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14589: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
14590: /* fprintf(ficrespijb," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
14591: }
14592: fprintf(ficrespijb,"******\n");
14593: if(invalidvarcomb[k]){ /* Is it necessary here? */
14594: fprintf(ficrespijb,"\n#Combination (%d) ignored because no cases \n",k);
14595: continue;
14596: }
14597:
14598: /* for (agedeb=fage; agedeb>=bage; agedeb--){ /\* If stepm=6 months *\/ */
14599: for (agedeb=bage; agedeb<=fage; agedeb++){ /* If stepm=6 months and estepm=24 (2 years) */
14600: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /\* Typically 20 years = 20*12/6=40 *\/ */
14601: 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 */
14602: nhstepm = nhstepm/hstepm; /* Typically 40/4=10, because estepm=24 stepm=6 => hstepm=24/6=4 or 28*/
14603:
14604: /* nhstepm=nhstepm*YEARM; aff par mois*/
14605:
14606: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); /* We can't have it at an upper level because of nhstepm */
14607: /* and memory limitations if stepm is small */
14608:
14609: /* oldm=oldms;savm=savms; */
14610: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
14611: hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm, k, nres);/* Bug valgrind */
14612: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm,oldm,savm, dnewm, doldm, dsavm, k); */
14613: fprintf(ficrespijb,"# Cov Agex agex-h hbijx with i,j=");
14614: for(i=1; i<=nlstate;i++)
14615: for(j=1; j<=nlstate+ndeath;j++)
14616: fprintf(ficrespijb," %1d-%1d",i,j);
14617: fprintf(ficrespijb,"\n");
14618: for (h=0; h<=nhstepm; h++){
14619: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
14620: fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb - h*hstepm/YEARM*stepm );
14621: /* fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm ); */
1.217 brouard 14622: for(i=1; i<=nlstate;i++)
14623: for(j=1; j<=nlstate+ndeath;j++)
1.337 brouard 14624: fprintf(ficrespijb," %.5f", p3mat[i][j][h]);/* Bug valgrind */
1.217 brouard 14625: fprintf(ficrespijb,"\n");
1.337 brouard 14626: }
14627: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
14628: fprintf(ficrespijb,"\n");
14629: } /* end age deb */
14630: /* } /\* end combination *\/ */
1.238 brouard 14631: } /* end nres */
1.218 brouard 14632: return 0;
14633: } /* hBijx */
1.217 brouard 14634:
1.180 brouard 14635:
1.136 brouard 14636: /***********************************************/
14637: /**************** Main Program *****************/
14638: /***********************************************/
14639:
14640: int main(int argc, char *argv[])
14641: {
14642: #ifdef GSL
14643: const gsl_multimin_fminimizer_type *T;
14644: size_t iteri = 0, it;
14645: int rval = GSL_CONTINUE;
14646: int status = GSL_SUCCESS;
14647: double ssval;
14648: #endif
14649: int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav);
1.290 brouard 14650: int i,j, k, iter=0,m,size=100, cptcod; /* Suppressing because nobs */
14651: /* int i,j, k, n=MAXN,iter=0,m,size=100, cptcod; */
1.209 brouard 14652: int ncvyear=0; /* Number of years needed for the period prevalence to converge */
1.164 brouard 14653: int jj, ll, li, lj, lk;
1.136 brouard 14654: int numlinepar=0; /* Current linenumber of parameter file */
1.197 brouard 14655: int num_filled;
1.136 brouard 14656: int itimes;
14657: int NDIM=2;
14658: int vpopbased=0;
1.235 brouard 14659: int nres=0;
1.258 brouard 14660: int endishere=0;
1.277 brouard 14661: int noffset=0;
1.274 brouard 14662: int ncurrv=0; /* Temporary variable */
14663:
1.164 brouard 14664: char ca[32], cb[32];
1.136 brouard 14665: /* FILE *fichtm; *//* Html File */
14666: /* FILE *ficgp;*/ /*Gnuplot File */
14667: struct stat info;
1.191 brouard 14668: double agedeb=0.;
1.194 brouard 14669:
14670: double ageminpar=AGEOVERFLOW,agemin=AGEOVERFLOW, agemaxpar=-AGEOVERFLOW, agemax=-AGEOVERFLOW;
1.219 brouard 14671: double ageminout=-AGEOVERFLOW,agemaxout=AGEOVERFLOW; /* Smaller Age range redefined after movingaverage */
1.136 brouard 14672:
1.361 brouard 14673: double stdpercent; /* for computing the std error of percent e.i: e.i/e.. */
1.165 brouard 14674: double fret;
1.191 brouard 14675: double dum=0.; /* Dummy variable */
1.359 brouard 14676: /* double*** p3mat;*/
1.218 brouard 14677: /* double ***mobaverage; */
1.319 brouard 14678: double wald;
1.164 brouard 14679:
1.351 brouard 14680: char line[MAXLINE], linetmp[MAXLINE];
1.197 brouard 14681: char path[MAXLINE],pathc[MAXLINE],pathcd[MAXLINE],pathtot[MAXLINE];
14682:
1.234 brouard 14683: char modeltemp[MAXLINE];
1.332 brouard 14684: char resultline[MAXLINE], resultlineori[MAXLINE];
1.230 brouard 14685:
1.136 brouard 14686: char pathr[MAXLINE], pathimach[MAXLINE];
1.164 brouard 14687: char *tok, *val; /* pathtot */
1.334 brouard 14688: /* int firstobs=1, lastobs=10; /\* nobs = lastobs-firstobs declared globally ;*\/ */
1.359 brouard 14689: int c, h; /* c2; */
1.191 brouard 14690: int jl=0;
14691: int i1, j1, jk, stepsize=0;
1.194 brouard 14692: int count=0;
14693:
1.164 brouard 14694: int *tab;
1.136 brouard 14695: int mobilavproj=0 , prevfcast=0 ; /* moving average of prev, If prevfcast=1 prevalence projection */
1.296 brouard 14696: /* double anprojd, mprojd, jprojd; /\* For eventual projections *\/ */
14697: /* double anprojf, mprojf, jprojf; */
14698: /* double jintmean,mintmean,aintmean; */
14699: int prvforecast = 0; /* Might be 1 (date of beginning of projection is a choice or 2 is the dateintmean */
14700: int prvbackcast = 0; /* Might be 1 (date of beginning of projection is a choice or 2 is the dateintmean */
14701: double yrfproj= 10.0; /* Number of years of forward projections */
14702: double yrbproj= 10.0; /* Number of years of backward projections */
14703: int prevbcast=0; /* defined as global for mlikeli and mle, replacing backcast */
1.136 brouard 14704: int mobilav=0,popforecast=0;
1.191 brouard 14705: int hstepm=0, nhstepm=0;
1.136 brouard 14706: int agemortsup;
14707: float sumlpop=0.;
14708: double jprev1=1, mprev1=1,anprev1=2000,jprev2=1, mprev2=1,anprev2=2000;
14709: double jpyram=1, mpyram=1,anpyram=2000,jpyram1=1, mpyram1=1,anpyram1=2000;
14710:
1.191 brouard 14711: double bage=0, fage=110., age, agelim=0., agebase=0.;
1.136 brouard 14712: double ftolpl=FTOL;
14713: double **prlim;
1.217 brouard 14714: double **bprlim;
1.317 brouard 14715: double ***param; /* Matrix of parameters, param[i][j][k] param=ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel)
14716: state of origin, state of destination including death, for each covariate: constante, age, and V1 V2 etc. */
1.251 brouard 14717: double ***paramstart; /* Matrix of starting parameter values */
14718: double *p, *pstart; /* p=param[1][1] pstart is for starting values guessed by freqsummary */
1.136 brouard 14719: double **matcov; /* Matrix of covariance */
1.203 brouard 14720: double **hess; /* Hessian matrix */
1.136 brouard 14721: double ***delti3; /* Scale */
14722: double *delti; /* Scale */
14723: double ***eij, ***vareij;
1.359 brouard 14724: //double **varpl; /* Variances of prevalence limits by age */
1.269 brouard 14725:
1.136 brouard 14726: double *epj, vepp;
1.164 brouard 14727:
1.273 brouard 14728: double dateprev1, dateprev2;
1.296 brouard 14729: double jproj1=1,mproj1=1,anproj1=2000,jproj2=1,mproj2=1,anproj2=2000, dateproj1=0, dateproj2=0, dateprojd=0, dateprojf=0;
14730: double jback1=1,mback1=1,anback1=2000,jback2=1,mback2=1,anback2=2000, dateback1=0, dateback2=0, datebackd=0, datebackf=0;
14731:
1.217 brouard 14732:
1.136 brouard 14733: double **ximort;
1.145 brouard 14734: char *alph[]={"a","a","b","c","d","e"}, str[4]="1234";
1.136 brouard 14735: int *dcwave;
14736:
1.164 brouard 14737: char z[1]="c";
1.136 brouard 14738:
14739: /*char *strt;*/
14740: char strtend[80];
1.126 brouard 14741:
1.164 brouard 14742:
1.126 brouard 14743: /* setlocale (LC_ALL, ""); */
14744: /* bindtextdomain (PACKAGE, LOCALEDIR); */
14745: /* textdomain (PACKAGE); */
14746: /* setlocale (LC_CTYPE, ""); */
14747: /* setlocale (LC_MESSAGES, ""); */
14748:
14749: /* gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */
1.157 brouard 14750: rstart_time = time(NULL);
14751: /* (void) gettimeofday(&start_time,&tzp);*/
14752: start_time = *localtime(&rstart_time);
1.126 brouard 14753: curr_time=start_time;
1.157 brouard 14754: /*tml = *localtime(&start_time.tm_sec);*/
14755: /* strcpy(strstart,asctime(&tml)); */
14756: strcpy(strstart,asctime(&start_time));
1.126 brouard 14757:
14758: /* printf("Localtime (at start)=%s",strstart); */
1.157 brouard 14759: /* tp.tm_sec = tp.tm_sec +86400; */
14760: /* tm = *localtime(&start_time.tm_sec); */
1.126 brouard 14761: /* tmg.tm_year=tmg.tm_year +dsign*dyear; */
14762: /* tmg.tm_mon=tmg.tm_mon +dsign*dmonth; */
14763: /* tmg.tm_hour=tmg.tm_hour + 1; */
1.157 brouard 14764: /* tp.tm_sec = mktime(&tmg); */
1.126 brouard 14765: /* strt=asctime(&tmg); */
14766: /* printf("Time(after) =%s",strstart); */
14767: /* (void) time (&time_value);
14768: * printf("time=%d,t-=%d\n",time_value,time_value-86400);
14769: * tm = *localtime(&time_value);
14770: * strstart=asctime(&tm);
14771: * printf("tim_value=%d,asctime=%s\n",time_value,strstart);
14772: */
14773:
14774: nberr=0; /* Number of errors and warnings */
14775: nbwarn=0;
1.184 brouard 14776: #ifdef WIN32
14777: _getcwd(pathcd, size);
14778: #else
1.126 brouard 14779: getcwd(pathcd, size);
1.184 brouard 14780: #endif
1.191 brouard 14781: syscompilerinfo(0);
1.359 brouard 14782: printf("\nIMaCh prax version %s, %s\n%s",version, copyright, fullversion);
1.126 brouard 14783: if(argc <=1){
14784: printf("\nEnter the parameter file name: ");
1.205 brouard 14785: if(!fgets(pathr,FILENAMELENGTH,stdin)){
14786: printf("ERROR Empty parameter file name\n");
14787: goto end;
14788: }
1.126 brouard 14789: i=strlen(pathr);
14790: if(pathr[i-1]=='\n')
14791: pathr[i-1]='\0';
1.156 brouard 14792: i=strlen(pathr);
1.205 brouard 14793: if(i >= 1 && pathr[i-1]==' ') {/* This may happen when dragging on oS/X! */
1.156 brouard 14794: pathr[i-1]='\0';
1.205 brouard 14795: }
14796: i=strlen(pathr);
14797: if( i==0 ){
14798: printf("ERROR Empty parameter file name\n");
14799: goto end;
14800: }
14801: for (tok = pathr; tok != NULL; ){
1.126 brouard 14802: printf("Pathr |%s|\n",pathr);
14803: while ((val = strsep(&tok, "\"" )) != NULL && *val == '\0');
14804: printf("val= |%s| pathr=%s\n",val,pathr);
14805: strcpy (pathtot, val);
14806: if(pathr[0] == '\0') break; /* Dirty */
14807: }
14808: }
1.281 brouard 14809: else if (argc<=2){
14810: strcpy(pathtot,argv[1]);
14811: }
1.126 brouard 14812: else{
14813: strcpy(pathtot,argv[1]);
1.281 brouard 14814: strcpy(z,argv[2]);
14815: printf("\nargv[2]=%s z=%c\n",argv[2],z[0]);
1.126 brouard 14816: }
14817: /*if(getcwd(pathcd, MAXLINE)!= NULL)printf ("Error pathcd\n");*/
14818: /*cygwin_split_path(pathtot,path,optionfile);
14819: printf("pathtot=%s, path=%s, optionfile=%s\n",pathtot,path,optionfile);*/
14820: /* cutv(path,optionfile,pathtot,'\\');*/
14821:
14822: /* Split argv[0], imach program to get pathimach */
14823: printf("\nargv[0]=%s argv[1]=%s, \n",argv[0],argv[1]);
14824: split(argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
14825: printf("\nargv[0]=%s pathimach=%s, \noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
14826: /* strcpy(pathimach,argv[0]); */
14827: /* Split argv[1]=pathtot, parameter file name to get path, optionfile, extension and name */
14828: split(pathtot,path,optionfile,optionfilext,optionfilefiname);
14829: printf("\npathtot=%s,\npath=%s,\noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname);
1.184 brouard 14830: #ifdef WIN32
14831: _chdir(path); /* Can be a relative path */
14832: if(_getcwd(pathcd,MAXLINE) > 0) /* So pathcd is the full path */
14833: #else
1.126 brouard 14834: chdir(path); /* Can be a relative path */
1.184 brouard 14835: if (getcwd(pathcd, MAXLINE) > 0) /* So pathcd is the full path */
14836: #endif
14837: printf("Current directory %s!\n",pathcd);
1.126 brouard 14838: strcpy(command,"mkdir ");
14839: strcat(command,optionfilefiname);
14840: if((outcmd=system(command)) != 0){
1.169 brouard 14841: printf("Directory already exists (or can't create it) %s%s, err=%d\n",path,optionfilefiname,outcmd);
1.126 brouard 14842: /* fprintf(ficlog,"Problem creating directory %s%s\n",path,optionfilefiname); */
14843: /* fclose(ficlog); */
14844: /* exit(1); */
14845: }
14846: /* if((imk=mkdir(optionfilefiname))<0){ */
14847: /* perror("mkdir"); */
14848: /* } */
14849:
14850: /*-------- arguments in the command line --------*/
14851:
1.186 brouard 14852: /* Main Log file */
1.126 brouard 14853: strcat(filelog, optionfilefiname);
14854: strcat(filelog,".log"); /* */
14855: if((ficlog=fopen(filelog,"w"))==NULL) {
14856: printf("Problem with logfile %s\n",filelog);
14857: goto end;
14858: }
14859: fprintf(ficlog,"Log filename:%s\n",filelog);
1.197 brouard 14860: fprintf(ficlog,"Version %s %s",version,fullversion);
1.126 brouard 14861: fprintf(ficlog,"\nEnter the parameter file name: \n");
14862: fprintf(ficlog,"pathimach=%s\npathtot=%s\n\
14863: path=%s \n\
14864: optionfile=%s\n\
14865: optionfilext=%s\n\
1.156 brouard 14866: optionfilefiname='%s'\n",pathimach,pathtot,path,optionfile,optionfilext,optionfilefiname);
1.126 brouard 14867:
1.197 brouard 14868: syscompilerinfo(1);
1.167 brouard 14869:
1.126 brouard 14870: printf("Local time (at start):%s",strstart);
14871: fprintf(ficlog,"Local time (at start): %s",strstart);
14872: fflush(ficlog);
14873: /* (void) gettimeofday(&curr_time,&tzp); */
1.157 brouard 14874: /* printf("Elapsed time %d\n", asc_diff_time(curr_time.tm_sec-start_time.tm_sec,tmpout)); */
1.126 brouard 14875:
14876: /* */
14877: strcpy(fileres,"r");
14878: strcat(fileres, optionfilefiname);
1.201 brouard 14879: strcat(fileresu, optionfilefiname); /* Without r in front */
1.126 brouard 14880: strcat(fileres,".txt"); /* Other files have txt extension */
1.201 brouard 14881: strcat(fileresu,".txt"); /* Other files have txt extension */
1.126 brouard 14882:
1.186 brouard 14883: /* Main ---------arguments file --------*/
1.126 brouard 14884:
14885: if((ficpar=fopen(optionfile,"r"))==NULL) {
1.155 brouard 14886: printf("Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
14887: fprintf(ficlog,"Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
1.126 brouard 14888: fflush(ficlog);
1.149 brouard 14889: /* goto end; */
14890: exit(70);
1.126 brouard 14891: }
14892:
14893: strcpy(filereso,"o");
1.201 brouard 14894: strcat(filereso,fileresu);
1.126 brouard 14895: if((ficparo=fopen(filereso,"w"))==NULL) { /* opened on subdirectory */
14896: printf("Problem with Output resultfile: %s\n", filereso);
14897: fprintf(ficlog,"Problem with Output resultfile: %s\n", filereso);
14898: fflush(ficlog);
14899: goto end;
14900: }
1.278 brouard 14901: /*-------- Rewriting parameter file ----------*/
14902: strcpy(rfileres,"r"); /* "Rparameterfile */
14903: strcat(rfileres,optionfilefiname); /* Parameter file first name */
14904: strcat(rfileres,"."); /* */
14905: strcat(rfileres,optionfilext); /* Other files have txt extension */
14906: if((ficres =fopen(rfileres,"w"))==NULL) {
14907: printf("Problem writing new parameter file: %s\n", rfileres);goto end;
14908: fprintf(ficlog,"Problem writing new parameter file: %s\n", rfileres);goto end;
14909: fflush(ficlog);
14910: goto end;
14911: }
14912: fprintf(ficres,"#IMaCh %s\n",version);
1.126 brouard 14913:
1.278 brouard 14914:
1.126 brouard 14915: /* Reads comments: lines beginning with '#' */
14916: numlinepar=0;
1.277 brouard 14917: /* Is it a BOM UTF-8 Windows file? */
14918: /* First parameter line */
1.197 brouard 14919: while(fgets(line, MAXLINE, ficpar)) {
1.277 brouard 14920: noffset=0;
14921: if( line[0] == (char)0xEF && line[1] == (char)0xBB) /* EF BB BF */
14922: {
14923: noffset=noffset+3;
14924: printf("# File is an UTF8 Bom.\n"); // 0xBF
14925: }
1.302 brouard 14926: /* else if( line[0] == (char)0xFE && line[1] == (char)0xFF)*/
14927: else if( line[0] == (char)0xFF && line[1] == (char)0xFE)
1.277 brouard 14928: {
14929: noffset=noffset+2;
14930: printf("# File is an UTF16BE BOM file\n");
14931: }
14932: else if( line[0] == 0 && line[1] == 0)
14933: {
14934: if( line[2] == (char)0xFE && line[3] == (char)0xFF){
14935: noffset=noffset+4;
14936: printf("# File is an UTF16BE BOM file\n");
14937: }
14938: } else{
14939: ;/*printf(" Not a BOM file\n");*/
14940: }
14941:
1.197 brouard 14942: /* If line starts with a # it is a comment */
1.277 brouard 14943: if (line[noffset] == '#') {
1.197 brouard 14944: numlinepar++;
14945: fputs(line,stdout);
14946: fputs(line,ficparo);
1.278 brouard 14947: fputs(line,ficres);
1.197 brouard 14948: fputs(line,ficlog);
14949: continue;
14950: }else
14951: break;
14952: }
14953: if((num_filled=sscanf(line,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", \
14954: title, datafile, &lastobs, &firstpass,&lastpass)) !=EOF){
14955: if (num_filled != 5) {
14956: printf("Should be 5 parameters\n");
1.283 brouard 14957: fprintf(ficlog,"Should be 5 parameters\n");
1.197 brouard 14958: }
1.126 brouard 14959: numlinepar++;
1.197 brouard 14960: printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
1.283 brouard 14961: fprintf(ficparo,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
14962: fprintf(ficres,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
14963: fprintf(ficlog,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
1.197 brouard 14964: }
14965: /* Second parameter line */
14966: while(fgets(line, MAXLINE, ficpar)) {
1.283 brouard 14967: /* while(fscanf(ficpar,"%[^\n]", line)) { */
14968: /* If line starts with a # it is a comment. Strangely fgets reads the EOL and fputs doesn't */
1.197 brouard 14969: if (line[0] == '#') {
14970: numlinepar++;
1.283 brouard 14971: printf("%s",line);
14972: fprintf(ficres,"%s",line);
14973: fprintf(ficparo,"%s",line);
14974: fprintf(ficlog,"%s",line);
1.197 brouard 14975: continue;
14976: }else
14977: break;
14978: }
1.223 brouard 14979: 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", \
14980: &ftol, &stepm, &ncovcol, &nqv, &ntv, &nqtv, &nlstate, &ndeath, &maxwav, &mle, &weightopt)) !=EOF){
14981: if (num_filled != 11) {
14982: printf("Not 11 parameters, for example:ftol=1.e-8 stepm=12 ncovcol=2 nqv=1 ntv=2 nqtv=1 nlstate=2 ndeath=1 maxwav=3 mle=1 weight=1\n");
1.209 brouard 14983: printf("but line=%s\n",line);
1.283 brouard 14984: 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");
14985: fprintf(ficlog,"but line=%s\n",line);
1.197 brouard 14986: }
1.286 brouard 14987: if( lastpass > maxwav){
14988: printf("Error (lastpass = %d) > (maxwav = %d)\n",lastpass, maxwav);
14989: fprintf(ficlog,"Error (lastpass = %d) > (maxwav = %d)\n",lastpass, maxwav);
14990: fflush(ficlog);
14991: goto end;
14992: }
14993: printf("ftol=%e stepm=%d ncovcol=%d nqv=%d ntv=%d nqtv=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\n",ftol, stepm, ncovcol, nqv, ntv, nqtv, nlstate, ndeath, maxwav, mle, weightopt);
1.283 brouard 14994: 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);
1.286 brouard 14995: 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);
1.283 brouard 14996: 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);
1.126 brouard 14997: }
1.203 brouard 14998: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
1.209 brouard 14999: /*ftolpl=6.e-4; *//* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
1.197 brouard 15000: /* Third parameter line */
15001: while(fgets(line, MAXLINE, ficpar)) {
15002: /* If line starts with a # it is a comment */
15003: if (line[0] == '#') {
15004: numlinepar++;
1.283 brouard 15005: printf("%s",line);
15006: fprintf(ficres,"%s",line);
15007: fprintf(ficparo,"%s",line);
15008: fprintf(ficlog,"%s",line);
1.197 brouard 15009: continue;
15010: }else
15011: break;
15012: }
1.351 brouard 15013: if((num_filled=sscanf(line,"model=%[^.\n]", model)) !=EOF){ /* Every character after model but dot and return */
15014: if (num_filled != 1){
15015: printf("ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line);
15016: fprintf(ficlog,"ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line);
15017: model[0]='\0';
15018: goto end;
15019: }else{
15020: trimbtab(linetmp,line); /* Trims multiple blanks in line */
15021: strcpy(line, linetmp);
15022: }
15023: }
15024: if((num_filled=sscanf(line,"model=1+age%[^.\n]", model)) !=EOF){ /* Every character after 1+age but dot and return */
1.279 brouard 15025: if (num_filled != 1){
1.302 brouard 15026: printf("ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line);
15027: fprintf(ficlog,"ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line);
1.197 brouard 15028: model[0]='\0';
15029: goto end;
15030: }
15031: else{
15032: if (model[0]=='+'){
15033: for(i=1; i<=strlen(model);i++)
15034: modeltemp[i-1]=model[i];
1.201 brouard 15035: strcpy(model,modeltemp);
1.197 brouard 15036: }
15037: }
1.338 brouard 15038: /* printf(" model=1+age%s modeltemp= %s, model=1+age+%s\n",model, modeltemp, model);fflush(stdout); */
1.203 brouard 15039: printf("model=1+age+%s\n",model);fflush(stdout);
1.283 brouard 15040: fprintf(ficparo,"model=1+age+%s\n",model);fflush(stdout);
15041: fprintf(ficres,"model=1+age+%s\n",model);fflush(stdout);
15042: fprintf(ficlog,"model=1+age+%s\n",model);fflush(stdout);
1.197 brouard 15043: }
15044: /* 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); */
15045: /* numlinepar=numlinepar+3; /\* In general *\/ */
15046: /* printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=1+age+%s\n", title, datafile, lastobs, firstpass,lastpass,ftol, stepm, ncovcol, nlstate,ndeath, maxwav, mle, weightopt,model); */
1.283 brouard 15047: /* 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); */
15048: /* fprintf(ficlog,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nqv=%d ntv=%d nqtv=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=1+age+%s.\n", title, datafile, lastobs, firstpass,lastpass,ftol,stepm,ncovcol, nqv, ntv, nqtv, nlstate,ndeath,maxwav, mle, weightopt,model); */
1.126 brouard 15049: fflush(ficlog);
1.190 brouard 15050: /* if(model[0]=='#'|| model[0]== '\0'){ */
15051: if(model[0]=='#'){
1.279 brouard 15052: printf("Error in 'model' line: model should start with 'model=1+age+' and end without space \n \
15053: 'model=1+age+' or 'model=1+age+V1.' or 'model=1+age+age*age+V1+V1*age' or \n \
15054: 'model=1+age+V1+V2' or 'model=1+age+V1+V2+V1*V2' etc. \n"); \
1.187 brouard 15055: if(mle != -1){
1.279 brouard 15056: 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");
1.187 brouard 15057: exit(1);
15058: }
15059: }
1.126 brouard 15060: while((c=getc(ficpar))=='#' && c!= EOF){
15061: ungetc(c,ficpar);
15062: fgets(line, MAXLINE, ficpar);
15063: numlinepar++;
1.195 brouard 15064: if(line[1]=='q'){ /* This #q will quit imach (the answer is q) */
15065: z[0]=line[1];
1.342 brouard 15066: }else if(line[1]=='d'){ /* For debugging individual values of covariates in ficresilk */
1.343 brouard 15067: debugILK=1;printf("DebugILK\n");
1.195 brouard 15068: }
15069: /* printf("****line [1] = %c \n",line[1]); */
1.141 brouard 15070: fputs(line, stdout);
15071: //puts(line);
1.126 brouard 15072: fputs(line,ficparo);
15073: fputs(line,ficlog);
15074: }
15075: ungetc(c,ficpar);
15076:
15077:
1.290 brouard 15078: covar=matrix(0,NCOVMAX,firstobs,lastobs); /**< used in readdata */
15079: if(nqv>=1)coqvar=matrix(1,nqv,firstobs,lastobs); /**< Fixed quantitative covariate */
15080: if(nqtv>=1)cotqvar=ma3x(1,maxwav,1,nqtv,firstobs,lastobs); /**< Time varying quantitative covariate */
1.341 brouard 15081: /* if(ntv+nqtv>=1)cotvar=ma3x(1,maxwav,1,ntv+nqtv,firstobs,lastobs); /\**< Time varying covariate (dummy and quantitative)*\/ */
15082: if(ntv+nqtv>=1)cotvar=ma3x(1,maxwav,ncovcol+nqv+1,ncovcol+nqv+ntv+nqtv,firstobs,lastobs); /**< Might be better */
1.136 brouard 15083: cptcovn=0; /*Number of covariates, i.e. number of '+' in model statement plus one, indepently of n in Vn*/
15084: /* v1+v2+v3+v2*v4+v5*age makes cptcovn = 5
15085: v1+v2*age+v2*v3 makes cptcovn = 3
15086: */
15087: if (strlen(model)>1)
1.187 brouard 15088: ncovmodel=2+nbocc(model,'+')+1; /*Number of variables including intercept and age = cptcovn + intercept + age : v1+v2+v3+v2*v4+v5*age makes 5+2=7,age*age makes 3*/
1.145 brouard 15089: else
1.187 brouard 15090: ncovmodel=2; /* Constant and age */
1.133 brouard 15091: nforce= (nlstate+ndeath-1)*nlstate; /* Number of forces ij from state i to j */
15092: npar= nforce*ncovmodel; /* Number of parameters like aij*/
1.131 brouard 15093: if(npar >MAXPARM || nlstate >NLSTATEMAX || ndeath >NDEATHMAX || ncovmodel>NCOVMAX){
15094: 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);
15095: 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);
15096: fflush(stdout);
15097: fclose (ficlog);
15098: goto end;
15099: }
1.126 brouard 15100: delti3= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
15101: delti=delti3[1][1];
15102: /*delti=vector(1,npar); *//* Scale of each paramater (output from hesscov)*/
15103: if(mle==-1){ /* Print a wizard for help writing covariance matrix */
1.247 brouard 15104: /* We could also provide initial parameters values giving by simple logistic regression
15105: * only one way, that is without matrix product. We will have nlstate maximizations */
15106: /* for(i=1;i<nlstate;i++){ */
15107: /* /\*reducing xi for 1 to npar to 1 to ncovmodel; *\/ */
15108: /* mlikeli(ficres,p, ncovmodel, ncovmodel, nlstate, ftol, funcnoprod); */
15109: /* } */
1.126 brouard 15110: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
1.191 brouard 15111: printf(" You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
15112: fprintf(ficlog," You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
1.126 brouard 15113: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
15114: fclose (ficparo);
15115: fclose (ficlog);
15116: goto end;
15117: exit(0);
1.220 brouard 15118: } else if(mle==-5) { /* Main Wizard */
1.126 brouard 15119: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
1.192 brouard 15120: printf(" You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
15121: fprintf(ficlog," You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
1.126 brouard 15122: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
15123: matcov=matrix(1,npar,1,npar);
1.203 brouard 15124: hess=matrix(1,npar,1,npar);
1.220 brouard 15125: } else{ /* Begin of mle != -1 or -5 */
1.145 brouard 15126: /* Read guessed parameters */
1.126 brouard 15127: /* Reads comments: lines beginning with '#' */
15128: while((c=getc(ficpar))=='#' && c!= EOF){
15129: ungetc(c,ficpar);
15130: fgets(line, MAXLINE, ficpar);
15131: numlinepar++;
1.141 brouard 15132: fputs(line,stdout);
1.126 brouard 15133: fputs(line,ficparo);
15134: fputs(line,ficlog);
15135: }
15136: ungetc(c,ficpar);
15137:
15138: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
1.251 brouard 15139: paramstart= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
1.126 brouard 15140: for(i=1; i <=nlstate; i++){
1.234 brouard 15141: j=0;
1.126 brouard 15142: for(jj=1; jj <=nlstate+ndeath; jj++){
1.234 brouard 15143: if(jj==i) continue;
15144: j++;
1.292 brouard 15145: while((c=getc(ficpar))=='#' && c!= EOF){
15146: ungetc(c,ficpar);
15147: fgets(line, MAXLINE, ficpar);
15148: numlinepar++;
15149: fputs(line,stdout);
15150: fputs(line,ficparo);
15151: fputs(line,ficlog);
15152: }
15153: ungetc(c,ficpar);
1.234 brouard 15154: fscanf(ficpar,"%1d%1d",&i1,&j1);
15155: if ((i1 != i) || (j1 != jj)){
15156: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n \
1.126 brouard 15157: It might be a problem of design; if ncovcol and the model are correct\n \
15158: run imach with mle=-1 to get a correct template of the parameter file.\n",numlinepar, i,j, i1, j1);
1.234 brouard 15159: exit(1);
15160: }
15161: fprintf(ficparo,"%1d%1d",i1,j1);
15162: if(mle==1)
15163: printf("%1d%1d",i,jj);
15164: fprintf(ficlog,"%1d%1d",i,jj);
15165: for(k=1; k<=ncovmodel;k++){
15166: fscanf(ficpar," %lf",¶m[i][j][k]);
15167: if(mle==1){
15168: printf(" %lf",param[i][j][k]);
15169: fprintf(ficlog," %lf",param[i][j][k]);
15170: }
15171: else
15172: fprintf(ficlog," %lf",param[i][j][k]);
15173: fprintf(ficparo," %lf",param[i][j][k]);
15174: }
15175: fscanf(ficpar,"\n");
15176: numlinepar++;
15177: if(mle==1)
15178: printf("\n");
15179: fprintf(ficlog,"\n");
15180: fprintf(ficparo,"\n");
1.126 brouard 15181: }
15182: }
15183: fflush(ficlog);
1.234 brouard 15184:
1.251 brouard 15185: /* Reads parameters values */
1.126 brouard 15186: p=param[1][1];
1.251 brouard 15187: pstart=paramstart[1][1];
1.126 brouard 15188:
15189: /* Reads comments: lines beginning with '#' */
15190: while((c=getc(ficpar))=='#' && c!= EOF){
15191: ungetc(c,ficpar);
15192: fgets(line, MAXLINE, ficpar);
15193: numlinepar++;
1.141 brouard 15194: fputs(line,stdout);
1.126 brouard 15195: fputs(line,ficparo);
15196: fputs(line,ficlog);
15197: }
15198: ungetc(c,ficpar);
15199:
15200: for(i=1; i <=nlstate; i++){
15201: for(j=1; j <=nlstate+ndeath-1; j++){
1.234 brouard 15202: fscanf(ficpar,"%1d%1d",&i1,&j1);
15203: if ( (i1-i) * (j1-j) != 0){
15204: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n",numlinepar, i,j, i1, j1);
15205: exit(1);
15206: }
15207: printf("%1d%1d",i,j);
15208: fprintf(ficparo,"%1d%1d",i1,j1);
15209: fprintf(ficlog,"%1d%1d",i1,j1);
15210: for(k=1; k<=ncovmodel;k++){
15211: fscanf(ficpar,"%le",&delti3[i][j][k]);
15212: printf(" %le",delti3[i][j][k]);
15213: fprintf(ficparo," %le",delti3[i][j][k]);
15214: fprintf(ficlog," %le",delti3[i][j][k]);
15215: }
15216: fscanf(ficpar,"\n");
15217: numlinepar++;
15218: printf("\n");
15219: fprintf(ficparo,"\n");
15220: fprintf(ficlog,"\n");
1.126 brouard 15221: }
15222: }
15223: fflush(ficlog);
1.234 brouard 15224:
1.145 brouard 15225: /* Reads covariance matrix */
1.126 brouard 15226: delti=delti3[1][1];
1.220 brouard 15227:
15228:
1.126 brouard 15229: /* free_ma3x(delti3,1,nlstate,1,nlstate+ndeath-1,1,ncovmodel); */ /* Hasn't to to freed here otherwise delti is no more allocated */
1.220 brouard 15230:
1.126 brouard 15231: /* Reads comments: lines beginning with '#' */
15232: while((c=getc(ficpar))=='#' && c!= EOF){
15233: ungetc(c,ficpar);
15234: fgets(line, MAXLINE, ficpar);
15235: numlinepar++;
1.141 brouard 15236: fputs(line,stdout);
1.126 brouard 15237: fputs(line,ficparo);
15238: fputs(line,ficlog);
15239: }
15240: ungetc(c,ficpar);
1.220 brouard 15241:
1.126 brouard 15242: matcov=matrix(1,npar,1,npar);
1.203 brouard 15243: hess=matrix(1,npar,1,npar);
1.131 brouard 15244: for(i=1; i <=npar; i++)
15245: for(j=1; j <=npar; j++) matcov[i][j]=0.;
1.220 brouard 15246:
1.194 brouard 15247: /* Scans npar lines */
1.126 brouard 15248: for(i=1; i <=npar; i++){
1.226 brouard 15249: count=fscanf(ficpar,"%1d%1d%d",&i1,&j1,&jk);
1.194 brouard 15250: if(count != 3){
1.226 brouard 15251: printf("Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
1.194 brouard 15252: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
15253: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
1.226 brouard 15254: fprintf(ficlog,"Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
1.194 brouard 15255: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
15256: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
1.226 brouard 15257: exit(1);
1.220 brouard 15258: }else{
1.226 brouard 15259: if(mle==1)
15260: printf("%1d%1d%d",i1,j1,jk);
15261: }
15262: fprintf(ficlog,"%1d%1d%d",i1,j1,jk);
15263: fprintf(ficparo,"%1d%1d%d",i1,j1,jk);
1.126 brouard 15264: for(j=1; j <=i; j++){
1.226 brouard 15265: fscanf(ficpar," %le",&matcov[i][j]);
15266: if(mle==1){
15267: printf(" %.5le",matcov[i][j]);
15268: }
15269: fprintf(ficlog," %.5le",matcov[i][j]);
15270: fprintf(ficparo," %.5le",matcov[i][j]);
1.126 brouard 15271: }
15272: fscanf(ficpar,"\n");
15273: numlinepar++;
15274: if(mle==1)
1.220 brouard 15275: printf("\n");
1.126 brouard 15276: fprintf(ficlog,"\n");
15277: fprintf(ficparo,"\n");
15278: }
1.194 brouard 15279: /* End of read covariance matrix npar lines */
1.126 brouard 15280: for(i=1; i <=npar; i++)
15281: for(j=i+1;j<=npar;j++)
1.226 brouard 15282: matcov[i][j]=matcov[j][i];
1.126 brouard 15283:
15284: if(mle==1)
15285: printf("\n");
15286: fprintf(ficlog,"\n");
15287:
15288: fflush(ficlog);
15289:
15290: } /* End of mle != -3 */
1.218 brouard 15291:
1.186 brouard 15292: /* Main data
15293: */
1.290 brouard 15294: nobs=lastobs-firstobs+1; /* was = lastobs;*/
15295: /* num=lvector(1,n); */
15296: /* moisnais=vector(1,n); */
15297: /* annais=vector(1,n); */
15298: /* moisdc=vector(1,n); */
15299: /* andc=vector(1,n); */
15300: /* weight=vector(1,n); */
15301: /* agedc=vector(1,n); */
15302: /* cod=ivector(1,n); */
15303: /* for(i=1;i<=n;i++){ */
15304: num=lvector(firstobs,lastobs);
15305: moisnais=vector(firstobs,lastobs);
15306: annais=vector(firstobs,lastobs);
15307: moisdc=vector(firstobs,lastobs);
15308: andc=vector(firstobs,lastobs);
15309: weight=vector(firstobs,lastobs);
15310: agedc=vector(firstobs,lastobs);
15311: cod=ivector(firstobs,lastobs);
15312: for(i=firstobs;i<=lastobs;i++){
1.234 brouard 15313: num[i]=0;
15314: moisnais[i]=0;
15315: annais[i]=0;
15316: moisdc[i]=0;
15317: andc[i]=0;
15318: agedc[i]=0;
15319: cod[i]=0;
15320: weight[i]=1.0; /* Equal weights, 1 by default */
15321: }
1.290 brouard 15322: mint=matrix(1,maxwav,firstobs,lastobs);
15323: anint=matrix(1,maxwav,firstobs,lastobs);
1.325 brouard 15324: s=imatrix(1,maxwav+1,firstobs,lastobs); /* s[i][j] health state for wave i and individual j */
1.336 brouard 15325: /* printf("BUG ncovmodel=%d NCOVMAX=%d 2**ncovmodel=%f BUG\n",ncovmodel,NCOVMAX,pow(2,ncovmodel)); */
1.126 brouard 15326: tab=ivector(1,NCOVMAX);
1.144 brouard 15327: ncodemax=ivector(1,NCOVMAX); /* Number of code per covariate; if O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */
1.192 brouard 15328: ncodemaxwundef=ivector(1,NCOVMAX); /* Number of code per covariate; if - 1 O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */
1.126 brouard 15329:
1.136 brouard 15330: /* Reads data from file datafile */
15331: if (readdata(datafile, firstobs, lastobs, &imx)==1)
15332: goto end;
15333:
15334: /* Calculation of the number of parameters from char model */
1.234 brouard 15335: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4
1.137 brouard 15336: k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tag[cptcovage=1]=4
15337: k=3 V4 Tvar[k=3]= 4 (from V4)
15338: k=2 V1 Tvar[k=2]= 1 (from V1)
15339: k=1 Tvar[1]=2 (from V2)
1.234 brouard 15340: */
15341:
15342: Tvar=ivector(1,NCOVMAX); /* Was 15 changed to NCOVMAX. */
15343: TvarsDind=ivector(1,NCOVMAX); /* */
1.330 brouard 15344: TnsdVar=ivector(1,NCOVMAX); /* */
1.335 brouard 15345: /* for(i=1; i<=NCOVMAX;i++) TnsdVar[i]=3; */
1.234 brouard 15346: TvarsD=ivector(1,NCOVMAX); /* */
15347: TvarsQind=ivector(1,NCOVMAX); /* */
15348: TvarsQ=ivector(1,NCOVMAX); /* */
1.232 brouard 15349: TvarF=ivector(1,NCOVMAX); /* */
15350: TvarFind=ivector(1,NCOVMAX); /* */
15351: TvarV=ivector(1,NCOVMAX); /* */
15352: TvarVind=ivector(1,NCOVMAX); /* */
15353: TvarA=ivector(1,NCOVMAX); /* */
15354: TvarAind=ivector(1,NCOVMAX); /* */
1.231 brouard 15355: TvarFD=ivector(1,NCOVMAX); /* */
15356: TvarFDind=ivector(1,NCOVMAX); /* */
15357: TvarFQ=ivector(1,NCOVMAX); /* */
15358: TvarFQind=ivector(1,NCOVMAX); /* */
15359: TvarVD=ivector(1,NCOVMAX); /* */
15360: TvarVDind=ivector(1,NCOVMAX); /* */
15361: TvarVQ=ivector(1,NCOVMAX); /* */
15362: TvarVQind=ivector(1,NCOVMAX); /* */
1.339 brouard 15363: TvarVV=ivector(1,NCOVMAX); /* */
15364: TvarVVind=ivector(1,NCOVMAX); /* */
1.349 brouard 15365: TvarVVA=ivector(1,NCOVMAX); /* */
15366: TvarVVAind=ivector(1,NCOVMAX); /* */
15367: TvarAVVA=ivector(1,NCOVMAX); /* */
15368: TvarAVVAind=ivector(1,NCOVMAX); /* */
1.231 brouard 15369:
1.230 brouard 15370: Tvalsel=vector(1,NCOVMAX); /* */
1.233 brouard 15371: Tvarsel=ivector(1,NCOVMAX); /* */
1.226 brouard 15372: Typevar=ivector(-1,NCOVMAX); /* -1 to 2 */
15373: Fixed=ivector(-1,NCOVMAX); /* -1 to 3 */
15374: Dummy=ivector(-1,NCOVMAX); /* -1 to 3 */
1.349 brouard 15375: DummyV=ivector(-1,NCOVMAX); /* 1 to 3 */
15376: FixedV=ivector(-1,NCOVMAX); /* 1 to 3 */
15377:
1.137 brouard 15378: /* V2+V1+V4+age*V3 is a model with 4 covariates (3 plus signs).
15379: For each model-covariate stores the data-covariate id. Tvar[1]=2, Tvar[2]=1, Tvar[3]=4,
15380: Tvar[4=age*V3] is 3 and 'age' is recorded in Tage.
15381: */
15382: /* For model-covariate k tells which data-covariate to use but
15383: because this model-covariate is a construction we invent a new column
15384: ncovcol + k1
15385: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3
15386: Tvar[3=V1*V4]=4+1 etc */
1.227 brouard 15387: Tprod=ivector(1,NCOVMAX); /* Gives the k position of the k1 product */
15388: Tposprod=ivector(1,NCOVMAX); /* Gives the k1 product from the k position */
1.137 brouard 15389: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3
15390: if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2)
1.227 brouard 15391: Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5]=2
1.137 brouard 15392: */
1.145 brouard 15393: Tvaraff=ivector(1,NCOVMAX); /* Unclear */
15394: Tvard=imatrix(1,NCOVMAX,1,2); /* n=Tvard[k1][1] and m=Tvard[k1][2] gives the couple n,m of the k1 th product Vn*Vm
1.141 brouard 15395: * For V3*V2 (in V2+V1+V1*V4+age*V3+V3*V2), V3*V2 position is 2nd.
15396: * Tvard[k1=2][1]=3 (V3) Tvard[k1=2][2]=2(V2) */
1.351 brouard 15397: Tvardk=imatrix(0,NCOVMAX,1,2);
1.145 brouard 15398: Tage=ivector(1,NCOVMAX); /* Gives the covariate id of covariates associated with age: V2 + V1 + age*V4 + V3*age
1.137 brouard 15399: 4 covariates (3 plus signs)
15400: Tage[1=V3*age]= 4; Tage[2=age*V4] = 3
1.328 brouard 15401: */
15402: for(i=1;i<NCOVMAX;i++)
15403: Tage[i]=0;
1.230 brouard 15404: Tmodelind=ivector(1,NCOVMAX);/** gives the k model position of an
1.227 brouard 15405: * individual dummy, fixed or varying:
15406: * Tmodelind[Tvaraff[3]]=9,Tvaraff[1]@9={4,
15407: * 3, 1, 0, 0, 0, 0, 0, 0},
1.230 brouard 15408: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 ,
15409: * V1 df, V2 qf, V3 & V4 dv, V5 qv
15410: * Tmodelind[1]@9={9,0,3,2,}*/
15411: TmodelInvind=ivector(1,NCOVMAX); /* TmodelInvind=Tvar[k]- ncovcol-nqv={5-2-1=2,*/
15412: TmodelInvQind=ivector(1,NCOVMAX);/** gives the k model position of an
1.228 brouard 15413: * individual quantitative, fixed or varying:
15414: * Tmodelqind[1]=1,Tvaraff[1]@9={4,
15415: * 3, 1, 0, 0, 0, 0, 0, 0},
15416: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1*/
1.349 brouard 15417:
15418: /* Probably useless zeroes */
15419: for(i=1;i<NCOVMAX;i++){
15420: DummyV[i]=0;
15421: FixedV[i]=0;
15422: }
15423:
15424: for(i=1; i <=ncovcol;i++){
15425: DummyV[i]=0;
15426: FixedV[i]=0;
15427: }
15428: for(i=ncovcol+1; i <=ncovcol+nqv;i++){
15429: DummyV[i]=1;
15430: FixedV[i]=0;
15431: }
15432: for(i=ncovcol+nqv+1; i <=ncovcol+nqv+ntv;i++){
15433: DummyV[i]=0;
15434: FixedV[i]=1;
15435: }
15436: for(i=ncovcol+nqv+ntv+1; i <=ncovcol+nqv+ntv+nqtv;i++){
15437: DummyV[i]=1;
15438: FixedV[i]=1;
15439: }
15440: for(i=1; i <=ncovcol+nqv+ntv+nqtv;i++){
15441: printf("Covariate type in the data: V%d, DummyV(V%d)=%d, FixedV(V%d)=%d\n",i,i,DummyV[i],i,FixedV[i]);
15442: 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]);
15443: }
15444:
15445:
15446:
1.186 brouard 15447: /* Main decodemodel */
15448:
1.187 brouard 15449:
1.223 brouard 15450: if(decodemodel(model, lastobs) == 1) /* In order to get Tvar[k] V4+V3+V5 p Tvar[1]@3 = {4, 3, 5}*/
1.136 brouard 15451: goto end;
15452:
1.137 brouard 15453: if((double)(lastobs-imx)/(double)imx > 1.10){
15454: nbwarn++;
15455: 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);
15456: 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);
15457: }
1.136 brouard 15458: /* if(mle==1){*/
1.137 brouard 15459: if (weightopt != 1) { /* Maximisation without weights. We can have weights different from 1 but want no weight*/
15460: for(i=1;i<=imx;i++) weight[i]=1.0; /* changed to imx */
1.136 brouard 15461: }
15462:
15463: /*-calculation of age at interview from date of interview and age at death -*/
15464: agev=matrix(1,maxwav,1,imx);
15465:
15466: if(calandcheckages(imx, maxwav, &agemin, &agemax, &nberr, &nbwarn) == 1)
15467: goto end;
15468:
1.126 brouard 15469:
1.136 brouard 15470: agegomp=(int)agemin;
1.290 brouard 15471: free_vector(moisnais,firstobs,lastobs);
15472: free_vector(annais,firstobs,lastobs);
1.126 brouard 15473: /* free_matrix(mint,1,maxwav,1,n);
15474: free_matrix(anint,1,maxwav,1,n);*/
1.215 brouard 15475: /* free_vector(moisdc,1,n); */
15476: /* free_vector(andc,1,n); */
1.145 brouard 15477: /* */
15478:
1.126 brouard 15479: wav=ivector(1,imx);
1.214 brouard 15480: /* dh=imatrix(1,lastpass-firstpass+1,1,imx); */
15481: /* bh=imatrix(1,lastpass-firstpass+1,1,imx); */
15482: /* mw=imatrix(1,lastpass-firstpass+1,1,imx); */
15483: 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.*/
15484: bh=imatrix(1,lastpass-firstpass+2,1,imx);
15485: mw=imatrix(1,lastpass-firstpass+2,1,imx);
1.126 brouard 15486:
15487: /* Concatenates waves */
1.214 brouard 15488: /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
15489: Death is a valid wave (if date is known).
15490: mw[mi][i] is the number of (mi=1 to wav[i]) effective wave out of mi of individual i
15491: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
15492: and mw[mi+1][i]. dh depends on stepm.
15493: */
15494:
1.126 brouard 15495: concatwav(wav, dh, bh, mw, s, agedc, agev, firstpass, lastpass, imx, nlstate, stepm);
1.248 brouard 15496: /* Concatenates waves */
1.145 brouard 15497:
1.290 brouard 15498: free_vector(moisdc,firstobs,lastobs);
15499: free_vector(andc,firstobs,lastobs);
1.215 brouard 15500:
1.126 brouard 15501: /* Routine tricode is to calculate cptcoveff (real number of unique covariates) and to associate covariable number and modality */
15502: nbcode=imatrix(0,NCOVMAX,0,NCOVMAX);
15503: ncodemax[1]=1;
1.145 brouard 15504: Ndum =ivector(-1,NCOVMAX);
1.225 brouard 15505: cptcoveff=0;
1.220 brouard 15506: if (ncovmodel-nagesqr > 2 ){ /* That is if covariate other than cst, age and age*age */
1.335 brouard 15507: tricode(&cptcoveff,Tvar,nbcode,imx, Ndum); /**< Fills nbcode[Tvar[j]][l]; as well as calculate cptcoveff or number of total effective dummy covariates*/
1.227 brouard 15508: }
15509:
15510: ncovcombmax=pow(2,cptcoveff);
1.338 brouard 15511: invalidvarcomb=ivector(0, ncovcombmax);
15512: for(i=0;i<ncovcombmax;i++)
1.227 brouard 15513: invalidvarcomb[i]=0;
15514:
1.211 brouard 15515: /* Nbcode gives the value of the lth modality (currently 1 to 2) of jth covariate, in
1.186 brouard 15516: V2+V1*age, there are 3 covariates Tvar[2]=1 (V1).*/
1.211 brouard 15517: /* 1 to ncodemax[j] which is the maximum value of this jth covariate */
1.227 brouard 15518:
1.200 brouard 15519: /* codtab=imatrix(1,100,1,10);*/ /* codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) */
1.198 brouard 15520: /*printf(" codtab[1,1],codtab[100,10]=%d,%d\n", codtab[1][1],codtabm(100,10));*/
1.186 brouard 15521: /* codtab gives the value 1 or 2 of the hth combination of k covariates (1 or 2).*/
1.211 brouard 15522: /* nbcode[Tvaraff[j]][codtabm(h,j)]) : if there are only 2 modalities for a covariate j,
15523: * codtabm(h,j) gives its value classified at position h and nbcode gives how it is coded
15524: * (currently 0 or 1) in the data.
15525: * In a loop on h=1 to 2**k, and a loop on j (=1 to k), we get the value of
15526: * corresponding modality (h,j).
15527: */
15528:
1.145 brouard 15529: h=0;
15530: /*if (cptcovn > 0) */
1.126 brouard 15531: m=pow(2,cptcoveff);
15532:
1.144 brouard 15533: /**< codtab(h,k) k = codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) + 1
1.211 brouard 15534: * For k=4 covariates, h goes from 1 to m=2**k
15535: * codtabm(h,k)= (1 & (h-1) >> (k-1)) + 1;
15536: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1.329 brouard 15537: * h\k 1 2 3 4 * h-1\k-1 4 3 2 1
15538: *______________________________ *______________________
15539: * 1 i=1 1 i=1 1 i=1 1 i=1 1 * 0 0 0 0 0
15540: * 2 2 1 1 1 * 1 0 0 0 1
15541: * 3 i=2 1 2 1 1 * 2 0 0 1 0
15542: * 4 2 2 1 1 * 3 0 0 1 1
15543: * 5 i=3 1 i=2 1 2 1 * 4 0 1 0 0
15544: * 6 2 1 2 1 * 5 0 1 0 1
15545: * 7 i=4 1 2 2 1 * 6 0 1 1 0
15546: * 8 2 2 2 1 * 7 0 1 1 1
15547: * 9 i=5 1 i=3 1 i=2 1 2 * 8 1 0 0 0
15548: * 10 2 1 1 2 * 9 1 0 0 1
15549: * 11 i=6 1 2 1 2 * 10 1 0 1 0
15550: * 12 2 2 1 2 * 11 1 0 1 1
15551: * 13 i=7 1 i=4 1 2 2 * 12 1 1 0 0
15552: * 14 2 1 2 2 * 13 1 1 0 1
15553: * 15 i=8 1 2 2 2 * 14 1 1 1 0
15554: * 16 2 2 2 2 * 15 1 1 1 1
15555: */
1.212 brouard 15556: /* How to do the opposite? From combination h (=1 to 2**k) how to get the value on the covariates? */
1.211 brouard 15557: /* from h=5 and m, we get then number of covariates k=log(m)/log(2)=4
15558: * and the value of each covariate?
15559: * V1=1, V2=1, V3=2, V4=1 ?
15560: * h-1=4 and 4 is 0100 or reverse 0010, and +1 is 1121 ok.
15561: * h=6, 6-1=5, 5 is 0101, 1010, 2121, V1=2nd, V2=1st, V3=2nd, V4=1st.
15562: * In order to get the real value in the data, we use nbcode
15563: * nbcode[Tvar[3][2nd]]=1 and nbcode[Tvar[4][1]]=0
15564: * We are keeping this crazy system in order to be able (in the future?)
15565: * to have more than 2 values (0 or 1) for a covariate.
15566: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
15567: * h=6, k=2? h-1=5=0101, reverse 1010, +1=2121, k=2nd position: value is 1: codtabm(6,2)=1
15568: * bbbbbbbb
15569: * 76543210
15570: * h-1 00000101 (6-1=5)
1.219 brouard 15571: *(h-1)>>(k-1)= 00000010 >> (2-1) = 1 right shift
1.211 brouard 15572: * &
15573: * 1 00000001 (1)
1.219 brouard 15574: * 00000000 = 1 & ((h-1) >> (k-1))
15575: * +1= 00000001 =1
1.211 brouard 15576: *
15577: * h=14, k=3 => h'=h-1=13, k'=k-1=2
15578: * h' 1101 =2^3+2^2+0x2^1+2^0
15579: * >>k' 11
15580: * & 00000001
15581: * = 00000001
15582: * +1 = 00000010=2 = codtabm(14,3)
15583: * Reverse h=6 and m=16?
15584: * cptcoveff=log(16)/log(2)=4 covariate: 6-1=5=0101 reversed=1010 +1=2121 =>V1=2, V2=1, V3=2, V4=1.
15585: * for (j=1 to cptcoveff) Vj=decodtabm(j,h,cptcoveff)
15586: * decodtabm(h,j,cptcoveff)= (((h-1) >> (j-1)) & 1) +1
15587: * decodtabm(h,j,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (j-1)) & 1) +1 : -1)
15588: * V3=decodtabm(14,3,2**4)=2
15589: * h'=13 1101 =2^3+2^2+0x2^1+2^0
15590: *(h-1) >> (j-1) 0011 =13 >> 2
15591: * &1 000000001
15592: * = 000000001
15593: * +1= 000000010 =2
15594: * 2211
15595: * V1=1+1, V2=0+1, V3=1+1, V4=1+1
15596: * V3=2
1.220 brouard 15597: * codtabm and decodtabm are identical
1.211 brouard 15598: */
15599:
1.145 brouard 15600:
15601: free_ivector(Ndum,-1,NCOVMAX);
15602:
15603:
1.126 brouard 15604:
1.186 brouard 15605: /* Initialisation of ----------- gnuplot -------------*/
1.126 brouard 15606: strcpy(optionfilegnuplot,optionfilefiname);
15607: if(mle==-3)
1.201 brouard 15608: strcat(optionfilegnuplot,"-MORT_");
1.126 brouard 15609: strcat(optionfilegnuplot,".gp");
15610:
15611: if((ficgp=fopen(optionfilegnuplot,"w"))==NULL) {
15612: printf("Problem with file %s",optionfilegnuplot);
15613: }
15614: else{
1.204 brouard 15615: fprintf(ficgp,"\n# IMaCh-%s\n", version);
1.126 brouard 15616: fprintf(ficgp,"# %s\n", optionfilegnuplot);
1.141 brouard 15617: //fprintf(ficgp,"set missing 'NaNq'\n");
15618: fprintf(ficgp,"set datafile missing 'NaNq'\n");
1.126 brouard 15619: }
15620: /* fclose(ficgp);*/
1.186 brouard 15621:
15622:
15623: /* Initialisation of --------- index.htm --------*/
1.126 brouard 15624:
15625: strcpy(optionfilehtm,optionfilefiname); /* Main html file */
15626: if(mle==-3)
1.201 brouard 15627: strcat(optionfilehtm,"-MORT_");
1.126 brouard 15628: strcat(optionfilehtm,".htm");
15629: if((fichtm=fopen(optionfilehtm,"w"))==NULL) {
1.131 brouard 15630: printf("Problem with %s \n",optionfilehtm);
15631: exit(0);
1.126 brouard 15632: }
15633:
15634: strcpy(optionfilehtmcov,optionfilefiname); /* Only for matrix of covariance */
15635: strcat(optionfilehtmcov,"-cov.htm");
15636: if((fichtmcov=fopen(optionfilehtmcov,"w"))==NULL) {
15637: printf("Problem with %s \n",optionfilehtmcov), exit(0);
15638: }
15639: else{
15640: fprintf(fichtmcov,"<html><head>\n<title>IMaCh Cov %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
15641: <hr size=\"2\" color=\"#EC5E5E\"> \n\
1.204 brouard 15642: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.126 brouard 15643: optionfilehtmcov,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
15644: }
15645:
1.335 brouard 15646: fprintf(fichtm,"<html><head>\n<meta charset=\"utf-8\"/><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n\
15647: <title>IMaCh %s</title></head>\n\
15648: <body><font size=\"7\"><a href=http:/euroreves.ined.fr/imach>IMaCh for Interpolated Markov Chain</a> </font><br>\n\
15649: <font size=\"3\">Sponsored by Copyright (C) 2002-2015 <a href=http://www.ined.fr>INED</a>\
15650: -EUROREVES-Institut de longévité-2013-2022-Japan Society for the Promotion of Sciences 日本学術振興会 \
15651: (<a href=https://www.jsps.go.jp/english/e-grants/>Grant-in-Aid for Scientific Research 25293121</a>) - \
15652: <a href=https://software.intel.com/en-us>Intel Software 2015-2018</a></font><br> \n", optionfilehtm);
15653:
15654: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\"> \n\
1.204 brouard 15655: <font size=\"2\">IMaCh-%s <br> %s</font> \
1.126 brouard 15656: <hr size=\"2\" color=\"#EC5E5E\"> \n\
1.337 brouard 15657: 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\
1.126 brouard 15658: \n\
15659: <hr size=\"2\" color=\"#EC5E5E\">\
15660: <ul><li><h4>Parameter files</h4>\n\
15661: - Parameter file: <a href=\"%s.%s\">%s.%s</a><br>\n\
15662: - Copy of the parameter file: <a href=\"o%s\">o%s</a><br>\n\
15663: - Log file of the run: <a href=\"%s\">%s</a><br>\n\
15664: - Gnuplot file name: <a href=\"%s\">%s</a><br>\n\
15665: - Date and time at start: %s</ul>\n",\
1.335 brouard 15666: version,fullversion,optionfilehtm,optionfilehtm,title,datafile,datafile,firstpass,lastpass,stepm, weightopt, model, \
1.126 brouard 15667: optionfilefiname,optionfilext,optionfilefiname,optionfilext,\
15668: fileres,fileres,\
15669: filelog,filelog,optionfilegnuplot,optionfilegnuplot,strstart);
15670: fflush(fichtm);
15671:
15672: strcpy(pathr,path);
15673: strcat(pathr,optionfilefiname);
1.184 brouard 15674: #ifdef WIN32
15675: _chdir(optionfilefiname); /* Move to directory named optionfile */
15676: #else
1.126 brouard 15677: chdir(optionfilefiname); /* Move to directory named optionfile */
1.184 brouard 15678: #endif
15679:
1.126 brouard 15680:
1.220 brouard 15681: /* Calculates basic frequencies. Computes observed prevalence at single age
15682: and for any valid combination of covariates
1.126 brouard 15683: and prints on file fileres'p'. */
1.359 brouard 15684: freqsummary(fileres, p, pstart, (double)agemin, agemax, s, agev, nlstate, imx, Tvaraff, invalidvarcomb, nbcode, ncodemax,mint,anint,strstart, \
1.227 brouard 15685: firstpass, lastpass, stepm, weightopt, model);
1.126 brouard 15686:
15687: fprintf(fichtm,"\n");
1.286 brouard 15688: 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",\
1.274 brouard 15689: ftol, stepm);
15690: fprintf(fichtm,"\n<li>Number of fixed dummy covariates: ncovcol=%d ", ncovcol);
15691: ncurrv=1;
15692: for(i=ncurrv; i <=ncovcol; i++) fprintf(fichtm,"V%d ", i);
15693: fprintf(fichtm,"\n<li> Number of fixed quantitative variables: nqv=%d ", nqv);
15694: ncurrv=i;
15695: for(i=ncurrv; i <=ncurrv-1+nqv; i++) fprintf(fichtm,"V%d ", i);
1.290 brouard 15696: fprintf(fichtm,"\n<li> Number of time varying (wave varying) dummy covariates: ntv=%d ", ntv);
1.274 brouard 15697: ncurrv=i;
15698: for(i=ncurrv; i <=ncurrv-1+ntv; i++) fprintf(fichtm,"V%d ", i);
1.290 brouard 15699: fprintf(fichtm,"\n<li>Number of time varying quantitative covariates: nqtv=%d ", nqtv);
1.274 brouard 15700: ncurrv=i;
15701: for(i=ncurrv; i <=ncurrv-1+nqtv; i++) fprintf(fichtm,"V%d ", i);
15702: 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", \
15703: nlstate, ndeath, maxwav, mle, weightopt);
15704:
15705: fprintf(fichtm,"<h4> Diagram of states <a href=\"%s_.svg\">%s_.svg</a></h4> \n\
15706: <img src=\"%s_.svg\">", subdirf2(optionfilefiname,"D_"),subdirf2(optionfilefiname,"D_"),subdirf2(optionfilefiname,"D_"));
15707:
15708:
1.317 brouard 15709: fprintf(fichtm,"\n<h4>Some descriptive statistics </h4>\n<br>Number of (used) observations=%d <br>\n\
1.126 brouard 15710: Youngest age at first (selected) pass %.2f, oldest age %.2f<br>\n\
15711: Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n",\
1.274 brouard 15712: imx,agemin,agemax,jmin,jmax,jmean);
1.126 brouard 15713: pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
1.268 brouard 15714: oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
15715: newms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
15716: savms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
15717: oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */
1.218 brouard 15718:
1.126 brouard 15719: /* For Powell, parameters are in a vector p[] starting at p[1]
15720: so we point p on param[1][1] so that p[1] maps on param[1][1][1] */
15721: p=param[1][1]; /* *(*(*(param +1)+1)+0) */
15722:
15723: globpr=0; /* To get the number ipmx of contributions and the sum of weights*/
1.186 brouard 15724: /* For mortality only */
1.126 brouard 15725: if (mle==-3){
1.136 brouard 15726: ximort=matrix(1,NDIM,1,NDIM);
1.248 brouard 15727: for(i=1;i<=NDIM;i++)
15728: for(j=1;j<=NDIM;j++)
15729: ximort[i][j]=0.;
1.186 brouard 15730: /* ximort=gsl_matrix_alloc(1,NDIM,1,NDIM); */
1.290 brouard 15731: cens=ivector(firstobs,lastobs);
15732: ageexmed=vector(firstobs,lastobs);
15733: agecens=vector(firstobs,lastobs);
15734: dcwave=ivector(firstobs,lastobs);
1.223 brouard 15735:
1.126 brouard 15736: for (i=1; i<=imx; i++){
15737: dcwave[i]=-1;
15738: for (m=firstpass; m<=lastpass; m++)
1.226 brouard 15739: if (s[m][i]>nlstate) {
15740: dcwave[i]=m;
15741: /* printf("i=%d j=%d s=%d dcwave=%d\n",i,j, s[j][i],dcwave[i]);*/
15742: break;
15743: }
1.126 brouard 15744: }
1.226 brouard 15745:
1.126 brouard 15746: for (i=1; i<=imx; i++) {
15747: if (wav[i]>0){
1.226 brouard 15748: ageexmed[i]=agev[mw[1][i]][i];
15749: j=wav[i];
15750: agecens[i]=1.;
15751:
15752: if (ageexmed[i]> 1 && wav[i] > 0){
15753: agecens[i]=agev[mw[j][i]][i];
15754: cens[i]= 1;
15755: }else if (ageexmed[i]< 1)
15756: cens[i]= -1;
15757: if (agedc[i]< AGESUP && agedc[i]>1 && dcwave[i]>firstpass && dcwave[i]<=lastpass)
15758: cens[i]=0 ;
1.126 brouard 15759: }
15760: else cens[i]=-1;
15761: }
15762:
15763: for (i=1;i<=NDIM;i++) {
15764: for (j=1;j<=NDIM;j++)
1.226 brouard 15765: ximort[i][j]=(i == j ? 1.0 : 0.0);
1.126 brouard 15766: }
15767:
1.302 brouard 15768: p[1]=0.0268; p[NDIM]=0.083;
15769: /* printf("%lf %lf", p[1], p[2]); */
1.126 brouard 15770:
15771:
1.136 brouard 15772: #ifdef GSL
15773: printf("GSL optimization\n"); fprintf(ficlog,"Powell\n");
1.162 brouard 15774: #else
1.359 brouard 15775: printf("Powell-mort\n"); fprintf(ficlog,"Powell-mort\n");
1.136 brouard 15776: #endif
1.201 brouard 15777: strcpy(filerespow,"POW-MORT_");
15778: strcat(filerespow,fileresu);
1.126 brouard 15779: if((ficrespow=fopen(filerespow,"w"))==NULL) {
15780: printf("Problem with resultfile: %s\n", filerespow);
15781: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
15782: }
1.136 brouard 15783: #ifdef GSL
15784: fprintf(ficrespow,"# GSL optimization\n# iter -2*LL");
1.162 brouard 15785: #else
1.126 brouard 15786: fprintf(ficrespow,"# Powell\n# iter -2*LL");
1.136 brouard 15787: #endif
1.126 brouard 15788: /* for (i=1;i<=nlstate;i++)
15789: for(j=1;j<=nlstate+ndeath;j++)
15790: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
15791: */
15792: fprintf(ficrespow,"\n");
1.136 brouard 15793: #ifdef GSL
15794: /* gsl starts here */
15795: T = gsl_multimin_fminimizer_nmsimplex;
15796: gsl_multimin_fminimizer *sfm = NULL;
15797: gsl_vector *ss, *x;
15798: gsl_multimin_function minex_func;
15799:
15800: /* Initial vertex size vector */
15801: ss = gsl_vector_alloc (NDIM);
15802:
15803: if (ss == NULL){
15804: GSL_ERROR_VAL ("failed to allocate space for ss", GSL_ENOMEM, 0);
15805: }
15806: /* Set all step sizes to 1 */
15807: gsl_vector_set_all (ss, 0.001);
15808:
15809: /* Starting point */
1.126 brouard 15810:
1.136 brouard 15811: x = gsl_vector_alloc (NDIM);
15812:
15813: if (x == NULL){
15814: gsl_vector_free(ss);
15815: GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0);
15816: }
15817:
15818: /* Initialize method and iterate */
15819: /* p[1]=0.0268; p[NDIM]=0.083; */
1.186 brouard 15820: /* gsl_vector_set(x, 0, 0.0268); */
15821: /* gsl_vector_set(x, 1, 0.083); */
1.136 brouard 15822: gsl_vector_set(x, 0, p[1]);
15823: gsl_vector_set(x, 1, p[2]);
15824:
15825: minex_func.f = &gompertz_f;
15826: minex_func.n = NDIM;
15827: minex_func.params = (void *)&p; /* ??? */
15828:
15829: sfm = gsl_multimin_fminimizer_alloc (T, NDIM);
15830: gsl_multimin_fminimizer_set (sfm, &minex_func, x, ss);
15831:
15832: printf("Iterations beginning .....\n\n");
15833: printf("Iter. # Intercept Slope -Log Likelihood Simplex size\n");
15834:
15835: iteri=0;
15836: while (rval == GSL_CONTINUE){
15837: iteri++;
15838: status = gsl_multimin_fminimizer_iterate(sfm);
15839:
15840: if (status) printf("error: %s\n", gsl_strerror (status));
15841: fflush(0);
15842:
15843: if (status)
15844: break;
15845:
15846: rval = gsl_multimin_test_size (gsl_multimin_fminimizer_size (sfm), 1e-6);
15847: ssval = gsl_multimin_fminimizer_size (sfm);
15848:
15849: if (rval == GSL_SUCCESS)
15850: printf ("converged to a local maximum at\n");
15851:
15852: printf("%5d ", iteri);
15853: for (it = 0; it < NDIM; it++){
15854: printf ("%10.5f ", gsl_vector_get (sfm->x, it));
15855: }
15856: printf("f() = %-10.5f ssize = %.7f\n", sfm->fval, ssval);
15857: }
15858:
15859: printf("\n\n Please note: Program should be run many times with varying starting points to detemine global maximum\n\n");
15860:
15861: gsl_vector_free(x); /* initial values */
15862: gsl_vector_free(ss); /* inital step size */
15863: for (it=0; it<NDIM; it++){
15864: p[it+1]=gsl_vector_get(sfm->x,it);
15865: fprintf(ficrespow," %.12lf", p[it]);
15866: }
15867: gsl_multimin_fminimizer_free (sfm); /* p *(sfm.x.data) et p *(sfm.x.data+1) */
15868: #endif
15869: #ifdef POWELL
1.361 brouard 15870: #ifdef LINMINORIGINAL
15871: #else /* LINMINORIGINAL */
15872:
15873: flatdir=ivector(1,npar);
15874: for (j=1;j<=npar;j++) flatdir[j]=0;
15875: #endif /*LINMINORIGINAL */
1.362 brouard 15876: /* powell(p,ximort,NDIM,ftol,&iter,&fret,gompertz); */
15877: /* double h0=0.25; */
15878: macheps=pow(16.0,-13.0);
15879: printf("Praxis Gegenfurtner mle=%d\n",mle);
15880: fprintf(ficlog, "Praxis Gegenfurtner mle=%d\n", mle);fflush(ficlog);
15881: /* ffmin = praxis(ftol,macheps, h0, npar, prin, p, gompertz); */
15882: /* For the Gompertz we use only two parameters */
15883: int _npar=2;
15884: ffmin = praxis(ftol,macheps, h0, _npar, 4, p, gompertz);
15885: printf("End Praxis\n");
1.126 brouard 15886: fclose(ficrespow);
1.361 brouard 15887: #ifdef LINMINORIGINAL
15888: #else
15889: free_ivector(flatdir,1,npar);
15890: #endif /* LINMINORIGINAL*/
1.364 brouard 15891: #endif /* POWELL */
1.203 brouard 15892: hesscov(matcov, hess, p, NDIM, delti, 1e-4, gompertz);
1.126 brouard 15893:
15894: for(i=1; i <=NDIM; i++)
15895: for(j=i+1;j<=NDIM;j++)
1.359 brouard 15896: matcov[i][j]=matcov[j][i];
1.126 brouard 15897:
15898: printf("\nCovariance matrix\n ");
1.203 brouard 15899: fprintf(ficlog,"\nCovariance matrix\n ");
1.126 brouard 15900: for(i=1; i <=NDIM; i++) {
15901: for(j=1;j<=NDIM;j++){
1.220 brouard 15902: printf("%f ",matcov[i][j]);
15903: fprintf(ficlog,"%f ",matcov[i][j]);
1.126 brouard 15904: }
1.203 brouard 15905: printf("\n "); fprintf(ficlog,"\n ");
1.126 brouard 15906: }
15907:
15908: printf("iter=%d MLE=%f Eq=%lf*exp(%lf*(age-%d))\n",iter,-gompertz(p),p[1],p[2],agegomp);
1.193 brouard 15909: for (i=1;i<=NDIM;i++) {
1.126 brouard 15910: printf("%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
1.193 brouard 15911: fprintf(ficlog,"%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
15912: }
1.302 brouard 15913: lsurv=vector(agegomp,AGESUP);
15914: lpop=vector(agegomp,AGESUP);
15915: tpop=vector(agegomp,AGESUP);
1.126 brouard 15916: lsurv[agegomp]=100000;
15917:
15918: for (k=agegomp;k<=AGESUP;k++) {
15919: agemortsup=k;
15920: if (p[1]*exp(p[2]*(k-agegomp))>1) break;
15921: }
15922:
15923: for (k=agegomp;k<agemortsup;k++)
15924: lsurv[k+1]=lsurv[k]-lsurv[k]*(p[1]*exp(p[2]*(k-agegomp)));
15925:
15926: for (k=agegomp;k<agemortsup;k++){
15927: lpop[k]=(lsurv[k]+lsurv[k+1])/2.;
15928: sumlpop=sumlpop+lpop[k];
15929: }
15930:
15931: tpop[agegomp]=sumlpop;
15932: for (k=agegomp;k<(agemortsup-3);k++){
15933: /* tpop[k+1]=2;*/
15934: tpop[k+1]=tpop[k]-lpop[k];
15935: }
15936:
15937:
15938: printf("\nAge lx qx dx Lx Tx e(x)\n");
15939: for (k=agegomp;k<(agemortsup-2);k++)
15940: 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]);
15941:
15942:
15943: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
1.220 brouard 15944: ageminpar=50;
15945: agemaxpar=100;
1.194 brouard 15946: if(ageminpar == AGEOVERFLOW ||agemaxpar == AGEOVERFLOW){
15947: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
15948: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
15949: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
15950: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
15951: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
15952: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.220 brouard 15953: }else{
15954: printf("Warning! ageminpar %f and agemaxpar %f have been fixed because for simplification until it is fixed...\n\n",ageminpar,agemaxpar);
15955: fprintf(ficlog,"Warning! ageminpar %f and agemaxpar %f have been fixed because for simplification until it is fixed...\n\n",ageminpar,agemaxpar);
1.201 brouard 15956: printinggnuplotmort(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, pathc,p);
1.220 brouard 15957: }
1.201 brouard 15958: printinghtmlmort(fileresu,title,datafile, firstpass, lastpass, \
1.126 brouard 15959: stepm, weightopt,\
15960: model,imx,p,matcov,agemortsup);
15961:
1.302 brouard 15962: free_vector(lsurv,agegomp,AGESUP);
15963: free_vector(lpop,agegomp,AGESUP);
15964: free_vector(tpop,agegomp,AGESUP);
1.220 brouard 15965: free_matrix(ximort,1,NDIM,1,NDIM);
1.290 brouard 15966: free_ivector(dcwave,firstobs,lastobs);
15967: free_vector(agecens,firstobs,lastobs);
15968: free_vector(ageexmed,firstobs,lastobs);
15969: free_ivector(cens,firstobs,lastobs);
1.220 brouard 15970: #ifdef GSL
1.136 brouard 15971: #endif
1.186 brouard 15972: } /* Endof if mle==-3 mortality only */
1.205 brouard 15973: /* Standard */
15974: else{ /* For mle !=- 3, could be 0 or 1 or 4 etc. */
15975: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
15976: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
1.132 brouard 15977: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
1.126 brouard 15978: printf("First Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
15979: for (k=1; k<=npar;k++)
15980: printf(" %d %8.5f",k,p[k]);
15981: printf("\n");
1.205 brouard 15982: if(mle>=1){ /* Could be 1 or 2, Real Maximization */
15983: /* mlikeli uses func not funcone */
1.247 brouard 15984: /* for(i=1;i<nlstate;i++){ */
15985: /* /\*reducing xi for 1 to npar to 1 to ncovmodel; *\/ */
15986: /* mlikeli(ficres,p, ncovmodel, ncovmodel, nlstate, ftol, funcnoprod); */
15987: /* } */
1.205 brouard 15988: mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func);
15989: }
15990: if(mle==0) {/* No optimization, will print the likelihoods for the datafile */
15991: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
15992: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
15993: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
15994: }
15995: globpr=1; /* again, to print the individual contributions using computed gpimx and gsw */
1.126 brouard 15996: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
15997: printf("Second Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
1.335 brouard 15998: /* exit(0); */
1.126 brouard 15999: for (k=1; k<=npar;k++)
16000: printf(" %d %8.5f",k,p[k]);
16001: printf("\n");
16002:
16003: /*--------- results files --------------*/
1.283 brouard 16004: /* fprintf(ficres,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nqv=%d ntv=%d nqtv=%d nlstate=%d ndeath=%d maxwav=%d mle= 0 weight=%d\nmodel=1+age+%s.\n", title, datafile, lastobs, firstpass,lastpass,ftol, stepm, ncovcol, nqv, ntv, nqtv, nlstate, ndeath, maxwav, weightopt,model); */
1.126 brouard 16005:
16006:
16007: fprintf(ficres,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
1.319 brouard 16008: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n"); /* Printing model equation */
1.126 brouard 16009: fprintf(ficlog,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
1.319 brouard 16010:
16011: printf("#model= 1 + age ");
16012: fprintf(ficres,"#model= 1 + age ");
16013: fprintf(ficlog,"#model= 1 + age ");
16014: fprintf(fichtm,"\n<ul><li> model=1+age+%s\n \
16015: </ul>", model);
16016:
16017: fprintf(fichtm,"\n<table style=\"text-align:center; border: 1px solid\">\n");
16018: fprintf(fichtm, "<tr><th>Model=</th><th>1</th><th>+ age</th>");
16019: if(nagesqr==1){
16020: printf(" + age*age ");
16021: fprintf(ficres," + age*age ");
16022: fprintf(ficlog," + age*age ");
16023: fprintf(fichtm, "<th>+ age*age</th>");
16024: }
1.362 brouard 16025: for(j=1;j <=ncovmodel-2-nagesqr;j++){
1.319 brouard 16026: if(Typevar[j]==0) {
16027: printf(" + V%d ",Tvar[j]);
16028: fprintf(ficres," + V%d ",Tvar[j]);
16029: fprintf(ficlog," + V%d ",Tvar[j]);
16030: fprintf(fichtm, "<th>+ V%d</th>",Tvar[j]);
16031: }else if(Typevar[j]==1) {
16032: printf(" + V%d*age ",Tvar[j]);
16033: fprintf(ficres," + V%d*age ",Tvar[j]);
16034: fprintf(ficlog," + V%d*age ",Tvar[j]);
16035: fprintf(fichtm, "<th>+ V%d*age</th>",Tvar[j]);
16036: }else if(Typevar[j]==2) {
16037: printf(" + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
16038: fprintf(ficres," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
16039: fprintf(ficlog," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
16040: fprintf(fichtm, "<th>+ V%d*V%d</th>",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
1.349 brouard 16041: }else if(Typevar[j]==3) { /* TO VERIFY */
16042: printf(" + V%d*V%d*age ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
16043: fprintf(ficres," + V%d*V%d*age ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
16044: fprintf(ficlog," + V%d*V%d*age ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
16045: fprintf(fichtm, "<th>+ V%d*V%d*age</th>",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
1.319 brouard 16046: }
16047: }
16048: printf("\n");
16049: fprintf(ficres,"\n");
16050: fprintf(ficlog,"\n");
16051: fprintf(fichtm, "</tr>");
16052: fprintf(fichtm, "\n");
16053:
16054:
1.126 brouard 16055: for(i=1,jk=1; i <=nlstate; i++){
16056: for(k=1; k <=(nlstate+ndeath); k++){
1.225 brouard 16057: if (k != i) {
1.319 brouard 16058: fprintf(fichtm, "<tr>");
1.225 brouard 16059: printf("%d%d ",i,k);
16060: fprintf(ficlog,"%d%d ",i,k);
16061: fprintf(ficres,"%1d%1d ",i,k);
1.319 brouard 16062: fprintf(fichtm, "<td>%1d%1d</td>",i,k);
1.225 brouard 16063: for(j=1; j <=ncovmodel; j++){
16064: printf("%12.7f ",p[jk]);
16065: fprintf(ficlog,"%12.7f ",p[jk]);
16066: fprintf(ficres,"%12.7f ",p[jk]);
1.319 brouard 16067: fprintf(fichtm, "<td>%12.7f</td>",p[jk]);
1.225 brouard 16068: jk++;
16069: }
16070: printf("\n");
16071: fprintf(ficlog,"\n");
16072: fprintf(ficres,"\n");
1.319 brouard 16073: fprintf(fichtm, "</tr>\n");
1.225 brouard 16074: }
1.126 brouard 16075: }
16076: }
1.319 brouard 16077: /* fprintf(fichtm,"</tr>\n"); */
16078: fprintf(fichtm,"</table>\n");
16079: fprintf(fichtm, "\n");
16080:
1.203 brouard 16081: if(mle != 0){
16082: /* Computing hessian and covariance matrix only at a peak of the Likelihood, that is after optimization */
1.126 brouard 16083: ftolhess=ftol; /* Usually correct */
1.203 brouard 16084: hesscov(matcov, hess, p, npar, delti, ftolhess, func);
16085: 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");
16086: 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");
1.322 brouard 16087: 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);
1.319 brouard 16088: fprintf(fichtm,"\n<table style=\"text-align:center; border: 1px solid\">");
16089: fprintf(fichtm, "\n<tr><th>Model=</th><th>1</th><th>+ age</th>");
16090: if(nagesqr==1){
16091: printf(" + age*age ");
16092: fprintf(ficres," + age*age ");
16093: fprintf(ficlog," + age*age ");
16094: fprintf(fichtm, "<th>+ age*age</th>");
16095: }
1.362 brouard 16096: for(j=1;j <=ncovmodel-2-nagesqr;j++){
1.319 brouard 16097: if(Typevar[j]==0) {
16098: printf(" + V%d ",Tvar[j]);
16099: fprintf(fichtm, "<th>+ V%d</th>",Tvar[j]);
16100: }else if(Typevar[j]==1) {
16101: printf(" + V%d*age ",Tvar[j]);
16102: fprintf(fichtm, "<th>+ V%d*age</th>",Tvar[j]);
16103: }else if(Typevar[j]==2) {
16104: fprintf(fichtm, "<th>+ V%d*V%d</th>",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
1.349 brouard 16105: }else if(Typevar[j]==3) { /* TO VERIFY */
16106: fprintf(fichtm, "<th>+ V%d*V%d*age</th>",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
1.319 brouard 16107: }
16108: }
16109: fprintf(fichtm, "</tr>\n");
16110:
1.203 brouard 16111: for(i=1,jk=1; i <=nlstate; i++){
1.225 brouard 16112: for(k=1; k <=(nlstate+ndeath); k++){
16113: if (k != i) {
1.319 brouard 16114: fprintf(fichtm, "<tr valign=top>");
1.225 brouard 16115: printf("%d%d ",i,k);
16116: fprintf(ficlog,"%d%d ",i,k);
1.319 brouard 16117: fprintf(fichtm, "<td>%1d%1d</td>",i,k);
1.225 brouard 16118: for(j=1; j <=ncovmodel; j++){
1.319 brouard 16119: wald=p[jk]/sqrt(matcov[jk][jk]);
1.324 brouard 16120: 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]));
16121: 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]));
1.319 brouard 16122: if(fabs(wald) > 1.96){
1.321 brouard 16123: fprintf(fichtm, "<td><b>%12.7f</b></br> (%12.7f)</br>",p[jk],sqrt(matcov[jk][jk]));
1.319 brouard 16124: }else{
16125: fprintf(fichtm, "<td>%12.7f (%12.7f)</br>",p[jk],sqrt(matcov[jk][jk]));
16126: }
1.324 brouard 16127: fprintf(fichtm,"W=%8.3f</br>",wald);
1.319 brouard 16128: fprintf(fichtm,"[%12.7f;%12.7f]</br></td>", p[jk]-1.96*sqrt(matcov[jk][jk]),p[jk]+1.96*sqrt(matcov[jk][jk]));
1.225 brouard 16129: jk++;
16130: }
16131: printf("\n");
16132: fprintf(ficlog,"\n");
1.319 brouard 16133: fprintf(fichtm, "</tr>\n");
1.225 brouard 16134: }
16135: }
1.193 brouard 16136: }
1.203 brouard 16137: } /* end of hesscov and Wald tests */
1.319 brouard 16138: fprintf(fichtm,"</table>\n");
1.225 brouard 16139:
1.203 brouard 16140: /* */
1.126 brouard 16141: fprintf(ficres,"# Scales (for hessian or gradient estimation)\n");
16142: printf("# Scales (for hessian or gradient estimation)\n");
16143: fprintf(ficlog,"# Scales (for hessian or gradient estimation)\n");
16144: for(i=1,jk=1; i <=nlstate; i++){
16145: for(j=1; j <=nlstate+ndeath; j++){
1.225 brouard 16146: if (j!=i) {
16147: fprintf(ficres,"%1d%1d",i,j);
16148: printf("%1d%1d",i,j);
16149: fprintf(ficlog,"%1d%1d",i,j);
16150: for(k=1; k<=ncovmodel;k++){
16151: printf(" %.5e",delti[jk]);
16152: fprintf(ficlog," %.5e",delti[jk]);
16153: fprintf(ficres," %.5e",delti[jk]);
16154: jk++;
16155: }
16156: printf("\n");
16157: fprintf(ficlog,"\n");
16158: fprintf(ficres,"\n");
16159: }
1.126 brouard 16160: }
16161: }
16162:
16163: fprintf(ficres,"# Covariance matrix \n# 121 Var(a12)\n# 122 Cov(b12,a12) Var(b12)\n# ...\n# 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n");
1.349 brouard 16164: if(mle >= 1) /* Too big for the screen */
1.126 brouard 16165: 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");
16166: 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");
16167: /* # 121 Var(a12)\n\ */
16168: /* # 122 Cov(b12,a12) Var(b12)\n\ */
16169: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
16170: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
16171: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
16172: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
16173: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
16174: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
16175:
16176:
16177: /* Just to have a covariance matrix which will be more understandable
16178: even is we still don't want to manage dictionary of variables
16179: */
16180: for(itimes=1;itimes<=2;itimes++){
16181: jj=0;
16182: for(i=1; i <=nlstate; i++){
1.225 brouard 16183: for(j=1; j <=nlstate+ndeath; j++){
16184: if(j==i) continue;
16185: for(k=1; k<=ncovmodel;k++){
16186: jj++;
16187: ca[0]= k+'a'-1;ca[1]='\0';
16188: if(itimes==1){
16189: if(mle>=1)
16190: printf("#%1d%1d%d",i,j,k);
16191: fprintf(ficlog,"#%1d%1d%d",i,j,k);
16192: fprintf(ficres,"#%1d%1d%d",i,j,k);
16193: }else{
16194: if(mle>=1)
16195: printf("%1d%1d%d",i,j,k);
16196: fprintf(ficlog,"%1d%1d%d",i,j,k);
16197: fprintf(ficres,"%1d%1d%d",i,j,k);
16198: }
16199: ll=0;
16200: for(li=1;li <=nlstate; li++){
16201: for(lj=1;lj <=nlstate+ndeath; lj++){
16202: if(lj==li) continue;
16203: for(lk=1;lk<=ncovmodel;lk++){
16204: ll++;
16205: if(ll<=jj){
16206: cb[0]= lk +'a'-1;cb[1]='\0';
16207: if(ll<jj){
16208: if(itimes==1){
16209: if(mle>=1)
16210: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
16211: fprintf(ficlog," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
16212: fprintf(ficres," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
16213: }else{
16214: if(mle>=1)
16215: printf(" %.5e",matcov[jj][ll]);
16216: fprintf(ficlog," %.5e",matcov[jj][ll]);
16217: fprintf(ficres," %.5e",matcov[jj][ll]);
16218: }
16219: }else{
16220: if(itimes==1){
16221: if(mle>=1)
16222: printf(" Var(%s%1d%1d)",ca,i,j);
16223: fprintf(ficlog," Var(%s%1d%1d)",ca,i,j);
16224: fprintf(ficres," Var(%s%1d%1d)",ca,i,j);
16225: }else{
16226: if(mle>=1)
16227: printf(" %.7e",matcov[jj][ll]);
16228: fprintf(ficlog," %.7e",matcov[jj][ll]);
16229: fprintf(ficres," %.7e",matcov[jj][ll]);
16230: }
16231: }
16232: }
16233: } /* end lk */
16234: } /* end lj */
16235: } /* end li */
16236: if(mle>=1)
16237: printf("\n");
16238: fprintf(ficlog,"\n");
16239: fprintf(ficres,"\n");
16240: numlinepar++;
16241: } /* end k*/
16242: } /*end j */
1.126 brouard 16243: } /* end i */
16244: } /* end itimes */
16245:
16246: fflush(ficlog);
16247: fflush(ficres);
1.225 brouard 16248: while(fgets(line, MAXLINE, ficpar)) {
16249: /* If line starts with a # it is a comment */
16250: if (line[0] == '#') {
16251: numlinepar++;
16252: fputs(line,stdout);
16253: fputs(line,ficparo);
16254: fputs(line,ficlog);
1.299 brouard 16255: fputs(line,ficres);
1.225 brouard 16256: continue;
16257: }else
16258: break;
16259: }
16260:
1.209 brouard 16261: /* while((c=getc(ficpar))=='#' && c!= EOF){ */
16262: /* ungetc(c,ficpar); */
16263: /* fgets(line, MAXLINE, ficpar); */
16264: /* fputs(line,stdout); */
16265: /* fputs(line,ficparo); */
16266: /* } */
16267: /* ungetc(c,ficpar); */
1.126 brouard 16268:
16269: estepm=0;
1.209 brouard 16270: if((num_filled=sscanf(line,"agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%lf\n",&ageminpar,&agemaxpar, &bage, &fage, &estepm, &ftolpl)) !=EOF){
1.225 brouard 16271:
16272: if (num_filled != 6) {
16273: 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);
16274: 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);
16275: goto end;
16276: }
16277: printf("agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%lf\n",ageminpar,agemaxpar, bage, fage, estepm, ftolpl);
16278: }
16279: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
16280: /*ftolpl=6.e-4;*/ /* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
16281:
1.209 brouard 16282: /* fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%\n",&ageminpar,&agemaxpar, &bage, &fage, &estepm); */
1.126 brouard 16283: if (estepm==0 || estepm < stepm) estepm=stepm;
16284: if (fage <= 2) {
16285: bage = ageminpar;
16286: fage = agemaxpar;
16287: }
16288:
16289: fprintf(ficres,"# agemin agemax for life expectancy, bage fage (if mle==0 ie no data nor Max likelihood).\n");
1.211 brouard 16290: fprintf(ficres,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
16291: fprintf(ficparo,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d, ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
1.220 brouard 16292:
1.186 brouard 16293: /* Other stuffs, more or less useful */
1.254 brouard 16294: while(fgets(line, MAXLINE, ficpar)) {
16295: /* If line starts with a # it is a comment */
16296: if (line[0] == '#') {
16297: numlinepar++;
16298: fputs(line,stdout);
16299: fputs(line,ficparo);
16300: fputs(line,ficlog);
1.299 brouard 16301: fputs(line,ficres);
1.254 brouard 16302: continue;
16303: }else
16304: break;
16305: }
16306:
16307: 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){
16308:
16309: if (num_filled != 7) {
16310: 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);
16311: 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);
16312: goto end;
16313: }
16314: printf("begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mov_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
16315: 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);
16316: 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);
16317: 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);
1.126 brouard 16318: }
1.254 brouard 16319:
16320: while(fgets(line, MAXLINE, ficpar)) {
16321: /* If line starts with a # it is a comment */
16322: if (line[0] == '#') {
16323: numlinepar++;
16324: fputs(line,stdout);
16325: fputs(line,ficparo);
16326: fputs(line,ficlog);
1.299 brouard 16327: fputs(line,ficres);
1.254 brouard 16328: continue;
16329: }else
16330: break;
1.126 brouard 16331: }
16332:
16333:
16334: dateprev1=anprev1+(mprev1-1)/12.+(jprev1-1)/365.;
16335: dateprev2=anprev2+(mprev2-1)/12.+(jprev2-1)/365.;
16336:
1.254 brouard 16337: if((num_filled=sscanf(line,"pop_based=%d\n",&popbased)) !=EOF){
16338: if (num_filled != 1) {
16339: 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);
16340: 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);
16341: goto end;
16342: }
16343: printf("pop_based=%d\n",popbased);
16344: fprintf(ficlog,"pop_based=%d\n",popbased);
16345: fprintf(ficparo,"pop_based=%d\n",popbased);
16346: fprintf(ficres,"pop_based=%d\n",popbased);
16347: }
16348:
1.258 brouard 16349: /* Results */
1.359 brouard 16350: /* Value of covariate in each resultine will be computed (if product) and sorted according to model rank */
1.332 brouard 16351: /* It is precov[] because we need the varying age in order to compute the real cov[] of the model equation */
16352: precov=matrix(1,MAXRESULTLINESPONE,1,NCOVMAX+1);
1.307 brouard 16353: endishere=0;
1.258 brouard 16354: nresult=0;
1.308 brouard 16355: parameterline=0;
1.258 brouard 16356: do{
16357: if(!fgets(line, MAXLINE, ficpar)){
16358: endishere=1;
1.308 brouard 16359: parameterline=15;
1.258 brouard 16360: }else if (line[0] == '#') {
16361: /* If line starts with a # it is a comment */
1.254 brouard 16362: numlinepar++;
16363: fputs(line,stdout);
16364: fputs(line,ficparo);
16365: fputs(line,ficlog);
1.299 brouard 16366: fputs(line,ficres);
1.254 brouard 16367: continue;
1.258 brouard 16368: }else if(sscanf(line,"prevforecast=%[^\n]\n",modeltemp))
16369: parameterline=11;
1.296 brouard 16370: else if(sscanf(line,"prevbackcast=%[^\n]\n",modeltemp))
1.258 brouard 16371: parameterline=12;
1.307 brouard 16372: else if(sscanf(line,"result:%[^\n]\n",modeltemp)){
1.258 brouard 16373: parameterline=13;
1.307 brouard 16374: }
1.258 brouard 16375: else{
16376: parameterline=14;
1.254 brouard 16377: }
1.308 brouard 16378: switch (parameterline){ /* =0 only if only comments */
1.258 brouard 16379: case 11:
1.296 brouard 16380: 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)){
16381: 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);
1.258 brouard 16382: 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);
16383: 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);
16384: 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);
16385: /* day and month of proj2 are not used but only year anproj2.*/
1.273 brouard 16386: dateproj1=anproj1+(mproj1-1)/12.+(jproj1-1)/365.;
16387: dateproj2=anproj2+(mproj2-1)/12.+(jproj2-1)/365.;
1.296 brouard 16388: prvforecast = 1;
16389: }
16390: else if((num_filled=sscanf(line,"prevforecast=%d yearsfproj=%lf mobil_average=%d\n",&prevfcast,&yrfproj,&mobilavproj)) !=EOF){/* && (num_filled == 3))*/
1.313 brouard 16391: printf("prevforecast=%d yearsfproj=%.2lf mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
16392: fprintf(ficlog,"prevforecast=%d yearsfproj=%.2lf mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
16393: fprintf(ficres,"prevforecast=%d yearsfproj=%.2lf mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
1.296 brouard 16394: prvforecast = 2;
16395: }
16396: else {
16397: 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);
16398: 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);
16399: goto end;
1.258 brouard 16400: }
1.254 brouard 16401: break;
1.258 brouard 16402: case 12:
1.296 brouard 16403: 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)){
16404: 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);
16405: 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);
16406: 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);
16407: 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);
16408: /* day and month of back2 are not used but only year anback2.*/
1.273 brouard 16409: dateback1=anback1+(mback1-1)/12.+(jback1-1)/365.;
16410: dateback2=anback2+(mback2-1)/12.+(jback2-1)/365.;
1.296 brouard 16411: prvbackcast = 1;
16412: }
16413: else if((num_filled=sscanf(line,"prevbackcast=%d yearsbproj=%lf mobil_average=%d\n",&prevbcast,&yrbproj,&mobilavproj)) ==3){/* && (num_filled == 3))*/
1.313 brouard 16414: printf("prevbackcast=%d yearsbproj=%.2lf mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
16415: fprintf(ficlog,"prevbackcast=%d yearsbproj=%.2lf mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
16416: fprintf(ficres,"prevbackcast=%d yearsbproj=%.2lf mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
1.296 brouard 16417: prvbackcast = 2;
16418: }
16419: else {
16420: 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);
16421: 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);
16422: goto end;
1.258 brouard 16423: }
1.230 brouard 16424: break;
1.258 brouard 16425: case 13:
1.332 brouard 16426: num_filled=sscanf(line,"result:%[^\n]\n",resultlineori);
1.307 brouard 16427: nresult++; /* Sum of resultlines */
1.342 brouard 16428: /* printf("Result %d: result:%s\n",nresult, resultlineori); */
1.332 brouard 16429: /* removefirstspace(&resultlineori); */
16430:
16431: if(strstr(resultlineori,"v") !=0){
16432: printf("Error. 'v' must be in upper case 'V' result: %s ",resultlineori);
16433: fprintf(ficlog,"Error. 'v' must be in upper case result: %s ",resultlineori);fflush(ficlog);
16434: return 1;
16435: }
16436: trimbb(resultline, resultlineori); /* Suppressing double blank in the resultline */
1.342 brouard 16437: /* printf("Decoderesult resultline=\"%s\" resultlineori=\"%s\"\n", resultline, resultlineori); */
1.318 brouard 16438: if(nresult > MAXRESULTLINESPONE-1){
16439: 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);
16440: 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);
1.307 brouard 16441: goto end;
16442: }
1.332 brouard 16443:
1.310 brouard 16444: if(!decoderesult(resultline, nresult)){ /* Fills TKresult[nresult] combination and Tresult[nresult][k4+1] combination values */
1.314 brouard 16445: fprintf(ficparo,"result: %s\n",resultline);
16446: fprintf(ficres,"result: %s\n",resultline);
16447: fprintf(ficlog,"result: %s\n",resultline);
1.310 brouard 16448: } else
16449: goto end;
1.307 brouard 16450: break;
16451: case 14:
16452: printf("Error: Unknown command '%s'\n",line);
16453: fprintf(ficlog,"Error: Unknown command '%s'\n",line);
1.314 brouard 16454: if(line[0] == ' ' || line[0] == '\n'){
16455: printf("It should not be an empty line '%s'\n",line);
16456: fprintf(ficlog,"It should not be an empty line '%s'\n",line);
16457: }
1.307 brouard 16458: if(ncovmodel >=2 && nresult==0 ){
16459: printf("ERROR: no result lines! It should be at minimum 'result: V2=0 V1=1 or result:.' %s\n",line);
16460: fprintf(ficlog,"ERROR: no result lines! It should be at minimum 'result: V2=0 V1=1 or result:.' %s\n",line);
1.258 brouard 16461: }
1.307 brouard 16462: /* goto end; */
16463: break;
1.308 brouard 16464: case 15:
16465: printf("End of resultlines.\n");
16466: fprintf(ficlog,"End of resultlines.\n");
16467: break;
16468: default: /* parameterline =0 */
1.307 brouard 16469: nresult=1;
16470: decoderesult(".",nresult ); /* No covariate */
1.258 brouard 16471: } /* End switch parameterline */
16472: }while(endishere==0); /* End do */
1.126 brouard 16473:
1.230 brouard 16474: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint); */
1.145 brouard 16475: /* ,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2); */
1.126 brouard 16476:
16477: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
1.194 brouard 16478: if(ageminpar == AGEOVERFLOW ||agemaxpar == -AGEOVERFLOW){
1.230 brouard 16479: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
1.194 brouard 16480: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
16481: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.230 brouard 16482: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
1.194 brouard 16483: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
16484: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.220 brouard 16485: }else{
1.270 brouard 16486: /* printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, prevfcast, backcast, pathc,p, (int)anproj1-(int)agemin, (int)anback1-(int)agemax+1); */
1.296 brouard 16487: /* It seems that anprojd which is computed from the mean year at interview which is known yet because of freqsummary */
16488: /* date2dmy(dateintmean,&jintmean,&mintmean,&aintmean); */ /* Done in freqsummary */
16489: if(prvforecast==1){
16490: dateprojd=(jproj1+12*mproj1+365*anproj1)/365;
16491: jprojd=jproj1;
16492: mprojd=mproj1;
16493: anprojd=anproj1;
16494: dateprojf=(jproj2+12*mproj2+365*anproj2)/365;
16495: jprojf=jproj2;
16496: mprojf=mproj2;
16497: anprojf=anproj2;
16498: } else if(prvforecast == 2){
16499: dateprojd=dateintmean;
16500: date2dmy(dateprojd,&jprojd, &mprojd, &anprojd);
16501: dateprojf=dateintmean+yrfproj;
16502: date2dmy(dateprojf,&jprojf, &mprojf, &anprojf);
16503: }
16504: if(prvbackcast==1){
16505: datebackd=(jback1+12*mback1+365*anback1)/365;
16506: jbackd=jback1;
16507: mbackd=mback1;
16508: anbackd=anback1;
16509: datebackf=(jback2+12*mback2+365*anback2)/365;
16510: jbackf=jback2;
16511: mbackf=mback2;
16512: anbackf=anback2;
16513: } else if(prvbackcast == 2){
16514: datebackd=dateintmean;
16515: date2dmy(datebackd,&jbackd, &mbackd, &anbackd);
16516: datebackf=dateintmean-yrbproj;
16517: date2dmy(datebackf,&jbackf, &mbackf, &anbackf);
16518: }
16519:
1.350 brouard 16520: printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,bage, fage, prevfcast, prevbcast, pathc,p, (int)anprojd-bage, (int)anbackd-fage);/* HERE valgrind Tvard*/
1.220 brouard 16521: }
16522: printinghtml(fileresu,title,datafile, firstpass, lastpass, stepm, weightopt, \
1.296 brouard 16523: model,imx,jmin,jmax,jmean,rfileres,popforecast,mobilav,prevfcast,mobilavproj,prevbcast, estepm, \
16524: jprev1,mprev1,anprev1,dateprev1, dateprojd, datebackd,jprev2,mprev2,anprev2,dateprev2,dateprojf, datebackf);
1.220 brouard 16525:
1.225 brouard 16526: /*------------ free_vector -------------*/
16527: /* chdir(path); */
1.220 brouard 16528:
1.215 brouard 16529: /* free_ivector(wav,1,imx); */ /* Moved after last prevalence call */
16530: /* free_imatrix(dh,1,lastpass-firstpass+2,1,imx); */
16531: /* free_imatrix(bh,1,lastpass-firstpass+2,1,imx); */
16532: /* free_imatrix(mw,1,lastpass-firstpass+2,1,imx); */
1.290 brouard 16533: free_lvector(num,firstobs,lastobs);
16534: free_vector(agedc,firstobs,lastobs);
1.126 brouard 16535: /*free_matrix(covar,0,NCOVMAX,1,n);*/
16536: /*free_matrix(covar,1,NCOVMAX,1,n);*/
16537: fclose(ficparo);
16538: fclose(ficres);
1.220 brouard 16539:
16540:
1.186 brouard 16541: /* Other results (useful)*/
1.220 brouard 16542:
16543:
1.126 brouard 16544: /*--------------- Prevalence limit (period or stable prevalence) --------------*/
1.180 brouard 16545: /*#include "prevlim.h"*/ /* Use ficrespl, ficlog */
16546: prlim=matrix(1,nlstate,1,nlstate);
1.332 brouard 16547: /* Computes the prevalence limit for each combination k of the dummy covariates by calling prevalim(k) */
1.209 brouard 16548: prevalence_limit(p, prlim, ageminpar, agemaxpar, ftolpl, &ncvyear);
1.126 brouard 16549: fclose(ficrespl);
16550:
16551: /*------------- h Pij x at various ages ------------*/
1.180 brouard 16552: /*#include "hpijx.h"*/
1.332 brouard 16553: /** 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?*/
16554: /* calls hpxij with combination k */
1.180 brouard 16555: hPijx(p, bage, fage);
1.145 brouard 16556: fclose(ficrespij);
1.227 brouard 16557:
1.220 brouard 16558: /* ncovcombmax= pow(2,cptcoveff); */
1.332 brouard 16559: /*-------------- Variance of one-step probabilities for a combination ij or for nres ?---*/
1.145 brouard 16560: k=1;
1.126 brouard 16561: varprob(optionfilefiname, matcov, p, delti, nlstate, bage, fage,k,Tvar,nbcode, ncodemax,strstart);
1.227 brouard 16562:
1.269 brouard 16563: /* Prevalence for each covariate combination in probs[age][status][cov] */
16564: probs= ma3x(AGEINF,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
16565: for(i=AGEINF;i<=AGESUP;i++)
1.219 brouard 16566: for(j=1;j<=nlstate+ndeath;j++) /* ndeath is useless but a necessity to be compared with mobaverages */
1.225 brouard 16567: for(k=1;k<=ncovcombmax;k++)
16568: probs[i][j][k]=0.;
1.269 brouard 16569: prevalence(probs, ageminpar, agemaxpar, s, agev, nlstate, imx, Tvar, nbcode,
16570: ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
1.219 brouard 16571: if (mobilav!=0 ||mobilavproj !=0 ) {
1.269 brouard 16572: mobaverages= ma3x(AGEINF, AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
16573: for(i=AGEINF;i<=AGESUP;i++)
1.268 brouard 16574: for(j=1;j<=nlstate+ndeath;j++)
1.227 brouard 16575: for(k=1;k<=ncovcombmax;k++)
16576: mobaverages[i][j][k]=0.;
1.219 brouard 16577: mobaverage=mobaverages;
16578: if (mobilav!=0) {
1.235 brouard 16579: printf("Movingaveraging observed prevalence\n");
1.258 brouard 16580: fprintf(ficlog,"Movingaveraging observed prevalence\n");
1.227 brouard 16581: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilav)!=0){
16582: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
16583: printf(" Error in movingaverage mobilav=%d\n",mobilav);
16584: }
1.269 brouard 16585: } else if (mobilavproj !=0) {
1.235 brouard 16586: printf("Movingaveraging projected observed prevalence\n");
1.258 brouard 16587: fprintf(ficlog,"Movingaveraging projected observed prevalence\n");
1.227 brouard 16588: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilavproj)!=0){
16589: fprintf(ficlog," Error in movingaverage mobilavproj=%d\n",mobilavproj);
16590: printf(" Error in movingaverage mobilavproj=%d\n",mobilavproj);
16591: }
1.269 brouard 16592: }else{
16593: printf("Internal error moving average\n");
16594: fflush(stdout);
16595: exit(1);
1.219 brouard 16596: }
16597: }/* end if moving average */
1.227 brouard 16598:
1.126 brouard 16599: /*---------- Forecasting ------------------*/
1.296 brouard 16600: if(prevfcast==1){
16601: /* /\* if(stepm ==1){*\/ */
16602: /* /\* anproj1, mproj1, jproj1 either read explicitly or yrfproj *\/ */
16603: /*This done previously after freqsummary.*/
16604: /* dateprojd=(jproj1+12*mproj1+365*anproj1)/365; */
16605: /* dateprojf=(jproj2+12*mproj2+365*anproj2)/365; */
16606:
16607: /* } else if (prvforecast==2){ */
16608: /* /\* if(stepm ==1){*\/ */
16609: /* /\* anproj1, mproj1, jproj1 either read explicitly or yrfproj *\/ */
16610: /* } */
16611: /*prevforecast(fileresu, dateintmean, anproj1, mproj1, jproj1, agemin, agemax, dateprev1, dateprev2, mobilavproj, mobaverage, bage, fage, firstpass, lastpass, anproj2, p, cptcoveff);*/
16612: prevforecast(fileresu,dateintmean, dateprojd, dateprojf, agemin, agemax, dateprev1, dateprev2, mobilavproj, mobaverage, bage, fage, firstpass, lastpass, p, cptcoveff);
1.126 brouard 16613: }
1.269 brouard 16614:
1.296 brouard 16615: /* Prevbcasting */
16616: if(prevbcast==1){
1.219 brouard 16617: ddnewms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
16618: ddoldms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
16619: ddsavms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
16620:
16621: /*--------------- Back Prevalence limit (period or stable prevalence) --------------*/
16622:
16623: bprlim=matrix(1,nlstate,1,nlstate);
1.269 brouard 16624:
1.219 brouard 16625: back_prevalence_limit(p, bprlim, ageminpar, agemaxpar, ftolpl, &ncvyear, dateprev1, dateprev2, firstpass, lastpass, mobilavproj);
16626: fclose(ficresplb);
16627:
1.222 brouard 16628: hBijx(p, bage, fage, mobaverage);
16629: fclose(ficrespijb);
1.219 brouard 16630:
1.296 brouard 16631: /* /\* prevbackforecast(fileresu, mobaverage, anback1, mback1, jback1, agemin, agemax, dateprev1, dateprev2, *\/ */
16632: /* /\* mobilavproj, bage, fage, firstpass, lastpass, anback2, p, cptcoveff); *\/ */
16633: /* prevbackforecast(fileresu, mobaverage, anback1, mback1, jback1, agemin, agemax, dateprev1, dateprev2, */
16634: /* mobilavproj, bage, fage, firstpass, lastpass, anback2, p, cptcoveff); */
16635: prevbackforecast(fileresu, mobaverage, dateintmean, dateprojd, dateprojf, agemin, agemax, dateprev1, dateprev2,
16636: mobilavproj, bage, fage, firstpass, lastpass, p, cptcoveff);
16637:
16638:
1.269 brouard 16639: varbprlim(fileresu, nresult, mobaverage, mobilavproj, bage, fage, bprlim, &ncvyear, ftolpl, p, matcov, delti, stepm, cptcoveff);
1.268 brouard 16640:
16641:
1.269 brouard 16642: free_matrix(bprlim,1,nlstate,1,nlstate); /*here or after loop ? */
1.219 brouard 16643: free_matrix(ddnewms, 1, nlstate+ndeath, 1, nlstate+ndeath);
16644: free_matrix(ddsavms, 1, nlstate+ndeath, 1, nlstate+ndeath);
16645: free_matrix(ddoldms, 1, nlstate+ndeath, 1, nlstate+ndeath);
1.296 brouard 16646: } /* end Prevbcasting */
1.268 brouard 16647:
1.186 brouard 16648:
16649: /* ------ Other prevalence ratios------------ */
1.126 brouard 16650:
1.215 brouard 16651: free_ivector(wav,1,imx);
16652: free_imatrix(dh,1,lastpass-firstpass+2,1,imx);
16653: free_imatrix(bh,1,lastpass-firstpass+2,1,imx);
16654: free_imatrix(mw,1,lastpass-firstpass+2,1,imx);
1.218 brouard 16655:
16656:
1.127 brouard 16657: /*---------- Health expectancies, no variances ------------*/
1.218 brouard 16658:
1.201 brouard 16659: strcpy(filerese,"E_");
16660: strcat(filerese,fileresu);
1.126 brouard 16661: if((ficreseij=fopen(filerese,"w"))==NULL) {
16662: printf("Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
16663: fprintf(ficlog,"Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
16664: }
1.208 brouard 16665: printf("Computing Health Expectancies: result on file '%s' ...", filerese);fflush(stdout);
16666: fprintf(ficlog,"Computing Health Expectancies: result on file '%s' ...", filerese);fflush(ficlog);
1.238 brouard 16667:
16668: pstamp(ficreseij);
1.219 brouard 16669:
1.351 brouard 16670: /* i1=pow(2,cptcoveff); /\* Number of combination of dummy covariates *\/ */
16671: /* if (cptcovn < 1){i1=1;} */
1.235 brouard 16672:
1.351 brouard 16673: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
16674: /* for(k=1; k<=i1;k++){ /\* For any combination of dummy covariates, fixed and varying *\/ */
16675: /* if(i1 != 1 && TKresult[nres]!= k) */
16676: /* continue; */
1.219 brouard 16677: fprintf(ficreseij,"\n#****** ");
1.235 brouard 16678: printf("\n#****** ");
1.351 brouard 16679: for(j=1;j<=cptcovs;j++){
16680: /* for(j=1;j<=cptcoveff;j++) { */
16681: /* fprintf(ficreseij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
16682: fprintf(ficreseij," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
16683: printf(" V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
16684: /* printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
1.235 brouard 16685: }
16686: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
1.337 brouard 16687: printf(" V%d=%lg ",TvarsQ[j], TinvDoQresult[nres][TvarsQ[j]]); /* TvarsQ[j] gives the name of the jth quantitative (fixed or time v) */
16688: fprintf(ficreseij,"V%d=%lg ",TvarsQ[j], TinvDoQresult[nres][TvarsQ[j]]);
1.219 brouard 16689: }
16690: fprintf(ficreseij,"******\n");
1.235 brouard 16691: printf("******\n");
1.219 brouard 16692:
16693: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
16694: oldm=oldms;savm=savms;
1.330 brouard 16695: /* printf("HELLO Entering evsij bage=%d fage=%d k=%d estepm=%d nres=%d\n",(int) bage, (int)fage, k, estepm, nres); */
1.235 brouard 16696: evsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, strstart, nres);
1.127 brouard 16697:
1.219 brouard 16698: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
1.127 brouard 16699: }
16700: fclose(ficreseij);
1.208 brouard 16701: printf("done evsij\n");fflush(stdout);
16702: fprintf(ficlog,"done evsij\n");fflush(ficlog);
1.269 brouard 16703:
1.218 brouard 16704:
1.227 brouard 16705: /*---------- State-specific expectancies and variances ------------*/
1.336 brouard 16706: /* Should be moved in a function */
1.201 brouard 16707: strcpy(filerest,"T_");
16708: strcat(filerest,fileresu);
1.127 brouard 16709: if((ficrest=fopen(filerest,"w"))==NULL) {
16710: printf("Problem with total LE resultfile: %s\n", filerest);goto end;
16711: fprintf(ficlog,"Problem with total LE resultfile: %s\n", filerest);goto end;
16712: }
1.208 brouard 16713: printf("Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(stdout);
16714: fprintf(ficlog,"Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(ficlog);
1.201 brouard 16715: strcpy(fileresstde,"STDE_");
16716: strcat(fileresstde,fileresu);
1.126 brouard 16717: if((ficresstdeij=fopen(fileresstde,"w"))==NULL) {
1.227 brouard 16718: printf("Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
16719: fprintf(ficlog,"Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
1.126 brouard 16720: }
1.227 brouard 16721: printf(" Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde);
16722: fprintf(ficlog," Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde);
1.126 brouard 16723:
1.201 brouard 16724: strcpy(filerescve,"CVE_");
16725: strcat(filerescve,fileresu);
1.126 brouard 16726: if((ficrescveij=fopen(filerescve,"w"))==NULL) {
1.227 brouard 16727: printf("Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0);
16728: fprintf(ficlog,"Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0);
1.126 brouard 16729: }
1.227 brouard 16730: printf(" Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve);
16731: fprintf(ficlog," Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve);
1.126 brouard 16732:
1.201 brouard 16733: strcpy(fileresv,"V_");
16734: strcat(fileresv,fileresu);
1.126 brouard 16735: if((ficresvij=fopen(fileresv,"w"))==NULL) {
16736: printf("Problem with variance resultfile: %s\n", fileresv);exit(0);
16737: fprintf(ficlog,"Problem with variance resultfile: %s\n", fileresv);exit(0);
16738: }
1.227 brouard 16739: printf(" Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(stdout);
16740: fprintf(ficlog," Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(ficlog);
1.126 brouard 16741:
1.235 brouard 16742: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
16743: if (cptcovn < 1){i1=1;}
16744:
1.334 brouard 16745: for(nres=1; nres <= nresult; nres++) /* For each resultline, find the combination and output results according to the values of dummies and then quanti. */
16746: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying. For each nres and each value at position k
16747: * we know Tresult[nres][result_position]= value of the dummy variable at the result_position in the nres resultline
16748: * Tvqresult[nres][result_position]= id of the variable at the result_position in the nres resultline
16749: * and Tqresult[nres][result_position]= value of the variable at the result_position in the nres resultline */
16750: /* */
16751: if(i1 != 1 && TKresult[nres]!= k) /* TKresult[nres] is the combination of this nres resultline. All the i1 combinations are not output */
1.235 brouard 16752: continue;
1.359 brouard 16753: printf("\n# model=1+age+%s \n#****** Result for:", model); /* HERE model is empty */
16754: fprintf(ficrest,"\n# model=1+age+%s \n#****** Result for:", model);
16755: fprintf(ficlog,"\n# model=1+age+%s \n#****** Result for:", model);
1.334 brouard 16756: /* It might not be a good idea to mix dummies and quantitative */
16757: /* 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 *\/ */
16758: 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 */
16759: /* printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); /\* Output by variables in the resultline *\/ */
16760: /* Tvaraff[j] is the name of the dummy variable in position j in the equation model:
16761: * Tvaraff[1]@9={4, 3, 0, 0, 0, 0, 0, 0, 0}, in model=V5+V4+V3+V4*V3+V5*age
16762: * (V5 is quanti) V4 and V3 are dummies
16763: * TnsdVar[4] is the position 1 and TnsdVar[3]=2 in codtabm(k,l)(V4 V3)=V4 V3
16764: * l=1 l=2
16765: * k=1 1 1 0 0
16766: * k=2 2 1 1 0
16767: * k=3 [1] [2] 0 1
16768: * k=4 2 2 1 1
16769: * If nres=1 result: V3=1 V4=0 then k=3 and outputs
16770: * If nres=2 result: V4=1 V3=0 then k=2 and outputs
16771: * nres=1 =>k=3 j=1 V4= nbcode[4][codtabm(3,1)=1)=0; j=2 V3= nbcode[3][codtabm(3,2)=2]=1
16772: * nres=2 =>k=2 j=1 V4= nbcode[4][codtabm(2,1)=2)=1; j=2 V3= nbcode[3][codtabm(2,2)=1]=0
16773: */
16774: /* Tvresult[nres][j] Name of the variable at position j in this resultline */
16775: /* Tresult[nres][j] Value of this variable at position j could be a float if quantitative */
16776: /* We give up with the combinations!! */
1.342 brouard 16777: /* if(debugILK) */
16778: /* 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 *\/ */
1.334 brouard 16779:
16780: if(Dummy[modelresult[nres][j]]==0){/* Dummy variable of the variable in position modelresult in the model corresponding to j in resultline */
1.344 brouard 16781: /* 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] */
16782: 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 */
16783: 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 */
16784: 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 */
1.334 brouard 16785: if(Fixed[modelresult[nres][j]]==0){ /* Fixed */
16786: printf("fixed ");fprintf(ficlog,"fixed ");fprintf(ficrest,"fixed ");
16787: }else{
16788: printf("varyi ");fprintf(ficlog,"varyi ");fprintf(ficrest,"varyi ");
16789: }
16790: /* fprintf(ficrest,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
16791: /* fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
16792: }else if(Dummy[modelresult[nres][j]]==1){ /* Quanti variable */
16793: /* For each selected (single) quantitative value */
1.337 brouard 16794: printf(" V%d=%lg ",Tvqresult[nres][j],Tqresult[nres][j]);
16795: fprintf(ficlog," V%d=%lg ",Tvqresult[nres][j],Tqresult[nres][j]);
16796: fprintf(ficrest," V%d=%lg ",Tvqresult[nres][j],Tqresult[nres][j]);
1.334 brouard 16797: if(Fixed[modelresult[nres][j]]==0){ /* Fixed */
16798: printf("fixed ");fprintf(ficlog,"fixed ");fprintf(ficrest,"fixed ");
16799: }else{
16800: printf("varyi ");fprintf(ficlog,"varyi ");fprintf(ficrest,"varyi ");
16801: }
16802: }else{
16803: 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 */
16804: 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 */
16805: exit(1);
16806: }
1.335 brouard 16807: } /* End loop for each variable in the resultline */
1.334 brouard 16808: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
16809: /* printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); /\* Wrong j is not in the equation model *\/ */
16810: /* fprintf(ficrest," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
16811: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
16812: /* } */
1.208 brouard 16813: fprintf(ficrest,"******\n");
1.227 brouard 16814: fprintf(ficlog,"******\n");
16815: printf("******\n");
1.208 brouard 16816:
16817: fprintf(ficresstdeij,"\n#****** ");
16818: fprintf(ficrescveij,"\n#****** ");
1.337 brouard 16819: /* It could have been: for(j=1;j<=cptcoveff;j++) {printf("V=%d=%lg",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);} */
16820: /* But it won't be sorted and depends on how the resultline is ordered */
1.225 brouard 16821: for(j=1;j<=cptcoveff;j++) {
1.334 brouard 16822: fprintf(ficresstdeij,"V%d=%d ",Tvresult[nres][j],Tresult[nres][j]);
16823: /* fprintf(ficresstdeij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
16824: /* fprintf(ficrescveij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
16825: }
16826: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value, TvarsQind gives the position of a quantitative in model equation */
1.337 brouard 16827: fprintf(ficresstdeij," V%d=%lg ",Tvar[TvarsQind[j]],Tqresult[nres][resultmodel[nres][TvarsQind[j]]]);
16828: fprintf(ficrescveij," V%d=%lg ",Tvar[TvarsQind[j]],Tqresult[nres][resultmodel[nres][TvarsQind[j]]]);
1.235 brouard 16829: }
1.208 brouard 16830: fprintf(ficresstdeij,"******\n");
16831: fprintf(ficrescveij,"******\n");
16832:
16833: fprintf(ficresvij,"\n#****** ");
1.238 brouard 16834: /* pstamp(ficresvij); */
1.225 brouard 16835: for(j=1;j<=cptcoveff;j++)
1.335 brouard 16836: fprintf(ficresvij,"V%d=%d ",Tvresult[nres][j],Tresult[nres][j]);
16837: /* fprintf(ficresvij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[TnsdVar[Tvaraff[j]]])]); */
1.235 brouard 16838: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
1.332 brouard 16839: /* fprintf(ficresvij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]); /\* To solve *\/ */
1.337 brouard 16840: fprintf(ficresvij," V%d=%lg ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); /* Solved */
1.235 brouard 16841: }
1.208 brouard 16842: fprintf(ficresvij,"******\n");
16843:
16844: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
16845: oldm=oldms;savm=savms;
1.235 brouard 16846: printf(" cvevsij ");
16847: fprintf(ficlog, " cvevsij ");
16848: cvevsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, delti, matcov, strstart, nres);
1.208 brouard 16849: printf(" end cvevsij \n ");
16850: fprintf(ficlog, " end cvevsij \n ");
16851:
16852: /*
16853: */
16854: /* goto endfree; */
16855:
16856: vareij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
16857: pstamp(ficrest);
16858:
1.269 brouard 16859: epj=vector(1,nlstate+1);
1.208 brouard 16860: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
1.227 brouard 16861: oldm=oldms;savm=savms; /* ZZ Segmentation fault */
16862: cptcod= 0; /* To be deleted */
1.360 brouard 16863: printf("varevsij vpopbased=%d popbased=%d \n",vpopbased,popbased);
16864: fprintf(ficlog, "varevsij vpopbased=%d popbased=%d \n",vpopbased,popbased);
1.361 brouard 16865: /* Call to varevsij to get cov(e.i, e.j)= vareij[i][j][(int)age]=sum_h sum_k trgrad(h_p.i) V(theta) grad(k_p.k) Equation 20 */
16866: /* Depending of popbased which changes the prevalences, either cross-sectional or period */
1.235 brouard 16867: varevsij(optionfilefiname, vareij, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl, &ncvyear, k, estepm, cptcov,cptcod,vpopbased,mobilav, strstart, nres); /* cptcod not initialized Intel */
1.360 brouard 16868: fprintf(ficrest,"# Total life expectancy with std error and decomposition into time to be expected in each state\n\
16869: # (these are weighted average of eij where weights are ");
1.227 brouard 16870: if(vpopbased==1)
1.360 brouard 16871: 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);
1.227 brouard 16872: else
1.360 brouard 16873: fprintf(ficrest,"the age specific forward period (stable) prevalences in each state) \n");
16874: fprintf(ficrest,"# with proportions of time spent in each state with standard error (on the right of the table.\n ");
1.335 brouard 16875: fprintf(ficrest,"# Age popbased mobilav e.. (std) "); /* Adding covariate values? */
1.227 brouard 16876: for (i=1;i<=nlstate;i++) fprintf(ficrest,"e.%d (std) ",i);
1.360 brouard 16877: for (i=1;i<=nlstate;i++) fprintf(ficrest," %% e.%d/e.. (std) ",i);
1.227 brouard 16878: fprintf(ficrest,"\n");
16879: /* printf("Which p?\n"); for(i=1;i<=npar;i++)printf("p[i=%d]=%lf,",i,p[i]);printf("\n"); */
1.288 brouard 16880: printf("Computing age specific forward period (stable) prevalences in each health state \n");
16881: fprintf(ficlog,"Computing age specific forward period (stable) prevalences in each health state \n");
1.227 brouard 16882: for(age=bage; age <=fage ;age++){
1.235 brouard 16883: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, &ncvyear, k, nres); /*ZZ Is it the correct prevalim */
1.227 brouard 16884: if (vpopbased==1) {
16885: if(mobilav ==0){
16886: for(i=1; i<=nlstate;i++)
16887: prlim[i][i]=probs[(int)age][i][k];
16888: }else{ /* mobilav */
16889: for(i=1; i<=nlstate;i++)
16890: prlim[i][i]=mobaverage[(int)age][i][k];
16891: }
16892: }
1.219 brouard 16893:
1.227 brouard 16894: fprintf(ficrest," %4.0f %d %d",age, vpopbased, mobilav);
16895: /* fprintf(ficrest," %4.0f %d %d %d %d",age, vpopbased, mobilav,Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */ /* to be done */
16896: /* printf(" age %4.0f ",age); */
16897: for(j=1, epj[nlstate+1]=0.;j <=nlstate;j++){
16898: for(i=1, epj[j]=0.;i <=nlstate;i++) {
16899: epj[j] += prlim[i][i]*eij[i][j][(int)age];
16900: /*ZZZ printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]);*/
16901: /* printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]); */
16902: }
1.361 brouard 16903: epj[nlstate+1] +=epj[j]; /* epp=sum_j epj = sum_j sum_i w_i e_ij */
1.227 brouard 16904: }
16905: /* printf(" age %4.0f \n",age); */
1.219 brouard 16906:
1.361 brouard 16907: for(i=1, vepp=0.;i <=nlstate;i++) /* Variance of total life expectancy e.. */
1.227 brouard 16908: for(j=1;j <=nlstate;j++)
1.361 brouard 16909: vepp += vareij[i][j][(int)age]; /* sum_i sum_j cov(e.i, e.j) = var(e..) */
1.227 brouard 16910: fprintf(ficrest," %7.3f (%7.3f)", epj[nlstate+1],sqrt(vepp));
1.361 brouard 16911: /* vareij[i][j] is the covariance cov(e.i, e.j) and vareij[j][j] is the variance of e.j */
1.227 brouard 16912: for(j=1;j <=nlstate;j++){
16913: fprintf(ficrest," %7.3f (%7.3f)", epj[j],sqrt(vareij[j][j][(int)age]));
16914: }
1.360 brouard 16915: /* And proportion of time spent in state j */
16916: /* $$ E[r(X,Y)-E(r(X,Y))]^2=[\frac{1}{\mu_y} -\frac{\mu_x}{{\mu_y}^2}]' Var(X,Y)[\frac{1}{\mu_y} -\frac{\mu_x}{{\mu_y}^2}]$$ */
1.361 brouard 16917: /* \frac{\mu_x^2}{\mu_y^2} ( \frac{\sigma^2_x}{\mu_x^2}-2\frac{\sigma_{xy}}{\mu_x\mu_y} +\frac{\sigma^2_y}{\mu_y^2}) */
16918: /* \frac{e_{.i}^2}{e_{..}^2} ( \frac{\Var e_{.i}}{e_{.i}^2}-2\frac{\Var e_{.i} + \sum_{j\ne i} \Cov e_{.j},e_{.i}}{e_{.i}e_{..}} +\frac{\Var e_{..}}{e_{..}^2})*/
16919: /*\mu_x = epj[j], \sigma^2_x = vareij[j][j][(int)age] and \mu_y=epj[nlstate+1], \sigma^2_y=vepp \sigmaxy= */
16920: /* vareij[j][j][(int)age]/epj[nlstate+1]^2 + vepp/epj[nlstate+1]^4 */
1.360 brouard 16921: for(j=1;j <=nlstate;j++){
16922: /* fprintf(ficrest," %7.3f (%7.3f)", epj[j]/epj[nlstate+1], sqrt( vareij[j][j][(int)age]/epj[j]/epj[j] + vepp/epj[j]/epj[j]/epj[j]/epj[j] )); */
1.361 brouard 16923: /* fprintf(ficrest," %7.3f (%7.3f)", epj[j]/epj[nlstate+1], sqrt( vareij[j][j][(int)age]/epj[j]/epj[j] + vepp/epj[j]/epj[j]/epj[j]/epj[j] )); */
16924:
16925: for(i=1,stdpercent=0.;i<=nlstate;i++){ /* Computing cov(e..,e.j)=cov(sum_i e.i,e.j)=sum_i cov(e.i, e.j) */
16926: stdpercent += vareij[i][j][(int)age];
16927: }
16928: stdpercent= epj[j]*epj[j]/epj[nlstate+1]/epj[nlstate+1]* (vareij[j][j][(int)age]/epj[j]/epj[j]-2.*stdpercent/epj[j]/epj[nlstate+1]+ vepp/epj[nlstate+1]/epj[nlstate+1]);
16929: /* stdpercent= epj[j]*epj[j]/epj[nlstate+1]/epj[nlstate+1]*(vareij[j][j][(int)age]/epj[j]/epj[j] + vepp/epj[nlstate+1]/epj[nlstate+1]); */ /* Without covariance */
16930: /* fprintf(ficrest," %7.3f (%7.3f)", epj[j]/epj[nlstate+1], sqrt( vareij[j][j][(int)age]/epj[nlstate+1]/epj[nlstate+1] + epj[j]*epj[j]*vepp/epj[nlstate+1]/epj[nlstate+1]/epj[nlstate+1]/epj[nlstate+1] )); */
16931: fprintf(ficrest," %7.3f (%7.3f)", epj[j]/epj[nlstate+1], sqrt(stdpercent));
1.360 brouard 16932: }
1.227 brouard 16933: fprintf(ficrest,"\n");
16934: }
1.208 brouard 16935: } /* End vpopbased */
1.269 brouard 16936: free_vector(epj,1,nlstate+1);
1.208 brouard 16937: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
16938: free_ma3x(vareij,1,nlstate,1,nlstate,(int) bage, (int)fage);
1.235 brouard 16939: printf("done selection\n");fflush(stdout);
16940: fprintf(ficlog,"done selection\n");fflush(ficlog);
1.208 brouard 16941:
1.335 brouard 16942: } /* End k selection or end covariate selection for nres */
1.227 brouard 16943:
16944: printf("done State-specific expectancies\n");fflush(stdout);
16945: fprintf(ficlog,"done State-specific expectancies\n");fflush(ficlog);
16946:
1.335 brouard 16947: /* variance-covariance of forward period prevalence */
1.269 brouard 16948: varprlim(fileresu, nresult, mobaverage, mobilavproj, bage, fage, prlim, &ncvyear, ftolpl, p, matcov, delti, stepm, cptcoveff);
1.268 brouard 16949:
1.227 brouard 16950:
1.290 brouard 16951: free_vector(weight,firstobs,lastobs);
1.351 brouard 16952: free_imatrix(Tvardk,0,NCOVMAX,1,2);
1.227 brouard 16953: free_imatrix(Tvard,1,NCOVMAX,1,2);
1.290 brouard 16954: free_imatrix(s,1,maxwav+1,firstobs,lastobs);
16955: free_matrix(anint,1,maxwav,firstobs,lastobs);
16956: free_matrix(mint,1,maxwav,firstobs,lastobs);
16957: free_ivector(cod,firstobs,lastobs);
1.227 brouard 16958: free_ivector(tab,1,NCOVMAX);
16959: fclose(ficresstdeij);
16960: fclose(ficrescveij);
16961: fclose(ficresvij);
16962: fclose(ficrest);
16963: fclose(ficpar);
16964:
16965:
1.126 brouard 16966: /*---------- End : free ----------------*/
1.219 brouard 16967: if (mobilav!=0 ||mobilavproj !=0)
1.269 brouard 16968: free_ma3x(mobaverages,AGEINF, AGESUP,1,nlstate+ndeath, 1,ncovcombmax); /* We need to have a squared matrix with prevalence of the dead! */
16969: free_ma3x(probs,AGEINF,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
1.220 brouard 16970: free_matrix(prlim,1,nlstate,1,nlstate); /*here or after loop ? */
16971: free_matrix(pmmij,1,nlstate+ndeath,1,nlstate+ndeath);
1.126 brouard 16972: } /* mle==-3 arrives here for freeing */
1.227 brouard 16973: /* endfree:*/
1.359 brouard 16974: if(mle!=-3) free_matrix(precov, 1,MAXRESULTLINESPONE,1,NCOVMAX+1); /* Could be elsewhere ?*/
1.227 brouard 16975: free_matrix(oldms, 1,nlstate+ndeath,1,nlstate+ndeath);
16976: free_matrix(newms, 1,nlstate+ndeath,1,nlstate+ndeath);
16977: free_matrix(savms, 1,nlstate+ndeath,1,nlstate+ndeath);
1.341 brouard 16978: /* if(ntv+nqtv>=1)free_ma3x(cotvar,1,maxwav,1,ntv+nqtv,firstobs,lastobs); */
16979: if(ntv+nqtv>=1)free_ma3x(cotvar,1,maxwav,ncovcol+nqv+1,ncovcol+nqv+ntv+nqtv,firstobs,lastobs);
1.290 brouard 16980: if(nqtv>=1)free_ma3x(cotqvar,1,maxwav,1,nqtv,firstobs,lastobs);
16981: if(nqv>=1)free_matrix(coqvar,1,nqv,firstobs,lastobs);
16982: free_matrix(covar,0,NCOVMAX,firstobs,lastobs);
1.227 brouard 16983: free_matrix(matcov,1,npar,1,npar);
16984: free_matrix(hess,1,npar,1,npar);
16985: /*free_vector(delti,1,npar);*/
16986: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
16987: free_matrix(agev,1,maxwav,1,imx);
1.269 brouard 16988: free_ma3x(paramstart,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
1.227 brouard 16989: free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
16990:
16991: free_ivector(ncodemax,1,NCOVMAX);
16992: free_ivector(ncodemaxwundef,1,NCOVMAX);
16993: free_ivector(Dummy,-1,NCOVMAX);
16994: free_ivector(Fixed,-1,NCOVMAX);
1.349 brouard 16995: free_ivector(DummyV,-1,NCOVMAX);
16996: free_ivector(FixedV,-1,NCOVMAX);
1.227 brouard 16997: free_ivector(Typevar,-1,NCOVMAX);
16998: free_ivector(Tvar,1,NCOVMAX);
1.234 brouard 16999: free_ivector(TvarsQ,1,NCOVMAX);
17000: free_ivector(TvarsQind,1,NCOVMAX);
17001: free_ivector(TvarsD,1,NCOVMAX);
1.330 brouard 17002: free_ivector(TnsdVar,1,NCOVMAX);
1.234 brouard 17003: free_ivector(TvarsDind,1,NCOVMAX);
1.231 brouard 17004: free_ivector(TvarFD,1,NCOVMAX);
17005: free_ivector(TvarFDind,1,NCOVMAX);
1.232 brouard 17006: free_ivector(TvarF,1,NCOVMAX);
17007: free_ivector(TvarFind,1,NCOVMAX);
17008: free_ivector(TvarV,1,NCOVMAX);
17009: free_ivector(TvarVind,1,NCOVMAX);
17010: free_ivector(TvarA,1,NCOVMAX);
17011: free_ivector(TvarAind,1,NCOVMAX);
1.231 brouard 17012: free_ivector(TvarFQ,1,NCOVMAX);
17013: free_ivector(TvarFQind,1,NCOVMAX);
17014: free_ivector(TvarVD,1,NCOVMAX);
17015: free_ivector(TvarVDind,1,NCOVMAX);
17016: free_ivector(TvarVQ,1,NCOVMAX);
17017: free_ivector(TvarVQind,1,NCOVMAX);
1.349 brouard 17018: free_ivector(TvarAVVA,1,NCOVMAX);
17019: free_ivector(TvarAVVAind,1,NCOVMAX);
17020: free_ivector(TvarVVA,1,NCOVMAX);
17021: free_ivector(TvarVVAind,1,NCOVMAX);
1.339 brouard 17022: free_ivector(TvarVV,1,NCOVMAX);
17023: free_ivector(TvarVVind,1,NCOVMAX);
17024:
1.230 brouard 17025: free_ivector(Tvarsel,1,NCOVMAX);
17026: free_vector(Tvalsel,1,NCOVMAX);
1.227 brouard 17027: free_ivector(Tposprod,1,NCOVMAX);
17028: free_ivector(Tprod,1,NCOVMAX);
17029: free_ivector(Tvaraff,1,NCOVMAX);
1.338 brouard 17030: free_ivector(invalidvarcomb,0,ncovcombmax);
1.227 brouard 17031: free_ivector(Tage,1,NCOVMAX);
17032: free_ivector(Tmodelind,1,NCOVMAX);
1.228 brouard 17033: free_ivector(TmodelInvind,1,NCOVMAX);
17034: free_ivector(TmodelInvQind,1,NCOVMAX);
1.332 brouard 17035:
1.359 brouard 17036: /* free_matrix(precov, 1,MAXRESULTLINESPONE,1,NCOVMAX+1); /\* Could be elsewhere ?*\/ */
1.332 brouard 17037:
1.227 brouard 17038: free_imatrix(nbcode,0,NCOVMAX,0,NCOVMAX);
17039: /* free_imatrix(codtab,1,100,1,10); */
1.126 brouard 17040: fflush(fichtm);
17041: fflush(ficgp);
17042:
1.227 brouard 17043:
1.126 brouard 17044: if((nberr >0) || (nbwarn>0)){
1.216 brouard 17045: printf("End of Imach with %d errors and/or %d warnings. Please look at the log file for details.\n",nberr,nbwarn);
17046: fprintf(ficlog,"End of Imach with %d errors and/or warnings %d. Please look at the log file for details.\n",nberr,nbwarn);
1.126 brouard 17047: }else{
17048: printf("End of Imach\n");
17049: fprintf(ficlog,"End of Imach\n");
17050: }
17051: printf("See log file on %s\n",filelog);
17052: /* gettimeofday(&end_time, (struct timezone*)0);*/ /* after time */
1.157 brouard 17053: /*(void) gettimeofday(&end_time,&tzp);*/
17054: rend_time = time(NULL);
17055: end_time = *localtime(&rend_time);
17056: /* tml = *localtime(&end_time.tm_sec); */
17057: strcpy(strtend,asctime(&end_time));
1.126 brouard 17058: printf("Local time at start %s\nLocal time at end %s",strstart, strtend);
17059: fprintf(ficlog,"Local time at start %s\nLocal time at end %s\n",strstart, strtend);
1.157 brouard 17060: printf("Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
1.227 brouard 17061:
1.157 brouard 17062: printf("Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
17063: fprintf(ficlog,"Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
17064: fprintf(ficlog,"Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
1.126 brouard 17065: /* printf("Total time was %d uSec.\n", total_usecs);*/
17066: /* if(fileappend(fichtm,optionfilehtm)){ */
17067: fprintf(fichtm,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
17068: fclose(fichtm);
17069: fprintf(fichtmcov,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
17070: fclose(fichtmcov);
17071: fclose(ficgp);
17072: fclose(ficlog);
17073: /*------ End -----------*/
1.227 brouard 17074:
1.281 brouard 17075:
17076: /* Executes gnuplot */
1.227 brouard 17077:
17078: printf("Before Current directory %s!\n",pathcd);
1.184 brouard 17079: #ifdef WIN32
1.227 brouard 17080: if (_chdir(pathcd) != 0)
17081: printf("Can't move to directory %s!\n",path);
17082: if(_getcwd(pathcd,MAXLINE) > 0)
1.184 brouard 17083: #else
1.227 brouard 17084: if(chdir(pathcd) != 0)
17085: printf("Can't move to directory %s!\n", path);
17086: if (getcwd(pathcd, MAXLINE) > 0)
1.184 brouard 17087: #endif
1.126 brouard 17088: printf("Current directory %s!\n",pathcd);
17089: /*strcat(plotcmd,CHARSEPARATOR);*/
17090: sprintf(plotcmd,"gnuplot");
1.157 brouard 17091: #ifdef _WIN32
1.126 brouard 17092: sprintf(plotcmd,"\"%sgnuplot.exe\"",pathimach);
17093: #endif
17094: if(!stat(plotcmd,&info)){
1.158 brouard 17095: printf("Error or gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 17096: if(!stat(getenv("GNUPLOTBIN"),&info)){
1.158 brouard 17097: printf("Error or gnuplot program not found: '%s' Environment GNUPLOTBIN not set.\n",plotcmd);fflush(stdout);
1.126 brouard 17098: }else
17099: strcpy(pplotcmd,plotcmd);
1.157 brouard 17100: #ifdef __unix
1.126 brouard 17101: strcpy(plotcmd,GNUPLOTPROGRAM);
17102: if(!stat(plotcmd,&info)){
1.158 brouard 17103: printf("Error gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 17104: }else
17105: strcpy(pplotcmd,plotcmd);
17106: #endif
17107: }else
17108: strcpy(pplotcmd,plotcmd);
17109:
17110: sprintf(plotcmd,"%s %s",pplotcmd, optionfilegnuplot);
1.158 brouard 17111: printf("Starting graphs with: '%s'\n",plotcmd);fflush(stdout);
1.292 brouard 17112: strcpy(pplotcmd,plotcmd);
1.227 brouard 17113:
1.126 brouard 17114: if((outcmd=system(plotcmd)) != 0){
1.292 brouard 17115: printf("Error in gnuplot, command might not be in your path: '%s', err=%d\n", plotcmd, outcmd);
1.154 brouard 17116: printf("\n Trying if gnuplot resides on the same directory that IMaCh\n");
1.152 brouard 17117: sprintf(plotcmd,"%sgnuplot %s", pathimach, optionfilegnuplot);
1.292 brouard 17118: if((outcmd=system(plotcmd)) != 0){
1.153 brouard 17119: printf("\n Still a problem with gnuplot command %s, err=%d\n", plotcmd, outcmd);
1.292 brouard 17120: strcpy(plotcmd,pplotcmd);
17121: }
1.126 brouard 17122: }
1.158 brouard 17123: printf(" Successful, please wait...");
1.126 brouard 17124: while (z[0] != 'q') {
17125: /* chdir(path); */
1.154 brouard 17126: printf("\nType e to edit results with your browser, g to graph again and q for exit: ");
1.126 brouard 17127: scanf("%s",z);
17128: /* if (z[0] == 'c') system("./imach"); */
17129: if (z[0] == 'e') {
1.158 brouard 17130: #ifdef __APPLE__
1.152 brouard 17131: sprintf(pplotcmd, "open %s", optionfilehtm);
1.157 brouard 17132: #elif __linux
17133: sprintf(pplotcmd, "xdg-open %s", optionfilehtm);
1.153 brouard 17134: #else
1.152 brouard 17135: sprintf(pplotcmd, "%s", optionfilehtm);
1.153 brouard 17136: #endif
17137: printf("Starting browser with: %s",pplotcmd);fflush(stdout);
17138: system(pplotcmd);
1.126 brouard 17139: }
17140: else if (z[0] == 'g') system(plotcmd);
17141: else if (z[0] == 'q') exit(0);
17142: }
1.227 brouard 17143: end:
1.126 brouard 17144: while (z[0] != 'q') {
1.195 brouard 17145: printf("\nType q for exiting: "); fflush(stdout);
1.126 brouard 17146: scanf("%s",z);
17147: }
1.283 brouard 17148: printf("End\n");
1.282 brouard 17149: exit(0);
1.126 brouard 17150: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>