>!

---- PARTIE 0 ---!
> preambule=read.table(+ "http://freakonometrics.free.fr/preambule.csv",header=TRUE,sep=";")> table(preambule$Y)01
2
3
45 133 160 101

4
51

5
8

6
2

> reg0=glm(Y/N~1,family="binomial",weights=N,data=preambule)> summary(reg0)
Call:glm(formula = Y/N ~ 1, family = "binomial", data = preambule,weights = N)
Deviance Residuals:
Min
1Q
Median-2.12673 -0.87408 -0.01892

3Q
0.73065Max
2.74209

Coefficients:
Estimate Std. Error z value Pr(>|z|)(Intercept) -1.3714
0.0352 -38.96<2e-16 ***--Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1(Dispersion parameter for binomial family taken to be 1)
Null deviance: 512.87
Residual deviance: 512.87
AIC: 1586.2

on 499on 499degrees of freedom
degrees of freedom

Number of Fisher Scoring iterations: 4!
!
---- PARTIE 1 ---> CORPOREL=read.table(+ "http://freakonometrics.free.fr/corporel-2040.csv",+ header=TRUE,sep=";")
1!!

tail(CORPOREL)
degre age cat.age sexe vehicule anciennete alcool cat.alc
76336 indemne 45
40-49
M voiture
600-20
76337 corporel 59
50-59
F voiture200-20
76338 indemne 34
30-39
F voiture200-2076339 indemne 29
26-29
F voiture590-20
76340 indemne 64
60+
M voiture000-20
76341 indemne 57
50-59
F voiture
10
0-20> attach(CORPOREL)> table(degre)
degre
corporeldeces indemne
31369
676
44296
> Y=degre=="deces"> table(Y)Y
FALSE TRUE
75665
676
> X1=vehicule; nom1=levels(X1)> X2=cat.alc; nom2=levels(X2)> comptage=table(X1,X2)> deces=comptage> for(k in 1:nrow(comptage)){+ deces[k,]=tapply(Y[X1==nom1[k]],X2[X1==nom1[k]],sum)}> deces[is.na(deces)]=0> comptage
X2
X1
0-20 150+ 20-50 50-80 80-150
bus-truck 3218
74
0
13
52
moto
2059
49
5
11
60
van
6237
120
8
32
113
voiture
62433
795
56
244
762
> deces
X2
X1
0-20 150+ 20-50 50-80 80-150
bus-truck
93
6
0
1
2
moto
51
4
0
2
4
van
76
7
0
2
3
voiture
372
25
0
11
17
2!!

>
> taux=deces/comptage> taux
X2
X1
0-20
150+
20-50
50-80
80-150bus-truck 0.028899938 0.081081081
0.076923077 0.038461538
moto
0.024769305 0.081632653 0.000000000 0.181818182 0.066666667
van
0.012185346 0.058333333 0.000000000 0.062500000 0.026548673
voiture0.005958387 0.031446541 0.000000000 0.045081967 0.022309711> comptage[is.na(comptage)]=0> m=mean(Y)>> L<-matrix(NA,10,nrow(deces));C<-matrix(NA,10,ncol(deces))> colnames(L)=nom1;colnames(C)=nom2> C[1,]<-m> for(j in 2:10){+
for(k in 1:nrow(deces)){+
L[j,k]<-sum(deces[k,])/sum(comptage[k,]*C[j-1,]) }+for(k in 1:ncol(deces)){+
C[j,k]<-sum(deces[,k])/sum(comptage[,k]*L[j,]) }+}> L[10,]bus-truckmoto
vanvoiture3.3578117 3.0102805 1.4996518 0.7585497>

C[10,]
0-20
150+
20-50
50-80
80-1500.008030879 0.035623800 0.000000000 0.051639617 0.023578519>>

pred1 = deces
for(k in 1:nrow(deces)){pred1[k,]<-L[10,k]*C[10,]}

pred1
X2

X1

0-20
150+
20-50
50-80
80-150bus-truck 0.026966178 0.119618012 0.000000000 0.173396109 0.079172227
moto
0.024175198 0.107237631 0.000000000 0.155449732 0.070977956
van
0.012043522 0.053423297 0.000000000 0.077441446 0.035359569
voiture0.006091821 0.027022425 0.000000000 0.039171218 0.017885480>>

reg1=glm(Y~vehicule+cat.alc,family=poisson(link="log"),data=CORPOREL)
summary(reg1)

Call:glm(formula = Y ~ vehicule + cat.alc, family = poisson(link = "log"),data = CORPOREL)
Deviance Residuals:
Min
1Q
Median-0.5889 -0.1104 -0.1104

3Q-0.1104Max
2.8660Coefficients:
Estimate Std. Error z value Pr(>|z|)(Intercept)-3.6132
0.1006 -35.924 < 2e-16 ***vehiculemoto-0.1093
0.1620 -0.675
0.500vehiculevan-0.80610.1455 -5.539 3.04e-08 ***vehiculevoiture -1.4876
0.1104 -13.472 < 2e-16 ***cat.alc150+
1.48970.16009.308 < 2e-16 ***cat.alc20-50-10.4584
151.4947 -0.069
0.945
cat.alc50-80
1.86100.25347.344 2.07e-13 ***cat.alc80-150
1.07700.20075.365 8.08e-08 ***--Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1(Dispersion parameter for poisson family taken to be 1)
Null deviance: 6390.6
Residual deviance: 6064.0
AIC: 7432

on 76340
on 76333

degrees of freedom
degrees of freedom

Number of Fisher Scoring iterations: 13> newd=data.frame(vehicule=rep(nom1,length(nom2)),+ cat.alc=rep(nom2,each=length(nom1)))
3!!

4!!

> pred2=predict(reg1,newdata=newd,type="response")> P2=matrix(pred2,length(nom1),length(nom2))> rownames(P2)=nom1;colnames(P2)=nom2> table(CORPOREL$cat.alc)
0-20
73947

150+
1038

20-50
69

50-80 80-150300987> CORPOREL$cat.alc2=CORPOREL$cat.alc> levels(CORPOREL$cat.alc2)=c("0-50","150+","0-50","50-150","50-150")> table(CORPOREL$cat.alc2)
0-50
74016

150+ 50-150
1038
1287

> table(CORPOREL$vehicule)bus-truck
3357

moto
2184

van
6510

voiture64290> CORPOREL$veh2=CORPOREL$vehicule> levels(CORPOREL$veh2)=c("bus-truck-moto",+ "bus-truck-moto","van","voiture")> table(CORPOREL$veh2)
bus-truck-moto
5541

van
6510

voiture64290> reg2=glm(Y~veh2+cat.alc2,family=poisson(link="log"),data=CORPOREL)> summary(reg2)
Call:glm(formula = Y ~ veh2 + cat.alc2, family = poisson(link = "log"),data = CORPOREL)
Deviance Residuals:
Min
1Q
Median-0.4783 -0.1104 -0.1104

3Q-0.1104Max
2.8658

Coefficients:
Estimate Std. Error z value Pr(>|z|)(Intercept)-3.65936
0.08069 -45.351 < 2e-16 ***veh2van-0.76075
0.13230 -5.750 8.93e-09 ***veh2voiture-1.44099
0.09242 -15.592 < 2e-16 ***cat.alc2150+
1.490990.16005
9.316 < 2e-16 ***cat.alc250-150 1.30600
0.15991
8.167 3.15e-16 ***--Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1(Dispersion parameter for poisson family taken to be 1)
Null deviance: 6390.6
Residual deviance: 6071.2
AIC: 7433.2

on 76340
on 76336

degrees of freedom
degrees of freedom

Number of Fisher Scoring iterations: 7> predict(reg2,newdata=data.frame(cat.alc2=c("0-50","50-150","150+"),+ veh2=c("voiture","voiture","voiture")),+ type="response")1
2
3
0.006094632 0.022497609 0.027069134> reg3=glm(Y~veh2+cat.alc2,family=binomial(link="logit"),data=CORPOREL)

5!!

6!!

> summary(reg3)> reg4=glm(Y~veh2+cat.alc2,family=quasipoisson(link="log"),data=CORPOREL)> summary(reg4)

Call:glm(formula = Y ~ veh2 + cat.alc2, family = binomial(link = "logit"),
data = CORPOREL)
Deviance Residuals:
Min
1Q
Median-0.4832 -0.1104 -0.1104

3Q-0.1104Max
3.1946Call:glm(formula = Y ~ veh2 + cat.alc2, family = quasipoisson(link = "log"),data = CORPOREL)
Deviance Residuals:
Min
1Q
Median-0.4783 -0.1104 -0.1104

3Q-0.1104Max
2.8658

Coefficients:
Estimate Std. Error z value Pr(>|z|)(Intercept)-3.62662
0.08184 -44.311 < 2e-16 ***veh2van-0.781780.13395 -5.836 5.34e-09 ***veh2voiture-1.46987
0.09370 -15.688 < 2e-16 ***cat.alc2150+
1.53780
0.16450
9.348 < 2e-16 ***cat.alc250-150 1.34111
0.163518.202 2.36e-16 ***--Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Coefficients:
Estimate Std. Error t value Pr(>|t|)(Intercept)-3.65936
0.07991 -45.794 < 2e-16 ***veh2van-0.76075
0.13102 -5.806 6.42e-09 ***veh2voiture-1.44099
0.09152 -15.745 < 2e-16 ***cat.alc2150+
1.49099
0.15850
9.407 < 2e-16 ***cat.alc250-150 1.30600
0.15836
8.247 < 2e-16 ***--Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1(Dispersion parameter for binomial family taken to be 1)(Dispersion parameter for quasipoisson family taken to be 0.9807156)
Null deviance: 7736.6
Residual deviance: 7411.8
AIC: 7421.8

on 76340
on 76336

degrees of freedom
degrees of freedom

Null deviance: 6390.6
Residual deviance: 6071.2
AIC: NA

on 76340
on 76336

degrees of freedom
degrees of freedom

Number of Fisher Scoring iterations: 7
Number of Fisher Scoring iterations: 7> predict(reg3,newdata=data.frame(cat.alc2=c("0-50","50-150","150+"),+ veh2=c("voiture","voiture","voiture")),+ type="response")1
2
3
0.006080978 0.022856896 0.027687728> table(CORPOREL$cat.alc2)/length((CORPOREL$cat.alc2))
0-50
150+
50-1500.96954454 0.01359689 0.01685857> predict(reg3,newdata=data.frame(cat.alc2=c("0-50","50-150","150+"),+ veh2=c("voiture","voiture","voiture")),+ type="response)
7!!

8!!

> library(nnet)> CORPOREL$Y=degre> reg5=multinom(Y~veh2+cat.alc2,data=CORPOREL)# weights: 18 (10 variable)
initial value 83869.160729
iter 10 value 56945.564900
iter 20 value 54368.409072
iter 30 value 54349.196650
final value 54348.927382
converged> summary(reg5)Call:multinom(formula = Y ~ veh2 + cat.alc2, data = CORPOREL)
Coefficients:(Intercept)
veh2van veh2voiture cat.alc2150+ cat.alc250-150
deces-3.128271 -0.3825518 -1.0360780
1.1238657
1.0381024
indemne-0.482676 0.8660091
0.9230502-0.9679921-0.6471955
Std. Errors:(Intercept)
veh2van veh2voiture cat.alc2150+ cat.alc250-150
deces
0.08209712 0.1350718 0.09420593
0.16545337
0.16485982
indemne 0.02827523 0.0379941 0.02937917
0.06732754
0.05821281
Residual Deviance: 108697.9
AIC: 108717.9> reg6=multinom(Y~veh2+cat.alc2+sexe+anciennete,data=CORPOREL)# weights: 24 (14 variable)
initial value 83869.160729
iter 10 value 60708.059345
iter 20 value 54354.056598
iter 30 value 54230.746815
iter 30 value 54230.746310
final value 54230.746310
converged> summary(reg6)Call:multinom(formula = Y ~ veh2 + cat.alc2 + sexe + anciennete, data =CORPOREL)
Coefficients:(Intercept)
veh2van veh2voiture cat.alc2150+ cat.alc250-150
sexeM anciennete
deces-3.4747251 -0.3657135 -0.9120289
1.082679
0.9816627 0.3509260 0.003250655
indemne -0.7231899 0.8768508
1.0129565-1.002271-0.6915642 0.2443382 0.002215666
Std. Errors:(Intercept)
veh2van veh2voiture cat.alc2150+ cat.alc250-150
sexeM anciennete
deces
0.12920722 0.13518641 0.09909299
0.16594222
0.16562513 0.09835261 0.009798542
indemne 0.03285738 0.03802368 0.03000948
0.06747606
0.05838016 0.01612267 0.001885893

Residual Deviance: 108461.5
AIC: 108489.5!
---- PARTIE 2 ---> source("http://freakonometrics.free.fr/triangle-intra2.R")> intra$triangle
0
1
2
3
4
5
6
7
8
9
1988 5244 9228 10823 11352 11791 12082 12120 12199 12215 12215
1989 5984 9939 11725 12346 12746 12909 13034 13109 13113
NA
1990 7452 12421 14171 14752 15066 15354 15637 15720
NA
NA
1991 7115 11117 12488 13274 13662 13859 13872
NA
NA
NA
1992 5753 8969 9917 10697 11135 11282
NA
NA
NA
NA
1993 3937 6524 7989 8543 8757
NA
NA
NA
NA
NA
1994 5127 8212 8976 9325
NA
NA
NA
NA
NA
NA
1995 5046 8006 8984
NA
NA
NA
NA
NA
NA
NA
1996 5129 8202
NA
NA
NA
NA
NA
NA
NA
NA
1997 3689
NA
NA
NA
NA
NA
NA
NA
NA
NA
$prime1988 1989 1990 1991 1992 1993 1994 1995 1996 1997
15883 16689 18029 17858 16709 14212 15083 15131 15465 11217> mC=intra$triangle> n=ncol(mC)

9!!

10
!!

> MackChainLadder(mC)
MackChainLadder(Triangle = mC)> reg1=lm(log(Y)~A+B,data=df)> summary(reg1)

Latest Dev.To.Date Ultimate
IBNR Mack.S.E CV(IBNR)
1988 12,215
1.000
12,2150.0
0.0
NaN
1989 13,113
1.000
13,1130.0
7.9
Inf
1990 15,720
0.999
15,732
12.4
15.7
1.262
1991 13,872
0.993
13,964
91.6
17.3
0.189
1992 11,282
0.985
11,453
170.7
111.9
0.656
1993 8,757
0.969
9,039
282.4
112.7
0.399
1994 9,325
0.940
9,923
598.2
148.2
0.248
1995 8,984
0.891
10,088 1,104.0
219.2
0.199
1996 8,202
0.779
10,529 2,326.9
473.3
0.203
1997 3,689
0.479
7,704 4,014.6
557.8
0.139
Totals
Latest:105,159.00
Dev:
0.92
Ultimate: 113,759.72
IBNR:
8,600.72
Mack S.E.:
859.63
CV(IBNR):
0.10

Call:
lm(formula = log(Y) ~ A + B, data = df)
Residuals:
Min
1Q-1.71387 -0.19797

Median0.06115

3Q
0.20978Max
1.29746

Coefficients:
Estimate Std. Error t value Pr(>|t|)(Intercept) 146.48471
61.81314
2.370
0.0216 *A-0.06916
0.03102 -2.229
0.0302 *B-0.74997
0.03244 -23.116<2e-16 ***--Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.4881 on 51 degrees of freedom(36 observations deleted due to missingness)
Multiple R-squared: 0.9257, Adjusted R-squared: 0.9228
F-statistic: 317.6 on 2 and 51 DF, p-value: < 2.2e-16> mY=mC> n=ncol(mC)> mY[,2:n]=mC[,2:n]-mC[,1:(n-1)]> mY0=mY[,-n]> Y=as.vector(mY0)> futur=is.na(Y)> A=rep(1988:1997,n-1)> B=rep(0:(n-2),each=n)> df=data.frame(Y,A,B,futur)> reg2=lm(log(Y)~as.factor(A)+as.factor(B),data=df)

11
!!

12
!!

> summary(reg2)> summary(reg3)Call:lm(formula = log(Y) ~ as.factor(A) + as.factor(B), data = df)

Call:glm(formula = Y ~ A + B, family = poisson(link = "log"), data = df)

Residuals:
Min
1Q-1.37436 -0.15434

Deviance Residuals:
Min
1Q-23.9887-6.8467Median0.005223Q
0.17412Max
1.22991

Median0.24443Q
4.5700Max
28.4783

Coefficients:

Coefficients:

Estimate Std. Error t value Pr(>|t|)(Intercept)
8.82410.2118 41.667 < 2e-16 ***as.factor(A)1989 -0.0580
0.2067 -0.281
0.7806as.factor(A)1990
0.22580.21621.045
0.3032
as.factor(A)1991 -0.2504
0.2265 -1.105
0.2763as.factor(A)1992 -0.1846
0.2388 -0.773
0.4446as.factor(A)1993 -0.3847
0.2544 -1.512
0.1392
as.factor(A)1994 -0.4954
0.2756 -1.798
0.0806 .
as.factor(A)1995 -0.3722
0.3070 -1.212
0.2333as.factor(A)1996 -0.3029
0.3610 -0.839
0.4069as.factor(A)1997 -0.6110
0.4870 -1.255
0.2176as.factor(B)1-0.4694
0.2067 -2.271
0.0292 *as.factor(B)2-1.48160.2162 -6.853 5.12e-08 ***as.factor(B)3-2.29370.2265 -10.126 4.44e-12 ***as.factor(B)4-2.84310.2388 -11.905 4.84e-14 ***as.factor(B)5-3.4300
0.2544 -13.482 1.22e-15 ***as.factor(B)6-4.63440.2756 -16.817 < 2e-16 ***as.factor(B)7-4.51150.3070 -14.695 < 2e-16 ***as.factor(B)8-6.7157
0.3610 -18.604 < 2e-16 ***--Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Estimate Std. Error z value Pr(>|z|)(Intercept) 111.387202
2.32320447.95<2e-16 ***A-0.051564
0.001166 -44.21<2e-16 ***B-0.697814
0.002702 -258.27<2e-16 ***--Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1(Dispersion parameter for poisson family taken to be 1)
Null deviance: 120911.7 on 53 degrees of freedom
Residual deviance:
5004.5 on 51 degrees of freedom(36 observations deleted due to missingness)
AIC: 5466.6
Number of Fisher Scoring iterations: 4> reg4=glm(Y~as.factor(A)+as.factor(B),+ data=df,family=poisson(link="log"))

Residual standard error: 0.4385 on 36 degrees of freedom(36 observations deleted due to missingness)
Multiple R-squared: 0.9577, Adjusted R-squared: 0.9377
F-statistic: 47.89 on 17 and 36 DF, p-value: < 2.2e-16> reg3=glm(Y~A+B,data=df,family=poisson(link="log"))

13
!!

14
!!

> summary(reg4)
Call:glm(formula = Y ~ as.factor(A) + as.factor(B), family = poisson(link ="log"),
data = df)
Deviance Residuals:
Min
1Q-12.2455-3.5148

Median-0.4767

3Q
3.5907Max
13.4573

Coefficients:
Estimate Std. Error z value Pr(>|z|)(Intercept)
8.674092
0.009635 900.279 < 2e-16 ***as.factor(A)1989 0.070939
0.012575
5.641 1.69e-08 ***as.factor(A)1990 0.253059
0.012063
20.978 < 2e-16 ***as.factor(A)1991 0.133791
0.012415
10.777 < 2e-16 ***as.factor(A)1992 -0.064441
0.013070-4.930 8.21e-07 ***as.factor(A)1993 -0.301073
0.014023 -21.470 < 2e-16 ***as.factor(A)1994 -0.207792
0.013788 -15.070 < 2e-16 ***as.factor(A)1995 -0.191317
0.013960 -13.705 < 2e-16 ***as.factor(A)1996 -0.148546
0.014393 -10.320 < 2e-16 ***as.factor(A)1997 -0.460981
0.019076 -24.165 < 2e-16 ***as.factor(B)1-0.467200
0.007149 -65.353 < 2e-16 ***as.factor(B)2-1.456867
0.010717 -135.937 < 2e-16 ***as.factor(B)3-2.276393
0.016140 -141.038 < 2e-16 ***as.factor(B)4-2.802747
0.021910 -127.921 < 2e-16 ***as.factor(B)5-3.378022
0.030769 -109.787 < 2e-16 ***as.factor(B)6-4.050147
0.046987 -86.198 < 2e-16 ***as.factor(B)7-4.418412
0.065228 -67.738 < 2e-16 ***as.factor(B)8-6.407605
0.223720 -28.641 < 2e-16 ***--Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1> sum(exp(predict(reg1,newdata=df)[futur]))[1] 7643.561> sum(exp(predict(reg2,newdata=df)[futur]))[1] 8060.825> sum(exp(predict(reg3,newdata=df)[futur]))[1] 9177.528> sum(exp(predict(reg4,newdata=df)[futur]))[1] 8600.721>> mP=intra$prime> df$P=rep(mP,n-1)>> reg5=glm(Y~as.factor(A)+as.factor(B)+offset(log(P)),+ data=df,family=poisson(link="log"))(Dispersion parameter for poisson family taken to be 1)
Null deviance: 120911.7 on 53 degrees of freedom
Residual deviance:
1558.2 on 36 degrees of freedom(36 observations deleted due to missingness)
AIC: 2050.4
Number of Fisher Scoring iterations: 5
15
!!

16
!!

> summary(reg5)
Call:glm(formula = Y ~ as.factor(A) + as.factor(B) + offset(log(P)),
family = poisson(link = "log"), data = df)
Deviance Residuals:
Min
1Q-12.2455-3.5148

Median-0.4767

3Q
3.5907Max
13.4573

Coefficients:
Estimate Std. Error z value Pr(>|z|)(Intercept)-0.998913
0.009635 -103.677 < 2e-16 ***as.factor(A)1989 0.021439
0.012575
1.705
0.0882 .
as.factor(A)1990 0.126327
0.012063
10.472 < 2e-16 ***as.factor(A)1991 0.016589
0.012415
1.336
0.1815as.factor(A)1992 -0.115139
0.013070-8.809 < 2e-16 ***as.factor(A)1993 -0.189910
0.014023 -13.543 < 2e-16 ***as.factor(A)1994 -0.156111
0.013788 -11.322 < 2e-16 ***as.factor(A)1995 -0.142813
0.013960 -10.230 < 2e-16 ***as.factor(A)1996 -0.121876
0.014393-8.468 < 2e-16 ***as.factor(A)1997 -0.113162
0.019076-5.932 2.99e-09 ***as.factor(B)1-0.467200
0.007149 -65.353 < 2e-16 ***as.factor(B)2-1.456867
0.010717 -135.937 < 2e-16 ***as.factor(B)3-2.276393
0.016140 -141.038 < 2e-16 ***as.factor(B)4-2.802747
0.021910 -127.921 < 2e-16 ***as.factor(B)5-3.378022
0.030769 -109.787 < 2e-16 ***as.factor(B)6-4.050147
0.046987 -86.198 < 2e-16 ***as.factor(B)7-4.418412
0.065228 -67.738 < 2e-16 ***as.factor(B)8-6.407605
0.223720 -28.641 < 2e-16 ***--Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1> reg6=glm(Y~as.factor(B)+offset(log(P)),data=df,+ family=poisson(link="log"))> summary(reg6)
Call:glm(formula = Y ~ as.factor(B) + offset(log(P)), family = poisson(link ="log"),
data = df)
Deviance Residuals:Min
1Q-14.9933-4.2664

Median-0.6501

3Q
4.3320Max
15.4796

Coefficients:
Estimate Std. Error z value Pr(>|z|)(Intercept)-1.053863
0.004284 -245.97<2e-16 ***as.factor(B)1 -0.462836
0.007055 -65.60<2e-16 ***as.factor(B)2 -1.444326
0.010592 -136.36<2e-16 ***as.factor(B)3 -2.251304
0.016014 -140.58<2e-16 ***as.factor(B)4 -2.759817
0.021780 -126.72<2e-16 ***as.factor(B)5 -3.308261
0.030646 -107.95<2e-16 ***as.factor(B)6 -3.951077
0.046872 -84.30<2e-16 ***as.factor(B)7 -4.309803
0.065098 -66.20<2e-16 ***as.factor(B)8 -6.341613
0.223648 -28.36<2e-16 ***--Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1(Dispersion parameter for poisson family taken to be 1)
Null deviance: 123798 on 53 degrees of freedom
Residual deviance:
2633 on 45 degrees of freedom(36 observations deleted due to missingness)
AIC: 3107.2(Dispersion parameter for poisson family taken to be 1)
Number of Fisher Scoring iterations: 5
Null deviance: 123797.6 on 53 degrees of freedom
Residual deviance:
1558.2 on 36 degrees of freedom(36 observations deleted due to missingness)
AIC: 2050.4> sum(exp(predict(reg6,newdata=df)[futur]))[1] 9511.738>> reg7=glm(Y/P~as.factor(B),weights=P,data=df,family=binomial)

Number of Fisher Scoring iterations: 5
17
!!

18
!!

> summary(reg7)---- PARTIE 3 ----Call:glm(formula = Y/P ~ as.factor(B), family = binomial, data = df,
weights = P)
Deviance Residuals:
Min
1Q-18.2392-4.7326

Median-0.76223Q
4.3798Max
18.0161

Coefficients:
Estimate Std. Error z value Pr(>|z|)(Intercept)-0.625250
0.005308 -117.78<2e-16 ***as.factor(B)1 -0.643713
0.008272 -77.82<2e-16 ***as.factor(B)2 -1.787127
0.011420 -156.49<2e-16 ***as.factor(B)3 -2.642534
0.016594 -159.25<2e-16 ***as.factor(B)4 -3.166117
0.022237 -142.38<2e-16 ***as.factor(B)5 -3.724041
0.030998 -120.14<2e-16 ***as.factor(B)6 -4.372963
0.047133 -92.78<2e-16 ***as.factor(B)7 -4.733722
0.065326 -72.46<2e-16 ***as.factor(B)8 -6.769612
0.223738 -30.26<2e-16 ***--Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1(Dispersion parameter for binomial family taken to be 1)
Null deviance: 141663.0 on 53 degrees of freedom
Residual deviance:
3202.2 on 45 degrees of freedom(36 observations deleted due to missingness)
AIC: 3668.7> DECES=read.table(+ "http://freakonometrics.free.fr/DECES-CAN.csv",header=TRUE,sep=";")> tail(DECES)DE
A
Y
772 84 147 105 2010
773 39 76 106 2010774 23 40 107 2010
775 15 20 108 2010776 7
8 109 2010
777 55 110 2010
> DECES[DECES$A==20,]> DECES[DECES$A==40,]> DECES[DECES$A==60,]> DECES[DECES$A==80,]> reg1=glm(D~as.factor(A)+Y+offset(log(E)),data=DECES,+ family=poisson(link="log"))> summary(reg1)
Call:glm(formula = D ~ as.factor(A) + Y + offset(log(E)), family =poisson(link = "log"),data = DECES)
Deviance Residuals:
Min
1Q
Median-42.979-2.853-0.608

3Q
1.939

Max
60.519Coefficients:
Number of Fisher Scoring iterations: 5> df1=df> df1$P=1> sum(predict(reg7,newdata=df1,type="response")[futur] * df$P[futur])[1] 9511.738

19!!

(Intercept)
as.factor(A)1
as.factor(A)2
as.factor(A)3
as.factor(A)4
as.factor(A)5
as.factor(A)6
as.factor(A)7
as.factor(A)8
as.factor(A)9Estimate Std. Error z value Pr(>|z|)2.649e+01 9.186e-02 288.393<2e-16 ***-2.623e+00 1.787e-02 -146.811<2e-16 ***-3.135e+00 2.280e-02 -137.495<2e-16 ***-3.372e+00 2.548e-02 -132.312<2e-16 ***-3.566e+00 2.796e-02 -127.526<2e-16 ***-3.588e+00 2.836e-02 -126.514<2e-16 ***-3.718e+00 3.029e-02 -122.770<2e-16 ***-3.869e+00 3.264e-02 -118.563<2e-16 ***-3.973e+00 3.435e-02 -115.663<2e-16 ***-4.035e+00 3.555e-02 -113.528<2e-16 ***20!!

as.factor(A)10
as.factor(A)11
as.factor(A)12
as.factor(A)13
as.factor(A)14
as.factor(A)15
as.factor(A)16
as.factor(A)17
as.factor(A)18
as.factor(A)19
as.factor(A)20
as.factor(A)21
as.factor(A)22
as.factor(A)23
as.factor(A)24
as.factor(A)25
as.factor(A)26
as.factor(A)27
as.factor(A)28
as.factor(A)29
as.factor(A)30
as.factor(A)31
as.factor(A)32
as.factor(A)33
as.factor(A)34
as.factor(A)35
as.factor(A)36
as.factor(A)37
as.factor(A)38
as.factor(A)39
as.factor(A)40
as.factor(A)41
as.factor(A)42
as.factor(A)43
as.factor(A)44
as.factor(A)45
as.factor(A)46
as.factor(A)47
as.factor(A)48
as.factor(A)49
as.factor(A)50
as.factor(A)51-4.058e+00-4.127e+00-3.992e+00-3.976e+00-3.706e+00-3.541e+00-3.227e+00-3.060e+00-2.926e+00-2.830e+00-2.795e+00-2.823e+00-2.888e+00-2.872e+00-2.923e+00-2.911e+00-2.907e+00-2.874e+00-2.916e+00-2.886e+00-2.854e+00-2.818e+00-2.788e+00-2.742e+00-2.648e+00-2.612e+00-2.546e+00-2.469e+00-2.382e+00-2.321e+00-2.237e+00-2.152e+00-2.039e+00-1.979e+00-1.891e+00-1.795e+00-1.681e+00-1.602e+00-1.492e+00-1.371e+00-1.301e+00-1.238e+00

3.622e-02 -112.050
3.766e-02 -109.592
3.527e-02 -113.181
3.495e-02 -113.741
3.062e-02 -121.003
2.833e-02 -124.982
2.441e-02 -132.164
2.255e-02 -135.661
2.113e-02 -138.484
2.021e-02 -140.058
1.999e-02 -139.849
2.036e-02 -138.685
2.102e-02 -137.360
2.083e-02 -137.838
2.132e-02 -137.143
2.122e-02 -137.181
2.126e-02 -136.779
2.097e-02 -137.034
2.144e-02 -136.019
2.122e-02 -135.988
2.100e-02 -135.863
2.070e-02 -136.177
2.045e-02 -136.351
2.011e-02 -136.353
1.930e-02 -137.218
1.902e-02 -137.339
1.853e-02 -137.379
1.799e-02 -137.210
1.738e-02 -137.061
1.701e-02 -136.458
1.649e-02 -135.686
1.595e-02 -134.895
1.525e-02 -133.694
1.496e-02 -132.317
1.446e-02 -130.814
1.392e-02 -128.981
1.334e-02 -125.979
1.303e-02 -122.918
1.256e-02 -118.802
1.206e-02 -113.757
1.182e-02 -109.982
1.161e-02 -106.611
21!!

<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***as.factor(A)52
as.factor(A)53
as.factor(A)54
as.factor(A)55
as.factor(A)56
as.factor(A)57
as.factor(A)58
as.factor(A)59
as.factor(A)60
as.factor(A)61
as.factor(A)62
as.factor(A)63
as.factor(A)64
as.factor(A)65
as.factor(A)66
as.factor(A)67
as.factor(A)68
as.factor(A)69
as.factor(A)70
as.factor(A)71
as.factor(A)72
as.factor(A)73
as.factor(A)74
as.factor(A)75
as.factor(A)76
as.factor(A)77
as.factor(A)78
as.factor(A)79
as.factor(A)80
as.factor(A)81
as.factor(A)82
as.factor(A)83
as.factor(A)84
as.factor(A)85
as.factor(A)86
as.factor(A)87
as.factor(A)88
as.factor(A)89
as.factor(A)90
as.factor(A)91
as.factor(A)92
as.factor(A)93-1.114e+00-1.029e+00-9.142e-01-8.511e-01-7.691e-01-6.683e-01-5.587e-01-4.648e-01-3.869e-01-3.073e-01-1.980e-01-1.323e-01-1.563e-02
7.591e-02
1.467e-01
2.320e-01
3.279e-01
4.324e-01
5.107e-01
5.712e-01
6.990e-01
7.894e-01
8.836e-01
9.676e-01
1.059e+00
1.146e+00
1.271e+00
1.378e+00
1.453e+00
1.543e+00
1.646e+00
1.749e+00
1.874e+00
1.959e+00
2.070e+00
2.159e+00
2.267e+00
2.374e+00
2.455e+00
2.556e+00
2.662e+00
2.764e+00

1.113e-02 -100.128
1.088e-02 -94.560
1.051e-02 -86.986
1.035e-02 -82.240
1.012e-02 -76.001
9.840e-03 -67.914
9.548e-03 -58.516
9.342e-03 -49.758
9.207e-03 -42.022
9.042e-03 -33.980
8.780e-03 -22.547
8.681e-03 -15.240
8.431e-03-1.8548.262e-03
9.188
8.161e-03
17.9738.031e-03
28.891
7.895e-03
41.5287.768e-03
55.6597.725e-03
66.1097.706e-03
74.1187.516e-03
93.0007.419e-03 106.396
7.327e-03 120.588
7.282e-03 132.867
7.238e-03 146.259
7.211e-03 158.902
7.119e-03 178.563
7.099e-03 194.088
7.169e-03 202.679
7.187e-03 214.687
7.196e-03 228.677
7.198e-03 242.927
7.179e-03 261.090
7.299e-03 268.420
7.392e-03 280.018
7.578e-03 284.909
7.739e-03 292.986
7.980e-03 297.484
8.399e-03 292.272
8.754e-03 291.982
9.207e-03 289.081
9.772e-03 282.813
22!!

<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***0.0637 .
<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***<2e-16 ***as.factor(A)94
2.839e+00 1.057e-02 268.734<2e-16 ***as.factor(A)95
2.947e+00 1.144e-02 257.572<2e-16 ***as.factor(A)96
3.026e+00 1.273e-02 237.685<2e-16 ***as.factor(A)97
3.125e+00 1.432e-02 218.195<2e-16 ***as.factor(A)98
3.254e+00 1.617e-02 201.188<2e-16 ***as.factor(A)99
3.212e+00 1.992e-02 161.250<2e-16 ***as.factor(A)100 3.377e+00 2.273e-02 148.581<2e-16 ***as.factor(A)101 3.466e+00 2.721e-02 127.372<2e-16 ***as.factor(A)102 3.438e+00 3.528e-02
97.440<2e-16 ***as.factor(A)103 3.586e+00 4.256e-02
84.254<2e-16 ***as.factor(A)104 3.600e+00 5.593e-02
64.369<2e-16 ***as.factor(A)105 3.800e+00 6.836e-02
55.589<2e-16 ***as.factor(A)106 3.686e+00 9.503e-02
38.789<2e-16 ***as.factor(A)107 3.739e+00 1.241e-01
30.125<2e-16 ***as.factor(A)108 3.738e+00 1.667e-01
22.419<2e-16 ***as.factor(A)109 3.735e+00 2.236e-01
16.703<2e-16 ***as.factor(A)110 3.505e+00 2.423e-01
14.464<2e-16 ***Y-1.544e-02 4.648e-05 -332.097<2e-16 ***--Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1(Dispersion parameter for poisson family taken to be 1)
Null deviance: 3445251
Residual deviance:
21594
AIC: 28187

on 776
on 665> brk=c(12,20,30)> positive1=function(x) ifelse(x<brk[1],brk[1]-x,0)> positive2=function(x) ifelse(x<brk[2],brk[2]-x,0)> positive3=function(x) ifelse(x<brk[3],brk[3]-x,0)>> reg2=glm(D~A+positive1(A)+positive2(A)+positive3(A)+Y++ offset(log(E)),data=DECES,family=poisson(link="log"))

degrees of freedom
degrees of freedom

Number of Fisher Scoring iterations: 4> coefA=c(0,coefficients(reg1)[2:111])+coefficients(reg1)[1]> plot(0:110,coefA)

23
!!

24
!!

> summary(reg2)> plot(0:110,predict(reg2,newdata=nd))Call:glm(formula = D ~ A + positive1(A) + positive2(A) + positive3(A) +Y + offset(log(E)), family = poisson(link = "log"), data = DECES)
Deviance Residuals:
Min
1Q
Median-51.631-3.229-0.473

3Q
3.120

Max
125.758

Coefficients:
Estimate Std. Error z value Pr(>|z|)(Intercept)
2.025e+01 9.144e-02
221.5<2e-16 ***A
9.320e-02 6.599e-05 1412.3<2e-16 ***positive1(A) 8.934e-01 4.069e-03
219.6<2e-16 ***positive2(A) -5.152e-01 3.083e-03 -167.1<2e-16 ***positive3(A) 1.631e-01 9.309e-04
175.2<2e-16 ***Y-1.531e-02 4.632e-05 -330.4<2e-16 ***--Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1> reg3=glm(D/E~A+positive1(A)+positive2(A)+positive3(A)+Y,+ data=DECES,weights=E,family=binomial(link="logit"))(Dispersion parameter for poisson family taken to be 1)
Null deviance: 3445251
Residual deviance:
83077
AIC: 89458

on 776
on 771degrees of freedom
degrees of freedom

Number of Fisher Scoring iterations: 5> nd=data.frame(A=0:110,Y=0,E=1)

25
!!

26
!!

> summary(reg3)> nd=data.frame(A=0:110,Y=0,E=1)> plot(0:110,predict(reg4,newdata=nd))Call:glm(formula = D/E ~ A + positive1(A) + positive2(A) + positive3(A) +Y, family = binomial(link = "logit"), data = DECES, weights = E)
Deviance Residuals:
Min
1Q
Median-52.293-2.748
0.550

3Q
4.211

Max
125.924

Coefficients:
Estimate Std. Error z value Pr(>|z|)(Intercept)
2.159e+01 9.457e-02
228.3<2e-16 ***A
9.741e-02 7.023e-05 1387.1<2e-16 ***positive1(A) 9.015e-01 4.079e-03
221.0<2e-16 ***positive2(A) -5.350e-01 3.088e-03 -173.3<2e-16 ***positive3(A) 1.810e-01 9.366e-04
193.2<2e-16 ***Y-1.610e-02 4.795e-05 -335.8<2e-16 ***--Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1(Dispersion parameter for binomial family taken to be 1)
Null deviance: 3519013
Residual deviance:
87176
AIC: 93459

on 776
on 771degrees of freedom
degrees of freedom

Number of Fisher Scoring iterations: 5

27
!!

28
!!